]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/pci/host/pci-layerscape.c
7d61cbb9bab1e099f531b3a709e69d70e0355dec
[karo-tx-linux.git] / drivers / pci / host / pci-layerscape.c
1 /*
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>
14 #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
37 struct ls_pcie_drvdata {
38         struct pcie_host_ops *ops;
39 };
40
41 struct ls_pcie {
42         void __iomem *dbi;
43         struct regmap *scfg;
44         struct pcie_port pp;
45         const struct ls_pcie_drvdata *drvdata;
46         int index;
47         int msi_irq;
48 };
49
50 #define to_ls_pcie(x)   container_of(x, struct ls_pcie, pp)
51
52 static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
53 {
54         u32 header_type;
55
56         header_type = ioread8(pcie->dbi + PCI_HEADER_TYPE);
57         header_type &= 0x7f;
58
59         return header_type == PCI_HEADER_TYPE_BRIDGE;
60 }
61
62 static int ls1021_pcie_link_up(struct pcie_port *pp)
63 {
64         u32 state;
65         struct ls_pcie *pcie = to_ls_pcie(pp);
66
67         if (!pcie->scfg)
68                 return 0;
69
70         regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
71         state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
72
73         if (state < LTSSM_PCIE_L0)
74                 return 0;
75
76         return 1;
77 }
78
79 static void ls1021_pcie_host_init(struct pcie_port *pp)
80 {
81         struct ls_pcie *pcie = to_ls_pcie(pp);
82         u32 val, index[2];
83
84         pcie->scfg = syscon_regmap_lookup_by_phandle(pp->dev->of_node,
85                                                      "fsl,pcie-scfg");
86         if (IS_ERR(pcie->scfg)) {
87                 dev_err(pp->dev, "No syscfg phandle specified\n");
88                 pcie->scfg = NULL;
89                 return;
90         }
91
92         if (of_property_read_u32_array(pp->dev->of_node,
93                                        "fsl,pcie-scfg", index, 2)) {
94                 pcie->scfg = NULL;
95                 return;
96         }
97         pcie->index = index[1];
98
99         dw_pcie_setup_rc(pp);
100
101         /*
102          * LS1021A Workaround for internal TKT228622
103          * to fix the INTx hang issue
104          */
105         val = ioread32(pcie->dbi + PCIE_STRFMR1);
106         val &= 0xffff;
107         iowrite32(val, pcie->dbi + PCIE_STRFMR1);
108 }
109
110 static struct pcie_host_ops ls1021_pcie_host_ops = {
111         .link_up = ls1021_pcie_link_up,
112         .host_init = ls1021_pcie_host_init,
113 };
114
115 static struct ls_pcie_drvdata ls1021_drvdata = {
116         .ops = &ls1021_pcie_host_ops,
117 };
118
119 static const struct of_device_id ls_pcie_of_match[] = {
120         { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
121         { },
122 };
123 MODULE_DEVICE_TABLE(of, ls_pcie_of_match);
124
125 static int __init ls_add_pcie_port(struct pcie_port *pp,
126                                    struct platform_device *pdev)
127 {
128         int ret;
129         struct ls_pcie *pcie = to_ls_pcie(pp);
130
131         pp->dev = &pdev->dev;
132         pp->dbi_base = pcie->dbi;
133         pp->ops = pcie->drvdata->ops;
134
135         ret = dw_pcie_host_init(pp);
136         if (ret) {
137                 dev_err(pp->dev, "failed to initialize host\n");
138                 return ret;
139         }
140
141         return 0;
142 }
143
144 static int __init ls_pcie_probe(struct platform_device *pdev)
145 {
146         const struct of_device_id *match;
147         struct ls_pcie *pcie;
148         struct resource *dbi_base;
149         int ret;
150
151         match = of_match_device(ls_pcie_of_match, &pdev->dev);
152         if (!match)
153                 return -ENODEV;
154
155         pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
156         if (!pcie)
157                 return -ENOMEM;
158
159         dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
160         pcie->dbi = devm_ioremap_resource(&pdev->dev, dbi_base);
161         if (IS_ERR(pcie->dbi)) {
162                 dev_err(&pdev->dev, "missing *regs* space\n");
163                 return PTR_ERR(pcie->dbi);
164         }
165
166         pcie->drvdata = match->data;
167
168         if (!ls_pcie_is_bridge(pcie))
169                 return -ENODEV;
170
171         ret = ls_add_pcie_port(&pcie->pp, pdev);
172         if (ret < 0)
173                 return ret;
174
175         platform_set_drvdata(pdev, pcie);
176
177         return 0;
178 }
179
180 static struct platform_driver ls_pcie_driver = {
181         .driver = {
182                 .name = "layerscape-pcie",
183                 .of_match_table = ls_pcie_of_match,
184         },
185 };
186
187 module_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);
188
189 MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@freescale.com>");
190 MODULE_DESCRIPTION("Freescale Layerscape PCIe host controller driver");
191 MODULE_LICENSE("GPL v2");