main.c 16 KB

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