blob: 5eabe9238f0ed87a87cd4a0b0ca2ab18853f3e7f [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 *
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>
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
34/* Symbol Timer Register and Filter Mask Register 1 */
35#define PCIE_STRFMR1 0x71c
36
37struct ls_pcie {
38 struct list_head node;
39 struct device *dev;
40 struct pci_bus *bus;
41 void __iomem *dbi;
42 struct regmap *scfg;
43 struct pcie_port pp;
44 int index;
45 int msi_irq;
46};
47
48#define to_ls_pcie(x) container_of(x, struct ls_pcie, pp)
49
Minghuan Lian7af4ce32015-10-16 15:19:16 +080050static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
51{
52 u32 header_type;
53
54 header_type = ioread8(pcie->dbi + PCI_HEADER_TYPE);
55 header_type &= 0x7f;
56
57 return header_type == PCI_HEADER_TYPE_BRIDGE;
58}
59
Minghuan Lian62d0ff832014-11-05 16:45:11 +080060static int ls_pcie_link_up(struct pcie_port *pp)
61{
62 u32 state;
63 struct ls_pcie *pcie = to_ls_pcie(pp);
64
65 regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
66 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
67
68 if (state < LTSSM_PCIE_L0)
69 return 0;
70
71 return 1;
72}
73
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -050074static void ls_pcie_host_init(struct pcie_port *pp)
75{
76 struct ls_pcie *pcie = to_ls_pcie(pp);
77 u32 val;
78
79 dw_pcie_setup_rc(pp);
Bjorn Helgaas1d3f9ba2015-06-02 16:24:25 -050080
Minghuan Lian62d0ff832014-11-05 16:45:11 +080081 /*
82 * LS1021A Workaround for internal TKT228622
83 * to fix the INTx hang issue
84 */
85 val = ioread32(pcie->dbi + PCIE_STRFMR1);
86 val &= 0xffff;
87 iowrite32(val, pcie->dbi + PCIE_STRFMR1);
88}
89
90static struct pcie_host_ops ls_pcie_host_ops = {
91 .link_up = ls_pcie_link_up,
92 .host_init = ls_pcie_host_init,
93};
94
95static int ls_add_pcie_port(struct ls_pcie *pcie)
96{
97 struct pcie_port *pp;
98 int ret;
99
100 pp = &pcie->pp;
101 pp->dev = pcie->dev;
102 pp->dbi_base = pcie->dbi;
103 pp->root_bus_nr = -1;
104 pp->ops = &ls_pcie_host_ops;
105
106 ret = dw_pcie_host_init(pp);
107 if (ret) {
108 dev_err(pp->dev, "failed to initialize host\n");
109 return ret;
110 }
111
112 return 0;
113}
114
115static int __init ls_pcie_probe(struct platform_device *pdev)
116{
117 struct ls_pcie *pcie;
118 struct resource *dbi_base;
119 u32 index[2];
120 int ret;
121
122 pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
123 if (!pcie)
124 return -ENOMEM;
125
126 pcie->dev = &pdev->dev;
127
128 dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800129 pcie->dbi = devm_ioremap_resource(&pdev->dev, dbi_base);
Bjorn Helgaase3dc17a2015-04-09 14:36:52 -0500130 if (IS_ERR(pcie->dbi)) {
131 dev_err(&pdev->dev, "missing *regs* space\n");
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800132 return PTR_ERR(pcie->dbi);
Bjorn Helgaase3dc17a2015-04-09 14:36:52 -0500133 }
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800134
135 pcie->scfg = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
136 "fsl,pcie-scfg");
137 if (IS_ERR(pcie->scfg)) {
138 dev_err(&pdev->dev, "No syscfg phandle specified\n");
139 return PTR_ERR(pcie->scfg);
140 }
141
142 ret = of_property_read_u32_array(pdev->dev.of_node,
143 "fsl,pcie-scfg", index, 2);
144 if (ret)
145 return ret;
146 pcie->index = index[1];
147
Minghuan Lian7af4ce32015-10-16 15:19:16 +0800148 if (!ls_pcie_is_bridge(pcie))
149 return -ENODEV;
150
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800151 ret = ls_add_pcie_port(pcie);
152 if (ret < 0)
153 return ret;
154
155 platform_set_drvdata(pdev, pcie);
156
157 return 0;
158}
159
160static const struct of_device_id ls_pcie_of_match[] = {
161 { .compatible = "fsl,ls1021a-pcie" },
162 { },
163};
164MODULE_DEVICE_TABLE(of, ls_pcie_of_match);
165
166static struct platform_driver ls_pcie_driver = {
167 .driver = {
168 .name = "layerscape-pcie",
Minghuan Lian62d0ff832014-11-05 16:45:11 +0800169 .of_match_table = ls_pcie_of_match,
170 },
171};
172
173module_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);
174
175MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@freescale.com>");
176MODULE_DESCRIPTION("Freescale Layerscape PCIe host controller driver");
177MODULE_LICENSE("GPL v2");