host_pci.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. pr_debug("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. 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. void bcma_host_pci_block_write(struct bcma_device *core, const void *buffer,
  92. size_t count, u16 offset, u8 reg_width)
  93. {
  94. void __iomem *addr = core->bus->mmio + offset;
  95. if (core->bus->mapped_core != core)
  96. bcma_host_pci_switch_core(core);
  97. switch (reg_width) {
  98. case sizeof(u8):
  99. iowrite8_rep(addr, buffer, count);
  100. break;
  101. case sizeof(u16):
  102. WARN_ON(count & 1);
  103. iowrite16_rep(addr, buffer, count >> 1);
  104. break;
  105. case sizeof(u32):
  106. WARN_ON(count & 3);
  107. iowrite32_rep(addr, buffer, count >> 2);
  108. break;
  109. default:
  110. WARN_ON(1);
  111. }
  112. }
  113. #endif
  114. static u32 bcma_host_pci_aread32(struct bcma_device *core, u16 offset)
  115. {
  116. if (core->bus->mapped_core != core)
  117. bcma_host_pci_switch_core(core);
  118. return ioread32(core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
  119. }
  120. static void bcma_host_pci_awrite32(struct bcma_device *core, u16 offset,
  121. u32 value)
  122. {
  123. if (core->bus->mapped_core != core)
  124. bcma_host_pci_switch_core(core);
  125. iowrite32(value, core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
  126. }
  127. const struct bcma_host_ops bcma_host_pci_ops = {
  128. .read8 = bcma_host_pci_read8,
  129. .read16 = bcma_host_pci_read16,
  130. .read32 = bcma_host_pci_read32,
  131. .write8 = bcma_host_pci_write8,
  132. .write16 = bcma_host_pci_write16,
  133. .write32 = bcma_host_pci_write32,
  134. #ifdef CONFIG_BCMA_BLOCKIO
  135. .block_read = bcma_host_pci_block_read,
  136. .block_write = bcma_host_pci_block_write,
  137. #endif
  138. .aread32 = bcma_host_pci_aread32,
  139. .awrite32 = bcma_host_pci_awrite32,
  140. };
  141. static int __devinit bcma_host_pci_probe(struct pci_dev *dev,
  142. const struct pci_device_id *id)
  143. {
  144. struct bcma_bus *bus;
  145. int err = -ENOMEM;
  146. const char *name;
  147. u32 val;
  148. /* Alloc */
  149. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  150. if (!bus)
  151. goto out;
  152. /* Basic PCI configuration */
  153. err = pci_enable_device(dev);
  154. if (err)
  155. goto err_kfree_bus;
  156. name = dev_name(&dev->dev);
  157. if (dev->driver && dev->driver->name)
  158. name = dev->driver->name;
  159. err = pci_request_regions(dev, name);
  160. if (err)
  161. goto err_pci_disable;
  162. pci_set_master(dev);
  163. /* Disable the RETRY_TIMEOUT register (0x41) to keep
  164. * PCI Tx retries from interfering with C3 CPU state */
  165. pci_read_config_dword(dev, 0x40, &val);
  166. if ((val & 0x0000ff00) != 0)
  167. pci_write_config_dword(dev, 0x40, val & 0xffff00ff);
  168. /* SSB needed additional powering up, do we have any AMBA PCI cards? */
  169. if (!pci_is_pcie(dev))
  170. pr_err("PCI card detected, report problems.\n");
  171. /* Map MMIO */
  172. err = -ENOMEM;
  173. bus->mmio = pci_iomap(dev, 0, ~0UL);
  174. if (!bus->mmio)
  175. goto err_pci_release_regions;
  176. /* Host specific */
  177. bus->host_pci = dev;
  178. bus->hosttype = BCMA_HOSTTYPE_PCI;
  179. bus->ops = &bcma_host_pci_ops;
  180. /* Register */
  181. err = bcma_bus_register(bus);
  182. if (err)
  183. goto err_pci_unmap_mmio;
  184. pci_set_drvdata(dev, bus);
  185. out:
  186. return err;
  187. err_pci_unmap_mmio:
  188. pci_iounmap(dev, bus->mmio);
  189. err_pci_release_regions:
  190. pci_release_regions(dev);
  191. err_pci_disable:
  192. pci_disable_device(dev);
  193. err_kfree_bus:
  194. kfree(bus);
  195. return err;
  196. }
  197. static void bcma_host_pci_remove(struct pci_dev *dev)
  198. {
  199. struct bcma_bus *bus = pci_get_drvdata(dev);
  200. bcma_bus_unregister(bus);
  201. pci_iounmap(dev, bus->mmio);
  202. pci_release_regions(dev);
  203. pci_disable_device(dev);
  204. kfree(bus);
  205. pci_set_drvdata(dev, NULL);
  206. }
  207. #ifdef CONFIG_PM
  208. static int bcma_host_pci_suspend(struct device *dev)
  209. {
  210. struct pci_dev *pdev = to_pci_dev(dev);
  211. struct bcma_bus *bus = pci_get_drvdata(pdev);
  212. bus->mapped_core = NULL;
  213. return bcma_bus_suspend(bus);
  214. }
  215. static int bcma_host_pci_resume(struct device *dev)
  216. {
  217. struct pci_dev *pdev = to_pci_dev(dev);
  218. struct bcma_bus *bus = pci_get_drvdata(pdev);
  219. return bcma_bus_resume(bus);
  220. }
  221. static SIMPLE_DEV_PM_OPS(bcma_pm_ops, bcma_host_pci_suspend,
  222. bcma_host_pci_resume);
  223. #define BCMA_PM_OPS (&bcma_pm_ops)
  224. #else /* CONFIG_PM */
  225. #define BCMA_PM_OPS NULL
  226. #endif /* CONFIG_PM */
  227. static DEFINE_PCI_DEVICE_TABLE(bcma_pci_bridge_tbl) = {
  228. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) },
  229. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4331) },
  230. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) },
  231. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4357) },
  232. { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) },
  233. { 0, },
  234. };
  235. MODULE_DEVICE_TABLE(pci, bcma_pci_bridge_tbl);
  236. static struct pci_driver bcma_pci_bridge_driver = {
  237. .name = "bcma-pci-bridge",
  238. .id_table = bcma_pci_bridge_tbl,
  239. .probe = bcma_host_pci_probe,
  240. .remove = bcma_host_pci_remove,
  241. .driver.pm = BCMA_PM_OPS,
  242. };
  243. int __init bcma_host_pci_init(void)
  244. {
  245. return pci_register_driver(&bcma_pci_bridge_driver);
  246. }
  247. void __exit bcma_host_pci_exit(void)
  248. {
  249. pci_unregister_driver(&bcma_pci_bridge_driver);
  250. }