main.c 7.8 KB

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