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