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