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