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) |
| 719 | maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT : |
| 720 | DEV_DMA_ISOC_RX_NBYTES_LIMIT; |
| 721 | else |
| 722 | maxsize = DEV_DMA_NBYTES_LIMIT; |
| 723 | |
| 724 | /* Above size of one descriptor was chosen, multiple it */ |
| 725 | maxsize *= MAX_DMA_DESC_NUM_GENERIC; |
| 726 | |
| 727 | return maxsize; |
| 728 | } |
| 729 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 730 | /* |
| 731 | * dwc2_gadget_get_desc_params - get DMA descriptor parameters. |
| 732 | * @hs_ep: The endpoint |
| 733 | * @mask: RX/TX bytes mask to be defined |
| 734 | * |
| 735 | * Returns maximum data payload for one descriptor after analyzing endpoint |
| 736 | * characteristics. |
| 737 | * DMA descriptor transfer bytes limit depends on EP type: |
| 738 | * Control out - MPS, |
| 739 | * Isochronous - descriptor rx/tx bytes bitfield limit, |
| 740 | * Control In/Bulk/Interrupt - multiple of mps. This will allow to not |
| 741 | * have concatenations from various descriptors within one packet. |
| 742 | * |
| 743 | * Selects corresponding mask for RX/TX bytes as well. |
| 744 | */ |
| 745 | static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask) |
| 746 | { |
| 747 | u32 mps = hs_ep->ep.maxpacket; |
| 748 | int dir_in = hs_ep->dir_in; |
| 749 | u32 desc_size = 0; |
| 750 | |
| 751 | if (!hs_ep->index && !dir_in) { |
| 752 | desc_size = mps; |
| 753 | *mask = DEV_DMA_NBYTES_MASK; |
| 754 | } else if (hs_ep->isochronous) { |
| 755 | if (dir_in) { |
| 756 | desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT; |
| 757 | *mask = DEV_DMA_ISOC_TX_NBYTES_MASK; |
| 758 | } else { |
| 759 | desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT; |
| 760 | *mask = DEV_DMA_ISOC_RX_NBYTES_MASK; |
| 761 | } |
| 762 | } else { |
| 763 | desc_size = DEV_DMA_NBYTES_LIMIT; |
| 764 | *mask = DEV_DMA_NBYTES_MASK; |
| 765 | |
| 766 | /* Round down desc_size to be mps multiple */ |
| 767 | desc_size -= desc_size % mps; |
| 768 | } |
| 769 | |
| 770 | return desc_size; |
| 771 | } |
| 772 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 773 | static void dwc2_gadget_fill_nonisoc_xfer_ddma_one(struct dwc2_hsotg_ep *hs_ep, |
| 774 | struct dwc2_dma_desc **desc, |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 775 | dma_addr_t dma_buff, |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 776 | unsigned int len, |
| 777 | bool true_last) |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 778 | { |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 779 | int dir_in = hs_ep->dir_in; |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 780 | u32 mps = hs_ep->ep.maxpacket; |
| 781 | u32 maxsize = 0; |
| 782 | u32 offset = 0; |
| 783 | u32 mask = 0; |
| 784 | int i; |
| 785 | |
| 786 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
| 787 | |
| 788 | hs_ep->desc_count = (len / maxsize) + |
| 789 | ((len % maxsize) ? 1 : 0); |
| 790 | if (len == 0) |
| 791 | hs_ep->desc_count = 1; |
| 792 | |
| 793 | for (i = 0; i < hs_ep->desc_count; ++i) { |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 794 | (*desc)->status = 0; |
| 795 | (*desc)->status |= (DEV_DMA_BUFF_STS_HBUSY |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 796 | << DEV_DMA_BUFF_STS_SHIFT); |
| 797 | |
| 798 | if (len > maxsize) { |
| 799 | if (!hs_ep->index && !dir_in) |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 800 | (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC); |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 801 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 802 | (*desc)->status |= |
| 803 | maxsize << DEV_DMA_NBYTES_SHIFT & mask; |
| 804 | (*desc)->buf = dma_buff + offset; |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 805 | |
| 806 | len -= maxsize; |
| 807 | offset += maxsize; |
| 808 | } else { |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 809 | if (true_last) |
| 810 | (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC); |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 811 | |
| 812 | if (dir_in) |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 813 | (*desc)->status |= (len % mps) ? DEV_DMA_SHORT : |
| 814 | ((hs_ep->send_zlp && true_last) ? |
| 815 | DEV_DMA_SHORT : 0); |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 816 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 817 | (*desc)->status |= |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 818 | len << DEV_DMA_NBYTES_SHIFT & mask; |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 819 | (*desc)->buf = dma_buff + offset; |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 822 | (*desc)->status &= ~DEV_DMA_BUFF_STS_MASK; |
| 823 | (*desc)->status |= (DEV_DMA_BUFF_STS_HREADY |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 824 | << DEV_DMA_BUFF_STS_SHIFT); |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 825 | (*desc)++; |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 829 | /* |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 830 | * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain. |
| 831 | * @hs_ep: The endpoint |
| 832 | * @ureq: Request to transfer |
| 833 | * @offset: offset in bytes |
| 834 | * @len: Length of the transfer |
| 835 | * |
| 836 | * This function will iterate over descriptor chain and fill its entries |
| 837 | * with corresponding information based on transfer data. |
| 838 | */ |
| 839 | static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep, |
| 840 | struct usb_request *ureq, |
| 841 | unsigned int offset, |
| 842 | unsigned int len) |
| 843 | { |
| 844 | struct dwc2_dma_desc *desc = hs_ep->desc_list; |
| 845 | struct scatterlist *sg; |
| 846 | int i; |
| 847 | u8 desc_count = 0; |
| 848 | |
| 849 | /* non-DMA sg buffer */ |
| 850 | if (!ureq->num_sgs) { |
| 851 | dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc, |
| 852 | ureq->dma + offset, len, true); |
| 853 | return; |
| 854 | } |
| 855 | |
| 856 | /* DMA sg buffer */ |
| 857 | for_each_sg(ureq->sg, sg, ureq->num_sgs, i) { |
| 858 | dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc, |
| 859 | sg_dma_address(sg) + sg->offset, sg_dma_len(sg), |
| 860 | sg_is_last(sg)); |
| 861 | desc_count += hs_ep->desc_count; |
| 862 | } |
| 863 | |
| 864 | hs_ep->desc_count = desc_count; |
| 865 | } |
| 866 | |
| 867 | /* |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 868 | * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain. |
| 869 | * @hs_ep: The isochronous endpoint. |
| 870 | * @dma_buff: usb requests dma buffer. |
| 871 | * @len: usb request transfer length. |
| 872 | * |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 873 | * Fills next free descriptor with the data of the arrived usb request, |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 874 | * frame info, sets Last and IOC bits increments next_desc. If filled |
| 875 | * descriptor is not the first one, removes L bit from the previous descriptor |
| 876 | * status. |
| 877 | */ |
| 878 | static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep, |
| 879 | dma_addr_t dma_buff, unsigned int len) |
| 880 | { |
| 881 | struct dwc2_dma_desc *desc; |
| 882 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 883 | u32 index; |
| 884 | u32 maxsize = 0; |
| 885 | u32 mask = 0; |
Minas Harutyunyan | 1d8e5c0 | 2018-05-23 16:24:44 +0400 | [diff] [blame] | 886 | u8 pid = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 887 | |
| 888 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 889 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 890 | index = hs_ep->next_desc; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 891 | desc = &hs_ep->desc_list[index]; |
| 892 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 893 | /* Check if descriptor chain full */ |
| 894 | if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) == |
| 895 | DEV_DMA_BUFF_STS_HREADY) { |
| 896 | dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__); |
| 897 | return 1; |
| 898 | } |
| 899 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 900 | /* Clear L bit of previous desc if more than one entries in the chain */ |
| 901 | if (hs_ep->next_desc) |
| 902 | hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L; |
| 903 | |
| 904 | dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n", |
| 905 | __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index); |
| 906 | |
| 907 | desc->status = 0; |
| 908 | desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT); |
| 909 | |
| 910 | desc->buf = dma_buff; |
| 911 | desc->status |= (DEV_DMA_L | DEV_DMA_IOC | |
| 912 | ((len << DEV_DMA_NBYTES_SHIFT) & mask)); |
| 913 | |
| 914 | if (hs_ep->dir_in) { |
Minas Harutyunyan | 1d8e5c0 | 2018-05-23 16:24:44 +0400 | [diff] [blame] | 915 | if (len) |
| 916 | pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket); |
| 917 | else |
| 918 | pid = 1; |
| 919 | desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) & |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 920 | DEV_DMA_ISOC_PID_MASK) | |
| 921 | ((len % hs_ep->ep.maxpacket) ? |
| 922 | DEV_DMA_SHORT : 0) | |
| 923 | ((hs_ep->target_frame << |
| 924 | DEV_DMA_ISOC_FRNUM_SHIFT) & |
| 925 | DEV_DMA_ISOC_FRNUM_MASK); |
| 926 | } |
| 927 | |
| 928 | desc->status &= ~DEV_DMA_BUFF_STS_MASK; |
| 929 | desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT); |
| 930 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 931 | /* Increment frame number by interval for IN */ |
| 932 | if (hs_ep->dir_in) |
| 933 | dwc2_gadget_incr_frame_num(hs_ep); |
| 934 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 935 | /* Update index of last configured entry in the chain */ |
| 936 | hs_ep->next_desc++; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 937 | if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_GENERIC) |
| 938 | hs_ep->next_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA |
| 945 | * @hs_ep: The isochronous endpoint. |
| 946 | * |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 947 | * Prepare descriptor chain for isochronous endpoints. Afterwards |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 948 | * write DMA address to HW and enable the endpoint. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 949 | */ |
| 950 | static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 951 | { |
| 952 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 953 | struct dwc2_hsotg_req *hs_req, *treq; |
| 954 | int index = hs_ep->index; |
| 955 | int ret; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 956 | int i; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 957 | u32 dma_reg; |
| 958 | u32 depctl; |
| 959 | u32 ctrl; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 960 | struct dwc2_dma_desc *desc; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 961 | |
| 962 | if (list_empty(&hs_ep->queue)) { |
Minas Harutyunyan | 1ffba90 | 2018-06-12 12:37:29 +0400 | [diff] [blame] | 963 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 964 | dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__); |
| 965 | return; |
| 966 | } |
| 967 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 968 | /* Initialize descriptor chain by Host Busy status */ |
| 969 | for (i = 0; i < MAX_DMA_DESC_NUM_GENERIC; i++) { |
| 970 | desc = &hs_ep->desc_list[i]; |
| 971 | desc->status = 0; |
| 972 | desc->status |= (DEV_DMA_BUFF_STS_HBUSY |
| 973 | << DEV_DMA_BUFF_STS_SHIFT); |
| 974 | } |
| 975 | |
| 976 | hs_ep->next_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 977 | list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) { |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 978 | dma_addr_t dma_addr = hs_req->req.dma; |
| 979 | |
| 980 | if (hs_req->req.num_sgs) { |
| 981 | WARN_ON(hs_req->req.num_sgs > 1); |
| 982 | dma_addr = sg_dma_address(hs_req->req.sg); |
| 983 | } |
| 984 | ret = dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr, |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 985 | hs_req->req.length); |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 986 | if (ret) |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 987 | break; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 988 | } |
| 989 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 990 | hs_ep->compl_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 991 | depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 992 | dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index); |
| 993 | |
| 994 | /* write descriptor chain address to control register */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 995 | dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 996 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 997 | ctrl = dwc2_readl(hsotg, depctl); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 998 | ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 999 | dwc2_writel(hsotg, ctrl, depctl); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 1002 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1003 | * 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] | 1004 | * @hsotg: The controller state. |
| 1005 | * @hs_ep: The endpoint to process a request for |
| 1006 | * @hs_req: The request to start. |
| 1007 | * @continuing: True if we are doing more for the current request. |
| 1008 | * |
| 1009 | * Start the given request running by setting the endpoint registers |
| 1010 | * appropriately, and writing any data to the FIFOs. |
| 1011 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1012 | static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1013 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1014 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1015 | bool continuing) |
| 1016 | { |
| 1017 | struct usb_request *ureq = &hs_req->req; |
| 1018 | int index = hs_ep->index; |
| 1019 | int dir_in = hs_ep->dir_in; |
| 1020 | u32 epctrl_reg; |
| 1021 | u32 epsize_reg; |
| 1022 | u32 epsize; |
| 1023 | u32 ctrl; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1024 | unsigned int length; |
| 1025 | unsigned int packets; |
| 1026 | unsigned int maxreq; |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1027 | unsigned int dma_reg; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1028 | |
| 1029 | if (index != 0) { |
| 1030 | if (hs_ep->req && !continuing) { |
| 1031 | dev_err(hsotg->dev, "%s: active request\n", __func__); |
| 1032 | WARN_ON(1); |
| 1033 | return; |
| 1034 | } else if (hs_ep->req != hs_req && continuing) { |
| 1035 | dev_err(hsotg->dev, |
| 1036 | "%s: continue different req\n", __func__); |
| 1037 | WARN_ON(1); |
| 1038 | return; |
| 1039 | } |
| 1040 | } |
| 1041 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1042 | dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index); |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 1043 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 1044 | epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1045 | |
| 1046 | 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] | 1047 | __func__, dwc2_readl(hsotg, epctrl_reg), index, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1048 | hs_ep->dir_in ? "in" : "out"); |
| 1049 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1050 | /* If endpoint is stalled, we will restart request later */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1051 | ctrl = dwc2_readl(hsotg, epctrl_reg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1052 | |
Mian Yousaf Kaukab | b2d4c54 | 2015-09-29 12:08:22 +0200 | [diff] [blame] | 1053 | if (index && ctrl & DXEPCTL_STALL) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1054 | dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index); |
| 1055 | return; |
| 1056 | } |
| 1057 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1058 | length = ureq->length - ureq->actual; |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1059 | dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n", |
| 1060 | ureq->length, ureq->actual); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1061 | |
Vahram Aharonyan | cf77b5f | 2016-11-09 19:28:01 -0800 | [diff] [blame] | 1062 | if (!using_desc_dma(hsotg)) |
| 1063 | maxreq = get_ep_limit(hs_ep); |
| 1064 | else |
| 1065 | maxreq = dwc2_gadget_get_chain_limit(hs_ep); |
| 1066 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1067 | if (length > maxreq) { |
| 1068 | int round = maxreq % hs_ep->ep.maxpacket; |
| 1069 | |
| 1070 | dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n", |
| 1071 | __func__, length, maxreq, round); |
| 1072 | |
| 1073 | /* round down to multiple of packets */ |
| 1074 | if (round) |
| 1075 | maxreq -= round; |
| 1076 | |
| 1077 | length = maxreq; |
| 1078 | } |
| 1079 | |
| 1080 | if (length) |
| 1081 | packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket); |
| 1082 | else |
| 1083 | packets = 1; /* send one packet if length is zero. */ |
| 1084 | |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1085 | if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) { |
| 1086 | dev_err(hsotg->dev, "req length > maxpacket*mc\n"); |
| 1087 | return; |
| 1088 | } |
| 1089 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1090 | if (dir_in && index != 0) |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1091 | if (hs_ep->isochronous) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1092 | epsize = DXEPTSIZ_MC(packets); |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 1093 | else |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1094 | epsize = DXEPTSIZ_MC(1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1095 | else |
| 1096 | epsize = 0; |
| 1097 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1098 | /* |
| 1099 | * zero length packet should be programmed on its own and should not |
| 1100 | * be counted in DIEPTSIZ.PktCnt with other packets. |
| 1101 | */ |
| 1102 | if (dir_in && ureq->zero && !continuing) { |
| 1103 | /* Test if zlp is actually required. */ |
| 1104 | if ((ureq->length >= hs_ep->ep.maxpacket) && |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1105 | !(ureq->length % hs_ep->ep.maxpacket)) |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 1106 | hs_ep->send_zlp = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1109 | epsize |= DXEPTSIZ_PKTCNT(packets); |
| 1110 | epsize |= DXEPTSIZ_XFERSIZE(length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1111 | |
| 1112 | dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n", |
| 1113 | __func__, packets, length, ureq->length, epsize, epsize_reg); |
| 1114 | |
| 1115 | /* store the request as the current one we're doing */ |
| 1116 | hs_ep->req = hs_req; |
| 1117 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1118 | if (using_desc_dma(hsotg)) { |
| 1119 | u32 offset = 0; |
| 1120 | u32 mps = hs_ep->ep.maxpacket; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1121 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1122 | /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */ |
| 1123 | if (!dir_in) { |
| 1124 | if (!index) |
| 1125 | length = mps; |
| 1126 | else if (length % mps) |
| 1127 | length += (mps - (length % mps)); |
| 1128 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1129 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1130 | /* |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1131 | * If more data to send, adjust DMA for EP0 out data stage. |
| 1132 | * ureq->dma stays unchanged, hence increment it by already |
| 1133 | * passed passed data count before starting new transaction. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1134 | */ |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1135 | if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT && |
| 1136 | continuing) |
| 1137 | offset = ureq->actual; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1138 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1139 | /* Fill DDMA chain entries */ |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 1140 | dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq, offset, |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1141 | length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1142 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1143 | /* write descriptor chain address to control register */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1144 | dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg); |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1145 | |
| 1146 | dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n", |
| 1147 | __func__, (u32)hs_ep->desc_list_dma, dma_reg); |
| 1148 | } else { |
| 1149 | /* write size / packets */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1150 | dwc2_writel(hsotg, epsize, epsize_reg); |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1151 | |
Razmik Karapetyan | 729e657 | 2016-11-16 15:33:55 -0800 | [diff] [blame] | 1152 | if (using_dma(hsotg) && !continuing && (length != 0)) { |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1153 | /* |
| 1154 | * write DMA address to control register, buffer |
| 1155 | * already synced by dwc2_hsotg_ep_queue(). |
| 1156 | */ |
| 1157 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1158 | dwc2_writel(hsotg, ureq->dma, dma_reg); |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 1159 | |
| 1160 | dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n", |
| 1161 | __func__, &ureq->dma, dma_reg); |
| 1162 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1163 | } |
| 1164 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1165 | if (hs_ep->isochronous && hs_ep->interval == 1) { |
| 1166 | hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg); |
| 1167 | dwc2_gadget_incr_frame_num(hs_ep); |
| 1168 | |
| 1169 | if (hs_ep->target_frame & 0x1) |
| 1170 | ctrl |= DXEPCTL_SETODDFR; |
| 1171 | else |
| 1172 | ctrl |= DXEPCTL_SETEVENFR; |
| 1173 | } |
| 1174 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1175 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1176 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1177 | dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state); |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1178 | |
| 1179 | /* For Setup request do not clear NAK */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1180 | if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP)) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1181 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
Lukasz Majewski | 71225be | 2012-05-04 14:17:03 +0200 | [diff] [blame] | 1182 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1183 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1184 | dwc2_writel(hsotg, ctrl, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1185 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1186 | /* |
| 1187 | * set these, it seems that DMA support increments past the end |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1188 | * of the packet buffer so we need to calculate the length from |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1189 | * this information. |
| 1190 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1191 | hs_ep->size_loaded = length; |
| 1192 | hs_ep->last_load = ureq->actual; |
| 1193 | |
| 1194 | if (dir_in && !using_dma(hsotg)) { |
| 1195 | /* set these anyway, we may need them for non-periodic in */ |
| 1196 | hs_ep->fifo_load = 0; |
| 1197 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1198 | dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1199 | } |
| 1200 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1201 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1202 | * Note, trying to clear the NAK here causes problems with transmit |
| 1203 | * on the S3C6400 ending up with the TXFIFO becoming full. |
| 1204 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1205 | |
| 1206 | /* check ep is enabled */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1207 | if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA)) |
Mian Yousaf Kaukab | 1a0ed86 | 2015-01-09 13:39:00 +0100 | [diff] [blame] | 1208 | dev_dbg(hsotg->dev, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1209 | "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1210 | index, dwc2_readl(hsotg, epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1211 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1212 | dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1213 | __func__, dwc2_readl(hsotg, epctrl_reg)); |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 1214 | |
| 1215 | /* enable ep interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1216 | 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] | 1217 | } |
| 1218 | |
| 1219 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1220 | * 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] | 1221 | * @hsotg: The device state. |
| 1222 | * @hs_ep: The endpoint the request is on. |
| 1223 | * @req: The request being processed. |
| 1224 | * |
| 1225 | * We've been asked to queue a request, so ensure that the memory buffer |
| 1226 | * is correctly setup for DMA. If we've been passed an extant DMA address |
| 1227 | * then ensure the buffer has been synced to memory. If our buffer has no |
| 1228 | * DMA memory, then we map the memory and mark our request to allow us to |
| 1229 | * cleanup on completion. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1230 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1231 | static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1232 | struct dwc2_hsotg_ep *hs_ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1233 | struct usb_request *req) |
| 1234 | { |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 1235 | int ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1236 | |
Felipe Balbi | e58ebcd | 2013-01-28 14:48:36 +0200 | [diff] [blame] | 1237 | ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in); |
| 1238 | if (ret) |
| 1239 | goto dma_error; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1240 | |
| 1241 | return 0; |
| 1242 | |
| 1243 | dma_error: |
| 1244 | dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n", |
| 1245 | __func__, req->buf, req->length); |
| 1246 | |
| 1247 | return -EIO; |
| 1248 | } |
| 1249 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1250 | static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg, |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 1251 | struct dwc2_hsotg_ep *hs_ep, |
| 1252 | struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1253 | { |
| 1254 | void *req_buf = hs_req->req.buf; |
| 1255 | |
| 1256 | /* If dma is not being used or buffer is aligned */ |
| 1257 | if (!using_dma(hsotg) || !((long)req_buf & 3)) |
| 1258 | return 0; |
| 1259 | |
| 1260 | WARN_ON(hs_req->saved_req_buf); |
| 1261 | |
| 1262 | dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1263 | hs_ep->ep.name, req_buf, hs_req->req.length); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1264 | |
| 1265 | hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC); |
| 1266 | if (!hs_req->req.buf) { |
| 1267 | hs_req->req.buf = req_buf; |
| 1268 | dev_err(hsotg->dev, |
| 1269 | "%s: unable to allocate memory for bounce buffer\n", |
| 1270 | __func__); |
| 1271 | return -ENOMEM; |
| 1272 | } |
| 1273 | |
| 1274 | /* Save actual buffer */ |
| 1275 | hs_req->saved_req_buf = req_buf; |
| 1276 | |
| 1277 | if (hs_ep->dir_in) |
| 1278 | memcpy(hs_req->req.buf, req_buf, hs_req->req.length); |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 1282 | static void |
| 1283 | dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg, |
| 1284 | struct dwc2_hsotg_ep *hs_ep, |
| 1285 | struct dwc2_hsotg_req *hs_req) |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1286 | { |
| 1287 | /* If dma is not being used or buffer was aligned */ |
| 1288 | if (!using_dma(hsotg) || !hs_req->saved_req_buf) |
| 1289 | return; |
| 1290 | |
| 1291 | dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__, |
| 1292 | hs_ep->ep.name, hs_req->req.status, hs_req->req.actual); |
| 1293 | |
| 1294 | /* Copy data from bounce buffer on successful out transfer */ |
| 1295 | if (!hs_ep->dir_in && !hs_req->req.status) |
| 1296 | memcpy(hs_req->saved_req_buf, hs_req->req.buf, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1297 | hs_req->req.actual); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1298 | |
| 1299 | /* Free bounce buffer */ |
| 1300 | kfree(hs_req->req.buf); |
| 1301 | |
| 1302 | hs_req->req.buf = hs_req->saved_req_buf; |
| 1303 | hs_req->saved_req_buf = NULL; |
| 1304 | } |
| 1305 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 1306 | /** |
| 1307 | * dwc2_gadget_target_frame_elapsed - Checks target frame |
| 1308 | * @hs_ep: The driver endpoint to check |
| 1309 | * |
| 1310 | * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop |
| 1311 | * corresponding transfer. |
| 1312 | */ |
| 1313 | static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep) |
| 1314 | { |
| 1315 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 1316 | u32 target_frame = hs_ep->target_frame; |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 1317 | u32 current_frame = hsotg->frame_number; |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 1318 | bool frame_overrun = hs_ep->frame_overrun; |
| 1319 | |
| 1320 | if (!frame_overrun && current_frame >= target_frame) |
| 1321 | return true; |
| 1322 | |
| 1323 | if (frame_overrun && current_frame >= target_frame && |
| 1324 | ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2)) |
| 1325 | return true; |
| 1326 | |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1330 | /* |
| 1331 | * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers |
| 1332 | * @hsotg: The driver state |
| 1333 | * @hs_ep: the ep descriptor chain is for |
| 1334 | * |
| 1335 | * Called to update EP0 structure's pointers depend on stage of |
| 1336 | * control transfer. |
| 1337 | */ |
| 1338 | static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg, |
| 1339 | struct dwc2_hsotg_ep *hs_ep) |
| 1340 | { |
| 1341 | switch (hsotg->ep0_state) { |
| 1342 | case DWC2_EP0_SETUP: |
| 1343 | case DWC2_EP0_STATUS_OUT: |
| 1344 | hs_ep->desc_list = hsotg->setup_desc[0]; |
| 1345 | hs_ep->desc_list_dma = hsotg->setup_desc_dma[0]; |
| 1346 | break; |
| 1347 | case DWC2_EP0_DATA_IN: |
| 1348 | case DWC2_EP0_STATUS_IN: |
| 1349 | hs_ep->desc_list = hsotg->ctrl_in_desc; |
| 1350 | hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma; |
| 1351 | break; |
| 1352 | case DWC2_EP0_DATA_OUT: |
| 1353 | hs_ep->desc_list = hsotg->ctrl_out_desc; |
| 1354 | hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma; |
| 1355 | break; |
| 1356 | default: |
| 1357 | dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n", |
| 1358 | hsotg->ep0_state); |
| 1359 | return -EINVAL; |
| 1360 | } |
| 1361 | |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1365 | 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] | 1366 | gfp_t gfp_flags) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1367 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1368 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 1369 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1370 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1371 | bool first; |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 1372 | int ret; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1373 | u32 maxsize = 0; |
| 1374 | u32 mask = 0; |
| 1375 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1376 | |
| 1377 | dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n", |
| 1378 | ep->name, req, req->length, req->buf, req->no_interrupt, |
| 1379 | req->zero, req->short_not_ok); |
| 1380 | |
Gregory Herrero | 7ababa9 | 2015-04-29 22:09:08 +0200 | [diff] [blame] | 1381 | /* Prevent new request submission when controller is suspended */ |
Grigor Tovmasyan | 88b02f2 | 2018-01-24 17:44:25 +0400 | [diff] [blame] | 1382 | if (hs->lx_state != DWC2_L0) { |
| 1383 | dev_dbg(hs->dev, "%s: submit request only in active state\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1384 | __func__); |
Gregory Herrero | 7ababa9 | 2015-04-29 22:09:08 +0200 | [diff] [blame] | 1385 | return -EAGAIN; |
| 1386 | } |
| 1387 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1388 | /* initialise status of the request */ |
| 1389 | INIT_LIST_HEAD(&hs_req->queue); |
| 1390 | req->actual = 0; |
| 1391 | req->status = -EINPROGRESS; |
| 1392 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1393 | /* In DDMA mode for ISOC's don't queue request if length greater |
| 1394 | * than descriptor limits. |
| 1395 | */ |
| 1396 | if (using_desc_dma(hs) && hs_ep->isochronous) { |
| 1397 | maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask); |
| 1398 | if (hs_ep->dir_in && req->length > maxsize) { |
| 1399 | dev_err(hs->dev, "wrong length %d (maxsize=%d)\n", |
| 1400 | req->length, maxsize); |
| 1401 | return -EINVAL; |
| 1402 | } |
| 1403 | |
| 1404 | if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) { |
| 1405 | dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n", |
| 1406 | req->length, hs_ep->ep.maxpacket); |
| 1407 | return -EINVAL; |
| 1408 | } |
| 1409 | } |
| 1410 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1411 | 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] | 1412 | if (ret) |
| 1413 | return ret; |
| 1414 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1415 | /* if we're using DMA, sync the buffers as necessary */ |
| 1416 | if (using_dma(hs)) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1417 | ret = dwc2_hsotg_map_dma(hs, hs_ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1418 | if (ret) |
| 1419 | return ret; |
| 1420 | } |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 1421 | /* If using descriptor DMA configure EP0 descriptor chain pointers */ |
| 1422 | if (using_desc_dma(hs) && !hs_ep->index) { |
| 1423 | ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep); |
| 1424 | if (ret) |
| 1425 | return ret; |
| 1426 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1427 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1428 | first = list_empty(&hs_ep->queue); |
| 1429 | list_add_tail(&hs_req->queue, &hs_ep->queue); |
| 1430 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1431 | /* |
| 1432 | * Handle DDMA isochronous transfers separately - just add new entry |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1433 | * to the descriptor chain. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1434 | * Transfer will be started once SW gets either one of NAK or |
| 1435 | * OutTknEpDis interrupts. |
| 1436 | */ |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1437 | if (using_desc_dma(hs) && hs_ep->isochronous) { |
| 1438 | if (hs_ep->target_frame != TARGET_FRAME_INITIAL) { |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 1439 | dma_addr_t dma_addr = hs_req->req.dma; |
| 1440 | |
| 1441 | if (hs_req->req.num_sgs) { |
| 1442 | WARN_ON(hs_req->req.num_sgs > 1); |
| 1443 | dma_addr = sg_dma_address(hs_req->req.sg); |
| 1444 | } |
| 1445 | dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr, |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 1446 | hs_req->req.length); |
| 1447 | } |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 1448 | return 0; |
| 1449 | } |
| 1450 | |
Minas Harutyunyan | b4c53b4 | 2019-03-12 11:45:12 +0400 | [diff] [blame^] | 1451 | /* Change EP direction if status phase request is after data out */ |
| 1452 | if (!hs_ep->index && !req->length && !hs_ep->dir_in && |
| 1453 | hs->ep0_state == DWC2_EP0_DATA_OUT) |
| 1454 | hs_ep->dir_in = 1; |
| 1455 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1456 | if (first) { |
| 1457 | if (!hs_ep->isochronous) { |
| 1458 | dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); |
| 1459 | return 0; |
| 1460 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1461 | |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 1462 | /* Update current frame number value. */ |
| 1463 | hs->frame_number = dwc2_hsotg_read_frameno(hs); |
| 1464 | while (dwc2_gadget_target_frame_elapsed(hs_ep)) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1465 | dwc2_gadget_incr_frame_num(hs_ep); |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 1466 | /* Update current frame number value once more as it |
| 1467 | * changes here. |
| 1468 | */ |
| 1469 | hs->frame_number = dwc2_hsotg_read_frameno(hs); |
| 1470 | } |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 1471 | |
| 1472 | if (hs_ep->target_frame != TARGET_FRAME_INITIAL) |
| 1473 | dwc2_hsotg_start_req(hs, hs_ep, hs_req, false); |
| 1474 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1475 | return 0; |
| 1476 | } |
| 1477 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1478 | 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] | 1479 | gfp_t gfp_flags) |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1480 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1481 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1482 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1483 | unsigned long flags = 0; |
| 1484 | int ret = 0; |
| 1485 | |
| 1486 | spin_lock_irqsave(&hs->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1487 | ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 1488 | spin_unlock_irqrestore(&hs->lock, flags); |
| 1489 | |
| 1490 | return ret; |
| 1491 | } |
| 1492 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1493 | static void dwc2_hsotg_ep_free_request(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1494 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1495 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1496 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1497 | |
| 1498 | kfree(hs_req); |
| 1499 | } |
| 1500 | |
| 1501 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1502 | * dwc2_hsotg_complete_oursetup - setup completion callback |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1503 | * @ep: The endpoint the request was on. |
| 1504 | * @req: The request completed. |
| 1505 | * |
| 1506 | * Called on completion of any requests the driver itself |
| 1507 | * submitted that need cleaning up. |
| 1508 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1509 | static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1510 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1511 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1512 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1513 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1514 | |
| 1515 | dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req); |
| 1516 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1517 | dwc2_hsotg_ep_free_request(ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /** |
| 1521 | * ep_from_windex - convert control wIndex value to endpoint |
| 1522 | * @hsotg: The driver state. |
| 1523 | * @windex: The control request wIndex field (in host order). |
| 1524 | * |
| 1525 | * Convert the given wIndex into a pointer to an driver endpoint |
| 1526 | * structure, or return NULL if it is not a valid endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1527 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1528 | static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1529 | u32 windex) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1530 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1531 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1532 | int dir = (windex & USB_DIR_IN) ? 1 : 0; |
| 1533 | int idx = windex & 0x7F; |
| 1534 | |
| 1535 | if (windex >= 0x100) |
| 1536 | return NULL; |
| 1537 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 1538 | if (idx > hsotg->num_of_eps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1539 | return NULL; |
| 1540 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 1541 | ep = index_to_ep(hsotg, idx, dir); |
| 1542 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1543 | if (idx && ep->dir_in != dir) |
| 1544 | return NULL; |
| 1545 | |
| 1546 | return ep; |
| 1547 | } |
| 1548 | |
| 1549 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1550 | * dwc2_hsotg_set_test_mode - Enable usb Test Modes |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1551 | * @hsotg: The driver state. |
| 1552 | * @testmode: requested usb test mode |
| 1553 | * Enable usb Test Mode requested by the Host. |
| 1554 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1555 | int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode) |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1556 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1557 | int dctl = dwc2_readl(hsotg, DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1558 | |
| 1559 | dctl &= ~DCTL_TSTCTL_MASK; |
| 1560 | switch (testmode) { |
| 1561 | case TEST_J: |
| 1562 | case TEST_K: |
| 1563 | case TEST_SE0_NAK: |
| 1564 | case TEST_PACKET: |
| 1565 | case TEST_FORCE_EN: |
| 1566 | dctl |= testmode << DCTL_TSTCTL_SHIFT; |
| 1567 | break; |
| 1568 | default: |
| 1569 | return -EINVAL; |
| 1570 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1571 | dwc2_writel(hsotg, dctl, DCTL); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1576 | * dwc2_hsotg_send_reply - send reply to control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1577 | * @hsotg: The device state |
| 1578 | * @ep: Endpoint 0 |
| 1579 | * @buff: Buffer for request |
| 1580 | * @length: Length of reply. |
| 1581 | * |
| 1582 | * Create a request and queue it on the given endpoint. This is useful as |
| 1583 | * an internal method of sending replies to certain control requests, etc. |
| 1584 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1585 | static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1586 | struct dwc2_hsotg_ep *ep, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1587 | void *buff, |
| 1588 | int length) |
| 1589 | { |
| 1590 | struct usb_request *req; |
| 1591 | int ret; |
| 1592 | |
| 1593 | dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length); |
| 1594 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1595 | req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1596 | hsotg->ep0_reply = req; |
| 1597 | if (!req) { |
| 1598 | dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__); |
| 1599 | return -ENOMEM; |
| 1600 | } |
| 1601 | |
| 1602 | req->buf = hsotg->ep0_buff; |
| 1603 | req->length = length; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 1604 | /* |
| 1605 | * zero flag is for sending zlp in DATA IN stage. It has no impact on |
| 1606 | * STATUS stage. |
| 1607 | */ |
| 1608 | req->zero = 0; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1609 | req->complete = dwc2_hsotg_complete_oursetup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1610 | |
| 1611 | if (length) |
| 1612 | memcpy(req->buf, buff, length); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1613 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1614 | ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1615 | if (ret) { |
| 1616 | dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__); |
| 1617 | return ret; |
| 1618 | } |
| 1619 | |
| 1620 | return 0; |
| 1621 | } |
| 1622 | |
| 1623 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1624 | * dwc2_hsotg_process_req_status - process request GET_STATUS |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1625 | * @hsotg: The device state |
| 1626 | * @ctrl: USB control request |
| 1627 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1628 | static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1629 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1630 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1631 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 1632 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1633 | __le16 reply; |
| 1634 | int ret; |
| 1635 | |
| 1636 | dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__); |
| 1637 | |
| 1638 | if (!ep0->dir_in) { |
| 1639 | dev_warn(hsotg->dev, "%s: direction out?\n", __func__); |
| 1640 | return -EINVAL; |
| 1641 | } |
| 1642 | |
| 1643 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1644 | case USB_RECIP_DEVICE: |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 1645 | /* |
| 1646 | * bit 0 => self powered |
| 1647 | * bit 1 => remote wakeup |
| 1648 | */ |
| 1649 | reply = cpu_to_le16(0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1650 | break; |
| 1651 | |
| 1652 | case USB_RECIP_INTERFACE: |
| 1653 | /* currently, the data result should be zero */ |
| 1654 | reply = cpu_to_le16(0); |
| 1655 | break; |
| 1656 | |
| 1657 | case USB_RECIP_ENDPOINT: |
| 1658 | ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex)); |
| 1659 | if (!ep) |
| 1660 | return -ENOENT; |
| 1661 | |
| 1662 | reply = cpu_to_le16(ep->halted ? 1 : 0); |
| 1663 | break; |
| 1664 | |
| 1665 | default: |
| 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | if (le16_to_cpu(ctrl->wLength) != 2) |
| 1670 | return -EINVAL; |
| 1671 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1672 | ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1673 | if (ret) { |
| 1674 | dev_err(hsotg->dev, "%s: failed to send reply\n", __func__); |
| 1675 | return ret; |
| 1676 | } |
| 1677 | |
| 1678 | return 1; |
| 1679 | } |
| 1680 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1681 | 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] | 1682 | |
| 1683 | /** |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1684 | * get_ep_head - return the first request on the endpoint |
| 1685 | * @hs_ep: The controller endpoint to get |
| 1686 | * |
| 1687 | * Get the first request on the endpoint. |
| 1688 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1689 | 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] | 1690 | { |
Masahiro Yamada | ffc4b40 | 2016-09-19 01:03:13 +0900 | [diff] [blame] | 1691 | return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req, |
| 1692 | queue); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | /** |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1696 | * dwc2_gadget_start_next_request - Starts next request from ep queue |
| 1697 | * @hs_ep: Endpoint structure |
| 1698 | * |
| 1699 | * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked |
| 1700 | * in its handler. Hence we need to unmask it here to be able to do |
| 1701 | * resynchronization. |
| 1702 | */ |
| 1703 | static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep) |
| 1704 | { |
| 1705 | u32 mask; |
| 1706 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 1707 | int dir_in = hs_ep->dir_in; |
| 1708 | struct dwc2_hsotg_req *hs_req; |
| 1709 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 1710 | |
| 1711 | if (!list_empty(&hs_ep->queue)) { |
| 1712 | hs_req = get_ep_head(hs_ep); |
| 1713 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false); |
| 1714 | return; |
| 1715 | } |
| 1716 | if (!hs_ep->isochronous) |
| 1717 | return; |
| 1718 | |
| 1719 | if (dir_in) { |
| 1720 | dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n", |
| 1721 | __func__); |
| 1722 | } else { |
| 1723 | dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n", |
| 1724 | __func__); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1725 | mask = dwc2_readl(hsotg, epmsk_reg); |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1726 | mask |= DOEPMSK_OUTTKNEPDISMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1727 | dwc2_writel(hsotg, mask, epmsk_reg); |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1732 | * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1733 | * @hsotg: The device state |
| 1734 | * @ctrl: USB control request |
| 1735 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1736 | static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1737 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1738 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1739 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
| 1740 | struct dwc2_hsotg_req *hs_req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1741 | bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1742 | struct dwc2_hsotg_ep *ep; |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1743 | int ret; |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1744 | bool halted; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1745 | u32 recip; |
| 1746 | u32 wValue; |
| 1747 | u32 wIndex; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1748 | |
| 1749 | dev_dbg(hsotg->dev, "%s: %s_FEATURE\n", |
| 1750 | __func__, set ? "SET" : "CLEAR"); |
| 1751 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1752 | wValue = le16_to_cpu(ctrl->wValue); |
| 1753 | wIndex = le16_to_cpu(ctrl->wIndex); |
| 1754 | recip = ctrl->bRequestType & USB_RECIP_MASK; |
| 1755 | |
| 1756 | switch (recip) { |
| 1757 | case USB_RECIP_DEVICE: |
| 1758 | switch (wValue) { |
Vardan Mikayelyan | fa389a6 | 2018-02-16 14:08:53 +0400 | [diff] [blame] | 1759 | case USB_DEVICE_REMOTE_WAKEUP: |
| 1760 | hsotg->remote_wakeup_allowed = 1; |
| 1761 | break; |
| 1762 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1763 | case USB_DEVICE_TEST_MODE: |
| 1764 | if ((wIndex & 0xff) != 0) |
| 1765 | return -EINVAL; |
| 1766 | if (!set) |
| 1767 | return -EINVAL; |
| 1768 | |
| 1769 | hsotg->test_mode = wIndex >> 8; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1770 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1771 | if (ret) { |
| 1772 | dev_err(hsotg->dev, |
| 1773 | "%s: failed to send reply\n", __func__); |
| 1774 | return ret; |
| 1775 | } |
| 1776 | break; |
| 1777 | default: |
| 1778 | return -ENOENT; |
| 1779 | } |
| 1780 | break; |
| 1781 | |
| 1782 | case USB_RECIP_ENDPOINT: |
| 1783 | ep = ep_from_windex(hsotg, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1784 | if (!ep) { |
| 1785 | dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n", |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1786 | __func__, wIndex); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1787 | return -ENOENT; |
| 1788 | } |
| 1789 | |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1790 | switch (wValue) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1791 | case USB_ENDPOINT_HALT: |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1792 | halted = ep->halted; |
| 1793 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 1794 | dwc2_hsotg_ep_sethalt(&ep->ep, set, true); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1795 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1796 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Anton Tikhomirov | 26ab3d0 | 2011-04-21 17:06:40 +0900 | [diff] [blame] | 1797 | if (ret) { |
| 1798 | dev_err(hsotg->dev, |
| 1799 | "%s: failed to send reply\n", __func__); |
| 1800 | return ret; |
| 1801 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1802 | |
Robert Baldyga | bd9ef7b | 2013-09-19 11:50:22 +0200 | [diff] [blame] | 1803 | /* |
| 1804 | * we have to complete all requests for ep if it was |
| 1805 | * halted, and the halt was cleared by CLEAR_FEATURE |
| 1806 | */ |
| 1807 | |
| 1808 | if (!set && halted) { |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1809 | /* |
| 1810 | * If we have request in progress, |
| 1811 | * then complete it |
| 1812 | */ |
| 1813 | if (ep->req) { |
| 1814 | hs_req = ep->req; |
| 1815 | ep->req = NULL; |
| 1816 | list_del_init(&hs_req->queue); |
Gregory Herrero | c00dd4a | 2015-01-30 09:09:27 +0100 | [diff] [blame] | 1817 | if (hs_req->req.complete) { |
| 1818 | spin_unlock(&hsotg->lock); |
| 1819 | usb_gadget_giveback_request( |
| 1820 | &ep->ep, &hs_req->req); |
| 1821 | spin_lock(&hsotg->lock); |
| 1822 | } |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | /* If we have pending request, then start it */ |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 1826 | if (!ep->req) |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 1827 | dwc2_gadget_start_next_request(ep); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 1828 | } |
| 1829 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1830 | break; |
| 1831 | |
| 1832 | default: |
| 1833 | return -ENOENT; |
| 1834 | } |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 1835 | break; |
| 1836 | default: |
| 1837 | return -ENOENT; |
| 1838 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1839 | return 1; |
| 1840 | } |
| 1841 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1842 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg); |
Robert Baldyga | ab93e01 | 2013-09-19 11:50:17 +0200 | [diff] [blame] | 1843 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1844 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1845 | * dwc2_hsotg_stall_ep0 - stall ep0 |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1846 | * @hsotg: The device state |
| 1847 | * |
| 1848 | * Set stall for ep0 as response for setup request. |
| 1849 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1850 | static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg) |
Jingoo Han | e9ebe7c | 2014-06-03 22:14:56 +0900 | [diff] [blame] | 1851 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1852 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1853 | u32 reg; |
| 1854 | u32 ctrl; |
| 1855 | |
| 1856 | dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in); |
| 1857 | reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0; |
| 1858 | |
| 1859 | /* |
| 1860 | * DxEPCTL_Stall will be cleared by EP once it has |
| 1861 | * taken effect, so no need to clear later. |
| 1862 | */ |
| 1863 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1864 | ctrl = dwc2_readl(hsotg, reg); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1865 | ctrl |= DXEPCTL_STALL; |
| 1866 | ctrl |= DXEPCTL_CNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1867 | dwc2_writel(hsotg, ctrl, reg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1868 | |
| 1869 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1870 | "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1871 | ctrl, reg, dwc2_readl(hsotg, reg)); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1872 | |
| 1873 | /* |
| 1874 | * complete won't be called, so we enqueue |
| 1875 | * setup request here |
| 1876 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1877 | dwc2_hsotg_enqueue_setup(hsotg); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1881 | * dwc2_hsotg_process_control - process a control request |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1882 | * @hsotg: The device state |
| 1883 | * @ctrl: The control request received |
| 1884 | * |
| 1885 | * The controller has received the SETUP phase of a control request, and |
| 1886 | * needs to work out what to do next (and whether to pass it on to the |
| 1887 | * gadget driver). |
| 1888 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1889 | static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1890 | struct usb_ctrlrequest *ctrl) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1891 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1892 | struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0]; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1893 | int ret = 0; |
| 1894 | u32 dcfg; |
| 1895 | |
Mian Yousaf Kaukab | e525e74 | 2015-09-29 12:08:23 +0200 | [diff] [blame] | 1896 | dev_dbg(hsotg->dev, |
| 1897 | "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n", |
| 1898 | ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, |
| 1899 | ctrl->wIndex, ctrl->wLength); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1900 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1901 | if (ctrl->wLength == 0) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1902 | ep0->dir_in = 1; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 1903 | hsotg->ep0_state = DWC2_EP0_STATUS_IN; |
| 1904 | } else if (ctrl->bRequestType & USB_DIR_IN) { |
| 1905 | ep0->dir_in = 1; |
| 1906 | hsotg->ep0_state = DWC2_EP0_DATA_IN; |
| 1907 | } else { |
| 1908 | ep0->dir_in = 0; |
| 1909 | hsotg->ep0_state = DWC2_EP0_DATA_OUT; |
| 1910 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1911 | |
| 1912 | if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { |
| 1913 | switch (ctrl->bRequest) { |
| 1914 | case USB_REQ_SET_ADDRESS: |
Mian Yousaf Kaukab | 6d713c1 | 2015-01-09 13:39:10 +0100 | [diff] [blame] | 1915 | hsotg->connected = 1; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1916 | dcfg = dwc2_readl(hsotg, DCFG); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 1917 | dcfg &= ~DCFG_DEVADDR_MASK; |
Paul Zimmerman | d5dbd3f | 2014-04-25 14:18:13 -0700 | [diff] [blame] | 1918 | dcfg |= (le16_to_cpu(ctrl->wValue) << |
| 1919 | DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 1920 | dwc2_writel(hsotg, dcfg, DCFG); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1921 | |
| 1922 | dev_info(hsotg->dev, "new address %d\n", ctrl->wValue); |
| 1923 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1924 | ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1925 | return; |
| 1926 | |
| 1927 | case USB_REQ_GET_STATUS: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1928 | ret = dwc2_hsotg_process_req_status(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1929 | break; |
| 1930 | |
| 1931 | case USB_REQ_CLEAR_FEATURE: |
| 1932 | case USB_REQ_SET_FEATURE: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1933 | ret = dwc2_hsotg_process_req_feature(hsotg, ctrl); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1934 | break; |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | /* as a fallback, try delivering it to the driver to deal with */ |
| 1939 | |
| 1940 | if (ret == 0 && hsotg->driver) { |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1941 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1942 | ret = hsotg->driver->setup(&hsotg->gadget, ctrl); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1943 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1944 | if (ret < 0) |
| 1945 | dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret); |
| 1946 | } |
| 1947 | |
Minas Harutyunyan | b4c53b4 | 2019-03-12 11:45:12 +0400 | [diff] [blame^] | 1948 | hsotg->delayed_status = false; |
| 1949 | if (ret == USB_GADGET_DELAYED_STATUS) |
| 1950 | hsotg->delayed_status = true; |
| 1951 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 1952 | /* |
| 1953 | * the request is either unhandlable, or is not formatted correctly |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1954 | * so respond with a STALL for the status stage to indicate failure. |
| 1955 | */ |
| 1956 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 1957 | if (ret < 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1958 | dwc2_hsotg_stall_ep0(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1961 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1962 | * dwc2_hsotg_complete_setup - completion of a setup transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1963 | * @ep: The endpoint the request was on. |
| 1964 | * @req: The request completed. |
| 1965 | * |
| 1966 | * Called on completion of any requests the driver itself submitted for |
| 1967 | * EP0 setup packets |
| 1968 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1969 | static void dwc2_hsotg_complete_setup(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 1970 | struct usb_request *req) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1971 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1972 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 1973 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1974 | |
| 1975 | if (req->status < 0) { |
| 1976 | dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status); |
| 1977 | return; |
| 1978 | } |
| 1979 | |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1980 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1981 | if (req->actual == 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1982 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1983 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1984 | dwc2_hsotg_process_control(hsotg, req->buf); |
Robert Baldyga | 93f599f | 2013-11-21 13:49:17 +0100 | [diff] [blame] | 1985 | spin_unlock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1989 | * dwc2_hsotg_enqueue_setup - start a request for EP0 packets |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1990 | * @hsotg: The device state. |
| 1991 | * |
| 1992 | * Enqueue a request on EP0 if necessary to received any SETUP packets |
| 1993 | * received from the host. |
| 1994 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1995 | static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1996 | { |
| 1997 | struct usb_request *req = hsotg->ctrl_req; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 1998 | struct dwc2_hsotg_req *hs_req = our_req(req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 1999 | int ret; |
| 2000 | |
| 2001 | dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__); |
| 2002 | |
| 2003 | req->zero = 0; |
| 2004 | req->length = 8; |
| 2005 | req->buf = hsotg->ctrl_buff; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2006 | req->complete = dwc2_hsotg_complete_setup; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2007 | |
| 2008 | if (!list_empty(&hs_req->queue)) { |
| 2009 | dev_dbg(hsotg->dev, "%s already queued???\n", __func__); |
| 2010 | return; |
| 2011 | } |
| 2012 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2013 | hsotg->eps_out[0]->dir_in = 0; |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2014 | hsotg->eps_out[0]->send_zlp = 0; |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2015 | hsotg->ep0_state = DWC2_EP0_SETUP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2016 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2017 | 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] | 2018 | if (ret < 0) { |
| 2019 | dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret); |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2020 | /* |
| 2021 | * Don't think there's much we can do other than watch the |
| 2022 | * driver fail. |
| 2023 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2024 | } |
| 2025 | } |
| 2026 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2027 | static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2028 | struct dwc2_hsotg_ep *hs_ep) |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2029 | { |
| 2030 | u32 ctrl; |
| 2031 | u8 index = hs_ep->index; |
| 2032 | u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index); |
| 2033 | u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index); |
| 2034 | |
Mian Yousaf Kaukab | ccb34a9 | 2015-01-30 09:09:34 +0100 | [diff] [blame] | 2035 | if (hs_ep->dir_in) |
| 2036 | dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 2037 | index); |
Mian Yousaf Kaukab | ccb34a9 | 2015-01-30 09:09:34 +0100 | [diff] [blame] | 2038 | else |
| 2039 | dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n", |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 2040 | index); |
| 2041 | if (using_desc_dma(hsotg)) { |
Minas Harutyunyan | 201ec56 | 2018-01-16 16:03:32 +0400 | [diff] [blame] | 2042 | if (!index) |
| 2043 | dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep); |
| 2044 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 2045 | /* Not specific buffer needed for ep0 ZLP */ |
| 2046 | dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &hs_ep->desc_list, |
| 2047 | hs_ep->desc_list_dma, 0, true); |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 2048 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2049 | dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
| 2050 | DXEPTSIZ_XFERSIZE(0), |
Vahram Aharonyan | e02f9aa | 2016-11-14 19:16:24 -0800 | [diff] [blame] | 2051 | epsiz_reg); |
| 2052 | } |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2053 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2054 | ctrl = dwc2_readl(hsotg, epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2055 | ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */ |
| 2056 | ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */ |
| 2057 | ctrl |= DXEPCTL_USBACTEP; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2058 | dwc2_writel(hsotg, ctrl, epctl_reg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2059 | } |
| 2060 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2061 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2062 | * dwc2_hsotg_complete_request - complete a request given to us |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2063 | * @hsotg: The device state. |
| 2064 | * @hs_ep: The endpoint the request was on. |
| 2065 | * @hs_req: The request to complete. |
| 2066 | * @result: The result code (0 => Ok, otherwise errno) |
| 2067 | * |
| 2068 | * The given request has finished, so call the necessary completion |
| 2069 | * if it has one and then look to see if we can start a new request |
| 2070 | * on the endpoint. |
| 2071 | * |
| 2072 | * Note, expects the ep to already be locked as appropriate. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2073 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2074 | static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2075 | struct dwc2_hsotg_ep *hs_ep, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2076 | struct dwc2_hsotg_req *hs_req, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2077 | int result) |
| 2078 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2079 | if (!hs_req) { |
| 2080 | dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__); |
| 2081 | return; |
| 2082 | } |
| 2083 | |
| 2084 | dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n", |
| 2085 | hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete); |
| 2086 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2087 | /* |
| 2088 | * only replace the status if we've not already set an error |
| 2089 | * from a previous transaction |
| 2090 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2091 | |
| 2092 | if (hs_req->req.status == -EINPROGRESS) |
| 2093 | hs_req->req.status = result; |
| 2094 | |
Yunzhi Li | 44583fe | 2015-09-29 12:25:01 +0200 | [diff] [blame] | 2095 | if (using_dma(hsotg)) |
| 2096 | dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req); |
| 2097 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2098 | dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req); |
Mian Yousaf Kaukab | 7d24c1b | 2015-01-30 09:09:31 +0100 | [diff] [blame] | 2099 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2100 | hs_ep->req = NULL; |
| 2101 | list_del_init(&hs_req->queue); |
| 2102 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2103 | /* |
| 2104 | * call the complete request with the locks off, just in case the |
| 2105 | * request tries to queue more work for this endpoint. |
| 2106 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2107 | |
| 2108 | if (hs_req->req.complete) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2109 | spin_unlock(&hsotg->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 2110 | usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 2111 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2112 | } |
| 2113 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2114 | /* In DDMA don't need to proceed to starting of next ISOC request */ |
| 2115 | if (using_desc_dma(hsotg) && hs_ep->isochronous) |
| 2116 | return; |
| 2117 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2118 | /* |
| 2119 | * 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] | 2120 | * 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] | 2121 | * so be careful when doing this. |
| 2122 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2123 | |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 2124 | if (!hs_ep->req && result >= 0) |
Vardan Mikayelyan | 41cc4cd | 2016-05-25 18:07:12 -0700 | [diff] [blame] | 2125 | dwc2_gadget_start_next_request(hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2126 | } |
| 2127 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2128 | /* |
| 2129 | * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA |
| 2130 | * @hs_ep: The endpoint the request was on. |
| 2131 | * |
| 2132 | * Get first request from the ep queue, determine descriptor on which complete |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2133 | * happened. SW discovers which descriptor currently in use by HW, adjusts |
| 2134 | * dma_address and calculates index of completed descriptor based on the value |
| 2135 | * of DEPDMA register. Update actual length of request, giveback to gadget. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2136 | */ |
| 2137 | static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 2138 | { |
| 2139 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2140 | struct dwc2_hsotg_req *hs_req; |
| 2141 | struct usb_request *ureq; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2142 | u32 desc_sts; |
| 2143 | u32 mask; |
| 2144 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2145 | desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status; |
| 2146 | |
| 2147 | /* Process only descriptors with buffer status set to DMA done */ |
| 2148 | while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >> |
| 2149 | DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) { |
| 2150 | |
| 2151 | hs_req = get_ep_head(hs_ep); |
| 2152 | if (!hs_req) { |
| 2153 | dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__); |
| 2154 | return; |
| 2155 | } |
| 2156 | ureq = &hs_req->req; |
| 2157 | |
| 2158 | /* Check completion status */ |
| 2159 | if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT == |
| 2160 | DEV_DMA_STS_SUCC) { |
| 2161 | mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK : |
| 2162 | DEV_DMA_ISOC_RX_NBYTES_MASK; |
| 2163 | ureq->actual = ureq->length - ((desc_sts & mask) >> |
| 2164 | DEV_DMA_ISOC_NBYTES_SHIFT); |
| 2165 | |
| 2166 | /* Adjust actual len for ISOC Out if len is |
| 2167 | * not align of 4 |
| 2168 | */ |
| 2169 | if (!hs_ep->dir_in && ureq->length & 0x3) |
| 2170 | ureq->actual += 4 - (ureq->length & 0x3); |
| 2171 | } |
| 2172 | |
| 2173 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
| 2174 | |
| 2175 | hs_ep->compl_desc++; |
| 2176 | if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_GENERIC - 1)) |
| 2177 | hs_ep->compl_desc = 0; |
| 2178 | desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2179 | } |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | /* |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2183 | * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC. |
| 2184 | * @hs_ep: The isochronous endpoint. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2185 | * |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2186 | * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA |
| 2187 | * interrupt. Reset target frame and next_desc to allow to start |
| 2188 | * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS |
| 2189 | * interrupt for OUT direction. |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2190 | */ |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2191 | 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] | 2192 | { |
| 2193 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2194 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2195 | if (!hs_ep->dir_in) |
| 2196 | dwc2_flush_rx_fifo(hsotg); |
| 2197 | 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] | 2198 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2199 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
| 2200 | hs_ep->next_desc = 0; |
| 2201 | hs_ep->compl_desc = 0; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2202 | } |
| 2203 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2204 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2205 | * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2206 | * @hsotg: The device state. |
| 2207 | * @ep_idx: The endpoint index for the data |
| 2208 | * @size: The size of data in the fifo, in bytes |
| 2209 | * |
| 2210 | * The FIFO status shows there is data to read from the FIFO for a given |
| 2211 | * endpoint, so sort out whether we need to read the data into a request |
| 2212 | * that has been made for that endpoint. |
| 2213 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2214 | 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] | 2215 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2216 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx]; |
| 2217 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2218 | int to_read; |
| 2219 | int max_req; |
| 2220 | int read_ptr; |
| 2221 | |
| 2222 | if (!hs_req) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2223 | u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2224 | int ptr; |
| 2225 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 2226 | dev_dbg(hsotg->dev, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2227 | "%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] | 2228 | __func__, size, ep_idx, epctl); |
| 2229 | |
| 2230 | /* dump the data from the FIFO, we've nothing we can do */ |
| 2231 | for (ptr = 0; ptr < size; ptr += 4) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2232 | (void)dwc2_readl(hsotg, EPFIFO(ep_idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2233 | |
| 2234 | return; |
| 2235 | } |
| 2236 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2237 | to_read = size; |
| 2238 | read_ptr = hs_req->req.actual; |
| 2239 | max_req = hs_req->req.length - read_ptr; |
| 2240 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2241 | dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n", |
| 2242 | __func__, to_read, max_req, read_ptr, hs_req->req.length); |
| 2243 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2244 | if (to_read > max_req) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2245 | /* |
| 2246 | * more data appeared than we where willing |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2247 | * to deal with in this request. |
| 2248 | */ |
| 2249 | |
| 2250 | /* currently we don't deal this */ |
| 2251 | WARN_ON_ONCE(1); |
| 2252 | } |
| 2253 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2254 | hs_ep->total_data += to_read; |
| 2255 | hs_req->req.actual += to_read; |
| 2256 | to_read = DIV_ROUND_UP(to_read, 4); |
| 2257 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2258 | /* |
| 2259 | * note, we might over-write the buffer end by 3 bytes depending on |
| 2260 | * alignment of the data. |
| 2261 | */ |
Gevorg Sahakyan | 342ccce | 2018-07-26 18:00:41 +0400 | [diff] [blame] | 2262 | dwc2_readl_rep(hsotg, EPFIFO(ep_idx), |
| 2263 | hs_req->req.buf + read_ptr, to_read); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2267 | * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2268 | * @hsotg: The device instance |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2269 | * @dir_in: If IN zlp |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2270 | * |
| 2271 | * Generate a zero-length IN packet request for terminating a SETUP |
| 2272 | * transaction. |
| 2273 | * |
| 2274 | * 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] | 2275 | * 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] | 2276 | * the TxFIFO. |
| 2277 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2278 | 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] | 2279 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2280 | /* eps_out[0] is used in both directions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2281 | hsotg->eps_out[0]->dir_in = dir_in; |
| 2282 | 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] | 2283 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2284 | dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2285 | } |
| 2286 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2287 | static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2288 | u32 epctl_reg) |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2289 | { |
| 2290 | u32 ctrl; |
| 2291 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2292 | ctrl = dwc2_readl(hsotg, epctl_reg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2293 | if (ctrl & DXEPCTL_EOFRNUM) |
| 2294 | ctrl |= DXEPCTL_SETEVENFR; |
| 2295 | else |
| 2296 | ctrl |= DXEPCTL_SETODDFR; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2297 | dwc2_writel(hsotg, ctrl, epctl_reg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2298 | } |
| 2299 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2300 | /* |
| 2301 | * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc |
| 2302 | * @hs_ep - The endpoint on which transfer went |
| 2303 | * |
| 2304 | * Iterate over endpoints descriptor chain and get info on bytes remained |
| 2305 | * in DMA descriptors after transfer has completed. Used for non isoc EPs. |
| 2306 | */ |
| 2307 | static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep) |
| 2308 | { |
| 2309 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2310 | unsigned int bytes_rem = 0; |
| 2311 | struct dwc2_dma_desc *desc = hs_ep->desc_list; |
| 2312 | int i; |
| 2313 | u32 status; |
| 2314 | |
| 2315 | if (!desc) |
| 2316 | return -EINVAL; |
| 2317 | |
| 2318 | for (i = 0; i < hs_ep->desc_count; ++i) { |
| 2319 | status = desc->status; |
| 2320 | bytes_rem += status & DEV_DMA_NBYTES_MASK; |
| 2321 | |
| 2322 | if (status & DEV_DMA_STS_MASK) |
| 2323 | dev_err(hsotg->dev, "descriptor %d closed with %x\n", |
| 2324 | i, status & DEV_DMA_STS_MASK); |
| 2325 | } |
| 2326 | |
| 2327 | return bytes_rem; |
| 2328 | } |
| 2329 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2330 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2331 | * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2332 | * @hsotg: The device instance |
| 2333 | * @epnum: The endpoint received from |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2334 | * |
| 2335 | * The RXFIFO has delivered an OutDone event, which means that the data |
| 2336 | * transfer for an OUT endpoint has been completed, either by a short |
| 2337 | * packet or by the finish of a transfer. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2338 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2339 | static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2340 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2341 | u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum)); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2342 | struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum]; |
| 2343 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2344 | struct usb_request *req = &hs_req->req; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2345 | unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2346 | int result = 0; |
| 2347 | |
| 2348 | if (!hs_req) { |
| 2349 | dev_dbg(hsotg->dev, "%s: no request active\n", __func__); |
| 2350 | return; |
| 2351 | } |
| 2352 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2353 | if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) { |
| 2354 | dev_dbg(hsotg->dev, "zlp packet received\n"); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2355 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
| 2356 | dwc2_hsotg_enqueue_setup(hsotg); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2357 | return; |
| 2358 | } |
| 2359 | |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2360 | if (using_desc_dma(hsotg)) |
| 2361 | size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); |
| 2362 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2363 | if (using_dma(hsotg)) { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2364 | unsigned int size_done; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2365 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2366 | /* |
| 2367 | * Calculate the size of the transfer by checking how much |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2368 | * is left in the endpoint size register and then working it |
| 2369 | * out from the amount we loaded for the transfer. |
| 2370 | * |
| 2371 | * We need to do this as DMA pointers are always 32bit aligned |
| 2372 | * so may overshoot/undershoot the transfer. |
| 2373 | */ |
| 2374 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2375 | size_done = hs_ep->size_loaded - size_left; |
| 2376 | size_done += hs_ep->last_load; |
| 2377 | |
| 2378 | req->actual = size_done; |
| 2379 | } |
| 2380 | |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2381 | /* if there is more request to do, schedule new transfer */ |
| 2382 | if (req->actual < req->length && size_left == 0) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2383 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Ben Dooks | a33e713 | 2010-07-19 09:40:49 +0100 | [diff] [blame] | 2384 | return; |
| 2385 | } |
| 2386 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2387 | if (req->actual < req->length && req->short_not_ok) { |
| 2388 | dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n", |
| 2389 | __func__, req->actual, req->length); |
| 2390 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2391 | /* |
| 2392 | * todo - what should we return here? there's no one else |
| 2393 | * even bothering to check the status. |
| 2394 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2395 | } |
| 2396 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 2397 | /* DDMA IN status phase will start from StsPhseRcvd interrupt */ |
| 2398 | if (!using_desc_dma(hsotg) && epnum == 0 && |
| 2399 | hsotg->ep0_state == DWC2_EP0_DATA_OUT) { |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2400 | /* Move to STATUS IN */ |
Minas Harutyunyan | b4c53b4 | 2019-03-12 11:45:12 +0400 | [diff] [blame^] | 2401 | if (!hsotg->delayed_status) |
| 2402 | dwc2_hsotg_ep0_zlp(hsotg, true); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2405 | /* |
| 2406 | * Slave mode OUT transfers do not go through XferComplete so |
| 2407 | * adjust the ISOC parity here. |
| 2408 | */ |
| 2409 | if (!using_dma(hsotg)) { |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2410 | if (hs_ep->isochronous && hs_ep->interval == 1) |
| 2411 | dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2412 | else if (hs_ep->isochronous && hs_ep->interval > 1) |
| 2413 | dwc2_gadget_incr_frame_num(hs_ep); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 2414 | } |
| 2415 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2416 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2420 | * dwc2_hsotg_handle_rx - RX FIFO has data |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2421 | * @hsotg: The device instance |
| 2422 | * |
| 2423 | * The IRQ handler has detected that the RX FIFO has some data in it |
| 2424 | * that requires processing, so find out what is in there and do the |
| 2425 | * appropriate read. |
| 2426 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2427 | * 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] | 2428 | * chunks, so if you have x packets received on an endpoint you'll get x |
| 2429 | * FIFO events delivered, each with a packet's worth of data in it. |
| 2430 | * |
| 2431 | * When using DMA, we should not be processing events from the RXFIFO |
| 2432 | * as the actual data should be sent to the memory directly and we turn |
| 2433 | * on the completion interrupts to get notifications of transfer completion. |
| 2434 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2435 | static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2436 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2437 | u32 grxstsr = dwc2_readl(hsotg, GRXSTSP); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2438 | u32 epnum, status, size; |
| 2439 | |
| 2440 | WARN_ON(using_dma(hsotg)); |
| 2441 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2442 | epnum = grxstsr & GRXSTS_EPNUM_MASK; |
| 2443 | status = grxstsr & GRXSTS_PKTSTS_MASK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2444 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2445 | size = grxstsr & GRXSTS_BYTECNT_MASK; |
| 2446 | size >>= GRXSTS_BYTECNT_SHIFT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2447 | |
Mian Yousaf Kaukab | d7c747c | 2015-01-30 09:09:30 +0100 | [diff] [blame] | 2448 | dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2449 | __func__, grxstsr, size, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2450 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2451 | switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) { |
| 2452 | case GRXSTS_PKTSTS_GLOBALOUTNAK: |
| 2453 | dev_dbg(hsotg->dev, "GLOBALOUTNAK\n"); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2454 | break; |
| 2455 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2456 | case GRXSTS_PKTSTS_OUTDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2457 | dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2458 | dwc2_hsotg_read_frameno(hsotg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2459 | |
| 2460 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2461 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2462 | break; |
| 2463 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2464 | case GRXSTS_PKTSTS_SETUPDONE: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2465 | dev_dbg(hsotg->dev, |
| 2466 | "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2467 | dwc2_hsotg_read_frameno(hsotg), |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2468 | dwc2_readl(hsotg, DOEPCTL(0))); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2469 | /* |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2470 | * 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] | 2471 | * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't |
| 2472 | * generate GRXSTS_PKTSTS_OUTDONE for setup packet. |
| 2473 | */ |
| 2474 | if (hsotg->ep0_state == DWC2_EP0_SETUP) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2475 | dwc2_hsotg_handle_outdone(hsotg, epnum); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2476 | break; |
| 2477 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2478 | case GRXSTS_PKTSTS_OUTRX: |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2479 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2480 | break; |
| 2481 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2482 | case GRXSTS_PKTSTS_SETUPRX: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2483 | dev_dbg(hsotg->dev, |
| 2484 | "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n", |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2485 | dwc2_hsotg_read_frameno(hsotg), |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2486 | dwc2_readl(hsotg, DOEPCTL(0))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2487 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2488 | WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP); |
| 2489 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2490 | dwc2_hsotg_rx_data(hsotg, epnum, size); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2491 | break; |
| 2492 | |
| 2493 | default: |
| 2494 | dev_warn(hsotg->dev, "%s: unknown status %08x\n", |
| 2495 | __func__, grxstsr); |
| 2496 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2497 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2498 | break; |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2503 | * dwc2_hsotg_ep0_mps - turn max packet size into register setting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2504 | * @mps: The maximum packet size in bytes. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2505 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2506 | static u32 dwc2_hsotg_ep0_mps(unsigned int mps) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2507 | { |
| 2508 | switch (mps) { |
| 2509 | case 64: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2510 | return D0EPCTL_MPS_64; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2511 | case 32: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2512 | return D0EPCTL_MPS_32; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2513 | case 16: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2514 | return D0EPCTL_MPS_16; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2515 | case 8: |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 2516 | return D0EPCTL_MPS_8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | /* bad max packet size, warn and return invalid result */ |
| 2520 | WARN_ON(1); |
| 2521 | return (u32)-1; |
| 2522 | } |
| 2523 | |
| 2524 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2525 | * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2526 | * @hsotg: The driver state. |
| 2527 | * @ep: The index number of the endpoint |
| 2528 | * @mps: The maximum packet size in bytes |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2529 | * @mc: The multicount value |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 2530 | * @dir_in: True if direction is in. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2531 | * |
| 2532 | * Configure the maximum packet size for the given endpoint, updating |
| 2533 | * the hardware control registers to reflect this. |
| 2534 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2535 | static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg, |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2536 | unsigned int ep, unsigned int mps, |
| 2537 | unsigned int mc, unsigned int dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2538 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2539 | struct dwc2_hsotg_ep *hs_ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2540 | u32 reg; |
| 2541 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2542 | hs_ep = index_to_ep(hsotg, ep, dir_in); |
| 2543 | if (!hs_ep) |
| 2544 | return; |
| 2545 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2546 | if (ep == 0) { |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2547 | u32 mps_bytes = mps; |
| 2548 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2549 | /* EP0 is a special case */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2550 | mps = dwc2_hsotg_ep0_mps(mps_bytes); |
| 2551 | if (mps > 3) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2552 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2553 | hs_ep->ep.maxpacket = mps_bytes; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 2554 | hs_ep->mc = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2555 | } else { |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2556 | if (mps > 1024) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2557 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2558 | hs_ep->mc = mc; |
| 2559 | if (mc > 3) |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 2560 | goto bad_mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2561 | hs_ep->ep.maxpacket = mps; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2562 | } |
| 2563 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2564 | if (dir_in) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2565 | reg = dwc2_readl(hsotg, DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2566 | reg &= ~DXEPCTL_MPS_MASK; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2567 | reg |= mps; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2568 | dwc2_writel(hsotg, reg, DIEPCTL(ep)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2569 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2570 | reg = dwc2_readl(hsotg, DOEPCTL(ep)); |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2571 | reg &= ~DXEPCTL_MPS_MASK; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 2572 | reg |= mps; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2573 | dwc2_writel(hsotg, reg, DOEPCTL(ep)); |
Anton Tikhomirov | 659ad60 | 2012-03-06 14:07:29 +0900 | [diff] [blame] | 2574 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2575 | |
| 2576 | return; |
| 2577 | |
| 2578 | bad_mps: |
| 2579 | dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps); |
| 2580 | } |
| 2581 | |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2582 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2583 | * dwc2_hsotg_txfifo_flush - flush Tx FIFO |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2584 | * @hsotg: The driver state |
| 2585 | * @idx: The index for the endpoint (0..15) |
| 2586 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2587 | 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] | 2588 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2589 | dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH, |
| 2590 | GRSTCTL); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2591 | |
| 2592 | /* wait until the fifo is flushed */ |
Sevak Arakelyan | 79d6b8c | 2018-01-19 14:39:31 +0400 | [diff] [blame] | 2593 | if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100)) |
| 2594 | dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n", |
| 2595 | __func__); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 2596 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2597 | |
| 2598 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2599 | * dwc2_hsotg_trytx - check to see if anything needs transmitting |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2600 | * @hsotg: The driver state |
| 2601 | * @hs_ep: The driver endpoint to check. |
| 2602 | * |
| 2603 | * Check to see if there is a request that has data to send, and if so |
| 2604 | * make an attempt to write data into the FIFO. |
| 2605 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2606 | static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2607 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2608 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2609 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2610 | |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 2611 | if (!hs_ep->dir_in || !hs_req) { |
| 2612 | /** |
| 2613 | * if request is not enqueued, we disable interrupts |
| 2614 | * for endpoints, excepting ep0 |
| 2615 | */ |
| 2616 | if (hs_ep->index != 0) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2617 | dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2618 | hs_ep->dir_in, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2619 | return 0; |
Robert Baldyga | afcf416 | 2013-09-19 11:50:19 +0200 | [diff] [blame] | 2620 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2621 | |
| 2622 | if (hs_req->req.actual < hs_req->req.length) { |
| 2623 | dev_dbg(hsotg->dev, "trying to write more for ep%d\n", |
| 2624 | hs_ep->index); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2625 | return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
| 2631 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2632 | * dwc2_hsotg_complete_in - complete IN transfer |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2633 | * @hsotg: The device state. |
| 2634 | * @hs_ep: The endpoint that has just completed. |
| 2635 | * |
| 2636 | * An IN transfer has been completed, update the transfer's state and then |
| 2637 | * call the relevant completion routines. |
| 2638 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2639 | static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2640 | struct dwc2_hsotg_ep *hs_ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2641 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2642 | struct dwc2_hsotg_req *hs_req = hs_ep->req; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2643 | u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2644 | int size_left, size_done; |
| 2645 | |
| 2646 | if (!hs_req) { |
| 2647 | dev_dbg(hsotg->dev, "XferCompl but no req\n"); |
| 2648 | return; |
| 2649 | } |
| 2650 | |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2651 | /* Finish ZLP handling for IN EP0 transactions */ |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2652 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) { |
| 2653 | dev_dbg(hsotg->dev, "zlp packet sent\n"); |
Razmik Karapetyan | c3b22fe | 2016-11-16 15:33:57 -0800 | [diff] [blame] | 2654 | |
| 2655 | /* |
| 2656 | * While send zlp for DWC2_EP0_STATUS_IN EP direction was |
| 2657 | * changed to IN. Change back to complete OUT transfer request |
| 2658 | */ |
| 2659 | hs_ep->dir_in = 0; |
| 2660 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2661 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2662 | if (hsotg->test_mode) { |
| 2663 | int ret; |
| 2664 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2665 | ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2666 | if (ret < 0) { |
| 2667 | dev_dbg(hsotg->dev, "Invalid Test #%d\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2668 | hsotg->test_mode); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2669 | dwc2_hsotg_stall_ep0(hsotg); |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 2670 | return; |
| 2671 | } |
| 2672 | } |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2673 | dwc2_hsotg_enqueue_setup(hsotg); |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2674 | return; |
| 2675 | } |
| 2676 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2677 | /* |
| 2678 | * Calculate the size of the transfer by checking how much is left |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2679 | * in the endpoint size register and then working it out from |
| 2680 | * the amount we loaded for the transfer. |
| 2681 | * |
| 2682 | * We do this even for DMA, as the transfer may have incremented |
| 2683 | * past the end of the buffer (DMA transfers are always 32bit |
| 2684 | * aligned). |
| 2685 | */ |
Vahram Aharonyan | aa3e8bc | 2016-11-14 19:16:26 -0800 | [diff] [blame] | 2686 | if (using_desc_dma(hsotg)) { |
| 2687 | size_left = dwc2_gadget_get_xfersize_ddma(hs_ep); |
| 2688 | if (size_left < 0) |
| 2689 | dev_err(hsotg->dev, "error parsing DDMA results %d\n", |
| 2690 | size_left); |
| 2691 | } else { |
| 2692 | size_left = DXEPTSIZ_XFERSIZE_GET(epsize); |
| 2693 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2694 | |
| 2695 | size_done = hs_ep->size_loaded - size_left; |
| 2696 | size_done += hs_ep->last_load; |
| 2697 | |
| 2698 | if (hs_req->req.actual != size_done) |
| 2699 | dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n", |
| 2700 | __func__, hs_req->req.actual, size_done); |
| 2701 | |
| 2702 | hs_req->req.actual = size_done; |
Lukasz Majewski | d3ca025 | 2012-05-04 14:17:04 +0200 | [diff] [blame] | 2703 | dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n", |
| 2704 | hs_req->req.length, hs_req->req.actual, hs_req->req.zero); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2705 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2706 | if (!size_left && hs_req->req.actual < hs_req->req.length) { |
| 2707 | dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2708 | dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2709 | return; |
| 2710 | } |
| 2711 | |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 2712 | /* Zlp for all endpoints, for ep0 only in DATA IN stage */ |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2713 | if (hs_ep->send_zlp) { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2714 | dwc2_hsotg_program_zlp(hsotg, hs_ep); |
Mian Yousaf Kaukab | 8a20fa4 | 2015-01-09 13:39:03 +0100 | [diff] [blame] | 2715 | hs_ep->send_zlp = 0; |
Mian Yousaf Kaukab | f71b5e2 | 2015-01-09 13:38:59 +0100 | [diff] [blame] | 2716 | /* transfer will be completed on next complete interrupt */ |
| 2717 | return; |
| 2718 | } |
| 2719 | |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2720 | if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) { |
| 2721 | /* Move to STATUS OUT */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2722 | dwc2_hsotg_ep0_zlp(hsotg, false); |
Mian Yousaf Kaukab | fe0b94a | 2015-01-09 13:38:58 +0100 | [diff] [blame] | 2723 | return; |
| 2724 | } |
| 2725 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2726 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | /** |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2730 | * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep |
| 2731 | * @hsotg: The device state. |
| 2732 | * @idx: Index of ep. |
| 2733 | * @dir_in: Endpoint direction 1-in 0-out. |
| 2734 | * |
| 2735 | * Reads for endpoint with given index and direction, by masking |
| 2736 | * epint_reg with coresponding mask. |
| 2737 | */ |
| 2738 | static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg, |
| 2739 | unsigned int idx, int dir_in) |
| 2740 | { |
| 2741 | u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK; |
| 2742 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 2743 | u32 ints; |
| 2744 | u32 mask; |
| 2745 | u32 diepempmsk; |
| 2746 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2747 | mask = dwc2_readl(hsotg, epmsk_reg); |
| 2748 | diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK); |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2749 | mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0; |
| 2750 | mask |= DXEPINT_SETUP_RCVD; |
| 2751 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2752 | ints = dwc2_readl(hsotg, epint_reg); |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2753 | ints &= mask; |
| 2754 | return ints; |
| 2755 | } |
| 2756 | |
| 2757 | /** |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2758 | * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD |
| 2759 | * @hs_ep: The endpoint on which interrupt is asserted. |
| 2760 | * |
| 2761 | * This interrupt indicates that the endpoint has been disabled per the |
| 2762 | * application's request. |
| 2763 | * |
| 2764 | * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK, |
| 2765 | * in case of ISOC completes current request. |
| 2766 | * |
| 2767 | * For ISOC-OUT endpoints completes expired requests. If there is remaining |
| 2768 | * request starts it. |
| 2769 | */ |
| 2770 | static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep) |
| 2771 | { |
| 2772 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2773 | struct dwc2_hsotg_req *hs_req; |
| 2774 | unsigned char idx = hs_ep->index; |
| 2775 | int dir_in = hs_ep->dir_in; |
| 2776 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2777 | int dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2778 | |
| 2779 | dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__); |
| 2780 | |
| 2781 | if (dir_in) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2782 | int epctl = dwc2_readl(hsotg, epctl_reg); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2783 | |
| 2784 | dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index); |
| 2785 | |
| 2786 | if (hs_ep->isochronous) { |
| 2787 | dwc2_hsotg_complete_in(hsotg, hs_ep); |
| 2788 | return; |
| 2789 | } |
| 2790 | |
| 2791 | if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2792 | int dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2793 | |
| 2794 | dctl |= DCTL_CGNPINNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2795 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2796 | } |
| 2797 | return; |
| 2798 | } |
| 2799 | |
| 2800 | if (dctl & DCTL_GOUTNAKSTS) { |
| 2801 | dctl |= DCTL_CGOUTNAK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2802 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | if (!hs_ep->isochronous) |
| 2806 | return; |
| 2807 | |
| 2808 | if (list_empty(&hs_ep->queue)) { |
| 2809 | dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n", |
| 2810 | __func__, hs_ep); |
| 2811 | return; |
| 2812 | } |
| 2813 | |
| 2814 | do { |
| 2815 | hs_req = get_ep_head(hs_ep); |
| 2816 | if (hs_req) |
| 2817 | dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, |
| 2818 | -ENODATA); |
| 2819 | dwc2_gadget_incr_frame_num(hs_ep); |
Artur Petrosyan | c7c24e7 | 2018-05-05 09:46:26 -0400 | [diff] [blame] | 2820 | /* Update current frame number value. */ |
| 2821 | hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg); |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 2822 | } while (dwc2_gadget_target_frame_elapsed(hs_ep)); |
| 2823 | |
| 2824 | dwc2_gadget_start_next_request(hs_ep); |
| 2825 | } |
| 2826 | |
| 2827 | /** |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2828 | * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 2829 | * @ep: The endpoint on which interrupt is asserted. |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2830 | * |
| 2831 | * This is starting point for ISOC-OUT transfer, synchronization done with |
| 2832 | * first out token received from host while corresponding EP is disabled. |
| 2833 | * |
| 2834 | * Device does not know initial frame in which out token will come. For this |
| 2835 | * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon |
| 2836 | * getting this interrupt SW starts calculation for next transfer frame. |
| 2837 | */ |
| 2838 | static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep) |
| 2839 | { |
| 2840 | struct dwc2_hsotg *hsotg = ep->parent; |
| 2841 | int dir_in = ep->dir_in; |
| 2842 | u32 doepmsk; |
| 2843 | |
| 2844 | if (dir_in || !ep->isochronous) |
| 2845 | return; |
| 2846 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2847 | if (using_desc_dma(hsotg)) { |
| 2848 | if (ep->target_frame == TARGET_FRAME_INITIAL) { |
| 2849 | /* Start first ISO Out */ |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2850 | ep->target_frame = hsotg->frame_number; |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2851 | dwc2_gadget_start_isoc_ddma(ep); |
| 2852 | } |
| 2853 | return; |
| 2854 | } |
| 2855 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2856 | if (ep->interval > 1 && |
| 2857 | ep->target_frame == TARGET_FRAME_INITIAL) { |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2858 | u32 ctrl; |
| 2859 | |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2860 | ep->target_frame = hsotg->frame_number; |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2861 | dwc2_gadget_incr_frame_num(ep); |
| 2862 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2863 | ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index)); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2864 | if (ep->target_frame & 0x1) |
| 2865 | ctrl |= DXEPCTL_SETODDFR; |
| 2866 | else |
| 2867 | ctrl |= DXEPCTL_SETEVENFR; |
| 2868 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2869 | dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index)); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | dwc2_gadget_start_next_request(ep); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2873 | doepmsk = dwc2_readl(hsotg, DOEPMSK); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2874 | doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2875 | dwc2_writel(hsotg, doepmsk, DOEPMSK); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | /** |
John Youn | 38beaec | 2017-01-17 20:31:13 -0800 | [diff] [blame] | 2879 | * dwc2_gadget_handle_nak - handle NAK interrupt |
| 2880 | * @hs_ep: The endpoint on which interrupt is asserted. |
| 2881 | * |
| 2882 | * This is starting point for ISOC-IN transfer, synchronization done with |
| 2883 | * first IN token received from host while corresponding EP is disabled. |
| 2884 | * |
| 2885 | * Device does not know when first one token will arrive from host. On first |
| 2886 | * token arrival HW generates 2 interrupts: 'in token received while FIFO empty' |
| 2887 | * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was |
| 2888 | * sent in response to that as there was no data in FIFO. SW is basing on this |
| 2889 | * interrupt to obtain frame in which token has come and then based on the |
| 2890 | * interval calculates next frame for transfer. |
| 2891 | */ |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2892 | static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep) |
| 2893 | { |
| 2894 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 2895 | int dir_in = hs_ep->dir_in; |
| 2896 | |
| 2897 | if (!dir_in || !hs_ep->isochronous) |
| 2898 | return; |
| 2899 | |
| 2900 | if (hs_ep->target_frame == TARGET_FRAME_INITIAL) { |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2901 | |
| 2902 | if (using_desc_dma(hsotg)) { |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2903 | hs_ep->target_frame = hsotg->frame_number; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2904 | dwc2_gadget_incr_frame_num(hs_ep); |
Grigor Tovmasyan | 48dac4e | 2018-08-29 21:00:33 +0400 | [diff] [blame] | 2905 | |
| 2906 | /* In service interval mode target_frame must |
| 2907 | * be set to last (u)frame of the service interval. |
| 2908 | */ |
| 2909 | if (hsotg->params.service_interval) { |
| 2910 | /* Set target_frame to the first (u)frame of |
| 2911 | * the service interval |
| 2912 | */ |
| 2913 | hs_ep->target_frame &= ~hs_ep->interval + 1; |
| 2914 | |
| 2915 | /* Set target_frame to the last (u)frame of |
| 2916 | * the service interval |
| 2917 | */ |
| 2918 | dwc2_gadget_incr_frame_num(hs_ep); |
| 2919 | dwc2_gadget_dec_frame_num_by_one(hs_ep); |
| 2920 | } |
| 2921 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2922 | dwc2_gadget_start_isoc_ddma(hs_ep); |
| 2923 | return; |
| 2924 | } |
| 2925 | |
Minas Harutyunyan | 4d4f1e7 | 2018-07-27 14:48:45 +0400 | [diff] [blame] | 2926 | hs_ep->target_frame = hsotg->frame_number; |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2927 | if (hs_ep->interval > 1) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2928 | u32 ctrl = dwc2_readl(hsotg, |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2929 | DIEPCTL(hs_ep->index)); |
| 2930 | if (hs_ep->target_frame & 0x1) |
| 2931 | ctrl |= DXEPCTL_SETODDFR; |
| 2932 | else |
| 2933 | ctrl |= DXEPCTL_SETEVENFR; |
| 2934 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2935 | dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index)); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | dwc2_hsotg_complete_request(hsotg, hs_ep, |
| 2939 | get_ep_head(hs_ep), 0); |
| 2940 | } |
| 2941 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 2942 | if (!using_desc_dma(hsotg)) |
| 2943 | dwc2_gadget_incr_frame_num(hs_ep); |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2947 | * dwc2_hsotg_epint - handle an in/out endpoint interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2948 | * @hsotg: The driver state |
| 2949 | * @idx: The index for the endpoint (0..15) |
| 2950 | * @dir_in: Set if this is an IN endpoint |
| 2951 | * |
| 2952 | * Process and clear any interrupt pending for an individual endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 2953 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2954 | static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2955 | int dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2956 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 2957 | 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] | 2958 | u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx); |
| 2959 | u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx); |
| 2960 | u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2961 | u32 ints; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 2962 | u32 ctrl; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2963 | |
Vardan Mikayelyan | 3260158 | 2016-05-25 18:07:10 -0700 | [diff] [blame] | 2964 | ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2965 | ctrl = dwc2_readl(hsotg, epctl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2966 | |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2967 | /* Clear endpoint interrupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2968 | dwc2_writel(hsotg, ints, epint_reg); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 2969 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2970 | if (!hs_ep) { |
| 2971 | dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n", |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 2972 | __func__, idx, dir_in ? "in" : "out"); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 2973 | return; |
| 2974 | } |
| 2975 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2976 | dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n", |
| 2977 | __func__, idx, dir_in ? "in" : "out", ints); |
| 2978 | |
Mian Yousaf Kaukab | b787d75 | 2015-01-09 13:38:43 +0100 | [diff] [blame] | 2979 | /* Don't process XferCompl interrupt if it is a setup packet */ |
| 2980 | if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD))) |
| 2981 | ints &= ~DXEPINT_XFERCOMPL; |
| 2982 | |
Vahram Aharonyan | f0afdb4 | 2016-11-14 19:16:48 -0800 | [diff] [blame] | 2983 | /* |
| 2984 | * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP |
| 2985 | * stage and xfercomplete was generated without SETUP phase done |
| 2986 | * interrupt. SW should parse received setup packet only after host's |
| 2987 | * exit from setup phase of control transfer. |
| 2988 | */ |
| 2989 | if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in && |
| 2990 | hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP)) |
| 2991 | ints &= ~DXEPINT_XFERCOMPL; |
| 2992 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 2993 | if (ints & DXEPINT_XFERCOMPL) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2994 | dev_dbg(hsotg->dev, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 2995 | "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 2996 | __func__, dwc2_readl(hsotg, epctl_reg), |
| 2997 | dwc2_readl(hsotg, epsiz_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 2998 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 2999 | /* In DDMA handle isochronous requests separately */ |
| 3000 | if (using_desc_dma(hsotg) && hs_ep->isochronous) { |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3001 | /* XferCompl set along with BNA */ |
| 3002 | if (!(ints & DXEPINT_BNAINTR)) |
| 3003 | dwc2_gadget_complete_isoc_request_ddma(hs_ep); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3004 | } else if (dir_in) { |
| 3005 | /* |
| 3006 | * We get OutDone from the FIFO, so we only |
| 3007 | * need to look at completing IN requests here |
| 3008 | * if operating slave mode |
| 3009 | */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3010 | if (hs_ep->isochronous && hs_ep->interval > 1) |
| 3011 | dwc2_gadget_incr_frame_num(hs_ep); |
| 3012 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3013 | dwc2_hsotg_complete_in(hsotg, hs_ep); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3014 | if (ints & DXEPINT_NAKINTRPT) |
| 3015 | ints &= ~DXEPINT_NAKINTRPT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3016 | |
Ben Dooks | c9a64ea | 2010-07-19 09:40:46 +0100 | [diff] [blame] | 3017 | if (idx == 0 && !hs_ep->req) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3018 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3019 | } else if (using_dma(hsotg)) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3020 | /* |
| 3021 | * We're using DMA, we need to fire an OutDone here |
| 3022 | * as we ignore the RXFIFO. |
| 3023 | */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3024 | if (hs_ep->isochronous && hs_ep->interval > 1) |
| 3025 | dwc2_gadget_incr_frame_num(hs_ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3026 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3027 | dwc2_hsotg_handle_outdone(hsotg, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3028 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3029 | } |
| 3030 | |
Vardan Mikayelyan | bd9971f | 2016-05-25 18:07:19 -0700 | [diff] [blame] | 3031 | if (ints & DXEPINT_EPDISBLD) |
| 3032 | dwc2_gadget_handle_ep_disabled(hs_ep); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 3033 | |
Vardan Mikayelyan | 5321922 | 2016-05-25 18:07:14 -0700 | [diff] [blame] | 3034 | if (ints & DXEPINT_OUTTKNEPDIS) |
| 3035 | dwc2_gadget_handle_out_token_ep_disabled(hs_ep); |
| 3036 | |
| 3037 | if (ints & DXEPINT_NAKINTRPT) |
| 3038 | dwc2_gadget_handle_nak(hs_ep); |
| 3039 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3040 | if (ints & DXEPINT_AHBERR) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3041 | dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3042 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3043 | if (ints & DXEPINT_SETUP) { /* Setup or Timeout */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3044 | dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__); |
| 3045 | |
| 3046 | if (using_dma(hsotg) && idx == 0) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3047 | /* |
| 3048 | * this is the notification we've received a |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3049 | * setup packet. In non-DMA mode we'd get this |
| 3050 | * from the RXFIFO, instead we need to process |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3051 | * the setup here. |
| 3052 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3053 | |
| 3054 | if (dir_in) |
| 3055 | WARN_ON_ONCE(1); |
| 3056 | else |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3057 | dwc2_hsotg_handle_outdone(hsotg, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3058 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3059 | } |
| 3060 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 3061 | if (ints & DXEPINT_STSPHSERCVD) { |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3062 | dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__); |
| 3063 | |
Minas Harutyunyan | 9e95a66 | 2018-01-16 16:03:58 +0400 | [diff] [blame] | 3064 | /* Safety check EP0 state when STSPHSERCVD asserted */ |
| 3065 | if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) { |
| 3066 | /* Move to STATUS IN for DDMA */ |
Minas Harutyunyan | b4c53b4 | 2019-03-12 11:45:12 +0400 | [diff] [blame^] | 3067 | if (using_desc_dma(hsotg)) { |
| 3068 | if (!hsotg->delayed_status) |
| 3069 | dwc2_hsotg_ep0_zlp(hsotg, true); |
| 3070 | else |
| 3071 | /* In case of 3 stage Control Write with delayed |
| 3072 | * status, when Status IN transfer started |
| 3073 | * before STSPHSERCVD asserted, NAKSTS bit not |
| 3074 | * cleared by CNAK in dwc2_hsotg_start_req() |
| 3075 | * function. Clear now NAKSTS to allow complete |
| 3076 | * transfer. |
| 3077 | */ |
| 3078 | dwc2_set_bit(hsotg, DIEPCTL(0), |
| 3079 | DXEPCTL_CNAK); |
| 3080 | } |
Minas Harutyunyan | 9e95a66 | 2018-01-16 16:03:58 +0400 | [diff] [blame] | 3081 | } |
| 3082 | |
Vahram Aharonyan | ef750c7 | 2016-11-14 19:16:31 -0800 | [diff] [blame] | 3083 | } |
| 3084 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3085 | if (ints & DXEPINT_BACK2BACKSETUP) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3086 | dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3087 | |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3088 | if (ints & DXEPINT_BNAINTR) { |
| 3089 | dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3090 | if (hs_ep->isochronous) |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3091 | dwc2_gadget_handle_isoc_bna(hs_ep); |
Vahram Aharonyan | 540ccba | 2016-11-14 19:16:41 -0800 | [diff] [blame] | 3092 | } |
| 3093 | |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3094 | if (dir_in && !hs_ep->isochronous) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3095 | /* not sure if this is important, but we'll clear it anyway */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3096 | if (ints & DXEPINT_INTKNTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3097 | dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n", |
| 3098 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | /* this probably means something bad is happening */ |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3102 | if (ints & DXEPINT_INTKNEPMIS) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3103 | dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n", |
| 3104 | __func__, idx); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3105 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3106 | |
| 3107 | /* FIFO has space or is empty (see GAHBCFG) */ |
| 3108 | if (hsotg->dedicated_fifos && |
Vardan Mikayelyan | 26ddef5 | 2016-05-25 18:07:00 -0700 | [diff] [blame] | 3109 | ints & DXEPINT_TXFEMP) { |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3110 | dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n", |
| 3111 | __func__, idx); |
Anton Tikhomirov | 70fa030 | 2012-03-06 14:08:29 +0900 | [diff] [blame] | 3112 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3113 | dwc2_hsotg_trytx(hsotg, hs_ep); |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 3114 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3115 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3119 | * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3120 | * @hsotg: The device state. |
| 3121 | * |
| 3122 | * Handle updating the device settings after the enumeration phase has |
| 3123 | * been completed. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3124 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3125 | static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3126 | { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3127 | u32 dsts = dwc2_readl(hsotg, DSTS); |
Jingoo Han | 9b2667f | 2014-08-20 12:04:09 +0900 | [diff] [blame] | 3128 | int ep0_mps = 0, ep_mps = 8; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3129 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3130 | /* |
| 3131 | * This should signal the finish of the enumeration phase |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3132 | * of the USB handshaking, so we should now know what rate |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3133 | * we connected at. |
| 3134 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3135 | |
| 3136 | dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts); |
| 3137 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3138 | /* |
| 3139 | * 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] | 3140 | * 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] | 3141 | * not advertise a 64byte MPS on EP0. |
| 3142 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3143 | |
| 3144 | /* catch both EnumSpd_FS and EnumSpd_FS48 */ |
Marek Vasut | 6d76c92 | 2015-12-18 03:26:17 +0100 | [diff] [blame] | 3145 | switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3146 | case DSTS_ENUMSPD_FS: |
| 3147 | case DSTS_ENUMSPD_FS48: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3148 | hsotg->gadget.speed = USB_SPEED_FULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3149 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 3150 | ep_mps = 1023; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3151 | break; |
| 3152 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3153 | case DSTS_ENUMSPD_HS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3154 | hsotg->gadget.speed = USB_SPEED_HIGH; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3155 | ep0_mps = EP0_MPS_LIMIT; |
Robert Baldyga | 295538f | 2013-12-06 13:03:44 +0100 | [diff] [blame] | 3156 | ep_mps = 1024; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3157 | break; |
| 3158 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3159 | case DSTS_ENUMSPD_LS: |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3160 | hsotg->gadget.speed = USB_SPEED_LOW; |
Vardan Mikayelyan | 552d940 | 2016-11-14 19:17:00 -0800 | [diff] [blame] | 3161 | ep0_mps = 8; |
| 3162 | ep_mps = 8; |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3163 | /* |
| 3164 | * note, we don't actually support LS in this driver at the |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3165 | * moment, and the documentation seems to imply that it isn't |
| 3166 | * supported by the PHYs on some of the devices. |
| 3167 | */ |
| 3168 | break; |
| 3169 | } |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 3170 | dev_info(hsotg->dev, "new device is %s\n", |
| 3171 | usb_speed_string(hsotg->gadget.speed)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3172 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3173 | /* |
| 3174 | * we should now know the maximum packet size for an |
| 3175 | * endpoint, so set the endpoints to a default value. |
| 3176 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3177 | |
| 3178 | if (ep0_mps) { |
| 3179 | int i; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3180 | /* Initialize ep0 for both in and out directions */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3181 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1); |
| 3182 | dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3183 | for (i = 1; i < hsotg->num_of_eps; i++) { |
| 3184 | if (hsotg->eps_in[i]) |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3185 | dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, |
| 3186 | 0, 1); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3187 | if (hsotg->eps_out[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, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3190 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | /* ensure after enumeration our EP0 is active */ |
| 3194 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3195 | dwc2_hsotg_enqueue_setup(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3196 | |
| 3197 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3198 | dwc2_readl(hsotg, DIEPCTL0), |
| 3199 | dwc2_readl(hsotg, DOEPCTL0)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | /** |
| 3203 | * kill_all_requests - remove all requests from the endpoint's queue |
| 3204 | * @hsotg: The device state. |
| 3205 | * @ep: The endpoint the requests may be on. |
| 3206 | * @result: The result code to use. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3207 | * |
| 3208 | * Go through the requests on the given endpoint and mark them |
| 3209 | * completed with the given result code. |
| 3210 | */ |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3211 | static void kill_all_requests(struct dwc2_hsotg *hsotg, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3212 | struct dwc2_hsotg_ep *ep, |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3213 | int result) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3214 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3215 | struct dwc2_hsotg_req *req, *treq; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3216 | unsigned int size; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3217 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3218 | ep->req = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3219 | |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3220 | list_for_each_entry_safe(req, treq, &ep->queue, queue) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3221 | dwc2_hsotg_complete_request(hsotg, ep, req, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3222 | result); |
Robert Baldyga | 6b448af4 | 2014-12-16 11:51:44 +0100 | [diff] [blame] | 3223 | |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3224 | if (!hsotg->dedicated_fifos) |
| 3225 | return; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3226 | size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 3227 | if (size < ep->fifo_size) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3228 | dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3229 | } |
| 3230 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3231 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3232 | * dwc2_hsotg_disconnect - disconnect service |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3233 | * @hsotg: The device state. |
| 3234 | * |
Lukasz Majewski | 5e89134 | 2012-05-04 14:17:07 +0200 | [diff] [blame] | 3235 | * The device has been disconnected. Remove all current |
| 3236 | * transactions and signal the gadget driver that this |
| 3237 | * has happened. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3238 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3239 | void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3240 | { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3241 | unsigned int ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3242 | |
Marek Szyprowski | 4ace06e | 2014-11-21 15:14:47 +0100 | [diff] [blame] | 3243 | if (!hsotg->connected) |
| 3244 | return; |
| 3245 | |
| 3246 | hsotg->connected = 0; |
Gregory Herrero | 9e14d0a | 2015-01-30 09:09:28 +0100 | [diff] [blame] | 3247 | hsotg->test_mode = 0; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3248 | |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3249 | /* all endpoints should be shutdown */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3250 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 3251 | if (hsotg->eps_in[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 3252 | kill_all_requests(hsotg, hsotg->eps_in[ep], |
| 3253 | -ESHUTDOWN); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3254 | if (hsotg->eps_out[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 3255 | kill_all_requests(hsotg, hsotg->eps_out[ep], |
| 3256 | -ESHUTDOWN); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3257 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3258 | |
| 3259 | call_gadget(hsotg, disconnect); |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 3260 | hsotg->lx_state = DWC2_L3; |
John Stultz | ce2b21a | 2017-10-23 14:32:50 -0700 | [diff] [blame] | 3261 | |
| 3262 | usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3266 | * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3267 | * @hsotg: The device state: |
| 3268 | * @periodic: True if this is a periodic FIFO interrupt |
| 3269 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3270 | static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3271 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3272 | struct dwc2_hsotg_ep *ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3273 | int epno, ret; |
| 3274 | |
| 3275 | /* look through for any more data to transmit */ |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 3276 | for (epno = 0; epno < hsotg->num_of_eps; epno++) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 3277 | ep = index_to_ep(hsotg, epno, 1); |
| 3278 | |
| 3279 | if (!ep) |
| 3280 | continue; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3281 | |
| 3282 | if (!ep->dir_in) |
| 3283 | continue; |
| 3284 | |
| 3285 | if ((periodic && !ep->periodic) || |
| 3286 | (!periodic && ep->periodic)) |
| 3287 | continue; |
| 3288 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3289 | ret = dwc2_hsotg_trytx(hsotg, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3290 | if (ret < 0) |
| 3291 | break; |
| 3292 | } |
| 3293 | } |
| 3294 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3295 | /* IRQ flags which will trigger a retry around the IRQ loop */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3296 | #define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \ |
| 3297 | GINTSTS_PTXFEMP | \ |
| 3298 | GINTSTS_RXFLVL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3299 | |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 3300 | static int dwc2_hsotg_ep_disable(struct usb_ep *ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3301 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3302 | * dwc2_hsotg_core_init - issue softreset to the core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3303 | * @hsotg: The device state |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 3304 | * @is_usb_reset: Usb resetting flag |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3305 | * |
| 3306 | * Issue a soft reset to the core, and await the core finishing it. |
| 3307 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3308 | void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3309 | bool is_usb_reset) |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3310 | { |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3311 | u32 intmsk; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3312 | u32 val; |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3313 | u32 usbcfg; |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3314 | u32 dcfg = 0; |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3315 | int ep; |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3316 | |
Mian Yousaf Kaukab | 5390d43 | 2015-09-29 12:08:25 +0200 | [diff] [blame] | 3317 | /* Kill any ep0 requests as controller will be reinitialized */ |
| 3318 | kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET); |
| 3319 | |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3320 | if (!is_usb_reset) { |
John Stultz | 6e6360b | 2017-01-23 14:59:14 -0800 | [diff] [blame] | 3321 | if (dwc2_core_reset(hsotg, true)) |
Gregory Herrero | 86de489 | 2015-09-29 12:08:21 +0200 | [diff] [blame] | 3322 | return; |
Minas Harutyunyan | dccf1ba | 2018-09-19 18:13:52 +0400 | [diff] [blame] | 3323 | } else { |
| 3324 | /* all endpoints should be shutdown */ |
| 3325 | for (ep = 1; ep < hsotg->num_of_eps; ep++) { |
| 3326 | if (hsotg->eps_in[ep]) |
| 3327 | dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep); |
| 3328 | if (hsotg->eps_out[ep]) |
| 3329 | dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep); |
| 3330 | } |
| 3331 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3332 | |
| 3333 | /* |
| 3334 | * we must now enable ep0 ready for host detection and then |
| 3335 | * set configuration. |
| 3336 | */ |
| 3337 | |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3338 | /* keep other bits untouched (so e.g. forced modes are not lost) */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3339 | usbcfg = dwc2_readl(hsotg, GUSBCFG); |
Jules Maselbas | 1e86854 | 2019-04-05 15:35:33 +0200 | [diff] [blame] | 3340 | usbcfg &= ~GUSBCFG_TOUTCAL_MASK; |
Jules Maselbas | 707d80f | 2019-04-05 15:35:31 +0200 | [diff] [blame] | 3341 | usbcfg |= GUSBCFG_TOUTCAL(7); |
Przemek Rudy | ecd9a7a | 2016-03-16 23:10:26 +0100 | [diff] [blame] | 3342 | |
Jules Maselbas | 1e86854 | 2019-04-05 15:35:33 +0200 | [diff] [blame] | 3343 | /* remove the HNP/SRP and set the PHY */ |
| 3344 | usbcfg &= ~(GUSBCFG_SRPCAP | GUSBCFG_HNPCAP); |
| 3345 | dwc2_writel(hsotg, usbcfg, GUSBCFG); |
Jules Maselbas | 707d80f | 2019-04-05 15:35:31 +0200 | [diff] [blame] | 3346 | |
Jules Maselbas | 1e86854 | 2019-04-05 15:35:33 +0200 | [diff] [blame] | 3347 | dwc2_phy_init(hsotg, true); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3348 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3349 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3350 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3351 | if (!is_usb_reset) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3352 | dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3353 | |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3354 | dcfg |= DCFG_EPMISCNT(1); |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3355 | |
| 3356 | switch (hsotg->params.speed) { |
| 3357 | case DWC2_SPEED_PARAM_LOW: |
| 3358 | dcfg |= DCFG_DEVSPD_LS; |
| 3359 | break; |
| 3360 | case DWC2_SPEED_PARAM_FULL: |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3361 | if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) |
| 3362 | dcfg |= DCFG_DEVSPD_FS48; |
| 3363 | else |
| 3364 | dcfg |= DCFG_DEVSPD_FS; |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3365 | break; |
| 3366 | default: |
Vahram Aharonyan | 79c3b5b | 2016-11-14 19:16:55 -0800 | [diff] [blame] | 3367 | dcfg |= DCFG_DEVSPD_HS; |
| 3368 | } |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 3369 | |
Grigor Tovmasyan | b43ebc9 | 2018-05-05 12:17:58 +0400 | [diff] [blame] | 3370 | if (hsotg->params.ipg_isoc_en) |
| 3371 | dcfg |= DCFG_IPG_ISOC_SUPPORDED; |
| 3372 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3373 | dwc2_writel(hsotg, dcfg, DCFG); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3374 | |
| 3375 | /* Clear any pending OTG interrupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3376 | dwc2_writel(hsotg, 0xffffffff, GOTGINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3377 | |
| 3378 | /* Clear any pending interrupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3379 | dwc2_writel(hsotg, 0xffffffff, GINTSTS); |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3380 | intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3381 | GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF | |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3382 | GINTSTS_USBRST | GINTSTS_RESETDET | |
| 3383 | GINTSTS_ENUMDONE | GINTSTS_OTGINT | |
Sevak Arakelyan | 376f040 | 2018-01-24 17:43:06 +0400 | [diff] [blame] | 3384 | GINTSTS_USBSUSP | GINTSTS_WKUPINT | |
| 3385 | GINTSTS_LPMTRANRCVD; |
Vahram Aharonyan | f473670 | 2016-11-14 19:16:38 -0800 | [diff] [blame] | 3386 | |
| 3387 | if (!using_desc_dma(hsotg)) |
| 3388 | intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT; |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3389 | |
John Youn | 95832c0 | 2017-01-23 14:57:26 -0800 | [diff] [blame] | 3390 | if (!hsotg->params.external_id_pin_ctl) |
Gregory Herrero | 1ee6903 | 2015-09-29 12:08:27 +0200 | [diff] [blame] | 3391 | intmsk |= GINTSTS_CONIDSTSCHNG; |
| 3392 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3393 | dwc2_writel(hsotg, intmsk, GINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3394 | |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3395 | if (using_dma(hsotg)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3396 | dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN | |
Razmik Karapetyan | d1ac8c8 | 2018-01-19 14:39:57 +0400 | [diff] [blame] | 3397 | hsotg->params.ahbcfg, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3398 | GAHBCFG); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3399 | |
| 3400 | /* Set DDMA mode support in the core if needed */ |
| 3401 | if (using_desc_dma(hsotg)) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3402 | dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3403 | |
| 3404 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3405 | dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ? |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 3406 | (GAHBCFG_NP_TXF_EMP_LVL | |
| 3407 | GAHBCFG_P_TXF_EMP_LVL) : 0) | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3408 | GAHBCFG_GLBL_INTR_EN, GAHBCFG); |
Vahram Aharonyan | a5c18f1 | 2016-11-14 19:16:34 -0800 | [diff] [blame] | 3409 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3410 | |
| 3411 | /* |
Robert Baldyga | 8acc829 | 2013-09-19 11:50:23 +0200 | [diff] [blame] | 3412 | * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts |
| 3413 | * when we have no data to transfer. Otherwise we get being flooded by |
| 3414 | * interrupts. |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3415 | */ |
| 3416 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3417 | dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ? |
Mian Yousaf Kaukab | 6ff2e83 | 2015-01-09 13:38:42 +0100 | [diff] [blame] | 3418 | DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3419 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3420 | DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3421 | DIEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3422 | |
| 3423 | /* |
| 3424 | * 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] | 3425 | * DMA mode we may need this and StsPhseRcvd. |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3426 | */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3427 | dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK | |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3428 | DOEPMSK_STSPHSERCVDMSK) : 0) | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3429 | DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK | |
Vahram Aharonyan | 9d9a6b0 | 2016-11-14 19:16:29 -0800 | [diff] [blame] | 3430 | DOEPMSK_SETUPMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3431 | DOEPMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3432 | |
Vahram Aharonyan | ec01f0b | 2016-11-14 19:16:43 -0800 | [diff] [blame] | 3433 | /* Enable BNA interrupt for DDMA */ |
Minas Harutyunyan | 37981e0 | 2018-05-03 17:25:37 +0400 | [diff] [blame] | 3434 | if (using_desc_dma(hsotg)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3435 | dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK); |
| 3436 | dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK); |
Minas Harutyunyan | 37981e0 | 2018-05-03 17:25:37 +0400 | [diff] [blame] | 3437 | } |
Vahram Aharonyan | ec01f0b | 2016-11-14 19:16:43 -0800 | [diff] [blame] | 3438 | |
Grigor Tovmasyan | ca531bc | 2018-08-29 20:59:34 +0400 | [diff] [blame] | 3439 | /* Enable Service Interval mode if supported */ |
| 3440 | if (using_desc_dma(hsotg) && hsotg->params.service_interval) |
| 3441 | dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED); |
| 3442 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3443 | dwc2_writel(hsotg, 0, DAINTMSK); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3444 | |
| 3445 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3446 | dwc2_readl(hsotg, DIEPCTL0), |
| 3447 | dwc2_readl(hsotg, DOEPCTL0)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3448 | |
| 3449 | /* enable in and out endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3450 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3451 | |
| 3452 | /* |
| 3453 | * Enable the RXFIFO when in slave mode, as this is how we collect |
| 3454 | * the data. In DMA mode, we get events from the FIFO but also |
| 3455 | * things we cannot process, so do not use it. |
| 3456 | */ |
| 3457 | if (!using_dma(hsotg)) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3458 | dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3459 | |
| 3460 | /* Enable interrupts for EP0 in and out */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3461 | dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1); |
| 3462 | dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3463 | |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3464 | if (!is_usb_reset) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3465 | dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE); |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3466 | udelay(10); /* see openiboot */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3467 | dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE); |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3468 | } |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3469 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3470 | dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL)); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3471 | |
| 3472 | /* |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3473 | * DxEPCTL_USBActEp says RO in manual, but seems to be set by |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3474 | * writing to the EPCTL register.. |
| 3475 | */ |
| 3476 | |
| 3477 | /* set to read 1 8byte packet */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3478 | dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) | |
| 3479 | DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3480 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3481 | 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] | 3482 | DXEPCTL_CNAK | DXEPCTL_EPENA | |
| 3483 | DXEPCTL_USBACTEP, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3484 | DOEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3485 | |
| 3486 | /* enable, but don't activate EP0in */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3487 | dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) | |
| 3488 | DXEPCTL_USBACTEP, DIEPCTL0); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3489 | |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3490 | /* clear global NAKs */ |
Gregory Herrero | 643cc4d | 2015-01-30 09:09:32 +0100 | [diff] [blame] | 3491 | val = DCTL_CGOUTNAK | DCTL_CGNPINNAK; |
| 3492 | if (!is_usb_reset) |
| 3493 | val |= DCTL_SFTDISCON; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3494 | dwc2_set_bit(hsotg, DCTL, val); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3495 | |
Sevak Arakelyan | 21b0340 | 2018-01-24 17:43:32 +0400 | [diff] [blame] | 3496 | /* configure the core to support LPM */ |
| 3497 | dwc2_gadget_init_lpm(hsotg); |
| 3498 | |
Grigor Tovmasyan | 15d9dbf | 2018-08-29 21:01:59 +0400 | [diff] [blame] | 3499 | /* program GREFCLK register if needed */ |
| 3500 | if (using_desc_dma(hsotg) && hsotg->params.service_interval) |
| 3501 | dwc2_gadget_program_ref_clk(hsotg); |
| 3502 | |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3503 | /* must be at-least 3ms to allow bus to see disconnect */ |
| 3504 | mdelay(3); |
| 3505 | |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 3506 | hsotg->lx_state = DWC2_L0; |
Vardan Mikayelyan | 755d739 | 2018-01-16 16:04:24 +0400 | [diff] [blame] | 3507 | |
| 3508 | dwc2_hsotg_enqueue_setup(hsotg); |
| 3509 | |
| 3510 | dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3511 | dwc2_readl(hsotg, DIEPCTL0), |
| 3512 | dwc2_readl(hsotg, DOEPCTL0)); |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3513 | } |
Marek Szyprowski | ac3c81f | 2014-10-20 12:45:35 +0200 | [diff] [blame] | 3514 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3515 | static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3516 | { |
| 3517 | /* set the soft-disconnect bit */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3518 | dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3519 | } |
| 3520 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3521 | void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | ad38dc5 | 2014-10-20 12:45:36 +0200 | [diff] [blame] | 3522 | { |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3523 | /* remove the soft-disconnect and let's go */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3524 | dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | 308d734 | 2012-05-04 14:17:05 +0200 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | /** |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3528 | * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt. |
| 3529 | * @hsotg: The device state: |
| 3530 | * |
| 3531 | * This interrupt indicates one of the following conditions occurred while |
| 3532 | * transmitting an ISOC transaction. |
| 3533 | * - Corrupted IN Token for ISOC EP. |
| 3534 | * - Packet not complete in FIFO. |
| 3535 | * |
| 3536 | * The following actions will be taken: |
| 3537 | * - Determine the EP |
| 3538 | * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO |
| 3539 | */ |
| 3540 | static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg) |
| 3541 | { |
| 3542 | struct dwc2_hsotg_ep *hs_ep; |
| 3543 | u32 epctrl; |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3544 | u32 daintmsk; |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3545 | u32 idx; |
| 3546 | |
| 3547 | dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n"); |
| 3548 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3549 | daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3550 | |
Artur Petrosyan | d5d5f07 | 2018-05-05 04:30:16 -0400 | [diff] [blame] | 3551 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3552 | hs_ep = hsotg->eps_in[idx]; |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3553 | /* Proceed only unmasked ISOC EPs */ |
John Keeping | 89066b3 | 2018-07-15 15:50:43 +0100 | [diff] [blame] | 3554 | if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous) |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3555 | continue; |
| 3556 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3557 | epctrl = dwc2_readl(hsotg, DIEPCTL(idx)); |
Razmik Karapetyan | 1b4977c | 2018-01-19 14:40:49 +0400 | [diff] [blame] | 3558 | if ((epctrl & DXEPCTL_EPENA) && |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3559 | dwc2_gadget_target_frame_elapsed(hs_ep)) { |
| 3560 | epctrl |= DXEPCTL_SNAK; |
| 3561 | epctrl |= DXEPCTL_EPDIS; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3562 | dwc2_writel(hsotg, epctrl, DIEPCTL(idx)); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3563 | } |
| 3564 | } |
| 3565 | |
| 3566 | /* Clear interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3567 | dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | /** |
| 3571 | * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt |
| 3572 | * @hsotg: The device state: |
| 3573 | * |
| 3574 | * This interrupt indicates one of the following conditions occurred while |
| 3575 | * transmitting an ISOC transaction. |
| 3576 | * - Corrupted OUT Token for ISOC EP. |
| 3577 | * - Packet not complete in FIFO. |
| 3578 | * |
| 3579 | * The following actions will be taken: |
| 3580 | * - Determine the EP |
| 3581 | * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed. |
| 3582 | */ |
| 3583 | static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg) |
| 3584 | { |
| 3585 | u32 gintsts; |
| 3586 | u32 gintmsk; |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3587 | u32 daintmsk; |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3588 | u32 epctrl; |
| 3589 | struct dwc2_hsotg_ep *hs_ep; |
| 3590 | int idx; |
| 3591 | |
| 3592 | dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__); |
| 3593 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3594 | daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3595 | daintmsk >>= DAINT_OUTEP_SHIFT; |
| 3596 | |
Artur Petrosyan | d5d5f07 | 2018-05-05 04:30:16 -0400 | [diff] [blame] | 3597 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3598 | hs_ep = hsotg->eps_out[idx]; |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3599 | /* Proceed only unmasked ISOC EPs */ |
John Keeping | 89066b3 | 2018-07-15 15:50:43 +0100 | [diff] [blame] | 3600 | if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous) |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3601 | continue; |
| 3602 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3603 | epctrl = dwc2_readl(hsotg, DOEPCTL(idx)); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3604 | if ((epctrl & DXEPCTL_EPENA) && |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3605 | dwc2_gadget_target_frame_elapsed(hs_ep)) { |
| 3606 | /* Unmask GOUTNAKEFF interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3607 | gintmsk = dwc2_readl(hsotg, GINTMSK); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3608 | gintmsk |= GINTSTS_GOUTNAKEFF; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3609 | dwc2_writel(hsotg, gintmsk, GINTMSK); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3610 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3611 | gintsts = dwc2_readl(hsotg, GINTSTS); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3612 | if (!(gintsts & GINTSTS_GOUTNAKEFF)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3613 | dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK); |
Razmik Karapetyan | 689efb2 | 2018-01-19 14:41:16 +0400 | [diff] [blame] | 3614 | break; |
| 3615 | } |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3616 | } |
| 3617 | } |
| 3618 | |
| 3619 | /* Clear interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3620 | dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS); |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3624 | * dwc2_hsotg_irq - handle device interrupt |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3625 | * @irq: The IRQ number triggered |
| 3626 | * @pw: The pw value when registered the handler. |
| 3627 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3628 | static irqreturn_t dwc2_hsotg_irq(int irq, void *pw) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3629 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3630 | struct dwc2_hsotg *hsotg = pw; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3631 | int retry_count = 8; |
| 3632 | u32 gintsts; |
| 3633 | u32 gintmsk; |
| 3634 | |
Vardan Mikayelyan | ee3de8d | 2016-04-27 20:20:48 -0700 | [diff] [blame] | 3635 | if (!dwc2_is_device_mode(hsotg)) |
| 3636 | return IRQ_NONE; |
| 3637 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3638 | spin_lock(&hsotg->lock); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3639 | irq_retry: |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3640 | gintsts = dwc2_readl(hsotg, GINTSTS); |
| 3641 | gintmsk = dwc2_readl(hsotg, GINTMSK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3642 | |
| 3643 | dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n", |
| 3644 | __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count); |
| 3645 | |
| 3646 | gintsts &= gintmsk; |
| 3647 | |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3648 | if (gintsts & GINTSTS_RESETDET) { |
| 3649 | dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__); |
| 3650 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3651 | dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3652 | |
| 3653 | /* This event must be used only if controller is suspended */ |
| 3654 | if (hsotg->lx_state == DWC2_L2) { |
Vardan Mikayelyan | 41ba9b9 | 2018-02-16 14:06:36 +0400 | [diff] [blame] | 3655 | dwc2_exit_partial_power_down(hsotg, true); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3656 | hsotg->lx_state = DWC2_L0; |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3661 | u32 usb_status = dwc2_readl(hsotg, GOTGCTL); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3662 | u32 connected = hsotg->connected; |
| 3663 | |
| 3664 | dev_dbg(hsotg->dev, "%s: USBRst\n", __func__); |
| 3665 | dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3666 | dwc2_readl(hsotg, GNPTXSTS)); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3667 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3668 | dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS); |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3669 | |
| 3670 | /* Report disconnection if it is not already done. */ |
| 3671 | dwc2_hsotg_disconnect(hsotg); |
| 3672 | |
Minas Harutyunyan | 307bc11 | 2017-07-11 14:25:13 +0400 | [diff] [blame] | 3673 | /* Reset device address to zero */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3674 | dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK); |
Minas Harutyunyan | 307bc11 | 2017-07-11 14:25:13 +0400 | [diff] [blame] | 3675 | |
Mian Yousaf Kaukab | 8fc37b8 | 2015-09-29 12:08:29 +0200 | [diff] [blame] | 3676 | if (usb_status & GOTGCTL_BSESVLD && connected) |
| 3677 | dwc2_hsotg_core_init_disconnected(hsotg, true); |
| 3678 | } |
| 3679 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3680 | if (gintsts & GINTSTS_ENUMDONE) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3681 | dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3682 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3683 | dwc2_hsotg_irq_enumdone(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3684 | } |
| 3685 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3686 | if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3687 | u32 daint = dwc2_readl(hsotg, DAINT); |
| 3688 | u32 daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3689 | u32 daint_out, daint_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3690 | int ep; |
| 3691 | |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3692 | daint &= daintmsk; |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3693 | daint_out = daint >> DAINT_OUTEP_SHIFT; |
| 3694 | daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT); |
Robert Baldyga | 7e80465 | 2013-09-19 11:50:20 +0200 | [diff] [blame] | 3695 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3696 | dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint); |
| 3697 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 3698 | for (ep = 0; ep < hsotg->num_of_eps && daint_out; |
| 3699 | ep++, daint_out >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3700 | if (daint_out & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3701 | dwc2_hsotg_epint(hsotg, ep, 0); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3702 | } |
| 3703 | |
Mian Yousaf Kaukab | cec87f1 | 2015-01-09 13:38:51 +0100 | [diff] [blame] | 3704 | for (ep = 0; ep < hsotg->num_of_eps && daint_in; |
| 3705 | ep++, daint_in >>= 1) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3706 | if (daint_in & 1) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3707 | dwc2_hsotg_epint(hsotg, ep, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3708 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3709 | } |
| 3710 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3711 | /* check both FIFOs */ |
| 3712 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3713 | if (gintsts & GINTSTS_NPTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3714 | dev_dbg(hsotg->dev, "NPTxFEmp\n"); |
| 3715 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3716 | /* |
| 3717 | * Disable the interrupt to stop it happening again |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3718 | * unless one of these endpoint routines decides that |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3719 | * it needs re-enabling |
| 3720 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3721 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3722 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP); |
| 3723 | dwc2_hsotg_irq_fifoempty(hsotg, false); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3724 | } |
| 3725 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3726 | if (gintsts & GINTSTS_PTXFEMP) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3727 | dev_dbg(hsotg->dev, "PTxFEmp\n"); |
| 3728 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3729 | /* See note in GINTSTS_NPTxFEmp */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3730 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3731 | dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP); |
| 3732 | dwc2_hsotg_irq_fifoempty(hsotg, true); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3733 | } |
| 3734 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3735 | if (gintsts & GINTSTS_RXFLVL) { |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3736 | /* |
| 3737 | * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3738 | * we need to retry dwc2_hsotg_handle_rx if this is still |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3739 | * set. |
| 3740 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3741 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3742 | dwc2_hsotg_handle_rx(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3743 | } |
| 3744 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3745 | if (gintsts & GINTSTS_ERLYSUSP) { |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3746 | dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n"); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3747 | dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3748 | } |
| 3749 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3750 | /* |
| 3751 | * these next two seem to crop-up occasionally causing the core |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3752 | * to shutdown the USB transfer, so try clearing them and logging |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3753 | * the occurrence. |
| 3754 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3755 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3756 | if (gintsts & GINTSTS_GOUTNAKEFF) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3757 | u8 idx; |
| 3758 | u32 epctrl; |
| 3759 | u32 gintmsk; |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3760 | u32 daintmsk; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3761 | struct dwc2_hsotg_ep *hs_ep; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3762 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3763 | daintmsk = dwc2_readl(hsotg, DAINTMSK); |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3764 | daintmsk >>= DAINT_OUTEP_SHIFT; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3765 | /* Mask this interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3766 | gintmsk = dwc2_readl(hsotg, GINTMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3767 | gintmsk &= ~GINTSTS_GOUTNAKEFF; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3768 | dwc2_writel(hsotg, gintmsk, GINTMSK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3769 | |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3770 | dev_dbg(hsotg->dev, "GOUTNakEff triggered\n"); |
Artur Petrosyan | d5d5f07 | 2018-05-05 04:30:16 -0400 | [diff] [blame] | 3771 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3772 | hs_ep = hsotg->eps_out[idx]; |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3773 | /* Proceed only unmasked ISOC EPs */ |
John Keeping | 89066b3 | 2018-07-15 15:50:43 +0100 | [diff] [blame] | 3774 | if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous) |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3775 | continue; |
| 3776 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3777 | epctrl = dwc2_readl(hsotg, DOEPCTL(idx)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3778 | |
Razmik Karapetyan | d848455 | 2018-01-19 14:41:42 +0400 | [diff] [blame] | 3779 | if (epctrl & DXEPCTL_EPENA) { |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3780 | epctrl |= DXEPCTL_SNAK; |
| 3781 | epctrl |= DXEPCTL_EPDIS; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3782 | dwc2_writel(hsotg, epctrl, DOEPCTL(idx)); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3783 | } |
| 3784 | } |
| 3785 | |
| 3786 | /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3787 | } |
| 3788 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3789 | if (gintsts & GINTSTS_GINNAKEFF) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3790 | dev_info(hsotg->dev, "GINNakEff triggered\n"); |
| 3791 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3792 | dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK); |
Anton Tikhomirov | a3395f0 | 2011-04-21 17:06:39 +0900 | [diff] [blame] | 3793 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3794 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3795 | } |
| 3796 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3797 | if (gintsts & GINTSTS_INCOMPL_SOIN) |
| 3798 | dwc2_gadget_handle_incomplete_isoc_in(hsotg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 3799 | |
Vardan Mikayelyan | 381fc8f | 2016-05-25 18:07:17 -0700 | [diff] [blame] | 3800 | if (gintsts & GINTSTS_INCOMPL_SOOUT) |
| 3801 | dwc2_gadget_handle_incomplete_isoc_out(hsotg); |
Roman Bacik | ec1f9d9 | 2015-09-10 18:13:43 -0700 | [diff] [blame] | 3802 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3803 | /* |
| 3804 | * if we've had fifo events, we should try and go around the |
| 3805 | * loop again to see if there's any point in returning yet. |
| 3806 | */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3807 | |
| 3808 | if (gintsts & IRQ_RETRY_MASK && --retry_count > 0) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 3809 | goto irq_retry; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3810 | |
Grigor Tovmasyan | 187c529 | 2018-08-29 21:02:57 +0400 | [diff] [blame] | 3811 | /* Check WKUP_ALERT interrupt*/ |
| 3812 | if (hsotg->params.service_interval) |
| 3813 | dwc2_gadget_wkup_alert_handler(hsotg); |
| 3814 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 3815 | spin_unlock(&hsotg->lock); |
| 3816 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3817 | return IRQ_HANDLED; |
| 3818 | } |
| 3819 | |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3820 | static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg, |
| 3821 | struct dwc2_hsotg_ep *hs_ep) |
| 3822 | { |
| 3823 | u32 epctrl_reg; |
| 3824 | u32 epint_reg; |
| 3825 | |
| 3826 | epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) : |
| 3827 | DOEPCTL(hs_ep->index); |
| 3828 | epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) : |
| 3829 | DOEPINT(hs_ep->index); |
| 3830 | |
| 3831 | dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__, |
| 3832 | hs_ep->name); |
| 3833 | |
| 3834 | if (hs_ep->dir_in) { |
| 3835 | if (hsotg->dedicated_fifos || hs_ep->periodic) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3836 | dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3837 | /* Wait for Nak effect */ |
| 3838 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, |
| 3839 | DXEPINT_INEPNAKEFF, 100)) |
| 3840 | dev_warn(hsotg->dev, |
| 3841 | "%s: timeout DIEPINT.NAKEFF\n", |
| 3842 | __func__); |
| 3843 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3844 | dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3845 | /* Wait for Nak effect */ |
| 3846 | if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, |
| 3847 | GINTSTS_GINNAKEFF, 100)) |
| 3848 | dev_warn(hsotg->dev, |
| 3849 | "%s: timeout GINTSTS.GINNAKEFF\n", |
| 3850 | __func__); |
| 3851 | } |
| 3852 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3853 | if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF)) |
| 3854 | dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3855 | |
| 3856 | /* Wait for global nak to take effect */ |
| 3857 | if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, |
| 3858 | GINTSTS_GOUTNAKEFF, 100)) |
| 3859 | dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n", |
| 3860 | __func__); |
| 3861 | } |
| 3862 | |
| 3863 | /* Disable ep */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3864 | dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3865 | |
| 3866 | /* Wait for ep to be disabled */ |
| 3867 | if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100)) |
| 3868 | dev_warn(hsotg->dev, |
| 3869 | "%s: timeout DOEPCTL.EPDisable\n", __func__); |
| 3870 | |
| 3871 | /* Clear EPDISBLD interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3872 | dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3873 | |
| 3874 | if (hs_ep->dir_in) { |
| 3875 | unsigned short fifo_index; |
| 3876 | |
| 3877 | if (hsotg->dedicated_fifos || hs_ep->periodic) |
| 3878 | fifo_index = hs_ep->fifo_index; |
| 3879 | else |
| 3880 | fifo_index = 0; |
| 3881 | |
| 3882 | /* Flush TX FIFO */ |
| 3883 | dwc2_flush_tx_fifo(hsotg, fifo_index); |
| 3884 | |
| 3885 | /* Clear Global In NP NAK in Shared FIFO for non periodic ep */ |
| 3886 | if (!hsotg->dedicated_fifos && !hs_ep->periodic) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3887 | dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3888 | |
| 3889 | } else { |
| 3890 | /* Remove global NAKs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3891 | dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 3892 | } |
| 3893 | } |
| 3894 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3895 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3896 | * dwc2_hsotg_ep_enable - enable the given endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3897 | * @ep: The USB endpint to configure |
| 3898 | * @desc: The USB endpoint descriptor to configure with. |
| 3899 | * |
| 3900 | * This is called from the USB gadget code's usb_ep_enable(). |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3901 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3902 | static int dwc2_hsotg_ep_enable(struct usb_ep *ep, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 3903 | const struct usb_endpoint_descriptor *desc) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3904 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3905 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 3906 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3907 | unsigned long flags; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3908 | unsigned int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3909 | u32 epctrl_reg; |
| 3910 | u32 epctrl; |
| 3911 | u32 mps; |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3912 | u32 mc; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 3913 | u32 mask; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 3914 | unsigned int dir_in; |
| 3915 | unsigned int i, val, size; |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 3916 | int ret = 0; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3917 | unsigned char ep_type; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3918 | |
| 3919 | dev_dbg(hsotg->dev, |
| 3920 | "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n", |
| 3921 | __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes, |
| 3922 | desc->wMaxPacketSize, desc->bInterval); |
| 3923 | |
| 3924 | /* not to be called for EP0 */ |
Vahram Aharonyan | 8c3d609 | 2016-04-27 20:20:46 -0700 | [diff] [blame] | 3925 | if (index == 0) { |
| 3926 | dev_err(hsotg->dev, "%s: called for EP 0\n", __func__); |
| 3927 | return -EINVAL; |
| 3928 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3929 | |
| 3930 | dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0; |
| 3931 | if (dir_in != hs_ep->dir_in) { |
| 3932 | dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__); |
| 3933 | return -EINVAL; |
| 3934 | } |
| 3935 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3936 | ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 3937 | mps = usb_endpoint_maxp(desc); |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3938 | mc = usb_endpoint_maxp_mult(desc); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3939 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3940 | /* ISOC IN in DDMA supported bInterval up to 10 */ |
| 3941 | if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC && |
| 3942 | dir_in && desc->bInterval > 10) { |
| 3943 | dev_err(hsotg->dev, |
| 3944 | "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__); |
| 3945 | return -EINVAL; |
| 3946 | } |
| 3947 | |
| 3948 | /* High bandwidth ISOC OUT in DDMA not supported */ |
| 3949 | if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC && |
| 3950 | !dir_in && mc > 1) { |
| 3951 | dev_err(hsotg->dev, |
| 3952 | "%s: ISOC OUT, DDMA: HB not supported!\n", __func__); |
| 3953 | return -EINVAL; |
| 3954 | } |
| 3955 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 3956 | /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3957 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 3958 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 3959 | epctrl = dwc2_readl(hsotg, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3960 | |
| 3961 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n", |
| 3962 | __func__, epctrl, epctrl_reg); |
| 3963 | |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3964 | /* Allocate DMA descriptor chain for non-ctrl endpoints */ |
Vardan Mikayelyan | 9383e08 | 2017-01-05 18:01:48 -0800 | [diff] [blame] | 3965 | if (using_desc_dma(hsotg) && !hs_ep->desc_list) { |
| 3966 | hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev, |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3967 | MAX_DMA_DESC_NUM_GENERIC * |
| 3968 | sizeof(struct dwc2_dma_desc), |
Marek Szyprowski | 86e881e | 2016-12-01 10:02:11 +0100 | [diff] [blame] | 3969 | &hs_ep->desc_list_dma, GFP_ATOMIC); |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 3970 | if (!hs_ep->desc_list) { |
| 3971 | ret = -ENOMEM; |
| 3972 | goto error2; |
| 3973 | } |
| 3974 | } |
| 3975 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 3976 | spin_lock_irqsave(&hsotg->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3977 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3978 | epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK); |
| 3979 | epctrl |= DXEPCTL_MPS(mps); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3980 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 3981 | /* |
| 3982 | * mark the endpoint as active, otherwise the core may ignore |
| 3983 | * transactions entirely for this endpoint |
| 3984 | */ |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3985 | epctrl |= DXEPCTL_USBACTEP; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3986 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3987 | /* update the endpoint state */ |
Vardan Mikayelyan | ee2c40d | 2016-11-08 10:57:00 -0800 | [diff] [blame] | 3988 | 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] | 3989 | |
| 3990 | /* default, set to non-periodic */ |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3991 | hs_ep->isochronous = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3992 | hs_ep->periodic = 0; |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 3993 | hs_ep->halted = 0; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 3994 | hs_ep->interval = desc->bInterval; |
Robert Baldyga | 4fca54a | 2013-10-09 09:00:02 +0200 | [diff] [blame] | 3995 | |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 3996 | switch (ep_type) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 3997 | case USB_ENDPOINT_XFER_ISOC: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 3998 | epctrl |= DXEPCTL_EPTYPE_ISO; |
| 3999 | epctrl |= DXEPCTL_SETEVENFR; |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 4000 | hs_ep->isochronous = 1; |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 4001 | hs_ep->interval = 1 << (desc->bInterval - 1); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4002 | hs_ep->target_frame = TARGET_FRAME_INITIAL; |
Vahram Aharonyan | ab7d219 | 2016-11-14 19:16:36 -0800 | [diff] [blame] | 4003 | hs_ep->next_desc = 0; |
Minas Harutyunyan | 729cac6 | 2018-05-03 17:24:28 +0400 | [diff] [blame] | 4004 | hs_ep->compl_desc = 0; |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4005 | if (dir_in) { |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 4006 | hs_ep->periodic = 1; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4007 | mask = dwc2_readl(hsotg, DIEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4008 | mask |= DIEPMSK_NAKMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4009 | dwc2_writel(hsotg, mask, DIEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4010 | } else { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4011 | mask = dwc2_readl(hsotg, DOEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4012 | mask |= DOEPMSK_OUTTKNEPDISMSK; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4013 | dwc2_writel(hsotg, mask, DOEPMSK); |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4014 | } |
Robert Baldyga | 1479e84 | 2013-10-09 08:41:57 +0200 | [diff] [blame] | 4015 | break; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4016 | |
| 4017 | case USB_ENDPOINT_XFER_BULK: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4018 | epctrl |= DXEPCTL_EPTYPE_BULK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4019 | break; |
| 4020 | |
| 4021 | case USB_ENDPOINT_XFER_INT: |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4022 | if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4023 | hs_ep->periodic = 1; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4024 | |
Vardan Mikayelyan | 142bd33 | 2016-05-25 18:07:07 -0700 | [diff] [blame] | 4025 | if (hsotg->gadget.speed == USB_SPEED_HIGH) |
| 4026 | hs_ep->interval = 1 << (desc->bInterval - 1); |
| 4027 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4028 | epctrl |= DXEPCTL_EPTYPE_INTERRUPT; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4029 | break; |
| 4030 | |
| 4031 | case USB_ENDPOINT_XFER_CONTROL: |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4032 | epctrl |= DXEPCTL_EPTYPE_CONTROL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4033 | break; |
| 4034 | } |
| 4035 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4036 | /* |
| 4037 | * if the hardware has dedicated fifos, we must give each IN EP |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4038 | * a unique tx-fifo even if it is non-periodic. |
| 4039 | */ |
Robert Baldyga | 21f3bb5 | 2016-08-29 13:38:57 -0700 | [diff] [blame] | 4040 | if (dir_in && hsotg->dedicated_fifos) { |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 4041 | u32 fifo_index = 0; |
| 4042 | u32 fifo_size = UINT_MAX; |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4043 | |
| 4044 | size = hs_ep->ep.maxpacket * hs_ep->mc; |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 4045 | for (i = 1; i < hsotg->num_of_eps; ++i) { |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4046 | if (hsotg->fifo_map & (1 << i)) |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4047 | continue; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4048 | val = dwc2_readl(hsotg, DPTXFSIZN(i)); |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4049 | val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4050 | if (val < size) |
| 4051 | continue; |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 4052 | /* Search for smallest acceptable fifo */ |
| 4053 | if (val < fifo_size) { |
| 4054 | fifo_size = val; |
| 4055 | fifo_index = i; |
| 4056 | } |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4057 | } |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 4058 | if (!fifo_index) { |
Mian Yousaf Kaukab | 5f2196b | 2015-01-09 13:38:56 +0100 | [diff] [blame] | 4059 | dev_err(hsotg->dev, |
| 4060 | "%s: No suitable fifo found\n", __func__); |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 4061 | ret = -ENOMEM; |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4062 | goto error1; |
Sudip Mukherjee | b585a48 | 2014-10-17 10:14:02 +0530 | [diff] [blame] | 4063 | } |
Minas Harutyunyan | 97311c8 | 2019-01-31 18:28:07 +0400 | [diff] [blame] | 4064 | epctrl &= ~(DXEPCTL_TXFNUM_LIMIT << DXEPCTL_TXFNUM_SHIFT); |
Mian Yousaf Kaukab | ca4c55a | 2015-01-09 13:39:04 +0100 | [diff] [blame] | 4065 | hsotg->fifo_map |= 1 << fifo_index; |
| 4066 | epctrl |= DXEPCTL_TXFNUM(fifo_index); |
| 4067 | hs_ep->fifo_index = fifo_index; |
| 4068 | hs_ep->fifo_size = fifo_size; |
Robert Baldyga | b203d0a | 2014-09-09 10:44:56 +0200 | [diff] [blame] | 4069 | } |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4070 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4071 | /* for non control endpoints, set PID to D0 */ |
Vardan Mikayelyan | 837e9f0 | 2016-05-25 18:07:22 -0700 | [diff] [blame] | 4072 | if (index && !hs_ep->isochronous) |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4073 | epctrl |= DXEPCTL_SETD0PID; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4074 | |
Artur Petrosyan | 5295322 | 2018-04-16 08:45:31 -0400 | [diff] [blame] | 4075 | /* WA for Full speed ISOC IN in DDMA mode. |
| 4076 | * By Clear NAK status of EP, core will send ZLP |
| 4077 | * to IN token and assert NAK interrupt relying |
| 4078 | * on TxFIFO status only |
| 4079 | */ |
| 4080 | |
| 4081 | if (hsotg->gadget.speed == USB_SPEED_FULL && |
| 4082 | hs_ep->isochronous && dir_in) { |
| 4083 | /* The WA applies only to core versions from 2.72a |
| 4084 | * to 4.00a (including both). Also for FS_IOT_1.00a |
| 4085 | * and HS_IOT_1.00a. |
| 4086 | */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4087 | u32 gsnpsid = dwc2_readl(hsotg, GSNPSID); |
Artur Petrosyan | 5295322 | 2018-04-16 08:45:31 -0400 | [diff] [blame] | 4088 | |
| 4089 | if ((gsnpsid >= DWC2_CORE_REV_2_72a && |
| 4090 | gsnpsid <= DWC2_CORE_REV_4_00a) || |
| 4091 | gsnpsid == DWC2_FS_IOT_REV_1_00a || |
| 4092 | gsnpsid == DWC2_HS_IOT_REV_1_00a) |
| 4093 | epctrl |= DXEPCTL_CNAK; |
| 4094 | } |
| 4095 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4096 | dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n", |
| 4097 | __func__, epctrl); |
| 4098 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4099 | dwc2_writel(hsotg, epctrl, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4100 | dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4101 | __func__, dwc2_readl(hsotg, epctrl_reg)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4102 | |
| 4103 | /* enable the endpoint interrupt */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4104 | dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4105 | |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4106 | error1: |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4107 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4108 | |
| 4109 | error2: |
| 4110 | if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) { |
Vardan Mikayelyan | 9383e08 | 2017-01-05 18:01:48 -0800 | [diff] [blame] | 4111 | dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC * |
Vahram Aharonyan | 5f54c54 | 2016-11-09 19:28:03 -0800 | [diff] [blame] | 4112 | sizeof(struct dwc2_dma_desc), |
| 4113 | hs_ep->desc_list, hs_ep->desc_list_dma); |
| 4114 | hs_ep->desc_list = NULL; |
| 4115 | } |
| 4116 | |
Julia Lawall | 19c190f | 2010-03-29 17:36:44 +0200 | [diff] [blame] | 4117 | return ret; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4118 | } |
| 4119 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4120 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4121 | * dwc2_hsotg_ep_disable - disable given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4122 | * @ep: The endpoint to disable. |
| 4123 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4124 | static int dwc2_hsotg_ep_disable(struct usb_ep *ep) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4125 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4126 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4127 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4128 | int dir_in = hs_ep->dir_in; |
| 4129 | int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4130 | u32 epctrl_reg; |
| 4131 | u32 ctrl; |
| 4132 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 4133 | dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4134 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4135 | if (ep == &hsotg->eps_out[0]->ep) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4136 | dev_err(hsotg->dev, "%s: called for ep0\n", __func__); |
| 4137 | return -EINVAL; |
| 4138 | } |
| 4139 | |
John Stultz | 9b481092 | 2017-10-23 14:32:49 -0700 | [diff] [blame] | 4140 | if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { |
| 4141 | dev_err(hsotg->dev, "%s: called in host mode?\n", __func__); |
| 4142 | return -EINVAL; |
| 4143 | } |
| 4144 | |
Lukasz Majewski | 94cb8fd | 2012-05-04 14:17:14 +0200 | [diff] [blame] | 4145 | epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4146 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4147 | ctrl = dwc2_readl(hsotg, epctrl_reg); |
Vahram Aharonyan | a4f82771 | 2016-11-14 19:16:53 -0800 | [diff] [blame] | 4148 | |
| 4149 | if (ctrl & DXEPCTL_EPENA) |
| 4150 | dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep); |
| 4151 | |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4152 | ctrl &= ~DXEPCTL_EPENA; |
| 4153 | ctrl &= ~DXEPCTL_USBACTEP; |
| 4154 | ctrl |= DXEPCTL_SNAK; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4155 | |
| 4156 | dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4157 | dwc2_writel(hsotg, ctrl, epctrl_reg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4158 | |
| 4159 | /* disable endpoint interrupts */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4160 | 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] | 4161 | |
Mian Yousaf Kaukab | 1141ea0 | 2015-01-09 13:38:57 +0100 | [diff] [blame] | 4162 | /* terminate all requests with shutdown */ |
| 4163 | kill_all_requests(hsotg, hs_ep, -ESHUTDOWN); |
| 4164 | |
Robert Baldyga | 1c07b20 | 2016-08-29 13:39:00 -0700 | [diff] [blame] | 4165 | hsotg->fifo_map &= ~(1 << hs_ep->fifo_index); |
| 4166 | hs_ep->fifo_index = 0; |
| 4167 | hs_ep->fifo_size = 0; |
| 4168 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4169 | return 0; |
| 4170 | } |
| 4171 | |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4172 | static int dwc2_hsotg_ep_disable_lock(struct usb_ep *ep) |
| 4173 | { |
| 4174 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
| 4175 | struct dwc2_hsotg *hsotg = hs_ep->parent; |
| 4176 | unsigned long flags; |
| 4177 | int ret; |
| 4178 | |
| 4179 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4180 | ret = dwc2_hsotg_ep_disable(ep); |
| 4181 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4182 | return ret; |
| 4183 | } |
| 4184 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4185 | /** |
| 4186 | * on_list - check request is on the given endpoint |
| 4187 | * @ep: The endpoint to check. |
| 4188 | * @test: The request to test if it is on the endpoint. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4189 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4190 | 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] | 4191 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4192 | struct dwc2_hsotg_req *req, *treq; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4193 | |
| 4194 | list_for_each_entry_safe(req, treq, &ep->queue, queue) { |
| 4195 | if (req == test) |
| 4196 | return true; |
| 4197 | } |
| 4198 | |
| 4199 | return false; |
| 4200 | } |
| 4201 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4202 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4203 | * dwc2_hsotg_ep_dequeue - dequeue given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4204 | * @ep: The endpoint to dequeue. |
| 4205 | * @req: The request to be removed from a queue. |
| 4206 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4207 | 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] | 4208 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4209 | struct dwc2_hsotg_req *hs_req = our_req(req); |
| 4210 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4211 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4212 | unsigned long flags; |
| 4213 | |
Marek Szyprowski | 1e01129 | 2014-09-09 10:44:54 +0200 | [diff] [blame] | 4214 | dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4215 | |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4216 | spin_lock_irqsave(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4217 | |
| 4218 | if (!on_list(hs_ep, hs_req)) { |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4219 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4220 | return -EINVAL; |
| 4221 | } |
| 4222 | |
Mian Yousaf Kaukab | c524dd5 | 2015-09-29 12:08:24 +0200 | [diff] [blame] | 4223 | /* Dequeue already started request */ |
| 4224 | if (req == &hs_ep->req->req) |
| 4225 | dwc2_hsotg_ep_stop_xfr(hs, hs_ep); |
| 4226 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4227 | dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET); |
Lukasz Majewski | 22258f4 | 2012-06-14 10:02:24 +0200 | [diff] [blame] | 4228 | spin_unlock_irqrestore(&hs->lock, flags); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4229 | |
| 4230 | return 0; |
| 4231 | } |
| 4232 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4233 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4234 | * dwc2_hsotg_ep_sethalt - set halt on a given endpoint |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4235 | * @ep: The endpoint to set halt. |
| 4236 | * @value: Set or unset the halt. |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4237 | * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if |
| 4238 | * the endpoint is busy processing requests. |
| 4239 | * |
| 4240 | * We need to stall the endpoint immediately if request comes from set_feature |
| 4241 | * protocol command handler. |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4242 | */ |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4243 | 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] | 4244 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4245 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4246 | struct dwc2_hsotg *hs = hs_ep->parent; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4247 | int index = hs_ep->index; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4248 | u32 epreg; |
| 4249 | u32 epctl; |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4250 | u32 xfertype; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4251 | |
| 4252 | dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value); |
| 4253 | |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 4254 | if (index == 0) { |
| 4255 | if (value) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4256 | dwc2_hsotg_stall_ep0(hs); |
Robert Baldyga | c9f721b | 2014-01-14 08:36:00 +0100 | [diff] [blame] | 4257 | else |
| 4258 | dev_warn(hs->dev, |
| 4259 | "%s: can't clear halt on ep0\n", __func__); |
| 4260 | return 0; |
| 4261 | } |
| 4262 | |
Vahram Aharonyan | 15186f1 | 2016-05-23 22:41:59 -0700 | [diff] [blame] | 4263 | if (hs_ep->isochronous) { |
| 4264 | dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name); |
| 4265 | return -EINVAL; |
| 4266 | } |
| 4267 | |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4268 | if (!now && value && !list_empty(&hs_ep->queue)) { |
| 4269 | dev_dbg(hs->dev, "%s request is pending, cannot halt\n", |
| 4270 | ep->name); |
| 4271 | return -EAGAIN; |
| 4272 | } |
| 4273 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4274 | if (hs_ep->dir_in) { |
| 4275 | epreg = DIEPCTL(index); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4276 | epctl = dwc2_readl(hs, epreg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4277 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4278 | if (value) { |
Felipe Balbi | 5a350d5 | 2015-06-29 20:17:22 -0500 | [diff] [blame] | 4279 | epctl |= DXEPCTL_STALL | DXEPCTL_SNAK; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4280 | if (epctl & DXEPCTL_EPENA) |
| 4281 | epctl |= DXEPCTL_EPDIS; |
| 4282 | } else { |
| 4283 | epctl &= ~DXEPCTL_STALL; |
| 4284 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 4285 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4286 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 4287 | epctl |= DXEPCTL_SETD0PID; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4288 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4289 | dwc2_writel(hs, epctl, epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4290 | } else { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4291 | epreg = DOEPCTL(index); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4292 | epctl = dwc2_readl(hs, epreg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4293 | |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 4294 | if (value) { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4295 | epctl |= DXEPCTL_STALL; |
John Youn | 34c0887f | 2017-01-17 20:31:43 -0800 | [diff] [blame] | 4296 | } else { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4297 | epctl &= ~DXEPCTL_STALL; |
| 4298 | xfertype = epctl & DXEPCTL_EPTYPE_MASK; |
| 4299 | if (xfertype == DXEPCTL_EPTYPE_BULK || |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4300 | xfertype == DXEPCTL_EPTYPE_INTERRUPT) |
John Youn | 77b6200 | 2017-01-17 20:32:12 -0800 | [diff] [blame] | 4301 | epctl |= DXEPCTL_SETD0PID; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4302 | } |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4303 | dwc2_writel(hs, epctl, epreg); |
Anton Tikhomirov | 9c39ddc | 2011-04-21 17:06:41 +0900 | [diff] [blame] | 4304 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4305 | |
Robert Baldyga | a18ed7b | 2013-09-19 11:50:21 +0200 | [diff] [blame] | 4306 | hs_ep->halted = value; |
| 4307 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4308 | return 0; |
| 4309 | } |
| 4310 | |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4311 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4312 | * 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] | 4313 | * @ep: The endpoint to set halt. |
| 4314 | * @value: Set or unset the halt. |
| 4315 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4316 | 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] | 4317 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4318 | struct dwc2_hsotg_ep *hs_ep = our_ep(ep); |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4319 | struct dwc2_hsotg *hs = hs_ep->parent; |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4320 | unsigned long flags = 0; |
| 4321 | int ret = 0; |
| 4322 | |
| 4323 | spin_lock_irqsave(&hs->lock, flags); |
Vahram Aharonyan | 51da43b | 2016-05-23 22:41:57 -0700 | [diff] [blame] | 4324 | ret = dwc2_hsotg_ep_sethalt(ep, value, false); |
Lukasz Majewski | 5ad1d31 | 2012-06-14 10:02:26 +0200 | [diff] [blame] | 4325 | spin_unlock_irqrestore(&hs->lock, flags); |
| 4326 | |
| 4327 | return ret; |
| 4328 | } |
| 4329 | |
Bhumika Goyal | ebce561 | 2017-08-12 17:34:55 +0530 | [diff] [blame] | 4330 | static const struct usb_ep_ops dwc2_hsotg_ep_ops = { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4331 | .enable = dwc2_hsotg_ep_enable, |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4332 | .disable = dwc2_hsotg_ep_disable_lock, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4333 | .alloc_request = dwc2_hsotg_ep_alloc_request, |
| 4334 | .free_request = dwc2_hsotg_ep_free_request, |
| 4335 | .queue = dwc2_hsotg_ep_queue_lock, |
| 4336 | .dequeue = dwc2_hsotg_ep_dequeue, |
| 4337 | .set_halt = dwc2_hsotg_ep_sethalt_lock, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4338 | /* note, don't believe we have any call for the fifo routines */ |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4339 | }; |
| 4340 | |
| 4341 | /** |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4342 | * dwc2_hsotg_init - initialize the usb core |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4343 | * @hsotg: The driver state |
| 4344 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4345 | static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg) |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4346 | { |
| 4347 | /* unmask subset of endpoint interrupts */ |
| 4348 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4349 | dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4350 | DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4351 | DIEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4352 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4353 | dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK | |
Antti Seppälä | 95c8bc3 | 2015-08-20 21:41:07 +0300 | [diff] [blame] | 4354 | DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK, |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4355 | DOEPMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4356 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4357 | dwc2_writel(hsotg, 0, DAINTMSK); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4358 | |
| 4359 | /* Be in disconnected state until gadget is registered */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4360 | dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4361 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4362 | /* setup fifos */ |
| 4363 | |
| 4364 | dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4365 | dwc2_readl(hsotg, GRXFSIZ), |
| 4366 | dwc2_readl(hsotg, GNPTXFSIZ)); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4367 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4368 | dwc2_hsotg_init_fifo(hsotg); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4369 | |
Gregory Herrero | f509004 | 2015-01-09 13:38:47 +0100 | [diff] [blame] | 4370 | if (using_dma(hsotg)) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4371 | dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN); |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4372 | } |
| 4373 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4374 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4375 | * dwc2_hsotg_udc_start - prepare the udc for work |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4376 | * @gadget: The usb gadget state |
| 4377 | * @driver: The usb gadget driver |
| 4378 | * |
| 4379 | * Perform initialization to prepare udc device and driver |
| 4380 | * to work. |
| 4381 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4382 | static int dwc2_hsotg_udc_start(struct usb_gadget *gadget, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4383 | struct usb_gadget_driver *driver) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4384 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4385 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4386 | unsigned long flags; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4387 | int ret; |
| 4388 | |
| 4389 | if (!hsotg) { |
Pavel Machek | a023da3 | 2013-09-30 14:56:02 +0200 | [diff] [blame] | 4390 | pr_err("%s: called with no device\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4391 | return -ENODEV; |
| 4392 | } |
| 4393 | |
| 4394 | if (!driver) { |
| 4395 | dev_err(hsotg->dev, "%s: no driver\n", __func__); |
| 4396 | return -EINVAL; |
| 4397 | } |
| 4398 | |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 4399 | if (driver->max_speed < USB_SPEED_FULL) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4400 | dev_err(hsotg->dev, "%s: bad speed\n", __func__); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4401 | |
Lukasz Majewski | f65f0f1 | 2012-05-04 14:17:10 +0200 | [diff] [blame] | 4402 | if (!driver->setup) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4403 | dev_err(hsotg->dev, "%s: missing entry points\n", __func__); |
| 4404 | return -EINVAL; |
| 4405 | } |
| 4406 | |
| 4407 | WARN_ON(hsotg->driver); |
| 4408 | |
| 4409 | driver->driver.bus = NULL; |
| 4410 | hsotg->driver = driver; |
Alexandre Pereira da Silva | 7d7b229 | 2012-06-26 11:27:10 -0300 | [diff] [blame] | 4411 | hsotg->gadget.dev.of_node = hsotg->dev->of_node; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4412 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4413 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4414 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) { |
| 4415 | ret = dwc2_lowlevel_hw_enable(hsotg); |
| 4416 | if (ret) |
| 4417 | goto err; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4418 | } |
| 4419 | |
Gregory Herrero | f6c0159 | 2015-01-09 13:38:41 +0100 | [diff] [blame] | 4420 | if (!IS_ERR_OR_NULL(hsotg->uphy)) |
| 4421 | otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 4422 | |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4423 | spin_lock_irqsave(&hsotg->lock, flags); |
John Youn | d0f0ac5 | 2016-09-07 19:39:37 -0700 | [diff] [blame] | 4424 | if (dwc2_hw_is_device(hsotg)) { |
| 4425 | dwc2_hsotg_init(hsotg); |
| 4426 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
| 4427 | } |
| 4428 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4429 | hsotg->enabled = 0; |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4430 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4431 | |
Andrzej Pietrasiewicz | 10209ab | 2019-01-21 14:44:47 +0100 | [diff] [blame] | 4432 | gadget->sg_supported = using_desc_dma(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4433 | dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name); |
Marek Szyprowski | 5b9451f | 2014-10-20 12:45:38 +0200 | [diff] [blame] | 4434 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4435 | return 0; |
| 4436 | |
| 4437 | err: |
| 4438 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4439 | return ret; |
| 4440 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4441 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4442 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4443 | * dwc2_hsotg_udc_stop - stop the udc |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4444 | * @gadget: The usb gadget state |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4445 | * |
| 4446 | * Stop udc hw block and stay tunned for future transmissions |
| 4447 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4448 | static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4449 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4450 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4451 | unsigned long flags = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4452 | int ep; |
| 4453 | |
| 4454 | if (!hsotg) |
| 4455 | return -ENODEV; |
| 4456 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4457 | /* all endpoints should be shutdown */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4458 | for (ep = 1; ep < hsotg->num_of_eps; ep++) { |
| 4459 | if (hsotg->eps_in[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4460 | dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4461 | if (hsotg->eps_out[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4462 | dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4463 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4464 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4465 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4466 | |
Marek Szyprowski | 32805c3 | 2014-10-20 12:45:33 +0200 | [diff] [blame] | 4467 | hsotg->driver = NULL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4468 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4469 | hsotg->enabled = 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4470 | |
Lukasz Majewski | 2b19a52 | 2012-06-14 10:02:25 +0200 | [diff] [blame] | 4471 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4472 | |
Gregory Herrero | f6c0159 | 2015-01-09 13:38:41 +0100 | [diff] [blame] | 4473 | if (!IS_ERR_OR_NULL(hsotg->uphy)) |
| 4474 | otg_set_peripheral(hsotg->uphy->otg, NULL); |
Marek Szyprowski | c816c47 | 2014-10-20 12:45:37 +0200 | [diff] [blame] | 4475 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4476 | if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 4477 | dwc2_lowlevel_hw_disable(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4478 | |
| 4479 | return 0; |
| 4480 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4481 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4482 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4483 | * dwc2_hsotg_gadget_getframe - read the frame number |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4484 | * @gadget: The usb gadget state |
| 4485 | * |
| 4486 | * Read the {micro} frame number |
| 4487 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4488 | static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4489 | { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4490 | return dwc2_hsotg_read_frameno(to_hsotg(gadget)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4491 | } |
| 4492 | |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4493 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4494 | * dwc2_hsotg_pullup - connect/disconnect the USB PHY |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4495 | * @gadget: The usb gadget state |
| 4496 | * @is_on: Current state of the USB PHY |
| 4497 | * |
| 4498 | * Connect/Disconnect the USB PHY pullup |
| 4499 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4500 | static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on) |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4501 | { |
Dinh Nguyen | 941fcce | 2014-11-11 11:13:33 -0600 | [diff] [blame] | 4502 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4503 | unsigned long flags = 0; |
| 4504 | |
Gregory Herrero | 77ba911 | 2015-09-29 12:08:19 +0200 | [diff] [blame] | 4505 | 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] | 4506 | hsotg->op_state); |
Gregory Herrero | 77ba911 | 2015-09-29 12:08:19 +0200 | [diff] [blame] | 4507 | |
| 4508 | /* Don't modify pullup state while in host mode */ |
| 4509 | if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) { |
| 4510 | hsotg->enabled = is_on; |
| 4511 | return 0; |
| 4512 | } |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4513 | |
| 4514 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4515 | if (is_on) { |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4516 | hsotg->enabled = 1; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4517 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4518 | /* Enable ACG feature in device mode,if supported */ |
| 4519 | dwc2_enable_acg(hsotg); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4520 | dwc2_hsotg_core_connect(hsotg); |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4521 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4522 | dwc2_hsotg_core_disconnect(hsotg); |
| 4523 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4524 | hsotg->enabled = 0; |
Lukasz Majewski | a188b68 | 2012-06-22 09:29:56 +0200 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4528 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4529 | |
| 4530 | return 0; |
| 4531 | } |
| 4532 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4533 | 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] | 4534 | { |
| 4535 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 4536 | unsigned long flags; |
| 4537 | |
| 4538 | dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active); |
| 4539 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4540 | |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4541 | /* |
Vardan Mikayelyan | 41ba9b9 | 2018-02-16 14:06:36 +0400 | [diff] [blame] | 4542 | * If controller is hibernated, it must exit from power_down |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4543 | * before being initialized / de-initialized |
| 4544 | */ |
| 4545 | if (hsotg->lx_state == DWC2_L2) |
Vardan Mikayelyan | 41ba9b9 | 2018-02-16 14:06:36 +0400 | [diff] [blame] | 4546 | dwc2_exit_partial_power_down(hsotg, false); |
Gregory Herrero | 61f7223 | 2015-09-29 12:08:28 +0200 | [diff] [blame] | 4547 | |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4548 | if (is_active) { |
Gregory Herrero | cd0e641 | 2015-09-29 12:08:20 +0200 | [diff] [blame] | 4549 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Gregory Herrero | 065d393 | 2015-09-22 15:16:54 +0200 | [diff] [blame] | 4550 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4551 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4552 | if (hsotg->enabled) { |
| 4553 | /* Enable ACG feature in device mode,if supported */ |
| 4554 | dwc2_enable_acg(hsotg); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4555 | dwc2_hsotg_core_connect(hsotg); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4556 | } |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4557 | } else { |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4558 | dwc2_hsotg_core_disconnect(hsotg); |
| 4559 | dwc2_hsotg_disconnect(hsotg); |
Gregory Herrero | 83d9822 | 2015-01-09 13:39:02 +0100 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | spin_unlock_irqrestore(&hsotg->lock, flags); |
| 4563 | return 0; |
| 4564 | } |
| 4565 | |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 4566 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4567 | * dwc2_hsotg_vbus_draw - report bMaxPower field |
Gregory Herrero | 596d696 | 2015-01-09 13:39:08 +0100 | [diff] [blame] | 4568 | * @gadget: The usb gadget state |
| 4569 | * @mA: Amount of current |
| 4570 | * |
| 4571 | * Report how much power the device may consume to the phy. |
| 4572 | */ |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4573 | 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] | 4574 | { |
| 4575 | struct dwc2_hsotg *hsotg = to_hsotg(gadget); |
| 4576 | |
| 4577 | if (IS_ERR_OR_NULL(hsotg->uphy)) |
| 4578 | return -ENOTSUPP; |
| 4579 | return usb_phy_set_power(hsotg->uphy, mA); |
| 4580 | } |
| 4581 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4582 | static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = { |
| 4583 | .get_frame = dwc2_hsotg_gadget_getframe, |
| 4584 | .udc_start = dwc2_hsotg_udc_start, |
| 4585 | .udc_stop = dwc2_hsotg_udc_stop, |
| 4586 | .pullup = dwc2_hsotg_pullup, |
| 4587 | .vbus_session = dwc2_hsotg_vbus_session, |
| 4588 | .vbus_draw = dwc2_hsotg_vbus_draw, |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4589 | }; |
| 4590 | |
| 4591 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4592 | * dwc2_hsotg_initep - initialise a single endpoint |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4593 | * @hsotg: The device state. |
| 4594 | * @hs_ep: The endpoint to be initialised. |
| 4595 | * @epnum: The endpoint number |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4596 | * @dir_in: True if direction is in. |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4597 | * |
| 4598 | * Initialise the given endpoint (as part of the probe and device state |
| 4599 | * creation) to give to the gadget driver. Setup the endpoint name, any |
| 4600 | * direction information and other state that may be required. |
| 4601 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4602 | static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4603 | struct dwc2_hsotg_ep *hs_ep, |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4604 | int epnum, |
| 4605 | bool dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4606 | { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4607 | char *dir; |
| 4608 | |
| 4609 | if (epnum == 0) |
| 4610 | dir = ""; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4611 | else if (dir_in) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4612 | dir = "in"; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4613 | else |
| 4614 | dir = "out"; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4615 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4616 | hs_ep->dir_in = dir_in; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4617 | hs_ep->index = epnum; |
| 4618 | |
| 4619 | snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir); |
| 4620 | |
| 4621 | INIT_LIST_HEAD(&hs_ep->queue); |
| 4622 | INIT_LIST_HEAD(&hs_ep->ep.ep_list); |
| 4623 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4624 | /* add to the list of endpoints known by the gadget driver */ |
| 4625 | if (epnum) |
| 4626 | list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list); |
| 4627 | |
| 4628 | hs_ep->parent = hsotg; |
| 4629 | hs_ep->ep.name = hs_ep->name; |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 4630 | |
| 4631 | if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW) |
| 4632 | usb_ep_set_maxpacket_limit(&hs_ep->ep, 8); |
| 4633 | else |
| 4634 | usb_ep_set_maxpacket_limit(&hs_ep->ep, |
| 4635 | epnum ? 1024 : EP0_MPS_LIMIT); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4636 | hs_ep->ep.ops = &dwc2_hsotg_ep_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4637 | |
Robert Baldyga | 2954522 | 2015-07-31 16:00:18 +0200 | [diff] [blame] | 4638 | if (epnum == 0) { |
| 4639 | hs_ep->ep.caps.type_control = true; |
| 4640 | } else { |
Vardan Mikayelyan | 38e9002 | 2016-11-14 19:17:03 -0800 | [diff] [blame] | 4641 | if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) { |
| 4642 | hs_ep->ep.caps.type_iso = true; |
| 4643 | hs_ep->ep.caps.type_bulk = true; |
| 4644 | } |
Robert Baldyga | 2954522 | 2015-07-31 16:00:18 +0200 | [diff] [blame] | 4645 | hs_ep->ep.caps.type_int = true; |
| 4646 | } |
| 4647 | |
| 4648 | if (dir_in) |
| 4649 | hs_ep->ep.caps.dir_in = true; |
| 4650 | else |
| 4651 | hs_ep->ep.caps.dir_out = true; |
| 4652 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4653 | /* |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4654 | * 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] | 4655 | * to be something valid. |
| 4656 | */ |
| 4657 | |
| 4658 | if (using_dma(hsotg)) { |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4659 | u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15); |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4660 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4661 | if (dir_in) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4662 | dwc2_writel(hsotg, next, DIEPCTL(epnum)); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4663 | else |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4664 | dwc2_writel(hsotg, next, DOEPCTL(epnum)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4665 | } |
| 4666 | } |
| 4667 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4668 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4669 | * dwc2_hsotg_hw_cfg - read HW configuration registers |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4670 | * @hsotg: Programming view of the DWC_otg controller |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4671 | * |
| 4672 | * Read the USB core HW configuration registers |
| 4673 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4674 | static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4675 | { |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4676 | u32 cfg; |
| 4677 | u32 ep_type; |
| 4678 | u32 i; |
| 4679 | |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4680 | /* check hardware configuration */ |
| 4681 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4682 | hsotg->num_of_eps = hsotg->hw_params.num_dev_ep; |
| 4683 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4684 | /* Add ep0 */ |
| 4685 | hsotg->num_of_eps++; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4686 | |
John Youn | b98866c | 2017-01-17 20:31:58 -0800 | [diff] [blame] | 4687 | hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, |
| 4688 | sizeof(struct dwc2_hsotg_ep), |
| 4689 | GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4690 | if (!hsotg->eps_in[0]) |
| 4691 | return -ENOMEM; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4692 | /* Same dwc2_hsotg_ep is used in both directions for ep0 */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4693 | hsotg->eps_out[0] = hsotg->eps_in[0]; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4694 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4695 | cfg = hsotg->hw_params.dev_ep_dirs; |
Roshan Pius | 251a17f | 2015-02-02 14:55:38 -0800 | [diff] [blame] | 4696 | 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] | 4697 | ep_type = cfg & 3; |
| 4698 | /* Direction in or both */ |
| 4699 | if (!(ep_type & 2)) { |
| 4700 | hsotg->eps_in[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4701 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4702 | if (!hsotg->eps_in[i]) |
| 4703 | return -ENOMEM; |
| 4704 | } |
| 4705 | /* Direction out or both */ |
| 4706 | if (!(ep_type & 1)) { |
| 4707 | hsotg->eps_out[i] = devm_kzalloc(hsotg->dev, |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4708 | sizeof(struct dwc2_hsotg_ep), GFP_KERNEL); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4709 | if (!hsotg->eps_out[i]) |
| 4710 | return -ENOMEM; |
| 4711 | } |
| 4712 | } |
| 4713 | |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4714 | hsotg->fifo_mem = hsotg->hw_params.total_fifo_size; |
| 4715 | hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo; |
Ben Dooks | 10aebc7 | 2010-07-19 09:40:44 +0100 | [diff] [blame] | 4716 | |
Marek Szyprowski | cff9eb7 | 2014-09-09 10:44:55 +0200 | [diff] [blame] | 4717 | dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n", |
| 4718 | hsotg->num_of_eps, |
| 4719 | hsotg->dedicated_fifos ? "dedicated" : "shared", |
| 4720 | hsotg->fifo_mem); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4721 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4722 | } |
| 4723 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4724 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4725 | * dwc2_hsotg_dump - dump state of the udc |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4726 | * @hsotg: Programming view of the DWC_otg controller |
| 4727 | * |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4728 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4729 | static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4730 | { |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 4731 | #ifdef DEBUG |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4732 | struct device *dev = hsotg->dev; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4733 | u32 val; |
| 4734 | int idx; |
| 4735 | |
| 4736 | dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4737 | dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL), |
| 4738 | dwc2_readl(hsotg, DIEPMSK)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4739 | |
Mian Yousaf Kaukab | f889f23 | 2015-01-30 09:09:36 +0100 | [diff] [blame] | 4740 | dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4741 | dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4742 | |
| 4743 | dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4744 | dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4745 | |
| 4746 | /* show periodic fifo settings */ |
| 4747 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 4748 | for (idx = 1; idx < hsotg->num_of_eps; idx++) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4749 | val = dwc2_readl(hsotg, DPTXFSIZN(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4750 | dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx, |
Dinh Nguyen | 47a1685 | 2014-04-14 14:13:34 -0700 | [diff] [blame] | 4751 | val >> FIFOSIZE_DEPTH_SHIFT, |
| 4752 | val & FIFOSIZE_STARTADDR_MASK); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4753 | } |
| 4754 | |
Mian Yousaf Kaukab | 364f8e9 | 2015-01-09 13:38:55 +0100 | [diff] [blame] | 4755 | for (idx = 0; idx < hsotg->num_of_eps; idx++) { |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4756 | dev_info(dev, |
| 4757 | "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] | 4758 | dwc2_readl(hsotg, DIEPCTL(idx)), |
| 4759 | dwc2_readl(hsotg, DIEPTSIZ(idx)), |
| 4760 | dwc2_readl(hsotg, DIEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4761 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4762 | val = dwc2_readl(hsotg, DOEPCTL(idx)); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4763 | dev_info(dev, |
| 4764 | "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] | 4765 | idx, dwc2_readl(hsotg, DOEPCTL(idx)), |
| 4766 | dwc2_readl(hsotg, DOEPTSIZ(idx)), |
| 4767 | dwc2_readl(hsotg, DOEPDMA(idx))); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n", |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4771 | dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE)); |
Mark Brown | 83a0180 | 2011-06-01 17:16:15 +0100 | [diff] [blame] | 4772 | #endif |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4773 | } |
| 4774 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4775 | /** |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4776 | * dwc2_gadget_init - init function for gadget |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4777 | * @hsotg: Programming view of the DWC_otg controller |
| 4778 | * |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4779 | */ |
Vardan Mikayelyan | f376899 | 2017-12-25 15:17:45 +0400 | [diff] [blame] | 4780 | int dwc2_gadget_init(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4781 | { |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4782 | struct device *dev = hsotg->dev; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4783 | int epnum; |
| 4784 | int ret; |
John Youn | 43e9034 | 2015-12-17 11:17:45 -0800 | [diff] [blame] | 4785 | |
Gregory Herrero | 0a17627 | 2015-01-09 13:38:52 +0100 | [diff] [blame] | 4786 | /* Dump fifo information */ |
| 4787 | dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n", |
John Youn | 05ee799 | 2016-11-03 17:56:05 -0700 | [diff] [blame] | 4788 | hsotg->params.g_np_tx_fifo_size); |
| 4789 | 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] | 4790 | |
Michal Nazarewicz | d327ab5 | 2011-11-19 18:27:37 +0100 | [diff] [blame] | 4791 | hsotg->gadget.max_speed = USB_SPEED_HIGH; |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4792 | hsotg->gadget.ops = &dwc2_hsotg_gadget_ops; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4793 | hsotg->gadget.name = dev_name(dev); |
Vardan Mikayelyan | fa389a6 | 2018-02-16 14:08:53 +0400 | [diff] [blame] | 4794 | hsotg->remote_wakeup_allowed = 0; |
John Youn | 7455f8b | 2018-01-24 17:44:51 +0400 | [diff] [blame] | 4795 | |
| 4796 | if (hsotg->params.lpm) |
| 4797 | hsotg->gadget.lpm_capable = true; |
| 4798 | |
Gregory Herrero | 097ee66 | 2015-04-29 22:09:10 +0200 | [diff] [blame] | 4799 | if (hsotg->dr_mode == USB_DR_MODE_OTG) |
| 4800 | hsotg->gadget.is_otg = 1; |
Mian Yousaf Kaukab | ec4cc65 | 2015-09-22 15:16:55 +0200 | [diff] [blame] | 4801 | else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) |
| 4802 | hsotg->op_state = OTG_STATE_B_PERIPHERAL; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4803 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4804 | ret = dwc2_hsotg_hw_cfg(hsotg); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4805 | if (ret) { |
| 4806 | dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4807 | return ret; |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4808 | } |
| 4809 | |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4810 | hsotg->ctrl_buff = devm_kzalloc(hsotg->dev, |
| 4811 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
Wolfram Sang | 8bae0f8 | 2016-08-25 19:39:02 +0200 | [diff] [blame] | 4812 | if (!hsotg->ctrl_buff) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4813 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4814 | |
| 4815 | hsotg->ep0_buff = devm_kzalloc(hsotg->dev, |
| 4816 | DWC2_CTRL_BUFF_SIZE, GFP_KERNEL); |
Wolfram Sang | 8bae0f8 | 2016-08-25 19:39:02 +0200 | [diff] [blame] | 4817 | if (!hsotg->ep0_buff) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4818 | return -ENOMEM; |
Mian Yousaf Kaukab | 3f95001 | 2015-01-09 13:38:44 +0100 | [diff] [blame] | 4819 | |
Vahram Aharonyan | 0f6b80c | 2016-11-09 19:27:56 -0800 | [diff] [blame] | 4820 | if (using_desc_dma(hsotg)) { |
| 4821 | ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg); |
| 4822 | if (ret < 0) |
| 4823 | return ret; |
| 4824 | } |
| 4825 | |
Vardan Mikayelyan | f376899 | 2017-12-25 15:17:45 +0400 | [diff] [blame] | 4826 | ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq, |
| 4827 | IRQF_SHARED, dev_name(hsotg->dev), hsotg); |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 4828 | if (ret < 0) { |
Dinh Nguyen | db8178c | 2014-11-11 11:13:37 -0600 | [diff] [blame] | 4829 | dev_err(dev, "cannot claim IRQ for gadget\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4830 | return ret; |
Marek Szyprowski | eb3c56c | 2014-09-09 10:44:12 +0200 | [diff] [blame] | 4831 | } |
| 4832 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4833 | /* hsotg->num_of_eps holds number of EPs other than ep0 */ |
| 4834 | |
| 4835 | if (hsotg->num_of_eps == 0) { |
| 4836 | dev_err(dev, "wrong number of EPs (zero)\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4837 | return -EINVAL; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4838 | } |
| 4839 | |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4840 | /* setup endpoint information */ |
| 4841 | |
| 4842 | INIT_LIST_HEAD(&hsotg->gadget.ep_list); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4843 | hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4844 | |
| 4845 | /* allocate EP0 request */ |
| 4846 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4847 | 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] | 4848 | GFP_KERNEL); |
| 4849 | if (!hsotg->ctrl_req) { |
| 4850 | dev_err(dev, "failed to allocate ctrl req\n"); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4851 | return -ENOMEM; |
Lukasz Majewski | b3f489b | 2012-05-04 14:17:09 +0200 | [diff] [blame] | 4852 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4853 | |
| 4854 | /* initialise the endpoints now the core has been initialised */ |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4855 | for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) { |
| 4856 | if (hsotg->eps_in[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4857 | dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4858 | epnum, 1); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4859 | if (hsotg->eps_out[epnum]) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4860 | dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum], |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 4861 | epnum, 0); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4862 | } |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4863 | |
Dinh Nguyen | 117777b | 2014-11-11 11:13:34 -0600 | [diff] [blame] | 4864 | ret = usb_add_gadget_udc(dev, &hsotg->gadget); |
Grigor Tovmasyan | 9bb073a | 2018-05-24 18:22:30 +0400 | [diff] [blame] | 4865 | if (ret) { |
| 4866 | dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, |
| 4867 | hsotg->ctrl_req); |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4868 | return ret; |
Grigor Tovmasyan | 9bb073a | 2018-05-24 18:22:30 +0400 | [diff] [blame] | 4869 | } |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4870 | dwc2_hsotg_dump(hsotg); |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4871 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4872 | return 0; |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4873 | } |
| 4874 | |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4875 | /** |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4876 | * dwc2_hsotg_remove - remove function for hsotg driver |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 4877 | * @hsotg: Programming view of the DWC_otg controller |
| 4878 | * |
Lukasz Majewski | 8b9bc46 | 2012-05-04 14:17:11 +0200 | [diff] [blame] | 4879 | */ |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4880 | int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg) |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4881 | { |
Sebastian Andrzej Siewior | 0f91349 | 2011-06-28 16:33:47 +0300 | [diff] [blame] | 4882 | usb_del_gadget_udc(&hsotg->gadget); |
Grigor Tovmasyan | 9bb073a | 2018-05-24 18:22:30 +0400 | [diff] [blame] | 4883 | 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] | 4884 | |
Ben Dooks | 5b7d70c | 2009-06-02 14:58:06 +0100 | [diff] [blame] | 4885 | return 0; |
| 4886 | } |
| 4887 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4888 | int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4889 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4890 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4891 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4892 | if (hsotg->lx_state != DWC2_L0) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4893 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4894 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4895 | if (hsotg->driver) { |
| 4896 | int ep; |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4897 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4898 | dev_info(hsotg->dev, "suspending usb gadget %s\n", |
| 4899 | hsotg->driver->driver.name); |
| 4900 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4901 | spin_lock_irqsave(&hsotg->lock, flags); |
| 4902 | if (hsotg->enabled) |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4903 | dwc2_hsotg_core_disconnect(hsotg); |
| 4904 | dwc2_hsotg_disconnect(hsotg); |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4905 | hsotg->gadget.speed = USB_SPEED_UNKNOWN; |
| 4906 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4907 | |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4908 | for (ep = 0; ep < hsotg->num_of_eps; ep++) { |
| 4909 | if (hsotg->eps_in[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4910 | dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4911 | if (hsotg->eps_out[ep]) |
Minas Harutyunyan | 4fe4f9f | 2018-12-10 18:09:32 +0400 | [diff] [blame] | 4912 | dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep); |
Mian Yousaf Kaukab | c6f5c05 | 2015-01-09 13:38:50 +0100 | [diff] [blame] | 4913 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4914 | } |
| 4915 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4916 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4917 | } |
| 4918 | |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4919 | int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg) |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4920 | { |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4921 | unsigned long flags; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4922 | |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4923 | if (hsotg->lx_state == DWC2_L2) |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4924 | return 0; |
Gregory Herrero | 9e77977 | 2015-04-29 22:09:07 +0200 | [diff] [blame] | 4925 | |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4926 | if (hsotg->driver) { |
| 4927 | dev_info(hsotg->dev, "resuming usb gadget %s\n", |
| 4928 | hsotg->driver->driver.name); |
Robert Baldyga | d00b414 | 2014-09-09 10:44:57 +0200 | [diff] [blame] | 4929 | |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4930 | spin_lock_irqsave(&hsotg->lock, flags); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4931 | dwc2_hsotg_core_init_disconnected(hsotg, false); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4932 | if (hsotg->enabled) { |
| 4933 | /* Enable ACG feature in device mode,if supported */ |
| 4934 | dwc2_enable_acg(hsotg); |
Felipe Balbi | 1f91b4c | 2015-08-06 18:11:54 -0500 | [diff] [blame] | 4935 | dwc2_hsotg_core_connect(hsotg); |
Razmik Karapetyan | 66e77a2 | 2018-01-24 17:40:29 +0400 | [diff] [blame] | 4936 | } |
Marek Szyprowski | dc6e69e | 2014-11-21 15:14:49 +0100 | [diff] [blame] | 4937 | spin_unlock_irqrestore(&hsotg->lock, flags); |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4938 | } |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4939 | |
Marek Szyprowski | 09a75e8 | 2015-10-14 08:52:29 +0200 | [diff] [blame] | 4940 | return 0; |
Marek Szyprowski | b83e333 | 2014-02-28 13:06:11 +0100 | [diff] [blame] | 4941 | } |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4942 | |
| 4943 | /** |
| 4944 | * dwc2_backup_device_registers() - Backup controller device registers. |
| 4945 | * When suspending usb bus, registers needs to be backuped |
| 4946 | * if controller power is disabled once suspended. |
| 4947 | * |
| 4948 | * @hsotg: Programming view of the DWC_otg controller |
| 4949 | */ |
| 4950 | int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg) |
| 4951 | { |
| 4952 | struct dwc2_dregs_backup *dr; |
| 4953 | int i; |
| 4954 | |
| 4955 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 4956 | |
| 4957 | /* Backup dev regs */ |
| 4958 | dr = &hsotg->dr_backup; |
| 4959 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4960 | dr->dcfg = dwc2_readl(hsotg, DCFG); |
| 4961 | dr->dctl = dwc2_readl(hsotg, DCTL); |
| 4962 | dr->daintmsk = dwc2_readl(hsotg, DAINTMSK); |
| 4963 | dr->diepmsk = dwc2_readl(hsotg, DIEPMSK); |
| 4964 | dr->doepmsk = dwc2_readl(hsotg, DOEPMSK); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4965 | |
| 4966 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 4967 | /* Backup IN EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4968 | dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4969 | |
| 4970 | /* Ensure DATA PID is correctly configured */ |
| 4971 | if (dr->diepctl[i] & DXEPCTL_DPID) |
| 4972 | dr->diepctl[i] |= DXEPCTL_SETD1PID; |
| 4973 | else |
| 4974 | dr->diepctl[i] |= DXEPCTL_SETD0PID; |
| 4975 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4976 | dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i)); |
| 4977 | dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4978 | |
| 4979 | /* Backup OUT EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4980 | dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4981 | |
| 4982 | /* Ensure DATA PID is correctly configured */ |
| 4983 | if (dr->doepctl[i] & DXEPCTL_DPID) |
| 4984 | dr->doepctl[i] |= DXEPCTL_SETD1PID; |
| 4985 | else |
| 4986 | dr->doepctl[i] |= DXEPCTL_SETD0PID; |
| 4987 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 4988 | dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i)); |
| 4989 | dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i)); |
| 4990 | dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 4991 | } |
| 4992 | dr->valid = true; |
| 4993 | return 0; |
| 4994 | } |
| 4995 | |
| 4996 | /** |
| 4997 | * dwc2_restore_device_registers() - Restore controller device registers. |
| 4998 | * When resuming usb bus, device registers needs to be restored |
| 4999 | * if controller power were disabled. |
| 5000 | * |
| 5001 | * @hsotg: Programming view of the DWC_otg controller |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5002 | * @remote_wakeup: Indicates whether resume is initiated by Device or Host. |
| 5003 | * |
| 5004 | * Return: 0 if successful, negative error code otherwise |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5005 | */ |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5006 | int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup) |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5007 | { |
| 5008 | struct dwc2_dregs_backup *dr; |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5009 | int i; |
| 5010 | |
| 5011 | dev_dbg(hsotg->dev, "%s\n", __func__); |
| 5012 | |
| 5013 | /* Restore dev regs */ |
| 5014 | dr = &hsotg->dr_backup; |
| 5015 | if (!dr->valid) { |
| 5016 | dev_err(hsotg->dev, "%s: no device registers to restore\n", |
| 5017 | __func__); |
| 5018 | return -EINVAL; |
| 5019 | } |
| 5020 | dr->valid = false; |
| 5021 | |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5022 | if (!remote_wakeup) |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5023 | dwc2_writel(hsotg, dr->dctl, DCTL); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5024 | |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5025 | dwc2_writel(hsotg, dr->daintmsk, DAINTMSK); |
| 5026 | dwc2_writel(hsotg, dr->diepmsk, DIEPMSK); |
| 5027 | dwc2_writel(hsotg, dr->doepmsk, DOEPMSK); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5028 | |
| 5029 | for (i = 0; i < hsotg->num_of_eps; i++) { |
| 5030 | /* Restore IN EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5031 | dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i)); |
| 5032 | dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i)); |
| 5033 | dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i)); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5034 | /** WA for enabled EPx's IN in DDMA mode. On entering to |
| 5035 | * hibernation wrong value read and saved from DIEPDMAx, |
| 5036 | * as result BNA interrupt asserted on hibernation exit |
| 5037 | * by restoring from saved area. |
| 5038 | */ |
| 5039 | if (hsotg->params.g_dma_desc && |
| 5040 | (dr->diepctl[i] & DXEPCTL_EPENA)) |
| 5041 | dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5042 | dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i)); |
| 5043 | dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i)); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5044 | /* Restore OUT EPs */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5045 | dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i)); |
Vardan Mikayelyan | 9a5d281 | 2018-02-16 14:08:00 +0400 | [diff] [blame] | 5046 | /* WA for enabled EPx's OUT in DDMA mode. On entering to |
| 5047 | * hibernation wrong value read and saved from DOEPDMAx, |
| 5048 | * as result BNA interrupt asserted on hibernation exit |
| 5049 | * by restoring from saved area. |
| 5050 | */ |
| 5051 | if (hsotg->params.g_dma_desc && |
| 5052 | (dr->doepctl[i] & DXEPCTL_EPENA)) |
| 5053 | dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5054 | dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i)); |
| 5055 | dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i)); |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5056 | } |
| 5057 | |
John Youn | 58e52ff6a | 2016-02-23 19:54:57 -0800 | [diff] [blame] | 5058 | return 0; |
| 5059 | } |
Sevak Arakelyan | 21b0340 | 2018-01-24 17:43:32 +0400 | [diff] [blame] | 5060 | |
| 5061 | /** |
| 5062 | * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode |
| 5063 | * |
| 5064 | * @hsotg: Programming view of DWC_otg controller |
| 5065 | * |
| 5066 | */ |
| 5067 | void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg) |
| 5068 | { |
| 5069 | u32 val; |
| 5070 | |
| 5071 | if (!hsotg->params.lpm) |
| 5072 | return; |
| 5073 | |
| 5074 | val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES; |
| 5075 | val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0; |
| 5076 | val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0; |
| 5077 | val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT; |
| 5078 | val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0; |
Minas Harutyunyan | 4663756 | 2019-04-18 15:40:43 +0400 | [diff] [blame] | 5079 | val |= GLPMCFG_LPM_REJECT_CTRL_CONTROL; |
Artur Petrosyan | 9aed8c0 | 2018-11-02 11:29:55 -0400 | [diff] [blame] | 5080 | val |= GLPMCFG_LPM_ACCEPT_CTRL_ISOC; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5081 | dwc2_writel(hsotg, val, GLPMCFG); |
| 5082 | dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG)); |
Grigor Tovmasyan | 4abe453 | 2018-08-29 21:02:28 +0400 | [diff] [blame] | 5083 | |
| 5084 | /* Unmask WKUP_ALERT Interrupt */ |
| 5085 | if (hsotg->params.service_interval) |
| 5086 | dwc2_set_bit(hsotg, GINTMSK2, GINTMSK2_WKUP_ALERT_INT_MSK); |
Sevak Arakelyan | 21b0340 | 2018-01-24 17:43:32 +0400 | [diff] [blame] | 5087 | } |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5088 | |
| 5089 | /** |
Grigor Tovmasyan | 15d9dbf | 2018-08-29 21:01:59 +0400 | [diff] [blame] | 5090 | * dwc2_gadget_program_ref_clk - Program GREFCLK register in device mode |
| 5091 | * |
| 5092 | * @hsotg: Programming view of DWC_otg controller |
| 5093 | * |
| 5094 | */ |
| 5095 | void dwc2_gadget_program_ref_clk(struct dwc2_hsotg *hsotg) |
| 5096 | { |
| 5097 | u32 val = 0; |
| 5098 | |
| 5099 | val |= GREFCLK_REF_CLK_MODE; |
| 5100 | val |= hsotg->params.ref_clk_per << GREFCLK_REFCLKPER_SHIFT; |
| 5101 | val |= hsotg->params.sof_cnt_wkup_alert << |
| 5102 | GREFCLK_SOF_CNT_WKUP_ALERT_SHIFT; |
| 5103 | |
| 5104 | dwc2_writel(hsotg, val, GREFCLK); |
| 5105 | dev_dbg(hsotg->dev, "GREFCLK=0x%08x\n", dwc2_readl(hsotg, GREFCLK)); |
| 5106 | } |
| 5107 | |
| 5108 | /** |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5109 | * dwc2_gadget_enter_hibernation() - Put controller in Hibernation. |
| 5110 | * |
| 5111 | * @hsotg: Programming view of the DWC_otg controller |
| 5112 | * |
| 5113 | * Return non-zero if failed to enter to hibernation. |
| 5114 | */ |
| 5115 | int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg) |
| 5116 | { |
| 5117 | u32 gpwrdn; |
| 5118 | int ret = 0; |
| 5119 | |
| 5120 | /* Change to L2(suspend) state */ |
| 5121 | hsotg->lx_state = DWC2_L2; |
| 5122 | dev_dbg(hsotg->dev, "Start of hibernation completed\n"); |
| 5123 | ret = dwc2_backup_global_registers(hsotg); |
| 5124 | if (ret) { |
| 5125 | dev_err(hsotg->dev, "%s: failed to backup global registers\n", |
| 5126 | __func__); |
| 5127 | return ret; |
| 5128 | } |
| 5129 | ret = dwc2_backup_device_registers(hsotg); |
| 5130 | if (ret) { |
| 5131 | dev_err(hsotg->dev, "%s: failed to backup device registers\n", |
| 5132 | __func__); |
| 5133 | return ret; |
| 5134 | } |
| 5135 | |
| 5136 | gpwrdn = GPWRDN_PWRDNRSTN; |
| 5137 | gpwrdn |= GPWRDN_PMUACTV; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5138 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5139 | udelay(10); |
| 5140 | |
| 5141 | /* Set flag to indicate that we are in hibernation */ |
| 5142 | hsotg->hibernated = 1; |
| 5143 | |
| 5144 | /* Enable interrupts from wake up logic */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5145 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5146 | gpwrdn |= GPWRDN_PMUINTSEL; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5147 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5148 | udelay(10); |
| 5149 | |
| 5150 | /* Unmask device mode interrupts in GPWRDN */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5151 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5152 | gpwrdn |= GPWRDN_RST_DET_MSK; |
| 5153 | gpwrdn |= GPWRDN_LNSTSCHG_MSK; |
| 5154 | gpwrdn |= GPWRDN_STS_CHGINT_MSK; |
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 | /* Enable Power Down Clamp */ |
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_PWRDNCLMP; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5161 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5162 | udelay(10); |
| 5163 | |
| 5164 | /* Switch off VDD */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5165 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5166 | gpwrdn |= GPWRDN_PWRDNSWTCH; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5167 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5168 | udelay(10); |
| 5169 | |
| 5170 | /* Save gpwrdn register for further usage if stschng interrupt */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5171 | hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5172 | dev_dbg(hsotg->dev, "Hibernation completed\n"); |
| 5173 | |
| 5174 | return ret; |
| 5175 | } |
| 5176 | |
| 5177 | /** |
| 5178 | * dwc2_gadget_exit_hibernation() |
| 5179 | * This function is for exiting from Device mode hibernation by host initiated |
| 5180 | * resume/reset and device initiated remote-wakeup. |
| 5181 | * |
| 5182 | * @hsotg: Programming view of the DWC_otg controller |
| 5183 | * @rem_wakeup: indicates whether resume is initiated by Device or Host. |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 5184 | * @reset: indicates whether resume is initiated by Reset. |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5185 | * |
| 5186 | * Return non-zero if failed to exit from hibernation. |
| 5187 | */ |
| 5188 | int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg, |
| 5189 | int rem_wakeup, int reset) |
| 5190 | { |
| 5191 | u32 pcgcctl; |
| 5192 | u32 gpwrdn; |
| 5193 | u32 dctl; |
| 5194 | int ret = 0; |
| 5195 | struct dwc2_gregs_backup *gr; |
| 5196 | struct dwc2_dregs_backup *dr; |
| 5197 | |
| 5198 | gr = &hsotg->gr_backup; |
| 5199 | dr = &hsotg->dr_backup; |
| 5200 | |
| 5201 | if (!hsotg->hibernated) { |
| 5202 | dev_dbg(hsotg->dev, "Already exited from Hibernation\n"); |
| 5203 | return 1; |
| 5204 | } |
| 5205 | dev_dbg(hsotg->dev, |
| 5206 | "%s: called with rem_wakeup = %d reset = %d\n", |
| 5207 | __func__, rem_wakeup, reset); |
| 5208 | |
| 5209 | dwc2_hib_restore_common(hsotg, rem_wakeup, 0); |
| 5210 | |
| 5211 | if (!reset) { |
| 5212 | /* Clear all pending interupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5213 | dwc2_writel(hsotg, 0xffffffff, GINTSTS); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | /* De-assert Restore */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5217 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5218 | gpwrdn &= ~GPWRDN_RESTORE; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5219 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5220 | udelay(10); |
| 5221 | |
| 5222 | if (!rem_wakeup) { |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5223 | pcgcctl = dwc2_readl(hsotg, PCGCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5224 | pcgcctl &= ~PCGCTL_RSTPDWNMODULE; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5225 | dwc2_writel(hsotg, pcgcctl, PCGCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | /* Restore GUSBCFG, DCFG and DCTL */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5229 | dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG); |
| 5230 | dwc2_writel(hsotg, dr->dcfg, DCFG); |
| 5231 | dwc2_writel(hsotg, dr->dctl, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5232 | |
| 5233 | /* De-assert Wakeup Logic */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5234 | gpwrdn = dwc2_readl(hsotg, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5235 | gpwrdn &= ~GPWRDN_PMUACTV; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5236 | dwc2_writel(hsotg, gpwrdn, GPWRDN); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5237 | |
| 5238 | if (rem_wakeup) { |
| 5239 | udelay(10); |
| 5240 | /* Start Remote Wakeup Signaling */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5241 | dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5242 | } else { |
| 5243 | udelay(50); |
| 5244 | /* Set Device programming done bit */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5245 | dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5246 | dctl |= DCTL_PWRONPRGDONE; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5247 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5248 | } |
| 5249 | /* Wait for interrupts which must be cleared */ |
| 5250 | mdelay(2); |
| 5251 | /* Clear all pending interupts */ |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5252 | dwc2_writel(hsotg, 0xffffffff, GINTSTS); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5253 | |
| 5254 | /* Restore global registers */ |
| 5255 | ret = dwc2_restore_global_registers(hsotg); |
| 5256 | if (ret) { |
| 5257 | dev_err(hsotg->dev, "%s: failed to restore registers\n", |
| 5258 | __func__); |
| 5259 | return ret; |
| 5260 | } |
| 5261 | |
| 5262 | /* Restore device registers */ |
| 5263 | ret = dwc2_restore_device_registers(hsotg, rem_wakeup); |
| 5264 | if (ret) { |
| 5265 | dev_err(hsotg->dev, "%s: failed to restore device registers\n", |
| 5266 | __func__); |
| 5267 | return ret; |
| 5268 | } |
| 5269 | |
| 5270 | if (rem_wakeup) { |
| 5271 | mdelay(10); |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5272 | dctl = dwc2_readl(hsotg, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5273 | dctl &= ~DCTL_RMTWKUPSIG; |
Gevorg Sahakyan | f25c42b | 2018-07-26 18:00:13 +0400 | [diff] [blame] | 5274 | dwc2_writel(hsotg, dctl, DCTL); |
Vardan Mikayelyan | c5c403d | 2018-02-16 14:10:13 +0400 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | hsotg->hibernated = 0; |
| 5278 | hsotg->lx_state = DWC2_L0; |
| 5279 | dev_dbg(hsotg->dev, "Hibernation recovery completes here\n"); |
| 5280 | |
| 5281 | return ret; |
| 5282 | } |