paxb.c 24.3 KB
Newer Older
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
/*
 * Copyright (c) 2016 - 2020, Broadcom
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <errno.h>
#include <stdbool.h>

#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <lib/mmio.h>

#include <paxb.h>
#include <sr_def.h>
#include <sr_utils.h>

#define PCIE_CORE_PWR_ARR_POWERON        0x8
#define PCIE_CORE_PWR_ARR_POWEROK        0x4
#define PCIE_CORE_PWR_POWERON            0x2
#define PCIE_CORE_PWR_POWEROK            0x1

#define PCIE_CORE_USER_CFG               (PCIE_CORE_BASE + 0x38)
#define PCIE_PAXB_SMMU_SID_CFG           (PCIE_CORE_BASE + 0x60)
#ifdef SID_B8_D1_F1
#define PAXB_SMMU_SID_CFG_BUS_WIDTH      (0x8 << 8)
#define PAXB_SMMU_SID_CFG_DEV_WIDTH      (0x1 << 12)
#define PAXB_SMMU_SID_CFG_FUN_WIDTH      (0x1 << 16)
#else
#define PAXB_SMMU_SID_CFG_BUS_WIDTH      (0x2 << 8)
#define PAXB_SMMU_SID_CFG_DEV_WIDTH      (0x5 << 12)
#define PAXB_SMMU_SID_CFG_FUN_WIDTH      (0x3 << 16)
#endif

#define PAXB_APB_TIMEOUT_COUNT_OFFSET 0x034

/* allow up to 5 ms for each power switch to stabilize */
#define PCIE_CORE_PWR_TIMEOUT_MS      5

/* wait 1 microsecond for PCIe core soft reset */
#define PCIE_CORE_SOFT_RST_DELAY_US 1

/*
 * List of PAXB APB registers
 */
#define PAXB_BASE                        0x48000000
#define PAXB_BASE_OFFSET                 0x4000
#define PAXB_OFFSET(core)                (PAXB_BASE + \
					  (core) * PAXB_BASE_OFFSET)

#define PAXB_CLK_CTRL_OFFSET             0x000
#define PAXB_EP_PERST_SRC_SEL_MASK       (1 << 2)
#define PAXB_EP_MODE_PERST_MASK          (1 << 1)
#define PAXB_RC_PCIE_RST_OUT_MASK        (1 << 0)

#define PAXB_MAX_IMAP_WINDOWS            8
#define PAXB_IMAP_REG_WIDTH              8
#define PAXB_IMAP0_REG_WIDTH             4
#define PAXB_AXUSER_REG_WIDTH            4

#define PAXB_CFG_IND_ADDR_OFFSET         0x120
#define PAXB_CFG_IND_DATA_OFFSET         0x124
#define PAXB_CFG_IND_ADDR_MASK           0x1ffc
#define PAXB_CFG_CFG_TYPE_MASK           0x1

#define PAXB_EP_CFG_ADDR_OFFSET          0x1f8
#define PAXB_EP_CFG_DATA_OFFSET          0x1fc
#define PAXB_EP_CFG_ADDR_MASK            0xffc
#define PAXB_EP_CFG_TYPE_MASK            0x1

#define PAXB_0_DEFAULT_IMAP              0xed0
#define DEFAULT_ADDR_INVALID             BIT(0)
#define PAXB_0_DEFAULT_IMAP_AXUSER       0xed8
#define PAXB_0_DEFAULT_IMAP_AXCACHE      0xedc
#define IMAP_AXCACHE                     0xff
#define OARR_VALID                       BIT(0)
#define IMAP_VALID                       BIT(0)

#define PAXB_IMAP0_BASE_OFFSET           0xc00
#define PAXB_IARR0_BASE_OFFSET           0xd00
#define PAXB_IMAP0_OFFSET(idx)           (PAXB_IMAP0_BASE_OFFSET + \
					  (idx) * PAXB_IMAP0_REG_WIDTH)
#define PAXB_IMAP0_WINDOW_SIZE           0x1000

#define PAXB_IMAP2_OFFSET                0xcc0
#define PAXB_IMAP0_REGS_TYPE_OFFSET      0xcd0
#define PAXB_IARR2_LOWER_OFFSET          0xd10

#define PAXB_IMAP3_BASE_OFFSET           0xe08
#define PAXB_IMAP3_OFFSET(idx)           (PAXB_IMAP3_BASE_OFFSET + \
					  (idx) * PAXB_IMAP_REG_WIDTH)

#define PAXB_IMAP3_0_AXUSER_B_OFFSET     0xe48
#define PAXB_IMAP3_0_AXUSER_OFFSET(idx)  (PAXB_IMAP3_0_AXUSER_B_OFFSET + \
					  (idx) * PAXB_AXUSER_REG_WIDTH)

#define PAXB_IMAP4_BASE_OFFSET           0xe70
#define PAXB_IMAP4_OFFSET(idx)           (PAXB_IMAP4_BASE_OFFSET + \
					  (idx) * PAXB_IMAP_REG_WIDTH)

#define PAXB_IMAP4_0_AXUSER_B_OFFSET     0xeb0
#define PAXB_IMAP4_0_AXUSER_OFFSET(idx)  (PAXB_IMAP4_0_AXUSER_B_OFFSET + \
					  (idx) * PAXB_AXUSER_REG_WIDTH)

#define PAXB_CFG_LINK_STATUS_OFFSET      0xf0c
#define PAXB_CFG_PHYLINKUP_MASK          (1 << 3)
#define PAXB_CFG_DL_ACTIVE_MASK          (1 << 2)

#define PAXB_IMAP0_0_AXUSER_OFFSET       0xf60
#define PAXB_IMAP2_AXUSER_OFFSET         0xfe0

/* cacheable write-back, allocate on both reads and writes */
#define IMAP_ARCACHE                     0x0f0
#define IMAP_AWCACHE                     0xf00
/* normal access, nonsecure access, and data access */
/* AWQOS:0xe and ARQOS:0xa */
/* AWPROT:0x2 and ARPROT:0x1 */
#define IMAP_AXUSER                      0x002e002a

/*
 * List of NIC security and PIPEMUX related registers
 */
#define SR_PCIE_NIC_SECURITY_BASE      0x58100000
#define NS3Z_PCIE_NIC_SECURITY_BASE    0x48100000

#define GITS_TRANSLATER                0x63c30000

#define VENDOR_ID                 0x14e4
#define CFG_RC_DEV_ID             0x434
#define CFG_RC_DEV_SUBID          0x438
#define PCI_BRIDGE_CTRL_REG_OFFSET     0x43c
#define PCI_CLASS_BRIDGE_MASK          0xffff00
#define PCI_CLASS_BRIDGE_SHIFT         8
#define PCI_CLASS_BRIDGE_PCI           0x0604

/*
 * List of PAXB RC configuration space registers
 */

/* first capability list entry */
#define PCI_CAPABILITY_LIST_OFFSET    0x34
#define PCI_CAPABILITY_SPEED_OFFSET   0xc
#define PCI_EP_CAPABILITY_OFFSET      0x10

#define CFG_RC_LINK_STATUS_CTRL_2     0x0dc
#define CFG_RC_LINK_SPEED_SHIFT       0
#define CFG_RC_LINK_SPEED_MASK        (0xf << CFG_RC_LINK_SPEED_SHIFT)

#define CFG_RC_DEVICE_CAP             0x4d4
#define CFG_RC_DEVICE_CAP_MPS_SHIFT   0
#define CFG_RC_DEVICE_CAP_MPS_MASK    (0x7 << CFG_RC_DEVICE_CAP_MPS_SHIFT)
/* MPS 256 bytes */
#define CFG_RC_DEVICE_CAP_MPS_256B    (0x1 << CFG_RC_DEVICE_CAP_MPS_SHIFT)
/* MPS 512 bytes */
#define CFG_RC_DEVICE_CAP_MPS_512B    (0x2 << CFG_RC_DEVICE_CAP_MPS_SHIFT)

#define CFG_RC_TL_FCIMM_NP_LIMIT       0xa10
#define CFG_RC_TL_FCIMM_NP_VAL         0x01500000
#define CFG_RC_TL_FCIMM_P_LIMIT        0xa14
#define CFG_RC_TL_FCIMM_P_VAL          0x03408080

#define CFG_RC_LINK_CAP               0x4dc
#define CFG_RC_LINK_CAP_SPEED_SHIFT   0
#define CFG_RC_LINK_CAP_SPEED_MASK    (0xf << CFG_RC_LINK_CAP_SPEED_SHIFT)
#define CFG_RC_LINK_CAP_WIDTH_SHIFT   4
#define CFG_RC_LINK_CAP_WIDTH_MASK    (0x1f << CFG_RC_LINK_CAP_WIDTH_SHIFT)

#define CFG_LINK_CAP_RC               0x4f0
#define CFG_RC_DL_ACTIVE_SHIFT        0
#define CFG_RC_DL_ACTIVE_MASK         (0x1 << CFG_RC_DL_ACTIVE_SHIFT)
#define CFG_RC_SLOT_CLK_SHIFT         1
#define CFG_RC_SLOT_CLK_MASK          (0x1 << CFG_RC_SLOT_CLK_SHIFT)

#define CFG_ROOT_CAP_RC               0x4f8
#define CFG_ROOT_CAP_LTR_SHIFT        1
#define CFG_ROOT_CAP_LTR_MASK         (0x1 << CFG_ROOT_CAP_LTR_SHIFT)

#define CFG_RC_CLKREQ_ENABLED         0x4fc
#define CFG_RC_CLKREQ_ENABLED_SHIFT   0
#define CFG_RC_CLKREQ_ENABLED_MASK    (0x1 << CFG_RC_CLKREQ_ENABLED_SHIFT)

#define CFG_RC_COEFF_ADDR             0x638

#define CFG_RC_TL_CTRL_0              0x800
#define RC_MEM_DW_CHK_MASK            0x03fe

#define CFG_RC_PDL_CTRL_4             0x1010
#define NPH_FC_INIT_SHIFT             24
#define NPH_FC_INIT_MASK              (U(0xff) << NPH_FC_INIT_SHIFT)
#define PD_FC_INIT_SHIFT              12
#define PD_FC_INIT_MASK               (0xffff << PD_FC_INIT_SHIFT)

#define CFG_RC_PDL_CTRL_5             0x1014
#define PH_INIT_SHIFT                 0
#define PH_INIT_MASK                  (0xff << PH_INIT_SHIFT)

#define DL_STATUS_OFFSET              0x1048
#define PHYLINKUP                     BIT(13)

#define PH_INIT                       0x10
#define PD_FC_INIT                    0x100
#define NPH_FC_INIT                   0x8

#define SRP_PH_INIT                   0x7F
#define SRP_PD_FC_INIT                0x200
#define SRP_NPH_FC_INIT               0x7F

#define CFG_ADDR_BUS_NUM_SHIFT        20
#define CFG_ADDR_DEV_NUM_SHIFT        15
#define CFG_ADDR_FUNC_NUM_SHIFT       12
#define CFG_ADDR_REG_NUM_SHIFT        2
#define CFG_ADDR_REG_NUM_MASK         0x00000ffc
#define CFG_ADDR_CFG_TYPE_MASK        0x00000003

#define DL_LINK_UP_TIMEOUT_MS         1000

#define CFG_RETRY_STATUS              0xffff0001
#define CRS_TIMEOUT_MS                5000

/* create EP config data to write */
#define DEF_BUS_NO                    1 /* default bus 1 */
#define DEF_SLOT_NO                   0 /* default slot 0 */
#define DEF_FN_NO                     0 /* default fn 0 */

#define EP_CONFIG_VAL(bus_no, slot, fn, where) \
	(((bus_no) << CFG_ADDR_BUS_NUM_SHIFT) | \
	((slot) << CFG_ADDR_DEV_NUM_SHIFT) | \
	((fn) << CFG_ADDR_FUNC_NUM_SHIFT) | \
	((where) & CFG_ADDR_REG_NUM_MASK) | \
	(1 & CFG_ADDR_CFG_TYPE_MASK))

/* PAXB security offset */
#define PAXB_SECURITY_IDM_OFFSET 0x1c
#define PAXB_SECURITY_APB_OFFSET 0x24
#define PAXB_SECURITY_ECAM_OFFSET 0x3c

#define paxb_get_config(type) paxb_get_##type##_config()

static unsigned int paxb_sec_reg_offset[] = {
	0x0c, /* PAXB0 AXI */
	0x10, /* PAXB1 AXI */
	0x14, /* PAXB2 AXI */
	0x18, /* PAXB3 AXI */
	0x20, /* PAXB4 AXI */
	0x28, /* PAXB5 AXI */
	0x2c, /* PAXB6 AXI */
	0x30, /* PAXB7 AXI */
	0x24, /* PAXB APB */
};

const paxb_cfg *paxb;

/*
 * Given a PIPEMUX strap and PCIe core index, this function returns 1 if a
 * PCIe core needs to be enabled
 */
int pcie_core_needs_enable(unsigned int core_idx)
{
	if (paxb->core_needs_enable)
		return paxb->core_needs_enable(core_idx);

	return 0;
}

static void pcie_set_default_tx_coeff(uint32_t core_idx, uint32_t link_width)
{
	unsigned int lanes = 0;
	uint32_t data, addr;

	addr = CFG_RC_COEFF_ADDR;
	for (lanes = 0; lanes < link_width; lanes = lanes + 2) {
		data = paxb_rc_cfg_read(core_idx, addr);
		data &= 0xf0f0f0f0;
		data |= (7 & 0xf);
		data |= (7 & 0xf) << 8;
		data |= (7 & 0xf) << 16;
		data |= (7 & 0xf) << 24;

		paxb_rc_cfg_write(core_idx, addr, data);
		addr += 4;
	}
}

static int paxb_rc_link_init(void)
{
	uint32_t val, link_speed;
	unsigned int link_width;
	uint32_t core_idx;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		link_width = paxb->get_link_width(core_idx);
		if (!link_width) {
			ERROR("Unsupported PIPEMUX\n");
			return -EOPNOTSUPP;
		}

		link_speed = paxb->get_link_speed();
		/* program RC's link cap reg to advertise proper link width */
		val = paxb_rc_cfg_read(core_idx, CFG_RC_LINK_CAP);
		val &= ~CFG_RC_LINK_CAP_WIDTH_MASK;
		val |= (link_width << CFG_RC_LINK_CAP_WIDTH_SHIFT);
		paxb_rc_cfg_write(core_idx, CFG_RC_LINK_CAP, val);

		/* program RC's link cap reg to advertise proper link speed */
		val = paxb_rc_cfg_read(core_idx, CFG_RC_LINK_CAP);
		val &= ~CFG_RC_LINK_CAP_SPEED_MASK;
		val |= link_speed << CFG_RC_LINK_CAP_SPEED_SHIFT;
		paxb_rc_cfg_write(core_idx, CFG_RC_LINK_CAP, val);

		/* also need to program RC's link status control register */
		val = paxb_rc_cfg_read(core_idx, CFG_RC_LINK_STATUS_CTRL_2);
		val &= ~(CFG_RC_LINK_SPEED_MASK);
		val |= link_speed << CFG_RC_LINK_SPEED_SHIFT;
		paxb_rc_cfg_write(core_idx, CFG_RC_LINK_STATUS_CTRL_2, val);

#ifdef WAR_PLX_PRESET_PARITY_FAIL
		/* WAR to avoid crash with PLX switch in GEN3*/
		/* While PRESET, PLX switch is not fixing parity so disabled */
		val = paxb_rc_cfg_read(core_idx, CFG_RC_REG_PHY_CTL_10);
		val &= ~(PHY_CTL_10_GEN3_MATCH_PARITY);
		paxb_rc_cfg_write(core_idx, CFG_RC_REG_PHY_CTL_10, val);
#endif
		pcie_set_default_tx_coeff(core_idx, link_width);
	}
	return 0;
}

#ifdef PAXB_LINKUP
static void paxb_perst_ctrl(unsigned int core_idx, bool assert)
{
	uint32_t clk_ctrl = PAXB_OFFSET(core_idx) + PAXB_CLK_CTRL_OFFSET;

	if (assert) {
		mmio_clrbits_32(clk_ctrl, PAXB_EP_PERST_SRC_SEL_MASK |
				PAXB_EP_MODE_PERST_MASK |
				PAXB_RC_PCIE_RST_OUT_MASK);
		udelay(250);
	} else {
		mmio_setbits_32(clk_ctrl, PAXB_RC_PCIE_RST_OUT_MASK);
		mdelay(100);
	}
}

static void paxb_start_link_up(void)
{
	unsigned int core_idx;
	uint32_t val, timeout;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		/* toggle PERST */
		paxb_perst_ctrl(core_idx, true);
		paxb_perst_ctrl(core_idx, false);

		timeout = DL_LINK_UP_TIMEOUT_MS;
		/* wait for Link up */
		do {
			val = mmio_read_32(PAXB_OFFSET(core_idx) +
					   PAXB_CFG_LINK_STATUS_OFFSET);
			if (val & PAXB_CFG_DL_ACTIVE_MASK)
				break;

			mdelay(1);
		} while (--timeout);

		if (!timeout)
			ERROR("PAXB core %u link is down\n", core_idx);
	}
}
#endif

static void pcie_core_soft_reset(unsigned int core_idx)
{
	uint32_t offset = core_idx * PCIE_CORE_PWR_OFFSET;
	uintptr_t ctrl = (uintptr_t)(PCIE_CORE_SOFT_RST_CFG_BASE + offset);

	/* Put PCIe core in soft reset */
	mmio_clrbits_32(ctrl, PCIE_CORE_SOFT_RST);

	/* Wait for 1 us before pulling PCIe core out of soft reset */
	udelay(PCIE_CORE_SOFT_RST_DELAY_US);

	mmio_setbits_32(ctrl, PCIE_CORE_SOFT_RST);
}

static int pcie_core_pwron_switch(uintptr_t ctrl, uintptr_t status,
				  uint32_t mask)
{
	uint32_t val;
	unsigned int timeout = PCIE_CORE_PWR_TIMEOUT_MS;

	/* enable switch */
	mmio_setbits_32(ctrl, mask);

	/* now wait for it to stabilize */
	do {
		val = mmio_read_32(status);
		if ((val & mask) == mask)
			return 0;
		mdelay(1);
	} while (--timeout);

	return -EIO;
}

static int pcie_core_pwr_seq(uintptr_t ctrl, uintptr_t status)
{
	int ret;

	/*
	 * Enable the switch with the following sequence:
	 * 1. Array weak switch output switch
	 * 2. Array strong switch
	 * 3. Weak switch output acknowledge
	 * 4. Strong switch output acknowledge
	 */
	ret = pcie_core_pwron_switch(ctrl, status, PCIE_CORE_PWR_ARR_POWERON);
	if (ret)
		return ret;

	ret = pcie_core_pwron_switch(ctrl, status, PCIE_CORE_PWR_ARR_POWEROK);
	if (ret)
		return ret;

	ret = pcie_core_pwron_switch(ctrl, status, PCIE_CORE_PWR_POWERON);
	if (ret)
		return ret;

	ret = pcie_core_pwron_switch(ctrl, status, PCIE_CORE_PWR_POWEROK);
	if (ret)
		return ret;

	return 0;
}

/*
 * This function enables PCIe core and PAXB memory buffer power, and then
 * remove the PCIe core from isolation
 */
static int pcie_core_pwr_init(unsigned int core_idx)
{
	int ret;
	uint32_t offset = core_idx * PCIE_CORE_PWR_OFFSET;
	uintptr_t ctrl, status;

	/* enable mem power to PCIe core */
	ctrl = (uintptr_t)(PCIE_CORE_MEM_PWR_BASE + offset);
	status = (uintptr_t)(PCIE_CORE_MEM_PWR_STATUS_BASE + offset);
	ret = pcie_core_pwr_seq(ctrl, status);
	if (ret) {
		ERROR("PCIe core mem power failed\n");
		return ret;
	}

	/* now enable mem power to PAXB wrapper */
	ctrl = (uintptr_t)(PCIE_PAXB_MEM_PWR_BASE + offset);
	status = (uintptr_t)(PCIE_PAXB_MEM_PWR_STATUS_BASE + offset);
	ret = pcie_core_pwr_seq(ctrl, status);
	if (ret) {
		ERROR("PAXB mem power failed\n");
		return ret;
	}

	/* now remove power isolation */
	ctrl = (uintptr_t)(PCIE_CORE_ISO_CFG_BASE + offset);
	mmio_clrbits_32(ctrl, PCIE_CORE_ISO | PCIE_CORE_MEM_ISO);

	return 0;
}

static void pcie_ss_reset(void)
{
	mmio_setbits_32(CDRU_MISC_RESET_CONTROL,
			1 << CDRU_MISC_RESET_CONTROL__CDRU_PCIE_RESET_N_R);
}

/*
 * This function reads the PIPEMUX strap, figures out all the PCIe cores that
 * need to be enabled and enable the mem power for those cores
 */
static int pcie_cores_init(void)
{
488
	int ret = 0;
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
	uint32_t core_idx;

	if (paxb->pipemux_init) {
		ret = paxb->pipemux_init();
		if (ret)
			return ret;
	}

	/* bring PCIe subsystem out of reset */
	pcie_ss_reset();

	/* power up all PCIe cores that will be used as RC */
	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		ret = pcie_core_pwr_init(core_idx);
		if (ret) {
			ERROR("PCIe core %u power up failed\n", core_idx);
			return ret;
		}

		pcie_core_soft_reset(core_idx);

		VERBOSE("PCIe core %u is powered up\n", core_idx);
	}

	return ret;
}

void paxb_rc_cfg_write(unsigned int core_idx, unsigned int where,
			      uint32_t val)
{
	mmio_write_32(PAXB_OFFSET(core_idx) + PAXB_CFG_IND_ADDR_OFFSET,
		      (where & PAXB_CFG_IND_ADDR_MASK) |
		      PAXB_CFG_CFG_TYPE_MASK);
	mmio_write_32(PAXB_OFFSET(core_idx) + PAXB_CFG_IND_DATA_OFFSET, val);
}

unsigned int paxb_rc_cfg_read(unsigned int core_idx, unsigned int where)
{
	unsigned int val;

	mmio_write_32(PAXB_OFFSET(core_idx) + PAXB_CFG_IND_ADDR_OFFSET,
		      (where & PAXB_CFG_IND_ADDR_MASK) |
		      PAXB_CFG_CFG_TYPE_MASK);
	val = mmio_read_32(PAXB_OFFSET(core_idx) + PAXB_CFG_IND_DATA_OFFSET);

	return val;
}

static void paxb_cfg_mps(void)
{
	uint32_t val, core_idx, mps;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		val = paxb_rc_cfg_read(core_idx, CFG_RC_DEVICE_CAP);
		val &= ~CFG_RC_DEVICE_CAP_MPS_MASK;
		mps = CFG_RC_DEVICE_CAP_MPS_256B;
		if (core_idx == 0 || core_idx == 1 ||
		    core_idx == 6 || core_idx == 7) {
			mps = CFG_RC_DEVICE_CAP_MPS_512B;
		}
		val |= mps;
		paxb_rc_cfg_write(core_idx, CFG_RC_DEVICE_CAP, val);
	}
}

static void paxb_cfg_dev_id(void)
{
	uint32_t val, core_idx;
	uint32_t device_id;

	device_id = paxb->device_id;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		/* Set Core in RC mode */
		mmio_setbits_32(PCIE_CORE_USER_CFG +
				(core_idx * PCIE_CORE_PWR_OFFSET), 1);

		/* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
		val = paxb_rc_cfg_read(core_idx, PCI_BRIDGE_CTRL_REG_OFFSET);
		val &= ~PCI_CLASS_BRIDGE_MASK;
		val |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
		paxb_rc_cfg_write(core_idx, PCI_BRIDGE_CTRL_REG_OFFSET, val);

		val = (VENDOR_ID << 16) | device_id;
		paxb_rc_cfg_write(core_idx, CFG_RC_DEV_ID, val);

		val = (device_id << 16) | VENDOR_ID;
		paxb_rc_cfg_write(core_idx, CFG_RC_DEV_SUBID, val);
	}
}

static void paxb_cfg_tgt_trn(void)
{
	uint32_t val, core_idx;

	/*
	 * Disable all mem Rd/Wr size check so it allows target read/write
	 * transactions to be more than stipulated DW. As a result, PAXB root
	 * complex will not abort these read/write transcations beyond
	 * stipulated limit
	 */
	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		val = paxb_rc_cfg_read(core_idx, CFG_RC_TL_CTRL_0);
		val &= ~(RC_MEM_DW_CHK_MASK);
		paxb_rc_cfg_write(core_idx, CFG_RC_TL_CTRL_0, val);
	}
}

static void paxb_cfg_pdl_ctrl(void)
{
	uint32_t val, core_idx;
	uint32_t nph, ph, pd;

	/* increase the credit counter to 4 for non-posted header */
	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		nph = NPH_FC_INIT;
		ph = PH_INIT;
		pd = PD_FC_INIT;

		if (core_idx == 0 || core_idx == 1 ||
		    core_idx == 6 || core_idx == 7) {
			nph = SRP_NPH_FC_INIT;
			ph = SRP_PH_INIT;
			pd = SRP_PD_FC_INIT;
		}
		val = paxb_rc_cfg_read(core_idx, CFG_RC_PDL_CTRL_4);
		val &= ~NPH_FC_INIT_MASK;
		val &= ~PD_FC_INIT_MASK;
		val = val | (nph << NPH_FC_INIT_SHIFT);
		val = val | (pd << PD_FC_INIT_SHIFT);
		paxb_rc_cfg_write(core_idx, CFG_RC_PDL_CTRL_4, val);

		val = paxb_rc_cfg_read(core_idx, CFG_RC_PDL_CTRL_5);
		val &= ~PH_INIT_MASK;
		val = val | (ph << PH_INIT_SHIFT);
		paxb_rc_cfg_write(core_idx, CFG_RC_PDL_CTRL_5, val);

		/*
		 * ASIC to give more optmized value after further investigation.
		 * till then this is important to have to get similar
		 * performance on all the slots.
		 */
		paxb_rc_cfg_write(core_idx, CFG_RC_TL_FCIMM_NP_LIMIT,
				CFG_RC_TL_FCIMM_NP_VAL);

		paxb_rc_cfg_write(core_idx, CFG_RC_TL_FCIMM_P_LIMIT,
				CFG_RC_TL_FCIMM_P_VAL);
	}
}

static void paxb_cfg_clkreq(void)
{
	uint32_t val, core_idx;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		val = paxb_rc_cfg_read(core_idx, CFG_RC_CLKREQ_ENABLED);
		val &= ~CFG_RC_CLKREQ_ENABLED_MASK;
		paxb_rc_cfg_write(core_idx, CFG_RC_CLKREQ_ENABLED, val);
	}
}

static void paxb_cfg_dl_active(bool enable)
{
	uint32_t val, core_idx;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		val = paxb_rc_cfg_read(core_idx, CFG_LINK_CAP_RC);
		if (enable)
			val |= CFG_RC_DL_ACTIVE_MASK;
		else
			val &= ~CFG_RC_DL_ACTIVE_MASK;
		paxb_rc_cfg_write(core_idx, CFG_LINK_CAP_RC, val);
	}
}

static void paxb_cfg_LTR(int enable)
{
	uint32_t val, core_idx;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		val = paxb_rc_cfg_read(core_idx, CFG_ROOT_CAP_RC);
		if (enable)
			val |= CFG_ROOT_CAP_LTR_MASK;
		else
			val &= ~CFG_ROOT_CAP_LTR_MASK;
		paxb_rc_cfg_write(core_idx, CFG_ROOT_CAP_RC, val);
	}
}

static void paxb_ib_regs_bypass(void)
{
	unsigned int i, j;

	for (i = 0; i < paxb->num_cores; i++) {
		if (!pcie_core_needs_enable(i))
			continue;

		/* Configure Default IMAP window */
		mmio_write_32(PAXB_OFFSET(i) + PAXB_0_DEFAULT_IMAP,
			      DEFAULT_ADDR_INVALID);
		mmio_write_32(PAXB_OFFSET(i) + PAXB_0_DEFAULT_IMAP_AXUSER,
			      IMAP_AXUSER);
		mmio_write_32(PAXB_OFFSET(i) + PAXB_0_DEFAULT_IMAP_AXCACHE,
			      IMAP_AXCACHE);

		/* Configure MSI IMAP window */
		mmio_setbits_32(PAXB_OFFSET(i) +
				PAXB_IMAP0_REGS_TYPE_OFFSET,
				0x1);
		mmio_write_32(PAXB_OFFSET(i) + PAXB_IARR0_BASE_OFFSET,
			      GITS_TRANSLATER | OARR_VALID);
		for (j = 0; j < PAXB_MAX_IMAP_WINDOWS; j++) {
			mmio_write_32(PAXB_OFFSET(i) + PAXB_IMAP0_OFFSET(j),
				      (GITS_TRANSLATER +
				       (j * PAXB_IMAP0_WINDOW_SIZE)) |
				      IMAP_VALID);
		}
	}
}

static void paxb_ib_regs_init(void)
{
	unsigned int core_idx;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		/* initialize IARR2 to zero */
		mmio_write_32(PAXB_OFFSET(core_idx) + PAXB_IARR2_LOWER_OFFSET,
			      0x0);
		mmio_setbits_32(PAXB_OFFSET(core_idx) +
				PAXB_IMAP0_REGS_TYPE_OFFSET,
				0x1);
	}
}

static void paxb_cfg_apb_timeout(void)
{
	unsigned int core_idx;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		/* allow unlimited timeout */
		mmio_write_32(PAXB_OFFSET(core_idx) +
			PAXB_APB_TIMEOUT_COUNT_OFFSET,
			0xFFFFFFFF);
	}
}

static void paxb_smmu_cfg(void)
{
	unsigned int core_idx;
	uint32_t offset;
	uint32_t val;

	for (core_idx = 0; core_idx < paxb->num_cores; core_idx++) {
		if (!pcie_core_needs_enable(core_idx))
			continue;

		offset = core_idx * PCIE_CORE_PWR_OFFSET;
		val = mmio_read_32(PCIE_PAXB_SMMU_SID_CFG + offset);
		val &= ~(0xFFF00);
		val |= (PAXB_SMMU_SID_CFG_FUN_WIDTH |
			PAXB_SMMU_SID_CFG_DEV_WIDTH |
			PAXB_SMMU_SID_CFG_BUS_WIDTH);
		mmio_write_32(PCIE_PAXB_SMMU_SID_CFG + offset, val);
		val = mmio_read_32(PCIE_PAXB_SMMU_SID_CFG + offset);
		VERBOSE("smmu cfg reg 0x%x\n", val);
	}
}

static void paxb_cfg_coherency(void)
{
	unsigned int i, j;

	for (i = 0; i < paxb->num_cores; i++) {
		if (!pcie_core_needs_enable(i))
			continue;

#ifdef USE_DDR
		mmio_write_32(PAXB_OFFSET(i) + PAXB_IMAP2_OFFSET,
			      IMAP_ARCACHE | IMAP_AWCACHE);
#endif

		mmio_write_32(PAXB_OFFSET(i) + PAXB_IMAP0_0_AXUSER_OFFSET,
			      IMAP_AXUSER);

		mmio_write_32(PAXB_OFFSET(i) + PAXB_IMAP2_AXUSER_OFFSET,
			      IMAP_AXUSER);

		for (j = 0; j < PAXB_MAX_IMAP_WINDOWS; j++) {
#ifdef USE_DDR
			mmio_write_32(PAXB_OFFSET(i) + PAXB_IMAP3_OFFSET(j),
				      IMAP_ARCACHE | IMAP_AWCACHE);
			mmio_write_32(PAXB_OFFSET(i) + PAXB_IMAP4_OFFSET(j),
				      IMAP_ARCACHE | IMAP_AWCACHE);
#endif
			/* zero out IMAP0 mapping windows for MSI/MSI-X */
			mmio_write_32(PAXB_OFFSET(i) + PAXB_IMAP0_OFFSET(j),
				      0x0);

			mmio_write_32(PAXB_OFFSET(i) +
				      PAXB_IMAP3_0_AXUSER_OFFSET(j),
				      IMAP_AXUSER);
			mmio_write_32(PAXB_OFFSET(i) +
				      PAXB_IMAP4_0_AXUSER_OFFSET(j),
				      IMAP_AXUSER);
		}
	}
}

/*
 * This function configures all PAXB related blocks to allow non-secure access
 */
void paxb_ns_init(enum paxb_type type)
{
	unsigned int reg;

	switch (type) {
	case PAXB_SR:
		for (reg = 0; reg < ARRAY_SIZE(paxb_sec_reg_offset); reg++) {

			mmio_setbits_32(SR_PCIE_NIC_SECURITY_BASE +
					paxb_sec_reg_offset[reg], 0x1);
		}
	/* Enabled all PAXB's relevant IDM blocks access in non-secure mode */
	mmio_setbits_32(SR_PCIE_NIC_SECURITY_BASE + PAXB_SECURITY_IDM_OFFSET,
			0xffff);
		break;
	case PAXB_NS3Z:
		mmio_setbits_32(NS3Z_PCIE_NIC_SECURITY_BASE +
				paxb_sec_reg_offset[0], 0x1);
		mmio_setbits_32(NS3Z_PCIE_NIC_SECURITY_BASE +
				PAXB_SECURITY_IDM_OFFSET, 0xffff);
		mmio_setbits_32(NS3Z_PCIE_NIC_SECURITY_BASE +
				PAXB_SECURITY_APB_OFFSET, 0x7);
		mmio_setbits_32(NS3Z_PCIE_NIC_SECURITY_BASE +
				PAXB_SECURITY_ECAM_OFFSET, 0x1);
		break;
	}
}

static int paxb_set_config(void)
{
	paxb = paxb_get_config(sr);
	if (paxb)
		return 0;

	return -ENODEV;
}

void paxb_init(void)
{
	int ret;

	ret = paxb_set_config();
	if (ret)
		return;

	paxb_ns_init(paxb->type);

	ret = pcie_cores_init();
	if (ret)
		return;

	if (paxb->phy_init) {
		ret = paxb->phy_init();
		if (ret)
			return;
	}

	paxb_cfg_dev_id();
	paxb_cfg_tgt_trn();
	paxb_cfg_pdl_ctrl();
	if (paxb->type == PAXB_SR) {
		paxb_ib_regs_init();
		paxb_cfg_coherency();
	} else
		paxb_ib_regs_bypass();

	paxb_cfg_apb_timeout();
	paxb_smmu_cfg();
	paxb_cfg_clkreq();
	paxb_rc_link_init();

	/* Stingray Doesn't support LTR */
	paxb_cfg_LTR(false);
	paxb_cfg_dl_active(true);

	paxb_cfg_mps();

#ifdef PAXB_LINKUP
	paxb_start_link_up();
#endif
	INFO("PAXB init done\n");
}