1 // SPDX-License-Identifier: GPL-2.0+
3 * SiFive FU740 DesignWare PCIe Controller
5 * Copyright (C) 2020-2021 SiFive, Inc.
7 * Based in early part on the i.MX6 PCIe host controller shim which is:
9 * Copyright (C) 2013 Kosagi
10 * http://www.kosagi.com
15 #include <asm-generic/gpio.h>
19 #include <dm/device_compat.h>
20 #include <generic-phy.h>
21 #include <linux/bitops.h>
22 #include <linux/log2.h>
30 #include "pcie_dw_common.h"
33 /* Must be first member of the struct */
36 /* private control regs */
37 void __iomem *priv_base;
39 /* reset, power, clock resources */
41 struct gpio_desc pwren_gpio;
42 struct gpio_desc reset_gpio;
44 struct reset_ctl reset;
47 enum pcie_sifive_devtype {
48 SV_PCIE_UNKNOWN_TYPE = 0,
49 SV_PCIE_ENDPOINT_TYPE = 1,
53 #define ASSERTION_DELAY 100
54 #define PCIE_PERST_ASSERT 0x0
55 #define PCIE_PERST_DEASSERT 0x1
56 #define PCIE_PHY_RESET 0x1
57 #define PCIE_PHY_RESET_DEASSERT 0x0
60 #define PCIE_PHY_SEL 0x1
62 #define sv_info(sv, fmt, arg...) printf(fmt, ## arg)
63 #define sv_warn(sv, fmt, arg...) printf(fmt, ## arg)
64 #define sv_debug(sv, fmt, arg...) debug(fmt, ## arg)
65 #define sv_err(sv, fmt, arg...) printf(fmt, ## arg)
67 /* Doorbell Interface */
68 #define DBI_OFFSET 0x0
69 #define DBI_SIZE 0x1000
71 #define PL_OFFSET 0x700
73 #define PHY_DEBUG_R0 (PL_OFFSET + 0x28)
75 #define PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
76 #define PHY_DEBUG_R1_LINK_UP (0x1 << 4)
77 #define PHY_DEBUG_R1_LINK_IN_TRAINING (0x1 << 29)
79 #define PCIE_MISC_CONTROL_1 0x8bc
80 #define DBI_RO_WR_EN BIT(0)
83 #define PCIEX8MGMT_PERST_N 0x0
86 #define PCIEX8MGMT_APP_LTSSM_ENABLE 0x10
87 #define LTSSM_ENABLE_BIT BIT(0)
90 #define PCIEX8MGMT_APP_HOLD_PHY_RST 0x18
93 #define PCIEX8MGMT_DEVICE_TYPE 0x708
94 #define DEVICE_TYPE_EP 0x0
95 #define DEVICE_TYPE_RC 0x4
97 /* phy control registers*/
98 #define PCIEX8MGMT_PHY0_CR_PARA_ADDR 0x860
99 #define PCIEX8MGMT_PHY0_CR_PARA_RD_EN 0x870
100 #define PCIEX8MGMT_PHY0_CR_PARA_RD_DATA 0x878
101 #define PCIEX8MGMT_PHY0_CR_PARA_SEL 0x880
102 #define PCIEX8MGMT_PHY0_CR_PARA_WR_DATA 0x888
103 #define PCIEX8MGMT_PHY0_CR_PARA_WR_EN 0x890
104 #define PCIEX8MGMT_PHY0_CR_PARA_ACK 0x898
105 #define PCIEX8MGMT_PHY1_CR_PARA_ADDR 0x8a0
106 #define PCIEX8MGMT_PHY1_CR_PARA_RD_EN 0x8b0
107 #define PCIEX8MGMT_PHY1_CR_PARA_RD_DATA 0x8b8
108 #define PCIEX8MGMT_PHY1_CR_PARA_SEL 0x8c0
109 #define PCIEX8MGMT_PHY1_CR_PARA_WR_DATA 0x8c8
110 #define PCIEX8MGMT_PHY1_CR_PARA_WR_EN 0x8d0
111 #define PCIEX8MGMT_PHY1_CR_PARA_ACK 0x8d8
113 #define PCIEX8MGMT_LANE_NUM 8
114 #define PCIEX8MGMT_LANE 0x1008
115 #define PCIEX8MGMT_LANE_OFF 0x100
116 #define PCIEX8MGMT_TERM_MODE 0x0e21
118 #define PCIE_CAP_BASE 0x70
119 #define PCI_CONFIG(r) (DBI_OFFSET + (r))
120 #define PCIE_CAPABILITIES(r) PCI_CONFIG(PCIE_CAP_BASE + (r))
122 /* Link capability */
123 #define PF0_PCIE_CAP_LINK_CAP PCIE_CAPABILITIES(0xc)
124 #define PCIE_LINK_CAP_MAX_SPEED_MASK 0xf
125 #define PCIE_LINK_CAP_MAX_SPEED_GEN1 BIT(0)
126 #define PCIE_LINK_CAP_MAX_SPEED_GEN2 BIT(1)
127 #define PCIE_LINK_CAP_MAX_SPEED_GEN3 BIT(2)
128 #define PCIE_LINK_CAP_MAX_SPEED_GEN4 BIT(3)
130 static enum pcie_sifive_devtype pcie_sifive_get_devtype(struct pcie_sifive *sv)
134 val = readl(sv->priv_base + PCIEX8MGMT_DEVICE_TYPE);
137 return SV_PCIE_HOST_TYPE;
139 return SV_PCIE_ENDPOINT_TYPE;
141 return SV_PCIE_UNKNOWN_TYPE;
145 static void pcie_sifive_priv_set_state(struct pcie_sifive *sv, u32 reg,
150 val = readl(sv->priv_base + reg);
151 val = state ? (val | bits) : (val & !bits);
152 writel(val, sv->priv_base + reg);
155 static void pcie_sifive_assert_reset(struct pcie_sifive *sv)
157 dm_gpio_set_value(&sv->reset_gpio, GPIO_LOW);
158 writel(PCIE_PERST_ASSERT, sv->priv_base + PCIEX8MGMT_PERST_N);
159 mdelay(ASSERTION_DELAY);
162 static void pcie_sifive_power_on(struct pcie_sifive *sv)
164 dm_gpio_set_value(&sv->pwren_gpio, GPIO_HIGH);
165 mdelay(ASSERTION_DELAY);
168 static void pcie_sifive_deassert_reset(struct pcie_sifive *sv)
170 writel(PCIE_PERST_DEASSERT, sv->priv_base + PCIEX8MGMT_PERST_N);
171 dm_gpio_set_value(&sv->reset_gpio, GPIO_HIGH);
172 mdelay(ASSERTION_DELAY);
175 static int pcie_sifive_setphy(const u8 phy, const u8 write,
176 const u16 addr, const u16 wrdata,
177 u16 *rddata, struct pcie_sifive *sv)
179 unsigned char ack = 0;
181 if (!(phy == 0 || phy == 1))
185 writel(addr, sv->priv_base +
186 (phy ? PCIEX8MGMT_PHY1_CR_PARA_ADDR :
187 PCIEX8MGMT_PHY0_CR_PARA_ADDR));
190 writel(wrdata, sv->priv_base +
191 (phy ? PCIEX8MGMT_PHY1_CR_PARA_WR_DATA :
192 PCIEX8MGMT_PHY0_CR_PARA_WR_DATA));
194 /* enable access if write */
196 writel(1, sv->priv_base +
197 (phy ? PCIEX8MGMT_PHY1_CR_PARA_WR_EN :
198 PCIEX8MGMT_PHY0_CR_PARA_WR_EN));
200 writel(1, sv->priv_base +
201 (phy ? PCIEX8MGMT_PHY1_CR_PARA_RD_EN :
202 PCIEX8MGMT_PHY0_CR_PARA_RD_EN));
204 /* wait for wait_idle */
208 val = readl(sv->priv_base +
209 (phy ? PCIEX8MGMT_PHY1_CR_PARA_ACK :
210 PCIEX8MGMT_PHY0_CR_PARA_ACK));
214 readl(sv->priv_base +
215 (phy ? PCIEX8MGMT_PHY1_CR_PARA_RD_DATA :
216 PCIEX8MGMT_PHY0_CR_PARA_RD_DATA));
223 writel(0, sv->priv_base +
224 (phy ? PCIEX8MGMT_PHY1_CR_PARA_WR_EN :
225 PCIEX8MGMT_PHY0_CR_PARA_WR_EN));
227 writel(0, sv->priv_base +
228 (phy ? PCIEX8MGMT_PHY1_CR_PARA_RD_EN :
229 PCIEX8MGMT_PHY0_CR_PARA_RD_EN));
231 while (readl(sv->priv_base +
232 (phy ? PCIEX8MGMT_PHY1_CR_PARA_ACK :
233 PCIEX8MGMT_PHY0_CR_PARA_ACK))) {
234 /* wait for ~wait_idle */
240 static void pcie_sifive_init_phy(struct pcie_sifive *sv)
244 /* enable phy cr_para_sel interfaces */
245 writel(PCIE_PHY_SEL, sv->priv_base + PCIEX8MGMT_PHY0_CR_PARA_SEL);
246 writel(PCIE_PHY_SEL, sv->priv_base + PCIEX8MGMT_PHY1_CR_PARA_SEL);
249 /* set PHY AC termination mode */
250 for (lane = 0; lane < PCIEX8MGMT_LANE_NUM; lane++) {
251 pcie_sifive_setphy(0, 1,
253 (PCIEX8MGMT_LANE_OFF * lane),
254 PCIEX8MGMT_TERM_MODE, NULL, sv);
255 pcie_sifive_setphy(1, 1,
257 (PCIEX8MGMT_LANE_OFF * lane),
258 PCIEX8MGMT_TERM_MODE, NULL, sv);
262 static int pcie_sifive_check_link(struct pcie_sifive *sv)
266 val = readl(sv->dw.dbi_base + PHY_DEBUG_R1);
267 return (val & PHY_DEBUG_R1_LINK_UP) &&
268 !(val & PHY_DEBUG_R1_LINK_IN_TRAINING);
271 static void pcie_sifive_force_gen1(struct pcie_sifive *sv)
276 * Force Gen1 operation when starting the link. In case the link is
277 * started in Gen2 mode, there is a possibility the devices on the
278 * bus will not be detected at all. This happens with PCIe switches.
281 /* ctrl_ro_wr_enable */
282 val = readl(sv->dw.dbi_base + PCIE_MISC_CONTROL_1);
284 writel(val, sv->dw.dbi_base + PCIE_MISC_CONTROL_1);
286 /* configure link cap */
287 linkcap = readl(sv->dw.dbi_base + PF0_PCIE_CAP_LINK_CAP);
288 linkcap |= PCIE_LINK_CAP_MAX_SPEED_MASK;
289 writel(linkcap, sv->dw.dbi_base + PF0_PCIE_CAP_LINK_CAP);
291 /* ctrl_ro_wr_disable */
292 val &= ~DBI_RO_WR_EN;
293 writel(val, sv->dw.dbi_base + PCIE_MISC_CONTROL_1);
296 static void pcie_sifive_print_phy_debug(struct pcie_sifive *sv)
298 sv_err(sv, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
299 readl(sv->dw.dbi_base + PHY_DEBUG_R0),
300 readl(sv->dw.dbi_base + PHY_DEBUG_R1));
303 static int pcie_sifive_wait_for_link(struct pcie_sifive *sv)
308 /* Wait for the link to train */
314 } while (--timeout && !pcie_sifive_check_link(sv));
316 val = readl(sv->dw.dbi_base + PHY_DEBUG_R1);
317 if (!(val & PHY_DEBUG_R1_LINK_UP) ||
318 (val & PHY_DEBUG_R1_LINK_IN_TRAINING)) {
319 sv_info(sv, "Failed to negotiate PCIe link!\n");
320 pcie_sifive_print_phy_debug(sv);
321 writel(PCIE_PHY_RESET,
322 sv->priv_base + PCIEX8MGMT_APP_HOLD_PHY_RST);
329 static int pcie_sifive_start_link(struct pcie_sifive *sv)
331 if (pcie_sifive_check_link(sv))
334 pcie_sifive_force_gen1(sv);
337 pcie_sifive_priv_set_state(sv, PCIEX8MGMT_APP_LTSSM_ENABLE,
338 LTSSM_ENABLE_BIT, 1);
342 static int pcie_sifive_init_port(struct udevice *dev,
343 enum pcie_sifive_devtype mode)
345 struct pcie_sifive *sv = dev_get_priv(dev);
349 pcie_sifive_assert_reset(sv);
350 pcie_sifive_power_on(sv);
351 pcie_sifive_deassert_reset(sv);
353 /* Enable pcieauxclk */
354 ret = clk_enable(&sv->aux_ck);
356 dev_err(dev, "unable to enable pcie_aux clock\n");
359 * assert hold_phy_rst (hold the controller LTSSM in reset
360 * after power_up_rst_n for register programming with cr_para)
362 writel(PCIE_PHY_RESET, sv->priv_base + PCIEX8MGMT_APP_HOLD_PHY_RST);
364 /* deassert power_up_rst_n */
365 ret = reset_deassert(&sv->reset);
367 dev_err(dev, "failed to deassert reset");
371 pcie_sifive_init_phy(sv);
373 /* disable pcieauxclk */
374 clk_disable(&sv->aux_ck);
376 /* deassert hold_phy_rst */
377 writel(PCIE_PHY_RESET_DEASSERT,
378 sv->priv_base + PCIEX8MGMT_APP_HOLD_PHY_RST);
380 /* enable pcieauxclk */
381 clk_enable(&sv->aux_ck);
383 /* Set desired mode while core is not operational */
384 if (mode == SV_PCIE_HOST_TYPE)
385 writel(DEVICE_TYPE_RC,
386 sv->priv_base + PCIEX8MGMT_DEVICE_TYPE);
388 writel(DEVICE_TYPE_EP,
389 sv->priv_base + PCIEX8MGMT_DEVICE_TYPE);
391 /* Confirm desired mode from operational core */
392 if (pcie_sifive_get_devtype(sv) != mode)
395 pcie_dw_setup_host(&sv->dw);
397 if (pcie_sifive_start_link(sv) == -EALREADY)
398 sv_info(sv, "PCIe link is already up\n");
399 else if (pcie_sifive_wait_for_link(sv) == -ETIMEDOUT)
405 static int pcie_sifive_probe(struct udevice *dev)
407 struct pcie_sifive *sv = dev_get_priv(dev);
408 struct udevice *parent = pci_get_controller(dev);
409 struct pci_controller *hose = dev_get_uclass_priv(parent);
412 sv->dw.first_busno = dev_seq(dev);
415 err = pcie_sifive_init_port(dev, SV_PCIE_HOST_TYPE);
417 sv_info(sv, "Failed to init port.\n");
421 printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n",
422 dev_seq(dev), pcie_dw_get_link_speed(&sv->dw),
423 pcie_dw_get_link_width(&sv->dw),
426 return pcie_dw_prog_outbound_atu_unroll(&sv->dw,
427 PCIE_ATU_REGION_INDEX0,
429 sv->dw.mem.phys_start,
430 sv->dw.mem.bus_start,
434 static void __iomem *get_fdt_addr(struct udevice *dev, const char *name)
438 addr = dev_read_addr_name(dev, name);
440 return (addr == FDT_ADDR_T_NONE) ? NULL : (void __iomem *)addr;
443 static int pcie_sifive_of_to_plat(struct udevice *dev)
445 struct pcie_sifive *sv = dev_get_priv(dev);
448 /* get designware DBI base addr */
449 sv->dw.dbi_base = get_fdt_addr(dev, "dbi");
450 if (!sv->dw.dbi_base)
453 /* get private control base addr */
454 sv->priv_base = get_fdt_addr(dev, "mgmt");
458 gpio_request_by_name(dev, "pwren-gpios", 0, &sv->pwren_gpio,
461 if (!dm_gpio_is_valid(&sv->pwren_gpio)) {
462 sv_info(sv, "pwren_gpio is invalid\n");
466 gpio_request_by_name(dev, "reset-gpios", 0, &sv->reset_gpio,
469 if (!dm_gpio_is_valid(&sv->reset_gpio)) {
470 sv_info(sv, "reset_gpio is invalid\n");
474 err = clk_get_by_index(dev, 0, &sv->aux_ck);
476 sv_info(sv, "clk_get_by_index(aux_ck) failed: %d\n", err);
480 err = reset_get_by_index(dev, 0, &sv->reset);
482 sv_info(sv, "reset_get_by_index(reset) failed: %d\n", err);
489 static const struct dm_pci_ops pcie_sifive_ops = {
490 .read_config = pcie_dw_read_config,
491 .write_config = pcie_dw_write_config,
494 static const struct udevice_id pcie_sifive_ids[] = {
495 { .compatible = "sifive,fu740-pcie" },
499 U_BOOT_DRIVER(pcie_sifive) = {
500 .name = "pcie_sifive",
502 .of_match = pcie_sifive_ids,
503 .ops = &pcie_sifive_ops,
504 .of_to_plat = pcie_sifive_of_to_plat,
505 .probe = pcie_sifive_probe,
506 .priv_auto = sizeof(struct pcie_sifive),