1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe endpoint controller driver for UniPhier SoCs
4 * Copyright 2018 Socionext Inc.
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/of_device.h>
14 #include <linux/pci.h>
15 #include <linux/phy/phy.h>
16 #include <linux/platform_device.h>
17 #include <linux/reset.h>
19 #include "pcie-designware.h"
21 /* Link Glue registers */
22 #define PCL_RSTCTRL0 0x0010
23 #define PCL_RSTCTRL_AXI_REG BIT(3)
24 #define PCL_RSTCTRL_AXI_SLAVE BIT(2)
25 #define PCL_RSTCTRL_AXI_MASTER BIT(1)
26 #define PCL_RSTCTRL_PIPE3 BIT(0)
28 #define PCL_RSTCTRL1 0x0020
29 #define PCL_RSTCTRL_PERST BIT(0)
31 #define PCL_RSTCTRL2 0x0024
32 #define PCL_RSTCTRL_PHY_RESET BIT(0)
34 #define PCL_MODE 0x8000
35 #define PCL_MODE_REGEN BIT(8)
36 #define PCL_MODE_REGVAL BIT(0)
38 #define PCL_APP_CLK_CTRL 0x8004
39 #define PCL_APP_CLK_REQ BIT(0)
41 #define PCL_APP_READY_CTRL 0x8008
42 #define PCL_APP_LTSSM_ENABLE BIT(0)
44 #define PCL_APP_MSI0 0x8040
45 #define PCL_APP_VEN_MSI_TC_MASK GENMASK(10, 8)
46 #define PCL_APP_VEN_MSI_VECTOR_MASK GENMASK(4, 0)
48 #define PCL_APP_MSI1 0x8044
49 #define PCL_APP_MSI_REQ BIT(0)
51 #define PCL_APP_INTX 0x8074
52 #define PCL_APP_INTX_SYS_INT BIT(0)
54 /* assertion time of INTx in usec */
55 #define PCL_INTX_WIDTH_USEC 30
57 struct uniphier_pcie_ep_priv {
60 struct clk *clk, *clk_gio;
61 struct reset_control *rst, *rst_gio;
63 const struct pci_epc_features *features;
66 #define to_uniphier_pcie(x) dev_get_drvdata((x)->dev)
68 static void uniphier_pcie_ltssm_enable(struct uniphier_pcie_ep_priv *priv,
73 val = readl(priv->base + PCL_APP_READY_CTRL);
75 val |= PCL_APP_LTSSM_ENABLE;
77 val &= ~PCL_APP_LTSSM_ENABLE;
78 writel(val, priv->base + PCL_APP_READY_CTRL);
81 static void uniphier_pcie_phy_reset(struct uniphier_pcie_ep_priv *priv,
86 val = readl(priv->base + PCL_RSTCTRL2);
88 val |= PCL_RSTCTRL_PHY_RESET;
90 val &= ~PCL_RSTCTRL_PHY_RESET;
91 writel(val, priv->base + PCL_RSTCTRL2);
94 static void uniphier_pcie_init_ep(struct uniphier_pcie_ep_priv *priv)
99 val = readl(priv->base + PCL_MODE);
100 val |= PCL_MODE_REGEN | PCL_MODE_REGVAL;
101 writel(val, priv->base + PCL_MODE);
104 val = readl(priv->base + PCL_APP_CLK_CTRL);
105 val &= ~PCL_APP_CLK_REQ;
106 writel(val, priv->base + PCL_APP_CLK_CTRL);
108 /* deassert PIPE3 and AXI reset */
109 val = readl(priv->base + PCL_RSTCTRL0);
110 val |= PCL_RSTCTRL_AXI_REG | PCL_RSTCTRL_AXI_SLAVE
111 | PCL_RSTCTRL_AXI_MASTER | PCL_RSTCTRL_PIPE3;
112 writel(val, priv->base + PCL_RSTCTRL0);
114 uniphier_pcie_ltssm_enable(priv, false);
119 static int uniphier_pcie_start_link(struct dw_pcie *pci)
121 struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
123 uniphier_pcie_ltssm_enable(priv, true);
128 static void uniphier_pcie_stop_link(struct dw_pcie *pci)
130 struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
132 uniphier_pcie_ltssm_enable(priv, false);
135 static void uniphier_pcie_ep_init(struct dw_pcie_ep *ep)
137 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
140 for (bar = BAR_0; bar <= BAR_5; bar++)
141 dw_pcie_ep_reset_bar(pci, bar);
144 static int uniphier_pcie_ep_raise_legacy_irq(struct dw_pcie_ep *ep)
146 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
147 struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
151 * This makes pulse signal to send INTx to the RC, so this should
152 * be cleared as soon as possible. This sequence is covered with
153 * mutex in pci_epc_raise_irq().
156 val = readl(priv->base + PCL_APP_INTX);
157 val |= PCL_APP_INTX_SYS_INT;
158 writel(val, priv->base + PCL_APP_INTX);
160 udelay(PCL_INTX_WIDTH_USEC);
163 val &= ~PCL_APP_INTX_SYS_INT;
164 writel(val, priv->base + PCL_APP_INTX);
169 static int uniphier_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep,
170 u8 func_no, u16 interrupt_num)
172 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
173 struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
176 val = FIELD_PREP(PCL_APP_VEN_MSI_TC_MASK, func_no)
177 | FIELD_PREP(PCL_APP_VEN_MSI_VECTOR_MASK, interrupt_num - 1);
178 writel(val, priv->base + PCL_APP_MSI0);
180 val = readl(priv->base + PCL_APP_MSI1);
181 val |= PCL_APP_MSI_REQ;
182 writel(val, priv->base + PCL_APP_MSI1);
187 static int uniphier_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
188 enum pci_epc_irq_type type,
191 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
194 case PCI_EPC_IRQ_LEGACY:
195 return uniphier_pcie_ep_raise_legacy_irq(ep);
196 case PCI_EPC_IRQ_MSI:
197 return uniphier_pcie_ep_raise_msi_irq(ep, func_no,
200 dev_err(pci->dev, "UNKNOWN IRQ type (%d)\n", type);
206 static const struct pci_epc_features*
207 uniphier_pcie_get_features(struct dw_pcie_ep *ep)
209 struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
210 struct uniphier_pcie_ep_priv *priv = to_uniphier_pcie(pci);
212 return priv->features;
215 static const struct dw_pcie_ep_ops uniphier_pcie_ep_ops = {
216 .ep_init = uniphier_pcie_ep_init,
217 .raise_irq = uniphier_pcie_ep_raise_irq,
218 .get_features = uniphier_pcie_get_features,
221 static int uniphier_add_pcie_ep(struct uniphier_pcie_ep_priv *priv,
222 struct platform_device *pdev)
224 struct dw_pcie *pci = &priv->pci;
225 struct dw_pcie_ep *ep = &pci->ep;
226 struct device *dev = &pdev->dev;
227 struct resource *res;
230 ep->ops = &uniphier_pcie_ep_ops;
232 pci->dbi_base2 = devm_platform_ioremap_resource_byname(pdev, "dbi2");
233 if (IS_ERR(pci->dbi_base2))
234 return PTR_ERR(pci->dbi_base2);
236 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
240 ep->phys_base = res->start;
241 ep->addr_size = resource_size(res);
243 ret = dw_pcie_ep_init(ep);
245 dev_err(dev, "Failed to initialize endpoint (%d)\n", ret);
250 static int uniphier_pcie_ep_enable(struct uniphier_pcie_ep_priv *priv)
254 ret = clk_prepare_enable(priv->clk);
258 ret = clk_prepare_enable(priv->clk_gio);
260 goto out_clk_disable;
262 ret = reset_control_deassert(priv->rst);
264 goto out_clk_gio_disable;
266 ret = reset_control_deassert(priv->rst_gio);
270 uniphier_pcie_init_ep(priv);
272 uniphier_pcie_phy_reset(priv, true);
274 ret = phy_init(priv->phy);
276 goto out_rst_gio_assert;
278 uniphier_pcie_phy_reset(priv, false);
283 reset_control_assert(priv->rst_gio);
285 reset_control_assert(priv->rst);
287 clk_disable_unprepare(priv->clk_gio);
289 clk_disable_unprepare(priv->clk);
294 static const struct dw_pcie_ops dw_pcie_ops = {
295 .start_link = uniphier_pcie_start_link,
296 .stop_link = uniphier_pcie_stop_link,
299 static int uniphier_pcie_ep_probe(struct platform_device *pdev)
301 struct device *dev = &pdev->dev;
302 struct uniphier_pcie_ep_priv *priv;
303 struct resource *res;
306 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
310 priv->features = of_device_get_match_data(dev);
311 if (WARN_ON(!priv->features))
315 priv->pci.ops = &dw_pcie_ops;
317 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
318 priv->pci.dbi_base = devm_pci_remap_cfg_resource(dev, res);
319 if (IS_ERR(priv->pci.dbi_base))
320 return PTR_ERR(priv->pci.dbi_base);
322 priv->base = devm_platform_ioremap_resource_byname(pdev, "link");
323 if (IS_ERR(priv->base))
324 return PTR_ERR(priv->base);
326 priv->clk_gio = devm_clk_get(dev, "gio");
327 if (IS_ERR(priv->clk_gio))
328 return PTR_ERR(priv->clk_gio);
330 priv->rst_gio = devm_reset_control_get_shared(dev, "gio");
331 if (IS_ERR(priv->rst_gio))
332 return PTR_ERR(priv->rst_gio);
334 priv->clk = devm_clk_get(dev, "link");
335 if (IS_ERR(priv->clk))
336 return PTR_ERR(priv->clk);
338 priv->rst = devm_reset_control_get_shared(dev, "link");
339 if (IS_ERR(priv->rst))
340 return PTR_ERR(priv->rst);
342 priv->phy = devm_phy_optional_get(dev, "pcie-phy");
343 if (IS_ERR(priv->phy)) {
344 ret = PTR_ERR(priv->phy);
345 dev_err(dev, "Failed to get phy (%d)\n", ret);
349 platform_set_drvdata(pdev, priv);
351 ret = uniphier_pcie_ep_enable(priv);
355 return uniphier_add_pcie_ep(priv, pdev);
358 static const struct pci_epc_features uniphier_pro5_data = {
359 .linkup_notifier = false,
361 .msix_capable = false,
363 .bar_fixed_64bit = BIT(BAR_0) | BIT(BAR_2) | BIT(BAR_4),
364 .reserved_bar = BIT(BAR_4),
367 static const struct of_device_id uniphier_pcie_ep_match[] = {
369 .compatible = "socionext,uniphier-pro5-pcie-ep",
370 .data = &uniphier_pro5_data,
375 static struct platform_driver uniphier_pcie_ep_driver = {
376 .probe = uniphier_pcie_ep_probe,
378 .name = "uniphier-pcie-ep",
379 .of_match_table = uniphier_pcie_ep_match,
380 .suppress_bind_attrs = true,
383 builtin_platform_driver(uniphier_pcie_ep_driver);