host_pci.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /* Register */
  190. err = bcma_bus_register(bus);
  191. if (err)
  192. goto err_pci_unmap_mmio;
  193. pci_set_drvdata(dev, bus);
  194. out:
  195. return err;
  196. err_pci_unmap_mmio:
  197. pci_iounmap(dev, bus->mmio);
  198. err_pci_release_regions:
  199. pci_release_regions(dev);
  200. err_pci_disable:
  201. pci_disable_device(dev);
  202. err_kfree_bus:
  203. kfree(bus);
  204. return err;
  205. }
  206. static void bcma_host_pci_remove(struct pci_dev *dev)
  207. {
  208. struct bcma_bus *bus = pci_get_drvdata(dev);
  209. bcma_bus_unregister(bus);
  210. pci_iounmap(dev, bus->mmio);
  211. pci_release_regions(dev);
  212. pci_disable_device(dev);
  213. kfree(bus);
  214. }
  215. #ifdef CONFIG_PM_SLEEP
  216. static int bcma_host_pci_suspend(struct device *dev)
  217. {
  218. struct pci_dev *pdev = to_pci_dev(dev);
  219. struct bcma_bus *bus = pci_get_drvdata(pdev);
  220. bus->mapped_core = NULL;
  221. return bcma_bus_suspend(bus);
  222. }
  223. static int bcma_host_pci_resume(struct device *dev)
  224. {
  225. struct pci_dev *pdev = to_pci_dev(dev);
  226. struct bcma_bus *bus = pci_get_drvdata(pdev);
  227. return bcma_bus_resume(bus);
  228. }
  229. static SIMPLE_DEV_PM_OPS(bcma_pm_ops, bcma_host_pci_suspend,
  230. bcma_host_pci_resume);
  231. #define BCMA_PM_OPS (&bcma_pm_ops)
  232. #else /* CONFIG_PM_SLEEP */
  233. #define BCMA_PM_OPS NULL
  234. #endif /* CONFIG_PM_SLEEP */
  235. static const struct pci_device_id bcma_pci_bridge_tbl[] = {
  236. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) },
  237. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4313) },
  238. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43224) }, /* 0xa8d8 */
  239. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4331) },
  240. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) },
  241. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4357) },
  242. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4358) },
  243. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4359) },
  244. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4365) },
  245. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43a9) },
  246. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43aa) },
  247. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) },
  248. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43227) }, /* 0xa8db, BCM43217 (sic!) */
  249. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43228) }, /* 0xa8dc */
  250. { 0, },
  251. };
  252. MODULE_DEVICE_TABLE(pci, bcma_pci_bridge_tbl);
  253. static struct pci_driver bcma_pci_bridge_driver = {
  254. .name = "bcma-pci-bridge",
  255. .id_table = bcma_pci_bridge_tbl,
  256. .probe = bcma_host_pci_probe,
  257. .remove = bcma_host_pci_remove,
  258. .driver.pm = BCMA_PM_OPS,
  259. };
  260. int __init bcma_host_pci_init(void)
  261. {
  262. return pci_register_driver(&bcma_pci_bridge_driver);
  263. }
  264. void __exit bcma_host_pci_exit(void)
  265. {
  266. pci_unregister_driver(&bcma_pci_bridge_driver);
  267. }
  268. /**************************************************
  269. * Runtime ops for drivers.
  270. **************************************************/
  271. /* See also pcicore_up */
  272. void bcma_host_pci_up(struct bcma_bus *bus)
  273. {
  274. if (bus->hosttype != BCMA_HOSTTYPE_PCI)
  275. return;
  276. if (bus->host_is_pcie2)
  277. pr_warn("Bringing up bus with PCIe Gen 2 host is unsupported yet\n");
  278. else
  279. bcma_core_pci_up(&bus->drv_pci[0]);
  280. }
  281. EXPORT_SYMBOL_GPL(bcma_host_pci_up);
  282. /* See also pcicore_down */
  283. void bcma_host_pci_down(struct bcma_bus *bus)
  284. {
  285. if (bus->hosttype != BCMA_HOSTTYPE_PCI)
  286. return;
  287. if (!bus->host_is_pcie2)
  288. bcma_core_pci_down(&bus->drv_pci[0]);
  289. }
  290. EXPORT_SYMBOL_GPL(bcma_host_pci_down);