2 * pcie-dra7xx - PCIe controller driver for TI DRA7xx SoCs
4 * Copyright (C) 2013-2014 Texas Instruments Incorporated - http://www.ti.com
6 * Authors: Kishon Vijay Abraham I <kishon@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/err.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/of_gpio.h>
20 #include <linux/of_pci.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)
65 #define DRA7XX_CPU_TO_BUS_ADDR 0x0FFFFFFF
67 #define EXP_CAP_ID_OFFSET 0x70
71 void __iomem *base; /* DT ti_conf */
72 int phy_count; /* DT phy-names count */
75 struct irq_domain *irq_domain;
78 #define to_dra7xx_pcie(x) dev_get_drvdata((x)->dev)
80 static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
82 return readl(pcie->base + offset);
85 static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
88 writel(value, pcie->base + offset);
91 static int dra7xx_pcie_link_up(struct dw_pcie *pci)
93 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
94 u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
96 return !!(reg & LINK_UP);
99 static int dra7xx_pcie_establish_link(struct dra7xx_pcie *dra7xx)
101 struct dw_pcie *pci = dra7xx->pci;
102 struct device *dev = pci->dev;
104 u32 exp_cap_off = EXP_CAP_ID_OFFSET;
106 if (dw_pcie_link_up(pci)) {
107 dev_err(dev, "link is already up\n");
111 if (dra7xx->link_gen == 1) {
112 dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCAP,
114 if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
115 reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
116 reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
117 dw_pcie_write(pci->dbi_base + exp_cap_off +
118 PCI_EXP_LNKCAP, 4, reg);
121 dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2,
123 if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
124 reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
125 reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
126 dw_pcie_write(pci->dbi_base + exp_cap_off +
127 PCI_EXP_LNKCTL2, 2, reg);
131 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
133 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
135 return dw_pcie_wait_for_link(pci);
138 static void dra7xx_pcie_enable_interrupts(struct dra7xx_pcie *dra7xx)
140 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
142 dra7xx_pcie_writel(dra7xx,
143 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN, INTERRUPTS);
144 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
145 ~LEG_EP_INTERRUPTS & ~MSI);
146 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
147 MSI | LEG_EP_INTERRUPTS);
150 static void dra7xx_pcie_host_init(struct pcie_port *pp)
152 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
153 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
155 pp->io_base &= DRA7XX_CPU_TO_BUS_ADDR;
156 pp->mem_base &= DRA7XX_CPU_TO_BUS_ADDR;
157 pp->cfg0_base &= DRA7XX_CPU_TO_BUS_ADDR;
158 pp->cfg1_base &= DRA7XX_CPU_TO_BUS_ADDR;
160 dw_pcie_setup_rc(pp);
162 dra7xx_pcie_establish_link(dra7xx);
163 dw_pcie_msi_init(pp);
164 dra7xx_pcie_enable_interrupts(dra7xx);
167 static struct dw_pcie_host_ops dra7xx_pcie_host_ops = {
168 .host_init = dra7xx_pcie_host_init,
171 static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
172 irq_hw_number_t hwirq)
174 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
175 irq_set_chip_data(irq, domain->host_data);
180 static const struct irq_domain_ops intx_domain_ops = {
181 .map = dra7xx_pcie_intx_map,
184 static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
186 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
187 struct device *dev = pci->dev;
188 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
189 struct device_node *node = dev->of_node;
190 struct device_node *pcie_intc_node = of_get_next_child(node, NULL);
192 if (!pcie_intc_node) {
193 dev_err(dev, "No PCIe Intc node found\n");
197 dra7xx->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
198 &intx_domain_ops, pp);
199 if (!dra7xx->irq_domain) {
200 dev_err(dev, "Failed to get a INTx IRQ domain\n");
207 static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
209 struct dra7xx_pcie *dra7xx = arg;
210 struct dw_pcie *pci = dra7xx->pci;
211 struct pcie_port *pp = &pci->pp;
214 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
218 dw_handle_msi_irq(pp);
224 generic_handle_irq(irq_find_mapping(dra7xx->irq_domain,
229 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
235 static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
237 struct dra7xx_pcie *dra7xx = arg;
238 struct dw_pcie *pci = dra7xx->pci;
239 struct device *dev = pci->dev;
242 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
245 dev_dbg(dev, "System Error\n");
248 dev_dbg(dev, "Fatal Error\n");
250 if (reg & ERR_NONFATAL)
251 dev_dbg(dev, "Non Fatal Error\n");
254 dev_dbg(dev, "Correctable Error\n");
257 dev_dbg(dev, "AXI tag lookup fatal Error\n");
260 dev_dbg(dev, "ECRC Error\n");
262 if (reg & PME_TURN_OFF)
264 "Power Management Event Turn-Off message received\n");
266 if (reg & PME_TO_ACK)
268 "Power Management Turn-Off Ack message received\n");
271 dev_dbg(dev, "PM Power Management Event message received\n");
273 if (reg & LINK_REQ_RST)
274 dev_dbg(dev, "Link Request Reset\n");
276 if (reg & LINK_UP_EVT)
277 dev_dbg(dev, "Link-up state change\n");
279 if (reg & CFG_BME_EVT)
280 dev_dbg(dev, "CFG 'Bus Master Enable' change\n");
282 if (reg & CFG_MSE_EVT)
283 dev_dbg(dev, "CFG 'Memory Space Enable' change\n");
285 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
290 static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
291 struct platform_device *pdev)
294 struct dw_pcie *pci = dra7xx->pci;
295 struct pcie_port *pp = &pci->pp;
296 struct device *dev = pci->dev;
297 struct resource *res;
299 pp->irq = platform_get_irq(pdev, 1);
301 dev_err(dev, "missing IRQ resource\n");
305 ret = devm_request_irq(dev, pp->irq, dra7xx_pcie_msi_irq_handler,
306 IRQF_SHARED | IRQF_NO_THREAD,
307 "dra7-pcie-msi", dra7xx);
309 dev_err(dev, "failed to request irq\n");
313 ret = dra7xx_pcie_init_irq_domain(pp);
317 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
318 pci->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
322 ret = dw_pcie_host_init(pp);
324 dev_err(dev, "failed to initialize host\n");
331 static const struct dw_pcie_ops dw_pcie_ops = {
332 .link_up = dra7xx_pcie_link_up,
335 static void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx)
337 int phy_count = dra7xx->phy_count;
339 while (phy_count--) {
340 phy_power_off(dra7xx->phy[phy_count]);
341 phy_exit(dra7xx->phy[phy_count]);
345 static int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx)
347 int phy_count = dra7xx->phy_count;
351 for (i = 0; i < phy_count; i++) {
352 ret = phy_init(dra7xx->phy[i]);
356 ret = phy_power_on(dra7xx->phy[i]);
358 phy_exit(dra7xx->phy[i]);
367 phy_power_off(dra7xx->phy[i]);
368 phy_exit(dra7xx->phy[i]);
374 static int __init dra7xx_pcie_probe(struct platform_device *pdev)
383 struct resource *res;
385 struct pcie_port *pp;
386 struct dra7xx_pcie *dra7xx;
387 struct device *dev = &pdev->dev;
388 struct device_node *np = dev->of_node;
390 struct gpio_desc *reset;
392 dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
396 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
401 pci->ops = &dw_pcie_ops;
404 pp->ops = &dra7xx_pcie_host_ops;
406 irq = platform_get_irq(pdev, 0);
408 dev_err(dev, "missing IRQ resource\n");
412 ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
413 IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
415 dev_err(dev, "failed to request irq\n");
419 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
420 base = devm_ioremap_nocache(dev, res->start, resource_size(res));
424 phy_count = of_property_count_strings(np, "phy-names");
426 dev_err(dev, "unable to find the strings\n");
430 phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL);
434 for (i = 0; i < phy_count; i++) {
435 snprintf(name, sizeof(name), "pcie-phy%d", i);
436 phy[i] = devm_phy_get(dev, name);
438 return PTR_ERR(phy[i]);
444 dra7xx->phy_count = phy_count;
446 ret = dra7xx_pcie_enable_phy(dra7xx);
448 dev_err(dev, "failed to enable phy\n");
452 platform_set_drvdata(pdev, dra7xx);
454 pm_runtime_enable(dev);
455 ret = pm_runtime_get_sync(dev);
457 dev_err(dev, "pm_runtime_get_sync failed\n");
461 reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
463 ret = PTR_ERR(reset);
464 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
468 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
470 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
472 dra7xx->link_gen = of_pci_get_max_link_speed(np);
473 if (dra7xx->link_gen < 0 || dra7xx->link_gen > 2)
474 dra7xx->link_gen = 2;
476 ret = dra7xx_add_pcie_port(dra7xx, pdev);
486 pm_runtime_disable(dev);
487 dra7xx_pcie_disable_phy(dra7xx);
492 #ifdef CONFIG_PM_SLEEP
493 static int dra7xx_pcie_suspend(struct device *dev)
495 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
496 struct dw_pcie *pci = dra7xx->pci;
500 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
501 val &= ~PCI_COMMAND_MEMORY;
502 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
507 static int dra7xx_pcie_resume(struct device *dev)
509 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
510 struct dw_pcie *pci = dra7xx->pci;
514 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
515 val |= PCI_COMMAND_MEMORY;
516 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
521 static int dra7xx_pcie_suspend_noirq(struct device *dev)
523 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
525 dra7xx_pcie_disable_phy(dra7xx);
530 static int dra7xx_pcie_resume_noirq(struct device *dev)
532 struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
535 ret = dra7xx_pcie_enable_phy(dra7xx);
537 dev_err(dev, "failed to enable phy\n");
545 static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
546 SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
547 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
548 dra7xx_pcie_resume_noirq)
551 static const struct of_device_id of_dra7xx_pcie_match[] = {
552 { .compatible = "ti,dra7-pcie", },
556 static struct platform_driver dra7xx_pcie_driver = {
559 .of_match_table = of_dra7xx_pcie_match,
560 .suppress_bind_attrs = true,
561 .pm = &dra7xx_pcie_pm_ops,
564 builtin_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);