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