bus.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/amba/bus.h>
  16. #include <asm/io.h>
  17. #include <asm/sizes.h>
  18. #define to_amba_device(d) container_of(d, struct amba_device, dev)
  19. #define to_amba_driver(d) container_of(d, struct amba_driver, drv)
  20. static struct amba_id *
  21. amba_lookup(struct amba_id *table, struct amba_device *dev)
  22. {
  23. int ret = 0;
  24. while (table->mask) {
  25. ret = (dev->periphid & table->mask) == table->id;
  26. if (ret)
  27. break;
  28. table++;
  29. }
  30. return ret ? table : NULL;
  31. }
  32. static int amba_match(struct device *dev, struct device_driver *drv)
  33. {
  34. struct amba_device *pcdev = to_amba_device(dev);
  35. struct amba_driver *pcdrv = to_amba_driver(drv);
  36. return amba_lookup(pcdrv->id_table, pcdev) != NULL;
  37. }
  38. #ifdef CONFIG_HOTPLUG
  39. static int amba_uevent(struct device *dev, char **envp, int nr_env, char *buf, int bufsz)
  40. {
  41. struct amba_device *pcdev = to_amba_device(dev);
  42. if (nr_env < 2)
  43. return -ENOMEM;
  44. snprintf(buf, bufsz, "AMBA_ID=%08x", pcdev->periphid);
  45. *envp++ = buf;
  46. *envp++ = NULL;
  47. return 0;
  48. }
  49. #else
  50. #define amba_uevent NULL
  51. #endif
  52. static int amba_suspend(struct device *dev, pm_message_t state)
  53. {
  54. struct amba_driver *drv = to_amba_driver(dev->driver);
  55. int ret = 0;
  56. if (dev->driver && drv->suspend)
  57. ret = drv->suspend(to_amba_device(dev), state);
  58. return ret;
  59. }
  60. static int amba_resume(struct device *dev)
  61. {
  62. struct amba_driver *drv = to_amba_driver(dev->driver);
  63. int ret = 0;
  64. if (dev->driver && drv->resume)
  65. ret = drv->resume(to_amba_device(dev));
  66. return ret;
  67. }
  68. /*
  69. * Primecells are part of the Advanced Microcontroller Bus Architecture,
  70. * so we call the bus "amba".
  71. */
  72. static struct bus_type amba_bustype = {
  73. .name = "amba",
  74. .match = amba_match,
  75. .uevent = amba_uevent,
  76. .suspend = amba_suspend,
  77. .resume = amba_resume,
  78. };
  79. static int __init amba_init(void)
  80. {
  81. return bus_register(&amba_bustype);
  82. }
  83. postcore_initcall(amba_init);
  84. /*
  85. * These are the device model conversion veneers; they convert the
  86. * device model structures to our more specific structures.
  87. */
  88. static int amba_probe(struct device *dev)
  89. {
  90. struct amba_device *pcdev = to_amba_device(dev);
  91. struct amba_driver *pcdrv = to_amba_driver(dev->driver);
  92. struct amba_id *id;
  93. id = amba_lookup(pcdrv->id_table, pcdev);
  94. return pcdrv->probe(pcdev, id);
  95. }
  96. static int amba_remove(struct device *dev)
  97. {
  98. struct amba_driver *drv = to_amba_driver(dev->driver);
  99. return drv->remove(to_amba_device(dev));
  100. }
  101. static void amba_shutdown(struct device *dev)
  102. {
  103. struct amba_driver *drv = to_amba_driver(dev->driver);
  104. drv->shutdown(to_amba_device(dev));
  105. }
  106. /**
  107. * amba_driver_register - register an AMBA device driver
  108. * @drv: amba device driver structure
  109. *
  110. * Register an AMBA device driver with the Linux device model
  111. * core. If devices pre-exist, the drivers probe function will
  112. * be called.
  113. */
  114. int amba_driver_register(struct amba_driver *drv)
  115. {
  116. drv->drv.bus = &amba_bustype;
  117. #define SETFN(fn) if (drv->fn) drv->drv.fn = amba_##fn
  118. SETFN(probe);
  119. SETFN(remove);
  120. SETFN(shutdown);
  121. return driver_register(&drv->drv);
  122. }
  123. /**
  124. * amba_driver_unregister - remove an AMBA device driver
  125. * @drv: AMBA device driver structure to remove
  126. *
  127. * Unregister an AMBA device driver from the Linux device
  128. * model. The device model will call the drivers remove function
  129. * for each device the device driver is currently handling.
  130. */
  131. void amba_driver_unregister(struct amba_driver *drv)
  132. {
  133. driver_unregister(&drv->drv);
  134. }
  135. static void amba_device_release(struct device *dev)
  136. {
  137. struct amba_device *d = to_amba_device(dev);
  138. if (d->res.parent)
  139. release_resource(&d->res);
  140. kfree(d);
  141. }
  142. #define amba_attr(name,fmt,arg...) \
  143. static ssize_t show_##name(struct device *_dev, struct device_attribute *attr, char *buf) \
  144. { \
  145. struct amba_device *dev = to_amba_device(_dev); \
  146. return sprintf(buf, fmt, arg); \
  147. } \
  148. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  149. amba_attr(id, "%08x\n", dev->periphid);
  150. amba_attr(irq0, "%u\n", dev->irq[0]);
  151. amba_attr(irq1, "%u\n", dev->irq[1]);
  152. amba_attr(resource, "\t%016llx\t%016llx\t%016lx\n",
  153. (unsigned long long)dev->res.start, (unsigned long long)dev->res.end,
  154. dev->res.flags);
  155. /**
  156. * amba_device_register - register an AMBA device
  157. * @dev: AMBA device to register
  158. * @parent: parent memory resource
  159. *
  160. * Setup the AMBA device, reading the cell ID if present.
  161. * Claim the resource, and register the AMBA device with
  162. * the Linux device manager.
  163. */
  164. int amba_device_register(struct amba_device *dev, struct resource *parent)
  165. {
  166. u32 pid, cid;
  167. void __iomem *tmp;
  168. int i, ret;
  169. dev->dev.release = amba_device_release;
  170. dev->dev.bus = &amba_bustype;
  171. dev->dev.dma_mask = &dev->dma_mask;
  172. dev->res.name = dev->dev.bus_id;
  173. if (!dev->dev.coherent_dma_mask && dev->dma_mask)
  174. dev_warn(&dev->dev, "coherent dma mask is unset\n");
  175. ret = request_resource(parent, &dev->res);
  176. if (ret == 0) {
  177. tmp = ioremap(dev->res.start, SZ_4K);
  178. if (!tmp) {
  179. ret = -ENOMEM;
  180. goto out;
  181. }
  182. for (pid = 0, i = 0; i < 4; i++)
  183. pid |= (readl(tmp + 0xfe0 + 4 * i) & 255) << (i * 8);
  184. for (cid = 0, i = 0; i < 4; i++)
  185. cid |= (readl(tmp + 0xff0 + 4 * i) & 255) << (i * 8);
  186. iounmap(tmp);
  187. if (cid == 0xb105f00d)
  188. dev->periphid = pid;
  189. if (dev->periphid)
  190. ret = device_register(&dev->dev);
  191. else
  192. ret = -ENODEV;
  193. if (ret == 0) {
  194. device_create_file(&dev->dev, &dev_attr_id);
  195. if (dev->irq[0] != NO_IRQ)
  196. device_create_file(&dev->dev, &dev_attr_irq0);
  197. if (dev->irq[1] != NO_IRQ)
  198. device_create_file(&dev->dev, &dev_attr_irq1);
  199. device_create_file(&dev->dev, &dev_attr_resource);
  200. } else {
  201. out:
  202. release_resource(&dev->res);
  203. }
  204. }
  205. return ret;
  206. }
  207. /**
  208. * amba_device_unregister - unregister an AMBA device
  209. * @dev: AMBA device to remove
  210. *
  211. * Remove the specified AMBA device from the Linux device
  212. * manager. All files associated with this object will be
  213. * destroyed, and device drivers notified that the device has
  214. * been removed. The AMBA device's resources including
  215. * the amba_device structure will be freed once all
  216. * references to it have been dropped.
  217. */
  218. void amba_device_unregister(struct amba_device *dev)
  219. {
  220. device_unregister(&dev->dev);
  221. }
  222. struct find_data {
  223. struct amba_device *dev;
  224. struct device *parent;
  225. const char *busid;
  226. unsigned int id;
  227. unsigned int mask;
  228. };
  229. static int amba_find_match(struct device *dev, void *data)
  230. {
  231. struct find_data *d = data;
  232. struct amba_device *pcdev = to_amba_device(dev);
  233. int r;
  234. r = (pcdev->periphid & d->mask) == d->id;
  235. if (d->parent)
  236. r &= d->parent == dev->parent;
  237. if (d->busid)
  238. r &= strcmp(dev->bus_id, d->busid) == 0;
  239. if (r) {
  240. get_device(dev);
  241. d->dev = pcdev;
  242. }
  243. return r;
  244. }
  245. /**
  246. * amba_find_device - locate an AMBA device given a bus id
  247. * @busid: bus id for device (or NULL)
  248. * @parent: parent device (or NULL)
  249. * @id: peripheral ID (or 0)
  250. * @mask: peripheral ID mask (or 0)
  251. *
  252. * Return the AMBA device corresponding to the supplied parameters.
  253. * If no device matches, returns NULL.
  254. *
  255. * NOTE: When a valid device is found, its refcount is
  256. * incremented, and must be decremented before the returned
  257. * reference.
  258. */
  259. struct amba_device *
  260. amba_find_device(const char *busid, struct device *parent, unsigned int id,
  261. unsigned int mask)
  262. {
  263. struct find_data data;
  264. data.dev = NULL;
  265. data.parent = parent;
  266. data.busid = busid;
  267. data.id = id;
  268. data.mask = mask;
  269. bus_for_each_dev(&amba_bustype, NULL, &data, amba_find_match);
  270. return data.dev;
  271. }
  272. /**
  273. * amba_request_regions - request all mem regions associated with device
  274. * @dev: amba_device structure for device
  275. * @name: name, or NULL to use driver name
  276. */
  277. int amba_request_regions(struct amba_device *dev, const char *name)
  278. {
  279. int ret = 0;
  280. if (!name)
  281. name = dev->dev.driver->name;
  282. if (!request_mem_region(dev->res.start, SZ_4K, name))
  283. ret = -EBUSY;
  284. return ret;
  285. }
  286. /**
  287. * amba_release_regions - release mem regions assoicated with device
  288. * @dev: amba_device structure for device
  289. *
  290. * Release regions claimed by a successful call to amba_request_regions.
  291. */
  292. void amba_release_regions(struct amba_device *dev)
  293. {
  294. release_mem_region(dev->res.start, SZ_4K);
  295. }
  296. EXPORT_SYMBOL(amba_driver_register);
  297. EXPORT_SYMBOL(amba_driver_unregister);
  298. EXPORT_SYMBOL(amba_device_register);
  299. EXPORT_SYMBOL(amba_device_unregister);
  300. EXPORT_SYMBOL(amba_find_device);
  301. EXPORT_SYMBOL(amba_request_regions);
  302. EXPORT_SYMBOL(amba_release_regions);