Lines Matching refs:hsotg
51 static inline void dwc2_set_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
53 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) | val, offset);
56 static inline void dwc2_clear_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
58 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) & ~val, offset);
61 static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
65 return hsotg->eps_in[ep_index];
67 return hsotg->eps_out[ep_index];
71 static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
75 * @hsotg: The driver state.
92 static inline bool using_dma(struct dwc2_hsotg *hsotg)
94 return hsotg->params.g_dma;
99 * @hsotg: The driver state.
103 static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
105 return hsotg->params.g_dma_desc;
117 struct dwc2_hsotg *hsotg = hs_ep->parent;
120 if (hsotg->gadget.speed != USB_SPEED_HIGH)
144 struct dwc2_hsotg *hsotg = hs_ep->parent;
147 if (hsotg->gadget.speed != USB_SPEED_HIGH)
158 * @hsotg: The device state
161 static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
163 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
169 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
170 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
176 * @hsotg: The device state
179 static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
181 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
187 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
192 * @hsotg: The device state
200 static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
212 daint = dwc2_readl(hsotg, DAINTMSK);
217 dwc2_writel(hsotg, daint, DAINTMSK);
224 * @hsotg: Programming view of the DWC_otg controller
226 int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
228 if (hsotg->hw_params.en_multiple_tx_fifo)
230 return hsotg->hw_params.num_dev_in_eps;
233 return hsotg->hw_params.num_dev_perio_in_ep;
240 * @hsotg: Programming view of the DWC_otg controller
242 int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
248 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
249 hsotg->params.g_np_tx_fifo_size);
252 tx_addr_max = hsotg->hw_params.total_fifo_size;
254 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
264 * @hsotg: Programming view of the DWC_otg controller
267 static void dwc2_gadget_wkup_alert_handler(struct dwc2_hsotg *hsotg)
272 gintsts2 = dwc2_readl(hsotg, GINTSTS2);
273 gintmsk2 = dwc2_readl(hsotg, GINTMSK2);
277 dev_dbg(hsotg->dev, "%s: Wkup_Alert_Int\n", __func__);
278 dwc2_set_bit(hsotg, GINTSTS2, GINTSTS2_WKUP_ALERT_INT);
279 dwc2_set_bit(hsotg, DCTL, DCTL_RMTWKUPSIG);
287 * @hsotg: Programming view of the DWC_otg controller
289 int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
294 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
296 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
306 * @hsotg: The device instance.
308 static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
315 u32 *txfsz = hsotg->params.g_tx_fifo_size;
318 WARN_ON(hsotg->fifo_map);
319 hsotg->fifo_map = 0;
322 dwc2_writel(hsotg, hsotg->params.g_rx_fifo_size, GRXFSIZ);
323 dwc2_writel(hsotg, (hsotg->params.g_rx_fifo_size <<
325 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
336 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
348 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
352 dwc2_writel(hsotg, val, DPTXFSIZN(ep));
353 val = dwc2_readl(hsotg, DPTXFSIZN(ep));
356 dwc2_writel(hsotg, hsotg->hw_params.total_fifo_size |
364 dwc2_writel(hsotg, GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
370 val = dwc2_readl(hsotg, GRSTCTL);
376 dev_err(hsotg->dev,
385 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
423 * @hsotg: The device state.
430 static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
436 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->map_dir);
442 * @hsotg: The device state.
447 static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
449 hsotg->setup_desc[0] =
450 dmam_alloc_coherent(hsotg->dev,
452 &hsotg->setup_desc_dma[0],
454 if (!hsotg->setup_desc[0])
457 hsotg->setup_desc[1] =
458 dmam_alloc_coherent(hsotg->dev,
460 &hsotg->setup_desc_dma[1],
462 if (!hsotg->setup_desc[1])
465 hsotg->ctrl_in_desc =
466 dmam_alloc_coherent(hsotg->dev,
468 &hsotg->ctrl_in_desc_dma,
470 if (!hsotg->ctrl_in_desc)
473 hsotg->ctrl_out_desc =
474 dmam_alloc_coherent(hsotg->dev,
476 &hsotg->ctrl_out_desc_dma,
478 if (!hsotg->ctrl_out_desc)
489 * @hsotg: The controller state.
503 static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
508 u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS);
522 if (periodic && !hsotg->dedicated_fifos) {
523 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
539 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
543 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
552 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
556 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
560 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
563 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
564 can_write = dwc2_readl(hsotg,
571 dev_dbg(hsotg->dev,
575 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
585 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
605 if (!hsotg->dedicated_fifos)
606 dwc2_hsotg_en_gsint(hsotg,
634 if (!hsotg->dedicated_fifos)
635 dwc2_hsotg_en_gsint(hsotg,
640 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
655 dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write);
701 * @hsotg: The device instance
705 static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
709 dsts = dwc2_readl(hsotg, DSTS);
913 struct dwc2_hsotg *hsotg = hs_ep->parent;
926 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
934 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
982 struct dwc2_hsotg *hsotg = hs_ep->parent;
994 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
1025 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
1027 ctrl = dwc2_readl(hsotg, depctl);
1029 dwc2_writel(hsotg, ctrl, depctl);
1033 static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
1040 * @hsotg: The controller state.
1048 static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
1067 dev_err(hsotg->dev, "%s: active request\n", __func__);
1071 dev_err(hsotg->dev,
1082 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
1083 __func__, dwc2_readl(hsotg, epctrl_reg), index,
1087 ctrl = dwc2_readl(hsotg, epctrl_reg);
1090 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
1095 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
1098 if (!using_desc_dma(hsotg))
1106 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
1143 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1149 if (using_desc_dma(hsotg)) {
1169 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
1171 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1175 dwc2_writel(hsotg, epsize, epsize_reg);
1177 if (using_dma(hsotg) && !continuing && (length != 0)) {
1183 dwc2_writel(hsotg, ureq->dma, dma_reg);
1185 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1202 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, -ENODATA);
1209 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
1212 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
1215 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
1216 dwc2_writel(hsotg, ctrl, epctrl_reg);
1226 if (dir_in && !using_dma(hsotg)) {
1230 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1239 if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA))
1240 dev_dbg(hsotg->dev,
1242 index, dwc2_readl(hsotg, epctrl_reg));
1244 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
1245 __func__, dwc2_readl(hsotg, epctrl_reg));
1248 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
1253 * @hsotg: The device state.
1263 static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
1270 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1277 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1283 static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
1290 if (!using_dma(hsotg) || !((long)req_buf & 3))
1295 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
1301 dev_err(hsotg->dev,
1316 dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1321 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1324 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1348 struct dwc2_hsotg *hsotg = hs_ep->parent;
1350 u32 current_frame = hsotg->frame_number;
1354 if (hsotg->gadget.speed != USB_SPEED_HIGH)
1369 * @hsotg: The driver state
1375 static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1378 switch (hsotg->ep0_state) {
1381 hs_ep->desc_list = hsotg->setup_desc[0];
1382 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1386 hs_ep->desc_list = hsotg->ctrl_in_desc;
1387 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1390 hs_ep->desc_list = hsotg->ctrl_out_desc;
1391 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1394 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1395 hsotg->ep0_state);
1557 struct dwc2_hsotg *hsotg = hs_ep->parent;
1559 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1566 * @hsotg: The driver state.
1572 static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
1581 if (idx > hsotg->num_of_eps)
1584 return index_to_ep(hsotg, idx, dir);
1589 * @hsotg: The driver state.
1593 int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
1595 int dctl = dwc2_readl(hsotg, DCTL);
1609 dwc2_writel(hsotg, dctl, DCTL);
1615 * @hsotg: The device state
1623 static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
1631 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1634 hsotg->ep0_reply = req;
1636 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1640 req->buf = hsotg->ep0_buff;
1654 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1663 * @hsotg: The device state
1666 static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
1669 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1675 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1678 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1684 status = hsotg->gadget.is_selfpowered <<
1686 status |= hsotg->remote_wakeup_allowed <<
1697 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1711 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
1713 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1744 struct dwc2_hsotg *hsotg = hs_ep->parent;
1750 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1757 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1760 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1767 * @hsotg: The device state
1770 static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
1773 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1783 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1795 hsotg->remote_wakeup_allowed = 1;
1797 hsotg->remote_wakeup_allowed = 0;
1806 hsotg->test_mode = wIndex >> 8;
1812 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1814 dev_err(hsotg->dev,
1821 ep = ep_from_windex(hsotg, wIndex);
1823 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1835 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1837 dev_err(hsotg->dev,
1857 spin_unlock(&hsotg->lock);
1860 spin_lock(&hsotg->lock);
1881 static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
1885 * @hsotg: The device state
1889 static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
1891 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1895 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1903 ctrl = dwc2_readl(hsotg, reg);
1906 dwc2_writel(hsotg, ctrl, reg);
1908 dev_dbg(hsotg->dev,
1910 ctrl, reg, dwc2_readl(hsotg, reg));
1916 dwc2_hsotg_enqueue_setup(hsotg);
1921 * @hsotg: The device state
1928 static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
1931 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1935 dev_dbg(hsotg->dev,
1942 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1945 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1948 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1954 hsotg->connected = 1;
1955 dcfg = dwc2_readl(hsotg, DCFG);
1959 dwc2_writel(hsotg, dcfg, DCFG);
1961 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1963 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1967 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
1972 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
1979 if (ret == 0 && hsotg->driver) {
1980 spin_unlock(&hsotg->lock);
1981 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1982 spin_lock(&hsotg->lock);
1984 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1987 hsotg->delayed_status = false;
1989 hsotg->delayed_status = true;
1997 dwc2_hsotg_stall_ep0(hsotg);
2012 struct dwc2_hsotg *hsotg = hs_ep->parent;
2015 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
2019 spin_lock(&hsotg->lock);
2021 dwc2_hsotg_enqueue_setup(hsotg);
2023 dwc2_hsotg_process_control(hsotg, req->buf);
2024 spin_unlock(&hsotg->lock);
2029 * @hsotg: The device state.
2034 static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
2036 struct usb_request *req = hsotg->ctrl_req;
2040 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
2044 req->buf = hsotg->ctrl_buff;
2048 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
2052 hsotg->eps_out[0]->dir_in = 0;
2053 hsotg->eps_out[0]->send_zlp = 0;
2054 hsotg->ep0_state = DWC2_EP0_SETUP;
2056 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
2058 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
2066 static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
2075 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
2078 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
2080 if (using_desc_dma(hsotg)) {
2085 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
2089 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2094 ctrl = dwc2_readl(hsotg, epctl_reg);
2098 dwc2_writel(hsotg, ctrl, epctl_reg);
2103 * @hsotg: The device state.
2114 static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
2120 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
2124 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
2135 if (using_dma(hsotg))
2136 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2138 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
2149 spin_unlock(&hsotg->lock);
2151 spin_lock(&hsotg->lock);
2155 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2179 struct dwc2_hsotg *hsotg = hs_ep->parent;
2193 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2218 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2238 struct dwc2_hsotg *hsotg = hs_ep->parent;
2241 dwc2_flush_rx_fifo(hsotg);
2242 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
2251 * @hsotg: The device state.
2259 static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
2261 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2268 u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx));
2271 dev_dbg(hsotg->dev,
2277 (void)dwc2_readl(hsotg, EPFIFO(ep_idx));
2286 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2307 dwc2_readl_rep(hsotg, EPFIFO(ep_idx),
2313 * @hsotg: The device instance
2323 static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
2326 hsotg->eps_out[0]->dir_in = dir_in;
2327 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
2329 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
2342 struct dwc2_hsotg *hsotg = hs_ep->parent;
2365 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2379 * @hsotg: The device instance
2386 static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
2388 u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
2389 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2396 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2400 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2401 dev_dbg(hsotg->dev, "zlp packet received\n");
2402 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2403 dwc2_hsotg_enqueue_setup(hsotg);
2407 if (using_desc_dma(hsotg))
2410 if (using_dma(hsotg)) {
2430 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
2435 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2445 if (!using_desc_dma(hsotg) && epnum == 0 &&
2446 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
2448 if (!hsotg->delayed_status)
2449 dwc2_hsotg_ep0_zlp(hsotg, true);
2453 if (!using_desc_dma(hsotg) && hs_ep->isochronous) {
2458 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
2463 * @hsotg: The device instance
2477 static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
2479 u32 grxstsr = dwc2_readl(hsotg, GRXSTSP);
2482 WARN_ON(using_dma(hsotg));
2490 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
2495 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
2499 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
2500 dwc2_hsotg_read_frameno(hsotg));
2502 if (!using_dma(hsotg))
2503 dwc2_hsotg_handle_outdone(hsotg, epnum);
2507 dev_dbg(hsotg->dev,
2509 dwc2_hsotg_read_frameno(hsotg),
2510 dwc2_readl(hsotg, DOEPCTL(0)));
2516 if (hsotg->ep0_state == DWC2_EP0_SETUP)
2517 dwc2_hsotg_handle_outdone(hsotg, epnum);
2521 dwc2_hsotg_rx_data(hsotg, epnum, size);
2525 dev_dbg(hsotg->dev,
2527 dwc2_hsotg_read_frameno(hsotg),
2528 dwc2_readl(hsotg, DOEPCTL(0)));
2530 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2532 dwc2_hsotg_rx_data(hsotg, epnum, size);
2536 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2539 dwc2_hsotg_dump(hsotg);
2568 * @hsotg: The driver state.
2577 static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
2584 hs_ep = index_to_ep(hsotg, ep, dir_in);
2607 reg = dwc2_readl(hsotg, DIEPCTL(ep));
2610 dwc2_writel(hsotg, reg, DIEPCTL(ep));
2612 reg = dwc2_readl(hsotg, DOEPCTL(ep));
2615 dwc2_writel(hsotg, reg, DOEPCTL(ep));
2621 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2626 * @hsotg: The driver state
2629 static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
2631 dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2635 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2636 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2642 * @hsotg: The driver state
2648 static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
2659 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
2665 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2667 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
2675 * @hsotg: The device state.
2681 static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
2685 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
2689 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2694 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2695 dev_dbg(hsotg->dev, "zlp packet sent\n");
2703 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2704 if (hsotg->test_mode) {
2707 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
2709 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
2710 hsotg->test_mode);
2711 dwc2_hsotg_stall_ep0(hsotg);
2715 dwc2_hsotg_enqueue_setup(hsotg);
2728 if (using_desc_dma(hsotg)) {
2731 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2741 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2745 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2749 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
2750 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
2757 if (!using_desc_dma(hsotg)) {
2758 dwc2_hsotg_program_zlp(hsotg, hs_ep);
2764 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2766 dwc2_hsotg_ep0_zlp(hsotg, false);
2771 if (!using_desc_dma(hsotg) && hs_ep->isochronous) {
2776 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2781 * @hsotg: The device state.
2788 static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2797 mask = dwc2_readl(hsotg, epmsk_reg);
2798 diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK);
2802 ints = dwc2_readl(hsotg, epint_reg);
2822 struct dwc2_hsotg *hsotg = hs_ep->parent;
2827 int dctl = dwc2_readl(hsotg, DCTL);
2829 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2832 int epctl = dwc2_readl(hsotg, epctl_reg);
2834 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2837 int dctl = dwc2_readl(hsotg, DCTL);
2840 dwc2_writel(hsotg, dctl, DCTL);
2846 dwc2_writel(hsotg, dctl, DCTL);
2854 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2864 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2869 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
2886 struct dwc2_hsotg *hsotg = ep->parent;
2893 if (using_desc_dma(hsotg)) {
2896 ep->target_frame = hsotg->frame_number;
2905 ep->target_frame = hsotg->frame_number;
2907 ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index));
2913 dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index));
2922 dwc2_hsotg_complete_request(hsotg, ep, hs_req, -ENODATA);
2927 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
2935 static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
2954 struct dwc2_hsotg *hsotg = hs_ep->parent;
2964 if (using_desc_dma(hsotg)) {
2965 hs_ep->target_frame = hsotg->frame_number;
2971 if (hsotg->params.service_interval) {
2988 hs_ep->target_frame = hsotg->frame_number;
2990 u32 ctrl = dwc2_readl(hsotg,
2997 dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index));
3001 if (using_desc_dma(hsotg))
3004 ctrl = dwc2_readl(hsotg, DIEPCTL(hs_ep->index));
3006 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
3008 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
3015 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, -ENODATA);
3020 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
3029 * @hsotg: The driver state
3035 static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
3038 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
3044 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
3047 dwc2_writel(hsotg, ints, epint_reg);
3050 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
3055 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
3068 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
3069 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
3073 dev_dbg(hsotg->dev,
3075 __func__, dwc2_readl(hsotg, epctl_reg),
3076 dwc2_readl(hsotg, epsiz_reg));
3079 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
3088 dwc2_hsotg_complete_in(hsotg, hs_ep);
3091 dwc2_hsotg_enqueue_setup(hsotg);
3092 } else if (using_dma(hsotg)) {
3098 dwc2_hsotg_handle_outdone(hsotg, idx);
3112 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
3115 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
3117 if (using_dma(hsotg) && idx == 0) {
3128 dwc2_hsotg_handle_outdone(hsotg, 0);
3133 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
3136 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
3138 if (using_desc_dma(hsotg)) {
3139 if (!hsotg->delayed_status)
3140 dwc2_hsotg_ep0_zlp(hsotg, true);
3149 dwc2_set_bit(hsotg, DIEPCTL(0),
3157 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
3160 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
3168 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
3174 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
3179 if (hsotg->dedicated_fifos &&
3181 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
3183 if (!using_dma(hsotg))
3184 dwc2_hsotg_trytx(hsotg, hs_ep);
3191 * @hsotg: The device state.
3196 static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
3198 u32 dsts = dwc2_readl(hsotg, DSTS);
3207 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3219 hsotg->gadget.speed = USB_SPEED_FULL;
3225 hsotg->gadget.speed = USB_SPEED_HIGH;
3231 hsotg->gadget.speed = USB_SPEED_LOW;
3241 dev_info(hsotg->dev, "new device is %s\n",
3242 usb_speed_string(hsotg->gadget.speed));
3252 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3253 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
3254 for (i = 1; i < hsotg->num_of_eps; i++) {
3255 if (hsotg->eps_in[i])
3256 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3258 if (hsotg->eps_out[i])
3259 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3266 dwc2_hsotg_enqueue_setup(hsotg);
3268 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3269 dwc2_readl(hsotg, DIEPCTL0),
3270 dwc2_readl(hsotg, DOEPCTL0));
3275 * @hsotg: The device state.
3282 static void kill_all_requests(struct dwc2_hsotg *hsotg,
3293 dwc2_hsotg_complete_request(hsotg, ep, req, result);
3296 if (!hsotg->dedicated_fifos)
3298 size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
3300 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
3305 * @hsotg: The device state.
3311 void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
3315 if (!hsotg->connected)
3318 hsotg->connected = 0;
3319 hsotg->test_mode = 0;
3322 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3323 if (hsotg->eps_in[ep])
3324 kill_all_requests(hsotg, hsotg->eps_in[ep],
3326 if (hsotg->eps_out[ep])
3327 kill_all_requests(hsotg, hsotg->eps_out[ep],
3331 call_gadget(hsotg, disconnect);
3332 hsotg->lx_state = DWC2_L3;
3334 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
3339 * @hsotg: The device state:
3342 static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
3348 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
3349 ep = index_to_ep(hsotg, epno, 1);
3361 ret = dwc2_hsotg_trytx(hsotg, ep);
3375 * @hsotg: The device state
3380 void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
3390 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3393 if (dwc2_core_reset(hsotg, true))
3397 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3398 if (hsotg->eps_in[ep])
3399 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3400 if (hsotg->eps_out[ep])
3401 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3411 usbcfg = dwc2_readl(hsotg, GUSBCFG);
3417 dwc2_writel(hsotg, usbcfg, GUSBCFG);
3419 dwc2_phy_init(hsotg, true);
3421 dwc2_hsotg_init_fifo(hsotg);
3424 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
3428 switch (hsotg->params.speed) {
3433 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3442 if (hsotg->params.ipg_isoc_en)
3445 dwc2_writel(hsotg, dcfg, DCFG);
3448 dwc2_writel(hsotg, 0xffffffff, GOTGINT);
3451 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
3459 if (!using_desc_dma(hsotg))
3462 if (!hsotg->params.external_id_pin_ctl)
3465 dwc2_writel(hsotg, intmsk, GINTMSK);
3467 if (using_dma(hsotg)) {
3468 dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
3469 hsotg->params.ahbcfg,
3473 if (using_desc_dma(hsotg))
3474 dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN);
3477 dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ?
3489 dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
3499 dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3506 if (using_desc_dma(hsotg)) {
3507 dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK);
3508 dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK);
3512 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3513 dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED);
3515 dwc2_writel(hsotg, 0, DAINTMSK);
3517 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3518 dwc2_readl(hsotg, DIEPCTL0),
3519 dwc2_readl(hsotg, DOEPCTL0));
3522 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
3529 if (!using_dma(hsotg))
3530 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
3533 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3534 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
3537 dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
3539 dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
3542 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL));
3550 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
3553 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3559 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3566 dwc2_set_bit(hsotg, DCTL, val);
3569 dwc2_gadget_init_lpm(hsotg);
3572 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3573 dwc2_gadget_program_ref_clk(hsotg);
3578 hsotg->lx_state = DWC2_L0;
3580 dwc2_hsotg_enqueue_setup(hsotg);
3582 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3583 dwc2_readl(hsotg, DIEPCTL0),
3584 dwc2_readl(hsotg, DOEPCTL0));
3587 void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
3590 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
3593 void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
3596 if (!hsotg->role_sw || (dwc2_readl(hsotg, GOTGCTL) & GOTGCTL_BSESVLD))
3597 dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON);
3602 * @hsotg: The device state:
3613 static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3620 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3622 daintmsk = dwc2_readl(hsotg, DAINTMSK);
3624 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
3625 hs_ep = hsotg->eps_in[idx];
3630 epctrl = dwc2_readl(hsotg, DIEPCTL(idx));
3635 dwc2_writel(hsotg, epctrl, DIEPCTL(idx));
3640 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS);
3645 * @hsotg: The device state:
3656 static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3665 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3667 daintmsk = dwc2_readl(hsotg, DAINTMSK);
3670 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
3671 hs_ep = hsotg->eps_out[idx];
3676 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
3680 gintmsk = dwc2_readl(hsotg, GINTMSK);
3682 dwc2_writel(hsotg, gintmsk, GINTMSK);
3684 gintsts = dwc2_readl(hsotg, GINTSTS);
3686 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
3693 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS);
3703 struct dwc2_hsotg *hsotg = pw;
3708 if (!dwc2_is_device_mode(hsotg))
3711 spin_lock(&hsotg->lock);
3713 gintsts = dwc2_readl(hsotg, GINTSTS);
3714 gintmsk = dwc2_readl(hsotg, GINTMSK);
3716 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3722 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3724 dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS);
3727 if (hsotg->in_ppd && hsotg->lx_state == DWC2_L2)
3728 dwc2_exit_partial_power_down(hsotg, 0, true);
3730 hsotg->lx_state = DWC2_L0;
3734 u32 usb_status = dwc2_readl(hsotg, GOTGCTL);
3735 u32 connected = hsotg->connected;
3737 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3738 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3739 dwc2_readl(hsotg, GNPTXSTS));
3741 dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS);
3744 dwc2_hsotg_disconnect(hsotg);
3747 dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
3750 dwc2_hsotg_core_init_disconnected(hsotg, true);
3754 dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS);
3756 dwc2_hsotg_irq_enumdone(hsotg);
3760 u32 daint = dwc2_readl(hsotg, DAINT);
3761 u32 daintmsk = dwc2_readl(hsotg, DAINTMSK);
3769 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3771 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3774 dwc2_hsotg_epint(hsotg, ep, 0);
3777 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3780 dwc2_hsotg_epint(hsotg, ep, 1);
3787 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3795 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3796 dwc2_hsotg_irq_fifoempty(hsotg, false);
3800 dev_dbg(hsotg->dev, "PTxFEmp\n");
3804 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3805 dwc2_hsotg_irq_fifoempty(hsotg, true);
3815 dwc2_hsotg_handle_rx(hsotg);
3819 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
3820 dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS);
3836 daintmsk = dwc2_readl(hsotg, DAINTMSK);
3839 gintmsk = dwc2_readl(hsotg, GINTMSK);
3841 dwc2_writel(hsotg, gintmsk, GINTMSK);
3843 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
3844 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
3845 hs_ep = hsotg->eps_out[idx];
3850 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
3856 dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
3866 dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
3874 dev_info(hsotg->dev, "GINNakEff triggered\n");
3876 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
3878 dwc2_hsotg_dump(hsotg);
3882 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
3885 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
3896 if (hsotg->params.service_interval)
3897 dwc2_gadget_wkup_alert_handler(hsotg);
3899 spin_unlock(&hsotg->lock);
3904 static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3915 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3919 if (hsotg->dedicated_fifos || hs_ep->periodic) {
3920 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK);
3922 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3924 dev_warn(hsotg->dev,
3928 dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK);
3930 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3932 dev_warn(hsotg->dev,
3938 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_GOUTNAKEFF);
3940 if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF))
3941 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
3943 if (!using_dma(hsotg)) {
3945 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3947 dev_warn(hsotg->dev, "%s: timeout GINTSTS.RXFLVL\n",
3954 dwc2_readl(hsotg, GRXSTSP);
3959 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3961 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3966 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3969 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3970 dev_warn(hsotg->dev,
3974 dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD);
3979 if (hsotg->dedicated_fifos || hs_ep->periodic)
3985 dwc2_flush_tx_fifo(hsotg, fifo_index);
3988 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
3989 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
3993 dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK);
4008 struct dwc2_hsotg *hsotg = hs_ep->parent;
4022 dev_dbg(hsotg->dev,
4029 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
4035 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
4044 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
4046 dev_err(hsotg->dev,
4052 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
4054 dev_err(hsotg->dev,
4062 epctrl = dwc2_readl(hsotg, epctrl_reg);
4064 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
4067 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC)
4073 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
4074 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
4083 spin_lock_irqsave(&hsotg->lock, flags);
4095 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
4115 mask = dwc2_readl(hsotg, DIEPMSK);
4117 dwc2_writel(hsotg, mask, DIEPMSK);
4120 mask = dwc2_readl(hsotg, DOEPMSK);
4122 dwc2_writel(hsotg, mask, DOEPMSK);
4134 if (hsotg->gadget.speed == USB_SPEED_HIGH)
4149 if (dir_in && hsotg->dedicated_fifos) {
4150 unsigned fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
4156 if (hsotg->fifo_map & (1 << i))
4158 val = dwc2_readl(hsotg, DPTXFSIZN(i));
4169 dev_err(hsotg->dev,
4175 hsotg->fifo_map |= 1 << fifo_index;
4191 if (hsotg->gadget.speed == USB_SPEED_FULL &&
4197 u32 gsnpsid = dwc2_readl(hsotg, GSNPSID);
4206 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
4209 dwc2_writel(hsotg, epctrl, epctrl_reg);
4210 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
4211 __func__, dwc2_readl(hsotg, epctrl_reg));
4214 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
4217 spin_unlock_irqrestore(&hsotg->lock, flags);
4220 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
4221 dmam_free_coherent(hsotg->dev, desc_num *
4237 struct dwc2_hsotg *hsotg = hs_ep->parent;
4243 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
4245 if (ep == &hsotg->eps_out[0]->ep) {
4246 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4250 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4251 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
4257 ctrl = dwc2_readl(hsotg, epctrl_reg);
4260 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4266 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
4267 dwc2_writel(hsotg, ctrl, epctrl_reg);
4270 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
4273 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4275 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4285 struct dwc2_hsotg *hsotg = hs_ep->parent;
4289 spin_lock_irqsave(&hsotg->lock, flags);
4291 spin_unlock_irqrestore(&hsotg->lock, flags);
4481 * @hsotg: The driver state
4483 static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
4487 dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4491 dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4495 dwc2_writel(hsotg, 0, DAINTMSK);
4498 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
4502 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
4503 dwc2_readl(hsotg, GRXFSIZ),
4504 dwc2_readl(hsotg, GNPTXFSIZ));
4506 dwc2_hsotg_init_fifo(hsotg);
4508 if (using_dma(hsotg))
4509 dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN);
4523 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4527 if (!hsotg) {
4533 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4538 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
4541 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4545 WARN_ON(hsotg->driver);
4547 hsotg->driver = driver;
4548 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
4549 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4551 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4552 ret = dwc2_lowlevel_hw_enable(hsotg);
4557 if (!IS_ERR_OR_NULL(hsotg->uphy))
4558 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
4560 spin_lock_irqsave(&hsotg->lock, flags);
4561 if (dwc2_hw_is_device(hsotg)) {
4562 dwc2_hsotg_init(hsotg);
4563 dwc2_hsotg_core_init_disconnected(hsotg, false);
4566 hsotg->enabled = 0;
4567 spin_unlock_irqrestore(&hsotg->lock, flags);
4569 gadget->sg_supported = using_desc_dma(hsotg);
4570 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
4575 hsotg->driver = NULL;
4587 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4591 if (!hsotg)
4595 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4596 if (hsotg->eps_in[ep])
4597 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
4598 if (hsotg->eps_out[ep])
4599 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
4602 spin_lock_irqsave(&hsotg->lock, flags);
4604 hsotg->driver = NULL;
4605 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4606 hsotg->enabled = 0;
4608 spin_unlock_irqrestore(&hsotg->lock, flags);
4610 if (!IS_ERR_OR_NULL(hsotg->uphy))
4611 otg_set_peripheral(hsotg->uphy->otg, NULL);
4613 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4614 dwc2_lowlevel_hw_disable(hsotg);
4640 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4643 spin_lock_irqsave(&hsotg->lock, flags);
4645 spin_unlock_irqrestore(&hsotg->lock, flags);
4659 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4662 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
4663 hsotg->op_state);
4666 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4667 hsotg->enabled = is_on;
4671 spin_lock_irqsave(&hsotg->lock, flags);
4673 hsotg->enabled = 1;
4674 dwc2_hsotg_core_init_disconnected(hsotg, false);
4676 dwc2_enable_acg(hsotg);
4677 dwc2_hsotg_core_connect(hsotg);
4679 dwc2_hsotg_core_disconnect(hsotg);
4680 dwc2_hsotg_disconnect(hsotg);
4681 hsotg->enabled = 0;
4684 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4685 spin_unlock_irqrestore(&hsotg->lock, flags);
4692 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4695 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4696 spin_lock_irqsave(&hsotg->lock, flags);
4702 if (hsotg->lx_state == DWC2_L2 && hsotg->in_ppd)
4707 dwc2_exit_partial_power_down(hsotg, 0, false);
4710 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
4712 dwc2_hsotg_core_init_disconnected(hsotg, false);
4713 if (hsotg->enabled) {
4715 dwc2_enable_acg(hsotg);
4716 dwc2_hsotg_core_connect(hsotg);
4719 dwc2_hsotg_core_disconnect(hsotg);
4720 dwc2_hsotg_disconnect(hsotg);
4723 spin_unlock_irqrestore(&hsotg->lock, flags);
4736 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4738 if (IS_ERR_OR_NULL(hsotg->uphy))
4740 return usb_phy_set_power(hsotg->uphy, mA);
4745 struct dwc2_hsotg *hsotg = to_hsotg(g);
4748 spin_lock_irqsave(&hsotg->lock, flags);
4751 hsotg->params.speed = DWC2_SPEED_PARAM_HIGH;
4754 hsotg->params.speed = DWC2_SPEED_PARAM_FULL;
4757 hsotg->params.speed = DWC2_SPEED_PARAM_LOW;
4760 dev_err(hsotg->dev, "invalid speed (%d)\n", speed);
4762 spin_unlock_irqrestore(&hsotg->lock, flags);
4778 * @hsotg: The device state.
4787 static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
4811 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4813 hs_ep->parent = hsotg;
4816 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4826 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4843 if (using_dma(hsotg)) {
4847 dwc2_writel(hsotg, next, DIEPCTL(epnum));
4849 dwc2_writel(hsotg, next, DOEPCTL(epnum));
4855 * @hsotg: Programming view of the DWC_otg controller
4859 static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
4867 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4870 hsotg->num_of_eps++;
4872 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4875 if (!hsotg->eps_in[0])
4878 hsotg->eps_out[0] = hsotg->eps_in[0];
4880 cfg = hsotg->hw_params.dev_ep_dirs;
4881 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
4885 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
4887 if (!hsotg->eps_in[i])
4892 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
4894 if (!hsotg->eps_out[i])
4899 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4900 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
4902 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4903 hsotg->num_of_eps,
4904 hsotg->dedicated_fifos ? "dedicated" : "shared",
4905 hsotg->fifo_mem);
4911 * @hsotg: Programming view of the DWC_otg controller
4914 static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
4917 struct device *dev = hsotg->dev;
4922 dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL),
4923 dwc2_readl(hsotg, DIEPMSK));
4926 dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1));
4929 dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ));
4933 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
4934 val = dwc2_readl(hsotg, DPTXFSIZN(idx));
4940 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
4943 dwc2_readl(hsotg, DIEPCTL(idx)),
4944 dwc2_readl(hsotg, DIEPTSIZ(idx)),
4945 dwc2_readl(hsotg, DIEPDMA(idx)));
4947 val = dwc2_readl(hsotg, DOEPCTL(idx));
4950 idx, dwc2_readl(hsotg, DOEPCTL(idx)),
4951 dwc2_readl(hsotg, DOEPTSIZ(idx)),
4952 dwc2_readl(hsotg, DOEPDMA(idx)));
4956 dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE));
4962 * @hsotg: Programming view of the DWC_otg controller
4965 int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
4967 struct device *dev = hsotg->dev;
4973 hsotg->params.g_np_tx_fifo_size);
4974 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
4976 switch (hsotg->params.speed) {
4978 hsotg->gadget.max_speed = USB_SPEED_LOW;
4981 hsotg->gadget.max_speed = USB_SPEED_FULL;
4984 hsotg->gadget.max_speed = USB_SPEED_HIGH;
4988 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
4989 hsotg->gadget.name = dev_name(dev);
4990 hsotg->gadget.otg_caps = &hsotg->params.otg_caps;
4991 hsotg->remote_wakeup_allowed = 0;
4993 if (hsotg->params.lpm)
4994 hsotg->gadget.lpm_capable = true;
4996 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4997 hsotg->gadget.is_otg = 1;
4998 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4999 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
5001 ret = dwc2_hsotg_hw_cfg(hsotg);
5003 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
5007 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
5009 if (!hsotg->ctrl_buff)
5012 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
5014 if (!hsotg->ep0_buff)
5017 if (using_desc_dma(hsotg)) {
5018 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
5023 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
5024 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
5030 /* hsotg->num_of_eps holds number of EPs other than ep0 */
5032 if (hsotg->num_of_eps == 0) {
5039 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
5040 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
5044 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
5046 if (!hsotg->ctrl_req) {
5052 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
5053 if (hsotg->eps_in[epnum])
5054 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
5056 if (hsotg->eps_out[epnum])
5057 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
5061 dwc2_hsotg_dump(hsotg);
5067 * dwc2_hsotg_remove - remove function for hsotg driver
5068 * @hsotg: Programming view of the DWC_otg controller
5071 int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
5073 usb_del_gadget_udc(&hsotg->gadget);
5074 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
5079 int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
5083 if (hsotg->lx_state != DWC2_L0)
5086 if (hsotg->driver) {
5089 dev_info(hsotg->dev, "suspending usb gadget %s\n",
5090 hsotg->driver->driver.name);
5092 spin_lock_irqsave(&hsotg->lock, flags);
5093 if (hsotg->enabled)
5094 dwc2_hsotg_core_disconnect(hsotg);
5095 dwc2_hsotg_disconnect(hsotg);
5096 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
5097 spin_unlock_irqrestore(&hsotg->lock, flags);
5099 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
5100 if (hsotg->eps_in[ep])
5101 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
5102 if (hsotg->eps_out[ep])
5103 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
5110 int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
5114 if (hsotg->lx_state == DWC2_L2)
5117 if (hsotg->driver) {
5118 dev_info(hsotg->dev, "resuming usb gadget %s\n",
5119 hsotg->driver->driver.name);
5121 spin_lock_irqsave(&hsotg->lock, flags);
5122 dwc2_hsotg_core_init_disconnected(hsotg, false);
5123 if (hsotg->enabled) {
5125 dwc2_enable_acg(hsotg);
5126 dwc2_hsotg_core_connect(hsotg);
5128 spin_unlock_irqrestore(&hsotg->lock, flags);
5139 * @hsotg: Programming view of the DWC_otg controller
5141 int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
5146 dev_dbg(hsotg->dev, "%s\n", __func__);
5149 dr = &hsotg->dr_backup;
5151 dr->dcfg = dwc2_readl(hsotg, DCFG);
5152 dr->dctl = dwc2_readl(hsotg, DCTL);
5153 dr->daintmsk = dwc2_readl(hsotg, DAINTMSK);
5154 dr->diepmsk = dwc2_readl(hsotg, DIEPMSK);
5155 dr->doepmsk = dwc2_readl(hsotg, DOEPMSK);
5157 for (i = 0; i < hsotg->num_of_eps; i++) {
5159 dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i));
5167 dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i));
5168 dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i));
5171 dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i));
5179 dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i));
5180 dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i));
5181 dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i));
5192 * @hsotg: Programming view of the DWC_otg controller
5197 int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
5202 dev_dbg(hsotg->dev, "%s\n", __func__);
5205 dr = &hsotg->dr_backup;
5207 dev_err(hsotg->dev, "%s: no device registers to restore\n",
5214 dwc2_writel(hsotg, dr->dctl, DCTL);
5216 dwc2_writel(hsotg, dr->daintmsk, DAINTMSK);
5217 dwc2_writel(hsotg, dr->diepmsk, DIEPMSK);
5218 dwc2_writel(hsotg, dr->doepmsk, DOEPMSK);
5220 for (i = 0; i < hsotg->num_of_eps; i++) {
5222 dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i));
5223 dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i));
5224 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
5230 if (using_desc_dma(hsotg) &&
5232 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
5233 dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i));
5234 dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i));
5236 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
5242 if (using_desc_dma(hsotg) &&
5244 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
5245 dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i));
5246 dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i));
5255 * @hsotg: Programming view of DWC_otg controller
5258 void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
5262 if (!hsotg->params.lpm)
5266 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
5267 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
5268 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
5269 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
5272 dwc2_writel(hsotg, val, GLPMCFG);
5273 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG));
5276 if (hsotg->params.service_interval)
5277 dwc2_set_bit(hsotg, GINTMSK2, GINTMSK2_WKUP_ALERT_INT_MSK);
5283 * @hsotg: Programming view of DWC_otg controller
5286 void dwc2_gadget_program_ref_clk(struct dwc2_hsotg *hsotg)
5291 val |= hsotg->params.ref_clk_per << GREFCLK_REFCLKPER_SHIFT;
5292 val |= hsotg->params.sof_cnt_wkup_alert <<
5295 dwc2_writel(hsotg, val, GREFCLK);
5296 dev_dbg(hsotg->dev, "GREFCLK=0x%08x\n", dwc2_readl(hsotg, GREFCLK));
5302 * @hsotg: Programming view of the DWC_otg controller
5306 int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
5312 hsotg->lx_state = DWC2_L2;
5313 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
5314 ret = dwc2_backup_global_registers(hsotg);
5316 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5320 ret = dwc2_backup_device_registers(hsotg);
5322 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
5329 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5333 hsotg->hibernated = 1;
5336 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5338 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5342 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5346 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5350 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5352 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5356 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5358 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5362 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN);
5363 dev_dbg(hsotg->dev, "Hibernation completed\n");
5373 * @hsotg: Programming view of the DWC_otg controller
5379 int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
5389 gr = &hsotg->gr_backup;
5390 dr = &hsotg->dr_backup;
5392 if (!hsotg->hibernated) {
5393 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5396 dev_dbg(hsotg->dev,
5400 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5404 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5408 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5410 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5414 pcgcctl = dwc2_readl(hsotg, PCGCTL);
5416 dwc2_writel(hsotg, pcgcctl, PCGCTL);
5420 dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5421 dwc2_writel(hsotg, dr->dcfg, DCFG);
5422 dwc2_writel(hsotg, dr->dctl, DCTL);
5426 dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
5429 gpwrdn = dwc2_readl(hsotg, GPWRDN);
5431 dwc2_writel(hsotg, gpwrdn, GPWRDN);
5436 dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL);
5440 dctl = dwc2_readl(hsotg, DCTL);
5442 dwc2_writel(hsotg, dctl, DCTL);
5447 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5450 ret = dwc2_restore_global_registers(hsotg);
5452 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5458 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5460 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5467 dctl = dwc2_readl(hsotg, DCTL);
5469 dwc2_writel(hsotg, dctl, DCTL);
5472 hsotg->hibernated = 0;
5473 hsotg->lx_state = DWC2_L0;
5474 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5483 * @hsotg: Programming view of the DWC_otg controller
5489 int dwc2_gadget_enter_partial_power_down(struct dwc2_hsotg *hsotg)
5494 dev_dbg(hsotg->dev, "Entering device partial power down started.\n");
5497 ret = dwc2_backup_global_registers(hsotg);
5499 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5504 ret = dwc2_backup_device_registers(hsotg);
5506 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
5515 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5518 pcgcctl = dwc2_readl(hsotg, PCGCTL);
5521 dwc2_writel(hsotg, pcgcctl, PCGCTL);
5525 dwc2_writel(hsotg, pcgcctl, PCGCTL);
5529 dwc2_writel(hsotg, pcgcctl, PCGCTL);
5532 hsotg->in_ppd = 1;
5533 hsotg->lx_state = DWC2_L2;
5535 dev_dbg(hsotg->dev, "Entering device partial power down completed.\n");
5544 * @hsotg: Programming view of the DWC_otg controller
5551 int dwc2_gadget_exit_partial_power_down(struct dwc2_hsotg *hsotg,
5559 dr = &hsotg->dr_backup;
5561 dev_dbg(hsotg->dev, "Exiting device partial Power Down started.\n");
5563 pcgcctl = dwc2_readl(hsotg, PCGCTL);
5565 dwc2_writel(hsotg, pcgcctl, PCGCTL);
5567 pcgcctl = dwc2_readl(hsotg, PCGCTL);
5569 dwc2_writel(hsotg, pcgcctl, PCGCTL);
5571 pcgcctl = dwc2_readl(hsotg, PCGCTL);
5573 dwc2_writel(hsotg, pcgcctl, PCGCTL);
5577 ret = dwc2_restore_global_registers(hsotg);
5579 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5584 dwc2_writel(hsotg, dr->dcfg, DCFG);
5586 ret = dwc2_restore_device_registers(hsotg, 0);
5588 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5595 dctl = dwc2_readl(hsotg, DCTL);
5597 dwc2_writel(hsotg, dctl, DCTL);
5600 hsotg->in_ppd = 0;
5601 hsotg->lx_state = DWC2_L0;
5603 dev_dbg(hsotg->dev, "Exiting device partial Power Down completed.\n");
5610 * @hsotg: Programming view of the DWC_otg controller
5616 void dwc2_gadget_enter_clock_gating(struct dwc2_hsotg *hsotg)
5620 dev_dbg(hsotg->dev, "Entering device clock gating.\n");
5623 pcgctl = dwc2_readl(hsotg, PCGCTL);
5625 dwc2_writel(hsotg, pcgctl, PCGCTL);
5629 pcgctl = dwc2_readl(hsotg, PCGCTL);
5631 dwc2_writel(hsotg, pcgctl, PCGCTL);
5634 hsotg->lx_state = DWC2_L2;
5635 hsotg->bus_suspended = true;
5641 * @hsotg: Programming view of the DWC_otg controller
5646 void dwc2_gadget_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
5651 dev_dbg(hsotg->dev, "Exiting device clock gating.\n");
5654 pcgctl = dwc2_readl(hsotg, PCGCTL);
5656 dwc2_writel(hsotg, pcgctl, PCGCTL);
5660 pcgctl = dwc2_readl(hsotg, PCGCTL);
5662 dwc2_writel(hsotg, pcgctl, PCGCTL);
5667 dctl = dwc2_readl(hsotg, DCTL);
5669 dwc2_writel(hsotg, dctl, DCTL);
5673 call_gadget(hsotg, resume);
5674 hsotg->lx_state = DWC2_L0;
5675 hsotg->bus_suspended = false;