blob: e3d15d7875c27aeefda40ded8a49776ee852b04c [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
86static int dra7xx_pcie_link_up(struct pcie_port *pp)
87{
88 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
89 u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
90
91 return !!(reg & LINK_UP);
92}
93
94static int dra7xx_pcie_establish_link(struct pcie_port *pp)
95{
96 u32 reg;
97 unsigned int retries = 1000;
98 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
99
100 if (dw_pcie_link_up(pp)) {
101 dev_err(pp->dev, "link is already up\n");
102 return 0;
103 }
104
105 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
106 reg |= LTSSM_EN;
107 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
108
109 while (retries--) {
Bjorn Helgaas30fb7ba2015-06-02 16:21:11 -0500110 if (dw_pcie_link_up(pp))
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600111 break;
112 usleep_range(10, 20);
113 }
114
115 if (retries == 0) {
116 dev_err(pp->dev, "link is not up\n");
117 return -ETIMEDOUT;
118 }
119
120 return 0;
121}
122
123static void dra7xx_pcie_enable_interrupts(struct pcie_port *pp)
124{
125 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
126
127 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
128 ~INTERRUPTS);
129 dra7xx_pcie_writel(dra7xx,
130 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN, INTERRUPTS);
131 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
132 ~LEG_EP_INTERRUPTS & ~MSI);
133
134 if (IS_ENABLED(CONFIG_PCI_MSI))
135 dra7xx_pcie_writel(dra7xx,
136 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI, MSI);
137 else
138 dra7xx_pcie_writel(dra7xx,
139 PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
140 LEG_EP_INTERRUPTS);
141}
142
143static void dra7xx_pcie_host_init(struct pcie_port *pp)
144{
145 dw_pcie_setup_rc(pp);
146 dra7xx_pcie_establish_link(pp);
147 if (IS_ENABLED(CONFIG_PCI_MSI))
148 dw_pcie_msi_init(pp);
149 dra7xx_pcie_enable_interrupts(pp);
150}
151
152static struct pcie_host_ops dra7xx_pcie_host_ops = {
153 .link_up = dra7xx_pcie_link_up,
154 .host_init = dra7xx_pcie_host_init,
155};
156
157static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
158 irq_hw_number_t hwirq)
159{
160 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
161 irq_set_chip_data(irq, domain->host_data);
162 set_irq_flags(irq, IRQF_VALID);
163
164 return 0;
165}
166
167static const struct irq_domain_ops intx_domain_ops = {
168 .map = dra7xx_pcie_intx_map,
169};
170
171static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
172{
173 struct device *dev = pp->dev;
174 struct device_node *node = dev->of_node;
175 struct device_node *pcie_intc_node = of_get_next_child(node, NULL);
176
177 if (!pcie_intc_node) {
178 dev_err(dev, "No PCIe Intc node found\n");
179 return PTR_ERR(pcie_intc_node);
180 }
181
182 pp->irq_domain = irq_domain_add_linear(pcie_intc_node, 4,
183 &intx_domain_ops, pp);
184 if (!pp->irq_domain) {
185 dev_err(dev, "Failed to get a INTx IRQ domain\n");
186 return PTR_ERR(pp->irq_domain);
187 }
188
189 return 0;
190}
191
192static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
193{
194 struct pcie_port *pp = arg;
195 struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pp);
196 u32 reg;
197
198 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
199
200 switch (reg) {
201 case MSI:
202 dw_handle_msi_irq(pp);
203 break;
204 case INTA:
205 case INTB:
206 case INTC:
207 case INTD:
208 generic_handle_irq(irq_find_mapping(pp->irq_domain, ffs(reg)));
209 break;
210 }
211
212 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
213
214 return IRQ_HANDLED;
215}
216
217
218static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
219{
220 struct dra7xx_pcie *dra7xx = arg;
221 u32 reg;
222
223 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
224
225 if (reg & ERR_SYS)
226 dev_dbg(dra7xx->dev, "System Error\n");
227
228 if (reg & ERR_FATAL)
229 dev_dbg(dra7xx->dev, "Fatal Error\n");
230
231 if (reg & ERR_NONFATAL)
232 dev_dbg(dra7xx->dev, "Non Fatal Error\n");
233
234 if (reg & ERR_COR)
235 dev_dbg(dra7xx->dev, "Correctable Error\n");
236
237 if (reg & ERR_AXI)
238 dev_dbg(dra7xx->dev, "AXI tag lookup fatal Error\n");
239
240 if (reg & ERR_ECRC)
241 dev_dbg(dra7xx->dev, "ECRC Error\n");
242
243 if (reg & PME_TURN_OFF)
244 dev_dbg(dra7xx->dev,
245 "Power Management Event Turn-Off message received\n");
246
247 if (reg & PME_TO_ACK)
248 dev_dbg(dra7xx->dev,
249 "Power Management Turn-Off Ack message received\n");
250
251 if (reg & PM_PME)
252 dev_dbg(dra7xx->dev,
253 "PM Power Management Event message received\n");
254
255 if (reg & LINK_REQ_RST)
256 dev_dbg(dra7xx->dev, "Link Request Reset\n");
257
258 if (reg & LINK_UP_EVT)
259 dev_dbg(dra7xx->dev, "Link-up state change\n");
260
261 if (reg & CFG_BME_EVT)
262 dev_dbg(dra7xx->dev, "CFG 'Bus Master Enable' change\n");
263
264 if (reg & CFG_MSE_EVT)
265 dev_dbg(dra7xx->dev, "CFG 'Memory Space Enable' change\n");
266
267 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
268
269 return IRQ_HANDLED;
270}
271
Jingoo Hane73044a2014-11-06 14:37:39 +0900272static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
273 struct platform_device *pdev)
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600274{
275 int ret;
276 struct pcie_port *pp;
277 struct resource *res;
278 struct device *dev = &pdev->dev;
279
280 pp = &dra7xx->pp;
281 pp->dev = dev;
282 pp->ops = &dra7xx_pcie_host_ops;
283
284 pp->irq = platform_get_irq(pdev, 1);
285 if (pp->irq < 0) {
286 dev_err(dev, "missing IRQ resource\n");
287 return -EINVAL;
288 }
289
290 ret = devm_request_irq(&pdev->dev, pp->irq,
291 dra7xx_pcie_msi_irq_handler, IRQF_SHARED,
292 "dra7-pcie-msi", pp);
293 if (ret) {
294 dev_err(&pdev->dev, "failed to request irq\n");
295 return ret;
296 }
297
298 if (!IS_ENABLED(CONFIG_PCI_MSI)) {
299 ret = dra7xx_pcie_init_irq_domain(pp);
300 if (ret < 0)
301 return ret;
302 }
303
304 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
305 pp->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
306 if (!pp->dbi_base)
307 return -ENOMEM;
308
309 ret = dw_pcie_host_init(pp);
310 if (ret) {
311 dev_err(dra7xx->dev, "failed to initialize host\n");
312 return ret;
313 }
314
315 return 0;
316}
317
318static int __init dra7xx_pcie_probe(struct platform_device *pdev)
319{
320 u32 reg;
321 int ret;
322 int irq;
323 int i;
324 int phy_count;
325 struct phy **phy;
326 void __iomem *base;
327 struct resource *res;
328 struct dra7xx_pcie *dra7xx;
329 struct device *dev = &pdev->dev;
330 struct device_node *np = dev->of_node;
331 char name[10];
332
333 dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
334 if (!dra7xx)
335 return -ENOMEM;
336
337 irq = platform_get_irq(pdev, 0);
338 if (irq < 0) {
339 dev_err(dev, "missing IRQ resource\n");
340 return -EINVAL;
341 }
342
343 ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
344 IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
345 if (ret) {
346 dev_err(dev, "failed to request irq\n");
347 return ret;
348 }
349
350 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
351 base = devm_ioremap_nocache(dev, res->start, resource_size(res));
352 if (!base)
353 return -ENOMEM;
354
355 phy_count = of_property_count_strings(np, "phy-names");
356 if (phy_count < 0) {
357 dev_err(dev, "unable to find the strings\n");
358 return phy_count;
359 }
360
361 phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL);
362 if (!phy)
363 return -ENOMEM;
364
365 for (i = 0; i < phy_count; i++) {
366 snprintf(name, sizeof(name), "pcie-phy%d", i);
367 phy[i] = devm_phy_get(dev, name);
368 if (IS_ERR(phy[i]))
369 return PTR_ERR(phy[i]);
370
371 ret = phy_init(phy[i]);
372 if (ret < 0)
373 goto err_phy;
374
375 ret = phy_power_on(phy[i]);
376 if (ret < 0) {
377 phy_exit(phy[i]);
378 goto err_phy;
379 }
380 }
381
382 dra7xx->base = base;
383 dra7xx->phy = phy;
384 dra7xx->dev = dev;
385 dra7xx->phy_count = phy_count;
386
387 pm_runtime_enable(dev);
388 ret = pm_runtime_get_sync(dev);
389 if (IS_ERR_VALUE(ret)) {
390 dev_err(dev, "pm_runtime_get_sync failed\n");
391 goto err_phy;
392 }
393
394 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
395 reg &= ~LTSSM_EN;
396 dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
397
398 platform_set_drvdata(pdev, dra7xx);
399
Jingoo Han23926c82014-11-06 14:30:49 +0900400 ret = dra7xx_add_pcie_port(dra7xx, pdev);
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600401 if (ret < 0)
402 goto err_add_port;
403
404 return 0;
405
406err_add_port:
407 pm_runtime_put(dev);
408 pm_runtime_disable(dev);
409
410err_phy:
411 while (--i >= 0) {
412 phy_power_off(phy[i]);
413 phy_exit(phy[i]);
414 }
415
416 return ret;
417}
418
419static int __exit dra7xx_pcie_remove(struct platform_device *pdev)
420{
421 struct dra7xx_pcie *dra7xx = platform_get_drvdata(pdev);
422 struct pcie_port *pp = &dra7xx->pp;
423 struct device *dev = &pdev->dev;
424 int count = dra7xx->phy_count;
425
426 if (pp->irq_domain)
427 irq_domain_remove(pp->irq_domain);
428 pm_runtime_put(dev);
429 pm_runtime_disable(dev);
430 while (count--) {
431 phy_power_off(dra7xx->phy[count]);
432 phy_exit(dra7xx->phy[count]);
433 }
434
435 return 0;
436}
437
438static const struct of_device_id of_dra7xx_pcie_match[] = {
439 { .compatible = "ti,dra7-pcie", },
440 {},
441};
442MODULE_DEVICE_TABLE(of, of_dra7xx_pcie_match);
443
444static struct platform_driver dra7xx_pcie_driver = {
445 .remove = __exit_p(dra7xx_pcie_remove),
446 .driver = {
447 .name = "dra7-pcie",
Kishon Vijay Abraham I47ff3de2014-07-22 15:23:45 -0600448 .of_match_table = of_dra7xx_pcie_match,
449 },
450};
451
452module_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);
453
454MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
455MODULE_DESCRIPTION("TI PCIe controller driver");
456MODULE_LICENSE("GPL v2");