blob: 962041148482c7eb2115e3e9b67ac438a835b3d3 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Russell King934848d2009-01-08 09:58:51 +000023#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define to_amba_driver(d) container_of(d, struct amba_driver, drv)
26
Mike Leach4a2910f2019-02-13 14:41:50 +010027/* called on periphid match and class 0x9 coresight device. */
28static int
29amba_cs_uci_id_match(const struct amba_id *table, struct amba_device *dev)
30{
31 int ret = 0;
32 struct amba_cs_uci_id *uci;
33
34 uci = table->data;
35
36 /* no table data or zero mask - return match on periphid */
37 if (!uci || (uci->devarch_mask == 0))
38 return 1;
39
40 /* test against read devtype and masked devarch value */
41 ret = (dev->uci.devtype == uci->devtype) &&
42 ((dev->uci.devarch & uci->devarch_mask) == uci->devarch);
43 return ret;
44}
45
Russell Kingc862aab2011-02-19 15:55:26 +000046static const struct amba_id *
47amba_lookup(const struct amba_id *table, struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 while (table->mask) {
Mike Leach4a2910f2019-02-13 14:41:50 +010050 if (((dev->periphid & table->mask) == table->id) &&
51 ((dev->cid != CORESIGHT_CID) ||
52 (amba_cs_uci_id_match(table, dev))))
53 return table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 table++;
55 }
Mike Leach4a2910f2019-02-13 14:41:50 +010056 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010059static int amba_get_enable_pclk(struct amba_device *pcdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010061 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010063 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
64 if (IS_ERR(pcdev->pclk))
65 return PTR_ERR(pcdev->pclk);
Antonios Motakis3cf38572015-01-06 11:15:11 +010066
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010067 ret = clk_prepare_enable(pcdev->pclk);
68 if (ret)
69 clk_put(pcdev->pclk);
70
71 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010074static void amba_put_disable_pclk(struct amba_device *pcdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010076 clk_disable_unprepare(pcdev->pclk);
77 clk_put(pcdev->pclk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +010080
Antonios Motakis3cf38572015-01-06 11:15:11 +010081static ssize_t driver_override_show(struct device *_dev,
82 struct device_attribute *attr, char *buf)
83{
84 struct amba_device *dev = to_amba_device(_dev);
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +020085 ssize_t len;
Antonios Motakis3cf38572015-01-06 11:15:11 +010086
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +020087 device_lock(_dev);
88 len = sprintf(buf, "%s\n", dev->driver_override);
89 device_unlock(_dev);
90 return len;
Antonios Motakis3cf38572015-01-06 11:15:11 +010091}
92
93static ssize_t driver_override_store(struct device *_dev,
94 struct device_attribute *attr,
95 const char *buf, size_t count)
96{
97 struct amba_device *dev = to_amba_device(_dev);
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +020098 char *driver_override, *old, *cp;
Antonios Motakis3cf38572015-01-06 11:15:11 +010099
Geert Uytterhoevend2ffed52018-04-10 15:21:45 +0200100 /* We need to keep extra room for a newline */
101 if (count >= (PAGE_SIZE - 1))
Antonios Motakis3cf38572015-01-06 11:15:11 +0100102 return -EINVAL;
103
104 driver_override = kstrndup(buf, count, GFP_KERNEL);
105 if (!driver_override)
106 return -ENOMEM;
107
108 cp = strchr(driver_override, '\n');
109 if (cp)
110 *cp = '\0';
111
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +0200112 device_lock(_dev);
113 old = dev->driver_override;
Antonios Motakis3cf38572015-01-06 11:15:11 +0100114 if (strlen(driver_override)) {
115 dev->driver_override = driver_override;
116 } else {
Geert Uytterhoeven6be5b5b2018-04-10 15:21:46 +0200117 kfree(driver_override);
118 dev->driver_override = NULL;
Antonios Motakis3cf38572015-01-06 11:15:11 +0100119 }
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +0200120 device_unlock(_dev);
Antonios Motakis3cf38572015-01-06 11:15:11 +0100121
122 kfree(old);
123
124 return count;
125}
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200126static DEVICE_ATTR_RW(driver_override);
Antonios Motakis3cf38572015-01-06 11:15:11 +0100127
Russell King96b13f52006-11-30 14:04:49 +0000128#define amba_attr_func(name,fmt,arg...) \
129static ssize_t name##_show(struct device *_dev, \
130 struct device_attribute *attr, char *buf) \
131{ \
132 struct amba_device *dev = to_amba_device(_dev); \
133 return sprintf(buf, fmt, arg); \
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200134} \
135static DEVICE_ATTR_RO(name)
Russell King96b13f52006-11-30 14:04:49 +0000136
137amba_attr_func(id, "%08x\n", dev->periphid);
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200138amba_attr_func(irq0, "%u\n", dev->irq[0]);
139amba_attr_func(irq1, "%u\n", dev->irq[1]);
Russell King96b13f52006-11-30 14:04:49 +0000140amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
141 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
142 dev->res.flags);
143
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200144static struct attribute *amba_dev_attrs[] = {
145 &dev_attr_id.attr,
146 &dev_attr_resource.attr,
147 &dev_attr_driver_override.attr,
148 NULL,
Russell King96b13f52006-11-30 14:04:49 +0000149};
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200150ATTRIBUTE_GROUPS(amba_dev);
Russell King96b13f52006-11-30 14:04:49 +0000151
Uwe Kleine-König5150a8f2021-01-26 17:58:32 +0100152static int amba_match(struct device *dev, struct device_driver *drv)
153{
154 struct amba_device *pcdev = to_amba_device(dev);
155 struct amba_driver *pcdrv = to_amba_driver(drv);
156
157 /* When driver_override is set, only bind to the matching driver */
158 if (pcdev->driver_override)
159 return !strcmp(pcdev->driver_override, drv->name);
160
161 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
162}
163
164static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
165{
166 struct amba_device *pcdev = to_amba_device(dev);
167 int retval = 0;
168
169 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
170 if (retval)
171 return retval;
172
173 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
174 return retval;
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
178 * These are the device model conversion veneers; they convert the
179 * device model structures to our more specific structures.
180 */
181static int amba_probe(struct device *dev)
182{
183 struct amba_device *pcdev = to_amba_device(dev);
184 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
Russell Kingc862aab2011-02-19 15:55:26 +0000185 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
Russell King7cfe2492010-07-15 10:47:14 +0100186 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Russell King7cfe2492010-07-15 10:47:14 +0100188 do {
Stephen Boydbcd30062016-08-10 20:17:34 +0100189 ret = of_clk_set_defaults(dev->of_node, false);
190 if (ret < 0)
191 break;
192
Ulf Hansson207f1a22014-09-19 20:27:42 +0200193 ret = dev_pm_domain_attach(dev, true);
Ulf Hanssond21bc892018-04-26 10:53:05 +0200194 if (ret)
Russell King7cfe2492010-07-15 10:47:14 +0100195 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Ulf Hansson207f1a22014-09-19 20:27:42 +0200197 ret = amba_get_enable_pclk(pcdev);
198 if (ret) {
199 dev_pm_domain_detach(dev, true);
200 break;
201 }
202
Russell King92b97f02011-08-14 09:13:48 +0100203 pm_runtime_get_noresume(dev);
204 pm_runtime_set_active(dev);
205 pm_runtime_enable(dev);
206
Russell King7cfe2492010-07-15 10:47:14 +0100207 ret = pcdrv->probe(pcdev, id);
208 if (ret == 0)
209 break;
210
Russell King92b97f02011-08-14 09:13:48 +0100211 pm_runtime_disable(dev);
212 pm_runtime_set_suspended(dev);
213 pm_runtime_put_noidle(dev);
214
Russell King7cfe2492010-07-15 10:47:14 +0100215 amba_put_disable_pclk(pcdev);
Ulf Hansson207f1a22014-09-19 20:27:42 +0200216 dev_pm_domain_detach(dev, true);
Russell King7cfe2492010-07-15 10:47:14 +0100217 } while (0);
218
219 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Uwe Kleine-Königfc7a6202021-07-13 21:35:22 +0200222static void amba_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Russell King7cfe2492010-07-15 10:47:14 +0100224 struct amba_device *pcdev = to_amba_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 struct amba_driver *drv = to_amba_driver(dev->driver);
Russell King92b97f02011-08-14 09:13:48 +0100226
227 pm_runtime_get_sync(dev);
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100228 if (drv->remove)
Uwe Kleine-König3fd269e2021-01-26 17:58:34 +0100229 drv->remove(pcdev);
Russell King92b97f02011-08-14 09:13:48 +0100230 pm_runtime_put_noidle(dev);
231
232 /* Undo the runtime PM settings in amba_probe() */
233 pm_runtime_disable(dev);
234 pm_runtime_set_suspended(dev);
235 pm_runtime_put_noidle(dev);
Russell King7cfe2492010-07-15 10:47:14 +0100236
237 amba_put_disable_pclk(pcdev);
Ulf Hansson207f1a22014-09-19 20:27:42 +0200238 dev_pm_domain_detach(dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241static void amba_shutdown(struct device *dev)
242{
Uwe Kleine-Königf170b592021-01-26 17:58:35 +0100243 struct amba_driver *drv;
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100244
Uwe Kleine-Königf170b592021-01-26 17:58:35 +0100245 if (!dev->driver)
246 return;
247
248 drv = to_amba_driver(dev->driver);
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100249 if (drv->shutdown)
250 drv->shutdown(to_amba_device(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Uwe Kleine-Königf170b592021-01-26 17:58:35 +0100253#ifdef CONFIG_PM
254/*
255 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
256 * enable/disable the bus clock at runtime PM suspend/resume as this
257 * does not result in loss of context.
258 */
259static int amba_pm_runtime_suspend(struct device *dev)
260{
261 struct amba_device *pcdev = to_amba_device(dev);
262 int ret = pm_generic_runtime_suspend(dev);
263
264 if (ret == 0 && dev->driver) {
265 if (pm_runtime_is_irq_safe(dev))
266 clk_disable(pcdev->pclk);
267 else
268 clk_disable_unprepare(pcdev->pclk);
269 }
270
271 return ret;
272}
273
274static int amba_pm_runtime_resume(struct device *dev)
275{
276 struct amba_device *pcdev = to_amba_device(dev);
277 int ret;
278
279 if (dev->driver) {
280 if (pm_runtime_is_irq_safe(dev))
281 ret = clk_enable(pcdev->pclk);
282 else
283 ret = clk_prepare_enable(pcdev->pclk);
284 /* Failure is probably fatal to the system, but... */
285 if (ret)
286 return ret;
287 }
288
289 return pm_generic_runtime_resume(dev);
290}
291#endif /* CONFIG_PM */
292
293static const struct dev_pm_ops amba_pm = {
294 .suspend = pm_generic_suspend,
295 .resume = pm_generic_resume,
296 .freeze = pm_generic_freeze,
297 .thaw = pm_generic_thaw,
298 .poweroff = pm_generic_poweroff,
299 .restore = pm_generic_restore,
300 SET_RUNTIME_PM_OPS(
301 amba_pm_runtime_suspend,
302 amba_pm_runtime_resume,
303 NULL
304 )
305};
306
307/*
308 * Primecells are part of the Advanced Microcontroller Bus Architecture,
309 * so we call the bus "amba".
310 * DMA configuration for platform and AMBA bus is same. So here we reuse
311 * platform's DMA config routine.
312 */
313struct bus_type amba_bustype = {
314 .name = "amba",
315 .dev_groups = amba_dev_groups,
316 .match = amba_match,
317 .uevent = amba_uevent,
318 .probe = amba_probe,
319 .remove = amba_remove,
320 .shutdown = amba_shutdown,
321 .dma_configure = platform_dma_configure,
322 .pm = &amba_pm,
323};
324EXPORT_SYMBOL_GPL(amba_bustype);
325
326static int __init amba_init(void)
327{
328 return bus_register(&amba_bustype);
329}
330
331postcore_initcall(amba_init);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/**
334 * amba_driver_register - register an AMBA device driver
335 * @drv: amba device driver structure
336 *
337 * Register an AMBA device driver with the Linux device model
338 * core. If devices pre-exist, the drivers probe function will
339 * be called.
340 */
341int amba_driver_register(struct amba_driver *drv)
342{
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100343 if (!drv->probe)
344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Uwe Kleine-Königde5d7ad2021-01-26 17:58:31 +0100346 drv->drv.bus = &amba_bustype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 return driver_register(&drv->drv);
349}
350
351/**
352 * amba_driver_unregister - remove an AMBA device driver
353 * @drv: AMBA device driver structure to remove
354 *
355 * Unregister an AMBA device driver from the Linux device
356 * model. The device model will call the drivers remove function
357 * for each device the device driver is currently handling.
358 */
359void amba_driver_unregister(struct amba_driver *drv)
360{
361 driver_unregister(&drv->drv);
362}
363
364
365static void amba_device_release(struct device *dev)
366{
367 struct amba_device *d = to_amba_device(dev);
368
369 if (d->res.parent)
370 release_resource(&d->res);
371 kfree(d);
372}
373
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100374static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Leo Chen8afe0b92009-07-28 23:34:59 +0100376 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 void __iomem *tmp;
378 int i, ret;
379
Russell King2eac58d2011-12-18 11:43:56 +0000380 WARN_ON(dev->irq[0] == (unsigned int)-1);
381 WARN_ON(dev->irq[1] == (unsigned int)-1);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 ret = request_resource(parent, &dev->res);
Russell King96b13f52006-11-30 14:04:49 +0000384 if (ret)
385 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Walleij97ceed12011-03-24 16:12:40 +0100387 /* Hard-coded primecell ID instead of plug-n-play */
388 if (dev->periphid != 0)
389 goto skip_probe;
390
Leo Chen8afe0b92009-07-28 23:34:59 +0100391 /*
392 * Dynamically calculate the size of the resource
393 * and use this for iomap
394 */
395 size = resource_size(&dev->res);
396 tmp = ioremap(dev->res.start, size);
Russell King96b13f52006-11-30 14:04:49 +0000397 if (!tmp) {
398 ret = -ENOMEM;
399 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Russell King96b13f52006-11-30 14:04:49 +0000401
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100402 ret = dev_pm_domain_attach(&dev->dev, true);
Ulf Hanssond21bc892018-04-26 10:53:05 +0200403 if (ret) {
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100404 iounmap(tmp);
405 goto err_release;
406 }
407
Russell King7cfe2492010-07-15 10:47:14 +0100408 ret = amba_get_enable_pclk(dev);
409 if (ret == 0) {
410 u32 pid, cid;
DINH L NGUYEN79bdcb202019-09-04 02:13:08 +0100411 struct reset_control *rstc;
412
413 /*
414 * Find reset control(s) of the amba bus and de-assert them.
415 */
416 rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
417 if (IS_ERR(rstc)) {
Russell Kinge9634082019-10-02 17:45:11 +0100418 ret = PTR_ERR(rstc);
419 if (ret != -EPROBE_DEFER)
420 dev_err(&dev->dev, "can't get reset: %d\n",
421 ret);
422 goto err_reset;
DINH L NGUYEN79bdcb202019-09-04 02:13:08 +0100423 }
424 reset_control_deassert(rstc);
425 reset_control_put(rstc);
Russell King7cfe2492010-07-15 10:47:14 +0100426
427 /*
428 * Read pid and cid based on size of resource
429 * they are located at end of region
430 */
431 for (pid = 0, i = 0; i < 4; i++)
432 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
433 (i * 8);
434 for (cid = 0, i = 0; i < 4; i++)
435 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
436 (i * 8);
437
Mike Leach4a2910f2019-02-13 14:41:50 +0100438 if (cid == CORESIGHT_CID) {
439 /* set the base to the start of the last 4k block */
440 void __iomem *csbase = tmp + size - 4096;
441
442 dev->uci.devarch =
443 readl(csbase + UCI_REG_DEVARCH_OFFSET);
444 dev->uci.devtype =
445 readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
446 }
447
Russell King7cfe2492010-07-15 10:47:14 +0100448 amba_put_disable_pclk(dev);
449
Mike Leach4a2910f2019-02-13 14:41:50 +0100450 if (cid == AMBA_CID || cid == CORESIGHT_CID) {
Russell King7cfe2492010-07-15 10:47:14 +0100451 dev->periphid = pid;
Mike Leach4a2910f2019-02-13 14:41:50 +0100452 dev->cid = cid;
453 }
Russell King7cfe2492010-07-15 10:47:14 +0100454
455 if (!dev->periphid)
456 ret = -ENODEV;
457 }
Russell King96b13f52006-11-30 14:04:49 +0000458
459 iounmap(tmp);
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100460 dev_pm_domain_detach(&dev->dev, true);
Russell King96b13f52006-11-30 14:04:49 +0000461
Russell King7cfe2492010-07-15 10:47:14 +0100462 if (ret)
Russell King96b13f52006-11-30 14:04:49 +0000463 goto err_release;
Russell King96b13f52006-11-30 14:04:49 +0000464
Linus Walleij97ceed12011-03-24 16:12:40 +0100465 skip_probe:
Russell King557dca52009-07-05 22:39:08 +0100466 ret = device_add(&dev->dev);
Russell King96b13f52006-11-30 14:04:49 +0000467 if (ret)
468 goto err_release;
469
Russell Kingdfb85182012-05-03 11:33:15 +0100470 if (dev->irq[0])
Russell King96b13f52006-11-30 14:04:49 +0000471 ret = device_create_file(&dev->dev, &dev_attr_irq0);
Russell Kingdfb85182012-05-03 11:33:15 +0100472 if (ret == 0 && dev->irq[1])
Russell King96b13f52006-11-30 14:04:49 +0000473 ret = device_create_file(&dev->dev, &dev_attr_irq1);
474 if (ret == 0)
475 return ret;
476
477 device_unregister(&dev->dev);
478
479 err_release:
480 release_resource(&dev->res);
481 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return ret;
Russell Kinge9634082019-10-02 17:45:11 +0100483
484 err_reset:
485 amba_put_disable_pclk(dev);
486 iounmap(tmp);
487 dev_pm_domain_detach(&dev->dev, true);
488 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100490
491/*
492 * Registration of AMBA device require reading its pid and cid registers.
493 * To do this, the device must be turned on (if it is a part of power domain)
494 * and have clocks enabled. However in some cases those resources might not be
495 * yet available. Returning EPROBE_DEFER is not a solution in such case,
496 * because callers don't handle this special error code. Instead such devices
497 * are added to the special list and their registration is retried from
498 * periodic worker, until all resources are available and registration succeeds.
499 */
500struct deferred_device {
501 struct amba_device *dev;
502 struct resource *parent;
503 struct list_head node;
504};
505
506static LIST_HEAD(deferred_devices);
507static DEFINE_MUTEX(deferred_devices_lock);
508
509static void amba_deferred_retry_func(struct work_struct *dummy);
510static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
511
512#define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
513
Rob Herring039599c2020-04-29 15:58:12 -0500514static int amba_deferred_retry(void)
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100515{
516 struct deferred_device *ddev, *tmp;
517
518 mutex_lock(&deferred_devices_lock);
519
520 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
521 int ret = amba_device_try_add(ddev->dev, ddev->parent);
522
523 if (ret == -EPROBE_DEFER)
524 continue;
525
526 list_del_init(&ddev->node);
527 kfree(ddev);
528 }
529
Rob Herring039599c2020-04-29 15:58:12 -0500530 mutex_unlock(&deferred_devices_lock);
531
532 return 0;
533}
534late_initcall(amba_deferred_retry);
535
536static void amba_deferred_retry_func(struct work_struct *dummy)
537{
538 amba_deferred_retry();
539
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100540 if (!list_empty(&deferred_devices))
541 schedule_delayed_work(&deferred_retry_work,
542 DEFERRED_DEVICE_TIMEOUT);
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100543}
544
545/**
546 * amba_device_add - add a previously allocated AMBA device structure
547 * @dev: AMBA device allocated by amba_device_alloc
548 * @parent: resource parent for this devices resources
549 *
550 * Claim the resource, and read the device cell ID if not already
551 * initialized. Register the AMBA device with the Linux device
552 * manager.
553 */
554int amba_device_add(struct amba_device *dev, struct resource *parent)
555{
556 int ret = amba_device_try_add(dev, parent);
557
558 if (ret == -EPROBE_DEFER) {
559 struct deferred_device *ddev;
560
561 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
562 if (!ddev)
563 return -ENOMEM;
564
565 ddev->dev = dev;
566 ddev->parent = parent;
567 ret = 0;
568
569 mutex_lock(&deferred_devices_lock);
570
571 if (list_empty(&deferred_devices))
572 schedule_delayed_work(&deferred_retry_work,
573 DEFERRED_DEVICE_TIMEOUT);
574 list_add_tail(&ddev->node, &deferred_devices);
575
576 mutex_unlock(&deferred_devices_lock);
577 }
578 return ret;
579}
Russell Kingd5dc9272011-12-18 11:07:47 +0000580EXPORT_SYMBOL_GPL(amba_device_add);
581
Linus Walleij6026aa92012-04-03 11:58:42 +0100582static struct amba_device *
583amba_aphb_device_add(struct device *parent, const char *name,
584 resource_size_t base, size_t size, int irq1, int irq2,
Linus Walleij3ad909b2012-11-30 16:30:43 +0100585 void *pdata, unsigned int periphid, u64 dma_mask,
586 struct resource *resbase)
Linus Walleij6026aa92012-04-03 11:58:42 +0100587{
588 struct amba_device *dev;
589 int ret;
590
591 dev = amba_device_alloc(name, base, size);
592 if (!dev)
593 return ERR_PTR(-ENOMEM);
594
Linus Walleij6026aa92012-04-03 11:58:42 +0100595 dev->dev.coherent_dma_mask = dma_mask;
596 dev->irq[0] = irq1;
597 dev->irq[1] = irq2;
598 dev->periphid = periphid;
599 dev->dev.platform_data = pdata;
600 dev->dev.parent = parent;
601
Linus Walleij3ad909b2012-11-30 16:30:43 +0100602 ret = amba_device_add(dev, resbase);
Linus Walleij6026aa92012-04-03 11:58:42 +0100603 if (ret) {
604 amba_device_put(dev);
605 return ERR_PTR(ret);
606 }
607
608 return dev;
609}
610
611struct amba_device *
612amba_apb_device_add(struct device *parent, const char *name,
613 resource_size_t base, size_t size, int irq1, int irq2,
614 void *pdata, unsigned int periphid)
615{
616 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
Linus Walleij3ad909b2012-11-30 16:30:43 +0100617 periphid, 0, &iomem_resource);
Linus Walleij6026aa92012-04-03 11:58:42 +0100618}
619EXPORT_SYMBOL_GPL(amba_apb_device_add);
620
621struct amba_device *
622amba_ahb_device_add(struct device *parent, const char *name,
623 resource_size_t base, size_t size, int irq1, int irq2,
624 void *pdata, unsigned int periphid)
625{
626 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
Linus Walleij3ad909b2012-11-30 16:30:43 +0100627 periphid, ~0ULL, &iomem_resource);
Linus Walleij6026aa92012-04-03 11:58:42 +0100628}
629EXPORT_SYMBOL_GPL(amba_ahb_device_add);
630
Linus Walleij3ad909b2012-11-30 16:30:43 +0100631struct amba_device *
632amba_apb_device_add_res(struct device *parent, const char *name,
633 resource_size_t base, size_t size, int irq1,
634 int irq2, void *pdata, unsigned int periphid,
635 struct resource *resbase)
636{
637 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
638 periphid, 0, resbase);
639}
640EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
641
642struct amba_device *
643amba_ahb_device_add_res(struct device *parent, const char *name,
644 resource_size_t base, size_t size, int irq1,
645 int irq2, void *pdata, unsigned int periphid,
646 struct resource *resbase)
647{
648 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
649 periphid, ~0ULL, resbase);
650}
651EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
652
653
Russell Kingd5dc9272011-12-18 11:07:47 +0000654static void amba_device_initialize(struct amba_device *dev, const char *name)
655{
656 device_initialize(&dev->dev);
657 if (name)
658 dev_set_name(&dev->dev, "%s", name);
659 dev->dev.release = amba_device_release;
660 dev->dev.bus = &amba_bustype;
Russell King446b2a92013-06-27 10:25:33 +0100661 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
Ulf Hanssonf4584882020-04-22 12:10:13 +0200662 dev->dev.dma_parms = &dev->dma_parms;
Russell Kingd5dc9272011-12-18 11:07:47 +0000663 dev->res.name = dev_name(&dev->dev);
664}
665
666/**
667 * amba_device_alloc - allocate an AMBA device
668 * @name: sysfs name of the AMBA device
669 * @base: base of AMBA device
670 * @size: size of AMBA device
671 *
672 * Allocate and initialize an AMBA device structure. Returns %NULL
673 * on failure.
674 */
675struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
676 size_t size)
677{
678 struct amba_device *dev;
679
680 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
681 if (dev) {
682 amba_device_initialize(dev, name);
683 dev->res.start = base;
684 dev->res.end = base + size - 1;
685 dev->res.flags = IORESOURCE_MEM;
686 }
687
688 return dev;
689}
690EXPORT_SYMBOL_GPL(amba_device_alloc);
691
692/**
693 * amba_device_register - register an AMBA device
694 * @dev: AMBA device to register
695 * @parent: parent memory resource
696 *
697 * Setup the AMBA device, reading the cell ID if present.
698 * Claim the resource, and register the AMBA device with
699 * the Linux device manager.
700 */
701int amba_device_register(struct amba_device *dev, struct resource *parent)
702{
703 amba_device_initialize(dev, dev->dev.init_name);
704 dev->dev.init_name = NULL;
705
Russell Kingd5dc9272011-12-18 11:07:47 +0000706 return amba_device_add(dev, parent);
707}
708
709/**
710 * amba_device_put - put an AMBA device
711 * @dev: AMBA device to put
712 */
713void amba_device_put(struct amba_device *dev)
714{
715 put_device(&dev->dev);
716}
717EXPORT_SYMBOL_GPL(amba_device_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719/**
720 * amba_device_unregister - unregister an AMBA device
721 * @dev: AMBA device to remove
722 *
723 * Remove the specified AMBA device from the Linux device
724 * manager. All files associated with this object will be
725 * destroyed, and device drivers notified that the device has
726 * been removed. The AMBA device's resources including
727 * the amba_device structure will be freed once all
728 * references to it have been dropped.
729 */
730void amba_device_unregister(struct amba_device *dev)
731{
732 device_unregister(&dev->dev);
733}
734
735
736struct find_data {
737 struct amba_device *dev;
738 struct device *parent;
739 const char *busid;
740 unsigned int id;
741 unsigned int mask;
742};
743
744static int amba_find_match(struct device *dev, void *data)
745{
746 struct find_data *d = data;
747 struct amba_device *pcdev = to_amba_device(dev);
748 int r;
749
750 r = (pcdev->periphid & d->mask) == d->id;
751 if (d->parent)
752 r &= d->parent == dev->parent;
753 if (d->busid)
Kay Sievers9d6b4c82009-03-24 16:38:22 -0700754 r &= strcmp(dev_name(dev), d->busid) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (r) {
757 get_device(dev);
758 d->dev = pcdev;
759 }
760
761 return r;
762}
763
764/**
765 * amba_find_device - locate an AMBA device given a bus id
766 * @busid: bus id for device (or NULL)
767 * @parent: parent device (or NULL)
768 * @id: peripheral ID (or 0)
769 * @mask: peripheral ID mask (or 0)
770 *
771 * Return the AMBA device corresponding to the supplied parameters.
772 * If no device matches, returns NULL.
773 *
774 * NOTE: When a valid device is found, its refcount is
775 * incremented, and must be decremented before the returned
776 * reference.
777 */
778struct amba_device *
779amba_find_device(const char *busid, struct device *parent, unsigned int id,
780 unsigned int mask)
781{
782 struct find_data data;
783
784 data.dev = NULL;
785 data.parent = parent;
786 data.busid = busid;
787 data.id = id;
788 data.mask = mask;
789
790 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
791
792 return data.dev;
793}
794
795/**
796 * amba_request_regions - request all mem regions associated with device
797 * @dev: amba_device structure for device
798 * @name: name, or NULL to use driver name
799 */
800int amba_request_regions(struct amba_device *dev, const char *name)
801{
802 int ret = 0;
Leo Chen8afe0b92009-07-28 23:34:59 +0100803 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (!name)
806 name = dev->dev.driver->name;
807
Leo Chen8afe0b92009-07-28 23:34:59 +0100808 size = resource_size(&dev->res);
809
810 if (!request_mem_region(dev->res.start, size, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 ret = -EBUSY;
812
813 return ret;
814}
815
816/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300817 * amba_release_regions - release mem regions associated with device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * @dev: amba_device structure for device
819 *
820 * Release regions claimed by a successful call to amba_request_regions.
821 */
822void amba_release_regions(struct amba_device *dev)
823{
Leo Chen8afe0b92009-07-28 23:34:59 +0100824 u32 size;
825
826 size = resource_size(&dev->res);
827 release_mem_region(dev->res.start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830EXPORT_SYMBOL(amba_driver_register);
831EXPORT_SYMBOL(amba_driver_unregister);
832EXPORT_SYMBOL(amba_device_register);
833EXPORT_SYMBOL(amba_device_unregister);
834EXPORT_SYMBOL(amba_find_device);
835EXPORT_SYMBOL(amba_request_regions);
836EXPORT_SYMBOL(amba_release_regions);