1 From 68b6fec37f4c6fa382da4b76039743c4de89b028 Mon Sep 17 00:00:00 2001
2 From: Hou Zhiqiang <Zhiqiang.Hou@nxp.com>
3 Date: Tue, 25 Jun 2019 09:09:35 +0000
4 Subject: [PATCH] PCI: mobiveil: Add PCIe Gen4 RC driver for NXP Layerscape
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
10 This PCIe controller is based on the Mobiveil GPEX IP, which is
11 compatible with the PCI Express™ Base Specification, Revision 4.0.
13 Signed-off-by: Hou Zhiqiang <Zhiqiang.Hou@nxp.com>
14 Reviewed-by: Minghuan Lian <Minghuan.Lian@nxp.com>
16 drivers/pci/controller/mobiveil/Kconfig | 10 +
17 drivers/pci/controller/mobiveil/Makefile | 1 +
18 .../pci/controller/mobiveil/pcie-layerscape-gen4.c | 274 +++++++++++++++++++++
19 drivers/pci/controller/mobiveil/pcie-mobiveil.h | 16 +-
20 4 files changed, 299 insertions(+), 2 deletions(-)
21 create mode 100644 drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c
23 --- a/drivers/pci/controller/mobiveil/Kconfig
24 +++ b/drivers/pci/controller/mobiveil/Kconfig
25 @@ -21,4 +21,14 @@ config PCIE_MOBIVEIL_PLAT
26 Soft IP. It has up to 8 outbound and inbound windows
27 for address translation and it is a PCIe Gen4 IP.
29 +config PCIE_LAYERSCAPE_GEN4
30 + bool "Freescale Layerscape PCIe Gen4 controller"
32 + depends on OF && (ARM64 || ARCH_LAYERSCAPE)
33 + depends on PCI_MSI_IRQ_DOMAIN
34 + select PCIE_MOBIVEIL_HOST
36 + Say Y here if you want PCIe Gen4 controller support on
37 + Layerscape SoCs. The PCIe controller can work in RC or
38 + EP mode according to RCW[HOST_AGT_PEX] setting.
40 --- a/drivers/pci/controller/mobiveil/Makefile
41 +++ b/drivers/pci/controller/mobiveil/Makefile
43 obj-$(CONFIG_PCIE_MOBIVEIL) += pcie-mobiveil.o
44 obj-$(CONFIG_PCIE_MOBIVEIL_HOST) += pcie-mobiveil-host.o
45 obj-$(CONFIG_PCIE_MOBIVEIL_PLAT) += pcie-mobiveil-plat.o
46 +obj-$(CONFIG_PCIE_LAYERSCAPE_GEN4) += pcie-layerscape-gen4.o
48 +++ b/drivers/pci/controller/mobiveil/pcie-layerscape-gen4.c
50 +// SPDX-License-Identifier: GPL-2.0
52 + * PCIe Gen4 host controller driver for NXP Layerscape SoCs
54 + * Copyright 2019 NXP
56 + * Author: Zhiqiang Hou <Zhiqiang.Hou@nxp.com>
59 +#include <linux/kernel.h>
60 +#include <linux/interrupt.h>
61 +#include <linux/init.h>
62 +#include <linux/of_pci.h>
63 +#include <linux/of_platform.h>
64 +#include <linux/of_irq.h>
65 +#include <linux/of_address.h>
66 +#include <linux/pci.h>
67 +#include <linux/platform_device.h>
68 +#include <linux/resource.h>
69 +#include <linux/mfd/syscon.h>
70 +#include <linux/regmap.h>
72 +#include "pcie-mobiveil.h"
74 +/* LUT and PF control registers */
75 +#define PCIE_LUT_OFF 0x80000
76 +#define PCIE_PF_OFF 0xc0000
77 +#define PCIE_PF_INT_STAT 0x18
78 +#define PF_INT_STAT_PABRST BIT(31)
80 +#define PCIE_PF_DBG 0x7fc
81 +#define PF_DBG_LTSSM_MASK 0x3f
82 +#define PF_DBG_LTSSM_L0 0x2d /* L0 state */
83 +#define PF_DBG_WE BIT(31)
84 +#define PF_DBG_PABR BIT(27)
86 +#define to_ls_pcie_g4(x) platform_get_drvdata((x)->pdev)
89 + struct mobiveil_pcie pci;
90 + struct delayed_work dwork;
94 +static inline u32 ls_pcie_g4_lut_readl(struct ls_pcie_g4 *pcie, u32 off)
96 + return ioread32(pcie->pci.csr_axi_slave_base + PCIE_LUT_OFF + off);
99 +static inline void ls_pcie_g4_lut_writel(struct ls_pcie_g4 *pcie,
102 + iowrite32(val, pcie->pci.csr_axi_slave_base + PCIE_LUT_OFF + off);
105 +static inline u32 ls_pcie_g4_pf_readl(struct ls_pcie_g4 *pcie, u32 off)
107 + return ioread32(pcie->pci.csr_axi_slave_base + PCIE_PF_OFF + off);
110 +static inline void ls_pcie_g4_pf_writel(struct ls_pcie_g4 *pcie,
113 + iowrite32(val, pcie->pci.csr_axi_slave_base + PCIE_PF_OFF + off);
116 +static bool ls_pcie_g4_is_bridge(struct ls_pcie_g4 *pcie)
118 + struct mobiveil_pcie *mv_pci = &pcie->pci;
121 + header_type = csr_readb(mv_pci, PCI_HEADER_TYPE);
122 + header_type &= 0x7f;
124 + return header_type == PCI_HEADER_TYPE_BRIDGE;
127 +static int ls_pcie_g4_link_up(struct mobiveil_pcie *pci)
129 + struct ls_pcie_g4 *pcie = to_ls_pcie_g4(pci);
132 + state = ls_pcie_g4_pf_readl(pcie, PCIE_PF_DBG);
133 + state = state & PF_DBG_LTSSM_MASK;
135 + if (state == PF_DBG_LTSSM_L0)
141 +static void ls_pcie_g4_disable_interrupt(struct ls_pcie_g4 *pcie)
143 + struct mobiveil_pcie *mv_pci = &pcie->pci;
145 + csr_writel(mv_pci, 0, PAB_INTP_AMBA_MISC_ENB);
148 +static void ls_pcie_g4_enable_interrupt(struct ls_pcie_g4 *pcie)
150 + struct mobiveil_pcie *mv_pci = &pcie->pci;
153 + /* Clear the interrupt status */
154 + csr_writel(mv_pci, 0xffffffff, PAB_INTP_AMBA_MISC_STAT);
156 + val = PAB_INTP_INTX_MASK | PAB_INTP_MSI | PAB_INTP_RESET |
157 + PAB_INTP_PCIE_UE | PAB_INTP_IE_PMREDI | PAB_INTP_IE_EC;
158 + csr_writel(mv_pci, val, PAB_INTP_AMBA_MISC_ENB);
161 +static void ls_pcie_g4_reinit_hw(struct ls_pcie_g4 *pcie)
163 + struct mobiveil_pcie *mv_pci = &pcie->pci;
164 + struct device *dev = &mv_pci->pdev->dev;
168 + /* Poll for pab_csb_reset to set and PAB activity to clear */
170 + usleep_range(10, 15);
171 + val = ls_pcie_g4_pf_readl(pcie, PCIE_PF_INT_STAT);
172 + act_stat = csr_readl(mv_pci, PAB_ACTIVITY_STAT);
173 + } while (((val & PF_INT_STAT_PABRST) == 0 || act_stat) && to--);
175 + dev_err(dev, "Poll PABRST&PABACT timeout\n");
179 + /* clear PEX_RESET bit in PEX_PF0_DBG register */
180 + val = ls_pcie_g4_pf_readl(pcie, PCIE_PF_DBG);
182 + ls_pcie_g4_pf_writel(pcie, PCIE_PF_DBG, val);
184 + val = ls_pcie_g4_pf_readl(pcie, PCIE_PF_DBG);
185 + val |= PF_DBG_PABR;
186 + ls_pcie_g4_pf_writel(pcie, PCIE_PF_DBG, val);
188 + val = ls_pcie_g4_pf_readl(pcie, PCIE_PF_DBG);
190 + ls_pcie_g4_pf_writel(pcie, PCIE_PF_DBG, val);
192 + mobiveil_host_init(mv_pci, true);
195 + while (!ls_pcie_g4_link_up(mv_pci) && to--)
196 + usleep_range(200, 250);
198 + dev_err(dev, "PCIe link training timeout\n");
201 +static irqreturn_t ls_pcie_g4_isr(int irq, void *dev_id)
203 + struct ls_pcie_g4 *pcie = (struct ls_pcie_g4 *)dev_id;
204 + struct mobiveil_pcie *mv_pci = &pcie->pci;
207 + val = csr_readl(mv_pci, PAB_INTP_AMBA_MISC_STAT);
211 + if (val & PAB_INTP_RESET) {
212 + ls_pcie_g4_disable_interrupt(pcie);
213 + schedule_delayed_work(&pcie->dwork, msecs_to_jiffies(1));
216 + csr_writel(mv_pci, val, PAB_INTP_AMBA_MISC_STAT);
218 + return IRQ_HANDLED;
221 +static int ls_pcie_g4_interrupt_init(struct mobiveil_pcie *mv_pci)
223 + struct ls_pcie_g4 *pcie = to_ls_pcie_g4(mv_pci);
224 + struct platform_device *pdev = mv_pci->pdev;
225 + struct device *dev = &pdev->dev;
228 + pcie->irq = platform_get_irq_byname(pdev, "intr");
229 + if (pcie->irq < 0) {
230 + dev_err(dev, "Can't get 'intr' IRQ, errno = %d\n", pcie->irq);
233 + ret = devm_request_irq(dev, pcie->irq, ls_pcie_g4_isr,
234 + IRQF_SHARED, pdev->name, pcie);
236 + dev_err(dev, "Can't register PCIe IRQ, errno = %d\n", ret);
243 +static void ls_pcie_g4_reset(struct work_struct *work)
245 + struct delayed_work *dwork = container_of(work, struct delayed_work,
247 + struct ls_pcie_g4 *pcie = container_of(dwork, struct ls_pcie_g4, dwork);
248 + struct mobiveil_pcie *mv_pci = &pcie->pci;
251 + ctrl = csr_readw(mv_pci, PCI_BRIDGE_CONTROL);
252 + ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
253 + csr_writew(mv_pci, ctrl, PCI_BRIDGE_CONTROL);
254 + ls_pcie_g4_reinit_hw(pcie);
255 + ls_pcie_g4_enable_interrupt(pcie);
258 +static struct mobiveil_rp_ops ls_pcie_g4_rp_ops = {
259 + .interrupt_init = ls_pcie_g4_interrupt_init,
262 +static const struct mobiveil_pab_ops ls_pcie_g4_pab_ops = {
263 + .link_up = ls_pcie_g4_link_up,
266 +static int __init ls_pcie_g4_probe(struct platform_device *pdev)
268 + struct device *dev = &pdev->dev;
269 + struct pci_host_bridge *bridge;
270 + struct mobiveil_pcie *mv_pci;
271 + struct ls_pcie_g4 *pcie;
272 + struct device_node *np = dev->of_node;
275 + if (!of_parse_phandle(np, "msi-parent", 0)) {
276 + dev_err(dev, "Failed to find msi-parent\n");
280 + bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
284 + pcie = pci_host_bridge_priv(bridge);
285 + mv_pci = &pcie->pci;
287 + mv_pci->pdev = pdev;
288 + mv_pci->ops = &ls_pcie_g4_pab_ops;
289 + mv_pci->rp.ops = &ls_pcie_g4_rp_ops;
290 + mv_pci->bridge = bridge;
292 + platform_set_drvdata(pdev, pcie);
294 + INIT_DELAYED_WORK(&pcie->dwork, ls_pcie_g4_reset);
296 + ret = mobiveil_pcie_host_probe(mv_pci);
298 + dev_err(dev, "Fail to probe\n");
302 + if (!ls_pcie_g4_is_bridge(pcie))
305 + ls_pcie_g4_enable_interrupt(pcie);
310 +static const struct of_device_id ls_pcie_g4_of_match[] = {
311 + { .compatible = "fsl,lx2160a-pcie", },
315 +static struct platform_driver ls_pcie_g4_driver = {
317 + .name = "layerscape-pcie-gen4",
318 + .of_match_table = ls_pcie_g4_of_match,
319 + .suppress_bind_attrs = true,
323 +builtin_platform_driver_probe(ls_pcie_g4_driver, ls_pcie_g4_probe);
324 --- a/drivers/pci/controller/mobiveil/pcie-mobiveil.h
325 +++ b/drivers/pci/controller/mobiveil/pcie-mobiveil.h
327 #define PAGE_LO_MASK 0x3ff
328 #define PAGE_SEL_OFFSET_SHIFT 10
330 +#define PAB_ACTIVITY_STAT 0x81c
332 #define PAB_AXI_PIO_CTRL 0x0840
333 #define APIO_EN_MASK 0xf
337 #define PAB_INTP_AMBA_MISC_ENB 0x0b0c
338 #define PAB_INTP_AMBA_MISC_STAT 0x0b1c
339 -#define PAB_INTP_INTX_MASK 0x01e0
340 -#define PAB_INTP_MSI_MASK 0x8
341 +#define PAB_INTP_RESET BIT(1)
342 +#define PAB_INTP_MSI BIT(3)
343 +#define PAB_INTP_INTA BIT(5)
344 +#define PAB_INTP_INTB BIT(6)
345 +#define PAB_INTP_INTC BIT(7)
346 +#define PAB_INTP_INTD BIT(8)
347 +#define PAB_INTP_PCIE_UE BIT(9)
348 +#define PAB_INTP_IE_PMREDI BIT(29)
349 +#define PAB_INTP_IE_EC BIT(30)
350 +#define PAB_INTP_MSI_MASK PAB_INTP_MSI
351 +#define PAB_INTP_INTX_MASK (PAB_INTP_INTA | PAB_INTP_INTB |\
352 + PAB_INTP_INTC | PAB_INTP_INTD)
354 #define PAB_AXI_AMAP_CTRL(win) PAB_REG_ADDR(0x0ba0, win)
355 #define WIN_ENABLE_SHIFT 0