X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=drivers%2Fusb%2Fhost%2Fdwc2.c;h=eee60a2b039a247b8c1282d1c2e92294a2377ae8;hb=82b9143bf8e5b359f094b90a19feb50a87268e8a;hp=189f6548f25ff2141d844fde289f169dc7a9fbed;hpb=ee837554011a4f0db6f246ee67f7c1d1161694e9;p=u-boot diff --git a/drivers/usb/host/dwc2.c b/drivers/usb/host/dwc2.c index 189f6548f2..eee60a2b03 100644 --- a/drivers/usb/host/dwc2.c +++ b/drivers/usb/host/dwc2.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -27,7 +28,6 @@ DEFINE_ALIGN_BUFFER(uint8_t, status_buffer, DWC2_STATUS_BUF_SIZE, 8); #define MAX_DEVICE 16 #define MAX_ENDPOINT 16 static int bulk_data_toggle[MAX_DEVICE][MAX_ENDPOINT]; -static int control_data_toggle[MAX_DEVICE][MAX_ENDPOINT]; static int root_hub_devnum; @@ -398,15 +398,18 @@ static void dwc_otg_core_init(struct dwc2_core_regs *regs) * @param hc Information needed to initialize the host channel */ static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num, - uint8_t dev_addr, uint8_t ep_num, uint8_t ep_is_in, - uint8_t ep_type, uint16_t max_packet) + struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num, + uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet) { struct dwc2_hc_regs *hc_regs = ®s->hc_regs[hc_num]; - const uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) | - (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) | - (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) | - (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) | - (max_packet << DWC2_HCCHAR_MPS_OFFSET); + uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) | + (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) | + (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) | + (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) | + (max_packet << DWC2_HCCHAR_MPS_OFFSET); + + if (dev->speed == USB_SPEED_LOW) + hcchar |= DWC2_HCCHAR_LSPDDEV; /* Clear old interrupt conditions for this host channel. */ writel(0x3fff, &hc_regs->hcint); @@ -463,7 +466,11 @@ static int dwc_otg_submit_rh_msg_in_status(struct usb_device *dev, void *buffer, if (hprt0 & DWC2_HPRT0_PRTPWR) port_status |= USB_PORT_STAT_POWER; - port_status |= USB_PORT_STAT_HIGH_SPEED; + if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW) + port_status |= USB_PORT_STAT_LOW_SPEED; + else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == + DWC2_HPRT0_PRTSPD_HIGH) + port_status |= USB_PORT_STAT_HIGH_SPEED; if (hprt0 & DWC2_HPRT0_PRTENCHNG) port_change |= USB_PORT_STAT_C_ENABLE; @@ -704,10 +711,9 @@ static int dwc_otg_submit_rh_msg(struct usb_device *dev, unsigned long pipe, return stat; } -int wait_for_chhltd(uint32_t *sub, int *toggle) +int wait_for_chhltd(uint32_t *sub, int *toggle, bool ignore_ack) { - const uint32_t hcint_comp_hlt_ack = DWC2_HCINT_XFERCOMP | - DWC2_HCINT_CHHLTD | DWC2_HCINT_ACK; + uint32_t hcint_comp_hlt_ack = DWC2_HCINT_XFERCOMP | DWC2_HCINT_CHHLTD; struct dwc2_hc_regs *hc_regs = ®s->hc_regs[DWC2_HC_CHANNEL]; int ret; uint32_t hcint, hctsiz; @@ -717,6 +723,12 @@ int wait_for_chhltd(uint32_t *sub, int *toggle) return ret; hcint = readl(&hc_regs->hcint); + if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN)) + return -EAGAIN; + if (ignore_ack) + hcint &= ~DWC2_HCINT_ACK; + else + hcint_comp_hlt_ack |= DWC2_HCINT_ACK; if (hcint != hcint_comp_hlt_ack) { debug("%s: Error (HCINT=%08x)\n", __func__, hcint); return -EINVAL; @@ -725,11 +737,9 @@ int wait_for_chhltd(uint32_t *sub, int *toggle) hctsiz = readl(&hc_regs->hctsiz); *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >> DWC2_HCTSIZ_XFERSIZE_OFFSET; - if (toggle) - *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> - DWC2_HCTSIZ_PID_OFFSET; + *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET; - debug("%s: sub=%u toggle=%d\n", __func__, *sub, toggle ? *toggle : -1); + debug("%s: sub=%u toggle=%d\n", __func__, *sub, *toggle); return 0; } @@ -742,7 +752,7 @@ static int dwc2_eptype[] = { }; int chunk_msg(struct usb_device *dev, unsigned long pipe, int *pid, int in, - void *buffer, int len) + void *buffer, int len, bool ignore_ack) { struct dwc2_hc_regs *hc_regs = ®s->hc_regs[DWC2_HC_CHANNEL]; int devnum = usb_pipedevice(pipe); @@ -750,7 +760,7 @@ int chunk_msg(struct usb_device *dev, unsigned long pipe, int *pid, int in, int max = usb_maxpacket(dev, pipe); int eptype = dwc2_eptype[usb_pipetype(pipe)]; int done = 0; - int ret; + int ret = 0; uint32_t sub; uint32_t xfer_len; uint32_t num_packets; @@ -759,24 +769,18 @@ int chunk_msg(struct usb_device *dev, unsigned long pipe, int *pid, int in, debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid, in, len); - if (len > DWC2_DATA_BUF_SIZE) { - printf("%s: %d is more then available buffer size (%d)\n", - __func__, len, DWC2_DATA_BUF_SIZE); - dev->status = 0; - dev->act_len = 0; - return -EINVAL; - } - do { /* Initialize channel */ - dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, devnum, ep, in, eptype, - max); + dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in, + eptype, max); xfer_len = len - done; - /* Make sure that xfer_len is a multiple of max packet size. */ if (xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE) xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE - max + 1; + if (xfer_len > DWC2_DATA_BUF_SIZE) + xfer_len = DWC2_DATA_BUF_SIZE - max + 1; + /* Make sure that xfer_len is a multiple of max packet size. */ if (xfer_len > 0) { num_packets = (xfer_len + max - 1) / max; if (num_packets > CONFIG_DWC2_MAX_PACKET_COUNT) { @@ -798,8 +802,11 @@ int chunk_msg(struct usb_device *dev, unsigned long pipe, int *pid, int in, (*pid << DWC2_HCTSIZ_PID_OFFSET), &hc_regs->hctsiz); - memcpy(aligned_buffer, (char *)buffer + done, len - done); - writel((uint32_t)aligned_buffer, &hc_regs->hcdma); + if (!in) + memcpy(aligned_buffer, (char *)buffer + done, len); + + writel(phys_to_bus((unsigned long)aligned_buffer), + &hc_regs->hcdma); /* Set host channel enable after all other setup is complete. */ clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK | @@ -807,22 +814,20 @@ int chunk_msg(struct usb_device *dev, unsigned long pipe, int *pid, int in, (1 << DWC2_HCCHAR_MULTICNT_OFFSET) | DWC2_HCCHAR_CHEN); - ret = wait_for_chhltd(&sub, pid); - if (ret) { - stop_transfer = 1; + ret = wait_for_chhltd(&sub, pid, ignore_ack); + if (ret) break; - } - done += xfer_len; if (in) { - done -= sub; + xfer_len -= sub; + memcpy(buffer + done, aligned_buffer, xfer_len); if (sub) stop_transfer = 1; } - } while ((done < len) && !stop_transfer); - if (done && in) - memcpy(buffer, aligned_buffer, done); + done += xfer_len; + + } while ((done < len) && !stop_transfer); writel(0, &hc_regs->hcintmsk); writel(0xFFFFFFFF, &hc_regs->hcint); @@ -830,7 +835,7 @@ int chunk_msg(struct usb_device *dev, unsigned long pipe, int *pid, int in, dev->status = 0; dev->act_len = done; - return 0; + return ret; } /* U-Boot USB transmission interface */ @@ -846,14 +851,13 @@ int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer, } return chunk_msg(dev, pipe, &bulk_data_toggle[devnum][ep], - usb_pipein(pipe), buffer, len); + usb_pipein(pipe), buffer, len, true); } int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, int len, struct devrequest *setup) { int devnum = usb_pipedevice(pipe); - int ep = usb_pipeendpoint(pipe); int pid, ret, act_len; /* For CONTROL endpoint pid should start with DATA1 */ int status_direction; @@ -865,14 +869,14 @@ int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, } pid = DWC2_HC_PID_SETUP; - ret = chunk_msg(dev, pipe, &pid, 0, setup, 8); + ret = chunk_msg(dev, pipe, &pid, 0, setup, 8, true); if (ret) return ret; if (buffer) { - control_data_toggle[devnum][ep] = DWC2_HC_PID_DATA1; - ret = chunk_msg(dev, pipe, &control_data_toggle[devnum][ep], - usb_pipein(pipe), buffer, len); + pid = DWC2_HC_PID_DATA1; + ret = chunk_msg(dev, pipe, &pid, usb_pipein(pipe), buffer, + len, false); if (ret) return ret; act_len = dev->act_len; @@ -887,7 +891,8 @@ int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, status_direction = 0; pid = DWC2_HC_PID_DATA1; - ret = chunk_msg(dev, pipe, &pid, status_direction, status_buffer, 0); + ret = chunk_msg(dev, pipe, &pid, status_direction, status_buffer, 0, + false); if (ret) return ret; @@ -899,9 +904,21 @@ int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer, int len, int interval) { - printf("dev = %p pipe = %#lx buf = %p size = %d int = %d\n", - dev, pipe, buffer, len, interval); - return -ENOSYS; + unsigned long timeout; + int ret; + + /* FIXME: what is interval? */ + + timeout = get_timer(0) + USB_TIMEOUT_MS(pipe); + for (;;) { + if (get_timer(0) > timeout) { + printf("Timeout poll on interrupt endpoint\n"); + return -ETIMEDOUT; + } + ret = submit_bulk_msg(dev, pipe, buffer, len); + if (ret != -EAGAIN) + return ret; + } } /* U-Boot USB control interface */ @@ -915,7 +932,8 @@ int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) snpsid = readl(®s->gsnpsid); printf("Core Release: %x.%03x\n", snpsid >> 12 & 0xf, snpsid & 0xfff); - if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx) { + if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx && + (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) { printf("SNPSID invalid (not DWC2 OTG device): %08x\n", snpsid); return -ENODEV; } @@ -933,10 +951,8 @@ int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) DWC2_HPRT0_PRTRST); for (i = 0; i < MAX_DEVICE; i++) { - for (j = 0; j < MAX_ENDPOINT; j++) { - control_data_toggle[i][j] = DWC2_HC_PID_DATA1; + for (j = 0; j < MAX_ENDPOINT; j++) bulk_data_toggle[i][j] = DWC2_HC_PID_DATA0; - } } return 0;