blob: 19f9e58a640dca9c3f0c1cf4e605fd122bdd6a41 [file] [log] [blame]
Jagan Teki02262472020-05-09 22:26:21 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Rockchip AXI PCIe host controller driver
4 *
5 * Copyright (c) 2016 Rockchip, Inc.
6 * Copyright (c) 2020 Amarula Solutions(India)
7 * Copyright (c) 2020 Jagan Teki <jagan@amarulasolutions.com>
8 * Copyright (c) 2019 Patrick Wildt <patrick@blueri.se>
9 * Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org>
10 *
11 * Bits taken from Linux Rockchip PCIe host controller.
12 */
13
Jagan Teki02262472020-05-09 22:26:21 +053014#include <dm.h>
15#include <dm/device_compat.h>
Jagan Teki0400f5e2020-07-09 23:41:03 +053016#include <generic-phy.h>
Jagan Teki02262472020-05-09 22:26:21 +053017#include <pci.h>
Jagan Teki02262472020-05-09 22:26:21 +053018#include <power/regulator.h>
19#include <reset.h>
Jagan Teki02262472020-05-09 22:26:21 +053020#include <asm-generic/gpio.h>
Jagan Teki02262472020-05-09 22:26:21 +053021#include <linux/iopoll.h>
22
Jagan Teki0400f5e2020-07-09 23:41:03 +053023#define HIWORD_UPDATE(mask, val) (((mask) << 16) | (val))
24#define HIWORD_UPDATE_BIT(val) HIWORD_UPDATE(val, val)
25
26#define ENCODE_LANES(x) ((((x) >> 1) & 3) << 4)
27#define PCIE_CLIENT_BASE 0x0
28#define PCIE_CLIENT_CONFIG (PCIE_CLIENT_BASE + 0x00)
29#define PCIE_CLIENT_CONF_ENABLE HIWORD_UPDATE_BIT(0x0001)
30#define PCIE_CLIENT_LINK_TRAIN_ENABLE HIWORD_UPDATE_BIT(0x0002)
31#define PCIE_CLIENT_MODE_RC HIWORD_UPDATE_BIT(0x0040)
32#define PCIE_CLIENT_GEN_SEL_1 HIWORD_UPDATE(0x0080, 0)
33#define PCIE_CLIENT_BASIC_STATUS1 0x0048
34#define PCIE_CLIENT_LINK_STATUS_UP GENMASK(21, 20)
35#define PCIE_CLIENT_LINK_STATUS_MASK GENMASK(21, 20)
36#define PCIE_LINK_UP(x) \
37 (((x) & PCIE_CLIENT_LINK_STATUS_MASK) == PCIE_CLIENT_LINK_STATUS_UP)
38#define PCIE_RC_NORMAL_BASE 0x800000
39#define PCIE_LM_BASE 0x900000
40#define PCIE_LM_VENDOR_ID (PCIE_LM_BASE + 0x44)
41#define PCIE_LM_VENDOR_ROCKCHIP 0x1d87
42#define PCIE_LM_RCBAR (PCIE_LM_BASE + 0x300)
43#define PCIE_LM_RCBARPIE BIT(19)
44#define PCIE_LM_RCBARPIS BIT(20)
45#define PCIE_RC_BASE 0xa00000
46#define PCIE_RC_CONFIG_DCR (PCIE_RC_BASE + 0x0c4)
47#define PCIE_RC_CONFIG_DCR_CSPL_SHIFT 18
48#define PCIE_RC_CONFIG_DCR_CPLS_SHIFT 26
49#define PCIE_RC_PCIE_LCAP (PCIE_RC_BASE + 0x0cc)
50#define PCIE_RC_PCIE_LCAP_APMS_L0S BIT(10)
51#define PCIE_ATR_BASE 0xc00000
52#define PCIE_ATR_OB_ADDR0(i) (PCIE_ATR_BASE + 0x000 + (i) * 0x20)
53#define PCIE_ATR_OB_ADDR1(i) (PCIE_ATR_BASE + 0x004 + (i) * 0x20)
54#define PCIE_ATR_OB_DESC0(i) (PCIE_ATR_BASE + 0x008 + (i) * 0x20)
55#define PCIE_ATR_OB_DESC1(i) (PCIE_ATR_BASE + 0x00c + (i) * 0x20)
56#define PCIE_ATR_IB_ADDR0(i) (PCIE_ATR_BASE + 0x800 + (i) * 0x8)
57#define PCIE_ATR_IB_ADDR1(i) (PCIE_ATR_BASE + 0x804 + (i) * 0x8)
58#define PCIE_ATR_HDR_MEM 0x2
59#define PCIE_ATR_HDR_IO 0x6
60#define PCIE_ATR_HDR_CFG_TYPE0 0xa
61#define PCIE_ATR_HDR_CFG_TYPE1 0xb
62#define PCIE_ATR_HDR_RID BIT(23)
63
64#define PCIE_ATR_OB_REGION0_SIZE (32 * 1024 * 1024)
65#define PCIE_ATR_OB_REGION_SIZE (1 * 1024 * 1024)
66
67struct rockchip_pcie {
68 fdt_addr_t axi_base;
69 fdt_addr_t apb_base;
70 int first_busno;
71 struct udevice *dev;
72
73 /* resets */
74 struct reset_ctl core_rst;
75 struct reset_ctl mgmt_rst;
76 struct reset_ctl mgmt_sticky_rst;
77 struct reset_ctl pipe_rst;
78 struct reset_ctl pm_rst;
79 struct reset_ctl pclk_rst;
80 struct reset_ctl aclk_rst;
81
82 /* gpio */
83 struct gpio_desc ep_gpio;
84
85 /* vpcie regulators */
86 struct udevice *vpcie12v;
87 struct udevice *vpcie3v3;
88 struct udevice *vpcie1v8;
89 struct udevice *vpcie0v9;
90
91 /* phy */
92 struct phy pcie_phy;
93};
94
Jagan Teki02262472020-05-09 22:26:21 +053095static int rockchip_pcie_rd_conf(const struct udevice *udev, pci_dev_t bdf,
96 uint offset, ulong *valuep,
97 enum pci_size_t size)
98{
99 struct rockchip_pcie *priv = dev_get_priv(udev);
100 unsigned int bus = PCI_BUS(bdf);
101 unsigned int dev = PCI_DEV(bdf);
Pali Rohár23769352021-11-03 01:01:05 +0100102 int where = PCIE_ECAM_OFFSET(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset & ~0x3);
Jagan Teki02262472020-05-09 22:26:21 +0530103 ulong value;
104
105 if (bus == priv->first_busno && dev == 0) {
106 value = readl(priv->apb_base + PCIE_RC_NORMAL_BASE + where);
107 *valuep = pci_conv_32_to_size(value, offset, size);
108 return 0;
109 }
110
111 if ((bus == priv->first_busno + 1) && dev == 0) {
112 value = readl(priv->axi_base + where);
113 *valuep = pci_conv_32_to_size(value, offset, size);
114 return 0;
115 }
116
117 *valuep = pci_get_ff(size);
118
119 return 0;
120}
121
122static int rockchip_pcie_wr_conf(struct udevice *udev, pci_dev_t bdf,
123 uint offset, ulong value,
124 enum pci_size_t size)
125{
126 struct rockchip_pcie *priv = dev_get_priv(udev);
127 unsigned int bus = PCI_BUS(bdf);
128 unsigned int dev = PCI_DEV(bdf);
Pali Rohár23769352021-11-03 01:01:05 +0100129 int where = PCIE_ECAM_OFFSET(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset & ~0x3);
Jagan Teki02262472020-05-09 22:26:21 +0530130 ulong old;
131
132 if (bus == priv->first_busno && dev == 0) {
133 old = readl(priv->apb_base + PCIE_RC_NORMAL_BASE + where);
134 value = pci_conv_size_to_32(old, value, offset, size);
135 writel(value, priv->apb_base + PCIE_RC_NORMAL_BASE + where);
136 return 0;
137 }
138
139 if ((bus == priv->first_busno + 1) && dev == 0) {
140 old = readl(priv->axi_base + where);
141 value = pci_conv_size_to_32(old, value, offset, size);
142 writel(value, priv->axi_base + where);
143 return 0;
144 }
145
146 return 0;
147}
148
149static int rockchip_pcie_atr_init(struct rockchip_pcie *priv)
150{
151 struct udevice *ctlr = pci_get_controller(priv->dev);
152 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
153 u64 addr, size, offset;
154 u32 type;
155 int i, region;
156
157 /* Use region 0 to map PCI configuration space. */
158 writel(25 - 1, priv->apb_base + PCIE_ATR_OB_ADDR0(0));
159 writel(0, priv->apb_base + PCIE_ATR_OB_ADDR1(0));
160 writel(PCIE_ATR_HDR_CFG_TYPE0 | PCIE_ATR_HDR_RID,
161 priv->apb_base + PCIE_ATR_OB_DESC0(0));
162 writel(0, priv->apb_base + PCIE_ATR_OB_DESC1(0));
163
164 for (i = 0; i < hose->region_count; i++) {
165 if (hose->regions[i].flags == PCI_REGION_SYS_MEMORY)
166 continue;
167
168 if (hose->regions[i].flags == PCI_REGION_IO)
169 type = PCIE_ATR_HDR_IO;
170 else
171 type = PCIE_ATR_HDR_MEM;
172
173 /* Only support identity mappings. */
174 if (hose->regions[i].bus_start !=
175 hose->regions[i].phys_start)
176 return -EINVAL;
177
178 /* Only support mappings aligned on a region boundary. */
179 addr = hose->regions[i].bus_start;
180 if (addr & (PCIE_ATR_OB_REGION_SIZE - 1))
181 return -EINVAL;
182
183 /* Mappings should lie between AXI and APB regions. */
184 size = hose->regions[i].size;
185 if (addr < (u64)priv->axi_base + PCIE_ATR_OB_REGION0_SIZE)
186 return -EINVAL;
187 if (addr + size > (u64)priv->apb_base)
188 return -EINVAL;
189
190 offset = addr - (u64)priv->axi_base - PCIE_ATR_OB_REGION0_SIZE;
191 region = 1 + (offset / PCIE_ATR_OB_REGION_SIZE);
192 while (size > 0) {
193 writel(32 - 1,
194 priv->apb_base + PCIE_ATR_OB_ADDR0(region));
195 writel(0, priv->apb_base + PCIE_ATR_OB_ADDR1(region));
196 writel(type | PCIE_ATR_HDR_RID,
197 priv->apb_base + PCIE_ATR_OB_DESC0(region));
198 writel(0, priv->apb_base + PCIE_ATR_OB_DESC1(region));
199
200 addr += PCIE_ATR_OB_REGION_SIZE;
201 size -= PCIE_ATR_OB_REGION_SIZE;
202 region++;
203 }
204 }
205
206 /* Passthrough inbound translations unmodified. */
207 writel(32 - 1, priv->apb_base + PCIE_ATR_IB_ADDR0(2));
208 writel(0, priv->apb_base + PCIE_ATR_IB_ADDR1(2));
209
210 return 0;
211}
212
213static int rockchip_pcie_init_port(struct udevice *dev)
214{
215 struct rockchip_pcie *priv = dev_get_priv(dev);
216 u32 cr, val, status;
217 int ret;
218
219 if (dm_gpio_is_valid(&priv->ep_gpio))
220 dm_gpio_set_value(&priv->ep_gpio, 0);
221
222 ret = reset_assert(&priv->aclk_rst);
223 if (ret) {
224 dev_err(dev, "failed to assert aclk reset (ret=%d)\n", ret);
225 return ret;
226 }
227
228 ret = reset_assert(&priv->pclk_rst);
229 if (ret) {
230 dev_err(dev, "failed to assert pclk reset (ret=%d)\n", ret);
231 return ret;
232 }
233
234 ret = reset_assert(&priv->pm_rst);
235 if (ret) {
236 dev_err(dev, "failed to assert pm reset (ret=%d)\n", ret);
237 return ret;
238 }
239
Jagan Teki427603b2020-07-09 23:41:02 +0530240 ret = generic_phy_init(&priv->pcie_phy);
Jagan Teki5f142322020-05-09 22:26:22 +0530241 if (ret) {
242 dev_err(dev, "failed to init phy (ret=%d)\n", ret);
243 goto err_exit_phy;
244 }
245
Jagan Teki02262472020-05-09 22:26:21 +0530246 ret = reset_assert(&priv->core_rst);
247 if (ret) {
248 dev_err(dev, "failed to assert core reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530249 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530250 }
251
252 ret = reset_assert(&priv->mgmt_rst);
253 if (ret) {
254 dev_err(dev, "failed to assert mgmt reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530255 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530256 }
257
258 ret = reset_assert(&priv->mgmt_sticky_rst);
259 if (ret) {
260 dev_err(dev, "failed to assert mgmt-sticky reset (ret=%d)\n",
261 ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530262 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530263 }
264
265 ret = reset_assert(&priv->pipe_rst);
266 if (ret) {
267 dev_err(dev, "failed to assert pipe reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530268 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530269 }
270
271 udelay(10);
272
273 ret = reset_deassert(&priv->pm_rst);
274 if (ret) {
275 dev_err(dev, "failed to deassert pm reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530276 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530277 }
278
279 ret = reset_deassert(&priv->aclk_rst);
280 if (ret) {
281 dev_err(dev, "failed to deassert aclk reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530282 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530283 }
284
285 ret = reset_deassert(&priv->pclk_rst);
286 if (ret) {
287 dev_err(dev, "failed to deassert pclk reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530288 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530289 }
290
291 /* Select GEN1 for now */
292 cr = PCIE_CLIENT_GEN_SEL_1;
293 /* Set Root complex mode */
294 cr |= PCIE_CLIENT_CONF_ENABLE | PCIE_CLIENT_MODE_RC;
295 writel(cr, priv->apb_base + PCIE_CLIENT_CONFIG);
296
Jagan Teki427603b2020-07-09 23:41:02 +0530297 ret = generic_phy_power_on(&priv->pcie_phy);
Jagan Teki5f142322020-05-09 22:26:22 +0530298 if (ret) {
299 dev_err(dev, "failed to power on phy (ret=%d)\n", ret);
300 goto err_power_off_phy;
301 }
302
Jagan Teki02262472020-05-09 22:26:21 +0530303 ret = reset_deassert(&priv->mgmt_sticky_rst);
304 if (ret) {
305 dev_err(dev, "failed to deassert mgmt-sticky reset (ret=%d)\n",
306 ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530307 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530308 }
309
310 ret = reset_deassert(&priv->core_rst);
311 if (ret) {
312 dev_err(dev, "failed to deassert core reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530313 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530314 }
315
316 ret = reset_deassert(&priv->mgmt_rst);
317 if (ret) {
318 dev_err(dev, "failed to deassert mgmt reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530319 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530320 }
321
322 ret = reset_deassert(&priv->pipe_rst);
323 if (ret) {
324 dev_err(dev, "failed to deassert pipe reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530325 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530326 }
327
328 /* Enable Gen1 training */
329 writel(PCIE_CLIENT_LINK_TRAIN_ENABLE,
330 priv->apb_base + PCIE_CLIENT_CONFIG);
331
332 if (dm_gpio_is_valid(&priv->ep_gpio))
333 dm_gpio_set_value(&priv->ep_gpio, 1);
334
335 ret = readl_poll_sleep_timeout
336 (priv->apb_base + PCIE_CLIENT_BASIC_STATUS1,
337 status, PCIE_LINK_UP(status), 20, 500 * 1000);
338 if (ret) {
339 dev_err(dev, "PCIe link training gen1 timeout!\n");
Jagan Teki5f142322020-05-09 22:26:22 +0530340 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530341 }
342
343 /* Initialize Root Complex registers. */
344 writel(PCIE_LM_VENDOR_ROCKCHIP, priv->apb_base + PCIE_LM_VENDOR_ID);
Pali Rohár25781e22022-02-18 13:18:40 +0100345 writel(PCI_CLASS_BRIDGE_PCI_NORMAL << 8,
Jagan Teki02262472020-05-09 22:26:21 +0530346 priv->apb_base + PCIE_RC_BASE + PCI_CLASS_REVISION);
347 writel(PCIE_LM_RCBARPIE | PCIE_LM_RCBARPIS,
348 priv->apb_base + PCIE_LM_RCBAR);
349
350 if (dev_read_bool(dev, "aspm-no-l0s")) {
351 val = readl(priv->apb_base + PCIE_RC_PCIE_LCAP);
352 val &= ~PCIE_RC_PCIE_LCAP_APMS_L0S;
353 writel(val, priv->apb_base + PCIE_RC_PCIE_LCAP);
354 }
355
356 /* Configure Address Translation. */
357 ret = rockchip_pcie_atr_init(priv);
358 if (ret) {
Simon Glass75e534b2020-12-16 21:20:07 -0700359 dev_err(dev, "PCIE-%d: ATR init failed\n", dev_seq(dev));
Jagan Teki5f142322020-05-09 22:26:22 +0530360 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530361 }
362
363 return 0;
Jagan Teki5f142322020-05-09 22:26:22 +0530364
365err_power_off_phy:
Jagan Teki427603b2020-07-09 23:41:02 +0530366 generic_phy_power_off(&priv->pcie_phy);
Jagan Teki5f142322020-05-09 22:26:22 +0530367err_exit_phy:
Jagan Teki427603b2020-07-09 23:41:02 +0530368 generic_phy_exit(&priv->pcie_phy);
Jagan Teki5f142322020-05-09 22:26:22 +0530369 return ret;
Jagan Teki02262472020-05-09 22:26:21 +0530370}
371
372static int rockchip_pcie_set_vpcie(struct udevice *dev)
373{
374 struct rockchip_pcie *priv = dev_get_priv(dev);
375 int ret;
376
Jonas Karlman97b82162023-07-11 23:13:56 +0000377 ret = regulator_set_enable_if_allowed(priv->vpcie12v, true);
378 if (ret && ret != -ENOSYS) {
379 dev_err(dev, "failed to enable vpcie12v (ret=%d)\n", ret);
380 return ret;
Jagan Teki02262472020-05-09 22:26:21 +0530381 }
382
Jonas Karlman97b82162023-07-11 23:13:56 +0000383 ret = regulator_set_enable_if_allowed(priv->vpcie3v3, true);
384 if (ret && ret != -ENOSYS) {
385 dev_err(dev, "failed to enable vpcie3v3 (ret=%d)\n", ret);
386 goto err_disable_12v;
Jagan Teki02262472020-05-09 22:26:21 +0530387 }
388
Jonas Karlman97b82162023-07-11 23:13:56 +0000389 ret = regulator_set_enable_if_allowed(priv->vpcie1v8, true);
390 if (ret && ret != -ENOSYS) {
391 dev_err(dev, "failed to enable vpcie1v8 (ret=%d)\n", ret);
392 goto err_disable_3v3;
393 }
394
395 ret = regulator_set_enable_if_allowed(priv->vpcie0v9, true);
396 if (ret && ret != -ENOSYS) {
397 dev_err(dev, "failed to enable vpcie0v9 (ret=%d)\n", ret);
398 goto err_disable_1v8;
Jagan Teki02262472020-05-09 22:26:21 +0530399 }
400
401 return 0;
402
403err_disable_1v8:
Jonas Karlman97b82162023-07-11 23:13:56 +0000404 regulator_set_enable_if_allowed(priv->vpcie1v8, false);
Jagan Teki02262472020-05-09 22:26:21 +0530405err_disable_3v3:
Jonas Karlman97b82162023-07-11 23:13:56 +0000406 regulator_set_enable_if_allowed(priv->vpcie3v3, false);
407err_disable_12v:
408 regulator_set_enable_if_allowed(priv->vpcie12v, false);
Jagan Teki02262472020-05-09 22:26:21 +0530409 return ret;
410}
411
412static int rockchip_pcie_parse_dt(struct udevice *dev)
413{
414 struct rockchip_pcie *priv = dev_get_priv(dev);
415 int ret;
416
417 priv->axi_base = dev_read_addr_name(dev, "axi-base");
Jonas Karlman97b82162023-07-11 23:13:56 +0000418 if (priv->axi_base == FDT_ADDR_T_NONE)
419 return -EINVAL;
Jagan Teki02262472020-05-09 22:26:21 +0530420
421 priv->apb_base = dev_read_addr_name(dev, "apb-base");
Jonas Karlman97b82162023-07-11 23:13:56 +0000422 if (priv->apb_base == FDT_ADDR_T_NONE)
423 return -EINVAL;
Jagan Teki02262472020-05-09 22:26:21 +0530424
425 ret = reset_get_by_name(dev, "core", &priv->core_rst);
426 if (ret) {
427 dev_err(dev, "failed to get core reset (ret=%d)\n", ret);
428 return ret;
429 }
430
431 ret = reset_get_by_name(dev, "mgmt", &priv->mgmt_rst);
432 if (ret) {
433 dev_err(dev, "failed to get mgmt reset (ret=%d)\n", ret);
434 return ret;
435 }
436
437 ret = reset_get_by_name(dev, "mgmt-sticky", &priv->mgmt_sticky_rst);
438 if (ret) {
439 dev_err(dev, "failed to get mgmt-sticky reset (ret=%d)\n", ret);
440 return ret;
441 }
442
443 ret = reset_get_by_name(dev, "pipe", &priv->pipe_rst);
444 if (ret) {
445 dev_err(dev, "failed to get pipe reset (ret=%d)\n", ret);
446 return ret;
447 }
448
449 ret = reset_get_by_name(dev, "pm", &priv->pm_rst);
450 if (ret) {
451 dev_err(dev, "failed to get pm reset (ret=%d)\n", ret);
452 return ret;
453 }
454
455 ret = reset_get_by_name(dev, "pclk", &priv->pclk_rst);
456 if (ret) {
457 dev_err(dev, "failed to get pclk reset (ret=%d)\n", ret);
458 return ret;
459 }
460
461 ret = reset_get_by_name(dev, "aclk", &priv->aclk_rst);
462 if (ret) {
463 dev_err(dev, "failed to get aclk reset (ret=%d)\n", ret);
464 return ret;
465 }
466
Jonas Karlman97b82162023-07-11 23:13:56 +0000467 ret = device_get_supply_regulator(dev, "vpcie12v-supply",
468 &priv->vpcie12v);
469 if (ret && ret != -ENOENT) {
470 dev_err(dev, "failed to get vpcie12v supply (ret=%d)\n", ret);
471 return ret;
472 }
473
Jagan Teki02262472020-05-09 22:26:21 +0530474 ret = device_get_supply_regulator(dev, "vpcie3v3-supply",
475 &priv->vpcie3v3);
476 if (ret && ret != -ENOENT) {
477 dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret);
478 return ret;
479 }
480
481 ret = device_get_supply_regulator(dev, "vpcie1v8-supply",
482 &priv->vpcie1v8);
Mark Kettenis15b40062020-05-24 22:32:51 +0200483 if (ret && ret != -ENOENT) {
Jagan Teki02262472020-05-09 22:26:21 +0530484 dev_err(dev, "failed to get vpcie1v8 supply (ret=%d)\n", ret);
485 return ret;
486 }
487
488 ret = device_get_supply_regulator(dev, "vpcie0v9-supply",
489 &priv->vpcie0v9);
Mark Kettenis15b40062020-05-24 22:32:51 +0200490 if (ret && ret != -ENOENT) {
Jagan Teki02262472020-05-09 22:26:21 +0530491 dev_err(dev, "failed to get vpcie0v9 supply (ret=%d)\n", ret);
492 return ret;
493 }
494
Jagan Teki427603b2020-07-09 23:41:02 +0530495 ret = generic_phy_get_by_index(dev, 0, &priv->pcie_phy);
496 if (ret) {
497 dev_err(dev, "failed to get pcie-phy (ret=%d)\n", ret);
498 return ret;
499 }
500
Jonas Karlman97b82162023-07-11 23:13:56 +0000501 ret = gpio_request_by_name(dev, "ep-gpios", 0,
502 &priv->ep_gpio, GPIOD_IS_OUT);
503 if (ret) {
504 dev_err(dev, "failed to find ep-gpios property\n");
505 return ret;
506 }
507
Jagan Teki02262472020-05-09 22:26:21 +0530508 return 0;
509}
510
511static int rockchip_pcie_probe(struct udevice *dev)
512{
513 struct rockchip_pcie *priv = dev_get_priv(dev);
514 struct udevice *ctlr = pci_get_controller(dev);
515 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
516 int ret;
517
Simon Glass75e534b2020-12-16 21:20:07 -0700518 priv->first_busno = dev_seq(dev);
Jagan Teki02262472020-05-09 22:26:21 +0530519 priv->dev = dev;
520
521 ret = rockchip_pcie_parse_dt(dev);
522 if (ret)
523 return ret;
524
Jagan Teki02262472020-05-09 22:26:21 +0530525 ret = rockchip_pcie_set_vpcie(dev);
526 if (ret)
Jonas Karlman97b82162023-07-11 23:13:56 +0000527 goto err_gpio_free;
Jagan Teki02262472020-05-09 22:26:21 +0530528
529 ret = rockchip_pcie_init_port(dev);
530 if (ret)
Jonas Karlman97b82162023-07-11 23:13:56 +0000531 goto err_disable_vpcie;
Jagan Teki02262472020-05-09 22:26:21 +0530532
533 dev_info(dev, "PCIE-%d: Link up (Bus%d)\n",
Simon Glass75e534b2020-12-16 21:20:07 -0700534 dev_seq(dev), hose->first_busno);
Jagan Teki02262472020-05-09 22:26:21 +0530535
536 return 0;
Jonas Karlman97b82162023-07-11 23:13:56 +0000537
538err_disable_vpcie:
539 regulator_set_enable_if_allowed(priv->vpcie0v9, false);
540 regulator_set_enable_if_allowed(priv->vpcie1v8, false);
541 regulator_set_enable_if_allowed(priv->vpcie3v3, false);
542 regulator_set_enable_if_allowed(priv->vpcie12v, false);
543err_gpio_free:
544 if (dm_gpio_is_valid(&priv->ep_gpio))
545 dm_gpio_free(dev, &priv->ep_gpio);
546 return ret;
Jagan Teki02262472020-05-09 22:26:21 +0530547}
548
549static const struct dm_pci_ops rockchip_pcie_ops = {
550 .read_config = rockchip_pcie_rd_conf,
551 .write_config = rockchip_pcie_wr_conf,
552};
553
554static const struct udevice_id rockchip_pcie_ids[] = {
555 { .compatible = "rockchip,rk3399-pcie" },
556 { }
557};
558
559U_BOOT_DRIVER(rockchip_pcie) = {
Jonas Karlman97b82162023-07-11 23:13:56 +0000560 .name = "rockchip_pcie",
561 .id = UCLASS_PCI,
562 .of_match = rockchip_pcie_ids,
563 .ops = &rockchip_pcie_ops,
564 .probe = rockchip_pcie_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700565 .priv_auto = sizeof(struct rockchip_pcie),
Jagan Teki02262472020-05-09 22:26:21 +0530566};