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/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)
65 #define DRA7XX_CPU_TO_BUS_ADDR 0x0FFFFFFF
75 #define to_dra7xx_pcie(x) container_of((x), struct dra7xx_pcie, pp)
77 static inline u32
dra7xx_pcie_readl(struct dra7xx_pcie
*pcie
, u32 offset
)
79 return readl(pcie
->base
+ offset
);
82 static inline void dra7xx_pcie_writel(struct dra7xx_pcie
*pcie
, u32 offset
,
85 writel(value
, pcie
->base
+ offset
);
88 static inline u32
dra7xx_pcie_readl_rc(struct pcie_port
*pp
, u32 offset
)
90 return readl(pp
->dbi_base
+ offset
);
93 static inline void dra7xx_pcie_writel_rc(struct pcie_port
*pp
, u32 offset
,
96 writel(value
, pp
->dbi_base
+ offset
);
99 static int dra7xx_pcie_link_up(struct pcie_port
*pp
)
101 struct dra7xx_pcie
*dra7xx
= to_dra7xx_pcie(pp
);
102 u32 reg
= dra7xx_pcie_readl(dra7xx
, PCIECTRL_DRA7XX_CONF_PHY_CS
);
104 return !!(reg
& LINK_UP
);
107 static int dra7xx_pcie_establish_link(struct pcie_port
*pp
)
109 struct dra7xx_pcie
*dra7xx
= to_dra7xx_pcie(pp
);
111 unsigned int retries
;
113 if (dw_pcie_link_up(pp
)) {
114 dev_err(pp
->dev
, "link is already up\n");
118 reg
= dra7xx_pcie_readl(dra7xx
, PCIECTRL_DRA7XX_CONF_DEVICE_CMD
);
120 dra7xx_pcie_writel(dra7xx
, PCIECTRL_DRA7XX_CONF_DEVICE_CMD
, reg
);
122 for (retries
= 0; retries
< 1000; retries
++) {
123 if (dw_pcie_link_up(pp
))
125 usleep_range(10, 20);
128 dev_err(pp
->dev
, "link is not up\n");
132 static void dra7xx_pcie_enable_interrupts(struct pcie_port
*pp
)
134 struct dra7xx_pcie
*dra7xx
= to_dra7xx_pcie(pp
);
136 dra7xx_pcie_writel(dra7xx
, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN
,
138 dra7xx_pcie_writel(dra7xx
,
139 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN
, INTERRUPTS
);
140 dra7xx_pcie_writel(dra7xx
, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI
,
141 ~LEG_EP_INTERRUPTS
& ~MSI
);
143 if (IS_ENABLED(CONFIG_PCI_MSI
))
144 dra7xx_pcie_writel(dra7xx
,
145 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI
, MSI
);
147 dra7xx_pcie_writel(dra7xx
,
148 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI
,
152 static void dra7xx_pcie_host_init(struct pcie_port
*pp
)
154 dw_pcie_setup_rc(pp
);
156 pp
->io_base
&= DRA7XX_CPU_TO_BUS_ADDR
;
157 pp
->mem_base
&= DRA7XX_CPU_TO_BUS_ADDR
;
158 pp
->cfg0_base
&= DRA7XX_CPU_TO_BUS_ADDR
;
159 pp
->cfg1_base
&= DRA7XX_CPU_TO_BUS_ADDR
;
161 dra7xx_pcie_establish_link(pp
);
162 if (IS_ENABLED(CONFIG_PCI_MSI
))
163 dw_pcie_msi_init(pp
);
164 dra7xx_pcie_enable_interrupts(pp
);
167 static struct pcie_host_ops dra7xx_pcie_host_ops
= {
168 .link_up
= dra7xx_pcie_link_up
,
169 .host_init
= dra7xx_pcie_host_init
,
172 static int dra7xx_pcie_intx_map(struct irq_domain
*domain
, unsigned int irq
,
173 irq_hw_number_t hwirq
)
175 irq_set_chip_and_handler(irq
, &dummy_irq_chip
, handle_simple_irq
);
176 irq_set_chip_data(irq
, domain
->host_data
);
181 static const struct irq_domain_ops intx_domain_ops
= {
182 .map
= dra7xx_pcie_intx_map
,
185 static int dra7xx_pcie_init_irq_domain(struct pcie_port
*pp
)
187 struct device
*dev
= pp
->dev
;
188 struct device_node
*node
= dev
->of_node
;
189 struct device_node
*pcie_intc_node
= of_get_next_child(node
, NULL
);
191 if (!pcie_intc_node
) {
192 dev_err(dev
, "No PCIe Intc node found\n");
193 return PTR_ERR(pcie_intc_node
);
196 pp
->irq_domain
= irq_domain_add_linear(pcie_intc_node
, 4,
197 &intx_domain_ops
, pp
);
198 if (!pp
->irq_domain
) {
199 dev_err(dev
, "Failed to get a INTx IRQ domain\n");
200 return PTR_ERR(pp
->irq_domain
);
206 static irqreturn_t
dra7xx_pcie_msi_irq_handler(int irq
, void *arg
)
208 struct pcie_port
*pp
= arg
;
209 struct dra7xx_pcie
*dra7xx
= to_dra7xx_pcie(pp
);
212 reg
= dra7xx_pcie_readl(dra7xx
, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI
);
216 dw_handle_msi_irq(pp
);
222 generic_handle_irq(irq_find_mapping(pp
->irq_domain
, ffs(reg
)));
226 dra7xx_pcie_writel(dra7xx
, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI
, reg
);
232 static irqreturn_t
dra7xx_pcie_irq_handler(int irq
, void *arg
)
234 struct dra7xx_pcie
*dra7xx
= arg
;
237 reg
= dra7xx_pcie_readl(dra7xx
, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN
);
240 dev_dbg(dra7xx
->dev
, "System Error\n");
243 dev_dbg(dra7xx
->dev
, "Fatal Error\n");
245 if (reg
& ERR_NONFATAL
)
246 dev_dbg(dra7xx
->dev
, "Non Fatal Error\n");
249 dev_dbg(dra7xx
->dev
, "Correctable Error\n");
252 dev_dbg(dra7xx
->dev
, "AXI tag lookup fatal Error\n");
255 dev_dbg(dra7xx
->dev
, "ECRC Error\n");
257 if (reg
& PME_TURN_OFF
)
259 "Power Management Event Turn-Off message received\n");
261 if (reg
& PME_TO_ACK
)
263 "Power Management Turn-Off Ack message received\n");
267 "PM Power Management Event message received\n");
269 if (reg
& LINK_REQ_RST
)
270 dev_dbg(dra7xx
->dev
, "Link Request Reset\n");
272 if (reg
& LINK_UP_EVT
)
273 dev_dbg(dra7xx
->dev
, "Link-up state change\n");
275 if (reg
& CFG_BME_EVT
)
276 dev_dbg(dra7xx
->dev
, "CFG 'Bus Master Enable' change\n");
278 if (reg
& CFG_MSE_EVT
)
279 dev_dbg(dra7xx
->dev
, "CFG 'Memory Space Enable' change\n");
281 dra7xx_pcie_writel(dra7xx
, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN
, reg
);
286 static int __init
dra7xx_add_pcie_port(struct dra7xx_pcie
*dra7xx
,
287 struct platform_device
*pdev
)
290 struct pcie_port
*pp
;
291 struct resource
*res
;
292 struct device
*dev
= &pdev
->dev
;
296 pp
->ops
= &dra7xx_pcie_host_ops
;
298 pp
->irq
= platform_get_irq(pdev
, 1);
300 dev_err(dev
, "missing IRQ resource\n");
304 ret
= devm_request_irq(&pdev
->dev
, pp
->irq
,
305 dra7xx_pcie_msi_irq_handler
, IRQF_SHARED
,
306 "dra7-pcie-msi", pp
);
308 dev_err(&pdev
->dev
, "failed to request irq\n");
312 if (!IS_ENABLED(CONFIG_PCI_MSI
)) {
313 ret
= dra7xx_pcie_init_irq_domain(pp
);
318 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "rc_dbics");
319 pp
->dbi_base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
323 ret
= dw_pcie_host_init(pp
);
325 dev_err(dra7xx
->dev
, "failed to initialize host\n");
332 static int __init
dra7xx_pcie_probe(struct platform_device
*pdev
)
341 struct resource
*res
;
342 struct dra7xx_pcie
*dra7xx
;
343 struct device
*dev
= &pdev
->dev
;
344 struct device_node
*np
= dev
->of_node
;
347 enum of_gpio_flags flags
;
348 unsigned long gpio_flags
;
350 dra7xx
= devm_kzalloc(dev
, sizeof(*dra7xx
), GFP_KERNEL
);
354 irq
= platform_get_irq(pdev
, 0);
356 dev_err(dev
, "missing IRQ resource\n");
360 ret
= devm_request_irq(dev
, irq
, dra7xx_pcie_irq_handler
,
361 IRQF_SHARED
, "dra7xx-pcie-main", dra7xx
);
363 dev_err(dev
, "failed to request irq\n");
367 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "ti_conf");
368 base
= devm_ioremap_nocache(dev
, res
->start
, resource_size(res
));
372 phy_count
= of_property_count_strings(np
, "phy-names");
374 dev_err(dev
, "unable to find the strings\n");
378 phy
= devm_kzalloc(dev
, sizeof(*phy
) * phy_count
, GFP_KERNEL
);
382 for (i
= 0; i
< phy_count
; i
++) {
383 snprintf(name
, sizeof(name
), "pcie-phy%d", i
);
384 phy
[i
] = devm_phy_get(dev
, name
);
386 return PTR_ERR(phy
[i
]);
388 ret
= phy_init(phy
[i
]);
392 ret
= phy_power_on(phy
[i
]);
402 dra7xx
->phy_count
= phy_count
;
404 pm_runtime_enable(dev
);
405 ret
= pm_runtime_get_sync(dev
);
407 dev_err(dev
, "pm_runtime_get_sync failed\n");
411 gpio_sel
= of_get_gpio_flags(dev
->of_node
, 0, &flags
);
412 if (gpio_is_valid(gpio_sel
)) {
413 gpio_flags
= (flags
& OF_GPIO_ACTIVE_LOW
) ?
414 GPIOF_OUT_INIT_LOW
: GPIOF_OUT_INIT_HIGH
;
415 ret
= devm_gpio_request_one(dev
, gpio_sel
, gpio_flags
,
418 dev_err(&pdev
->dev
, "gpio%d request failed, ret %d\n",
422 } else if (gpio_sel
== -EPROBE_DEFER
) {
427 reg
= dra7xx_pcie_readl(dra7xx
, PCIECTRL_DRA7XX_CONF_DEVICE_CMD
);
429 dra7xx_pcie_writel(dra7xx
, PCIECTRL_DRA7XX_CONF_DEVICE_CMD
, reg
);
431 platform_set_drvdata(pdev
, dra7xx
);
433 ret
= dra7xx_add_pcie_port(dra7xx
, pdev
);
443 pm_runtime_disable(dev
);
447 phy_power_off(phy
[i
]);
454 static int __exit
dra7xx_pcie_remove(struct platform_device
*pdev
)
456 struct dra7xx_pcie
*dra7xx
= platform_get_drvdata(pdev
);
457 struct pcie_port
*pp
= &dra7xx
->pp
;
458 struct device
*dev
= &pdev
->dev
;
459 int count
= dra7xx
->phy_count
;
462 irq_domain_remove(pp
->irq_domain
);
464 pm_runtime_disable(dev
);
466 phy_power_off(dra7xx
->phy
[count
]);
467 phy_exit(dra7xx
->phy
[count
]);
473 #ifdef CONFIG_PM_SLEEP
474 static int dra7xx_pcie_suspend(struct device
*dev
)
476 struct dra7xx_pcie
*dra7xx
= dev_get_drvdata(dev
);
477 struct pcie_port
*pp
= &dra7xx
->pp
;
481 val
= dra7xx_pcie_readl_rc(pp
, PCI_COMMAND
);
482 val
&= ~PCI_COMMAND_MEMORY
;
483 dra7xx_pcie_writel_rc(pp
, PCI_COMMAND
, val
);
488 static int dra7xx_pcie_resume(struct device
*dev
)
490 struct dra7xx_pcie
*dra7xx
= dev_get_drvdata(dev
);
491 struct pcie_port
*pp
= &dra7xx
->pp
;
495 val
= dra7xx_pcie_readl_rc(pp
, PCI_COMMAND
);
496 val
|= PCI_COMMAND_MEMORY
;
497 dra7xx_pcie_writel_rc(pp
, PCI_COMMAND
, val
);
502 static int dra7xx_pcie_suspend_noirq(struct device
*dev
)
504 struct dra7xx_pcie
*dra7xx
= dev_get_drvdata(dev
);
505 int count
= dra7xx
->phy_count
;
508 phy_power_off(dra7xx
->phy
[count
]);
509 phy_exit(dra7xx
->phy
[count
]);
515 static int dra7xx_pcie_resume_noirq(struct device
*dev
)
517 struct dra7xx_pcie
*dra7xx
= dev_get_drvdata(dev
);
518 int phy_count
= dra7xx
->phy_count
;
522 for (i
= 0; i
< phy_count
; i
++) {
523 ret
= phy_init(dra7xx
->phy
[i
]);
527 ret
= phy_power_on(dra7xx
->phy
[i
]);
529 phy_exit(dra7xx
->phy
[i
]);
538 phy_power_off(dra7xx
->phy
[i
]);
539 phy_exit(dra7xx
->phy
[i
]);
546 static const struct dev_pm_ops dra7xx_pcie_pm_ops
= {
547 SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend
, dra7xx_pcie_resume
)
548 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq
,
549 dra7xx_pcie_resume_noirq
)
552 static const struct of_device_id of_dra7xx_pcie_match
[] = {
553 { .compatible
= "ti,dra7-pcie", },
556 MODULE_DEVICE_TABLE(of
, of_dra7xx_pcie_match
);
558 static struct platform_driver dra7xx_pcie_driver
= {
559 .remove
= __exit_p(dra7xx_pcie_remove
),
562 .of_match_table
= of_dra7xx_pcie_match
,
563 .pm
= &dra7xx_pcie_pm_ops
,
567 module_platform_driver_probe(dra7xx_pcie_driver
, dra7xx_pcie_probe
);
569 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
570 MODULE_DESCRIPTION("TI PCIe controller driver");
571 MODULE_LICENSE("GPL v2");