main.c 9.4 KB

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