main.c 9.1 KB

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