blob: bcee3745f4ce3bdff6db0f2430e2e6b3862594b1 [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
Marek Vasute9be4292013-12-14 05:55:28 +0100100/*
101 * PHY access functions
102 */
103static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
104{
105 u32 val;
106 u32 max_iterations = 10;
107 u32 wait_counter = 0;
108
109 do {
110 val = readl(dbi_base + PCIE_PHY_STAT);
111 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
112 wait_counter++;
113
114 if (val == exp_val)
115 return 0;
116
117 udelay(1);
118 } while (wait_counter < max_iterations);
119
120 return -ETIMEDOUT;
121}
122
123static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
124{
125 u32 val;
126 int ret;
127
128 val = addr << PCIE_PHY_CTRL_DATA_LOC;
129 writel(val, dbi_base + PCIE_PHY_CTRL);
130
131 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
132 writel(val, dbi_base + PCIE_PHY_CTRL);
133
134 ret = pcie_phy_poll_ack(dbi_base, 1);
135 if (ret)
136 return ret;
137
138 val = addr << PCIE_PHY_CTRL_DATA_LOC;
139 writel(val, dbi_base + PCIE_PHY_CTRL);
140
141 ret = pcie_phy_poll_ack(dbi_base, 0);
142 if (ret)
143 return ret;
144
145 return 0;
146}
147
148/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
149static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
150{
151 u32 val, phy_ctl;
152 int ret;
153
154 ret = pcie_phy_wait_ack(dbi_base, addr);
155 if (ret)
156 return ret;
157
158 /* assert Read signal */
159 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
160 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
161
162 ret = pcie_phy_poll_ack(dbi_base, 1);
163 if (ret)
164 return ret;
165
166 val = readl(dbi_base + PCIE_PHY_STAT);
167 *data = val & 0xffff;
168
169 /* deassert Read signal */
170 writel(0x00, dbi_base + PCIE_PHY_CTRL);
171
172 ret = pcie_phy_poll_ack(dbi_base, 0);
173 if (ret)
174 return ret;
175
176 return 0;
177}
178
179static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
180{
181 u32 var;
182 int ret;
183
184 /* write addr */
185 /* cap addr */
186 ret = pcie_phy_wait_ack(dbi_base, addr);
187 if (ret)
188 return ret;
189
190 var = data << PCIE_PHY_CTRL_DATA_LOC;
191 writel(var, dbi_base + PCIE_PHY_CTRL);
192
193 /* capture data */
194 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
195 writel(var, dbi_base + PCIE_PHY_CTRL);
196
197 ret = pcie_phy_poll_ack(dbi_base, 1);
198 if (ret)
199 return ret;
200
201 /* deassert cap data */
202 var = data << PCIE_PHY_CTRL_DATA_LOC;
203 writel(var, dbi_base + PCIE_PHY_CTRL);
204
205 /* wait for ack de-assertion */
206 ret = pcie_phy_poll_ack(dbi_base, 0);
207 if (ret)
208 return ret;
209
210 /* assert wr signal */
211 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
212 writel(var, dbi_base + PCIE_PHY_CTRL);
213
214 /* wait for ack */
215 ret = pcie_phy_poll_ack(dbi_base, 1);
216 if (ret)
217 return ret;
218
219 /* deassert wr signal */
220 var = data << PCIE_PHY_CTRL_DATA_LOC;
221 writel(var, dbi_base + PCIE_PHY_CTRL);
222
223 /* wait for ack de-assertion */
224 ret = pcie_phy_poll_ack(dbi_base, 0);
225 if (ret)
226 return ret;
227
228 writel(0x0, dbi_base + PCIE_PHY_CTRL);
229
230 return 0;
231}
232
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200233static int imx6_pcie_link_up(struct imx_pcie_priv *priv)
Marek Vasute9be4292013-12-14 05:55:28 +0100234{
235 u32 rc, ltssm;
236 int rx_valid, temp;
237
238 /* link is debug bit 36, debug register 1 starts at bit 32 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200239 rc = readl(priv->dbi_base + PCIE_PHY_DEBUG_R1);
Marek Vasute9be4292013-12-14 05:55:28 +0100240 if ((rc & PCIE_PHY_DEBUG_R1_LINK_UP) &&
241 !(rc & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING))
242 return -EAGAIN;
243
244 /*
245 * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
246 * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
247 * If (MAC/LTSSM.state == Recovery.RcvrLock)
248 * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
249 * to gen2 is stuck
250 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200251 pcie_phy_read(priv->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
252 ltssm = readl(priv->dbi_base + PCIE_PHY_DEBUG_R0) & 0x3F;
Marek Vasute9be4292013-12-14 05:55:28 +0100253
254 if (rx_valid & 0x01)
255 return 0;
256
257 if (ltssm != 0x0d)
258 return 0;
259
260 printf("transition to gen2 is stuck, reset PHY!\n");
261
Marek Vasut33f794b2019-06-09 03:50:52 +0200262 pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
Marek Vasute9be4292013-12-14 05:55:28 +0100263 temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Marek Vasut33f794b2019-06-09 03:50:52 +0200264 pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
Marek Vasute9be4292013-12-14 05:55:28 +0100265
266 udelay(3000);
267
Marek Vasut33f794b2019-06-09 03:50:52 +0200268 pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
Marek Vasute9be4292013-12-14 05:55:28 +0100269 temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Marek Vasut33f794b2019-06-09 03:50:52 +0200270 pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
Marek Vasute9be4292013-12-14 05:55:28 +0100271
272 return 0;
273}
274
275/*
276 * iATU region setup
277 */
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200278static int imx_pcie_regions_setup(struct imx_pcie_priv *priv)
Marek Vasute9be4292013-12-14 05:55:28 +0100279{
280 /*
281 * i.MX6 defines 16MB in the AXI address map for PCIe.
282 *
283 * That address space excepted the pcie registers is
284 * split and defined into different regions by iATU,
285 * with sizes and offsets as follows:
286 *
287 * 0x0100_0000 --- 0x010F_FFFF 1MB IORESOURCE_IO
288 * 0x0110_0000 --- 0x01EF_FFFF 14MB IORESOURCE_MEM
289 * 0x01F0_0000 --- 0x01FF_FFFF 1MB Cfg + Registers
290 */
291
292 /* CMD reg:I/O space, MEM space, and Bus Master Enable */
Marek Vasut33f794b2019-06-09 03:50:52 +0200293 setbits_le32(priv->dbi_base + PCI_COMMAND,
Marek Vasute9be4292013-12-14 05:55:28 +0100294 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
295
296 /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI */
Marek Vasut33f794b2019-06-09 03:50:52 +0200297 setbits_le32(priv->dbi_base + PCI_CLASS_REVISION,
Marek Vasute9be4292013-12-14 05:55:28 +0100298 PCI_CLASS_BRIDGE_PCI << 16);
299
300 /* Region #0 is used for Outbound CFG space access. */
Marek Vasut33f794b2019-06-09 03:50:52 +0200301 writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
Marek Vasute9be4292013-12-14 05:55:28 +0100302
Marek Vasut90f87fb2019-06-09 03:50:53 +0200303 writel(lower_32_bits((uintptr_t)priv->cfg_base),
304 priv->dbi_base + PCIE_ATU_LOWER_BASE);
305 writel(upper_32_bits((uintptr_t)priv->cfg_base),
306 priv->dbi_base + PCIE_ATU_UPPER_BASE);
307 writel(lower_32_bits((uintptr_t)priv->cfg_base + MX6_ROOT_SIZE),
Marek Vasut33f794b2019-06-09 03:50:52 +0200308 priv->dbi_base + PCIE_ATU_LIMIT);
Marek Vasute9be4292013-12-14 05:55:28 +0100309
Marek Vasut33f794b2019-06-09 03:50:52 +0200310 writel(0, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
311 writel(0, priv->dbi_base + PCIE_ATU_UPPER_TARGET);
312 writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
313 writel(PCIE_ATU_ENABLE, priv->dbi_base + PCIE_ATU_CR2);
Marek Vasute9be4292013-12-14 05:55:28 +0100314
315 return 0;
316}
317
318/*
319 * PCI Express accessors
320 */
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200321static void __iomem *get_bus_address(struct imx_pcie_priv *priv,
322 pci_dev_t d, int where)
Marek Vasute9be4292013-12-14 05:55:28 +0100323{
Marek Vasut90f87fb2019-06-09 03:50:53 +0200324 void __iomem *va_address;
Marek Vasute9be4292013-12-14 05:55:28 +0100325
326 /* Reconfigure Region #0 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200327 writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
Marek Vasute9be4292013-12-14 05:55:28 +0100328
329 if (PCI_BUS(d) < 2)
Marek Vasut33f794b2019-06-09 03:50:52 +0200330 writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
Marek Vasute9be4292013-12-14 05:55:28 +0100331 else
Marek Vasut33f794b2019-06-09 03:50:52 +0200332 writel(PCIE_ATU_TYPE_CFG1, priv->dbi_base + PCIE_ATU_CR1);
Marek Vasute9be4292013-12-14 05:55:28 +0100333
334 if (PCI_BUS(d) == 0) {
Marek Vasut90f87fb2019-06-09 03:50:53 +0200335 va_address = priv->dbi_base;
Marek Vasute9be4292013-12-14 05:55:28 +0100336 } else {
Marek Vasut33f794b2019-06-09 03:50:52 +0200337 writel(d << 8, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
Marek Vasut90f87fb2019-06-09 03:50:53 +0200338 va_address = priv->cfg_base;
Marek Vasute9be4292013-12-14 05:55:28 +0100339 }
340
341 va_address += (where & ~0x3);
342
343 return va_address;
344}
345
346static int imx_pcie_addr_valid(pci_dev_t d)
347{
348 if ((PCI_BUS(d) == 0) && (PCI_DEV(d) > 1))
349 return -EINVAL;
350 if ((PCI_BUS(d) == 1) && (PCI_DEV(d) > 0))
351 return -EINVAL;
352 return 0;
353}
354
355/*
356 * Replace the original ARM DABT handler with a simple jump-back one.
357 *
358 * The problem here is that if we have a PCIe bridge attached to this PCIe
359 * controller, but no PCIe device is connected to the bridges' downstream
360 * port, the attempt to read/write from/to the config space will produce
361 * a DABT. This is a behavior of the controller and can not be disabled
362 * unfortuatelly.
363 *
364 * To work around the problem, we backup the current DABT handler address
365 * and replace it with our own DABT handler, which only bounces right back
366 * into the code.
367 */
368static void imx_pcie_fix_dabt_handler(bool set)
369{
370 extern uint32_t *_data_abort;
371 uint32_t *data_abort_addr = (uint32_t *)&_data_abort;
372
373 static const uint32_t data_abort_bounce_handler = 0xe25ef004;
374 uint32_t data_abort_bounce_addr = (uint32_t)&data_abort_bounce_handler;
375
376 static uint32_t data_abort_backup;
377
378 if (set) {
379 data_abort_backup = *data_abort_addr;
380 *data_abort_addr = data_abort_bounce_addr;
381 } else {
382 *data_abort_addr = data_abort_backup;
383 }
384}
385
386static int imx_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
387 int where, u32 *val)
388{
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200389 struct imx_pcie_priv *priv = hose->priv_data;
Marek Vasut90f87fb2019-06-09 03:50:53 +0200390 void __iomem *va_address;
Marek Vasute9be4292013-12-14 05:55:28 +0100391 int ret;
392
393 ret = imx_pcie_addr_valid(d);
394 if (ret) {
395 *val = 0xffffffff;
Bin Meng9642b782016-01-08 01:03:20 -0800396 return 0;
Marek Vasute9be4292013-12-14 05:55:28 +0100397 }
398
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200399 va_address = get_bus_address(priv, d, where);
Marek Vasute9be4292013-12-14 05:55:28 +0100400
401 /*
402 * Read the PCIe config space. We must replace the DABT handler
403 * here in case we got data abort from the PCIe controller, see
404 * imx_pcie_fix_dabt_handler() description. Note that writing the
405 * "val" with valid value is also imperative here as in case we
406 * did got DABT, the val would contain random value.
407 */
408 imx_pcie_fix_dabt_handler(true);
409 writel(0xffffffff, val);
410 *val = readl(va_address);
411 imx_pcie_fix_dabt_handler(false);
412
413 return 0;
414}
415
416static int imx_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
417 int where, u32 val)
418{
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200419 struct imx_pcie_priv *priv = hose->priv_data;
Marek Vasut90f87fb2019-06-09 03:50:53 +0200420 void __iomem *va_address = NULL;
Marek Vasute9be4292013-12-14 05:55:28 +0100421 int ret;
422
423 ret = imx_pcie_addr_valid(d);
424 if (ret)
425 return ret;
426
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200427 va_address = get_bus_address(priv, d, where);
Marek Vasute9be4292013-12-14 05:55:28 +0100428
429 /*
430 * Write the PCIe config space. We must replace the DABT handler
431 * here in case we got data abort from the PCIe controller, see
432 * imx_pcie_fix_dabt_handler() description.
433 */
434 imx_pcie_fix_dabt_handler(true);
435 writel(val, va_address);
436 imx_pcie_fix_dabt_handler(false);
437
438 return 0;
439}
440
441/*
442 * Initial bus setup
443 */
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200444static int imx6_pcie_assert_core_reset(struct imx_pcie_priv *priv,
445 bool prepare_for_boot)
Marek Vasute9be4292013-12-14 05:55:28 +0100446{
447 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
Fabio Estevamaaf87f02015-10-13 11:01:27 -0300448
449 if (is_mx6dqp())
450 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
451
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300452#if defined(CONFIG_MX6SX)
453 struct gpc *gpc_regs = (struct gpc *)GPC_BASE_ADDR;
Marek Vasute9be4292013-12-14 05:55:28 +0100454
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300455 /* SSP_EN is not used on MX6SX anymore */
456 setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
457 /* Force PCIe PHY reset */
458 setbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
459 /* Power up PCIe PHY */
460 setbits_le32(&gpc_regs->cntr, PCIE_PHY_PUP_REQ);
461#else
Tim Harvey6ecbe132017-05-12 12:58:41 -0700462 /*
463 * If the bootloader already enabled the link we need some special
464 * handling to get the core back into a state where it is safe to
465 * touch it for configuration. As there is no dedicated reset signal
466 * wired up for MX6QDL, we need to manually force LTSSM into "detect"
467 * state before completely disabling LTSSM, which is a prerequisite
468 * for core configuration.
469 *
470 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
471 * indication that the bootloader activated the link.
472 */
Sven-Ola Tueckeb2915ba2017-10-05 08:46:42 -0300473 if (is_mx6dq() && prepare_for_boot) {
Tim Harvey6ecbe132017-05-12 12:58:41 -0700474 u32 val, gpr1, gpr12;
475
476 gpr1 = readl(&iomuxc_regs->gpr[1]);
477 gpr12 = readl(&iomuxc_regs->gpr[12]);
478 if ((gpr1 & IOMUXC_GPR1_PCIE_REF_CLK_EN) &&
479 (gpr12 & IOMUXC_GPR12_PCIE_CTL_2)) {
Marek Vasut33f794b2019-06-09 03:50:52 +0200480 val = readl(priv->dbi_base + PCIE_PL_PFLR);
Tim Harvey6ecbe132017-05-12 12:58:41 -0700481 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
482 val |= PCIE_PL_PFLR_FORCE_LINK;
483
484 imx_pcie_fix_dabt_handler(true);
Marek Vasut33f794b2019-06-09 03:50:52 +0200485 writel(val, priv->dbi_base + PCIE_PL_PFLR);
Tim Harvey6ecbe132017-05-12 12:58:41 -0700486 imx_pcie_fix_dabt_handler(false);
487
488 gpr12 &= ~IOMUXC_GPR12_PCIE_CTL_2;
489 writel(val, &iomuxc_regs->gpr[12]);
490 }
491 }
Marek Vasute9be4292013-12-14 05:55:28 +0100492 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
493 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300494#endif
Marek Vasute9be4292013-12-14 05:55:28 +0100495
496 return 0;
497}
498
499static int imx6_pcie_init_phy(void)
500{
501 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
502
503 clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
504
505 clrsetbits_le32(&iomuxc_regs->gpr[12],
506 IOMUXC_GPR12_DEVICE_TYPE_MASK,
507 IOMUXC_GPR12_DEVICE_TYPE_RC);
508 clrsetbits_le32(&iomuxc_regs->gpr[12],
509 IOMUXC_GPR12_LOS_LEVEL_MASK,
510 IOMUXC_GPR12_LOS_LEVEL_9);
511
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300512#ifdef CONFIG_MX6SX
513 clrsetbits_le32(&iomuxc_regs->gpr[12],
514 IOMUXC_GPR12_RX_EQ_MASK,
515 IOMUXC_GPR12_RX_EQ_2);
516#endif
517
Marek Vasute9be4292013-12-14 05:55:28 +0100518 writel((0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN1_OFFSET) |
519 (0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_3P5DB_OFFSET) |
520 (20 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_6DB_OFFSET) |
521 (127 << IOMUXC_GPR8_PCS_TX_SWING_FULL_OFFSET) |
522 (127 << IOMUXC_GPR8_PCS_TX_SWING_LOW_OFFSET),
523 &iomuxc_regs->gpr[8]);
524
525 return 0;
526}
527
Marek Vasuta778aea2014-03-23 22:45:40 +0100528__weak int imx6_pcie_toggle_power(void)
529{
530#ifdef CONFIG_PCIE_IMX_POWER_GPIO
Peng Fan67b71df2018-01-02 18:27:29 +0800531 gpio_request(CONFIG_PCIE_IMX_POWER_GPIO, "pcie_power");
Marek Vasuta778aea2014-03-23 22:45:40 +0100532 gpio_direction_output(CONFIG_PCIE_IMX_POWER_GPIO, 0);
533 mdelay(20);
534 gpio_set_value(CONFIG_PCIE_IMX_POWER_GPIO, 1);
535 mdelay(20);
Peng Fan67b71df2018-01-02 18:27:29 +0800536 gpio_free(CONFIG_PCIE_IMX_POWER_GPIO);
Marek Vasuta778aea2014-03-23 22:45:40 +0100537#endif
538 return 0;
539}
540
Marek Vasutbb019562014-02-03 21:46:22 +0100541__weak int imx6_pcie_toggle_reset(void)
542{
543 /*
544 * See 'PCI EXPRESS BASE SPECIFICATION, REV 3.0, SECTION 6.6.1'
545 * for detailed understanding of the PCIe CR reset logic.
546 *
547 * The PCIe #PERST reset line _MUST_ be connected, otherwise your
548 * design does not conform to the specification. You must wait at
Fabio Estevam8f6edf62015-09-10 20:45:25 -0300549 * least 20 ms after de-asserting the #PERST so the EP device can
Marek Vasutbb019562014-02-03 21:46:22 +0100550 * do self-initialisation.
551 *
552 * In case your #PERST pin is connected to a plain GPIO pin of the
553 * CPU, you can define CONFIG_PCIE_IMX_PERST_GPIO in your board's
554 * configuration file and the condition below will handle the rest
555 * of the reset toggling.
556 *
557 * In case your #PERST toggling logic is more complex, for example
558 * connected via CPLD or somesuch, you can override this function
559 * in your board file and implement reset logic as needed. You must
Fabio Estevam8f6edf62015-09-10 20:45:25 -0300560 * not forget to wait at least 20 ms after de-asserting #PERST in
Marek Vasutbb019562014-02-03 21:46:22 +0100561 * this case either though.
562 *
563 * In case your #PERST line of the PCIe EP device is not connected
564 * at all, your design is broken and you should fix your design,
565 * otherwise you will observe problems like for example the link
566 * not coming up after rebooting the system back from running Linux
567 * that uses the PCIe as well OR the PCIe link might not come up in
568 * Linux at all in the first place since it's in some non-reset
569 * state due to being previously used in U-Boot.
570 */
571#ifdef CONFIG_PCIE_IMX_PERST_GPIO
Peng Fan67b71df2018-01-02 18:27:29 +0800572 gpio_request(CONFIG_PCIE_IMX_PERST_GPIO, "pcie_reset");
Marek Vasutbb019562014-02-03 21:46:22 +0100573 gpio_direction_output(CONFIG_PCIE_IMX_PERST_GPIO, 0);
574 mdelay(20);
575 gpio_set_value(CONFIG_PCIE_IMX_PERST_GPIO, 1);
576 mdelay(20);
Peng Fan67b71df2018-01-02 18:27:29 +0800577 gpio_free(CONFIG_PCIE_IMX_PERST_GPIO);
Marek Vasutbb019562014-02-03 21:46:22 +0100578#else
579 puts("WARNING: Make sure the PCIe #PERST line is connected!\n");
580#endif
581 return 0;
582}
583
Marek Vasute9be4292013-12-14 05:55:28 +0100584static int imx6_pcie_deassert_core_reset(void)
585{
586 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
587
Marek Vasuta778aea2014-03-23 22:45:40 +0100588 imx6_pcie_toggle_power();
Marek Vasute9be4292013-12-14 05:55:28 +0100589
Marek Vasute9be4292013-12-14 05:55:28 +0100590 enable_pcie_clock();
591
Fabio Estevamaaf87f02015-10-13 11:01:27 -0300592 if (is_mx6dqp())
593 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
594
Marek Vasute9be4292013-12-14 05:55:28 +0100595 /*
596 * Wait for the clock to settle a bit, when the clock are sourced
Fabio Estevam8f6edf62015-09-10 20:45:25 -0300597 * from the CPU, we need about 30 ms to settle.
Marek Vasute9be4292013-12-14 05:55:28 +0100598 */
Marek Vasutbb019562014-02-03 21:46:22 +0100599 mdelay(50);
Marek Vasute9be4292013-12-14 05:55:28 +0100600
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300601#if defined(CONFIG_MX6SX)
602 /* SSP_EN is not used on MX6SX anymore */
603 clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
604 /* Clear PCIe PHY reset bit */
605 clrbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
606#else
Tim Harvey5a82e1a2014-08-07 22:57:29 -0700607 /* Enable PCIe */
608 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
609 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
Fabio Estevam1b8ad742014-08-25 14:26:45 -0300610#endif
Tim Harvey5a82e1a2014-08-07 22:57:29 -0700611
Marek Vasutbb019562014-02-03 21:46:22 +0100612 imx6_pcie_toggle_reset();
Marek Vasute9be4292013-12-14 05:55:28 +0100613
614 return 0;
615}
616
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200617static int imx_pcie_link_up(struct imx_pcie_priv *priv)
Marek Vasute9be4292013-12-14 05:55:28 +0100618{
619 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
620 uint32_t tmp;
621 int count = 0;
622
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200623 imx6_pcie_assert_core_reset(priv, false);
Marek Vasute9be4292013-12-14 05:55:28 +0100624 imx6_pcie_init_phy();
625 imx6_pcie_deassert_core_reset();
626
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200627 imx_pcie_regions_setup(priv);
Marek Vasute9be4292013-12-14 05:55:28 +0100628
629 /*
Koen Vandeputtef57263e2018-01-04 14:54:34 +0100630 * By default, the subordinate is set equally to the secondary
631 * bus (0x01) when the RC boots.
632 * This means that theoretically, only bus 1 is reachable from the RC.
633 * Force the PCIe RC subordinate to 0xff, otherwise no downstream
634 * devices will be detected if the enumeration is applied strictly.
635 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200636 tmp = readl(priv->dbi_base + 0x18);
Koen Vandeputtef57263e2018-01-04 14:54:34 +0100637 tmp |= (0xff << 16);
Marek Vasut33f794b2019-06-09 03:50:52 +0200638 writel(tmp, priv->dbi_base + 0x18);
Koen Vandeputtef57263e2018-01-04 14:54:34 +0100639
640 /*
Marek Vasute9be4292013-12-14 05:55:28 +0100641 * FIXME: Force the PCIe RC to Gen1 operation
642 * The RC must be forced into Gen1 mode before bringing the link
643 * up, otherwise no downstream devices are detected. After the
644 * link is up, a managed Gen1->Gen2 transition can be initiated.
645 */
Marek Vasut33f794b2019-06-09 03:50:52 +0200646 tmp = readl(priv->dbi_base + 0x7c);
Marek Vasute9be4292013-12-14 05:55:28 +0100647 tmp &= ~0xf;
648 tmp |= 0x1;
Marek Vasut33f794b2019-06-09 03:50:52 +0200649 writel(tmp, priv->dbi_base + 0x7c);
Marek Vasute9be4292013-12-14 05:55:28 +0100650
651 /* LTSSM enable, starting link. */
652 setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
653
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200654 while (!imx6_pcie_link_up(priv)) {
Marek Vasute9be4292013-12-14 05:55:28 +0100655 udelay(10);
656 count++;
Stefano Babica32b4a02016-06-06 11:14:19 +0200657 if (count >= 4000) {
Tim Harvey378b02d2015-05-08 15:17:10 -0700658#ifdef CONFIG_PCI_SCAN_SHOW
659 puts("PCI: pcie phy link never came up\n");
660#endif
Marek Vasute9be4292013-12-14 05:55:28 +0100661 debug("DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
Marek Vasut33f794b2019-06-09 03:50:52 +0200662 readl(priv->dbi_base + PCIE_PHY_DEBUG_R0),
663 readl(priv->dbi_base + PCIE_PHY_DEBUG_R1));
Marek Vasute9be4292013-12-14 05:55:28 +0100664 return -EINVAL;
665 }
666 }
667
668 return 0;
669}
670
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200671static struct imx_pcie_priv imx_pcie_priv = {
672 .dbi_base = (void __iomem *)MX6_DBI_ADDR,
673 .cfg_base = (void __iomem *)MX6_ROOT_ADDR,
674};
675
676static struct imx_pcie_priv *priv = &imx_pcie_priv;
677
Marek Vasute9be4292013-12-14 05:55:28 +0100678void imx_pcie_init(void)
679{
680 /* Static instance of the controller. */
681 static struct pci_controller pcc;
682 struct pci_controller *hose = &pcc;
683 int ret;
684
685 memset(&pcc, 0, sizeof(pcc));
686
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200687 hose->priv_data = priv;
688
Marek Vasute9be4292013-12-14 05:55:28 +0100689 /* PCI I/O space */
690 pci_set_region(&hose->regions[0],
691 MX6_IO_ADDR, MX6_IO_ADDR,
692 MX6_IO_SIZE, PCI_REGION_IO);
693
694 /* PCI memory space */
695 pci_set_region(&hose->regions[1],
696 MX6_MEM_ADDR, MX6_MEM_ADDR,
697 MX6_MEM_SIZE, PCI_REGION_MEM);
698
699 /* System memory space */
700 pci_set_region(&hose->regions[2],
701 MMDC0_ARB_BASE_ADDR, MMDC0_ARB_BASE_ADDR,
702 0xefffffff, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
703
704 hose->region_count = 3;
705
706 pci_set_ops(hose,
707 pci_hose_read_config_byte_via_dword,
708 pci_hose_read_config_word_via_dword,
709 imx_pcie_read_config,
710 pci_hose_write_config_byte_via_dword,
711 pci_hose_write_config_word_via_dword,
712 imx_pcie_write_config);
713
714 /* Start the controller. */
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200715 ret = imx_pcie_link_up(priv);
Marek Vasute9be4292013-12-14 05:55:28 +0100716
717 if (!ret) {
718 pci_register_hose(hose);
719 hose->last_busno = pci_hose_scan(hose);
720 }
721}
722
Tim Harvey6ecbe132017-05-12 12:58:41 -0700723void imx_pcie_remove(void)
724{
Marek Vasutd2cc2e82019-06-09 03:50:54 +0200725 imx6_pcie_assert_core_reset(priv, true);
Tim Harvey6ecbe132017-05-12 12:58:41 -0700726}
727
Marek Vasute9be4292013-12-14 05:55:28 +0100728/* Probe function. */
729void pci_init_board(void)
730{
731 imx_pcie_init();
732}