GNU Linux-libre 4.4.283-gnu1
[releases.git] / drivers / amba / bus.c
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 *
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
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
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
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
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  */
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
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
205 static int __init amba_init(void)
206 {
207         return bus_register(&amba_bustype);
208 }
209
210 postcore_initcall(amba_init);
211
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
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  */
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
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
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  */
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  */
333 void amba_driver_unregister(struct amba_driver *drv)
334 {
335         driver_unregister(&drv->drv);
336 }
337
338
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  */
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         WARN_ON(dev->irq[0] == (unsigned int)-1);
364         WARN_ON(dev->irq[1] == (unsigned int)-1);
365
366         ret = request_resource(parent, &dev->res);
367         if (ret)
368                 goto err_out;
369
370         /* Hard-coded primecell ID instead of plug-n-play */
371         if (dev->periphid != 0)
372                 goto skip_probe;
373
374         /*
375          * Dynamically calculate the size of the resource
376          * and use this for iomap
377          */
378         size = resource_size(&dev->res);
379         tmp = ioremap(dev->res.start, size);
380         if (!tmp) {
381                 ret = -ENOMEM;
382                 goto err_release;
383         }
384
385         ret = amba_get_enable_pclk(dev);
386         if (ret == 0) {
387                 u32 pid, cid;
388
389                 /*
390                  * Read pid and cid based on size of resource
391                  * they are located at end of region
392                  */
393                 for (pid = 0, i = 0; i < 4; i++)
394                         pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
395                                 (i * 8);
396                 for (cid = 0, i = 0; i < 4; i++)
397                         cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
398                                 (i * 8);
399
400                 amba_put_disable_pclk(dev);
401
402                 if (cid == AMBA_CID || cid == CORESIGHT_CID)
403                         dev->periphid = pid;
404
405                 if (!dev->periphid)
406                         ret = -ENODEV;
407         }
408
409         iounmap(tmp);
410
411         if (ret)
412                 goto err_release;
413
414  skip_probe:
415         ret = device_add(&dev->dev);
416         if (ret)
417                 goto err_release;
418
419         if (dev->irq[0])
420                 ret = device_create_file(&dev->dev, &dev_attr_irq0);
421         if (ret == 0 && dev->irq[1])
422                 ret = device_create_file(&dev->dev, &dev_attr_irq1);
423         if (ret == 0)
424                 return ret;
425
426         device_unregister(&dev->dev);
427
428  err_release:
429         release_resource(&dev->res);
430  err_out:
431         return ret;
432 }
433 EXPORT_SYMBOL_GPL(amba_device_add);
434
435 static struct amba_device *
436 amba_aphb_device_add(struct device *parent, const char *name,
437                      resource_size_t base, size_t size, int irq1, int irq2,
438                      void *pdata, unsigned int periphid, u64 dma_mask,
439                      struct resource *resbase)
440 {
441         struct amba_device *dev;
442         int ret;
443
444         dev = amba_device_alloc(name, base, size);
445         if (!dev)
446                 return ERR_PTR(-ENOMEM);
447
448         dev->dev.coherent_dma_mask = dma_mask;
449         dev->irq[0] = irq1;
450         dev->irq[1] = irq2;
451         dev->periphid = periphid;
452         dev->dev.platform_data = pdata;
453         dev->dev.parent = parent;
454
455         ret = amba_device_add(dev, resbase);
456         if (ret) {
457                 amba_device_put(dev);
458                 return ERR_PTR(ret);
459         }
460
461         return dev;
462 }
463
464 struct amba_device *
465 amba_apb_device_add(struct device *parent, const char *name,
466                     resource_size_t base, size_t size, int irq1, int irq2,
467                     void *pdata, unsigned int periphid)
468 {
469         return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
470                                     periphid, 0, &iomem_resource);
471 }
472 EXPORT_SYMBOL_GPL(amba_apb_device_add);
473
474 struct amba_device *
475 amba_ahb_device_add(struct device *parent, const char *name,
476                     resource_size_t base, size_t size, int irq1, int irq2,
477                     void *pdata, unsigned int periphid)
478 {
479         return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
480                                     periphid, ~0ULL, &iomem_resource);
481 }
482 EXPORT_SYMBOL_GPL(amba_ahb_device_add);
483
484 struct amba_device *
485 amba_apb_device_add_res(struct device *parent, const char *name,
486                         resource_size_t base, size_t size, int irq1,
487                         int irq2, void *pdata, unsigned int periphid,
488                         struct resource *resbase)
489 {
490         return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
491                                     periphid, 0, resbase);
492 }
493 EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
494
495 struct amba_device *
496 amba_ahb_device_add_res(struct device *parent, const char *name,
497                         resource_size_t base, size_t size, int irq1,
498                         int irq2, void *pdata, unsigned int periphid,
499                         struct resource *resbase)
500 {
501         return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
502                                     periphid, ~0ULL, resbase);
503 }
504 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
505
506
507 static void amba_device_initialize(struct amba_device *dev, const char *name)
508 {
509         device_initialize(&dev->dev);
510         if (name)
511                 dev_set_name(&dev->dev, "%s", name);
512         dev->dev.release = amba_device_release;
513         dev->dev.bus = &amba_bustype;
514         dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
515         dev->res.name = dev_name(&dev->dev);
516 }
517
518 /**
519  *      amba_device_alloc - allocate an AMBA device
520  *      @name: sysfs name of the AMBA device
521  *      @base: base of AMBA device
522  *      @size: size of AMBA device
523  *
524  *      Allocate and initialize an AMBA device structure.  Returns %NULL
525  *      on failure.
526  */
527 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
528         size_t size)
529 {
530         struct amba_device *dev;
531
532         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
533         if (dev) {
534                 amba_device_initialize(dev, name);
535                 dev->res.start = base;
536                 dev->res.end = base + size - 1;
537                 dev->res.flags = IORESOURCE_MEM;
538         }
539
540         return dev;
541 }
542 EXPORT_SYMBOL_GPL(amba_device_alloc);
543
544 /**
545  *      amba_device_register - register an AMBA device
546  *      @dev: AMBA device to register
547  *      @parent: parent memory resource
548  *
549  *      Setup the AMBA device, reading the cell ID if present.
550  *      Claim the resource, and register the AMBA device with
551  *      the Linux device manager.
552  */
553 int amba_device_register(struct amba_device *dev, struct resource *parent)
554 {
555         amba_device_initialize(dev, dev->dev.init_name);
556         dev->dev.init_name = NULL;
557
558         return amba_device_add(dev, parent);
559 }
560
561 /**
562  *      amba_device_put - put an AMBA device
563  *      @dev: AMBA device to put
564  */
565 void amba_device_put(struct amba_device *dev)
566 {
567         put_device(&dev->dev);
568 }
569 EXPORT_SYMBOL_GPL(amba_device_put);
570
571 /**
572  *      amba_device_unregister - unregister an AMBA device
573  *      @dev: AMBA device to remove
574  *
575  *      Remove the specified AMBA device from the Linux device
576  *      manager.  All files associated with this object will be
577  *      destroyed, and device drivers notified that the device has
578  *      been removed.  The AMBA device's resources including
579  *      the amba_device structure will be freed once all
580  *      references to it have been dropped.
581  */
582 void amba_device_unregister(struct amba_device *dev)
583 {
584         device_unregister(&dev->dev);
585 }
586
587
588 struct find_data {
589         struct amba_device *dev;
590         struct device *parent;
591         const char *busid;
592         unsigned int id;
593         unsigned int mask;
594 };
595
596 static int amba_find_match(struct device *dev, void *data)
597 {
598         struct find_data *d = data;
599         struct amba_device *pcdev = to_amba_device(dev);
600         int r;
601
602         r = (pcdev->periphid & d->mask) == d->id;
603         if (d->parent)
604                 r &= d->parent == dev->parent;
605         if (d->busid)
606                 r &= strcmp(dev_name(dev), d->busid) == 0;
607
608         if (r) {
609                 get_device(dev);
610                 d->dev = pcdev;
611         }
612
613         return r;
614 }
615
616 /**
617  *      amba_find_device - locate an AMBA device given a bus id
618  *      @busid: bus id for device (or NULL)
619  *      @parent: parent device (or NULL)
620  *      @id: peripheral ID (or 0)
621  *      @mask: peripheral ID mask (or 0)
622  *
623  *      Return the AMBA device corresponding to the supplied parameters.
624  *      If no device matches, returns NULL.
625  *
626  *      NOTE: When a valid device is found, its refcount is
627  *      incremented, and must be decremented before the returned
628  *      reference.
629  */
630 struct amba_device *
631 amba_find_device(const char *busid, struct device *parent, unsigned int id,
632                  unsigned int mask)
633 {
634         struct find_data data;
635
636         data.dev = NULL;
637         data.parent = parent;
638         data.busid = busid;
639         data.id = id;
640         data.mask = mask;
641
642         bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
643
644         return data.dev;
645 }
646
647 /**
648  *      amba_request_regions - request all mem regions associated with device
649  *      @dev: amba_device structure for device
650  *      @name: name, or NULL to use driver name
651  */
652 int amba_request_regions(struct amba_device *dev, const char *name)
653 {
654         int ret = 0;
655         u32 size;
656
657         if (!name)
658                 name = dev->dev.driver->name;
659
660         size = resource_size(&dev->res);
661
662         if (!request_mem_region(dev->res.start, size, name))
663                 ret = -EBUSY;
664
665         return ret;
666 }
667
668 /**
669  *      amba_release_regions - release mem regions associated with device
670  *      @dev: amba_device structure for device
671  *
672  *      Release regions claimed by a successful call to amba_request_regions.
673  */
674 void amba_release_regions(struct amba_device *dev)
675 {
676         u32 size;
677
678         size = resource_size(&dev->res);
679         release_mem_region(dev->res.start, size);
680 }
681
682 EXPORT_SYMBOL(amba_driver_register);
683 EXPORT_SYMBOL(amba_driver_unregister);
684 EXPORT_SYMBOL(amba_device_register);
685 EXPORT_SYMBOL(amba_device_unregister);
686 EXPORT_SYMBOL(amba_find_device);
687 EXPORT_SYMBOL(amba_request_regions);
688 EXPORT_SYMBOL(amba_release_regions);