main.c 8.9 KB

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