host_pci.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. const char *name;
  149. u32 val;
  150. /* Alloc */
  151. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  152. if (!bus)
  153. goto out;
  154. /* Basic PCI configuration */
  155. err = pci_enable_device(dev);
  156. if (err)
  157. goto err_kfree_bus;
  158. name = dev_name(&dev->dev);
  159. if (dev->driver && dev->driver->name)
  160. name = dev->driver->name;
  161. err = pci_request_regions(dev, name);
  162. if (err)
  163. goto err_pci_disable;
  164. pci_set_master(dev);
  165. /* Disable the RETRY_TIMEOUT register (0x41) to keep
  166. * PCI Tx retries from interfering with C3 CPU state */
  167. pci_read_config_dword(dev, 0x40, &val);
  168. if ((val & 0x0000ff00) != 0)
  169. pci_write_config_dword(dev, 0x40, val & 0xffff00ff);
  170. /* SSB needed additional powering up, do we have any AMBA PCI cards? */
  171. if (!pci_is_pcie(dev)) {
  172. bcma_err(bus, "PCI card detected, they are not supported.\n");
  173. err = -ENXIO;
  174. goto err_pci_release_regions;
  175. }
  176. /* Map MMIO */
  177. err = -ENOMEM;
  178. bus->mmio = pci_iomap(dev, 0, ~0UL);
  179. if (!bus->mmio)
  180. goto err_pci_release_regions;
  181. /* Host specific */
  182. bus->host_pci = dev;
  183. bus->hosttype = BCMA_HOSTTYPE_PCI;
  184. bus->ops = &bcma_host_pci_ops;
  185. bus->boardinfo.vendor = bus->host_pci->subsystem_vendor;
  186. bus->boardinfo.type = bus->host_pci->subsystem_device;
  187. /* Initialize struct, detect chip */
  188. bcma_init_bus(bus);
  189. /* Scan bus to find out generation of PCIe core */
  190. err = bcma_bus_scan(bus);
  191. if (err)
  192. goto err_pci_unmap_mmio;
  193. if (bcma_find_core(bus, BCMA_CORE_PCIE2))
  194. bus->host_is_pcie2 = true;
  195. /* Register */
  196. err = bcma_bus_register(bus);
  197. if (err)
  198. goto err_unregister_cores;
  199. pci_set_drvdata(dev, bus);
  200. out:
  201. return err;
  202. err_unregister_cores:
  203. bcma_unregister_cores(bus);
  204. err_pci_unmap_mmio:
  205. pci_iounmap(dev, bus->mmio);
  206. err_pci_release_regions:
  207. pci_release_regions(dev);
  208. err_pci_disable:
  209. pci_disable_device(dev);
  210. err_kfree_bus:
  211. kfree(bus);
  212. return err;
  213. }
  214. static void bcma_host_pci_remove(struct pci_dev *dev)
  215. {
  216. struct bcma_bus *bus = pci_get_drvdata(dev);
  217. bcma_bus_unregister(bus);
  218. pci_iounmap(dev, bus->mmio);
  219. pci_release_regions(dev);
  220. pci_disable_device(dev);
  221. kfree(bus);
  222. }
  223. #ifdef CONFIG_PM_SLEEP
  224. static int bcma_host_pci_suspend(struct device *dev)
  225. {
  226. struct pci_dev *pdev = to_pci_dev(dev);
  227. struct bcma_bus *bus = pci_get_drvdata(pdev);
  228. bus->mapped_core = NULL;
  229. return bcma_bus_suspend(bus);
  230. }
  231. static int bcma_host_pci_resume(struct device *dev)
  232. {
  233. struct pci_dev *pdev = to_pci_dev(dev);
  234. struct bcma_bus *bus = pci_get_drvdata(pdev);
  235. return bcma_bus_resume(bus);
  236. }
  237. static SIMPLE_DEV_PM_OPS(bcma_pm_ops, bcma_host_pci_suspend,
  238. bcma_host_pci_resume);
  239. #define BCMA_PM_OPS (&bcma_pm_ops)
  240. #else /* CONFIG_PM_SLEEP */
  241. #define BCMA_PM_OPS NULL
  242. #endif /* CONFIG_PM_SLEEP */
  243. static const struct pci_device_id bcma_pci_bridge_tbl[] = {
  244. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) },
  245. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4313) },
  246. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43224) }, /* 0xa8d8 */
  247. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4331) },
  248. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) },
  249. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4357) },
  250. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4358) },
  251. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4359) },
  252. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4360) },
  253. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4365) },
  254. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43a0) },
  255. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43a9) },
  256. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43aa) },
  257. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43b1) },
  258. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) },
  259. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43227) }, /* 0xa8db, BCM43217 (sic!) */
  260. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43228) }, /* 0xa8dc */
  261. { 0, },
  262. };
  263. MODULE_DEVICE_TABLE(pci, bcma_pci_bridge_tbl);
  264. static struct pci_driver bcma_pci_bridge_driver = {
  265. .name = "bcma-pci-bridge",
  266. .id_table = bcma_pci_bridge_tbl,
  267. .probe = bcma_host_pci_probe,
  268. .remove = bcma_host_pci_remove,
  269. .driver.pm = BCMA_PM_OPS,
  270. };
  271. int __init bcma_host_pci_init(void)
  272. {
  273. return pci_register_driver(&bcma_pci_bridge_driver);
  274. }
  275. void __exit bcma_host_pci_exit(void)
  276. {
  277. pci_unregister_driver(&bcma_pci_bridge_driver);
  278. }
  279. /**************************************************
  280. * Runtime ops for drivers.
  281. **************************************************/
  282. /* See also pcicore_up */
  283. void bcma_host_pci_up(struct bcma_bus *bus)
  284. {
  285. if (bus->hosttype != BCMA_HOSTTYPE_PCI)
  286. return;
  287. if (bus->host_is_pcie2)
  288. bcma_core_pcie2_up(&bus->drv_pcie2);
  289. else
  290. bcma_core_pci_up(&bus->drv_pci[0]);
  291. }
  292. EXPORT_SYMBOL_GPL(bcma_host_pci_up);
  293. /* See also pcicore_down */
  294. void bcma_host_pci_down(struct bcma_bus *bus)
  295. {
  296. if (bus->hosttype != BCMA_HOSTTYPE_PCI)
  297. return;
  298. if (!bus->host_is_pcie2)
  299. bcma_core_pci_down(&bus->drv_pci[0]);
  300. }
  301. EXPORT_SYMBOL_GPL(bcma_host_pci_down);
  302. /* See also si_pci_setup */
  303. int bcma_host_pci_irq_ctl(struct bcma_bus *bus, struct bcma_device *core,
  304. bool enable)
  305. {
  306. struct pci_dev *pdev;
  307. u32 coremask, tmp;
  308. int err = 0;
  309. if (bus->hosttype != BCMA_HOSTTYPE_PCI) {
  310. /* This bcma device is not on a PCI host-bus. So the IRQs are
  311. * not routed through the PCI core.
  312. * So we must not enable routing through the PCI core. */
  313. goto out;
  314. }
  315. pdev = bus->host_pci;
  316. err = pci_read_config_dword(pdev, BCMA_PCI_IRQMASK, &tmp);
  317. if (err)
  318. goto out;
  319. coremask = BIT(core->core_index) << 8;
  320. if (enable)
  321. tmp |= coremask;
  322. else
  323. tmp &= ~coremask;
  324. err = pci_write_config_dword(pdev, BCMA_PCI_IRQMASK, tmp);
  325. out:
  326. return err;
  327. }
  328. EXPORT_SYMBOL_GPL(bcma_host_pci_irq_ctl);