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