blob: 8558b629880b17ae4906f6ee22dfa3a10e6d2e29 [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
59static int amba_match(struct device *dev, struct device_driver *drv)
60{
61 struct amba_device *pcdev = to_amba_device(dev);
62 struct amba_driver *pcdrv = to_amba_driver(drv);
63
Antonios Motakis3cf38572015-01-06 11:15:11 +010064 /* When driver_override is set, only bind to the matching driver */
65 if (pcdev->driver_override)
66 return !strcmp(pcdev->driver_override, drv->name);
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return amba_lookup(pcdrv->id_table, pcdev) != NULL;
69}
70
Kay Sievers7eff2e72007-08-14 15:15:12 +020071static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct amba_device *pcdev = to_amba_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +020074 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Kay Sievers7eff2e72007-08-14 15:15:12 +020076 retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
Dave Martin523817b2011-10-05 14:44:57 +010077 if (retval)
78 return retval;
79
80 retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
Eric Rannaudbf624562007-03-30 22:23:12 -070081 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Antonios Motakis3cf38572015-01-06 11:15:11 +010084static ssize_t driver_override_show(struct device *_dev,
85 struct device_attribute *attr, char *buf)
86{
87 struct amba_device *dev = to_amba_device(_dev);
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +020088 ssize_t len;
Antonios Motakis3cf38572015-01-06 11:15:11 +010089
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +020090 device_lock(_dev);
91 len = sprintf(buf, "%s\n", dev->driver_override);
92 device_unlock(_dev);
93 return len;
Antonios Motakis3cf38572015-01-06 11:15:11 +010094}
95
96static ssize_t driver_override_store(struct device *_dev,
97 struct device_attribute *attr,
98 const char *buf, size_t count)
99{
100 struct amba_device *dev = to_amba_device(_dev);
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +0200101 char *driver_override, *old, *cp;
Antonios Motakis3cf38572015-01-06 11:15:11 +0100102
Geert Uytterhoevend2ffed52018-04-10 15:21:45 +0200103 /* We need to keep extra room for a newline */
104 if (count >= (PAGE_SIZE - 1))
Antonios Motakis3cf38572015-01-06 11:15:11 +0100105 return -EINVAL;
106
107 driver_override = kstrndup(buf, count, GFP_KERNEL);
108 if (!driver_override)
109 return -ENOMEM;
110
111 cp = strchr(driver_override, '\n');
112 if (cp)
113 *cp = '\0';
114
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +0200115 device_lock(_dev);
116 old = dev->driver_override;
Antonios Motakis3cf38572015-01-06 11:15:11 +0100117 if (strlen(driver_override)) {
118 dev->driver_override = driver_override;
119 } else {
Geert Uytterhoeven6be5b5b2018-04-10 15:21:46 +0200120 kfree(driver_override);
121 dev->driver_override = NULL;
Antonios Motakis3cf38572015-01-06 11:15:11 +0100122 }
Geert Uytterhoeven6a7228d2018-04-10 15:21:44 +0200123 device_unlock(_dev);
Antonios Motakis3cf38572015-01-06 11:15:11 +0100124
125 kfree(old);
126
127 return count;
128}
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200129static DEVICE_ATTR_RW(driver_override);
Antonios Motakis3cf38572015-01-06 11:15:11 +0100130
Russell King96b13f52006-11-30 14:04:49 +0000131#define amba_attr_func(name,fmt,arg...) \
132static ssize_t name##_show(struct device *_dev, \
133 struct device_attribute *attr, char *buf) \
134{ \
135 struct amba_device *dev = to_amba_device(_dev); \
136 return sprintf(buf, fmt, arg); \
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200137} \
138static DEVICE_ATTR_RO(name)
Russell King96b13f52006-11-30 14:04:49 +0000139
140amba_attr_func(id, "%08x\n", dev->periphid);
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200141amba_attr_func(irq0, "%u\n", dev->irq[0]);
142amba_attr_func(irq1, "%u\n", dev->irq[1]);
Russell King96b13f52006-11-30 14:04:49 +0000143amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
144 (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
145 dev->res.flags);
146
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200147static struct attribute *amba_dev_attrs[] = {
148 &dev_attr_id.attr,
149 &dev_attr_resource.attr,
150 &dev_attr_driver_override.attr,
151 NULL,
Russell King96b13f52006-11-30 14:04:49 +0000152};
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200153ATTRIBUTE_GROUPS(amba_dev);
Russell King96b13f52006-11-30 14:04:49 +0000154
Ulf Hanssonf210c532014-02-12 14:06:43 +0100155#ifdef CONFIG_PM
Russell King92b97f02011-08-14 09:13:48 +0100156/*
157 * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
158 * enable/disable the bus clock at runtime PM suspend/resume as this
Mark Brown1e458602012-04-13 13:11:50 +0100159 * does not result in loss of context.
Russell King92b97f02011-08-14 09:13:48 +0100160 */
161static int amba_pm_runtime_suspend(struct device *dev)
162{
163 struct amba_device *pcdev = to_amba_device(dev);
164 int ret = pm_generic_runtime_suspend(dev);
165
Krzysztof Kozlowski5670c2a2014-11-14 09:48:27 +0100166 if (ret == 0 && dev->driver) {
167 if (pm_runtime_is_irq_safe(dev))
168 clk_disable(pcdev->pclk);
169 else
170 clk_disable_unprepare(pcdev->pclk);
171 }
Russell King92b97f02011-08-14 09:13:48 +0100172
173 return ret;
174}
175
176static int amba_pm_runtime_resume(struct device *dev)
177{
178 struct amba_device *pcdev = to_amba_device(dev);
179 int ret;
180
181 if (dev->driver) {
Krzysztof Kozlowski5670c2a2014-11-14 09:48:27 +0100182 if (pm_runtime_is_irq_safe(dev))
183 ret = clk_enable(pcdev->pclk);
184 else
185 ret = clk_prepare_enable(pcdev->pclk);
Russell King92b97f02011-08-14 09:13:48 +0100186 /* Failure is probably fatal to the system, but... */
187 if (ret)
188 return ret;
189 }
190
191 return pm_generic_runtime_resume(dev);
192}
Krzysztof Kozlowski5670c2a2014-11-14 09:48:27 +0100193#endif /* CONFIG_PM */
Russell King92b97f02011-08-14 09:13:48 +0100194
Rabin Vincentba74ec72011-02-23 04:33:17 +0100195static const struct dev_pm_ops amba_pm = {
Ulf Hansson26825cf2013-12-09 10:38:20 +0100196 .suspend = pm_generic_suspend,
197 .resume = pm_generic_resume,
198 .freeze = pm_generic_freeze,
199 .thaw = pm_generic_thaw,
200 .poweroff = pm_generic_poweroff,
201 .restore = pm_generic_restore,
Rafael J. Wysocki6ed23b82014-12-04 00:34:11 +0100202 SET_RUNTIME_PM_OPS(
Russell King92b97f02011-08-14 09:13:48 +0100203 amba_pm_runtime_suspend,
204 amba_pm_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200205 NULL
Rabin Vincentba74ec72011-02-23 04:33:17 +0100206 )
207};
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * Primecells are part of the Advanced Microcontroller Bus Architecture,
211 * so we call the bus "amba".
Nipun Gupta07397df2018-04-28 08:21:58 +0530212 * DMA configuration for platform and AMBA bus is same. So here we reuse
213 * platform's DMA config routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
Rob Herring394d5ae2011-02-12 15:58:25 +0100215struct bus_type amba_bustype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .name = "amba",
Greg Kroah-Hartman966449a2017-06-06 14:16:49 +0200217 .dev_groups = amba_dev_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .match = amba_match,
Paul Jackson6d20b032005-11-25 20:04:26 -0800219 .uevent = amba_uevent,
Nipun Gupta07397df2018-04-28 08:21:58 +0530220 .dma_configure = platform_dma_configure,
Ulf Hansson26825cf2013-12-09 10:38:20 +0100221 .pm = &amba_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
Kim Phillipse9ac68c2018-05-14 21:51:55 +0100223EXPORT_SYMBOL_GPL(amba_bustype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225static int __init amba_init(void)
226{
227 return bus_register(&amba_bustype);
228}
229
230postcore_initcall(amba_init);
231
Russell King7cfe2492010-07-15 10:47:14 +0100232static int amba_get_enable_pclk(struct amba_device *pcdev)
233{
Russell King7cfe2492010-07-15 10:47:14 +0100234 int ret;
235
Ulf Hansson89a5c982013-12-09 10:39:13 +0100236 pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
237 if (IS_ERR(pcdev->pclk))
238 return PTR_ERR(pcdev->pclk);
Russell King7cfe2492010-07-15 10:47:14 +0100239
Ulf Hansson89a5c982013-12-09 10:39:13 +0100240 ret = clk_prepare_enable(pcdev->pclk);
241 if (ret)
242 clk_put(pcdev->pclk);
Russell King7cfe2492010-07-15 10:47:14 +0100243
244 return ret;
245}
246
247static void amba_put_disable_pclk(struct amba_device *pcdev)
248{
Ulf Hansson89a5c982013-12-09 10:39:13 +0100249 clk_disable_unprepare(pcdev->pclk);
250 clk_put(pcdev->pclk);
Russell King7cfe2492010-07-15 10:47:14 +0100251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/*
254 * These are the device model conversion veneers; they convert the
255 * device model structures to our more specific structures.
256 */
257static int amba_probe(struct device *dev)
258{
259 struct amba_device *pcdev = to_amba_device(dev);
260 struct amba_driver *pcdrv = to_amba_driver(dev->driver);
Russell Kingc862aab2011-02-19 15:55:26 +0000261 const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
Russell King7cfe2492010-07-15 10:47:14 +0100262 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Russell King7cfe2492010-07-15 10:47:14 +0100264 do {
Stephen Boydbcd30062016-08-10 20:17:34 +0100265 ret = of_clk_set_defaults(dev->of_node, false);
266 if (ret < 0)
267 break;
268
Ulf Hansson207f1a22014-09-19 20:27:42 +0200269 ret = dev_pm_domain_attach(dev, true);
Ulf Hanssond21bc892018-04-26 10:53:05 +0200270 if (ret)
Russell King7cfe2492010-07-15 10:47:14 +0100271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Ulf Hansson207f1a22014-09-19 20:27:42 +0200273 ret = amba_get_enable_pclk(pcdev);
274 if (ret) {
275 dev_pm_domain_detach(dev, true);
276 break;
277 }
278
Russell King92b97f02011-08-14 09:13:48 +0100279 pm_runtime_get_noresume(dev);
280 pm_runtime_set_active(dev);
281 pm_runtime_enable(dev);
282
Russell King7cfe2492010-07-15 10:47:14 +0100283 ret = pcdrv->probe(pcdev, id);
284 if (ret == 0)
285 break;
286
Russell King92b97f02011-08-14 09:13:48 +0100287 pm_runtime_disable(dev);
288 pm_runtime_set_suspended(dev);
289 pm_runtime_put_noidle(dev);
290
Russell King7cfe2492010-07-15 10:47:14 +0100291 amba_put_disable_pclk(pcdev);
Ulf Hansson207f1a22014-09-19 20:27:42 +0200292 dev_pm_domain_detach(dev, true);
Russell King7cfe2492010-07-15 10:47:14 +0100293 } while (0);
294
295 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298static int amba_remove(struct device *dev)
299{
Russell King7cfe2492010-07-15 10:47:14 +0100300 struct amba_device *pcdev = to_amba_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct amba_driver *drv = to_amba_driver(dev->driver);
Russell King92b97f02011-08-14 09:13:48 +0100302 int ret;
303
304 pm_runtime_get_sync(dev);
305 ret = drv->remove(pcdev);
306 pm_runtime_put_noidle(dev);
307
308 /* Undo the runtime PM settings in amba_probe() */
309 pm_runtime_disable(dev);
310 pm_runtime_set_suspended(dev);
311 pm_runtime_put_noidle(dev);
Russell King7cfe2492010-07-15 10:47:14 +0100312
313 amba_put_disable_pclk(pcdev);
Ulf Hansson207f1a22014-09-19 20:27:42 +0200314 dev_pm_domain_detach(dev, true);
Russell King7cfe2492010-07-15 10:47:14 +0100315
316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
319static void amba_shutdown(struct device *dev)
320{
321 struct amba_driver *drv = to_amba_driver(dev->driver);
322 drv->shutdown(to_amba_device(dev));
323}
324
325/**
326 * amba_driver_register - register an AMBA device driver
327 * @drv: amba device driver structure
328 *
329 * Register an AMBA device driver with the Linux device model
330 * core. If devices pre-exist, the drivers probe function will
331 * be called.
332 */
333int amba_driver_register(struct amba_driver *drv)
334{
335 drv->drv.bus = &amba_bustype;
336
337#define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
338 SETFN(probe);
339 SETFN(remove);
340 SETFN(shutdown);
341
342 return driver_register(&drv->drv);
343}
344
345/**
346 * amba_driver_unregister - remove an AMBA device driver
347 * @drv: AMBA device driver structure to remove
348 *
349 * Unregister an AMBA device driver from the Linux device
350 * model. The device model will call the drivers remove function
351 * for each device the device driver is currently handling.
352 */
353void amba_driver_unregister(struct amba_driver *drv)
354{
355 driver_unregister(&drv->drv);
356}
357
358
359static void amba_device_release(struct device *dev)
360{
361 struct amba_device *d = to_amba_device(dev);
362
363 if (d->res.parent)
364 release_resource(&d->res);
365 kfree(d);
366}
367
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100368static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Leo Chen8afe0b92009-07-28 23:34:59 +0100370 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 void __iomem *tmp;
372 int i, ret;
373
Russell King2eac58d2011-12-18 11:43:56 +0000374 WARN_ON(dev->irq[0] == (unsigned int)-1);
375 WARN_ON(dev->irq[1] == (unsigned int)-1);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ret = request_resource(parent, &dev->res);
Russell King96b13f52006-11-30 14:04:49 +0000378 if (ret)
379 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Walleij97ceed12011-03-24 16:12:40 +0100381 /* Hard-coded primecell ID instead of plug-n-play */
382 if (dev->periphid != 0)
383 goto skip_probe;
384
Leo Chen8afe0b92009-07-28 23:34:59 +0100385 /*
386 * Dynamically calculate the size of the resource
387 * and use this for iomap
388 */
389 size = resource_size(&dev->res);
390 tmp = ioremap(dev->res.start, size);
Russell King96b13f52006-11-30 14:04:49 +0000391 if (!tmp) {
392 ret = -ENOMEM;
393 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Russell King96b13f52006-11-30 14:04:49 +0000395
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100396 ret = dev_pm_domain_attach(&dev->dev, true);
Ulf Hanssond21bc892018-04-26 10:53:05 +0200397 if (ret) {
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100398 iounmap(tmp);
399 goto err_release;
400 }
401
Russell King7cfe2492010-07-15 10:47:14 +0100402 ret = amba_get_enable_pclk(dev);
403 if (ret == 0) {
404 u32 pid, cid;
DINH L NGUYEN79bdcb202019-09-04 02:13:08 +0100405 struct reset_control *rstc;
406
407 /*
408 * Find reset control(s) of the amba bus and de-assert them.
409 */
410 rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
411 if (IS_ERR(rstc)) {
Russell Kinge9634082019-10-02 17:45:11 +0100412 ret = PTR_ERR(rstc);
413 if (ret != -EPROBE_DEFER)
414 dev_err(&dev->dev, "can't get reset: %d\n",
415 ret);
416 goto err_reset;
DINH L NGUYEN79bdcb202019-09-04 02:13:08 +0100417 }
418 reset_control_deassert(rstc);
419 reset_control_put(rstc);
Russell King7cfe2492010-07-15 10:47:14 +0100420
421 /*
422 * Read pid and cid based on size of resource
423 * they are located at end of region
424 */
425 for (pid = 0, i = 0; i < 4; i++)
426 pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
427 (i * 8);
428 for (cid = 0, i = 0; i < 4; i++)
429 cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
430 (i * 8);
431
Mike Leach4a2910f2019-02-13 14:41:50 +0100432 if (cid == CORESIGHT_CID) {
433 /* set the base to the start of the last 4k block */
434 void __iomem *csbase = tmp + size - 4096;
435
436 dev->uci.devarch =
437 readl(csbase + UCI_REG_DEVARCH_OFFSET);
438 dev->uci.devtype =
439 readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
440 }
441
Russell King7cfe2492010-07-15 10:47:14 +0100442 amba_put_disable_pclk(dev);
443
Mike Leach4a2910f2019-02-13 14:41:50 +0100444 if (cid == AMBA_CID || cid == CORESIGHT_CID) {
Russell King7cfe2492010-07-15 10:47:14 +0100445 dev->periphid = pid;
Mike Leach4a2910f2019-02-13 14:41:50 +0100446 dev->cid = cid;
447 }
Russell King7cfe2492010-07-15 10:47:14 +0100448
449 if (!dev->periphid)
450 ret = -ENODEV;
451 }
Russell King96b13f52006-11-30 14:04:49 +0000452
453 iounmap(tmp);
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100454 dev_pm_domain_detach(&dev->dev, true);
Russell King96b13f52006-11-30 14:04:49 +0000455
Russell King7cfe2492010-07-15 10:47:14 +0100456 if (ret)
Russell King96b13f52006-11-30 14:04:49 +0000457 goto err_release;
Russell King96b13f52006-11-30 14:04:49 +0000458
Linus Walleij97ceed12011-03-24 16:12:40 +0100459 skip_probe:
Russell King557dca52009-07-05 22:39:08 +0100460 ret = device_add(&dev->dev);
Russell King96b13f52006-11-30 14:04:49 +0000461 if (ret)
462 goto err_release;
463
Russell Kingdfb85182012-05-03 11:33:15 +0100464 if (dev->irq[0])
Russell King96b13f52006-11-30 14:04:49 +0000465 ret = device_create_file(&dev->dev, &dev_attr_irq0);
Russell Kingdfb85182012-05-03 11:33:15 +0100466 if (ret == 0 && dev->irq[1])
Russell King96b13f52006-11-30 14:04:49 +0000467 ret = device_create_file(&dev->dev, &dev_attr_irq1);
468 if (ret == 0)
469 return ret;
470
471 device_unregister(&dev->dev);
472
473 err_release:
474 release_resource(&dev->res);
475 err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return ret;
Russell Kinge9634082019-10-02 17:45:11 +0100477
478 err_reset:
479 amba_put_disable_pclk(dev);
480 iounmap(tmp);
481 dev_pm_domain_detach(&dev->dev, true);
482 goto err_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
Marek Szyprowskia41980f2016-04-21 07:58:35 +0100484
485/*
486 * Registration of AMBA device require reading its pid and cid registers.
487 * To do this, the device must be turned on (if it is a part of power domain)
488 * and have clocks enabled. However in some cases those resources might not be
489 * yet available. Returning EPROBE_DEFER is not a solution in such case,
490 * because callers don't handle this special error code. Instead such devices
491 * are added to the special list and their registration is retried from
492 * periodic worker, until all resources are available and registration succeeds.
493 */
494struct deferred_device {
495 struct amba_device *dev;
496 struct resource *parent;
497 struct list_head node;
498};
499
500static LIST_HEAD(deferred_devices);
501static DEFINE_MUTEX(deferred_devices_lock);
502
503static void amba_deferred_retry_func(struct work_struct *dummy);
504static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
505
506#define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
507
508static void amba_deferred_retry_func(struct work_struct *dummy)
509{
510 struct deferred_device *ddev, *tmp;
511
512 mutex_lock(&deferred_devices_lock);
513
514 list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
515 int ret = amba_device_try_add(ddev->dev, ddev->parent);
516
517 if (ret == -EPROBE_DEFER)
518 continue;
519
520 list_del_init(&ddev->node);
521 kfree(ddev);
522 }
523
524 if (!list_empty(&deferred_devices))
525 schedule_delayed_work(&deferred_retry_work,
526 DEFERRED_DEVICE_TIMEOUT);
527
528 mutex_unlock(&deferred_devices_lock);
529}
530
531/**
532 * amba_device_add - add a previously allocated AMBA device structure
533 * @dev: AMBA device allocated by amba_device_alloc
534 * @parent: resource parent for this devices resources
535 *
536 * Claim the resource, and read the device cell ID if not already
537 * initialized. Register the AMBA device with the Linux device
538 * manager.
539 */
540int amba_device_add(struct amba_device *dev, struct resource *parent)
541{
542 int ret = amba_device_try_add(dev, parent);
543
544 if (ret == -EPROBE_DEFER) {
545 struct deferred_device *ddev;
546
547 ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
548 if (!ddev)
549 return -ENOMEM;
550
551 ddev->dev = dev;
552 ddev->parent = parent;
553 ret = 0;
554
555 mutex_lock(&deferred_devices_lock);
556
557 if (list_empty(&deferred_devices))
558 schedule_delayed_work(&deferred_retry_work,
559 DEFERRED_DEVICE_TIMEOUT);
560 list_add_tail(&ddev->node, &deferred_devices);
561
562 mutex_unlock(&deferred_devices_lock);
563 }
564 return ret;
565}
Russell Kingd5dc9272011-12-18 11:07:47 +0000566EXPORT_SYMBOL_GPL(amba_device_add);
567
Linus Walleij6026aa92012-04-03 11:58:42 +0100568static struct amba_device *
569amba_aphb_device_add(struct device *parent, const char *name,
570 resource_size_t base, size_t size, int irq1, int irq2,
Linus Walleij3ad909b2012-11-30 16:30:43 +0100571 void *pdata, unsigned int periphid, u64 dma_mask,
572 struct resource *resbase)
Linus Walleij6026aa92012-04-03 11:58:42 +0100573{
574 struct amba_device *dev;
575 int ret;
576
577 dev = amba_device_alloc(name, base, size);
578 if (!dev)
579 return ERR_PTR(-ENOMEM);
580
Linus Walleij6026aa92012-04-03 11:58:42 +0100581 dev->dev.coherent_dma_mask = dma_mask;
582 dev->irq[0] = irq1;
583 dev->irq[1] = irq2;
584 dev->periphid = periphid;
585 dev->dev.platform_data = pdata;
586 dev->dev.parent = parent;
587
Linus Walleij3ad909b2012-11-30 16:30:43 +0100588 ret = amba_device_add(dev, resbase);
Linus Walleij6026aa92012-04-03 11:58:42 +0100589 if (ret) {
590 amba_device_put(dev);
591 return ERR_PTR(ret);
592 }
593
594 return dev;
595}
596
597struct amba_device *
598amba_apb_device_add(struct device *parent, const char *name,
599 resource_size_t base, size_t size, int irq1, int irq2,
600 void *pdata, unsigned int periphid)
601{
602 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
Linus Walleij3ad909b2012-11-30 16:30:43 +0100603 periphid, 0, &iomem_resource);
Linus Walleij6026aa92012-04-03 11:58:42 +0100604}
605EXPORT_SYMBOL_GPL(amba_apb_device_add);
606
607struct amba_device *
608amba_ahb_device_add(struct device *parent, const char *name,
609 resource_size_t base, size_t size, int irq1, int irq2,
610 void *pdata, unsigned int periphid)
611{
612 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
Linus Walleij3ad909b2012-11-30 16:30:43 +0100613 periphid, ~0ULL, &iomem_resource);
Linus Walleij6026aa92012-04-03 11:58:42 +0100614}
615EXPORT_SYMBOL_GPL(amba_ahb_device_add);
616
Linus Walleij3ad909b2012-11-30 16:30:43 +0100617struct amba_device *
618amba_apb_device_add_res(struct device *parent, const char *name,
619 resource_size_t base, size_t size, int irq1,
620 int irq2, void *pdata, unsigned int periphid,
621 struct resource *resbase)
622{
623 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
624 periphid, 0, resbase);
625}
626EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
627
628struct amba_device *
629amba_ahb_device_add_res(struct device *parent, const char *name,
630 resource_size_t base, size_t size, int irq1,
631 int irq2, void *pdata, unsigned int periphid,
632 struct resource *resbase)
633{
634 return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
635 periphid, ~0ULL, resbase);
636}
637EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
638
639
Russell Kingd5dc9272011-12-18 11:07:47 +0000640static void amba_device_initialize(struct amba_device *dev, const char *name)
641{
642 device_initialize(&dev->dev);
643 if (name)
644 dev_set_name(&dev->dev, "%s", name);
645 dev->dev.release = amba_device_release;
646 dev->dev.bus = &amba_bustype;
Russell King446b2a92013-06-27 10:25:33 +0100647 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
Ulf Hanssonf4584882020-04-22 12:10:13 +0200648 dev->dev.dma_parms = &dev->dma_parms;
Russell Kingd5dc9272011-12-18 11:07:47 +0000649 dev->res.name = dev_name(&dev->dev);
650}
651
652/**
653 * amba_device_alloc - allocate an AMBA device
654 * @name: sysfs name of the AMBA device
655 * @base: base of AMBA device
656 * @size: size of AMBA device
657 *
658 * Allocate and initialize an AMBA device structure. Returns %NULL
659 * on failure.
660 */
661struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
662 size_t size)
663{
664 struct amba_device *dev;
665
666 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
667 if (dev) {
668 amba_device_initialize(dev, name);
669 dev->res.start = base;
670 dev->res.end = base + size - 1;
671 dev->res.flags = IORESOURCE_MEM;
672 }
673
674 return dev;
675}
676EXPORT_SYMBOL_GPL(amba_device_alloc);
677
678/**
679 * amba_device_register - register an AMBA device
680 * @dev: AMBA device to register
681 * @parent: parent memory resource
682 *
683 * Setup the AMBA device, reading the cell ID if present.
684 * Claim the resource, and register the AMBA device with
685 * the Linux device manager.
686 */
687int amba_device_register(struct amba_device *dev, struct resource *parent)
688{
689 amba_device_initialize(dev, dev->dev.init_name);
690 dev->dev.init_name = NULL;
691
Russell Kingd5dc9272011-12-18 11:07:47 +0000692 return amba_device_add(dev, parent);
693}
694
695/**
696 * amba_device_put - put an AMBA device
697 * @dev: AMBA device to put
698 */
699void amba_device_put(struct amba_device *dev)
700{
701 put_device(&dev->dev);
702}
703EXPORT_SYMBOL_GPL(amba_device_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705/**
706 * amba_device_unregister - unregister an AMBA device
707 * @dev: AMBA device to remove
708 *
709 * Remove the specified AMBA device from the Linux device
710 * manager. All files associated with this object will be
711 * destroyed, and device drivers notified that the device has
712 * been removed. The AMBA device's resources including
713 * the amba_device structure will be freed once all
714 * references to it have been dropped.
715 */
716void amba_device_unregister(struct amba_device *dev)
717{
718 device_unregister(&dev->dev);
719}
720
721
722struct find_data {
723 struct amba_device *dev;
724 struct device *parent;
725 const char *busid;
726 unsigned int id;
727 unsigned int mask;
728};
729
730static int amba_find_match(struct device *dev, void *data)
731{
732 struct find_data *d = data;
733 struct amba_device *pcdev = to_amba_device(dev);
734 int r;
735
736 r = (pcdev->periphid & d->mask) == d->id;
737 if (d->parent)
738 r &= d->parent == dev->parent;
739 if (d->busid)
Kay Sievers9d6b4c82009-03-24 16:38:22 -0700740 r &= strcmp(dev_name(dev), d->busid) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (r) {
743 get_device(dev);
744 d->dev = pcdev;
745 }
746
747 return r;
748}
749
750/**
751 * amba_find_device - locate an AMBA device given a bus id
752 * @busid: bus id for device (or NULL)
753 * @parent: parent device (or NULL)
754 * @id: peripheral ID (or 0)
755 * @mask: peripheral ID mask (or 0)
756 *
757 * Return the AMBA device corresponding to the supplied parameters.
758 * If no device matches, returns NULL.
759 *
760 * NOTE: When a valid device is found, its refcount is
761 * incremented, and must be decremented before the returned
762 * reference.
763 */
764struct amba_device *
765amba_find_device(const char *busid, struct device *parent, unsigned int id,
766 unsigned int mask)
767{
768 struct find_data data;
769
770 data.dev = NULL;
771 data.parent = parent;
772 data.busid = busid;
773 data.id = id;
774 data.mask = mask;
775
776 bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
777
778 return data.dev;
779}
780
781/**
782 * amba_request_regions - request all mem regions associated with device
783 * @dev: amba_device structure for device
784 * @name: name, or NULL to use driver name
785 */
786int amba_request_regions(struct amba_device *dev, const char *name)
787{
788 int ret = 0;
Leo Chen8afe0b92009-07-28 23:34:59 +0100789 u32 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (!name)
792 name = dev->dev.driver->name;
793
Leo Chen8afe0b92009-07-28 23:34:59 +0100794 size = resource_size(&dev->res);
795
796 if (!request_mem_region(dev->res.start, size, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 ret = -EBUSY;
798
799 return ret;
800}
801
802/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300803 * amba_release_regions - release mem regions associated with device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * @dev: amba_device structure for device
805 *
806 * Release regions claimed by a successful call to amba_request_regions.
807 */
808void amba_release_regions(struct amba_device *dev)
809{
Leo Chen8afe0b92009-07-28 23:34:59 +0100810 u32 size;
811
812 size = resource_size(&dev->res);
813 release_mem_region(dev->res.start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816EXPORT_SYMBOL(amba_driver_register);
817EXPORT_SYMBOL(amba_driver_unregister);
818EXPORT_SYMBOL(amba_device_register);
819EXPORT_SYMBOL(amba_device_unregister);
820EXPORT_SYMBOL(amba_find_device);
821EXPORT_SYMBOL(amba_request_regions);
822EXPORT_SYMBOL(amba_release_regions);