bus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * linux/arch/arm/common/amba.c
  3. *
  4. * Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/device.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/pm.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/pm_domain.h>
  19. #include <linux/amba/bus.h>
  20. #include <linux/sizes.h>
  21. #include <linux/limits.h>
  22. #include <linux/clk/clk-conf.h>
  23. #include <asm/irq.h>
  24. #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
  25. static const struct amba_id *
  26. amba_lookup(const struct amba_id *table, struct amba_device *dev)
  27. {
  28. int ret = 0;
  29. while (table->mask) {
  30. ret = (dev->periphid & table->mask) == table->id;
  31. if (ret)
  32. break;
  33. table++;
  34. }
  35. return ret ? table : NULL;
  36. }
  37. static int amba_match(struct device *dev, struct device_driver *drv)
  38. {
  39. struct amba_device *pcdev = to_amba_device(dev);
  40. struct amba_driver *pcdrv = to_amba_driver(drv);
  41. /* When driver_override is set, only bind to the matching driver */
  42. if (pcdev->driver_override)
  43. return !strcmp(pcdev->driver_override, drv->name);
  44. return amba_lookup(pcdrv->id_table, pcdev) != NULL;
  45. }
  46. static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
  47. {
  48. struct amba_device *pcdev = to_amba_device(dev);
  49. int retval = 0;
  50. retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
  51. if (retval)
  52. return retval;
  53. retval = add_uevent_var(env, "MODALIAS=amba:d%08X", pcdev->periphid);
  54. return retval;
  55. }
  56. static ssize_t driver_override_show(struct device *_dev,
  57. struct device_attribute *attr, char *buf)
  58. {
  59. struct amba_device *dev = to_amba_device(_dev);
  60. ssize_t len;
  61. device_lock(_dev);
  62. len = sprintf(buf, "%s\n", dev->driver_override);
  63. device_unlock(_dev);
  64. return len;
  65. }
  66. static ssize_t driver_override_store(struct device *_dev,
  67. struct device_attribute *attr,
  68. const char *buf, size_t count)
  69. {
  70. struct amba_device *dev = to_amba_device(_dev);
  71. char *driver_override, *old, *cp;
  72. /* We need to keep extra room for a newline */
  73. if (count >= (PAGE_SIZE - 1))
  74. return -EINVAL;
  75. driver_override = kstrndup(buf, count, GFP_KERNEL);
  76. if (!driver_override)
  77. return -ENOMEM;
  78. cp = strchr(driver_override, '\n');
  79. if (cp)
  80. *cp = '\0';
  81. device_lock(_dev);
  82. old = dev->driver_override;
  83. if (strlen(driver_override)) {
  84. dev->driver_override = driver_override;
  85. } else {
  86. kfree(driver_override);
  87. dev->driver_override = NULL;
  88. }
  89. device_unlock(_dev);
  90. kfree(old);
  91. return count;
  92. }
  93. #define amba_attr_func(name,fmt,arg...) \
  94. static ssize_t name##_show(struct device *_dev, \
  95. struct device_attribute *attr, char *buf) \
  96. { \
  97. struct amba_device *dev = to_amba_device(_dev); \
  98. return sprintf(buf, fmt, arg); \
  99. }
  100. #define amba_attr(name,fmt,arg...) \
  101. amba_attr_func(name,fmt,arg) \
  102. static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
  103. amba_attr_func(id, "%08x\n", dev->periphid);
  104. amba_attr(irq0, "%u\n", dev->irq[0]);
  105. amba_attr(irq1, "%u\n", dev->irq[1]);
  106. amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
  107. (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
  108. dev->res.flags);
  109. static struct device_attribute amba_dev_attrs[] = {
  110. __ATTR_RO(id),
  111. __ATTR_RO(resource),
  112. __ATTR_RW(driver_override),
  113. __ATTR_NULL,
  114. };
  115. #ifdef CONFIG_PM
  116. /*
  117. * Hooks to provide runtime PM of the pclk (bus clock). It is safe to
  118. * enable/disable the bus clock at runtime PM suspend/resume as this
  119. * does not result in loss of context.
  120. */
  121. static int amba_pm_runtime_suspend(struct device *dev)
  122. {
  123. struct amba_device *pcdev = to_amba_device(dev);
  124. int ret = pm_generic_runtime_suspend(dev);
  125. if (ret == 0 && dev->driver) {
  126. if (pm_runtime_is_irq_safe(dev))
  127. clk_disable(pcdev->pclk);
  128. else
  129. clk_disable_unprepare(pcdev->pclk);
  130. }
  131. return ret;
  132. }
  133. static int amba_pm_runtime_resume(struct device *dev)
  134. {
  135. struct amba_device *pcdev = to_amba_device(dev);
  136. int ret;
  137. if (dev->driver) {
  138. if (pm_runtime_is_irq_safe(dev))
  139. ret = clk_enable(pcdev->pclk);
  140. else
  141. ret = clk_prepare_enable(pcdev->pclk);
  142. /* Failure is probably fatal to the system, but... */
  143. if (ret)
  144. return ret;
  145. }
  146. return pm_generic_runtime_resume(dev);
  147. }
  148. #endif /* CONFIG_PM */
  149. static const struct dev_pm_ops amba_pm = {
  150. .suspend = pm_generic_suspend,
  151. .resume = pm_generic_resume,
  152. .freeze = pm_generic_freeze,
  153. .thaw = pm_generic_thaw,
  154. .poweroff = pm_generic_poweroff,
  155. .restore = pm_generic_restore,
  156. SET_RUNTIME_PM_OPS(
  157. amba_pm_runtime_suspend,
  158. amba_pm_runtime_resume,
  159. NULL
  160. )
  161. };
  162. /*
  163. * Primecells are part of the Advanced Microcontroller Bus Architecture,
  164. * so we call the bus "amba".
  165. */
  166. struct bus_type amba_bustype = {
  167. .name = "amba",
  168. .dev_attrs = amba_dev_attrs,
  169. .match = amba_match,
  170. .uevent = amba_uevent,
  171. .pm = &amba_pm,
  172. };
  173. static int __init amba_init(void)
  174. {
  175. return bus_register(&amba_bustype);
  176. }
  177. postcore_initcall(amba_init);
  178. static int amba_get_enable_pclk(struct amba_device *pcdev)
  179. {
  180. int ret;
  181. pcdev->pclk = clk_get(&pcdev->dev, "apb_pclk");
  182. if (IS_ERR(pcdev->pclk))
  183. return PTR_ERR(pcdev->pclk);
  184. ret = clk_prepare_enable(pcdev->pclk);
  185. if (ret)
  186. clk_put(pcdev->pclk);
  187. return ret;
  188. }
  189. static void amba_put_disable_pclk(struct amba_device *pcdev)
  190. {
  191. clk_disable_unprepare(pcdev->pclk);
  192. clk_put(pcdev->pclk);
  193. }
  194. /*
  195. * These are the device model conversion veneers; they convert the
  196. * device model structures to our more specific structures.
  197. */
  198. static int amba_probe(struct device *dev)
  199. {
  200. struct amba_device *pcdev = to_amba_device(dev);
  201. struct amba_driver *pcdrv = to_amba_driver(dev->driver);
  202. const struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
  203. int ret;
  204. do {
  205. ret = of_clk_set_defaults(dev->of_node, false);
  206. if (ret < 0)
  207. break;
  208. ret = dev_pm_domain_attach(dev, true);
  209. if (ret == -EPROBE_DEFER)
  210. break;
  211. ret = amba_get_enable_pclk(pcdev);
  212. if (ret) {
  213. dev_pm_domain_detach(dev, true);
  214. break;
  215. }
  216. pm_runtime_get_noresume(dev);
  217. pm_runtime_set_active(dev);
  218. pm_runtime_enable(dev);
  219. ret = pcdrv->probe(pcdev, id);
  220. if (ret == 0)
  221. break;
  222. pm_runtime_disable(dev);
  223. pm_runtime_set_suspended(dev);
  224. pm_runtime_put_noidle(dev);
  225. amba_put_disable_pclk(pcdev);
  226. dev_pm_domain_detach(dev, true);
  227. } while (0);
  228. return ret;
  229. }
  230. static int amba_remove(struct device *dev)
  231. {
  232. struct amba_device *pcdev = to_amba_device(dev);
  233. struct amba_driver *drv = to_amba_driver(dev->driver);
  234. int ret;
  235. pm_runtime_get_sync(dev);
  236. ret = drv->remove(pcdev);
  237. pm_runtime_put_noidle(dev);
  238. /* Undo the runtime PM settings in amba_probe() */
  239. pm_runtime_disable(dev);
  240. pm_runtime_set_suspended(dev);
  241. pm_runtime_put_noidle(dev);
  242. amba_put_disable_pclk(pcdev);
  243. dev_pm_domain_detach(dev, true);
  244. return ret;
  245. }
  246. static void amba_shutdown(struct device *dev)
  247. {
  248. struct amba_driver *drv = to_amba_driver(dev->driver);
  249. drv->shutdown(to_amba_device(dev));
  250. }
  251. /**
  252. * amba_driver_register - register an AMBA device driver
  253. * @drv: amba device driver structure
  254. *
  255. * Register an AMBA device driver with the Linux device model
  256. * core. If devices pre-exist, the drivers probe function will
  257. * be called.
  258. */
  259. int amba_driver_register(struct amba_driver *drv)
  260. {
  261. drv->drv.bus = &amba_bustype;
  262. #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
  263. SETFN(probe);
  264. SETFN(remove);
  265. SETFN(shutdown);
  266. return driver_register(&drv->drv);
  267. }
  268. /**
  269. * amba_driver_unregister - remove an AMBA device driver
  270. * @drv: AMBA device driver structure to remove
  271. *
  272. * Unregister an AMBA device driver from the Linux device
  273. * model. The device model will call the drivers remove function
  274. * for each device the device driver is currently handling.
  275. */
  276. void amba_driver_unregister(struct amba_driver *drv)
  277. {
  278. driver_unregister(&drv->drv);
  279. }
  280. static void amba_device_release(struct device *dev)
  281. {
  282. struct amba_device *d = to_amba_device(dev);
  283. if (d->res.parent)
  284. release_resource(&d->res);
  285. kfree(d);
  286. }
  287. static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
  288. {
  289. u32 size;
  290. void __iomem *tmp;
  291. int i, ret;
  292. WARN_ON(dev->irq[0] == (unsigned int)-1);
  293. WARN_ON(dev->irq[1] == (unsigned int)-1);
  294. ret = request_resource(parent, &dev->res);
  295. if (ret)
  296. goto err_out;
  297. /* Hard-coded primecell ID instead of plug-n-play */
  298. if (dev->periphid != 0)
  299. goto skip_probe;
  300. /*
  301. * Dynamically calculate the size of the resource
  302. * and use this for iomap
  303. */
  304. size = resource_size(&dev->res);
  305. tmp = ioremap(dev->res.start, size);
  306. if (!tmp) {
  307. ret = -ENOMEM;
  308. goto err_release;
  309. }
  310. ret = dev_pm_domain_attach(&dev->dev, true);
  311. if (ret == -EPROBE_DEFER) {
  312. iounmap(tmp);
  313. goto err_release;
  314. }
  315. ret = amba_get_enable_pclk(dev);
  316. if (ret == 0) {
  317. u32 pid, cid;
  318. /*
  319. * Read pid and cid based on size of resource
  320. * they are located at end of region
  321. */
  322. for (pid = 0, i = 0; i < 4; i++)
  323. pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
  324. (i * 8);
  325. for (cid = 0, i = 0; i < 4; i++)
  326. cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
  327. (i * 8);
  328. amba_put_disable_pclk(dev);
  329. if (cid == AMBA_CID || cid == CORESIGHT_CID)
  330. dev->periphid = pid;
  331. if (!dev->periphid)
  332. ret = -ENODEV;
  333. }
  334. iounmap(tmp);
  335. dev_pm_domain_detach(&dev->dev, true);
  336. if (ret)
  337. goto err_release;
  338. skip_probe:
  339. ret = device_add(&dev->dev);
  340. if (ret)
  341. goto err_release;
  342. if (dev->irq[0])
  343. ret = device_create_file(&dev->dev, &dev_attr_irq0);
  344. if (ret == 0 && dev->irq[1])
  345. ret = device_create_file(&dev->dev, &dev_attr_irq1);
  346. if (ret == 0)
  347. return ret;
  348. device_unregister(&dev->dev);
  349. err_release:
  350. release_resource(&dev->res);
  351. err_out:
  352. return ret;
  353. }
  354. /*
  355. * Registration of AMBA device require reading its pid and cid registers.
  356. * To do this, the device must be turned on (if it is a part of power domain)
  357. * and have clocks enabled. However in some cases those resources might not be
  358. * yet available. Returning EPROBE_DEFER is not a solution in such case,
  359. * because callers don't handle this special error code. Instead such devices
  360. * are added to the special list and their registration is retried from
  361. * periodic worker, until all resources are available and registration succeeds.
  362. */
  363. struct deferred_device {
  364. struct amba_device *dev;
  365. struct resource *parent;
  366. struct list_head node;
  367. };
  368. static LIST_HEAD(deferred_devices);
  369. static DEFINE_MUTEX(deferred_devices_lock);
  370. static void amba_deferred_retry_func(struct work_struct *dummy);
  371. static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
  372. #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
  373. static void amba_deferred_retry_func(struct work_struct *dummy)
  374. {
  375. struct deferred_device *ddev, *tmp;
  376. mutex_lock(&deferred_devices_lock);
  377. list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
  378. int ret = amba_device_try_add(ddev->dev, ddev->parent);
  379. if (ret == -EPROBE_DEFER)
  380. continue;
  381. list_del_init(&ddev->node);
  382. kfree(ddev);
  383. }
  384. if (!list_empty(&deferred_devices))
  385. schedule_delayed_work(&deferred_retry_work,
  386. DEFERRED_DEVICE_TIMEOUT);
  387. mutex_unlock(&deferred_devices_lock);
  388. }
  389. /**
  390. * amba_device_add - add a previously allocated AMBA device structure
  391. * @dev: AMBA device allocated by amba_device_alloc
  392. * @parent: resource parent for this devices resources
  393. *
  394. * Claim the resource, and read the device cell ID if not already
  395. * initialized. Register the AMBA device with the Linux device
  396. * manager.
  397. */
  398. int amba_device_add(struct amba_device *dev, struct resource *parent)
  399. {
  400. int ret = amba_device_try_add(dev, parent);
  401. if (ret == -EPROBE_DEFER) {
  402. struct deferred_device *ddev;
  403. ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
  404. if (!ddev)
  405. return -ENOMEM;
  406. ddev->dev = dev;
  407. ddev->parent = parent;
  408. ret = 0;
  409. mutex_lock(&deferred_devices_lock);
  410. if (list_empty(&deferred_devices))
  411. schedule_delayed_work(&deferred_retry_work,
  412. DEFERRED_DEVICE_TIMEOUT);
  413. list_add_tail(&ddev->node, &deferred_devices);
  414. mutex_unlock(&deferred_devices_lock);
  415. }
  416. return ret;
  417. }
  418. EXPORT_SYMBOL_GPL(amba_device_add);
  419. static struct amba_device *
  420. amba_aphb_device_add(struct device *parent, const char *name,
  421. resource_size_t base, size_t size, int irq1, int irq2,
  422. void *pdata, unsigned int periphid, u64 dma_mask,
  423. struct resource *resbase)
  424. {
  425. struct amba_device *dev;
  426. int ret;
  427. dev = amba_device_alloc(name, base, size);
  428. if (!dev)
  429. return ERR_PTR(-ENOMEM);
  430. dev->dev.coherent_dma_mask = dma_mask;
  431. dev->irq[0] = irq1;
  432. dev->irq[1] = irq2;
  433. dev->periphid = periphid;
  434. dev->dev.platform_data = pdata;
  435. dev->dev.parent = parent;
  436. ret = amba_device_add(dev, resbase);
  437. if (ret) {
  438. amba_device_put(dev);
  439. return ERR_PTR(ret);
  440. }
  441. return dev;
  442. }
  443. struct amba_device *
  444. amba_apb_device_add(struct device *parent, const char *name,
  445. resource_size_t base, size_t size, int irq1, int irq2,
  446. void *pdata, unsigned int periphid)
  447. {
  448. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  449. periphid, 0, &iomem_resource);
  450. }
  451. EXPORT_SYMBOL_GPL(amba_apb_device_add);
  452. struct amba_device *
  453. amba_ahb_device_add(struct device *parent, const char *name,
  454. resource_size_t base, size_t size, int irq1, int irq2,
  455. void *pdata, unsigned int periphid)
  456. {
  457. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  458. periphid, ~0ULL, &iomem_resource);
  459. }
  460. EXPORT_SYMBOL_GPL(amba_ahb_device_add);
  461. struct amba_device *
  462. amba_apb_device_add_res(struct device *parent, const char *name,
  463. resource_size_t base, size_t size, int irq1,
  464. int irq2, void *pdata, unsigned int periphid,
  465. struct resource *resbase)
  466. {
  467. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  468. periphid, 0, resbase);
  469. }
  470. EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
  471. struct amba_device *
  472. amba_ahb_device_add_res(struct device *parent, const char *name,
  473. resource_size_t base, size_t size, int irq1,
  474. int irq2, void *pdata, unsigned int periphid,
  475. struct resource *resbase)
  476. {
  477. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  478. periphid, ~0ULL, resbase);
  479. }
  480. EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
  481. static void amba_device_initialize(struct amba_device *dev, const char *name)
  482. {
  483. device_initialize(&dev->dev);
  484. if (name)
  485. dev_set_name(&dev->dev, "%s", name);
  486. dev->dev.release = amba_device_release;
  487. dev->dev.bus = &amba_bustype;
  488. dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
  489. dev->res.name = dev_name(&dev->dev);
  490. }
  491. /**
  492. * amba_device_alloc - allocate an AMBA device
  493. * @name: sysfs name of the AMBA device
  494. * @base: base of AMBA device
  495. * @size: size of AMBA device
  496. *
  497. * Allocate and initialize an AMBA device structure. Returns %NULL
  498. * on failure.
  499. */
  500. struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
  501. size_t size)
  502. {
  503. struct amba_device *dev;
  504. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  505. if (dev) {
  506. amba_device_initialize(dev, name);
  507. dev->res.start = base;
  508. dev->res.end = base + size - 1;
  509. dev->res.flags = IORESOURCE_MEM;
  510. }
  511. return dev;
  512. }
  513. EXPORT_SYMBOL_GPL(amba_device_alloc);
  514. /**
  515. * amba_device_register - register an AMBA device
  516. * @dev: AMBA device to register
  517. * @parent: parent memory resource
  518. *
  519. * Setup the AMBA device, reading the cell ID if present.
  520. * Claim the resource, and register the AMBA device with
  521. * the Linux device manager.
  522. */
  523. int amba_device_register(struct amba_device *dev, struct resource *parent)
  524. {
  525. amba_device_initialize(dev, dev->dev.init_name);
  526. dev->dev.init_name = NULL;
  527. return amba_device_add(dev, parent);
  528. }
  529. /**
  530. * amba_device_put - put an AMBA device
  531. * @dev: AMBA device to put
  532. */
  533. void amba_device_put(struct amba_device *dev)
  534. {
  535. put_device(&dev->dev);
  536. }
  537. EXPORT_SYMBOL_GPL(amba_device_put);
  538. /**
  539. * amba_device_unregister - unregister an AMBA device
  540. * @dev: AMBA device to remove
  541. *
  542. * Remove the specified AMBA device from the Linux device
  543. * manager. All files associated with this object will be
  544. * destroyed, and device drivers notified that the device has
  545. * been removed. The AMBA device's resources including
  546. * the amba_device structure will be freed once all
  547. * references to it have been dropped.
  548. */
  549. void amba_device_unregister(struct amba_device *dev)
  550. {
  551. device_unregister(&dev->dev);
  552. }
  553. struct find_data {
  554. struct amba_device *dev;
  555. struct device *parent;
  556. const char *busid;
  557. unsigned int id;
  558. unsigned int mask;
  559. };
  560. static int amba_find_match(struct device *dev, void *data)
  561. {
  562. struct find_data *d = data;
  563. struct amba_device *pcdev = to_amba_device(dev);
  564. int r;
  565. r = (pcdev->periphid & d->mask) == d->id;
  566. if (d->parent)
  567. r &= d->parent == dev->parent;
  568. if (d->busid)
  569. r &= strcmp(dev_name(dev), d->busid) == 0;
  570. if (r) {
  571. get_device(dev);
  572. d->dev = pcdev;
  573. }
  574. return r;
  575. }
  576. /**
  577. * amba_find_device - locate an AMBA device given a bus id
  578. * @busid: bus id for device (or NULL)
  579. * @parent: parent device (or NULL)
  580. * @id: peripheral ID (or 0)
  581. * @mask: peripheral ID mask (or 0)
  582. *
  583. * Return the AMBA device corresponding to the supplied parameters.
  584. * If no device matches, returns NULL.
  585. *
  586. * NOTE: When a valid device is found, its refcount is
  587. * incremented, and must be decremented before the returned
  588. * reference.
  589. */
  590. struct amba_device *
  591. amba_find_device(const char *busid, struct device *parent, unsigned int id,
  592. unsigned int mask)
  593. {
  594. struct find_data data;
  595. data.dev = NULL;
  596. data.parent = parent;
  597. data.busid = busid;
  598. data.id = id;
  599. data.mask = mask;
  600. bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
  601. return data.dev;
  602. }
  603. /**
  604. * amba_request_regions - request all mem regions associated with device
  605. * @dev: amba_device structure for device
  606. * @name: name, or NULL to use driver name
  607. */
  608. int amba_request_regions(struct amba_device *dev, const char *name)
  609. {
  610. int ret = 0;
  611. u32 size;
  612. if (!name)
  613. name = dev->dev.driver->name;
  614. size = resource_size(&dev->res);
  615. if (!request_mem_region(dev->res.start, size, name))
  616. ret = -EBUSY;
  617. return ret;
  618. }
  619. /**
  620. * amba_release_regions - release mem regions associated with device
  621. * @dev: amba_device structure for device
  622. *
  623. * Release regions claimed by a successful call to amba_request_regions.
  624. */
  625. void amba_release_regions(struct amba_device *dev)
  626. {
  627. u32 size;
  628. size = resource_size(&dev->res);
  629. release_mem_region(dev->res.start, size);
  630. }
  631. EXPORT_SYMBOL(amba_driver_register);
  632. EXPORT_SYMBOL(amba_driver_unregister);
  633. EXPORT_SYMBOL(amba_device_register);
  634. EXPORT_SYMBOL(amba_device_unregister);
  635. EXPORT_SYMBOL(amba_find_device);
  636. EXPORT_SYMBOL(amba_request_regions);
  637. EXPORT_SYMBOL(amba_release_regions);