blob: 11c4ccbfc5553a826f72c40d4ad1c5a7e8aae07c [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>
Sumit Garg3fc43942024-03-21 20:25:04 +053010 *
11 * This is a legacy PCIe iMX driver kept to support older iMX6 SoCs. It is
12 * rather tied to quite old port of pcie-designware driver from Linux which
13 * suffices only iMX6 specific needs. But now we have modern PCIe iMX driver
14 * (drivers/pci/pcie_dw_imx.c) utilizing all the common DWC specific bits from
15 * (drivers/pci/pcie_dw_common.*). So you are encouraged to add any further iMX
16 * SoC support there or even better if you posses older iMX6 SoCs then switch
17 * those too in order to have a single modern PCIe iMX driver.
Marek Vasut988ee702013-12-14 05:55:28 +010018 */
19
Simon Glass18afe102019-11-14 12:57:47 -070020#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060021#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <malloc.h>
Marek Vasut988ee702013-12-14 05:55:28 +010023#include <pci.h>
Tim Harvey541e2732022-04-13 15:54:37 -070024#include <power/regulator.h>
Marek Vasut988ee702013-12-14 05:55:28 +010025#include <asm/arch/clock.h>
26#include <asm/arch/iomux.h>
27#include <asm/arch/crm_regs.h>
Marek Vasutd9056c22014-02-03 21:46:22 +010028#include <asm/gpio.h>
Marek Vasut988ee702013-12-14 05:55:28 +010029#include <asm/io.h>
Marek Vasut3c9f33e2019-06-09 03:50:55 +020030#include <dm.h>
Simon Glassdbd79542020-05-10 11:40:11 -060031#include <linux/delay.h>
Alexey Brodkin267d8e22014-02-26 17:47:58 +040032#include <linux/sizes.h>
Marek Vasut988ee702013-12-14 05:55:28 +010033#include <errno.h>
Fabio Estevamb5272bd2015-10-13 11:01:27 -030034#include <asm/arch/sys_proto.h>
Marek Vasut988ee702013-12-14 05:55:28 +010035
36#define PCI_ACCESS_READ 0
37#define PCI_ACCESS_WRITE 1
38
Fabio Estevam211a4902014-08-25 14:26:45 -030039#ifdef CONFIG_MX6SX
40#define MX6_DBI_ADDR 0x08ffc000
41#define MX6_IO_ADDR 0x08000000
42#define MX6_MEM_ADDR 0x08100000
43#define MX6_ROOT_ADDR 0x08f00000
44#else
Marek Vasut988ee702013-12-14 05:55:28 +010045#define MX6_DBI_ADDR 0x01ffc000
Marek Vasut988ee702013-12-14 05:55:28 +010046#define MX6_IO_ADDR 0x01000000
Marek Vasut988ee702013-12-14 05:55:28 +010047#define MX6_MEM_ADDR 0x01100000
Marek Vasut988ee702013-12-14 05:55:28 +010048#define MX6_ROOT_ADDR 0x01f00000
Fabio Estevam211a4902014-08-25 14:26:45 -030049#endif
50#define MX6_DBI_SIZE 0x4000
51#define MX6_IO_SIZE 0x100000
52#define MX6_MEM_SIZE 0xe00000
Marek Vasut988ee702013-12-14 05:55:28 +010053#define MX6_ROOT_SIZE 0xfc000
54
55/* PCIe Port Logic registers (memory-mapped) */
56#define PL_OFFSET 0x700
Tim Harveyc22f2ea2017-05-12 12:58:41 -070057#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
58#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
59#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Marek Vasut988ee702013-12-14 05:55:28 +010060#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
61#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
62#define PCIE_PHY_DEBUG_R1_LINK_UP (1 << 4)
63#define PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING (1 << 29)
64
65#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
66#define PCIE_PHY_CTRL_DATA_LOC 0
67#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
68#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
69#define PCIE_PHY_CTRL_WR_LOC 18
70#define PCIE_PHY_CTRL_RD_LOC 19
71
72#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
73#define PCIE_PHY_STAT_DATA_LOC 0
74#define PCIE_PHY_STAT_ACK_LOC 16
75
76/* PHY registers (not memory-mapped) */
77#define PCIE_PHY_RX_ASIC_OUT 0x100D
78
79#define PHY_RX_OVRD_IN_LO 0x1005
80#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
81#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
82
Fabio Estevam211a4902014-08-25 14:26:45 -030083#define PCIE_PHY_PUP_REQ (1 << 7)
84
Marek Vasut988ee702013-12-14 05:55:28 +010085/* iATU registers */
86#define PCIE_ATU_VIEWPORT 0x900
87#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
88#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
89#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
90#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
91#define PCIE_ATU_CR1 0x904
92#define PCIE_ATU_TYPE_MEM (0x0 << 0)
93#define PCIE_ATU_TYPE_IO (0x2 << 0)
94#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
95#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
96#define PCIE_ATU_CR2 0x908
97#define PCIE_ATU_ENABLE (0x1 << 31)
98#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
99#define PCIE_ATU_LOWER_BASE 0x90C
100#define PCIE_ATU_UPPER_BASE 0x910
101#define PCIE_ATU_LIMIT 0x914
102#define PCIE_ATU_LOWER_TARGET 0x918
103#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
104#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
105#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
106#define PCIE_ATU_UPPER_TARGET 0x91C
107
Marek Vasut6ce573f2019-06-09 03:50:52 +0200108struct imx_pcie_priv {
109 void __iomem *dbi_base;
110 void __iomem *cfg_base;
Tim Harvey7367b392021-07-06 10:19:09 -0700111 struct gpio_desc reset_gpio;
112 bool reset_active_high;
Tim Harvey541e2732022-04-13 15:54:37 -0700113 struct udevice *vpcie;
Marek Vasut6ce573f2019-06-09 03:50:52 +0200114};
115
Marek Vasut988ee702013-12-14 05:55:28 +0100116/*
117 * PHY access functions
118 */
119static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
120{
121 u32 val;
122 u32 max_iterations = 10;
123 u32 wait_counter = 0;
124
125 do {
126 val = readl(dbi_base + PCIE_PHY_STAT);
127 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
128 wait_counter++;
129
130 if (val == exp_val)
131 return 0;
132
133 udelay(1);
134 } while (wait_counter < max_iterations);
135
136 return -ETIMEDOUT;
137}
138
139static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
140{
141 u32 val;
142 int ret;
143
144 val = addr << PCIE_PHY_CTRL_DATA_LOC;
145 writel(val, dbi_base + PCIE_PHY_CTRL);
146
147 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
148 writel(val, dbi_base + PCIE_PHY_CTRL);
149
150 ret = pcie_phy_poll_ack(dbi_base, 1);
151 if (ret)
152 return ret;
153
154 val = addr << PCIE_PHY_CTRL_DATA_LOC;
155 writel(val, dbi_base + PCIE_PHY_CTRL);
156
157 ret = pcie_phy_poll_ack(dbi_base, 0);
158 if (ret)
159 return ret;
160
161 return 0;
162}
163
164/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
165static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
166{
167 u32 val, phy_ctl;
168 int ret;
169
170 ret = pcie_phy_wait_ack(dbi_base, addr);
171 if (ret)
172 return ret;
173
174 /* assert Read signal */
175 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
176 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
177
178 ret = pcie_phy_poll_ack(dbi_base, 1);
179 if (ret)
180 return ret;
181
182 val = readl(dbi_base + PCIE_PHY_STAT);
183 *data = val & 0xffff;
184
185 /* deassert Read signal */
186 writel(0x00, dbi_base + PCIE_PHY_CTRL);
187
188 ret = pcie_phy_poll_ack(dbi_base, 0);
189 if (ret)
190 return ret;
191
192 return 0;
193}
194
195static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
196{
197 u32 var;
198 int ret;
199
200 /* write addr */
201 /* cap addr */
202 ret = pcie_phy_wait_ack(dbi_base, addr);
203 if (ret)
204 return ret;
205
206 var = data << PCIE_PHY_CTRL_DATA_LOC;
207 writel(var, dbi_base + PCIE_PHY_CTRL);
208
209 /* capture data */
210 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
211 writel(var, dbi_base + PCIE_PHY_CTRL);
212
213 ret = pcie_phy_poll_ack(dbi_base, 1);
214 if (ret)
215 return ret;
216
217 /* deassert cap data */
218 var = data << PCIE_PHY_CTRL_DATA_LOC;
219 writel(var, dbi_base + PCIE_PHY_CTRL);
220
221 /* wait for ack de-assertion */
222 ret = pcie_phy_poll_ack(dbi_base, 0);
223 if (ret)
224 return ret;
225
226 /* assert wr signal */
227 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
228 writel(var, dbi_base + PCIE_PHY_CTRL);
229
230 /* wait for ack */
231 ret = pcie_phy_poll_ack(dbi_base, 1);
232 if (ret)
233 return ret;
234
235 /* deassert wr signal */
236 var = data << PCIE_PHY_CTRL_DATA_LOC;
237 writel(var, dbi_base + PCIE_PHY_CTRL);
238
239 /* wait for ack de-assertion */
240 ret = pcie_phy_poll_ack(dbi_base, 0);
241 if (ret)
242 return ret;
243
244 writel(0x0, dbi_base + PCIE_PHY_CTRL);
245
246 return 0;
247}
248
Marek Vasut120efeb2019-06-09 03:50:54 +0200249static int imx6_pcie_link_up(struct imx_pcie_priv *priv)
Marek Vasut988ee702013-12-14 05:55:28 +0100250{
251 u32 rc, ltssm;
252 int rx_valid, temp;
253
254 /* link is debug bit 36, debug register 1 starts at bit 32 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200255 rc = readl(priv->dbi_base + PCIE_PHY_DEBUG_R1);
Marek Vasut988ee702013-12-14 05:55:28 +0100256 if ((rc & PCIE_PHY_DEBUG_R1_LINK_UP) &&
257 !(rc & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING))
258 return -EAGAIN;
259
260 /*
261 * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
262 * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
263 * If (MAC/LTSSM.state == Recovery.RcvrLock)
264 * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
265 * to gen2 is stuck
266 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200267 pcie_phy_read(priv->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
268 ltssm = readl(priv->dbi_base + PCIE_PHY_DEBUG_R0) & 0x3F;
Marek Vasut988ee702013-12-14 05:55:28 +0100269
270 if (rx_valid & 0x01)
271 return 0;
272
273 if (ltssm != 0x0d)
274 return 0;
275
276 printf("transition to gen2 is stuck, reset PHY!\n");
277
Marek Vasut6ce573f2019-06-09 03:50:52 +0200278 pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
Marek Vasut988ee702013-12-14 05:55:28 +0100279 temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Marek Vasut6ce573f2019-06-09 03:50:52 +0200280 pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
Marek Vasut988ee702013-12-14 05:55:28 +0100281
282 udelay(3000);
283
Marek Vasut6ce573f2019-06-09 03:50:52 +0200284 pcie_phy_read(priv->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
Marek Vasut988ee702013-12-14 05:55:28 +0100285 temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Marek Vasut6ce573f2019-06-09 03:50:52 +0200286 pcie_phy_write(priv->dbi_base, PHY_RX_OVRD_IN_LO, temp);
Marek Vasut988ee702013-12-14 05:55:28 +0100287
288 return 0;
289}
290
291/*
292 * iATU region setup
293 */
Marek Vasut120efeb2019-06-09 03:50:54 +0200294static int imx_pcie_regions_setup(struct imx_pcie_priv *priv)
Marek Vasut988ee702013-12-14 05:55:28 +0100295{
296 /*
297 * i.MX6 defines 16MB in the AXI address map for PCIe.
298 *
299 * That address space excepted the pcie registers is
300 * split and defined into different regions by iATU,
301 * with sizes and offsets as follows:
302 *
303 * 0x0100_0000 --- 0x010F_FFFF 1MB IORESOURCE_IO
304 * 0x0110_0000 --- 0x01EF_FFFF 14MB IORESOURCE_MEM
305 * 0x01F0_0000 --- 0x01FF_FFFF 1MB Cfg + Registers
306 */
307
308 /* CMD reg:I/O space, MEM space, and Bus Master Enable */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200309 setbits_le32(priv->dbi_base + PCI_COMMAND,
Marek Vasut988ee702013-12-14 05:55:28 +0100310 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
311
Pali Rohár25781e22022-02-18 13:18:40 +0100312 /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI_NORMAL */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200313 setbits_le32(priv->dbi_base + PCI_CLASS_REVISION,
Pali Rohár25781e22022-02-18 13:18:40 +0100314 PCI_CLASS_BRIDGE_PCI_NORMAL << 8);
Marek Vasut988ee702013-12-14 05:55:28 +0100315
316 /* Region #0 is used for Outbound CFG space access. */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200317 writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
Marek Vasut988ee702013-12-14 05:55:28 +0100318
Marek Vasut2c20b752019-06-09 03:50:53 +0200319 writel(lower_32_bits((uintptr_t)priv->cfg_base),
320 priv->dbi_base + PCIE_ATU_LOWER_BASE);
321 writel(upper_32_bits((uintptr_t)priv->cfg_base),
322 priv->dbi_base + PCIE_ATU_UPPER_BASE);
323 writel(lower_32_bits((uintptr_t)priv->cfg_base + MX6_ROOT_SIZE),
Marek Vasut6ce573f2019-06-09 03:50:52 +0200324 priv->dbi_base + PCIE_ATU_LIMIT);
Marek Vasut988ee702013-12-14 05:55:28 +0100325
Marek Vasut6ce573f2019-06-09 03:50:52 +0200326 writel(0, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
327 writel(0, priv->dbi_base + PCIE_ATU_UPPER_TARGET);
328 writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
329 writel(PCIE_ATU_ENABLE, priv->dbi_base + PCIE_ATU_CR2);
Marek Vasut988ee702013-12-14 05:55:28 +0100330
331 return 0;
332}
333
334/*
335 * PCI Express accessors
336 */
Marek Vasut120efeb2019-06-09 03:50:54 +0200337static void __iomem *get_bus_address(struct imx_pcie_priv *priv,
338 pci_dev_t d, int where)
Marek Vasut988ee702013-12-14 05:55:28 +0100339{
Marek Vasut2c20b752019-06-09 03:50:53 +0200340 void __iomem *va_address;
Marek Vasut988ee702013-12-14 05:55:28 +0100341
342 /* Reconfigure Region #0 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200343 writel(0, priv->dbi_base + PCIE_ATU_VIEWPORT);
Marek Vasut988ee702013-12-14 05:55:28 +0100344
345 if (PCI_BUS(d) < 2)
Marek Vasut6ce573f2019-06-09 03:50:52 +0200346 writel(PCIE_ATU_TYPE_CFG0, priv->dbi_base + PCIE_ATU_CR1);
Marek Vasut988ee702013-12-14 05:55:28 +0100347 else
Marek Vasut6ce573f2019-06-09 03:50:52 +0200348 writel(PCIE_ATU_TYPE_CFG1, priv->dbi_base + PCIE_ATU_CR1);
Marek Vasut988ee702013-12-14 05:55:28 +0100349
350 if (PCI_BUS(d) == 0) {
Marek Vasut2c20b752019-06-09 03:50:53 +0200351 va_address = priv->dbi_base;
Marek Vasut988ee702013-12-14 05:55:28 +0100352 } else {
Marek Vasut6ce573f2019-06-09 03:50:52 +0200353 writel(d << 8, priv->dbi_base + PCIE_ATU_LOWER_TARGET);
Marek Vasut2c20b752019-06-09 03:50:53 +0200354 va_address = priv->cfg_base;
Marek Vasut988ee702013-12-14 05:55:28 +0100355 }
356
357 va_address += (where & ~0x3);
358
359 return va_address;
360}
361
362static int imx_pcie_addr_valid(pci_dev_t d)
363{
364 if ((PCI_BUS(d) == 0) && (PCI_DEV(d) > 1))
365 return -EINVAL;
366 if ((PCI_BUS(d) == 1) && (PCI_DEV(d) > 0))
367 return -EINVAL;
368 return 0;
369}
370
371/*
372 * Replace the original ARM DABT handler with a simple jump-back one.
373 *
374 * The problem here is that if we have a PCIe bridge attached to this PCIe
375 * controller, but no PCIe device is connected to the bridges' downstream
376 * port, the attempt to read/write from/to the config space will produce
377 * a DABT. This is a behavior of the controller and can not be disabled
378 * unfortuatelly.
379 *
380 * To work around the problem, we backup the current DABT handler address
381 * and replace it with our own DABT handler, which only bounces right back
382 * into the code.
383 */
384static void imx_pcie_fix_dabt_handler(bool set)
385{
386 extern uint32_t *_data_abort;
387 uint32_t *data_abort_addr = (uint32_t *)&_data_abort;
388
389 static const uint32_t data_abort_bounce_handler = 0xe25ef004;
390 uint32_t data_abort_bounce_addr = (uint32_t)&data_abort_bounce_handler;
391
392 static uint32_t data_abort_backup;
393
394 if (set) {
395 data_abort_backup = *data_abort_addr;
396 *data_abort_addr = data_abort_bounce_addr;
397 } else {
398 *data_abort_addr = data_abort_backup;
399 }
400}
401
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200402static int imx_pcie_read_cfg(struct imx_pcie_priv *priv, pci_dev_t d,
403 int where, u32 *val)
Marek Vasut988ee702013-12-14 05:55:28 +0100404{
Marek Vasut2c20b752019-06-09 03:50:53 +0200405 void __iomem *va_address;
Marek Vasut988ee702013-12-14 05:55:28 +0100406 int ret;
407
408 ret = imx_pcie_addr_valid(d);
409 if (ret) {
410 *val = 0xffffffff;
Bin Meng122c7662016-01-08 01:03:20 -0800411 return 0;
Marek Vasut988ee702013-12-14 05:55:28 +0100412 }
413
Marek Vasut120efeb2019-06-09 03:50:54 +0200414 va_address = get_bus_address(priv, d, where);
Marek Vasut988ee702013-12-14 05:55:28 +0100415
416 /*
417 * Read the PCIe config space. We must replace the DABT handler
418 * here in case we got data abort from the PCIe controller, see
419 * imx_pcie_fix_dabt_handler() description. Note that writing the
420 * "val" with valid value is also imperative here as in case we
421 * did got DABT, the val would contain random value.
422 */
423 imx_pcie_fix_dabt_handler(true);
424 writel(0xffffffff, val);
425 *val = readl(va_address);
426 imx_pcie_fix_dabt_handler(false);
427
428 return 0;
429}
430
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200431static int imx_pcie_write_cfg(struct imx_pcie_priv *priv, pci_dev_t d,
432 int where, u32 val)
Marek Vasut988ee702013-12-14 05:55:28 +0100433{
Marek Vasut2c20b752019-06-09 03:50:53 +0200434 void __iomem *va_address = NULL;
Marek Vasut988ee702013-12-14 05:55:28 +0100435 int ret;
436
437 ret = imx_pcie_addr_valid(d);
438 if (ret)
439 return ret;
440
Marek Vasut120efeb2019-06-09 03:50:54 +0200441 va_address = get_bus_address(priv, d, where);
Marek Vasut988ee702013-12-14 05:55:28 +0100442
443 /*
444 * Write the PCIe config space. We must replace the DABT handler
445 * here in case we got data abort from the PCIe controller, see
446 * imx_pcie_fix_dabt_handler() description.
447 */
448 imx_pcie_fix_dabt_handler(true);
449 writel(val, va_address);
450 imx_pcie_fix_dabt_handler(false);
451
452 return 0;
453}
454
455/*
456 * Initial bus setup
457 */
Marek Vasut120efeb2019-06-09 03:50:54 +0200458static int imx6_pcie_assert_core_reset(struct imx_pcie_priv *priv,
459 bool prepare_for_boot)
Marek Vasut988ee702013-12-14 05:55:28 +0100460{
461 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
Fabio Estevamb5272bd2015-10-13 11:01:27 -0300462
463 if (is_mx6dqp())
464 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
465
Fabio Estevam211a4902014-08-25 14:26:45 -0300466#if defined(CONFIG_MX6SX)
467 struct gpc *gpc_regs = (struct gpc *)GPC_BASE_ADDR;
Marek Vasut988ee702013-12-14 05:55:28 +0100468
Fabio Estevam211a4902014-08-25 14:26:45 -0300469 /* SSP_EN is not used on MX6SX anymore */
470 setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
471 /* Force PCIe PHY reset */
472 setbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
473 /* Power up PCIe PHY */
474 setbits_le32(&gpc_regs->cntr, PCIE_PHY_PUP_REQ);
475#else
Tim Harveyc22f2ea2017-05-12 12:58:41 -0700476 /*
477 * If the bootloader already enabled the link we need some special
478 * handling to get the core back into a state where it is safe to
479 * touch it for configuration. As there is no dedicated reset signal
480 * wired up for MX6QDL, we need to manually force LTSSM into "detect"
481 * state before completely disabling LTSSM, which is a prerequisite
482 * for core configuration.
483 *
484 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
485 * indication that the bootloader activated the link.
486 */
Tim Harvey3b10a832021-04-16 13:30:41 -0700487 if ((is_mx6dq() || is_mx6sdl()) && prepare_for_boot) {
Tim Harveyc22f2ea2017-05-12 12:58:41 -0700488 u32 val, gpr1, gpr12;
489
490 gpr1 = readl(&iomuxc_regs->gpr[1]);
491 gpr12 = readl(&iomuxc_regs->gpr[12]);
492 if ((gpr1 & IOMUXC_GPR1_PCIE_REF_CLK_EN) &&
493 (gpr12 & IOMUXC_GPR12_PCIE_CTL_2)) {
Marek Vasut6ce573f2019-06-09 03:50:52 +0200494 val = readl(priv->dbi_base + PCIE_PL_PFLR);
Tim Harveyc22f2ea2017-05-12 12:58:41 -0700495 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
496 val |= PCIE_PL_PFLR_FORCE_LINK;
497
498 imx_pcie_fix_dabt_handler(true);
Marek Vasut6ce573f2019-06-09 03:50:52 +0200499 writel(val, priv->dbi_base + PCIE_PL_PFLR);
Tim Harveyc22f2ea2017-05-12 12:58:41 -0700500 imx_pcie_fix_dabt_handler(false);
501
502 gpr12 &= ~IOMUXC_GPR12_PCIE_CTL_2;
503 writel(val, &iomuxc_regs->gpr[12]);
504 }
505 }
Marek Vasut988ee702013-12-14 05:55:28 +0100506 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
507 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
Fabio Estevam211a4902014-08-25 14:26:45 -0300508#endif
Marek Vasut988ee702013-12-14 05:55:28 +0100509
510 return 0;
511}
512
513static int imx6_pcie_init_phy(void)
514{
515 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
516
517 clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
518
519 clrsetbits_le32(&iomuxc_regs->gpr[12],
520 IOMUXC_GPR12_DEVICE_TYPE_MASK,
521 IOMUXC_GPR12_DEVICE_TYPE_RC);
522 clrsetbits_le32(&iomuxc_regs->gpr[12],
523 IOMUXC_GPR12_LOS_LEVEL_MASK,
524 IOMUXC_GPR12_LOS_LEVEL_9);
525
Fabio Estevam211a4902014-08-25 14:26:45 -0300526#ifdef CONFIG_MX6SX
527 clrsetbits_le32(&iomuxc_regs->gpr[12],
528 IOMUXC_GPR12_RX_EQ_MASK,
529 IOMUXC_GPR12_RX_EQ_2);
530#endif
531
Marek Vasut988ee702013-12-14 05:55:28 +0100532 writel((0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN1_OFFSET) |
533 (0x0 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_3P5DB_OFFSET) |
534 (20 << IOMUXC_GPR8_PCS_TX_DEEMPH_GEN2_6DB_OFFSET) |
535 (127 << IOMUXC_GPR8_PCS_TX_SWING_FULL_OFFSET) |
536 (127 << IOMUXC_GPR8_PCS_TX_SWING_LOW_OFFSET),
537 &iomuxc_regs->gpr[8]);
538
539 return 0;
540}
541
Tim Harveybc6b7f12022-04-13 15:57:37 -0700542int imx6_pcie_toggle_power(struct udevice *vpcie)
Marek Vasut77779a42014-03-23 22:45:40 +0100543{
Tom Rinic43e27a2022-12-04 10:13:26 -0500544#ifdef CFG_PCIE_IMX_POWER_GPIO
545 gpio_request(CFG_PCIE_IMX_POWER_GPIO, "pcie_power");
546 gpio_direction_output(CFG_PCIE_IMX_POWER_GPIO, 0);
Marek Vasut77779a42014-03-23 22:45:40 +0100547 mdelay(20);
Tom Rinic43e27a2022-12-04 10:13:26 -0500548 gpio_set_value(CFG_PCIE_IMX_POWER_GPIO, 1);
Marek Vasut77779a42014-03-23 22:45:40 +0100549 mdelay(20);
Tom Rinic43e27a2022-12-04 10:13:26 -0500550 gpio_free(CFG_PCIE_IMX_POWER_GPIO);
Marek Vasut77779a42014-03-23 22:45:40 +0100551#endif
Tim Harvey541e2732022-04-13 15:54:37 -0700552
553#if CONFIG_IS_ENABLED(DM_REGULATOR)
554 if (vpcie) {
555 regulator_set_enable(vpcie, false);
556 mdelay(20);
557 regulator_set_enable(vpcie, true);
558 mdelay(20);
559 }
560#endif
Marek Vasut77779a42014-03-23 22:45:40 +0100561 return 0;
562}
563
Tim Harveybc6b7f12022-04-13 15:57:37 -0700564int imx6_pcie_toggle_reset(struct gpio_desc *gpio, bool active_high)
Marek Vasutd9056c22014-02-03 21:46:22 +0100565{
566 /*
567 * See 'PCI EXPRESS BASE SPECIFICATION, REV 3.0, SECTION 6.6.1'
568 * for detailed understanding of the PCIe CR reset logic.
569 *
570 * The PCIe #PERST reset line _MUST_ be connected, otherwise your
571 * design does not conform to the specification. You must wait at
Fabio Estevam1d0b7e32015-09-10 20:45:25 -0300572 * least 20 ms after de-asserting the #PERST so the EP device can
Marek Vasutd9056c22014-02-03 21:46:22 +0100573 * do self-initialisation.
574 *
575 * In case your #PERST pin is connected to a plain GPIO pin of the
Tom Rini9fa71af2022-12-04 10:13:25 -0500576 * CPU, you can define CFG_PCIE_IMX_PERST_GPIO in your board's
Marek Vasutd9056c22014-02-03 21:46:22 +0100577 * configuration file and the condition below will handle the rest
578 * of the reset toggling.
579 *
Marek Vasutd9056c22014-02-03 21:46:22 +0100580 * In case your #PERST line of the PCIe EP device is not connected
581 * at all, your design is broken and you should fix your design,
582 * otherwise you will observe problems like for example the link
583 * not coming up after rebooting the system back from running Linux
584 * that uses the PCIe as well OR the PCIe link might not come up in
585 * Linux at all in the first place since it's in some non-reset
586 * state due to being previously used in U-Boot.
587 */
Tom Rini9fa71af2022-12-04 10:13:25 -0500588#ifdef CFG_PCIE_IMX_PERST_GPIO
589 gpio_request(CFG_PCIE_IMX_PERST_GPIO, "pcie_reset");
590 gpio_direction_output(CFG_PCIE_IMX_PERST_GPIO, 0);
Marek Vasutd9056c22014-02-03 21:46:22 +0100591 mdelay(20);
Tom Rini9fa71af2022-12-04 10:13:25 -0500592 gpio_set_value(CFG_PCIE_IMX_PERST_GPIO, 1);
Marek Vasutd9056c22014-02-03 21:46:22 +0100593 mdelay(20);
Tom Rini9fa71af2022-12-04 10:13:25 -0500594 gpio_free(CFG_PCIE_IMX_PERST_GPIO);
Marek Vasutd9056c22014-02-03 21:46:22 +0100595#else
Tim Harvey7367b392021-07-06 10:19:09 -0700596 if (dm_gpio_is_valid(gpio)) {
597 /* Assert PERST# for 20ms then de-assert */
598 dm_gpio_set_value(gpio, active_high ? 0 : 1);
599 mdelay(20);
600 dm_gpio_set_value(gpio, active_high ? 1 : 0);
601 mdelay(20);
602 } else {
603 puts("WARNING: Make sure the PCIe #PERST line is connected!\n");
604 }
Marek Vasutd9056c22014-02-03 21:46:22 +0100605#endif
606 return 0;
607}
608
Tim Harvey7367b392021-07-06 10:19:09 -0700609static int imx6_pcie_deassert_core_reset(struct imx_pcie_priv *priv)
Marek Vasut988ee702013-12-14 05:55:28 +0100610{
611 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
612
Tim Harvey541e2732022-04-13 15:54:37 -0700613 imx6_pcie_toggle_power(priv->vpcie);
Marek Vasut988ee702013-12-14 05:55:28 +0100614
Marek Vasut988ee702013-12-14 05:55:28 +0100615 enable_pcie_clock();
616
Fabio Estevamb5272bd2015-10-13 11:01:27 -0300617 if (is_mx6dqp())
618 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_PCIE_SW_RST);
619
Marek Vasut988ee702013-12-14 05:55:28 +0100620 /*
621 * Wait for the clock to settle a bit, when the clock are sourced
Fabio Estevam1d0b7e32015-09-10 20:45:25 -0300622 * from the CPU, we need about 30 ms to settle.
Marek Vasut988ee702013-12-14 05:55:28 +0100623 */
Marek Vasutd9056c22014-02-03 21:46:22 +0100624 mdelay(50);
Marek Vasut988ee702013-12-14 05:55:28 +0100625
Fabio Estevam211a4902014-08-25 14:26:45 -0300626#if defined(CONFIG_MX6SX)
627 /* SSP_EN is not used on MX6SX anymore */
628 clrbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_TEST_POWERDOWN);
629 /* Clear PCIe PHY reset bit */
630 clrbits_le32(&iomuxc_regs->gpr[5], IOMUXC_GPR5_PCIE_BTNRST);
631#else
Tim Harveyd53204a2014-08-07 22:57:29 -0700632 /* Enable PCIe */
633 clrbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_TEST_POWERDOWN);
634 setbits_le32(&iomuxc_regs->gpr[1], IOMUXC_GPR1_REF_SSP_EN);
Fabio Estevam211a4902014-08-25 14:26:45 -0300635#endif
Tim Harveyd53204a2014-08-07 22:57:29 -0700636
Tim Harvey7367b392021-07-06 10:19:09 -0700637 imx6_pcie_toggle_reset(&priv->reset_gpio, priv->reset_active_high);
Marek Vasut988ee702013-12-14 05:55:28 +0100638
639 return 0;
640}
641
Marek Vasut120efeb2019-06-09 03:50:54 +0200642static int imx_pcie_link_up(struct imx_pcie_priv *priv)
Marek Vasut988ee702013-12-14 05:55:28 +0100643{
644 struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
645 uint32_t tmp;
646 int count = 0;
647
Marek Vasut120efeb2019-06-09 03:50:54 +0200648 imx6_pcie_assert_core_reset(priv, false);
Marek Vasut988ee702013-12-14 05:55:28 +0100649 imx6_pcie_init_phy();
Tim Harvey7367b392021-07-06 10:19:09 -0700650 imx6_pcie_deassert_core_reset(priv);
Marek Vasut988ee702013-12-14 05:55:28 +0100651
Marek Vasut120efeb2019-06-09 03:50:54 +0200652 imx_pcie_regions_setup(priv);
Marek Vasut988ee702013-12-14 05:55:28 +0100653
654 /*
Koen Vandeputte6be35052018-01-04 14:54:34 +0100655 * By default, the subordinate is set equally to the secondary
656 * bus (0x01) when the RC boots.
657 * This means that theoretically, only bus 1 is reachable from the RC.
658 * Force the PCIe RC subordinate to 0xff, otherwise no downstream
659 * devices will be detected if the enumeration is applied strictly.
660 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200661 tmp = readl(priv->dbi_base + 0x18);
Koen Vandeputte6be35052018-01-04 14:54:34 +0100662 tmp |= (0xff << 16);
Marek Vasut6ce573f2019-06-09 03:50:52 +0200663 writel(tmp, priv->dbi_base + 0x18);
Koen Vandeputte6be35052018-01-04 14:54:34 +0100664
665 /*
Marek Vasut988ee702013-12-14 05:55:28 +0100666 * FIXME: Force the PCIe RC to Gen1 operation
667 * The RC must be forced into Gen1 mode before bringing the link
668 * up, otherwise no downstream devices are detected. After the
669 * link is up, a managed Gen1->Gen2 transition can be initiated.
670 */
Marek Vasut6ce573f2019-06-09 03:50:52 +0200671 tmp = readl(priv->dbi_base + 0x7c);
Marek Vasut988ee702013-12-14 05:55:28 +0100672 tmp &= ~0xf;
673 tmp |= 0x1;
Marek Vasut6ce573f2019-06-09 03:50:52 +0200674 writel(tmp, priv->dbi_base + 0x7c);
Marek Vasut988ee702013-12-14 05:55:28 +0100675
676 /* LTSSM enable, starting link. */
677 setbits_le32(&iomuxc_regs->gpr[12], IOMUXC_GPR12_APPS_LTSSM_ENABLE);
678
Marek Vasut120efeb2019-06-09 03:50:54 +0200679 while (!imx6_pcie_link_up(priv)) {
Marek Vasut988ee702013-12-14 05:55:28 +0100680 udelay(10);
681 count++;
Stefano Babicd1457922016-06-06 11:14:19 +0200682 if (count >= 4000) {
Tim Harveyc3260002015-05-08 15:17:10 -0700683#ifdef CONFIG_PCI_SCAN_SHOW
684 puts("PCI: pcie phy link never came up\n");
685#endif
Marek Vasut988ee702013-12-14 05:55:28 +0100686 debug("DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
Marek Vasut6ce573f2019-06-09 03:50:52 +0200687 readl(priv->dbi_base + PCIE_PHY_DEBUG_R0),
688 readl(priv->dbi_base + PCIE_PHY_DEBUG_R1));
Marek Vasut988ee702013-12-14 05:55:28 +0100689 return -EINVAL;
690 }
691 }
692
693 return 0;
694}
695
Simon Glass2a311e82020-01-27 08:49:37 -0700696static int imx_pcie_dm_read_config(const struct udevice *dev, pci_dev_t bdf,
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200697 uint offset, ulong *value,
698 enum pci_size_t size)
699{
700 struct imx_pcie_priv *priv = dev_get_priv(dev);
701 u32 tmpval;
702 int ret;
703
704 ret = imx_pcie_read_cfg(priv, bdf, offset, &tmpval);
705 if (ret)
706 return ret;
707
708 *value = pci_conv_32_to_size(tmpval, offset, size);
709 return 0;
710}
711
712static int imx_pcie_dm_write_config(struct udevice *dev, pci_dev_t bdf,
713 uint offset, ulong value,
714 enum pci_size_t size)
715{
716 struct imx_pcie_priv *priv = dev_get_priv(dev);
717 u32 tmpval, newval;
718 int ret;
719
720 ret = imx_pcie_read_cfg(priv, bdf, offset, &tmpval);
721 if (ret)
722 return ret;
723
724 newval = pci_conv_size_to_32(tmpval, value, offset, size);
725 return imx_pcie_write_cfg(priv, bdf, offset, newval);
726}
727
728static int imx_pcie_dm_probe(struct udevice *dev)
729{
730 struct imx_pcie_priv *priv = dev_get_priv(dev);
731
Tim Harvey541e2732022-04-13 15:54:37 -0700732#if CONFIG_IS_ENABLED(DM_REGULATOR)
733 device_get_supply_regulator(dev, "vpcie-supply", &priv->vpcie);
734#endif
735
Tim Harvey7367b392021-07-06 10:19:09 -0700736 /* if PERST# valid from dt then assert it */
737 gpio_request_by_name(dev, "reset-gpio", 0, &priv->reset_gpio,
738 GPIOD_IS_OUT);
739 priv->reset_active_high = dev_read_bool(dev, "reset-gpio-active-high");
740 if (dm_gpio_is_valid(&priv->reset_gpio)) {
741 dm_gpio_set_value(&priv->reset_gpio,
742 priv->reset_active_high ? 0 : 1);
743 }
744
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200745 return imx_pcie_link_up(priv);
746}
747
748static int imx_pcie_dm_remove(struct udevice *dev)
749{
750 struct imx_pcie_priv *priv = dev_get_priv(dev);
751
752 imx6_pcie_assert_core_reset(priv, true);
753
754 return 0;
755}
756
Simon Glassaad29ae2020-12-03 16:55:21 -0700757static int imx_pcie_of_to_plat(struct udevice *dev)
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200758{
759 struct imx_pcie_priv *priv = dev_get_priv(dev);
760
Johan Jonkerb52189e2023-03-13 01:32:31 +0100761 priv->dbi_base = devfdt_get_addr_index_ptr(dev, 0);
762 priv->cfg_base = devfdt_get_addr_index_ptr(dev, 1);
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200763 if (!priv->dbi_base || !priv->cfg_base)
764 return -EINVAL;
765
766 return 0;
767}
768
769static const struct dm_pci_ops imx_pcie_ops = {
770 .read_config = imx_pcie_dm_read_config,
771 .write_config = imx_pcie_dm_write_config,
772};
773
774static const struct udevice_id imx_pcie_ids[] = {
775 { .compatible = "fsl,imx6q-pcie" },
Marek Vasutbec7be12019-11-26 09:33:29 +0100776 { .compatible = "fsl,imx6sx-pcie" },
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200777 { }
778};
779
780U_BOOT_DRIVER(imx_pcie) = {
781 .name = "imx_pcie",
782 .id = UCLASS_PCI,
783 .of_match = imx_pcie_ids,
784 .ops = &imx_pcie_ops,
785 .probe = imx_pcie_dm_probe,
786 .remove = imx_pcie_dm_remove,
Simon Glassaad29ae2020-12-03 16:55:21 -0700787 .of_to_plat = imx_pcie_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700788 .priv_auto = sizeof(struct imx_pcie_priv),
Marek Vasut3c9f33e2019-06-09 03:50:55 +0200789 .flags = DM_FLAG_OS_PREPARE,
790};