blob: 0edc2464a8913e7cf8c2677b1667937538b3264e [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
14#include <common.h>
15#include <clk.h>
16#include <dm.h>
17#include <dm/device_compat.h>
18#include <pci.h>
19#include <power-domain.h>
20#include <power/regulator.h>
21#include <reset.h>
22#include <syscon.h>
23#include <asm/io.h>
24#include <asm-generic/gpio.h>
25#include <asm/arch-rockchip/clock.h>
26#include <linux/iopoll.h>
27
28#include "pcie_rockchip.h"
29
30DECLARE_GLOBAL_DATA_PTR;
31
32static int rockchip_pcie_off_conf(pci_dev_t bdf, uint offset)
33{
34 unsigned int bus = PCI_BUS(bdf);
35 unsigned int dev = PCI_DEV(bdf);
36 unsigned int func = PCI_FUNC(bdf);
37
38 return (bus << 20) | (dev << 15) | (func << 12) | (offset & ~0x3);
39}
40
41static int rockchip_pcie_rd_conf(const struct udevice *udev, pci_dev_t bdf,
42 uint offset, ulong *valuep,
43 enum pci_size_t size)
44{
45 struct rockchip_pcie *priv = dev_get_priv(udev);
46 unsigned int bus = PCI_BUS(bdf);
47 unsigned int dev = PCI_DEV(bdf);
48 int where = rockchip_pcie_off_conf(bdf, offset);
49 ulong value;
50
51 if (bus == priv->first_busno && dev == 0) {
52 value = readl(priv->apb_base + PCIE_RC_NORMAL_BASE + where);
53 *valuep = pci_conv_32_to_size(value, offset, size);
54 return 0;
55 }
56
57 if ((bus == priv->first_busno + 1) && dev == 0) {
58 value = readl(priv->axi_base + where);
59 *valuep = pci_conv_32_to_size(value, offset, size);
60 return 0;
61 }
62
63 *valuep = pci_get_ff(size);
64
65 return 0;
66}
67
68static int rockchip_pcie_wr_conf(struct udevice *udev, pci_dev_t bdf,
69 uint offset, ulong value,
70 enum pci_size_t size)
71{
72 struct rockchip_pcie *priv = dev_get_priv(udev);
73 unsigned int bus = PCI_BUS(bdf);
74 unsigned int dev = PCI_DEV(bdf);
75 int where = rockchip_pcie_off_conf(bdf, offset);
76 ulong old;
77
78 if (bus == priv->first_busno && dev == 0) {
79 old = readl(priv->apb_base + PCIE_RC_NORMAL_BASE + where);
80 value = pci_conv_size_to_32(old, value, offset, size);
81 writel(value, priv->apb_base + PCIE_RC_NORMAL_BASE + where);
82 return 0;
83 }
84
85 if ((bus == priv->first_busno + 1) && dev == 0) {
86 old = readl(priv->axi_base + where);
87 value = pci_conv_size_to_32(old, value, offset, size);
88 writel(value, priv->axi_base + where);
89 return 0;
90 }
91
92 return 0;
93}
94
95static int rockchip_pcie_atr_init(struct rockchip_pcie *priv)
96{
97 struct udevice *ctlr = pci_get_controller(priv->dev);
98 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
99 u64 addr, size, offset;
100 u32 type;
101 int i, region;
102
103 /* Use region 0 to map PCI configuration space. */
104 writel(25 - 1, priv->apb_base + PCIE_ATR_OB_ADDR0(0));
105 writel(0, priv->apb_base + PCIE_ATR_OB_ADDR1(0));
106 writel(PCIE_ATR_HDR_CFG_TYPE0 | PCIE_ATR_HDR_RID,
107 priv->apb_base + PCIE_ATR_OB_DESC0(0));
108 writel(0, priv->apb_base + PCIE_ATR_OB_DESC1(0));
109
110 for (i = 0; i < hose->region_count; i++) {
111 if (hose->regions[i].flags == PCI_REGION_SYS_MEMORY)
112 continue;
113
114 if (hose->regions[i].flags == PCI_REGION_IO)
115 type = PCIE_ATR_HDR_IO;
116 else
117 type = PCIE_ATR_HDR_MEM;
118
119 /* Only support identity mappings. */
120 if (hose->regions[i].bus_start !=
121 hose->regions[i].phys_start)
122 return -EINVAL;
123
124 /* Only support mappings aligned on a region boundary. */
125 addr = hose->regions[i].bus_start;
126 if (addr & (PCIE_ATR_OB_REGION_SIZE - 1))
127 return -EINVAL;
128
129 /* Mappings should lie between AXI and APB regions. */
130 size = hose->regions[i].size;
131 if (addr < (u64)priv->axi_base + PCIE_ATR_OB_REGION0_SIZE)
132 return -EINVAL;
133 if (addr + size > (u64)priv->apb_base)
134 return -EINVAL;
135
136 offset = addr - (u64)priv->axi_base - PCIE_ATR_OB_REGION0_SIZE;
137 region = 1 + (offset / PCIE_ATR_OB_REGION_SIZE);
138 while (size > 0) {
139 writel(32 - 1,
140 priv->apb_base + PCIE_ATR_OB_ADDR0(region));
141 writel(0, priv->apb_base + PCIE_ATR_OB_ADDR1(region));
142 writel(type | PCIE_ATR_HDR_RID,
143 priv->apb_base + PCIE_ATR_OB_DESC0(region));
144 writel(0, priv->apb_base + PCIE_ATR_OB_DESC1(region));
145
146 addr += PCIE_ATR_OB_REGION_SIZE;
147 size -= PCIE_ATR_OB_REGION_SIZE;
148 region++;
149 }
150 }
151
152 /* Passthrough inbound translations unmodified. */
153 writel(32 - 1, priv->apb_base + PCIE_ATR_IB_ADDR0(2));
154 writel(0, priv->apb_base + PCIE_ATR_IB_ADDR1(2));
155
156 return 0;
157}
158
159static int rockchip_pcie_init_port(struct udevice *dev)
160{
161 struct rockchip_pcie *priv = dev_get_priv(dev);
Jagan Teki5f142322020-05-09 22:26:22 +0530162 struct rockchip_pcie_phy *phy = pcie_get_phy(priv);
163 struct rockchip_pcie_phy_ops *ops = phy_get_ops(phy);
Jagan Teki02262472020-05-09 22:26:21 +0530164 u32 cr, val, status;
165 int ret;
166
167 if (dm_gpio_is_valid(&priv->ep_gpio))
168 dm_gpio_set_value(&priv->ep_gpio, 0);
169
170 ret = reset_assert(&priv->aclk_rst);
171 if (ret) {
172 dev_err(dev, "failed to assert aclk reset (ret=%d)\n", ret);
173 return ret;
174 }
175
176 ret = reset_assert(&priv->pclk_rst);
177 if (ret) {
178 dev_err(dev, "failed to assert pclk reset (ret=%d)\n", ret);
179 return ret;
180 }
181
182 ret = reset_assert(&priv->pm_rst);
183 if (ret) {
184 dev_err(dev, "failed to assert pm reset (ret=%d)\n", ret);
185 return ret;
186 }
187
Jagan Teki5f142322020-05-09 22:26:22 +0530188 ret = ops->init(phy);
189 if (ret) {
190 dev_err(dev, "failed to init phy (ret=%d)\n", ret);
191 goto err_exit_phy;
192 }
193
Jagan Teki02262472020-05-09 22:26:21 +0530194 ret = reset_assert(&priv->core_rst);
195 if (ret) {
196 dev_err(dev, "failed to assert core reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530197 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530198 }
199
200 ret = reset_assert(&priv->mgmt_rst);
201 if (ret) {
202 dev_err(dev, "failed to assert mgmt reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530203 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530204 }
205
206 ret = reset_assert(&priv->mgmt_sticky_rst);
207 if (ret) {
208 dev_err(dev, "failed to assert mgmt-sticky reset (ret=%d)\n",
209 ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530210 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530211 }
212
213 ret = reset_assert(&priv->pipe_rst);
214 if (ret) {
215 dev_err(dev, "failed to assert pipe reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530216 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530217 }
218
219 udelay(10);
220
221 ret = reset_deassert(&priv->pm_rst);
222 if (ret) {
223 dev_err(dev, "failed to deassert pm reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530224 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530225 }
226
227 ret = reset_deassert(&priv->aclk_rst);
228 if (ret) {
229 dev_err(dev, "failed to deassert aclk reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530230 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530231 }
232
233 ret = reset_deassert(&priv->pclk_rst);
234 if (ret) {
235 dev_err(dev, "failed to deassert pclk reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530236 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530237 }
238
239 /* Select GEN1 for now */
240 cr = PCIE_CLIENT_GEN_SEL_1;
241 /* Set Root complex mode */
242 cr |= PCIE_CLIENT_CONF_ENABLE | PCIE_CLIENT_MODE_RC;
243 writel(cr, priv->apb_base + PCIE_CLIENT_CONFIG);
244
Jagan Teki5f142322020-05-09 22:26:22 +0530245 ret = ops->power_on(phy);
246 if (ret) {
247 dev_err(dev, "failed to power on phy (ret=%d)\n", ret);
248 goto err_power_off_phy;
249 }
250
Jagan Teki02262472020-05-09 22:26:21 +0530251 ret = reset_deassert(&priv->mgmt_sticky_rst);
252 if (ret) {
253 dev_err(dev, "failed to deassert mgmt-sticky reset (ret=%d)\n",
254 ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530255 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530256 }
257
258 ret = reset_deassert(&priv->core_rst);
259 if (ret) {
260 dev_err(dev, "failed to deassert core reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530261 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530262 }
263
264 ret = reset_deassert(&priv->mgmt_rst);
265 if (ret) {
266 dev_err(dev, "failed to deassert mgmt reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530267 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530268 }
269
270 ret = reset_deassert(&priv->pipe_rst);
271 if (ret) {
272 dev_err(dev, "failed to deassert pipe reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530273 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530274 }
275
276 /* Enable Gen1 training */
277 writel(PCIE_CLIENT_LINK_TRAIN_ENABLE,
278 priv->apb_base + PCIE_CLIENT_CONFIG);
279
280 if (dm_gpio_is_valid(&priv->ep_gpio))
281 dm_gpio_set_value(&priv->ep_gpio, 1);
282
283 ret = readl_poll_sleep_timeout
284 (priv->apb_base + PCIE_CLIENT_BASIC_STATUS1,
285 status, PCIE_LINK_UP(status), 20, 500 * 1000);
286 if (ret) {
287 dev_err(dev, "PCIe link training gen1 timeout!\n");
Jagan Teki5f142322020-05-09 22:26:22 +0530288 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530289 }
290
291 /* Initialize Root Complex registers. */
292 writel(PCIE_LM_VENDOR_ROCKCHIP, priv->apb_base + PCIE_LM_VENDOR_ID);
293 writel(PCI_CLASS_BRIDGE_PCI << 16,
294 priv->apb_base + PCIE_RC_BASE + PCI_CLASS_REVISION);
295 writel(PCIE_LM_RCBARPIE | PCIE_LM_RCBARPIS,
296 priv->apb_base + PCIE_LM_RCBAR);
297
298 if (dev_read_bool(dev, "aspm-no-l0s")) {
299 val = readl(priv->apb_base + PCIE_RC_PCIE_LCAP);
300 val &= ~PCIE_RC_PCIE_LCAP_APMS_L0S;
301 writel(val, priv->apb_base + PCIE_RC_PCIE_LCAP);
302 }
303
304 /* Configure Address Translation. */
305 ret = rockchip_pcie_atr_init(priv);
306 if (ret) {
307 dev_err(dev, "PCIE-%d: ATR init failed\n", dev->seq);
Jagan Teki5f142322020-05-09 22:26:22 +0530308 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530309 }
310
311 return 0;
Jagan Teki5f142322020-05-09 22:26:22 +0530312
313err_power_off_phy:
314 ops->power_off(phy);
315err_exit_phy:
316 ops->exit(phy);
317 return ret;
Jagan Teki02262472020-05-09 22:26:21 +0530318}
319
320static int rockchip_pcie_set_vpcie(struct udevice *dev)
321{
322 struct rockchip_pcie *priv = dev_get_priv(dev);
323 int ret;
324
Mark Kettenis15b40062020-05-24 22:32:51 +0200325 if (priv->vpcie3v3) {
Jagan Teki02262472020-05-09 22:26:21 +0530326 ret = regulator_set_enable(priv->vpcie3v3, true);
327 if (ret) {
328 dev_err(dev, "failed to enable vpcie3v3 (ret=%d)\n",
329 ret);
330 return ret;
331 }
332 }
333
Mark Kettenis15b40062020-05-24 22:32:51 +0200334 if (priv->vpcie1v8) {
335 ret = regulator_set_enable(priv->vpcie1v8, true);
336 if (ret) {
337 dev_err(dev, "failed to enable vpcie1v8 (ret=%d)\n",
338 ret);
339 goto err_disable_3v3;
340 }
Jagan Teki02262472020-05-09 22:26:21 +0530341 }
342
Mark Kettenis15b40062020-05-24 22:32:51 +0200343 if (priv->vpcie0v9) {
344 ret = regulator_set_enable(priv->vpcie0v9, true);
345 if (ret) {
346 dev_err(dev, "failed to enable vpcie0v9 (ret=%d)\n",
347 ret);
348 goto err_disable_1v8;
349 }
Jagan Teki02262472020-05-09 22:26:21 +0530350 }
351
352 return 0;
353
354err_disable_1v8:
Mark Kettenis15b40062020-05-24 22:32:51 +0200355 if (priv->vpcie1v8)
356 regulator_set_enable(priv->vpcie1v8, false);
Jagan Teki02262472020-05-09 22:26:21 +0530357err_disable_3v3:
Mark Kettenis15b40062020-05-24 22:32:51 +0200358 if (priv->vpcie3v3)
Jagan Teki02262472020-05-09 22:26:21 +0530359 regulator_set_enable(priv->vpcie3v3, false);
360 return ret;
361}
362
363static int rockchip_pcie_parse_dt(struct udevice *dev)
364{
365 struct rockchip_pcie *priv = dev_get_priv(dev);
366 int ret;
367
368 priv->axi_base = dev_read_addr_name(dev, "axi-base");
369 if (!priv->axi_base)
370 return -ENODEV;
371
372 priv->apb_base = dev_read_addr_name(dev, "apb-base");
373 if (!priv->axi_base)
374 return -ENODEV;
375
376 ret = gpio_request_by_name(dev, "ep-gpios", 0,
377 &priv->ep_gpio, GPIOD_IS_OUT);
378 if (ret) {
379 dev_err(dev, "failed to find ep-gpios property\n");
380 return ret;
381 }
382
383 ret = reset_get_by_name(dev, "core", &priv->core_rst);
384 if (ret) {
385 dev_err(dev, "failed to get core reset (ret=%d)\n", ret);
386 return ret;
387 }
388
389 ret = reset_get_by_name(dev, "mgmt", &priv->mgmt_rst);
390 if (ret) {
391 dev_err(dev, "failed to get mgmt reset (ret=%d)\n", ret);
392 return ret;
393 }
394
395 ret = reset_get_by_name(dev, "mgmt-sticky", &priv->mgmt_sticky_rst);
396 if (ret) {
397 dev_err(dev, "failed to get mgmt-sticky reset (ret=%d)\n", ret);
398 return ret;
399 }
400
401 ret = reset_get_by_name(dev, "pipe", &priv->pipe_rst);
402 if (ret) {
403 dev_err(dev, "failed to get pipe reset (ret=%d)\n", ret);
404 return ret;
405 }
406
407 ret = reset_get_by_name(dev, "pm", &priv->pm_rst);
408 if (ret) {
409 dev_err(dev, "failed to get pm reset (ret=%d)\n", ret);
410 return ret;
411 }
412
413 ret = reset_get_by_name(dev, "pclk", &priv->pclk_rst);
414 if (ret) {
415 dev_err(dev, "failed to get pclk reset (ret=%d)\n", ret);
416 return ret;
417 }
418
419 ret = reset_get_by_name(dev, "aclk", &priv->aclk_rst);
420 if (ret) {
421 dev_err(dev, "failed to get aclk reset (ret=%d)\n", ret);
422 return ret;
423 }
424
425 ret = device_get_supply_regulator(dev, "vpcie3v3-supply",
426 &priv->vpcie3v3);
427 if (ret && ret != -ENOENT) {
428 dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret);
429 return ret;
430 }
431
432 ret = device_get_supply_regulator(dev, "vpcie1v8-supply",
433 &priv->vpcie1v8);
Mark Kettenis15b40062020-05-24 22:32:51 +0200434 if (ret && ret != -ENOENT) {
Jagan Teki02262472020-05-09 22:26:21 +0530435 dev_err(dev, "failed to get vpcie1v8 supply (ret=%d)\n", ret);
436 return ret;
437 }
438
439 ret = device_get_supply_regulator(dev, "vpcie0v9-supply",
440 &priv->vpcie0v9);
Mark Kettenis15b40062020-05-24 22:32:51 +0200441 if (ret && ret != -ENOENT) {
Jagan Teki02262472020-05-09 22:26:21 +0530442 dev_err(dev, "failed to get vpcie0v9 supply (ret=%d)\n", ret);
443 return ret;
444 }
445
446 return 0;
447}
448
449static int rockchip_pcie_probe(struct udevice *dev)
450{
451 struct rockchip_pcie *priv = dev_get_priv(dev);
452 struct udevice *ctlr = pci_get_controller(dev);
453 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
454 int ret;
455
456 priv->first_busno = dev->seq;
457 priv->dev = dev;
458
459 ret = rockchip_pcie_parse_dt(dev);
460 if (ret)
461 return ret;
462
Jagan Teki5f142322020-05-09 22:26:22 +0530463 ret = rockchip_pcie_phy_get(dev);
464 if (ret)
465 return ret;
466
Jagan Teki02262472020-05-09 22:26:21 +0530467 ret = rockchip_pcie_set_vpcie(dev);
468 if (ret)
469 return ret;
470
471 ret = rockchip_pcie_init_port(dev);
472 if (ret)
473 return ret;
474
475 dev_info(dev, "PCIE-%d: Link up (Bus%d)\n",
476 dev->seq, hose->first_busno);
477
478 return 0;
479}
480
481static const struct dm_pci_ops rockchip_pcie_ops = {
482 .read_config = rockchip_pcie_rd_conf,
483 .write_config = rockchip_pcie_wr_conf,
484};
485
486static const struct udevice_id rockchip_pcie_ids[] = {
487 { .compatible = "rockchip,rk3399-pcie" },
488 { }
489};
490
491U_BOOT_DRIVER(rockchip_pcie) = {
492 .name = "rockchip_pcie",
493 .id = UCLASS_PCI,
494 .of_match = rockchip_pcie_ids,
495 .ops = &rockchip_pcie_ops,
496 .probe = rockchip_pcie_probe,
497 .priv_auto_alloc_size = sizeof(struct rockchip_pcie),
498};