2 * pcie-dra7xx - PCIe controller driver for TI DRA7xx SoCs
4 * Copyright (C) 2013-2014 Texas Instruments Incorporated - http://www.ti.com
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.
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/of_gpio.h>
21 #include <linux/pci.h>
22 #include <linux/phy/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/resource.h>
26 #include <linux/types.h>
28 #include "pcie-designware.h"
30 /* PCIe controller wrapper DRA7XX configuration registers */
32 #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN 0x0024
33 #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN 0x0028
34 #define ERR_SYS BIT(0)
35 #define ERR_FATAL BIT(1)
36 #define ERR_NONFATAL BIT(2)
37 #define ERR_COR BIT(3)
38 #define ERR_AXI BIT(4)
39 #define ERR_ECRC BIT(5)
40 #define PME_TURN_OFF BIT(8)
41 #define PME_TO_ACK BIT(9)
42 #define PM_PME BIT(10)
43 #define LINK_REQ_RST BIT(11)
44 #define LINK_UP_EVT BIT(12)
45 #define CFG_BME_EVT BIT(13)
46 #define CFG_MSE_EVT BIT(14)
47 #define INTERRUPTS (ERR_SYS | ERR_FATAL | ERR_NONFATAL | ERR_COR | ERR_AXI | \
48 ERR_ECRC | PME_TURN_OFF | PME_TO_ACK | PM_PME | \
49 LINK_REQ_RST | LINK_UP_EVT | CFG_BME_EVT | CFG_MSE_EVT)
51 #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI 0x0034
52 #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI 0x0038
58 #define LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD)
60 #define PCIECTRL_DRA7XX_CONF_DEVICE_CMD 0x0104
63 #define PCIECTRL_DRA7XX_CONF_PHY_CS 0x010C
64 #define LINK_UP BIT(16)
74 #define to_dra7xx_pcie(x) container_of((x), struct dra7xx_pcie, pp)
76 static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
78 return readl(pcie->base + offset);
81 static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
84 writel(value, pcie->base + offset);
87 static inline u32 dra7xx_pcie_readl_rc(struct pcie_port *pp, u32 offset)
89 return readl(pp->dbi_base + offset);
92 static inline void dra7xx_pcie_writel_rc(struct pcie_port *pp, u32 offset,
95 writel(value, pp->dbi_base + offset);
98 static int dra7xx_pcie_link_up(struct pcie_port *pp)
100 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
101 u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
103 return !!(reg & LINK_UP);
106 static int dra7xx_pcie_establish_link(struct pcie_port *pp)
108 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
110 unsigned int retries;
112 if (dw_pcie_link_up(pp)) {
113 dev_err(pp->dev, "link is already up\n");
117 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
119 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
121 for (retries = 0; retries < 1000; retries++) {
122 if (dw_pcie_link_up(pp))
124 usleep_range(10, 20);
127 dev_err(pp->dev, "link is not up\n");
131 static void dra7xx_pcie_enable_interrupts(struct pcie_port *pp)
133 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
135 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
137 dra7xx_pcie_writel(dra7xx,
138 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN, INTERRUPTS);
139 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
140 ~LEG_EP_INTERRUPTS & ~MSI);
142 if (IS_ENABLED(CONFIG_PCI_MSI))
143 dra7xx_pcie_writel(dra7xx,
144 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI, MSI);
146 dra7xx_pcie_writel(dra7xx,
147 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
151 static void dra7xx_pcie_host_init(struct pcie_port *pp)
153 dw_pcie_setup_rc(pp);
154 dra7xx_pcie_establish_link(pp);
155 if (IS_ENABLED(CONFIG_PCI_MSI))
156 dw_pcie_msi_init(pp);
157 dra7xx_pcie_enable_interrupts(pp);
160 static struct pcie_host_ops dra7xx_pcie_host_ops = {
161 .link_up = dra7xx_pcie_link_up,
162 .host_init = dra7xx_pcie_host_init,
165 static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
166 irq_hw_number_t hwirq)
168 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
169 irq_set_chip_data(irq, domain->host_data);
174 static const struct irq_domain_ops intx_domain_ops = {
175 .map = dra7xx_pcie_intx_map,
178 static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
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);
184 if (!pcie_intc_node) {
185 dev_err(dev, "No PCIe Intc node found\n");
186 return PTR_ERR(pcie_intc_node);
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);
199 static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
201 struct pcie_port *pp = arg;
202 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
205 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
209 dw_handle_msi_irq(pp);
215 generic_handle_irq(irq_find_mapping(pp->irq_domain, ffs(reg)));
219 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
225 static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
227 struct dra7xx_pcie *dra7xx = arg;
230 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
233 dev_dbg(dra7xx->dev, "System Error\n");
236 dev_dbg(dra7xx->dev, "Fatal Error\n");
238 if (reg & ERR_NONFATAL)
239 dev_dbg(dra7xx->dev, "Non Fatal Error\n");
242 dev_dbg(dra7xx->dev, "Correctable Error\n");
245 dev_dbg(dra7xx->dev, "AXI tag lookup fatal Error\n");
248 dev_dbg(dra7xx->dev, "ECRC Error\n");
250 if (reg & PME_TURN_OFF)
252 "Power Management Event Turn-Off message received\n");
254 if (reg & PME_TO_ACK)
256 "Power Management Turn-Off Ack message received\n");
260 "PM Power Management Event message received\n");
262 if (reg & LINK_REQ_RST)
263 dev_dbg(dra7xx->dev, "Link Request Reset\n");
265 if (reg & LINK_UP_EVT)
266 dev_dbg(dra7xx->dev, "Link-up state change\n");
268 if (reg & CFG_BME_EVT)
269 dev_dbg(dra7xx->dev, "CFG 'Bus Master Enable' change\n");
271 if (reg & CFG_MSE_EVT)
272 dev_dbg(dra7xx->dev, "CFG 'Memory Space Enable' change\n");
274 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
279 static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
280 struct platform_device *pdev)
283 struct pcie_port *pp;
284 struct resource *res;
285 struct device *dev = &pdev->dev;
289 pp->ops = &dra7xx_pcie_host_ops;
291 pp->irq = platform_get_irq(pdev, 1);
293 dev_err(dev, "missing IRQ resource\n");
297 ret = devm_request_irq(&pdev->dev, pp->irq,
298 dra7xx_pcie_msi_irq_handler, IRQF_SHARED,
299 "dra7-pcie-msi", pp);
301 dev_err(&pdev->dev, "failed to request irq\n");
305 if (!IS_ENABLED(CONFIG_PCI_MSI)) {
306 ret = dra7xx_pcie_init_irq_domain(pp);
311 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
312 pp->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
316 ret = dw_pcie_host_init(pp);
318 dev_err(dra7xx->dev, "failed to initialize host\n");
325 static int __init dra7xx_pcie_probe(struct platform_device *pdev)
334 struct resource *res;
335 struct dra7xx_pcie *dra7xx;
336 struct device *dev = &pdev->dev;
337 struct device_node *np = dev->of_node;
340 enum of_gpio_flags flags;
341 unsigned long gpio_flags;
343 dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
347 irq = platform_get_irq(pdev, 0);
349 dev_err(dev, "missing IRQ resource\n");
353 ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
354 IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
356 dev_err(dev, "failed to request irq\n");
360 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
361 base = devm_ioremap_nocache(dev, res->start, resource_size(res));
365 phy_count = of_property_count_strings(np, "phy-names");
367 dev_err(dev, "unable to find the strings\n");
371 phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL);
375 for (i = 0; i < phy_count; i++) {
376 snprintf(name, sizeof(name), "pcie-phy%d", i);
377 phy[i] = devm_phy_get(dev, name);
379 return PTR_ERR(phy[i]);
381 ret = phy_init(phy[i]);
385 ret = phy_power_on(phy[i]);
395 dra7xx->phy_count = phy_count;
397 pm_runtime_enable(dev);
398 ret = pm_runtime_get_sync(dev);
400 dev_err(dev, "pm_runtime_get_sync failed\n");
404 gpio_sel = of_get_gpio_flags(dev->of_node, 0, &flags);
405 if (gpio_is_valid(gpio_sel)) {
406 gpio_flags = (flags & OF_GPIO_ACTIVE_LOW) ?
407 GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH;
408 ret = devm_gpio_request_one(dev, gpio_sel, gpio_flags,
411 dev_err(&pdev->dev, "gpio%d request failed, ret %d\n",
415 } else if (gpio_sel == -EPROBE_DEFER) {
420 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
422 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
424 platform_set_drvdata(pdev, dra7xx);
426 ret = dra7xx_add_pcie_port(dra7xx, pdev);
436 pm_runtime_disable(dev);
440 phy_power_off(phy[i]);
447 static int __exit dra7xx_pcie_remove(struct platform_device *pdev)
449 struct dra7xx_pcie *dra7xx = platform_get_drvdata(pdev);
450 struct pcie_port *pp = &dra7xx->pp;
451 struct device *dev = &pdev->dev;
452 int count = dra7xx->phy_count;
455 irq_domain_remove(pp->irq_domain);
457 pm_runtime_disable(dev);
459 phy_power_off(dra7xx->phy[count]);
460 phy_exit(dra7xx->phy[count]);
466 #ifdef CONFIG_PM_SLEEP
467 static int dra7xx_pcie_suspend(struct device *dev)
469 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
470 struct pcie_port *pp = &dra7xx->pp;
474 val = dra7xx_pcie_readl_rc(pp, PCI_COMMAND);
475 val &= ~PCI_COMMAND_MEMORY;
476 dra7xx_pcie_writel_rc(pp, PCI_COMMAND, val);
481 static int dra7xx_pcie_resume(struct device *dev)
483 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
484 struct pcie_port *pp = &dra7xx->pp;
488 val = dra7xx_pcie_readl_rc(pp, PCI_COMMAND);
489 val |= PCI_COMMAND_MEMORY;
490 dra7xx_pcie_writel_rc(pp, PCI_COMMAND, val);
495 static int dra7xx_pcie_suspend_noirq(struct device *dev)
497 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
498 int count = dra7xx->phy_count;
501 phy_power_off(dra7xx->phy[count]);
502 phy_exit(dra7xx->phy[count]);
508 static int dra7xx_pcie_resume_noirq(struct device *dev)
510 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
511 int phy_count = dra7xx->phy_count;
515 for (i = 0; i < phy_count; i++) {
516 ret = phy_init(dra7xx->phy[i]);
520 ret = phy_power_on(dra7xx->phy[i]);
522 phy_exit(dra7xx->phy[i]);
531 phy_power_off(dra7xx->phy[i]);
532 phy_exit(dra7xx->phy[i]);
539 static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
540 SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
541 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
542 dra7xx_pcie_resume_noirq)
545 static const struct of_device_id of_dra7xx_pcie_match[] = {
546 { .compatible = "ti,dra7-pcie", },
549 MODULE_DEVICE_TABLE(of, of_dra7xx_pcie_match);
551 static struct platform_driver dra7xx_pcie_driver = {
552 .remove = __exit_p(dra7xx_pcie_remove),
555 .of_match_table = of_dra7xx_pcie_match,
556 .pm = &dra7xx_pcie_pm_ops,
560 module_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);
563 MODULE_DESCRIPTION("TI PCIe controller driver");
564 MODULE_LICENSE("GPL v2");