blob: 3f06f783cab515421f7d53668f8fd30f0a77c8db [file] [log] [blame]
Jagan Teki99d59062020-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
186 ret = reset_assert(&priv->core_rst);
187 if (ret) {
188 dev_err(dev, "failed to assert core reset (ret=%d)\n", ret);
189 return ret;
190 }
191
192 ret = reset_assert(&priv->mgmt_rst);
193 if (ret) {
194 dev_err(dev, "failed to assert mgmt reset (ret=%d)\n", ret);
195 return ret;
196 }
197
198 ret = reset_assert(&priv->mgmt_sticky_rst);
199 if (ret) {
200 dev_err(dev, "failed to assert mgmt-sticky reset (ret=%d)\n",
201 ret);
202 return ret;
203 }
204
205 ret = reset_assert(&priv->pipe_rst);
206 if (ret) {
207 dev_err(dev, "failed to assert pipe reset (ret=%d)\n", ret);
208 return ret;
209 }
210
211 udelay(10);
212
213 ret = reset_deassert(&priv->pm_rst);
214 if (ret) {
215 dev_err(dev, "failed to deassert pm reset (ret=%d)\n", ret);
216 return ret;
217 }
218
219 ret = reset_deassert(&priv->aclk_rst);
220 if (ret) {
221 dev_err(dev, "failed to deassert aclk reset (ret=%d)\n", ret);
222 return ret;
223 }
224
225 ret = reset_deassert(&priv->pclk_rst);
226 if (ret) {
227 dev_err(dev, "failed to deassert pclk reset (ret=%d)\n", ret);
228 return ret;
229 }
230
231 /* Select GEN1 for now */
232 cr = PCIE_CLIENT_GEN_SEL_1;
233 /* Set Root complex mode */
234 cr |= PCIE_CLIENT_CONF_ENABLE | PCIE_CLIENT_MODE_RC;
235 writel(cr, priv->apb_base + PCIE_CLIENT_CONFIG);
236
237 ret = reset_deassert(&priv->mgmt_sticky_rst);
238 if (ret) {
239 dev_err(dev, "failed to deassert mgmt-sticky reset (ret=%d)\n",
240 ret);
241 return ret;
242 }
243
244 ret = reset_deassert(&priv->core_rst);
245 if (ret) {
246 dev_err(dev, "failed to deassert core reset (ret=%d)\n", ret);
247 return ret;
248 }
249
250 ret = reset_deassert(&priv->mgmt_rst);
251 if (ret) {
252 dev_err(dev, "failed to deassert mgmt reset (ret=%d)\n", ret);
253 return ret;
254 }
255
256 ret = reset_deassert(&priv->pipe_rst);
257 if (ret) {
258 dev_err(dev, "failed to deassert pipe reset (ret=%d)\n", ret);
259 return ret;
260 }
261
262 /* Enable Gen1 training */
263 writel(PCIE_CLIENT_LINK_TRAIN_ENABLE,
264 priv->apb_base + PCIE_CLIENT_CONFIG);
265
266 if (dm_gpio_is_valid(&priv->ep_gpio))
267 dm_gpio_set_value(&priv->ep_gpio, 1);
268
269 ret = readl_poll_sleep_timeout
270 (priv->apb_base + PCIE_CLIENT_BASIC_STATUS1,
271 status, PCIE_LINK_UP(status), 20, 500 * 1000);
272 if (ret) {
273 dev_err(dev, "PCIe link training gen1 timeout!\n");
274 return ret;
275 }
276
277 /* Initialize Root Complex registers. */
278 writel(PCIE_LM_VENDOR_ROCKCHIP, priv->apb_base + PCIE_LM_VENDOR_ID);
279 writel(PCI_CLASS_BRIDGE_PCI << 16,
280 priv->apb_base + PCIE_RC_BASE + PCI_CLASS_REVISION);
281 writel(PCIE_LM_RCBARPIE | PCIE_LM_RCBARPIS,
282 priv->apb_base + PCIE_LM_RCBAR);
283
284 if (dev_read_bool(dev, "aspm-no-l0s")) {
285 val = readl(priv->apb_base + PCIE_RC_PCIE_LCAP);
286 val &= ~PCIE_RC_PCIE_LCAP_APMS_L0S;
287 writel(val, priv->apb_base + PCIE_RC_PCIE_LCAP);
288 }
289
290 /* Configure Address Translation. */
291 ret = rockchip_pcie_atr_init(priv);
292 if (ret) {
293 dev_err(dev, "PCIE-%d: ATR init failed\n", dev->seq);
294 return ret;
295 }
296
297 return 0;
298}
299
300static int rockchip_pcie_set_vpcie(struct udevice *dev)
301{
302 struct rockchip_pcie *priv = dev_get_priv(dev);
303 int ret;
304
305 if (!IS_ERR(priv->vpcie3v3)) {
306 ret = regulator_set_enable(priv->vpcie3v3, true);
307 if (ret) {
308 dev_err(dev, "failed to enable vpcie3v3 (ret=%d)\n",
309 ret);
310 return ret;
311 }
312 }
313
314 ret = regulator_set_enable(priv->vpcie1v8, true);
315 if (ret) {
316 dev_err(dev, "failed to enable vpcie1v8 (ret=%d)\n", ret);
317 goto err_disable_3v3;
318 }
319
320 ret = regulator_set_enable(priv->vpcie0v9, true);
321 if (ret) {
322 dev_err(dev, "failed to enable vpcie0v9 (ret=%d)\n", ret);
323 goto err_disable_1v8;
324 }
325
326 return 0;
327
328err_disable_1v8:
329 regulator_set_enable(priv->vpcie1v8, false);
330err_disable_3v3:
331 if (!IS_ERR(priv->vpcie3v3))
332 regulator_set_enable(priv->vpcie3v3, false);
333 return ret;
334}
335
336static int rockchip_pcie_parse_dt(struct udevice *dev)
337{
338 struct rockchip_pcie *priv = dev_get_priv(dev);
339 int ret;
340
341 priv->axi_base = dev_read_addr_name(dev, "axi-base");
342 if (!priv->axi_base)
343 return -ENODEV;
344
345 priv->apb_base = dev_read_addr_name(dev, "apb-base");
346 if (!priv->axi_base)
347 return -ENODEV;
348
349 ret = gpio_request_by_name(dev, "ep-gpios", 0,
350 &priv->ep_gpio, GPIOD_IS_OUT);
351 if (ret) {
352 dev_err(dev, "failed to find ep-gpios property\n");
353 return ret;
354 }
355
356 ret = reset_get_by_name(dev, "core", &priv->core_rst);
357 if (ret) {
358 dev_err(dev, "failed to get core reset (ret=%d)\n", ret);
359 return ret;
360 }
361
362 ret = reset_get_by_name(dev, "mgmt", &priv->mgmt_rst);
363 if (ret) {
364 dev_err(dev, "failed to get mgmt reset (ret=%d)\n", ret);
365 return ret;
366 }
367
368 ret = reset_get_by_name(dev, "mgmt-sticky", &priv->mgmt_sticky_rst);
369 if (ret) {
370 dev_err(dev, "failed to get mgmt-sticky reset (ret=%d)\n", ret);
371 return ret;
372 }
373
374 ret = reset_get_by_name(dev, "pipe", &priv->pipe_rst);
375 if (ret) {
376 dev_err(dev, "failed to get pipe reset (ret=%d)\n", ret);
377 return ret;
378 }
379
380 ret = reset_get_by_name(dev, "pm", &priv->pm_rst);
381 if (ret) {
382 dev_err(dev, "failed to get pm reset (ret=%d)\n", ret);
383 return ret;
384 }
385
386 ret = reset_get_by_name(dev, "pclk", &priv->pclk_rst);
387 if (ret) {
388 dev_err(dev, "failed to get pclk reset (ret=%d)\n", ret);
389 return ret;
390 }
391
392 ret = reset_get_by_name(dev, "aclk", &priv->aclk_rst);
393 if (ret) {
394 dev_err(dev, "failed to get aclk reset (ret=%d)\n", ret);
395 return ret;
396 }
397
398 ret = device_get_supply_regulator(dev, "vpcie3v3-supply",
399 &priv->vpcie3v3);
400 if (ret && ret != -ENOENT) {
401 dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret);
402 return ret;
403 }
404
405 ret = device_get_supply_regulator(dev, "vpcie1v8-supply",
406 &priv->vpcie1v8);
407 if (ret) {
408 dev_err(dev, "failed to get vpcie1v8 supply (ret=%d)\n", ret);
409 return ret;
410 }
411
412 ret = device_get_supply_regulator(dev, "vpcie0v9-supply",
413 &priv->vpcie0v9);
414 if (ret) {
415 dev_err(dev, "failed to get vpcie0v9 supply (ret=%d)\n", ret);
416 return ret;
417 }
418
419 return 0;
420}
421
422static int rockchip_pcie_probe(struct udevice *dev)
423{
424 struct rockchip_pcie *priv = dev_get_priv(dev);
425 struct udevice *ctlr = pci_get_controller(dev);
426 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
427 int ret;
428
429 priv->first_busno = dev->seq;
430 priv->dev = dev;
431
432 ret = rockchip_pcie_parse_dt(dev);
433 if (ret)
434 return ret;
435
436 ret = rockchip_pcie_set_vpcie(dev);
437 if (ret)
438 return ret;
439
440 ret = rockchip_pcie_init_port(dev);
441 if (ret)
442 return ret;
443
444 dev_info(dev, "PCIE-%d: Link up (Bus%d)\n",
445 dev->seq, hose->first_busno);
446
447 return 0;
448}
449
450static const struct dm_pci_ops rockchip_pcie_ops = {
451 .read_config = rockchip_pcie_rd_conf,
452 .write_config = rockchip_pcie_wr_conf,
453};
454
455static const struct udevice_id rockchip_pcie_ids[] = {
456 { .compatible = "rockchip,rk3399-pcie" },
457 { }
458};
459
460U_BOOT_DRIVER(rockchip_pcie) = {
461 .name = "rockchip_pcie",
462 .id = UCLASS_PCI,
463 .of_match = rockchip_pcie_ids,
464 .ops = &rockchip_pcie_ops,
465 .probe = rockchip_pcie_probe,
466 .priv_auto_alloc_size = sizeof(struct rockchip_pcie),
467};