blob: 173636f48e2c81efdcc1ffb4620fcf2997109d07 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Marek Vasute9be4292013-12-14 05:55:28 +01002/*
3 * Freescale i.MX6 PCI Express Root-Complex driver
4 *
5 * Copyright (C) 2013 Marek Vasut <marex@denx.de>
6 *
7 * Based on upstream Linux kernel driver:
8 * pci-imx6.c: Sean Cross <xobs@kosagi.com>
9 * pcie-designware.c: Jingoo Han <jg1.han@samsung.com>
Marek Vasute9be4292013-12-14 05:55:28 +010010 */
11
12#include <common.h>
13#include <pci.h>
14#include <asm/arch/clock.h>
15#include <asm/arch/iomux.h>
16#include <asm/arch/crm_regs.h>
Marek Vasutbb019562014-02-03 21:46:22 +010017#include <asm/gpio.h>
Marek Vasute9be4292013-12-14 05:55:28 +010018#include <asm/io.h>
Alexey Brodkin1ace4022014-02-26 17:47:58 +040019#include <linux/sizes.h>
Marek Vasute9be4292013-12-14 05:55:28 +010020#include <errno.h>
Fabio Estevamaaf87f02015-10-13 11:01:27 -030021#include <asm/arch/sys_proto.h>
Marek Vasute9be4292013-12-14 05:55:28 +010022
23#define PCI_ACCESS_READ 0
24#define PCI_ACCESS_WRITE 1
25
Fabio Estevam1b8ad742014-08-25 14:26:45 -030026#ifdef CONFIG_MX6SX
27#define MX6_DBI_ADDR 0x08ffc000
28#define MX6_IO_ADDR 0x08000000
29#define MX6_MEM_ADDR 0x08100000
30#define MX6_ROOT_ADDR 0x08f00000
31#else
Marek Vasute9be4292013-12-14 05:55:28 +010032#define MX6_DBI_ADDR 0x01ffc000
Marek Vasute9be4292013-12-14 05:55:28 +010033#define MX6_IO_ADDR 0x01000000
Marek Vasute9be4292013-12-14 05:55:28 +010034#define MX6_MEM_ADDR 0x01100000
Marek Vasute9be4292013-12-14 05:55:28 +010035#define MX6_ROOT_ADDR 0x01f00000
Fabio Estevam1b8ad742014-08-25 14:26:45 -030036#endif
37#define MX6_DBI_SIZE 0x4000
38#define MX6_IO_SIZE 0x100000
39#define MX6_MEM_SIZE 0xe00000
Marek Vasute9be4292013-12-14 05:55:28 +010040#define MX6_ROOT_SIZE 0xfc000
41
42/* PCIe Port Logic registers (memory-mapped) */
43#define PL_OFFSET 0x700
Tim Harvey6ecbe132017-05-12 12:58:41 -070044#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
45#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
46#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Marek Vasute9be4292013-12-14 05:55:28 +010047#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
48#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
49#define PCIE_PHY_DEBUG_R1_LINK_UP (1 << 4)
50#define PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING (1 << 29)
51
52#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
53#define PCIE_PHY_CTRL_DATA_LOC 0
54#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
55#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
56#define PCIE_PHY_CTRL_WR_LOC 18
57#define PCIE_PHY_CTRL_RD_LOC 19
58
59#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
60#define PCIE_PHY_STAT_DATA_LOC 0
61#define PCIE_PHY_STAT_ACK_LOC 16
62
63/* PHY registers (not memory-mapped) */
64#define PCIE_PHY_RX_ASIC_OUT 0x100D
65
66#define PHY_RX_OVRD_IN_LO 0x1005
67#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
68#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
69
Fabio Estevam1b8ad742014-08-25 14:26:45 -030070#define PCIE_PHY_PUP_REQ (1 << 7)
71
Marek Vasute9be4292013-12-14 05:55:28 +010072/* iATU registers */
73#define PCIE_ATU_VIEWPORT 0x900
74#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
75#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
76#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
77#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
78#define PCIE_ATU_CR1 0x904
79#define PCIE_ATU_TYPE_MEM (0x0 << 0)
80#define PCIE_ATU_TYPE_IO (0x2 << 0)
81#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
82#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
83#define PCIE_ATU_CR2 0x908
84#define PCIE_ATU_ENABLE (0x1 << 31)
85#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
86#define PCIE_ATU_LOWER_BASE 0x90C
87#define PCIE_ATU_UPPER_BASE 0x910
88#define PCIE_ATU_LIMIT 0x914
89#define PCIE_ATU_LOWER_TARGET 0x918
90#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
91#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
92#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
93#define PCIE_ATU_UPPER_TARGET 0x91C
94
Marek Vasut33f794b2019-06-09 03:50:52 +020095struct imx_pcie_priv {
96 void __iomem *dbi_base;
97 void __iomem *cfg_base;
98};
99
100static struct imx_pcie_priv imx_pcie_priv = {
101 .dbi_base = (void __iomem *)MX6_DBI_ADDR,
102 .cfg_base = (void __iomem *)MX6_ROOT_ADDR,
103};
104
105static struct imx_pcie_priv *priv = &imx_pcie_priv;
106
Marek Vasute9be4292013-12-14 05:55:28 +0100107/*
108 * PHY access functions
109 */
110static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
111{
112 u32 val;
113 u32 max_iterations = 10;
114 u32 wait_counter = 0;
115
116 do {
117 val = readl(dbi_base + PCIE_PHY_STAT);
118 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
119 wait_counter++;
120
121 if (val == exp_val)
122 return 0;
123
124 udelay(1);
125 } while (wait_counter < max_iterations);
126
127 return -ETIMEDOUT;
128}
129
130static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
131{
132 u32 val;
133 int ret;
134
135 val = addr << PCIE_PHY_CTRL_DATA_LOC;
136 writel(val, dbi_base + PCIE_PHY_CTRL);
137
138 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
139 writel(val, dbi_base + PCIE_PHY_CTRL);
140
141 ret = pcie_phy_poll_ack(dbi_base, 1);
142 if (ret)
143 return ret;
144
145 val = addr << PCIE_PHY_CTRL_DATA_LOC;
146 writel(val, dbi_base + PCIE_PHY_CTRL);
147
148 ret = pcie_phy_poll_ack(dbi_base, 0);
149 if (ret)
150 return ret;
151
152 return 0;
153}
154
155/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
156static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
157{
158 u32 val, phy_ctl;
159 int ret;
160
161 ret = pcie_phy_wait_ack(dbi_base, addr);
162 if (ret)
163 return ret;
164
165 /* assert Read signal */
166 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
167 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
168
169 ret = pcie_phy_poll_ack(dbi_base, 1);
170 if (ret)
171 return ret;
172
173 val = readl(dbi_base + PCIE_PHY_STAT);
174 *data = val & 0xffff;
175
176 /* deassert Read signal */
177 writel(0x00, dbi_base + PCIE_PHY_CTRL);
178
179 ret = pcie_phy_poll_ack(dbi_base, 0);
180 if (ret)
181 return ret;
182
183 return 0;
184}
185
186static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
187{
188 u32 var;
189 int ret;
190
191 /* write addr */
192 /* cap addr */
193 ret = pcie_phy_wait_ack(dbi_base, addr);
194 if (ret)
195 return ret;
196
197 var = data << PCIE_PHY_CTRL_DATA_LOC;
198 writel(var, dbi_base + PCIE_PHY_CTRL);
199
200 /* capture data */
201 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
202 writel(var, dbi_base + PCIE_PHY_CTRL);
203
204 ret = pcie_phy_poll_ack(dbi_base, 1);
205 if (ret)
206 return ret;
207
208 /* deassert cap data */
209 var = data << PCIE_PHY_CTRL_DATA_LOC;
210 writel(var, dbi_base + PCIE_PHY_CTRL);
211
212 /* wait for ack de-assertion */
213 ret = pcie_phy_poll_ack(dbi_base, 0);
214 if (ret)
215 return ret;
216
217 /* assert wr signal */
218 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
219 writel(var, dbi_base + PCIE_PHY_CTRL);
220
221 /* wait for ack */
222 ret = pcie_phy_poll_ack(dbi_base, 1);
223 if (ret)
224 return ret;
225
226 /* deassert wr signal */
227 var = data << PCIE_PHY_CTRL_DATA_LOC;
228 writel(var, dbi_base + PCIE_PHY_CTRL);
229
230 /* wait for ack de-assertion */
231 ret = pcie_phy_poll_ack(dbi_base, 0);
232 if (ret)
233 return ret;
234
235 writel(0x0, dbi_base + PCIE_PHY_CTRL);
236
237 return 0;
238}
239
240static int imx6_pcie_link_up(void)
241{
242 u32 rc, ltssm;
243 int rx_valid, temp;
244
245 /* link is debug bit 36, debug register 1 starts at bit 32 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200246 rc = readl(priv->dbi_base + PCIE_PHY_DEBUG_R1);
Marek Vasute9be4292013-12-14 05:55:28 +0100247 if ((rc & PCIE_PHY_DEBUG_R1_LINK_UP) &&
248 !(rc & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING))
249 return -EAGAIN;
250
251 /*
252 * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
253 * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
254 * If (MAC/LTSSM.state == Recovery.RcvrLock)
255 * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
256 * to gen2 is stuck
257 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200258 pcie_phy_read(priv->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
259 ltssm = readl(priv->dbi_base + PCIE_PHY_DEBUG_R0) & 0x3F;
Marek Vasute9be4292013-12-14 05:55:28 +0100260
261 if (rx_valid & 0x01)
262 return 0;
263
264 if (ltssm != 0x0d)
265 return 0;
266
267 printf("transition to gen2 is stuck, reset PHY!\n");
268
Marek Vasut33f794b2019-06-09 03:50:52 +0200269 pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
Marek Vasute9be4292013-12-14 05:55:28 +0100270 temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Marek Vasut33f794b2019-06-09 03:50:52 +0200271 pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
Marek Vasute9be4292013-12-14 05:55:28 +0100272
273 udelay(3000);
274
Marek Vasut33f794b2019-06-09 03:50:52 +0200275 pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
Marek Vasute9be4292013-12-14 05:55:28 +0100276 temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Marek Vasut33f794b2019-06-09 03:50:52 +0200277 pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
Marek Vasute9be4292013-12-14 05:55:28 +0100278
279 return 0;
280}
281
282/*
283 * iATU region setup
284 */
285static int imx_pcie_regions_setup(void)
286{
287 /*
288 * i.MX6 defines 16MB in the AXI address map for PCIe.
289 *
290 * That address space excepted the pcie registers is
291 * split and defined into different regions by iATU,
292 * with sizes and offsets as follows:
293 *
294 * 0x0100_0000 --- 0x010F_FFFF 1MB IORESOURCE_IO
295 * 0x0110_0000 --- 0x01EF_FFFF 14MB IORESOURCE_MEM
296 * 0x01F0_0000 --- 0x01FF_FFFF 1MB Cfg + Registers
297 */
298
299 /* CMD reg:I/O space, MEM space, and Bus Master Enable */
Marek Vasut33f794b2019-06-09 03:50:52 +0200300 setbits_le32(priv->dbi_base + PCI_COMMAND,
Marek Vasute9be4292013-12-14 05:55:28 +0100301 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
302
303 /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI */
Marek Vasut33f794b2019-06-09 03:50:52 +0200304 setbits_le32(priv->dbi_base + PCI_CLASS_REVISION,
Marek Vasute9be4292013-12-14 05:55:28 +0100305 PCI_CLASS_BRIDGE_PCI << 16);
306
307 /* Region #0 is used for Outbound CFG space access. */
Marek Vasut33f794b2019-06-09 03:50:52 +0200308 writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
Marek Vasute9be4292013-12-14 05:55:28 +0100309
Marek Vasut33f794b2019-06-09 03:50:52 +0200310 writel((u32)priv->cfg_base, priv->dbi_base + PCIE_ATU_LOWER_BASE);
311 writel(0, priv->dbi_base + PCIE_ATU_UPPER_BASE);
312 writel((u32)priv->cfg_base + MX6_ROOT_SIZE,
313 priv->dbi_base + PCIE_ATU_LIMIT);
Marek Vasute9be4292013-12-14 05:55:28 +0100314
Marek Vasut33f794b2019-06-09 03:50:52 +0200315 writel(0, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
316 writel(0, priv->dbi_base + PCIE_ATU_UPPER_TARGET);
317 writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
318 writel(PCIE_ATU_ENABLE, priv->dbi_base + PCIE_ATU_CR2);
Marek Vasute9be4292013-12-14 05:55:28 +0100319
320 return 0;
321}
322
323/*
324 * PCI Express accessors
325 */
326static uint32_t get_bus_address(pci_dev_t d, int where)
327{
328 uint32_t va_address;
329
330 /* Reconfigure Region #0 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200331 writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
Marek Vasute9be4292013-12-14 05:55:28 +0100332
333 if (PCI_BUS(d) < 2)
Marek Vasut33f794b2019-06-09 03:50:52 +0200334 writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
Marek Vasute9be4292013-12-14 05:55:28 +0100335 else
Marek Vasut33f794b2019-06-09 03:50:52 +0200336 writel(PCIE_ATU_TYPE_CFG1, priv->dbi_base + PCIE_ATU_CR1);
Marek Vasute9be4292013-12-14 05:55:28 +0100337
338 if (PCI_BUS(d) == 0) {
Marek Vasut33f794b2019-06-09 03:50:52 +0200339 va_address = (u32)priv->dbi_base;
Marek Vasute9be4292013-12-14 05:55:28 +0100340 } else {
Marek Vasut33f794b2019-06-09 03:50:52 +0200341 writel(d << 8, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
342 va_address = (u32)priv->cfg_base;
Marek Vasute9be4292013-12-14 05:55:28 +0100343 }
344
345 va_address += (where & ~0x3);
346
347 return va_address;
348}
349
350static int imx_pcie_addr_valid(pci_dev_t d)
351{
352 if ((PCI_BUS(d) == 0) && (PCI_DEV(d) > 1))
353 return -EINVAL;
354 if ((PCI_BUS(d) == 1) && (PCI_DEV(d) > 0))
355 return -EINVAL;
356 return 0;
357}
358
359/*
360 * Replace the original ARM DABT handler with a simple jump-back one.
361 *
362 * The problem here is that if we have a PCIe bridge attached to this PCIe
363 * controller, but no PCIe device is connected to the bridges' downstream
364 * port, the attempt to read/write from/to the config space will produce
365 * a DABT. This is a behavior of the controller and can not be disabled
366 * unfortuatelly.
367 *
368 * To work around the problem, we backup the current DABT handler address
369 * and replace it with our own DABT handler, which only bounces right back
370 * into the code.
371 */
372static void imx_pcie_fix_dabt_handler(bool set)
373{
374 extern uint32_t *_data_abort;
375 uint32_t *data_abort_addr = (uint32_t *)&_data_abort;
376
377 static const uint32_t data_abort_bounce_handler = 0xe25ef004;
378 uint32_t data_abort_bounce_addr = (uint32_t)&data_abort_bounce_handler;
379
380 static uint32_t data_abort_backup;
381
382 if (set) {
383 data_abort_backup = *data_abort_addr;
384 *data_abort_addr = data_abort_bounce_addr;
385 } else {
386 *data_abort_addr = data_abort_backup;
387 }
388}
389
390static int imx_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
391 int where, u32 *val)
392{
393 uint32_t va_address;
394 int ret;
395
396 ret = imx_pcie_addr_valid(d);
397 if (ret) {
398 *val = 0xffffffff;
Bin Meng9642b782016-01-08 01:03:20 -0800399 return 0;
Marek Vasute9be4292013-12-14 05:55:28 +0100400 }
401
402 va_address = get_bus_address(d, where);
403
404 /*
405 * Read the PCIe config space. We must replace the DABT handler
406 * here in case we got data abort from the PCIe controller, see
407 * imx_pcie_fix_dabt_handler() description. Note that writing the
408 * "val" with valid value is also imperative here as in case we
409 * did got DABT, the val would contain random value.
410 */
411 imx_pcie_fix_dabt_handler(true);
412 writel(0xffffffff, val);
413 *val = readl(va_address);
414 imx_pcie_fix_dabt_handler(false);
415
416 return 0;
417}
418
419static int imx_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
420 int where, u32 val)
421{
422 uint32_t va_address = 0;
423 int ret;
424
425 ret = imx_pcie_addr_valid(d);
426 if (ret)
427 return ret;
428
429 va_address = get_bus_address(d, where);
430
431 /*
432 * Write the PCIe config space. We must replace the DABT handler
433 * here in case we got data abort from the PCIe controller, see
434 * imx_pcie_fix_dabt_handler() description.
435 */
436 imx_pcie_fix_dabt_handler(true);
437 writel(val, va_address);
438 imx_pcie_fix_dabt_handler(false);
439
440 return 0;
441}
442
443/*
444 * Initial bus setup
445 */
Sven-Ola Tueckeb2915ba2017-10-05 08:46:42 -0300446static int imx6_pcie_assert_core_reset(bool prepare_for_boot)
Marek Vasute9be4292013-12-14 05:55:28 +0100447{
448 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
Fabio Estevamaaf87f02015-10-13 11:01:27 -0300449
450 if (is_mx6dqp())
451 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
452
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300453#if defined(CONFIG_MX6SX)
454 struct gpc *gpc_regs = (struct gpc *)GPC_BASE_ADDR;
Marek Vasute9be4292013-12-14 05:55:28 +0100455
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300456 /* SSP_EN is not used on MX6SX anymore */
457 setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
458 /* Force PCIe PHY reset */
459 setbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
460 /* Power up PCIe PHY */
461 setbits_le32(&gpc_regs->cntr, PCIE_PHY_PUP_REQ);
462#else
Tim Harvey6ecbe132017-05-12 12:58:41 -0700463 /*
464 * If the bootloader already enabled the link we need some special
465 * handling to get the core back into a state where it is safe to
466 * touch it for configuration. As there is no dedicated reset signal
467 * wired up for MX6QDL, we need to manually force LTSSM into "detect"
468 * state before completely disabling LTSSM, which is a prerequisite
469 * for core configuration.
470 *
471 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
472 * indication that the bootloader activated the link.
473 */
Sven-Ola Tueckeb2915ba2017-10-05 08:46:42 -0300474 if (is_mx6dq() && prepare_for_boot) {
Tim Harvey6ecbe132017-05-12 12:58:41 -0700475 u32 val, gpr1, gpr12;
476
477 gpr1 = readl(&iomuxc_regs->gpr[1]);
478 gpr12 = readl(&iomuxc_regs->gpr[12]);
479 if ((gpr1 & IOMUXC_GPR1_PCIE_REF_CLK_EN) &&
480 (gpr12 & IOMUXC_GPR12_PCIE_CTL_2)) {
Marek Vasut33f794b2019-06-09 03:50:52 +0200481 val = readl(priv->dbi_base + PCIE_PL_PFLR);
Tim Harvey6ecbe132017-05-12 12:58:41 -0700482 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
483 val |= PCIE_PL_PFLR_FORCE_LINK;
484
485 imx_pcie_fix_dabt_handler(true);
Marek Vasut33f794b2019-06-09 03:50:52 +0200486 writel(val, priv->dbi_base + PCIE_PL_PFLR);
Tim Harvey6ecbe132017-05-12 12:58:41 -0700487 imx_pcie_fix_dabt_handler(false);
488
489 gpr12 &= ~IOMUXC_GPR12_PCIE_CTL_2;
490 writel(val, &iomuxc_regs->gpr[12]);
491 }
492 }
Marek Vasute9be4292013-12-14 05:55:28 +0100493 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
494 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300495#endif
Marek Vasute9be4292013-12-14 05:55:28 +0100496
497 return 0;
498}
499
500static int imx6_pcie_init_phy(void)
501{
502 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
503
504 clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
505
506 clrsetbits_le32(&iomuxc_regs->gpr[12],
507 IOMUXC_GPR12_DEVICE_TYPE_MASK,
508 IOMUXC_GPR12_DEVICE_TYPE_RC);
509 clrsetbits_le32(&iomuxc_regs->gpr[12],
510 IOMUXC_GPR12_LOS_LEVEL_MASK,
511 IOMUXC_GPR12_LOS_LEVEL_9);
512
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300513#ifdef CONFIG_MX6SX
514 clrsetbits_le32(&iomuxc_regs->gpr[12],
515 IOMUXC_GPR12_RX_EQ_MASK,
516 IOMUXC_GPR12_RX_EQ_2);
517#endif
518
Marek Vasute9be4292013-12-14 05:55:28 +0100519 writel((0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN1_OFFSET) |
520 (0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_3P5DB_OFFSET) |
521 (20 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_6DB_OFFSET) |
522 (127 << IOMUXC_GPR8_PCS_TX_SWING_FULL_OFFSET) |
523 (127 << IOMUXC_GPR8_PCS_TX_SWING_LOW_OFFSET),
524 &iomuxc_regs->gpr[8]);
525
526 return 0;
527}
528
Marek Vasuta778aea2014-03-23 22:45:40 +0100529__weak int imx6_pcie_toggle_power(void)
530{
531#ifdef CONFIG_PCIE_IMX_POWER_GPIO
Peng Fan67b71df2018-01-02 18:27:29 +0800532 gpio_request(CONFIG_PCIE_IMX_POWER_GPIO, "pcie_power");
Marek Vasuta778aea2014-03-23 22:45:40 +0100533 gpio_direction_output(CONFIG_PCIE_IMX_POWER_GPIO, 0);
534 mdelay(20);
535 gpio_set_value(CONFIG_PCIE_IMX_POWER_GPIO, 1);
536 mdelay(20);
Peng Fan67b71df2018-01-02 18:27:29 +0800537 gpio_free(CONFIG_PCIE_IMX_POWER_GPIO);
Marek Vasuta778aea2014-03-23 22:45:40 +0100538#endif
539 return 0;
540}
541
Marek Vasutbb019562014-02-03 21:46:22 +0100542__weak int imx6_pcie_toggle_reset(void)
543{
544 /*
545 * See 'PCI EXPRESS BASE SPECIFICATION, REV 3.0, SECTION 6.6.1'
546 * for detailed understanding of the PCIe CR reset logic.
547 *
548 * The PCIe #PERST reset line _MUST_ be connected, otherwise your
549 * design does not conform to the specification. You must wait at
Fabio Estevam8f6edf62015-09-10 20:45:25 -0300550 * least 20 ms after de-asserting the #PERST so the EP device can
Marek Vasutbb019562014-02-03 21:46:22 +0100551 * do self-initialisation.
552 *
553 * In case your #PERST pin is connected to a plain GPIO pin of the
554 * CPU, you can define CONFIG_PCIE_IMX_PERST_GPIO in your board's
555 * configuration file and the condition below will handle the rest
556 * of the reset toggling.
557 *
558 * In case your #PERST toggling logic is more complex, for example
559 * connected via CPLD or somesuch, you can override this function
560 * in your board file and implement reset logic as needed. You must
Fabio Estevam8f6edf62015-09-10 20:45:25 -0300561 * not forget to wait at least 20 ms after de-asserting #PERST in
Marek Vasutbb019562014-02-03 21:46:22 +0100562 * this case either though.
563 *
564 * In case your #PERST line of the PCIe EP device is not connected
565 * at all, your design is broken and you should fix your design,
566 * otherwise you will observe problems like for example the link
567 * not coming up after rebooting the system back from running Linux
568 * that uses the PCIe as well OR the PCIe link might not come up in
569 * Linux at all in the first place since it's in some non-reset
570 * state due to being previously used in U-Boot.
571 */
572#ifdef CONFIG_PCIE_IMX_PERST_GPIO
Peng Fan67b71df2018-01-02 18:27:29 +0800573 gpio_request(CONFIG_PCIE_IMX_PERST_GPIO, "pcie_reset");
Marek Vasutbb019562014-02-03 21:46:22 +0100574 gpio_direction_output(CONFIG_PCIE_IMX_PERST_GPIO, 0);
575 mdelay(20);
576 gpio_set_value(CONFIG_PCIE_IMX_PERST_GPIO, 1);
577 mdelay(20);
Peng Fan67b71df2018-01-02 18:27:29 +0800578 gpio_free(CONFIG_PCIE_IMX_PERST_GPIO);
Marek Vasutbb019562014-02-03 21:46:22 +0100579#else
580 puts("WARNING: Make sure the PCIe #PERST line is connected!\n");
581#endif
582 return 0;
583}
584
Marek Vasute9be4292013-12-14 05:55:28 +0100585static int imx6_pcie_deassert_core_reset(void)
586{
587 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
588
Marek Vasuta778aea2014-03-23 22:45:40 +0100589 imx6_pcie_toggle_power();
Marek Vasute9be4292013-12-14 05:55:28 +0100590
Marek Vasute9be4292013-12-14 05:55:28 +0100591 enable_pcie_clock();
592
Fabio Estevamaaf87f02015-10-13 11:01:27 -0300593 if (is_mx6dqp())
594 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
595
Marek Vasute9be4292013-12-14 05:55:28 +0100596 /*
597 * Wait for the clock to settle a bit, when the clock are sourced
Fabio Estevam8f6edf62015-09-10 20:45:25 -0300598 * from the CPU, we need about 30 ms to settle.
Marek Vasute9be4292013-12-14 05:55:28 +0100599 */
Marek Vasutbb019562014-02-03 21:46:22 +0100600 mdelay(50);
Marek Vasute9be4292013-12-14 05:55:28 +0100601
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300602#if defined(CONFIG_MX6SX)
603 /* SSP_EN is not used on MX6SX anymore */
604 clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
605 /* Clear PCIe PHY reset bit */
606 clrbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
607#else
Tim Harvey5a82e1a2014-08-07 22:57:29 -0700608 /* Enable PCIe */
609 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
610 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300611#endif
Tim Harvey5a82e1a2014-08-07 22:57:29 -0700612
Marek Vasutbb019562014-02-03 21:46:22 +0100613 imx6_pcie_toggle_reset();
Marek Vasute9be4292013-12-14 05:55:28 +0100614
615 return 0;
616}
617
618static int imx_pcie_link_up(void)
619{
620 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
621 uint32_t tmp;
622 int count = 0;
623
Sven-Ola Tueckeb2915ba2017-10-05 08:46:42 -0300624 imx6_pcie_assert_core_reset(false);
Marek Vasute9be4292013-12-14 05:55:28 +0100625 imx6_pcie_init_phy();
626 imx6_pcie_deassert_core_reset();
627
628 imx_pcie_regions_setup();
629
630 /*
Koen Vandeputtef57263e2018-01-04 14:54:34 +0100631 * By default, the subordinate is set equally to the secondary
632 * bus (0x01) when the RC boots.
633 * This means that theoretically, only bus 1 is reachable from the RC.
634 * Force the PCIe RC subordinate to 0xff, otherwise no downstream
635 * devices will be detected if the enumeration is applied strictly.
636 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200637 tmp = readl(priv->dbi_base + 0x18);
Koen Vandeputtef57263e2018-01-04 14:54:34 +0100638 tmp |= (0xff << 16);
Marek Vasut33f794b2019-06-09 03:50:52 +0200639 writel(tmp, priv->dbi_base + 0x18);
Koen Vandeputtef57263e2018-01-04 14:54:34 +0100640
641 /*
Marek Vasute9be4292013-12-14 05:55:28 +0100642 * FIXME: Force the PCIe RC to Gen1 operation
643 * The RC must be forced into Gen1 mode before bringing the link
644 * up, otherwise no downstream devices are detected. After the
645 * link is up, a managed Gen1->Gen2 transition can be initiated.
646 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200647 tmp = readl(priv->dbi_base + 0x7c);
Marek Vasute9be4292013-12-14 05:55:28 +0100648 tmp &= ~0xf;
649 tmp |= 0x1;
Marek Vasut33f794b2019-06-09 03:50:52 +0200650 writel(tmp, priv->dbi_base + 0x7c);
Marek Vasute9be4292013-12-14 05:55:28 +0100651
652 /* LTSSM enable, starting link. */
653 setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
654
655 while (!imx6_pcie_link_up()) {
656 udelay(10);
657 count++;
Stefano Babica32b4a02016-06-06 11:14:19 +0200658 if (count >= 4000) {
Tim Harvey378b02d2015-05-08 15:17:10 -0700659#ifdef CONFIG_PCI_SCAN_SHOW
660 puts("PCI: pcie phy link never came up\n");
661#endif
Marek Vasute9be4292013-12-14 05:55:28 +0100662 debug("DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
Marek Vasut33f794b2019-06-09 03:50:52 +0200663 readl(priv->dbi_base + PCIE_PHY_DEBUG_R0),
664 readl(priv->dbi_base + PCIE_PHY_DEBUG_R1));
Marek Vasute9be4292013-12-14 05:55:28 +0100665 return -EINVAL;
666 }
667 }
668
669 return 0;
670}
671
672void imx_pcie_init(void)
673{
674 /* Static instance of the controller. */
675 static struct pci_controller pcc;
676 struct pci_controller *hose = &pcc;
677 int ret;
678
679 memset(&pcc, 0, sizeof(pcc));
680
681 /* PCI I/O space */
682 pci_set_region(&hose->regions[0],
683 MX6_IO_ADDR, MX6_IO_ADDR,
684 MX6_IO_SIZE, PCI_REGION_IO);
685
686 /* PCI memory space */
687 pci_set_region(&hose->regions[1],
688 MX6_MEM_ADDR, MX6_MEM_ADDR,
689 MX6_MEM_SIZE, PCI_REGION_MEM);
690
691 /* System memory space */
692 pci_set_region(&hose->regions[2],
693 MMDC0_ARB_BASE_ADDR, MMDC0_ARB_BASE_ADDR,
694 0xefffffff, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
695
696 hose->region_count = 3;
697
698 pci_set_ops(hose,
699 pci_hose_read_config_byte_via_dword,
700 pci_hose_read_config_word_via_dword,
701 imx_pcie_read_config,
702 pci_hose_write_config_byte_via_dword,
703 pci_hose_write_config_word_via_dword,
704 imx_pcie_write_config);
705
706 /* Start the controller. */
707 ret = imx_pcie_link_up();
708
709 if (!ret) {
710 pci_register_hose(hose);
711 hose->last_busno = pci_hose_scan(hose);
712 }
713}
714
Tim Harvey6ecbe132017-05-12 12:58:41 -0700715void imx_pcie_remove(void)
716{
Sven-Ola Tueckeb2915ba2017-10-05 08:46:42 -0300717 imx6_pcie_assert_core_reset(true);
Tim Harvey6ecbe132017-05-12 12:58:41 -0700718}
719
Marek Vasute9be4292013-12-14 05:55:28 +0100720/* Probe function. */
721void pci_init_board(void)
722{
723 imx_pcie_init();
724}