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