blob: 0f6dd445640938b200f9b80e687385f084f2abde [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>
15#include <linux/module.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
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
38/* PEX LUT registers */
39#define PCIE_LUT_DBG 0x7FC /* PEX LUT Debug Register */
Minghuan Lian62d0ff832014-11-05 16:45:11 +080040
Minghuan Liand6463342015-10-16 15:19:17 +080041struct ls_pcie_drvdata {
Minghuan Lian5192ec72015-10-16 15:19:19 +080042 u32 lut_offset;
43 u32 ltssm_shift;
Minghuan Liand6463342015-10-16 15:19:17 +080044 struct pcie_host_ops *ops;
45};
46
Minghuan Lian62d0ff832014-11-05 16:45:11 +080047struct ls_pcie {
Minghuan Lian62d0ff832014-11-05 16:45:11 +080048 void __iomem *dbi;
Minghuan Lian5192ec72015-10-16 15:19:19 +080049 void __iomem *lut;
Minghuan Lian62d0ff832014-11-05 16:45:11 +080050 struct regmap *scfg;
51 struct pcie_port pp;
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
56#define to_ls_pcie(x) container_of(x, struct ls_pcie, pp)
57
Minghuan Lian7af4ce32015-10-16 15:19:16 +080058static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
59{
60 u32 header_type;
61
62 header_type = ioread8(pcie->dbi + PCI_HEADER_TYPE);
63 header_type &= 0x7f;
64
65 return header_type == PCI_HEADER_TYPE_BRIDGE;
66}
67
Minghuan Lian5192ec72015-10-16 15:19:19 +080068/* Clear multi-function bit */
69static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
70{
71 iowrite8(PCI_HEADER_TYPE_BRIDGE, pcie->dbi + PCI_HEADER_TYPE);
72}
73
74/* Fix class value */
75static void ls_pcie_fix_class(struct ls_pcie *pcie)
76{
77 iowrite16(PCI_CLASS_BRIDGE_PCI, pcie->dbi + PCI_CLASS_DEVICE);
78}
79
Minghuan Liand6463342015-10-16 15:19:17 +080080static int ls1021_pcie_link_up(struct pcie_port *pp)
Minghuan Lian62d0ff832014-11-05 16:45:11 +080081{
82 u32 state;
83 struct ls_pcie *pcie = to_ls_pcie(pp);
84
Minghuan Liand6463342015-10-16 15:19:17 +080085 if (!pcie->scfg)
86 return 0;
87
Minghuan Lian62d0ff832014-11-05 16:45:11 +080088 regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
89 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
90
91 if (state < LTSSM_PCIE_L0)
92 return 0;
93
94 return 1;
95}
96
Minghuan Liand6463342015-10-16 15:19:17 +080097static void ls1021_pcie_host_init(struct pcie_port *pp)
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -050098{
99 struct ls_pcie *pcie = to_ls_pcie(pp);
Minghuan Liand6463342015-10-16 15:19:17 +0800100 u32 val, index[2];
101
102 pcie->scfg = syscon_regmap_lookup_by_phandle(pp->dev->of_node,
103 "fsl,pcie-scfg");
104 if (IS_ERR(pcie->scfg)) {
105 dev_err(pp->dev, "No syscfg phandle specified\n");
106 pcie->scfg = NULL;
107 return;
108 }
109
110 if (of_property_read_u32_array(pp->dev->of_node,
111 "fsl,pcie-scfg", index, 2)) {
112 pcie->scfg = NULL;
113 return;
114 }
115 pcie->index = index[1];
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500116
117 dw_pcie_setup_rc(pp);
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -0500118
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800119 /*
120 * LS1021A Workaround for internal TKT228622
121 * to fix the INTx hang issue
122 */
123 val = ioread32(pcie->dbi + PCIE_STRFMR1);
124 val &= 0xffff;
125 iowrite32(val, pcie->dbi + PCIE_STRFMR1);
126}
127
Minghuan Lian5192ec72015-10-16 15:19:19 +0800128static int ls_pcie_link_up(struct pcie_port *pp)
129{
130 struct ls_pcie *pcie = to_ls_pcie(pp);
131 u32 state;
132
133 state = (ioread32(pcie->lut + PCIE_LUT_DBG) >>
134 pcie->drvdata->ltssm_shift) &
135 LTSSM_STATE_MASK;
136
137 if (state < LTSSM_PCIE_L0)
138 return 0;
139
140 return 1;
141}
142
143static void ls_pcie_host_init(struct pcie_port *pp)
144{
145 struct ls_pcie *pcie = to_ls_pcie(pp);
146
147 iowrite32(1, pcie->dbi + PCIE_DBI_RO_WR_EN);
148 ls_pcie_fix_class(pcie);
149 ls_pcie_clear_multifunction(pcie);
150 iowrite32(0, pcie->dbi + PCIE_DBI_RO_WR_EN);
151}
152
Minghuan Liand6463342015-10-16 15:19:17 +0800153static struct pcie_host_ops ls1021_pcie_host_ops = {
154 .link_up = ls1021_pcie_link_up,
155 .host_init = ls1021_pcie_host_init,
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800156};
157
Minghuan Lian5192ec72015-10-16 15:19:19 +0800158static struct pcie_host_ops ls_pcie_host_ops = {
159 .link_up = ls_pcie_link_up,
160 .host_init = ls_pcie_host_init,
161};
162
Minghuan Liand6463342015-10-16 15:19:17 +0800163static struct ls_pcie_drvdata ls1021_drvdata = {
164 .ops = &ls1021_pcie_host_ops,
165};
166
Minghuan Lian5192ec72015-10-16 15:19:19 +0800167static struct ls_pcie_drvdata ls1043_drvdata = {
168 .lut_offset = 0x10000,
169 .ltssm_shift = 24,
170 .ops = &ls_pcie_host_ops,
171};
172
173static struct ls_pcie_drvdata ls2080_drvdata = {
174 .lut_offset = 0x80000,
175 .ltssm_shift = 0,
176 .ops = &ls_pcie_host_ops,
177};
178
Minghuan Liand6463342015-10-16 15:19:17 +0800179static const struct of_device_id ls_pcie_of_match[] = {
180 { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
Minghuan Lian5192ec72015-10-16 15:19:19 +0800181 { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
182 { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
Minghuan Liand6463342015-10-16 15:19:17 +0800183 { },
184};
185MODULE_DEVICE_TABLE(of, ls_pcie_of_match);
186
Minghuan Liana167fb72015-10-16 15:19:18 +0800187static int __init ls_add_pcie_port(struct pcie_port *pp,
188 struct platform_device *pdev)
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800189{
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800190 int ret;
Minghuan Liana167fb72015-10-16 15:19:18 +0800191 struct ls_pcie *pcie = to_ls_pcie(pp);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800192
Minghuan Liana167fb72015-10-16 15:19:18 +0800193 pp->dev = &pdev->dev;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800194 pp->dbi_base = pcie->dbi;
Minghuan Liand6463342015-10-16 15:19:17 +0800195 pp->ops = pcie->drvdata->ops;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800196
197 ret = dw_pcie_host_init(pp);
198 if (ret) {
199 dev_err(pp->dev, "failed to initialize host\n");
200 return ret;
201 }
202
203 return 0;
204}
205
206static int __init ls_pcie_probe(struct platform_device *pdev)
207{
Minghuan Liand6463342015-10-16 15:19:17 +0800208 const struct of_device_id *match;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800209 struct ls_pcie *pcie;
210 struct resource *dbi_base;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800211 int ret;
212
Minghuan Liand6463342015-10-16 15:19:17 +0800213 match = of_match_device(ls_pcie_of_match, &pdev->dev);
214 if (!match)
215 return -ENODEV;
216
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800217 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
218 if (!pcie)
219 return -ENOMEM;
220
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800221 dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800222 pcie->dbi = devm_ioremap_resource(&pdev->dev, dbi_base);
Bjorn Helgaase3dc17a2015-04-09 14:36:52 -0500223 if (IS_ERR(pcie->dbi)) {
224 dev_err(&pdev->dev, "missing *regs* space\n");
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800225 return PTR_ERR(pcie->dbi);
Bjorn Helgaase3dc17a2015-04-09 14:36:52 -0500226 }
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800227
Minghuan Liand6463342015-10-16 15:19:17 +0800228 pcie->drvdata = match->data;
Minghuan Lian5192ec72015-10-16 15:19:19 +0800229 pcie->lut = pcie->dbi + pcie->drvdata->lut_offset;
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800230
Minghuan Lian7af4ce32015-10-16 15:19:16 +0800231 if (!ls_pcie_is_bridge(pcie))
232 return -ENODEV;
233
Minghuan Liana167fb72015-10-16 15:19:18 +0800234 ret = ls_add_pcie_port(&pcie->pp, pdev);
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800235 if (ret < 0)
236 return ret;
237
238 platform_set_drvdata(pdev, pcie);
239
240 return 0;
241}
242
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800243static struct platform_driver ls_pcie_driver = {
244 .driver = {
245 .name = "layerscape-pcie",
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800246 .of_match_table = ls_pcie_of_match,
247 },
248};
249
250module_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);
251
252MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@freescale.com>");
253MODULE_DESCRIPTION("Freescale Layerscape PCIe host controller driver");
254MODULE_LICENSE("GPL v2");