main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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) {
  132. pr_err("Failed to get SPROM: %d\n", err);
  133. return -ENOENT;
  134. }
  135. /* Register found cores */
  136. bcma_register_cores(bus);
  137. pr_info("Bus registered\n");
  138. return 0;
  139. }
  140. void bcma_bus_unregister(struct bcma_bus *bus)
  141. {
  142. bcma_unregister_cores(bus);
  143. }
  144. int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
  145. {
  146. drv->drv.name = drv->name;
  147. drv->drv.bus = &bcma_bus_type;
  148. drv->drv.owner = owner;
  149. return driver_register(&drv->drv);
  150. }
  151. EXPORT_SYMBOL_GPL(__bcma_driver_register);
  152. void bcma_driver_unregister(struct bcma_driver *drv)
  153. {
  154. driver_unregister(&drv->drv);
  155. }
  156. EXPORT_SYMBOL_GPL(bcma_driver_unregister);
  157. static int bcma_bus_match(struct device *dev, struct device_driver *drv)
  158. {
  159. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  160. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  161. const struct bcma_device_id *cid = &core->id;
  162. const struct bcma_device_id *did;
  163. for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
  164. if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
  165. (did->id == cid->id || did->id == BCMA_ANY_ID) &&
  166. (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
  167. (did->class == cid->class || did->class == BCMA_ANY_CLASS))
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. static int bcma_device_probe(struct device *dev)
  173. {
  174. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  175. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  176. drv);
  177. int err = 0;
  178. if (adrv->probe)
  179. err = adrv->probe(core);
  180. return err;
  181. }
  182. static int bcma_device_remove(struct device *dev)
  183. {
  184. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  185. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  186. drv);
  187. if (adrv->remove)
  188. adrv->remove(core);
  189. return 0;
  190. }
  191. static int __init bcma_modinit(void)
  192. {
  193. int err;
  194. err = bus_register(&bcma_bus_type);
  195. if (err)
  196. return err;
  197. #ifdef CONFIG_BCMA_HOST_PCI
  198. err = bcma_host_pci_init();
  199. if (err) {
  200. pr_err("PCI host initialization failed\n");
  201. err = 0;
  202. }
  203. #endif
  204. return err;
  205. }
  206. fs_initcall(bcma_modinit);
  207. static void __exit bcma_modexit(void)
  208. {
  209. #ifdef CONFIG_BCMA_HOST_PCI
  210. bcma_host_pci_exit();
  211. #endif
  212. bus_unregister(&bcma_bus_type);
  213. }
  214. module_exit(bcma_modexit)