main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. break;
  81. case BCMA_HOSTTYPE_NONE:
  82. case BCMA_HOSTTYPE_SDIO:
  83. break;
  84. }
  85. err = device_register(&core->dev);
  86. if (err) {
  87. pr_err("Could not register dev for core 0x%03X\n",
  88. core->id.id);
  89. continue;
  90. }
  91. core->dev_registered = true;
  92. dev_id++;
  93. }
  94. return 0;
  95. }
  96. static void bcma_unregister_cores(struct bcma_bus *bus)
  97. {
  98. struct bcma_device *core;
  99. list_for_each_entry(core, &bus->cores, list) {
  100. if (core->dev_registered)
  101. device_unregister(&core->dev);
  102. }
  103. }
  104. int bcma_bus_register(struct bcma_bus *bus)
  105. {
  106. int err;
  107. struct bcma_device *core;
  108. /* Scan for devices (cores) */
  109. err = bcma_bus_scan(bus);
  110. if (err) {
  111. pr_err("Failed to scan: %d\n", err);
  112. return -1;
  113. }
  114. /* Init CC core */
  115. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  116. if (core) {
  117. bus->drv_cc.core = core;
  118. bcma_core_chipcommon_init(&bus->drv_cc);
  119. }
  120. /* Init PCIE core */
  121. core = bcma_find_core(bus, BCMA_CORE_PCIE);
  122. if (core) {
  123. bus->drv_pci.core = core;
  124. bcma_core_pci_init(&bus->drv_pci);
  125. }
  126. /* Register found cores */
  127. bcma_register_cores(bus);
  128. pr_info("Bus registered\n");
  129. return 0;
  130. }
  131. EXPORT_SYMBOL_GPL(bcma_bus_register);
  132. void bcma_bus_unregister(struct bcma_bus *bus)
  133. {
  134. bcma_unregister_cores(bus);
  135. }
  136. EXPORT_SYMBOL_GPL(bcma_bus_unregister);
  137. int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
  138. {
  139. drv->drv.name = drv->name;
  140. drv->drv.bus = &bcma_bus_type;
  141. drv->drv.owner = owner;
  142. return driver_register(&drv->drv);
  143. }
  144. EXPORT_SYMBOL_GPL(__bcma_driver_register);
  145. void bcma_driver_unregister(struct bcma_driver *drv)
  146. {
  147. driver_unregister(&drv->drv);
  148. }
  149. EXPORT_SYMBOL_GPL(bcma_driver_unregister);
  150. static int bcma_bus_match(struct device *dev, struct device_driver *drv)
  151. {
  152. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  153. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  154. const struct bcma_device_id *cid = &core->id;
  155. const struct bcma_device_id *did;
  156. for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
  157. if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
  158. (did->id == cid->id || did->id == BCMA_ANY_ID) &&
  159. (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
  160. (did->class == cid->class || did->class == BCMA_ANY_CLASS))
  161. return 1;
  162. }
  163. return 0;
  164. }
  165. static int bcma_device_probe(struct device *dev)
  166. {
  167. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  168. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  169. drv);
  170. int err = 0;
  171. if (adrv->probe)
  172. err = adrv->probe(core);
  173. return err;
  174. }
  175. static int bcma_device_remove(struct device *dev)
  176. {
  177. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  178. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  179. drv);
  180. if (adrv->remove)
  181. adrv->remove(core);
  182. return 0;
  183. }
  184. static int __init bcma_modinit(void)
  185. {
  186. int err;
  187. err = bus_register(&bcma_bus_type);
  188. if (err)
  189. return err;
  190. #ifdef CONFIG_BCMA_HOST_PCI
  191. err = bcma_host_pci_init();
  192. if (err) {
  193. pr_err("PCI host initialization failed\n");
  194. err = 0;
  195. }
  196. #endif
  197. return err;
  198. }
  199. fs_initcall(bcma_modinit);
  200. static void __exit bcma_modexit(void)
  201. {
  202. #ifdef CONFIG_BCMA_HOST_PCI
  203. bcma_host_pci_exit();
  204. #endif
  205. bus_unregister(&bcma_bus_type);
  206. }
  207. module_exit(bcma_modexit)