PM / Domains: Remove legacy API for adding devices through DT
[deliverable/linux.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>
ba74ec7f
RV
16#include <linux/pm.h>
17#include <linux/pm_runtime.h>
a62c80e5 18#include <linux/amba/bus.h>
a875cfbb 19#include <linux/sizes.h>
1da177e4 20
934848da 21#include <asm/irq.h>
1da177e4 22
1da177e4
LT
23#define to_amba_driver(d) container_of(d, struct amba_driver, drv)
24
c862aab0
RK
25static const struct amba_id *
26amba_lookup(const struct amba_id *table, struct amba_device *dev)
1da177e4
LT
27{
28 int ret = 0;
29
30 while (table->mask) {
31 ret = (dev->periphid & table->mask) == table->id;
32 if (ret)
33 break;
34 table++;
35 }
36
37 return ret ? table : NULL;
38}
39
40static int amba_match(struct device *dev, struct device_driver *drv)
41{
42 struct amba_device *pcdev = to_amba_device(dev);
43 struct amba_driver *pcdrv = to_amba_driver(drv);
44
45 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
46}
47
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);
523817bd
DM
54 if (retval)
55 return retval;
56
57 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
bf62456e 58 return retval;
1da177e4 59}
1da177e4 60
96b13f5c
RK
61#define amba_attr_func(name,fmt,arg...) \
62static ssize_t name##_show(struct device *_dev, \
63 struct device_attribute *attr, char *buf) \
64{ \
65 struct amba_device *dev = to_amba_device(_dev); \
66 return sprintf(buf, fmt, arg); \
67}
68
69#define amba_attr(name,fmt,arg...) \
70amba_attr_func(name,fmt,arg) \
71static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
72
73amba_attr_func(id, "%08x\n", dev->periphid);
74amba_attr(irq0, "%u\n", dev->irq[0]);
75amba_attr(irq1, "%u\n", dev->irq[1]);
76amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
77 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
78 dev->res.flags);
79
80static struct device_attribute amba_dev_attrs[] = {
81 __ATTR_RO(id),
82 __ATTR_RO(resource),
83 __ATTR_NULL,
84};
85
f210c53a 86#ifdef CONFIG_PM
92b97f0a
RK
87/*
88 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
89 * enable/disable the bus clock at runtime PM suspend/resume as this
1e45860f 90 * does not result in loss of context.
92b97f0a
RK
91 */
92static int amba_pm_runtime_suspend(struct device *dev)
93{
94 struct amba_device *pcdev = to_amba_device(dev);
95 int ret = pm_generic_runtime_suspend(dev);
96
97 if (ret == 0 && dev->driver)
5303c0f4 98 clk_disable_unprepare(pcdev->pclk);
92b97f0a
RK
99
100 return ret;
101}
102
103static int amba_pm_runtime_resume(struct device *dev)
104{
105 struct amba_device *pcdev = to_amba_device(dev);
106 int ret;
107
108 if (dev->driver) {
5303c0f4 109 ret = clk_prepare_enable(pcdev->pclk);
92b97f0a
RK
110 /* Failure is probably fatal to the system, but... */
111 if (ret)
112 return ret;
113 }
114
115 return pm_generic_runtime_resume(dev);
116}
117#endif
118
ba74ec7f 119static const struct dev_pm_ops amba_pm = {
26825cfd
UH
120 .suspend = pm_generic_suspend,
121 .resume = pm_generic_resume,
122 .freeze = pm_generic_freeze,
123 .thaw = pm_generic_thaw,
124 .poweroff = pm_generic_poweroff,
125 .restore = pm_generic_restore,
f210c53a 126 SET_PM_RUNTIME_PM_OPS(
92b97f0a
RK
127 amba_pm_runtime_suspend,
128 amba_pm_runtime_resume,
45f0a85c 129 NULL
ba74ec7f
RV
130 )
131};
132
1da177e4
LT
133/*
134 * Primecells are part of the Advanced Microcontroller Bus Architecture,
135 * so we call the bus "amba".
136 */
394d5aef 137struct bus_type amba_bustype = {
1da177e4 138 .name = "amba",
96b13f5c 139 .dev_attrs = amba_dev_attrs,
1da177e4 140 .match = amba_match,
6d20b035 141 .uevent = amba_uevent,
26825cfd 142 .pm = &amba_pm,
1da177e4
LT
143};
144
145static int __init amba_init(void)
146{
147 return bus_register(&amba_bustype);
148}
149
150postcore_initcall(amba_init);
151
7cfe2494
RK
152static int amba_get_enable_pclk(struct amba_device *pcdev)
153{
7cfe2494
RK
154 int ret;
155
89a5c985
UH
156 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
157 if (IS_ERR(pcdev->pclk))
158 return PTR_ERR(pcdev->pclk);
7cfe2494 159
89a5c985
UH
160 ret = clk_prepare_enable(pcdev->pclk);
161 if (ret)
162 clk_put(pcdev->pclk);
7cfe2494
RK
163
164 return ret;
165}
166
167static void amba_put_disable_pclk(struct amba_device *pcdev)
168{
89a5c985
UH
169 clk_disable_unprepare(pcdev->pclk);
170 clk_put(pcdev->pclk);
7cfe2494
RK
171}
172
1da177e4
LT
173/*
174 * These are the device model conversion veneers; they convert the
175 * device model structures to our more specific structures.
176 */
177static int amba_probe(struct device *dev)
178{
179 struct amba_device *pcdev = to_amba_device(dev);
180 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
c862aab0 181 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
7cfe2494 182 int ret;
1da177e4 183
7cfe2494 184 do {
207f1a2d
UH
185 ret = dev_pm_domain_attach(dev, true);
186 if (ret == -EPROBE_DEFER)
187 break;
188
7cfe2494 189 ret = amba_get_enable_pclk(pcdev);
207f1a2d
UH
190 if (ret) {
191 dev_pm_domain_detach(dev, true);
7cfe2494 192 break;
207f1a2d 193 }
7cfe2494 194
92b97f0a
RK
195 pm_runtime_get_noresume(dev);
196 pm_runtime_set_active(dev);
197 pm_runtime_enable(dev);
198
7cfe2494
RK
199 ret = pcdrv->probe(pcdev, id);
200 if (ret == 0)
201 break;
1da177e4 202
92b97f0a
RK
203 pm_runtime_disable(dev);
204 pm_runtime_set_suspended(dev);
205 pm_runtime_put_noidle(dev);
206
7cfe2494 207 amba_put_disable_pclk(pcdev);
207f1a2d 208 dev_pm_domain_detach(dev, true);
7cfe2494
RK
209 } while (0);
210
211 return ret;
1da177e4
LT
212}
213
214static int amba_remove(struct device *dev)
215{
7cfe2494 216 struct amba_device *pcdev = to_amba_device(dev);
1da177e4 217 struct amba_driver *drv = to_amba_driver(dev->driver);
92b97f0a
RK
218 int ret;
219
220 pm_runtime_get_sync(dev);
221 ret = drv->remove(pcdev);
222 pm_runtime_put_noidle(dev);
223
224 /* Undo the runtime PM settings in amba_probe() */
225 pm_runtime_disable(dev);
226 pm_runtime_set_suspended(dev);
227 pm_runtime_put_noidle(dev);
7cfe2494
RK
228
229 amba_put_disable_pclk(pcdev);
207f1a2d 230 dev_pm_domain_detach(dev, true);
7cfe2494
RK
231
232 return ret;
1da177e4
LT
233}
234
235static void amba_shutdown(struct device *dev)
236{
237 struct amba_driver *drv = to_amba_driver(dev->driver);
238 drv->shutdown(to_amba_device(dev));
239}
240
241/**
242 * amba_driver_register - register an AMBA device driver
243 * @drv: amba device driver structure
244 *
245 * Register an AMBA device driver with the Linux device model
246 * core. If devices pre-exist, the drivers probe function will
247 * be called.
248 */
249int amba_driver_register(struct amba_driver *drv)
250{
251 drv->drv.bus = &amba_bustype;
252
253#define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
254 SETFN(probe);
255 SETFN(remove);
256 SETFN(shutdown);
257
258 return driver_register(&drv->drv);
259}
260
261/**
262 * amba_driver_unregister - remove an AMBA device driver
263 * @drv: AMBA device driver structure to remove
264 *
265 * Unregister an AMBA device driver from the Linux device
266 * model. The device model will call the drivers remove function
267 * for each device the device driver is currently handling.
268 */
269void amba_driver_unregister(struct amba_driver *drv)
270{
271 driver_unregister(&drv->drv);
272}
273
274
275static void amba_device_release(struct device *dev)
276{
277 struct amba_device *d = to_amba_device(dev);
278
279 if (d->res.parent)
280 release_resource(&d->res);
281 kfree(d);
282}
283
1da177e4 284/**
d5dc9271
RK
285 * amba_device_add - add a previously allocated AMBA device structure
286 * @dev: AMBA device allocated by amba_device_alloc
287 * @parent: resource parent for this devices resources
1da177e4 288 *
d5dc9271
RK
289 * Claim the resource, and read the device cell ID if not already
290 * initialized. Register the AMBA device with the Linux device
291 * manager.
1da177e4 292 */
d5dc9271 293int amba_device_add(struct amba_device *dev, struct resource *parent)
1da177e4 294{
8afe0b96 295 u32 size;
1da177e4
LT
296 void __iomem *tmp;
297 int i, ret;
298
2eac58d5
RK
299 WARN_ON(dev->irq[0] == (unsigned int)-1);
300 WARN_ON(dev->irq[1] == (unsigned int)-1);
301
1da177e4 302 ret = request_resource(parent, &dev->res);
96b13f5c
RK
303 if (ret)
304 goto err_out;
305
97ceed1f
LW
306 /* Hard-coded primecell ID instead of plug-n-play */
307 if (dev->periphid != 0)
308 goto skip_probe;
309
8afe0b96
LC
310 /*
311 * Dynamically calculate the size of the resource
312 * and use this for iomap
313 */
314 size = resource_size(&dev->res);
315 tmp = ioremap(dev->res.start, size);
96b13f5c
RK
316 if (!tmp) {
317 ret = -ENOMEM;
318 goto err_release;
1da177e4 319 }
96b13f5c 320
7cfe2494
RK
321 ret = amba_get_enable_pclk(dev);
322 if (ret == 0) {
323 u32 pid, cid;
96b13f5c 324
7cfe2494
RK
325 /*
326 * Read pid and cid based on size of resource
327 * they are located at end of region
328 */
329 for (pid = 0, i = 0; i < 4; i++)
330 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
331 (i * 8);
332 for (cid = 0, i = 0; i < 4; i++)
333 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
334 (i * 8);
96b13f5c 335
7cfe2494 336 amba_put_disable_pclk(dev);
96b13f5c 337
01723a95 338 if (cid == AMBA_CID)
7cfe2494
RK
339 dev->periphid = pid;
340
341 if (!dev->periphid)
342 ret = -ENODEV;
96b13f5c
RK
343 }
344
7cfe2494
RK
345 iounmap(tmp);
346
347 if (ret)
348 goto err_release;
349
97ceed1f 350 skip_probe:
557dca5f 351 ret = device_add(&dev->dev);
96b13f5c
RK
352 if (ret)
353 goto err_release;
354
dfb85185 355 if (dev->irq[0])
96b13f5c 356 ret = device_create_file(&dev->dev, &dev_attr_irq0);
dfb85185 357 if (ret == 0 && dev->irq[1])
96b13f5c
RK
358 ret = device_create_file(&dev->dev, &dev_attr_irq1);
359 if (ret == 0)
360 return ret;
361
362 device_unregister(&dev->dev);
363
364 err_release:
365 release_resource(&dev->res);
366 err_out:
1da177e4
LT
367 return ret;
368}
d5dc9271
RK
369EXPORT_SYMBOL_GPL(amba_device_add);
370
6026aa90
LW
371static struct amba_device *
372amba_aphb_device_add(struct device *parent, const char *name,
373 resource_size_t base, size_t size, int irq1, int irq2,
3ad909bc
LW
374 void *pdata, unsigned int periphid, u64 dma_mask,
375 struct resource *resbase)
6026aa90
LW
376{
377 struct amba_device *dev;
378 int ret;
379
380 dev = amba_device_alloc(name, base, size);
381 if (!dev)
382 return ERR_PTR(-ENOMEM);
383
6026aa90
LW
384 dev->dev.coherent_dma_mask = dma_mask;
385 dev->irq[0] = irq1;
386 dev->irq[1] = irq2;
387 dev->periphid = periphid;
388 dev->dev.platform_data = pdata;
389 dev->dev.parent = parent;
390
3ad909bc 391 ret = amba_device_add(dev, resbase);
6026aa90
LW
392 if (ret) {
393 amba_device_put(dev);
394 return ERR_PTR(ret);
395 }
396
397 return dev;
398}
399
400struct amba_device *
401amba_apb_device_add(struct device *parent, const char *name,
402 resource_size_t base, size_t size, int irq1, int irq2,
403 void *pdata, unsigned int periphid)
404{
405 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
3ad909bc 406 periphid, 0, &iomem_resource);
6026aa90
LW
407}
408EXPORT_SYMBOL_GPL(amba_apb_device_add);
409
410struct amba_device *
411amba_ahb_device_add(struct device *parent, const char *name,
412 resource_size_t base, size_t size, int irq1, int irq2,
413 void *pdata, unsigned int periphid)
414{
415 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
3ad909bc 416 periphid, ~0ULL, &iomem_resource);
6026aa90
LW
417}
418EXPORT_SYMBOL_GPL(amba_ahb_device_add);
419
3ad909bc
LW
420struct amba_device *
421amba_apb_device_add_res(struct device *parent, const char *name,
422 resource_size_t base, size_t size, int irq1,
423 int irq2, void *pdata, unsigned int periphid,
424 struct resource *resbase)
425{
426 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
427 periphid, 0, resbase);
428}
429EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
430
431struct amba_device *
432amba_ahb_device_add_res(struct device *parent, const char *name,
433 resource_size_t base, size_t size, int irq1,
434 int irq2, void *pdata, unsigned int periphid,
435 struct resource *resbase)
436{
437 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
438 periphid, ~0ULL, resbase);
439}
440EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
441
442
d5dc9271
RK
443static void amba_device_initialize(struct amba_device *dev, const char *name)
444{
445 device_initialize(&dev->dev);
446 if (name)
447 dev_set_name(&dev->dev, "%s", name);
448 dev->dev.release = amba_device_release;
449 dev->dev.bus = &amba_bustype;
446b2a93 450 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
d5dc9271
RK
451 dev->res.name = dev_name(&dev->dev);
452}
453
454/**
455 * amba_device_alloc - allocate an AMBA device
456 * @name: sysfs name of the AMBA device
457 * @base: base of AMBA device
458 * @size: size of AMBA device
459 *
460 * Allocate and initialize an AMBA device structure. Returns %NULL
461 * on failure.
462 */
463struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
464 size_t size)
465{
466 struct amba_device *dev;
467
468 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
469 if (dev) {
470 amba_device_initialize(dev, name);
471 dev->res.start = base;
472 dev->res.end = base + size - 1;
473 dev->res.flags = IORESOURCE_MEM;
474 }
475
476 return dev;
477}
478EXPORT_SYMBOL_GPL(amba_device_alloc);
479
480/**
481 * amba_device_register - register an AMBA device
482 * @dev: AMBA device to register
483 * @parent: parent memory resource
484 *
485 * Setup the AMBA device, reading the cell ID if present.
486 * Claim the resource, and register the AMBA device with
487 * the Linux device manager.
488 */
489int amba_device_register(struct amba_device *dev, struct resource *parent)
490{
491 amba_device_initialize(dev, dev->dev.init_name);
492 dev->dev.init_name = NULL;
493
d5dc9271
RK
494 return amba_device_add(dev, parent);
495}
496
497/**
498 * amba_device_put - put an AMBA device
499 * @dev: AMBA device to put
500 */
501void amba_device_put(struct amba_device *dev)
502{
503 put_device(&dev->dev);
504}
505EXPORT_SYMBOL_GPL(amba_device_put);
1da177e4
LT
506
507/**
508 * amba_device_unregister - unregister an AMBA device
509 * @dev: AMBA device to remove
510 *
511 * Remove the specified AMBA device from the Linux device
512 * manager. All files associated with this object will be
513 * destroyed, and device drivers notified that the device has
514 * been removed. The AMBA device's resources including
515 * the amba_device structure will be freed once all
516 * references to it have been dropped.
517 */
518void amba_device_unregister(struct amba_device *dev)
519{
520 device_unregister(&dev->dev);
521}
522
523
524struct find_data {
525 struct amba_device *dev;
526 struct device *parent;
527 const char *busid;
528 unsigned int id;
529 unsigned int mask;
530};
531
532static int amba_find_match(struct device *dev, void *data)
533{
534 struct find_data *d = data;
535 struct amba_device *pcdev = to_amba_device(dev);
536 int r;
537
538 r = (pcdev->periphid & d->mask) == d->id;
539 if (d->parent)
540 r &= d->parent == dev->parent;
541 if (d->busid)
9d6b4c82 542 r &= strcmp(dev_name(dev), d->busid) == 0;
1da177e4
LT
543
544 if (r) {
545 get_device(dev);
546 d->dev = pcdev;
547 }
548
549 return r;
550}
551
552/**
553 * amba_find_device - locate an AMBA device given a bus id
554 * @busid: bus id for device (or NULL)
555 * @parent: parent device (or NULL)
556 * @id: peripheral ID (or 0)
557 * @mask: peripheral ID mask (or 0)
558 *
559 * Return the AMBA device corresponding to the supplied parameters.
560 * If no device matches, returns NULL.
561 *
562 * NOTE: When a valid device is found, its refcount is
563 * incremented, and must be decremented before the returned
564 * reference.
565 */
566struct amba_device *
567amba_find_device(const char *busid, struct device *parent, unsigned int id,
568 unsigned int mask)
569{
570 struct find_data data;
571
572 data.dev = NULL;
573 data.parent = parent;
574 data.busid = busid;
575 data.id = id;
576 data.mask = mask;
577
578 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
579
580 return data.dev;
581}
582
583/**
584 * amba_request_regions - request all mem regions associated with device
585 * @dev: amba_device structure for device
586 * @name: name, or NULL to use driver name
587 */
588int amba_request_regions(struct amba_device *dev, const char *name)
589{
590 int ret = 0;
8afe0b96 591 u32 size;
1da177e4
LT
592
593 if (!name)
594 name = dev->dev.driver->name;
595
8afe0b96
LC
596 size = resource_size(&dev->res);
597
598 if (!request_mem_region(dev->res.start, size, name))
1da177e4
LT
599 ret = -EBUSY;
600
601 return ret;
602}
603
604/**
25985edc 605 * amba_release_regions - release mem regions associated with device
1da177e4
LT
606 * @dev: amba_device structure for device
607 *
608 * Release regions claimed by a successful call to amba_request_regions.
609 */
610void amba_release_regions(struct amba_device *dev)
611{
8afe0b96
LC
612 u32 size;
613
614 size = resource_size(&dev->res);
615 release_mem_region(dev->res.start, size);
1da177e4
LT
616}
617
618EXPORT_SYMBOL(amba_driver_register);
619EXPORT_SYMBOL(amba_driver_unregister);
620EXPORT_SYMBOL(amba_device_register);
621EXPORT_SYMBOL(amba_device_unregister);
622EXPORT_SYMBOL(amba_find_device);
623EXPORT_SYMBOL(amba_request_regions);
624EXPORT_SYMBOL(amba_release_regions);
This page took 0.751955 seconds and 5 git commands to generate.