main.c 10 KB

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