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