blob: fd861289ad8bc53a3e474ca2b922060bfddb7493 [file] [log] [blame]
Minghuan Lian62d0ff832014-11-05 16:45:11 +08001/*
2 * PCIe host controller driver for Freescale Layerscape SoCs
3 *
4 * Copyright (C) 2014 Freescale Semiconductor.
5 *
Minghuan Lian5192ec72015-10-16 15:19:19 +08006 * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
Minghuan Lian62d0ff832014-11-05 16:45:11 +08007 *
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>
Minghuan Lian62d0ff832014-11-05 16:45:11 +080014#include <linux/interrupt.h>
Paul Gortmaker154fb602016-07-02 19:13:27 -040015#include <linux/init.h>
Minghuan Lian62d0ff832014-11-05 16:45:11 +080016#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
Minghuan Lian5192ec72015-10-16 15:19:19 +080034/* PEX Internal Configuration Registers */
35#define PCIE_STRFMR1 0x71c /* Symbol Timer & Filter Mask Register1 */
36#define PCIE_DBI_RO_WR_EN 0x8bc /* DBI Read-Only Write Enable Register */
37
Minghuan Liand6463342015-10-16 15:19:17 +080038struct ls_pcie_drvdata {
Minghuan Lian5192ec72015-10-16 15:19:19 +080039 u32 lut_offset;
40 u32 ltssm_shift;
Mingkai Hu1d770402016-10-25 20:36:56 +080041 u32 lut_dbg;
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +080042 const struct dw_pcie_host_ops *ops;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053043 const struct dw_pcie_ops *dw_pcie_ops;
Minghuan Liand6463342015-10-16 15:19:17 +080044};
45
Minghuan Lian62d0ff832014-11-05 16:45:11 +080046struct ls_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053047 struct dw_pcie *pci;
Minghuan Lian5192ec72015-10-16 15:19:19 +080048 void __iomem *lut;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080049 struct regmap *scfg;
Minghuan Liand6463342015-10-16 15:19:17 +080050 const struct ls_pcie_drvdata *drvdata;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080051 int index;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080052};
53
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053054#define to_ls_pcie(x) dev_get_drvdata((x)->dev)
Minghuan Lian62d0ff832014-11-05 16:45:11 +080055
Minghuan Lian7af4ce32015-10-16 15:19:16 +080056static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
57{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053058 struct dw_pcie *pci = pcie->pci;
Minghuan Lian7af4ce32015-10-16 15:19:16 +080059 u32 header_type;
60
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053061 header_type = ioread8(pci->dbi_base + PCI_HEADER_TYPE);
Minghuan Lian7af4ce32015-10-16 15:19:16 +080062 header_type &= 0x7f;
63
64 return header_type == PCI_HEADER_TYPE_BRIDGE;
65}
66
Minghuan Lian5192ec72015-10-16 15:19:19 +080067/* Clear multi-function bit */
68static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
69{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053070 struct dw_pcie *pci = pcie->pci;
71
72 iowrite8(PCI_HEADER_TYPE_BRIDGE, pci->dbi_base + PCI_HEADER_TYPE);
Minghuan Lian5192ec72015-10-16 15:19:19 +080073}
74
75/* Fix class value */
76static void ls_pcie_fix_class(struct ls_pcie *pcie)
77{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053078 struct dw_pcie *pci = pcie->pci;
79
80 iowrite16(PCI_CLASS_BRIDGE_PCI, pci->dbi_base + PCI_CLASS_DEVICE);
Minghuan Lian5192ec72015-10-16 15:19:19 +080081}
82
Minghuan Lian1195c102016-02-29 17:24:15 -060083/* Drop MSG TLP except for Vendor MSG */
84static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
85{
86 u32 val;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053087 struct dw_pcie *pci = pcie->pci;
Minghuan Lian1195c102016-02-29 17:24:15 -060088
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053089 val = ioread32(pci->dbi_base + PCIE_STRFMR1);
Minghuan Lian1195c102016-02-29 17:24:15 -060090 val &= 0xDFFFFFFF;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053091 iowrite32(val, pci->dbi_base + PCIE_STRFMR1);
Minghuan Lian1195c102016-02-29 17:24:15 -060092}
93
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053094static int ls1021_pcie_link_up(struct dw_pcie *pci)
Minghuan Lian62d0ff832014-11-05 16:45:11 +080095{
96 u32 state;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053097 struct ls_pcie *pcie = to_ls_pcie(pci);
Minghuan Lian62d0ff832014-11-05 16:45:11 +080098
Minghuan Liand6463342015-10-16 15:19:17 +080099 if (!pcie->scfg)
100 return 0;
101
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800102 regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
103 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
104
105 if (state < LTSSM_PCIE_L0)
106 return 0;
107
108 return 1;
109}
110
Minghuan Liand6463342015-10-16 15:19:17 +0800111static void ls1021_pcie_host_init(struct pcie_port *pp)
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500112{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530113 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
114 struct ls_pcie *pcie = to_ls_pcie(pci);
115 struct device *dev = pci->dev;
Minghuan Lian1195c102016-02-29 17:24:15 -0600116 u32 index[2];
Minghuan Liand6463342015-10-16 15:19:17 +0800117
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500118 pcie->scfg = syscon_regmap_lookup_by_phandle(dev->of_node,
Minghuan Liand6463342015-10-16 15:19:17 +0800119 "fsl,pcie-scfg");
120 if (IS_ERR(pcie->scfg)) {
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500121 dev_err(dev, "No syscfg phandle specified\n");
Minghuan Liand6463342015-10-16 15:19:17 +0800122 pcie->scfg = NULL;
123 return;
124 }
125
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500126 if (of_property_read_u32_array(dev->of_node,
Minghuan Liand6463342015-10-16 15:19:17 +0800127 "fsl,pcie-scfg", index, 2)) {
128 pcie->scfg = NULL;
129 return;
130 }
131 pcie->index = index[1];
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500132
133 dw_pcie_setup_rc(pp);
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500134
Minghuan Lian1195c102016-02-29 17:24:15 -0600135 ls_pcie_drop_msg_tlp(pcie);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800136}
137
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530138static int ls_pcie_link_up(struct dw_pcie *pci)
Minghuan Lian5192ec72015-10-16 15:19:19 +0800139{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530140 struct ls_pcie *pcie = to_ls_pcie(pci);
Minghuan Lian5192ec72015-10-16 15:19:19 +0800141 u32 state;
142
Mingkai Hu1d770402016-10-25 20:36:56 +0800143 state = (ioread32(pcie->lut + pcie->drvdata->lut_dbg) >>
Minghuan Lian5192ec72015-10-16 15:19:19 +0800144 pcie->drvdata->ltssm_shift) &
145 LTSSM_STATE_MASK;
146
147 if (state < LTSSM_PCIE_L0)
148 return 0;
149
150 return 1;
151}
152
153static void ls_pcie_host_init(struct pcie_port *pp)
154{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530155 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
156 struct ls_pcie *pcie = to_ls_pcie(pci);
Minghuan Lian5192ec72015-10-16 15:19:19 +0800157
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530158 iowrite32(1, pci->dbi_base + PCIE_DBI_RO_WR_EN);
Minghuan Lian5192ec72015-10-16 15:19:19 +0800159 ls_pcie_fix_class(pcie);
160 ls_pcie_clear_multifunction(pcie);
Minghuan Lian1195c102016-02-29 17:24:15 -0600161 ls_pcie_drop_msg_tlp(pcie);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530162 iowrite32(0, pci->dbi_base + PCIE_DBI_RO_WR_EN);
Minghuan Lian5192ec72015-10-16 15:19:19 +0800163}
164
Minghuan Lianbd33b872015-10-16 15:19:20 +0800165static int ls_pcie_msi_host_init(struct pcie_port *pp,
166 struct msi_controller *chip)
167{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530168 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
169 struct device *dev = pci->dev;
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500170 struct device_node *np = dev->of_node;
Minghuan Lianbd33b872015-10-16 15:19:20 +0800171 struct device_node *msi_node;
Minghuan Lianbd33b872015-10-16 15:19:20 +0800172
173 /*
174 * The MSI domain is set by the generic of_msi_configure(). This
175 * .msi_host_init() function keeps us from doing the default MSI
176 * domain setup in dw_pcie_host_init() and also enforces the
177 * requirement that "msi-parent" exists.
178 */
179 msi_node = of_parse_phandle(np, "msi-parent", 0);
180 if (!msi_node) {
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500181 dev_err(dev, "failed to find msi-parent\n");
Minghuan Lianbd33b872015-10-16 15:19:20 +0800182 return -EINVAL;
183 }
184
185 return 0;
186}
187
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800188static const struct dw_pcie_host_ops ls1021_pcie_host_ops = {
Minghuan Liand6463342015-10-16 15:19:17 +0800189 .host_init = ls1021_pcie_host_init,
Minghuan Lianbd33b872015-10-16 15:19:20 +0800190 .msi_host_init = ls_pcie_msi_host_init,
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800191};
192
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800193static const struct dw_pcie_host_ops ls_pcie_host_ops = {
Minghuan Lian5192ec72015-10-16 15:19:19 +0800194 .host_init = ls_pcie_host_init,
Minghuan Lianbd33b872015-10-16 15:19:20 +0800195 .msi_host_init = ls_pcie_msi_host_init,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800196};
197
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530198static const struct dw_pcie_ops dw_ls1021_pcie_ops = {
199 .link_up = ls1021_pcie_link_up,
200};
201
202static const struct dw_pcie_ops dw_ls_pcie_ops = {
203 .link_up = ls_pcie_link_up,
204};
205
Minghuan Liand6463342015-10-16 15:19:17 +0800206static struct ls_pcie_drvdata ls1021_drvdata = {
207 .ops = &ls1021_pcie_host_ops,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530208 .dw_pcie_ops = &dw_ls1021_pcie_ops,
Minghuan Liand6463342015-10-16 15:19:17 +0800209};
210
Minghuan Lian5192ec72015-10-16 15:19:19 +0800211static struct ls_pcie_drvdata ls1043_drvdata = {
212 .lut_offset = 0x10000,
213 .ltssm_shift = 24,
Mingkai Hu1d770402016-10-25 20:36:56 +0800214 .lut_dbg = 0x7fc,
215 .ops = &ls_pcie_host_ops,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530216 .dw_pcie_ops = &dw_ls_pcie_ops,
Mingkai Hu1d770402016-10-25 20:36:56 +0800217};
218
219static struct ls_pcie_drvdata ls1046_drvdata = {
220 .lut_offset = 0x80000,
221 .ltssm_shift = 24,
222 .lut_dbg = 0x407fc,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800223 .ops = &ls_pcie_host_ops,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530224 .dw_pcie_ops = &dw_ls_pcie_ops,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800225};
226
227static struct ls_pcie_drvdata ls2080_drvdata = {
228 .lut_offset = 0x80000,
229 .ltssm_shift = 0,
Mingkai Hu1d770402016-10-25 20:36:56 +0800230 .lut_dbg = 0x7fc,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800231 .ops = &ls_pcie_host_ops,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530232 .dw_pcie_ops = &dw_ls_pcie_ops,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800233};
234
Minghuan Liand6463342015-10-16 15:19:17 +0800235static const struct of_device_id ls_pcie_of_match[] = {
236 { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
Minghuan Lian5192ec72015-10-16 15:19:19 +0800237 { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
Mingkai Hu1d770402016-10-25 20:36:56 +0800238 { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
Minghuan Lian5192ec72015-10-16 15:19:19 +0800239 { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
Yang Shidbae40b2016-01-27 09:32:05 -0800240 { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
Minghuan Liand6463342015-10-16 15:19:17 +0800241 { },
242};
Minghuan Liand6463342015-10-16 15:19:17 +0800243
Bjorn Helgaas4726a822016-10-06 13:38:06 -0500244static int __init ls_add_pcie_port(struct ls_pcie *pcie)
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800245{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530246 struct dw_pcie *pci = pcie->pci;
247 struct pcie_port *pp = &pci->pp;
248 struct device *dev = pci->dev;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800249 int ret;
250
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530251 pp->ops = pcie->drvdata->ops;
252
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800253 ret = dw_pcie_host_init(pp);
254 if (ret) {
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500255 dev_err(dev, "failed to initialize host\n");
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800256 return ret;
257 }
258
259 return 0;
260}
261
262static int __init ls_pcie_probe(struct platform_device *pdev)
263{
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500264 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530265 struct dw_pcie *pci;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800266 struct ls_pcie *pcie;
267 struct resource *dbi_base;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800268 int ret;
269
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500270 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800271 if (!pcie)
272 return -ENOMEM;
273
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530274 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
275 if (!pci)
276 return -ENOMEM;
277
Bjorn Helgaas6dc2c042017-01-31 16:36:11 -0600278 pcie->drvdata = of_device_get_match_data(dev);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530279
280 pci->dev = dev;
281 pci->ops = pcie->drvdata->dw_pcie_ops;
Bjorn Helgaasfefe6732016-10-06 13:38:06 -0500282
Guenter Roeckc0464062017-02-25 02:08:12 -0800283 pcie->pci = pci;
284
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800285 dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
Lorenzo Pieralisi01bd4892017-04-19 17:49:08 +0100286 pci->dbi_base = devm_pci_remap_cfg_resource(dev, dbi_base);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530287 if (IS_ERR(pci->dbi_base))
288 return PTR_ERR(pci->dbi_base);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800289
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530290 pcie->lut = pci->dbi_base + pcie->drvdata->lut_offset;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800291
Minghuan Lian7af4ce32015-10-16 15:19:16 +0800292 if (!ls_pcie_is_bridge(pcie))
293 return -ENODEV;
294
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530295 platform_set_drvdata(pdev, pcie);
296
Bjorn Helgaas4726a822016-10-06 13:38:06 -0500297 ret = ls_add_pcie_port(pcie);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800298 if (ret < 0)
299 return ret;
300
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800301 return 0;
302}
303
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800304static struct platform_driver ls_pcie_driver = {
305 .driver = {
306 .name = "layerscape-pcie",
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800307 .of_match_table = ls_pcie_of_match,
Brian Norrisa5f40e82017-04-20 15:36:25 -0500308 .suppress_bind_attrs = true,
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800309 },
310};
Paul Gortmaker154fb602016-07-02 19:13:27 -0400311builtin_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);