main.c 14 KB

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