Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/arch/arm/common/amba.c | |
4 | * | |
5 | * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved. | |
1da177e4 LT |
6 | */ |
7 | #include <linux/module.h> | |
8 | #include <linux/init.h> | |
9 | #include <linux/device.h> | |
4e57b681 TS |
10 | #include <linux/string.h> |
11 | #include <linux/slab.h> | |
934848da | 12 | #include <linux/io.h> |
ba74ec7f RV |
13 | #include <linux/pm.h> |
14 | #include <linux/pm_runtime.h> | |
f48c767c | 15 | #include <linux/pm_domain.h> |
a62c80e5 | 16 | #include <linux/amba/bus.h> |
a875cfbb | 17 | #include <linux/sizes.h> |
3cf38571 | 18 | #include <linux/limits.h> |
bcd3006f | 19 | #include <linux/clk/clk-conf.h> |
07397df2 | 20 | #include <linux/platform_device.h> |
79bdcb20 | 21 | #include <linux/reset.h> |
854f695c | 22 | #include <linux/of_irq.h> |
1da177e4 | 23 | |
1da177e4 LT |
24 | #define to_amba_driver(d) container_of(d, struct amba_driver, drv) |
25 | ||
4a2910fa ML |
26 | /* called on periphid match and class 0x9 coresight device. */ |
27 | static int | |
28 | amba_cs_uci_id_match(const struct amba_id *table, struct amba_device *dev) | |
1da177e4 LT |
29 | { |
30 | int ret = 0; | |
4a2910fa ML |
31 | struct amba_cs_uci_id *uci; |
32 | ||
33 | uci = table->data; | |
1da177e4 | 34 | |
4a2910fa ML |
35 | /* no table data or zero mask - return match on periphid */ |
36 | if (!uci || (uci->devarch_mask == 0)) | |
37 | return 1; | |
38 | ||
39 | /* test against read devtype and masked devarch value */ | |
40 | ret = (dev->uci.devtype == uci->devtype) && | |
41 | ((dev->uci.devarch & uci->devarch_mask) == uci->devarch); | |
42 | return ret; | |
43 | } | |
44 | ||
45 | static const struct amba_id * | |
46 | amba_lookup(const struct amba_id *table, struct amba_device *dev) | |
47 | { | |
1da177e4 | 48 | while (table->mask) { |
4a2910fa ML |
49 | if (((dev->periphid & table->mask) == table->id) && |
50 | ((dev->cid != CORESIGHT_CID) || | |
51 | (amba_cs_uci_id_match(table, dev)))) | |
52 | return table; | |
1da177e4 LT |
53 | table++; |
54 | } | |
4a2910fa | 55 | return NULL; |
1da177e4 LT |
56 | } |
57 | ||
5150a8f0 | 58 | static int amba_get_enable_pclk(struct amba_device *pcdev) |
1da177e4 | 59 | { |
5150a8f0 | 60 | int ret; |
1da177e4 | 61 | |
5150a8f0 UKK |
62 | pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk"); |
63 | if (IS_ERR(pcdev->pclk)) | |
64 | return PTR_ERR(pcdev->pclk); | |
3cf38571 | 65 | |
5150a8f0 UKK |
66 | ret = clk_prepare_enable(pcdev->pclk); |
67 | if (ret) | |
68 | clk_put(pcdev->pclk); | |
69 | ||
70 | return ret; | |
1da177e4 LT |
71 | } |
72 | ||
5150a8f0 | 73 | static void amba_put_disable_pclk(struct amba_device *pcdev) |
1da177e4 | 74 | { |
5150a8f0 UKK |
75 | clk_disable_unprepare(pcdev->pclk); |
76 | clk_put(pcdev->pclk); | |
1da177e4 | 77 | } |
1da177e4 | 78 | |
5150a8f0 | 79 | |
3cf38571 AM |
80 | static ssize_t driver_override_show(struct device *_dev, |
81 | struct device_attribute *attr, char *buf) | |
82 | { | |
83 | struct amba_device *dev = to_amba_device(_dev); | |
6a7228d9 | 84 | ssize_t len; |
3cf38571 | 85 | |
6a7228d9 GU |
86 | device_lock(_dev); |
87 | len = sprintf(buf, "%s\n", dev->driver_override); | |
88 | device_unlock(_dev); | |
89 | return len; | |
3cf38571 AM |
90 | } |
91 | ||
92 | static ssize_t driver_override_store(struct device *_dev, | |
93 | struct device_attribute *attr, | |
94 | const char *buf, size_t count) | |
95 | { | |
96 | struct amba_device *dev = to_amba_device(_dev); | |
6a7228d9 | 97 | char *driver_override, *old, *cp; |
3cf38571 | 98 | |
d2ffed51 GU |
99 | /* We need to keep extra room for a newline */ |
100 | if (count >= (PAGE_SIZE - 1)) | |
3cf38571 AM |
101 | return -EINVAL; |
102 | ||
103 | driver_override = kstrndup(buf, count, GFP_KERNEL); | |
104 | if (!driver_override) | |
105 | return -ENOMEM; | |
106 | ||
107 | cp = strchr(driver_override, '\n'); | |
108 | if (cp) | |
109 | *cp = '\0'; | |
110 | ||
6a7228d9 GU |
111 | device_lock(_dev); |
112 | old = dev->driver_override; | |
3cf38571 AM |
113 | if (strlen(driver_override)) { |
114 | dev->driver_override = driver_override; | |
115 | } else { | |
6be5b5b9 GU |
116 | kfree(driver_override); |
117 | dev->driver_override = NULL; | |
3cf38571 | 118 | } |
6a7228d9 | 119 | device_unlock(_dev); |
3cf38571 AM |
120 | |
121 | kfree(old); | |
122 | ||
123 | return count; | |
124 | } | |
966449a3 | 125 | static DEVICE_ATTR_RW(driver_override); |
3cf38571 | 126 | |
96b13f5c RK |
127 | #define amba_attr_func(name,fmt,arg...) \ |
128 | static ssize_t name##_show(struct device *_dev, \ | |
129 | struct device_attribute *attr, char *buf) \ | |
130 | { \ | |
131 | struct amba_device *dev = to_amba_device(_dev); \ | |
132 | return sprintf(buf, fmt, arg); \ | |
966449a3 GKH |
133 | } \ |
134 | static DEVICE_ATTR_RO(name) | |
96b13f5c RK |
135 | |
136 | amba_attr_func(id, "%08x\n", dev->periphid); | |
96b13f5c RK |
137 | amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n", |
138 | (unsigned long long)dev->res.start, (unsigned long long)dev->res.end, | |
139 | dev->res.flags); | |
140 | ||
966449a3 GKH |
141 | static struct attribute *amba_dev_attrs[] = { |
142 | &dev_attr_id.attr, | |
143 | &dev_attr_resource.attr, | |
144 | &dev_attr_driver_override.attr, | |
145 | NULL, | |
96b13f5c | 146 | }; |
966449a3 | 147 | ATTRIBUTE_GROUPS(amba_dev); |
96b13f5c | 148 | |
5150a8f0 UKK |
149 | static int amba_match(struct device *dev, struct device_driver *drv) |
150 | { | |
151 | struct amba_device *pcdev = to_amba_device(dev); | |
152 | struct amba_driver *pcdrv = to_amba_driver(drv); | |
153 | ||
154 | /* When driver_override is set, only bind to the matching driver */ | |
155 | if (pcdev->driver_override) | |
156 | return !strcmp(pcdev->driver_override, drv->name); | |
157 | ||
158 | return amba_lookup(pcdrv->id_table, pcdev) != NULL; | |
159 | } | |
160 | ||
161 | static int amba_uevent(struct device *dev, struct kobj_uevent_env *env) | |
162 | { | |
163 | struct amba_device *pcdev = to_amba_device(dev); | |
164 | int retval = 0; | |
165 | ||
166 | retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid); | |
167 | if (retval) | |
168 | return retval; | |
169 | ||
170 | retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid); | |
171 | return retval; | |
172 | } | |
173 | ||
dcc0a8f6 WK |
174 | static int of_amba_device_decode_irq(struct amba_device *dev) |
175 | { | |
176 | struct device_node *node = dev->dev.of_node; | |
177 | int i, irq = 0; | |
178 | ||
179 | if (IS_ENABLED(CONFIG_OF_IRQ) && node) { | |
180 | /* Decode the IRQs and address ranges */ | |
181 | for (i = 0; i < AMBA_NR_IRQS; i++) { | |
182 | irq = of_irq_get(node, i); | |
183 | if (irq < 0) { | |
184 | if (irq == -EPROBE_DEFER) | |
185 | return irq; | |
186 | irq = 0; | |
187 | } | |
188 | ||
189 | dev->irq[i] = irq; | |
190 | } | |
191 | } | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
f170b59f UKK |
196 | /* |
197 | * These are the device model conversion veneers; they convert the | |
198 | * device model structures to our more specific structures. | |
199 | */ | |
200 | static int amba_probe(struct device *dev) | |
201 | { | |
202 | struct amba_device *pcdev = to_amba_device(dev); | |
203 | struct amba_driver *pcdrv = to_amba_driver(dev->driver); | |
204 | const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev); | |
205 | int ret; | |
206 | ||
207 | do { | |
dcc0a8f6 WK |
208 | ret = of_amba_device_decode_irq(pcdev); |
209 | if (ret) | |
210 | break; | |
211 | ||
f170b59f UKK |
212 | ret = of_clk_set_defaults(dev->of_node, false); |
213 | if (ret < 0) | |
214 | break; | |
215 | ||
216 | ret = dev_pm_domain_attach(dev, true); | |
217 | if (ret) | |
218 | break; | |
219 | ||
220 | ret = amba_get_enable_pclk(pcdev); | |
221 | if (ret) { | |
222 | dev_pm_domain_detach(dev, true); | |
223 | break; | |
224 | } | |
225 | ||
226 | pm_runtime_get_noresume(dev); | |
227 | pm_runtime_set_active(dev); | |
228 | pm_runtime_enable(dev); | |
229 | ||
230 | ret = pcdrv->probe(pcdev, id); | |
231 | if (ret == 0) | |
232 | break; | |
233 | ||
234 | pm_runtime_disable(dev); | |
235 | pm_runtime_set_suspended(dev); | |
236 | pm_runtime_put_noidle(dev); | |
237 | ||
238 | amba_put_disable_pclk(pcdev); | |
239 | dev_pm_domain_detach(dev, true); | |
240 | } while (0); | |
241 | ||
242 | return ret; | |
243 | } | |
244 | ||
fc7a6209 | 245 | static void amba_remove(struct device *dev) |
f170b59f UKK |
246 | { |
247 | struct amba_device *pcdev = to_amba_device(dev); | |
248 | struct amba_driver *drv = to_amba_driver(dev->driver); | |
249 | ||
250 | pm_runtime_get_sync(dev); | |
251 | if (drv->remove) | |
252 | drv->remove(pcdev); | |
253 | pm_runtime_put_noidle(dev); | |
254 | ||
255 | /* Undo the runtime PM settings in amba_probe() */ | |
256 | pm_runtime_disable(dev); | |
257 | pm_runtime_set_suspended(dev); | |
258 | pm_runtime_put_noidle(dev); | |
259 | ||
260 | amba_put_disable_pclk(pcdev); | |
261 | dev_pm_domain_detach(dev, true); | |
f170b59f UKK |
262 | } |
263 | ||
264 | static void amba_shutdown(struct device *dev) | |
265 | { | |
266 | struct amba_driver *drv; | |
267 | ||
268 | if (!dev->driver) | |
269 | return; | |
270 | ||
271 | drv = to_amba_driver(dev->driver); | |
272 | if (drv->shutdown) | |
273 | drv->shutdown(to_amba_device(dev)); | |
274 | } | |
275 | ||
f210c53a | 276 | #ifdef CONFIG_PM |
92b97f0a RK |
277 | /* |
278 | * Hooks to provide runtime PM of the pclk (bus clock). It is safe to | |
279 | * enable/disable the bus clock at runtime PM suspend/resume as this | |
1e45860f | 280 | * does not result in loss of context. |
92b97f0a RK |
281 | */ |
282 | static int amba_pm_runtime_suspend(struct device *dev) | |
283 | { | |
284 | struct amba_device *pcdev = to_amba_device(dev); | |
285 | int ret = pm_generic_runtime_suspend(dev); | |
286 | ||
5670c2a5 KK |
287 | if (ret == 0 && dev->driver) { |
288 | if (pm_runtime_is_irq_safe(dev)) | |
289 | clk_disable(pcdev->pclk); | |
290 | else | |
291 | clk_disable_unprepare(pcdev->pclk); | |
292 | } | |
92b97f0a RK |
293 | |
294 | return ret; | |
295 | } | |
296 | ||
297 | static int amba_pm_runtime_resume(struct device *dev) | |
298 | { | |
299 | struct amba_device *pcdev = to_amba_device(dev); | |
300 | int ret; | |
301 | ||
302 | if (dev->driver) { | |
5670c2a5 KK |
303 | if (pm_runtime_is_irq_safe(dev)) |
304 | ret = clk_enable(pcdev->pclk); | |
305 | else | |
306 | ret = clk_prepare_enable(pcdev->pclk); | |
92b97f0a RK |
307 | /* Failure is probably fatal to the system, but... */ |
308 | if (ret) | |
309 | return ret; | |
310 | } | |
311 | ||
312 | return pm_generic_runtime_resume(dev); | |
313 | } | |
5670c2a5 | 314 | #endif /* CONFIG_PM */ |
92b97f0a | 315 | |
ba74ec7f | 316 | static const struct dev_pm_ops amba_pm = { |
26825cfd UH |
317 | .suspend = pm_generic_suspend, |
318 | .resume = pm_generic_resume, | |
319 | .freeze = pm_generic_freeze, | |
320 | .thaw = pm_generic_thaw, | |
321 | .poweroff = pm_generic_poweroff, | |
322 | .restore = pm_generic_restore, | |
6ed23b80 | 323 | SET_RUNTIME_PM_OPS( |
92b97f0a RK |
324 | amba_pm_runtime_suspend, |
325 | amba_pm_runtime_resume, | |
45f0a85c | 326 | NULL |
ba74ec7f RV |
327 | ) |
328 | }; | |
329 | ||
1da177e4 LT |
330 | /* |
331 | * Primecells are part of the Advanced Microcontroller Bus Architecture, | |
332 | * so we call the bus "amba". | |
07397df2 NG |
333 | * DMA configuration for platform and AMBA bus is same. So here we reuse |
334 | * platform's DMA config routine. | |
1da177e4 | 335 | */ |
394d5aef | 336 | struct bus_type amba_bustype = { |
1da177e4 | 337 | .name = "amba", |
966449a3 | 338 | .dev_groups = amba_dev_groups, |
1da177e4 | 339 | .match = amba_match, |
6d20b035 | 340 | .uevent = amba_uevent, |
f170b59f UKK |
341 | .probe = amba_probe, |
342 | .remove = amba_remove, | |
343 | .shutdown = amba_shutdown, | |
07397df2 | 344 | .dma_configure = platform_dma_configure, |
26825cfd | 345 | .pm = &amba_pm, |
1da177e4 | 346 | }; |
e9ac68c3 | 347 | EXPORT_SYMBOL_GPL(amba_bustype); |
1da177e4 LT |
348 | |
349 | static int __init amba_init(void) | |
350 | { | |
351 | return bus_register(&amba_bustype); | |
352 | } | |
353 | ||
354 | postcore_initcall(amba_init); | |
355 | ||
1da177e4 LT |
356 | /** |
357 | * amba_driver_register - register an AMBA device driver | |
358 | * @drv: amba device driver structure | |
359 | * | |
360 | * Register an AMBA device driver with the Linux device model | |
361 | * core. If devices pre-exist, the drivers probe function will | |
362 | * be called. | |
363 | */ | |
364 | int amba_driver_register(struct amba_driver *drv) | |
365 | { | |
de5d7adb UKK |
366 | if (!drv->probe) |
367 | return -EINVAL; | |
1da177e4 | 368 | |
de5d7adb | 369 | drv->drv.bus = &amba_bustype; |
1da177e4 LT |
370 | |
371 | return driver_register(&drv->drv); | |
372 | } | |
a2e7ae86 | 373 | EXPORT_SYMBOL(amba_driver_register); |
1da177e4 LT |
374 | |
375 | /** | |
376 | * amba_driver_unregister - remove an AMBA device driver | |
377 | * @drv: AMBA device driver structure to remove | |
378 | * | |
379 | * Unregister an AMBA device driver from the Linux device | |
380 | * model. The device model will call the drivers remove function | |
381 | * for each device the device driver is currently handling. | |
382 | */ | |
383 | void amba_driver_unregister(struct amba_driver *drv) | |
384 | { | |
385 | driver_unregister(&drv->drv); | |
386 | } | |
a2e7ae86 | 387 | EXPORT_SYMBOL(amba_driver_unregister); |
1da177e4 LT |
388 | |
389 | static void amba_device_release(struct device *dev) | |
390 | { | |
391 | struct amba_device *d = to_amba_device(dev); | |
392 | ||
393 | if (d->res.parent) | |
394 | release_resource(&d->res); | |
395 | kfree(d); | |
396 | } | |
397 | ||
a41980f2 | 398 | static int amba_device_try_add(struct amba_device *dev, struct resource *parent) |
1da177e4 | 399 | { |
8afe0b96 | 400 | u32 size; |
1da177e4 LT |
401 | void __iomem *tmp; |
402 | int i, ret; | |
854f695c | 403 | |
1da177e4 | 404 | ret = request_resource(parent, &dev->res); |
96b13f5c RK |
405 | if (ret) |
406 | goto err_out; | |
407 | ||
97ceed1f LW |
408 | /* Hard-coded primecell ID instead of plug-n-play */ |
409 | if (dev->periphid != 0) | |
410 | goto skip_probe; | |
411 | ||
8afe0b96 LC |
412 | /* |
413 | * Dynamically calculate the size of the resource | |
414 | * and use this for iomap | |
415 | */ | |
416 | size = resource_size(&dev->res); | |
417 | tmp = ioremap(dev->res.start, size); | |
96b13f5c RK |
418 | if (!tmp) { |
419 | ret = -ENOMEM; | |
420 | goto err_release; | |
1da177e4 | 421 | } |
96b13f5c | 422 | |
a41980f2 | 423 | ret = dev_pm_domain_attach(&dev->dev, true); |
d21bc89e | 424 | if (ret) { |
a41980f2 MS |
425 | iounmap(tmp); |
426 | goto err_release; | |
427 | } | |
428 | ||
7cfe2494 RK |
429 | ret = amba_get_enable_pclk(dev); |
430 | if (ret == 0) { | |
431 | u32 pid, cid; | |
79bdcb20 DN |
432 | struct reset_control *rstc; |
433 | ||
434 | /* | |
435 | * Find reset control(s) of the amba bus and de-assert them. | |
436 | */ | |
437 | rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node); | |
438 | if (IS_ERR(rstc)) { | |
e963408e RK |
439 | ret = PTR_ERR(rstc); |
440 | if (ret != -EPROBE_DEFER) | |
441 | dev_err(&dev->dev, "can't get reset: %d\n", | |
442 | ret); | |
443 | goto err_reset; | |
79bdcb20 DN |
444 | } |
445 | reset_control_deassert(rstc); | |
446 | reset_control_put(rstc); | |
96b13f5c | 447 | |
7cfe2494 RK |
448 | /* |
449 | * Read pid and cid based on size of resource | |
450 | * they are located at end of region | |
451 | */ | |
452 | for (pid = 0, i = 0; i < 4; i++) | |
453 | pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) << | |
454 | (i * 8); | |
455 | for (cid = 0, i = 0; i < 4; i++) | |
456 | cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) << | |
457 | (i * 8); | |
96b13f5c | 458 | |
4a2910fa ML |
459 | if (cid == CORESIGHT_CID) { |
460 | /* set the base to the start of the last 4k block */ | |
461 | void __iomem *csbase = tmp + size - 4096; | |
462 | ||
463 | dev->uci.devarch = | |
464 | readl(csbase + UCI_REG_DEVARCH_OFFSET); | |
465 | dev->uci.devtype = | |
466 | readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff; | |
467 | } | |
468 | ||
7cfe2494 | 469 | amba_put_disable_pclk(dev); |
96b13f5c | 470 | |
4a2910fa | 471 | if (cid == AMBA_CID || cid == CORESIGHT_CID) { |
7cfe2494 | 472 | dev->periphid = pid; |
4a2910fa ML |
473 | dev->cid = cid; |
474 | } | |
7cfe2494 RK |
475 | |
476 | if (!dev->periphid) | |
477 | ret = -ENODEV; | |
96b13f5c RK |
478 | } |
479 | ||
7cfe2494 | 480 | iounmap(tmp); |
a41980f2 | 481 | dev_pm_domain_detach(&dev->dev, true); |
7cfe2494 RK |
482 | |
483 | if (ret) | |
484 | goto err_release; | |
485 | ||
97ceed1f | 486 | skip_probe: |
557dca5f | 487 | ret = device_add(&dev->dev); |
96b13f5c | 488 | err_release: |
33c6a549 WK |
489 | if (ret) |
490 | release_resource(&dev->res); | |
96b13f5c | 491 | err_out: |
1da177e4 | 492 | return ret; |
e963408e RK |
493 | |
494 | err_reset: | |
495 | amba_put_disable_pclk(dev); | |
496 | iounmap(tmp); | |
497 | dev_pm_domain_detach(&dev->dev, true); | |
498 | goto err_release; | |
1da177e4 | 499 | } |
a41980f2 MS |
500 | |
501 | /* | |
502 | * Registration of AMBA device require reading its pid and cid registers. | |
503 | * To do this, the device must be turned on (if it is a part of power domain) | |
504 | * and have clocks enabled. However in some cases those resources might not be | |
505 | * yet available. Returning EPROBE_DEFER is not a solution in such case, | |
506 | * because callers don't handle this special error code. Instead such devices | |
507 | * are added to the special list and their registration is retried from | |
508 | * periodic worker, until all resources are available and registration succeeds. | |
509 | */ | |
510 | struct deferred_device { | |
511 | struct amba_device *dev; | |
512 | struct resource *parent; | |
513 | struct list_head node; | |
514 | }; | |
515 | ||
516 | static LIST_HEAD(deferred_devices); | |
517 | static DEFINE_MUTEX(deferred_devices_lock); | |
518 | ||
519 | static void amba_deferred_retry_func(struct work_struct *dummy); | |
520 | static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func); | |
521 | ||
522 | #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000)) | |
523 | ||
039599c9 | 524 | static int amba_deferred_retry(void) |
a41980f2 MS |
525 | { |
526 | struct deferred_device *ddev, *tmp; | |
527 | ||
528 | mutex_lock(&deferred_devices_lock); | |
529 | ||
530 | list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) { | |
531 | int ret = amba_device_try_add(ddev->dev, ddev->parent); | |
532 | ||
533 | if (ret == -EPROBE_DEFER) | |
534 | continue; | |
535 | ||
536 | list_del_init(&ddev->node); | |
537 | kfree(ddev); | |
538 | } | |
539 | ||
039599c9 RH |
540 | mutex_unlock(&deferred_devices_lock); |
541 | ||
542 | return 0; | |
543 | } | |
544 | late_initcall(amba_deferred_retry); | |
545 | ||
546 | static void amba_deferred_retry_func(struct work_struct *dummy) | |
547 | { | |
548 | amba_deferred_retry(); | |
549 | ||
a41980f2 MS |
550 | if (!list_empty(&deferred_devices)) |
551 | schedule_delayed_work(&deferred_retry_work, | |
552 | DEFERRED_DEVICE_TIMEOUT); | |
a41980f2 MS |
553 | } |
554 | ||
555 | /** | |
556 | * amba_device_add - add a previously allocated AMBA device structure | |
557 | * @dev: AMBA device allocated by amba_device_alloc | |
558 | * @parent: resource parent for this devices resources | |
559 | * | |
560 | * Claim the resource, and read the device cell ID if not already | |
561 | * initialized. Register the AMBA device with the Linux device | |
562 | * manager. | |
563 | */ | |
564 | int amba_device_add(struct amba_device *dev, struct resource *parent) | |
565 | { | |
566 | int ret = amba_device_try_add(dev, parent); | |
567 | ||
568 | if (ret == -EPROBE_DEFER) { | |
569 | struct deferred_device *ddev; | |
570 | ||
571 | ddev = kmalloc(sizeof(*ddev), GFP_KERNEL); | |
572 | if (!ddev) | |
573 | return -ENOMEM; | |
574 | ||
575 | ddev->dev = dev; | |
576 | ddev->parent = parent; | |
577 | ret = 0; | |
578 | ||
579 | mutex_lock(&deferred_devices_lock); | |
580 | ||
581 | if (list_empty(&deferred_devices)) | |
582 | schedule_delayed_work(&deferred_retry_work, | |
583 | DEFERRED_DEVICE_TIMEOUT); | |
584 | list_add_tail(&ddev->node, &deferred_devices); | |
585 | ||
586 | mutex_unlock(&deferred_devices_lock); | |
587 | } | |
588 | return ret; | |
589 | } | |
d5dc9271 RK |
590 | EXPORT_SYMBOL_GPL(amba_device_add); |
591 | ||
592 | static void amba_device_initialize(struct amba_device *dev, const char *name) | |
593 | { | |
594 | device_initialize(&dev->dev); | |
595 | if (name) | |
596 | dev_set_name(&dev->dev, "%s", name); | |
597 | dev->dev.release = amba_device_release; | |
598 | dev->dev.bus = &amba_bustype; | |
446b2a93 | 599 | dev->dev.dma_mask = &dev->dev.coherent_dma_mask; |
f4584884 | 600 | dev->dev.dma_parms = &dev->dma_parms; |
d5dc9271 RK |
601 | dev->res.name = dev_name(&dev->dev); |
602 | } | |
603 | ||
604 | /** | |
605 | * amba_device_alloc - allocate an AMBA device | |
606 | * @name: sysfs name of the AMBA device | |
607 | * @base: base of AMBA device | |
608 | * @size: size of AMBA device | |
609 | * | |
610 | * Allocate and initialize an AMBA device structure. Returns %NULL | |
611 | * on failure. | |
612 | */ | |
613 | struct amba_device *amba_device_alloc(const char *name, resource_size_t base, | |
614 | size_t size) | |
615 | { | |
616 | struct amba_device *dev; | |
617 | ||
618 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
619 | if (dev) { | |
620 | amba_device_initialize(dev, name); | |
621 | dev->res.start = base; | |
622 | dev->res.end = base + size - 1; | |
623 | dev->res.flags = IORESOURCE_MEM; | |
624 | } | |
625 | ||
626 | return dev; | |
627 | } | |
628 | EXPORT_SYMBOL_GPL(amba_device_alloc); | |
629 | ||
630 | /** | |
631 | * amba_device_register - register an AMBA device | |
632 | * @dev: AMBA device to register | |
633 | * @parent: parent memory resource | |
634 | * | |
635 | * Setup the AMBA device, reading the cell ID if present. | |
636 | * Claim the resource, and register the AMBA device with | |
637 | * the Linux device manager. | |
638 | */ | |
639 | int amba_device_register(struct amba_device *dev, struct resource *parent) | |
640 | { | |
641 | amba_device_initialize(dev, dev->dev.init_name); | |
642 | dev->dev.init_name = NULL; | |
643 | ||
d5dc9271 RK |
644 | return amba_device_add(dev, parent); |
645 | } | |
a2e7ae86 | 646 | EXPORT_SYMBOL(amba_device_register); |
d5dc9271 RK |
647 | |
648 | /** | |
649 | * amba_device_put - put an AMBA device | |
650 | * @dev: AMBA device to put | |
651 | */ | |
652 | void amba_device_put(struct amba_device *dev) | |
653 | { | |
654 | put_device(&dev->dev); | |
655 | } | |
656 | EXPORT_SYMBOL_GPL(amba_device_put); | |
1da177e4 LT |
657 | |
658 | /** | |
659 | * amba_device_unregister - unregister an AMBA device | |
660 | * @dev: AMBA device to remove | |
661 | * | |
662 | * Remove the specified AMBA device from the Linux device | |
663 | * manager. All files associated with this object will be | |
664 | * destroyed, and device drivers notified that the device has | |
665 | * been removed. The AMBA device's resources including | |
666 | * the amba_device structure will be freed once all | |
667 | * references to it have been dropped. | |
668 | */ | |
669 | void amba_device_unregister(struct amba_device *dev) | |
670 | { | |
671 | device_unregister(&dev->dev); | |
672 | } | |
a2e7ae86 | 673 | EXPORT_SYMBOL(amba_device_unregister); |
1da177e4 | 674 | |
1da177e4 LT |
675 | /** |
676 | * amba_request_regions - request all mem regions associated with device | |
677 | * @dev: amba_device structure for device | |
678 | * @name: name, or NULL to use driver name | |
679 | */ | |
680 | int amba_request_regions(struct amba_device *dev, const char *name) | |
681 | { | |
682 | int ret = 0; | |
8afe0b96 | 683 | u32 size; |
1da177e4 LT |
684 | |
685 | if (!name) | |
686 | name = dev->dev.driver->name; | |
687 | ||
8afe0b96 LC |
688 | size = resource_size(&dev->res); |
689 | ||
690 | if (!request_mem_region(dev->res.start, size, name)) | |
1da177e4 LT |
691 | ret = -EBUSY; |
692 | ||
693 | return ret; | |
694 | } | |
a2e7ae86 | 695 | EXPORT_SYMBOL(amba_request_regions); |
1da177e4 LT |
696 | |
697 | /** | |
25985edc | 698 | * amba_release_regions - release mem regions associated with device |
1da177e4 LT |
699 | * @dev: amba_device structure for device |
700 | * | |
701 | * Release regions claimed by a successful call to amba_request_regions. | |
702 | */ | |
703 | void amba_release_regions(struct amba_device *dev) | |
704 | { | |
8afe0b96 LC |
705 | u32 size; |
706 | ||
707 | size = resource_size(&dev->res); | |
708 | release_mem_region(dev->res.start, size); | |
1da177e4 | 709 | } |
1da177e4 | 710 | EXPORT_SYMBOL(amba_release_regions); |