blob: 82a8396e42c0d4418223e56cdfcef1650ea902fe [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
325 if (!IS_ERR(priv->vpcie3v3)) {
326 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
334 ret = regulator_set_enable(priv->vpcie1v8, true);
335 if (ret) {
336 dev_err(dev, "failed to enable vpcie1v8 (ret=%d)\n", ret);
337 goto err_disable_3v3;
338 }
339
340 ret = regulator_set_enable(priv->vpcie0v9, true);
341 if (ret) {
342 dev_err(dev, "failed to enable vpcie0v9 (ret=%d)\n", ret);
343 goto err_disable_1v8;
344 }
345
346 return 0;
347
348err_disable_1v8:
349 regulator_set_enable(priv->vpcie1v8, false);
350err_disable_3v3:
351 if (!IS_ERR(priv->vpcie3v3))
352 regulator_set_enable(priv->vpcie3v3, false);
353 return ret;
354}
355
356static int rockchip_pcie_parse_dt(struct udevice *dev)
357{
358 struct rockchip_pcie *priv = dev_get_priv(dev);
359 int ret;
360
361 priv->axi_base = dev_read_addr_name(dev, "axi-base");
362 if (!priv->axi_base)
363 return -ENODEV;
364
365 priv->apb_base = dev_read_addr_name(dev, "apb-base");
366 if (!priv->axi_base)
367 return -ENODEV;
368
369 ret = gpio_request_by_name(dev, "ep-gpios", 0,
370 &priv->ep_gpio, GPIOD_IS_OUT);
371 if (ret) {
372 dev_err(dev, "failed to find ep-gpios property\n");
373 return ret;
374 }
375
376 ret = reset_get_by_name(dev, "core", &priv->core_rst);
377 if (ret) {
378 dev_err(dev, "failed to get core reset (ret=%d)\n", ret);
379 return ret;
380 }
381
382 ret = reset_get_by_name(dev, "mgmt", &priv->mgmt_rst);
383 if (ret) {
384 dev_err(dev, "failed to get mgmt reset (ret=%d)\n", ret);
385 return ret;
386 }
387
388 ret = reset_get_by_name(dev, "mgmt-sticky", &priv->mgmt_sticky_rst);
389 if (ret) {
390 dev_err(dev, "failed to get mgmt-sticky reset (ret=%d)\n", ret);
391 return ret;
392 }
393
394 ret = reset_get_by_name(dev, "pipe", &priv->pipe_rst);
395 if (ret) {
396 dev_err(dev, "failed to get pipe reset (ret=%d)\n", ret);
397 return ret;
398 }
399
400 ret = reset_get_by_name(dev, "pm", &priv->pm_rst);
401 if (ret) {
402 dev_err(dev, "failed to get pm reset (ret=%d)\n", ret);
403 return ret;
404 }
405
406 ret = reset_get_by_name(dev, "pclk", &priv->pclk_rst);
407 if (ret) {
408 dev_err(dev, "failed to get pclk reset (ret=%d)\n", ret);
409 return ret;
410 }
411
412 ret = reset_get_by_name(dev, "aclk", &priv->aclk_rst);
413 if (ret) {
414 dev_err(dev, "failed to get aclk reset (ret=%d)\n", ret);
415 return ret;
416 }
417
418 ret = device_get_supply_regulator(dev, "vpcie3v3-supply",
419 &priv->vpcie3v3);
420 if (ret && ret != -ENOENT) {
421 dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret);
422 return ret;
423 }
424
425 ret = device_get_supply_regulator(dev, "vpcie1v8-supply",
426 &priv->vpcie1v8);
427 if (ret) {
428 dev_err(dev, "failed to get vpcie1v8 supply (ret=%d)\n", ret);
429 return ret;
430 }
431
432 ret = device_get_supply_regulator(dev, "vpcie0v9-supply",
433 &priv->vpcie0v9);
434 if (ret) {
435 dev_err(dev, "failed to get vpcie0v9 supply (ret=%d)\n", ret);
436 return ret;
437 }
438
439 return 0;
440}
441
442static int rockchip_pcie_probe(struct udevice *dev)
443{
444 struct rockchip_pcie *priv = dev_get_priv(dev);
445 struct udevice *ctlr = pci_get_controller(dev);
446 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
447 int ret;
448
449 priv->first_busno = dev->seq;
450 priv->dev = dev;
451
452 ret = rockchip_pcie_parse_dt(dev);
453 if (ret)
454 return ret;
455
Jagan Teki5f142322020-05-09 22:26:22 +0530456 ret = rockchip_pcie_phy_get(dev);
457 if (ret)
458 return ret;
459
Jagan Teki02262472020-05-09 22:26:21 +0530460 ret = rockchip_pcie_set_vpcie(dev);
461 if (ret)
462 return ret;
463
464 ret = rockchip_pcie_init_port(dev);
465 if (ret)
466 return ret;
467
468 dev_info(dev, "PCIE-%d: Link up (Bus%d)\n",
469 dev->seq, hose->first_busno);
470
471 return 0;
472}
473
474static const struct dm_pci_ops rockchip_pcie_ops = {
475 .read_config = rockchip_pcie_rd_conf,
476 .write_config = rockchip_pcie_wr_conf,
477};
478
479static const struct udevice_id rockchip_pcie_ids[] = {
480 { .compatible = "rockchip,rk3399-pcie" },
481 { }
482};
483
484U_BOOT_DRIVER(rockchip_pcie) = {
485 .name = "rockchip_pcie",
486 .id = UCLASS_PCI,
487 .of_match = rockchip_pcie_ids,
488 .ops = &rockchip_pcie_ops,
489 .probe = rockchip_pcie_probe,
490 .priv_auto_alloc_size = sizeof(struct rockchip_pcie),
491};