bus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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 = 0;
  240. pm_runtime_get_sync(dev);
  241. if (drv->remove)
  242. ret = drv->remove(pcdev);
  243. pm_runtime_put_noidle(dev);
  244. /* Undo the runtime PM settings in amba_probe() */
  245. pm_runtime_disable(dev);
  246. pm_runtime_set_suspended(dev);
  247. pm_runtime_put_noidle(dev);
  248. amba_put_disable_pclk(pcdev);
  249. dev_pm_domain_detach(dev, true);
  250. return ret;
  251. }
  252. static void amba_shutdown(struct device *dev)
  253. {
  254. struct amba_driver *drv = to_amba_driver(dev->driver);
  255. if (drv->shutdown)
  256. drv->shutdown(to_amba_device(dev));
  257. }
  258. /**
  259. * amba_driver_register - register an AMBA device driver
  260. * @drv: amba device driver structure
  261. *
  262. * Register an AMBA device driver with the Linux device model
  263. * core. If devices pre-exist, the drivers probe function will
  264. * be called.
  265. */
  266. int amba_driver_register(struct amba_driver *drv)
  267. {
  268. if (!drv->probe)
  269. return -EINVAL;
  270. drv->drv.bus = &amba_bustype;
  271. drv->drv.probe = amba_probe;
  272. drv->drv.remove = amba_remove;
  273. drv->drv.shutdown = amba_shutdown;
  274. return driver_register(&drv->drv);
  275. }
  276. /**
  277. * amba_driver_unregister - remove an AMBA device driver
  278. * @drv: AMBA device driver structure to remove
  279. *
  280. * Unregister an AMBA device driver from the Linux device
  281. * model. The device model will call the drivers remove function
  282. * for each device the device driver is currently handling.
  283. */
  284. void amba_driver_unregister(struct amba_driver *drv)
  285. {
  286. driver_unregister(&drv->drv);
  287. }
  288. static void amba_device_release(struct device *dev)
  289. {
  290. struct amba_device *d = to_amba_device(dev);
  291. if (d->res.parent)
  292. release_resource(&d->res);
  293. kfree(d);
  294. }
  295. static int amba_device_try_add(struct amba_device *dev, struct resource *parent)
  296. {
  297. u32 size;
  298. void __iomem *tmp;
  299. int i, ret;
  300. WARN_ON(dev->irq[0] == (unsigned int)-1);
  301. WARN_ON(dev->irq[1] == (unsigned int)-1);
  302. ret = request_resource(parent, &dev->res);
  303. if (ret)
  304. goto err_out;
  305. /* Hard-coded primecell ID instead of plug-n-play */
  306. if (dev->periphid != 0)
  307. goto skip_probe;
  308. /*
  309. * Dynamically calculate the size of the resource
  310. * and use this for iomap
  311. */
  312. size = resource_size(&dev->res);
  313. tmp = ioremap(dev->res.start, size);
  314. if (!tmp) {
  315. ret = -ENOMEM;
  316. goto err_release;
  317. }
  318. ret = dev_pm_domain_attach(&dev->dev, true);
  319. if (ret) {
  320. iounmap(tmp);
  321. goto err_release;
  322. }
  323. ret = amba_get_enable_pclk(dev);
  324. if (ret == 0) {
  325. u32 pid, cid;
  326. /*
  327. * Read pid and cid based on size of resource
  328. * they are located at end of region
  329. */
  330. for (pid = 0, i = 0; i < 4; i++)
  331. pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
  332. (i * 8);
  333. for (cid = 0, i = 0; i < 4; i++)
  334. cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
  335. (i * 8);
  336. amba_put_disable_pclk(dev);
  337. if (cid == AMBA_CID || cid == CORESIGHT_CID)
  338. dev->periphid = pid;
  339. if (!dev->periphid)
  340. ret = -ENODEV;
  341. }
  342. iounmap(tmp);
  343. dev_pm_domain_detach(&dev->dev, true);
  344. if (ret)
  345. goto err_release;
  346. skip_probe:
  347. ret = device_add(&dev->dev);
  348. if (ret)
  349. goto err_release;
  350. if (dev->irq[0])
  351. ret = device_create_file(&dev->dev, &dev_attr_irq0);
  352. if (ret == 0 && dev->irq[1])
  353. ret = device_create_file(&dev->dev, &dev_attr_irq1);
  354. if (ret == 0)
  355. return ret;
  356. device_unregister(&dev->dev);
  357. err_release:
  358. release_resource(&dev->res);
  359. err_out:
  360. return ret;
  361. }
  362. /*
  363. * Registration of AMBA device require reading its pid and cid registers.
  364. * To do this, the device must be turned on (if it is a part of power domain)
  365. * and have clocks enabled. However in some cases those resources might not be
  366. * yet available. Returning EPROBE_DEFER is not a solution in such case,
  367. * because callers don't handle this special error code. Instead such devices
  368. * are added to the special list and their registration is retried from
  369. * periodic worker, until all resources are available and registration succeeds.
  370. */
  371. struct deferred_device {
  372. struct amba_device *dev;
  373. struct resource *parent;
  374. struct list_head node;
  375. };
  376. static LIST_HEAD(deferred_devices);
  377. static DEFINE_MUTEX(deferred_devices_lock);
  378. static void amba_deferred_retry_func(struct work_struct *dummy);
  379. static DECLARE_DELAYED_WORK(deferred_retry_work, amba_deferred_retry_func);
  380. #define DEFERRED_DEVICE_TIMEOUT (msecs_to_jiffies(5 * 1000))
  381. static void amba_deferred_retry_func(struct work_struct *dummy)
  382. {
  383. struct deferred_device *ddev, *tmp;
  384. mutex_lock(&deferred_devices_lock);
  385. list_for_each_entry_safe(ddev, tmp, &deferred_devices, node) {
  386. int ret = amba_device_try_add(ddev->dev, ddev->parent);
  387. if (ret == -EPROBE_DEFER)
  388. continue;
  389. list_del_init(&ddev->node);
  390. kfree(ddev);
  391. }
  392. if (!list_empty(&deferred_devices))
  393. schedule_delayed_work(&deferred_retry_work,
  394. DEFERRED_DEVICE_TIMEOUT);
  395. mutex_unlock(&deferred_devices_lock);
  396. }
  397. /**
  398. * amba_device_add - add a previously allocated AMBA device structure
  399. * @dev: AMBA device allocated by amba_device_alloc
  400. * @parent: resource parent for this devices resources
  401. *
  402. * Claim the resource, and read the device cell ID if not already
  403. * initialized. Register the AMBA device with the Linux device
  404. * manager.
  405. */
  406. int amba_device_add(struct amba_device *dev, struct resource *parent)
  407. {
  408. int ret = amba_device_try_add(dev, parent);
  409. if (ret == -EPROBE_DEFER) {
  410. struct deferred_device *ddev;
  411. ddev = kmalloc(sizeof(*ddev), GFP_KERNEL);
  412. if (!ddev)
  413. return -ENOMEM;
  414. ddev->dev = dev;
  415. ddev->parent = parent;
  416. ret = 0;
  417. mutex_lock(&deferred_devices_lock);
  418. if (list_empty(&deferred_devices))
  419. schedule_delayed_work(&deferred_retry_work,
  420. DEFERRED_DEVICE_TIMEOUT);
  421. list_add_tail(&ddev->node, &deferred_devices);
  422. mutex_unlock(&deferred_devices_lock);
  423. }
  424. return ret;
  425. }
  426. EXPORT_SYMBOL_GPL(amba_device_add);
  427. static struct amba_device *
  428. amba_aphb_device_add(struct device *parent, const char *name,
  429. resource_size_t base, size_t size, int irq1, int irq2,
  430. void *pdata, unsigned int periphid, u64 dma_mask,
  431. struct resource *resbase)
  432. {
  433. struct amba_device *dev;
  434. int ret;
  435. dev = amba_device_alloc(name, base, size);
  436. if (!dev)
  437. return ERR_PTR(-ENOMEM);
  438. dev->dev.coherent_dma_mask = dma_mask;
  439. dev->irq[0] = irq1;
  440. dev->irq[1] = irq2;
  441. dev->periphid = periphid;
  442. dev->dev.platform_data = pdata;
  443. dev->dev.parent = parent;
  444. ret = amba_device_add(dev, resbase);
  445. if (ret) {
  446. amba_device_put(dev);
  447. return ERR_PTR(ret);
  448. }
  449. return dev;
  450. }
  451. struct amba_device *
  452. amba_apb_device_add(struct device *parent, const char *name,
  453. resource_size_t base, size_t size, int irq1, int irq2,
  454. void *pdata, unsigned int periphid)
  455. {
  456. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  457. periphid, 0, &iomem_resource);
  458. }
  459. EXPORT_SYMBOL_GPL(amba_apb_device_add);
  460. struct amba_device *
  461. amba_ahb_device_add(struct device *parent, const char *name,
  462. resource_size_t base, size_t size, int irq1, int irq2,
  463. void *pdata, unsigned int periphid)
  464. {
  465. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  466. periphid, ~0ULL, &iomem_resource);
  467. }
  468. EXPORT_SYMBOL_GPL(amba_ahb_device_add);
  469. struct amba_device *
  470. amba_apb_device_add_res(struct device *parent, const char *name,
  471. resource_size_t base, size_t size, int irq1,
  472. int irq2, void *pdata, unsigned int periphid,
  473. struct resource *resbase)
  474. {
  475. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  476. periphid, 0, resbase);
  477. }
  478. EXPORT_SYMBOL_GPL(amba_apb_device_add_res);
  479. struct amba_device *
  480. amba_ahb_device_add_res(struct device *parent, const char *name,
  481. resource_size_t base, size_t size, int irq1,
  482. int irq2, void *pdata, unsigned int periphid,
  483. struct resource *resbase)
  484. {
  485. return amba_aphb_device_add(parent, name, base, size, irq1, irq2, pdata,
  486. periphid, ~0ULL, resbase);
  487. }
  488. EXPORT_SYMBOL_GPL(amba_ahb_device_add_res);
  489. static void amba_device_initialize(struct amba_device *dev, const char *name)
  490. {
  491. device_initialize(&dev->dev);
  492. if (name)
  493. dev_set_name(&dev->dev, "%s", name);
  494. dev->dev.release = amba_device_release;
  495. dev->dev.bus = &amba_bustype;
  496. dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
  497. dev->res.name = dev_name(&dev->dev);
  498. }
  499. /**
  500. * amba_device_alloc - allocate an AMBA device
  501. * @name: sysfs name of the AMBA device
  502. * @base: base of AMBA device
  503. * @size: size of AMBA device
  504. *
  505. * Allocate and initialize an AMBA device structure. Returns %NULL
  506. * on failure.
  507. */
  508. struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
  509. size_t size)
  510. {
  511. struct amba_device *dev;
  512. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  513. if (dev) {
  514. amba_device_initialize(dev, name);
  515. dev->res.start = base;
  516. dev->res.end = base + size - 1;
  517. dev->res.flags = IORESOURCE_MEM;
  518. }
  519. return dev;
  520. }
  521. EXPORT_SYMBOL_GPL(amba_device_alloc);
  522. /**
  523. * amba_device_register - register an AMBA device
  524. * @dev: AMBA device to register
  525. * @parent: parent memory resource
  526. *
  527. * Setup the AMBA device, reading the cell ID if present.
  528. * Claim the resource, and register the AMBA device with
  529. * the Linux device manager.
  530. */
  531. int amba_device_register(struct amba_device *dev, struct resource *parent)
  532. {
  533. amba_device_initialize(dev, dev->dev.init_name);
  534. dev->dev.init_name = NULL;
  535. return amba_device_add(dev, parent);
  536. }
  537. /**
  538. * amba_device_put - put an AMBA device
  539. * @dev: AMBA device to put
  540. */
  541. void amba_device_put(struct amba_device *dev)
  542. {
  543. put_device(&dev->dev);
  544. }
  545. EXPORT_SYMBOL_GPL(amba_device_put);
  546. /**
  547. * amba_device_unregister - unregister an AMBA device
  548. * @dev: AMBA device to remove
  549. *
  550. * Remove the specified AMBA device from the Linux device
  551. * manager. All files associated with this object will be
  552. * destroyed, and device drivers notified that the device has
  553. * been removed. The AMBA device's resources including
  554. * the amba_device structure will be freed once all
  555. * references to it have been dropped.
  556. */
  557. void amba_device_unregister(struct amba_device *dev)
  558. {
  559. device_unregister(&dev->dev);
  560. }
  561. struct find_data {
  562. struct amba_device *dev;
  563. struct device *parent;
  564. const char *busid;
  565. unsigned int id;
  566. unsigned int mask;
  567. };
  568. static int amba_find_match(struct device *dev, void *data)
  569. {
  570. struct find_data *d = data;
  571. struct amba_device *pcdev = to_amba_device(dev);
  572. int r;
  573. r = (pcdev->periphid & d->mask) == d->id;
  574. if (d->parent)
  575. r &= d->parent == dev->parent;
  576. if (d->busid)
  577. r &= strcmp(dev_name(dev), d->busid) == 0;
  578. if (r) {
  579. get_device(dev);
  580. d->dev = pcdev;
  581. }
  582. return r;
  583. }
  584. /**
  585. * amba_find_device - locate an AMBA device given a bus id
  586. * @busid: bus id for device (or NULL)
  587. * @parent: parent device (or NULL)
  588. * @id: peripheral ID (or 0)
  589. * @mask: peripheral ID mask (or 0)
  590. *
  591. * Return the AMBA device corresponding to the supplied parameters.
  592. * If no device matches, returns NULL.
  593. *
  594. * NOTE: When a valid device is found, its refcount is
  595. * incremented, and must be decremented before the returned
  596. * reference.
  597. */
  598. struct amba_device *
  599. amba_find_device(const char *busid, struct device *parent, unsigned int id,
  600. unsigned int mask)
  601. {
  602. struct find_data data;
  603. data.dev = NULL;
  604. data.parent = parent;
  605. data.busid = busid;
  606. data.id = id;
  607. data.mask = mask;
  608. bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
  609. return data.dev;
  610. }
  611. /**
  612. * amba_request_regions - request all mem regions associated with device
  613. * @dev: amba_device structure for device
  614. * @name: name, or NULL to use driver name
  615. */
  616. int amba_request_regions(struct amba_device *dev, const char *name)
  617. {
  618. int ret = 0;
  619. u32 size;
  620. if (!name)
  621. name = dev->dev.driver->name;
  622. size = resource_size(&dev->res);
  623. if (!request_mem_region(dev->res.start, size, name))
  624. ret = -EBUSY;
  625. return ret;
  626. }
  627. /**
  628. * amba_release_regions - release mem regions associated with device
  629. * @dev: amba_device structure for device
  630. *
  631. * Release regions claimed by a successful call to amba_request_regions.
  632. */
  633. void amba_release_regions(struct amba_device *dev)
  634. {
  635. u32 size;
  636. size = resource_size(&dev->res);
  637. release_mem_region(dev->res.start, size);
  638. }
  639. EXPORT_SYMBOL(amba_driver_register);
  640. EXPORT_SYMBOL(amba_driver_unregister);
  641. EXPORT_SYMBOL(amba_device_register);
  642. EXPORT_SYMBOL(amba_device_unregister);
  643. EXPORT_SYMBOL(amba_find_device);
  644. EXPORT_SYMBOL(amba_request_regions);
  645. EXPORT_SYMBOL(amba_release_regions);