2 * Rockchip AXI PCIe host controller driver
4 * Copyright (c) 2016 Rockchip, Inc.
6 * Author: Shawn Lin <shawn.lin@rock-chips.com>
7 * Wenrui Li <wenrui.li@rock-chips.com>
9 * Bits taken from Synopsys Designware Host controller driver and
10 * ARM PCI Host generic driver.
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 2 of the License, or
15 * (at your option) any later version.
18 #include <linux/clk.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/irqchip/chained_irq.h>
25 #include <linux/irqdomain.h>
26 #include <linux/kernel.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/of_address.h>
29 #include <linux/of_device.h>
30 #include <linux/of_pci.h>
31 #include <linux/of_platform.h>
32 #include <linux/of_irq.h>
33 #include <linux/pci.h>
34 #include <linux/pci_ids.h>
35 #include <linux/phy/phy.h>
36 #include <linux/platform_device.h>
37 #include <linux/reset.h>
38 #include <linux/regmap.h>
41 * The upper 16 bits of PCIE_CLIENT_CONFIG are a write mask for the lower 16
42 * bits. This allows atomic updates of the register without locking.
44 #define HIWORD_UPDATE(mask, val) (((mask) << 16) | (val))
45 #define HIWORD_UPDATE_BIT(val) HIWORD_UPDATE(val, val)
47 #define ENCODE_LANES(x) ((((x) >> 1) & 3) << 4)
49 #define PCIE_CLIENT_BASE 0x0
50 #define PCIE_CLIENT_CONFIG (PCIE_CLIENT_BASE + 0x00)
51 #define PCIE_CLIENT_CONF_ENABLE HIWORD_UPDATE_BIT(0x0001)
52 #define PCIE_CLIENT_LINK_TRAIN_ENABLE HIWORD_UPDATE_BIT(0x0002)
53 #define PCIE_CLIENT_ARI_ENABLE HIWORD_UPDATE_BIT(0x0008)
54 #define PCIE_CLIENT_CONF_LANE_NUM(x) HIWORD_UPDATE(0x0030, ENCODE_LANES(x))
55 #define PCIE_CLIENT_MODE_RC HIWORD_UPDATE_BIT(0x0040)
56 #define PCIE_CLIENT_GEN_SEL_2 HIWORD_UPDATE_BIT(0x0080)
57 #define PCIE_CLIENT_BASIC_STATUS1 (PCIE_CLIENT_BASE + 0x48)
58 #define PCIE_CLIENT_LINK_STATUS_UP 0x00300000
59 #define PCIE_CLIENT_LINK_STATUS_MASK 0x00300000
60 #define PCIE_CLIENT_INT_MASK (PCIE_CLIENT_BASE + 0x4c)
61 #define PCIE_CLIENT_INT_STATUS (PCIE_CLIENT_BASE + 0x50)
62 #define PCIE_CLIENT_INTR_MASK GENMASK(8, 5)
63 #define PCIE_CLIENT_INTR_SHIFT 5
64 #define PCIE_CLIENT_INT_LEGACY_DONE BIT(15)
65 #define PCIE_CLIENT_INT_MSG BIT(14)
66 #define PCIE_CLIENT_INT_HOT_RST BIT(13)
67 #define PCIE_CLIENT_INT_DPA BIT(12)
68 #define PCIE_CLIENT_INT_FATAL_ERR BIT(11)
69 #define PCIE_CLIENT_INT_NFATAL_ERR BIT(10)
70 #define PCIE_CLIENT_INT_CORR_ERR BIT(9)
71 #define PCIE_CLIENT_INT_INTD BIT(8)
72 #define PCIE_CLIENT_INT_INTC BIT(7)
73 #define PCIE_CLIENT_INT_INTB BIT(6)
74 #define PCIE_CLIENT_INT_INTA BIT(5)
75 #define PCIE_CLIENT_INT_LOCAL BIT(4)
76 #define PCIE_CLIENT_INT_UDMA BIT(3)
77 #define PCIE_CLIENT_INT_PHY BIT(2)
78 #define PCIE_CLIENT_INT_HOT_PLUG BIT(1)
79 #define PCIE_CLIENT_INT_PWR_STCG BIT(0)
81 #define PCIE_CLIENT_INT_LEGACY \
82 (PCIE_CLIENT_INT_INTA | PCIE_CLIENT_INT_INTB | \
83 PCIE_CLIENT_INT_INTC | PCIE_CLIENT_INT_INTD)
85 #define PCIE_CLIENT_INT_CLI \
86 (PCIE_CLIENT_INT_CORR_ERR | PCIE_CLIENT_INT_NFATAL_ERR | \
87 PCIE_CLIENT_INT_FATAL_ERR | PCIE_CLIENT_INT_DPA | \
88 PCIE_CLIENT_INT_HOT_RST | PCIE_CLIENT_INT_MSG | \
89 PCIE_CLIENT_INT_LEGACY_DONE | PCIE_CLIENT_INT_LEGACY | \
92 #define PCIE_CORE_CTRL_MGMT_BASE 0x900000
93 #define PCIE_CORE_CTRL (PCIE_CORE_CTRL_MGMT_BASE + 0x000)
94 #define PCIE_CORE_PL_CONF_SPEED_5G 0x00000008
95 #define PCIE_CORE_PL_CONF_SPEED_MASK 0x00000018
96 #define PCIE_CORE_PL_CONF_LANE_MASK 0x00000006
97 #define PCIE_CORE_PL_CONF_LANE_SHIFT 1
98 #define PCIE_CORE_CTRL_PLC1 (PCIE_CORE_CTRL_MGMT_BASE + 0x004)
99 #define PCIE_CORE_CTRL_PLC1_FTS_MASK GENMASK(23, 8)
100 #define PCIE_CORE_CTRL_PLC1_FTS_SHIFT 8
101 #define PCIE_CORE_CTRL_PLC1_FTS_CNT 0xffff
102 #define PCIE_CORE_TXCREDIT_CFG1 (PCIE_CORE_CTRL_MGMT_BASE + 0x020)
103 #define PCIE_CORE_TXCREDIT_CFG1_MUI_MASK 0xFFFF0000
104 #define PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT 16
105 #define PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(x) \
106 (((x) >> 3) << PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT)
107 #define PCIE_CORE_INT_STATUS (PCIE_CORE_CTRL_MGMT_BASE + 0x20c)
108 #define PCIE_CORE_INT_PRFPE BIT(0)
109 #define PCIE_CORE_INT_CRFPE BIT(1)
110 #define PCIE_CORE_INT_RRPE BIT(2)
111 #define PCIE_CORE_INT_PRFO BIT(3)
112 #define PCIE_CORE_INT_CRFO BIT(4)
113 #define PCIE_CORE_INT_RT BIT(5)
114 #define PCIE_CORE_INT_RTR BIT(6)
115 #define PCIE_CORE_INT_PE BIT(7)
116 #define PCIE_CORE_INT_MTR BIT(8)
117 #define PCIE_CORE_INT_UCR BIT(9)
118 #define PCIE_CORE_INT_FCE BIT(10)
119 #define PCIE_CORE_INT_CT BIT(11)
120 #define PCIE_CORE_INT_UTC BIT(18)
121 #define PCIE_CORE_INT_MMVC BIT(19)
122 #define PCIE_CORE_INT_MASK (PCIE_CORE_CTRL_MGMT_BASE + 0x210)
123 #define PCIE_RC_BAR_CONF (PCIE_CORE_CTRL_MGMT_BASE + 0x300)
125 #define PCIE_CORE_INT \
126 (PCIE_CORE_INT_PRFPE | PCIE_CORE_INT_CRFPE | \
127 PCIE_CORE_INT_RRPE | PCIE_CORE_INT_CRFO | \
128 PCIE_CORE_INT_RT | PCIE_CORE_INT_RTR | \
129 PCIE_CORE_INT_PE | PCIE_CORE_INT_MTR | \
130 PCIE_CORE_INT_UCR | PCIE_CORE_INT_FCE | \
131 PCIE_CORE_INT_CT | PCIE_CORE_INT_UTC | \
134 #define PCIE_RC_CONFIG_BASE 0xa00000
135 #define PCIE_RC_CONFIG_VENDOR (PCIE_RC_CONFIG_BASE + 0x00)
136 #define PCIE_RC_CONFIG_RID_CCR (PCIE_RC_CONFIG_BASE + 0x08)
137 #define PCIE_RC_CONFIG_SCC_SHIFT 16
138 #define PCIE_RC_CONFIG_DCR (PCIE_RC_CONFIG_BASE + 0xc4)
139 #define PCIE_RC_CONFIG_DCR_CSPL_SHIFT 18
140 #define PCIE_RC_CONFIG_DCR_CSPL_LIMIT 0xff
141 #define PCIE_RC_CONFIG_DCR_CPLS_SHIFT 26
142 #define PCIE_RC_CONFIG_LCS (PCIE_RC_CONFIG_BASE + 0xd0)
143 #define PCIE_RC_CONFIG_LCS_RETRAIN_LINK BIT(5)
144 #define PCIE_RC_CONFIG_LCS_LBMIE BIT(10)
145 #define PCIE_RC_CONFIG_LCS_LABIE BIT(11)
146 #define PCIE_RC_CONFIG_LCS_LBMS BIT(30)
147 #define PCIE_RC_CONFIG_LCS_LAMS BIT(31)
148 #define PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 (PCIE_RC_CONFIG_BASE + 0x90c)
150 #define PCIE_CORE_AXI_CONF_BASE 0xc00000
151 #define PCIE_CORE_OB_REGION_ADDR0 (PCIE_CORE_AXI_CONF_BASE + 0x0)
152 #define PCIE_CORE_OB_REGION_ADDR0_NUM_BITS 0x3f
153 #define PCIE_CORE_OB_REGION_ADDR0_LO_ADDR 0xffffff00
154 #define PCIE_CORE_OB_REGION_ADDR1 (PCIE_CORE_AXI_CONF_BASE + 0x4)
155 #define PCIE_CORE_OB_REGION_DESC0 (PCIE_CORE_AXI_CONF_BASE + 0x8)
156 #define PCIE_CORE_OB_REGION_DESC1 (PCIE_CORE_AXI_CONF_BASE + 0xc)
158 #define PCIE_CORE_AXI_INBOUND_BASE 0xc00800
159 #define PCIE_RP_IB_ADDR0 (PCIE_CORE_AXI_INBOUND_BASE + 0x0)
160 #define PCIE_CORE_IB_REGION_ADDR0_NUM_BITS 0x3f
161 #define PCIE_CORE_IB_REGION_ADDR0_LO_ADDR 0xffffff00
162 #define PCIE_RP_IB_ADDR1 (PCIE_CORE_AXI_INBOUND_BASE + 0x4)
164 /* Size of one AXI Region (not Region 0) */
165 #define AXI_REGION_SIZE BIT(20)
166 /* Size of Region 0, equal to sum of sizes of other regions */
167 #define AXI_REGION_0_SIZE (32 * (0x1 << 20))
168 #define OB_REG_SIZE_SHIFT 5
169 #define IB_ROOT_PORT_REG_SIZE_SHIFT 3
170 #define AXI_WRAPPER_IO_WRITE 0x6
171 #define AXI_WRAPPER_MEM_WRITE 0x2
173 #define MAX_AXI_IB_ROOTPORT_REGION_NUM 3
174 #define MIN_AXI_ADDR_BITS_PASSED 8
175 #define ROCKCHIP_VENDOR_ID 0x1d87
176 #define PCIE_ECAM_BUS(x) (((x) & 0xff) << 20)
177 #define PCIE_ECAM_DEV(x) (((x) & 0x1f) << 15)
178 #define PCIE_ECAM_FUNC(x) (((x) & 0x7) << 12)
179 #define PCIE_ECAM_REG(x) (((x) & 0xfff) << 0)
180 #define PCIE_ECAM_ADDR(bus, dev, func, reg) \
181 (PCIE_ECAM_BUS(bus) | PCIE_ECAM_DEV(dev) | \
182 PCIE_ECAM_FUNC(func) | PCIE_ECAM_REG(reg))
184 #define RC_REGION_0_ADDR_TRANS_H 0x00000000
185 #define RC_REGION_0_ADDR_TRANS_L 0x00000000
186 #define RC_REGION_0_PASS_BITS (25 - 1)
187 #define MAX_AXI_WRAPPER_REGION_NUM 33
189 struct rockchip_pcie {
190 void __iomem *reg_base; /* DT axi-base */
191 void __iomem *apb_base; /* DT apb-base */
193 struct reset_control *core_rst;
194 struct reset_control *mgmt_rst;
195 struct reset_control *mgmt_sticky_rst;
196 struct reset_control *pipe_rst;
197 struct clk *aclk_pcie;
198 struct clk *aclk_perf_pcie;
199 struct clk *hclk_pcie;
200 struct clk *clk_pcie_pm;
201 struct regulator *vpcie3v3; /* 3.3V power supply */
202 struct regulator *vpcie1v8; /* 1.8V power supply */
203 struct regulator *vpcie0v9; /* 0.9V power supply */
204 struct gpio_desc *ep_gpio;
208 struct irq_domain *irq_domain;
211 static u32 rockchip_pcie_read(struct rockchip_pcie *rockchip, u32 reg)
213 return readl(rockchip->apb_base + reg);
216 static void rockchip_pcie_write(struct rockchip_pcie *rockchip, u32 val,
219 writel(val, rockchip->apb_base + reg);
222 static void rockchip_pcie_enable_bw_int(struct rockchip_pcie *rockchip)
226 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
227 status |= (PCIE_RC_CONFIG_LCS_LBMIE | PCIE_RC_CONFIG_LCS_LABIE);
228 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
231 static void rockchip_pcie_clr_bw_int(struct rockchip_pcie *rockchip)
235 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
236 status |= (PCIE_RC_CONFIG_LCS_LBMS | PCIE_RC_CONFIG_LCS_LAMS);
237 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
240 static void rockchip_pcie_update_txcredit_mui(struct rockchip_pcie *rockchip)
244 /* Update Tx credit maximum update interval */
245 val = rockchip_pcie_read(rockchip, PCIE_CORE_TXCREDIT_CFG1);
246 val &= ~PCIE_CORE_TXCREDIT_CFG1_MUI_MASK;
247 val |= PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(24000); /* ns */
248 rockchip_pcie_write(rockchip, val, PCIE_CORE_TXCREDIT_CFG1);
251 static int rockchip_pcie_valid_device(struct rockchip_pcie *rockchip,
252 struct pci_bus *bus, int dev)
254 /* access only one slot on each root port */
255 if (bus->number == rockchip->root_bus_nr && dev > 0)
259 * do not read more than one device on the bus directly attached
260 * to RC's downstream side.
262 if (bus->primary == rockchip->root_bus_nr && dev > 0)
268 static int rockchip_pcie_rd_own_conf(struct rockchip_pcie *rockchip,
269 int where, int size, u32 *val)
271 void __iomem *addr = rockchip->apb_base + PCIE_RC_CONFIG_BASE + where;
273 if (!IS_ALIGNED((uintptr_t)addr, size)) {
275 return PCIBIOS_BAD_REGISTER_NUMBER;
280 } else if (size == 2) {
282 } else if (size == 1) {
286 return PCIBIOS_BAD_REGISTER_NUMBER;
288 return PCIBIOS_SUCCESSFUL;
291 static int rockchip_pcie_wr_own_conf(struct rockchip_pcie *rockchip,
292 int where, int size, u32 val)
294 u32 mask, tmp, offset;
296 offset = where & ~0x3;
299 writel(val, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset);
300 return PCIBIOS_SUCCESSFUL;
303 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
306 * N.B. This read/modify/write isn't safe in general because it can
307 * corrupt RW1C bits in adjacent registers. But the hardware
308 * doesn't support smaller writes.
310 tmp = readl(rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset) & mask;
311 tmp |= val << ((where & 0x3) * 8);
312 writel(tmp, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset);
314 return PCIBIOS_SUCCESSFUL;
317 static int rockchip_pcie_rd_other_conf(struct rockchip_pcie *rockchip,
318 struct pci_bus *bus, u32 devfn,
319 int where, int size, u32 *val)
323 busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
324 PCI_FUNC(devfn), where);
326 if (!IS_ALIGNED(busdev, size)) {
328 return PCIBIOS_BAD_REGISTER_NUMBER;
332 *val = readl(rockchip->reg_base + busdev);
333 } else if (size == 2) {
334 *val = readw(rockchip->reg_base + busdev);
335 } else if (size == 1) {
336 *val = readb(rockchip->reg_base + busdev);
339 return PCIBIOS_BAD_REGISTER_NUMBER;
341 return PCIBIOS_SUCCESSFUL;
344 static int rockchip_pcie_wr_other_conf(struct rockchip_pcie *rockchip,
345 struct pci_bus *bus, u32 devfn,
346 int where, int size, u32 val)
350 busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn),
351 PCI_FUNC(devfn), where);
352 if (!IS_ALIGNED(busdev, size))
353 return PCIBIOS_BAD_REGISTER_NUMBER;
356 writel(val, rockchip->reg_base + busdev);
358 writew(val, rockchip->reg_base + busdev);
360 writeb(val, rockchip->reg_base + busdev);
362 return PCIBIOS_BAD_REGISTER_NUMBER;
364 return PCIBIOS_SUCCESSFUL;
367 static int rockchip_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
370 struct rockchip_pcie *rockchip = bus->sysdata;
372 if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn))) {
374 return PCIBIOS_DEVICE_NOT_FOUND;
377 if (bus->number == rockchip->root_bus_nr)
378 return rockchip_pcie_rd_own_conf(rockchip, where, size, val);
380 return rockchip_pcie_rd_other_conf(rockchip, bus, devfn, where, size, val);
383 static int rockchip_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
384 int where, int size, u32 val)
386 struct rockchip_pcie *rockchip = bus->sysdata;
388 if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn)))
389 return PCIBIOS_DEVICE_NOT_FOUND;
391 if (bus->number == rockchip->root_bus_nr)
392 return rockchip_pcie_wr_own_conf(rockchip, where, size, val);
394 return rockchip_pcie_wr_other_conf(rockchip, bus, devfn, where, size, val);
397 static struct pci_ops rockchip_pcie_ops = {
398 .read = rockchip_pcie_rd_conf,
399 .write = rockchip_pcie_wr_conf,
402 static void rockchip_pcie_set_power_limit(struct rockchip_pcie *rockchip)
404 u32 status, curr, scale, power;
406 if (IS_ERR(rockchip->vpcie3v3))
410 * Set RC's captured slot power limit and scale if
411 * vpcie3v3 available. The default values are both zero
412 * which means the software should set these two according
413 * to the actual power supply.
415 curr = regulator_get_current_limit(rockchip->vpcie3v3);
417 scale = 3; /* 0.001x */
418 curr = curr / 1000; /* convert to mA */
419 power = (curr * 3300) / 1000; /* milliwatt */
420 while (power > PCIE_RC_CONFIG_DCR_CSPL_LIMIT) {
422 dev_warn(rockchip->dev, "invalid power supply\n");
429 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_DCR);
430 status |= (power << PCIE_RC_CONFIG_DCR_CSPL_SHIFT) |
431 (scale << PCIE_RC_CONFIG_DCR_CPLS_SHIFT);
432 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_DCR);
437 * rockchip_pcie_init_port - Initialize hardware
438 * @rockchip: PCIe port information
440 static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip)
442 struct device *dev = rockchip->dev;
445 unsigned long timeout;
447 gpiod_set_value(rockchip->ep_gpio, 0);
449 err = phy_init(rockchip->phy);
451 dev_err(dev, "fail to init phy, err %d\n", err);
455 err = reset_control_assert(rockchip->core_rst);
457 dev_err(dev, "assert core_rst err %d\n", err);
461 err = reset_control_assert(rockchip->mgmt_rst);
463 dev_err(dev, "assert mgmt_rst err %d\n", err);
467 err = reset_control_assert(rockchip->mgmt_sticky_rst);
469 dev_err(dev, "assert mgmt_sticky_rst err %d\n", err);
473 err = reset_control_assert(rockchip->pipe_rst);
475 dev_err(dev, "assert pipe_rst err %d\n", err);
479 rockchip_pcie_write(rockchip,
480 PCIE_CLIENT_CONF_ENABLE |
481 PCIE_CLIENT_LINK_TRAIN_ENABLE |
482 PCIE_CLIENT_ARI_ENABLE |
483 PCIE_CLIENT_CONF_LANE_NUM(rockchip->lanes) |
484 PCIE_CLIENT_MODE_RC |
485 PCIE_CLIENT_GEN_SEL_2,
488 err = phy_power_on(rockchip->phy);
490 dev_err(dev, "fail to power on phy, err %d\n", err);
495 * Please don't reorder the deassert sequence of the following
498 err = reset_control_deassert(rockchip->mgmt_sticky_rst);
500 dev_err(dev, "deassert mgmt_sticky_rst err %d\n", err);
504 err = reset_control_deassert(rockchip->core_rst);
506 dev_err(dev, "deassert core_rst err %d\n", err);
510 err = reset_control_deassert(rockchip->mgmt_rst);
512 dev_err(dev, "deassert mgmt_rst err %d\n", err);
516 err = reset_control_deassert(rockchip->pipe_rst);
518 dev_err(dev, "deassert pipe_rst err %d\n", err);
523 * We need to read/write PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 before
524 * enabling ASPM. Otherwise L1PwrOnSc and L1PwrOnVal isn't
525 * reliable and enabling ASPM doesn't work. This is a controller
526 * bug we need to work around.
528 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2);
529 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2);
531 /* Fix the transmitted FTS count desired to exit from L0s. */
532 status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL_PLC1);
533 status = (status & PCIE_CORE_CTRL_PLC1_FTS_MASK) |
534 (PCIE_CORE_CTRL_PLC1_FTS_CNT << PCIE_CORE_CTRL_PLC1_FTS_SHIFT);
535 rockchip_pcie_write(rockchip, status, PCIE_CORE_CTRL_PLC1);
537 rockchip_pcie_set_power_limit(rockchip);
539 /* Enable Gen1 training */
540 rockchip_pcie_write(rockchip, PCIE_CLIENT_LINK_TRAIN_ENABLE,
543 gpiod_set_value(rockchip->ep_gpio, 1);
545 /* 500ms timeout value should be enough for Gen1/2 training */
546 timeout = jiffies + msecs_to_jiffies(500);
549 status = rockchip_pcie_read(rockchip,
550 PCIE_CLIENT_BASIC_STATUS1);
551 if ((status & PCIE_CLIENT_LINK_STATUS_MASK) ==
552 PCIE_CLIENT_LINK_STATUS_UP) {
553 dev_dbg(dev, "PCIe link training gen1 pass!\n");
557 if (time_after(jiffies, timeout)) {
558 dev_err(dev, "PCIe link training gen1 timeout!\n");
566 * Enable retrain for gen2. This should be configured only after
569 status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS);
570 status |= PCIE_RC_CONFIG_LCS_RETRAIN_LINK;
571 rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS);
573 timeout = jiffies + msecs_to_jiffies(500);
575 status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
576 if ((status & PCIE_CORE_PL_CONF_SPEED_MASK) ==
577 PCIE_CORE_PL_CONF_SPEED_5G) {
578 dev_dbg(dev, "PCIe link training gen2 pass!\n");
582 if (time_after(jiffies, timeout)) {
583 dev_dbg(dev, "PCIe link training gen2 timeout, fall back to gen1!\n");
590 /* Check the final link width from negotiated lane counter from MGMT */
591 status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL);
592 status = 0x1 << ((status & PCIE_CORE_PL_CONF_LANE_MASK) >>
593 PCIE_CORE_PL_CONF_LANE_MASK);
594 dev_dbg(dev, "current link width is x%d\n", status);
596 rockchip_pcie_write(rockchip, ROCKCHIP_VENDOR_ID,
597 PCIE_RC_CONFIG_VENDOR);
598 rockchip_pcie_write(rockchip,
599 PCI_CLASS_BRIDGE_PCI << PCIE_RC_CONFIG_SCC_SHIFT,
600 PCIE_RC_CONFIG_RID_CCR);
601 rockchip_pcie_write(rockchip, 0x0, PCIE_RC_BAR_CONF);
603 rockchip_pcie_write(rockchip,
604 (RC_REGION_0_ADDR_TRANS_L + RC_REGION_0_PASS_BITS),
605 PCIE_CORE_OB_REGION_ADDR0);
606 rockchip_pcie_write(rockchip, RC_REGION_0_ADDR_TRANS_H,
607 PCIE_CORE_OB_REGION_ADDR1);
608 rockchip_pcie_write(rockchip, 0x0080000a, PCIE_CORE_OB_REGION_DESC0);
609 rockchip_pcie_write(rockchip, 0x0, PCIE_CORE_OB_REGION_DESC1);
614 static irqreturn_t rockchip_pcie_subsys_irq_handler(int irq, void *arg)
616 struct rockchip_pcie *rockchip = arg;
617 struct device *dev = rockchip->dev;
621 reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
622 if (reg & PCIE_CLIENT_INT_LOCAL) {
623 dev_dbg(dev, "local interrupt received\n");
624 sub_reg = rockchip_pcie_read(rockchip, PCIE_CORE_INT_STATUS);
625 if (sub_reg & PCIE_CORE_INT_PRFPE)
626 dev_dbg(dev, "parity error detected while reading from the PNP receive FIFO RAM\n");
628 if (sub_reg & PCIE_CORE_INT_CRFPE)
629 dev_dbg(dev, "parity error detected while reading from the Completion Receive FIFO RAM\n");
631 if (sub_reg & PCIE_CORE_INT_RRPE)
632 dev_dbg(dev, "parity error detected while reading from replay buffer RAM\n");
634 if (sub_reg & PCIE_CORE_INT_PRFO)
635 dev_dbg(dev, "overflow occurred in the PNP receive FIFO\n");
637 if (sub_reg & PCIE_CORE_INT_CRFO)
638 dev_dbg(dev, "overflow occurred in the completion receive FIFO\n");
640 if (sub_reg & PCIE_CORE_INT_RT)
641 dev_dbg(dev, "replay timer timed out\n");
643 if (sub_reg & PCIE_CORE_INT_RTR)
644 dev_dbg(dev, "replay timer rolled over after 4 transmissions of the same TLP\n");
646 if (sub_reg & PCIE_CORE_INT_PE)
647 dev_dbg(dev, "phy error detected on receive side\n");
649 if (sub_reg & PCIE_CORE_INT_MTR)
650 dev_dbg(dev, "malformed TLP received from the link\n");
652 if (sub_reg & PCIE_CORE_INT_UCR)
653 dev_dbg(dev, "malformed TLP received from the link\n");
655 if (sub_reg & PCIE_CORE_INT_FCE)
656 dev_dbg(dev, "an error was observed in the flow control advertisements from the other side\n");
658 if (sub_reg & PCIE_CORE_INT_CT)
659 dev_dbg(dev, "a request timed out waiting for completion\n");
661 if (sub_reg & PCIE_CORE_INT_UTC)
662 dev_dbg(dev, "unmapped TC error\n");
664 if (sub_reg & PCIE_CORE_INT_MMVC)
665 dev_dbg(dev, "MSI mask register changes\n");
667 rockchip_pcie_write(rockchip, sub_reg, PCIE_CORE_INT_STATUS);
668 } else if (reg & PCIE_CLIENT_INT_PHY) {
669 dev_dbg(dev, "phy link changes\n");
670 rockchip_pcie_update_txcredit_mui(rockchip);
671 rockchip_pcie_clr_bw_int(rockchip);
674 rockchip_pcie_write(rockchip, reg & PCIE_CLIENT_INT_LOCAL,
675 PCIE_CLIENT_INT_STATUS);
680 static irqreturn_t rockchip_pcie_client_irq_handler(int irq, void *arg)
682 struct rockchip_pcie *rockchip = arg;
683 struct device *dev = rockchip->dev;
686 reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
687 if (reg & PCIE_CLIENT_INT_LEGACY_DONE)
688 dev_dbg(dev, "legacy done interrupt received\n");
690 if (reg & PCIE_CLIENT_INT_MSG)
691 dev_dbg(dev, "message done interrupt received\n");
693 if (reg & PCIE_CLIENT_INT_HOT_RST)
694 dev_dbg(dev, "hot reset interrupt received\n");
696 if (reg & PCIE_CLIENT_INT_DPA)
697 dev_dbg(dev, "dpa interrupt received\n");
699 if (reg & PCIE_CLIENT_INT_FATAL_ERR)
700 dev_dbg(dev, "fatal error interrupt received\n");
702 if (reg & PCIE_CLIENT_INT_NFATAL_ERR)
703 dev_dbg(dev, "no fatal error interrupt received\n");
705 if (reg & PCIE_CLIENT_INT_CORR_ERR)
706 dev_dbg(dev, "correctable error interrupt received\n");
708 if (reg & PCIE_CLIENT_INT_PHY)
709 dev_dbg(dev, "phy interrupt received\n");
711 rockchip_pcie_write(rockchip, reg & (PCIE_CLIENT_INT_LEGACY_DONE |
712 PCIE_CLIENT_INT_MSG | PCIE_CLIENT_INT_HOT_RST |
713 PCIE_CLIENT_INT_DPA | PCIE_CLIENT_INT_FATAL_ERR |
714 PCIE_CLIENT_INT_NFATAL_ERR |
715 PCIE_CLIENT_INT_CORR_ERR |
716 PCIE_CLIENT_INT_PHY),
717 PCIE_CLIENT_INT_STATUS);
722 static void rockchip_pcie_legacy_int_handler(struct irq_desc *desc)
724 struct irq_chip *chip = irq_desc_get_chip(desc);
725 struct rockchip_pcie *rockchip = irq_desc_get_handler_data(desc);
726 struct device *dev = rockchip->dev;
731 chained_irq_enter(chip, desc);
733 reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS);
734 reg = (reg & PCIE_CLIENT_INTR_MASK) >> PCIE_CLIENT_INTR_SHIFT;
737 hwirq = ffs(reg) - 1;
740 virq = irq_find_mapping(rockchip->irq_domain, hwirq);
742 generic_handle_irq(virq);
744 dev_err(dev, "unexpected IRQ, INT%d\n", hwirq);
747 chained_irq_exit(chip, desc);
752 * rockchip_pcie_parse_dt - Parse Device Tree
753 * @rockchip: PCIe port information
755 * Return: '0' on success and error value on failure
757 static int rockchip_pcie_parse_dt(struct rockchip_pcie *rockchip)
759 struct device *dev = rockchip->dev;
760 struct platform_device *pdev = to_platform_device(dev);
761 struct device_node *node = dev->of_node;
762 struct resource *regs;
766 regs = platform_get_resource_byname(pdev,
769 rockchip->reg_base = devm_ioremap_resource(dev, regs);
770 if (IS_ERR(rockchip->reg_base))
771 return PTR_ERR(rockchip->reg_base);
773 regs = platform_get_resource_byname(pdev,
776 rockchip->apb_base = devm_ioremap_resource(dev, regs);
777 if (IS_ERR(rockchip->apb_base))
778 return PTR_ERR(rockchip->apb_base);
780 rockchip->phy = devm_phy_get(dev, "pcie-phy");
781 if (IS_ERR(rockchip->phy)) {
782 if (PTR_ERR(rockchip->phy) != -EPROBE_DEFER)
783 dev_err(dev, "missing phy\n");
784 return PTR_ERR(rockchip->phy);
788 err = of_property_read_u32(node, "num-lanes", &rockchip->lanes);
789 if (!err && (rockchip->lanes == 0 ||
790 rockchip->lanes == 3 ||
791 rockchip->lanes > 4)) {
792 dev_warn(dev, "invalid num-lanes, default to use one lane\n");
796 rockchip->core_rst = devm_reset_control_get(dev, "core");
797 if (IS_ERR(rockchip->core_rst)) {
798 if (PTR_ERR(rockchip->core_rst) != -EPROBE_DEFER)
799 dev_err(dev, "missing core reset property in node\n");
800 return PTR_ERR(rockchip->core_rst);
803 rockchip->mgmt_rst = devm_reset_control_get(dev, "mgmt");
804 if (IS_ERR(rockchip->mgmt_rst)) {
805 if (PTR_ERR(rockchip->mgmt_rst) != -EPROBE_DEFER)
806 dev_err(dev, "missing mgmt reset property in node\n");
807 return PTR_ERR(rockchip->mgmt_rst);
810 rockchip->mgmt_sticky_rst = devm_reset_control_get(dev, "mgmt-sticky");
811 if (IS_ERR(rockchip->mgmt_sticky_rst)) {
812 if (PTR_ERR(rockchip->mgmt_sticky_rst) != -EPROBE_DEFER)
813 dev_err(dev, "missing mgmt-sticky reset property in node\n");
814 return PTR_ERR(rockchip->mgmt_sticky_rst);
817 rockchip->pipe_rst = devm_reset_control_get(dev, "pipe");
818 if (IS_ERR(rockchip->pipe_rst)) {
819 if (PTR_ERR(rockchip->pipe_rst) != -EPROBE_DEFER)
820 dev_err(dev, "missing pipe reset property in node\n");
821 return PTR_ERR(rockchip->pipe_rst);
824 rockchip->ep_gpio = devm_gpiod_get(dev, "ep", GPIOD_OUT_HIGH);
825 if (IS_ERR(rockchip->ep_gpio)) {
826 dev_err(dev, "missing ep-gpios property in node\n");
827 return PTR_ERR(rockchip->ep_gpio);
830 rockchip->aclk_pcie = devm_clk_get(dev, "aclk");
831 if (IS_ERR(rockchip->aclk_pcie)) {
832 dev_err(dev, "aclk clock not found\n");
833 return PTR_ERR(rockchip->aclk_pcie);
836 rockchip->aclk_perf_pcie = devm_clk_get(dev, "aclk-perf");
837 if (IS_ERR(rockchip->aclk_perf_pcie)) {
838 dev_err(dev, "aclk_perf clock not found\n");
839 return PTR_ERR(rockchip->aclk_perf_pcie);
842 rockchip->hclk_pcie = devm_clk_get(dev, "hclk");
843 if (IS_ERR(rockchip->hclk_pcie)) {
844 dev_err(dev, "hclk clock not found\n");
845 return PTR_ERR(rockchip->hclk_pcie);
848 rockchip->clk_pcie_pm = devm_clk_get(dev, "pm");
849 if (IS_ERR(rockchip->clk_pcie_pm)) {
850 dev_err(dev, "pm clock not found\n");
851 return PTR_ERR(rockchip->clk_pcie_pm);
854 irq = platform_get_irq_byname(pdev, "sys");
856 dev_err(dev, "missing sys IRQ resource\n");
860 err = devm_request_irq(dev, irq, rockchip_pcie_subsys_irq_handler,
861 IRQF_SHARED, "pcie-sys", rockchip);
863 dev_err(dev, "failed to request PCIe subsystem IRQ\n");
867 irq = platform_get_irq_byname(pdev, "legacy");
869 dev_err(dev, "missing legacy IRQ resource\n");
873 irq_set_chained_handler_and_data(irq,
874 rockchip_pcie_legacy_int_handler,
877 irq = platform_get_irq_byname(pdev, "client");
879 dev_err(dev, "missing client IRQ resource\n");
883 err = devm_request_irq(dev, irq, rockchip_pcie_client_irq_handler,
884 IRQF_SHARED, "pcie-client", rockchip);
886 dev_err(dev, "failed to request PCIe client IRQ\n");
890 rockchip->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3");
891 if (IS_ERR(rockchip->vpcie3v3)) {
892 if (PTR_ERR(rockchip->vpcie3v3) == -EPROBE_DEFER)
893 return -EPROBE_DEFER;
894 dev_info(dev, "no vpcie3v3 regulator found\n");
897 rockchip->vpcie1v8 = devm_regulator_get_optional(dev, "vpcie1v8");
898 if (IS_ERR(rockchip->vpcie1v8)) {
899 if (PTR_ERR(rockchip->vpcie1v8) == -EPROBE_DEFER)
900 return -EPROBE_DEFER;
901 dev_info(dev, "no vpcie1v8 regulator found\n");
904 rockchip->vpcie0v9 = devm_regulator_get_optional(dev, "vpcie0v9");
905 if (IS_ERR(rockchip->vpcie0v9)) {
906 if (PTR_ERR(rockchip->vpcie0v9) == -EPROBE_DEFER)
907 return -EPROBE_DEFER;
908 dev_info(dev, "no vpcie0v9 regulator found\n");
914 static int rockchip_pcie_set_vpcie(struct rockchip_pcie *rockchip)
916 struct device *dev = rockchip->dev;
919 if (!IS_ERR(rockchip->vpcie3v3)) {
920 err = regulator_enable(rockchip->vpcie3v3);
922 dev_err(dev, "fail to enable vpcie3v3 regulator\n");
927 if (!IS_ERR(rockchip->vpcie1v8)) {
928 err = regulator_enable(rockchip->vpcie1v8);
930 dev_err(dev, "fail to enable vpcie1v8 regulator\n");
931 goto err_disable_3v3;
935 if (!IS_ERR(rockchip->vpcie0v9)) {
936 err = regulator_enable(rockchip->vpcie0v9);
938 dev_err(dev, "fail to enable vpcie0v9 regulator\n");
939 goto err_disable_1v8;
946 if (!IS_ERR(rockchip->vpcie1v8))
947 regulator_disable(rockchip->vpcie1v8);
949 if (!IS_ERR(rockchip->vpcie3v3))
950 regulator_disable(rockchip->vpcie3v3);
955 static void rockchip_pcie_enable_interrupts(struct rockchip_pcie *rockchip)
957 rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) &
958 (~PCIE_CLIENT_INT_CLI), PCIE_CLIENT_INT_MASK);
959 rockchip_pcie_write(rockchip, (u32)(~PCIE_CORE_INT),
962 rockchip_pcie_enable_bw_int(rockchip);
965 static int rockchip_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
966 irq_hw_number_t hwirq)
968 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
969 irq_set_chip_data(irq, domain->host_data);
974 static const struct irq_domain_ops intx_domain_ops = {
975 .map = rockchip_pcie_intx_map,
978 static int rockchip_pcie_init_irq_domain(struct rockchip_pcie *rockchip)
980 struct device *dev = rockchip->dev;
981 struct device_node *intc = of_get_next_child(dev->of_node, NULL);
984 dev_err(dev, "missing child interrupt-controller node\n");
988 rockchip->irq_domain = irq_domain_add_linear(intc, 4,
989 &intx_domain_ops, rockchip);
990 if (!rockchip->irq_domain) {
991 dev_err(dev, "failed to get a INTx IRQ domain\n");
998 static int rockchip_pcie_prog_ob_atu(struct rockchip_pcie *rockchip,
999 int region_no, int type, u8 num_pass_bits,
1000 u32 lower_addr, u32 upper_addr)
1007 if (region_no >= MAX_AXI_WRAPPER_REGION_NUM)
1009 if (num_pass_bits + 1 < 8)
1011 if (num_pass_bits > 63)
1013 if (region_no == 0) {
1014 if (AXI_REGION_0_SIZE < (2ULL << num_pass_bits))
1017 if (region_no != 0) {
1018 if (AXI_REGION_SIZE < (2ULL << num_pass_bits))
1022 aw_offset = (region_no << OB_REG_SIZE_SHIFT);
1024 ob_addr_0 = num_pass_bits & PCIE_CORE_OB_REGION_ADDR0_NUM_BITS;
1025 ob_addr_0 |= lower_addr & PCIE_CORE_OB_REGION_ADDR0_LO_ADDR;
1026 ob_addr_1 = upper_addr;
1027 ob_desc_0 = (1 << 23 | type);
1029 rockchip_pcie_write(rockchip, ob_addr_0,
1030 PCIE_CORE_OB_REGION_ADDR0 + aw_offset);
1031 rockchip_pcie_write(rockchip, ob_addr_1,
1032 PCIE_CORE_OB_REGION_ADDR1 + aw_offset);
1033 rockchip_pcie_write(rockchip, ob_desc_0,
1034 PCIE_CORE_OB_REGION_DESC0 + aw_offset);
1035 rockchip_pcie_write(rockchip, 0,
1036 PCIE_CORE_OB_REGION_DESC1 + aw_offset);
1041 static int rockchip_pcie_prog_ib_atu(struct rockchip_pcie *rockchip,
1042 int region_no, u8 num_pass_bits,
1043 u32 lower_addr, u32 upper_addr)
1049 if (region_no > MAX_AXI_IB_ROOTPORT_REGION_NUM)
1051 if (num_pass_bits + 1 < MIN_AXI_ADDR_BITS_PASSED)
1053 if (num_pass_bits > 63)
1056 aw_offset = (region_no << IB_ROOT_PORT_REG_SIZE_SHIFT);
1058 ib_addr_0 = num_pass_bits & PCIE_CORE_IB_REGION_ADDR0_NUM_BITS;
1059 ib_addr_0 |= (lower_addr << 8) & PCIE_CORE_IB_REGION_ADDR0_LO_ADDR;
1060 ib_addr_1 = upper_addr;
1062 rockchip_pcie_write(rockchip, ib_addr_0, PCIE_RP_IB_ADDR0 + aw_offset);
1063 rockchip_pcie_write(rockchip, ib_addr_1, PCIE_RP_IB_ADDR1 + aw_offset);
1068 static int rockchip_pcie_probe(struct platform_device *pdev)
1070 struct rockchip_pcie *rockchip;
1071 struct device *dev = &pdev->dev;
1072 struct pci_bus *bus, *child;
1073 struct resource_entry *win;
1074 resource_size_t io_base;
1075 struct resource *mem;
1076 struct resource *io;
1077 phys_addr_t io_bus_addr = 0;
1079 phys_addr_t mem_bus_addr = 0;
1090 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
1094 rockchip->dev = dev;
1096 err = rockchip_pcie_parse_dt(rockchip);
1100 err = clk_prepare_enable(rockchip->aclk_pcie);
1102 dev_err(dev, "unable to enable aclk_pcie clock\n");
1106 err = clk_prepare_enable(rockchip->aclk_perf_pcie);
1108 dev_err(dev, "unable to enable aclk_perf_pcie clock\n");
1109 goto err_aclk_perf_pcie;
1112 err = clk_prepare_enable(rockchip->hclk_pcie);
1114 dev_err(dev, "unable to enable hclk_pcie clock\n");
1118 err = clk_prepare_enable(rockchip->clk_pcie_pm);
1120 dev_err(dev, "unable to enable hclk_pcie clock\n");
1124 err = rockchip_pcie_set_vpcie(rockchip);
1126 dev_err(dev, "failed to set vpcie regulator\n");
1130 err = rockchip_pcie_init_port(rockchip);
1134 platform_set_drvdata(pdev, rockchip);
1136 rockchip_pcie_enable_interrupts(rockchip);
1138 err = rockchip_pcie_init_irq_domain(rockchip);
1142 err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff,
1147 err = devm_request_pci_bus_resources(dev, &res);
1151 /* Get the I/O and memory ranges from DT */
1153 resource_list_for_each_entry(win, &res) {
1154 switch (resource_type(win->res)) {
1158 io_size = resource_size(io);
1159 io_bus_addr = io->start - win->offset;
1160 err = pci_remap_iospace(io, io_base);
1162 dev_warn(dev, "error %d: failed to map resource %pR\n",
1167 case IORESOURCE_MEM:
1170 mem_size = resource_size(mem);
1171 mem_bus_addr = mem->start - win->offset;
1173 case IORESOURCE_BUS:
1174 rockchip->root_bus_nr = win->res->start;
1182 for (reg_no = 0; reg_no < (mem_size >> 20); reg_no++) {
1183 err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1,
1184 AXI_WRAPPER_MEM_WRITE,
1190 dev_err(dev, "program RC mem outbound ATU failed\n");
1196 err = rockchip_pcie_prog_ib_atu(rockchip, 2, 32 - 1, 0x0, 0);
1198 dev_err(dev, "program RC mem inbound ATU failed\n");
1202 offset = mem_size >> 20;
1205 for (reg_no = 0; reg_no < (io_size >> 20); reg_no++) {
1206 err = rockchip_pcie_prog_ob_atu(rockchip,
1207 reg_no + 1 + offset,
1208 AXI_WRAPPER_IO_WRITE,
1214 dev_err(dev, "program RC io outbound ATU failed\n");
1220 bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res);
1226 pci_bus_size_bridges(bus);
1227 pci_bus_assign_resources(bus);
1228 list_for_each_entry(child, &bus->children, node)
1229 pcie_bus_configure_settings(child);
1231 pci_bus_add_devices(bus);
1233 dev_warn(dev, "only 32-bit config accesses supported; smaller writes may corrupt adjacent RW1C fields\n");
1238 if (!IS_ERR(rockchip->vpcie3v3))
1239 regulator_disable(rockchip->vpcie3v3);
1240 if (!IS_ERR(rockchip->vpcie1v8))
1241 regulator_disable(rockchip->vpcie1v8);
1242 if (!IS_ERR(rockchip->vpcie0v9))
1243 regulator_disable(rockchip->vpcie0v9);
1245 clk_disable_unprepare(rockchip->clk_pcie_pm);
1247 clk_disable_unprepare(rockchip->hclk_pcie);
1249 clk_disable_unprepare(rockchip->aclk_perf_pcie);
1251 clk_disable_unprepare(rockchip->aclk_pcie);
1256 static const struct of_device_id rockchip_pcie_of_match[] = {
1257 { .compatible = "rockchip,rk3399-pcie", },
1261 static struct platform_driver rockchip_pcie_driver = {
1263 .name = "rockchip-pcie",
1264 .of_match_table = rockchip_pcie_of_match,
1266 .probe = rockchip_pcie_probe,
1269 builtin_platform_driver(rockchip_pcie_driver);