blob: 46ac01713ff73f79b86799f4c239be0240789fe9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Marek Vasut988ee702013-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 Vasut988ee702013-12-14 05:55:28 +010010 */
11
12#include <common.h>
Simon Glass18afe102019-11-14 12:57:47 -070013#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
Marek Vasut988ee702013-12-14 05:55:28 +010016#include <pci.h>
Tim Harvey541e2732022-04-13 15:54:37 -070017#include <power/regulator.h>
Marek Vasut988ee702013-12-14 05:55:28 +010018#include <asm/arch/clock.h>
19#include <asm/arch/iomux.h>
20#include <asm/arch/crm_regs.h>
Marek Vasutd9056c22014-02-03 21:46:22 +010021#include <asm/gpio.h>
Marek Vasut988ee702013-12-14 05:55:28 +010022#include <asm/io.h>
Marek Vasut3c9f33e2019-06-09 03:50:55 +020023#include <dm.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Alexey Brodkin267d8e22014-02-26 17:47:58 +040025#include <linux/sizes.h>
Marek Vasut988ee702013-12-14 05:55:28 +010026#include <errno.h>
Fabio Estevamb5272bd2015-10-13 11:01:27 -030027#include <asm/arch/sys_proto.h>
Marek Vasut988ee702013-12-14 05:55:28 +010028
29#define PCI_ACCESS_READ 0
30#define PCI_ACCESS_WRITE 1
31
Fabio Estevam211a4902014-08-25 14:26:45 -030032#ifdef CONFIG_MX6SX
33#define MX6_DBI_ADDR 0x08ffc000
34#define MX6_IO_ADDR 0x08000000
35#define MX6_MEM_ADDR 0x08100000
36#define MX6_ROOT_ADDR 0x08f00000
37#else
Marek Vasut988ee702013-12-14 05:55:28 +010038#define MX6_DBI_ADDR 0x01ffc000
Marek Vasut988ee702013-12-14 05:55:28 +010039#define MX6_IO_ADDR 0x01000000
Marek Vasut988ee702013-12-14 05:55:28 +010040#define MX6_MEM_ADDR 0x01100000
Marek Vasut988ee702013-12-14 05:55:28 +010041#define MX6_ROOT_ADDR 0x01f00000
Fabio Estevam211a4902014-08-25 14:26:45 -030042#endif
43#define MX6_DBI_SIZE 0x4000
44#define MX6_IO_SIZE 0x100000
45#define MX6_MEM_SIZE 0xe00000
Marek Vasut988ee702013-12-14 05:55:28 +010046#define MX6_ROOT_SIZE 0xfc000
47
48/* PCIe Port Logic registers (memory-mapped) */
49#define PL_OFFSET 0x700
Tim Harveyc22f2ea2017-05-12 12:58:41 -070050#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
51#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
52#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Marek Vasut988ee702013-12-14 05:55:28 +010053#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
54#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
55#define PCIE_PHY_DEBUG_R1_LINK_UP (1 << 4)
56#define PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING (1 << 29)
57
58#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
59#define PCIE_PHY_CTRL_DATA_LOC 0
60#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
61#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
62#define PCIE_PHY_CTRL_WR_LOC 18
63#define PCIE_PHY_CTRL_RD_LOC 19
64
65#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
66#define PCIE_PHY_STAT_DATA_LOC 0
67#define PCIE_PHY_STAT_ACK_LOC 16
68
69/* PHY registers (not memory-mapped) */
70#define PCIE_PHY_RX_ASIC_OUT 0x100D
71
72#define PHY_RX_OVRD_IN_LO 0x1005
73#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
74#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
75
Fabio Estevam211a4902014-08-25 14:26:45 -030076#define PCIE_PHY_PUP_REQ (1 << 7)
77
Marek Vasut988ee702013-12-14 05:55:28 +010078/* iATU registers */
79#define PCIE_ATU_VIEWPORT 0x900
80#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
81#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
82#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
83#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
84#define PCIE_ATU_CR1 0x904
85#define PCIE_ATU_TYPE_MEM (0x0 << 0)
86#define PCIE_ATU_TYPE_IO (0x2 << 0)
87#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
88#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
89#define PCIE_ATU_CR2 0x908
90#define PCIE_ATU_ENABLE (0x1 << 31)
91#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
92#define PCIE_ATU_LOWER_BASE 0x90C
93#define PCIE_ATU_UPPER_BASE 0x910
94#define PCIE_ATU_LIMIT 0x914
95#define PCIE_ATU_LOWER_TARGET 0x918
96#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
97#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
98#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
99#define PCIE_ATU_UPPER_TARGET 0x91C
100
Marek Vasut6ce573f2019-06-09 03:50:52 +0200101struct imx_pcie_priv {
102 void __iomem *dbi_base;
103 void __iomem *cfg_base;
Tim Harvey7367b392021-07-06 10:19:09 -0700104 struct gpio_desc reset_gpio;
105 bool reset_active_high;
Tim Harvey541e2732022-04-13 15:54:37 -0700106 struct udevice *vpcie;
Marek Vasut6ce573f2019-06-09 03:50:52 +0200107};
108
Marek Vasut988ee702013-12-14 05:55:28 +0100109/*
110 * PHY access functions
111 */
112static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
113{
114 u32 val;
115 u32 max_iterations = 10;
116 u32 wait_counter = 0;
117
118 do {
119 val = readl(dbi_base + PCIE_PHY_STAT);
120 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
121 wait_counter++;
122
123 if (val == exp_val)
124 return 0;
125
126 udelay(1);
127 } while (wait_counter < max_iterations);
128
129 return -ETIMEDOUT;
130}
131
132static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
133{
134 u32 val;
135 int ret;
136
137 val = addr << PCIE_PHY_CTRL_DATA_LOC;
138 writel(val, dbi_base + PCIE_PHY_CTRL);
139
140 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
141 writel(val, dbi_base + PCIE_PHY_CTRL);
142
143 ret = pcie_phy_poll_ack(dbi_base, 1);
144 if (ret)
145 return ret;
146
147 val = addr << PCIE_PHY_CTRL_DATA_LOC;
148 writel(val, dbi_base + PCIE_PHY_CTRL);
149
150 ret = pcie_phy_poll_ack(dbi_base, 0);
151 if (ret)
152 return ret;
153
154 return 0;
155}
156
157/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
158static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
159{
160 u32 val, phy_ctl;
161 int ret;
162
163 ret = pcie_phy_wait_ack(dbi_base, addr);
164 if (ret)
165 return ret;
166
167 /* assert Read signal */
168 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
169 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
170
171 ret = pcie_phy_poll_ack(dbi_base, 1);
172 if (ret)
173 return ret;
174
175 val = readl(dbi_base + PCIE_PHY_STAT);
176 *data = val & 0xffff;
177
178 /* deassert Read signal */
179 writel(0x00, dbi_base + PCIE_PHY_CTRL);
180
181 ret = pcie_phy_poll_ack(dbi_base, 0);
182 if (ret)
183 return ret;
184
185 return 0;
186}
187
188static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
189{
190 u32 var;
191 int ret;
192
193 /* write addr */
194 /* cap addr */
195 ret = pcie_phy_wait_ack(dbi_base, addr);
196 if (ret)
197 return ret;
198
199 var = data << PCIE_PHY_CTRL_DATA_LOC;
200 writel(var, dbi_base + PCIE_PHY_CTRL);
201
202 /* capture data */
203 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
204 writel(var, dbi_base + PCIE_PHY_CTRL);
205
206 ret = pcie_phy_poll_ack(dbi_base, 1);
207 if (ret)
208 return ret;
209
210 /* deassert cap data */
211 var = data << PCIE_PHY_CTRL_DATA_LOC;
212 writel(var, dbi_base + PCIE_PHY_CTRL);
213
214 /* wait for ack de-assertion */
215 ret = pcie_phy_poll_ack(dbi_base, 0);
216 if (ret)
217 return ret;
218
219 /* assert wr signal */
220 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
221 writel(var, dbi_base + PCIE_PHY_CTRL);
222
223 /* wait for ack */
224 ret = pcie_phy_poll_ack(dbi_base, 1);
225 if (ret)
226 return ret;
227
228 /* deassert wr signal */
229 var = data << PCIE_PHY_CTRL_DATA_LOC;
230 writel(var, dbi_base + PCIE_PHY_CTRL);
231
232 /* wait for ack de-assertion */
233 ret = pcie_phy_poll_ack(dbi_base, 0);
234 if (ret)
235 return ret;
236
237 writel(0x0, dbi_base + PCIE_PHY_CTRL);
238
239 return 0;
240}
241
Marek Vasut120efeb2019-06-09 03:50:54 +0200242static int imx6_pcie_link_up(struct imx_pcie_priv *priv)
Marek Vasut988ee702013-12-14 05:55:28 +0100243{
244 u32 rc, ltssm;
245 int rx_valid, temp;
246
247 /* link is debug bit 36, debug register 1 starts at bit 32 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200248 rc = readl(priv->dbi_base + PCIE_PHY_DEBUG_R1);
Marek Vasut988ee702013-12-14 05:55:28 +0100249 if ((rc & PCIE_PHY_DEBUG_R1_LINK_UP) &&
250 !(rc & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING))
251 return -EAGAIN;
252
253 /*
254 * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
255 * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
256 * If (MAC/LTSSM.state == Recovery.RcvrLock)
257 * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
258 * to gen2 is stuck
259 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200260 pcie_phy_read(priv->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
261 ltssm = readl(priv->dbi_base + PCIE_PHY_DEBUG_R0) & 0x3F;
Marek Vasut988ee702013-12-14 05:55:28 +0100262
263 if (rx_valid & 0x01)
264 return 0;
265
266 if (ltssm != 0x0d)
267 return 0;
268
269 printf("transition to gen2 is stuck, reset PHY!\n");
270
Marek Vasut6ce573f2019-06-09 03:50:52 +0200271 pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
Marek Vasut988ee702013-12-14 05:55:28 +0100272 temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Marek Vasut6ce573f2019-06-09 03:50:52 +0200273 pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
Marek Vasut988ee702013-12-14 05:55:28 +0100274
275 udelay(3000);
276
Marek Vasut6ce573f2019-06-09 03:50:52 +0200277 pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
Marek Vasut988ee702013-12-14 05:55:28 +0100278 temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Marek Vasut6ce573f2019-06-09 03:50:52 +0200279 pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
Marek Vasut988ee702013-12-14 05:55:28 +0100280
281 return 0;
282}
283
284/*
285 * iATU region setup
286 */
Marek Vasut120efeb2019-06-09 03:50:54 +0200287static int imx_pcie_regions_setup(struct imx_pcie_priv *priv)
Marek Vasut988ee702013-12-14 05:55:28 +0100288{
289 /*
290 * i.MX6 defines 16MB in the AXI address map for PCIe.
291 *
292 * That address space excepted the pcie registers is
293 * split and defined into different regions by iATU,
294 * with sizes and offsets as follows:
295 *
296 * 0x0100_0000 --- 0x010F_FFFF 1MB IORESOURCE_IO
297 * 0x0110_0000 --- 0x01EF_FFFF 14MB IORESOURCE_MEM
298 * 0x01F0_0000 --- 0x01FF_FFFF 1MB Cfg + Registers
299 */
300
301 /* CMD reg:I/O space, MEM space, and Bus Master Enable */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200302 setbits_le32(priv->dbi_base + PCI_COMMAND,
Marek Vasut988ee702013-12-14 05:55:28 +0100303 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
304
Pali Rohár25781e22022-02-18 13:18:40 +0100305 /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI_NORMAL */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200306 setbits_le32(priv->dbi_base + PCI_CLASS_REVISION,
Pali Rohár25781e22022-02-18 13:18:40 +0100307 PCI_CLASS_BRIDGE_PCI_NORMAL << 8);
Marek Vasut988ee702013-12-14 05:55:28 +0100308
309 /* Region #0 is used for Outbound CFG space access. */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200310 writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
Marek Vasut988ee702013-12-14 05:55:28 +0100311
Marek Vasut2c20b752019-06-09 03:50:53 +0200312 writel(lower_32_bits((uintptr_t)priv->cfg_base),
313 priv->dbi_base + PCIE_ATU_LOWER_BASE);
314 writel(upper_32_bits((uintptr_t)priv->cfg_base),
315 priv->dbi_base + PCIE_ATU_UPPER_BASE);
316 writel(lower_32_bits((uintptr_t)priv->cfg_base + MX6_ROOT_SIZE),
Marek Vasut6ce573f2019-06-09 03:50:52 +0200317 priv->dbi_base + PCIE_ATU_LIMIT);
Marek Vasut988ee702013-12-14 05:55:28 +0100318
Marek Vasut6ce573f2019-06-09 03:50:52 +0200319 writel(0, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
320 writel(0, priv->dbi_base + PCIE_ATU_UPPER_TARGET);
321 writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
322 writel(PCIE_ATU_ENABLE, priv->dbi_base + PCIE_ATU_CR2);
Marek Vasut988ee702013-12-14 05:55:28 +0100323
324 return 0;
325}
326
327/*
328 * PCI Express accessors
329 */
Marek Vasut120efeb2019-06-09 03:50:54 +0200330static void __iomem *get_bus_address(struct imx_pcie_priv *priv,
331 pci_dev_t d, int where)
Marek Vasut988ee702013-12-14 05:55:28 +0100332{
Marek Vasut2c20b752019-06-09 03:50:53 +0200333 void __iomem *va_address;
Marek Vasut988ee702013-12-14 05:55:28 +0100334
335 /* Reconfigure Region #0 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200336 writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
Marek Vasut988ee702013-12-14 05:55:28 +0100337
338 if (PCI_BUS(d) < 2)
Marek Vasut6ce573f2019-06-09 03:50:52 +0200339 writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
Marek Vasut988ee702013-12-14 05:55:28 +0100340 else
Marek Vasut6ce573f2019-06-09 03:50:52 +0200341 writel(PCIE_ATU_TYPE_CFG1, priv->dbi_base + PCIE_ATU_CR1);
Marek Vasut988ee702013-12-14 05:55:28 +0100342
343 if (PCI_BUS(d) == 0) {
Marek Vasut2c20b752019-06-09 03:50:53 +0200344 va_address = priv->dbi_base;
Marek Vasut988ee702013-12-14 05:55:28 +0100345 } else {
Marek Vasut6ce573f2019-06-09 03:50:52 +0200346 writel(d << 8, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
Marek Vasut2c20b752019-06-09 03:50:53 +0200347 va_address = priv->cfg_base;
Marek Vasut988ee702013-12-14 05:55:28 +0100348 }
349
350 va_address += (where & ~0x3);
351
352 return va_address;
353}
354
355static int imx_pcie_addr_valid(pci_dev_t d)
356{
357 if ((PCI_BUS(d) == 0) && (PCI_DEV(d) > 1))
358 return -EINVAL;
359 if ((PCI_BUS(d) == 1) && (PCI_DEV(d) > 0))
360 return -EINVAL;
361 return 0;
362}
363
364/*
365 * Replace the original ARM DABT handler with a simple jump-back one.
366 *
367 * The problem here is that if we have a PCIe bridge attached to this PCIe
368 * controller, but no PCIe device is connected to the bridges' downstream
369 * port, the attempt to read/write from/to the config space will produce
370 * a DABT. This is a behavior of the controller and can not be disabled
371 * unfortuatelly.
372 *
373 * To work around the problem, we backup the current DABT handler address
374 * and replace it with our own DABT handler, which only bounces right back
375 * into the code.
376 */
377static void imx_pcie_fix_dabt_handler(bool set)
378{
379 extern uint32_t *_data_abort;
380 uint32_t *data_abort_addr = (uint32_t *)&_data_abort;
381
382 static const uint32_t data_abort_bounce_handler = 0xe25ef004;
383 uint32_t data_abort_bounce_addr = (uint32_t)&data_abort_bounce_handler;
384
385 static uint32_t data_abort_backup;
386
387 if (set) {
388 data_abort_backup = *data_abort_addr;
389 *data_abort_addr = data_abort_bounce_addr;
390 } else {
391 *data_abort_addr = data_abort_backup;
392 }
393}
394
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200395static int imx_pcie_read_cfg(struct imx_pcie_priv *priv, pci_dev_t d,
396 int where, u32 *val)
Marek Vasut988ee702013-12-14 05:55:28 +0100397{
Marek Vasut2c20b752019-06-09 03:50:53 +0200398 void __iomem *va_address;
Marek Vasut988ee702013-12-14 05:55:28 +0100399 int ret;
400
401 ret = imx_pcie_addr_valid(d);
402 if (ret) {
403 *val = 0xffffffff;
Bin Meng122c7662016-01-08 01:03:20 -0800404 return 0;
Marek Vasut988ee702013-12-14 05:55:28 +0100405 }
406
Marek Vasut120efeb2019-06-09 03:50:54 +0200407 va_address = get_bus_address(priv, d, where);
Marek Vasut988ee702013-12-14 05:55:28 +0100408
409 /*
410 * Read the PCIe config space. We must replace the DABT handler
411 * here in case we got data abort from the PCIe controller, see
412 * imx_pcie_fix_dabt_handler() description. Note that writing the
413 * "val" with valid value is also imperative here as in case we
414 * did got DABT, the val would contain random value.
415 */
416 imx_pcie_fix_dabt_handler(true);
417 writel(0xffffffff, val);
418 *val = readl(va_address);
419 imx_pcie_fix_dabt_handler(false);
420
421 return 0;
422}
423
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200424static int imx_pcie_write_cfg(struct imx_pcie_priv *priv, pci_dev_t d,
425 int where, u32 val)
Marek Vasut988ee702013-12-14 05:55:28 +0100426{
Marek Vasut2c20b752019-06-09 03:50:53 +0200427 void __iomem *va_address = NULL;
Marek Vasut988ee702013-12-14 05:55:28 +0100428 int ret;
429
430 ret = imx_pcie_addr_valid(d);
431 if (ret)
432 return ret;
433
Marek Vasut120efeb2019-06-09 03:50:54 +0200434 va_address = get_bus_address(priv, d, where);
Marek Vasut988ee702013-12-14 05:55:28 +0100435
436 /*
437 * Write the PCIe config space. We must replace the DABT handler
438 * here in case we got data abort from the PCIe controller, see
439 * imx_pcie_fix_dabt_handler() description.
440 */
441 imx_pcie_fix_dabt_handler(true);
442 writel(val, va_address);
443 imx_pcie_fix_dabt_handler(false);
444
445 return 0;
446}
447
448/*
449 * Initial bus setup
450 */
Marek Vasut120efeb2019-06-09 03:50:54 +0200451static int imx6_pcie_assert_core_reset(struct imx_pcie_priv *priv,
452 bool prepare_for_boot)
Marek Vasut988ee702013-12-14 05:55:28 +0100453{
454 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
Fabio Estevamb5272bd2015-10-13 11:01:27 -0300455
456 if (is_mx6dqp())
457 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
458
Fabio Estevam211a4902014-08-25 14:26:45 -0300459#if defined(CONFIG_MX6SX)
460 struct gpc *gpc_regs = (struct gpc *)GPC_BASE_ADDR;
Marek Vasut988ee702013-12-14 05:55:28 +0100461
Fabio Estevam211a4902014-08-25 14:26:45 -0300462 /* SSP_EN is not used on MX6SX anymore */
463 setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
464 /* Force PCIe PHY reset */
465 setbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
466 /* Power up PCIe PHY */
467 setbits_le32(&gpc_regs->cntr, PCIE_PHY_PUP_REQ);
468#else
Tim Harveyc22f2ea2017-05-12 12:58:41 -0700469 /*
470 * If the bootloader already enabled the link we need some special
471 * handling to get the core back into a state where it is safe to
472 * touch it for configuration. As there is no dedicated reset signal
473 * wired up for MX6QDL, we need to manually force LTSSM into "detect"
474 * state before completely disabling LTSSM, which is a prerequisite
475 * for core configuration.
476 *
477 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
478 * indication that the bootloader activated the link.
479 */
Tim Harvey3b10a832021-04-16 13:30:41 -0700480 if ((is_mx6dq() || is_mx6sdl()) && prepare_for_boot) {
Tim Harveyc22f2ea2017-05-12 12:58:41 -0700481 u32 val, gpr1, gpr12;
482
483 gpr1 = readl(&iomuxc_regs->gpr[1]);
484 gpr12 = readl(&iomuxc_regs->gpr[12]);
485 if ((gpr1 & IOMUXC_GPR1_PCIE_REF_CLK_EN) &&
486 (gpr12 & IOMUXC_GPR12_PCIE_CTL_2)) {
Marek Vasut6ce573f2019-06-09 03:50:52 +0200487 val = readl(priv->dbi_base + PCIE_PL_PFLR);
Tim Harveyc22f2ea2017-05-12 12:58:41 -0700488 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
489 val |= PCIE_PL_PFLR_FORCE_LINK;
490
491 imx_pcie_fix_dabt_handler(true);
Marek Vasut6ce573f2019-06-09 03:50:52 +0200492 writel(val, priv->dbi_base + PCIE_PL_PFLR);
Tim Harveyc22f2ea2017-05-12 12:58:41 -0700493 imx_pcie_fix_dabt_handler(false);
494
495 gpr12 &= ~IOMUXC_GPR12_PCIE_CTL_2;
496 writel(val, &iomuxc_regs->gpr[12]);
497 }
498 }
Marek Vasut988ee702013-12-14 05:55:28 +0100499 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
500 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
Fabio Estevam211a4902014-08-25 14:26:45 -0300501#endif
Marek Vasut988ee702013-12-14 05:55:28 +0100502
503 return 0;
504}
505
506static int imx6_pcie_init_phy(void)
507{
508 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
509
510 clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
511
512 clrsetbits_le32(&iomuxc_regs->gpr[12],
513 IOMUXC_GPR12_DEVICE_TYPE_MASK,
514 IOMUXC_GPR12_DEVICE_TYPE_RC);
515 clrsetbits_le32(&iomuxc_regs->gpr[12],
516 IOMUXC_GPR12_LOS_LEVEL_MASK,
517 IOMUXC_GPR12_LOS_LEVEL_9);
518
Fabio Estevam211a4902014-08-25 14:26:45 -0300519#ifdef CONFIG_MX6SX
520 clrsetbits_le32(&iomuxc_regs->gpr[12],
521 IOMUXC_GPR12_RX_EQ_MASK,
522 IOMUXC_GPR12_RX_EQ_2);
523#endif
524
Marek Vasut988ee702013-12-14 05:55:28 +0100525 writel((0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN1_OFFSET) |
526 (0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_3P5DB_OFFSET) |
527 (20 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_6DB_OFFSET) |
528 (127 << IOMUXC_GPR8_PCS_TX_SWING_FULL_OFFSET) |
529 (127 << IOMUXC_GPR8_PCS_TX_SWING_LOW_OFFSET),
530 &iomuxc_regs->gpr[8]);
531
532 return 0;
533}
534
Tim Harveybc6b7f12022-04-13 15:57:37 -0700535int imx6_pcie_toggle_power(struct udevice *vpcie)
Marek Vasut77779a42014-03-23 22:45:40 +0100536{
537#ifdef CONFIG_PCIE_IMX_POWER_GPIO
Peng Fan08e93382018-01-02 18:27:29 +0800538 gpio_request(CONFIG_PCIE_IMX_POWER_GPIO, "pcie_power");
Marek Vasut77779a42014-03-23 22:45:40 +0100539 gpio_direction_output(CONFIG_PCIE_IMX_POWER_GPIO, 0);
540 mdelay(20);
541 gpio_set_value(CONFIG_PCIE_IMX_POWER_GPIO, 1);
542 mdelay(20);
Peng Fan08e93382018-01-02 18:27:29 +0800543 gpio_free(CONFIG_PCIE_IMX_POWER_GPIO);
Marek Vasut77779a42014-03-23 22:45:40 +0100544#endif
Tim Harvey541e2732022-04-13 15:54:37 -0700545
546#if CONFIG_IS_ENABLED(DM_REGULATOR)
547 if (vpcie) {
548 regulator_set_enable(vpcie, false);
549 mdelay(20);
550 regulator_set_enable(vpcie, true);
551 mdelay(20);
552 }
553#endif
Marek Vasut77779a42014-03-23 22:45:40 +0100554 return 0;
555}
556
Tim Harveybc6b7f12022-04-13 15:57:37 -0700557int imx6_pcie_toggle_reset(struct gpio_desc *gpio, bool active_high)
Marek Vasutd9056c22014-02-03 21:46:22 +0100558{
559 /*
560 * See 'PCI EXPRESS BASE SPECIFICATION, REV 3.0, SECTION 6.6.1'
561 * for detailed understanding of the PCIe CR reset logic.
562 *
563 * The PCIe #PERST reset line _MUST_ be connected, otherwise your
564 * design does not conform to the specification. You must wait at
Fabio Estevam1d0b7e32015-09-10 20:45:25 -0300565 * least 20 ms after de-asserting the #PERST so the EP device can
Marek Vasutd9056c22014-02-03 21:46:22 +0100566 * do self-initialisation.
567 *
568 * In case your #PERST pin is connected to a plain GPIO pin of the
569 * CPU, you can define CONFIG_PCIE_IMX_PERST_GPIO in your board's
570 * configuration file and the condition below will handle the rest
571 * of the reset toggling.
572 *
Marek Vasutd9056c22014-02-03 21:46:22 +0100573 * In case your #PERST line of the PCIe EP device is not connected
574 * at all, your design is broken and you should fix your design,
575 * otherwise you will observe problems like for example the link
576 * not coming up after rebooting the system back from running Linux
577 * that uses the PCIe as well OR the PCIe link might not come up in
578 * Linux at all in the first place since it's in some non-reset
579 * state due to being previously used in U-Boot.
580 */
581#ifdef CONFIG_PCIE_IMX_PERST_GPIO
Peng Fan08e93382018-01-02 18:27:29 +0800582 gpio_request(CONFIG_PCIE_IMX_PERST_GPIO, "pcie_reset");
Marek Vasutd9056c22014-02-03 21:46:22 +0100583 gpio_direction_output(CONFIG_PCIE_IMX_PERST_GPIO, 0);
584 mdelay(20);
585 gpio_set_value(CONFIG_PCIE_IMX_PERST_GPIO, 1);
586 mdelay(20);
Peng Fan08e93382018-01-02 18:27:29 +0800587 gpio_free(CONFIG_PCIE_IMX_PERST_GPIO);
Marek Vasutd9056c22014-02-03 21:46:22 +0100588#else
Tim Harvey7367b392021-07-06 10:19:09 -0700589 if (dm_gpio_is_valid(gpio)) {
590 /* Assert PERST# for 20ms then de-assert */
591 dm_gpio_set_value(gpio, active_high ? 0 : 1);
592 mdelay(20);
593 dm_gpio_set_value(gpio, active_high ? 1 : 0);
594 mdelay(20);
595 } else {
596 puts("WARNING: Make sure the PCIe #PERST line is connected!\n");
597 }
Marek Vasutd9056c22014-02-03 21:46:22 +0100598#endif
599 return 0;
600}
601
Tim Harvey7367b392021-07-06 10:19:09 -0700602static int imx6_pcie_deassert_core_reset(struct imx_pcie_priv *priv)
Marek Vasut988ee702013-12-14 05:55:28 +0100603{
604 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
605
Tim Harvey541e2732022-04-13 15:54:37 -0700606 imx6_pcie_toggle_power(priv->vpcie);
Marek Vasut988ee702013-12-14 05:55:28 +0100607
Marek Vasut988ee702013-12-14 05:55:28 +0100608 enable_pcie_clock();
609
Fabio Estevamb5272bd2015-10-13 11:01:27 -0300610 if (is_mx6dqp())
611 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
612
Marek Vasut988ee702013-12-14 05:55:28 +0100613 /*
614 * Wait for the clock to settle a bit, when the clock are sourced
Fabio Estevam1d0b7e32015-09-10 20:45:25 -0300615 * from the CPU, we need about 30 ms to settle.
Marek Vasut988ee702013-12-14 05:55:28 +0100616 */
Marek Vasutd9056c22014-02-03 21:46:22 +0100617 mdelay(50);
Marek Vasut988ee702013-12-14 05:55:28 +0100618
Fabio Estevam211a4902014-08-25 14:26:45 -0300619#if defined(CONFIG_MX6SX)
620 /* SSP_EN is not used on MX6SX anymore */
621 clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
622 /* Clear PCIe PHY reset bit */
623 clrbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
624#else
Tim Harveyd53204a2014-08-07 22:57:29 -0700625 /* Enable PCIe */
626 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
627 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
Fabio Estevam211a4902014-08-25 14:26:45 -0300628#endif
Tim Harveyd53204a2014-08-07 22:57:29 -0700629
Tim Harvey7367b392021-07-06 10:19:09 -0700630 imx6_pcie_toggle_reset(&priv->reset_gpio, priv->reset_active_high);
Marek Vasut988ee702013-12-14 05:55:28 +0100631
632 return 0;
633}
634
Marek Vasut120efeb2019-06-09 03:50:54 +0200635static int imx_pcie_link_up(struct imx_pcie_priv *priv)
Marek Vasut988ee702013-12-14 05:55:28 +0100636{
637 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
638 uint32_t tmp;
639 int count = 0;
640
Marek Vasut120efeb2019-06-09 03:50:54 +0200641 imx6_pcie_assert_core_reset(priv, false);
Marek Vasut988ee702013-12-14 05:55:28 +0100642 imx6_pcie_init_phy();
Tim Harvey7367b392021-07-06 10:19:09 -0700643 imx6_pcie_deassert_core_reset(priv);
Marek Vasut988ee702013-12-14 05:55:28 +0100644
Marek Vasut120efeb2019-06-09 03:50:54 +0200645 imx_pcie_regions_setup(priv);
Marek Vasut988ee702013-12-14 05:55:28 +0100646
647 /*
Koen Vandeputte6be35052018-01-04 14:54:34 +0100648 * By default, the subordinate is set equally to the secondary
649 * bus (0x01) when the RC boots.
650 * This means that theoretically, only bus 1 is reachable from the RC.
651 * Force the PCIe RC subordinate to 0xff, otherwise no downstream
652 * devices will be detected if the enumeration is applied strictly.
653 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200654 tmp = readl(priv->dbi_base + 0x18);
Koen Vandeputte6be35052018-01-04 14:54:34 +0100655 tmp |= (0xff << 16);
Marek Vasut6ce573f2019-06-09 03:50:52 +0200656 writel(tmp, priv->dbi_base + 0x18);
Koen Vandeputte6be35052018-01-04 14:54:34 +0100657
658 /*
Marek Vasut988ee702013-12-14 05:55:28 +0100659 * FIXME: Force the PCIe RC to Gen1 operation
660 * The RC must be forced into Gen1 mode before bringing the link
661 * up, otherwise no downstream devices are detected. After the
662 * link is up, a managed Gen1->Gen2 transition can be initiated.
663 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200664 tmp = readl(priv->dbi_base + 0x7c);
Marek Vasut988ee702013-12-14 05:55:28 +0100665 tmp &= ~0xf;
666 tmp |= 0x1;
Marek Vasut6ce573f2019-06-09 03:50:52 +0200667 writel(tmp, priv->dbi_base + 0x7c);
Marek Vasut988ee702013-12-14 05:55:28 +0100668
669 /* LTSSM enable, starting link. */
670 setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
671
Marek Vasut120efeb2019-06-09 03:50:54 +0200672 while (!imx6_pcie_link_up(priv)) {
Marek Vasut988ee702013-12-14 05:55:28 +0100673 udelay(10);
674 count++;
Stefano Babicd1457922016-06-06 11:14:19 +0200675 if (count >= 4000) {
Tim Harveyc3260002015-05-08 15:17:10 -0700676#ifdef CONFIG_PCI_SCAN_SHOW
677 puts("PCI: pcie phy link never came up\n");
678#endif
Marek Vasut988ee702013-12-14 05:55:28 +0100679 debug("DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
Marek Vasut6ce573f2019-06-09 03:50:52 +0200680 readl(priv->dbi_base + PCIE_PHY_DEBUG_R0),
681 readl(priv->dbi_base + PCIE_PHY_DEBUG_R1));
Marek Vasut988ee702013-12-14 05:55:28 +0100682 return -EINVAL;
683 }
684 }
685
686 return 0;
687}
688
Simon Glass2a311e82020-01-27 08:49:37 -0700689static int imx_pcie_dm_read_config(const struct udevice *dev, pci_dev_t bdf,
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200690 uint offset, ulong *value,
691 enum pci_size_t size)
692{
693 struct imx_pcie_priv *priv = dev_get_priv(dev);
694 u32 tmpval;
695 int ret;
696
697 ret = imx_pcie_read_cfg(priv, bdf, offset, &tmpval);
698 if (ret)
699 return ret;
700
701 *value = pci_conv_32_to_size(tmpval, offset, size);
702 return 0;
703}
704
705static int imx_pcie_dm_write_config(struct udevice *dev, pci_dev_t bdf,
706 uint offset, ulong value,
707 enum pci_size_t size)
708{
709 struct imx_pcie_priv *priv = dev_get_priv(dev);
710 u32 tmpval, newval;
711 int ret;
712
713 ret = imx_pcie_read_cfg(priv, bdf, offset, &tmpval);
714 if (ret)
715 return ret;
716
717 newval = pci_conv_size_to_32(tmpval, value, offset, size);
718 return imx_pcie_write_cfg(priv, bdf, offset, newval);
719}
720
721static int imx_pcie_dm_probe(struct udevice *dev)
722{
723 struct imx_pcie_priv *priv = dev_get_priv(dev);
724
Tim Harvey541e2732022-04-13 15:54:37 -0700725#if CONFIG_IS_ENABLED(DM_REGULATOR)
726 device_get_supply_regulator(dev, "vpcie-supply", &priv->vpcie);
727#endif
728
Tim Harvey7367b392021-07-06 10:19:09 -0700729 /* if PERST# valid from dt then assert it */
730 gpio_request_by_name(dev, "reset-gpio", 0, &priv->reset_gpio,
731 GPIOD_IS_OUT);
732 priv->reset_active_high = dev_read_bool(dev, "reset-gpio-active-high");
733 if (dm_gpio_is_valid(&priv->reset_gpio)) {
734 dm_gpio_set_value(&priv->reset_gpio,
735 priv->reset_active_high ? 0 : 1);
736 }
737
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200738 return imx_pcie_link_up(priv);
739}
740
741static int imx_pcie_dm_remove(struct udevice *dev)
742{
743 struct imx_pcie_priv *priv = dev_get_priv(dev);
744
745 imx6_pcie_assert_core_reset(priv, true);
746
747 return 0;
748}
749
Simon Glassaad29ae2020-12-03 16:55:21 -0700750static int imx_pcie_of_to_plat(struct udevice *dev)
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200751{
752 struct imx_pcie_priv *priv = dev_get_priv(dev);
753
754 priv->dbi_base = (void __iomem *)devfdt_get_addr_index(dev, 0);
755 priv->cfg_base = (void __iomem *)devfdt_get_addr_index(dev, 1);
756 if (!priv->dbi_base || !priv->cfg_base)
757 return -EINVAL;
758
759 return 0;
760}
761
762static const struct dm_pci_ops imx_pcie_ops = {
763 .read_config = imx_pcie_dm_read_config,
764 .write_config = imx_pcie_dm_write_config,
765};
766
767static const struct udevice_id imx_pcie_ids[] = {
768 { .compatible = "fsl,imx6q-pcie" },
Marek Vasutbec7be12019-11-26 09:33:29 +0100769 { .compatible = "fsl,imx6sx-pcie" },
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200770 { }
771};
772
773U_BOOT_DRIVER(imx_pcie) = {
774 .name = "imx_pcie",
775 .id = UCLASS_PCI,
776 .of_match = imx_pcie_ids,
777 .ops = &imx_pcie_ops,
778 .probe = imx_pcie_dm_probe,
779 .remove = imx_pcie_dm_remove,
Simon Glassaad29ae2020-12-03 16:55:21 -0700780 .of_to_plat = imx_pcie_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700781 .priv_auto = sizeof(struct imx_pcie_priv),
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200782 .flags = DM_FLAG_OS_PREPARE,
783};