]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/pci/controller/dwc/pci-keystone.c
1f14de0ef27f1186301ceb9f8f9cfad8829d707e
[linux.git] / drivers / pci / controller / dwc / pci-keystone.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCIe host controller driver for Texas Instruments Keystone SoCs
4  *
5  * Copyright (C) 2013-2014 Texas Instruments., Ltd.
6  *              http://www.ti.com
7  *
8  * Author: Murali Karicheri <m-karicheri2@ti.com>
9  * Implementation based on pci-exynos.c and pcie-designware.c
10  */
11
12 #include <linux/irqchip/chained_irq.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqdomain.h>
17 #include <linux/init.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/msi.h>
20 #include <linux/of_irq.h>
21 #include <linux/of.h>
22 #include <linux/of_pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/phy/phy.h>
25 #include <linux/regmap.h>
26 #include <linux/resource.h>
27 #include <linux/signal.h>
28
29 #include "pcie-designware.h"
30
31 #define DRIVER_NAME     "keystone-pcie"
32
33 #define PCIE_VENDORID_MASK      0xffff
34 #define PCIE_DEVICEID_SHIFT     16
35
36 /* DEV_STAT_CTRL */
37 #define PCIE_CAP_BASE           0x70
38
39 /* Application register defines */
40 #define LTSSM_EN_VAL                    BIT(0)
41 #define LTSSM_STATE_MASK                0x1f
42 #define LTSSM_STATE_L0                  0x11
43 #define DBI_CS2_EN_VAL                  0x20
44 #define OB_XLAT_EN_VAL                  2
45
46 /* Application registers */
47 #define CMD_STATUS                      0x004
48
49 #define CFG_SETUP                       0x008
50 #define CFG_BUS(x)                      (((x) & 0xff) << 16)
51 #define CFG_DEVICE(x)                   (((x) & 0x1f) << 8)
52 #define CFG_FUNC(x)                     ((x) & 0x7)
53 #define CFG_TYPE1                       BIT(24)
54
55 #define OB_SIZE                         0x030
56 #define CFG_PCIM_WIN_SZ_IDX             3
57 #define CFG_PCIM_WIN_CNT                32
58 #define SPACE0_REMOTE_CFG_OFFSET        0x1000
59 #define OB_OFFSET_INDEX(n)              (0x200 + (8 * (n)))
60 #define OB_OFFSET_HI(n)                 (0x204 + (8 * (n)))
61
62 /* IRQ register defines */
63 #define IRQ_EOI                         0x050
64 #define IRQ_STATUS                      0x184
65 #define IRQ_ENABLE_SET                  0x188
66 #define IRQ_ENABLE_CLR                  0x18c
67
68 #define MSI_IRQ                         0x054
69 #define MSI0_IRQ_STATUS                 0x104
70 #define MSI0_IRQ_ENABLE_SET             0x108
71 #define MSI0_IRQ_ENABLE_CLR             0x10c
72 #define IRQ_STATUS                      0x184
73 #define MSI_IRQ_OFFSET                  4
74
75 /* Error IRQ bits */
76 #define ERR_AER         BIT(5)  /* ECRC error */
77 #define ERR_AXI         BIT(4)  /* AXI tag lookup fatal error */
78 #define ERR_CORR        BIT(3)  /* Correctable error */
79 #define ERR_NONFATAL    BIT(2)  /* Non-fatal error */
80 #define ERR_FATAL       BIT(1)  /* Fatal error */
81 #define ERR_SYS         BIT(0)  /* System (fatal, non-fatal, or correctable) */
82 #define ERR_IRQ_ALL     (ERR_AER | ERR_AXI | ERR_CORR | \
83                          ERR_NONFATAL | ERR_FATAL | ERR_SYS)
84 #define ERR_FATAL_IRQ   (ERR_FATAL | ERR_AXI)
85 #define ERR_IRQ_STATUS_RAW              0x1c0
86 #define ERR_IRQ_STATUS                  0x1c4
87 #define ERR_IRQ_ENABLE_SET              0x1c8
88 #define ERR_IRQ_ENABLE_CLR              0x1cc
89
90 /* Config space registers */
91 #define DEBUG0                          0x728
92
93 #define MAX_MSI_HOST_IRQS               8
94
95 /* PCIE controller device IDs */
96 #define PCIE_RC_K2HK            0xb008
97 #define PCIE_RC_K2E             0xb009
98 #define PCIE_RC_K2L             0xb00a
99 #define PCIE_RC_K2G             0xb00b
100
101 #define to_keystone_pcie(x)     dev_get_drvdata((x)->dev)
102
103 struct keystone_pcie {
104         struct dw_pcie          *pci;
105         /* PCI Device ID */
106         u32                     device_id;
107         int                     num_legacy_host_irqs;
108         int                     legacy_host_irqs[PCI_NUM_INTX];
109         struct                  device_node *legacy_intc_np;
110
111         int                     num_msi_host_irqs;
112         int                     msi_host_irqs[MAX_MSI_HOST_IRQS];
113         int                     num_lanes;
114         struct phy              **phy;
115         struct device_link      **link;
116         struct                  device_node *msi_intc_np;
117         struct irq_domain       *legacy_irq_domain;
118         struct device_node      *np;
119
120         int error_irq;
121
122         /* Application register space */
123         void __iomem            *va_app_base;   /* DT 1st resource */
124         struct resource         app;
125 };
126
127 static inline void update_reg_offset_bit_pos(u32 offset, u32 *reg_offset,
128                                              u32 *bit_pos)
129 {
130         *reg_offset = offset % 8;
131         *bit_pos = offset >> 3;
132 }
133
134 static phys_addr_t ks_pcie_get_msi_addr(struct pcie_port *pp)
135 {
136         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
137         struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
138
139         return ks_pcie->app.start + MSI_IRQ;
140 }
141
142 static u32 ks_pcie_app_readl(struct keystone_pcie *ks_pcie, u32 offset)
143 {
144         return readl(ks_pcie->va_app_base + offset);
145 }
146
147 static void ks_pcie_app_writel(struct keystone_pcie *ks_pcie, u32 offset,
148                                u32 val)
149 {
150         writel(val, ks_pcie->va_app_base + offset);
151 }
152
153 static void ks_pcie_handle_msi_irq(struct keystone_pcie *ks_pcie, int offset)
154 {
155         struct dw_pcie *pci = ks_pcie->pci;
156         struct pcie_port *pp = &pci->pp;
157         struct device *dev = pci->dev;
158         u32 pending, vector;
159         int src, virq;
160
161         pending = ks_pcie_app_readl(ks_pcie, MSI0_IRQ_STATUS + (offset << 4));
162
163         /*
164          * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
165          * shows 1, 9, 17, 25 and so forth
166          */
167         for (src = 0; src < 4; src++) {
168                 if (BIT(src) & pending) {
169                         vector = offset + (src << 3);
170                         virq = irq_linear_revmap(pp->irq_domain, vector);
171                         dev_dbg(dev, "irq: bit %d, vector %d, virq %d\n",
172                                 src, vector, virq);
173                         generic_handle_irq(virq);
174                 }
175         }
176 }
177
178 static void ks_pcie_msi_irq_ack(int irq, struct pcie_port *pp)
179 {
180         u32 reg_offset, bit_pos;
181         struct keystone_pcie *ks_pcie;
182         struct dw_pcie *pci;
183
184         pci = to_dw_pcie_from_pp(pp);
185         ks_pcie = to_keystone_pcie(pci);
186         update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
187
188         ks_pcie_app_writel(ks_pcie, MSI0_IRQ_STATUS + (reg_offset << 4),
189                            BIT(bit_pos));
190         ks_pcie_app_writel(ks_pcie, IRQ_EOI, reg_offset + MSI_IRQ_OFFSET);
191 }
192
193 static void ks_pcie_msi_set_irq(struct pcie_port *pp, int irq)
194 {
195         u32 reg_offset, bit_pos;
196         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
197         struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
198
199         update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
200         ks_pcie_app_writel(ks_pcie, MSI0_IRQ_ENABLE_SET + (reg_offset << 4),
201                            BIT(bit_pos));
202 }
203
204 static void ks_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
205 {
206         u32 reg_offset, bit_pos;
207         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
208         struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
209
210         update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
211         ks_pcie_app_writel(ks_pcie, MSI0_IRQ_ENABLE_CLR + (reg_offset << 4),
212                            BIT(bit_pos));
213 }
214
215 static int ks_pcie_msi_host_init(struct pcie_port *pp)
216 {
217         return dw_pcie_allocate_domains(pp);
218 }
219
220 static void ks_pcie_enable_legacy_irqs(struct keystone_pcie *ks_pcie)
221 {
222         int i;
223
224         for (i = 0; i < PCI_NUM_INTX; i++)
225                 ks_pcie_app_writel(ks_pcie, IRQ_ENABLE_SET + (i << 4), 0x1);
226 }
227
228 static void ks_pcie_handle_legacy_irq(struct keystone_pcie *ks_pcie,
229                                       int offset)
230 {
231         struct dw_pcie *pci = ks_pcie->pci;
232         struct device *dev = pci->dev;
233         u32 pending;
234         int virq;
235
236         pending = ks_pcie_app_readl(ks_pcie, IRQ_STATUS + (offset << 4));
237
238         if (BIT(0) & pending) {
239                 virq = irq_linear_revmap(ks_pcie->legacy_irq_domain, offset);
240                 dev_dbg(dev, ": irq: irq_offset %d, virq %d\n", offset, virq);
241                 generic_handle_irq(virq);
242         }
243
244         /* EOI the INTx interrupt */
245         ks_pcie_app_writel(ks_pcie, IRQ_EOI, offset);
246 }
247
248 static void ks_pcie_enable_error_irq(struct keystone_pcie *ks_pcie)
249 {
250         ks_pcie_app_writel(ks_pcie, ERR_IRQ_ENABLE_SET, ERR_IRQ_ALL);
251 }
252
253 static irqreturn_t ks_pcie_handle_error_irq(struct keystone_pcie *ks_pcie)
254 {
255         u32 status;
256
257         status = ks_pcie_app_readl(ks_pcie, ERR_IRQ_STATUS_RAW) & ERR_IRQ_ALL;
258         if (!status)
259                 return IRQ_NONE;
260
261         if (status & ERR_FATAL_IRQ)
262                 dev_err(ks_pcie->pci->dev, "fatal error (status %#010x)\n",
263                         status);
264
265         /* Ack the IRQ; status bits are RW1C */
266         ks_pcie_app_writel(ks_pcie, ERR_IRQ_STATUS, status);
267         return IRQ_HANDLED;
268 }
269
270 static void ks_pcie_ack_legacy_irq(struct irq_data *d)
271 {
272 }
273
274 static void ks_pcie_mask_legacy_irq(struct irq_data *d)
275 {
276 }
277
278 static void ks_pcie_unmask_legacy_irq(struct irq_data *d)
279 {
280 }
281
282 static struct irq_chip ks_pcie_legacy_irq_chip = {
283         .name = "Keystone-PCI-Legacy-IRQ",
284         .irq_ack = ks_pcie_ack_legacy_irq,
285         .irq_mask = ks_pcie_mask_legacy_irq,
286         .irq_unmask = ks_pcie_unmask_legacy_irq,
287 };
288
289 static int ks_pcie_init_legacy_irq_map(struct irq_domain *d,
290                                        unsigned int irq,
291                                        irq_hw_number_t hw_irq)
292 {
293         irq_set_chip_and_handler(irq, &ks_pcie_legacy_irq_chip,
294                                  handle_level_irq);
295         irq_set_chip_data(irq, d->host_data);
296
297         return 0;
298 }
299
300 static const struct irq_domain_ops ks_pcie_legacy_irq_domain_ops = {
301         .map = ks_pcie_init_legacy_irq_map,
302         .xlate = irq_domain_xlate_onetwocell,
303 };
304
305 /**
306  * ks_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
307  * registers
308  *
309  * Since modification of dbi_cs2 involves different clock domain, read the
310  * status back to ensure the transition is complete.
311  */
312 static void ks_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie)
313 {
314         u32 val;
315
316         val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
317         ks_pcie_app_writel(ks_pcie, CMD_STATUS, DBI_CS2_EN_VAL | val);
318
319         do {
320                 val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
321         } while (!(val & DBI_CS2_EN_VAL));
322 }
323
324 /**
325  * ks_pcie_clear_dbi_mode() - Disable DBI mode
326  *
327  * Since modification of dbi_cs2 involves different clock domain, read the
328  * status back to ensure the transition is complete.
329  */
330 static void ks_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie)
331 {
332         u32 val;
333
334         val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
335         ks_pcie_app_writel(ks_pcie, CMD_STATUS, ~DBI_CS2_EN_VAL & val);
336
337         do {
338                 val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
339         } while (val & DBI_CS2_EN_VAL);
340 }
341
342 static void ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie)
343 {
344         struct dw_pcie *pci = ks_pcie->pci;
345         struct pcie_port *pp = &pci->pp;
346         u32 start = pp->mem->start, end = pp->mem->end;
347         int i, tr_size;
348         u32 val;
349
350         /* Disable BARs for inbound access */
351         ks_pcie_set_dbi_mode(ks_pcie);
352         dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
353         dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0);
354         ks_pcie_clear_dbi_mode(ks_pcie);
355
356         /* Set outbound translation size per window division */
357         ks_pcie_app_writel(ks_pcie, OB_SIZE, CFG_PCIM_WIN_SZ_IDX & 0x7);
358
359         tr_size = (1 << (CFG_PCIM_WIN_SZ_IDX & 0x7)) * SZ_1M;
360
361         /* Using Direct 1:1 mapping of RC <-> PCI memory space */
362         for (i = 0; (i < CFG_PCIM_WIN_CNT) && (start < end); i++) {
363                 ks_pcie_app_writel(ks_pcie, OB_OFFSET_INDEX(i), start | 1);
364                 ks_pcie_app_writel(ks_pcie, OB_OFFSET_HI(i), 0);
365                 start += tr_size;
366         }
367
368         /* Enable OB translation */
369         val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
370         ks_pcie_app_writel(ks_pcie, CMD_STATUS, OB_XLAT_EN_VAL | val);
371 }
372
373 static int ks_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
374                                  unsigned int devfn, int where, int size,
375                                  u32 *val)
376 {
377         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
378         struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
379         u32 reg;
380
381         reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) |
382                 CFG_FUNC(PCI_FUNC(devfn));
383         if (bus->parent->number != pp->root_bus_nr)
384                 reg |= CFG_TYPE1;
385         ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg);
386
387         return dw_pcie_read(pp->va_cfg0_base + where, size, val);
388 }
389
390 static int ks_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
391                                  unsigned int devfn, int where, int size,
392                                  u32 val)
393 {
394         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
395         struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
396         u32 reg;
397
398         reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) |
399                 CFG_FUNC(PCI_FUNC(devfn));
400         if (bus->parent->number != pp->root_bus_nr)
401                 reg |= CFG_TYPE1;
402         ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg);
403
404         return dw_pcie_write(pp->va_cfg0_base + where, size, val);
405 }
406
407 /**
408  * ks_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
409  *
410  * This sets BAR0 to enable inbound access for MSI_IRQ register
411  */
412 static void ks_pcie_v3_65_scan_bus(struct pcie_port *pp)
413 {
414         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
415         struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
416
417         /* Configure and set up BAR0 */
418         ks_pcie_set_dbi_mode(ks_pcie);
419
420         /* Enable BAR0 */
421         dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 1);
422         dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, SZ_4K - 1);
423
424         ks_pcie_clear_dbi_mode(ks_pcie);
425
426          /*
427           * For BAR0, just setting bus address for inbound writes (MSI) should
428           * be sufficient.  Use physical address to avoid any conflicts.
429           */
430         dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, ks_pcie->app.start);
431 }
432
433 /**
434  * ks_pcie_link_up() - Check if link up
435  */
436 static int ks_pcie_link_up(struct dw_pcie *pci)
437 {
438         u32 val;
439
440         val = dw_pcie_readl_dbi(pci, DEBUG0);
441         return (val & LTSSM_STATE_MASK) == LTSSM_STATE_L0;
442 }
443
444 static void ks_pcie_initiate_link_train(struct keystone_pcie *ks_pcie)
445 {
446         u32 val;
447
448         /* Disable Link training */
449         val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
450         val &= ~LTSSM_EN_VAL;
451         ks_pcie_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
452
453         /* Initiate Link Training */
454         val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
455         ks_pcie_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
456 }
457
458 /**
459  * ks_pcie_dw_host_init() - initialize host for v3_65 dw hardware
460  *
461  * Ioremap the register resources, initialize legacy irq domain
462  * and call dw_pcie_v3_65_host_init() API to initialize the Keystone
463  * PCI host controller.
464  */
465 static int __init ks_pcie_dw_host_init(struct keystone_pcie *ks_pcie)
466 {
467         struct dw_pcie *pci = ks_pcie->pci;
468         struct pcie_port *pp = &pci->pp;
469         struct device *dev = pci->dev;
470         struct platform_device *pdev = to_platform_device(dev);
471         struct resource *res;
472
473         /* Index 0 is the config reg. space address */
474         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
475         pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
476         if (IS_ERR(pci->dbi_base))
477                 return PTR_ERR(pci->dbi_base);
478
479         /*
480          * We set these same and is used in pcie rd/wr_other_conf
481          * functions
482          */
483         pp->va_cfg0_base = pci->dbi_base + SPACE0_REMOTE_CFG_OFFSET;
484         pp->va_cfg1_base = pp->va_cfg0_base;
485
486         /* Index 1 is the application reg. space address */
487         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
488         ks_pcie->va_app_base = devm_ioremap_resource(dev, res);
489         if (IS_ERR(ks_pcie->va_app_base))
490                 return PTR_ERR(ks_pcie->va_app_base);
491
492         ks_pcie->app = *res;
493
494         /* Create legacy IRQ domain */
495         ks_pcie->legacy_irq_domain =
496                         irq_domain_add_linear(ks_pcie->legacy_intc_np,
497                                               PCI_NUM_INTX,
498                                               &ks_pcie_legacy_irq_domain_ops,
499                                               NULL);
500         if (!ks_pcie->legacy_irq_domain) {
501                 dev_err(dev, "Failed to add irq domain for legacy irqs\n");
502                 return -EINVAL;
503         }
504
505         return dw_pcie_host_init(pp);
506 }
507
508 static void ks_pcie_quirk(struct pci_dev *dev)
509 {
510         struct pci_bus *bus = dev->bus;
511         struct pci_dev *bridge;
512         static const struct pci_device_id rc_pci_devids[] = {
513                 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
514                  .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
515                 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
516                  .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
517                 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
518                  .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
519                 { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2G),
520                  .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
521                 { 0, },
522         };
523
524         if (pci_is_root_bus(bus))
525                 bridge = dev;
526
527         /* look for the host bridge */
528         while (!pci_is_root_bus(bus)) {
529                 bridge = bus->self;
530                 bus = bus->parent;
531         }
532
533         if (!bridge)
534                 return;
535
536         /*
537          * Keystone PCI controller has a h/w limitation of
538          * 256 bytes maximum read request size.  It can't handle
539          * anything higher than this.  So force this limit on
540          * all downstream devices.
541          */
542         if (pci_match_id(rc_pci_devids, bridge)) {
543                 if (pcie_get_readrq(dev) > 256) {
544                         dev_info(&dev->dev, "limiting MRRS to 256\n");
545                         pcie_set_readrq(dev, 256);
546                 }
547         }
548 }
549 DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, ks_pcie_quirk);
550
551 static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
552 {
553         struct dw_pcie *pci = ks_pcie->pci;
554         struct device *dev = pci->dev;
555
556         if (dw_pcie_link_up(pci)) {
557                 dev_info(dev, "Link already up\n");
558                 return 0;
559         }
560
561         ks_pcie_initiate_link_train(ks_pcie);
562
563         /* check if the link is up or not */
564         if (!dw_pcie_wait_for_link(pci))
565                 return 0;
566
567         dev_err(dev, "phy link never came up\n");
568         return -ETIMEDOUT;
569 }
570
571 static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
572 {
573         unsigned int irq = irq_desc_get_irq(desc);
574         struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
575         u32 offset = irq - ks_pcie->msi_host_irqs[0];
576         struct dw_pcie *pci = ks_pcie->pci;
577         struct device *dev = pci->dev;
578         struct irq_chip *chip = irq_desc_get_chip(desc);
579
580         dev_dbg(dev, "%s, irq %d\n", __func__, irq);
581
582         /*
583          * The chained irq handler installation would have replaced normal
584          * interrupt driver handler so we need to take care of mask/unmask and
585          * ack operation.
586          */
587         chained_irq_enter(chip, desc);
588         ks_pcie_handle_msi_irq(ks_pcie, offset);
589         chained_irq_exit(chip, desc);
590 }
591
592 /**
593  * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
594  * @irq: IRQ line for legacy interrupts
595  * @desc: Pointer to irq descriptor
596  *
597  * Traverse through pending legacy interrupts and invoke handler for each. Also
598  * takes care of interrupt controller level mask/ack operation.
599  */
600 static void ks_pcie_legacy_irq_handler(struct irq_desc *desc)
601 {
602         unsigned int irq = irq_desc_get_irq(desc);
603         struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
604         struct dw_pcie *pci = ks_pcie->pci;
605         struct device *dev = pci->dev;
606         u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
607         struct irq_chip *chip = irq_desc_get_chip(desc);
608
609         dev_dbg(dev, ": Handling legacy irq %d\n", irq);
610
611         /*
612          * The chained irq handler installation would have replaced normal
613          * interrupt driver handler so we need to take care of mask/unmask and
614          * ack operation.
615          */
616         chained_irq_enter(chip, desc);
617         ks_pcie_handle_legacy_irq(ks_pcie, irq_offset);
618         chained_irq_exit(chip, desc);
619 }
620
621 static int ks_pcie_get_irq_controller_info(struct keystone_pcie *ks_pcie,
622                                            char *controller, int *num_irqs)
623 {
624         int temp, max_host_irqs, legacy = 1, *host_irqs;
625         struct device *dev = ks_pcie->pci->dev;
626         struct device_node *np_pcie = dev->of_node, **np_temp;
627
628         if (!strcmp(controller, "msi-interrupt-controller"))
629                 legacy = 0;
630
631         if (legacy) {
632                 np_temp = &ks_pcie->legacy_intc_np;
633                 max_host_irqs = PCI_NUM_INTX;
634                 host_irqs = &ks_pcie->legacy_host_irqs[0];
635         } else {
636                 np_temp = &ks_pcie->msi_intc_np;
637                 max_host_irqs = MAX_MSI_HOST_IRQS;
638                 host_irqs =  &ks_pcie->msi_host_irqs[0];
639         }
640
641         /* interrupt controller is in a child node */
642         *np_temp = of_get_child_by_name(np_pcie, controller);
643         if (!(*np_temp)) {
644                 dev_err(dev, "Node for %s is absent\n", controller);
645                 return -EINVAL;
646         }
647
648         temp = of_irq_count(*np_temp);
649         if (!temp) {
650                 dev_err(dev, "No IRQ entries in %s\n", controller);
651                 of_node_put(*np_temp);
652                 return -EINVAL;
653         }
654
655         if (temp > max_host_irqs)
656                 dev_warn(dev, "Too many %s interrupts defined %u\n",
657                         (legacy ? "legacy" : "MSI"), temp);
658
659         /*
660          * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
661          * 7 (MSI)
662          */
663         for (temp = 0; temp < max_host_irqs; temp++) {
664                 host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
665                 if (!host_irqs[temp])
666                         break;
667         }
668
669         of_node_put(*np_temp);
670
671         if (temp) {
672                 *num_irqs = temp;
673                 return 0;
674         }
675
676         return -EINVAL;
677 }
678
679 static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
680 {
681         int i;
682
683         /* Legacy IRQ */
684         for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
685                 irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
686                                                  ks_pcie_legacy_irq_handler,
687                                                  ks_pcie);
688         }
689         ks_pcie_enable_legacy_irqs(ks_pcie);
690
691         /* MSI IRQ */
692         if (IS_ENABLED(CONFIG_PCI_MSI)) {
693                 for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
694                         irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
695                                                          ks_pcie_msi_irq_handler,
696                                                          ks_pcie);
697                 }
698         }
699
700         if (ks_pcie->error_irq > 0)
701                 ks_pcie_enable_error_irq(ks_pcie);
702 }
703
704 /*
705  * When a PCI device does not exist during config cycles, keystone host gets a
706  * bus error instead of returning 0xffffffff. This handler always returns 0
707  * for this kind of faults.
708  */
709 static int ks_pcie_fault(unsigned long addr, unsigned int fsr,
710                          struct pt_regs *regs)
711 {
712         unsigned long instr = *(unsigned long *) instruction_pointer(regs);
713
714         if ((instr & 0x0e100090) == 0x00100090) {
715                 int reg = (instr >> 12) & 15;
716
717                 regs->uregs[reg] = -1;
718                 regs->ARM_pc += 4;
719         }
720
721         return 0;
722 }
723
724 static int __init ks_pcie_init_id(struct keystone_pcie *ks_pcie)
725 {
726         int ret;
727         unsigned int id;
728         struct regmap *devctrl_regs;
729         struct dw_pcie *pci = ks_pcie->pci;
730         struct device *dev = pci->dev;
731         struct device_node *np = dev->of_node;
732
733         devctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-id");
734         if (IS_ERR(devctrl_regs))
735                 return PTR_ERR(devctrl_regs);
736
737         ret = regmap_read(devctrl_regs, 0, &id);
738         if (ret)
739                 return ret;
740
741         dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, id & PCIE_VENDORID_MASK);
742         dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, id >> PCIE_DEVICEID_SHIFT);
743
744         return 0;
745 }
746
747 static int __init ks_pcie_host_init(struct pcie_port *pp)
748 {
749         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
750         struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
751         int ret;
752
753         dw_pcie_setup_rc(pp);
754
755         ks_pcie_establish_link(ks_pcie);
756         ks_pcie_setup_rc_app_regs(ks_pcie);
757         ks_pcie_setup_interrupts(ks_pcie);
758         writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
759                         pci->dbi_base + PCI_IO_BASE);
760
761         ret = ks_pcie_init_id(ks_pcie);
762         if (ret < 0)
763                 return ret;
764
765         /*
766          * PCIe access errors that result into OCP errors are caught by ARM as
767          * "External aborts"
768          */
769         hook_fault_code(17, ks_pcie_fault, SIGBUS, 0,
770                         "Asynchronous external abort");
771
772         return 0;
773 }
774
775 static const struct dw_pcie_host_ops ks_pcie_host_ops = {
776         .rd_other_conf = ks_pcie_rd_other_conf,
777         .wr_other_conf = ks_pcie_wr_other_conf,
778         .host_init = ks_pcie_host_init,
779         .msi_set_irq = ks_pcie_msi_set_irq,
780         .msi_clear_irq = ks_pcie_msi_clear_irq,
781         .get_msi_addr = ks_pcie_get_msi_addr,
782         .msi_host_init = ks_pcie_msi_host_init,
783         .msi_irq_ack = ks_pcie_msi_irq_ack,
784         .scan_bus = ks_pcie_v3_65_scan_bus,
785 };
786
787 static irqreturn_t ks_pcie_err_irq_handler(int irq, void *priv)
788 {
789         struct keystone_pcie *ks_pcie = priv;
790
791         return ks_pcie_handle_error_irq(ks_pcie);
792 }
793
794 static int __init ks_pcie_add_pcie_port(struct keystone_pcie *ks_pcie,
795                                         struct platform_device *pdev)
796 {
797         struct dw_pcie *pci = ks_pcie->pci;
798         struct pcie_port *pp = &pci->pp;
799         struct device *dev = &pdev->dev;
800         int ret;
801
802         ret = ks_pcie_get_irq_controller_info(ks_pcie,
803                                         "legacy-interrupt-controller",
804                                         &ks_pcie->num_legacy_host_irqs);
805         if (ret)
806                 return ret;
807
808         if (IS_ENABLED(CONFIG_PCI_MSI)) {
809                 ret = ks_pcie_get_irq_controller_info(ks_pcie,
810                                                 "msi-interrupt-controller",
811                                                 &ks_pcie->num_msi_host_irqs);
812                 if (ret)
813                         return ret;
814         }
815
816         /*
817          * Index 0 is the platform interrupt for error interrupt
818          * from RC.  This is optional.
819          */
820         ks_pcie->error_irq = irq_of_parse_and_map(ks_pcie->np, 0);
821         if (ks_pcie->error_irq <= 0)
822                 dev_info(dev, "no error IRQ defined\n");
823         else {
824                 ret = request_irq(ks_pcie->error_irq, ks_pcie_err_irq_handler,
825                                   IRQF_SHARED, "pcie-error-irq", ks_pcie);
826                 if (ret < 0) {
827                         dev_err(dev, "failed to request error IRQ %d\n",
828                                 ks_pcie->error_irq);
829                         return ret;
830                 }
831         }
832
833         pp->ops = &ks_pcie_host_ops;
834         ret = ks_pcie_dw_host_init(ks_pcie);
835         if (ret) {
836                 dev_err(dev, "failed to initialize host\n");
837                 return ret;
838         }
839
840         return 0;
841 }
842
843 static const struct of_device_id ks_pcie_of_match[] = {
844         {
845                 .type = "pci",
846                 .compatible = "ti,keystone-pcie",
847         },
848         { },
849 };
850
851 static const struct dw_pcie_ops ks_pcie_dw_pcie_ops = {
852         .link_up = ks_pcie_link_up,
853 };
854
855 static void ks_pcie_disable_phy(struct keystone_pcie *ks_pcie)
856 {
857         int num_lanes = ks_pcie->num_lanes;
858
859         while (num_lanes--) {
860                 phy_power_off(ks_pcie->phy[num_lanes]);
861                 phy_exit(ks_pcie->phy[num_lanes]);
862         }
863 }
864
865 static int ks_pcie_enable_phy(struct keystone_pcie *ks_pcie)
866 {
867         int i;
868         int ret;
869         int num_lanes = ks_pcie->num_lanes;
870
871         for (i = 0; i < num_lanes; i++) {
872                 ret = phy_init(ks_pcie->phy[i]);
873                 if (ret < 0)
874                         goto err_phy;
875
876                 ret = phy_power_on(ks_pcie->phy[i]);
877                 if (ret < 0) {
878                         phy_exit(ks_pcie->phy[i]);
879                         goto err_phy;
880                 }
881         }
882
883         return 0;
884
885 err_phy:
886         while (--i >= 0) {
887                 phy_power_off(ks_pcie->phy[i]);
888                 phy_exit(ks_pcie->phy[i]);
889         }
890
891         return ret;
892 }
893
894 static int __init ks_pcie_probe(struct platform_device *pdev)
895 {
896         struct device *dev = &pdev->dev;
897         struct device_node *np = dev->of_node;
898         struct dw_pcie *pci;
899         struct keystone_pcie *ks_pcie;
900         struct device_link **link;
901         struct phy **phy;
902         u32 num_lanes;
903         char name[10];
904         int ret;
905         int i;
906
907         ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
908         if (!ks_pcie)
909                 return -ENOMEM;
910
911         pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
912         if (!pci)
913                 return -ENOMEM;
914
915         pci->dev = dev;
916         pci->ops = &ks_pcie_dw_pcie_ops;
917
918         ret = of_property_read_u32(np, "num-lanes", &num_lanes);
919         if (ret)
920                 num_lanes = 1;
921
922         phy = devm_kzalloc(dev, sizeof(*phy) * num_lanes, GFP_KERNEL);
923         if (!phy)
924                 return -ENOMEM;
925
926         link = devm_kzalloc(dev, sizeof(*link) * num_lanes, GFP_KERNEL);
927         if (!link)
928                 return -ENOMEM;
929
930         for (i = 0; i < num_lanes; i++) {
931                 snprintf(name, sizeof(name), "pcie-phy%d", i);
932                 phy[i] = devm_phy_optional_get(dev, name);
933                 if (IS_ERR(phy[i])) {
934                         ret = PTR_ERR(phy[i]);
935                         goto err_link;
936                 }
937
938                 if (!phy[i])
939                         continue;
940
941                 link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
942                 if (!link[i]) {
943                         ret = -EINVAL;
944                         goto err_link;
945                 }
946         }
947
948         ks_pcie->np = np;
949         ks_pcie->pci = pci;
950         ks_pcie->link = link;
951         ks_pcie->num_lanes = num_lanes;
952         ks_pcie->phy = phy;
953
954         ret = ks_pcie_enable_phy(ks_pcie);
955         if (ret) {
956                 dev_err(dev, "failed to enable phy\n");
957                 goto err_link;
958         }
959
960         platform_set_drvdata(pdev, ks_pcie);
961         pm_runtime_enable(dev);
962         ret = pm_runtime_get_sync(dev);
963         if (ret < 0) {
964                 dev_err(dev, "pm_runtime_get_sync failed\n");
965                 goto err_get_sync;
966         }
967
968         ret = ks_pcie_add_pcie_port(ks_pcie, pdev);
969         if (ret < 0)
970                 goto err_get_sync;
971
972         return 0;
973
974 err_get_sync:
975         pm_runtime_put(dev);
976         pm_runtime_disable(dev);
977         ks_pcie_disable_phy(ks_pcie);
978
979 err_link:
980         while (--i >= 0 && link[i])
981                 device_link_del(link[i]);
982
983         return ret;
984 }
985
986 static int __exit ks_pcie_remove(struct platform_device *pdev)
987 {
988         struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
989         struct device_link **link = ks_pcie->link;
990         int num_lanes = ks_pcie->num_lanes;
991         struct device *dev = &pdev->dev;
992
993         pm_runtime_put(dev);
994         pm_runtime_disable(dev);
995         ks_pcie_disable_phy(ks_pcie);
996         while (num_lanes--)
997                 device_link_del(link[num_lanes]);
998
999         return 0;
1000 }
1001
1002 static struct platform_driver ks_pcie_driver __refdata = {
1003         .probe  = ks_pcie_probe,
1004         .remove = __exit_p(ks_pcie_remove),
1005         .driver = {
1006                 .name   = "keystone-pcie",
1007                 .of_match_table = of_match_ptr(ks_pcie_of_match),
1008         },
1009 };
1010 builtin_platform_driver(ks_pcie_driver);