blob: ea789138531be438ebeac3c0de62a47adf6c84eb [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;
Minghuan Liand6463342015-10-16 15:19:17 +080042 struct pcie_host_ops *ops;
43};
44
Minghuan Lian62d0ff832014-11-05 16:45:11 +080045struct ls_pcie {
Bjorn Helgaas6caaa282016-10-06 13:38:05 -050046 struct pcie_port pp; /* pp.dbi_base is DT regs */
Minghuan Lian5192ec72015-10-16 15:19:19 +080047 void __iomem *lut;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080048 struct regmap *scfg;
Minghuan Liand6463342015-10-16 15:19:17 +080049 const struct ls_pcie_drvdata *drvdata;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080050 int index;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080051};
52
53#define to_ls_pcie(x) container_of(x, struct ls_pcie, pp)
54
Minghuan Lian7af4ce32015-10-16 15:19:16 +080055static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
56{
57 u32 header_type;
58
Bjorn Helgaasd41d2952016-10-06 13:38:05 -050059 header_type = ioread8(pcie->pp.dbi_base + PCI_HEADER_TYPE);
Minghuan Lian7af4ce32015-10-16 15:19:16 +080060 header_type &= 0x7f;
61
62 return header_type == PCI_HEADER_TYPE_BRIDGE;
63}
64
Minghuan Lian5192ec72015-10-16 15:19:19 +080065/* Clear multi-function bit */
66static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
67{
Bjorn Helgaasd41d2952016-10-06 13:38:05 -050068 iowrite8(PCI_HEADER_TYPE_BRIDGE, pcie->pp.dbi_base + PCI_HEADER_TYPE);
Minghuan Lian5192ec72015-10-16 15:19:19 +080069}
70
71/* Fix class value */
72static void ls_pcie_fix_class(struct ls_pcie *pcie)
73{
Bjorn Helgaasd41d2952016-10-06 13:38:05 -050074 iowrite16(PCI_CLASS_BRIDGE_PCI, pcie->pp.dbi_base + PCI_CLASS_DEVICE);
Minghuan Lian5192ec72015-10-16 15:19:19 +080075}
76
Minghuan Lian1195c102016-02-29 17:24:15 -060077/* Drop MSG TLP except for Vendor MSG */
78static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
79{
80 u32 val;
81
Bjorn Helgaasd41d2952016-10-06 13:38:05 -050082 val = ioread32(pcie->pp.dbi_base + PCIE_STRFMR1);
Minghuan Lian1195c102016-02-29 17:24:15 -060083 val &= 0xDFFFFFFF;
Bjorn Helgaasd41d2952016-10-06 13:38:05 -050084 iowrite32(val, pcie->pp.dbi_base + PCIE_STRFMR1);
Minghuan Lian1195c102016-02-29 17:24:15 -060085}
86
Minghuan Liand6463342015-10-16 15:19:17 +080087static int ls1021_pcie_link_up(struct pcie_port *pp)
Minghuan Lian62d0ff832014-11-05 16:45:11 +080088{
89 u32 state;
90 struct ls_pcie *pcie = to_ls_pcie(pp);
91
Minghuan Liand6463342015-10-16 15:19:17 +080092 if (!pcie->scfg)
93 return 0;
94
Minghuan Lian62d0ff832014-11-05 16:45:11 +080095 regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
96 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
97
98 if (state < LTSSM_PCIE_L0)
99 return 0;
100
101 return 1;
102}
103
Minghuan Liand6463342015-10-16 15:19:17 +0800104static void ls1021_pcie_host_init(struct pcie_port *pp)
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500105{
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500106 struct device *dev = pp->dev;
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500107 struct ls_pcie *pcie = to_ls_pcie(pp);
Minghuan Lian1195c102016-02-29 17:24:15 -0600108 u32 index[2];
Minghuan Liand6463342015-10-16 15:19:17 +0800109
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500110 pcie->scfg = syscon_regmap_lookup_by_phandle(dev->of_node,
Minghuan Liand6463342015-10-16 15:19:17 +0800111 "fsl,pcie-scfg");
112 if (IS_ERR(pcie->scfg)) {
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500113 dev_err(dev, "No syscfg phandle specified\n");
Minghuan Liand6463342015-10-16 15:19:17 +0800114 pcie->scfg = NULL;
115 return;
116 }
117
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500118 if (of_property_read_u32_array(dev->of_node,
Minghuan Liand6463342015-10-16 15:19:17 +0800119 "fsl,pcie-scfg", index, 2)) {
120 pcie->scfg = NULL;
121 return;
122 }
123 pcie->index = index[1];
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500124
125 dw_pcie_setup_rc(pp);
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500126
Minghuan Lian1195c102016-02-29 17:24:15 -0600127 ls_pcie_drop_msg_tlp(pcie);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800128}
129
Minghuan Lian5192ec72015-10-16 15:19:19 +0800130static int ls_pcie_link_up(struct pcie_port *pp)
131{
132 struct ls_pcie *pcie = to_ls_pcie(pp);
133 u32 state;
134
Mingkai Hu1d770402016-10-25 20:36:56 +0800135 state = (ioread32(pcie->lut + pcie->drvdata->lut_dbg) >>
Minghuan Lian5192ec72015-10-16 15:19:19 +0800136 pcie->drvdata->ltssm_shift) &
137 LTSSM_STATE_MASK;
138
139 if (state < LTSSM_PCIE_L0)
140 return 0;
141
142 return 1;
143}
144
145static void ls_pcie_host_init(struct pcie_port *pp)
146{
147 struct ls_pcie *pcie = to_ls_pcie(pp);
148
Bjorn Helgaasd41d2952016-10-06 13:38:05 -0500149 iowrite32(1, pcie->pp.dbi_base + PCIE_DBI_RO_WR_EN);
Minghuan Lian5192ec72015-10-16 15:19:19 +0800150 ls_pcie_fix_class(pcie);
151 ls_pcie_clear_multifunction(pcie);
Minghuan Lian1195c102016-02-29 17:24:15 -0600152 ls_pcie_drop_msg_tlp(pcie);
Bjorn Helgaasd41d2952016-10-06 13:38:05 -0500153 iowrite32(0, pcie->pp.dbi_base + PCIE_DBI_RO_WR_EN);
Minghuan Lian5192ec72015-10-16 15:19:19 +0800154}
155
Minghuan Lianbd33b872015-10-16 15:19:20 +0800156static int ls_pcie_msi_host_init(struct pcie_port *pp,
157 struct msi_controller *chip)
158{
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500159 struct device *dev = pp->dev;
160 struct device_node *np = dev->of_node;
Minghuan Lianbd33b872015-10-16 15:19:20 +0800161 struct device_node *msi_node;
Minghuan Lianbd33b872015-10-16 15:19:20 +0800162
163 /*
164 * The MSI domain is set by the generic of_msi_configure(). This
165 * .msi_host_init() function keeps us from doing the default MSI
166 * domain setup in dw_pcie_host_init() and also enforces the
167 * requirement that "msi-parent" exists.
168 */
169 msi_node = of_parse_phandle(np, "msi-parent", 0);
170 if (!msi_node) {
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500171 dev_err(dev, "failed to find msi-parent\n");
Minghuan Lianbd33b872015-10-16 15:19:20 +0800172 return -EINVAL;
173 }
174
175 return 0;
176}
177
Minghuan Liand6463342015-10-16 15:19:17 +0800178static struct pcie_host_ops ls1021_pcie_host_ops = {
179 .link_up = ls1021_pcie_link_up,
180 .host_init = ls1021_pcie_host_init,
Minghuan Lianbd33b872015-10-16 15:19:20 +0800181 .msi_host_init = ls_pcie_msi_host_init,
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800182};
183
Minghuan Lian5192ec72015-10-16 15:19:19 +0800184static struct pcie_host_ops ls_pcie_host_ops = {
185 .link_up = ls_pcie_link_up,
186 .host_init = ls_pcie_host_init,
Minghuan Lianbd33b872015-10-16 15:19:20 +0800187 .msi_host_init = ls_pcie_msi_host_init,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800188};
189
Minghuan Liand6463342015-10-16 15:19:17 +0800190static struct ls_pcie_drvdata ls1021_drvdata = {
191 .ops = &ls1021_pcie_host_ops,
192};
193
Minghuan Lian5192ec72015-10-16 15:19:19 +0800194static struct ls_pcie_drvdata ls1043_drvdata = {
195 .lut_offset = 0x10000,
196 .ltssm_shift = 24,
Mingkai Hu1d770402016-10-25 20:36:56 +0800197 .lut_dbg = 0x7fc,
198 .ops = &ls_pcie_host_ops,
199};
200
201static struct ls_pcie_drvdata ls1046_drvdata = {
202 .lut_offset = 0x80000,
203 .ltssm_shift = 24,
204 .lut_dbg = 0x407fc,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800205 .ops = &ls_pcie_host_ops,
206};
207
208static struct ls_pcie_drvdata ls2080_drvdata = {
209 .lut_offset = 0x80000,
210 .ltssm_shift = 0,
Mingkai Hu1d770402016-10-25 20:36:56 +0800211 .lut_dbg = 0x7fc,
Minghuan Lian5192ec72015-10-16 15:19:19 +0800212 .ops = &ls_pcie_host_ops,
213};
214
Minghuan Liand6463342015-10-16 15:19:17 +0800215static const struct of_device_id ls_pcie_of_match[] = {
216 { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
Minghuan Lian5192ec72015-10-16 15:19:19 +0800217 { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
Mingkai Hu1d770402016-10-25 20:36:56 +0800218 { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
Minghuan Lian5192ec72015-10-16 15:19:19 +0800219 { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
Yang Shidbae40b2016-01-27 09:32:05 -0800220 { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
Minghuan Liand6463342015-10-16 15:19:17 +0800221 { },
222};
Minghuan Liand6463342015-10-16 15:19:17 +0800223
Bjorn Helgaas4726a822016-10-06 13:38:06 -0500224static int __init ls_add_pcie_port(struct ls_pcie *pcie)
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800225{
Bjorn Helgaas7b0b1112016-10-06 13:38:05 -0500226 struct pcie_port *pp = &pcie->pp;
Bjorn Helgaasfefe6732016-10-06 13:38:06 -0500227 struct device *dev = pp->dev;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800228 int ret;
229
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800230 ret = dw_pcie_host_init(pp);
231 if (ret) {
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500232 dev_err(dev, "failed to initialize host\n");
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800233 return ret;
234 }
235
236 return 0;
237}
238
239static int __init ls_pcie_probe(struct platform_device *pdev)
240{
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500241 struct device *dev = &pdev->dev;
Minghuan Liand6463342015-10-16 15:19:17 +0800242 const struct of_device_id *match;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800243 struct ls_pcie *pcie;
Bjorn Helgaasfefe6732016-10-06 13:38:06 -0500244 struct pcie_port *pp;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800245 struct resource *dbi_base;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800246 int ret;
247
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500248 match = of_match_device(ls_pcie_of_match, dev);
Minghuan Liand6463342015-10-16 15:19:17 +0800249 if (!match)
250 return -ENODEV;
251
Bjorn Helgaasc11125e2016-10-06 13:38:05 -0500252 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800253 if (!pcie)
254 return -ENOMEM;
255
Bjorn Helgaasfefe6732016-10-06 13:38:06 -0500256 pp = &pcie->pp;
257 pp->dev = dev;
Marc Zyngier15480f32016-10-17 11:39:32 +0100258 pcie->drvdata = match->data;
Bjorn Helgaasfefe6732016-10-06 13:38:06 -0500259 pp->ops = pcie->drvdata->ops;
260
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800261 dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
Bjorn Helgaasd41d2952016-10-06 13:38:05 -0500262 pcie->pp.dbi_base = devm_ioremap_resource(dev, dbi_base);
Wei Yongjune5942332016-10-17 14:55:40 +0000263 if (IS_ERR(pcie->pp.dbi_base))
Bjorn Helgaasd41d2952016-10-06 13:38:05 -0500264 return PTR_ERR(pcie->pp.dbi_base);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800265
Bjorn Helgaasd41d2952016-10-06 13:38:05 -0500266 pcie->lut = pcie->pp.dbi_base + pcie->drvdata->lut_offset;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800267
Minghuan Lian7af4ce32015-10-16 15:19:16 +0800268 if (!ls_pcie_is_bridge(pcie))
269 return -ENODEV;
270
Bjorn Helgaas4726a822016-10-06 13:38:06 -0500271 ret = ls_add_pcie_port(pcie);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800272 if (ret < 0)
273 return ret;
274
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800275 return 0;
276}
277
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800278static struct platform_driver ls_pcie_driver = {
279 .driver = {
280 .name = "layerscape-pcie",
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800281 .of_match_table = ls_pcie_of_match,
282 },
283};
Paul Gortmaker154fb602016-07-02 19:13:27 -0400284builtin_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);