pcie.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) ARM Ltd 2015
  3. *
  4. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  5. *
  6. * SPDX-Licence-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <asm/io.h>
  10. #include <linux/bitops.h>
  11. #include <pci_ids.h>
  12. #include "pcie.h"
  13. /* XpressRICH3 support */
  14. #define XR3_CONFIG_BASE 0x7ff30000
  15. #define XR3_RESET_BASE 0x7ff20000
  16. #define XR3_PCI_ECAM_START 0x40000000
  17. #define XR3_PCI_ECAM_SIZE 28 /* as power of 2 = 0x10000000 */
  18. #define XR3_PCI_IOSPACE_START 0x5f800000
  19. #define XR3_PCI_IOSPACE_SIZE 23 /* as power of 2 = 0x800000 */
  20. #define XR3_PCI_MEMSPACE_START 0x50000000
  21. #define XR3_PCI_MEMSPACE_SIZE 27 /* as power of 2 = 0x8000000 */
  22. #define XR3_PCI_MEMSPACE64_START 0x4000000000
  23. #define XR3_PCI_MEMSPACE64_SIZE 33 /* as power of 2 = 0x200000000 */
  24. #define JUNO_V2M_MSI_START 0x2c1c0000
  25. #define JUNO_V2M_MSI_SIZE 12 /* as power of 2 = 4096 */
  26. #define XR3PCI_BASIC_STATUS 0x18
  27. #define XR3PCI_BS_GEN_MASK (0xf << 8)
  28. #define XR3PCI_BS_LINK_MASK 0xff
  29. #define XR3PCI_VIRTCHAN_CREDITS 0x90
  30. #define XR3PCI_BRIDGE_PCI_IDS 0x9c
  31. #define XR3PCI_PEX_SPC2 0xd8
  32. #define XR3PCI_ATR_PCIE_WIN0 0x600
  33. #define XR3PCI_ATR_PCIE_WIN1 0x700
  34. #define XR3PCI_ATR_AXI4_SLV0 0x800
  35. #define XR3PCI_ATR_TABLE_SIZE 0x20
  36. #define XR3PCI_ATR_SRC_ADDR_LOW 0x0
  37. #define XR3PCI_ATR_SRC_ADDR_HIGH 0x4
  38. #define XR3PCI_ATR_TRSL_ADDR_LOW 0x8
  39. #define XR3PCI_ATR_TRSL_ADDR_HIGH 0xc
  40. #define XR3PCI_ATR_TRSL_PARAM 0x10
  41. /* IDs used in the XR3PCI_ATR_TRSL_PARAM */
  42. #define XR3PCI_ATR_TRSLID_AXIDEVICE (0x420004)
  43. #define XR3PCI_ATR_TRSLID_AXIMEMORY (0x4e0004) /* Write-through, read/write allocate */
  44. #define XR3PCI_ATR_TRSLID_PCIE_CONF (0x000001)
  45. #define XR3PCI_ATR_TRSLID_PCIE_IO (0x020000)
  46. #define XR3PCI_ATR_TRSLID_PCIE_MEMORY (0x000000)
  47. #define XR3PCI_ECAM_OFFSET(b, d, o) (((b) << 20) | \
  48. (PCI_SLOT(d) << 15) | \
  49. (PCI_FUNC(d) << 12) | o)
  50. #define JUNO_RESET_CTRL 0x1004
  51. #define JUNO_RESET_CTRL_PHY BIT(0)
  52. #define JUNO_RESET_CTRL_RC BIT(1)
  53. #define JUNO_RESET_STATUS 0x1008
  54. #define JUNO_RESET_STATUS_PLL BIT(0)
  55. #define JUNO_RESET_STATUS_PHY BIT(1)
  56. #define JUNO_RESET_STATUS_RC BIT(2)
  57. #define JUNO_RESET_STATUS_MASK (JUNO_RESET_STATUS_PLL | \
  58. JUNO_RESET_STATUS_PHY | \
  59. JUNO_RESET_STATUS_RC)
  60. void xr3pci_set_atr_entry(unsigned long base, unsigned long src_addr,
  61. unsigned long trsl_addr, int window_size,
  62. int trsl_param)
  63. {
  64. /* X3PCI_ATR_SRC_ADDR_LOW:
  65. - bit 0: enable entry,
  66. - bits 1-6: ATR window size: total size in bytes: 2^(ATR_WSIZE + 1)
  67. - bits 7-11: reserved
  68. - bits 12-31: start of source address
  69. */
  70. writel((u32)(src_addr & 0xfffff000) | (window_size - 1) << 1 | 1,
  71. base + XR3PCI_ATR_SRC_ADDR_LOW);
  72. writel((u32)(src_addr >> 32), base + XR3PCI_ATR_SRC_ADDR_HIGH);
  73. writel((u32)(trsl_addr & 0xfffff000), base + XR3PCI_ATR_TRSL_ADDR_LOW);
  74. writel((u32)(trsl_addr >> 32), base + XR3PCI_ATR_TRSL_ADDR_HIGH);
  75. writel(trsl_param, base + XR3PCI_ATR_TRSL_PARAM);
  76. debug("ATR entry: 0x%010lx %s 0x%010lx [0x%010llx] (param: 0x%06x)\n",
  77. src_addr, (trsl_param & 0x400000) ? "<-" : "->", trsl_addr,
  78. ((u64)1) << window_size, trsl_param);
  79. }
  80. void xr3pci_setup_atr(void)
  81. {
  82. /* setup PCIe to CPU address translation tables */
  83. unsigned long base = XR3_CONFIG_BASE + XR3PCI_ATR_PCIE_WIN0;
  84. /* forward all writes from PCIe to GIC V2M (used for MSI) */
  85. xr3pci_set_atr_entry(base, JUNO_V2M_MSI_START, JUNO_V2M_MSI_START,
  86. JUNO_V2M_MSI_SIZE, XR3PCI_ATR_TRSLID_AXIDEVICE);
  87. base += XR3PCI_ATR_TABLE_SIZE;
  88. /* PCIe devices can write anywhere in memory */
  89. xr3pci_set_atr_entry(base, PHYS_SDRAM_1, PHYS_SDRAM_1,
  90. 31 /* grant access to all RAM under 4GB */,
  91. XR3PCI_ATR_TRSLID_AXIMEMORY);
  92. base += XR3PCI_ATR_TABLE_SIZE;
  93. xr3pci_set_atr_entry(base, PHYS_SDRAM_2, PHYS_SDRAM_2,
  94. XR3_PCI_MEMSPACE64_SIZE,
  95. XR3PCI_ATR_TRSLID_AXIMEMORY);
  96. /* setup CPU to PCIe address translation table */
  97. base = XR3_CONFIG_BASE + XR3PCI_ATR_AXI4_SLV0;
  98. /* setup ECAM space to bus configuration interface */
  99. xr3pci_set_atr_entry(base, XR3_PCI_ECAM_START, 0, XR3_PCI_ECAM_SIZE,
  100. XR3PCI_ATR_TRSLID_PCIE_CONF);
  101. base += XR3PCI_ATR_TABLE_SIZE;
  102. /* setup IO space translation */
  103. xr3pci_set_atr_entry(base, XR3_PCI_IOSPACE_START, 0,
  104. XR3_PCI_IOSPACE_SIZE, XR3PCI_ATR_TRSLID_PCIE_IO);
  105. base += XR3PCI_ATR_TABLE_SIZE;
  106. /* setup 32bit MEM space translation */
  107. xr3pci_set_atr_entry(base, XR3_PCI_MEMSPACE_START, XR3_PCI_MEMSPACE_START,
  108. XR3_PCI_MEMSPACE_SIZE, XR3PCI_ATR_TRSLID_PCIE_MEMORY);
  109. base += XR3PCI_ATR_TABLE_SIZE;
  110. /* setup 64bit MEM space translation */
  111. xr3pci_set_atr_entry(base, XR3_PCI_MEMSPACE64_START, XR3_PCI_MEMSPACE64_START,
  112. XR3_PCI_MEMSPACE64_SIZE, XR3PCI_ATR_TRSLID_PCIE_MEMORY);
  113. }
  114. void xr3pci_init(void)
  115. {
  116. u32 val;
  117. int timeout = 200;
  118. /* Initialise the XpressRICH3 PCIe host bridge */
  119. /* add credits */
  120. writel(0x00f0b818, XR3_CONFIG_BASE + XR3PCI_VIRTCHAN_CREDITS);
  121. writel(0x1, XR3_CONFIG_BASE + XR3PCI_VIRTCHAN_CREDITS + 4);
  122. /* allow ECRC */
  123. writel(0x6006, XR3_CONFIG_BASE + XR3PCI_PEX_SPC2);
  124. /* setup the correct class code for the host bridge */
  125. writel(PCI_CLASS_BRIDGE_PCI << 16, XR3_CONFIG_BASE + XR3PCI_BRIDGE_PCI_IDS);
  126. /* reset phy and root complex */
  127. writel(JUNO_RESET_CTRL_PHY | JUNO_RESET_CTRL_RC,
  128. XR3_RESET_BASE + JUNO_RESET_CTRL);
  129. do {
  130. mdelay(1);
  131. val = readl(XR3_RESET_BASE + JUNO_RESET_STATUS);
  132. } while (--timeout &&
  133. (val & JUNO_RESET_STATUS_MASK) != JUNO_RESET_STATUS_MASK);
  134. if (!timeout) {
  135. printf("PCI XR3 Root complex reset timed out\n");
  136. return;
  137. }
  138. /* Wait for the link to train */
  139. mdelay(20);
  140. timeout = 20;
  141. do {
  142. mdelay(1);
  143. val = readl(XR3_CONFIG_BASE + XR3PCI_BASIC_STATUS);
  144. } while (--timeout && !(val & XR3PCI_BS_LINK_MASK));
  145. if (!(val & XR3PCI_BS_LINK_MASK)) {
  146. printf("Failed to negotiate a link!\n");
  147. return;
  148. }
  149. printf("PCIe XR3 Host Bridge enabled: x%d link (Gen %d)\n",
  150. val & XR3PCI_BS_LINK_MASK, (val & XR3PCI_BS_GEN_MASK) >> 8);
  151. xr3pci_setup_atr();
  152. }
  153. void vexpress64_pcie_init(void)
  154. {
  155. xr3pci_init();
  156. }