host_pci.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Broadcom specific AMBA
  3. * PCI Host
  4. *
  5. * Licensed under the GNU/GPL. See COPYING for details.
  6. */
  7. #include "bcma_private.h"
  8. #include <linux/slab.h>
  9. #include <linux/bcma/bcma.h>
  10. #include <linux/pci.h>
  11. #include <linux/module.h>
  12. static void bcma_host_pci_switch_core(struct bcma_device *core)
  13. {
  14. int win2 = core->bus->host_is_pcie2 ?
  15. BCMA_PCIE2_BAR0_WIN2 : BCMA_PCI_BAR0_WIN2;
  16. pci_write_config_dword(core->bus->host_pci, BCMA_PCI_BAR0_WIN,
  17. core->addr);
  18. pci_write_config_dword(core->bus->host_pci, win2, core->wrap);
  19. core->bus->mapped_core = core;
  20. bcma_debug(core->bus, "Switched to core: 0x%X\n", core->id.id);
  21. }
  22. /* Provides access to the requested core. Returns base offset that has to be
  23. * used. It makes use of fixed windows when possible. */
  24. static u16 bcma_host_pci_provide_access_to_core(struct bcma_device *core)
  25. {
  26. switch (core->id.id) {
  27. case BCMA_CORE_CHIPCOMMON:
  28. return 3 * BCMA_CORE_SIZE;
  29. case BCMA_CORE_PCIE:
  30. return 2 * BCMA_CORE_SIZE;
  31. }
  32. if (core->bus->mapped_core != core)
  33. bcma_host_pci_switch_core(core);
  34. return 0;
  35. }
  36. static u8 bcma_host_pci_read8(struct bcma_device *core, u16 offset)
  37. {
  38. offset += bcma_host_pci_provide_access_to_core(core);
  39. return ioread8(core->bus->mmio + offset);
  40. }
  41. static u16 bcma_host_pci_read16(struct bcma_device *core, u16 offset)
  42. {
  43. offset += bcma_host_pci_provide_access_to_core(core);
  44. return ioread16(core->bus->mmio + offset);
  45. }
  46. static u32 bcma_host_pci_read32(struct bcma_device *core, u16 offset)
  47. {
  48. offset += bcma_host_pci_provide_access_to_core(core);
  49. return ioread32(core->bus->mmio + offset);
  50. }
  51. static void bcma_host_pci_write8(struct bcma_device *core, u16 offset,
  52. u8 value)
  53. {
  54. offset += bcma_host_pci_provide_access_to_core(core);
  55. iowrite8(value, core->bus->mmio + offset);
  56. }
  57. static void bcma_host_pci_write16(struct bcma_device *core, u16 offset,
  58. u16 value)
  59. {
  60. offset += bcma_host_pci_provide_access_to_core(core);
  61. iowrite16(value, core->bus->mmio + offset);
  62. }
  63. static void bcma_host_pci_write32(struct bcma_device *core, u16 offset,
  64. u32 value)
  65. {
  66. offset += bcma_host_pci_provide_access_to_core(core);
  67. iowrite32(value, core->bus->mmio + offset);
  68. }
  69. #ifdef CONFIG_BCMA_BLOCKIO
  70. static void bcma_host_pci_block_read(struct bcma_device *core, void *buffer,
  71. size_t count, u16 offset, u8 reg_width)
  72. {
  73. void __iomem *addr = core->bus->mmio + offset;
  74. if (core->bus->mapped_core != core)
  75. bcma_host_pci_switch_core(core);
  76. switch (reg_width) {
  77. case sizeof(u8):
  78. ioread8_rep(addr, buffer, count);
  79. break;
  80. case sizeof(u16):
  81. WARN_ON(count & 1);
  82. ioread16_rep(addr, buffer, count >> 1);
  83. break;
  84. case sizeof(u32):
  85. WARN_ON(count & 3);
  86. ioread32_rep(addr, buffer, count >> 2);
  87. break;
  88. default:
  89. WARN_ON(1);
  90. }
  91. }
  92. static void bcma_host_pci_block_write(struct bcma_device *core,
  93. const void *buffer, size_t count,
  94. u16 offset, u8 reg_width)
  95. {
  96. void __iomem *addr = core->bus->mmio + offset;
  97. if (core->bus->mapped_core != core)
  98. bcma_host_pci_switch_core(core);
  99. switch (reg_width) {
  100. case sizeof(u8):
  101. iowrite8_rep(addr, buffer, count);
  102. break;
  103. case sizeof(u16):
  104. WARN_ON(count & 1);
  105. iowrite16_rep(addr, buffer, count >> 1);
  106. break;
  107. case sizeof(u32):
  108. WARN_ON(count & 3);
  109. iowrite32_rep(addr, buffer, count >> 2);
  110. break;
  111. default:
  112. WARN_ON(1);
  113. }
  114. }
  115. #endif
  116. static u32 bcma_host_pci_aread32(struct bcma_device *core, u16 offset)
  117. {
  118. if (core->bus->mapped_core != core)
  119. bcma_host_pci_switch_core(core);
  120. return ioread32(core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
  121. }
  122. static void bcma_host_pci_awrite32(struct bcma_device *core, u16 offset,
  123. u32 value)
  124. {
  125. if (core->bus->mapped_core != core)
  126. bcma_host_pci_switch_core(core);
  127. iowrite32(value, core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
  128. }
  129. static const struct bcma_host_ops bcma_host_pci_ops = {
  130. .read8 = bcma_host_pci_read8,
  131. .read16 = bcma_host_pci_read16,
  132. .read32 = bcma_host_pci_read32,
  133. .write8 = bcma_host_pci_write8,
  134. .write16 = bcma_host_pci_write16,
  135. .write32 = bcma_host_pci_write32,
  136. #ifdef CONFIG_BCMA_BLOCKIO
  137. .block_read = bcma_host_pci_block_read,
  138. .block_write = bcma_host_pci_block_write,
  139. #endif
  140. .aread32 = bcma_host_pci_aread32,
  141. .awrite32 = bcma_host_pci_awrite32,
  142. };
  143. static int bcma_host_pci_probe(struct pci_dev *dev,
  144. const struct pci_device_id *id)
  145. {
  146. struct bcma_bus *bus;
  147. int err = -ENOMEM;
  148. u32 val;
  149. /* Alloc */
  150. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  151. if (!bus)
  152. goto out;
  153. /* Basic PCI configuration */
  154. err = pci_enable_device(dev);
  155. if (err)
  156. goto err_kfree_bus;
  157. err = pci_request_regions(dev, "bcma-pci-bridge");
  158. if (err)
  159. goto err_pci_disable;
  160. pci_set_master(dev);
  161. /* Disable the RETRY_TIMEOUT register (0x41) to keep
  162. * PCI Tx retries from interfering with C3 CPU state */
  163. pci_read_config_dword(dev, 0x40, &val);
  164. if ((val & 0x0000ff00) != 0)
  165. pci_write_config_dword(dev, 0x40, val & 0xffff00ff);
  166. /* SSB needed additional powering up, do we have any AMBA PCI cards? */
  167. if (!pci_is_pcie(dev)) {
  168. bcma_err(bus, "PCI card detected, they are not supported.\n");
  169. err = -ENXIO;
  170. goto err_pci_release_regions;
  171. }
  172. bus->dev = &dev->dev;
  173. /* Map MMIO */
  174. err = -ENOMEM;
  175. bus->mmio = pci_iomap(dev, 0, ~0UL);
  176. if (!bus->mmio)
  177. goto err_pci_release_regions;
  178. /* Host specific */
  179. bus->host_pci = dev;
  180. bus->hosttype = BCMA_HOSTTYPE_PCI;
  181. bus->ops = &bcma_host_pci_ops;
  182. bus->boardinfo.vendor = bus->host_pci->subsystem_vendor;
  183. bus->boardinfo.type = bus->host_pci->subsystem_device;
  184. /* Initialize struct, detect chip */
  185. bcma_init_bus(bus);
  186. /* Scan bus to find out generation of PCIe core */
  187. err = bcma_bus_scan(bus);
  188. if (err)
  189. goto err_pci_unmap_mmio;
  190. if (bcma_find_core(bus, BCMA_CORE_PCIE2))
  191. bus->host_is_pcie2 = true;
  192. /* Register */
  193. err = bcma_bus_register(bus);
  194. if (err)
  195. goto err_unregister_cores;
  196. pci_set_drvdata(dev, bus);
  197. out:
  198. return err;
  199. err_unregister_cores:
  200. bcma_unregister_cores(bus);
  201. err_pci_unmap_mmio:
  202. pci_iounmap(dev, bus->mmio);
  203. err_pci_release_regions:
  204. pci_release_regions(dev);
  205. err_pci_disable:
  206. pci_disable_device(dev);
  207. err_kfree_bus:
  208. kfree(bus);
  209. return err;
  210. }
  211. static void bcma_host_pci_remove(struct pci_dev *dev)
  212. {
  213. struct bcma_bus *bus = pci_get_drvdata(dev);
  214. bcma_bus_unregister(bus);
  215. pci_iounmap(dev, bus->mmio);
  216. pci_release_regions(dev);
  217. pci_disable_device(dev);
  218. kfree(bus);
  219. }
  220. #ifdef CONFIG_PM_SLEEP
  221. static int bcma_host_pci_suspend(struct device *dev)
  222. {
  223. struct bcma_bus *bus = dev_get_drvdata(dev);
  224. bus->mapped_core = NULL;
  225. return bcma_bus_suspend(bus);
  226. }
  227. static int bcma_host_pci_resume(struct device *dev)
  228. {
  229. struct bcma_bus *bus = dev_get_drvdata(dev);
  230. return bcma_bus_resume(bus);
  231. }
  232. static SIMPLE_DEV_PM_OPS(bcma_pm_ops, bcma_host_pci_suspend,
  233. bcma_host_pci_resume);
  234. #define BCMA_PM_OPS (&bcma_pm_ops)
  235. #else /* CONFIG_PM_SLEEP */
  236. #define BCMA_PM_OPS NULL
  237. #endif /* CONFIG_PM_SLEEP */
  238. static const struct pci_device_id bcma_pci_bridge_tbl[] = {
  239. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) },
  240. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4313) },
  241. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43224) }, /* 0xa8d8 */
  242. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4331) },
  243. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) },
  244. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4357) },
  245. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4358) },
  246. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4359) },
  247. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4360) },
  248. { PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, 0x4365, PCI_VENDOR_ID_DELL, 0x0016) },
  249. { PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, 0x4365, PCI_VENDOR_ID_DELL, 0x0018) },
  250. { PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, 0x4365, PCI_VENDOR_ID_FOXCONN, 0xe092) },
  251. { PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, 0x4365, PCI_VENDOR_ID_HP, 0x804a) },
  252. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43a0) },
  253. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43a9) },
  254. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43aa) },
  255. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43b1) },
  256. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) },
  257. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43227) }, /* 0xa8db, BCM43217 (sic!) */
  258. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43228) }, /* 0xa8dc */
  259. { 0, },
  260. };
  261. MODULE_DEVICE_TABLE(pci, bcma_pci_bridge_tbl);
  262. static struct pci_driver bcma_pci_bridge_driver = {
  263. .name = "bcma-pci-bridge",
  264. .id_table = bcma_pci_bridge_tbl,
  265. .probe = bcma_host_pci_probe,
  266. .remove = bcma_host_pci_remove,
  267. .driver.pm = BCMA_PM_OPS,
  268. };
  269. int __init bcma_host_pci_init(void)
  270. {
  271. return pci_register_driver(&bcma_pci_bridge_driver);
  272. }
  273. void __exit bcma_host_pci_exit(void)
  274. {
  275. pci_unregister_driver(&bcma_pci_bridge_driver);
  276. }
  277. /**************************************************
  278. * Runtime ops for drivers.
  279. **************************************************/
  280. /* See also pcicore_up */
  281. void bcma_host_pci_up(struct bcma_bus *bus)
  282. {
  283. if (bus->hosttype != BCMA_HOSTTYPE_PCI)
  284. return;
  285. if (bus->host_is_pcie2)
  286. bcma_core_pcie2_up(&bus->drv_pcie2);
  287. else
  288. bcma_core_pci_up(&bus->drv_pci[0]);
  289. }
  290. EXPORT_SYMBOL_GPL(bcma_host_pci_up);
  291. /* See also pcicore_down */
  292. void bcma_host_pci_down(struct bcma_bus *bus)
  293. {
  294. if (bus->hosttype != BCMA_HOSTTYPE_PCI)
  295. return;
  296. if (!bus->host_is_pcie2)
  297. bcma_core_pci_down(&bus->drv_pci[0]);
  298. }
  299. EXPORT_SYMBOL_GPL(bcma_host_pci_down);
  300. /* See also si_pci_setup */
  301. int bcma_host_pci_irq_ctl(struct bcma_bus *bus, struct bcma_device *core,
  302. bool enable)
  303. {
  304. struct pci_dev *pdev;
  305. u32 coremask, tmp;
  306. int err = 0;
  307. if (bus->hosttype != BCMA_HOSTTYPE_PCI) {
  308. /* This bcma device is not on a PCI host-bus. So the IRQs are
  309. * not routed through the PCI core.
  310. * So we must not enable routing through the PCI core. */
  311. goto out;
  312. }
  313. pdev = bus->host_pci;
  314. err = pci_read_config_dword(pdev, BCMA_PCI_IRQMASK, &tmp);
  315. if (err)
  316. goto out;
  317. coremask = BIT(core->core_index) << 8;
  318. if (enable)
  319. tmp |= coremask;
  320. else
  321. tmp &= ~coremask;
  322. err = pci_write_config_dword(pdev, BCMA_PCI_IRQMASK, tmp);
  323. out:
  324. return err;
  325. }
  326. EXPORT_SYMBOL_GPL(bcma_host_pci_irq_ctl);