15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 247a1685fSDinh Nguyen /** 347a1685fSDinh Nguyen * Copyright (c) 2011 Samsung Electronics Co., Ltd. 447a1685fSDinh Nguyen * http://www.samsung.com 547a1685fSDinh Nguyen * 647a1685fSDinh Nguyen * Copyright 2008 Openmoko, Inc. 747a1685fSDinh Nguyen * Copyright 2008 Simtec Electronics 847a1685fSDinh Nguyen * Ben Dooks <ben@simtec.co.uk> 947a1685fSDinh Nguyen * http://armlinux.simtec.co.uk/ 1047a1685fSDinh Nguyen * 1147a1685fSDinh Nguyen * S3C USB2.0 High-speed / OtG driver 1247a1685fSDinh Nguyen */ 1347a1685fSDinh Nguyen 1447a1685fSDinh Nguyen #include <linux/kernel.h> 1547a1685fSDinh Nguyen #include <linux/module.h> 1647a1685fSDinh Nguyen #include <linux/spinlock.h> 1747a1685fSDinh Nguyen #include <linux/interrupt.h> 1847a1685fSDinh Nguyen #include <linux/platform_device.h> 1947a1685fSDinh Nguyen #include <linux/dma-mapping.h> 207ad8096eSMarek Szyprowski #include <linux/mutex.h> 2147a1685fSDinh Nguyen #include <linux/seq_file.h> 2247a1685fSDinh Nguyen #include <linux/delay.h> 2347a1685fSDinh Nguyen #include <linux/io.h> 2447a1685fSDinh Nguyen #include <linux/slab.h> 2547a1685fSDinh Nguyen #include <linux/of_platform.h> 2647a1685fSDinh Nguyen 2747a1685fSDinh Nguyen #include <linux/usb/ch9.h> 2847a1685fSDinh Nguyen #include <linux/usb/gadget.h> 2947a1685fSDinh Nguyen #include <linux/usb/phy.h> 3047a1685fSDinh Nguyen 31f7c0b143SDinh Nguyen #include "core.h" 32941fcce4SDinh Nguyen #include "hw.h" 3347a1685fSDinh Nguyen 3447a1685fSDinh Nguyen /* conversion functions */ 351f91b4ccSFelipe Balbi static inline struct dwc2_hsotg_req *our_req(struct usb_request *req) 3647a1685fSDinh Nguyen { 371f91b4ccSFelipe Balbi return container_of(req, struct dwc2_hsotg_req, req); 3847a1685fSDinh Nguyen } 3947a1685fSDinh Nguyen 401f91b4ccSFelipe Balbi static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep) 4147a1685fSDinh Nguyen { 421f91b4ccSFelipe Balbi return container_of(ep, struct dwc2_hsotg_ep, ep); 4347a1685fSDinh Nguyen } 4447a1685fSDinh Nguyen 45941fcce4SDinh Nguyen static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget) 4647a1685fSDinh Nguyen { 47941fcce4SDinh Nguyen return container_of(gadget, struct dwc2_hsotg, gadget); 4847a1685fSDinh Nguyen } 4947a1685fSDinh Nguyen 5047a1685fSDinh Nguyen static inline void __orr32(void __iomem *ptr, u32 val) 5147a1685fSDinh Nguyen { 5295c8bc36SAntti Seppälä dwc2_writel(dwc2_readl(ptr) | val, ptr); 5347a1685fSDinh Nguyen } 5447a1685fSDinh Nguyen 5547a1685fSDinh Nguyen static inline void __bic32(void __iomem *ptr, u32 val) 5647a1685fSDinh Nguyen { 5795c8bc36SAntti Seppälä dwc2_writel(dwc2_readl(ptr) & ~val, ptr); 5847a1685fSDinh Nguyen } 5947a1685fSDinh Nguyen 601f91b4ccSFelipe Balbi static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg, 61c6f5c050SMian Yousaf Kaukab u32 ep_index, u32 dir_in) 62c6f5c050SMian Yousaf Kaukab { 63c6f5c050SMian Yousaf Kaukab if (dir_in) 64c6f5c050SMian Yousaf Kaukab return hsotg->eps_in[ep_index]; 65c6f5c050SMian Yousaf Kaukab else 66c6f5c050SMian Yousaf Kaukab return hsotg->eps_out[ep_index]; 67c6f5c050SMian Yousaf Kaukab } 68c6f5c050SMian Yousaf Kaukab 69997f4f81SMickael Maison /* forward declaration of functions */ 701f91b4ccSFelipe Balbi static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg); 7147a1685fSDinh Nguyen 7247a1685fSDinh Nguyen /** 7347a1685fSDinh Nguyen * using_dma - return the DMA status of the driver. 7447a1685fSDinh Nguyen * @hsotg: The driver state. 7547a1685fSDinh Nguyen * 7647a1685fSDinh Nguyen * Return true if we're using DMA. 7747a1685fSDinh Nguyen * 7847a1685fSDinh Nguyen * Currently, we have the DMA support code worked into everywhere 7947a1685fSDinh Nguyen * that needs it, but the AMBA DMA implementation in the hardware can 8047a1685fSDinh Nguyen * only DMA from 32bit aligned addresses. This means that gadgets such 8147a1685fSDinh Nguyen * as the CDC Ethernet cannot work as they often pass packets which are 8247a1685fSDinh Nguyen * not 32bit aligned. 8347a1685fSDinh Nguyen * 8447a1685fSDinh Nguyen * Unfortunately the choice to use DMA or not is global to the controller 8547a1685fSDinh Nguyen * and seems to be only settable when the controller is being put through 8647a1685fSDinh Nguyen * a core reset. This means we either need to fix the gadgets to take 8747a1685fSDinh Nguyen * account of DMA alignment, or add bounce buffers (yuerk). 8847a1685fSDinh Nguyen * 89edd74be8SGregory Herrero * g_using_dma is set depending on dts flag. 9047a1685fSDinh Nguyen */ 91941fcce4SDinh Nguyen static inline bool using_dma(struct dwc2_hsotg *hsotg) 9247a1685fSDinh Nguyen { 9305ee799fSJohn Youn return hsotg->params.g_dma; 9447a1685fSDinh Nguyen } 9547a1685fSDinh Nguyen 96dec4b556SVahram Aharonyan /* 97dec4b556SVahram Aharonyan * using_desc_dma - return the descriptor DMA status of the driver. 98dec4b556SVahram Aharonyan * @hsotg: The driver state. 99dec4b556SVahram Aharonyan * 100dec4b556SVahram Aharonyan * Return true if we're using descriptor DMA. 101dec4b556SVahram Aharonyan */ 102dec4b556SVahram Aharonyan static inline bool using_desc_dma(struct dwc2_hsotg *hsotg) 103dec4b556SVahram Aharonyan { 104dec4b556SVahram Aharonyan return hsotg->params.g_dma_desc; 105dec4b556SVahram Aharonyan } 106dec4b556SVahram Aharonyan 10747a1685fSDinh Nguyen /** 10892d1635dSVardan Mikayelyan * dwc2_gadget_incr_frame_num - Increments the targeted frame number. 10992d1635dSVardan Mikayelyan * @hs_ep: The endpoint 11092d1635dSVardan Mikayelyan * @increment: The value to increment by 11192d1635dSVardan Mikayelyan * 11292d1635dSVardan Mikayelyan * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT. 11392d1635dSVardan Mikayelyan * If an overrun occurs it will wrap the value and set the frame_overrun flag. 11492d1635dSVardan Mikayelyan */ 11592d1635dSVardan Mikayelyan static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep) 11692d1635dSVardan Mikayelyan { 11792d1635dSVardan Mikayelyan hs_ep->target_frame += hs_ep->interval; 11892d1635dSVardan Mikayelyan if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) { 11992d1635dSVardan Mikayelyan hs_ep->frame_overrun = 1; 12092d1635dSVardan Mikayelyan hs_ep->target_frame &= DSTS_SOFFN_LIMIT; 12192d1635dSVardan Mikayelyan } else { 12292d1635dSVardan Mikayelyan hs_ep->frame_overrun = 0; 12392d1635dSVardan Mikayelyan } 12492d1635dSVardan Mikayelyan } 12592d1635dSVardan Mikayelyan 12692d1635dSVardan Mikayelyan /** 1271f91b4ccSFelipe Balbi * dwc2_hsotg_en_gsint - enable one or more of the general interrupt 12847a1685fSDinh Nguyen * @hsotg: The device state 12947a1685fSDinh Nguyen * @ints: A bitmask of the interrupts to enable 13047a1685fSDinh Nguyen */ 1311f91b4ccSFelipe Balbi static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints) 13247a1685fSDinh Nguyen { 13395c8bc36SAntti Seppälä u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK); 13447a1685fSDinh Nguyen u32 new_gsintmsk; 13547a1685fSDinh Nguyen 13647a1685fSDinh Nguyen new_gsintmsk = gsintmsk | ints; 13747a1685fSDinh Nguyen 13847a1685fSDinh Nguyen if (new_gsintmsk != gsintmsk) { 13947a1685fSDinh Nguyen dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk); 14095c8bc36SAntti Seppälä dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK); 14147a1685fSDinh Nguyen } 14247a1685fSDinh Nguyen } 14347a1685fSDinh Nguyen 14447a1685fSDinh Nguyen /** 1451f91b4ccSFelipe Balbi * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt 14647a1685fSDinh Nguyen * @hsotg: The device state 14747a1685fSDinh Nguyen * @ints: A bitmask of the interrupts to enable 14847a1685fSDinh Nguyen */ 1491f91b4ccSFelipe Balbi static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints) 15047a1685fSDinh Nguyen { 15195c8bc36SAntti Seppälä u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK); 15247a1685fSDinh Nguyen u32 new_gsintmsk; 15347a1685fSDinh Nguyen 15447a1685fSDinh Nguyen new_gsintmsk = gsintmsk & ~ints; 15547a1685fSDinh Nguyen 15647a1685fSDinh Nguyen if (new_gsintmsk != gsintmsk) 15795c8bc36SAntti Seppälä dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK); 15847a1685fSDinh Nguyen } 15947a1685fSDinh Nguyen 16047a1685fSDinh Nguyen /** 1611f91b4ccSFelipe Balbi * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq 16247a1685fSDinh Nguyen * @hsotg: The device state 16347a1685fSDinh Nguyen * @ep: The endpoint index 16447a1685fSDinh Nguyen * @dir_in: True if direction is in. 16547a1685fSDinh Nguyen * @en: The enable value, true to enable 16647a1685fSDinh Nguyen * 16747a1685fSDinh Nguyen * Set or clear the mask for an individual endpoint's interrupt 16847a1685fSDinh Nguyen * request. 16947a1685fSDinh Nguyen */ 1701f91b4ccSFelipe Balbi static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg, 17147a1685fSDinh Nguyen unsigned int ep, unsigned int dir_in, 17247a1685fSDinh Nguyen unsigned int en) 17347a1685fSDinh Nguyen { 17447a1685fSDinh Nguyen unsigned long flags; 17547a1685fSDinh Nguyen u32 bit = 1 << ep; 17647a1685fSDinh Nguyen u32 daint; 17747a1685fSDinh Nguyen 17847a1685fSDinh Nguyen if (!dir_in) 17947a1685fSDinh Nguyen bit <<= 16; 18047a1685fSDinh Nguyen 18147a1685fSDinh Nguyen local_irq_save(flags); 18295c8bc36SAntti Seppälä daint = dwc2_readl(hsotg->regs + DAINTMSK); 18347a1685fSDinh Nguyen if (en) 18447a1685fSDinh Nguyen daint |= bit; 18547a1685fSDinh Nguyen else 18647a1685fSDinh Nguyen daint &= ~bit; 18795c8bc36SAntti Seppälä dwc2_writel(daint, hsotg->regs + DAINTMSK); 18847a1685fSDinh Nguyen local_irq_restore(flags); 18947a1685fSDinh Nguyen } 19047a1685fSDinh Nguyen 19147a1685fSDinh Nguyen /** 192c138ecfaSSevak Arakelyan * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode 193c138ecfaSSevak Arakelyan */ 194c138ecfaSSevak Arakelyan int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg) 195c138ecfaSSevak Arakelyan { 196c138ecfaSSevak Arakelyan if (hsotg->hw_params.en_multiple_tx_fifo) 197c138ecfaSSevak Arakelyan /* In dedicated FIFO mode we need count of IN EPs */ 1989273083aSMinas Harutyunyan return hsotg->hw_params.num_dev_in_eps; 199c138ecfaSSevak Arakelyan else 200c138ecfaSSevak Arakelyan /* In shared FIFO mode we need count of Periodic IN EPs */ 201c138ecfaSSevak Arakelyan return hsotg->hw_params.num_dev_perio_in_ep; 202c138ecfaSSevak Arakelyan } 203c138ecfaSSevak Arakelyan 204c138ecfaSSevak Arakelyan /** 205c138ecfaSSevak Arakelyan * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for 206c138ecfaSSevak Arakelyan * device mode TX FIFOs 207c138ecfaSSevak Arakelyan */ 208c138ecfaSSevak Arakelyan int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg) 209c138ecfaSSevak Arakelyan { 210c138ecfaSSevak Arakelyan int addr; 211c138ecfaSSevak Arakelyan int tx_addr_max; 212c138ecfaSSevak Arakelyan u32 np_tx_fifo_size; 213c138ecfaSSevak Arakelyan 214c138ecfaSSevak Arakelyan np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size, 215c138ecfaSSevak Arakelyan hsotg->params.g_np_tx_fifo_size); 216c138ecfaSSevak Arakelyan 217c138ecfaSSevak Arakelyan /* Get Endpoint Info Control block size in DWORDs. */ 2189273083aSMinas Harutyunyan tx_addr_max = hsotg->hw_params.total_fifo_size; 219c138ecfaSSevak Arakelyan 220c138ecfaSSevak Arakelyan addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size; 221c138ecfaSSevak Arakelyan if (tx_addr_max <= addr) 222c138ecfaSSevak Arakelyan return 0; 223c138ecfaSSevak Arakelyan 224c138ecfaSSevak Arakelyan return tx_addr_max - addr; 225c138ecfaSSevak Arakelyan } 226c138ecfaSSevak Arakelyan 227c138ecfaSSevak Arakelyan /** 228c138ecfaSSevak Arakelyan * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode 229c138ecfaSSevak Arakelyan * TX FIFOs 230c138ecfaSSevak Arakelyan */ 231c138ecfaSSevak Arakelyan int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg) 232c138ecfaSSevak Arakelyan { 233c138ecfaSSevak Arakelyan int tx_fifo_count; 234c138ecfaSSevak Arakelyan int tx_fifo_depth; 235c138ecfaSSevak Arakelyan 236c138ecfaSSevak Arakelyan tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg); 237c138ecfaSSevak Arakelyan 238c138ecfaSSevak Arakelyan tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg); 239c138ecfaSSevak Arakelyan 240c138ecfaSSevak Arakelyan if (!tx_fifo_count) 241c138ecfaSSevak Arakelyan return tx_fifo_depth; 242c138ecfaSSevak Arakelyan else 243c138ecfaSSevak Arakelyan return tx_fifo_depth / tx_fifo_count; 244c138ecfaSSevak Arakelyan } 245c138ecfaSSevak Arakelyan 246c138ecfaSSevak Arakelyan /** 2471f91b4ccSFelipe Balbi * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs 24847a1685fSDinh Nguyen * @hsotg: The device instance. 24947a1685fSDinh Nguyen */ 2501f91b4ccSFelipe Balbi static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg) 25147a1685fSDinh Nguyen { 2522317eacdSJohn Youn unsigned int ep; 25347a1685fSDinh Nguyen unsigned int addr; 25447a1685fSDinh Nguyen int timeout; 25579d6b8c5SSevak Arakelyan 25647a1685fSDinh Nguyen u32 val; 25705ee799fSJohn Youn u32 *txfsz = hsotg->params.g_tx_fifo_size; 25847a1685fSDinh Nguyen 2597fcbc95cSGregory Herrero /* Reset fifo map if not correctly cleared during previous session */ 2607fcbc95cSGregory Herrero WARN_ON(hsotg->fifo_map); 2617fcbc95cSGregory Herrero hsotg->fifo_map = 0; 2627fcbc95cSGregory Herrero 2630a176279SGregory Herrero /* set RX/NPTX FIFO sizes */ 26405ee799fSJohn Youn dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ); 26505ee799fSJohn Youn dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) | 26605ee799fSJohn Youn (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT), 2670a176279SGregory Herrero hsotg->regs + GNPTXFSIZ); 26847a1685fSDinh Nguyen 26947a1685fSDinh Nguyen /* 27047a1685fSDinh Nguyen * arange all the rest of the TX FIFOs, as some versions of this 27147a1685fSDinh Nguyen * block have overlapping default addresses. This also ensures 27247a1685fSDinh Nguyen * that if the settings have been changed, then they are set to 27347a1685fSDinh Nguyen * known values. 27447a1685fSDinh Nguyen */ 27547a1685fSDinh Nguyen 27647a1685fSDinh Nguyen /* start at the end of the GNPTXFSIZ, rounded up */ 27705ee799fSJohn Youn addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size; 27847a1685fSDinh Nguyen 27947a1685fSDinh Nguyen /* 2800a176279SGregory Herrero * Configure fifos sizes from provided configuration and assign 281b203d0a2SRobert Baldyga * them to endpoints dynamically according to maxpacket size value of 282b203d0a2SRobert Baldyga * given endpoint. 28347a1685fSDinh Nguyen */ 2842317eacdSJohn Youn for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) { 28505ee799fSJohn Youn if (!txfsz[ep]) 2863fa95385SJohn Youn continue; 2873fa95385SJohn Youn val = addr; 28805ee799fSJohn Youn val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT; 28905ee799fSJohn Youn WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem, 2903fa95385SJohn Youn "insufficient fifo memory"); 29105ee799fSJohn Youn addr += txfsz[ep]; 29247a1685fSDinh Nguyen 2932317eacdSJohn Youn dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep)); 29405ee799fSJohn Youn val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep)); 29547a1685fSDinh Nguyen } 29647a1685fSDinh Nguyen 297f87c842fSSevak Arakelyan dwc2_writel(hsotg->hw_params.total_fifo_size | 298f87c842fSSevak Arakelyan addr << GDFIFOCFG_EPINFOBASE_SHIFT, 299f87c842fSSevak Arakelyan hsotg->regs + GDFIFOCFG); 30047a1685fSDinh Nguyen /* 30147a1685fSDinh Nguyen * according to p428 of the design guide, we need to ensure that 30247a1685fSDinh Nguyen * all fifos are flushed before continuing 30347a1685fSDinh Nguyen */ 30447a1685fSDinh Nguyen 30595c8bc36SAntti Seppälä dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH | 30647a1685fSDinh Nguyen GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL); 30747a1685fSDinh Nguyen 30847a1685fSDinh Nguyen /* wait until the fifos are both flushed */ 30947a1685fSDinh Nguyen timeout = 100; 31047a1685fSDinh Nguyen while (1) { 31195c8bc36SAntti Seppälä val = dwc2_readl(hsotg->regs + GRSTCTL); 31247a1685fSDinh Nguyen 31347a1685fSDinh Nguyen if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0) 31447a1685fSDinh Nguyen break; 31547a1685fSDinh Nguyen 31647a1685fSDinh Nguyen if (--timeout == 0) { 31747a1685fSDinh Nguyen dev_err(hsotg->dev, 31847a1685fSDinh Nguyen "%s: timeout flushing fifos (GRSTCTL=%08x)\n", 31947a1685fSDinh Nguyen __func__, val); 32048b20bcbSGregory Herrero break; 32147a1685fSDinh Nguyen } 32247a1685fSDinh Nguyen 32347a1685fSDinh Nguyen udelay(1); 32447a1685fSDinh Nguyen } 32547a1685fSDinh Nguyen 32647a1685fSDinh Nguyen dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout); 32747a1685fSDinh Nguyen } 32847a1685fSDinh Nguyen 32947a1685fSDinh Nguyen /** 33047a1685fSDinh Nguyen * @ep: USB endpoint to allocate request for. 33147a1685fSDinh Nguyen * @flags: Allocation flags 33247a1685fSDinh Nguyen * 33347a1685fSDinh Nguyen * Allocate a new USB request structure appropriate for the specified endpoint 33447a1685fSDinh Nguyen */ 3351f91b4ccSFelipe Balbi static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep, 33647a1685fSDinh Nguyen gfp_t flags) 33747a1685fSDinh Nguyen { 3381f91b4ccSFelipe Balbi struct dwc2_hsotg_req *req; 33947a1685fSDinh Nguyen 340ec33efe2SJohn Youn req = kzalloc(sizeof(*req), flags); 34147a1685fSDinh Nguyen if (!req) 34247a1685fSDinh Nguyen return NULL; 34347a1685fSDinh Nguyen 34447a1685fSDinh Nguyen INIT_LIST_HEAD(&req->queue); 34547a1685fSDinh Nguyen 34647a1685fSDinh Nguyen return &req->req; 34747a1685fSDinh Nguyen } 34847a1685fSDinh Nguyen 34947a1685fSDinh Nguyen /** 35047a1685fSDinh Nguyen * is_ep_periodic - return true if the endpoint is in periodic mode. 35147a1685fSDinh Nguyen * @hs_ep: The endpoint to query. 35247a1685fSDinh Nguyen * 35347a1685fSDinh Nguyen * Returns true if the endpoint is in periodic mode, meaning it is being 35447a1685fSDinh Nguyen * used for an Interrupt or ISO transfer. 35547a1685fSDinh Nguyen */ 3561f91b4ccSFelipe Balbi static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep) 35747a1685fSDinh Nguyen { 35847a1685fSDinh Nguyen return hs_ep->periodic; 35947a1685fSDinh Nguyen } 36047a1685fSDinh Nguyen 36147a1685fSDinh Nguyen /** 3621f91b4ccSFelipe Balbi * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request 36347a1685fSDinh Nguyen * @hsotg: The device state. 36447a1685fSDinh Nguyen * @hs_ep: The endpoint for the request 36547a1685fSDinh Nguyen * @hs_req: The request being processed. 36647a1685fSDinh Nguyen * 3671f91b4ccSFelipe Balbi * This is the reverse of dwc2_hsotg_map_dma(), called for the completion 36847a1685fSDinh Nguyen * of a request to ensure the buffer is ready for access by the caller. 36947a1685fSDinh Nguyen */ 3701f91b4ccSFelipe Balbi static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg, 3711f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep, 3721f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req) 37347a1685fSDinh Nguyen { 37447a1685fSDinh Nguyen struct usb_request *req = &hs_req->req; 3759da51974SJohn Youn 37647a1685fSDinh Nguyen usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in); 37747a1685fSDinh Nguyen } 37847a1685fSDinh Nguyen 3790f6b80c0SVahram Aharonyan /* 3800f6b80c0SVahram Aharonyan * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains 3810f6b80c0SVahram Aharonyan * for Control endpoint 3820f6b80c0SVahram Aharonyan * @hsotg: The device state. 3830f6b80c0SVahram Aharonyan * 3840f6b80c0SVahram Aharonyan * This function will allocate 4 descriptor chains for EP 0: 2 for 3850f6b80c0SVahram Aharonyan * Setup stage, per one for IN and OUT data/status transactions. 3860f6b80c0SVahram Aharonyan */ 3870f6b80c0SVahram Aharonyan static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg) 3880f6b80c0SVahram Aharonyan { 3890f6b80c0SVahram Aharonyan hsotg->setup_desc[0] = 3900f6b80c0SVahram Aharonyan dmam_alloc_coherent(hsotg->dev, 3910f6b80c0SVahram Aharonyan sizeof(struct dwc2_dma_desc), 3920f6b80c0SVahram Aharonyan &hsotg->setup_desc_dma[0], 3930f6b80c0SVahram Aharonyan GFP_KERNEL); 3940f6b80c0SVahram Aharonyan if (!hsotg->setup_desc[0]) 3950f6b80c0SVahram Aharonyan goto fail; 3960f6b80c0SVahram Aharonyan 3970f6b80c0SVahram Aharonyan hsotg->setup_desc[1] = 3980f6b80c0SVahram Aharonyan dmam_alloc_coherent(hsotg->dev, 3990f6b80c0SVahram Aharonyan sizeof(struct dwc2_dma_desc), 4000f6b80c0SVahram Aharonyan &hsotg->setup_desc_dma[1], 4010f6b80c0SVahram Aharonyan GFP_KERNEL); 4020f6b80c0SVahram Aharonyan if (!hsotg->setup_desc[1]) 4030f6b80c0SVahram Aharonyan goto fail; 4040f6b80c0SVahram Aharonyan 4050f6b80c0SVahram Aharonyan hsotg->ctrl_in_desc = 4060f6b80c0SVahram Aharonyan dmam_alloc_coherent(hsotg->dev, 4070f6b80c0SVahram Aharonyan sizeof(struct dwc2_dma_desc), 4080f6b80c0SVahram Aharonyan &hsotg->ctrl_in_desc_dma, 4090f6b80c0SVahram Aharonyan GFP_KERNEL); 4100f6b80c0SVahram Aharonyan if (!hsotg->ctrl_in_desc) 4110f6b80c0SVahram Aharonyan goto fail; 4120f6b80c0SVahram Aharonyan 4130f6b80c0SVahram Aharonyan hsotg->ctrl_out_desc = 4140f6b80c0SVahram Aharonyan dmam_alloc_coherent(hsotg->dev, 4150f6b80c0SVahram Aharonyan sizeof(struct dwc2_dma_desc), 4160f6b80c0SVahram Aharonyan &hsotg->ctrl_out_desc_dma, 4170f6b80c0SVahram Aharonyan GFP_KERNEL); 4180f6b80c0SVahram Aharonyan if (!hsotg->ctrl_out_desc) 4190f6b80c0SVahram Aharonyan goto fail; 4200f6b80c0SVahram Aharonyan 4210f6b80c0SVahram Aharonyan return 0; 4220f6b80c0SVahram Aharonyan 4230f6b80c0SVahram Aharonyan fail: 4240f6b80c0SVahram Aharonyan return -ENOMEM; 4250f6b80c0SVahram Aharonyan } 4260f6b80c0SVahram Aharonyan 42747a1685fSDinh Nguyen /** 4281f91b4ccSFelipe Balbi * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO 42947a1685fSDinh Nguyen * @hsotg: The controller state. 43047a1685fSDinh Nguyen * @hs_ep: The endpoint we're going to write for. 43147a1685fSDinh Nguyen * @hs_req: The request to write data for. 43247a1685fSDinh Nguyen * 43347a1685fSDinh Nguyen * This is called when the TxFIFO has some space in it to hold a new 43447a1685fSDinh Nguyen * transmission and we have something to give it. The actual setup of 43547a1685fSDinh Nguyen * the data size is done elsewhere, so all we have to do is to actually 43647a1685fSDinh Nguyen * write the data. 43747a1685fSDinh Nguyen * 43847a1685fSDinh Nguyen * The return value is zero if there is more space (or nothing was done) 43947a1685fSDinh Nguyen * otherwise -ENOSPC is returned if the FIFO space was used up. 44047a1685fSDinh Nguyen * 44147a1685fSDinh Nguyen * This routine is only needed for PIO 44247a1685fSDinh Nguyen */ 4431f91b4ccSFelipe Balbi static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg, 4441f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep, 4451f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req) 44647a1685fSDinh Nguyen { 44747a1685fSDinh Nguyen bool periodic = is_ep_periodic(hs_ep); 44895c8bc36SAntti Seppälä u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS); 44947a1685fSDinh Nguyen int buf_pos = hs_req->req.actual; 45047a1685fSDinh Nguyen int to_write = hs_ep->size_loaded; 45147a1685fSDinh Nguyen void *data; 45247a1685fSDinh Nguyen int can_write; 45347a1685fSDinh Nguyen int pkt_round; 45447a1685fSDinh Nguyen int max_transfer; 45547a1685fSDinh Nguyen 45647a1685fSDinh Nguyen to_write -= (buf_pos - hs_ep->last_load); 45747a1685fSDinh Nguyen 45847a1685fSDinh Nguyen /* if there's nothing to write, get out early */ 45947a1685fSDinh Nguyen if (to_write == 0) 46047a1685fSDinh Nguyen return 0; 46147a1685fSDinh Nguyen 46247a1685fSDinh Nguyen if (periodic && !hsotg->dedicated_fifos) { 46395c8bc36SAntti Seppälä u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index)); 46447a1685fSDinh Nguyen int size_left; 46547a1685fSDinh Nguyen int size_done; 46647a1685fSDinh Nguyen 46747a1685fSDinh Nguyen /* 46847a1685fSDinh Nguyen * work out how much data was loaded so we can calculate 46947a1685fSDinh Nguyen * how much data is left in the fifo. 47047a1685fSDinh Nguyen */ 47147a1685fSDinh Nguyen 47247a1685fSDinh Nguyen size_left = DXEPTSIZ_XFERSIZE_GET(epsize); 47347a1685fSDinh Nguyen 47447a1685fSDinh Nguyen /* 47547a1685fSDinh Nguyen * if shared fifo, we cannot write anything until the 47647a1685fSDinh Nguyen * previous data has been completely sent. 47747a1685fSDinh Nguyen */ 47847a1685fSDinh Nguyen if (hs_ep->fifo_load != 0) { 4791f91b4ccSFelipe Balbi dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); 48047a1685fSDinh Nguyen return -ENOSPC; 48147a1685fSDinh Nguyen } 48247a1685fSDinh Nguyen 48347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n", 48447a1685fSDinh Nguyen __func__, size_left, 48547a1685fSDinh Nguyen hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size); 48647a1685fSDinh Nguyen 48747a1685fSDinh Nguyen /* how much of the data has moved */ 48847a1685fSDinh Nguyen size_done = hs_ep->size_loaded - size_left; 48947a1685fSDinh Nguyen 49047a1685fSDinh Nguyen /* how much data is left in the fifo */ 49147a1685fSDinh Nguyen can_write = hs_ep->fifo_load - size_done; 49247a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: => can_write1=%d\n", 49347a1685fSDinh Nguyen __func__, can_write); 49447a1685fSDinh Nguyen 49547a1685fSDinh Nguyen can_write = hs_ep->fifo_size - can_write; 49647a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: => can_write2=%d\n", 49747a1685fSDinh Nguyen __func__, can_write); 49847a1685fSDinh Nguyen 49947a1685fSDinh Nguyen if (can_write <= 0) { 5001f91b4ccSFelipe Balbi dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); 50147a1685fSDinh Nguyen return -ENOSPC; 50247a1685fSDinh Nguyen } 50347a1685fSDinh Nguyen } else if (hsotg->dedicated_fifos && hs_ep->index != 0) { 504ad674a15SRobert Baldyga can_write = dwc2_readl(hsotg->regs + 505ad674a15SRobert Baldyga DTXFSTS(hs_ep->fifo_index)); 50647a1685fSDinh Nguyen 50747a1685fSDinh Nguyen can_write &= 0xffff; 50847a1685fSDinh Nguyen can_write *= 4; 50947a1685fSDinh Nguyen } else { 51047a1685fSDinh Nguyen if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) { 51147a1685fSDinh Nguyen dev_dbg(hsotg->dev, 51247a1685fSDinh Nguyen "%s: no queue slots available (0x%08x)\n", 51347a1685fSDinh Nguyen __func__, gnptxsts); 51447a1685fSDinh Nguyen 5151f91b4ccSFelipe Balbi dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP); 51647a1685fSDinh Nguyen return -ENOSPC; 51747a1685fSDinh Nguyen } 51847a1685fSDinh Nguyen 51947a1685fSDinh Nguyen can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts); 52047a1685fSDinh Nguyen can_write *= 4; /* fifo size is in 32bit quantities. */ 52147a1685fSDinh Nguyen } 52247a1685fSDinh Nguyen 52347a1685fSDinh Nguyen max_transfer = hs_ep->ep.maxpacket * hs_ep->mc; 52447a1685fSDinh Nguyen 52547a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n", 52647a1685fSDinh Nguyen __func__, gnptxsts, can_write, to_write, max_transfer); 52747a1685fSDinh Nguyen 52847a1685fSDinh Nguyen /* 52947a1685fSDinh Nguyen * limit to 512 bytes of data, it seems at least on the non-periodic 53047a1685fSDinh Nguyen * FIFO, requests of >512 cause the endpoint to get stuck with a 53147a1685fSDinh Nguyen * fragment of the end of the transfer in it. 53247a1685fSDinh Nguyen */ 53347a1685fSDinh Nguyen if (can_write > 512 && !periodic) 53447a1685fSDinh Nguyen can_write = 512; 53547a1685fSDinh Nguyen 53647a1685fSDinh Nguyen /* 53747a1685fSDinh Nguyen * limit the write to one max-packet size worth of data, but allow 53847a1685fSDinh Nguyen * the transfer to return that it did not run out of fifo space 53947a1685fSDinh Nguyen * doing it. 54047a1685fSDinh Nguyen */ 54147a1685fSDinh Nguyen if (to_write > max_transfer) { 54247a1685fSDinh Nguyen to_write = max_transfer; 54347a1685fSDinh Nguyen 54447a1685fSDinh Nguyen /* it's needed only when we do not use dedicated fifos */ 54547a1685fSDinh Nguyen if (!hsotg->dedicated_fifos) 5461f91b4ccSFelipe Balbi dwc2_hsotg_en_gsint(hsotg, 54747a1685fSDinh Nguyen periodic ? GINTSTS_PTXFEMP : 54847a1685fSDinh Nguyen GINTSTS_NPTXFEMP); 54947a1685fSDinh Nguyen } 55047a1685fSDinh Nguyen 55147a1685fSDinh Nguyen /* see if we can write data */ 55247a1685fSDinh Nguyen 55347a1685fSDinh Nguyen if (to_write > can_write) { 55447a1685fSDinh Nguyen to_write = can_write; 55547a1685fSDinh Nguyen pkt_round = to_write % max_transfer; 55647a1685fSDinh Nguyen 55747a1685fSDinh Nguyen /* 55847a1685fSDinh Nguyen * Round the write down to an 55947a1685fSDinh Nguyen * exact number of packets. 56047a1685fSDinh Nguyen * 56147a1685fSDinh Nguyen * Note, we do not currently check to see if we can ever 56247a1685fSDinh Nguyen * write a full packet or not to the FIFO. 56347a1685fSDinh Nguyen */ 56447a1685fSDinh Nguyen 56547a1685fSDinh Nguyen if (pkt_round) 56647a1685fSDinh Nguyen to_write -= pkt_round; 56747a1685fSDinh Nguyen 56847a1685fSDinh Nguyen /* 56947a1685fSDinh Nguyen * enable correct FIFO interrupt to alert us when there 57047a1685fSDinh Nguyen * is more room left. 57147a1685fSDinh Nguyen */ 57247a1685fSDinh Nguyen 57347a1685fSDinh Nguyen /* it's needed only when we do not use dedicated fifos */ 57447a1685fSDinh Nguyen if (!hsotg->dedicated_fifos) 5751f91b4ccSFelipe Balbi dwc2_hsotg_en_gsint(hsotg, 57647a1685fSDinh Nguyen periodic ? GINTSTS_PTXFEMP : 57747a1685fSDinh Nguyen GINTSTS_NPTXFEMP); 57847a1685fSDinh Nguyen } 57947a1685fSDinh Nguyen 58047a1685fSDinh Nguyen dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n", 58147a1685fSDinh Nguyen to_write, hs_req->req.length, can_write, buf_pos); 58247a1685fSDinh Nguyen 58347a1685fSDinh Nguyen if (to_write <= 0) 58447a1685fSDinh Nguyen return -ENOSPC; 58547a1685fSDinh Nguyen 58647a1685fSDinh Nguyen hs_req->req.actual = buf_pos + to_write; 58747a1685fSDinh Nguyen hs_ep->total_data += to_write; 58847a1685fSDinh Nguyen 58947a1685fSDinh Nguyen if (periodic) 59047a1685fSDinh Nguyen hs_ep->fifo_load += to_write; 59147a1685fSDinh Nguyen 59247a1685fSDinh Nguyen to_write = DIV_ROUND_UP(to_write, 4); 59347a1685fSDinh Nguyen data = hs_req->req.buf + buf_pos; 59447a1685fSDinh Nguyen 59547a1685fSDinh Nguyen iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write); 59647a1685fSDinh Nguyen 59747a1685fSDinh Nguyen return (to_write >= can_write) ? -ENOSPC : 0; 59847a1685fSDinh Nguyen } 59947a1685fSDinh Nguyen 60047a1685fSDinh Nguyen /** 60147a1685fSDinh Nguyen * get_ep_limit - get the maximum data legnth for this endpoint 60247a1685fSDinh Nguyen * @hs_ep: The endpoint 60347a1685fSDinh Nguyen * 60447a1685fSDinh Nguyen * Return the maximum data that can be queued in one go on a given endpoint 60547a1685fSDinh Nguyen * so that transfers that are too long can be split. 60647a1685fSDinh Nguyen */ 6079da51974SJohn Youn static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep) 60847a1685fSDinh Nguyen { 60947a1685fSDinh Nguyen int index = hs_ep->index; 6109da51974SJohn Youn unsigned int maxsize; 6119da51974SJohn Youn unsigned int maxpkt; 61247a1685fSDinh Nguyen 61347a1685fSDinh Nguyen if (index != 0) { 61447a1685fSDinh Nguyen maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1; 61547a1685fSDinh Nguyen maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1; 61647a1685fSDinh Nguyen } else { 61747a1685fSDinh Nguyen maxsize = 64 + 64; 61847a1685fSDinh Nguyen if (hs_ep->dir_in) 61947a1685fSDinh Nguyen maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1; 62047a1685fSDinh Nguyen else 62147a1685fSDinh Nguyen maxpkt = 2; 62247a1685fSDinh Nguyen } 62347a1685fSDinh Nguyen 62447a1685fSDinh Nguyen /* we made the constant loading easier above by using +1 */ 62547a1685fSDinh Nguyen maxpkt--; 62647a1685fSDinh Nguyen maxsize--; 62747a1685fSDinh Nguyen 62847a1685fSDinh Nguyen /* 62947a1685fSDinh Nguyen * constrain by packet count if maxpkts*pktsize is greater 63047a1685fSDinh Nguyen * than the length register size. 63147a1685fSDinh Nguyen */ 63247a1685fSDinh Nguyen 63347a1685fSDinh Nguyen if ((maxpkt * hs_ep->ep.maxpacket) < maxsize) 63447a1685fSDinh Nguyen maxsize = maxpkt * hs_ep->ep.maxpacket; 63547a1685fSDinh Nguyen 63647a1685fSDinh Nguyen return maxsize; 63747a1685fSDinh Nguyen } 63847a1685fSDinh Nguyen 63947a1685fSDinh Nguyen /** 640381fc8f8SVardan Mikayelyan * dwc2_hsotg_read_frameno - read current frame number 641381fc8f8SVardan Mikayelyan * @hsotg: The device instance 642381fc8f8SVardan Mikayelyan * 643381fc8f8SVardan Mikayelyan * Return the current frame number 644381fc8f8SVardan Mikayelyan */ 645381fc8f8SVardan Mikayelyan static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg) 646381fc8f8SVardan Mikayelyan { 647381fc8f8SVardan Mikayelyan u32 dsts; 648381fc8f8SVardan Mikayelyan 649381fc8f8SVardan Mikayelyan dsts = dwc2_readl(hsotg->regs + DSTS); 650381fc8f8SVardan Mikayelyan dsts &= DSTS_SOFFN_MASK; 651381fc8f8SVardan Mikayelyan dsts >>= DSTS_SOFFN_SHIFT; 652381fc8f8SVardan Mikayelyan 653381fc8f8SVardan Mikayelyan return dsts; 654381fc8f8SVardan Mikayelyan } 655381fc8f8SVardan Mikayelyan 656381fc8f8SVardan Mikayelyan /** 657cf77b5fbSVahram Aharonyan * dwc2_gadget_get_chain_limit - get the maximum data payload value of the 658cf77b5fbSVahram Aharonyan * DMA descriptor chain prepared for specific endpoint 659cf77b5fbSVahram Aharonyan * @hs_ep: The endpoint 660cf77b5fbSVahram Aharonyan * 661cf77b5fbSVahram Aharonyan * Return the maximum data that can be queued in one go on a given endpoint 662cf77b5fbSVahram Aharonyan * depending on its descriptor chain capacity so that transfers that 663cf77b5fbSVahram Aharonyan * are too long can be split. 664cf77b5fbSVahram Aharonyan */ 665cf77b5fbSVahram Aharonyan static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep) 666cf77b5fbSVahram Aharonyan { 667cf77b5fbSVahram Aharonyan int is_isoc = hs_ep->isochronous; 668cf77b5fbSVahram Aharonyan unsigned int maxsize; 669cf77b5fbSVahram Aharonyan 670cf77b5fbSVahram Aharonyan if (is_isoc) 671cf77b5fbSVahram Aharonyan maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT : 672cf77b5fbSVahram Aharonyan DEV_DMA_ISOC_RX_NBYTES_LIMIT; 673cf77b5fbSVahram Aharonyan else 674cf77b5fbSVahram Aharonyan maxsize = DEV_DMA_NBYTES_LIMIT; 675cf77b5fbSVahram Aharonyan 676cf77b5fbSVahram Aharonyan /* Above size of one descriptor was chosen, multiple it */ 677cf77b5fbSVahram Aharonyan maxsize *= MAX_DMA_DESC_NUM_GENERIC; 678cf77b5fbSVahram Aharonyan 679cf77b5fbSVahram Aharonyan return maxsize; 680cf77b5fbSVahram Aharonyan } 681cf77b5fbSVahram Aharonyan 682e02f9aa6SVahram Aharonyan /* 683e02f9aa6SVahram Aharonyan * dwc2_gadget_get_desc_params - get DMA descriptor parameters. 684e02f9aa6SVahram Aharonyan * @hs_ep: The endpoint 685e02f9aa6SVahram Aharonyan * @mask: RX/TX bytes mask to be defined 686e02f9aa6SVahram Aharonyan * 687e02f9aa6SVahram Aharonyan * Returns maximum data payload for one descriptor after analyzing endpoint 688e02f9aa6SVahram Aharonyan * characteristics. 689e02f9aa6SVahram Aharonyan * DMA descriptor transfer bytes limit depends on EP type: 690e02f9aa6SVahram Aharonyan * Control out - MPS, 691e02f9aa6SVahram Aharonyan * Isochronous - descriptor rx/tx bytes bitfield limit, 692e02f9aa6SVahram Aharonyan * Control In/Bulk/Interrupt - multiple of mps. This will allow to not 693e02f9aa6SVahram Aharonyan * have concatenations from various descriptors within one packet. 694e02f9aa6SVahram Aharonyan * 695e02f9aa6SVahram Aharonyan * Selects corresponding mask for RX/TX bytes as well. 696e02f9aa6SVahram Aharonyan */ 697e02f9aa6SVahram Aharonyan static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask) 698e02f9aa6SVahram Aharonyan { 699e02f9aa6SVahram Aharonyan u32 mps = hs_ep->ep.maxpacket; 700e02f9aa6SVahram Aharonyan int dir_in = hs_ep->dir_in; 701e02f9aa6SVahram Aharonyan u32 desc_size = 0; 702e02f9aa6SVahram Aharonyan 703e02f9aa6SVahram Aharonyan if (!hs_ep->index && !dir_in) { 704e02f9aa6SVahram Aharonyan desc_size = mps; 705e02f9aa6SVahram Aharonyan *mask = DEV_DMA_NBYTES_MASK; 706e02f9aa6SVahram Aharonyan } else if (hs_ep->isochronous) { 707e02f9aa6SVahram Aharonyan if (dir_in) { 708e02f9aa6SVahram Aharonyan desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT; 709e02f9aa6SVahram Aharonyan *mask = DEV_DMA_ISOC_TX_NBYTES_MASK; 710e02f9aa6SVahram Aharonyan } else { 711e02f9aa6SVahram Aharonyan desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT; 712e02f9aa6SVahram Aharonyan *mask = DEV_DMA_ISOC_RX_NBYTES_MASK; 713e02f9aa6SVahram Aharonyan } 714e02f9aa6SVahram Aharonyan } else { 715e02f9aa6SVahram Aharonyan desc_size = DEV_DMA_NBYTES_LIMIT; 716e02f9aa6SVahram Aharonyan *mask = DEV_DMA_NBYTES_MASK; 717e02f9aa6SVahram Aharonyan 718e02f9aa6SVahram Aharonyan /* Round down desc_size to be mps multiple */ 719e02f9aa6SVahram Aharonyan desc_size -= desc_size % mps; 720e02f9aa6SVahram Aharonyan } 721e02f9aa6SVahram Aharonyan 722e02f9aa6SVahram Aharonyan return desc_size; 723e02f9aa6SVahram Aharonyan } 724e02f9aa6SVahram Aharonyan 725e02f9aa6SVahram Aharonyan /* 726e02f9aa6SVahram Aharonyan * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain. 727e02f9aa6SVahram Aharonyan * @hs_ep: The endpoint 728e02f9aa6SVahram Aharonyan * @dma_buff: DMA address to use 729e02f9aa6SVahram Aharonyan * @len: Length of the transfer 730e02f9aa6SVahram Aharonyan * 731e02f9aa6SVahram Aharonyan * This function will iterate over descriptor chain and fill its entries 732e02f9aa6SVahram Aharonyan * with corresponding information based on transfer data. 733e02f9aa6SVahram Aharonyan */ 734e02f9aa6SVahram Aharonyan static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep, 735e02f9aa6SVahram Aharonyan dma_addr_t dma_buff, 736e02f9aa6SVahram Aharonyan unsigned int len) 737e02f9aa6SVahram Aharonyan { 738e02f9aa6SVahram Aharonyan struct dwc2_hsotg *hsotg = hs_ep->parent; 739e02f9aa6SVahram Aharonyan int dir_in = hs_ep->dir_in; 740e02f9aa6SVahram Aharonyan struct dwc2_dma_desc *desc = hs_ep->desc_list; 741e02f9aa6SVahram Aharonyan u32 mps = hs_ep->ep.maxpacket; 742e02f9aa6SVahram Aharonyan u32 maxsize = 0; 743e02f9aa6SVahram Aharonyan u32 offset = 0; 744e02f9aa6SVahram Aharonyan u32 mask = 0; 745e02f9aa6SVahram Aharonyan int i; 746e02f9aa6SVahram Aharonyan 747e02f9aa6SVahram Aharonyan maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); 748e02f9aa6SVahram Aharonyan 749e02f9aa6SVahram Aharonyan hs_ep->desc_count = (len / maxsize) + 750e02f9aa6SVahram Aharonyan ((len % maxsize) ? 1 : 0); 751e02f9aa6SVahram Aharonyan if (len == 0) 752e02f9aa6SVahram Aharonyan hs_ep->desc_count = 1; 753e02f9aa6SVahram Aharonyan 754e02f9aa6SVahram Aharonyan for (i = 0; i < hs_ep->desc_count; ++i) { 755e02f9aa6SVahram Aharonyan desc->status = 0; 756e02f9aa6SVahram Aharonyan desc->status |= (DEV_DMA_BUFF_STS_HBUSY 757e02f9aa6SVahram Aharonyan << DEV_DMA_BUFF_STS_SHIFT); 758e02f9aa6SVahram Aharonyan 759e02f9aa6SVahram Aharonyan if (len > maxsize) { 760e02f9aa6SVahram Aharonyan if (!hs_ep->index && !dir_in) 761e02f9aa6SVahram Aharonyan desc->status |= (DEV_DMA_L | DEV_DMA_IOC); 762e02f9aa6SVahram Aharonyan 763e02f9aa6SVahram Aharonyan desc->status |= (maxsize << 764e02f9aa6SVahram Aharonyan DEV_DMA_NBYTES_SHIFT & mask); 765e02f9aa6SVahram Aharonyan desc->buf = dma_buff + offset; 766e02f9aa6SVahram Aharonyan 767e02f9aa6SVahram Aharonyan len -= maxsize; 768e02f9aa6SVahram Aharonyan offset += maxsize; 769e02f9aa6SVahram Aharonyan } else { 770e02f9aa6SVahram Aharonyan desc->status |= (DEV_DMA_L | DEV_DMA_IOC); 771e02f9aa6SVahram Aharonyan 772e02f9aa6SVahram Aharonyan if (dir_in) 773e02f9aa6SVahram Aharonyan desc->status |= (len % mps) ? DEV_DMA_SHORT : 774e02f9aa6SVahram Aharonyan ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0); 775e02f9aa6SVahram Aharonyan if (len > maxsize) 776e02f9aa6SVahram Aharonyan dev_err(hsotg->dev, "wrong len %d\n", len); 777e02f9aa6SVahram Aharonyan 778e02f9aa6SVahram Aharonyan desc->status |= 779e02f9aa6SVahram Aharonyan len << DEV_DMA_NBYTES_SHIFT & mask; 780e02f9aa6SVahram Aharonyan desc->buf = dma_buff + offset; 781e02f9aa6SVahram Aharonyan } 782e02f9aa6SVahram Aharonyan 783e02f9aa6SVahram Aharonyan desc->status &= ~DEV_DMA_BUFF_STS_MASK; 784e02f9aa6SVahram Aharonyan desc->status |= (DEV_DMA_BUFF_STS_HREADY 785e02f9aa6SVahram Aharonyan << DEV_DMA_BUFF_STS_SHIFT); 786e02f9aa6SVahram Aharonyan desc++; 787e02f9aa6SVahram Aharonyan } 788e02f9aa6SVahram Aharonyan } 789e02f9aa6SVahram Aharonyan 790540ccba0SVahram Aharonyan /* 791540ccba0SVahram Aharonyan * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain. 792540ccba0SVahram Aharonyan * @hs_ep: The isochronous endpoint. 793540ccba0SVahram Aharonyan * @dma_buff: usb requests dma buffer. 794540ccba0SVahram Aharonyan * @len: usb request transfer length. 795540ccba0SVahram Aharonyan * 796540ccba0SVahram Aharonyan * Finds out index of first free entry either in the bottom or up half of 797540ccba0SVahram Aharonyan * descriptor chain depend on which is under SW control and not processed 798540ccba0SVahram Aharonyan * by HW. Then fills that descriptor with the data of the arrived usb request, 799540ccba0SVahram Aharonyan * frame info, sets Last and IOC bits increments next_desc. If filled 800540ccba0SVahram Aharonyan * descriptor is not the first one, removes L bit from the previous descriptor 801540ccba0SVahram Aharonyan * status. 802540ccba0SVahram Aharonyan */ 803540ccba0SVahram Aharonyan static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep, 804540ccba0SVahram Aharonyan dma_addr_t dma_buff, unsigned int len) 805540ccba0SVahram Aharonyan { 806540ccba0SVahram Aharonyan struct dwc2_dma_desc *desc; 807540ccba0SVahram Aharonyan struct dwc2_hsotg *hsotg = hs_ep->parent; 808540ccba0SVahram Aharonyan u32 index; 809540ccba0SVahram Aharonyan u32 maxsize = 0; 810540ccba0SVahram Aharonyan u32 mask = 0; 811540ccba0SVahram Aharonyan 812540ccba0SVahram Aharonyan maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); 813540ccba0SVahram Aharonyan if (len > maxsize) { 814540ccba0SVahram Aharonyan dev_err(hsotg->dev, "wrong len %d\n", len); 815540ccba0SVahram Aharonyan return -EINVAL; 816540ccba0SVahram Aharonyan } 817540ccba0SVahram Aharonyan 818540ccba0SVahram Aharonyan /* 819540ccba0SVahram Aharonyan * If SW has already filled half of chain, then return and wait for 820540ccba0SVahram Aharonyan * the other chain to be processed by HW. 821540ccba0SVahram Aharonyan */ 822540ccba0SVahram Aharonyan if (hs_ep->next_desc == MAX_DMA_DESC_NUM_GENERIC / 2) 823540ccba0SVahram Aharonyan return -EBUSY; 824540ccba0SVahram Aharonyan 825540ccba0SVahram Aharonyan /* Increment frame number by interval for IN */ 826540ccba0SVahram Aharonyan if (hs_ep->dir_in) 827540ccba0SVahram Aharonyan dwc2_gadget_incr_frame_num(hs_ep); 828540ccba0SVahram Aharonyan 829540ccba0SVahram Aharonyan index = (MAX_DMA_DESC_NUM_GENERIC / 2) * hs_ep->isoc_chain_num + 830540ccba0SVahram Aharonyan hs_ep->next_desc; 831540ccba0SVahram Aharonyan 832540ccba0SVahram Aharonyan /* Sanity check of calculated index */ 833540ccba0SVahram Aharonyan if ((hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC) || 834540ccba0SVahram Aharonyan (!hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC / 2)) { 835540ccba0SVahram Aharonyan dev_err(hsotg->dev, "wrong index %d for iso chain\n", index); 836540ccba0SVahram Aharonyan return -EINVAL; 837540ccba0SVahram Aharonyan } 838540ccba0SVahram Aharonyan 839540ccba0SVahram Aharonyan desc = &hs_ep->desc_list[index]; 840540ccba0SVahram Aharonyan 841540ccba0SVahram Aharonyan /* Clear L bit of previous desc if more than one entries in the chain */ 842540ccba0SVahram Aharonyan if (hs_ep->next_desc) 843540ccba0SVahram Aharonyan hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L; 844540ccba0SVahram Aharonyan 845540ccba0SVahram Aharonyan dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n", 846540ccba0SVahram Aharonyan __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index); 847540ccba0SVahram Aharonyan 848540ccba0SVahram Aharonyan desc->status = 0; 849540ccba0SVahram Aharonyan desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT); 850540ccba0SVahram Aharonyan 851540ccba0SVahram Aharonyan desc->buf = dma_buff; 852540ccba0SVahram Aharonyan desc->status |= (DEV_DMA_L | DEV_DMA_IOC | 853540ccba0SVahram Aharonyan ((len << DEV_DMA_NBYTES_SHIFT) & mask)); 854540ccba0SVahram Aharonyan 855540ccba0SVahram Aharonyan if (hs_ep->dir_in) { 856540ccba0SVahram Aharonyan desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) & 857540ccba0SVahram Aharonyan DEV_DMA_ISOC_PID_MASK) | 858540ccba0SVahram Aharonyan ((len % hs_ep->ep.maxpacket) ? 859540ccba0SVahram Aharonyan DEV_DMA_SHORT : 0) | 860540ccba0SVahram Aharonyan ((hs_ep->target_frame << 861540ccba0SVahram Aharonyan DEV_DMA_ISOC_FRNUM_SHIFT) & 862540ccba0SVahram Aharonyan DEV_DMA_ISOC_FRNUM_MASK); 863540ccba0SVahram Aharonyan } 864540ccba0SVahram Aharonyan 865540ccba0SVahram Aharonyan desc->status &= ~DEV_DMA_BUFF_STS_MASK; 866540ccba0SVahram Aharonyan desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT); 867540ccba0SVahram Aharonyan 868540ccba0SVahram Aharonyan /* Update index of last configured entry in the chain */ 869540ccba0SVahram Aharonyan hs_ep->next_desc++; 870540ccba0SVahram Aharonyan 871540ccba0SVahram Aharonyan return 0; 872540ccba0SVahram Aharonyan } 873540ccba0SVahram Aharonyan 874540ccba0SVahram Aharonyan /* 875540ccba0SVahram Aharonyan * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA 876540ccba0SVahram Aharonyan * @hs_ep: The isochronous endpoint. 877540ccba0SVahram Aharonyan * 878540ccba0SVahram Aharonyan * Prepare first descriptor chain for isochronous endpoints. Afterwards 879540ccba0SVahram Aharonyan * write DMA address to HW and enable the endpoint. 880540ccba0SVahram Aharonyan * 881540ccba0SVahram Aharonyan * Switch between descriptor chains via isoc_chain_num to give SW opportunity 882540ccba0SVahram Aharonyan * to prepare second descriptor chain while first one is being processed by HW. 883540ccba0SVahram Aharonyan */ 884540ccba0SVahram Aharonyan static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep) 885540ccba0SVahram Aharonyan { 886540ccba0SVahram Aharonyan struct dwc2_hsotg *hsotg = hs_ep->parent; 887540ccba0SVahram Aharonyan struct dwc2_hsotg_req *hs_req, *treq; 888540ccba0SVahram Aharonyan int index = hs_ep->index; 889540ccba0SVahram Aharonyan int ret; 890540ccba0SVahram Aharonyan u32 dma_reg; 891540ccba0SVahram Aharonyan u32 depctl; 892540ccba0SVahram Aharonyan u32 ctrl; 893540ccba0SVahram Aharonyan 894540ccba0SVahram Aharonyan if (list_empty(&hs_ep->queue)) { 895540ccba0SVahram Aharonyan dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__); 896540ccba0SVahram Aharonyan return; 897540ccba0SVahram Aharonyan } 898540ccba0SVahram Aharonyan 899540ccba0SVahram Aharonyan list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) { 900540ccba0SVahram Aharonyan ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma, 901540ccba0SVahram Aharonyan hs_req->req.length); 902540ccba0SVahram Aharonyan if (ret) { 903540ccba0SVahram Aharonyan dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__); 904540ccba0SVahram Aharonyan break; 905540ccba0SVahram Aharonyan } 906540ccba0SVahram Aharonyan } 907540ccba0SVahram Aharonyan 908540ccba0SVahram Aharonyan depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); 909540ccba0SVahram Aharonyan dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index); 910540ccba0SVahram Aharonyan 911540ccba0SVahram Aharonyan /* write descriptor chain address to control register */ 912540ccba0SVahram Aharonyan dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg); 913540ccba0SVahram Aharonyan 914540ccba0SVahram Aharonyan ctrl = dwc2_readl(hsotg->regs + depctl); 915540ccba0SVahram Aharonyan ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK; 916540ccba0SVahram Aharonyan dwc2_writel(ctrl, hsotg->regs + depctl); 917540ccba0SVahram Aharonyan 918540ccba0SVahram Aharonyan /* Switch ISOC descriptor chain number being processed by SW*/ 919540ccba0SVahram Aharonyan hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1; 920540ccba0SVahram Aharonyan hs_ep->next_desc = 0; 921540ccba0SVahram Aharonyan } 922540ccba0SVahram Aharonyan 923cf77b5fbSVahram Aharonyan /** 9241f91b4ccSFelipe Balbi * dwc2_hsotg_start_req - start a USB request from an endpoint's queue 92547a1685fSDinh Nguyen * @hsotg: The controller state. 92647a1685fSDinh Nguyen * @hs_ep: The endpoint to process a request for 92747a1685fSDinh Nguyen * @hs_req: The request to start. 92847a1685fSDinh Nguyen * @continuing: True if we are doing more for the current request. 92947a1685fSDinh Nguyen * 93047a1685fSDinh Nguyen * Start the given request running by setting the endpoint registers 93147a1685fSDinh Nguyen * appropriately, and writing any data to the FIFOs. 93247a1685fSDinh Nguyen */ 9331f91b4ccSFelipe Balbi static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg, 9341f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep, 9351f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req, 93647a1685fSDinh Nguyen bool continuing) 93747a1685fSDinh Nguyen { 93847a1685fSDinh Nguyen struct usb_request *ureq = &hs_req->req; 93947a1685fSDinh Nguyen int index = hs_ep->index; 94047a1685fSDinh Nguyen int dir_in = hs_ep->dir_in; 94147a1685fSDinh Nguyen u32 epctrl_reg; 94247a1685fSDinh Nguyen u32 epsize_reg; 94347a1685fSDinh Nguyen u32 epsize; 94447a1685fSDinh Nguyen u32 ctrl; 9459da51974SJohn Youn unsigned int length; 9469da51974SJohn Youn unsigned int packets; 9479da51974SJohn Youn unsigned int maxreq; 948aa3e8bc8SVahram Aharonyan unsigned int dma_reg; 94947a1685fSDinh Nguyen 95047a1685fSDinh Nguyen if (index != 0) { 95147a1685fSDinh Nguyen if (hs_ep->req && !continuing) { 95247a1685fSDinh Nguyen dev_err(hsotg->dev, "%s: active request\n", __func__); 95347a1685fSDinh Nguyen WARN_ON(1); 95447a1685fSDinh Nguyen return; 95547a1685fSDinh Nguyen } else if (hs_ep->req != hs_req && continuing) { 95647a1685fSDinh Nguyen dev_err(hsotg->dev, 95747a1685fSDinh Nguyen "%s: continue different req\n", __func__); 95847a1685fSDinh Nguyen WARN_ON(1); 95947a1685fSDinh Nguyen return; 96047a1685fSDinh Nguyen } 96147a1685fSDinh Nguyen } 96247a1685fSDinh Nguyen 963aa3e8bc8SVahram Aharonyan dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index); 96447a1685fSDinh Nguyen epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); 96547a1685fSDinh Nguyen epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); 96647a1685fSDinh Nguyen 96747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n", 96895c8bc36SAntti Seppälä __func__, dwc2_readl(hsotg->regs + epctrl_reg), index, 96947a1685fSDinh Nguyen hs_ep->dir_in ? "in" : "out"); 97047a1685fSDinh Nguyen 97147a1685fSDinh Nguyen /* If endpoint is stalled, we will restart request later */ 97295c8bc36SAntti Seppälä ctrl = dwc2_readl(hsotg->regs + epctrl_reg); 97347a1685fSDinh Nguyen 974b2d4c54eSMian Yousaf Kaukab if (index && ctrl & DXEPCTL_STALL) { 97547a1685fSDinh Nguyen dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index); 97647a1685fSDinh Nguyen return; 97747a1685fSDinh Nguyen } 97847a1685fSDinh Nguyen 97947a1685fSDinh Nguyen length = ureq->length - ureq->actual; 98047a1685fSDinh Nguyen dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n", 98147a1685fSDinh Nguyen ureq->length, ureq->actual); 98247a1685fSDinh Nguyen 983cf77b5fbSVahram Aharonyan if (!using_desc_dma(hsotg)) 98447a1685fSDinh Nguyen maxreq = get_ep_limit(hs_ep); 985cf77b5fbSVahram Aharonyan else 986cf77b5fbSVahram Aharonyan maxreq = dwc2_gadget_get_chain_limit(hs_ep); 987cf77b5fbSVahram Aharonyan 98847a1685fSDinh Nguyen if (length > maxreq) { 98947a1685fSDinh Nguyen int round = maxreq % hs_ep->ep.maxpacket; 99047a1685fSDinh Nguyen 99147a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n", 99247a1685fSDinh Nguyen __func__, length, maxreq, round); 99347a1685fSDinh Nguyen 99447a1685fSDinh Nguyen /* round down to multiple of packets */ 99547a1685fSDinh Nguyen if (round) 99647a1685fSDinh Nguyen maxreq -= round; 99747a1685fSDinh Nguyen 99847a1685fSDinh Nguyen length = maxreq; 99947a1685fSDinh Nguyen } 100047a1685fSDinh Nguyen 100147a1685fSDinh Nguyen if (length) 100247a1685fSDinh Nguyen packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket); 100347a1685fSDinh Nguyen else 100447a1685fSDinh Nguyen packets = 1; /* send one packet if length is zero. */ 100547a1685fSDinh Nguyen 100647a1685fSDinh Nguyen if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) { 100747a1685fSDinh Nguyen dev_err(hsotg->dev, "req length > maxpacket*mc\n"); 100847a1685fSDinh Nguyen return; 100947a1685fSDinh Nguyen } 101047a1685fSDinh Nguyen 101147a1685fSDinh Nguyen if (dir_in && index != 0) 101247a1685fSDinh Nguyen if (hs_ep->isochronous) 101347a1685fSDinh Nguyen epsize = DXEPTSIZ_MC(packets); 101447a1685fSDinh Nguyen else 101547a1685fSDinh Nguyen epsize = DXEPTSIZ_MC(1); 101647a1685fSDinh Nguyen else 101747a1685fSDinh Nguyen epsize = 0; 101847a1685fSDinh Nguyen 101947a1685fSDinh Nguyen /* 1020f71b5e25SMian Yousaf Kaukab * zero length packet should be programmed on its own and should not 1021f71b5e25SMian Yousaf Kaukab * be counted in DIEPTSIZ.PktCnt with other packets. 102247a1685fSDinh Nguyen */ 1023f71b5e25SMian Yousaf Kaukab if (dir_in && ureq->zero && !continuing) { 1024f71b5e25SMian Yousaf Kaukab /* Test if zlp is actually required. */ 1025f71b5e25SMian Yousaf Kaukab if ((ureq->length >= hs_ep->ep.maxpacket) && 1026f71b5e25SMian Yousaf Kaukab !(ureq->length % hs_ep->ep.maxpacket)) 10278a20fa45SMian Yousaf Kaukab hs_ep->send_zlp = 1; 102847a1685fSDinh Nguyen } 102947a1685fSDinh Nguyen 103047a1685fSDinh Nguyen epsize |= DXEPTSIZ_PKTCNT(packets); 103147a1685fSDinh Nguyen epsize |= DXEPTSIZ_XFERSIZE(length); 103247a1685fSDinh Nguyen 103347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n", 103447a1685fSDinh Nguyen __func__, packets, length, ureq->length, epsize, epsize_reg); 103547a1685fSDinh Nguyen 103647a1685fSDinh Nguyen /* store the request as the current one we're doing */ 103747a1685fSDinh Nguyen hs_ep->req = hs_req; 103847a1685fSDinh Nguyen 1039aa3e8bc8SVahram Aharonyan if (using_desc_dma(hsotg)) { 1040aa3e8bc8SVahram Aharonyan u32 offset = 0; 1041aa3e8bc8SVahram Aharonyan u32 mps = hs_ep->ep.maxpacket; 1042aa3e8bc8SVahram Aharonyan 1043aa3e8bc8SVahram Aharonyan /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */ 1044aa3e8bc8SVahram Aharonyan if (!dir_in) { 1045aa3e8bc8SVahram Aharonyan if (!index) 1046aa3e8bc8SVahram Aharonyan length = mps; 1047aa3e8bc8SVahram Aharonyan else if (length % mps) 1048aa3e8bc8SVahram Aharonyan length += (mps - (length % mps)); 1049aa3e8bc8SVahram Aharonyan } 1050aa3e8bc8SVahram Aharonyan 1051aa3e8bc8SVahram Aharonyan /* 1052aa3e8bc8SVahram Aharonyan * If more data to send, adjust DMA for EP0 out data stage. 1053aa3e8bc8SVahram Aharonyan * ureq->dma stays unchanged, hence increment it by already 1054aa3e8bc8SVahram Aharonyan * passed passed data count before starting new transaction. 1055aa3e8bc8SVahram Aharonyan */ 1056aa3e8bc8SVahram Aharonyan if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT && 1057aa3e8bc8SVahram Aharonyan continuing) 1058aa3e8bc8SVahram Aharonyan offset = ureq->actual; 1059aa3e8bc8SVahram Aharonyan 1060aa3e8bc8SVahram Aharonyan /* Fill DDMA chain entries */ 1061aa3e8bc8SVahram Aharonyan dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset, 1062aa3e8bc8SVahram Aharonyan length); 1063aa3e8bc8SVahram Aharonyan 1064aa3e8bc8SVahram Aharonyan /* write descriptor chain address to control register */ 1065aa3e8bc8SVahram Aharonyan dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg); 1066aa3e8bc8SVahram Aharonyan 1067aa3e8bc8SVahram Aharonyan dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n", 1068aa3e8bc8SVahram Aharonyan __func__, (u32)hs_ep->desc_list_dma, dma_reg); 1069aa3e8bc8SVahram Aharonyan } else { 107047a1685fSDinh Nguyen /* write size / packets */ 107195c8bc36SAntti Seppälä dwc2_writel(epsize, hsotg->regs + epsize_reg); 107247a1685fSDinh Nguyen 1073729e6574SRazmik Karapetyan if (using_dma(hsotg) && !continuing && (length != 0)) { 107447a1685fSDinh Nguyen /* 1075aa3e8bc8SVahram Aharonyan * write DMA address to control register, buffer 1076aa3e8bc8SVahram Aharonyan * already synced by dwc2_hsotg_ep_queue(). 107747a1685fSDinh Nguyen */ 107847a1685fSDinh Nguyen 107995c8bc36SAntti Seppälä dwc2_writel(ureq->dma, hsotg->regs + dma_reg); 108047a1685fSDinh Nguyen 10810cc4cf6fSFabio Estevam dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n", 108247a1685fSDinh Nguyen __func__, &ureq->dma, dma_reg); 108347a1685fSDinh Nguyen } 1084aa3e8bc8SVahram Aharonyan } 108547a1685fSDinh Nguyen 1086837e9f00SVardan Mikayelyan if (hs_ep->isochronous && hs_ep->interval == 1) { 1087837e9f00SVardan Mikayelyan hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg); 1088837e9f00SVardan Mikayelyan dwc2_gadget_incr_frame_num(hs_ep); 1089837e9f00SVardan Mikayelyan 1090837e9f00SVardan Mikayelyan if (hs_ep->target_frame & 0x1) 1091837e9f00SVardan Mikayelyan ctrl |= DXEPCTL_SETODDFR; 1092837e9f00SVardan Mikayelyan else 1093837e9f00SVardan Mikayelyan ctrl |= DXEPCTL_SETEVENFR; 1094837e9f00SVardan Mikayelyan } 1095837e9f00SVardan Mikayelyan 109647a1685fSDinh Nguyen ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ 109747a1685fSDinh Nguyen 1098fe0b94abSMian Yousaf Kaukab dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state); 109947a1685fSDinh Nguyen 110047a1685fSDinh Nguyen /* For Setup request do not clear NAK */ 1101fe0b94abSMian Yousaf Kaukab if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP)) 110247a1685fSDinh Nguyen ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ 110347a1685fSDinh Nguyen 110447a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); 110595c8bc36SAntti Seppälä dwc2_writel(ctrl, hsotg->regs + epctrl_reg); 110647a1685fSDinh Nguyen 110747a1685fSDinh Nguyen /* 110847a1685fSDinh Nguyen * set these, it seems that DMA support increments past the end 110947a1685fSDinh Nguyen * of the packet buffer so we need to calculate the length from 111047a1685fSDinh Nguyen * this information. 111147a1685fSDinh Nguyen */ 111247a1685fSDinh Nguyen hs_ep->size_loaded = length; 111347a1685fSDinh Nguyen hs_ep->last_load = ureq->actual; 111447a1685fSDinh Nguyen 111547a1685fSDinh Nguyen if (dir_in && !using_dma(hsotg)) { 111647a1685fSDinh Nguyen /* set these anyway, we may need them for non-periodic in */ 111747a1685fSDinh Nguyen hs_ep->fifo_load = 0; 111847a1685fSDinh Nguyen 11191f91b4ccSFelipe Balbi dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); 112047a1685fSDinh Nguyen } 112147a1685fSDinh Nguyen 112247a1685fSDinh Nguyen /* 112347a1685fSDinh Nguyen * Note, trying to clear the NAK here causes problems with transmit 112447a1685fSDinh Nguyen * on the S3C6400 ending up with the TXFIFO becoming full. 112547a1685fSDinh Nguyen */ 112647a1685fSDinh Nguyen 112747a1685fSDinh Nguyen /* check ep is enabled */ 112895c8bc36SAntti Seppälä if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA)) 11291a0ed863SMian Yousaf Kaukab dev_dbg(hsotg->dev, 113047a1685fSDinh Nguyen "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n", 113195c8bc36SAntti Seppälä index, dwc2_readl(hsotg->regs + epctrl_reg)); 113247a1685fSDinh Nguyen 113347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n", 113495c8bc36SAntti Seppälä __func__, dwc2_readl(hsotg->regs + epctrl_reg)); 113547a1685fSDinh Nguyen 113647a1685fSDinh Nguyen /* enable ep interrupts */ 11371f91b4ccSFelipe Balbi dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1); 113847a1685fSDinh Nguyen } 113947a1685fSDinh Nguyen 114047a1685fSDinh Nguyen /** 11411f91b4ccSFelipe Balbi * dwc2_hsotg_map_dma - map the DMA memory being used for the request 114247a1685fSDinh Nguyen * @hsotg: The device state. 114347a1685fSDinh Nguyen * @hs_ep: The endpoint the request is on. 114447a1685fSDinh Nguyen * @req: The request being processed. 114547a1685fSDinh Nguyen * 114647a1685fSDinh Nguyen * We've been asked to queue a request, so ensure that the memory buffer 114747a1685fSDinh Nguyen * is correctly setup for DMA. If we've been passed an extant DMA address 114847a1685fSDinh Nguyen * then ensure the buffer has been synced to memory. If our buffer has no 114947a1685fSDinh Nguyen * DMA memory, then we map the memory and mark our request to allow us to 115047a1685fSDinh Nguyen * cleanup on completion. 115147a1685fSDinh Nguyen */ 11521f91b4ccSFelipe Balbi static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg, 11531f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep, 115447a1685fSDinh Nguyen struct usb_request *req) 115547a1685fSDinh Nguyen { 115647a1685fSDinh Nguyen int ret; 115747a1685fSDinh Nguyen 115847a1685fSDinh Nguyen ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in); 115947a1685fSDinh Nguyen if (ret) 116047a1685fSDinh Nguyen goto dma_error; 116147a1685fSDinh Nguyen 116247a1685fSDinh Nguyen return 0; 116347a1685fSDinh Nguyen 116447a1685fSDinh Nguyen dma_error: 116547a1685fSDinh Nguyen dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n", 116647a1685fSDinh Nguyen __func__, req->buf, req->length); 116747a1685fSDinh Nguyen 116847a1685fSDinh Nguyen return -EIO; 116947a1685fSDinh Nguyen } 117047a1685fSDinh Nguyen 11711f91b4ccSFelipe Balbi static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg, 1172b98866c2SJohn Youn struct dwc2_hsotg_ep *hs_ep, 1173b98866c2SJohn Youn struct dwc2_hsotg_req *hs_req) 11747d24c1b5SMian Yousaf Kaukab { 11757d24c1b5SMian Yousaf Kaukab void *req_buf = hs_req->req.buf; 11767d24c1b5SMian Yousaf Kaukab 11777d24c1b5SMian Yousaf Kaukab /* If dma is not being used or buffer is aligned */ 11787d24c1b5SMian Yousaf Kaukab if (!using_dma(hsotg) || !((long)req_buf & 3)) 11797d24c1b5SMian Yousaf Kaukab return 0; 11807d24c1b5SMian Yousaf Kaukab 11817d24c1b5SMian Yousaf Kaukab WARN_ON(hs_req->saved_req_buf); 11827d24c1b5SMian Yousaf Kaukab 11837d24c1b5SMian Yousaf Kaukab dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__, 11847d24c1b5SMian Yousaf Kaukab hs_ep->ep.name, req_buf, hs_req->req.length); 11857d24c1b5SMian Yousaf Kaukab 11867d24c1b5SMian Yousaf Kaukab hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC); 11877d24c1b5SMian Yousaf Kaukab if (!hs_req->req.buf) { 11887d24c1b5SMian Yousaf Kaukab hs_req->req.buf = req_buf; 11897d24c1b5SMian Yousaf Kaukab dev_err(hsotg->dev, 11907d24c1b5SMian Yousaf Kaukab "%s: unable to allocate memory for bounce buffer\n", 11917d24c1b5SMian Yousaf Kaukab __func__); 11927d24c1b5SMian Yousaf Kaukab return -ENOMEM; 11937d24c1b5SMian Yousaf Kaukab } 11947d24c1b5SMian Yousaf Kaukab 11957d24c1b5SMian Yousaf Kaukab /* Save actual buffer */ 11967d24c1b5SMian Yousaf Kaukab hs_req->saved_req_buf = req_buf; 11977d24c1b5SMian Yousaf Kaukab 11987d24c1b5SMian Yousaf Kaukab if (hs_ep->dir_in) 11997d24c1b5SMian Yousaf Kaukab memcpy(hs_req->req.buf, req_buf, hs_req->req.length); 12007d24c1b5SMian Yousaf Kaukab return 0; 12017d24c1b5SMian Yousaf Kaukab } 12027d24c1b5SMian Yousaf Kaukab 1203b98866c2SJohn Youn static void 1204b98866c2SJohn Youn dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg, 1205b98866c2SJohn Youn struct dwc2_hsotg_ep *hs_ep, 1206b98866c2SJohn Youn struct dwc2_hsotg_req *hs_req) 12077d24c1b5SMian Yousaf Kaukab { 12087d24c1b5SMian Yousaf Kaukab /* If dma is not being used or buffer was aligned */ 12097d24c1b5SMian Yousaf Kaukab if (!using_dma(hsotg) || !hs_req->saved_req_buf) 12107d24c1b5SMian Yousaf Kaukab return; 12117d24c1b5SMian Yousaf Kaukab 12127d24c1b5SMian Yousaf Kaukab dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__, 12137d24c1b5SMian Yousaf Kaukab hs_ep->ep.name, hs_req->req.status, hs_req->req.actual); 12147d24c1b5SMian Yousaf Kaukab 12157d24c1b5SMian Yousaf Kaukab /* Copy data from bounce buffer on successful out transfer */ 12167d24c1b5SMian Yousaf Kaukab if (!hs_ep->dir_in && !hs_req->req.status) 12177d24c1b5SMian Yousaf Kaukab memcpy(hs_req->saved_req_buf, hs_req->req.buf, 12187d24c1b5SMian Yousaf Kaukab hs_req->req.actual); 12197d24c1b5SMian Yousaf Kaukab 12207d24c1b5SMian Yousaf Kaukab /* Free bounce buffer */ 12217d24c1b5SMian Yousaf Kaukab kfree(hs_req->req.buf); 12227d24c1b5SMian Yousaf Kaukab 12237d24c1b5SMian Yousaf Kaukab hs_req->req.buf = hs_req->saved_req_buf; 12247d24c1b5SMian Yousaf Kaukab hs_req->saved_req_buf = NULL; 12257d24c1b5SMian Yousaf Kaukab } 12267d24c1b5SMian Yousaf Kaukab 1227381fc8f8SVardan Mikayelyan /** 1228381fc8f8SVardan Mikayelyan * dwc2_gadget_target_frame_elapsed - Checks target frame 1229381fc8f8SVardan Mikayelyan * @hs_ep: The driver endpoint to check 1230381fc8f8SVardan Mikayelyan * 1231381fc8f8SVardan Mikayelyan * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop 1232381fc8f8SVardan Mikayelyan * corresponding transfer. 1233381fc8f8SVardan Mikayelyan */ 1234381fc8f8SVardan Mikayelyan static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep) 1235381fc8f8SVardan Mikayelyan { 1236381fc8f8SVardan Mikayelyan struct dwc2_hsotg *hsotg = hs_ep->parent; 1237381fc8f8SVardan Mikayelyan u32 target_frame = hs_ep->target_frame; 1238381fc8f8SVardan Mikayelyan u32 current_frame = dwc2_hsotg_read_frameno(hsotg); 1239381fc8f8SVardan Mikayelyan bool frame_overrun = hs_ep->frame_overrun; 1240381fc8f8SVardan Mikayelyan 1241381fc8f8SVardan Mikayelyan if (!frame_overrun && current_frame >= target_frame) 1242381fc8f8SVardan Mikayelyan return true; 1243381fc8f8SVardan Mikayelyan 1244381fc8f8SVardan Mikayelyan if (frame_overrun && current_frame >= target_frame && 1245381fc8f8SVardan Mikayelyan ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2)) 1246381fc8f8SVardan Mikayelyan return true; 1247381fc8f8SVardan Mikayelyan 1248381fc8f8SVardan Mikayelyan return false; 1249381fc8f8SVardan Mikayelyan } 1250381fc8f8SVardan Mikayelyan 1251e02f9aa6SVahram Aharonyan /* 1252e02f9aa6SVahram Aharonyan * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers 1253e02f9aa6SVahram Aharonyan * @hsotg: The driver state 1254e02f9aa6SVahram Aharonyan * @hs_ep: the ep descriptor chain is for 1255e02f9aa6SVahram Aharonyan * 1256e02f9aa6SVahram Aharonyan * Called to update EP0 structure's pointers depend on stage of 1257e02f9aa6SVahram Aharonyan * control transfer. 1258e02f9aa6SVahram Aharonyan */ 1259e02f9aa6SVahram Aharonyan static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg, 1260e02f9aa6SVahram Aharonyan struct dwc2_hsotg_ep *hs_ep) 1261e02f9aa6SVahram Aharonyan { 1262e02f9aa6SVahram Aharonyan switch (hsotg->ep0_state) { 1263e02f9aa6SVahram Aharonyan case DWC2_EP0_SETUP: 1264e02f9aa6SVahram Aharonyan case DWC2_EP0_STATUS_OUT: 1265e02f9aa6SVahram Aharonyan hs_ep->desc_list = hsotg->setup_desc[0]; 1266e02f9aa6SVahram Aharonyan hs_ep->desc_list_dma = hsotg->setup_desc_dma[0]; 1267e02f9aa6SVahram Aharonyan break; 1268e02f9aa6SVahram Aharonyan case DWC2_EP0_DATA_IN: 1269e02f9aa6SVahram Aharonyan case DWC2_EP0_STATUS_IN: 1270e02f9aa6SVahram Aharonyan hs_ep->desc_list = hsotg->ctrl_in_desc; 1271e02f9aa6SVahram Aharonyan hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma; 1272e02f9aa6SVahram Aharonyan break; 1273e02f9aa6SVahram Aharonyan case DWC2_EP0_DATA_OUT: 1274e02f9aa6SVahram Aharonyan hs_ep->desc_list = hsotg->ctrl_out_desc; 1275e02f9aa6SVahram Aharonyan hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma; 1276e02f9aa6SVahram Aharonyan break; 1277e02f9aa6SVahram Aharonyan default: 1278e02f9aa6SVahram Aharonyan dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n", 1279e02f9aa6SVahram Aharonyan hsotg->ep0_state); 1280e02f9aa6SVahram Aharonyan return -EINVAL; 1281e02f9aa6SVahram Aharonyan } 1282e02f9aa6SVahram Aharonyan 1283e02f9aa6SVahram Aharonyan return 0; 1284e02f9aa6SVahram Aharonyan } 1285e02f9aa6SVahram Aharonyan 12861f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req, 128747a1685fSDinh Nguyen gfp_t gfp_flags) 128847a1685fSDinh Nguyen { 12891f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req = our_req(req); 12901f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = our_ep(ep); 1291941fcce4SDinh Nguyen struct dwc2_hsotg *hs = hs_ep->parent; 129247a1685fSDinh Nguyen bool first; 12937d24c1b5SMian Yousaf Kaukab int ret; 129447a1685fSDinh Nguyen 129547a1685fSDinh Nguyen dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n", 129647a1685fSDinh Nguyen ep->name, req, req->length, req->buf, req->no_interrupt, 129747a1685fSDinh Nguyen req->zero, req->short_not_ok); 129847a1685fSDinh Nguyen 12997ababa92SGregory Herrero /* Prevent new request submission when controller is suspended */ 13007ababa92SGregory Herrero if (hs->lx_state == DWC2_L2) { 13017ababa92SGregory Herrero dev_dbg(hs->dev, "%s: don't submit request while suspended\n", 13027ababa92SGregory Herrero __func__); 13037ababa92SGregory Herrero return -EAGAIN; 13047ababa92SGregory Herrero } 13057ababa92SGregory Herrero 130647a1685fSDinh Nguyen /* initialise status of the request */ 130747a1685fSDinh Nguyen INIT_LIST_HEAD(&hs_req->queue); 130847a1685fSDinh Nguyen req->actual = 0; 130947a1685fSDinh Nguyen req->status = -EINPROGRESS; 131047a1685fSDinh Nguyen 13111f91b4ccSFelipe Balbi ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req); 13127d24c1b5SMian Yousaf Kaukab if (ret) 13137d24c1b5SMian Yousaf Kaukab return ret; 13147d24c1b5SMian Yousaf Kaukab 131547a1685fSDinh Nguyen /* if we're using DMA, sync the buffers as necessary */ 131647a1685fSDinh Nguyen if (using_dma(hs)) { 13171f91b4ccSFelipe Balbi ret = dwc2_hsotg_map_dma(hs, hs_ep, req); 131847a1685fSDinh Nguyen if (ret) 131947a1685fSDinh Nguyen return ret; 132047a1685fSDinh Nguyen } 1321e02f9aa6SVahram Aharonyan /* If using descriptor DMA configure EP0 descriptor chain pointers */ 1322e02f9aa6SVahram Aharonyan if (using_desc_dma(hs) && !hs_ep->index) { 1323e02f9aa6SVahram Aharonyan ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep); 1324e02f9aa6SVahram Aharonyan if (ret) 1325e02f9aa6SVahram Aharonyan return ret; 1326e02f9aa6SVahram Aharonyan } 132747a1685fSDinh Nguyen 132847a1685fSDinh Nguyen first = list_empty(&hs_ep->queue); 132947a1685fSDinh Nguyen list_add_tail(&hs_req->queue, &hs_ep->queue); 133047a1685fSDinh Nguyen 1331540ccba0SVahram Aharonyan /* 1332540ccba0SVahram Aharonyan * Handle DDMA isochronous transfers separately - just add new entry 1333540ccba0SVahram Aharonyan * to the half of descriptor chain that is not processed by HW. 1334540ccba0SVahram Aharonyan * Transfer will be started once SW gets either one of NAK or 1335540ccba0SVahram Aharonyan * OutTknEpDis interrupts. 1336540ccba0SVahram Aharonyan */ 1337540ccba0SVahram Aharonyan if (using_desc_dma(hs) && hs_ep->isochronous && 1338540ccba0SVahram Aharonyan hs_ep->target_frame != TARGET_FRAME_INITIAL) { 1339540ccba0SVahram Aharonyan ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma, 1340540ccba0SVahram Aharonyan hs_req->req.length); 1341540ccba0SVahram Aharonyan if (ret) 1342540ccba0SVahram Aharonyan dev_dbg(hs->dev, "%s: ISO desc chain full\n", __func__); 1343540ccba0SVahram Aharonyan 1344540ccba0SVahram Aharonyan return 0; 1345540ccba0SVahram Aharonyan } 1346540ccba0SVahram Aharonyan 1347837e9f00SVardan Mikayelyan if (first) { 1348837e9f00SVardan Mikayelyan if (!hs_ep->isochronous) { 13491f91b4ccSFelipe Balbi dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); 1350837e9f00SVardan Mikayelyan return 0; 1351837e9f00SVardan Mikayelyan } 135247a1685fSDinh Nguyen 1353837e9f00SVardan Mikayelyan while (dwc2_gadget_target_frame_elapsed(hs_ep)) 1354837e9f00SVardan Mikayelyan dwc2_gadget_incr_frame_num(hs_ep); 1355837e9f00SVardan Mikayelyan 1356837e9f00SVardan Mikayelyan if (hs_ep->target_frame != TARGET_FRAME_INITIAL) 1357837e9f00SVardan Mikayelyan dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); 1358837e9f00SVardan Mikayelyan } 135947a1685fSDinh Nguyen return 0; 136047a1685fSDinh Nguyen } 136147a1685fSDinh Nguyen 13621f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req, 136347a1685fSDinh Nguyen gfp_t gfp_flags) 136447a1685fSDinh Nguyen { 13651f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = our_ep(ep); 1366941fcce4SDinh Nguyen struct dwc2_hsotg *hs = hs_ep->parent; 136747a1685fSDinh Nguyen unsigned long flags = 0; 136847a1685fSDinh Nguyen int ret = 0; 136947a1685fSDinh Nguyen 137047a1685fSDinh Nguyen spin_lock_irqsave(&hs->lock, flags); 13711f91b4ccSFelipe Balbi ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags); 137247a1685fSDinh Nguyen spin_unlock_irqrestore(&hs->lock, flags); 137347a1685fSDinh Nguyen 137447a1685fSDinh Nguyen return ret; 137547a1685fSDinh Nguyen } 137647a1685fSDinh Nguyen 13771f91b4ccSFelipe Balbi static void dwc2_hsotg_ep_free_request(struct usb_ep *ep, 137847a1685fSDinh Nguyen struct usb_request *req) 137947a1685fSDinh Nguyen { 13801f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req = our_req(req); 138147a1685fSDinh Nguyen 138247a1685fSDinh Nguyen kfree(hs_req); 138347a1685fSDinh Nguyen } 138447a1685fSDinh Nguyen 138547a1685fSDinh Nguyen /** 13861f91b4ccSFelipe Balbi * dwc2_hsotg_complete_oursetup - setup completion callback 138747a1685fSDinh Nguyen * @ep: The endpoint the request was on. 138847a1685fSDinh Nguyen * @req: The request completed. 138947a1685fSDinh Nguyen * 139047a1685fSDinh Nguyen * Called on completion of any requests the driver itself 139147a1685fSDinh Nguyen * submitted that need cleaning up. 139247a1685fSDinh Nguyen */ 13931f91b4ccSFelipe Balbi static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep, 139447a1685fSDinh Nguyen struct usb_request *req) 139547a1685fSDinh Nguyen { 13961f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = our_ep(ep); 1397941fcce4SDinh Nguyen struct dwc2_hsotg *hsotg = hs_ep->parent; 139847a1685fSDinh Nguyen 139947a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req); 140047a1685fSDinh Nguyen 14011f91b4ccSFelipe Balbi dwc2_hsotg_ep_free_request(ep, req); 140247a1685fSDinh Nguyen } 140347a1685fSDinh Nguyen 140447a1685fSDinh Nguyen /** 140547a1685fSDinh Nguyen * ep_from_windex - convert control wIndex value to endpoint 140647a1685fSDinh Nguyen * @hsotg: The driver state. 140747a1685fSDinh Nguyen * @windex: The control request wIndex field (in host order). 140847a1685fSDinh Nguyen * 140947a1685fSDinh Nguyen * Convert the given wIndex into a pointer to an driver endpoint 141047a1685fSDinh Nguyen * structure, or return NULL if it is not a valid endpoint. 141147a1685fSDinh Nguyen */ 14121f91b4ccSFelipe Balbi static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg, 141347a1685fSDinh Nguyen u32 windex) 141447a1685fSDinh Nguyen { 14151f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep; 141647a1685fSDinh Nguyen int dir = (windex & USB_DIR_IN) ? 1 : 0; 141747a1685fSDinh Nguyen int idx = windex & 0x7F; 141847a1685fSDinh Nguyen 141947a1685fSDinh Nguyen if (windex >= 0x100) 142047a1685fSDinh Nguyen return NULL; 142147a1685fSDinh Nguyen 142247a1685fSDinh Nguyen if (idx > hsotg->num_of_eps) 142347a1685fSDinh Nguyen return NULL; 142447a1685fSDinh Nguyen 1425c6f5c050SMian Yousaf Kaukab ep = index_to_ep(hsotg, idx, dir); 1426c6f5c050SMian Yousaf Kaukab 142747a1685fSDinh Nguyen if (idx && ep->dir_in != dir) 142847a1685fSDinh Nguyen return NULL; 142947a1685fSDinh Nguyen 143047a1685fSDinh Nguyen return ep; 143147a1685fSDinh Nguyen } 143247a1685fSDinh Nguyen 143347a1685fSDinh Nguyen /** 14341f91b4ccSFelipe Balbi * dwc2_hsotg_set_test_mode - Enable usb Test Modes 14359e14d0a5SGregory Herrero * @hsotg: The driver state. 14369e14d0a5SGregory Herrero * @testmode: requested usb test mode 14379e14d0a5SGregory Herrero * Enable usb Test Mode requested by the Host. 14389e14d0a5SGregory Herrero */ 14391f91b4ccSFelipe Balbi int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode) 14409e14d0a5SGregory Herrero { 144195c8bc36SAntti Seppälä int dctl = dwc2_readl(hsotg->regs + DCTL); 14429e14d0a5SGregory Herrero 14439e14d0a5SGregory Herrero dctl &= ~DCTL_TSTCTL_MASK; 14449e14d0a5SGregory Herrero switch (testmode) { 14459e14d0a5SGregory Herrero case TEST_J: 14469e14d0a5SGregory Herrero case TEST_K: 14479e14d0a5SGregory Herrero case TEST_SE0_NAK: 14489e14d0a5SGregory Herrero case TEST_PACKET: 14499e14d0a5SGregory Herrero case TEST_FORCE_EN: 14509e14d0a5SGregory Herrero dctl |= testmode << DCTL_TSTCTL_SHIFT; 14519e14d0a5SGregory Herrero break; 14529e14d0a5SGregory Herrero default: 14539e14d0a5SGregory Herrero return -EINVAL; 14549e14d0a5SGregory Herrero } 145595c8bc36SAntti Seppälä dwc2_writel(dctl, hsotg->regs + DCTL); 14569e14d0a5SGregory Herrero return 0; 14579e14d0a5SGregory Herrero } 14589e14d0a5SGregory Herrero 14599e14d0a5SGregory Herrero /** 14601f91b4ccSFelipe Balbi * dwc2_hsotg_send_reply - send reply to control request 146147a1685fSDinh Nguyen * @hsotg: The device state 146247a1685fSDinh Nguyen * @ep: Endpoint 0 146347a1685fSDinh Nguyen * @buff: Buffer for request 146447a1685fSDinh Nguyen * @length: Length of reply. 146547a1685fSDinh Nguyen * 146647a1685fSDinh Nguyen * Create a request and queue it on the given endpoint. This is useful as 146747a1685fSDinh Nguyen * an internal method of sending replies to certain control requests, etc. 146847a1685fSDinh Nguyen */ 14691f91b4ccSFelipe Balbi static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg, 14701f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep, 147147a1685fSDinh Nguyen void *buff, 147247a1685fSDinh Nguyen int length) 147347a1685fSDinh Nguyen { 147447a1685fSDinh Nguyen struct usb_request *req; 147547a1685fSDinh Nguyen int ret; 147647a1685fSDinh Nguyen 147747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length); 147847a1685fSDinh Nguyen 14791f91b4ccSFelipe Balbi req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC); 148047a1685fSDinh Nguyen hsotg->ep0_reply = req; 148147a1685fSDinh Nguyen if (!req) { 148247a1685fSDinh Nguyen dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__); 148347a1685fSDinh Nguyen return -ENOMEM; 148447a1685fSDinh Nguyen } 148547a1685fSDinh Nguyen 148647a1685fSDinh Nguyen req->buf = hsotg->ep0_buff; 148747a1685fSDinh Nguyen req->length = length; 1488f71b5e25SMian Yousaf Kaukab /* 1489f71b5e25SMian Yousaf Kaukab * zero flag is for sending zlp in DATA IN stage. It has no impact on 1490f71b5e25SMian Yousaf Kaukab * STATUS stage. 1491f71b5e25SMian Yousaf Kaukab */ 1492f71b5e25SMian Yousaf Kaukab req->zero = 0; 14931f91b4ccSFelipe Balbi req->complete = dwc2_hsotg_complete_oursetup; 149447a1685fSDinh Nguyen 149547a1685fSDinh Nguyen if (length) 149647a1685fSDinh Nguyen memcpy(req->buf, buff, length); 149747a1685fSDinh Nguyen 14981f91b4ccSFelipe Balbi ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC); 149947a1685fSDinh Nguyen if (ret) { 150047a1685fSDinh Nguyen dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__); 150147a1685fSDinh Nguyen return ret; 150247a1685fSDinh Nguyen } 150347a1685fSDinh Nguyen 150447a1685fSDinh Nguyen return 0; 150547a1685fSDinh Nguyen } 150647a1685fSDinh Nguyen 150747a1685fSDinh Nguyen /** 15081f91b4ccSFelipe Balbi * dwc2_hsotg_process_req_status - process request GET_STATUS 150947a1685fSDinh Nguyen * @hsotg: The device state 151047a1685fSDinh Nguyen * @ctrl: USB control request 151147a1685fSDinh Nguyen */ 15121f91b4ccSFelipe Balbi static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg, 151347a1685fSDinh Nguyen struct usb_ctrlrequest *ctrl) 151447a1685fSDinh Nguyen { 15151f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; 15161f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep; 151747a1685fSDinh Nguyen __le16 reply; 151847a1685fSDinh Nguyen int ret; 151947a1685fSDinh Nguyen 152047a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__); 152147a1685fSDinh Nguyen 152247a1685fSDinh Nguyen if (!ep0->dir_in) { 152347a1685fSDinh Nguyen dev_warn(hsotg->dev, "%s: direction out?\n", __func__); 152447a1685fSDinh Nguyen return -EINVAL; 152547a1685fSDinh Nguyen } 152647a1685fSDinh Nguyen 152747a1685fSDinh Nguyen switch (ctrl->bRequestType & USB_RECIP_MASK) { 152847a1685fSDinh Nguyen case USB_RECIP_DEVICE: 152938beaec6SJohn Youn /* 153038beaec6SJohn Youn * bit 0 => self powered 153138beaec6SJohn Youn * bit 1 => remote wakeup 153238beaec6SJohn Youn */ 153338beaec6SJohn Youn reply = cpu_to_le16(0); 153447a1685fSDinh Nguyen break; 153547a1685fSDinh Nguyen 153647a1685fSDinh Nguyen case USB_RECIP_INTERFACE: 153747a1685fSDinh Nguyen /* currently, the data result should be zero */ 153847a1685fSDinh Nguyen reply = cpu_to_le16(0); 153947a1685fSDinh Nguyen break; 154047a1685fSDinh Nguyen 154147a1685fSDinh Nguyen case USB_RECIP_ENDPOINT: 154247a1685fSDinh Nguyen ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex)); 154347a1685fSDinh Nguyen if (!ep) 154447a1685fSDinh Nguyen return -ENOENT; 154547a1685fSDinh Nguyen 154647a1685fSDinh Nguyen reply = cpu_to_le16(ep->halted ? 1 : 0); 154747a1685fSDinh Nguyen break; 154847a1685fSDinh Nguyen 154947a1685fSDinh Nguyen default: 155047a1685fSDinh Nguyen return 0; 155147a1685fSDinh Nguyen } 155247a1685fSDinh Nguyen 155347a1685fSDinh Nguyen if (le16_to_cpu(ctrl->wLength) != 2) 155447a1685fSDinh Nguyen return -EINVAL; 155547a1685fSDinh Nguyen 15561f91b4ccSFelipe Balbi ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2); 155747a1685fSDinh Nguyen if (ret) { 155847a1685fSDinh Nguyen dev_err(hsotg->dev, "%s: failed to send reply\n", __func__); 155947a1685fSDinh Nguyen return ret; 156047a1685fSDinh Nguyen } 156147a1685fSDinh Nguyen 156247a1685fSDinh Nguyen return 1; 156347a1685fSDinh Nguyen } 156447a1685fSDinh Nguyen 156551da43b5SVahram Aharonyan static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now); 156647a1685fSDinh Nguyen 156747a1685fSDinh Nguyen /** 156847a1685fSDinh Nguyen * get_ep_head - return the first request on the endpoint 156947a1685fSDinh Nguyen * @hs_ep: The controller endpoint to get 157047a1685fSDinh Nguyen * 157147a1685fSDinh Nguyen * Get the first request on the endpoint. 157247a1685fSDinh Nguyen */ 15731f91b4ccSFelipe Balbi static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep) 157447a1685fSDinh Nguyen { 1575ffc4b406SMasahiro Yamada return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req, 1576ffc4b406SMasahiro Yamada queue); 157747a1685fSDinh Nguyen } 157847a1685fSDinh Nguyen 157947a1685fSDinh Nguyen /** 158041cc4cd2SVardan Mikayelyan * dwc2_gadget_start_next_request - Starts next request from ep queue 158141cc4cd2SVardan Mikayelyan * @hs_ep: Endpoint structure 158241cc4cd2SVardan Mikayelyan * 158341cc4cd2SVardan Mikayelyan * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked 158441cc4cd2SVardan Mikayelyan * in its handler. Hence we need to unmask it here to be able to do 158541cc4cd2SVardan Mikayelyan * resynchronization. 158641cc4cd2SVardan Mikayelyan */ 158741cc4cd2SVardan Mikayelyan static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep) 158841cc4cd2SVardan Mikayelyan { 158941cc4cd2SVardan Mikayelyan u32 mask; 159041cc4cd2SVardan Mikayelyan struct dwc2_hsotg *hsotg = hs_ep->parent; 159141cc4cd2SVardan Mikayelyan int dir_in = hs_ep->dir_in; 159241cc4cd2SVardan Mikayelyan struct dwc2_hsotg_req *hs_req; 159341cc4cd2SVardan Mikayelyan u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; 159441cc4cd2SVardan Mikayelyan 159541cc4cd2SVardan Mikayelyan if (!list_empty(&hs_ep->queue)) { 159641cc4cd2SVardan Mikayelyan hs_req = get_ep_head(hs_ep); 159741cc4cd2SVardan Mikayelyan dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false); 159841cc4cd2SVardan Mikayelyan return; 159941cc4cd2SVardan Mikayelyan } 160041cc4cd2SVardan Mikayelyan if (!hs_ep->isochronous) 160141cc4cd2SVardan Mikayelyan return; 160241cc4cd2SVardan Mikayelyan 160341cc4cd2SVardan Mikayelyan if (dir_in) { 160441cc4cd2SVardan Mikayelyan dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n", 160541cc4cd2SVardan Mikayelyan __func__); 160641cc4cd2SVardan Mikayelyan } else { 160741cc4cd2SVardan Mikayelyan dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n", 160841cc4cd2SVardan Mikayelyan __func__); 160941cc4cd2SVardan Mikayelyan mask = dwc2_readl(hsotg->regs + epmsk_reg); 161041cc4cd2SVardan Mikayelyan mask |= DOEPMSK_OUTTKNEPDISMSK; 161141cc4cd2SVardan Mikayelyan dwc2_writel(mask, hsotg->regs + epmsk_reg); 161241cc4cd2SVardan Mikayelyan } 161341cc4cd2SVardan Mikayelyan } 161441cc4cd2SVardan Mikayelyan 161541cc4cd2SVardan Mikayelyan /** 16161f91b4ccSFelipe Balbi * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE 161747a1685fSDinh Nguyen * @hsotg: The device state 161847a1685fSDinh Nguyen * @ctrl: USB control request 161947a1685fSDinh Nguyen */ 16201f91b4ccSFelipe Balbi static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg, 162147a1685fSDinh Nguyen struct usb_ctrlrequest *ctrl) 162247a1685fSDinh Nguyen { 16231f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; 16241f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req; 162547a1685fSDinh Nguyen bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE); 16261f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep; 162747a1685fSDinh Nguyen int ret; 162847a1685fSDinh Nguyen bool halted; 16299e14d0a5SGregory Herrero u32 recip; 16309e14d0a5SGregory Herrero u32 wValue; 16319e14d0a5SGregory Herrero u32 wIndex; 163247a1685fSDinh Nguyen 163347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: %s_FEATURE\n", 163447a1685fSDinh Nguyen __func__, set ? "SET" : "CLEAR"); 163547a1685fSDinh Nguyen 16369e14d0a5SGregory Herrero wValue = le16_to_cpu(ctrl->wValue); 16379e14d0a5SGregory Herrero wIndex = le16_to_cpu(ctrl->wIndex); 16389e14d0a5SGregory Herrero recip = ctrl->bRequestType & USB_RECIP_MASK; 16399e14d0a5SGregory Herrero 16409e14d0a5SGregory Herrero switch (recip) { 16419e14d0a5SGregory Herrero case USB_RECIP_DEVICE: 16429e14d0a5SGregory Herrero switch (wValue) { 16439e14d0a5SGregory Herrero case USB_DEVICE_TEST_MODE: 16449e14d0a5SGregory Herrero if ((wIndex & 0xff) != 0) 16459e14d0a5SGregory Herrero return -EINVAL; 16469e14d0a5SGregory Herrero if (!set) 16479e14d0a5SGregory Herrero return -EINVAL; 16489e14d0a5SGregory Herrero 16499e14d0a5SGregory Herrero hsotg->test_mode = wIndex >> 8; 16501f91b4ccSFelipe Balbi ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); 16519e14d0a5SGregory Herrero if (ret) { 16529e14d0a5SGregory Herrero dev_err(hsotg->dev, 16539e14d0a5SGregory Herrero "%s: failed to send reply\n", __func__); 16549e14d0a5SGregory Herrero return ret; 16559e14d0a5SGregory Herrero } 16569e14d0a5SGregory Herrero break; 16579e14d0a5SGregory Herrero default: 16589e14d0a5SGregory Herrero return -ENOENT; 16599e14d0a5SGregory Herrero } 16609e14d0a5SGregory Herrero break; 16619e14d0a5SGregory Herrero 16629e14d0a5SGregory Herrero case USB_RECIP_ENDPOINT: 16639e14d0a5SGregory Herrero ep = ep_from_windex(hsotg, wIndex); 166447a1685fSDinh Nguyen if (!ep) { 166547a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n", 16669e14d0a5SGregory Herrero __func__, wIndex); 166747a1685fSDinh Nguyen return -ENOENT; 166847a1685fSDinh Nguyen } 166947a1685fSDinh Nguyen 16709e14d0a5SGregory Herrero switch (wValue) { 167147a1685fSDinh Nguyen case USB_ENDPOINT_HALT: 167247a1685fSDinh Nguyen halted = ep->halted; 167347a1685fSDinh Nguyen 167451da43b5SVahram Aharonyan dwc2_hsotg_ep_sethalt(&ep->ep, set, true); 167547a1685fSDinh Nguyen 16761f91b4ccSFelipe Balbi ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); 167747a1685fSDinh Nguyen if (ret) { 167847a1685fSDinh Nguyen dev_err(hsotg->dev, 167947a1685fSDinh Nguyen "%s: failed to send reply\n", __func__); 168047a1685fSDinh Nguyen return ret; 168147a1685fSDinh Nguyen } 168247a1685fSDinh Nguyen 168347a1685fSDinh Nguyen /* 168447a1685fSDinh Nguyen * we have to complete all requests for ep if it was 168547a1685fSDinh Nguyen * halted, and the halt was cleared by CLEAR_FEATURE 168647a1685fSDinh Nguyen */ 168747a1685fSDinh Nguyen 168847a1685fSDinh Nguyen if (!set && halted) { 168947a1685fSDinh Nguyen /* 169047a1685fSDinh Nguyen * If we have request in progress, 169147a1685fSDinh Nguyen * then complete it 169247a1685fSDinh Nguyen */ 169347a1685fSDinh Nguyen if (ep->req) { 169447a1685fSDinh Nguyen hs_req = ep->req; 169547a1685fSDinh Nguyen ep->req = NULL; 169647a1685fSDinh Nguyen list_del_init(&hs_req->queue); 1697c00dd4a6SGregory Herrero if (hs_req->req.complete) { 1698c00dd4a6SGregory Herrero spin_unlock(&hsotg->lock); 1699c00dd4a6SGregory Herrero usb_gadget_giveback_request( 1700c00dd4a6SGregory Herrero &ep->ep, &hs_req->req); 1701c00dd4a6SGregory Herrero spin_lock(&hsotg->lock); 1702c00dd4a6SGregory Herrero } 170347a1685fSDinh Nguyen } 170447a1685fSDinh Nguyen 170547a1685fSDinh Nguyen /* If we have pending request, then start it */ 170634c0887fSJohn Youn if (!ep->req) 170741cc4cd2SVardan Mikayelyan dwc2_gadget_start_next_request(ep); 170847a1685fSDinh Nguyen } 170947a1685fSDinh Nguyen 171047a1685fSDinh Nguyen break; 171147a1685fSDinh Nguyen 171247a1685fSDinh Nguyen default: 171347a1685fSDinh Nguyen return -ENOENT; 171447a1685fSDinh Nguyen } 17159e14d0a5SGregory Herrero break; 17169e14d0a5SGregory Herrero default: 17179e14d0a5SGregory Herrero return -ENOENT; 17189e14d0a5SGregory Herrero } 171947a1685fSDinh Nguyen return 1; 172047a1685fSDinh Nguyen } 172147a1685fSDinh Nguyen 17221f91b4ccSFelipe Balbi static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg); 172347a1685fSDinh Nguyen 172447a1685fSDinh Nguyen /** 17251f91b4ccSFelipe Balbi * dwc2_hsotg_stall_ep0 - stall ep0 172647a1685fSDinh Nguyen * @hsotg: The device state 172747a1685fSDinh Nguyen * 172847a1685fSDinh Nguyen * Set stall for ep0 as response for setup request. 172947a1685fSDinh Nguyen */ 17301f91b4ccSFelipe Balbi static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg) 1731e9ebe7c3SJingoo Han { 17321f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; 173347a1685fSDinh Nguyen u32 reg; 173447a1685fSDinh Nguyen u32 ctrl; 173547a1685fSDinh Nguyen 173647a1685fSDinh Nguyen dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in); 173747a1685fSDinh Nguyen reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0; 173847a1685fSDinh Nguyen 173947a1685fSDinh Nguyen /* 174047a1685fSDinh Nguyen * DxEPCTL_Stall will be cleared by EP once it has 174147a1685fSDinh Nguyen * taken effect, so no need to clear later. 174247a1685fSDinh Nguyen */ 174347a1685fSDinh Nguyen 174495c8bc36SAntti Seppälä ctrl = dwc2_readl(hsotg->regs + reg); 174547a1685fSDinh Nguyen ctrl |= DXEPCTL_STALL; 174647a1685fSDinh Nguyen ctrl |= DXEPCTL_CNAK; 174795c8bc36SAntti Seppälä dwc2_writel(ctrl, hsotg->regs + reg); 174847a1685fSDinh Nguyen 174947a1685fSDinh Nguyen dev_dbg(hsotg->dev, 175047a1685fSDinh Nguyen "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n", 175195c8bc36SAntti Seppälä ctrl, reg, dwc2_readl(hsotg->regs + reg)); 175247a1685fSDinh Nguyen 175347a1685fSDinh Nguyen /* 175447a1685fSDinh Nguyen * complete won't be called, so we enqueue 175547a1685fSDinh Nguyen * setup request here 175647a1685fSDinh Nguyen */ 17571f91b4ccSFelipe Balbi dwc2_hsotg_enqueue_setup(hsotg); 175847a1685fSDinh Nguyen } 175947a1685fSDinh Nguyen 176047a1685fSDinh Nguyen /** 17611f91b4ccSFelipe Balbi * dwc2_hsotg_process_control - process a control request 176247a1685fSDinh Nguyen * @hsotg: The device state 176347a1685fSDinh Nguyen * @ctrl: The control request received 176447a1685fSDinh Nguyen * 176547a1685fSDinh Nguyen * The controller has received the SETUP phase of a control request, and 176647a1685fSDinh Nguyen * needs to work out what to do next (and whether to pass it on to the 176747a1685fSDinh Nguyen * gadget driver). 176847a1685fSDinh Nguyen */ 17691f91b4ccSFelipe Balbi static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg, 177047a1685fSDinh Nguyen struct usb_ctrlrequest *ctrl) 177147a1685fSDinh Nguyen { 17721f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; 177347a1685fSDinh Nguyen int ret = 0; 177447a1685fSDinh Nguyen u32 dcfg; 177547a1685fSDinh Nguyen 1776e525e743SMian Yousaf Kaukab dev_dbg(hsotg->dev, 1777e525e743SMian Yousaf Kaukab "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n", 1778e525e743SMian Yousaf Kaukab ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, 1779e525e743SMian Yousaf Kaukab ctrl->wIndex, ctrl->wLength); 178047a1685fSDinh Nguyen 1781fe0b94abSMian Yousaf Kaukab if (ctrl->wLength == 0) { 178247a1685fSDinh Nguyen ep0->dir_in = 1; 1783fe0b94abSMian Yousaf Kaukab hsotg->ep0_state = DWC2_EP0_STATUS_IN; 1784fe0b94abSMian Yousaf Kaukab } else if (ctrl->bRequestType & USB_DIR_IN) { 1785fe0b94abSMian Yousaf Kaukab ep0->dir_in = 1; 1786fe0b94abSMian Yousaf Kaukab hsotg->ep0_state = DWC2_EP0_DATA_IN; 1787fe0b94abSMian Yousaf Kaukab } else { 1788fe0b94abSMian Yousaf Kaukab ep0->dir_in = 0; 1789fe0b94abSMian Yousaf Kaukab hsotg->ep0_state = DWC2_EP0_DATA_OUT; 1790fe0b94abSMian Yousaf Kaukab } 179147a1685fSDinh Nguyen 179247a1685fSDinh Nguyen if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { 179347a1685fSDinh Nguyen switch (ctrl->bRequest) { 179447a1685fSDinh Nguyen case USB_REQ_SET_ADDRESS: 17956d713c15SMian Yousaf Kaukab hsotg->connected = 1; 179695c8bc36SAntti Seppälä dcfg = dwc2_readl(hsotg->regs + DCFG); 179747a1685fSDinh Nguyen dcfg &= ~DCFG_DEVADDR_MASK; 1798d5dbd3f7SPaul Zimmerman dcfg |= (le16_to_cpu(ctrl->wValue) << 1799d5dbd3f7SPaul Zimmerman DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK; 180095c8bc36SAntti Seppälä dwc2_writel(dcfg, hsotg->regs + DCFG); 180147a1685fSDinh Nguyen 180247a1685fSDinh Nguyen dev_info(hsotg->dev, "new address %d\n", ctrl->wValue); 180347a1685fSDinh Nguyen 18041f91b4ccSFelipe Balbi ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); 180547a1685fSDinh Nguyen return; 180647a1685fSDinh Nguyen 180747a1685fSDinh Nguyen case USB_REQ_GET_STATUS: 18081f91b4ccSFelipe Balbi ret = dwc2_hsotg_process_req_status(hsotg, ctrl); 180947a1685fSDinh Nguyen break; 181047a1685fSDinh Nguyen 181147a1685fSDinh Nguyen case USB_REQ_CLEAR_FEATURE: 181247a1685fSDinh Nguyen case USB_REQ_SET_FEATURE: 18131f91b4ccSFelipe Balbi ret = dwc2_hsotg_process_req_feature(hsotg, ctrl); 181447a1685fSDinh Nguyen break; 181547a1685fSDinh Nguyen } 181647a1685fSDinh Nguyen } 181747a1685fSDinh Nguyen 181847a1685fSDinh Nguyen /* as a fallback, try delivering it to the driver to deal with */ 181947a1685fSDinh Nguyen 182047a1685fSDinh Nguyen if (ret == 0 && hsotg->driver) { 182147a1685fSDinh Nguyen spin_unlock(&hsotg->lock); 182247a1685fSDinh Nguyen ret = hsotg->driver->setup(&hsotg->gadget, ctrl); 182347a1685fSDinh Nguyen spin_lock(&hsotg->lock); 182447a1685fSDinh Nguyen if (ret < 0) 182547a1685fSDinh Nguyen dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret); 182647a1685fSDinh Nguyen } 182747a1685fSDinh Nguyen 182847a1685fSDinh Nguyen /* 182947a1685fSDinh Nguyen * the request is either unhandlable, or is not formatted correctly 183047a1685fSDinh Nguyen * so respond with a STALL for the status stage to indicate failure. 183147a1685fSDinh Nguyen */ 183247a1685fSDinh Nguyen 183347a1685fSDinh Nguyen if (ret < 0) 18341f91b4ccSFelipe Balbi dwc2_hsotg_stall_ep0(hsotg); 183547a1685fSDinh Nguyen } 183647a1685fSDinh Nguyen 183747a1685fSDinh Nguyen /** 18381f91b4ccSFelipe Balbi * dwc2_hsotg_complete_setup - completion of a setup transfer 183947a1685fSDinh Nguyen * @ep: The endpoint the request was on. 184047a1685fSDinh Nguyen * @req: The request completed. 184147a1685fSDinh Nguyen * 184247a1685fSDinh Nguyen * Called on completion of any requests the driver itself submitted for 184347a1685fSDinh Nguyen * EP0 setup packets 184447a1685fSDinh Nguyen */ 18451f91b4ccSFelipe Balbi static void dwc2_hsotg_complete_setup(struct usb_ep *ep, 184647a1685fSDinh Nguyen struct usb_request *req) 184747a1685fSDinh Nguyen { 18481f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = our_ep(ep); 1849941fcce4SDinh Nguyen struct dwc2_hsotg *hsotg = hs_ep->parent; 185047a1685fSDinh Nguyen 185147a1685fSDinh Nguyen if (req->status < 0) { 185247a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status); 185347a1685fSDinh Nguyen return; 185447a1685fSDinh Nguyen } 185547a1685fSDinh Nguyen 185647a1685fSDinh Nguyen spin_lock(&hsotg->lock); 185747a1685fSDinh Nguyen if (req->actual == 0) 18581f91b4ccSFelipe Balbi dwc2_hsotg_enqueue_setup(hsotg); 185947a1685fSDinh Nguyen else 18601f91b4ccSFelipe Balbi dwc2_hsotg_process_control(hsotg, req->buf); 186147a1685fSDinh Nguyen spin_unlock(&hsotg->lock); 186247a1685fSDinh Nguyen } 186347a1685fSDinh Nguyen 186447a1685fSDinh Nguyen /** 18651f91b4ccSFelipe Balbi * dwc2_hsotg_enqueue_setup - start a request for EP0 packets 186647a1685fSDinh Nguyen * @hsotg: The device state. 186747a1685fSDinh Nguyen * 186847a1685fSDinh Nguyen * Enqueue a request on EP0 if necessary to received any SETUP packets 186947a1685fSDinh Nguyen * received from the host. 187047a1685fSDinh Nguyen */ 18711f91b4ccSFelipe Balbi static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg) 187247a1685fSDinh Nguyen { 187347a1685fSDinh Nguyen struct usb_request *req = hsotg->ctrl_req; 18741f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req = our_req(req); 187547a1685fSDinh Nguyen int ret; 187647a1685fSDinh Nguyen 187747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__); 187847a1685fSDinh Nguyen 187947a1685fSDinh Nguyen req->zero = 0; 188047a1685fSDinh Nguyen req->length = 8; 188147a1685fSDinh Nguyen req->buf = hsotg->ctrl_buff; 18821f91b4ccSFelipe Balbi req->complete = dwc2_hsotg_complete_setup; 188347a1685fSDinh Nguyen 188447a1685fSDinh Nguyen if (!list_empty(&hs_req->queue)) { 188547a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s already queued???\n", __func__); 188647a1685fSDinh Nguyen return; 188747a1685fSDinh Nguyen } 188847a1685fSDinh Nguyen 1889c6f5c050SMian Yousaf Kaukab hsotg->eps_out[0]->dir_in = 0; 18908a20fa45SMian Yousaf Kaukab hsotg->eps_out[0]->send_zlp = 0; 1891fe0b94abSMian Yousaf Kaukab hsotg->ep0_state = DWC2_EP0_SETUP; 189247a1685fSDinh Nguyen 18931f91b4ccSFelipe Balbi ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC); 189447a1685fSDinh Nguyen if (ret < 0) { 189547a1685fSDinh Nguyen dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret); 189647a1685fSDinh Nguyen /* 189747a1685fSDinh Nguyen * Don't think there's much we can do other than watch the 189847a1685fSDinh Nguyen * driver fail. 189947a1685fSDinh Nguyen */ 190047a1685fSDinh Nguyen } 190147a1685fSDinh Nguyen } 190247a1685fSDinh Nguyen 19031f91b4ccSFelipe Balbi static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg, 19041f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep) 1905fe0b94abSMian Yousaf Kaukab { 1906fe0b94abSMian Yousaf Kaukab u32 ctrl; 1907fe0b94abSMian Yousaf Kaukab u8 index = hs_ep->index; 1908fe0b94abSMian Yousaf Kaukab u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); 1909fe0b94abSMian Yousaf Kaukab u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); 1910fe0b94abSMian Yousaf Kaukab 1911ccb34a91SMian Yousaf Kaukab if (hs_ep->dir_in) 1912ccb34a91SMian Yousaf Kaukab dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", 1913ccb34a91SMian Yousaf Kaukab index); 1914ccb34a91SMian Yousaf Kaukab else 1915ccb34a91SMian Yousaf Kaukab dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n", 1916ccb34a91SMian Yousaf Kaukab index); 1917e02f9aa6SVahram Aharonyan if (using_desc_dma(hsotg)) { 1918e02f9aa6SVahram Aharonyan /* Not specific buffer needed for ep0 ZLP */ 1919e02f9aa6SVahram Aharonyan dma_addr_t dma = hs_ep->desc_list_dma; 1920fe0b94abSMian Yousaf Kaukab 1921201ec568SMinas Harutyunyan if (!index) 1922e02f9aa6SVahram Aharonyan dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep); 1923201ec568SMinas Harutyunyan 1924e02f9aa6SVahram Aharonyan dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0); 1925e02f9aa6SVahram Aharonyan } else { 192695c8bc36SAntti Seppälä dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | 1927fe0b94abSMian Yousaf Kaukab DXEPTSIZ_XFERSIZE(0), hsotg->regs + 1928fe0b94abSMian Yousaf Kaukab epsiz_reg); 1929e02f9aa6SVahram Aharonyan } 1930fe0b94abSMian Yousaf Kaukab 193195c8bc36SAntti Seppälä ctrl = dwc2_readl(hsotg->regs + epctl_reg); 1932fe0b94abSMian Yousaf Kaukab ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ 1933fe0b94abSMian Yousaf Kaukab ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ 1934fe0b94abSMian Yousaf Kaukab ctrl |= DXEPCTL_USBACTEP; 193595c8bc36SAntti Seppälä dwc2_writel(ctrl, hsotg->regs + epctl_reg); 1936fe0b94abSMian Yousaf Kaukab } 1937fe0b94abSMian Yousaf Kaukab 193847a1685fSDinh Nguyen /** 19391f91b4ccSFelipe Balbi * dwc2_hsotg_complete_request - complete a request given to us 194047a1685fSDinh Nguyen * @hsotg: The device state. 194147a1685fSDinh Nguyen * @hs_ep: The endpoint the request was on. 194247a1685fSDinh Nguyen * @hs_req: The request to complete. 194347a1685fSDinh Nguyen * @result: The result code (0 => Ok, otherwise errno) 194447a1685fSDinh Nguyen * 194547a1685fSDinh Nguyen * The given request has finished, so call the necessary completion 194647a1685fSDinh Nguyen * if it has one and then look to see if we can start a new request 194747a1685fSDinh Nguyen * on the endpoint. 194847a1685fSDinh Nguyen * 194947a1685fSDinh Nguyen * Note, expects the ep to already be locked as appropriate. 195047a1685fSDinh Nguyen */ 19511f91b4ccSFelipe Balbi static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg, 19521f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep, 19531f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req, 195447a1685fSDinh Nguyen int result) 195547a1685fSDinh Nguyen { 195647a1685fSDinh Nguyen if (!hs_req) { 195747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__); 195847a1685fSDinh Nguyen return; 195947a1685fSDinh Nguyen } 196047a1685fSDinh Nguyen 196147a1685fSDinh Nguyen dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n", 196247a1685fSDinh Nguyen hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete); 196347a1685fSDinh Nguyen 196447a1685fSDinh Nguyen /* 196547a1685fSDinh Nguyen * only replace the status if we've not already set an error 196647a1685fSDinh Nguyen * from a previous transaction 196747a1685fSDinh Nguyen */ 196847a1685fSDinh Nguyen 196947a1685fSDinh Nguyen if (hs_req->req.status == -EINPROGRESS) 197047a1685fSDinh Nguyen hs_req->req.status = result; 197147a1685fSDinh Nguyen 197244583fecSYunzhi Li if (using_dma(hsotg)) 197344583fecSYunzhi Li dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req); 197444583fecSYunzhi Li 19751f91b4ccSFelipe Balbi dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req); 19767d24c1b5SMian Yousaf Kaukab 197747a1685fSDinh Nguyen hs_ep->req = NULL; 197847a1685fSDinh Nguyen list_del_init(&hs_req->queue); 197947a1685fSDinh Nguyen 198047a1685fSDinh Nguyen /* 198147a1685fSDinh Nguyen * call the complete request with the locks off, just in case the 198247a1685fSDinh Nguyen * request tries to queue more work for this endpoint. 198347a1685fSDinh Nguyen */ 198447a1685fSDinh Nguyen 198547a1685fSDinh Nguyen if (hs_req->req.complete) { 198647a1685fSDinh Nguyen spin_unlock(&hsotg->lock); 1987304f7e5eSMichal Sojka usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req); 198847a1685fSDinh Nguyen spin_lock(&hsotg->lock); 198947a1685fSDinh Nguyen } 199047a1685fSDinh Nguyen 1991540ccba0SVahram Aharonyan /* In DDMA don't need to proceed to starting of next ISOC request */ 1992540ccba0SVahram Aharonyan if (using_desc_dma(hsotg) && hs_ep->isochronous) 1993540ccba0SVahram Aharonyan return; 1994540ccba0SVahram Aharonyan 199547a1685fSDinh Nguyen /* 199647a1685fSDinh Nguyen * Look to see if there is anything else to do. Note, the completion 199747a1685fSDinh Nguyen * of the previous request may have caused a new request to be started 199847a1685fSDinh Nguyen * so be careful when doing this. 199947a1685fSDinh Nguyen */ 200047a1685fSDinh Nguyen 200134c0887fSJohn Youn if (!hs_ep->req && result >= 0) 200241cc4cd2SVardan Mikayelyan dwc2_gadget_start_next_request(hs_ep); 200347a1685fSDinh Nguyen } 200447a1685fSDinh Nguyen 2005540ccba0SVahram Aharonyan /* 2006540ccba0SVahram Aharonyan * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA 2007540ccba0SVahram Aharonyan * @hs_ep: The endpoint the request was on. 2008540ccba0SVahram Aharonyan * 2009540ccba0SVahram Aharonyan * Get first request from the ep queue, determine descriptor on which complete 2010540ccba0SVahram Aharonyan * happened. SW based on isoc_chain_num discovers which half of the descriptor 2011540ccba0SVahram Aharonyan * chain is currently in use by HW, adjusts dma_address and calculates index 2012540ccba0SVahram Aharonyan * of completed descriptor based on the value of DEPDMA register. Update actual 2013540ccba0SVahram Aharonyan * length of request, giveback to gadget. 2014540ccba0SVahram Aharonyan */ 2015540ccba0SVahram Aharonyan static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep) 2016540ccba0SVahram Aharonyan { 2017540ccba0SVahram Aharonyan struct dwc2_hsotg *hsotg = hs_ep->parent; 2018540ccba0SVahram Aharonyan struct dwc2_hsotg_req *hs_req; 2019540ccba0SVahram Aharonyan struct usb_request *ureq; 2020540ccba0SVahram Aharonyan int index; 2021540ccba0SVahram Aharonyan dma_addr_t dma_addr; 2022540ccba0SVahram Aharonyan u32 dma_reg; 2023540ccba0SVahram Aharonyan u32 depdma; 2024540ccba0SVahram Aharonyan u32 desc_sts; 2025540ccba0SVahram Aharonyan u32 mask; 2026540ccba0SVahram Aharonyan 2027540ccba0SVahram Aharonyan hs_req = get_ep_head(hs_ep); 2028540ccba0SVahram Aharonyan if (!hs_req) { 2029540ccba0SVahram Aharonyan dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__); 2030540ccba0SVahram Aharonyan return; 2031540ccba0SVahram Aharonyan } 2032540ccba0SVahram Aharonyan ureq = &hs_req->req; 2033540ccba0SVahram Aharonyan 2034540ccba0SVahram Aharonyan dma_addr = hs_ep->desc_list_dma; 2035540ccba0SVahram Aharonyan 2036540ccba0SVahram Aharonyan /* 2037540ccba0SVahram Aharonyan * If lower half of descriptor chain is currently use by SW, 2038540ccba0SVahram Aharonyan * that means higher half is being processed by HW, so shift 2039540ccba0SVahram Aharonyan * DMA address to higher half of descriptor chain. 2040540ccba0SVahram Aharonyan */ 2041540ccba0SVahram Aharonyan if (!hs_ep->isoc_chain_num) 2042540ccba0SVahram Aharonyan dma_addr += sizeof(struct dwc2_dma_desc) * 2043540ccba0SVahram Aharonyan (MAX_DMA_DESC_NUM_GENERIC / 2); 2044540ccba0SVahram Aharonyan 2045540ccba0SVahram Aharonyan dma_reg = hs_ep->dir_in ? DIEPDMA(hs_ep->index) : DOEPDMA(hs_ep->index); 2046540ccba0SVahram Aharonyan depdma = dwc2_readl(hsotg->regs + dma_reg); 2047540ccba0SVahram Aharonyan 2048540ccba0SVahram Aharonyan index = (depdma - dma_addr) / sizeof(struct dwc2_dma_desc) - 1; 2049540ccba0SVahram Aharonyan desc_sts = hs_ep->desc_list[index].status; 2050540ccba0SVahram Aharonyan 2051540ccba0SVahram Aharonyan mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK : 2052540ccba0SVahram Aharonyan DEV_DMA_ISOC_RX_NBYTES_MASK; 2053540ccba0SVahram Aharonyan ureq->actual = ureq->length - 2054540ccba0SVahram Aharonyan ((desc_sts & mask) >> DEV_DMA_ISOC_NBYTES_SHIFT); 2055540ccba0SVahram Aharonyan 205695d2b037SVahram Aharonyan /* Adjust actual length for ISOC Out if length is not align of 4 */ 205795d2b037SVahram Aharonyan if (!hs_ep->dir_in && ureq->length & 0x3) 205895d2b037SVahram Aharonyan ureq->actual += 4 - (ureq->length & 0x3); 205995d2b037SVahram Aharonyan 2060540ccba0SVahram Aharonyan dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); 2061540ccba0SVahram Aharonyan } 2062540ccba0SVahram Aharonyan 2063540ccba0SVahram Aharonyan /* 2064540ccba0SVahram Aharonyan * dwc2_gadget_start_next_isoc_ddma - start next isoc request, if any. 2065540ccba0SVahram Aharonyan * @hs_ep: The isochronous endpoint to be re-enabled. 2066540ccba0SVahram Aharonyan * 2067540ccba0SVahram Aharonyan * If ep has been disabled due to last descriptor servicing (IN endpoint) or 2068540ccba0SVahram Aharonyan * BNA (OUT endpoint) check the status of other half of descriptor chain that 2069540ccba0SVahram Aharonyan * was under SW control till HW was busy and restart the endpoint if needed. 2070540ccba0SVahram Aharonyan */ 2071540ccba0SVahram Aharonyan static void dwc2_gadget_start_next_isoc_ddma(struct dwc2_hsotg_ep *hs_ep) 2072540ccba0SVahram Aharonyan { 2073540ccba0SVahram Aharonyan struct dwc2_hsotg *hsotg = hs_ep->parent; 2074540ccba0SVahram Aharonyan u32 depctl; 2075540ccba0SVahram Aharonyan u32 dma_reg; 2076540ccba0SVahram Aharonyan u32 ctrl; 2077540ccba0SVahram Aharonyan u32 dma_addr = hs_ep->desc_list_dma; 2078540ccba0SVahram Aharonyan unsigned char index = hs_ep->index; 2079540ccba0SVahram Aharonyan 2080540ccba0SVahram Aharonyan dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index); 2081540ccba0SVahram Aharonyan depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); 2082540ccba0SVahram Aharonyan 2083540ccba0SVahram Aharonyan ctrl = dwc2_readl(hsotg->regs + depctl); 2084540ccba0SVahram Aharonyan 2085540ccba0SVahram Aharonyan /* 2086540ccba0SVahram Aharonyan * EP was disabled if HW has processed last descriptor or BNA was set. 2087540ccba0SVahram Aharonyan * So restart ep if SW has prepared new descriptor chain in ep_queue 2088540ccba0SVahram Aharonyan * routine while HW was busy. 2089540ccba0SVahram Aharonyan */ 2090540ccba0SVahram Aharonyan if (!(ctrl & DXEPCTL_EPENA)) { 2091540ccba0SVahram Aharonyan if (!hs_ep->next_desc) { 2092540ccba0SVahram Aharonyan dev_dbg(hsotg->dev, "%s: No more ISOC requests\n", 2093540ccba0SVahram Aharonyan __func__); 2094540ccba0SVahram Aharonyan return; 2095540ccba0SVahram Aharonyan } 2096540ccba0SVahram Aharonyan 2097540ccba0SVahram Aharonyan dma_addr += sizeof(struct dwc2_dma_desc) * 2098540ccba0SVahram Aharonyan (MAX_DMA_DESC_NUM_GENERIC / 2) * 2099540ccba0SVahram Aharonyan hs_ep->isoc_chain_num; 2100540ccba0SVahram Aharonyan dwc2_writel(dma_addr, hsotg->regs + dma_reg); 2101540ccba0SVahram Aharonyan 2102540ccba0SVahram Aharonyan ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK; 2103540ccba0SVahram Aharonyan dwc2_writel(ctrl, hsotg->regs + depctl); 2104540ccba0SVahram Aharonyan 2105540ccba0SVahram Aharonyan /* Switch ISOC descriptor chain number being processed by SW*/ 2106540ccba0SVahram Aharonyan hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1; 2107540ccba0SVahram Aharonyan hs_ep->next_desc = 0; 2108540ccba0SVahram Aharonyan 2109540ccba0SVahram Aharonyan dev_dbg(hsotg->dev, "%s: Restarted isochronous endpoint\n", 2110540ccba0SVahram Aharonyan __func__); 2111540ccba0SVahram Aharonyan } 2112540ccba0SVahram Aharonyan } 2113540ccba0SVahram Aharonyan 211447a1685fSDinh Nguyen /** 21151f91b4ccSFelipe Balbi * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint 211647a1685fSDinh Nguyen * @hsotg: The device state. 211747a1685fSDinh Nguyen * @ep_idx: The endpoint index for the data 211847a1685fSDinh Nguyen * @size: The size of data in the fifo, in bytes 211947a1685fSDinh Nguyen * 212047a1685fSDinh Nguyen * The FIFO status shows there is data to read from the FIFO for a given 212147a1685fSDinh Nguyen * endpoint, so sort out whether we need to read the data into a request 212247a1685fSDinh Nguyen * that has been made for that endpoint. 212347a1685fSDinh Nguyen */ 21241f91b4ccSFelipe Balbi static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size) 212547a1685fSDinh Nguyen { 21261f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx]; 21271f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req = hs_ep->req; 212847a1685fSDinh Nguyen void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx); 212947a1685fSDinh Nguyen int to_read; 213047a1685fSDinh Nguyen int max_req; 213147a1685fSDinh Nguyen int read_ptr; 213247a1685fSDinh Nguyen 213347a1685fSDinh Nguyen if (!hs_req) { 213495c8bc36SAntti Seppälä u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx)); 213547a1685fSDinh Nguyen int ptr; 213647a1685fSDinh Nguyen 21376b448af4SRobert Baldyga dev_dbg(hsotg->dev, 213847a1685fSDinh Nguyen "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n", 213947a1685fSDinh Nguyen __func__, size, ep_idx, epctl); 214047a1685fSDinh Nguyen 214147a1685fSDinh Nguyen /* dump the data from the FIFO, we've nothing we can do */ 214247a1685fSDinh Nguyen for (ptr = 0; ptr < size; ptr += 4) 214395c8bc36SAntti Seppälä (void)dwc2_readl(fifo); 214447a1685fSDinh Nguyen 214547a1685fSDinh Nguyen return; 214647a1685fSDinh Nguyen } 214747a1685fSDinh Nguyen 214847a1685fSDinh Nguyen to_read = size; 214947a1685fSDinh Nguyen read_ptr = hs_req->req.actual; 215047a1685fSDinh Nguyen max_req = hs_req->req.length - read_ptr; 215147a1685fSDinh Nguyen 215247a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n", 215347a1685fSDinh Nguyen __func__, to_read, max_req, read_ptr, hs_req->req.length); 215447a1685fSDinh Nguyen 215547a1685fSDinh Nguyen if (to_read > max_req) { 215647a1685fSDinh Nguyen /* 215747a1685fSDinh Nguyen * more data appeared than we where willing 215847a1685fSDinh Nguyen * to deal with in this request. 215947a1685fSDinh Nguyen */ 216047a1685fSDinh Nguyen 216147a1685fSDinh Nguyen /* currently we don't deal this */ 216247a1685fSDinh Nguyen WARN_ON_ONCE(1); 216347a1685fSDinh Nguyen } 216447a1685fSDinh Nguyen 216547a1685fSDinh Nguyen hs_ep->total_data += to_read; 216647a1685fSDinh Nguyen hs_req->req.actual += to_read; 216747a1685fSDinh Nguyen to_read = DIV_ROUND_UP(to_read, 4); 216847a1685fSDinh Nguyen 216947a1685fSDinh Nguyen /* 217047a1685fSDinh Nguyen * note, we might over-write the buffer end by 3 bytes depending on 217147a1685fSDinh Nguyen * alignment of the data. 217247a1685fSDinh Nguyen */ 217347a1685fSDinh Nguyen ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read); 217447a1685fSDinh Nguyen } 217547a1685fSDinh Nguyen 217647a1685fSDinh Nguyen /** 21771f91b4ccSFelipe Balbi * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint 217847a1685fSDinh Nguyen * @hsotg: The device instance 2179fe0b94abSMian Yousaf Kaukab * @dir_in: If IN zlp 218047a1685fSDinh Nguyen * 218147a1685fSDinh Nguyen * Generate a zero-length IN packet request for terminating a SETUP 218247a1685fSDinh Nguyen * transaction. 218347a1685fSDinh Nguyen * 218447a1685fSDinh Nguyen * Note, since we don't write any data to the TxFIFO, then it is 218547a1685fSDinh Nguyen * currently believed that we do not need to wait for any space in 218647a1685fSDinh Nguyen * the TxFIFO. 218747a1685fSDinh Nguyen */ 21881f91b4ccSFelipe Balbi static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in) 218947a1685fSDinh Nguyen { 2190c6f5c050SMian Yousaf Kaukab /* eps_out[0] is used in both directions */ 2191fe0b94abSMian Yousaf Kaukab hsotg->eps_out[0]->dir_in = dir_in; 2192fe0b94abSMian Yousaf Kaukab hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT; 219347a1685fSDinh Nguyen 21941f91b4ccSFelipe Balbi dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]); 219547a1685fSDinh Nguyen } 219647a1685fSDinh Nguyen 2197ec1f9d9fSRoman Bacik static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg, 2198ec1f9d9fSRoman Bacik u32 epctl_reg) 2199ec1f9d9fSRoman Bacik { 2200ec1f9d9fSRoman Bacik u32 ctrl; 2201ec1f9d9fSRoman Bacik 2202ec1f9d9fSRoman Bacik ctrl = dwc2_readl(hsotg->regs + epctl_reg); 2203ec1f9d9fSRoman Bacik if (ctrl & DXEPCTL_EOFRNUM) 2204ec1f9d9fSRoman Bacik ctrl |= DXEPCTL_SETEVENFR; 2205ec1f9d9fSRoman Bacik else 2206ec1f9d9fSRoman Bacik ctrl |= DXEPCTL_SETODDFR; 2207ec1f9d9fSRoman Bacik dwc2_writel(ctrl, hsotg->regs + epctl_reg); 2208ec1f9d9fSRoman Bacik } 2209ec1f9d9fSRoman Bacik 2210aa3e8bc8SVahram Aharonyan /* 2211aa3e8bc8SVahram Aharonyan * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc 2212aa3e8bc8SVahram Aharonyan * @hs_ep - The endpoint on which transfer went 2213aa3e8bc8SVahram Aharonyan * 2214aa3e8bc8SVahram Aharonyan * Iterate over endpoints descriptor chain and get info on bytes remained 2215aa3e8bc8SVahram Aharonyan * in DMA descriptors after transfer has completed. Used for non isoc EPs. 2216aa3e8bc8SVahram Aharonyan */ 2217aa3e8bc8SVahram Aharonyan static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep) 2218aa3e8bc8SVahram Aharonyan { 2219aa3e8bc8SVahram Aharonyan struct dwc2_hsotg *hsotg = hs_ep->parent; 2220aa3e8bc8SVahram Aharonyan unsigned int bytes_rem = 0; 2221aa3e8bc8SVahram Aharonyan struct dwc2_dma_desc *desc = hs_ep->desc_list; 2222aa3e8bc8SVahram Aharonyan int i; 2223aa3e8bc8SVahram Aharonyan u32 status; 2224aa3e8bc8SVahram Aharonyan 2225aa3e8bc8SVahram Aharonyan if (!desc) 2226aa3e8bc8SVahram Aharonyan return -EINVAL; 2227aa3e8bc8SVahram Aharonyan 2228aa3e8bc8SVahram Aharonyan for (i = 0; i < hs_ep->desc_count; ++i) { 2229aa3e8bc8SVahram Aharonyan status = desc->status; 2230aa3e8bc8SVahram Aharonyan bytes_rem += status & DEV_DMA_NBYTES_MASK; 2231aa3e8bc8SVahram Aharonyan 2232aa3e8bc8SVahram Aharonyan if (status & DEV_DMA_STS_MASK) 2233aa3e8bc8SVahram Aharonyan dev_err(hsotg->dev, "descriptor %d closed with %x\n", 2234aa3e8bc8SVahram Aharonyan i, status & DEV_DMA_STS_MASK); 2235aa3e8bc8SVahram Aharonyan } 2236aa3e8bc8SVahram Aharonyan 2237aa3e8bc8SVahram Aharonyan return bytes_rem; 2238aa3e8bc8SVahram Aharonyan } 2239aa3e8bc8SVahram Aharonyan 224047a1685fSDinh Nguyen /** 22411f91b4ccSFelipe Balbi * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO 224247a1685fSDinh Nguyen * @hsotg: The device instance 224347a1685fSDinh Nguyen * @epnum: The endpoint received from 224447a1685fSDinh Nguyen * 224547a1685fSDinh Nguyen * The RXFIFO has delivered an OutDone event, which means that the data 224647a1685fSDinh Nguyen * transfer for an OUT endpoint has been completed, either by a short 224747a1685fSDinh Nguyen * packet or by the finish of a transfer. 224847a1685fSDinh Nguyen */ 22491f91b4ccSFelipe Balbi static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum) 225047a1685fSDinh Nguyen { 225195c8bc36SAntti Seppälä u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum)); 22521f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum]; 22531f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req = hs_ep->req; 225447a1685fSDinh Nguyen struct usb_request *req = &hs_req->req; 22559da51974SJohn Youn unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize); 225647a1685fSDinh Nguyen int result = 0; 225747a1685fSDinh Nguyen 225847a1685fSDinh Nguyen if (!hs_req) { 225947a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: no request active\n", __func__); 226047a1685fSDinh Nguyen return; 226147a1685fSDinh Nguyen } 226247a1685fSDinh Nguyen 2263fe0b94abSMian Yousaf Kaukab if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) { 2264fe0b94abSMian Yousaf Kaukab dev_dbg(hsotg->dev, "zlp packet received\n"); 22651f91b4ccSFelipe Balbi dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); 22661f91b4ccSFelipe Balbi dwc2_hsotg_enqueue_setup(hsotg); 2267fe0b94abSMian Yousaf Kaukab return; 2268fe0b94abSMian Yousaf Kaukab } 2269fe0b94abSMian Yousaf Kaukab 2270aa3e8bc8SVahram Aharonyan if (using_desc_dma(hsotg)) 2271aa3e8bc8SVahram Aharonyan size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); 2272aa3e8bc8SVahram Aharonyan 227347a1685fSDinh Nguyen if (using_dma(hsotg)) { 22749da51974SJohn Youn unsigned int size_done; 227547a1685fSDinh Nguyen 227647a1685fSDinh Nguyen /* 227747a1685fSDinh Nguyen * Calculate the size of the transfer by checking how much 227847a1685fSDinh Nguyen * is left in the endpoint size register and then working it 227947a1685fSDinh Nguyen * out from the amount we loaded for the transfer. 228047a1685fSDinh Nguyen * 228147a1685fSDinh Nguyen * We need to do this as DMA pointers are always 32bit aligned 228247a1685fSDinh Nguyen * so may overshoot/undershoot the transfer. 228347a1685fSDinh Nguyen */ 228447a1685fSDinh Nguyen 228547a1685fSDinh Nguyen size_done = hs_ep->size_loaded - size_left; 228647a1685fSDinh Nguyen size_done += hs_ep->last_load; 228747a1685fSDinh Nguyen 228847a1685fSDinh Nguyen req->actual = size_done; 228947a1685fSDinh Nguyen } 229047a1685fSDinh Nguyen 229147a1685fSDinh Nguyen /* if there is more request to do, schedule new transfer */ 229247a1685fSDinh Nguyen if (req->actual < req->length && size_left == 0) { 22931f91b4ccSFelipe Balbi dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); 229447a1685fSDinh Nguyen return; 229547a1685fSDinh Nguyen } 229647a1685fSDinh Nguyen 229747a1685fSDinh Nguyen if (req->actual < req->length && req->short_not_ok) { 229847a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n", 229947a1685fSDinh Nguyen __func__, req->actual, req->length); 230047a1685fSDinh Nguyen 230147a1685fSDinh Nguyen /* 230247a1685fSDinh Nguyen * todo - what should we return here? there's no one else 230347a1685fSDinh Nguyen * even bothering to check the status. 230447a1685fSDinh Nguyen */ 230547a1685fSDinh Nguyen } 230647a1685fSDinh Nguyen 2307ef750c71SVahram Aharonyan /* DDMA IN status phase will start from StsPhseRcvd interrupt */ 2308ef750c71SVahram Aharonyan if (!using_desc_dma(hsotg) && epnum == 0 && 2309ef750c71SVahram Aharonyan hsotg->ep0_state == DWC2_EP0_DATA_OUT) { 2310fe0b94abSMian Yousaf Kaukab /* Move to STATUS IN */ 23111f91b4ccSFelipe Balbi dwc2_hsotg_ep0_zlp(hsotg, true); 2312fe0b94abSMian Yousaf Kaukab return; 231347a1685fSDinh Nguyen } 231447a1685fSDinh Nguyen 2315ec1f9d9fSRoman Bacik /* 2316ec1f9d9fSRoman Bacik * Slave mode OUT transfers do not go through XferComplete so 2317ec1f9d9fSRoman Bacik * adjust the ISOC parity here. 2318ec1f9d9fSRoman Bacik */ 2319ec1f9d9fSRoman Bacik if (!using_dma(hsotg)) { 2320ec1f9d9fSRoman Bacik if (hs_ep->isochronous && hs_ep->interval == 1) 2321ec1f9d9fSRoman Bacik dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum)); 2322837e9f00SVardan Mikayelyan else if (hs_ep->isochronous && hs_ep->interval > 1) 2323837e9f00SVardan Mikayelyan dwc2_gadget_incr_frame_num(hs_ep); 2324ec1f9d9fSRoman Bacik } 2325ec1f9d9fSRoman Bacik 23261f91b4ccSFelipe Balbi dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result); 232747a1685fSDinh Nguyen } 232847a1685fSDinh Nguyen 232947a1685fSDinh Nguyen /** 23301f91b4ccSFelipe Balbi * dwc2_hsotg_handle_rx - RX FIFO has data 233147a1685fSDinh Nguyen * @hsotg: The device instance 233247a1685fSDinh Nguyen * 233347a1685fSDinh Nguyen * The IRQ handler has detected that the RX FIFO has some data in it 233447a1685fSDinh Nguyen * that requires processing, so find out what is in there and do the 233547a1685fSDinh Nguyen * appropriate read. 233647a1685fSDinh Nguyen * 233747a1685fSDinh Nguyen * The RXFIFO is a true FIFO, the packets coming out are still in packet 233847a1685fSDinh Nguyen * chunks, so if you have x packets received on an endpoint you'll get x 233947a1685fSDinh Nguyen * FIFO events delivered, each with a packet's worth of data in it. 234047a1685fSDinh Nguyen * 234147a1685fSDinh Nguyen * When using DMA, we should not be processing events from the RXFIFO 234247a1685fSDinh Nguyen * as the actual data should be sent to the memory directly and we turn 234347a1685fSDinh Nguyen * on the completion interrupts to get notifications of transfer completion. 234447a1685fSDinh Nguyen */ 23451f91b4ccSFelipe Balbi static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg) 234647a1685fSDinh Nguyen { 234795c8bc36SAntti Seppälä u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP); 234847a1685fSDinh Nguyen u32 epnum, status, size; 234947a1685fSDinh Nguyen 235047a1685fSDinh Nguyen WARN_ON(using_dma(hsotg)); 235147a1685fSDinh Nguyen 235247a1685fSDinh Nguyen epnum = grxstsr & GRXSTS_EPNUM_MASK; 235347a1685fSDinh Nguyen status = grxstsr & GRXSTS_PKTSTS_MASK; 235447a1685fSDinh Nguyen 235547a1685fSDinh Nguyen size = grxstsr & GRXSTS_BYTECNT_MASK; 235647a1685fSDinh Nguyen size >>= GRXSTS_BYTECNT_SHIFT; 235747a1685fSDinh Nguyen 235847a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n", 235947a1685fSDinh Nguyen __func__, grxstsr, size, epnum); 236047a1685fSDinh Nguyen 236147a1685fSDinh Nguyen switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) { 236247a1685fSDinh Nguyen case GRXSTS_PKTSTS_GLOBALOUTNAK: 236347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "GLOBALOUTNAK\n"); 236447a1685fSDinh Nguyen break; 236547a1685fSDinh Nguyen 236647a1685fSDinh Nguyen case GRXSTS_PKTSTS_OUTDONE: 236747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n", 23681f91b4ccSFelipe Balbi dwc2_hsotg_read_frameno(hsotg)); 236947a1685fSDinh Nguyen 237047a1685fSDinh Nguyen if (!using_dma(hsotg)) 23711f91b4ccSFelipe Balbi dwc2_hsotg_handle_outdone(hsotg, epnum); 237247a1685fSDinh Nguyen break; 237347a1685fSDinh Nguyen 237447a1685fSDinh Nguyen case GRXSTS_PKTSTS_SETUPDONE: 237547a1685fSDinh Nguyen dev_dbg(hsotg->dev, 237647a1685fSDinh Nguyen "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n", 23771f91b4ccSFelipe Balbi dwc2_hsotg_read_frameno(hsotg), 237895c8bc36SAntti Seppälä dwc2_readl(hsotg->regs + DOEPCTL(0))); 2379fe0b94abSMian Yousaf Kaukab /* 23801f91b4ccSFelipe Balbi * Call dwc2_hsotg_handle_outdone here if it was not called from 2381fe0b94abSMian Yousaf Kaukab * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't 2382fe0b94abSMian Yousaf Kaukab * generate GRXSTS_PKTSTS_OUTDONE for setup packet. 2383fe0b94abSMian Yousaf Kaukab */ 2384fe0b94abSMian Yousaf Kaukab if (hsotg->ep0_state == DWC2_EP0_SETUP) 23851f91b4ccSFelipe Balbi dwc2_hsotg_handle_outdone(hsotg, epnum); 238647a1685fSDinh Nguyen break; 238747a1685fSDinh Nguyen 238847a1685fSDinh Nguyen case GRXSTS_PKTSTS_OUTRX: 23891f91b4ccSFelipe Balbi dwc2_hsotg_rx_data(hsotg, epnum, size); 239047a1685fSDinh Nguyen break; 239147a1685fSDinh Nguyen 239247a1685fSDinh Nguyen case GRXSTS_PKTSTS_SETUPRX: 239347a1685fSDinh Nguyen dev_dbg(hsotg->dev, 239447a1685fSDinh Nguyen "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n", 23951f91b4ccSFelipe Balbi dwc2_hsotg_read_frameno(hsotg), 239695c8bc36SAntti Seppälä dwc2_readl(hsotg->regs + DOEPCTL(0))); 239747a1685fSDinh Nguyen 2398fe0b94abSMian Yousaf Kaukab WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP); 2399fe0b94abSMian Yousaf Kaukab 24001f91b4ccSFelipe Balbi dwc2_hsotg_rx_data(hsotg, epnum, size); 240147a1685fSDinh Nguyen break; 240247a1685fSDinh Nguyen 240347a1685fSDinh Nguyen default: 240447a1685fSDinh Nguyen dev_warn(hsotg->dev, "%s: unknown status %08x\n", 240547a1685fSDinh Nguyen __func__, grxstsr); 240647a1685fSDinh Nguyen 24071f91b4ccSFelipe Balbi dwc2_hsotg_dump(hsotg); 240847a1685fSDinh Nguyen break; 240947a1685fSDinh Nguyen } 241047a1685fSDinh Nguyen } 241147a1685fSDinh Nguyen 241247a1685fSDinh Nguyen /** 24131f91b4ccSFelipe Balbi * dwc2_hsotg_ep0_mps - turn max packet size into register setting 241447a1685fSDinh Nguyen * @mps: The maximum packet size in bytes. 241547a1685fSDinh Nguyen */ 24161f91b4ccSFelipe Balbi static u32 dwc2_hsotg_ep0_mps(unsigned int mps) 241747a1685fSDinh Nguyen { 241847a1685fSDinh Nguyen switch (mps) { 241947a1685fSDinh Nguyen case 64: 242047a1685fSDinh Nguyen return D0EPCTL_MPS_64; 242147a1685fSDinh Nguyen case 32: 242247a1685fSDinh Nguyen return D0EPCTL_MPS_32; 242347a1685fSDinh Nguyen case 16: 242447a1685fSDinh Nguyen return D0EPCTL_MPS_16; 242547a1685fSDinh Nguyen case 8: 242647a1685fSDinh Nguyen return D0EPCTL_MPS_8; 242747a1685fSDinh Nguyen } 242847a1685fSDinh Nguyen 242947a1685fSDinh Nguyen /* bad max packet size, warn and return invalid result */ 243047a1685fSDinh Nguyen WARN_ON(1); 243147a1685fSDinh Nguyen return (u32)-1; 243247a1685fSDinh Nguyen } 243347a1685fSDinh Nguyen 243447a1685fSDinh Nguyen /** 24351f91b4ccSFelipe Balbi * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field 243647a1685fSDinh Nguyen * @hsotg: The driver state. 243747a1685fSDinh Nguyen * @ep: The index number of the endpoint 243847a1685fSDinh Nguyen * @mps: The maximum packet size in bytes 2439ee2c40deSVardan Mikayelyan * @mc: The multicount value 244047a1685fSDinh Nguyen * 244147a1685fSDinh Nguyen * Configure the maximum packet size for the given endpoint, updating 244247a1685fSDinh Nguyen * the hardware control registers to reflect this. 244347a1685fSDinh Nguyen */ 24441f91b4ccSFelipe Balbi static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg, 2445ee2c40deSVardan Mikayelyan unsigned int ep, unsigned int mps, 2446ee2c40deSVardan Mikayelyan unsigned int mc, unsigned int dir_in) 244747a1685fSDinh Nguyen { 24481f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep; 244947a1685fSDinh Nguyen void __iomem *regs = hsotg->regs; 245047a1685fSDinh Nguyen u32 reg; 245147a1685fSDinh Nguyen 2452c6f5c050SMian Yousaf Kaukab hs_ep = index_to_ep(hsotg, ep, dir_in); 2453c6f5c050SMian Yousaf Kaukab if (!hs_ep) 2454c6f5c050SMian Yousaf Kaukab return; 2455c6f5c050SMian Yousaf Kaukab 245647a1685fSDinh Nguyen if (ep == 0) { 2457ee2c40deSVardan Mikayelyan u32 mps_bytes = mps; 2458ee2c40deSVardan Mikayelyan 245947a1685fSDinh Nguyen /* EP0 is a special case */ 2460ee2c40deSVardan Mikayelyan mps = dwc2_hsotg_ep0_mps(mps_bytes); 2461ee2c40deSVardan Mikayelyan if (mps > 3) 246247a1685fSDinh Nguyen goto bad_mps; 2463ee2c40deSVardan Mikayelyan hs_ep->ep.maxpacket = mps_bytes; 246447a1685fSDinh Nguyen hs_ep->mc = 1; 246547a1685fSDinh Nguyen } else { 2466ee2c40deSVardan Mikayelyan if (mps > 1024) 246747a1685fSDinh Nguyen goto bad_mps; 2468ee2c40deSVardan Mikayelyan hs_ep->mc = mc; 2469ee2c40deSVardan Mikayelyan if (mc > 3) 247047a1685fSDinh Nguyen goto bad_mps; 2471ee2c40deSVardan Mikayelyan hs_ep->ep.maxpacket = mps; 247247a1685fSDinh Nguyen } 247347a1685fSDinh Nguyen 2474c6f5c050SMian Yousaf Kaukab if (dir_in) { 247595c8bc36SAntti Seppälä reg = dwc2_readl(regs + DIEPCTL(ep)); 247647a1685fSDinh Nguyen reg &= ~DXEPCTL_MPS_MASK; 2477ee2c40deSVardan Mikayelyan reg |= mps; 247895c8bc36SAntti Seppälä dwc2_writel(reg, regs + DIEPCTL(ep)); 2479c6f5c050SMian Yousaf Kaukab } else { 248095c8bc36SAntti Seppälä reg = dwc2_readl(regs + DOEPCTL(ep)); 248147a1685fSDinh Nguyen reg &= ~DXEPCTL_MPS_MASK; 2482ee2c40deSVardan Mikayelyan reg |= mps; 248395c8bc36SAntti Seppälä dwc2_writel(reg, regs + DOEPCTL(ep)); 248447a1685fSDinh Nguyen } 248547a1685fSDinh Nguyen 248647a1685fSDinh Nguyen return; 248747a1685fSDinh Nguyen 248847a1685fSDinh Nguyen bad_mps: 248947a1685fSDinh Nguyen dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps); 249047a1685fSDinh Nguyen } 249147a1685fSDinh Nguyen 249247a1685fSDinh Nguyen /** 24931f91b4ccSFelipe Balbi * dwc2_hsotg_txfifo_flush - flush Tx FIFO 249447a1685fSDinh Nguyen * @hsotg: The driver state 249547a1685fSDinh Nguyen * @idx: The index for the endpoint (0..15) 249647a1685fSDinh Nguyen */ 24971f91b4ccSFelipe Balbi static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx) 249847a1685fSDinh Nguyen { 249995c8bc36SAntti Seppälä dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH, 250047a1685fSDinh Nguyen hsotg->regs + GRSTCTL); 250147a1685fSDinh Nguyen 250247a1685fSDinh Nguyen /* wait until the fifo is flushed */ 250379d6b8c5SSevak Arakelyan if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100)) 250479d6b8c5SSevak Arakelyan dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n", 250579d6b8c5SSevak Arakelyan __func__); 250647a1685fSDinh Nguyen } 250747a1685fSDinh Nguyen 250847a1685fSDinh Nguyen /** 25091f91b4ccSFelipe Balbi * dwc2_hsotg_trytx - check to see if anything needs transmitting 251047a1685fSDinh Nguyen * @hsotg: The driver state 251147a1685fSDinh Nguyen * @hs_ep: The driver endpoint to check. 251247a1685fSDinh Nguyen * 251347a1685fSDinh Nguyen * Check to see if there is a request that has data to send, and if so 251447a1685fSDinh Nguyen * make an attempt to write data into the FIFO. 251547a1685fSDinh Nguyen */ 25161f91b4ccSFelipe Balbi static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg, 25171f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep) 251847a1685fSDinh Nguyen { 25191f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req = hs_ep->req; 252047a1685fSDinh Nguyen 252147a1685fSDinh Nguyen if (!hs_ep->dir_in || !hs_req) { 252247a1685fSDinh Nguyen /** 252347a1685fSDinh Nguyen * if request is not enqueued, we disable interrupts 252447a1685fSDinh Nguyen * for endpoints, excepting ep0 252547a1685fSDinh Nguyen */ 252647a1685fSDinh Nguyen if (hs_ep->index != 0) 25271f91b4ccSFelipe Balbi dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, 252847a1685fSDinh Nguyen hs_ep->dir_in, 0); 252947a1685fSDinh Nguyen return 0; 253047a1685fSDinh Nguyen } 253147a1685fSDinh Nguyen 253247a1685fSDinh Nguyen if (hs_req->req.actual < hs_req->req.length) { 253347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "trying to write more for ep%d\n", 253447a1685fSDinh Nguyen hs_ep->index); 25351f91b4ccSFelipe Balbi return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); 253647a1685fSDinh Nguyen } 253747a1685fSDinh Nguyen 253847a1685fSDinh Nguyen return 0; 253947a1685fSDinh Nguyen } 254047a1685fSDinh Nguyen 254147a1685fSDinh Nguyen /** 25421f91b4ccSFelipe Balbi * dwc2_hsotg_complete_in - complete IN transfer 254347a1685fSDinh Nguyen * @hsotg: The device state. 254447a1685fSDinh Nguyen * @hs_ep: The endpoint that has just completed. 254547a1685fSDinh Nguyen * 254647a1685fSDinh Nguyen * An IN transfer has been completed, update the transfer's state and then 254747a1685fSDinh Nguyen * call the relevant completion routines. 254847a1685fSDinh Nguyen */ 25491f91b4ccSFelipe Balbi static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg, 25501f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep) 255147a1685fSDinh Nguyen { 25521f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req = hs_ep->req; 255395c8bc36SAntti Seppälä u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index)); 255447a1685fSDinh Nguyen int size_left, size_done; 255547a1685fSDinh Nguyen 255647a1685fSDinh Nguyen if (!hs_req) { 255747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "XferCompl but no req\n"); 255847a1685fSDinh Nguyen return; 255947a1685fSDinh Nguyen } 256047a1685fSDinh Nguyen 256147a1685fSDinh Nguyen /* Finish ZLP handling for IN EP0 transactions */ 2562fe0b94abSMian Yousaf Kaukab if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) { 2563fe0b94abSMian Yousaf Kaukab dev_dbg(hsotg->dev, "zlp packet sent\n"); 2564c3b22fe2SRazmik Karapetyan 2565c3b22fe2SRazmik Karapetyan /* 2566c3b22fe2SRazmik Karapetyan * While send zlp for DWC2_EP0_STATUS_IN EP direction was 2567c3b22fe2SRazmik Karapetyan * changed to IN. Change back to complete OUT transfer request 2568c3b22fe2SRazmik Karapetyan */ 2569c3b22fe2SRazmik Karapetyan hs_ep->dir_in = 0; 2570c3b22fe2SRazmik Karapetyan 25711f91b4ccSFelipe Balbi dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); 25729e14d0a5SGregory Herrero if (hsotg->test_mode) { 25739e14d0a5SGregory Herrero int ret; 25749e14d0a5SGregory Herrero 25751f91b4ccSFelipe Balbi ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode); 25769e14d0a5SGregory Herrero if (ret < 0) { 25779e14d0a5SGregory Herrero dev_dbg(hsotg->dev, "Invalid Test #%d\n", 25789e14d0a5SGregory Herrero hsotg->test_mode); 25791f91b4ccSFelipe Balbi dwc2_hsotg_stall_ep0(hsotg); 25809e14d0a5SGregory Herrero return; 25819e14d0a5SGregory Herrero } 25829e14d0a5SGregory Herrero } 25831f91b4ccSFelipe Balbi dwc2_hsotg_enqueue_setup(hsotg); 258447a1685fSDinh Nguyen return; 258547a1685fSDinh Nguyen } 258647a1685fSDinh Nguyen 258747a1685fSDinh Nguyen /* 258847a1685fSDinh Nguyen * Calculate the size of the transfer by checking how much is left 258947a1685fSDinh Nguyen * in the endpoint size register and then working it out from 259047a1685fSDinh Nguyen * the amount we loaded for the transfer. 259147a1685fSDinh Nguyen * 259247a1685fSDinh Nguyen * We do this even for DMA, as the transfer may have incremented 259347a1685fSDinh Nguyen * past the end of the buffer (DMA transfers are always 32bit 259447a1685fSDinh Nguyen * aligned). 259547a1685fSDinh Nguyen */ 2596aa3e8bc8SVahram Aharonyan if (using_desc_dma(hsotg)) { 2597aa3e8bc8SVahram Aharonyan size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); 2598aa3e8bc8SVahram Aharonyan if (size_left < 0) 2599aa3e8bc8SVahram Aharonyan dev_err(hsotg->dev, "error parsing DDMA results %d\n", 2600aa3e8bc8SVahram Aharonyan size_left); 2601aa3e8bc8SVahram Aharonyan } else { 260247a1685fSDinh Nguyen size_left = DXEPTSIZ_XFERSIZE_GET(epsize); 2603aa3e8bc8SVahram Aharonyan } 260447a1685fSDinh Nguyen 260547a1685fSDinh Nguyen size_done = hs_ep->size_loaded - size_left; 260647a1685fSDinh Nguyen size_done += hs_ep->last_load; 260747a1685fSDinh Nguyen 260847a1685fSDinh Nguyen if (hs_req->req.actual != size_done) 260947a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n", 261047a1685fSDinh Nguyen __func__, hs_req->req.actual, size_done); 261147a1685fSDinh Nguyen 261247a1685fSDinh Nguyen hs_req->req.actual = size_done; 261347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n", 261447a1685fSDinh Nguyen hs_req->req.length, hs_req->req.actual, hs_req->req.zero); 261547a1685fSDinh Nguyen 261647a1685fSDinh Nguyen if (!size_left && hs_req->req.actual < hs_req->req.length) { 261747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__); 26181f91b4ccSFelipe Balbi dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); 2619fe0b94abSMian Yousaf Kaukab return; 2620fe0b94abSMian Yousaf Kaukab } 2621fe0b94abSMian Yousaf Kaukab 2622f71b5e25SMian Yousaf Kaukab /* Zlp for all endpoints, for ep0 only in DATA IN stage */ 26238a20fa45SMian Yousaf Kaukab if (hs_ep->send_zlp) { 26241f91b4ccSFelipe Balbi dwc2_hsotg_program_zlp(hsotg, hs_ep); 26258a20fa45SMian Yousaf Kaukab hs_ep->send_zlp = 0; 2626f71b5e25SMian Yousaf Kaukab /* transfer will be completed on next complete interrupt */ 2627f71b5e25SMian Yousaf Kaukab return; 2628f71b5e25SMian Yousaf Kaukab } 2629f71b5e25SMian Yousaf Kaukab 2630fe0b94abSMian Yousaf Kaukab if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) { 2631fe0b94abSMian Yousaf Kaukab /* Move to STATUS OUT */ 26321f91b4ccSFelipe Balbi dwc2_hsotg_ep0_zlp(hsotg, false); 2633fe0b94abSMian Yousaf Kaukab return; 2634fe0b94abSMian Yousaf Kaukab } 2635fe0b94abSMian Yousaf Kaukab 26361f91b4ccSFelipe Balbi dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); 263747a1685fSDinh Nguyen } 263847a1685fSDinh Nguyen 263947a1685fSDinh Nguyen /** 264032601588SVardan Mikayelyan * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep 264132601588SVardan Mikayelyan * @hsotg: The device state. 264232601588SVardan Mikayelyan * @idx: Index of ep. 264332601588SVardan Mikayelyan * @dir_in: Endpoint direction 1-in 0-out. 264432601588SVardan Mikayelyan * 264532601588SVardan Mikayelyan * Reads for endpoint with given index and direction, by masking 264632601588SVardan Mikayelyan * epint_reg with coresponding mask. 264732601588SVardan Mikayelyan */ 264832601588SVardan Mikayelyan static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg, 264932601588SVardan Mikayelyan unsigned int idx, int dir_in) 265032601588SVardan Mikayelyan { 265132601588SVardan Mikayelyan u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; 265232601588SVardan Mikayelyan u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); 265332601588SVardan Mikayelyan u32 ints; 265432601588SVardan Mikayelyan u32 mask; 265532601588SVardan Mikayelyan u32 diepempmsk; 265632601588SVardan Mikayelyan 265732601588SVardan Mikayelyan mask = dwc2_readl(hsotg->regs + epmsk_reg); 265832601588SVardan Mikayelyan diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK); 265932601588SVardan Mikayelyan mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0; 266032601588SVardan Mikayelyan mask |= DXEPINT_SETUP_RCVD; 266132601588SVardan Mikayelyan 266232601588SVardan Mikayelyan ints = dwc2_readl(hsotg->regs + epint_reg); 266332601588SVardan Mikayelyan ints &= mask; 266432601588SVardan Mikayelyan return ints; 266532601588SVardan Mikayelyan } 266632601588SVardan Mikayelyan 266732601588SVardan Mikayelyan /** 2668bd9971f0SVardan Mikayelyan * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD 2669bd9971f0SVardan Mikayelyan * @hs_ep: The endpoint on which interrupt is asserted. 2670bd9971f0SVardan Mikayelyan * 2671bd9971f0SVardan Mikayelyan * This interrupt indicates that the endpoint has been disabled per the 2672bd9971f0SVardan Mikayelyan * application's request. 2673bd9971f0SVardan Mikayelyan * 2674bd9971f0SVardan Mikayelyan * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK, 2675bd9971f0SVardan Mikayelyan * in case of ISOC completes current request. 2676bd9971f0SVardan Mikayelyan * 2677bd9971f0SVardan Mikayelyan * For ISOC-OUT endpoints completes expired requests. If there is remaining 2678bd9971f0SVardan Mikayelyan * request starts it. 2679bd9971f0SVardan Mikayelyan */ 2680bd9971f0SVardan Mikayelyan static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep) 2681bd9971f0SVardan Mikayelyan { 2682bd9971f0SVardan Mikayelyan struct dwc2_hsotg *hsotg = hs_ep->parent; 2683bd9971f0SVardan Mikayelyan struct dwc2_hsotg_req *hs_req; 2684bd9971f0SVardan Mikayelyan unsigned char idx = hs_ep->index; 2685bd9971f0SVardan Mikayelyan int dir_in = hs_ep->dir_in; 2686bd9971f0SVardan Mikayelyan u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); 2687bd9971f0SVardan Mikayelyan int dctl = dwc2_readl(hsotg->regs + DCTL); 2688bd9971f0SVardan Mikayelyan 2689bd9971f0SVardan Mikayelyan dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__); 2690bd9971f0SVardan Mikayelyan 2691bd9971f0SVardan Mikayelyan if (dir_in) { 2692bd9971f0SVardan Mikayelyan int epctl = dwc2_readl(hsotg->regs + epctl_reg); 2693bd9971f0SVardan Mikayelyan 2694bd9971f0SVardan Mikayelyan dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index); 2695bd9971f0SVardan Mikayelyan 2696bd9971f0SVardan Mikayelyan if (hs_ep->isochronous) { 2697bd9971f0SVardan Mikayelyan dwc2_hsotg_complete_in(hsotg, hs_ep); 2698bd9971f0SVardan Mikayelyan return; 2699bd9971f0SVardan Mikayelyan } 2700bd9971f0SVardan Mikayelyan 2701bd9971f0SVardan Mikayelyan if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) { 2702bd9971f0SVardan Mikayelyan int dctl = dwc2_readl(hsotg->regs + DCTL); 2703bd9971f0SVardan Mikayelyan 2704bd9971f0SVardan Mikayelyan dctl |= DCTL_CGNPINNAK; 2705bd9971f0SVardan Mikayelyan dwc2_writel(dctl, hsotg->regs + DCTL); 2706bd9971f0SVardan Mikayelyan } 2707bd9971f0SVardan Mikayelyan return; 2708bd9971f0SVardan Mikayelyan } 2709bd9971f0SVardan Mikayelyan 2710bd9971f0SVardan Mikayelyan if (dctl & DCTL_GOUTNAKSTS) { 2711bd9971f0SVardan Mikayelyan dctl |= DCTL_CGOUTNAK; 2712bd9971f0SVardan Mikayelyan dwc2_writel(dctl, hsotg->regs + DCTL); 2713bd9971f0SVardan Mikayelyan } 2714bd9971f0SVardan Mikayelyan 2715bd9971f0SVardan Mikayelyan if (!hs_ep->isochronous) 2716bd9971f0SVardan Mikayelyan return; 2717bd9971f0SVardan Mikayelyan 2718bd9971f0SVardan Mikayelyan if (list_empty(&hs_ep->queue)) { 2719bd9971f0SVardan Mikayelyan dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n", 2720bd9971f0SVardan Mikayelyan __func__, hs_ep); 2721bd9971f0SVardan Mikayelyan return; 2722bd9971f0SVardan Mikayelyan } 2723bd9971f0SVardan Mikayelyan 2724bd9971f0SVardan Mikayelyan do { 2725bd9971f0SVardan Mikayelyan hs_req = get_ep_head(hs_ep); 2726bd9971f0SVardan Mikayelyan if (hs_req) 2727bd9971f0SVardan Mikayelyan dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 2728bd9971f0SVardan Mikayelyan -ENODATA); 2729bd9971f0SVardan Mikayelyan dwc2_gadget_incr_frame_num(hs_ep); 2730bd9971f0SVardan Mikayelyan } while (dwc2_gadget_target_frame_elapsed(hs_ep)); 2731bd9971f0SVardan Mikayelyan 2732bd9971f0SVardan Mikayelyan dwc2_gadget_start_next_request(hs_ep); 2733bd9971f0SVardan Mikayelyan } 2734bd9971f0SVardan Mikayelyan 2735bd9971f0SVardan Mikayelyan /** 27365321922cSVardan Mikayelyan * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS 27375321922cSVardan Mikayelyan * @hs_ep: The endpoint on which interrupt is asserted. 27385321922cSVardan Mikayelyan * 27395321922cSVardan Mikayelyan * This is starting point for ISOC-OUT transfer, synchronization done with 27405321922cSVardan Mikayelyan * first out token received from host while corresponding EP is disabled. 27415321922cSVardan Mikayelyan * 27425321922cSVardan Mikayelyan * Device does not know initial frame in which out token will come. For this 27435321922cSVardan Mikayelyan * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon 27445321922cSVardan Mikayelyan * getting this interrupt SW starts calculation for next transfer frame. 27455321922cSVardan Mikayelyan */ 27465321922cSVardan Mikayelyan static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep) 27475321922cSVardan Mikayelyan { 27485321922cSVardan Mikayelyan struct dwc2_hsotg *hsotg = ep->parent; 27495321922cSVardan Mikayelyan int dir_in = ep->dir_in; 27505321922cSVardan Mikayelyan u32 doepmsk; 2751540ccba0SVahram Aharonyan u32 tmp; 27525321922cSVardan Mikayelyan 27535321922cSVardan Mikayelyan if (dir_in || !ep->isochronous) 27545321922cSVardan Mikayelyan return; 27555321922cSVardan Mikayelyan 2756540ccba0SVahram Aharonyan /* 2757540ccba0SVahram Aharonyan * Store frame in which irq was asserted here, as 2758540ccba0SVahram Aharonyan * it can change while completing request below. 2759540ccba0SVahram Aharonyan */ 2760540ccba0SVahram Aharonyan tmp = dwc2_hsotg_read_frameno(hsotg); 2761540ccba0SVahram Aharonyan 27625321922cSVardan Mikayelyan dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA); 27635321922cSVardan Mikayelyan 2764540ccba0SVahram Aharonyan if (using_desc_dma(hsotg)) { 2765540ccba0SVahram Aharonyan if (ep->target_frame == TARGET_FRAME_INITIAL) { 2766540ccba0SVahram Aharonyan /* Start first ISO Out */ 2767540ccba0SVahram Aharonyan ep->target_frame = tmp; 2768540ccba0SVahram Aharonyan dwc2_gadget_start_isoc_ddma(ep); 2769540ccba0SVahram Aharonyan } 2770540ccba0SVahram Aharonyan return; 2771540ccba0SVahram Aharonyan } 2772540ccba0SVahram Aharonyan 27735321922cSVardan Mikayelyan if (ep->interval > 1 && 27745321922cSVardan Mikayelyan ep->target_frame == TARGET_FRAME_INITIAL) { 27755321922cSVardan Mikayelyan u32 dsts; 27765321922cSVardan Mikayelyan u32 ctrl; 27775321922cSVardan Mikayelyan 27785321922cSVardan Mikayelyan dsts = dwc2_readl(hsotg->regs + DSTS); 27795321922cSVardan Mikayelyan ep->target_frame = dwc2_hsotg_read_frameno(hsotg); 27805321922cSVardan Mikayelyan dwc2_gadget_incr_frame_num(ep); 27815321922cSVardan Mikayelyan 27825321922cSVardan Mikayelyan ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index)); 27835321922cSVardan Mikayelyan if (ep->target_frame & 0x1) 27845321922cSVardan Mikayelyan ctrl |= DXEPCTL_SETODDFR; 27855321922cSVardan Mikayelyan else 27865321922cSVardan Mikayelyan ctrl |= DXEPCTL_SETEVENFR; 27875321922cSVardan Mikayelyan 27885321922cSVardan Mikayelyan dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index)); 27895321922cSVardan Mikayelyan } 27905321922cSVardan Mikayelyan 27915321922cSVardan Mikayelyan dwc2_gadget_start_next_request(ep); 27925321922cSVardan Mikayelyan doepmsk = dwc2_readl(hsotg->regs + DOEPMSK); 27935321922cSVardan Mikayelyan doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK; 27945321922cSVardan Mikayelyan dwc2_writel(doepmsk, hsotg->regs + DOEPMSK); 27955321922cSVardan Mikayelyan } 27965321922cSVardan Mikayelyan 27975321922cSVardan Mikayelyan /** 27985321922cSVardan Mikayelyan * dwc2_gadget_handle_nak - handle NAK interrupt 27995321922cSVardan Mikayelyan * @hs_ep: The endpoint on which interrupt is asserted. 28005321922cSVardan Mikayelyan * 28015321922cSVardan Mikayelyan * This is starting point for ISOC-IN transfer, synchronization done with 28025321922cSVardan Mikayelyan * first IN token received from host while corresponding EP is disabled. 28035321922cSVardan Mikayelyan * 28045321922cSVardan Mikayelyan * Device does not know when first one token will arrive from host. On first 28055321922cSVardan Mikayelyan * token arrival HW generates 2 interrupts: 'in token received while FIFO empty' 28065321922cSVardan Mikayelyan * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was 28075321922cSVardan Mikayelyan * sent in response to that as there was no data in FIFO. SW is basing on this 28085321922cSVardan Mikayelyan * interrupt to obtain frame in which token has come and then based on the 28095321922cSVardan Mikayelyan * interval calculates next frame for transfer. 28105321922cSVardan Mikayelyan */ 28115321922cSVardan Mikayelyan static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep) 28125321922cSVardan Mikayelyan { 28135321922cSVardan Mikayelyan struct dwc2_hsotg *hsotg = hs_ep->parent; 28145321922cSVardan Mikayelyan int dir_in = hs_ep->dir_in; 28155321922cSVardan Mikayelyan 28165321922cSVardan Mikayelyan if (!dir_in || !hs_ep->isochronous) 28175321922cSVardan Mikayelyan return; 28185321922cSVardan Mikayelyan 28195321922cSVardan Mikayelyan if (hs_ep->target_frame == TARGET_FRAME_INITIAL) { 28205321922cSVardan Mikayelyan hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg); 2821540ccba0SVahram Aharonyan 2822540ccba0SVahram Aharonyan if (using_desc_dma(hsotg)) { 2823540ccba0SVahram Aharonyan dwc2_gadget_start_isoc_ddma(hs_ep); 2824540ccba0SVahram Aharonyan return; 2825540ccba0SVahram Aharonyan } 2826540ccba0SVahram Aharonyan 28275321922cSVardan Mikayelyan if (hs_ep->interval > 1) { 28285321922cSVardan Mikayelyan u32 ctrl = dwc2_readl(hsotg->regs + 28295321922cSVardan Mikayelyan DIEPCTL(hs_ep->index)); 28305321922cSVardan Mikayelyan if (hs_ep->target_frame & 0x1) 28315321922cSVardan Mikayelyan ctrl |= DXEPCTL_SETODDFR; 28325321922cSVardan Mikayelyan else 28335321922cSVardan Mikayelyan ctrl |= DXEPCTL_SETEVENFR; 28345321922cSVardan Mikayelyan 28355321922cSVardan Mikayelyan dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index)); 28365321922cSVardan Mikayelyan } 28375321922cSVardan Mikayelyan 28385321922cSVardan Mikayelyan dwc2_hsotg_complete_request(hsotg, hs_ep, 28395321922cSVardan Mikayelyan get_ep_head(hs_ep), 0); 28405321922cSVardan Mikayelyan } 28415321922cSVardan Mikayelyan 28425321922cSVardan Mikayelyan dwc2_gadget_incr_frame_num(hs_ep); 28435321922cSVardan Mikayelyan } 28445321922cSVardan Mikayelyan 28455321922cSVardan Mikayelyan /** 28461f91b4ccSFelipe Balbi * dwc2_hsotg_epint - handle an in/out endpoint interrupt 284747a1685fSDinh Nguyen * @hsotg: The driver state 284847a1685fSDinh Nguyen * @idx: The index for the endpoint (0..15) 284947a1685fSDinh Nguyen * @dir_in: Set if this is an IN endpoint 285047a1685fSDinh Nguyen * 285147a1685fSDinh Nguyen * Process and clear any interrupt pending for an individual endpoint 285247a1685fSDinh Nguyen */ 28531f91b4ccSFelipe Balbi static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx, 285447a1685fSDinh Nguyen int dir_in) 285547a1685fSDinh Nguyen { 28561f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in); 285747a1685fSDinh Nguyen u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); 285847a1685fSDinh Nguyen u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); 285947a1685fSDinh Nguyen u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx); 286047a1685fSDinh Nguyen u32 ints; 286147a1685fSDinh Nguyen u32 ctrl; 286247a1685fSDinh Nguyen 286332601588SVardan Mikayelyan ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in); 286495c8bc36SAntti Seppälä ctrl = dwc2_readl(hsotg->regs + epctl_reg); 286547a1685fSDinh Nguyen 286647a1685fSDinh Nguyen /* Clear endpoint interrupts */ 286795c8bc36SAntti Seppälä dwc2_writel(ints, hsotg->regs + epint_reg); 286847a1685fSDinh Nguyen 2869c6f5c050SMian Yousaf Kaukab if (!hs_ep) { 2870c6f5c050SMian Yousaf Kaukab dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n", 2871c6f5c050SMian Yousaf Kaukab __func__, idx, dir_in ? "in" : "out"); 2872c6f5c050SMian Yousaf Kaukab return; 2873c6f5c050SMian Yousaf Kaukab } 2874c6f5c050SMian Yousaf Kaukab 287547a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n", 287647a1685fSDinh Nguyen __func__, idx, dir_in ? "in" : "out", ints); 287747a1685fSDinh Nguyen 2878b787d755SMian Yousaf Kaukab /* Don't process XferCompl interrupt if it is a setup packet */ 2879b787d755SMian Yousaf Kaukab if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD))) 2880b787d755SMian Yousaf Kaukab ints &= ~DXEPINT_XFERCOMPL; 2881b787d755SMian Yousaf Kaukab 2882f0afdb42SVahram Aharonyan /* 2883f0afdb42SVahram Aharonyan * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP 2884f0afdb42SVahram Aharonyan * stage and xfercomplete was generated without SETUP phase done 2885f0afdb42SVahram Aharonyan * interrupt. SW should parse received setup packet only after host's 2886f0afdb42SVahram Aharonyan * exit from setup phase of control transfer. 2887f0afdb42SVahram Aharonyan */ 2888f0afdb42SVahram Aharonyan if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in && 2889f0afdb42SVahram Aharonyan hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP)) 2890f0afdb42SVahram Aharonyan ints &= ~DXEPINT_XFERCOMPL; 2891f0afdb42SVahram Aharonyan 2892837e9f00SVardan Mikayelyan if (ints & DXEPINT_XFERCOMPL) { 289347a1685fSDinh Nguyen dev_dbg(hsotg->dev, 289447a1685fSDinh Nguyen "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n", 289595c8bc36SAntti Seppälä __func__, dwc2_readl(hsotg->regs + epctl_reg), 289695c8bc36SAntti Seppälä dwc2_readl(hsotg->regs + epsiz_reg)); 289747a1685fSDinh Nguyen 2898540ccba0SVahram Aharonyan /* In DDMA handle isochronous requests separately */ 2899540ccba0SVahram Aharonyan if (using_desc_dma(hsotg) && hs_ep->isochronous) { 2900540ccba0SVahram Aharonyan dwc2_gadget_complete_isoc_request_ddma(hs_ep); 2901540ccba0SVahram Aharonyan /* Try to start next isoc request */ 2902540ccba0SVahram Aharonyan dwc2_gadget_start_next_isoc_ddma(hs_ep); 2903540ccba0SVahram Aharonyan } else if (dir_in) { 290447a1685fSDinh Nguyen /* 2905540ccba0SVahram Aharonyan * We get OutDone from the FIFO, so we only 2906540ccba0SVahram Aharonyan * need to look at completing IN requests here 2907540ccba0SVahram Aharonyan * if operating slave mode 290847a1685fSDinh Nguyen */ 2909837e9f00SVardan Mikayelyan if (hs_ep->isochronous && hs_ep->interval > 1) 2910837e9f00SVardan Mikayelyan dwc2_gadget_incr_frame_num(hs_ep); 2911837e9f00SVardan Mikayelyan 29121f91b4ccSFelipe Balbi dwc2_hsotg_complete_in(hsotg, hs_ep); 2913837e9f00SVardan Mikayelyan if (ints & DXEPINT_NAKINTRPT) 2914837e9f00SVardan Mikayelyan ints &= ~DXEPINT_NAKINTRPT; 291547a1685fSDinh Nguyen 291647a1685fSDinh Nguyen if (idx == 0 && !hs_ep->req) 29171f91b4ccSFelipe Balbi dwc2_hsotg_enqueue_setup(hsotg); 291847a1685fSDinh Nguyen } else if (using_dma(hsotg)) { 291947a1685fSDinh Nguyen /* 292047a1685fSDinh Nguyen * We're using DMA, we need to fire an OutDone here 292147a1685fSDinh Nguyen * as we ignore the RXFIFO. 292247a1685fSDinh Nguyen */ 2923837e9f00SVardan Mikayelyan if (hs_ep->isochronous && hs_ep->interval > 1) 2924837e9f00SVardan Mikayelyan dwc2_gadget_incr_frame_num(hs_ep); 292547a1685fSDinh Nguyen 29261f91b4ccSFelipe Balbi dwc2_hsotg_handle_outdone(hsotg, idx); 292747a1685fSDinh Nguyen } 292847a1685fSDinh Nguyen } 292947a1685fSDinh Nguyen 2930bd9971f0SVardan Mikayelyan if (ints & DXEPINT_EPDISBLD) 2931bd9971f0SVardan Mikayelyan dwc2_gadget_handle_ep_disabled(hs_ep); 293247a1685fSDinh Nguyen 29335321922cSVardan Mikayelyan if (ints & DXEPINT_OUTTKNEPDIS) 29345321922cSVardan Mikayelyan dwc2_gadget_handle_out_token_ep_disabled(hs_ep); 29355321922cSVardan Mikayelyan 29365321922cSVardan Mikayelyan if (ints & DXEPINT_NAKINTRPT) 29375321922cSVardan Mikayelyan dwc2_gadget_handle_nak(hs_ep); 29385321922cSVardan Mikayelyan 293947a1685fSDinh Nguyen if (ints & DXEPINT_AHBERR) 294047a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__); 294147a1685fSDinh Nguyen 294247a1685fSDinh Nguyen if (ints & DXEPINT_SETUP) { /* Setup or Timeout */ 294347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__); 294447a1685fSDinh Nguyen 294547a1685fSDinh Nguyen if (using_dma(hsotg) && idx == 0) { 294647a1685fSDinh Nguyen /* 294747a1685fSDinh Nguyen * this is the notification we've received a 294847a1685fSDinh Nguyen * setup packet. In non-DMA mode we'd get this 294947a1685fSDinh Nguyen * from the RXFIFO, instead we need to process 295047a1685fSDinh Nguyen * the setup here. 295147a1685fSDinh Nguyen */ 295247a1685fSDinh Nguyen 295347a1685fSDinh Nguyen if (dir_in) 295447a1685fSDinh Nguyen WARN_ON_ONCE(1); 295547a1685fSDinh Nguyen else 29561f91b4ccSFelipe Balbi dwc2_hsotg_handle_outdone(hsotg, 0); 295747a1685fSDinh Nguyen } 295847a1685fSDinh Nguyen } 295947a1685fSDinh Nguyen 2960ef750c71SVahram Aharonyan if (ints & DXEPINT_STSPHSERCVD) { 29619d9a6b07SVahram Aharonyan dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__); 29629d9a6b07SVahram Aharonyan 29639e95a66cSMinas Harutyunyan /* Safety check EP0 state when STSPHSERCVD asserted */ 29649e95a66cSMinas Harutyunyan if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) { 2965ef750c71SVahram Aharonyan /* Move to STATUS IN for DDMA */ 2966ef750c71SVahram Aharonyan if (using_desc_dma(hsotg)) 2967ef750c71SVahram Aharonyan dwc2_hsotg_ep0_zlp(hsotg, true); 2968ef750c71SVahram Aharonyan } 2969ef750c71SVahram Aharonyan 29709e95a66cSMinas Harutyunyan } 29719e95a66cSMinas Harutyunyan 297247a1685fSDinh Nguyen if (ints & DXEPINT_BACK2BACKSETUP) 297347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__); 297447a1685fSDinh Nguyen 2975540ccba0SVahram Aharonyan if (ints & DXEPINT_BNAINTR) { 2976540ccba0SVahram Aharonyan dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__); 2977540ccba0SVahram Aharonyan 2978540ccba0SVahram Aharonyan /* 2979540ccba0SVahram Aharonyan * Try to start next isoc request, if any. 2980540ccba0SVahram Aharonyan * Sometimes the endpoint remains enabled after BNA interrupt 2981540ccba0SVahram Aharonyan * assertion, which is not expected, hence we can enter here 2982540ccba0SVahram Aharonyan * couple of times. 2983540ccba0SVahram Aharonyan */ 2984540ccba0SVahram Aharonyan if (hs_ep->isochronous) 2985540ccba0SVahram Aharonyan dwc2_gadget_start_next_isoc_ddma(hs_ep); 2986540ccba0SVahram Aharonyan } 2987540ccba0SVahram Aharonyan 298847a1685fSDinh Nguyen if (dir_in && !hs_ep->isochronous) { 298947a1685fSDinh Nguyen /* not sure if this is important, but we'll clear it anyway */ 299026ddef5dSVardan Mikayelyan if (ints & DXEPINT_INTKNTXFEMP) { 299147a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n", 299247a1685fSDinh Nguyen __func__, idx); 299347a1685fSDinh Nguyen } 299447a1685fSDinh Nguyen 299547a1685fSDinh Nguyen /* this probably means something bad is happening */ 299626ddef5dSVardan Mikayelyan if (ints & DXEPINT_INTKNEPMIS) { 299747a1685fSDinh Nguyen dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n", 299847a1685fSDinh Nguyen __func__, idx); 299947a1685fSDinh Nguyen } 300047a1685fSDinh Nguyen 300147a1685fSDinh Nguyen /* FIFO has space or is empty (see GAHBCFG) */ 300247a1685fSDinh Nguyen if (hsotg->dedicated_fifos && 300326ddef5dSVardan Mikayelyan ints & DXEPINT_TXFEMP) { 300447a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n", 300547a1685fSDinh Nguyen __func__, idx); 300647a1685fSDinh Nguyen if (!using_dma(hsotg)) 30071f91b4ccSFelipe Balbi dwc2_hsotg_trytx(hsotg, hs_ep); 300847a1685fSDinh Nguyen } 300947a1685fSDinh Nguyen } 301047a1685fSDinh Nguyen } 301147a1685fSDinh Nguyen 301247a1685fSDinh Nguyen /** 30131f91b4ccSFelipe Balbi * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done) 301447a1685fSDinh Nguyen * @hsotg: The device state. 301547a1685fSDinh Nguyen * 301647a1685fSDinh Nguyen * Handle updating the device settings after the enumeration phase has 301747a1685fSDinh Nguyen * been completed. 301847a1685fSDinh Nguyen */ 30191f91b4ccSFelipe Balbi static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg) 302047a1685fSDinh Nguyen { 302195c8bc36SAntti Seppälä u32 dsts = dwc2_readl(hsotg->regs + DSTS); 30229b2667f1SJingoo Han int ep0_mps = 0, ep_mps = 8; 302347a1685fSDinh Nguyen 302447a1685fSDinh Nguyen /* 302547a1685fSDinh Nguyen * This should signal the finish of the enumeration phase 302647a1685fSDinh Nguyen * of the USB handshaking, so we should now know what rate 302747a1685fSDinh Nguyen * we connected at. 302847a1685fSDinh Nguyen */ 302947a1685fSDinh Nguyen 303047a1685fSDinh Nguyen dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts); 303147a1685fSDinh Nguyen 303247a1685fSDinh Nguyen /* 303347a1685fSDinh Nguyen * note, since we're limited by the size of transfer on EP0, and 303447a1685fSDinh Nguyen * it seems IN transfers must be a even number of packets we do 303547a1685fSDinh Nguyen * not advertise a 64byte MPS on EP0. 303647a1685fSDinh Nguyen */ 303747a1685fSDinh Nguyen 303847a1685fSDinh Nguyen /* catch both EnumSpd_FS and EnumSpd_FS48 */ 30396d76c92cSMarek Vasut switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) { 304047a1685fSDinh Nguyen case DSTS_ENUMSPD_FS: 304147a1685fSDinh Nguyen case DSTS_ENUMSPD_FS48: 304247a1685fSDinh Nguyen hsotg->gadget.speed = USB_SPEED_FULL; 304347a1685fSDinh Nguyen ep0_mps = EP0_MPS_LIMIT; 304447a1685fSDinh Nguyen ep_mps = 1023; 304547a1685fSDinh Nguyen break; 304647a1685fSDinh Nguyen 304747a1685fSDinh Nguyen case DSTS_ENUMSPD_HS: 304847a1685fSDinh Nguyen hsotg->gadget.speed = USB_SPEED_HIGH; 304947a1685fSDinh Nguyen ep0_mps = EP0_MPS_LIMIT; 305047a1685fSDinh Nguyen ep_mps = 1024; 305147a1685fSDinh Nguyen break; 305247a1685fSDinh Nguyen 305347a1685fSDinh Nguyen case DSTS_ENUMSPD_LS: 305447a1685fSDinh Nguyen hsotg->gadget.speed = USB_SPEED_LOW; 3055552d940fSVardan Mikayelyan ep0_mps = 8; 3056552d940fSVardan Mikayelyan ep_mps = 8; 305747a1685fSDinh Nguyen /* 305847a1685fSDinh Nguyen * note, we don't actually support LS in this driver at the 305947a1685fSDinh Nguyen * moment, and the documentation seems to imply that it isn't 306047a1685fSDinh Nguyen * supported by the PHYs on some of the devices. 306147a1685fSDinh Nguyen */ 306247a1685fSDinh Nguyen break; 306347a1685fSDinh Nguyen } 306447a1685fSDinh Nguyen dev_info(hsotg->dev, "new device is %s\n", 306547a1685fSDinh Nguyen usb_speed_string(hsotg->gadget.speed)); 306647a1685fSDinh Nguyen 306747a1685fSDinh Nguyen /* 306847a1685fSDinh Nguyen * we should now know the maximum packet size for an 306947a1685fSDinh Nguyen * endpoint, so set the endpoints to a default value. 307047a1685fSDinh Nguyen */ 307147a1685fSDinh Nguyen 307247a1685fSDinh Nguyen if (ep0_mps) { 307347a1685fSDinh Nguyen int i; 3074c6f5c050SMian Yousaf Kaukab /* Initialize ep0 for both in and out directions */ 3075ee2c40deSVardan Mikayelyan dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1); 3076ee2c40deSVardan Mikayelyan dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0); 3077c6f5c050SMian Yousaf Kaukab for (i = 1; i < hsotg->num_of_eps; i++) { 3078c6f5c050SMian Yousaf Kaukab if (hsotg->eps_in[i]) 3079ee2c40deSVardan Mikayelyan dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 3080ee2c40deSVardan Mikayelyan 0, 1); 3081c6f5c050SMian Yousaf Kaukab if (hsotg->eps_out[i]) 3082ee2c40deSVardan Mikayelyan dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 3083ee2c40deSVardan Mikayelyan 0, 0); 3084c6f5c050SMian Yousaf Kaukab } 308547a1685fSDinh Nguyen } 308647a1685fSDinh Nguyen 308747a1685fSDinh Nguyen /* ensure after enumeration our EP0 is active */ 308847a1685fSDinh Nguyen 30891f91b4ccSFelipe Balbi dwc2_hsotg_enqueue_setup(hsotg); 309047a1685fSDinh Nguyen 309147a1685fSDinh Nguyen dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", 309295c8bc36SAntti Seppälä dwc2_readl(hsotg->regs + DIEPCTL0), 309395c8bc36SAntti Seppälä dwc2_readl(hsotg->regs + DOEPCTL0)); 309447a1685fSDinh Nguyen } 309547a1685fSDinh Nguyen 309647a1685fSDinh Nguyen /** 309747a1685fSDinh Nguyen * kill_all_requests - remove all requests from the endpoint's queue 309847a1685fSDinh Nguyen * @hsotg: The device state. 309947a1685fSDinh Nguyen * @ep: The endpoint the requests may be on. 310047a1685fSDinh Nguyen * @result: The result code to use. 310147a1685fSDinh Nguyen * 310247a1685fSDinh Nguyen * Go through the requests on the given endpoint and mark them 310347a1685fSDinh Nguyen * completed with the given result code. 310447a1685fSDinh Nguyen */ 3105941fcce4SDinh Nguyen static void kill_all_requests(struct dwc2_hsotg *hsotg, 31061f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep, 31076b448af4SRobert Baldyga int result) 310847a1685fSDinh Nguyen { 31091f91b4ccSFelipe Balbi struct dwc2_hsotg_req *req, *treq; 31109da51974SJohn Youn unsigned int size; 311147a1685fSDinh Nguyen 31126b448af4SRobert Baldyga ep->req = NULL; 311347a1685fSDinh Nguyen 31146b448af4SRobert Baldyga list_for_each_entry_safe(req, treq, &ep->queue, queue) 31151f91b4ccSFelipe Balbi dwc2_hsotg_complete_request(hsotg, ep, req, 311647a1685fSDinh Nguyen result); 31176b448af4SRobert Baldyga 3118b203d0a2SRobert Baldyga if (!hsotg->dedicated_fifos) 3119b203d0a2SRobert Baldyga return; 3120ad674a15SRobert Baldyga size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4; 3121b203d0a2SRobert Baldyga if (size < ep->fifo_size) 31221f91b4ccSFelipe Balbi dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index); 312347a1685fSDinh Nguyen } 312447a1685fSDinh Nguyen 312547a1685fSDinh Nguyen /** 31261f91b4ccSFelipe Balbi * dwc2_hsotg_disconnect - disconnect service 312747a1685fSDinh Nguyen * @hsotg: The device state. 312847a1685fSDinh Nguyen * 312947a1685fSDinh Nguyen * The device has been disconnected. Remove all current 313047a1685fSDinh Nguyen * transactions and signal the gadget driver that this 313147a1685fSDinh Nguyen * has happened. 313247a1685fSDinh Nguyen */ 31331f91b4ccSFelipe Balbi void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg) 313447a1685fSDinh Nguyen { 31359da51974SJohn Youn unsigned int ep; 313647a1685fSDinh Nguyen 31374ace06e8SMarek Szyprowski if (!hsotg->connected) 31384ace06e8SMarek Szyprowski return; 31394ace06e8SMarek Szyprowski 31404ace06e8SMarek Szyprowski hsotg->connected = 0; 31419e14d0a5SGregory Herrero hsotg->test_mode = 0; 3142c6f5c050SMian Yousaf Kaukab 3143c6f5c050SMian Yousaf Kaukab for (ep = 0; ep < hsotg->num_of_eps; ep++) { 3144c6f5c050SMian Yousaf Kaukab if (hsotg->eps_in[ep]) 3145c6f5c050SMian Yousaf Kaukab kill_all_requests(hsotg, hsotg->eps_in[ep], 3146c6f5c050SMian Yousaf Kaukab -ESHUTDOWN); 3147c6f5c050SMian Yousaf Kaukab if (hsotg->eps_out[ep]) 3148c6f5c050SMian Yousaf Kaukab kill_all_requests(hsotg, hsotg->eps_out[ep], 3149c6f5c050SMian Yousaf Kaukab -ESHUTDOWN); 3150c6f5c050SMian Yousaf Kaukab } 315147a1685fSDinh Nguyen 315247a1685fSDinh Nguyen call_gadget(hsotg, disconnect); 3153065d3931SGregory Herrero hsotg->lx_state = DWC2_L3; 3154ce2b21a4SJohn Stultz 3155ce2b21a4SJohn Stultz usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED); 315647a1685fSDinh Nguyen } 315747a1685fSDinh Nguyen 315847a1685fSDinh Nguyen /** 31591f91b4ccSFelipe Balbi * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler 316047a1685fSDinh Nguyen * @hsotg: The device state: 316147a1685fSDinh Nguyen * @periodic: True if this is a periodic FIFO interrupt 316247a1685fSDinh Nguyen */ 31631f91b4ccSFelipe Balbi static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic) 316447a1685fSDinh Nguyen { 31651f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *ep; 316647a1685fSDinh Nguyen int epno, ret; 316747a1685fSDinh Nguyen 316847a1685fSDinh Nguyen /* look through for any more data to transmit */ 316947a1685fSDinh Nguyen for (epno = 0; epno < hsotg->num_of_eps; epno++) { 3170c6f5c050SMian Yousaf Kaukab ep = index_to_ep(hsotg, epno, 1); 3171c6f5c050SMian Yousaf Kaukab 3172c6f5c050SMian Yousaf Kaukab if (!ep) 3173c6f5c050SMian Yousaf Kaukab continue; 317447a1685fSDinh Nguyen 317547a1685fSDinh Nguyen if (!ep->dir_in) 317647a1685fSDinh Nguyen continue; 317747a1685fSDinh Nguyen 317847a1685fSDinh Nguyen if ((periodic && !ep->periodic) || 317947a1685fSDinh Nguyen (!periodic && ep->periodic)) 318047a1685fSDinh Nguyen continue; 318147a1685fSDinh Nguyen 31821f91b4ccSFelipe Balbi ret = dwc2_hsotg_trytx(hsotg, ep); 318347a1685fSDinh Nguyen if (ret < 0) 318447a1685fSDinh Nguyen break; 318547a1685fSDinh Nguyen } 318647a1685fSDinh Nguyen } 318747a1685fSDinh Nguyen 318847a1685fSDinh Nguyen /* IRQ flags which will trigger a retry around the IRQ loop */ 318947a1685fSDinh Nguyen #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \ 319047a1685fSDinh Nguyen GINTSTS_PTXFEMP | \ 319147a1685fSDinh Nguyen GINTSTS_RXFLVL) 319247a1685fSDinh Nguyen 319347a1685fSDinh Nguyen /** 31941f91b4ccSFelipe Balbi * dwc2_hsotg_core_init - issue softreset to the core 319547a1685fSDinh Nguyen * @hsotg: The device state 319647a1685fSDinh Nguyen * 319747a1685fSDinh Nguyen * Issue a soft reset to the core, and await the core finishing it. 319847a1685fSDinh Nguyen */ 31991f91b4ccSFelipe Balbi void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg, 3200643cc4deSGregory Herrero bool is_usb_reset) 320147a1685fSDinh Nguyen { 32021ee6903bSGregory Herrero u32 intmsk; 3203643cc4deSGregory Herrero u32 val; 3204ecd9a7adSPrzemek Rudy u32 usbcfg; 320579c3b5bbSVahram Aharonyan u32 dcfg = 0; 3206643cc4deSGregory Herrero 32075390d438SMian Yousaf Kaukab /* Kill any ep0 requests as controller will be reinitialized */ 32085390d438SMian Yousaf Kaukab kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET); 32095390d438SMian Yousaf Kaukab 3210643cc4deSGregory Herrero if (!is_usb_reset) 32116e6360b6SJohn Stultz if (dwc2_core_reset(hsotg, true)) 321286de4895SGregory Herrero return; 321347a1685fSDinh Nguyen 321447a1685fSDinh Nguyen /* 321547a1685fSDinh Nguyen * we must now enable ep0 ready for host detection and then 321647a1685fSDinh Nguyen * set configuration. 321747a1685fSDinh Nguyen */ 321847a1685fSDinh Nguyen 3219ecd9a7adSPrzemek Rudy /* keep other bits untouched (so e.g. forced modes are not lost) */ 3220ecd9a7adSPrzemek Rudy usbcfg = dwc2_readl(hsotg->regs + GUSBCFG); 3221ecd9a7adSPrzemek Rudy usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP | 3222ca02954aSAmelie Delaunay GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK); 3223ecd9a7adSPrzemek Rudy 322479c3b5bbSVahram Aharonyan if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS && 322538e9002bSVardan Mikayelyan (hsotg->params.speed == DWC2_SPEED_PARAM_FULL || 322638e9002bSVardan Mikayelyan hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) { 322779c3b5bbSVahram Aharonyan /* FS/LS Dedicated Transceiver Interface */ 322879c3b5bbSVahram Aharonyan usbcfg |= GUSBCFG_PHYSEL; 322979c3b5bbSVahram Aharonyan } else { 323047a1685fSDinh Nguyen /* set the PLL on, remove the HNP/SRP and set the PHY */ 3231fa4a8d72SMian Yousaf Kaukab val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5; 3232ecd9a7adSPrzemek Rudy usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) | 3233ecd9a7adSPrzemek Rudy (val << GUSBCFG_USBTRDTIM_SHIFT); 323479c3b5bbSVahram Aharonyan } 3235ecd9a7adSPrzemek Rudy dwc2_writel(usbcfg, hsotg->regs + GUSBCFG); 323647a1685fSDinh Nguyen 32371f91b4ccSFelipe Balbi dwc2_hsotg_init_fifo(hsotg); 323847a1685fSDinh Nguyen 3239643cc4deSGregory Herrero if (!is_usb_reset) 324047a1685fSDinh Nguyen __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON); 324147a1685fSDinh Nguyen 324279c3b5bbSVahram Aharonyan dcfg |= DCFG_EPMISCNT(1); 324338e9002bSVardan Mikayelyan 324438e9002bSVardan Mikayelyan switch (hsotg->params.speed) { 324538e9002bSVardan Mikayelyan case DWC2_SPEED_PARAM_LOW: 324638e9002bSVardan Mikayelyan dcfg |= DCFG_DEVSPD_LS; 324738e9002bSVardan Mikayelyan break; 324838e9002bSVardan Mikayelyan case DWC2_SPEED_PARAM_FULL: 324979c3b5bbSVahram Aharonyan if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) 325079c3b5bbSVahram Aharonyan dcfg |= DCFG_DEVSPD_FS48; 325179c3b5bbSVahram Aharonyan else 325279c3b5bbSVahram Aharonyan dcfg |= DCFG_DEVSPD_FS; 325338e9002bSVardan Mikayelyan break; 325438e9002bSVardan Mikayelyan default: 325579c3b5bbSVahram Aharonyan dcfg |= DCFG_DEVSPD_HS; 325679c3b5bbSVahram Aharonyan } 325738e9002bSVardan Mikayelyan 325879c3b5bbSVahram Aharonyan dwc2_writel(dcfg, hsotg->regs + DCFG); 325947a1685fSDinh Nguyen 326047a1685fSDinh Nguyen /* Clear any pending OTG interrupts */ 326195c8bc36SAntti Seppälä dwc2_writel(0xffffffff, hsotg->regs + GOTGINT); 326247a1685fSDinh Nguyen 326347a1685fSDinh Nguyen /* Clear any pending interrupts */ 326495c8bc36SAntti Seppälä dwc2_writel(0xffffffff, hsotg->regs + GINTSTS); 32651ee6903bSGregory Herrero intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT | 326647a1685fSDinh Nguyen GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF | 32671ee6903bSGregory Herrero GINTSTS_USBRST | GINTSTS_RESETDET | 32681ee6903bSGregory Herrero GINTSTS_ENUMDONE | GINTSTS_OTGINT | 3269f4736701SVahram Aharonyan GINTSTS_USBSUSP | GINTSTS_WKUPINT; 3270f4736701SVahram Aharonyan 3271f4736701SVahram Aharonyan if (!using_desc_dma(hsotg)) 3272f4736701SVahram Aharonyan intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT; 32731ee6903bSGregory Herrero 327495832c00SJohn Youn if (!hsotg->params.external_id_pin_ctl) 32751ee6903bSGregory Herrero intmsk |= GINTSTS_CONIDSTSCHNG; 32761ee6903bSGregory Herrero 32771ee6903bSGregory Herrero dwc2_writel(intmsk, hsotg->regs + GINTMSK); 327847a1685fSDinh Nguyen 3279a5c18f11SVahram Aharonyan if (using_dma(hsotg)) { 328095c8bc36SAntti Seppälä dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN | 3281d1ac8c80SRazmik Karapetyan hsotg->params.ahbcfg, 328247a1685fSDinh Nguyen hsotg->regs + GAHBCFG); 3283a5c18f11SVahram Aharonyan 3284a5c18f11SVahram Aharonyan /* Set DDMA mode support in the core if needed */ 3285a5c18f11SVahram Aharonyan if (using_desc_dma(hsotg)) 3286a5c18f11SVahram Aharonyan __orr32(hsotg->regs + DCFG, DCFG_DESCDMA_EN); 3287a5c18f11SVahram Aharonyan 3288a5c18f11SVahram Aharonyan } else { 328995c8bc36SAntti Seppälä dwc2_writel(((hsotg->dedicated_fifos) ? 329095c8bc36SAntti Seppälä (GAHBCFG_NP_TXF_EMP_LVL | 329147a1685fSDinh Nguyen GAHBCFG_P_TXF_EMP_LVL) : 0) | 329295c8bc36SAntti Seppälä GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG); 3293a5c18f11SVahram Aharonyan } 329447a1685fSDinh Nguyen 329547a1685fSDinh Nguyen /* 329647a1685fSDinh Nguyen * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts 329747a1685fSDinh Nguyen * when we have no data to transfer. Otherwise we get being flooded by 329847a1685fSDinh Nguyen * interrupts. 329947a1685fSDinh Nguyen */ 330047a1685fSDinh Nguyen 330195c8bc36SAntti Seppälä dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ? 33026ff2e832SMian Yousaf Kaukab DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) | 330347a1685fSDinh Nguyen DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK | 3304837e9f00SVardan Mikayelyan DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK, 330547a1685fSDinh Nguyen hsotg->regs + DIEPMSK); 330647a1685fSDinh Nguyen 330747a1685fSDinh Nguyen /* 330847a1685fSDinh Nguyen * don't need XferCompl, we get that from RXFIFO in slave mode. In 33099d9a6b07SVahram Aharonyan * DMA mode we may need this and StsPhseRcvd. 331047a1685fSDinh Nguyen */ 33119d9a6b07SVahram Aharonyan dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK | 33129d9a6b07SVahram Aharonyan DOEPMSK_STSPHSERCVDMSK) : 0) | 331347a1685fSDinh Nguyen DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK | 33149d9a6b07SVahram Aharonyan DOEPMSK_SETUPMSK, 331547a1685fSDinh Nguyen hsotg->regs + DOEPMSK); 331647a1685fSDinh Nguyen 3317ec01f0b2SVahram Aharonyan /* Enable BNA interrupt for DDMA */ 3318ec01f0b2SVahram Aharonyan if (using_desc_dma(hsotg)) 3319ec01f0b2SVahram Aharonyan __orr32(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK); 3320ec01f0b2SVahram Aharonyan 332195c8bc36SAntti Seppälä dwc2_writel(0, hsotg->regs + DAINTMSK); 332247a1685fSDinh Nguyen 332347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", 332495c8bc36SAntti Seppälä dwc2_readl(hsotg->regs + DIEPCTL0), 332595c8bc36SAntti Seppälä dwc2_readl(hsotg->regs + DOEPCTL0)); 332647a1685fSDinh Nguyen 332747a1685fSDinh Nguyen /* enable in and out endpoint interrupts */ 33281f91b4ccSFelipe Balbi dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT); 332947a1685fSDinh Nguyen 333047a1685fSDinh Nguyen /* 333147a1685fSDinh Nguyen * Enable the RXFIFO when in slave mode, as this is how we collect 333247a1685fSDinh Nguyen * the data. In DMA mode, we get events from the FIFO but also 333347a1685fSDinh Nguyen * things we cannot process, so do not use it. 333447a1685fSDinh Nguyen */ 333547a1685fSDinh Nguyen if (!using_dma(hsotg)) 33361f91b4ccSFelipe Balbi dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL); 333747a1685fSDinh Nguyen 333847a1685fSDinh Nguyen /* Enable interrupts for EP0 in and out */ 33391f91b4ccSFelipe Balbi dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1); 33401f91b4ccSFelipe Balbi dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1); 334147a1685fSDinh Nguyen 3342643cc4deSGregory Herrero if (!is_usb_reset) { 334347a1685fSDinh Nguyen __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE); 334447a1685fSDinh Nguyen udelay(10); /* see openiboot */ 334547a1685fSDinh Nguyen __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE); 3346643cc4deSGregory Herrero } 334747a1685fSDinh Nguyen 334895c8bc36SAntti Seppälä dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL)); 334947a1685fSDinh Nguyen 335047a1685fSDinh Nguyen /* 335147a1685fSDinh Nguyen * DxEPCTL_USBActEp says RO in manual, but seems to be set by 335247a1685fSDinh Nguyen * writing to the EPCTL register.. 335347a1685fSDinh Nguyen */ 335447a1685fSDinh Nguyen 335547a1685fSDinh Nguyen /* set to read 1 8byte packet */ 335695c8bc36SAntti Seppälä dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | 335747a1685fSDinh Nguyen DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0); 335847a1685fSDinh Nguyen 335995c8bc36SAntti Seppälä dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | 336047a1685fSDinh Nguyen DXEPCTL_CNAK | DXEPCTL_EPENA | 336147a1685fSDinh Nguyen DXEPCTL_USBACTEP, 336247a1685fSDinh Nguyen hsotg->regs + DOEPCTL0); 336347a1685fSDinh Nguyen 336447a1685fSDinh Nguyen /* enable, but don't activate EP0in */ 336595c8bc36SAntti Seppälä dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | 336647a1685fSDinh Nguyen DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0); 336747a1685fSDinh Nguyen 336847a1685fSDinh Nguyen /* clear global NAKs */ 3369643cc4deSGregory Herrero val = DCTL_CGOUTNAK | DCTL_CGNPINNAK; 3370643cc4deSGregory Herrero if (!is_usb_reset) 3371643cc4deSGregory Herrero val |= DCTL_SFTDISCON; 3372643cc4deSGregory Herrero __orr32(hsotg->regs + DCTL, val); 337347a1685fSDinh Nguyen 337447a1685fSDinh Nguyen /* must be at-least 3ms to allow bus to see disconnect */ 337547a1685fSDinh Nguyen mdelay(3); 337647a1685fSDinh Nguyen 3377065d3931SGregory Herrero hsotg->lx_state = DWC2_L0; 3378755d7395SVardan Mikayelyan 3379755d7395SVardan Mikayelyan dwc2_hsotg_enqueue_setup(hsotg); 3380755d7395SVardan Mikayelyan 3381755d7395SVardan Mikayelyan dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", 3382755d7395SVardan Mikayelyan dwc2_readl(hsotg->regs + DIEPCTL0), 3383755d7395SVardan Mikayelyan dwc2_readl(hsotg->regs + DOEPCTL0)); 3384ad38dc5dSMarek Szyprowski } 3385ac3c81f3SMarek Szyprowski 33861f91b4ccSFelipe Balbi static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg) 3387ad38dc5dSMarek Szyprowski { 3388ad38dc5dSMarek Szyprowski /* set the soft-disconnect bit */ 3389ad38dc5dSMarek Szyprowski __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON); 3390ad38dc5dSMarek Szyprowski } 3391ad38dc5dSMarek Szyprowski 33921f91b4ccSFelipe Balbi void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) 3393ad38dc5dSMarek Szyprowski { 339447a1685fSDinh Nguyen /* remove the soft-disconnect and let's go */ 339547a1685fSDinh Nguyen __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON); 339647a1685fSDinh Nguyen } 339747a1685fSDinh Nguyen 339847a1685fSDinh Nguyen /** 3399381fc8f8SVardan Mikayelyan * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt. 3400381fc8f8SVardan Mikayelyan * @hsotg: The device state: 3401381fc8f8SVardan Mikayelyan * 3402381fc8f8SVardan Mikayelyan * This interrupt indicates one of the following conditions occurred while 3403381fc8f8SVardan Mikayelyan * transmitting an ISOC transaction. 3404381fc8f8SVardan Mikayelyan * - Corrupted IN Token for ISOC EP. 3405381fc8f8SVardan Mikayelyan * - Packet not complete in FIFO. 3406381fc8f8SVardan Mikayelyan * 3407381fc8f8SVardan Mikayelyan * The following actions will be taken: 3408381fc8f8SVardan Mikayelyan * - Determine the EP 3409381fc8f8SVardan Mikayelyan * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO 3410381fc8f8SVardan Mikayelyan */ 3411381fc8f8SVardan Mikayelyan static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg) 3412381fc8f8SVardan Mikayelyan { 3413381fc8f8SVardan Mikayelyan struct dwc2_hsotg_ep *hs_ep; 3414381fc8f8SVardan Mikayelyan u32 epctrl; 3415*1b4977c7SRazmik Karapetyan u32 daintmsk; 3416381fc8f8SVardan Mikayelyan u32 idx; 3417381fc8f8SVardan Mikayelyan 3418381fc8f8SVardan Mikayelyan dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n"); 3419381fc8f8SVardan Mikayelyan 3420*1b4977c7SRazmik Karapetyan daintmsk = dwc2_readl(hsotg->regs + DAINTMSK); 3421*1b4977c7SRazmik Karapetyan 3422381fc8f8SVardan Mikayelyan for (idx = 1; idx <= hsotg->num_of_eps; idx++) { 3423381fc8f8SVardan Mikayelyan hs_ep = hsotg->eps_in[idx]; 3424*1b4977c7SRazmik Karapetyan /* Proceed only unmasked ISOC EPs */ 3425*1b4977c7SRazmik Karapetyan if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk)) 3426*1b4977c7SRazmik Karapetyan continue; 3427*1b4977c7SRazmik Karapetyan 3428381fc8f8SVardan Mikayelyan epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx)); 3429*1b4977c7SRazmik Karapetyan if ((epctrl & DXEPCTL_EPENA) && 3430381fc8f8SVardan Mikayelyan dwc2_gadget_target_frame_elapsed(hs_ep)) { 3431381fc8f8SVardan Mikayelyan epctrl |= DXEPCTL_SNAK; 3432381fc8f8SVardan Mikayelyan epctrl |= DXEPCTL_EPDIS; 3433381fc8f8SVardan Mikayelyan dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx)); 3434381fc8f8SVardan Mikayelyan } 3435381fc8f8SVardan Mikayelyan } 3436381fc8f8SVardan Mikayelyan 3437381fc8f8SVardan Mikayelyan /* Clear interrupt */ 3438381fc8f8SVardan Mikayelyan dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS); 3439381fc8f8SVardan Mikayelyan } 3440381fc8f8SVardan Mikayelyan 3441381fc8f8SVardan Mikayelyan /** 3442381fc8f8SVardan Mikayelyan * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt 3443381fc8f8SVardan Mikayelyan * @hsotg: The device state: 3444381fc8f8SVardan Mikayelyan * 3445381fc8f8SVardan Mikayelyan * This interrupt indicates one of the following conditions occurred while 3446381fc8f8SVardan Mikayelyan * transmitting an ISOC transaction. 3447381fc8f8SVardan Mikayelyan * - Corrupted OUT Token for ISOC EP. 3448381fc8f8SVardan Mikayelyan * - Packet not complete in FIFO. 3449381fc8f8SVardan Mikayelyan * 3450381fc8f8SVardan Mikayelyan * The following actions will be taken: 3451381fc8f8SVardan Mikayelyan * - Determine the EP 3452381fc8f8SVardan Mikayelyan * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed. 3453381fc8f8SVardan Mikayelyan */ 3454381fc8f8SVardan Mikayelyan static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg) 3455381fc8f8SVardan Mikayelyan { 3456381fc8f8SVardan Mikayelyan u32 gintsts; 3457381fc8f8SVardan Mikayelyan u32 gintmsk; 3458381fc8f8SVardan Mikayelyan u32 epctrl; 3459381fc8f8SVardan Mikayelyan struct dwc2_hsotg_ep *hs_ep; 3460381fc8f8SVardan Mikayelyan int idx; 3461381fc8f8SVardan Mikayelyan 3462381fc8f8SVardan Mikayelyan dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__); 3463381fc8f8SVardan Mikayelyan 3464381fc8f8SVardan Mikayelyan for (idx = 1; idx <= hsotg->num_of_eps; idx++) { 3465381fc8f8SVardan Mikayelyan hs_ep = hsotg->eps_out[idx]; 3466381fc8f8SVardan Mikayelyan epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx)); 3467381fc8f8SVardan Mikayelyan if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous && 3468381fc8f8SVardan Mikayelyan dwc2_gadget_target_frame_elapsed(hs_ep)) { 3469381fc8f8SVardan Mikayelyan /* Unmask GOUTNAKEFF interrupt */ 3470381fc8f8SVardan Mikayelyan gintmsk = dwc2_readl(hsotg->regs + GINTMSK); 3471381fc8f8SVardan Mikayelyan gintmsk |= GINTSTS_GOUTNAKEFF; 3472381fc8f8SVardan Mikayelyan dwc2_writel(gintmsk, hsotg->regs + GINTMSK); 3473381fc8f8SVardan Mikayelyan 3474381fc8f8SVardan Mikayelyan gintsts = dwc2_readl(hsotg->regs + GINTSTS); 3475381fc8f8SVardan Mikayelyan if (!(gintsts & GINTSTS_GOUTNAKEFF)) 3476381fc8f8SVardan Mikayelyan __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK); 3477381fc8f8SVardan Mikayelyan } 3478381fc8f8SVardan Mikayelyan } 3479381fc8f8SVardan Mikayelyan 3480381fc8f8SVardan Mikayelyan /* Clear interrupt */ 3481381fc8f8SVardan Mikayelyan dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS); 3482381fc8f8SVardan Mikayelyan } 3483381fc8f8SVardan Mikayelyan 3484381fc8f8SVardan Mikayelyan /** 34851f91b4ccSFelipe Balbi * dwc2_hsotg_irq - handle device interrupt 348647a1685fSDinh Nguyen * @irq: The IRQ number triggered 348747a1685fSDinh Nguyen * @pw: The pw value when registered the handler. 348847a1685fSDinh Nguyen */ 34891f91b4ccSFelipe Balbi static irqreturn_t dwc2_hsotg_irq(int irq, void *pw) 349047a1685fSDinh Nguyen { 3491941fcce4SDinh Nguyen struct dwc2_hsotg *hsotg = pw; 349247a1685fSDinh Nguyen int retry_count = 8; 349347a1685fSDinh Nguyen u32 gintsts; 349447a1685fSDinh Nguyen u32 gintmsk; 349547a1685fSDinh Nguyen 3496ee3de8d7SVardan Mikayelyan if (!dwc2_is_device_mode(hsotg)) 3497ee3de8d7SVardan Mikayelyan return IRQ_NONE; 3498ee3de8d7SVardan Mikayelyan 349947a1685fSDinh Nguyen spin_lock(&hsotg->lock); 350047a1685fSDinh Nguyen irq_retry: 350195c8bc36SAntti Seppälä gintsts = dwc2_readl(hsotg->regs + GINTSTS); 350295c8bc36SAntti Seppälä gintmsk = dwc2_readl(hsotg->regs + GINTMSK); 350347a1685fSDinh Nguyen 350447a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n", 350547a1685fSDinh Nguyen __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count); 350647a1685fSDinh Nguyen 350747a1685fSDinh Nguyen gintsts &= gintmsk; 350847a1685fSDinh Nguyen 35098fc37b82SMian Yousaf Kaukab if (gintsts & GINTSTS_RESETDET) { 35108fc37b82SMian Yousaf Kaukab dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__); 35118fc37b82SMian Yousaf Kaukab 35128fc37b82SMian Yousaf Kaukab dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS); 35138fc37b82SMian Yousaf Kaukab 35148fc37b82SMian Yousaf Kaukab /* This event must be used only if controller is suspended */ 35158fc37b82SMian Yousaf Kaukab if (hsotg->lx_state == DWC2_L2) { 35168fc37b82SMian Yousaf Kaukab dwc2_exit_hibernation(hsotg, true); 35178fc37b82SMian Yousaf Kaukab hsotg->lx_state = DWC2_L0; 35188fc37b82SMian Yousaf Kaukab } 35198fc37b82SMian Yousaf Kaukab } 35208fc37b82SMian Yousaf Kaukab 35218fc37b82SMian Yousaf Kaukab if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) { 35228fc37b82SMian Yousaf Kaukab u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL); 35238fc37b82SMian Yousaf Kaukab u32 connected = hsotg->connected; 35248fc37b82SMian Yousaf Kaukab 35258fc37b82SMian Yousaf Kaukab dev_dbg(hsotg->dev, "%s: USBRst\n", __func__); 35268fc37b82SMian Yousaf Kaukab dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n", 35278fc37b82SMian Yousaf Kaukab dwc2_readl(hsotg->regs + GNPTXSTS)); 35288fc37b82SMian Yousaf Kaukab 35298fc37b82SMian Yousaf Kaukab dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS); 35308fc37b82SMian Yousaf Kaukab 35318fc37b82SMian Yousaf Kaukab /* Report disconnection if it is not already done. */ 35328fc37b82SMian Yousaf Kaukab dwc2_hsotg_disconnect(hsotg); 35338fc37b82SMian Yousaf Kaukab 3534307bc11fSMinas Harutyunyan /* Reset device address to zero */ 3535307bc11fSMinas Harutyunyan __bic32(hsotg->regs + DCFG, DCFG_DEVADDR_MASK); 3536307bc11fSMinas Harutyunyan 35378fc37b82SMian Yousaf Kaukab if (usb_status & GOTGCTL_BSESVLD && connected) 35388fc37b82SMian Yousaf Kaukab dwc2_hsotg_core_init_disconnected(hsotg, true); 35398fc37b82SMian Yousaf Kaukab } 35408fc37b82SMian Yousaf Kaukab 354147a1685fSDinh Nguyen if (gintsts & GINTSTS_ENUMDONE) { 354295c8bc36SAntti Seppälä dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS); 354347a1685fSDinh Nguyen 35441f91b4ccSFelipe Balbi dwc2_hsotg_irq_enumdone(hsotg); 354547a1685fSDinh Nguyen } 354647a1685fSDinh Nguyen 354747a1685fSDinh Nguyen if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) { 354895c8bc36SAntti Seppälä u32 daint = dwc2_readl(hsotg->regs + DAINT); 354995c8bc36SAntti Seppälä u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK); 355047a1685fSDinh Nguyen u32 daint_out, daint_in; 355147a1685fSDinh Nguyen int ep; 355247a1685fSDinh Nguyen 355347a1685fSDinh Nguyen daint &= daintmsk; 355447a1685fSDinh Nguyen daint_out = daint >> DAINT_OUTEP_SHIFT; 355547a1685fSDinh Nguyen daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT); 355647a1685fSDinh Nguyen 355747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint); 355847a1685fSDinh Nguyen 3559cec87f1dSMian Yousaf Kaukab for (ep = 0; ep < hsotg->num_of_eps && daint_out; 3560cec87f1dSMian Yousaf Kaukab ep++, daint_out >>= 1) { 356147a1685fSDinh Nguyen if (daint_out & 1) 35621f91b4ccSFelipe Balbi dwc2_hsotg_epint(hsotg, ep, 0); 356347a1685fSDinh Nguyen } 356447a1685fSDinh Nguyen 3565cec87f1dSMian Yousaf Kaukab for (ep = 0; ep < hsotg->num_of_eps && daint_in; 3566cec87f1dSMian Yousaf Kaukab ep++, daint_in >>= 1) { 356747a1685fSDinh Nguyen if (daint_in & 1) 35681f91b4ccSFelipe Balbi dwc2_hsotg_epint(hsotg, ep, 1); 356947a1685fSDinh Nguyen } 357047a1685fSDinh Nguyen } 357147a1685fSDinh Nguyen 357247a1685fSDinh Nguyen /* check both FIFOs */ 357347a1685fSDinh Nguyen 357447a1685fSDinh Nguyen if (gintsts & GINTSTS_NPTXFEMP) { 357547a1685fSDinh Nguyen dev_dbg(hsotg->dev, "NPTxFEmp\n"); 357647a1685fSDinh Nguyen 357747a1685fSDinh Nguyen /* 357847a1685fSDinh Nguyen * Disable the interrupt to stop it happening again 357947a1685fSDinh Nguyen * unless one of these endpoint routines decides that 358047a1685fSDinh Nguyen * it needs re-enabling 358147a1685fSDinh Nguyen */ 358247a1685fSDinh Nguyen 35831f91b4ccSFelipe Balbi dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP); 35841f91b4ccSFelipe Balbi dwc2_hsotg_irq_fifoempty(hsotg, false); 358547a1685fSDinh Nguyen } 358647a1685fSDinh Nguyen 358747a1685fSDinh Nguyen if (gintsts & GINTSTS_PTXFEMP) { 358847a1685fSDinh Nguyen dev_dbg(hsotg->dev, "PTxFEmp\n"); 358947a1685fSDinh Nguyen 359047a1685fSDinh Nguyen /* See note in GINTSTS_NPTxFEmp */ 359147a1685fSDinh Nguyen 35921f91b4ccSFelipe Balbi dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP); 35931f91b4ccSFelipe Balbi dwc2_hsotg_irq_fifoempty(hsotg, true); 359447a1685fSDinh Nguyen } 359547a1685fSDinh Nguyen 359647a1685fSDinh Nguyen if (gintsts & GINTSTS_RXFLVL) { 359747a1685fSDinh Nguyen /* 359847a1685fSDinh Nguyen * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty, 35991f91b4ccSFelipe Balbi * we need to retry dwc2_hsotg_handle_rx if this is still 360047a1685fSDinh Nguyen * set. 360147a1685fSDinh Nguyen */ 360247a1685fSDinh Nguyen 36031f91b4ccSFelipe Balbi dwc2_hsotg_handle_rx(hsotg); 360447a1685fSDinh Nguyen } 360547a1685fSDinh Nguyen 360647a1685fSDinh Nguyen if (gintsts & GINTSTS_ERLYSUSP) { 360747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n"); 360895c8bc36SAntti Seppälä dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS); 360947a1685fSDinh Nguyen } 361047a1685fSDinh Nguyen 361147a1685fSDinh Nguyen /* 361247a1685fSDinh Nguyen * these next two seem to crop-up occasionally causing the core 361347a1685fSDinh Nguyen * to shutdown the USB transfer, so try clearing them and logging 361447a1685fSDinh Nguyen * the occurrence. 361547a1685fSDinh Nguyen */ 361647a1685fSDinh Nguyen 361747a1685fSDinh Nguyen if (gintsts & GINTSTS_GOUTNAKEFF) { 3618837e9f00SVardan Mikayelyan u8 idx; 3619837e9f00SVardan Mikayelyan u32 epctrl; 3620837e9f00SVardan Mikayelyan u32 gintmsk; 3621837e9f00SVardan Mikayelyan struct dwc2_hsotg_ep *hs_ep; 362247a1685fSDinh Nguyen 3623837e9f00SVardan Mikayelyan /* Mask this interrupt */ 3624837e9f00SVardan Mikayelyan gintmsk = dwc2_readl(hsotg->regs + GINTMSK); 3625837e9f00SVardan Mikayelyan gintmsk &= ~GINTSTS_GOUTNAKEFF; 3626837e9f00SVardan Mikayelyan dwc2_writel(gintmsk, hsotg->regs + GINTMSK); 362747a1685fSDinh Nguyen 3628837e9f00SVardan Mikayelyan dev_dbg(hsotg->dev, "GOUTNakEff triggered\n"); 3629837e9f00SVardan Mikayelyan for (idx = 1; idx <= hsotg->num_of_eps; idx++) { 3630837e9f00SVardan Mikayelyan hs_ep = hsotg->eps_out[idx]; 3631837e9f00SVardan Mikayelyan epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx)); 3632837e9f00SVardan Mikayelyan 3633837e9f00SVardan Mikayelyan if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) { 3634837e9f00SVardan Mikayelyan epctrl |= DXEPCTL_SNAK; 3635837e9f00SVardan Mikayelyan epctrl |= DXEPCTL_EPDIS; 3636837e9f00SVardan Mikayelyan dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx)); 3637837e9f00SVardan Mikayelyan } 3638837e9f00SVardan Mikayelyan } 3639837e9f00SVardan Mikayelyan 3640837e9f00SVardan Mikayelyan /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */ 364147a1685fSDinh Nguyen } 364247a1685fSDinh Nguyen 364347a1685fSDinh Nguyen if (gintsts & GINTSTS_GINNAKEFF) { 364447a1685fSDinh Nguyen dev_info(hsotg->dev, "GINNakEff triggered\n"); 364547a1685fSDinh Nguyen 36463be99cd0SGregory Herrero __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK); 364747a1685fSDinh Nguyen 36481f91b4ccSFelipe Balbi dwc2_hsotg_dump(hsotg); 364947a1685fSDinh Nguyen } 365047a1685fSDinh Nguyen 3651381fc8f8SVardan Mikayelyan if (gintsts & GINTSTS_INCOMPL_SOIN) 3652381fc8f8SVardan Mikayelyan dwc2_gadget_handle_incomplete_isoc_in(hsotg); 3653ec1f9d9fSRoman Bacik 3654381fc8f8SVardan Mikayelyan if (gintsts & GINTSTS_INCOMPL_SOOUT) 3655381fc8f8SVardan Mikayelyan dwc2_gadget_handle_incomplete_isoc_out(hsotg); 3656ec1f9d9fSRoman Bacik 365747a1685fSDinh Nguyen /* 365847a1685fSDinh Nguyen * if we've had fifo events, we should try and go around the 365947a1685fSDinh Nguyen * loop again to see if there's any point in returning yet. 366047a1685fSDinh Nguyen */ 366147a1685fSDinh Nguyen 366247a1685fSDinh Nguyen if (gintsts & IRQ_RETRY_MASK && --retry_count > 0) 366347a1685fSDinh Nguyen goto irq_retry; 366447a1685fSDinh Nguyen 366547a1685fSDinh Nguyen spin_unlock(&hsotg->lock); 366647a1685fSDinh Nguyen 366747a1685fSDinh Nguyen return IRQ_HANDLED; 366847a1685fSDinh Nguyen } 366947a1685fSDinh Nguyen 3670a4f82771SVahram Aharonyan static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg, 3671a4f82771SVahram Aharonyan struct dwc2_hsotg_ep *hs_ep) 3672a4f82771SVahram Aharonyan { 3673a4f82771SVahram Aharonyan u32 epctrl_reg; 3674a4f82771SVahram Aharonyan u32 epint_reg; 3675a4f82771SVahram Aharonyan 3676a4f82771SVahram Aharonyan epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) : 3677a4f82771SVahram Aharonyan DOEPCTL(hs_ep->index); 3678a4f82771SVahram Aharonyan epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) : 3679a4f82771SVahram Aharonyan DOEPINT(hs_ep->index); 3680a4f82771SVahram Aharonyan 3681a4f82771SVahram Aharonyan dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__, 3682a4f82771SVahram Aharonyan hs_ep->name); 3683a4f82771SVahram Aharonyan 3684a4f82771SVahram Aharonyan if (hs_ep->dir_in) { 3685a4f82771SVahram Aharonyan if (hsotg->dedicated_fifos || hs_ep->periodic) { 3686a4f82771SVahram Aharonyan __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK); 3687a4f82771SVahram Aharonyan /* Wait for Nak effect */ 3688a4f82771SVahram Aharonyan if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, 3689a4f82771SVahram Aharonyan DXEPINT_INEPNAKEFF, 100)) 3690a4f82771SVahram Aharonyan dev_warn(hsotg->dev, 3691a4f82771SVahram Aharonyan "%s: timeout DIEPINT.NAKEFF\n", 3692a4f82771SVahram Aharonyan __func__); 3693a4f82771SVahram Aharonyan } else { 3694a4f82771SVahram Aharonyan __orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK); 3695a4f82771SVahram Aharonyan /* Wait for Nak effect */ 3696a4f82771SVahram Aharonyan if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, 3697a4f82771SVahram Aharonyan GINTSTS_GINNAKEFF, 100)) 3698a4f82771SVahram Aharonyan dev_warn(hsotg->dev, 3699a4f82771SVahram Aharonyan "%s: timeout GINTSTS.GINNAKEFF\n", 3700a4f82771SVahram Aharonyan __func__); 3701a4f82771SVahram Aharonyan } 3702a4f82771SVahram Aharonyan } else { 3703a4f82771SVahram Aharonyan if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF)) 3704a4f82771SVahram Aharonyan __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK); 3705a4f82771SVahram Aharonyan 3706a4f82771SVahram Aharonyan /* Wait for global nak to take effect */ 3707a4f82771SVahram Aharonyan if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, 3708a4f82771SVahram Aharonyan GINTSTS_GOUTNAKEFF, 100)) 3709a4f82771SVahram Aharonyan dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n", 3710a4f82771SVahram Aharonyan __func__); 3711a4f82771SVahram Aharonyan } 3712a4f82771SVahram Aharonyan 3713a4f82771SVahram Aharonyan /* Disable ep */ 3714a4f82771SVahram Aharonyan __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK); 3715a4f82771SVahram Aharonyan 3716a4f82771SVahram Aharonyan /* Wait for ep to be disabled */ 3717a4f82771SVahram Aharonyan if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100)) 3718a4f82771SVahram Aharonyan dev_warn(hsotg->dev, 3719a4f82771SVahram Aharonyan "%s: timeout DOEPCTL.EPDisable\n", __func__); 3720a4f82771SVahram Aharonyan 3721a4f82771SVahram Aharonyan /* Clear EPDISBLD interrupt */ 3722a4f82771SVahram Aharonyan __orr32(hsotg->regs + epint_reg, DXEPINT_EPDISBLD); 3723a4f82771SVahram Aharonyan 3724a4f82771SVahram Aharonyan if (hs_ep->dir_in) { 3725a4f82771SVahram Aharonyan unsigned short fifo_index; 3726a4f82771SVahram Aharonyan 3727a4f82771SVahram Aharonyan if (hsotg->dedicated_fifos || hs_ep->periodic) 3728a4f82771SVahram Aharonyan fifo_index = hs_ep->fifo_index; 3729a4f82771SVahram Aharonyan else 3730a4f82771SVahram Aharonyan fifo_index = 0; 3731a4f82771SVahram Aharonyan 3732a4f82771SVahram Aharonyan /* Flush TX FIFO */ 3733a4f82771SVahram Aharonyan dwc2_flush_tx_fifo(hsotg, fifo_index); 3734a4f82771SVahram Aharonyan 3735a4f82771SVahram Aharonyan /* Clear Global In NP NAK in Shared FIFO for non periodic ep */ 3736a4f82771SVahram Aharonyan if (!hsotg->dedicated_fifos && !hs_ep->periodic) 3737a4f82771SVahram Aharonyan __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK); 3738a4f82771SVahram Aharonyan 3739a4f82771SVahram Aharonyan } else { 3740a4f82771SVahram Aharonyan /* Remove global NAKs */ 3741a4f82771SVahram Aharonyan __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK); 3742a4f82771SVahram Aharonyan } 3743a4f82771SVahram Aharonyan } 3744a4f82771SVahram Aharonyan 374547a1685fSDinh Nguyen /** 37461f91b4ccSFelipe Balbi * dwc2_hsotg_ep_enable - enable the given endpoint 374747a1685fSDinh Nguyen * @ep: The USB endpint to configure 374847a1685fSDinh Nguyen * @desc: The USB endpoint descriptor to configure with. 374947a1685fSDinh Nguyen * 375047a1685fSDinh Nguyen * This is called from the USB gadget code's usb_ep_enable(). 375147a1685fSDinh Nguyen */ 37521f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_enable(struct usb_ep *ep, 375347a1685fSDinh Nguyen const struct usb_endpoint_descriptor *desc) 375447a1685fSDinh Nguyen { 37551f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = our_ep(ep); 3756941fcce4SDinh Nguyen struct dwc2_hsotg *hsotg = hs_ep->parent; 375747a1685fSDinh Nguyen unsigned long flags; 3758ca4c55adSMian Yousaf Kaukab unsigned int index = hs_ep->index; 375947a1685fSDinh Nguyen u32 epctrl_reg; 376047a1685fSDinh Nguyen u32 epctrl; 376147a1685fSDinh Nguyen u32 mps; 3762ee2c40deSVardan Mikayelyan u32 mc; 3763837e9f00SVardan Mikayelyan u32 mask; 3764ca4c55adSMian Yousaf Kaukab unsigned int dir_in; 3765ca4c55adSMian Yousaf Kaukab unsigned int i, val, size; 376647a1685fSDinh Nguyen int ret = 0; 376747a1685fSDinh Nguyen 376847a1685fSDinh Nguyen dev_dbg(hsotg->dev, 376947a1685fSDinh Nguyen "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n", 377047a1685fSDinh Nguyen __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes, 377147a1685fSDinh Nguyen desc->wMaxPacketSize, desc->bInterval); 377247a1685fSDinh Nguyen 377347a1685fSDinh Nguyen /* not to be called for EP0 */ 37748c3d6092SVahram Aharonyan if (index == 0) { 37758c3d6092SVahram Aharonyan dev_err(hsotg->dev, "%s: called for EP 0\n", __func__); 37768c3d6092SVahram Aharonyan return -EINVAL; 37778c3d6092SVahram Aharonyan } 377847a1685fSDinh Nguyen 377947a1685fSDinh Nguyen dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0; 378047a1685fSDinh Nguyen if (dir_in != hs_ep->dir_in) { 378147a1685fSDinh Nguyen dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__); 378247a1685fSDinh Nguyen return -EINVAL; 378347a1685fSDinh Nguyen } 378447a1685fSDinh Nguyen 378547a1685fSDinh Nguyen mps = usb_endpoint_maxp(desc); 3786ee2c40deSVardan Mikayelyan mc = usb_endpoint_maxp_mult(desc); 378747a1685fSDinh Nguyen 37881f91b4ccSFelipe Balbi /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */ 378947a1685fSDinh Nguyen 379047a1685fSDinh Nguyen epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); 379195c8bc36SAntti Seppälä epctrl = dwc2_readl(hsotg->regs + epctrl_reg); 379247a1685fSDinh Nguyen 379347a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n", 379447a1685fSDinh Nguyen __func__, epctrl, epctrl_reg); 379547a1685fSDinh Nguyen 37965f54c54bSVahram Aharonyan /* Allocate DMA descriptor chain for non-ctrl endpoints */ 37979383e084SVardan Mikayelyan if (using_desc_dma(hsotg) && !hs_ep->desc_list) { 37989383e084SVardan Mikayelyan hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev, 37995f54c54bSVahram Aharonyan MAX_DMA_DESC_NUM_GENERIC * 38005f54c54bSVahram Aharonyan sizeof(struct dwc2_dma_desc), 380186e881e7SMarek Szyprowski &hs_ep->desc_list_dma, GFP_ATOMIC); 38025f54c54bSVahram Aharonyan if (!hs_ep->desc_list) { 38035f54c54bSVahram Aharonyan ret = -ENOMEM; 38045f54c54bSVahram Aharonyan goto error2; 38055f54c54bSVahram Aharonyan } 38065f54c54bSVahram Aharonyan } 38075f54c54bSVahram Aharonyan 380847a1685fSDinh Nguyen spin_lock_irqsave(&hsotg->lock, flags); 380947a1685fSDinh Nguyen 381047a1685fSDinh Nguyen epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK); 381147a1685fSDinh Nguyen epctrl |= DXEPCTL_MPS(mps); 381247a1685fSDinh Nguyen 381347a1685fSDinh Nguyen /* 381447a1685fSDinh Nguyen * mark the endpoint as active, otherwise the core may ignore 381547a1685fSDinh Nguyen * transactions entirely for this endpoint 381647a1685fSDinh Nguyen */ 381747a1685fSDinh Nguyen epctrl |= DXEPCTL_USBACTEP; 381847a1685fSDinh Nguyen 381947a1685fSDinh Nguyen /* update the endpoint state */ 3820ee2c40deSVardan Mikayelyan dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in); 382147a1685fSDinh Nguyen 382247a1685fSDinh Nguyen /* default, set to non-periodic */ 382347a1685fSDinh Nguyen hs_ep->isochronous = 0; 382447a1685fSDinh Nguyen hs_ep->periodic = 0; 382547a1685fSDinh Nguyen hs_ep->halted = 0; 382647a1685fSDinh Nguyen hs_ep->interval = desc->bInterval; 382747a1685fSDinh Nguyen 382847a1685fSDinh Nguyen switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { 382947a1685fSDinh Nguyen case USB_ENDPOINT_XFER_ISOC: 383047a1685fSDinh Nguyen epctrl |= DXEPCTL_EPTYPE_ISO; 383147a1685fSDinh Nguyen epctrl |= DXEPCTL_SETEVENFR; 383247a1685fSDinh Nguyen hs_ep->isochronous = 1; 3833142bd33fSVardan Mikayelyan hs_ep->interval = 1 << (desc->bInterval - 1); 3834837e9f00SVardan Mikayelyan hs_ep->target_frame = TARGET_FRAME_INITIAL; 3835ab7d2192SVahram Aharonyan hs_ep->isoc_chain_num = 0; 3836ab7d2192SVahram Aharonyan hs_ep->next_desc = 0; 3837837e9f00SVardan Mikayelyan if (dir_in) { 383847a1685fSDinh Nguyen hs_ep->periodic = 1; 3839837e9f00SVardan Mikayelyan mask = dwc2_readl(hsotg->regs + DIEPMSK); 3840837e9f00SVardan Mikayelyan mask |= DIEPMSK_NAKMSK; 3841837e9f00SVardan Mikayelyan dwc2_writel(mask, hsotg->regs + DIEPMSK); 3842837e9f00SVardan Mikayelyan } else { 3843837e9f00SVardan Mikayelyan mask = dwc2_readl(hsotg->regs + DOEPMSK); 3844837e9f00SVardan Mikayelyan mask |= DOEPMSK_OUTTKNEPDISMSK; 3845837e9f00SVardan Mikayelyan dwc2_writel(mask, hsotg->regs + DOEPMSK); 3846837e9f00SVardan Mikayelyan } 384747a1685fSDinh Nguyen break; 384847a1685fSDinh Nguyen 384947a1685fSDinh Nguyen case USB_ENDPOINT_XFER_BULK: 385047a1685fSDinh Nguyen epctrl |= DXEPCTL_EPTYPE_BULK; 385147a1685fSDinh Nguyen break; 385247a1685fSDinh Nguyen 385347a1685fSDinh Nguyen case USB_ENDPOINT_XFER_INT: 3854b203d0a2SRobert Baldyga if (dir_in) 385547a1685fSDinh Nguyen hs_ep->periodic = 1; 385647a1685fSDinh Nguyen 3857142bd33fSVardan Mikayelyan if (hsotg->gadget.speed == USB_SPEED_HIGH) 3858142bd33fSVardan Mikayelyan hs_ep->interval = 1 << (desc->bInterval - 1); 3859142bd33fSVardan Mikayelyan 386047a1685fSDinh Nguyen epctrl |= DXEPCTL_EPTYPE_INTERRUPT; 386147a1685fSDinh Nguyen break; 386247a1685fSDinh Nguyen 386347a1685fSDinh Nguyen case USB_ENDPOINT_XFER_CONTROL: 386447a1685fSDinh Nguyen epctrl |= DXEPCTL_EPTYPE_CONTROL; 386547a1685fSDinh Nguyen break; 386647a1685fSDinh Nguyen } 386747a1685fSDinh Nguyen 386847a1685fSDinh Nguyen /* 386947a1685fSDinh Nguyen * if the hardware has dedicated fifos, we must give each IN EP 387047a1685fSDinh Nguyen * a unique tx-fifo even if it is non-periodic. 387147a1685fSDinh Nguyen */ 387221f3bb52SRobert Baldyga if (dir_in && hsotg->dedicated_fifos) { 3873ca4c55adSMian Yousaf Kaukab u32 fifo_index = 0; 3874ca4c55adSMian Yousaf Kaukab u32 fifo_size = UINT_MAX; 38759da51974SJohn Youn 3876b203d0a2SRobert Baldyga size = hs_ep->ep.maxpacket * hs_ep->mc; 38775f2196bdSMian Yousaf Kaukab for (i = 1; i < hsotg->num_of_eps; ++i) { 3878b203d0a2SRobert Baldyga if (hsotg->fifo_map & (1 << i)) 3879b203d0a2SRobert Baldyga continue; 388095c8bc36SAntti Seppälä val = dwc2_readl(hsotg->regs + DPTXFSIZN(i)); 3881b203d0a2SRobert Baldyga val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4; 3882b203d0a2SRobert Baldyga if (val < size) 3883b203d0a2SRobert Baldyga continue; 3884ca4c55adSMian Yousaf Kaukab /* Search for smallest acceptable fifo */ 3885ca4c55adSMian Yousaf Kaukab if (val < fifo_size) { 3886ca4c55adSMian Yousaf Kaukab fifo_size = val; 3887ca4c55adSMian Yousaf Kaukab fifo_index = i; 3888b203d0a2SRobert Baldyga } 3889ca4c55adSMian Yousaf Kaukab } 3890ca4c55adSMian Yousaf Kaukab if (!fifo_index) { 38915f2196bdSMian Yousaf Kaukab dev_err(hsotg->dev, 38925f2196bdSMian Yousaf Kaukab "%s: No suitable fifo found\n", __func__); 3893b585a48bSSudip Mukherjee ret = -ENOMEM; 38945f54c54bSVahram Aharonyan goto error1; 3895b585a48bSSudip Mukherjee } 3896ca4c55adSMian Yousaf Kaukab hsotg->fifo_map |= 1 << fifo_index; 3897ca4c55adSMian Yousaf Kaukab epctrl |= DXEPCTL_TXFNUM(fifo_index); 3898ca4c55adSMian Yousaf Kaukab hs_ep->fifo_index = fifo_index; 3899ca4c55adSMian Yousaf Kaukab hs_ep->fifo_size = fifo_size; 3900b203d0a2SRobert Baldyga } 390147a1685fSDinh Nguyen 390247a1685fSDinh Nguyen /* for non control endpoints, set PID to D0 */ 3903837e9f00SVardan Mikayelyan if (index && !hs_ep->isochronous) 390447a1685fSDinh Nguyen epctrl |= DXEPCTL_SETD0PID; 390547a1685fSDinh Nguyen 390647a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n", 390747a1685fSDinh Nguyen __func__, epctrl); 390847a1685fSDinh Nguyen 390995c8bc36SAntti Seppälä dwc2_writel(epctrl, hsotg->regs + epctrl_reg); 391047a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n", 391195c8bc36SAntti Seppälä __func__, dwc2_readl(hsotg->regs + epctrl_reg)); 391247a1685fSDinh Nguyen 391347a1685fSDinh Nguyen /* enable the endpoint interrupt */ 39141f91b4ccSFelipe Balbi dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1); 391547a1685fSDinh Nguyen 39165f54c54bSVahram Aharonyan error1: 391747a1685fSDinh Nguyen spin_unlock_irqrestore(&hsotg->lock, flags); 39185f54c54bSVahram Aharonyan 39195f54c54bSVahram Aharonyan error2: 39205f54c54bSVahram Aharonyan if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) { 39219383e084SVardan Mikayelyan dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC * 39225f54c54bSVahram Aharonyan sizeof(struct dwc2_dma_desc), 39235f54c54bSVahram Aharonyan hs_ep->desc_list, hs_ep->desc_list_dma); 39245f54c54bSVahram Aharonyan hs_ep->desc_list = NULL; 39255f54c54bSVahram Aharonyan } 39265f54c54bSVahram Aharonyan 392747a1685fSDinh Nguyen return ret; 392847a1685fSDinh Nguyen } 392947a1685fSDinh Nguyen 393047a1685fSDinh Nguyen /** 39311f91b4ccSFelipe Balbi * dwc2_hsotg_ep_disable - disable given endpoint 393247a1685fSDinh Nguyen * @ep: The endpoint to disable. 393347a1685fSDinh Nguyen */ 39341f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_disable(struct usb_ep *ep) 393547a1685fSDinh Nguyen { 39361f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = our_ep(ep); 3937941fcce4SDinh Nguyen struct dwc2_hsotg *hsotg = hs_ep->parent; 393847a1685fSDinh Nguyen int dir_in = hs_ep->dir_in; 393947a1685fSDinh Nguyen int index = hs_ep->index; 394047a1685fSDinh Nguyen unsigned long flags; 394147a1685fSDinh Nguyen u32 epctrl_reg; 394247a1685fSDinh Nguyen u32 ctrl; 394347a1685fSDinh Nguyen 39441e011293SMarek Szyprowski dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep); 394547a1685fSDinh Nguyen 3946c6f5c050SMian Yousaf Kaukab if (ep == &hsotg->eps_out[0]->ep) { 394747a1685fSDinh Nguyen dev_err(hsotg->dev, "%s: called for ep0\n", __func__); 394847a1685fSDinh Nguyen return -EINVAL; 394947a1685fSDinh Nguyen } 395047a1685fSDinh Nguyen 39519b481092SJohn Stultz if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { 39529b481092SJohn Stultz dev_err(hsotg->dev, "%s: called in host mode?\n", __func__); 39539b481092SJohn Stultz return -EINVAL; 39549b481092SJohn Stultz } 39559b481092SJohn Stultz 395647a1685fSDinh Nguyen epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); 395747a1685fSDinh Nguyen 395847a1685fSDinh Nguyen spin_lock_irqsave(&hsotg->lock, flags); 395947a1685fSDinh Nguyen 396095c8bc36SAntti Seppälä ctrl = dwc2_readl(hsotg->regs + epctrl_reg); 3961a4f82771SVahram Aharonyan 3962a4f82771SVahram Aharonyan if (ctrl & DXEPCTL_EPENA) 3963a4f82771SVahram Aharonyan dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep); 3964a4f82771SVahram Aharonyan 396547a1685fSDinh Nguyen ctrl &= ~DXEPCTL_EPENA; 396647a1685fSDinh Nguyen ctrl &= ~DXEPCTL_USBACTEP; 396747a1685fSDinh Nguyen ctrl |= DXEPCTL_SNAK; 396847a1685fSDinh Nguyen 396947a1685fSDinh Nguyen dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); 397095c8bc36SAntti Seppälä dwc2_writel(ctrl, hsotg->regs + epctrl_reg); 397147a1685fSDinh Nguyen 397247a1685fSDinh Nguyen /* disable endpoint interrupts */ 39731f91b4ccSFelipe Balbi dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0); 397447a1685fSDinh Nguyen 39751141ea01SMian Yousaf Kaukab /* terminate all requests with shutdown */ 39761141ea01SMian Yousaf Kaukab kill_all_requests(hsotg, hs_ep, -ESHUTDOWN); 39771141ea01SMian Yousaf Kaukab 39781c07b20eSRobert Baldyga hsotg->fifo_map &= ~(1 << hs_ep->fifo_index); 39791c07b20eSRobert Baldyga hs_ep->fifo_index = 0; 39801c07b20eSRobert Baldyga hs_ep->fifo_size = 0; 39811c07b20eSRobert Baldyga 398247a1685fSDinh Nguyen spin_unlock_irqrestore(&hsotg->lock, flags); 398347a1685fSDinh Nguyen return 0; 398447a1685fSDinh Nguyen } 398547a1685fSDinh Nguyen 398647a1685fSDinh Nguyen /** 398747a1685fSDinh Nguyen * on_list - check request is on the given endpoint 398847a1685fSDinh Nguyen * @ep: The endpoint to check. 398947a1685fSDinh Nguyen * @test: The request to test if it is on the endpoint. 399047a1685fSDinh Nguyen */ 39911f91b4ccSFelipe Balbi static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test) 399247a1685fSDinh Nguyen { 39931f91b4ccSFelipe Balbi struct dwc2_hsotg_req *req, *treq; 399447a1685fSDinh Nguyen 399547a1685fSDinh Nguyen list_for_each_entry_safe(req, treq, &ep->queue, queue) { 399647a1685fSDinh Nguyen if (req == test) 399747a1685fSDinh Nguyen return true; 399847a1685fSDinh Nguyen } 399947a1685fSDinh Nguyen 400047a1685fSDinh Nguyen return false; 400147a1685fSDinh Nguyen } 400247a1685fSDinh Nguyen 400347a1685fSDinh Nguyen /** 40041f91b4ccSFelipe Balbi * dwc2_hsotg_ep_dequeue - dequeue given endpoint 400547a1685fSDinh Nguyen * @ep: The endpoint to dequeue. 400647a1685fSDinh Nguyen * @req: The request to be removed from a queue. 400747a1685fSDinh Nguyen */ 40081f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req) 400947a1685fSDinh Nguyen { 40101f91b4ccSFelipe Balbi struct dwc2_hsotg_req *hs_req = our_req(req); 40111f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = our_ep(ep); 4012941fcce4SDinh Nguyen struct dwc2_hsotg *hs = hs_ep->parent; 401347a1685fSDinh Nguyen unsigned long flags; 401447a1685fSDinh Nguyen 40151e011293SMarek Szyprowski dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req); 401647a1685fSDinh Nguyen 401747a1685fSDinh Nguyen spin_lock_irqsave(&hs->lock, flags); 401847a1685fSDinh Nguyen 401947a1685fSDinh Nguyen if (!on_list(hs_ep, hs_req)) { 402047a1685fSDinh Nguyen spin_unlock_irqrestore(&hs->lock, flags); 402147a1685fSDinh Nguyen return -EINVAL; 402247a1685fSDinh Nguyen } 402347a1685fSDinh Nguyen 4024c524dd5fSMian Yousaf Kaukab /* Dequeue already started request */ 4025c524dd5fSMian Yousaf Kaukab if (req == &hs_ep->req->req) 4026c524dd5fSMian Yousaf Kaukab dwc2_hsotg_ep_stop_xfr(hs, hs_ep); 4027c524dd5fSMian Yousaf Kaukab 40281f91b4ccSFelipe Balbi dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET); 402947a1685fSDinh Nguyen spin_unlock_irqrestore(&hs->lock, flags); 403047a1685fSDinh Nguyen 403147a1685fSDinh Nguyen return 0; 403247a1685fSDinh Nguyen } 403347a1685fSDinh Nguyen 403447a1685fSDinh Nguyen /** 40351f91b4ccSFelipe Balbi * dwc2_hsotg_ep_sethalt - set halt on a given endpoint 403647a1685fSDinh Nguyen * @ep: The endpoint to set halt. 403747a1685fSDinh Nguyen * @value: Set or unset the halt. 403851da43b5SVahram Aharonyan * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if 403951da43b5SVahram Aharonyan * the endpoint is busy processing requests. 404051da43b5SVahram Aharonyan * 404151da43b5SVahram Aharonyan * We need to stall the endpoint immediately if request comes from set_feature 404251da43b5SVahram Aharonyan * protocol command handler. 404347a1685fSDinh Nguyen */ 404451da43b5SVahram Aharonyan static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now) 404547a1685fSDinh Nguyen { 40461f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = our_ep(ep); 4047941fcce4SDinh Nguyen struct dwc2_hsotg *hs = hs_ep->parent; 404847a1685fSDinh Nguyen int index = hs_ep->index; 404947a1685fSDinh Nguyen u32 epreg; 405047a1685fSDinh Nguyen u32 epctl; 405147a1685fSDinh Nguyen u32 xfertype; 405247a1685fSDinh Nguyen 405347a1685fSDinh Nguyen dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value); 405447a1685fSDinh Nguyen 405547a1685fSDinh Nguyen if (index == 0) { 405647a1685fSDinh Nguyen if (value) 40571f91b4ccSFelipe Balbi dwc2_hsotg_stall_ep0(hs); 405847a1685fSDinh Nguyen else 405947a1685fSDinh Nguyen dev_warn(hs->dev, 406047a1685fSDinh Nguyen "%s: can't clear halt on ep0\n", __func__); 406147a1685fSDinh Nguyen return 0; 406247a1685fSDinh Nguyen } 406347a1685fSDinh Nguyen 406415186f10SVahram Aharonyan if (hs_ep->isochronous) { 406515186f10SVahram Aharonyan dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name); 406615186f10SVahram Aharonyan return -EINVAL; 406715186f10SVahram Aharonyan } 406815186f10SVahram Aharonyan 406951da43b5SVahram Aharonyan if (!now && value && !list_empty(&hs_ep->queue)) { 407051da43b5SVahram Aharonyan dev_dbg(hs->dev, "%s request is pending, cannot halt\n", 407151da43b5SVahram Aharonyan ep->name); 407251da43b5SVahram Aharonyan return -EAGAIN; 407351da43b5SVahram Aharonyan } 407451da43b5SVahram Aharonyan 4075c6f5c050SMian Yousaf Kaukab if (hs_ep->dir_in) { 407647a1685fSDinh Nguyen epreg = DIEPCTL(index); 407795c8bc36SAntti Seppälä epctl = dwc2_readl(hs->regs + epreg); 407847a1685fSDinh Nguyen 407947a1685fSDinh Nguyen if (value) { 40805a350d53SFelipe Balbi epctl |= DXEPCTL_STALL | DXEPCTL_SNAK; 408147a1685fSDinh Nguyen if (epctl & DXEPCTL_EPENA) 408247a1685fSDinh Nguyen epctl |= DXEPCTL_EPDIS; 408347a1685fSDinh Nguyen } else { 408447a1685fSDinh Nguyen epctl &= ~DXEPCTL_STALL; 408547a1685fSDinh Nguyen xfertype = epctl & DXEPCTL_EPTYPE_MASK; 408647a1685fSDinh Nguyen if (xfertype == DXEPCTL_EPTYPE_BULK || 408747a1685fSDinh Nguyen xfertype == DXEPCTL_EPTYPE_INTERRUPT) 408847a1685fSDinh Nguyen epctl |= DXEPCTL_SETD0PID; 408947a1685fSDinh Nguyen } 409095c8bc36SAntti Seppälä dwc2_writel(epctl, hs->regs + epreg); 4091c6f5c050SMian Yousaf Kaukab } else { 409247a1685fSDinh Nguyen epreg = DOEPCTL(index); 409395c8bc36SAntti Seppälä epctl = dwc2_readl(hs->regs + epreg); 409447a1685fSDinh Nguyen 409534c0887fSJohn Youn if (value) { 409647a1685fSDinh Nguyen epctl |= DXEPCTL_STALL; 409734c0887fSJohn Youn } else { 409847a1685fSDinh Nguyen epctl &= ~DXEPCTL_STALL; 409947a1685fSDinh Nguyen xfertype = epctl & DXEPCTL_EPTYPE_MASK; 410047a1685fSDinh Nguyen if (xfertype == DXEPCTL_EPTYPE_BULK || 410147a1685fSDinh Nguyen xfertype == DXEPCTL_EPTYPE_INTERRUPT) 410247a1685fSDinh Nguyen epctl |= DXEPCTL_SETD0PID; 410347a1685fSDinh Nguyen } 410495c8bc36SAntti Seppälä dwc2_writel(epctl, hs->regs + epreg); 4105c6f5c050SMian Yousaf Kaukab } 410647a1685fSDinh Nguyen 410747a1685fSDinh Nguyen hs_ep->halted = value; 410847a1685fSDinh Nguyen 410947a1685fSDinh Nguyen return 0; 411047a1685fSDinh Nguyen } 411147a1685fSDinh Nguyen 411247a1685fSDinh Nguyen /** 41131f91b4ccSFelipe Balbi * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held 411447a1685fSDinh Nguyen * @ep: The endpoint to set halt. 411547a1685fSDinh Nguyen * @value: Set or unset the halt. 411647a1685fSDinh Nguyen */ 41171f91b4ccSFelipe Balbi static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value) 411847a1685fSDinh Nguyen { 41191f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep = our_ep(ep); 4120941fcce4SDinh Nguyen struct dwc2_hsotg *hs = hs_ep->parent; 412147a1685fSDinh Nguyen unsigned long flags = 0; 412247a1685fSDinh Nguyen int ret = 0; 412347a1685fSDinh Nguyen 412447a1685fSDinh Nguyen spin_lock_irqsave(&hs->lock, flags); 412551da43b5SVahram Aharonyan ret = dwc2_hsotg_ep_sethalt(ep, value, false); 412647a1685fSDinh Nguyen spin_unlock_irqrestore(&hs->lock, flags); 412747a1685fSDinh Nguyen 412847a1685fSDinh Nguyen return ret; 412947a1685fSDinh Nguyen } 413047a1685fSDinh Nguyen 4131ebce561aSBhumika Goyal static const struct usb_ep_ops dwc2_hsotg_ep_ops = { 41321f91b4ccSFelipe Balbi .enable = dwc2_hsotg_ep_enable, 41331f91b4ccSFelipe Balbi .disable = dwc2_hsotg_ep_disable, 41341f91b4ccSFelipe Balbi .alloc_request = dwc2_hsotg_ep_alloc_request, 41351f91b4ccSFelipe Balbi .free_request = dwc2_hsotg_ep_free_request, 41361f91b4ccSFelipe Balbi .queue = dwc2_hsotg_ep_queue_lock, 41371f91b4ccSFelipe Balbi .dequeue = dwc2_hsotg_ep_dequeue, 41381f91b4ccSFelipe Balbi .set_halt = dwc2_hsotg_ep_sethalt_lock, 413947a1685fSDinh Nguyen /* note, don't believe we have any call for the fifo routines */ 414047a1685fSDinh Nguyen }; 414147a1685fSDinh Nguyen 414247a1685fSDinh Nguyen /** 41439da51974SJohn Youn * dwc2_hsotg_init - initialize the usb core 414447a1685fSDinh Nguyen * @hsotg: The driver state 414547a1685fSDinh Nguyen */ 41461f91b4ccSFelipe Balbi static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg) 414747a1685fSDinh Nguyen { 4148fa4a8d72SMian Yousaf Kaukab u32 trdtim; 4149ecd9a7adSPrzemek Rudy u32 usbcfg; 415047a1685fSDinh Nguyen /* unmask subset of endpoint interrupts */ 415147a1685fSDinh Nguyen 415295c8bc36SAntti Seppälä dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK | 415347a1685fSDinh Nguyen DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK, 415447a1685fSDinh Nguyen hsotg->regs + DIEPMSK); 415547a1685fSDinh Nguyen 415695c8bc36SAntti Seppälä dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK | 415747a1685fSDinh Nguyen DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK, 415847a1685fSDinh Nguyen hsotg->regs + DOEPMSK); 415947a1685fSDinh Nguyen 416095c8bc36SAntti Seppälä dwc2_writel(0, hsotg->regs + DAINTMSK); 416147a1685fSDinh Nguyen 416247a1685fSDinh Nguyen /* Be in disconnected state until gadget is registered */ 416347a1685fSDinh Nguyen __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON); 416447a1685fSDinh Nguyen 416547a1685fSDinh Nguyen /* setup fifos */ 416647a1685fSDinh Nguyen 416747a1685fSDinh Nguyen dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", 416895c8bc36SAntti Seppälä dwc2_readl(hsotg->regs + GRXFSIZ), 416995c8bc36SAntti Seppälä dwc2_readl(hsotg->regs + GNPTXFSIZ)); 417047a1685fSDinh Nguyen 41711f91b4ccSFelipe Balbi dwc2_hsotg_init_fifo(hsotg); 417247a1685fSDinh Nguyen 4173ecd9a7adSPrzemek Rudy /* keep other bits untouched (so e.g. forced modes are not lost) */ 4174ecd9a7adSPrzemek Rudy usbcfg = dwc2_readl(hsotg->regs + GUSBCFG); 4175ecd9a7adSPrzemek Rudy usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP | 4176ca02954aSAmelie Delaunay GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK); 4177ecd9a7adSPrzemek Rudy 417847a1685fSDinh Nguyen /* set the PLL on, remove the HNP/SRP and set the PHY */ 4179fa4a8d72SMian Yousaf Kaukab trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5; 4180ecd9a7adSPrzemek Rudy usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) | 4181ecd9a7adSPrzemek Rudy (trdtim << GUSBCFG_USBTRDTIM_SHIFT); 4182ecd9a7adSPrzemek Rudy dwc2_writel(usbcfg, hsotg->regs + GUSBCFG); 418347a1685fSDinh Nguyen 4184f5090044SGregory Herrero if (using_dma(hsotg)) 4185f5090044SGregory Herrero __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN); 418647a1685fSDinh Nguyen } 418747a1685fSDinh Nguyen 418847a1685fSDinh Nguyen /** 41891f91b4ccSFelipe Balbi * dwc2_hsotg_udc_start - prepare the udc for work 419047a1685fSDinh Nguyen * @gadget: The usb gadget state 419147a1685fSDinh Nguyen * @driver: The usb gadget driver 419247a1685fSDinh Nguyen * 419347a1685fSDinh Nguyen * Perform initialization to prepare udc device and driver 419447a1685fSDinh Nguyen * to work. 419547a1685fSDinh Nguyen */ 41961f91b4ccSFelipe Balbi static int dwc2_hsotg_udc_start(struct usb_gadget *gadget, 419747a1685fSDinh Nguyen struct usb_gadget_driver *driver) 419847a1685fSDinh Nguyen { 4199941fcce4SDinh Nguyen struct dwc2_hsotg *hsotg = to_hsotg(gadget); 42005b9451f8SMarek Szyprowski unsigned long flags; 420147a1685fSDinh Nguyen int ret; 420247a1685fSDinh Nguyen 420347a1685fSDinh Nguyen if (!hsotg) { 420447a1685fSDinh Nguyen pr_err("%s: called with no device\n", __func__); 420547a1685fSDinh Nguyen return -ENODEV; 420647a1685fSDinh Nguyen } 420747a1685fSDinh Nguyen 420847a1685fSDinh Nguyen if (!driver) { 420947a1685fSDinh Nguyen dev_err(hsotg->dev, "%s: no driver\n", __func__); 421047a1685fSDinh Nguyen return -EINVAL; 421147a1685fSDinh Nguyen } 421247a1685fSDinh Nguyen 421347a1685fSDinh Nguyen if (driver->max_speed < USB_SPEED_FULL) 421447a1685fSDinh Nguyen dev_err(hsotg->dev, "%s: bad speed\n", __func__); 421547a1685fSDinh Nguyen 421647a1685fSDinh Nguyen if (!driver->setup) { 421747a1685fSDinh Nguyen dev_err(hsotg->dev, "%s: missing entry points\n", __func__); 421847a1685fSDinh Nguyen return -EINVAL; 421947a1685fSDinh Nguyen } 422047a1685fSDinh Nguyen 422147a1685fSDinh Nguyen WARN_ON(hsotg->driver); 422247a1685fSDinh Nguyen 422347a1685fSDinh Nguyen driver->driver.bus = NULL; 422447a1685fSDinh Nguyen hsotg->driver = driver; 422547a1685fSDinh Nguyen hsotg->gadget.dev.of_node = hsotg->dev->of_node; 422647a1685fSDinh Nguyen hsotg->gadget.speed = USB_SPEED_UNKNOWN; 422747a1685fSDinh Nguyen 422809a75e85SMarek Szyprowski if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) { 422909a75e85SMarek Szyprowski ret = dwc2_lowlevel_hw_enable(hsotg); 423009a75e85SMarek Szyprowski if (ret) 423147a1685fSDinh Nguyen goto err; 423247a1685fSDinh Nguyen } 423347a1685fSDinh Nguyen 4234f6c01592SGregory Herrero if (!IS_ERR_OR_NULL(hsotg->uphy)) 4235f6c01592SGregory Herrero otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget); 4236c816c47fSMarek Szyprowski 42375b9451f8SMarek Szyprowski spin_lock_irqsave(&hsotg->lock, flags); 4238d0f0ac56SJohn Youn if (dwc2_hw_is_device(hsotg)) { 42391f91b4ccSFelipe Balbi dwc2_hsotg_init(hsotg); 42401f91b4ccSFelipe Balbi dwc2_hsotg_core_init_disconnected(hsotg, false); 4241d0f0ac56SJohn Youn } 4242d0f0ac56SJohn Youn 4243dc6e69e6SMarek Szyprowski hsotg->enabled = 0; 42445b9451f8SMarek Szyprowski spin_unlock_irqrestore(&hsotg->lock, flags); 42455b9451f8SMarek Szyprowski 424647a1685fSDinh Nguyen dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name); 42475b9451f8SMarek Szyprowski 424847a1685fSDinh Nguyen return 0; 424947a1685fSDinh Nguyen 425047a1685fSDinh Nguyen err: 425147a1685fSDinh Nguyen hsotg->driver = NULL; 425247a1685fSDinh Nguyen return ret; 425347a1685fSDinh Nguyen } 425447a1685fSDinh Nguyen 425547a1685fSDinh Nguyen /** 42561f91b4ccSFelipe Balbi * dwc2_hsotg_udc_stop - stop the udc 425747a1685fSDinh Nguyen * @gadget: The usb gadget state 425847a1685fSDinh Nguyen * @driver: The usb gadget driver 425947a1685fSDinh Nguyen * 426047a1685fSDinh Nguyen * Stop udc hw block and stay tunned for future transmissions 426147a1685fSDinh Nguyen */ 42621f91b4ccSFelipe Balbi static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget) 426347a1685fSDinh Nguyen { 4264941fcce4SDinh Nguyen struct dwc2_hsotg *hsotg = to_hsotg(gadget); 426547a1685fSDinh Nguyen unsigned long flags = 0; 426647a1685fSDinh Nguyen int ep; 426747a1685fSDinh Nguyen 426847a1685fSDinh Nguyen if (!hsotg) 426947a1685fSDinh Nguyen return -ENODEV; 427047a1685fSDinh Nguyen 427147a1685fSDinh Nguyen /* all endpoints should be shutdown */ 4272c6f5c050SMian Yousaf Kaukab for (ep = 1; ep < hsotg->num_of_eps; ep++) { 4273c6f5c050SMian Yousaf Kaukab if (hsotg->eps_in[ep]) 42741f91b4ccSFelipe Balbi dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); 4275c6f5c050SMian Yousaf Kaukab if (hsotg->eps_out[ep]) 42761f91b4ccSFelipe Balbi dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); 4277c6f5c050SMian Yousaf Kaukab } 427847a1685fSDinh Nguyen 427947a1685fSDinh Nguyen spin_lock_irqsave(&hsotg->lock, flags); 428047a1685fSDinh Nguyen 428147a1685fSDinh Nguyen hsotg->driver = NULL; 428247a1685fSDinh Nguyen hsotg->gadget.speed = USB_SPEED_UNKNOWN; 4283dc6e69e6SMarek Szyprowski hsotg->enabled = 0; 428447a1685fSDinh Nguyen 428547a1685fSDinh Nguyen spin_unlock_irqrestore(&hsotg->lock, flags); 428647a1685fSDinh Nguyen 4287f6c01592SGregory Herrero if (!IS_ERR_OR_NULL(hsotg->uphy)) 4288f6c01592SGregory Herrero otg_set_peripheral(hsotg->uphy->otg, NULL); 4289c816c47fSMarek Szyprowski 429009a75e85SMarek Szyprowski if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) 429109a75e85SMarek Szyprowski dwc2_lowlevel_hw_disable(hsotg); 429247a1685fSDinh Nguyen 429347a1685fSDinh Nguyen return 0; 429447a1685fSDinh Nguyen } 429547a1685fSDinh Nguyen 429647a1685fSDinh Nguyen /** 42971f91b4ccSFelipe Balbi * dwc2_hsotg_gadget_getframe - read the frame number 429847a1685fSDinh Nguyen * @gadget: The usb gadget state 429947a1685fSDinh Nguyen * 430047a1685fSDinh Nguyen * Read the {micro} frame number 430147a1685fSDinh Nguyen */ 43021f91b4ccSFelipe Balbi static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget) 430347a1685fSDinh Nguyen { 43041f91b4ccSFelipe Balbi return dwc2_hsotg_read_frameno(to_hsotg(gadget)); 430547a1685fSDinh Nguyen } 430647a1685fSDinh Nguyen 430747a1685fSDinh Nguyen /** 43081f91b4ccSFelipe Balbi * dwc2_hsotg_pullup - connect/disconnect the USB PHY 430947a1685fSDinh Nguyen * @gadget: The usb gadget state 431047a1685fSDinh Nguyen * @is_on: Current state of the USB PHY 431147a1685fSDinh Nguyen * 431247a1685fSDinh Nguyen * Connect/Disconnect the USB PHY pullup 431347a1685fSDinh Nguyen */ 43141f91b4ccSFelipe Balbi static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on) 431547a1685fSDinh Nguyen { 4316941fcce4SDinh Nguyen struct dwc2_hsotg *hsotg = to_hsotg(gadget); 431747a1685fSDinh Nguyen unsigned long flags = 0; 431847a1685fSDinh Nguyen 431977ba9119SGregory Herrero dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on, 432077ba9119SGregory Herrero hsotg->op_state); 432177ba9119SGregory Herrero 432277ba9119SGregory Herrero /* Don't modify pullup state while in host mode */ 432377ba9119SGregory Herrero if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { 432477ba9119SGregory Herrero hsotg->enabled = is_on; 432577ba9119SGregory Herrero return 0; 432677ba9119SGregory Herrero } 432747a1685fSDinh Nguyen 432847a1685fSDinh Nguyen spin_lock_irqsave(&hsotg->lock, flags); 432947a1685fSDinh Nguyen if (is_on) { 4330dc6e69e6SMarek Szyprowski hsotg->enabled = 1; 43311f91b4ccSFelipe Balbi dwc2_hsotg_core_init_disconnected(hsotg, false); 43321f91b4ccSFelipe Balbi dwc2_hsotg_core_connect(hsotg); 433347a1685fSDinh Nguyen } else { 43341f91b4ccSFelipe Balbi dwc2_hsotg_core_disconnect(hsotg); 43351f91b4ccSFelipe Balbi dwc2_hsotg_disconnect(hsotg); 4336dc6e69e6SMarek Szyprowski hsotg->enabled = 0; 433747a1685fSDinh Nguyen } 433847a1685fSDinh Nguyen 433947a1685fSDinh Nguyen hsotg->gadget.speed = USB_SPEED_UNKNOWN; 434047a1685fSDinh Nguyen spin_unlock_irqrestore(&hsotg->lock, flags); 434147a1685fSDinh Nguyen 434247a1685fSDinh Nguyen return 0; 434347a1685fSDinh Nguyen } 434447a1685fSDinh Nguyen 43451f91b4ccSFelipe Balbi static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active) 434683d98223SGregory Herrero { 434783d98223SGregory Herrero struct dwc2_hsotg *hsotg = to_hsotg(gadget); 434883d98223SGregory Herrero unsigned long flags; 434983d98223SGregory Herrero 435083d98223SGregory Herrero dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active); 435183d98223SGregory Herrero spin_lock_irqsave(&hsotg->lock, flags); 435283d98223SGregory Herrero 435318b2b37cSGregory Herrero /* 435418b2b37cSGregory Herrero * If controller is hibernated, it must exit from hibernation 435561f7223bSGregory Herrero * before being initialized / de-initialized 435618b2b37cSGregory Herrero */ 4357065d3931SGregory Herrero if (hsotg->lx_state == DWC2_L2) 435818b2b37cSGregory Herrero dwc2_exit_hibernation(hsotg, false); 4359065d3931SGregory Herrero 436061f7223bSGregory Herrero if (is_active) { 436161f7223bSGregory Herrero hsotg->op_state = OTG_STATE_B_PERIPHERAL; 436261f7223bSGregory Herrero 43631f91b4ccSFelipe Balbi dwc2_hsotg_core_init_disconnected(hsotg, false); 436483d98223SGregory Herrero if (hsotg->enabled) 43651f91b4ccSFelipe Balbi dwc2_hsotg_core_connect(hsotg); 436683d98223SGregory Herrero } else { 43671f91b4ccSFelipe Balbi dwc2_hsotg_core_disconnect(hsotg); 43681f91b4ccSFelipe Balbi dwc2_hsotg_disconnect(hsotg); 436983d98223SGregory Herrero } 437083d98223SGregory Herrero 437183d98223SGregory Herrero spin_unlock_irqrestore(&hsotg->lock, flags); 437283d98223SGregory Herrero return 0; 437383d98223SGregory Herrero } 437483d98223SGregory Herrero 4375596d696aSGregory Herrero /** 43761f91b4ccSFelipe Balbi * dwc2_hsotg_vbus_draw - report bMaxPower field 4377596d696aSGregory Herrero * @gadget: The usb gadget state 4378596d696aSGregory Herrero * @mA: Amount of current 4379596d696aSGregory Herrero * 4380596d696aSGregory Herrero * Report how much power the device may consume to the phy. 4381596d696aSGregory Herrero */ 43829da51974SJohn Youn static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA) 4383596d696aSGregory Herrero { 4384596d696aSGregory Herrero struct dwc2_hsotg *hsotg = to_hsotg(gadget); 4385596d696aSGregory Herrero 4386596d696aSGregory Herrero if (IS_ERR_OR_NULL(hsotg->uphy)) 4387596d696aSGregory Herrero return -ENOTSUPP; 4388596d696aSGregory Herrero return usb_phy_set_power(hsotg->uphy, mA); 4389596d696aSGregory Herrero } 4390596d696aSGregory Herrero 43911f91b4ccSFelipe Balbi static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = { 43921f91b4ccSFelipe Balbi .get_frame = dwc2_hsotg_gadget_getframe, 43931f91b4ccSFelipe Balbi .udc_start = dwc2_hsotg_udc_start, 43941f91b4ccSFelipe Balbi .udc_stop = dwc2_hsotg_udc_stop, 43951f91b4ccSFelipe Balbi .pullup = dwc2_hsotg_pullup, 43961f91b4ccSFelipe Balbi .vbus_session = dwc2_hsotg_vbus_session, 43971f91b4ccSFelipe Balbi .vbus_draw = dwc2_hsotg_vbus_draw, 439847a1685fSDinh Nguyen }; 439947a1685fSDinh Nguyen 440047a1685fSDinh Nguyen /** 44011f91b4ccSFelipe Balbi * dwc2_hsotg_initep - initialise a single endpoint 440247a1685fSDinh Nguyen * @hsotg: The device state. 440347a1685fSDinh Nguyen * @hs_ep: The endpoint to be initialised. 440447a1685fSDinh Nguyen * @epnum: The endpoint number 440547a1685fSDinh Nguyen * 440647a1685fSDinh Nguyen * Initialise the given endpoint (as part of the probe and device state 440747a1685fSDinh Nguyen * creation) to give to the gadget driver. Setup the endpoint name, any 440847a1685fSDinh Nguyen * direction information and other state that may be required. 440947a1685fSDinh Nguyen */ 44101f91b4ccSFelipe Balbi static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg, 44111f91b4ccSFelipe Balbi struct dwc2_hsotg_ep *hs_ep, 4412c6f5c050SMian Yousaf Kaukab int epnum, 4413c6f5c050SMian Yousaf Kaukab bool dir_in) 441447a1685fSDinh Nguyen { 441547a1685fSDinh Nguyen char *dir; 441647a1685fSDinh Nguyen 441747a1685fSDinh Nguyen if (epnum == 0) 441847a1685fSDinh Nguyen dir = ""; 4419c6f5c050SMian Yousaf Kaukab else if (dir_in) 442047a1685fSDinh Nguyen dir = "in"; 4421c6f5c050SMian Yousaf Kaukab else 4422c6f5c050SMian Yousaf Kaukab dir = "out"; 442347a1685fSDinh Nguyen 4424c6f5c050SMian Yousaf Kaukab hs_ep->dir_in = dir_in; 442547a1685fSDinh Nguyen hs_ep->index = epnum; 442647a1685fSDinh Nguyen 442747a1685fSDinh Nguyen snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir); 442847a1685fSDinh Nguyen 442947a1685fSDinh Nguyen INIT_LIST_HEAD(&hs_ep->queue); 443047a1685fSDinh Nguyen INIT_LIST_HEAD(&hs_ep->ep.ep_list); 443147a1685fSDinh Nguyen 443247a1685fSDinh Nguyen /* add to the list of endpoints known by the gadget driver */ 443347a1685fSDinh Nguyen if (epnum) 443447a1685fSDinh Nguyen list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list); 443547a1685fSDinh Nguyen 443647a1685fSDinh Nguyen hs_ep->parent = hsotg; 443747a1685fSDinh Nguyen hs_ep->ep.name = hs_ep->name; 443838e9002bSVardan Mikayelyan 443938e9002bSVardan Mikayelyan if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW) 444038e9002bSVardan Mikayelyan usb_ep_set_maxpacket_limit(&hs_ep->ep, 8); 444138e9002bSVardan Mikayelyan else 444238e9002bSVardan Mikayelyan usb_ep_set_maxpacket_limit(&hs_ep->ep, 444338e9002bSVardan Mikayelyan epnum ? 1024 : EP0_MPS_LIMIT); 44441f91b4ccSFelipe Balbi hs_ep->ep.ops = &dwc2_hsotg_ep_ops; 444547a1685fSDinh Nguyen 44462954522fSRobert Baldyga if (epnum == 0) { 44472954522fSRobert Baldyga hs_ep->ep.caps.type_control = true; 44482954522fSRobert Baldyga } else { 444938e9002bSVardan Mikayelyan if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) { 44502954522fSRobert Baldyga hs_ep->ep.caps.type_iso = true; 44512954522fSRobert Baldyga hs_ep->ep.caps.type_bulk = true; 445238e9002bSVardan Mikayelyan } 44532954522fSRobert Baldyga hs_ep->ep.caps.type_int = true; 44542954522fSRobert Baldyga } 44552954522fSRobert Baldyga 44562954522fSRobert Baldyga if (dir_in) 44572954522fSRobert Baldyga hs_ep->ep.caps.dir_in = true; 44582954522fSRobert Baldyga else 44592954522fSRobert Baldyga hs_ep->ep.caps.dir_out = true; 44602954522fSRobert Baldyga 446147a1685fSDinh Nguyen /* 446247a1685fSDinh Nguyen * if we're using dma, we need to set the next-endpoint pointer 446347a1685fSDinh Nguyen * to be something valid. 446447a1685fSDinh Nguyen */ 446547a1685fSDinh Nguyen 446647a1685fSDinh Nguyen if (using_dma(hsotg)) { 446747a1685fSDinh Nguyen u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15); 44689da51974SJohn Youn 4469c6f5c050SMian Yousaf Kaukab if (dir_in) 447095c8bc36SAntti Seppälä dwc2_writel(next, hsotg->regs + DIEPCTL(epnum)); 4471c6f5c050SMian Yousaf Kaukab else 447295c8bc36SAntti Seppälä dwc2_writel(next, hsotg->regs + DOEPCTL(epnum)); 447347a1685fSDinh Nguyen } 447447a1685fSDinh Nguyen } 447547a1685fSDinh Nguyen 447647a1685fSDinh Nguyen /** 44771f91b4ccSFelipe Balbi * dwc2_hsotg_hw_cfg - read HW configuration registers 447847a1685fSDinh Nguyen * @param: The device state 447947a1685fSDinh Nguyen * 448047a1685fSDinh Nguyen * Read the USB core HW configuration registers 448147a1685fSDinh Nguyen */ 44821f91b4ccSFelipe Balbi static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg) 448347a1685fSDinh Nguyen { 4484c6f5c050SMian Yousaf Kaukab u32 cfg; 4485c6f5c050SMian Yousaf Kaukab u32 ep_type; 4486c6f5c050SMian Yousaf Kaukab u32 i; 4487c6f5c050SMian Yousaf Kaukab 448847a1685fSDinh Nguyen /* check hardware configuration */ 448947a1685fSDinh Nguyen 449043e90349SJohn Youn hsotg->num_of_eps = hsotg->hw_params.num_dev_ep; 449143e90349SJohn Youn 4492c6f5c050SMian Yousaf Kaukab /* Add ep0 */ 4493c6f5c050SMian Yousaf Kaukab hsotg->num_of_eps++; 449447a1685fSDinh Nguyen 4495b98866c2SJohn Youn hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, 4496b98866c2SJohn Youn sizeof(struct dwc2_hsotg_ep), 4497c6f5c050SMian Yousaf Kaukab GFP_KERNEL); 4498c6f5c050SMian Yousaf Kaukab if (!hsotg->eps_in[0]) 4499c6f5c050SMian Yousaf Kaukab return -ENOMEM; 45001f91b4ccSFelipe Balbi /* Same dwc2_hsotg_ep is used in both directions for ep0 */ 4501c6f5c050SMian Yousaf Kaukab hsotg->eps_out[0] = hsotg->eps_in[0]; 450247a1685fSDinh Nguyen 450343e90349SJohn Youn cfg = hsotg->hw_params.dev_ep_dirs; 4504251a17f5SRoshan Pius for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) { 4505c6f5c050SMian Yousaf Kaukab ep_type = cfg & 3; 4506c6f5c050SMian Yousaf Kaukab /* Direction in or both */ 4507c6f5c050SMian Yousaf Kaukab if (!(ep_type & 2)) { 4508c6f5c050SMian Yousaf Kaukab hsotg->eps_in[i] = devm_kzalloc(hsotg->dev, 45091f91b4ccSFelipe Balbi sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); 4510c6f5c050SMian Yousaf Kaukab if (!hsotg->eps_in[i]) 4511c6f5c050SMian Yousaf Kaukab return -ENOMEM; 4512c6f5c050SMian Yousaf Kaukab } 4513c6f5c050SMian Yousaf Kaukab /* Direction out or both */ 4514c6f5c050SMian Yousaf Kaukab if (!(ep_type & 1)) { 4515c6f5c050SMian Yousaf Kaukab hsotg->eps_out[i] = devm_kzalloc(hsotg->dev, 45161f91b4ccSFelipe Balbi sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); 4517c6f5c050SMian Yousaf Kaukab if (!hsotg->eps_out[i]) 4518c6f5c050SMian Yousaf Kaukab return -ENOMEM; 4519c6f5c050SMian Yousaf Kaukab } 4520c6f5c050SMian Yousaf Kaukab } 4521c6f5c050SMian Yousaf Kaukab 452243e90349SJohn Youn hsotg->fifo_mem = hsotg->hw_params.total_fifo_size; 452343e90349SJohn Youn hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo; 452447a1685fSDinh Nguyen 4525cff9eb75SMarek Szyprowski dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n", 4526cff9eb75SMarek Szyprowski hsotg->num_of_eps, 4527cff9eb75SMarek Szyprowski hsotg->dedicated_fifos ? "dedicated" : "shared", 4528cff9eb75SMarek Szyprowski hsotg->fifo_mem); 4529c6f5c050SMian Yousaf Kaukab return 0; 453047a1685fSDinh Nguyen } 453147a1685fSDinh Nguyen 453247a1685fSDinh Nguyen /** 45331f91b4ccSFelipe Balbi * dwc2_hsotg_dump - dump state of the udc 453447a1685fSDinh Nguyen * @param: The device state 453547a1685fSDinh Nguyen */ 45361f91b4ccSFelipe Balbi static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg) 453747a1685fSDinh Nguyen { 453847a1685fSDinh Nguyen #ifdef DEBUG 453947a1685fSDinh Nguyen struct device *dev = hsotg->dev; 454047a1685fSDinh Nguyen void __iomem *regs = hsotg->regs; 454147a1685fSDinh Nguyen u32 val; 454247a1685fSDinh Nguyen int idx; 454347a1685fSDinh Nguyen 454447a1685fSDinh Nguyen dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n", 454595c8bc36SAntti Seppälä dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL), 454695c8bc36SAntti Seppälä dwc2_readl(regs + DIEPMSK)); 454747a1685fSDinh Nguyen 4548f889f23dSMian Yousaf Kaukab dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n", 454995c8bc36SAntti Seppälä dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1)); 455047a1685fSDinh Nguyen 455147a1685fSDinh Nguyen dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", 455295c8bc36SAntti Seppälä dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ)); 455347a1685fSDinh Nguyen 455447a1685fSDinh Nguyen /* show periodic fifo settings */ 455547a1685fSDinh Nguyen 4556364f8e93SMian Yousaf Kaukab for (idx = 1; idx < hsotg->num_of_eps; idx++) { 455795c8bc36SAntti Seppälä val = dwc2_readl(regs + DPTXFSIZN(idx)); 455847a1685fSDinh Nguyen dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx, 455947a1685fSDinh Nguyen val >> FIFOSIZE_DEPTH_SHIFT, 456047a1685fSDinh Nguyen val & FIFOSIZE_STARTADDR_MASK); 456147a1685fSDinh Nguyen } 456247a1685fSDinh Nguyen 4563364f8e93SMian Yousaf Kaukab for (idx = 0; idx < hsotg->num_of_eps; idx++) { 456447a1685fSDinh Nguyen dev_info(dev, 456547a1685fSDinh Nguyen "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx, 456695c8bc36SAntti Seppälä dwc2_readl(regs + DIEPCTL(idx)), 456795c8bc36SAntti Seppälä dwc2_readl(regs + DIEPTSIZ(idx)), 456895c8bc36SAntti Seppälä dwc2_readl(regs + DIEPDMA(idx))); 456947a1685fSDinh Nguyen 457095c8bc36SAntti Seppälä val = dwc2_readl(regs + DOEPCTL(idx)); 457147a1685fSDinh Nguyen dev_info(dev, 457247a1685fSDinh Nguyen "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", 457395c8bc36SAntti Seppälä idx, dwc2_readl(regs + DOEPCTL(idx)), 457495c8bc36SAntti Seppälä dwc2_readl(regs + DOEPTSIZ(idx)), 457595c8bc36SAntti Seppälä dwc2_readl(regs + DOEPDMA(idx))); 457647a1685fSDinh Nguyen } 457747a1685fSDinh Nguyen 457847a1685fSDinh Nguyen dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n", 457995c8bc36SAntti Seppälä dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE)); 458047a1685fSDinh Nguyen #endif 458147a1685fSDinh Nguyen } 458247a1685fSDinh Nguyen 458347a1685fSDinh Nguyen /** 4584117777b2SDinh Nguyen * dwc2_gadget_init - init function for gadget 4585117777b2SDinh Nguyen * @dwc2: The data structure for the DWC2 driver. 458647a1685fSDinh Nguyen */ 4587f3768997SVardan Mikayelyan int dwc2_gadget_init(struct dwc2_hsotg *hsotg) 458847a1685fSDinh Nguyen { 4589117777b2SDinh Nguyen struct device *dev = hsotg->dev; 459047a1685fSDinh Nguyen int epnum; 459147a1685fSDinh Nguyen int ret; 459243e90349SJohn Youn 45930a176279SGregory Herrero /* Dump fifo information */ 45940a176279SGregory Herrero dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n", 459505ee799fSJohn Youn hsotg->params.g_np_tx_fifo_size); 459605ee799fSJohn Youn dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size); 459747a1685fSDinh Nguyen 459847a1685fSDinh Nguyen hsotg->gadget.max_speed = USB_SPEED_HIGH; 45991f91b4ccSFelipe Balbi hsotg->gadget.ops = &dwc2_hsotg_gadget_ops; 460047a1685fSDinh Nguyen hsotg->gadget.name = dev_name(dev); 4601097ee662SGregory Herrero if (hsotg->dr_mode == USB_DR_MODE_OTG) 4602097ee662SGregory Herrero hsotg->gadget.is_otg = 1; 4603ec4cc657SMian Yousaf Kaukab else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) 4604ec4cc657SMian Yousaf Kaukab hsotg->op_state = OTG_STATE_B_PERIPHERAL; 460547a1685fSDinh Nguyen 46061f91b4ccSFelipe Balbi ret = dwc2_hsotg_hw_cfg(hsotg); 4607c6f5c050SMian Yousaf Kaukab if (ret) { 4608c6f5c050SMian Yousaf Kaukab dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret); 460909a75e85SMarek Szyprowski return ret; 4610c6f5c050SMian Yousaf Kaukab } 4611c6f5c050SMian Yousaf Kaukab 46123f95001dSMian Yousaf Kaukab hsotg->ctrl_buff = devm_kzalloc(hsotg->dev, 46133f95001dSMian Yousaf Kaukab DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); 46148bae0f8cSWolfram Sang if (!hsotg->ctrl_buff) 461509a75e85SMarek Szyprowski return -ENOMEM; 46163f95001dSMian Yousaf Kaukab 46173f95001dSMian Yousaf Kaukab hsotg->ep0_buff = devm_kzalloc(hsotg->dev, 46183f95001dSMian Yousaf Kaukab DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); 46198bae0f8cSWolfram Sang if (!hsotg->ep0_buff) 462009a75e85SMarek Szyprowski return -ENOMEM; 46213f95001dSMian Yousaf Kaukab 46220f6b80c0SVahram Aharonyan if (using_desc_dma(hsotg)) { 46230f6b80c0SVahram Aharonyan ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg); 46240f6b80c0SVahram Aharonyan if (ret < 0) 46250f6b80c0SVahram Aharonyan return ret; 46260f6b80c0SVahram Aharonyan } 46270f6b80c0SVahram Aharonyan 4628f3768997SVardan Mikayelyan ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq, 4629f3768997SVardan Mikayelyan IRQF_SHARED, dev_name(hsotg->dev), hsotg); 4630eb3c56c5SMarek Szyprowski if (ret < 0) { 4631db8178c3SDinh Nguyen dev_err(dev, "cannot claim IRQ for gadget\n"); 463209a75e85SMarek Szyprowski return ret; 4633eb3c56c5SMarek Szyprowski } 4634eb3c56c5SMarek Szyprowski 463547a1685fSDinh Nguyen /* hsotg->num_of_eps holds number of EPs other than ep0 */ 463647a1685fSDinh Nguyen 463747a1685fSDinh Nguyen if (hsotg->num_of_eps == 0) { 463847a1685fSDinh Nguyen dev_err(dev, "wrong number of EPs (zero)\n"); 463909a75e85SMarek Szyprowski return -EINVAL; 464047a1685fSDinh Nguyen } 464147a1685fSDinh Nguyen 464247a1685fSDinh Nguyen /* setup endpoint information */ 464347a1685fSDinh Nguyen 464447a1685fSDinh Nguyen INIT_LIST_HEAD(&hsotg->gadget.ep_list); 4645c6f5c050SMian Yousaf Kaukab hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep; 464647a1685fSDinh Nguyen 464747a1685fSDinh Nguyen /* allocate EP0 request */ 464847a1685fSDinh Nguyen 46491f91b4ccSFelipe Balbi hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep, 465047a1685fSDinh Nguyen GFP_KERNEL); 465147a1685fSDinh Nguyen if (!hsotg->ctrl_req) { 465247a1685fSDinh Nguyen dev_err(dev, "failed to allocate ctrl req\n"); 465309a75e85SMarek Szyprowski return -ENOMEM; 465447a1685fSDinh Nguyen } 465547a1685fSDinh Nguyen 465647a1685fSDinh Nguyen /* initialise the endpoints now the core has been initialised */ 4657c6f5c050SMian Yousaf Kaukab for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) { 4658c6f5c050SMian Yousaf Kaukab if (hsotg->eps_in[epnum]) 46591f91b4ccSFelipe Balbi dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum], 4660c6f5c050SMian Yousaf Kaukab epnum, 1); 4661c6f5c050SMian Yousaf Kaukab if (hsotg->eps_out[epnum]) 46621f91b4ccSFelipe Balbi dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum], 4663c6f5c050SMian Yousaf Kaukab epnum, 0); 4664c6f5c050SMian Yousaf Kaukab } 466547a1685fSDinh Nguyen 4666117777b2SDinh Nguyen ret = usb_add_gadget_udc(dev, &hsotg->gadget); 466747a1685fSDinh Nguyen if (ret) 466809a75e85SMarek Szyprowski return ret; 466947a1685fSDinh Nguyen 46701f91b4ccSFelipe Balbi dwc2_hsotg_dump(hsotg); 467147a1685fSDinh Nguyen 467247a1685fSDinh Nguyen return 0; 467347a1685fSDinh Nguyen } 467447a1685fSDinh Nguyen 467547a1685fSDinh Nguyen /** 46761f91b4ccSFelipe Balbi * dwc2_hsotg_remove - remove function for hsotg driver 467747a1685fSDinh Nguyen * @pdev: The platform information for the driver 467847a1685fSDinh Nguyen */ 46791f91b4ccSFelipe Balbi int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg) 468047a1685fSDinh Nguyen { 468147a1685fSDinh Nguyen usb_del_gadget_udc(&hsotg->gadget); 468247a1685fSDinh Nguyen 468347a1685fSDinh Nguyen return 0; 468447a1685fSDinh Nguyen } 468547a1685fSDinh Nguyen 46861f91b4ccSFelipe Balbi int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg) 468747a1685fSDinh Nguyen { 468847a1685fSDinh Nguyen unsigned long flags; 468947a1685fSDinh Nguyen 46909e779778SGregory Herrero if (hsotg->lx_state != DWC2_L0) 469109a75e85SMarek Szyprowski return 0; 46929e779778SGregory Herrero 4693dc6e69e6SMarek Szyprowski if (hsotg->driver) { 4694dc6e69e6SMarek Szyprowski int ep; 4695dc6e69e6SMarek Szyprowski 469647a1685fSDinh Nguyen dev_info(hsotg->dev, "suspending usb gadget %s\n", 469747a1685fSDinh Nguyen hsotg->driver->driver.name); 469847a1685fSDinh Nguyen 469947a1685fSDinh Nguyen spin_lock_irqsave(&hsotg->lock, flags); 4700dc6e69e6SMarek Szyprowski if (hsotg->enabled) 47011f91b4ccSFelipe Balbi dwc2_hsotg_core_disconnect(hsotg); 47021f91b4ccSFelipe Balbi dwc2_hsotg_disconnect(hsotg); 470347a1685fSDinh Nguyen hsotg->gadget.speed = USB_SPEED_UNKNOWN; 470447a1685fSDinh Nguyen spin_unlock_irqrestore(&hsotg->lock, flags); 470547a1685fSDinh Nguyen 4706c6f5c050SMian Yousaf Kaukab for (ep = 0; ep < hsotg->num_of_eps; ep++) { 4707c6f5c050SMian Yousaf Kaukab if (hsotg->eps_in[ep]) 47081f91b4ccSFelipe Balbi dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); 4709c6f5c050SMian Yousaf Kaukab if (hsotg->eps_out[ep]) 47101f91b4ccSFelipe Balbi dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); 4711c6f5c050SMian Yousaf Kaukab } 471247a1685fSDinh Nguyen } 471347a1685fSDinh Nguyen 471409a75e85SMarek Szyprowski return 0; 471547a1685fSDinh Nguyen } 471647a1685fSDinh Nguyen 47171f91b4ccSFelipe Balbi int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg) 471847a1685fSDinh Nguyen { 471947a1685fSDinh Nguyen unsigned long flags; 472047a1685fSDinh Nguyen 47219e779778SGregory Herrero if (hsotg->lx_state == DWC2_L2) 472209a75e85SMarek Szyprowski return 0; 47239e779778SGregory Herrero 472447a1685fSDinh Nguyen if (hsotg->driver) { 472547a1685fSDinh Nguyen dev_info(hsotg->dev, "resuming usb gadget %s\n", 472647a1685fSDinh Nguyen hsotg->driver->driver.name); 4727d00b4142SRobert Baldyga 472847a1685fSDinh Nguyen spin_lock_irqsave(&hsotg->lock, flags); 47291f91b4ccSFelipe Balbi dwc2_hsotg_core_init_disconnected(hsotg, false); 4730dc6e69e6SMarek Szyprowski if (hsotg->enabled) 47311f91b4ccSFelipe Balbi dwc2_hsotg_core_connect(hsotg); 473247a1685fSDinh Nguyen spin_unlock_irqrestore(&hsotg->lock, flags); 4733dc6e69e6SMarek Szyprowski } 473447a1685fSDinh Nguyen 473509a75e85SMarek Szyprowski return 0; 473647a1685fSDinh Nguyen } 473758e52ff6SJohn Youn 473858e52ff6SJohn Youn /** 473958e52ff6SJohn Youn * dwc2_backup_device_registers() - Backup controller device registers. 474058e52ff6SJohn Youn * When suspending usb bus, registers needs to be backuped 474158e52ff6SJohn Youn * if controller power is disabled once suspended. 474258e52ff6SJohn Youn * 474358e52ff6SJohn Youn * @hsotg: Programming view of the DWC_otg controller 474458e52ff6SJohn Youn */ 474558e52ff6SJohn Youn int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg) 474658e52ff6SJohn Youn { 474758e52ff6SJohn Youn struct dwc2_dregs_backup *dr; 474858e52ff6SJohn Youn int i; 474958e52ff6SJohn Youn 475058e52ff6SJohn Youn dev_dbg(hsotg->dev, "%s\n", __func__); 475158e52ff6SJohn Youn 475258e52ff6SJohn Youn /* Backup dev regs */ 475358e52ff6SJohn Youn dr = &hsotg->dr_backup; 475458e52ff6SJohn Youn 475558e52ff6SJohn Youn dr->dcfg = dwc2_readl(hsotg->regs + DCFG); 475658e52ff6SJohn Youn dr->dctl = dwc2_readl(hsotg->regs + DCTL); 475758e52ff6SJohn Youn dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK); 475858e52ff6SJohn Youn dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK); 475958e52ff6SJohn Youn dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK); 476058e52ff6SJohn Youn 476158e52ff6SJohn Youn for (i = 0; i < hsotg->num_of_eps; i++) { 476258e52ff6SJohn Youn /* Backup IN EPs */ 476358e52ff6SJohn Youn dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i)); 476458e52ff6SJohn Youn 476558e52ff6SJohn Youn /* Ensure DATA PID is correctly configured */ 476658e52ff6SJohn Youn if (dr->diepctl[i] & DXEPCTL_DPID) 476758e52ff6SJohn Youn dr->diepctl[i] |= DXEPCTL_SETD1PID; 476858e52ff6SJohn Youn else 476958e52ff6SJohn Youn dr->diepctl[i] |= DXEPCTL_SETD0PID; 477058e52ff6SJohn Youn 477158e52ff6SJohn Youn dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i)); 477258e52ff6SJohn Youn dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i)); 477358e52ff6SJohn Youn 477458e52ff6SJohn Youn /* Backup OUT EPs */ 477558e52ff6SJohn Youn dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i)); 477658e52ff6SJohn Youn 477758e52ff6SJohn Youn /* Ensure DATA PID is correctly configured */ 477858e52ff6SJohn Youn if (dr->doepctl[i] & DXEPCTL_DPID) 477958e52ff6SJohn Youn dr->doepctl[i] |= DXEPCTL_SETD1PID; 478058e52ff6SJohn Youn else 478158e52ff6SJohn Youn dr->doepctl[i] |= DXEPCTL_SETD0PID; 478258e52ff6SJohn Youn 478358e52ff6SJohn Youn dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i)); 478458e52ff6SJohn Youn dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i)); 478558e52ff6SJohn Youn } 478658e52ff6SJohn Youn dr->valid = true; 478758e52ff6SJohn Youn return 0; 478858e52ff6SJohn Youn } 478958e52ff6SJohn Youn 479058e52ff6SJohn Youn /** 479158e52ff6SJohn Youn * dwc2_restore_device_registers() - Restore controller device registers. 479258e52ff6SJohn Youn * When resuming usb bus, device registers needs to be restored 479358e52ff6SJohn Youn * if controller power were disabled. 479458e52ff6SJohn Youn * 479558e52ff6SJohn Youn * @hsotg: Programming view of the DWC_otg controller 479658e52ff6SJohn Youn */ 479758e52ff6SJohn Youn int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg) 479858e52ff6SJohn Youn { 479958e52ff6SJohn Youn struct dwc2_dregs_backup *dr; 480058e52ff6SJohn Youn u32 dctl; 480158e52ff6SJohn Youn int i; 480258e52ff6SJohn Youn 480358e52ff6SJohn Youn dev_dbg(hsotg->dev, "%s\n", __func__); 480458e52ff6SJohn Youn 480558e52ff6SJohn Youn /* Restore dev regs */ 480658e52ff6SJohn Youn dr = &hsotg->dr_backup; 480758e52ff6SJohn Youn if (!dr->valid) { 480858e52ff6SJohn Youn dev_err(hsotg->dev, "%s: no device registers to restore\n", 480958e52ff6SJohn Youn __func__); 481058e52ff6SJohn Youn return -EINVAL; 481158e52ff6SJohn Youn } 481258e52ff6SJohn Youn dr->valid = false; 481358e52ff6SJohn Youn 481458e52ff6SJohn Youn dwc2_writel(dr->dcfg, hsotg->regs + DCFG); 481558e52ff6SJohn Youn dwc2_writel(dr->dctl, hsotg->regs + DCTL); 481658e52ff6SJohn Youn dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK); 481758e52ff6SJohn Youn dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK); 481858e52ff6SJohn Youn dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK); 481958e52ff6SJohn Youn 482058e52ff6SJohn Youn for (i = 0; i < hsotg->num_of_eps; i++) { 482158e52ff6SJohn Youn /* Restore IN EPs */ 482258e52ff6SJohn Youn dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i)); 482358e52ff6SJohn Youn dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i)); 482458e52ff6SJohn Youn dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i)); 482558e52ff6SJohn Youn 482658e52ff6SJohn Youn /* Restore OUT EPs */ 482758e52ff6SJohn Youn dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i)); 482858e52ff6SJohn Youn dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i)); 482958e52ff6SJohn Youn dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i)); 483058e52ff6SJohn Youn } 483158e52ff6SJohn Youn 483258e52ff6SJohn Youn /* Set the Power-On Programming done bit */ 483358e52ff6SJohn Youn dctl = dwc2_readl(hsotg->regs + DCTL); 483458e52ff6SJohn Youn dctl |= DCTL_PWRONPRGDONE; 483558e52ff6SJohn Youn dwc2_writel(dctl, hsotg->regs + DCTL); 483658e52ff6SJohn Youn 483758e52ff6SJohn Youn return 0; 483858e52ff6SJohn Youn } 4839