driver_pci.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Broadcom specific AMBA
  3. * PCI Core
  4. *
  5. * Copyright 2005, 2011, 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. void __devinit bcma_core_pci_init(struct bcma_drv_pci *pc)
  159. {
  160. if (pc->setup_done)
  161. return;
  162. #ifdef CONFIG_BCMA_DRIVER_PCI_HOSTMODE
  163. pc->hostmode = bcma_core_pci_is_in_hostmode(pc);
  164. if (pc->hostmode)
  165. bcma_core_pci_hostmode_init(pc);
  166. #endif /* CONFIG_BCMA_DRIVER_PCI_HOSTMODE */
  167. if (!pc->hostmode)
  168. bcma_core_pci_clientmode_init(pc);
  169. }
  170. int bcma_core_pci_irq_ctl(struct bcma_drv_pci *pc, struct bcma_device *core,
  171. bool enable)
  172. {
  173. struct pci_dev *pdev = pc->core->bus->host_pci;
  174. u32 coremask, tmp;
  175. int err = 0;
  176. if (core->bus->hosttype != BCMA_HOSTTYPE_PCI) {
  177. /* This bcma device is not on a PCI host-bus. So the IRQs are
  178. * not routed through the PCI core.
  179. * So we must not enable routing through the PCI core. */
  180. goto out;
  181. }
  182. err = pci_read_config_dword(pdev, BCMA_PCI_IRQMASK, &tmp);
  183. if (err)
  184. goto out;
  185. coremask = BIT(core->core_index) << 8;
  186. if (enable)
  187. tmp |= coremask;
  188. else
  189. tmp &= ~coremask;
  190. err = pci_write_config_dword(pdev, BCMA_PCI_IRQMASK, tmp);
  191. out:
  192. return err;
  193. }
  194. EXPORT_SYMBOL_GPL(bcma_core_pci_irq_ctl);