blob: c0ff1cb91f20691c7887f89a3a648e752ebca4e3 [file] [log] [blame]
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001/**
2 * linux/drivers/usb/gadget/s3c-hsotg.c
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003 *
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09004 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01007 * 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 Majewski8b9bc462012-05-04 14:17:11 +020017 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +010018
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 Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Maurus Cuelenaeree50bf382010-07-19 09:40:50 +010030#include <linux/clk.h>
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020031#include <linux/regulator/consumer.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020032#include <linux/of_platform.h>
Matt Porter74084842013-12-19 09:23:06 -050033#include <linux/phy/phy.h>
34#include <linux/usb/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010035
36#include <linux/usb/ch9.h>
37#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053038#include <linux/usb/phy.h>
Lukasz Majewski126625e2012-05-09 13:16:53 +020039#include <linux/platform_data/s3c-hsotg.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010040
Lukasz Majewski127d42a2012-05-04 14:16:59 +020041#include "s3c-hsotg.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010042
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020043static const char * const s3c_hsotg_supply_names[] = {
44 "vusb_d", /* digital USB supply, 1.2V */
45 "vusb_a", /* analog USB supply, 1.1V */
46};
47
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020048/*
49 * EP0_MPS_LIMIT
Ben Dooks5b7d70c2009-06-02 14:58:06 +010050 *
51 * Unfortunately there seems to be a limit of the amount of data that can
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052 * be transferred by IN transactions on EP0. This is either 127 bytes or 3
53 * packets (which practically means 1 packet and 63 bytes of data) when the
Ben Dooks5b7d70c2009-06-02 14:58:06 +010054 * MPS is set to 64.
55 *
56 * This means if we are wanting to move >127 bytes of data, we need to
57 * split the transactions up, but just doing one packet at a time does
58 * not work (this may be an implicit DATA0 PID on first packet of the
59 * transaction) and doing 2 packets is outside the controller's limits.
60 *
61 * If we try to lower the MPS size for EP0, then no transfers work properly
62 * for EP0, and the system will fail basic enumeration. As no cause for this
63 * has currently been found, we cannot support any large IN transfers for
64 * EP0.
65 */
66#define EP0_MPS_LIMIT 64
67
68struct s3c_hsotg;
69struct s3c_hsotg_req;
70
71/**
72 * struct s3c_hsotg_ep - driver endpoint definition.
73 * @ep: The gadget layer representation of the endpoint.
74 * @name: The driver generated name for the endpoint.
75 * @queue: Queue of requests for this endpoint.
76 * @parent: Reference back to the parent device structure.
77 * @req: The current request that the endpoint is processing. This is
78 * used to indicate an request has been loaded onto the endpoint
79 * and has yet to be completed (maybe due to data move, or simply
80 * awaiting an ack from the core all the data has been completed).
81 * @debugfs: File entry for debugfs file for this endpoint.
82 * @lock: State lock to protect contents of endpoint.
83 * @dir_in: Set to true if this endpoint is of the IN direction, which
84 * means that it is sending data to the Host.
85 * @index: The index for the endpoint registers.
Robert Baldyga4fca54a2013-10-09 09:00:02 +020086 * @mc: Multi Count - number of transactions per microframe
Robert Baldyga1479e842013-10-09 08:41:57 +020087 * @interval - Interval for periodic endpoints
Ben Dooks5b7d70c2009-06-02 14:58:06 +010088 * @name: The name array passed to the USB core.
89 * @halted: Set if the endpoint has been halted.
90 * @periodic: Set if this is a periodic ep, such as Interrupt
Robert Baldyga1479e842013-10-09 08:41:57 +020091 * @isochronous: Set if this is a isochronous ep
Ben Dooks5b7d70c2009-06-02 14:58:06 +010092 * @sent_zlp: Set if we've sent a zero-length packet.
93 * @total_data: The total number of data bytes done.
94 * @fifo_size: The size of the FIFO (for periodic IN endpoints)
95 * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
96 * @last_load: The offset of data for the last start of request.
97 * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
98 *
99 * This is the driver's state for each registered enpoint, allowing it
100 * to keep track of transactions that need doing. Each endpoint has a
101 * lock to protect the state, to try and avoid using an overall lock
102 * for the host controller as much as possible.
103 *
104 * For periodic IN endpoints, we have fifo_size and fifo_load to try
105 * and keep track of the amount of data in the periodic FIFO for each
106 * of these as we don't have a status register that tells us how much
Ben Dookse7a9ff52010-07-19 09:40:42 +0100107 * is in each of them. (note, this may actually be useless information
108 * as in shared-fifo mode periodic in acts like a single-frame packet
109 * buffer than a fifo)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100110 */
111struct s3c_hsotg_ep {
112 struct usb_ep ep;
113 struct list_head queue;
114 struct s3c_hsotg *parent;
115 struct s3c_hsotg_req *req;
116 struct dentry *debugfs;
117
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100118
119 unsigned long total_data;
120 unsigned int size_loaded;
121 unsigned int last_load;
122 unsigned int fifo_load;
123 unsigned short fifo_size;
124
125 unsigned char dir_in;
126 unsigned char index;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200127 unsigned char mc;
Robert Baldyga1479e842013-10-09 08:41:57 +0200128 unsigned char interval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100129
130 unsigned int halted:1;
131 unsigned int periodic:1;
Robert Baldyga1479e842013-10-09 08:41:57 +0200132 unsigned int isochronous:1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100133 unsigned int sent_zlp:1;
134
135 char name[10];
136};
137
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100138/**
139 * struct s3c_hsotg - driver state.
140 * @dev: The parent device supplied to the probe function
141 * @driver: USB gadget driver
Praveen Panerib2e587d2012-11-14 15:57:16 +0530142 * @phy: The otg phy transceiver structure for phy control.
Matt Porter74084842013-12-19 09:23:06 -0500143 * @uphy: The otg phy transceiver structure for old USB phy control.
Praveen Panerib2e587d2012-11-14 15:57:16 +0530144 * @plat: The platform specific configuration data. This can be removed once
145 * all SoCs support usb transceiver.
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100146 * @regs: The memory area mapped for accessing registers.
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100147 * @irq: The IRQ number we are using
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200148 * @supplies: Definition of USB power supplies
Matt Porterf7e504c2013-12-19 09:23:07 -0500149 * @phyif: PHY interface width
Ben Dooks10aebc72010-07-19 09:40:44 +0100150 * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200151 * @num_of_eps: Number of available EPs (excluding EP0)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100152 * @debug_root: root directrory for debugfs.
153 * @debug_file: main status file for debugfs.
154 * @debug_fifo: FIFO status file for debugfs.
155 * @ep0_reply: Request used for ep0 reply.
156 * @ep0_buff: Buffer for EP0 reply data, if needed.
157 * @ctrl_buff: Buffer for EP0 control requests.
158 * @ctrl_req: Request for EP0 control packets.
Lukasz Majewski71225be2012-05-04 14:17:03 +0200159 * @setup: NAK management for EP0 SETUP
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +0200160 * @last_rst: Time of last reset
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100161 * @eps: The endpoints being supplied to the gadget framework
162 */
163struct s3c_hsotg {
164 struct device *dev;
165 struct usb_gadget_driver *driver;
Matt Porter74084842013-12-19 09:23:06 -0500166 struct phy *phy;
167 struct usb_phy *uphy;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100168 struct s3c_hsotg_plat *plat;
169
Lukasz Majewski22258f42012-06-14 10:02:24 +0200170 spinlock_t lock;
171
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100172 void __iomem *regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100173 int irq;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +0200174 struct clk *clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100175
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200176 struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
177
Matt Porterf7e504c2013-12-19 09:23:07 -0500178 u32 phyif;
Ben Dooks10aebc72010-07-19 09:40:44 +0100179 unsigned int dedicated_fifos:1;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200180 unsigned char num_of_eps;
Ben Dooks10aebc72010-07-19 09:40:44 +0100181
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100182 struct dentry *debug_root;
183 struct dentry *debug_file;
184 struct dentry *debug_fifo;
185
186 struct usb_request *ep0_reply;
187 struct usb_request *ctrl_req;
188 u8 ep0_buff[8];
189 u8 ctrl_buff[8];
190
191 struct usb_gadget gadget;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200192 unsigned int setup;
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +0200193 unsigned long last_rst;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200194 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100195};
196
197/**
198 * struct s3c_hsotg_req - data transfer request
199 * @req: The USB gadget request
200 * @queue: The list of requests for the endpoint this is queued for.
201 * @in_progress: Has already had size/packets written to core
202 * @mapped: DMA buffer for this request has been mapped via dma_map_single().
203 */
204struct s3c_hsotg_req {
205 struct usb_request req;
206 struct list_head queue;
207 unsigned char in_progress;
208 unsigned char mapped;
209};
210
211/* conversion functions */
212static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
213{
214 return container_of(req, struct s3c_hsotg_req, req);
215}
216
217static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
218{
219 return container_of(ep, struct s3c_hsotg_ep, ep);
220}
221
222static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
223{
224 return container_of(gadget, struct s3c_hsotg, gadget);
225}
226
227static inline void __orr32(void __iomem *ptr, u32 val)
228{
229 writel(readl(ptr) | val, ptr);
230}
231
232static inline void __bic32(void __iomem *ptr, u32 val)
233{
234 writel(readl(ptr) & ~val, ptr);
235}
236
237/* forward decleration of functions */
238static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
239
240/**
241 * using_dma - return the DMA status of the driver.
242 * @hsotg: The driver state.
243 *
244 * Return true if we're using DMA.
245 *
246 * Currently, we have the DMA support code worked into everywhere
247 * that needs it, but the AMBA DMA implementation in the hardware can
248 * only DMA from 32bit aligned addresses. This means that gadgets such
249 * as the CDC Ethernet cannot work as they often pass packets which are
250 * not 32bit aligned.
251 *
252 * Unfortunately the choice to use DMA or not is global to the controller
253 * and seems to be only settable when the controller is being put through
254 * a core reset. This means we either need to fix the gadgets to take
255 * account of DMA alignment, or add bounce buffers (yuerk).
256 *
257 * Until this issue is sorted out, we always return 'false'.
258 */
259static inline bool using_dma(struct s3c_hsotg *hsotg)
260{
261 return false; /* support is not complete */
262}
263
264/**
265 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
266 * @hsotg: The device state
267 * @ints: A bitmask of the interrupts to enable
268 */
269static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
270{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200271 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100272 u32 new_gsintmsk;
273
274 new_gsintmsk = gsintmsk | ints;
275
276 if (new_gsintmsk != gsintmsk) {
277 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200278 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100279 }
280}
281
282/**
283 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
284 * @hsotg: The device state
285 * @ints: A bitmask of the interrupts to enable
286 */
287static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
288{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200289 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100290 u32 new_gsintmsk;
291
292 new_gsintmsk = gsintmsk & ~ints;
293
294 if (new_gsintmsk != gsintmsk)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200295 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100296}
297
298/**
299 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
300 * @hsotg: The device state
301 * @ep: The endpoint index
302 * @dir_in: True if direction is in.
303 * @en: The enable value, true to enable
304 *
305 * Set or clear the mask for an individual endpoint's interrupt
306 * request.
307 */
308static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
309 unsigned int ep, unsigned int dir_in,
310 unsigned int en)
311{
312 unsigned long flags;
313 u32 bit = 1 << ep;
314 u32 daint;
315
316 if (!dir_in)
317 bit <<= 16;
318
319 local_irq_save(flags);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200320 daint = readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100321 if (en)
322 daint |= bit;
323 else
324 daint &= ~bit;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200325 writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100326 local_irq_restore(flags);
327}
328
329/**
330 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
331 * @hsotg: The device instance.
332 */
333static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
334{
Ben Dooks0f002d22010-05-25 05:36:50 +0100335 unsigned int ep;
336 unsigned int addr;
337 unsigned int size;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100338 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100339 u32 val;
340
Ben Dooks6d091ee72010-07-19 09:40:40 +0100341 /* set FIFO sizes to 2048/1024 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100342
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200343 writel(2048, hsotg->regs + GRXFSIZ);
344 writel(GNPTXFSIZ_NPTxFStAddr(2048) |
345 GNPTXFSIZ_NPTxFDep(1024),
346 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100347
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200348 /*
349 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100350 * block have overlapping default addresses. This also ensures
351 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200352 * known values.
353 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100354
355 /* start at the end of the GNPTXFSIZ, rounded up */
356 addr = 2048 + 1024;
357 size = 768;
358
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200359 /*
360 * currently we allocate TX FIFOs for all possible endpoints,
361 * and assume that they are all the same size.
362 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100363
Anton Tikhomirovf7a83fe2012-03-06 14:05:49 +0900364 for (ep = 1; ep <= 15; ep++) {
Ben Dooks0f002d22010-05-25 05:36:50 +0100365 val = addr;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200366 val |= size << DPTXFSIZn_DPTxFSize_SHIFT;
Ben Dooks0f002d22010-05-25 05:36:50 +0100367 addr += size;
368
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200369 writel(val, hsotg->regs + DPTXFSIZn(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100370 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100371
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200372 /*
373 * according to p428 of the design guide, we need to ensure that
374 * all fifos are flushed before continuing
375 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100376
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200377 writel(GRSTCTL_TxFNum(0x10) | GRSTCTL_TxFFlsh |
378 GRSTCTL_RxFFlsh, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100379
380 /* wait until the fifos are both flushed */
381 timeout = 100;
382 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200383 val = readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100384
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200385 if ((val & (GRSTCTL_TxFFlsh | GRSTCTL_RxFFlsh)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100386 break;
387
388 if (--timeout == 0) {
389 dev_err(hsotg->dev,
390 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
391 __func__, val);
392 }
393
394 udelay(1);
395 }
396
397 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100398}
399
400/**
401 * @ep: USB endpoint to allocate request for.
402 * @flags: Allocation flags
403 *
404 * Allocate a new USB request structure appropriate for the specified endpoint
405 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000406static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
407 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100408{
409 struct s3c_hsotg_req *req;
410
411 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
412 if (!req)
413 return NULL;
414
415 INIT_LIST_HEAD(&req->queue);
416
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100417 return &req->req;
418}
419
420/**
421 * is_ep_periodic - return true if the endpoint is in periodic mode.
422 * @hs_ep: The endpoint to query.
423 *
424 * Returns true if the endpoint is in periodic mode, meaning it is being
425 * used for an Interrupt or ISO transfer.
426 */
427static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
428{
429 return hs_ep->periodic;
430}
431
432/**
433 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
434 * @hsotg: The device state.
435 * @hs_ep: The endpoint for the request
436 * @hs_req: The request being processed.
437 *
438 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
439 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200440 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100441static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
442 struct s3c_hsotg_ep *hs_ep,
443 struct s3c_hsotg_req *hs_req)
444{
445 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100446
447 /* ignore this if we're not moving any data */
448 if (hs_req->req.length == 0)
449 return;
450
Jingoo Han17d966a2013-05-11 21:14:00 +0900451 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100452}
453
454/**
455 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
456 * @hsotg: The controller state.
457 * @hs_ep: The endpoint we're going to write for.
458 * @hs_req: The request to write data for.
459 *
460 * This is called when the TxFIFO has some space in it to hold a new
461 * transmission and we have something to give it. The actual setup of
462 * the data size is done elsewhere, so all we have to do is to actually
463 * write the data.
464 *
465 * The return value is zero if there is more space (or nothing was done)
466 * otherwise -ENOSPC is returned if the FIFO space was used up.
467 *
468 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200469 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100470static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
471 struct s3c_hsotg_ep *hs_ep,
472 struct s3c_hsotg_req *hs_req)
473{
474 bool periodic = is_ep_periodic(hs_ep);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200475 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100476 int buf_pos = hs_req->req.actual;
477 int to_write = hs_ep->size_loaded;
478 void *data;
479 int can_write;
480 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200481 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100482
483 to_write -= (buf_pos - hs_ep->last_load);
484
485 /* if there's nothing to write, get out early */
486 if (to_write == 0)
487 return 0;
488
Ben Dooks10aebc72010-07-19 09:40:44 +0100489 if (periodic && !hsotg->dedicated_fifos) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200490 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100491 int size_left;
492 int size_done;
493
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200494 /*
495 * work out how much data was loaded so we can calculate
496 * how much data is left in the fifo.
497 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100498
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200499 size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100500
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200501 /*
502 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100503 * previous data has been completely sent.
504 */
505 if (hs_ep->fifo_load != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200506 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100507 return -ENOSPC;
508 }
509
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100510 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
511 __func__, size_left,
512 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
513
514 /* how much of the data has moved */
515 size_done = hs_ep->size_loaded - size_left;
516
517 /* how much data is left in the fifo */
518 can_write = hs_ep->fifo_load - size_done;
519 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
520 __func__, can_write);
521
522 can_write = hs_ep->fifo_size - can_write;
523 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
524 __func__, can_write);
525
526 if (can_write <= 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200527 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100528 return -ENOSPC;
529 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100530 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200531 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100532
533 can_write &= 0xffff;
534 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100535 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200536 if (GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100537 dev_dbg(hsotg->dev,
538 "%s: no queue slots available (0x%08x)\n",
539 __func__, gnptxsts);
540
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200541 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100542 return -ENOSPC;
543 }
544
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200545 can_write = GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100546 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100547 }
548
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200549 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
550
551 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
552 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100553
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200554 /*
555 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100556 * FIFO, requests of >512 cause the endpoint to get stuck with a
557 * fragment of the end of the transfer in it.
558 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200559 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100560 can_write = 512;
561
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200562 /*
563 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100564 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200565 * doing it.
566 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200567 if (to_write > max_transfer) {
568 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100569
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200570 /* it's needed only when we do not use dedicated fifos */
571 if (!hsotg->dedicated_fifos)
572 s3c_hsotg_en_gsint(hsotg,
573 periodic ? GINTSTS_PTxFEmp :
574 GINTSTS_NPTxFEmp);
Ben Dooks03e10e52010-07-19 09:40:45 +0100575 }
576
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100577 /* see if we can write data */
578
579 if (to_write > can_write) {
580 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200581 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100582
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200583 /*
584 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100585 * exact number of packets.
586 *
587 * Note, we do not currently check to see if we can ever
588 * write a full packet or not to the FIFO.
589 */
590
591 if (pkt_round)
592 to_write -= pkt_round;
593
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200594 /*
595 * enable correct FIFO interrupt to alert us when there
596 * is more room left.
597 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100598
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200599 /* it's needed only when we do not use dedicated fifos */
600 if (!hsotg->dedicated_fifos)
601 s3c_hsotg_en_gsint(hsotg,
602 periodic ? GINTSTS_PTxFEmp :
603 GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100604 }
605
606 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
607 to_write, hs_req->req.length, can_write, buf_pos);
608
609 if (to_write <= 0)
610 return -ENOSPC;
611
612 hs_req->req.actual = buf_pos + to_write;
613 hs_ep->total_data += to_write;
614
615 if (periodic)
616 hs_ep->fifo_load += to_write;
617
618 to_write = DIV_ROUND_UP(to_write, 4);
619 data = hs_req->req.buf + buf_pos;
620
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200621 writesl(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100622
623 return (to_write >= can_write) ? -ENOSPC : 0;
624}
625
626/**
627 * get_ep_limit - get the maximum data legnth for this endpoint
628 * @hs_ep: The endpoint
629 *
630 * Return the maximum data that can be queued in one go on a given endpoint
631 * so that transfers that are too long can be split.
632 */
633static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
634{
635 int index = hs_ep->index;
636 unsigned maxsize;
637 unsigned maxpkt;
638
639 if (index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200640 maxsize = DxEPTSIZ_XferSize_LIMIT + 1;
641 maxpkt = DxEPTSIZ_PktCnt_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100642 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100643 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900644 if (hs_ep->dir_in)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200645 maxpkt = DIEPTSIZ0_PktCnt_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900646 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100647 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100648 }
649
650 /* we made the constant loading easier above by using +1 */
651 maxpkt--;
652 maxsize--;
653
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200654 /*
655 * constrain by packet count if maxpkts*pktsize is greater
656 * than the length register size.
657 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100658
659 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
660 maxsize = maxpkt * hs_ep->ep.maxpacket;
661
662 return maxsize;
663}
664
665/**
666 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
667 * @hsotg: The controller state.
668 * @hs_ep: The endpoint to process a request for
669 * @hs_req: The request to start.
670 * @continuing: True if we are doing more for the current request.
671 *
672 * Start the given request running by setting the endpoint registers
673 * appropriately, and writing any data to the FIFOs.
674 */
675static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
676 struct s3c_hsotg_ep *hs_ep,
677 struct s3c_hsotg_req *hs_req,
678 bool continuing)
679{
680 struct usb_request *ureq = &hs_req->req;
681 int index = hs_ep->index;
682 int dir_in = hs_ep->dir_in;
683 u32 epctrl_reg;
684 u32 epsize_reg;
685 u32 epsize;
686 u32 ctrl;
687 unsigned length;
688 unsigned packets;
689 unsigned maxreq;
690
691 if (index != 0) {
692 if (hs_ep->req && !continuing) {
693 dev_err(hsotg->dev, "%s: active request\n", __func__);
694 WARN_ON(1);
695 return;
696 } else if (hs_ep->req != hs_req && continuing) {
697 dev_err(hsotg->dev,
698 "%s: continue different req\n", __func__);
699 WARN_ON(1);
700 return;
701 }
702 }
703
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200704 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
705 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100706
707 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
708 __func__, readl(hsotg->regs + epctrl_reg), index,
709 hs_ep->dir_in ? "in" : "out");
710
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900711 /* If endpoint is stalled, we will restart request later */
712 ctrl = readl(hsotg->regs + epctrl_reg);
713
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200714 if (ctrl & DxEPCTL_Stall) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900715 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
716 return;
717 }
718
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100719 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200720 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
721 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100722 if (0)
723 dev_dbg(hsotg->dev,
724 "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
725 ureq->buf, length, ureq->dma,
726 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
727
728 maxreq = get_ep_limit(hs_ep);
729 if (length > maxreq) {
730 int round = maxreq % hs_ep->ep.maxpacket;
731
732 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
733 __func__, length, maxreq, round);
734
735 /* round down to multiple of packets */
736 if (round)
737 maxreq -= round;
738
739 length = maxreq;
740 }
741
742 if (length)
743 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
744 else
745 packets = 1; /* send one packet if length is zero. */
746
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200747 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
748 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
749 return;
750 }
751
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100752 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200753 if (hs_ep->isochronous)
754 epsize = DxEPTSIZ_MC(packets);
755 else
756 epsize = DxEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100757 else
758 epsize = 0;
759
760 if (index != 0 && ureq->zero) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200761 /*
762 * test for the packets being exactly right for the
763 * transfer
764 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100765
766 if (length == (packets * hs_ep->ep.maxpacket))
767 packets++;
768 }
769
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200770 epsize |= DxEPTSIZ_PktCnt(packets);
771 epsize |= DxEPTSIZ_XferSize(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100772
773 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
774 __func__, packets, length, ureq->length, epsize, epsize_reg);
775
776 /* store the request as the current one we're doing */
777 hs_ep->req = hs_req;
778
779 /* write size / packets */
780 writel(epsize, hsotg->regs + epsize_reg);
781
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900782 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100783 unsigned int dma_reg;
784
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200785 /*
786 * write DMA address to control register, buffer already
787 * synced by s3c_hsotg_ep_queue().
788 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100789
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200790 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100791 writel(ureq->dma, hsotg->regs + dma_reg);
792
793 dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
794 __func__, ureq->dma, dma_reg);
795 }
796
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200797 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
798 ctrl |= DxEPCTL_USBActEp;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200799
800 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
801
802 /* For Setup request do not clear NAK */
803 if (hsotg->setup && index == 0)
804 hsotg->setup = 0;
805 else
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200806 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200807
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100808
809 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
810 writel(ctrl, hsotg->regs + epctrl_reg);
811
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200812 /*
813 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100814 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200815 * this information.
816 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100817 hs_ep->size_loaded = length;
818 hs_ep->last_load = ureq->actual;
819
820 if (dir_in && !using_dma(hsotg)) {
821 /* set these anyway, we may need them for non-periodic in */
822 hs_ep->fifo_load = 0;
823
824 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
825 }
826
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200827 /*
828 * clear the INTknTXFEmpMsk when we start request, more as a aide
829 * to debugging to see what is going on.
830 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100831 if (dir_in)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200832 writel(DIEPMSK_INTknTXFEmpMsk,
833 hsotg->regs + DIEPINT(index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100834
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200835 /*
836 * Note, trying to clear the NAK here causes problems with transmit
837 * on the S3C6400 ending up with the TXFIFO becoming full.
838 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100839
840 /* check ep is enabled */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200841 if (!(readl(hsotg->regs + epctrl_reg) & DxEPCTL_EPEna))
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100842 dev_warn(hsotg->dev,
843 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
844 index, readl(hsotg->regs + epctrl_reg));
845
846 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
847 __func__, readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200848
849 /* enable ep interrupts */
850 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100851}
852
853/**
854 * s3c_hsotg_map_dma - map the DMA memory being used for the request
855 * @hsotg: The device state.
856 * @hs_ep: The endpoint the request is on.
857 * @req: The request being processed.
858 *
859 * We've been asked to queue a request, so ensure that the memory buffer
860 * is correctly setup for DMA. If we've been passed an extant DMA address
861 * then ensure the buffer has been synced to memory. If our buffer has no
862 * DMA memory, then we map the memory and mark our request to allow us to
863 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200864 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100865static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
866 struct s3c_hsotg_ep *hs_ep,
867 struct usb_request *req)
868{
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100869 struct s3c_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200870 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100871
872 /* if the length is zero, ignore the DMA data */
873 if (hs_req->req.length == 0)
874 return 0;
875
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200876 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
877 if (ret)
878 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100879
880 return 0;
881
882dma_error:
883 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
884 __func__, req->buf, req->length);
885
886 return -EIO;
887}
888
889static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
890 gfp_t gfp_flags)
891{
892 struct s3c_hsotg_req *hs_req = our_req(req);
893 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
894 struct s3c_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100895 bool first;
896
897 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
898 ep->name, req, req->length, req->buf, req->no_interrupt,
899 req->zero, req->short_not_ok);
900
901 /* initialise status of the request */
902 INIT_LIST_HEAD(&hs_req->queue);
903 req->actual = 0;
904 req->status = -EINPROGRESS;
905
906 /* if we're using DMA, sync the buffers as necessary */
907 if (using_dma(hs)) {
908 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
909 if (ret)
910 return ret;
911 }
912
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100913 first = list_empty(&hs_ep->queue);
914 list_add_tail(&hs_req->queue, &hs_ep->queue);
915
916 if (first)
917 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
918
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100919 return 0;
920}
921
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200922static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
923 gfp_t gfp_flags)
924{
925 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
926 struct s3c_hsotg *hs = hs_ep->parent;
927 unsigned long flags = 0;
928 int ret = 0;
929
930 spin_lock_irqsave(&hs->lock, flags);
931 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
932 spin_unlock_irqrestore(&hs->lock, flags);
933
934 return ret;
935}
936
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100937static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
938 struct usb_request *req)
939{
940 struct s3c_hsotg_req *hs_req = our_req(req);
941
942 kfree(hs_req);
943}
944
945/**
946 * s3c_hsotg_complete_oursetup - setup completion callback
947 * @ep: The endpoint the request was on.
948 * @req: The request completed.
949 *
950 * Called on completion of any requests the driver itself
951 * submitted that need cleaning up.
952 */
953static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
954 struct usb_request *req)
955{
956 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
957 struct s3c_hsotg *hsotg = hs_ep->parent;
958
959 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
960
961 s3c_hsotg_ep_free_request(ep, req);
962}
963
964/**
965 * ep_from_windex - convert control wIndex value to endpoint
966 * @hsotg: The driver state.
967 * @windex: The control request wIndex field (in host order).
968 *
969 * Convert the given wIndex into a pointer to an driver endpoint
970 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200971 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100972static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
973 u32 windex)
974{
975 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
976 int dir = (windex & USB_DIR_IN) ? 1 : 0;
977 int idx = windex & 0x7F;
978
979 if (windex >= 0x100)
980 return NULL;
981
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200982 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100983 return NULL;
984
985 if (idx && ep->dir_in != dir)
986 return NULL;
987
988 return ep;
989}
990
991/**
992 * s3c_hsotg_send_reply - send reply to control request
993 * @hsotg: The device state
994 * @ep: Endpoint 0
995 * @buff: Buffer for request
996 * @length: Length of reply.
997 *
998 * Create a request and queue it on the given endpoint. This is useful as
999 * an internal method of sending replies to certain control requests, etc.
1000 */
1001static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
1002 struct s3c_hsotg_ep *ep,
1003 void *buff,
1004 int length)
1005{
1006 struct usb_request *req;
1007 int ret;
1008
1009 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1010
1011 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
1012 hsotg->ep0_reply = req;
1013 if (!req) {
1014 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1015 return -ENOMEM;
1016 }
1017
1018 req->buf = hsotg->ep0_buff;
1019 req->length = length;
1020 req->zero = 1; /* always do zero-length final transfer */
1021 req->complete = s3c_hsotg_complete_oursetup;
1022
1023 if (length)
1024 memcpy(req->buf, buff, length);
1025 else
1026 ep->sent_zlp = 1;
1027
1028 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1029 if (ret) {
1030 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1031 return ret;
1032 }
1033
1034 return 0;
1035}
1036
1037/**
1038 * s3c_hsotg_process_req_status - process request GET_STATUS
1039 * @hsotg: The device state
1040 * @ctrl: USB control request
1041 */
1042static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
1043 struct usb_ctrlrequest *ctrl)
1044{
1045 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1046 struct s3c_hsotg_ep *ep;
1047 __le16 reply;
1048 int ret;
1049
1050 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1051
1052 if (!ep0->dir_in) {
1053 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1054 return -EINVAL;
1055 }
1056
1057 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1058 case USB_RECIP_DEVICE:
1059 reply = cpu_to_le16(0); /* bit 0 => self powered,
1060 * bit 1 => remote wakeup */
1061 break;
1062
1063 case USB_RECIP_INTERFACE:
1064 /* currently, the data result should be zero */
1065 reply = cpu_to_le16(0);
1066 break;
1067
1068 case USB_RECIP_ENDPOINT:
1069 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1070 if (!ep)
1071 return -ENOENT;
1072
1073 reply = cpu_to_le16(ep->halted ? 1 : 0);
1074 break;
1075
1076 default:
1077 return 0;
1078 }
1079
1080 if (le16_to_cpu(ctrl->wLength) != 2)
1081 return -EINVAL;
1082
1083 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1084 if (ret) {
1085 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1086 return ret;
1087 }
1088
1089 return 1;
1090}
1091
1092static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1093
1094/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001095 * get_ep_head - return the first request on the endpoint
1096 * @hs_ep: The controller endpoint to get
1097 *
1098 * Get the first request on the endpoint.
1099 */
1100static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1101{
1102 if (list_empty(&hs_ep->queue))
1103 return NULL;
1104
1105 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1106}
1107
1108/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001109 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
1110 * @hsotg: The device state
1111 * @ctrl: USB control request
1112 */
1113static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1114 struct usb_ctrlrequest *ctrl)
1115{
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001116 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001117 struct s3c_hsotg_req *hs_req;
1118 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001119 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1120 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001121 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001122 bool halted;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001123
1124 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1125 __func__, set ? "SET" : "CLEAR");
1126
1127 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
1128 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1129 if (!ep) {
1130 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1131 __func__, le16_to_cpu(ctrl->wIndex));
1132 return -ENOENT;
1133 }
1134
1135 switch (le16_to_cpu(ctrl->wValue)) {
1136 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001137 halted = ep->halted;
1138
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001139 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001140
1141 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1142 if (ret) {
1143 dev_err(hsotg->dev,
1144 "%s: failed to send reply\n", __func__);
1145 return ret;
1146 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001147
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001148 /*
1149 * we have to complete all requests for ep if it was
1150 * halted, and the halt was cleared by CLEAR_FEATURE
1151 */
1152
1153 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001154 /*
1155 * If we have request in progress,
1156 * then complete it
1157 */
1158 if (ep->req) {
1159 hs_req = ep->req;
1160 ep->req = NULL;
1161 list_del_init(&hs_req->queue);
1162 hs_req->req.complete(&ep->ep,
1163 &hs_req->req);
1164 }
1165
1166 /* If we have pending request, then start it */
1167 restart = !list_empty(&ep->queue);
1168 if (restart) {
1169 hs_req = get_ep_head(ep);
1170 s3c_hsotg_start_req(hsotg, ep,
1171 hs_req, false);
1172 }
1173 }
1174
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001175 break;
1176
1177 default:
1178 return -ENOENT;
1179 }
1180 } else
1181 return -ENOENT; /* currently only deal with endpoint */
1182
1183 return 1;
1184}
1185
Robert Baldygaab93e012013-09-19 11:50:17 +02001186static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
Robert Baldygad18f71162013-11-21 13:49:18 +01001187static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001188
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001189/**
1190 * s3c_hsotg_process_control - process a control request
1191 * @hsotg: The device state
1192 * @ctrl: The control request received
1193 *
1194 * The controller has received the SETUP phase of a control request, and
1195 * needs to work out what to do next (and whether to pass it on to the
1196 * gadget driver).
1197 */
1198static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1199 struct usb_ctrlrequest *ctrl)
1200{
1201 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1202 int ret = 0;
1203 u32 dcfg;
1204
1205 ep0->sent_zlp = 0;
1206
1207 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1208 ctrl->bRequest, ctrl->bRequestType,
1209 ctrl->wValue, ctrl->wLength);
1210
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001211 /*
1212 * record the direction of the request, for later use when enquing
1213 * packets onto EP0.
1214 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001215
1216 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1217 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1218
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001219 /*
1220 * if we've no data with this request, then the last part of the
1221 * transaction is going to implicitly be IN.
1222 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001223 if (ctrl->wLength == 0)
1224 ep0->dir_in = 1;
1225
1226 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1227 switch (ctrl->bRequest) {
1228 case USB_REQ_SET_ADDRESS:
Robert Baldygad18f71162013-11-21 13:49:18 +01001229 s3c_hsotg_disconnect(hsotg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001230 dcfg = readl(hsotg->regs + DCFG);
1231 dcfg &= ~DCFG_DevAddr_MASK;
1232 dcfg |= ctrl->wValue << DCFG_DevAddr_SHIFT;
1233 writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001234
1235 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1236
1237 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1238 return;
1239
1240 case USB_REQ_GET_STATUS:
1241 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1242 break;
1243
1244 case USB_REQ_CLEAR_FEATURE:
1245 case USB_REQ_SET_FEATURE:
1246 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1247 break;
1248 }
1249 }
1250
1251 /* as a fallback, try delivering it to the driver to deal with */
1252
1253 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001254 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001255 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001256 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001257 if (ret < 0)
1258 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1259 }
1260
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001261 /*
1262 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001263 * so respond with a STALL for the status stage to indicate failure.
1264 */
1265
1266 if (ret < 0) {
1267 u32 reg;
1268 u32 ctrl;
1269
1270 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001271 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001272
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001273 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001274 * DxEPCTL_Stall will be cleared by EP once it has
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001275 * taken effect, so no need to clear later.
1276 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001277
1278 ctrl = readl(hsotg->regs + reg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001279 ctrl |= DxEPCTL_Stall;
1280 ctrl |= DxEPCTL_CNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001281 writel(ctrl, hsotg->regs + reg);
1282
1283 dev_dbg(hsotg->dev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001284 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001285 ctrl, reg, readl(hsotg->regs + reg));
1286
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001287 /*
1288 * don't believe we need to anything more to get the EP
1289 * to reply with a STALL packet
1290 */
Robert Baldygaab93e012013-09-19 11:50:17 +02001291
1292 /*
1293 * complete won't be called, so we enqueue
1294 * setup request here
1295 */
1296 s3c_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001297 }
1298}
1299
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001300/**
1301 * s3c_hsotg_complete_setup - completion of a setup transfer
1302 * @ep: The endpoint the request was on.
1303 * @req: The request completed.
1304 *
1305 * Called on completion of any requests the driver itself submitted for
1306 * EP0 setup packets
1307 */
1308static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1309 struct usb_request *req)
1310{
1311 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1312 struct s3c_hsotg *hsotg = hs_ep->parent;
1313
1314 if (req->status < 0) {
1315 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1316 return;
1317 }
1318
Robert Baldyga93f599f2013-11-21 13:49:17 +01001319 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001320 if (req->actual == 0)
1321 s3c_hsotg_enqueue_setup(hsotg);
1322 else
1323 s3c_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001324 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001325}
1326
1327/**
1328 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1329 * @hsotg: The device state.
1330 *
1331 * Enqueue a request on EP0 if necessary to received any SETUP packets
1332 * received from the host.
1333 */
1334static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1335{
1336 struct usb_request *req = hsotg->ctrl_req;
1337 struct s3c_hsotg_req *hs_req = our_req(req);
1338 int ret;
1339
1340 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1341
1342 req->zero = 0;
1343 req->length = 8;
1344 req->buf = hsotg->ctrl_buff;
1345 req->complete = s3c_hsotg_complete_setup;
1346
1347 if (!list_empty(&hs_req->queue)) {
1348 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1349 return;
1350 }
1351
1352 hsotg->eps[0].dir_in = 0;
1353
1354 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1355 if (ret < 0) {
1356 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001357 /*
1358 * Don't think there's much we can do other than watch the
1359 * driver fail.
1360 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001361 }
1362}
1363
1364/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001365 * s3c_hsotg_complete_request - complete a request given to us
1366 * @hsotg: The device state.
1367 * @hs_ep: The endpoint the request was on.
1368 * @hs_req: The request to complete.
1369 * @result: The result code (0 => Ok, otherwise errno)
1370 *
1371 * The given request has finished, so call the necessary completion
1372 * if it has one and then look to see if we can start a new request
1373 * on the endpoint.
1374 *
1375 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001376 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001377static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1378 struct s3c_hsotg_ep *hs_ep,
1379 struct s3c_hsotg_req *hs_req,
1380 int result)
1381{
1382 bool restart;
1383
1384 if (!hs_req) {
1385 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1386 return;
1387 }
1388
1389 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1390 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1391
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001392 /*
1393 * only replace the status if we've not already set an error
1394 * from a previous transaction
1395 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001396
1397 if (hs_req->req.status == -EINPROGRESS)
1398 hs_req->req.status = result;
1399
1400 hs_ep->req = NULL;
1401 list_del_init(&hs_req->queue);
1402
1403 if (using_dma(hsotg))
1404 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1405
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001406 /*
1407 * call the complete request with the locks off, just in case the
1408 * request tries to queue more work for this endpoint.
1409 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001410
1411 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001412 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001413 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001414 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001415 }
1416
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001417 /*
1418 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001419 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001420 * so be careful when doing this.
1421 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001422
1423 if (!hs_ep->req && result >= 0) {
1424 restart = !list_empty(&hs_ep->queue);
1425 if (restart) {
1426 hs_req = get_ep_head(hs_ep);
1427 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1428 }
1429 }
1430}
1431
1432/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001433 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1434 * @hsotg: The device state.
1435 * @ep_idx: The endpoint index for the data
1436 * @size: The size of data in the fifo, in bytes
1437 *
1438 * The FIFO status shows there is data to read from the FIFO for a given
1439 * endpoint, so sort out whether we need to read the data into a request
1440 * that has been made for that endpoint.
1441 */
1442static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1443{
1444 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1445 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001446 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001447 int to_read;
1448 int max_req;
1449 int read_ptr;
1450
Lukasz Majewski22258f42012-06-14 10:02:24 +02001451
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001452 if (!hs_req) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001453 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001454 int ptr;
1455
1456 dev_warn(hsotg->dev,
1457 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1458 __func__, size, ep_idx, epctl);
1459
1460 /* dump the data from the FIFO, we've nothing we can do */
1461 for (ptr = 0; ptr < size; ptr += 4)
1462 (void)readl(fifo);
1463
1464 return;
1465 }
1466
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001467 to_read = size;
1468 read_ptr = hs_req->req.actual;
1469 max_req = hs_req->req.length - read_ptr;
1470
Ben Dooksa33e7132010-07-19 09:40:49 +01001471 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1472 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1473
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001474 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001475 /*
1476 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001477 * to deal with in this request.
1478 */
1479
1480 /* currently we don't deal this */
1481 WARN_ON_ONCE(1);
1482 }
1483
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001484 hs_ep->total_data += to_read;
1485 hs_req->req.actual += to_read;
1486 to_read = DIV_ROUND_UP(to_read, 4);
1487
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001488 /*
1489 * note, we might over-write the buffer end by 3 bytes depending on
1490 * alignment of the data.
1491 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001492 readsl(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001493}
1494
1495/**
1496 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1497 * @hsotg: The device instance
1498 * @req: The request currently on this endpoint
1499 *
1500 * Generate a zero-length IN packet request for terminating a SETUP
1501 * transaction.
1502 *
1503 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001504 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001505 * the TxFIFO.
1506 */
1507static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1508 struct s3c_hsotg_req *req)
1509{
1510 u32 ctrl;
1511
1512 if (!req) {
1513 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1514 return;
1515 }
1516
1517 if (req->req.length == 0) {
1518 hsotg->eps[0].sent_zlp = 1;
1519 s3c_hsotg_enqueue_setup(hsotg);
1520 return;
1521 }
1522
1523 hsotg->eps[0].dir_in = 1;
1524 hsotg->eps[0].sent_zlp = 1;
1525
1526 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1527
1528 /* issue a zero-sized packet to terminate this */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001529 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
1530 DxEPTSIZ_XferSize(0), hsotg->regs + DIEPTSIZ(0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001531
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001532 ctrl = readl(hsotg->regs + DIEPCTL0);
1533 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
1534 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
1535 ctrl |= DxEPCTL_USBActEp;
1536 writel(ctrl, hsotg->regs + DIEPCTL0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001537}
1538
1539/**
1540 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1541 * @hsotg: The device instance
1542 * @epnum: The endpoint received from
1543 * @was_setup: Set if processing a SetupDone event.
1544 *
1545 * The RXFIFO has delivered an OutDone event, which means that the data
1546 * transfer for an OUT endpoint has been completed, either by a short
1547 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001548 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001549static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1550 int epnum, bool was_setup)
1551{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001552 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001553 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1554 struct s3c_hsotg_req *hs_req = hs_ep->req;
1555 struct usb_request *req = &hs_req->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001556 unsigned size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001557 int result = 0;
1558
1559 if (!hs_req) {
1560 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1561 return;
1562 }
1563
1564 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001565 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001566
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001567 /*
1568 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001569 * is left in the endpoint size register and then working it
1570 * out from the amount we loaded for the transfer.
1571 *
1572 * We need to do this as DMA pointers are always 32bit aligned
1573 * so may overshoot/undershoot the transfer.
1574 */
1575
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001576 size_done = hs_ep->size_loaded - size_left;
1577 size_done += hs_ep->last_load;
1578
1579 req->actual = size_done;
1580 }
1581
Ben Dooksa33e7132010-07-19 09:40:49 +01001582 /* if there is more request to do, schedule new transfer */
1583 if (req->actual < req->length && size_left == 0) {
1584 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1585 return;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001586 } else if (epnum == 0) {
1587 /*
1588 * After was_setup = 1 =>
1589 * set CNAK for non Setup requests
1590 */
1591 hsotg->setup = was_setup ? 0 : 1;
Ben Dooksa33e7132010-07-19 09:40:49 +01001592 }
1593
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001594 if (req->actual < req->length && req->short_not_ok) {
1595 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1596 __func__, req->actual, req->length);
1597
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001598 /*
1599 * todo - what should we return here? there's no one else
1600 * even bothering to check the status.
1601 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001602 }
1603
1604 if (epnum == 0) {
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001605 /*
1606 * Condition req->complete != s3c_hsotg_complete_setup says:
1607 * send ZLP when we have an asynchronous request from gadget
1608 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001609 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1610 s3c_hsotg_send_zlp(hsotg, hs_req);
1611 }
1612
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001613 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001614}
1615
1616/**
1617 * s3c_hsotg_read_frameno - read current frame number
1618 * @hsotg: The device instance
1619 *
1620 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001621 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001622static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1623{
1624 u32 dsts;
1625
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001626 dsts = readl(hsotg->regs + DSTS);
1627 dsts &= DSTS_SOFFN_MASK;
1628 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001629
1630 return dsts;
1631}
1632
1633/**
1634 * s3c_hsotg_handle_rx - RX FIFO has data
1635 * @hsotg: The device instance
1636 *
1637 * The IRQ handler has detected that the RX FIFO has some data in it
1638 * that requires processing, so find out what is in there and do the
1639 * appropriate read.
1640 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001641 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001642 * chunks, so if you have x packets received on an endpoint you'll get x
1643 * FIFO events delivered, each with a packet's worth of data in it.
1644 *
1645 * When using DMA, we should not be processing events from the RXFIFO
1646 * as the actual data should be sent to the memory directly and we turn
1647 * on the completion interrupts to get notifications of transfer completion.
1648 */
Mark Brown0978f8c2010-01-18 13:18:35 +00001649static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001650{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001651 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001652 u32 epnum, status, size;
1653
1654 WARN_ON(using_dma(hsotg));
1655
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001656 epnum = grxstsr & GRXSTS_EPNum_MASK;
1657 status = grxstsr & GRXSTS_PktSts_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001658
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001659 size = grxstsr & GRXSTS_ByteCnt_MASK;
1660 size >>= GRXSTS_ByteCnt_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001661
1662 if (1)
1663 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1664 __func__, grxstsr, size, epnum);
1665
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001666#define __status(x) ((x) >> GRXSTS_PktSts_SHIFT)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001667
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001668 switch (status >> GRXSTS_PktSts_SHIFT) {
1669 case __status(GRXSTS_PktSts_GlobalOutNAK):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001670 dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1671 break;
1672
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001673 case __status(GRXSTS_PktSts_OutDone):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001674 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1675 s3c_hsotg_read_frameno(hsotg));
1676
1677 if (!using_dma(hsotg))
1678 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1679 break;
1680
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001681 case __status(GRXSTS_PktSts_SetupDone):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001682 dev_dbg(hsotg->dev,
1683 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1684 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001685 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001686
1687 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1688 break;
1689
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001690 case __status(GRXSTS_PktSts_OutRX):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001691 s3c_hsotg_rx_data(hsotg, epnum, size);
1692 break;
1693
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001694 case __status(GRXSTS_PktSts_SetupRX):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001695 dev_dbg(hsotg->dev,
1696 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1697 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001698 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001699
1700 s3c_hsotg_rx_data(hsotg, epnum, size);
1701 break;
1702
1703 default:
1704 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1705 __func__, grxstsr);
1706
1707 s3c_hsotg_dump(hsotg);
1708 break;
1709 }
1710}
1711
1712/**
1713 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1714 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001715 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001716static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1717{
1718 switch (mps) {
1719 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001720 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001721 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001722 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001723 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001724 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001725 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001726 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001727 }
1728
1729 /* bad max packet size, warn and return invalid result */
1730 WARN_ON(1);
1731 return (u32)-1;
1732}
1733
1734/**
1735 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1736 * @hsotg: The driver state.
1737 * @ep: The index number of the endpoint
1738 * @mps: The maximum packet size in bytes
1739 *
1740 * Configure the maximum packet size for the given endpoint, updating
1741 * the hardware control registers to reflect this.
1742 */
1743static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1744 unsigned int ep, unsigned int mps)
1745{
1746 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1747 void __iomem *regs = hsotg->regs;
1748 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001749 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001750 u32 reg;
1751
1752 if (ep == 0) {
1753 /* EP0 is a special case */
1754 mpsval = s3c_hsotg_ep0_mps(mps);
1755 if (mpsval > 3)
1756 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001757 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001758 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001759 } else {
Robert Baldygae9edd1992013-10-09 08:20:02 +02001760 mpsval = mps & DxEPCTL_MPS_MASK;
1761 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001762 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001763 mcval = ((mps >> 11) & 0x3) + 1;
1764 hs_ep->mc = mcval;
1765 if (mcval > 3)
1766 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001767 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001768 }
1769
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001770 /*
1771 * update both the in and out endpoint controldir_ registers, even
1772 * if one of the directions may not be in use.
1773 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001774
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001775 reg = readl(regs + DIEPCTL(ep));
1776 reg &= ~DxEPCTL_MPS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001777 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001778 writel(reg, regs + DIEPCTL(ep));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001779
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001780 if (ep) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001781 reg = readl(regs + DOEPCTL(ep));
1782 reg &= ~DxEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001783 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001784 writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001785 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001786
1787 return;
1788
1789bad_mps:
1790 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1791}
1792
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001793/**
1794 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1795 * @hsotg: The driver state
1796 * @idx: The index for the endpoint (0..15)
1797 */
1798static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1799{
1800 int timeout;
1801 int val;
1802
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001803 writel(GRSTCTL_TxFNum(idx) | GRSTCTL_TxFFlsh,
1804 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001805
1806 /* wait until the fifo is flushed */
1807 timeout = 100;
1808
1809 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001810 val = readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001811
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001812 if ((val & (GRSTCTL_TxFFlsh)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001813 break;
1814
1815 if (--timeout == 0) {
1816 dev_err(hsotg->dev,
1817 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1818 __func__, val);
1819 }
1820
1821 udelay(1);
1822 }
1823}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001824
1825/**
1826 * s3c_hsotg_trytx - check to see if anything needs transmitting
1827 * @hsotg: The driver state
1828 * @hs_ep: The driver endpoint to check.
1829 *
1830 * Check to see if there is a request that has data to send, and if so
1831 * make an attempt to write data into the FIFO.
1832 */
1833static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1834 struct s3c_hsotg_ep *hs_ep)
1835{
1836 struct s3c_hsotg_req *hs_req = hs_ep->req;
1837
Robert Baldygaafcf4162013-09-19 11:50:19 +02001838 if (!hs_ep->dir_in || !hs_req) {
1839 /**
1840 * if request is not enqueued, we disable interrupts
1841 * for endpoints, excepting ep0
1842 */
1843 if (hs_ep->index != 0)
1844 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1845 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001846 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001847 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001848
1849 if (hs_req->req.actual < hs_req->req.length) {
1850 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1851 hs_ep->index);
1852 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1853 }
1854
1855 return 0;
1856}
1857
1858/**
1859 * s3c_hsotg_complete_in - complete IN transfer
1860 * @hsotg: The device state.
1861 * @hs_ep: The endpoint that has just completed.
1862 *
1863 * An IN transfer has been completed, update the transfer's state and then
1864 * call the relevant completion routines.
1865 */
1866static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1867 struct s3c_hsotg_ep *hs_ep)
1868{
1869 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001870 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001871 int size_left, size_done;
1872
1873 if (!hs_req) {
1874 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1875 return;
1876 }
1877
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001878 /* Finish ZLP handling for IN EP0 transactions */
1879 if (hsotg->eps[0].sent_zlp) {
1880 dev_dbg(hsotg->dev, "zlp packet received\n");
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001881 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001882 return;
1883 }
1884
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001885 /*
1886 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001887 * in the endpoint size register and then working it out from
1888 * the amount we loaded for the transfer.
1889 *
1890 * We do this even for DMA, as the transfer may have incremented
1891 * past the end of the buffer (DMA transfers are always 32bit
1892 * aligned).
1893 */
1894
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001895 size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001896
1897 size_done = hs_ep->size_loaded - size_left;
1898 size_done += hs_ep->last_load;
1899
1900 if (hs_req->req.actual != size_done)
1901 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1902 __func__, hs_req->req.actual, size_done);
1903
1904 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001905 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1906 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001907
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001908 /*
1909 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1910 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1911 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1912 * inform the host that no more data is available.
1913 * The state of req.zero member is checked to be sure that the value to
1914 * send is smaller than wValue expected from host.
1915 * Check req.length to NOT send another ZLP when the current one is
1916 * under completion (the one for which this completion has been called).
1917 */
1918 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1919 hs_req->req.length == hs_req->req.actual &&
1920 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1921
1922 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1923 s3c_hsotg_send_zlp(hsotg, hs_req);
1924
1925 return;
1926 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001927
1928 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1929 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1930 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1931 } else
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001932 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001933}
1934
1935/**
1936 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1937 * @hsotg: The driver state
1938 * @idx: The index for the endpoint (0..15)
1939 * @dir_in: Set if this is an IN endpoint
1940 *
1941 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001942 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001943static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1944 int dir_in)
1945{
1946 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001947 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1948 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1949 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02001951 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001952
1953 ints = readl(hsotg->regs + epint_reg);
Robert Baldyga1479e842013-10-09 08:41:57 +02001954 ctrl = readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001955
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001956 /* Clear endpoint interrupts */
1957 writel(ints, hsotg->regs + epint_reg);
1958
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001959 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1960 __func__, idx, dir_in ? "in" : "out", ints);
1961
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001962 if (ints & DxEPINT_XferCompl) {
Robert Baldyga1479e842013-10-09 08:41:57 +02001963 if (hs_ep->isochronous && hs_ep->interval == 1) {
1964 if (ctrl & DxEPCTL_EOFrNum)
1965 ctrl |= DxEPCTL_SetEvenFr;
1966 else
1967 ctrl |= DxEPCTL_SetOddFr;
1968 writel(ctrl, hsotg->regs + epctl_reg);
1969 }
1970
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001971 dev_dbg(hsotg->dev,
1972 "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1973 __func__, readl(hsotg->regs + epctl_reg),
1974 readl(hsotg->regs + epsiz_reg));
1975
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001976 /*
1977 * we get OutDone from the FIFO, so we only need to look
1978 * at completing IN requests here
1979 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001980 if (dir_in) {
1981 s3c_hsotg_complete_in(hsotg, hs_ep);
1982
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001983 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001984 s3c_hsotg_enqueue_setup(hsotg);
1985 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001986 /*
1987 * We're using DMA, we need to fire an OutDone here
1988 * as we ignore the RXFIFO.
1989 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001990
1991 s3c_hsotg_handle_outdone(hsotg, idx, false);
1992 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001993 }
1994
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001995 if (ints & DxEPINT_EPDisbld) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001996 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001997
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001998 if (dir_in) {
1999 int epctl = readl(hsotg->regs + epctl_reg);
2000
2001 s3c_hsotg_txfifo_flush(hsotg, idx);
2002
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002003 if ((epctl & DxEPCTL_Stall) &&
2004 (epctl & DxEPCTL_EPType_Bulk)) {
2005 int dctl = readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002006
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002007 dctl |= DCTL_CGNPInNAK;
2008 writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002009 }
2010 }
2011 }
2012
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002013 if (ints & DxEPINT_AHBErr)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002014 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002015
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002016 if (ints & DxEPINT_Setup) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002017 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2018
2019 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002020 /*
2021 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002022 * setup packet. In non-DMA mode we'd get this
2023 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002024 * the setup here.
2025 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002026
2027 if (dir_in)
2028 WARN_ON_ONCE(1);
2029 else
2030 s3c_hsotg_handle_outdone(hsotg, 0, true);
2031 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002032 }
2033
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002034 if (ints & DxEPINT_Back2BackSetup)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002035 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002036
Robert Baldyga1479e842013-10-09 08:41:57 +02002037 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002038 /* not sure if this is important, but we'll clear it anyway */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002039 if (ints & DIEPMSK_INTknTXFEmpMsk) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002040 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2041 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002042 }
2043
2044 /* this probably means something bad is happening */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002045 if (ints & DIEPMSK_INTknEPMisMsk) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002046 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2047 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002048 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002049
2050 /* FIFO has space or is empty (see GAHBCFG) */
2051 if (hsotg->dedicated_fifos &&
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002052 ints & DIEPMSK_TxFIFOEmpty) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002053 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2054 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002055 if (!using_dma(hsotg))
2056 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002057 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002058 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002059}
2060
2061/**
2062 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
2063 * @hsotg: The device state.
2064 *
2065 * Handle updating the device settings after the enumeration phase has
2066 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002067 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002068static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
2069{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002070 u32 dsts = readl(hsotg->regs + DSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002071 int ep0_mps = 0, ep_mps;
2072
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002073 /*
2074 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002075 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002076 * we connected at.
2077 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002078
2079 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2080
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002081 /*
2082 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002083 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002084 * not advertise a 64byte MPS on EP0.
2085 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002086
2087 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002088 switch (dsts & DSTS_EnumSpd_MASK) {
2089 case DSTS_EnumSpd_FS:
2090 case DSTS_EnumSpd_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002091 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002092 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002093 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002094 break;
2095
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002096 case DSTS_EnumSpd_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002097 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002098 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002099 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002100 break;
2101
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002102 case DSTS_EnumSpd_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002103 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002104 /*
2105 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002106 * moment, and the documentation seems to imply that it isn't
2107 * supported by the PHYs on some of the devices.
2108 */
2109 break;
2110 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002111 dev_info(hsotg->dev, "new device is %s\n",
2112 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002113
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002114 /*
2115 * we should now know the maximum packet size for an
2116 * endpoint, so set the endpoints to a default value.
2117 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002118
2119 if (ep0_mps) {
2120 int i;
2121 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002122 for (i = 1; i < hsotg->num_of_eps; i++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002123 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
2124 }
2125
2126 /* ensure after enumeration our EP0 is active */
2127
2128 s3c_hsotg_enqueue_setup(hsotg);
2129
2130 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002131 readl(hsotg->regs + DIEPCTL0),
2132 readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002133}
2134
2135/**
2136 * kill_all_requests - remove all requests from the endpoint's queue
2137 * @hsotg: The device state.
2138 * @ep: The endpoint the requests may be on.
2139 * @result: The result code to use.
2140 * @force: Force removal of any current requests
2141 *
2142 * Go through the requests on the given endpoint and mark them
2143 * completed with the given result code.
2144 */
2145static void kill_all_requests(struct s3c_hsotg *hsotg,
2146 struct s3c_hsotg_ep *ep,
2147 int result, bool force)
2148{
2149 struct s3c_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002150
2151 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002152 /*
2153 * currently, we can't do much about an already
2154 * running request on an in endpoint
2155 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002156
2157 if (ep->req == req && ep->dir_in && !force)
2158 continue;
2159
2160 s3c_hsotg_complete_request(hsotg, ep, req,
2161 result);
2162 }
Robert Baldygab963a812013-12-06 13:03:45 +01002163 if(hsotg->dedicated_fifos)
2164 if ((readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4 < 3072)
2165 s3c_hsotg_txfifo_flush(hsotg, ep->index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002166}
2167
2168#define call_gadget(_hs, _entry) \
Pavel Macheka023da32013-09-30 14:56:02 +02002169do { \
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002170 if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002171 (_hs)->driver && (_hs)->driver->_entry) { \
2172 spin_unlock(&_hs->lock); \
2173 (_hs)->driver->_entry(&(_hs)->gadget); \
2174 spin_lock(&_hs->lock); \
Pavel Macheka023da32013-09-30 14:56:02 +02002175 } \
2176} while (0)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002177
2178/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002179 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002180 * @hsotg: The device state.
2181 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002182 * The device has been disconnected. Remove all current
2183 * transactions and signal the gadget driver that this
2184 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002185 */
Lukasz Majewski5e891342012-05-04 14:17:07 +02002186static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002187{
2188 unsigned ep;
2189
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002190 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002191 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2192
2193 call_gadget(hsotg, disconnect);
2194}
2195
2196/**
2197 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2198 * @hsotg: The device state:
2199 * @periodic: True if this is a periodic FIFO interrupt
2200 */
2201static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2202{
2203 struct s3c_hsotg_ep *ep;
2204 int epno, ret;
2205
2206 /* look through for any more data to transmit */
2207
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002208 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002209 ep = &hsotg->eps[epno];
2210
2211 if (!ep->dir_in)
2212 continue;
2213
2214 if ((periodic && !ep->periodic) ||
2215 (!periodic && ep->periodic))
2216 continue;
2217
2218 ret = s3c_hsotg_trytx(hsotg, ep);
2219 if (ret < 0)
2220 break;
2221 }
2222}
2223
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002224/* IRQ flags which will trigger a retry around the IRQ loop */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002225#define IRQ_RETRY_MASK (GINTSTS_NPTxFEmp | \
2226 GINTSTS_PTxFEmp | \
2227 GINTSTS_RxFLvl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002228
2229/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002230 * s3c_hsotg_corereset - issue softreset to the core
2231 * @hsotg: The device state
2232 *
2233 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002234 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002235static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2236{
2237 int timeout;
2238 u32 grstctl;
2239
2240 dev_dbg(hsotg->dev, "resetting core\n");
2241
2242 /* issue soft reset */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002243 writel(GRSTCTL_CSftRst, hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002244
Du, Changbin2868fea2012-07-24 08:19:25 +08002245 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002246 do {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002247 grstctl = readl(hsotg->regs + GRSTCTL);
2248 } while ((grstctl & GRSTCTL_CSftRst) && timeout-- > 0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002249
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002250 if (grstctl & GRSTCTL_CSftRst) {
Lukasz Majewski308d7342012-05-04 14:17:05 +02002251 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2252 return -EINVAL;
2253 }
2254
Du, Changbin2868fea2012-07-24 08:19:25 +08002255 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002256
2257 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002258 u32 grstctl = readl(hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002259
2260 if (timeout-- < 0) {
2261 dev_info(hsotg->dev,
2262 "%s: reset failed, GRSTCTL=%08x\n",
2263 __func__, grstctl);
2264 return -ETIMEDOUT;
2265 }
2266
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002267 if (!(grstctl & GRSTCTL_AHBIdle))
Lukasz Majewski308d7342012-05-04 14:17:05 +02002268 continue;
2269
2270 break; /* reset done */
2271 }
2272
2273 dev_dbg(hsotg->dev, "reset successful\n");
2274 return 0;
2275}
2276
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002277/**
2278 * s3c_hsotg_core_init - issue softreset to the core
2279 * @hsotg: The device state
2280 *
2281 * Issue a soft reset to the core, and await the core finishing it.
2282 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002283static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
2284{
2285 s3c_hsotg_corereset(hsotg);
2286
2287 /*
2288 * we must now enable ep0 ready for host detection and then
2289 * set configuration.
2290 */
2291
2292 /* set the PLL on, remove the HNP/SRP and set the PHY */
Matt Porterf7e504c2013-12-19 09:23:07 -05002293 writel(hsotg->phyif | GUSBCFG_TOutCal(7) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002294 (0x5 << 10), hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002295
2296 s3c_hsotg_init_fifo(hsotg);
2297
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002298 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002299
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002300 writel(1 << 18 | DCFG_DevSpd_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002301
2302 /* Clear any pending OTG interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002303 writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002304
2305 /* Clear any pending interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002306 writel(0xffffffff, hsotg->regs + GINTSTS);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002307
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002308 writel(GINTSTS_ErlySusp | GINTSTS_SessReqInt |
2309 GINTSTS_GOUTNakEff | GINTSTS_GINNakEff |
2310 GINTSTS_ConIDStsChng | GINTSTS_USBRst |
2311 GINTSTS_EnumDone | GINTSTS_OTGInt |
2312 GINTSTS_USBSusp | GINTSTS_WkUpInt,
2313 hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002314
2315 if (using_dma(hsotg))
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002316 writel(GAHBCFG_GlblIntrEn | GAHBCFG_DMAEn |
2317 GAHBCFG_HBstLen_Incr4,
2318 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002319 else
Robert Baldyga8acc8292013-09-19 11:50:23 +02002320 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NPTxFEmpLvl |
2321 GAHBCFG_PTxFEmpLvl) : 0) |
2322 GAHBCFG_GlblIntrEn,
2323 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002324
2325 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002326 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2327 * when we have no data to transfer. Otherwise we get being flooded by
2328 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002329 */
2330
Robert Baldyga8acc8292013-09-19 11:50:23 +02002331 writel(((hsotg->dedicated_fifos) ? DIEPMSK_TxFIFOEmpty |
2332 DIEPMSK_INTknTXFEmpMsk : 0) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002333 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk |
2334 DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2335 DIEPMSK_INTknEPMisMsk,
2336 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002337
2338 /*
2339 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2340 * DMA mode we may need this.
2341 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002342 writel((using_dma(hsotg) ? (DIEPMSK_XferComplMsk |
2343 DIEPMSK_TimeOUTMsk) : 0) |
2344 DOEPMSK_EPDisbldMsk | DOEPMSK_AHBErrMsk |
2345 DOEPMSK_SetupMsk,
2346 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002347
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002348 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002349
2350 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002351 readl(hsotg->regs + DIEPCTL0),
2352 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002353
2354 /* enable in and out endpoint interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002355 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPInt | GINTSTS_IEPInt);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002356
2357 /*
2358 * Enable the RXFIFO when in slave mode, as this is how we collect
2359 * the data. In DMA mode, we get events from the FIFO but also
2360 * things we cannot process, so do not use it.
2361 */
2362 if (!using_dma(hsotg))
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002363 s3c_hsotg_en_gsint(hsotg, GINTSTS_RxFLvl);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002364
2365 /* Enable interrupts for EP0 in and out */
2366 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2367 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2368
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002369 __orr32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002370 udelay(10); /* see openiboot */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002371 __bic32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002372
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002373 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002374
2375 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002376 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002377 * writing to the EPCTL register..
2378 */
2379
2380 /* set to read 1 8byte packet */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002381 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
2382 DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002383
2384 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002385 DxEPCTL_CNAK | DxEPCTL_EPEna |
2386 DxEPCTL_USBActEp,
2387 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002388
2389 /* enable, but don't activate EP0in */
2390 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002391 DxEPCTL_USBActEp, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002392
2393 s3c_hsotg_enqueue_setup(hsotg);
2394
2395 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002396 readl(hsotg->regs + DIEPCTL0),
2397 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002398
2399 /* clear global NAKs */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002400 writel(DCTL_CGOUTNak | DCTL_CGNPInNAK,
2401 hsotg->regs + DCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002402
2403 /* must be at-least 3ms to allow bus to see disconnect */
2404 mdelay(3);
2405
2406 /* remove the soft-disconnect and let's go */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002407 __bic32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002408}
2409
2410/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002411 * s3c_hsotg_irq - handle device interrupt
2412 * @irq: The IRQ number triggered
2413 * @pw: The pw value when registered the handler.
2414 */
2415static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2416{
2417 struct s3c_hsotg *hsotg = pw;
2418 int retry_count = 8;
2419 u32 gintsts;
2420 u32 gintmsk;
2421
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002422 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002423irq_retry:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002424 gintsts = readl(hsotg->regs + GINTSTS);
2425 gintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002426
2427 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2428 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2429
2430 gintsts &= gintmsk;
2431
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002432 if (gintsts & GINTSTS_OTGInt) {
2433 u32 otgint = readl(hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002434
2435 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2436
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002437 writel(otgint, hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002438 }
2439
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002440 if (gintsts & GINTSTS_SessReqInt) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002441 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002442 writel(GINTSTS_SessReqInt, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002443 }
2444
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002445 if (gintsts & GINTSTS_EnumDone) {
2446 writel(GINTSTS_EnumDone, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002447
2448 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002449 }
2450
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002451 if (gintsts & GINTSTS_ConIDStsChng) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002452 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002453 readl(hsotg->regs + DSTS),
2454 readl(hsotg->regs + GOTGCTL));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002455
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002456 writel(GINTSTS_ConIDStsChng, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002457 }
2458
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002459 if (gintsts & (GINTSTS_OEPInt | GINTSTS_IEPInt)) {
2460 u32 daint = readl(hsotg->regs + DAINT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002461 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2462 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002463 int ep;
2464
Robert Baldyga7e804652013-09-19 11:50:20 +02002465 daint &= daintmsk;
2466 daint_out = daint >> DAINT_OutEP_SHIFT;
2467 daint_in = daint & ~(daint_out << DAINT_OutEP_SHIFT);
2468
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002469 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2470
2471 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2472 if (daint_out & 1)
2473 s3c_hsotg_epint(hsotg, ep, 0);
2474 }
2475
2476 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2477 if (daint_in & 1)
2478 s3c_hsotg_epint(hsotg, ep, 1);
2479 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002480 }
2481
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002482 if (gintsts & GINTSTS_USBRst) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002483
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002484 u32 usb_status = readl(hsotg->regs + GOTGCTL);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002485
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002486 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2487 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002488 readl(hsotg->regs + GNPTXSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002489
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002490 writel(GINTSTS_USBRst, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002491
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002492 if (usb_status & GOTGCTL_BSESVLD) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002493 if (time_after(jiffies, hsotg->last_rst +
2494 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002495
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002496 kill_all_requests(hsotg, &hsotg->eps[0],
2497 -ECONNRESET, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002499 s3c_hsotg_core_init(hsotg);
2500 hsotg->last_rst = jiffies;
2501 }
2502 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002503 }
2504
2505 /* check both FIFOs */
2506
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002507 if (gintsts & GINTSTS_NPTxFEmp) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002508 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2509
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002510 /*
2511 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002512 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002513 * it needs re-enabling
2514 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002515
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002516 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002517 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002518 }
2519
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002520 if (gintsts & GINTSTS_PTxFEmp) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002521 dev_dbg(hsotg->dev, "PTxFEmp\n");
2522
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002523 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002524
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002525 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002526 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002527 }
2528
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002529 if (gintsts & GINTSTS_RxFLvl) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002530 /*
2531 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002532 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002533 * set.
2534 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002535
2536 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002537 }
2538
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002539 if (gintsts & GINTSTS_ModeMis) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002540 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002541 writel(GINTSTS_ModeMis, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002542 }
2543
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002544 if (gintsts & GINTSTS_USBSusp) {
2545 dev_info(hsotg->dev, "GINTSTS_USBSusp\n");
2546 writel(GINTSTS_USBSusp, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002547
2548 call_gadget(hsotg, suspend);
2549 }
2550
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002551 if (gintsts & GINTSTS_WkUpInt) {
2552 dev_info(hsotg->dev, "GINTSTS_WkUpIn\n");
2553 writel(GINTSTS_WkUpInt, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002554
2555 call_gadget(hsotg, resume);
2556 }
2557
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002558 if (gintsts & GINTSTS_ErlySusp) {
2559 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
2560 writel(GINTSTS_ErlySusp, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002561 }
2562
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002563 /*
2564 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002565 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002566 * the occurrence.
2567 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002568
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002569 if (gintsts & GINTSTS_GOUTNakEff) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002570 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2571
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002572 writel(DCTL_CGOUTNak, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002573
2574 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002575 }
2576
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002577 if (gintsts & GINTSTS_GINNakEff) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002578 dev_info(hsotg->dev, "GINNakEff triggered\n");
2579
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002580 writel(DCTL_CGNPInNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002581
2582 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002583 }
2584
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002585 /*
2586 * if we've had fifo events, we should try and go around the
2587 * loop again to see if there's any point in returning yet.
2588 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002589
2590 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2591 goto irq_retry;
2592
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002593 spin_unlock(&hsotg->lock);
2594
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002595 return IRQ_HANDLED;
2596}
2597
2598/**
2599 * s3c_hsotg_ep_enable - enable the given endpoint
2600 * @ep: The USB endpint to configure
2601 * @desc: The USB endpoint descriptor to configure with.
2602 *
2603 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002604 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002605static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2606 const struct usb_endpoint_descriptor *desc)
2607{
2608 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2609 struct s3c_hsotg *hsotg = hs_ep->parent;
2610 unsigned long flags;
2611 int index = hs_ep->index;
2612 u32 epctrl_reg;
2613 u32 epctrl;
2614 u32 mps;
2615 int dir_in;
Julia Lawall19c190f2010-03-29 17:36:44 +02002616 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002617
2618 dev_dbg(hsotg->dev,
2619 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2620 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2621 desc->wMaxPacketSize, desc->bInterval);
2622
2623 /* not to be called for EP0 */
2624 WARN_ON(index == 0);
2625
2626 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2627 if (dir_in != hs_ep->dir_in) {
2628 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2629 return -EINVAL;
2630 }
2631
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002632 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002633
2634 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2635
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002636 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002637 epctrl = readl(hsotg->regs + epctrl_reg);
2638
2639 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2640 __func__, epctrl, epctrl_reg);
2641
Lukasz Majewski22258f42012-06-14 10:02:24 +02002642 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002643
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002644 epctrl &= ~(DxEPCTL_EPType_MASK | DxEPCTL_MPS_MASK);
2645 epctrl |= DxEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002646
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002647 /*
2648 * mark the endpoint as active, otherwise the core may ignore
2649 * transactions entirely for this endpoint
2650 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002651 epctrl |= DxEPCTL_USBActEp;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002652
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002653 /*
2654 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002655 * do something with data that we've yet got a request to process
2656 * since the RXFIFO will take data for an endpoint even if the
2657 * size register hasn't been set.
2658 */
2659
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002660 epctrl |= DxEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002661
2662 /* update the endpoint state */
Robert Baldygae9edd1992013-10-09 08:20:02 +02002663 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002664
2665 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02002666 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002667 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002668 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02002669 hs_ep->interval = desc->bInterval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002670
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002671 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2672 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2673
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002674 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2675 case USB_ENDPOINT_XFER_ISOC:
Robert Baldyga1479e842013-10-09 08:41:57 +02002676 epctrl |= DxEPCTL_EPType_Iso;
2677 epctrl |= DxEPCTL_SetEvenFr;
2678 hs_ep->isochronous = 1;
2679 if (dir_in)
2680 hs_ep->periodic = 1;
2681 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002682
2683 case USB_ENDPOINT_XFER_BULK:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002684 epctrl |= DxEPCTL_EPType_Bulk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002685 break;
2686
2687 case USB_ENDPOINT_XFER_INT:
2688 if (dir_in) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002689 /*
2690 * Allocate our TxFNum by simply using the index
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002691 * of the endpoint for the moment. We could do
2692 * something better if the host indicates how
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002693 * many FIFOs we are expecting to use.
2694 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002695
2696 hs_ep->periodic = 1;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002697 epctrl |= DxEPCTL_TxFNum(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002698 }
2699
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002700 epctrl |= DxEPCTL_EPType_Intterupt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002701 break;
2702
2703 case USB_ENDPOINT_XFER_CONTROL:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002704 epctrl |= DxEPCTL_EPType_Control;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002705 break;
2706 }
2707
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002708 /*
2709 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002710 * a unique tx-fifo even if it is non-periodic.
2711 */
2712 if (dir_in && hsotg->dedicated_fifos)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002713 epctrl |= DxEPCTL_TxFNum(index);
Ben Dooks10aebc72010-07-19 09:40:44 +01002714
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002715 /* for non control endpoints, set PID to D0 */
2716 if (index)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002717 epctrl |= DxEPCTL_SetD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002718
2719 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2720 __func__, epctrl);
2721
2722 writel(epctrl, hsotg->regs + epctrl_reg);
2723 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2724 __func__, readl(hsotg->regs + epctrl_reg));
2725
2726 /* enable the endpoint interrupt */
2727 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2728
Lukasz Majewski22258f42012-06-14 10:02:24 +02002729 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002730 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002731}
2732
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002733/**
2734 * s3c_hsotg_ep_disable - disable given endpoint
2735 * @ep: The endpoint to disable.
2736 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002737static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2738{
2739 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2740 struct s3c_hsotg *hsotg = hs_ep->parent;
2741 int dir_in = hs_ep->dir_in;
2742 int index = hs_ep->index;
2743 unsigned long flags;
2744 u32 epctrl_reg;
2745 u32 ctrl;
2746
2747 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2748
2749 if (ep == &hsotg->eps[0].ep) {
2750 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2751 return -EINVAL;
2752 }
2753
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002754 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002755
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002756 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002757 /* terminate all requests with shutdown */
2758 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2759
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002760
2761 ctrl = readl(hsotg->regs + epctrl_reg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002762 ctrl &= ~DxEPCTL_EPEna;
2763 ctrl &= ~DxEPCTL_USBActEp;
2764 ctrl |= DxEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002765
2766 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2767 writel(ctrl, hsotg->regs + epctrl_reg);
2768
2769 /* disable endpoint interrupts */
2770 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2771
Lukasz Majewski22258f42012-06-14 10:02:24 +02002772 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002773 return 0;
2774}
2775
2776/**
2777 * on_list - check request is on the given endpoint
2778 * @ep: The endpoint to check.
2779 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002780 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002781static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2782{
2783 struct s3c_hsotg_req *req, *treq;
2784
2785 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2786 if (req == test)
2787 return true;
2788 }
2789
2790 return false;
2791}
2792
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002793/**
2794 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2795 * @ep: The endpoint to dequeue.
2796 * @req: The request to be removed from a queue.
2797 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002798static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2799{
2800 struct s3c_hsotg_req *hs_req = our_req(req);
2801 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2802 struct s3c_hsotg *hs = hs_ep->parent;
2803 unsigned long flags;
2804
2805 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2806
Lukasz Majewski22258f42012-06-14 10:02:24 +02002807 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002808
2809 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002810 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002811 return -EINVAL;
2812 }
2813
2814 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002815 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002816
2817 return 0;
2818}
2819
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002820/**
2821 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2822 * @ep: The endpoint to set halt.
2823 * @value: Set or unset the halt.
2824 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002825static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2826{
2827 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2828 struct s3c_hsotg *hs = hs_ep->parent;
2829 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002830 u32 epreg;
2831 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002832 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002833
2834 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2835
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002836 /* write both IN and OUT control registers */
2837
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002838 epreg = DIEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002839 epctl = readl(hs->regs + epreg);
2840
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002841 if (value) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002842 epctl |= DxEPCTL_Stall + DxEPCTL_SNAK;
2843 if (epctl & DxEPCTL_EPEna)
2844 epctl |= DxEPCTL_EPDis;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002845 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002846 epctl &= ~DxEPCTL_Stall;
2847 xfertype = epctl & DxEPCTL_EPType_MASK;
2848 if (xfertype == DxEPCTL_EPType_Bulk ||
2849 xfertype == DxEPCTL_EPType_Intterupt)
2850 epctl |= DxEPCTL_SetD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002851 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002852
2853 writel(epctl, hs->regs + epreg);
2854
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002855 epreg = DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002856 epctl = readl(hs->regs + epreg);
2857
2858 if (value)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002859 epctl |= DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002860 else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002861 epctl &= ~DxEPCTL_Stall;
2862 xfertype = epctl & DxEPCTL_EPType_MASK;
2863 if (xfertype == DxEPCTL_EPType_Bulk ||
2864 xfertype == DxEPCTL_EPType_Intterupt)
2865 epctl |= DxEPCTL_SetD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002866 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002867
2868 writel(epctl, hs->regs + epreg);
2869
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002870 hs_ep->halted = value;
2871
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002872 return 0;
2873}
2874
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002875/**
2876 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2877 * @ep: The endpoint to set halt.
2878 * @value: Set or unset the halt.
2879 */
2880static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2881{
2882 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2883 struct s3c_hsotg *hs = hs_ep->parent;
2884 unsigned long flags = 0;
2885 int ret = 0;
2886
2887 spin_lock_irqsave(&hs->lock, flags);
2888 ret = s3c_hsotg_ep_sethalt(ep, value);
2889 spin_unlock_irqrestore(&hs->lock, flags);
2890
2891 return ret;
2892}
2893
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002894static struct usb_ep_ops s3c_hsotg_ep_ops = {
2895 .enable = s3c_hsotg_ep_enable,
2896 .disable = s3c_hsotg_ep_disable,
2897 .alloc_request = s3c_hsotg_ep_alloc_request,
2898 .free_request = s3c_hsotg_ep_free_request,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002899 .queue = s3c_hsotg_ep_queue_lock,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002900 .dequeue = s3c_hsotg_ep_dequeue,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002901 .set_halt = s3c_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002902 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002903};
2904
2905/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002906 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002907 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002908 *
2909 * A wrapper for platform code responsible for controlling
2910 * low-level USB code
2911 */
2912static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2913{
2914 struct platform_device *pdev = to_platform_device(hsotg->dev);
2915
2916 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302917
Matt Porter74084842013-12-19 09:23:06 -05002918 if (hsotg->phy) {
2919 phy_init(hsotg->phy);
2920 phy_power_on(hsotg->phy);
2921 } else if (hsotg->uphy)
2922 usb_phy_init(hsotg->uphy);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302923 else if (hsotg->plat->phy_init)
Lukasz Majewski41188782012-05-04 14:17:01 +02002924 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2925}
2926
2927/**
2928 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002929 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002930 *
2931 * A wrapper for platform code responsible for controlling
2932 * low-level USB code
2933 */
2934static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2935{
2936 struct platform_device *pdev = to_platform_device(hsotg->dev);
2937
Matt Porter74084842013-12-19 09:23:06 -05002938 if (hsotg->phy) {
2939 phy_power_off(hsotg->phy);
2940 phy_exit(hsotg->phy);
2941 } else if (hsotg->uphy)
2942 usb_phy_shutdown(hsotg->uphy);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302943 else if (hsotg->plat->phy_exit)
Lukasz Majewski41188782012-05-04 14:17:01 +02002944 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2945}
2946
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002947/**
2948 * s3c_hsotg_init - initalize the usb core
2949 * @hsotg: The driver state
2950 */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002951static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2952{
2953 /* unmask subset of endpoint interrupts */
2954
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002955 writel(DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2956 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk,
2957 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002958
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002959 writel(DOEPMSK_SetupMsk | DOEPMSK_AHBErrMsk |
2960 DOEPMSK_EPDisbldMsk | DOEPMSK_XferComplMsk,
2961 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002962
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002963 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002964
2965 /* Be in disconnected state until gadget is registered */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002966 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002967
2968 if (0) {
2969 /* post global nak until we're ready */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002970 writel(DCTL_SGNPInNAK | DCTL_SGOUTNak,
2971 hsotg->regs + DCTL);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002972 }
2973
2974 /* setup fifos */
2975
2976 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002977 readl(hsotg->regs + GRXFSIZ),
2978 readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002979
2980 s3c_hsotg_init_fifo(hsotg);
2981
2982 /* set the PLL on, remove the HNP/SRP and set the PHY */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002983 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) | (0x5 << 10),
2984 hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002985
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002986 writel(using_dma(hsotg) ? GAHBCFG_DMAEn : 0x0,
2987 hsotg->regs + GAHBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002988}
2989
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002990/**
2991 * s3c_hsotg_udc_start - prepare the udc for work
2992 * @gadget: The usb gadget state
2993 * @driver: The usb gadget driver
2994 *
2995 * Perform initialization to prepare udc device and driver
2996 * to work.
2997 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002998static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2999 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003000{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02003001 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003002 int ret;
3003
3004 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02003005 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003006 return -ENODEV;
3007 }
3008
3009 if (!driver) {
3010 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3011 return -EINVAL;
3012 }
3013
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01003014 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003015 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003016
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003017 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003018 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3019 return -EINVAL;
3020 }
3021
3022 WARN_ON(hsotg->driver);
3023
3024 driver->driver.bus = NULL;
3025 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03003026 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003027 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3028
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003029 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3030 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003031 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003032 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003033 goto err;
3034 }
3035
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02003036 hsotg->last_rst = jiffies;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003037 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
3038 return 0;
3039
3040err:
3041 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003042 return ret;
3043}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003044
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003045/**
3046 * s3c_hsotg_udc_stop - stop the udc
3047 * @gadget: The usb gadget state
3048 * @driver: The usb gadget driver
3049 *
3050 * Stop udc hw block and stay tunned for future transmissions
3051 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003052static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
3053 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003054{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02003055 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003056 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003057 int ep;
3058
3059 if (!hsotg)
3060 return -ENODEV;
3061
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003062 /* all endpoints should be shutdown */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003063 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003064 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3065
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003066 spin_lock_irqsave(&hsotg->lock, flags);
3067
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003068 s3c_hsotg_phy_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003069
Marek Szyprowskic8c10252013-09-12 16:18:48 +02003070 if (!driver)
3071 hsotg->driver = NULL;
3072
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003073 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003074
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003075 spin_unlock_irqrestore(&hsotg->lock, flags);
3076
Marek Szyprowskic8c10252013-09-12 16:18:48 +02003077 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003078
3079 return 0;
3080}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003081
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003082/**
3083 * s3c_hsotg_gadget_getframe - read the frame number
3084 * @gadget: The usb gadget state
3085 *
3086 * Read the {micro} frame number
3087 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003088static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
3089{
3090 return s3c_hsotg_read_frameno(to_hsotg(gadget));
3091}
3092
Lukasz Majewskia188b682012-06-22 09:29:56 +02003093/**
3094 * s3c_hsotg_pullup - connect/disconnect the USB PHY
3095 * @gadget: The usb gadget state
3096 * @is_on: Current state of the USB PHY
3097 *
3098 * Connect/Disconnect the USB PHY pullup
3099 */
3100static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
3101{
3102 struct s3c_hsotg *hsotg = to_hsotg(gadget);
3103 unsigned long flags = 0;
3104
3105 dev_dbg(hsotg->dev, "%s: is_in: %d\n", __func__, is_on);
3106
3107 spin_lock_irqsave(&hsotg->lock, flags);
3108 if (is_on) {
3109 s3c_hsotg_phy_enable(hsotg);
3110 s3c_hsotg_core_init(hsotg);
3111 } else {
3112 s3c_hsotg_disconnect(hsotg);
3113 s3c_hsotg_phy_disable(hsotg);
3114 }
3115
3116 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3117 spin_unlock_irqrestore(&hsotg->lock, flags);
3118
3119 return 0;
3120}
3121
Felipe Balbieeef4582013-01-24 17:58:16 +02003122static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003123 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003124 .udc_start = s3c_hsotg_udc_start,
3125 .udc_stop = s3c_hsotg_udc_stop,
Lukasz Majewskia188b682012-06-22 09:29:56 +02003126 .pullup = s3c_hsotg_pullup,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003127};
3128
3129/**
3130 * s3c_hsotg_initep - initialise a single endpoint
3131 * @hsotg: The device state.
3132 * @hs_ep: The endpoint to be initialised.
3133 * @epnum: The endpoint number
3134 *
3135 * Initialise the given endpoint (as part of the probe and device state
3136 * creation) to give to the gadget driver. Setup the endpoint name, any
3137 * direction information and other state that may be required.
3138 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003139static void s3c_hsotg_initep(struct s3c_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003140 struct s3c_hsotg_ep *hs_ep,
3141 int epnum)
3142{
3143 u32 ptxfifo;
3144 char *dir;
3145
3146 if (epnum == 0)
3147 dir = "";
3148 else if ((epnum % 2) == 0) {
3149 dir = "out";
3150 } else {
3151 dir = "in";
3152 hs_ep->dir_in = 1;
3153 }
3154
3155 hs_ep->index = epnum;
3156
3157 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3158
3159 INIT_LIST_HEAD(&hs_ep->queue);
3160 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3161
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003162 /* add to the list of endpoints known by the gadget driver */
3163 if (epnum)
3164 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3165
3166 hs_ep->parent = hsotg;
3167 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003168 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003169 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3170
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003171 /*
3172 * Read the FIFO size for the Periodic TX FIFO, even if we're
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003173 * an OUT endpoint, we may as well do this if in future the
3174 * code is changed to make each endpoint's direction changeable.
3175 */
3176
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003177 ptxfifo = readl(hsotg->regs + DPTXFSIZn(epnum));
3178 hs_ep->fifo_size = DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003179
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003180 /*
3181 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003182 * to be something valid.
3183 */
3184
3185 if (using_dma(hsotg)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003186 u32 next = DxEPCTL_NextEp((epnum + 1) % 15);
3187 writel(next, hsotg->regs + DIEPCTL(epnum));
3188 writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003189 }
3190}
3191
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003192/**
3193 * s3c_hsotg_hw_cfg - read HW configuration registers
3194 * @param: The device state
3195 *
3196 * Read the USB core HW configuration registers
3197 */
3198static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003199{
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003200 u32 cfg2, cfg4;
Ben Dooks10aebc72010-07-19 09:40:44 +01003201 /* check hardware configuration */
3202
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003203 cfg2 = readl(hsotg->regs + 0x48);
3204 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3205
3206 dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps);
3207
Ben Dooks10aebc72010-07-19 09:40:44 +01003208 cfg4 = readl(hsotg->regs + 0x50);
3209 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3210
3211 dev_info(hsotg->dev, "%s fifos\n",
3212 hsotg->dedicated_fifos ? "dedicated" : "shared");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003213}
3214
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003215/**
3216 * s3c_hsotg_dump - dump state of the udc
3217 * @param: The device state
3218 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003219static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
3220{
Mark Brown83a01802011-06-01 17:16:15 +01003221#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003222 struct device *dev = hsotg->dev;
3223 void __iomem *regs = hsotg->regs;
3224 u32 val;
3225 int idx;
3226
3227 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003228 readl(regs + DCFG), readl(regs + DCTL),
3229 readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003230
3231 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003232 readl(regs + GAHBCFG), readl(regs + 0x44));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003233
3234 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003235 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003236
3237 /* show periodic fifo settings */
3238
3239 for (idx = 1; idx <= 15; idx++) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003240 val = readl(regs + DPTXFSIZn(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003241 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003242 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3243 val & DPTXFSIZn_DPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003244 }
3245
3246 for (idx = 0; idx < 15; idx++) {
3247 dev_info(dev,
3248 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003249 readl(regs + DIEPCTL(idx)),
3250 readl(regs + DIEPTSIZ(idx)),
3251 readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003252
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003253 val = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003254 dev_info(dev,
3255 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003256 idx, readl(regs + DOEPCTL(idx)),
3257 readl(regs + DOEPTSIZ(idx)),
3258 readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003259
3260 }
3261
3262 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003263 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003264#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003265}
3266
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003267/**
3268 * state_show - debugfs: show overall driver and device state.
3269 * @seq: The seq file to write to.
3270 * @v: Unused parameter.
3271 *
3272 * This debugfs entry shows the overall state of the hardware and
3273 * some general information about each of the endpoints available
3274 * to the system.
3275 */
3276static int state_show(struct seq_file *seq, void *v)
3277{
3278 struct s3c_hsotg *hsotg = seq->private;
3279 void __iomem *regs = hsotg->regs;
3280 int idx;
3281
3282 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003283 readl(regs + DCFG),
3284 readl(regs + DCTL),
3285 readl(regs + DSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003286
3287 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003288 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003289
3290 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003291 readl(regs + GINTMSK),
3292 readl(regs + GINTSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003293
3294 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003295 readl(regs + DAINTMSK),
3296 readl(regs + DAINT));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003297
3298 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003299 readl(regs + GNPTXSTS),
3300 readl(regs + GRXSTSR));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003301
Pavel Macheka023da32013-09-30 14:56:02 +02003302 seq_puts(seq, "\nEndpoint status:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003303
3304 for (idx = 0; idx < 15; idx++) {
3305 u32 in, out;
3306
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003307 in = readl(regs + DIEPCTL(idx));
3308 out = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003309
3310 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3311 idx, in, out);
3312
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003313 in = readl(regs + DIEPTSIZ(idx));
3314 out = readl(regs + DOEPTSIZ(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003315
3316 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3317 in, out);
3318
Pavel Macheka023da32013-09-30 14:56:02 +02003319 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003320 }
3321
3322 return 0;
3323}
3324
3325static int state_open(struct inode *inode, struct file *file)
3326{
3327 return single_open(file, state_show, inode->i_private);
3328}
3329
3330static const struct file_operations state_fops = {
3331 .owner = THIS_MODULE,
3332 .open = state_open,
3333 .read = seq_read,
3334 .llseek = seq_lseek,
3335 .release = single_release,
3336};
3337
3338/**
3339 * fifo_show - debugfs: show the fifo information
3340 * @seq: The seq_file to write data to.
3341 * @v: Unused parameter.
3342 *
3343 * Show the FIFO information for the overall fifo and all the
3344 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003345 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003346static int fifo_show(struct seq_file *seq, void *v)
3347{
3348 struct s3c_hsotg *hsotg = seq->private;
3349 void __iomem *regs = hsotg->regs;
3350 u32 val;
3351 int idx;
3352
Pavel Macheka023da32013-09-30 14:56:02 +02003353 seq_puts(seq, "Non-periodic FIFOs:\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003354 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003355
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003356 val = readl(regs + GNPTXFSIZ);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003357 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003358 val >> GNPTXFSIZ_NPTxFDep_SHIFT,
3359 val & GNPTXFSIZ_NPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003360
Pavel Macheka023da32013-09-30 14:56:02 +02003361 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003362
3363 for (idx = 1; idx <= 15; idx++) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003364 val = readl(regs + DPTXFSIZn(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003365
3366 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003367 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3368 val & DPTXFSIZn_DPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003369 }
3370
3371 return 0;
3372}
3373
3374static int fifo_open(struct inode *inode, struct file *file)
3375{
3376 return single_open(file, fifo_show, inode->i_private);
3377}
3378
3379static const struct file_operations fifo_fops = {
3380 .owner = THIS_MODULE,
3381 .open = fifo_open,
3382 .read = seq_read,
3383 .llseek = seq_lseek,
3384 .release = single_release,
3385};
3386
3387
3388static const char *decode_direction(int is_in)
3389{
3390 return is_in ? "in" : "out";
3391}
3392
3393/**
3394 * ep_show - debugfs: show the state of an endpoint.
3395 * @seq: The seq_file to write data to.
3396 * @v: Unused parameter.
3397 *
3398 * This debugfs entry shows the state of the given endpoint (one is
3399 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003400 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003401static int ep_show(struct seq_file *seq, void *v)
3402{
3403 struct s3c_hsotg_ep *ep = seq->private;
3404 struct s3c_hsotg *hsotg = ep->parent;
3405 struct s3c_hsotg_req *req;
3406 void __iomem *regs = hsotg->regs;
3407 int index = ep->index;
3408 int show_limit = 15;
3409 unsigned long flags;
3410
3411 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3412 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3413
3414 /* first show the register state */
3415
3416 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003417 readl(regs + DIEPCTL(index)),
3418 readl(regs + DOEPCTL(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003419
3420 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003421 readl(regs + DIEPDMA(index)),
3422 readl(regs + DOEPDMA(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003423
3424 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003425 readl(regs + DIEPINT(index)),
3426 readl(regs + DOEPINT(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003427
3428 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003429 readl(regs + DIEPTSIZ(index)),
3430 readl(regs + DOEPTSIZ(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003431
Pavel Macheka023da32013-09-30 14:56:02 +02003432 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003433 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3434 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3435
3436 seq_printf(seq, "request list (%p,%p):\n",
3437 ep->queue.next, ep->queue.prev);
3438
Lukasz Majewski22258f42012-06-14 10:02:24 +02003439 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003440
3441 list_for_each_entry(req, &ep->queue, queue) {
3442 if (--show_limit < 0) {
Pavel Macheka023da32013-09-30 14:56:02 +02003443 seq_puts(seq, "not showing more requests...\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003444 break;
3445 }
3446
3447 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3448 req == ep->req ? '*' : ' ',
3449 req, req->req.length, req->req.buf);
3450 seq_printf(seq, "%d done, res %d\n",
3451 req->req.actual, req->req.status);
3452 }
3453
Lukasz Majewski22258f42012-06-14 10:02:24 +02003454 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003455
3456 return 0;
3457}
3458
3459static int ep_open(struct inode *inode, struct file *file)
3460{
3461 return single_open(file, ep_show, inode->i_private);
3462}
3463
3464static const struct file_operations ep_fops = {
3465 .owner = THIS_MODULE,
3466 .open = ep_open,
3467 .read = seq_read,
3468 .llseek = seq_lseek,
3469 .release = single_release,
3470};
3471
3472/**
3473 * s3c_hsotg_create_debug - create debugfs directory and files
3474 * @hsotg: The driver state
3475 *
3476 * Create the debugfs files to allow the user to get information
3477 * about the state of the system. The directory name is created
3478 * with the same name as the device itself, in case we end up
3479 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003480 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003481static void s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003482{
3483 struct dentry *root;
3484 unsigned epidx;
3485
3486 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3487 hsotg->debug_root = root;
3488 if (IS_ERR(root)) {
3489 dev_err(hsotg->dev, "cannot create debug root\n");
3490 return;
3491 }
3492
3493 /* create general state file */
3494
3495 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3496 hsotg, &state_fops);
3497
3498 if (IS_ERR(hsotg->debug_file))
3499 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3500
3501 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3502 hsotg, &fifo_fops);
3503
3504 if (IS_ERR(hsotg->debug_fifo))
3505 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3506
3507 /* create one file for each endpoint */
3508
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003509 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003510 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3511
3512 ep->debugfs = debugfs_create_file(ep->name, 0444,
3513 root, ep, &ep_fops);
3514
3515 if (IS_ERR(ep->debugfs))
3516 dev_err(hsotg->dev, "failed to create %s debug file\n",
3517 ep->name);
3518 }
3519}
3520
3521/**
3522 * s3c_hsotg_delete_debug - cleanup debugfs entries
3523 * @hsotg: The driver state
3524 *
3525 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003526 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05003527static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003528{
3529 unsigned epidx;
3530
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003531 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003532 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3533 debugfs_remove(ep->debugfs);
3534 }
3535
3536 debugfs_remove(hsotg->debug_file);
3537 debugfs_remove(hsotg->debug_fifo);
3538 debugfs_remove(hsotg->debug_root);
3539}
3540
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003541/**
3542 * s3c_hsotg_probe - probe function for hsotg driver
3543 * @pdev: The platform information for the driver
3544 */
Lukasz Majewskif026a522012-05-04 14:17:13 +02003545
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003546static int s3c_hsotg_probe(struct platform_device *pdev)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003547{
Jingoo Hane01ee9f2013-07-30 17:00:51 +09003548 struct s3c_hsotg_plat *plat = dev_get_platdata(&pdev->dev);
Matt Porter74084842013-12-19 09:23:06 -05003549 struct phy *phy;
3550 struct usb_phy *uphy;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003551 struct device *dev = &pdev->dev;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003552 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003553 struct s3c_hsotg *hsotg;
3554 struct resource *res;
3555 int epnum;
3556 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003557 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003558
Sachin Kamat338edab2012-05-18 14:33:46 +05303559 hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003560 if (!hsotg) {
3561 dev_err(dev, "cannot get memory\n");
3562 return -ENOMEM;
3563 }
3564
Matt Porter74084842013-12-19 09:23:06 -05003565 /*
3566 * Attempt to find a generic PHY, then look for an old style
3567 * USB PHY, finally fall back to pdata
3568 */
3569 phy = devm_phy_get(&pdev->dev, "usb2-phy");
Felipe Balbif4f5ba52013-03-15 10:56:19 +02003570 if (IS_ERR(phy)) {
Matt Porter74084842013-12-19 09:23:06 -05003571 uphy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
3572 if (IS_ERR(uphy)) {
3573 /* Fallback for pdata */
3574 plat = dev_get_platdata(&pdev->dev);
3575 if (!plat) {
3576 dev_err(&pdev->dev,
3577 "no platform data or transceiver defined\n");
3578 return -EPROBE_DEFER;
3579 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303580 hsotg->plat = plat;
Matt Porter74084842013-12-19 09:23:06 -05003581 } else
3582 hsotg->uphy = uphy;
3583 } else
Praveen Panerib2e587d2012-11-14 15:57:16 +05303584 hsotg->phy = phy;
Praveen Panerib2e587d2012-11-14 15:57:16 +05303585
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003586 hsotg->dev = dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003587
Sachin Kamat84749c62012-09-03 16:15:18 +05303588 hsotg->clk = devm_clk_get(&pdev->dev, "otg");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003589 if (IS_ERR(hsotg->clk)) {
3590 dev_err(dev, "cannot get otg clock\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303591 return PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003592 }
3593
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003594 platform_set_drvdata(pdev, hsotg);
3595
3596 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003597
Thierry Reding148e1132013-01-21 11:09:22 +01003598 hsotg->regs = devm_ioremap_resource(&pdev->dev, res);
3599 if (IS_ERR(hsotg->regs)) {
3600 ret = PTR_ERR(hsotg->regs);
Sachin Kamat338edab2012-05-18 14:33:46 +05303601 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003602 }
3603
3604 ret = platform_get_irq(pdev, 0);
3605 if (ret < 0) {
3606 dev_err(dev, "cannot find IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303607 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003608 }
3609
Lukasz Majewski22258f42012-06-14 10:02:24 +02003610 spin_lock_init(&hsotg->lock);
3611
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003612 hsotg->irq = ret;
3613
Sachin Kamat338edab2012-05-18 14:33:46 +05303614 ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0,
3615 dev_name(dev), hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003616 if (ret < 0) {
3617 dev_err(dev, "cannot claim IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303618 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003619 }
3620
3621 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3622
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003623 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003624 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3625 hsotg->gadget.name = dev_name(dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003626
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003627 /* reset the system */
3628
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003629 clk_prepare_enable(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003630
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003631 /* regulators */
3632
3633 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3634 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3635
Sachin Kamatcd762132013-01-08 14:27:00 +05303636 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003637 hsotg->supplies);
3638 if (ret) {
3639 dev_err(dev, "failed to request supplies: %d\n", ret);
Sachin Kamat338edab2012-05-18 14:33:46 +05303640 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003641 }
3642
3643 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3644 hsotg->supplies);
3645
3646 if (ret) {
3647 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3648 goto err_supplies;
3649 }
3650
Matt Porterf7e504c2013-12-19 09:23:07 -05003651 /* Set default UTMI width */
3652 hsotg->phyif = GUSBCFG_PHYIf16;
3653
3654 /*
3655 * If using the generic PHY framework, check if the PHY bus
3656 * width is 8-bit and set the phyif appropriately.
3657 */
3658 if (hsotg->phy && (phy_get_bus_width(phy) == 8))
3659 hsotg->phyif = GUSBCFG_PHYIf8;
3660
Matt Porter74084842013-12-19 09:23:06 -05003661 if (hsotg->phy)
3662 phy_init(hsotg->phy);
3663
Lukasz Majewski41188782012-05-04 14:17:01 +02003664 /* usb phy enable */
3665 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003666
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003667 s3c_hsotg_corereset(hsotg);
3668 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003669 s3c_hsotg_hw_cfg(hsotg);
3670
3671 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3672
3673 if (hsotg->num_of_eps == 0) {
3674 dev_err(dev, "wrong number of EPs (zero)\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003675 ret = -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003676 goto err_supplies;
3677 }
3678
3679 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3680 GFP_KERNEL);
3681 if (!eps) {
3682 dev_err(dev, "cannot get memory\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003683 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003684 goto err_supplies;
3685 }
3686
3687 hsotg->eps = eps;
3688
3689 /* setup endpoint information */
3690
3691 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3692 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3693
3694 /* allocate EP0 request */
3695
3696 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3697 GFP_KERNEL);
3698 if (!hsotg->ctrl_req) {
3699 dev_err(dev, "failed to allocate ctrl req\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003700 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003701 goto err_ep_mem;
3702 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003703
3704 /* initialise the endpoints now the core has been initialised */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003705 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003706 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3707
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003708 /* disable power and clock */
3709
3710 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3711 hsotg->supplies);
3712 if (ret) {
3713 dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
3714 goto err_ep_mem;
3715 }
3716
3717 s3c_hsotg_phy_disable(hsotg);
3718
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003719 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3720 if (ret)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003721 goto err_ep_mem;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003722
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003723 s3c_hsotg_create_debug(hsotg);
3724
3725 s3c_hsotg_dump(hsotg);
3726
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003727 return 0;
3728
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003729err_ep_mem:
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003730 kfree(eps);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003731err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003732 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003733err_clk:
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003734 clk_disable_unprepare(hsotg->clk);
Sachin Kamat338edab2012-05-18 14:33:46 +05303735
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003736 return ret;
3737}
3738
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003739/**
3740 * s3c_hsotg_remove - remove function for hsotg driver
3741 * @pdev: The platform information for the driver
3742 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05003743static int s3c_hsotg_remove(struct platform_device *pdev)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003744{
3745 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3746
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003747 usb_del_gadget_udc(&hsotg->gadget);
3748
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003749 s3c_hsotg_delete_debug(hsotg);
3750
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003751 if (hsotg->driver) {
3752 /* should have been done already by driver model core */
3753 usb_gadget_unregister_driver(hsotg->driver);
3754 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003755
Lukasz Majewski41188782012-05-04 14:17:01 +02003756 s3c_hsotg_phy_disable(hsotg);
Matt Porter74084842013-12-19 09:23:06 -05003757 if (hsotg->phy)
3758 phy_exit(hsotg->phy);
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003759 clk_disable_unprepare(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003760
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003761 return 0;
3762}
3763
3764#if 1
3765#define s3c_hsotg_suspend NULL
3766#define s3c_hsotg_resume NULL
3767#endif
3768
Tomasz Figac50f056c2013-06-25 17:38:23 +02003769#ifdef CONFIG_OF
3770static const struct of_device_id s3c_hsotg_of_ids[] = {
3771 { .compatible = "samsung,s3c6400-hsotg", },
Matt Porter0d33d822013-12-19 09:23:05 -05003772 { .compatible = "snps,dwc2", },
Tomasz Figac50f056c2013-06-25 17:38:23 +02003773 { /* sentinel */ }
3774};
3775MODULE_DEVICE_TABLE(of, s3c_hsotg_of_ids);
3776#endif
3777
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003778static struct platform_driver s3c_hsotg_driver = {
3779 .driver = {
3780 .name = "s3c-hsotg",
3781 .owner = THIS_MODULE,
Tomasz Figac50f056c2013-06-25 17:38:23 +02003782 .of_match_table = of_match_ptr(s3c_hsotg_of_ids),
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003783 },
3784 .probe = s3c_hsotg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05003785 .remove = s3c_hsotg_remove,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003786 .suspend = s3c_hsotg_suspend,
3787 .resume = s3c_hsotg_resume,
3788};
3789
Axel Lincc27c962011-11-27 20:16:27 +08003790module_platform_driver(s3c_hsotg_driver);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003791
3792MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3793MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3794MODULE_LICENSE("GPL");
3795MODULE_ALIAS("platform:s3c-hsotg");