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