main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. /* Register found cores */
  153. bcma_register_cores(bus);
  154. pr_info("Bus registered\n");
  155. return 0;
  156. }
  157. void bcma_bus_unregister(struct bcma_bus *bus)
  158. {
  159. bcma_unregister_cores(bus);
  160. }
  161. int __init bcma_bus_early_register(struct bcma_bus *bus,
  162. struct bcma_device *core_cc,
  163. struct bcma_device *core_mips)
  164. {
  165. int err;
  166. struct bcma_device *core;
  167. struct bcma_device_id match;
  168. bcma_init_bus(bus);
  169. match.manuf = BCMA_MANUF_BCM;
  170. match.id = BCMA_CORE_CHIPCOMMON;
  171. match.class = BCMA_CL_SIM;
  172. match.rev = BCMA_ANY_REV;
  173. /* Scan for chip common core */
  174. err = bcma_bus_scan_early(bus, &match, core_cc);
  175. if (err) {
  176. pr_err("Failed to scan for common core: %d\n", err);
  177. return -1;
  178. }
  179. match.manuf = BCMA_MANUF_MIPS;
  180. match.id = BCMA_CORE_MIPS_74K;
  181. match.class = BCMA_CL_SIM;
  182. match.rev = BCMA_ANY_REV;
  183. /* Scan for mips core */
  184. err = bcma_bus_scan_early(bus, &match, core_mips);
  185. if (err) {
  186. pr_err("Failed to scan for mips core: %d\n", err);
  187. return -1;
  188. }
  189. /* Init CC core */
  190. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  191. if (core) {
  192. bus->drv_cc.core = core;
  193. bcma_core_chipcommon_init(&bus->drv_cc);
  194. }
  195. /* Init MIPS core */
  196. core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  197. if (core) {
  198. bus->drv_mips.core = core;
  199. bcma_core_mips_init(&bus->drv_mips);
  200. }
  201. pr_info("Early bus registered\n");
  202. return 0;
  203. }
  204. #ifdef CONFIG_PM
  205. int bcma_bus_suspend(struct bcma_bus *bus)
  206. {
  207. struct bcma_device *core;
  208. list_for_each_entry(core, &bus->cores, list) {
  209. struct device_driver *drv = core->dev.driver;
  210. if (drv) {
  211. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  212. if (adrv->suspend)
  213. adrv->suspend(core);
  214. }
  215. }
  216. return 0;
  217. }
  218. int bcma_bus_resume(struct bcma_bus *bus)
  219. {
  220. struct bcma_device *core;
  221. /* Init CC core */
  222. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  223. if (core) {
  224. bus->drv_cc.setup_done = false;
  225. bcma_core_chipcommon_init(&bus->drv_cc);
  226. }
  227. list_for_each_entry(core, &bus->cores, list) {
  228. struct device_driver *drv = core->dev.driver;
  229. if (drv) {
  230. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  231. if (adrv->resume)
  232. adrv->resume(core);
  233. }
  234. }
  235. return 0;
  236. }
  237. #endif
  238. int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
  239. {
  240. drv->drv.name = drv->name;
  241. drv->drv.bus = &bcma_bus_type;
  242. drv->drv.owner = owner;
  243. return driver_register(&drv->drv);
  244. }
  245. EXPORT_SYMBOL_GPL(__bcma_driver_register);
  246. void bcma_driver_unregister(struct bcma_driver *drv)
  247. {
  248. driver_unregister(&drv->drv);
  249. }
  250. EXPORT_SYMBOL_GPL(bcma_driver_unregister);
  251. static int bcma_bus_match(struct device *dev, struct device_driver *drv)
  252. {
  253. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  254. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  255. const struct bcma_device_id *cid = &core->id;
  256. const struct bcma_device_id *did;
  257. for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
  258. if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
  259. (did->id == cid->id || did->id == BCMA_ANY_ID) &&
  260. (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
  261. (did->class == cid->class || did->class == BCMA_ANY_CLASS))
  262. return 1;
  263. }
  264. return 0;
  265. }
  266. static int bcma_device_probe(struct device *dev)
  267. {
  268. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  269. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  270. drv);
  271. int err = 0;
  272. if (adrv->probe)
  273. err = adrv->probe(core);
  274. return err;
  275. }
  276. static int bcma_device_remove(struct device *dev)
  277. {
  278. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  279. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  280. drv);
  281. if (adrv->remove)
  282. adrv->remove(core);
  283. return 0;
  284. }
  285. static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  286. {
  287. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  288. return add_uevent_var(env,
  289. "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
  290. core->id.manuf, core->id.id,
  291. core->id.rev, core->id.class);
  292. }
  293. static int __init bcma_modinit(void)
  294. {
  295. int err;
  296. err = bus_register(&bcma_bus_type);
  297. if (err)
  298. return err;
  299. #ifdef CONFIG_BCMA_HOST_PCI
  300. err = bcma_host_pci_init();
  301. if (err) {
  302. pr_err("PCI host initialization failed\n");
  303. err = 0;
  304. }
  305. #endif
  306. return err;
  307. }
  308. fs_initcall(bcma_modinit);
  309. static void __exit bcma_modexit(void)
  310. {
  311. #ifdef CONFIG_BCMA_HOST_PCI
  312. bcma_host_pci_exit();
  313. #endif
  314. bus_unregister(&bcma_bus_type);
  315. }
  316. module_exit(bcma_modexit)