pci_mvebu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * PCIe driver for Marvell MVEBU SoCs
  3. *
  4. * Based on Barebox drivers/pci/pci-mvebu.c
  5. *
  6. * Ported to U-Boot by:
  7. * Anton Schubert <anton.schubert@gmx.de>
  8. * Stefan Roese <sr@denx.de>
  9. *
  10. * SPDX-License-Identifier: GPL-2.0
  11. */
  12. #include <common.h>
  13. #include <pci.h>
  14. #include <linux/errno.h>
  15. #include <asm/io.h>
  16. #include <asm/arch/cpu.h>
  17. #include <asm/arch/soc.h>
  18. #include <linux/mbus.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /* PCIe unit register offsets */
  21. #define SELECT(x, n) ((x >> n) & 1UL)
  22. #define PCIE_DEV_ID_OFF 0x0000
  23. #define PCIE_CMD_OFF 0x0004
  24. #define PCIE_DEV_REV_OFF 0x0008
  25. #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
  26. #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
  27. #define PCIE_CAPAB_OFF 0x0060
  28. #define PCIE_CTRL_STAT_OFF 0x0068
  29. #define PCIE_HEADER_LOG_4_OFF 0x0128
  30. #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
  31. #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
  32. #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
  33. #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
  34. #define PCIE_WIN5_CTRL_OFF 0x1880
  35. #define PCIE_WIN5_BASE_OFF 0x1884
  36. #define PCIE_WIN5_REMAP_OFF 0x188c
  37. #define PCIE_CONF_ADDR_OFF 0x18f8
  38. #define PCIE_CONF_ADDR_EN BIT(31)
  39. #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
  40. #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
  41. #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
  42. #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
  43. #define PCIE_CONF_ADDR(dev, reg) \
  44. (PCIE_CONF_BUS(PCI_BUS(dev)) | PCIE_CONF_DEV(PCI_DEV(dev)) | \
  45. PCIE_CONF_FUNC(PCI_FUNC(dev)) | PCIE_CONF_REG(reg) | \
  46. PCIE_CONF_ADDR_EN)
  47. #define PCIE_CONF_DATA_OFF 0x18fc
  48. #define PCIE_MASK_OFF 0x1910
  49. #define PCIE_MASK_ENABLE_INTS (0xf << 24)
  50. #define PCIE_CTRL_OFF 0x1a00
  51. #define PCIE_CTRL_X1_MODE BIT(0)
  52. #define PCIE_STAT_OFF 0x1a04
  53. #define PCIE_STAT_BUS (0xff << 8)
  54. #define PCIE_STAT_DEV (0x1f << 16)
  55. #define PCIE_STAT_LINK_DOWN BIT(0)
  56. #define PCIE_DEBUG_CTRL 0x1a60
  57. #define PCIE_DEBUG_SOFT_RESET BIT(20)
  58. struct resource {
  59. u32 start;
  60. u32 end;
  61. };
  62. struct mvebu_pcie {
  63. struct pci_controller hose;
  64. char *name;
  65. void __iomem *base;
  66. void __iomem *membase;
  67. struct resource mem;
  68. void __iomem *iobase;
  69. u32 port;
  70. u32 lane;
  71. u32 lane_mask;
  72. pci_dev_t dev;
  73. };
  74. #define to_pcie(_hc) container_of(_hc, struct mvebu_pcie, pci)
  75. /*
  76. * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
  77. * into SoCs address space. Each controller will map 128M of MEM
  78. * and 64K of I/O space when registered.
  79. */
  80. static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
  81. #define PCIE_MEM_SIZE (128 << 20)
  82. #if defined(CONFIG_ARMADA_38X)
  83. #define PCIE_BASE(if) \
  84. ((if) == 0 ? \
  85. MVEBU_REG_PCIE0_BASE : \
  86. (MVEBU_REG_PCIE_BASE + 0x4000 * (if - 1)))
  87. /*
  88. * On A38x MV6820 these PEX ports are supported:
  89. * 0 - Port 0.0
  90. * 1 - Port 1.0
  91. * 2 - Port 2.0
  92. * 3 - Port 3.0
  93. */
  94. #define MAX_PEX 4
  95. static struct mvebu_pcie pcie_bus[MAX_PEX];
  96. static void mvebu_get_port_lane(struct mvebu_pcie *pcie, int pex_idx,
  97. int *mem_target, int *mem_attr)
  98. {
  99. u8 port[] = { 0, 1, 2, 3 };
  100. u8 lane[] = { 0, 0, 0, 0 };
  101. u8 target[] = { 8, 4, 4, 4 };
  102. u8 attr[] = { 0xe8, 0xe8, 0xd8, 0xb8 };
  103. pcie->port = port[pex_idx];
  104. pcie->lane = lane[pex_idx];
  105. *mem_target = target[pex_idx];
  106. *mem_attr = attr[pex_idx];
  107. }
  108. #else
  109. #define PCIE_BASE(if) \
  110. ((if) < 8 ? \
  111. (MVEBU_REG_PCIE_BASE + ((if) / 4) * 0x40000 + ((if) % 4) * 0x4000) : \
  112. (MVEBU_REG_PCIE_BASE + 0x2000 + ((if) % 8) * 0x40000))
  113. /*
  114. * On AXP MV78460 these PEX ports are supported:
  115. * 0 - Port 0.0
  116. * 1 - Port 0.1
  117. * 2 - Port 0.2
  118. * 3 - Port 0.3
  119. * 4 - Port 1.0
  120. * 5 - Port 1.1
  121. * 6 - Port 1.2
  122. * 7 - Port 1.3
  123. * 8 - Port 2.0
  124. * 9 - Port 3.0
  125. */
  126. #define MAX_PEX 10
  127. static struct mvebu_pcie pcie_bus[MAX_PEX];
  128. static void mvebu_get_port_lane(struct mvebu_pcie *pcie, int pex_idx,
  129. int *mem_target, int *mem_attr)
  130. {
  131. u8 port[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 3 };
  132. u8 lane[] = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 0 };
  133. u8 target[] = { 4, 4, 4, 4, 8, 8, 8, 8, 4, 8 };
  134. u8 attr[] = { 0xe8, 0xd8, 0xb8, 0x78,
  135. 0xe8, 0xd8, 0xb8, 0x78,
  136. 0xf8, 0xf8 };
  137. pcie->port = port[pex_idx];
  138. pcie->lane = lane[pex_idx];
  139. *mem_target = target[pex_idx];
  140. *mem_attr = attr[pex_idx];
  141. }
  142. #endif
  143. static int mvebu_pex_unit_is_x4(int pex_idx)
  144. {
  145. int pex_unit = pex_idx < 9 ? pex_idx >> 2 : 3;
  146. u32 mask = (0x0f << (pex_unit * 8));
  147. return (readl(COMPHY_REFCLK_ALIGNMENT) & mask) == mask;
  148. }
  149. static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
  150. {
  151. u32 val;
  152. val = readl(pcie->base + PCIE_STAT_OFF);
  153. return !(val & PCIE_STAT_LINK_DOWN);
  154. }
  155. static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
  156. {
  157. u32 stat;
  158. stat = readl(pcie->base + PCIE_STAT_OFF);
  159. stat &= ~PCIE_STAT_BUS;
  160. stat |= busno << 8;
  161. writel(stat, pcie->base + PCIE_STAT_OFF);
  162. }
  163. static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
  164. {
  165. u32 stat;
  166. stat = readl(pcie->base + PCIE_STAT_OFF);
  167. stat &= ~PCIE_STAT_DEV;
  168. stat |= devno << 16;
  169. writel(stat, pcie->base + PCIE_STAT_OFF);
  170. }
  171. static int mvebu_pcie_get_local_bus_nr(struct mvebu_pcie *pcie)
  172. {
  173. u32 stat;
  174. stat = readl(pcie->base + PCIE_STAT_OFF);
  175. return (stat & PCIE_STAT_BUS) >> 8;
  176. }
  177. static int mvebu_pcie_get_local_dev_nr(struct mvebu_pcie *pcie)
  178. {
  179. u32 stat;
  180. stat = readl(pcie->base + PCIE_STAT_OFF);
  181. return (stat & PCIE_STAT_DEV) >> 16;
  182. }
  183. static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
  184. {
  185. return container_of(hose, struct mvebu_pcie, hose);
  186. }
  187. static int mvebu_pcie_read_config_dword(struct pci_controller *hose,
  188. pci_dev_t dev, int offset, u32 *val)
  189. {
  190. struct mvebu_pcie *pcie = hose_to_pcie(hose);
  191. int local_bus = PCI_BUS(pcie->dev);
  192. int local_dev = PCI_DEV(pcie->dev);
  193. u32 reg;
  194. /* Only allow one other device besides the local one on the local bus */
  195. if (PCI_BUS(dev) == local_bus && PCI_DEV(dev) != local_dev) {
  196. if (local_dev == 0 && PCI_DEV(dev) != 1) {
  197. /*
  198. * If local dev is 0, the first other dev can
  199. * only be 1
  200. */
  201. *val = 0xffffffff;
  202. return 1;
  203. } else if (local_dev != 0 && PCI_DEV(dev) != 0) {
  204. /*
  205. * If local dev is not 0, the first other dev can
  206. * only be 0
  207. */
  208. *val = 0xffffffff;
  209. return 1;
  210. }
  211. }
  212. /* write address */
  213. reg = PCIE_CONF_ADDR(dev, offset);
  214. writel(reg, pcie->base + PCIE_CONF_ADDR_OFF);
  215. *val = readl(pcie->base + PCIE_CONF_DATA_OFF);
  216. return 0;
  217. }
  218. static int mvebu_pcie_write_config_dword(struct pci_controller *hose,
  219. pci_dev_t dev, int offset, u32 val)
  220. {
  221. struct mvebu_pcie *pcie = hose_to_pcie(hose);
  222. int local_bus = PCI_BUS(pcie->dev);
  223. int local_dev = PCI_DEV(pcie->dev);
  224. /* Only allow one other device besides the local one on the local bus */
  225. if (PCI_BUS(dev) == local_bus && PCI_DEV(dev) != local_dev) {
  226. if (local_dev == 0 && PCI_DEV(dev) != 1) {
  227. /*
  228. * If local dev is 0, the first other dev can
  229. * only be 1
  230. */
  231. return 1;
  232. } else if (local_dev != 0 && PCI_DEV(dev) != 0) {
  233. /*
  234. * If local dev is not 0, the first other dev can
  235. * only be 0
  236. */
  237. return 1;
  238. }
  239. }
  240. writel(PCIE_CONF_ADDR(dev, offset), pcie->base + PCIE_CONF_ADDR_OFF);
  241. writel(val, pcie->base + PCIE_CONF_DATA_OFF);
  242. return 0;
  243. }
  244. /*
  245. * Setup PCIE BARs and Address Decode Wins:
  246. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  247. * WIN[0-3] -> DRAM bank[0-3]
  248. */
  249. static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
  250. {
  251. const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
  252. u32 size;
  253. int i;
  254. /* First, disable and clear BARs and windows. */
  255. for (i = 1; i < 3; i++) {
  256. writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
  257. writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
  258. writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
  259. }
  260. for (i = 0; i < 5; i++) {
  261. writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
  262. writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
  263. writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
  264. }
  265. writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
  266. writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
  267. writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
  268. /* Setup windows for DDR banks. Count total DDR size on the fly. */
  269. size = 0;
  270. for (i = 0; i < dram->num_cs; i++) {
  271. const struct mbus_dram_window *cs = dram->cs + i;
  272. writel(cs->base & 0xffff0000,
  273. pcie->base + PCIE_WIN04_BASE_OFF(i));
  274. writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
  275. writel(((cs->size - 1) & 0xffff0000) |
  276. (cs->mbus_attr << 8) |
  277. (dram->mbus_dram_target_id << 4) | 1,
  278. pcie->base + PCIE_WIN04_CTRL_OFF(i));
  279. size += cs->size;
  280. }
  281. /* Round up 'size' to the nearest power of two. */
  282. if ((size & (size - 1)) != 0)
  283. size = 1 << fls(size);
  284. /* Setup BAR[1] to all DRAM banks. */
  285. writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
  286. writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
  287. writel(((size - 1) & 0xffff0000) | 0x1,
  288. pcie->base + PCIE_BAR_CTRL_OFF(1));
  289. }
  290. void pci_init_board(void)
  291. {
  292. int mem_target, mem_attr, i;
  293. int bus = 0;
  294. u32 reg;
  295. u32 soc_ctrl = readl(MVEBU_SYSTEM_REG_BASE + 0x4);
  296. /* Check SoC Control Power State */
  297. debug("%s: SoC Control %08x, 0en %01lx, 1en %01lx, 2en %01lx\n",
  298. __func__, soc_ctrl, SELECT(soc_ctrl, 0), SELECT(soc_ctrl, 1),
  299. SELECT(soc_ctrl, 2));
  300. for (i = 0; i < MAX_PEX; i++) {
  301. struct mvebu_pcie *pcie = &pcie_bus[i];
  302. struct pci_controller *hose = &pcie->hose;
  303. /* Get port number, lane number and memory target / attr */
  304. mvebu_get_port_lane(pcie, i, &mem_target, &mem_attr);
  305. /* Don't read at all from pci registers if port power is down */
  306. if (SELECT(soc_ctrl, pcie->port) == 0) {
  307. if (pcie->lane == 0)
  308. debug("%s: skipping port %d\n", __func__, pcie->port);
  309. continue;
  310. }
  311. pcie->base = (void __iomem *)PCIE_BASE(i);
  312. /* Check link and skip ports that have no link */
  313. if (!mvebu_pcie_link_up(pcie)) {
  314. debug("%s: PCIe %d.%d - down\n", __func__,
  315. pcie->port, pcie->lane);
  316. continue;
  317. }
  318. debug("%s: PCIe %d.%d - up, base %08x\n", __func__,
  319. pcie->port, pcie->lane, (u32)pcie->base);
  320. /* Read Id info and local bus/dev */
  321. debug("direct conf read %08x, local bus %d, local dev %d\n",
  322. readl(pcie->base), mvebu_pcie_get_local_bus_nr(pcie),
  323. mvebu_pcie_get_local_dev_nr(pcie));
  324. mvebu_pcie_set_local_bus_nr(pcie, bus);
  325. mvebu_pcie_set_local_dev_nr(pcie, 0);
  326. pcie->dev = PCI_BDF(bus, 0, 0);
  327. pcie->mem.start = (u32)mvebu_pcie_membase;
  328. pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
  329. mvebu_pcie_membase += PCIE_MEM_SIZE;
  330. if (mvebu_mbus_add_window_by_id(mem_target, mem_attr,
  331. (phys_addr_t)pcie->mem.start,
  332. PCIE_MEM_SIZE)) {
  333. printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
  334. (u32)pcie->mem.start, PCIE_MEM_SIZE);
  335. }
  336. /* Setup windows and configure host bridge */
  337. mvebu_pcie_setup_wins(pcie);
  338. /* Master + slave enable. */
  339. reg = readl(pcie->base + PCIE_CMD_OFF);
  340. reg |= PCI_COMMAND_MEMORY;
  341. reg |= PCI_COMMAND_MASTER;
  342. reg |= BIT(10); /* disable interrupts */
  343. writel(reg, pcie->base + PCIE_CMD_OFF);
  344. /* Setup U-Boot PCI Controller */
  345. hose->first_busno = 0;
  346. hose->current_busno = bus;
  347. /* PCI memory space */
  348. pci_set_region(hose->regions + 0, pcie->mem.start,
  349. pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
  350. pci_set_region(hose->regions + 1,
  351. 0, 0,
  352. gd->ram_size,
  353. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  354. hose->region_count = 2;
  355. pci_set_ops(hose,
  356. pci_hose_read_config_byte_via_dword,
  357. pci_hose_read_config_word_via_dword,
  358. mvebu_pcie_read_config_dword,
  359. pci_hose_write_config_byte_via_dword,
  360. pci_hose_write_config_word_via_dword,
  361. mvebu_pcie_write_config_dword);
  362. pci_register_hose(hose);
  363. hose->last_busno = pci_hose_scan(hose);
  364. /* Set BAR0 to internal registers */
  365. writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
  366. writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
  367. bus = hose->last_busno + 1;
  368. /* need to skip more for X4 links, otherwise scan will hang */
  369. if (mvebu_soc_family() == MVEBU_SOC_AXP) {
  370. if (mvebu_pex_unit_is_x4(i))
  371. i += 3;
  372. }
  373. }
  374. }