blob: a77f7562c310c099d17c9d2530f0d926756682a9 [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
Hou Zhiqiang4a2745d2017-08-28 18:52:58 +080038#define PCIE_IATU_NUM 6
39
Minghuan Liand6463342015-10-16 15:19:17 +080040struct ls_pcie_drvdata {
Minghuan Lian5192ec72015-10-16 15:19:19 +080041 u32 lut_offset;
42 u32 ltssm_shift;
Mingkai Hu1d770402016-10-25 20:36:56 +080043 u32 lut_dbg;
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +080044 const struct dw_pcie_host_ops *ops;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053045 const struct dw_pcie_ops *dw_pcie_ops;
Minghuan Liand6463342015-10-16 15:19:17 +080046};
47
Minghuan Lian62d0ff832014-11-05 16:45:11 +080048struct ls_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053049 struct dw_pcie *pci;
Minghuan Lian5192ec72015-10-16 15:19:19 +080050 void __iomem *lut;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080051 struct regmap *scfg;
Minghuan Liand6463342015-10-16 15:19:17 +080052 const struct ls_pcie_drvdata *drvdata;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080053 int index;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080054};
55
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053056#define to_ls_pcie(x) dev_get_drvdata((x)->dev)
Minghuan Lian62d0ff832014-11-05 16:45:11 +080057
Minghuan Lian7af4ce32015-10-16 15:19:16 +080058static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
59{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053060 struct dw_pcie *pci = pcie->pci;
Minghuan Lian7af4ce32015-10-16 15:19:16 +080061 u32 header_type;
62
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053063 header_type = ioread8(pci->dbi_base + PCI_HEADER_TYPE);
Minghuan Lian7af4ce32015-10-16 15:19:16 +080064 header_type &= 0x7f;
65
66 return header_type == PCI_HEADER_TYPE_BRIDGE;
67}
68
Minghuan Lian5192ec72015-10-16 15:19:19 +080069/* Clear multi-function bit */
70static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
71{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053072 struct dw_pcie *pci = pcie->pci;
73
74 iowrite8(PCI_HEADER_TYPE_BRIDGE, pci->dbi_base + PCI_HEADER_TYPE);
Minghuan Lian5192ec72015-10-16 15:19:19 +080075}
76
77/* Fix class value */
78static void ls_pcie_fix_class(struct ls_pcie *pcie)
79{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053080 struct dw_pcie *pci = pcie->pci;
81
82 iowrite16(PCI_CLASS_BRIDGE_PCI, pci->dbi_base + PCI_CLASS_DEVICE);
Minghuan Lian5192ec72015-10-16 15:19:19 +080083}
84
Minghuan Lian1195c102016-02-29 17:24:15 -060085/* Drop MSG TLP except for Vendor MSG */
86static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
87{
88 u32 val;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053089 struct dw_pcie *pci = pcie->pci;
Minghuan Lian1195c102016-02-29 17:24:15 -060090
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053091 val = ioread32(pci->dbi_base + PCIE_STRFMR1);
Minghuan Lian1195c102016-02-29 17:24:15 -060092 val &= 0xDFFFFFFF;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053093 iowrite32(val, pci->dbi_base + PCIE_STRFMR1);
Minghuan Lian1195c102016-02-29 17:24:15 -060094}
95
Hou Zhiqiang4a2745d2017-08-28 18:52:58 +080096static void ls_pcie_disable_outbound_atus(struct ls_pcie *pcie)
97{
98 int i;
99
100 for (i = 0; i < PCIE_IATU_NUM; i++)
101 dw_pcie_disable_atu(pcie->pci, DW_PCIE_REGION_OUTBOUND, i);
102}
103
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530104static int ls1021_pcie_link_up(struct dw_pcie *pci)
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800105{
106 u32 state;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530107 struct ls_pcie *pcie = to_ls_pcie(pci);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800108
Minghuan Liand6463342015-10-16 15:19:17 +0800109 if (!pcie->scfg)
110 return 0;
111
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800112 regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
113 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
114
115 if (state < LTSSM_PCIE_L0)
116 return 0;
117
118 return 1;
119}
120
Hou Zhiqiangba95a822017-08-28 18:52:56 +0800121static int ls_pcie_link_up(struct dw_pcie *pci)
122{
123 struct ls_pcie *pcie = to_ls_pcie(pci);
124 u32 state;
125
126 state = (ioread32(pcie->lut + pcie->drvdata->lut_dbg) >>
127 pcie->drvdata->ltssm_shift) &
128 LTSSM_STATE_MASK;
129
130 if (state < LTSSM_PCIE_L0)
131 return 0;
132
133 return 1;
134}
135
136static int ls_pcie_host_init(struct pcie_port *pp)
137{
138 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
139 struct ls_pcie *pcie = to_ls_pcie(pci);
140
Hou Zhiqiang4a2745d2017-08-28 18:52:58 +0800141 /*
142 * Disable outbound windows configured by the bootloader to avoid
143 * one transaction hitting multiple outbound windows.
144 * dw_pcie_setup_rc() will reconfigure the outbound windows.
145 */
146 ls_pcie_disable_outbound_atus(pcie);
147
Hou Zhiqiangba95a822017-08-28 18:52:56 +0800148 iowrite32(1, pci->dbi_base + PCIE_DBI_RO_WR_EN);
149 ls_pcie_fix_class(pcie);
150 ls_pcie_clear_multifunction(pcie);
151 iowrite32(0, pci->dbi_base + PCIE_DBI_RO_WR_EN);
152
153 ls_pcie_drop_msg_tlp(pcie);
154
155 dw_pcie_setup_rc(pp);
156
157 return 0;
158}
159
Bjorn Andersson4a301762017-07-15 23:39:45 -0700160static int ls1021_pcie_host_init(struct pcie_port *pp)
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500161{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530162 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
163 struct ls_pcie *pcie = to_ls_pcie(pci);
164 struct device *dev = pci->dev;
Minghuan Lian1195c102016-02-29 17:24:15 -0600165 u32 index[2];
Bjorn Andersson4a301762017-07-15 23:39:45 -0700166 int ret;
Minghuan Liand6463342015-10-16 15:19:17 +0800167
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500168 pcie->scfg = syscon_regmap_lookup_by_phandle(dev->of_node,
Minghuan Liand6463342015-10-16 15:19:17 +0800169 "fsl,pcie-scfg");
170 if (IS_ERR(pcie->scfg)) {
Bjorn Andersson4a301762017-07-15 23:39:45 -0700171 ret = PTR_ERR(pcie->scfg);
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500172 dev_err(dev, "No syscfg phandle specified\n");
Minghuan Liand6463342015-10-16 15:19:17 +0800173 pcie->scfg = NULL;
Bjorn Andersson4a301762017-07-15 23:39:45 -0700174 return ret;
Minghuan Liand6463342015-10-16 15:19:17 +0800175 }
176
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500177 if (of_property_read_u32_array(dev->of_node,
Minghuan Liand6463342015-10-16 15:19:17 +0800178 "fsl,pcie-scfg", index, 2)) {
179 pcie->scfg = NULL;
Bjorn Andersson4a301762017-07-15 23:39:45 -0700180 return -EINVAL;
Minghuan Liand6463342015-10-16 15:19:17 +0800181 }
182 pcie->index = index[1];
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500183
Hou Zhiqiangfa92dba2017-08-28 18:52:57 +0800184 return ls_pcie_host_init(pp);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800185}
186
Minghuan Lianbd33b872015-10-16 15:19:20 +0800187static int ls_pcie_msi_host_init(struct pcie_port *pp,
188 struct msi_controller *chip)
189{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530190 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
191 struct device *dev = pci->dev;
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500192 struct device_node *np = dev->of_node;
Minghuan Lianbd33b872015-10-16 15:19:20 +0800193 struct device_node *msi_node;
Minghuan Lianbd33b872015-10-16 15:19:20 +0800194
195 /*
196 * The MSI domain is set by the generic of_msi_configure(). This
197 * .msi_host_init() function keeps us from doing the default MSI
198 * domain setup in dw_pcie_host_init() and also enforces the
199 * requirement that "msi-parent" exists.
200 */
201 msi_node = of_parse_phandle(np, "msi-parent", 0);
202 if (!msi_node) {
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500203 dev_err(dev, "failed to find msi-parent\n");
Minghuan Lianbd33b872015-10-16 15:19:20 +0800204 return -EINVAL;
205 }
206
207 return 0;
208}
209
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800210static const struct dw_pcie_host_ops ls1021_pcie_host_ops = {
Minghuan Liand6463342015-10-16 15:19:17 +0800211 .host_init = ls1021_pcie_host_init,
Minghuan Lianbd33b872015-10-16 15:19:20 +0800212 .msi_host_init = ls_pcie_msi_host_init,
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800213};
214
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800215static const struct dw_pcie_host_ops ls_pcie_host_ops = {
Minghuan Lian5192ec72015-10-16 15:19:19 +0800216 .host_init = ls_pcie_host_init,
Minghuan Lianbd33b872015-10-16 15:19:20 +0800217 .msi_host_init = ls_pcie_msi_host_init,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800218};
219
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530220static const struct dw_pcie_ops dw_ls1021_pcie_ops = {
221 .link_up = ls1021_pcie_link_up,
222};
223
224static const struct dw_pcie_ops dw_ls_pcie_ops = {
225 .link_up = ls_pcie_link_up,
226};
227
Minghuan Liand6463342015-10-16 15:19:17 +0800228static struct ls_pcie_drvdata ls1021_drvdata = {
229 .ops = &ls1021_pcie_host_ops,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530230 .dw_pcie_ops = &dw_ls1021_pcie_ops,
Minghuan Liand6463342015-10-16 15:19:17 +0800231};
232
Minghuan Lian5192ec72015-10-16 15:19:19 +0800233static struct ls_pcie_drvdata ls1043_drvdata = {
234 .lut_offset = 0x10000,
235 .ltssm_shift = 24,
Mingkai Hu1d770402016-10-25 20:36:56 +0800236 .lut_dbg = 0x7fc,
237 .ops = &ls_pcie_host_ops,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530238 .dw_pcie_ops = &dw_ls_pcie_ops,
Mingkai Hu1d770402016-10-25 20:36:56 +0800239};
240
241static struct ls_pcie_drvdata ls1046_drvdata = {
242 .lut_offset = 0x80000,
243 .ltssm_shift = 24,
244 .lut_dbg = 0x407fc,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800245 .ops = &ls_pcie_host_ops,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530246 .dw_pcie_ops = &dw_ls_pcie_ops,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800247};
248
249static struct ls_pcie_drvdata ls2080_drvdata = {
250 .lut_offset = 0x80000,
251 .ltssm_shift = 0,
Mingkai Hu1d770402016-10-25 20:36:56 +0800252 .lut_dbg = 0x7fc,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800253 .ops = &ls_pcie_host_ops,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530254 .dw_pcie_ops = &dw_ls_pcie_ops,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800255};
256
Minghuan Liand6463342015-10-16 15:19:17 +0800257static const struct of_device_id ls_pcie_of_match[] = {
258 { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
Minghuan Lian5192ec72015-10-16 15:19:19 +0800259 { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
Mingkai Hu1d770402016-10-25 20:36:56 +0800260 { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
Minghuan Lian5192ec72015-10-16 15:19:19 +0800261 { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
Yang Shidbae40b2016-01-27 09:32:05 -0800262 { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
Minghuan Liand6463342015-10-16 15:19:17 +0800263 { },
264};
Minghuan Liand6463342015-10-16 15:19:17 +0800265
Bjorn Helgaas4726a822016-10-06 13:38:06 -0500266static int __init ls_add_pcie_port(struct ls_pcie *pcie)
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800267{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530268 struct dw_pcie *pci = pcie->pci;
269 struct pcie_port *pp = &pci->pp;
270 struct device *dev = pci->dev;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800271 int ret;
272
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530273 pp->ops = pcie->drvdata->ops;
274
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800275 ret = dw_pcie_host_init(pp);
276 if (ret) {
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500277 dev_err(dev, "failed to initialize host\n");
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800278 return ret;
279 }
280
281 return 0;
282}
283
284static int __init ls_pcie_probe(struct platform_device *pdev)
285{
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500286 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530287 struct dw_pcie *pci;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800288 struct ls_pcie *pcie;
289 struct resource *dbi_base;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800290 int ret;
291
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500292 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800293 if (!pcie)
294 return -ENOMEM;
295
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530296 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
297 if (!pci)
298 return -ENOMEM;
299
Bjorn Helgaas6dc2c042017-01-31 16:36:11 -0600300 pcie->drvdata = of_device_get_match_data(dev);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530301
302 pci->dev = dev;
303 pci->ops = pcie->drvdata->dw_pcie_ops;
Bjorn Helgaasfefe6732016-10-06 13:38:06 -0500304
Guenter Roeckc0464062017-02-25 02:08:12 -0800305 pcie->pci = pci;
306
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800307 dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
Lorenzo Pieralisi01bd4892017-04-19 17:49:08 +0100308 pci->dbi_base = devm_pci_remap_cfg_resource(dev, dbi_base);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530309 if (IS_ERR(pci->dbi_base))
310 return PTR_ERR(pci->dbi_base);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800311
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530312 pcie->lut = pci->dbi_base + pcie->drvdata->lut_offset;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800313
Minghuan Lian7af4ce32015-10-16 15:19:16 +0800314 if (!ls_pcie_is_bridge(pcie))
315 return -ENODEV;
316
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530317 platform_set_drvdata(pdev, pcie);
318
Bjorn Helgaas4726a822016-10-06 13:38:06 -0500319 ret = ls_add_pcie_port(pcie);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800320 if (ret < 0)
321 return ret;
322
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800323 return 0;
324}
325
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800326static struct platform_driver ls_pcie_driver = {
327 .driver = {
328 .name = "layerscape-pcie",
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800329 .of_match_table = ls_pcie_of_match,
Brian Norrisa5f40e82017-04-20 15:36:25 -0500330 .suppress_bind_attrs = true,
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800331 },
332};
Paul Gortmaker154fb602016-07-02 19:13:27 -0400333builtin_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);