blob: ce573aa4b430a4cef32861a4753ede2e0aab8d7e [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);
162 u32 cr, val, status;
163 int ret;
164
165 if (dm_gpio_is_valid(&priv->ep_gpio))
166 dm_gpio_set_value(&priv->ep_gpio, 0);
167
168 ret = reset_assert(&priv->aclk_rst);
169 if (ret) {
170 dev_err(dev, "failed to assert aclk reset (ret=%d)\n", ret);
171 return ret;
172 }
173
174 ret = reset_assert(&priv->pclk_rst);
175 if (ret) {
176 dev_err(dev, "failed to assert pclk reset (ret=%d)\n", ret);
177 return ret;
178 }
179
180 ret = reset_assert(&priv->pm_rst);
181 if (ret) {
182 dev_err(dev, "failed to assert pm reset (ret=%d)\n", ret);
183 return ret;
184 }
185
Jagan Teki427603b2020-07-09 23:41:02 +0530186 ret = generic_phy_init(&priv->pcie_phy);
Jagan Teki5f142322020-05-09 22:26:22 +0530187 if (ret) {
188 dev_err(dev, "failed to init phy (ret=%d)\n", ret);
189 goto err_exit_phy;
190 }
191
Jagan Teki02262472020-05-09 22:26:21 +0530192 ret = reset_assert(&priv->core_rst);
193 if (ret) {
194 dev_err(dev, "failed to assert core reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530195 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530196 }
197
198 ret = reset_assert(&priv->mgmt_rst);
199 if (ret) {
200 dev_err(dev, "failed to assert mgmt reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530201 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530202 }
203
204 ret = reset_assert(&priv->mgmt_sticky_rst);
205 if (ret) {
206 dev_err(dev, "failed to assert mgmt-sticky reset (ret=%d)\n",
207 ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530208 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530209 }
210
211 ret = reset_assert(&priv->pipe_rst);
212 if (ret) {
213 dev_err(dev, "failed to assert pipe reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530214 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530215 }
216
217 udelay(10);
218
219 ret = reset_deassert(&priv->pm_rst);
220 if (ret) {
221 dev_err(dev, "failed to deassert pm reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530222 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530223 }
224
225 ret = reset_deassert(&priv->aclk_rst);
226 if (ret) {
227 dev_err(dev, "failed to deassert aclk reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530228 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530229 }
230
231 ret = reset_deassert(&priv->pclk_rst);
232 if (ret) {
233 dev_err(dev, "failed to deassert pclk reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530234 goto err_exit_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530235 }
236
237 /* Select GEN1 for now */
238 cr = PCIE_CLIENT_GEN_SEL_1;
239 /* Set Root complex mode */
240 cr |= PCIE_CLIENT_CONF_ENABLE | PCIE_CLIENT_MODE_RC;
241 writel(cr, priv->apb_base + PCIE_CLIENT_CONFIG);
242
Jagan Teki427603b2020-07-09 23:41:02 +0530243 ret = generic_phy_power_on(&priv->pcie_phy);
Jagan Teki5f142322020-05-09 22:26:22 +0530244 if (ret) {
245 dev_err(dev, "failed to power on phy (ret=%d)\n", ret);
246 goto err_power_off_phy;
247 }
248
Jagan Teki02262472020-05-09 22:26:21 +0530249 ret = reset_deassert(&priv->mgmt_sticky_rst);
250 if (ret) {
251 dev_err(dev, "failed to deassert mgmt-sticky reset (ret=%d)\n",
252 ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530253 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530254 }
255
256 ret = reset_deassert(&priv->core_rst);
257 if (ret) {
258 dev_err(dev, "failed to deassert core reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530259 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530260 }
261
262 ret = reset_deassert(&priv->mgmt_rst);
263 if (ret) {
264 dev_err(dev, "failed to deassert mgmt reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530265 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530266 }
267
268 ret = reset_deassert(&priv->pipe_rst);
269 if (ret) {
270 dev_err(dev, "failed to deassert pipe reset (ret=%d)\n", ret);
Jagan Teki5f142322020-05-09 22:26:22 +0530271 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530272 }
273
274 /* Enable Gen1 training */
275 writel(PCIE_CLIENT_LINK_TRAIN_ENABLE,
276 priv->apb_base + PCIE_CLIENT_CONFIG);
277
278 if (dm_gpio_is_valid(&priv->ep_gpio))
279 dm_gpio_set_value(&priv->ep_gpio, 1);
280
281 ret = readl_poll_sleep_timeout
282 (priv->apb_base + PCIE_CLIENT_BASIC_STATUS1,
283 status, PCIE_LINK_UP(status), 20, 500 * 1000);
284 if (ret) {
285 dev_err(dev, "PCIe link training gen1 timeout!\n");
Jagan Teki5f142322020-05-09 22:26:22 +0530286 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530287 }
288
289 /* Initialize Root Complex registers. */
290 writel(PCIE_LM_VENDOR_ROCKCHIP, priv->apb_base + PCIE_LM_VENDOR_ID);
291 writel(PCI_CLASS_BRIDGE_PCI << 16,
292 priv->apb_base + PCIE_RC_BASE + PCI_CLASS_REVISION);
293 writel(PCIE_LM_RCBARPIE | PCIE_LM_RCBARPIS,
294 priv->apb_base + PCIE_LM_RCBAR);
295
296 if (dev_read_bool(dev, "aspm-no-l0s")) {
297 val = readl(priv->apb_base + PCIE_RC_PCIE_LCAP);
298 val &= ~PCIE_RC_PCIE_LCAP_APMS_L0S;
299 writel(val, priv->apb_base + PCIE_RC_PCIE_LCAP);
300 }
301
302 /* Configure Address Translation. */
303 ret = rockchip_pcie_atr_init(priv);
304 if (ret) {
305 dev_err(dev, "PCIE-%d: ATR init failed\n", dev->seq);
Jagan Teki5f142322020-05-09 22:26:22 +0530306 goto err_power_off_phy;
Jagan Teki02262472020-05-09 22:26:21 +0530307 }
308
309 return 0;
Jagan Teki5f142322020-05-09 22:26:22 +0530310
311err_power_off_phy:
Jagan Teki427603b2020-07-09 23:41:02 +0530312 generic_phy_power_off(&priv->pcie_phy);
Jagan Teki5f142322020-05-09 22:26:22 +0530313err_exit_phy:
Jagan Teki427603b2020-07-09 23:41:02 +0530314 generic_phy_exit(&priv->pcie_phy);
Jagan Teki5f142322020-05-09 22:26:22 +0530315 return ret;
Jagan Teki02262472020-05-09 22:26:21 +0530316}
317
318static int rockchip_pcie_set_vpcie(struct udevice *dev)
319{
320 struct rockchip_pcie *priv = dev_get_priv(dev);
321 int ret;
322
Mark Kettenis15b40062020-05-24 22:32:51 +0200323 if (priv->vpcie3v3) {
Jagan Teki02262472020-05-09 22:26:21 +0530324 ret = regulator_set_enable(priv->vpcie3v3, true);
325 if (ret) {
326 dev_err(dev, "failed to enable vpcie3v3 (ret=%d)\n",
327 ret);
328 return ret;
329 }
330 }
331
Mark Kettenis15b40062020-05-24 22:32:51 +0200332 if (priv->vpcie1v8) {
333 ret = regulator_set_enable(priv->vpcie1v8, true);
334 if (ret) {
335 dev_err(dev, "failed to enable vpcie1v8 (ret=%d)\n",
336 ret);
337 goto err_disable_3v3;
338 }
Jagan Teki02262472020-05-09 22:26:21 +0530339 }
340
Mark Kettenis15b40062020-05-24 22:32:51 +0200341 if (priv->vpcie0v9) {
342 ret = regulator_set_enable(priv->vpcie0v9, true);
343 if (ret) {
344 dev_err(dev, "failed to enable vpcie0v9 (ret=%d)\n",
345 ret);
346 goto err_disable_1v8;
347 }
Jagan Teki02262472020-05-09 22:26:21 +0530348 }
349
350 return 0;
351
352err_disable_1v8:
Mark Kettenis15b40062020-05-24 22:32:51 +0200353 if (priv->vpcie1v8)
354 regulator_set_enable(priv->vpcie1v8, false);
Jagan Teki02262472020-05-09 22:26:21 +0530355err_disable_3v3:
Mark Kettenis15b40062020-05-24 22:32:51 +0200356 if (priv->vpcie3v3)
Jagan Teki02262472020-05-09 22:26:21 +0530357 regulator_set_enable(priv->vpcie3v3, false);
358 return ret;
359}
360
361static int rockchip_pcie_parse_dt(struct udevice *dev)
362{
363 struct rockchip_pcie *priv = dev_get_priv(dev);
364 int ret;
365
366 priv->axi_base = dev_read_addr_name(dev, "axi-base");
367 if (!priv->axi_base)
368 return -ENODEV;
369
370 priv->apb_base = dev_read_addr_name(dev, "apb-base");
371 if (!priv->axi_base)
372 return -ENODEV;
373
374 ret = gpio_request_by_name(dev, "ep-gpios", 0,
375 &priv->ep_gpio, GPIOD_IS_OUT);
376 if (ret) {
377 dev_err(dev, "failed to find ep-gpios property\n");
378 return ret;
379 }
380
381 ret = reset_get_by_name(dev, "core", &priv->core_rst);
382 if (ret) {
383 dev_err(dev, "failed to get core reset (ret=%d)\n", ret);
384 return ret;
385 }
386
387 ret = reset_get_by_name(dev, "mgmt", &priv->mgmt_rst);
388 if (ret) {
389 dev_err(dev, "failed to get mgmt reset (ret=%d)\n", ret);
390 return ret;
391 }
392
393 ret = reset_get_by_name(dev, "mgmt-sticky", &priv->mgmt_sticky_rst);
394 if (ret) {
395 dev_err(dev, "failed to get mgmt-sticky reset (ret=%d)\n", ret);
396 return ret;
397 }
398
399 ret = reset_get_by_name(dev, "pipe", &priv->pipe_rst);
400 if (ret) {
401 dev_err(dev, "failed to get pipe reset (ret=%d)\n", ret);
402 return ret;
403 }
404
405 ret = reset_get_by_name(dev, "pm", &priv->pm_rst);
406 if (ret) {
407 dev_err(dev, "failed to get pm reset (ret=%d)\n", ret);
408 return ret;
409 }
410
411 ret = reset_get_by_name(dev, "pclk", &priv->pclk_rst);
412 if (ret) {
413 dev_err(dev, "failed to get pclk reset (ret=%d)\n", ret);
414 return ret;
415 }
416
417 ret = reset_get_by_name(dev, "aclk", &priv->aclk_rst);
418 if (ret) {
419 dev_err(dev, "failed to get aclk reset (ret=%d)\n", ret);
420 return ret;
421 }
422
423 ret = device_get_supply_regulator(dev, "vpcie3v3-supply",
424 &priv->vpcie3v3);
425 if (ret && ret != -ENOENT) {
426 dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret);
427 return ret;
428 }
429
430 ret = device_get_supply_regulator(dev, "vpcie1v8-supply",
431 &priv->vpcie1v8);
Mark Kettenis15b40062020-05-24 22:32:51 +0200432 if (ret && ret != -ENOENT) {
Jagan Teki02262472020-05-09 22:26:21 +0530433 dev_err(dev, "failed to get vpcie1v8 supply (ret=%d)\n", ret);
434 return ret;
435 }
436
437 ret = device_get_supply_regulator(dev, "vpcie0v9-supply",
438 &priv->vpcie0v9);
Mark Kettenis15b40062020-05-24 22:32:51 +0200439 if (ret && ret != -ENOENT) {
Jagan Teki02262472020-05-09 22:26:21 +0530440 dev_err(dev, "failed to get vpcie0v9 supply (ret=%d)\n", ret);
441 return ret;
442 }
443
Jagan Teki427603b2020-07-09 23:41:02 +0530444 ret = generic_phy_get_by_index(dev, 0, &priv->pcie_phy);
445 if (ret) {
446 dev_err(dev, "failed to get pcie-phy (ret=%d)\n", ret);
447 return ret;
448 }
449
Jagan Teki02262472020-05-09 22:26:21 +0530450 return 0;
451}
452
453static int rockchip_pcie_probe(struct udevice *dev)
454{
455 struct rockchip_pcie *priv = dev_get_priv(dev);
456 struct udevice *ctlr = pci_get_controller(dev);
457 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
458 int ret;
459
460 priv->first_busno = dev->seq;
461 priv->dev = dev;
462
463 ret = rockchip_pcie_parse_dt(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};