Skip to content

Commit ee4747e

Browse files
committed
pci: move pcie_retraining_link in boot_x86_fsp
the function relies a non-general delay() function, so move the function in a more target-specific file.
1 parent 53d012f commit ee4747e

File tree

3 files changed

+70
-79
lines changed

3 files changed

+70
-79
lines changed

include/pci.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
#define PCIE_LINK_CONTROL_OFF (0x10)
8282
#define PCIE_LINK_STATUS_TRAINING (1 << 11)
8383
#define PCIE_LINK_CONTROL_RETRAINING (1 << 5)
84-
#define PCIE_TRAINING_TIMEOUT_MS (100)
8584
typedef struct {
8685
int bus;
8786
int device;
@@ -124,7 +123,6 @@ uint32_t pci_enum_bus(uint8_t bus, struct pci_enum_info *info);
124123

125124
int pci_enum_do(void);
126125
int pci_pre_enum(void);
127-
int pcie_retraining_link(uint8_t bus, uint8_t dev, uint8_t fun);
128126
void pci_dump_config_space(void);
129127

130128
#ifdef __cplusplus

src/boot_x86_fsp.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const uint8_t __attribute__((section(".sig_wolfboot_raw")))
8686
#define ENDLINE "\r\n"
8787

8888
#define PCI_DEVICE_CONTROLLER_TO_PEX 0x6
89+
#define PCIE_TRAINING_TIMEOUT_MS (100)
8990

9091
typedef uint32_t (*memory_init_cb)(void *udp, struct efi_hob **HobList);
9192
typedef uint32_t (*temp_ram_exit_cb)(void *udp);
@@ -400,6 +401,75 @@ static void print_fsp_image_revision(struct fsp_info_header *h)
400401
wolfBoot_printf("%x.%x.%x build %x\r\n", maj, min, rev, build);
401402
}
402403

404+
static int pci_get_capability(uint8_t bus, uint8_t dev, uint8_t fun,
405+
uint8_t cap_id, uint8_t *cap_off)
406+
{
407+
uint8_t r8, id;
408+
uint32_t r32;
409+
410+
r32 = pci_config_read16(bus, dev, fun, PCI_STATUS_OFFSET);
411+
if (!(r32 & PCI_STATUS_CAP_LIST))
412+
return -1;
413+
r8 = pci_config_read8(bus, dev, fun, PCI_CAP_OFFSET);
414+
while (r8 != 0) {
415+
id = pci_config_read8(bus, dev, fun, r8);
416+
if (id == cap_id) {
417+
*cap_off = r8;
418+
return 0;
419+
}
420+
r8 = pci_config_read8(bus, dev, fun, r8 + 1);
421+
}
422+
return -1;
423+
}
424+
425+
int pcie_retraining_link(uint8_t bus, uint8_t dev, uint8_t fun)
426+
{
427+
uint16_t link_status, link_control, vid;
428+
uint8_t pcie_cap_off;
429+
int ret, tries;
430+
431+
vid = pci_config_read16(bus, dev, 0, PCI_VENDOR_ID_OFFSET);
432+
if (vid == 0xffff) {
433+
return -1;
434+
}
435+
436+
ret = pci_get_capability(bus, dev, fun, PCI_PCIE_CAP_ID, &pcie_cap_off);
437+
if (ret != 0) {
438+
return -1;
439+
}
440+
441+
link_status = pci_config_read16(bus, dev, fun,
442+
pcie_cap_off + PCIE_LINK_STATUS_OFF);
443+
if (link_status & PCIE_LINK_STATUS_TRAINING) {
444+
delay(PCIE_TRAINING_TIMEOUT_MS);
445+
link_status = pci_config_read16(bus, dev, fun,
446+
pcie_cap_off + PCIE_LINK_STATUS_OFF);
447+
if (link_status & PCIE_LINK_STATUS_TRAINING) {
448+
return -1;
449+
}
450+
}
451+
452+
link_control = pci_config_read16(bus, dev, fun,
453+
pcie_cap_off + PCIE_LINK_CONTROL_OFF);
454+
link_control |= PCIE_LINK_CONTROL_RETRAINING;
455+
pci_config_write16(bus, dev, fun, pcie_cap_off + PCIE_LINK_CONTROL_OFF,
456+
link_control);
457+
tries = PCIE_TRAINING_TIMEOUT_MS / 10;
458+
do {
459+
link_status = pci_config_read16(bus, dev, fun,
460+
pcie_cap_off + PCIE_LINK_STATUS_OFF);
461+
if (!(link_status & PCIE_LINK_STATUS_TRAINING))
462+
break;
463+
delay(10);
464+
} while(tries--);
465+
466+
if ((link_status & PCIE_LINK_STATUS_TRAINING)) {
467+
return -1;
468+
}
469+
470+
return 0;
471+
}
472+
403473
/*!
404474
* \brief Staging of FSP_S after verification
405475
*

src/pci.c

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -775,83 +775,6 @@ uint32_t pci_enum_bus(uint8_t bus, struct pci_enum_info *info)
775775
return 0;
776776
}
777777

778-
static int pci_get_capability(uint8_t bus, uint8_t dev, uint8_t fun,
779-
uint8_t cap_id, uint8_t *cap_off)
780-
{
781-
uint8_t r8, id;
782-
uint32_t r32;
783-
784-
r32 = pci_config_read16(bus, dev, fun, PCI_STATUS_OFFSET);
785-
if (!(r32 & PCI_STATUS_CAP_LIST))
786-
return -1;
787-
r8 = pci_config_read8(bus, dev, fun, PCI_CAP_OFFSET);
788-
while (r8 != 0) {
789-
id = pci_config_read8(bus, dev, fun, r8);
790-
if (id == cap_id) {
791-
*cap_off = r8;
792-
return 0;
793-
}
794-
r8 = pci_config_read8(bus, dev, fun, r8 + 1);
795-
}
796-
return -1;
797-
}
798-
799-
int pcie_retraining_link(uint8_t bus, uint8_t dev, uint8_t fun)
800-
{
801-
uint16_t link_status, link_control, vid;
802-
uint8_t pcie_cap_off;
803-
int ret, tries;
804-
805-
PCI_DEBUG_PRINTF("retraining link: %x:%x.%x\r\n", bus, dev, fun);
806-
vid = pci_config_read16(bus, dev, 0, PCI_VENDOR_ID_OFFSET);
807-
if (vid == 0xffff) {
808-
PCI_DEBUG_PRINTF("can't find dev: %x:%x.%d\r\n", bus, dev, fun);
809-
return -1;
810-
}
811-
812-
ret = pci_get_capability(bus, dev, fun, PCI_PCIE_CAP_ID, &pcie_cap_off);
813-
if (ret != 0) {
814-
PCI_DEBUG_PRINTF("can't find PCIE cap pointer\r\n");
815-
return -1;
816-
}
817-
818-
PCI_DEBUG_PRINTF("pcie cap off: 0x%x\r\n", pcie_cap_off);
819-
link_status = pci_config_read16(bus, dev, fun,
820-
pcie_cap_off + PCIE_LINK_STATUS_OFF);
821-
if (link_status & PCIE_LINK_STATUS_TRAINING) {
822-
PCI_DEBUG_PRINTF("link already training, waiting...\r\n");
823-
delay(PCIE_TRAINING_TIMEOUT_MS);
824-
link_status = pci_config_read16(bus, dev, fun,
825-
pcie_cap_off + PCIE_LINK_STATUS_OFF);
826-
if (link_status & PCIE_LINK_STATUS_TRAINING) {
827-
PCI_DEBUG_PRINTF("link training error: timeout\r\n");
828-
return -1;
829-
}
830-
}
831-
832-
link_control = pci_config_read16(bus, dev, fun,
833-
pcie_cap_off + PCIE_LINK_CONTROL_OFF);
834-
link_control |= PCIE_LINK_CONTROL_RETRAINING;
835-
pci_config_write16(bus, dev, fun, pcie_cap_off + PCIE_LINK_CONTROL_OFF,
836-
link_control);
837-
tries = PCIE_TRAINING_TIMEOUT_MS / 10;
838-
do {
839-
link_status = pci_config_read16(bus, dev, fun,
840-
pcie_cap_off + PCIE_LINK_STATUS_OFF);
841-
if (!(link_status & PCIE_LINK_STATUS_TRAINING))
842-
break;
843-
delay(10);
844-
} while(tries--);
845-
846-
if ((link_status & PCIE_LINK_STATUS_TRAINING)) {
847-
PCI_DEBUG_PRINTF("Timeout reached during retraining\r\n");
848-
return -1;
849-
}
850-
851-
PCI_DEBUG_PRINTF("retraining complete\r\n");
852-
return 0;
853-
}
854-
855778
int pci_pre_enum(void)
856779
{
857780
uint32_t reg;

0 commit comments

Comments
 (0)