usb: gadget: at91_udc: Remove unneeded variable
[linux-2.6-block.git] / drivers / usb / gadget / udc / fotg210-udc.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
b84a8dee
YHC
2/*
3 * FOTG210 UDC Driver supports Bulk transfer so far
4 *
5 * Copyright (C) 2013 Faraday Technology Corporation
6 *
7 * Author : Yuan-Hsin Chen <yhchen@faraday-tech.com>
b84a8dee
YHC
8 */
9
10#include <linux/dma-mapping.h>
11#include <linux/err.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/usb/ch9.h>
17#include <linux/usb/gadget.h>
18
19#include "fotg210.h"
20
21#define DRIVER_DESC "FOTG210 USB Device Controller Driver"
22#define DRIVER_VERSION "30-April-2013"
23
24static const char udc_name[] = "fotg210_udc";
25static const char * const fotg210_ep_name[] = {
26 "ep0", "ep1", "ep2", "ep3", "ep4"};
27
28static void fotg210_disable_fifo_int(struct fotg210_ep *ep)
29{
30 u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
31
32 if (ep->dir_in)
33 value |= DMISGR1_MF_IN_INT(ep->epnum - 1);
34 else
35 value |= DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
36 iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
37}
38
39static void fotg210_enable_fifo_int(struct fotg210_ep *ep)
40{
41 u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
42
43 if (ep->dir_in)
44 value &= ~DMISGR1_MF_IN_INT(ep->epnum - 1);
45 else
46 value &= ~DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
47 iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
48}
49
50static void fotg210_set_cxdone(struct fotg210_udc *fotg210)
51{
52 u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
53
54 value |= DCFESR_CX_DONE;
55 iowrite32(value, fotg210->reg + FOTG210_DCFESR);
56}
57
58static void fotg210_done(struct fotg210_ep *ep, struct fotg210_request *req,
59 int status)
60{
61 list_del_init(&req->queue);
62
63 /* don't modify queue heads during completion callback */
64 if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
65 req->req.status = -ESHUTDOWN;
66 else
67 req->req.status = status;
68
69 spin_unlock(&ep->fotg210->lock);
304f7e5e 70 usb_gadget_giveback_request(&ep->ep, &req->req);
b84a8dee
YHC
71 spin_lock(&ep->fotg210->lock);
72
73 if (ep->epnum) {
74 if (list_empty(&ep->queue))
75 fotg210_disable_fifo_int(ep);
76 } else {
77 fotg210_set_cxdone(ep->fotg210);
78 }
79}
80
81static void fotg210_fifo_ep_mapping(struct fotg210_ep *ep, u32 epnum,
82 u32 dir_in)
83{
84 struct fotg210_udc *fotg210 = ep->fotg210;
85 u32 val;
86
87 /* Driver should map an ep to a fifo and then map the fifo
88 * to the ep. What a brain-damaged design!
89 */
90
91 /* map a fifo to an ep */
92 val = ioread32(fotg210->reg + FOTG210_EPMAP);
93 val &= ~EPMAP_FIFONOMSK(epnum, dir_in);
94 val |= EPMAP_FIFONO(epnum, dir_in);
95 iowrite32(val, fotg210->reg + FOTG210_EPMAP);
96
97 /* map the ep to the fifo */
98 val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
99 val &= ~FIFOMAP_EPNOMSK(epnum);
100 val |= FIFOMAP_EPNO(epnum);
101 iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
102
103 /* enable fifo */
104 val = ioread32(fotg210->reg + FOTG210_FIFOCF);
105 val |= FIFOCF_FIFO_EN(epnum - 1);
106 iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
107}
108
109static void fotg210_set_fifo_dir(struct fotg210_ep *ep, u32 epnum, u32 dir_in)
110{
111 struct fotg210_udc *fotg210 = ep->fotg210;
112 u32 val;
113
114 val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
115 val |= (dir_in ? FIFOMAP_DIRIN(epnum - 1) : FIFOMAP_DIROUT(epnum - 1));
116 iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
117}
118
119static void fotg210_set_tfrtype(struct fotg210_ep *ep, u32 epnum, u32 type)
120{
121 struct fotg210_udc *fotg210 = ep->fotg210;
122 u32 val;
123
124 val = ioread32(fotg210->reg + FOTG210_FIFOCF);
125 val |= FIFOCF_TYPE(type, epnum - 1);
126 iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
127}
128
129static void fotg210_set_mps(struct fotg210_ep *ep, u32 epnum, u32 mps,
130 u32 dir_in)
131{
132 struct fotg210_udc *fotg210 = ep->fotg210;
133 u32 val;
134 u32 offset = dir_in ? FOTG210_INEPMPSR(epnum) :
135 FOTG210_OUTEPMPSR(epnum);
136
137 val = ioread32(fotg210->reg + offset);
138 val |= INOUTEPMPSR_MPS(mps);
139 iowrite32(val, fotg210->reg + offset);
140}
141
142static int fotg210_config_ep(struct fotg210_ep *ep,
143 const struct usb_endpoint_descriptor *desc)
144{
145 struct fotg210_udc *fotg210 = ep->fotg210;
146
147 fotg210_set_fifo_dir(ep, ep->epnum, ep->dir_in);
148 fotg210_set_tfrtype(ep, ep->epnum, ep->type);
149 fotg210_set_mps(ep, ep->epnum, ep->ep.maxpacket, ep->dir_in);
150 fotg210_fifo_ep_mapping(ep, ep->epnum, ep->dir_in);
151
152 fotg210->ep[ep->epnum] = ep;
153
154 return 0;
155}
156
157static int fotg210_ep_enable(struct usb_ep *_ep,
158 const struct usb_endpoint_descriptor *desc)
159{
160 struct fotg210_ep *ep;
161
162 ep = container_of(_ep, struct fotg210_ep, ep);
163
164 ep->desc = desc;
165 ep->epnum = usb_endpoint_num(desc);
166 ep->type = usb_endpoint_type(desc);
167 ep->dir_in = usb_endpoint_dir_in(desc);
168 ep->ep.maxpacket = usb_endpoint_maxp(desc);
169
170 return fotg210_config_ep(ep, desc);
171}
172
173static void fotg210_reset_tseq(struct fotg210_udc *fotg210, u8 epnum)
174{
175 struct fotg210_ep *ep = fotg210->ep[epnum];
176 u32 value;
177 void __iomem *reg;
178
179 reg = (ep->dir_in) ?
180 fotg210->reg + FOTG210_INEPMPSR(epnum) :
181 fotg210->reg + FOTG210_OUTEPMPSR(epnum);
182
183 /* Note: Driver needs to set and clear INOUTEPMPSR_RESET_TSEQ
184 * bit. Controller wouldn't clear this bit. WTF!!!
185 */
186
187 value = ioread32(reg);
188 value |= INOUTEPMPSR_RESET_TSEQ;
189 iowrite32(value, reg);
190
191 value = ioread32(reg);
192 value &= ~INOUTEPMPSR_RESET_TSEQ;
193 iowrite32(value, reg);
194}
195
196static int fotg210_ep_release(struct fotg210_ep *ep)
197{
198 if (!ep->epnum)
199 return 0;
200 ep->epnum = 0;
201 ep->stall = 0;
202 ep->wedged = 0;
203
204 fotg210_reset_tseq(ep->fotg210, ep->epnum);
205
206 return 0;
207}
208
209static int fotg210_ep_disable(struct usb_ep *_ep)
210{
211 struct fotg210_ep *ep;
212 struct fotg210_request *req;
213 unsigned long flags;
214
215 BUG_ON(!_ep);
216
217 ep = container_of(_ep, struct fotg210_ep, ep);
218
219 while (!list_empty(&ep->queue)) {
220 req = list_entry(ep->queue.next,
221 struct fotg210_request, queue);
222 spin_lock_irqsave(&ep->fotg210->lock, flags);
223 fotg210_done(ep, req, -ECONNRESET);
224 spin_unlock_irqrestore(&ep->fotg210->lock, flags);
225 }
226
227 return fotg210_ep_release(ep);
228}
229
230static struct usb_request *fotg210_ep_alloc_request(struct usb_ep *_ep,
231 gfp_t gfp_flags)
232{
233 struct fotg210_request *req;
234
235 req = kzalloc(sizeof(struct fotg210_request), gfp_flags);
236 if (!req)
237 return NULL;
238
239 INIT_LIST_HEAD(&req->queue);
240
241 return &req->req;
242}
243
244static void fotg210_ep_free_request(struct usb_ep *_ep,
245 struct usb_request *_req)
246{
247 struct fotg210_request *req;
248
249 req = container_of(_req, struct fotg210_request, req);
250 kfree(req);
251}
252
253static void fotg210_enable_dma(struct fotg210_ep *ep,
254 dma_addr_t d, u32 len)
255{
256 u32 value;
257 struct fotg210_udc *fotg210 = ep->fotg210;
258
259 /* set transfer length and direction */
260 value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
261 value &= ~(DMACPSR1_DMA_LEN(0xFFFF) | DMACPSR1_DMA_TYPE(1));
262 value |= DMACPSR1_DMA_LEN(len) | DMACPSR1_DMA_TYPE(ep->dir_in);
263 iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
264
265 /* set device DMA target FIFO number */
266 value = ioread32(fotg210->reg + FOTG210_DMATFNR);
267 if (ep->epnum)
268 value |= DMATFNR_ACC_FN(ep->epnum - 1);
269 else
270 value |= DMATFNR_ACC_CXF;
271 iowrite32(value, fotg210->reg + FOTG210_DMATFNR);
272
273 /* set DMA memory address */
274 iowrite32(d, fotg210->reg + FOTG210_DMACPSR2);
275
276 /* enable MDMA_EROR and MDMA_CMPLT interrupt */
277 value = ioread32(fotg210->reg + FOTG210_DMISGR2);
278 value &= ~(DMISGR2_MDMA_CMPLT | DMISGR2_MDMA_ERROR);
279 iowrite32(value, fotg210->reg + FOTG210_DMISGR2);
280
281 /* start DMA */
282 value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
283 value |= DMACPSR1_DMA_START;
284 iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
285}
286
287static void fotg210_disable_dma(struct fotg210_ep *ep)
288{
289 iowrite32(DMATFNR_DISDMA, ep->fotg210->reg + FOTG210_DMATFNR);
290}
291
292static void fotg210_wait_dma_done(struct fotg210_ep *ep)
293{
294 u32 value;
295
296 do {
297 value = ioread32(ep->fotg210->reg + FOTG210_DISGR2);
298 if ((value & DISGR2_USBRST_INT) ||
299 (value & DISGR2_DMA_ERROR))
300 goto dma_reset;
301 } while (!(value & DISGR2_DMA_CMPLT));
302
303 value &= ~DISGR2_DMA_CMPLT;
304 iowrite32(value, ep->fotg210->reg + FOTG210_DISGR2);
305 return;
306
307dma_reset:
308 value = ioread32(ep->fotg210->reg + FOTG210_DMACPSR1);
309 value |= DMACPSR1_DMA_ABORT;
310 iowrite32(value, ep->fotg210->reg + FOTG210_DMACPSR1);
311
312 /* reset fifo */
313 if (ep->epnum) {
314 value = ioread32(ep->fotg210->reg +
315 FOTG210_FIBCR(ep->epnum - 1));
316 value |= FIBCR_FFRST;
317 iowrite32(value, ep->fotg210->reg +
318 FOTG210_FIBCR(ep->epnum - 1));
319 } else {
320 value = ioread32(ep->fotg210->reg + FOTG210_DCFESR);
321 value |= DCFESR_CX_CLR;
322 iowrite32(value, ep->fotg210->reg + FOTG210_DCFESR);
323 }
324}
325
326static void fotg210_start_dma(struct fotg210_ep *ep,
327 struct fotg210_request *req)
328{
e26bdb01 329 struct device *dev = &ep->fotg210->gadget.dev;
b84a8dee
YHC
330 dma_addr_t d;
331 u8 *buffer;
332 u32 length;
333
334 if (ep->epnum) {
335 if (ep->dir_in) {
336 buffer = req->req.buf;
337 length = req->req.length;
338 } else {
339 buffer = req->req.buf + req->req.actual;
340 length = ioread32(ep->fotg210->reg +
341 FOTG210_FIBCR(ep->epnum - 1));
342 length &= FIBCR_BCFX;
343 }
344 } else {
345 buffer = req->req.buf + req->req.actual;
346 if (req->req.length - req->req.actual > ep->ep.maxpacket)
347 length = ep->ep.maxpacket;
348 else
349 length = req->req.length;
350 }
351
e26bdb01 352 d = dma_map_single(dev, buffer, length,
b84a8dee
YHC
353 ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
354
e26bdb01 355 if (dma_mapping_error(dev, d)) {
b84a8dee
YHC
356 pr_err("dma_mapping_error\n");
357 return;
358 }
359
b84a8dee
YHC
360 fotg210_enable_dma(ep, d, length);
361
362 /* check if dma is done */
363 fotg210_wait_dma_done(ep);
364
365 fotg210_disable_dma(ep);
366
367 /* update actual transfer length */
368 req->req.actual += length;
369
e26bdb01 370 dma_unmap_single(dev, d, length, DMA_TO_DEVICE);
b84a8dee
YHC
371}
372
373static void fotg210_ep0_queue(struct fotg210_ep *ep,
374 struct fotg210_request *req)
375{
376 if (!req->req.length) {
377 fotg210_done(ep, req, 0);
378 return;
379 }
380 if (ep->dir_in) { /* if IN */
1c99cabf 381 fotg210_start_dma(ep, req);
b84a8dee
YHC
382 if ((req->req.length == req->req.actual) ||
383 (req->req.actual < ep->ep.maxpacket))
384 fotg210_done(ep, req, 0);
385 } else { /* OUT */
1c99cabf 386 u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR0);
b84a8dee 387
1c99cabf
DC
388 value &= ~DMISGR0_MCX_OUT_INT;
389 iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR0);
b84a8dee
YHC
390 }
391}
392
393static int fotg210_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
394 gfp_t gfp_flags)
395{
396 struct fotg210_ep *ep;
397 struct fotg210_request *req;
398 unsigned long flags;
399 int request = 0;
400
401 ep = container_of(_ep, struct fotg210_ep, ep);
402 req = container_of(_req, struct fotg210_request, req);
403
404 if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
405 return -ESHUTDOWN;
406
407 spin_lock_irqsave(&ep->fotg210->lock, flags);
408
409 if (list_empty(&ep->queue))
410 request = 1;
411
412 list_add_tail(&req->queue, &ep->queue);
413
414 req->req.actual = 0;
415 req->req.status = -EINPROGRESS;
416
417 if (!ep->epnum) /* ep0 */
418 fotg210_ep0_queue(ep, req);
419 else if (request && !ep->stall)
420 fotg210_enable_fifo_int(ep);
421
422 spin_unlock_irqrestore(&ep->fotg210->lock, flags);
423
424 return 0;
425}
426
427static int fotg210_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
428{
429 struct fotg210_ep *ep;
430 struct fotg210_request *req;
431 unsigned long flags;
432
433 ep = container_of(_ep, struct fotg210_ep, ep);
434 req = container_of(_req, struct fotg210_request, req);
435
436 spin_lock_irqsave(&ep->fotg210->lock, flags);
437 if (!list_empty(&ep->queue))
438 fotg210_done(ep, req, -ECONNRESET);
439 spin_unlock_irqrestore(&ep->fotg210->lock, flags);
440
441 return 0;
442}
443
444static void fotg210_set_epnstall(struct fotg210_ep *ep)
445{
446 struct fotg210_udc *fotg210 = ep->fotg210;
447 u32 value;
448 void __iomem *reg;
449
450 /* check if IN FIFO is empty before stall */
451 if (ep->dir_in) {
452 do {
453 value = ioread32(fotg210->reg + FOTG210_DCFESR);
454 } while (!(value & DCFESR_FIFO_EMPTY(ep->epnum - 1)));
455 }
456
457 reg = (ep->dir_in) ?
458 fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
459 fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
460 value = ioread32(reg);
461 value |= INOUTEPMPSR_STL_EP;
462 iowrite32(value, reg);
463}
464
465static void fotg210_clear_epnstall(struct fotg210_ep *ep)
466{
467 struct fotg210_udc *fotg210 = ep->fotg210;
468 u32 value;
469 void __iomem *reg;
470
471 reg = (ep->dir_in) ?
472 fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
473 fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
474 value = ioread32(reg);
475 value &= ~INOUTEPMPSR_STL_EP;
476 iowrite32(value, reg);
477}
478
479static int fotg210_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
480{
481 struct fotg210_ep *ep;
482 struct fotg210_udc *fotg210;
483 unsigned long flags;
484 int ret = 0;
485
486 ep = container_of(_ep, struct fotg210_ep, ep);
487
488 fotg210 = ep->fotg210;
489
490 spin_lock_irqsave(&ep->fotg210->lock, flags);
491
492 if (value) {
493 fotg210_set_epnstall(ep);
494 ep->stall = 1;
495 if (wedge)
496 ep->wedged = 1;
497 } else {
498 fotg210_reset_tseq(fotg210, ep->epnum);
499 fotg210_clear_epnstall(ep);
500 ep->stall = 0;
501 ep->wedged = 0;
502 if (!list_empty(&ep->queue))
503 fotg210_enable_fifo_int(ep);
504 }
505
506 spin_unlock_irqrestore(&ep->fotg210->lock, flags);
507 return ret;
508}
509
510static int fotg210_ep_set_halt(struct usb_ep *_ep, int value)
511{
512 return fotg210_set_halt_and_wedge(_ep, value, 0);
513}
514
515static int fotg210_ep_set_wedge(struct usb_ep *_ep)
516{
517 return fotg210_set_halt_and_wedge(_ep, 1, 1);
518}
519
520static void fotg210_ep_fifo_flush(struct usb_ep *_ep)
521{
522}
523
977ac789 524static const struct usb_ep_ops fotg210_ep_ops = {
b84a8dee
YHC
525 .enable = fotg210_ep_enable,
526 .disable = fotg210_ep_disable,
527
528 .alloc_request = fotg210_ep_alloc_request,
529 .free_request = fotg210_ep_free_request,
530
531 .queue = fotg210_ep_queue,
532 .dequeue = fotg210_ep_dequeue,
533
534 .set_halt = fotg210_ep_set_halt,
535 .fifo_flush = fotg210_ep_fifo_flush,
536 .set_wedge = fotg210_ep_set_wedge,
537};
538
539static void fotg210_clear_tx0byte(struct fotg210_udc *fotg210)
540{
541 u32 value = ioread32(fotg210->reg + FOTG210_TX0BYTE);
542
543 value &= ~(TX0BYTE_EP1 | TX0BYTE_EP2 | TX0BYTE_EP3
544 | TX0BYTE_EP4);
545 iowrite32(value, fotg210->reg + FOTG210_TX0BYTE);
546}
547
548static void fotg210_clear_rx0byte(struct fotg210_udc *fotg210)
549{
550 u32 value = ioread32(fotg210->reg + FOTG210_RX0BYTE);
551
552 value &= ~(RX0BYTE_EP1 | RX0BYTE_EP2 | RX0BYTE_EP3
553 | RX0BYTE_EP4);
554 iowrite32(value, fotg210->reg + FOTG210_RX0BYTE);
555}
556
557/* read 8-byte setup packet only */
558static void fotg210_rdsetupp(struct fotg210_udc *fotg210,
559 u8 *buffer)
560{
561 int i = 0;
562 u8 *tmp = buffer;
563 u32 data;
564 u32 length = 8;
565
566 iowrite32(DMATFNR_ACC_CXF, fotg210->reg + FOTG210_DMATFNR);
567
568 for (i = (length >> 2); i > 0; i--) {
569 data = ioread32(fotg210->reg + FOTG210_CXPORT);
570 *tmp = data & 0xFF;
571 *(tmp + 1) = (data >> 8) & 0xFF;
572 *(tmp + 2) = (data >> 16) & 0xFF;
573 *(tmp + 3) = (data >> 24) & 0xFF;
574 tmp = tmp + 4;
575 }
576
577 switch (length % 4) {
578 case 1:
579 data = ioread32(fotg210->reg + FOTG210_CXPORT);
580 *tmp = data & 0xFF;
581 break;
582 case 2:
583 data = ioread32(fotg210->reg + FOTG210_CXPORT);
584 *tmp = data & 0xFF;
585 *(tmp + 1) = (data >> 8) & 0xFF;
586 break;
587 case 3:
588 data = ioread32(fotg210->reg + FOTG210_CXPORT);
589 *tmp = data & 0xFF;
590 *(tmp + 1) = (data >> 8) & 0xFF;
591 *(tmp + 2) = (data >> 16) & 0xFF;
592 break;
593 default:
594 break;
595 }
596
597 iowrite32(DMATFNR_DISDMA, fotg210->reg + FOTG210_DMATFNR);
598}
599
600static void fotg210_set_configuration(struct fotg210_udc *fotg210)
601{
602 u32 value = ioread32(fotg210->reg + FOTG210_DAR);
603
604 value |= DAR_AFT_CONF;
605 iowrite32(value, fotg210->reg + FOTG210_DAR);
606}
607
608static void fotg210_set_dev_addr(struct fotg210_udc *fotg210, u32 addr)
609{
610 u32 value = ioread32(fotg210->reg + FOTG210_DAR);
611
612 value |= (addr & 0x7F);
613 iowrite32(value, fotg210->reg + FOTG210_DAR);
614}
615
616static void fotg210_set_cxstall(struct fotg210_udc *fotg210)
617{
618 u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
619
620 value |= DCFESR_CX_STL;
621 iowrite32(value, fotg210->reg + FOTG210_DCFESR);
622}
623
624static void fotg210_request_error(struct fotg210_udc *fotg210)
625{
626 fotg210_set_cxstall(fotg210);
627 pr_err("request error!!\n");
628}
629
630static void fotg210_set_address(struct fotg210_udc *fotg210,
631 struct usb_ctrlrequest *ctrl)
632{
633 if (ctrl->wValue >= 0x0100) {
634 fotg210_request_error(fotg210);
635 } else {
636 fotg210_set_dev_addr(fotg210, ctrl->wValue);
637 fotg210_set_cxdone(fotg210);
638 }
639}
640
641static void fotg210_set_feature(struct fotg210_udc *fotg210,
642 struct usb_ctrlrequest *ctrl)
643{
644 switch (ctrl->bRequestType & USB_RECIP_MASK) {
645 case USB_RECIP_DEVICE:
646 fotg210_set_cxdone(fotg210);
647 break;
648 case USB_RECIP_INTERFACE:
649 fotg210_set_cxdone(fotg210);
650 break;
651 case USB_RECIP_ENDPOINT: {
652 u8 epnum;
653 epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
654 if (epnum)
655 fotg210_set_epnstall(fotg210->ep[epnum]);
656 else
657 fotg210_set_cxstall(fotg210);
658 fotg210_set_cxdone(fotg210);
659 }
660 break;
661 default:
662 fotg210_request_error(fotg210);
663 break;
664 }
665}
666
667static void fotg210_clear_feature(struct fotg210_udc *fotg210,
668 struct usb_ctrlrequest *ctrl)
669{
670 struct fotg210_ep *ep =
671 fotg210->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
672
673 switch (ctrl->bRequestType & USB_RECIP_MASK) {
674 case USB_RECIP_DEVICE:
675 fotg210_set_cxdone(fotg210);
676 break;
677 case USB_RECIP_INTERFACE:
678 fotg210_set_cxdone(fotg210);
679 break;
680 case USB_RECIP_ENDPOINT:
681 if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
682 if (ep->wedged) {
683 fotg210_set_cxdone(fotg210);
684 break;
685 }
686 if (ep->stall)
687 fotg210_set_halt_and_wedge(&ep->ep, 0, 0);
688 }
689 fotg210_set_cxdone(fotg210);
690 break;
691 default:
692 fotg210_request_error(fotg210);
693 break;
694 }
695}
696
697static int fotg210_is_epnstall(struct fotg210_ep *ep)
698{
699 struct fotg210_udc *fotg210 = ep->fotg210;
700 u32 value;
701 void __iomem *reg;
702
703 reg = (ep->dir_in) ?
704 fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
705 fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
706 value = ioread32(reg);
707 return value & INOUTEPMPSR_STL_EP ? 1 : 0;
708}
709
710static void fotg210_get_status(struct fotg210_udc *fotg210,
711 struct usb_ctrlrequest *ctrl)
712{
713 u8 epnum;
714
715 switch (ctrl->bRequestType & USB_RECIP_MASK) {
716 case USB_RECIP_DEVICE:
717 fotg210->ep0_data = 1 << USB_DEVICE_SELF_POWERED;
718 break;
719 case USB_RECIP_INTERFACE:
720 fotg210->ep0_data = 0;
721 break;
722 case USB_RECIP_ENDPOINT:
723 epnum = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
724 if (epnum)
725 fotg210->ep0_data =
726 fotg210_is_epnstall(fotg210->ep[epnum])
727 << USB_ENDPOINT_HALT;
728 else
729 fotg210_request_error(fotg210);
730 break;
731
732 default:
733 fotg210_request_error(fotg210);
734 return; /* exit */
735 }
736
737 fotg210->ep0_req->buf = &fotg210->ep0_data;
738 fotg210->ep0_req->length = 2;
739
740 spin_unlock(&fotg210->lock);
2337a77c 741 fotg210_ep_queue(fotg210->gadget.ep0, fotg210->ep0_req, GFP_ATOMIC);
b84a8dee
YHC
742 spin_lock(&fotg210->lock);
743}
744
745static int fotg210_setup_packet(struct fotg210_udc *fotg210,
746 struct usb_ctrlrequest *ctrl)
747{
748 u8 *p = (u8 *)ctrl;
749 u8 ret = 0;
750
751 fotg210_rdsetupp(fotg210, p);
752
753 fotg210->ep[0]->dir_in = ctrl->bRequestType & USB_DIR_IN;
754
755 if (fotg210->gadget.speed == USB_SPEED_UNKNOWN) {
756 u32 value = ioread32(fotg210->reg + FOTG210_DMCR);
757 fotg210->gadget.speed = value & DMCR_HS_EN ?
758 USB_SPEED_HIGH : USB_SPEED_FULL;
759 }
760
761 /* check request */
762 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
763 switch (ctrl->bRequest) {
764 case USB_REQ_GET_STATUS:
765 fotg210_get_status(fotg210, ctrl);
766 break;
767 case USB_REQ_CLEAR_FEATURE:
768 fotg210_clear_feature(fotg210, ctrl);
769 break;
770 case USB_REQ_SET_FEATURE:
771 fotg210_set_feature(fotg210, ctrl);
772 break;
773 case USB_REQ_SET_ADDRESS:
774 fotg210_set_address(fotg210, ctrl);
775 break;
776 case USB_REQ_SET_CONFIGURATION:
777 fotg210_set_configuration(fotg210);
778 ret = 1;
779 break;
780 default:
781 ret = 1;
782 break;
783 }
784 } else {
785 ret = 1;
786 }
787
788 return ret;
789}
790
791static void fotg210_ep0out(struct fotg210_udc *fotg210)
792{
793 struct fotg210_ep *ep = fotg210->ep[0];
794
795 if (!list_empty(&ep->queue) && !ep->dir_in) {
796 struct fotg210_request *req;
797
798 req = list_first_entry(&ep->queue,
799 struct fotg210_request, queue);
800
801 if (req->req.length)
802 fotg210_start_dma(ep, req);
803
804 if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
805 fotg210_done(ep, req, 0);
806 } else {
807 pr_err("%s : empty queue\n", __func__);
808 }
809}
810
811static void fotg210_ep0in(struct fotg210_udc *fotg210)
812{
813 struct fotg210_ep *ep = fotg210->ep[0];
814
815 if ((!list_empty(&ep->queue)) && (ep->dir_in)) {
816 struct fotg210_request *req;
817
818 req = list_entry(ep->queue.next,
819 struct fotg210_request, queue);
820
821 if (req->req.length)
822 fotg210_start_dma(ep, req);
823
824 if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
825 fotg210_done(ep, req, 0);
826 } else {
827 fotg210_set_cxdone(fotg210);
828 }
829}
830
831static void fotg210_clear_comabt_int(struct fotg210_udc *fotg210)
832{
833 u32 value = ioread32(fotg210->reg + FOTG210_DISGR0);
834
835 value &= ~DISGR0_CX_COMABT_INT;
836 iowrite32(value, fotg210->reg + FOTG210_DISGR0);
837}
838
839static void fotg210_in_fifo_handler(struct fotg210_ep *ep)
840{
841 struct fotg210_request *req = list_entry(ep->queue.next,
842 struct fotg210_request, queue);
843
844 if (req->req.length)
845 fotg210_start_dma(ep, req);
846 fotg210_done(ep, req, 0);
847}
848
849static void fotg210_out_fifo_handler(struct fotg210_ep *ep)
850{
851 struct fotg210_request *req = list_entry(ep->queue.next,
852 struct fotg210_request, queue);
853
854 fotg210_start_dma(ep, req);
855
856 /* finish out transfer */
857 if (req->req.length == req->req.actual ||
858 req->req.actual < ep->ep.maxpacket)
859 fotg210_done(ep, req, 0);
860}
861
862static irqreturn_t fotg210_irq(int irq, void *_fotg210)
863{
864 struct fotg210_udc *fotg210 = _fotg210;
865 u32 int_grp = ioread32(fotg210->reg + FOTG210_DIGR);
866 u32 int_msk = ioread32(fotg210->reg + FOTG210_DMIGR);
867
868 int_grp &= ~int_msk;
869
870 spin_lock(&fotg210->lock);
871
872 if (int_grp & DIGR_INT_G2) {
873 void __iomem *reg = fotg210->reg + FOTG210_DISGR2;
874 u32 int_grp2 = ioread32(reg);
875 u32 int_msk2 = ioread32(fotg210->reg + FOTG210_DMISGR2);
876 u32 value;
877
878 int_grp2 &= ~int_msk2;
879
880 if (int_grp2 & DISGR2_USBRST_INT) {
881 value = ioread32(reg);
882 value &= ~DISGR2_USBRST_INT;
883 iowrite32(value, reg);
884 pr_info("fotg210 udc reset\n");
885 }
886 if (int_grp2 & DISGR2_SUSP_INT) {
887 value = ioread32(reg);
888 value &= ~DISGR2_SUSP_INT;
889 iowrite32(value, reg);
890 pr_info("fotg210 udc suspend\n");
891 }
892 if (int_grp2 & DISGR2_RESM_INT) {
893 value = ioread32(reg);
894 value &= ~DISGR2_RESM_INT;
895 iowrite32(value, reg);
896 pr_info("fotg210 udc resume\n");
897 }
898 if (int_grp2 & DISGR2_ISO_SEQ_ERR_INT) {
899 value = ioread32(reg);
900 value &= ~DISGR2_ISO_SEQ_ERR_INT;
901 iowrite32(value, reg);
902 pr_info("fotg210 iso sequence error\n");
903 }
904 if (int_grp2 & DISGR2_ISO_SEQ_ABORT_INT) {
905 value = ioread32(reg);
906 value &= ~DISGR2_ISO_SEQ_ABORT_INT;
907 iowrite32(value, reg);
908 pr_info("fotg210 iso sequence abort\n");
909 }
910 if (int_grp2 & DISGR2_TX0BYTE_INT) {
911 fotg210_clear_tx0byte(fotg210);
912 value = ioread32(reg);
913 value &= ~DISGR2_TX0BYTE_INT;
914 iowrite32(value, reg);
915 pr_info("fotg210 transferred 0 byte\n");
916 }
917 if (int_grp2 & DISGR2_RX0BYTE_INT) {
918 fotg210_clear_rx0byte(fotg210);
919 value = ioread32(reg);
920 value &= ~DISGR2_RX0BYTE_INT;
921 iowrite32(value, reg);
922 pr_info("fotg210 received 0 byte\n");
923 }
924 if (int_grp2 & DISGR2_DMA_ERROR) {
925 value = ioread32(reg);
926 value &= ~DISGR2_DMA_ERROR;
927 iowrite32(value, reg);
928 }
929 }
930
931 if (int_grp & DIGR_INT_G0) {
932 void __iomem *reg = fotg210->reg + FOTG210_DISGR0;
933 u32 int_grp0 = ioread32(reg);
934 u32 int_msk0 = ioread32(fotg210->reg + FOTG210_DMISGR0);
935 struct usb_ctrlrequest ctrl;
936
937 int_grp0 &= ~int_msk0;
938
939 /* the highest priority in this source register */
940 if (int_grp0 & DISGR0_CX_COMABT_INT) {
941 fotg210_clear_comabt_int(fotg210);
942 pr_info("fotg210 CX command abort\n");
943 }
944
945 if (int_grp0 & DISGR0_CX_SETUP_INT) {
946 if (fotg210_setup_packet(fotg210, &ctrl)) {
947 spin_unlock(&fotg210->lock);
948 if (fotg210->driver->setup(&fotg210->gadget,
949 &ctrl) < 0)
950 fotg210_set_cxstall(fotg210);
951 spin_lock(&fotg210->lock);
952 }
953 }
954 if (int_grp0 & DISGR0_CX_COMEND_INT)
955 pr_info("fotg210 cmd end\n");
956
957 if (int_grp0 & DISGR0_CX_IN_INT)
958 fotg210_ep0in(fotg210);
959
960 if (int_grp0 & DISGR0_CX_OUT_INT)
961 fotg210_ep0out(fotg210);
962
963 if (int_grp0 & DISGR0_CX_COMFAIL_INT) {
964 fotg210_set_cxstall(fotg210);
965 pr_info("fotg210 ep0 fail\n");
966 }
967 }
968
969 if (int_grp & DIGR_INT_G1) {
970 void __iomem *reg = fotg210->reg + FOTG210_DISGR1;
971 u32 int_grp1 = ioread32(reg);
972 u32 int_msk1 = ioread32(fotg210->reg + FOTG210_DMISGR1);
973 int fifo;
974
975 int_grp1 &= ~int_msk1;
976
977 for (fifo = 0; fifo < FOTG210_MAX_FIFO_NUM; fifo++) {
978 if (int_grp1 & DISGR1_IN_INT(fifo))
979 fotg210_in_fifo_handler(fotg210->ep[fifo + 1]);
980
981 if ((int_grp1 & DISGR1_OUT_INT(fifo)) ||
982 (int_grp1 & DISGR1_SPK_INT(fifo)))
983 fotg210_out_fifo_handler(fotg210->ep[fifo + 1]);
984 }
985 }
986
987 spin_unlock(&fotg210->lock);
988
989 return IRQ_HANDLED;
990}
991
992static void fotg210_disable_unplug(struct fotg210_udc *fotg210)
993{
994 u32 reg = ioread32(fotg210->reg + FOTG210_PHYTMSR);
995
996 reg &= ~PHYTMSR_UNPLUG;
997 iowrite32(reg, fotg210->reg + FOTG210_PHYTMSR);
998}
999
1000static int fotg210_udc_start(struct usb_gadget *g,
1001 struct usb_gadget_driver *driver)
1002{
1003 struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
1004 u32 value;
1005
1006 /* hook up the driver */
1007 driver->driver.bus = NULL;
1008 fotg210->driver = driver;
1009
1010 /* enable device global interrupt */
1011 value = ioread32(fotg210->reg + FOTG210_DMCR);
1012 value |= DMCR_GLINT_EN;
1013 iowrite32(value, fotg210->reg + FOTG210_DMCR);
1014
1015 return 0;
1016}
1017
1018static void fotg210_init(struct fotg210_udc *fotg210)
1019{
1020 u32 value;
1021
1022 /* disable global interrupt and set int polarity to active high */
1023 iowrite32(GMIR_MHC_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
1024 fotg210->reg + FOTG210_GMIR);
1025
1026 /* disable device global interrupt */
1027 value = ioread32(fotg210->reg + FOTG210_DMCR);
1028 value &= ~DMCR_GLINT_EN;
1029 iowrite32(value, fotg210->reg + FOTG210_DMCR);
1030
1031 /* disable all fifo interrupt */
1032 iowrite32(~(u32)0, fotg210->reg + FOTG210_DMISGR1);
1033
1034 /* disable cmd end */
1035 value = ioread32(fotg210->reg + FOTG210_DMISGR0);
1036 value |= DMISGR0_MCX_COMEND;
1037 iowrite32(value, fotg210->reg + FOTG210_DMISGR0);
1038}
1039
22835b80 1040static int fotg210_udc_stop(struct usb_gadget *g)
b84a8dee
YHC
1041{
1042 struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
1043 unsigned long flags;
1044
1045 spin_lock_irqsave(&fotg210->lock, flags);
1046
1047 fotg210_init(fotg210);
1048 fotg210->driver = NULL;
1049
1050 spin_unlock_irqrestore(&fotg210->lock, flags);
1051
1052 return 0;
1053}
1054
bee56235 1055static const struct usb_gadget_ops fotg210_gadget_ops = {
b84a8dee
YHC
1056 .udc_start = fotg210_udc_start,
1057 .udc_stop = fotg210_udc_stop,
1058};
1059
88ae7423 1060static int fotg210_udc_remove(struct platform_device *pdev)
b84a8dee 1061{
3cb40a59 1062 struct fotg210_udc *fotg210 = platform_get_drvdata(pdev);
c37bd528 1063 int i;
b84a8dee
YHC
1064
1065 usb_del_gadget_udc(&fotg210->gadget);
1066 iounmap(fotg210->reg);
1067 free_irq(platform_get_irq(pdev, 0), fotg210);
1068
1069 fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
c37bd528
AV
1070 for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
1071 kfree(fotg210->ep[i]);
b84a8dee
YHC
1072 kfree(fotg210);
1073
1074 return 0;
1075}
1076
88ae7423 1077static int fotg210_udc_probe(struct platform_device *pdev)
b84a8dee
YHC
1078{
1079 struct resource *res, *ires;
1080 struct fotg210_udc *fotg210 = NULL;
1081 struct fotg210_ep *_ep[FOTG210_MAX_NUM_EP];
1082 int ret = 0;
1083 int i;
1084
1085 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1086 if (!res) {
1087 pr_err("platform_get_resource error.\n");
1088 return -ENODEV;
1089 }
1090
1091 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1092 if (!ires) {
1093 pr_err("platform_get_resource IORESOURCE_IRQ error.\n");
1094 return -ENODEV;
1095 }
1096
1097 ret = -ENOMEM;
1098
1099 /* initialize udc */
1100 fotg210 = kzalloc(sizeof(struct fotg210_udc), GFP_KERNEL);
0351c329 1101 if (fotg210 == NULL)
c37bd528 1102 goto err;
b84a8dee
YHC
1103
1104 for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
1105 _ep[i] = kzalloc(sizeof(struct fotg210_ep), GFP_KERNEL);
0351c329 1106 if (_ep[i] == NULL)
b84a8dee 1107 goto err_alloc;
b84a8dee
YHC
1108 fotg210->ep[i] = _ep[i];
1109 }
1110
1111 fotg210->reg = ioremap(res->start, resource_size(res));
1112 if (fotg210->reg == NULL) {
1113 pr_err("ioremap error.\n");
c37bd528 1114 goto err_alloc;
b84a8dee
YHC
1115 }
1116
1117 spin_lock_init(&fotg210->lock);
1118
3cb40a59 1119 platform_set_drvdata(pdev, fotg210);
b84a8dee
YHC
1120
1121 fotg210->gadget.ops = &fotg210_gadget_ops;
1122
1123 fotg210->gadget.max_speed = USB_SPEED_HIGH;
1124 fotg210->gadget.dev.parent = &pdev->dev;
1125 fotg210->gadget.dev.dma_mask = pdev->dev.dma_mask;
1126 fotg210->gadget.name = udc_name;
1127
1128 INIT_LIST_HEAD(&fotg210->gadget.ep_list);
1129
1130 for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
1131 struct fotg210_ep *ep = fotg210->ep[i];
1132
1133 if (i) {
1134 INIT_LIST_HEAD(&fotg210->ep[i]->ep.ep_list);
1135 list_add_tail(&fotg210->ep[i]->ep.ep_list,
1136 &fotg210->gadget.ep_list);
1137 }
1138 ep->fotg210 = fotg210;
1139 INIT_LIST_HEAD(&ep->queue);
1140 ep->ep.name = fotg210_ep_name[i];
1141 ep->ep.ops = &fotg210_ep_ops;
e117e742 1142 usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
8d29237a
RB
1143
1144 if (i == 0) {
1145 ep->ep.caps.type_control = true;
1146 } else {
1147 ep->ep.caps.type_iso = true;
1148 ep->ep.caps.type_bulk = true;
1149 ep->ep.caps.type_int = true;
1150 }
1151
1152 ep->ep.caps.dir_in = true;
1153 ep->ep.caps.dir_out = true;
b84a8dee 1154 }
e117e742 1155 usb_ep_set_maxpacket_limit(&fotg210->ep[0]->ep, 0x40);
b84a8dee
YHC
1156 fotg210->gadget.ep0 = &fotg210->ep[0]->ep;
1157 INIT_LIST_HEAD(&fotg210->gadget.ep0->ep_list);
1158
1159 fotg210->ep0_req = fotg210_ep_alloc_request(&fotg210->ep[0]->ep,
1160 GFP_KERNEL);
1161 if (fotg210->ep0_req == NULL)
c37bd528 1162 goto err_map;
b84a8dee
YHC
1163
1164 fotg210_init(fotg210);
1165
1166 fotg210_disable_unplug(fotg210);
1167
1168 ret = request_irq(ires->start, fotg210_irq, IRQF_SHARED,
1169 udc_name, fotg210);
1170 if (ret < 0) {
1171 pr_err("request_irq error (%d)\n", ret);
2184fe63 1172 goto err_req;
b84a8dee
YHC
1173 }
1174
1175 ret = usb_add_gadget_udc(&pdev->dev, &fotg210->gadget);
1176 if (ret)
1177 goto err_add_udc;
1178
1179 dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1180
1181 return 0;
1182
1183err_add_udc:
b84a8dee
YHC
1184 free_irq(ires->start, fotg210);
1185
1186err_req:
1187 fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
1188
1189err_map:
c37bd528 1190 iounmap(fotg210->reg);
b84a8dee
YHC
1191
1192err_alloc:
c37bd528
AV
1193 for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
1194 kfree(fotg210->ep[i]);
b84a8dee
YHC
1195 kfree(fotg210);
1196
c37bd528 1197err:
b84a8dee
YHC
1198 return ret;
1199}
1200
1201static struct platform_driver fotg210_driver = {
1202 .driver = {
1203 .name = (char *)udc_name,
b84a8dee
YHC
1204 },
1205 .probe = fotg210_udc_probe,
1206 .remove = fotg210_udc_remove,
1207};
1208
1209module_platform_driver(fotg210_driver);
1210
dfe29020 1211MODULE_AUTHOR("Yuan-Hsin Chen, Feng-Hsin Chiang <john453@faraday-tech.com>");
b84a8dee
YHC
1212MODULE_LICENSE("GPL");
1213MODULE_DESCRIPTION(DRIVER_DESC);