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