pcie.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * arch/arm/plat-orion/pcie.c
  3. *
  4. * Marvell Orion SoC PCIe handling.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/pci.h>
  12. #include <linux/mbus.h>
  13. #include <asm/mach/pci.h>
  14. #include <asm/plat-orion/pcie.h>
  15. /*
  16. * PCIe unit register offsets.
  17. */
  18. #define PCIE_DEV_ID_OFF 0x0000
  19. #define PCIE_CMD_OFF 0x0004
  20. #define PCIE_DEV_REV_OFF 0x0008
  21. #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
  22. #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
  23. #define PCIE_HEADER_LOG_4_OFF 0x0128
  24. #define PCIE_BAR_CTRL_OFF(n) (0x1804 + ((n - 1) * 4))
  25. #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
  26. #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
  27. #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
  28. #define PCIE_WIN5_CTRL_OFF 0x1880
  29. #define PCIE_WIN5_BASE_OFF 0x1884
  30. #define PCIE_WIN5_REMAP_OFF 0x188c
  31. #define PCIE_CONF_ADDR_OFF 0x18f8
  32. #define PCIE_CONF_ADDR_EN 0x80000000
  33. #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
  34. #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
  35. #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
  36. #define PCIE_CONF_FUNC(f) (((f) & 0x3) << 8)
  37. #define PCIE_CONF_DATA_OFF 0x18fc
  38. #define PCIE_MASK_OFF 0x1910
  39. #define PCIE_CTRL_OFF 0x1a00
  40. #define PCIE_STAT_OFF 0x1a04
  41. #define PCIE_STAT_DEV_OFFS 20
  42. #define PCIE_STAT_DEV_MASK 0x1f
  43. #define PCIE_STAT_BUS_OFFS 8
  44. #define PCIE_STAT_BUS_MASK 0xff
  45. #define PCIE_STAT_LINK_DOWN 1
  46. u32 __init orion_pcie_dev_id(void __iomem *base)
  47. {
  48. return readl(base + PCIE_DEV_ID_OFF) >> 16;
  49. }
  50. u32 __init orion_pcie_rev(void __iomem *base)
  51. {
  52. return readl(base + PCIE_DEV_REV_OFF) & 0xff;
  53. }
  54. int orion_pcie_link_up(void __iomem *base)
  55. {
  56. return !(readl(base + PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
  57. }
  58. int orion_pcie_get_local_bus_nr(void __iomem *base)
  59. {
  60. u32 stat = readl(base + PCIE_STAT_OFF);
  61. return (stat >> PCIE_STAT_BUS_OFFS) & PCIE_STAT_BUS_MASK;
  62. }
  63. void __init orion_pcie_set_local_bus_nr(void __iomem *base, int nr)
  64. {
  65. u32 stat;
  66. stat = readl(base + PCIE_STAT_OFF);
  67. stat &= ~(PCIE_STAT_BUS_MASK << PCIE_STAT_BUS_OFFS);
  68. stat |= nr << PCIE_STAT_BUS_OFFS;
  69. writel(stat, base + PCIE_STAT_OFF);
  70. }
  71. /*
  72. * Setup PCIE BARs and Address Decode Wins:
  73. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  74. * WIN[0-3] -> DRAM bank[0-3]
  75. */
  76. static void __init orion_pcie_setup_wins(void __iomem *base,
  77. struct mbus_dram_target_info *dram)
  78. {
  79. u32 size;
  80. int i;
  81. /*
  82. * First, disable and clear BARs and windows.
  83. */
  84. for (i = 1; i <= 2; i++) {
  85. writel(0, base + PCIE_BAR_CTRL_OFF(i));
  86. writel(0, base + PCIE_BAR_LO_OFF(i));
  87. writel(0, base + PCIE_BAR_HI_OFF(i));
  88. }
  89. for (i = 0; i < 5; i++) {
  90. writel(0, base + PCIE_WIN04_CTRL_OFF(i));
  91. writel(0, base + PCIE_WIN04_BASE_OFF(i));
  92. writel(0, base + PCIE_WIN04_REMAP_OFF(i));
  93. }
  94. writel(0, base + PCIE_WIN5_CTRL_OFF);
  95. writel(0, base + PCIE_WIN5_BASE_OFF);
  96. writel(0, base + PCIE_WIN5_REMAP_OFF);
  97. /*
  98. * Setup windows for DDR banks. Count total DDR size on the fly.
  99. */
  100. size = 0;
  101. for (i = 0; i < dram->num_cs; i++) {
  102. struct mbus_dram_window *cs = dram->cs + i;
  103. writel(cs->base & 0xffff0000, base + PCIE_WIN04_BASE_OFF(i));
  104. writel(0, base + PCIE_WIN04_REMAP_OFF(i));
  105. writel(((cs->size - 1) & 0xffff0000) |
  106. (cs->mbus_attr << 8) |
  107. (dram->mbus_dram_target_id << 4) | 1,
  108. base + PCIE_WIN04_CTRL_OFF(i));
  109. size += cs->size;
  110. }
  111. /*
  112. * Setup BAR[1] to all DRAM banks.
  113. */
  114. writel(dram->cs[0].base, base + PCIE_BAR_LO_OFF(1));
  115. writel(0, base + PCIE_BAR_HI_OFF(1));
  116. writel(((size - 1) & 0xffff0000) | 1, base + PCIE_BAR_CTRL_OFF(1));
  117. }
  118. void __init orion_pcie_setup(void __iomem *base,
  119. struct mbus_dram_target_info *dram)
  120. {
  121. u16 cmd;
  122. u32 mask;
  123. /*
  124. * Point PCIe unit MBUS decode windows to DRAM space.
  125. */
  126. orion_pcie_setup_wins(base, dram);
  127. /*
  128. * Master + slave enable.
  129. */
  130. cmd = readw(base + PCIE_CMD_OFF);
  131. cmd |= PCI_COMMAND_IO;
  132. cmd |= PCI_COMMAND_MEMORY;
  133. cmd |= PCI_COMMAND_MASTER;
  134. writew(cmd, base + PCIE_CMD_OFF);
  135. /*
  136. * Enable interrupt lines A-D.
  137. */
  138. mask = readl(base + PCIE_MASK_OFF);
  139. mask |= 0x0f000000;
  140. writel(mask, base + PCIE_MASK_OFF);
  141. }
  142. int orion_pcie_rd_conf(void __iomem *base, struct pci_bus *bus,
  143. u32 devfn, int where, int size, u32 *val)
  144. {
  145. writel(PCIE_CONF_BUS(bus->number) |
  146. PCIE_CONF_DEV(PCI_SLOT(devfn)) |
  147. PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
  148. PCIE_CONF_REG(where) | PCIE_CONF_ADDR_EN,
  149. base + PCIE_CONF_ADDR_OFF);
  150. *val = readl(base + PCIE_CONF_DATA_OFF);
  151. if (size == 1)
  152. *val = (*val >> (8 * (where & 3))) & 0xff;
  153. else if (size == 2)
  154. *val = (*val >> (8 * (where & 3))) & 0xffff;
  155. return PCIBIOS_SUCCESSFUL;
  156. }
  157. int orion_pcie_rd_conf_tlp(void __iomem *base, struct pci_bus *bus,
  158. u32 devfn, int where, int size, u32 *val)
  159. {
  160. writel(PCIE_CONF_BUS(bus->number) |
  161. PCIE_CONF_DEV(PCI_SLOT(devfn)) |
  162. PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
  163. PCIE_CONF_REG(where) | PCIE_CONF_ADDR_EN,
  164. base + PCIE_CONF_ADDR_OFF);
  165. *val = readl(base + PCIE_CONF_DATA_OFF);
  166. if (bus->number != orion_pcie_get_local_bus_nr(base) ||
  167. PCI_FUNC(devfn) != 0)
  168. *val = readl(base + PCIE_HEADER_LOG_4_OFF);
  169. if (size == 1)
  170. *val = (*val >> (8 * (where & 3))) & 0xff;
  171. else if (size == 2)
  172. *val = (*val >> (8 * (where & 3))) & 0xffff;
  173. return PCIBIOS_SUCCESSFUL;
  174. }
  175. int orion_pcie_rd_conf_wa(void __iomem *wa_base, struct pci_bus *bus,
  176. u32 devfn, int where, int size, u32 *val)
  177. {
  178. *val = readl(wa_base + (PCIE_CONF_BUS(bus->number) |
  179. PCIE_CONF_DEV(PCI_SLOT(devfn)) |
  180. PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
  181. PCIE_CONF_REG(where)));
  182. if (size == 1)
  183. *val = (*val >> (8 * (where & 3))) & 0xff;
  184. else if (size == 2)
  185. *val = (*val >> (8 * (where & 3))) & 0xffff;
  186. return PCIBIOS_SUCCESSFUL;
  187. }
  188. int orion_pcie_wr_conf(void __iomem *base, struct pci_bus *bus,
  189. u32 devfn, int where, int size, u32 val)
  190. {
  191. int ret = PCIBIOS_SUCCESSFUL;
  192. writel(PCIE_CONF_BUS(bus->number) |
  193. PCIE_CONF_DEV(PCI_SLOT(devfn)) |
  194. PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
  195. PCIE_CONF_REG(where) | PCIE_CONF_ADDR_EN,
  196. base + PCIE_CONF_ADDR_OFF);
  197. if (size == 4) {
  198. writel(val, base + PCIE_CONF_DATA_OFF);
  199. } else if (size == 2) {
  200. writew(val, base + PCIE_CONF_DATA_OFF + (where & 3));
  201. } else if (size == 1) {
  202. writeb(val, base + PCIE_CONF_DATA_OFF + (where & 3));
  203. } else {
  204. ret = PCIBIOS_BAD_REGISTER_NUMBER;
  205. }
  206. return ret;
  207. }