Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1 | /** |
Anton Tikhomirov | dfbc6fa | 2011-04-21 17:06:43 +0900 | [diff] [blame] | 2 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 3 | * http://www.samsung.com |
| 4 | * |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 5 | * Copyright 2008 Openmoko, Inc. |
| 6 | * Copyright 2008 Simtec Electronics |
| 7 | * Ben Dooks <ben@simtec.co.uk> |
| 8 | * http://armlinux.simtec.co.uk/ |
| 9 | * |
| 10 | * S3C USB2.0 High-speed / OtG driver |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 15 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/dma-mapping.h> |
Marek Szyprowski | 7ad8096 | 2014-11-21 15:14:48 +0100 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 24 | #include <linux/seq_file.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Tomasz Figa | c50f056c | 2013-06-25 17:38:23 +0200 | [diff] [blame] | 28 | #include <linux/of_platform.h> |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 29 | |
| 30 | #include <linux/usb/ch9.h> |
| 31 | #include <linux/usb/gadget.h> |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 32 | #include <linux/usb/phy.h> |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 33 | |
Dinh Nguyen | f7c0b14 | 2014-04-14 14:13:35 -0700 | [diff] [blame] | 34 | #include "core.h" |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 35 | #include "hw.h" |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 36 | |
| 37 | /* conversion functions */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 38 | static inline struct dwc2_hsotg_req *our_req(struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 39 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 40 | return container_of(req, struct dwc2_hsotg_req, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 43 | static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 44 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 45 | return container_of(ep, struct dwc2_hsotg_ep, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 48 | static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 49 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 50 | return container_of(gadget, struct dwc2_hsotg, gadget); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static inline void __orr32(void __iomem *ptr, u32 val) |
| 54 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 55 | dwc2_writel(dwc2_readl(ptr) | val, ptr); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static inline void __bic32(void __iomem *ptr, u32 val) |
| 59 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 60 | dwc2_writel(dwc2_readl(ptr) & ~val, ptr); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 63 | static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg, |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 64 | u32 ep_index, u32 dir_in) |
| 65 | { |
| 66 | if (dir_in) |
| 67 | return hsotg->eps_in[ep_index]; |
| 68 | else |
| 69 | return hsotg->eps_out[ep_index]; |
| 70 | } |
| 71 | |
Mickael Maison | 997f4f8 | 2014-12-23 17:39:45 +0100 | [diff] [blame] | 72 | /* forward declaration of functions */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 73 | static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * using_dma - return the DMA status of the driver. |
| 77 | * @hsotg: The driver state. |
| 78 | * |
| 79 | * Return true if we're using DMA. |
| 80 | * |
| 81 | * Currently, we have the DMA support code worked into everywhere |
| 82 | * that needs it, but the AMBA DMA implementation in the hardware can |
| 83 | * only DMA from 32bit aligned addresses. This means that gadgets such |
| 84 | * as the CDC Ethernet cannot work as they often pass packets which are |
| 85 | * not 32bit aligned. |
| 86 | * |
| 87 | * Unfortunately the choice to use DMA or not is global to the controller |
| 88 | * and seems to be only settable when the controller is being put through |
| 89 | * a core reset. This means we either need to fix the gadgets to take |
| 90 | * account of DMA alignment, or add bounce buffers (yuerk). |
| 91 | * |
Gregory Herrero | edd74be | 2015-01-09 13:38:48 +0100 | [diff] [blame] | 92 | * g_using_dma is set depending on dts flag. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 93 | */ |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 94 | static inline bool using_dma(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 95 | { |
Gregory Herrero | edd74be | 2015-01-09 13:38:48 +0100 | [diff] [blame] | 96 | return hsotg->g_using_dma; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Vardan Mikayelyan | 92d1635 | 2016-05-25 18:07:05 -0700 | [diff] [blame] | 100 | * dwc2_gadget_incr_frame_num - Increments the targeted frame number. |
| 101 | * @hs_ep: The endpoint |
| 102 | * @increment: The value to increment by |
| 103 | * |
| 104 | * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT. |
| 105 | * If an overrun occurs it will wrap the value and set the frame_overrun flag. |
| 106 | */ |
| 107 | static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep) |
| 108 | { |
| 109 | hs_ep->target_frame += hs_ep->interval; |
| 110 | if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) { |
| 111 | hs_ep->frame_overrun = 1; |
| 112 | hs_ep->target_frame &= DSTS_SOFFN_LIMIT; |
| 113 | } else { |
| 114 | hs_ep->frame_overrun = 0; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 119 | * dwc2_hsotg_en_gsint - enable one or more of the general interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 120 | * @hsotg: The device state |
| 121 | * @ints: A bitmask of the interrupts to enable |
| 122 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 123 | static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 124 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 125 | u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 126 | u32 new_gsintmsk; |
| 127 | |
| 128 | new_gsintmsk = gsintmsk | ints; |
| 129 | |
| 130 | if (new_gsintmsk != gsintmsk) { |
| 131 | dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 132 | dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 137 | * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 138 | * @hsotg: The device state |
| 139 | * @ints: A bitmask of the interrupts to enable |
| 140 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 141 | static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 142 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 143 | u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 144 | u32 new_gsintmsk; |
| 145 | |
| 146 | new_gsintmsk = gsintmsk & ~ints; |
| 147 | |
| 148 | if (new_gsintmsk != gsintmsk) |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 149 | dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 153 | * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 154 | * @hsotg: The device state |
| 155 | * @ep: The endpoint index |
| 156 | * @dir_in: True if direction is in. |
| 157 | * @en: The enable value, true to enable |
| 158 | * |
| 159 | * Set or clear the mask for an individual endpoint's interrupt |
| 160 | * request. |
| 161 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 162 | static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 163 | unsigned int ep, unsigned int dir_in, |
| 164 | unsigned int en) |
| 165 | { |
| 166 | unsigned long flags; |
| 167 | u32 bit = 1 << ep; |
| 168 | u32 daint; |
| 169 | |
| 170 | if (!dir_in) |
| 171 | bit <<= 16; |
| 172 | |
| 173 | local_irq_save(flags); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 174 | daint = dwc2_readl(hsotg->regs + DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 175 | if (en) |
| 176 | daint |= bit; |
| 177 | else |
| 178 | daint &= ~bit; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 179 | dwc2_writel(daint, hsotg->regs + DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 180 | local_irq_restore(flags); |
| 181 | } |
| 182 | |
| 183 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 184 | * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 185 | * @hsotg: The device instance. |
| 186 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 187 | static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 188 | { |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 189 | unsigned int ep; |
| 190 | unsigned int addr; |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 191 | int timeout; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 192 | u32 val; |
| 193 | |
Gregory Herrero | 7fcbc95 | 2015-01-09 13:39:06 +0100 | [diff] [blame] | 194 | /* Reset fifo map if not correctly cleared during previous session */ |
| 195 | WARN_ON(hsotg->fifo_map); |
| 196 | hsotg->fifo_map = 0; |
| 197 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 198 | /* set RX/NPTX FIFO sizes */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 199 | dwc2_writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ); |
| 200 | dwc2_writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 201 | (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT), |
| 202 | hsotg->regs + GNPTXFSIZ); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 203 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 204 | /* |
| 205 | * arange all the rest of the TX FIFOs, as some versions of this |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 206 | * block have overlapping default addresses. This also ensures |
| 207 | * that if the settings have been changed, then they are set to |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 208 | * known values. |
| 209 | */ |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 210 | |
| 211 | /* start at the end of the GNPTXFSIZ, rounded up */ |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 212 | addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 213 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 214 | /* |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 215 | * Configure fifos sizes from provided configuration and assign |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 216 | * them to endpoints dynamically according to maxpacket size value of |
| 217 | * given endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 218 | */ |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 219 | for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) { |
| 220 | if (!hsotg->g_tx_fifo_sz[ep]) |
| 221 | continue; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 222 | val = addr; |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 223 | val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT; |
| 224 | WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem, |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 225 | "insufficient fifo memory"); |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 226 | addr += hsotg->g_tx_fifo_sz[ep]; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 227 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 228 | dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep)); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 229 | } |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 230 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 231 | /* |
| 232 | * according to p428 of the design guide, we need to ensure that |
| 233 | * all fifos are flushed before continuing |
| 234 | */ |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 235 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 236 | dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 237 | GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 238 | |
| 239 | /* wait until the fifos are both flushed */ |
| 240 | timeout = 100; |
| 241 | while (1) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 242 | val = dwc2_readl(hsotg->regs + GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 243 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 244 | if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0) |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 245 | break; |
| 246 | |
| 247 | if (--timeout == 0) { |
| 248 | dev_err(hsotg->dev, |
| 249 | "%s: timeout flushing fifos (GRSTCTL=%08x)\n", |
| 250 | __func__, val); |
Gregory Herrero | 48b20bc | 2015-01-09 13:39:01 +0100 | [diff] [blame] | 251 | break; |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | udelay(1); |
| 255 | } |
| 256 | |
| 257 | dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * @ep: USB endpoint to allocate request for. |
| 262 | * @flags: Allocation flags |
| 263 | * |
| 264 | * Allocate a new USB request structure appropriate for the specified endpoint |
| 265 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 266 | static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep, |
Mark Brown | 0978f8c | 2010-01-18 13:18:35 +0000 | [diff] [blame] | 267 | gfp_t flags) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 268 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 269 | struct dwc2_hsotg_req *req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 270 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 271 | req = kzalloc(sizeof(struct dwc2_hsotg_req), flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 272 | if (!req) |
| 273 | return NULL; |
| 274 | |
| 275 | INIT_LIST_HEAD(&req->queue); |
| 276 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 277 | return &req->req; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * is_ep_periodic - return true if the endpoint is in periodic mode. |
| 282 | * @hs_ep: The endpoint to query. |
| 283 | * |
| 284 | * Returns true if the endpoint is in periodic mode, meaning it is being |
| 285 | * used for an Interrupt or ISO transfer. |
| 286 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 287 | static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 288 | { |
| 289 | return hs_ep->periodic; |
| 290 | } |
| 291 | |
| 292 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 293 | * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 294 | * @hsotg: The device state. |
| 295 | * @hs_ep: The endpoint for the request |
| 296 | * @hs_req: The request being processed. |
| 297 | * |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 298 | * This is the reverse of dwc2_hsotg_map_dma(), called for the completion |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 299 | * of a request to ensure the buffer is ready for access by the caller. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 300 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 301 | static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg, |
| 302 | struct dwc2_hsotg_ep *hs_ep, |
| 303 | struct dwc2_hsotg_req *hs_req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 304 | { |
| 305 | struct usb_request *req = &hs_req->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 306 | |
| 307 | /* ignore this if we're not moving any data */ |
| 308 | if (hs_req->req.length == 0) |
| 309 | return; |
| 310 | |
Jingoo Han | 17d966a | 2013-05-11 21:14:00 +0900 | [diff] [blame] | 311 | usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 315 | * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 316 | * @hsotg: The controller state. |
| 317 | * @hs_ep: The endpoint we're going to write for. |
| 318 | * @hs_req: The request to write data for. |
| 319 | * |
| 320 | * This is called when the TxFIFO has some space in it to hold a new |
| 321 | * transmission and we have something to give it. The actual setup of |
| 322 | * the data size is done elsewhere, so all we have to do is to actually |
| 323 | * write the data. |
| 324 | * |
| 325 | * The return value is zero if there is more space (or nothing was done) |
| 326 | * otherwise -ENOSPC is returned if the FIFO space was used up. |
| 327 | * |
| 328 | * This routine is only needed for PIO |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 329 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 330 | static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg, |
| 331 | struct dwc2_hsotg_ep *hs_ep, |
| 332 | struct dwc2_hsotg_req *hs_req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 333 | { |
| 334 | bool periodic = is_ep_periodic(hs_ep); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 335 | u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 336 | int buf_pos = hs_req->req.actual; |
| 337 | int to_write = hs_ep->size_loaded; |
| 338 | void *data; |
| 339 | int can_write; |
| 340 | int pkt_round; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 341 | int max_transfer; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 342 | |
| 343 | to_write -= (buf_pos - hs_ep->last_load); |
| 344 | |
| 345 | /* if there's nothing to write, get out early */ |
| 346 | if (to_write == 0) |
| 347 | return 0; |
| 348 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 349 | if (periodic && !hsotg->dedicated_fifos) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 350 | u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 351 | int size_left; |
| 352 | int size_done; |
| 353 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 354 | /* |
| 355 | * work out how much data was loaded so we can calculate |
| 356 | * how much data is left in the fifo. |
| 357 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 358 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 359 | size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 360 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 361 | /* |
| 362 | * if shared fifo, we cannot write anything until the |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 363 | * previous data has been completely sent. |
| 364 | */ |
| 365 | if (hs_ep->fifo_load != 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 366 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 367 | return -ENOSPC; |
| 368 | } |
| 369 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 370 | dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n", |
| 371 | __func__, size_left, |
| 372 | hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size); |
| 373 | |
| 374 | /* how much of the data has moved */ |
| 375 | size_done = hs_ep->size_loaded - size_left; |
| 376 | |
| 377 | /* how much data is left in the fifo */ |
| 378 | can_write = hs_ep->fifo_load - size_done; |
| 379 | dev_dbg(hsotg->dev, "%s: => can_write1=%d\n", |
| 380 | __func__, can_write); |
| 381 | |
| 382 | can_write = hs_ep->fifo_size - can_write; |
| 383 | dev_dbg(hsotg->dev, "%s: => can_write2=%d\n", |
| 384 | __func__, can_write); |
| 385 | |
| 386 | if (can_write <= 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 387 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 388 | return -ENOSPC; |
| 389 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 390 | } else if (hsotg->dedicated_fifos && hs_ep->index != 0) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 391 | can_write = dwc2_readl(hsotg->regs + DTXFSTS(hs_ep->index)); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 392 | |
| 393 | can_write &= 0xffff; |
| 394 | can_write *= 4; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 395 | } else { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 396 | if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 397 | dev_dbg(hsotg->dev, |
| 398 | "%s: no queue slots available (0x%08x)\n", |
| 399 | __func__, gnptxsts); |
| 400 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 401 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 402 | return -ENOSPC; |
| 403 | } |
| 404 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 405 | can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts); |
Ben Dooks | 679f9b7 | 2010-07-19 09:40:41 +0100 | [diff] [blame] | 406 | can_write *= 4; /* fifo size is in 32bit quantities. */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 409 | max_transfer = hs_ep->ep.maxpacket * hs_ep->mc; |
| 410 | |
| 411 | dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n", |
| 412 | __func__, gnptxsts, can_write, to_write, max_transfer); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 413 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 414 | /* |
| 415 | * limit to 512 bytes of data, it seems at least on the non-periodic |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 416 | * FIFO, requests of >512 cause the endpoint to get stuck with a |
| 417 | * fragment of the end of the transfer in it. |
| 418 | */ |
Robert Baldyga | 811f330 | 2013-09-24 11:24:28 +0200 | [diff] [blame] | 419 | if (can_write > 512 && !periodic) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 420 | can_write = 512; |
| 421 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 422 | /* |
| 423 | * limit the write to one max-packet size worth of data, but allow |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 424 | * the transfer to return that it did not run out of fifo space |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 425 | * doing it. |
| 426 | */ |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 427 | if (to_write > max_transfer) { |
| 428 | to_write = max_transfer; |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 429 | |
Robert Baldyga | 5cb2ff0 | 2013-09-19 11:50:18 +0200 | [diff] [blame] | 430 | /* it's needed only when we do not use dedicated fifos */ |
| 431 | if (!hsotg->dedicated_fifos) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 432 | dwc2_hsotg_en_gsint(hsotg, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 433 | periodic ? GINTSTS_PTXFEMP : |
| 434 | GINTSTS_NPTXFEMP); |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 437 | /* see if we can write data */ |
| 438 | |
| 439 | if (to_write > can_write) { |
| 440 | to_write = can_write; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 441 | pkt_round = to_write % max_transfer; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 442 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 443 | /* |
| 444 | * Round the write down to an |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 445 | * exact number of packets. |
| 446 | * |
| 447 | * Note, we do not currently check to see if we can ever |
| 448 | * write a full packet or not to the FIFO. |
| 449 | */ |
| 450 | |
| 451 | if (pkt_round) |
| 452 | to_write -= pkt_round; |
| 453 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 454 | /* |
| 455 | * enable correct FIFO interrupt to alert us when there |
| 456 | * is more room left. |
| 457 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 458 | |
Robert Baldyga | 5cb2ff0 | 2013-09-19 11:50:18 +0200 | [diff] [blame] | 459 | /* it's needed only when we do not use dedicated fifos */ |
| 460 | if (!hsotg->dedicated_fifos) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 461 | dwc2_hsotg_en_gsint(hsotg, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 462 | periodic ? GINTSTS_PTXFEMP : |
| 463 | GINTSTS_NPTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n", |
| 467 | to_write, hs_req->req.length, can_write, buf_pos); |
| 468 | |
| 469 | if (to_write <= 0) |
| 470 | return -ENOSPC; |
| 471 | |
| 472 | hs_req->req.actual = buf_pos + to_write; |
| 473 | hs_ep->total_data += to_write; |
| 474 | |
| 475 | if (periodic) |
| 476 | hs_ep->fifo_load += to_write; |
| 477 | |
| 478 | to_write = DIV_ROUND_UP(to_write, 4); |
| 479 | data = hs_req->req.buf + buf_pos; |
| 480 | |
Matt Porter | 1a7ed5b | 2014-02-03 10:29:09 -0500 | [diff] [blame] | 481 | iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 482 | |
| 483 | return (to_write >= can_write) ? -ENOSPC : 0; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * get_ep_limit - get the maximum data legnth for this endpoint |
| 488 | * @hs_ep: The endpoint |
| 489 | * |
| 490 | * Return the maximum data that can be queued in one go on a given endpoint |
| 491 | * so that transfers that are too long can be split. |
| 492 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 493 | static unsigned get_ep_limit(struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 494 | { |
| 495 | int index = hs_ep->index; |
| 496 | unsigned maxsize; |
| 497 | unsigned maxpkt; |
| 498 | |
| 499 | if (index != 0) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 500 | maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1; |
| 501 | maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 502 | } else { |
Ben Dooks | b05ca58 | 2010-07-19 09:40:48 +0100 | [diff] [blame] | 503 | maxsize = 64+64; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 504 | if (hs_ep->dir_in) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 505 | maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 506 | else |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 507 | maxpkt = 2; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /* we made the constant loading easier above by using +1 */ |
| 511 | maxpkt--; |
| 512 | maxsize--; |
| 513 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 514 | /* |
| 515 | * constrain by packet count if maxpkts*pktsize is greater |
| 516 | * than the length register size. |
| 517 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 518 | |
| 519 | if ((maxpkt * hs_ep->ep.maxpacket) < maxsize) |
| 520 | maxsize = maxpkt * hs_ep->ep.maxpacket; |
| 521 | |
| 522 | return maxsize; |
| 523 | } |
| 524 | |
| 525 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 526 | * dwc2_hsotg_start_req - start a USB request from an endpoint's queue |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 527 | * @hsotg: The controller state. |
| 528 | * @hs_ep: The endpoint to process a request for |
| 529 | * @hs_req: The request to start. |
| 530 | * @continuing: True if we are doing more for the current request. |
| 531 | * |
| 532 | * Start the given request running by setting the endpoint registers |
| 533 | * appropriately, and writing any data to the FIFOs. |
| 534 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 535 | static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg, |
| 536 | struct dwc2_hsotg_ep *hs_ep, |
| 537 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 538 | bool continuing) |
| 539 | { |
| 540 | struct usb_request *ureq = &hs_req->req; |
| 541 | int index = hs_ep->index; |
| 542 | int dir_in = hs_ep->dir_in; |
| 543 | u32 epctrl_reg; |
| 544 | u32 epsize_reg; |
| 545 | u32 epsize; |
| 546 | u32 ctrl; |
| 547 | unsigned length; |
| 548 | unsigned packets; |
| 549 | unsigned maxreq; |
| 550 | |
| 551 | if (index != 0) { |
| 552 | if (hs_ep->req && !continuing) { |
| 553 | dev_err(hsotg->dev, "%s: active request\n", __func__); |
| 554 | WARN_ON(1); |
| 555 | return; |
| 556 | } else if (hs_ep->req != hs_req && continuing) { |
| 557 | dev_err(hsotg->dev, |
| 558 | "%s: continue different req\n", __func__); |
| 559 | WARN_ON(1); |
| 560 | return; |
| 561 | } |
| 562 | } |
| 563 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 564 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 565 | epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 566 | |
| 567 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 568 | __func__, dwc2_readl(hsotg->regs + epctrl_reg), index, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 569 | hs_ep->dir_in ? "in" : "out"); |
| 570 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 571 | /* If endpoint is stalled, we will restart request later */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 572 | ctrl = dwc2_readl(hsotg->regs + epctrl_reg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 573 | |
Mian Yousaf Kaukab | b2d4c54 | 2015-09-29 12:08:22 +0200 | [diff] [blame] | 574 | if (index && ctrl & DXEPCTL_STALL) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 575 | dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index); |
| 576 | return; |
| 577 | } |
| 578 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 579 | length = ureq->length - ureq->actual; |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 580 | dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n", |
| 581 | ureq->length, ureq->actual); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 582 | |
| 583 | maxreq = get_ep_limit(hs_ep); |
| 584 | if (length > maxreq) { |
| 585 | int round = maxreq % hs_ep->ep.maxpacket; |
| 586 | |
| 587 | dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n", |
| 588 | __func__, length, maxreq, round); |
| 589 | |
| 590 | /* round down to multiple of packets */ |
| 591 | if (round) |
| 592 | maxreq -= round; |
| 593 | |
| 594 | length = maxreq; |
| 595 | } |
| 596 | |
| 597 | if (length) |
| 598 | packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket); |
| 599 | else |
| 600 | packets = 1; /* send one packet if length is zero. */ |
| 601 | |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 602 | if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) { |
| 603 | dev_err(hsotg->dev, "req length > maxpacket*mc\n"); |
| 604 | return; |
| 605 | } |
| 606 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 607 | if (dir_in && index != 0) |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 608 | if (hs_ep->isochronous) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 609 | epsize = DXEPTSIZ_MC(packets); |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 610 | else |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 611 | epsize = DXEPTSIZ_MC(1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 612 | else |
| 613 | epsize = 0; |
| 614 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 615 | /* |
| 616 | * zero length packet should be programmed on its own and should not |
| 617 | * be counted in DIEPTSIZ.PktCnt with other packets. |
| 618 | */ |
| 619 | if (dir_in && ureq->zero && !continuing) { |
| 620 | /* Test if zlp is actually required. */ |
| 621 | if ((ureq->length >= hs_ep->ep.maxpacket) && |
| 622 | !(ureq->length % hs_ep->ep.maxpacket)) |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 623 | hs_ep->send_zlp = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 624 | } |
| 625 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 626 | epsize |= DXEPTSIZ_PKTCNT(packets); |
| 627 | epsize |= DXEPTSIZ_XFERSIZE(length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 628 | |
| 629 | dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n", |
| 630 | __func__, packets, length, ureq->length, epsize, epsize_reg); |
| 631 | |
| 632 | /* store the request as the current one we're doing */ |
| 633 | hs_ep->req = hs_req; |
| 634 | |
| 635 | /* write size / packets */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 636 | dwc2_writel(epsize, hsotg->regs + epsize_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 637 | |
Anton Tikhomirov | db1d8ba | 2012-03-06 14:09:19 +0900 | [diff] [blame] | 638 | if (using_dma(hsotg) && !continuing) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 639 | unsigned int dma_reg; |
| 640 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 641 | /* |
| 642 | * write DMA address to control register, buffer already |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 643 | * synced by dwc2_hsotg_ep_queue(). |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 644 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 645 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 646 | dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 647 | dwc2_writel(ureq->dma, hsotg->regs + dma_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 648 | |
Fabio Estevam | 0cc4cf6 | 2014-04-29 00:49:42 -0300 | [diff] [blame] | 649 | dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n", |
Jingoo Han | 8b3bc14 | 2014-02-04 14:25:29 +0900 | [diff] [blame] | 650 | __func__, &ureq->dma, dma_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 653 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 654 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 655 | dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state); |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 656 | |
| 657 | /* For Setup request do not clear NAK */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 658 | if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP)) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 659 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 660 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 661 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 662 | dwc2_writel(ctrl, hsotg->regs + epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 663 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 664 | /* |
| 665 | * set these, it seems that DMA support increments past the end |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 666 | * of the packet buffer so we need to calculate the length from |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 667 | * this information. |
| 668 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 669 | hs_ep->size_loaded = length; |
| 670 | hs_ep->last_load = ureq->actual; |
| 671 | |
| 672 | if (dir_in && !using_dma(hsotg)) { |
| 673 | /* set these anyway, we may need them for non-periodic in */ |
| 674 | hs_ep->fifo_load = 0; |
| 675 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 676 | dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 679 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 680 | * Note, trying to clear the NAK here causes problems with transmit |
| 681 | * on the S3C6400 ending up with the TXFIFO becoming full. |
| 682 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 683 | |
| 684 | /* check ep is enabled */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 685 | if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA)) |
Mian Yousaf Kaukab | 1a0ed86 | 2015-01-09 13:39:00 +0100 | [diff] [blame] | 686 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 687 | "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 688 | index, dwc2_readl(hsotg->regs + epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 689 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 690 | dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 691 | __func__, dwc2_readl(hsotg->regs + epctrl_reg)); |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 692 | |
| 693 | /* enable ep interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 694 | dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 698 | * dwc2_hsotg_map_dma - map the DMA memory being used for the request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 699 | * @hsotg: The device state. |
| 700 | * @hs_ep: The endpoint the request is on. |
| 701 | * @req: The request being processed. |
| 702 | * |
| 703 | * We've been asked to queue a request, so ensure that the memory buffer |
| 704 | * is correctly setup for DMA. If we've been passed an extant DMA address |
| 705 | * then ensure the buffer has been synced to memory. If our buffer has no |
| 706 | * DMA memory, then we map the memory and mark our request to allow us to |
| 707 | * cleanup on completion. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 708 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 709 | static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg, |
| 710 | struct dwc2_hsotg_ep *hs_ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 711 | struct usb_request *req) |
| 712 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 713 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 714 | int ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 715 | |
| 716 | /* if the length is zero, ignore the DMA data */ |
| 717 | if (hs_req->req.length == 0) |
| 718 | return 0; |
| 719 | |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 720 | ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in); |
| 721 | if (ret) |
| 722 | goto dma_error; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 723 | |
| 724 | return 0; |
| 725 | |
| 726 | dma_error: |
| 727 | dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n", |
| 728 | __func__, req->buf, req->length); |
| 729 | |
| 730 | return -EIO; |
| 731 | } |
| 732 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 733 | static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg, |
| 734 | struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 735 | { |
| 736 | void *req_buf = hs_req->req.buf; |
| 737 | |
| 738 | /* If dma is not being used or buffer is aligned */ |
| 739 | if (!using_dma(hsotg) || !((long)req_buf & 3)) |
| 740 | return 0; |
| 741 | |
| 742 | WARN_ON(hs_req->saved_req_buf); |
| 743 | |
| 744 | dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__, |
| 745 | hs_ep->ep.name, req_buf, hs_req->req.length); |
| 746 | |
| 747 | hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC); |
| 748 | if (!hs_req->req.buf) { |
| 749 | hs_req->req.buf = req_buf; |
| 750 | dev_err(hsotg->dev, |
| 751 | "%s: unable to allocate memory for bounce buffer\n", |
| 752 | __func__); |
| 753 | return -ENOMEM; |
| 754 | } |
| 755 | |
| 756 | /* Save actual buffer */ |
| 757 | hs_req->saved_req_buf = req_buf; |
| 758 | |
| 759 | if (hs_ep->dir_in) |
| 760 | memcpy(hs_req->req.buf, req_buf, hs_req->req.length); |
| 761 | return 0; |
| 762 | } |
| 763 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 764 | static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg, |
| 765 | struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 766 | { |
| 767 | /* If dma is not being used or buffer was aligned */ |
| 768 | if (!using_dma(hsotg) || !hs_req->saved_req_buf) |
| 769 | return; |
| 770 | |
| 771 | dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__, |
| 772 | hs_ep->ep.name, hs_req->req.status, hs_req->req.actual); |
| 773 | |
| 774 | /* Copy data from bounce buffer on successful out transfer */ |
| 775 | if (!hs_ep->dir_in && !hs_req->req.status) |
| 776 | memcpy(hs_req->saved_req_buf, hs_req->req.buf, |
| 777 | hs_req->req.actual); |
| 778 | |
| 779 | /* Free bounce buffer */ |
| 780 | kfree(hs_req->req.buf); |
| 781 | |
| 782 | hs_req->req.buf = hs_req->saved_req_buf; |
| 783 | hs_req->saved_req_buf = NULL; |
| 784 | } |
| 785 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 786 | static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 787 | gfp_t gfp_flags) |
| 788 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 789 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 790 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 791 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 792 | bool first; |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 793 | int ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 794 | |
| 795 | dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n", |
| 796 | ep->name, req, req->length, req->buf, req->no_interrupt, |
| 797 | req->zero, req->short_not_ok); |
| 798 | |
Gregory Herrero | 7ababa9 | 2015-04-29 22:09:08 +0200 | [diff] [blame] | 799 | /* Prevent new request submission when controller is suspended */ |
| 800 | if (hs->lx_state == DWC2_L2) { |
| 801 | dev_dbg(hs->dev, "%s: don't submit request while suspended\n", |
| 802 | __func__); |
| 803 | return -EAGAIN; |
| 804 | } |
| 805 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 806 | /* initialise status of the request */ |
| 807 | INIT_LIST_HEAD(&hs_req->queue); |
| 808 | req->actual = 0; |
| 809 | req->status = -EINPROGRESS; |
| 810 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 811 | ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 812 | if (ret) |
| 813 | return ret; |
| 814 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 815 | /* if we're using DMA, sync the buffers as necessary */ |
| 816 | if (using_dma(hs)) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 817 | ret = dwc2_hsotg_map_dma(hs, hs_ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 818 | if (ret) |
| 819 | return ret; |
| 820 | } |
| 821 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 822 | first = list_empty(&hs_ep->queue); |
| 823 | list_add_tail(&hs_req->queue, &hs_ep->queue); |
| 824 | |
| 825 | if (first) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 826 | dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 827 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 828 | return 0; |
| 829 | } |
| 830 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 831 | static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req, |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 832 | gfp_t gfp_flags) |
| 833 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 834 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 835 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 836 | unsigned long flags = 0; |
| 837 | int ret = 0; |
| 838 | |
| 839 | spin_lock_irqsave(&hs->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 840 | ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 841 | spin_unlock_irqrestore(&hs->lock, flags); |
| 842 | |
| 843 | return ret; |
| 844 | } |
| 845 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 846 | static void dwc2_hsotg_ep_free_request(struct usb_ep *ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 847 | struct usb_request *req) |
| 848 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 849 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 850 | |
| 851 | kfree(hs_req); |
| 852 | } |
| 853 | |
| 854 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 855 | * dwc2_hsotg_complete_oursetup - setup completion callback |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 856 | * @ep: The endpoint the request was on. |
| 857 | * @req: The request completed. |
| 858 | * |
| 859 | * Called on completion of any requests the driver itself |
| 860 | * submitted that need cleaning up. |
| 861 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 862 | static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 863 | struct usb_request *req) |
| 864 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 865 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 866 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 867 | |
| 868 | dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req); |
| 869 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 870 | dwc2_hsotg_ep_free_request(ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /** |
| 874 | * ep_from_windex - convert control wIndex value to endpoint |
| 875 | * @hsotg: The driver state. |
| 876 | * @windex: The control request wIndex field (in host order). |
| 877 | * |
| 878 | * Convert the given wIndex into a pointer to an driver endpoint |
| 879 | * structure, or return NULL if it is not a valid endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 880 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 881 | static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 882 | u32 windex) |
| 883 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 884 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 885 | int dir = (windex & USB_DIR_IN) ? 1 : 0; |
| 886 | int idx = windex & 0x7F; |
| 887 | |
| 888 | if (windex >= 0x100) |
| 889 | return NULL; |
| 890 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 891 | if (idx > hsotg->num_of_eps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 892 | return NULL; |
| 893 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 894 | ep = index_to_ep(hsotg, idx, dir); |
| 895 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 896 | if (idx && ep->dir_in != dir) |
| 897 | return NULL; |
| 898 | |
| 899 | return ep; |
| 900 | } |
| 901 | |
| 902 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 903 | * dwc2_hsotg_set_test_mode - Enable usb Test Modes |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 904 | * @hsotg: The driver state. |
| 905 | * @testmode: requested usb test mode |
| 906 | * Enable usb Test Mode requested by the Host. |
| 907 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 908 | int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode) |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 909 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 910 | int dctl = dwc2_readl(hsotg->regs + DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 911 | |
| 912 | dctl &= ~DCTL_TSTCTL_MASK; |
| 913 | switch (testmode) { |
| 914 | case TEST_J: |
| 915 | case TEST_K: |
| 916 | case TEST_SE0_NAK: |
| 917 | case TEST_PACKET: |
| 918 | case TEST_FORCE_EN: |
| 919 | dctl |= testmode << DCTL_TSTCTL_SHIFT; |
| 920 | break; |
| 921 | default: |
| 922 | return -EINVAL; |
| 923 | } |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 924 | dwc2_writel(dctl, hsotg->regs + DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 929 | * dwc2_hsotg_send_reply - send reply to control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 930 | * @hsotg: The device state |
| 931 | * @ep: Endpoint 0 |
| 932 | * @buff: Buffer for request |
| 933 | * @length: Length of reply. |
| 934 | * |
| 935 | * Create a request and queue it on the given endpoint. This is useful as |
| 936 | * an internal method of sending replies to certain control requests, etc. |
| 937 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 938 | static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg, |
| 939 | struct dwc2_hsotg_ep *ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 940 | void *buff, |
| 941 | int length) |
| 942 | { |
| 943 | struct usb_request *req; |
| 944 | int ret; |
| 945 | |
| 946 | dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length); |
| 947 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 948 | req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 949 | hsotg->ep0_reply = req; |
| 950 | if (!req) { |
| 951 | dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__); |
| 952 | return -ENOMEM; |
| 953 | } |
| 954 | |
| 955 | req->buf = hsotg->ep0_buff; |
| 956 | req->length = length; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 957 | /* |
| 958 | * zero flag is for sending zlp in DATA IN stage. It has no impact on |
| 959 | * STATUS stage. |
| 960 | */ |
| 961 | req->zero = 0; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 962 | req->complete = dwc2_hsotg_complete_oursetup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 963 | |
| 964 | if (length) |
| 965 | memcpy(req->buf, buff, length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 966 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 967 | ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 968 | if (ret) { |
| 969 | dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__); |
| 970 | return ret; |
| 971 | } |
| 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 977 | * dwc2_hsotg_process_req_status - process request GET_STATUS |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 978 | * @hsotg: The device state |
| 979 | * @ctrl: USB control request |
| 980 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 981 | static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 982 | struct usb_ctrlrequest *ctrl) |
| 983 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 984 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 985 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 986 | __le16 reply; |
| 987 | int ret; |
| 988 | |
| 989 | dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__); |
| 990 | |
| 991 | if (!ep0->dir_in) { |
| 992 | dev_warn(hsotg->dev, "%s: direction out?\n", __func__); |
| 993 | return -EINVAL; |
| 994 | } |
| 995 | |
| 996 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 997 | case USB_RECIP_DEVICE: |
| 998 | reply = cpu_to_le16(0); /* bit 0 => self powered, |
| 999 | * bit 1 => remote wakeup */ |
| 1000 | break; |
| 1001 | |
| 1002 | case USB_RECIP_INTERFACE: |
| 1003 | /* currently, the data result should be zero */ |
| 1004 | reply = cpu_to_le16(0); |
| 1005 | break; |
| 1006 | |
| 1007 | case USB_RECIP_ENDPOINT: |
| 1008 | ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex)); |
| 1009 | if (!ep) |
| 1010 | return -ENOENT; |
| 1011 | |
| 1012 | reply = cpu_to_le16(ep->halted ? 1 : 0); |
| 1013 | break; |
| 1014 | |
| 1015 | default: |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | if (le16_to_cpu(ctrl->wLength) != 2) |
| 1020 | return -EINVAL; |
| 1021 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1022 | ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1023 | if (ret) { |
| 1024 | dev_err(hsotg->dev, "%s: failed to send reply\n", __func__); |
| 1025 | return ret; |
| 1026 | } |
| 1027 | |
| 1028 | return 1; |
| 1029 | } |
| 1030 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1031 | static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1032 | |
| 1033 | /** |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1034 | * get_ep_head - return the first request on the endpoint |
| 1035 | * @hs_ep: The controller endpoint to get |
| 1036 | * |
| 1037 | * Get the first request on the endpoint. |
| 1038 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1039 | static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep) |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1040 | { |
| 1041 | if (list_empty(&hs_ep->queue)) |
| 1042 | return NULL; |
| 1043 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1044 | return list_first_entry(&hs_ep->queue, struct dwc2_hsotg_req, queue); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | /** |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame^] | 1048 | * dwc2_gadget_start_next_request - Starts next request from ep queue |
| 1049 | * @hs_ep: Endpoint structure |
| 1050 | * |
| 1051 | * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked |
| 1052 | * in its handler. Hence we need to unmask it here to be able to do |
| 1053 | * resynchronization. |
| 1054 | */ |
| 1055 | static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep) |
| 1056 | { |
| 1057 | u32 mask; |
| 1058 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 1059 | int dir_in = hs_ep->dir_in; |
| 1060 | struct dwc2_hsotg_req *hs_req; |
| 1061 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 1062 | |
| 1063 | if (!list_empty(&hs_ep->queue)) { |
| 1064 | hs_req = get_ep_head(hs_ep); |
| 1065 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false); |
| 1066 | return; |
| 1067 | } |
| 1068 | if (!hs_ep->isochronous) |
| 1069 | return; |
| 1070 | |
| 1071 | if (dir_in) { |
| 1072 | dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n", |
| 1073 | __func__); |
| 1074 | } else { |
| 1075 | dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n", |
| 1076 | __func__); |
| 1077 | mask = dwc2_readl(hsotg->regs + epmsk_reg); |
| 1078 | mask |= DOEPMSK_OUTTKNEPDISMSK; |
| 1079 | dwc2_writel(mask, hsotg->regs + epmsk_reg); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1084 | * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1085 | * @hsotg: The device state |
| 1086 | * @ctrl: USB control request |
| 1087 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1088 | static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1089 | struct usb_ctrlrequest *ctrl) |
| 1090 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1091 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 1092 | struct dwc2_hsotg_req *hs_req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1093 | bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1094 | struct dwc2_hsotg_ep *ep; |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1095 | int ret; |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1096 | bool halted; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1097 | u32 recip; |
| 1098 | u32 wValue; |
| 1099 | u32 wIndex; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1100 | |
| 1101 | dev_dbg(hsotg->dev, "%s: %s_FEATURE\n", |
| 1102 | __func__, set ? "SET" : "CLEAR"); |
| 1103 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1104 | wValue = le16_to_cpu(ctrl->wValue); |
| 1105 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 1106 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 1107 | |
| 1108 | switch (recip) { |
| 1109 | case USB_RECIP_DEVICE: |
| 1110 | switch (wValue) { |
| 1111 | case USB_DEVICE_TEST_MODE: |
| 1112 | if ((wIndex & 0xff) != 0) |
| 1113 | return -EINVAL; |
| 1114 | if (!set) |
| 1115 | return -EINVAL; |
| 1116 | |
| 1117 | hsotg->test_mode = wIndex >> 8; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1118 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1119 | if (ret) { |
| 1120 | dev_err(hsotg->dev, |
| 1121 | "%s: failed to send reply\n", __func__); |
| 1122 | return ret; |
| 1123 | } |
| 1124 | break; |
| 1125 | default: |
| 1126 | return -ENOENT; |
| 1127 | } |
| 1128 | break; |
| 1129 | |
| 1130 | case USB_RECIP_ENDPOINT: |
| 1131 | ep = ep_from_windex(hsotg, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1132 | if (!ep) { |
| 1133 | dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n", |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1134 | __func__, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1135 | return -ENOENT; |
| 1136 | } |
| 1137 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1138 | switch (wValue) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1139 | case USB_ENDPOINT_HALT: |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1140 | halted = ep->halted; |
| 1141 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1142 | dwc2_hsotg_ep_sethalt(&ep->ep, set, true); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1143 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1144 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1145 | if (ret) { |
| 1146 | dev_err(hsotg->dev, |
| 1147 | "%s: failed to send reply\n", __func__); |
| 1148 | return ret; |
| 1149 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1150 | |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1151 | /* |
| 1152 | * we have to complete all requests for ep if it was |
| 1153 | * halted, and the halt was cleared by CLEAR_FEATURE |
| 1154 | */ |
| 1155 | |
| 1156 | if (!set && halted) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1157 | /* |
| 1158 | * If we have request in progress, |
| 1159 | * then complete it |
| 1160 | */ |
| 1161 | if (ep->req) { |
| 1162 | hs_req = ep->req; |
| 1163 | ep->req = NULL; |
| 1164 | list_del_init(&hs_req->queue); |
Gregory Herrero | c00dd4a | 2015-01-30 09:09:27 +0100 | [diff] [blame] | 1165 | if (hs_req->req.complete) { |
| 1166 | spin_unlock(&hsotg->lock); |
| 1167 | usb_gadget_giveback_request( |
| 1168 | &ep->ep, &hs_req->req); |
| 1169 | spin_lock(&hsotg->lock); |
| 1170 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | /* If we have pending request, then start it */ |
Gregory Herrero | c00dd4a | 2015-01-30 09:09:27 +0100 | [diff] [blame] | 1174 | if (!ep->req) { |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame^] | 1175 | dwc2_gadget_start_next_request(ep); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1179 | break; |
| 1180 | |
| 1181 | default: |
| 1182 | return -ENOENT; |
| 1183 | } |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1184 | break; |
| 1185 | default: |
| 1186 | return -ENOENT; |
| 1187 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1188 | return 1; |
| 1189 | } |
| 1190 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1191 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg); |
Robert Baldyga | ab93e01 | 2013-09-19 11:50:17 +0200 | [diff] [blame] | 1192 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1193 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1194 | * dwc2_hsotg_stall_ep0 - stall ep0 |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1195 | * @hsotg: The device state |
| 1196 | * |
| 1197 | * Set stall for ep0 as response for setup request. |
| 1198 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1199 | static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg) |
Jingoo Han | e9ebe7c | 2014-06-03 22:14:56 +0900 | [diff] [blame] | 1200 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1201 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1202 | u32 reg; |
| 1203 | u32 ctrl; |
| 1204 | |
| 1205 | dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in); |
| 1206 | reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0; |
| 1207 | |
| 1208 | /* |
| 1209 | * DxEPCTL_Stall will be cleared by EP once it has |
| 1210 | * taken effect, so no need to clear later. |
| 1211 | */ |
| 1212 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1213 | ctrl = dwc2_readl(hsotg->regs + reg); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1214 | ctrl |= DXEPCTL_STALL; |
| 1215 | ctrl |= DXEPCTL_CNAK; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1216 | dwc2_writel(ctrl, hsotg->regs + reg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1217 | |
| 1218 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1219 | "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1220 | ctrl, reg, dwc2_readl(hsotg->regs + reg)); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1221 | |
| 1222 | /* |
| 1223 | * complete won't be called, so we enqueue |
| 1224 | * setup request here |
| 1225 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1226 | dwc2_hsotg_enqueue_setup(hsotg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1230 | * dwc2_hsotg_process_control - process a control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1231 | * @hsotg: The device state |
| 1232 | * @ctrl: The control request received |
| 1233 | * |
| 1234 | * The controller has received the SETUP phase of a control request, and |
| 1235 | * needs to work out what to do next (and whether to pass it on to the |
| 1236 | * gadget driver). |
| 1237 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1238 | static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1239 | struct usb_ctrlrequest *ctrl) |
| 1240 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1241 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1242 | int ret = 0; |
| 1243 | u32 dcfg; |
| 1244 | |
Mian Yousaf Kaukab | e525e74 | 2015-09-29 12:08:23 +0200 | [diff] [blame] | 1245 | dev_dbg(hsotg->dev, |
| 1246 | "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n", |
| 1247 | ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, |
| 1248 | ctrl->wIndex, ctrl->wLength); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1249 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1250 | if (ctrl->wLength == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1251 | ep0->dir_in = 1; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1252 | hsotg->ep0_state = DWC2_EP0_STATUS_IN; |
| 1253 | } else if (ctrl->bRequestType & USB_DIR_IN) { |
| 1254 | ep0->dir_in = 1; |
| 1255 | hsotg->ep0_state = DWC2_EP0_DATA_IN; |
| 1256 | } else { |
| 1257 | ep0->dir_in = 0; |
| 1258 | hsotg->ep0_state = DWC2_EP0_DATA_OUT; |
| 1259 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1260 | |
| 1261 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { |
| 1262 | switch (ctrl->bRequest) { |
| 1263 | case USB_REQ_SET_ADDRESS: |
Mian Yousaf Kaukab | 6d713c1 | 2015-01-09 13:39:10 +0100 | [diff] [blame] | 1264 | hsotg->connected = 1; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1265 | dcfg = dwc2_readl(hsotg->regs + DCFG); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1266 | dcfg &= ~DCFG_DEVADDR_MASK; |
Paul Zimmerman | d5dbd3f | 2014-04-25 14:18:13 -0700 | [diff] [blame] | 1267 | dcfg |= (le16_to_cpu(ctrl->wValue) << |
| 1268 | DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1269 | dwc2_writel(dcfg, hsotg->regs + DCFG); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1270 | |
| 1271 | dev_info(hsotg->dev, "new address %d\n", ctrl->wValue); |
| 1272 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1273 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1274 | return; |
| 1275 | |
| 1276 | case USB_REQ_GET_STATUS: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1277 | ret = dwc2_hsotg_process_req_status(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1278 | break; |
| 1279 | |
| 1280 | case USB_REQ_CLEAR_FEATURE: |
| 1281 | case USB_REQ_SET_FEATURE: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1282 | ret = dwc2_hsotg_process_req_feature(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1283 | break; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | /* as a fallback, try delivering it to the driver to deal with */ |
| 1288 | |
| 1289 | if (ret == 0 && hsotg->driver) { |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1290 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1291 | ret = hsotg->driver->setup(&hsotg->gadget, ctrl); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1292 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1293 | if (ret < 0) |
| 1294 | dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret); |
| 1295 | } |
| 1296 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1297 | /* |
| 1298 | * the request is either unhandlable, or is not formatted correctly |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1299 | * so respond with a STALL for the status stage to indicate failure. |
| 1300 | */ |
| 1301 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1302 | if (ret < 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1303 | dwc2_hsotg_stall_ep0(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1304 | } |
| 1305 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1306 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1307 | * dwc2_hsotg_complete_setup - completion of a setup transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1308 | * @ep: The endpoint the request was on. |
| 1309 | * @req: The request completed. |
| 1310 | * |
| 1311 | * Called on completion of any requests the driver itself submitted for |
| 1312 | * EP0 setup packets |
| 1313 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1314 | static void dwc2_hsotg_complete_setup(struct usb_ep *ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1315 | struct usb_request *req) |
| 1316 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1317 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1318 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1319 | |
| 1320 | if (req->status < 0) { |
| 1321 | dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status); |
| 1322 | return; |
| 1323 | } |
| 1324 | |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1325 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1326 | if (req->actual == 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1327 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1328 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1329 | dwc2_hsotg_process_control(hsotg, req->buf); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1330 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1334 | * dwc2_hsotg_enqueue_setup - start a request for EP0 packets |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1335 | * @hsotg: The device state. |
| 1336 | * |
| 1337 | * Enqueue a request on EP0 if necessary to received any SETUP packets |
| 1338 | * received from the host. |
| 1339 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1340 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1341 | { |
| 1342 | struct usb_request *req = hsotg->ctrl_req; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1343 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1344 | int ret; |
| 1345 | |
| 1346 | dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__); |
| 1347 | |
| 1348 | req->zero = 0; |
| 1349 | req->length = 8; |
| 1350 | req->buf = hsotg->ctrl_buff; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1351 | req->complete = dwc2_hsotg_complete_setup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1352 | |
| 1353 | if (!list_empty(&hs_req->queue)) { |
| 1354 | dev_dbg(hsotg->dev, "%s already queued???\n", __func__); |
| 1355 | return; |
| 1356 | } |
| 1357 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1358 | hsotg->eps_out[0]->dir_in = 0; |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 1359 | hsotg->eps_out[0]->send_zlp = 0; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1360 | hsotg->ep0_state = DWC2_EP0_SETUP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1361 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1362 | ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1363 | if (ret < 0) { |
| 1364 | dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret); |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1365 | /* |
| 1366 | * Don't think there's much we can do other than watch the |
| 1367 | * driver fail. |
| 1368 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1372 | static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg, |
| 1373 | struct dwc2_hsotg_ep *hs_ep) |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1374 | { |
| 1375 | u32 ctrl; |
| 1376 | u8 index = hs_ep->index; |
| 1377 | u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 1378 | u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
| 1379 | |
Mian Yousaf Kaukab | ccb34a9 | 2015-01-30 09:09:34 +0100 | [diff] [blame] | 1380 | if (hs_ep->dir_in) |
| 1381 | dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", |
| 1382 | index); |
| 1383 | else |
| 1384 | dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n", |
| 1385 | index); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1386 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1387 | dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
| 1388 | DXEPTSIZ_XFERSIZE(0), hsotg->regs + |
| 1389 | epsiz_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1390 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1391 | ctrl = dwc2_readl(hsotg->regs + epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1392 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
| 1393 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
| 1394 | ctrl |= DXEPCTL_USBACTEP; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1395 | dwc2_writel(ctrl, hsotg->regs + epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1396 | } |
| 1397 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1398 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1399 | * dwc2_hsotg_complete_request - complete a request given to us |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1400 | * @hsotg: The device state. |
| 1401 | * @hs_ep: The endpoint the request was on. |
| 1402 | * @hs_req: The request to complete. |
| 1403 | * @result: The result code (0 => Ok, otherwise errno) |
| 1404 | * |
| 1405 | * The given request has finished, so call the necessary completion |
| 1406 | * if it has one and then look to see if we can start a new request |
| 1407 | * on the endpoint. |
| 1408 | * |
| 1409 | * Note, expects the ep to already be locked as appropriate. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1410 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1411 | static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg, |
| 1412 | struct dwc2_hsotg_ep *hs_ep, |
| 1413 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1414 | int result) |
| 1415 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1416 | |
| 1417 | if (!hs_req) { |
| 1418 | dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__); |
| 1419 | return; |
| 1420 | } |
| 1421 | |
| 1422 | dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n", |
| 1423 | hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete); |
| 1424 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1425 | /* |
| 1426 | * only replace the status if we've not already set an error |
| 1427 | * from a previous transaction |
| 1428 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1429 | |
| 1430 | if (hs_req->req.status == -EINPROGRESS) |
| 1431 | hs_req->req.status = result; |
| 1432 | |
Yunzhi Li | 44583fe | 2015-09-29 12:25:01 +0200 | [diff] [blame] | 1433 | if (using_dma(hsotg)) |
| 1434 | dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req); |
| 1435 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1436 | dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1437 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1438 | hs_ep->req = NULL; |
| 1439 | list_del_init(&hs_req->queue); |
| 1440 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1441 | /* |
| 1442 | * call the complete request with the locks off, just in case the |
| 1443 | * request tries to queue more work for this endpoint. |
| 1444 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1445 | |
| 1446 | if (hs_req->req.complete) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 1447 | spin_unlock(&hsotg->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 1448 | usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 1449 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1450 | } |
| 1451 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1452 | /* |
| 1453 | * Look to see if there is anything else to do. Note, the completion |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1454 | * of the previous request may have caused a new request to be started |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1455 | * so be careful when doing this. |
| 1456 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1457 | |
| 1458 | if (!hs_ep->req && result >= 0) { |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame^] | 1459 | dwc2_gadget_start_next_request(hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1464 | * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1465 | * @hsotg: The device state. |
| 1466 | * @ep_idx: The endpoint index for the data |
| 1467 | * @size: The size of data in the fifo, in bytes |
| 1468 | * |
| 1469 | * The FIFO status shows there is data to read from the FIFO for a given |
| 1470 | * endpoint, so sort out whether we need to read the data into a request |
| 1471 | * that has been made for that endpoint. |
| 1472 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1473 | static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1474 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1475 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx]; |
| 1476 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1477 | void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1478 | int to_read; |
| 1479 | int max_req; |
| 1480 | int read_ptr; |
| 1481 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 1482 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1483 | if (!hs_req) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1484 | u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1485 | int ptr; |
| 1486 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 1487 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1488 | "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n", |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1489 | __func__, size, ep_idx, epctl); |
| 1490 | |
| 1491 | /* dump the data from the FIFO, we've nothing we can do */ |
| 1492 | for (ptr = 0; ptr < size; ptr += 4) |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1493 | (void)dwc2_readl(fifo); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1494 | |
| 1495 | return; |
| 1496 | } |
| 1497 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1498 | to_read = size; |
| 1499 | read_ptr = hs_req->req.actual; |
| 1500 | max_req = hs_req->req.length - read_ptr; |
| 1501 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 1502 | dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n", |
| 1503 | __func__, to_read, max_req, read_ptr, hs_req->req.length); |
| 1504 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1505 | if (to_read > max_req) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1506 | /* |
| 1507 | * more data appeared than we where willing |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1508 | * to deal with in this request. |
| 1509 | */ |
| 1510 | |
| 1511 | /* currently we don't deal this */ |
| 1512 | WARN_ON_ONCE(1); |
| 1513 | } |
| 1514 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1515 | hs_ep->total_data += to_read; |
| 1516 | hs_req->req.actual += to_read; |
| 1517 | to_read = DIV_ROUND_UP(to_read, 4); |
| 1518 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1519 | /* |
| 1520 | * note, we might over-write the buffer end by 3 bytes depending on |
| 1521 | * alignment of the data. |
| 1522 | */ |
Matt Porter | 1a7ed5b | 2014-02-03 10:29:09 -0500 | [diff] [blame] | 1523 | ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1527 | * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1528 | * @hsotg: The device instance |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1529 | * @dir_in: If IN zlp |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1530 | * |
| 1531 | * Generate a zero-length IN packet request for terminating a SETUP |
| 1532 | * transaction. |
| 1533 | * |
| 1534 | * Note, since we don't write any data to the TxFIFO, then it is |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1535 | * currently believed that we do not need to wait for any space in |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1536 | * the TxFIFO. |
| 1537 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1538 | static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1539 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1540 | /* eps_out[0] is used in both directions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1541 | hsotg->eps_out[0]->dir_in = dir_in; |
| 1542 | hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1543 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1544 | dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1545 | } |
| 1546 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 1547 | static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg, |
| 1548 | u32 epctl_reg) |
| 1549 | { |
| 1550 | u32 ctrl; |
| 1551 | |
| 1552 | ctrl = dwc2_readl(hsotg->regs + epctl_reg); |
| 1553 | if (ctrl & DXEPCTL_EOFRNUM) |
| 1554 | ctrl |= DXEPCTL_SETEVENFR; |
| 1555 | else |
| 1556 | ctrl |= DXEPCTL_SETODDFR; |
| 1557 | dwc2_writel(ctrl, hsotg->regs + epctl_reg); |
| 1558 | } |
| 1559 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1560 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1561 | * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1562 | * @hsotg: The device instance |
| 1563 | * @epnum: The endpoint received from |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1564 | * |
| 1565 | * The RXFIFO has delivered an OutDone event, which means that the data |
| 1566 | * transfer for an OUT endpoint has been completed, either by a short |
| 1567 | * packet or by the finish of a transfer. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1568 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1569 | static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1570 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1571 | u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum)); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1572 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum]; |
| 1573 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1574 | struct usb_request *req = &hs_req->req; |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1575 | unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1576 | int result = 0; |
| 1577 | |
| 1578 | if (!hs_req) { |
| 1579 | dev_dbg(hsotg->dev, "%s: no request active\n", __func__); |
| 1580 | return; |
| 1581 | } |
| 1582 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1583 | if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) { |
| 1584 | dev_dbg(hsotg->dev, "zlp packet received\n"); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1585 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
| 1586 | dwc2_hsotg_enqueue_setup(hsotg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1587 | return; |
| 1588 | } |
| 1589 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1590 | if (using_dma(hsotg)) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1591 | unsigned size_done; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1592 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1593 | /* |
| 1594 | * Calculate the size of the transfer by checking how much |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1595 | * is left in the endpoint size register and then working it |
| 1596 | * out from the amount we loaded for the transfer. |
| 1597 | * |
| 1598 | * We need to do this as DMA pointers are always 32bit aligned |
| 1599 | * so may overshoot/undershoot the transfer. |
| 1600 | */ |
| 1601 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1602 | size_done = hs_ep->size_loaded - size_left; |
| 1603 | size_done += hs_ep->last_load; |
| 1604 | |
| 1605 | req->actual = size_done; |
| 1606 | } |
| 1607 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 1608 | /* if there is more request to do, schedule new transfer */ |
| 1609 | if (req->actual < req->length && size_left == 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1610 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 1611 | return; |
| 1612 | } |
| 1613 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1614 | if (req->actual < req->length && req->short_not_ok) { |
| 1615 | dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n", |
| 1616 | __func__, req->actual, req->length); |
| 1617 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1618 | /* |
| 1619 | * todo - what should we return here? there's no one else |
| 1620 | * even bothering to check the status. |
| 1621 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1622 | } |
| 1623 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1624 | if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) { |
| 1625 | /* Move to STATUS IN */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1626 | dwc2_hsotg_ep0_zlp(hsotg, true); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1627 | return; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1628 | } |
| 1629 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 1630 | /* |
| 1631 | * Slave mode OUT transfers do not go through XferComplete so |
| 1632 | * adjust the ISOC parity here. |
| 1633 | */ |
| 1634 | if (!using_dma(hsotg)) { |
| 1635 | hs_ep->has_correct_parity = 1; |
| 1636 | if (hs_ep->isochronous && hs_ep->interval == 1) |
| 1637 | dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum)); |
| 1638 | } |
| 1639 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1640 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1644 | * dwc2_hsotg_read_frameno - read current frame number |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1645 | * @hsotg: The device instance |
| 1646 | * |
| 1647 | * Return the current frame number |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1648 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1649 | static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1650 | { |
| 1651 | u32 dsts; |
| 1652 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1653 | dsts = dwc2_readl(hsotg->regs + DSTS); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1654 | dsts &= DSTS_SOFFN_MASK; |
| 1655 | dsts >>= DSTS_SOFFN_SHIFT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1656 | |
| 1657 | return dsts; |
| 1658 | } |
| 1659 | |
| 1660 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1661 | * dwc2_hsotg_handle_rx - RX FIFO has data |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1662 | * @hsotg: The device instance |
| 1663 | * |
| 1664 | * The IRQ handler has detected that the RX FIFO has some data in it |
| 1665 | * that requires processing, so find out what is in there and do the |
| 1666 | * appropriate read. |
| 1667 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1668 | * The RXFIFO is a true FIFO, the packets coming out are still in packet |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1669 | * chunks, so if you have x packets received on an endpoint you'll get x |
| 1670 | * FIFO events delivered, each with a packet's worth of data in it. |
| 1671 | * |
| 1672 | * When using DMA, we should not be processing events from the RXFIFO |
| 1673 | * as the actual data should be sent to the memory directly and we turn |
| 1674 | * on the completion interrupts to get notifications of transfer completion. |
| 1675 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1676 | static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1677 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1678 | u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1679 | u32 epnum, status, size; |
| 1680 | |
| 1681 | WARN_ON(using_dma(hsotg)); |
| 1682 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1683 | epnum = grxstsr & GRXSTS_EPNUM_MASK; |
| 1684 | status = grxstsr & GRXSTS_PKTSTS_MASK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1685 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1686 | size = grxstsr & GRXSTS_BYTECNT_MASK; |
| 1687 | size >>= GRXSTS_BYTECNT_SHIFT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1688 | |
Mian Yousaf Kaukab | d7c747c | 2015-01-30 09:09:30 +0100 | [diff] [blame] | 1689 | dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n", |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1690 | __func__, grxstsr, size, epnum); |
| 1691 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1692 | switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) { |
| 1693 | case GRXSTS_PKTSTS_GLOBALOUTNAK: |
| 1694 | dev_dbg(hsotg->dev, "GLOBALOUTNAK\n"); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1695 | break; |
| 1696 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1697 | case GRXSTS_PKTSTS_OUTDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1698 | dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1699 | dwc2_hsotg_read_frameno(hsotg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1700 | |
| 1701 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1702 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1703 | break; |
| 1704 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1705 | case GRXSTS_PKTSTS_SETUPDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1706 | dev_dbg(hsotg->dev, |
| 1707 | "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1708 | dwc2_hsotg_read_frameno(hsotg), |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1709 | dwc2_readl(hsotg->regs + DOEPCTL(0))); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1710 | /* |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1711 | * Call dwc2_hsotg_handle_outdone here if it was not called from |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1712 | * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't |
| 1713 | * generate GRXSTS_PKTSTS_OUTDONE for setup packet. |
| 1714 | */ |
| 1715 | if (hsotg->ep0_state == DWC2_EP0_SETUP) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1716 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1717 | break; |
| 1718 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1719 | case GRXSTS_PKTSTS_OUTRX: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1720 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1721 | break; |
| 1722 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1723 | case GRXSTS_PKTSTS_SETUPRX: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1724 | dev_dbg(hsotg->dev, |
| 1725 | "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1726 | dwc2_hsotg_read_frameno(hsotg), |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1727 | dwc2_readl(hsotg->regs + DOEPCTL(0))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1728 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1729 | WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP); |
| 1730 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1731 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1732 | break; |
| 1733 | |
| 1734 | default: |
| 1735 | dev_warn(hsotg->dev, "%s: unknown status %08x\n", |
| 1736 | __func__, grxstsr); |
| 1737 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1738 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1739 | break; |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1744 | * dwc2_hsotg_ep0_mps - turn max packet size into register setting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1745 | * @mps: The maximum packet size in bytes. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1746 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1747 | static u32 dwc2_hsotg_ep0_mps(unsigned int mps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1748 | { |
| 1749 | switch (mps) { |
| 1750 | case 64: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1751 | return D0EPCTL_MPS_64; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1752 | case 32: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1753 | return D0EPCTL_MPS_32; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1754 | case 16: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1755 | return D0EPCTL_MPS_16; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1756 | case 8: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1757 | return D0EPCTL_MPS_8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | /* bad max packet size, warn and return invalid result */ |
| 1761 | WARN_ON(1); |
| 1762 | return (u32)-1; |
| 1763 | } |
| 1764 | |
| 1765 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1766 | * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1767 | * @hsotg: The driver state. |
| 1768 | * @ep: The index number of the endpoint |
| 1769 | * @mps: The maximum packet size in bytes |
| 1770 | * |
| 1771 | * Configure the maximum packet size for the given endpoint, updating |
| 1772 | * the hardware control registers to reflect this. |
| 1773 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1774 | static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg, |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1775 | unsigned int ep, unsigned int mps, unsigned int dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1776 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1777 | struct dwc2_hsotg_ep *hs_ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1778 | void __iomem *regs = hsotg->regs; |
| 1779 | u32 mpsval; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1780 | u32 mcval; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1781 | u32 reg; |
| 1782 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1783 | hs_ep = index_to_ep(hsotg, ep, dir_in); |
| 1784 | if (!hs_ep) |
| 1785 | return; |
| 1786 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1787 | if (ep == 0) { |
| 1788 | /* EP0 is a special case */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1789 | mpsval = dwc2_hsotg_ep0_mps(mps); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1790 | if (mpsval > 3) |
| 1791 | goto bad_mps; |
Robert Baldyga | e9edd199 | 2013-10-09 08:20:02 +0200 | [diff] [blame] | 1792 | hs_ep->ep.maxpacket = mps; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1793 | hs_ep->mc = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1794 | } else { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1795 | mpsval = mps & DXEPCTL_MPS_MASK; |
Robert Baldyga | e9edd199 | 2013-10-09 08:20:02 +0200 | [diff] [blame] | 1796 | if (mpsval > 1024) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1797 | goto bad_mps; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1798 | mcval = ((mps >> 11) & 0x3) + 1; |
| 1799 | hs_ep->mc = mcval; |
| 1800 | if (mcval > 3) |
| 1801 | goto bad_mps; |
Robert Baldyga | e9edd199 | 2013-10-09 08:20:02 +0200 | [diff] [blame] | 1802 | hs_ep->ep.maxpacket = mpsval; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1803 | } |
| 1804 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1805 | if (dir_in) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1806 | reg = dwc2_readl(regs + DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1807 | reg &= ~DXEPCTL_MPS_MASK; |
| 1808 | reg |= mpsval; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1809 | dwc2_writel(reg, regs + DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1810 | } else { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1811 | reg = dwc2_readl(regs + DOEPCTL(ep)); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1812 | reg &= ~DXEPCTL_MPS_MASK; |
Anton Tikhomirov | 659ad60 | 2012-03-06 14:07:29 +0900 | [diff] [blame] | 1813 | reg |= mpsval; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1814 | dwc2_writel(reg, regs + DOEPCTL(ep)); |
Anton Tikhomirov | 659ad60 | 2012-03-06 14:07:29 +0900 | [diff] [blame] | 1815 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1816 | |
| 1817 | return; |
| 1818 | |
| 1819 | bad_mps: |
| 1820 | dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps); |
| 1821 | } |
| 1822 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1823 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1824 | * dwc2_hsotg_txfifo_flush - flush Tx FIFO |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1825 | * @hsotg: The driver state |
| 1826 | * @idx: The index for the endpoint (0..15) |
| 1827 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1828 | static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx) |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1829 | { |
| 1830 | int timeout; |
| 1831 | int val; |
| 1832 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1833 | dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH, |
| 1834 | hsotg->regs + GRSTCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1835 | |
| 1836 | /* wait until the fifo is flushed */ |
| 1837 | timeout = 100; |
| 1838 | |
| 1839 | while (1) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1840 | val = dwc2_readl(hsotg->regs + GRSTCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1841 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1842 | if ((val & (GRSTCTL_TXFFLSH)) == 0) |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1843 | break; |
| 1844 | |
| 1845 | if (--timeout == 0) { |
| 1846 | dev_err(hsotg->dev, |
| 1847 | "%s: timeout flushing fifo (GRSTCTL=%08x)\n", |
| 1848 | __func__, val); |
Marek Szyprowski | e0cbe59 | 2014-09-09 10:44:10 +0200 | [diff] [blame] | 1849 | break; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | udelay(1); |
| 1853 | } |
| 1854 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1855 | |
| 1856 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1857 | * dwc2_hsotg_trytx - check to see if anything needs transmitting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1858 | * @hsotg: The driver state |
| 1859 | * @hs_ep: The driver endpoint to check. |
| 1860 | * |
| 1861 | * Check to see if there is a request that has data to send, and if so |
| 1862 | * make an attempt to write data into the FIFO. |
| 1863 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1864 | static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg, |
| 1865 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1866 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1867 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1868 | |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 1869 | if (!hs_ep->dir_in || !hs_req) { |
| 1870 | /** |
| 1871 | * if request is not enqueued, we disable interrupts |
| 1872 | * for endpoints, excepting ep0 |
| 1873 | */ |
| 1874 | if (hs_ep->index != 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1875 | dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 1876 | hs_ep->dir_in, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1877 | return 0; |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 1878 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1879 | |
| 1880 | if (hs_req->req.actual < hs_req->req.length) { |
| 1881 | dev_dbg(hsotg->dev, "trying to write more for ep%d\n", |
| 1882 | hs_ep->index); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1883 | return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | return 0; |
| 1887 | } |
| 1888 | |
| 1889 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1890 | * dwc2_hsotg_complete_in - complete IN transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1891 | * @hsotg: The device state. |
| 1892 | * @hs_ep: The endpoint that has just completed. |
| 1893 | * |
| 1894 | * An IN transfer has been completed, update the transfer's state and then |
| 1895 | * call the relevant completion routines. |
| 1896 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1897 | static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg, |
| 1898 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1899 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1900 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1901 | u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1902 | int size_left, size_done; |
| 1903 | |
| 1904 | if (!hs_req) { |
| 1905 | dev_dbg(hsotg->dev, "XferCompl but no req\n"); |
| 1906 | return; |
| 1907 | } |
| 1908 | |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 1909 | /* Finish ZLP handling for IN EP0 transactions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1910 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) { |
| 1911 | dev_dbg(hsotg->dev, "zlp packet sent\n"); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1912 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1913 | if (hsotg->test_mode) { |
| 1914 | int ret; |
| 1915 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1916 | ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1917 | if (ret < 0) { |
| 1918 | dev_dbg(hsotg->dev, "Invalid Test #%d\n", |
| 1919 | hsotg->test_mode); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1920 | dwc2_hsotg_stall_ep0(hsotg); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1921 | return; |
| 1922 | } |
| 1923 | } |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1924 | dwc2_hsotg_enqueue_setup(hsotg); |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 1925 | return; |
| 1926 | } |
| 1927 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1928 | /* |
| 1929 | * Calculate the size of the transfer by checking how much is left |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1930 | * in the endpoint size register and then working it out from |
| 1931 | * the amount we loaded for the transfer. |
| 1932 | * |
| 1933 | * We do this even for DMA, as the transfer may have incremented |
| 1934 | * past the end of the buffer (DMA transfers are always 32bit |
| 1935 | * aligned). |
| 1936 | */ |
| 1937 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1938 | size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1939 | |
| 1940 | size_done = hs_ep->size_loaded - size_left; |
| 1941 | size_done += hs_ep->last_load; |
| 1942 | |
| 1943 | if (hs_req->req.actual != size_done) |
| 1944 | dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n", |
| 1945 | __func__, hs_req->req.actual, size_done); |
| 1946 | |
| 1947 | hs_req->req.actual = size_done; |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 1948 | dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n", |
| 1949 | hs_req->req.length, hs_req->req.actual, hs_req->req.zero); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1950 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1951 | if (!size_left && hs_req->req.actual < hs_req->req.length) { |
| 1952 | dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1953 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1954 | return; |
| 1955 | } |
| 1956 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1957 | /* Zlp for all endpoints, for ep0 only in DATA IN stage */ |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 1958 | if (hs_ep->send_zlp) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1959 | dwc2_hsotg_program_zlp(hsotg, hs_ep); |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 1960 | hs_ep->send_zlp = 0; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1961 | /* transfer will be completed on next complete interrupt */ |
| 1962 | return; |
| 1963 | } |
| 1964 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1965 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) { |
| 1966 | /* Move to STATUS OUT */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1967 | dwc2_hsotg_ep0_zlp(hsotg, false); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1968 | return; |
| 1969 | } |
| 1970 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1971 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /** |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 1975 | * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep |
| 1976 | * @hsotg: The device state. |
| 1977 | * @idx: Index of ep. |
| 1978 | * @dir_in: Endpoint direction 1-in 0-out. |
| 1979 | * |
| 1980 | * Reads for endpoint with given index and direction, by masking |
| 1981 | * epint_reg with coresponding mask. |
| 1982 | */ |
| 1983 | static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg, |
| 1984 | unsigned int idx, int dir_in) |
| 1985 | { |
| 1986 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 1987 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 1988 | u32 ints; |
| 1989 | u32 mask; |
| 1990 | u32 diepempmsk; |
| 1991 | |
| 1992 | mask = dwc2_readl(hsotg->regs + epmsk_reg); |
| 1993 | diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK); |
| 1994 | mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0; |
| 1995 | mask |= DXEPINT_SETUP_RCVD; |
| 1996 | |
| 1997 | ints = dwc2_readl(hsotg->regs + epint_reg); |
| 1998 | ints &= mask; |
| 1999 | return ints; |
| 2000 | } |
| 2001 | |
| 2002 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2003 | * dwc2_hsotg_epint - handle an in/out endpoint interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2004 | * @hsotg: The driver state |
| 2005 | * @idx: The index for the endpoint (0..15) |
| 2006 | * @dir_in: Set if this is an IN endpoint |
| 2007 | * |
| 2008 | * Process and clear any interrupt pending for an individual endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2009 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2010 | static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2011 | int dir_in) |
| 2012 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2013 | struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2014 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 2015 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
| 2016 | u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2017 | u32 ints; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2018 | u32 ctrl; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2019 | |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2020 | ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2021 | ctrl = dwc2_readl(hsotg->regs + epctl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2022 | |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2023 | /* Clear endpoint interrupts */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2024 | dwc2_writel(ints, hsotg->regs + epint_reg); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2025 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2026 | if (!hs_ep) { |
| 2027 | dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n", |
| 2028 | __func__, idx, dir_in ? "in" : "out"); |
| 2029 | return; |
| 2030 | } |
| 2031 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2032 | dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n", |
| 2033 | __func__, idx, dir_in ? "in" : "out", ints); |
| 2034 | |
Mian Yousaf Kaukab | b787d75 | 2015-01-09 13:38:43 +0100 | [diff] [blame] | 2035 | /* Don't process XferCompl interrupt if it is a setup packet */ |
| 2036 | if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD))) |
| 2037 | ints &= ~DXEPINT_XFERCOMPL; |
| 2038 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2039 | if (ints & DXEPINT_XFERCOMPL) { |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2040 | hs_ep->has_correct_parity = 1; |
| 2041 | if (hs_ep->isochronous && hs_ep->interval == 1) |
| 2042 | dwc2_hsotg_change_ep_iso_parity(hsotg, epctl_reg); |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2043 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2044 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2045 | "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2046 | __func__, dwc2_readl(hsotg->regs + epctl_reg), |
| 2047 | dwc2_readl(hsotg->regs + epsiz_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2048 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2049 | /* |
| 2050 | * we get OutDone from the FIFO, so we only need to look |
| 2051 | * at completing IN requests here |
| 2052 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2053 | if (dir_in) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2054 | dwc2_hsotg_complete_in(hsotg, hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2055 | |
Ben Dooks | c9a64ea | 2010-07-19 09:40:46 +0100 | [diff] [blame] | 2056 | if (idx == 0 && !hs_ep->req) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2057 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2058 | } else if (using_dma(hsotg)) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2059 | /* |
| 2060 | * We're using DMA, we need to fire an OutDone here |
| 2061 | * as we ignore the RXFIFO. |
| 2062 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2063 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2064 | dwc2_hsotg_handle_outdone(hsotg, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2065 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2066 | } |
| 2067 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2068 | if (ints & DXEPINT_EPDISBLD) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2069 | dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2070 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2071 | if (dir_in) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2072 | int epctl = dwc2_readl(hsotg->regs + epctl_reg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2073 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2074 | dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2075 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2076 | if ((epctl & DXEPCTL_STALL) && |
| 2077 | (epctl & DXEPCTL_EPTYPE_BULK)) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2078 | int dctl = dwc2_readl(hsotg->regs + DCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2079 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2080 | dctl |= DCTL_CGNPINNAK; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2081 | dwc2_writel(dctl, hsotg->regs + DCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2082 | } |
| 2083 | } |
| 2084 | } |
| 2085 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2086 | if (ints & DXEPINT_AHBERR) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2087 | dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2088 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2089 | if (ints & DXEPINT_SETUP) { /* Setup or Timeout */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2090 | dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__); |
| 2091 | |
| 2092 | if (using_dma(hsotg) && idx == 0) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2093 | /* |
| 2094 | * this is the notification we've received a |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2095 | * setup packet. In non-DMA mode we'd get this |
| 2096 | * from the RXFIFO, instead we need to process |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2097 | * the setup here. |
| 2098 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2099 | |
| 2100 | if (dir_in) |
| 2101 | WARN_ON_ONCE(1); |
| 2102 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2103 | dwc2_hsotg_handle_outdone(hsotg, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2104 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2105 | } |
| 2106 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2107 | if (ints & DXEPINT_BACK2BACKSETUP) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2108 | dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2109 | |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2110 | if (dir_in && !hs_ep->isochronous) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2111 | /* not sure if this is important, but we'll clear it anyway */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 2112 | if (ints & DXEPINT_INTKNTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2113 | dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n", |
| 2114 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | /* this probably means something bad is happening */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 2118 | if (ints & DXEPINT_INTKNEPMIS) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2119 | dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n", |
| 2120 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2121 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 2122 | |
| 2123 | /* FIFO has space or is empty (see GAHBCFG) */ |
| 2124 | if (hsotg->dedicated_fifos && |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 2125 | ints & DXEPINT_TXFEMP) { |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 2126 | dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n", |
| 2127 | __func__, idx); |
Anton Tikhomirov | 70fa030 | 2012-03-06 14:08:29 +0900 | [diff] [blame] | 2128 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2129 | dwc2_hsotg_trytx(hsotg, hs_ep); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 2130 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2131 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2135 | * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2136 | * @hsotg: The device state. |
| 2137 | * |
| 2138 | * Handle updating the device settings after the enumeration phase has |
| 2139 | * been completed. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2140 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2141 | static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2142 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2143 | u32 dsts = dwc2_readl(hsotg->regs + DSTS); |
Jingoo Han | 9b2667f | 2014-08-20 12:04:09 +0900 | [diff] [blame] | 2144 | int ep0_mps = 0, ep_mps = 8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2145 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2146 | /* |
| 2147 | * This should signal the finish of the enumeration phase |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2148 | * of the USB handshaking, so we should now know what rate |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2149 | * we connected at. |
| 2150 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2151 | |
| 2152 | dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts); |
| 2153 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2154 | /* |
| 2155 | * note, since we're limited by the size of transfer on EP0, and |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2156 | * it seems IN transfers must be a even number of packets we do |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2157 | * not advertise a 64byte MPS on EP0. |
| 2158 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2159 | |
| 2160 | /* catch both EnumSpd_FS and EnumSpd_FS48 */ |
Marek Vasut | 6d76c92 | 2015-12-18 03:26:17 +0100 | [diff] [blame] | 2161 | switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2162 | case DSTS_ENUMSPD_FS: |
| 2163 | case DSTS_ENUMSPD_FS48: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2164 | hsotg->gadget.speed = USB_SPEED_FULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2165 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 2166 | ep_mps = 1023; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2167 | break; |
| 2168 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2169 | case DSTS_ENUMSPD_HS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2170 | hsotg->gadget.speed = USB_SPEED_HIGH; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2171 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 2172 | ep_mps = 1024; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2173 | break; |
| 2174 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2175 | case DSTS_ENUMSPD_LS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2176 | hsotg->gadget.speed = USB_SPEED_LOW; |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2177 | /* |
| 2178 | * note, we don't actually support LS in this driver at the |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2179 | * moment, and the documentation seems to imply that it isn't |
| 2180 | * supported by the PHYs on some of the devices. |
| 2181 | */ |
| 2182 | break; |
| 2183 | } |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 2184 | dev_info(hsotg->dev, "new device is %s\n", |
| 2185 | usb_speed_string(hsotg->gadget.speed)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2186 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2187 | /* |
| 2188 | * we should now know the maximum packet size for an |
| 2189 | * endpoint, so set the endpoints to a default value. |
| 2190 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2191 | |
| 2192 | if (ep0_mps) { |
| 2193 | int i; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2194 | /* Initialize ep0 for both in and out directions */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2195 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1); |
| 2196 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2197 | for (i = 1; i < hsotg->num_of_eps; i++) { |
| 2198 | if (hsotg->eps_in[i]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2199 | dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2200 | if (hsotg->eps_out[i]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2201 | dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2202 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | /* ensure after enumeration our EP0 is active */ |
| 2206 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2207 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2208 | |
| 2209 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2210 | dwc2_readl(hsotg->regs + DIEPCTL0), |
| 2211 | dwc2_readl(hsotg->regs + DOEPCTL0)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | /** |
| 2215 | * kill_all_requests - remove all requests from the endpoint's queue |
| 2216 | * @hsotg: The device state. |
| 2217 | * @ep: The endpoint the requests may be on. |
| 2218 | * @result: The result code to use. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2219 | * |
| 2220 | * Go through the requests on the given endpoint and mark them |
| 2221 | * completed with the given result code. |
| 2222 | */ |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 2223 | static void kill_all_requests(struct dwc2_hsotg *hsotg, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2224 | struct dwc2_hsotg_ep *ep, |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 2225 | int result) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2226 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2227 | struct dwc2_hsotg_req *req, *treq; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2228 | unsigned size; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2229 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 2230 | ep->req = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2231 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 2232 | list_for_each_entry_safe(req, treq, &ep->queue, queue) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2233 | dwc2_hsotg_complete_request(hsotg, ep, req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2234 | result); |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 2235 | |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2236 | if (!hsotg->dedicated_fifos) |
| 2237 | return; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2238 | size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2239 | if (size < ep->fifo_size) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2240 | dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2241 | } |
| 2242 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2243 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2244 | * dwc2_hsotg_disconnect - disconnect service |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2245 | * @hsotg: The device state. |
| 2246 | * |
Lukasz Majewski | 5e89134 | 2012-05-04 14:17:07 +0200 | [diff] [blame] | 2247 | * The device has been disconnected. Remove all current |
| 2248 | * transactions and signal the gadget driver that this |
| 2249 | * has happened. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2250 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2251 | void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2252 | { |
| 2253 | unsigned ep; |
| 2254 | |
Marek Szyprowski | 4ace06e | 2014-11-21 15:14:47 +0100 | [diff] [blame] | 2255 | if (!hsotg->connected) |
| 2256 | return; |
| 2257 | |
| 2258 | hsotg->connected = 0; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2259 | hsotg->test_mode = 0; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2260 | |
| 2261 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 2262 | if (hsotg->eps_in[ep]) |
| 2263 | kill_all_requests(hsotg, hsotg->eps_in[ep], |
| 2264 | -ESHUTDOWN); |
| 2265 | if (hsotg->eps_out[ep]) |
| 2266 | kill_all_requests(hsotg, hsotg->eps_out[ep], |
| 2267 | -ESHUTDOWN); |
| 2268 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2269 | |
| 2270 | call_gadget(hsotg, disconnect); |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 2271 | hsotg->lx_state = DWC2_L3; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2275 | * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2276 | * @hsotg: The device state: |
| 2277 | * @periodic: True if this is a periodic FIFO interrupt |
| 2278 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2279 | static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2280 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2281 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2282 | int epno, ret; |
| 2283 | |
| 2284 | /* look through for any more data to transmit */ |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 2285 | for (epno = 0; epno < hsotg->num_of_eps; epno++) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2286 | ep = index_to_ep(hsotg, epno, 1); |
| 2287 | |
| 2288 | if (!ep) |
| 2289 | continue; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2290 | |
| 2291 | if (!ep->dir_in) |
| 2292 | continue; |
| 2293 | |
| 2294 | if ((periodic && !ep->periodic) || |
| 2295 | (!periodic && ep->periodic)) |
| 2296 | continue; |
| 2297 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2298 | ret = dwc2_hsotg_trytx(hsotg, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2299 | if (ret < 0) |
| 2300 | break; |
| 2301 | } |
| 2302 | } |
| 2303 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2304 | /* IRQ flags which will trigger a retry around the IRQ loop */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2305 | #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \ |
| 2306 | GINTSTS_PTXFEMP | \ |
| 2307 | GINTSTS_RXFLVL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2308 | |
| 2309 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2310 | * dwc2_hsotg_core_init - issue softreset to the core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2311 | * @hsotg: The device state |
| 2312 | * |
| 2313 | * Issue a soft reset to the core, and await the core finishing it. |
| 2314 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2315 | void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg, |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 2316 | bool is_usb_reset) |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2317 | { |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 2318 | u32 intmsk; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 2319 | u32 val; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 2320 | u32 usbcfg; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 2321 | |
Mian Yousaf Kaukab | 5390d43 | 2015-09-29 12:08:25 +0200 | [diff] [blame] | 2322 | /* Kill any ep0 requests as controller will be reinitialized */ |
| 2323 | kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET); |
| 2324 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 2325 | if (!is_usb_reset) |
John Youn | 241729b | 2015-12-17 11:17:59 -0800 | [diff] [blame] | 2326 | if (dwc2_core_reset(hsotg)) |
Gregory Herrero | 86de489 | 2015-09-29 12:08:21 +0200 | [diff] [blame] | 2327 | return; |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2328 | |
| 2329 | /* |
| 2330 | * we must now enable ep0 ready for host detection and then |
| 2331 | * set configuration. |
| 2332 | */ |
| 2333 | |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 2334 | /* keep other bits untouched (so e.g. forced modes are not lost) */ |
| 2335 | usbcfg = dwc2_readl(hsotg->regs + GUSBCFG); |
| 2336 | usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP | |
| 2337 | GUSBCFG_HNPCAP); |
| 2338 | |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2339 | /* set the PLL on, remove the HNP/SRP and set the PHY */ |
Mian Yousaf Kaukab | fa4a8d7 | 2015-01-30 09:09:35 +0100 | [diff] [blame] | 2340 | val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 2341 | usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) | |
| 2342 | (val << GUSBCFG_USBTRDTIM_SHIFT); |
| 2343 | dwc2_writel(usbcfg, hsotg->regs + GUSBCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2344 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2345 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2346 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 2347 | if (!is_usb_reset) |
| 2348 | __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2349 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2350 | dwc2_writel(DCFG_EPMISCNT(1) | DCFG_DEVSPD_HS, hsotg->regs + DCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2351 | |
| 2352 | /* Clear any pending OTG interrupts */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2353 | dwc2_writel(0xffffffff, hsotg->regs + GOTGINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2354 | |
| 2355 | /* Clear any pending interrupts */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2356 | dwc2_writel(0xffffffff, hsotg->regs + GINTSTS); |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 2357 | intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2358 | GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF | |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 2359 | GINTSTS_USBRST | GINTSTS_RESETDET | |
| 2360 | GINTSTS_ENUMDONE | GINTSTS_OTGINT | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2361 | GINTSTS_USBSUSP | GINTSTS_WKUPINT | |
| 2362 | GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT; |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 2363 | |
| 2364 | if (hsotg->core_params->external_id_pin_ctl <= 0) |
| 2365 | intmsk |= GINTSTS_CONIDSTSCHNG; |
| 2366 | |
| 2367 | dwc2_writel(intmsk, hsotg->regs + GINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2368 | |
| 2369 | if (using_dma(hsotg)) |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2370 | dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN | |
| 2371 | (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT), |
| 2372 | hsotg->regs + GAHBCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2373 | else |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2374 | dwc2_writel(((hsotg->dedicated_fifos) ? |
| 2375 | (GAHBCFG_NP_TXF_EMP_LVL | |
| 2376 | GAHBCFG_P_TXF_EMP_LVL) : 0) | |
| 2377 | GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2378 | |
| 2379 | /* |
Robert Baldyga | 8acc829 | 2013-09-19 11:50:23 +0200 | [diff] [blame] | 2380 | * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts |
| 2381 | * when we have no data to transfer. Otherwise we get being flooded by |
| 2382 | * interrupts. |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2383 | */ |
| 2384 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2385 | dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ? |
Mian Yousaf Kaukab | 6ff2e83 | 2015-01-09 13:38:42 +0100 | [diff] [blame] | 2386 | DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2387 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK | |
| 2388 | DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK | |
| 2389 | DIEPMSK_INTKNEPMISMSK, |
| 2390 | hsotg->regs + DIEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2391 | |
| 2392 | /* |
| 2393 | * don't need XferCompl, we get that from RXFIFO in slave mode. In |
| 2394 | * DMA mode we may need this. |
| 2395 | */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2396 | dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2397 | DIEPMSK_TIMEOUTMSK) : 0) | |
| 2398 | DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK | |
| 2399 | DOEPMSK_SETUPMSK, |
| 2400 | hsotg->regs + DOEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2401 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2402 | dwc2_writel(0, hsotg->regs + DAINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2403 | |
| 2404 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2405 | dwc2_readl(hsotg->regs + DIEPCTL0), |
| 2406 | dwc2_readl(hsotg->regs + DOEPCTL0)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2407 | |
| 2408 | /* enable in and out endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2409 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2410 | |
| 2411 | /* |
| 2412 | * Enable the RXFIFO when in slave mode, as this is how we collect |
| 2413 | * the data. In DMA mode, we get events from the FIFO but also |
| 2414 | * things we cannot process, so do not use it. |
| 2415 | */ |
| 2416 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2417 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2418 | |
| 2419 | /* Enable interrupts for EP0 in and out */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2420 | dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1); |
| 2421 | dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2422 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 2423 | if (!is_usb_reset) { |
| 2424 | __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE); |
| 2425 | udelay(10); /* see openiboot */ |
| 2426 | __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE); |
| 2427 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2428 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2429 | dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2430 | |
| 2431 | /* |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2432 | * DxEPCTL_USBActEp says RO in manual, but seems to be set by |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2433 | * writing to the EPCTL register.. |
| 2434 | */ |
| 2435 | |
| 2436 | /* set to read 1 8byte packet */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2437 | dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2438 | DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2439 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2440 | dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2441 | DXEPCTL_CNAK | DXEPCTL_EPENA | |
| 2442 | DXEPCTL_USBACTEP, |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2443 | hsotg->regs + DOEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2444 | |
| 2445 | /* enable, but don't activate EP0in */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2446 | dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2447 | DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2448 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2449 | dwc2_hsotg_enqueue_setup(hsotg); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2450 | |
| 2451 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2452 | dwc2_readl(hsotg->regs + DIEPCTL0), |
| 2453 | dwc2_readl(hsotg->regs + DOEPCTL0)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2454 | |
| 2455 | /* clear global NAKs */ |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 2456 | val = DCTL_CGOUTNAK | DCTL_CGNPINNAK; |
| 2457 | if (!is_usb_reset) |
| 2458 | val |= DCTL_SFTDISCON; |
| 2459 | __orr32(hsotg->regs + DCTL, val); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2460 | |
| 2461 | /* must be at-least 3ms to allow bus to see disconnect */ |
| 2462 | mdelay(3); |
| 2463 | |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 2464 | hsotg->lx_state = DWC2_L0; |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 2465 | } |
Marek Szyprowski | ac3c81f | 2014-10-20 12:45:35 +0200 | [diff] [blame] | 2466 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2467 | static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 2468 | { |
| 2469 | /* set the soft-disconnect bit */ |
| 2470 | __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON); |
| 2471 | } |
| 2472 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2473 | void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 2474 | { |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2475 | /* remove the soft-disconnect and let's go */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2476 | __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2480 | * dwc2_hsotg_irq - handle device interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2481 | * @irq: The IRQ number triggered |
| 2482 | * @pw: The pw value when registered the handler. |
| 2483 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2484 | static irqreturn_t dwc2_hsotg_irq(int irq, void *pw) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2485 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 2486 | struct dwc2_hsotg *hsotg = pw; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2487 | int retry_count = 8; |
| 2488 | u32 gintsts; |
| 2489 | u32 gintmsk; |
| 2490 | |
Vardan Mikayelyan | ee3de8d | 2016-04-27 20:20:48 -0700 | [diff] [blame] | 2491 | if (!dwc2_is_device_mode(hsotg)) |
| 2492 | return IRQ_NONE; |
| 2493 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2494 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2495 | irq_retry: |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2496 | gintsts = dwc2_readl(hsotg->regs + GINTSTS); |
| 2497 | gintmsk = dwc2_readl(hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2498 | |
| 2499 | dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n", |
| 2500 | __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count); |
| 2501 | |
| 2502 | gintsts &= gintmsk; |
| 2503 | |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 2504 | if (gintsts & GINTSTS_RESETDET) { |
| 2505 | dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__); |
| 2506 | |
| 2507 | dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS); |
| 2508 | |
| 2509 | /* This event must be used only if controller is suspended */ |
| 2510 | if (hsotg->lx_state == DWC2_L2) { |
| 2511 | dwc2_exit_hibernation(hsotg, true); |
| 2512 | hsotg->lx_state = DWC2_L0; |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) { |
| 2517 | |
| 2518 | u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL); |
| 2519 | u32 connected = hsotg->connected; |
| 2520 | |
| 2521 | dev_dbg(hsotg->dev, "%s: USBRst\n", __func__); |
| 2522 | dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n", |
| 2523 | dwc2_readl(hsotg->regs + GNPTXSTS)); |
| 2524 | |
| 2525 | dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS); |
| 2526 | |
| 2527 | /* Report disconnection if it is not already done. */ |
| 2528 | dwc2_hsotg_disconnect(hsotg); |
| 2529 | |
| 2530 | if (usb_status & GOTGCTL_BSESVLD && connected) |
| 2531 | dwc2_hsotg_core_init_disconnected(hsotg, true); |
| 2532 | } |
| 2533 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2534 | if (gintsts & GINTSTS_ENUMDONE) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2535 | dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2536 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2537 | dwc2_hsotg_irq_enumdone(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2538 | } |
| 2539 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2540 | if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2541 | u32 daint = dwc2_readl(hsotg->regs + DAINT); |
| 2542 | u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 2543 | u32 daint_out, daint_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2544 | int ep; |
| 2545 | |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 2546 | daint &= daintmsk; |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2547 | daint_out = daint >> DAINT_OUTEP_SHIFT; |
| 2548 | daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 2549 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2550 | dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint); |
| 2551 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 2552 | for (ep = 0; ep < hsotg->num_of_eps && daint_out; |
| 2553 | ep++, daint_out >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2554 | if (daint_out & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2555 | dwc2_hsotg_epint(hsotg, ep, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2556 | } |
| 2557 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 2558 | for (ep = 0; ep < hsotg->num_of_eps && daint_in; |
| 2559 | ep++, daint_in >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2560 | if (daint_in & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2561 | dwc2_hsotg_epint(hsotg, ep, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2562 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2563 | } |
| 2564 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2565 | /* check both FIFOs */ |
| 2566 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2567 | if (gintsts & GINTSTS_NPTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2568 | dev_dbg(hsotg->dev, "NPTxFEmp\n"); |
| 2569 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2570 | /* |
| 2571 | * Disable the interrupt to stop it happening again |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2572 | * unless one of these endpoint routines decides that |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2573 | * it needs re-enabling |
| 2574 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2575 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2576 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP); |
| 2577 | dwc2_hsotg_irq_fifoempty(hsotg, false); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2578 | } |
| 2579 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2580 | if (gintsts & GINTSTS_PTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2581 | dev_dbg(hsotg->dev, "PTxFEmp\n"); |
| 2582 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2583 | /* See note in GINTSTS_NPTxFEmp */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2584 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2585 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP); |
| 2586 | dwc2_hsotg_irq_fifoempty(hsotg, true); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2587 | } |
| 2588 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2589 | if (gintsts & GINTSTS_RXFLVL) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2590 | /* |
| 2591 | * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2592 | * we need to retry dwc2_hsotg_handle_rx if this is still |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2593 | * set. |
| 2594 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2595 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2596 | dwc2_hsotg_handle_rx(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2597 | } |
| 2598 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2599 | if (gintsts & GINTSTS_ERLYSUSP) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2600 | dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n"); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2601 | dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2602 | } |
| 2603 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2604 | /* |
| 2605 | * these next two seem to crop-up occasionally causing the core |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2606 | * to shutdown the USB transfer, so try clearing them and logging |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2607 | * the occurrence. |
| 2608 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2609 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2610 | if (gintsts & GINTSTS_GOUTNAKEFF) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2611 | dev_info(hsotg->dev, "GOUTNakEff triggered\n"); |
| 2612 | |
Gregory Herrero | 3be99cd | 2015-12-07 12:07:31 +0100 | [diff] [blame] | 2613 | __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2614 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2615 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2616 | } |
| 2617 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2618 | if (gintsts & GINTSTS_GINNAKEFF) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2619 | dev_info(hsotg->dev, "GINNakEff triggered\n"); |
| 2620 | |
Gregory Herrero | 3be99cd | 2015-12-07 12:07:31 +0100 | [diff] [blame] | 2621 | __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2622 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2623 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2624 | } |
| 2625 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2626 | if (gintsts & GINTSTS_INCOMPL_SOIN) { |
| 2627 | u32 idx, epctl_reg; |
| 2628 | struct dwc2_hsotg_ep *hs_ep; |
| 2629 | |
| 2630 | dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOIN\n", __func__); |
| 2631 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
| 2632 | hs_ep = hsotg->eps_in[idx]; |
| 2633 | |
| 2634 | if (!hs_ep->isochronous || hs_ep->has_correct_parity) |
| 2635 | continue; |
| 2636 | |
| 2637 | epctl_reg = DIEPCTL(idx); |
| 2638 | dwc2_hsotg_change_ep_iso_parity(hsotg, epctl_reg); |
| 2639 | } |
| 2640 | dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS); |
| 2641 | } |
| 2642 | |
| 2643 | if (gintsts & GINTSTS_INCOMPL_SOOUT) { |
| 2644 | u32 idx, epctl_reg; |
| 2645 | struct dwc2_hsotg_ep *hs_ep; |
| 2646 | |
| 2647 | dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__); |
| 2648 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
| 2649 | hs_ep = hsotg->eps_out[idx]; |
| 2650 | |
| 2651 | if (!hs_ep->isochronous || hs_ep->has_correct_parity) |
| 2652 | continue; |
| 2653 | |
| 2654 | epctl_reg = DOEPCTL(idx); |
| 2655 | dwc2_hsotg_change_ep_iso_parity(hsotg, epctl_reg); |
| 2656 | } |
| 2657 | dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS); |
| 2658 | } |
| 2659 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2660 | /* |
| 2661 | * if we've had fifo events, we should try and go around the |
| 2662 | * loop again to see if there's any point in returning yet. |
| 2663 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2664 | |
| 2665 | if (gintsts & IRQ_RETRY_MASK && --retry_count > 0) |
| 2666 | goto irq_retry; |
| 2667 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2668 | spin_unlock(&hsotg->lock); |
| 2669 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2670 | return IRQ_HANDLED; |
| 2671 | } |
| 2672 | |
| 2673 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2674 | * dwc2_hsotg_ep_enable - enable the given endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2675 | * @ep: The USB endpint to configure |
| 2676 | * @desc: The USB endpoint descriptor to configure with. |
| 2677 | * |
| 2678 | * This is called from the USB gadget code's usb_ep_enable(). |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2679 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2680 | static int dwc2_hsotg_ep_enable(struct usb_ep *ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2681 | const struct usb_endpoint_descriptor *desc) |
| 2682 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2683 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 2684 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2685 | unsigned long flags; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 2686 | unsigned int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2687 | u32 epctrl_reg; |
| 2688 | u32 epctrl; |
| 2689 | u32 mps; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 2690 | unsigned int dir_in; |
| 2691 | unsigned int i, val, size; |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 2692 | int ret = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2693 | |
| 2694 | dev_dbg(hsotg->dev, |
| 2695 | "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n", |
| 2696 | __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes, |
| 2697 | desc->wMaxPacketSize, desc->bInterval); |
| 2698 | |
| 2699 | /* not to be called for EP0 */ |
Vahram Aharonyan | 8c3d609 | 2016-04-27 20:20:46 -0700 | [diff] [blame] | 2700 | if (index == 0) { |
| 2701 | dev_err(hsotg->dev, "%s: called for EP 0\n", __func__); |
| 2702 | return -EINVAL; |
| 2703 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2704 | |
| 2705 | dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0; |
| 2706 | if (dir_in != hs_ep->dir_in) { |
| 2707 | dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__); |
| 2708 | return -EINVAL; |
| 2709 | } |
| 2710 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 2711 | mps = usb_endpoint_maxp(desc); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2712 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2713 | /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2714 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2715 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2716 | epctrl = dwc2_readl(hsotg->regs + epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2717 | |
| 2718 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n", |
| 2719 | __func__, epctrl, epctrl_reg); |
| 2720 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2721 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2722 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2723 | epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK); |
| 2724 | epctrl |= DXEPCTL_MPS(mps); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2725 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2726 | /* |
| 2727 | * mark the endpoint as active, otherwise the core may ignore |
| 2728 | * transactions entirely for this endpoint |
| 2729 | */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2730 | epctrl |= DXEPCTL_USBACTEP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2731 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2732 | /* |
| 2733 | * set the NAK status on the endpoint, otherwise we might try and |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2734 | * do something with data that we've yet got a request to process |
| 2735 | * since the RXFIFO will take data for an endpoint even if the |
| 2736 | * size register hasn't been set. |
| 2737 | */ |
| 2738 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2739 | epctrl |= DXEPCTL_SNAK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2740 | |
| 2741 | /* update the endpoint state */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2742 | dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2743 | |
| 2744 | /* default, set to non-periodic */ |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2745 | hs_ep->isochronous = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2746 | hs_ep->periodic = 0; |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 2747 | hs_ep->halted = 0; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2748 | hs_ep->interval = desc->bInterval; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 2749 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2750 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
| 2751 | case USB_ENDPOINT_XFER_ISOC: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2752 | epctrl |= DXEPCTL_EPTYPE_ISO; |
| 2753 | epctrl |= DXEPCTL_SETEVENFR; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2754 | hs_ep->isochronous = 1; |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 2755 | hs_ep->interval = 1 << (desc->bInterval - 1); |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2756 | if (dir_in) |
| 2757 | hs_ep->periodic = 1; |
| 2758 | break; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2759 | |
| 2760 | case USB_ENDPOINT_XFER_BULK: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2761 | epctrl |= DXEPCTL_EPTYPE_BULK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2762 | break; |
| 2763 | |
| 2764 | case USB_ENDPOINT_XFER_INT: |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2765 | if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2766 | hs_ep->periodic = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2767 | |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 2768 | if (hsotg->gadget.speed == USB_SPEED_HIGH) |
| 2769 | hs_ep->interval = 1 << (desc->bInterval - 1); |
| 2770 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2771 | epctrl |= DXEPCTL_EPTYPE_INTERRUPT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2772 | break; |
| 2773 | |
| 2774 | case USB_ENDPOINT_XFER_CONTROL: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2775 | epctrl |= DXEPCTL_EPTYPE_CONTROL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2776 | break; |
| 2777 | } |
| 2778 | |
Mian Yousaf Kaukab | 4556e12 | 2015-01-09 13:39:05 +0100 | [diff] [blame] | 2779 | /* If fifo is already allocated for this ep */ |
| 2780 | if (hs_ep->fifo_index) { |
| 2781 | size = hs_ep->ep.maxpacket * hs_ep->mc; |
| 2782 | /* If bigger fifo is required deallocate current one */ |
| 2783 | if (size > hs_ep->fifo_size) { |
| 2784 | hsotg->fifo_map &= ~(1 << hs_ep->fifo_index); |
| 2785 | hs_ep->fifo_index = 0; |
| 2786 | hs_ep->fifo_size = 0; |
| 2787 | } |
| 2788 | } |
| 2789 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2790 | /* |
| 2791 | * if the hardware has dedicated fifos, we must give each IN EP |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 2792 | * a unique tx-fifo even if it is non-periodic. |
| 2793 | */ |
Mian Yousaf Kaukab | 4556e12 | 2015-01-09 13:39:05 +0100 | [diff] [blame] | 2794 | if (dir_in && hsotg->dedicated_fifos && !hs_ep->fifo_index) { |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 2795 | u32 fifo_index = 0; |
| 2796 | u32 fifo_size = UINT_MAX; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2797 | size = hs_ep->ep.maxpacket*hs_ep->mc; |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 2798 | for (i = 1; i < hsotg->num_of_eps; ++i) { |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2799 | if (hsotg->fifo_map & (1<<i)) |
| 2800 | continue; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2801 | val = dwc2_readl(hsotg->regs + DPTXFSIZN(i)); |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2802 | val = (val >> FIFOSIZE_DEPTH_SHIFT)*4; |
| 2803 | if (val < size) |
| 2804 | continue; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 2805 | /* Search for smallest acceptable fifo */ |
| 2806 | if (val < fifo_size) { |
| 2807 | fifo_size = val; |
| 2808 | fifo_index = i; |
| 2809 | } |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2810 | } |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 2811 | if (!fifo_index) { |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 2812 | dev_err(hsotg->dev, |
| 2813 | "%s: No suitable fifo found\n", __func__); |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 2814 | ret = -ENOMEM; |
| 2815 | goto error; |
| 2816 | } |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 2817 | hsotg->fifo_map |= 1 << fifo_index; |
| 2818 | epctrl |= DXEPCTL_TXFNUM(fifo_index); |
| 2819 | hs_ep->fifo_index = fifo_index; |
| 2820 | hs_ep->fifo_size = fifo_size; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2821 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 2822 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2823 | /* for non control endpoints, set PID to D0 */ |
| 2824 | if (index) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2825 | epctrl |= DXEPCTL_SETD0PID; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2826 | |
| 2827 | dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n", |
| 2828 | __func__, epctrl); |
| 2829 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2830 | dwc2_writel(epctrl, hsotg->regs + epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2831 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2832 | __func__, dwc2_readl(hsotg->regs + epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2833 | |
| 2834 | /* enable the endpoint interrupt */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2835 | dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2836 | |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 2837 | error: |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2838 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 2839 | return ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2840 | } |
| 2841 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2842 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2843 | * dwc2_hsotg_ep_disable - disable given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2844 | * @ep: The endpoint to disable. |
| 2845 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2846 | static int dwc2_hsotg_ep_disable(struct usb_ep *ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2847 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2848 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 2849 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2850 | int dir_in = hs_ep->dir_in; |
| 2851 | int index = hs_ep->index; |
| 2852 | unsigned long flags; |
| 2853 | u32 epctrl_reg; |
| 2854 | u32 ctrl; |
| 2855 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 2856 | dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2857 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2858 | if (ep == &hsotg->eps_out[0]->ep) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2859 | dev_err(hsotg->dev, "%s: called for ep0\n", __func__); |
| 2860 | return -EINVAL; |
| 2861 | } |
| 2862 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2863 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2864 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 2865 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2866 | |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 2867 | hsotg->fifo_map &= ~(1<<hs_ep->fifo_index); |
| 2868 | hs_ep->fifo_index = 0; |
| 2869 | hs_ep->fifo_size = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2870 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2871 | ctrl = dwc2_readl(hsotg->regs + epctrl_reg); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2872 | ctrl &= ~DXEPCTL_EPENA; |
| 2873 | ctrl &= ~DXEPCTL_USBACTEP; |
| 2874 | ctrl |= DXEPCTL_SNAK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2875 | |
| 2876 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2877 | dwc2_writel(ctrl, hsotg->regs + epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2878 | |
| 2879 | /* disable endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2880 | dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2881 | |
Mian Yousaf Kaukab | 1141ea0 | 2015-01-09 13:38:57 +0100 | [diff] [blame] | 2882 | /* terminate all requests with shutdown */ |
| 2883 | kill_all_requests(hsotg, hs_ep, -ESHUTDOWN); |
| 2884 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2885 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2886 | return 0; |
| 2887 | } |
| 2888 | |
| 2889 | /** |
| 2890 | * on_list - check request is on the given endpoint |
| 2891 | * @ep: The endpoint to check. |
| 2892 | * @test: The request to test if it is on the endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2893 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2894 | static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2895 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2896 | struct dwc2_hsotg_req *req, *treq; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2897 | |
| 2898 | list_for_each_entry_safe(req, treq, &ep->queue, queue) { |
| 2899 | if (req == test) |
| 2900 | return true; |
| 2901 | } |
| 2902 | |
| 2903 | return false; |
| 2904 | } |
| 2905 | |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 2906 | static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg, |
| 2907 | u32 bit, u32 timeout) |
| 2908 | { |
| 2909 | u32 i; |
| 2910 | |
| 2911 | for (i = 0; i < timeout; i++) { |
| 2912 | if (dwc2_readl(hs_otg->regs + reg) & bit) |
| 2913 | return 0; |
| 2914 | udelay(1); |
| 2915 | } |
| 2916 | |
| 2917 | return -ETIMEDOUT; |
| 2918 | } |
| 2919 | |
| 2920 | static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg, |
| 2921 | struct dwc2_hsotg_ep *hs_ep) |
| 2922 | { |
| 2923 | u32 epctrl_reg; |
| 2924 | u32 epint_reg; |
| 2925 | |
| 2926 | epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) : |
| 2927 | DOEPCTL(hs_ep->index); |
| 2928 | epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) : |
| 2929 | DOEPINT(hs_ep->index); |
| 2930 | |
| 2931 | dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__, |
| 2932 | hs_ep->name); |
| 2933 | if (hs_ep->dir_in) { |
| 2934 | __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK); |
| 2935 | /* Wait for Nak effect */ |
| 2936 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, |
| 2937 | DXEPINT_INEPNAKEFF, 100)) |
| 2938 | dev_warn(hsotg->dev, |
| 2939 | "%s: timeout DIEPINT.NAKEFF\n", __func__); |
| 2940 | } else { |
Vardan Mikayelyan | 6b58cb0 | 2016-05-25 18:07:02 -0700 | [diff] [blame] | 2941 | if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF)) |
| 2942 | __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK); |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 2943 | |
| 2944 | /* Wait for global nak to take effect */ |
| 2945 | if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, |
Du, Changbin | 0676c7e | 2015-12-04 15:38:23 +0800 | [diff] [blame] | 2946 | GINTSTS_GOUTNAKEFF, 100)) |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 2947 | dev_warn(hsotg->dev, |
Du, Changbin | 0676c7e | 2015-12-04 15:38:23 +0800 | [diff] [blame] | 2948 | "%s: timeout GINTSTS.GOUTNAKEFF\n", __func__); |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | /* Disable ep */ |
| 2952 | __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK); |
| 2953 | |
| 2954 | /* Wait for ep to be disabled */ |
| 2955 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100)) |
| 2956 | dev_warn(hsotg->dev, |
| 2957 | "%s: timeout DOEPCTL.EPDisable\n", __func__); |
| 2958 | |
| 2959 | if (hs_ep->dir_in) { |
| 2960 | if (hsotg->dedicated_fifos) { |
| 2961 | dwc2_writel(GRSTCTL_TXFNUM(hs_ep->fifo_index) | |
| 2962 | GRSTCTL_TXFFLSH, hsotg->regs + GRSTCTL); |
| 2963 | /* Wait for fifo flush */ |
| 2964 | if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, |
| 2965 | GRSTCTL_TXFFLSH, 100)) |
| 2966 | dev_warn(hsotg->dev, |
| 2967 | "%s: timeout flushing fifos\n", |
| 2968 | __func__); |
| 2969 | } |
| 2970 | /* TODO: Flush shared tx fifo */ |
| 2971 | } else { |
| 2972 | /* Remove global NAKs */ |
Du, Changbin | 0676c7e | 2015-12-04 15:38:23 +0800 | [diff] [blame] | 2973 | __bic32(hsotg->regs + DCTL, DCTL_SGOUTNAK); |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 2974 | } |
| 2975 | } |
| 2976 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2977 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2978 | * dwc2_hsotg_ep_dequeue - dequeue given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2979 | * @ep: The endpoint to dequeue. |
| 2980 | * @req: The request to be removed from a queue. |
| 2981 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2982 | static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2983 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2984 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 2985 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 2986 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2987 | unsigned long flags; |
| 2988 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 2989 | dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2990 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2991 | spin_lock_irqsave(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2992 | |
| 2993 | if (!on_list(hs_ep, hs_req)) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2994 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2995 | return -EINVAL; |
| 2996 | } |
| 2997 | |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 2998 | /* Dequeue already started request */ |
| 2999 | if (req == &hs_ep->req->req) |
| 3000 | dwc2_hsotg_ep_stop_xfr(hs, hs_ep); |
| 3001 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3002 | dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 3003 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3004 | |
| 3005 | return 0; |
| 3006 | } |
| 3007 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3008 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3009 | * dwc2_hsotg_ep_sethalt - set halt on a given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3010 | * @ep: The endpoint to set halt. |
| 3011 | * @value: Set or unset the halt. |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 3012 | * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if |
| 3013 | * the endpoint is busy processing requests. |
| 3014 | * |
| 3015 | * We need to stall the endpoint immediately if request comes from set_feature |
| 3016 | * protocol command handler. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3017 | */ |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 3018 | static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3019 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3020 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3021 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3022 | int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3023 | u32 epreg; |
| 3024 | u32 epctl; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 3025 | u32 xfertype; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3026 | |
| 3027 | dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value); |
| 3028 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 3029 | if (index == 0) { |
| 3030 | if (value) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3031 | dwc2_hsotg_stall_ep0(hs); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 3032 | else |
| 3033 | dev_warn(hs->dev, |
| 3034 | "%s: can't clear halt on ep0\n", __func__); |
| 3035 | return 0; |
| 3036 | } |
| 3037 | |
Vahram Aharonyan | 15186f1 | 2016-05-23 22:41:59 -0700 | [diff] [blame] | 3038 | if (hs_ep->isochronous) { |
| 3039 | dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name); |
| 3040 | return -EINVAL; |
| 3041 | } |
| 3042 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 3043 | if (!now && value && !list_empty(&hs_ep->queue)) { |
| 3044 | dev_dbg(hs->dev, "%s request is pending, cannot halt\n", |
| 3045 | ep->name); |
| 3046 | return -EAGAIN; |
| 3047 | } |
| 3048 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3049 | if (hs_ep->dir_in) { |
| 3050 | epreg = DIEPCTL(index); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3051 | epctl = dwc2_readl(hs->regs + epreg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3052 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3053 | if (value) { |
Felipe Balbi | 5a350d5 | 2015-06-29 20:17:22 -0500 | [diff] [blame] | 3054 | epctl |= DXEPCTL_STALL | DXEPCTL_SNAK; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3055 | if (epctl & DXEPCTL_EPENA) |
| 3056 | epctl |= DXEPCTL_EPDIS; |
| 3057 | } else { |
| 3058 | epctl &= ~DXEPCTL_STALL; |
| 3059 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 3060 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
| 3061 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
| 3062 | epctl |= DXEPCTL_SETD0PID; |
| 3063 | } |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3064 | dwc2_writel(epctl, hs->regs + epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 3065 | } else { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3066 | |
| 3067 | epreg = DOEPCTL(index); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3068 | epctl = dwc2_readl(hs->regs + epreg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3069 | |
| 3070 | if (value) |
| 3071 | epctl |= DXEPCTL_STALL; |
| 3072 | else { |
| 3073 | epctl &= ~DXEPCTL_STALL; |
| 3074 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 3075 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
| 3076 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
| 3077 | epctl |= DXEPCTL_SETD0PID; |
| 3078 | } |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3079 | dwc2_writel(epctl, hs->regs + epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 3080 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3081 | |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 3082 | hs_ep->halted = value; |
| 3083 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3084 | return 0; |
| 3085 | } |
| 3086 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3087 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3088 | * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3089 | * @ep: The endpoint to set halt. |
| 3090 | * @value: Set or unset the halt. |
| 3091 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3092 | static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value) |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3093 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3094 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3095 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3096 | unsigned long flags = 0; |
| 3097 | int ret = 0; |
| 3098 | |
| 3099 | spin_lock_irqsave(&hs->lock, flags); |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 3100 | ret = dwc2_hsotg_ep_sethalt(ep, value, false); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3101 | spin_unlock_irqrestore(&hs->lock, flags); |
| 3102 | |
| 3103 | return ret; |
| 3104 | } |
| 3105 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3106 | static struct usb_ep_ops dwc2_hsotg_ep_ops = { |
| 3107 | .enable = dwc2_hsotg_ep_enable, |
| 3108 | .disable = dwc2_hsotg_ep_disable, |
| 3109 | .alloc_request = dwc2_hsotg_ep_alloc_request, |
| 3110 | .free_request = dwc2_hsotg_ep_free_request, |
| 3111 | .queue = dwc2_hsotg_ep_queue_lock, |
| 3112 | .dequeue = dwc2_hsotg_ep_dequeue, |
| 3113 | .set_halt = dwc2_hsotg_ep_sethalt_lock, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3114 | /* note, don't believe we have any call for the fifo routines */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3115 | }; |
| 3116 | |
| 3117 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3118 | * dwc2_hsotg_init - initalize the usb core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3119 | * @hsotg: The driver state |
| 3120 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3121 | static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg) |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3122 | { |
Mian Yousaf Kaukab | fa4a8d7 | 2015-01-30 09:09:35 +0100 | [diff] [blame] | 3123 | u32 trdtim; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3124 | u32 usbcfg; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3125 | /* unmask subset of endpoint interrupts */ |
| 3126 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3127 | dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK | |
| 3128 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK, |
| 3129 | hsotg->regs + DIEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3130 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3131 | dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK | |
| 3132 | DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK, |
| 3133 | hsotg->regs + DOEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3134 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3135 | dwc2_writel(0, hsotg->regs + DAINTMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3136 | |
| 3137 | /* Be in disconnected state until gadget is registered */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3138 | __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3139 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3140 | /* setup fifos */ |
| 3141 | |
| 3142 | dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3143 | dwc2_readl(hsotg->regs + GRXFSIZ), |
| 3144 | dwc2_readl(hsotg->regs + GNPTXFSIZ)); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3145 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3146 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3147 | |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3148 | /* keep other bits untouched (so e.g. forced modes are not lost) */ |
| 3149 | usbcfg = dwc2_readl(hsotg->regs + GUSBCFG); |
| 3150 | usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP | |
| 3151 | GUSBCFG_HNPCAP); |
| 3152 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3153 | /* set the PLL on, remove the HNP/SRP and set the PHY */ |
Mian Yousaf Kaukab | fa4a8d7 | 2015-01-30 09:09:35 +0100 | [diff] [blame] | 3154 | trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3155 | usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) | |
| 3156 | (trdtim << GUSBCFG_USBTRDTIM_SHIFT); |
| 3157 | dwc2_writel(usbcfg, hsotg->regs + GUSBCFG); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3158 | |
Gregory Herrero | f509004 | 2015-01-09 13:38:47 +0100 | [diff] [blame] | 3159 | if (using_dma(hsotg)) |
| 3160 | __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3161 | } |
| 3162 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3163 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3164 | * dwc2_hsotg_udc_start - prepare the udc for work |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3165 | * @gadget: The usb gadget state |
| 3166 | * @driver: The usb gadget driver |
| 3167 | * |
| 3168 | * Perform initialization to prepare udc device and driver |
| 3169 | * to work. |
| 3170 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3171 | static int dwc2_hsotg_udc_start(struct usb_gadget *gadget, |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 3172 | struct usb_gadget_driver *driver) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3173 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3174 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 3175 | unsigned long flags; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3176 | int ret; |
| 3177 | |
| 3178 | if (!hsotg) { |
Pavel Machek | a023da3 | 2013-09-30 14:56:02 +0200 | [diff] [blame] | 3179 | pr_err("%s: called with no device\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3180 | return -ENODEV; |
| 3181 | } |
| 3182 | |
| 3183 | if (!driver) { |
| 3184 | dev_err(hsotg->dev, "%s: no driver\n", __func__); |
| 3185 | return -EINVAL; |
| 3186 | } |
| 3187 | |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 3188 | if (driver->max_speed < USB_SPEED_FULL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3189 | dev_err(hsotg->dev, "%s: bad speed\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3190 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 3191 | if (!driver->setup) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3192 | dev_err(hsotg->dev, "%s: missing entry points\n", __func__); |
| 3193 | return -EINVAL; |
| 3194 | } |
| 3195 | |
| 3196 | WARN_ON(hsotg->driver); |
| 3197 | |
| 3198 | driver->driver.bus = NULL; |
| 3199 | hsotg->driver = driver; |
Alexandre Pereira da Silva | 7d7b229 | 2012-06-26 11:27:10 -0300 | [diff] [blame] | 3200 | hsotg->gadget.dev.of_node = hsotg->dev->of_node; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3201 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 3202 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3203 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) { |
| 3204 | ret = dwc2_lowlevel_hw_enable(hsotg); |
| 3205 | if (ret) |
| 3206 | goto err; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3207 | } |
| 3208 | |
Gregory Herrero | f6c0159 | 2015-01-09 13:38:41 +0100 | [diff] [blame] | 3209 | if (!IS_ERR_OR_NULL(hsotg->uphy)) |
| 3210 | otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 3211 | |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 3212 | spin_lock_irqsave(&hsotg->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3213 | dwc2_hsotg_init(hsotg); |
| 3214 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3215 | hsotg->enabled = 0; |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 3216 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 3217 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3218 | dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 3219 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3220 | return 0; |
| 3221 | |
| 3222 | err: |
| 3223 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3224 | return ret; |
| 3225 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3226 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3227 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3228 | * dwc2_hsotg_udc_stop - stop the udc |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3229 | * @gadget: The usb gadget state |
| 3230 | * @driver: The usb gadget driver |
| 3231 | * |
| 3232 | * Stop udc hw block and stay tunned for future transmissions |
| 3233 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3234 | static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3235 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3236 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 3237 | unsigned long flags = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3238 | int ep; |
| 3239 | |
| 3240 | if (!hsotg) |
| 3241 | return -ENODEV; |
| 3242 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3243 | /* all endpoints should be shutdown */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3244 | for (ep = 1; ep < hsotg->num_of_eps; ep++) { |
| 3245 | if (hsotg->eps_in[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3246 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3247 | if (hsotg->eps_out[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3248 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3249 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3250 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 3251 | spin_lock_irqsave(&hsotg->lock, flags); |
| 3252 | |
Marek Szyprowski | 32805c3 | 2014-10-20 12:45:33 +0200 | [diff] [blame] | 3253 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3254 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3255 | hsotg->enabled = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3256 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 3257 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 3258 | |
Gregory Herrero | f6c0159 | 2015-01-09 13:38:41 +0100 | [diff] [blame] | 3259 | if (!IS_ERR_OR_NULL(hsotg->uphy)) |
| 3260 | otg_set_peripheral(hsotg->uphy->otg, NULL); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 3261 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3262 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 3263 | dwc2_lowlevel_hw_disable(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3264 | |
| 3265 | return 0; |
| 3266 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3267 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3268 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3269 | * dwc2_hsotg_gadget_getframe - read the frame number |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3270 | * @gadget: The usb gadget state |
| 3271 | * |
| 3272 | * Read the {micro} frame number |
| 3273 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3274 | static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3275 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3276 | return dwc2_hsotg_read_frameno(to_hsotg(gadget)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3277 | } |
| 3278 | |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 3279 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3280 | * dwc2_hsotg_pullup - connect/disconnect the USB PHY |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 3281 | * @gadget: The usb gadget state |
| 3282 | * @is_on: Current state of the USB PHY |
| 3283 | * |
| 3284 | * Connect/Disconnect the USB PHY pullup |
| 3285 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3286 | static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on) |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 3287 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3288 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 3289 | unsigned long flags = 0; |
| 3290 | |
Gregory Herrero | 77ba911 | 2015-09-29 12:08:19 +0200 | [diff] [blame] | 3291 | dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on, |
| 3292 | hsotg->op_state); |
| 3293 | |
| 3294 | /* Don't modify pullup state while in host mode */ |
| 3295 | if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { |
| 3296 | hsotg->enabled = is_on; |
| 3297 | return 0; |
| 3298 | } |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 3299 | |
| 3300 | spin_lock_irqsave(&hsotg->lock, flags); |
| 3301 | if (is_on) { |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3302 | hsotg->enabled = 1; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3303 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
| 3304 | dwc2_hsotg_core_connect(hsotg); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 3305 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3306 | dwc2_hsotg_core_disconnect(hsotg); |
| 3307 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3308 | hsotg->enabled = 0; |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 3312 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 3313 | |
| 3314 | return 0; |
| 3315 | } |
| 3316 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3317 | static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active) |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 3318 | { |
| 3319 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 3320 | unsigned long flags; |
| 3321 | |
| 3322 | dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active); |
| 3323 | spin_lock_irqsave(&hsotg->lock, flags); |
| 3324 | |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 3325 | /* |
| 3326 | * If controller is hibernated, it must exit from hibernation |
| 3327 | * before being initialized / de-initialized |
| 3328 | */ |
| 3329 | if (hsotg->lx_state == DWC2_L2) |
| 3330 | dwc2_exit_hibernation(hsotg, false); |
| 3331 | |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 3332 | if (is_active) { |
Gregory Herrero | cd0e641 | 2015-09-29 12:08:20 +0200 | [diff] [blame] | 3333 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 3334 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3335 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 3336 | if (hsotg->enabled) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3337 | dwc2_hsotg_core_connect(hsotg); |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 3338 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3339 | dwc2_hsotg_core_disconnect(hsotg); |
| 3340 | dwc2_hsotg_disconnect(hsotg); |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 3344 | return 0; |
| 3345 | } |
| 3346 | |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 3347 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3348 | * dwc2_hsotg_vbus_draw - report bMaxPower field |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 3349 | * @gadget: The usb gadget state |
| 3350 | * @mA: Amount of current |
| 3351 | * |
| 3352 | * Report how much power the device may consume to the phy. |
| 3353 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3354 | static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA) |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 3355 | { |
| 3356 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 3357 | |
| 3358 | if (IS_ERR_OR_NULL(hsotg->uphy)) |
| 3359 | return -ENOTSUPP; |
| 3360 | return usb_phy_set_power(hsotg->uphy, mA); |
| 3361 | } |
| 3362 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3363 | static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = { |
| 3364 | .get_frame = dwc2_hsotg_gadget_getframe, |
| 3365 | .udc_start = dwc2_hsotg_udc_start, |
| 3366 | .udc_stop = dwc2_hsotg_udc_stop, |
| 3367 | .pullup = dwc2_hsotg_pullup, |
| 3368 | .vbus_session = dwc2_hsotg_vbus_session, |
| 3369 | .vbus_draw = dwc2_hsotg_vbus_draw, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3370 | }; |
| 3371 | |
| 3372 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3373 | * dwc2_hsotg_initep - initialise a single endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3374 | * @hsotg: The device state. |
| 3375 | * @hs_ep: The endpoint to be initialised. |
| 3376 | * @epnum: The endpoint number |
| 3377 | * |
| 3378 | * Initialise the given endpoint (as part of the probe and device state |
| 3379 | * creation) to give to the gadget driver. Setup the endpoint name, any |
| 3380 | * direction information and other state that may be required. |
| 3381 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3382 | static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg, |
| 3383 | struct dwc2_hsotg_ep *hs_ep, |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3384 | int epnum, |
| 3385 | bool dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3386 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3387 | char *dir; |
| 3388 | |
| 3389 | if (epnum == 0) |
| 3390 | dir = ""; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3391 | else if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3392 | dir = "in"; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3393 | else |
| 3394 | dir = "out"; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3395 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3396 | hs_ep->dir_in = dir_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3397 | hs_ep->index = epnum; |
| 3398 | |
| 3399 | snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir); |
| 3400 | |
| 3401 | INIT_LIST_HEAD(&hs_ep->queue); |
| 3402 | INIT_LIST_HEAD(&hs_ep->ep.ep_list); |
| 3403 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3404 | /* add to the list of endpoints known by the gadget driver */ |
| 3405 | if (epnum) |
| 3406 | list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list); |
| 3407 | |
| 3408 | hs_ep->parent = hsotg; |
| 3409 | hs_ep->ep.name = hs_ep->name; |
Robert Baldyga | e117e74 | 2013-12-13 12:23:38 +0100 | [diff] [blame] | 3410 | usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3411 | hs_ep->ep.ops = &dwc2_hsotg_ep_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3412 | |
Robert Baldyga | 2954522 | 2015-07-31 16:00:18 +0200 | [diff] [blame] | 3413 | if (epnum == 0) { |
| 3414 | hs_ep->ep.caps.type_control = true; |
| 3415 | } else { |
| 3416 | hs_ep->ep.caps.type_iso = true; |
| 3417 | hs_ep->ep.caps.type_bulk = true; |
| 3418 | hs_ep->ep.caps.type_int = true; |
| 3419 | } |
| 3420 | |
| 3421 | if (dir_in) |
| 3422 | hs_ep->ep.caps.dir_in = true; |
| 3423 | else |
| 3424 | hs_ep->ep.caps.dir_out = true; |
| 3425 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3426 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3427 | * if we're using dma, we need to set the next-endpoint pointer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3428 | * to be something valid. |
| 3429 | */ |
| 3430 | |
| 3431 | if (using_dma(hsotg)) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3432 | u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3433 | if (dir_in) |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3434 | dwc2_writel(next, hsotg->regs + DIEPCTL(epnum)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3435 | else |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3436 | dwc2_writel(next, hsotg->regs + DOEPCTL(epnum)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3437 | } |
| 3438 | } |
| 3439 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3440 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3441 | * dwc2_hsotg_hw_cfg - read HW configuration registers |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3442 | * @param: The device state |
| 3443 | * |
| 3444 | * Read the USB core HW configuration registers |
| 3445 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3446 | static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3447 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3448 | u32 cfg; |
| 3449 | u32 ep_type; |
| 3450 | u32 i; |
| 3451 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3452 | /* check hardware configuration */ |
| 3453 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 3454 | hsotg->num_of_eps = hsotg->hw_params.num_dev_ep; |
| 3455 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3456 | /* Add ep0 */ |
| 3457 | hsotg->num_of_eps++; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3458 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3459 | hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep), |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3460 | GFP_KERNEL); |
| 3461 | if (!hsotg->eps_in[0]) |
| 3462 | return -ENOMEM; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3463 | /* Same dwc2_hsotg_ep is used in both directions for ep0 */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3464 | hsotg->eps_out[0] = hsotg->eps_in[0]; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3465 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 3466 | cfg = hsotg->hw_params.dev_ep_dirs; |
Roshan Pius | 251a17f | 2015-02-02 14:55:38 -0800 | [diff] [blame] | 3467 | for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3468 | ep_type = cfg & 3; |
| 3469 | /* Direction in or both */ |
| 3470 | if (!(ep_type & 2)) { |
| 3471 | hsotg->eps_in[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3472 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3473 | if (!hsotg->eps_in[i]) |
| 3474 | return -ENOMEM; |
| 3475 | } |
| 3476 | /* Direction out or both */ |
| 3477 | if (!(ep_type & 1)) { |
| 3478 | hsotg->eps_out[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3479 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3480 | if (!hsotg->eps_out[i]) |
| 3481 | return -ENOMEM; |
| 3482 | } |
| 3483 | } |
| 3484 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 3485 | hsotg->fifo_mem = hsotg->hw_params.total_fifo_size; |
| 3486 | hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo; |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3487 | |
Marek Szyprowski | cff9eb7 | 2014-09-09 10:44:55 +0200 | [diff] [blame] | 3488 | dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n", |
| 3489 | hsotg->num_of_eps, |
| 3490 | hsotg->dedicated_fifos ? "dedicated" : "shared", |
| 3491 | hsotg->fifo_mem); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3492 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3493 | } |
| 3494 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3495 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3496 | * dwc2_hsotg_dump - dump state of the udc |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3497 | * @param: The device state |
| 3498 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3499 | static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3500 | { |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 3501 | #ifdef DEBUG |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3502 | struct device *dev = hsotg->dev; |
| 3503 | void __iomem *regs = hsotg->regs; |
| 3504 | u32 val; |
| 3505 | int idx; |
| 3506 | |
| 3507 | dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3508 | dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL), |
| 3509 | dwc2_readl(regs + DIEPMSK)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3510 | |
Mian Yousaf Kaukab | f889f23 | 2015-01-30 09:09:36 +0100 | [diff] [blame] | 3511 | dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3512 | dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3513 | |
| 3514 | dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3515 | dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3516 | |
| 3517 | /* show periodic fifo settings */ |
| 3518 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 3519 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3520 | val = dwc2_readl(regs + DPTXFSIZN(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3521 | dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3522 | val >> FIFOSIZE_DEPTH_SHIFT, |
| 3523 | val & FIFOSIZE_STARTADDR_MASK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3524 | } |
| 3525 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 3526 | for (idx = 0; idx < hsotg->num_of_eps; idx++) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3527 | dev_info(dev, |
| 3528 | "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx, |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3529 | dwc2_readl(regs + DIEPCTL(idx)), |
| 3530 | dwc2_readl(regs + DIEPTSIZ(idx)), |
| 3531 | dwc2_readl(regs + DIEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3532 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3533 | val = dwc2_readl(regs + DOEPCTL(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3534 | dev_info(dev, |
| 3535 | "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3536 | idx, dwc2_readl(regs + DOEPCTL(idx)), |
| 3537 | dwc2_readl(regs + DOEPTSIZ(idx)), |
| 3538 | dwc2_readl(regs + DOEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3539 | |
| 3540 | } |
| 3541 | |
| 3542 | dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3543 | dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE)); |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 3544 | #endif |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3545 | } |
| 3546 | |
Gregory Herrero | edd74be | 2015-01-09 13:38:48 +0100 | [diff] [blame] | 3547 | #ifdef CONFIG_OF |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3548 | static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg) |
Gregory Herrero | edd74be | 2015-01-09 13:38:48 +0100 | [diff] [blame] | 3549 | { |
| 3550 | struct device_node *np = hsotg->dev->of_node; |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 3551 | u32 len = 0; |
| 3552 | u32 i = 0; |
Gregory Herrero | edd74be | 2015-01-09 13:38:48 +0100 | [diff] [blame] | 3553 | |
| 3554 | /* Enable dma if requested in device tree */ |
| 3555 | hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma"); |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 3556 | |
| 3557 | /* |
| 3558 | * Register TX periodic fifo size per endpoint. |
| 3559 | * EP0 is excluded since it has no fifo configuration. |
| 3560 | */ |
| 3561 | if (!of_find_property(np, "g-tx-fifo-size", &len)) |
| 3562 | goto rx_fifo; |
| 3563 | |
| 3564 | len /= sizeof(u32); |
| 3565 | |
| 3566 | /* Read tx fifo sizes other than ep0 */ |
| 3567 | if (of_property_read_u32_array(np, "g-tx-fifo-size", |
| 3568 | &hsotg->g_tx_fifo_sz[1], len)) |
| 3569 | goto rx_fifo; |
| 3570 | |
| 3571 | /* Add ep0 */ |
| 3572 | len++; |
| 3573 | |
| 3574 | /* Make remaining TX fifos unavailable */ |
| 3575 | if (len < MAX_EPS_CHANNELS) { |
| 3576 | for (i = len; i < MAX_EPS_CHANNELS; i++) |
| 3577 | hsotg->g_tx_fifo_sz[i] = 0; |
| 3578 | } |
| 3579 | |
| 3580 | rx_fifo: |
| 3581 | /* Register RX fifo size */ |
| 3582 | of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz); |
| 3583 | |
| 3584 | /* Register NPTX fifo size */ |
| 3585 | of_property_read_u32(np, "g-np-tx-fifo-size", |
| 3586 | &hsotg->g_np_g_tx_fifo_sz); |
Gregory Herrero | edd74be | 2015-01-09 13:38:48 +0100 | [diff] [blame] | 3587 | } |
| 3588 | #else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3589 | static inline void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg) { } |
Gregory Herrero | edd74be | 2015-01-09 13:38:48 +0100 | [diff] [blame] | 3590 | #endif |
| 3591 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3592 | /** |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 3593 | * dwc2_gadget_init - init function for gadget |
| 3594 | * @dwc2: The data structure for the DWC2 driver. |
| 3595 | * @irq: The IRQ number for the controller. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3596 | */ |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 3597 | int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3598 | { |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 3599 | struct device *dev = hsotg->dev; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3600 | int epnum; |
| 3601 | int ret; |
Lukasz Majewski | fc9a731 | 2012-05-04 14:17:02 +0200 | [diff] [blame] | 3602 | int i; |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 3603 | u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3604 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 3605 | /* Initialize to legacy fifo configuration values */ |
| 3606 | hsotg->g_rx_fifo_sz = 2048; |
| 3607 | hsotg->g_np_g_tx_fifo_sz = 1024; |
| 3608 | memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo)); |
| 3609 | /* Device tree specific probe */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3610 | dwc2_hsotg_of_probe(hsotg); |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 3611 | |
| 3612 | /* Check against largest possible value. */ |
| 3613 | if (hsotg->g_np_g_tx_fifo_sz > |
| 3614 | hsotg->hw_params.dev_nperio_tx_fifo_size) { |
| 3615 | dev_warn(dev, "Specified GNPTXFDEP=%d > %d\n", |
| 3616 | hsotg->g_np_g_tx_fifo_sz, |
| 3617 | hsotg->hw_params.dev_nperio_tx_fifo_size); |
| 3618 | hsotg->g_np_g_tx_fifo_sz = |
| 3619 | hsotg->hw_params.dev_nperio_tx_fifo_size; |
| 3620 | } |
| 3621 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 3622 | /* Dump fifo information */ |
| 3623 | dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n", |
| 3624 | hsotg->g_np_g_tx_fifo_sz); |
| 3625 | dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz); |
| 3626 | for (i = 0; i < MAX_EPS_CHANNELS; i++) |
| 3627 | dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i, |
| 3628 | hsotg->g_tx_fifo_sz[i]); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 3629 | |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 3630 | hsotg->gadget.max_speed = USB_SPEED_HIGH; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3631 | hsotg->gadget.ops = &dwc2_hsotg_gadget_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3632 | hsotg->gadget.name = dev_name(dev); |
Gregory Herrero | 097ee66 | 2015-04-29 22:09:10 +0200 | [diff] [blame] | 3633 | if (hsotg->dr_mode == USB_DR_MODE_OTG) |
| 3634 | hsotg->gadget.is_otg = 1; |
Mian Yousaf Kaukab | ec4cc65 | 2015-09-22 15:16:55 +0200 | [diff] [blame] | 3635 | else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 3636 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3637 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3638 | ret = dwc2_hsotg_hw_cfg(hsotg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3639 | if (ret) { |
| 3640 | dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3641 | return ret; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3642 | } |
| 3643 | |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 3644 | hsotg->ctrl_buff = devm_kzalloc(hsotg->dev, |
| 3645 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
| 3646 | if (!hsotg->ctrl_buff) { |
| 3647 | dev_err(dev, "failed to allocate ctrl request buff\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3648 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | hsotg->ep0_buff = devm_kzalloc(hsotg->dev, |
| 3652 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
| 3653 | if (!hsotg->ep0_buff) { |
| 3654 | dev_err(dev, "failed to allocate ctrl reply buff\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3655 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 3656 | } |
| 3657 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3658 | ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED, |
Dinh Nguyen | db8178c | 2014-11-11 11:13:37 -0600 | [diff] [blame] | 3659 | dev_name(hsotg->dev), hsotg); |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 3660 | if (ret < 0) { |
Dinh Nguyen | db8178c | 2014-11-11 11:13:37 -0600 | [diff] [blame] | 3661 | dev_err(dev, "cannot claim IRQ for gadget\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3662 | return ret; |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 3663 | } |
| 3664 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3665 | /* hsotg->num_of_eps holds number of EPs other than ep0 */ |
| 3666 | |
| 3667 | if (hsotg->num_of_eps == 0) { |
| 3668 | dev_err(dev, "wrong number of EPs (zero)\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3669 | return -EINVAL; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3670 | } |
| 3671 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3672 | /* setup endpoint information */ |
| 3673 | |
| 3674 | INIT_LIST_HEAD(&hsotg->gadget.ep_list); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3675 | hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3676 | |
| 3677 | /* allocate EP0 request */ |
| 3678 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3679 | hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep, |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3680 | GFP_KERNEL); |
| 3681 | if (!hsotg->ctrl_req) { |
| 3682 | dev_err(dev, "failed to allocate ctrl req\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3683 | return -ENOMEM; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3684 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3685 | |
| 3686 | /* initialise the endpoints now the core has been initialised */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3687 | for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) { |
| 3688 | if (hsotg->eps_in[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3689 | dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum], |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3690 | epnum, 1); |
| 3691 | if (hsotg->eps_out[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3692 | dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum], |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3693 | epnum, 0); |
| 3694 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3695 | |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 3696 | ret = usb_add_gadget_udc(dev, &hsotg->gadget); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 3697 | if (ret) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3698 | return ret; |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 3699 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3700 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3701 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3702 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3703 | } |
| 3704 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3705 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3706 | * dwc2_hsotg_remove - remove function for hsotg driver |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3707 | * @pdev: The platform information for the driver |
| 3708 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3709 | int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3710 | { |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 3711 | usb_del_gadget_udc(&hsotg->gadget); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 3712 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3713 | return 0; |
| 3714 | } |
| 3715 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3716 | int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3717 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3718 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3719 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 3720 | if (hsotg->lx_state != DWC2_L0) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3721 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 3722 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3723 | if (hsotg->driver) { |
| 3724 | int ep; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3725 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3726 | dev_info(hsotg->dev, "suspending usb gadget %s\n", |
| 3727 | hsotg->driver->driver.name); |
| 3728 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3729 | spin_lock_irqsave(&hsotg->lock, flags); |
| 3730 | if (hsotg->enabled) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3731 | dwc2_hsotg_core_disconnect(hsotg); |
| 3732 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3733 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 3734 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3735 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3736 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 3737 | if (hsotg->eps_in[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3738 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3739 | if (hsotg->eps_out[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3740 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3741 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3742 | } |
| 3743 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3744 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3745 | } |
| 3746 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3747 | int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3748 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3749 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3750 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 3751 | if (hsotg->lx_state == DWC2_L2) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3752 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 3753 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3754 | if (hsotg->driver) { |
| 3755 | dev_info(hsotg->dev, "resuming usb gadget %s\n", |
| 3756 | hsotg->driver->driver.name); |
Robert Baldyga | d00b414 | 2014-09-09 10:44:57 +0200 | [diff] [blame] | 3757 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3758 | spin_lock_irqsave(&hsotg->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3759 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3760 | if (hsotg->enabled) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3761 | dwc2_hsotg_core_connect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 3762 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3763 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3764 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 3765 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 3766 | } |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 3767 | |
| 3768 | /** |
| 3769 | * dwc2_backup_device_registers() - Backup controller device registers. |
| 3770 | * When suspending usb bus, registers needs to be backuped |
| 3771 | * if controller power is disabled once suspended. |
| 3772 | * |
| 3773 | * @hsotg: Programming view of the DWC_otg controller |
| 3774 | */ |
| 3775 | int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg) |
| 3776 | { |
| 3777 | struct dwc2_dregs_backup *dr; |
| 3778 | int i; |
| 3779 | |
| 3780 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 3781 | |
| 3782 | /* Backup dev regs */ |
| 3783 | dr = &hsotg->dr_backup; |
| 3784 | |
| 3785 | dr->dcfg = dwc2_readl(hsotg->regs + DCFG); |
| 3786 | dr->dctl = dwc2_readl(hsotg->regs + DCTL); |
| 3787 | dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK); |
| 3788 | dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK); |
| 3789 | dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK); |
| 3790 | |
| 3791 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 3792 | /* Backup IN EPs */ |
| 3793 | dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i)); |
| 3794 | |
| 3795 | /* Ensure DATA PID is correctly configured */ |
| 3796 | if (dr->diepctl[i] & DXEPCTL_DPID) |
| 3797 | dr->diepctl[i] |= DXEPCTL_SETD1PID; |
| 3798 | else |
| 3799 | dr->diepctl[i] |= DXEPCTL_SETD0PID; |
| 3800 | |
| 3801 | dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i)); |
| 3802 | dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i)); |
| 3803 | |
| 3804 | /* Backup OUT EPs */ |
| 3805 | dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i)); |
| 3806 | |
| 3807 | /* Ensure DATA PID is correctly configured */ |
| 3808 | if (dr->doepctl[i] & DXEPCTL_DPID) |
| 3809 | dr->doepctl[i] |= DXEPCTL_SETD1PID; |
| 3810 | else |
| 3811 | dr->doepctl[i] |= DXEPCTL_SETD0PID; |
| 3812 | |
| 3813 | dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i)); |
| 3814 | dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i)); |
| 3815 | } |
| 3816 | dr->valid = true; |
| 3817 | return 0; |
| 3818 | } |
| 3819 | |
| 3820 | /** |
| 3821 | * dwc2_restore_device_registers() - Restore controller device registers. |
| 3822 | * When resuming usb bus, device registers needs to be restored |
| 3823 | * if controller power were disabled. |
| 3824 | * |
| 3825 | * @hsotg: Programming view of the DWC_otg controller |
| 3826 | */ |
| 3827 | int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg) |
| 3828 | { |
| 3829 | struct dwc2_dregs_backup *dr; |
| 3830 | u32 dctl; |
| 3831 | int i; |
| 3832 | |
| 3833 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 3834 | |
| 3835 | /* Restore dev regs */ |
| 3836 | dr = &hsotg->dr_backup; |
| 3837 | if (!dr->valid) { |
| 3838 | dev_err(hsotg->dev, "%s: no device registers to restore\n", |
| 3839 | __func__); |
| 3840 | return -EINVAL; |
| 3841 | } |
| 3842 | dr->valid = false; |
| 3843 | |
| 3844 | dwc2_writel(dr->dcfg, hsotg->regs + DCFG); |
| 3845 | dwc2_writel(dr->dctl, hsotg->regs + DCTL); |
| 3846 | dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK); |
| 3847 | dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK); |
| 3848 | dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK); |
| 3849 | |
| 3850 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 3851 | /* Restore IN EPs */ |
| 3852 | dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i)); |
| 3853 | dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i)); |
| 3854 | dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i)); |
| 3855 | |
| 3856 | /* Restore OUT EPs */ |
| 3857 | dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i)); |
| 3858 | dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i)); |
| 3859 | dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i)); |
| 3860 | } |
| 3861 | |
| 3862 | /* Set the Power-On Programming done bit */ |
| 3863 | dctl = dwc2_readl(hsotg->regs + DCTL); |
| 3864 | dctl |= DCTL_PWRONPRGDONE; |
| 3865 | dwc2_writel(dctl, hsotg->regs + DCTL); |
| 3866 | |
| 3867 | return 0; |
| 3868 | } |