blob: 3772aff7df4f060cbc511c615d574a52504dd4ff [file] [log] [blame]
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -06001/*
2 * pcie-dra7xx - PCIe controller driver for TI DRA7xx SoCs
3 *
4 * Copyright (C) 2013-2014 Texas Instruments Incorporated - http://www.ti.com
5 *
6 * Authors: Kishon Vijay Abraham I <kishon@ti.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/delay.h>
14#include <linux/err.h>
15#include <linux/interrupt.h>
16#include <linux/irq.h>
17#include <linux/irqdomain.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21#include <linux/phy/phy.h>
22#include <linux/platform_device.h>
23#include <linux/pm_runtime.h>
24#include <linux/resource.h>
25#include <linux/types.h>
26
27#include "pcie-designware.h"
28
29/* PCIe controller wrapper DRA7XX configuration registers */
30
31#define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN 0x0024
32#define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN 0x0028
33#define ERR_SYS BIT(0)
34#define ERR_FATAL BIT(1)
35#define ERR_NONFATAL BIT(2)
36#define ERR_COR BIT(3)
37#define ERR_AXI BIT(4)
38#define ERR_ECRC BIT(5)
39#define PME_TURN_OFF BIT(8)
40#define PME_TO_ACK BIT(9)
41#define PM_PME BIT(10)
42#define LINK_REQ_RST BIT(11)
43#define LINK_UP_EVT BIT(12)
44#define CFG_BME_EVT BIT(13)
45#define CFG_MSE_EVT BIT(14)
46#define INTERRUPTS (ERR_SYS | ERR_FATAL | ERR_NONFATAL | ERR_COR | ERR_AXI | \
47 ERR_ECRC | PME_TURN_OFF | PME_TO_ACK | PM_PME | \
48 LINK_REQ_RST | LINK_UP_EVT | CFG_BME_EVT | CFG_MSE_EVT)
49
50#define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI 0x0034
51#define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI 0x0038
52#define INTA BIT(0)
53#define INTB BIT(1)
54#define INTC BIT(2)
55#define INTD BIT(3)
56#define MSI BIT(4)
57#define LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD)
58
59#define PCIECTRL_DRA7XX_CONF_DEVICE_CMD 0x0104
60#define LTSSM_EN 0x1
61
62#define PCIECTRL_DRA7XX_CONF_PHY_CS 0x010C
63#define LINK_UP BIT(16)
64
65struct dra7xx_pcie {
66 void __iomem *base;
67 struct phy **phy;
68 int phy_count;
69 struct device *dev;
70 struct pcie_port pp;
71};
72
73#define to_dra7xx_pcie(x) container_of((x), struct dra7xx_pcie, pp)
74
75static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
76{
77 return readl(pcie->base + offset);
78}
79
80static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
81 u32 value)
82{
83 writel(value, pcie->base + offset);
84}
85
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +053086static inline u32 dra7xx_pcie_readl_rc(struct pcie_port *pp, u32 offset)
87{
88 return readl(pp->dbi_base + offset);
89}
90
91static inline void dra7xx_pcie_writel_rc(struct pcie_port *pp, u32 offset,
92 u32 value)
93{
94 writel(value, pp->dbi_base + offset);
95}
96
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -060097static int dra7xx_pcie_link_up(struct pcie_port *pp)
98{
99 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
100 u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
101
102 return !!(reg & LINK_UP);
103}
104
105static int dra7xx_pcie_establish_link(struct pcie_port *pp)
106{
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600107 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500108 u32 reg;
109 unsigned int retries;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600110
111 if (dw_pcie_link_up(pp)) {
112 dev_err(pp->dev, "link is already up\n");
113 return 0;
114 }
115
116 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
117 reg |= LTSSM_EN;
118 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
119
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500120 for (retries = 0; retries < 1000; retries++) {
Bjorn Helgaas30fb7ba2015-06-02 16:21:11 -0500121 if (dw_pcie_link_up(pp))
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500122 return 0;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600123 usleep_range(10, 20);
124 }
125
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500126 dev_err(pp->dev, "link is not up\n");
127 return -EINVAL;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600128}
129
130static void dra7xx_pcie_enable_interrupts(struct pcie_port *pp)
131{
132 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
133
134 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
135 ~INTERRUPTS);
136 dra7xx_pcie_writel(dra7xx,
137 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN, INTERRUPTS);
138 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
139 ~LEG_EP_INTERRUPTS & ~MSI);
140
141 if (IS_ENABLED(CONFIG_PCI_MSI))
142 dra7xx_pcie_writel(dra7xx,
143 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI, MSI);
144 else
145 dra7xx_pcie_writel(dra7xx,
146 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
147 LEG_EP_INTERRUPTS);
148}
149
150static void dra7xx_pcie_host_init(struct pcie_port *pp)
151{
152 dw_pcie_setup_rc(pp);
153 dra7xx_pcie_establish_link(pp);
154 if (IS_ENABLED(CONFIG_PCI_MSI))
155 dw_pcie_msi_init(pp);
156 dra7xx_pcie_enable_interrupts(pp);
157}
158
159static struct pcie_host_ops dra7xx_pcie_host_ops = {
160 .link_up = dra7xx_pcie_link_up,
161 .host_init = dra7xx_pcie_host_init,
162};
163
164static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
165 irq_hw_number_t hwirq)
166{
167 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
168 irq_set_chip_data(irq, domain->host_data);
169 set_irq_flags(irq, IRQF_VALID);
170
171 return 0;
172}
173
174static const struct irq_domain_ops intx_domain_ops = {
175 .map = dra7xx_pcie_intx_map,
176};
177
178static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
179{
180 struct device *dev = pp->dev;
181 struct device_node *node = dev->of_node;
182 struct device_node *pcie_intc_node = of_get_next_child(node, NULL);
183
184 if (!pcie_intc_node) {
185 dev_err(dev, "No PCIe Intc node found\n");
186 return PTR_ERR(pcie_intc_node);
187 }
188
189 pp->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
190 &intx_domain_ops, pp);
191 if (!pp->irq_domain) {
192 dev_err(dev, "Failed to get a INTx IRQ domain\n");
193 return PTR_ERR(pp->irq_domain);
194 }
195
196 return 0;
197}
198
199static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
200{
201 struct pcie_port *pp = arg;
202 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
203 u32 reg;
204
205 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
206
207 switch (reg) {
208 case MSI:
209 dw_handle_msi_irq(pp);
210 break;
211 case INTA:
212 case INTB:
213 case INTC:
214 case INTD:
215 generic_handle_irq(irq_find_mapping(pp->irq_domain, ffs(reg)));
216 break;
217 }
218
219 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
220
221 return IRQ_HANDLED;
222}
223
224
225static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
226{
227 struct dra7xx_pcie *dra7xx = arg;
228 u32 reg;
229
230 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
231
232 if (reg & ERR_SYS)
233 dev_dbg(dra7xx->dev, "System Error\n");
234
235 if (reg & ERR_FATAL)
236 dev_dbg(dra7xx->dev, "Fatal Error\n");
237
238 if (reg & ERR_NONFATAL)
239 dev_dbg(dra7xx->dev, "Non Fatal Error\n");
240
241 if (reg & ERR_COR)
242 dev_dbg(dra7xx->dev, "Correctable Error\n");
243
244 if (reg & ERR_AXI)
245 dev_dbg(dra7xx->dev, "AXI tag lookup fatal Error\n");
246
247 if (reg & ERR_ECRC)
248 dev_dbg(dra7xx->dev, "ECRC Error\n");
249
250 if (reg & PME_TURN_OFF)
251 dev_dbg(dra7xx->dev,
252 "Power Management Event Turn-Off message received\n");
253
254 if (reg & PME_TO_ACK)
255 dev_dbg(dra7xx->dev,
256 "Power Management Turn-Off Ack message received\n");
257
258 if (reg & PM_PME)
259 dev_dbg(dra7xx->dev,
260 "PM Power Management Event message received\n");
261
262 if (reg & LINK_REQ_RST)
263 dev_dbg(dra7xx->dev, "Link Request Reset\n");
264
265 if (reg & LINK_UP_EVT)
266 dev_dbg(dra7xx->dev, "Link-up state change\n");
267
268 if (reg & CFG_BME_EVT)
269 dev_dbg(dra7xx->dev, "CFG 'Bus Master Enable' change\n");
270
271 if (reg & CFG_MSE_EVT)
272 dev_dbg(dra7xx->dev, "CFG 'Memory Space Enable' change\n");
273
274 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
275
276 return IRQ_HANDLED;
277}
278
Jingoo Hane73044a2014-11-06 14:37:39 +0900279static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
280 struct platform_device *pdev)
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600281{
282 int ret;
283 struct pcie_port *pp;
284 struct resource *res;
285 struct device *dev = &pdev->dev;
286
287 pp = &dra7xx->pp;
288 pp->dev = dev;
289 pp->ops = &dra7xx_pcie_host_ops;
290
291 pp->irq = platform_get_irq(pdev, 1);
292 if (pp->irq < 0) {
293 dev_err(dev, "missing IRQ resource\n");
294 return -EINVAL;
295 }
296
297 ret = devm_request_irq(&pdev->dev, pp->irq,
298 dra7xx_pcie_msi_irq_handler, IRQF_SHARED,
299 "dra7-pcie-msi", pp);
300 if (ret) {
301 dev_err(&pdev->dev, "failed to request irq\n");
302 return ret;
303 }
304
305 if (!IS_ENABLED(CONFIG_PCI_MSI)) {
306 ret = dra7xx_pcie_init_irq_domain(pp);
307 if (ret < 0)
308 return ret;
309 }
310
311 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
312 pp->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
313 if (!pp->dbi_base)
314 return -ENOMEM;
315
316 ret = dw_pcie_host_init(pp);
317 if (ret) {
318 dev_err(dra7xx->dev, "failed to initialize host\n");
319 return ret;
320 }
321
322 return 0;
323}
324
325static int __init dra7xx_pcie_probe(struct platform_device *pdev)
326{
327 u32 reg;
328 int ret;
329 int irq;
330 int i;
331 int phy_count;
332 struct phy **phy;
333 void __iomem *base;
334 struct resource *res;
335 struct dra7xx_pcie *dra7xx;
336 struct device *dev = &pdev->dev;
337 struct device_node *np = dev->of_node;
338 char name[10];
339
340 dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
341 if (!dra7xx)
342 return -ENOMEM;
343
344 irq = platform_get_irq(pdev, 0);
345 if (irq < 0) {
346 dev_err(dev, "missing IRQ resource\n");
347 return -EINVAL;
348 }
349
350 ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
351 IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
352 if (ret) {
353 dev_err(dev, "failed to request irq\n");
354 return ret;
355 }
356
357 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
358 base = devm_ioremap_nocache(dev, res->start, resource_size(res));
359 if (!base)
360 return -ENOMEM;
361
362 phy_count = of_property_count_strings(np, "phy-names");
363 if (phy_count < 0) {
364 dev_err(dev, "unable to find the strings\n");
365 return phy_count;
366 }
367
368 phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL);
369 if (!phy)
370 return -ENOMEM;
371
372 for (i = 0; i < phy_count; i++) {
373 snprintf(name, sizeof(name), "pcie-phy%d", i);
374 phy[i] = devm_phy_get(dev, name);
375 if (IS_ERR(phy[i]))
376 return PTR_ERR(phy[i]);
377
378 ret = phy_init(phy[i]);
379 if (ret < 0)
380 goto err_phy;
381
382 ret = phy_power_on(phy[i]);
383 if (ret < 0) {
384 phy_exit(phy[i]);
385 goto err_phy;
386 }
387 }
388
389 dra7xx->base = base;
390 dra7xx->phy = phy;
391 dra7xx->dev = dev;
392 dra7xx->phy_count = phy_count;
393
394 pm_runtime_enable(dev);
395 ret = pm_runtime_get_sync(dev);
396 if (IS_ERR_VALUE(ret)) {
397 dev_err(dev, "pm_runtime_get_sync failed\n");
Kishon Vijay Abraham I0e2bdb02015-07-31 17:55:10 +0530398 goto err_get_sync;
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600399 }
400
401 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
402 reg &= ~LTSSM_EN;
403 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
404
405 platform_set_drvdata(pdev, dra7xx);
406
Jingoo Han23926c82014-11-06 14:30:49 +0900407 ret = dra7xx_add_pcie_port(dra7xx, pdev);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600408 if (ret < 0)
409 goto err_add_port;
410
411 return 0;
412
413err_add_port:
414 pm_runtime_put(dev);
Kishon Vijay Abraham I0e2bdb02015-07-31 17:55:10 +0530415
416err_get_sync:
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600417 pm_runtime_disable(dev);
418
419err_phy:
420 while (--i >= 0) {
421 phy_power_off(phy[i]);
422 phy_exit(phy[i]);
423 }
424
425 return ret;
426}
427
428static int __exit dra7xx_pcie_remove(struct platform_device *pdev)
429{
430 struct dra7xx_pcie *dra7xx = platform_get_drvdata(pdev);
431 struct pcie_port *pp = &dra7xx->pp;
432 struct device *dev = &pdev->dev;
433 int count = dra7xx->phy_count;
434
435 if (pp->irq_domain)
436 irq_domain_remove(pp->irq_domain);
437 pm_runtime_put(dev);
438 pm_runtime_disable(dev);
439 while (count--) {
440 phy_power_off(dra7xx->phy[count]);
441 phy_exit(dra7xx->phy[count]);
442 }
443
444 return 0;
445}
446
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530447#ifdef CONFIG_PM_SLEEP
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530448static int dra7xx_pcie_suspend(struct device *dev)
449{
450 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
451 struct pcie_port *pp = &dra7xx->pp;
452 u32 val;
453
454 /* clear MSE */
455 val = dra7xx_pcie_readl_rc(pp, PCI_COMMAND);
456 val &= ~PCI_COMMAND_MEMORY;
457 dra7xx_pcie_writel_rc(pp, PCI_COMMAND, val);
458
459 return 0;
460}
461
462static int dra7xx_pcie_resume(struct device *dev)
463{
464 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
465 struct pcie_port *pp = &dra7xx->pp;
466 u32 val;
467
468 /* set MSE */
469 val = dra7xx_pcie_readl_rc(pp, PCI_COMMAND);
470 val |= PCI_COMMAND_MEMORY;
471 dra7xx_pcie_writel_rc(pp, PCI_COMMAND, val);
472
473 return 0;
474}
475
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530476static int dra7xx_pcie_suspend_noirq(struct device *dev)
477{
478 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
479 int count = dra7xx->phy_count;
480
481 while (count--) {
482 phy_power_off(dra7xx->phy[count]);
483 phy_exit(dra7xx->phy[count]);
484 }
485
486 return 0;
487}
488
489static int dra7xx_pcie_resume_noirq(struct device *dev)
490{
491 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
492 int phy_count = dra7xx->phy_count;
493 int ret;
494 int i;
495
496 for (i = 0; i < phy_count; i++) {
497 ret = phy_init(dra7xx->phy[i]);
498 if (ret < 0)
499 goto err_phy;
500
501 ret = phy_power_on(dra7xx->phy[i]);
502 if (ret < 0) {
503 phy_exit(dra7xx->phy[i]);
504 goto err_phy;
505 }
506 }
507
508 return 0;
509
510err_phy:
511 while (--i >= 0) {
512 phy_power_off(dra7xx->phy[i]);
513 phy_exit(dra7xx->phy[i]);
514 }
515
516 return ret;
517}
518#endif
519
520static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
Kishon Vijay Abraham I389c7092015-07-31 17:55:12 +0530521 SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530522 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
523 dra7xx_pcie_resume_noirq)
524};
525
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600526static const struct of_device_id of_dra7xx_pcie_match[] = {
527 { .compatible = "ti,dra7-pcie", },
528 {},
529};
530MODULE_DEVICE_TABLE(of, of_dra7xx_pcie_match);
531
532static struct platform_driver dra7xx_pcie_driver = {
533 .remove = __exit_p(dra7xx_pcie_remove),
534 .driver = {
535 .name = "dra7-pcie",
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600536 .of_match_table = of_dra7xx_pcie_match,
Kishon Vijay Abraham Ie52eb442015-07-31 17:55:11 +0530537 .pm = &dra7xx_pcie_pm_ops,
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600538 },
539};
540
541module_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);
542
543MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
544MODULE_DESCRIPTION("TI PCIe controller driver");
545MODULE_LICENSE("GPL v2");