• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
amba_cs_uci_id_match(const struct amba_id * table,struct amba_device * dev)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 *
amba_lookup(const struct amba_id * table,struct amba_device * dev)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 
amba_get_enable_pclk(struct amba_device * pcdev)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 
amba_put_disable_pclk(struct amba_device * pcdev)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 
driver_override_show(struct device * _dev,struct device_attribute * attr,char * buf)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 
driver_override_store(struct device * _dev,struct device_attribute * attr,const char * buf,size_t count)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 
amba_match(struct device * dev,struct device_driver * drv)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 
amba_uevent(struct device * dev,struct kobj_uevent_env * env)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  */
amba_probe(struct device * dev)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 
amba_remove(struct device * dev)222 static void 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 
amba_shutdown(struct device * dev)241 static void amba_shutdown(struct device *dev)
242 {
243 	struct amba_driver *drv;
244 
245 	if (!dev->driver)
246 		return;
247 
248 	drv = to_amba_driver(dev->driver);
249 	if (drv->shutdown)
250 		drv->shutdown(to_amba_device(dev));
251 }
252 
253 #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  */
amba_pm_runtime_suspend(struct device * dev)259 static 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 
amba_pm_runtime_resume(struct device * dev)274 static 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 
293 static 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  */
313 struct 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 };
324 EXPORT_SYMBOL_GPL(amba_bustype);
325 
amba_init(void)326 static int __init amba_init(void)
327 {
328 	return bus_register(&amba_bustype);
329 }
330 
331 postcore_initcall(amba_init);
332 
333 /**
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  */
amba_driver_register(struct amba_driver * drv)341 int amba_driver_register(struct amba_driver *drv)
342 {
343 	if (!drv->probe)
344 		return -EINVAL;
345 
346 	drv->drv.bus = &amba_bustype;
347 
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  */
amba_driver_unregister(struct amba_driver * drv)359 void amba_driver_unregister(struct amba_driver *drv)
360 {
361 	driver_unregister(&drv->drv);
362 }
363 
364 
amba_device_release(struct device * dev)365 static void amba_device_release(struct device *dev)
366 {
367 	struct amba_device *d = to_amba_device(dev);
368 
369 	of_node_put(d->dev.of_node);
370 	if (d->res.parent)
371 		release_resource(&d->res);
372 	kfree(d);
373 }
374 
amba_device_try_add(struct amba_device * dev,struct resource * parent)375 static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
376 {
377 	u32 size;
378 	void __iomem *tmp;
379 	int i, ret;
380 
381 	ret = request_resource(parent, &dev->res);
382 	if (ret)
383 		goto err_out;
384 
385 	/* Hard-coded primecell ID instead of plug-n-play */
386 	if (dev->periphid != 0)
387 		goto skip_probe;
388 
389 	/*
390 	 * Dynamically calculate the size of the resource
391 	 * and use this for iomap
392 	 */
393 	size = resource_size(&dev->res);
394 	tmp = ioremap(dev->res.start, size);
395 	if (!tmp) {
396 		ret = -ENOMEM;
397 		goto err_release;
398 	}
399 
400 	ret = dev_pm_domain_attach(&dev->dev, true);
401 	if (ret) {
402 		iounmap(tmp);
403 		goto err_release;
404 	}
405 
406 	ret = amba_get_enable_pclk(dev);
407 	if (ret == 0) {
408 		u32 pid, cid;
409 		struct reset_control *rstc;
410 
411 		/*
412 		 * Find reset control(s) of the amba bus and de-assert them.
413 		 */
414 		rstc = of_reset_control_array_get_optional_shared(dev->dev.of_node);
415 		if (IS_ERR(rstc)) {
416 			ret = PTR_ERR(rstc);
417 			if (ret != -EPROBE_DEFER)
418 				dev_err(&dev->dev, "can't get reset: %d\n",
419 					ret);
420 			goto err_reset;
421 		}
422 		reset_control_deassert(rstc);
423 		reset_control_put(rstc);
424 
425 		/*
426 		 * Read pid and cid based on size of resource
427 		 * they are located at end of region
428 		 */
429 		for (pid = 0, i = 0; i < 4; i++)
430 			pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
431 				(i * 8);
432 		for (cid = 0, i = 0; i < 4; i++)
433 			cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
434 				(i * 8);
435 
436 		if (cid == CORESIGHT_CID) {
437 			/* set the base to the start of the last 4k block */
438 			void __iomem *csbase = tmp + size - 4096;
439 
440 			dev->uci.devarch =
441 				readl(csbase + UCI_REG_DEVARCH_OFFSET);
442 			dev->uci.devtype =
443 				readl(csbase + UCI_REG_DEVTYPE_OFFSET) & 0xff;
444 		}
445 
446 		amba_put_disable_pclk(dev);
447 
448 		if (cid == AMBA_CID || cid == CORESIGHT_CID) {
449 			dev->periphid = pid;
450 			dev->cid = cid;
451 		}
452 
453 		if (!dev->periphid)
454 			ret = -ENODEV;
455 	}
456 
457 	iounmap(tmp);
458 	dev_pm_domain_detach(&dev->dev, true);
459 
460 	if (ret)
461 		goto err_release;
462 
463  skip_probe:
464 	ret = device_add(&dev->dev);
465 	if (ret)
466 		goto err_release;
467 
468 	if (dev->irq[0])
469 		ret = device_create_file(&dev->dev, &dev_attr_irq0);
470 	if (ret == 0 && dev->irq[1])
471 		ret = device_create_file(&dev->dev, &dev_attr_irq1);
472 	if (ret == 0)
473 		return ret;
474 
475 	device_unregister(&dev->dev);
476 
477  err_release:
478 	release_resource(&dev->res);
479  err_out:
480 	return ret;
481 
482  err_reset:
483 	amba_put_disable_pclk(dev);
484 	iounmap(tmp);
485 	dev_pm_domain_detach(&dev->dev, true);
486 	goto err_release;
487 }
488 
489 /*
490  * Registration of AMBA device require reading its pid and cid registers.
491  * To do this, the device must be turned on (if it is a part of power domain)
492  * and have clocks enabled. However in some cases those resources might not be
493  * yet available. Returning EPROBE_DEFER is not a solution in such case,
494  * because callers don't handle this special error code. Instead such devices
495  * are added to the special list and their registration is retried from
496  * periodic worker, until all resources are available and registration succeeds.
497  */
498 struct deferred_device {
499 	struct amba_device *dev;
500 	struct resource *parent;
501 	struct list_head node;
502 };
503 
504 static LIST_HEAD(deferred_devices);
505 static DEFINE_MUTEX(deferred_devices_lock);
506 
507 static void amba_deferred_retry_func(struct work_struct *dummy);
508 static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
509 
510 #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
511 
amba_deferred_retry(void)512 static int amba_deferred_retry(void)
513 {
514 	struct deferred_device *ddev, *tmp;
515 
516 	mutex_lock(&deferred_devices_lock);
517 
518 	list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
519 		int ret = amba_device_try_add(ddev->dev, ddev->parent);
520 
521 		if (ret == -EPROBE_DEFER)
522 			continue;
523 
524 		list_del_init(&ddev->node);
525 		kfree(ddev);
526 	}
527 
528 	mutex_unlock(&deferred_devices_lock);
529 
530 	return 0;
531 }
532 late_initcall(amba_deferred_retry);
533 
amba_deferred_retry_func(struct work_struct * dummy)534 static void amba_deferred_retry_func(struct work_struct *dummy)
535 {
536 	amba_deferred_retry();
537 
538 	if (!list_empty(&deferred_devices))
539 		schedule_delayed_work(&deferred_retry_work,
540 				      DEFERRED_DEVICE_TIMEOUT);
541 }
542 
543 /**
544  *	amba_device_add - add a previously allocated AMBA device structure
545  *	@dev: AMBA device allocated by amba_device_alloc
546  *	@parent: resource parent for this devices resources
547  *
548  *	Claim the resource, and read the device cell ID if not already
549  *	initialized.  Register the AMBA device with the Linux device
550  *	manager.
551  */
amba_device_add(struct amba_device * dev,struct resource * parent)552 int amba_device_add(struct amba_device *dev, struct resource *parent)
553 {
554 	int ret = amba_device_try_add(dev, parent);
555 
556 	if (ret == -EPROBE_DEFER) {
557 		struct deferred_device *ddev;
558 
559 		ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
560 		if (!ddev)
561 			return -ENOMEM;
562 
563 		ddev->dev = dev;
564 		ddev->parent = parent;
565 		ret = 0;
566 
567 		mutex_lock(&deferred_devices_lock);
568 
569 		if (list_empty(&deferred_devices))
570 			schedule_delayed_work(&deferred_retry_work,
571 					      DEFERRED_DEVICE_TIMEOUT);
572 		list_add_tail(&ddev->node, &deferred_devices);
573 
574 		mutex_unlock(&deferred_devices_lock);
575 	}
576 	return ret;
577 }
578 EXPORT_SYMBOL_GPL(amba_device_add);
579 
580 static struct amba_device *
amba_aphb_device_add(struct device * parent,const char * name,resource_size_t base,size_t size,int irq1,int irq2,void * pdata,unsigned int periphid,u64 dma_mask,struct resource * resbase)581 amba_aphb_device_add(struct device *parent, const char *name,
582 		     resource_size_t base, size_t size, int irq1, int irq2,
583 		     void *pdata, unsigned int periphid, u64 dma_mask,
584 		     struct resource *resbase)
585 {
586 	struct amba_device *dev;
587 	int ret;
588 
589 	dev = amba_device_alloc(name, base, size);
590 	if (!dev)
591 		return ERR_PTR(-ENOMEM);
592 
593 	dev->dev.coherent_dma_mask = dma_mask;
594 	dev->irq[0] = irq1;
595 	dev->irq[1] = irq2;
596 	dev->periphid = periphid;
597 	dev->dev.platform_data = pdata;
598 	dev->dev.parent = parent;
599 
600 	ret = amba_device_add(dev, resbase);
601 	if (ret) {
602 		amba_device_put(dev);
603 		return ERR_PTR(ret);
604 	}
605 
606 	return dev;
607 }
608 
609 struct amba_device *
amba_apb_device_add(struct device * parent,const char * name,resource_size_t base,size_t size,int irq1,int irq2,void * pdata,unsigned int periphid)610 amba_apb_device_add(struct device *parent, const char *name,
611 		    resource_size_t base, size_t size, int irq1, int irq2,
612 		    void *pdata, unsigned int periphid)
613 {
614 	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
615 				    periphid, 0, &iomem_resource);
616 }
617 EXPORT_SYMBOL_GPL(amba_apb_device_add);
618 
619 struct amba_device *
amba_ahb_device_add(struct device * parent,const char * name,resource_size_t base,size_t size,int irq1,int irq2,void * pdata,unsigned int periphid)620 amba_ahb_device_add(struct device *parent, const char *name,
621 		    resource_size_t base, size_t size, int irq1, int irq2,
622 		    void *pdata, unsigned int periphid)
623 {
624 	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
625 				    periphid, ~0ULL, &iomem_resource);
626 }
627 EXPORT_SYMBOL_GPL(amba_ahb_device_add);
628 
629 struct amba_device *
amba_apb_device_add_res(struct device * parent,const char * name,resource_size_t base,size_t size,int irq1,int irq2,void * pdata,unsigned int periphid,struct resource * resbase)630 amba_apb_device_add_res(struct device *parent, const char *name,
631 			resource_size_t base, size_t size, int irq1,
632 			int irq2, void *pdata, unsigned int periphid,
633 			struct resource *resbase)
634 {
635 	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
636 				    periphid, 0, resbase);
637 }
638 EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
639 
640 struct amba_device *
amba_ahb_device_add_res(struct device * parent,const char * name,resource_size_t base,size_t size,int irq1,int irq2,void * pdata,unsigned int periphid,struct resource * resbase)641 amba_ahb_device_add_res(struct device *parent, const char *name,
642 			resource_size_t base, size_t size, int irq1,
643 			int irq2, void *pdata, unsigned int periphid,
644 			struct resource *resbase)
645 {
646 	return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
647 				    periphid, ~0ULL, resbase);
648 }
649 EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
650 
651 
amba_device_initialize(struct amba_device * dev,const char * name)652 static void amba_device_initialize(struct amba_device *dev, const char *name)
653 {
654 	device_initialize(&dev->dev);
655 	if (name)
656 		dev_set_name(&dev->dev, "%s", name);
657 	dev->dev.release = amba_device_release;
658 	dev->dev.bus = &amba_bustype;
659 	dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
660 	dev->dev.dma_parms = &dev->dma_parms;
661 	dev->res.name = dev_name(&dev->dev);
662 }
663 
664 /**
665  *	amba_device_alloc - allocate an AMBA device
666  *	@name: sysfs name of the AMBA device
667  *	@base: base of AMBA device
668  *	@size: size of AMBA device
669  *
670  *	Allocate and initialize an AMBA device structure.  Returns %NULL
671  *	on failure.
672  */
amba_device_alloc(const char * name,resource_size_t base,size_t size)673 struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
674 	size_t size)
675 {
676 	struct amba_device *dev;
677 
678 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
679 	if (dev) {
680 		amba_device_initialize(dev, name);
681 		dev->res.start = base;
682 		dev->res.end = base + size - 1;
683 		dev->res.flags = IORESOURCE_MEM;
684 	}
685 
686 	return dev;
687 }
688 EXPORT_SYMBOL_GPL(amba_device_alloc);
689 
690 /**
691  *	amba_device_register - register an AMBA device
692  *	@dev: AMBA device to register
693  *	@parent: parent memory resource
694  *
695  *	Setup the AMBA device, reading the cell ID if present.
696  *	Claim the resource, and register the AMBA device with
697  *	the Linux device manager.
698  */
amba_device_register(struct amba_device * dev,struct resource * parent)699 int amba_device_register(struct amba_device *dev, struct resource *parent)
700 {
701 	amba_device_initialize(dev, dev->dev.init_name);
702 	dev->dev.init_name = NULL;
703 
704 	return amba_device_add(dev, parent);
705 }
706 
707 /**
708  *	amba_device_put - put an AMBA device
709  *	@dev: AMBA device to put
710  */
amba_device_put(struct amba_device * dev)711 void amba_device_put(struct amba_device *dev)
712 {
713 	put_device(&dev->dev);
714 }
715 EXPORT_SYMBOL_GPL(amba_device_put);
716 
717 /**
718  *	amba_device_unregister - unregister an AMBA device
719  *	@dev: AMBA device to remove
720  *
721  *	Remove the specified AMBA device from the Linux device
722  *	manager.  All files associated with this object will be
723  *	destroyed, and device drivers notified that the device has
724  *	been removed.  The AMBA device's resources including
725  *	the amba_device structure will be freed once all
726  *	references to it have been dropped.
727  */
amba_device_unregister(struct amba_device * dev)728 void amba_device_unregister(struct amba_device *dev)
729 {
730 	device_unregister(&dev->dev);
731 }
732 
733 
734 struct find_data {
735 	struct amba_device *dev;
736 	struct device *parent;
737 	const char *busid;
738 	unsigned int id;
739 	unsigned int mask;
740 };
741 
amba_find_match(struct device * dev,void * data)742 static int amba_find_match(struct device *dev, void *data)
743 {
744 	struct find_data *d = data;
745 	struct amba_device *pcdev = to_amba_device(dev);
746 	int r;
747 
748 	r = (pcdev->periphid & d->mask) == d->id;
749 	if (d->parent)
750 		r &= d->parent == dev->parent;
751 	if (d->busid)
752 		r &= strcmp(dev_name(dev), d->busid) == 0;
753 
754 	if (r) {
755 		get_device(dev);
756 		d->dev = pcdev;
757 	}
758 
759 	return r;
760 }
761 
762 /**
763  *	amba_find_device - locate an AMBA device given a bus id
764  *	@busid: bus id for device (or NULL)
765  *	@parent: parent device (or NULL)
766  *	@id: peripheral ID (or 0)
767  *	@mask: peripheral ID mask (or 0)
768  *
769  *	Return the AMBA device corresponding to the supplied parameters.
770  *	If no device matches, returns NULL.
771  *
772  *	NOTE: When a valid device is found, its refcount is
773  *	incremented, and must be decremented before the returned
774  *	reference.
775  */
776 struct amba_device *
amba_find_device(const char * busid,struct device * parent,unsigned int id,unsigned int mask)777 amba_find_device(const char *busid, struct device *parent, unsigned int id,
778 		 unsigned int mask)
779 {
780 	struct find_data data;
781 
782 	data.dev = NULL;
783 	data.parent = parent;
784 	data.busid = busid;
785 	data.id = id;
786 	data.mask = mask;
787 
788 	bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
789 
790 	return data.dev;
791 }
792 
793 /**
794  *	amba_request_regions - request all mem regions associated with device
795  *	@dev: amba_device structure for device
796  *	@name: name, or NULL to use driver name
797  */
amba_request_regions(struct amba_device * dev,const char * name)798 int amba_request_regions(struct amba_device *dev, const char *name)
799 {
800 	int ret = 0;
801 	u32 size;
802 
803 	if (!name)
804 		name = dev->dev.driver->name;
805 
806 	size = resource_size(&dev->res);
807 
808 	if (!request_mem_region(dev->res.start, size, name))
809 		ret = -EBUSY;
810 
811 	return ret;
812 }
813 
814 /**
815  *	amba_release_regions - release mem regions associated with device
816  *	@dev: amba_device structure for device
817  *
818  *	Release regions claimed by a successful call to amba_request_regions.
819  */
amba_release_regions(struct amba_device * dev)820 void amba_release_regions(struct amba_device *dev)
821 {
822 	u32 size;
823 
824 	size = resource_size(&dev->res);
825 	release_mem_region(dev->res.start, size);
826 }
827 
828 EXPORT_SYMBOL(amba_driver_register);
829 EXPORT_SYMBOL(amba_driver_unregister);
830 EXPORT_SYMBOL(amba_device_register);
831 EXPORT_SYMBOL(amba_device_unregister);
832 EXPORT_SYMBOL(amba_find_device);
833 EXPORT_SYMBOL(amba_request_regions);
834 EXPORT_SYMBOL(amba_release_regions);
835