amba.c 8.5 KB

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