driver_pci.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Broadcom specific AMBA
  3. * PCI Core
  4. *
  5. * Copyright 2005, Broadcom Corporation
  6. * Copyright 2006, 2007, Michael Buesch <m@bues.ch>
  7. * Copyright 2011, 2012, Hauke Mehrtens <hauke@hauke-m.de>
  8. *
  9. * Licensed under the GNU/GPL. See COPYING for details.
  10. */
  11. #include "bcma_private.h"
  12. #include <linux/export.h>
  13. #include <linux/bcma/bcma.h>
  14. /**************************************************
  15. * R/W ops.
  16. **************************************************/
  17. u32 bcma_pcie_read(struct bcma_drv_pci *pc, u32 address)
  18. {
  19. pcicore_write32(pc, BCMA_CORE_PCI_PCIEIND_ADDR, address);
  20. pcicore_read32(pc, BCMA_CORE_PCI_PCIEIND_ADDR);
  21. return pcicore_read32(pc, BCMA_CORE_PCI_PCIEIND_DATA);
  22. }
  23. #if 0
  24. static void bcma_pcie_write(struct bcma_drv_pci *pc, u32 address, u32 data)
  25. {
  26. pcicore_write32(pc, BCMA_CORE_PCI_PCIEIND_ADDR, address);
  27. pcicore_read32(pc, BCMA_CORE_PCI_PCIEIND_ADDR);
  28. pcicore_write32(pc, BCMA_CORE_PCI_PCIEIND_DATA, data);
  29. }
  30. #endif
  31. static void bcma_pcie_mdio_set_phy(struct bcma_drv_pci *pc, u8 phy)
  32. {
  33. u32 v;
  34. int i;
  35. v = BCMA_CORE_PCI_MDIODATA_START;
  36. v |= BCMA_CORE_PCI_MDIODATA_WRITE;
  37. v |= (BCMA_CORE_PCI_MDIODATA_DEV_ADDR <<
  38. BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF);
  39. v |= (BCMA_CORE_PCI_MDIODATA_BLK_ADDR <<
  40. BCMA_CORE_PCI_MDIODATA_REGADDR_SHF);
  41. v |= BCMA_CORE_PCI_MDIODATA_TA;
  42. v |= (phy << 4);
  43. pcicore_write32(pc, BCMA_CORE_PCI_MDIO_DATA, v);
  44. udelay(10);
  45. for (i = 0; i < 200; i++) {
  46. v = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_CONTROL);
  47. if (v & BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE)
  48. break;
  49. msleep(1);
  50. }
  51. }
  52. static u16 bcma_pcie_mdio_read(struct bcma_drv_pci *pc, u8 device, u8 address)
  53. {
  54. int max_retries = 10;
  55. u16 ret = 0;
  56. u32 v;
  57. int i;
  58. /* enable mdio access to SERDES */
  59. v = BCMA_CORE_PCI_MDIOCTL_PREAM_EN;
  60. v |= BCMA_CORE_PCI_MDIOCTL_DIVISOR_VAL;
  61. pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, v);
  62. if (pc->core->id.rev >= 10) {
  63. max_retries = 200;
  64. bcma_pcie_mdio_set_phy(pc, device);
  65. v = (BCMA_CORE_PCI_MDIODATA_DEV_ADDR <<
  66. BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF);
  67. v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF);
  68. } else {
  69. v = (device << BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF_OLD);
  70. v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF_OLD);
  71. }
  72. v = BCMA_CORE_PCI_MDIODATA_START;
  73. v |= BCMA_CORE_PCI_MDIODATA_READ;
  74. v |= BCMA_CORE_PCI_MDIODATA_TA;
  75. pcicore_write32(pc, BCMA_CORE_PCI_MDIO_DATA, v);
  76. /* Wait for the device to complete the transaction */
  77. udelay(10);
  78. for (i = 0; i < max_retries; i++) {
  79. v = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_CONTROL);
  80. if (v & BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE) {
  81. udelay(10);
  82. ret = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_DATA);
  83. break;
  84. }
  85. msleep(1);
  86. }
  87. pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, 0);
  88. return ret;
  89. }
  90. static void bcma_pcie_mdio_write(struct bcma_drv_pci *pc, u8 device,
  91. u8 address, u16 data)
  92. {
  93. int max_retries = 10;
  94. u32 v;
  95. int i;
  96. /* enable mdio access to SERDES */
  97. v = BCMA_CORE_PCI_MDIOCTL_PREAM_EN;
  98. v |= BCMA_CORE_PCI_MDIOCTL_DIVISOR_VAL;
  99. pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, v);
  100. if (pc->core->id.rev >= 10) {
  101. max_retries = 200;
  102. bcma_pcie_mdio_set_phy(pc, device);
  103. v = (BCMA_CORE_PCI_MDIODATA_DEV_ADDR <<
  104. BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF);
  105. v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF);
  106. } else {
  107. v = (device << BCMA_CORE_PCI_MDIODATA_DEVADDR_SHF_OLD);
  108. v |= (address << BCMA_CORE_PCI_MDIODATA_REGADDR_SHF_OLD);
  109. }
  110. v = BCMA_CORE_PCI_MDIODATA_START;
  111. v |= BCMA_CORE_PCI_MDIODATA_WRITE;
  112. v |= BCMA_CORE_PCI_MDIODATA_TA;
  113. v |= data;
  114. pcicore_write32(pc, BCMA_CORE_PCI_MDIO_DATA, v);
  115. /* Wait for the device to complete the transaction */
  116. udelay(10);
  117. for (i = 0; i < max_retries; i++) {
  118. v = pcicore_read32(pc, BCMA_CORE_PCI_MDIO_CONTROL);
  119. if (v & BCMA_CORE_PCI_MDIOCTL_ACCESS_DONE)
  120. break;
  121. msleep(1);
  122. }
  123. pcicore_write32(pc, BCMA_CORE_PCI_MDIO_CONTROL, 0);
  124. }
  125. /**************************************************
  126. * Workarounds.
  127. **************************************************/
  128. static u8 bcma_pcicore_polarity_workaround(struct bcma_drv_pci *pc)
  129. {
  130. u32 tmp;
  131. tmp = bcma_pcie_read(pc, BCMA_CORE_PCI_PLP_STATUSREG);
  132. if (tmp & BCMA_CORE_PCI_PLP_POLARITYINV_STAT)
  133. return BCMA_CORE_PCI_SERDES_RX_CTRL_FORCE |
  134. BCMA_CORE_PCI_SERDES_RX_CTRL_POLARITY;
  135. else
  136. return BCMA_CORE_PCI_SERDES_RX_CTRL_FORCE;
  137. }
  138. static void bcma_pcicore_serdes_workaround(struct bcma_drv_pci *pc)
  139. {
  140. u16 tmp;
  141. bcma_pcie_mdio_write(pc, BCMA_CORE_PCI_MDIODATA_DEV_RX,
  142. BCMA_CORE_PCI_SERDES_RX_CTRL,
  143. bcma_pcicore_polarity_workaround(pc));
  144. tmp = bcma_pcie_mdio_read(pc, BCMA_CORE_PCI_MDIODATA_DEV_PLL,
  145. BCMA_CORE_PCI_SERDES_PLL_CTRL);
  146. if (tmp & BCMA_CORE_PCI_PLL_CTRL_FREQDET_EN)
  147. bcma_pcie_mdio_write(pc, BCMA_CORE_PCI_MDIODATA_DEV_PLL,
  148. BCMA_CORE_PCI_SERDES_PLL_CTRL,
  149. tmp & ~BCMA_CORE_PCI_PLL_CTRL_FREQDET_EN);
  150. }
  151. /**************************************************
  152. * Init.
  153. **************************************************/
  154. static void __devinit bcma_core_pci_clientmode_init(struct bcma_drv_pci *pc)
  155. {
  156. bcma_pcicore_serdes_workaround(pc);
  157. }
  158. static bool __devinit bcma_core_pci_is_in_hostmode(struct bcma_drv_pci *pc)
  159. {
  160. struct bcma_bus *bus = pc->core->bus;
  161. u16 chipid_top;
  162. chipid_top = (bus->chipinfo.id & 0xFF00);
  163. if (chipid_top != 0x4700 &&
  164. chipid_top != 0x5300)
  165. return false;
  166. #ifdef CONFIG_SSB_DRIVER_PCICORE
  167. if (bus->sprom.boardflags_lo & SSB_BFL_NOPCI)
  168. return false;
  169. #endif /* CONFIG_SSB_DRIVER_PCICORE */
  170. #if 0
  171. /* TODO: on BCMA we use address from EROM instead of magic formula */
  172. u32 tmp;
  173. return !mips_busprobe32(tmp, (bus->mmio +
  174. (pc->core->core_index * BCMA_CORE_SIZE)));
  175. #endif
  176. return true;
  177. }
  178. void __devinit bcma_core_pci_init(struct bcma_drv_pci *pc)
  179. {
  180. if (pc->setup_done)
  181. return;
  182. if (bcma_core_pci_is_in_hostmode(pc)) {
  183. #ifdef CONFIG_BCMA_DRIVER_PCI_HOSTMODE
  184. bcma_core_pci_hostmode_init(pc);
  185. #else
  186. pr_err("Driver compiled without support for hostmode PCI\n");
  187. #endif /* CONFIG_BCMA_DRIVER_PCI_HOSTMODE */
  188. } else {
  189. bcma_core_pci_clientmode_init(pc);
  190. }
  191. pc->setup_done = true;
  192. }
  193. int bcma_core_pci_irq_ctl(struct bcma_drv_pci *pc, struct bcma_device *core,
  194. bool enable)
  195. {
  196. struct pci_dev *pdev = pc->core->bus->host_pci;
  197. u32 coremask, tmp;
  198. int err = 0;
  199. if (core->bus->hosttype != BCMA_HOSTTYPE_PCI) {
  200. /* This bcma device is not on a PCI host-bus. So the IRQs are
  201. * not routed through the PCI core.
  202. * So we must not enable routing through the PCI core. */
  203. goto out;
  204. }
  205. err = pci_read_config_dword(pdev, BCMA_PCI_IRQMASK, &tmp);
  206. if (err)
  207. goto out;
  208. coremask = BIT(core->core_index) << 8;
  209. if (enable)
  210. tmp |= coremask;
  211. else
  212. tmp &= ~coremask;
  213. err = pci_write_config_dword(pdev, BCMA_PCI_IRQMASK, tmp);
  214. out:
  215. return err;
  216. }
  217. EXPORT_SYMBOL_GPL(bcma_core_pci_irq_ctl);