main.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/bcma/bcma.h>
  10. #include <linux/slab.h>
  11. MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
  12. MODULE_LICENSE("GPL");
  13. /* contains the number the next bus should get. */
  14. static unsigned int bcma_bus_next_num = 0;
  15. /* bcma_buses_mutex locks the bcma_bus_next_num */
  16. static DEFINE_MUTEX(bcma_buses_mutex);
  17. static int bcma_bus_match(struct device *dev, struct device_driver *drv);
  18. static int bcma_device_probe(struct device *dev);
  19. static int bcma_device_remove(struct device *dev);
  20. static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
  21. static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
  22. {
  23. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  24. return sprintf(buf, "0x%03X\n", core->id.manuf);
  25. }
  26. static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
  27. {
  28. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  29. return sprintf(buf, "0x%03X\n", core->id.id);
  30. }
  31. static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
  32. {
  33. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  34. return sprintf(buf, "0x%02X\n", core->id.rev);
  35. }
  36. static ssize_t class_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%X\n", core->id.class);
  40. }
  41. static struct device_attribute bcma_device_attrs[] = {
  42. __ATTR_RO(manuf),
  43. __ATTR_RO(id),
  44. __ATTR_RO(rev),
  45. __ATTR_RO(class),
  46. __ATTR_NULL,
  47. };
  48. static struct bus_type bcma_bus_type = {
  49. .name = "bcma",
  50. .match = bcma_bus_match,
  51. .probe = bcma_device_probe,
  52. .remove = bcma_device_remove,
  53. .uevent = bcma_device_uevent,
  54. .dev_attrs = bcma_device_attrs,
  55. };
  56. static struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
  57. {
  58. struct bcma_device *core;
  59. list_for_each_entry(core, &bus->cores, list) {
  60. if (core->id.id == coreid)
  61. return core;
  62. }
  63. return NULL;
  64. }
  65. static void bcma_release_core_dev(struct device *dev)
  66. {
  67. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  68. if (core->io_addr)
  69. iounmap(core->io_addr);
  70. if (core->io_wrap)
  71. iounmap(core->io_wrap);
  72. kfree(core);
  73. }
  74. static int bcma_register_cores(struct bcma_bus *bus)
  75. {
  76. struct bcma_device *core;
  77. int err, dev_id = 0;
  78. list_for_each_entry(core, &bus->cores, list) {
  79. /* We support that cores ourself */
  80. switch (core->id.id) {
  81. case BCMA_CORE_CHIPCOMMON:
  82. case BCMA_CORE_PCI:
  83. case BCMA_CORE_PCIE:
  84. case BCMA_CORE_MIPS_74K:
  85. continue;
  86. }
  87. core->dev.release = bcma_release_core_dev;
  88. core->dev.bus = &bcma_bus_type;
  89. dev_set_name(&core->dev, "bcma%d:%d", bus->num, dev_id);
  90. switch (bus->hosttype) {
  91. case BCMA_HOSTTYPE_PCI:
  92. core->dev.parent = &bus->host_pci->dev;
  93. core->dma_dev = &bus->host_pci->dev;
  94. core->irq = bus->host_pci->irq;
  95. break;
  96. case BCMA_HOSTTYPE_SOC:
  97. core->dev.dma_mask = &core->dev.coherent_dma_mask;
  98. core->dma_dev = &core->dev;
  99. break;
  100. case BCMA_HOSTTYPE_SDIO:
  101. break;
  102. }
  103. err = device_register(&core->dev);
  104. if (err) {
  105. pr_err("Could not register dev for core 0x%03X\n",
  106. core->id.id);
  107. continue;
  108. }
  109. core->dev_registered = true;
  110. dev_id++;
  111. }
  112. return 0;
  113. }
  114. static void bcma_unregister_cores(struct bcma_bus *bus)
  115. {
  116. struct bcma_device *core;
  117. list_for_each_entry(core, &bus->cores, list) {
  118. if (core->dev_registered)
  119. device_unregister(&core->dev);
  120. }
  121. }
  122. int __devinit bcma_bus_register(struct bcma_bus *bus)
  123. {
  124. int err;
  125. struct bcma_device *core;
  126. mutex_lock(&bcma_buses_mutex);
  127. bus->num = bcma_bus_next_num++;
  128. mutex_unlock(&bcma_buses_mutex);
  129. /* Scan for devices (cores) */
  130. err = bcma_bus_scan(bus);
  131. if (err) {
  132. pr_err("Failed to scan: %d\n", err);
  133. return -1;
  134. }
  135. /* Init CC core */
  136. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  137. if (core) {
  138. bus->drv_cc.core = core;
  139. bcma_core_chipcommon_init(&bus->drv_cc);
  140. }
  141. /* Init MIPS core */
  142. core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  143. if (core) {
  144. bus->drv_mips.core = core;
  145. bcma_core_mips_init(&bus->drv_mips);
  146. }
  147. /* Init PCIE core */
  148. core = bcma_find_core(bus, BCMA_CORE_PCIE);
  149. if (core) {
  150. bus->drv_pci.core = core;
  151. bcma_core_pci_init(&bus->drv_pci);
  152. }
  153. /* Try to get SPROM */
  154. err = bcma_sprom_get(bus);
  155. if (err == -ENOENT) {
  156. pr_err("No SPROM available\n");
  157. } else if (err)
  158. pr_err("Failed to get SPROM: %d\n", err);
  159. /* Register found cores */
  160. bcma_register_cores(bus);
  161. pr_info("Bus registered\n");
  162. return 0;
  163. }
  164. void bcma_bus_unregister(struct bcma_bus *bus)
  165. {
  166. bcma_unregister_cores(bus);
  167. }
  168. int __init bcma_bus_early_register(struct bcma_bus *bus,
  169. struct bcma_device *core_cc,
  170. struct bcma_device *core_mips)
  171. {
  172. int err;
  173. struct bcma_device *core;
  174. struct bcma_device_id match;
  175. bcma_init_bus(bus);
  176. match.manuf = BCMA_MANUF_BCM;
  177. match.id = BCMA_CORE_CHIPCOMMON;
  178. match.class = BCMA_CL_SIM;
  179. match.rev = BCMA_ANY_REV;
  180. /* Scan for chip common core */
  181. err = bcma_bus_scan_early(bus, &match, core_cc);
  182. if (err) {
  183. pr_err("Failed to scan for common core: %d\n", err);
  184. return -1;
  185. }
  186. match.manuf = BCMA_MANUF_MIPS;
  187. match.id = BCMA_CORE_MIPS_74K;
  188. match.class = BCMA_CL_SIM;
  189. match.rev = BCMA_ANY_REV;
  190. /* Scan for mips core */
  191. err = bcma_bus_scan_early(bus, &match, core_mips);
  192. if (err) {
  193. pr_err("Failed to scan for mips core: %d\n", err);
  194. return -1;
  195. }
  196. /* Init CC core */
  197. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  198. if (core) {
  199. bus->drv_cc.core = core;
  200. bcma_core_chipcommon_init(&bus->drv_cc);
  201. }
  202. /* Init MIPS core */
  203. core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
  204. if (core) {
  205. bus->drv_mips.core = core;
  206. bcma_core_mips_init(&bus->drv_mips);
  207. }
  208. pr_info("Early bus registered\n");
  209. return 0;
  210. }
  211. #ifdef CONFIG_PM
  212. int bcma_bus_suspend(struct bcma_bus *bus)
  213. {
  214. struct bcma_device *core;
  215. list_for_each_entry(core, &bus->cores, list) {
  216. struct device_driver *drv = core->dev.driver;
  217. if (drv) {
  218. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  219. if (adrv->suspend)
  220. adrv->suspend(core);
  221. }
  222. }
  223. return 0;
  224. }
  225. int bcma_bus_resume(struct bcma_bus *bus)
  226. {
  227. struct bcma_device *core;
  228. /* Init CC core */
  229. core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
  230. if (core) {
  231. bus->drv_cc.setup_done = false;
  232. bcma_core_chipcommon_init(&bus->drv_cc);
  233. }
  234. list_for_each_entry(core, &bus->cores, list) {
  235. struct device_driver *drv = core->dev.driver;
  236. if (drv) {
  237. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  238. if (adrv->resume)
  239. adrv->resume(core);
  240. }
  241. }
  242. return 0;
  243. }
  244. #endif
  245. int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
  246. {
  247. drv->drv.name = drv->name;
  248. drv->drv.bus = &bcma_bus_type;
  249. drv->drv.owner = owner;
  250. return driver_register(&drv->drv);
  251. }
  252. EXPORT_SYMBOL_GPL(__bcma_driver_register);
  253. void bcma_driver_unregister(struct bcma_driver *drv)
  254. {
  255. driver_unregister(&drv->drv);
  256. }
  257. EXPORT_SYMBOL_GPL(bcma_driver_unregister);
  258. static int bcma_bus_match(struct device *dev, struct device_driver *drv)
  259. {
  260. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  261. struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
  262. const struct bcma_device_id *cid = &core->id;
  263. const struct bcma_device_id *did;
  264. for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
  265. if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
  266. (did->id == cid->id || did->id == BCMA_ANY_ID) &&
  267. (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
  268. (did->class == cid->class || did->class == BCMA_ANY_CLASS))
  269. return 1;
  270. }
  271. return 0;
  272. }
  273. static int bcma_device_probe(struct device *dev)
  274. {
  275. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  276. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  277. drv);
  278. int err = 0;
  279. if (adrv->probe)
  280. err = adrv->probe(core);
  281. return err;
  282. }
  283. static int bcma_device_remove(struct device *dev)
  284. {
  285. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  286. struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
  287. drv);
  288. if (adrv->remove)
  289. adrv->remove(core);
  290. return 0;
  291. }
  292. static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  293. {
  294. struct bcma_device *core = container_of(dev, struct bcma_device, dev);
  295. return add_uevent_var(env,
  296. "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
  297. core->id.manuf, core->id.id,
  298. core->id.rev, core->id.class);
  299. }
  300. static int __init bcma_modinit(void)
  301. {
  302. int err;
  303. err = bus_register(&bcma_bus_type);
  304. if (err)
  305. return err;
  306. #ifdef CONFIG_BCMA_HOST_PCI
  307. err = bcma_host_pci_init();
  308. if (err) {
  309. pr_err("PCI host initialization failed\n");
  310. err = 0;
  311. }
  312. #endif
  313. return err;
  314. }
  315. fs_initcall(bcma_modinit);
  316. static void __exit bcma_modexit(void)
  317. {
  318. #ifdef CONFIG_BCMA_HOST_PCI
  319. bcma_host_pci_exit();
  320. #endif
  321. bus_unregister(&bcma_bus_type);
  322. }
  323. module_exit(bcma_modexit)