main.c 5.9 KB

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