main.c 7.8 KB

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