Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2 | /** |
Anton Tikhomirov | dfbc6fa | 2011-04-21 17:06:43 +0900 | [diff] [blame] | 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * http://www.samsung.com |
| 5 | * |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 6 | * Copyright 2008 Openmoko, Inc. |
| 7 | * Copyright 2008 Simtec Electronics |
| 8 | * Ben Dooks <ben@simtec.co.uk> |
| 9 | * http://armlinux.simtec.co.uk/ |
| 10 | * |
| 11 | * S3C USB2.0 High-speed / OtG driver |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 12 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/dma-mapping.h> |
Marek Szyprowski | 7ad8096 | 2014-11-21 15:14:48 +0100 | [diff] [blame] | 20 | #include <linux/mutex.h> |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Tomasz Figa | c50f056c | 2013-06-25 17:38:23 +0200 | [diff] [blame] | 25 | #include <linux/of_platform.h> |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 26 | |
| 27 | #include <linux/usb/ch9.h> |
| 28 | #include <linux/usb/gadget.h> |
Praveen Paneri | b2e587d | 2012-11-14 15:57:16 +0530 | [diff] [blame] | 29 | #include <linux/usb/phy.h> |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 30 | |
Dinh Nguyen | f7c0b14 | 2014-04-14 14:13:35 -0700 | [diff] [blame] | 31 | #include "core.h" |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 32 | #include "hw.h" |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 33 | |
| 34 | /* conversion functions */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 35 | static inline struct dwc2_hsotg_req *our_req(struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 36 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 37 | return container_of(req, struct dwc2_hsotg_req, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 40 | static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 41 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 42 | return container_of(ep, struct dwc2_hsotg_ep, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 45 | static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 46 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 47 | return container_of(gadget, struct dwc2_hsotg, gadget); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static inline void __orr32(void __iomem *ptr, u32 val) |
| 51 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 52 | dwc2_writel(dwc2_readl(ptr) | val, ptr); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static inline void __bic32(void __iomem *ptr, u32 val) |
| 56 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 57 | dwc2_writel(dwc2_readl(ptr) & ~val, ptr); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 60 | 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] | 61 | u32 ep_index, u32 dir_in) |
| 62 | { |
| 63 | if (dir_in) |
| 64 | return hsotg->eps_in[ep_index]; |
| 65 | else |
| 66 | return hsotg->eps_out[ep_index]; |
| 67 | } |
| 68 | |
Mickael Maison | 997f4f8 | 2014-12-23 17:39:45 +0100 | [diff] [blame] | 69 | /* forward declaration of functions */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 70 | static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * using_dma - return the DMA status of the driver. |
| 74 | * @hsotg: The driver state. |
| 75 | * |
| 76 | * Return true if we're using DMA. |
| 77 | * |
| 78 | * Currently, we have the DMA support code worked into everywhere |
| 79 | * that needs it, but the AMBA DMA implementation in the hardware can |
| 80 | * only DMA from 32bit aligned addresses. This means that gadgets such |
| 81 | * as the CDC Ethernet cannot work as they often pass packets which are |
| 82 | * not 32bit aligned. |
| 83 | * |
| 84 | * Unfortunately the choice to use DMA or not is global to the controller |
| 85 | * and seems to be only settable when the controller is being put through |
| 86 | * a core reset. This means we either need to fix the gadgets to take |
| 87 | * account of DMA alignment, or add bounce buffers (yuerk). |
| 88 | * |
Gregory Herrero | edd74be | 2015-01-09 13:38:48 +0100 | [diff] [blame] | 89 | * g_using_dma is set depending on dts flag. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 90 | */ |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 91 | static inline bool using_dma(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 92 | { |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 93 | return hsotg->params.g_dma; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Vahram Aharonyan | dec4b55 | 2016-11-09 19:27:48 -0800 | [diff] [blame] | 96 | /* |
| 97 | * using_desc_dma - return the descriptor DMA status of the driver. |
| 98 | * @hsotg: The driver state. |
| 99 | * |
| 100 | * Return true if we're using descriptor DMA. |
| 101 | */ |
| 102 | static inline bool using_desc_dma(struct dwc2_hsotg *hsotg) |
| 103 | { |
| 104 | return hsotg->params.g_dma_desc; |
| 105 | } |
| 106 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 107 | /** |
Vardan Mikayelyan | 92d1635 | 2016-05-25 18:07:05 -0700 | [diff] [blame] | 108 | * dwc2_gadget_incr_frame_num - Increments the targeted frame number. |
| 109 | * @hs_ep: The endpoint |
| 110 | * @increment: The value to increment by |
| 111 | * |
| 112 | * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT. |
| 113 | * If an overrun occurs it will wrap the value and set the frame_overrun flag. |
| 114 | */ |
| 115 | static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep) |
| 116 | { |
| 117 | hs_ep->target_frame += hs_ep->interval; |
| 118 | if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) { |
| 119 | hs_ep->frame_overrun = 1; |
| 120 | hs_ep->target_frame &= DSTS_SOFFN_LIMIT; |
| 121 | } else { |
| 122 | hs_ep->frame_overrun = 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 127 | * dwc2_hsotg_en_gsint - enable one or more of the general interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 128 | * @hsotg: The device state |
| 129 | * @ints: A bitmask of the interrupts to enable |
| 130 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 131 | static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 132 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 133 | u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 134 | u32 new_gsintmsk; |
| 135 | |
| 136 | new_gsintmsk = gsintmsk | ints; |
| 137 | |
| 138 | if (new_gsintmsk != gsintmsk) { |
| 139 | dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 140 | dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 145 | * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 146 | * @hsotg: The device state |
| 147 | * @ints: A bitmask of the interrupts to enable |
| 148 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 149 | static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 150 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 151 | u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 152 | u32 new_gsintmsk; |
| 153 | |
| 154 | new_gsintmsk = gsintmsk & ~ints; |
| 155 | |
| 156 | if (new_gsintmsk != gsintmsk) |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 157 | dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 161 | * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 162 | * @hsotg: The device state |
| 163 | * @ep: The endpoint index |
| 164 | * @dir_in: True if direction is in. |
| 165 | * @en: The enable value, true to enable |
| 166 | * |
| 167 | * Set or clear the mask for an individual endpoint's interrupt |
| 168 | * request. |
| 169 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 170 | static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 171 | unsigned int ep, unsigned int dir_in, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 172 | unsigned int en) |
| 173 | { |
| 174 | unsigned long flags; |
| 175 | u32 bit = 1 << ep; |
| 176 | u32 daint; |
| 177 | |
| 178 | if (!dir_in) |
| 179 | bit <<= 16; |
| 180 | |
| 181 | local_irq_save(flags); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 182 | daint = dwc2_readl(hsotg->regs + DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 183 | if (en) |
| 184 | daint |= bit; |
| 185 | else |
| 186 | daint &= ~bit; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 187 | dwc2_writel(daint, hsotg->regs + DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 188 | local_irq_restore(flags); |
| 189 | } |
| 190 | |
| 191 | /** |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 192 | * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode |
| 193 | */ |
| 194 | int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg) |
| 195 | { |
| 196 | if (hsotg->hw_params.en_multiple_tx_fifo) |
| 197 | /* In dedicated FIFO mode we need count of IN EPs */ |
| 198 | return (dwc2_readl(hsotg->regs + GHWCFG4) & |
| 199 | GHWCFG4_NUM_IN_EPS_MASK) >> GHWCFG4_NUM_IN_EPS_SHIFT; |
| 200 | else |
| 201 | /* In shared FIFO mode we need count of Periodic IN EPs */ |
| 202 | return hsotg->hw_params.num_dev_perio_in_ep; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * dwc2_hsotg_ep_info_size - return Endpoint Info Control block size in DWORDs |
| 207 | */ |
| 208 | static int dwc2_hsotg_ep_info_size(struct dwc2_hsotg *hsotg) |
| 209 | { |
| 210 | int val = 0; |
| 211 | int i; |
| 212 | u32 ep_dirs; |
| 213 | |
| 214 | /* |
| 215 | * Don't need additional space for ep info control registers in |
| 216 | * slave mode. |
| 217 | */ |
| 218 | if (!using_dma(hsotg)) { |
| 219 | dev_dbg(hsotg->dev, "Buffer DMA ep info size 0\n"); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Buffer DMA mode - 1 location per endpoit |
| 225 | * Descriptor DMA mode - 4 locations per endpoint |
| 226 | */ |
| 227 | ep_dirs = hsotg->hw_params.dev_ep_dirs; |
| 228 | |
| 229 | for (i = 0; i <= hsotg->hw_params.num_dev_ep; i++) { |
| 230 | val += ep_dirs & 3 ? 1 : 2; |
| 231 | ep_dirs >>= 2; |
| 232 | } |
| 233 | |
| 234 | if (using_desc_dma(hsotg)) |
| 235 | val = val * 4; |
| 236 | |
| 237 | return val; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for |
| 242 | * device mode TX FIFOs |
| 243 | */ |
| 244 | int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg) |
| 245 | { |
| 246 | int ep_info_size; |
| 247 | int addr; |
| 248 | int tx_addr_max; |
| 249 | u32 np_tx_fifo_size; |
| 250 | |
| 251 | np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size, |
| 252 | hsotg->params.g_np_tx_fifo_size); |
| 253 | |
| 254 | /* Get Endpoint Info Control block size in DWORDs. */ |
| 255 | ep_info_size = dwc2_hsotg_ep_info_size(hsotg); |
| 256 | tx_addr_max = hsotg->hw_params.total_fifo_size - ep_info_size; |
| 257 | |
| 258 | addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size; |
| 259 | if (tx_addr_max <= addr) |
| 260 | return 0; |
| 261 | |
| 262 | return tx_addr_max - addr; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode |
| 267 | * TX FIFOs |
| 268 | */ |
| 269 | int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg) |
| 270 | { |
| 271 | int tx_fifo_count; |
| 272 | int tx_fifo_depth; |
| 273 | |
| 274 | tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg); |
| 275 | |
| 276 | tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg); |
| 277 | |
| 278 | if (!tx_fifo_count) |
| 279 | return tx_fifo_depth; |
| 280 | else |
| 281 | return tx_fifo_depth / tx_fifo_count; |
| 282 | } |
| 283 | |
| 284 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 285 | * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 286 | * @hsotg: The device instance. |
| 287 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 288 | static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 289 | { |
John Youn | 2317eac | 2016-10-17 17:36:23 -0700 | [diff] [blame] | 290 | unsigned int ep; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 291 | unsigned int addr; |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 292 | int timeout; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 293 | u32 val; |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 294 | u32 *txfsz = hsotg->params.g_tx_fifo_size; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 295 | |
Gregory Herrero | 7fcbc95 | 2015-01-09 13:39:06 +0100 | [diff] [blame] | 296 | /* Reset fifo map if not correctly cleared during previous session */ |
| 297 | WARN_ON(hsotg->fifo_map); |
| 298 | hsotg->fifo_map = 0; |
| 299 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 300 | /* set RX/NPTX FIFO sizes */ |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 301 | dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ); |
| 302 | dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) | |
| 303 | (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT), |
| 304 | hsotg->regs + GNPTXFSIZ); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 305 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 306 | /* |
| 307 | * 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] | 308 | * block have overlapping default addresses. This also ensures |
| 309 | * that if the settings have been changed, then they are set to |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 310 | * known values. |
| 311 | */ |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 312 | |
| 313 | /* start at the end of the GNPTXFSIZ, rounded up */ |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 314 | addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 315 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 316 | /* |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 317 | * Configure fifos sizes from provided configuration and assign |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 318 | * them to endpoints dynamically according to maxpacket size value of |
| 319 | * given endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 320 | */ |
John Youn | 2317eac | 2016-10-17 17:36:23 -0700 | [diff] [blame] | 321 | for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) { |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 322 | if (!txfsz[ep]) |
John Youn | 3fa9538 | 2016-10-17 17:36:25 -0700 | [diff] [blame] | 323 | continue; |
| 324 | val = addr; |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 325 | val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT; |
| 326 | WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem, |
John Youn | 3fa9538 | 2016-10-17 17:36:25 -0700 | [diff] [blame] | 327 | "insufficient fifo memory"); |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 328 | addr += txfsz[ep]; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 329 | |
John Youn | 2317eac | 2016-10-17 17:36:23 -0700 | [diff] [blame] | 330 | dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep)); |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 331 | val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep)); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 332 | } |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 333 | |
Sevak Arakelyan | f87c842 | 2017-01-18 18:34:19 -0800 | [diff] [blame] | 334 | dwc2_writel(hsotg->hw_params.total_fifo_size | |
| 335 | addr << GDFIFOCFG_EPINFOBASE_SHIFT, |
| 336 | hsotg->regs + GDFIFOCFG); |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 337 | /* |
| 338 | * according to p428 of the design guide, we need to ensure that |
| 339 | * all fifos are flushed before continuing |
| 340 | */ |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 341 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 342 | dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 343 | GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 344 | |
| 345 | /* wait until the fifos are both flushed */ |
| 346 | timeout = 100; |
| 347 | while (1) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 348 | val = dwc2_readl(hsotg->regs + GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 349 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 350 | if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0) |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 351 | break; |
| 352 | |
| 353 | if (--timeout == 0) { |
| 354 | dev_err(hsotg->dev, |
| 355 | "%s: timeout flushing fifos (GRSTCTL=%08x)\n", |
| 356 | __func__, val); |
Gregory Herrero | 48b20bc | 2015-01-09 13:39:01 +0100 | [diff] [blame] | 357 | break; |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | udelay(1); |
| 361 | } |
| 362 | |
| 363 | dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * @ep: USB endpoint to allocate request for. |
| 368 | * @flags: Allocation flags |
| 369 | * |
| 370 | * Allocate a new USB request structure appropriate for the specified endpoint |
| 371 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 372 | static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 373 | gfp_t flags) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 374 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 375 | struct dwc2_hsotg_req *req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 376 | |
John Youn | ec33efe | 2017-01-17 20:32:41 -0800 | [diff] [blame] | 377 | req = kzalloc(sizeof(*req), flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 378 | if (!req) |
| 379 | return NULL; |
| 380 | |
| 381 | INIT_LIST_HEAD(&req->queue); |
| 382 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 383 | return &req->req; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * is_ep_periodic - return true if the endpoint is in periodic mode. |
| 388 | * @hs_ep: The endpoint to query. |
| 389 | * |
| 390 | * Returns true if the endpoint is in periodic mode, meaning it is being |
| 391 | * used for an Interrupt or ISO transfer. |
| 392 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 393 | static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 394 | { |
| 395 | return hs_ep->periodic; |
| 396 | } |
| 397 | |
| 398 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 399 | * 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] | 400 | * @hsotg: The device state. |
| 401 | * @hs_ep: The endpoint for the request |
| 402 | * @hs_req: The request being processed. |
| 403 | * |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 404 | * 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] | 405 | * 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] | 406 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 407 | static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 408 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 409 | struct dwc2_hsotg_req *hs_req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 410 | { |
| 411 | struct usb_request *req = &hs_req->req; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 412 | |
Jingoo Han | 17d966a | 2013-05-11 21:14:00 +0900 | [diff] [blame] | 413 | usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Vahram Aharonyan | 0f6b80c | 2016-11-09 19:27:56 -0800 | [diff] [blame] | 416 | /* |
| 417 | * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains |
| 418 | * for Control endpoint |
| 419 | * @hsotg: The device state. |
| 420 | * |
| 421 | * This function will allocate 4 descriptor chains for EP 0: 2 for |
| 422 | * Setup stage, per one for IN and OUT data/status transactions. |
| 423 | */ |
| 424 | static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg) |
| 425 | { |
| 426 | hsotg->setup_desc[0] = |
| 427 | dmam_alloc_coherent(hsotg->dev, |
| 428 | sizeof(struct dwc2_dma_desc), |
| 429 | &hsotg->setup_desc_dma[0], |
| 430 | GFP_KERNEL); |
| 431 | if (!hsotg->setup_desc[0]) |
| 432 | goto fail; |
| 433 | |
| 434 | hsotg->setup_desc[1] = |
| 435 | dmam_alloc_coherent(hsotg->dev, |
| 436 | sizeof(struct dwc2_dma_desc), |
| 437 | &hsotg->setup_desc_dma[1], |
| 438 | GFP_KERNEL); |
| 439 | if (!hsotg->setup_desc[1]) |
| 440 | goto fail; |
| 441 | |
| 442 | hsotg->ctrl_in_desc = |
| 443 | dmam_alloc_coherent(hsotg->dev, |
| 444 | sizeof(struct dwc2_dma_desc), |
| 445 | &hsotg->ctrl_in_desc_dma, |
| 446 | GFP_KERNEL); |
| 447 | if (!hsotg->ctrl_in_desc) |
| 448 | goto fail; |
| 449 | |
| 450 | hsotg->ctrl_out_desc = |
| 451 | dmam_alloc_coherent(hsotg->dev, |
| 452 | sizeof(struct dwc2_dma_desc), |
| 453 | &hsotg->ctrl_out_desc_dma, |
| 454 | GFP_KERNEL); |
| 455 | if (!hsotg->ctrl_out_desc) |
| 456 | goto fail; |
| 457 | |
| 458 | return 0; |
| 459 | |
| 460 | fail: |
| 461 | return -ENOMEM; |
| 462 | } |
| 463 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 464 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 465 | * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 466 | * @hsotg: The controller state. |
| 467 | * @hs_ep: The endpoint we're going to write for. |
| 468 | * @hs_req: The request to write data for. |
| 469 | * |
| 470 | * This is called when the TxFIFO has some space in it to hold a new |
| 471 | * transmission and we have something to give it. The actual setup of |
| 472 | * the data size is done elsewhere, so all we have to do is to actually |
| 473 | * write the data. |
| 474 | * |
| 475 | * The return value is zero if there is more space (or nothing was done) |
| 476 | * otherwise -ENOSPC is returned if the FIFO space was used up. |
| 477 | * |
| 478 | * This routine is only needed for PIO |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 479 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 480 | static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 481 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 482 | struct dwc2_hsotg_req *hs_req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 483 | { |
| 484 | bool periodic = is_ep_periodic(hs_ep); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 485 | u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 486 | int buf_pos = hs_req->req.actual; |
| 487 | int to_write = hs_ep->size_loaded; |
| 488 | void *data; |
| 489 | int can_write; |
| 490 | int pkt_round; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 491 | int max_transfer; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 492 | |
| 493 | to_write -= (buf_pos - hs_ep->last_load); |
| 494 | |
| 495 | /* if there's nothing to write, get out early */ |
| 496 | if (to_write == 0) |
| 497 | return 0; |
| 498 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 499 | if (periodic && !hsotg->dedicated_fifos) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 500 | u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 501 | int size_left; |
| 502 | int size_done; |
| 503 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 504 | /* |
| 505 | * work out how much data was loaded so we can calculate |
| 506 | * how much data is left in the fifo. |
| 507 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 508 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 509 | size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 510 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 511 | /* |
| 512 | * if shared fifo, we cannot write anything until the |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 513 | * previous data has been completely sent. |
| 514 | */ |
| 515 | if (hs_ep->fifo_load != 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 516 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 517 | return -ENOSPC; |
| 518 | } |
| 519 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 520 | dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n", |
| 521 | __func__, size_left, |
| 522 | hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size); |
| 523 | |
| 524 | /* how much of the data has moved */ |
| 525 | size_done = hs_ep->size_loaded - size_left; |
| 526 | |
| 527 | /* how much data is left in the fifo */ |
| 528 | can_write = hs_ep->fifo_load - size_done; |
| 529 | dev_dbg(hsotg->dev, "%s: => can_write1=%d\n", |
| 530 | __func__, can_write); |
| 531 | |
| 532 | can_write = hs_ep->fifo_size - can_write; |
| 533 | dev_dbg(hsotg->dev, "%s: => can_write2=%d\n", |
| 534 | __func__, can_write); |
| 535 | |
| 536 | if (can_write <= 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 537 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 538 | return -ENOSPC; |
| 539 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 540 | } else if (hsotg->dedicated_fifos && hs_ep->index != 0) { |
Robert Baldyga | ad674a1 | 2016-08-29 13:38:50 -0700 | [diff] [blame] | 541 | can_write = dwc2_readl(hsotg->regs + |
| 542 | DTXFSTS(hs_ep->fifo_index)); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 543 | |
| 544 | can_write &= 0xffff; |
| 545 | can_write *= 4; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 546 | } else { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 547 | if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 548 | dev_dbg(hsotg->dev, |
| 549 | "%s: no queue slots available (0x%08x)\n", |
| 550 | __func__, gnptxsts); |
| 551 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 552 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 553 | return -ENOSPC; |
| 554 | } |
| 555 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 556 | can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts); |
Ben Dooks | 679f9b7 | 2010-07-19 09:40:41 +0100 | [diff] [blame] | 557 | can_write *= 4; /* fifo size is in 32bit quantities. */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 558 | } |
| 559 | |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 560 | max_transfer = hs_ep->ep.maxpacket * hs_ep->mc; |
| 561 | |
| 562 | dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 563 | __func__, gnptxsts, can_write, to_write, max_transfer); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 564 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 565 | /* |
| 566 | * 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] | 567 | * FIFO, requests of >512 cause the endpoint to get stuck with a |
| 568 | * fragment of the end of the transfer in it. |
| 569 | */ |
Robert Baldyga | 811f330 | 2013-09-24 11:24:28 +0200 | [diff] [blame] | 570 | if (can_write > 512 && !periodic) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 571 | can_write = 512; |
| 572 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 573 | /* |
| 574 | * 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] | 575 | * 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] | 576 | * doing it. |
| 577 | */ |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 578 | if (to_write > max_transfer) { |
| 579 | to_write = max_transfer; |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 580 | |
Robert Baldyga | 5cb2ff0 | 2013-09-19 11:50:18 +0200 | [diff] [blame] | 581 | /* it's needed only when we do not use dedicated fifos */ |
| 582 | if (!hsotg->dedicated_fifos) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 583 | dwc2_hsotg_en_gsint(hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 584 | periodic ? GINTSTS_PTXFEMP : |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 585 | GINTSTS_NPTXFEMP); |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 588 | /* see if we can write data */ |
| 589 | |
| 590 | if (to_write > can_write) { |
| 591 | to_write = can_write; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 592 | pkt_round = to_write % max_transfer; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 593 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 594 | /* |
| 595 | * Round the write down to an |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 596 | * exact number of packets. |
| 597 | * |
| 598 | * Note, we do not currently check to see if we can ever |
| 599 | * write a full packet or not to the FIFO. |
| 600 | */ |
| 601 | |
| 602 | if (pkt_round) |
| 603 | to_write -= pkt_round; |
| 604 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 605 | /* |
| 606 | * enable correct FIFO interrupt to alert us when there |
| 607 | * is more room left. |
| 608 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 609 | |
Robert Baldyga | 5cb2ff0 | 2013-09-19 11:50:18 +0200 | [diff] [blame] | 610 | /* it's needed only when we do not use dedicated fifos */ |
| 611 | if (!hsotg->dedicated_fifos) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 612 | dwc2_hsotg_en_gsint(hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 613 | periodic ? GINTSTS_PTXFEMP : |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 614 | GINTSTS_NPTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 618 | to_write, hs_req->req.length, can_write, buf_pos); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 619 | |
| 620 | if (to_write <= 0) |
| 621 | return -ENOSPC; |
| 622 | |
| 623 | hs_req->req.actual = buf_pos + to_write; |
| 624 | hs_ep->total_data += to_write; |
| 625 | |
| 626 | if (periodic) |
| 627 | hs_ep->fifo_load += to_write; |
| 628 | |
| 629 | to_write = DIV_ROUND_UP(to_write, 4); |
| 630 | data = hs_req->req.buf + buf_pos; |
| 631 | |
Matt Porter | 1a7ed5b | 2014-02-03 10:29:09 -0500 | [diff] [blame] | 632 | iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 633 | |
| 634 | return (to_write >= can_write) ? -ENOSPC : 0; |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * get_ep_limit - get the maximum data legnth for this endpoint |
| 639 | * @hs_ep: The endpoint |
| 640 | * |
| 641 | * Return the maximum data that can be queued in one go on a given endpoint |
| 642 | * so that transfers that are too long can be split. |
| 643 | */ |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 644 | static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 645 | { |
| 646 | int index = hs_ep->index; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 647 | unsigned int maxsize; |
| 648 | unsigned int maxpkt; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 649 | |
| 650 | if (index != 0) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 651 | maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1; |
| 652 | maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 653 | } else { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 654 | maxsize = 64 + 64; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 655 | if (hs_ep->dir_in) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 656 | maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 657 | else |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 658 | maxpkt = 2; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* we made the constant loading easier above by using +1 */ |
| 662 | maxpkt--; |
| 663 | maxsize--; |
| 664 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 665 | /* |
| 666 | * constrain by packet count if maxpkts*pktsize is greater |
| 667 | * than the length register size. |
| 668 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 669 | |
| 670 | if ((maxpkt * hs_ep->ep.maxpacket) < maxsize) |
| 671 | maxsize = maxpkt * hs_ep->ep.maxpacket; |
| 672 | |
| 673 | return maxsize; |
| 674 | } |
| 675 | |
| 676 | /** |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 677 | * dwc2_hsotg_read_frameno - read current frame number |
| 678 | * @hsotg: The device instance |
| 679 | * |
| 680 | * Return the current frame number |
| 681 | */ |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 682 | static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg) |
| 683 | { |
| 684 | u32 dsts; |
| 685 | |
| 686 | dsts = dwc2_readl(hsotg->regs + DSTS); |
| 687 | dsts &= DSTS_SOFFN_MASK; |
| 688 | dsts >>= DSTS_SOFFN_SHIFT; |
| 689 | |
| 690 | return dsts; |
| 691 | } |
| 692 | |
| 693 | /** |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 694 | * dwc2_gadget_get_chain_limit - get the maximum data payload value of the |
| 695 | * DMA descriptor chain prepared for specific endpoint |
| 696 | * @hs_ep: The endpoint |
| 697 | * |
| 698 | * Return the maximum data that can be queued in one go on a given endpoint |
| 699 | * depending on its descriptor chain capacity so that transfers that |
| 700 | * are too long can be split. |
| 701 | */ |
| 702 | static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep) |
| 703 | { |
| 704 | int is_isoc = hs_ep->isochronous; |
| 705 | unsigned int maxsize; |
| 706 | |
| 707 | if (is_isoc) |
| 708 | maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT : |
| 709 | DEV_DMA_ISOC_RX_NBYTES_LIMIT; |
| 710 | else |
| 711 | maxsize = DEV_DMA_NBYTES_LIMIT; |
| 712 | |
| 713 | /* Above size of one descriptor was chosen, multiple it */ |
| 714 | maxsize *= MAX_DMA_DESC_NUM_GENERIC; |
| 715 | |
| 716 | return maxsize; |
| 717 | } |
| 718 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 719 | /* |
| 720 | * dwc2_gadget_get_desc_params - get DMA descriptor parameters. |
| 721 | * @hs_ep: The endpoint |
| 722 | * @mask: RX/TX bytes mask to be defined |
| 723 | * |
| 724 | * Returns maximum data payload for one descriptor after analyzing endpoint |
| 725 | * characteristics. |
| 726 | * DMA descriptor transfer bytes limit depends on EP type: |
| 727 | * Control out - MPS, |
| 728 | * Isochronous - descriptor rx/tx bytes bitfield limit, |
| 729 | * Control In/Bulk/Interrupt - multiple of mps. This will allow to not |
| 730 | * have concatenations from various descriptors within one packet. |
| 731 | * |
| 732 | * Selects corresponding mask for RX/TX bytes as well. |
| 733 | */ |
| 734 | static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask) |
| 735 | { |
| 736 | u32 mps = hs_ep->ep.maxpacket; |
| 737 | int dir_in = hs_ep->dir_in; |
| 738 | u32 desc_size = 0; |
| 739 | |
| 740 | if (!hs_ep->index && !dir_in) { |
| 741 | desc_size = mps; |
| 742 | *mask = DEV_DMA_NBYTES_MASK; |
| 743 | } else if (hs_ep->isochronous) { |
| 744 | if (dir_in) { |
| 745 | desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT; |
| 746 | *mask = DEV_DMA_ISOC_TX_NBYTES_MASK; |
| 747 | } else { |
| 748 | desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT; |
| 749 | *mask = DEV_DMA_ISOC_RX_NBYTES_MASK; |
| 750 | } |
| 751 | } else { |
| 752 | desc_size = DEV_DMA_NBYTES_LIMIT; |
| 753 | *mask = DEV_DMA_NBYTES_MASK; |
| 754 | |
| 755 | /* Round down desc_size to be mps multiple */ |
| 756 | desc_size -= desc_size % mps; |
| 757 | } |
| 758 | |
| 759 | return desc_size; |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain. |
| 764 | * @hs_ep: The endpoint |
| 765 | * @dma_buff: DMA address to use |
| 766 | * @len: Length of the transfer |
| 767 | * |
| 768 | * This function will iterate over descriptor chain and fill its entries |
| 769 | * with corresponding information based on transfer data. |
| 770 | */ |
| 771 | static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep, |
| 772 | dma_addr_t dma_buff, |
| 773 | unsigned int len) |
| 774 | { |
| 775 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 776 | int dir_in = hs_ep->dir_in; |
| 777 | struct dwc2_dma_desc *desc = hs_ep->desc_list; |
| 778 | u32 mps = hs_ep->ep.maxpacket; |
| 779 | u32 maxsize = 0; |
| 780 | u32 offset = 0; |
| 781 | u32 mask = 0; |
| 782 | int i; |
| 783 | |
| 784 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
| 785 | |
| 786 | hs_ep->desc_count = (len / maxsize) + |
| 787 | ((len % maxsize) ? 1 : 0); |
| 788 | if (len == 0) |
| 789 | hs_ep->desc_count = 1; |
| 790 | |
| 791 | for (i = 0; i < hs_ep->desc_count; ++i) { |
| 792 | desc->status = 0; |
| 793 | desc->status |= (DEV_DMA_BUFF_STS_HBUSY |
| 794 | << DEV_DMA_BUFF_STS_SHIFT); |
| 795 | |
| 796 | if (len > maxsize) { |
| 797 | if (!hs_ep->index && !dir_in) |
| 798 | desc->status |= (DEV_DMA_L | DEV_DMA_IOC); |
| 799 | |
| 800 | desc->status |= (maxsize << |
| 801 | DEV_DMA_NBYTES_SHIFT & mask); |
| 802 | desc->buf = dma_buff + offset; |
| 803 | |
| 804 | len -= maxsize; |
| 805 | offset += maxsize; |
| 806 | } else { |
| 807 | desc->status |= (DEV_DMA_L | DEV_DMA_IOC); |
| 808 | |
| 809 | if (dir_in) |
| 810 | desc->status |= (len % mps) ? DEV_DMA_SHORT : |
| 811 | ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0); |
| 812 | if (len > maxsize) |
| 813 | dev_err(hsotg->dev, "wrong len %d\n", len); |
| 814 | |
| 815 | desc->status |= |
| 816 | len << DEV_DMA_NBYTES_SHIFT & mask; |
| 817 | desc->buf = dma_buff + offset; |
| 818 | } |
| 819 | |
| 820 | desc->status &= ~DEV_DMA_BUFF_STS_MASK; |
| 821 | desc->status |= (DEV_DMA_BUFF_STS_HREADY |
| 822 | << DEV_DMA_BUFF_STS_SHIFT); |
| 823 | desc++; |
| 824 | } |
| 825 | } |
| 826 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 827 | /* |
| 828 | * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain. |
| 829 | * @hs_ep: The isochronous endpoint. |
| 830 | * @dma_buff: usb requests dma buffer. |
| 831 | * @len: usb request transfer length. |
| 832 | * |
| 833 | * Finds out index of first free entry either in the bottom or up half of |
| 834 | * descriptor chain depend on which is under SW control and not processed |
| 835 | * by HW. Then fills that descriptor with the data of the arrived usb request, |
| 836 | * frame info, sets Last and IOC bits increments next_desc. If filled |
| 837 | * descriptor is not the first one, removes L bit from the previous descriptor |
| 838 | * status. |
| 839 | */ |
| 840 | static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep, |
| 841 | dma_addr_t dma_buff, unsigned int len) |
| 842 | { |
| 843 | struct dwc2_dma_desc *desc; |
| 844 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 845 | u32 index; |
| 846 | u32 maxsize = 0; |
| 847 | u32 mask = 0; |
| 848 | |
| 849 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
| 850 | if (len > maxsize) { |
| 851 | dev_err(hsotg->dev, "wrong len %d\n", len); |
| 852 | return -EINVAL; |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * If SW has already filled half of chain, then return and wait for |
| 857 | * the other chain to be processed by HW. |
| 858 | */ |
| 859 | if (hs_ep->next_desc == MAX_DMA_DESC_NUM_GENERIC / 2) |
| 860 | return -EBUSY; |
| 861 | |
| 862 | /* Increment frame number by interval for IN */ |
| 863 | if (hs_ep->dir_in) |
| 864 | dwc2_gadget_incr_frame_num(hs_ep); |
| 865 | |
| 866 | index = (MAX_DMA_DESC_NUM_GENERIC / 2) * hs_ep->isoc_chain_num + |
| 867 | hs_ep->next_desc; |
| 868 | |
| 869 | /* Sanity check of calculated index */ |
| 870 | if ((hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC) || |
| 871 | (!hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC / 2)) { |
| 872 | dev_err(hsotg->dev, "wrong index %d for iso chain\n", index); |
| 873 | return -EINVAL; |
| 874 | } |
| 875 | |
| 876 | desc = &hs_ep->desc_list[index]; |
| 877 | |
| 878 | /* Clear L bit of previous desc if more than one entries in the chain */ |
| 879 | if (hs_ep->next_desc) |
| 880 | hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L; |
| 881 | |
| 882 | dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n", |
| 883 | __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index); |
| 884 | |
| 885 | desc->status = 0; |
| 886 | desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT); |
| 887 | |
| 888 | desc->buf = dma_buff; |
| 889 | desc->status |= (DEV_DMA_L | DEV_DMA_IOC | |
| 890 | ((len << DEV_DMA_NBYTES_SHIFT) & mask)); |
| 891 | |
| 892 | if (hs_ep->dir_in) { |
| 893 | desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) & |
| 894 | DEV_DMA_ISOC_PID_MASK) | |
| 895 | ((len % hs_ep->ep.maxpacket) ? |
| 896 | DEV_DMA_SHORT : 0) | |
| 897 | ((hs_ep->target_frame << |
| 898 | DEV_DMA_ISOC_FRNUM_SHIFT) & |
| 899 | DEV_DMA_ISOC_FRNUM_MASK); |
| 900 | } |
| 901 | |
| 902 | desc->status &= ~DEV_DMA_BUFF_STS_MASK; |
| 903 | desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT); |
| 904 | |
| 905 | /* Update index of last configured entry in the chain */ |
| 906 | hs_ep->next_desc++; |
| 907 | |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA |
| 913 | * @hs_ep: The isochronous endpoint. |
| 914 | * |
| 915 | * Prepare first descriptor chain for isochronous endpoints. Afterwards |
| 916 | * write DMA address to HW and enable the endpoint. |
| 917 | * |
| 918 | * Switch between descriptor chains via isoc_chain_num to give SW opportunity |
| 919 | * to prepare second descriptor chain while first one is being processed by HW. |
| 920 | */ |
| 921 | static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 922 | { |
| 923 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 924 | struct dwc2_hsotg_req *hs_req, *treq; |
| 925 | int index = hs_ep->index; |
| 926 | int ret; |
| 927 | u32 dma_reg; |
| 928 | u32 depctl; |
| 929 | u32 ctrl; |
| 930 | |
| 931 | if (list_empty(&hs_ep->queue)) { |
| 932 | dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__); |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) { |
| 937 | ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma, |
| 938 | hs_req->req.length); |
| 939 | if (ret) { |
| 940 | dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__); |
| 941 | break; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 946 | dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index); |
| 947 | |
| 948 | /* write descriptor chain address to control register */ |
| 949 | dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg); |
| 950 | |
| 951 | ctrl = dwc2_readl(hsotg->regs + depctl); |
| 952 | ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK; |
| 953 | dwc2_writel(ctrl, hsotg->regs + depctl); |
| 954 | |
| 955 | /* Switch ISOC descriptor chain number being processed by SW*/ |
| 956 | hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1; |
| 957 | hs_ep->next_desc = 0; |
| 958 | } |
| 959 | |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 960 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 961 | * 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] | 962 | * @hsotg: The controller state. |
| 963 | * @hs_ep: The endpoint to process a request for |
| 964 | * @hs_req: The request to start. |
| 965 | * @continuing: True if we are doing more for the current request. |
| 966 | * |
| 967 | * Start the given request running by setting the endpoint registers |
| 968 | * appropriately, and writing any data to the FIFOs. |
| 969 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 970 | static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 971 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 972 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 973 | bool continuing) |
| 974 | { |
| 975 | struct usb_request *ureq = &hs_req->req; |
| 976 | int index = hs_ep->index; |
| 977 | int dir_in = hs_ep->dir_in; |
| 978 | u32 epctrl_reg; |
| 979 | u32 epsize_reg; |
| 980 | u32 epsize; |
| 981 | u32 ctrl; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 982 | unsigned int length; |
| 983 | unsigned int packets; |
| 984 | unsigned int maxreq; |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 985 | unsigned int dma_reg; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 986 | |
| 987 | if (index != 0) { |
| 988 | if (hs_ep->req && !continuing) { |
| 989 | dev_err(hsotg->dev, "%s: active request\n", __func__); |
| 990 | WARN_ON(1); |
| 991 | return; |
| 992 | } else if (hs_ep->req != hs_req && continuing) { |
| 993 | dev_err(hsotg->dev, |
| 994 | "%s: continue different req\n", __func__); |
| 995 | WARN_ON(1); |
| 996 | return; |
| 997 | } |
| 998 | } |
| 999 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1000 | dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1001 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 1002 | epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1003 | |
| 1004 | 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] | 1005 | __func__, dwc2_readl(hsotg->regs + epctrl_reg), index, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1006 | hs_ep->dir_in ? "in" : "out"); |
| 1007 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1008 | /* If endpoint is stalled, we will restart request later */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1009 | ctrl = dwc2_readl(hsotg->regs + epctrl_reg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1010 | |
Mian Yousaf Kaukab | b2d4c54 | 2015-09-29 12:08:22 +0200 | [diff] [blame] | 1011 | if (index && ctrl & DXEPCTL_STALL) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1012 | dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index); |
| 1013 | return; |
| 1014 | } |
| 1015 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1016 | length = ureq->length - ureq->actual; |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1017 | dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n", |
| 1018 | ureq->length, ureq->actual); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1019 | |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 1020 | if (!using_desc_dma(hsotg)) |
| 1021 | maxreq = get_ep_limit(hs_ep); |
| 1022 | else |
| 1023 | maxreq = dwc2_gadget_get_chain_limit(hs_ep); |
| 1024 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1025 | if (length > maxreq) { |
| 1026 | int round = maxreq % hs_ep->ep.maxpacket; |
| 1027 | |
| 1028 | dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n", |
| 1029 | __func__, length, maxreq, round); |
| 1030 | |
| 1031 | /* round down to multiple of packets */ |
| 1032 | if (round) |
| 1033 | maxreq -= round; |
| 1034 | |
| 1035 | length = maxreq; |
| 1036 | } |
| 1037 | |
| 1038 | if (length) |
| 1039 | packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket); |
| 1040 | else |
| 1041 | packets = 1; /* send one packet if length is zero. */ |
| 1042 | |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1043 | if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) { |
| 1044 | dev_err(hsotg->dev, "req length > maxpacket*mc\n"); |
| 1045 | return; |
| 1046 | } |
| 1047 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1048 | if (dir_in && index != 0) |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1049 | if (hs_ep->isochronous) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1050 | epsize = DXEPTSIZ_MC(packets); |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1051 | else |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1052 | epsize = DXEPTSIZ_MC(1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1053 | else |
| 1054 | epsize = 0; |
| 1055 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1056 | /* |
| 1057 | * zero length packet should be programmed on its own and should not |
| 1058 | * be counted in DIEPTSIZ.PktCnt with other packets. |
| 1059 | */ |
| 1060 | if (dir_in && ureq->zero && !continuing) { |
| 1061 | /* Test if zlp is actually required. */ |
| 1062 | if ((ureq->length >= hs_ep->ep.maxpacket) && |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1063 | !(ureq->length % hs_ep->ep.maxpacket)) |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 1064 | hs_ep->send_zlp = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1067 | epsize |= DXEPTSIZ_PKTCNT(packets); |
| 1068 | epsize |= DXEPTSIZ_XFERSIZE(length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1069 | |
| 1070 | dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n", |
| 1071 | __func__, packets, length, ureq->length, epsize, epsize_reg); |
| 1072 | |
| 1073 | /* store the request as the current one we're doing */ |
| 1074 | hs_ep->req = hs_req; |
| 1075 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1076 | if (using_desc_dma(hsotg)) { |
| 1077 | u32 offset = 0; |
| 1078 | u32 mps = hs_ep->ep.maxpacket; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1079 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1080 | /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */ |
| 1081 | if (!dir_in) { |
| 1082 | if (!index) |
| 1083 | length = mps; |
| 1084 | else if (length % mps) |
| 1085 | length += (mps - (length % mps)); |
| 1086 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1087 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1088 | /* |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1089 | * If more data to send, adjust DMA for EP0 out data stage. |
| 1090 | * ureq->dma stays unchanged, hence increment it by already |
| 1091 | * passed passed data count before starting new transaction. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1092 | */ |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1093 | if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT && |
| 1094 | continuing) |
| 1095 | offset = ureq->actual; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1096 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1097 | /* Fill DDMA chain entries */ |
| 1098 | dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset, |
| 1099 | length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1100 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1101 | /* write descriptor chain address to control register */ |
| 1102 | dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg); |
| 1103 | |
| 1104 | dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n", |
| 1105 | __func__, (u32)hs_ep->desc_list_dma, dma_reg); |
| 1106 | } else { |
| 1107 | /* write size / packets */ |
| 1108 | dwc2_writel(epsize, hsotg->regs + epsize_reg); |
| 1109 | |
Razmik Karapetyan | 729e657 | 2016-11-16 15:33:55 -0800 | [diff] [blame] | 1110 | if (using_dma(hsotg) && !continuing && (length != 0)) { |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1111 | /* |
| 1112 | * write DMA address to control register, buffer |
| 1113 | * already synced by dwc2_hsotg_ep_queue(). |
| 1114 | */ |
| 1115 | |
| 1116 | dwc2_writel(ureq->dma, hsotg->regs + dma_reg); |
| 1117 | |
| 1118 | dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n", |
| 1119 | __func__, &ureq->dma, dma_reg); |
| 1120 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1121 | } |
| 1122 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1123 | if (hs_ep->isochronous && hs_ep->interval == 1) { |
| 1124 | hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg); |
| 1125 | dwc2_gadget_incr_frame_num(hs_ep); |
| 1126 | |
| 1127 | if (hs_ep->target_frame & 0x1) |
| 1128 | ctrl |= DXEPCTL_SETODDFR; |
| 1129 | else |
| 1130 | ctrl |= DXEPCTL_SETEVENFR; |
| 1131 | } |
| 1132 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1133 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1134 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1135 | dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state); |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1136 | |
| 1137 | /* For Setup request do not clear NAK */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1138 | if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP)) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1139 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1140 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1141 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1142 | dwc2_writel(ctrl, hsotg->regs + epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1143 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1144 | /* |
| 1145 | * set these, it seems that DMA support increments past the end |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1146 | * of the packet buffer so we need to calculate the length from |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1147 | * this information. |
| 1148 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1149 | hs_ep->size_loaded = length; |
| 1150 | hs_ep->last_load = ureq->actual; |
| 1151 | |
| 1152 | if (dir_in && !using_dma(hsotg)) { |
| 1153 | /* set these anyway, we may need them for non-periodic in */ |
| 1154 | hs_ep->fifo_load = 0; |
| 1155 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1156 | dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1157 | } |
| 1158 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1159 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1160 | * Note, trying to clear the NAK here causes problems with transmit |
| 1161 | * on the S3C6400 ending up with the TXFIFO becoming full. |
| 1162 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1163 | |
| 1164 | /* check ep is enabled */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1165 | if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA)) |
Mian Yousaf Kaukab | 1a0ed86 | 2015-01-09 13:39:00 +0100 | [diff] [blame] | 1166 | dev_dbg(hsotg->dev, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1167 | "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1168 | index, dwc2_readl(hsotg->regs + epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1169 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1170 | dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1171 | __func__, dwc2_readl(hsotg->regs + epctrl_reg)); |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 1172 | |
| 1173 | /* enable ep interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1174 | 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] | 1175 | } |
| 1176 | |
| 1177 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1178 | * 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] | 1179 | * @hsotg: The device state. |
| 1180 | * @hs_ep: The endpoint the request is on. |
| 1181 | * @req: The request being processed. |
| 1182 | * |
| 1183 | * We've been asked to queue a request, so ensure that the memory buffer |
| 1184 | * is correctly setup for DMA. If we've been passed an extant DMA address |
| 1185 | * then ensure the buffer has been synced to memory. If our buffer has no |
| 1186 | * DMA memory, then we map the memory and mark our request to allow us to |
| 1187 | * cleanup on completion. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1188 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1189 | static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1190 | struct dwc2_hsotg_ep *hs_ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1191 | struct usb_request *req) |
| 1192 | { |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 1193 | int ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1194 | |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 1195 | ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in); |
| 1196 | if (ret) |
| 1197 | goto dma_error; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1198 | |
| 1199 | return 0; |
| 1200 | |
| 1201 | dma_error: |
| 1202 | dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n", |
| 1203 | __func__, req->buf, req->length); |
| 1204 | |
| 1205 | return -EIO; |
| 1206 | } |
| 1207 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1208 | static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg, |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 1209 | struct dwc2_hsotg_ep *hs_ep, |
| 1210 | struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1211 | { |
| 1212 | void *req_buf = hs_req->req.buf; |
| 1213 | |
| 1214 | /* If dma is not being used or buffer is aligned */ |
| 1215 | if (!using_dma(hsotg) || !((long)req_buf & 3)) |
| 1216 | return 0; |
| 1217 | |
| 1218 | WARN_ON(hs_req->saved_req_buf); |
| 1219 | |
| 1220 | dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1221 | hs_ep->ep.name, req_buf, hs_req->req.length); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1222 | |
| 1223 | hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC); |
| 1224 | if (!hs_req->req.buf) { |
| 1225 | hs_req->req.buf = req_buf; |
| 1226 | dev_err(hsotg->dev, |
| 1227 | "%s: unable to allocate memory for bounce buffer\n", |
| 1228 | __func__); |
| 1229 | return -ENOMEM; |
| 1230 | } |
| 1231 | |
| 1232 | /* Save actual buffer */ |
| 1233 | hs_req->saved_req_buf = req_buf; |
| 1234 | |
| 1235 | if (hs_ep->dir_in) |
| 1236 | memcpy(hs_req->req.buf, req_buf, hs_req->req.length); |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 1240 | static void |
| 1241 | dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg, |
| 1242 | struct dwc2_hsotg_ep *hs_ep, |
| 1243 | struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1244 | { |
| 1245 | /* If dma is not being used or buffer was aligned */ |
| 1246 | if (!using_dma(hsotg) || !hs_req->saved_req_buf) |
| 1247 | return; |
| 1248 | |
| 1249 | dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__, |
| 1250 | hs_ep->ep.name, hs_req->req.status, hs_req->req.actual); |
| 1251 | |
| 1252 | /* Copy data from bounce buffer on successful out transfer */ |
| 1253 | if (!hs_ep->dir_in && !hs_req->req.status) |
| 1254 | memcpy(hs_req->saved_req_buf, hs_req->req.buf, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1255 | hs_req->req.actual); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1256 | |
| 1257 | /* Free bounce buffer */ |
| 1258 | kfree(hs_req->req.buf); |
| 1259 | |
| 1260 | hs_req->req.buf = hs_req->saved_req_buf; |
| 1261 | hs_req->saved_req_buf = NULL; |
| 1262 | } |
| 1263 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 1264 | /** |
| 1265 | * dwc2_gadget_target_frame_elapsed - Checks target frame |
| 1266 | * @hs_ep: The driver endpoint to check |
| 1267 | * |
| 1268 | * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop |
| 1269 | * corresponding transfer. |
| 1270 | */ |
| 1271 | static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep) |
| 1272 | { |
| 1273 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 1274 | u32 target_frame = hs_ep->target_frame; |
| 1275 | u32 current_frame = dwc2_hsotg_read_frameno(hsotg); |
| 1276 | bool frame_overrun = hs_ep->frame_overrun; |
| 1277 | |
| 1278 | if (!frame_overrun && current_frame >= target_frame) |
| 1279 | return true; |
| 1280 | |
| 1281 | if (frame_overrun && current_frame >= target_frame && |
| 1282 | ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2)) |
| 1283 | return true; |
| 1284 | |
| 1285 | return false; |
| 1286 | } |
| 1287 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1288 | /* |
| 1289 | * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers |
| 1290 | * @hsotg: The driver state |
| 1291 | * @hs_ep: the ep descriptor chain is for |
| 1292 | * |
| 1293 | * Called to update EP0 structure's pointers depend on stage of |
| 1294 | * control transfer. |
| 1295 | */ |
| 1296 | static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg, |
| 1297 | struct dwc2_hsotg_ep *hs_ep) |
| 1298 | { |
| 1299 | switch (hsotg->ep0_state) { |
| 1300 | case DWC2_EP0_SETUP: |
| 1301 | case DWC2_EP0_STATUS_OUT: |
| 1302 | hs_ep->desc_list = hsotg->setup_desc[0]; |
| 1303 | hs_ep->desc_list_dma = hsotg->setup_desc_dma[0]; |
| 1304 | break; |
| 1305 | case DWC2_EP0_DATA_IN: |
| 1306 | case DWC2_EP0_STATUS_IN: |
| 1307 | hs_ep->desc_list = hsotg->ctrl_in_desc; |
| 1308 | hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma; |
| 1309 | break; |
| 1310 | case DWC2_EP0_DATA_OUT: |
| 1311 | hs_ep->desc_list = hsotg->ctrl_out_desc; |
| 1312 | hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma; |
| 1313 | break; |
| 1314 | default: |
| 1315 | dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n", |
| 1316 | hsotg->ep0_state); |
| 1317 | return -EINVAL; |
| 1318 | } |
| 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1323 | static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1324 | gfp_t gfp_flags) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1325 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1326 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 1327 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1328 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1329 | bool first; |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1330 | int ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1331 | |
| 1332 | dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n", |
| 1333 | ep->name, req, req->length, req->buf, req->no_interrupt, |
| 1334 | req->zero, req->short_not_ok); |
| 1335 | |
Gregory Herrero | 7ababa9 | 2015-04-29 22:09:08 +0200 | [diff] [blame] | 1336 | /* Prevent new request submission when controller is suspended */ |
| 1337 | if (hs->lx_state == DWC2_L2) { |
| 1338 | dev_dbg(hs->dev, "%s: don't submit request while suspended\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1339 | __func__); |
Gregory Herrero | 7ababa9 | 2015-04-29 22:09:08 +0200 | [diff] [blame] | 1340 | return -EAGAIN; |
| 1341 | } |
| 1342 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1343 | /* initialise status of the request */ |
| 1344 | INIT_LIST_HEAD(&hs_req->queue); |
| 1345 | req->actual = 0; |
| 1346 | req->status = -EINPROGRESS; |
| 1347 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1348 | 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] | 1349 | if (ret) |
| 1350 | return ret; |
| 1351 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1352 | /* if we're using DMA, sync the buffers as necessary */ |
| 1353 | if (using_dma(hs)) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1354 | ret = dwc2_hsotg_map_dma(hs, hs_ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1355 | if (ret) |
| 1356 | return ret; |
| 1357 | } |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1358 | /* If using descriptor DMA configure EP0 descriptor chain pointers */ |
| 1359 | if (using_desc_dma(hs) && !hs_ep->index) { |
| 1360 | ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep); |
| 1361 | if (ret) |
| 1362 | return ret; |
| 1363 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1364 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1365 | first = list_empty(&hs_ep->queue); |
| 1366 | list_add_tail(&hs_req->queue, &hs_ep->queue); |
| 1367 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1368 | /* |
| 1369 | * Handle DDMA isochronous transfers separately - just add new entry |
| 1370 | * to the half of descriptor chain that is not processed by HW. |
| 1371 | * Transfer will be started once SW gets either one of NAK or |
| 1372 | * OutTknEpDis interrupts. |
| 1373 | */ |
| 1374 | if (using_desc_dma(hs) && hs_ep->isochronous && |
| 1375 | hs_ep->target_frame != TARGET_FRAME_INITIAL) { |
| 1376 | ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma, |
| 1377 | hs_req->req.length); |
| 1378 | if (ret) |
| 1379 | dev_dbg(hs->dev, "%s: ISO desc chain full\n", __func__); |
| 1380 | |
| 1381 | return 0; |
| 1382 | } |
| 1383 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1384 | if (first) { |
| 1385 | if (!hs_ep->isochronous) { |
| 1386 | dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); |
| 1387 | return 0; |
| 1388 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1389 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1390 | while (dwc2_gadget_target_frame_elapsed(hs_ep)) |
| 1391 | dwc2_gadget_incr_frame_num(hs_ep); |
| 1392 | |
| 1393 | if (hs_ep->target_frame != TARGET_FRAME_INITIAL) |
| 1394 | dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); |
| 1395 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1399 | static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1400 | gfp_t gfp_flags) |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1401 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1402 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1403 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1404 | unsigned long flags = 0; |
| 1405 | int ret = 0; |
| 1406 | |
| 1407 | spin_lock_irqsave(&hs->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1408 | ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1409 | spin_unlock_irqrestore(&hs->lock, flags); |
| 1410 | |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1414 | static void dwc2_hsotg_ep_free_request(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1415 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1416 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1417 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1418 | |
| 1419 | kfree(hs_req); |
| 1420 | } |
| 1421 | |
| 1422 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1423 | * dwc2_hsotg_complete_oursetup - setup completion callback |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1424 | * @ep: The endpoint the request was on. |
| 1425 | * @req: The request completed. |
| 1426 | * |
| 1427 | * Called on completion of any requests the driver itself |
| 1428 | * submitted that need cleaning up. |
| 1429 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1430 | static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1431 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1432 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1433 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1434 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1435 | |
| 1436 | dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req); |
| 1437 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1438 | dwc2_hsotg_ep_free_request(ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * ep_from_windex - convert control wIndex value to endpoint |
| 1443 | * @hsotg: The driver state. |
| 1444 | * @windex: The control request wIndex field (in host order). |
| 1445 | * |
| 1446 | * Convert the given wIndex into a pointer to an driver endpoint |
| 1447 | * structure, or return NULL if it is not a valid endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1448 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1449 | static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1450 | u32 windex) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1451 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1452 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1453 | int dir = (windex & USB_DIR_IN) ? 1 : 0; |
| 1454 | int idx = windex & 0x7F; |
| 1455 | |
| 1456 | if (windex >= 0x100) |
| 1457 | return NULL; |
| 1458 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 1459 | if (idx > hsotg->num_of_eps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1460 | return NULL; |
| 1461 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1462 | ep = index_to_ep(hsotg, idx, dir); |
| 1463 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1464 | if (idx && ep->dir_in != dir) |
| 1465 | return NULL; |
| 1466 | |
| 1467 | return ep; |
| 1468 | } |
| 1469 | |
| 1470 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1471 | * dwc2_hsotg_set_test_mode - Enable usb Test Modes |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1472 | * @hsotg: The driver state. |
| 1473 | * @testmode: requested usb test mode |
| 1474 | * Enable usb Test Mode requested by the Host. |
| 1475 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1476 | int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode) |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1477 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1478 | int dctl = dwc2_readl(hsotg->regs + DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1479 | |
| 1480 | dctl &= ~DCTL_TSTCTL_MASK; |
| 1481 | switch (testmode) { |
| 1482 | case TEST_J: |
| 1483 | case TEST_K: |
| 1484 | case TEST_SE0_NAK: |
| 1485 | case TEST_PACKET: |
| 1486 | case TEST_FORCE_EN: |
| 1487 | dctl |= testmode << DCTL_TSTCTL_SHIFT; |
| 1488 | break; |
| 1489 | default: |
| 1490 | return -EINVAL; |
| 1491 | } |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1492 | dwc2_writel(dctl, hsotg->regs + DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1493 | return 0; |
| 1494 | } |
| 1495 | |
| 1496 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1497 | * dwc2_hsotg_send_reply - send reply to control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1498 | * @hsotg: The device state |
| 1499 | * @ep: Endpoint 0 |
| 1500 | * @buff: Buffer for request |
| 1501 | * @length: Length of reply. |
| 1502 | * |
| 1503 | * Create a request and queue it on the given endpoint. This is useful as |
| 1504 | * an internal method of sending replies to certain control requests, etc. |
| 1505 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1506 | static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1507 | struct dwc2_hsotg_ep *ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1508 | void *buff, |
| 1509 | int length) |
| 1510 | { |
| 1511 | struct usb_request *req; |
| 1512 | int ret; |
| 1513 | |
| 1514 | dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length); |
| 1515 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1516 | req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1517 | hsotg->ep0_reply = req; |
| 1518 | if (!req) { |
| 1519 | dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__); |
| 1520 | return -ENOMEM; |
| 1521 | } |
| 1522 | |
| 1523 | req->buf = hsotg->ep0_buff; |
| 1524 | req->length = length; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1525 | /* |
| 1526 | * zero flag is for sending zlp in DATA IN stage. It has no impact on |
| 1527 | * STATUS stage. |
| 1528 | */ |
| 1529 | req->zero = 0; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1530 | req->complete = dwc2_hsotg_complete_oursetup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1531 | |
| 1532 | if (length) |
| 1533 | memcpy(req->buf, buff, length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1534 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1535 | ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1536 | if (ret) { |
| 1537 | dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__); |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
| 1544 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1545 | * dwc2_hsotg_process_req_status - process request GET_STATUS |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1546 | * @hsotg: The device state |
| 1547 | * @ctrl: USB control request |
| 1548 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1549 | static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1550 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1551 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1552 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 1553 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1554 | __le16 reply; |
| 1555 | int ret; |
| 1556 | |
| 1557 | dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__); |
| 1558 | |
| 1559 | if (!ep0->dir_in) { |
| 1560 | dev_warn(hsotg->dev, "%s: direction out?\n", __func__); |
| 1561 | return -EINVAL; |
| 1562 | } |
| 1563 | |
| 1564 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1565 | case USB_RECIP_DEVICE: |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 1566 | /* |
| 1567 | * bit 0 => self powered |
| 1568 | * bit 1 => remote wakeup |
| 1569 | */ |
| 1570 | reply = cpu_to_le16(0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1571 | break; |
| 1572 | |
| 1573 | case USB_RECIP_INTERFACE: |
| 1574 | /* currently, the data result should be zero */ |
| 1575 | reply = cpu_to_le16(0); |
| 1576 | break; |
| 1577 | |
| 1578 | case USB_RECIP_ENDPOINT: |
| 1579 | ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex)); |
| 1580 | if (!ep) |
| 1581 | return -ENOENT; |
| 1582 | |
| 1583 | reply = cpu_to_le16(ep->halted ? 1 : 0); |
| 1584 | break; |
| 1585 | |
| 1586 | default: |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
| 1590 | if (le16_to_cpu(ctrl->wLength) != 2) |
| 1591 | return -EINVAL; |
| 1592 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1593 | ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1594 | if (ret) { |
| 1595 | dev_err(hsotg->dev, "%s: failed to send reply\n", __func__); |
| 1596 | return ret; |
| 1597 | } |
| 1598 | |
| 1599 | return 1; |
| 1600 | } |
| 1601 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1602 | 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] | 1603 | |
| 1604 | /** |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1605 | * get_ep_head - return the first request on the endpoint |
| 1606 | * @hs_ep: The controller endpoint to get |
| 1607 | * |
| 1608 | * Get the first request on the endpoint. |
| 1609 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1610 | 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] | 1611 | { |
Masahiro Yamada | ffc4b40 | 2016-09-19 01:03:13 +0900 | [diff] [blame] | 1612 | return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req, |
| 1613 | queue); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | /** |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1617 | * dwc2_gadget_start_next_request - Starts next request from ep queue |
| 1618 | * @hs_ep: Endpoint structure |
| 1619 | * |
| 1620 | * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked |
| 1621 | * in its handler. Hence we need to unmask it here to be able to do |
| 1622 | * resynchronization. |
| 1623 | */ |
| 1624 | static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep) |
| 1625 | { |
| 1626 | u32 mask; |
| 1627 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 1628 | int dir_in = hs_ep->dir_in; |
| 1629 | struct dwc2_hsotg_req *hs_req; |
| 1630 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 1631 | |
| 1632 | if (!list_empty(&hs_ep->queue)) { |
| 1633 | hs_req = get_ep_head(hs_ep); |
| 1634 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false); |
| 1635 | return; |
| 1636 | } |
| 1637 | if (!hs_ep->isochronous) |
| 1638 | return; |
| 1639 | |
| 1640 | if (dir_in) { |
| 1641 | dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n", |
| 1642 | __func__); |
| 1643 | } else { |
| 1644 | dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n", |
| 1645 | __func__); |
| 1646 | mask = dwc2_readl(hsotg->regs + epmsk_reg); |
| 1647 | mask |= DOEPMSK_OUTTKNEPDISMSK; |
| 1648 | dwc2_writel(mask, hsotg->regs + epmsk_reg); |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1653 | * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1654 | * @hsotg: The device state |
| 1655 | * @ctrl: USB control request |
| 1656 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1657 | static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1658 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1659 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1660 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 1661 | struct dwc2_hsotg_req *hs_req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1662 | bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1663 | struct dwc2_hsotg_ep *ep; |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1664 | int ret; |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1665 | bool halted; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1666 | u32 recip; |
| 1667 | u32 wValue; |
| 1668 | u32 wIndex; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1669 | |
| 1670 | dev_dbg(hsotg->dev, "%s: %s_FEATURE\n", |
| 1671 | __func__, set ? "SET" : "CLEAR"); |
| 1672 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1673 | wValue = le16_to_cpu(ctrl->wValue); |
| 1674 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 1675 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 1676 | |
| 1677 | switch (recip) { |
| 1678 | case USB_RECIP_DEVICE: |
| 1679 | switch (wValue) { |
| 1680 | case USB_DEVICE_TEST_MODE: |
| 1681 | if ((wIndex & 0xff) != 0) |
| 1682 | return -EINVAL; |
| 1683 | if (!set) |
| 1684 | return -EINVAL; |
| 1685 | |
| 1686 | hsotg->test_mode = wIndex >> 8; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1687 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1688 | if (ret) { |
| 1689 | dev_err(hsotg->dev, |
| 1690 | "%s: failed to send reply\n", __func__); |
| 1691 | return ret; |
| 1692 | } |
| 1693 | break; |
| 1694 | default: |
| 1695 | return -ENOENT; |
| 1696 | } |
| 1697 | break; |
| 1698 | |
| 1699 | case USB_RECIP_ENDPOINT: |
| 1700 | ep = ep_from_windex(hsotg, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1701 | if (!ep) { |
| 1702 | dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n", |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1703 | __func__, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1704 | return -ENOENT; |
| 1705 | } |
| 1706 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1707 | switch (wValue) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1708 | case USB_ENDPOINT_HALT: |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1709 | halted = ep->halted; |
| 1710 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1711 | dwc2_hsotg_ep_sethalt(&ep->ep, set, true); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1712 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1713 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1714 | if (ret) { |
| 1715 | dev_err(hsotg->dev, |
| 1716 | "%s: failed to send reply\n", __func__); |
| 1717 | return ret; |
| 1718 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1719 | |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1720 | /* |
| 1721 | * we have to complete all requests for ep if it was |
| 1722 | * halted, and the halt was cleared by CLEAR_FEATURE |
| 1723 | */ |
| 1724 | |
| 1725 | if (!set && halted) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1726 | /* |
| 1727 | * If we have request in progress, |
| 1728 | * then complete it |
| 1729 | */ |
| 1730 | if (ep->req) { |
| 1731 | hs_req = ep->req; |
| 1732 | ep->req = NULL; |
| 1733 | list_del_init(&hs_req->queue); |
Gregory Herrero | c00dd4a | 2015-01-30 09:09:27 +0100 | [diff] [blame] | 1734 | if (hs_req->req.complete) { |
| 1735 | spin_unlock(&hsotg->lock); |
| 1736 | usb_gadget_giveback_request( |
| 1737 | &ep->ep, &hs_req->req); |
| 1738 | spin_lock(&hsotg->lock); |
| 1739 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | /* If we have pending request, then start it */ |
John Youn | 34c0887 | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 1743 | if (!ep->req) |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1744 | dwc2_gadget_start_next_request(ep); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1745 | } |
| 1746 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1747 | break; |
| 1748 | |
| 1749 | default: |
| 1750 | return -ENOENT; |
| 1751 | } |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1752 | break; |
| 1753 | default: |
| 1754 | return -ENOENT; |
| 1755 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1756 | return 1; |
| 1757 | } |
| 1758 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1759 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg); |
Robert Baldyga | ab93e01 | 2013-09-19 11:50:17 +0200 | [diff] [blame] | 1760 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1761 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1762 | * dwc2_hsotg_stall_ep0 - stall ep0 |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1763 | * @hsotg: The device state |
| 1764 | * |
| 1765 | * Set stall for ep0 as response for setup request. |
| 1766 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1767 | static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg) |
Jingoo Han | e9ebe7c | 2014-06-03 22:14:56 +0900 | [diff] [blame] | 1768 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1769 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1770 | u32 reg; |
| 1771 | u32 ctrl; |
| 1772 | |
| 1773 | dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in); |
| 1774 | reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0; |
| 1775 | |
| 1776 | /* |
| 1777 | * DxEPCTL_Stall will be cleared by EP once it has |
| 1778 | * taken effect, so no need to clear later. |
| 1779 | */ |
| 1780 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1781 | ctrl = dwc2_readl(hsotg->regs + reg); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1782 | ctrl |= DXEPCTL_STALL; |
| 1783 | ctrl |= DXEPCTL_CNAK; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1784 | dwc2_writel(ctrl, hsotg->regs + reg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1785 | |
| 1786 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1787 | "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1788 | ctrl, reg, dwc2_readl(hsotg->regs + reg)); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1789 | |
| 1790 | /* |
| 1791 | * complete won't be called, so we enqueue |
| 1792 | * setup request here |
| 1793 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1794 | dwc2_hsotg_enqueue_setup(hsotg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1798 | * dwc2_hsotg_process_control - process a control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1799 | * @hsotg: The device state |
| 1800 | * @ctrl: The control request received |
| 1801 | * |
| 1802 | * The controller has received the SETUP phase of a control request, and |
| 1803 | * needs to work out what to do next (and whether to pass it on to the |
| 1804 | * gadget driver). |
| 1805 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1806 | static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1807 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1808 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1809 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1810 | int ret = 0; |
| 1811 | u32 dcfg; |
| 1812 | |
Mian Yousaf Kaukab | e525e74 | 2015-09-29 12:08:23 +0200 | [diff] [blame] | 1813 | dev_dbg(hsotg->dev, |
| 1814 | "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n", |
| 1815 | ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, |
| 1816 | ctrl->wIndex, ctrl->wLength); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1817 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1818 | if (ctrl->wLength == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1819 | ep0->dir_in = 1; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1820 | hsotg->ep0_state = DWC2_EP0_STATUS_IN; |
| 1821 | } else if (ctrl->bRequestType & USB_DIR_IN) { |
| 1822 | ep0->dir_in = 1; |
| 1823 | hsotg->ep0_state = DWC2_EP0_DATA_IN; |
| 1824 | } else { |
| 1825 | ep0->dir_in = 0; |
| 1826 | hsotg->ep0_state = DWC2_EP0_DATA_OUT; |
| 1827 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1828 | |
| 1829 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { |
| 1830 | switch (ctrl->bRequest) { |
| 1831 | case USB_REQ_SET_ADDRESS: |
Mian Yousaf Kaukab | 6d713c1 | 2015-01-09 13:39:10 +0100 | [diff] [blame] | 1832 | hsotg->connected = 1; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1833 | dcfg = dwc2_readl(hsotg->regs + DCFG); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1834 | dcfg &= ~DCFG_DEVADDR_MASK; |
Paul Zimmerman | d5dbd3f | 2014-04-25 14:18:13 -0700 | [diff] [blame] | 1835 | dcfg |= (le16_to_cpu(ctrl->wValue) << |
| 1836 | DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1837 | dwc2_writel(dcfg, hsotg->regs + DCFG); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1838 | |
| 1839 | dev_info(hsotg->dev, "new address %d\n", ctrl->wValue); |
| 1840 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1841 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1842 | return; |
| 1843 | |
| 1844 | case USB_REQ_GET_STATUS: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1845 | ret = dwc2_hsotg_process_req_status(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1846 | break; |
| 1847 | |
| 1848 | case USB_REQ_CLEAR_FEATURE: |
| 1849 | case USB_REQ_SET_FEATURE: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1850 | ret = dwc2_hsotg_process_req_feature(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1851 | break; |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | /* as a fallback, try delivering it to the driver to deal with */ |
| 1856 | |
| 1857 | if (ret == 0 && hsotg->driver) { |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1858 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1859 | ret = hsotg->driver->setup(&hsotg->gadget, ctrl); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1860 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1861 | if (ret < 0) |
| 1862 | dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret); |
| 1863 | } |
| 1864 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1865 | /* |
| 1866 | * the request is either unhandlable, or is not formatted correctly |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1867 | * so respond with a STALL for the status stage to indicate failure. |
| 1868 | */ |
| 1869 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1870 | if (ret < 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1871 | dwc2_hsotg_stall_ep0(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1872 | } |
| 1873 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1874 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1875 | * dwc2_hsotg_complete_setup - completion of a setup transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1876 | * @ep: The endpoint the request was on. |
| 1877 | * @req: The request completed. |
| 1878 | * |
| 1879 | * Called on completion of any requests the driver itself submitted for |
| 1880 | * EP0 setup packets |
| 1881 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1882 | static void dwc2_hsotg_complete_setup(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1883 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1884 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1885 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1886 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1887 | |
| 1888 | if (req->status < 0) { |
| 1889 | dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status); |
| 1890 | return; |
| 1891 | } |
| 1892 | |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1893 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1894 | if (req->actual == 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1895 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1896 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1897 | dwc2_hsotg_process_control(hsotg, req->buf); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1898 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1902 | * dwc2_hsotg_enqueue_setup - start a request for EP0 packets |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1903 | * @hsotg: The device state. |
| 1904 | * |
| 1905 | * Enqueue a request on EP0 if necessary to received any SETUP packets |
| 1906 | * received from the host. |
| 1907 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1908 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1909 | { |
| 1910 | struct usb_request *req = hsotg->ctrl_req; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1911 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1912 | int ret; |
| 1913 | |
| 1914 | dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__); |
| 1915 | |
| 1916 | req->zero = 0; |
| 1917 | req->length = 8; |
| 1918 | req->buf = hsotg->ctrl_buff; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1919 | req->complete = dwc2_hsotg_complete_setup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1920 | |
| 1921 | if (!list_empty(&hs_req->queue)) { |
| 1922 | dev_dbg(hsotg->dev, "%s already queued???\n", __func__); |
| 1923 | return; |
| 1924 | } |
| 1925 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1926 | hsotg->eps_out[0]->dir_in = 0; |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 1927 | hsotg->eps_out[0]->send_zlp = 0; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1928 | hsotg->ep0_state = DWC2_EP0_SETUP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1929 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1930 | 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] | 1931 | if (ret < 0) { |
| 1932 | dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret); |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1933 | /* |
| 1934 | * Don't think there's much we can do other than watch the |
| 1935 | * driver fail. |
| 1936 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1940 | static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1941 | struct dwc2_hsotg_ep *hs_ep) |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1942 | { |
| 1943 | u32 ctrl; |
| 1944 | u8 index = hs_ep->index; |
| 1945 | u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 1946 | u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
| 1947 | |
Mian Yousaf Kaukab | ccb34a9 | 2015-01-30 09:09:34 +0100 | [diff] [blame] | 1948 | if (hs_ep->dir_in) |
| 1949 | dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1950 | index); |
Mian Yousaf Kaukab | ccb34a9 | 2015-01-30 09:09:34 +0100 | [diff] [blame] | 1951 | else |
| 1952 | dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n", |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1953 | index); |
| 1954 | if (using_desc_dma(hsotg)) { |
| 1955 | /* Not specific buffer needed for ep0 ZLP */ |
| 1956 | dma_addr_t dma = hs_ep->desc_list_dma; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1957 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1958 | dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep); |
| 1959 | dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0); |
| 1960 | } else { |
| 1961 | dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
| 1962 | DXEPTSIZ_XFERSIZE(0), hsotg->regs + |
| 1963 | epsiz_reg); |
| 1964 | } |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1965 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1966 | ctrl = dwc2_readl(hsotg->regs + epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1967 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
| 1968 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
| 1969 | ctrl |= DXEPCTL_USBACTEP; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 1970 | dwc2_writel(ctrl, hsotg->regs + epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1971 | } |
| 1972 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1973 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1974 | * dwc2_hsotg_complete_request - complete a request given to us |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1975 | * @hsotg: The device state. |
| 1976 | * @hs_ep: The endpoint the request was on. |
| 1977 | * @hs_req: The request to complete. |
| 1978 | * @result: The result code (0 => Ok, otherwise errno) |
| 1979 | * |
| 1980 | * The given request has finished, so call the necessary completion |
| 1981 | * if it has one and then look to see if we can start a new request |
| 1982 | * on the endpoint. |
| 1983 | * |
| 1984 | * Note, expects the ep to already be locked as appropriate. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1985 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1986 | static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1987 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1988 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1989 | int result) |
| 1990 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1991 | if (!hs_req) { |
| 1992 | dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__); |
| 1993 | return; |
| 1994 | } |
| 1995 | |
| 1996 | dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n", |
| 1997 | hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete); |
| 1998 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1999 | /* |
| 2000 | * only replace the status if we've not already set an error |
| 2001 | * from a previous transaction |
| 2002 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2003 | |
| 2004 | if (hs_req->req.status == -EINPROGRESS) |
| 2005 | hs_req->req.status = result; |
| 2006 | |
Yunzhi Li | 44583fe | 2015-09-29 12:25:01 +0200 | [diff] [blame] | 2007 | if (using_dma(hsotg)) |
| 2008 | dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req); |
| 2009 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2010 | dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 2011 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2012 | hs_ep->req = NULL; |
| 2013 | list_del_init(&hs_req->queue); |
| 2014 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2015 | /* |
| 2016 | * call the complete request with the locks off, just in case the |
| 2017 | * request tries to queue more work for this endpoint. |
| 2018 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2019 | |
| 2020 | if (hs_req->req.complete) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2021 | spin_unlock(&hsotg->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 2022 | usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2023 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2026 | /* In DDMA don't need to proceed to starting of next ISOC request */ |
| 2027 | if (using_desc_dma(hsotg) && hs_ep->isochronous) |
| 2028 | return; |
| 2029 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2030 | /* |
| 2031 | * 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] | 2032 | * 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] | 2033 | * so be careful when doing this. |
| 2034 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2035 | |
John Youn | 34c0887 | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 2036 | if (!hs_ep->req && result >= 0) |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 2037 | dwc2_gadget_start_next_request(hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2040 | /* |
| 2041 | * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA |
| 2042 | * @hs_ep: The endpoint the request was on. |
| 2043 | * |
| 2044 | * Get first request from the ep queue, determine descriptor on which complete |
| 2045 | * happened. SW based on isoc_chain_num discovers which half of the descriptor |
| 2046 | * chain is currently in use by HW, adjusts dma_address and calculates index |
| 2047 | * of completed descriptor based on the value of DEPDMA register. Update actual |
| 2048 | * length of request, giveback to gadget. |
| 2049 | */ |
| 2050 | static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 2051 | { |
| 2052 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2053 | struct dwc2_hsotg_req *hs_req; |
| 2054 | struct usb_request *ureq; |
| 2055 | int index; |
| 2056 | dma_addr_t dma_addr; |
| 2057 | u32 dma_reg; |
| 2058 | u32 depdma; |
| 2059 | u32 desc_sts; |
| 2060 | u32 mask; |
| 2061 | |
| 2062 | hs_req = get_ep_head(hs_ep); |
| 2063 | if (!hs_req) { |
| 2064 | dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__); |
| 2065 | return; |
| 2066 | } |
| 2067 | ureq = &hs_req->req; |
| 2068 | |
| 2069 | dma_addr = hs_ep->desc_list_dma; |
| 2070 | |
| 2071 | /* |
| 2072 | * If lower half of descriptor chain is currently use by SW, |
| 2073 | * that means higher half is being processed by HW, so shift |
| 2074 | * DMA address to higher half of descriptor chain. |
| 2075 | */ |
| 2076 | if (!hs_ep->isoc_chain_num) |
| 2077 | dma_addr += sizeof(struct dwc2_dma_desc) * |
| 2078 | (MAX_DMA_DESC_NUM_GENERIC / 2); |
| 2079 | |
| 2080 | dma_reg = hs_ep->dir_in ? DIEPDMA(hs_ep->index) : DOEPDMA(hs_ep->index); |
| 2081 | depdma = dwc2_readl(hsotg->regs + dma_reg); |
| 2082 | |
| 2083 | index = (depdma - dma_addr) / sizeof(struct dwc2_dma_desc) - 1; |
| 2084 | desc_sts = hs_ep->desc_list[index].status; |
| 2085 | |
| 2086 | mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK : |
| 2087 | DEV_DMA_ISOC_RX_NBYTES_MASK; |
| 2088 | ureq->actual = ureq->length - |
| 2089 | ((desc_sts & mask) >> DEV_DMA_ISOC_NBYTES_SHIFT); |
| 2090 | |
Vahram Aharonyan | 95d2b03 | 2016-11-14 19:16:46 -0800 | [diff] [blame] | 2091 | /* Adjust actual length for ISOC Out if length is not align of 4 */ |
| 2092 | if (!hs_ep->dir_in && ureq->length & 0x3) |
| 2093 | ureq->actual += 4 - (ureq->length & 0x3); |
| 2094 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2095 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
| 2096 | } |
| 2097 | |
| 2098 | /* |
| 2099 | * dwc2_gadget_start_next_isoc_ddma - start next isoc request, if any. |
| 2100 | * @hs_ep: The isochronous endpoint to be re-enabled. |
| 2101 | * |
| 2102 | * If ep has been disabled due to last descriptor servicing (IN endpoint) or |
| 2103 | * BNA (OUT endpoint) check the status of other half of descriptor chain that |
| 2104 | * was under SW control till HW was busy and restart the endpoint if needed. |
| 2105 | */ |
| 2106 | static void dwc2_gadget_start_next_isoc_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 2107 | { |
| 2108 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2109 | u32 depctl; |
| 2110 | u32 dma_reg; |
| 2111 | u32 ctrl; |
| 2112 | u32 dma_addr = hs_ep->desc_list_dma; |
| 2113 | unsigned char index = hs_ep->index; |
| 2114 | |
| 2115 | dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index); |
| 2116 | depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 2117 | |
| 2118 | ctrl = dwc2_readl(hsotg->regs + depctl); |
| 2119 | |
| 2120 | /* |
| 2121 | * EP was disabled if HW has processed last descriptor or BNA was set. |
| 2122 | * So restart ep if SW has prepared new descriptor chain in ep_queue |
| 2123 | * routine while HW was busy. |
| 2124 | */ |
| 2125 | if (!(ctrl & DXEPCTL_EPENA)) { |
| 2126 | if (!hs_ep->next_desc) { |
| 2127 | dev_dbg(hsotg->dev, "%s: No more ISOC requests\n", |
| 2128 | __func__); |
| 2129 | return; |
| 2130 | } |
| 2131 | |
| 2132 | dma_addr += sizeof(struct dwc2_dma_desc) * |
| 2133 | (MAX_DMA_DESC_NUM_GENERIC / 2) * |
| 2134 | hs_ep->isoc_chain_num; |
| 2135 | dwc2_writel(dma_addr, hsotg->regs + dma_reg); |
| 2136 | |
| 2137 | ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK; |
| 2138 | dwc2_writel(ctrl, hsotg->regs + depctl); |
| 2139 | |
| 2140 | /* Switch ISOC descriptor chain number being processed by SW*/ |
| 2141 | hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1; |
| 2142 | hs_ep->next_desc = 0; |
| 2143 | |
| 2144 | dev_dbg(hsotg->dev, "%s: Restarted isochronous endpoint\n", |
| 2145 | __func__); |
| 2146 | } |
| 2147 | } |
| 2148 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2149 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2150 | * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2151 | * @hsotg: The device state. |
| 2152 | * @ep_idx: The endpoint index for the data |
| 2153 | * @size: The size of data in the fifo, in bytes |
| 2154 | * |
| 2155 | * The FIFO status shows there is data to read from the FIFO for a given |
| 2156 | * endpoint, so sort out whether we need to read the data into a request |
| 2157 | * that has been made for that endpoint. |
| 2158 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2159 | 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] | 2160 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2161 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx]; |
| 2162 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2163 | void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2164 | int to_read; |
| 2165 | int max_req; |
| 2166 | int read_ptr; |
| 2167 | |
| 2168 | if (!hs_req) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2169 | u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2170 | int ptr; |
| 2171 | |
Robert Baldyga | 6b448af | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 2172 | dev_dbg(hsotg->dev, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2173 | "%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] | 2174 | __func__, size, ep_idx, epctl); |
| 2175 | |
| 2176 | /* dump the data from the FIFO, we've nothing we can do */ |
| 2177 | for (ptr = 0; ptr < size; ptr += 4) |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2178 | (void)dwc2_readl(fifo); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2179 | |
| 2180 | return; |
| 2181 | } |
| 2182 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2183 | to_read = size; |
| 2184 | read_ptr = hs_req->req.actual; |
| 2185 | max_req = hs_req->req.length - read_ptr; |
| 2186 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2187 | dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n", |
| 2188 | __func__, to_read, max_req, read_ptr, hs_req->req.length); |
| 2189 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2190 | if (to_read > max_req) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2191 | /* |
| 2192 | * more data appeared than we where willing |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2193 | * to deal with in this request. |
| 2194 | */ |
| 2195 | |
| 2196 | /* currently we don't deal this */ |
| 2197 | WARN_ON_ONCE(1); |
| 2198 | } |
| 2199 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2200 | hs_ep->total_data += to_read; |
| 2201 | hs_req->req.actual += to_read; |
| 2202 | to_read = DIV_ROUND_UP(to_read, 4); |
| 2203 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2204 | /* |
| 2205 | * note, we might over-write the buffer end by 3 bytes depending on |
| 2206 | * alignment of the data. |
| 2207 | */ |
Matt Porter | 1a7ed5b | 2014-02-03 10:29:09 -0500 | [diff] [blame] | 2208 | ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2212 | * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2213 | * @hsotg: The device instance |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2214 | * @dir_in: If IN zlp |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2215 | * |
| 2216 | * Generate a zero-length IN packet request for terminating a SETUP |
| 2217 | * transaction. |
| 2218 | * |
| 2219 | * 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] | 2220 | * 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] | 2221 | * the TxFIFO. |
| 2222 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2223 | 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] | 2224 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2225 | /* eps_out[0] is used in both directions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2226 | hsotg->eps_out[0]->dir_in = dir_in; |
| 2227 | 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] | 2228 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2229 | dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2230 | } |
| 2231 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2232 | static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2233 | u32 epctl_reg) |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2234 | { |
| 2235 | u32 ctrl; |
| 2236 | |
| 2237 | ctrl = dwc2_readl(hsotg->regs + epctl_reg); |
| 2238 | if (ctrl & DXEPCTL_EOFRNUM) |
| 2239 | ctrl |= DXEPCTL_SETEVENFR; |
| 2240 | else |
| 2241 | ctrl |= DXEPCTL_SETODDFR; |
| 2242 | dwc2_writel(ctrl, hsotg->regs + epctl_reg); |
| 2243 | } |
| 2244 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2245 | /* |
| 2246 | * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc |
| 2247 | * @hs_ep - The endpoint on which transfer went |
| 2248 | * |
| 2249 | * Iterate over endpoints descriptor chain and get info on bytes remained |
| 2250 | * in DMA descriptors after transfer has completed. Used for non isoc EPs. |
| 2251 | */ |
| 2252 | static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 2253 | { |
| 2254 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2255 | unsigned int bytes_rem = 0; |
| 2256 | struct dwc2_dma_desc *desc = hs_ep->desc_list; |
| 2257 | int i; |
| 2258 | u32 status; |
| 2259 | |
| 2260 | if (!desc) |
| 2261 | return -EINVAL; |
| 2262 | |
| 2263 | for (i = 0; i < hs_ep->desc_count; ++i) { |
| 2264 | status = desc->status; |
| 2265 | bytes_rem += status & DEV_DMA_NBYTES_MASK; |
| 2266 | |
| 2267 | if (status & DEV_DMA_STS_MASK) |
| 2268 | dev_err(hsotg->dev, "descriptor %d closed with %x\n", |
| 2269 | i, status & DEV_DMA_STS_MASK); |
| 2270 | } |
| 2271 | |
| 2272 | return bytes_rem; |
| 2273 | } |
| 2274 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2275 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2276 | * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2277 | * @hsotg: The device instance |
| 2278 | * @epnum: The endpoint received from |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2279 | * |
| 2280 | * The RXFIFO has delivered an OutDone event, which means that the data |
| 2281 | * transfer for an OUT endpoint has been completed, either by a short |
| 2282 | * packet or by the finish of a transfer. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2283 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2284 | static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2285 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2286 | u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum)); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2287 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum]; |
| 2288 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2289 | struct usb_request *req = &hs_req->req; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2290 | unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2291 | int result = 0; |
| 2292 | |
| 2293 | if (!hs_req) { |
| 2294 | dev_dbg(hsotg->dev, "%s: no request active\n", __func__); |
| 2295 | return; |
| 2296 | } |
| 2297 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2298 | if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) { |
| 2299 | dev_dbg(hsotg->dev, "zlp packet received\n"); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2300 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
| 2301 | dwc2_hsotg_enqueue_setup(hsotg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2302 | return; |
| 2303 | } |
| 2304 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2305 | if (using_desc_dma(hsotg)) |
| 2306 | size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); |
| 2307 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2308 | if (using_dma(hsotg)) { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2309 | unsigned int size_done; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2310 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2311 | /* |
| 2312 | * Calculate the size of the transfer by checking how much |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2313 | * is left in the endpoint size register and then working it |
| 2314 | * out from the amount we loaded for the transfer. |
| 2315 | * |
| 2316 | * We need to do this as DMA pointers are always 32bit aligned |
| 2317 | * so may overshoot/undershoot the transfer. |
| 2318 | */ |
| 2319 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2320 | size_done = hs_ep->size_loaded - size_left; |
| 2321 | size_done += hs_ep->last_load; |
| 2322 | |
| 2323 | req->actual = size_done; |
| 2324 | } |
| 2325 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2326 | /* if there is more request to do, schedule new transfer */ |
| 2327 | if (req->actual < req->length && size_left == 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2328 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2329 | return; |
| 2330 | } |
| 2331 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2332 | if (req->actual < req->length && req->short_not_ok) { |
| 2333 | dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n", |
| 2334 | __func__, req->actual, req->length); |
| 2335 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2336 | /* |
| 2337 | * todo - what should we return here? there's no one else |
| 2338 | * even bothering to check the status. |
| 2339 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2340 | } |
| 2341 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 2342 | /* DDMA IN status phase will start from StsPhseRcvd interrupt */ |
| 2343 | if (!using_desc_dma(hsotg) && epnum == 0 && |
| 2344 | hsotg->ep0_state == DWC2_EP0_DATA_OUT) { |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2345 | /* Move to STATUS IN */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2346 | dwc2_hsotg_ep0_zlp(hsotg, true); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2347 | return; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2348 | } |
| 2349 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2350 | /* |
| 2351 | * Slave mode OUT transfers do not go through XferComplete so |
| 2352 | * adjust the ISOC parity here. |
| 2353 | */ |
| 2354 | if (!using_dma(hsotg)) { |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2355 | if (hs_ep->isochronous && hs_ep->interval == 1) |
| 2356 | dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2357 | else if (hs_ep->isochronous && hs_ep->interval > 1) |
| 2358 | dwc2_gadget_incr_frame_num(hs_ep); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2361 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2365 | * dwc2_hsotg_handle_rx - RX FIFO has data |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2366 | * @hsotg: The device instance |
| 2367 | * |
| 2368 | * The IRQ handler has detected that the RX FIFO has some data in it |
| 2369 | * that requires processing, so find out what is in there and do the |
| 2370 | * appropriate read. |
| 2371 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2372 | * 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] | 2373 | * chunks, so if you have x packets received on an endpoint you'll get x |
| 2374 | * FIFO events delivered, each with a packet's worth of data in it. |
| 2375 | * |
| 2376 | * When using DMA, we should not be processing events from the RXFIFO |
| 2377 | * as the actual data should be sent to the memory directly and we turn |
| 2378 | * on the completion interrupts to get notifications of transfer completion. |
| 2379 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2380 | static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2381 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2382 | u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2383 | u32 epnum, status, size; |
| 2384 | |
| 2385 | WARN_ON(using_dma(hsotg)); |
| 2386 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2387 | epnum = grxstsr & GRXSTS_EPNUM_MASK; |
| 2388 | status = grxstsr & GRXSTS_PKTSTS_MASK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2389 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2390 | size = grxstsr & GRXSTS_BYTECNT_MASK; |
| 2391 | size >>= GRXSTS_BYTECNT_SHIFT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2392 | |
Mian Yousaf Kaukab | d7c747c | 2015-01-30 09:09:30 +0100 | [diff] [blame] | 2393 | dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2394 | __func__, grxstsr, size, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2395 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2396 | switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) { |
| 2397 | case GRXSTS_PKTSTS_GLOBALOUTNAK: |
| 2398 | dev_dbg(hsotg->dev, "GLOBALOUTNAK\n"); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2399 | break; |
| 2400 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2401 | case GRXSTS_PKTSTS_OUTDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2402 | dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2403 | dwc2_hsotg_read_frameno(hsotg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2404 | |
| 2405 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2406 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2407 | break; |
| 2408 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2409 | case GRXSTS_PKTSTS_SETUPDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2410 | dev_dbg(hsotg->dev, |
| 2411 | "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2412 | dwc2_hsotg_read_frameno(hsotg), |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2413 | dwc2_readl(hsotg->regs + DOEPCTL(0))); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2414 | /* |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2415 | * 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] | 2416 | * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't |
| 2417 | * generate GRXSTS_PKTSTS_OUTDONE for setup packet. |
| 2418 | */ |
| 2419 | if (hsotg->ep0_state == DWC2_EP0_SETUP) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2420 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2421 | break; |
| 2422 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2423 | case GRXSTS_PKTSTS_OUTRX: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2424 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2425 | break; |
| 2426 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2427 | case GRXSTS_PKTSTS_SETUPRX: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2428 | dev_dbg(hsotg->dev, |
| 2429 | "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2430 | dwc2_hsotg_read_frameno(hsotg), |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2431 | dwc2_readl(hsotg->regs + DOEPCTL(0))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2432 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2433 | WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP); |
| 2434 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2435 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2436 | break; |
| 2437 | |
| 2438 | default: |
| 2439 | dev_warn(hsotg->dev, "%s: unknown status %08x\n", |
| 2440 | __func__, grxstsr); |
| 2441 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2442 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2443 | break; |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2448 | * dwc2_hsotg_ep0_mps - turn max packet size into register setting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2449 | * @mps: The maximum packet size in bytes. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2450 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2451 | static u32 dwc2_hsotg_ep0_mps(unsigned int mps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2452 | { |
| 2453 | switch (mps) { |
| 2454 | case 64: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2455 | return D0EPCTL_MPS_64; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2456 | case 32: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2457 | return D0EPCTL_MPS_32; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2458 | case 16: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2459 | return D0EPCTL_MPS_16; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2460 | case 8: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2461 | return D0EPCTL_MPS_8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | /* bad max packet size, warn and return invalid result */ |
| 2465 | WARN_ON(1); |
| 2466 | return (u32)-1; |
| 2467 | } |
| 2468 | |
| 2469 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2470 | * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2471 | * @hsotg: The driver state. |
| 2472 | * @ep: The index number of the endpoint |
| 2473 | * @mps: The maximum packet size in bytes |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2474 | * @mc: The multicount value |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2475 | * |
| 2476 | * Configure the maximum packet size for the given endpoint, updating |
| 2477 | * the hardware control registers to reflect this. |
| 2478 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2479 | static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg, |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2480 | unsigned int ep, unsigned int mps, |
| 2481 | unsigned int mc, unsigned int dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2482 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2483 | struct dwc2_hsotg_ep *hs_ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2484 | void __iomem *regs = hsotg->regs; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2485 | u32 reg; |
| 2486 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2487 | hs_ep = index_to_ep(hsotg, ep, dir_in); |
| 2488 | if (!hs_ep) |
| 2489 | return; |
| 2490 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2491 | if (ep == 0) { |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2492 | u32 mps_bytes = mps; |
| 2493 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2494 | /* EP0 is a special case */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2495 | mps = dwc2_hsotg_ep0_mps(mps_bytes); |
| 2496 | if (mps > 3) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2497 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2498 | hs_ep->ep.maxpacket = mps_bytes; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 2499 | hs_ep->mc = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2500 | } else { |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2501 | if (mps > 1024) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2502 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2503 | hs_ep->mc = mc; |
| 2504 | if (mc > 3) |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 2505 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2506 | hs_ep->ep.maxpacket = mps; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2507 | } |
| 2508 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2509 | if (dir_in) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2510 | reg = dwc2_readl(regs + DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2511 | reg &= ~DXEPCTL_MPS_MASK; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2512 | reg |= mps; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2513 | dwc2_writel(reg, regs + DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2514 | } else { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2515 | reg = dwc2_readl(regs + DOEPCTL(ep)); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2516 | reg &= ~DXEPCTL_MPS_MASK; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2517 | reg |= mps; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2518 | dwc2_writel(reg, regs + DOEPCTL(ep)); |
Anton Tikhomirov | 659ad60 | 2012-03-06 14:07:29 +0900 | [diff] [blame] | 2519 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2520 | |
| 2521 | return; |
| 2522 | |
| 2523 | bad_mps: |
| 2524 | dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps); |
| 2525 | } |
| 2526 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2527 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2528 | * dwc2_hsotg_txfifo_flush - flush Tx FIFO |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2529 | * @hsotg: The driver state |
| 2530 | * @idx: The index for the endpoint (0..15) |
| 2531 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2532 | 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] | 2533 | { |
| 2534 | int timeout; |
| 2535 | int val; |
| 2536 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2537 | dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH, |
| 2538 | hsotg->regs + GRSTCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2539 | |
| 2540 | /* wait until the fifo is flushed */ |
| 2541 | timeout = 100; |
| 2542 | |
| 2543 | while (1) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2544 | val = dwc2_readl(hsotg->regs + GRSTCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2545 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2546 | if ((val & (GRSTCTL_TXFFLSH)) == 0) |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2547 | break; |
| 2548 | |
| 2549 | if (--timeout == 0) { |
| 2550 | dev_err(hsotg->dev, |
| 2551 | "%s: timeout flushing fifo (GRSTCTL=%08x)\n", |
| 2552 | __func__, val); |
Marek Szyprowski | e0cbe59 | 2014-09-09 10:44:10 +0200 | [diff] [blame] | 2553 | break; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | udelay(1); |
| 2557 | } |
| 2558 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2559 | |
| 2560 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2561 | * dwc2_hsotg_trytx - check to see if anything needs transmitting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2562 | * @hsotg: The driver state |
| 2563 | * @hs_ep: The driver endpoint to check. |
| 2564 | * |
| 2565 | * Check to see if there is a request that has data to send, and if so |
| 2566 | * make an attempt to write data into the FIFO. |
| 2567 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2568 | static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2569 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2570 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2571 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2572 | |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 2573 | if (!hs_ep->dir_in || !hs_req) { |
| 2574 | /** |
| 2575 | * if request is not enqueued, we disable interrupts |
| 2576 | * for endpoints, excepting ep0 |
| 2577 | */ |
| 2578 | if (hs_ep->index != 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2579 | dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2580 | hs_ep->dir_in, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2581 | return 0; |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 2582 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2583 | |
| 2584 | if (hs_req->req.actual < hs_req->req.length) { |
| 2585 | dev_dbg(hsotg->dev, "trying to write more for ep%d\n", |
| 2586 | hs_ep->index); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2587 | return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | return 0; |
| 2591 | } |
| 2592 | |
| 2593 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2594 | * dwc2_hsotg_complete_in - complete IN transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2595 | * @hsotg: The device state. |
| 2596 | * @hs_ep: The endpoint that has just completed. |
| 2597 | * |
| 2598 | * An IN transfer has been completed, update the transfer's state and then |
| 2599 | * call the relevant completion routines. |
| 2600 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2601 | static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2602 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2603 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2604 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2605 | u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2606 | int size_left, size_done; |
| 2607 | |
| 2608 | if (!hs_req) { |
| 2609 | dev_dbg(hsotg->dev, "XferCompl but no req\n"); |
| 2610 | return; |
| 2611 | } |
| 2612 | |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2613 | /* Finish ZLP handling for IN EP0 transactions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2614 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) { |
| 2615 | dev_dbg(hsotg->dev, "zlp packet sent\n"); |
Razmik Karapetyan | c3b22fe | 2016-11-16 15:33:57 -0800 | [diff] [blame] | 2616 | |
| 2617 | /* |
| 2618 | * While send zlp for DWC2_EP0_STATUS_IN EP direction was |
| 2619 | * changed to IN. Change back to complete OUT transfer request |
| 2620 | */ |
| 2621 | hs_ep->dir_in = 0; |
| 2622 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2623 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2624 | if (hsotg->test_mode) { |
| 2625 | int ret; |
| 2626 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2627 | ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2628 | if (ret < 0) { |
| 2629 | dev_dbg(hsotg->dev, "Invalid Test #%d\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2630 | hsotg->test_mode); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2631 | dwc2_hsotg_stall_ep0(hsotg); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2632 | return; |
| 2633 | } |
| 2634 | } |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2635 | dwc2_hsotg_enqueue_setup(hsotg); |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2636 | return; |
| 2637 | } |
| 2638 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2639 | /* |
| 2640 | * Calculate the size of the transfer by checking how much is left |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2641 | * in the endpoint size register and then working it out from |
| 2642 | * the amount we loaded for the transfer. |
| 2643 | * |
| 2644 | * We do this even for DMA, as the transfer may have incremented |
| 2645 | * past the end of the buffer (DMA transfers are always 32bit |
| 2646 | * aligned). |
| 2647 | */ |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2648 | if (using_desc_dma(hsotg)) { |
| 2649 | size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); |
| 2650 | if (size_left < 0) |
| 2651 | dev_err(hsotg->dev, "error parsing DDMA results %d\n", |
| 2652 | size_left); |
| 2653 | } else { |
| 2654 | size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
| 2655 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2656 | |
| 2657 | size_done = hs_ep->size_loaded - size_left; |
| 2658 | size_done += hs_ep->last_load; |
| 2659 | |
| 2660 | if (hs_req->req.actual != size_done) |
| 2661 | dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n", |
| 2662 | __func__, hs_req->req.actual, size_done); |
| 2663 | |
| 2664 | hs_req->req.actual = size_done; |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2665 | dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n", |
| 2666 | hs_req->req.length, hs_req->req.actual, hs_req->req.zero); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2667 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2668 | if (!size_left && hs_req->req.actual < hs_req->req.length) { |
| 2669 | dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2670 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2671 | return; |
| 2672 | } |
| 2673 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 2674 | /* Zlp for all endpoints, for ep0 only in DATA IN stage */ |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2675 | if (hs_ep->send_zlp) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2676 | dwc2_hsotg_program_zlp(hsotg, hs_ep); |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2677 | hs_ep->send_zlp = 0; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 2678 | /* transfer will be completed on next complete interrupt */ |
| 2679 | return; |
| 2680 | } |
| 2681 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2682 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) { |
| 2683 | /* Move to STATUS OUT */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2684 | dwc2_hsotg_ep0_zlp(hsotg, false); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2685 | return; |
| 2686 | } |
| 2687 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2688 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | /** |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2692 | * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep |
| 2693 | * @hsotg: The device state. |
| 2694 | * @idx: Index of ep. |
| 2695 | * @dir_in: Endpoint direction 1-in 0-out. |
| 2696 | * |
| 2697 | * Reads for endpoint with given index and direction, by masking |
| 2698 | * epint_reg with coresponding mask. |
| 2699 | */ |
| 2700 | static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg, |
| 2701 | unsigned int idx, int dir_in) |
| 2702 | { |
| 2703 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 2704 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 2705 | u32 ints; |
| 2706 | u32 mask; |
| 2707 | u32 diepempmsk; |
| 2708 | |
| 2709 | mask = dwc2_readl(hsotg->regs + epmsk_reg); |
| 2710 | diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK); |
| 2711 | mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0; |
| 2712 | mask |= DXEPINT_SETUP_RCVD; |
| 2713 | |
| 2714 | ints = dwc2_readl(hsotg->regs + epint_reg); |
| 2715 | ints &= mask; |
| 2716 | return ints; |
| 2717 | } |
| 2718 | |
| 2719 | /** |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2720 | * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD |
| 2721 | * @hs_ep: The endpoint on which interrupt is asserted. |
| 2722 | * |
| 2723 | * This interrupt indicates that the endpoint has been disabled per the |
| 2724 | * application's request. |
| 2725 | * |
| 2726 | * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK, |
| 2727 | * in case of ISOC completes current request. |
| 2728 | * |
| 2729 | * For ISOC-OUT endpoints completes expired requests. If there is remaining |
| 2730 | * request starts it. |
| 2731 | */ |
| 2732 | static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep) |
| 2733 | { |
| 2734 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2735 | struct dwc2_hsotg_req *hs_req; |
| 2736 | unsigned char idx = hs_ep->index; |
| 2737 | int dir_in = hs_ep->dir_in; |
| 2738 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
| 2739 | int dctl = dwc2_readl(hsotg->regs + DCTL); |
| 2740 | |
| 2741 | dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__); |
| 2742 | |
| 2743 | if (dir_in) { |
| 2744 | int epctl = dwc2_readl(hsotg->regs + epctl_reg); |
| 2745 | |
| 2746 | dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index); |
| 2747 | |
| 2748 | if (hs_ep->isochronous) { |
| 2749 | dwc2_hsotg_complete_in(hsotg, hs_ep); |
| 2750 | return; |
| 2751 | } |
| 2752 | |
| 2753 | if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) { |
| 2754 | int dctl = dwc2_readl(hsotg->regs + DCTL); |
| 2755 | |
| 2756 | dctl |= DCTL_CGNPINNAK; |
| 2757 | dwc2_writel(dctl, hsotg->regs + DCTL); |
| 2758 | } |
| 2759 | return; |
| 2760 | } |
| 2761 | |
| 2762 | if (dctl & DCTL_GOUTNAKSTS) { |
| 2763 | dctl |= DCTL_CGOUTNAK; |
| 2764 | dwc2_writel(dctl, hsotg->regs + DCTL); |
| 2765 | } |
| 2766 | |
| 2767 | if (!hs_ep->isochronous) |
| 2768 | return; |
| 2769 | |
| 2770 | if (list_empty(&hs_ep->queue)) { |
| 2771 | dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n", |
| 2772 | __func__, hs_ep); |
| 2773 | return; |
| 2774 | } |
| 2775 | |
| 2776 | do { |
| 2777 | hs_req = get_ep_head(hs_ep); |
| 2778 | if (hs_req) |
| 2779 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, |
| 2780 | -ENODATA); |
| 2781 | dwc2_gadget_incr_frame_num(hs_ep); |
| 2782 | } while (dwc2_gadget_target_frame_elapsed(hs_ep)); |
| 2783 | |
| 2784 | dwc2_gadget_start_next_request(hs_ep); |
| 2785 | } |
| 2786 | |
| 2787 | /** |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2788 | * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS |
| 2789 | * @hs_ep: The endpoint on which interrupt is asserted. |
| 2790 | * |
| 2791 | * This is starting point for ISOC-OUT transfer, synchronization done with |
| 2792 | * first out token received from host while corresponding EP is disabled. |
| 2793 | * |
| 2794 | * Device does not know initial frame in which out token will come. For this |
| 2795 | * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon |
| 2796 | * getting this interrupt SW starts calculation for next transfer frame. |
| 2797 | */ |
| 2798 | static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep) |
| 2799 | { |
| 2800 | struct dwc2_hsotg *hsotg = ep->parent; |
| 2801 | int dir_in = ep->dir_in; |
| 2802 | u32 doepmsk; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2803 | u32 tmp; |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2804 | |
| 2805 | if (dir_in || !ep->isochronous) |
| 2806 | return; |
| 2807 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2808 | /* |
| 2809 | * Store frame in which irq was asserted here, as |
| 2810 | * it can change while completing request below. |
| 2811 | */ |
| 2812 | tmp = dwc2_hsotg_read_frameno(hsotg); |
| 2813 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2814 | dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA); |
| 2815 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2816 | if (using_desc_dma(hsotg)) { |
| 2817 | if (ep->target_frame == TARGET_FRAME_INITIAL) { |
| 2818 | /* Start first ISO Out */ |
| 2819 | ep->target_frame = tmp; |
| 2820 | dwc2_gadget_start_isoc_ddma(ep); |
| 2821 | } |
| 2822 | return; |
| 2823 | } |
| 2824 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2825 | if (ep->interval > 1 && |
| 2826 | ep->target_frame == TARGET_FRAME_INITIAL) { |
| 2827 | u32 dsts; |
| 2828 | u32 ctrl; |
| 2829 | |
| 2830 | dsts = dwc2_readl(hsotg->regs + DSTS); |
| 2831 | ep->target_frame = dwc2_hsotg_read_frameno(hsotg); |
| 2832 | dwc2_gadget_incr_frame_num(ep); |
| 2833 | |
| 2834 | ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index)); |
| 2835 | if (ep->target_frame & 0x1) |
| 2836 | ctrl |= DXEPCTL_SETODDFR; |
| 2837 | else |
| 2838 | ctrl |= DXEPCTL_SETEVENFR; |
| 2839 | |
| 2840 | dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index)); |
| 2841 | } |
| 2842 | |
| 2843 | dwc2_gadget_start_next_request(ep); |
| 2844 | doepmsk = dwc2_readl(hsotg->regs + DOEPMSK); |
| 2845 | doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK; |
| 2846 | dwc2_writel(doepmsk, hsotg->regs + DOEPMSK); |
| 2847 | } |
| 2848 | |
| 2849 | /** |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 2850 | * dwc2_gadget_handle_nak - handle NAK interrupt |
| 2851 | * @hs_ep: The endpoint on which interrupt is asserted. |
| 2852 | * |
| 2853 | * This is starting point for ISOC-IN transfer, synchronization done with |
| 2854 | * first IN token received from host while corresponding EP is disabled. |
| 2855 | * |
| 2856 | * Device does not know when first one token will arrive from host. On first |
| 2857 | * token arrival HW generates 2 interrupts: 'in token received while FIFO empty' |
| 2858 | * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was |
| 2859 | * sent in response to that as there was no data in FIFO. SW is basing on this |
| 2860 | * interrupt to obtain frame in which token has come and then based on the |
| 2861 | * interval calculates next frame for transfer. |
| 2862 | */ |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2863 | static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep) |
| 2864 | { |
| 2865 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2866 | int dir_in = hs_ep->dir_in; |
| 2867 | |
| 2868 | if (!dir_in || !hs_ep->isochronous) |
| 2869 | return; |
| 2870 | |
| 2871 | if (hs_ep->target_frame == TARGET_FRAME_INITIAL) { |
| 2872 | hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2873 | |
| 2874 | if (using_desc_dma(hsotg)) { |
| 2875 | dwc2_gadget_start_isoc_ddma(hs_ep); |
| 2876 | return; |
| 2877 | } |
| 2878 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2879 | if (hs_ep->interval > 1) { |
| 2880 | u32 ctrl = dwc2_readl(hsotg->regs + |
| 2881 | DIEPCTL(hs_ep->index)); |
| 2882 | if (hs_ep->target_frame & 0x1) |
| 2883 | ctrl |= DXEPCTL_SETODDFR; |
| 2884 | else |
| 2885 | ctrl |= DXEPCTL_SETEVENFR; |
| 2886 | |
| 2887 | dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index)); |
| 2888 | } |
| 2889 | |
| 2890 | dwc2_hsotg_complete_request(hsotg, hs_ep, |
| 2891 | get_ep_head(hs_ep), 0); |
| 2892 | } |
| 2893 | |
| 2894 | dwc2_gadget_incr_frame_num(hs_ep); |
| 2895 | } |
| 2896 | |
| 2897 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2898 | * dwc2_hsotg_epint - handle an in/out endpoint interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2899 | * @hsotg: The driver state |
| 2900 | * @idx: The index for the endpoint (0..15) |
| 2901 | * @dir_in: Set if this is an IN endpoint |
| 2902 | * |
| 2903 | * Process and clear any interrupt pending for an individual endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2904 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2905 | static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2906 | int dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2907 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2908 | 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] | 2909 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 2910 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
| 2911 | u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2912 | u32 ints; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2913 | u32 ctrl; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2914 | |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2915 | ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2916 | ctrl = dwc2_readl(hsotg->regs + epctl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2917 | |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2918 | /* Clear endpoint interrupts */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2919 | dwc2_writel(ints, hsotg->regs + epint_reg); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2920 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2921 | if (!hs_ep) { |
| 2922 | dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2923 | __func__, idx, dir_in ? "in" : "out"); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2924 | return; |
| 2925 | } |
| 2926 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2927 | dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n", |
| 2928 | __func__, idx, dir_in ? "in" : "out", ints); |
| 2929 | |
Mian Yousaf Kaukab | b787d75 | 2015-01-09 13:38:43 +0100 | [diff] [blame] | 2930 | /* Don't process XferCompl interrupt if it is a setup packet */ |
| 2931 | if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD))) |
| 2932 | ints &= ~DXEPINT_XFERCOMPL; |
| 2933 | |
Vahram Aharonyan | f0afdb4 | 2016-11-14 19:16:48 -0800 | [diff] [blame] | 2934 | /* |
| 2935 | * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP |
| 2936 | * stage and xfercomplete was generated without SETUP phase done |
| 2937 | * interrupt. SW should parse received setup packet only after host's |
| 2938 | * exit from setup phase of control transfer. |
| 2939 | */ |
| 2940 | if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in && |
| 2941 | hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP)) |
| 2942 | ints &= ~DXEPINT_XFERCOMPL; |
| 2943 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2944 | if (ints & DXEPINT_XFERCOMPL) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2945 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2946 | "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 2947 | __func__, dwc2_readl(hsotg->regs + epctl_reg), |
| 2948 | dwc2_readl(hsotg->regs + epsiz_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2949 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2950 | /* In DDMA handle isochronous requests separately */ |
| 2951 | if (using_desc_dma(hsotg) && hs_ep->isochronous) { |
| 2952 | dwc2_gadget_complete_isoc_request_ddma(hs_ep); |
| 2953 | /* Try to start next isoc request */ |
| 2954 | dwc2_gadget_start_next_isoc_ddma(hs_ep); |
| 2955 | } else if (dir_in) { |
| 2956 | /* |
| 2957 | * We get OutDone from the FIFO, so we only |
| 2958 | * need to look at completing IN requests here |
| 2959 | * if operating slave mode |
| 2960 | */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2961 | if (hs_ep->isochronous && hs_ep->interval > 1) |
| 2962 | dwc2_gadget_incr_frame_num(hs_ep); |
| 2963 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2964 | dwc2_hsotg_complete_in(hsotg, hs_ep); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2965 | if (ints & DXEPINT_NAKINTRPT) |
| 2966 | ints &= ~DXEPINT_NAKINTRPT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2967 | |
Ben Dooks | c9a64ea | 2010-07-19 09:40:46 +0100 | [diff] [blame] | 2968 | if (idx == 0 && !hs_ep->req) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2969 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2970 | } else if (using_dma(hsotg)) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2971 | /* |
| 2972 | * We're using DMA, we need to fire an OutDone here |
| 2973 | * as we ignore the RXFIFO. |
| 2974 | */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2975 | if (hs_ep->isochronous && hs_ep->interval > 1) |
| 2976 | dwc2_gadget_incr_frame_num(hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2977 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2978 | dwc2_hsotg_handle_outdone(hsotg, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2979 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2980 | } |
| 2981 | |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2982 | if (ints & DXEPINT_EPDISBLD) |
| 2983 | dwc2_gadget_handle_ep_disabled(hs_ep); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2984 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2985 | if (ints & DXEPINT_OUTTKNEPDIS) |
| 2986 | dwc2_gadget_handle_out_token_ep_disabled(hs_ep); |
| 2987 | |
| 2988 | if (ints & DXEPINT_NAKINTRPT) |
| 2989 | dwc2_gadget_handle_nak(hs_ep); |
| 2990 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2991 | if (ints & DXEPINT_AHBERR) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2992 | dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2993 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2994 | if (ints & DXEPINT_SETUP) { /* Setup or Timeout */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2995 | dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__); |
| 2996 | |
| 2997 | if (using_dma(hsotg) && idx == 0) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2998 | /* |
| 2999 | * this is the notification we've received a |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3000 | * setup packet. In non-DMA mode we'd get this |
| 3001 | * from the RXFIFO, instead we need to process |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3002 | * the setup here. |
| 3003 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3004 | |
| 3005 | if (dir_in) |
| 3006 | WARN_ON_ONCE(1); |
| 3007 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3008 | dwc2_hsotg_handle_outdone(hsotg, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3009 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3010 | } |
| 3011 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 3012 | if (ints & DXEPINT_STSPHSERCVD) { |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3013 | dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__); |
| 3014 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 3015 | /* Move to STATUS IN for DDMA */ |
| 3016 | if (using_desc_dma(hsotg)) |
| 3017 | dwc2_hsotg_ep0_zlp(hsotg, true); |
| 3018 | } |
| 3019 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3020 | if (ints & DXEPINT_BACK2BACKSETUP) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3021 | dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3022 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3023 | if (ints & DXEPINT_BNAINTR) { |
| 3024 | dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__); |
| 3025 | |
| 3026 | /* |
| 3027 | * Try to start next isoc request, if any. |
| 3028 | * Sometimes the endpoint remains enabled after BNA interrupt |
| 3029 | * assertion, which is not expected, hence we can enter here |
| 3030 | * couple of times. |
| 3031 | */ |
| 3032 | if (hs_ep->isochronous) |
| 3033 | dwc2_gadget_start_next_isoc_ddma(hs_ep); |
| 3034 | } |
| 3035 | |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3036 | if (dir_in && !hs_ep->isochronous) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3037 | /* not sure if this is important, but we'll clear it anyway */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3038 | if (ints & DXEPINT_INTKNTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3039 | dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n", |
| 3040 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | /* this probably means something bad is happening */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3044 | if (ints & DXEPINT_INTKNEPMIS) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3045 | dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n", |
| 3046 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3047 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3048 | |
| 3049 | /* FIFO has space or is empty (see GAHBCFG) */ |
| 3050 | if (hsotg->dedicated_fifos && |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3051 | ints & DXEPINT_TXFEMP) { |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3052 | dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n", |
| 3053 | __func__, idx); |
Anton Tikhomirov | 70fa030 | 2012-03-06 14:08:29 +0900 | [diff] [blame] | 3054 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3055 | dwc2_hsotg_trytx(hsotg, hs_ep); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3056 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3057 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3061 | * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3062 | * @hsotg: The device state. |
| 3063 | * |
| 3064 | * Handle updating the device settings after the enumeration phase has |
| 3065 | * been completed. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3066 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3067 | static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3068 | { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3069 | u32 dsts = dwc2_readl(hsotg->regs + DSTS); |
Jingoo Han | 9b2667f | 2014-08-20 12:04:09 +0900 | [diff] [blame] | 3070 | int ep0_mps = 0, ep_mps = 8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3071 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3072 | /* |
| 3073 | * This should signal the finish of the enumeration phase |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3074 | * of the USB handshaking, so we should now know what rate |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3075 | * we connected at. |
| 3076 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3077 | |
| 3078 | dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts); |
| 3079 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3080 | /* |
| 3081 | * 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] | 3082 | * 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] | 3083 | * not advertise a 64byte MPS on EP0. |
| 3084 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3085 | |
| 3086 | /* catch both EnumSpd_FS and EnumSpd_FS48 */ |
Marek Vasut | 6d76c92 | 2015-12-18 03:26:17 +0100 | [diff] [blame] | 3087 | switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3088 | case DSTS_ENUMSPD_FS: |
| 3089 | case DSTS_ENUMSPD_FS48: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3090 | hsotg->gadget.speed = USB_SPEED_FULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3091 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 3092 | ep_mps = 1023; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3093 | break; |
| 3094 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3095 | case DSTS_ENUMSPD_HS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3096 | hsotg->gadget.speed = USB_SPEED_HIGH; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3097 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 3098 | ep_mps = 1024; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3099 | break; |
| 3100 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3101 | case DSTS_ENUMSPD_LS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3102 | hsotg->gadget.speed = USB_SPEED_LOW; |
Vardan Mikayelyan | 552d940 | 2016-11-14 19:17:00 -0800 | [diff] [blame] | 3103 | ep0_mps = 8; |
| 3104 | ep_mps = 8; |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3105 | /* |
| 3106 | * note, we don't actually support LS in this driver at the |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3107 | * moment, and the documentation seems to imply that it isn't |
| 3108 | * supported by the PHYs on some of the devices. |
| 3109 | */ |
| 3110 | break; |
| 3111 | } |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 3112 | dev_info(hsotg->dev, "new device is %s\n", |
| 3113 | usb_speed_string(hsotg->gadget.speed)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3114 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3115 | /* |
| 3116 | * we should now know the maximum packet size for an |
| 3117 | * endpoint, so set the endpoints to a default value. |
| 3118 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3119 | |
| 3120 | if (ep0_mps) { |
| 3121 | int i; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3122 | /* Initialize ep0 for both in and out directions */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3123 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1); |
| 3124 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3125 | for (i = 1; i < hsotg->num_of_eps; i++) { |
| 3126 | if (hsotg->eps_in[i]) |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3127 | dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, |
| 3128 | 0, 1); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3129 | if (hsotg->eps_out[i]) |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3130 | dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, |
| 3131 | 0, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3132 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | /* ensure after enumeration our EP0 is active */ |
| 3136 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3137 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3138 | |
| 3139 | 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] | 3140 | dwc2_readl(hsotg->regs + DIEPCTL0), |
| 3141 | dwc2_readl(hsotg->regs + DOEPCTL0)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | /** |
| 3145 | * kill_all_requests - remove all requests from the endpoint's queue |
| 3146 | * @hsotg: The device state. |
| 3147 | * @ep: The endpoint the requests may be on. |
| 3148 | * @result: The result code to use. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3149 | * |
| 3150 | * Go through the requests on the given endpoint and mark them |
| 3151 | * completed with the given result code. |
| 3152 | */ |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3153 | static void kill_all_requests(struct dwc2_hsotg *hsotg, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3154 | struct dwc2_hsotg_ep *ep, |
Robert Baldyga | 6b448af | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3155 | int result) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3156 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3157 | struct dwc2_hsotg_req *req, *treq; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3158 | unsigned int size; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3159 | |
Robert Baldyga | 6b448af | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3160 | ep->req = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3161 | |
Robert Baldyga | 6b448af | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3162 | list_for_each_entry_safe(req, treq, &ep->queue, queue) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3163 | dwc2_hsotg_complete_request(hsotg, ep, req, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3164 | result); |
Robert Baldyga | 6b448af | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3165 | |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3166 | if (!hsotg->dedicated_fifos) |
| 3167 | return; |
Robert Baldyga | ad674a1 | 2016-08-29 13:38:50 -0700 | [diff] [blame] | 3168 | size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3169 | if (size < ep->fifo_size) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3170 | dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3171 | } |
| 3172 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3173 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3174 | * dwc2_hsotg_disconnect - disconnect service |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3175 | * @hsotg: The device state. |
| 3176 | * |
Lukasz Majewski | 5e89134 | 2012-05-04 14:17:07 +0200 | [diff] [blame] | 3177 | * The device has been disconnected. Remove all current |
| 3178 | * transactions and signal the gadget driver that this |
| 3179 | * has happened. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3180 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3181 | void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3182 | { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3183 | unsigned int ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3184 | |
Marek Szyprowski | 4ace06e | 2014-11-21 15:14:47 +0100 | [diff] [blame] | 3185 | if (!hsotg->connected) |
| 3186 | return; |
| 3187 | |
| 3188 | hsotg->connected = 0; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 3189 | hsotg->test_mode = 0; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3190 | |
| 3191 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 3192 | if (hsotg->eps_in[ep]) |
| 3193 | kill_all_requests(hsotg, hsotg->eps_in[ep], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3194 | -ESHUTDOWN); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3195 | if (hsotg->eps_out[ep]) |
| 3196 | kill_all_requests(hsotg, hsotg->eps_out[ep], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3197 | -ESHUTDOWN); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3198 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3199 | |
| 3200 | call_gadget(hsotg, disconnect); |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 3201 | hsotg->lx_state = DWC2_L3; |
John Stultz | ce2b21a | 2017-10-23 14:32:50 -0700 | [diff] [blame] | 3202 | |
| 3203 | usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3207 | * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3208 | * @hsotg: The device state: |
| 3209 | * @periodic: True if this is a periodic FIFO interrupt |
| 3210 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3211 | static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3212 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3213 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3214 | int epno, ret; |
| 3215 | |
| 3216 | /* look through for any more data to transmit */ |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3217 | for (epno = 0; epno < hsotg->num_of_eps; epno++) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3218 | ep = index_to_ep(hsotg, epno, 1); |
| 3219 | |
| 3220 | if (!ep) |
| 3221 | continue; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3222 | |
| 3223 | if (!ep->dir_in) |
| 3224 | continue; |
| 3225 | |
| 3226 | if ((periodic && !ep->periodic) || |
| 3227 | (!periodic && ep->periodic)) |
| 3228 | continue; |
| 3229 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3230 | ret = dwc2_hsotg_trytx(hsotg, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3231 | if (ret < 0) |
| 3232 | break; |
| 3233 | } |
| 3234 | } |
| 3235 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3236 | /* IRQ flags which will trigger a retry around the IRQ loop */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3237 | #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \ |
| 3238 | GINTSTS_PTXFEMP | \ |
| 3239 | GINTSTS_RXFLVL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3240 | |
| 3241 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3242 | * dwc2_hsotg_core_init - issue softreset to the core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3243 | * @hsotg: The device state |
| 3244 | * |
| 3245 | * Issue a soft reset to the core, and await the core finishing it. |
| 3246 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3247 | void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3248 | bool is_usb_reset) |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3249 | { |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3250 | u32 intmsk; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3251 | u32 val; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3252 | u32 usbcfg; |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3253 | u32 dcfg = 0; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3254 | |
Mian Yousaf Kaukab | 5390d43 | 2015-09-29 12:08:25 +0200 | [diff] [blame] | 3255 | /* Kill any ep0 requests as controller will be reinitialized */ |
| 3256 | kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET); |
| 3257 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3258 | if (!is_usb_reset) |
John Stultz | 6e6360b | 2017-01-23 14:59:14 -0800 | [diff] [blame] | 3259 | if (dwc2_core_reset(hsotg, true)) |
Gregory Herrero | 86de489 | 2015-09-29 12:08:21 +0200 | [diff] [blame] | 3260 | return; |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3261 | |
| 3262 | /* |
| 3263 | * we must now enable ep0 ready for host detection and then |
| 3264 | * set configuration. |
| 3265 | */ |
| 3266 | |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3267 | /* keep other bits untouched (so e.g. forced modes are not lost) */ |
| 3268 | usbcfg = dwc2_readl(hsotg->regs + GUSBCFG); |
| 3269 | usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP | |
Amelie Delaunay | ca02954 | 2017-01-12 16:09:44 +0100 | [diff] [blame] | 3270 | GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK); |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3271 | |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3272 | if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS && |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3273 | (hsotg->params.speed == DWC2_SPEED_PARAM_FULL || |
| 3274 | hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) { |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3275 | /* FS/LS Dedicated Transceiver Interface */ |
| 3276 | usbcfg |= GUSBCFG_PHYSEL; |
| 3277 | } else { |
| 3278 | /* set the PLL on, remove the HNP/SRP and set the PHY */ |
| 3279 | val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5; |
| 3280 | usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) | |
| 3281 | (val << GUSBCFG_USBTRDTIM_SHIFT); |
| 3282 | } |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3283 | dwc2_writel(usbcfg, hsotg->regs + GUSBCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3284 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3285 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3286 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3287 | if (!is_usb_reset) |
| 3288 | __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3289 | |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3290 | dcfg |= DCFG_EPMISCNT(1); |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3291 | |
| 3292 | switch (hsotg->params.speed) { |
| 3293 | case DWC2_SPEED_PARAM_LOW: |
| 3294 | dcfg |= DCFG_DEVSPD_LS; |
| 3295 | break; |
| 3296 | case DWC2_SPEED_PARAM_FULL: |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3297 | if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) |
| 3298 | dcfg |= DCFG_DEVSPD_FS48; |
| 3299 | else |
| 3300 | dcfg |= DCFG_DEVSPD_FS; |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3301 | break; |
| 3302 | default: |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3303 | dcfg |= DCFG_DEVSPD_HS; |
| 3304 | } |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3305 | |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3306 | dwc2_writel(dcfg, hsotg->regs + DCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3307 | |
| 3308 | /* Clear any pending OTG interrupts */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3309 | dwc2_writel(0xffffffff, hsotg->regs + GOTGINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3310 | |
| 3311 | /* Clear any pending interrupts */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3312 | dwc2_writel(0xffffffff, hsotg->regs + GINTSTS); |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3313 | intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3314 | GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF | |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3315 | GINTSTS_USBRST | GINTSTS_RESETDET | |
| 3316 | GINTSTS_ENUMDONE | GINTSTS_OTGINT | |
Vahram Aharonyan | f473670 | 2016-11-14 19:16:38 -0800 | [diff] [blame] | 3317 | GINTSTS_USBSUSP | GINTSTS_WKUPINT; |
| 3318 | |
| 3319 | if (!using_desc_dma(hsotg)) |
| 3320 | intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT; |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3321 | |
John Youn | 95832c0 | 2017-01-23 14:57:26 -0800 | [diff] [blame] | 3322 | if (!hsotg->params.external_id_pin_ctl) |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3323 | intmsk |= GINTSTS_CONIDSTSCHNG; |
| 3324 | |
| 3325 | dwc2_writel(intmsk, hsotg->regs + GINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3326 | |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3327 | if (using_dma(hsotg)) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3328 | dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN | |
| 3329 | (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT), |
| 3330 | hsotg->regs + GAHBCFG); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3331 | |
| 3332 | /* Set DDMA mode support in the core if needed */ |
| 3333 | if (using_desc_dma(hsotg)) |
| 3334 | __orr32(hsotg->regs + DCFG, DCFG_DESCDMA_EN); |
| 3335 | |
| 3336 | } else { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3337 | dwc2_writel(((hsotg->dedicated_fifos) ? |
| 3338 | (GAHBCFG_NP_TXF_EMP_LVL | |
| 3339 | GAHBCFG_P_TXF_EMP_LVL) : 0) | |
| 3340 | GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3341 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3342 | |
| 3343 | /* |
Robert Baldyga | 8acc829 | 2013-09-19 11:50:23 +0200 | [diff] [blame] | 3344 | * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts |
| 3345 | * when we have no data to transfer. Otherwise we get being flooded by |
| 3346 | * interrupts. |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3347 | */ |
| 3348 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3349 | dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ? |
Mian Yousaf Kaukab | 6ff2e83 | 2015-01-09 13:38:42 +0100 | [diff] [blame] | 3350 | DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3351 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3352 | DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3353 | hsotg->regs + DIEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3354 | |
| 3355 | /* |
| 3356 | * don't need XferCompl, we get that from RXFIFO in slave mode. In |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3357 | * DMA mode we may need this and StsPhseRcvd. |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3358 | */ |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3359 | dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK | |
| 3360 | DOEPMSK_STSPHSERCVDMSK) : 0) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3361 | DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK | |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3362 | DOEPMSK_SETUPMSK, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3363 | hsotg->regs + DOEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3364 | |
Vahram Aharonyan | ec01f0b | 2016-11-14 19:16:43 -0800 | [diff] [blame] | 3365 | /* Enable BNA interrupt for DDMA */ |
| 3366 | if (using_desc_dma(hsotg)) |
| 3367 | __orr32(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK); |
| 3368 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3369 | dwc2_writel(0, hsotg->regs + DAINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3370 | |
| 3371 | 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] | 3372 | dwc2_readl(hsotg->regs + DIEPCTL0), |
| 3373 | dwc2_readl(hsotg->regs + DOEPCTL0)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3374 | |
| 3375 | /* enable in and out endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3376 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3377 | |
| 3378 | /* |
| 3379 | * Enable the RXFIFO when in slave mode, as this is how we collect |
| 3380 | * the data. In DMA mode, we get events from the FIFO but also |
| 3381 | * things we cannot process, so do not use it. |
| 3382 | */ |
| 3383 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3384 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3385 | |
| 3386 | /* Enable interrupts for EP0 in and out */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3387 | dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1); |
| 3388 | dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3389 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3390 | if (!is_usb_reset) { |
| 3391 | __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE); |
| 3392 | udelay(10); /* see openiboot */ |
| 3393 | __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE); |
| 3394 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3395 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3396 | 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] | 3397 | |
| 3398 | /* |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3399 | * DxEPCTL_USBActEp says RO in manual, but seems to be set by |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3400 | * writing to the EPCTL register.. |
| 3401 | */ |
| 3402 | |
| 3403 | /* set to read 1 8byte packet */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3404 | dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3405 | DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3406 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3407 | dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3408 | DXEPCTL_CNAK | DXEPCTL_EPENA | |
| 3409 | DXEPCTL_USBACTEP, |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3410 | hsotg->regs + DOEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3411 | |
| 3412 | /* enable, but don't activate EP0in */ |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3413 | dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3414 | DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3415 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3416 | dwc2_hsotg_enqueue_setup(hsotg); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3417 | |
| 3418 | 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] | 3419 | dwc2_readl(hsotg->regs + DIEPCTL0), |
| 3420 | dwc2_readl(hsotg->regs + DOEPCTL0)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3421 | |
| 3422 | /* clear global NAKs */ |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3423 | val = DCTL_CGOUTNAK | DCTL_CGNPINNAK; |
| 3424 | if (!is_usb_reset) |
| 3425 | val |= DCTL_SFTDISCON; |
| 3426 | __orr32(hsotg->regs + DCTL, val); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3427 | |
| 3428 | /* must be at-least 3ms to allow bus to see disconnect */ |
| 3429 | mdelay(3); |
| 3430 | |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 3431 | hsotg->lx_state = DWC2_L0; |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3432 | } |
Marek Szyprowski | ac3c81f | 2014-10-20 12:45:35 +0200 | [diff] [blame] | 3433 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3434 | static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3435 | { |
| 3436 | /* set the soft-disconnect bit */ |
| 3437 | __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON); |
| 3438 | } |
| 3439 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3440 | void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3441 | { |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3442 | /* remove the soft-disconnect and let's go */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3443 | __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | /** |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3447 | * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt. |
| 3448 | * @hsotg: The device state: |
| 3449 | * |
| 3450 | * This interrupt indicates one of the following conditions occurred while |
| 3451 | * transmitting an ISOC transaction. |
| 3452 | * - Corrupted IN Token for ISOC EP. |
| 3453 | * - Packet not complete in FIFO. |
| 3454 | * |
| 3455 | * The following actions will be taken: |
| 3456 | * - Determine the EP |
| 3457 | * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO |
| 3458 | */ |
| 3459 | static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg) |
| 3460 | { |
| 3461 | struct dwc2_hsotg_ep *hs_ep; |
| 3462 | u32 epctrl; |
| 3463 | u32 idx; |
| 3464 | |
| 3465 | dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n"); |
| 3466 | |
| 3467 | for (idx = 1; idx <= hsotg->num_of_eps; idx++) { |
| 3468 | hs_ep = hsotg->eps_in[idx]; |
| 3469 | epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx)); |
| 3470 | if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous && |
| 3471 | dwc2_gadget_target_frame_elapsed(hs_ep)) { |
| 3472 | epctrl |= DXEPCTL_SNAK; |
| 3473 | epctrl |= DXEPCTL_EPDIS; |
| 3474 | dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx)); |
| 3475 | } |
| 3476 | } |
| 3477 | |
| 3478 | /* Clear interrupt */ |
| 3479 | dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS); |
| 3480 | } |
| 3481 | |
| 3482 | /** |
| 3483 | * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt |
| 3484 | * @hsotg: The device state: |
| 3485 | * |
| 3486 | * This interrupt indicates one of the following conditions occurred while |
| 3487 | * transmitting an ISOC transaction. |
| 3488 | * - Corrupted OUT Token for ISOC EP. |
| 3489 | * - Packet not complete in FIFO. |
| 3490 | * |
| 3491 | * The following actions will be taken: |
| 3492 | * - Determine the EP |
| 3493 | * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed. |
| 3494 | */ |
| 3495 | static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg) |
| 3496 | { |
| 3497 | u32 gintsts; |
| 3498 | u32 gintmsk; |
| 3499 | u32 epctrl; |
| 3500 | struct dwc2_hsotg_ep *hs_ep; |
| 3501 | int idx; |
| 3502 | |
| 3503 | dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__); |
| 3504 | |
| 3505 | for (idx = 1; idx <= hsotg->num_of_eps; idx++) { |
| 3506 | hs_ep = hsotg->eps_out[idx]; |
| 3507 | epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx)); |
| 3508 | if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous && |
| 3509 | dwc2_gadget_target_frame_elapsed(hs_ep)) { |
| 3510 | /* Unmask GOUTNAKEFF interrupt */ |
| 3511 | gintmsk = dwc2_readl(hsotg->regs + GINTMSK); |
| 3512 | gintmsk |= GINTSTS_GOUTNAKEFF; |
| 3513 | dwc2_writel(gintmsk, hsotg->regs + GINTMSK); |
| 3514 | |
| 3515 | gintsts = dwc2_readl(hsotg->regs + GINTSTS); |
| 3516 | if (!(gintsts & GINTSTS_GOUTNAKEFF)) |
| 3517 | __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK); |
| 3518 | } |
| 3519 | } |
| 3520 | |
| 3521 | /* Clear interrupt */ |
| 3522 | dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS); |
| 3523 | } |
| 3524 | |
| 3525 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3526 | * dwc2_hsotg_irq - handle device interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3527 | * @irq: The IRQ number triggered |
| 3528 | * @pw: The pw value when registered the handler. |
| 3529 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3530 | static irqreturn_t dwc2_hsotg_irq(int irq, void *pw) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3531 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3532 | struct dwc2_hsotg *hsotg = pw; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3533 | int retry_count = 8; |
| 3534 | u32 gintsts; |
| 3535 | u32 gintmsk; |
| 3536 | |
Vardan Mikayelyan | ee3de8d | 2016-04-27 20:20:48 -0700 | [diff] [blame] | 3537 | if (!dwc2_is_device_mode(hsotg)) |
| 3538 | return IRQ_NONE; |
| 3539 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3540 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3541 | irq_retry: |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3542 | gintsts = dwc2_readl(hsotg->regs + GINTSTS); |
| 3543 | gintmsk = dwc2_readl(hsotg->regs + GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3544 | |
| 3545 | dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n", |
| 3546 | __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count); |
| 3547 | |
| 3548 | gintsts &= gintmsk; |
| 3549 | |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3550 | if (gintsts & GINTSTS_RESETDET) { |
| 3551 | dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__); |
| 3552 | |
| 3553 | dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS); |
| 3554 | |
| 3555 | /* This event must be used only if controller is suspended */ |
| 3556 | if (hsotg->lx_state == DWC2_L2) { |
| 3557 | dwc2_exit_hibernation(hsotg, true); |
| 3558 | hsotg->lx_state = DWC2_L0; |
| 3559 | } |
| 3560 | } |
| 3561 | |
| 3562 | if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) { |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3563 | u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL); |
| 3564 | u32 connected = hsotg->connected; |
| 3565 | |
| 3566 | dev_dbg(hsotg->dev, "%s: USBRst\n", __func__); |
| 3567 | dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n", |
| 3568 | dwc2_readl(hsotg->regs + GNPTXSTS)); |
| 3569 | |
| 3570 | dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS); |
| 3571 | |
| 3572 | /* Report disconnection if it is not already done. */ |
| 3573 | dwc2_hsotg_disconnect(hsotg); |
| 3574 | |
Minas Harutyunyan | 307bc11 | 2017-07-11 14:25:13 +0400 | [diff] [blame] | 3575 | /* Reset device address to zero */ |
| 3576 | __bic32(hsotg->regs + DCFG, DCFG_DEVADDR_MASK); |
| 3577 | |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3578 | if (usb_status & GOTGCTL_BSESVLD && connected) |
| 3579 | dwc2_hsotg_core_init_disconnected(hsotg, true); |
| 3580 | } |
| 3581 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3582 | if (gintsts & GINTSTS_ENUMDONE) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3583 | dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3584 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3585 | dwc2_hsotg_irq_enumdone(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3586 | } |
| 3587 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3588 | if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3589 | u32 daint = dwc2_readl(hsotg->regs + DAINT); |
| 3590 | u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3591 | u32 daint_out, daint_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3592 | int ep; |
| 3593 | |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3594 | daint &= daintmsk; |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3595 | daint_out = daint >> DAINT_OUTEP_SHIFT; |
| 3596 | daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3597 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3598 | dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint); |
| 3599 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 3600 | for (ep = 0; ep < hsotg->num_of_eps && daint_out; |
| 3601 | ep++, daint_out >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3602 | if (daint_out & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3603 | dwc2_hsotg_epint(hsotg, ep, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3604 | } |
| 3605 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 3606 | for (ep = 0; ep < hsotg->num_of_eps && daint_in; |
| 3607 | ep++, daint_in >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3608 | if (daint_in & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3609 | dwc2_hsotg_epint(hsotg, ep, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3610 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3611 | } |
| 3612 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3613 | /* check both FIFOs */ |
| 3614 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3615 | if (gintsts & GINTSTS_NPTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3616 | dev_dbg(hsotg->dev, "NPTxFEmp\n"); |
| 3617 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3618 | /* |
| 3619 | * Disable the interrupt to stop it happening again |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3620 | * unless one of these endpoint routines decides that |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3621 | * it needs re-enabling |
| 3622 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3623 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3624 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP); |
| 3625 | dwc2_hsotg_irq_fifoempty(hsotg, false); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3626 | } |
| 3627 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3628 | if (gintsts & GINTSTS_PTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3629 | dev_dbg(hsotg->dev, "PTxFEmp\n"); |
| 3630 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3631 | /* See note in GINTSTS_NPTxFEmp */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3632 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3633 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP); |
| 3634 | dwc2_hsotg_irq_fifoempty(hsotg, true); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3635 | } |
| 3636 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3637 | if (gintsts & GINTSTS_RXFLVL) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3638 | /* |
| 3639 | * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3640 | * we need to retry dwc2_hsotg_handle_rx if this is still |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3641 | * set. |
| 3642 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3643 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3644 | dwc2_hsotg_handle_rx(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3645 | } |
| 3646 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3647 | if (gintsts & GINTSTS_ERLYSUSP) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3648 | dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n"); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3649 | dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3650 | } |
| 3651 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3652 | /* |
| 3653 | * these next two seem to crop-up occasionally causing the core |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3654 | * to shutdown the USB transfer, so try clearing them and logging |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3655 | * the occurrence. |
| 3656 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3657 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3658 | if (gintsts & GINTSTS_GOUTNAKEFF) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3659 | u8 idx; |
| 3660 | u32 epctrl; |
| 3661 | u32 gintmsk; |
| 3662 | struct dwc2_hsotg_ep *hs_ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3663 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3664 | /* Mask this interrupt */ |
| 3665 | gintmsk = dwc2_readl(hsotg->regs + GINTMSK); |
| 3666 | gintmsk &= ~GINTSTS_GOUTNAKEFF; |
| 3667 | dwc2_writel(gintmsk, hsotg->regs + GINTMSK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3668 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3669 | dev_dbg(hsotg->dev, "GOUTNakEff triggered\n"); |
| 3670 | for (idx = 1; idx <= hsotg->num_of_eps; idx++) { |
| 3671 | hs_ep = hsotg->eps_out[idx]; |
| 3672 | epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx)); |
| 3673 | |
| 3674 | if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) { |
| 3675 | epctrl |= DXEPCTL_SNAK; |
| 3676 | epctrl |= DXEPCTL_EPDIS; |
| 3677 | dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx)); |
| 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3682 | } |
| 3683 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3684 | if (gintsts & GINTSTS_GINNAKEFF) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3685 | dev_info(hsotg->dev, "GINNakEff triggered\n"); |
| 3686 | |
Gregory Herrero | 3be99cd | 2015-12-07 12:07:31 +0100 | [diff] [blame] | 3687 | __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3688 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3689 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3690 | } |
| 3691 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3692 | if (gintsts & GINTSTS_INCOMPL_SOIN) |
| 3693 | dwc2_gadget_handle_incomplete_isoc_in(hsotg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 3694 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3695 | if (gintsts & GINTSTS_INCOMPL_SOOUT) |
| 3696 | dwc2_gadget_handle_incomplete_isoc_out(hsotg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 3697 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3698 | /* |
| 3699 | * if we've had fifo events, we should try and go around the |
| 3700 | * loop again to see if there's any point in returning yet. |
| 3701 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3702 | |
| 3703 | if (gintsts & IRQ_RETRY_MASK && --retry_count > 0) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 3704 | goto irq_retry; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3705 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3706 | spin_unlock(&hsotg->lock); |
| 3707 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3708 | return IRQ_HANDLED; |
| 3709 | } |
| 3710 | |
Vahram Aharonyan | a4f8277 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3711 | static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg, |
| 3712 | u32 bit, u32 timeout) |
| 3713 | { |
| 3714 | u32 i; |
| 3715 | |
| 3716 | for (i = 0; i < timeout; i++) { |
| 3717 | if (dwc2_readl(hs_otg->regs + reg) & bit) |
| 3718 | return 0; |
| 3719 | udelay(1); |
| 3720 | } |
| 3721 | |
| 3722 | return -ETIMEDOUT; |
| 3723 | } |
| 3724 | |
| 3725 | static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg, |
| 3726 | struct dwc2_hsotg_ep *hs_ep) |
| 3727 | { |
| 3728 | u32 epctrl_reg; |
| 3729 | u32 epint_reg; |
| 3730 | |
| 3731 | epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) : |
| 3732 | DOEPCTL(hs_ep->index); |
| 3733 | epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) : |
| 3734 | DOEPINT(hs_ep->index); |
| 3735 | |
| 3736 | dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__, |
| 3737 | hs_ep->name); |
| 3738 | |
| 3739 | if (hs_ep->dir_in) { |
| 3740 | if (hsotg->dedicated_fifos || hs_ep->periodic) { |
| 3741 | __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK); |
| 3742 | /* Wait for Nak effect */ |
| 3743 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, |
| 3744 | DXEPINT_INEPNAKEFF, 100)) |
| 3745 | dev_warn(hsotg->dev, |
| 3746 | "%s: timeout DIEPINT.NAKEFF\n", |
| 3747 | __func__); |
| 3748 | } else { |
| 3749 | __orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK); |
| 3750 | /* Wait for Nak effect */ |
| 3751 | if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, |
| 3752 | GINTSTS_GINNAKEFF, 100)) |
| 3753 | dev_warn(hsotg->dev, |
| 3754 | "%s: timeout GINTSTS.GINNAKEFF\n", |
| 3755 | __func__); |
| 3756 | } |
| 3757 | } else { |
| 3758 | if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF)) |
| 3759 | __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK); |
| 3760 | |
| 3761 | /* Wait for global nak to take effect */ |
| 3762 | if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, |
| 3763 | GINTSTS_GOUTNAKEFF, 100)) |
| 3764 | dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n", |
| 3765 | __func__); |
| 3766 | } |
| 3767 | |
| 3768 | /* Disable ep */ |
| 3769 | __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK); |
| 3770 | |
| 3771 | /* Wait for ep to be disabled */ |
| 3772 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100)) |
| 3773 | dev_warn(hsotg->dev, |
| 3774 | "%s: timeout DOEPCTL.EPDisable\n", __func__); |
| 3775 | |
| 3776 | /* Clear EPDISBLD interrupt */ |
| 3777 | __orr32(hsotg->regs + epint_reg, DXEPINT_EPDISBLD); |
| 3778 | |
| 3779 | if (hs_ep->dir_in) { |
| 3780 | unsigned short fifo_index; |
| 3781 | |
| 3782 | if (hsotg->dedicated_fifos || hs_ep->periodic) |
| 3783 | fifo_index = hs_ep->fifo_index; |
| 3784 | else |
| 3785 | fifo_index = 0; |
| 3786 | |
| 3787 | /* Flush TX FIFO */ |
| 3788 | dwc2_flush_tx_fifo(hsotg, fifo_index); |
| 3789 | |
| 3790 | /* Clear Global In NP NAK in Shared FIFO for non periodic ep */ |
| 3791 | if (!hsotg->dedicated_fifos && !hs_ep->periodic) |
| 3792 | __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK); |
| 3793 | |
| 3794 | } else { |
| 3795 | /* Remove global NAKs */ |
| 3796 | __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK); |
| 3797 | } |
| 3798 | } |
| 3799 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3800 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3801 | * dwc2_hsotg_ep_enable - enable the given endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3802 | * @ep: The USB endpint to configure |
| 3803 | * @desc: The USB endpoint descriptor to configure with. |
| 3804 | * |
| 3805 | * This is called from the USB gadget code's usb_ep_enable(). |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3806 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3807 | static int dwc2_hsotg_ep_enable(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3808 | const struct usb_endpoint_descriptor *desc) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3809 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3810 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3811 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3812 | unsigned long flags; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3813 | unsigned int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3814 | u32 epctrl_reg; |
| 3815 | u32 epctrl; |
| 3816 | u32 mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3817 | u32 mc; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3818 | u32 mask; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3819 | unsigned int dir_in; |
| 3820 | unsigned int i, val, size; |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 3821 | int ret = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3822 | |
| 3823 | dev_dbg(hsotg->dev, |
| 3824 | "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n", |
| 3825 | __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes, |
| 3826 | desc->wMaxPacketSize, desc->bInterval); |
| 3827 | |
| 3828 | /* not to be called for EP0 */ |
Vahram Aharonyan | 8c3d609 | 2016-04-27 20:20:46 -0700 | [diff] [blame] | 3829 | if (index == 0) { |
| 3830 | dev_err(hsotg->dev, "%s: called for EP 0\n", __func__); |
| 3831 | return -EINVAL; |
| 3832 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3833 | |
| 3834 | dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0; |
| 3835 | if (dir_in != hs_ep->dir_in) { |
| 3836 | dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__); |
| 3837 | return -EINVAL; |
| 3838 | } |
| 3839 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 3840 | mps = usb_endpoint_maxp(desc); |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3841 | mc = usb_endpoint_maxp_mult(desc); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3842 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3843 | /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3844 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3845 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3846 | epctrl = dwc2_readl(hsotg->regs + epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3847 | |
| 3848 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n", |
| 3849 | __func__, epctrl, epctrl_reg); |
| 3850 | |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3851 | /* Allocate DMA descriptor chain for non-ctrl endpoints */ |
Vardan Mikayelyan | 9383e08 | 2017-01-05 18:01:48 -0800 | [diff] [blame] | 3852 | if (using_desc_dma(hsotg) && !hs_ep->desc_list) { |
| 3853 | hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev, |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3854 | MAX_DMA_DESC_NUM_GENERIC * |
| 3855 | sizeof(struct dwc2_dma_desc), |
Marek Szyprowski | 86e881e | 2016-12-01 10:02:11 +0100 | [diff] [blame] | 3856 | &hs_ep->desc_list_dma, GFP_ATOMIC); |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3857 | if (!hs_ep->desc_list) { |
| 3858 | ret = -ENOMEM; |
| 3859 | goto error2; |
| 3860 | } |
| 3861 | } |
| 3862 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 3863 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3864 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3865 | epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK); |
| 3866 | epctrl |= DXEPCTL_MPS(mps); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3867 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3868 | /* |
| 3869 | * mark the endpoint as active, otherwise the core may ignore |
| 3870 | * transactions entirely for this endpoint |
| 3871 | */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3872 | epctrl |= DXEPCTL_USBACTEP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3873 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3874 | /* update the endpoint state */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3875 | dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3876 | |
| 3877 | /* default, set to non-periodic */ |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3878 | hs_ep->isochronous = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3879 | hs_ep->periodic = 0; |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 3880 | hs_ep->halted = 0; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3881 | hs_ep->interval = desc->bInterval; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 3882 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3883 | switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
| 3884 | case USB_ENDPOINT_XFER_ISOC: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3885 | epctrl |= DXEPCTL_EPTYPE_ISO; |
| 3886 | epctrl |= DXEPCTL_SETEVENFR; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3887 | hs_ep->isochronous = 1; |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 3888 | hs_ep->interval = 1 << (desc->bInterval - 1); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3889 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
Vahram Aharonyan | ab7d219 | 2016-11-14 19:16:36 -0800 | [diff] [blame] | 3890 | hs_ep->isoc_chain_num = 0; |
| 3891 | hs_ep->next_desc = 0; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3892 | if (dir_in) { |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3893 | hs_ep->periodic = 1; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3894 | mask = dwc2_readl(hsotg->regs + DIEPMSK); |
| 3895 | mask |= DIEPMSK_NAKMSK; |
| 3896 | dwc2_writel(mask, hsotg->regs + DIEPMSK); |
| 3897 | } else { |
| 3898 | mask = dwc2_readl(hsotg->regs + DOEPMSK); |
| 3899 | mask |= DOEPMSK_OUTTKNEPDISMSK; |
| 3900 | dwc2_writel(mask, hsotg->regs + DOEPMSK); |
| 3901 | } |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3902 | break; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3903 | |
| 3904 | case USB_ENDPOINT_XFER_BULK: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3905 | epctrl |= DXEPCTL_EPTYPE_BULK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3906 | break; |
| 3907 | |
| 3908 | case USB_ENDPOINT_XFER_INT: |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3909 | if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3910 | hs_ep->periodic = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3911 | |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 3912 | if (hsotg->gadget.speed == USB_SPEED_HIGH) |
| 3913 | hs_ep->interval = 1 << (desc->bInterval - 1); |
| 3914 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3915 | epctrl |= DXEPCTL_EPTYPE_INTERRUPT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3916 | break; |
| 3917 | |
| 3918 | case USB_ENDPOINT_XFER_CONTROL: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3919 | epctrl |= DXEPCTL_EPTYPE_CONTROL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3920 | break; |
| 3921 | } |
| 3922 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3923 | /* |
| 3924 | * if the hardware has dedicated fifos, we must give each IN EP |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3925 | * a unique tx-fifo even if it is non-periodic. |
| 3926 | */ |
Robert Baldyga | 21f3bb5 | 2016-08-29 13:38:57 -0700 | [diff] [blame] | 3927 | if (dir_in && hsotg->dedicated_fifos) { |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3928 | u32 fifo_index = 0; |
| 3929 | u32 fifo_size = UINT_MAX; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3930 | |
| 3931 | size = hs_ep->ep.maxpacket * hs_ep->mc; |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 3932 | for (i = 1; i < hsotg->num_of_eps; ++i) { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3933 | if (hsotg->fifo_map & (1 << i)) |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3934 | continue; |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3935 | val = dwc2_readl(hsotg->regs + DPTXFSIZN(i)); |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3936 | val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3937 | if (val < size) |
| 3938 | continue; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3939 | /* Search for smallest acceptable fifo */ |
| 3940 | if (val < fifo_size) { |
| 3941 | fifo_size = val; |
| 3942 | fifo_index = i; |
| 3943 | } |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3944 | } |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3945 | if (!fifo_index) { |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 3946 | dev_err(hsotg->dev, |
| 3947 | "%s: No suitable fifo found\n", __func__); |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 3948 | ret = -ENOMEM; |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3949 | goto error1; |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 3950 | } |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3951 | hsotg->fifo_map |= 1 << fifo_index; |
| 3952 | epctrl |= DXEPCTL_TXFNUM(fifo_index); |
| 3953 | hs_ep->fifo_index = fifo_index; |
| 3954 | hs_ep->fifo_size = fifo_size; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3955 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3956 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3957 | /* for non control endpoints, set PID to D0 */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3958 | if (index && !hs_ep->isochronous) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3959 | epctrl |= DXEPCTL_SETD0PID; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3960 | |
| 3961 | dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n", |
| 3962 | __func__, epctrl); |
| 3963 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3964 | dwc2_writel(epctrl, hsotg->regs + epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3965 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3966 | __func__, dwc2_readl(hsotg->regs + epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3967 | |
| 3968 | /* enable the endpoint interrupt */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3969 | dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3970 | |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3971 | error1: |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 3972 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3973 | |
| 3974 | error2: |
| 3975 | if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) { |
Vardan Mikayelyan | 9383e08 | 2017-01-05 18:01:48 -0800 | [diff] [blame] | 3976 | dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC * |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3977 | sizeof(struct dwc2_dma_desc), |
| 3978 | hs_ep->desc_list, hs_ep->desc_list_dma); |
| 3979 | hs_ep->desc_list = NULL; |
| 3980 | } |
| 3981 | |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 3982 | return ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3983 | } |
| 3984 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3985 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3986 | * dwc2_hsotg_ep_disable - disable given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3987 | * @ep: The endpoint to disable. |
| 3988 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3989 | static int dwc2_hsotg_ep_disable(struct usb_ep *ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3990 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3991 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3992 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3993 | int dir_in = hs_ep->dir_in; |
| 3994 | int index = hs_ep->index; |
| 3995 | unsigned long flags; |
| 3996 | u32 epctrl_reg; |
| 3997 | u32 ctrl; |
| 3998 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 3999 | dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4000 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4001 | if (ep == &hsotg->eps_out[0]->ep) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4002 | dev_err(hsotg->dev, "%s: called for ep0\n", __func__); |
| 4003 | return -EINVAL; |
| 4004 | } |
| 4005 | |
John Stultz | 9b481092 | 2017-10-23 14:32:49 -0700 | [diff] [blame] | 4006 | if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { |
| 4007 | dev_err(hsotg->dev, "%s: called in host mode?\n", __func__); |
| 4008 | return -EINVAL; |
| 4009 | } |
| 4010 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 4011 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4012 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4013 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4014 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4015 | ctrl = dwc2_readl(hsotg->regs + epctrl_reg); |
Vahram Aharonyan | a4f8277 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 4016 | |
| 4017 | if (ctrl & DXEPCTL_EPENA) |
| 4018 | dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep); |
| 4019 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4020 | ctrl &= ~DXEPCTL_EPENA; |
| 4021 | ctrl &= ~DXEPCTL_USBACTEP; |
| 4022 | ctrl |= DXEPCTL_SNAK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4023 | |
| 4024 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4025 | dwc2_writel(ctrl, hsotg->regs + epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4026 | |
| 4027 | /* disable endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4028 | 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] | 4029 | |
Mian Yousaf Kaukab | 1141ea0 | 2015-01-09 13:38:57 +0100 | [diff] [blame] | 4030 | /* terminate all requests with shutdown */ |
| 4031 | kill_all_requests(hsotg, hs_ep, -ESHUTDOWN); |
| 4032 | |
Robert Baldyga | 1c07b20 | 2016-08-29 13:39:00 -0700 | [diff] [blame] | 4033 | hsotg->fifo_map &= ~(1 << hs_ep->fifo_index); |
| 4034 | hs_ep->fifo_index = 0; |
| 4035 | hs_ep->fifo_size = 0; |
| 4036 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4037 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4038 | return 0; |
| 4039 | } |
| 4040 | |
| 4041 | /** |
| 4042 | * on_list - check request is on the given endpoint |
| 4043 | * @ep: The endpoint to check. |
| 4044 | * @test: The request to test if it is on the endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4045 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4046 | 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] | 4047 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4048 | struct dwc2_hsotg_req *req, *treq; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4049 | |
| 4050 | list_for_each_entry_safe(req, treq, &ep->queue, queue) { |
| 4051 | if (req == test) |
| 4052 | return true; |
| 4053 | } |
| 4054 | |
| 4055 | return false; |
| 4056 | } |
| 4057 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4058 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4059 | * dwc2_hsotg_ep_dequeue - dequeue given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4060 | * @ep: The endpoint to dequeue. |
| 4061 | * @req: The request to be removed from a queue. |
| 4062 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4063 | 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] | 4064 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4065 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 4066 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4067 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4068 | unsigned long flags; |
| 4069 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 4070 | dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4071 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4072 | spin_lock_irqsave(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4073 | |
| 4074 | if (!on_list(hs_ep, hs_req)) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4075 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4076 | return -EINVAL; |
| 4077 | } |
| 4078 | |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 4079 | /* Dequeue already started request */ |
| 4080 | if (req == &hs_ep->req->req) |
| 4081 | dwc2_hsotg_ep_stop_xfr(hs, hs_ep); |
| 4082 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4083 | dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4084 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4085 | |
| 4086 | return 0; |
| 4087 | } |
| 4088 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4089 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4090 | * dwc2_hsotg_ep_sethalt - set halt on a given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4091 | * @ep: The endpoint to set halt. |
| 4092 | * @value: Set or unset the halt. |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4093 | * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if |
| 4094 | * the endpoint is busy processing requests. |
| 4095 | * |
| 4096 | * We need to stall the endpoint immediately if request comes from set_feature |
| 4097 | * protocol command handler. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4098 | */ |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4099 | 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] | 4100 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4101 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4102 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4103 | int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4104 | u32 epreg; |
| 4105 | u32 epctl; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4106 | u32 xfertype; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4107 | |
| 4108 | dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value); |
| 4109 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 4110 | if (index == 0) { |
| 4111 | if (value) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4112 | dwc2_hsotg_stall_ep0(hs); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 4113 | else |
| 4114 | dev_warn(hs->dev, |
| 4115 | "%s: can't clear halt on ep0\n", __func__); |
| 4116 | return 0; |
| 4117 | } |
| 4118 | |
Vahram Aharonyan | 15186f1 | 2016-05-23 22:41:59 -0700 | [diff] [blame] | 4119 | if (hs_ep->isochronous) { |
| 4120 | dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name); |
| 4121 | return -EINVAL; |
| 4122 | } |
| 4123 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4124 | if (!now && value && !list_empty(&hs_ep->queue)) { |
| 4125 | dev_dbg(hs->dev, "%s request is pending, cannot halt\n", |
| 4126 | ep->name); |
| 4127 | return -EAGAIN; |
| 4128 | } |
| 4129 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4130 | if (hs_ep->dir_in) { |
| 4131 | epreg = DIEPCTL(index); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4132 | epctl = dwc2_readl(hs->regs + epreg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4133 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4134 | if (value) { |
Felipe Balbi | 5a350d5 | 2015-06-29 20:17:22 -0500 | [diff] [blame] | 4135 | epctl |= DXEPCTL_STALL | DXEPCTL_SNAK; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4136 | if (epctl & DXEPCTL_EPENA) |
| 4137 | epctl |= DXEPCTL_EPDIS; |
| 4138 | } else { |
| 4139 | epctl &= ~DXEPCTL_STALL; |
| 4140 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 4141 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4142 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 4143 | epctl |= DXEPCTL_SETD0PID; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4144 | } |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4145 | dwc2_writel(epctl, hs->regs + epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4146 | } else { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4147 | epreg = DOEPCTL(index); |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4148 | epctl = dwc2_readl(hs->regs + epreg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4149 | |
John Youn | 34c0887 | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 4150 | if (value) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4151 | epctl |= DXEPCTL_STALL; |
John Youn | 34c0887 | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 4152 | } else { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4153 | epctl &= ~DXEPCTL_STALL; |
| 4154 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 4155 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4156 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 4157 | epctl |= DXEPCTL_SETD0PID; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4158 | } |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4159 | dwc2_writel(epctl, hs->regs + epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4160 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4161 | |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 4162 | hs_ep->halted = value; |
| 4163 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4164 | return 0; |
| 4165 | } |
| 4166 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4167 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4168 | * 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] | 4169 | * @ep: The endpoint to set halt. |
| 4170 | * @value: Set or unset the halt. |
| 4171 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4172 | 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] | 4173 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4174 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4175 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4176 | unsigned long flags = 0; |
| 4177 | int ret = 0; |
| 4178 | |
| 4179 | spin_lock_irqsave(&hs->lock, flags); |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4180 | ret = dwc2_hsotg_ep_sethalt(ep, value, false); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4181 | spin_unlock_irqrestore(&hs->lock, flags); |
| 4182 | |
| 4183 | return ret; |
| 4184 | } |
| 4185 | |
Bhumika Goyal | ebce561 | 2017-08-12 17:34:55 +0530 | [diff] [blame] | 4186 | static const struct usb_ep_ops dwc2_hsotg_ep_ops = { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4187 | .enable = dwc2_hsotg_ep_enable, |
| 4188 | .disable = dwc2_hsotg_ep_disable, |
| 4189 | .alloc_request = dwc2_hsotg_ep_alloc_request, |
| 4190 | .free_request = dwc2_hsotg_ep_free_request, |
| 4191 | .queue = dwc2_hsotg_ep_queue_lock, |
| 4192 | .dequeue = dwc2_hsotg_ep_dequeue, |
| 4193 | .set_halt = dwc2_hsotg_ep_sethalt_lock, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4194 | /* note, don't believe we have any call for the fifo routines */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4195 | }; |
| 4196 | |
| 4197 | /** |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4198 | * dwc2_hsotg_init - initialize the usb core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4199 | * @hsotg: The driver state |
| 4200 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4201 | static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg) |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4202 | { |
Mian Yousaf Kaukab | fa4a8d7 | 2015-01-30 09:09:35 +0100 | [diff] [blame] | 4203 | u32 trdtim; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 4204 | u32 usbcfg; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4205 | /* unmask subset of endpoint interrupts */ |
| 4206 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4207 | dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK | |
| 4208 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK, |
| 4209 | hsotg->regs + DIEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4210 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4211 | dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK | |
| 4212 | DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK, |
| 4213 | hsotg->regs + DOEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4214 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4215 | dwc2_writel(0, hsotg->regs + DAINTMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4216 | |
| 4217 | /* Be in disconnected state until gadget is registered */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4218 | __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4219 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4220 | /* setup fifos */ |
| 4221 | |
| 4222 | dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4223 | dwc2_readl(hsotg->regs + GRXFSIZ), |
| 4224 | dwc2_readl(hsotg->regs + GNPTXFSIZ)); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4225 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4226 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4227 | |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 4228 | /* keep other bits untouched (so e.g. forced modes are not lost) */ |
| 4229 | usbcfg = dwc2_readl(hsotg->regs + GUSBCFG); |
| 4230 | usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP | |
Amelie Delaunay | ca02954 | 2017-01-12 16:09:44 +0100 | [diff] [blame] | 4231 | GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK); |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 4232 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4233 | /* 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] | 4234 | trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 4235 | usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) | |
| 4236 | (trdtim << GUSBCFG_USBTRDTIM_SHIFT); |
| 4237 | dwc2_writel(usbcfg, hsotg->regs + GUSBCFG); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4238 | |
Gregory Herrero | f509004 | 2015-01-09 13:38:47 +0100 | [diff] [blame] | 4239 | if (using_dma(hsotg)) |
| 4240 | __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4241 | } |
| 4242 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4243 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4244 | * dwc2_hsotg_udc_start - prepare the udc for work |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4245 | * @gadget: The usb gadget state |
| 4246 | * @driver: The usb gadget driver |
| 4247 | * |
| 4248 | * Perform initialization to prepare udc device and driver |
| 4249 | * to work. |
| 4250 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4251 | static int dwc2_hsotg_udc_start(struct usb_gadget *gadget, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4252 | struct usb_gadget_driver *driver) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4253 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4254 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4255 | unsigned long flags; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4256 | int ret; |
| 4257 | |
| 4258 | if (!hsotg) { |
Pavel Machek | a023da3 | 2013-09-30 14:56:02 +0200 | [diff] [blame] | 4259 | pr_err("%s: called with no device\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4260 | return -ENODEV; |
| 4261 | } |
| 4262 | |
| 4263 | if (!driver) { |
| 4264 | dev_err(hsotg->dev, "%s: no driver\n", __func__); |
| 4265 | return -EINVAL; |
| 4266 | } |
| 4267 | |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 4268 | if (driver->max_speed < USB_SPEED_FULL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4269 | dev_err(hsotg->dev, "%s: bad speed\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4270 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 4271 | if (!driver->setup) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4272 | dev_err(hsotg->dev, "%s: missing entry points\n", __func__); |
| 4273 | return -EINVAL; |
| 4274 | } |
| 4275 | |
| 4276 | WARN_ON(hsotg->driver); |
| 4277 | |
| 4278 | driver->driver.bus = NULL; |
| 4279 | hsotg->driver = driver; |
Alexandre Pereira da Silva | 7d7b229 | 2012-06-26 11:27:10 -0300 | [diff] [blame] | 4280 | hsotg->gadget.dev.of_node = hsotg->dev->of_node; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4281 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4282 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4283 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) { |
| 4284 | ret = dwc2_lowlevel_hw_enable(hsotg); |
| 4285 | if (ret) |
| 4286 | goto err; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4287 | } |
| 4288 | |
Gregory Herrero | f6c0159 | 2015-01-09 13:38:41 +0100 | [diff] [blame] | 4289 | if (!IS_ERR_OR_NULL(hsotg->uphy)) |
| 4290 | otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 4291 | |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4292 | spin_lock_irqsave(&hsotg->lock, flags); |
John Youn | d0f0ac5 | 2016-09-07 19:39:37 -0700 | [diff] [blame] | 4293 | if (dwc2_hw_is_device(hsotg)) { |
| 4294 | dwc2_hsotg_init(hsotg); |
| 4295 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
| 4296 | } |
| 4297 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4298 | hsotg->enabled = 0; |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4299 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4300 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4301 | dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4302 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4303 | return 0; |
| 4304 | |
| 4305 | err: |
| 4306 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4307 | return ret; |
| 4308 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4309 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4310 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4311 | * dwc2_hsotg_udc_stop - stop the udc |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4312 | * @gadget: The usb gadget state |
| 4313 | * @driver: The usb gadget driver |
| 4314 | * |
| 4315 | * Stop udc hw block and stay tunned for future transmissions |
| 4316 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4317 | static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4318 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4319 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4320 | unsigned long flags = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4321 | int ep; |
| 4322 | |
| 4323 | if (!hsotg) |
| 4324 | return -ENODEV; |
| 4325 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4326 | /* all endpoints should be shutdown */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4327 | for (ep = 1; ep < hsotg->num_of_eps; ep++) { |
| 4328 | if (hsotg->eps_in[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4329 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4330 | if (hsotg->eps_out[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4331 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4332 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4333 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4334 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4335 | |
Marek Szyprowski | 32805c3 | 2014-10-20 12:45:33 +0200 | [diff] [blame] | 4336 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4337 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4338 | hsotg->enabled = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4339 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4340 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4341 | |
Gregory Herrero | f6c0159 | 2015-01-09 13:38:41 +0100 | [diff] [blame] | 4342 | if (!IS_ERR_OR_NULL(hsotg->uphy)) |
| 4343 | otg_set_peripheral(hsotg->uphy->otg, NULL); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 4344 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4345 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 4346 | dwc2_lowlevel_hw_disable(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4347 | |
| 4348 | return 0; |
| 4349 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4350 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4351 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4352 | * dwc2_hsotg_gadget_getframe - read the frame number |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4353 | * @gadget: The usb gadget state |
| 4354 | * |
| 4355 | * Read the {micro} frame number |
| 4356 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4357 | static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4358 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4359 | return dwc2_hsotg_read_frameno(to_hsotg(gadget)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4360 | } |
| 4361 | |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4362 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4363 | * dwc2_hsotg_pullup - connect/disconnect the USB PHY |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4364 | * @gadget: The usb gadget state |
| 4365 | * @is_on: Current state of the USB PHY |
| 4366 | * |
| 4367 | * Connect/Disconnect the USB PHY pullup |
| 4368 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4369 | static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on) |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4370 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4371 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4372 | unsigned long flags = 0; |
| 4373 | |
Gregory Herrero | 77ba911 | 2015-09-29 12:08:19 +0200 | [diff] [blame] | 4374 | dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4375 | hsotg->op_state); |
Gregory Herrero | 77ba911 | 2015-09-29 12:08:19 +0200 | [diff] [blame] | 4376 | |
| 4377 | /* Don't modify pullup state while in host mode */ |
| 4378 | if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { |
| 4379 | hsotg->enabled = is_on; |
| 4380 | return 0; |
| 4381 | } |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4382 | |
| 4383 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4384 | if (is_on) { |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4385 | hsotg->enabled = 1; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4386 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
| 4387 | dwc2_hsotg_core_connect(hsotg); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4388 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4389 | dwc2_hsotg_core_disconnect(hsotg); |
| 4390 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4391 | hsotg->enabled = 0; |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4395 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4396 | |
| 4397 | return 0; |
| 4398 | } |
| 4399 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4400 | 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] | 4401 | { |
| 4402 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 4403 | unsigned long flags; |
| 4404 | |
| 4405 | dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active); |
| 4406 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4407 | |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4408 | /* |
| 4409 | * If controller is hibernated, it must exit from hibernation |
| 4410 | * before being initialized / de-initialized |
| 4411 | */ |
| 4412 | if (hsotg->lx_state == DWC2_L2) |
| 4413 | dwc2_exit_hibernation(hsotg, false); |
| 4414 | |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4415 | if (is_active) { |
Gregory Herrero | cd0e641 | 2015-09-29 12:08:20 +0200 | [diff] [blame] | 4416 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 4417 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4418 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4419 | if (hsotg->enabled) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4420 | dwc2_hsotg_core_connect(hsotg); |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4421 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4422 | dwc2_hsotg_core_disconnect(hsotg); |
| 4423 | dwc2_hsotg_disconnect(hsotg); |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 4430 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4431 | * dwc2_hsotg_vbus_draw - report bMaxPower field |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 4432 | * @gadget: The usb gadget state |
| 4433 | * @mA: Amount of current |
| 4434 | * |
| 4435 | * Report how much power the device may consume to the phy. |
| 4436 | */ |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4437 | static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA) |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 4438 | { |
| 4439 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 4440 | |
| 4441 | if (IS_ERR_OR_NULL(hsotg->uphy)) |
| 4442 | return -ENOTSUPP; |
| 4443 | return usb_phy_set_power(hsotg->uphy, mA); |
| 4444 | } |
| 4445 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4446 | static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = { |
| 4447 | .get_frame = dwc2_hsotg_gadget_getframe, |
| 4448 | .udc_start = dwc2_hsotg_udc_start, |
| 4449 | .udc_stop = dwc2_hsotg_udc_stop, |
| 4450 | .pullup = dwc2_hsotg_pullup, |
| 4451 | .vbus_session = dwc2_hsotg_vbus_session, |
| 4452 | .vbus_draw = dwc2_hsotg_vbus_draw, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4453 | }; |
| 4454 | |
| 4455 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4456 | * dwc2_hsotg_initep - initialise a single endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4457 | * @hsotg: The device state. |
| 4458 | * @hs_ep: The endpoint to be initialised. |
| 4459 | * @epnum: The endpoint number |
| 4460 | * |
| 4461 | * Initialise the given endpoint (as part of the probe and device state |
| 4462 | * creation) to give to the gadget driver. Setup the endpoint name, any |
| 4463 | * direction information and other state that may be required. |
| 4464 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4465 | static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4466 | struct dwc2_hsotg_ep *hs_ep, |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4467 | int epnum, |
| 4468 | bool dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4469 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4470 | char *dir; |
| 4471 | |
| 4472 | if (epnum == 0) |
| 4473 | dir = ""; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4474 | else if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4475 | dir = "in"; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4476 | else |
| 4477 | dir = "out"; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4478 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4479 | hs_ep->dir_in = dir_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4480 | hs_ep->index = epnum; |
| 4481 | |
| 4482 | snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir); |
| 4483 | |
| 4484 | INIT_LIST_HEAD(&hs_ep->queue); |
| 4485 | INIT_LIST_HEAD(&hs_ep->ep.ep_list); |
| 4486 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4487 | /* add to the list of endpoints known by the gadget driver */ |
| 4488 | if (epnum) |
| 4489 | list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list); |
| 4490 | |
| 4491 | hs_ep->parent = hsotg; |
| 4492 | hs_ep->ep.name = hs_ep->name; |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 4493 | |
| 4494 | if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW) |
| 4495 | usb_ep_set_maxpacket_limit(&hs_ep->ep, 8); |
| 4496 | else |
| 4497 | usb_ep_set_maxpacket_limit(&hs_ep->ep, |
| 4498 | epnum ? 1024 : EP0_MPS_LIMIT); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4499 | hs_ep->ep.ops = &dwc2_hsotg_ep_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4500 | |
Robert Baldyga | 2954522 | 2015-07-31 16:00:18 +0200 | [diff] [blame] | 4501 | if (epnum == 0) { |
| 4502 | hs_ep->ep.caps.type_control = true; |
| 4503 | } else { |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 4504 | if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) { |
| 4505 | hs_ep->ep.caps.type_iso = true; |
| 4506 | hs_ep->ep.caps.type_bulk = true; |
| 4507 | } |
Robert Baldyga | 2954522 | 2015-07-31 16:00:18 +0200 | [diff] [blame] | 4508 | hs_ep->ep.caps.type_int = true; |
| 4509 | } |
| 4510 | |
| 4511 | if (dir_in) |
| 4512 | hs_ep->ep.caps.dir_in = true; |
| 4513 | else |
| 4514 | hs_ep->ep.caps.dir_out = true; |
| 4515 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4516 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4517 | * 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] | 4518 | * to be something valid. |
| 4519 | */ |
| 4520 | |
| 4521 | if (using_dma(hsotg)) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4522 | u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15); |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4523 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4524 | if (dir_in) |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4525 | dwc2_writel(next, hsotg->regs + DIEPCTL(epnum)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4526 | else |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4527 | dwc2_writel(next, hsotg->regs + DOEPCTL(epnum)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4528 | } |
| 4529 | } |
| 4530 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4531 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4532 | * dwc2_hsotg_hw_cfg - read HW configuration registers |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4533 | * @param: The device state |
| 4534 | * |
| 4535 | * Read the USB core HW configuration registers |
| 4536 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4537 | static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4538 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4539 | u32 cfg; |
| 4540 | u32 ep_type; |
| 4541 | u32 i; |
| 4542 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4543 | /* check hardware configuration */ |
| 4544 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4545 | hsotg->num_of_eps = hsotg->hw_params.num_dev_ep; |
| 4546 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4547 | /* Add ep0 */ |
| 4548 | hsotg->num_of_eps++; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4549 | |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 4550 | hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, |
| 4551 | sizeof(struct dwc2_hsotg_ep), |
| 4552 | GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4553 | if (!hsotg->eps_in[0]) |
| 4554 | return -ENOMEM; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4555 | /* Same dwc2_hsotg_ep is used in both directions for ep0 */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4556 | hsotg->eps_out[0] = hsotg->eps_in[0]; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4557 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4558 | cfg = hsotg->hw_params.dev_ep_dirs; |
Roshan Pius | 251a17f | 2015-02-02 14:55:38 -0800 | [diff] [blame] | 4559 | 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] | 4560 | ep_type = cfg & 3; |
| 4561 | /* Direction in or both */ |
| 4562 | if (!(ep_type & 2)) { |
| 4563 | hsotg->eps_in[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4564 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4565 | if (!hsotg->eps_in[i]) |
| 4566 | return -ENOMEM; |
| 4567 | } |
| 4568 | /* Direction out or both */ |
| 4569 | if (!(ep_type & 1)) { |
| 4570 | hsotg->eps_out[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4571 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4572 | if (!hsotg->eps_out[i]) |
| 4573 | return -ENOMEM; |
| 4574 | } |
| 4575 | } |
| 4576 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4577 | hsotg->fifo_mem = hsotg->hw_params.total_fifo_size; |
| 4578 | hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo; |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4579 | |
Marek Szyprowski | cff9eb7 | 2014-09-09 10:44:55 +0200 | [diff] [blame] | 4580 | dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n", |
| 4581 | hsotg->num_of_eps, |
| 4582 | hsotg->dedicated_fifos ? "dedicated" : "shared", |
| 4583 | hsotg->fifo_mem); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4584 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4585 | } |
| 4586 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4587 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4588 | * dwc2_hsotg_dump - dump state of the udc |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4589 | * @param: The device state |
| 4590 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4591 | static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4592 | { |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 4593 | #ifdef DEBUG |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4594 | struct device *dev = hsotg->dev; |
| 4595 | void __iomem *regs = hsotg->regs; |
| 4596 | u32 val; |
| 4597 | int idx; |
| 4598 | |
| 4599 | 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] | 4600 | dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL), |
| 4601 | dwc2_readl(regs + DIEPMSK)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4602 | |
Mian Yousaf Kaukab | f889f23 | 2015-01-30 09:09:36 +0100 | [diff] [blame] | 4603 | dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4604 | dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4605 | |
| 4606 | dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4607 | dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4608 | |
| 4609 | /* show periodic fifo settings */ |
| 4610 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 4611 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4612 | val = dwc2_readl(regs + DPTXFSIZN(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4613 | dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4614 | val >> FIFOSIZE_DEPTH_SHIFT, |
| 4615 | val & FIFOSIZE_STARTADDR_MASK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4616 | } |
| 4617 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 4618 | for (idx = 0; idx < hsotg->num_of_eps; idx++) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4619 | dev_info(dev, |
| 4620 | "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] | 4621 | dwc2_readl(regs + DIEPCTL(idx)), |
| 4622 | dwc2_readl(regs + DIEPTSIZ(idx)), |
| 4623 | dwc2_readl(regs + DIEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4624 | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4625 | val = dwc2_readl(regs + DOEPCTL(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4626 | dev_info(dev, |
| 4627 | "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] | 4628 | idx, dwc2_readl(regs + DOEPCTL(idx)), |
| 4629 | dwc2_readl(regs + DOEPTSIZ(idx)), |
| 4630 | dwc2_readl(regs + DOEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4631 | } |
| 4632 | |
| 4633 | dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n", |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4634 | dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE)); |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 4635 | #endif |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4636 | } |
| 4637 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4638 | /** |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4639 | * dwc2_gadget_init - init function for gadget |
| 4640 | * @dwc2: The data structure for the DWC2 driver. |
| 4641 | * @irq: The IRQ number for the controller. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4642 | */ |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4643 | int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4644 | { |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4645 | struct device *dev = hsotg->dev; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4646 | int epnum; |
| 4647 | int ret; |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4648 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 4649 | /* Dump fifo information */ |
| 4650 | dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n", |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 4651 | hsotg->params.g_np_tx_fifo_size); |
| 4652 | dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 4653 | |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 4654 | hsotg->gadget.max_speed = USB_SPEED_HIGH; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4655 | hsotg->gadget.ops = &dwc2_hsotg_gadget_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4656 | hsotg->gadget.name = dev_name(dev); |
Gregory Herrero | 097ee66 | 2015-04-29 22:09:10 +0200 | [diff] [blame] | 4657 | if (hsotg->dr_mode == USB_DR_MODE_OTG) |
| 4658 | hsotg->gadget.is_otg = 1; |
Mian Yousaf Kaukab | ec4cc65 | 2015-09-22 15:16:55 +0200 | [diff] [blame] | 4659 | else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 4660 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4661 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4662 | ret = dwc2_hsotg_hw_cfg(hsotg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4663 | if (ret) { |
| 4664 | dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4665 | return ret; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4666 | } |
| 4667 | |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4668 | hsotg->ctrl_buff = devm_kzalloc(hsotg->dev, |
| 4669 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
Wolfram Sang | 8bae0f8 | 2016-08-25 19:39:02 +0200 | [diff] [blame] | 4670 | if (!hsotg->ctrl_buff) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4671 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4672 | |
| 4673 | hsotg->ep0_buff = devm_kzalloc(hsotg->dev, |
| 4674 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
Wolfram Sang | 8bae0f8 | 2016-08-25 19:39:02 +0200 | [diff] [blame] | 4675 | if (!hsotg->ep0_buff) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4676 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4677 | |
Vahram Aharonyan | 0f6b80c | 2016-11-09 19:27:56 -0800 | [diff] [blame] | 4678 | if (using_desc_dma(hsotg)) { |
| 4679 | ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg); |
| 4680 | if (ret < 0) |
| 4681 | return ret; |
| 4682 | } |
| 4683 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4684 | ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4685 | dev_name(hsotg->dev), hsotg); |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 4686 | if (ret < 0) { |
Dinh Nguyen | db8178c | 2014-11-11 11:13:37 -0600 | [diff] [blame] | 4687 | dev_err(dev, "cannot claim IRQ for gadget\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4688 | return ret; |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 4689 | } |
| 4690 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4691 | /* hsotg->num_of_eps holds number of EPs other than ep0 */ |
| 4692 | |
| 4693 | if (hsotg->num_of_eps == 0) { |
| 4694 | dev_err(dev, "wrong number of EPs (zero)\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4695 | return -EINVAL; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4696 | } |
| 4697 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4698 | /* setup endpoint information */ |
| 4699 | |
| 4700 | INIT_LIST_HEAD(&hsotg->gadget.ep_list); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4701 | hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4702 | |
| 4703 | /* allocate EP0 request */ |
| 4704 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4705 | 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] | 4706 | GFP_KERNEL); |
| 4707 | if (!hsotg->ctrl_req) { |
| 4708 | dev_err(dev, "failed to allocate ctrl req\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4709 | return -ENOMEM; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4710 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4711 | |
| 4712 | /* initialise the endpoints now the core has been initialised */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4713 | for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) { |
| 4714 | if (hsotg->eps_in[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4715 | dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4716 | epnum, 1); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4717 | if (hsotg->eps_out[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4718 | dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4719 | epnum, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4720 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4721 | |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4722 | ret = usb_add_gadget_udc(dev, &hsotg->gadget); |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 4723 | if (ret) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4724 | return ret; |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 4725 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4726 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4727 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4728 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4729 | } |
| 4730 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4731 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4732 | * dwc2_hsotg_remove - remove function for hsotg driver |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4733 | * @pdev: The platform information for the driver |
| 4734 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4735 | int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4736 | { |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 4737 | usb_del_gadget_udc(&hsotg->gadget); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 4738 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4739 | return 0; |
| 4740 | } |
| 4741 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4742 | int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4743 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4744 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4745 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4746 | if (hsotg->lx_state != DWC2_L0) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4747 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4748 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4749 | if (hsotg->driver) { |
| 4750 | int ep; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4751 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4752 | dev_info(hsotg->dev, "suspending usb gadget %s\n", |
| 4753 | hsotg->driver->driver.name); |
| 4754 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4755 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4756 | if (hsotg->enabled) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4757 | dwc2_hsotg_core_disconnect(hsotg); |
| 4758 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4759 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4760 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4761 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4762 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 4763 | if (hsotg->eps_in[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4764 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4765 | if (hsotg->eps_out[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4766 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4767 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4768 | } |
| 4769 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4770 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4771 | } |
| 4772 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4773 | int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4774 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4775 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4776 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4777 | if (hsotg->lx_state == DWC2_L2) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4778 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4779 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4780 | if (hsotg->driver) { |
| 4781 | dev_info(hsotg->dev, "resuming usb gadget %s\n", |
| 4782 | hsotg->driver->driver.name); |
Robert Baldyga | d00b414 | 2014-09-09 10:44:57 +0200 | [diff] [blame] | 4783 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4784 | spin_lock_irqsave(&hsotg->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4785 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4786 | if (hsotg->enabled) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4787 | dwc2_hsotg_core_connect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4788 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4789 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4790 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4791 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4792 | } |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4793 | |
| 4794 | /** |
| 4795 | * dwc2_backup_device_registers() - Backup controller device registers. |
| 4796 | * When suspending usb bus, registers needs to be backuped |
| 4797 | * if controller power is disabled once suspended. |
| 4798 | * |
| 4799 | * @hsotg: Programming view of the DWC_otg controller |
| 4800 | */ |
| 4801 | int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg) |
| 4802 | { |
| 4803 | struct dwc2_dregs_backup *dr; |
| 4804 | int i; |
| 4805 | |
| 4806 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 4807 | |
| 4808 | /* Backup dev regs */ |
| 4809 | dr = &hsotg->dr_backup; |
| 4810 | |
| 4811 | dr->dcfg = dwc2_readl(hsotg->regs + DCFG); |
| 4812 | dr->dctl = dwc2_readl(hsotg->regs + DCTL); |
| 4813 | dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK); |
| 4814 | dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK); |
| 4815 | dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK); |
| 4816 | |
| 4817 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 4818 | /* Backup IN EPs */ |
| 4819 | dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i)); |
| 4820 | |
| 4821 | /* Ensure DATA PID is correctly configured */ |
| 4822 | if (dr->diepctl[i] & DXEPCTL_DPID) |
| 4823 | dr->diepctl[i] |= DXEPCTL_SETD1PID; |
| 4824 | else |
| 4825 | dr->diepctl[i] |= DXEPCTL_SETD0PID; |
| 4826 | |
| 4827 | dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i)); |
| 4828 | dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i)); |
| 4829 | |
| 4830 | /* Backup OUT EPs */ |
| 4831 | dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i)); |
| 4832 | |
| 4833 | /* Ensure DATA PID is correctly configured */ |
| 4834 | if (dr->doepctl[i] & DXEPCTL_DPID) |
| 4835 | dr->doepctl[i] |= DXEPCTL_SETD1PID; |
| 4836 | else |
| 4837 | dr->doepctl[i] |= DXEPCTL_SETD0PID; |
| 4838 | |
| 4839 | dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i)); |
| 4840 | dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i)); |
| 4841 | } |
| 4842 | dr->valid = true; |
| 4843 | return 0; |
| 4844 | } |
| 4845 | |
| 4846 | /** |
| 4847 | * dwc2_restore_device_registers() - Restore controller device registers. |
| 4848 | * When resuming usb bus, device registers needs to be restored |
| 4849 | * if controller power were disabled. |
| 4850 | * |
| 4851 | * @hsotg: Programming view of the DWC_otg controller |
| 4852 | */ |
| 4853 | int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg) |
| 4854 | { |
| 4855 | struct dwc2_dregs_backup *dr; |
| 4856 | u32 dctl; |
| 4857 | int i; |
| 4858 | |
| 4859 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 4860 | |
| 4861 | /* Restore dev regs */ |
| 4862 | dr = &hsotg->dr_backup; |
| 4863 | if (!dr->valid) { |
| 4864 | dev_err(hsotg->dev, "%s: no device registers to restore\n", |
| 4865 | __func__); |
| 4866 | return -EINVAL; |
| 4867 | } |
| 4868 | dr->valid = false; |
| 4869 | |
| 4870 | dwc2_writel(dr->dcfg, hsotg->regs + DCFG); |
| 4871 | dwc2_writel(dr->dctl, hsotg->regs + DCTL); |
| 4872 | dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK); |
| 4873 | dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK); |
| 4874 | dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK); |
| 4875 | |
| 4876 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 4877 | /* Restore IN EPs */ |
| 4878 | dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i)); |
| 4879 | dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i)); |
| 4880 | dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i)); |
| 4881 | |
| 4882 | /* Restore OUT EPs */ |
| 4883 | dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i)); |
| 4884 | dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i)); |
| 4885 | dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i)); |
| 4886 | } |
| 4887 | |
| 4888 | /* Set the Power-On Programming done bit */ |
| 4889 | dctl = dwc2_readl(hsotg->regs + DCTL); |
| 4890 | dctl |= DCTL_PWRONPRGDONE; |
| 4891 | dwc2_writel(dctl, hsotg->regs + DCTL); |
| 4892 | |
| 4893 | return 0; |
| 4894 | } |