Merge tag 'efi-2019-07-rc5-3' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[platform/kernel/u-boot.git] / drivers / pci / pcie_fsl.c
1 // SPDX-License-Identifier: GPL-2.0+ OR X11
2 /*
3  * Copyright 2019 NXP
4  *
5  * PCIe DM U-Boot driver for Freescale PowerPC SoCs
6  * Author: Hou Zhiqiang <Zhiqiang.Hou@nxp.com>
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <pci.h>
14 #include <asm/fsl_pci.h>
15 #include <asm/fsl_serdes.h>
16 #include <asm/io.h>
17 #include "pcie_fsl.h"
18
19 LIST_HEAD(fsl_pcie_list);
20
21 static int fsl_pcie_link_up(struct fsl_pcie *pcie);
22
23 static int fsl_pcie_addr_valid(struct fsl_pcie *pcie, pci_dev_t bdf)
24 {
25         struct udevice *bus = pcie->bus;
26
27         if (!pcie->enabled)
28                 return -ENXIO;
29
30         if (PCI_BUS(bdf) < bus->seq)
31                 return -EINVAL;
32
33         if (PCI_BUS(bdf) > bus->seq && (!fsl_pcie_link_up(pcie) || pcie->mode))
34                 return -EINVAL;
35
36         if (PCI_BUS(bdf) == bus->seq && (PCI_DEV(bdf) > 0 || PCI_FUNC(bdf) > 0))
37                 return -EINVAL;
38
39         if (PCI_BUS(bdf) == (bus->seq + 1) && (PCI_DEV(bdf) > 0))
40                 return -EINVAL;
41
42         return 0;
43 }
44
45 static int fsl_pcie_read_config(struct udevice *bus, pci_dev_t bdf,
46                                 uint offset, ulong *valuep,
47                                 enum pci_size_t size)
48 {
49         struct fsl_pcie *pcie = dev_get_priv(bus);
50         ccsr_fsl_pci_t *regs = pcie->regs;
51         u32 val;
52
53         if (fsl_pcie_addr_valid(pcie, bdf)) {
54                 *valuep = pci_get_ff(size);
55                 return 0;
56         }
57
58         bdf = bdf - PCI_BDF(bus->seq, 0, 0);
59         val = bdf | (offset & 0xfc) | ((offset & 0xf00) << 16) | 0x80000000;
60         out_be32(&regs->cfg_addr, val);
61
62         sync();
63
64         switch (size) {
65         case PCI_SIZE_8:
66                 *valuep = in_8((u8 *)&regs->cfg_data + (offset & 3));
67                 break;
68         case PCI_SIZE_16:
69                 *valuep = in_le16((u16 *)((u8 *)&regs->cfg_data +
70                           (offset & 2)));
71                 break;
72         case PCI_SIZE_32:
73                 *valuep = in_le32(&regs->cfg_data);
74                 break;
75         }
76
77         return 0;
78 }
79
80 static int fsl_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
81                                  uint offset, ulong value,
82                                  enum pci_size_t size)
83 {
84         struct fsl_pcie *pcie = dev_get_priv(bus);
85         ccsr_fsl_pci_t *regs = pcie->regs;
86         u32 val;
87         u8 val_8;
88         u16 val_16;
89         u32 val_32;
90
91         if (fsl_pcie_addr_valid(pcie, bdf))
92                 return 0;
93
94         bdf = bdf - PCI_BDF(bus->seq, 0, 0);
95         val = bdf | (offset & 0xfc) | ((offset & 0xf00) << 16) | 0x80000000;
96         out_be32(&regs->cfg_addr, val);
97
98         sync();
99
100         switch (size) {
101         case PCI_SIZE_8:
102                 val_8 = value;
103                 out_8((u8 *)&regs->cfg_data + (offset & 3), val_8);
104                 break;
105         case PCI_SIZE_16:
106                 val_16 = value;
107                 out_le16((u16 *)((u8 *)&regs->cfg_data + (offset & 2)), val_16);
108                 break;
109         case PCI_SIZE_32:
110                 val_32 = value;
111                 out_le32(&regs->cfg_data, val_32);
112                 break;
113         }
114
115         return 0;
116 }
117
118 static int fsl_pcie_hose_read_config(struct fsl_pcie *pcie, uint offset,
119                                      ulong *valuep, enum pci_size_t size)
120 {
121         int ret;
122         struct udevice *bus = pcie->bus;
123
124         ret = fsl_pcie_read_config(bus, PCI_BDF(bus->seq, 0, 0),
125                                    offset, valuep, size);
126
127         return ret;
128 }
129
130 static int fsl_pcie_hose_write_config(struct fsl_pcie *pcie, uint offset,
131                                       ulong value, enum pci_size_t size)
132 {
133         struct udevice *bus = pcie->bus;
134
135         return fsl_pcie_write_config(bus, PCI_BDF(bus->seq, 0, 0),
136                                      offset, value, size);
137 }
138
139 static int fsl_pcie_hose_read_config_byte(struct fsl_pcie *pcie, uint offset,
140                                           u8 *valuep)
141 {
142         ulong val;
143         int ret;
144
145         ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_8);
146         *valuep = val;
147
148         return ret;
149 }
150
151 static int fsl_pcie_hose_read_config_word(struct fsl_pcie *pcie, uint offset,
152                                           u16 *valuep)
153 {
154         ulong val;
155         int ret;
156
157         ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_16);
158         *valuep = val;
159
160         return ret;
161 }
162
163 static int fsl_pcie_hose_read_config_dword(struct fsl_pcie *pcie, uint offset,
164                                            u32 *valuep)
165 {
166         ulong val;
167         int ret;
168
169         ret = fsl_pcie_hose_read_config(pcie, offset, &val, PCI_SIZE_32);
170         *valuep = val;
171
172         return ret;
173 }
174
175 static int fsl_pcie_hose_write_config_byte(struct fsl_pcie *pcie, uint offset,
176                                            u8 value)
177 {
178         return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_8);
179 }
180
181 static int fsl_pcie_hose_write_config_word(struct fsl_pcie *pcie, uint offset,
182                                            u16 value)
183 {
184         return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_16);
185 }
186
187 static int fsl_pcie_hose_write_config_dword(struct fsl_pcie *pcie, uint offset,
188                                             u32 value)
189 {
190         return fsl_pcie_hose_write_config(pcie, offset, value, PCI_SIZE_32);
191 }
192
193 static int fsl_pcie_link_up(struct fsl_pcie *pcie)
194 {
195         ccsr_fsl_pci_t *regs = pcie->regs;
196         u16 ltssm;
197
198         if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
199                 ltssm = (in_be32(&regs->pex_csr0)
200                         & PEX_CSR0_LTSSM_MASK) >> PEX_CSR0_LTSSM_SHIFT;
201                 return ltssm == LTSSM_L0_REV3;
202         }
203
204         fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, &ltssm);
205
206         return ltssm == LTSSM_L0;
207 }
208
209 static bool fsl_pcie_is_agent(struct fsl_pcie *pcie)
210 {
211         u8 header_type;
212
213         fsl_pcie_hose_read_config_byte(pcie, PCI_HEADER_TYPE, &header_type);
214
215         return (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
216 }
217
218 static int fsl_pcie_setup_law(struct fsl_pcie *pcie)
219 {
220         struct pci_region *io, *mem, *pref;
221
222         pci_get_regions(pcie->bus, &io, &mem, &pref);
223
224         if (mem)
225                 set_next_law(mem->phys_start,
226                              law_size_bits(mem->size),
227                              pcie->law_trgt_if);
228
229         if (io)
230                 set_next_law(io->phys_start,
231                              law_size_bits(io->size),
232                              pcie->law_trgt_if);
233
234         return 0;
235 }
236
237 static void fsl_pcie_config_ready(struct fsl_pcie *pcie)
238 {
239         ccsr_fsl_pci_t *regs = pcie->regs;
240
241         if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
242                 setbits_be32(&regs->config, FSL_PCIE_V3_CFG_RDY);
243                 return;
244         }
245
246         fsl_pcie_hose_write_config_byte(pcie, FSL_PCIE_CFG_RDY, 0x1);
247 }
248
249 static int fsl_pcie_setup_outbound_win(struct fsl_pcie *pcie, int idx,
250                                        int type, u64 phys, u64 bus_addr,
251                                        pci_size_t size)
252 {
253         ccsr_fsl_pci_t *regs = pcie->regs;
254         pot_t *po = &regs->pot[idx];
255         u32 war, sz;
256
257         if (idx < 0)
258                 return -EINVAL;
259
260         out_be32(&po->powbar, phys >> 12);
261         out_be32(&po->potar, bus_addr >> 12);
262 #ifdef CONFIG_SYS_PCI_64BIT
263         out_be32(&po->potear, bus_addr >> 44);
264 #else
265         out_be32(&po->potear, 0);
266 #endif
267
268         sz = (__ilog2_u64((u64)size) - 1);
269         war = POWAR_EN | sz;
270
271         if (type == PCI_REGION_IO)
272                 war |= POWAR_IO_READ | POWAR_IO_WRITE;
273         else
274                 war |= POWAR_MEM_READ | POWAR_MEM_WRITE;
275
276         out_be32(&po->powar, war);
277
278         return 0;
279 }
280
281 static int fsl_pcie_setup_inbound_win(struct fsl_pcie *pcie, int idx,
282                                       bool pf, u64 phys, u64 bus_addr,
283                                       pci_size_t size)
284 {
285         ccsr_fsl_pci_t *regs = pcie->regs;
286         pit_t *pi = &regs->pit[idx];
287         u32 sz = (__ilog2_u64(size) - 1);
288         u32 flag = PIWAR_LOCAL;
289
290         if (idx < 0)
291                 return -EINVAL;
292
293         out_be32(&pi->pitar, phys >> 12);
294         out_be32(&pi->piwbar, bus_addr >> 12);
295
296 #ifdef CONFIG_SYS_PCI_64BIT
297         out_be32(&pi->piwbear, bus_addr >> 44);
298 #else
299         out_be32(&pi->piwbear, 0);
300 #endif
301
302         if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A005434))
303                 flag = 0;
304
305         flag |= PIWAR_EN | PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
306         if (pf)
307                 flag |= PIWAR_PF;
308         out_be32(&pi->piwar, flag | sz);
309
310         return 0;
311 }
312
313 static int fsl_pcie_setup_outbound_wins(struct fsl_pcie *pcie)
314 {
315         struct pci_region *io, *mem, *pref;
316         int idx = 1; /* skip 0 */
317
318         pci_get_regions(pcie->bus, &io, &mem, &pref);
319
320         if (io)
321                 /* ATU : OUTBOUND : IO */
322                 fsl_pcie_setup_outbound_win(pcie, idx++,
323                                             PCI_REGION_IO,
324                                             io->phys_start,
325                                             io->bus_start,
326                                             io->size);
327
328         if (mem)
329                 /* ATU : OUTBOUND : MEM */
330                 fsl_pcie_setup_outbound_win(pcie, idx++,
331                                             PCI_REGION_MEM,
332                                             mem->phys_start,
333                                             mem->bus_start,
334                                             mem->size);
335         return 0;
336 }
337
338 static int fsl_pcie_setup_inbound_wins(struct fsl_pcie *pcie)
339 {
340         phys_addr_t phys_start = CONFIG_SYS_PCI_MEMORY_PHYS;
341         pci_addr_t bus_start = CONFIG_SYS_PCI_MEMORY_BUS;
342         u64 sz = min((u64)gd->ram_size, (1ull << 32));
343         pci_size_t pci_sz;
344         int idx;
345
346         if (pcie->block_rev >= PEX_IP_BLK_REV_2_2)
347                 idx = 2;
348         else
349                 idx = 3;
350
351         pci_sz = 1ull << __ilog2_u64(sz);
352
353         dev_dbg(pcie->bus, "R0 bus_start: %llx phys_start: %llx size: %llx\n",
354                 (u64)bus_start, (u64)phys_start, (u64)sz);
355
356         /* if we aren't an exact power of two match, pci_sz is smaller
357          * round it up to the next power of two.  We report the actual
358          * size to pci region tracking.
359          */
360         if (pci_sz != sz)
361                 sz = 2ull << __ilog2_u64(sz);
362
363         fsl_pcie_setup_inbound_win(pcie, idx--, true,
364                                    CONFIG_SYS_PCI_MEMORY_PHYS,
365                                    CONFIG_SYS_PCI_MEMORY_BUS, sz);
366 #if defined(CONFIG_PHYS_64BIT) && defined(CONFIG_SYS_PCI_64BIT)
367         /*
368          * On 64-bit capable systems, set up a mapping for all of DRAM
369          * in high pci address space.
370          */
371         pci_sz = 1ull << __ilog2_u64(gd->ram_size);
372         /* round up to the next largest power of two */
373         if (gd->ram_size > pci_sz)
374                 pci_sz = 1ull << (__ilog2_u64(gd->ram_size) + 1);
375
376         dev_dbg(pcie->bus, "R64 bus_start: %llx phys_start: %llx size: %llx\n",
377                 (u64)CONFIG_SYS_PCI64_MEMORY_BUS,
378                 (u64)CONFIG_SYS_PCI_MEMORY_PHYS, (u64)pci_sz);
379
380         fsl_pcie_setup_inbound_win(pcie, idx--, true,
381                                    CONFIG_SYS_PCI_MEMORY_PHYS,
382                                    CONFIG_SYS_PCI64_MEMORY_BUS, pci_sz);
383 #endif
384
385         return 0;
386 }
387
388 static int fsl_pcie_init_atmu(struct fsl_pcie *pcie)
389 {
390         fsl_pcie_setup_outbound_wins(pcie);
391         fsl_pcie_setup_inbound_wins(pcie);
392
393         return 0;
394 }
395
396 static int fsl_pcie_init_port(struct fsl_pcie *pcie)
397 {
398         ccsr_fsl_pci_t *regs = pcie->regs;
399         u32 val_32;
400         u16 val_16;
401
402         fsl_pcie_init_atmu(pcie);
403
404         if (IS_ENABLED(CONFIG_FSL_PCIE_DISABLE_ASPM)) {
405                 val_32 = 0;
406                 fsl_pcie_hose_read_config_dword(pcie, PCI_LCR, &val_32);
407                 val_32 &= ~0x03;
408                 fsl_pcie_hose_write_config_dword(pcie, PCI_LCR, val_32);
409                 udelay(1);
410         }
411
412         if (IS_ENABLED(CONFIG_FSL_PCIE_RESET)) {
413                 u16 ltssm;
414                 int i;
415
416                 if (pcie->block_rev >= PEX_IP_BLK_REV_3_0) {
417                         /* assert PCIe reset */
418                         setbits_be32(&regs->pdb_stat, 0x08000000);
419                         (void)in_be32(&regs->pdb_stat);
420                         udelay(1000);
421                         /* clear PCIe reset */
422                         clrbits_be32(&regs->pdb_stat, 0x08000000);
423                         asm("sync;isync");
424                         for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++)
425                                 udelay(1000);
426                 } else {
427                         fsl_pcie_hose_read_config_word(pcie, PCI_LTSSM, &ltssm);
428                         if (ltssm == 1) {
429                                 /* assert PCIe reset */
430                                 setbits_be32(&regs->pdb_stat, 0x08000000);
431                                 (void)in_be32(&regs->pdb_stat);
432                                 udelay(100);
433                                 /* clear PCIe reset */
434                                 clrbits_be32(&regs->pdb_stat, 0x08000000);
435                                 asm("sync;isync");
436                                 for (i = 0; i < 100 &&
437                                      !fsl_pcie_link_up(pcie); i++)
438                                         udelay(1000);
439                         }
440                 }
441         }
442
443         if (IS_ENABLED(CONFIG_SYS_P4080_ERRATUM_PCIE_A003) &&
444             !fsl_pcie_link_up(pcie)) {
445                 serdes_corenet_t *srds_regs;
446
447                 srds_regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
448                 val_32 = in_be32(&srds_regs->srdspccr0);
449
450                 if ((val_32 >> 28) == 3) {
451                         int i;
452
453                         out_be32(&srds_regs->srdspccr0, 2 << 28);
454                         setbits_be32(&regs->pdb_stat, 0x08000000);
455                         in_be32(&regs->pdb_stat);
456                         udelay(100);
457                         clrbits_be32(&regs->pdb_stat, 0x08000000);
458                         asm("sync;isync");
459                         for (i = 0; i < 100 && !fsl_pcie_link_up(pcie); i++)
460                                 udelay(1000);
461                 }
462         }
463
464         /*
465          * The Read-Only Write Enable bit defaults to 1 instead of 0.
466          * Set to 0 to protect the read-only registers.
467          */
468         if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A007815))
469                 clrbits_be32(&regs->dbi_ro_wr_en, 0x01);
470
471         /*
472          * Enable All Error Interrupts except
473          * - Master abort (pci)
474          * - Master PERR (pci)
475          * - ICCA (PCIe)
476          */
477         out_be32(&regs->peer, ~0x20140);
478
479         /* set URR, FER, NFER (but not CER) */
480         fsl_pcie_hose_read_config_dword(pcie, PCI_DCR, &val_32);
481         val_32 |= 0xf000e;
482         fsl_pcie_hose_write_config_dword(pcie, PCI_DCR, val_32);
483
484         /* Clear all error indications */
485         out_be32(&regs->pme_msg_det, 0xffffffff);
486         out_be32(&regs->pme_msg_int_en, 0xffffffff);
487         out_be32(&regs->pedr, 0xffffffff);
488
489         fsl_pcie_hose_read_config_word(pcie, PCI_DSR, &val_16);
490         if (val_16)
491                 fsl_pcie_hose_write_config_word(pcie, PCI_DSR, 0xffff);
492
493         fsl_pcie_hose_read_config_word(pcie, PCI_SEC_STATUS, &val_16);
494         if (val_16)
495                 fsl_pcie_hose_write_config_word(pcie, PCI_SEC_STATUS, 0xffff);
496
497         return 0;
498 }
499
500 static int fsl_pcie_fixup_classcode(struct fsl_pcie *pcie)
501 {
502         ccsr_fsl_pci_t *regs = pcie->regs;
503         u32 val;
504
505         setbits_be32(&regs->dbi_ro_wr_en, 0x01);
506         fsl_pcie_hose_read_config_dword(pcie, PCI_CLASS_REVISION, &val);
507         val &= 0xff;
508         val |= PCI_CLASS_BRIDGE_PCI << 16;
509         fsl_pcie_hose_write_config_dword(pcie, PCI_CLASS_REVISION, val);
510         clrbits_be32(&regs->dbi_ro_wr_en, 0x01);
511
512         return 0;
513 }
514
515 static int fsl_pcie_init_rc(struct fsl_pcie *pcie)
516 {
517         return fsl_pcie_fixup_classcode(pcie);
518 }
519
520 static int fsl_pcie_init_ep(struct fsl_pcie *pcie)
521 {
522         fsl_pcie_config_ready(pcie);
523
524         return 0;
525 }
526
527 static int fsl_pcie_probe(struct udevice *dev)
528 {
529         struct fsl_pcie *pcie = dev_get_priv(dev);
530         ccsr_fsl_pci_t *regs = pcie->regs;
531         u16 val_16;
532
533         pcie->bus = dev;
534         pcie->block_rev = in_be32(&regs->block_rev1);
535
536         list_add(&pcie->list, &fsl_pcie_list);
537         pcie->enabled = is_serdes_configured(PCIE1 + pcie->idx);
538         if (!pcie->enabled) {
539                 printf("PCIe%d: %s disabled\n", pcie->idx, dev->name);
540                 return 0;
541         }
542
543         fsl_pcie_setup_law(pcie);
544
545         pcie->mode = fsl_pcie_is_agent(pcie);
546
547         fsl_pcie_init_port(pcie);
548
549         printf("PCIe%d: %s ", pcie->idx, dev->name);
550
551         if (pcie->mode) {
552                 printf("Endpoint");
553                 fsl_pcie_init_ep(pcie);
554         } else {
555                 printf("Root Complex");
556                 fsl_pcie_init_rc(pcie);
557         }
558
559         if (!fsl_pcie_link_up(pcie)) {
560                 printf(": %s\n", pcie->mode ? "undetermined link" : "no link");
561                 return 0;
562         }
563
564         fsl_pcie_hose_read_config_word(pcie, PCI_LSR, &val_16);
565         printf(": x%d gen%d\n", (val_16 & 0x3f0) >> 4, (val_16 & 0xf));
566
567         return 0;
568 }
569
570 static int fsl_pcie_ofdata_to_platdata(struct udevice *dev)
571 {
572         struct fsl_pcie *pcie = dev_get_priv(dev);
573         int ret;
574
575         pcie->regs = dev_remap_addr(dev);
576         if (!pcie->regs) {
577                 pr_err("\"reg\" resource not found\n");
578                 return -EINVAL;
579         }
580
581         ret = dev_read_u32(dev, "law_trgt_if", &pcie->law_trgt_if);
582         if (ret < 0) {
583                 pr_err("\"law_trgt_if\" not found\n");
584                 return ret;
585         }
586
587         pcie->idx = (dev_read_addr(dev) - 0xffe240000) / 0x10000;
588
589         return 0;
590 }
591
592 static const struct dm_pci_ops fsl_pcie_ops = {
593         .read_config    = fsl_pcie_read_config,
594         .write_config   = fsl_pcie_write_config,
595 };
596
597 static const struct udevice_id fsl_pcie_ids[] = {
598         { .compatible = "fsl,pcie-t2080" },
599         { }
600 };
601
602 U_BOOT_DRIVER(fsl_pcie) = {
603         .name = "fsl_pcie",
604         .id = UCLASS_PCI,
605         .of_match = fsl_pcie_ids,
606         .ops = &fsl_pcie_ops,
607         .ofdata_to_platdata = fsl_pcie_ofdata_to_platdata,
608         .probe = fsl_pcie_probe,
609         .priv_auto_alloc_size = sizeof(struct fsl_pcie),
610 };