]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/pci/controller/dwc/pci-dra7xx.c
PCI: pci-dra7xx: Populate ->get_features() dw_pcie_ep_ops
[linux.git] / drivers / pci / controller / dwc / pci-dra7xx.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * pcie-dra7xx - PCIe controller driver for TI DRA7xx SoCs
4  *
5  * Copyright (C) 2013-2014 Texas Instruments Incorporated - http://www.ti.com
6  *
7  * Authors: Kishon Vijay Abraham I <kishon@ti.com>
8  */
9
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/irqdomain.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/of_device.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>
27 #include <linux/mfd/syscon.h>
28 #include <linux/regmap.h>
29
30 #include "../../pci.h"
31 #include "pcie-designware.h"
32
33 /* PCIe controller wrapper DRA7XX configuration registers */
34
35 #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN             0x0024
36 #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN         0x0028
37 #define ERR_SYS                                         BIT(0)
38 #define ERR_FATAL                                       BIT(1)
39 #define ERR_NONFATAL                                    BIT(2)
40 #define ERR_COR                                         BIT(3)
41 #define ERR_AXI                                         BIT(4)
42 #define ERR_ECRC                                        BIT(5)
43 #define PME_TURN_OFF                                    BIT(8)
44 #define PME_TO_ACK                                      BIT(9)
45 #define PM_PME                                          BIT(10)
46 #define LINK_REQ_RST                                    BIT(11)
47 #define LINK_UP_EVT                                     BIT(12)
48 #define CFG_BME_EVT                                     BIT(13)
49 #define CFG_MSE_EVT                                     BIT(14)
50 #define INTERRUPTS (ERR_SYS | ERR_FATAL | ERR_NONFATAL | ERR_COR | ERR_AXI | \
51                         ERR_ECRC | PME_TURN_OFF | PME_TO_ACK | PM_PME | \
52                         LINK_REQ_RST | LINK_UP_EVT | CFG_BME_EVT | CFG_MSE_EVT)
53
54 #define PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI              0x0034
55 #define PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI          0x0038
56 #define INTA                                            BIT(0)
57 #define INTB                                            BIT(1)
58 #define INTC                                            BIT(2)
59 #define INTD                                            BIT(3)
60 #define MSI                                             BIT(4)
61 #define LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD)
62
63 #define PCIECTRL_TI_CONF_DEVICE_TYPE                    0x0100
64 #define DEVICE_TYPE_EP                                  0x0
65 #define DEVICE_TYPE_LEG_EP                              0x1
66 #define DEVICE_TYPE_RC                                  0x4
67
68 #define PCIECTRL_DRA7XX_CONF_DEVICE_CMD                 0x0104
69 #define LTSSM_EN                                        0x1
70
71 #define PCIECTRL_DRA7XX_CONF_PHY_CS                     0x010C
72 #define LINK_UP                                         BIT(16)
73 #define DRA7XX_CPU_TO_BUS_ADDR                          0x0FFFFFFF
74
75 #define EXP_CAP_ID_OFFSET                               0x70
76
77 #define PCIECTRL_TI_CONF_INTX_ASSERT                    0x0124
78 #define PCIECTRL_TI_CONF_INTX_DEASSERT                  0x0128
79
80 #define PCIECTRL_TI_CONF_MSI_XMT                        0x012c
81 #define MSI_REQ_GRANT                                   BIT(0)
82 #define MSI_VECTOR_SHIFT                                7
83
84 struct dra7xx_pcie {
85         struct dw_pcie          *pci;
86         void __iomem            *base;          /* DT ti_conf */
87         int                     phy_count;      /* DT phy-names count */
88         struct phy              **phy;
89         int                     link_gen;
90         struct irq_domain       *irq_domain;
91         enum dw_pcie_device_mode mode;
92 };
93
94 struct dra7xx_pcie_of_data {
95         enum dw_pcie_device_mode mode;
96 };
97
98 #define to_dra7xx_pcie(x)       dev_get_drvdata((x)->dev)
99
100 static inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
101 {
102         return readl(pcie->base + offset);
103 }
104
105 static inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
106                                       u32 value)
107 {
108         writel(value, pcie->base + offset);
109 }
110
111 static u64 dra7xx_pcie_cpu_addr_fixup(struct dw_pcie *pci, u64 pci_addr)
112 {
113         return pci_addr & DRA7XX_CPU_TO_BUS_ADDR;
114 }
115
116 static int dra7xx_pcie_link_up(struct dw_pcie *pci)
117 {
118         struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
119         u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
120
121         return !!(reg & LINK_UP);
122 }
123
124 static void dra7xx_pcie_stop_link(struct dw_pcie *pci)
125 {
126         struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
127         u32 reg;
128
129         reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
130         reg &= ~LTSSM_EN;
131         dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
132 }
133
134 static int dra7xx_pcie_establish_link(struct dw_pcie *pci)
135 {
136         struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
137         struct device *dev = pci->dev;
138         u32 reg;
139         u32 exp_cap_off = EXP_CAP_ID_OFFSET;
140
141         if (dw_pcie_link_up(pci)) {
142                 dev_err(dev, "link is already up\n");
143                 return 0;
144         }
145
146         if (dra7xx->link_gen == 1) {
147                 dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCAP,
148                              4, &reg);
149                 if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
150                         reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
151                         reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
152                         dw_pcie_write(pci->dbi_base + exp_cap_off +
153                                       PCI_EXP_LNKCAP, 4, reg);
154                 }
155
156                 dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2,
157                              2, &reg);
158                 if ((reg & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
159                         reg &= ~((u32)PCI_EXP_LNKCAP_SLS);
160                         reg |= PCI_EXP_LNKCAP_SLS_2_5GB;
161                         dw_pcie_write(pci->dbi_base + exp_cap_off +
162                                       PCI_EXP_LNKCTL2, 2, reg);
163                 }
164         }
165
166         reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
167         reg |= LTSSM_EN;
168         dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
169
170         return 0;
171 }
172
173 static void dra7xx_pcie_enable_msi_interrupts(struct dra7xx_pcie *dra7xx)
174 {
175         dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
176                            LEG_EP_INTERRUPTS | MSI);
177
178         dra7xx_pcie_writel(dra7xx,
179                            PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
180                            MSI | LEG_EP_INTERRUPTS);
181 }
182
183 static void dra7xx_pcie_enable_wrapper_interrupts(struct dra7xx_pcie *dra7xx)
184 {
185         dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
186                            INTERRUPTS);
187         dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN,
188                            INTERRUPTS);
189 }
190
191 static void dra7xx_pcie_enable_interrupts(struct dra7xx_pcie *dra7xx)
192 {
193         dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
194         dra7xx_pcie_enable_msi_interrupts(dra7xx);
195 }
196
197 static int dra7xx_pcie_host_init(struct pcie_port *pp)
198 {
199         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
200         struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
201
202         dw_pcie_setup_rc(pp);
203
204         dra7xx_pcie_establish_link(pci);
205         dw_pcie_wait_for_link(pci);
206         dw_pcie_msi_init(pp);
207         dra7xx_pcie_enable_interrupts(dra7xx);
208
209         return 0;
210 }
211
212 static const struct dw_pcie_host_ops dra7xx_pcie_host_ops = {
213         .host_init = dra7xx_pcie_host_init,
214 };
215
216 static int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
217                                 irq_hw_number_t hwirq)
218 {
219         irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
220         irq_set_chip_data(irq, domain->host_data);
221
222         return 0;
223 }
224
225 static const struct irq_domain_ops intx_domain_ops = {
226         .map = dra7xx_pcie_intx_map,
227         .xlate = pci_irqd_intx_xlate,
228 };
229
230 static int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
231 {
232         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
233         struct device *dev = pci->dev;
234         struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
235         struct device_node *node = dev->of_node;
236         struct device_node *pcie_intc_node =  of_get_next_child(node, NULL);
237
238         if (!pcie_intc_node) {
239                 dev_err(dev, "No PCIe Intc node found\n");
240                 return -ENODEV;
241         }
242
243         dra7xx->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
244                                                    &intx_domain_ops, pp);
245         if (!dra7xx->irq_domain) {
246                 dev_err(dev, "Failed to get a INTx IRQ domain\n");
247                 return -ENODEV;
248         }
249
250         return 0;
251 }
252
253 static irqreturn_t dra7xx_pcie_msi_irq_handler(int irq, void *arg)
254 {
255         struct dra7xx_pcie *dra7xx = arg;
256         struct dw_pcie *pci = dra7xx->pci;
257         struct pcie_port *pp = &pci->pp;
258         unsigned long reg;
259         u32 virq, bit;
260
261         reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
262
263         switch (reg) {
264         case MSI:
265                 dw_handle_msi_irq(pp);
266                 break;
267         case INTA:
268         case INTB:
269         case INTC:
270         case INTD:
271                 for_each_set_bit(bit, &reg, PCI_NUM_INTX) {
272                         virq = irq_find_mapping(dra7xx->irq_domain, bit);
273                         if (virq)
274                                 generic_handle_irq(virq);
275                 }
276                 break;
277         }
278
279         dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
280
281         return IRQ_HANDLED;
282 }
283
284 static irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
285 {
286         struct dra7xx_pcie *dra7xx = arg;
287         struct dw_pcie *pci = dra7xx->pci;
288         struct device *dev = pci->dev;
289         struct dw_pcie_ep *ep = &pci->ep;
290         u32 reg;
291
292         reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
293
294         if (reg & ERR_SYS)
295                 dev_dbg(dev, "System Error\n");
296
297         if (reg & ERR_FATAL)
298                 dev_dbg(dev, "Fatal Error\n");
299
300         if (reg & ERR_NONFATAL)
301                 dev_dbg(dev, "Non Fatal Error\n");
302
303         if (reg & ERR_COR)
304                 dev_dbg(dev, "Correctable Error\n");
305
306         if (reg & ERR_AXI)
307                 dev_dbg(dev, "AXI tag lookup fatal Error\n");
308
309         if (reg & ERR_ECRC)
310                 dev_dbg(dev, "ECRC Error\n");
311
312         if (reg & PME_TURN_OFF)
313                 dev_dbg(dev,
314                         "Power Management Event Turn-Off message received\n");
315
316         if (reg & PME_TO_ACK)
317                 dev_dbg(dev,
318                         "Power Management Turn-Off Ack message received\n");
319
320         if (reg & PM_PME)
321                 dev_dbg(dev, "PM Power Management Event message received\n");
322
323         if (reg & LINK_REQ_RST)
324                 dev_dbg(dev, "Link Request Reset\n");
325
326         if (reg & LINK_UP_EVT) {
327                 if (dra7xx->mode == DW_PCIE_EP_TYPE)
328                         dw_pcie_ep_linkup(ep);
329                 dev_dbg(dev, "Link-up state change\n");
330         }
331
332         if (reg & CFG_BME_EVT)
333                 dev_dbg(dev, "CFG 'Bus Master Enable' change\n");
334
335         if (reg & CFG_MSE_EVT)
336                 dev_dbg(dev, "CFG 'Memory Space Enable' change\n");
337
338         dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
339
340         return IRQ_HANDLED;
341 }
342
343 static void dra7xx_pcie_ep_init(struct dw_pcie_ep *ep)
344 {
345         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
346         struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
347         enum pci_barno bar;
348
349         for (bar = BAR_0; bar <= BAR_5; bar++)
350                 dw_pcie_ep_reset_bar(pci, bar);
351
352         dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
353 }
354
355 static void dra7xx_pcie_raise_legacy_irq(struct dra7xx_pcie *dra7xx)
356 {
357         dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_ASSERT, 0x1);
358         mdelay(1);
359         dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_DEASSERT, 0x1);
360 }
361
362 static void dra7xx_pcie_raise_msi_irq(struct dra7xx_pcie *dra7xx,
363                                       u8 interrupt_num)
364 {
365         u32 reg;
366
367         reg = (interrupt_num - 1) << MSI_VECTOR_SHIFT;
368         reg |= MSI_REQ_GRANT;
369         dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_MSI_XMT, reg);
370 }
371
372 static int dra7xx_pcie_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
373                                  enum pci_epc_irq_type type, u16 interrupt_num)
374 {
375         struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
376         struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
377
378         switch (type) {
379         case PCI_EPC_IRQ_LEGACY:
380                 dra7xx_pcie_raise_legacy_irq(dra7xx);
381                 break;
382         case PCI_EPC_IRQ_MSI:
383                 dra7xx_pcie_raise_msi_irq(dra7xx, interrupt_num);
384                 break;
385         default:
386                 dev_err(pci->dev, "UNKNOWN IRQ type\n");
387         }
388
389         return 0;
390 }
391
392 static const struct pci_epc_features dra7xx_pcie_epc_features = {
393         .linkup_notifier = true,
394         .msi_capable = true,
395         .msix_capable = false,
396 };
397
398 static const struct pci_epc_features*
399 dra7xx_pcie_get_features(struct dw_pcie_ep *ep)
400 {
401         return &dra7xx_pcie_epc_features;
402 }
403
404 static struct dw_pcie_ep_ops pcie_ep_ops = {
405         .ep_init = dra7xx_pcie_ep_init,
406         .raise_irq = dra7xx_pcie_raise_irq,
407         .get_features = dra7xx_pcie_get_features,
408 };
409
410 static int __init dra7xx_add_pcie_ep(struct dra7xx_pcie *dra7xx,
411                                      struct platform_device *pdev)
412 {
413         int ret;
414         struct dw_pcie_ep *ep;
415         struct resource *res;
416         struct device *dev = &pdev->dev;
417         struct dw_pcie *pci = dra7xx->pci;
418
419         ep = &pci->ep;
420         ep->ops = &pcie_ep_ops;
421
422         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics");
423         pci->dbi_base = devm_ioremap_resource(dev, res);
424         if (IS_ERR(pci->dbi_base))
425                 return PTR_ERR(pci->dbi_base);
426
427         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics2");
428         pci->dbi_base2 = devm_ioremap_resource(dev, res);
429         if (IS_ERR(pci->dbi_base2))
430                 return PTR_ERR(pci->dbi_base2);
431
432         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
433         if (!res)
434                 return -EINVAL;
435
436         ep->phys_base = res->start;
437         ep->addr_size = resource_size(res);
438
439         ret = dw_pcie_ep_init(ep);
440         if (ret) {
441                 dev_err(dev, "failed to initialize endpoint\n");
442                 return ret;
443         }
444
445         return 0;
446 }
447
448 static int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
449                                        struct platform_device *pdev)
450 {
451         int ret;
452         struct dw_pcie *pci = dra7xx->pci;
453         struct pcie_port *pp = &pci->pp;
454         struct device *dev = pci->dev;
455         struct resource *res;
456
457         pp->irq = platform_get_irq(pdev, 1);
458         if (pp->irq < 0) {
459                 dev_err(dev, "missing IRQ resource\n");
460                 return pp->irq;
461         }
462
463         ret = devm_request_irq(dev, pp->irq, dra7xx_pcie_msi_irq_handler,
464                                IRQF_SHARED | IRQF_NO_THREAD,
465                                "dra7-pcie-msi", dra7xx);
466         if (ret) {
467                 dev_err(dev, "failed to request irq\n");
468                 return ret;
469         }
470
471         ret = dra7xx_pcie_init_irq_domain(pp);
472         if (ret < 0)
473                 return ret;
474
475         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbics");
476         pci->dbi_base = devm_ioremap_resource(dev, res);
477         if (IS_ERR(pci->dbi_base))
478                 return PTR_ERR(pci->dbi_base);
479
480         pp->ops = &dra7xx_pcie_host_ops;
481
482         ret = dw_pcie_host_init(pp);
483         if (ret) {
484                 dev_err(dev, "failed to initialize host\n");
485                 return ret;
486         }
487
488         return 0;
489 }
490
491 static const struct dw_pcie_ops dw_pcie_ops = {
492         .cpu_addr_fixup = dra7xx_pcie_cpu_addr_fixup,
493         .start_link = dra7xx_pcie_establish_link,
494         .stop_link = dra7xx_pcie_stop_link,
495         .link_up = dra7xx_pcie_link_up,
496 };
497
498 static void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx)
499 {
500         int phy_count = dra7xx->phy_count;
501
502         while (phy_count--) {
503                 phy_power_off(dra7xx->phy[phy_count]);
504                 phy_exit(dra7xx->phy[phy_count]);
505         }
506 }
507
508 static int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx)
509 {
510         int phy_count = dra7xx->phy_count;
511         int ret;
512         int i;
513
514         for (i = 0; i < phy_count; i++) {
515                 ret = phy_init(dra7xx->phy[i]);
516                 if (ret < 0)
517                         goto err_phy;
518
519                 ret = phy_power_on(dra7xx->phy[i]);
520                 if (ret < 0) {
521                         phy_exit(dra7xx->phy[i]);
522                         goto err_phy;
523                 }
524         }
525
526         return 0;
527
528 err_phy:
529         while (--i >= 0) {
530                 phy_power_off(dra7xx->phy[i]);
531                 phy_exit(dra7xx->phy[i]);
532         }
533
534         return ret;
535 }
536
537 static const struct dra7xx_pcie_of_data dra7xx_pcie_rc_of_data = {
538         .mode = DW_PCIE_RC_TYPE,
539 };
540
541 static const struct dra7xx_pcie_of_data dra7xx_pcie_ep_of_data = {
542         .mode = DW_PCIE_EP_TYPE,
543 };
544
545 static const struct of_device_id of_dra7xx_pcie_match[] = {
546         {
547                 .compatible = "ti,dra7-pcie",
548                 .data = &dra7xx_pcie_rc_of_data,
549         },
550         {
551                 .compatible = "ti,dra7-pcie-ep",
552                 .data = &dra7xx_pcie_ep_of_data,
553         },
554         {},
555 };
556
557 /*
558  * dra7xx_pcie_unaligned_memaccess: workaround for AM572x/AM571x Errata i870
559  * @dra7xx: the dra7xx device where the workaround should be applied
560  *
561  * Access to the PCIe slave port that are not 32-bit aligned will result
562  * in incorrect mapping to TLP Address and Byte enable fields. Therefore,
563  * byte and half-word accesses are not possible to byte offset 0x1, 0x2, or
564  * 0x3.
565  *
566  * To avoid this issue set PCIE_SS1_AXI2OCP_LEGACY_MODE_ENABLE to 1.
567  */
568 static int dra7xx_pcie_unaligned_memaccess(struct device *dev)
569 {
570         int ret;
571         struct device_node *np = dev->of_node;
572         struct of_phandle_args args;
573         struct regmap *regmap;
574
575         regmap = syscon_regmap_lookup_by_phandle(np,
576                                                  "ti,syscon-unaligned-access");
577         if (IS_ERR(regmap)) {
578                 dev_dbg(dev, "can't get ti,syscon-unaligned-access\n");
579                 return -EINVAL;
580         }
581
582         ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-unaligned-access",
583                                                2, 0, &args);
584         if (ret) {
585                 dev_err(dev, "failed to parse ti,syscon-unaligned-access\n");
586                 return ret;
587         }
588
589         ret = regmap_update_bits(regmap, args.args[0], args.args[1],
590                                  args.args[1]);
591         if (ret)
592                 dev_err(dev, "failed to enable unaligned access\n");
593
594         of_node_put(args.np);
595
596         return ret;
597 }
598
599 static int __init dra7xx_pcie_probe(struct platform_device *pdev)
600 {
601         u32 reg;
602         int ret;
603         int irq;
604         int i;
605         int phy_count;
606         struct phy **phy;
607         struct device_link **link;
608         void __iomem *base;
609         struct resource *res;
610         struct dw_pcie *pci;
611         struct dra7xx_pcie *dra7xx;
612         struct device *dev = &pdev->dev;
613         struct device_node *np = dev->of_node;
614         char name[10];
615         struct gpio_desc *reset;
616         const struct of_device_id *match;
617         const struct dra7xx_pcie_of_data *data;
618         enum dw_pcie_device_mode mode;
619
620         match = of_match_device(of_match_ptr(of_dra7xx_pcie_match), dev);
621         if (!match)
622                 return -EINVAL;
623
624         data = (struct dra7xx_pcie_of_data *)match->data;
625         mode = (enum dw_pcie_device_mode)data->mode;
626
627         dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
628         if (!dra7xx)
629                 return -ENOMEM;
630
631         pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
632         if (!pci)
633                 return -ENOMEM;
634
635         pci->dev = dev;
636         pci->ops = &dw_pcie_ops;
637
638         irq = platform_get_irq(pdev, 0);
639         if (irq < 0) {
640                 dev_err(dev, "missing IRQ resource: %d\n", irq);
641                 return irq;
642         }
643
644         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ti_conf");
645         base = devm_ioremap_nocache(dev, res->start, resource_size(res));
646         if (!base)
647                 return -ENOMEM;
648
649         phy_count = of_property_count_strings(np, "phy-names");
650         if (phy_count < 0) {
651                 dev_err(dev, "unable to find the strings\n");
652                 return phy_count;
653         }
654
655         phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
656         if (!phy)
657                 return -ENOMEM;
658
659         link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
660         if (!link)
661                 return -ENOMEM;
662
663         for (i = 0; i < phy_count; i++) {
664                 snprintf(name, sizeof(name), "pcie-phy%d", i);
665                 phy[i] = devm_phy_get(dev, name);
666                 if (IS_ERR(phy[i]))
667                         return PTR_ERR(phy[i]);
668
669                 link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
670                 if (!link[i]) {
671                         ret = -EINVAL;
672                         goto err_link;
673                 }
674         }
675
676         dra7xx->base = base;
677         dra7xx->phy = phy;
678         dra7xx->pci = pci;
679         dra7xx->phy_count = phy_count;
680
681         ret = dra7xx_pcie_enable_phy(dra7xx);
682         if (ret) {
683                 dev_err(dev, "failed to enable phy\n");
684                 return ret;
685         }
686
687         platform_set_drvdata(pdev, dra7xx);
688
689         pm_runtime_enable(dev);
690         ret = pm_runtime_get_sync(dev);
691         if (ret < 0) {
692                 dev_err(dev, "pm_runtime_get_sync failed\n");
693                 goto err_get_sync;
694         }
695
696         reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
697         if (IS_ERR(reset)) {
698                 ret = PTR_ERR(reset);
699                 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
700                 goto err_gpio;
701         }
702
703         reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
704         reg &= ~LTSSM_EN;
705         dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
706
707         dra7xx->link_gen = of_pci_get_max_link_speed(np);
708         if (dra7xx->link_gen < 0 || dra7xx->link_gen > 2)
709                 dra7xx->link_gen = 2;
710
711         switch (mode) {
712         case DW_PCIE_RC_TYPE:
713                 if (!IS_ENABLED(CONFIG_PCI_DRA7XX_HOST)) {
714                         ret = -ENODEV;
715                         goto err_gpio;
716                 }
717
718                 dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
719                                    DEVICE_TYPE_RC);
720
721                 ret = dra7xx_pcie_unaligned_memaccess(dev);
722                 if (ret)
723                         dev_err(dev, "WA for Errata i870 not applied\n");
724
725                 ret = dra7xx_add_pcie_port(dra7xx, pdev);
726                 if (ret < 0)
727                         goto err_gpio;
728                 break;
729         case DW_PCIE_EP_TYPE:
730                 if (!IS_ENABLED(CONFIG_PCI_DRA7XX_EP)) {
731                         ret = -ENODEV;
732                         goto err_gpio;
733                 }
734
735                 dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
736                                    DEVICE_TYPE_EP);
737
738                 ret = dra7xx_pcie_unaligned_memaccess(dev);
739                 if (ret)
740                         goto err_gpio;
741
742                 ret = dra7xx_add_pcie_ep(dra7xx, pdev);
743                 if (ret < 0)
744                         goto err_gpio;
745                 break;
746         default:
747                 dev_err(dev, "INVALID device type %d\n", mode);
748         }
749         dra7xx->mode = mode;
750
751         ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
752                                IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
753         if (ret) {
754                 dev_err(dev, "failed to request irq\n");
755                 goto err_gpio;
756         }
757
758         return 0;
759
760 err_gpio:
761         pm_runtime_put(dev);
762
763 err_get_sync:
764         pm_runtime_disable(dev);
765         dra7xx_pcie_disable_phy(dra7xx);
766
767 err_link:
768         while (--i >= 0)
769                 device_link_del(link[i]);
770
771         return ret;
772 }
773
774 #ifdef CONFIG_PM_SLEEP
775 static int dra7xx_pcie_suspend(struct device *dev)
776 {
777         struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
778         struct dw_pcie *pci = dra7xx->pci;
779         u32 val;
780
781         if (dra7xx->mode != DW_PCIE_RC_TYPE)
782                 return 0;
783
784         /* clear MSE */
785         val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
786         val &= ~PCI_COMMAND_MEMORY;
787         dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
788
789         return 0;
790 }
791
792 static int dra7xx_pcie_resume(struct device *dev)
793 {
794         struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
795         struct dw_pcie *pci = dra7xx->pci;
796         u32 val;
797
798         if (dra7xx->mode != DW_PCIE_RC_TYPE)
799                 return 0;
800
801         /* set MSE */
802         val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
803         val |= PCI_COMMAND_MEMORY;
804         dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
805
806         return 0;
807 }
808
809 static int dra7xx_pcie_suspend_noirq(struct device *dev)
810 {
811         struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
812
813         dra7xx_pcie_disable_phy(dra7xx);
814
815         return 0;
816 }
817
818 static int dra7xx_pcie_resume_noirq(struct device *dev)
819 {
820         struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
821         int ret;
822
823         ret = dra7xx_pcie_enable_phy(dra7xx);
824         if (ret) {
825                 dev_err(dev, "failed to enable phy\n");
826                 return ret;
827         }
828
829         return 0;
830 }
831 #endif
832
833 static void dra7xx_pcie_shutdown(struct platform_device *pdev)
834 {
835         struct device *dev = &pdev->dev;
836         struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
837         int ret;
838
839         dra7xx_pcie_stop_link(dra7xx->pci);
840
841         ret = pm_runtime_put_sync(dev);
842         if (ret < 0)
843                 dev_dbg(dev, "pm_runtime_put_sync failed\n");
844
845         pm_runtime_disable(dev);
846         dra7xx_pcie_disable_phy(dra7xx);
847 }
848
849 static const struct dev_pm_ops dra7xx_pcie_pm_ops = {
850         SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
851         SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
852                                       dra7xx_pcie_resume_noirq)
853 };
854
855 static struct platform_driver dra7xx_pcie_driver = {
856         .driver = {
857                 .name   = "dra7-pcie",
858                 .of_match_table = of_dra7xx_pcie_match,
859                 .suppress_bind_attrs = true,
860                 .pm     = &dra7xx_pcie_pm_ops,
861         },
862         .shutdown = dra7xx_pcie_shutdown,
863 };
864 builtin_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);