Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * PCIe host controller driver for HiSilicon SoCs
3 *
4 * Copyright (C) 2015 HiSilicon Co., Ltd. http://www.hisilicon.com
5 *
6 * Authors: Zhou Wang <wangzhou1@hisilicon.com>
7 * Dacai Zhu <zhudacai@hisilicon.com>
8 * Gabriele Paoloni <gabriele.paoloni@huawei.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14#include <linux/interrupt.h>
15#include <linux/init.h>
16#include <linux/mfd/syscon.h>
17#include <linux/of_address.h>
18#include <linux/of_pci.h>
19#include <linux/platform_device.h>
20#include <linux/of_device.h>
21#include <linux/pci.h>
22#include <linux/pci-acpi.h>
23#include <linux/pci-ecam.h>
24#include <linux/regmap.h>
25#include "../pci.h"
26
27#if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
28
29static int hisi_pcie_acpi_rd_conf(struct pci_bus *bus, u32 devfn, int where,
30 int size, u32 *val)
31{
32 struct pci_config_window *cfg = bus->sysdata;
33 int dev = PCI_SLOT(devfn);
34
35 if (bus->number == cfg->busr.start) {
36 /* access only one slot on each root port */
37 if (dev > 0)
38 return PCIBIOS_DEVICE_NOT_FOUND;
39 else
40 return pci_generic_config_read32(bus, devfn, where,
41 size, val);
42 }
43
44 return pci_generic_config_read(bus, devfn, where, size, val);
45}
46
47static int hisi_pcie_acpi_wr_conf(struct pci_bus *bus, u32 devfn,
48 int where, int size, u32 val)
49{
50 struct pci_config_window *cfg = bus->sysdata;
51 int dev = PCI_SLOT(devfn);
52
53 if (bus->number == cfg->busr.start) {
54 /* access only one slot on each root port */
55 if (dev > 0)
56 return PCIBIOS_DEVICE_NOT_FOUND;
57 else
58 return pci_generic_config_write32(bus, devfn, where,
59 size, val);
60 }
61
62 return pci_generic_config_write(bus, devfn, where, size, val);
63}
64
65static void __iomem *hisi_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
66 int where)
67{
68 struct pci_config_window *cfg = bus->sysdata;
69 void __iomem *reg_base = cfg->priv;
70
71 if (bus->number == cfg->busr.start)
72 return reg_base + where;
73 else
74 return pci_ecam_map_bus(bus, devfn, where);
75}
76
77static int hisi_pcie_init(struct pci_config_window *cfg)
78{
79 struct device *dev = cfg->parent;
80 struct acpi_device *adev = to_acpi_device(dev);
81 struct acpi_pci_root *root = acpi_driver_data(adev);
82 struct resource *res;
83 void __iomem *reg_base;
84 int ret;
85
86 /*
87 * Retrieve RC base and size from a HISI0081 device with _UID
88 * matching our segment.
89 */
90 res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
91 if (!res)
92 return -ENOMEM;
93
94 ret = acpi_get_rc_resources(dev, "HISI0081", root->segment, res);
95 if (ret) {
96 dev_err(dev, "can't get rc base address\n");
97 return -ENOMEM;
98 }
99
100 reg_base = devm_ioremap(dev, res->start, resource_size(res));
101 if (!reg_base)
102 return -ENOMEM;
103
104 cfg->priv = reg_base;
105 return 0;
106}
107
108struct pci_ecam_ops hisi_pcie_ops = {
109 .bus_shift = 20,
110 .init = hisi_pcie_init,
111 .pci_ops = {
112 .map_bus = hisi_pcie_map_bus,
113 .read = hisi_pcie_acpi_rd_conf,
114 .write = hisi_pcie_acpi_wr_conf,
115 }
116};
117
118#endif
119
120#ifdef CONFIG_PCI_HISI
121
122#include "pcie-designware.h"
123
124#define PCIE_SUBCTRL_SYS_STATE4_REG 0x6818
125#define PCIE_HIP06_CTRL_OFF 0x1000
126#define PCIE_SYS_STATE4 (PCIE_HIP06_CTRL_OFF + 0x31c)
127#define PCIE_LTSSM_LINKUP_STATE 0x11
128#define PCIE_LTSSM_STATE_MASK 0x3F
129
130#define to_hisi_pcie(x) container_of(x, struct hisi_pcie, pp)
131
132struct hisi_pcie;
133
134struct pcie_soc_ops {
135 int (*hisi_pcie_link_up)(struct hisi_pcie *hisi_pcie);
136};
137
138struct hisi_pcie {
139 struct pcie_port pp; /* pp.dbi_base is DT rc_dbi */
140 struct regmap *subctrl;
141 u32 port_id;
142 struct pcie_soc_ops *soc_ops;
143};
144
145/* HipXX PCIe host only supports 32-bit config access */
146static int hisi_pcie_cfg_read(struct pcie_port *pp, int where, int size,
147 u32 *val)
148{
149 u32 reg;
150 u32 reg_val;
151 void *walker = ®_val;
152
153 walker += (where & 0x3);
154 reg = where & ~0x3;
155 reg_val = dw_pcie_readl_rc(pp, reg);
156
157 if (size == 1)
158 *val = *(u8 __force *) walker;
159 else if (size == 2)
160 *val = *(u16 __force *) walker;
161 else if (size == 4)
162 *val = reg_val;
163 else
164 return PCIBIOS_BAD_REGISTER_NUMBER;
165
166 return PCIBIOS_SUCCESSFUL;
167}
168
169/* HipXX PCIe host only supports 32-bit config access */
170static int hisi_pcie_cfg_write(struct pcie_port *pp, int where, int size,
171 u32 val)
172{
173 u32 reg_val;
174 u32 reg;
175 void *walker = ®_val;
176
177 walker += (where & 0x3);
178 reg = where & ~0x3;
179 if (size == 4)
180 dw_pcie_writel_rc(pp, reg, val);
181 else if (size == 2) {
182 reg_val = dw_pcie_readl_rc(pp, reg);
183 *(u16 __force *) walker = val;
184 dw_pcie_writel_rc(pp, reg, reg_val);
185 } else if (size == 1) {
186 reg_val = dw_pcie_readl_rc(pp, reg);
187 *(u8 __force *) walker = val;
188 dw_pcie_writel_rc(pp, reg, reg_val);
189 } else
190 return PCIBIOS_BAD_REGISTER_NUMBER;
191
192 return PCIBIOS_SUCCESSFUL;
193}
194
195static int hisi_pcie_link_up_hip05(struct hisi_pcie *hisi_pcie)
196{
197 u32 val;
198
199 regmap_read(hisi_pcie->subctrl, PCIE_SUBCTRL_SYS_STATE4_REG +
200 0x100 * hisi_pcie->port_id, &val);
201
202 return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
203}
204
205static int hisi_pcie_link_up_hip06(struct hisi_pcie *hisi_pcie)
206{
207 struct pcie_port *pp = &hisi_pcie->pp;
208 u32 val;
209
210 val = dw_pcie_readl_rc(pp, PCIE_SYS_STATE4);
211
212 return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
213}
214
215static int hisi_pcie_link_up(struct pcie_port *pp)
216{
217 struct hisi_pcie *hisi_pcie = to_hisi_pcie(pp);
218
219 return hisi_pcie->soc_ops->hisi_pcie_link_up(hisi_pcie);
220}
221
222static struct pcie_host_ops hisi_pcie_host_ops = {
223 .rd_own_conf = hisi_pcie_cfg_read,
224 .wr_own_conf = hisi_pcie_cfg_write,
225 .link_up = hisi_pcie_link_up,
226};
227
228static int hisi_add_pcie_port(struct hisi_pcie *hisi_pcie,
229 struct platform_device *pdev)
230{
231 struct pcie_port *pp = &hisi_pcie->pp;
232 struct device *dev = pp->dev;
233 int ret;
234 u32 port_id;
235
236 if (of_property_read_u32(dev->of_node, "port-id", &port_id)) {
237 dev_err(dev, "failed to read port-id\n");
238 return -EINVAL;
239 }
240 if (port_id > 3) {
241 dev_err(dev, "Invalid port-id: %d\n", port_id);
242 return -EINVAL;
243 }
244 hisi_pcie->port_id = port_id;
245
246 pp->ops = &hisi_pcie_host_ops;
247
248 ret = dw_pcie_host_init(pp);
249 if (ret) {
250 dev_err(dev, "failed to initialize host\n");
251 return ret;
252 }
253
254 return 0;
255}
256
257static int hisi_pcie_probe(struct platform_device *pdev)
258{
259 struct device *dev = &pdev->dev;
260 struct hisi_pcie *hisi_pcie;
261 struct pcie_port *pp;
262 const struct of_device_id *match;
263 struct resource *reg;
264 struct device_driver *driver;
265 int ret;
266
267 hisi_pcie = devm_kzalloc(dev, sizeof(*hisi_pcie), GFP_KERNEL);
268 if (!hisi_pcie)
269 return -ENOMEM;
270
271 pp = &hisi_pcie->pp;
272 pp->dev = dev;
273 driver = dev->driver;
274
275 match = of_match_device(driver->of_match_table, dev);
276 hisi_pcie->soc_ops = (struct pcie_soc_ops *) match->data;
277
278 hisi_pcie->subctrl =
279 syscon_regmap_lookup_by_compatible("hisilicon,pcie-sas-subctrl");
280 if (IS_ERR(hisi_pcie->subctrl)) {
281 dev_err(dev, "cannot get subctrl base\n");
282 return PTR_ERR(hisi_pcie->subctrl);
283 }
284
285 reg = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbi");
286 pp->dbi_base = devm_ioremap_resource(dev, reg);
287 if (IS_ERR(pp->dbi_base))
288 return PTR_ERR(pp->dbi_base);
289
290 ret = hisi_add_pcie_port(hisi_pcie, pdev);
291 if (ret)
292 return ret;
293
294 return 0;
295}
296
297static struct pcie_soc_ops hip05_ops = {
298 &hisi_pcie_link_up_hip05
299};
300
301static struct pcie_soc_ops hip06_ops = {
302 &hisi_pcie_link_up_hip06
303};
304
305static const struct of_device_id hisi_pcie_of_match[] = {
306 {
307 .compatible = "hisilicon,hip05-pcie",
308 .data = (void *) &hip05_ops,
309 },
310 {
311 .compatible = "hisilicon,hip06-pcie",
312 .data = (void *) &hip06_ops,
313 },
314 {},
315};
316
317static struct platform_driver hisi_pcie_driver = {
318 .probe = hisi_pcie_probe,
319 .driver = {
320 .name = "hisi-pcie",
321 .of_match_table = hisi_pcie_of_match,
322 },
323};
324builtin_platform_driver(hisi_pcie_driver);
325
326#endif