]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/pci/dwc/pci-keystone.c
5bee3af47588876fc33f6eac259271a4c97e23b7
[mirror_ubuntu-bionic-kernel.git] / drivers / pci / dwc / pci-keystone.c
1 /*
2 * PCIe host controller driver for Texas Instruments Keystone SoCs
3 *
4 * Copyright (C) 2013-2014 Texas Instruments., Ltd.
5 * http://www.ti.com
6 *
7 * Author: Murali Karicheri <m-karicheri2@ti.com>
8 * Implementation based on pci-exynos.c and pcie-designware.c
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
15 #include <linux/irqchip/chained_irq.h>
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/irqdomain.h>
20 #include <linux/init.h>
21 #include <linux/msi.h>
22 #include <linux/of_irq.h>
23 #include <linux/of.h>
24 #include <linux/of_pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/phy/phy.h>
27 #include <linux/resource.h>
28 #include <linux/signal.h>
29
30 #include "pcie-designware.h"
31 #include "pci-keystone.h"
32
33 #define DRIVER_NAME "keystone-pcie"
34
35 /* DEV_STAT_CTRL */
36 #define PCIE_CAP_BASE 0x70
37
38 /* PCIE controller device IDs */
39 #define PCIE_RC_K2HK 0xb008
40 #define PCIE_RC_K2E 0xb009
41 #define PCIE_RC_K2L 0xb00a
42
43 #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
44
45 static void quirk_limit_mrrs(struct pci_dev *dev)
46 {
47 struct pci_bus *bus = dev->bus;
48 struct pci_dev *bridge = bus->self;
49 static const struct pci_device_id rc_pci_devids[] = {
50 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
51 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
52 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
53 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
54 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
55 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
56 { 0, },
57 };
58
59 if (pci_is_root_bus(bus))
60 return;
61
62 /* look for the host bridge */
63 while (!pci_is_root_bus(bus)) {
64 bridge = bus->self;
65 bus = bus->parent;
66 }
67
68 if (bridge) {
69 /*
70 * Keystone PCI controller has a h/w limitation of
71 * 256 bytes maximum read request size. It can't handle
72 * anything higher than this. So force this limit on
73 * all downstream devices.
74 */
75 if (pci_match_id(rc_pci_devids, bridge)) {
76 if (pcie_get_readrq(dev) > 256) {
77 dev_info(&dev->dev, "limiting MRRS to 256\n");
78 pcie_set_readrq(dev, 256);
79 }
80 }
81 }
82 }
83 DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, quirk_limit_mrrs);
84
85 static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
86 {
87 struct dw_pcie *pci = ks_pcie->pci;
88 struct pcie_port *pp = &pci->pp;
89 struct device *dev = pci->dev;
90 unsigned int retries;
91
92 dw_pcie_setup_rc(pp);
93
94 if (dw_pcie_link_up(pci)) {
95 dev_err(dev, "Link already up\n");
96 return 0;
97 }
98
99 /* check if the link is up or not */
100 for (retries = 0; retries < 5; retries++) {
101 ks_dw_pcie_initiate_link_train(ks_pcie);
102 if (!dw_pcie_wait_for_link(pci))
103 return 0;
104 }
105
106 dev_err(dev, "phy link never came up\n");
107 return -ETIMEDOUT;
108 }
109
110 static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
111 {
112 unsigned int irq = irq_desc_get_irq(desc);
113 struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
114 u32 offset = irq - ks_pcie->msi_host_irqs[0];
115 struct dw_pcie *pci = ks_pcie->pci;
116 struct device *dev = pci->dev;
117 struct irq_chip *chip = irq_desc_get_chip(desc);
118
119 dev_dbg(dev, "%s, irq %d\n", __func__, irq);
120
121 /*
122 * The chained irq handler installation would have replaced normal
123 * interrupt driver handler so we need to take care of mask/unmask and
124 * ack operation.
125 */
126 chained_irq_enter(chip, desc);
127 ks_dw_pcie_handle_msi_irq(ks_pcie, offset);
128 chained_irq_exit(chip, desc);
129 }
130
131 /**
132 * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
133 * @irq: IRQ line for legacy interrupts
134 * @desc: Pointer to irq descriptor
135 *
136 * Traverse through pending legacy interrupts and invoke handler for each. Also
137 * takes care of interrupt controller level mask/ack operation.
138 */
139 static void ks_pcie_legacy_irq_handler(struct irq_desc *desc)
140 {
141 unsigned int irq = irq_desc_get_irq(desc);
142 struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
143 struct dw_pcie *pci = ks_pcie->pci;
144 struct device *dev = pci->dev;
145 u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
146 struct irq_chip *chip = irq_desc_get_chip(desc);
147
148 dev_dbg(dev, ": Handling legacy irq %d\n", irq);
149
150 /*
151 * The chained irq handler installation would have replaced normal
152 * interrupt driver handler so we need to take care of mask/unmask and
153 * ack operation.
154 */
155 chained_irq_enter(chip, desc);
156 ks_dw_pcie_handle_legacy_irq(ks_pcie, irq_offset);
157 chained_irq_exit(chip, desc);
158 }
159
160 static int ks_pcie_get_irq_controller_info(struct keystone_pcie *ks_pcie,
161 char *controller, int *num_irqs)
162 {
163 int temp, max_host_irqs, legacy = 1, *host_irqs;
164 struct device *dev = ks_pcie->pci->dev;
165 struct device_node *np_pcie = dev->of_node, **np_temp;
166
167 if (!strcmp(controller, "msi-interrupt-controller"))
168 legacy = 0;
169
170 if (legacy) {
171 np_temp = &ks_pcie->legacy_intc_np;
172 max_host_irqs = PCI_NUM_INTX;
173 host_irqs = &ks_pcie->legacy_host_irqs[0];
174 } else {
175 np_temp = &ks_pcie->msi_intc_np;
176 max_host_irqs = MAX_MSI_HOST_IRQS;
177 host_irqs = &ks_pcie->msi_host_irqs[0];
178 }
179
180 /* interrupt controller is in a child node */
181 *np_temp = of_find_node_by_name(np_pcie, controller);
182 if (!(*np_temp)) {
183 dev_err(dev, "Node for %s is absent\n", controller);
184 return -EINVAL;
185 }
186
187 temp = of_irq_count(*np_temp);
188 if (!temp) {
189 dev_err(dev, "No IRQ entries in %s\n", controller);
190 return -EINVAL;
191 }
192
193 if (temp > max_host_irqs)
194 dev_warn(dev, "Too many %s interrupts defined %u\n",
195 (legacy ? "legacy" : "MSI"), temp);
196
197 /*
198 * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
199 * 7 (MSI)
200 */
201 for (temp = 0; temp < max_host_irqs; temp++) {
202 host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
203 if (!host_irqs[temp])
204 break;
205 }
206
207 if (temp) {
208 *num_irqs = temp;
209 return 0;
210 }
211
212 return -EINVAL;
213 }
214
215 static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
216 {
217 int i;
218
219 /* Legacy IRQ */
220 for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
221 irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
222 ks_pcie_legacy_irq_handler,
223 ks_pcie);
224 }
225 ks_dw_pcie_enable_legacy_irqs(ks_pcie);
226
227 /* MSI IRQ */
228 if (IS_ENABLED(CONFIG_PCI_MSI)) {
229 for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
230 irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
231 ks_pcie_msi_irq_handler,
232 ks_pcie);
233 }
234 }
235
236 if (ks_pcie->error_irq > 0)
237 ks_dw_pcie_enable_error_irq(ks_pcie);
238 }
239
240 /*
241 * When a PCI device does not exist during config cycles, keystone host gets a
242 * bus error instead of returning 0xffffffff. This handler always returns 0
243 * for this kind of faults.
244 */
245 static int keystone_pcie_fault(unsigned long addr, unsigned int fsr,
246 struct pt_regs *regs)
247 {
248 unsigned long instr = *(unsigned long *) instruction_pointer(regs);
249
250 if ((instr & 0x0e100090) == 0x00100090) {
251 int reg = (instr >> 12) & 15;
252
253 regs->uregs[reg] = -1;
254 regs->ARM_pc += 4;
255 }
256
257 return 0;
258 }
259
260 static int __init ks_pcie_host_init(struct pcie_port *pp)
261 {
262 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
263 struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
264 u32 val;
265
266 ks_pcie_establish_link(ks_pcie);
267 ks_dw_pcie_setup_rc_app_regs(ks_pcie);
268 ks_pcie_setup_interrupts(ks_pcie);
269 writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
270 pci->dbi_base + PCI_IO_BASE);
271
272 /* update the Vendor ID */
273 writew(ks_pcie->device_id, pci->dbi_base + PCI_DEVICE_ID);
274
275 /* update the DEV_STAT_CTRL to publish right mrrs */
276 val = readl(pci->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
277 val &= ~PCI_EXP_DEVCTL_READRQ;
278 /* set the mrrs to 256 bytes */
279 val |= BIT(12);
280 writel(val, pci->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
281
282 /*
283 * PCIe access errors that result into OCP errors are caught by ARM as
284 * "External aborts"
285 */
286 hook_fault_code(17, keystone_pcie_fault, SIGBUS, 0,
287 "Asynchronous external abort");
288
289 return 0;
290 }
291
292 static const struct dw_pcie_host_ops keystone_pcie_host_ops = {
293 .rd_other_conf = ks_dw_pcie_rd_other_conf,
294 .wr_other_conf = ks_dw_pcie_wr_other_conf,
295 .host_init = ks_pcie_host_init,
296 .msi_set_irq = ks_dw_pcie_msi_set_irq,
297 .msi_clear_irq = ks_dw_pcie_msi_clear_irq,
298 .get_msi_addr = ks_dw_pcie_get_msi_addr,
299 .msi_host_init = ks_dw_pcie_msi_host_init,
300 .scan_bus = ks_dw_pcie_v3_65_scan_bus,
301 };
302
303 static irqreturn_t pcie_err_irq_handler(int irq, void *priv)
304 {
305 struct keystone_pcie *ks_pcie = priv;
306
307 return ks_dw_pcie_handle_error_irq(ks_pcie);
308 }
309
310 static int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
311 struct platform_device *pdev)
312 {
313 struct dw_pcie *pci = ks_pcie->pci;
314 struct pcie_port *pp = &pci->pp;
315 struct device *dev = &pdev->dev;
316 int ret;
317
318 ret = ks_pcie_get_irq_controller_info(ks_pcie,
319 "legacy-interrupt-controller",
320 &ks_pcie->num_legacy_host_irqs);
321 if (ret)
322 return ret;
323
324 if (IS_ENABLED(CONFIG_PCI_MSI)) {
325 ret = ks_pcie_get_irq_controller_info(ks_pcie,
326 "msi-interrupt-controller",
327 &ks_pcie->num_msi_host_irqs);
328 if (ret)
329 return ret;
330 }
331
332 /*
333 * Index 0 is the platform interrupt for error interrupt
334 * from RC. This is optional.
335 */
336 ks_pcie->error_irq = irq_of_parse_and_map(ks_pcie->np, 0);
337 if (ks_pcie->error_irq <= 0)
338 dev_info(dev, "no error IRQ defined\n");
339 else {
340 ret = request_irq(ks_pcie->error_irq, pcie_err_irq_handler,
341 IRQF_SHARED, "pcie-error-irq", ks_pcie);
342 if (ret < 0) {
343 dev_err(dev, "failed to request error IRQ %d\n",
344 ks_pcie->error_irq);
345 return ret;
346 }
347 }
348
349 pp->root_bus_nr = -1;
350 pp->ops = &keystone_pcie_host_ops;
351 ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
352 if (ret) {
353 dev_err(dev, "failed to initialize host\n");
354 return ret;
355 }
356
357 return 0;
358 }
359
360 static const struct of_device_id ks_pcie_of_match[] = {
361 {
362 .type = "pci",
363 .compatible = "ti,keystone-pcie",
364 },
365 { },
366 };
367
368 static const struct dw_pcie_ops dw_pcie_ops = {
369 .link_up = ks_dw_pcie_link_up,
370 };
371
372 static int __exit ks_pcie_remove(struct platform_device *pdev)
373 {
374 struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
375
376 clk_disable_unprepare(ks_pcie->clk);
377
378 return 0;
379 }
380
381 static int __init ks_pcie_probe(struct platform_device *pdev)
382 {
383 struct device *dev = &pdev->dev;
384 struct dw_pcie *pci;
385 struct keystone_pcie *ks_pcie;
386 struct resource *res;
387 void __iomem *reg_p;
388 struct phy *phy;
389 int ret;
390
391 ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
392 if (!ks_pcie)
393 return -ENOMEM;
394
395 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
396 if (!pci)
397 return -ENOMEM;
398
399 pci->dev = dev;
400 pci->ops = &dw_pcie_ops;
401
402 ks_pcie->pci = pci;
403
404 /* initialize SerDes Phy if present */
405 phy = devm_phy_get(dev, "pcie-phy");
406 if (PTR_ERR_OR_ZERO(phy) == -EPROBE_DEFER)
407 return PTR_ERR(phy);
408
409 if (!IS_ERR_OR_NULL(phy)) {
410 ret = phy_init(phy);
411 if (ret < 0)
412 return ret;
413 }
414
415 /* index 2 is to read PCI DEVICE_ID */
416 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
417 reg_p = devm_ioremap_resource(dev, res);
418 if (IS_ERR(reg_p))
419 return PTR_ERR(reg_p);
420 ks_pcie->device_id = readl(reg_p) >> 16;
421 devm_iounmap(dev, reg_p);
422 devm_release_mem_region(dev, res->start, resource_size(res));
423
424 ks_pcie->np = dev->of_node;
425 platform_set_drvdata(pdev, ks_pcie);
426 ks_pcie->clk = devm_clk_get(dev, "pcie");
427 if (IS_ERR(ks_pcie->clk)) {
428 dev_err(dev, "Failed to get pcie rc clock\n");
429 return PTR_ERR(ks_pcie->clk);
430 }
431 ret = clk_prepare_enable(ks_pcie->clk);
432 if (ret)
433 return ret;
434
435 platform_set_drvdata(pdev, ks_pcie);
436
437 ret = ks_add_pcie_port(ks_pcie, pdev);
438 if (ret < 0)
439 goto fail_clk;
440
441 return 0;
442 fail_clk:
443 clk_disable_unprepare(ks_pcie->clk);
444
445 return ret;
446 }
447
448 static struct platform_driver ks_pcie_driver __refdata = {
449 .probe = ks_pcie_probe,
450 .remove = __exit_p(ks_pcie_remove),
451 .driver = {
452 .name = "keystone-pcie",
453 .of_match_table = of_match_ptr(ks_pcie_of_match),
454 },
455 };
456 builtin_platform_driver(ks_pcie_driver);