bus.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/amba/bus.h>
  17. #include <asm/irq.h>
  18. #include <asm/sizes.h>
  19. #define to_amba_device(d) container_of(d, struct amba_device, dev)
  20. #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
  21. static struct amba_id *
  22. amba_lookup(struct amba_id *table, struct amba_device *dev)
  23. {
  24. int ret = 0;
  25. while (table->mask) {
  26. ret = (dev->periphid & table->mask) == table->id;
  27. if (ret)
  28. break;
  29. table++;
  30. }
  31. return ret ? table : NULL;
  32. }
  33. static int amba_match(struct device *dev, struct device_driver *drv)
  34. {
  35. struct amba_device *pcdev = to_amba_device(dev);
  36. struct amba_driver *pcdrv = to_amba_driver(drv);
  37. return amba_lookup(pcdrv->id_table, pcdev) != NULL;
  38. }
  39. #ifdef CONFIG_HOTPLUG
  40. static int amba_uevent(struct device *dev, struct kobj_uevent_env *env)
  41. {
  42. struct amba_device *pcdev = to_amba_device(dev);
  43. int retval = 0;
  44. retval = add_uevent_var(env, "AMBA_ID=%08x", pcdev->periphid);
  45. return retval;
  46. }
  47. #else
  48. #define amba_uevent NULL
  49. #endif
  50. static int amba_suspend(struct device *dev, pm_message_t state)
  51. {
  52. struct amba_driver *drv = to_amba_driver(dev->driver);
  53. int ret = 0;
  54. if (dev->driver && drv->suspend)
  55. ret = drv->suspend(to_amba_device(dev), state);
  56. return ret;
  57. }
  58. static int amba_resume(struct device *dev)
  59. {
  60. struct amba_driver *drv = to_amba_driver(dev->driver);
  61. int ret = 0;
  62. if (dev->driver && drv->resume)
  63. ret = drv->resume(to_amba_device(dev));
  64. return ret;
  65. }
  66. #define amba_attr_func(name,fmt,arg...) \
  67. static ssize_t name##_show(struct device *_dev, \
  68. struct device_attribute *attr, char *buf) \
  69. { \
  70. struct amba_device *dev = to_amba_device(_dev); \
  71. return sprintf(buf, fmt, arg); \
  72. }
  73. #define amba_attr(name,fmt,arg...) \
  74. amba_attr_func(name,fmt,arg) \
  75. static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL)
  76. amba_attr_func(id, "%08x\n", dev->periphid);
  77. amba_attr(irq0, "%u\n", dev->irq[0]);
  78. amba_attr(irq1, "%u\n", dev->irq[1]);
  79. amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",
  80. (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
  81. dev->res.flags);
  82. static struct device_attribute amba_dev_attrs[] = {
  83. __ATTR_RO(id),
  84. __ATTR_RO(resource),
  85. __ATTR_NULL,
  86. };
  87. /*
  88. * Primecells are part of the Advanced Microcontroller Bus Architecture,
  89. * so we call the bus "amba".
  90. */
  91. static struct bus_type amba_bustype = {
  92. .name = "amba",
  93. .dev_attrs = amba_dev_attrs,
  94. .match = amba_match,
  95. .uevent = amba_uevent,
  96. .suspend = amba_suspend,
  97. .resume = amba_resume,
  98. };
  99. static int __init amba_init(void)
  100. {
  101. return bus_register(&amba_bustype);
  102. }
  103. postcore_initcall(amba_init);
  104. static int amba_get_enable_pclk(struct amba_device *pcdev)
  105. {
  106. struct clk *pclk = clk_get(&pcdev->dev, "apb_pclk");
  107. int ret;
  108. pcdev->pclk = pclk;
  109. if (IS_ERR(pclk))
  110. return PTR_ERR(pclk);
  111. ret = clk_enable(pclk);
  112. if (ret)
  113. clk_put(pclk);
  114. return ret;
  115. }
  116. static void amba_put_disable_pclk(struct amba_device *pcdev)
  117. {
  118. struct clk *pclk = pcdev->pclk;
  119. clk_disable(pclk);
  120. clk_put(pclk);
  121. }
  122. static int amba_get_enable_vcore(struct amba_device *pcdev)
  123. {
  124. struct regulator *vcore = regulator_get(&pcdev->dev, "vcore");
  125. int ret;
  126. pcdev->vcore = vcore;
  127. if (IS_ERR(vcore)) {
  128. /* It is OK not to supply a vcore regulator */
  129. if (PTR_ERR(vcore) == -ENODEV)
  130. return 0;
  131. return PTR_ERR(vcore);
  132. }
  133. ret = regulator_enable(vcore);
  134. if (ret) {
  135. regulator_put(vcore);
  136. pcdev->vcore = ERR_PTR(-ENODEV);
  137. }
  138. return ret;
  139. }
  140. static void amba_put_disable_vcore(struct amba_device *pcdev)
  141. {
  142. struct regulator *vcore = pcdev->vcore;
  143. if (!IS_ERR(vcore)) {
  144. regulator_disable(vcore);
  145. regulator_put(vcore);
  146. }
  147. }
  148. /*
  149. * These are the device model conversion veneers; they convert the
  150. * device model structures to our more specific structures.
  151. */
  152. static int amba_probe(struct device *dev)
  153. {
  154. struct amba_device *pcdev = to_amba_device(dev);
  155. struct amba_driver *pcdrv = to_amba_driver(dev->driver);
  156. struct amba_id *id = amba_lookup(pcdrv->id_table, pcdev);
  157. int ret;
  158. do {
  159. ret = amba_get_enable_vcore(pcdev);
  160. if (ret)
  161. break;
  162. ret = amba_get_enable_pclk(pcdev);
  163. if (ret)
  164. break;
  165. ret = pcdrv->probe(pcdev, id);
  166. if (ret == 0)
  167. break;
  168. amba_put_disable_pclk(pcdev);
  169. amba_put_disable_vcore(pcdev);
  170. } while (0);
  171. return ret;
  172. }
  173. static int amba_remove(struct device *dev)
  174. {
  175. struct amba_device *pcdev = to_amba_device(dev);
  176. struct amba_driver *drv = to_amba_driver(dev->driver);
  177. int ret = drv->remove(pcdev);
  178. amba_put_disable_pclk(pcdev);
  179. amba_put_disable_vcore(pcdev);
  180. return ret;
  181. }
  182. static void amba_shutdown(struct device *dev)
  183. {
  184. struct amba_driver *drv = to_amba_driver(dev->driver);
  185. drv->shutdown(to_amba_device(dev));
  186. }
  187. /**
  188. * amba_driver_register - register an AMBA device driver
  189. * @drv: amba device driver structure
  190. *
  191. * Register an AMBA device driver with the Linux device model
  192. * core. If devices pre-exist, the drivers probe function will
  193. * be called.
  194. */
  195. int amba_driver_register(struct amba_driver *drv)
  196. {
  197. drv->drv.bus = &amba_bustype;
  198. #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
  199. SETFN(probe);
  200. SETFN(remove);
  201. SETFN(shutdown);
  202. return driver_register(&drv->drv);
  203. }
  204. /**
  205. * amba_driver_unregister - remove an AMBA device driver
  206. * @drv: AMBA device driver structure to remove
  207. *
  208. * Unregister an AMBA device driver from the Linux device
  209. * model. The device model will call the drivers remove function
  210. * for each device the device driver is currently handling.
  211. */
  212. void amba_driver_unregister(struct amba_driver *drv)
  213. {
  214. driver_unregister(&drv->drv);
  215. }
  216. static void amba_device_release(struct device *dev)
  217. {
  218. struct amba_device *d = to_amba_device(dev);
  219. if (d->res.parent)
  220. release_resource(&d->res);
  221. kfree(d);
  222. }
  223. /**
  224. * amba_device_register - register an AMBA device
  225. * @dev: AMBA device to register
  226. * @parent: parent memory resource
  227. *
  228. * Setup the AMBA device, reading the cell ID if present.
  229. * Claim the resource, and register the AMBA device with
  230. * the Linux device manager.
  231. */
  232. int amba_device_register(struct amba_device *dev, struct resource *parent)
  233. {
  234. u32 size;
  235. void __iomem *tmp;
  236. int i, ret;
  237. device_initialize(&dev->dev);
  238. /*
  239. * Copy from device_add
  240. */
  241. if (dev->dev.init_name) {
  242. dev_set_name(&dev->dev, "%s", dev->dev.init_name);
  243. dev->dev.init_name = NULL;
  244. }
  245. dev->dev.release = amba_device_release;
  246. dev->dev.bus = &amba_bustype;
  247. dev->dev.dma_mask = &dev->dma_mask;
  248. dev->res.name = dev_name(&dev->dev);
  249. if (!dev->dev.coherent_dma_mask && dev->dma_mask)
  250. dev_warn(&dev->dev, "coherent dma mask is unset\n");
  251. ret = request_resource(parent, &dev->res);
  252. if (ret)
  253. goto err_out;
  254. /*
  255. * Dynamically calculate the size of the resource
  256. * and use this for iomap
  257. */
  258. size = resource_size(&dev->res);
  259. tmp = ioremap(dev->res.start, size);
  260. if (!tmp) {
  261. ret = -ENOMEM;
  262. goto err_release;
  263. }
  264. ret = amba_get_enable_pclk(dev);
  265. if (ret == 0) {
  266. u32 pid, cid;
  267. /*
  268. * Read pid and cid based on size of resource
  269. * they are located at end of region
  270. */
  271. for (pid = 0, i = 0; i < 4; i++)
  272. pid |= (readl(tmp + size - 0x20 + 4 * i) & 255) <<
  273. (i * 8);
  274. for (cid = 0, i = 0; i < 4; i++)
  275. cid |= (readl(tmp + size - 0x10 + 4 * i) & 255) <<
  276. (i * 8);
  277. amba_put_disable_pclk(dev);
  278. if (cid == AMBA_CID)
  279. dev->periphid = pid;
  280. if (!dev->periphid)
  281. ret = -ENODEV;
  282. }
  283. iounmap(tmp);
  284. if (ret)
  285. goto err_release;
  286. ret = device_add(&dev->dev);
  287. if (ret)
  288. goto err_release;
  289. if (dev->irq[0] != NO_IRQ)
  290. ret = device_create_file(&dev->dev, &dev_attr_irq0);
  291. if (ret == 0 && dev->irq[1] != NO_IRQ)
  292. ret = device_create_file(&dev->dev, &dev_attr_irq1);
  293. if (ret == 0)
  294. return ret;
  295. device_unregister(&dev->dev);
  296. err_release:
  297. release_resource(&dev->res);
  298. err_out:
  299. return ret;
  300. }
  301. /**
  302. * amba_device_unregister - unregister an AMBA device
  303. * @dev: AMBA device to remove
  304. *
  305. * Remove the specified AMBA device from the Linux device
  306. * manager. All files associated with this object will be
  307. * destroyed, and device drivers notified that the device has
  308. * been removed. The AMBA device's resources including
  309. * the amba_device structure will be freed once all
  310. * references to it have been dropped.
  311. */
  312. void amba_device_unregister(struct amba_device *dev)
  313. {
  314. device_unregister(&dev->dev);
  315. }
  316. struct find_data {
  317. struct amba_device *dev;
  318. struct device *parent;
  319. const char *busid;
  320. unsigned int id;
  321. unsigned int mask;
  322. };
  323. static int amba_find_match(struct device *dev, void *data)
  324. {
  325. struct find_data *d = data;
  326. struct amba_device *pcdev = to_amba_device(dev);
  327. int r;
  328. r = (pcdev->periphid & d->mask) == d->id;
  329. if (d->parent)
  330. r &= d->parent == dev->parent;
  331. if (d->busid)
  332. r &= strcmp(dev_name(dev), d->busid) == 0;
  333. if (r) {
  334. get_device(dev);
  335. d->dev = pcdev;
  336. }
  337. return r;
  338. }
  339. /**
  340. * amba_find_device - locate an AMBA device given a bus id
  341. * @busid: bus id for device (or NULL)
  342. * @parent: parent device (or NULL)
  343. * @id: peripheral ID (or 0)
  344. * @mask: peripheral ID mask (or 0)
  345. *
  346. * Return the AMBA device corresponding to the supplied parameters.
  347. * If no device matches, returns NULL.
  348. *
  349. * NOTE: When a valid device is found, its refcount is
  350. * incremented, and must be decremented before the returned
  351. * reference.
  352. */
  353. struct amba_device *
  354. amba_find_device(const char *busid, struct device *parent, unsigned int id,
  355. unsigned int mask)
  356. {
  357. struct find_data data;
  358. data.dev = NULL;
  359. data.parent = parent;
  360. data.busid = busid;
  361. data.id = id;
  362. data.mask = mask;
  363. bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
  364. return data.dev;
  365. }
  366. /**
  367. * amba_request_regions - request all mem regions associated with device
  368. * @dev: amba_device structure for device
  369. * @name: name, or NULL to use driver name
  370. */
  371. int amba_request_regions(struct amba_device *dev, const char *name)
  372. {
  373. int ret = 0;
  374. u32 size;
  375. if (!name)
  376. name = dev->dev.driver->name;
  377. size = resource_size(&dev->res);
  378. if (!request_mem_region(dev->res.start, size, name))
  379. ret = -EBUSY;
  380. return ret;
  381. }
  382. /**
  383. * amba_release_regions - release mem regions assoicated with device
  384. * @dev: amba_device structure for device
  385. *
  386. * Release regions claimed by a successful call to amba_request_regions.
  387. */
  388. void amba_release_regions(struct amba_device *dev)
  389. {
  390. u32 size;
  391. size = resource_size(&dev->res);
  392. release_mem_region(dev->res.start, size);
  393. }
  394. EXPORT_SYMBOL(amba_driver_register);
  395. EXPORT_SYMBOL(amba_driver_unregister);
  396. EXPORT_SYMBOL(amba_device_register);
  397. EXPORT_SYMBOL(amba_device_unregister);
  398. EXPORT_SYMBOL(amba_find_device);
  399. EXPORT_SYMBOL(amba_request_regions);
  400. EXPORT_SYMBOL(amba_release_regions);