main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. #if defined(CONFIG_OF) && defined(CONFIG_OF_ADDRESS)
  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. static void bcma_register_core(struct bcma_bus *bus, struct bcma_device *core)
  146. {
  147. int err;
  148. core->dev.release = bcma_release_core_dev;
  149. core->dev.bus = &bcma_bus_type;
  150. dev_set_name(&core->dev, "bcma%d:%d", bus->num, core->core_index);
  151. switch (bus->hosttype) {
  152. case BCMA_HOSTTYPE_PCI:
  153. core->dev.parent = &bus->host_pci->dev;
  154. core->dma_dev = &bus->host_pci->dev;
  155. core->irq = bus->host_pci->irq;
  156. break;
  157. case BCMA_HOSTTYPE_SOC:
  158. core->dev.dma_mask = &core->dev.coherent_dma_mask;
  159. if (bus->host_pdev) {
  160. core->dma_dev = &bus->host_pdev->dev;
  161. core->dev.parent = &bus->host_pdev->dev;
  162. bcma_of_fill_device(bus->host_pdev, core);
  163. } else {
  164. core->dma_dev = &core->dev;
  165. }
  166. break;
  167. case BCMA_HOSTTYPE_SDIO:
  168. break;
  169. }
  170. err = device_register(&core->dev);
  171. if (err) {
  172. bcma_err(bus, "Could not register dev for core 0x%03X\n",
  173. core->id.id);
  174. put_device(&core->dev);
  175. return;
  176. }
  177. core->dev_registered = true;
  178. }
  179. static int bcma_register_devices(struct bcma_bus *bus)
  180. {
  181. struct bcma_device *core;
  182. int err;
  183. list_for_each_entry(core, &bus->cores, list) {
  184. /* We support that cores ourself */
  185. switch (core->id.id) {
  186. case BCMA_CORE_4706_CHIPCOMMON:
  187. case BCMA_CORE_CHIPCOMMON:
  188. case BCMA_CORE_NS_CHIPCOMMON_B:
  189. case BCMA_CORE_PCI:
  190. case BCMA_CORE_PCIE:
  191. case BCMA_CORE_PCIE2:
  192. case BCMA_CORE_MIPS_74K:
  193. case BCMA_CORE_4706_MAC_GBIT_COMMON:
  194. continue;
  195. }
  196. /* Early cores were already registered */
  197. if (bcma_is_core_needed_early(core->id.id))
  198. continue;
  199. /* Only first GMAC core on BCM4706 is connected and working */
  200. if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
  201. core->core_unit > 0)
  202. continue;
  203. bcma_register_core(bus, core);
  204. }
  205. #ifdef CONFIG_BCMA_DRIVER_MIPS
  206. if (bus->drv_cc.pflash.present) {
  207. err = platform_device_register(&bcma_pflash_dev);
  208. if (err)
  209. bcma_err(bus, "Error registering parallel flash\n");
  210. }
  211. #endif
  212. #ifdef CONFIG_BCMA_SFLASH
  213. if (bus->drv_cc.sflash.present) {
  214. err = platform_device_register(&bcma_sflash_dev);
  215. if (err)
  216. bcma_err(bus, "Error registering serial flash\n");
  217. }
  218. #endif
  219. #ifdef CONFIG_BCMA_NFLASH
  220. if (bus->drv_cc.nflash.present) {
  221. err = platform_device_register(&bcma_nflash_dev);
  222. if (err)
  223. bcma_err(bus, "Error registering NAND flash\n");
  224. }
  225. #endif
  226. err = bcma_gpio_init(&bus->drv_cc);
  227. if (err == -ENOTSUPP)
  228. bcma_debug(bus, "GPIO driver not activated\n");
  229. else if (err)
  230. bcma_err(bus, "Error registering GPIO driver: %i\n", err);
  231. if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
  232. err = bcma_chipco_watchdog_register(&bus->drv_cc);
  233. if (err)
  234. bcma_err(bus, "Error registering watchdog driver\n");
  235. }
  236. return 0;
  237. }
  238. static void bcma_unregister_cores(struct bcma_bus *bus)
  239. {
  240. struct bcma_device *core, *tmp;
  241. list_for_each_entry_safe(core, tmp, &bus->cores, list) {
  242. list_del(&core->list);
  243. if (core->dev_registered)
  244. device_unregister(&core->dev);
  245. }
  246. if (bus->hosttype == BCMA_HOSTTYPE_SOC)
  247. platform_device_unregister(bus->drv_cc.watchdog);
  248. }
  249. int bcma_bus_register(struct bcma_bus *bus)
  250. {
  251. int err;
  252. struct bcma_device *core;
  253. mutex_lock(&bcma_buses_mutex);
  254. bus->num = bcma_bus_next_num++;
  255. mutex_unlock(&bcma_buses_mutex);
  256. /* Scan for devices (cores) */
  257. err = bcma_bus_scan(bus);
  258. if (err) {
  259. bcma_err(bus, "Failed to scan: %d\n", err);
  260. return err;
  261. }
  262. /* Early init CC core */
  263. core = bcma_find_core(bus, bcma_cc_core_id(bus));
  264. if (core) {
  265. bus->drv_cc.core = core;
  266. bcma_core_chipcommon_early_init(&bus->drv_cc);
  267. }
  268. /* Cores providing flash access go before SPROM init */
  269. list_for_each_entry(core, &bus->cores, list) {
  270. if (bcma_is_core_needed_early(core->id.id))
  271. bcma_register_core(bus, core);
  272. }
  273. /* Try to get SPROM */
  274. err = bcma_sprom_get(bus);
  275. if (err == -ENOENT) {
  276. bcma_err(bus, "No SPROM available\n");
  277. } else if (err)
  278. bcma_err(bus, "Failed to get SPROM: %d\n", err);
  279. /* Init CC core */
  280. core = bcma_find_core(bus, bcma_cc_core_id(bus));
  281. if (core) {
  282. bus->drv_cc.core = core;
  283. bcma_core_chipcommon_init(&bus->drv_cc);
  284. }
  285. /* Init CC core */
  286. core = bcma_find_core(bus, BCMA_CORE_NS_CHIPCOMMON_B);
  287. if (core) {
  288. bus->drv_cc_b.core = core;
  289. bcma_core_chipcommon_b_init(&bus->drv_cc_b);
  290. }
  291. /* Init MIPS core */
  292. core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  293. if (core) {
  294. bus->drv_mips.core = core;
  295. bcma_core_mips_init(&bus->drv_mips);
  296. }
  297. /* Init PCIE core */
  298. core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
  299. if (core) {
  300. bus->drv_pci[0].core = core;
  301. bcma_core_pci_init(&bus->drv_pci[0]);
  302. }
  303. /* Init PCIE core */
  304. core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1);
  305. if (core) {
  306. bus->drv_pci[1].core = core;
  307. bcma_core_pci_init(&bus->drv_pci[1]);
  308. }
  309. /* Init PCIe Gen 2 core */
  310. core = bcma_find_core_unit(bus, BCMA_CORE_PCIE2, 0);
  311. if (core) {
  312. bus->drv_pcie2.core = core;
  313. bcma_core_pcie2_init(&bus->drv_pcie2);
  314. }
  315. /* Init GBIT MAC COMMON core */
  316. core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
  317. if (core) {
  318. bus->drv_gmac_cmn.core = core;
  319. bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn);
  320. }
  321. /* Register found cores */
  322. bcma_register_devices(bus);
  323. bcma_info(bus, "Bus registered\n");
  324. return 0;
  325. }
  326. void bcma_bus_unregister(struct bcma_bus *bus)
  327. {
  328. struct bcma_device *cores[3];
  329. int err;
  330. err = bcma_gpio_unregister(&bus->drv_cc);
  331. if (err == -EBUSY)
  332. bcma_err(bus, "Some GPIOs are still in use.\n");
  333. else if (err)
  334. bcma_err(bus, "Can not unregister GPIO driver: %i\n", err);
  335. bcma_core_chipcommon_b_free(&bus->drv_cc_b);
  336. cores[0] = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  337. cores[1] = bcma_find_core(bus, BCMA_CORE_PCIE);
  338. cores[2] = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
  339. bcma_unregister_cores(bus);
  340. kfree(cores[2]);
  341. kfree(cores[1]);
  342. kfree(cores[0]);
  343. }
  344. int __init bcma_bus_early_register(struct bcma_bus *bus,
  345. struct bcma_device *core_cc,
  346. struct bcma_device *core_mips)
  347. {
  348. int err;
  349. struct bcma_device *core;
  350. struct bcma_device_id match;
  351. match.manuf = BCMA_MANUF_BCM;
  352. match.id = bcma_cc_core_id(bus);
  353. match.class = BCMA_CL_SIM;
  354. match.rev = BCMA_ANY_REV;
  355. /* Scan for chip common core */
  356. err = bcma_bus_scan_early(bus, &match, core_cc);
  357. if (err) {
  358. bcma_err(bus, "Failed to scan for common core: %d\n", err);
  359. return -1;
  360. }
  361. match.manuf = BCMA_MANUF_MIPS;
  362. match.id = BCMA_CORE_MIPS_74K;
  363. match.class = BCMA_CL_SIM;
  364. match.rev = BCMA_ANY_REV;
  365. /* Scan for mips core */
  366. err = bcma_bus_scan_early(bus, &match, core_mips);
  367. if (err) {
  368. bcma_err(bus, "Failed to scan for mips core: %d\n", err);
  369. return -1;
  370. }
  371. /* Early init CC core */
  372. core = bcma_find_core(bus, bcma_cc_core_id(bus));
  373. if (core) {
  374. bus->drv_cc.core = core;
  375. bcma_core_chipcommon_early_init(&bus->drv_cc);
  376. }
  377. /* Early init MIPS core */
  378. core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  379. if (core) {
  380. bus->drv_mips.core = core;
  381. bcma_core_mips_early_init(&bus->drv_mips);
  382. }
  383. bcma_info(bus, "Early bus registered\n");
  384. return 0;
  385. }
  386. #ifdef CONFIG_PM
  387. int bcma_bus_suspend(struct bcma_bus *bus)
  388. {
  389. struct bcma_device *core;
  390. list_for_each_entry(core, &bus->cores, list) {
  391. struct device_driver *drv = core->dev.driver;
  392. if (drv) {
  393. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  394. if (adrv->suspend)
  395. adrv->suspend(core);
  396. }
  397. }
  398. return 0;
  399. }
  400. int bcma_bus_resume(struct bcma_bus *bus)
  401. {
  402. struct bcma_device *core;
  403. /* Init CC core */
  404. if (bus->drv_cc.core) {
  405. bus->drv_cc.setup_done = false;
  406. bcma_core_chipcommon_init(&bus->drv_cc);
  407. }
  408. list_for_each_entry(core, &bus->cores, list) {
  409. struct device_driver *drv = core->dev.driver;
  410. if (drv) {
  411. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  412. if (adrv->resume)
  413. adrv->resume(core);
  414. }
  415. }
  416. return 0;
  417. }
  418. #endif
  419. int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
  420. {
  421. drv->drv.name = drv->name;
  422. drv->drv.bus = &bcma_bus_type;
  423. drv->drv.owner = owner;
  424. return driver_register(&drv->drv);
  425. }
  426. EXPORT_SYMBOL_GPL(__bcma_driver_register);
  427. void bcma_driver_unregister(struct bcma_driver *drv)
  428. {
  429. driver_unregister(&drv->drv);
  430. }
  431. EXPORT_SYMBOL_GPL(bcma_driver_unregister);
  432. static int bcma_bus_match(struct device *dev, struct device_driver *drv)
  433. {
  434. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  435. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  436. const struct bcma_device_id *cid = &core->id;
  437. const struct bcma_device_id *did;
  438. for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
  439. if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
  440. (did->id == cid->id || did->id == BCMA_ANY_ID) &&
  441. (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
  442. (did->class == cid->class || did->class == BCMA_ANY_CLASS))
  443. return 1;
  444. }
  445. return 0;
  446. }
  447. static int bcma_device_probe(struct device *dev)
  448. {
  449. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  450. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  451. drv);
  452. int err = 0;
  453. if (adrv->probe)
  454. err = adrv->probe(core);
  455. return err;
  456. }
  457. static int bcma_device_remove(struct device *dev)
  458. {
  459. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  460. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  461. drv);
  462. if (adrv->remove)
  463. adrv->remove(core);
  464. return 0;
  465. }
  466. static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  467. {
  468. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  469. return add_uevent_var(env,
  470. "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
  471. core->id.manuf, core->id.id,
  472. core->id.rev, core->id.class);
  473. }
  474. static int __init bcma_modinit(void)
  475. {
  476. int err;
  477. err = bus_register(&bcma_bus_type);
  478. if (err)
  479. return err;
  480. err = bcma_host_soc_register_driver();
  481. if (err) {
  482. pr_err("SoC host initialization failed\n");
  483. err = 0;
  484. }
  485. #ifdef CONFIG_BCMA_HOST_PCI
  486. err = bcma_host_pci_init();
  487. if (err) {
  488. pr_err("PCI host initialization failed\n");
  489. err = 0;
  490. }
  491. #endif
  492. return err;
  493. }
  494. fs_initcall(bcma_modinit);
  495. static void __exit bcma_modexit(void)
  496. {
  497. #ifdef CONFIG_BCMA_HOST_PCI
  498. bcma_host_pci_exit();
  499. #endif
  500. bcma_host_soc_unregister_driver();
  501. bus_unregister(&bcma_bus_type);
  502. }
  503. module_exit(bcma_modexit)