bus.c 8.7 KB

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