1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for UniPhier SoCs
4 * Copyright 2018 Socionext Inc.
5 * Author: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
8 #include <linux/bitops.h>
9 #include <linux/bitfield.h>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/iopoll.h>
15 #include <linux/irqchip/chained_irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/of_irq.h>
18 #include <linux/pci.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_device.h>
21 #include <linux/reset.h>
23 #include "pcie-designware.h"
25 #define PCL_PINCTRL0 0x002c
26 #define PCL_PERST_PLDN_REGEN BIT(12)
27 #define PCL_PERST_NOE_REGEN BIT(11)
28 #define PCL_PERST_OUT_REGEN BIT(8)
29 #define PCL_PERST_PLDN_REGVAL BIT(4)
30 #define PCL_PERST_NOE_REGVAL BIT(3)
31 #define PCL_PERST_OUT_REGVAL BIT(0)
33 #define PCL_PIPEMON 0x0044
34 #define PCL_PCLK_ALIVE BIT(15)
36 #define PCL_MODE 0x8000
37 #define PCL_MODE_REGEN BIT(8)
38 #define PCL_MODE_REGVAL BIT(0)
40 #define PCL_APP_READY_CTRL 0x8008
41 #define PCL_APP_LTSSM_ENABLE BIT(0)
43 #define PCL_APP_PM0 0x8078
44 #define PCL_SYS_AUX_PWR_DET BIT(8)
46 #define PCL_RCV_INT 0x8108
47 #define PCL_RCV_INT_ALL_ENABLE GENMASK(20, 17)
48 #define PCL_CFG_BW_MGT_STATUS BIT(4)
49 #define PCL_CFG_LINK_AUTO_BW_STATUS BIT(3)
50 #define PCL_CFG_AER_RC_ERR_MSI_STATUS BIT(2)
51 #define PCL_CFG_PME_MSI_STATUS BIT(1)
53 #define PCL_RCV_INTX 0x810c
54 #define PCL_RCV_INTX_ALL_ENABLE GENMASK(19, 16)
55 #define PCL_RCV_INTX_ALL_MASK GENMASK(11, 8)
56 #define PCL_RCV_INTX_MASK_SHIFT 8
57 #define PCL_RCV_INTX_ALL_STATUS GENMASK(3, 0)
58 #define PCL_RCV_INTX_STATUS_SHIFT 0
60 #define PCL_STATUS_LINK 0x8140
61 #define PCL_RDLH_LINK_UP BIT(1)
62 #define PCL_XMLH_LINK_UP BIT(0)
64 struct uniphier_pcie_priv {
68 struct reset_control *rst;
70 struct irq_domain *legacy_irq_domain;
73 #define to_uniphier_pcie(x) dev_get_drvdata((x)->dev)
75 static void uniphier_pcie_ltssm_enable(struct uniphier_pcie_priv *priv,
80 val = readl(priv->base + PCL_APP_READY_CTRL);
82 val |= PCL_APP_LTSSM_ENABLE;
84 val &= ~PCL_APP_LTSSM_ENABLE;
85 writel(val, priv->base + PCL_APP_READY_CTRL);
88 static void uniphier_pcie_init_rc(struct uniphier_pcie_priv *priv)
93 val = readl(priv->base + PCL_MODE);
94 val |= PCL_MODE_REGEN;
95 val &= ~PCL_MODE_REGVAL;
96 writel(val, priv->base + PCL_MODE);
98 /* use auxiliary power detection */
99 val = readl(priv->base + PCL_APP_PM0);
100 val |= PCL_SYS_AUX_PWR_DET;
101 writel(val, priv->base + PCL_APP_PM0);
104 val = readl(priv->base + PCL_PINCTRL0);
105 val &= ~(PCL_PERST_NOE_REGVAL | PCL_PERST_OUT_REGVAL
106 | PCL_PERST_PLDN_REGVAL);
107 val |= PCL_PERST_NOE_REGEN | PCL_PERST_OUT_REGEN
108 | PCL_PERST_PLDN_REGEN;
109 writel(val, priv->base + PCL_PINCTRL0);
111 uniphier_pcie_ltssm_enable(priv, false);
113 usleep_range(100000, 200000);
115 /* deassert PERST# */
116 val = readl(priv->base + PCL_PINCTRL0);
117 val |= PCL_PERST_OUT_REGVAL | PCL_PERST_OUT_REGEN;
118 writel(val, priv->base + PCL_PINCTRL0);
121 static int uniphier_pcie_wait_rc(struct uniphier_pcie_priv *priv)
126 /* wait PIPE clock */
127 ret = readl_poll_timeout(priv->base + PCL_PIPEMON, status,
128 status & PCL_PCLK_ALIVE, 100000, 1000000);
130 dev_err(priv->pci.dev,
131 "Failed to initialize controller in RC mode\n");
138 static int uniphier_pcie_link_up(struct dw_pcie *pci)
140 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
143 val = readl(priv->base + PCL_STATUS_LINK);
144 mask = PCL_RDLH_LINK_UP | PCL_XMLH_LINK_UP;
146 return (val & mask) == mask;
149 static int uniphier_pcie_establish_link(struct dw_pcie *pci)
151 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
153 if (dw_pcie_link_up(pci))
156 uniphier_pcie_ltssm_enable(priv, true);
158 return dw_pcie_wait_for_link(pci);
161 static void uniphier_pcie_stop_link(struct dw_pcie *pci)
163 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
165 uniphier_pcie_ltssm_enable(priv, false);
168 static void uniphier_pcie_irq_enable(struct uniphier_pcie_priv *priv)
170 writel(PCL_RCV_INT_ALL_ENABLE, priv->base + PCL_RCV_INT);
171 writel(PCL_RCV_INTX_ALL_ENABLE, priv->base + PCL_RCV_INTX);
174 static void uniphier_pcie_irq_mask(struct irq_data *d)
176 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
177 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
178 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
182 raw_spin_lock_irqsave(&pp->lock, flags);
184 val = readl(priv->base + PCL_RCV_INTX);
185 val |= BIT(irqd_to_hwirq(d) + PCL_RCV_INTX_MASK_SHIFT);
186 writel(val, priv->base + PCL_RCV_INTX);
188 raw_spin_unlock_irqrestore(&pp->lock, flags);
191 static void uniphier_pcie_irq_unmask(struct irq_data *d)
193 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
194 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
195 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
199 raw_spin_lock_irqsave(&pp->lock, flags);
201 val = readl(priv->base + PCL_RCV_INTX);
202 val &= ~BIT(irqd_to_hwirq(d) + PCL_RCV_INTX_MASK_SHIFT);
203 writel(val, priv->base + PCL_RCV_INTX);
205 raw_spin_unlock_irqrestore(&pp->lock, flags);
208 static struct irq_chip uniphier_pcie_irq_chip = {
210 .irq_mask = uniphier_pcie_irq_mask,
211 .irq_unmask = uniphier_pcie_irq_unmask,
214 static int uniphier_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
215 irq_hw_number_t hwirq)
217 irq_set_chip_and_handler(irq, &uniphier_pcie_irq_chip,
219 irq_set_chip_data(irq, domain->host_data);
224 static const struct irq_domain_ops uniphier_intx_domain_ops = {
225 .map = uniphier_pcie_intx_map,
228 static void uniphier_pcie_irq_handler(struct irq_desc *desc)
230 struct pcie_port *pp = irq_desc_get_handler_data(desc);
231 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
232 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
233 struct irq_chip *chip = irq_desc_get_chip(desc);
238 val = readl(priv->base + PCL_RCV_INT);
240 if (val & PCL_CFG_BW_MGT_STATUS)
241 dev_dbg(pci->dev, "Link Bandwidth Management Event\n");
242 if (val & PCL_CFG_LINK_AUTO_BW_STATUS)
243 dev_dbg(pci->dev, "Link Autonomous Bandwidth Event\n");
244 if (val & PCL_CFG_AER_RC_ERR_MSI_STATUS)
245 dev_dbg(pci->dev, "Root Error\n");
246 if (val & PCL_CFG_PME_MSI_STATUS)
247 dev_dbg(pci->dev, "PME Interrupt\n");
249 writel(val, priv->base + PCL_RCV_INT);
252 chained_irq_enter(chip, desc);
254 val = readl(priv->base + PCL_RCV_INTX);
255 reg = FIELD_GET(PCL_RCV_INTX_ALL_STATUS, val);
257 for_each_set_bit(bit, ®, PCI_NUM_INTX) {
258 virq = irq_linear_revmap(priv->legacy_irq_domain, bit);
259 generic_handle_irq(virq);
262 chained_irq_exit(chip, desc);
265 static int uniphier_pcie_config_legacy_irq(struct pcie_port *pp)
267 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
268 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
269 struct device_node *np = pci->dev->of_node;
270 struct device_node *np_intc;
273 np_intc = of_get_child_by_name(np, "legacy-interrupt-controller");
275 dev_err(pci->dev, "Failed to get legacy-interrupt-controller node\n");
279 pp->irq = irq_of_parse_and_map(np_intc, 0);
281 dev_err(pci->dev, "Failed to get an IRQ entry in legacy-interrupt-controller\n");
286 priv->legacy_irq_domain = irq_domain_add_linear(np_intc, PCI_NUM_INTX,
287 &uniphier_intx_domain_ops, pp);
288 if (!priv->legacy_irq_domain) {
289 dev_err(pci->dev, "Failed to get INTx domain\n");
294 irq_set_chained_handler_and_data(pp->irq, uniphier_pcie_irq_handler,
298 of_node_put(np_intc);
302 static int uniphier_pcie_host_init(struct pcie_port *pp)
304 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
305 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
308 ret = uniphier_pcie_config_legacy_irq(pp);
312 uniphier_pcie_irq_enable(priv);
314 dw_pcie_setup_rc(pp);
315 ret = uniphier_pcie_establish_link(pci);
319 dw_pcie_msi_init(pp);
324 static const struct dw_pcie_host_ops uniphier_pcie_host_ops = {
325 .host_init = uniphier_pcie_host_init,
328 static int uniphier_add_pcie_port(struct uniphier_pcie_priv *priv,
329 struct platform_device *pdev)
331 struct dw_pcie *pci = &priv->pci;
332 struct pcie_port *pp = &pci->pp;
333 struct device *dev = &pdev->dev;
336 pp->ops = &uniphier_pcie_host_ops;
338 if (IS_ENABLED(CONFIG_PCI_MSI)) {
339 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
344 ret = dw_pcie_host_init(pp);
346 dev_err(dev, "Failed to initialize host (%d)\n", ret);
353 static int uniphier_pcie_host_enable(struct uniphier_pcie_priv *priv)
357 ret = clk_prepare_enable(priv->clk);
361 ret = reset_control_deassert(priv->rst);
363 goto out_clk_disable;
365 uniphier_pcie_init_rc(priv);
367 ret = phy_init(priv->phy);
371 ret = uniphier_pcie_wait_rc(priv);
380 reset_control_assert(priv->rst);
382 clk_disable_unprepare(priv->clk);
387 static const struct dw_pcie_ops dw_pcie_ops = {
388 .start_link = uniphier_pcie_establish_link,
389 .stop_link = uniphier_pcie_stop_link,
390 .link_up = uniphier_pcie_link_up,
393 static int uniphier_pcie_probe(struct platform_device *pdev)
395 struct device *dev = &pdev->dev;
396 struct uniphier_pcie_priv *priv;
397 struct resource *res;
400 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
405 priv->pci.ops = &dw_pcie_ops;
407 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
408 priv->pci.dbi_base = devm_pci_remap_cfg_resource(dev, res);
409 if (IS_ERR(priv->pci.dbi_base))
410 return PTR_ERR(priv->pci.dbi_base);
412 priv->base = devm_platform_ioremap_resource_byname(pdev, "link");
413 if (IS_ERR(priv->base))
414 return PTR_ERR(priv->base);
416 priv->clk = devm_clk_get(dev, NULL);
417 if (IS_ERR(priv->clk))
418 return PTR_ERR(priv->clk);
420 priv->rst = devm_reset_control_get_shared(dev, NULL);
421 if (IS_ERR(priv->rst))
422 return PTR_ERR(priv->rst);
424 priv->phy = devm_phy_optional_get(dev, "pcie-phy");
425 if (IS_ERR(priv->phy))
426 return PTR_ERR(priv->phy);
428 platform_set_drvdata(pdev, priv);
430 ret = uniphier_pcie_host_enable(priv);
434 return uniphier_add_pcie_port(priv, pdev);
437 static const struct of_device_id uniphier_pcie_match[] = {
438 { .compatible = "socionext,uniphier-pcie", },
442 static struct platform_driver uniphier_pcie_driver = {
443 .probe = uniphier_pcie_probe,
445 .name = "uniphier-pcie",
446 .of_match_table = uniphier_pcie_match,
449 builtin_platform_driver(uniphier_pcie_driver);