bus.c 18 KB

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