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>
16 #include <asm/hardware/amba.h>
17 #include <asm/sizes.h>
19 #define to_amba_device(d) container_of(d, struct amba_device, dev)
20 #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
22 static struct amba_id
*
23 amba_lookup(struct amba_id
*table
, struct amba_device
*dev
)
28 ret
= (dev
->periphid
& table
->mask
) == table
->id
;
34 return ret
? table
: NULL
;
37 static int amba_match(struct device
*dev
, struct device_driver
*drv
)
39 struct amba_device
*pcdev
= to_amba_device(dev
);
40 struct amba_driver
*pcdrv
= to_amba_driver(drv
);
42 return amba_lookup(pcdrv
->id_table
, pcdev
) != NULL
;
46 static int amba_hotplug(struct device
*dev
, char **envp
, int nr_env
, char *buf
, int bufsz
)
48 struct amba_device
*pcdev
= to_amba_device(dev
);
53 snprintf(buf
, bufsz
, "AMBA_ID=%08x", pcdev
->periphid
);
59 #define amba_hotplug NULL
62 static int amba_suspend(struct device
*dev
, pm_message_t state
)
64 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
67 if (dev
->driver
&& drv
->suspend
)
68 ret
= drv
->suspend(to_amba_device(dev
), state
);
72 static int amba_resume(struct device
*dev
)
74 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
77 if (dev
->driver
&& drv
->resume
)
78 ret
= drv
->resume(to_amba_device(dev
));
83 * Primecells are part of the Advanced Microcontroller Bus Architecture,
84 * so we call the bus "amba".
86 static struct bus_type amba_bustype
= {
89 .hotplug
= amba_hotplug
,
90 .suspend
= amba_suspend
,
91 .resume
= amba_resume
,
94 static int __init
amba_init(void)
96 return bus_register(&amba_bustype
);
99 postcore_initcall(amba_init
);
102 * These are the device model conversion veneers; they convert the
103 * device model structures to our more specific structures.
105 static int amba_probe(struct device
*dev
)
107 struct amba_device
*pcdev
= to_amba_device(dev
);
108 struct amba_driver
*pcdrv
= to_amba_driver(dev
->driver
);
111 id
= amba_lookup(pcdrv
->id_table
, pcdev
);
113 return pcdrv
->probe(pcdev
, id
);
116 static int amba_remove(struct device
*dev
)
118 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
119 return drv
->remove(to_amba_device(dev
));
122 static void amba_shutdown(struct device
*dev
)
124 struct amba_driver
*drv
= to_amba_driver(dev
->driver
);
125 drv
->shutdown(to_amba_device(dev
));
129 * amba_driver_register - register an AMBA device driver
130 * @drv: amba device driver structure
132 * Register an AMBA device driver with the Linux device model
133 * core. If devices pre-exist, the drivers probe function will
136 int amba_driver_register(struct amba_driver
*drv
)
138 drv
->drv
.bus
= &amba_bustype
;
140 #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
145 return driver_register(&drv
->drv
);
149 * amba_driver_unregister - remove an AMBA device driver
150 * @drv: AMBA device driver structure to remove
152 * Unregister an AMBA device driver from the Linux device
153 * model. The device model will call the drivers remove function
154 * for each device the device driver is currently handling.
156 void amba_driver_unregister(struct amba_driver
*drv
)
158 driver_unregister(&drv
->drv
);
162 static void amba_device_release(struct device
*dev
)
164 struct amba_device
*d
= to_amba_device(dev
);
167 release_resource(&d
->res
);
171 #define amba_attr(name,fmt,arg...) \
172 static ssize_t show_##name(struct device *_dev, char *buf) \
174 struct amba_device *dev = to_amba_device(_dev); \
175 return sprintf(buf, fmt, arg); \
177 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
179 amba_attr(id
, "%08x\n", dev
->periphid
);
180 amba_attr(irq0
, "%u\n", dev
->irq
[0]);
181 amba_attr(irq1
, "%u\n", dev
->irq
[1]);
182 amba_attr(resource
, "\t%08lx\t%08lx\t%08lx\n",
183 dev
->res
.start
, dev
->res
.end
, dev
->res
.flags
);
186 * amba_device_register - register an AMBA device
187 * @dev: AMBA device to register
188 * @parent: parent memory resource
190 * Setup the AMBA device, reading the cell ID if present.
191 * Claim the resource, and register the AMBA device with
192 * the Linux device manager.
194 int amba_device_register(struct amba_device
*dev
, struct resource
*parent
)
200 dev
->dev
.release
= amba_device_release
;
201 dev
->dev
.bus
= &amba_bustype
;
202 dev
->dev
.dma_mask
= &dev
->dma_mask
;
203 dev
->res
.name
= dev
->dev
.bus_id
;
205 if (!dev
->dev
.coherent_dma_mask
&& dev
->dma_mask
)
206 dev_warn(&dev
->dev
, "coherent dma mask is unset\n");
208 ret
= request_resource(parent
, &dev
->res
);
210 tmp
= ioremap(dev
->res
.start
, SZ_4K
);
216 for (pid
= 0, i
= 0; i
< 4; i
++)
217 pid
|= (readl(tmp
+ 0xfe0 + 4 * i
) & 255) << (i
* 8);
218 for (cid
= 0, i
= 0; i
< 4; i
++)
219 cid
|= (readl(tmp
+ 0xff0 + 4 * i
) & 255) << (i
* 8);
223 if (cid
== 0xb105f00d)
227 ret
= device_register(&dev
->dev
);
232 device_create_file(&dev
->dev
, &dev_attr_id
);
233 if (dev
->irq
[0] != NO_IRQ
)
234 device_create_file(&dev
->dev
, &dev_attr_irq0
);
235 if (dev
->irq
[1] != NO_IRQ
)
236 device_create_file(&dev
->dev
, &dev_attr_irq1
);
237 device_create_file(&dev
->dev
, &dev_attr_resource
);
240 release_resource(&dev
->res
);
247 * amba_device_unregister - unregister an AMBA device
248 * @dev: AMBA device to remove
250 * Remove the specified AMBA device from the Linux device
251 * manager. All files associated with this object will be
252 * destroyed, and device drivers notified that the device has
253 * been removed. The AMBA device's resources including
254 * the amba_device structure will be freed once all
255 * references to it have been dropped.
257 void amba_device_unregister(struct amba_device
*dev
)
259 device_unregister(&dev
->dev
);
264 struct amba_device
*dev
;
265 struct device
*parent
;
271 static int amba_find_match(struct device
*dev
, void *data
)
273 struct find_data
*d
= data
;
274 struct amba_device
*pcdev
= to_amba_device(dev
);
277 r
= (pcdev
->periphid
& d
->mask
) == d
->id
;
279 r
&= d
->parent
== dev
->parent
;
281 r
&= strcmp(dev
->bus_id
, d
->busid
) == 0;
292 * amba_find_device - locate an AMBA device given a bus id
293 * @busid: bus id for device (or NULL)
294 * @parent: parent device (or NULL)
295 * @id: peripheral ID (or 0)
296 * @mask: peripheral ID mask (or 0)
298 * Return the AMBA device corresponding to the supplied parameters.
299 * If no device matches, returns NULL.
301 * NOTE: When a valid device is found, its refcount is
302 * incremented, and must be decremented before the returned
306 amba_find_device(const char *busid
, struct device
*parent
, unsigned int id
,
309 struct find_data data
;
312 data
.parent
= parent
;
317 bus_for_each_dev(&amba_bustype
, NULL
, &data
, amba_find_match
);
323 * amba_request_regions - request all mem regions associated with device
324 * @dev: amba_device structure for device
325 * @name: name, or NULL to use driver name
327 int amba_request_regions(struct amba_device
*dev
, const char *name
)
332 name
= dev
->dev
.driver
->name
;
334 if (!request_mem_region(dev
->res
.start
, SZ_4K
, name
))
341 * amba_release_regions - release mem regions assoicated with device
342 * @dev: amba_device structure for device
344 * Release regions claimed by a successful call to amba_request_regions.
346 void amba_release_regions(struct amba_device
*dev
)
348 release_mem_region(dev
->res
.start
, SZ_4K
);
351 EXPORT_SYMBOL(amba_driver_register
);
352 EXPORT_SYMBOL(amba_driver_unregister
);
353 EXPORT_SYMBOL(amba_device_register
);
354 EXPORT_SYMBOL(amba_device_unregister
);
355 EXPORT_SYMBOL(amba_find_device
);
356 EXPORT_SYMBOL(amba_request_regions
);
357 EXPORT_SYMBOL(amba_release_regions
);