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> |
Minas Harutyunyan | b4c53b4 | 2019-03-12 11:45:12 +0400 | [diff] [blame] | 30 | #include <linux/usb/composite.h> |
| 31 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 32 | |
Dinh Nguyen | f7c0b14 | 2014-04-14 14:13:35 -0700 | [diff] [blame] | 33 | #include "core.h" |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 34 | #include "hw.h" |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 35 | |
| 36 | /* conversion functions */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 37 | static inline struct dwc2_hsotg_req *our_req(struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 38 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 39 | return container_of(req, struct dwc2_hsotg_req, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 42 | static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 43 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 44 | return container_of(ep, struct dwc2_hsotg_ep, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 47 | static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 48 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 49 | return container_of(gadget, struct dwc2_hsotg, gadget); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 52 | 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] | 53 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 54 | dwc2_writel(hsotg, dwc2_readl(hsotg, offset) | val, offset); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 57 | 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] | 58 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 59 | dwc2_writel(hsotg, dwc2_readl(hsotg, offset) & ~val, offset); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 62 | 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] | 63 | u32 ep_index, u32 dir_in) |
| 64 | { |
| 65 | if (dir_in) |
| 66 | return hsotg->eps_in[ep_index]; |
| 67 | else |
| 68 | return hsotg->eps_out[ep_index]; |
| 69 | } |
| 70 | |
Mickael Maison | 997f4f8 | 2014-12-23 17:39:45 +0100 | [diff] [blame] | 71 | /* forward declaration of functions */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 72 | static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * using_dma - return the DMA status of the driver. |
| 76 | * @hsotg: The driver state. |
| 77 | * |
| 78 | * Return true if we're using DMA. |
| 79 | * |
| 80 | * Currently, we have the DMA support code worked into everywhere |
| 81 | * that needs it, but the AMBA DMA implementation in the hardware can |
| 82 | * only DMA from 32bit aligned addresses. This means that gadgets such |
| 83 | * as the CDC Ethernet cannot work as they often pass packets which are |
| 84 | * not 32bit aligned. |
| 85 | * |
| 86 | * Unfortunately the choice to use DMA or not is global to the controller |
| 87 | * and seems to be only settable when the controller is being put through |
| 88 | * a core reset. This means we either need to fix the gadgets to take |
| 89 | * account of DMA alignment, or add bounce buffers (yuerk). |
| 90 | * |
Gregory Herrero | edd74be | 2015-01-09 13:38:48 +0100 | [diff] [blame] | 91 | * g_using_dma is set depending on dts flag. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 92 | */ |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 93 | static inline bool using_dma(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 94 | { |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 95 | return hsotg->params.g_dma; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Vahram Aharonyan | dec4b55 | 2016-11-09 19:27:48 -0800 | [diff] [blame] | 98 | /* |
| 99 | * using_desc_dma - return the descriptor DMA status of the driver. |
| 100 | * @hsotg: The driver state. |
| 101 | * |
| 102 | * Return true if we're using descriptor DMA. |
| 103 | */ |
| 104 | static inline bool using_desc_dma(struct dwc2_hsotg *hsotg) |
| 105 | { |
| 106 | return hsotg->params.g_dma_desc; |
| 107 | } |
| 108 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 109 | /** |
Vardan Mikayelyan | 92d1635 | 2016-05-25 18:07:05 -0700 | [diff] [blame] | 110 | * dwc2_gadget_incr_frame_num - Increments the targeted frame number. |
| 111 | * @hs_ep: The endpoint |
Vardan Mikayelyan | 92d1635 | 2016-05-25 18:07:05 -0700 | [diff] [blame] | 112 | * |
| 113 | * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT. |
| 114 | * If an overrun occurs it will wrap the value and set the frame_overrun flag. |
| 115 | */ |
| 116 | static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep) |
| 117 | { |
| 118 | hs_ep->target_frame += hs_ep->interval; |
| 119 | if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) { |
Gustavo A. R. Silva | c1d5df6 | 2018-01-23 09:45:31 -0600 | [diff] [blame] | 120 | hs_ep->frame_overrun = true; |
Vardan Mikayelyan | 92d1635 | 2016-05-25 18:07:05 -0700 | [diff] [blame] | 121 | hs_ep->target_frame &= DSTS_SOFFN_LIMIT; |
| 122 | } else { |
Gustavo A. R. Silva | c1d5df6 | 2018-01-23 09:45:31 -0600 | [diff] [blame] | 123 | hs_ep->frame_overrun = false; |
Vardan Mikayelyan | 92d1635 | 2016-05-25 18:07:05 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
Grigor Tovmasyan | 9d630b9 | 2018-08-29 21:00:03 +0400 | [diff] [blame] | 128 | * dwc2_gadget_dec_frame_num_by_one - Decrements the targeted frame number |
| 129 | * by one. |
| 130 | * @hs_ep: The endpoint. |
| 131 | * |
| 132 | * This function used in service interval based scheduling flow to calculate |
| 133 | * descriptor frame number filed value. For service interval mode frame |
| 134 | * number in descriptor should point to last (u)frame in the interval. |
| 135 | * |
| 136 | */ |
| 137 | static inline void dwc2_gadget_dec_frame_num_by_one(struct dwc2_hsotg_ep *hs_ep) |
| 138 | { |
| 139 | if (hs_ep->target_frame) |
| 140 | hs_ep->target_frame -= 1; |
| 141 | else |
| 142 | hs_ep->target_frame = DSTS_SOFFN_LIMIT; |
| 143 | } |
| 144 | |
| 145 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 146 | * dwc2_hsotg_en_gsint - enable one or more of the general interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 147 | * @hsotg: The device state |
| 148 | * @ints: A bitmask of the interrupts to enable |
| 149 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 150 | static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 151 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 152 | u32 gsintmsk = dwc2_readl(hsotg, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 153 | u32 new_gsintmsk; |
| 154 | |
| 155 | new_gsintmsk = gsintmsk | ints; |
| 156 | |
| 157 | if (new_gsintmsk != gsintmsk) { |
| 158 | dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 159 | dwc2_writel(hsotg, new_gsintmsk, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 164 | * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 165 | * @hsotg: The device state |
| 166 | * @ints: A bitmask of the interrupts to enable |
| 167 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 168 | static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 169 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 170 | u32 gsintmsk = dwc2_readl(hsotg, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 171 | u32 new_gsintmsk; |
| 172 | |
| 173 | new_gsintmsk = gsintmsk & ~ints; |
| 174 | |
| 175 | if (new_gsintmsk != gsintmsk) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 176 | dwc2_writel(hsotg, new_gsintmsk, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 180 | * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 181 | * @hsotg: The device state |
| 182 | * @ep: The endpoint index |
| 183 | * @dir_in: True if direction is in. |
| 184 | * @en: The enable value, true to enable |
| 185 | * |
| 186 | * Set or clear the mask for an individual endpoint's interrupt |
| 187 | * request. |
| 188 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 189 | static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 190 | unsigned int ep, unsigned int dir_in, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 191 | unsigned int en) |
| 192 | { |
| 193 | unsigned long flags; |
| 194 | u32 bit = 1 << ep; |
| 195 | u32 daint; |
| 196 | |
| 197 | if (!dir_in) |
| 198 | bit <<= 16; |
| 199 | |
| 200 | local_irq_save(flags); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 201 | daint = dwc2_readl(hsotg, DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 202 | if (en) |
| 203 | daint |= bit; |
| 204 | else |
| 205 | daint &= ~bit; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 206 | dwc2_writel(hsotg, daint, DAINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 207 | local_irq_restore(flags); |
| 208 | } |
| 209 | |
| 210 | /** |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 211 | * 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] | 212 | * |
| 213 | * @hsotg: Programming view of the DWC_otg controller |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 214 | */ |
| 215 | int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg) |
| 216 | { |
| 217 | if (hsotg->hw_params.en_multiple_tx_fifo) |
| 218 | /* In dedicated FIFO mode we need count of IN EPs */ |
Minas Harutyunyan | 9273083 | 2017-11-30 12:16:37 +0400 | [diff] [blame] | 219 | return hsotg->hw_params.num_dev_in_eps; |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 220 | else |
| 221 | /* In shared FIFO mode we need count of Periodic IN EPs */ |
| 222 | return hsotg->hw_params.num_dev_perio_in_ep; |
| 223 | } |
| 224 | |
| 225 | /** |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 226 | * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for |
| 227 | * device mode TX FIFOs |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 228 | * |
| 229 | * @hsotg: Programming view of the DWC_otg controller |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 230 | */ |
| 231 | int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg) |
| 232 | { |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 233 | int addr; |
| 234 | int tx_addr_max; |
| 235 | u32 np_tx_fifo_size; |
| 236 | |
| 237 | np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size, |
| 238 | hsotg->params.g_np_tx_fifo_size); |
| 239 | |
| 240 | /* Get Endpoint Info Control block size in DWORDs. */ |
Minas Harutyunyan | 9273083 | 2017-11-30 12:16:37 +0400 | [diff] [blame] | 241 | tx_addr_max = hsotg->hw_params.total_fifo_size; |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 242 | |
| 243 | addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size; |
| 244 | if (tx_addr_max <= addr) |
| 245 | return 0; |
| 246 | |
| 247 | return tx_addr_max - addr; |
| 248 | } |
| 249 | |
| 250 | /** |
Grigor Tovmasyan | 187c529 | 2018-08-29 21:02:57 +0400 | [diff] [blame] | 251 | * dwc2_gadget_wkup_alert_handler - Handler for WKUP_ALERT interrupt |
| 252 | * |
| 253 | * @hsotg: Programming view of the DWC_otg controller |
| 254 | * |
| 255 | */ |
| 256 | static void dwc2_gadget_wkup_alert_handler(struct dwc2_hsotg *hsotg) |
| 257 | { |
| 258 | u32 gintsts2; |
| 259 | u32 gintmsk2; |
| 260 | |
| 261 | gintsts2 = dwc2_readl(hsotg, GINTSTS2); |
| 262 | gintmsk2 = dwc2_readl(hsotg, GINTMSK2); |
| 263 | |
| 264 | if (gintsts2 & GINTSTS2_WKUP_ALERT_INT) { |
| 265 | dev_dbg(hsotg->dev, "%s: Wkup_Alert_Int\n", __func__); |
Minas Harutyunyan | 87b6d2c | 2018-12-12 16:44:32 +0400 | [diff] [blame] | 266 | dwc2_set_bit(hsotg, GINTSTS2, GINTSTS2_WKUP_ALERT_INT); |
Artur Petrosyan | d64bc8e | 2018-11-02 11:29:48 -0400 | [diff] [blame] | 267 | dwc2_set_bit(hsotg, DCTL, DCTL_RMTWKUPSIG); |
Grigor Tovmasyan | 187c529 | 2018-08-29 21:02:57 +0400 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 272 | * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode |
| 273 | * TX FIFOs |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 274 | * |
| 275 | * @hsotg: Programming view of the DWC_otg controller |
Sevak Arakelyan | c138ecf | 2017-01-23 15:01:23 -0800 | [diff] [blame] | 276 | */ |
| 277 | int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg) |
| 278 | { |
| 279 | int tx_fifo_count; |
| 280 | int tx_fifo_depth; |
| 281 | |
| 282 | tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg); |
| 283 | |
| 284 | tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg); |
| 285 | |
| 286 | if (!tx_fifo_count) |
| 287 | return tx_fifo_depth; |
| 288 | else |
| 289 | return tx_fifo_depth / tx_fifo_count; |
| 290 | } |
| 291 | |
| 292 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 293 | * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 294 | * @hsotg: The device instance. |
| 295 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 296 | static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 297 | { |
John Youn | 2317eac | 2016-10-17 17:36:23 -0700 | [diff] [blame] | 298 | unsigned int ep; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 299 | unsigned int addr; |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 300 | int timeout; |
Sevak Arakelyan | 79d6b8c | 2018-01-19 14:39:31 +0400 | [diff] [blame] | 301 | |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 302 | u32 val; |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 303 | u32 *txfsz = hsotg->params.g_tx_fifo_size; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 304 | |
Gregory Herrero | 7fcbc95 | 2015-01-09 13:39:06 +0100 | [diff] [blame] | 305 | /* Reset fifo map if not correctly cleared during previous session */ |
| 306 | WARN_ON(hsotg->fifo_map); |
| 307 | hsotg->fifo_map = 0; |
| 308 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 309 | /* set RX/NPTX FIFO sizes */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 310 | dwc2_writel(hsotg, hsotg->params.g_rx_fifo_size, GRXFSIZ); |
| 311 | dwc2_writel(hsotg, (hsotg->params.g_rx_fifo_size << |
| 312 | FIFOSIZE_STARTADDR_SHIFT) | |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 313 | (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT), |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 314 | GNPTXFSIZ); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 315 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 316 | /* |
| 317 | * 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] | 318 | * block have overlapping default addresses. This also ensures |
| 319 | * that if the settings have been changed, then they are set to |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 320 | * known values. |
| 321 | */ |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 322 | |
| 323 | /* start at the end of the GNPTXFSIZ, rounded up */ |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 324 | 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] | 325 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 326 | /* |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 327 | * Configure fifos sizes from provided configuration and assign |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 328 | * them to endpoints dynamically according to maxpacket size value of |
| 329 | * given endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 330 | */ |
John Youn | 2317eac | 2016-10-17 17:36:23 -0700 | [diff] [blame] | 331 | for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) { |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 332 | if (!txfsz[ep]) |
John Youn | 3fa9538 | 2016-10-17 17:36:25 -0700 | [diff] [blame] | 333 | continue; |
| 334 | val = addr; |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 335 | val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT; |
| 336 | WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem, |
John Youn | 3fa9538 | 2016-10-17 17:36:25 -0700 | [diff] [blame] | 337 | "insufficient fifo memory"); |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 338 | addr += txfsz[ep]; |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 339 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 340 | dwc2_writel(hsotg, val, DPTXFSIZN(ep)); |
| 341 | val = dwc2_readl(hsotg, DPTXFSIZN(ep)); |
Ben Dooks | 0f002d2 | 2010-05-25 05:36:50 +0100 | [diff] [blame] | 342 | } |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 343 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 344 | dwc2_writel(hsotg, hsotg->hw_params.total_fifo_size | |
Sevak Arakelyan | f87c842 | 2017-01-18 18:34:19 -0800 | [diff] [blame] | 345 | addr << GDFIFOCFG_EPINFOBASE_SHIFT, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 346 | GDFIFOCFG); |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 347 | /* |
| 348 | * according to p428 of the design guide, we need to ensure that |
| 349 | * all fifos are flushed before continuing |
| 350 | */ |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 351 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 352 | dwc2_writel(hsotg, GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH | |
| 353 | GRSTCTL_RXFFLSH, GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 354 | |
| 355 | /* wait until the fifos are both flushed */ |
| 356 | timeout = 100; |
| 357 | while (1) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 358 | val = dwc2_readl(hsotg, GRSTCTL); |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 359 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 360 | if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0) |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 361 | break; |
| 362 | |
| 363 | if (--timeout == 0) { |
| 364 | dev_err(hsotg->dev, |
| 365 | "%s: timeout flushing fifos (GRSTCTL=%08x)\n", |
| 366 | __func__, val); |
Gregory Herrero | 48b20bc | 2015-01-09 13:39:01 +0100 | [diff] [blame] | 367 | break; |
Ben Dooks | 1703a6d | 2010-05-25 05:36:52 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | udelay(1); |
| 371 | } |
| 372 | |
| 373 | dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 377 | * dwc2_hsotg_ep_alloc_request - allocate USB rerequest structure |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 378 | * @ep: USB endpoint to allocate request for. |
| 379 | * @flags: Allocation flags |
| 380 | * |
| 381 | * Allocate a new USB request structure appropriate for the specified endpoint |
| 382 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 383 | 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] | 384 | gfp_t flags) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 385 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 386 | struct dwc2_hsotg_req *req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 387 | |
John Youn | ec33efe | 2017-01-17 20:32:41 -0800 | [diff] [blame] | 388 | req = kzalloc(sizeof(*req), flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 389 | if (!req) |
| 390 | return NULL; |
| 391 | |
| 392 | INIT_LIST_HEAD(&req->queue); |
| 393 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 394 | return &req->req; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * is_ep_periodic - return true if the endpoint is in periodic mode. |
| 399 | * @hs_ep: The endpoint to query. |
| 400 | * |
| 401 | * Returns true if the endpoint is in periodic mode, meaning it is being |
| 402 | * used for an Interrupt or ISO transfer. |
| 403 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 404 | static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 405 | { |
| 406 | return hs_ep->periodic; |
| 407 | } |
| 408 | |
| 409 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 410 | * 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] | 411 | * @hsotg: The device state. |
| 412 | * @hs_ep: The endpoint for the request |
| 413 | * @hs_req: The request being processed. |
| 414 | * |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 415 | * 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] | 416 | * 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] | 417 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 418 | static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 419 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 420 | struct dwc2_hsotg_req *hs_req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 421 | { |
| 422 | struct usb_request *req = &hs_req->req; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 423 | |
Jingoo Han | 17d966a | 2013-05-11 21:14:00 +0900 | [diff] [blame] | 424 | usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Vahram Aharonyan | 0f6b80c | 2016-11-09 19:27:56 -0800 | [diff] [blame] | 427 | /* |
| 428 | * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains |
| 429 | * for Control endpoint |
| 430 | * @hsotg: The device state. |
| 431 | * |
| 432 | * This function will allocate 4 descriptor chains for EP 0: 2 for |
| 433 | * Setup stage, per one for IN and OUT data/status transactions. |
| 434 | */ |
| 435 | static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg) |
| 436 | { |
| 437 | hsotg->setup_desc[0] = |
| 438 | dmam_alloc_coherent(hsotg->dev, |
| 439 | sizeof(struct dwc2_dma_desc), |
| 440 | &hsotg->setup_desc_dma[0], |
| 441 | GFP_KERNEL); |
| 442 | if (!hsotg->setup_desc[0]) |
| 443 | goto fail; |
| 444 | |
| 445 | hsotg->setup_desc[1] = |
| 446 | dmam_alloc_coherent(hsotg->dev, |
| 447 | sizeof(struct dwc2_dma_desc), |
| 448 | &hsotg->setup_desc_dma[1], |
| 449 | GFP_KERNEL); |
| 450 | if (!hsotg->setup_desc[1]) |
| 451 | goto fail; |
| 452 | |
| 453 | hsotg->ctrl_in_desc = |
| 454 | dmam_alloc_coherent(hsotg->dev, |
| 455 | sizeof(struct dwc2_dma_desc), |
| 456 | &hsotg->ctrl_in_desc_dma, |
| 457 | GFP_KERNEL); |
| 458 | if (!hsotg->ctrl_in_desc) |
| 459 | goto fail; |
| 460 | |
| 461 | hsotg->ctrl_out_desc = |
| 462 | dmam_alloc_coherent(hsotg->dev, |
| 463 | sizeof(struct dwc2_dma_desc), |
| 464 | &hsotg->ctrl_out_desc_dma, |
| 465 | GFP_KERNEL); |
| 466 | if (!hsotg->ctrl_out_desc) |
| 467 | goto fail; |
| 468 | |
| 469 | return 0; |
| 470 | |
| 471 | fail: |
| 472 | return -ENOMEM; |
| 473 | } |
| 474 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 475 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 476 | * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 477 | * @hsotg: The controller state. |
| 478 | * @hs_ep: The endpoint we're going to write for. |
| 479 | * @hs_req: The request to write data for. |
| 480 | * |
| 481 | * This is called when the TxFIFO has some space in it to hold a new |
| 482 | * transmission and we have something to give it. The actual setup of |
| 483 | * the data size is done elsewhere, so all we have to do is to actually |
| 484 | * write the data. |
| 485 | * |
| 486 | * The return value is zero if there is more space (or nothing was done) |
| 487 | * otherwise -ENOSPC is returned if the FIFO space was used up. |
| 488 | * |
| 489 | * This routine is only needed for PIO |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 490 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 491 | static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 492 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 493 | struct dwc2_hsotg_req *hs_req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 494 | { |
| 495 | bool periodic = is_ep_periodic(hs_ep); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 496 | u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 497 | int buf_pos = hs_req->req.actual; |
| 498 | int to_write = hs_ep->size_loaded; |
| 499 | void *data; |
| 500 | int can_write; |
| 501 | int pkt_round; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 502 | int max_transfer; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 503 | |
| 504 | to_write -= (buf_pos - hs_ep->last_load); |
| 505 | |
| 506 | /* if there's nothing to write, get out early */ |
| 507 | if (to_write == 0) |
| 508 | return 0; |
| 509 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 510 | if (periodic && !hsotg->dedicated_fifos) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 511 | u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 512 | int size_left; |
| 513 | int size_done; |
| 514 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 515 | /* |
| 516 | * work out how much data was loaded so we can calculate |
| 517 | * how much data is left in the fifo. |
| 518 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 519 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 520 | size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 521 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 522 | /* |
| 523 | * if shared fifo, we cannot write anything until the |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 524 | * previous data has been completely sent. |
| 525 | */ |
| 526 | if (hs_ep->fifo_load != 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 527 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); |
Ben Dooks | e7a9ff5 | 2010-07-19 09:40:42 +0100 | [diff] [blame] | 528 | return -ENOSPC; |
| 529 | } |
| 530 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 531 | dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n", |
| 532 | __func__, size_left, |
| 533 | hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size); |
| 534 | |
| 535 | /* how much of the data has moved */ |
| 536 | size_done = hs_ep->size_loaded - size_left; |
| 537 | |
| 538 | /* how much data is left in the fifo */ |
| 539 | can_write = hs_ep->fifo_load - size_done; |
| 540 | dev_dbg(hsotg->dev, "%s: => can_write1=%d\n", |
| 541 | __func__, can_write); |
| 542 | |
| 543 | can_write = hs_ep->fifo_size - can_write; |
| 544 | dev_dbg(hsotg->dev, "%s: => can_write2=%d\n", |
| 545 | __func__, can_write); |
| 546 | |
| 547 | if (can_write <= 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 548 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 549 | return -ENOSPC; |
| 550 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 551 | } else if (hsotg->dedicated_fifos && hs_ep->index != 0) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 552 | can_write = dwc2_readl(hsotg, |
| 553 | DTXFSTS(hs_ep->fifo_index)); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 554 | |
| 555 | can_write &= 0xffff; |
| 556 | can_write *= 4; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 557 | } else { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 558 | if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 559 | dev_dbg(hsotg->dev, |
| 560 | "%s: no queue slots available (0x%08x)\n", |
| 561 | __func__, gnptxsts); |
| 562 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 563 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 564 | return -ENOSPC; |
| 565 | } |
| 566 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 567 | can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts); |
Ben Dooks | 679f9b7 | 2010-07-19 09:40:41 +0100 | [diff] [blame] | 568 | can_write *= 4; /* fifo size is in 32bit quantities. */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 571 | max_transfer = hs_ep->ep.maxpacket * hs_ep->mc; |
| 572 | |
| 573 | 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] | 574 | __func__, gnptxsts, can_write, to_write, max_transfer); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 575 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 576 | /* |
| 577 | * 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] | 578 | * FIFO, requests of >512 cause the endpoint to get stuck with a |
| 579 | * fragment of the end of the transfer in it. |
| 580 | */ |
Robert Baldyga | 811f330 | 2013-09-24 11:24:28 +0200 | [diff] [blame] | 581 | if (can_write > 512 && !periodic) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 582 | can_write = 512; |
| 583 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 584 | /* |
| 585 | * 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] | 586 | * 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] | 587 | * doing it. |
| 588 | */ |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 589 | if (to_write > max_transfer) { |
| 590 | to_write = max_transfer; |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 591 | |
Robert Baldyga | 5cb2ff0 | 2013-09-19 11:50:18 +0200 | [diff] [blame] | 592 | /* it's needed only when we do not use dedicated fifos */ |
| 593 | if (!hsotg->dedicated_fifos) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 594 | dwc2_hsotg_en_gsint(hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 595 | periodic ? GINTSTS_PTXFEMP : |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 596 | GINTSTS_NPTXFEMP); |
Ben Dooks | 03e10e5 | 2010-07-19 09:40:45 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 599 | /* see if we can write data */ |
| 600 | |
| 601 | if (to_write > can_write) { |
| 602 | to_write = can_write; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 603 | pkt_round = to_write % max_transfer; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 604 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 605 | /* |
| 606 | * Round the write down to an |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 607 | * exact number of packets. |
| 608 | * |
| 609 | * Note, we do not currently check to see if we can ever |
| 610 | * write a full packet or not to the FIFO. |
| 611 | */ |
| 612 | |
| 613 | if (pkt_round) |
| 614 | to_write -= pkt_round; |
| 615 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 616 | /* |
| 617 | * enable correct FIFO interrupt to alert us when there |
| 618 | * is more room left. |
| 619 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 620 | |
Robert Baldyga | 5cb2ff0 | 2013-09-19 11:50:18 +0200 | [diff] [blame] | 621 | /* it's needed only when we do not use dedicated fifos */ |
| 622 | if (!hsotg->dedicated_fifos) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 623 | dwc2_hsotg_en_gsint(hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 624 | periodic ? GINTSTS_PTXFEMP : |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 625 | GINTSTS_NPTXFEMP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | 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] | 629 | to_write, hs_req->req.length, can_write, buf_pos); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 630 | |
| 631 | if (to_write <= 0) |
| 632 | return -ENOSPC; |
| 633 | |
| 634 | hs_req->req.actual = buf_pos + to_write; |
| 635 | hs_ep->total_data += to_write; |
| 636 | |
| 637 | if (periodic) |
| 638 | hs_ep->fifo_load += to_write; |
| 639 | |
| 640 | to_write = DIV_ROUND_UP(to_write, 4); |
| 641 | data = hs_req->req.buf + buf_pos; |
| 642 | |
Gevorg Sahakyan | 342ccce | 2018-07-26 18:00:41 +0400 | [diff] [blame] | 643 | dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 644 | |
| 645 | return (to_write >= can_write) ? -ENOSPC : 0; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * get_ep_limit - get the maximum data legnth for this endpoint |
| 650 | * @hs_ep: The endpoint |
| 651 | * |
| 652 | * Return the maximum data that can be queued in one go on a given endpoint |
| 653 | * so that transfers that are too long can be split. |
| 654 | */ |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 655 | static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 656 | { |
| 657 | int index = hs_ep->index; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 658 | unsigned int maxsize; |
| 659 | unsigned int maxpkt; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 660 | |
| 661 | if (index != 0) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 662 | maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1; |
| 663 | maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 664 | } else { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 665 | maxsize = 64 + 64; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 666 | if (hs_ep->dir_in) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 667 | maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1; |
Jingoo Han | 66e5c64 | 2011-05-13 21:26:15 +0900 | [diff] [blame] | 668 | else |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 669 | maxpkt = 2; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* we made the constant loading easier above by using +1 */ |
| 673 | maxpkt--; |
| 674 | maxsize--; |
| 675 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 676 | /* |
| 677 | * constrain by packet count if maxpkts*pktsize is greater |
| 678 | * than the length register size. |
| 679 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 680 | |
| 681 | if ((maxpkt * hs_ep->ep.maxpacket) < maxsize) |
| 682 | maxsize = maxpkt * hs_ep->ep.maxpacket; |
| 683 | |
| 684 | return maxsize; |
| 685 | } |
| 686 | |
| 687 | /** |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 688 | * dwc2_hsotg_read_frameno - read current frame number |
| 689 | * @hsotg: The device instance |
| 690 | * |
| 691 | * Return the current frame number |
| 692 | */ |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 693 | static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg) |
| 694 | { |
| 695 | u32 dsts; |
| 696 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 697 | dsts = dwc2_readl(hsotg, DSTS); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 698 | dsts &= DSTS_SOFFN_MASK; |
| 699 | dsts >>= DSTS_SOFFN_SHIFT; |
| 700 | |
| 701 | return dsts; |
| 702 | } |
| 703 | |
| 704 | /** |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 705 | * dwc2_gadget_get_chain_limit - get the maximum data payload value of the |
| 706 | * DMA descriptor chain prepared for specific endpoint |
| 707 | * @hs_ep: The endpoint |
| 708 | * |
| 709 | * Return the maximum data that can be queued in one go on a given endpoint |
| 710 | * depending on its descriptor chain capacity so that transfers that |
| 711 | * are too long can be split. |
| 712 | */ |
| 713 | static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep) |
| 714 | { |
| 715 | int is_isoc = hs_ep->isochronous; |
| 716 | unsigned int maxsize; |
| 717 | |
| 718 | if (is_isoc) |
Minas Harutyunyan | 54f37f5 | 2019-03-18 14:24:30 +0400 | [diff] [blame] | 719 | maxsize = (hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT : |
| 720 | DEV_DMA_ISOC_RX_NBYTES_LIMIT) * |
| 721 | MAX_DMA_DESC_NUM_HS_ISOC; |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 722 | else |
Minas Harutyunyan | 54f37f5 | 2019-03-18 14:24:30 +0400 | [diff] [blame] | 723 | maxsize = DEV_DMA_NBYTES_LIMIT * MAX_DMA_DESC_NUM_GENERIC; |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 724 | |
| 725 | return maxsize; |
| 726 | } |
| 727 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 728 | /* |
| 729 | * dwc2_gadget_get_desc_params - get DMA descriptor parameters. |
| 730 | * @hs_ep: The endpoint |
| 731 | * @mask: RX/TX bytes mask to be defined |
| 732 | * |
| 733 | * Returns maximum data payload for one descriptor after analyzing endpoint |
| 734 | * characteristics. |
| 735 | * DMA descriptor transfer bytes limit depends on EP type: |
| 736 | * Control out - MPS, |
| 737 | * Isochronous - descriptor rx/tx bytes bitfield limit, |
| 738 | * Control In/Bulk/Interrupt - multiple of mps. This will allow to not |
| 739 | * have concatenations from various descriptors within one packet. |
| 740 | * |
| 741 | * Selects corresponding mask for RX/TX bytes as well. |
| 742 | */ |
| 743 | static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask) |
| 744 | { |
| 745 | u32 mps = hs_ep->ep.maxpacket; |
| 746 | int dir_in = hs_ep->dir_in; |
| 747 | u32 desc_size = 0; |
| 748 | |
| 749 | if (!hs_ep->index && !dir_in) { |
| 750 | desc_size = mps; |
| 751 | *mask = DEV_DMA_NBYTES_MASK; |
| 752 | } else if (hs_ep->isochronous) { |
| 753 | if (dir_in) { |
| 754 | desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT; |
| 755 | *mask = DEV_DMA_ISOC_TX_NBYTES_MASK; |
| 756 | } else { |
| 757 | desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT; |
| 758 | *mask = DEV_DMA_ISOC_RX_NBYTES_MASK; |
| 759 | } |
| 760 | } else { |
| 761 | desc_size = DEV_DMA_NBYTES_LIMIT; |
| 762 | *mask = DEV_DMA_NBYTES_MASK; |
| 763 | |
| 764 | /* Round down desc_size to be mps multiple */ |
| 765 | desc_size -= desc_size % mps; |
| 766 | } |
| 767 | |
| 768 | return desc_size; |
| 769 | } |
| 770 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 771 | static void dwc2_gadget_fill_nonisoc_xfer_ddma_one(struct dwc2_hsotg_ep *hs_ep, |
| 772 | struct dwc2_dma_desc **desc, |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 773 | dma_addr_t dma_buff, |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 774 | unsigned int len, |
| 775 | bool true_last) |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 776 | { |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 777 | int dir_in = hs_ep->dir_in; |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 778 | u32 mps = hs_ep->ep.maxpacket; |
| 779 | u32 maxsize = 0; |
| 780 | u32 offset = 0; |
| 781 | u32 mask = 0; |
| 782 | int i; |
| 783 | |
| 784 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
| 785 | |
| 786 | hs_ep->desc_count = (len / maxsize) + |
| 787 | ((len % maxsize) ? 1 : 0); |
| 788 | if (len == 0) |
| 789 | hs_ep->desc_count = 1; |
| 790 | |
| 791 | for (i = 0; i < hs_ep->desc_count; ++i) { |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 792 | (*desc)->status = 0; |
| 793 | (*desc)->status |= (DEV_DMA_BUFF_STS_HBUSY |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 794 | << DEV_DMA_BUFF_STS_SHIFT); |
| 795 | |
| 796 | if (len > maxsize) { |
| 797 | if (!hs_ep->index && !dir_in) |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 798 | (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC); |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 799 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 800 | (*desc)->status |= |
| 801 | maxsize << DEV_DMA_NBYTES_SHIFT & mask; |
| 802 | (*desc)->buf = dma_buff + offset; |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 803 | |
| 804 | len -= maxsize; |
| 805 | offset += maxsize; |
| 806 | } else { |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 807 | if (true_last) |
| 808 | (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC); |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 809 | |
| 810 | if (dir_in) |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 811 | (*desc)->status |= (len % mps) ? DEV_DMA_SHORT : |
| 812 | ((hs_ep->send_zlp && true_last) ? |
| 813 | DEV_DMA_SHORT : 0); |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 814 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 815 | (*desc)->status |= |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 816 | len << DEV_DMA_NBYTES_SHIFT & mask; |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 817 | (*desc)->buf = dma_buff + offset; |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 818 | } |
| 819 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 820 | (*desc)->status &= ~DEV_DMA_BUFF_STS_MASK; |
| 821 | (*desc)->status |= (DEV_DMA_BUFF_STS_HREADY |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 822 | << DEV_DMA_BUFF_STS_SHIFT); |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 823 | (*desc)++; |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 827 | /* |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 828 | * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain. |
| 829 | * @hs_ep: The endpoint |
| 830 | * @ureq: Request to transfer |
| 831 | * @offset: offset in bytes |
| 832 | * @len: Length of the transfer |
| 833 | * |
| 834 | * This function will iterate over descriptor chain and fill its entries |
| 835 | * with corresponding information based on transfer data. |
| 836 | */ |
| 837 | static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep, |
| 838 | struct usb_request *ureq, |
| 839 | unsigned int offset, |
| 840 | unsigned int len) |
| 841 | { |
| 842 | struct dwc2_dma_desc *desc = hs_ep->desc_list; |
| 843 | struct scatterlist *sg; |
| 844 | int i; |
| 845 | u8 desc_count = 0; |
| 846 | |
| 847 | /* non-DMA sg buffer */ |
| 848 | if (!ureq->num_sgs) { |
| 849 | dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc, |
| 850 | ureq->dma + offset, len, true); |
| 851 | return; |
| 852 | } |
| 853 | |
| 854 | /* DMA sg buffer */ |
| 855 | for_each_sg(ureq->sg, sg, ureq->num_sgs, i) { |
| 856 | dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc, |
| 857 | sg_dma_address(sg) + sg->offset, sg_dma_len(sg), |
| 858 | sg_is_last(sg)); |
| 859 | desc_count += hs_ep->desc_count; |
| 860 | } |
| 861 | |
| 862 | hs_ep->desc_count = desc_count; |
| 863 | } |
| 864 | |
| 865 | /* |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 866 | * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain. |
| 867 | * @hs_ep: The isochronous endpoint. |
| 868 | * @dma_buff: usb requests dma buffer. |
| 869 | * @len: usb request transfer length. |
| 870 | * |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 871 | * Fills next free descriptor with the data of the arrived usb request, |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 872 | * frame info, sets Last and IOC bits increments next_desc. If filled |
| 873 | * descriptor is not the first one, removes L bit from the previous descriptor |
| 874 | * status. |
| 875 | */ |
| 876 | static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep, |
| 877 | dma_addr_t dma_buff, unsigned int len) |
| 878 | { |
| 879 | struct dwc2_dma_desc *desc; |
| 880 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 881 | u32 index; |
| 882 | u32 maxsize = 0; |
| 883 | u32 mask = 0; |
Minas Harutyunyan | 1d8e5c0 | 2018-05-23 16:24:44 +0400 | [diff] [blame] | 884 | u8 pid = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 885 | |
| 886 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 887 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 888 | index = hs_ep->next_desc; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 889 | desc = &hs_ep->desc_list[index]; |
| 890 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 891 | /* Check if descriptor chain full */ |
| 892 | if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) == |
| 893 | DEV_DMA_BUFF_STS_HREADY) { |
| 894 | dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__); |
| 895 | return 1; |
| 896 | } |
| 897 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 898 | /* Clear L bit of previous desc if more than one entries in the chain */ |
| 899 | if (hs_ep->next_desc) |
| 900 | hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L; |
| 901 | |
| 902 | dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n", |
| 903 | __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index); |
| 904 | |
| 905 | desc->status = 0; |
| 906 | desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT); |
| 907 | |
| 908 | desc->buf = dma_buff; |
| 909 | desc->status |= (DEV_DMA_L | DEV_DMA_IOC | |
| 910 | ((len << DEV_DMA_NBYTES_SHIFT) & mask)); |
| 911 | |
| 912 | if (hs_ep->dir_in) { |
Minas Harutyunyan | 1d8e5c0 | 2018-05-23 16:24:44 +0400 | [diff] [blame] | 913 | if (len) |
| 914 | pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket); |
| 915 | else |
| 916 | pid = 1; |
| 917 | desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) & |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 918 | DEV_DMA_ISOC_PID_MASK) | |
| 919 | ((len % hs_ep->ep.maxpacket) ? |
| 920 | DEV_DMA_SHORT : 0) | |
| 921 | ((hs_ep->target_frame << |
| 922 | DEV_DMA_ISOC_FRNUM_SHIFT) & |
| 923 | DEV_DMA_ISOC_FRNUM_MASK); |
| 924 | } |
| 925 | |
| 926 | desc->status &= ~DEV_DMA_BUFF_STS_MASK; |
| 927 | desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT); |
| 928 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 929 | /* Increment frame number by interval for IN */ |
| 930 | if (hs_ep->dir_in) |
| 931 | dwc2_gadget_incr_frame_num(hs_ep); |
| 932 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 933 | /* Update index of last configured entry in the chain */ |
| 934 | hs_ep->next_desc++; |
Minas Harutyunyan | 54f37f5 | 2019-03-18 14:24:30 +0400 | [diff] [blame] | 935 | if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_HS_ISOC) |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 936 | hs_ep->next_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA |
| 943 | * @hs_ep: The isochronous endpoint. |
| 944 | * |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 945 | * Prepare descriptor chain for isochronous endpoints. Afterwards |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 946 | * write DMA address to HW and enable the endpoint. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 947 | */ |
| 948 | static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 949 | { |
| 950 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 951 | struct dwc2_hsotg_req *hs_req, *treq; |
| 952 | int index = hs_ep->index; |
| 953 | int ret; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 954 | int i; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 955 | u32 dma_reg; |
| 956 | u32 depctl; |
| 957 | u32 ctrl; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 958 | struct dwc2_dma_desc *desc; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 959 | |
| 960 | if (list_empty(&hs_ep->queue)) { |
Minas Harutyunyan | 1ffba90 | 2018-06-12 12:37:29 +0400 | [diff] [blame] | 961 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 962 | dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__); |
| 963 | return; |
| 964 | } |
| 965 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 966 | /* Initialize descriptor chain by Host Busy status */ |
Minas Harutyunyan | 54f37f5 | 2019-03-18 14:24:30 +0400 | [diff] [blame] | 967 | for (i = 0; i < MAX_DMA_DESC_NUM_HS_ISOC; i++) { |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 968 | desc = &hs_ep->desc_list[i]; |
| 969 | desc->status = 0; |
| 970 | desc->status |= (DEV_DMA_BUFF_STS_HBUSY |
| 971 | << DEV_DMA_BUFF_STS_SHIFT); |
| 972 | } |
| 973 | |
| 974 | hs_ep->next_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 975 | list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) { |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 976 | dma_addr_t dma_addr = hs_req->req.dma; |
| 977 | |
| 978 | if (hs_req->req.num_sgs) { |
| 979 | WARN_ON(hs_req->req.num_sgs > 1); |
| 980 | dma_addr = sg_dma_address(hs_req->req.sg); |
| 981 | } |
| 982 | ret = dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr, |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 983 | hs_req->req.length); |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 984 | if (ret) |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 985 | break; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 986 | } |
| 987 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 988 | hs_ep->compl_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 989 | depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 990 | dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index); |
| 991 | |
| 992 | /* write descriptor chain address to control register */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 993 | dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 994 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 995 | ctrl = dwc2_readl(hsotg, depctl); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 996 | ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 997 | dwc2_writel(hsotg, ctrl, depctl); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 1000 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1001 | * 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] | 1002 | * @hsotg: The controller state. |
| 1003 | * @hs_ep: The endpoint to process a request for |
| 1004 | * @hs_req: The request to start. |
| 1005 | * @continuing: True if we are doing more for the current request. |
| 1006 | * |
| 1007 | * Start the given request running by setting the endpoint registers |
| 1008 | * appropriately, and writing any data to the FIFOs. |
| 1009 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1010 | static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1011 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1012 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1013 | bool continuing) |
| 1014 | { |
| 1015 | struct usb_request *ureq = &hs_req->req; |
| 1016 | int index = hs_ep->index; |
| 1017 | int dir_in = hs_ep->dir_in; |
| 1018 | u32 epctrl_reg; |
| 1019 | u32 epsize_reg; |
| 1020 | u32 epsize; |
| 1021 | u32 ctrl; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1022 | unsigned int length; |
| 1023 | unsigned int packets; |
| 1024 | unsigned int maxreq; |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1025 | unsigned int dma_reg; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1026 | |
| 1027 | if (index != 0) { |
| 1028 | if (hs_ep->req && !continuing) { |
| 1029 | dev_err(hsotg->dev, "%s: active request\n", __func__); |
| 1030 | WARN_ON(1); |
| 1031 | return; |
| 1032 | } else if (hs_ep->req != hs_req && continuing) { |
| 1033 | dev_err(hsotg->dev, |
| 1034 | "%s: continue different req\n", __func__); |
| 1035 | WARN_ON(1); |
| 1036 | return; |
| 1037 | } |
| 1038 | } |
| 1039 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1040 | dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1041 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 1042 | epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1043 | |
| 1044 | 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] | 1045 | __func__, dwc2_readl(hsotg, epctrl_reg), index, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1046 | hs_ep->dir_in ? "in" : "out"); |
| 1047 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1048 | /* If endpoint is stalled, we will restart request later */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1049 | ctrl = dwc2_readl(hsotg, epctrl_reg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1050 | |
Mian Yousaf Kaukab | b2d4c54 | 2015-09-29 12:08:22 +0200 | [diff] [blame] | 1051 | if (index && ctrl & DXEPCTL_STALL) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1052 | dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index); |
| 1053 | return; |
| 1054 | } |
| 1055 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1056 | length = ureq->length - ureq->actual; |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1057 | dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n", |
| 1058 | ureq->length, ureq->actual); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1059 | |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 1060 | if (!using_desc_dma(hsotg)) |
| 1061 | maxreq = get_ep_limit(hs_ep); |
| 1062 | else |
| 1063 | maxreq = dwc2_gadget_get_chain_limit(hs_ep); |
| 1064 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1065 | if (length > maxreq) { |
| 1066 | int round = maxreq % hs_ep->ep.maxpacket; |
| 1067 | |
| 1068 | dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n", |
| 1069 | __func__, length, maxreq, round); |
| 1070 | |
| 1071 | /* round down to multiple of packets */ |
| 1072 | if (round) |
| 1073 | maxreq -= round; |
| 1074 | |
| 1075 | length = maxreq; |
| 1076 | } |
| 1077 | |
| 1078 | if (length) |
| 1079 | packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket); |
| 1080 | else |
| 1081 | packets = 1; /* send one packet if length is zero. */ |
| 1082 | |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1083 | if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) { |
| 1084 | dev_err(hsotg->dev, "req length > maxpacket*mc\n"); |
| 1085 | return; |
| 1086 | } |
| 1087 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1088 | if (dir_in && index != 0) |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1089 | if (hs_ep->isochronous) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1090 | epsize = DXEPTSIZ_MC(packets); |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1091 | else |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1092 | epsize = DXEPTSIZ_MC(1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1093 | else |
| 1094 | epsize = 0; |
| 1095 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1096 | /* |
| 1097 | * zero length packet should be programmed on its own and should not |
| 1098 | * be counted in DIEPTSIZ.PktCnt with other packets. |
| 1099 | */ |
| 1100 | if (dir_in && ureq->zero && !continuing) { |
| 1101 | /* Test if zlp is actually required. */ |
| 1102 | if ((ureq->length >= hs_ep->ep.maxpacket) && |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1103 | !(ureq->length % hs_ep->ep.maxpacket)) |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 1104 | hs_ep->send_zlp = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1105 | } |
| 1106 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1107 | epsize |= DXEPTSIZ_PKTCNT(packets); |
| 1108 | epsize |= DXEPTSIZ_XFERSIZE(length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1109 | |
| 1110 | dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n", |
| 1111 | __func__, packets, length, ureq->length, epsize, epsize_reg); |
| 1112 | |
| 1113 | /* store the request as the current one we're doing */ |
| 1114 | hs_ep->req = hs_req; |
| 1115 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1116 | if (using_desc_dma(hsotg)) { |
| 1117 | u32 offset = 0; |
| 1118 | u32 mps = hs_ep->ep.maxpacket; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1119 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1120 | /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */ |
| 1121 | if (!dir_in) { |
| 1122 | if (!index) |
| 1123 | length = mps; |
| 1124 | else if (length % mps) |
| 1125 | length += (mps - (length % mps)); |
| 1126 | } |
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 | /* |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1129 | * If more data to send, adjust DMA for EP0 out data stage. |
| 1130 | * ureq->dma stays unchanged, hence increment it by already |
| 1131 | * passed passed data count before starting new transaction. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1132 | */ |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1133 | if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT && |
| 1134 | continuing) |
| 1135 | offset = ureq->actual; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1136 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1137 | /* Fill DDMA chain entries */ |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 1138 | dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq, offset, |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1139 | length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1140 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1141 | /* write descriptor chain address to control register */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1142 | dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg); |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1143 | |
| 1144 | dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n", |
| 1145 | __func__, (u32)hs_ep->desc_list_dma, dma_reg); |
| 1146 | } else { |
| 1147 | /* write size / packets */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1148 | dwc2_writel(hsotg, epsize, epsize_reg); |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1149 | |
Razmik Karapetyan | 729e657 | 2016-11-16 15:33:55 -0800 | [diff] [blame] | 1150 | if (using_dma(hsotg) && !continuing && (length != 0)) { |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1151 | /* |
| 1152 | * write DMA address to control register, buffer |
| 1153 | * already synced by dwc2_hsotg_ep_queue(). |
| 1154 | */ |
| 1155 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1156 | dwc2_writel(hsotg, ureq->dma, dma_reg); |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1157 | |
| 1158 | dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n", |
| 1159 | __func__, &ureq->dma, dma_reg); |
| 1160 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1163 | if (hs_ep->isochronous && hs_ep->interval == 1) { |
| 1164 | hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg); |
| 1165 | dwc2_gadget_incr_frame_num(hs_ep); |
| 1166 | |
| 1167 | if (hs_ep->target_frame & 0x1) |
| 1168 | ctrl |= DXEPCTL_SETODDFR; |
| 1169 | else |
| 1170 | ctrl |= DXEPCTL_SETEVENFR; |
| 1171 | } |
| 1172 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1173 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1174 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1175 | dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state); |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1176 | |
| 1177 | /* For Setup request do not clear NAK */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1178 | if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP)) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1179 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1180 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1181 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1182 | dwc2_writel(hsotg, ctrl, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1183 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1184 | /* |
| 1185 | * set these, it seems that DMA support increments past the end |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1186 | * of the packet buffer so we need to calculate the length from |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1187 | * this information. |
| 1188 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1189 | hs_ep->size_loaded = length; |
| 1190 | hs_ep->last_load = ureq->actual; |
| 1191 | |
| 1192 | if (dir_in && !using_dma(hsotg)) { |
| 1193 | /* set these anyway, we may need them for non-periodic in */ |
| 1194 | hs_ep->fifo_load = 0; |
| 1195 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1196 | dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1199 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1200 | * Note, trying to clear the NAK here causes problems with transmit |
| 1201 | * on the S3C6400 ending up with the TXFIFO becoming full. |
| 1202 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1203 | |
| 1204 | /* check ep is enabled */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1205 | if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA)) |
Mian Yousaf Kaukab | 1a0ed86 | 2015-01-09 13:39:00 +0100 | [diff] [blame] | 1206 | dev_dbg(hsotg->dev, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1207 | "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1208 | index, dwc2_readl(hsotg, epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1209 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1210 | dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1211 | __func__, dwc2_readl(hsotg, epctrl_reg)); |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 1212 | |
| 1213 | /* enable ep interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1214 | 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] | 1215 | } |
| 1216 | |
| 1217 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1218 | * 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] | 1219 | * @hsotg: The device state. |
| 1220 | * @hs_ep: The endpoint the request is on. |
| 1221 | * @req: The request being processed. |
| 1222 | * |
| 1223 | * We've been asked to queue a request, so ensure that the memory buffer |
| 1224 | * is correctly setup for DMA. If we've been passed an extant DMA address |
| 1225 | * then ensure the buffer has been synced to memory. If our buffer has no |
| 1226 | * DMA memory, then we map the memory and mark our request to allow us to |
| 1227 | * cleanup on completion. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1228 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1229 | static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1230 | struct dwc2_hsotg_ep *hs_ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1231 | struct usb_request *req) |
| 1232 | { |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 1233 | int ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1234 | |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 1235 | ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in); |
| 1236 | if (ret) |
| 1237 | goto dma_error; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1238 | |
| 1239 | return 0; |
| 1240 | |
| 1241 | dma_error: |
| 1242 | dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n", |
| 1243 | __func__, req->buf, req->length); |
| 1244 | |
| 1245 | return -EIO; |
| 1246 | } |
| 1247 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1248 | static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg, |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 1249 | struct dwc2_hsotg_ep *hs_ep, |
| 1250 | struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1251 | { |
| 1252 | void *req_buf = hs_req->req.buf; |
| 1253 | |
| 1254 | /* If dma is not being used or buffer is aligned */ |
| 1255 | if (!using_dma(hsotg) || !((long)req_buf & 3)) |
| 1256 | return 0; |
| 1257 | |
| 1258 | WARN_ON(hs_req->saved_req_buf); |
| 1259 | |
| 1260 | dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1261 | hs_ep->ep.name, req_buf, hs_req->req.length); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1262 | |
| 1263 | hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC); |
| 1264 | if (!hs_req->req.buf) { |
| 1265 | hs_req->req.buf = req_buf; |
| 1266 | dev_err(hsotg->dev, |
| 1267 | "%s: unable to allocate memory for bounce buffer\n", |
| 1268 | __func__); |
| 1269 | return -ENOMEM; |
| 1270 | } |
| 1271 | |
| 1272 | /* Save actual buffer */ |
| 1273 | hs_req->saved_req_buf = req_buf; |
| 1274 | |
| 1275 | if (hs_ep->dir_in) |
| 1276 | memcpy(hs_req->req.buf, req_buf, hs_req->req.length); |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 1280 | static void |
| 1281 | dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg, |
| 1282 | struct dwc2_hsotg_ep *hs_ep, |
| 1283 | struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1284 | { |
| 1285 | /* If dma is not being used or buffer was aligned */ |
| 1286 | if (!using_dma(hsotg) || !hs_req->saved_req_buf) |
| 1287 | return; |
| 1288 | |
| 1289 | dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__, |
| 1290 | hs_ep->ep.name, hs_req->req.status, hs_req->req.actual); |
| 1291 | |
| 1292 | /* Copy data from bounce buffer on successful out transfer */ |
| 1293 | if (!hs_ep->dir_in && !hs_req->req.status) |
| 1294 | memcpy(hs_req->saved_req_buf, hs_req->req.buf, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1295 | hs_req->req.actual); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1296 | |
| 1297 | /* Free bounce buffer */ |
| 1298 | kfree(hs_req->req.buf); |
| 1299 | |
| 1300 | hs_req->req.buf = hs_req->saved_req_buf; |
| 1301 | hs_req->saved_req_buf = NULL; |
| 1302 | } |
| 1303 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 1304 | /** |
| 1305 | * dwc2_gadget_target_frame_elapsed - Checks target frame |
| 1306 | * @hs_ep: The driver endpoint to check |
| 1307 | * |
| 1308 | * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop |
| 1309 | * corresponding transfer. |
| 1310 | */ |
| 1311 | static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep) |
| 1312 | { |
| 1313 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 1314 | u32 target_frame = hs_ep->target_frame; |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 1315 | u32 current_frame = hsotg->frame_number; |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 1316 | bool frame_overrun = hs_ep->frame_overrun; |
| 1317 | |
| 1318 | if (!frame_overrun && current_frame >= target_frame) |
| 1319 | return true; |
| 1320 | |
| 1321 | if (frame_overrun && current_frame >= target_frame && |
| 1322 | ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2)) |
| 1323 | return true; |
| 1324 | |
| 1325 | return false; |
| 1326 | } |
| 1327 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1328 | /* |
| 1329 | * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers |
| 1330 | * @hsotg: The driver state |
| 1331 | * @hs_ep: the ep descriptor chain is for |
| 1332 | * |
| 1333 | * Called to update EP0 structure's pointers depend on stage of |
| 1334 | * control transfer. |
| 1335 | */ |
| 1336 | static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg, |
| 1337 | struct dwc2_hsotg_ep *hs_ep) |
| 1338 | { |
| 1339 | switch (hsotg->ep0_state) { |
| 1340 | case DWC2_EP0_SETUP: |
| 1341 | case DWC2_EP0_STATUS_OUT: |
| 1342 | hs_ep->desc_list = hsotg->setup_desc[0]; |
| 1343 | hs_ep->desc_list_dma = hsotg->setup_desc_dma[0]; |
| 1344 | break; |
| 1345 | case DWC2_EP0_DATA_IN: |
| 1346 | case DWC2_EP0_STATUS_IN: |
| 1347 | hs_ep->desc_list = hsotg->ctrl_in_desc; |
| 1348 | hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma; |
| 1349 | break; |
| 1350 | case DWC2_EP0_DATA_OUT: |
| 1351 | hs_ep->desc_list = hsotg->ctrl_out_desc; |
| 1352 | hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma; |
| 1353 | break; |
| 1354 | default: |
| 1355 | dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n", |
| 1356 | hsotg->ep0_state); |
| 1357 | return -EINVAL; |
| 1358 | } |
| 1359 | |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1363 | 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] | 1364 | gfp_t gfp_flags) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1365 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1366 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 1367 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1368 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1369 | bool first; |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1370 | int ret; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1371 | u32 maxsize = 0; |
| 1372 | u32 mask = 0; |
| 1373 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1374 | |
| 1375 | dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n", |
| 1376 | ep->name, req, req->length, req->buf, req->no_interrupt, |
| 1377 | req->zero, req->short_not_ok); |
| 1378 | |
Gregory Herrero | 7ababa9 | 2015-04-29 22:09:08 +0200 | [diff] [blame] | 1379 | /* Prevent new request submission when controller is suspended */ |
Grigor Tovmasyan | 88b02f2 | 2018-01-24 17:44:25 +0400 | [diff] [blame] | 1380 | if (hs->lx_state != DWC2_L0) { |
| 1381 | dev_dbg(hs->dev, "%s: submit request only in active state\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1382 | __func__); |
Gregory Herrero | 7ababa9 | 2015-04-29 22:09:08 +0200 | [diff] [blame] | 1383 | return -EAGAIN; |
| 1384 | } |
| 1385 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1386 | /* initialise status of the request */ |
| 1387 | INIT_LIST_HEAD(&hs_req->queue); |
| 1388 | req->actual = 0; |
| 1389 | req->status = -EINPROGRESS; |
| 1390 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1391 | /* In DDMA mode for ISOC's don't queue request if length greater |
| 1392 | * than descriptor limits. |
| 1393 | */ |
| 1394 | if (using_desc_dma(hs) && hs_ep->isochronous) { |
| 1395 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
| 1396 | if (hs_ep->dir_in && req->length > maxsize) { |
| 1397 | dev_err(hs->dev, "wrong length %d (maxsize=%d)\n", |
| 1398 | req->length, maxsize); |
| 1399 | return -EINVAL; |
| 1400 | } |
| 1401 | |
| 1402 | if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) { |
| 1403 | dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n", |
| 1404 | req->length, hs_ep->ep.maxpacket); |
| 1405 | return -EINVAL; |
| 1406 | } |
| 1407 | } |
| 1408 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1409 | 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] | 1410 | if (ret) |
| 1411 | return ret; |
| 1412 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1413 | /* if we're using DMA, sync the buffers as necessary */ |
| 1414 | if (using_dma(hs)) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1415 | ret = dwc2_hsotg_map_dma(hs, hs_ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1416 | if (ret) |
| 1417 | return ret; |
| 1418 | } |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1419 | /* If using descriptor DMA configure EP0 descriptor chain pointers */ |
| 1420 | if (using_desc_dma(hs) && !hs_ep->index) { |
| 1421 | ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep); |
| 1422 | if (ret) |
| 1423 | return ret; |
| 1424 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1425 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1426 | first = list_empty(&hs_ep->queue); |
| 1427 | list_add_tail(&hs_req->queue, &hs_ep->queue); |
| 1428 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1429 | /* |
| 1430 | * Handle DDMA isochronous transfers separately - just add new entry |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1431 | * to the descriptor chain. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1432 | * Transfer will be started once SW gets either one of NAK or |
| 1433 | * OutTknEpDis interrupts. |
| 1434 | */ |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1435 | if (using_desc_dma(hs) && hs_ep->isochronous) { |
| 1436 | if (hs_ep->target_frame != TARGET_FRAME_INITIAL) { |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 1437 | dma_addr_t dma_addr = hs_req->req.dma; |
| 1438 | |
| 1439 | if (hs_req->req.num_sgs) { |
| 1440 | WARN_ON(hs_req->req.num_sgs > 1); |
| 1441 | dma_addr = sg_dma_address(hs_req->req.sg); |
| 1442 | } |
| 1443 | dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr, |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1444 | hs_req->req.length); |
| 1445 | } |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1446 | return 0; |
| 1447 | } |
| 1448 | |
Minas Harutyunyan | b4c53b4 | 2019-03-12 11:45:12 +0400 | [diff] [blame] | 1449 | /* Change EP direction if status phase request is after data out */ |
| 1450 | if (!hs_ep->index && !req->length && !hs_ep->dir_in && |
| 1451 | hs->ep0_state == DWC2_EP0_DATA_OUT) |
| 1452 | hs_ep->dir_in = 1; |
| 1453 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1454 | if (first) { |
| 1455 | if (!hs_ep->isochronous) { |
| 1456 | dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); |
| 1457 | return 0; |
| 1458 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1459 | |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 1460 | /* Update current frame number value. */ |
| 1461 | hs->frame_number = dwc2_hsotg_read_frameno(hs); |
| 1462 | while (dwc2_gadget_target_frame_elapsed(hs_ep)) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1463 | dwc2_gadget_incr_frame_num(hs_ep); |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 1464 | /* Update current frame number value once more as it |
| 1465 | * changes here. |
| 1466 | */ |
| 1467 | hs->frame_number = dwc2_hsotg_read_frameno(hs); |
| 1468 | } |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1469 | |
| 1470 | if (hs_ep->target_frame != TARGET_FRAME_INITIAL) |
| 1471 | dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); |
| 1472 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1473 | return 0; |
| 1474 | } |
| 1475 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1476 | 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] | 1477 | gfp_t gfp_flags) |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1478 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1479 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1480 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1481 | unsigned long flags = 0; |
| 1482 | int ret = 0; |
| 1483 | |
| 1484 | spin_lock_irqsave(&hs->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1485 | ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1486 | spin_unlock_irqrestore(&hs->lock, flags); |
| 1487 | |
| 1488 | return ret; |
| 1489 | } |
| 1490 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1491 | static void dwc2_hsotg_ep_free_request(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1492 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1493 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1494 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1495 | |
| 1496 | kfree(hs_req); |
| 1497 | } |
| 1498 | |
| 1499 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1500 | * dwc2_hsotg_complete_oursetup - setup completion callback |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1501 | * @ep: The endpoint the request was on. |
| 1502 | * @req: The request completed. |
| 1503 | * |
| 1504 | * Called on completion of any requests the driver itself |
| 1505 | * submitted that need cleaning up. |
| 1506 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1507 | static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1508 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1509 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1510 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1511 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1512 | |
| 1513 | dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req); |
| 1514 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1515 | dwc2_hsotg_ep_free_request(ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /** |
| 1519 | * ep_from_windex - convert control wIndex value to endpoint |
| 1520 | * @hsotg: The driver state. |
| 1521 | * @windex: The control request wIndex field (in host order). |
| 1522 | * |
| 1523 | * Convert the given wIndex into a pointer to an driver endpoint |
| 1524 | * structure, or return NULL if it is not a valid endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1525 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1526 | static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1527 | u32 windex) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1528 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1529 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1530 | int dir = (windex & USB_DIR_IN) ? 1 : 0; |
| 1531 | int idx = windex & 0x7F; |
| 1532 | |
| 1533 | if (windex >= 0x100) |
| 1534 | return NULL; |
| 1535 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 1536 | if (idx > hsotg->num_of_eps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1537 | return NULL; |
| 1538 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1539 | ep = index_to_ep(hsotg, idx, dir); |
| 1540 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1541 | if (idx && ep->dir_in != dir) |
| 1542 | return NULL; |
| 1543 | |
| 1544 | return ep; |
| 1545 | } |
| 1546 | |
| 1547 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1548 | * dwc2_hsotg_set_test_mode - Enable usb Test Modes |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1549 | * @hsotg: The driver state. |
| 1550 | * @testmode: requested usb test mode |
| 1551 | * Enable usb Test Mode requested by the Host. |
| 1552 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1553 | int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode) |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1554 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1555 | int dctl = dwc2_readl(hsotg, DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1556 | |
| 1557 | dctl &= ~DCTL_TSTCTL_MASK; |
| 1558 | switch (testmode) { |
| 1559 | case TEST_J: |
| 1560 | case TEST_K: |
| 1561 | case TEST_SE0_NAK: |
| 1562 | case TEST_PACKET: |
| 1563 | case TEST_FORCE_EN: |
| 1564 | dctl |= testmode << DCTL_TSTCTL_SHIFT; |
| 1565 | break; |
| 1566 | default: |
| 1567 | return -EINVAL; |
| 1568 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1569 | dwc2_writel(hsotg, dctl, DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1570 | return 0; |
| 1571 | } |
| 1572 | |
| 1573 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1574 | * dwc2_hsotg_send_reply - send reply to control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1575 | * @hsotg: The device state |
| 1576 | * @ep: Endpoint 0 |
| 1577 | * @buff: Buffer for request |
| 1578 | * @length: Length of reply. |
| 1579 | * |
| 1580 | * Create a request and queue it on the given endpoint. This is useful as |
| 1581 | * an internal method of sending replies to certain control requests, etc. |
| 1582 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1583 | static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1584 | struct dwc2_hsotg_ep *ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1585 | void *buff, |
| 1586 | int length) |
| 1587 | { |
| 1588 | struct usb_request *req; |
| 1589 | int ret; |
| 1590 | |
| 1591 | dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length); |
| 1592 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1593 | req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1594 | hsotg->ep0_reply = req; |
| 1595 | if (!req) { |
| 1596 | dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__); |
| 1597 | return -ENOMEM; |
| 1598 | } |
| 1599 | |
| 1600 | req->buf = hsotg->ep0_buff; |
| 1601 | req->length = length; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1602 | /* |
| 1603 | * zero flag is for sending zlp in DATA IN stage. It has no impact on |
| 1604 | * STATUS stage. |
| 1605 | */ |
| 1606 | req->zero = 0; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1607 | req->complete = dwc2_hsotg_complete_oursetup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1608 | |
| 1609 | if (length) |
| 1610 | memcpy(req->buf, buff, length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1611 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1612 | ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1613 | if (ret) { |
| 1614 | dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__); |
| 1615 | return ret; |
| 1616 | } |
| 1617 | |
| 1618 | return 0; |
| 1619 | } |
| 1620 | |
| 1621 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1622 | * dwc2_hsotg_process_req_status - process request GET_STATUS |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1623 | * @hsotg: The device state |
| 1624 | * @ctrl: USB control request |
| 1625 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1626 | static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1627 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1628 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1629 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 1630 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1631 | __le16 reply; |
| 1632 | int ret; |
| 1633 | |
| 1634 | dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__); |
| 1635 | |
| 1636 | if (!ep0->dir_in) { |
| 1637 | dev_warn(hsotg->dev, "%s: direction out?\n", __func__); |
| 1638 | return -EINVAL; |
| 1639 | } |
| 1640 | |
| 1641 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1642 | case USB_RECIP_DEVICE: |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 1643 | /* |
| 1644 | * bit 0 => self powered |
| 1645 | * bit 1 => remote wakeup |
| 1646 | */ |
| 1647 | reply = cpu_to_le16(0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1648 | break; |
| 1649 | |
| 1650 | case USB_RECIP_INTERFACE: |
| 1651 | /* currently, the data result should be zero */ |
| 1652 | reply = cpu_to_le16(0); |
| 1653 | break; |
| 1654 | |
| 1655 | case USB_RECIP_ENDPOINT: |
| 1656 | ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex)); |
| 1657 | if (!ep) |
| 1658 | return -ENOENT; |
| 1659 | |
| 1660 | reply = cpu_to_le16(ep->halted ? 1 : 0); |
| 1661 | break; |
| 1662 | |
| 1663 | default: |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | if (le16_to_cpu(ctrl->wLength) != 2) |
| 1668 | return -EINVAL; |
| 1669 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1670 | ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1671 | if (ret) { |
| 1672 | dev_err(hsotg->dev, "%s: failed to send reply\n", __func__); |
| 1673 | return ret; |
| 1674 | } |
| 1675 | |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1679 | 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] | 1680 | |
| 1681 | /** |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1682 | * get_ep_head - return the first request on the endpoint |
| 1683 | * @hs_ep: The controller endpoint to get |
| 1684 | * |
| 1685 | * Get the first request on the endpoint. |
| 1686 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1687 | 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] | 1688 | { |
Masahiro Yamada | ffc4b40 | 2016-09-19 01:03:13 +0900 | [diff] [blame] | 1689 | return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req, |
| 1690 | queue); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | /** |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1694 | * dwc2_gadget_start_next_request - Starts next request from ep queue |
| 1695 | * @hs_ep: Endpoint structure |
| 1696 | * |
| 1697 | * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked |
| 1698 | * in its handler. Hence we need to unmask it here to be able to do |
| 1699 | * resynchronization. |
| 1700 | */ |
| 1701 | static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep) |
| 1702 | { |
| 1703 | u32 mask; |
| 1704 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 1705 | int dir_in = hs_ep->dir_in; |
| 1706 | struct dwc2_hsotg_req *hs_req; |
| 1707 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 1708 | |
| 1709 | if (!list_empty(&hs_ep->queue)) { |
| 1710 | hs_req = get_ep_head(hs_ep); |
| 1711 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false); |
| 1712 | return; |
| 1713 | } |
| 1714 | if (!hs_ep->isochronous) |
| 1715 | return; |
| 1716 | |
| 1717 | if (dir_in) { |
| 1718 | dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n", |
| 1719 | __func__); |
| 1720 | } else { |
| 1721 | dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n", |
| 1722 | __func__); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1723 | mask = dwc2_readl(hsotg, epmsk_reg); |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1724 | mask |= DOEPMSK_OUTTKNEPDISMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1725 | dwc2_writel(hsotg, mask, epmsk_reg); |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1730 | * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1731 | * @hsotg: The device state |
| 1732 | * @ctrl: USB control request |
| 1733 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1734 | static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1735 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1736 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1737 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 1738 | struct dwc2_hsotg_req *hs_req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1739 | bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1740 | struct dwc2_hsotg_ep *ep; |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1741 | int ret; |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1742 | bool halted; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1743 | u32 recip; |
| 1744 | u32 wValue; |
| 1745 | u32 wIndex; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1746 | |
| 1747 | dev_dbg(hsotg->dev, "%s: %s_FEATURE\n", |
| 1748 | __func__, set ? "SET" : "CLEAR"); |
| 1749 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1750 | wValue = le16_to_cpu(ctrl->wValue); |
| 1751 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 1752 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 1753 | |
| 1754 | switch (recip) { |
| 1755 | case USB_RECIP_DEVICE: |
| 1756 | switch (wValue) { |
Vardan Mikayelyan | fa389a6 | 2018-02-16 14:08:53 +0400 | [diff] [blame] | 1757 | case USB_DEVICE_REMOTE_WAKEUP: |
| 1758 | hsotg->remote_wakeup_allowed = 1; |
| 1759 | break; |
| 1760 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1761 | case USB_DEVICE_TEST_MODE: |
| 1762 | if ((wIndex & 0xff) != 0) |
| 1763 | return -EINVAL; |
| 1764 | if (!set) |
| 1765 | return -EINVAL; |
| 1766 | |
| 1767 | hsotg->test_mode = wIndex >> 8; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1768 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1769 | if (ret) { |
| 1770 | dev_err(hsotg->dev, |
| 1771 | "%s: failed to send reply\n", __func__); |
| 1772 | return ret; |
| 1773 | } |
| 1774 | break; |
| 1775 | default: |
| 1776 | return -ENOENT; |
| 1777 | } |
| 1778 | break; |
| 1779 | |
| 1780 | case USB_RECIP_ENDPOINT: |
| 1781 | ep = ep_from_windex(hsotg, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1782 | if (!ep) { |
| 1783 | dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n", |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1784 | __func__, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1785 | return -ENOENT; |
| 1786 | } |
| 1787 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1788 | switch (wValue) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1789 | case USB_ENDPOINT_HALT: |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1790 | halted = ep->halted; |
| 1791 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1792 | dwc2_hsotg_ep_sethalt(&ep->ep, set, true); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1793 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1794 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1795 | if (ret) { |
| 1796 | dev_err(hsotg->dev, |
| 1797 | "%s: failed to send reply\n", __func__); |
| 1798 | return ret; |
| 1799 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1800 | |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1801 | /* |
| 1802 | * we have to complete all requests for ep if it was |
| 1803 | * halted, and the halt was cleared by CLEAR_FEATURE |
| 1804 | */ |
| 1805 | |
| 1806 | if (!set && halted) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1807 | /* |
| 1808 | * If we have request in progress, |
| 1809 | * then complete it |
| 1810 | */ |
| 1811 | if (ep->req) { |
| 1812 | hs_req = ep->req; |
| 1813 | ep->req = NULL; |
| 1814 | list_del_init(&hs_req->queue); |
Gregory Herrero | c00dd4a | 2015-01-30 09:09:27 +0100 | [diff] [blame] | 1815 | if (hs_req->req.complete) { |
| 1816 | spin_unlock(&hsotg->lock); |
| 1817 | usb_gadget_giveback_request( |
| 1818 | &ep->ep, &hs_req->req); |
| 1819 | spin_lock(&hsotg->lock); |
| 1820 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | /* If we have pending request, then start it */ |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 1824 | if (!ep->req) |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1825 | dwc2_gadget_start_next_request(ep); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1826 | } |
| 1827 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1828 | break; |
| 1829 | |
| 1830 | default: |
| 1831 | return -ENOENT; |
| 1832 | } |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1833 | break; |
| 1834 | default: |
| 1835 | return -ENOENT; |
| 1836 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1837 | return 1; |
| 1838 | } |
| 1839 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1840 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg); |
Robert Baldyga | ab93e01 | 2013-09-19 11:50:17 +0200 | [diff] [blame] | 1841 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1842 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1843 | * dwc2_hsotg_stall_ep0 - stall ep0 |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1844 | * @hsotg: The device state |
| 1845 | * |
| 1846 | * Set stall for ep0 as response for setup request. |
| 1847 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1848 | static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg) |
Jingoo Han | e9ebe7c | 2014-06-03 22:14:56 +0900 | [diff] [blame] | 1849 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1850 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1851 | u32 reg; |
| 1852 | u32 ctrl; |
| 1853 | |
| 1854 | dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in); |
| 1855 | reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0; |
| 1856 | |
| 1857 | /* |
| 1858 | * DxEPCTL_Stall will be cleared by EP once it has |
| 1859 | * taken effect, so no need to clear later. |
| 1860 | */ |
| 1861 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1862 | ctrl = dwc2_readl(hsotg, reg); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1863 | ctrl |= DXEPCTL_STALL; |
| 1864 | ctrl |= DXEPCTL_CNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1865 | dwc2_writel(hsotg, ctrl, reg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1866 | |
| 1867 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1868 | "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1869 | ctrl, reg, dwc2_readl(hsotg, reg)); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1870 | |
| 1871 | /* |
| 1872 | * complete won't be called, so we enqueue |
| 1873 | * setup request here |
| 1874 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1875 | dwc2_hsotg_enqueue_setup(hsotg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1879 | * dwc2_hsotg_process_control - process a control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1880 | * @hsotg: The device state |
| 1881 | * @ctrl: The control request received |
| 1882 | * |
| 1883 | * The controller has received the SETUP phase of a control request, and |
| 1884 | * needs to work out what to do next (and whether to pass it on to the |
| 1885 | * gadget driver). |
| 1886 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1887 | static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1888 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1889 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1890 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1891 | int ret = 0; |
| 1892 | u32 dcfg; |
| 1893 | |
Mian Yousaf Kaukab | e525e74 | 2015-09-29 12:08:23 +0200 | [diff] [blame] | 1894 | dev_dbg(hsotg->dev, |
| 1895 | "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n", |
| 1896 | ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, |
| 1897 | ctrl->wIndex, ctrl->wLength); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1898 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1899 | if (ctrl->wLength == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1900 | ep0->dir_in = 1; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1901 | hsotg->ep0_state = DWC2_EP0_STATUS_IN; |
| 1902 | } else if (ctrl->bRequestType & USB_DIR_IN) { |
| 1903 | ep0->dir_in = 1; |
| 1904 | hsotg->ep0_state = DWC2_EP0_DATA_IN; |
| 1905 | } else { |
| 1906 | ep0->dir_in = 0; |
| 1907 | hsotg->ep0_state = DWC2_EP0_DATA_OUT; |
| 1908 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1909 | |
| 1910 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { |
| 1911 | switch (ctrl->bRequest) { |
| 1912 | case USB_REQ_SET_ADDRESS: |
Mian Yousaf Kaukab | 6d713c1 | 2015-01-09 13:39:10 +0100 | [diff] [blame] | 1913 | hsotg->connected = 1; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1914 | dcfg = dwc2_readl(hsotg, DCFG); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1915 | dcfg &= ~DCFG_DEVADDR_MASK; |
Paul Zimmerman | d5dbd3f | 2014-04-25 14:18:13 -0700 | [diff] [blame] | 1916 | dcfg |= (le16_to_cpu(ctrl->wValue) << |
| 1917 | DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1918 | dwc2_writel(hsotg, dcfg, DCFG); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1919 | |
| 1920 | dev_info(hsotg->dev, "new address %d\n", ctrl->wValue); |
| 1921 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1922 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1923 | return; |
| 1924 | |
| 1925 | case USB_REQ_GET_STATUS: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1926 | ret = dwc2_hsotg_process_req_status(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1927 | break; |
| 1928 | |
| 1929 | case USB_REQ_CLEAR_FEATURE: |
| 1930 | case USB_REQ_SET_FEATURE: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1931 | ret = dwc2_hsotg_process_req_feature(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1932 | break; |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | /* as a fallback, try delivering it to the driver to deal with */ |
| 1937 | |
| 1938 | if (ret == 0 && hsotg->driver) { |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1939 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1940 | ret = hsotg->driver->setup(&hsotg->gadget, ctrl); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1941 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1942 | if (ret < 0) |
| 1943 | dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret); |
| 1944 | } |
| 1945 | |
Minas Harutyunyan | b4c53b4 | 2019-03-12 11:45:12 +0400 | [diff] [blame] | 1946 | hsotg->delayed_status = false; |
| 1947 | if (ret == USB_GADGET_DELAYED_STATUS) |
| 1948 | hsotg->delayed_status = true; |
| 1949 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1950 | /* |
| 1951 | * the request is either unhandlable, or is not formatted correctly |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1952 | * so respond with a STALL for the status stage to indicate failure. |
| 1953 | */ |
| 1954 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1955 | if (ret < 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1956 | dwc2_hsotg_stall_ep0(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1957 | } |
| 1958 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1959 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1960 | * dwc2_hsotg_complete_setup - completion of a setup transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1961 | * @ep: The endpoint the request was on. |
| 1962 | * @req: The request completed. |
| 1963 | * |
| 1964 | * Called on completion of any requests the driver itself submitted for |
| 1965 | * EP0 setup packets |
| 1966 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1967 | static void dwc2_hsotg_complete_setup(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1968 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1969 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1970 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1971 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1972 | |
| 1973 | if (req->status < 0) { |
| 1974 | dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status); |
| 1975 | return; |
| 1976 | } |
| 1977 | |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1978 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1979 | if (req->actual == 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1980 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1981 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1982 | dwc2_hsotg_process_control(hsotg, req->buf); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1983 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1987 | * dwc2_hsotg_enqueue_setup - start a request for EP0 packets |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1988 | * @hsotg: The device state. |
| 1989 | * |
| 1990 | * Enqueue a request on EP0 if necessary to received any SETUP packets |
| 1991 | * received from the host. |
| 1992 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1993 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1994 | { |
| 1995 | struct usb_request *req = hsotg->ctrl_req; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1996 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1997 | int ret; |
| 1998 | |
| 1999 | dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__); |
| 2000 | |
| 2001 | req->zero = 0; |
| 2002 | req->length = 8; |
| 2003 | req->buf = hsotg->ctrl_buff; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2004 | req->complete = dwc2_hsotg_complete_setup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2005 | |
| 2006 | if (!list_empty(&hs_req->queue)) { |
| 2007 | dev_dbg(hsotg->dev, "%s already queued???\n", __func__); |
| 2008 | return; |
| 2009 | } |
| 2010 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2011 | hsotg->eps_out[0]->dir_in = 0; |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2012 | hsotg->eps_out[0]->send_zlp = 0; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2013 | hsotg->ep0_state = DWC2_EP0_SETUP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2014 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2015 | 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] | 2016 | if (ret < 0) { |
| 2017 | dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret); |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2018 | /* |
| 2019 | * Don't think there's much we can do other than watch the |
| 2020 | * driver fail. |
| 2021 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2022 | } |
| 2023 | } |
| 2024 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2025 | static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2026 | struct dwc2_hsotg_ep *hs_ep) |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2027 | { |
| 2028 | u32 ctrl; |
| 2029 | u8 index = hs_ep->index; |
| 2030 | u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 2031 | u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
| 2032 | |
Mian Yousaf Kaukab | ccb34a9 | 2015-01-30 09:09:34 +0100 | [diff] [blame] | 2033 | if (hs_ep->dir_in) |
| 2034 | dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 2035 | index); |
Mian Yousaf Kaukab | ccb34a9 | 2015-01-30 09:09:34 +0100 | [diff] [blame] | 2036 | else |
| 2037 | dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n", |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 2038 | index); |
| 2039 | if (using_desc_dma(hsotg)) { |
Minas Harutyunyan | 201ec56 | 2018-01-16 16:03:32 +0400 | [diff] [blame] | 2040 | if (!index) |
| 2041 | dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep); |
| 2042 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 2043 | /* Not specific buffer needed for ep0 ZLP */ |
| 2044 | dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &hs_ep->desc_list, |
| 2045 | hs_ep->desc_list_dma, 0, true); |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 2046 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2047 | dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
| 2048 | DXEPTSIZ_XFERSIZE(0), |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 2049 | epsiz_reg); |
| 2050 | } |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2051 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2052 | ctrl = dwc2_readl(hsotg, epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2053 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
| 2054 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
| 2055 | ctrl |= DXEPCTL_USBACTEP; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2056 | dwc2_writel(hsotg, ctrl, epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2059 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2060 | * dwc2_hsotg_complete_request - complete a request given to us |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2061 | * @hsotg: The device state. |
| 2062 | * @hs_ep: The endpoint the request was on. |
| 2063 | * @hs_req: The request to complete. |
| 2064 | * @result: The result code (0 => Ok, otherwise errno) |
| 2065 | * |
| 2066 | * The given request has finished, so call the necessary completion |
| 2067 | * if it has one and then look to see if we can start a new request |
| 2068 | * on the endpoint. |
| 2069 | * |
| 2070 | * Note, expects the ep to already be locked as appropriate. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2071 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2072 | static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2073 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2074 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2075 | int result) |
| 2076 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2077 | if (!hs_req) { |
| 2078 | dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__); |
| 2079 | return; |
| 2080 | } |
| 2081 | |
| 2082 | dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n", |
| 2083 | hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete); |
| 2084 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2085 | /* |
| 2086 | * only replace the status if we've not already set an error |
| 2087 | * from a previous transaction |
| 2088 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2089 | |
| 2090 | if (hs_req->req.status == -EINPROGRESS) |
| 2091 | hs_req->req.status = result; |
| 2092 | |
Yunzhi Li | 44583fe | 2015-09-29 12:25:01 +0200 | [diff] [blame] | 2093 | if (using_dma(hsotg)) |
| 2094 | dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req); |
| 2095 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2096 | dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 2097 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2098 | hs_ep->req = NULL; |
| 2099 | list_del_init(&hs_req->queue); |
| 2100 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2101 | /* |
| 2102 | * call the complete request with the locks off, just in case the |
| 2103 | * request tries to queue more work for this endpoint. |
| 2104 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2105 | |
| 2106 | if (hs_req->req.complete) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2107 | spin_unlock(&hsotg->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 2108 | usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2109 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2110 | } |
| 2111 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2112 | /* In DDMA don't need to proceed to starting of next ISOC request */ |
| 2113 | if (using_desc_dma(hsotg) && hs_ep->isochronous) |
| 2114 | return; |
| 2115 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2116 | /* |
| 2117 | * 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] | 2118 | * 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] | 2119 | * so be careful when doing this. |
| 2120 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2121 | |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 2122 | if (!hs_ep->req && result >= 0) |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 2123 | dwc2_gadget_start_next_request(hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2124 | } |
| 2125 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2126 | /* |
| 2127 | * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA |
| 2128 | * @hs_ep: The endpoint the request was on. |
| 2129 | * |
| 2130 | * Get first request from the ep queue, determine descriptor on which complete |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2131 | * happened. SW discovers which descriptor currently in use by HW, adjusts |
| 2132 | * dma_address and calculates index of completed descriptor based on the value |
| 2133 | * of DEPDMA register. Update actual length of request, giveback to gadget. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2134 | */ |
| 2135 | static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 2136 | { |
| 2137 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2138 | struct dwc2_hsotg_req *hs_req; |
| 2139 | struct usb_request *ureq; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2140 | u32 desc_sts; |
| 2141 | u32 mask; |
| 2142 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2143 | desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status; |
| 2144 | |
| 2145 | /* Process only descriptors with buffer status set to DMA done */ |
| 2146 | while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >> |
| 2147 | DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) { |
| 2148 | |
| 2149 | hs_req = get_ep_head(hs_ep); |
| 2150 | if (!hs_req) { |
| 2151 | dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__); |
| 2152 | return; |
| 2153 | } |
| 2154 | ureq = &hs_req->req; |
| 2155 | |
| 2156 | /* Check completion status */ |
| 2157 | if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT == |
| 2158 | DEV_DMA_STS_SUCC) { |
| 2159 | mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK : |
| 2160 | DEV_DMA_ISOC_RX_NBYTES_MASK; |
| 2161 | ureq->actual = ureq->length - ((desc_sts & mask) >> |
| 2162 | DEV_DMA_ISOC_NBYTES_SHIFT); |
| 2163 | |
| 2164 | /* Adjust actual len for ISOC Out if len is |
| 2165 | * not align of 4 |
| 2166 | */ |
| 2167 | if (!hs_ep->dir_in && ureq->length & 0x3) |
| 2168 | ureq->actual += 4 - (ureq->length & 0x3); |
Minas Harutyunyan | c8006f6 | 2019-03-12 13:27:46 +0400 | [diff] [blame^] | 2169 | |
| 2170 | /* Set actual frame number for completed transfers */ |
| 2171 | ureq->frame_number = |
| 2172 | (desc_sts & DEV_DMA_ISOC_FRNUM_MASK) >> |
| 2173 | DEV_DMA_ISOC_FRNUM_SHIFT; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
| 2177 | |
| 2178 | hs_ep->compl_desc++; |
Minas Harutyunyan | 54f37f5 | 2019-03-18 14:24:30 +0400 | [diff] [blame] | 2179 | if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_HS_ISOC - 1)) |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2180 | hs_ep->compl_desc = 0; |
| 2181 | desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2182 | } |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | /* |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2186 | * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC. |
| 2187 | * @hs_ep: The isochronous endpoint. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2188 | * |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2189 | * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA |
| 2190 | * interrupt. Reset target frame and next_desc to allow to start |
| 2191 | * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS |
| 2192 | * interrupt for OUT direction. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2193 | */ |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2194 | 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] | 2195 | { |
| 2196 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2197 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2198 | if (!hs_ep->dir_in) |
| 2199 | dwc2_flush_rx_fifo(hsotg); |
| 2200 | 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] | 2201 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2202 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
| 2203 | hs_ep->next_desc = 0; |
| 2204 | hs_ep->compl_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2205 | } |
| 2206 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2207 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2208 | * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2209 | * @hsotg: The device state. |
| 2210 | * @ep_idx: The endpoint index for the data |
| 2211 | * @size: The size of data in the fifo, in bytes |
| 2212 | * |
| 2213 | * The FIFO status shows there is data to read from the FIFO for a given |
| 2214 | * endpoint, so sort out whether we need to read the data into a request |
| 2215 | * that has been made for that endpoint. |
| 2216 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2217 | 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] | 2218 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2219 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx]; |
| 2220 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2221 | int to_read; |
| 2222 | int max_req; |
| 2223 | int read_ptr; |
| 2224 | |
| 2225 | if (!hs_req) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2226 | u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2227 | int ptr; |
| 2228 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 2229 | dev_dbg(hsotg->dev, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2230 | "%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] | 2231 | __func__, size, ep_idx, epctl); |
| 2232 | |
| 2233 | /* dump the data from the FIFO, we've nothing we can do */ |
| 2234 | for (ptr = 0; ptr < size; ptr += 4) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2235 | (void)dwc2_readl(hsotg, EPFIFO(ep_idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2236 | |
| 2237 | return; |
| 2238 | } |
| 2239 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2240 | to_read = size; |
| 2241 | read_ptr = hs_req->req.actual; |
| 2242 | max_req = hs_req->req.length - read_ptr; |
| 2243 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2244 | dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n", |
| 2245 | __func__, to_read, max_req, read_ptr, hs_req->req.length); |
| 2246 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2247 | if (to_read > max_req) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2248 | /* |
| 2249 | * more data appeared than we where willing |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2250 | * to deal with in this request. |
| 2251 | */ |
| 2252 | |
| 2253 | /* currently we don't deal this */ |
| 2254 | WARN_ON_ONCE(1); |
| 2255 | } |
| 2256 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2257 | hs_ep->total_data += to_read; |
| 2258 | hs_req->req.actual += to_read; |
| 2259 | to_read = DIV_ROUND_UP(to_read, 4); |
| 2260 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2261 | /* |
| 2262 | * note, we might over-write the buffer end by 3 bytes depending on |
| 2263 | * alignment of the data. |
| 2264 | */ |
Gevorg Sahakyan | 342ccce | 2018-07-26 18:00:41 +0400 | [diff] [blame] | 2265 | dwc2_readl_rep(hsotg, EPFIFO(ep_idx), |
| 2266 | hs_req->req.buf + read_ptr, to_read); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2270 | * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2271 | * @hsotg: The device instance |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2272 | * @dir_in: If IN zlp |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2273 | * |
| 2274 | * Generate a zero-length IN packet request for terminating a SETUP |
| 2275 | * transaction. |
| 2276 | * |
| 2277 | * 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] | 2278 | * 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] | 2279 | * the TxFIFO. |
| 2280 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2281 | 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] | 2282 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2283 | /* eps_out[0] is used in both directions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2284 | hsotg->eps_out[0]->dir_in = dir_in; |
| 2285 | 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] | 2286 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2287 | dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2288 | } |
| 2289 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2290 | static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2291 | u32 epctl_reg) |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2292 | { |
| 2293 | u32 ctrl; |
| 2294 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2295 | ctrl = dwc2_readl(hsotg, epctl_reg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2296 | if (ctrl & DXEPCTL_EOFRNUM) |
| 2297 | ctrl |= DXEPCTL_SETEVENFR; |
| 2298 | else |
| 2299 | ctrl |= DXEPCTL_SETODDFR; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2300 | dwc2_writel(hsotg, ctrl, epctl_reg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2301 | } |
| 2302 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2303 | /* |
| 2304 | * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc |
| 2305 | * @hs_ep - The endpoint on which transfer went |
| 2306 | * |
| 2307 | * Iterate over endpoints descriptor chain and get info on bytes remained |
| 2308 | * in DMA descriptors after transfer has completed. Used for non isoc EPs. |
| 2309 | */ |
| 2310 | static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 2311 | { |
| 2312 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2313 | unsigned int bytes_rem = 0; |
| 2314 | struct dwc2_dma_desc *desc = hs_ep->desc_list; |
| 2315 | int i; |
| 2316 | u32 status; |
| 2317 | |
| 2318 | if (!desc) |
| 2319 | return -EINVAL; |
| 2320 | |
| 2321 | for (i = 0; i < hs_ep->desc_count; ++i) { |
| 2322 | status = desc->status; |
| 2323 | bytes_rem += status & DEV_DMA_NBYTES_MASK; |
| 2324 | |
| 2325 | if (status & DEV_DMA_STS_MASK) |
| 2326 | dev_err(hsotg->dev, "descriptor %d closed with %x\n", |
| 2327 | i, status & DEV_DMA_STS_MASK); |
| 2328 | } |
| 2329 | |
| 2330 | return bytes_rem; |
| 2331 | } |
| 2332 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2333 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2334 | * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2335 | * @hsotg: The device instance |
| 2336 | * @epnum: The endpoint received from |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2337 | * |
| 2338 | * The RXFIFO has delivered an OutDone event, which means that the data |
| 2339 | * transfer for an OUT endpoint has been completed, either by a short |
| 2340 | * packet or by the finish of a transfer. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2341 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2342 | static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2343 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2344 | u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum)); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2345 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum]; |
| 2346 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2347 | struct usb_request *req = &hs_req->req; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2348 | unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2349 | int result = 0; |
| 2350 | |
| 2351 | if (!hs_req) { |
| 2352 | dev_dbg(hsotg->dev, "%s: no request active\n", __func__); |
| 2353 | return; |
| 2354 | } |
| 2355 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2356 | if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) { |
| 2357 | dev_dbg(hsotg->dev, "zlp packet received\n"); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2358 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
| 2359 | dwc2_hsotg_enqueue_setup(hsotg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2360 | return; |
| 2361 | } |
| 2362 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2363 | if (using_desc_dma(hsotg)) |
| 2364 | size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); |
| 2365 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2366 | if (using_dma(hsotg)) { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2367 | unsigned int size_done; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2368 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2369 | /* |
| 2370 | * Calculate the size of the transfer by checking how much |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2371 | * is left in the endpoint size register and then working it |
| 2372 | * out from the amount we loaded for the transfer. |
| 2373 | * |
| 2374 | * We need to do this as DMA pointers are always 32bit aligned |
| 2375 | * so may overshoot/undershoot the transfer. |
| 2376 | */ |
| 2377 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2378 | size_done = hs_ep->size_loaded - size_left; |
| 2379 | size_done += hs_ep->last_load; |
| 2380 | |
| 2381 | req->actual = size_done; |
| 2382 | } |
| 2383 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2384 | /* if there is more request to do, schedule new transfer */ |
| 2385 | if (req->actual < req->length && size_left == 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2386 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2387 | return; |
| 2388 | } |
| 2389 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2390 | if (req->actual < req->length && req->short_not_ok) { |
| 2391 | dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n", |
| 2392 | __func__, req->actual, req->length); |
| 2393 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2394 | /* |
| 2395 | * todo - what should we return here? there's no one else |
| 2396 | * even bothering to check the status. |
| 2397 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 2400 | /* DDMA IN status phase will start from StsPhseRcvd interrupt */ |
| 2401 | if (!using_desc_dma(hsotg) && epnum == 0 && |
| 2402 | hsotg->ep0_state == DWC2_EP0_DATA_OUT) { |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2403 | /* Move to STATUS IN */ |
Minas Harutyunyan | b4c53b4 | 2019-03-12 11:45:12 +0400 | [diff] [blame] | 2404 | if (!hsotg->delayed_status) |
| 2405 | dwc2_hsotg_ep0_zlp(hsotg, true); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2406 | } |
| 2407 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2408 | /* |
| 2409 | * Slave mode OUT transfers do not go through XferComplete so |
| 2410 | * adjust the ISOC parity here. |
| 2411 | */ |
| 2412 | if (!using_dma(hsotg)) { |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2413 | if (hs_ep->isochronous && hs_ep->interval == 1) |
| 2414 | dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2415 | else if (hs_ep->isochronous && hs_ep->interval > 1) |
| 2416 | dwc2_gadget_incr_frame_num(hs_ep); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2419 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2423 | * dwc2_hsotg_handle_rx - RX FIFO has data |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2424 | * @hsotg: The device instance |
| 2425 | * |
| 2426 | * The IRQ handler has detected that the RX FIFO has some data in it |
| 2427 | * that requires processing, so find out what is in there and do the |
| 2428 | * appropriate read. |
| 2429 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2430 | * 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] | 2431 | * chunks, so if you have x packets received on an endpoint you'll get x |
| 2432 | * FIFO events delivered, each with a packet's worth of data in it. |
| 2433 | * |
| 2434 | * When using DMA, we should not be processing events from the RXFIFO |
| 2435 | * as the actual data should be sent to the memory directly and we turn |
| 2436 | * on the completion interrupts to get notifications of transfer completion. |
| 2437 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2438 | static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2439 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2440 | u32 grxstsr = dwc2_readl(hsotg, GRXSTSP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2441 | u32 epnum, status, size; |
| 2442 | |
| 2443 | WARN_ON(using_dma(hsotg)); |
| 2444 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2445 | epnum = grxstsr & GRXSTS_EPNUM_MASK; |
| 2446 | status = grxstsr & GRXSTS_PKTSTS_MASK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2447 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2448 | size = grxstsr & GRXSTS_BYTECNT_MASK; |
| 2449 | size >>= GRXSTS_BYTECNT_SHIFT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2450 | |
Mian Yousaf Kaukab | d7c747c | 2015-01-30 09:09:30 +0100 | [diff] [blame] | 2451 | dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2452 | __func__, grxstsr, size, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2453 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2454 | switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) { |
| 2455 | case GRXSTS_PKTSTS_GLOBALOUTNAK: |
| 2456 | dev_dbg(hsotg->dev, "GLOBALOUTNAK\n"); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2457 | break; |
| 2458 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2459 | case GRXSTS_PKTSTS_OUTDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2460 | dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2461 | dwc2_hsotg_read_frameno(hsotg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2462 | |
| 2463 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2464 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2465 | break; |
| 2466 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2467 | case GRXSTS_PKTSTS_SETUPDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2468 | dev_dbg(hsotg->dev, |
| 2469 | "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2470 | dwc2_hsotg_read_frameno(hsotg), |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2471 | dwc2_readl(hsotg, DOEPCTL(0))); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2472 | /* |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2473 | * 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] | 2474 | * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't |
| 2475 | * generate GRXSTS_PKTSTS_OUTDONE for setup packet. |
| 2476 | */ |
| 2477 | if (hsotg->ep0_state == DWC2_EP0_SETUP) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2478 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2479 | break; |
| 2480 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2481 | case GRXSTS_PKTSTS_OUTRX: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2482 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2483 | break; |
| 2484 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2485 | case GRXSTS_PKTSTS_SETUPRX: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2486 | dev_dbg(hsotg->dev, |
| 2487 | "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2488 | dwc2_hsotg_read_frameno(hsotg), |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2489 | dwc2_readl(hsotg, DOEPCTL(0))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2490 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2491 | WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP); |
| 2492 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2493 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2494 | break; |
| 2495 | |
| 2496 | default: |
| 2497 | dev_warn(hsotg->dev, "%s: unknown status %08x\n", |
| 2498 | __func__, grxstsr); |
| 2499 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2500 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2501 | break; |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2506 | * dwc2_hsotg_ep0_mps - turn max packet size into register setting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2507 | * @mps: The maximum packet size in bytes. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2508 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2509 | static u32 dwc2_hsotg_ep0_mps(unsigned int mps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2510 | { |
| 2511 | switch (mps) { |
| 2512 | case 64: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2513 | return D0EPCTL_MPS_64; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2514 | case 32: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2515 | return D0EPCTL_MPS_32; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2516 | case 16: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2517 | return D0EPCTL_MPS_16; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2518 | case 8: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2519 | return D0EPCTL_MPS_8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | /* bad max packet size, warn and return invalid result */ |
| 2523 | WARN_ON(1); |
| 2524 | return (u32)-1; |
| 2525 | } |
| 2526 | |
| 2527 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2528 | * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2529 | * @hsotg: The driver state. |
| 2530 | * @ep: The index number of the endpoint |
| 2531 | * @mps: The maximum packet size in bytes |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2532 | * @mc: The multicount value |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 2533 | * @dir_in: True if direction is in. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2534 | * |
| 2535 | * Configure the maximum packet size for the given endpoint, updating |
| 2536 | * the hardware control registers to reflect this. |
| 2537 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2538 | static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg, |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2539 | unsigned int ep, unsigned int mps, |
| 2540 | unsigned int mc, unsigned int dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2541 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2542 | struct dwc2_hsotg_ep *hs_ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2543 | u32 reg; |
| 2544 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2545 | hs_ep = index_to_ep(hsotg, ep, dir_in); |
| 2546 | if (!hs_ep) |
| 2547 | return; |
| 2548 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2549 | if (ep == 0) { |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2550 | u32 mps_bytes = mps; |
| 2551 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2552 | /* EP0 is a special case */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2553 | mps = dwc2_hsotg_ep0_mps(mps_bytes); |
| 2554 | if (mps > 3) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2555 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2556 | hs_ep->ep.maxpacket = mps_bytes; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 2557 | hs_ep->mc = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2558 | } else { |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2559 | if (mps > 1024) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2560 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2561 | hs_ep->mc = mc; |
| 2562 | if (mc > 3) |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 2563 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2564 | hs_ep->ep.maxpacket = mps; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2565 | } |
| 2566 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2567 | if (dir_in) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2568 | reg = dwc2_readl(hsotg, DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2569 | reg &= ~DXEPCTL_MPS_MASK; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2570 | reg |= mps; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2571 | dwc2_writel(hsotg, reg, DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2572 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2573 | reg = dwc2_readl(hsotg, DOEPCTL(ep)); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2574 | reg &= ~DXEPCTL_MPS_MASK; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2575 | reg |= mps; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2576 | dwc2_writel(hsotg, reg, DOEPCTL(ep)); |
Anton Tikhomirov | 659ad60 | 2012-03-06 14:07:29 +0900 | [diff] [blame] | 2577 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2578 | |
| 2579 | return; |
| 2580 | |
| 2581 | bad_mps: |
| 2582 | dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps); |
| 2583 | } |
| 2584 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2585 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2586 | * dwc2_hsotg_txfifo_flush - flush Tx FIFO |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2587 | * @hsotg: The driver state |
| 2588 | * @idx: The index for the endpoint (0..15) |
| 2589 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2590 | 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] | 2591 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2592 | dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH, |
| 2593 | GRSTCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2594 | |
| 2595 | /* wait until the fifo is flushed */ |
Sevak Arakelyan | 79d6b8c | 2018-01-19 14:39:31 +0400 | [diff] [blame] | 2596 | if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100)) |
| 2597 | dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n", |
| 2598 | __func__); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2599 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2600 | |
| 2601 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2602 | * dwc2_hsotg_trytx - check to see if anything needs transmitting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2603 | * @hsotg: The driver state |
| 2604 | * @hs_ep: The driver endpoint to check. |
| 2605 | * |
| 2606 | * Check to see if there is a request that has data to send, and if so |
| 2607 | * make an attempt to write data into the FIFO. |
| 2608 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2609 | static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2610 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2611 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2612 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2613 | |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 2614 | if (!hs_ep->dir_in || !hs_req) { |
| 2615 | /** |
| 2616 | * if request is not enqueued, we disable interrupts |
| 2617 | * for endpoints, excepting ep0 |
| 2618 | */ |
| 2619 | if (hs_ep->index != 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2620 | dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2621 | hs_ep->dir_in, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2622 | return 0; |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 2623 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2624 | |
| 2625 | if (hs_req->req.actual < hs_req->req.length) { |
| 2626 | dev_dbg(hsotg->dev, "trying to write more for ep%d\n", |
| 2627 | hs_ep->index); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2628 | return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
| 2634 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2635 | * dwc2_hsotg_complete_in - complete IN transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2636 | * @hsotg: The device state. |
| 2637 | * @hs_ep: The endpoint that has just completed. |
| 2638 | * |
| 2639 | * An IN transfer has been completed, update the transfer's state and then |
| 2640 | * call the relevant completion routines. |
| 2641 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2642 | static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2643 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2644 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2645 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2646 | u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2647 | int size_left, size_done; |
| 2648 | |
| 2649 | if (!hs_req) { |
| 2650 | dev_dbg(hsotg->dev, "XferCompl but no req\n"); |
| 2651 | return; |
| 2652 | } |
| 2653 | |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2654 | /* Finish ZLP handling for IN EP0 transactions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2655 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) { |
| 2656 | dev_dbg(hsotg->dev, "zlp packet sent\n"); |
Razmik Karapetyan | c3b22fe | 2016-11-16 15:33:57 -0800 | [diff] [blame] | 2657 | |
| 2658 | /* |
| 2659 | * While send zlp for DWC2_EP0_STATUS_IN EP direction was |
| 2660 | * changed to IN. Change back to complete OUT transfer request |
| 2661 | */ |
| 2662 | hs_ep->dir_in = 0; |
| 2663 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2664 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2665 | if (hsotg->test_mode) { |
| 2666 | int ret; |
| 2667 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2668 | ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2669 | if (ret < 0) { |
| 2670 | dev_dbg(hsotg->dev, "Invalid Test #%d\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2671 | hsotg->test_mode); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2672 | dwc2_hsotg_stall_ep0(hsotg); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2673 | return; |
| 2674 | } |
| 2675 | } |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2676 | dwc2_hsotg_enqueue_setup(hsotg); |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2677 | return; |
| 2678 | } |
| 2679 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2680 | /* |
| 2681 | * Calculate the size of the transfer by checking how much is left |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2682 | * in the endpoint size register and then working it out from |
| 2683 | * the amount we loaded for the transfer. |
| 2684 | * |
| 2685 | * We do this even for DMA, as the transfer may have incremented |
| 2686 | * past the end of the buffer (DMA transfers are always 32bit |
| 2687 | * aligned). |
| 2688 | */ |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2689 | if (using_desc_dma(hsotg)) { |
| 2690 | size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); |
| 2691 | if (size_left < 0) |
| 2692 | dev_err(hsotg->dev, "error parsing DDMA results %d\n", |
| 2693 | size_left); |
| 2694 | } else { |
| 2695 | size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
| 2696 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2697 | |
| 2698 | size_done = hs_ep->size_loaded - size_left; |
| 2699 | size_done += hs_ep->last_load; |
| 2700 | |
| 2701 | if (hs_req->req.actual != size_done) |
| 2702 | dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n", |
| 2703 | __func__, hs_req->req.actual, size_done); |
| 2704 | |
| 2705 | hs_req->req.actual = size_done; |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2706 | dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n", |
| 2707 | hs_req->req.length, hs_req->req.actual, hs_req->req.zero); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2708 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2709 | if (!size_left && hs_req->req.actual < hs_req->req.length) { |
| 2710 | dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2711 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2712 | return; |
| 2713 | } |
| 2714 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 2715 | /* Zlp for all endpoints, for ep0 only in DATA IN stage */ |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2716 | if (hs_ep->send_zlp) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2717 | dwc2_hsotg_program_zlp(hsotg, hs_ep); |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2718 | hs_ep->send_zlp = 0; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 2719 | /* transfer will be completed on next complete interrupt */ |
| 2720 | return; |
| 2721 | } |
| 2722 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2723 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) { |
| 2724 | /* Move to STATUS OUT */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2725 | dwc2_hsotg_ep0_zlp(hsotg, false); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2726 | return; |
| 2727 | } |
| 2728 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2729 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /** |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2733 | * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep |
| 2734 | * @hsotg: The device state. |
| 2735 | * @idx: Index of ep. |
| 2736 | * @dir_in: Endpoint direction 1-in 0-out. |
| 2737 | * |
| 2738 | * Reads for endpoint with given index and direction, by masking |
| 2739 | * epint_reg with coresponding mask. |
| 2740 | */ |
| 2741 | static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg, |
| 2742 | unsigned int idx, int dir_in) |
| 2743 | { |
| 2744 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 2745 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 2746 | u32 ints; |
| 2747 | u32 mask; |
| 2748 | u32 diepempmsk; |
| 2749 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2750 | mask = dwc2_readl(hsotg, epmsk_reg); |
| 2751 | diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK); |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2752 | mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0; |
| 2753 | mask |= DXEPINT_SETUP_RCVD; |
| 2754 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2755 | ints = dwc2_readl(hsotg, epint_reg); |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2756 | ints &= mask; |
| 2757 | return ints; |
| 2758 | } |
| 2759 | |
| 2760 | /** |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2761 | * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD |
| 2762 | * @hs_ep: The endpoint on which interrupt is asserted. |
| 2763 | * |
| 2764 | * This interrupt indicates that the endpoint has been disabled per the |
| 2765 | * application's request. |
| 2766 | * |
| 2767 | * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK, |
| 2768 | * in case of ISOC completes current request. |
| 2769 | * |
| 2770 | * For ISOC-OUT endpoints completes expired requests. If there is remaining |
| 2771 | * request starts it. |
| 2772 | */ |
| 2773 | static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep) |
| 2774 | { |
| 2775 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2776 | struct dwc2_hsotg_req *hs_req; |
| 2777 | unsigned char idx = hs_ep->index; |
| 2778 | int dir_in = hs_ep->dir_in; |
| 2779 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2780 | int dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2781 | |
| 2782 | dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__); |
| 2783 | |
| 2784 | if (dir_in) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2785 | int epctl = dwc2_readl(hsotg, epctl_reg); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2786 | |
| 2787 | dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index); |
| 2788 | |
| 2789 | if (hs_ep->isochronous) { |
| 2790 | dwc2_hsotg_complete_in(hsotg, hs_ep); |
| 2791 | return; |
| 2792 | } |
| 2793 | |
| 2794 | if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2795 | int dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2796 | |
| 2797 | dctl |= DCTL_CGNPINNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2798 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2799 | } |
| 2800 | return; |
| 2801 | } |
| 2802 | |
| 2803 | if (dctl & DCTL_GOUTNAKSTS) { |
| 2804 | dctl |= DCTL_CGOUTNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2805 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | if (!hs_ep->isochronous) |
| 2809 | return; |
| 2810 | |
| 2811 | if (list_empty(&hs_ep->queue)) { |
| 2812 | dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n", |
| 2813 | __func__, hs_ep); |
| 2814 | return; |
| 2815 | } |
| 2816 | |
| 2817 | do { |
| 2818 | hs_req = get_ep_head(hs_ep); |
| 2819 | if (hs_req) |
| 2820 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, |
| 2821 | -ENODATA); |
| 2822 | dwc2_gadget_incr_frame_num(hs_ep); |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 2823 | /* Update current frame number value. */ |
| 2824 | hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2825 | } while (dwc2_gadget_target_frame_elapsed(hs_ep)); |
| 2826 | |
| 2827 | dwc2_gadget_start_next_request(hs_ep); |
| 2828 | } |
| 2829 | |
| 2830 | /** |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2831 | * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 2832 | * @ep: The endpoint on which interrupt is asserted. |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2833 | * |
| 2834 | * This is starting point for ISOC-OUT transfer, synchronization done with |
| 2835 | * first out token received from host while corresponding EP is disabled. |
| 2836 | * |
| 2837 | * Device does not know initial frame in which out token will come. For this |
| 2838 | * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon |
| 2839 | * getting this interrupt SW starts calculation for next transfer frame. |
| 2840 | */ |
| 2841 | static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep) |
| 2842 | { |
| 2843 | struct dwc2_hsotg *hsotg = ep->parent; |
| 2844 | int dir_in = ep->dir_in; |
| 2845 | u32 doepmsk; |
| 2846 | |
| 2847 | if (dir_in || !ep->isochronous) |
| 2848 | return; |
| 2849 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2850 | if (using_desc_dma(hsotg)) { |
| 2851 | if (ep->target_frame == TARGET_FRAME_INITIAL) { |
| 2852 | /* Start first ISO Out */ |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2853 | ep->target_frame = hsotg->frame_number; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2854 | dwc2_gadget_start_isoc_ddma(ep); |
| 2855 | } |
| 2856 | return; |
| 2857 | } |
| 2858 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2859 | if (ep->interval > 1 && |
| 2860 | ep->target_frame == TARGET_FRAME_INITIAL) { |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2861 | u32 ctrl; |
| 2862 | |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2863 | ep->target_frame = hsotg->frame_number; |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2864 | dwc2_gadget_incr_frame_num(ep); |
| 2865 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2866 | ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index)); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2867 | if (ep->target_frame & 0x1) |
| 2868 | ctrl |= DXEPCTL_SETODDFR; |
| 2869 | else |
| 2870 | ctrl |= DXEPCTL_SETEVENFR; |
| 2871 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2872 | dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index)); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | dwc2_gadget_start_next_request(ep); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2876 | doepmsk = dwc2_readl(hsotg, DOEPMSK); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2877 | doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2878 | dwc2_writel(hsotg, doepmsk, DOEPMSK); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | /** |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 2882 | * dwc2_gadget_handle_nak - handle NAK interrupt |
| 2883 | * @hs_ep: The endpoint on which interrupt is asserted. |
| 2884 | * |
| 2885 | * This is starting point for ISOC-IN transfer, synchronization done with |
| 2886 | * first IN token received from host while corresponding EP is disabled. |
| 2887 | * |
| 2888 | * Device does not know when first one token will arrive from host. On first |
| 2889 | * token arrival HW generates 2 interrupts: 'in token received while FIFO empty' |
| 2890 | * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was |
| 2891 | * sent in response to that as there was no data in FIFO. SW is basing on this |
| 2892 | * interrupt to obtain frame in which token has come and then based on the |
| 2893 | * interval calculates next frame for transfer. |
| 2894 | */ |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2895 | static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep) |
| 2896 | { |
| 2897 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2898 | int dir_in = hs_ep->dir_in; |
| 2899 | |
| 2900 | if (!dir_in || !hs_ep->isochronous) |
| 2901 | return; |
| 2902 | |
| 2903 | if (hs_ep->target_frame == TARGET_FRAME_INITIAL) { |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2904 | |
| 2905 | if (using_desc_dma(hsotg)) { |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2906 | hs_ep->target_frame = hsotg->frame_number; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2907 | dwc2_gadget_incr_frame_num(hs_ep); |
Grigor Tovmasyan | 48dac4e | 2018-08-29 21:00:33 +0400 | [diff] [blame] | 2908 | |
| 2909 | /* In service interval mode target_frame must |
| 2910 | * be set to last (u)frame of the service interval. |
| 2911 | */ |
| 2912 | if (hsotg->params.service_interval) { |
| 2913 | /* Set target_frame to the first (u)frame of |
| 2914 | * the service interval |
| 2915 | */ |
| 2916 | hs_ep->target_frame &= ~hs_ep->interval + 1; |
| 2917 | |
| 2918 | /* Set target_frame to the last (u)frame of |
| 2919 | * the service interval |
| 2920 | */ |
| 2921 | dwc2_gadget_incr_frame_num(hs_ep); |
| 2922 | dwc2_gadget_dec_frame_num_by_one(hs_ep); |
| 2923 | } |
| 2924 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2925 | dwc2_gadget_start_isoc_ddma(hs_ep); |
| 2926 | return; |
| 2927 | } |
| 2928 | |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2929 | hs_ep->target_frame = hsotg->frame_number; |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2930 | if (hs_ep->interval > 1) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2931 | u32 ctrl = dwc2_readl(hsotg, |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2932 | DIEPCTL(hs_ep->index)); |
| 2933 | if (hs_ep->target_frame & 0x1) |
| 2934 | ctrl |= DXEPCTL_SETODDFR; |
| 2935 | else |
| 2936 | ctrl |= DXEPCTL_SETEVENFR; |
| 2937 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2938 | dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index)); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | dwc2_hsotg_complete_request(hsotg, hs_ep, |
| 2942 | get_ep_head(hs_ep), 0); |
| 2943 | } |
| 2944 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2945 | if (!using_desc_dma(hsotg)) |
| 2946 | dwc2_gadget_incr_frame_num(hs_ep); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2950 | * dwc2_hsotg_epint - handle an in/out endpoint interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2951 | * @hsotg: The driver state |
| 2952 | * @idx: The index for the endpoint (0..15) |
| 2953 | * @dir_in: Set if this is an IN endpoint |
| 2954 | * |
| 2955 | * Process and clear any interrupt pending for an individual endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2956 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2957 | static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2958 | int dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2959 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2960 | 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] | 2961 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 2962 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
| 2963 | u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2964 | u32 ints; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2965 | u32 ctrl; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2966 | |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2967 | ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2968 | ctrl = dwc2_readl(hsotg, epctl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2969 | |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2970 | /* Clear endpoint interrupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2971 | dwc2_writel(hsotg, ints, epint_reg); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2972 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2973 | if (!hs_ep) { |
| 2974 | dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2975 | __func__, idx, dir_in ? "in" : "out"); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2976 | return; |
| 2977 | } |
| 2978 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2979 | dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n", |
| 2980 | __func__, idx, dir_in ? "in" : "out", ints); |
| 2981 | |
Mian Yousaf Kaukab | b787d75 | 2015-01-09 13:38:43 +0100 | [diff] [blame] | 2982 | /* Don't process XferCompl interrupt if it is a setup packet */ |
| 2983 | if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD))) |
| 2984 | ints &= ~DXEPINT_XFERCOMPL; |
| 2985 | |
Vahram Aharonyan | f0afdb4 | 2016-11-14 19:16:48 -0800 | [diff] [blame] | 2986 | /* |
| 2987 | * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP |
| 2988 | * stage and xfercomplete was generated without SETUP phase done |
| 2989 | * interrupt. SW should parse received setup packet only after host's |
| 2990 | * exit from setup phase of control transfer. |
| 2991 | */ |
| 2992 | if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in && |
| 2993 | hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP)) |
| 2994 | ints &= ~DXEPINT_XFERCOMPL; |
| 2995 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2996 | if (ints & DXEPINT_XFERCOMPL) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2997 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2998 | "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2999 | __func__, dwc2_readl(hsotg, epctl_reg), |
| 3000 | dwc2_readl(hsotg, epsiz_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3001 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3002 | /* In DDMA handle isochronous requests separately */ |
| 3003 | if (using_desc_dma(hsotg) && hs_ep->isochronous) { |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3004 | /* XferCompl set along with BNA */ |
| 3005 | if (!(ints & DXEPINT_BNAINTR)) |
| 3006 | dwc2_gadget_complete_isoc_request_ddma(hs_ep); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3007 | } else if (dir_in) { |
| 3008 | /* |
| 3009 | * We get OutDone from the FIFO, so we only |
| 3010 | * need to look at completing IN requests here |
| 3011 | * if operating slave mode |
| 3012 | */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3013 | if (hs_ep->isochronous && hs_ep->interval > 1) |
| 3014 | dwc2_gadget_incr_frame_num(hs_ep); |
| 3015 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3016 | dwc2_hsotg_complete_in(hsotg, hs_ep); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3017 | if (ints & DXEPINT_NAKINTRPT) |
| 3018 | ints &= ~DXEPINT_NAKINTRPT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3019 | |
Ben Dooks | c9a64ea | 2010-07-19 09:40:46 +0100 | [diff] [blame] | 3020 | if (idx == 0 && !hs_ep->req) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3021 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3022 | } else if (using_dma(hsotg)) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3023 | /* |
| 3024 | * We're using DMA, we need to fire an OutDone here |
| 3025 | * as we ignore the RXFIFO. |
| 3026 | */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3027 | if (hs_ep->isochronous && hs_ep->interval > 1) |
| 3028 | dwc2_gadget_incr_frame_num(hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3029 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3030 | dwc2_hsotg_handle_outdone(hsotg, idx); |
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 | |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 3034 | if (ints & DXEPINT_EPDISBLD) |
| 3035 | dwc2_gadget_handle_ep_disabled(hs_ep); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 3036 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 3037 | if (ints & DXEPINT_OUTTKNEPDIS) |
| 3038 | dwc2_gadget_handle_out_token_ep_disabled(hs_ep); |
| 3039 | |
| 3040 | if (ints & DXEPINT_NAKINTRPT) |
| 3041 | dwc2_gadget_handle_nak(hs_ep); |
| 3042 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3043 | if (ints & DXEPINT_AHBERR) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3044 | dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3045 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3046 | if (ints & DXEPINT_SETUP) { /* Setup or Timeout */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3047 | dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__); |
| 3048 | |
| 3049 | if (using_dma(hsotg) && idx == 0) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3050 | /* |
| 3051 | * this is the notification we've received a |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3052 | * setup packet. In non-DMA mode we'd get this |
| 3053 | * from the RXFIFO, instead we need to process |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3054 | * the setup here. |
| 3055 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3056 | |
| 3057 | if (dir_in) |
| 3058 | WARN_ON_ONCE(1); |
| 3059 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3060 | dwc2_hsotg_handle_outdone(hsotg, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3061 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3062 | } |
| 3063 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 3064 | if (ints & DXEPINT_STSPHSERCVD) { |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3065 | dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__); |
| 3066 | |
Minas Harutyunyan | 9e95a66 | 2018-01-16 16:03:58 +0400 | [diff] [blame] | 3067 | /* Safety check EP0 state when STSPHSERCVD asserted */ |
| 3068 | if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) { |
| 3069 | /* Move to STATUS IN for DDMA */ |
Minas Harutyunyan | b4c53b4 | 2019-03-12 11:45:12 +0400 | [diff] [blame] | 3070 | if (using_desc_dma(hsotg)) { |
| 3071 | if (!hsotg->delayed_status) |
| 3072 | dwc2_hsotg_ep0_zlp(hsotg, true); |
| 3073 | else |
| 3074 | /* In case of 3 stage Control Write with delayed |
| 3075 | * status, when Status IN transfer started |
| 3076 | * before STSPHSERCVD asserted, NAKSTS bit not |
| 3077 | * cleared by CNAK in dwc2_hsotg_start_req() |
| 3078 | * function. Clear now NAKSTS to allow complete |
| 3079 | * transfer. |
| 3080 | */ |
| 3081 | dwc2_set_bit(hsotg, DIEPCTL(0), |
| 3082 | DXEPCTL_CNAK); |
| 3083 | } |
Minas Harutyunyan | 9e95a66 | 2018-01-16 16:03:58 +0400 | [diff] [blame] | 3084 | } |
| 3085 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 3086 | } |
| 3087 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3088 | if (ints & DXEPINT_BACK2BACKSETUP) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3089 | dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3090 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3091 | if (ints & DXEPINT_BNAINTR) { |
| 3092 | dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3093 | if (hs_ep->isochronous) |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3094 | dwc2_gadget_handle_isoc_bna(hs_ep); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3095 | } |
| 3096 | |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3097 | if (dir_in && !hs_ep->isochronous) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3098 | /* not sure if this is important, but we'll clear it anyway */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3099 | if (ints & DXEPINT_INTKNTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3100 | dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n", |
| 3101 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | /* this probably means something bad is happening */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3105 | if (ints & DXEPINT_INTKNEPMIS) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3106 | dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n", |
| 3107 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3108 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3109 | |
| 3110 | /* FIFO has space or is empty (see GAHBCFG) */ |
| 3111 | if (hsotg->dedicated_fifos && |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3112 | ints & DXEPINT_TXFEMP) { |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3113 | dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n", |
| 3114 | __func__, idx); |
Anton Tikhomirov | 70fa030 | 2012-03-06 14:08:29 +0900 | [diff] [blame] | 3115 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3116 | dwc2_hsotg_trytx(hsotg, hs_ep); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3117 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3118 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3122 | * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3123 | * @hsotg: The device state. |
| 3124 | * |
| 3125 | * Handle updating the device settings after the enumeration phase has |
| 3126 | * been completed. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3127 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3128 | static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3129 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3130 | u32 dsts = dwc2_readl(hsotg, DSTS); |
Jingoo Han | 9b2667f | 2014-08-20 12:04:09 +0900 | [diff] [blame] | 3131 | int ep0_mps = 0, ep_mps = 8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3132 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3133 | /* |
| 3134 | * This should signal the finish of the enumeration phase |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3135 | * of the USB handshaking, so we should now know what rate |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3136 | * we connected at. |
| 3137 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3138 | |
| 3139 | dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts); |
| 3140 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3141 | /* |
| 3142 | * 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] | 3143 | * 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] | 3144 | * not advertise a 64byte MPS on EP0. |
| 3145 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3146 | |
| 3147 | /* catch both EnumSpd_FS and EnumSpd_FS48 */ |
Marek Vasut | 6d76c92 | 2015-12-18 03:26:17 +0100 | [diff] [blame] | 3148 | switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3149 | case DSTS_ENUMSPD_FS: |
| 3150 | case DSTS_ENUMSPD_FS48: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3151 | hsotg->gadget.speed = USB_SPEED_FULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3152 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 3153 | ep_mps = 1023; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3154 | break; |
| 3155 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3156 | case DSTS_ENUMSPD_HS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3157 | hsotg->gadget.speed = USB_SPEED_HIGH; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3158 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 3159 | ep_mps = 1024; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3160 | break; |
| 3161 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3162 | case DSTS_ENUMSPD_LS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3163 | hsotg->gadget.speed = USB_SPEED_LOW; |
Vardan Mikayelyan | 552d940 | 2016-11-14 19:17:00 -0800 | [diff] [blame] | 3164 | ep0_mps = 8; |
| 3165 | ep_mps = 8; |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3166 | /* |
| 3167 | * note, we don't actually support LS in this driver at the |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3168 | * moment, and the documentation seems to imply that it isn't |
| 3169 | * supported by the PHYs on some of the devices. |
| 3170 | */ |
| 3171 | break; |
| 3172 | } |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 3173 | dev_info(hsotg->dev, "new device is %s\n", |
| 3174 | usb_speed_string(hsotg->gadget.speed)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3175 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3176 | /* |
| 3177 | * we should now know the maximum packet size for an |
| 3178 | * endpoint, so set the endpoints to a default value. |
| 3179 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3180 | |
| 3181 | if (ep0_mps) { |
| 3182 | int i; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3183 | /* Initialize ep0 for both in and out directions */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3184 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1); |
| 3185 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3186 | for (i = 1; i < hsotg->num_of_eps; i++) { |
| 3187 | if (hsotg->eps_in[i]) |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3188 | dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, |
| 3189 | 0, 1); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3190 | if (hsotg->eps_out[i]) |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3191 | dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, |
| 3192 | 0, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3193 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | /* ensure after enumeration our EP0 is active */ |
| 3197 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3198 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3199 | |
| 3200 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3201 | dwc2_readl(hsotg, DIEPCTL0), |
| 3202 | dwc2_readl(hsotg, DOEPCTL0)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | /** |
| 3206 | * kill_all_requests - remove all requests from the endpoint's queue |
| 3207 | * @hsotg: The device state. |
| 3208 | * @ep: The endpoint the requests may be on. |
| 3209 | * @result: The result code to use. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3210 | * |
| 3211 | * Go through the requests on the given endpoint and mark them |
| 3212 | * completed with the given result code. |
| 3213 | */ |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3214 | static void kill_all_requests(struct dwc2_hsotg *hsotg, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3215 | struct dwc2_hsotg_ep *ep, |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3216 | int result) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3217 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3218 | struct dwc2_hsotg_req *req, *treq; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3219 | unsigned int size; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3220 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3221 | ep->req = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3222 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3223 | list_for_each_entry_safe(req, treq, &ep->queue, queue) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3224 | dwc2_hsotg_complete_request(hsotg, ep, req, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3225 | result); |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3226 | |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3227 | if (!hsotg->dedicated_fifos) |
| 3228 | return; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3229 | size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3230 | if (size < ep->fifo_size) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3231 | dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3232 | } |
| 3233 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3234 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3235 | * dwc2_hsotg_disconnect - disconnect service |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3236 | * @hsotg: The device state. |
| 3237 | * |
Lukasz Majewski | 5e89134 | 2012-05-04 14:17:07 +0200 | [diff] [blame] | 3238 | * The device has been disconnected. Remove all current |
| 3239 | * transactions and signal the gadget driver that this |
| 3240 | * has happened. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3241 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3242 | void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3243 | { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3244 | unsigned int ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3245 | |
Marek Szyprowski | 4ace06e | 2014-11-21 15:14:47 +0100 | [diff] [blame] | 3246 | if (!hsotg->connected) |
| 3247 | return; |
| 3248 | |
| 3249 | hsotg->connected = 0; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 3250 | hsotg->test_mode = 0; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3251 | |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3252 | /* all endpoints should be shutdown */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3253 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 3254 | if (hsotg->eps_in[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 3255 | kill_all_requests(hsotg, hsotg->eps_in[ep], |
| 3256 | -ESHUTDOWN); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3257 | if (hsotg->eps_out[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 3258 | kill_all_requests(hsotg, hsotg->eps_out[ep], |
| 3259 | -ESHUTDOWN); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3260 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3261 | |
| 3262 | call_gadget(hsotg, disconnect); |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 3263 | hsotg->lx_state = DWC2_L3; |
John Stultz | ce2b21a | 2017-10-23 14:32:50 -0700 | [diff] [blame] | 3264 | |
| 3265 | usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3269 | * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3270 | * @hsotg: The device state: |
| 3271 | * @periodic: True if this is a periodic FIFO interrupt |
| 3272 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3273 | static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3274 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3275 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3276 | int epno, ret; |
| 3277 | |
| 3278 | /* look through for any more data to transmit */ |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3279 | for (epno = 0; epno < hsotg->num_of_eps; epno++) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3280 | ep = index_to_ep(hsotg, epno, 1); |
| 3281 | |
| 3282 | if (!ep) |
| 3283 | continue; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3284 | |
| 3285 | if (!ep->dir_in) |
| 3286 | continue; |
| 3287 | |
| 3288 | if ((periodic && !ep->periodic) || |
| 3289 | (!periodic && ep->periodic)) |
| 3290 | continue; |
| 3291 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3292 | ret = dwc2_hsotg_trytx(hsotg, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3293 | if (ret < 0) |
| 3294 | break; |
| 3295 | } |
| 3296 | } |
| 3297 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3298 | /* IRQ flags which will trigger a retry around the IRQ loop */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3299 | #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \ |
| 3300 | GINTSTS_PTXFEMP | \ |
| 3301 | GINTSTS_RXFLVL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3302 | |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 3303 | static int dwc2_hsotg_ep_disable(struct usb_ep *ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3304 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3305 | * dwc2_hsotg_core_init - issue softreset to the core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3306 | * @hsotg: The device state |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 3307 | * @is_usb_reset: Usb resetting flag |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3308 | * |
| 3309 | * Issue a soft reset to the core, and await the core finishing it. |
| 3310 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3311 | void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3312 | bool is_usb_reset) |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3313 | { |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3314 | u32 intmsk; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3315 | u32 val; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3316 | u32 usbcfg; |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3317 | u32 dcfg = 0; |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3318 | int ep; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3319 | |
Mian Yousaf Kaukab | 5390d43 | 2015-09-29 12:08:25 +0200 | [diff] [blame] | 3320 | /* Kill any ep0 requests as controller will be reinitialized */ |
| 3321 | kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET); |
| 3322 | |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3323 | if (!is_usb_reset) { |
John Stultz | 6e6360b | 2017-01-23 14:59:14 -0800 | [diff] [blame] | 3324 | if (dwc2_core_reset(hsotg, true)) |
Gregory Herrero | 86de489 | 2015-09-29 12:08:21 +0200 | [diff] [blame] | 3325 | return; |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3326 | } else { |
| 3327 | /* all endpoints should be shutdown */ |
| 3328 | for (ep = 1; ep < hsotg->num_of_eps; ep++) { |
| 3329 | if (hsotg->eps_in[ep]) |
| 3330 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
| 3331 | if (hsotg->eps_out[ep]) |
| 3332 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
| 3333 | } |
| 3334 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3335 | |
| 3336 | /* |
| 3337 | * we must now enable ep0 ready for host detection and then |
| 3338 | * set configuration. |
| 3339 | */ |
| 3340 | |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3341 | /* keep other bits untouched (so e.g. forced modes are not lost) */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3342 | usbcfg = dwc2_readl(hsotg, GUSBCFG); |
Jules Maselbas | 1e86854 | 2019-04-05 15:35:33 +0200 | [diff] [blame] | 3343 | usbcfg &= ~GUSBCFG_TOUTCAL_MASK; |
Jules Maselbas | 707d80f | 2019-04-05 15:35:31 +0200 | [diff] [blame] | 3344 | usbcfg |= GUSBCFG_TOUTCAL(7); |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3345 | |
Jules Maselbas | 1e86854 | 2019-04-05 15:35:33 +0200 | [diff] [blame] | 3346 | /* remove the HNP/SRP and set the PHY */ |
| 3347 | usbcfg &= ~(GUSBCFG_SRPCAP | GUSBCFG_HNPCAP); |
| 3348 | dwc2_writel(hsotg, usbcfg, GUSBCFG); |
Jules Maselbas | 707d80f | 2019-04-05 15:35:31 +0200 | [diff] [blame] | 3349 | |
Jules Maselbas | 1e86854 | 2019-04-05 15:35:33 +0200 | [diff] [blame] | 3350 | dwc2_phy_init(hsotg, true); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3351 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3352 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3353 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3354 | if (!is_usb_reset) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3355 | dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3356 | |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3357 | dcfg |= DCFG_EPMISCNT(1); |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3358 | |
| 3359 | switch (hsotg->params.speed) { |
| 3360 | case DWC2_SPEED_PARAM_LOW: |
| 3361 | dcfg |= DCFG_DEVSPD_LS; |
| 3362 | break; |
| 3363 | case DWC2_SPEED_PARAM_FULL: |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3364 | if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) |
| 3365 | dcfg |= DCFG_DEVSPD_FS48; |
| 3366 | else |
| 3367 | dcfg |= DCFG_DEVSPD_FS; |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3368 | break; |
| 3369 | default: |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3370 | dcfg |= DCFG_DEVSPD_HS; |
| 3371 | } |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3372 | |
Grigor Tovmasyan | b43ebc9 | 2018-05-05 12:17:58 +0400 | [diff] [blame] | 3373 | if (hsotg->params.ipg_isoc_en) |
| 3374 | dcfg |= DCFG_IPG_ISOC_SUPPORDED; |
| 3375 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3376 | dwc2_writel(hsotg, dcfg, DCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3377 | |
| 3378 | /* Clear any pending OTG interrupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3379 | dwc2_writel(hsotg, 0xffffffff, GOTGINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3380 | |
| 3381 | /* Clear any pending interrupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3382 | dwc2_writel(hsotg, 0xffffffff, GINTSTS); |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3383 | intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3384 | GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF | |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3385 | GINTSTS_USBRST | GINTSTS_RESETDET | |
| 3386 | GINTSTS_ENUMDONE | GINTSTS_OTGINT | |
Sevak Arakelyan | 376f040 | 2018-01-24 17:43:06 +0400 | [diff] [blame] | 3387 | GINTSTS_USBSUSP | GINTSTS_WKUPINT | |
| 3388 | GINTSTS_LPMTRANRCVD; |
Vahram Aharonyan | f473670 | 2016-11-14 19:16:38 -0800 | [diff] [blame] | 3389 | |
| 3390 | if (!using_desc_dma(hsotg)) |
| 3391 | intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT; |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3392 | |
John Youn | 95832c0 | 2017-01-23 14:57:26 -0800 | [diff] [blame] | 3393 | if (!hsotg->params.external_id_pin_ctl) |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3394 | intmsk |= GINTSTS_CONIDSTSCHNG; |
| 3395 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3396 | dwc2_writel(hsotg, intmsk, GINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3397 | |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3398 | if (using_dma(hsotg)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3399 | dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN | |
Razmik Karapetyan | d1ac8c8 | 2018-01-19 14:39:57 +0400 | [diff] [blame] | 3400 | hsotg->params.ahbcfg, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3401 | GAHBCFG); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3402 | |
| 3403 | /* Set DDMA mode support in the core if needed */ |
| 3404 | if (using_desc_dma(hsotg)) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3405 | dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3406 | |
| 3407 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3408 | dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ? |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3409 | (GAHBCFG_NP_TXF_EMP_LVL | |
| 3410 | GAHBCFG_P_TXF_EMP_LVL) : 0) | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3411 | GAHBCFG_GLBL_INTR_EN, GAHBCFG); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3412 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3413 | |
| 3414 | /* |
Robert Baldyga | 8acc829 | 2013-09-19 11:50:23 +0200 | [diff] [blame] | 3415 | * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts |
| 3416 | * when we have no data to transfer. Otherwise we get being flooded by |
| 3417 | * interrupts. |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3418 | */ |
| 3419 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3420 | dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ? |
Mian Yousaf Kaukab | 6ff2e83 | 2015-01-09 13:38:42 +0100 | [diff] [blame] | 3421 | DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3422 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3423 | DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3424 | DIEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3425 | |
| 3426 | /* |
| 3427 | * 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] | 3428 | * DMA mode we may need this and StsPhseRcvd. |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3429 | */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3430 | dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK | |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3431 | DOEPMSK_STSPHSERCVDMSK) : 0) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3432 | DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK | |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3433 | DOEPMSK_SETUPMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3434 | DOEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3435 | |
Vahram Aharonyan | ec01f0b | 2016-11-14 19:16:43 -0800 | [diff] [blame] | 3436 | /* Enable BNA interrupt for DDMA */ |
Minas Harutyunyan | 37981e0 | 2018-05-03 17:25:37 +0400 | [diff] [blame] | 3437 | if (using_desc_dma(hsotg)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3438 | dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK); |
| 3439 | dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK); |
Minas Harutyunyan | 37981e0 | 2018-05-03 17:25:37 +0400 | [diff] [blame] | 3440 | } |
Vahram Aharonyan | ec01f0b | 2016-11-14 19:16:43 -0800 | [diff] [blame] | 3441 | |
Grigor Tovmasyan | ca531bc | 2018-08-29 20:59:34 +0400 | [diff] [blame] | 3442 | /* Enable Service Interval mode if supported */ |
| 3443 | if (using_desc_dma(hsotg) && hsotg->params.service_interval) |
| 3444 | dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED); |
| 3445 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3446 | dwc2_writel(hsotg, 0, DAINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3447 | |
| 3448 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3449 | dwc2_readl(hsotg, DIEPCTL0), |
| 3450 | dwc2_readl(hsotg, DOEPCTL0)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3451 | |
| 3452 | /* enable in and out endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3453 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3454 | |
| 3455 | /* |
| 3456 | * Enable the RXFIFO when in slave mode, as this is how we collect |
| 3457 | * the data. In DMA mode, we get events from the FIFO but also |
| 3458 | * things we cannot process, so do not use it. |
| 3459 | */ |
| 3460 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3461 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3462 | |
| 3463 | /* Enable interrupts for EP0 in and out */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3464 | dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1); |
| 3465 | dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3466 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3467 | if (!is_usb_reset) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3468 | dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE); |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3469 | udelay(10); /* see openiboot */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3470 | dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE); |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3471 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3472 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3473 | dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3474 | |
| 3475 | /* |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3476 | * DxEPCTL_USBActEp says RO in manual, but seems to be set by |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3477 | * writing to the EPCTL register.. |
| 3478 | */ |
| 3479 | |
| 3480 | /* set to read 1 8byte packet */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3481 | dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
| 3482 | DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3483 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3484 | 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] | 3485 | DXEPCTL_CNAK | DXEPCTL_EPENA | |
| 3486 | DXEPCTL_USBACTEP, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3487 | DOEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3488 | |
| 3489 | /* enable, but don't activate EP0in */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3490 | dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | |
| 3491 | DXEPCTL_USBACTEP, DIEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3492 | |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3493 | /* clear global NAKs */ |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3494 | val = DCTL_CGOUTNAK | DCTL_CGNPINNAK; |
| 3495 | if (!is_usb_reset) |
| 3496 | val |= DCTL_SFTDISCON; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3497 | dwc2_set_bit(hsotg, DCTL, val); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3498 | |
Sevak Arakelyan | 21b0340 | 2018-01-24 17:43:32 +0400 | [diff] [blame] | 3499 | /* configure the core to support LPM */ |
| 3500 | dwc2_gadget_init_lpm(hsotg); |
| 3501 | |
Grigor Tovmasyan | 15d9dbf | 2018-08-29 21:01:59 +0400 | [diff] [blame] | 3502 | /* program GREFCLK register if needed */ |
| 3503 | if (using_desc_dma(hsotg) && hsotg->params.service_interval) |
| 3504 | dwc2_gadget_program_ref_clk(hsotg); |
| 3505 | |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3506 | /* must be at-least 3ms to allow bus to see disconnect */ |
| 3507 | mdelay(3); |
| 3508 | |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 3509 | hsotg->lx_state = DWC2_L0; |
Vardan Mikayelyan | 755d739 | 2018-01-16 16:04:24 +0400 | [diff] [blame] | 3510 | |
| 3511 | dwc2_hsotg_enqueue_setup(hsotg); |
| 3512 | |
| 3513 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3514 | dwc2_readl(hsotg, DIEPCTL0), |
| 3515 | dwc2_readl(hsotg, DOEPCTL0)); |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3516 | } |
Marek Szyprowski | ac3c81f | 2014-10-20 12:45:35 +0200 | [diff] [blame] | 3517 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3518 | static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3519 | { |
| 3520 | /* set the soft-disconnect bit */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3521 | dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3522 | } |
| 3523 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3524 | void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3525 | { |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3526 | /* remove the soft-disconnect and let's go */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3527 | dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | /** |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3531 | * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt. |
| 3532 | * @hsotg: The device state: |
| 3533 | * |
| 3534 | * This interrupt indicates one of the following conditions occurred while |
| 3535 | * transmitting an ISOC transaction. |
| 3536 | * - Corrupted IN Token for ISOC EP. |
| 3537 | * - Packet not complete in FIFO. |
| 3538 | * |
| 3539 | * The following actions will be taken: |
| 3540 | * - Determine the EP |
| 3541 | * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO |
| 3542 | */ |
| 3543 | static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg) |
| 3544 | { |
| 3545 | struct dwc2_hsotg_ep *hs_ep; |
| 3546 | u32 epctrl; |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3547 | u32 daintmsk; |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3548 | u32 idx; |
| 3549 | |
| 3550 | dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n"); |
| 3551 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3552 | daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3553 | |
Artur Petrosyan | d5d5f07 | 2018-05-05 04:30:16 -0400 | [diff] [blame] | 3554 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3555 | hs_ep = hsotg->eps_in[idx]; |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3556 | /* Proceed only unmasked ISOC EPs */ |
John Keeping | 89066b3 | 2018-07-15 15:50:43 +0100 | [diff] [blame] | 3557 | if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous) |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3558 | continue; |
| 3559 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3560 | epctrl = dwc2_readl(hsotg, DIEPCTL(idx)); |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3561 | if ((epctrl & DXEPCTL_EPENA) && |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3562 | dwc2_gadget_target_frame_elapsed(hs_ep)) { |
| 3563 | epctrl |= DXEPCTL_SNAK; |
| 3564 | epctrl |= DXEPCTL_EPDIS; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3565 | dwc2_writel(hsotg, epctrl, DIEPCTL(idx)); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | /* Clear interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3570 | dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | /** |
| 3574 | * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt |
| 3575 | * @hsotg: The device state: |
| 3576 | * |
| 3577 | * This interrupt indicates one of the following conditions occurred while |
| 3578 | * transmitting an ISOC transaction. |
| 3579 | * - Corrupted OUT Token for ISOC EP. |
| 3580 | * - Packet not complete in FIFO. |
| 3581 | * |
| 3582 | * The following actions will be taken: |
| 3583 | * - Determine the EP |
| 3584 | * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed. |
| 3585 | */ |
| 3586 | static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg) |
| 3587 | { |
| 3588 | u32 gintsts; |
| 3589 | u32 gintmsk; |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3590 | u32 daintmsk; |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3591 | u32 epctrl; |
| 3592 | struct dwc2_hsotg_ep *hs_ep; |
| 3593 | int idx; |
| 3594 | |
| 3595 | dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__); |
| 3596 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3597 | daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3598 | daintmsk >>= DAINT_OUTEP_SHIFT; |
| 3599 | |
Artur Petrosyan | d5d5f07 | 2018-05-05 04:30:16 -0400 | [diff] [blame] | 3600 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3601 | hs_ep = hsotg->eps_out[idx]; |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3602 | /* Proceed only unmasked ISOC EPs */ |
John Keeping | 89066b3 | 2018-07-15 15:50:43 +0100 | [diff] [blame] | 3603 | if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous) |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3604 | continue; |
| 3605 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3606 | epctrl = dwc2_readl(hsotg, DOEPCTL(idx)); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3607 | if ((epctrl & DXEPCTL_EPENA) && |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3608 | dwc2_gadget_target_frame_elapsed(hs_ep)) { |
| 3609 | /* Unmask GOUTNAKEFF interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3610 | gintmsk = dwc2_readl(hsotg, GINTMSK); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3611 | gintmsk |= GINTSTS_GOUTNAKEFF; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3612 | dwc2_writel(hsotg, gintmsk, GINTMSK); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3613 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3614 | gintsts = dwc2_readl(hsotg, GINTSTS); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3615 | if (!(gintsts & GINTSTS_GOUTNAKEFF)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3616 | dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3617 | break; |
| 3618 | } |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | /* Clear interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3623 | dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3627 | * dwc2_hsotg_irq - handle device interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3628 | * @irq: The IRQ number triggered |
| 3629 | * @pw: The pw value when registered the handler. |
| 3630 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3631 | static irqreturn_t dwc2_hsotg_irq(int irq, void *pw) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3632 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3633 | struct dwc2_hsotg *hsotg = pw; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3634 | int retry_count = 8; |
| 3635 | u32 gintsts; |
| 3636 | u32 gintmsk; |
| 3637 | |
Vardan Mikayelyan | ee3de8d | 2016-04-27 20:20:48 -0700 | [diff] [blame] | 3638 | if (!dwc2_is_device_mode(hsotg)) |
| 3639 | return IRQ_NONE; |
| 3640 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3641 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3642 | irq_retry: |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3643 | gintsts = dwc2_readl(hsotg, GINTSTS); |
| 3644 | gintmsk = dwc2_readl(hsotg, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3645 | |
| 3646 | dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n", |
| 3647 | __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count); |
| 3648 | |
| 3649 | gintsts &= gintmsk; |
| 3650 | |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3651 | if (gintsts & GINTSTS_RESETDET) { |
| 3652 | dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__); |
| 3653 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3654 | dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3655 | |
| 3656 | /* This event must be used only if controller is suspended */ |
| 3657 | if (hsotg->lx_state == DWC2_L2) { |
Vardan Mikayelyan | 41ba9b9 | 2018-02-16 14:06:36 +0400 | [diff] [blame] | 3658 | dwc2_exit_partial_power_down(hsotg, true); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3659 | hsotg->lx_state = DWC2_L0; |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3664 | u32 usb_status = dwc2_readl(hsotg, GOTGCTL); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3665 | u32 connected = hsotg->connected; |
| 3666 | |
| 3667 | dev_dbg(hsotg->dev, "%s: USBRst\n", __func__); |
| 3668 | dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3669 | dwc2_readl(hsotg, GNPTXSTS)); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3670 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3671 | dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3672 | |
| 3673 | /* Report disconnection if it is not already done. */ |
| 3674 | dwc2_hsotg_disconnect(hsotg); |
| 3675 | |
Minas Harutyunyan | 307bc11 | 2017-07-11 14:25:13 +0400 | [diff] [blame] | 3676 | /* Reset device address to zero */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3677 | dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK); |
Minas Harutyunyan | 307bc11 | 2017-07-11 14:25:13 +0400 | [diff] [blame] | 3678 | |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3679 | if (usb_status & GOTGCTL_BSESVLD && connected) |
| 3680 | dwc2_hsotg_core_init_disconnected(hsotg, true); |
| 3681 | } |
| 3682 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3683 | if (gintsts & GINTSTS_ENUMDONE) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3684 | dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3685 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3686 | dwc2_hsotg_irq_enumdone(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3687 | } |
| 3688 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3689 | if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3690 | u32 daint = dwc2_readl(hsotg, DAINT); |
| 3691 | u32 daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3692 | u32 daint_out, daint_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3693 | int ep; |
| 3694 | |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3695 | daint &= daintmsk; |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3696 | daint_out = daint >> DAINT_OUTEP_SHIFT; |
| 3697 | daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3698 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3699 | dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint); |
| 3700 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 3701 | for (ep = 0; ep < hsotg->num_of_eps && daint_out; |
| 3702 | ep++, daint_out >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3703 | if (daint_out & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3704 | dwc2_hsotg_epint(hsotg, ep, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3705 | } |
| 3706 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 3707 | for (ep = 0; ep < hsotg->num_of_eps && daint_in; |
| 3708 | ep++, daint_in >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3709 | if (daint_in & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3710 | dwc2_hsotg_epint(hsotg, ep, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3711 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3712 | } |
| 3713 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3714 | /* check both FIFOs */ |
| 3715 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3716 | if (gintsts & GINTSTS_NPTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3717 | dev_dbg(hsotg->dev, "NPTxFEmp\n"); |
| 3718 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3719 | /* |
| 3720 | * Disable the interrupt to stop it happening again |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3721 | * unless one of these endpoint routines decides that |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3722 | * it needs re-enabling |
| 3723 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3724 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3725 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP); |
| 3726 | dwc2_hsotg_irq_fifoempty(hsotg, false); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3727 | } |
| 3728 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3729 | if (gintsts & GINTSTS_PTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3730 | dev_dbg(hsotg->dev, "PTxFEmp\n"); |
| 3731 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3732 | /* See note in GINTSTS_NPTxFEmp */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3733 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3734 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP); |
| 3735 | dwc2_hsotg_irq_fifoempty(hsotg, true); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3736 | } |
| 3737 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3738 | if (gintsts & GINTSTS_RXFLVL) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3739 | /* |
| 3740 | * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3741 | * we need to retry dwc2_hsotg_handle_rx if this is still |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3742 | * set. |
| 3743 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3744 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3745 | dwc2_hsotg_handle_rx(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3746 | } |
| 3747 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3748 | if (gintsts & GINTSTS_ERLYSUSP) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3749 | dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n"); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3750 | dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3751 | } |
| 3752 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3753 | /* |
| 3754 | * these next two seem to crop-up occasionally causing the core |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3755 | * to shutdown the USB transfer, so try clearing them and logging |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3756 | * the occurrence. |
| 3757 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3758 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3759 | if (gintsts & GINTSTS_GOUTNAKEFF) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3760 | u8 idx; |
| 3761 | u32 epctrl; |
| 3762 | u32 gintmsk; |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3763 | u32 daintmsk; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3764 | struct dwc2_hsotg_ep *hs_ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3765 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3766 | daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3767 | daintmsk >>= DAINT_OUTEP_SHIFT; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3768 | /* Mask this interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3769 | gintmsk = dwc2_readl(hsotg, GINTMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3770 | gintmsk &= ~GINTSTS_GOUTNAKEFF; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3771 | dwc2_writel(hsotg, gintmsk, GINTMSK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3772 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3773 | dev_dbg(hsotg->dev, "GOUTNakEff triggered\n"); |
Artur Petrosyan | d5d5f07 | 2018-05-05 04:30:16 -0400 | [diff] [blame] | 3774 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3775 | hs_ep = hsotg->eps_out[idx]; |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3776 | /* Proceed only unmasked ISOC EPs */ |
John Keeping | 89066b3 | 2018-07-15 15:50:43 +0100 | [diff] [blame] | 3777 | if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous) |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3778 | continue; |
| 3779 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3780 | epctrl = dwc2_readl(hsotg, DOEPCTL(idx)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3781 | |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3782 | if (epctrl & DXEPCTL_EPENA) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3783 | epctrl |= DXEPCTL_SNAK; |
| 3784 | epctrl |= DXEPCTL_EPDIS; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3785 | dwc2_writel(hsotg, epctrl, DOEPCTL(idx)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3790 | } |
| 3791 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3792 | if (gintsts & GINTSTS_GINNAKEFF) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3793 | dev_info(hsotg->dev, "GINNakEff triggered\n"); |
| 3794 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3795 | dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3796 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3797 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3798 | } |
| 3799 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3800 | if (gintsts & GINTSTS_INCOMPL_SOIN) |
| 3801 | dwc2_gadget_handle_incomplete_isoc_in(hsotg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 3802 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3803 | if (gintsts & GINTSTS_INCOMPL_SOOUT) |
| 3804 | dwc2_gadget_handle_incomplete_isoc_out(hsotg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 3805 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3806 | /* |
| 3807 | * if we've had fifo events, we should try and go around the |
| 3808 | * loop again to see if there's any point in returning yet. |
| 3809 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3810 | |
| 3811 | if (gintsts & IRQ_RETRY_MASK && --retry_count > 0) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 3812 | goto irq_retry; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3813 | |
Grigor Tovmasyan | 187c529 | 2018-08-29 21:02:57 +0400 | [diff] [blame] | 3814 | /* Check WKUP_ALERT interrupt*/ |
| 3815 | if (hsotg->params.service_interval) |
| 3816 | dwc2_gadget_wkup_alert_handler(hsotg); |
| 3817 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3818 | spin_unlock(&hsotg->lock); |
| 3819 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3820 | return IRQ_HANDLED; |
| 3821 | } |
| 3822 | |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3823 | static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg, |
| 3824 | struct dwc2_hsotg_ep *hs_ep) |
| 3825 | { |
| 3826 | u32 epctrl_reg; |
| 3827 | u32 epint_reg; |
| 3828 | |
| 3829 | epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) : |
| 3830 | DOEPCTL(hs_ep->index); |
| 3831 | epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) : |
| 3832 | DOEPINT(hs_ep->index); |
| 3833 | |
| 3834 | dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__, |
| 3835 | hs_ep->name); |
| 3836 | |
| 3837 | if (hs_ep->dir_in) { |
| 3838 | if (hsotg->dedicated_fifos || hs_ep->periodic) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3839 | dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3840 | /* Wait for Nak effect */ |
| 3841 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, |
| 3842 | DXEPINT_INEPNAKEFF, 100)) |
| 3843 | dev_warn(hsotg->dev, |
| 3844 | "%s: timeout DIEPINT.NAKEFF\n", |
| 3845 | __func__); |
| 3846 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3847 | dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3848 | /* Wait for Nak effect */ |
| 3849 | if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, |
| 3850 | GINTSTS_GINNAKEFF, 100)) |
| 3851 | dev_warn(hsotg->dev, |
| 3852 | "%s: timeout GINTSTS.GINNAKEFF\n", |
| 3853 | __func__); |
| 3854 | } |
| 3855 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3856 | if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF)) |
| 3857 | dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3858 | |
| 3859 | /* Wait for global nak to take effect */ |
| 3860 | if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, |
| 3861 | GINTSTS_GOUTNAKEFF, 100)) |
| 3862 | dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n", |
| 3863 | __func__); |
| 3864 | } |
| 3865 | |
| 3866 | /* Disable ep */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3867 | dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3868 | |
| 3869 | /* Wait for ep to be disabled */ |
| 3870 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100)) |
| 3871 | dev_warn(hsotg->dev, |
| 3872 | "%s: timeout DOEPCTL.EPDisable\n", __func__); |
| 3873 | |
| 3874 | /* Clear EPDISBLD interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3875 | dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3876 | |
| 3877 | if (hs_ep->dir_in) { |
| 3878 | unsigned short fifo_index; |
| 3879 | |
| 3880 | if (hsotg->dedicated_fifos || hs_ep->periodic) |
| 3881 | fifo_index = hs_ep->fifo_index; |
| 3882 | else |
| 3883 | fifo_index = 0; |
| 3884 | |
| 3885 | /* Flush TX FIFO */ |
| 3886 | dwc2_flush_tx_fifo(hsotg, fifo_index); |
| 3887 | |
| 3888 | /* Clear Global In NP NAK in Shared FIFO for non periodic ep */ |
| 3889 | if (!hsotg->dedicated_fifos && !hs_ep->periodic) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3890 | dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3891 | |
| 3892 | } else { |
| 3893 | /* Remove global NAKs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3894 | dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3895 | } |
| 3896 | } |
| 3897 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3898 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3899 | * dwc2_hsotg_ep_enable - enable the given endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3900 | * @ep: The USB endpint to configure |
| 3901 | * @desc: The USB endpoint descriptor to configure with. |
| 3902 | * |
| 3903 | * This is called from the USB gadget code's usb_ep_enable(). |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3904 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3905 | static int dwc2_hsotg_ep_enable(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3906 | const struct usb_endpoint_descriptor *desc) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3907 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3908 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3909 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3910 | unsigned long flags; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3911 | unsigned int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3912 | u32 epctrl_reg; |
| 3913 | u32 epctrl; |
| 3914 | u32 mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3915 | u32 mc; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3916 | u32 mask; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3917 | unsigned int dir_in; |
| 3918 | unsigned int i, val, size; |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 3919 | int ret = 0; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3920 | unsigned char ep_type; |
Minas Harutyunyan | 54f37f5 | 2019-03-18 14:24:30 +0400 | [diff] [blame] | 3921 | int desc_num; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3922 | |
| 3923 | dev_dbg(hsotg->dev, |
| 3924 | "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n", |
| 3925 | __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes, |
| 3926 | desc->wMaxPacketSize, desc->bInterval); |
| 3927 | |
| 3928 | /* not to be called for EP0 */ |
Vahram Aharonyan | 8c3d609 | 2016-04-27 20:20:46 -0700 | [diff] [blame] | 3929 | if (index == 0) { |
| 3930 | dev_err(hsotg->dev, "%s: called for EP 0\n", __func__); |
| 3931 | return -EINVAL; |
| 3932 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3933 | |
| 3934 | dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0; |
| 3935 | if (dir_in != hs_ep->dir_in) { |
| 3936 | dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__); |
| 3937 | return -EINVAL; |
| 3938 | } |
| 3939 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3940 | ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 3941 | mps = usb_endpoint_maxp(desc); |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3942 | mc = usb_endpoint_maxp_mult(desc); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3943 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3944 | /* ISOC IN in DDMA supported bInterval up to 10 */ |
| 3945 | if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC && |
| 3946 | dir_in && desc->bInterval > 10) { |
| 3947 | dev_err(hsotg->dev, |
| 3948 | "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__); |
| 3949 | return -EINVAL; |
| 3950 | } |
| 3951 | |
| 3952 | /* High bandwidth ISOC OUT in DDMA not supported */ |
| 3953 | if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC && |
| 3954 | !dir_in && mc > 1) { |
| 3955 | dev_err(hsotg->dev, |
| 3956 | "%s: ISOC OUT, DDMA: HB not supported!\n", __func__); |
| 3957 | return -EINVAL; |
| 3958 | } |
| 3959 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3960 | /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3961 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3962 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3963 | epctrl = dwc2_readl(hsotg, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3964 | |
| 3965 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n", |
| 3966 | __func__, epctrl, epctrl_reg); |
| 3967 | |
Minas Harutyunyan | 54f37f5 | 2019-03-18 14:24:30 +0400 | [diff] [blame] | 3968 | if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC) |
| 3969 | desc_num = MAX_DMA_DESC_NUM_HS_ISOC; |
| 3970 | else |
| 3971 | desc_num = MAX_DMA_DESC_NUM_GENERIC; |
| 3972 | |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3973 | /* Allocate DMA descriptor chain for non-ctrl endpoints */ |
Vardan Mikayelyan | 9383e08 | 2017-01-05 18:01:48 -0800 | [diff] [blame] | 3974 | if (using_desc_dma(hsotg) && !hs_ep->desc_list) { |
| 3975 | hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev, |
Minas Harutyunyan | 54f37f5 | 2019-03-18 14:24:30 +0400 | [diff] [blame] | 3976 | desc_num * sizeof(struct dwc2_dma_desc), |
Marek Szyprowski | 86e881e | 2016-12-01 10:02:11 +0100 | [diff] [blame] | 3977 | &hs_ep->desc_list_dma, GFP_ATOMIC); |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3978 | if (!hs_ep->desc_list) { |
| 3979 | ret = -ENOMEM; |
| 3980 | goto error2; |
| 3981 | } |
| 3982 | } |
| 3983 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 3984 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3985 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3986 | epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK); |
| 3987 | epctrl |= DXEPCTL_MPS(mps); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3988 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3989 | /* |
| 3990 | * mark the endpoint as active, otherwise the core may ignore |
| 3991 | * transactions entirely for this endpoint |
| 3992 | */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3993 | epctrl |= DXEPCTL_USBACTEP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3994 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3995 | /* update the endpoint state */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3996 | 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] | 3997 | |
| 3998 | /* default, set to non-periodic */ |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3999 | hs_ep->isochronous = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4000 | hs_ep->periodic = 0; |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 4001 | hs_ep->halted = 0; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 4002 | hs_ep->interval = desc->bInterval; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 4003 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 4004 | switch (ep_type) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4005 | case USB_ENDPOINT_XFER_ISOC: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4006 | epctrl |= DXEPCTL_EPTYPE_ISO; |
| 4007 | epctrl |= DXEPCTL_SETEVENFR; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 4008 | hs_ep->isochronous = 1; |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 4009 | hs_ep->interval = 1 << (desc->bInterval - 1); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4010 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
Vahram Aharonyan | ab7d219 | 2016-11-14 19:16:36 -0800 | [diff] [blame] | 4011 | hs_ep->next_desc = 0; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 4012 | hs_ep->compl_desc = 0; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4013 | if (dir_in) { |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 4014 | hs_ep->periodic = 1; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4015 | mask = dwc2_readl(hsotg, DIEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4016 | mask |= DIEPMSK_NAKMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4017 | dwc2_writel(hsotg, mask, DIEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4018 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4019 | mask = dwc2_readl(hsotg, DOEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4020 | mask |= DOEPMSK_OUTTKNEPDISMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4021 | dwc2_writel(hsotg, mask, DOEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4022 | } |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 4023 | break; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4024 | |
| 4025 | case USB_ENDPOINT_XFER_BULK: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4026 | epctrl |= DXEPCTL_EPTYPE_BULK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4027 | break; |
| 4028 | |
| 4029 | case USB_ENDPOINT_XFER_INT: |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4030 | if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4031 | hs_ep->periodic = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4032 | |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 4033 | if (hsotg->gadget.speed == USB_SPEED_HIGH) |
| 4034 | hs_ep->interval = 1 << (desc->bInterval - 1); |
| 4035 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4036 | epctrl |= DXEPCTL_EPTYPE_INTERRUPT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4037 | break; |
| 4038 | |
| 4039 | case USB_ENDPOINT_XFER_CONTROL: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4040 | epctrl |= DXEPCTL_EPTYPE_CONTROL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4041 | break; |
| 4042 | } |
| 4043 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4044 | /* |
| 4045 | * if the hardware has dedicated fifos, we must give each IN EP |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4046 | * a unique tx-fifo even if it is non-periodic. |
| 4047 | */ |
Robert Baldyga | 21f3bb5 | 2016-08-29 13:38:57 -0700 | [diff] [blame] | 4048 | if (dir_in && hsotg->dedicated_fifos) { |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 4049 | u32 fifo_index = 0; |
| 4050 | u32 fifo_size = UINT_MAX; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4051 | |
| 4052 | size = hs_ep->ep.maxpacket * hs_ep->mc; |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 4053 | for (i = 1; i < hsotg->num_of_eps; ++i) { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4054 | if (hsotg->fifo_map & (1 << i)) |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4055 | continue; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4056 | val = dwc2_readl(hsotg, DPTXFSIZN(i)); |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4057 | val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4058 | if (val < size) |
| 4059 | continue; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 4060 | /* Search for smallest acceptable fifo */ |
| 4061 | if (val < fifo_size) { |
| 4062 | fifo_size = val; |
| 4063 | fifo_index = i; |
| 4064 | } |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4065 | } |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 4066 | if (!fifo_index) { |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 4067 | dev_err(hsotg->dev, |
| 4068 | "%s: No suitable fifo found\n", __func__); |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 4069 | ret = -ENOMEM; |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4070 | goto error1; |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 4071 | } |
Minas Harutyunyan | 97311c8 | 2019-01-31 18:28:07 +0400 | [diff] [blame] | 4072 | epctrl &= ~(DXEPCTL_TXFNUM_LIMIT << DXEPCTL_TXFNUM_SHIFT); |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 4073 | hsotg->fifo_map |= 1 << fifo_index; |
| 4074 | epctrl |= DXEPCTL_TXFNUM(fifo_index); |
| 4075 | hs_ep->fifo_index = fifo_index; |
| 4076 | hs_ep->fifo_size = fifo_size; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4077 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4078 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4079 | /* for non control endpoints, set PID to D0 */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4080 | if (index && !hs_ep->isochronous) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4081 | epctrl |= DXEPCTL_SETD0PID; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4082 | |
Artur Petrosyan | 5295322 | 2018-04-16 08:45:31 -0400 | [diff] [blame] | 4083 | /* WA for Full speed ISOC IN in DDMA mode. |
| 4084 | * By Clear NAK status of EP, core will send ZLP |
| 4085 | * to IN token and assert NAK interrupt relying |
| 4086 | * on TxFIFO status only |
| 4087 | */ |
| 4088 | |
| 4089 | if (hsotg->gadget.speed == USB_SPEED_FULL && |
| 4090 | hs_ep->isochronous && dir_in) { |
| 4091 | /* The WA applies only to core versions from 2.72a |
| 4092 | * to 4.00a (including both). Also for FS_IOT_1.00a |
| 4093 | * and HS_IOT_1.00a. |
| 4094 | */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4095 | u32 gsnpsid = dwc2_readl(hsotg, GSNPSID); |
Artur Petrosyan | 5295322 | 2018-04-16 08:45:31 -0400 | [diff] [blame] | 4096 | |
| 4097 | if ((gsnpsid >= DWC2_CORE_REV_2_72a && |
| 4098 | gsnpsid <= DWC2_CORE_REV_4_00a) || |
| 4099 | gsnpsid == DWC2_FS_IOT_REV_1_00a || |
| 4100 | gsnpsid == DWC2_HS_IOT_REV_1_00a) |
| 4101 | epctrl |= DXEPCTL_CNAK; |
| 4102 | } |
| 4103 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4104 | dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n", |
| 4105 | __func__, epctrl); |
| 4106 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4107 | dwc2_writel(hsotg, epctrl, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4108 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4109 | __func__, dwc2_readl(hsotg, epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4110 | |
| 4111 | /* enable the endpoint interrupt */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4112 | dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4113 | |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4114 | error1: |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4115 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4116 | |
| 4117 | error2: |
| 4118 | if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) { |
Minas Harutyunyan | 54f37f5 | 2019-03-18 14:24:30 +0400 | [diff] [blame] | 4119 | dmam_free_coherent(hsotg->dev, desc_num * |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4120 | sizeof(struct dwc2_dma_desc), |
| 4121 | hs_ep->desc_list, hs_ep->desc_list_dma); |
| 4122 | hs_ep->desc_list = NULL; |
| 4123 | } |
| 4124 | |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 4125 | return ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4126 | } |
| 4127 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4128 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4129 | * dwc2_hsotg_ep_disable - disable given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4130 | * @ep: The endpoint to disable. |
| 4131 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4132 | static int dwc2_hsotg_ep_disable(struct usb_ep *ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4133 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4134 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4135 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4136 | int dir_in = hs_ep->dir_in; |
| 4137 | int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4138 | u32 epctrl_reg; |
| 4139 | u32 ctrl; |
| 4140 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 4141 | dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4142 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4143 | if (ep == &hsotg->eps_out[0]->ep) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4144 | dev_err(hsotg->dev, "%s: called for ep0\n", __func__); |
| 4145 | return -EINVAL; |
| 4146 | } |
| 4147 | |
John Stultz | 9b481092 | 2017-10-23 14:32:49 -0700 | [diff] [blame] | 4148 | if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { |
| 4149 | dev_err(hsotg->dev, "%s: called in host mode?\n", __func__); |
| 4150 | return -EINVAL; |
| 4151 | } |
| 4152 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 4153 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4154 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4155 | ctrl = dwc2_readl(hsotg, epctrl_reg); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 4156 | |
| 4157 | if (ctrl & DXEPCTL_EPENA) |
| 4158 | dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep); |
| 4159 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4160 | ctrl &= ~DXEPCTL_EPENA; |
| 4161 | ctrl &= ~DXEPCTL_USBACTEP; |
| 4162 | ctrl |= DXEPCTL_SNAK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4163 | |
| 4164 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4165 | dwc2_writel(hsotg, ctrl, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4166 | |
| 4167 | /* disable endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4168 | 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] | 4169 | |
Mian Yousaf Kaukab | 1141ea0 | 2015-01-09 13:38:57 +0100 | [diff] [blame] | 4170 | /* terminate all requests with shutdown */ |
| 4171 | kill_all_requests(hsotg, hs_ep, -ESHUTDOWN); |
| 4172 | |
Robert Baldyga | 1c07b20 | 2016-08-29 13:39:00 -0700 | [diff] [blame] | 4173 | hsotg->fifo_map &= ~(1 << hs_ep->fifo_index); |
| 4174 | hs_ep->fifo_index = 0; |
| 4175 | hs_ep->fifo_size = 0; |
| 4176 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4177 | return 0; |
| 4178 | } |
| 4179 | |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4180 | static int dwc2_hsotg_ep_disable_lock(struct usb_ep *ep) |
| 4181 | { |
| 4182 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
| 4183 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 4184 | unsigned long flags; |
| 4185 | int ret; |
| 4186 | |
| 4187 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4188 | ret = dwc2_hsotg_ep_disable(ep); |
| 4189 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4190 | return ret; |
| 4191 | } |
| 4192 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4193 | /** |
| 4194 | * on_list - check request is on the given endpoint |
| 4195 | * @ep: The endpoint to check. |
| 4196 | * @test: The request to test if it is on the endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4197 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4198 | 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] | 4199 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4200 | struct dwc2_hsotg_req *req, *treq; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4201 | |
| 4202 | list_for_each_entry_safe(req, treq, &ep->queue, queue) { |
| 4203 | if (req == test) |
| 4204 | return true; |
| 4205 | } |
| 4206 | |
| 4207 | return false; |
| 4208 | } |
| 4209 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4210 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4211 | * dwc2_hsotg_ep_dequeue - dequeue given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4212 | * @ep: The endpoint to dequeue. |
| 4213 | * @req: The request to be removed from a queue. |
| 4214 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4215 | 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] | 4216 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4217 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 4218 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4219 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4220 | unsigned long flags; |
| 4221 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 4222 | dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4223 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4224 | spin_lock_irqsave(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4225 | |
| 4226 | if (!on_list(hs_ep, hs_req)) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4227 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4228 | return -EINVAL; |
| 4229 | } |
| 4230 | |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 4231 | /* Dequeue already started request */ |
| 4232 | if (req == &hs_ep->req->req) |
| 4233 | dwc2_hsotg_ep_stop_xfr(hs, hs_ep); |
| 4234 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4235 | dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4236 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4237 | |
| 4238 | return 0; |
| 4239 | } |
| 4240 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4241 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4242 | * dwc2_hsotg_ep_sethalt - set halt on a given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4243 | * @ep: The endpoint to set halt. |
| 4244 | * @value: Set or unset the halt. |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4245 | * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if |
| 4246 | * the endpoint is busy processing requests. |
| 4247 | * |
| 4248 | * We need to stall the endpoint immediately if request comes from set_feature |
| 4249 | * protocol command handler. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4250 | */ |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4251 | 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] | 4252 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4253 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4254 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4255 | int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4256 | u32 epreg; |
| 4257 | u32 epctl; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4258 | u32 xfertype; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4259 | |
| 4260 | dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value); |
| 4261 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 4262 | if (index == 0) { |
| 4263 | if (value) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4264 | dwc2_hsotg_stall_ep0(hs); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 4265 | else |
| 4266 | dev_warn(hs->dev, |
| 4267 | "%s: can't clear halt on ep0\n", __func__); |
| 4268 | return 0; |
| 4269 | } |
| 4270 | |
Vahram Aharonyan | 15186f1 | 2016-05-23 22:41:59 -0700 | [diff] [blame] | 4271 | if (hs_ep->isochronous) { |
| 4272 | dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name); |
| 4273 | return -EINVAL; |
| 4274 | } |
| 4275 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4276 | if (!now && value && !list_empty(&hs_ep->queue)) { |
| 4277 | dev_dbg(hs->dev, "%s request is pending, cannot halt\n", |
| 4278 | ep->name); |
| 4279 | return -EAGAIN; |
| 4280 | } |
| 4281 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4282 | if (hs_ep->dir_in) { |
| 4283 | epreg = DIEPCTL(index); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4284 | epctl = dwc2_readl(hs, epreg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4285 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4286 | if (value) { |
Felipe Balbi | 5a350d5 | 2015-06-29 20:17:22 -0500 | [diff] [blame] | 4287 | epctl |= DXEPCTL_STALL | DXEPCTL_SNAK; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4288 | if (epctl & DXEPCTL_EPENA) |
| 4289 | epctl |= DXEPCTL_EPDIS; |
| 4290 | } else { |
| 4291 | epctl &= ~DXEPCTL_STALL; |
| 4292 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 4293 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4294 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 4295 | epctl |= DXEPCTL_SETD0PID; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4296 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4297 | dwc2_writel(hs, epctl, epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4298 | } else { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4299 | epreg = DOEPCTL(index); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4300 | epctl = dwc2_readl(hs, epreg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4301 | |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 4302 | if (value) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4303 | epctl |= DXEPCTL_STALL; |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 4304 | } else { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4305 | epctl &= ~DXEPCTL_STALL; |
| 4306 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 4307 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4308 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 4309 | epctl |= DXEPCTL_SETD0PID; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4310 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4311 | dwc2_writel(hs, epctl, epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4312 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4313 | |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 4314 | hs_ep->halted = value; |
| 4315 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4316 | return 0; |
| 4317 | } |
| 4318 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4319 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4320 | * 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] | 4321 | * @ep: The endpoint to set halt. |
| 4322 | * @value: Set or unset the halt. |
| 4323 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4324 | 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] | 4325 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4326 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4327 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4328 | unsigned long flags = 0; |
| 4329 | int ret = 0; |
| 4330 | |
| 4331 | spin_lock_irqsave(&hs->lock, flags); |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4332 | ret = dwc2_hsotg_ep_sethalt(ep, value, false); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4333 | spin_unlock_irqrestore(&hs->lock, flags); |
| 4334 | |
| 4335 | return ret; |
| 4336 | } |
| 4337 | |
Bhumika Goyal | ebce561 | 2017-08-12 17:34:55 +0530 | [diff] [blame] | 4338 | static const struct usb_ep_ops dwc2_hsotg_ep_ops = { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4339 | .enable = dwc2_hsotg_ep_enable, |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4340 | .disable = dwc2_hsotg_ep_disable_lock, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4341 | .alloc_request = dwc2_hsotg_ep_alloc_request, |
| 4342 | .free_request = dwc2_hsotg_ep_free_request, |
| 4343 | .queue = dwc2_hsotg_ep_queue_lock, |
| 4344 | .dequeue = dwc2_hsotg_ep_dequeue, |
| 4345 | .set_halt = dwc2_hsotg_ep_sethalt_lock, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4346 | /* note, don't believe we have any call for the fifo routines */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4347 | }; |
| 4348 | |
| 4349 | /** |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4350 | * dwc2_hsotg_init - initialize the usb core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4351 | * @hsotg: The driver state |
| 4352 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4353 | static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg) |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4354 | { |
| 4355 | /* unmask subset of endpoint interrupts */ |
| 4356 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4357 | dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4358 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4359 | DIEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4360 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4361 | dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4362 | DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4363 | DOEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4364 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4365 | dwc2_writel(hsotg, 0, DAINTMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4366 | |
| 4367 | /* Be in disconnected state until gadget is registered */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4368 | dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4369 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4370 | /* setup fifos */ |
| 4371 | |
| 4372 | dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4373 | dwc2_readl(hsotg, GRXFSIZ), |
| 4374 | dwc2_readl(hsotg, GNPTXFSIZ)); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4375 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4376 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4377 | |
Gregory Herrero | f509004 | 2015-01-09 13:38:47 +0100 | [diff] [blame] | 4378 | if (using_dma(hsotg)) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4379 | dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4380 | } |
| 4381 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4382 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4383 | * dwc2_hsotg_udc_start - prepare the udc for work |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4384 | * @gadget: The usb gadget state |
| 4385 | * @driver: The usb gadget driver |
| 4386 | * |
| 4387 | * Perform initialization to prepare udc device and driver |
| 4388 | * to work. |
| 4389 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4390 | static int dwc2_hsotg_udc_start(struct usb_gadget *gadget, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4391 | struct usb_gadget_driver *driver) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4392 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4393 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4394 | unsigned long flags; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4395 | int ret; |
| 4396 | |
| 4397 | if (!hsotg) { |
Pavel Machek | a023da3 | 2013-09-30 14:56:02 +0200 | [diff] [blame] | 4398 | pr_err("%s: called with no device\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4399 | return -ENODEV; |
| 4400 | } |
| 4401 | |
| 4402 | if (!driver) { |
| 4403 | dev_err(hsotg->dev, "%s: no driver\n", __func__); |
| 4404 | return -EINVAL; |
| 4405 | } |
| 4406 | |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 4407 | if (driver->max_speed < USB_SPEED_FULL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4408 | dev_err(hsotg->dev, "%s: bad speed\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4409 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 4410 | if (!driver->setup) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4411 | dev_err(hsotg->dev, "%s: missing entry points\n", __func__); |
| 4412 | return -EINVAL; |
| 4413 | } |
| 4414 | |
| 4415 | WARN_ON(hsotg->driver); |
| 4416 | |
| 4417 | driver->driver.bus = NULL; |
| 4418 | hsotg->driver = driver; |
Alexandre Pereira da Silva | 7d7b229 | 2012-06-26 11:27:10 -0300 | [diff] [blame] | 4419 | hsotg->gadget.dev.of_node = hsotg->dev->of_node; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4420 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4421 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4422 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) { |
| 4423 | ret = dwc2_lowlevel_hw_enable(hsotg); |
| 4424 | if (ret) |
| 4425 | goto err; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4426 | } |
| 4427 | |
Gregory Herrero | f6c0159 | 2015-01-09 13:38:41 +0100 | [diff] [blame] | 4428 | if (!IS_ERR_OR_NULL(hsotg->uphy)) |
| 4429 | otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 4430 | |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4431 | spin_lock_irqsave(&hsotg->lock, flags); |
John Youn | d0f0ac5 | 2016-09-07 19:39:37 -0700 | [diff] [blame] | 4432 | if (dwc2_hw_is_device(hsotg)) { |
| 4433 | dwc2_hsotg_init(hsotg); |
| 4434 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
| 4435 | } |
| 4436 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4437 | hsotg->enabled = 0; |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4438 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4439 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 4440 | gadget->sg_supported = using_desc_dma(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4441 | dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4442 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4443 | return 0; |
| 4444 | |
| 4445 | err: |
| 4446 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4447 | return ret; |
| 4448 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4449 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4450 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4451 | * dwc2_hsotg_udc_stop - stop the udc |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4452 | * @gadget: The usb gadget state |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4453 | * |
| 4454 | * Stop udc hw block and stay tunned for future transmissions |
| 4455 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4456 | static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4457 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4458 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4459 | unsigned long flags = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4460 | int ep; |
| 4461 | |
| 4462 | if (!hsotg) |
| 4463 | return -ENODEV; |
| 4464 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4465 | /* all endpoints should be shutdown */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4466 | for (ep = 1; ep < hsotg->num_of_eps; ep++) { |
| 4467 | if (hsotg->eps_in[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4468 | dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4469 | if (hsotg->eps_out[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4470 | dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4471 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4472 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4473 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4474 | |
Marek Szyprowski | 32805c3 | 2014-10-20 12:45:33 +0200 | [diff] [blame] | 4475 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4476 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4477 | hsotg->enabled = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4478 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4479 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4480 | |
Gregory Herrero | f6c0159 | 2015-01-09 13:38:41 +0100 | [diff] [blame] | 4481 | if (!IS_ERR_OR_NULL(hsotg->uphy)) |
| 4482 | otg_set_peripheral(hsotg->uphy->otg, NULL); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 4483 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4484 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 4485 | dwc2_lowlevel_hw_disable(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4486 | |
| 4487 | return 0; |
| 4488 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4489 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4490 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4491 | * dwc2_hsotg_gadget_getframe - read the frame number |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4492 | * @gadget: The usb gadget state |
| 4493 | * |
| 4494 | * Read the {micro} frame number |
| 4495 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4496 | static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4497 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4498 | return dwc2_hsotg_read_frameno(to_hsotg(gadget)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4499 | } |
| 4500 | |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4501 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4502 | * dwc2_hsotg_pullup - connect/disconnect the USB PHY |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4503 | * @gadget: The usb gadget state |
| 4504 | * @is_on: Current state of the USB PHY |
| 4505 | * |
| 4506 | * Connect/Disconnect the USB PHY pullup |
| 4507 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4508 | static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on) |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4509 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4510 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4511 | unsigned long flags = 0; |
| 4512 | |
Gregory Herrero | 77ba911 | 2015-09-29 12:08:19 +0200 | [diff] [blame] | 4513 | 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] | 4514 | hsotg->op_state); |
Gregory Herrero | 77ba911 | 2015-09-29 12:08:19 +0200 | [diff] [blame] | 4515 | |
| 4516 | /* Don't modify pullup state while in host mode */ |
| 4517 | if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { |
| 4518 | hsotg->enabled = is_on; |
| 4519 | return 0; |
| 4520 | } |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4521 | |
| 4522 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4523 | if (is_on) { |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4524 | hsotg->enabled = 1; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4525 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4526 | /* Enable ACG feature in device mode,if supported */ |
| 4527 | dwc2_enable_acg(hsotg); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4528 | dwc2_hsotg_core_connect(hsotg); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4529 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4530 | dwc2_hsotg_core_disconnect(hsotg); |
| 4531 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4532 | hsotg->enabled = 0; |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4536 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4537 | |
| 4538 | return 0; |
| 4539 | } |
| 4540 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4541 | 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] | 4542 | { |
| 4543 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 4544 | unsigned long flags; |
| 4545 | |
| 4546 | dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active); |
| 4547 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4548 | |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4549 | /* |
Vardan Mikayelyan | 41ba9b9 | 2018-02-16 14:06:36 +0400 | [diff] [blame] | 4550 | * If controller is hibernated, it must exit from power_down |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4551 | * before being initialized / de-initialized |
| 4552 | */ |
| 4553 | if (hsotg->lx_state == DWC2_L2) |
Vardan Mikayelyan | 41ba9b9 | 2018-02-16 14:06:36 +0400 | [diff] [blame] | 4554 | dwc2_exit_partial_power_down(hsotg, false); |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4555 | |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4556 | if (is_active) { |
Gregory Herrero | cd0e641 | 2015-09-29 12:08:20 +0200 | [diff] [blame] | 4557 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 4558 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4559 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4560 | if (hsotg->enabled) { |
| 4561 | /* Enable ACG feature in device mode,if supported */ |
| 4562 | dwc2_enable_acg(hsotg); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4563 | dwc2_hsotg_core_connect(hsotg); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4564 | } |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4565 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4566 | dwc2_hsotg_core_disconnect(hsotg); |
| 4567 | dwc2_hsotg_disconnect(hsotg); |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4571 | return 0; |
| 4572 | } |
| 4573 | |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 4574 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4575 | * dwc2_hsotg_vbus_draw - report bMaxPower field |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 4576 | * @gadget: The usb gadget state |
| 4577 | * @mA: Amount of current |
| 4578 | * |
| 4579 | * Report how much power the device may consume to the phy. |
| 4580 | */ |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4581 | 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] | 4582 | { |
| 4583 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 4584 | |
| 4585 | if (IS_ERR_OR_NULL(hsotg->uphy)) |
| 4586 | return -ENOTSUPP; |
| 4587 | return usb_phy_set_power(hsotg->uphy, mA); |
| 4588 | } |
| 4589 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4590 | static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = { |
| 4591 | .get_frame = dwc2_hsotg_gadget_getframe, |
| 4592 | .udc_start = dwc2_hsotg_udc_start, |
| 4593 | .udc_stop = dwc2_hsotg_udc_stop, |
| 4594 | .pullup = dwc2_hsotg_pullup, |
| 4595 | .vbus_session = dwc2_hsotg_vbus_session, |
| 4596 | .vbus_draw = dwc2_hsotg_vbus_draw, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4597 | }; |
| 4598 | |
| 4599 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4600 | * dwc2_hsotg_initep - initialise a single endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4601 | * @hsotg: The device state. |
| 4602 | * @hs_ep: The endpoint to be initialised. |
| 4603 | * @epnum: The endpoint number |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4604 | * @dir_in: True if direction is in. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4605 | * |
| 4606 | * Initialise the given endpoint (as part of the probe and device state |
| 4607 | * creation) to give to the gadget driver. Setup the endpoint name, any |
| 4608 | * direction information and other state that may be required. |
| 4609 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4610 | static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4611 | struct dwc2_hsotg_ep *hs_ep, |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4612 | int epnum, |
| 4613 | bool dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4614 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4615 | char *dir; |
| 4616 | |
| 4617 | if (epnum == 0) |
| 4618 | dir = ""; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4619 | else if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4620 | dir = "in"; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4621 | else |
| 4622 | dir = "out"; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4623 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4624 | hs_ep->dir_in = dir_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4625 | hs_ep->index = epnum; |
| 4626 | |
| 4627 | snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir); |
| 4628 | |
| 4629 | INIT_LIST_HEAD(&hs_ep->queue); |
| 4630 | INIT_LIST_HEAD(&hs_ep->ep.ep_list); |
| 4631 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4632 | /* add to the list of endpoints known by the gadget driver */ |
| 4633 | if (epnum) |
| 4634 | list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list); |
| 4635 | |
| 4636 | hs_ep->parent = hsotg; |
| 4637 | hs_ep->ep.name = hs_ep->name; |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 4638 | |
| 4639 | if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW) |
| 4640 | usb_ep_set_maxpacket_limit(&hs_ep->ep, 8); |
| 4641 | else |
| 4642 | usb_ep_set_maxpacket_limit(&hs_ep->ep, |
| 4643 | epnum ? 1024 : EP0_MPS_LIMIT); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4644 | hs_ep->ep.ops = &dwc2_hsotg_ep_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4645 | |
Robert Baldyga | 2954522 | 2015-07-31 16:00:18 +0200 | [diff] [blame] | 4646 | if (epnum == 0) { |
| 4647 | hs_ep->ep.caps.type_control = true; |
| 4648 | } else { |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 4649 | if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) { |
| 4650 | hs_ep->ep.caps.type_iso = true; |
| 4651 | hs_ep->ep.caps.type_bulk = true; |
| 4652 | } |
Robert Baldyga | 2954522 | 2015-07-31 16:00:18 +0200 | [diff] [blame] | 4653 | hs_ep->ep.caps.type_int = true; |
| 4654 | } |
| 4655 | |
| 4656 | if (dir_in) |
| 4657 | hs_ep->ep.caps.dir_in = true; |
| 4658 | else |
| 4659 | hs_ep->ep.caps.dir_out = true; |
| 4660 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4661 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4662 | * 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] | 4663 | * to be something valid. |
| 4664 | */ |
| 4665 | |
| 4666 | if (using_dma(hsotg)) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4667 | u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15); |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4668 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4669 | if (dir_in) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4670 | dwc2_writel(hsotg, next, DIEPCTL(epnum)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4671 | else |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4672 | dwc2_writel(hsotg, next, DOEPCTL(epnum)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4673 | } |
| 4674 | } |
| 4675 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4676 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4677 | * dwc2_hsotg_hw_cfg - read HW configuration registers |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4678 | * @hsotg: Programming view of the DWC_otg controller |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4679 | * |
| 4680 | * Read the USB core HW configuration registers |
| 4681 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4682 | static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4683 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4684 | u32 cfg; |
| 4685 | u32 ep_type; |
| 4686 | u32 i; |
| 4687 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4688 | /* check hardware configuration */ |
| 4689 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4690 | hsotg->num_of_eps = hsotg->hw_params.num_dev_ep; |
| 4691 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4692 | /* Add ep0 */ |
| 4693 | hsotg->num_of_eps++; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4694 | |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 4695 | hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, |
| 4696 | sizeof(struct dwc2_hsotg_ep), |
| 4697 | GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4698 | if (!hsotg->eps_in[0]) |
| 4699 | return -ENOMEM; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4700 | /* Same dwc2_hsotg_ep is used in both directions for ep0 */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4701 | hsotg->eps_out[0] = hsotg->eps_in[0]; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4702 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4703 | cfg = hsotg->hw_params.dev_ep_dirs; |
Roshan Pius | 251a17f | 2015-02-02 14:55:38 -0800 | [diff] [blame] | 4704 | 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] | 4705 | ep_type = cfg & 3; |
| 4706 | /* Direction in or both */ |
| 4707 | if (!(ep_type & 2)) { |
| 4708 | hsotg->eps_in[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4709 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4710 | if (!hsotg->eps_in[i]) |
| 4711 | return -ENOMEM; |
| 4712 | } |
| 4713 | /* Direction out or both */ |
| 4714 | if (!(ep_type & 1)) { |
| 4715 | hsotg->eps_out[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4716 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4717 | if (!hsotg->eps_out[i]) |
| 4718 | return -ENOMEM; |
| 4719 | } |
| 4720 | } |
| 4721 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4722 | hsotg->fifo_mem = hsotg->hw_params.total_fifo_size; |
| 4723 | hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo; |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4724 | |
Marek Szyprowski | cff9eb7 | 2014-09-09 10:44:55 +0200 | [diff] [blame] | 4725 | dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n", |
| 4726 | hsotg->num_of_eps, |
| 4727 | hsotg->dedicated_fifos ? "dedicated" : "shared", |
| 4728 | hsotg->fifo_mem); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4729 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4730 | } |
| 4731 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4732 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4733 | * dwc2_hsotg_dump - dump state of the udc |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4734 | * @hsotg: Programming view of the DWC_otg controller |
| 4735 | * |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4736 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4737 | static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4738 | { |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 4739 | #ifdef DEBUG |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4740 | struct device *dev = hsotg->dev; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4741 | u32 val; |
| 4742 | int idx; |
| 4743 | |
| 4744 | dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4745 | dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL), |
| 4746 | dwc2_readl(hsotg, DIEPMSK)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4747 | |
Mian Yousaf Kaukab | f889f23 | 2015-01-30 09:09:36 +0100 | [diff] [blame] | 4748 | dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4749 | dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4750 | |
| 4751 | dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4752 | dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4753 | |
| 4754 | /* show periodic fifo settings */ |
| 4755 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 4756 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4757 | val = dwc2_readl(hsotg, DPTXFSIZN(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4758 | dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4759 | val >> FIFOSIZE_DEPTH_SHIFT, |
| 4760 | val & FIFOSIZE_STARTADDR_MASK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4761 | } |
| 4762 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 4763 | for (idx = 0; idx < hsotg->num_of_eps; idx++) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4764 | dev_info(dev, |
| 4765 | "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] | 4766 | dwc2_readl(hsotg, DIEPCTL(idx)), |
| 4767 | dwc2_readl(hsotg, DIEPTSIZ(idx)), |
| 4768 | dwc2_readl(hsotg, DIEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4769 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4770 | val = dwc2_readl(hsotg, DOEPCTL(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4771 | dev_info(dev, |
| 4772 | "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] | 4773 | idx, dwc2_readl(hsotg, DOEPCTL(idx)), |
| 4774 | dwc2_readl(hsotg, DOEPTSIZ(idx)), |
| 4775 | dwc2_readl(hsotg, DOEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4779 | dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE)); |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 4780 | #endif |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4781 | } |
| 4782 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4783 | /** |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4784 | * dwc2_gadget_init - init function for gadget |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4785 | * @hsotg: Programming view of the DWC_otg controller |
| 4786 | * |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4787 | */ |
Vardan Mikayelyan | f376899 | 2017-12-25 15:17:45 +0400 | [diff] [blame] | 4788 | int dwc2_gadget_init(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4789 | { |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4790 | struct device *dev = hsotg->dev; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4791 | int epnum; |
| 4792 | int ret; |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4793 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 4794 | /* Dump fifo information */ |
| 4795 | dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n", |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 4796 | hsotg->params.g_np_tx_fifo_size); |
| 4797 | 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] | 4798 | |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 4799 | hsotg->gadget.max_speed = USB_SPEED_HIGH; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4800 | hsotg->gadget.ops = &dwc2_hsotg_gadget_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4801 | hsotg->gadget.name = dev_name(dev); |
Vardan Mikayelyan | fa389a6 | 2018-02-16 14:08:53 +0400 | [diff] [blame] | 4802 | hsotg->remote_wakeup_allowed = 0; |
John Youn | 7455f8b | 2018-01-24 17:44:51 +0400 | [diff] [blame] | 4803 | |
| 4804 | if (hsotg->params.lpm) |
| 4805 | hsotg->gadget.lpm_capable = true; |
| 4806 | |
Gregory Herrero | 097ee66 | 2015-04-29 22:09:10 +0200 | [diff] [blame] | 4807 | if (hsotg->dr_mode == USB_DR_MODE_OTG) |
| 4808 | hsotg->gadget.is_otg = 1; |
Mian Yousaf Kaukab | ec4cc65 | 2015-09-22 15:16:55 +0200 | [diff] [blame] | 4809 | else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 4810 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4811 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4812 | ret = dwc2_hsotg_hw_cfg(hsotg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4813 | if (ret) { |
| 4814 | dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4815 | return ret; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4816 | } |
| 4817 | |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4818 | hsotg->ctrl_buff = devm_kzalloc(hsotg->dev, |
| 4819 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
Wolfram Sang | 8bae0f8 | 2016-08-25 19:39:02 +0200 | [diff] [blame] | 4820 | if (!hsotg->ctrl_buff) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4821 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4822 | |
| 4823 | hsotg->ep0_buff = devm_kzalloc(hsotg->dev, |
| 4824 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
Wolfram Sang | 8bae0f8 | 2016-08-25 19:39:02 +0200 | [diff] [blame] | 4825 | if (!hsotg->ep0_buff) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4826 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4827 | |
Vahram Aharonyan | 0f6b80c | 2016-11-09 19:27:56 -0800 | [diff] [blame] | 4828 | if (using_desc_dma(hsotg)) { |
| 4829 | ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg); |
| 4830 | if (ret < 0) |
| 4831 | return ret; |
| 4832 | } |
| 4833 | |
Vardan Mikayelyan | f376899 | 2017-12-25 15:17:45 +0400 | [diff] [blame] | 4834 | ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq, |
| 4835 | IRQF_SHARED, dev_name(hsotg->dev), hsotg); |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 4836 | if (ret < 0) { |
Dinh Nguyen | db8178c | 2014-11-11 11:13:37 -0600 | [diff] [blame] | 4837 | dev_err(dev, "cannot claim IRQ for gadget\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4838 | return ret; |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 4839 | } |
| 4840 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4841 | /* hsotg->num_of_eps holds number of EPs other than ep0 */ |
| 4842 | |
| 4843 | if (hsotg->num_of_eps == 0) { |
| 4844 | dev_err(dev, "wrong number of EPs (zero)\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4845 | return -EINVAL; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4846 | } |
| 4847 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4848 | /* setup endpoint information */ |
| 4849 | |
| 4850 | INIT_LIST_HEAD(&hsotg->gadget.ep_list); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4851 | hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4852 | |
| 4853 | /* allocate EP0 request */ |
| 4854 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4855 | 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] | 4856 | GFP_KERNEL); |
| 4857 | if (!hsotg->ctrl_req) { |
| 4858 | dev_err(dev, "failed to allocate ctrl req\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4859 | return -ENOMEM; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4860 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4861 | |
| 4862 | /* initialise the endpoints now the core has been initialised */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4863 | for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) { |
| 4864 | if (hsotg->eps_in[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4865 | dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4866 | epnum, 1); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4867 | if (hsotg->eps_out[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4868 | dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4869 | epnum, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4870 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4871 | |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4872 | ret = usb_add_gadget_udc(dev, &hsotg->gadget); |
Grigor Tovmasyan | 9bb073a | 2018-05-24 18:22:30 +0400 | [diff] [blame] | 4873 | if (ret) { |
| 4874 | dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, |
| 4875 | hsotg->ctrl_req); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4876 | return ret; |
Grigor Tovmasyan | 9bb073a | 2018-05-24 18:22:30 +0400 | [diff] [blame] | 4877 | } |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4878 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4879 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4880 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4881 | } |
| 4882 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4883 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4884 | * dwc2_hsotg_remove - remove function for hsotg driver |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4885 | * @hsotg: Programming view of the DWC_otg controller |
| 4886 | * |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4887 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4888 | int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4889 | { |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 4890 | usb_del_gadget_udc(&hsotg->gadget); |
Grigor Tovmasyan | 9bb073a | 2018-05-24 18:22:30 +0400 | [diff] [blame] | 4891 | 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] | 4892 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4893 | return 0; |
| 4894 | } |
| 4895 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4896 | int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4897 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4898 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4899 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4900 | if (hsotg->lx_state != DWC2_L0) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4901 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4902 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4903 | if (hsotg->driver) { |
| 4904 | int ep; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4905 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4906 | dev_info(hsotg->dev, "suspending usb gadget %s\n", |
| 4907 | hsotg->driver->driver.name); |
| 4908 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4909 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4910 | if (hsotg->enabled) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4911 | dwc2_hsotg_core_disconnect(hsotg); |
| 4912 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4913 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4914 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4915 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4916 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 4917 | if (hsotg->eps_in[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4918 | dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4919 | if (hsotg->eps_out[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4920 | dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4921 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4922 | } |
| 4923 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4924 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4925 | } |
| 4926 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4927 | int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4928 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4929 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4930 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4931 | if (hsotg->lx_state == DWC2_L2) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4932 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4933 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4934 | if (hsotg->driver) { |
| 4935 | dev_info(hsotg->dev, "resuming usb gadget %s\n", |
| 4936 | hsotg->driver->driver.name); |
Robert Baldyga | d00b414 | 2014-09-09 10:44:57 +0200 | [diff] [blame] | 4937 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4938 | spin_lock_irqsave(&hsotg->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4939 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4940 | if (hsotg->enabled) { |
| 4941 | /* Enable ACG feature in device mode,if supported */ |
| 4942 | dwc2_enable_acg(hsotg); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4943 | dwc2_hsotg_core_connect(hsotg); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4944 | } |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4945 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4946 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4947 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4948 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4949 | } |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4950 | |
| 4951 | /** |
| 4952 | * dwc2_backup_device_registers() - Backup controller device registers. |
| 4953 | * When suspending usb bus, registers needs to be backuped |
| 4954 | * if controller power is disabled once suspended. |
| 4955 | * |
| 4956 | * @hsotg: Programming view of the DWC_otg controller |
| 4957 | */ |
| 4958 | int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg) |
| 4959 | { |
| 4960 | struct dwc2_dregs_backup *dr; |
| 4961 | int i; |
| 4962 | |
| 4963 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 4964 | |
| 4965 | /* Backup dev regs */ |
| 4966 | dr = &hsotg->dr_backup; |
| 4967 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4968 | dr->dcfg = dwc2_readl(hsotg, DCFG); |
| 4969 | dr->dctl = dwc2_readl(hsotg, DCTL); |
| 4970 | dr->daintmsk = dwc2_readl(hsotg, DAINTMSK); |
| 4971 | dr->diepmsk = dwc2_readl(hsotg, DIEPMSK); |
| 4972 | dr->doepmsk = dwc2_readl(hsotg, DOEPMSK); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4973 | |
| 4974 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 4975 | /* Backup IN EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4976 | dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4977 | |
| 4978 | /* Ensure DATA PID is correctly configured */ |
| 4979 | if (dr->diepctl[i] & DXEPCTL_DPID) |
| 4980 | dr->diepctl[i] |= DXEPCTL_SETD1PID; |
| 4981 | else |
| 4982 | dr->diepctl[i] |= DXEPCTL_SETD0PID; |
| 4983 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4984 | dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i)); |
| 4985 | dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4986 | |
| 4987 | /* Backup OUT EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4988 | dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4989 | |
| 4990 | /* Ensure DATA PID is correctly configured */ |
| 4991 | if (dr->doepctl[i] & DXEPCTL_DPID) |
| 4992 | dr->doepctl[i] |= DXEPCTL_SETD1PID; |
| 4993 | else |
| 4994 | dr->doepctl[i] |= DXEPCTL_SETD0PID; |
| 4995 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4996 | dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i)); |
| 4997 | dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i)); |
| 4998 | dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4999 | } |
| 5000 | dr->valid = true; |
| 5001 | return 0; |
| 5002 | } |
| 5003 | |
| 5004 | /** |
| 5005 | * dwc2_restore_device_registers() - Restore controller device registers. |
| 5006 | * When resuming usb bus, device registers needs to be restored |
| 5007 | * if controller power were disabled. |
| 5008 | * |
| 5009 | * @hsotg: Programming view of the DWC_otg controller |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5010 | * @remote_wakeup: Indicates whether resume is initiated by Device or Host. |
| 5011 | * |
| 5012 | * Return: 0 if successful, negative error code otherwise |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5013 | */ |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5014 | int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup) |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5015 | { |
| 5016 | struct dwc2_dregs_backup *dr; |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5017 | int i; |
| 5018 | |
| 5019 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 5020 | |
| 5021 | /* Restore dev regs */ |
| 5022 | dr = &hsotg->dr_backup; |
| 5023 | if (!dr->valid) { |
| 5024 | dev_err(hsotg->dev, "%s: no device registers to restore\n", |
| 5025 | __func__); |
| 5026 | return -EINVAL; |
| 5027 | } |
| 5028 | dr->valid = false; |
| 5029 | |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5030 | if (!remote_wakeup) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5031 | dwc2_writel(hsotg, dr->dctl, DCTL); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5032 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5033 | dwc2_writel(hsotg, dr->daintmsk, DAINTMSK); |
| 5034 | dwc2_writel(hsotg, dr->diepmsk, DIEPMSK); |
| 5035 | dwc2_writel(hsotg, dr->doepmsk, DOEPMSK); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5036 | |
| 5037 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 5038 | /* Restore IN EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5039 | dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i)); |
| 5040 | dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i)); |
| 5041 | dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i)); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5042 | /** WA for enabled EPx's IN in DDMA mode. On entering to |
| 5043 | * hibernation wrong value read and saved from DIEPDMAx, |
| 5044 | * as result BNA interrupt asserted on hibernation exit |
| 5045 | * by restoring from saved area. |
| 5046 | */ |
| 5047 | if (hsotg->params.g_dma_desc && |
| 5048 | (dr->diepctl[i] & DXEPCTL_EPENA)) |
| 5049 | dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5050 | dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i)); |
| 5051 | dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i)); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5052 | /* Restore OUT EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5053 | dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i)); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5054 | /* WA for enabled EPx's OUT in DDMA mode. On entering to |
| 5055 | * hibernation wrong value read and saved from DOEPDMAx, |
| 5056 | * as result BNA interrupt asserted on hibernation exit |
| 5057 | * by restoring from saved area. |
| 5058 | */ |
| 5059 | if (hsotg->params.g_dma_desc && |
| 5060 | (dr->doepctl[i] & DXEPCTL_EPENA)) |
| 5061 | dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5062 | dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i)); |
| 5063 | dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5064 | } |
| 5065 | |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5066 | return 0; |
| 5067 | } |
Sevak Arakelyan | 21b0340 | 2018-01-24 17:43:32 +0400 | [diff] [blame] | 5068 | |
| 5069 | /** |
| 5070 | * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode |
| 5071 | * |
| 5072 | * @hsotg: Programming view of DWC_otg controller |
| 5073 | * |
| 5074 | */ |
| 5075 | void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg) |
| 5076 | { |
| 5077 | u32 val; |
| 5078 | |
| 5079 | if (!hsotg->params.lpm) |
| 5080 | return; |
| 5081 | |
| 5082 | val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES; |
| 5083 | val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0; |
| 5084 | val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0; |
| 5085 | val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT; |
| 5086 | val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0; |
Minas Harutyunyan | 4663756 | 2019-04-18 15:40:43 +0400 | [diff] [blame] | 5087 | val |= GLPMCFG_LPM_REJECT_CTRL_CONTROL; |
Artur Petrosyan | 9aed8c0 | 2018-11-02 11:29:55 -0400 | [diff] [blame] | 5088 | val |= GLPMCFG_LPM_ACCEPT_CTRL_ISOC; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5089 | dwc2_writel(hsotg, val, GLPMCFG); |
| 5090 | dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG)); |
Grigor Tovmasyan | 4abe453 | 2018-08-29 21:02:28 +0400 | [diff] [blame] | 5091 | |
| 5092 | /* Unmask WKUP_ALERT Interrupt */ |
| 5093 | if (hsotg->params.service_interval) |
| 5094 | dwc2_set_bit(hsotg, GINTMSK2, GINTMSK2_WKUP_ALERT_INT_MSK); |
Sevak Arakelyan | 21b0340 | 2018-01-24 17:43:32 +0400 | [diff] [blame] | 5095 | } |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5096 | |
| 5097 | /** |
Grigor Tovmasyan | 15d9dbf | 2018-08-29 21:01:59 +0400 | [diff] [blame] | 5098 | * dwc2_gadget_program_ref_clk - Program GREFCLK register in device mode |
| 5099 | * |
| 5100 | * @hsotg: Programming view of DWC_otg controller |
| 5101 | * |
| 5102 | */ |
| 5103 | void dwc2_gadget_program_ref_clk(struct dwc2_hsotg *hsotg) |
| 5104 | { |
| 5105 | u32 val = 0; |
| 5106 | |
| 5107 | val |= GREFCLK_REF_CLK_MODE; |
| 5108 | val |= hsotg->params.ref_clk_per << GREFCLK_REFCLKPER_SHIFT; |
| 5109 | val |= hsotg->params.sof_cnt_wkup_alert << |
| 5110 | GREFCLK_SOF_CNT_WKUP_ALERT_SHIFT; |
| 5111 | |
| 5112 | dwc2_writel(hsotg, val, GREFCLK); |
| 5113 | dev_dbg(hsotg->dev, "GREFCLK=0x%08x\n", dwc2_readl(hsotg, GREFCLK)); |
| 5114 | } |
| 5115 | |
| 5116 | /** |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5117 | * dwc2_gadget_enter_hibernation() - Put controller in Hibernation. |
| 5118 | * |
| 5119 | * @hsotg: Programming view of the DWC_otg controller |
| 5120 | * |
| 5121 | * Return non-zero if failed to enter to hibernation. |
| 5122 | */ |
| 5123 | int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg) |
| 5124 | { |
| 5125 | u32 gpwrdn; |
| 5126 | int ret = 0; |
| 5127 | |
| 5128 | /* Change to L2(suspend) state */ |
| 5129 | hsotg->lx_state = DWC2_L2; |
| 5130 | dev_dbg(hsotg->dev, "Start of hibernation completed\n"); |
| 5131 | ret = dwc2_backup_global_registers(hsotg); |
| 5132 | if (ret) { |
| 5133 | dev_err(hsotg->dev, "%s: failed to backup global registers\n", |
| 5134 | __func__); |
| 5135 | return ret; |
| 5136 | } |
| 5137 | ret = dwc2_backup_device_registers(hsotg); |
| 5138 | if (ret) { |
| 5139 | dev_err(hsotg->dev, "%s: failed to backup device registers\n", |
| 5140 | __func__); |
| 5141 | return ret; |
| 5142 | } |
| 5143 | |
| 5144 | gpwrdn = GPWRDN_PWRDNRSTN; |
| 5145 | gpwrdn |= GPWRDN_PMUACTV; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5146 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5147 | udelay(10); |
| 5148 | |
| 5149 | /* Set flag to indicate that we are in hibernation */ |
| 5150 | hsotg->hibernated = 1; |
| 5151 | |
| 5152 | /* Enable interrupts from wake up logic */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5153 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5154 | gpwrdn |= GPWRDN_PMUINTSEL; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5155 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5156 | udelay(10); |
| 5157 | |
| 5158 | /* Unmask device mode interrupts in GPWRDN */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5159 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5160 | gpwrdn |= GPWRDN_RST_DET_MSK; |
| 5161 | gpwrdn |= GPWRDN_LNSTSCHG_MSK; |
| 5162 | gpwrdn |= GPWRDN_STS_CHGINT_MSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5163 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5164 | udelay(10); |
| 5165 | |
| 5166 | /* Enable Power Down Clamp */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5167 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5168 | gpwrdn |= GPWRDN_PWRDNCLMP; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5169 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5170 | udelay(10); |
| 5171 | |
| 5172 | /* Switch off VDD */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5173 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5174 | gpwrdn |= GPWRDN_PWRDNSWTCH; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5175 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5176 | udelay(10); |
| 5177 | |
| 5178 | /* Save gpwrdn register for further usage if stschng interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5179 | hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5180 | dev_dbg(hsotg->dev, "Hibernation completed\n"); |
| 5181 | |
| 5182 | return ret; |
| 5183 | } |
| 5184 | |
| 5185 | /** |
| 5186 | * dwc2_gadget_exit_hibernation() |
| 5187 | * This function is for exiting from Device mode hibernation by host initiated |
| 5188 | * resume/reset and device initiated remote-wakeup. |
| 5189 | * |
| 5190 | * @hsotg: Programming view of the DWC_otg controller |
| 5191 | * @rem_wakeup: indicates whether resume is initiated by Device or Host. |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 5192 | * @reset: indicates whether resume is initiated by Reset. |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5193 | * |
| 5194 | * Return non-zero if failed to exit from hibernation. |
| 5195 | */ |
| 5196 | int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg, |
| 5197 | int rem_wakeup, int reset) |
| 5198 | { |
| 5199 | u32 pcgcctl; |
| 5200 | u32 gpwrdn; |
| 5201 | u32 dctl; |
| 5202 | int ret = 0; |
| 5203 | struct dwc2_gregs_backup *gr; |
| 5204 | struct dwc2_dregs_backup *dr; |
| 5205 | |
| 5206 | gr = &hsotg->gr_backup; |
| 5207 | dr = &hsotg->dr_backup; |
| 5208 | |
| 5209 | if (!hsotg->hibernated) { |
| 5210 | dev_dbg(hsotg->dev, "Already exited from Hibernation\n"); |
| 5211 | return 1; |
| 5212 | } |
| 5213 | dev_dbg(hsotg->dev, |
| 5214 | "%s: called with rem_wakeup = %d reset = %d\n", |
| 5215 | __func__, rem_wakeup, reset); |
| 5216 | |
| 5217 | dwc2_hib_restore_common(hsotg, rem_wakeup, 0); |
| 5218 | |
| 5219 | if (!reset) { |
| 5220 | /* Clear all pending interupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5221 | dwc2_writel(hsotg, 0xffffffff, GINTSTS); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5222 | } |
| 5223 | |
| 5224 | /* De-assert Restore */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5225 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5226 | gpwrdn &= ~GPWRDN_RESTORE; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5227 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5228 | udelay(10); |
| 5229 | |
| 5230 | if (!rem_wakeup) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5231 | pcgcctl = dwc2_readl(hsotg, PCGCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5232 | pcgcctl &= ~PCGCTL_RSTPDWNMODULE; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5233 | dwc2_writel(hsotg, pcgcctl, PCGCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5234 | } |
| 5235 | |
| 5236 | /* Restore GUSBCFG, DCFG and DCTL */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5237 | dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG); |
| 5238 | dwc2_writel(hsotg, dr->dcfg, DCFG); |
| 5239 | dwc2_writel(hsotg, dr->dctl, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5240 | |
| 5241 | /* De-assert Wakeup Logic */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5242 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5243 | gpwrdn &= ~GPWRDN_PMUACTV; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5244 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5245 | |
| 5246 | if (rem_wakeup) { |
| 5247 | udelay(10); |
| 5248 | /* Start Remote Wakeup Signaling */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5249 | dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5250 | } else { |
| 5251 | udelay(50); |
| 5252 | /* Set Device programming done bit */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5253 | dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5254 | dctl |= DCTL_PWRONPRGDONE; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5255 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5256 | } |
| 5257 | /* Wait for interrupts which must be cleared */ |
| 5258 | mdelay(2); |
| 5259 | /* Clear all pending interupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5260 | dwc2_writel(hsotg, 0xffffffff, GINTSTS); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5261 | |
| 5262 | /* Restore global registers */ |
| 5263 | ret = dwc2_restore_global_registers(hsotg); |
| 5264 | if (ret) { |
| 5265 | dev_err(hsotg->dev, "%s: failed to restore registers\n", |
| 5266 | __func__); |
| 5267 | return ret; |
| 5268 | } |
| 5269 | |
| 5270 | /* Restore device registers */ |
| 5271 | ret = dwc2_restore_device_registers(hsotg, rem_wakeup); |
| 5272 | if (ret) { |
| 5273 | dev_err(hsotg->dev, "%s: failed to restore device registers\n", |
| 5274 | __func__); |
| 5275 | return ret; |
| 5276 | } |
| 5277 | |
| 5278 | if (rem_wakeup) { |
| 5279 | mdelay(10); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5280 | dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5281 | dctl &= ~DCTL_RMTWKUPSIG; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5282 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5283 | } |
| 5284 | |
| 5285 | hsotg->hibernated = 0; |
| 5286 | hsotg->lx_state = DWC2_L0; |
| 5287 | dev_dbg(hsotg->dev, "Hibernation recovery completes here\n"); |
| 5288 | |
| 5289 | return ret; |
| 5290 | } |