main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. if (core->io_addr)
  62. iounmap(core->io_addr);
  63. if (core->io_wrap)
  64. iounmap(core->io_wrap);
  65. kfree(core);
  66. }
  67. static int bcma_register_cores(struct bcma_bus *bus)
  68. {
  69. struct bcma_device *core;
  70. int err, dev_id = 0;
  71. list_for_each_entry(core, &bus->cores, list) {
  72. /* We support that cores ourself */
  73. switch (core->id.id) {
  74. case BCMA_CORE_CHIPCOMMON:
  75. case BCMA_CORE_PCI:
  76. case BCMA_CORE_PCIE:
  77. case BCMA_CORE_MIPS_74K:
  78. continue;
  79. }
  80. core->dev.release = bcma_release_core_dev;
  81. core->dev.bus = &bcma_bus_type;
  82. dev_set_name(&core->dev, "bcma%d:%d", 0/*bus->num*/, dev_id);
  83. switch (bus->hosttype) {
  84. case BCMA_HOSTTYPE_PCI:
  85. core->dev.parent = &bus->host_pci->dev;
  86. core->dma_dev = &bus->host_pci->dev;
  87. core->irq = bus->host_pci->irq;
  88. break;
  89. case BCMA_HOSTTYPE_SOC:
  90. core->dev.dma_mask = &core->dev.coherent_dma_mask;
  91. core->dma_dev = &core->dev;
  92. break;
  93. case BCMA_HOSTTYPE_SDIO:
  94. break;
  95. }
  96. err = device_register(&core->dev);
  97. if (err) {
  98. pr_err("Could not register dev for core 0x%03X\n",
  99. core->id.id);
  100. continue;
  101. }
  102. core->dev_registered = true;
  103. dev_id++;
  104. }
  105. return 0;
  106. }
  107. static void bcma_unregister_cores(struct bcma_bus *bus)
  108. {
  109. struct bcma_device *core;
  110. list_for_each_entry(core, &bus->cores, list) {
  111. if (core->dev_registered)
  112. device_unregister(&core->dev);
  113. }
  114. }
  115. int bcma_bus_register(struct bcma_bus *bus)
  116. {
  117. int err;
  118. struct bcma_device *core;
  119. /* Scan for devices (cores) */
  120. err = bcma_bus_scan(bus);
  121. if (err) {
  122. pr_err("Failed to scan: %d\n", err);
  123. return -1;
  124. }
  125. /* Init CC core */
  126. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  127. if (core) {
  128. bus->drv_cc.core = core;
  129. bcma_core_chipcommon_init(&bus->drv_cc);
  130. }
  131. /* Init MIPS core */
  132. core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  133. if (core) {
  134. bus->drv_mips.core = core;
  135. bcma_core_mips_init(&bus->drv_mips);
  136. }
  137. /* Init PCIE core */
  138. core = bcma_find_core(bus, BCMA_CORE_PCIE);
  139. if (core) {
  140. bus->drv_pci.core = core;
  141. bcma_core_pci_init(&bus->drv_pci);
  142. }
  143. /* Try to get SPROM */
  144. err = bcma_sprom_get(bus);
  145. if (err == -ENOENT) {
  146. pr_err("No SPROM available\n");
  147. } else if (err) {
  148. pr_err("Failed to get SPROM: %d\n", err);
  149. return -ENOENT;
  150. }
  151. /* Register found cores */
  152. bcma_register_cores(bus);
  153. pr_info("Bus registered\n");
  154. return 0;
  155. }
  156. void bcma_bus_unregister(struct bcma_bus *bus)
  157. {
  158. bcma_unregister_cores(bus);
  159. }
  160. int __init bcma_bus_early_register(struct bcma_bus *bus,
  161. struct bcma_device *core_cc,
  162. struct bcma_device *core_mips)
  163. {
  164. int err;
  165. struct bcma_device *core;
  166. struct bcma_device_id match;
  167. bcma_init_bus(bus);
  168. match.manuf = BCMA_MANUF_BCM;
  169. match.id = BCMA_CORE_CHIPCOMMON;
  170. match.class = BCMA_CL_SIM;
  171. match.rev = BCMA_ANY_REV;
  172. /* Scan for chip common core */
  173. err = bcma_bus_scan_early(bus, &match, core_cc);
  174. if (err) {
  175. pr_err("Failed to scan for common core: %d\n", err);
  176. return -1;
  177. }
  178. match.manuf = BCMA_MANUF_MIPS;
  179. match.id = BCMA_CORE_MIPS_74K;
  180. match.class = BCMA_CL_SIM;
  181. match.rev = BCMA_ANY_REV;
  182. /* Scan for mips core */
  183. err = bcma_bus_scan_early(bus, &match, core_mips);
  184. if (err) {
  185. pr_err("Failed to scan for mips core: %d\n", err);
  186. return -1;
  187. }
  188. /* Init CC core */
  189. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  190. if (core) {
  191. bus->drv_cc.core = core;
  192. bcma_core_chipcommon_init(&bus->drv_cc);
  193. }
  194. /* Init MIPS core */
  195. core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  196. if (core) {
  197. bus->drv_mips.core = core;
  198. bcma_core_mips_init(&bus->drv_mips);
  199. }
  200. pr_info("Early bus registered\n");
  201. return 0;
  202. }
  203. int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
  204. {
  205. drv->drv.name = drv->name;
  206. drv->drv.bus = &bcma_bus_type;
  207. drv->drv.owner = owner;
  208. return driver_register(&drv->drv);
  209. }
  210. EXPORT_SYMBOL_GPL(__bcma_driver_register);
  211. void bcma_driver_unregister(struct bcma_driver *drv)
  212. {
  213. driver_unregister(&drv->drv);
  214. }
  215. EXPORT_SYMBOL_GPL(bcma_driver_unregister);
  216. static int bcma_bus_match(struct device *dev, struct device_driver *drv)
  217. {
  218. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  219. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  220. const struct bcma_device_id *cid = &core->id;
  221. const struct bcma_device_id *did;
  222. for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
  223. if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
  224. (did->id == cid->id || did->id == BCMA_ANY_ID) &&
  225. (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
  226. (did->class == cid->class || did->class == BCMA_ANY_CLASS))
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. static int bcma_device_probe(struct device *dev)
  232. {
  233. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  234. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  235. drv);
  236. int err = 0;
  237. if (adrv->probe)
  238. err = adrv->probe(core);
  239. return err;
  240. }
  241. static int bcma_device_remove(struct device *dev)
  242. {
  243. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  244. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  245. drv);
  246. if (adrv->remove)
  247. adrv->remove(core);
  248. return 0;
  249. }
  250. static int __init bcma_modinit(void)
  251. {
  252. int err;
  253. err = bus_register(&bcma_bus_type);
  254. if (err)
  255. return err;
  256. #ifdef CONFIG_BCMA_HOST_PCI
  257. err = bcma_host_pci_init();
  258. if (err) {
  259. pr_err("PCI host initialization failed\n");
  260. err = 0;
  261. }
  262. #endif
  263. return err;
  264. }
  265. fs_initcall(bcma_modinit);
  266. static void __exit bcma_modexit(void)
  267. {
  268. #ifdef CONFIG_BCMA_HOST_PCI
  269. bcma_host_pci_exit();
  270. #endif
  271. bus_unregister(&bcma_bus_type);
  272. }
  273. module_exit(bcma_modexit)