bus.c 18 KB

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