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