main.c 12 KB

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