Skip to content

Commit

Permalink
proper initialization to work with short packets
Browse files Browse the repository at this point in the history
  • Loading branch information
sergforce committed Jan 15, 2019
1 parent 9b219ff commit da900c7
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions libusb3380.c
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,7 @@ int usb3380_init_root_complex(libusb3380_context_t* ctx,
{
int res;
uint32_t reg;
unsigned i;
bool reinit = false;

/* Check intersection of BAR2 and BAR3 spaces */
Expand Down Expand Up @@ -1385,13 +1386,31 @@ int usb3380_init_root_complex(libusb3380_context_t* ctx,
return res;
}
#endif
// Check and reset dedicated EP states
const char* ep_names[6] = { "CSROUT", "CSRIN", "PCIOUT", "PCIIN", "STATIN",
"RCIN" };
for (i = 0; i < 6; i++) {
res = usb3380_csr_mm_cfg_read(ctx, DEP_RSP + i * 0x10, &reg);
if (res) {
return res;
}
LOG_DEBUG("EPd %9s: RSP:%08x Halt:%d Toggle:%d",
ep_names[i], reg, (reg & 1) ? 1 : 0, (reg & 2) ? 1 : 0);

res = usb3380_csr_mm_cfg_write(ctx, DEP_RSP + i * 0x10,
(1<<0) | (1<<2) | (1<<7));
if (res) {
return res;
}
}

res = usb3380_idxreg_read(ctx, CHIPREV, &reg);
if (res) {
return res;
}
LOG_DEBUG("Chip REV %08x", reg);

#if 0
res = usb3380_csr_mm_cfg_read(ctx, USBCTL, &reg);
if (res) {
return res;
Expand All @@ -1400,7 +1419,7 @@ int usb3380_init_root_complex(libusb3380_context_t* ctx,
(reg & USBSTAT_SS_MODE) ? 5000 :
(reg & USBSTAT_HS_MODE) ? 480 :
(reg & USBSTAT_FS_MODE) ? 12 : 0);

#endif

res = usb3380_idxreg_read(ctx, HS_INTPOLL_RATE, &reg);
if (res) {
Expand Down Expand Up @@ -1542,7 +1561,6 @@ int usb3380_init_root_complex(libusb3380_context_t* ctx,
LOG_DEBUG("INCH %8x", reg);
#endif
// Enabgle credit calculation only for requested GPEPs
unsigned i;
for (reg = 0, i = 0; i < 4; i++) {
if (cfg->gpep_fifo_in_size[i] == 0)
reg |= (1U << (28 + i));
Expand Down Expand Up @@ -1586,31 +1604,14 @@ int usb3380_init_root_complex(libusb3380_context_t* ctx,
return res;
}

// Check EP states
const char* ep_names[6] = { "CSROUT", "CSRIN", "PCIOUT", "PCIIN", "STATIN",
"RCIN" };
for (i = 0; i < 6; i++) {
res = usb3380_csr_mm_cfg_read(ctx, DEP_RSP + i * 0x10, &reg);
if (res) {
return res;
}
LOG_DEBUG("EPd %9s: %08x Halt:%d Toggle:%d",
ep_names[i], reg, (reg & 1) ? 1 : 0, (reg & 2) ? 1 : 0);

res = usb3380_csr_mm_cfg_write(ctx, DEP_RSP + i * 0x10, 0);
if (res) {
return res;
}
}

const char* gpep_names[11] = { "EP0", "GPEP0 OUT", "GPEP1 OUT", "GPEP2 OUT",
"GPEP3 OUT", NULL, NULL, "GPEP0 IN",
"GPEP1 IN", "GPEP2 IN", "GPEP3 IN" };
for (i = 0; i < 11; i++) {
if (i == 5 || i == 6)
continue;

uint32_t eprsp, avail;
uint32_t epcfg, eprsp, avail;
res = usb3380_csr_mm_cfg_read(ctx, EP_STAT + i * 0x20, &reg);
if (res) {
return res;
Expand All @@ -1624,17 +1625,31 @@ int usb3380_init_root_complex(libusb3380_context_t* ctx,
return res;
}

LOG_DEBUG("EPg %9s: %04x %08x %08x Pkts: %2d Avail:%5d Empty:%d Full:%d STALL:%d TO:%d",
LOG_DEBUG("EPg %9s: %04x RSP:%08x STAT:%08x Pkts: %2d Avail:%5d Empty:%d Full:%d STALL:%d TO:%d",
gpep_names[i], EP_CFG + i*0x20, eprsp, reg, (reg >> 24) & 0x1f, avail,
(reg & (1<<10)) ? 1 : 0, (reg & (1<<11)) ? 1 : 0,
(reg & (1<<20)) ? 1 : 0, (reg & (1<<21)) ? 1 : 0);

if (i == 1 || i == 3) { //GPEP0 & GPEP2
res = usb3380_csr_mm_cfg_read(ctx, EP_CFG + i * 0x20, &epcfg);
if (res) {
return res;
}

epcfg = epcfg | (0xfu << 24); // Max burst size
res = usb3380_csr_mm_cfg_write(ctx, EP_CFG + i * 0x20, epcfg);
if (res) {
return res;
}
}

res = usb3380_csr_mm_cfg_write(ctx, EP_STAT + i * 0x20, 1<<9);
if (res) {
return res;
}

res = usb3380_csr_mm_cfg_write(ctx, EP_RSP + i * 0x20, 0);
// Clear all response bits, except toggle
res = usb3380_csr_mm_cfg_write(ctx, EP_RSP + i * 0x20, 0xfd);
if (res) {
return res;
}
Expand Down

0 comments on commit da900c7

Please sign in to comment.