blob: 77f1a1b48f1e322ebfc17c097b80f3248bc403ad [file] [log] [blame]
Shawn Linc0649da2021-01-15 18:01:22 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Rockchip DesignWare based PCIe host controller driver
4 *
5 * Copyright (c) 2021 Rockchip, Inc.
6 */
7
8#include <common.h>
9#include <clk.h>
10#include <dm.h>
11#include <generic-phy.h>
12#include <pci.h>
13#include <power-domain.h>
14#include <reset.h>
15#include <syscon.h>
16#include <asm/arch-rockchip/clock.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Shawn Linc0649da2021-01-15 18:01:22 +080018#include <asm/io.h>
19#include <asm-generic/gpio.h>
20#include <dm/device_compat.h>
21#include <linux/iopoll.h>
22#include <linux/delay.h>
23#include <power/regulator.h>
24
25DECLARE_GLOBAL_DATA_PTR;
26
27/**
28 * struct rk_pcie - RK DW PCIe controller state
29 *
30 * @vpcie3v3: The 3.3v power supply for slot
31 * @dbi_base: The base address of dwc core regs
32 * @apb_base: The base address of vendor regs
33 * @cfg_base: The base address of config header space
34 * @cfg_size: The size of the configuration space which is needed
35 * as it gets written into the PCIE_ATU_LIMIT register
36 * @first_busno: This driver supports multiple PCIe controllers.
37 * first_busno stores the bus number of the PCIe root-port
38 * number which may vary depending on the PCIe setup
39 * (PEX switches etc).
40 * @rst_gpio: The #PERST signal for slot
41 * @io: The IO space for EP's BAR
42 * @mem: The memory space for EP's BAR
43 */
44struct rk_pcie {
45 struct udevice *dev;
46 struct udevice *vpcie3v3;
47 void *dbi_base;
48 void *apb_base;
49 void *cfg_base;
50 fdt_size_t cfg_size;
51 struct phy phy;
52 struct clk_bulk clks;
53 int first_busno;
54 struct reset_ctl_bulk rsts;
55 struct gpio_desc rst_gpio;
56 struct pci_region io;
57 struct pci_region mem;
58};
59
60/* Parameters for the waiting for iATU enabled routine */
61#define PCIE_CLIENT_GENERAL_DEBUG 0x104
62#define PCIE_CLIENT_HOT_RESET_CTRL 0x180
63#define PCIE_LTSSM_ENABLE_ENHANCE BIT(4)
64#define PCIE_CLIENT_LTSSM_STATUS 0x300
65#define SMLH_LINKUP BIT(16)
66#define RDLH_LINKUP BIT(17)
67#define PCIE_CLIENT_DBG_FIFO_MODE_CON 0x310
68#define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0 0x320
69#define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1 0x324
70#define PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0 0x328
71#define PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1 0x32c
72#define PCIE_CLIENT_DBG_FIFO_STATUS 0x350
73#define PCIE_CLIENT_DBG_TRANSITION_DATA 0xffff0000
74#define PCIE_CLIENT_DBF_EN 0xffff0003
75
76/* PCI DBICS registers */
77#define PCIE_LINK_STATUS_REG 0x80
78#define PCIE_LINK_STATUS_SPEED_OFF 16
79#define PCIE_LINK_STATUS_SPEED_MASK (0xf << PCIE_LINK_STATUS_SPEED_OFF)
80#define PCIE_LINK_STATUS_WIDTH_OFF 20
81#define PCIE_LINK_STATUS_WIDTH_MASK (0xf << PCIE_LINK_STATUS_WIDTH_OFF)
82
83#define PCIE_LINK_CAPABILITY 0x7c
84#define PCIE_LINK_CTL_2 0xa0
85#define TARGET_LINK_SPEED_MASK 0xf
86#define LINK_SPEED_GEN_1 0x1
87#define LINK_SPEED_GEN_2 0x2
88#define LINK_SPEED_GEN_3 0x3
89
90#define PCIE_MISC_CONTROL_1_OFF 0x8bc
91#define PCIE_DBI_RO_WR_EN BIT(0)
92
93#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80c
94#define PORT_LOGIC_SPEED_CHANGE BIT(17)
95
96/*
97 * iATU Unroll-specific register definitions
98 * From 4.80 core version the address translation will be made by unroll.
99 * The registers are offset from atu_base
100 */
101#define PCIE_ATU_UNR_REGION_CTRL1 0x00
102#define PCIE_ATU_UNR_REGION_CTRL2 0x04
103#define PCIE_ATU_UNR_LOWER_BASE 0x08
104#define PCIE_ATU_UNR_UPPER_BASE 0x0c
105#define PCIE_ATU_UNR_LIMIT 0x10
106#define PCIE_ATU_UNR_LOWER_TARGET 0x14
107#define PCIE_ATU_UNR_UPPER_TARGET 0x18
108
109#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
110#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
111#define PCIE_ATU_TYPE_MEM (0x0 << 0)
112#define PCIE_ATU_TYPE_IO (0x2 << 0)
113#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
114#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
115#define PCIE_ATU_ENABLE (0x1 << 31)
116#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
117#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
118#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
119#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
120
121/* Register address builder */
122#define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region) \
123 ((0x3 << 20) | ((region) << 9))
124
125/* Parameters for the waiting for iATU enabled routine */
126#define LINK_WAIT_MAX_IATU_RETRIES 5
127#define LINK_WAIT_IATU_US 10000
128
129/* Parameters for the waiting for #perst signal */
130#define PERST_WAIT_MS 1000
131
132static int rk_pcie_read(void __iomem *addr, int size, u32 *val)
133{
134 if ((uintptr_t)addr & (size - 1)) {
135 *val = 0;
136 return PCIBIOS_UNSUPPORTED;
137 }
138
139 if (size == 4) {
140 *val = readl(addr);
141 } else if (size == 2) {
142 *val = readw(addr);
143 } else if (size == 1) {
144 *val = readb(addr);
145 } else {
146 *val = 0;
147 return -ENODEV;
148 }
149
150 return 0;
151}
152
153static int rk_pcie_write(void __iomem *addr, int size, u32 val)
154{
155 if ((uintptr_t)addr & (size - 1))
156 return PCIBIOS_UNSUPPORTED;
157
158 if (size == 4)
159 writel(val, addr);
160 else if (size == 2)
161 writew(val, addr);
162 else if (size == 1)
163 writeb(val, addr);
164 else
165 return -ENODEV;
166
167 return 0;
168}
169
170static u32 __rk_pcie_read_apb(struct rk_pcie *rk_pcie, void __iomem *base,
171 u32 reg, size_t size)
172{
173 int ret;
174 u32 val;
175
176 ret = rk_pcie_read(base + reg, size, &val);
177 if (ret)
178 dev_err(rk_pcie->dev, "Read APB address failed\n");
179
180 return val;
181}
182
183static void __rk_pcie_write_apb(struct rk_pcie *rk_pcie, void __iomem *base,
184 u32 reg, size_t size, u32 val)
185{
186 int ret;
187
188 ret = rk_pcie_write(base + reg, size, val);
189 if (ret)
190 dev_err(rk_pcie->dev, "Write APB address failed\n");
191}
192
193/**
194 * rk_pcie_readl_apb() - Read vendor regs
195 *
196 * @rk_pcie: Pointer to the PCI controller state
197 * @reg: Offset of regs
198 */
199static inline u32 rk_pcie_readl_apb(struct rk_pcie *rk_pcie, u32 reg)
200{
201 return __rk_pcie_read_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4);
202}
203
204/**
205 * rk_pcie_writel_apb() - Write vendor regs
206 *
207 * @rk_pcie: Pointer to the PCI controller state
208 * @reg: Offset of regs
209 * @val: Value to be writen
210 */
211static inline void rk_pcie_writel_apb(struct rk_pcie *rk_pcie, u32 reg,
212 u32 val)
213{
214 __rk_pcie_write_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4, val);
215}
216
217static int rk_pcie_get_link_speed(struct rk_pcie *rk_pcie)
218{
219 return (readl(rk_pcie->dbi_base + PCIE_LINK_STATUS_REG) &
220 PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
221}
222
223static int rk_pcie_get_link_width(struct rk_pcie *rk_pcie)
224{
225 return (readl(rk_pcie->dbi_base + PCIE_LINK_STATUS_REG) &
226 PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
227}
228
229static void rk_pcie_writel_ob_unroll(struct rk_pcie *rk_pcie, u32 index,
230 u32 reg, u32 val)
231{
232 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
233 void __iomem *base = rk_pcie->dbi_base;
234
235 writel(val, base + offset + reg);
236}
237
238static u32 rk_pcie_readl_ob_unroll(struct rk_pcie *rk_pcie, u32 index, u32 reg)
239{
240 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
241 void __iomem *base = rk_pcie->dbi_base;
242
243 return readl(base + offset + reg);
244}
245
246static inline void rk_pcie_dbi_write_enable(struct rk_pcie *rk_pcie, bool en)
247{
248 u32 val;
249
250 val = readl(rk_pcie->dbi_base + PCIE_MISC_CONTROL_1_OFF);
251
252 if (en)
253 val |= PCIE_DBI_RO_WR_EN;
254 else
255 val &= ~PCIE_DBI_RO_WR_EN;
256 writel(val, rk_pcie->dbi_base + PCIE_MISC_CONTROL_1_OFF);
257}
258
259/**
260 * rockchip_pcie_setup_host() - Setup the PCIe controller for RC opertaion
261 *
262 * @rk_pcie: Pointer to the PCI controller state
263 *
264 * Configure the host BARs of the PCIe controller root port so that
265 * PCI(e) devices may access the system memory.
266 */
267static void rk_pcie_setup_host(struct rk_pcie *rk_pcie)
268{
269 u32 val;
270
271 rk_pcie_dbi_write_enable(rk_pcie, true);
272
273 /* setup RC BARs */
274 writel(PCI_BASE_ADDRESS_MEM_TYPE_64,
275 rk_pcie->dbi_base + PCI_BASE_ADDRESS_0);
276 writel(0x0, rk_pcie->dbi_base + PCI_BASE_ADDRESS_1);
277
278 /* setup interrupt pins */
279 clrsetbits_le32(rk_pcie->dbi_base + PCI_INTERRUPT_LINE,
280 0xff00, 0x100);
281
282 /* setup bus numbers */
283 clrsetbits_le32(rk_pcie->dbi_base + PCI_PRIMARY_BUS,
284 0xffffff, 0x00ff0100);
285
286 /* setup command register */
287 clrsetbits_le32(rk_pcie->dbi_base + PCI_PRIMARY_BUS,
288 0xffff,
289 PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
290 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
291
292 /* program correct class for RC */
293 writew(PCI_CLASS_BRIDGE_PCI, rk_pcie->dbi_base + PCI_CLASS_DEVICE);
294 /* Better disable write permission right after the update */
295
296 setbits_le32(rk_pcie->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL,
297 PORT_LOGIC_SPEED_CHANGE)
298
299 rk_pcie_dbi_write_enable(rk_pcie, false);
300}
301
302/**
303 * rk_pcie_configure() - Configure link capabilities and speed
304 *
305 * @rk_pcie: Pointer to the PCI controller state
306 * @cap_speed: The capabilities and speed to configure
307 *
308 * Configure the link capabilities and speed in the PCIe root complex.
309 */
310static void rk_pcie_configure(struct rk_pcie *pci, u32 cap_speed)
311{
312 u32 val;
313
314 rk_pcie_dbi_write_enable(pci, true);
315
316 clrsetbits_le32(pci->dbi_base + PCIE_LINK_CAPABILITY,
317 TARGET_LINK_SPEED_MASK, cap_speed);
318
319 clrsetbits_le32(pci->dbi_base + PCIE_LINK_CTL_2,
320 TARGET_LINK_SPEED_MASK, cap_speed);
321
322 rk_pcie_dbi_write_enable(pci, false);
323}
324
325/**
326 * rk_pcie_dw_prog_outbound_atu_unroll() - Configure ATU for outbound accesses
327 *
328 * @rk_pcie: Pointer to the PCI controller state
329 * @index: ATU region index
330 * @type: ATU accsess type
331 * @cpu_addr: the physical address for the translation entry
332 * @pci_addr: the pcie bus address for the translation entry
333 * @size: the size of the translation entry
334 *
335 * Return: 0 is successful and -1 is failure
336 */
337static int rk_pcie_prog_outbound_atu_unroll(struct rk_pcie *pci, int index,
338 int type, u64 cpu_addr,
339 u64 pci_addr, u32 size)
340{
341 u32 retries, val;
342
343 dev_dbg(pci->dev, "ATU programmed with: index: %d, type: %d, cpu addr: %8llx, pci addr: %8llx, size: %8x\n",
344 index, type, cpu_addr, pci_addr, size);
345
346 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
347 lower_32_bits(cpu_addr));
348 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
349 upper_32_bits(cpu_addr));
350 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LIMIT,
351 lower_32_bits(cpu_addr + size - 1));
352 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
353 lower_32_bits(pci_addr));
354 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
355 upper_32_bits(pci_addr));
356 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1,
357 type);
358 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
359 PCIE_ATU_ENABLE);
360
361 /*
362 * Make sure ATU enable takes effect before any subsequent config
363 * and I/O accesses.
364 */
365 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
366 val = rk_pcie_readl_ob_unroll(pci, index,
367 PCIE_ATU_UNR_REGION_CTRL2);
368 if (val & PCIE_ATU_ENABLE)
369 return 0;
370
371 udelay(LINK_WAIT_IATU_US);
372 }
373 dev_err(pci->dev, "outbound iATU is not being enabled\n");
374
375 return -1;
376}
377
378/**
379 * rk_pcie_dw_addr_valid() - Check for valid bus address
380 *
381 * @d: The PCI device to access
382 * @first_busno: Bus number of the PCIe controller root complex
383 *
384 * Return 1 (true) if the PCI device can be accessed by this controller.
385 *
386 * Return: 1 on valid, 0 on invalid
387 */
388static int rk_pcie_addr_valid(pci_dev_t d, int first_busno)
389{
390 if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
391 return 0;
392 if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
393 return 0;
394
395 return 1;
396}
397
398/**
399 * set_cfg_address() - Configure the PCIe controller config space access
400 *
401 * @rk_pcie: Pointer to the PCI controller state
402 * @d: PCI device to access
403 * @where: Offset in the configuration space
404 *
405 * Configures the PCIe controller to access the configuration space of
406 * a specific PCIe device and returns the address to use for this
407 * access.
408 *
409 * Return: Address that can be used to access the configation space
410 * of the requested device / offset
411 */
412static uintptr_t set_cfg_address(struct rk_pcie *pcie,
413 pci_dev_t d, uint where)
414{
415 int rel_bus = PCI_BUS(d) - pcie->first_busno;
416 uintptr_t va_address;
417 u32 atu_type;
418 int ret;
419
420 /* Use dbi_base for own configuration read and write */
421 if (!rel_bus) {
422 va_address = (uintptr_t)pcie->dbi_base;
423 goto out;
424 }
425
426 if (rel_bus == 1)
427 /*
428 * For local bus whose primary bus number is root bridge,
429 * change TLP Type field to 4.
430 */
431 atu_type = PCIE_ATU_TYPE_CFG0;
432 else
433 /* Otherwise, change TLP Type field to 5. */
434 atu_type = PCIE_ATU_TYPE_CFG1;
435
436 /*
437 * Not accessing root port configuration space?
438 * Region #0 is used for Outbound CFG space access.
439 * Direction = Outbound
440 * Region Index = 0
441 */
442 d = PCI_MASK_BUS(d);
443 d = PCI_ADD_BUS(rel_bus, d);
444 ret = rk_pcie_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
445 atu_type, (u64)pcie->cfg_base,
446 d << 8, pcie->cfg_size);
447 if (ret)
448 return (uintptr_t)ret;
449
450 va_address = (uintptr_t)pcie->cfg_base;
451
452out:
453 va_address += where & ~0x3;
454
455 return va_address;
456}
457
458/**
459 * rockchip_pcie_rd_conf() - Read from configuration space
460 *
461 * @bus: Pointer to the PCI bus
462 * @bdf: Identifies the PCIe device to access
463 * @offset: The offset into the device's configuration space
464 * @valuep: A pointer at which to store the read value
465 * @size: Indicates the size of access to perform
466 *
467 * Read a value of size @size from offset @offset within the configuration
468 * space of the device identified by the bus, device & function numbers in @bdf
469 * on the PCI bus @bus.
470 *
471 * Return: 0 on success
472 */
473static int rockchip_pcie_rd_conf(const struct udevice *bus, pci_dev_t bdf,
474 uint offset, ulong *valuep,
475 enum pci_size_t size)
476{
477 struct rk_pcie *pcie = dev_get_priv(bus);
478 uintptr_t va_address;
479 ulong value;
480
481 debug("PCIE CFG read: bdf=%2x:%2x:%2x\n",
482 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
483
484 if (!rk_pcie_addr_valid(bdf, pcie->first_busno)) {
485 debug("- out of range\n");
486 *valuep = pci_get_ff(size);
487 return 0;
488 }
489
490 va_address = set_cfg_address(pcie, bdf, offset);
491
492 value = readl(va_address);
493
494 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
495 *valuep = pci_conv_32_to_size(value, offset, size);
496
497 return rk_pcie_prog_outbound_atu_unroll(pcie,
498 PCIE_ATU_REGION_INDEX1,
499 PCIE_ATU_TYPE_IO,
500 pcie->io.phys_start,
501 pcie->io.bus_start,
502 pcie->io.size);
503}
504
505/**
506 * rockchip_pcie_wr_conf() - Write to configuration space
507 *
508 * @bus: Pointer to the PCI bus
509 * @bdf: Identifies the PCIe device to access
510 * @offset: The offset into the device's configuration space
511 * @value: The value to write
512 * @size: Indicates the size of access to perform
513 *
514 * Write the value @value of size @size from offset @offset within the
515 * configuration space of the device identified by the bus, device & function
516 * numbers in @bdf on the PCI bus @bus.
517 *
518 * Return: 0 on success
519 */
520static int rockchip_pcie_wr_conf(struct udevice *bus, pci_dev_t bdf,
521 uint offset, ulong value,
522 enum pci_size_t size)
523{
524 struct rk_pcie *pcie = dev_get_priv(bus);
525 uintptr_t va_address;
526 ulong old;
527
528 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d)\n",
529 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
530 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
531
532 if (!rk_pcie_addr_valid(bdf, pcie->first_busno)) {
533 debug("- out of range\n");
534 return 0;
535 }
536
537 va_address = set_cfg_address(pcie, bdf, offset);
538
539 old = readl(va_address);
540 value = pci_conv_size_to_32(old, value, offset, size);
541 writel(value, va_address);
542
543 return rk_pcie_prog_outbound_atu_unroll(pcie,
544 PCIE_ATU_REGION_INDEX1,
545 PCIE_ATU_TYPE_IO,
546 pcie->io.phys_start,
547 pcie->io.bus_start,
548 pcie->io.size);
549
550}
551
552static void rk_pcie_enable_debug(struct rk_pcie *rk_pcie)
553{
554 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0,
555 PCIE_CLIENT_DBG_TRANSITION_DATA);
556 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1,
557 PCIE_CLIENT_DBG_TRANSITION_DATA);
558 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0,
559 PCIE_CLIENT_DBG_TRANSITION_DATA);
560 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1,
561 PCIE_CLIENT_DBG_TRANSITION_DATA);
562 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_MODE_CON,
563 PCIE_CLIENT_DBF_EN);
564}
565
566static void rk_pcie_debug_dump(struct rk_pcie *rk_pcie)
567{
568 u32 loop;
569
570 debug("ltssm = 0x%x\n",
571 rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS));
572 for (loop = 0; loop < 64; loop++)
573 debug("fifo_status = 0x%x\n",
574 rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_STATUS));
575}
576
577static inline void rk_pcie_link_status_clear(struct rk_pcie *rk_pcie)
578{
579 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_GENERAL_DEBUG, 0x0);
580}
581
582static inline void rk_pcie_disable_ltssm(struct rk_pcie *rk_pcie)
583{
584 rk_pcie_writel_apb(rk_pcie, 0x0, 0xc0008);
585}
586
587static inline void rk_pcie_enable_ltssm(struct rk_pcie *rk_pcie)
588{
589 rk_pcie_writel_apb(rk_pcie, 0x0, 0xc000c);
590}
591
592static int is_link_up(struct rk_pcie *priv)
593{
594 u32 val;
595
596 val = rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS);
597 if ((val & (RDLH_LINKUP | SMLH_LINKUP)) == 0x30000 &&
598 (val & GENMASK(5, 0)) == 0x11)
599 return 1;
600
601 return 0;
602}
603
604/**
605 * rk_pcie_link_up() - Wait for the link to come up
606 *
607 * @rk_pcie: Pointer to the PCI controller state
608 * @cap_speed: Desired link speed
609 *
610 * Return: 1 (true) for active line and negetive (false) for no link (timeout)
611 */
612static int rk_pcie_link_up(struct rk_pcie *priv, u32 cap_speed)
613{
614 int retries;
615
616 if (is_link_up(priv)) {
617 printf("PCI Link already up before configuration!\n");
618 return 1;
619 }
620
621 /* DW pre link configurations */
622 rk_pcie_configure(priv, cap_speed);
623
624 /* Rest the device */
625 if (dm_gpio_is_valid(&priv->rst_gpio)) {
626 dm_gpio_set_value(&priv->rst_gpio, 0);
627 /*
628 * Minimal is 100ms from spec but we see
629 * some wired devices need much more, such as 600ms.
630 * Add a enough delay to cover all cases.
631 */
632 msleep(PERST_WAIT_MS);
633 dm_gpio_set_value(&priv->rst_gpio, 1);
634 }
635
636 rk_pcie_disable_ltssm(priv);
637 rk_pcie_link_status_clear(priv);
638 rk_pcie_enable_debug(priv);
639
640 /* Enable LTSSM */
641 rk_pcie_enable_ltssm(priv);
642
643 for (retries = 0; retries < 5; retries++) {
644 if (is_link_up(priv)) {
645 dev_info(priv->dev, "PCIe Link up, LTSSM is 0x%x\n",
646 rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS));
647 rk_pcie_debug_dump(priv);
648 return 0;
649 }
650
651 dev_info(priv->dev, "PCIe Linking... LTSSM is 0x%x\n",
652 rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS));
653 rk_pcie_debug_dump(priv);
654 msleep(1000);
655 }
656
657 dev_err(priv->dev, "PCIe-%d Link Fail\n", dev_seq(priv->dev));
658 /* Link maybe in Gen switch recovery but we need to wait more 1s */
659 msleep(1000);
660 return -EIO;
661}
662
663static int rockchip_pcie_init_port(struct udevice *dev)
664{
665 int ret;
666 u32 val;
667 struct rk_pcie *priv = dev_get_priv(dev);
668
669 /* Set power and maybe external ref clk input */
670 if (priv->vpcie3v3) {
671 ret = regulator_set_value(priv->vpcie3v3, 3300000);
672 if (ret) {
673 dev_err(priv->dev, "failed to enable vpcie3v3 (ret=%d)\n",
674 ret);
675 return ret;
676 }
677 }
678
679 msleep(1000);
680
681 ret = generic_phy_init(&priv->phy);
682 if (ret) {
683 dev_err(dev, "failed to init phy (ret=%d)\n", ret);
684 return ret;
685 }
686
687 ret = generic_phy_power_on(&priv->phy);
688 if (ret) {
689 dev_err(dev, "failed to power on phy (ret=%d)\n", ret);
690 goto err_exit_phy;
691 }
692
693 ret = reset_deassert_bulk(&priv->rsts);
694 if (ret) {
695 dev_err(dev, "failed to deassert resets (ret=%d)\n", ret);
696 goto err_power_off_phy;
697 }
698
699 ret = clk_enable_bulk(&priv->clks);
700 if (ret) {
701 dev_err(dev, "failed to enable clks (ret=%d)\n", ret);
702 goto err_deassert_bulk;
703 }
704
705 /* LTSSM EN ctrl mode */
706 val = rk_pcie_readl_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL);
707 val |= PCIE_LTSSM_ENABLE_ENHANCE | (PCIE_LTSSM_ENABLE_ENHANCE << 16);
708 rk_pcie_writel_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL, val);
709
710 /* Set RC mode */
711 rk_pcie_writel_apb(priv, 0x0, 0xf00040);
712 rk_pcie_setup_host(priv);
713
714 ret = rk_pcie_link_up(priv, LINK_SPEED_GEN_3);
715 if (ret < 0)
716 goto err_link_up;
717
718 return 0;
719err_link_up:
720 clk_disable_bulk(&priv->clks);
721err_deassert_bulk:
722 reset_assert_bulk(&priv->rsts);
723err_power_off_phy:
724 generic_phy_power_off(&priv->phy);
725err_exit_phy:
726 generic_phy_exit(&priv->phy);
727
728 return ret;
729}
730
731static int rockchip_pcie_parse_dt(struct udevice *dev)
732{
733 struct rk_pcie *priv = dev_get_priv(dev);
734 int ret;
735
736 priv->dbi_base = (void *)dev_read_addr_index(dev, 0);
737 if (!priv->dbi_base)
738 return -ENODEV;
739
740 dev_dbg(dev, "DBI address is 0x%p\n", priv->dbi_base);
741
742 priv->apb_base = (void *)dev_read_addr_index(dev, 1);
743 if (!priv->apb_base)
744 return -ENODEV;
745
746 dev_dbg(dev, "APB address is 0x%p\n", priv->apb_base);
747
748 ret = gpio_request_by_name(dev, "reset-gpios", 0,
749 &priv->rst_gpio, GPIOD_IS_OUT);
750 if (ret) {
751 dev_err(dev, "failed to find reset-gpios property\n");
752 return ret;
753 }
754
755 ret = reset_get_bulk(dev, &priv->rsts);
756 if (ret) {
757 dev_err(dev, "Can't get reset: %d\n", ret);
758 return ret;
759 }
760
761 ret = clk_get_bulk(dev, &priv->clks);
762 if (ret) {
763 dev_err(dev, "Can't get clock: %d\n", ret);
764 return ret;
765 }
766
767 ret = device_get_supply_regulator(dev, "vpcie3v3-supply",
768 &priv->vpcie3v3);
769 if (ret && ret != -ENOENT) {
770 dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret);
771 return ret;
772 }
773
774 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
775 if (ret) {
776 dev_err(dev, "failed to get pcie phy (ret=%d)\n", ret);
777 return ret;
778 }
779
780 return 0;
781}
782
783/**
784 * rockchip_pcie_probe() - Probe the PCIe bus for active link
785 *
786 * @dev: A pointer to the device being operated on
787 *
788 * Probe for an active link on the PCIe bus and configure the controller
789 * to enable this port.
790 *
791 * Return: 0 on success, else -ENODEV
792 */
793static int rockchip_pcie_probe(struct udevice *dev)
794{
795 struct rk_pcie *priv = dev_get_priv(dev);
796 struct udevice *ctlr = pci_get_controller(dev);
797 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
798 int reti = 0;
799
800 priv->first_busno = dev_seq(dev);
801 priv->dev = dev;
802
803 ret = rockchip_pcie_parse_dt(dev);
804 if (ret)
805 return ret;
806
807 ret = rockchip_pcie_init_port(dev);
808 if (ret)
809 return ret;
810
811 dev_info(dev, "PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n",
812 dev_seq(dev), rk_pcie_get_link_speed(priv),
813 rk_pcie_get_link_width(priv),
814 hose->first_busno);
815
816 for (ret = 0; ret < hose->region_count; ret++) {
817 if (hose->regions[ret].flags == PCI_REGION_IO) {
818 priv->io.phys_start = hose->regions[ret].phys_start; /* IO base */
819 priv->io.bus_start = hose->regions[ret].bus_start; /* IO_bus_addr */
820 priv->io.size = hose->regions[ret].size; /* IO size */
821 } else if (hose->regions[ret].flags == PCI_REGION_MEM) {
822 priv->mem.phys_start = hose->regions[ret].phys_start; /* MEM base */
823 priv->mem.bus_start = hose->regions[ret].bus_start; /* MEM_bus_addr */
824 priv->mem.size = hose->regions[ret].size; /* MEM size */
825 } else if (hose->regions[ret].flags == PCI_REGION_SYS_MEMORY) {
826 priv->cfg_base = (void *)(priv->io.phys_start - priv->io.size);
827 priv->cfg_size = priv->io.size;
828 } else {
829 dev_err(dev, "invalid flags type!\n");
830 }
831 }
832
833 dev_dbg(dev, "Config space: [0x%p - 0x%p, size 0x%llx]\n",
834 priv->cfg_base, priv->cfg_base + priv->cfg_size,
835 priv->cfg_size);
836
837 dev_dbg(dev, "IO space: [0x%llx - 0x%llx, size 0x%lx]\n",
838 priv->io.phys_start, priv->io.phys_start + priv->io.size,
839 priv->io.size);
840
841 dev_dbg(dev, "IO bus: [0x%lx - 0x%lx, size 0x%lx]\n",
842 priv->io.bus_start, priv->io.bus_start + priv->io.size,
843 priv->io.size);
844
845 dev_dbg(dev, "MEM space: [0x%llx - 0x%llx, size 0x%lx]\n",
846 priv->mem.phys_start, priv->mem.phys_start + priv->mem.size,
847 priv->mem.size);
848
849 dev_dbg(dev, "MEM bus: [0x%lx - 0x%lx, size 0x%lx]\n",
850 priv->mem.bus_start, priv->mem.bus_start + priv->mem.size,
851 priv->mem.size);
852
853 return rk_pcie_prog_outbound_atu_unroll(priv,
854 PCIE_ATU_REGION_INDEX0,
855 PCIE_ATU_TYPE_MEM,
856 priv->mem.phys_start,
857 priv->mem.bus_start,
858 priv->mem.size);
859}
860
861static const struct dm_pci_ops rockchip_pcie_ops = {
862 .read_config = rockchip_pcie_rd_conf,
863 .write_config = rockchip_pcie_wr_conf,
864};
865
866static const struct udevice_id rockchip_pcie_ids[] = {
867 { .compatible = "rockchip,rk3568-pcie" },
868 { }
869};
870
871U_BOOT_DRIVER(rockchip_dw_pcie) = {
872 .name = "pcie_dw_rockchip",
873 .id = UCLASS_PCI,
874 .of_match = rockchip_pcie_ids,
875 .ops = &rockchip_pcie_ops,
876 .probe = rockchip_pcie_probe,
877 .priv_auto = sizeof(struct rk_pcie),
878};