main.c 5.7 KB

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