blob: 81253e70b1c5441b41544a58f682caa73f84dbc8 [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
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -060038/* DEV_STAT_CTRL */
39#define PCIE_CAP_BASE 0x70
40
Murali Karicheric15982d2014-09-08 13:03:34 -040041/* PCIE controller device IDs */
42#define PCIE_RC_K2HK 0xb008
43#define PCIE_RC_K2E 0xb009
44#define PCIE_RC_K2L 0xb00a
45
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -060046#define to_keystone_pcie(x) container_of(x, struct keystone_pcie, pp)
47
Murali Karicheric15982d2014-09-08 13:03:34 -040048static void quirk_limit_mrrs(struct pci_dev *dev)
49{
50 struct pci_bus *bus = dev->bus;
51 struct pci_dev *bridge = bus->self;
52 static const struct pci_device_id rc_pci_devids[] = {
53 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
54 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
55 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
56 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
57 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
58 .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
59 { 0, },
60 };
61
62 if (pci_is_root_bus(bus))
63 return;
64
65 /* look for the host bridge */
66 while (!pci_is_root_bus(bus)) {
67 bridge = bus->self;
68 bus = bus->parent;
69 }
70
71 if (bridge) {
72 /*
73 * Keystone PCI controller has a h/w limitation of
74 * 256 bytes maximum read request size. It can't handle
75 * anything higher than this. So force this limit on
76 * all downstream devices.
77 */
78 if (pci_match_id(rc_pci_devids, bridge)) {
79 if (pcie_get_readrq(dev) > 256) {
80 dev_info(&dev->dev, "limiting MRRS to 256\n");
81 pcie_set_readrq(dev, 256);
82 }
83 }
84 }
85}
86DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, quirk_limit_mrrs);
87
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -060088static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
89{
90 struct pcie_port *pp = &ks_pcie->pp;
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -050091 unsigned int retries;
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -060092
93 dw_pcie_setup_rc(pp);
94
95 if (dw_pcie_link_up(pp)) {
96 dev_err(pp->dev, "Link already up\n");
97 return 0;
98 }
99
100 ks_dw_pcie_initiate_link_train(ks_pcie);
101 /* check if the link is up or not */
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500102 for (retries = 0; retries < 200; retries++) {
103 if (dw_pcie_link_up(pp))
104 return 0;
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600105 usleep_range(100, 1000);
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500106 ks_dw_pcie_initiate_link_train(ks_pcie);
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600107 }
108
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500109 dev_err(pp->dev, "phy link never came up\n");
110 return -EINVAL;
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600111}
112
Thomas Gleixner97a85962015-07-16 23:24:10 +0200113static void ks_pcie_msi_irq_handler(unsigned int __irq, struct irq_desc *desc)
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600114{
Thomas Gleixner97a85962015-07-16 23:24:10 +0200115 unsigned int irq = irq_desc_get_irq(desc);
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600116 struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
117 u32 offset = irq - ks_pcie->msi_host_irqs[0];
118 struct pcie_port *pp = &ks_pcie->pp;
119 struct irq_chip *chip = irq_desc_get_chip(desc);
120
Julia Lawall4808c352015-01-09 11:30:32 -0700121 dev_dbg(pp->dev, "%s, irq %d\n", __func__, irq);
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600122
123 /*
124 * The chained irq handler installation would have replaced normal
125 * interrupt driver handler so we need to take care of mask/unmask and
126 * ack operation.
127 */
128 chained_irq_enter(chip, desc);
129 ks_dw_pcie_handle_msi_irq(ks_pcie, offset);
130 chained_irq_exit(chip, desc);
131}
132
133/**
134 * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
135 * @irq: IRQ line for legacy interrupts
136 * @desc: Pointer to irq descriptor
137 *
138 * Traverse through pending legacy interrupts and invoke handler for each. Also
139 * takes care of interrupt controller level mask/ack operation.
140 */
Thomas Gleixner97a85962015-07-16 23:24:10 +0200141static void ks_pcie_legacy_irq_handler(unsigned int __irq,
142 struct irq_desc *desc)
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600143{
Thomas Gleixner97a85962015-07-16 23:24:10 +0200144 unsigned int irq = irq_desc_get_irq(desc);
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600145 struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
146 struct pcie_port *pp = &ks_pcie->pp;
147 u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
148 struct irq_chip *chip = irq_desc_get_chip(desc);
149
150 dev_dbg(pp->dev, ": Handling legacy irq %d\n", irq);
151
152 /*
153 * The chained irq handler installation would have replaced normal
154 * interrupt driver handler so we need to take care of mask/unmask and
155 * ack operation.
156 */
157 chained_irq_enter(chip, desc);
158 ks_dw_pcie_handle_legacy_irq(ks_pcie, irq_offset);
159 chained_irq_exit(chip, desc);
160}
161
162static int ks_pcie_get_irq_controller_info(struct keystone_pcie *ks_pcie,
163 char *controller, int *num_irqs)
164{
165 int temp, max_host_irqs, legacy = 1, *host_irqs, ret = -EINVAL;
166 struct device *dev = ks_pcie->pp.dev;
167 struct device_node *np_pcie = dev->of_node, **np_temp;
168
169 if (!strcmp(controller, "msi-interrupt-controller"))
170 legacy = 0;
171
172 if (legacy) {
173 np_temp = &ks_pcie->legacy_intc_np;
174 max_host_irqs = MAX_LEGACY_HOST_IRQS;
175 host_irqs = &ks_pcie->legacy_host_irqs[0];
176 } else {
177 np_temp = &ks_pcie->msi_intc_np;
178 max_host_irqs = MAX_MSI_HOST_IRQS;
179 host_irqs = &ks_pcie->msi_host_irqs[0];
180 }
181
182 /* interrupt controller is in a child node */
183 *np_temp = of_find_node_by_name(np_pcie, controller);
184 if (!(*np_temp)) {
185 dev_err(dev, "Node for %s is absent\n", controller);
186 goto out;
187 }
188 temp = of_irq_count(*np_temp);
189 if (!temp)
190 goto out;
191 if (temp > max_host_irqs)
192 dev_warn(dev, "Too many %s interrupts defined %u\n",
193 (legacy ? "legacy" : "MSI"), temp);
194
195 /*
196 * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
197 * 7 (MSI)
198 */
199 for (temp = 0; temp < max_host_irqs; temp++) {
200 host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
Dmitry Torokhovea3651f2014-11-14 14:19:03 -0800201 if (!host_irqs[temp])
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600202 break;
203 }
204 if (temp) {
205 *num_irqs = temp;
206 ret = 0;
207 }
208out:
209 return ret;
210}
211
212static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
213{
214 int i;
215
216 /* Legacy IRQ */
217 for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
Thomas Gleixner5168a732015-06-21 21:11:05 +0200218 irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
219 ks_pcie_legacy_irq_handler,
220 ks_pcie);
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600221 }
222 ks_dw_pcie_enable_legacy_irqs(ks_pcie);
223
224 /* MSI IRQ */
225 if (IS_ENABLED(CONFIG_PCI_MSI)) {
226 for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
Thomas Gleixner2cf5a032015-06-21 20:16:09 +0200227 irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
228 ks_pcie_msi_irq_handler,
229 ks_pcie);
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600230 }
231 }
232}
233
234/*
235 * When a PCI device does not exist during config cycles, keystone host gets a
236 * bus error instead of returning 0xffffffff. This handler always returns 0
237 * for this kind of faults.
238 */
239static int keystone_pcie_fault(unsigned long addr, unsigned int fsr,
240 struct pt_regs *regs)
241{
242 unsigned long instr = *(unsigned long *) instruction_pointer(regs);
243
244 if ((instr & 0x0e100090) == 0x00100090) {
245 int reg = (instr >> 12) & 15;
246
247 regs->uregs[reg] = -1;
248 regs->ARM_pc += 4;
249 }
250
251 return 0;
252}
253
254static void __init ks_pcie_host_init(struct pcie_port *pp)
255{
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600256 struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
Murali Karicheri8665a482014-09-10 13:12:39 -0400257 u32 val;
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600258
259 ks_pcie_establish_link(ks_pcie);
260 ks_dw_pcie_setup_rc_app_regs(ks_pcie);
261 ks_pcie_setup_interrupts(ks_pcie);
262 writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
263 pp->dbi_base + PCI_IO_BASE);
264
265 /* update the Vendor ID */
Murali Karicheri8665a482014-09-10 13:12:39 -0400266 writew(ks_pcie->device_id, pp->dbi_base + PCI_DEVICE_ID);
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600267
268 /* update the DEV_STAT_CTRL to publish right mrrs */
269 val = readl(pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
270 val &= ~PCI_EXP_DEVCTL_READRQ;
271 /* set the mrrs to 256 bytes */
272 val |= BIT(12);
273 writel(val, pp->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
274
275 /*
276 * PCIe access errors that result into OCP errors are caught by ARM as
277 * "External aborts"
278 */
279 hook_fault_code(17, keystone_pcie_fault, SIGBUS, 0,
280 "Asynchronous external abort");
281}
282
283static struct pcie_host_ops keystone_pcie_host_ops = {
284 .rd_other_conf = ks_dw_pcie_rd_other_conf,
285 .wr_other_conf = ks_dw_pcie_wr_other_conf,
286 .link_up = ks_dw_pcie_link_up,
287 .host_init = ks_pcie_host_init,
288 .msi_set_irq = ks_dw_pcie_msi_set_irq,
289 .msi_clear_irq = ks_dw_pcie_msi_clear_irq,
Bjorn Helgaas11045282014-09-29 13:24:24 -0600290 .get_msi_addr = ks_dw_pcie_get_msi_addr,
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600291 .msi_host_init = ks_dw_pcie_msi_host_init,
292 .scan_bus = ks_dw_pcie_v3_65_scan_bus,
293};
294
295static int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
296 struct platform_device *pdev)
297{
298 struct pcie_port *pp = &ks_pcie->pp;
299 int ret;
300
301 ret = ks_pcie_get_irq_controller_info(ks_pcie,
302 "legacy-interrupt-controller",
303 &ks_pcie->num_legacy_host_irqs);
304 if (ret)
305 return ret;
306
307 if (IS_ENABLED(CONFIG_PCI_MSI)) {
308 ret = ks_pcie_get_irq_controller_info(ks_pcie,
309 "msi-interrupt-controller",
310 &ks_pcie->num_msi_host_irqs);
311 if (ret)
312 return ret;
313 }
314
315 pp->root_bus_nr = -1;
316 pp->ops = &keystone_pcie_host_ops;
317 ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
318 if (ret) {
319 dev_err(&pdev->dev, "failed to initialize host\n");
320 return ret;
321 }
322
323 return ret;
324}
325
326static const struct of_device_id ks_pcie_of_match[] = {
327 {
328 .type = "pci",
329 .compatible = "ti,keystone-pcie",
330 },
331 { },
332};
333MODULE_DEVICE_TABLE(of, ks_pcie_of_match);
334
335static int __exit ks_pcie_remove(struct platform_device *pdev)
336{
337 struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
338
339 clk_disable_unprepare(ks_pcie->clk);
340
341 return 0;
342}
343
344static int __init ks_pcie_probe(struct platform_device *pdev)
345{
346 struct device *dev = &pdev->dev;
347 struct keystone_pcie *ks_pcie;
348 struct pcie_port *pp;
349 struct resource *res;
350 void __iomem *reg_p;
351 struct phy *phy;
352 int ret = 0;
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600353
354 ks_pcie = devm_kzalloc(&pdev->dev, sizeof(*ks_pcie),
355 GFP_KERNEL);
Jingoo Han66700702014-11-12 12:22:56 +0900356 if (!ks_pcie)
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600357 return -ENOMEM;
Jingoo Han66700702014-11-12 12:22:56 +0900358
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600359 pp = &ks_pcie->pp;
360
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600361 /* initialize SerDes Phy if present */
362 phy = devm_phy_get(dev, "pcie-phy");
363 if (!IS_ERR_OR_NULL(phy)) {
364 ret = phy_init(phy);
365 if (ret < 0)
366 return ret;
367 }
368
Murali Karicheri4455efc2014-09-10 13:12:38 -0400369 /* index 2 is to read PCI DEVICE_ID */
370 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600371 reg_p = devm_ioremap_resource(dev, res);
372 if (IS_ERR(reg_p))
373 return PTR_ERR(reg_p);
Murali Karicheri8665a482014-09-10 13:12:39 -0400374 ks_pcie->device_id = readl(reg_p) >> 16;
375 devm_iounmap(dev, reg_p);
376 devm_release_mem_region(dev, res->start, resource_size(res));
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600377
378 pp->dev = dev;
379 platform_set_drvdata(pdev, ks_pcie);
380 ks_pcie->clk = devm_clk_get(dev, "pcie");
381 if (IS_ERR(ks_pcie->clk)) {
382 dev_err(dev, "Failed to get pcie rc clock\n");
383 return PTR_ERR(ks_pcie->clk);
384 }
385 ret = clk_prepare_enable(ks_pcie->clk);
386 if (ret)
387 return ret;
388
389 ret = ks_add_pcie_port(ks_pcie, pdev);
390 if (ret < 0)
391 goto fail_clk;
392
393 return 0;
394fail_clk:
395 clk_disable_unprepare(ks_pcie->clk);
396
397 return ret;
398}
399
400static struct platform_driver ks_pcie_driver __refdata = {
401 .probe = ks_pcie_probe,
402 .remove = __exit_p(ks_pcie_remove),
403 .driver = {
404 .name = "keystone-pcie",
Murali Karicheri0c4ffcf2014-09-02 17:26:19 -0600405 .of_match_table = of_match_ptr(ks_pcie_of_match),
406 },
407};
408
409module_platform_driver(ks_pcie_driver);
410
411MODULE_AUTHOR("Murali Karicheri <m-karicheri2@ti.com>");
412MODULE_DESCRIPTION("Keystone PCIe host controller driver");
413MODULE_LICENSE("GPL v2");