pci_mvebu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 32M 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 (32 << 20)
  82. #if defined(CONFIG_ARMADA_38X)
  83. #define PCIE_BASE(if) \
  84. ((if) == 0 ? \
  85. MVEBU_REG_PCIE_BASE + 0x40000 : \
  86. MVEBU_REG_PCIE_BASE + 0x4000 * (if))
  87. /*
  88. * On A38x MV6820 these PEX ports are supported:
  89. * 0 - Port 0.0
  90. * 1 - Port 0.1
  91. * 2 - Port 0.2
  92. */
  93. #define MAX_PEX 3
  94. static struct mvebu_pcie pcie_bus[MAX_PEX];
  95. static void mvebu_get_port_lane(struct mvebu_pcie *pcie, int pex_idx,
  96. int *mem_target, int *mem_attr)
  97. {
  98. u8 port[] = { 0, 1, 2 };
  99. u8 lane[] = { 0, 0, 0 };
  100. u8 target[] = { 8, 4, 4 };
  101. u8 attr[] = { 0xe8, 0xe8, 0xd8 };
  102. pcie->port = port[pex_idx];
  103. pcie->lane = lane[pex_idx];
  104. *mem_target = target[pex_idx];
  105. *mem_attr = attr[pex_idx];
  106. }
  107. #else
  108. #define PCIE_BASE(if) \
  109. ((if) < 8 ? \
  110. (MVEBU_REG_PCIE_BASE + ((if) / 4) * 0x40000 + ((if) % 4) * 0x4000) : \
  111. (MVEBU_REG_PCIE_BASE + 0x2000 + ((if) % 8) * 0x40000))
  112. /*
  113. * On AXP MV78460 these PEX ports are supported:
  114. * 0 - Port 0.0
  115. * 1 - Port 0.1
  116. * 2 - Port 0.2
  117. * 3 - Port 0.3
  118. * 4 - Port 1.0
  119. * 5 - Port 1.1
  120. * 6 - Port 1.2
  121. * 7 - Port 1.3
  122. * 8 - Port 2.0
  123. * 9 - Port 3.0
  124. */
  125. #define MAX_PEX 10
  126. static struct mvebu_pcie pcie_bus[MAX_PEX];
  127. static void mvebu_get_port_lane(struct mvebu_pcie *pcie, int pex_idx,
  128. int *mem_target, int *mem_attr)
  129. {
  130. u8 port[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 3 };
  131. u8 lane[] = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 0 };
  132. u8 target[] = { 4, 4, 4, 4, 8, 8, 8, 8, 4, 8 };
  133. u8 attr[] = { 0xe8, 0xd8, 0xb8, 0x78,
  134. 0xe8, 0xd8, 0xb8, 0x78,
  135. 0xf8, 0xf8 };
  136. pcie->port = port[pex_idx];
  137. pcie->lane = lane[pex_idx];
  138. *mem_target = target[pex_idx];
  139. *mem_attr = attr[pex_idx];
  140. }
  141. #endif
  142. static int mvebu_pex_unit_is_x4(int pex_idx)
  143. {
  144. int pex_unit = pex_idx < 9 ? pex_idx >> 2 : 3;
  145. u32 mask = (0x0f << (pex_unit * 8));
  146. return (readl(COMPHY_REFCLK_ALIGNMENT) & mask) == mask;
  147. }
  148. static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
  149. {
  150. u32 val;
  151. val = readl(pcie->base + PCIE_STAT_OFF);
  152. return !(val & PCIE_STAT_LINK_DOWN);
  153. }
  154. static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
  155. {
  156. u32 stat;
  157. stat = readl(pcie->base + PCIE_STAT_OFF);
  158. stat &= ~PCIE_STAT_BUS;
  159. stat |= busno << 8;
  160. writel(stat, pcie->base + PCIE_STAT_OFF);
  161. }
  162. static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
  163. {
  164. u32 stat;
  165. stat = readl(pcie->base + PCIE_STAT_OFF);
  166. stat &= ~PCIE_STAT_DEV;
  167. stat |= devno << 16;
  168. writel(stat, pcie->base + PCIE_STAT_OFF);
  169. }
  170. static int mvebu_pcie_get_local_bus_nr(struct mvebu_pcie *pcie)
  171. {
  172. u32 stat;
  173. stat = readl(pcie->base + PCIE_STAT_OFF);
  174. return (stat & PCIE_STAT_BUS) >> 8;
  175. }
  176. static int mvebu_pcie_get_local_dev_nr(struct mvebu_pcie *pcie)
  177. {
  178. u32 stat;
  179. stat = readl(pcie->base + PCIE_STAT_OFF);
  180. return (stat & PCIE_STAT_DEV) >> 16;
  181. }
  182. static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
  183. {
  184. return container_of(hose, struct mvebu_pcie, hose);
  185. }
  186. static int mvebu_pcie_read_config_dword(struct pci_controller *hose,
  187. pci_dev_t dev, int offset, u32 *val)
  188. {
  189. struct mvebu_pcie *pcie = hose_to_pcie(hose);
  190. int local_bus = PCI_BUS(pcie->dev);
  191. int local_dev = PCI_DEV(pcie->dev);
  192. u32 reg;
  193. /* Only allow one other device besides the local one on the local bus */
  194. if (PCI_BUS(dev) == local_bus && PCI_DEV(dev) != local_dev) {
  195. if (local_dev == 0 && PCI_DEV(dev) != 1) {
  196. /*
  197. * If local dev is 0, the first other dev can
  198. * only be 1
  199. */
  200. *val = 0xffffffff;
  201. return 1;
  202. } else if (local_dev != 0 && PCI_DEV(dev) != 0) {
  203. /*
  204. * If local dev is not 0, the first other dev can
  205. * only be 0
  206. */
  207. *val = 0xffffffff;
  208. return 1;
  209. }
  210. }
  211. /* write address */
  212. reg = PCIE_CONF_ADDR(dev, offset);
  213. writel(reg, pcie->base + PCIE_CONF_ADDR_OFF);
  214. *val = readl(pcie->base + PCIE_CONF_DATA_OFF);
  215. return 0;
  216. }
  217. static int mvebu_pcie_write_config_dword(struct pci_controller *hose,
  218. pci_dev_t dev, int offset, u32 val)
  219. {
  220. struct mvebu_pcie *pcie = hose_to_pcie(hose);
  221. int local_bus = PCI_BUS(pcie->dev);
  222. int local_dev = PCI_DEV(pcie->dev);
  223. /* Only allow one other device besides the local one on the local bus */
  224. if (PCI_BUS(dev) == local_bus && PCI_DEV(dev) != local_dev) {
  225. if (local_dev == 0 && PCI_DEV(dev) != 1) {
  226. /*
  227. * If local dev is 0, the first other dev can
  228. * only be 1
  229. */
  230. return 1;
  231. } else if (local_dev != 0 && PCI_DEV(dev) != 0) {
  232. /*
  233. * If local dev is not 0, the first other dev can
  234. * only be 0
  235. */
  236. return 1;
  237. }
  238. }
  239. writel(PCIE_CONF_ADDR(dev, offset), pcie->base + PCIE_CONF_ADDR_OFF);
  240. writel(val, pcie->base + PCIE_CONF_DATA_OFF);
  241. return 0;
  242. }
  243. /*
  244. * Setup PCIE BARs and Address Decode Wins:
  245. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  246. * WIN[0-3] -> DRAM bank[0-3]
  247. */
  248. static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
  249. {
  250. const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
  251. u32 size;
  252. int i;
  253. /* First, disable and clear BARs and windows. */
  254. for (i = 1; i < 3; i++) {
  255. writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
  256. writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
  257. writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
  258. }
  259. for (i = 0; i < 5; i++) {
  260. writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
  261. writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
  262. writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
  263. }
  264. writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
  265. writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
  266. writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
  267. /* Setup windows for DDR banks. Count total DDR size on the fly. */
  268. size = 0;
  269. for (i = 0; i < dram->num_cs; i++) {
  270. const struct mbus_dram_window *cs = dram->cs + i;
  271. writel(cs->base & 0xffff0000,
  272. pcie->base + PCIE_WIN04_BASE_OFF(i));
  273. writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
  274. writel(((cs->size - 1) & 0xffff0000) |
  275. (cs->mbus_attr << 8) |
  276. (dram->mbus_dram_target_id << 4) | 1,
  277. pcie->base + PCIE_WIN04_CTRL_OFF(i));
  278. size += cs->size;
  279. }
  280. /* Round up 'size' to the nearest power of two. */
  281. if ((size & (size - 1)) != 0)
  282. size = 1 << fls(size);
  283. /* Setup BAR[1] to all DRAM banks. */
  284. writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
  285. writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
  286. writel(((size - 1) & 0xffff0000) | 0x1,
  287. pcie->base + PCIE_BAR_CTRL_OFF(1));
  288. }
  289. void pci_init_board(void)
  290. {
  291. int mem_target, mem_attr, i;
  292. int bus = 0;
  293. u32 reg;
  294. u32 soc_ctrl = readl(MVEBU_SYSTEM_REG_BASE + 0x4);
  295. /* Check SoC Control Power State */
  296. debug("%s: SoC Control %08x, 0en %01lx, 1en %01lx, 2en %01lx\n",
  297. __func__, soc_ctrl, SELECT(soc_ctrl, 0), SELECT(soc_ctrl, 1),
  298. SELECT(soc_ctrl, 2));
  299. for (i = 0; i < MAX_PEX; i++) {
  300. struct mvebu_pcie *pcie = &pcie_bus[i];
  301. struct pci_controller *hose = &pcie->hose;
  302. /* Get port number, lane number and memory target / attr */
  303. mvebu_get_port_lane(pcie, i, &mem_target, &mem_attr);
  304. /* Don't read at all from pci registers if port power is down */
  305. if (pcie->lane == 0 && SELECT(soc_ctrl, pcie->port) == 0) {
  306. i += 3;
  307. debug("%s: skipping port %d\n", __func__, pcie->port);
  308. continue;
  309. }
  310. pcie->base = (void __iomem *)PCIE_BASE(i);
  311. /* Check link and skip ports that have no link */
  312. if (!mvebu_pcie_link_up(pcie)) {
  313. debug("%s: PCIe %d.%d - down\n", __func__,
  314. pcie->port, pcie->lane);
  315. continue;
  316. }
  317. debug("%s: PCIe %d.%d - up, base %08x\n", __func__,
  318. pcie->port, pcie->lane, (u32)pcie->base);
  319. /* Read Id info and local bus/dev */
  320. debug("direct conf read %08x, local bus %d, local dev %d\n",
  321. readl(pcie->base), mvebu_pcie_get_local_bus_nr(pcie),
  322. mvebu_pcie_get_local_dev_nr(pcie));
  323. mvebu_pcie_set_local_bus_nr(pcie, bus);
  324. mvebu_pcie_set_local_dev_nr(pcie, 0);
  325. pcie->dev = PCI_BDF(bus, 0, 0);
  326. pcie->mem.start = (u32)mvebu_pcie_membase;
  327. pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
  328. mvebu_pcie_membase += PCIE_MEM_SIZE;
  329. if (mvebu_mbus_add_window_by_id(mem_target, mem_attr,
  330. (phys_addr_t)pcie->mem.start,
  331. PCIE_MEM_SIZE)) {
  332. printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
  333. (u32)pcie->mem.start, PCIE_MEM_SIZE);
  334. }
  335. /* Setup windows and configure host bridge */
  336. mvebu_pcie_setup_wins(pcie);
  337. /* Master + slave enable. */
  338. reg = readl(pcie->base + PCIE_CMD_OFF);
  339. reg |= PCI_COMMAND_MEMORY;
  340. reg |= PCI_COMMAND_MASTER;
  341. reg |= BIT(10); /* disable interrupts */
  342. writel(reg, pcie->base + PCIE_CMD_OFF);
  343. /* Setup U-Boot PCI Controller */
  344. hose->first_busno = 0;
  345. hose->current_busno = bus;
  346. /* PCI memory space */
  347. pci_set_region(hose->regions + 0, pcie->mem.start,
  348. pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
  349. pci_set_region(hose->regions + 1,
  350. 0, 0,
  351. gd->ram_size,
  352. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  353. hose->region_count = 2;
  354. pci_set_ops(hose,
  355. pci_hose_read_config_byte_via_dword,
  356. pci_hose_read_config_word_via_dword,
  357. mvebu_pcie_read_config_dword,
  358. pci_hose_write_config_byte_via_dword,
  359. pci_hose_write_config_word_via_dword,
  360. mvebu_pcie_write_config_dword);
  361. pci_register_hose(hose);
  362. hose->last_busno = pci_hose_scan(hose);
  363. /* Set BAR0 to internal registers */
  364. writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
  365. writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
  366. bus = hose->last_busno + 1;
  367. /* need to skip more for X4 links, otherwise scan will hang */
  368. if (mvebu_soc_family() == MVEBU_SOC_AXP) {
  369. if (mvebu_pex_unit_is_x4(i))
  370. i += 3;
  371. }
  372. }
  373. }