1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Rockchip SoCs.
5 * Copyright (C) 2021 Rockchip Electronics Co., Ltd.
6 * http://www.rock-chips.com
11 #include <linux/clk.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/irqchip/chained_irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/of_irq.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/reset.h>
24 #include "pcie-designware.h"
27 * The upper 16 bits of PCIE_CLIENT_CONFIG are a write
28 * mask for the lower 16 bits.
30 #define HIWORD_UPDATE(mask, val) (((mask) << 16) | (val))
31 #define HIWORD_UPDATE_BIT(val) HIWORD_UPDATE(val, val)
32 #define HIWORD_DISABLE_BIT(val) HIWORD_UPDATE(val, ~val)
34 #define to_rockchip_pcie(x) dev_get_drvdata((x)->dev)
36 #define PCIE_CLIENT_RC_MODE HIWORD_UPDATE_BIT(0x40)
37 #define PCIE_CLIENT_ENABLE_LTSSM HIWORD_UPDATE_BIT(0xc)
38 #define PCIE_SMLH_LINKUP BIT(16)
39 #define PCIE_RDLH_LINKUP BIT(17)
40 #define PCIE_LINKUP (PCIE_SMLH_LINKUP | PCIE_RDLH_LINKUP)
41 #define PCIE_L0S_ENTRY 0x11
42 #define PCIE_CLIENT_GENERAL_CONTROL 0x0
43 #define PCIE_CLIENT_INTR_STATUS_LEGACY 0x8
44 #define PCIE_CLIENT_INTR_MASK_LEGACY 0x1c
45 #define PCIE_CLIENT_GENERAL_DEBUG 0x104
46 #define PCIE_CLIENT_HOT_RESET_CTRL 0x180
47 #define PCIE_CLIENT_LTSSM_STATUS 0x300
48 #define PCIE_LTSSM_ENABLE_ENHANCE BIT(4)
49 #define PCIE_LTSSM_STATUS_MASK GENMASK(5, 0)
51 struct rockchip_pcie {
53 void __iomem *apb_base;
55 struct clk_bulk_data *clks;
57 struct reset_control *rst;
58 struct gpio_desc *rst_gpio;
59 struct regulator *vpcie3v3;
60 struct irq_domain *irq_domain;
63 static int rockchip_pcie_readl_apb(struct rockchip_pcie *rockchip,
66 return readl_relaxed(rockchip->apb_base + reg);
69 static void rockchip_pcie_writel_apb(struct rockchip_pcie *rockchip,
72 writel_relaxed(val, rockchip->apb_base + reg);
75 static void rockchip_pcie_legacy_int_handler(struct irq_desc *desc)
77 struct irq_chip *chip = irq_desc_get_chip(desc);
78 struct rockchip_pcie *rockchip = irq_desc_get_handler_data(desc);
79 unsigned long reg, hwirq;
81 chained_irq_enter(chip, desc);
83 reg = rockchip_pcie_readl_apb(rockchip, PCIE_CLIENT_INTR_STATUS_LEGACY);
85 for_each_set_bit(hwirq, ®, 4)
86 generic_handle_domain_irq(rockchip->irq_domain, hwirq);
88 chained_irq_exit(chip, desc);
91 static void rockchip_intx_mask(struct irq_data *data)
93 rockchip_pcie_writel_apb(irq_data_get_irq_chip_data(data),
94 HIWORD_UPDATE_BIT(BIT(data->hwirq)),
95 PCIE_CLIENT_INTR_MASK_LEGACY);
98 static void rockchip_intx_unmask(struct irq_data *data)
100 rockchip_pcie_writel_apb(irq_data_get_irq_chip_data(data),
101 HIWORD_DISABLE_BIT(BIT(data->hwirq)),
102 PCIE_CLIENT_INTR_MASK_LEGACY);
105 static struct irq_chip rockchip_intx_irq_chip = {
107 .irq_mask = rockchip_intx_mask,
108 .irq_unmask = rockchip_intx_unmask,
109 .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND,
112 static int rockchip_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
113 irq_hw_number_t hwirq)
115 irq_set_chip_and_handler(irq, &rockchip_intx_irq_chip, handle_level_irq);
116 irq_set_chip_data(irq, domain->host_data);
121 static const struct irq_domain_ops intx_domain_ops = {
122 .map = rockchip_pcie_intx_map,
125 static int rockchip_pcie_init_irq_domain(struct rockchip_pcie *rockchip)
127 struct device *dev = rockchip->pci.dev;
128 struct device_node *intc;
130 intc = of_get_child_by_name(dev->of_node, "legacy-interrupt-controller");
132 dev_err(dev, "missing child interrupt-controller node\n");
136 rockchip->irq_domain = irq_domain_add_linear(intc, PCI_NUM_INTX,
137 &intx_domain_ops, rockchip);
139 if (!rockchip->irq_domain) {
140 dev_err(dev, "failed to get a INTx IRQ domain\n");
147 static void rockchip_pcie_enable_ltssm(struct rockchip_pcie *rockchip)
149 rockchip_pcie_writel_apb(rockchip, PCIE_CLIENT_ENABLE_LTSSM,
150 PCIE_CLIENT_GENERAL_CONTROL);
153 static int rockchip_pcie_link_up(struct dw_pcie *pci)
155 struct rockchip_pcie *rockchip = to_rockchip_pcie(pci);
156 u32 val = rockchip_pcie_readl_apb(rockchip, PCIE_CLIENT_LTSSM_STATUS);
158 if ((val & PCIE_LINKUP) == PCIE_LINKUP &&
159 (val & PCIE_LTSSM_STATUS_MASK) == PCIE_L0S_ENTRY)
165 static int rockchip_pcie_start_link(struct dw_pcie *pci)
167 struct rockchip_pcie *rockchip = to_rockchip_pcie(pci);
170 gpiod_set_value_cansleep(rockchip->rst_gpio, 0);
172 rockchip_pcie_enable_ltssm(rockchip);
175 * PCIe requires the refclk to be stable for 100µs prior to releasing
176 * PERST. See table 2-4 in section 2.6.2 AC Specifications of the PCI
177 * Express Card Electromechanical Specification, 1.1. However, we don't
178 * know if the refclk is coming from RC's PHY or external OSC. If it's
179 * from RC, so enabling LTSSM is the just right place to release #PERST.
180 * We need more extra time as before, rather than setting just
181 * 100us as we don't know how long should the device need to reset.
184 gpiod_set_value_cansleep(rockchip->rst_gpio, 1);
189 static int rockchip_pcie_host_init(struct dw_pcie_rp *pp)
191 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
192 struct rockchip_pcie *rockchip = to_rockchip_pcie(pci);
193 struct device *dev = rockchip->pci.dev;
194 u32 val = HIWORD_UPDATE_BIT(PCIE_LTSSM_ENABLE_ENHANCE);
197 irq = of_irq_get_byname(dev->of_node, "legacy");
201 ret = rockchip_pcie_init_irq_domain(rockchip);
203 dev_err(dev, "failed to init irq domain\n");
205 irq_set_chained_handler_and_data(irq, rockchip_pcie_legacy_int_handler,
208 /* LTSSM enable control mode */
209 rockchip_pcie_writel_apb(rockchip, val, PCIE_CLIENT_HOT_RESET_CTRL);
211 rockchip_pcie_writel_apb(rockchip, PCIE_CLIENT_RC_MODE,
212 PCIE_CLIENT_GENERAL_CONTROL);
217 static const struct dw_pcie_host_ops rockchip_pcie_host_ops = {
218 .host_init = rockchip_pcie_host_init,
221 static int rockchip_pcie_clk_init(struct rockchip_pcie *rockchip)
223 struct device *dev = rockchip->pci.dev;
226 ret = devm_clk_bulk_get_all(dev, &rockchip->clks);
230 rockchip->clk_cnt = ret;
232 return clk_bulk_prepare_enable(rockchip->clk_cnt, rockchip->clks);
235 static int rockchip_pcie_resource_get(struct platform_device *pdev,
236 struct rockchip_pcie *rockchip)
238 rockchip->apb_base = devm_platform_ioremap_resource_byname(pdev, "apb");
239 if (IS_ERR(rockchip->apb_base))
240 return PTR_ERR(rockchip->apb_base);
242 rockchip->rst_gpio = devm_gpiod_get_optional(&pdev->dev, "reset",
244 if (IS_ERR(rockchip->rst_gpio))
245 return PTR_ERR(rockchip->rst_gpio);
247 rockchip->rst = devm_reset_control_array_get_exclusive(&pdev->dev);
248 if (IS_ERR(rockchip->rst))
249 return dev_err_probe(&pdev->dev, PTR_ERR(rockchip->rst),
250 "failed to get reset lines\n");
255 static int rockchip_pcie_phy_init(struct rockchip_pcie *rockchip)
257 struct device *dev = rockchip->pci.dev;
260 rockchip->phy = devm_phy_get(dev, "pcie-phy");
261 if (IS_ERR(rockchip->phy))
262 return dev_err_probe(dev, PTR_ERR(rockchip->phy),
265 ret = phy_init(rockchip->phy);
269 ret = phy_power_on(rockchip->phy);
271 phy_exit(rockchip->phy);
276 static void rockchip_pcie_phy_deinit(struct rockchip_pcie *rockchip)
278 phy_exit(rockchip->phy);
279 phy_power_off(rockchip->phy);
282 static const struct dw_pcie_ops dw_pcie_ops = {
283 .link_up = rockchip_pcie_link_up,
284 .start_link = rockchip_pcie_start_link,
287 static int rockchip_pcie_probe(struct platform_device *pdev)
289 struct device *dev = &pdev->dev;
290 struct rockchip_pcie *rockchip;
291 struct dw_pcie_rp *pp;
294 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
298 platform_set_drvdata(pdev, rockchip);
300 rockchip->pci.dev = dev;
301 rockchip->pci.ops = &dw_pcie_ops;
303 pp = &rockchip->pci.pp;
304 pp->ops = &rockchip_pcie_host_ops;
306 ret = rockchip_pcie_resource_get(pdev, rockchip);
310 ret = reset_control_assert(rockchip->rst);
314 /* DON'T MOVE ME: must be enable before PHY init */
315 rockchip->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3");
316 if (IS_ERR(rockchip->vpcie3v3)) {
317 if (PTR_ERR(rockchip->vpcie3v3) != -ENODEV)
318 return dev_err_probe(dev, PTR_ERR(rockchip->vpcie3v3),
319 "failed to get vpcie3v3 regulator\n");
320 rockchip->vpcie3v3 = NULL;
322 ret = regulator_enable(rockchip->vpcie3v3);
324 dev_err(dev, "failed to enable vpcie3v3 regulator\n");
329 ret = rockchip_pcie_phy_init(rockchip);
331 goto disable_regulator;
333 ret = reset_control_deassert(rockchip->rst);
337 ret = rockchip_pcie_clk_init(rockchip);
341 ret = dw_pcie_host_init(pp);
345 clk_bulk_disable_unprepare(rockchip->clk_cnt, rockchip->clks);
347 rockchip_pcie_phy_deinit(rockchip);
349 if (rockchip->vpcie3v3)
350 regulator_disable(rockchip->vpcie3v3);
355 static const struct of_device_id rockchip_pcie_of_match[] = {
356 { .compatible = "rockchip,rk3568-pcie", },
360 static struct platform_driver rockchip_pcie_driver = {
362 .name = "rockchip-dw-pcie",
363 .of_match_table = rockchip_pcie_of_match,
364 .suppress_bind_attrs = true,
366 .probe = rockchip_pcie_probe,
368 builtin_platform_driver(rockchip_pcie_driver);