main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #ifdef CONFIG_PM
  207. int bcma_bus_suspend(struct bcma_bus *bus)
  208. {
  209. struct bcma_device *core;
  210. list_for_each_entry(core, &bus->cores, list) {
  211. struct device_driver *drv = core->dev.driver;
  212. if (drv) {
  213. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  214. if (adrv->suspend)
  215. adrv->suspend(core);
  216. }
  217. }
  218. return 0;
  219. }
  220. int bcma_bus_resume(struct bcma_bus *bus)
  221. {
  222. struct bcma_device *core;
  223. /* Init CC core */
  224. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  225. if (core) {
  226. bus->drv_cc.setup_done = false;
  227. bcma_core_chipcommon_init(&bus->drv_cc);
  228. }
  229. list_for_each_entry(core, &bus->cores, list) {
  230. struct device_driver *drv = core->dev.driver;
  231. if (drv) {
  232. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  233. if (adrv->resume)
  234. adrv->resume(core);
  235. }
  236. }
  237. return 0;
  238. }
  239. #endif
  240. int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
  241. {
  242. drv->drv.name = drv->name;
  243. drv->drv.bus = &bcma_bus_type;
  244. drv->drv.owner = owner;
  245. return driver_register(&drv->drv);
  246. }
  247. EXPORT_SYMBOL_GPL(__bcma_driver_register);
  248. void bcma_driver_unregister(struct bcma_driver *drv)
  249. {
  250. driver_unregister(&drv->drv);
  251. }
  252. EXPORT_SYMBOL_GPL(bcma_driver_unregister);
  253. static int bcma_bus_match(struct device *dev, struct device_driver *drv)
  254. {
  255. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  256. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  257. const struct bcma_device_id *cid = &core->id;
  258. const struct bcma_device_id *did;
  259. for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
  260. if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
  261. (did->id == cid->id || did->id == BCMA_ANY_ID) &&
  262. (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
  263. (did->class == cid->class || did->class == BCMA_ANY_CLASS))
  264. return 1;
  265. }
  266. return 0;
  267. }
  268. static int bcma_device_probe(struct device *dev)
  269. {
  270. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  271. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  272. drv);
  273. int err = 0;
  274. if (adrv->probe)
  275. err = adrv->probe(core);
  276. return err;
  277. }
  278. static int bcma_device_remove(struct device *dev)
  279. {
  280. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  281. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  282. drv);
  283. if (adrv->remove)
  284. adrv->remove(core);
  285. return 0;
  286. }
  287. static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  288. {
  289. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  290. return add_uevent_var(env,
  291. "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
  292. core->id.manuf, core->id.id,
  293. core->id.rev, core->id.class);
  294. }
  295. static int __init bcma_modinit(void)
  296. {
  297. int err;
  298. err = bus_register(&bcma_bus_type);
  299. if (err)
  300. return err;
  301. #ifdef CONFIG_BCMA_HOST_PCI
  302. err = bcma_host_pci_init();
  303. if (err) {
  304. pr_err("PCI host initialization failed\n");
  305. err = 0;
  306. }
  307. #endif
  308. return err;
  309. }
  310. fs_initcall(bcma_modinit);
  311. static void __exit bcma_modexit(void)
  312. {
  313. #ifdef CONFIG_BCMA_HOST_PCI
  314. bcma_host_pci_exit();
  315. #endif
  316. bus_unregister(&bcma_bus_type);
  317. }
  318. module_exit(bcma_modexit)