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