xref: /openbmc/linux/drivers/amba/bus.c (revision c4f7ac64)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/arch/arm/common/amba.c
4  *
5  *  Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
6  */
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/device.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/io.h>
13 #include <linux/pm.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/pm_domain.h>
16 #include <linux/amba/bus.h>
17 #include <linux/sizes.h>
18 #include <linux/limits.h>
19 #include <linux/clk/clk-conf.h>
20 #include <linux/platform_device.h>
21 #include <linux/reset.h>
22 
23 #include <asm/irq.h>
24 
25 #define to_amba_driver(d)	container_of(d, struct amba_driver, drv)
26 
27 /* called on periphid match and class 0x9 coresight device. */
28 static int
29 amba_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 
46 static const struct amba_id *
47 amba_lookup(const struct amba_id *table, struct amba_device *dev)
48 {
49 	while (table->mask) {
50 		if (((dev->periphid & table->mask) == table->id) &&
51 			((dev->cid != CORESIGHT_CID) ||
52 			 (amba_cs_uci_id_match(table, dev))))
53 			return table;
54 		table++;
55 	}
56 	return NULL;
57 }
58 
59 static int amba_get_enable_pclk(struct amba_device *pcdev)
60 {
61 	int ret;
62 
63 	pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
64 	if (IS_ERR(pcdev->pclk))
65 		return PTR_ERR(pcdev->pclk);
66 
67 	ret = clk_prepare_enable(pcdev->pclk);
68 	if (ret)
69 		clk_put(pcdev->pclk);
70 
71 	return ret;
72 }
73 
74 static void amba_put_disable_pclk(struct amba_device *pcdev)
75 {
76 	clk_disable_unprepare(pcdev->pclk);
77 	clk_put(pcdev->pclk);
78 }
79 
80 
81 static 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);
85 	ssize_t len;
86 
87 	device_lock(_dev);
88 	len = sprintf(buf, "%s\n", dev->driver_override);
89 	device_unlock(_dev);
90 	return len;
91 }
92 
93 static 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);
98 	char *driver_override, *old, *cp;
99 
100 	/* We need to keep extra room for a newline */
101 	if (count >= (PAGE_SIZE - 1))
102 		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 
112 	device_lock(_dev);
113 	old = dev->driver_override;
114 	if (strlen(driver_override)) {
115 		dev->driver_override = driver_override;
116 	} else {
117 		kfree(driver_override);
118 		dev->driver_override = NULL;
119 	}
120 	device_unlock(_dev);
121 
122 	kfree(old);
123 
124 	return count;
125 }
126 static DEVICE_ATTR_RW(driver_override);
127 
128 #define amba_attr_func(name,fmt,arg...)					\
129 static 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);					\
134 }									\
135 static DEVICE_ATTR_RO(name)
136 
137 amba_attr_func(id, "%08x\n", dev->periphid);
138 amba_attr_func(irq0, "%u\n", dev->irq[0]);
139 amba_attr_func(irq1, "%u\n", dev->irq[1]);
140 amba_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 
144 static struct attribute *amba_dev_attrs[] = {
145 	&dev_attr_id.attr,
146 	&dev_attr_resource.attr,
147 	&dev_attr_driver_override.attr,
148 	NULL,
149 };
150 ATTRIBUTE_GROUPS(amba_dev);
151 
152 static 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 
164 static 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 
177 /*
178  * These are the device model conversion veneers; they convert the
179  * device model structures to our more specific structures.
180  */
181 static 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);
185 	const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
186 	int ret;
187 
188 	do {
189 		ret = of_clk_set_defaults(dev->of_node, false);
190 		if (ret < 0)
191 			break;
192 
193 		ret = dev_pm_domain_attach(dev, true);
194 		if (ret)
195 			break;
196 
197 		ret = amba_get_enable_pclk(pcdev);
198 		if (ret) {
199 			dev_pm_domain_detach(dev, true);
200 			break;
201 		}
202 
203 		pm_runtime_get_noresume(dev);
204 		pm_runtime_set_active(dev);
205 		pm_runtime_enable(dev);
206 
207 		ret = pcdrv->probe(pcdev, id);
208 		if (ret == 0)
209 			break;
210 
211 		pm_runtime_disable(dev);
212 		pm_runtime_set_suspended(dev);
213 		pm_runtime_put_noidle(dev);
214 
215 		amba_put_disable_pclk(pcdev);
216 		dev_pm_domain_detach(dev, true);
217 	} while (0);
218 
219 	return ret;
220 }
221 
222 static int amba_remove(struct device *dev)
223 {
224 	struct amba_device *pcdev = to_amba_device(dev);
225 	struct amba_driver *drv = to_amba_driver(dev->driver);
226 
227 	pm_runtime_get_sync(dev);
228 	if (drv->remove)
229 		drv->remove(pcdev);
230 	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);
236 
237 	amba_put_disable_pclk(pcdev);
238 	dev_pm_domain_detach(dev, true);
239 
240 	return 0;
241 }
242 
243 static void amba_shutdown(struct device *dev)
244 {
245 	struct amba_driver *drv;
246 
247 	if (!dev->driver)
248 		return;
249 
250 	drv = to_amba_driver(dev->driver);
251 	if (drv->shutdown)
252 		drv->shutdown(to_amba_device(dev));
253 }
254 
255 #ifdef CONFIG_PM
256 /*
257  * Hooks to provide runtime PM of the pclk (bus clock).  It is safe to
258  * enable/disable the bus clock at runtime PM suspend/resume as this
259  * does not result in loss of context.
260  */
261 static int amba_pm_runtime_suspend(struct device *dev)
262 {
263 	struct amba_device *pcdev = to_amba_device(dev);
264 	int ret = pm_generic_runtime_suspend(dev);
265 
266 	if (ret == 0 && dev->driver) {
267 		if (pm_runtime_is_irq_safe(dev))
268 			clk_disable(pcdev->pclk);
269 		else
270 			clk_disable_unprepare(pcdev->pclk);
271 	}
272 
273 	return ret;
274 }
275 
276 static int amba_pm_runtime_resume(struct device *dev)
277 {
278 	struct amba_device *pcdev = to_amba_device(dev);
279 	int ret;
280 
281 	if (dev->driver) {
282 		if (pm_runtime_is_irq_safe(dev))
283 			ret = clk_enable(pcdev->pclk);
284 		else
285 			ret = clk_prepare_enable(pcdev->pclk);
286 		/* Failure is probably fatal to the system, but... */
287 		if (ret)
288 			return ret;
289 	}
290 
291 	return pm_generic_runtime_resume(dev);
292 }
293 #endif /* CONFIG_PM */
294 
295 static const struct dev_pm_ops amba_pm = {
296 	.suspend	= pm_generic_suspend,
297 	.resume		= pm_generic_resume,
298 	.freeze		= pm_generic_freeze,
299 	.thaw		= pm_generic_thaw,
300 	.poweroff	= pm_generic_poweroff,
301 	.restore	= pm_generic_restore,
302 	SET_RUNTIME_PM_OPS(
303 		amba_pm_runtime_suspend,
304 		amba_pm_runtime_resume,
305 		NULL
306 	)
307 };
308 
309 /*
310  * Primecells are part of the Advanced Microcontroller Bus Architecture,
311  * so we call the bus "amba".
312  * DMA configuration for platform and AMBA bus is same. So here we reuse
313  * platform's DMA config routine.
314  */
315 struct bus_type amba_bustype = {
316 	.name		= "amba",
317 	.dev_groups	= amba_dev_groups,
318 	.match		= amba_match,
319 	.uevent		= amba_uevent,
320 	.probe		= amba_probe,
321 	.remove		= amba_remove,
322 	.shutdown	= amba_shutdown,
323 	.dma_configure	= platform_dma_configure,
324 	.pm		= &amba_pm,
325 };
326 EXPORT_SYMBOL_GPL(amba_bustype);
327 
328 static int __init amba_init(void)
329 {
330 	return bus_register(&amba_bustype);
331 }
332 
333 postcore_initcall(amba_init);
334 
335 /**
336  *	amba_driver_register - register an AMBA device driver
337  *	@drv: amba device driver structure
338  *
339  *	Register an AMBA device driver with the Linux device model
340  *	core.  If devices pre-exist, the drivers probe function will
341  *	be called.
342  */
343 int amba_driver_register(struct amba_driver *drv)
344 {
345 	if (!drv->probe)
346 		return -EINVAL;
347 
348 	drv->drv.bus = &amba_bustype;
349 
350 	return driver_register(&drv->drv);
351 }
352 
353 /**
354  *	amba_driver_unregister - remove an AMBA device driver
355  *	@drv: AMBA device driver structure to remove
356  *
357  *	Unregister an AMBA device driver from the Linux device
358  *	model.  The device model will call the drivers remove function
359  *	for each device the device driver is currently handling.
360  */
361 void amba_driver_unregister(struct amba_driver *drv)
362 {
363 	driver_unregister(&drv->drv);
364 }
365 
366 
367 static void amba_device_release(struct device *dev)
368 {
369 	struct amba_device *d = to_amba_device(dev);
370 
371 	if (d->res.parent)
372 		release_resource(&d->res);
373 	kfree(d);
374 }
375 
376 static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
377 {
378 	u32 size;
379 	void __iomem *tmp;
380 	int i, ret;
381 
382 	WARN_ON(dev->irq[0] == (unsigned int)-1);
383 	WARN_ON(dev->irq[1] == (unsigned int)-1);
384 
385 	ret = request_resource(parent, &dev->res);
386 	if (ret)
387 		goto err_out;
388 
389 	/* Hard-coded primecell ID instead of plug-n-play */
390 	if (dev->periphid != 0)
391 		goto skip_probe;
392 
393 	/*
394 	 * Dynamically calculate the size of the resource
395 	 * and use this for iomap
396 	 */
397 	size = resource_size(&dev->res);
398 	tmp = ioremap(dev->res.start, size);
399 	if (!tmp) {
400 		ret = -ENOMEM;
401 		goto err_release;
402 	}
403 
404 	ret = dev_pm_domain_attach(&dev->dev, true);
405 	if (ret) {
406 		iounmap(tmp);
407 		goto err_release;
408 	}
409 
410 	ret = amba_get_enable_pclk(dev);
411 	if (ret == 0) {
412 		u32 pid, cid;
413 		struct reset_control *rstc;
414 
415 		/*
416 		 * Find reset control(s) of the amba bus and de-assert them.
417 		 */
418 		rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
419 		if (IS_ERR(rstc)) {
420 			ret = PTR_ERR(rstc);
421 			if (ret != -EPROBE_DEFER)
422 				dev_err(&dev->dev, "can't get reset: %d\n",
423 					ret);
424 			goto err_reset;
425 		}
426 		reset_control_deassert(rstc);
427 		reset_control_put(rstc);
428 
429 		/*
430 		 * Read pid and cid based on size of resource
431 		 * they are located at end of region
432 		 */
433 		for (pid = 0, i = 0; i < 4; i++)
434 			pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
435 				(i * 8);
436 		for (cid = 0, i = 0; i < 4; i++)
437 			cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
438 				(i * 8);
439 
440 		if (cid == CORESIGHT_CID) {
441 			/* set the base to the start of the last 4k block */
442 			void __iomem *csbase = tmp + size - 4096;
443 
444 			dev->uci.devarch =
445 				readl(csbase + UCI_REG_DEVARCH_OFFSET);
446 			dev->uci.devtype =
447 				readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
448 		}
449 
450 		amba_put_disable_pclk(dev);
451 
452 		if (cid == AMBA_CID || cid == CORESIGHT_CID) {
453 			dev->periphid = pid;
454 			dev->cid = cid;
455 		}
456 
457 		if (!dev->periphid)
458 			ret = -ENODEV;
459 	}
460 
461 	iounmap(tmp);
462 	dev_pm_domain_detach(&dev->dev, true);
463 
464 	if (ret)
465 		goto err_release;
466 
467  skip_probe:
468 	ret = device_add(&dev->dev);
469 	if (ret)
470 		goto err_release;
471 
472 	if (dev->irq[0])
473 		ret = device_create_file(&dev->dev, &dev_attr_irq0);
474 	if (ret == 0 && dev->irq[1])
475 		ret = device_create_file(&dev->dev, &dev_attr_irq1);
476 	if (ret == 0)
477 		return ret;
478 
479 	device_unregister(&dev->dev);
480 
481  err_release:
482 	release_resource(&dev->res);
483  err_out:
484 	return ret;
485 
486  err_reset:
487 	amba_put_disable_pclk(dev);
488 	iounmap(tmp);
489 	dev_pm_domain_detach(&dev->dev, true);
490 	goto err_release;
491 }
492 
493 /*
494  * Registration of AMBA device require reading its pid and cid registers.
495  * To do this, the device must be turned on (if it is a part of power domain)
496  * and have clocks enabled. However in some cases those resources might not be
497  * yet available. Returning EPROBE_DEFER is not a solution in such case,
498  * because callers don't handle this special error code. Instead such devices
499  * are added to the special list and their registration is retried from
500  * periodic worker, until all resources are available and registration succeeds.
501  */
502 struct deferred_device {
503 	struct amba_device *dev;
504 	struct resource *parent;
505 	struct list_head node;
506 };
507 
508 static LIST_HEAD(deferred_devices);
509 static DEFINE_MUTEX(deferred_devices_lock);
510 
511 static void amba_deferred_retry_func(struct work_struct *dummy);
512 static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
513 
514 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
515 
516 static int amba_deferred_retry(void)
517 {
518 	struct deferred_device *ddev, *tmp;
519 
520 	mutex_lock(&deferred_devices_lock);
521 
522 	list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
523 		int ret = amba_device_try_add(ddev->dev, ddev->parent);
524 
525 		if (ret == -EPROBE_DEFER)
526 			continue;
527 
528 		list_del_init(&ddev->node);
529 		kfree(ddev);
530 	}
531 
532 	mutex_unlock(&deferred_devices_lock);
533 
534 	return 0;
535 }
536 late_initcall(amba_deferred_retry);
537 
538 static void amba_deferred_retry_func(struct work_struct *dummy)
539 {
540 	amba_deferred_retry();
541 
542 	if (!list_empty(&deferred_devices))
543 		schedule_delayed_work(&deferred_retry_work,
544 				      DEFERRED_DEVICE_TIMEOUT);
545 }
546 
547 /**
548  *	amba_device_add - add a previously allocated AMBA device structure
549  *	@dev: AMBA device allocated by amba_device_alloc
550  *	@parent: resource parent for this devices resources
551  *
552  *	Claim the resource, and read the device cell ID if not already
553  *	initialized.  Register the AMBA device with the Linux device
554  *	manager.
555  */
556 int amba_device_add(struct amba_device *dev, struct resource *parent)
557 {
558 	int ret = amba_device_try_add(dev, parent);
559 
560 	if (ret == -EPROBE_DEFER) {
561 		struct deferred_device *ddev;
562 
563 		ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
564 		if (!ddev)
565 			return -ENOMEM;
566 
567 		ddev->dev = dev;
568 		ddev->parent = parent;
569 		ret = 0;
570 
571 		mutex_lock(&deferred_devices_lock);
572 
573 		if (list_empty(&deferred_devices))
574 			schedule_delayed_work(&deferred_retry_work,
575 					      DEFERRED_DEVICE_TIMEOUT);
576 		list_add_tail(&ddev->node, &deferred_devices);
577 
578 		mutex_unlock(&deferred_devices_lock);
579 	}
580 	return ret;
581 }
582 EXPORT_SYMBOL_GPL(amba_device_add);
583 
584 static struct amba_device *
585 amba_aphb_device_add(struct device *parent, const char *name,
586 		     resource_size_t base, size_t size, int irq1, int irq2,
587 		     void *pdata, unsigned int periphid, u64 dma_mask,
588 		     struct resource *resbase)
589 {
590 	struct amba_device *dev;
591 	int ret;
592 
593 	dev = amba_device_alloc(name, base, size);
594 	if (!dev)
595 		return ERR_PTR(-ENOMEM);
596 
597 	dev->dev.coherent_dma_mask = dma_mask;
598 	dev->irq[0] = irq1;
599 	dev->irq[1] = irq2;
600 	dev->periphid = periphid;
601 	dev->dev.platform_data = pdata;
602 	dev->dev.parent = parent;
603 
604 	ret = amba_device_add(dev, resbase);
605 	if (ret) {
606 		amba_device_put(dev);
607 		return ERR_PTR(ret);
608 	}
609 
610 	return dev;
611 }
612 
613 struct amba_device *
614 amba_apb_device_add(struct device *parent, const char *name,
615 		    resource_size_t base, size_t size, int irq1, int irq2,
616 		    void *pdata, unsigned int periphid)
617 {
618 	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
619 				    periphid, 0, &iomem_resource);
620 }
621 EXPORT_SYMBOL_GPL(amba_apb_device_add);
622 
623 struct amba_device *
624 amba_ahb_device_add(struct device *parent, const char *name,
625 		    resource_size_t base, size_t size, int irq1, int irq2,
626 		    void *pdata, unsigned int periphid)
627 {
628 	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
629 				    periphid, ~0ULL, &iomem_resource);
630 }
631 EXPORT_SYMBOL_GPL(amba_ahb_device_add);
632 
633 struct amba_device *
634 amba_apb_device_add_res(struct device *parent, const char *name,
635 			resource_size_t base, size_t size, int irq1,
636 			int irq2, void *pdata, unsigned int periphid,
637 			struct resource *resbase)
638 {
639 	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
640 				    periphid, 0, resbase);
641 }
642 EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
643 
644 struct amba_device *
645 amba_ahb_device_add_res(struct device *parent, const char *name,
646 			resource_size_t base, size_t size, int irq1,
647 			int irq2, void *pdata, unsigned int periphid,
648 			struct resource *resbase)
649 {
650 	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
651 				    periphid, ~0ULL, resbase);
652 }
653 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
654 
655 
656 static void amba_device_initialize(struct amba_device *dev, const char *name)
657 {
658 	device_initialize(&dev->dev);
659 	if (name)
660 		dev_set_name(&dev->dev, "%s", name);
661 	dev->dev.release = amba_device_release;
662 	dev->dev.bus = &amba_bustype;
663 	dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
664 	dev->dev.dma_parms = &dev->dma_parms;
665 	dev->res.name = dev_name(&dev->dev);
666 }
667 
668 /**
669  *	amba_device_alloc - allocate an AMBA device
670  *	@name: sysfs name of the AMBA device
671  *	@base: base of AMBA device
672  *	@size: size of AMBA device
673  *
674  *	Allocate and initialize an AMBA device structure.  Returns %NULL
675  *	on failure.
676  */
677 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
678 	size_t size)
679 {
680 	struct amba_device *dev;
681 
682 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
683 	if (dev) {
684 		amba_device_initialize(dev, name);
685 		dev->res.start = base;
686 		dev->res.end = base + size - 1;
687 		dev->res.flags = IORESOURCE_MEM;
688 	}
689 
690 	return dev;
691 }
692 EXPORT_SYMBOL_GPL(amba_device_alloc);
693 
694 /**
695  *	amba_device_register - register an AMBA device
696  *	@dev: AMBA device to register
697  *	@parent: parent memory resource
698  *
699  *	Setup the AMBA device, reading the cell ID if present.
700  *	Claim the resource, and register the AMBA device with
701  *	the Linux device manager.
702  */
703 int amba_device_register(struct amba_device *dev, struct resource *parent)
704 {
705 	amba_device_initialize(dev, dev->dev.init_name);
706 	dev->dev.init_name = NULL;
707 
708 	return amba_device_add(dev, parent);
709 }
710 
711 /**
712  *	amba_device_put - put an AMBA device
713  *	@dev: AMBA device to put
714  */
715 void amba_device_put(struct amba_device *dev)
716 {
717 	put_device(&dev->dev);
718 }
719 EXPORT_SYMBOL_GPL(amba_device_put);
720 
721 /**
722  *	amba_device_unregister - unregister an AMBA device
723  *	@dev: AMBA device to remove
724  *
725  *	Remove the specified AMBA device from the Linux device
726  *	manager.  All files associated with this object will be
727  *	destroyed, and device drivers notified that the device has
728  *	been removed.  The AMBA device's resources including
729  *	the amba_device structure will be freed once all
730  *	references to it have been dropped.
731  */
732 void amba_device_unregister(struct amba_device *dev)
733 {
734 	device_unregister(&dev->dev);
735 }
736 
737 
738 struct find_data {
739 	struct amba_device *dev;
740 	struct device *parent;
741 	const char *busid;
742 	unsigned int id;
743 	unsigned int mask;
744 };
745 
746 static int amba_find_match(struct device *dev, void *data)
747 {
748 	struct find_data *d = data;
749 	struct amba_device *pcdev = to_amba_device(dev);
750 	int r;
751 
752 	r = (pcdev->periphid & d->mask) == d->id;
753 	if (d->parent)
754 		r &= d->parent == dev->parent;
755 	if (d->busid)
756 		r &= strcmp(dev_name(dev), d->busid) == 0;
757 
758 	if (r) {
759 		get_device(dev);
760 		d->dev = pcdev;
761 	}
762 
763 	return r;
764 }
765 
766 /**
767  *	amba_find_device - locate an AMBA device given a bus id
768  *	@busid: bus id for device (or NULL)
769  *	@parent: parent device (or NULL)
770  *	@id: peripheral ID (or 0)
771  *	@mask: peripheral ID mask (or 0)
772  *
773  *	Return the AMBA device corresponding to the supplied parameters.
774  *	If no device matches, returns NULL.
775  *
776  *	NOTE: When a valid device is found, its refcount is
777  *	incremented, and must be decremented before the returned
778  *	reference.
779  */
780 struct amba_device *
781 amba_find_device(const char *busid, struct device *parent, unsigned int id,
782 		 unsigned int mask)
783 {
784 	struct find_data data;
785 
786 	data.dev = NULL;
787 	data.parent = parent;
788 	data.busid = busid;
789 	data.id = id;
790 	data.mask = mask;
791 
792 	bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
793 
794 	return data.dev;
795 }
796 
797 /**
798  *	amba_request_regions - request all mem regions associated with device
799  *	@dev: amba_device structure for device
800  *	@name: name, or NULL to use driver name
801  */
802 int amba_request_regions(struct amba_device *dev, const char *name)
803 {
804 	int ret = 0;
805 	u32 size;
806 
807 	if (!name)
808 		name = dev->dev.driver->name;
809 
810 	size = resource_size(&dev->res);
811 
812 	if (!request_mem_region(dev->res.start, size, name))
813 		ret = -EBUSY;
814 
815 	return ret;
816 }
817 
818 /**
819  *	amba_release_regions - release mem regions associated with device
820  *	@dev: amba_device structure for device
821  *
822  *	Release regions claimed by a successful call to amba_request_regions.
823  */
824 void amba_release_regions(struct amba_device *dev)
825 {
826 	u32 size;
827 
828 	size = resource_size(&dev->res);
829 	release_mem_region(dev->res.start, size);
830 }
831 
832 EXPORT_SYMBOL(amba_driver_register);
833 EXPORT_SYMBOL(amba_driver_unregister);
834 EXPORT_SYMBOL(amba_device_register);
835 EXPORT_SYMBOL(amba_device_unregister);
836 EXPORT_SYMBOL(amba_find_device);
837 EXPORT_SYMBOL(amba_request_regions);
838 EXPORT_SYMBOL(amba_release_regions);
839