treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / pci / controller / dwc / pcie-uniphier.c
bloba5401a0b1e58713bdfdb3aa26cd210837571f75b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCIe host controller driver for UniPhier SoCs
4 * Copyright 2018 Socionext Inc.
5 * Author: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
6 */
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 {
65 void __iomem *base;
66 struct dw_pcie pci;
67 struct clk *clk;
68 struct reset_control *rst;
69 struct phy *phy;
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,
76 bool enable)
78 u32 val;
80 val = readl(priv->base + PCL_APP_READY_CTRL);
81 if (enable)
82 val |= PCL_APP_LTSSM_ENABLE;
83 else
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)
90 u32 val;
92 /* set RC MODE */
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);
103 /* assert PERST# */
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)
123 u32 status;
124 int ret;
126 /* wait PIPE clock */
127 ret = readl_poll_timeout(priv->base + PCL_PIPEMON, status,
128 status & PCL_PCLK_ALIVE, 100000, 1000000);
129 if (ret) {
130 dev_err(priv->pci.dev,
131 "Failed to initialize controller in RC mode\n");
132 return ret;
135 return 0;
138 static int uniphier_pcie_link_up(struct dw_pcie *pci)
140 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
141 u32 val, mask;
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))
154 return 0;
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_ack(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);
179 u32 val;
181 val = readl(priv->base + PCL_RCV_INTX);
182 val &= ~PCL_RCV_INTX_ALL_STATUS;
183 val |= BIT(irqd_to_hwirq(d) + PCL_RCV_INTX_STATUS_SHIFT);
184 writel(val, priv->base + PCL_RCV_INTX);
187 static void uniphier_pcie_irq_mask(struct irq_data *d)
189 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
190 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
191 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
192 u32 val;
194 val = readl(priv->base + PCL_RCV_INTX);
195 val &= ~PCL_RCV_INTX_ALL_MASK;
196 val |= BIT(irqd_to_hwirq(d) + PCL_RCV_INTX_MASK_SHIFT);
197 writel(val, priv->base + PCL_RCV_INTX);
200 static void uniphier_pcie_irq_unmask(struct irq_data *d)
202 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
203 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
204 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
205 u32 val;
207 val = readl(priv->base + PCL_RCV_INTX);
208 val &= ~PCL_RCV_INTX_ALL_MASK;
209 val &= ~BIT(irqd_to_hwirq(d) + PCL_RCV_INTX_MASK_SHIFT);
210 writel(val, priv->base + PCL_RCV_INTX);
213 static struct irq_chip uniphier_pcie_irq_chip = {
214 .name = "PCI",
215 .irq_ack = uniphier_pcie_irq_ack,
216 .irq_mask = uniphier_pcie_irq_mask,
217 .irq_unmask = uniphier_pcie_irq_unmask,
220 static int uniphier_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
221 irq_hw_number_t hwirq)
223 irq_set_chip_and_handler(irq, &uniphier_pcie_irq_chip,
224 handle_level_irq);
225 irq_set_chip_data(irq, domain->host_data);
227 return 0;
230 static const struct irq_domain_ops uniphier_intx_domain_ops = {
231 .map = uniphier_pcie_intx_map,
234 static void uniphier_pcie_irq_handler(struct irq_desc *desc)
236 struct pcie_port *pp = irq_desc_get_handler_data(desc);
237 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
238 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
239 struct irq_chip *chip = irq_desc_get_chip(desc);
240 unsigned long reg;
241 u32 val, bit, virq;
243 /* INT for debug */
244 val = readl(priv->base + PCL_RCV_INT);
246 if (val & PCL_CFG_BW_MGT_STATUS)
247 dev_dbg(pci->dev, "Link Bandwidth Management Event\n");
248 if (val & PCL_CFG_LINK_AUTO_BW_STATUS)
249 dev_dbg(pci->dev, "Link Autonomous Bandwidth Event\n");
250 if (val & PCL_CFG_AER_RC_ERR_MSI_STATUS)
251 dev_dbg(pci->dev, "Root Error\n");
252 if (val & PCL_CFG_PME_MSI_STATUS)
253 dev_dbg(pci->dev, "PME Interrupt\n");
255 writel(val, priv->base + PCL_RCV_INT);
257 /* INTx */
258 chained_irq_enter(chip, desc);
260 val = readl(priv->base + PCL_RCV_INTX);
261 reg = FIELD_GET(PCL_RCV_INTX_ALL_STATUS, val);
263 for_each_set_bit(bit, &reg, PCI_NUM_INTX) {
264 virq = irq_linear_revmap(priv->legacy_irq_domain, bit);
265 generic_handle_irq(virq);
268 chained_irq_exit(chip, desc);
271 static int uniphier_pcie_config_legacy_irq(struct pcie_port *pp)
273 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
274 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
275 struct device_node *np = pci->dev->of_node;
276 struct device_node *np_intc;
277 int ret = 0;
279 np_intc = of_get_child_by_name(np, "legacy-interrupt-controller");
280 if (!np_intc) {
281 dev_err(pci->dev, "Failed to get legacy-interrupt-controller node\n");
282 return -EINVAL;
285 pp->irq = irq_of_parse_and_map(np_intc, 0);
286 if (!pp->irq) {
287 dev_err(pci->dev, "Failed to get an IRQ entry in legacy-interrupt-controller\n");
288 ret = -EINVAL;
289 goto out_put_node;
292 priv->legacy_irq_domain = irq_domain_add_linear(np_intc, PCI_NUM_INTX,
293 &uniphier_intx_domain_ops, pp);
294 if (!priv->legacy_irq_domain) {
295 dev_err(pci->dev, "Failed to get INTx domain\n");
296 ret = -ENODEV;
297 goto out_put_node;
300 irq_set_chained_handler_and_data(pp->irq, uniphier_pcie_irq_handler,
301 pp);
303 out_put_node:
304 of_node_put(np_intc);
305 return ret;
308 static int uniphier_pcie_host_init(struct pcie_port *pp)
310 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
311 struct uniphier_pcie_priv *priv = to_uniphier_pcie(pci);
312 int ret;
314 ret = uniphier_pcie_config_legacy_irq(pp);
315 if (ret)
316 return ret;
318 uniphier_pcie_irq_enable(priv);
320 dw_pcie_setup_rc(pp);
321 ret = uniphier_pcie_establish_link(pci);
322 if (ret)
323 return ret;
325 if (IS_ENABLED(CONFIG_PCI_MSI))
326 dw_pcie_msi_init(pp);
328 return 0;
331 static const struct dw_pcie_host_ops uniphier_pcie_host_ops = {
332 .host_init = uniphier_pcie_host_init,
335 static int uniphier_add_pcie_port(struct uniphier_pcie_priv *priv,
336 struct platform_device *pdev)
338 struct dw_pcie *pci = &priv->pci;
339 struct pcie_port *pp = &pci->pp;
340 struct device *dev = &pdev->dev;
341 int ret;
343 pp->ops = &uniphier_pcie_host_ops;
345 if (IS_ENABLED(CONFIG_PCI_MSI)) {
346 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
347 if (pp->msi_irq < 0)
348 return pp->msi_irq;
351 ret = dw_pcie_host_init(pp);
352 if (ret) {
353 dev_err(dev, "Failed to initialize host (%d)\n", ret);
354 return ret;
357 return 0;
360 static int uniphier_pcie_host_enable(struct uniphier_pcie_priv *priv)
362 int ret;
364 ret = clk_prepare_enable(priv->clk);
365 if (ret)
366 return ret;
368 ret = reset_control_deassert(priv->rst);
369 if (ret)
370 goto out_clk_disable;
372 uniphier_pcie_init_rc(priv);
374 ret = phy_init(priv->phy);
375 if (ret)
376 goto out_rst_assert;
378 ret = uniphier_pcie_wait_rc(priv);
379 if (ret)
380 goto out_phy_exit;
382 return 0;
384 out_phy_exit:
385 phy_exit(priv->phy);
386 out_rst_assert:
387 reset_control_assert(priv->rst);
388 out_clk_disable:
389 clk_disable_unprepare(priv->clk);
391 return ret;
394 static const struct dw_pcie_ops dw_pcie_ops = {
395 .start_link = uniphier_pcie_establish_link,
396 .stop_link = uniphier_pcie_stop_link,
397 .link_up = uniphier_pcie_link_up,
400 static int uniphier_pcie_probe(struct platform_device *pdev)
402 struct device *dev = &pdev->dev;
403 struct uniphier_pcie_priv *priv;
404 struct resource *res;
405 int ret;
407 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
408 if (!priv)
409 return -ENOMEM;
411 priv->pci.dev = dev;
412 priv->pci.ops = &dw_pcie_ops;
414 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
415 priv->pci.dbi_base = devm_pci_remap_cfg_resource(dev, res);
416 if (IS_ERR(priv->pci.dbi_base))
417 return PTR_ERR(priv->pci.dbi_base);
419 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "link");
420 priv->base = devm_ioremap_resource(dev, res);
421 if (IS_ERR(priv->base))
422 return PTR_ERR(priv->base);
424 priv->clk = devm_clk_get(dev, NULL);
425 if (IS_ERR(priv->clk))
426 return PTR_ERR(priv->clk);
428 priv->rst = devm_reset_control_get_shared(dev, NULL);
429 if (IS_ERR(priv->rst))
430 return PTR_ERR(priv->rst);
432 priv->phy = devm_phy_optional_get(dev, "pcie-phy");
433 if (IS_ERR(priv->phy))
434 return PTR_ERR(priv->phy);
436 platform_set_drvdata(pdev, priv);
438 ret = uniphier_pcie_host_enable(priv);
439 if (ret)
440 return ret;
442 return uniphier_add_pcie_port(priv, pdev);
445 static const struct of_device_id uniphier_pcie_match[] = {
446 { .compatible = "socionext,uniphier-pcie", },
447 { /* sentinel */ },
450 static struct platform_driver uniphier_pcie_driver = {
451 .probe = uniphier_pcie_probe,
452 .driver = {
453 .name = "uniphier-pcie",
454 .of_match_table = uniphier_pcie_match,
457 builtin_platform_driver(uniphier_pcie_driver);