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