Merge tag 'pci-v4.15-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[sfrench/cifs-2.6.git] / drivers / pci / dwc / pci-layerscape.c
1 /*
2  * PCIe host controller driver for Freescale Layerscape SoCs
3  *
4  * Copyright (C) 2014 Freescale Semiconductor.
5  *
6  * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include <linux/of_pci.h>
17 #include <linux/of_platform.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_address.h>
20 #include <linux/pci.h>
21 #include <linux/platform_device.h>
22 #include <linux/resource.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/regmap.h>
25
26 #include "pcie-designware.h"
27
28 /* PEX1/2 Misc Ports Status Register */
29 #define SCFG_PEXMSCPORTSR(pex_idx)      (0x94 + (pex_idx) * 4)
30 #define LTSSM_STATE_SHIFT       20
31 #define LTSSM_STATE_MASK        0x3f
32 #define LTSSM_PCIE_L0           0x11 /* L0 state */
33
34 /* PEX Internal Configuration Registers */
35 #define PCIE_STRFMR1            0x71c /* Symbol Timer & Filter Mask Register1 */
36 #define PCIE_ABSERR             0x8d0 /* Bridge Slave Error Response Register */
37 #define PCIE_ABSERR_SETTING     0x9401 /* Forward error of non-posted request */
38
39 #define PCIE_IATU_NUM           6
40
41 struct ls_pcie_drvdata {
42         u32 lut_offset;
43         u32 ltssm_shift;
44         u32 lut_dbg;
45         const struct dw_pcie_host_ops *ops;
46         const struct dw_pcie_ops *dw_pcie_ops;
47 };
48
49 struct ls_pcie {
50         struct dw_pcie *pci;
51         void __iomem *lut;
52         struct regmap *scfg;
53         const struct ls_pcie_drvdata *drvdata;
54         int index;
55 };
56
57 #define to_ls_pcie(x)   dev_get_drvdata((x)->dev)
58
59 static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
60 {
61         struct dw_pcie *pci = pcie->pci;
62         u32 header_type;
63
64         header_type = ioread8(pci->dbi_base + PCI_HEADER_TYPE);
65         header_type &= 0x7f;
66
67         return header_type == PCI_HEADER_TYPE_BRIDGE;
68 }
69
70 /* Clear multi-function bit */
71 static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
72 {
73         struct dw_pcie *pci = pcie->pci;
74
75         iowrite8(PCI_HEADER_TYPE_BRIDGE, pci->dbi_base + PCI_HEADER_TYPE);
76 }
77
78 /* Drop MSG TLP except for Vendor MSG */
79 static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
80 {
81         u32 val;
82         struct dw_pcie *pci = pcie->pci;
83
84         val = ioread32(pci->dbi_base + PCIE_STRFMR1);
85         val &= 0xDFFFFFFF;
86         iowrite32(val, pci->dbi_base + PCIE_STRFMR1);
87 }
88
89 static void ls_pcie_disable_outbound_atus(struct ls_pcie *pcie)
90 {
91         int i;
92
93         for (i = 0; i < PCIE_IATU_NUM; i++)
94                 dw_pcie_disable_atu(pcie->pci, DW_PCIE_REGION_OUTBOUND, i);
95 }
96
97 static int ls1021_pcie_link_up(struct dw_pcie *pci)
98 {
99         u32 state;
100         struct ls_pcie *pcie = to_ls_pcie(pci);
101
102         if (!pcie->scfg)
103                 return 0;
104
105         regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
106         state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
107
108         if (state < LTSSM_PCIE_L0)
109                 return 0;
110
111         return 1;
112 }
113
114 static int ls_pcie_link_up(struct dw_pcie *pci)
115 {
116         struct ls_pcie *pcie = to_ls_pcie(pci);
117         u32 state;
118
119         state = (ioread32(pcie->lut + pcie->drvdata->lut_dbg) >>
120                  pcie->drvdata->ltssm_shift) &
121                  LTSSM_STATE_MASK;
122
123         if (state < LTSSM_PCIE_L0)
124                 return 0;
125
126         return 1;
127 }
128
129 /* Forward error response of outbound non-posted requests */
130 static void ls_pcie_fix_error_response(struct ls_pcie *pcie)
131 {
132         struct dw_pcie *pci = pcie->pci;
133
134         iowrite32(PCIE_ABSERR_SETTING, pci->dbi_base + PCIE_ABSERR);
135 }
136
137 static int ls_pcie_host_init(struct pcie_port *pp)
138 {
139         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
140         struct ls_pcie *pcie = to_ls_pcie(pci);
141
142         /*
143          * Disable outbound windows configured by the bootloader to avoid
144          * one transaction hitting multiple outbound windows.
145          * dw_pcie_setup_rc() will reconfigure the outbound windows.
146          */
147         ls_pcie_disable_outbound_atus(pcie);
148         ls_pcie_fix_error_response(pcie);
149
150         dw_pcie_dbi_ro_wr_en(pci);
151         ls_pcie_clear_multifunction(pcie);
152         dw_pcie_dbi_ro_wr_dis(pci);
153
154         ls_pcie_drop_msg_tlp(pcie);
155
156         dw_pcie_setup_rc(pp);
157
158         return 0;
159 }
160
161 static int ls1021_pcie_host_init(struct pcie_port *pp)
162 {
163         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
164         struct ls_pcie *pcie = to_ls_pcie(pci);
165         struct device *dev = pci->dev;
166         u32 index[2];
167         int ret;
168
169         pcie->scfg = syscon_regmap_lookup_by_phandle(dev->of_node,
170                                                      "fsl,pcie-scfg");
171         if (IS_ERR(pcie->scfg)) {
172                 ret = PTR_ERR(pcie->scfg);
173                 dev_err(dev, "No syscfg phandle specified\n");
174                 pcie->scfg = NULL;
175                 return ret;
176         }
177
178         if (of_property_read_u32_array(dev->of_node,
179                                        "fsl,pcie-scfg", index, 2)) {
180                 pcie->scfg = NULL;
181                 return -EINVAL;
182         }
183         pcie->index = index[1];
184
185         return ls_pcie_host_init(pp);
186 }
187
188 static int ls_pcie_msi_host_init(struct pcie_port *pp,
189                                  struct msi_controller *chip)
190 {
191         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
192         struct device *dev = pci->dev;
193         struct device_node *np = dev->of_node;
194         struct device_node *msi_node;
195
196         /*
197          * The MSI domain is set by the generic of_msi_configure().  This
198          * .msi_host_init() function keeps us from doing the default MSI
199          * domain setup in dw_pcie_host_init() and also enforces the
200          * requirement that "msi-parent" exists.
201          */
202         msi_node = of_parse_phandle(np, "msi-parent", 0);
203         if (!msi_node) {
204                 dev_err(dev, "failed to find msi-parent\n");
205                 return -EINVAL;
206         }
207
208         return 0;
209 }
210
211 static const struct dw_pcie_host_ops ls1021_pcie_host_ops = {
212         .host_init = ls1021_pcie_host_init,
213         .msi_host_init = ls_pcie_msi_host_init,
214 };
215
216 static const struct dw_pcie_host_ops ls_pcie_host_ops = {
217         .host_init = ls_pcie_host_init,
218         .msi_host_init = ls_pcie_msi_host_init,
219 };
220
221 static const struct dw_pcie_ops dw_ls1021_pcie_ops = {
222         .link_up = ls1021_pcie_link_up,
223 };
224
225 static const struct dw_pcie_ops dw_ls_pcie_ops = {
226         .link_up = ls_pcie_link_up,
227 };
228
229 static struct ls_pcie_drvdata ls1021_drvdata = {
230         .ops = &ls1021_pcie_host_ops,
231         .dw_pcie_ops = &dw_ls1021_pcie_ops,
232 };
233
234 static struct ls_pcie_drvdata ls1043_drvdata = {
235         .lut_offset = 0x10000,
236         .ltssm_shift = 24,
237         .lut_dbg = 0x7fc,
238         .ops = &ls_pcie_host_ops,
239         .dw_pcie_ops = &dw_ls_pcie_ops,
240 };
241
242 static struct ls_pcie_drvdata ls1046_drvdata = {
243         .lut_offset = 0x80000,
244         .ltssm_shift = 24,
245         .lut_dbg = 0x407fc,
246         .ops = &ls_pcie_host_ops,
247         .dw_pcie_ops = &dw_ls_pcie_ops,
248 };
249
250 static struct ls_pcie_drvdata ls2080_drvdata = {
251         .lut_offset = 0x80000,
252         .ltssm_shift = 0,
253         .lut_dbg = 0x7fc,
254         .ops = &ls_pcie_host_ops,
255         .dw_pcie_ops = &dw_ls_pcie_ops,
256 };
257
258 static struct ls_pcie_drvdata ls2088_drvdata = {
259         .lut_offset = 0x80000,
260         .ltssm_shift = 0,
261         .lut_dbg = 0x407fc,
262         .ops = &ls_pcie_host_ops,
263         .dw_pcie_ops = &dw_ls_pcie_ops,
264 };
265
266 static const struct of_device_id ls_pcie_of_match[] = {
267         { .compatible = "fsl,ls1012a-pcie", .data = &ls1046_drvdata },
268         { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
269         { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
270         { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
271         { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
272         { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
273         { .compatible = "fsl,ls2088a-pcie", .data = &ls2088_drvdata },
274         { .compatible = "fsl,ls1088a-pcie", .data = &ls2088_drvdata },
275         { },
276 };
277
278 static int __init ls_add_pcie_port(struct ls_pcie *pcie)
279 {
280         struct dw_pcie *pci = pcie->pci;
281         struct pcie_port *pp = &pci->pp;
282         struct device *dev = pci->dev;
283         int ret;
284
285         pp->ops = pcie->drvdata->ops;
286
287         ret = dw_pcie_host_init(pp);
288         if (ret) {
289                 dev_err(dev, "failed to initialize host\n");
290                 return ret;
291         }
292
293         return 0;
294 }
295
296 static int __init ls_pcie_probe(struct platform_device *pdev)
297 {
298         struct device *dev = &pdev->dev;
299         struct dw_pcie *pci;
300         struct ls_pcie *pcie;
301         struct resource *dbi_base;
302         int ret;
303
304         pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
305         if (!pcie)
306                 return -ENOMEM;
307
308         pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
309         if (!pci)
310                 return -ENOMEM;
311
312         pcie->drvdata = of_device_get_match_data(dev);
313
314         pci->dev = dev;
315         pci->ops = pcie->drvdata->dw_pcie_ops;
316
317         pcie->pci = pci;
318
319         dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
320         pci->dbi_base = devm_pci_remap_cfg_resource(dev, dbi_base);
321         if (IS_ERR(pci->dbi_base))
322                 return PTR_ERR(pci->dbi_base);
323
324         pcie->lut = pci->dbi_base + pcie->drvdata->lut_offset;
325
326         if (!ls_pcie_is_bridge(pcie))
327                 return -ENODEV;
328
329         platform_set_drvdata(pdev, pcie);
330
331         ret = ls_add_pcie_port(pcie);
332         if (ret < 0)
333                 return ret;
334
335         return 0;
336 }
337
338 static struct platform_driver ls_pcie_driver = {
339         .driver = {
340                 .name = "layerscape-pcie",
341                 .of_match_table = ls_pcie_of_match,
342                 .suppress_bind_attrs = true,
343         },
344 };
345 builtin_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);