1d29ad70aSHou Zhiqiang // SPDX-License-Identifier: GPL-2.0
2d29ad70aSHou Zhiqiang /*
3d29ad70aSHou Zhiqiang * PCIe Gen4 host controller driver for NXP Layerscape SoCs
4d29ad70aSHou Zhiqiang *
5d29ad70aSHou Zhiqiang * Copyright 2019-2020 NXP
6d29ad70aSHou Zhiqiang *
7d29ad70aSHou Zhiqiang * Author: Zhiqiang Hou <Zhiqiang.Hou@nxp.com>
8d29ad70aSHou Zhiqiang */
9d29ad70aSHou Zhiqiang
10d29ad70aSHou Zhiqiang #include <linux/kernel.h>
11d29ad70aSHou Zhiqiang #include <linux/interrupt.h>
12d29ad70aSHou Zhiqiang #include <linux/init.h>
13d29ad70aSHou Zhiqiang #include <linux/of_pci.h>
14d29ad70aSHou Zhiqiang #include <linux/of_platform.h>
15d29ad70aSHou Zhiqiang #include <linux/of_irq.h>
16d29ad70aSHou Zhiqiang #include <linux/of_address.h>
17d29ad70aSHou Zhiqiang #include <linux/pci.h>
18d29ad70aSHou Zhiqiang #include <linux/platform_device.h>
19d29ad70aSHou Zhiqiang #include <linux/resource.h>
20d29ad70aSHou Zhiqiang #include <linux/mfd/syscon.h>
21d29ad70aSHou Zhiqiang #include <linux/regmap.h>
22d29ad70aSHou Zhiqiang
23d29ad70aSHou Zhiqiang #include "pcie-mobiveil.h"
24d29ad70aSHou Zhiqiang
25d29ad70aSHou Zhiqiang /* LUT and PF control registers */
26d29ad70aSHou Zhiqiang #define PCIE_LUT_OFF 0x80000
27d29ad70aSHou Zhiqiang #define PCIE_PF_OFF 0xc0000
28d29ad70aSHou Zhiqiang #define PCIE_PF_INT_STAT 0x18
29d29ad70aSHou Zhiqiang #define PF_INT_STAT_PABRST BIT(31)
30d29ad70aSHou Zhiqiang
31d29ad70aSHou Zhiqiang #define PCIE_PF_DBG 0x7fc
32d29ad70aSHou Zhiqiang #define PF_DBG_LTSSM_MASK 0x3f
33d29ad70aSHou Zhiqiang #define PF_DBG_LTSSM_L0 0x2d /* L0 state */
34d29ad70aSHou Zhiqiang #define PF_DBG_WE BIT(31)
35d29ad70aSHou Zhiqiang #define PF_DBG_PABR BIT(27)
36d29ad70aSHou Zhiqiang
37*4688594fSBjorn Helgaas #define to_ls_g4_pcie(x) platform_get_drvdata((x)->pdev)
38d29ad70aSHou Zhiqiang
39*4688594fSBjorn Helgaas struct ls_g4_pcie {
40d29ad70aSHou Zhiqiang struct mobiveil_pcie pci;
41d29ad70aSHou Zhiqiang struct delayed_work dwork;
42d29ad70aSHou Zhiqiang int irq;
43d29ad70aSHou Zhiqiang };
44d29ad70aSHou Zhiqiang
ls_g4_pcie_pf_readl(struct ls_g4_pcie * pcie,u32 off)45*4688594fSBjorn Helgaas static inline u32 ls_g4_pcie_pf_readl(struct ls_g4_pcie *pcie, u32 off)
46d29ad70aSHou Zhiqiang {
47d29ad70aSHou Zhiqiang return ioread32(pcie->pci.csr_axi_slave_base + PCIE_PF_OFF + off);
48d29ad70aSHou Zhiqiang }
49d29ad70aSHou Zhiqiang
ls_g4_pcie_pf_writel(struct ls_g4_pcie * pcie,u32 off,u32 val)50*4688594fSBjorn Helgaas static inline void ls_g4_pcie_pf_writel(struct ls_g4_pcie *pcie,
51d29ad70aSHou Zhiqiang u32 off, u32 val)
52d29ad70aSHou Zhiqiang {
53d29ad70aSHou Zhiqiang iowrite32(val, pcie->pci.csr_axi_slave_base + PCIE_PF_OFF + off);
54d29ad70aSHou Zhiqiang }
55d29ad70aSHou Zhiqiang
ls_g4_pcie_link_up(struct mobiveil_pcie * pci)56*4688594fSBjorn Helgaas static int ls_g4_pcie_link_up(struct mobiveil_pcie *pci)
57d29ad70aSHou Zhiqiang {
58*4688594fSBjorn Helgaas struct ls_g4_pcie *pcie = to_ls_g4_pcie(pci);
59d29ad70aSHou Zhiqiang u32 state;
60d29ad70aSHou Zhiqiang
61*4688594fSBjorn Helgaas state = ls_g4_pcie_pf_readl(pcie, PCIE_PF_DBG);
62d29ad70aSHou Zhiqiang state = state & PF_DBG_LTSSM_MASK;
63d29ad70aSHou Zhiqiang
64d29ad70aSHou Zhiqiang if (state == PF_DBG_LTSSM_L0)
65d29ad70aSHou Zhiqiang return 1;
66d29ad70aSHou Zhiqiang
67d29ad70aSHou Zhiqiang return 0;
68d29ad70aSHou Zhiqiang }
69d29ad70aSHou Zhiqiang
ls_g4_pcie_disable_interrupt(struct ls_g4_pcie * pcie)70*4688594fSBjorn Helgaas static void ls_g4_pcie_disable_interrupt(struct ls_g4_pcie *pcie)
71d29ad70aSHou Zhiqiang {
72d29ad70aSHou Zhiqiang struct mobiveil_pcie *mv_pci = &pcie->pci;
73d29ad70aSHou Zhiqiang
74d29ad70aSHou Zhiqiang mobiveil_csr_writel(mv_pci, 0, PAB_INTP_AMBA_MISC_ENB);
75d29ad70aSHou Zhiqiang }
76d29ad70aSHou Zhiqiang
ls_g4_pcie_enable_interrupt(struct ls_g4_pcie * pcie)77*4688594fSBjorn Helgaas static void ls_g4_pcie_enable_interrupt(struct ls_g4_pcie *pcie)
78d29ad70aSHou Zhiqiang {
79d29ad70aSHou Zhiqiang struct mobiveil_pcie *mv_pci = &pcie->pci;
80d29ad70aSHou Zhiqiang u32 val;
81d29ad70aSHou Zhiqiang
82d29ad70aSHou Zhiqiang /* Clear the interrupt status */
83d29ad70aSHou Zhiqiang mobiveil_csr_writel(mv_pci, 0xffffffff, PAB_INTP_AMBA_MISC_STAT);
84d29ad70aSHou Zhiqiang
85d29ad70aSHou Zhiqiang val = PAB_INTP_INTX_MASK | PAB_INTP_MSI | PAB_INTP_RESET |
86d29ad70aSHou Zhiqiang PAB_INTP_PCIE_UE | PAB_INTP_IE_PMREDI | PAB_INTP_IE_EC;
87d29ad70aSHou Zhiqiang mobiveil_csr_writel(mv_pci, val, PAB_INTP_AMBA_MISC_ENB);
88d29ad70aSHou Zhiqiang }
89d29ad70aSHou Zhiqiang
ls_g4_pcie_reinit_hw(struct ls_g4_pcie * pcie)90*4688594fSBjorn Helgaas static int ls_g4_pcie_reinit_hw(struct ls_g4_pcie *pcie)
91d29ad70aSHou Zhiqiang {
92d29ad70aSHou Zhiqiang struct mobiveil_pcie *mv_pci = &pcie->pci;
93d29ad70aSHou Zhiqiang struct device *dev = &mv_pci->pdev->dev;
94d29ad70aSHou Zhiqiang u32 val, act_stat;
95d29ad70aSHou Zhiqiang int to = 100;
96d29ad70aSHou Zhiqiang
97d29ad70aSHou Zhiqiang /* Poll for pab_csb_reset to set and PAB activity to clear */
98d29ad70aSHou Zhiqiang do {
99d29ad70aSHou Zhiqiang usleep_range(10, 15);
100*4688594fSBjorn Helgaas val = ls_g4_pcie_pf_readl(pcie, PCIE_PF_INT_STAT);
101d29ad70aSHou Zhiqiang act_stat = mobiveil_csr_readl(mv_pci, PAB_ACTIVITY_STAT);
102d29ad70aSHou Zhiqiang } while (((val & PF_INT_STAT_PABRST) == 0 || act_stat) && to--);
103d29ad70aSHou Zhiqiang if (to < 0) {
104d29ad70aSHou Zhiqiang dev_err(dev, "Poll PABRST&PABACT timeout\n");
105d29ad70aSHou Zhiqiang return -EIO;
106d29ad70aSHou Zhiqiang }
107d29ad70aSHou Zhiqiang
108d29ad70aSHou Zhiqiang /* clear PEX_RESET bit in PEX_PF0_DBG register */
109*4688594fSBjorn Helgaas val = ls_g4_pcie_pf_readl(pcie, PCIE_PF_DBG);
110d29ad70aSHou Zhiqiang val |= PF_DBG_WE;
111*4688594fSBjorn Helgaas ls_g4_pcie_pf_writel(pcie, PCIE_PF_DBG, val);
112d29ad70aSHou Zhiqiang
113*4688594fSBjorn Helgaas val = ls_g4_pcie_pf_readl(pcie, PCIE_PF_DBG);
114d29ad70aSHou Zhiqiang val |= PF_DBG_PABR;
115*4688594fSBjorn Helgaas ls_g4_pcie_pf_writel(pcie, PCIE_PF_DBG, val);
116d29ad70aSHou Zhiqiang
117*4688594fSBjorn Helgaas val = ls_g4_pcie_pf_readl(pcie, PCIE_PF_DBG);
118d29ad70aSHou Zhiqiang val &= ~PF_DBG_WE;
119*4688594fSBjorn Helgaas ls_g4_pcie_pf_writel(pcie, PCIE_PF_DBG, val);
120d29ad70aSHou Zhiqiang
121d29ad70aSHou Zhiqiang mobiveil_host_init(mv_pci, true);
122d29ad70aSHou Zhiqiang
123d29ad70aSHou Zhiqiang to = 100;
124*4688594fSBjorn Helgaas while (!ls_g4_pcie_link_up(mv_pci) && to--)
125d29ad70aSHou Zhiqiang usleep_range(200, 250);
126d29ad70aSHou Zhiqiang if (to < 0) {
127d29ad70aSHou Zhiqiang dev_err(dev, "PCIe link training timeout\n");
128d29ad70aSHou Zhiqiang return -EIO;
129d29ad70aSHou Zhiqiang }
130d29ad70aSHou Zhiqiang
131d29ad70aSHou Zhiqiang return 0;
132d29ad70aSHou Zhiqiang }
133d29ad70aSHou Zhiqiang
ls_g4_pcie_isr(int irq,void * dev_id)134*4688594fSBjorn Helgaas static irqreturn_t ls_g4_pcie_isr(int irq, void *dev_id)
135d29ad70aSHou Zhiqiang {
136*4688594fSBjorn Helgaas struct ls_g4_pcie *pcie = (struct ls_g4_pcie *)dev_id;
137d29ad70aSHou Zhiqiang struct mobiveil_pcie *mv_pci = &pcie->pci;
138d29ad70aSHou Zhiqiang u32 val;
139d29ad70aSHou Zhiqiang
140d29ad70aSHou Zhiqiang val = mobiveil_csr_readl(mv_pci, PAB_INTP_AMBA_MISC_STAT);
141d29ad70aSHou Zhiqiang if (!val)
142d29ad70aSHou Zhiqiang return IRQ_NONE;
143d29ad70aSHou Zhiqiang
144d29ad70aSHou Zhiqiang if (val & PAB_INTP_RESET) {
145*4688594fSBjorn Helgaas ls_g4_pcie_disable_interrupt(pcie);
146d29ad70aSHou Zhiqiang schedule_delayed_work(&pcie->dwork, msecs_to_jiffies(1));
147d29ad70aSHou Zhiqiang }
148d29ad70aSHou Zhiqiang
149d29ad70aSHou Zhiqiang mobiveil_csr_writel(mv_pci, val, PAB_INTP_AMBA_MISC_STAT);
150d29ad70aSHou Zhiqiang
151d29ad70aSHou Zhiqiang return IRQ_HANDLED;
152d29ad70aSHou Zhiqiang }
153d29ad70aSHou Zhiqiang
ls_g4_pcie_interrupt_init(struct mobiveil_pcie * mv_pci)154*4688594fSBjorn Helgaas static int ls_g4_pcie_interrupt_init(struct mobiveil_pcie *mv_pci)
155d29ad70aSHou Zhiqiang {
156*4688594fSBjorn Helgaas struct ls_g4_pcie *pcie = to_ls_g4_pcie(mv_pci);
157d29ad70aSHou Zhiqiang struct platform_device *pdev = mv_pci->pdev;
158d29ad70aSHou Zhiqiang struct device *dev = &pdev->dev;
159d29ad70aSHou Zhiqiang int ret;
160d29ad70aSHou Zhiqiang
161d29ad70aSHou Zhiqiang pcie->irq = platform_get_irq_byname(pdev, "intr");
162caecb05cSKrzysztof Wilczyński if (pcie->irq < 0)
163d29ad70aSHou Zhiqiang return pcie->irq;
164caecb05cSKrzysztof Wilczyński
165*4688594fSBjorn Helgaas ret = devm_request_irq(dev, pcie->irq, ls_g4_pcie_isr,
166d29ad70aSHou Zhiqiang IRQF_SHARED, pdev->name, pcie);
167d29ad70aSHou Zhiqiang if (ret) {
168d29ad70aSHou Zhiqiang dev_err(dev, "Can't register PCIe IRQ, errno = %d\n", ret);
169d29ad70aSHou Zhiqiang return ret;
170d29ad70aSHou Zhiqiang }
171d29ad70aSHou Zhiqiang
172d29ad70aSHou Zhiqiang return 0;
173d29ad70aSHou Zhiqiang }
174d29ad70aSHou Zhiqiang
ls_g4_pcie_reset(struct work_struct * work)175*4688594fSBjorn Helgaas static void ls_g4_pcie_reset(struct work_struct *work)
176d29ad70aSHou Zhiqiang {
177d29ad70aSHou Zhiqiang struct delayed_work *dwork = container_of(work, struct delayed_work,
178d29ad70aSHou Zhiqiang work);
179*4688594fSBjorn Helgaas struct ls_g4_pcie *pcie = container_of(dwork, struct ls_g4_pcie, dwork);
180d29ad70aSHou Zhiqiang struct mobiveil_pcie *mv_pci = &pcie->pci;
181d29ad70aSHou Zhiqiang u16 ctrl;
182d29ad70aSHou Zhiqiang
183d29ad70aSHou Zhiqiang ctrl = mobiveil_csr_readw(mv_pci, PCI_BRIDGE_CONTROL);
184d29ad70aSHou Zhiqiang ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
185d29ad70aSHou Zhiqiang mobiveil_csr_writew(mv_pci, ctrl, PCI_BRIDGE_CONTROL);
186d29ad70aSHou Zhiqiang
187*4688594fSBjorn Helgaas if (!ls_g4_pcie_reinit_hw(pcie))
188d29ad70aSHou Zhiqiang return;
189d29ad70aSHou Zhiqiang
190*4688594fSBjorn Helgaas ls_g4_pcie_enable_interrupt(pcie);
191d29ad70aSHou Zhiqiang }
192d29ad70aSHou Zhiqiang
193*4688594fSBjorn Helgaas static struct mobiveil_rp_ops ls_g4_pcie_rp_ops = {
194*4688594fSBjorn Helgaas .interrupt_init = ls_g4_pcie_interrupt_init,
195d29ad70aSHou Zhiqiang };
196d29ad70aSHou Zhiqiang
197*4688594fSBjorn Helgaas static const struct mobiveil_pab_ops ls_g4_pcie_pab_ops = {
198*4688594fSBjorn Helgaas .link_up = ls_g4_pcie_link_up,
199d29ad70aSHou Zhiqiang };
200d29ad70aSHou Zhiqiang
ls_g4_pcie_probe(struct platform_device * pdev)201*4688594fSBjorn Helgaas static int __init ls_g4_pcie_probe(struct platform_device *pdev)
202d29ad70aSHou Zhiqiang {
203d29ad70aSHou Zhiqiang struct device *dev = &pdev->dev;
204d29ad70aSHou Zhiqiang struct pci_host_bridge *bridge;
205d29ad70aSHou Zhiqiang struct mobiveil_pcie *mv_pci;
206*4688594fSBjorn Helgaas struct ls_g4_pcie *pcie;
207d29ad70aSHou Zhiqiang struct device_node *np = dev->of_node;
208d29ad70aSHou Zhiqiang int ret;
209d29ad70aSHou Zhiqiang
210d29ad70aSHou Zhiqiang if (!of_parse_phandle(np, "msi-parent", 0)) {
211d29ad70aSHou Zhiqiang dev_err(dev, "Failed to find msi-parent\n");
212d29ad70aSHou Zhiqiang return -EINVAL;
213d29ad70aSHou Zhiqiang }
214d29ad70aSHou Zhiqiang
215d29ad70aSHou Zhiqiang bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
216d29ad70aSHou Zhiqiang if (!bridge)
217d29ad70aSHou Zhiqiang return -ENOMEM;
218d29ad70aSHou Zhiqiang
219d29ad70aSHou Zhiqiang pcie = pci_host_bridge_priv(bridge);
220d29ad70aSHou Zhiqiang mv_pci = &pcie->pci;
221d29ad70aSHou Zhiqiang
222d29ad70aSHou Zhiqiang mv_pci->pdev = pdev;
223*4688594fSBjorn Helgaas mv_pci->ops = &ls_g4_pcie_pab_ops;
224*4688594fSBjorn Helgaas mv_pci->rp.ops = &ls_g4_pcie_rp_ops;
225d29ad70aSHou Zhiqiang mv_pci->rp.bridge = bridge;
226d29ad70aSHou Zhiqiang
227d29ad70aSHou Zhiqiang platform_set_drvdata(pdev, pcie);
228d29ad70aSHou Zhiqiang
229*4688594fSBjorn Helgaas INIT_DELAYED_WORK(&pcie->dwork, ls_g4_pcie_reset);
230d29ad70aSHou Zhiqiang
231d29ad70aSHou Zhiqiang ret = mobiveil_pcie_host_probe(mv_pci);
232d29ad70aSHou Zhiqiang if (ret) {
233d29ad70aSHou Zhiqiang dev_err(dev, "Fail to probe\n");
234d29ad70aSHou Zhiqiang return ret;
235d29ad70aSHou Zhiqiang }
236d29ad70aSHou Zhiqiang
237*4688594fSBjorn Helgaas ls_g4_pcie_enable_interrupt(pcie);
238d29ad70aSHou Zhiqiang
239d29ad70aSHou Zhiqiang return 0;
240d29ad70aSHou Zhiqiang }
241d29ad70aSHou Zhiqiang
242*4688594fSBjorn Helgaas static const struct of_device_id ls_g4_pcie_of_match[] = {
243d29ad70aSHou Zhiqiang { .compatible = "fsl,lx2160a-pcie", },
244d29ad70aSHou Zhiqiang { },
245d29ad70aSHou Zhiqiang };
246d29ad70aSHou Zhiqiang
247*4688594fSBjorn Helgaas static struct platform_driver ls_g4_pcie_driver = {
248d29ad70aSHou Zhiqiang .driver = {
249d29ad70aSHou Zhiqiang .name = "layerscape-pcie-gen4",
250*4688594fSBjorn Helgaas .of_match_table = ls_g4_pcie_of_match,
251d29ad70aSHou Zhiqiang .suppress_bind_attrs = true,
252d29ad70aSHou Zhiqiang },
253d29ad70aSHou Zhiqiang };
254d29ad70aSHou Zhiqiang
255*4688594fSBjorn Helgaas builtin_platform_driver_probe(ls_g4_pcie_driver, ls_g4_pcie_probe);
256