176
176
(PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
177
177
PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
178
178
179
- #define PIO_TIMEOUT_MS 1
179
+ #define PIO_RETRY_CNT 500
180
+ #define PIO_RETRY_DELAY 2 /* 2 us*/
180
181
181
182
#define LINK_WAIT_MAX_RETRIES 10
182
183
#define LINK_WAIT_USLEEP_MIN 90000
183
184
#define LINK_WAIT_USLEEP_MAX 100000
185
+ #define RETRAIN_WAIT_MAX_RETRIES 10
186
+ #define RETRAIN_WAIT_USLEEP_US 2000
184
187
185
188
#define MSI_IRQ_NUM 32
186
189
@@ -240,6 +243,17 @@ static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
240
243
return - ETIMEDOUT ;
241
244
}
242
245
246
+ static void advk_pcie_wait_for_retrain (struct advk_pcie * pcie )
247
+ {
248
+ size_t retries ;
249
+
250
+ for (retries = 0 ; retries < RETRAIN_WAIT_MAX_RETRIES ; ++ retries ) {
251
+ if (!advk_pcie_link_up (pcie ))
252
+ break ;
253
+ udelay (RETRAIN_WAIT_USLEEP_US );
254
+ }
255
+ }
256
+
243
257
static void advk_pcie_setup_hw (struct advk_pcie * pcie )
244
258
{
245
259
u32 reg ;
@@ -325,6 +339,14 @@ static void advk_pcie_setup_hw(struct advk_pcie *pcie)
325
339
reg |= PIO_CTRL_ADDR_WIN_DISABLE ;
326
340
advk_writel (pcie , reg , PIO_CTRL );
327
341
342
+ /*
343
+ * PERST# signal could have been asserted by pinctrl subsystem before
344
+ * probe() callback has been called, making the endpoint going into
345
+ * fundamental reset. As required by PCI Express spec a delay for at
346
+ * least 100ms after such a reset before link training is needed.
347
+ */
348
+ msleep (PCI_PM_D3COLD_WAIT );
349
+
328
350
/* Start link training */
329
351
reg = advk_readl (pcie , PCIE_CORE_LINK_CTRL_STAT_REG );
330
352
reg |= PCIE_CORE_LINK_TRAINING ;
@@ -384,17 +406,16 @@ static void advk_pcie_check_pio_status(struct advk_pcie *pcie)
384
406
static int advk_pcie_wait_pio (struct advk_pcie * pcie )
385
407
{
386
408
struct device * dev = & pcie -> pdev -> dev ;
387
- unsigned long timeout ;
409
+ int i ;
388
410
389
- timeout = jiffies + msecs_to_jiffies (PIO_TIMEOUT_MS );
390
-
391
- while (time_before (jiffies , timeout )) {
411
+ for (i = 0 ; i < PIO_RETRY_CNT ; i ++ ) {
392
412
u32 start , isr ;
393
413
394
414
start = advk_readl (pcie , PIO_START );
395
415
isr = advk_readl (pcie , PIO_ISR );
396
416
if (!start && isr )
397
417
return 0 ;
418
+ udelay (PIO_RETRY_DELAY );
398
419
}
399
420
400
421
dev_err (dev , "config read/write timed out\n" );
@@ -416,7 +437,7 @@ advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
416
437
417
438
case PCI_EXP_RTCTL : {
418
439
u32 val = advk_readl (pcie , PCIE_ISR0_MASK_REG );
419
- * value = (val & PCIE_MSG_PM_PME_MASK ) ? PCI_EXP_RTCTL_PMEIE : 0 ;
440
+ * value = (val & PCIE_MSG_PM_PME_MASK ) ? 0 : PCI_EXP_RTCTL_PMEIE ;
420
441
return PCI_BRIDGE_EMUL_HANDLED ;
421
442
}
422
443
@@ -427,11 +448,20 @@ advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
427
448
return PCI_BRIDGE_EMUL_HANDLED ;
428
449
}
429
450
451
+ case PCI_EXP_LNKCTL : {
452
+ /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */
453
+ u32 val = advk_readl (pcie , PCIE_CORE_PCIEXP_CAP + reg ) &
454
+ ~(PCI_EXP_LNKSTA_LT << 16 );
455
+ if (!advk_pcie_link_up (pcie ))
456
+ val |= (PCI_EXP_LNKSTA_LT << 16 );
457
+ * value = val ;
458
+ return PCI_BRIDGE_EMUL_HANDLED ;
459
+ }
460
+
430
461
case PCI_CAP_LIST_ID :
431
462
case PCI_EXP_DEVCAP :
432
463
case PCI_EXP_DEVCTL :
433
464
case PCI_EXP_LNKCAP :
434
- case PCI_EXP_LNKCTL :
435
465
* value = advk_readl (pcie , PCIE_CORE_PCIEXP_CAP + reg );
436
466
return PCI_BRIDGE_EMUL_HANDLED ;
437
467
default :
@@ -448,14 +478,24 @@ advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
448
478
449
479
switch (reg ) {
450
480
case PCI_EXP_DEVCTL :
481
+ advk_writel (pcie , new , PCIE_CORE_PCIEXP_CAP + reg );
482
+ break ;
483
+
451
484
case PCI_EXP_LNKCTL :
452
485
advk_writel (pcie , new , PCIE_CORE_PCIEXP_CAP + reg );
486
+ if (new & PCI_EXP_LNKCTL_RL )
487
+ advk_pcie_wait_for_retrain (pcie );
453
488
break ;
454
489
455
- case PCI_EXP_RTCTL :
456
- new = (new & PCI_EXP_RTCTL_PMEIE ) << 3 ;
457
- advk_writel (pcie , new , PCIE_ISR0_MASK_REG );
490
+ case PCI_EXP_RTCTL : {
491
+ /* Only mask/unmask PME interrupt */
492
+ u32 val = advk_readl (pcie , PCIE_ISR0_MASK_REG ) &
493
+ ~PCIE_MSG_PM_PME_MASK ;
494
+ if ((new & PCI_EXP_RTCTL_PMEIE ) == 0 )
495
+ val |= PCIE_MSG_PM_PME_MASK ;
496
+ advk_writel (pcie , val , PCIE_ISR0_MASK_REG );
458
497
break ;
498
+ }
459
499
460
500
case PCI_EXP_RTSTA :
461
501
new = (new & PCI_EXP_RTSTA_PME ) >> 9 ;
@@ -480,18 +520,20 @@ static void advk_sw_pci_bridge_init(struct advk_pcie *pcie)
480
520
{
481
521
struct pci_bridge_emul * bridge = & pcie -> bridge ;
482
522
483
- bridge -> conf .vendor = advk_readl (pcie , PCIE_CORE_DEV_ID_REG ) & 0xffff ;
484
- bridge -> conf .device = advk_readl (pcie , PCIE_CORE_DEV_ID_REG ) >> 16 ;
523
+ bridge -> conf .vendor =
524
+ cpu_to_le16 (advk_readl (pcie , PCIE_CORE_DEV_ID_REG ) & 0xffff );
525
+ bridge -> conf .device =
526
+ cpu_to_le16 (advk_readl (pcie , PCIE_CORE_DEV_ID_REG ) >> 16 );
485
527
bridge -> conf .class_revision =
486
- advk_readl (pcie , PCIE_CORE_DEV_REV_REG ) & 0xff ;
528
+ cpu_to_le32 ( advk_readl (pcie , PCIE_CORE_DEV_REV_REG ) & 0xff ) ;
487
529
488
530
/* Support 32 bits I/O addressing */
489
531
bridge -> conf .iobase = PCI_IO_RANGE_TYPE_32 ;
490
532
bridge -> conf .iolimit = PCI_IO_RANGE_TYPE_32 ;
491
533
492
534
/* Support 64 bits memory pref */
493
- bridge -> conf .pref_mem_base = PCI_PREF_RANGE_TYPE_64 ;
494
- bridge -> conf .pref_mem_limit = PCI_PREF_RANGE_TYPE_64 ;
535
+ bridge -> conf .pref_mem_base = cpu_to_le16 ( PCI_PREF_RANGE_TYPE_64 ) ;
536
+ bridge -> conf .pref_mem_limit = cpu_to_le16 ( PCI_PREF_RANGE_TYPE_64 ) ;
495
537
496
538
/* Support interrupt A for MSI feature */
497
539
bridge -> conf .intpin = PCIE_CORE_INT_A_ASSERT_ENABLE ;
0 commit comments