GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / usb / gadget / udc / fotg210-udc.c
1 // SPDX-License-Identifier: GPL-2.0
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>
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
24 static const char udc_name[] = "fotg210_udc";
25 static const char * const fotg210_ep_name[] = {
26         "ep0", "ep1", "ep2", "ep3", "ep4"};
27
28 static 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
39 static 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
50 static 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
58 static 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);
70         usb_gadget_giveback_request(&ep->ep, &req->req);
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
81 static 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
109 static 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
119 static 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
129 static 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
142 static 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
157 static 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
173 static 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
196 static 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
209 static 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
230 static 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
244 static 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
253 static 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
287 static void fotg210_disable_dma(struct fotg210_ep *ep)
288 {
289         iowrite32(DMATFNR_DISDMA, ep->fotg210->reg + FOTG210_DMATFNR);
290 }
291
292 static 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
307 dma_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
326 static void fotg210_start_dma(struct fotg210_ep *ep,
327                         struct fotg210_request *req)
328 {
329         dma_addr_t d;
330         u8 *buffer;
331         u32 length;
332
333         if (ep->epnum) {
334                 if (ep->dir_in) {
335                         buffer = req->req.buf;
336                         length = req->req.length;
337                 } else {
338                         buffer = req->req.buf + req->req.actual;
339                         length = ioread32(ep->fotg210->reg +
340                                         FOTG210_FIBCR(ep->epnum - 1)) & FIBCR_BCFX;
341                         if (length > req->req.length - req->req.actual)
342                                 length = req->req.length - req->req.actual;
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 - req->req.actual;
350         }
351
352         d = dma_map_single(NULL, buffer, length,
353                         ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
354
355         if (dma_mapping_error(NULL, d)) {
356                 pr_err("dma_mapping_error\n");
357                 return;
358         }
359
360         dma_sync_single_for_device(NULL, d, length,
361                                    ep->dir_in ? DMA_TO_DEVICE :
362                                         DMA_FROM_DEVICE);
363
364         fotg210_enable_dma(ep, d, length);
365
366         /* check if dma is done */
367         fotg210_wait_dma_done(ep);
368
369         fotg210_disable_dma(ep);
370
371         /* update actual transfer length */
372         req->req.actual += length;
373
374         dma_unmap_single(NULL, d, length, DMA_TO_DEVICE);
375 }
376
377 static void fotg210_ep0_queue(struct fotg210_ep *ep,
378                                 struct fotg210_request *req)
379 {
380         if (!req->req.length) {
381                 fotg210_done(ep, req, 0);
382                 return;
383         }
384         if (ep->dir_in) { /* if IN */
385                 fotg210_start_dma(ep, req);
386                 if (req->req.length == req->req.actual)
387                         fotg210_done(ep, req, 0);
388         } else { /* OUT */
389                 u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR0);
390
391                 value &= ~DMISGR0_MCX_OUT_INT;
392                 iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR0);
393         }
394 }
395
396 static int fotg210_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
397                                 gfp_t gfp_flags)
398 {
399         struct fotg210_ep *ep;
400         struct fotg210_request *req;
401         unsigned long flags;
402         int request = 0;
403
404         ep = container_of(_ep, struct fotg210_ep, ep);
405         req = container_of(_req, struct fotg210_request, req);
406
407         if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
408                 return -ESHUTDOWN;
409
410         spin_lock_irqsave(&ep->fotg210->lock, flags);
411
412         if (list_empty(&ep->queue))
413                 request = 1;
414
415         list_add_tail(&req->queue, &ep->queue);
416
417         req->req.actual = 0;
418         req->req.status = -EINPROGRESS;
419
420         if (!ep->epnum) /* ep0 */
421                 fotg210_ep0_queue(ep, req);
422         else if (request && !ep->stall)
423                 fotg210_enable_fifo_int(ep);
424
425         spin_unlock_irqrestore(&ep->fotg210->lock, flags);
426
427         return 0;
428 }
429
430 static int fotg210_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
431 {
432         struct fotg210_ep *ep;
433         struct fotg210_request *req;
434         unsigned long flags;
435
436         ep = container_of(_ep, struct fotg210_ep, ep);
437         req = container_of(_req, struct fotg210_request, req);
438
439         spin_lock_irqsave(&ep->fotg210->lock, flags);
440         if (!list_empty(&ep->queue))
441                 fotg210_done(ep, req, -ECONNRESET);
442         spin_unlock_irqrestore(&ep->fotg210->lock, flags);
443
444         return 0;
445 }
446
447 static void fotg210_set_epnstall(struct fotg210_ep *ep)
448 {
449         struct fotg210_udc *fotg210 = ep->fotg210;
450         u32 value;
451         void __iomem *reg;
452
453         /* check if IN FIFO is empty before stall */
454         if (ep->dir_in) {
455                 do {
456                         value = ioread32(fotg210->reg + FOTG210_DCFESR);
457                 } while (!(value & DCFESR_FIFO_EMPTY(ep->epnum - 1)));
458         }
459
460         reg = (ep->dir_in) ?
461                 fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
462                 fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
463         value = ioread32(reg);
464         value |= INOUTEPMPSR_STL_EP;
465         iowrite32(value, reg);
466 }
467
468 static void fotg210_clear_epnstall(struct fotg210_ep *ep)
469 {
470         struct fotg210_udc *fotg210 = ep->fotg210;
471         u32 value;
472         void __iomem *reg;
473
474         reg = (ep->dir_in) ?
475                 fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
476                 fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
477         value = ioread32(reg);
478         value &= ~INOUTEPMPSR_STL_EP;
479         iowrite32(value, reg);
480 }
481
482 static int fotg210_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
483 {
484         struct fotg210_ep *ep;
485         struct fotg210_udc *fotg210;
486         unsigned long flags;
487         int ret = 0;
488
489         ep = container_of(_ep, struct fotg210_ep, ep);
490
491         fotg210 = ep->fotg210;
492
493         spin_lock_irqsave(&ep->fotg210->lock, flags);
494
495         if (value) {
496                 fotg210_set_epnstall(ep);
497                 ep->stall = 1;
498                 if (wedge)
499                         ep->wedged = 1;
500         } else {
501                 fotg210_reset_tseq(fotg210, ep->epnum);
502                 fotg210_clear_epnstall(ep);
503                 ep->stall = 0;
504                 ep->wedged = 0;
505                 if (!list_empty(&ep->queue))
506                         fotg210_enable_fifo_int(ep);
507         }
508
509         spin_unlock_irqrestore(&ep->fotg210->lock, flags);
510         return ret;
511 }
512
513 static int fotg210_ep_set_halt(struct usb_ep *_ep, int value)
514 {
515         return fotg210_set_halt_and_wedge(_ep, value, 0);
516 }
517
518 static int fotg210_ep_set_wedge(struct usb_ep *_ep)
519 {
520         return fotg210_set_halt_and_wedge(_ep, 1, 1);
521 }
522
523 static void fotg210_ep_fifo_flush(struct usb_ep *_ep)
524 {
525 }
526
527 static const struct usb_ep_ops fotg210_ep_ops = {
528         .enable         = fotg210_ep_enable,
529         .disable        = fotg210_ep_disable,
530
531         .alloc_request  = fotg210_ep_alloc_request,
532         .free_request   = fotg210_ep_free_request,
533
534         .queue          = fotg210_ep_queue,
535         .dequeue        = fotg210_ep_dequeue,
536
537         .set_halt       = fotg210_ep_set_halt,
538         .fifo_flush     = fotg210_ep_fifo_flush,
539         .set_wedge      = fotg210_ep_set_wedge,
540 };
541
542 static void fotg210_clear_tx0byte(struct fotg210_udc *fotg210)
543 {
544         u32 value = ioread32(fotg210->reg + FOTG210_TX0BYTE);
545
546         value &= ~(TX0BYTE_EP1 | TX0BYTE_EP2 | TX0BYTE_EP3
547                    | TX0BYTE_EP4);
548         iowrite32(value, fotg210->reg + FOTG210_TX0BYTE);
549 }
550
551 static void fotg210_clear_rx0byte(struct fotg210_udc *fotg210)
552 {
553         u32 value = ioread32(fotg210->reg + FOTG210_RX0BYTE);
554
555         value &= ~(RX0BYTE_EP1 | RX0BYTE_EP2 | RX0BYTE_EP3
556                    | RX0BYTE_EP4);
557         iowrite32(value, fotg210->reg + FOTG210_RX0BYTE);
558 }
559
560 /* read 8-byte setup packet only */
561 static void fotg210_rdsetupp(struct fotg210_udc *fotg210,
562                    u8 *buffer)
563 {
564         int i = 0;
565         u8 *tmp = buffer;
566         u32 data;
567         u32 length = 8;
568
569         iowrite32(DMATFNR_ACC_CXF, fotg210->reg + FOTG210_DMATFNR);
570
571         for (i = (length >> 2); i > 0; i--) {
572                 data = ioread32(fotg210->reg + FOTG210_CXPORT);
573                 *tmp = data & 0xFF;
574                 *(tmp + 1) = (data >> 8) & 0xFF;
575                 *(tmp + 2) = (data >> 16) & 0xFF;
576                 *(tmp + 3) = (data >> 24) & 0xFF;
577                 tmp = tmp + 4;
578         }
579
580         switch (length % 4) {
581         case 1:
582                 data = ioread32(fotg210->reg + FOTG210_CXPORT);
583                 *tmp = data & 0xFF;
584                 break;
585         case 2:
586                 data = ioread32(fotg210->reg + FOTG210_CXPORT);
587                 *tmp = data & 0xFF;
588                 *(tmp + 1) = (data >> 8) & 0xFF;
589                 break;
590         case 3:
591                 data = ioread32(fotg210->reg + FOTG210_CXPORT);
592                 *tmp = data & 0xFF;
593                 *(tmp + 1) = (data >> 8) & 0xFF;
594                 *(tmp + 2) = (data >> 16) & 0xFF;
595                 break;
596         default:
597                 break;
598         }
599
600         iowrite32(DMATFNR_DISDMA, fotg210->reg + FOTG210_DMATFNR);
601 }
602
603 static void fotg210_set_configuration(struct fotg210_udc *fotg210)
604 {
605         u32 value = ioread32(fotg210->reg + FOTG210_DAR);
606
607         value |= DAR_AFT_CONF;
608         iowrite32(value, fotg210->reg + FOTG210_DAR);
609 }
610
611 static void fotg210_set_dev_addr(struct fotg210_udc *fotg210, u32 addr)
612 {
613         u32 value = ioread32(fotg210->reg + FOTG210_DAR);
614
615         value |= (addr & 0x7F);
616         iowrite32(value, fotg210->reg + FOTG210_DAR);
617 }
618
619 static void fotg210_set_cxstall(struct fotg210_udc *fotg210)
620 {
621         u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
622
623         value |= DCFESR_CX_STL;
624         iowrite32(value, fotg210->reg + FOTG210_DCFESR);
625 }
626
627 static void fotg210_request_error(struct fotg210_udc *fotg210)
628 {
629         fotg210_set_cxstall(fotg210);
630         pr_err("request error!!\n");
631 }
632
633 static void fotg210_set_address(struct fotg210_udc *fotg210,
634                                 struct usb_ctrlrequest *ctrl)
635 {
636         if (le16_to_cpu(ctrl->wValue) >= 0x0100) {
637                 fotg210_request_error(fotg210);
638         } else {
639                 fotg210_set_dev_addr(fotg210, le16_to_cpu(ctrl->wValue));
640                 fotg210_set_cxdone(fotg210);
641         }
642 }
643
644 static void fotg210_set_feature(struct fotg210_udc *fotg210,
645                                 struct usb_ctrlrequest *ctrl)
646 {
647         switch (ctrl->bRequestType & USB_RECIP_MASK) {
648         case USB_RECIP_DEVICE:
649                 fotg210_set_cxdone(fotg210);
650                 break;
651         case USB_RECIP_INTERFACE:
652                 fotg210_set_cxdone(fotg210);
653                 break;
654         case USB_RECIP_ENDPOINT: {
655                 u8 epnum;
656                 epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
657                 if (epnum)
658                         fotg210_set_epnstall(fotg210->ep[epnum]);
659                 else
660                         fotg210_set_cxstall(fotg210);
661                 fotg210_set_cxdone(fotg210);
662                 }
663                 break;
664         default:
665                 fotg210_request_error(fotg210);
666                 break;
667         }
668 }
669
670 static void fotg210_clear_feature(struct fotg210_udc *fotg210,
671                                 struct usb_ctrlrequest *ctrl)
672 {
673         struct fotg210_ep *ep =
674                 fotg210->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
675
676         switch (ctrl->bRequestType & USB_RECIP_MASK) {
677         case USB_RECIP_DEVICE:
678                 fotg210_set_cxdone(fotg210);
679                 break;
680         case USB_RECIP_INTERFACE:
681                 fotg210_set_cxdone(fotg210);
682                 break;
683         case USB_RECIP_ENDPOINT:
684                 if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
685                         if (ep->wedged) {
686                                 fotg210_set_cxdone(fotg210);
687                                 break;
688                         }
689                         if (ep->stall)
690                                 fotg210_set_halt_and_wedge(&ep->ep, 0, 0);
691                 }
692                 fotg210_set_cxdone(fotg210);
693                 break;
694         default:
695                 fotg210_request_error(fotg210);
696                 break;
697         }
698 }
699
700 static int fotg210_is_epnstall(struct fotg210_ep *ep)
701 {
702         struct fotg210_udc *fotg210 = ep->fotg210;
703         u32 value;
704         void __iomem *reg;
705
706         reg = (ep->dir_in) ?
707                 fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
708                 fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
709         value = ioread32(reg);
710         return value & INOUTEPMPSR_STL_EP ? 1 : 0;
711 }
712
713 static void fotg210_get_status(struct fotg210_udc *fotg210,
714                                 struct usb_ctrlrequest *ctrl)
715 {
716         u8 epnum;
717
718         switch (ctrl->bRequestType & USB_RECIP_MASK) {
719         case USB_RECIP_DEVICE:
720                 fotg210->ep0_data = cpu_to_le16(1 << USB_DEVICE_SELF_POWERED);
721                 break;
722         case USB_RECIP_INTERFACE:
723                 fotg210->ep0_data = cpu_to_le16(0);
724                 break;
725         case USB_RECIP_ENDPOINT:
726                 epnum = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
727                 if (epnum)
728                         fotg210->ep0_data =
729                                 cpu_to_le16(fotg210_is_epnstall(fotg210->ep[epnum])
730                                             << USB_ENDPOINT_HALT);
731                 else
732                         fotg210_request_error(fotg210);
733                 break;
734
735         default:
736                 fotg210_request_error(fotg210);
737                 return;         /* exit */
738         }
739
740         fotg210->ep0_req->buf = &fotg210->ep0_data;
741         fotg210->ep0_req->length = 2;
742
743         spin_unlock(&fotg210->lock);
744         fotg210_ep_queue(fotg210->gadget.ep0, fotg210->ep0_req, GFP_ATOMIC);
745         spin_lock(&fotg210->lock);
746 }
747
748 static int fotg210_setup_packet(struct fotg210_udc *fotg210,
749                                 struct usb_ctrlrequest *ctrl)
750 {
751         u8 *p = (u8 *)ctrl;
752         u8 ret = 0;
753
754         fotg210_rdsetupp(fotg210, p);
755
756         fotg210->ep[0]->dir_in = ctrl->bRequestType & USB_DIR_IN;
757
758         if (fotg210->gadget.speed == USB_SPEED_UNKNOWN) {
759                 u32 value = ioread32(fotg210->reg + FOTG210_DMCR);
760                 fotg210->gadget.speed = value & DMCR_HS_EN ?
761                                 USB_SPEED_HIGH : USB_SPEED_FULL;
762         }
763
764         /* check request */
765         if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
766                 switch (ctrl->bRequest) {
767                 case USB_REQ_GET_STATUS:
768                         fotg210_get_status(fotg210, ctrl);
769                         break;
770                 case USB_REQ_CLEAR_FEATURE:
771                         fotg210_clear_feature(fotg210, ctrl);
772                         break;
773                 case USB_REQ_SET_FEATURE:
774                         fotg210_set_feature(fotg210, ctrl);
775                         break;
776                 case USB_REQ_SET_ADDRESS:
777                         fotg210_set_address(fotg210, ctrl);
778                         break;
779                 case USB_REQ_SET_CONFIGURATION:
780                         fotg210_set_configuration(fotg210);
781                         ret = 1;
782                         break;
783                 default:
784                         ret = 1;
785                         break;
786                 }
787         } else {
788                 ret = 1;
789         }
790
791         return ret;
792 }
793
794 static void fotg210_ep0out(struct fotg210_udc *fotg210)
795 {
796         struct fotg210_ep *ep = fotg210->ep[0];
797
798         if (!list_empty(&ep->queue) && !ep->dir_in) {
799                 struct fotg210_request *req;
800
801                 req = list_first_entry(&ep->queue,
802                         struct fotg210_request, queue);
803
804                 if (req->req.length)
805                         fotg210_start_dma(ep, req);
806
807                 if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
808                         fotg210_done(ep, req, 0);
809         } else {
810                 pr_err("%s : empty queue\n", __func__);
811         }
812 }
813
814 static void fotg210_ep0in(struct fotg210_udc *fotg210)
815 {
816         struct fotg210_ep *ep = fotg210->ep[0];
817
818         if ((!list_empty(&ep->queue)) && (ep->dir_in)) {
819                 struct fotg210_request *req;
820
821                 req = list_entry(ep->queue.next,
822                                 struct fotg210_request, queue);
823
824                 if (req->req.length)
825                         fotg210_start_dma(ep, req);
826
827                 if (req->req.actual == req->req.length)
828                         fotg210_done(ep, req, 0);
829         } else {
830                 fotg210_set_cxdone(fotg210);
831         }
832 }
833
834 static void fotg210_clear_comabt_int(struct fotg210_udc *fotg210)
835 {
836         u32 value = ioread32(fotg210->reg + FOTG210_DISGR0);
837
838         value &= ~DISGR0_CX_COMABT_INT;
839         iowrite32(value, fotg210->reg + FOTG210_DISGR0);
840 }
841
842 static void fotg210_in_fifo_handler(struct fotg210_ep *ep)
843 {
844         struct fotg210_request *req = list_entry(ep->queue.next,
845                                         struct fotg210_request, queue);
846
847         if (req->req.length)
848                 fotg210_start_dma(ep, req);
849         fotg210_done(ep, req, 0);
850 }
851
852 static void fotg210_out_fifo_handler(struct fotg210_ep *ep)
853 {
854         struct fotg210_request *req = list_entry(ep->queue.next,
855                                                  struct fotg210_request, queue);
856         int disgr1 = ioread32(ep->fotg210->reg + FOTG210_DISGR1);
857
858         fotg210_start_dma(ep, req);
859
860         /* Complete the request when it's full or a short packet arrived.
861          * Like other drivers, short_not_ok isn't handled.
862          */
863
864         if (req->req.length == req->req.actual ||
865             (disgr1 & DISGR1_SPK_INT(ep->epnum - 1)))
866                 fotg210_done(ep, req, 0);
867 }
868
869 static irqreturn_t fotg210_irq(int irq, void *_fotg210)
870 {
871         struct fotg210_udc *fotg210 = _fotg210;
872         u32 int_grp = ioread32(fotg210->reg + FOTG210_DIGR);
873         u32 int_msk = ioread32(fotg210->reg + FOTG210_DMIGR);
874
875         int_grp &= ~int_msk;
876
877         spin_lock(&fotg210->lock);
878
879         if (int_grp & DIGR_INT_G2) {
880                 void __iomem *reg = fotg210->reg + FOTG210_DISGR2;
881                 u32 int_grp2 = ioread32(reg);
882                 u32 int_msk2 = ioread32(fotg210->reg + FOTG210_DMISGR2);
883                 u32 value;
884
885                 int_grp2 &= ~int_msk2;
886
887                 if (int_grp2 & DISGR2_USBRST_INT) {
888                         value = ioread32(reg);
889                         value &= ~DISGR2_USBRST_INT;
890                         iowrite32(value, reg);
891                         pr_info("fotg210 udc reset\n");
892                 }
893                 if (int_grp2 & DISGR2_SUSP_INT) {
894                         value = ioread32(reg);
895                         value &= ~DISGR2_SUSP_INT;
896                         iowrite32(value, reg);
897                         pr_info("fotg210 udc suspend\n");
898                 }
899                 if (int_grp2 & DISGR2_RESM_INT) {
900                         value = ioread32(reg);
901                         value &= ~DISGR2_RESM_INT;
902                         iowrite32(value, reg);
903                         pr_info("fotg210 udc resume\n");
904                 }
905                 if (int_grp2 & DISGR2_ISO_SEQ_ERR_INT) {
906                         value = ioread32(reg);
907                         value &= ~DISGR2_ISO_SEQ_ERR_INT;
908                         iowrite32(value, reg);
909                         pr_info("fotg210 iso sequence error\n");
910                 }
911                 if (int_grp2 & DISGR2_ISO_SEQ_ABORT_INT) {
912                         value = ioread32(reg);
913                         value &= ~DISGR2_ISO_SEQ_ABORT_INT;
914                         iowrite32(value, reg);
915                         pr_info("fotg210 iso sequence abort\n");
916                 }
917                 if (int_grp2 & DISGR2_TX0BYTE_INT) {
918                         fotg210_clear_tx0byte(fotg210);
919                         value = ioread32(reg);
920                         value &= ~DISGR2_TX0BYTE_INT;
921                         iowrite32(value, reg);
922                         pr_info("fotg210 transferred 0 byte\n");
923                 }
924                 if (int_grp2 & DISGR2_RX0BYTE_INT) {
925                         fotg210_clear_rx0byte(fotg210);
926                         value = ioread32(reg);
927                         value &= ~DISGR2_RX0BYTE_INT;
928                         iowrite32(value, reg);
929                         pr_info("fotg210 received 0 byte\n");
930                 }
931                 if (int_grp2 & DISGR2_DMA_ERROR) {
932                         value = ioread32(reg);
933                         value &= ~DISGR2_DMA_ERROR;
934                         iowrite32(value, reg);
935                 }
936         }
937
938         if (int_grp & DIGR_INT_G0) {
939                 void __iomem *reg = fotg210->reg + FOTG210_DISGR0;
940                 u32 int_grp0 = ioread32(reg);
941                 u32 int_msk0 = ioread32(fotg210->reg + FOTG210_DMISGR0);
942                 struct usb_ctrlrequest ctrl;
943
944                 int_grp0 &= ~int_msk0;
945
946                 /* the highest priority in this source register */
947                 if (int_grp0 & DISGR0_CX_COMABT_INT) {
948                         fotg210_clear_comabt_int(fotg210);
949                         pr_info("fotg210 CX command abort\n");
950                 }
951
952                 if (int_grp0 & DISGR0_CX_SETUP_INT) {
953                         if (fotg210_setup_packet(fotg210, &ctrl)) {
954                                 spin_unlock(&fotg210->lock);
955                                 if (fotg210->driver->setup(&fotg210->gadget,
956                                                            &ctrl) < 0)
957                                         fotg210_set_cxstall(fotg210);
958                                 spin_lock(&fotg210->lock);
959                         }
960                 }
961                 if (int_grp0 & DISGR0_CX_COMEND_INT)
962                         pr_info("fotg210 cmd end\n");
963
964                 if (int_grp0 & DISGR0_CX_IN_INT)
965                         fotg210_ep0in(fotg210);
966
967                 if (int_grp0 & DISGR0_CX_OUT_INT)
968                         fotg210_ep0out(fotg210);
969
970                 if (int_grp0 & DISGR0_CX_COMFAIL_INT) {
971                         fotg210_set_cxstall(fotg210);
972                         pr_info("fotg210 ep0 fail\n");
973                 }
974         }
975
976         if (int_grp & DIGR_INT_G1) {
977                 void __iomem *reg = fotg210->reg + FOTG210_DISGR1;
978                 u32 int_grp1 = ioread32(reg);
979                 u32 int_msk1 = ioread32(fotg210->reg + FOTG210_DMISGR1);
980                 int fifo;
981
982                 int_grp1 &= ~int_msk1;
983
984                 for (fifo = 0; fifo < FOTG210_MAX_FIFO_NUM; fifo++) {
985                         if (int_grp1 & DISGR1_IN_INT(fifo))
986                                 fotg210_in_fifo_handler(fotg210->ep[fifo + 1]);
987
988                         if ((int_grp1 & DISGR1_OUT_INT(fifo)) ||
989                             (int_grp1 & DISGR1_SPK_INT(fifo)))
990                                 fotg210_out_fifo_handler(fotg210->ep[fifo + 1]);
991                 }
992         }
993
994         spin_unlock(&fotg210->lock);
995
996         return IRQ_HANDLED;
997 }
998
999 static void fotg210_disable_unplug(struct fotg210_udc *fotg210)
1000 {
1001         u32 reg = ioread32(fotg210->reg + FOTG210_PHYTMSR);
1002
1003         reg &= ~PHYTMSR_UNPLUG;
1004         iowrite32(reg, fotg210->reg + FOTG210_PHYTMSR);
1005 }
1006
1007 static int fotg210_udc_start(struct usb_gadget *g,
1008                 struct usb_gadget_driver *driver)
1009 {
1010         struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
1011         u32 value;
1012
1013         /* hook up the driver */
1014         driver->driver.bus = NULL;
1015         fotg210->driver = driver;
1016
1017         /* enable device global interrupt */
1018         value = ioread32(fotg210->reg + FOTG210_DMCR);
1019         value |= DMCR_GLINT_EN;
1020         iowrite32(value, fotg210->reg + FOTG210_DMCR);
1021
1022         return 0;
1023 }
1024
1025 static void fotg210_init(struct fotg210_udc *fotg210)
1026 {
1027         u32 value;
1028
1029         /* disable global interrupt and set int polarity to active high */
1030         iowrite32(GMIR_MHC_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
1031                   fotg210->reg + FOTG210_GMIR);
1032
1033         /* disable device global interrupt */
1034         value = ioread32(fotg210->reg + FOTG210_DMCR);
1035         value &= ~DMCR_GLINT_EN;
1036         iowrite32(value, fotg210->reg + FOTG210_DMCR);
1037
1038         /* enable only grp2 irqs we handle */
1039         iowrite32(~(DISGR2_DMA_ERROR | DISGR2_RX0BYTE_INT | DISGR2_TX0BYTE_INT
1040                     | DISGR2_ISO_SEQ_ABORT_INT | DISGR2_ISO_SEQ_ERR_INT
1041                     | DISGR2_RESM_INT | DISGR2_SUSP_INT | DISGR2_USBRST_INT),
1042                   fotg210->reg + FOTG210_DMISGR2);
1043
1044         /* disable all fifo interrupt */
1045         iowrite32(~(u32)0, fotg210->reg + FOTG210_DMISGR1);
1046
1047         /* disable cmd end */
1048         value = ioread32(fotg210->reg + FOTG210_DMISGR0);
1049         value |= DMISGR0_MCX_COMEND;
1050         iowrite32(value, fotg210->reg + FOTG210_DMISGR0);
1051 }
1052
1053 static int fotg210_udc_stop(struct usb_gadget *g)
1054 {
1055         struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
1056         unsigned long   flags;
1057
1058         spin_lock_irqsave(&fotg210->lock, flags);
1059
1060         fotg210_init(fotg210);
1061         fotg210->driver = NULL;
1062
1063         spin_unlock_irqrestore(&fotg210->lock, flags);
1064
1065         return 0;
1066 }
1067
1068 static const struct usb_gadget_ops fotg210_gadget_ops = {
1069         .udc_start              = fotg210_udc_start,
1070         .udc_stop               = fotg210_udc_stop,
1071 };
1072
1073 static int fotg210_udc_remove(struct platform_device *pdev)
1074 {
1075         struct fotg210_udc *fotg210 = platform_get_drvdata(pdev);
1076         int i;
1077
1078         usb_del_gadget_udc(&fotg210->gadget);
1079         iounmap(fotg210->reg);
1080         free_irq(platform_get_irq(pdev, 0), fotg210);
1081
1082         fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
1083         for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
1084                 kfree(fotg210->ep[i]);
1085         kfree(fotg210);
1086
1087         return 0;
1088 }
1089
1090 static int fotg210_udc_probe(struct platform_device *pdev)
1091 {
1092         struct resource *res, *ires;
1093         struct fotg210_udc *fotg210 = NULL;
1094         struct fotg210_ep *_ep[FOTG210_MAX_NUM_EP];
1095         int ret = 0;
1096         int i;
1097
1098         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1099         if (!res) {
1100                 pr_err("platform_get_resource error.\n");
1101                 return -ENODEV;
1102         }
1103
1104         ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1105         if (!ires) {
1106                 pr_err("platform_get_resource IORESOURCE_IRQ error.\n");
1107                 return -ENODEV;
1108         }
1109
1110         ret = -ENOMEM;
1111
1112         /* initialize udc */
1113         fotg210 = kzalloc(sizeof(struct fotg210_udc), GFP_KERNEL);
1114         if (fotg210 == NULL)
1115                 goto err;
1116
1117         for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
1118                 _ep[i] = kzalloc(sizeof(struct fotg210_ep), GFP_KERNEL);
1119                 if (_ep[i] == NULL)
1120                         goto err_alloc;
1121                 fotg210->ep[i] = _ep[i];
1122         }
1123
1124         fotg210->reg = ioremap(res->start, resource_size(res));
1125         if (fotg210->reg == NULL) {
1126                 pr_err("ioremap error.\n");
1127                 goto err_alloc;
1128         }
1129
1130         spin_lock_init(&fotg210->lock);
1131
1132         platform_set_drvdata(pdev, fotg210);
1133
1134         fotg210->gadget.ops = &fotg210_gadget_ops;
1135
1136         fotg210->gadget.max_speed = USB_SPEED_HIGH;
1137         fotg210->gadget.dev.parent = &pdev->dev;
1138         fotg210->gadget.dev.dma_mask = pdev->dev.dma_mask;
1139         fotg210->gadget.name = udc_name;
1140
1141         INIT_LIST_HEAD(&fotg210->gadget.ep_list);
1142
1143         for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
1144                 struct fotg210_ep *ep = fotg210->ep[i];
1145
1146                 if (i) {
1147                         INIT_LIST_HEAD(&fotg210->ep[i]->ep.ep_list);
1148                         list_add_tail(&fotg210->ep[i]->ep.ep_list,
1149                                       &fotg210->gadget.ep_list);
1150                 }
1151                 ep->fotg210 = fotg210;
1152                 INIT_LIST_HEAD(&ep->queue);
1153                 ep->ep.name = fotg210_ep_name[i];
1154                 ep->ep.ops = &fotg210_ep_ops;
1155                 usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
1156
1157                 if (i == 0) {
1158                         ep->ep.caps.type_control = true;
1159                 } else {
1160                         ep->ep.caps.type_iso = true;
1161                         ep->ep.caps.type_bulk = true;
1162                         ep->ep.caps.type_int = true;
1163                 }
1164
1165                 ep->ep.caps.dir_in = true;
1166                 ep->ep.caps.dir_out = true;
1167         }
1168         usb_ep_set_maxpacket_limit(&fotg210->ep[0]->ep, 0x40);
1169         fotg210->gadget.ep0 = &fotg210->ep[0]->ep;
1170         INIT_LIST_HEAD(&fotg210->gadget.ep0->ep_list);
1171
1172         fotg210->ep0_req = fotg210_ep_alloc_request(&fotg210->ep[0]->ep,
1173                                 GFP_KERNEL);
1174         if (fotg210->ep0_req == NULL)
1175                 goto err_map;
1176
1177         fotg210_init(fotg210);
1178
1179         fotg210_disable_unplug(fotg210);
1180
1181         ret = request_irq(ires->start, fotg210_irq, IRQF_SHARED,
1182                           udc_name, fotg210);
1183         if (ret < 0) {
1184                 pr_err("request_irq error (%d)\n", ret);
1185                 goto err_req;
1186         }
1187
1188         ret = usb_add_gadget_udc(&pdev->dev, &fotg210->gadget);
1189         if (ret)
1190                 goto err_add_udc;
1191
1192         dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1193
1194         return 0;
1195
1196 err_add_udc:
1197         free_irq(ires->start, fotg210);
1198
1199 err_req:
1200         fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
1201
1202 err_map:
1203         iounmap(fotg210->reg);
1204
1205 err_alloc:
1206         for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
1207                 kfree(fotg210->ep[i]);
1208         kfree(fotg210);
1209
1210 err:
1211         return ret;
1212 }
1213
1214 static struct platform_driver fotg210_driver = {
1215         .driver         = {
1216                 .name = (char *)udc_name,
1217         },
1218         .probe          = fotg210_udc_probe,
1219         .remove         = fotg210_udc_remove,
1220 };
1221
1222 module_platform_driver(fotg210_driver);
1223
1224 MODULE_AUTHOR("Yuan-Hsin Chen, Feng-Hsin Chiang <john453@faraday-tech.com>");
1225 MODULE_LICENSE("GPL");
1226 MODULE_DESCRIPTION(DRIVER_DESC);