2 * linux/arch/arm/common/amba.c
4 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
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.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
16 #include <linux/amba/bus.h>
19 #include <asm/sizes.h>
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)
24 static struct amba_id
*
25 amba_lookup(struct amba_id
*table
, struct amba_device
*dev
)
30 ret
= (dev
->periphid
& table
->mask
) == table
->id
;
36 return ret
? table
: NULL
;
39 static int amba_match(struct device
*dev
, struct device_driver
*drv
)
41 struct amba_device
*pcdev
= to_amba_device(dev
);
42 struct amba_driver
*pcdrv
= to_amba_driver(drv
);
44 return amba_lookup(pcdrv
->id_table
, pcdev
) != NULL
;
48 static int amba_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
50 struct amba_device
*pcdev
= to_amba_device(dev
);
53 retval
= add_uevent_var(env
, "AMBA_ID=%08x", pcdev
->periphid
);
57 #define amba_uevent NULL
60 static int amba_suspend(struct device
*dev
, pm_message_t state
)
62 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
65 if (dev
->driver
&& drv
->suspend
)
66 ret
= drv
->suspend(to_amba_device(dev
), state
);
70 static int amba_resume(struct device
*dev
)
72 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
75 if (dev
->driver
&& drv
->resume
)
76 ret
= drv
->resume(to_amba_device(dev
));
80 #define amba_attr_func(name,fmt,arg...) \
81 static ssize_t name##_show(struct device *_dev, \
82 struct device_attribute *attr, char *buf) \
84 struct amba_device *dev = to_amba_device(_dev); \
85 return sprintf(buf, fmt, arg); \
88 #define amba_attr(name,fmt,arg...) \
89 amba_attr_func(name,fmt,arg) \
90 static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
92 amba_attr_func(id
, "%08x\n", dev
->periphid
);
93 amba_attr(irq0
, "%u\n", dev
->irq
[0]);
94 amba_attr(irq1
, "%u\n", dev
->irq
[1]);
95 amba_attr_func(resource
, "\t%016llx\t%016llx\t%016lx\n",
96 (unsigned long long)dev
->res
.start
, (unsigned long long)dev
->res
.end
,
99 static struct device_attribute amba_dev_attrs
[] = {
106 * Primecells are part of the Advanced Microcontroller Bus Architecture,
107 * so we call the bus "amba".
109 static struct bus_type amba_bustype
= {
111 .dev_attrs
= amba_dev_attrs
,
113 .uevent
= amba_uevent
,
114 .suspend
= amba_suspend
,
115 .resume
= amba_resume
,
118 static int __init
amba_init(void)
120 return bus_register(&amba_bustype
);
123 postcore_initcall(amba_init
);
125 static int amba_get_enable_pclk(struct amba_device
*pcdev
)
127 struct clk
*pclk
= clk_get(&pcdev
->dev
, "apb_pclk");
133 return PTR_ERR(pclk
);
135 ret
= clk_enable(pclk
);
142 static void amba_put_disable_pclk(struct amba_device
*pcdev
)
144 struct clk
*pclk
= pcdev
->pclk
;
150 static int amba_get_enable_vcore(struct amba_device
*pcdev
)
152 struct regulator
*vcore
= regulator_get(&pcdev
->dev
, "vcore");
155 pcdev
->vcore
= vcore
;
158 /* It is OK not to supply a vcore regulator */
159 if (PTR_ERR(vcore
) == -ENODEV
)
161 return PTR_ERR(vcore
);
164 ret
= regulator_enable(vcore
);
166 regulator_put(vcore
);
167 pcdev
->vcore
= ERR_PTR(-ENODEV
);
173 static void amba_put_disable_vcore(struct amba_device
*pcdev
)
175 struct regulator
*vcore
= pcdev
->vcore
;
177 if (!IS_ERR(vcore
)) {
178 regulator_disable(vcore
);
179 regulator_put(vcore
);
184 * These are the device model conversion veneers; they convert the
185 * device model structures to our more specific structures.
187 static int amba_probe(struct device
*dev
)
189 struct amba_device
*pcdev
= to_amba_device(dev
);
190 struct amba_driver
*pcdrv
= to_amba_driver(dev
->driver
);
191 struct amba_id
*id
= amba_lookup(pcdrv
->id_table
, pcdev
);
195 ret
= amba_get_enable_vcore(pcdev
);
199 ret
= amba_get_enable_pclk(pcdev
);
203 ret
= pcdrv
->probe(pcdev
, id
);
207 amba_put_disable_pclk(pcdev
);
208 amba_put_disable_vcore(pcdev
);
214 static int amba_remove(struct device
*dev
)
216 struct amba_device
*pcdev
= to_amba_device(dev
);
217 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
218 int ret
= drv
->remove(pcdev
);
220 amba_put_disable_pclk(pcdev
);
221 amba_put_disable_vcore(pcdev
);
226 static void amba_shutdown(struct device
*dev
)
228 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
229 drv
->shutdown(to_amba_device(dev
));
233 * amba_driver_register - register an AMBA device driver
234 * @drv: amba device driver structure
236 * Register an AMBA device driver with the Linux device model
237 * core. If devices pre-exist, the drivers probe function will
240 int amba_driver_register(struct amba_driver
*drv
)
242 drv
->drv
.bus
= &amba_bustype
;
244 #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
249 return driver_register(&drv
->drv
);
253 * amba_driver_unregister - remove an AMBA device driver
254 * @drv: AMBA device driver structure to remove
256 * Unregister an AMBA device driver from the Linux device
257 * model. The device model will call the drivers remove function
258 * for each device the device driver is currently handling.
260 void amba_driver_unregister(struct amba_driver
*drv
)
262 driver_unregister(&drv
->drv
);
266 static void amba_device_release(struct device
*dev
)
268 struct amba_device
*d
= to_amba_device(dev
);
271 release_resource(&d
->res
);
276 * amba_device_register - register an AMBA device
277 * @dev: AMBA device to register
278 * @parent: parent memory resource
280 * Setup the AMBA device, reading the cell ID if present.
281 * Claim the resource, and register the AMBA device with
282 * the Linux device manager.
284 int amba_device_register(struct amba_device
*dev
, struct resource
*parent
)
290 device_initialize(&dev
->dev
);
293 * Copy from device_add
295 if (dev
->dev
.init_name
) {
296 dev_set_name(&dev
->dev
, "%s", dev
->dev
.init_name
);
297 dev
->dev
.init_name
= NULL
;
300 dev
->dev
.release
= amba_device_release
;
301 dev
->dev
.bus
= &amba_bustype
;
302 dev
->dev
.dma_mask
= &dev
->dma_mask
;
303 dev
->res
.name
= dev_name(&dev
->dev
);
305 if (!dev
->dev
.coherent_dma_mask
&& dev
->dma_mask
)
306 dev_warn(&dev
->dev
, "coherent dma mask is unset\n");
308 ret
= request_resource(parent
, &dev
->res
);
313 * Dynamically calculate the size of the resource
314 * and use this for iomap
316 size
= resource_size(&dev
->res
);
317 tmp
= ioremap(dev
->res
.start
, size
);
323 ret
= amba_get_enable_pclk(dev
);
328 * Read pid and cid based on size of resource
329 * they are located at end of region
331 for (pid
= 0, i
= 0; i
< 4; i
++)
332 pid
|= (readl(tmp
+ size
- 0x20 + 4 * i
) & 255) <<
334 for (cid
= 0, i
= 0; i
< 4; i
++)
335 cid
|= (readl(tmp
+ size
- 0x10 + 4 * i
) & 255) <<
338 amba_put_disable_pclk(dev
);
352 ret
= device_add(&dev
->dev
);
356 if (dev
->irq
[0] != NO_IRQ
)
357 ret
= device_create_file(&dev
->dev
, &dev_attr_irq0
);
358 if (ret
== 0 && dev
->irq
[1] != NO_IRQ
)
359 ret
= device_create_file(&dev
->dev
, &dev_attr_irq1
);
363 device_unregister(&dev
->dev
);
366 release_resource(&dev
->res
);
372 * amba_device_unregister - unregister an AMBA device
373 * @dev: AMBA device to remove
375 * Remove the specified AMBA device from the Linux device
376 * manager. All files associated with this object will be
377 * destroyed, and device drivers notified that the device has
378 * been removed. The AMBA device's resources including
379 * the amba_device structure will be freed once all
380 * references to it have been dropped.
382 void amba_device_unregister(struct amba_device
*dev
)
384 device_unregister(&dev
->dev
);
389 struct amba_device
*dev
;
390 struct device
*parent
;
396 static int amba_find_match(struct device
*dev
, void *data
)
398 struct find_data
*d
= data
;
399 struct amba_device
*pcdev
= to_amba_device(dev
);
402 r
= (pcdev
->periphid
& d
->mask
) == d
->id
;
404 r
&= d
->parent
== dev
->parent
;
406 r
&= strcmp(dev_name(dev
), d
->busid
) == 0;
417 * amba_find_device - locate an AMBA device given a bus id
418 * @busid: bus id for device (or NULL)
419 * @parent: parent device (or NULL)
420 * @id: peripheral ID (or 0)
421 * @mask: peripheral ID mask (or 0)
423 * Return the AMBA device corresponding to the supplied parameters.
424 * If no device matches, returns NULL.
426 * NOTE: When a valid device is found, its refcount is
427 * incremented, and must be decremented before the returned
431 amba_find_device(const char *busid
, struct device
*parent
, unsigned int id
,
434 struct find_data data
;
437 data
.parent
= parent
;
442 bus_for_each_dev(&amba_bustype
, NULL
, &data
, amba_find_match
);
448 * amba_request_regions - request all mem regions associated with device
449 * @dev: amba_device structure for device
450 * @name: name, or NULL to use driver name
452 int amba_request_regions(struct amba_device
*dev
, const char *name
)
458 name
= dev
->dev
.driver
->name
;
460 size
= resource_size(&dev
->res
);
462 if (!request_mem_region(dev
->res
.start
, size
, name
))
469 * amba_release_regions - release mem regions assoicated with device
470 * @dev: amba_device structure for device
472 * Release regions claimed by a successful call to amba_request_regions.
474 void amba_release_regions(struct amba_device
*dev
)
478 size
= resource_size(&dev
->res
);
479 release_mem_region(dev
->res
.start
, size
);
482 EXPORT_SYMBOL(amba_driver_register
);
483 EXPORT_SYMBOL(amba_driver_unregister
);
484 EXPORT_SYMBOL(amba_device_register
);
485 EXPORT_SYMBOL(amba_device_unregister
);
486 EXPORT_SYMBOL(amba_find_device
);
487 EXPORT_SYMBOL(amba_request_regions
);
488 EXPORT_SYMBOL(amba_release_regions
);