main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
  74. u8 unit)
  75. {
  76. struct bcma_device *core;
  77. list_for_each_entry(core, &bus->cores, list) {
  78. if (core->id.id == coreid && core->core_unit == unit)
  79. return core;
  80. }
  81. return NULL;
  82. }
  83. static void bcma_release_core_dev(struct device *dev)
  84. {
  85. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  86. if (core->io_addr)
  87. iounmap(core->io_addr);
  88. if (core->io_wrap)
  89. iounmap(core->io_wrap);
  90. kfree(core);
  91. }
  92. static int bcma_register_cores(struct bcma_bus *bus)
  93. {
  94. struct bcma_device *core;
  95. int err, dev_id = 0;
  96. list_for_each_entry(core, &bus->cores, list) {
  97. /* We support that cores ourself */
  98. switch (core->id.id) {
  99. case BCMA_CORE_4706_CHIPCOMMON:
  100. case BCMA_CORE_CHIPCOMMON:
  101. case BCMA_CORE_PCI:
  102. case BCMA_CORE_PCIE:
  103. case BCMA_CORE_MIPS_74K:
  104. case BCMA_CORE_4706_MAC_GBIT_COMMON:
  105. continue;
  106. }
  107. core->dev.release = bcma_release_core_dev;
  108. core->dev.bus = &bcma_bus_type;
  109. dev_set_name(&core->dev, "bcma%d:%d", bus->num, dev_id);
  110. switch (bus->hosttype) {
  111. case BCMA_HOSTTYPE_PCI:
  112. core->dev.parent = &bus->host_pci->dev;
  113. core->dma_dev = &bus->host_pci->dev;
  114. core->irq = bus->host_pci->irq;
  115. break;
  116. case BCMA_HOSTTYPE_SOC:
  117. core->dev.dma_mask = &core->dev.coherent_dma_mask;
  118. core->dma_dev = &core->dev;
  119. break;
  120. case BCMA_HOSTTYPE_SDIO:
  121. break;
  122. }
  123. err = device_register(&core->dev);
  124. if (err) {
  125. bcma_err(bus,
  126. "Could not register dev for core 0x%03X\n",
  127. core->id.id);
  128. continue;
  129. }
  130. core->dev_registered = true;
  131. dev_id++;
  132. }
  133. #ifdef CONFIG_BCMA_DRIVER_MIPS
  134. if (bus->drv_cc.pflash.present) {
  135. err = platform_device_register(&bcma_pflash_dev);
  136. if (err)
  137. bcma_err(bus, "Error registering parallel flash\n");
  138. }
  139. #endif
  140. #ifdef CONFIG_BCMA_SFLASH
  141. if (bus->drv_cc.sflash.present) {
  142. err = platform_device_register(&bcma_sflash_dev);
  143. if (err)
  144. bcma_err(bus, "Error registering serial flash\n");
  145. }
  146. #endif
  147. #ifdef CONFIG_BCMA_NFLASH
  148. if (bus->drv_cc.nflash.present) {
  149. err = platform_device_register(&bcma_nflash_dev);
  150. if (err)
  151. bcma_err(bus, "Error registering NAND flash\n");
  152. }
  153. #endif
  154. err = bcma_gpio_init(&bus->drv_cc);
  155. if (err == -ENOTSUPP)
  156. bcma_debug(bus, "GPIO driver not activated\n");
  157. else if (err)
  158. bcma_err(bus, "Error registering GPIO driver: %i\n", err);
  159. if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
  160. err = bcma_chipco_watchdog_register(&bus->drv_cc);
  161. if (err)
  162. bcma_err(bus, "Error registering watchdog driver\n");
  163. }
  164. return 0;
  165. }
  166. static void bcma_unregister_cores(struct bcma_bus *bus)
  167. {
  168. struct bcma_device *core, *tmp;
  169. list_for_each_entry_safe(core, tmp, &bus->cores, list) {
  170. list_del(&core->list);
  171. if (core->dev_registered)
  172. device_unregister(&core->dev);
  173. }
  174. if (bus->hosttype == BCMA_HOSTTYPE_SOC)
  175. platform_device_unregister(bus->drv_cc.watchdog);
  176. }
  177. int bcma_bus_register(struct bcma_bus *bus)
  178. {
  179. int err;
  180. struct bcma_device *core;
  181. mutex_lock(&bcma_buses_mutex);
  182. bus->num = bcma_bus_next_num++;
  183. mutex_unlock(&bcma_buses_mutex);
  184. /* Scan for devices (cores) */
  185. err = bcma_bus_scan(bus);
  186. if (err) {
  187. bcma_err(bus, "Failed to scan: %d\n", err);
  188. return -1;
  189. }
  190. /* Early init CC core */
  191. core = bcma_find_core(bus, bcma_cc_core_id(bus));
  192. if (core) {
  193. bus->drv_cc.core = core;
  194. bcma_core_chipcommon_early_init(&bus->drv_cc);
  195. }
  196. /* Try to get SPROM */
  197. err = bcma_sprom_get(bus);
  198. if (err == -ENOENT) {
  199. bcma_err(bus, "No SPROM available\n");
  200. } else if (err)
  201. bcma_err(bus, "Failed to get SPROM: %d\n", err);
  202. /* Init CC core */
  203. core = bcma_find_core(bus, bcma_cc_core_id(bus));
  204. if (core) {
  205. bus->drv_cc.core = core;
  206. bcma_core_chipcommon_init(&bus->drv_cc);
  207. }
  208. /* Init MIPS core */
  209. core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  210. if (core) {
  211. bus->drv_mips.core = core;
  212. bcma_core_mips_init(&bus->drv_mips);
  213. }
  214. /* Init PCIE core */
  215. core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
  216. if (core) {
  217. bus->drv_pci[0].core = core;
  218. bcma_core_pci_init(&bus->drv_pci[0]);
  219. }
  220. /* Init PCIE core */
  221. core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1);
  222. if (core) {
  223. bus->drv_pci[1].core = core;
  224. bcma_core_pci_init(&bus->drv_pci[1]);
  225. }
  226. /* Init GBIT MAC COMMON core */
  227. core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
  228. if (core) {
  229. bus->drv_gmac_cmn.core = core;
  230. bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn);
  231. }
  232. /* Register found cores */
  233. bcma_register_cores(bus);
  234. bcma_info(bus, "Bus registered\n");
  235. return 0;
  236. }
  237. void bcma_bus_unregister(struct bcma_bus *bus)
  238. {
  239. struct bcma_device *cores[3];
  240. int err;
  241. err = bcma_gpio_unregister(&bus->drv_cc);
  242. if (err == -EBUSY)
  243. bcma_err(bus, "Some GPIOs are still in use.\n");
  244. else if (err)
  245. bcma_err(bus, "Can not unregister GPIO driver: %i\n", err);
  246. cores[0] = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  247. cores[1] = bcma_find_core(bus, BCMA_CORE_PCIE);
  248. cores[2] = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
  249. bcma_unregister_cores(bus);
  250. kfree(cores[2]);
  251. kfree(cores[1]);
  252. kfree(cores[0]);
  253. }
  254. int __init bcma_bus_early_register(struct bcma_bus *bus,
  255. struct bcma_device *core_cc,
  256. struct bcma_device *core_mips)
  257. {
  258. int err;
  259. struct bcma_device *core;
  260. struct bcma_device_id match;
  261. bcma_init_bus(bus);
  262. match.manuf = BCMA_MANUF_BCM;
  263. match.id = bcma_cc_core_id(bus);
  264. match.class = BCMA_CL_SIM;
  265. match.rev = BCMA_ANY_REV;
  266. /* Scan for chip common core */
  267. err = bcma_bus_scan_early(bus, &match, core_cc);
  268. if (err) {
  269. bcma_err(bus, "Failed to scan for common core: %d\n", err);
  270. return -1;
  271. }
  272. match.manuf = BCMA_MANUF_MIPS;
  273. match.id = BCMA_CORE_MIPS_74K;
  274. match.class = BCMA_CL_SIM;
  275. match.rev = BCMA_ANY_REV;
  276. /* Scan for mips core */
  277. err = bcma_bus_scan_early(bus, &match, core_mips);
  278. if (err) {
  279. bcma_err(bus, "Failed to scan for mips core: %d\n", err);
  280. return -1;
  281. }
  282. /* Early init CC core */
  283. core = bcma_find_core(bus, bcma_cc_core_id(bus));
  284. if (core) {
  285. bus->drv_cc.core = core;
  286. bcma_core_chipcommon_early_init(&bus->drv_cc);
  287. }
  288. /* Early init MIPS core */
  289. core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  290. if (core) {
  291. bus->drv_mips.core = core;
  292. bcma_core_mips_early_init(&bus->drv_mips);
  293. }
  294. bcma_info(bus, "Early bus registered\n");
  295. return 0;
  296. }
  297. #ifdef CONFIG_PM
  298. int bcma_bus_suspend(struct bcma_bus *bus)
  299. {
  300. struct bcma_device *core;
  301. list_for_each_entry(core, &bus->cores, list) {
  302. struct device_driver *drv = core->dev.driver;
  303. if (drv) {
  304. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  305. if (adrv->suspend)
  306. adrv->suspend(core);
  307. }
  308. }
  309. return 0;
  310. }
  311. int bcma_bus_resume(struct bcma_bus *bus)
  312. {
  313. struct bcma_device *core;
  314. /* Init CC core */
  315. if (bus->drv_cc.core) {
  316. bus->drv_cc.setup_done = false;
  317. bcma_core_chipcommon_init(&bus->drv_cc);
  318. }
  319. list_for_each_entry(core, &bus->cores, list) {
  320. struct device_driver *drv = core->dev.driver;
  321. if (drv) {
  322. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  323. if (adrv->resume)
  324. adrv->resume(core);
  325. }
  326. }
  327. return 0;
  328. }
  329. #endif
  330. int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
  331. {
  332. drv->drv.name = drv->name;
  333. drv->drv.bus = &bcma_bus_type;
  334. drv->drv.owner = owner;
  335. return driver_register(&drv->drv);
  336. }
  337. EXPORT_SYMBOL_GPL(__bcma_driver_register);
  338. void bcma_driver_unregister(struct bcma_driver *drv)
  339. {
  340. driver_unregister(&drv->drv);
  341. }
  342. EXPORT_SYMBOL_GPL(bcma_driver_unregister);
  343. static int bcma_bus_match(struct device *dev, struct device_driver *drv)
  344. {
  345. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  346. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  347. const struct bcma_device_id *cid = &core->id;
  348. const struct bcma_device_id *did;
  349. for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
  350. if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
  351. (did->id == cid->id || did->id == BCMA_ANY_ID) &&
  352. (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
  353. (did->class == cid->class || did->class == BCMA_ANY_CLASS))
  354. return 1;
  355. }
  356. return 0;
  357. }
  358. static int bcma_device_probe(struct device *dev)
  359. {
  360. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  361. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  362. drv);
  363. int err = 0;
  364. if (adrv->probe)
  365. err = adrv->probe(core);
  366. return err;
  367. }
  368. static int bcma_device_remove(struct device *dev)
  369. {
  370. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  371. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  372. drv);
  373. if (adrv->remove)
  374. adrv->remove(core);
  375. return 0;
  376. }
  377. static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  378. {
  379. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  380. return add_uevent_var(env,
  381. "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
  382. core->id.manuf, core->id.id,
  383. core->id.rev, core->id.class);
  384. }
  385. static int __init bcma_modinit(void)
  386. {
  387. int err;
  388. err = bus_register(&bcma_bus_type);
  389. if (err)
  390. return err;
  391. #ifdef CONFIG_BCMA_HOST_PCI
  392. err = bcma_host_pci_init();
  393. if (err) {
  394. pr_err("PCI host initialization failed\n");
  395. err = 0;
  396. }
  397. #endif
  398. return err;
  399. }
  400. fs_initcall(bcma_modinit);
  401. static void __exit bcma_modexit(void)
  402. {
  403. #ifdef CONFIG_BCMA_HOST_PCI
  404. bcma_host_pci_exit();
  405. #endif
  406. bus_unregister(&bcma_bus_type);
  407. }
  408. module_exit(bcma_modexit)