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_pcie_ep_enable(struct uniphier_pcie_ep_priv *priv)
225 ret = clk_prepare_enable(priv->clk);
229 ret = clk_prepare_enable(priv->clk_gio);
231 goto out_clk_disable;
233 ret = reset_control_deassert(priv->rst);
235 goto out_clk_gio_disable;
237 ret = reset_control_deassert(priv->rst_gio);
241 uniphier_pcie_init_ep(priv);
243 uniphier_pcie_phy_reset(priv, true);
245 ret = phy_init(priv->phy);
247 goto out_rst_gio_assert;
249 uniphier_pcie_phy_reset(priv, false);
254 reset_control_assert(priv->rst_gio);
256 reset_control_assert(priv->rst);
258 clk_disable_unprepare(priv->clk_gio);
260 clk_disable_unprepare(priv->clk);
265 static const struct dw_pcie_ops dw_pcie_ops = {
266 .start_link = uniphier_pcie_start_link,
267 .stop_link = uniphier_pcie_stop_link,
270 static int uniphier_pcie_ep_probe(struct platform_device *pdev)
272 struct device *dev = &pdev->dev;
273 struct uniphier_pcie_ep_priv *priv;
276 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
280 priv->features = of_device_get_match_data(dev);
281 if (WARN_ON(!priv->features))
285 priv->pci.ops = &dw_pcie_ops;
287 priv->base = devm_platform_ioremap_resource_byname(pdev, "link");
288 if (IS_ERR(priv->base))
289 return PTR_ERR(priv->base);
291 priv->clk_gio = devm_clk_get(dev, "gio");
292 if (IS_ERR(priv->clk_gio))
293 return PTR_ERR(priv->clk_gio);
295 priv->rst_gio = devm_reset_control_get_shared(dev, "gio");
296 if (IS_ERR(priv->rst_gio))
297 return PTR_ERR(priv->rst_gio);
299 priv->clk = devm_clk_get(dev, "link");
300 if (IS_ERR(priv->clk))
301 return PTR_ERR(priv->clk);
303 priv->rst = devm_reset_control_get_shared(dev, "link");
304 if (IS_ERR(priv->rst))
305 return PTR_ERR(priv->rst);
307 priv->phy = devm_phy_optional_get(dev, "pcie-phy");
308 if (IS_ERR(priv->phy)) {
309 ret = PTR_ERR(priv->phy);
310 dev_err(dev, "Failed to get phy (%d)\n", ret);
314 platform_set_drvdata(pdev, priv);
316 ret = uniphier_pcie_ep_enable(priv);
320 priv->pci.ep.ops = &uniphier_pcie_ep_ops;
321 return dw_pcie_ep_init(&priv->pci.ep);
324 static const struct pci_epc_features uniphier_pro5_data = {
325 .linkup_notifier = false,
327 .msix_capable = false,
329 .bar_fixed_64bit = BIT(BAR_0) | BIT(BAR_2) | BIT(BAR_4),
330 .reserved_bar = BIT(BAR_4),
333 static const struct of_device_id uniphier_pcie_ep_match[] = {
335 .compatible = "socionext,uniphier-pro5-pcie-ep",
336 .data = &uniphier_pro5_data,
341 static struct platform_driver uniphier_pcie_ep_driver = {
342 .probe = uniphier_pcie_ep_probe,
344 .name = "uniphier-pcie-ep",
345 .of_match_table = uniphier_pcie_ep_match,
346 .suppress_bind_attrs = true,
349 builtin_platform_driver(uniphier_pcie_ep_driver);