]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/amba/bus.c
scm: lower SCM_MAX_FD
[net-next-2.6.git] / drivers / amba / bus.c
CommitLineData
1da177e4
LT
1/*
2 * linux/arch/arm/common/amba.c
3 *
4 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/device.h>
4e57b681
TS
13#include <linux/string.h>
14#include <linux/slab.h>
934848da 15#include <linux/io.h>
a62c80e5 16#include <linux/amba/bus.h>
1da177e4 17
934848da 18#include <asm/irq.h>
1da177e4
LT
19#include <asm/sizes.h>
20
21#define to_amba_device(d) container_of(d, struct amba_device, dev)
22#define to_amba_driver(d) container_of(d, struct amba_driver, drv)
23
24static struct amba_id *
25amba_lookup(struct amba_id *table, struct amba_device *dev)
26{
27 int ret = 0;
28
29 while (table->mask) {
30 ret = (dev->periphid & table->mask) == table->id;
31 if (ret)
32 break;
33 table++;
34 }
35
36 return ret ? table : NULL;
37}
38
39static int amba_match(struct device *dev, struct device_driver *drv)
40{
41 struct amba_device *pcdev = to_amba_device(dev);
42 struct amba_driver *pcdrv = to_amba_driver(drv);
43
44 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
45}
46
47#ifdef CONFIG_HOTPLUG
7eff2e7a 48static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4
LT
49{
50 struct amba_device *pcdev = to_amba_device(dev);
7eff2e7a 51 int retval = 0;
1da177e4 52
7eff2e7a 53 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
bf62456e 54 return retval;
1da177e4
LT
55}
56#else
6d20b035 57#define amba_uevent NULL
1da177e4
LT
58#endif
59
60static int amba_suspend(struct device *dev, pm_message_t state)
61{
62 struct amba_driver *drv = to_amba_driver(dev->driver);
63 int ret = 0;
64
65 if (dev->driver && drv->suspend)
66 ret = drv->suspend(to_amba_device(dev), state);
67 return ret;
68}
69
70static int amba_resume(struct device *dev)
71{
72 struct amba_driver *drv = to_amba_driver(dev->driver);
73 int ret = 0;
74
75 if (dev->driver && drv->resume)
76 ret = drv->resume(to_amba_device(dev));
77 return ret;
78}
79
96b13f5c
RK
80#define amba_attr_func(name,fmt,arg...) \
81static ssize_t name##_show(struct device *_dev, \
82 struct device_attribute *attr, char *buf) \
83{ \
84 struct amba_device *dev = to_amba_device(_dev); \
85 return sprintf(buf, fmt, arg); \
86}
87
88#define amba_attr(name,fmt,arg...) \
89amba_attr_func(name,fmt,arg) \
90static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
91
92amba_attr_func(id, "%08x\n", dev->periphid);
93amba_attr(irq0, "%u\n", dev->irq[0]);
94amba_attr(irq1, "%u\n", dev->irq[1]);
95amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
96 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
97 dev->res.flags);
98
99static struct device_attribute amba_dev_attrs[] = {
100 __ATTR_RO(id),
101 __ATTR_RO(resource),
102 __ATTR_NULL,
103};
104
1da177e4
LT
105/*
106 * Primecells are part of the Advanced Microcontroller Bus Architecture,
107 * so we call the bus "amba".
108 */
109static struct bus_type amba_bustype = {
110 .name = "amba",
96b13f5c 111 .dev_attrs = amba_dev_attrs,
1da177e4 112 .match = amba_match,
6d20b035 113 .uevent = amba_uevent,
1da177e4
LT
114 .suspend = amba_suspend,
115 .resume = amba_resume,
116};
117
118static int __init amba_init(void)
119{
120 return bus_register(&amba_bustype);
121}
122
123postcore_initcall(amba_init);
124
7cfe2494
RK
125static int amba_get_enable_pclk(struct amba_device *pcdev)
126{
127 struct clk *pclk = clk_get(&pcdev->dev, "apb_pclk");
128 int ret;
129
130 pcdev->pclk = pclk;
131
132 if (IS_ERR(pclk))
133 return PTR_ERR(pclk);
134
135 ret = clk_enable(pclk);
136 if (ret)
137 clk_put(pclk);
138
139 return ret;
140}
141
142static void amba_put_disable_pclk(struct amba_device *pcdev)
143{
144 struct clk *pclk = pcdev->pclk;
145
146 clk_disable(pclk);
147 clk_put(pclk);
148}
149
1da177e4
LT
150/*
151 * These are the device model conversion veneers; they convert the
152 * device model structures to our more specific structures.
153 */
154static int amba_probe(struct device *dev)
155{
156 struct amba_device *pcdev = to_amba_device(dev);
157 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
7cfe2494
RK
158 struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
159 int ret;
1da177e4 160
7cfe2494
RK
161 do {
162 ret = amba_get_enable_pclk(pcdev);
163 if (ret)
164 break;
165
166 ret = pcdrv->probe(pcdev, id);
167 if (ret == 0)
168 break;
1da177e4 169
7cfe2494
RK
170 amba_put_disable_pclk(pcdev);
171 } while (0);
172
173 return ret;
1da177e4
LT
174}
175
176static int amba_remove(struct device *dev)
177{
7cfe2494 178 struct amba_device *pcdev = to_amba_device(dev);
1da177e4 179 struct amba_driver *drv = to_amba_driver(dev->driver);
7cfe2494
RK
180 int ret = drv->remove(pcdev);
181
182 amba_put_disable_pclk(pcdev);
183
184 return ret;
1da177e4
LT
185}
186
187static void amba_shutdown(struct device *dev)
188{
189 struct amba_driver *drv = to_amba_driver(dev->driver);
190 drv->shutdown(to_amba_device(dev));
191}
192
193/**
194 * amba_driver_register - register an AMBA device driver
195 * @drv: amba device driver structure
196 *
197 * Register an AMBA device driver with the Linux device model
198 * core. If devices pre-exist, the drivers probe function will
199 * be called.
200 */
201int amba_driver_register(struct amba_driver *drv)
202{
203 drv->drv.bus = &amba_bustype;
204
205#define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
206 SETFN(probe);
207 SETFN(remove);
208 SETFN(shutdown);
209
210 return driver_register(&drv->drv);
211}
212
213/**
214 * amba_driver_unregister - remove an AMBA device driver
215 * @drv: AMBA device driver structure to remove
216 *
217 * Unregister an AMBA device driver from the Linux device
218 * model. The device model will call the drivers remove function
219 * for each device the device driver is currently handling.
220 */
221void amba_driver_unregister(struct amba_driver *drv)
222{
223 driver_unregister(&drv->drv);
224}
225
226
227static void amba_device_release(struct device *dev)
228{
229 struct amba_device *d = to_amba_device(dev);
230
231 if (d->res.parent)
232 release_resource(&d->res);
233 kfree(d);
234}
235
1da177e4
LT
236/**
237 * amba_device_register - register an AMBA device
238 * @dev: AMBA device to register
239 * @parent: parent memory resource
240 *
241 * Setup the AMBA device, reading the cell ID if present.
242 * Claim the resource, and register the AMBA device with
243 * the Linux device manager.
244 */
245int amba_device_register(struct amba_device *dev, struct resource *parent)
246{
8afe0b96 247 u32 size;
1da177e4
LT
248 void __iomem *tmp;
249 int i, ret;
250
557dca5f
RK
251 device_initialize(&dev->dev);
252
253 /*
254 * Copy from device_add
255 */
256 if (dev->dev.init_name) {
257 dev_set_name(&dev->dev, "%s", dev->dev.init_name);
258 dev->dev.init_name = NULL;
259 }
260
1da177e4
LT
261 dev->dev.release = amba_device_release;
262 dev->dev.bus = &amba_bustype;
263 dev->dev.dma_mask = &dev->dma_mask;
9d6b4c82 264 dev->res.name = dev_name(&dev->dev);
1da177e4
LT
265
266 if (!dev->dev.coherent_dma_mask && dev->dma_mask)
267 dev_warn(&dev->dev, "coherent dma mask is unset\n");
268
269 ret = request_resource(parent, &dev->res);
96b13f5c
RK
270 if (ret)
271 goto err_out;
272
8afe0b96
LC
273 /*
274 * Dynamically calculate the size of the resource
275 * and use this for iomap
276 */
277 size = resource_size(&dev->res);
278 tmp = ioremap(dev->res.start, size);
96b13f5c
RK
279 if (!tmp) {
280 ret = -ENOMEM;
281 goto err_release;
1da177e4 282 }
96b13f5c 283
7cfe2494
RK
284 ret = amba_get_enable_pclk(dev);
285 if (ret == 0) {
286 u32 pid, cid;
96b13f5c 287
7cfe2494
RK
288 /*
289 * Read pid and cid based on size of resource
290 * they are located at end of region
291 */
292 for (pid = 0, i = 0; i < 4; i++)
293 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
294 (i * 8);
295 for (cid = 0, i = 0; i < 4; i++)
296 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
297 (i * 8);
96b13f5c 298
7cfe2494 299 amba_put_disable_pclk(dev);
96b13f5c 300
01723a95 301 if (cid == AMBA_CID)
7cfe2494
RK
302 dev->periphid = pid;
303
304 if (!dev->periphid)
305 ret = -ENODEV;
96b13f5c
RK
306 }
307
7cfe2494
RK
308 iounmap(tmp);
309
310 if (ret)
311 goto err_release;
312
557dca5f 313 ret = device_add(&dev->dev);
96b13f5c
RK
314 if (ret)
315 goto err_release;
316
317 if (dev->irq[0] != NO_IRQ)
318 ret = device_create_file(&dev->dev, &dev_attr_irq0);
319 if (ret == 0 && dev->irq[1] != NO_IRQ)
320 ret = device_create_file(&dev->dev, &dev_attr_irq1);
321 if (ret == 0)
322 return ret;
323
324 device_unregister(&dev->dev);
325
326 err_release:
327 release_resource(&dev->res);
328 err_out:
1da177e4
LT
329 return ret;
330}
331
332/**
333 * amba_device_unregister - unregister an AMBA device
334 * @dev: AMBA device to remove
335 *
336 * Remove the specified AMBA device from the Linux device
337 * manager. All files associated with this object will be
338 * destroyed, and device drivers notified that the device has
339 * been removed. The AMBA device's resources including
340 * the amba_device structure will be freed once all
341 * references to it have been dropped.
342 */
343void amba_device_unregister(struct amba_device *dev)
344{
345 device_unregister(&dev->dev);
346}
347
348
349struct find_data {
350 struct amba_device *dev;
351 struct device *parent;
352 const char *busid;
353 unsigned int id;
354 unsigned int mask;
355};
356
357static int amba_find_match(struct device *dev, void *data)
358{
359 struct find_data *d = data;
360 struct amba_device *pcdev = to_amba_device(dev);
361 int r;
362
363 r = (pcdev->periphid & d->mask) == d->id;
364 if (d->parent)
365 r &= d->parent == dev->parent;
366 if (d->busid)
9d6b4c82 367 r &= strcmp(dev_name(dev), d->busid) == 0;
1da177e4
LT
368
369 if (r) {
370 get_device(dev);
371 d->dev = pcdev;
372 }
373
374 return r;
375}
376
377/**
378 * amba_find_device - locate an AMBA device given a bus id
379 * @busid: bus id for device (or NULL)
380 * @parent: parent device (or NULL)
381 * @id: peripheral ID (or 0)
382 * @mask: peripheral ID mask (or 0)
383 *
384 * Return the AMBA device corresponding to the supplied parameters.
385 * If no device matches, returns NULL.
386 *
387 * NOTE: When a valid device is found, its refcount is
388 * incremented, and must be decremented before the returned
389 * reference.
390 */
391struct amba_device *
392amba_find_device(const char *busid, struct device *parent, unsigned int id,
393 unsigned int mask)
394{
395 struct find_data data;
396
397 data.dev = NULL;
398 data.parent = parent;
399 data.busid = busid;
400 data.id = id;
401 data.mask = mask;
402
403 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
404
405 return data.dev;
406}
407
408/**
409 * amba_request_regions - request all mem regions associated with device
410 * @dev: amba_device structure for device
411 * @name: name, or NULL to use driver name
412 */
413int amba_request_regions(struct amba_device *dev, const char *name)
414{
415 int ret = 0;
8afe0b96 416 u32 size;
1da177e4
LT
417
418 if (!name)
419 name = dev->dev.driver->name;
420
8afe0b96
LC
421 size = resource_size(&dev->res);
422
423 if (!request_mem_region(dev->res.start, size, name))
1da177e4
LT
424 ret = -EBUSY;
425
426 return ret;
427}
428
429/**
430 * amba_release_regions - release mem regions assoicated with device
431 * @dev: amba_device structure for device
432 *
433 * Release regions claimed by a successful call to amba_request_regions.
434 */
435void amba_release_regions(struct amba_device *dev)
436{
8afe0b96
LC
437 u32 size;
438
439 size = resource_size(&dev->res);
440 release_mem_region(dev->res.start, size);
1da177e4
LT
441}
442
443EXPORT_SYMBOL(amba_driver_register);
444EXPORT_SYMBOL(amba_driver_unregister);
445EXPORT_SYMBOL(amba_device_register);
446EXPORT_SYMBOL(amba_device_unregister);
447EXPORT_SYMBOL(amba_find_device);
448EXPORT_SYMBOL(amba_request_regions);
449EXPORT_SYMBOL(amba_release_regions);