main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Broadcom specific AMBA
  3. * Bus subsystem
  4. *
  5. * Licensed under the GNU/GPL. See COPYING for details.
  6. */
  7. #include "bcma_private.h"
  8. #include <linux/bcma/bcma.h>
  9. #include <linux/slab.h>
  10. MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
  11. MODULE_LICENSE("GPL");
  12. static int bcma_bus_match(struct device *dev, struct device_driver *drv);
  13. static int bcma_device_probe(struct device *dev);
  14. static int bcma_device_remove(struct device *dev);
  15. static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
  16. static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
  17. {
  18. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  19. return sprintf(buf, "0x%03X\n", core->id.manuf);
  20. }
  21. static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
  22. {
  23. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  24. return sprintf(buf, "0x%03X\n", core->id.id);
  25. }
  26. static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
  27. {
  28. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  29. return sprintf(buf, "0x%02X\n", core->id.rev);
  30. }
  31. static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
  32. {
  33. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  34. return sprintf(buf, "0x%X\n", core->id.class);
  35. }
  36. static struct device_attribute bcma_device_attrs[] = {
  37. __ATTR_RO(manuf),
  38. __ATTR_RO(id),
  39. __ATTR_RO(rev),
  40. __ATTR_RO(class),
  41. __ATTR_NULL,
  42. };
  43. static struct bus_type bcma_bus_type = {
  44. .name = "bcma",
  45. .match = bcma_bus_match,
  46. .probe = bcma_device_probe,
  47. .remove = bcma_device_remove,
  48. .uevent = bcma_device_uevent,
  49. .dev_attrs = bcma_device_attrs,
  50. };
  51. static struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
  52. {
  53. struct bcma_device *core;
  54. list_for_each_entry(core, &bus->cores, list) {
  55. if (core->id.id == coreid)
  56. return core;
  57. }
  58. return NULL;
  59. }
  60. static void bcma_release_core_dev(struct device *dev)
  61. {
  62. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  63. kfree(core);
  64. }
  65. static int bcma_register_cores(struct bcma_bus *bus)
  66. {
  67. struct bcma_device *core;
  68. int err, dev_id = 0;
  69. list_for_each_entry(core, &bus->cores, list) {
  70. /* We support that cores ourself */
  71. switch (core->id.id) {
  72. case BCMA_CORE_CHIPCOMMON:
  73. case BCMA_CORE_PCI:
  74. case BCMA_CORE_PCIE:
  75. continue;
  76. }
  77. core->dev.release = bcma_release_core_dev;
  78. core->dev.bus = &bcma_bus_type;
  79. dev_set_name(&core->dev, "bcma%d:%d", 0/*bus->num*/, dev_id);
  80. switch (bus->hosttype) {
  81. case BCMA_HOSTTYPE_PCI:
  82. core->dev.parent = &bus->host_pci->dev;
  83. core->dma_dev = &bus->host_pci->dev;
  84. core->irq = bus->host_pci->irq;
  85. break;
  86. case BCMA_HOSTTYPE_NONE:
  87. case BCMA_HOSTTYPE_SDIO:
  88. break;
  89. }
  90. err = device_register(&core->dev);
  91. if (err) {
  92. pr_err("Could not register dev for core 0x%03X\n",
  93. core->id.id);
  94. continue;
  95. }
  96. core->dev_registered = true;
  97. dev_id++;
  98. }
  99. return 0;
  100. }
  101. static void bcma_unregister_cores(struct bcma_bus *bus)
  102. {
  103. struct bcma_device *core;
  104. list_for_each_entry(core, &bus->cores, list) {
  105. if (core->dev_registered)
  106. device_unregister(&core->dev);
  107. }
  108. }
  109. int bcma_bus_register(struct bcma_bus *bus)
  110. {
  111. int err;
  112. struct bcma_device *core;
  113. /* Scan for devices (cores) */
  114. err = bcma_bus_scan(bus);
  115. if (err) {
  116. pr_err("Failed to scan: %d\n", err);
  117. return -1;
  118. }
  119. /* Init CC core */
  120. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  121. if (core) {
  122. bus->drv_cc.core = core;
  123. bcma_core_chipcommon_init(&bus->drv_cc);
  124. }
  125. /* Init PCIE core */
  126. core = bcma_find_core(bus, BCMA_CORE_PCIE);
  127. if (core) {
  128. bus->drv_pci.core = core;
  129. bcma_core_pci_init(&bus->drv_pci);
  130. }
  131. /* Try to get SPROM */
  132. err = bcma_sprom_get(bus);
  133. if (err == -ENOENT) {
  134. pr_err("No SPROM available\n");
  135. } else if (err) {
  136. pr_err("Failed to get SPROM: %d\n", err);
  137. return -ENOENT;
  138. }
  139. /* Register found cores */
  140. bcma_register_cores(bus);
  141. pr_info("Bus registered\n");
  142. return 0;
  143. }
  144. void bcma_bus_unregister(struct bcma_bus *bus)
  145. {
  146. bcma_unregister_cores(bus);
  147. }
  148. int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
  149. {
  150. drv->drv.name = drv->name;
  151. drv->drv.bus = &bcma_bus_type;
  152. drv->drv.owner = owner;
  153. return driver_register(&drv->drv);
  154. }
  155. EXPORT_SYMBOL_GPL(__bcma_driver_register);
  156. void bcma_driver_unregister(struct bcma_driver *drv)
  157. {
  158. driver_unregister(&drv->drv);
  159. }
  160. EXPORT_SYMBOL_GPL(bcma_driver_unregister);
  161. static int bcma_bus_match(struct device *dev, struct device_driver *drv)
  162. {
  163. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  164. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  165. const struct bcma_device_id *cid = &core->id;
  166. const struct bcma_device_id *did;
  167. for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
  168. if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
  169. (did->id == cid->id || did->id == BCMA_ANY_ID) &&
  170. (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
  171. (did->class == cid->class || did->class == BCMA_ANY_CLASS))
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. static int bcma_device_probe(struct device *dev)
  177. {
  178. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  179. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  180. drv);
  181. int err = 0;
  182. if (adrv->probe)
  183. err = adrv->probe(core);
  184. return err;
  185. }
  186. static int bcma_device_remove(struct device *dev)
  187. {
  188. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  189. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  190. drv);
  191. if (adrv->remove)
  192. adrv->remove(core);
  193. return 0;
  194. }
  195. static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  196. {
  197. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  198. return add_uevent_var(env,
  199. "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
  200. core->id.manuf, core->id.id,
  201. core->id.rev, core->id.class);
  202. }
  203. static int __init bcma_modinit(void)
  204. {
  205. int err;
  206. err = bus_register(&bcma_bus_type);
  207. if (err)
  208. return err;
  209. #ifdef CONFIG_BCMA_HOST_PCI
  210. err = bcma_host_pci_init();
  211. if (err) {
  212. pr_err("PCI host initialization failed\n");
  213. err = 0;
  214. }
  215. #endif
  216. return err;
  217. }
  218. fs_initcall(bcma_modinit);
  219. static void __exit bcma_modexit(void)
  220. {
  221. #ifdef CONFIG_BCMA_HOST_PCI
  222. bcma_host_pci_exit();
  223. #endif
  224. bus_unregister(&bcma_bus_type);
  225. }
  226. module_exit(bcma_modexit)