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