main.c 9.0 KB

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