driver_pci.c 5.6 KB

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