host_pci.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. pci_write_config_dword(core->bus->host_pci, BCMA_PCI_BAR0_WIN,
  15. core->addr);
  16. pci_write_config_dword(core->bus->host_pci, BCMA_PCI_BAR0_WIN2,
  17. core->wrap);
  18. core->bus->mapped_core = core;
  19. bcma_debug(core->bus, "Switched to core: 0x%X\n", core->id.id);
  20. }
  21. /* Provides access to the requested core. Returns base offset that has to be
  22. * used. It makes use of fixed windows when possible. */
  23. static u16 bcma_host_pci_provide_access_to_core(struct bcma_device *core)
  24. {
  25. switch (core->id.id) {
  26. case BCMA_CORE_CHIPCOMMON:
  27. return 3 * BCMA_CORE_SIZE;
  28. case BCMA_CORE_PCIE:
  29. return 2 * BCMA_CORE_SIZE;
  30. }
  31. if (core->bus->mapped_core != core)
  32. bcma_host_pci_switch_core(core);
  33. return 0;
  34. }
  35. static u8 bcma_host_pci_read8(struct bcma_device *core, u16 offset)
  36. {
  37. offset += bcma_host_pci_provide_access_to_core(core);
  38. return ioread8(core->bus->mmio + offset);
  39. }
  40. static u16 bcma_host_pci_read16(struct bcma_device *core, u16 offset)
  41. {
  42. offset += bcma_host_pci_provide_access_to_core(core);
  43. return ioread16(core->bus->mmio + offset);
  44. }
  45. static u32 bcma_host_pci_read32(struct bcma_device *core, u16 offset)
  46. {
  47. offset += bcma_host_pci_provide_access_to_core(core);
  48. return ioread32(core->bus->mmio + offset);
  49. }
  50. static void bcma_host_pci_write8(struct bcma_device *core, u16 offset,
  51. u8 value)
  52. {
  53. offset += bcma_host_pci_provide_access_to_core(core);
  54. iowrite8(value, core->bus->mmio + offset);
  55. }
  56. static void bcma_host_pci_write16(struct bcma_device *core, u16 offset,
  57. u16 value)
  58. {
  59. offset += bcma_host_pci_provide_access_to_core(core);
  60. iowrite16(value, core->bus->mmio + offset);
  61. }
  62. static void bcma_host_pci_write32(struct bcma_device *core, u16 offset,
  63. u32 value)
  64. {
  65. offset += bcma_host_pci_provide_access_to_core(core);
  66. iowrite32(value, core->bus->mmio + offset);
  67. }
  68. #ifdef CONFIG_BCMA_BLOCKIO
  69. static void bcma_host_pci_block_read(struct bcma_device *core, void *buffer,
  70. size_t count, u16 offset, u8 reg_width)
  71. {
  72. void __iomem *addr = core->bus->mmio + offset;
  73. if (core->bus->mapped_core != core)
  74. bcma_host_pci_switch_core(core);
  75. switch (reg_width) {
  76. case sizeof(u8):
  77. ioread8_rep(addr, buffer, count);
  78. break;
  79. case sizeof(u16):
  80. WARN_ON(count & 1);
  81. ioread16_rep(addr, buffer, count >> 1);
  82. break;
  83. case sizeof(u32):
  84. WARN_ON(count & 3);
  85. ioread32_rep(addr, buffer, count >> 2);
  86. break;
  87. default:
  88. WARN_ON(1);
  89. }
  90. }
  91. static void bcma_host_pci_block_write(struct bcma_device *core,
  92. const void *buffer, size_t count,
  93. u16 offset, u8 reg_width)
  94. {
  95. void __iomem *addr = core->bus->mmio + offset;
  96. if (core->bus->mapped_core != core)
  97. bcma_host_pci_switch_core(core);
  98. switch (reg_width) {
  99. case sizeof(u8):
  100. iowrite8_rep(addr, buffer, count);
  101. break;
  102. case sizeof(u16):
  103. WARN_ON(count & 1);
  104. iowrite16_rep(addr, buffer, count >> 1);
  105. break;
  106. case sizeof(u32):
  107. WARN_ON(count & 3);
  108. iowrite32_rep(addr, buffer, count >> 2);
  109. break;
  110. default:
  111. WARN_ON(1);
  112. }
  113. }
  114. #endif
  115. static u32 bcma_host_pci_aread32(struct bcma_device *core, u16 offset)
  116. {
  117. if (core->bus->mapped_core != core)
  118. bcma_host_pci_switch_core(core);
  119. return ioread32(core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
  120. }
  121. static void bcma_host_pci_awrite32(struct bcma_device *core, u16 offset,
  122. u32 value)
  123. {
  124. if (core->bus->mapped_core != core)
  125. bcma_host_pci_switch_core(core);
  126. iowrite32(value, core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
  127. }
  128. static const struct bcma_host_ops bcma_host_pci_ops = {
  129. .read8 = bcma_host_pci_read8,
  130. .read16 = bcma_host_pci_read16,
  131. .read32 = bcma_host_pci_read32,
  132. .write8 = bcma_host_pci_write8,
  133. .write16 = bcma_host_pci_write16,
  134. .write32 = bcma_host_pci_write32,
  135. #ifdef CONFIG_BCMA_BLOCKIO
  136. .block_read = bcma_host_pci_block_read,
  137. .block_write = bcma_host_pci_block_write,
  138. #endif
  139. .aread32 = bcma_host_pci_aread32,
  140. .awrite32 = bcma_host_pci_awrite32,
  141. };
  142. static int bcma_host_pci_probe(struct pci_dev *dev,
  143. const struct pci_device_id *id)
  144. {
  145. struct bcma_bus *bus;
  146. int err = -ENOMEM;
  147. const char *name;
  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. name = dev_name(&dev->dev);
  158. if (dev->driver && dev->driver->name)
  159. name = dev->driver->name;
  160. err = pci_request_regions(dev, name);
  161. if (err)
  162. goto err_pci_disable;
  163. pci_set_master(dev);
  164. /* Disable the RETRY_TIMEOUT register (0x41) to keep
  165. * PCI Tx retries from interfering with C3 CPU state */
  166. pci_read_config_dword(dev, 0x40, &val);
  167. if ((val & 0x0000ff00) != 0)
  168. pci_write_config_dword(dev, 0x40, val & 0xffff00ff);
  169. /* SSB needed additional powering up, do we have any AMBA PCI cards? */
  170. if (!pci_is_pcie(dev)) {
  171. bcma_err(bus, "PCI card detected, they are not supported.\n");
  172. err = -ENXIO;
  173. goto err_pci_release_regions;
  174. }
  175. /* Map MMIO */
  176. err = -ENOMEM;
  177. bus->mmio = pci_iomap(dev, 0, ~0UL);
  178. if (!bus->mmio)
  179. goto err_pci_release_regions;
  180. /* Host specific */
  181. bus->host_pci = dev;
  182. bus->hosttype = BCMA_HOSTTYPE_PCI;
  183. bus->ops = &bcma_host_pci_ops;
  184. bus->boardinfo.vendor = bus->host_pci->subsystem_vendor;
  185. bus->boardinfo.type = bus->host_pci->subsystem_device;
  186. /* Register */
  187. err = bcma_bus_register(bus);
  188. if (err)
  189. goto err_pci_unmap_mmio;
  190. pci_set_drvdata(dev, bus);
  191. out:
  192. return err;
  193. err_pci_unmap_mmio:
  194. pci_iounmap(dev, bus->mmio);
  195. err_pci_release_regions:
  196. pci_release_regions(dev);
  197. err_pci_disable:
  198. pci_disable_device(dev);
  199. err_kfree_bus:
  200. kfree(bus);
  201. return err;
  202. }
  203. static void bcma_host_pci_remove(struct pci_dev *dev)
  204. {
  205. struct bcma_bus *bus = pci_get_drvdata(dev);
  206. bcma_bus_unregister(bus);
  207. pci_iounmap(dev, bus->mmio);
  208. pci_release_regions(dev);
  209. pci_disable_device(dev);
  210. kfree(bus);
  211. pci_set_drvdata(dev, NULL);
  212. }
  213. #ifdef CONFIG_PM_SLEEP
  214. static int bcma_host_pci_suspend(struct device *dev)
  215. {
  216. struct pci_dev *pdev = to_pci_dev(dev);
  217. struct bcma_bus *bus = pci_get_drvdata(pdev);
  218. bus->mapped_core = NULL;
  219. return bcma_bus_suspend(bus);
  220. }
  221. static int bcma_host_pci_resume(struct device *dev)
  222. {
  223. struct pci_dev *pdev = to_pci_dev(dev);
  224. struct bcma_bus *bus = pci_get_drvdata(pdev);
  225. return bcma_bus_resume(bus);
  226. }
  227. static SIMPLE_DEV_PM_OPS(bcma_pm_ops, bcma_host_pci_suspend,
  228. bcma_host_pci_resume);
  229. #define BCMA_PM_OPS (&bcma_pm_ops)
  230. #else /* CONFIG_PM_SLEEP */
  231. #define BCMA_PM_OPS NULL
  232. #endif /* CONFIG_PM_SLEEP */
  233. static DEFINE_PCI_DEVICE_TABLE(bcma_pci_bridge_tbl) = {
  234. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) },
  235. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4313) },
  236. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43224) },
  237. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4331) },
  238. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) },
  239. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4357) },
  240. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4358) },
  241. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4359) },
  242. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4365) },
  243. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) },
  244. { 0, },
  245. };
  246. MODULE_DEVICE_TABLE(pci, bcma_pci_bridge_tbl);
  247. static struct pci_driver bcma_pci_bridge_driver = {
  248. .name = "bcma-pci-bridge",
  249. .id_table = bcma_pci_bridge_tbl,
  250. .probe = bcma_host_pci_probe,
  251. .remove = bcma_host_pci_remove,
  252. .driver.pm = BCMA_PM_OPS,
  253. };
  254. int __init bcma_host_pci_init(void)
  255. {
  256. return pci_register_driver(&bcma_pci_bridge_driver);
  257. }
  258. void __exit bcma_host_pci_exit(void)
  259. {
  260. pci_unregister_driver(&bcma_pci_bridge_driver);
  261. }