blob: 720aa6cdd40250870df56dd4a1ae004d40c8d3dc [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/arch/arm/common/amba.c
4 *
5 * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080010#include <linux/string.h>
11#include <linux/slab.h>
Russell King934848d2009-01-08 09:58:51 +000012#include <linux/io.h>
Rabin Vincentba74ec72011-02-23 04:33:17 +010013#include <linux/pm.h>
14#include <linux/pm_runtime.h>
Ulf Hanssonf48c7672014-09-29 13:58:47 +020015#include <linux/pm_domain.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000016#include <linux/amba/bus.h>
Alessandro Rubinia875cfb2012-06-24 12:46:16 +010017#include <linux/sizes.h>
Antonios Motakis3cf38572015-01-06 11:15:11 +010018#include <linux/limits.h>
Stephen Boydbcd30062016-08-10 20:17:34 +010019#include <linux/clk/clk-conf.h>
Nipun Gupta07397df2018-04-28 08:21:58 +053020#include <linux/platform_device.h>
DINH L NGUYEN79bdcb202019-09-04 02:13:08 +010021#include <linux/reset.h>
Wang Kefeng854f6952021-08-23 10:41:43 +010022#include <linux/of_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Russell King934848d2009-01-08 09:58:51 +000024#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define to_amba_driver(d) container_of(d, struct amba_driver, drv)
27
Mike Leach4a2910f2019-02-13 14:41:50 +010028/* called on periphid match and class 0x9 coresight device. */
29static int
30amba_cs_uci_id_match(const struct amba_id *table, struct amba_device *dev)
31{
32 int ret = 0;
33 struct amba_cs_uci_id *uci;
34
35 uci = table->data;
36
37 /* no table data or zero mask - return match on periphid */
38 if (!uci || (uci->devarch_mask == 0))
39 return 1;
40
41 /* test against read devtype and masked devarch value */
42 ret = (dev->uci.devtype == uci->devtype) &&
43 ((dev->uci.devarch & uci->devarch_mask) == uci->devarch);
44 return ret;
45}
46
Russell Kingc862aab2011-02-19 15:55:26 +000047static const struct amba_id *
48amba_lookup(const struct amba_id *table, struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 while (table->mask) {
Mike Leach4a2910f2019-02-13 14:41:50 +010051 if (((dev->periphid & table->mask) == table->id) &&
52 ((dev->cid != CORESIGHT_CID) ||
53 (amba_cs_uci_id_match(table, dev))))
54 return table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 table++;
56 }
Mike Leach4a2910f2019-02-13 14:41:50 +010057 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010060static int amba_get_enable_pclk(struct amba_device *pcdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010062 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010064 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
65 if (IS_ERR(pcdev->pclk))
66 return PTR_ERR(pcdev->pclk);
Antonios Motakis3cf38572015-01-06 11:15:11 +010067
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010068 ret = clk_prepare_enable(pcdev->pclk);
69 if (ret)
70 clk_put(pcdev->pclk);
71
72 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010075static void amba_put_disable_pclk(struct amba_device *pcdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010077 clk_disable_unprepare(pcdev->pclk);
78 clk_put(pcdev->pclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010081
Antonios Motakis3cf38572015-01-06 11:15:11 +010082static ssize_t driver_override_show(struct device *_dev,
83 struct device_attribute *attr, char *buf)
84{
85 struct amba_device *dev = to_amba_device(_dev);
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +020086 ssize_t len;
Antonios Motakis3cf38572015-01-06 11:15:11 +010087
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +020088 device_lock(_dev);
89 len = sprintf(buf, "%s\n", dev->driver_override);
90 device_unlock(_dev);
91 return len;
Antonios Motakis3cf38572015-01-06 11:15:11 +010092}
93
94static ssize_t driver_override_store(struct device *_dev,
95 struct device_attribute *attr,
96 const char *buf, size_t count)
97{
98 struct amba_device *dev = to_amba_device(_dev);
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +020099 char *driver_override, *old, *cp;
Antonios Motakis3cf38572015-01-06 11:15:11 +0100100
Geert Uytterhoevend2ffed52018-04-10 15:21:45 +0200101 /* We need to keep extra room for a newline */
102 if (count >= (PAGE_SIZE - 1))
Antonios Motakis3cf38572015-01-06 11:15:11 +0100103 return -EINVAL;
104
105 driver_override = kstrndup(buf, count, GFP_KERNEL);
106 if (!driver_override)
107 return -ENOMEM;
108
109 cp = strchr(driver_override, '\n');
110 if (cp)
111 *cp = '\0';
112
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +0200113 device_lock(_dev);
114 old = dev->driver_override;
Antonios Motakis3cf38572015-01-06 11:15:11 +0100115 if (strlen(driver_override)) {
116 dev->driver_override = driver_override;
117 } else {
Geert Uytterhoeven6be5b5b2018-04-10 15:21:46 +0200118 kfree(driver_override);
119 dev->driver_override = NULL;
Antonios Motakis3cf38572015-01-06 11:15:11 +0100120 }
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +0200121 device_unlock(_dev);
Antonios Motakis3cf38572015-01-06 11:15:11 +0100122
123 kfree(old);
124
125 return count;
126}
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200127static DEVICE_ATTR_RW(driver_override);
Antonios Motakis3cf38572015-01-06 11:15:11 +0100128
Russell King96b13f52006-11-30 14:04:49 +0000129#define amba_attr_func(name,fmt,arg...) \
130static ssize_t name##_show(struct device *_dev, \
131 struct device_attribute *attr, char *buf) \
132{ \
133 struct amba_device *dev = to_amba_device(_dev); \
134 return sprintf(buf, fmt, arg); \
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200135} \
136static DEVICE_ATTR_RO(name)
Russell King96b13f52006-11-30 14:04:49 +0000137
138amba_attr_func(id, "%08x\n", dev->periphid);
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200139amba_attr_func(irq0, "%u\n", dev->irq[0]);
140amba_attr_func(irq1, "%u\n", dev->irq[1]);
Russell King96b13f52006-11-30 14:04:49 +0000141amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
142 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
143 dev->res.flags);
144
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200145static struct attribute *amba_dev_attrs[] = {
146 &dev_attr_id.attr,
147 &dev_attr_resource.attr,
148 &dev_attr_driver_override.attr,
149 NULL,
Russell King96b13f52006-11-30 14:04:49 +0000150};
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200151ATTRIBUTE_GROUPS(amba_dev);
Russell King96b13f52006-11-30 14:04:49 +0000152
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +0100153static int amba_match(struct device *dev, struct device_driver *drv)
154{
155 struct amba_device *pcdev = to_amba_device(dev);
156 struct amba_driver *pcdrv = to_amba_driver(drv);
157
158 /* When driver_override is set, only bind to the matching driver */
159 if (pcdev->driver_override)
160 return !strcmp(pcdev->driver_override, drv->name);
161
162 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
163}
164
165static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
166{
167 struct amba_device *pcdev = to_amba_device(dev);
168 int retval = 0;
169
170 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
171 if (retval)
172 return retval;
173
174 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
175 return retval;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
179 * These are the device model conversion veneers; they convert the
180 * device model structures to our more specific structures.
181 */
182static int amba_probe(struct device *dev)
183{
184 struct amba_device *pcdev = to_amba_device(dev);
185 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
Russell Kingc862aab2011-02-19 15:55:26 +0000186 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
Russell King7cfe2492010-07-15 10:47:14 +0100187 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Russell King7cfe2492010-07-15 10:47:14 +0100189 do {
Stephen Boydbcd30062016-08-10 20:17:34 +0100190 ret = of_clk_set_defaults(dev->of_node, false);
191 if (ret < 0)
192 break;
193
Ulf Hansson207f1a22014-09-19 20:27:42 +0200194 ret = dev_pm_domain_attach(dev, true);
Ulf Hanssond21bc892018-04-26 10:53:05 +0200195 if (ret)
Russell King7cfe2492010-07-15 10:47:14 +0100196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Ulf Hansson207f1a22014-09-19 20:27:42 +0200198 ret = amba_get_enable_pclk(pcdev);
199 if (ret) {
200 dev_pm_domain_detach(dev, true);
201 break;
202 }
203
Russell King92b97f02011-08-14 09:13:48 +0100204 pm_runtime_get_noresume(dev);
205 pm_runtime_set_active(dev);
206 pm_runtime_enable(dev);
207
Russell King7cfe2492010-07-15 10:47:14 +0100208 ret = pcdrv->probe(pcdev, id);
209 if (ret == 0)
210 break;
211
Russell King92b97f02011-08-14 09:13:48 +0100212 pm_runtime_disable(dev);
213 pm_runtime_set_suspended(dev);
214 pm_runtime_put_noidle(dev);
215
Russell King7cfe2492010-07-15 10:47:14 +0100216 amba_put_disable_pclk(pcdev);
Ulf Hansson207f1a22014-09-19 20:27:42 +0200217 dev_pm_domain_detach(dev, true);
Russell King7cfe2492010-07-15 10:47:14 +0100218 } while (0);
219
220 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Uwe Kleine-Königfc7a6202021-07-13 21:35:22 +0200223static void amba_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Russell King7cfe2492010-07-15 10:47:14 +0100225 struct amba_device *pcdev = to_amba_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct amba_driver *drv = to_amba_driver(dev->driver);
Russell King92b97f02011-08-14 09:13:48 +0100227
228 pm_runtime_get_sync(dev);
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100229 if (drv->remove)
Uwe Kleine-König3fd269e2021-01-26 17:58:34 +0100230 drv->remove(pcdev);
Russell King92b97f02011-08-14 09:13:48 +0100231 pm_runtime_put_noidle(dev);
232
233 /* Undo the runtime PM settings in amba_probe() */
234 pm_runtime_disable(dev);
235 pm_runtime_set_suspended(dev);
236 pm_runtime_put_noidle(dev);
Russell King7cfe2492010-07-15 10:47:14 +0100237
238 amba_put_disable_pclk(pcdev);
Ulf Hansson207f1a22014-09-19 20:27:42 +0200239 dev_pm_domain_detach(dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242static void amba_shutdown(struct device *dev)
243{
Uwe Kleine-Königf170b592021-01-26 17:58:35 +0100244 struct amba_driver *drv;
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100245
Uwe Kleine-Königf170b592021-01-26 17:58:35 +0100246 if (!dev->driver)
247 return;
248
249 drv = to_amba_driver(dev->driver);
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100250 if (drv->shutdown)
251 drv->shutdown(to_amba_device(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Uwe Kleine-Königf170b592021-01-26 17:58:35 +0100254#ifdef CONFIG_PM
255/*
256 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
257 * enable/disable the bus clock at runtime PM suspend/resume as this
258 * does not result in loss of context.
259 */
260static int amba_pm_runtime_suspend(struct device *dev)
261{
262 struct amba_device *pcdev = to_amba_device(dev);
263 int ret = pm_generic_runtime_suspend(dev);
264
265 if (ret == 0 && dev->driver) {
266 if (pm_runtime_is_irq_safe(dev))
267 clk_disable(pcdev->pclk);
268 else
269 clk_disable_unprepare(pcdev->pclk);
270 }
271
272 return ret;
273}
274
275static int amba_pm_runtime_resume(struct device *dev)
276{
277 struct amba_device *pcdev = to_amba_device(dev);
278 int ret;
279
280 if (dev->driver) {
281 if (pm_runtime_is_irq_safe(dev))
282 ret = clk_enable(pcdev->pclk);
283 else
284 ret = clk_prepare_enable(pcdev->pclk);
285 /* Failure is probably fatal to the system, but... */
286 if (ret)
287 return ret;
288 }
289
290 return pm_generic_runtime_resume(dev);
291}
292#endif /* CONFIG_PM */
293
294static const struct dev_pm_ops amba_pm = {
295 .suspend = pm_generic_suspend,
296 .resume = pm_generic_resume,
297 .freeze = pm_generic_freeze,
298 .thaw = pm_generic_thaw,
299 .poweroff = pm_generic_poweroff,
300 .restore = pm_generic_restore,
301 SET_RUNTIME_PM_OPS(
302 amba_pm_runtime_suspend,
303 amba_pm_runtime_resume,
304 NULL
305 )
306};
307
308/*
309 * Primecells are part of the Advanced Microcontroller Bus Architecture,
310 * so we call the bus "amba".
311 * DMA configuration for platform and AMBA bus is same. So here we reuse
312 * platform's DMA config routine.
313 */
314struct bus_type amba_bustype = {
315 .name = "amba",
316 .dev_groups = amba_dev_groups,
317 .match = amba_match,
318 .uevent = amba_uevent,
319 .probe = amba_probe,
320 .remove = amba_remove,
321 .shutdown = amba_shutdown,
322 .dma_configure = platform_dma_configure,
323 .pm = &amba_pm,
324};
325EXPORT_SYMBOL_GPL(amba_bustype);
326
327static int __init amba_init(void)
328{
329 return bus_register(&amba_bustype);
330}
331
332postcore_initcall(amba_init);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/**
335 * amba_driver_register - register an AMBA device driver
336 * @drv: amba device driver structure
337 *
338 * Register an AMBA device driver with the Linux device model
339 * core. If devices pre-exist, the drivers probe function will
340 * be called.
341 */
342int amba_driver_register(struct amba_driver *drv)
343{
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100344 if (!drv->probe)
345 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100347 drv->drv.bus = &amba_bustype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 return driver_register(&drv->drv);
350}
351
352/**
353 * amba_driver_unregister - remove an AMBA device driver
354 * @drv: AMBA device driver structure to remove
355 *
356 * Unregister an AMBA device driver from the Linux device
357 * model. The device model will call the drivers remove function
358 * for each device the device driver is currently handling.
359 */
360void amba_driver_unregister(struct amba_driver *drv)
361{
362 driver_unregister(&drv->drv);
363}
364
365
366static void amba_device_release(struct device *dev)
367{
368 struct amba_device *d = to_amba_device(dev);
369
370 if (d->res.parent)
371 release_resource(&d->res);
372 kfree(d);
373}
374
Wang Kefeng854f6952021-08-23 10:41:43 +0100375static int of_amba_device_decode_irq(struct amba_device *dev)
376{
377 struct device_node *node = dev->dev.of_node;
378 int i, irq = 0;
379
380 if (IS_ENABLED(CONFIG_OF_IRQ) && node) {
381 /* Decode the IRQs and address ranges */
382 for (i = 0; i < AMBA_NR_IRQS; i++) {
383 irq = of_irq_get(node, i);
384 if (irq < 0) {
385 if (irq == -EPROBE_DEFER)
386 return irq;
387 irq = 0;
388 }
389
390 dev->irq[i] = irq;
391 }
392 }
393
394 return 0;
395}
396
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100397static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Leo Chen8afe0b92009-07-28 23:34:59 +0100399 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 void __iomem *tmp;
401 int i, ret;
402
Wang Kefeng854f6952021-08-23 10:41:43 +0100403 ret = of_amba_device_decode_irq(dev);
404 if (ret)
405 goto err_out;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 ret = request_resource(parent, &dev->res);
Russell King96b13f52006-11-30 14:04:49 +0000408 if (ret)
409 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Walleij97ceed12011-03-24 16:12:40 +0100411 /* Hard-coded primecell ID instead of plug-n-play */
412 if (dev->periphid != 0)
413 goto skip_probe;
414
Leo Chen8afe0b92009-07-28 23:34:59 +0100415 /*
416 * Dynamically calculate the size of the resource
417 * and use this for iomap
418 */
419 size = resource_size(&dev->res);
420 tmp = ioremap(dev->res.start, size);
Russell King96b13f52006-11-30 14:04:49 +0000421 if (!tmp) {
422 ret = -ENOMEM;
423 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Russell King96b13f52006-11-30 14:04:49 +0000425
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100426 ret = dev_pm_domain_attach(&dev->dev, true);
Ulf Hanssond21bc892018-04-26 10:53:05 +0200427 if (ret) {
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100428 iounmap(tmp);
429 goto err_release;
430 }
431
Russell King7cfe2492010-07-15 10:47:14 +0100432 ret = amba_get_enable_pclk(dev);
433 if (ret == 0) {
434 u32 pid, cid;
DINH L NGUYEN79bdcb202019-09-04 02:13:08 +0100435 struct reset_control *rstc;
436
437 /*
438 * Find reset control(s) of the amba bus and de-assert them.
439 */
440 rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
441 if (IS_ERR(rstc)) {
Russell Kinge9634082019-10-02 17:45:11 +0100442 ret = PTR_ERR(rstc);
443 if (ret != -EPROBE_DEFER)
444 dev_err(&dev->dev, "can't get reset: %d\n",
445 ret);
446 goto err_reset;
DINH L NGUYEN79bdcb202019-09-04 02:13:08 +0100447 }
448 reset_control_deassert(rstc);
449 reset_control_put(rstc);
Russell King7cfe2492010-07-15 10:47:14 +0100450
451 /*
452 * Read pid and cid based on size of resource
453 * they are located at end of region
454 */
455 for (pid = 0, i = 0; i < 4; i++)
456 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
457 (i * 8);
458 for (cid = 0, i = 0; i < 4; i++)
459 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
460 (i * 8);
461
Mike Leach4a2910f2019-02-13 14:41:50 +0100462 if (cid == CORESIGHT_CID) {
463 /* set the base to the start of the last 4k block */
464 void __iomem *csbase = tmp + size - 4096;
465
466 dev->uci.devarch =
467 readl(csbase + UCI_REG_DEVARCH_OFFSET);
468 dev->uci.devtype =
469 readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
470 }
471
Russell King7cfe2492010-07-15 10:47:14 +0100472 amba_put_disable_pclk(dev);
473
Mike Leach4a2910f2019-02-13 14:41:50 +0100474 if (cid == AMBA_CID || cid == CORESIGHT_CID) {
Russell King7cfe2492010-07-15 10:47:14 +0100475 dev->periphid = pid;
Mike Leach4a2910f2019-02-13 14:41:50 +0100476 dev->cid = cid;
477 }
Russell King7cfe2492010-07-15 10:47:14 +0100478
479 if (!dev->periphid)
480 ret = -ENODEV;
481 }
Russell King96b13f52006-11-30 14:04:49 +0000482
483 iounmap(tmp);
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100484 dev_pm_domain_detach(&dev->dev, true);
Russell King96b13f52006-11-30 14:04:49 +0000485
Russell King7cfe2492010-07-15 10:47:14 +0100486 if (ret)
Russell King96b13f52006-11-30 14:04:49 +0000487 goto err_release;
Russell King96b13f52006-11-30 14:04:49 +0000488
Linus Walleij97ceed12011-03-24 16:12:40 +0100489 skip_probe:
Russell King557dca52009-07-05 22:39:08 +0100490 ret = device_add(&dev->dev);
Russell King96b13f52006-11-30 14:04:49 +0000491 if (ret)
492 goto err_release;
493
Russell Kingdfb85182012-05-03 11:33:15 +0100494 if (dev->irq[0])
Russell King96b13f52006-11-30 14:04:49 +0000495 ret = device_create_file(&dev->dev, &dev_attr_irq0);
Russell Kingdfb85182012-05-03 11:33:15 +0100496 if (ret == 0 && dev->irq[1])
Russell King96b13f52006-11-30 14:04:49 +0000497 ret = device_create_file(&dev->dev, &dev_attr_irq1);
498 if (ret == 0)
499 return ret;
500
501 device_unregister(&dev->dev);
502
503 err_release:
504 release_resource(&dev->res);
505 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return ret;
Russell Kinge9634082019-10-02 17:45:11 +0100507
508 err_reset:
509 amba_put_disable_pclk(dev);
510 iounmap(tmp);
511 dev_pm_domain_detach(&dev->dev, true);
512 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100514
515/*
516 * Registration of AMBA device require reading its pid and cid registers.
517 * To do this, the device must be turned on (if it is a part of power domain)
518 * and have clocks enabled. However in some cases those resources might not be
519 * yet available. Returning EPROBE_DEFER is not a solution in such case,
520 * because callers don't handle this special error code. Instead such devices
521 * are added to the special list and their registration is retried from
522 * periodic worker, until all resources are available and registration succeeds.
523 */
524struct deferred_device {
525 struct amba_device *dev;
526 struct resource *parent;
527 struct list_head node;
528};
529
530static LIST_HEAD(deferred_devices);
531static DEFINE_MUTEX(deferred_devices_lock);
532
533static void amba_deferred_retry_func(struct work_struct *dummy);
534static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
535
536#define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
537
Rob Herring039599c2020-04-29 15:58:12 -0500538static int amba_deferred_retry(void)
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100539{
540 struct deferred_device *ddev, *tmp;
541
542 mutex_lock(&deferred_devices_lock);
543
544 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
545 int ret = amba_device_try_add(ddev->dev, ddev->parent);
546
547 if (ret == -EPROBE_DEFER)
548 continue;
549
550 list_del_init(&ddev->node);
551 kfree(ddev);
552 }
553
Rob Herring039599c2020-04-29 15:58:12 -0500554 mutex_unlock(&deferred_devices_lock);
555
556 return 0;
557}
558late_initcall(amba_deferred_retry);
559
560static void amba_deferred_retry_func(struct work_struct *dummy)
561{
562 amba_deferred_retry();
563
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100564 if (!list_empty(&deferred_devices))
565 schedule_delayed_work(&deferred_retry_work,
566 DEFERRED_DEVICE_TIMEOUT);
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100567}
568
569/**
570 * amba_device_add - add a previously allocated AMBA device structure
571 * @dev: AMBA device allocated by amba_device_alloc
572 * @parent: resource parent for this devices resources
573 *
574 * Claim the resource, and read the device cell ID if not already
575 * initialized. Register the AMBA device with the Linux device
576 * manager.
577 */
578int amba_device_add(struct amba_device *dev, struct resource *parent)
579{
580 int ret = amba_device_try_add(dev, parent);
581
582 if (ret == -EPROBE_DEFER) {
583 struct deferred_device *ddev;
584
585 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
586 if (!ddev)
587 return -ENOMEM;
588
589 ddev->dev = dev;
590 ddev->parent = parent;
591 ret = 0;
592
593 mutex_lock(&deferred_devices_lock);
594
595 if (list_empty(&deferred_devices))
596 schedule_delayed_work(&deferred_retry_work,
597 DEFERRED_DEVICE_TIMEOUT);
598 list_add_tail(&ddev->node, &deferred_devices);
599
600 mutex_unlock(&deferred_devices_lock);
601 }
602 return ret;
603}
Russell Kingd5dc9272011-12-18 11:07:47 +0000604EXPORT_SYMBOL_GPL(amba_device_add);
605
606static void amba_device_initialize(struct amba_device *dev, const char *name)
607{
608 device_initialize(&dev->dev);
609 if (name)
610 dev_set_name(&dev->dev, "%s", name);
611 dev->dev.release = amba_device_release;
612 dev->dev.bus = &amba_bustype;
Russell King446b2a92013-06-27 10:25:33 +0100613 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
Ulf Hanssonf4584882020-04-22 12:10:13 +0200614 dev->dev.dma_parms = &dev->dma_parms;
Russell Kingd5dc9272011-12-18 11:07:47 +0000615 dev->res.name = dev_name(&dev->dev);
616}
617
618/**
619 * amba_device_alloc - allocate an AMBA device
620 * @name: sysfs name of the AMBA device
621 * @base: base of AMBA device
622 * @size: size of AMBA device
623 *
624 * Allocate and initialize an AMBA device structure. Returns %NULL
625 * on failure.
626 */
627struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
628 size_t size)
629{
630 struct amba_device *dev;
631
632 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
633 if (dev) {
634 amba_device_initialize(dev, name);
635 dev->res.start = base;
636 dev->res.end = base + size - 1;
637 dev->res.flags = IORESOURCE_MEM;
638 }
639
640 return dev;
641}
642EXPORT_SYMBOL_GPL(amba_device_alloc);
643
644/**
645 * amba_device_register - register an AMBA device
646 * @dev: AMBA device to register
647 * @parent: parent memory resource
648 *
649 * Setup the AMBA device, reading the cell ID if present.
650 * Claim the resource, and register the AMBA device with
651 * the Linux device manager.
652 */
653int amba_device_register(struct amba_device *dev, struct resource *parent)
654{
655 amba_device_initialize(dev, dev->dev.init_name);
656 dev->dev.init_name = NULL;
657
Russell Kingd5dc9272011-12-18 11:07:47 +0000658 return amba_device_add(dev, parent);
659}
660
661/**
662 * amba_device_put - put an AMBA device
663 * @dev: AMBA device to put
664 */
665void amba_device_put(struct amba_device *dev)
666{
667 put_device(&dev->dev);
668}
669EXPORT_SYMBOL_GPL(amba_device_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671/**
672 * amba_device_unregister - unregister an AMBA device
673 * @dev: AMBA device to remove
674 *
675 * Remove the specified AMBA device from the Linux device
676 * manager. All files associated with this object will be
677 * destroyed, and device drivers notified that the device has
678 * been removed. The AMBA device's resources including
679 * the amba_device structure will be freed once all
680 * references to it have been dropped.
681 */
682void amba_device_unregister(struct amba_device *dev)
683{
684 device_unregister(&dev->dev);
685}
686
687
688struct find_data {
689 struct amba_device *dev;
690 struct device *parent;
691 const char *busid;
692 unsigned int id;
693 unsigned int mask;
694};
695
696static int amba_find_match(struct device *dev, void *data)
697{
698 struct find_data *d = data;
699 struct amba_device *pcdev = to_amba_device(dev);
700 int r;
701
702 r = (pcdev->periphid & d->mask) == d->id;
703 if (d->parent)
704 r &= d->parent == dev->parent;
705 if (d->busid)
Kay Sievers9d6b4c82009-03-24 16:38:22 -0700706 r &= strcmp(dev_name(dev), d->busid) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (r) {
709 get_device(dev);
710 d->dev = pcdev;
711 }
712
713 return r;
714}
715
716/**
717 * amba_find_device - locate an AMBA device given a bus id
718 * @busid: bus id for device (or NULL)
719 * @parent: parent device (or NULL)
720 * @id: peripheral ID (or 0)
721 * @mask: peripheral ID mask (or 0)
722 *
723 * Return the AMBA device corresponding to the supplied parameters.
724 * If no device matches, returns NULL.
725 *
726 * NOTE: When a valid device is found, its refcount is
727 * incremented, and must be decremented before the returned
728 * reference.
729 */
730struct amba_device *
731amba_find_device(const char *busid, struct device *parent, unsigned int id,
732 unsigned int mask)
733{
734 struct find_data data;
735
736 data.dev = NULL;
737 data.parent = parent;
738 data.busid = busid;
739 data.id = id;
740 data.mask = mask;
741
742 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
743
744 return data.dev;
745}
746
747/**
748 * amba_request_regions - request all mem regions associated with device
749 * @dev: amba_device structure for device
750 * @name: name, or NULL to use driver name
751 */
752int amba_request_regions(struct amba_device *dev, const char *name)
753{
754 int ret = 0;
Leo Chen8afe0b92009-07-28 23:34:59 +0100755 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (!name)
758 name = dev->dev.driver->name;
759
Leo Chen8afe0b92009-07-28 23:34:59 +0100760 size = resource_size(&dev->res);
761
762 if (!request_mem_region(dev->res.start, size, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ret = -EBUSY;
764
765 return ret;
766}
767
768/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300769 * amba_release_regions - release mem regions associated with device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * @dev: amba_device structure for device
771 *
772 * Release regions claimed by a successful call to amba_request_regions.
773 */
774void amba_release_regions(struct amba_device *dev)
775{
Leo Chen8afe0b92009-07-28 23:34:59 +0100776 u32 size;
777
778 size = resource_size(&dev->res);
779 release_mem_region(dev->res.start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782EXPORT_SYMBOL(amba_driver_register);
783EXPORT_SYMBOL(amba_driver_unregister);
784EXPORT_SYMBOL(amba_device_register);
785EXPORT_SYMBOL(amba_device_unregister);
786EXPORT_SYMBOL(amba_find_device);
787EXPORT_SYMBOL(amba_request_regions);
788EXPORT_SYMBOL(amba_release_regions);