Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [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 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 50 | static inline void dwc2_set_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 51 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 52 | dwc2_writel(hsotg, dwc2_readl(hsotg, offset) | val, offset); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 55 | static inline void dwc2_clear_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 56 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 57 | dwc2_writel(hsotg, dwc2_readl(hsotg, offset) & ~val, offset); |
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 |
Vardan Mikayelyan | 92d1635 | 2016-05-25 18:07:05 -0700 | [diff] [blame] | 110 | * |
| 111 | * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT. |
| 112 | * If an overrun occurs it will wrap the value and set the frame_overrun flag. |
| 113 | */ |
| 114 | static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep) |
| 115 | { |
| 116 | hs_ep->target_frame += hs_ep->interval; |
| 117 | if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) { |
Gustavo A. R. Silva | c1d5df6 | 2018-01-23 09:45:31 -0600 | [diff] [blame] | 118 | hs_ep->frame_overrun = true; |
Vardan Mikayelyan | 92d1635 | 2016-05-25 18:07:05 -0700 | [diff] [blame] | 119 | hs_ep->target_frame &= DSTS_SOFFN_LIMIT; |
| 120 | } else { |
Gustavo A. R. Silva | c1d5df6 | 2018-01-23 09:45:31 -0600 | [diff] [blame] | 121 | hs_ep->frame_overrun = false; |
Vardan Mikayelyan | 92d1635 | 2016-05-25 18:07:05 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
Grigor Tovmasyan | 9d630b9 | 2018-08-29 21:00:03 +0400 | [diff] [blame] | 126 | * dwc2_gadget_dec_frame_num_by_one - Decrements the targeted frame number |
| 127 | * by one. |
| 128 | * @hs_ep: The endpoint. |
| 129 | * |
| 130 | * This function used in service interval based scheduling flow to calculate |
| 131 | * descriptor frame number filed value. For service interval mode frame |
| 132 | * number in descriptor should point to last (u)frame in the interval. |
| 133 | * |
| 134 | */ |
| 135 | static inline void dwc2_gadget_dec_frame_num_by_one(struct dwc2_hsotg_ep *hs_ep) |
| 136 | { |
| 137 | if (hs_ep->target_frame) |
| 138 | hs_ep->target_frame -= 1; |
| 139 | else |
| 140 | hs_ep->target_frame = DSTS_SOFFN_LIMIT; |
| 141 | } |
| 142 | |
| 143 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 144 | * dwc2_hsotg_en_gsint - enable one or more of the general interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 145 | * @hsotg: The device state |
| 146 | * @ints: A bitmask of the interrupts to enable |
| 147 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 148 | static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 149 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 150 | u32 gsintmsk = dwc2_readl(hsotg, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 151 | u32 new_gsintmsk; |
| 152 | |
| 153 | new_gsintmsk = gsintmsk | ints; |
| 154 | |
| 155 | if (new_gsintmsk != gsintmsk) { |
| 156 | dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 157 | dwc2_writel(hsotg, new_gsintmsk, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 162 | * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 163 | * @hsotg: The device state |
| 164 | * @ints: A bitmask of the interrupts to enable |
| 165 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 166 | static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 167 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 168 | u32 gsintmsk = dwc2_readl(hsotg, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 169 | u32 new_gsintmsk; |
| 170 | |
| 171 | new_gsintmsk = gsintmsk & ~ints; |
| 172 | |
| 173 | if (new_gsintmsk != gsintmsk) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 174 | dwc2_writel(hsotg, new_gsintmsk, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 178 | * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 179 | * @hsotg: The device state |
| 180 | * @ep: The endpoint index |
| 181 | * @dir_in: True if direction is in. |
| 182 | * @en: The enable value, true to enable |
| 183 | * |
| 184 | * Set or clear the mask for an individual endpoint's interrupt |
| 185 | * request. |
| 186 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 187 | static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 188 | unsigned int ep, unsigned int dir_in, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 189 | unsigned int en) |
| 190 | { |
| 191 | unsigned long flags; |
| 192 | u32 bit = 1 << ep; |
| 193 | u32 daint; |
| 194 | |
| 195 | if (!dir_in) |
| 196 | bit <<= 16; |
| 197 | |
| 198 | local_irq_save(flags); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 199 | daint = dwc2_readl(hsotg, DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 200 | if (en) |
| 201 | daint |= bit; |
| 202 | else |
| 203 | daint &= ~bit; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 204 | dwc2_writel(hsotg, daint, DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 205 | local_irq_restore(flags); |
| 206 | } |
| 207 | |
| 208 | /** |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 209 | * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 210 | * |
| 211 | * @hsotg: Programming view of the DWC_otg controller |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 212 | */ |
| 213 | int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg) |
| 214 | { |
| 215 | if (hsotg->hw_params.en_multiple_tx_fifo) |
| 216 | /* In dedicated FIFO mode we need count of IN EPs */ |
Minas Harutyunyan | 9273083 | 2017-11-30 12:16:37 +0400 | [diff] [blame] | 217 | return hsotg->hw_params.num_dev_in_eps; |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 218 | else |
| 219 | /* In shared FIFO mode we need count of Periodic IN EPs */ |
| 220 | return hsotg->hw_params.num_dev_perio_in_ep; |
| 221 | } |
| 222 | |
| 223 | /** |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 224 | * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for |
| 225 | * device mode TX FIFOs |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 226 | * |
| 227 | * @hsotg: Programming view of the DWC_otg controller |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 228 | */ |
| 229 | int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg) |
| 230 | { |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 231 | int addr; |
| 232 | int tx_addr_max; |
| 233 | u32 np_tx_fifo_size; |
| 234 | |
| 235 | np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size, |
| 236 | hsotg->params.g_np_tx_fifo_size); |
| 237 | |
| 238 | /* Get Endpoint Info Control block size in DWORDs. */ |
Minas Harutyunyan | 9273083 | 2017-11-30 12:16:37 +0400 | [diff] [blame] | 239 | tx_addr_max = hsotg->hw_params.total_fifo_size; |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 240 | |
| 241 | addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size; |
| 242 | if (tx_addr_max <= addr) |
| 243 | return 0; |
| 244 | |
| 245 | return tx_addr_max - addr; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode |
| 250 | * TX FIFOs |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 251 | * |
| 252 | * @hsotg: Programming view of the DWC_otg controller |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 253 | */ |
| 254 | int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg) |
| 255 | { |
| 256 | int tx_fifo_count; |
| 257 | int tx_fifo_depth; |
| 258 | |
| 259 | tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg); |
| 260 | |
| 261 | tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg); |
| 262 | |
| 263 | if (!tx_fifo_count) |
| 264 | return tx_fifo_depth; |
| 265 | else |
| 266 | return tx_fifo_depth / tx_fifo_count; |
| 267 | } |
| 268 | |
| 269 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 270 | * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 271 | * @hsotg: The device instance. |
| 272 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 273 | static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 274 | { |
John Youn | 2317eac | 2016-10-17 17:36:23 -0700 | [diff] [blame] | 275 | unsigned int ep; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 276 | unsigned int addr; |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 277 | int timeout; |
Sevak Arakelyan | 79d6b8c | 2018-01-19 14:39:31 +0400 | [diff] [blame] | 278 | |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 279 | u32 val; |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 280 | u32 *txfsz = hsotg->params.g_tx_fifo_size; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 281 | |
Gregory Herrero | 7fcbc95 | 2015-01-09 13:39:06 +0100 | [diff] [blame] | 282 | /* Reset fifo map if not correctly cleared during previous session */ |
| 283 | WARN_ON(hsotg->fifo_map); |
| 284 | hsotg->fifo_map = 0; |
| 285 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 286 | /* set RX/NPTX FIFO sizes */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 287 | dwc2_writel(hsotg, hsotg->params.g_rx_fifo_size, GRXFSIZ); |
| 288 | dwc2_writel(hsotg, (hsotg->params.g_rx_fifo_size << |
| 289 | FIFOSIZE_STARTADDR_SHIFT) | |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 290 | (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT), |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 291 | GNPTXFSIZ); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 292 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 293 | /* |
| 294 | * 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] | 295 | * block have overlapping default addresses. This also ensures |
| 296 | * that if the settings have been changed, then they are set to |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 297 | * known values. |
| 298 | */ |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 299 | |
| 300 | /* start at the end of the GNPTXFSIZ, rounded up */ |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 301 | 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] | 302 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 303 | /* |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 304 | * Configure fifos sizes from provided configuration and assign |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 305 | * them to endpoints dynamically according to maxpacket size value of |
| 306 | * given endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 307 | */ |
John Youn | 2317eac | 2016-10-17 17:36:23 -0700 | [diff] [blame] | 308 | for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) { |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 309 | if (!txfsz[ep]) |
John Youn | 3fa9538 | 2016-10-17 17:36:25 -0700 | [diff] [blame] | 310 | continue; |
| 311 | val = addr; |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 312 | val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT; |
| 313 | WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem, |
John Youn | 3fa9538 | 2016-10-17 17:36:25 -0700 | [diff] [blame] | 314 | "insufficient fifo memory"); |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 315 | addr += txfsz[ep]; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 316 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 317 | dwc2_writel(hsotg, val, DPTXFSIZN(ep)); |
| 318 | val = dwc2_readl(hsotg, DPTXFSIZN(ep)); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 319 | } |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 320 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 321 | dwc2_writel(hsotg, hsotg->hw_params.total_fifo_size | |
Sevak Arakelyan | f87c842 | 2017-01-18 18:34:19 -0800 | [diff] [blame] | 322 | addr << GDFIFOCFG_EPINFOBASE_SHIFT, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 323 | GDFIFOCFG); |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 324 | /* |
| 325 | * according to p428 of the design guide, we need to ensure that |
| 326 | * all fifos are flushed before continuing |
| 327 | */ |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 328 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 329 | dwc2_writel(hsotg, GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH | |
| 330 | GRSTCTL_RXFFLSH, GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 331 | |
| 332 | /* wait until the fifos are both flushed */ |
| 333 | timeout = 100; |
| 334 | while (1) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 335 | val = dwc2_readl(hsotg, GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 336 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 337 | if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0) |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 338 | break; |
| 339 | |
| 340 | if (--timeout == 0) { |
| 341 | dev_err(hsotg->dev, |
| 342 | "%s: timeout flushing fifos (GRSTCTL=%08x)\n", |
| 343 | __func__, val); |
Gregory Herrero | 48b20bc | 2015-01-09 13:39:01 +0100 | [diff] [blame] | 344 | break; |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | udelay(1); |
| 348 | } |
| 349 | |
| 350 | dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 354 | * dwc2_hsotg_ep_alloc_request - allocate USB rerequest structure |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 355 | * @ep: USB endpoint to allocate request for. |
| 356 | * @flags: Allocation flags |
| 357 | * |
| 358 | * Allocate a new USB request structure appropriate for the specified endpoint |
| 359 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 360 | 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] | 361 | gfp_t flags) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 362 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 363 | struct dwc2_hsotg_req *req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 364 | |
John Youn | ec33efe | 2017-01-17 20:32:41 -0800 | [diff] [blame] | 365 | req = kzalloc(sizeof(*req), flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 366 | if (!req) |
| 367 | return NULL; |
| 368 | |
| 369 | INIT_LIST_HEAD(&req->queue); |
| 370 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 371 | return &req->req; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * is_ep_periodic - return true if the endpoint is in periodic mode. |
| 376 | * @hs_ep: The endpoint to query. |
| 377 | * |
| 378 | * Returns true if the endpoint is in periodic mode, meaning it is being |
| 379 | * used for an Interrupt or ISO transfer. |
| 380 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 381 | static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 382 | { |
| 383 | return hs_ep->periodic; |
| 384 | } |
| 385 | |
| 386 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 387 | * 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] | 388 | * @hsotg: The device state. |
| 389 | * @hs_ep: The endpoint for the request |
| 390 | * @hs_req: The request being processed. |
| 391 | * |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 392 | * 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] | 393 | * 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] | 394 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 395 | static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 396 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 397 | struct dwc2_hsotg_req *hs_req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 398 | { |
| 399 | struct usb_request *req = &hs_req->req; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 400 | |
Jingoo Han | 17d966a | 2013-05-11 21:14:00 +0900 | [diff] [blame] | 401 | usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Vahram Aharonyan | 0f6b80c | 2016-11-09 19:27:56 -0800 | [diff] [blame] | 404 | /* |
| 405 | * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains |
| 406 | * for Control endpoint |
| 407 | * @hsotg: The device state. |
| 408 | * |
| 409 | * This function will allocate 4 descriptor chains for EP 0: 2 for |
| 410 | * Setup stage, per one for IN and OUT data/status transactions. |
| 411 | */ |
| 412 | static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg) |
| 413 | { |
| 414 | hsotg->setup_desc[0] = |
| 415 | dmam_alloc_coherent(hsotg->dev, |
| 416 | sizeof(struct dwc2_dma_desc), |
| 417 | &hsotg->setup_desc_dma[0], |
| 418 | GFP_KERNEL); |
| 419 | if (!hsotg->setup_desc[0]) |
| 420 | goto fail; |
| 421 | |
| 422 | hsotg->setup_desc[1] = |
| 423 | dmam_alloc_coherent(hsotg->dev, |
| 424 | sizeof(struct dwc2_dma_desc), |
| 425 | &hsotg->setup_desc_dma[1], |
| 426 | GFP_KERNEL); |
| 427 | if (!hsotg->setup_desc[1]) |
| 428 | goto fail; |
| 429 | |
| 430 | hsotg->ctrl_in_desc = |
| 431 | dmam_alloc_coherent(hsotg->dev, |
| 432 | sizeof(struct dwc2_dma_desc), |
| 433 | &hsotg->ctrl_in_desc_dma, |
| 434 | GFP_KERNEL); |
| 435 | if (!hsotg->ctrl_in_desc) |
| 436 | goto fail; |
| 437 | |
| 438 | hsotg->ctrl_out_desc = |
| 439 | dmam_alloc_coherent(hsotg->dev, |
| 440 | sizeof(struct dwc2_dma_desc), |
| 441 | &hsotg->ctrl_out_desc_dma, |
| 442 | GFP_KERNEL); |
| 443 | if (!hsotg->ctrl_out_desc) |
| 444 | goto fail; |
| 445 | |
| 446 | return 0; |
| 447 | |
| 448 | fail: |
| 449 | return -ENOMEM; |
| 450 | } |
| 451 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 452 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 453 | * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 454 | * @hsotg: The controller state. |
| 455 | * @hs_ep: The endpoint we're going to write for. |
| 456 | * @hs_req: The request to write data for. |
| 457 | * |
| 458 | * This is called when the TxFIFO has some space in it to hold a new |
| 459 | * transmission and we have something to give it. The actual setup of |
| 460 | * the data size is done elsewhere, so all we have to do is to actually |
| 461 | * write the data. |
| 462 | * |
| 463 | * The return value is zero if there is more space (or nothing was done) |
| 464 | * otherwise -ENOSPC is returned if the FIFO space was used up. |
| 465 | * |
| 466 | * This routine is only needed for PIO |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 467 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 468 | static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 469 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 470 | struct dwc2_hsotg_req *hs_req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 471 | { |
| 472 | bool periodic = is_ep_periodic(hs_ep); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 473 | u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 474 | int buf_pos = hs_req->req.actual; |
| 475 | int to_write = hs_ep->size_loaded; |
| 476 | void *data; |
| 477 | int can_write; |
| 478 | int pkt_round; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 479 | int max_transfer; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 480 | |
| 481 | to_write -= (buf_pos - hs_ep->last_load); |
| 482 | |
| 483 | /* if there's nothing to write, get out early */ |
| 484 | if (to_write == 0) |
| 485 | return 0; |
| 486 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 487 | if (periodic && !hsotg->dedicated_fifos) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 488 | u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 489 | int size_left; |
| 490 | int size_done; |
| 491 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 492 | /* |
| 493 | * work out how much data was loaded so we can calculate |
| 494 | * how much data is left in the fifo. |
| 495 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 496 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 497 | size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 498 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 499 | /* |
| 500 | * if shared fifo, we cannot write anything until the |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 501 | * previous data has been completely sent. |
| 502 | */ |
| 503 | if (hs_ep->fifo_load != 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 504 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 505 | return -ENOSPC; |
| 506 | } |
| 507 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 508 | dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n", |
| 509 | __func__, size_left, |
| 510 | hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size); |
| 511 | |
| 512 | /* how much of the data has moved */ |
| 513 | size_done = hs_ep->size_loaded - size_left; |
| 514 | |
| 515 | /* how much data is left in the fifo */ |
| 516 | can_write = hs_ep->fifo_load - size_done; |
| 517 | dev_dbg(hsotg->dev, "%s: => can_write1=%d\n", |
| 518 | __func__, can_write); |
| 519 | |
| 520 | can_write = hs_ep->fifo_size - can_write; |
| 521 | dev_dbg(hsotg->dev, "%s: => can_write2=%d\n", |
| 522 | __func__, can_write); |
| 523 | |
| 524 | if (can_write <= 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 525 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 526 | return -ENOSPC; |
| 527 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 528 | } else if (hsotg->dedicated_fifos && hs_ep->index != 0) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 529 | can_write = dwc2_readl(hsotg, |
| 530 | DTXFSTS(hs_ep->fifo_index)); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 531 | |
| 532 | can_write &= 0xffff; |
| 533 | can_write *= 4; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 534 | } else { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 535 | if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 536 | dev_dbg(hsotg->dev, |
| 537 | "%s: no queue slots available (0x%08x)\n", |
| 538 | __func__, gnptxsts); |
| 539 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 540 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 541 | return -ENOSPC; |
| 542 | } |
| 543 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 544 | can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts); |
Ben Dooks | 679f9b7 | 2010-07-19 09:40:41 +0100 | [diff] [blame] | 545 | can_write *= 4; /* fifo size is in 32bit quantities. */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 548 | max_transfer = hs_ep->ep.maxpacket * hs_ep->mc; |
| 549 | |
| 550 | 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] | 551 | __func__, gnptxsts, can_write, to_write, max_transfer); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 552 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 553 | /* |
| 554 | * 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] | 555 | * FIFO, requests of >512 cause the endpoint to get stuck with a |
| 556 | * fragment of the end of the transfer in it. |
| 557 | */ |
Robert Baldyga | 811f330 | 2013-09-24 11:24:28 +0200 | [diff] [blame] | 558 | if (can_write > 512 && !periodic) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 559 | can_write = 512; |
| 560 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 561 | /* |
| 562 | * 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] | 563 | * 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] | 564 | * doing it. |
| 565 | */ |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 566 | if (to_write > max_transfer) { |
| 567 | to_write = max_transfer; |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 568 | |
Robert Baldyga | 5cb2ff0 | 2013-09-19 11:50:18 +0200 | [diff] [blame] | 569 | /* it's needed only when we do not use dedicated fifos */ |
| 570 | if (!hsotg->dedicated_fifos) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 571 | dwc2_hsotg_en_gsint(hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 572 | periodic ? GINTSTS_PTXFEMP : |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 573 | GINTSTS_NPTXFEMP); |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 574 | } |
| 575 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 576 | /* see if we can write data */ |
| 577 | |
| 578 | if (to_write > can_write) { |
| 579 | to_write = can_write; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 580 | pkt_round = to_write % max_transfer; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 581 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 582 | /* |
| 583 | * Round the write down to an |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 584 | * exact number of packets. |
| 585 | * |
| 586 | * Note, we do not currently check to see if we can ever |
| 587 | * write a full packet or not to the FIFO. |
| 588 | */ |
| 589 | |
| 590 | if (pkt_round) |
| 591 | to_write -= pkt_round; |
| 592 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 593 | /* |
| 594 | * enable correct FIFO interrupt to alert us when there |
| 595 | * is more room left. |
| 596 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 597 | |
Robert Baldyga | 5cb2ff0 | 2013-09-19 11:50:18 +0200 | [diff] [blame] | 598 | /* it's needed only when we do not use dedicated fifos */ |
| 599 | if (!hsotg->dedicated_fifos) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 600 | dwc2_hsotg_en_gsint(hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 601 | periodic ? GINTSTS_PTXFEMP : |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 602 | GINTSTS_NPTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | 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] | 606 | to_write, hs_req->req.length, can_write, buf_pos); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 607 | |
| 608 | if (to_write <= 0) |
| 609 | return -ENOSPC; |
| 610 | |
| 611 | hs_req->req.actual = buf_pos + to_write; |
| 612 | hs_ep->total_data += to_write; |
| 613 | |
| 614 | if (periodic) |
| 615 | hs_ep->fifo_load += to_write; |
| 616 | |
| 617 | to_write = DIV_ROUND_UP(to_write, 4); |
| 618 | data = hs_req->req.buf + buf_pos; |
| 619 | |
Gevorg Sahakyan | 342ccce | 2018-07-26 18:00:41 +0400 | [diff] [blame] | 620 | dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 621 | |
| 622 | return (to_write >= can_write) ? -ENOSPC : 0; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * get_ep_limit - get the maximum data legnth for this endpoint |
| 627 | * @hs_ep: The endpoint |
| 628 | * |
| 629 | * Return the maximum data that can be queued in one go on a given endpoint |
| 630 | * so that transfers that are too long can be split. |
| 631 | */ |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 632 | static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 633 | { |
| 634 | int index = hs_ep->index; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 635 | unsigned int maxsize; |
| 636 | unsigned int maxpkt; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 637 | |
| 638 | if (index != 0) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 639 | maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1; |
| 640 | maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 641 | } else { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 642 | maxsize = 64 + 64; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 643 | if (hs_ep->dir_in) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 644 | maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 645 | else |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 646 | maxpkt = 2; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /* we made the constant loading easier above by using +1 */ |
| 650 | maxpkt--; |
| 651 | maxsize--; |
| 652 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 653 | /* |
| 654 | * constrain by packet count if maxpkts*pktsize is greater |
| 655 | * than the length register size. |
| 656 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 657 | |
| 658 | if ((maxpkt * hs_ep->ep.maxpacket) < maxsize) |
| 659 | maxsize = maxpkt * hs_ep->ep.maxpacket; |
| 660 | |
| 661 | return maxsize; |
| 662 | } |
| 663 | |
| 664 | /** |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 665 | * dwc2_hsotg_read_frameno - read current frame number |
| 666 | * @hsotg: The device instance |
| 667 | * |
| 668 | * Return the current frame number |
| 669 | */ |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 670 | static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg) |
| 671 | { |
| 672 | u32 dsts; |
| 673 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 674 | dsts = dwc2_readl(hsotg, DSTS); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 675 | dsts &= DSTS_SOFFN_MASK; |
| 676 | dsts >>= DSTS_SOFFN_SHIFT; |
| 677 | |
| 678 | return dsts; |
| 679 | } |
| 680 | |
| 681 | /** |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 682 | * dwc2_gadget_get_chain_limit - get the maximum data payload value of the |
| 683 | * DMA descriptor chain prepared for specific endpoint |
| 684 | * @hs_ep: The endpoint |
| 685 | * |
| 686 | * Return the maximum data that can be queued in one go on a given endpoint |
| 687 | * depending on its descriptor chain capacity so that transfers that |
| 688 | * are too long can be split. |
| 689 | */ |
| 690 | static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep) |
| 691 | { |
| 692 | int is_isoc = hs_ep->isochronous; |
| 693 | unsigned int maxsize; |
| 694 | |
| 695 | if (is_isoc) |
| 696 | maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT : |
| 697 | DEV_DMA_ISOC_RX_NBYTES_LIMIT; |
| 698 | else |
| 699 | maxsize = DEV_DMA_NBYTES_LIMIT; |
| 700 | |
| 701 | /* Above size of one descriptor was chosen, multiple it */ |
| 702 | maxsize *= MAX_DMA_DESC_NUM_GENERIC; |
| 703 | |
| 704 | return maxsize; |
| 705 | } |
| 706 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 707 | /* |
| 708 | * dwc2_gadget_get_desc_params - get DMA descriptor parameters. |
| 709 | * @hs_ep: The endpoint |
| 710 | * @mask: RX/TX bytes mask to be defined |
| 711 | * |
| 712 | * Returns maximum data payload for one descriptor after analyzing endpoint |
| 713 | * characteristics. |
| 714 | * DMA descriptor transfer bytes limit depends on EP type: |
| 715 | * Control out - MPS, |
| 716 | * Isochronous - descriptor rx/tx bytes bitfield limit, |
| 717 | * Control In/Bulk/Interrupt - multiple of mps. This will allow to not |
| 718 | * have concatenations from various descriptors within one packet. |
| 719 | * |
| 720 | * Selects corresponding mask for RX/TX bytes as well. |
| 721 | */ |
| 722 | static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask) |
| 723 | { |
| 724 | u32 mps = hs_ep->ep.maxpacket; |
| 725 | int dir_in = hs_ep->dir_in; |
| 726 | u32 desc_size = 0; |
| 727 | |
| 728 | if (!hs_ep->index && !dir_in) { |
| 729 | desc_size = mps; |
| 730 | *mask = DEV_DMA_NBYTES_MASK; |
| 731 | } else if (hs_ep->isochronous) { |
| 732 | if (dir_in) { |
| 733 | desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT; |
| 734 | *mask = DEV_DMA_ISOC_TX_NBYTES_MASK; |
| 735 | } else { |
| 736 | desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT; |
| 737 | *mask = DEV_DMA_ISOC_RX_NBYTES_MASK; |
| 738 | } |
| 739 | } else { |
| 740 | desc_size = DEV_DMA_NBYTES_LIMIT; |
| 741 | *mask = DEV_DMA_NBYTES_MASK; |
| 742 | |
| 743 | /* Round down desc_size to be mps multiple */ |
| 744 | desc_size -= desc_size % mps; |
| 745 | } |
| 746 | |
| 747 | return desc_size; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain. |
| 752 | * @hs_ep: The endpoint |
| 753 | * @dma_buff: DMA address to use |
| 754 | * @len: Length of the transfer |
| 755 | * |
| 756 | * This function will iterate over descriptor chain and fill its entries |
| 757 | * with corresponding information based on transfer data. |
| 758 | */ |
| 759 | static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep, |
| 760 | dma_addr_t dma_buff, |
| 761 | unsigned int len) |
| 762 | { |
| 763 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 764 | int dir_in = hs_ep->dir_in; |
| 765 | struct dwc2_dma_desc *desc = hs_ep->desc_list; |
| 766 | u32 mps = hs_ep->ep.maxpacket; |
| 767 | u32 maxsize = 0; |
| 768 | u32 offset = 0; |
| 769 | u32 mask = 0; |
| 770 | int i; |
| 771 | |
| 772 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
| 773 | |
| 774 | hs_ep->desc_count = (len / maxsize) + |
| 775 | ((len % maxsize) ? 1 : 0); |
| 776 | if (len == 0) |
| 777 | hs_ep->desc_count = 1; |
| 778 | |
| 779 | for (i = 0; i < hs_ep->desc_count; ++i) { |
| 780 | desc->status = 0; |
| 781 | desc->status |= (DEV_DMA_BUFF_STS_HBUSY |
| 782 | << DEV_DMA_BUFF_STS_SHIFT); |
| 783 | |
| 784 | if (len > maxsize) { |
| 785 | if (!hs_ep->index && !dir_in) |
| 786 | desc->status |= (DEV_DMA_L | DEV_DMA_IOC); |
| 787 | |
| 788 | desc->status |= (maxsize << |
| 789 | DEV_DMA_NBYTES_SHIFT & mask); |
| 790 | desc->buf = dma_buff + offset; |
| 791 | |
| 792 | len -= maxsize; |
| 793 | offset += maxsize; |
| 794 | } else { |
| 795 | desc->status |= (DEV_DMA_L | DEV_DMA_IOC); |
| 796 | |
| 797 | if (dir_in) |
| 798 | desc->status |= (len % mps) ? DEV_DMA_SHORT : |
| 799 | ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0); |
| 800 | if (len > maxsize) |
| 801 | dev_err(hsotg->dev, "wrong len %d\n", len); |
| 802 | |
| 803 | desc->status |= |
| 804 | len << DEV_DMA_NBYTES_SHIFT & mask; |
| 805 | desc->buf = dma_buff + offset; |
| 806 | } |
| 807 | |
| 808 | desc->status &= ~DEV_DMA_BUFF_STS_MASK; |
| 809 | desc->status |= (DEV_DMA_BUFF_STS_HREADY |
| 810 | << DEV_DMA_BUFF_STS_SHIFT); |
| 811 | desc++; |
| 812 | } |
| 813 | } |
| 814 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 815 | /* |
| 816 | * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain. |
| 817 | * @hs_ep: The isochronous endpoint. |
| 818 | * @dma_buff: usb requests dma buffer. |
| 819 | * @len: usb request transfer length. |
| 820 | * |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 821 | * Fills next free descriptor with the data of the arrived usb request, |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 822 | * frame info, sets Last and IOC bits increments next_desc. If filled |
| 823 | * descriptor is not the first one, removes L bit from the previous descriptor |
| 824 | * status. |
| 825 | */ |
| 826 | static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep, |
| 827 | dma_addr_t dma_buff, unsigned int len) |
| 828 | { |
| 829 | struct dwc2_dma_desc *desc; |
| 830 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 831 | u32 index; |
| 832 | u32 maxsize = 0; |
| 833 | u32 mask = 0; |
Minas Harutyunyan | 1d8e5c0 | 2018-05-23 16:24:44 +0400 | [diff] [blame] | 834 | u8 pid = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 835 | |
| 836 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 837 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 838 | index = hs_ep->next_desc; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 839 | desc = &hs_ep->desc_list[index]; |
| 840 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 841 | /* Check if descriptor chain full */ |
| 842 | if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) == |
| 843 | DEV_DMA_BUFF_STS_HREADY) { |
| 844 | dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__); |
| 845 | return 1; |
| 846 | } |
| 847 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 848 | /* Clear L bit of previous desc if more than one entries in the chain */ |
| 849 | if (hs_ep->next_desc) |
| 850 | hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L; |
| 851 | |
| 852 | dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n", |
| 853 | __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index); |
| 854 | |
| 855 | desc->status = 0; |
| 856 | desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT); |
| 857 | |
| 858 | desc->buf = dma_buff; |
| 859 | desc->status |= (DEV_DMA_L | DEV_DMA_IOC | |
| 860 | ((len << DEV_DMA_NBYTES_SHIFT) & mask)); |
| 861 | |
| 862 | if (hs_ep->dir_in) { |
Minas Harutyunyan | 1d8e5c0 | 2018-05-23 16:24:44 +0400 | [diff] [blame] | 863 | if (len) |
| 864 | pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket); |
| 865 | else |
| 866 | pid = 1; |
| 867 | desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) & |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 868 | DEV_DMA_ISOC_PID_MASK) | |
| 869 | ((len % hs_ep->ep.maxpacket) ? |
| 870 | DEV_DMA_SHORT : 0) | |
| 871 | ((hs_ep->target_frame << |
| 872 | DEV_DMA_ISOC_FRNUM_SHIFT) & |
| 873 | DEV_DMA_ISOC_FRNUM_MASK); |
| 874 | } |
| 875 | |
| 876 | desc->status &= ~DEV_DMA_BUFF_STS_MASK; |
| 877 | desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT); |
| 878 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 879 | /* Increment frame number by interval for IN */ |
| 880 | if (hs_ep->dir_in) |
| 881 | dwc2_gadget_incr_frame_num(hs_ep); |
| 882 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 883 | /* Update index of last configured entry in the chain */ |
| 884 | hs_ep->next_desc++; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 885 | if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_GENERIC) |
| 886 | hs_ep->next_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 887 | |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | /* |
| 892 | * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA |
| 893 | * @hs_ep: The isochronous endpoint. |
| 894 | * |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 895 | * Prepare descriptor chain for isochronous endpoints. Afterwards |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 896 | * write DMA address to HW and enable the endpoint. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 897 | */ |
| 898 | static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 899 | { |
| 900 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 901 | struct dwc2_hsotg_req *hs_req, *treq; |
| 902 | int index = hs_ep->index; |
| 903 | int ret; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 904 | int i; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 905 | u32 dma_reg; |
| 906 | u32 depctl; |
| 907 | u32 ctrl; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 908 | struct dwc2_dma_desc *desc; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 909 | |
| 910 | if (list_empty(&hs_ep->queue)) { |
Minas Harutyunyan | 1ffba90 | 2018-06-12 12:37:29 +0400 | [diff] [blame] | 911 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 912 | dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__); |
| 913 | return; |
| 914 | } |
| 915 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 916 | /* Initialize descriptor chain by Host Busy status */ |
| 917 | for (i = 0; i < MAX_DMA_DESC_NUM_GENERIC; i++) { |
| 918 | desc = &hs_ep->desc_list[i]; |
| 919 | desc->status = 0; |
| 920 | desc->status |= (DEV_DMA_BUFF_STS_HBUSY |
| 921 | << DEV_DMA_BUFF_STS_SHIFT); |
| 922 | } |
| 923 | |
| 924 | hs_ep->next_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 925 | list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) { |
| 926 | ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma, |
| 927 | hs_req->req.length); |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 928 | if (ret) |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 929 | break; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 932 | hs_ep->compl_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 933 | depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 934 | dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index); |
| 935 | |
| 936 | /* write descriptor chain address to control register */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 937 | dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 938 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 939 | ctrl = dwc2_readl(hsotg, depctl); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 940 | ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 941 | dwc2_writel(hsotg, ctrl, depctl); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 944 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 945 | * 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] | 946 | * @hsotg: The controller state. |
| 947 | * @hs_ep: The endpoint to process a request for |
| 948 | * @hs_req: The request to start. |
| 949 | * @continuing: True if we are doing more for the current request. |
| 950 | * |
| 951 | * Start the given request running by setting the endpoint registers |
| 952 | * appropriately, and writing any data to the FIFOs. |
| 953 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 954 | static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 955 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 956 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 957 | bool continuing) |
| 958 | { |
| 959 | struct usb_request *ureq = &hs_req->req; |
| 960 | int index = hs_ep->index; |
| 961 | int dir_in = hs_ep->dir_in; |
| 962 | u32 epctrl_reg; |
| 963 | u32 epsize_reg; |
| 964 | u32 epsize; |
| 965 | u32 ctrl; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 966 | unsigned int length; |
| 967 | unsigned int packets; |
| 968 | unsigned int maxreq; |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 969 | unsigned int dma_reg; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 970 | |
| 971 | if (index != 0) { |
| 972 | if (hs_ep->req && !continuing) { |
| 973 | dev_err(hsotg->dev, "%s: active request\n", __func__); |
| 974 | WARN_ON(1); |
| 975 | return; |
| 976 | } else if (hs_ep->req != hs_req && continuing) { |
| 977 | dev_err(hsotg->dev, |
| 978 | "%s: continue different req\n", __func__); |
| 979 | WARN_ON(1); |
| 980 | return; |
| 981 | } |
| 982 | } |
| 983 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 984 | dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 985 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 986 | epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 987 | |
| 988 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 989 | __func__, dwc2_readl(hsotg, epctrl_reg), index, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 990 | hs_ep->dir_in ? "in" : "out"); |
| 991 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 992 | /* If endpoint is stalled, we will restart request later */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 993 | ctrl = dwc2_readl(hsotg, epctrl_reg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 994 | |
Mian Yousaf Kaukab | b2d4c54 | 2015-09-29 12:08:22 +0200 | [diff] [blame] | 995 | if (index && ctrl & DXEPCTL_STALL) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 996 | dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index); |
| 997 | return; |
| 998 | } |
| 999 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1000 | length = ureq->length - ureq->actual; |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1001 | dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n", |
| 1002 | ureq->length, ureq->actual); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1003 | |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 1004 | if (!using_desc_dma(hsotg)) |
| 1005 | maxreq = get_ep_limit(hs_ep); |
| 1006 | else |
| 1007 | maxreq = dwc2_gadget_get_chain_limit(hs_ep); |
| 1008 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1009 | if (length > maxreq) { |
| 1010 | int round = maxreq % hs_ep->ep.maxpacket; |
| 1011 | |
| 1012 | dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n", |
| 1013 | __func__, length, maxreq, round); |
| 1014 | |
| 1015 | /* round down to multiple of packets */ |
| 1016 | if (round) |
| 1017 | maxreq -= round; |
| 1018 | |
| 1019 | length = maxreq; |
| 1020 | } |
| 1021 | |
| 1022 | if (length) |
| 1023 | packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket); |
| 1024 | else |
| 1025 | packets = 1; /* send one packet if length is zero. */ |
| 1026 | |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1027 | if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) { |
| 1028 | dev_err(hsotg->dev, "req length > maxpacket*mc\n"); |
| 1029 | return; |
| 1030 | } |
| 1031 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1032 | if (dir_in && index != 0) |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1033 | if (hs_ep->isochronous) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1034 | epsize = DXEPTSIZ_MC(packets); |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1035 | else |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1036 | epsize = DXEPTSIZ_MC(1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1037 | else |
| 1038 | epsize = 0; |
| 1039 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1040 | /* |
| 1041 | * zero length packet should be programmed on its own and should not |
| 1042 | * be counted in DIEPTSIZ.PktCnt with other packets. |
| 1043 | */ |
| 1044 | if (dir_in && ureq->zero && !continuing) { |
| 1045 | /* Test if zlp is actually required. */ |
| 1046 | if ((ureq->length >= hs_ep->ep.maxpacket) && |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1047 | !(ureq->length % hs_ep->ep.maxpacket)) |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 1048 | hs_ep->send_zlp = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1051 | epsize |= DXEPTSIZ_PKTCNT(packets); |
| 1052 | epsize |= DXEPTSIZ_XFERSIZE(length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1053 | |
| 1054 | dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n", |
| 1055 | __func__, packets, length, ureq->length, epsize, epsize_reg); |
| 1056 | |
| 1057 | /* store the request as the current one we're doing */ |
| 1058 | hs_ep->req = hs_req; |
| 1059 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1060 | if (using_desc_dma(hsotg)) { |
| 1061 | u32 offset = 0; |
| 1062 | u32 mps = hs_ep->ep.maxpacket; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1063 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1064 | /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */ |
| 1065 | if (!dir_in) { |
| 1066 | if (!index) |
| 1067 | length = mps; |
| 1068 | else if (length % mps) |
| 1069 | length += (mps - (length % mps)); |
| 1070 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1071 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1072 | /* |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1073 | * If more data to send, adjust DMA for EP0 out data stage. |
| 1074 | * ureq->dma stays unchanged, hence increment it by already |
| 1075 | * passed passed data count before starting new transaction. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1076 | */ |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1077 | if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT && |
| 1078 | continuing) |
| 1079 | offset = ureq->actual; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1080 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1081 | /* Fill DDMA chain entries */ |
| 1082 | dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset, |
| 1083 | length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1084 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1085 | /* write descriptor chain address to control register */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1086 | dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg); |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1087 | |
| 1088 | dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n", |
| 1089 | __func__, (u32)hs_ep->desc_list_dma, dma_reg); |
| 1090 | } else { |
| 1091 | /* write size / packets */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1092 | dwc2_writel(hsotg, epsize, epsize_reg); |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1093 | |
Razmik Karapetyan | 729e657 | 2016-11-16 15:33:55 -0800 | [diff] [blame] | 1094 | if (using_dma(hsotg) && !continuing && (length != 0)) { |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1095 | /* |
| 1096 | * write DMA address to control register, buffer |
| 1097 | * already synced by dwc2_hsotg_ep_queue(). |
| 1098 | */ |
| 1099 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1100 | dwc2_writel(hsotg, ureq->dma, dma_reg); |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1101 | |
| 1102 | dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n", |
| 1103 | __func__, &ureq->dma, dma_reg); |
| 1104 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1107 | if (hs_ep->isochronous && hs_ep->interval == 1) { |
| 1108 | hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg); |
| 1109 | dwc2_gadget_incr_frame_num(hs_ep); |
| 1110 | |
| 1111 | if (hs_ep->target_frame & 0x1) |
| 1112 | ctrl |= DXEPCTL_SETODDFR; |
| 1113 | else |
| 1114 | ctrl |= DXEPCTL_SETEVENFR; |
| 1115 | } |
| 1116 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1117 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1118 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1119 | dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state); |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1120 | |
| 1121 | /* For Setup request do not clear NAK */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1122 | if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP)) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1123 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1124 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1125 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1126 | dwc2_writel(hsotg, ctrl, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1127 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1128 | /* |
| 1129 | * set these, it seems that DMA support increments past the end |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1130 | * of the packet buffer so we need to calculate the length from |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1131 | * this information. |
| 1132 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1133 | hs_ep->size_loaded = length; |
| 1134 | hs_ep->last_load = ureq->actual; |
| 1135 | |
| 1136 | if (dir_in && !using_dma(hsotg)) { |
| 1137 | /* set these anyway, we may need them for non-periodic in */ |
| 1138 | hs_ep->fifo_load = 0; |
| 1139 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1140 | dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1143 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1144 | * Note, trying to clear the NAK here causes problems with transmit |
| 1145 | * on the S3C6400 ending up with the TXFIFO becoming full. |
| 1146 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1147 | |
| 1148 | /* check ep is enabled */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1149 | if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA)) |
Mian Yousaf Kaukab | 1a0ed86 | 2015-01-09 13:39:00 +0100 | [diff] [blame] | 1150 | dev_dbg(hsotg->dev, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1151 | "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1152 | index, dwc2_readl(hsotg, epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1153 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1154 | dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1155 | __func__, dwc2_readl(hsotg, epctrl_reg)); |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 1156 | |
| 1157 | /* enable ep interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1158 | 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] | 1159 | } |
| 1160 | |
| 1161 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1162 | * 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] | 1163 | * @hsotg: The device state. |
| 1164 | * @hs_ep: The endpoint the request is on. |
| 1165 | * @req: The request being processed. |
| 1166 | * |
| 1167 | * We've been asked to queue a request, so ensure that the memory buffer |
| 1168 | * is correctly setup for DMA. If we've been passed an extant DMA address |
| 1169 | * then ensure the buffer has been synced to memory. If our buffer has no |
| 1170 | * DMA memory, then we map the memory and mark our request to allow us to |
| 1171 | * cleanup on completion. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1172 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1173 | static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1174 | struct dwc2_hsotg_ep *hs_ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1175 | struct usb_request *req) |
| 1176 | { |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 1177 | int ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1178 | |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 1179 | ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in); |
| 1180 | if (ret) |
| 1181 | goto dma_error; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1182 | |
| 1183 | return 0; |
| 1184 | |
| 1185 | dma_error: |
| 1186 | dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n", |
| 1187 | __func__, req->buf, req->length); |
| 1188 | |
| 1189 | return -EIO; |
| 1190 | } |
| 1191 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1192 | static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg, |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 1193 | struct dwc2_hsotg_ep *hs_ep, |
| 1194 | struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1195 | { |
| 1196 | void *req_buf = hs_req->req.buf; |
| 1197 | |
| 1198 | /* If dma is not being used or buffer is aligned */ |
| 1199 | if (!using_dma(hsotg) || !((long)req_buf & 3)) |
| 1200 | return 0; |
| 1201 | |
| 1202 | WARN_ON(hs_req->saved_req_buf); |
| 1203 | |
| 1204 | dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1205 | hs_ep->ep.name, req_buf, hs_req->req.length); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1206 | |
| 1207 | hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC); |
| 1208 | if (!hs_req->req.buf) { |
| 1209 | hs_req->req.buf = req_buf; |
| 1210 | dev_err(hsotg->dev, |
| 1211 | "%s: unable to allocate memory for bounce buffer\n", |
| 1212 | __func__); |
| 1213 | return -ENOMEM; |
| 1214 | } |
| 1215 | |
| 1216 | /* Save actual buffer */ |
| 1217 | hs_req->saved_req_buf = req_buf; |
| 1218 | |
| 1219 | if (hs_ep->dir_in) |
| 1220 | memcpy(hs_req->req.buf, req_buf, hs_req->req.length); |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 1224 | static void |
| 1225 | dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg, |
| 1226 | struct dwc2_hsotg_ep *hs_ep, |
| 1227 | struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1228 | { |
| 1229 | /* If dma is not being used or buffer was aligned */ |
| 1230 | if (!using_dma(hsotg) || !hs_req->saved_req_buf) |
| 1231 | return; |
| 1232 | |
| 1233 | dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__, |
| 1234 | hs_ep->ep.name, hs_req->req.status, hs_req->req.actual); |
| 1235 | |
| 1236 | /* Copy data from bounce buffer on successful out transfer */ |
| 1237 | if (!hs_ep->dir_in && !hs_req->req.status) |
| 1238 | memcpy(hs_req->saved_req_buf, hs_req->req.buf, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1239 | hs_req->req.actual); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1240 | |
| 1241 | /* Free bounce buffer */ |
| 1242 | kfree(hs_req->req.buf); |
| 1243 | |
| 1244 | hs_req->req.buf = hs_req->saved_req_buf; |
| 1245 | hs_req->saved_req_buf = NULL; |
| 1246 | } |
| 1247 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 1248 | /** |
| 1249 | * dwc2_gadget_target_frame_elapsed - Checks target frame |
| 1250 | * @hs_ep: The driver endpoint to check |
| 1251 | * |
| 1252 | * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop |
| 1253 | * corresponding transfer. |
| 1254 | */ |
| 1255 | static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep) |
| 1256 | { |
| 1257 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 1258 | u32 target_frame = hs_ep->target_frame; |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 1259 | u32 current_frame = hsotg->frame_number; |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 1260 | bool frame_overrun = hs_ep->frame_overrun; |
| 1261 | |
| 1262 | if (!frame_overrun && current_frame >= target_frame) |
| 1263 | return true; |
| 1264 | |
| 1265 | if (frame_overrun && current_frame >= target_frame && |
| 1266 | ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2)) |
| 1267 | return true; |
| 1268 | |
| 1269 | return false; |
| 1270 | } |
| 1271 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1272 | /* |
| 1273 | * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers |
| 1274 | * @hsotg: The driver state |
| 1275 | * @hs_ep: the ep descriptor chain is for |
| 1276 | * |
| 1277 | * Called to update EP0 structure's pointers depend on stage of |
| 1278 | * control transfer. |
| 1279 | */ |
| 1280 | static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg, |
| 1281 | struct dwc2_hsotg_ep *hs_ep) |
| 1282 | { |
| 1283 | switch (hsotg->ep0_state) { |
| 1284 | case DWC2_EP0_SETUP: |
| 1285 | case DWC2_EP0_STATUS_OUT: |
| 1286 | hs_ep->desc_list = hsotg->setup_desc[0]; |
| 1287 | hs_ep->desc_list_dma = hsotg->setup_desc_dma[0]; |
| 1288 | break; |
| 1289 | case DWC2_EP0_DATA_IN: |
| 1290 | case DWC2_EP0_STATUS_IN: |
| 1291 | hs_ep->desc_list = hsotg->ctrl_in_desc; |
| 1292 | hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma; |
| 1293 | break; |
| 1294 | case DWC2_EP0_DATA_OUT: |
| 1295 | hs_ep->desc_list = hsotg->ctrl_out_desc; |
| 1296 | hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma; |
| 1297 | break; |
| 1298 | default: |
| 1299 | dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n", |
| 1300 | hsotg->ep0_state); |
| 1301 | return -EINVAL; |
| 1302 | } |
| 1303 | |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1307 | 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] | 1308 | gfp_t gfp_flags) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1309 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1310 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 1311 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1312 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1313 | bool first; |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1314 | int ret; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1315 | u32 maxsize = 0; |
| 1316 | u32 mask = 0; |
| 1317 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1318 | |
| 1319 | dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n", |
| 1320 | ep->name, req, req->length, req->buf, req->no_interrupt, |
| 1321 | req->zero, req->short_not_ok); |
| 1322 | |
Gregory Herrero | 7ababa9 | 2015-04-29 22:09:08 +0200 | [diff] [blame] | 1323 | /* Prevent new request submission when controller is suspended */ |
Grigor Tovmasyan | 88b02f2 | 2018-01-24 17:44:25 +0400 | [diff] [blame] | 1324 | if (hs->lx_state != DWC2_L0) { |
| 1325 | dev_dbg(hs->dev, "%s: submit request only in active state\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1326 | __func__); |
Gregory Herrero | 7ababa9 | 2015-04-29 22:09:08 +0200 | [diff] [blame] | 1327 | return -EAGAIN; |
| 1328 | } |
| 1329 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1330 | /* initialise status of the request */ |
| 1331 | INIT_LIST_HEAD(&hs_req->queue); |
| 1332 | req->actual = 0; |
| 1333 | req->status = -EINPROGRESS; |
| 1334 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1335 | /* In DDMA mode for ISOC's don't queue request if length greater |
| 1336 | * than descriptor limits. |
| 1337 | */ |
| 1338 | if (using_desc_dma(hs) && hs_ep->isochronous) { |
| 1339 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
| 1340 | if (hs_ep->dir_in && req->length > maxsize) { |
| 1341 | dev_err(hs->dev, "wrong length %d (maxsize=%d)\n", |
| 1342 | req->length, maxsize); |
| 1343 | return -EINVAL; |
| 1344 | } |
| 1345 | |
| 1346 | if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) { |
| 1347 | dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n", |
| 1348 | req->length, hs_ep->ep.maxpacket); |
| 1349 | return -EINVAL; |
| 1350 | } |
| 1351 | } |
| 1352 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1353 | 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] | 1354 | if (ret) |
| 1355 | return ret; |
| 1356 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1357 | /* if we're using DMA, sync the buffers as necessary */ |
| 1358 | if (using_dma(hs)) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1359 | ret = dwc2_hsotg_map_dma(hs, hs_ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1360 | if (ret) |
| 1361 | return ret; |
| 1362 | } |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1363 | /* If using descriptor DMA configure EP0 descriptor chain pointers */ |
| 1364 | if (using_desc_dma(hs) && !hs_ep->index) { |
| 1365 | ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep); |
| 1366 | if (ret) |
| 1367 | return ret; |
| 1368 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1369 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1370 | first = list_empty(&hs_ep->queue); |
| 1371 | list_add_tail(&hs_req->queue, &hs_ep->queue); |
| 1372 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1373 | /* |
| 1374 | * Handle DDMA isochronous transfers separately - just add new entry |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1375 | * to the descriptor chain. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1376 | * Transfer will be started once SW gets either one of NAK or |
| 1377 | * OutTknEpDis interrupts. |
| 1378 | */ |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1379 | if (using_desc_dma(hs) && hs_ep->isochronous) { |
| 1380 | if (hs_ep->target_frame != TARGET_FRAME_INITIAL) { |
| 1381 | dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma, |
| 1382 | hs_req->req.length); |
| 1383 | } |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1384 | return 0; |
| 1385 | } |
| 1386 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1387 | if (first) { |
| 1388 | if (!hs_ep->isochronous) { |
| 1389 | dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); |
| 1390 | return 0; |
| 1391 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1392 | |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 1393 | /* Update current frame number value. */ |
| 1394 | hs->frame_number = dwc2_hsotg_read_frameno(hs); |
| 1395 | while (dwc2_gadget_target_frame_elapsed(hs_ep)) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1396 | dwc2_gadget_incr_frame_num(hs_ep); |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 1397 | /* Update current frame number value once more as it |
| 1398 | * changes here. |
| 1399 | */ |
| 1400 | hs->frame_number = dwc2_hsotg_read_frameno(hs); |
| 1401 | } |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1402 | |
| 1403 | if (hs_ep->target_frame != TARGET_FRAME_INITIAL) |
| 1404 | dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); |
| 1405 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1406 | return 0; |
| 1407 | } |
| 1408 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1409 | 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] | 1410 | gfp_t gfp_flags) |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1411 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1412 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1413 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1414 | unsigned long flags = 0; |
| 1415 | int ret = 0; |
| 1416 | |
| 1417 | spin_lock_irqsave(&hs->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1418 | ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1419 | spin_unlock_irqrestore(&hs->lock, flags); |
| 1420 | |
| 1421 | return ret; |
| 1422 | } |
| 1423 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1424 | static void dwc2_hsotg_ep_free_request(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1425 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1426 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1427 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1428 | |
| 1429 | kfree(hs_req); |
| 1430 | } |
| 1431 | |
| 1432 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1433 | * dwc2_hsotg_complete_oursetup - setup completion callback |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1434 | * @ep: The endpoint the request was on. |
| 1435 | * @req: The request completed. |
| 1436 | * |
| 1437 | * Called on completion of any requests the driver itself |
| 1438 | * submitted that need cleaning up. |
| 1439 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1440 | static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1441 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1442 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1443 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1444 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1445 | |
| 1446 | dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req); |
| 1447 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1448 | dwc2_hsotg_ep_free_request(ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * ep_from_windex - convert control wIndex value to endpoint |
| 1453 | * @hsotg: The driver state. |
| 1454 | * @windex: The control request wIndex field (in host order). |
| 1455 | * |
| 1456 | * Convert the given wIndex into a pointer to an driver endpoint |
| 1457 | * structure, or return NULL if it is not a valid endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1458 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1459 | static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1460 | u32 windex) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1461 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1462 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1463 | int dir = (windex & USB_DIR_IN) ? 1 : 0; |
| 1464 | int idx = windex & 0x7F; |
| 1465 | |
| 1466 | if (windex >= 0x100) |
| 1467 | return NULL; |
| 1468 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 1469 | if (idx > hsotg->num_of_eps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1470 | return NULL; |
| 1471 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1472 | ep = index_to_ep(hsotg, idx, dir); |
| 1473 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1474 | if (idx && ep->dir_in != dir) |
| 1475 | return NULL; |
| 1476 | |
| 1477 | return ep; |
| 1478 | } |
| 1479 | |
| 1480 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1481 | * dwc2_hsotg_set_test_mode - Enable usb Test Modes |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1482 | * @hsotg: The driver state. |
| 1483 | * @testmode: requested usb test mode |
| 1484 | * Enable usb Test Mode requested by the Host. |
| 1485 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1486 | int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode) |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1487 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1488 | int dctl = dwc2_readl(hsotg, DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1489 | |
| 1490 | dctl &= ~DCTL_TSTCTL_MASK; |
| 1491 | switch (testmode) { |
| 1492 | case TEST_J: |
| 1493 | case TEST_K: |
| 1494 | case TEST_SE0_NAK: |
| 1495 | case TEST_PACKET: |
| 1496 | case TEST_FORCE_EN: |
| 1497 | dctl |= testmode << DCTL_TSTCTL_SHIFT; |
| 1498 | break; |
| 1499 | default: |
| 1500 | return -EINVAL; |
| 1501 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1502 | dwc2_writel(hsotg, dctl, DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1507 | * dwc2_hsotg_send_reply - send reply to control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1508 | * @hsotg: The device state |
| 1509 | * @ep: Endpoint 0 |
| 1510 | * @buff: Buffer for request |
| 1511 | * @length: Length of reply. |
| 1512 | * |
| 1513 | * Create a request and queue it on the given endpoint. This is useful as |
| 1514 | * an internal method of sending replies to certain control requests, etc. |
| 1515 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1516 | static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1517 | struct dwc2_hsotg_ep *ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1518 | void *buff, |
| 1519 | int length) |
| 1520 | { |
| 1521 | struct usb_request *req; |
| 1522 | int ret; |
| 1523 | |
| 1524 | dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length); |
| 1525 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1526 | req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1527 | hsotg->ep0_reply = req; |
| 1528 | if (!req) { |
| 1529 | dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__); |
| 1530 | return -ENOMEM; |
| 1531 | } |
| 1532 | |
| 1533 | req->buf = hsotg->ep0_buff; |
| 1534 | req->length = length; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1535 | /* |
| 1536 | * zero flag is for sending zlp in DATA IN stage. It has no impact on |
| 1537 | * STATUS stage. |
| 1538 | */ |
| 1539 | req->zero = 0; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1540 | req->complete = dwc2_hsotg_complete_oursetup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1541 | |
| 1542 | if (length) |
| 1543 | memcpy(req->buf, buff, length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1544 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1545 | ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1546 | if (ret) { |
| 1547 | dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__); |
| 1548 | return ret; |
| 1549 | } |
| 1550 | |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1555 | * dwc2_hsotg_process_req_status - process request GET_STATUS |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1556 | * @hsotg: The device state |
| 1557 | * @ctrl: USB control request |
| 1558 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1559 | static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1560 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1561 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1562 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 1563 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1564 | __le16 reply; |
| 1565 | int ret; |
| 1566 | |
| 1567 | dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__); |
| 1568 | |
| 1569 | if (!ep0->dir_in) { |
| 1570 | dev_warn(hsotg->dev, "%s: direction out?\n", __func__); |
| 1571 | return -EINVAL; |
| 1572 | } |
| 1573 | |
| 1574 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1575 | case USB_RECIP_DEVICE: |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 1576 | /* |
| 1577 | * bit 0 => self powered |
| 1578 | * bit 1 => remote wakeup |
| 1579 | */ |
| 1580 | reply = cpu_to_le16(0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1581 | break; |
| 1582 | |
| 1583 | case USB_RECIP_INTERFACE: |
| 1584 | /* currently, the data result should be zero */ |
| 1585 | reply = cpu_to_le16(0); |
| 1586 | break; |
| 1587 | |
| 1588 | case USB_RECIP_ENDPOINT: |
| 1589 | ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex)); |
| 1590 | if (!ep) |
| 1591 | return -ENOENT; |
| 1592 | |
| 1593 | reply = cpu_to_le16(ep->halted ? 1 : 0); |
| 1594 | break; |
| 1595 | |
| 1596 | default: |
| 1597 | return 0; |
| 1598 | } |
| 1599 | |
| 1600 | if (le16_to_cpu(ctrl->wLength) != 2) |
| 1601 | return -EINVAL; |
| 1602 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1603 | ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1604 | if (ret) { |
| 1605 | dev_err(hsotg->dev, "%s: failed to send reply\n", __func__); |
| 1606 | return ret; |
| 1607 | } |
| 1608 | |
| 1609 | return 1; |
| 1610 | } |
| 1611 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1612 | 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] | 1613 | |
| 1614 | /** |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1615 | * get_ep_head - return the first request on the endpoint |
| 1616 | * @hs_ep: The controller endpoint to get |
| 1617 | * |
| 1618 | * Get the first request on the endpoint. |
| 1619 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1620 | 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] | 1621 | { |
Masahiro Yamada | ffc4b40 | 2016-09-19 01:03:13 +0900 | [diff] [blame] | 1622 | return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req, |
| 1623 | queue); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | /** |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1627 | * dwc2_gadget_start_next_request - Starts next request from ep queue |
| 1628 | * @hs_ep: Endpoint structure |
| 1629 | * |
| 1630 | * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked |
| 1631 | * in its handler. Hence we need to unmask it here to be able to do |
| 1632 | * resynchronization. |
| 1633 | */ |
| 1634 | static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep) |
| 1635 | { |
| 1636 | u32 mask; |
| 1637 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 1638 | int dir_in = hs_ep->dir_in; |
| 1639 | struct dwc2_hsotg_req *hs_req; |
| 1640 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 1641 | |
| 1642 | if (!list_empty(&hs_ep->queue)) { |
| 1643 | hs_req = get_ep_head(hs_ep); |
| 1644 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false); |
| 1645 | return; |
| 1646 | } |
| 1647 | if (!hs_ep->isochronous) |
| 1648 | return; |
| 1649 | |
| 1650 | if (dir_in) { |
| 1651 | dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n", |
| 1652 | __func__); |
| 1653 | } else { |
| 1654 | dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n", |
| 1655 | __func__); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1656 | mask = dwc2_readl(hsotg, epmsk_reg); |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1657 | mask |= DOEPMSK_OUTTKNEPDISMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1658 | dwc2_writel(hsotg, mask, epmsk_reg); |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1663 | * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1664 | * @hsotg: The device state |
| 1665 | * @ctrl: USB control request |
| 1666 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1667 | static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1668 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1669 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1670 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 1671 | struct dwc2_hsotg_req *hs_req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1672 | bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1673 | struct dwc2_hsotg_ep *ep; |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1674 | int ret; |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1675 | bool halted; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1676 | u32 recip; |
| 1677 | u32 wValue; |
| 1678 | u32 wIndex; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1679 | |
| 1680 | dev_dbg(hsotg->dev, "%s: %s_FEATURE\n", |
| 1681 | __func__, set ? "SET" : "CLEAR"); |
| 1682 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1683 | wValue = le16_to_cpu(ctrl->wValue); |
| 1684 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 1685 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 1686 | |
| 1687 | switch (recip) { |
| 1688 | case USB_RECIP_DEVICE: |
| 1689 | switch (wValue) { |
Vardan Mikayelyan | fa389a6 | 2018-02-16 14:08:53 +0400 | [diff] [blame] | 1690 | case USB_DEVICE_REMOTE_WAKEUP: |
| 1691 | hsotg->remote_wakeup_allowed = 1; |
| 1692 | break; |
| 1693 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1694 | case USB_DEVICE_TEST_MODE: |
| 1695 | if ((wIndex & 0xff) != 0) |
| 1696 | return -EINVAL; |
| 1697 | if (!set) |
| 1698 | return -EINVAL; |
| 1699 | |
| 1700 | hsotg->test_mode = wIndex >> 8; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1701 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1702 | if (ret) { |
| 1703 | dev_err(hsotg->dev, |
| 1704 | "%s: failed to send reply\n", __func__); |
| 1705 | return ret; |
| 1706 | } |
| 1707 | break; |
| 1708 | default: |
| 1709 | return -ENOENT; |
| 1710 | } |
| 1711 | break; |
| 1712 | |
| 1713 | case USB_RECIP_ENDPOINT: |
| 1714 | ep = ep_from_windex(hsotg, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1715 | if (!ep) { |
| 1716 | dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n", |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1717 | __func__, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1718 | return -ENOENT; |
| 1719 | } |
| 1720 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1721 | switch (wValue) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1722 | case USB_ENDPOINT_HALT: |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1723 | halted = ep->halted; |
| 1724 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1725 | dwc2_hsotg_ep_sethalt(&ep->ep, set, true); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1726 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1727 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1728 | if (ret) { |
| 1729 | dev_err(hsotg->dev, |
| 1730 | "%s: failed to send reply\n", __func__); |
| 1731 | return ret; |
| 1732 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1733 | |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1734 | /* |
| 1735 | * we have to complete all requests for ep if it was |
| 1736 | * halted, and the halt was cleared by CLEAR_FEATURE |
| 1737 | */ |
| 1738 | |
| 1739 | if (!set && halted) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1740 | /* |
| 1741 | * If we have request in progress, |
| 1742 | * then complete it |
| 1743 | */ |
| 1744 | if (ep->req) { |
| 1745 | hs_req = ep->req; |
| 1746 | ep->req = NULL; |
| 1747 | list_del_init(&hs_req->queue); |
Gregory Herrero | c00dd4a | 2015-01-30 09:09:27 +0100 | [diff] [blame] | 1748 | if (hs_req->req.complete) { |
| 1749 | spin_unlock(&hsotg->lock); |
| 1750 | usb_gadget_giveback_request( |
| 1751 | &ep->ep, &hs_req->req); |
| 1752 | spin_lock(&hsotg->lock); |
| 1753 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /* If we have pending request, then start it */ |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 1757 | if (!ep->req) |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1758 | dwc2_gadget_start_next_request(ep); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1759 | } |
| 1760 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1761 | break; |
| 1762 | |
| 1763 | default: |
| 1764 | return -ENOENT; |
| 1765 | } |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1766 | break; |
| 1767 | default: |
| 1768 | return -ENOENT; |
| 1769 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1770 | return 1; |
| 1771 | } |
| 1772 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1773 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg); |
Robert Baldyga | ab93e01 | 2013-09-19 11:50:17 +0200 | [diff] [blame] | 1774 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1775 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1776 | * dwc2_hsotg_stall_ep0 - stall ep0 |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1777 | * @hsotg: The device state |
| 1778 | * |
| 1779 | * Set stall for ep0 as response for setup request. |
| 1780 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1781 | static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg) |
Jingoo Han | e9ebe7c | 2014-06-03 22:14:56 +0900 | [diff] [blame] | 1782 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1783 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1784 | u32 reg; |
| 1785 | u32 ctrl; |
| 1786 | |
| 1787 | dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in); |
| 1788 | reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0; |
| 1789 | |
| 1790 | /* |
| 1791 | * DxEPCTL_Stall will be cleared by EP once it has |
| 1792 | * taken effect, so no need to clear later. |
| 1793 | */ |
| 1794 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1795 | ctrl = dwc2_readl(hsotg, reg); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1796 | ctrl |= DXEPCTL_STALL; |
| 1797 | ctrl |= DXEPCTL_CNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1798 | dwc2_writel(hsotg, ctrl, reg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1799 | |
| 1800 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1801 | "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1802 | ctrl, reg, dwc2_readl(hsotg, reg)); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1803 | |
| 1804 | /* |
| 1805 | * complete won't be called, so we enqueue |
| 1806 | * setup request here |
| 1807 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1808 | dwc2_hsotg_enqueue_setup(hsotg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1812 | * dwc2_hsotg_process_control - process a control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1813 | * @hsotg: The device state |
| 1814 | * @ctrl: The control request received |
| 1815 | * |
| 1816 | * The controller has received the SETUP phase of a control request, and |
| 1817 | * needs to work out what to do next (and whether to pass it on to the |
| 1818 | * gadget driver). |
| 1819 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1820 | static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1821 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1822 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1823 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1824 | int ret = 0; |
| 1825 | u32 dcfg; |
| 1826 | |
Mian Yousaf Kaukab | e525e74 | 2015-09-29 12:08:23 +0200 | [diff] [blame] | 1827 | dev_dbg(hsotg->dev, |
| 1828 | "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n", |
| 1829 | ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, |
| 1830 | ctrl->wIndex, ctrl->wLength); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1831 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1832 | if (ctrl->wLength == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1833 | ep0->dir_in = 1; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1834 | hsotg->ep0_state = DWC2_EP0_STATUS_IN; |
| 1835 | } else if (ctrl->bRequestType & USB_DIR_IN) { |
| 1836 | ep0->dir_in = 1; |
| 1837 | hsotg->ep0_state = DWC2_EP0_DATA_IN; |
| 1838 | } else { |
| 1839 | ep0->dir_in = 0; |
| 1840 | hsotg->ep0_state = DWC2_EP0_DATA_OUT; |
| 1841 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1842 | |
| 1843 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { |
| 1844 | switch (ctrl->bRequest) { |
| 1845 | case USB_REQ_SET_ADDRESS: |
Mian Yousaf Kaukab | 6d713c1 | 2015-01-09 13:39:10 +0100 | [diff] [blame] | 1846 | hsotg->connected = 1; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1847 | dcfg = dwc2_readl(hsotg, DCFG); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1848 | dcfg &= ~DCFG_DEVADDR_MASK; |
Paul Zimmerman | d5dbd3f | 2014-04-25 14:18:13 -0700 | [diff] [blame] | 1849 | dcfg |= (le16_to_cpu(ctrl->wValue) << |
| 1850 | DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1851 | dwc2_writel(hsotg, dcfg, DCFG); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1852 | |
| 1853 | dev_info(hsotg->dev, "new address %d\n", ctrl->wValue); |
| 1854 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1855 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1856 | return; |
| 1857 | |
| 1858 | case USB_REQ_GET_STATUS: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1859 | ret = dwc2_hsotg_process_req_status(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1860 | break; |
| 1861 | |
| 1862 | case USB_REQ_CLEAR_FEATURE: |
| 1863 | case USB_REQ_SET_FEATURE: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1864 | ret = dwc2_hsotg_process_req_feature(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1865 | break; |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | /* as a fallback, try delivering it to the driver to deal with */ |
| 1870 | |
| 1871 | if (ret == 0 && hsotg->driver) { |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1872 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1873 | ret = hsotg->driver->setup(&hsotg->gadget, ctrl); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1874 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1875 | if (ret < 0) |
| 1876 | dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret); |
| 1877 | } |
| 1878 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1879 | /* |
| 1880 | * the request is either unhandlable, or is not formatted correctly |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1881 | * so respond with a STALL for the status stage to indicate failure. |
| 1882 | */ |
| 1883 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1884 | if (ret < 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1885 | dwc2_hsotg_stall_ep0(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1886 | } |
| 1887 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1888 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1889 | * dwc2_hsotg_complete_setup - completion of a setup transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1890 | * @ep: The endpoint the request was on. |
| 1891 | * @req: The request completed. |
| 1892 | * |
| 1893 | * Called on completion of any requests the driver itself submitted for |
| 1894 | * EP0 setup packets |
| 1895 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1896 | static void dwc2_hsotg_complete_setup(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1897 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1898 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1899 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1900 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1901 | |
| 1902 | if (req->status < 0) { |
| 1903 | dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status); |
| 1904 | return; |
| 1905 | } |
| 1906 | |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1907 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1908 | if (req->actual == 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1909 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1910 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1911 | dwc2_hsotg_process_control(hsotg, req->buf); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1912 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1916 | * dwc2_hsotg_enqueue_setup - start a request for EP0 packets |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1917 | * @hsotg: The device state. |
| 1918 | * |
| 1919 | * Enqueue a request on EP0 if necessary to received any SETUP packets |
| 1920 | * received from the host. |
| 1921 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1922 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1923 | { |
| 1924 | struct usb_request *req = hsotg->ctrl_req; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1925 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1926 | int ret; |
| 1927 | |
| 1928 | dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__); |
| 1929 | |
| 1930 | req->zero = 0; |
| 1931 | req->length = 8; |
| 1932 | req->buf = hsotg->ctrl_buff; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1933 | req->complete = dwc2_hsotg_complete_setup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1934 | |
| 1935 | if (!list_empty(&hs_req->queue)) { |
| 1936 | dev_dbg(hsotg->dev, "%s already queued???\n", __func__); |
| 1937 | return; |
| 1938 | } |
| 1939 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1940 | hsotg->eps_out[0]->dir_in = 0; |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 1941 | hsotg->eps_out[0]->send_zlp = 0; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1942 | hsotg->ep0_state = DWC2_EP0_SETUP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1943 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1944 | 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] | 1945 | if (ret < 0) { |
| 1946 | dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret); |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1947 | /* |
| 1948 | * Don't think there's much we can do other than watch the |
| 1949 | * driver fail. |
| 1950 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1951 | } |
| 1952 | } |
| 1953 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1954 | static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1955 | struct dwc2_hsotg_ep *hs_ep) |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1956 | { |
| 1957 | u32 ctrl; |
| 1958 | u8 index = hs_ep->index; |
| 1959 | u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 1960 | u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
| 1961 | |
Mian Yousaf Kaukab | ccb34a9 | 2015-01-30 09:09:34 +0100 | [diff] [blame] | 1962 | if (hs_ep->dir_in) |
| 1963 | dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1964 | index); |
Mian Yousaf Kaukab | ccb34a9 | 2015-01-30 09:09:34 +0100 | [diff] [blame] | 1965 | else |
| 1966 | dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n", |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1967 | index); |
| 1968 | if (using_desc_dma(hsotg)) { |
| 1969 | /* Not specific buffer needed for ep0 ZLP */ |
| 1970 | dma_addr_t dma = hs_ep->desc_list_dma; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1971 | |
Minas Harutyunyan | 201ec56 | 2018-01-16 16:03:32 +0400 | [diff] [blame] | 1972 | if (!index) |
| 1973 | dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep); |
| 1974 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1975 | dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0); |
| 1976 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1977 | dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
| 1978 | DXEPTSIZ_XFERSIZE(0), |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1979 | epsiz_reg); |
| 1980 | } |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1981 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1982 | ctrl = dwc2_readl(hsotg, epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1983 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
| 1984 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
| 1985 | ctrl |= DXEPCTL_USBACTEP; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1986 | dwc2_writel(hsotg, ctrl, epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1987 | } |
| 1988 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1989 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1990 | * dwc2_hsotg_complete_request - complete a request given to us |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1991 | * @hsotg: The device state. |
| 1992 | * @hs_ep: The endpoint the request was on. |
| 1993 | * @hs_req: The request to complete. |
| 1994 | * @result: The result code (0 => Ok, otherwise errno) |
| 1995 | * |
| 1996 | * The given request has finished, so call the necessary completion |
| 1997 | * if it has one and then look to see if we can start a new request |
| 1998 | * on the endpoint. |
| 1999 | * |
| 2000 | * Note, expects the ep to already be locked as appropriate. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2001 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2002 | static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2003 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2004 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2005 | int result) |
| 2006 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2007 | if (!hs_req) { |
| 2008 | dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__); |
| 2009 | return; |
| 2010 | } |
| 2011 | |
| 2012 | dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n", |
| 2013 | hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete); |
| 2014 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2015 | /* |
| 2016 | * only replace the status if we've not already set an error |
| 2017 | * from a previous transaction |
| 2018 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2019 | |
| 2020 | if (hs_req->req.status == -EINPROGRESS) |
| 2021 | hs_req->req.status = result; |
| 2022 | |
Yunzhi Li | 44583fe | 2015-09-29 12:25:01 +0200 | [diff] [blame] | 2023 | if (using_dma(hsotg)) |
| 2024 | dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req); |
| 2025 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2026 | dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 2027 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2028 | hs_ep->req = NULL; |
| 2029 | list_del_init(&hs_req->queue); |
| 2030 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2031 | /* |
| 2032 | * call the complete request with the locks off, just in case the |
| 2033 | * request tries to queue more work for this endpoint. |
| 2034 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2035 | |
| 2036 | if (hs_req->req.complete) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2037 | spin_unlock(&hsotg->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 2038 | usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2039 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2040 | } |
| 2041 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2042 | /* In DDMA don't need to proceed to starting of next ISOC request */ |
| 2043 | if (using_desc_dma(hsotg) && hs_ep->isochronous) |
| 2044 | return; |
| 2045 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2046 | /* |
| 2047 | * 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] | 2048 | * 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] | 2049 | * so be careful when doing this. |
| 2050 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2051 | |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 2052 | if (!hs_ep->req && result >= 0) |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 2053 | dwc2_gadget_start_next_request(hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2054 | } |
| 2055 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2056 | /* |
| 2057 | * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA |
| 2058 | * @hs_ep: The endpoint the request was on. |
| 2059 | * |
| 2060 | * Get first request from the ep queue, determine descriptor on which complete |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2061 | * happened. SW discovers which descriptor currently in use by HW, adjusts |
| 2062 | * dma_address and calculates index of completed descriptor based on the value |
| 2063 | * of DEPDMA register. Update actual length of request, giveback to gadget. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2064 | */ |
| 2065 | static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 2066 | { |
| 2067 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2068 | struct dwc2_hsotg_req *hs_req; |
| 2069 | struct usb_request *ureq; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2070 | u32 desc_sts; |
| 2071 | u32 mask; |
| 2072 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2073 | desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status; |
| 2074 | |
| 2075 | /* Process only descriptors with buffer status set to DMA done */ |
| 2076 | while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >> |
| 2077 | DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) { |
| 2078 | |
| 2079 | hs_req = get_ep_head(hs_ep); |
| 2080 | if (!hs_req) { |
| 2081 | dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__); |
| 2082 | return; |
| 2083 | } |
| 2084 | ureq = &hs_req->req; |
| 2085 | |
| 2086 | /* Check completion status */ |
| 2087 | if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT == |
| 2088 | DEV_DMA_STS_SUCC) { |
| 2089 | mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK : |
| 2090 | DEV_DMA_ISOC_RX_NBYTES_MASK; |
| 2091 | ureq->actual = ureq->length - ((desc_sts & mask) >> |
| 2092 | DEV_DMA_ISOC_NBYTES_SHIFT); |
| 2093 | |
| 2094 | /* Adjust actual len for ISOC Out if len is |
| 2095 | * not align of 4 |
| 2096 | */ |
| 2097 | if (!hs_ep->dir_in && ureq->length & 0x3) |
| 2098 | ureq->actual += 4 - (ureq->length & 0x3); |
| 2099 | } |
| 2100 | |
| 2101 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
| 2102 | |
| 2103 | hs_ep->compl_desc++; |
| 2104 | if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_GENERIC - 1)) |
| 2105 | hs_ep->compl_desc = 0; |
| 2106 | desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2107 | } |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | /* |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2111 | * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC. |
| 2112 | * @hs_ep: The isochronous endpoint. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2113 | * |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2114 | * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA |
| 2115 | * interrupt. Reset target frame and next_desc to allow to start |
| 2116 | * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS |
| 2117 | * interrupt for OUT direction. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2118 | */ |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2119 | static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep) |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2120 | { |
| 2121 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2122 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2123 | if (!hs_ep->dir_in) |
| 2124 | dwc2_flush_rx_fifo(hsotg); |
| 2125 | dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2126 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2127 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
| 2128 | hs_ep->next_desc = 0; |
| 2129 | hs_ep->compl_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2130 | } |
| 2131 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2132 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2133 | * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2134 | * @hsotg: The device state. |
| 2135 | * @ep_idx: The endpoint index for the data |
| 2136 | * @size: The size of data in the fifo, in bytes |
| 2137 | * |
| 2138 | * The FIFO status shows there is data to read from the FIFO for a given |
| 2139 | * endpoint, so sort out whether we need to read the data into a request |
| 2140 | * that has been made for that endpoint. |
| 2141 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2142 | 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] | 2143 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2144 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx]; |
| 2145 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2146 | int to_read; |
| 2147 | int max_req; |
| 2148 | int read_ptr; |
| 2149 | |
| 2150 | if (!hs_req) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2151 | u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2152 | int ptr; |
| 2153 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 2154 | dev_dbg(hsotg->dev, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2155 | "%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] | 2156 | __func__, size, ep_idx, epctl); |
| 2157 | |
| 2158 | /* dump the data from the FIFO, we've nothing we can do */ |
| 2159 | for (ptr = 0; ptr < size; ptr += 4) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2160 | (void)dwc2_readl(hsotg, EPFIFO(ep_idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2161 | |
| 2162 | return; |
| 2163 | } |
| 2164 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2165 | to_read = size; |
| 2166 | read_ptr = hs_req->req.actual; |
| 2167 | max_req = hs_req->req.length - read_ptr; |
| 2168 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2169 | dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n", |
| 2170 | __func__, to_read, max_req, read_ptr, hs_req->req.length); |
| 2171 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2172 | if (to_read > max_req) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2173 | /* |
| 2174 | * more data appeared than we where willing |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2175 | * to deal with in this request. |
| 2176 | */ |
| 2177 | |
| 2178 | /* currently we don't deal this */ |
| 2179 | WARN_ON_ONCE(1); |
| 2180 | } |
| 2181 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2182 | hs_ep->total_data += to_read; |
| 2183 | hs_req->req.actual += to_read; |
| 2184 | to_read = DIV_ROUND_UP(to_read, 4); |
| 2185 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2186 | /* |
| 2187 | * note, we might over-write the buffer end by 3 bytes depending on |
| 2188 | * alignment of the data. |
| 2189 | */ |
Gevorg Sahakyan | 342ccce | 2018-07-26 18:00:41 +0400 | [diff] [blame] | 2190 | dwc2_readl_rep(hsotg, EPFIFO(ep_idx), |
| 2191 | hs_req->req.buf + read_ptr, to_read); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2195 | * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2196 | * @hsotg: The device instance |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2197 | * @dir_in: If IN zlp |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2198 | * |
| 2199 | * Generate a zero-length IN packet request for terminating a SETUP |
| 2200 | * transaction. |
| 2201 | * |
| 2202 | * 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] | 2203 | * 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] | 2204 | * the TxFIFO. |
| 2205 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2206 | 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] | 2207 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2208 | /* eps_out[0] is used in both directions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2209 | hsotg->eps_out[0]->dir_in = dir_in; |
| 2210 | 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] | 2211 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2212 | dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2213 | } |
| 2214 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2215 | static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2216 | u32 epctl_reg) |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2217 | { |
| 2218 | u32 ctrl; |
| 2219 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2220 | ctrl = dwc2_readl(hsotg, epctl_reg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2221 | if (ctrl & DXEPCTL_EOFRNUM) |
| 2222 | ctrl |= DXEPCTL_SETEVENFR; |
| 2223 | else |
| 2224 | ctrl |= DXEPCTL_SETODDFR; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2225 | dwc2_writel(hsotg, ctrl, epctl_reg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2228 | /* |
| 2229 | * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc |
| 2230 | * @hs_ep - The endpoint on which transfer went |
| 2231 | * |
| 2232 | * Iterate over endpoints descriptor chain and get info on bytes remained |
| 2233 | * in DMA descriptors after transfer has completed. Used for non isoc EPs. |
| 2234 | */ |
| 2235 | static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 2236 | { |
| 2237 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2238 | unsigned int bytes_rem = 0; |
| 2239 | struct dwc2_dma_desc *desc = hs_ep->desc_list; |
| 2240 | int i; |
| 2241 | u32 status; |
| 2242 | |
| 2243 | if (!desc) |
| 2244 | return -EINVAL; |
| 2245 | |
| 2246 | for (i = 0; i < hs_ep->desc_count; ++i) { |
| 2247 | status = desc->status; |
| 2248 | bytes_rem += status & DEV_DMA_NBYTES_MASK; |
| 2249 | |
| 2250 | if (status & DEV_DMA_STS_MASK) |
| 2251 | dev_err(hsotg->dev, "descriptor %d closed with %x\n", |
| 2252 | i, status & DEV_DMA_STS_MASK); |
| 2253 | } |
| 2254 | |
| 2255 | return bytes_rem; |
| 2256 | } |
| 2257 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2258 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2259 | * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2260 | * @hsotg: The device instance |
| 2261 | * @epnum: The endpoint received from |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2262 | * |
| 2263 | * The RXFIFO has delivered an OutDone event, which means that the data |
| 2264 | * transfer for an OUT endpoint has been completed, either by a short |
| 2265 | * packet or by the finish of a transfer. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2266 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2267 | static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2268 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2269 | u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum)); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2270 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum]; |
| 2271 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2272 | struct usb_request *req = &hs_req->req; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2273 | unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2274 | int result = 0; |
| 2275 | |
| 2276 | if (!hs_req) { |
| 2277 | dev_dbg(hsotg->dev, "%s: no request active\n", __func__); |
| 2278 | return; |
| 2279 | } |
| 2280 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2281 | if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) { |
| 2282 | dev_dbg(hsotg->dev, "zlp packet received\n"); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2283 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
| 2284 | dwc2_hsotg_enqueue_setup(hsotg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2285 | return; |
| 2286 | } |
| 2287 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2288 | if (using_desc_dma(hsotg)) |
| 2289 | size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); |
| 2290 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2291 | if (using_dma(hsotg)) { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2292 | unsigned int size_done; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2293 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2294 | /* |
| 2295 | * Calculate the size of the transfer by checking how much |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2296 | * is left in the endpoint size register and then working it |
| 2297 | * out from the amount we loaded for the transfer. |
| 2298 | * |
| 2299 | * We need to do this as DMA pointers are always 32bit aligned |
| 2300 | * so may overshoot/undershoot the transfer. |
| 2301 | */ |
| 2302 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2303 | size_done = hs_ep->size_loaded - size_left; |
| 2304 | size_done += hs_ep->last_load; |
| 2305 | |
| 2306 | req->actual = size_done; |
| 2307 | } |
| 2308 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2309 | /* if there is more request to do, schedule new transfer */ |
| 2310 | if (req->actual < req->length && size_left == 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2311 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2312 | return; |
| 2313 | } |
| 2314 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2315 | if (req->actual < req->length && req->short_not_ok) { |
| 2316 | dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n", |
| 2317 | __func__, req->actual, req->length); |
| 2318 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2319 | /* |
| 2320 | * todo - what should we return here? there's no one else |
| 2321 | * even bothering to check the status. |
| 2322 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2323 | } |
| 2324 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 2325 | /* DDMA IN status phase will start from StsPhseRcvd interrupt */ |
| 2326 | if (!using_desc_dma(hsotg) && epnum == 0 && |
| 2327 | hsotg->ep0_state == DWC2_EP0_DATA_OUT) { |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2328 | /* Move to STATUS IN */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2329 | dwc2_hsotg_ep0_zlp(hsotg, true); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2330 | return; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2331 | } |
| 2332 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2333 | /* |
| 2334 | * Slave mode OUT transfers do not go through XferComplete so |
| 2335 | * adjust the ISOC parity here. |
| 2336 | */ |
| 2337 | if (!using_dma(hsotg)) { |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2338 | if (hs_ep->isochronous && hs_ep->interval == 1) |
| 2339 | dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2340 | else if (hs_ep->isochronous && hs_ep->interval > 1) |
| 2341 | dwc2_gadget_incr_frame_num(hs_ep); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2344 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2348 | * dwc2_hsotg_handle_rx - RX FIFO has data |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2349 | * @hsotg: The device instance |
| 2350 | * |
| 2351 | * The IRQ handler has detected that the RX FIFO has some data in it |
| 2352 | * that requires processing, so find out what is in there and do the |
| 2353 | * appropriate read. |
| 2354 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2355 | * 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] | 2356 | * chunks, so if you have x packets received on an endpoint you'll get x |
| 2357 | * FIFO events delivered, each with a packet's worth of data in it. |
| 2358 | * |
| 2359 | * When using DMA, we should not be processing events from the RXFIFO |
| 2360 | * as the actual data should be sent to the memory directly and we turn |
| 2361 | * on the completion interrupts to get notifications of transfer completion. |
| 2362 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2363 | static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2364 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2365 | u32 grxstsr = dwc2_readl(hsotg, GRXSTSP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2366 | u32 epnum, status, size; |
| 2367 | |
| 2368 | WARN_ON(using_dma(hsotg)); |
| 2369 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2370 | epnum = grxstsr & GRXSTS_EPNUM_MASK; |
| 2371 | status = grxstsr & GRXSTS_PKTSTS_MASK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2372 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2373 | size = grxstsr & GRXSTS_BYTECNT_MASK; |
| 2374 | size >>= GRXSTS_BYTECNT_SHIFT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2375 | |
Mian Yousaf Kaukab | d7c747c | 2015-01-30 09:09:30 +0100 | [diff] [blame] | 2376 | dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2377 | __func__, grxstsr, size, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2378 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2379 | switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) { |
| 2380 | case GRXSTS_PKTSTS_GLOBALOUTNAK: |
| 2381 | dev_dbg(hsotg->dev, "GLOBALOUTNAK\n"); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2382 | break; |
| 2383 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2384 | case GRXSTS_PKTSTS_OUTDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2385 | dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2386 | dwc2_hsotg_read_frameno(hsotg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2387 | |
| 2388 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2389 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2390 | break; |
| 2391 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2392 | case GRXSTS_PKTSTS_SETUPDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2393 | dev_dbg(hsotg->dev, |
| 2394 | "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2395 | dwc2_hsotg_read_frameno(hsotg), |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2396 | dwc2_readl(hsotg, DOEPCTL(0))); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2397 | /* |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2398 | * 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] | 2399 | * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't |
| 2400 | * generate GRXSTS_PKTSTS_OUTDONE for setup packet. |
| 2401 | */ |
| 2402 | if (hsotg->ep0_state == DWC2_EP0_SETUP) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2403 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2404 | break; |
| 2405 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2406 | case GRXSTS_PKTSTS_OUTRX: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2407 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2408 | break; |
| 2409 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2410 | case GRXSTS_PKTSTS_SETUPRX: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2411 | dev_dbg(hsotg->dev, |
| 2412 | "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2413 | dwc2_hsotg_read_frameno(hsotg), |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2414 | dwc2_readl(hsotg, DOEPCTL(0))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2415 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2416 | WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP); |
| 2417 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2418 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2419 | break; |
| 2420 | |
| 2421 | default: |
| 2422 | dev_warn(hsotg->dev, "%s: unknown status %08x\n", |
| 2423 | __func__, grxstsr); |
| 2424 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2425 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2426 | break; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2431 | * dwc2_hsotg_ep0_mps - turn max packet size into register setting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2432 | * @mps: The maximum packet size in bytes. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2433 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2434 | static u32 dwc2_hsotg_ep0_mps(unsigned int mps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2435 | { |
| 2436 | switch (mps) { |
| 2437 | case 64: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2438 | return D0EPCTL_MPS_64; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2439 | case 32: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2440 | return D0EPCTL_MPS_32; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2441 | case 16: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2442 | return D0EPCTL_MPS_16; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2443 | case 8: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2444 | return D0EPCTL_MPS_8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | /* bad max packet size, warn and return invalid result */ |
| 2448 | WARN_ON(1); |
| 2449 | return (u32)-1; |
| 2450 | } |
| 2451 | |
| 2452 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2453 | * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2454 | * @hsotg: The driver state. |
| 2455 | * @ep: The index number of the endpoint |
| 2456 | * @mps: The maximum packet size in bytes |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2457 | * @mc: The multicount value |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 2458 | * @dir_in: True if direction is in. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2459 | * |
| 2460 | * Configure the maximum packet size for the given endpoint, updating |
| 2461 | * the hardware control registers to reflect this. |
| 2462 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2463 | static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg, |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2464 | unsigned int ep, unsigned int mps, |
| 2465 | unsigned int mc, unsigned int dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2466 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2467 | struct dwc2_hsotg_ep *hs_ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2468 | u32 reg; |
| 2469 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2470 | hs_ep = index_to_ep(hsotg, ep, dir_in); |
| 2471 | if (!hs_ep) |
| 2472 | return; |
| 2473 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2474 | if (ep == 0) { |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2475 | u32 mps_bytes = mps; |
| 2476 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2477 | /* EP0 is a special case */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2478 | mps = dwc2_hsotg_ep0_mps(mps_bytes); |
| 2479 | if (mps > 3) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2480 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2481 | hs_ep->ep.maxpacket = mps_bytes; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 2482 | hs_ep->mc = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2483 | } else { |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2484 | if (mps > 1024) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2485 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2486 | hs_ep->mc = mc; |
| 2487 | if (mc > 3) |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 2488 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2489 | hs_ep->ep.maxpacket = mps; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2490 | } |
| 2491 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2492 | if (dir_in) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2493 | reg = dwc2_readl(hsotg, DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2494 | reg &= ~DXEPCTL_MPS_MASK; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2495 | reg |= mps; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2496 | dwc2_writel(hsotg, reg, DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2497 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2498 | reg = dwc2_readl(hsotg, DOEPCTL(ep)); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2499 | reg &= ~DXEPCTL_MPS_MASK; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2500 | reg |= mps; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2501 | dwc2_writel(hsotg, reg, DOEPCTL(ep)); |
Anton Tikhomirov | 659ad60 | 2012-03-06 14:07:29 +0900 | [diff] [blame] | 2502 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2503 | |
| 2504 | return; |
| 2505 | |
| 2506 | bad_mps: |
| 2507 | dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps); |
| 2508 | } |
| 2509 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2510 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2511 | * dwc2_hsotg_txfifo_flush - flush Tx FIFO |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2512 | * @hsotg: The driver state |
| 2513 | * @idx: The index for the endpoint (0..15) |
| 2514 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2515 | 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] | 2516 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2517 | dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH, |
| 2518 | GRSTCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2519 | |
| 2520 | /* wait until the fifo is flushed */ |
Sevak Arakelyan | 79d6b8c | 2018-01-19 14:39:31 +0400 | [diff] [blame] | 2521 | if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100)) |
| 2522 | dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n", |
| 2523 | __func__); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2524 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2525 | |
| 2526 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2527 | * dwc2_hsotg_trytx - check to see if anything needs transmitting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2528 | * @hsotg: The driver state |
| 2529 | * @hs_ep: The driver endpoint to check. |
| 2530 | * |
| 2531 | * Check to see if there is a request that has data to send, and if so |
| 2532 | * make an attempt to write data into the FIFO. |
| 2533 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2534 | static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2535 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2536 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2537 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2538 | |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 2539 | if (!hs_ep->dir_in || !hs_req) { |
| 2540 | /** |
| 2541 | * if request is not enqueued, we disable interrupts |
| 2542 | * for endpoints, excepting ep0 |
| 2543 | */ |
| 2544 | if (hs_ep->index != 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2545 | dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2546 | hs_ep->dir_in, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2547 | return 0; |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 2548 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2549 | |
| 2550 | if (hs_req->req.actual < hs_req->req.length) { |
| 2551 | dev_dbg(hsotg->dev, "trying to write more for ep%d\n", |
| 2552 | hs_ep->index); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2553 | return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | return 0; |
| 2557 | } |
| 2558 | |
| 2559 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2560 | * dwc2_hsotg_complete_in - complete IN transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2561 | * @hsotg: The device state. |
| 2562 | * @hs_ep: The endpoint that has just completed. |
| 2563 | * |
| 2564 | * An IN transfer has been completed, update the transfer's state and then |
| 2565 | * call the relevant completion routines. |
| 2566 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2567 | static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2568 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2569 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2570 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2571 | u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2572 | int size_left, size_done; |
| 2573 | |
| 2574 | if (!hs_req) { |
| 2575 | dev_dbg(hsotg->dev, "XferCompl but no req\n"); |
| 2576 | return; |
| 2577 | } |
| 2578 | |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2579 | /* Finish ZLP handling for IN EP0 transactions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2580 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) { |
| 2581 | dev_dbg(hsotg->dev, "zlp packet sent\n"); |
Razmik Karapetyan | c3b22fe | 2016-11-16 15:33:57 -0800 | [diff] [blame] | 2582 | |
| 2583 | /* |
| 2584 | * While send zlp for DWC2_EP0_STATUS_IN EP direction was |
| 2585 | * changed to IN. Change back to complete OUT transfer request |
| 2586 | */ |
| 2587 | hs_ep->dir_in = 0; |
| 2588 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2589 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2590 | if (hsotg->test_mode) { |
| 2591 | int ret; |
| 2592 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2593 | ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2594 | if (ret < 0) { |
| 2595 | dev_dbg(hsotg->dev, "Invalid Test #%d\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2596 | hsotg->test_mode); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2597 | dwc2_hsotg_stall_ep0(hsotg); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2598 | return; |
| 2599 | } |
| 2600 | } |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2601 | dwc2_hsotg_enqueue_setup(hsotg); |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2602 | return; |
| 2603 | } |
| 2604 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2605 | /* |
| 2606 | * Calculate the size of the transfer by checking how much is left |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2607 | * in the endpoint size register and then working it out from |
| 2608 | * the amount we loaded for the transfer. |
| 2609 | * |
| 2610 | * We do this even for DMA, as the transfer may have incremented |
| 2611 | * past the end of the buffer (DMA transfers are always 32bit |
| 2612 | * aligned). |
| 2613 | */ |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2614 | if (using_desc_dma(hsotg)) { |
| 2615 | size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); |
| 2616 | if (size_left < 0) |
| 2617 | dev_err(hsotg->dev, "error parsing DDMA results %d\n", |
| 2618 | size_left); |
| 2619 | } else { |
| 2620 | size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
| 2621 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2622 | |
| 2623 | size_done = hs_ep->size_loaded - size_left; |
| 2624 | size_done += hs_ep->last_load; |
| 2625 | |
| 2626 | if (hs_req->req.actual != size_done) |
| 2627 | dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n", |
| 2628 | __func__, hs_req->req.actual, size_done); |
| 2629 | |
| 2630 | hs_req->req.actual = size_done; |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2631 | dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n", |
| 2632 | hs_req->req.length, hs_req->req.actual, hs_req->req.zero); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2633 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2634 | if (!size_left && hs_req->req.actual < hs_req->req.length) { |
| 2635 | dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2636 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2637 | return; |
| 2638 | } |
| 2639 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 2640 | /* Zlp for all endpoints, for ep0 only in DATA IN stage */ |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2641 | if (hs_ep->send_zlp) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2642 | dwc2_hsotg_program_zlp(hsotg, hs_ep); |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2643 | hs_ep->send_zlp = 0; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 2644 | /* transfer will be completed on next complete interrupt */ |
| 2645 | return; |
| 2646 | } |
| 2647 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2648 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) { |
| 2649 | /* Move to STATUS OUT */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2650 | dwc2_hsotg_ep0_zlp(hsotg, false); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2651 | return; |
| 2652 | } |
| 2653 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2654 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | /** |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2658 | * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep |
| 2659 | * @hsotg: The device state. |
| 2660 | * @idx: Index of ep. |
| 2661 | * @dir_in: Endpoint direction 1-in 0-out. |
| 2662 | * |
| 2663 | * Reads for endpoint with given index and direction, by masking |
| 2664 | * epint_reg with coresponding mask. |
| 2665 | */ |
| 2666 | static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg, |
| 2667 | unsigned int idx, int dir_in) |
| 2668 | { |
| 2669 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 2670 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 2671 | u32 ints; |
| 2672 | u32 mask; |
| 2673 | u32 diepempmsk; |
| 2674 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2675 | mask = dwc2_readl(hsotg, epmsk_reg); |
| 2676 | diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK); |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2677 | mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0; |
| 2678 | mask |= DXEPINT_SETUP_RCVD; |
| 2679 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2680 | ints = dwc2_readl(hsotg, epint_reg); |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2681 | ints &= mask; |
| 2682 | return ints; |
| 2683 | } |
| 2684 | |
| 2685 | /** |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2686 | * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD |
| 2687 | * @hs_ep: The endpoint on which interrupt is asserted. |
| 2688 | * |
| 2689 | * This interrupt indicates that the endpoint has been disabled per the |
| 2690 | * application's request. |
| 2691 | * |
| 2692 | * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK, |
| 2693 | * in case of ISOC completes current request. |
| 2694 | * |
| 2695 | * For ISOC-OUT endpoints completes expired requests. If there is remaining |
| 2696 | * request starts it. |
| 2697 | */ |
| 2698 | static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep) |
| 2699 | { |
| 2700 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2701 | struct dwc2_hsotg_req *hs_req; |
| 2702 | unsigned char idx = hs_ep->index; |
| 2703 | int dir_in = hs_ep->dir_in; |
| 2704 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2705 | int dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2706 | |
| 2707 | dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__); |
| 2708 | |
| 2709 | if (dir_in) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2710 | int epctl = dwc2_readl(hsotg, epctl_reg); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2711 | |
| 2712 | dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index); |
| 2713 | |
| 2714 | if (hs_ep->isochronous) { |
| 2715 | dwc2_hsotg_complete_in(hsotg, hs_ep); |
| 2716 | return; |
| 2717 | } |
| 2718 | |
| 2719 | if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2720 | int dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2721 | |
| 2722 | dctl |= DCTL_CGNPINNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2723 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2724 | } |
| 2725 | return; |
| 2726 | } |
| 2727 | |
| 2728 | if (dctl & DCTL_GOUTNAKSTS) { |
| 2729 | dctl |= DCTL_CGOUTNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2730 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | if (!hs_ep->isochronous) |
| 2734 | return; |
| 2735 | |
| 2736 | if (list_empty(&hs_ep->queue)) { |
| 2737 | dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n", |
| 2738 | __func__, hs_ep); |
| 2739 | return; |
| 2740 | } |
| 2741 | |
| 2742 | do { |
| 2743 | hs_req = get_ep_head(hs_ep); |
| 2744 | if (hs_req) |
| 2745 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, |
| 2746 | -ENODATA); |
| 2747 | dwc2_gadget_incr_frame_num(hs_ep); |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 2748 | /* Update current frame number value. */ |
| 2749 | hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2750 | } while (dwc2_gadget_target_frame_elapsed(hs_ep)); |
| 2751 | |
| 2752 | dwc2_gadget_start_next_request(hs_ep); |
| 2753 | } |
| 2754 | |
| 2755 | /** |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2756 | * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 2757 | * @ep: The endpoint on which interrupt is asserted. |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2758 | * |
| 2759 | * This is starting point for ISOC-OUT transfer, synchronization done with |
| 2760 | * first out token received from host while corresponding EP is disabled. |
| 2761 | * |
| 2762 | * Device does not know initial frame in which out token will come. For this |
| 2763 | * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon |
| 2764 | * getting this interrupt SW starts calculation for next transfer frame. |
| 2765 | */ |
| 2766 | static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep) |
| 2767 | { |
| 2768 | struct dwc2_hsotg *hsotg = ep->parent; |
| 2769 | int dir_in = ep->dir_in; |
| 2770 | u32 doepmsk; |
| 2771 | |
| 2772 | if (dir_in || !ep->isochronous) |
| 2773 | return; |
| 2774 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2775 | if (using_desc_dma(hsotg)) { |
| 2776 | if (ep->target_frame == TARGET_FRAME_INITIAL) { |
| 2777 | /* Start first ISO Out */ |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2778 | ep->target_frame = hsotg->frame_number; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2779 | dwc2_gadget_start_isoc_ddma(ep); |
| 2780 | } |
| 2781 | return; |
| 2782 | } |
| 2783 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2784 | if (ep->interval > 1 && |
| 2785 | ep->target_frame == TARGET_FRAME_INITIAL) { |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2786 | u32 ctrl; |
| 2787 | |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2788 | ep->target_frame = hsotg->frame_number; |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2789 | dwc2_gadget_incr_frame_num(ep); |
| 2790 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2791 | ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index)); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2792 | if (ep->target_frame & 0x1) |
| 2793 | ctrl |= DXEPCTL_SETODDFR; |
| 2794 | else |
| 2795 | ctrl |= DXEPCTL_SETEVENFR; |
| 2796 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2797 | dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index)); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | dwc2_gadget_start_next_request(ep); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2801 | doepmsk = dwc2_readl(hsotg, DOEPMSK); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2802 | doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2803 | dwc2_writel(hsotg, doepmsk, DOEPMSK); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | /** |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 2807 | * dwc2_gadget_handle_nak - handle NAK interrupt |
| 2808 | * @hs_ep: The endpoint on which interrupt is asserted. |
| 2809 | * |
| 2810 | * This is starting point for ISOC-IN transfer, synchronization done with |
| 2811 | * first IN token received from host while corresponding EP is disabled. |
| 2812 | * |
| 2813 | * Device does not know when first one token will arrive from host. On first |
| 2814 | * token arrival HW generates 2 interrupts: 'in token received while FIFO empty' |
| 2815 | * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was |
| 2816 | * sent in response to that as there was no data in FIFO. SW is basing on this |
| 2817 | * interrupt to obtain frame in which token has come and then based on the |
| 2818 | * interval calculates next frame for transfer. |
| 2819 | */ |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2820 | static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep) |
| 2821 | { |
| 2822 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2823 | int dir_in = hs_ep->dir_in; |
| 2824 | |
| 2825 | if (!dir_in || !hs_ep->isochronous) |
| 2826 | return; |
| 2827 | |
| 2828 | if (hs_ep->target_frame == TARGET_FRAME_INITIAL) { |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2829 | |
| 2830 | if (using_desc_dma(hsotg)) { |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2831 | hs_ep->target_frame = hsotg->frame_number; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2832 | dwc2_gadget_incr_frame_num(hs_ep); |
Grigor Tovmasyan | 48dac4e | 2018-08-29 21:00:33 +0400 | [diff] [blame^] | 2833 | |
| 2834 | /* In service interval mode target_frame must |
| 2835 | * be set to last (u)frame of the service interval. |
| 2836 | */ |
| 2837 | if (hsotg->params.service_interval) { |
| 2838 | /* Set target_frame to the first (u)frame of |
| 2839 | * the service interval |
| 2840 | */ |
| 2841 | hs_ep->target_frame &= ~hs_ep->interval + 1; |
| 2842 | |
| 2843 | /* Set target_frame to the last (u)frame of |
| 2844 | * the service interval |
| 2845 | */ |
| 2846 | dwc2_gadget_incr_frame_num(hs_ep); |
| 2847 | dwc2_gadget_dec_frame_num_by_one(hs_ep); |
| 2848 | } |
| 2849 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2850 | dwc2_gadget_start_isoc_ddma(hs_ep); |
| 2851 | return; |
| 2852 | } |
| 2853 | |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2854 | hs_ep->target_frame = hsotg->frame_number; |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2855 | if (hs_ep->interval > 1) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2856 | u32 ctrl = dwc2_readl(hsotg, |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2857 | DIEPCTL(hs_ep->index)); |
| 2858 | if (hs_ep->target_frame & 0x1) |
| 2859 | ctrl |= DXEPCTL_SETODDFR; |
| 2860 | else |
| 2861 | ctrl |= DXEPCTL_SETEVENFR; |
| 2862 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2863 | dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index)); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | dwc2_hsotg_complete_request(hsotg, hs_ep, |
| 2867 | get_ep_head(hs_ep), 0); |
| 2868 | } |
| 2869 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2870 | if (!using_desc_dma(hsotg)) |
| 2871 | dwc2_gadget_incr_frame_num(hs_ep); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2875 | * dwc2_hsotg_epint - handle an in/out endpoint interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2876 | * @hsotg: The driver state |
| 2877 | * @idx: The index for the endpoint (0..15) |
| 2878 | * @dir_in: Set if this is an IN endpoint |
| 2879 | * |
| 2880 | * Process and clear any interrupt pending for an individual endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2881 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2882 | static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2883 | int dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2884 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2885 | 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] | 2886 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 2887 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
| 2888 | u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2889 | u32 ints; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2890 | u32 ctrl; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2891 | |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2892 | ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2893 | ctrl = dwc2_readl(hsotg, epctl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2894 | |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2895 | /* Clear endpoint interrupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2896 | dwc2_writel(hsotg, ints, epint_reg); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2897 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2898 | if (!hs_ep) { |
| 2899 | dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2900 | __func__, idx, dir_in ? "in" : "out"); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2901 | return; |
| 2902 | } |
| 2903 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2904 | dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n", |
| 2905 | __func__, idx, dir_in ? "in" : "out", ints); |
| 2906 | |
Mian Yousaf Kaukab | b787d75 | 2015-01-09 13:38:43 +0100 | [diff] [blame] | 2907 | /* Don't process XferCompl interrupt if it is a setup packet */ |
| 2908 | if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD))) |
| 2909 | ints &= ~DXEPINT_XFERCOMPL; |
| 2910 | |
Vahram Aharonyan | f0afdb4 | 2016-11-14 19:16:48 -0800 | [diff] [blame] | 2911 | /* |
| 2912 | * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP |
| 2913 | * stage and xfercomplete was generated without SETUP phase done |
| 2914 | * interrupt. SW should parse received setup packet only after host's |
| 2915 | * exit from setup phase of control transfer. |
| 2916 | */ |
| 2917 | if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in && |
| 2918 | hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP)) |
| 2919 | ints &= ~DXEPINT_XFERCOMPL; |
| 2920 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2921 | if (ints & DXEPINT_XFERCOMPL) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2922 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2923 | "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2924 | __func__, dwc2_readl(hsotg, epctl_reg), |
| 2925 | dwc2_readl(hsotg, epsiz_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2926 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2927 | /* In DDMA handle isochronous requests separately */ |
| 2928 | if (using_desc_dma(hsotg) && hs_ep->isochronous) { |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2929 | /* XferCompl set along with BNA */ |
| 2930 | if (!(ints & DXEPINT_BNAINTR)) |
| 2931 | dwc2_gadget_complete_isoc_request_ddma(hs_ep); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2932 | } else if (dir_in) { |
| 2933 | /* |
| 2934 | * We get OutDone from the FIFO, so we only |
| 2935 | * need to look at completing IN requests here |
| 2936 | * if operating slave mode |
| 2937 | */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2938 | if (hs_ep->isochronous && hs_ep->interval > 1) |
| 2939 | dwc2_gadget_incr_frame_num(hs_ep); |
| 2940 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2941 | dwc2_hsotg_complete_in(hsotg, hs_ep); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2942 | if (ints & DXEPINT_NAKINTRPT) |
| 2943 | ints &= ~DXEPINT_NAKINTRPT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2944 | |
Ben Dooks | c9a64ea | 2010-07-19 09:40:46 +0100 | [diff] [blame] | 2945 | if (idx == 0 && !hs_ep->req) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2946 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2947 | } else if (using_dma(hsotg)) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2948 | /* |
| 2949 | * We're using DMA, we need to fire an OutDone here |
| 2950 | * as we ignore the RXFIFO. |
| 2951 | */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2952 | if (hs_ep->isochronous && hs_ep->interval > 1) |
| 2953 | dwc2_gadget_incr_frame_num(hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2954 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2955 | dwc2_hsotg_handle_outdone(hsotg, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2956 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2957 | } |
| 2958 | |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2959 | if (ints & DXEPINT_EPDISBLD) |
| 2960 | dwc2_gadget_handle_ep_disabled(hs_ep); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2961 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2962 | if (ints & DXEPINT_OUTTKNEPDIS) |
| 2963 | dwc2_gadget_handle_out_token_ep_disabled(hs_ep); |
| 2964 | |
| 2965 | if (ints & DXEPINT_NAKINTRPT) |
| 2966 | dwc2_gadget_handle_nak(hs_ep); |
| 2967 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2968 | if (ints & DXEPINT_AHBERR) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2969 | dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2970 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2971 | if (ints & DXEPINT_SETUP) { /* Setup or Timeout */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2972 | dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__); |
| 2973 | |
| 2974 | if (using_dma(hsotg) && idx == 0) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2975 | /* |
| 2976 | * this is the notification we've received a |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2977 | * setup packet. In non-DMA mode we'd get this |
| 2978 | * from the RXFIFO, instead we need to process |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2979 | * the setup here. |
| 2980 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2981 | |
| 2982 | if (dir_in) |
| 2983 | WARN_ON_ONCE(1); |
| 2984 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2985 | dwc2_hsotg_handle_outdone(hsotg, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2986 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2987 | } |
| 2988 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 2989 | if (ints & DXEPINT_STSPHSERCVD) { |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 2990 | dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__); |
| 2991 | |
Minas Harutyunyan | 9e95a66 | 2018-01-16 16:03:58 +0400 | [diff] [blame] | 2992 | /* Safety check EP0 state when STSPHSERCVD asserted */ |
| 2993 | if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) { |
| 2994 | /* Move to STATUS IN for DDMA */ |
| 2995 | if (using_desc_dma(hsotg)) |
| 2996 | dwc2_hsotg_ep0_zlp(hsotg, true); |
| 2997 | } |
| 2998 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 2999 | } |
| 3000 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3001 | if (ints & DXEPINT_BACK2BACKSETUP) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3002 | dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3003 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3004 | if (ints & DXEPINT_BNAINTR) { |
| 3005 | dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3006 | if (hs_ep->isochronous) |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3007 | dwc2_gadget_handle_isoc_bna(hs_ep); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3008 | } |
| 3009 | |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3010 | if (dir_in && !hs_ep->isochronous) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3011 | /* not sure if this is important, but we'll clear it anyway */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3012 | if (ints & DXEPINT_INTKNTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3013 | dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n", |
| 3014 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | /* this probably means something bad is happening */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3018 | if (ints & DXEPINT_INTKNEPMIS) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3019 | dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n", |
| 3020 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3021 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3022 | |
| 3023 | /* FIFO has space or is empty (see GAHBCFG) */ |
| 3024 | if (hsotg->dedicated_fifos && |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3025 | ints & DXEPINT_TXFEMP) { |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3026 | dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n", |
| 3027 | __func__, idx); |
Anton Tikhomirov | 70fa030 | 2012-03-06 14:08:29 +0900 | [diff] [blame] | 3028 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3029 | dwc2_hsotg_trytx(hsotg, hs_ep); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3030 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3031 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3035 | * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3036 | * @hsotg: The device state. |
| 3037 | * |
| 3038 | * Handle updating the device settings after the enumeration phase has |
| 3039 | * been completed. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3040 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3041 | static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3042 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3043 | u32 dsts = dwc2_readl(hsotg, DSTS); |
Jingoo Han | 9b2667f | 2014-08-20 12:04:09 +0900 | [diff] [blame] | 3044 | int ep0_mps = 0, ep_mps = 8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3045 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3046 | /* |
| 3047 | * This should signal the finish of the enumeration phase |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3048 | * of the USB handshaking, so we should now know what rate |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3049 | * we connected at. |
| 3050 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3051 | |
| 3052 | dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts); |
| 3053 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3054 | /* |
| 3055 | * 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] | 3056 | * 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] | 3057 | * not advertise a 64byte MPS on EP0. |
| 3058 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3059 | |
| 3060 | /* catch both EnumSpd_FS and EnumSpd_FS48 */ |
Marek Vasut | 6d76c92 | 2015-12-18 03:26:17 +0100 | [diff] [blame] | 3061 | switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3062 | case DSTS_ENUMSPD_FS: |
| 3063 | case DSTS_ENUMSPD_FS48: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3064 | hsotg->gadget.speed = USB_SPEED_FULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3065 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 3066 | ep_mps = 1023; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3067 | break; |
| 3068 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3069 | case DSTS_ENUMSPD_HS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3070 | hsotg->gadget.speed = USB_SPEED_HIGH; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3071 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 3072 | ep_mps = 1024; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3073 | break; |
| 3074 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3075 | case DSTS_ENUMSPD_LS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3076 | hsotg->gadget.speed = USB_SPEED_LOW; |
Vardan Mikayelyan | 552d940 | 2016-11-14 19:17:00 -0800 | [diff] [blame] | 3077 | ep0_mps = 8; |
| 3078 | ep_mps = 8; |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3079 | /* |
| 3080 | * note, we don't actually support LS in this driver at the |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3081 | * moment, and the documentation seems to imply that it isn't |
| 3082 | * supported by the PHYs on some of the devices. |
| 3083 | */ |
| 3084 | break; |
| 3085 | } |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 3086 | dev_info(hsotg->dev, "new device is %s\n", |
| 3087 | usb_speed_string(hsotg->gadget.speed)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3088 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3089 | /* |
| 3090 | * we should now know the maximum packet size for an |
| 3091 | * endpoint, so set the endpoints to a default value. |
| 3092 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3093 | |
| 3094 | if (ep0_mps) { |
| 3095 | int i; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3096 | /* Initialize ep0 for both in and out directions */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3097 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1); |
| 3098 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3099 | for (i = 1; i < hsotg->num_of_eps; i++) { |
| 3100 | if (hsotg->eps_in[i]) |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3101 | dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, |
| 3102 | 0, 1); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3103 | if (hsotg->eps_out[i]) |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3104 | dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, |
| 3105 | 0, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3106 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | /* ensure after enumeration our EP0 is active */ |
| 3110 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3111 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3112 | |
| 3113 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3114 | dwc2_readl(hsotg, DIEPCTL0), |
| 3115 | dwc2_readl(hsotg, DOEPCTL0)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | /** |
| 3119 | * kill_all_requests - remove all requests from the endpoint's queue |
| 3120 | * @hsotg: The device state. |
| 3121 | * @ep: The endpoint the requests may be on. |
| 3122 | * @result: The result code to use. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3123 | * |
| 3124 | * Go through the requests on the given endpoint and mark them |
| 3125 | * completed with the given result code. |
| 3126 | */ |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3127 | static void kill_all_requests(struct dwc2_hsotg *hsotg, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3128 | struct dwc2_hsotg_ep *ep, |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3129 | int result) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3130 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3131 | struct dwc2_hsotg_req *req, *treq; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3132 | unsigned int size; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3133 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3134 | ep->req = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3135 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3136 | list_for_each_entry_safe(req, treq, &ep->queue, queue) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3137 | dwc2_hsotg_complete_request(hsotg, ep, req, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3138 | result); |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3139 | |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3140 | if (!hsotg->dedicated_fifos) |
| 3141 | return; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3142 | size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3143 | if (size < ep->fifo_size) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3144 | dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3145 | } |
| 3146 | |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3147 | static int dwc2_hsotg_ep_disable(struct usb_ep *ep); |
| 3148 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3149 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3150 | * dwc2_hsotg_disconnect - disconnect service |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3151 | * @hsotg: The device state. |
| 3152 | * |
Lukasz Majewski | 5e89134 | 2012-05-04 14:17:07 +0200 | [diff] [blame] | 3153 | * The device has been disconnected. Remove all current |
| 3154 | * transactions and signal the gadget driver that this |
| 3155 | * has happened. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3156 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3157 | void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3158 | { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3159 | unsigned int ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3160 | |
Marek Szyprowski | 4ace06e | 2014-11-21 15:14:47 +0100 | [diff] [blame] | 3161 | if (!hsotg->connected) |
| 3162 | return; |
| 3163 | |
| 3164 | hsotg->connected = 0; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 3165 | hsotg->test_mode = 0; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3166 | |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3167 | /* all endpoints should be shutdown */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3168 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 3169 | if (hsotg->eps_in[ep]) |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3170 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3171 | if (hsotg->eps_out[ep]) |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3172 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3173 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3174 | |
| 3175 | call_gadget(hsotg, disconnect); |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 3176 | hsotg->lx_state = DWC2_L3; |
John Stultz | ce2b21a | 2017-10-23 14:32:50 -0700 | [diff] [blame] | 3177 | |
| 3178 | usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3182 | * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3183 | * @hsotg: The device state: |
| 3184 | * @periodic: True if this is a periodic FIFO interrupt |
| 3185 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3186 | static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3187 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3188 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3189 | int epno, ret; |
| 3190 | |
| 3191 | /* look through for any more data to transmit */ |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3192 | for (epno = 0; epno < hsotg->num_of_eps; epno++) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3193 | ep = index_to_ep(hsotg, epno, 1); |
| 3194 | |
| 3195 | if (!ep) |
| 3196 | continue; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3197 | |
| 3198 | if (!ep->dir_in) |
| 3199 | continue; |
| 3200 | |
| 3201 | if ((periodic && !ep->periodic) || |
| 3202 | (!periodic && ep->periodic)) |
| 3203 | continue; |
| 3204 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3205 | ret = dwc2_hsotg_trytx(hsotg, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3206 | if (ret < 0) |
| 3207 | break; |
| 3208 | } |
| 3209 | } |
| 3210 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3211 | /* IRQ flags which will trigger a retry around the IRQ loop */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3212 | #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \ |
| 3213 | GINTSTS_PTXFEMP | \ |
| 3214 | GINTSTS_RXFLVL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3215 | |
| 3216 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3217 | * dwc2_hsotg_core_init - issue softreset to the core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3218 | * @hsotg: The device state |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 3219 | * @is_usb_reset: Usb resetting flag |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3220 | * |
| 3221 | * Issue a soft reset to the core, and await the core finishing it. |
| 3222 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3223 | void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3224 | bool is_usb_reset) |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3225 | { |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3226 | u32 intmsk; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3227 | u32 val; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3228 | u32 usbcfg; |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3229 | u32 dcfg = 0; |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3230 | int ep; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3231 | |
Mian Yousaf Kaukab | 5390d43 | 2015-09-29 12:08:25 +0200 | [diff] [blame] | 3232 | /* Kill any ep0 requests as controller will be reinitialized */ |
| 3233 | kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET); |
| 3234 | |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3235 | if (!is_usb_reset) { |
John Stultz | 6e6360b | 2017-01-23 14:59:14 -0800 | [diff] [blame] | 3236 | if (dwc2_core_reset(hsotg, true)) |
Gregory Herrero | 86de489 | 2015-09-29 12:08:21 +0200 | [diff] [blame] | 3237 | return; |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3238 | } else { |
| 3239 | /* all endpoints should be shutdown */ |
| 3240 | for (ep = 1; ep < hsotg->num_of_eps; ep++) { |
| 3241 | if (hsotg->eps_in[ep]) |
| 3242 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
| 3243 | if (hsotg->eps_out[ep]) |
| 3244 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
| 3245 | } |
| 3246 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3247 | |
| 3248 | /* |
| 3249 | * we must now enable ep0 ready for host detection and then |
| 3250 | * set configuration. |
| 3251 | */ |
| 3252 | |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3253 | /* keep other bits untouched (so e.g. forced modes are not lost) */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3254 | usbcfg = dwc2_readl(hsotg, GUSBCFG); |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3255 | usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP | |
Amelie Delaunay | ca02954 | 2017-01-12 16:09:44 +0100 | [diff] [blame] | 3256 | GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK); |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3257 | |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3258 | if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS && |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3259 | (hsotg->params.speed == DWC2_SPEED_PARAM_FULL || |
| 3260 | hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) { |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3261 | /* FS/LS Dedicated Transceiver Interface */ |
| 3262 | usbcfg |= GUSBCFG_PHYSEL; |
| 3263 | } else { |
| 3264 | /* set the PLL on, remove the HNP/SRP and set the PHY */ |
| 3265 | val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5; |
| 3266 | usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) | |
| 3267 | (val << GUSBCFG_USBTRDTIM_SHIFT); |
| 3268 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3269 | dwc2_writel(hsotg, usbcfg, GUSBCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3270 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3271 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3272 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3273 | if (!is_usb_reset) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3274 | dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3275 | |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3276 | dcfg |= DCFG_EPMISCNT(1); |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3277 | |
| 3278 | switch (hsotg->params.speed) { |
| 3279 | case DWC2_SPEED_PARAM_LOW: |
| 3280 | dcfg |= DCFG_DEVSPD_LS; |
| 3281 | break; |
| 3282 | case DWC2_SPEED_PARAM_FULL: |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3283 | if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) |
| 3284 | dcfg |= DCFG_DEVSPD_FS48; |
| 3285 | else |
| 3286 | dcfg |= DCFG_DEVSPD_FS; |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3287 | break; |
| 3288 | default: |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3289 | dcfg |= DCFG_DEVSPD_HS; |
| 3290 | } |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3291 | |
Grigor Tovmasyan | b43ebc9 | 2018-05-05 12:17:58 +0400 | [diff] [blame] | 3292 | if (hsotg->params.ipg_isoc_en) |
| 3293 | dcfg |= DCFG_IPG_ISOC_SUPPORDED; |
| 3294 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3295 | dwc2_writel(hsotg, dcfg, DCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3296 | |
| 3297 | /* Clear any pending OTG interrupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3298 | dwc2_writel(hsotg, 0xffffffff, GOTGINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3299 | |
| 3300 | /* Clear any pending interrupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3301 | dwc2_writel(hsotg, 0xffffffff, GINTSTS); |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3302 | intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3303 | GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF | |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3304 | GINTSTS_USBRST | GINTSTS_RESETDET | |
| 3305 | GINTSTS_ENUMDONE | GINTSTS_OTGINT | |
Sevak Arakelyan | 376f040 | 2018-01-24 17:43:06 +0400 | [diff] [blame] | 3306 | GINTSTS_USBSUSP | GINTSTS_WKUPINT | |
| 3307 | GINTSTS_LPMTRANRCVD; |
Vahram Aharonyan | f473670 | 2016-11-14 19:16:38 -0800 | [diff] [blame] | 3308 | |
| 3309 | if (!using_desc_dma(hsotg)) |
| 3310 | intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT; |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3311 | |
John Youn | 95832c0 | 2017-01-23 14:57:26 -0800 | [diff] [blame] | 3312 | if (!hsotg->params.external_id_pin_ctl) |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3313 | intmsk |= GINTSTS_CONIDSTSCHNG; |
| 3314 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3315 | dwc2_writel(hsotg, intmsk, GINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3316 | |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3317 | if (using_dma(hsotg)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3318 | dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN | |
Razmik Karapetyan | d1ac8c8 | 2018-01-19 14:39:57 +0400 | [diff] [blame] | 3319 | hsotg->params.ahbcfg, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3320 | GAHBCFG); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3321 | |
| 3322 | /* Set DDMA mode support in the core if needed */ |
| 3323 | if (using_desc_dma(hsotg)) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3324 | dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3325 | |
| 3326 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3327 | dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ? |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3328 | (GAHBCFG_NP_TXF_EMP_LVL | |
| 3329 | GAHBCFG_P_TXF_EMP_LVL) : 0) | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3330 | GAHBCFG_GLBL_INTR_EN, GAHBCFG); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3331 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3332 | |
| 3333 | /* |
Robert Baldyga | 8acc829 | 2013-09-19 11:50:23 +0200 | [diff] [blame] | 3334 | * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts |
| 3335 | * when we have no data to transfer. Otherwise we get being flooded by |
| 3336 | * interrupts. |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3337 | */ |
| 3338 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3339 | dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ? |
Mian Yousaf Kaukab | 6ff2e83 | 2015-01-09 13:38:42 +0100 | [diff] [blame] | 3340 | DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3341 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3342 | DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3343 | DIEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3344 | |
| 3345 | /* |
| 3346 | * 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] | 3347 | * DMA mode we may need this and StsPhseRcvd. |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3348 | */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3349 | dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK | |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3350 | DOEPMSK_STSPHSERCVDMSK) : 0) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3351 | DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK | |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3352 | DOEPMSK_SETUPMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3353 | DOEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3354 | |
Vahram Aharonyan | ec01f0b | 2016-11-14 19:16:43 -0800 | [diff] [blame] | 3355 | /* Enable BNA interrupt for DDMA */ |
Minas Harutyunyan | 37981e0 | 2018-05-03 17:25:37 +0400 | [diff] [blame] | 3356 | if (using_desc_dma(hsotg)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3357 | dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK); |
| 3358 | dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK); |
Minas Harutyunyan | 37981e0 | 2018-05-03 17:25:37 +0400 | [diff] [blame] | 3359 | } |
Vahram Aharonyan | ec01f0b | 2016-11-14 19:16:43 -0800 | [diff] [blame] | 3360 | |
Grigor Tovmasyan | ca531bc | 2018-08-29 20:59:34 +0400 | [diff] [blame] | 3361 | /* Enable Service Interval mode if supported */ |
| 3362 | if (using_desc_dma(hsotg) && hsotg->params.service_interval) |
| 3363 | dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED); |
| 3364 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3365 | dwc2_writel(hsotg, 0, DAINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3366 | |
| 3367 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3368 | dwc2_readl(hsotg, DIEPCTL0), |
| 3369 | dwc2_readl(hsotg, DOEPCTL0)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3370 | |
| 3371 | /* enable in and out endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3372 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3373 | |
| 3374 | /* |
| 3375 | * Enable the RXFIFO when in slave mode, as this is how we collect |
| 3376 | * the data. In DMA mode, we get events from the FIFO but also |
| 3377 | * things we cannot process, so do not use it. |
| 3378 | */ |
| 3379 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3380 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3381 | |
| 3382 | /* Enable interrupts for EP0 in and out */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3383 | dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1); |
| 3384 | dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3385 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3386 | if (!is_usb_reset) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3387 | dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE); |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3388 | udelay(10); /* see openiboot */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3389 | dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE); |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3390 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3391 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3392 | dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3393 | |
| 3394 | /* |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3395 | * DxEPCTL_USBActEp says RO in manual, but seems to be set by |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3396 | * writing to the EPCTL register.. |
| 3397 | */ |
| 3398 | |
| 3399 | /* set to read 1 8byte packet */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3400 | dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
| 3401 | DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3402 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3403 | dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3404 | DXEPCTL_CNAK | DXEPCTL_EPENA | |
| 3405 | DXEPCTL_USBACTEP, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3406 | DOEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3407 | |
| 3408 | /* enable, but don't activate EP0in */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3409 | dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | |
| 3410 | DXEPCTL_USBACTEP, DIEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3411 | |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3412 | /* clear global NAKs */ |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3413 | val = DCTL_CGOUTNAK | DCTL_CGNPINNAK; |
| 3414 | if (!is_usb_reset) |
| 3415 | val |= DCTL_SFTDISCON; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3416 | dwc2_set_bit(hsotg, DCTL, val); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3417 | |
Sevak Arakelyan | 21b0340 | 2018-01-24 17:43:32 +0400 | [diff] [blame] | 3418 | /* configure the core to support LPM */ |
| 3419 | dwc2_gadget_init_lpm(hsotg); |
| 3420 | |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3421 | /* must be at-least 3ms to allow bus to see disconnect */ |
| 3422 | mdelay(3); |
| 3423 | |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 3424 | hsotg->lx_state = DWC2_L0; |
Vardan Mikayelyan | 755d739 | 2018-01-16 16:04:24 +0400 | [diff] [blame] | 3425 | |
| 3426 | dwc2_hsotg_enqueue_setup(hsotg); |
| 3427 | |
| 3428 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3429 | dwc2_readl(hsotg, DIEPCTL0), |
| 3430 | dwc2_readl(hsotg, DOEPCTL0)); |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3431 | } |
Marek Szyprowski | ac3c81f | 2014-10-20 12:45:35 +0200 | [diff] [blame] | 3432 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3433 | static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3434 | { |
| 3435 | /* set the soft-disconnect bit */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3436 | dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3437 | } |
| 3438 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3439 | void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3440 | { |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3441 | /* remove the soft-disconnect and let's go */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3442 | dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | /** |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3446 | * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt. |
| 3447 | * @hsotg: The device state: |
| 3448 | * |
| 3449 | * This interrupt indicates one of the following conditions occurred while |
| 3450 | * transmitting an ISOC transaction. |
| 3451 | * - Corrupted IN Token for ISOC EP. |
| 3452 | * - Packet not complete in FIFO. |
| 3453 | * |
| 3454 | * The following actions will be taken: |
| 3455 | * - Determine the EP |
| 3456 | * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO |
| 3457 | */ |
| 3458 | static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg) |
| 3459 | { |
| 3460 | struct dwc2_hsotg_ep *hs_ep; |
| 3461 | u32 epctrl; |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3462 | u32 daintmsk; |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3463 | u32 idx; |
| 3464 | |
| 3465 | dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n"); |
| 3466 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3467 | daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3468 | |
Artur Petrosyan | d5d5f07 | 2018-05-05 04:30:16 -0400 | [diff] [blame] | 3469 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3470 | hs_ep = hsotg->eps_in[idx]; |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3471 | /* Proceed only unmasked ISOC EPs */ |
John Keeping | 89066b3 | 2018-07-15 15:50:43 +0100 | [diff] [blame] | 3472 | if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous) |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3473 | continue; |
| 3474 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3475 | epctrl = dwc2_readl(hsotg, DIEPCTL(idx)); |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3476 | if ((epctrl & DXEPCTL_EPENA) && |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3477 | dwc2_gadget_target_frame_elapsed(hs_ep)) { |
| 3478 | epctrl |= DXEPCTL_SNAK; |
| 3479 | epctrl |= DXEPCTL_EPDIS; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3480 | dwc2_writel(hsotg, epctrl, DIEPCTL(idx)); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | /* Clear interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3485 | dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | /** |
| 3489 | * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt |
| 3490 | * @hsotg: The device state: |
| 3491 | * |
| 3492 | * This interrupt indicates one of the following conditions occurred while |
| 3493 | * transmitting an ISOC transaction. |
| 3494 | * - Corrupted OUT Token for ISOC EP. |
| 3495 | * - Packet not complete in FIFO. |
| 3496 | * |
| 3497 | * The following actions will be taken: |
| 3498 | * - Determine the EP |
| 3499 | * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed. |
| 3500 | */ |
| 3501 | static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg) |
| 3502 | { |
| 3503 | u32 gintsts; |
| 3504 | u32 gintmsk; |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3505 | u32 daintmsk; |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3506 | u32 epctrl; |
| 3507 | struct dwc2_hsotg_ep *hs_ep; |
| 3508 | int idx; |
| 3509 | |
| 3510 | dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__); |
| 3511 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3512 | daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3513 | daintmsk >>= DAINT_OUTEP_SHIFT; |
| 3514 | |
Artur Petrosyan | d5d5f07 | 2018-05-05 04:30:16 -0400 | [diff] [blame] | 3515 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3516 | hs_ep = hsotg->eps_out[idx]; |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3517 | /* Proceed only unmasked ISOC EPs */ |
John Keeping | 89066b3 | 2018-07-15 15:50:43 +0100 | [diff] [blame] | 3518 | if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous) |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3519 | continue; |
| 3520 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3521 | epctrl = dwc2_readl(hsotg, DOEPCTL(idx)); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3522 | if ((epctrl & DXEPCTL_EPENA) && |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3523 | dwc2_gadget_target_frame_elapsed(hs_ep)) { |
| 3524 | /* Unmask GOUTNAKEFF interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3525 | gintmsk = dwc2_readl(hsotg, GINTMSK); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3526 | gintmsk |= GINTSTS_GOUTNAKEFF; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3527 | dwc2_writel(hsotg, gintmsk, GINTMSK); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3528 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3529 | gintsts = dwc2_readl(hsotg, GINTSTS); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3530 | if (!(gintsts & GINTSTS_GOUTNAKEFF)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3531 | dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3532 | break; |
| 3533 | } |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | /* Clear interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3538 | dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3542 | * dwc2_hsotg_irq - handle device interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3543 | * @irq: The IRQ number triggered |
| 3544 | * @pw: The pw value when registered the handler. |
| 3545 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3546 | static irqreturn_t dwc2_hsotg_irq(int irq, void *pw) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3547 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3548 | struct dwc2_hsotg *hsotg = pw; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3549 | int retry_count = 8; |
| 3550 | u32 gintsts; |
| 3551 | u32 gintmsk; |
| 3552 | |
Vardan Mikayelyan | ee3de8d | 2016-04-27 20:20:48 -0700 | [diff] [blame] | 3553 | if (!dwc2_is_device_mode(hsotg)) |
| 3554 | return IRQ_NONE; |
| 3555 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3556 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3557 | irq_retry: |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3558 | gintsts = dwc2_readl(hsotg, GINTSTS); |
| 3559 | gintmsk = dwc2_readl(hsotg, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3560 | |
| 3561 | dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n", |
| 3562 | __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count); |
| 3563 | |
| 3564 | gintsts &= gintmsk; |
| 3565 | |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3566 | if (gintsts & GINTSTS_RESETDET) { |
| 3567 | dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__); |
| 3568 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3569 | dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3570 | |
| 3571 | /* This event must be used only if controller is suspended */ |
| 3572 | if (hsotg->lx_state == DWC2_L2) { |
Vardan Mikayelyan | 41ba9b9 | 2018-02-16 14:06:36 +0400 | [diff] [blame] | 3573 | dwc2_exit_partial_power_down(hsotg, true); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3574 | hsotg->lx_state = DWC2_L0; |
| 3575 | } |
| 3576 | } |
| 3577 | |
| 3578 | if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3579 | u32 usb_status = dwc2_readl(hsotg, GOTGCTL); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3580 | u32 connected = hsotg->connected; |
| 3581 | |
| 3582 | dev_dbg(hsotg->dev, "%s: USBRst\n", __func__); |
| 3583 | dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3584 | dwc2_readl(hsotg, GNPTXSTS)); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3585 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3586 | dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3587 | |
| 3588 | /* Report disconnection if it is not already done. */ |
| 3589 | dwc2_hsotg_disconnect(hsotg); |
| 3590 | |
Minas Harutyunyan | 307bc11 | 2017-07-11 14:25:13 +0400 | [diff] [blame] | 3591 | /* Reset device address to zero */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3592 | dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK); |
Minas Harutyunyan | 307bc11 | 2017-07-11 14:25:13 +0400 | [diff] [blame] | 3593 | |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3594 | if (usb_status & GOTGCTL_BSESVLD && connected) |
| 3595 | dwc2_hsotg_core_init_disconnected(hsotg, true); |
| 3596 | } |
| 3597 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3598 | if (gintsts & GINTSTS_ENUMDONE) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3599 | dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3600 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3601 | dwc2_hsotg_irq_enumdone(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3602 | } |
| 3603 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3604 | if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3605 | u32 daint = dwc2_readl(hsotg, DAINT); |
| 3606 | u32 daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3607 | u32 daint_out, daint_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3608 | int ep; |
| 3609 | |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3610 | daint &= daintmsk; |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3611 | daint_out = daint >> DAINT_OUTEP_SHIFT; |
| 3612 | daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3613 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3614 | dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint); |
| 3615 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 3616 | for (ep = 0; ep < hsotg->num_of_eps && daint_out; |
| 3617 | ep++, daint_out >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3618 | if (daint_out & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3619 | dwc2_hsotg_epint(hsotg, ep, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3620 | } |
| 3621 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 3622 | for (ep = 0; ep < hsotg->num_of_eps && daint_in; |
| 3623 | ep++, daint_in >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3624 | if (daint_in & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3625 | dwc2_hsotg_epint(hsotg, ep, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3626 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3627 | } |
| 3628 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3629 | /* check both FIFOs */ |
| 3630 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3631 | if (gintsts & GINTSTS_NPTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3632 | dev_dbg(hsotg->dev, "NPTxFEmp\n"); |
| 3633 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3634 | /* |
| 3635 | * Disable the interrupt to stop it happening again |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3636 | * unless one of these endpoint routines decides that |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3637 | * it needs re-enabling |
| 3638 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3639 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3640 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP); |
| 3641 | dwc2_hsotg_irq_fifoempty(hsotg, false); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3642 | } |
| 3643 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3644 | if (gintsts & GINTSTS_PTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3645 | dev_dbg(hsotg->dev, "PTxFEmp\n"); |
| 3646 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3647 | /* See note in GINTSTS_NPTxFEmp */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3648 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3649 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP); |
| 3650 | dwc2_hsotg_irq_fifoempty(hsotg, true); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3651 | } |
| 3652 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3653 | if (gintsts & GINTSTS_RXFLVL) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3654 | /* |
| 3655 | * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3656 | * we need to retry dwc2_hsotg_handle_rx if this is still |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3657 | * set. |
| 3658 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3659 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3660 | dwc2_hsotg_handle_rx(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3661 | } |
| 3662 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3663 | if (gintsts & GINTSTS_ERLYSUSP) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3664 | dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n"); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3665 | dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3666 | } |
| 3667 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3668 | /* |
| 3669 | * these next two seem to crop-up occasionally causing the core |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3670 | * to shutdown the USB transfer, so try clearing them and logging |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3671 | * the occurrence. |
| 3672 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3673 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3674 | if (gintsts & GINTSTS_GOUTNAKEFF) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3675 | u8 idx; |
| 3676 | u32 epctrl; |
| 3677 | u32 gintmsk; |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3678 | u32 daintmsk; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3679 | struct dwc2_hsotg_ep *hs_ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3680 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3681 | daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3682 | daintmsk >>= DAINT_OUTEP_SHIFT; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3683 | /* Mask this interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3684 | gintmsk = dwc2_readl(hsotg, GINTMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3685 | gintmsk &= ~GINTSTS_GOUTNAKEFF; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3686 | dwc2_writel(hsotg, gintmsk, GINTMSK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3687 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3688 | dev_dbg(hsotg->dev, "GOUTNakEff triggered\n"); |
Artur Petrosyan | d5d5f07 | 2018-05-05 04:30:16 -0400 | [diff] [blame] | 3689 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3690 | hs_ep = hsotg->eps_out[idx]; |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3691 | /* Proceed only unmasked ISOC EPs */ |
John Keeping | 89066b3 | 2018-07-15 15:50:43 +0100 | [diff] [blame] | 3692 | if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous) |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3693 | continue; |
| 3694 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3695 | epctrl = dwc2_readl(hsotg, DOEPCTL(idx)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3696 | |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3697 | if (epctrl & DXEPCTL_EPENA) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3698 | epctrl |= DXEPCTL_SNAK; |
| 3699 | epctrl |= DXEPCTL_EPDIS; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3700 | dwc2_writel(hsotg, epctrl, DOEPCTL(idx)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3705 | } |
| 3706 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3707 | if (gintsts & GINTSTS_GINNAKEFF) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3708 | dev_info(hsotg->dev, "GINNakEff triggered\n"); |
| 3709 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3710 | dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3711 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3712 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3713 | } |
| 3714 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3715 | if (gintsts & GINTSTS_INCOMPL_SOIN) |
| 3716 | dwc2_gadget_handle_incomplete_isoc_in(hsotg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 3717 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3718 | if (gintsts & GINTSTS_INCOMPL_SOOUT) |
| 3719 | dwc2_gadget_handle_incomplete_isoc_out(hsotg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 3720 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3721 | /* |
| 3722 | * if we've had fifo events, we should try and go around the |
| 3723 | * loop again to see if there's any point in returning yet. |
| 3724 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3725 | |
| 3726 | if (gintsts & IRQ_RETRY_MASK && --retry_count > 0) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 3727 | goto irq_retry; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3728 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3729 | spin_unlock(&hsotg->lock); |
| 3730 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3731 | return IRQ_HANDLED; |
| 3732 | } |
| 3733 | |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3734 | static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg, |
| 3735 | struct dwc2_hsotg_ep *hs_ep) |
| 3736 | { |
| 3737 | u32 epctrl_reg; |
| 3738 | u32 epint_reg; |
| 3739 | |
| 3740 | epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) : |
| 3741 | DOEPCTL(hs_ep->index); |
| 3742 | epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) : |
| 3743 | DOEPINT(hs_ep->index); |
| 3744 | |
| 3745 | dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__, |
| 3746 | hs_ep->name); |
| 3747 | |
| 3748 | if (hs_ep->dir_in) { |
| 3749 | if (hsotg->dedicated_fifos || hs_ep->periodic) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3750 | dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3751 | /* Wait for Nak effect */ |
| 3752 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, |
| 3753 | DXEPINT_INEPNAKEFF, 100)) |
| 3754 | dev_warn(hsotg->dev, |
| 3755 | "%s: timeout DIEPINT.NAKEFF\n", |
| 3756 | __func__); |
| 3757 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3758 | dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3759 | /* Wait for Nak effect */ |
| 3760 | if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, |
| 3761 | GINTSTS_GINNAKEFF, 100)) |
| 3762 | dev_warn(hsotg->dev, |
| 3763 | "%s: timeout GINTSTS.GINNAKEFF\n", |
| 3764 | __func__); |
| 3765 | } |
| 3766 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3767 | if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF)) |
| 3768 | dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3769 | |
| 3770 | /* Wait for global nak to take effect */ |
| 3771 | if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, |
| 3772 | GINTSTS_GOUTNAKEFF, 100)) |
| 3773 | dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n", |
| 3774 | __func__); |
| 3775 | } |
| 3776 | |
| 3777 | /* Disable ep */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3778 | dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3779 | |
| 3780 | /* Wait for ep to be disabled */ |
| 3781 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100)) |
| 3782 | dev_warn(hsotg->dev, |
| 3783 | "%s: timeout DOEPCTL.EPDisable\n", __func__); |
| 3784 | |
| 3785 | /* Clear EPDISBLD interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3786 | dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3787 | |
| 3788 | if (hs_ep->dir_in) { |
| 3789 | unsigned short fifo_index; |
| 3790 | |
| 3791 | if (hsotg->dedicated_fifos || hs_ep->periodic) |
| 3792 | fifo_index = hs_ep->fifo_index; |
| 3793 | else |
| 3794 | fifo_index = 0; |
| 3795 | |
| 3796 | /* Flush TX FIFO */ |
| 3797 | dwc2_flush_tx_fifo(hsotg, fifo_index); |
| 3798 | |
| 3799 | /* Clear Global In NP NAK in Shared FIFO for non periodic ep */ |
| 3800 | if (!hsotg->dedicated_fifos && !hs_ep->periodic) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3801 | dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3802 | |
| 3803 | } else { |
| 3804 | /* Remove global NAKs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3805 | dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3806 | } |
| 3807 | } |
| 3808 | |
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 | * dwc2_hsotg_ep_enable - enable the given endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3811 | * @ep: The USB endpint to configure |
| 3812 | * @desc: The USB endpoint descriptor to configure with. |
| 3813 | * |
| 3814 | * This is called from the USB gadget code's usb_ep_enable(). |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3815 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3816 | static int dwc2_hsotg_ep_enable(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3817 | const struct usb_endpoint_descriptor *desc) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3818 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3819 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3820 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3821 | unsigned long flags; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3822 | unsigned int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3823 | u32 epctrl_reg; |
| 3824 | u32 epctrl; |
| 3825 | u32 mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3826 | u32 mc; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3827 | u32 mask; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3828 | unsigned int dir_in; |
| 3829 | unsigned int i, val, size; |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 3830 | int ret = 0; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3831 | unsigned char ep_type; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3832 | |
| 3833 | dev_dbg(hsotg->dev, |
| 3834 | "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n", |
| 3835 | __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes, |
| 3836 | desc->wMaxPacketSize, desc->bInterval); |
| 3837 | |
| 3838 | /* not to be called for EP0 */ |
Vahram Aharonyan | 8c3d609 | 2016-04-27 20:20:46 -0700 | [diff] [blame] | 3839 | if (index == 0) { |
| 3840 | dev_err(hsotg->dev, "%s: called for EP 0\n", __func__); |
| 3841 | return -EINVAL; |
| 3842 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3843 | |
| 3844 | dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0; |
| 3845 | if (dir_in != hs_ep->dir_in) { |
| 3846 | dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__); |
| 3847 | return -EINVAL; |
| 3848 | } |
| 3849 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3850 | ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 3851 | mps = usb_endpoint_maxp(desc); |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3852 | mc = usb_endpoint_maxp_mult(desc); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3853 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3854 | /* ISOC IN in DDMA supported bInterval up to 10 */ |
| 3855 | if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC && |
| 3856 | dir_in && desc->bInterval > 10) { |
| 3857 | dev_err(hsotg->dev, |
| 3858 | "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__); |
| 3859 | return -EINVAL; |
| 3860 | } |
| 3861 | |
| 3862 | /* High bandwidth ISOC OUT in DDMA not supported */ |
| 3863 | if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC && |
| 3864 | !dir_in && mc > 1) { |
| 3865 | dev_err(hsotg->dev, |
| 3866 | "%s: ISOC OUT, DDMA: HB not supported!\n", __func__); |
| 3867 | return -EINVAL; |
| 3868 | } |
| 3869 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3870 | /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3871 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3872 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3873 | epctrl = dwc2_readl(hsotg, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3874 | |
| 3875 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n", |
| 3876 | __func__, epctrl, epctrl_reg); |
| 3877 | |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3878 | /* Allocate DMA descriptor chain for non-ctrl endpoints */ |
Vardan Mikayelyan | 9383e08 | 2017-01-05 18:01:48 -0800 | [diff] [blame] | 3879 | if (using_desc_dma(hsotg) && !hs_ep->desc_list) { |
| 3880 | hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev, |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3881 | MAX_DMA_DESC_NUM_GENERIC * |
| 3882 | sizeof(struct dwc2_dma_desc), |
Marek Szyprowski | 86e881e | 2016-12-01 10:02:11 +0100 | [diff] [blame] | 3883 | &hs_ep->desc_list_dma, GFP_ATOMIC); |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3884 | if (!hs_ep->desc_list) { |
| 3885 | ret = -ENOMEM; |
| 3886 | goto error2; |
| 3887 | } |
| 3888 | } |
| 3889 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 3890 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3891 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3892 | epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK); |
| 3893 | epctrl |= DXEPCTL_MPS(mps); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3894 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3895 | /* |
| 3896 | * mark the endpoint as active, otherwise the core may ignore |
| 3897 | * transactions entirely for this endpoint |
| 3898 | */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3899 | epctrl |= DXEPCTL_USBACTEP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3900 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3901 | /* update the endpoint state */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3902 | 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] | 3903 | |
| 3904 | /* default, set to non-periodic */ |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3905 | hs_ep->isochronous = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3906 | hs_ep->periodic = 0; |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 3907 | hs_ep->halted = 0; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3908 | hs_ep->interval = desc->bInterval; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 3909 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3910 | switch (ep_type) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3911 | case USB_ENDPOINT_XFER_ISOC: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3912 | epctrl |= DXEPCTL_EPTYPE_ISO; |
| 3913 | epctrl |= DXEPCTL_SETEVENFR; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3914 | hs_ep->isochronous = 1; |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 3915 | hs_ep->interval = 1 << (desc->bInterval - 1); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3916 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
Vahram Aharonyan | ab7d219 | 2016-11-14 19:16:36 -0800 | [diff] [blame] | 3917 | hs_ep->next_desc = 0; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3918 | hs_ep->compl_desc = 0; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3919 | if (dir_in) { |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3920 | hs_ep->periodic = 1; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3921 | mask = dwc2_readl(hsotg, DIEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3922 | mask |= DIEPMSK_NAKMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3923 | dwc2_writel(hsotg, mask, DIEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3924 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3925 | mask = dwc2_readl(hsotg, DOEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3926 | mask |= DOEPMSK_OUTTKNEPDISMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3927 | dwc2_writel(hsotg, mask, DOEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3928 | } |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3929 | break; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3930 | |
| 3931 | case USB_ENDPOINT_XFER_BULK: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3932 | epctrl |= DXEPCTL_EPTYPE_BULK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3933 | break; |
| 3934 | |
| 3935 | case USB_ENDPOINT_XFER_INT: |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3936 | if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3937 | hs_ep->periodic = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3938 | |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 3939 | if (hsotg->gadget.speed == USB_SPEED_HIGH) |
| 3940 | hs_ep->interval = 1 << (desc->bInterval - 1); |
| 3941 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3942 | epctrl |= DXEPCTL_EPTYPE_INTERRUPT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3943 | break; |
| 3944 | |
| 3945 | case USB_ENDPOINT_XFER_CONTROL: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3946 | epctrl |= DXEPCTL_EPTYPE_CONTROL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3947 | break; |
| 3948 | } |
| 3949 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3950 | /* |
| 3951 | * if the hardware has dedicated fifos, we must give each IN EP |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3952 | * a unique tx-fifo even if it is non-periodic. |
| 3953 | */ |
Robert Baldyga | 21f3bb5 | 2016-08-29 13:38:57 -0700 | [diff] [blame] | 3954 | if (dir_in && hsotg->dedicated_fifos) { |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3955 | u32 fifo_index = 0; |
| 3956 | u32 fifo_size = UINT_MAX; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3957 | |
| 3958 | size = hs_ep->ep.maxpacket * hs_ep->mc; |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 3959 | for (i = 1; i < hsotg->num_of_eps; ++i) { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3960 | if (hsotg->fifo_map & (1 << i)) |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3961 | continue; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3962 | val = dwc2_readl(hsotg, DPTXFSIZN(i)); |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3963 | val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3964 | if (val < size) |
| 3965 | continue; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3966 | /* Search for smallest acceptable fifo */ |
| 3967 | if (val < fifo_size) { |
| 3968 | fifo_size = val; |
| 3969 | fifo_index = i; |
| 3970 | } |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3971 | } |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3972 | if (!fifo_index) { |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 3973 | dev_err(hsotg->dev, |
| 3974 | "%s: No suitable fifo found\n", __func__); |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 3975 | ret = -ENOMEM; |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3976 | goto error1; |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 3977 | } |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3978 | hsotg->fifo_map |= 1 << fifo_index; |
| 3979 | epctrl |= DXEPCTL_TXFNUM(fifo_index); |
| 3980 | hs_ep->fifo_index = fifo_index; |
| 3981 | hs_ep->fifo_size = fifo_size; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3982 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3983 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3984 | /* for non control endpoints, set PID to D0 */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3985 | if (index && !hs_ep->isochronous) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3986 | epctrl |= DXEPCTL_SETD0PID; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3987 | |
Artur Petrosyan | 5295322 | 2018-04-16 08:45:31 -0400 | [diff] [blame] | 3988 | /* WA for Full speed ISOC IN in DDMA mode. |
| 3989 | * By Clear NAK status of EP, core will send ZLP |
| 3990 | * to IN token and assert NAK interrupt relying |
| 3991 | * on TxFIFO status only |
| 3992 | */ |
| 3993 | |
| 3994 | if (hsotg->gadget.speed == USB_SPEED_FULL && |
| 3995 | hs_ep->isochronous && dir_in) { |
| 3996 | /* The WA applies only to core versions from 2.72a |
| 3997 | * to 4.00a (including both). Also for FS_IOT_1.00a |
| 3998 | * and HS_IOT_1.00a. |
| 3999 | */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4000 | u32 gsnpsid = dwc2_readl(hsotg, GSNPSID); |
Artur Petrosyan | 5295322 | 2018-04-16 08:45:31 -0400 | [diff] [blame] | 4001 | |
| 4002 | if ((gsnpsid >= DWC2_CORE_REV_2_72a && |
| 4003 | gsnpsid <= DWC2_CORE_REV_4_00a) || |
| 4004 | gsnpsid == DWC2_FS_IOT_REV_1_00a || |
| 4005 | gsnpsid == DWC2_HS_IOT_REV_1_00a) |
| 4006 | epctrl |= DXEPCTL_CNAK; |
| 4007 | } |
| 4008 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4009 | dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n", |
| 4010 | __func__, epctrl); |
| 4011 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4012 | dwc2_writel(hsotg, epctrl, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4013 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4014 | __func__, dwc2_readl(hsotg, epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4015 | |
| 4016 | /* enable the endpoint interrupt */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4017 | dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4018 | |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4019 | error1: |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4020 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4021 | |
| 4022 | error2: |
| 4023 | if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) { |
Vardan Mikayelyan | 9383e08 | 2017-01-05 18:01:48 -0800 | [diff] [blame] | 4024 | dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC * |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4025 | sizeof(struct dwc2_dma_desc), |
| 4026 | hs_ep->desc_list, hs_ep->desc_list_dma); |
| 4027 | hs_ep->desc_list = NULL; |
| 4028 | } |
| 4029 | |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 4030 | return ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4031 | } |
| 4032 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4033 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4034 | * dwc2_hsotg_ep_disable - disable given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4035 | * @ep: The endpoint to disable. |
| 4036 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4037 | static int dwc2_hsotg_ep_disable(struct usb_ep *ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4038 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4039 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4040 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4041 | int dir_in = hs_ep->dir_in; |
| 4042 | int index = hs_ep->index; |
| 4043 | unsigned long flags; |
| 4044 | u32 epctrl_reg; |
| 4045 | u32 ctrl; |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 4046 | int locked; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4047 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 4048 | dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4049 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4050 | if (ep == &hsotg->eps_out[0]->ep) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4051 | dev_err(hsotg->dev, "%s: called for ep0\n", __func__); |
| 4052 | return -EINVAL; |
| 4053 | } |
| 4054 | |
John Stultz | 9b481092 | 2017-10-23 14:32:49 -0700 | [diff] [blame] | 4055 | if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { |
| 4056 | dev_err(hsotg->dev, "%s: called in host mode?\n", __func__); |
| 4057 | return -EINVAL; |
| 4058 | } |
| 4059 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 4060 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4061 | |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 4062 | locked = spin_is_locked(&hsotg->lock); |
| 4063 | if (!locked) |
| 4064 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4065 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4066 | ctrl = dwc2_readl(hsotg, epctrl_reg); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 4067 | |
| 4068 | if (ctrl & DXEPCTL_EPENA) |
| 4069 | dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep); |
| 4070 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4071 | ctrl &= ~DXEPCTL_EPENA; |
| 4072 | ctrl &= ~DXEPCTL_USBACTEP; |
| 4073 | ctrl |= DXEPCTL_SNAK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4074 | |
| 4075 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4076 | dwc2_writel(hsotg, ctrl, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4077 | |
| 4078 | /* disable endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4079 | 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] | 4080 | |
Mian Yousaf Kaukab | 1141ea0 | 2015-01-09 13:38:57 +0100 | [diff] [blame] | 4081 | /* terminate all requests with shutdown */ |
| 4082 | kill_all_requests(hsotg, hs_ep, -ESHUTDOWN); |
| 4083 | |
Robert Baldyga | 1c07b20 | 2016-08-29 13:39:00 -0700 | [diff] [blame] | 4084 | hsotg->fifo_map &= ~(1 << hs_ep->fifo_index); |
| 4085 | hs_ep->fifo_index = 0; |
| 4086 | hs_ep->fifo_size = 0; |
| 4087 | |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 4088 | if (!locked) |
| 4089 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4090 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4091 | return 0; |
| 4092 | } |
| 4093 | |
| 4094 | /** |
| 4095 | * on_list - check request is on the given endpoint |
| 4096 | * @ep: The endpoint to check. |
| 4097 | * @test: The request to test if it is on the endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4098 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4099 | 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] | 4100 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4101 | struct dwc2_hsotg_req *req, *treq; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4102 | |
| 4103 | list_for_each_entry_safe(req, treq, &ep->queue, queue) { |
| 4104 | if (req == test) |
| 4105 | return true; |
| 4106 | } |
| 4107 | |
| 4108 | return false; |
| 4109 | } |
| 4110 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4111 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4112 | * dwc2_hsotg_ep_dequeue - dequeue given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4113 | * @ep: The endpoint to dequeue. |
| 4114 | * @req: The request to be removed from a queue. |
| 4115 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4116 | 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] | 4117 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4118 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 4119 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4120 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4121 | unsigned long flags; |
| 4122 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 4123 | dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4124 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4125 | spin_lock_irqsave(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4126 | |
| 4127 | if (!on_list(hs_ep, hs_req)) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4128 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4129 | return -EINVAL; |
| 4130 | } |
| 4131 | |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 4132 | /* Dequeue already started request */ |
| 4133 | if (req == &hs_ep->req->req) |
| 4134 | dwc2_hsotg_ep_stop_xfr(hs, hs_ep); |
| 4135 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4136 | dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4137 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4138 | |
| 4139 | return 0; |
| 4140 | } |
| 4141 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4142 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4143 | * dwc2_hsotg_ep_sethalt - set halt on a given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4144 | * @ep: The endpoint to set halt. |
| 4145 | * @value: Set or unset the halt. |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4146 | * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if |
| 4147 | * the endpoint is busy processing requests. |
| 4148 | * |
| 4149 | * We need to stall the endpoint immediately if request comes from set_feature |
| 4150 | * protocol command handler. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4151 | */ |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4152 | 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] | 4153 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4154 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4155 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4156 | int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4157 | u32 epreg; |
| 4158 | u32 epctl; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4159 | u32 xfertype; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4160 | |
| 4161 | dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value); |
| 4162 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 4163 | if (index == 0) { |
| 4164 | if (value) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4165 | dwc2_hsotg_stall_ep0(hs); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 4166 | else |
| 4167 | dev_warn(hs->dev, |
| 4168 | "%s: can't clear halt on ep0\n", __func__); |
| 4169 | return 0; |
| 4170 | } |
| 4171 | |
Vahram Aharonyan | 15186f1 | 2016-05-23 22:41:59 -0700 | [diff] [blame] | 4172 | if (hs_ep->isochronous) { |
| 4173 | dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name); |
| 4174 | return -EINVAL; |
| 4175 | } |
| 4176 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4177 | if (!now && value && !list_empty(&hs_ep->queue)) { |
| 4178 | dev_dbg(hs->dev, "%s request is pending, cannot halt\n", |
| 4179 | ep->name); |
| 4180 | return -EAGAIN; |
| 4181 | } |
| 4182 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4183 | if (hs_ep->dir_in) { |
| 4184 | epreg = DIEPCTL(index); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4185 | epctl = dwc2_readl(hs, epreg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4186 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4187 | if (value) { |
Felipe Balbi | 5a350d5 | 2015-06-29 20:17:22 -0500 | [diff] [blame] | 4188 | epctl |= DXEPCTL_STALL | DXEPCTL_SNAK; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4189 | if (epctl & DXEPCTL_EPENA) |
| 4190 | epctl |= DXEPCTL_EPDIS; |
| 4191 | } else { |
| 4192 | epctl &= ~DXEPCTL_STALL; |
| 4193 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 4194 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4195 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 4196 | epctl |= DXEPCTL_SETD0PID; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4197 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4198 | dwc2_writel(hs, epctl, epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4199 | } else { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4200 | epreg = DOEPCTL(index); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4201 | epctl = dwc2_readl(hs, epreg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4202 | |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 4203 | if (value) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4204 | epctl |= DXEPCTL_STALL; |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 4205 | } else { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4206 | epctl &= ~DXEPCTL_STALL; |
| 4207 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 4208 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4209 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 4210 | epctl |= DXEPCTL_SETD0PID; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4211 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4212 | dwc2_writel(hs, epctl, epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4213 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4214 | |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 4215 | hs_ep->halted = value; |
| 4216 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4217 | return 0; |
| 4218 | } |
| 4219 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4220 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4221 | * 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] | 4222 | * @ep: The endpoint to set halt. |
| 4223 | * @value: Set or unset the halt. |
| 4224 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4225 | 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] | 4226 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4227 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4228 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4229 | unsigned long flags = 0; |
| 4230 | int ret = 0; |
| 4231 | |
| 4232 | spin_lock_irqsave(&hs->lock, flags); |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4233 | ret = dwc2_hsotg_ep_sethalt(ep, value, false); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4234 | spin_unlock_irqrestore(&hs->lock, flags); |
| 4235 | |
| 4236 | return ret; |
| 4237 | } |
| 4238 | |
Bhumika Goyal | ebce561 | 2017-08-12 17:34:55 +0530 | [diff] [blame] | 4239 | static const struct usb_ep_ops dwc2_hsotg_ep_ops = { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4240 | .enable = dwc2_hsotg_ep_enable, |
| 4241 | .disable = dwc2_hsotg_ep_disable, |
| 4242 | .alloc_request = dwc2_hsotg_ep_alloc_request, |
| 4243 | .free_request = dwc2_hsotg_ep_free_request, |
| 4244 | .queue = dwc2_hsotg_ep_queue_lock, |
| 4245 | .dequeue = dwc2_hsotg_ep_dequeue, |
| 4246 | .set_halt = dwc2_hsotg_ep_sethalt_lock, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4247 | /* note, don't believe we have any call for the fifo routines */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4248 | }; |
| 4249 | |
| 4250 | /** |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4251 | * dwc2_hsotg_init - initialize the usb core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4252 | * @hsotg: The driver state |
| 4253 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4254 | static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg) |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4255 | { |
Mian Yousaf Kaukab | fa4a8d7 | 2015-01-30 09:09:35 +0100 | [diff] [blame] | 4256 | u32 trdtim; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 4257 | u32 usbcfg; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4258 | /* unmask subset of endpoint interrupts */ |
| 4259 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4260 | dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4261 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4262 | DIEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4263 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4264 | dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4265 | DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4266 | DOEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4267 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4268 | dwc2_writel(hsotg, 0, DAINTMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4269 | |
| 4270 | /* Be in disconnected state until gadget is registered */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4271 | dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4272 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4273 | /* setup fifos */ |
| 4274 | |
| 4275 | dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4276 | dwc2_readl(hsotg, GRXFSIZ), |
| 4277 | dwc2_readl(hsotg, GNPTXFSIZ)); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4278 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4279 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4280 | |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 4281 | /* keep other bits untouched (so e.g. forced modes are not lost) */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4282 | usbcfg = dwc2_readl(hsotg, GUSBCFG); |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 4283 | usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP | |
Amelie Delaunay | ca02954 | 2017-01-12 16:09:44 +0100 | [diff] [blame] | 4284 | GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK); |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 4285 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4286 | /* 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] | 4287 | trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 4288 | usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) | |
| 4289 | (trdtim << GUSBCFG_USBTRDTIM_SHIFT); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4290 | dwc2_writel(hsotg, usbcfg, GUSBCFG); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4291 | |
Gregory Herrero | f509004 | 2015-01-09 13:38:47 +0100 | [diff] [blame] | 4292 | if (using_dma(hsotg)) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4293 | dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4294 | } |
| 4295 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4296 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4297 | * dwc2_hsotg_udc_start - prepare the udc for work |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4298 | * @gadget: The usb gadget state |
| 4299 | * @driver: The usb gadget driver |
| 4300 | * |
| 4301 | * Perform initialization to prepare udc device and driver |
| 4302 | * to work. |
| 4303 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4304 | static int dwc2_hsotg_udc_start(struct usb_gadget *gadget, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4305 | struct usb_gadget_driver *driver) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4306 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4307 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4308 | unsigned long flags; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4309 | int ret; |
| 4310 | |
| 4311 | if (!hsotg) { |
Pavel Machek | a023da3 | 2013-09-30 14:56:02 +0200 | [diff] [blame] | 4312 | pr_err("%s: called with no device\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4313 | return -ENODEV; |
| 4314 | } |
| 4315 | |
| 4316 | if (!driver) { |
| 4317 | dev_err(hsotg->dev, "%s: no driver\n", __func__); |
| 4318 | return -EINVAL; |
| 4319 | } |
| 4320 | |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 4321 | if (driver->max_speed < USB_SPEED_FULL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4322 | dev_err(hsotg->dev, "%s: bad speed\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4323 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 4324 | if (!driver->setup) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4325 | dev_err(hsotg->dev, "%s: missing entry points\n", __func__); |
| 4326 | return -EINVAL; |
| 4327 | } |
| 4328 | |
| 4329 | WARN_ON(hsotg->driver); |
| 4330 | |
| 4331 | driver->driver.bus = NULL; |
| 4332 | hsotg->driver = driver; |
Alexandre Pereira da Silva | 7d7b229 | 2012-06-26 11:27:10 -0300 | [diff] [blame] | 4333 | hsotg->gadget.dev.of_node = hsotg->dev->of_node; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4334 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4335 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4336 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) { |
| 4337 | ret = dwc2_lowlevel_hw_enable(hsotg); |
| 4338 | if (ret) |
| 4339 | goto err; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4340 | } |
| 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, &hsotg->gadget); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 4344 | |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4345 | spin_lock_irqsave(&hsotg->lock, flags); |
John Youn | d0f0ac5 | 2016-09-07 19:39:37 -0700 | [diff] [blame] | 4346 | if (dwc2_hw_is_device(hsotg)) { |
| 4347 | dwc2_hsotg_init(hsotg); |
| 4348 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
| 4349 | } |
| 4350 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4351 | hsotg->enabled = 0; |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4352 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4353 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4354 | dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4355 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4356 | return 0; |
| 4357 | |
| 4358 | err: |
| 4359 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4360 | return ret; |
| 4361 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4362 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4363 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4364 | * dwc2_hsotg_udc_stop - stop the udc |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4365 | * @gadget: The usb gadget state |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4366 | * |
| 4367 | * Stop udc hw block and stay tunned for future transmissions |
| 4368 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4369 | static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [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 | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4372 | unsigned long flags = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4373 | int ep; |
| 4374 | |
| 4375 | if (!hsotg) |
| 4376 | return -ENODEV; |
| 4377 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4378 | /* all endpoints should be shutdown */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4379 | for (ep = 1; ep < hsotg->num_of_eps; ep++) { |
| 4380 | if (hsotg->eps_in[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4381 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4382 | if (hsotg->eps_out[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4383 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4384 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4385 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4386 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4387 | |
Marek Szyprowski | 32805c3 | 2014-10-20 12:45:33 +0200 | [diff] [blame] | 4388 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4389 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4390 | hsotg->enabled = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4391 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4392 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4393 | |
Gregory Herrero | f6c0159 | 2015-01-09 13:38:41 +0100 | [diff] [blame] | 4394 | if (!IS_ERR_OR_NULL(hsotg->uphy)) |
| 4395 | otg_set_peripheral(hsotg->uphy->otg, NULL); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 4396 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4397 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 4398 | dwc2_lowlevel_hw_disable(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4399 | |
| 4400 | return 0; |
| 4401 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4402 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4403 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4404 | * dwc2_hsotg_gadget_getframe - read the frame number |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4405 | * @gadget: The usb gadget state |
| 4406 | * |
| 4407 | * Read the {micro} frame number |
| 4408 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4409 | static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4410 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4411 | return dwc2_hsotg_read_frameno(to_hsotg(gadget)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4412 | } |
| 4413 | |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4414 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4415 | * dwc2_hsotg_pullup - connect/disconnect the USB PHY |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4416 | * @gadget: The usb gadget state |
| 4417 | * @is_on: Current state of the USB PHY |
| 4418 | * |
| 4419 | * Connect/Disconnect the USB PHY pullup |
| 4420 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4421 | static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on) |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4422 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4423 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4424 | unsigned long flags = 0; |
| 4425 | |
Gregory Herrero | 77ba911 | 2015-09-29 12:08:19 +0200 | [diff] [blame] | 4426 | 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] | 4427 | hsotg->op_state); |
Gregory Herrero | 77ba911 | 2015-09-29 12:08:19 +0200 | [diff] [blame] | 4428 | |
| 4429 | /* Don't modify pullup state while in host mode */ |
| 4430 | if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { |
| 4431 | hsotg->enabled = is_on; |
| 4432 | return 0; |
| 4433 | } |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4434 | |
| 4435 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4436 | if (is_on) { |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4437 | hsotg->enabled = 1; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4438 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4439 | /* Enable ACG feature in device mode,if supported */ |
| 4440 | dwc2_enable_acg(hsotg); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4441 | dwc2_hsotg_core_connect(hsotg); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4442 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4443 | dwc2_hsotg_core_disconnect(hsotg); |
| 4444 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4445 | hsotg->enabled = 0; |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4446 | } |
| 4447 | |
| 4448 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4449 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4450 | |
| 4451 | return 0; |
| 4452 | } |
| 4453 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4454 | 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] | 4455 | { |
| 4456 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 4457 | unsigned long flags; |
| 4458 | |
| 4459 | dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active); |
| 4460 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4461 | |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4462 | /* |
Vardan Mikayelyan | 41ba9b9 | 2018-02-16 14:06:36 +0400 | [diff] [blame] | 4463 | * If controller is hibernated, it must exit from power_down |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4464 | * before being initialized / de-initialized |
| 4465 | */ |
| 4466 | if (hsotg->lx_state == DWC2_L2) |
Vardan Mikayelyan | 41ba9b9 | 2018-02-16 14:06:36 +0400 | [diff] [blame] | 4467 | dwc2_exit_partial_power_down(hsotg, false); |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4468 | |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4469 | if (is_active) { |
Gregory Herrero | cd0e641 | 2015-09-29 12:08:20 +0200 | [diff] [blame] | 4470 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 4471 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4472 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4473 | if (hsotg->enabled) { |
| 4474 | /* Enable ACG feature in device mode,if supported */ |
| 4475 | dwc2_enable_acg(hsotg); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4476 | dwc2_hsotg_core_connect(hsotg); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4477 | } |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4478 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4479 | dwc2_hsotg_core_disconnect(hsotg); |
| 4480 | dwc2_hsotg_disconnect(hsotg); |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4484 | return 0; |
| 4485 | } |
| 4486 | |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 4487 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4488 | * dwc2_hsotg_vbus_draw - report bMaxPower field |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 4489 | * @gadget: The usb gadget state |
| 4490 | * @mA: Amount of current |
| 4491 | * |
| 4492 | * Report how much power the device may consume to the phy. |
| 4493 | */ |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4494 | 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] | 4495 | { |
| 4496 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 4497 | |
| 4498 | if (IS_ERR_OR_NULL(hsotg->uphy)) |
| 4499 | return -ENOTSUPP; |
| 4500 | return usb_phy_set_power(hsotg->uphy, mA); |
| 4501 | } |
| 4502 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4503 | static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = { |
| 4504 | .get_frame = dwc2_hsotg_gadget_getframe, |
| 4505 | .udc_start = dwc2_hsotg_udc_start, |
| 4506 | .udc_stop = dwc2_hsotg_udc_stop, |
| 4507 | .pullup = dwc2_hsotg_pullup, |
| 4508 | .vbus_session = dwc2_hsotg_vbus_session, |
| 4509 | .vbus_draw = dwc2_hsotg_vbus_draw, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4510 | }; |
| 4511 | |
| 4512 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4513 | * dwc2_hsotg_initep - initialise a single endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4514 | * @hsotg: The device state. |
| 4515 | * @hs_ep: The endpoint to be initialised. |
| 4516 | * @epnum: The endpoint number |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4517 | * @dir_in: True if direction is in. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4518 | * |
| 4519 | * Initialise the given endpoint (as part of the probe and device state |
| 4520 | * creation) to give to the gadget driver. Setup the endpoint name, any |
| 4521 | * direction information and other state that may be required. |
| 4522 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4523 | static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4524 | struct dwc2_hsotg_ep *hs_ep, |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4525 | int epnum, |
| 4526 | bool dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4527 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4528 | char *dir; |
| 4529 | |
| 4530 | if (epnum == 0) |
| 4531 | dir = ""; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4532 | else if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4533 | dir = "in"; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4534 | else |
| 4535 | dir = "out"; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4536 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4537 | hs_ep->dir_in = dir_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4538 | hs_ep->index = epnum; |
| 4539 | |
| 4540 | snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir); |
| 4541 | |
| 4542 | INIT_LIST_HEAD(&hs_ep->queue); |
| 4543 | INIT_LIST_HEAD(&hs_ep->ep.ep_list); |
| 4544 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4545 | /* add to the list of endpoints known by the gadget driver */ |
| 4546 | if (epnum) |
| 4547 | list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list); |
| 4548 | |
| 4549 | hs_ep->parent = hsotg; |
| 4550 | hs_ep->ep.name = hs_ep->name; |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 4551 | |
| 4552 | if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW) |
| 4553 | usb_ep_set_maxpacket_limit(&hs_ep->ep, 8); |
| 4554 | else |
| 4555 | usb_ep_set_maxpacket_limit(&hs_ep->ep, |
| 4556 | epnum ? 1024 : EP0_MPS_LIMIT); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4557 | hs_ep->ep.ops = &dwc2_hsotg_ep_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4558 | |
Robert Baldyga | 2954522 | 2015-07-31 16:00:18 +0200 | [diff] [blame] | 4559 | if (epnum == 0) { |
| 4560 | hs_ep->ep.caps.type_control = true; |
| 4561 | } else { |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 4562 | if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) { |
| 4563 | hs_ep->ep.caps.type_iso = true; |
| 4564 | hs_ep->ep.caps.type_bulk = true; |
| 4565 | } |
Robert Baldyga | 2954522 | 2015-07-31 16:00:18 +0200 | [diff] [blame] | 4566 | hs_ep->ep.caps.type_int = true; |
| 4567 | } |
| 4568 | |
| 4569 | if (dir_in) |
| 4570 | hs_ep->ep.caps.dir_in = true; |
| 4571 | else |
| 4572 | hs_ep->ep.caps.dir_out = true; |
| 4573 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4574 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4575 | * 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] | 4576 | * to be something valid. |
| 4577 | */ |
| 4578 | |
| 4579 | if (using_dma(hsotg)) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4580 | u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15); |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4581 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4582 | if (dir_in) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4583 | dwc2_writel(hsotg, next, DIEPCTL(epnum)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4584 | else |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4585 | dwc2_writel(hsotg, next, DOEPCTL(epnum)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4586 | } |
| 4587 | } |
| 4588 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4589 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4590 | * dwc2_hsotg_hw_cfg - read HW configuration registers |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4591 | * @hsotg: Programming view of the DWC_otg controller |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4592 | * |
| 4593 | * Read the USB core HW configuration registers |
| 4594 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4595 | static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4596 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4597 | u32 cfg; |
| 4598 | u32 ep_type; |
| 4599 | u32 i; |
| 4600 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4601 | /* check hardware configuration */ |
| 4602 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4603 | hsotg->num_of_eps = hsotg->hw_params.num_dev_ep; |
| 4604 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4605 | /* Add ep0 */ |
| 4606 | hsotg->num_of_eps++; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4607 | |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 4608 | hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, |
| 4609 | sizeof(struct dwc2_hsotg_ep), |
| 4610 | GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4611 | if (!hsotg->eps_in[0]) |
| 4612 | return -ENOMEM; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4613 | /* Same dwc2_hsotg_ep is used in both directions for ep0 */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4614 | hsotg->eps_out[0] = hsotg->eps_in[0]; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4615 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4616 | cfg = hsotg->hw_params.dev_ep_dirs; |
Roshan Pius | 251a17f | 2015-02-02 14:55:38 -0800 | [diff] [blame] | 4617 | 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] | 4618 | ep_type = cfg & 3; |
| 4619 | /* Direction in or both */ |
| 4620 | if (!(ep_type & 2)) { |
| 4621 | hsotg->eps_in[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4622 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4623 | if (!hsotg->eps_in[i]) |
| 4624 | return -ENOMEM; |
| 4625 | } |
| 4626 | /* Direction out or both */ |
| 4627 | if (!(ep_type & 1)) { |
| 4628 | hsotg->eps_out[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4629 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4630 | if (!hsotg->eps_out[i]) |
| 4631 | return -ENOMEM; |
| 4632 | } |
| 4633 | } |
| 4634 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4635 | hsotg->fifo_mem = hsotg->hw_params.total_fifo_size; |
| 4636 | hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo; |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4637 | |
Marek Szyprowski | cff9eb7 | 2014-09-09 10:44:55 +0200 | [diff] [blame] | 4638 | dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n", |
| 4639 | hsotg->num_of_eps, |
| 4640 | hsotg->dedicated_fifos ? "dedicated" : "shared", |
| 4641 | hsotg->fifo_mem); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4642 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4643 | } |
| 4644 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4645 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4646 | * dwc2_hsotg_dump - dump state of the udc |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4647 | * @hsotg: Programming view of the DWC_otg controller |
| 4648 | * |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4649 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4650 | static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4651 | { |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 4652 | #ifdef DEBUG |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4653 | struct device *dev = hsotg->dev; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4654 | u32 val; |
| 4655 | int idx; |
| 4656 | |
| 4657 | dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4658 | dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL), |
| 4659 | dwc2_readl(hsotg, DIEPMSK)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4660 | |
Mian Yousaf Kaukab | f889f23 | 2015-01-30 09:09:36 +0100 | [diff] [blame] | 4661 | dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4662 | dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4663 | |
| 4664 | dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4665 | dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4666 | |
| 4667 | /* show periodic fifo settings */ |
| 4668 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 4669 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4670 | val = dwc2_readl(hsotg, DPTXFSIZN(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4671 | dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4672 | val >> FIFOSIZE_DEPTH_SHIFT, |
| 4673 | val & FIFOSIZE_STARTADDR_MASK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4674 | } |
| 4675 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 4676 | for (idx = 0; idx < hsotg->num_of_eps; idx++) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4677 | dev_info(dev, |
| 4678 | "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4679 | dwc2_readl(hsotg, DIEPCTL(idx)), |
| 4680 | dwc2_readl(hsotg, DIEPTSIZ(idx)), |
| 4681 | dwc2_readl(hsotg, DIEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4682 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4683 | val = dwc2_readl(hsotg, DOEPCTL(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4684 | dev_info(dev, |
| 4685 | "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4686 | idx, dwc2_readl(hsotg, DOEPCTL(idx)), |
| 4687 | dwc2_readl(hsotg, DOEPTSIZ(idx)), |
| 4688 | dwc2_readl(hsotg, DOEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4692 | dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE)); |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 4693 | #endif |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4694 | } |
| 4695 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4696 | /** |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4697 | * dwc2_gadget_init - init function for gadget |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4698 | * @hsotg: Programming view of the DWC_otg controller |
| 4699 | * |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4700 | */ |
Vardan Mikayelyan | f376899 | 2017-12-25 15:17:45 +0400 | [diff] [blame] | 4701 | int dwc2_gadget_init(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4702 | { |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4703 | struct device *dev = hsotg->dev; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4704 | int epnum; |
| 4705 | int ret; |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4706 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 4707 | /* Dump fifo information */ |
| 4708 | dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n", |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 4709 | hsotg->params.g_np_tx_fifo_size); |
| 4710 | 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] | 4711 | |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 4712 | hsotg->gadget.max_speed = USB_SPEED_HIGH; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4713 | hsotg->gadget.ops = &dwc2_hsotg_gadget_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4714 | hsotg->gadget.name = dev_name(dev); |
Vardan Mikayelyan | fa389a6 | 2018-02-16 14:08:53 +0400 | [diff] [blame] | 4715 | hsotg->remote_wakeup_allowed = 0; |
John Youn | 7455f8b | 2018-01-24 17:44:51 +0400 | [diff] [blame] | 4716 | |
| 4717 | if (hsotg->params.lpm) |
| 4718 | hsotg->gadget.lpm_capable = true; |
| 4719 | |
Gregory Herrero | 097ee66 | 2015-04-29 22:09:10 +0200 | [diff] [blame] | 4720 | if (hsotg->dr_mode == USB_DR_MODE_OTG) |
| 4721 | hsotg->gadget.is_otg = 1; |
Mian Yousaf Kaukab | ec4cc65 | 2015-09-22 15:16:55 +0200 | [diff] [blame] | 4722 | else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 4723 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4724 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4725 | ret = dwc2_hsotg_hw_cfg(hsotg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4726 | if (ret) { |
| 4727 | dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4728 | return ret; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4729 | } |
| 4730 | |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4731 | hsotg->ctrl_buff = devm_kzalloc(hsotg->dev, |
| 4732 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
Wolfram Sang | 8bae0f8 | 2016-08-25 19:39:02 +0200 | [diff] [blame] | 4733 | if (!hsotg->ctrl_buff) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4734 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4735 | |
| 4736 | hsotg->ep0_buff = devm_kzalloc(hsotg->dev, |
| 4737 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
Wolfram Sang | 8bae0f8 | 2016-08-25 19:39:02 +0200 | [diff] [blame] | 4738 | if (!hsotg->ep0_buff) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4739 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4740 | |
Vahram Aharonyan | 0f6b80c | 2016-11-09 19:27:56 -0800 | [diff] [blame] | 4741 | if (using_desc_dma(hsotg)) { |
| 4742 | ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg); |
| 4743 | if (ret < 0) |
| 4744 | return ret; |
| 4745 | } |
| 4746 | |
Vardan Mikayelyan | f376899 | 2017-12-25 15:17:45 +0400 | [diff] [blame] | 4747 | ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq, |
| 4748 | IRQF_SHARED, dev_name(hsotg->dev), hsotg); |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 4749 | if (ret < 0) { |
Dinh Nguyen | db8178c | 2014-11-11 11:13:37 -0600 | [diff] [blame] | 4750 | dev_err(dev, "cannot claim IRQ for gadget\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4751 | return ret; |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 4752 | } |
| 4753 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4754 | /* hsotg->num_of_eps holds number of EPs other than ep0 */ |
| 4755 | |
| 4756 | if (hsotg->num_of_eps == 0) { |
| 4757 | dev_err(dev, "wrong number of EPs (zero)\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4758 | return -EINVAL; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4759 | } |
| 4760 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4761 | /* setup endpoint information */ |
| 4762 | |
| 4763 | INIT_LIST_HEAD(&hsotg->gadget.ep_list); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4764 | hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4765 | |
| 4766 | /* allocate EP0 request */ |
| 4767 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4768 | 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] | 4769 | GFP_KERNEL); |
| 4770 | if (!hsotg->ctrl_req) { |
| 4771 | dev_err(dev, "failed to allocate ctrl req\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4772 | return -ENOMEM; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4773 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4774 | |
| 4775 | /* initialise the endpoints now the core has been initialised */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4776 | for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) { |
| 4777 | if (hsotg->eps_in[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4778 | dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4779 | epnum, 1); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4780 | if (hsotg->eps_out[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4781 | dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4782 | epnum, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4783 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4784 | |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4785 | ret = usb_add_gadget_udc(dev, &hsotg->gadget); |
Grigor Tovmasyan | 9bb073a | 2018-05-24 18:22:30 +0400 | [diff] [blame] | 4786 | if (ret) { |
| 4787 | dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, |
| 4788 | hsotg->ctrl_req); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4789 | return ret; |
Grigor Tovmasyan | 9bb073a | 2018-05-24 18:22:30 +0400 | [diff] [blame] | 4790 | } |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4791 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4792 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4793 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4794 | } |
| 4795 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4796 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4797 | * dwc2_hsotg_remove - remove function for hsotg driver |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4798 | * @hsotg: Programming view of the DWC_otg controller |
| 4799 | * |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4800 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4801 | int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4802 | { |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 4803 | usb_del_gadget_udc(&hsotg->gadget); |
Grigor Tovmasyan | 9bb073a | 2018-05-24 18:22:30 +0400 | [diff] [blame] | 4804 | dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req); |
Marek Szyprowski | 31ee04d | 2010-07-19 16:01:42 +0200 | [diff] [blame] | 4805 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4806 | return 0; |
| 4807 | } |
| 4808 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4809 | int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4810 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4811 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4812 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4813 | if (hsotg->lx_state != DWC2_L0) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4814 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4815 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4816 | if (hsotg->driver) { |
| 4817 | int ep; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4818 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4819 | dev_info(hsotg->dev, "suspending usb gadget %s\n", |
| 4820 | hsotg->driver->driver.name); |
| 4821 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4822 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4823 | if (hsotg->enabled) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4824 | dwc2_hsotg_core_disconnect(hsotg); |
| 4825 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4826 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4827 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4828 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4829 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 4830 | if (hsotg->eps_in[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4831 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4832 | if (hsotg->eps_out[ep]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4833 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4834 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4835 | } |
| 4836 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4837 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4838 | } |
| 4839 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4840 | int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4841 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4842 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4843 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4844 | if (hsotg->lx_state == DWC2_L2) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4845 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4846 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4847 | if (hsotg->driver) { |
| 4848 | dev_info(hsotg->dev, "resuming usb gadget %s\n", |
| 4849 | hsotg->driver->driver.name); |
Robert Baldyga | d00b414 | 2014-09-09 10:44:57 +0200 | [diff] [blame] | 4850 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4851 | spin_lock_irqsave(&hsotg->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4852 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4853 | if (hsotg->enabled) { |
| 4854 | /* Enable ACG feature in device mode,if supported */ |
| 4855 | dwc2_enable_acg(hsotg); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4856 | dwc2_hsotg_core_connect(hsotg); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4857 | } |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4858 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4859 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4860 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4861 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4862 | } |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4863 | |
| 4864 | /** |
| 4865 | * dwc2_backup_device_registers() - Backup controller device registers. |
| 4866 | * When suspending usb bus, registers needs to be backuped |
| 4867 | * if controller power is disabled once suspended. |
| 4868 | * |
| 4869 | * @hsotg: Programming view of the DWC_otg controller |
| 4870 | */ |
| 4871 | int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg) |
| 4872 | { |
| 4873 | struct dwc2_dregs_backup *dr; |
| 4874 | int i; |
| 4875 | |
| 4876 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 4877 | |
| 4878 | /* Backup dev regs */ |
| 4879 | dr = &hsotg->dr_backup; |
| 4880 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4881 | dr->dcfg = dwc2_readl(hsotg, DCFG); |
| 4882 | dr->dctl = dwc2_readl(hsotg, DCTL); |
| 4883 | dr->daintmsk = dwc2_readl(hsotg, DAINTMSK); |
| 4884 | dr->diepmsk = dwc2_readl(hsotg, DIEPMSK); |
| 4885 | dr->doepmsk = dwc2_readl(hsotg, DOEPMSK); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4886 | |
| 4887 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 4888 | /* Backup IN EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4889 | dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4890 | |
| 4891 | /* Ensure DATA PID is correctly configured */ |
| 4892 | if (dr->diepctl[i] & DXEPCTL_DPID) |
| 4893 | dr->diepctl[i] |= DXEPCTL_SETD1PID; |
| 4894 | else |
| 4895 | dr->diepctl[i] |= DXEPCTL_SETD0PID; |
| 4896 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4897 | dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i)); |
| 4898 | dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4899 | |
| 4900 | /* Backup OUT EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4901 | dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4902 | |
| 4903 | /* Ensure DATA PID is correctly configured */ |
| 4904 | if (dr->doepctl[i] & DXEPCTL_DPID) |
| 4905 | dr->doepctl[i] |= DXEPCTL_SETD1PID; |
| 4906 | else |
| 4907 | dr->doepctl[i] |= DXEPCTL_SETD0PID; |
| 4908 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4909 | dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i)); |
| 4910 | dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i)); |
| 4911 | dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4912 | } |
| 4913 | dr->valid = true; |
| 4914 | return 0; |
| 4915 | } |
| 4916 | |
| 4917 | /** |
| 4918 | * dwc2_restore_device_registers() - Restore controller device registers. |
| 4919 | * When resuming usb bus, device registers needs to be restored |
| 4920 | * if controller power were disabled. |
| 4921 | * |
| 4922 | * @hsotg: Programming view of the DWC_otg controller |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 4923 | * @remote_wakeup: Indicates whether resume is initiated by Device or Host. |
| 4924 | * |
| 4925 | * Return: 0 if successful, negative error code otherwise |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4926 | */ |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 4927 | int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup) |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4928 | { |
| 4929 | struct dwc2_dregs_backup *dr; |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4930 | int i; |
| 4931 | |
| 4932 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 4933 | |
| 4934 | /* Restore dev regs */ |
| 4935 | dr = &hsotg->dr_backup; |
| 4936 | if (!dr->valid) { |
| 4937 | dev_err(hsotg->dev, "%s: no device registers to restore\n", |
| 4938 | __func__); |
| 4939 | return -EINVAL; |
| 4940 | } |
| 4941 | dr->valid = false; |
| 4942 | |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 4943 | if (!remote_wakeup) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4944 | dwc2_writel(hsotg, dr->dctl, DCTL); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 4945 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4946 | dwc2_writel(hsotg, dr->daintmsk, DAINTMSK); |
| 4947 | dwc2_writel(hsotg, dr->diepmsk, DIEPMSK); |
| 4948 | dwc2_writel(hsotg, dr->doepmsk, DOEPMSK); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4949 | |
| 4950 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 4951 | /* Restore IN EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4952 | dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i)); |
| 4953 | dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i)); |
| 4954 | dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i)); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 4955 | /** WA for enabled EPx's IN in DDMA mode. On entering to |
| 4956 | * hibernation wrong value read and saved from DIEPDMAx, |
| 4957 | * as result BNA interrupt asserted on hibernation exit |
| 4958 | * by restoring from saved area. |
| 4959 | */ |
| 4960 | if (hsotg->params.g_dma_desc && |
| 4961 | (dr->diepctl[i] & DXEPCTL_EPENA)) |
| 4962 | dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4963 | dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i)); |
| 4964 | dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i)); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 4965 | /* Restore OUT EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4966 | dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i)); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 4967 | /* WA for enabled EPx's OUT in DDMA mode. On entering to |
| 4968 | * hibernation wrong value read and saved from DOEPDMAx, |
| 4969 | * as result BNA interrupt asserted on hibernation exit |
| 4970 | * by restoring from saved area. |
| 4971 | */ |
| 4972 | if (hsotg->params.g_dma_desc && |
| 4973 | (dr->doepctl[i] & DXEPCTL_EPENA)) |
| 4974 | dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4975 | dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i)); |
| 4976 | dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4977 | } |
| 4978 | |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4979 | return 0; |
| 4980 | } |
Sevak Arakelyan | 21b0340 | 2018-01-24 17:43:32 +0400 | [diff] [blame] | 4981 | |
| 4982 | /** |
| 4983 | * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode |
| 4984 | * |
| 4985 | * @hsotg: Programming view of DWC_otg controller |
| 4986 | * |
| 4987 | */ |
| 4988 | void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg) |
| 4989 | { |
| 4990 | u32 val; |
| 4991 | |
| 4992 | if (!hsotg->params.lpm) |
| 4993 | return; |
| 4994 | |
| 4995 | val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES; |
| 4996 | val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0; |
| 4997 | val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0; |
| 4998 | val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT; |
| 4999 | val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5000 | dwc2_writel(hsotg, val, GLPMCFG); |
| 5001 | dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG)); |
Sevak Arakelyan | 21b0340 | 2018-01-24 17:43:32 +0400 | [diff] [blame] | 5002 | } |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5003 | |
| 5004 | /** |
| 5005 | * dwc2_gadget_enter_hibernation() - Put controller in Hibernation. |
| 5006 | * |
| 5007 | * @hsotg: Programming view of the DWC_otg controller |
| 5008 | * |
| 5009 | * Return non-zero if failed to enter to hibernation. |
| 5010 | */ |
| 5011 | int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg) |
| 5012 | { |
| 5013 | u32 gpwrdn; |
| 5014 | int ret = 0; |
| 5015 | |
| 5016 | /* Change to L2(suspend) state */ |
| 5017 | hsotg->lx_state = DWC2_L2; |
| 5018 | dev_dbg(hsotg->dev, "Start of hibernation completed\n"); |
| 5019 | ret = dwc2_backup_global_registers(hsotg); |
| 5020 | if (ret) { |
| 5021 | dev_err(hsotg->dev, "%s: failed to backup global registers\n", |
| 5022 | __func__); |
| 5023 | return ret; |
| 5024 | } |
| 5025 | ret = dwc2_backup_device_registers(hsotg); |
| 5026 | if (ret) { |
| 5027 | dev_err(hsotg->dev, "%s: failed to backup device registers\n", |
| 5028 | __func__); |
| 5029 | return ret; |
| 5030 | } |
| 5031 | |
| 5032 | gpwrdn = GPWRDN_PWRDNRSTN; |
| 5033 | gpwrdn |= GPWRDN_PMUACTV; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5034 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5035 | udelay(10); |
| 5036 | |
| 5037 | /* Set flag to indicate that we are in hibernation */ |
| 5038 | hsotg->hibernated = 1; |
| 5039 | |
| 5040 | /* Enable interrupts from wake up logic */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5041 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5042 | gpwrdn |= GPWRDN_PMUINTSEL; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5043 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5044 | udelay(10); |
| 5045 | |
| 5046 | /* Unmask device mode interrupts in GPWRDN */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5047 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5048 | gpwrdn |= GPWRDN_RST_DET_MSK; |
| 5049 | gpwrdn |= GPWRDN_LNSTSCHG_MSK; |
| 5050 | gpwrdn |= GPWRDN_STS_CHGINT_MSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5051 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5052 | udelay(10); |
| 5053 | |
| 5054 | /* Enable Power Down Clamp */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5055 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5056 | gpwrdn |= GPWRDN_PWRDNCLMP; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5057 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5058 | udelay(10); |
| 5059 | |
| 5060 | /* Switch off VDD */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5061 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5062 | gpwrdn |= GPWRDN_PWRDNSWTCH; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5063 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5064 | udelay(10); |
| 5065 | |
| 5066 | /* Save gpwrdn register for further usage if stschng interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5067 | hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5068 | dev_dbg(hsotg->dev, "Hibernation completed\n"); |
| 5069 | |
| 5070 | return ret; |
| 5071 | } |
| 5072 | |
| 5073 | /** |
| 5074 | * dwc2_gadget_exit_hibernation() |
| 5075 | * This function is for exiting from Device mode hibernation by host initiated |
| 5076 | * resume/reset and device initiated remote-wakeup. |
| 5077 | * |
| 5078 | * @hsotg: Programming view of the DWC_otg controller |
| 5079 | * @rem_wakeup: indicates whether resume is initiated by Device or Host. |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 5080 | * @reset: indicates whether resume is initiated by Reset. |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5081 | * |
| 5082 | * Return non-zero if failed to exit from hibernation. |
| 5083 | */ |
| 5084 | int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg, |
| 5085 | int rem_wakeup, int reset) |
| 5086 | { |
| 5087 | u32 pcgcctl; |
| 5088 | u32 gpwrdn; |
| 5089 | u32 dctl; |
| 5090 | int ret = 0; |
| 5091 | struct dwc2_gregs_backup *gr; |
| 5092 | struct dwc2_dregs_backup *dr; |
| 5093 | |
| 5094 | gr = &hsotg->gr_backup; |
| 5095 | dr = &hsotg->dr_backup; |
| 5096 | |
| 5097 | if (!hsotg->hibernated) { |
| 5098 | dev_dbg(hsotg->dev, "Already exited from Hibernation\n"); |
| 5099 | return 1; |
| 5100 | } |
| 5101 | dev_dbg(hsotg->dev, |
| 5102 | "%s: called with rem_wakeup = %d reset = %d\n", |
| 5103 | __func__, rem_wakeup, reset); |
| 5104 | |
| 5105 | dwc2_hib_restore_common(hsotg, rem_wakeup, 0); |
| 5106 | |
| 5107 | if (!reset) { |
| 5108 | /* Clear all pending interupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5109 | dwc2_writel(hsotg, 0xffffffff, GINTSTS); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | /* De-assert Restore */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5113 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5114 | gpwrdn &= ~GPWRDN_RESTORE; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5115 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5116 | udelay(10); |
| 5117 | |
| 5118 | if (!rem_wakeup) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5119 | pcgcctl = dwc2_readl(hsotg, PCGCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5120 | pcgcctl &= ~PCGCTL_RSTPDWNMODULE; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5121 | dwc2_writel(hsotg, pcgcctl, PCGCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5122 | } |
| 5123 | |
| 5124 | /* Restore GUSBCFG, DCFG and DCTL */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5125 | dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG); |
| 5126 | dwc2_writel(hsotg, dr->dcfg, DCFG); |
| 5127 | dwc2_writel(hsotg, dr->dctl, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5128 | |
| 5129 | /* De-assert Wakeup Logic */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5130 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5131 | gpwrdn &= ~GPWRDN_PMUACTV; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5132 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5133 | |
| 5134 | if (rem_wakeup) { |
| 5135 | udelay(10); |
| 5136 | /* Start Remote Wakeup Signaling */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5137 | dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5138 | } else { |
| 5139 | udelay(50); |
| 5140 | /* Set Device programming done bit */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5141 | dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5142 | dctl |= DCTL_PWRONPRGDONE; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5143 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5144 | } |
| 5145 | /* Wait for interrupts which must be cleared */ |
| 5146 | mdelay(2); |
| 5147 | /* Clear all pending interupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5148 | dwc2_writel(hsotg, 0xffffffff, GINTSTS); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5149 | |
| 5150 | /* Restore global registers */ |
| 5151 | ret = dwc2_restore_global_registers(hsotg); |
| 5152 | if (ret) { |
| 5153 | dev_err(hsotg->dev, "%s: failed to restore registers\n", |
| 5154 | __func__); |
| 5155 | return ret; |
| 5156 | } |
| 5157 | |
| 5158 | /* Restore device registers */ |
| 5159 | ret = dwc2_restore_device_registers(hsotg, rem_wakeup); |
| 5160 | if (ret) { |
| 5161 | dev_err(hsotg->dev, "%s: failed to restore device registers\n", |
| 5162 | __func__); |
| 5163 | return ret; |
| 5164 | } |
| 5165 | |
| 5166 | if (rem_wakeup) { |
| 5167 | mdelay(10); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5168 | dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5169 | dctl &= ~DCTL_RMTWKUPSIG; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5170 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | hsotg->hibernated = 0; |
| 5174 | hsotg->lx_state = DWC2_L0; |
| 5175 | dev_dbg(hsotg->dev, "Hibernation recovery completes here\n"); |
| 5176 | |
| 5177 | return ret; |
| 5178 | } |