blob: 6da618055cbe48ca019c96939f13fc42a4a430c8 [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>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Shawn Lin9ddc0782021-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
Neil Armstrongc90f3d02021-03-25 15:49:20 +010025#include "pcie_dw_common.h"
26
Shawn Lin9ddc0782021-01-15 18:01:22 +080027DECLARE_GLOBAL_DATA_PTR;
28
29/**
30 * struct rk_pcie - RK DW PCIe controller state
31 *
32 * @vpcie3v3: The 3.3v power supply for slot
Shawn Lin9ddc0782021-01-15 18:01:22 +080033 * @apb_base: The base address of vendor regs
Shawn Lin9ddc0782021-01-15 18:01:22 +080034 * @rst_gpio: The #PERST signal for slot
Shawn Lin9ddc0782021-01-15 18:01:22 +080035 */
36struct rk_pcie {
Neil Armstrongc90f3d02021-03-25 15:49:20 +010037 /* Must be first member of the struct */
38 struct pcie_dw dw;
Shawn Lin9ddc0782021-01-15 18:01:22 +080039 struct udevice *vpcie3v3;
Shawn Lin9ddc0782021-01-15 18:01:22 +080040 void *apb_base;
Shawn Lin9ddc0782021-01-15 18:01:22 +080041 struct phy phy;
42 struct clk_bulk clks;
Shawn Lin9ddc0782021-01-15 18:01:22 +080043 struct reset_ctl_bulk rsts;
44 struct gpio_desc rst_gpio;
Jon Lin014a3192023-04-27 10:35:33 +030045 u32 gen;
Shawn Lin9ddc0782021-01-15 18:01:22 +080046};
47
48/* Parameters for the waiting for iATU enabled routine */
49#define PCIE_CLIENT_GENERAL_DEBUG 0x104
50#define PCIE_CLIENT_HOT_RESET_CTRL 0x180
51#define PCIE_LTSSM_ENABLE_ENHANCE BIT(4)
52#define PCIE_CLIENT_LTSSM_STATUS 0x300
53#define SMLH_LINKUP BIT(16)
54#define RDLH_LINKUP BIT(17)
55#define PCIE_CLIENT_DBG_FIFO_MODE_CON 0x310
56#define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0 0x320
57#define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1 0x324
58#define PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0 0x328
59#define PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1 0x32c
60#define PCIE_CLIENT_DBG_FIFO_STATUS 0x350
61#define PCIE_CLIENT_DBG_TRANSITION_DATA 0xffff0000
62#define PCIE_CLIENT_DBF_EN 0xffff0003
63
Shawn Lin9ddc0782021-01-15 18:01:22 +080064/* Parameters for the waiting for #perst signal */
Anand Moon88647f02021-06-05 14:38:43 +000065#define MACRO_US 1000
Shawn Lin9ddc0782021-01-15 18:01:22 +080066
67static int rk_pcie_read(void __iomem *addr, int size, u32 *val)
68{
69 if ((uintptr_t)addr & (size - 1)) {
70 *val = 0;
Anand Moona122d3a2021-06-05 14:38:41 +000071 return -EOPNOTSUPP;
Shawn Lin9ddc0782021-01-15 18:01:22 +080072 }
73
74 if (size == 4) {
75 *val = readl(addr);
76 } else if (size == 2) {
77 *val = readw(addr);
78 } else if (size == 1) {
79 *val = readb(addr);
80 } else {
81 *val = 0;
82 return -ENODEV;
83 }
84
85 return 0;
86}
87
88static int rk_pcie_write(void __iomem *addr, int size, u32 val)
89{
90 if ((uintptr_t)addr & (size - 1))
Anand Moona122d3a2021-06-05 14:38:41 +000091 return -EOPNOTSUPP;
Shawn Lin9ddc0782021-01-15 18:01:22 +080092
93 if (size == 4)
94 writel(val, addr);
95 else if (size == 2)
96 writew(val, addr);
97 else if (size == 1)
98 writeb(val, addr);
99 else
100 return -ENODEV;
101
102 return 0;
103}
104
105static u32 __rk_pcie_read_apb(struct rk_pcie *rk_pcie, void __iomem *base,
106 u32 reg, size_t size)
107{
108 int ret;
109 u32 val;
110
111 ret = rk_pcie_read(base + reg, size, &val);
112 if (ret)
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100113 dev_err(rk_pcie->dw.dev, "Read APB address failed\n");
Shawn Lin9ddc0782021-01-15 18:01:22 +0800114
115 return val;
116}
117
118static void __rk_pcie_write_apb(struct rk_pcie *rk_pcie, void __iomem *base,
119 u32 reg, size_t size, u32 val)
120{
121 int ret;
122
123 ret = rk_pcie_write(base + reg, size, val);
124 if (ret)
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100125 dev_err(rk_pcie->dw.dev, "Write APB address failed\n");
Shawn Lin9ddc0782021-01-15 18:01:22 +0800126}
127
128/**
129 * rk_pcie_readl_apb() - Read vendor regs
130 *
131 * @rk_pcie: Pointer to the PCI controller state
132 * @reg: Offset of regs
133 */
134static inline u32 rk_pcie_readl_apb(struct rk_pcie *rk_pcie, u32 reg)
135{
136 return __rk_pcie_read_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4);
137}
138
139/**
140 * rk_pcie_writel_apb() - Write vendor regs
141 *
142 * @rk_pcie: Pointer to the PCI controller state
143 * @reg: Offset of regs
144 * @val: Value to be writen
145 */
146static inline void rk_pcie_writel_apb(struct rk_pcie *rk_pcie, u32 reg,
147 u32 val)
148{
149 __rk_pcie_write_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4, val);
150}
151
Shawn Lin9ddc0782021-01-15 18:01:22 +0800152/**
153 * rk_pcie_configure() - Configure link capabilities and speed
154 *
155 * @rk_pcie: Pointer to the PCI controller state
156 * @cap_speed: The capabilities and speed to configure
157 *
158 * Configure the link capabilities and speed in the PCIe root complex.
159 */
160static void rk_pcie_configure(struct rk_pcie *pci, u32 cap_speed)
161{
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100162 dw_pcie_dbi_write_enable(&pci->dw, true);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800163
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100164 clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CAPABILITY,
Shawn Lin9ddc0782021-01-15 18:01:22 +0800165 TARGET_LINK_SPEED_MASK, cap_speed);
166
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100167 clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CTL_2,
Shawn Lin9ddc0782021-01-15 18:01:22 +0800168 TARGET_LINK_SPEED_MASK, cap_speed);
169
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100170 dw_pcie_dbi_write_enable(&pci->dw, false);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800171}
172
173static void rk_pcie_enable_debug(struct rk_pcie *rk_pcie)
174{
175 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0,
176 PCIE_CLIENT_DBG_TRANSITION_DATA);
177 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1,
178 PCIE_CLIENT_DBG_TRANSITION_DATA);
179 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0,
180 PCIE_CLIENT_DBG_TRANSITION_DATA);
181 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1,
182 PCIE_CLIENT_DBG_TRANSITION_DATA);
183 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_MODE_CON,
184 PCIE_CLIENT_DBF_EN);
185}
186
187static void rk_pcie_debug_dump(struct rk_pcie *rk_pcie)
188{
189 u32 loop;
190
191 debug("ltssm = 0x%x\n",
192 rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS));
193 for (loop = 0; loop < 64; loop++)
194 debug("fifo_status = 0x%x\n",
195 rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_STATUS));
196}
197
198static inline void rk_pcie_link_status_clear(struct rk_pcie *rk_pcie)
199{
200 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_GENERAL_DEBUG, 0x0);
201}
202
203static inline void rk_pcie_disable_ltssm(struct rk_pcie *rk_pcie)
204{
205 rk_pcie_writel_apb(rk_pcie, 0x0, 0xc0008);
206}
207
208static inline void rk_pcie_enable_ltssm(struct rk_pcie *rk_pcie)
209{
210 rk_pcie_writel_apb(rk_pcie, 0x0, 0xc000c);
211}
212
213static int is_link_up(struct rk_pcie *priv)
214{
215 u32 val;
216
217 val = rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS);
218 if ((val & (RDLH_LINKUP | SMLH_LINKUP)) == 0x30000 &&
219 (val & GENMASK(5, 0)) == 0x11)
220 return 1;
221
222 return 0;
223}
224
225/**
226 * rk_pcie_link_up() - Wait for the link to come up
227 *
228 * @rk_pcie: Pointer to the PCI controller state
229 * @cap_speed: Desired link speed
230 *
231 * Return: 1 (true) for active line and negetive (false) for no link (timeout)
232 */
233static int rk_pcie_link_up(struct rk_pcie *priv, u32 cap_speed)
234{
235 int retries;
236
237 if (is_link_up(priv)) {
238 printf("PCI Link already up before configuration!\n");
239 return 1;
240 }
241
242 /* DW pre link configurations */
243 rk_pcie_configure(priv, cap_speed);
244
245 /* Rest the device */
246 if (dm_gpio_is_valid(&priv->rst_gpio)) {
247 dm_gpio_set_value(&priv->rst_gpio, 0);
248 /*
249 * Minimal is 100ms from spec but we see
250 * some wired devices need much more, such as 600ms.
251 * Add a enough delay to cover all cases.
252 */
Anand Moon88647f02021-06-05 14:38:43 +0000253 udelay(MACRO_US * 1000);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800254 dm_gpio_set_value(&priv->rst_gpio, 1);
255 }
256
257 rk_pcie_disable_ltssm(priv);
258 rk_pcie_link_status_clear(priv);
259 rk_pcie_enable_debug(priv);
260
261 /* Enable LTSSM */
262 rk_pcie_enable_ltssm(priv);
263
264 for (retries = 0; retries < 5; retries++) {
265 if (is_link_up(priv)) {
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100266 dev_info(priv->dw.dev, "PCIe Link up, LTSSM is 0x%x\n",
Shawn Lin9ddc0782021-01-15 18:01:22 +0800267 rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS));
268 rk_pcie_debug_dump(priv);
269 return 0;
270 }
271
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100272 dev_info(priv->dw.dev, "PCIe Linking... LTSSM is 0x%x\n",
Shawn Lin9ddc0782021-01-15 18:01:22 +0800273 rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS));
274 rk_pcie_debug_dump(priv);
Anand Moon88647f02021-06-05 14:38:43 +0000275 udelay(MACRO_US * 1000);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800276 }
277
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100278 dev_err(priv->dw.dev, "PCIe-%d Link Fail\n", dev_seq(priv->dw.dev));
Shawn Lin9ddc0782021-01-15 18:01:22 +0800279 /* Link maybe in Gen switch recovery but we need to wait more 1s */
Anand Moon88647f02021-06-05 14:38:43 +0000280 udelay(MACRO_US * 1000);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800281 return -EIO;
282}
283
284static int rockchip_pcie_init_port(struct udevice *dev)
285{
286 int ret;
287 u32 val;
288 struct rk_pcie *priv = dev_get_priv(dev);
289
290 /* Set power and maybe external ref clk input */
291 if (priv->vpcie3v3) {
292 ret = regulator_set_value(priv->vpcie3v3, 3300000);
293 if (ret) {
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100294 dev_err(priv->dw.dev, "failed to enable vpcie3v3 (ret=%d)\n",
Shawn Lin9ddc0782021-01-15 18:01:22 +0800295 ret);
296 return ret;
297 }
298 }
299
Anand Moon88647f02021-06-05 14:38:43 +0000300 udelay(MACRO_US * 1000);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800301
302 ret = generic_phy_init(&priv->phy);
303 if (ret) {
304 dev_err(dev, "failed to init phy (ret=%d)\n", ret);
305 return ret;
306 }
307
308 ret = generic_phy_power_on(&priv->phy);
309 if (ret) {
310 dev_err(dev, "failed to power on phy (ret=%d)\n", ret);
311 goto err_exit_phy;
312 }
313
314 ret = reset_deassert_bulk(&priv->rsts);
315 if (ret) {
316 dev_err(dev, "failed to deassert resets (ret=%d)\n", ret);
317 goto err_power_off_phy;
318 }
319
320 ret = clk_enable_bulk(&priv->clks);
321 if (ret) {
322 dev_err(dev, "failed to enable clks (ret=%d)\n", ret);
323 goto err_deassert_bulk;
324 }
325
326 /* LTSSM EN ctrl mode */
327 val = rk_pcie_readl_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL);
328 val |= PCIE_LTSSM_ENABLE_ENHANCE | (PCIE_LTSSM_ENABLE_ENHANCE << 16);
329 rk_pcie_writel_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL, val);
330
331 /* Set RC mode */
332 rk_pcie_writel_apb(priv, 0x0, 0xf00040);
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100333 pcie_dw_setup_host(&priv->dw);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800334
Jon Lin014a3192023-04-27 10:35:33 +0300335 ret = rk_pcie_link_up(priv, priv->gen);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800336 if (ret < 0)
337 goto err_link_up;
338
339 return 0;
340err_link_up:
341 clk_disable_bulk(&priv->clks);
342err_deassert_bulk:
343 reset_assert_bulk(&priv->rsts);
344err_power_off_phy:
345 generic_phy_power_off(&priv->phy);
346err_exit_phy:
347 generic_phy_exit(&priv->phy);
348
349 return ret;
350}
351
352static int rockchip_pcie_parse_dt(struct udevice *dev)
353{
354 struct rk_pcie *priv = dev_get_priv(dev);
355 int ret;
356
Johan Jonkere5822ec2023-03-13 01:31:49 +0100357 priv->dw.dbi_base = dev_read_addr_index_ptr(dev, 0);
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100358 if (!priv->dw.dbi_base)
Johan Jonkere5822ec2023-03-13 01:31:49 +0100359 return -EINVAL;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800360
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100361 dev_dbg(dev, "DBI address is 0x%p\n", priv->dw.dbi_base);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800362
Johan Jonkere5822ec2023-03-13 01:31:49 +0100363 priv->apb_base = dev_read_addr_index_ptr(dev, 1);
Shawn Lin9ddc0782021-01-15 18:01:22 +0800364 if (!priv->apb_base)
Johan Jonkere5822ec2023-03-13 01:31:49 +0100365 return -EINVAL;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800366
367 dev_dbg(dev, "APB address is 0x%p\n", priv->apb_base);
368
369 ret = gpio_request_by_name(dev, "reset-gpios", 0,
370 &priv->rst_gpio, GPIOD_IS_OUT);
371 if (ret) {
372 dev_err(dev, "failed to find reset-gpios property\n");
373 return ret;
374 }
375
376 ret = reset_get_bulk(dev, &priv->rsts);
377 if (ret) {
378 dev_err(dev, "Can't get reset: %d\n", ret);
Eugen Hristeve04b67a2023-04-13 17:11:03 +0300379 goto rockchip_pcie_parse_dt_err_reset_get_bulk;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800380 }
381
382 ret = clk_get_bulk(dev, &priv->clks);
383 if (ret) {
384 dev_err(dev, "Can't get clock: %d\n", ret);
Eugen Hristeve04b67a2023-04-13 17:11:03 +0300385 goto rockchip_pcie_parse_dt_err_clk_get_bulk;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800386 }
387
388 ret = device_get_supply_regulator(dev, "vpcie3v3-supply",
389 &priv->vpcie3v3);
390 if (ret && ret != -ENOENT) {
391 dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret);
Eugen Hristeve04b67a2023-04-13 17:11:03 +0300392 goto rockchip_pcie_parse_dt_err_supply_regulator;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800393 }
394
395 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
396 if (ret) {
397 dev_err(dev, "failed to get pcie phy (ret=%d)\n", ret);
Eugen Hristeve04b67a2023-04-13 17:11:03 +0300398 goto rockchip_pcie_parse_dt_err_phy_get_by_index;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800399 }
400
Jon Lin014a3192023-04-27 10:35:33 +0300401 priv->gen = dev_read_u32_default(dev, "max-link-speed",
402 LINK_SPEED_GEN_3);
403
Shawn Lin9ddc0782021-01-15 18:01:22 +0800404 return 0;
Eugen Hristeve04b67a2023-04-13 17:11:03 +0300405
406rockchip_pcie_parse_dt_err_phy_get_by_index:
407 /* regulators don't need release */
408rockchip_pcie_parse_dt_err_supply_regulator:
409 clk_release_bulk(&priv->clks);
410rockchip_pcie_parse_dt_err_clk_get_bulk:
411 reset_release_bulk(&priv->rsts);
412rockchip_pcie_parse_dt_err_reset_get_bulk:
413 dm_gpio_free(dev, &priv->rst_gpio);
414 return ret;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800415}
416
417/**
418 * rockchip_pcie_probe() - Probe the PCIe bus for active link
419 *
420 * @dev: A pointer to the device being operated on
421 *
422 * Probe for an active link on the PCIe bus and configure the controller
423 * to enable this port.
424 *
425 * Return: 0 on success, else -ENODEV
426 */
427static int rockchip_pcie_probe(struct udevice *dev)
428{
429 struct rk_pcie *priv = dev_get_priv(dev);
430 struct udevice *ctlr = pci_get_controller(dev);
431 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100432 int ret = 0;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800433
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100434 priv->dw.first_busno = dev_seq(dev);
435 priv->dw.dev = dev;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800436
437 ret = rockchip_pcie_parse_dt(dev);
438 if (ret)
439 return ret;
440
441 ret = rockchip_pcie_init_port(dev);
442 if (ret)
Eugen Hristeve04b67a2023-04-13 17:11:03 +0300443 goto rockchip_pcie_probe_err_init_port;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800444
445 dev_info(dev, "PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n",
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100446 dev_seq(dev), pcie_dw_get_link_speed(&priv->dw),
447 pcie_dw_get_link_width(&priv->dw),
Shawn Lin9ddc0782021-01-15 18:01:22 +0800448 hose->first_busno);
449
Shawn Lin9ddc0782021-01-15 18:01:22 +0800450
Eugen Hristeve04b67a2023-04-13 17:11:03 +0300451 ret = pcie_dw_prog_outbound_atu_unroll(&priv->dw,
452 PCIE_ATU_REGION_INDEX0,
453 PCIE_ATU_TYPE_MEM,
454 priv->dw.mem.phys_start,
455 priv->dw.mem.bus_start,
456 priv->dw.mem.size);
457 if (!ret)
458 return ret;
459
460rockchip_pcie_probe_err_init_port:
461 clk_release_bulk(&priv->clks);
462 reset_release_bulk(&priv->rsts);
463 dm_gpio_free(dev, &priv->rst_gpio);
464
465 return ret;
Shawn Lin9ddc0782021-01-15 18:01:22 +0800466}
467
468static const struct dm_pci_ops rockchip_pcie_ops = {
Neil Armstrongc90f3d02021-03-25 15:49:20 +0100469 .read_config = pcie_dw_read_config,
470 .write_config = pcie_dw_write_config,
Shawn Lin9ddc0782021-01-15 18:01:22 +0800471};
472
473static const struct udevice_id rockchip_pcie_ids[] = {
474 { .compatible = "rockchip,rk3568-pcie" },
Jon Lin53744802023-04-27 10:35:32 +0300475 { .compatible = "rockchip,rk3588-pcie" },
Shawn Lin9ddc0782021-01-15 18:01:22 +0800476 { }
477};
478
479U_BOOT_DRIVER(rockchip_dw_pcie) = {
480 .name = "pcie_dw_rockchip",
481 .id = UCLASS_PCI,
482 .of_match = rockchip_pcie_ids,
483 .ops = &rockchip_pcie_ops,
484 .probe = rockchip_pcie_probe,
485 .priv_auto = sizeof(struct rk_pcie),
486};