main.c 12 KB

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