2 * R8A66597 UDC (USB gadget)
4 * Copyright (C) 2006-2009 Renesas Solutions Corp.
6 * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
25 #include "r8a66597-udc.h"
27 #define DRIVER_VERSION "2009-08-18"
29 static const char udc_name[] = "r8a66597_udc";
30 static const char *r8a66597_ep_name[] = {
31 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7",
35 static void init_controller(struct r8a66597 *r8a66597);
36 static void disable_controller(struct r8a66597 *r8a66597);
37 static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req);
38 static void irq_packet_write(struct r8a66597_ep *ep,
39 struct r8a66597_request *req);
40 static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
43 static void transfer_complete(struct r8a66597_ep *ep,
44 struct r8a66597_request *req, int status);
46 /*-------------------------------------------------------------------------*/
47 static inline u16 get_usb_speed(struct r8a66597 *r8a66597)
49 return r8a66597_read(r8a66597, DVSTCTR0) & RHST;
52 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
57 tmp = r8a66597_read(r8a66597, INTENB0);
58 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
60 r8a66597_bset(r8a66597, (1 << pipenum), reg);
61 r8a66597_write(r8a66597, tmp, INTENB0);
64 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
69 tmp = r8a66597_read(r8a66597, INTENB0);
70 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
72 r8a66597_bclr(r8a66597, (1 << pipenum), reg);
73 r8a66597_write(r8a66597, tmp, INTENB0);
76 static void r8a66597_usb_connect(struct r8a66597 *r8a66597)
78 r8a66597_bset(r8a66597, CTRE, INTENB0);
79 r8a66597_bset(r8a66597, BEMPE | BRDYE, INTENB0);
81 r8a66597_bset(r8a66597, DPRPU, SYSCFG0);
84 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597)
85 __releases(r8a66597->lock)
86 __acquires(r8a66597->lock)
88 r8a66597_bclr(r8a66597, CTRE, INTENB0);
89 r8a66597_bclr(r8a66597, BEMPE | BRDYE, INTENB0);
90 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
92 r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
93 spin_unlock(&r8a66597->lock);
94 r8a66597->driver->disconnect(&r8a66597->gadget);
95 spin_lock(&r8a66597->lock);
97 disable_controller(r8a66597);
98 init_controller(r8a66597);
99 r8a66597_bset(r8a66597, VBSE, INTENB0);
100 INIT_LIST_HEAD(&r8a66597->ep[0].queue);
103 static inline u16 control_reg_get_pid(struct r8a66597 *r8a66597, u16 pipenum)
106 unsigned long offset;
109 pid = r8a66597_read(r8a66597, DCPCTR) & PID;
110 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
111 offset = get_pipectr_addr(pipenum);
112 pid = r8a66597_read(r8a66597, offset) & PID;
114 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
119 static inline void control_reg_set_pid(struct r8a66597 *r8a66597, u16 pipenum,
122 unsigned long offset;
125 r8a66597_mdfy(r8a66597, pid, PID, DCPCTR);
126 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
127 offset = get_pipectr_addr(pipenum);
128 r8a66597_mdfy(r8a66597, pid, PID, offset);
130 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
133 static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum)
135 control_reg_set_pid(r8a66597, pipenum, PID_BUF);
138 static inline void pipe_stop(struct r8a66597 *r8a66597, u16 pipenum)
140 control_reg_set_pid(r8a66597, pipenum, PID_NAK);
143 static inline void pipe_stall(struct r8a66597 *r8a66597, u16 pipenum)
145 control_reg_set_pid(r8a66597, pipenum, PID_STALL);
148 static inline u16 control_reg_get(struct r8a66597 *r8a66597, u16 pipenum)
151 unsigned long offset;
154 ret = r8a66597_read(r8a66597, DCPCTR);
155 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
156 offset = get_pipectr_addr(pipenum);
157 ret = r8a66597_read(r8a66597, offset);
159 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
164 static inline void control_reg_sqclr(struct r8a66597 *r8a66597, u16 pipenum)
166 unsigned long offset;
168 pipe_stop(r8a66597, pipenum);
171 r8a66597_bset(r8a66597, SQCLR, DCPCTR);
172 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
173 offset = get_pipectr_addr(pipenum);
174 r8a66597_bset(r8a66597, SQCLR, offset);
176 printk(KERN_ERR "unexpect pipe num(%d)\n", pipenum);
179 static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum)
185 tmp = r8a66597_read(r8a66597, DCPCFG);
186 if ((tmp & R8A66597_CNTMD) != 0)
189 tmp = r8a66597_read(r8a66597, DCPMAXP);
193 r8a66597_write(r8a66597, pipenum, PIPESEL);
194 tmp = r8a66597_read(r8a66597, PIPECFG);
195 if ((tmp & R8A66597_CNTMD) != 0) {
196 tmp = r8a66597_read(r8a66597, PIPEBUF);
197 size = ((tmp >> 10) + 1) * 64;
199 tmp = r8a66597_read(r8a66597, PIPEMAXP);
207 static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
209 if (r8a66597->pdata->on_chip)
215 static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum)
217 struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
222 r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel);
226 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
229 static int pipe_buffer_setting(struct r8a66597 *r8a66597,
230 struct r8a66597_pipe_info *info)
232 u16 bufnum = 0, buf_bsize = 0;
238 r8a66597_write(r8a66597, info->pipe, PIPESEL);
241 pipecfg |= R8A66597_DIR;
242 pipecfg |= info->type;
243 pipecfg |= info->epnum;
244 switch (info->type) {
246 bufnum = 4 + (info->pipe - R8A66597_BASE_PIPENUM_INT);
250 /* isochronous pipes may be used as bulk pipes */
251 if (info->pipe >= R8A66597_BASE_PIPENUM_BULK)
252 bufnum = info->pipe - R8A66597_BASE_PIPENUM_BULK;
254 bufnum = info->pipe - R8A66597_BASE_PIPENUM_ISOC;
256 bufnum = R8A66597_BASE_BUFNUM + (bufnum * 16);
258 pipecfg |= R8A66597_DBLB;
260 pipecfg |= R8A66597_SHTNAK;
263 bufnum = R8A66597_BASE_BUFNUM +
264 (info->pipe - R8A66597_BASE_PIPENUM_ISOC) * 16;
269 if (buf_bsize && ((bufnum + 16) >= R8A66597_MAX_BUFNUM)) {
270 pr_err("r8a66597 pipe memory is insufficient\n");
274 r8a66597_write(r8a66597, pipecfg, PIPECFG);
275 r8a66597_write(r8a66597, (buf_bsize << 10) | (bufnum), PIPEBUF);
276 r8a66597_write(r8a66597, info->maxpacket, PIPEMAXP);
279 r8a66597_write(r8a66597, info->interval, PIPEPERI);
284 static void pipe_buffer_release(struct r8a66597 *r8a66597,
285 struct r8a66597_pipe_info *info)
290 if (is_bulk_pipe(info->pipe))
292 else if (is_interrupt_pipe(info->pipe))
293 r8a66597->interrupt--;
294 else if (is_isoc_pipe(info->pipe)) {
295 r8a66597->isochronous--;
296 if (info->type == R8A66597_BULK)
299 printk(KERN_ERR "ep_release: unexpect pipenum (%d)\n",
303 static void pipe_initialize(struct r8a66597_ep *ep)
305 struct r8a66597 *r8a66597 = ep->r8a66597;
307 r8a66597_mdfy(r8a66597, 0, CURPIPE, ep->fifosel);
309 r8a66597_write(r8a66597, ACLRM, ep->pipectr);
310 r8a66597_write(r8a66597, 0, ep->pipectr);
311 r8a66597_write(r8a66597, SQCLR, ep->pipectr);
313 r8a66597_mdfy(r8a66597, ep->pipenum, CURPIPE, ep->fifosel);
317 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
321 static void r8a66597_ep_setting(struct r8a66597 *r8a66597,
322 struct r8a66597_ep *ep,
323 const struct usb_endpoint_descriptor *desc,
324 u16 pipenum, int dma)
327 ep->fifoaddr = CFIFO;
328 ep->fifosel = CFIFOSEL;
329 ep->fifoctr = CFIFOCTR;
332 ep->pipectr = get_pipectr_addr(pipenum);
333 ep->pipenum = pipenum;
334 ep->ep.maxpacket = usb_endpoint_maxp(desc);
335 r8a66597->pipenum2ep[pipenum] = ep;
336 r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK]
338 INIT_LIST_HEAD(&ep->queue);
341 static void r8a66597_ep_release(struct r8a66597_ep *ep)
343 struct r8a66597 *r8a66597 = ep->r8a66597;
344 u16 pipenum = ep->pipenum;
356 static int alloc_pipe_config(struct r8a66597_ep *ep,
357 const struct usb_endpoint_descriptor *desc)
359 struct r8a66597 *r8a66597 = ep->r8a66597;
360 struct r8a66597_pipe_info info;
362 unsigned char *counter;
367 if (ep->pipenum) /* already allocated pipe */
370 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
371 case USB_ENDPOINT_XFER_BULK:
372 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) {
373 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
374 printk(KERN_ERR "bulk pipe is insufficient\n");
377 info.pipe = R8A66597_BASE_PIPENUM_ISOC
378 + r8a66597->isochronous;
379 counter = &r8a66597->isochronous;
382 info.pipe = R8A66597_BASE_PIPENUM_BULK + r8a66597->bulk;
383 counter = &r8a66597->bulk;
385 info.type = R8A66597_BULK;
388 case USB_ENDPOINT_XFER_INT:
389 if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) {
390 printk(KERN_ERR "interrupt pipe is insufficient\n");
393 info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt;
394 info.type = R8A66597_INT;
395 counter = &r8a66597->interrupt;
397 case USB_ENDPOINT_XFER_ISOC:
398 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
399 printk(KERN_ERR "isochronous pipe is insufficient\n");
402 info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous;
403 info.type = R8A66597_ISO;
404 counter = &r8a66597->isochronous;
407 printk(KERN_ERR "unexpect xfer type\n");
410 ep->type = info.type;
412 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
413 info.maxpacket = usb_endpoint_maxp(desc);
414 info.interval = desc->bInterval;
415 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
420 ret = pipe_buffer_setting(r8a66597, &info);
422 printk(KERN_ERR "pipe_buffer_setting fail\n");
427 if ((counter == &r8a66597->isochronous) && info.type == R8A66597_BULK)
430 r8a66597_ep_setting(r8a66597, ep, desc, info.pipe, dma);
436 static int free_pipe_config(struct r8a66597_ep *ep)
438 struct r8a66597 *r8a66597 = ep->r8a66597;
439 struct r8a66597_pipe_info info;
441 info.pipe = ep->pipenum;
442 info.type = ep->type;
443 pipe_buffer_release(r8a66597, &info);
444 r8a66597_ep_release(ep);
449 /*-------------------------------------------------------------------------*/
450 static void pipe_irq_enable(struct r8a66597 *r8a66597, u16 pipenum)
452 enable_irq_ready(r8a66597, pipenum);
453 enable_irq_nrdy(r8a66597, pipenum);
456 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
458 disable_irq_ready(r8a66597, pipenum);
459 disable_irq_nrdy(r8a66597, pipenum);
462 /* if complete is true, gadget driver complete function is not call */
463 static void control_end(struct r8a66597 *r8a66597, unsigned ccpl)
465 r8a66597->ep[0].internal_ccpl = ccpl;
466 pipe_start(r8a66597, 0);
467 r8a66597_bset(r8a66597, CCPL, DCPCTR);
470 static void start_ep0_write(struct r8a66597_ep *ep,
471 struct r8a66597_request *req)
473 struct r8a66597 *r8a66597 = ep->r8a66597;
475 pipe_change(r8a66597, ep->pipenum);
476 r8a66597_mdfy(r8a66597, ISEL, (ISEL | CURPIPE), CFIFOSEL);
477 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
478 if (req->req.length == 0) {
479 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
480 pipe_start(r8a66597, 0);
481 transfer_complete(ep, req, 0);
483 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
484 irq_ep0_write(ep, req);
488 static void start_packet_write(struct r8a66597_ep *ep,
489 struct r8a66597_request *req)
491 struct r8a66597 *r8a66597 = ep->r8a66597;
494 pipe_change(r8a66597, ep->pipenum);
495 disable_irq_empty(r8a66597, ep->pipenum);
496 pipe_start(r8a66597, ep->pipenum);
498 tmp = r8a66597_read(r8a66597, ep->fifoctr);
499 if (unlikely((tmp & FRDY) == 0))
500 pipe_irq_enable(r8a66597, ep->pipenum);
502 irq_packet_write(ep, req);
505 static void start_packet_read(struct r8a66597_ep *ep,
506 struct r8a66597_request *req)
508 struct r8a66597 *r8a66597 = ep->r8a66597;
509 u16 pipenum = ep->pipenum;
511 if (ep->pipenum == 0) {
512 r8a66597_mdfy(r8a66597, 0, (ISEL | CURPIPE), CFIFOSEL);
513 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
514 pipe_start(r8a66597, pipenum);
515 pipe_irq_enable(r8a66597, pipenum);
518 r8a66597_bset(r8a66597, TRCLR, ep->fifosel);
519 pipe_change(r8a66597, pipenum);
520 r8a66597_bset(r8a66597, TRENB, ep->fifosel);
521 r8a66597_write(r8a66597,
522 (req->req.length + ep->ep.maxpacket - 1)
526 pipe_start(r8a66597, pipenum); /* trigger once */
527 pipe_irq_enable(r8a66597, pipenum);
531 static void start_packet(struct r8a66597_ep *ep, struct r8a66597_request *req)
533 if (ep->desc->bEndpointAddress & USB_DIR_IN)
534 start_packet_write(ep, req);
536 start_packet_read(ep, req);
539 static void start_ep0(struct r8a66597_ep *ep, struct r8a66597_request *req)
543 ctsq = r8a66597_read(ep->r8a66597, INTSTS0) & CTSQ;
547 start_ep0_write(ep, req);
550 start_packet_read(ep, req);
554 control_end(ep->r8a66597, 0);
557 printk(KERN_ERR "start_ep0: unexpect ctsq(%x)\n", ctsq);
562 static void init_controller(struct r8a66597 *r8a66597)
564 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
565 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
566 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
568 if (r8a66597->pdata->on_chip) {
569 if (r8a66597->pdata->buswait)
570 r8a66597_write(r8a66597, r8a66597->pdata->buswait,
573 r8a66597_write(r8a66597, 0x0f, SYSCFG1);
574 r8a66597_bset(r8a66597, HSE, SYSCFG0);
576 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
577 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
578 r8a66597_bset(r8a66597, USBE, SYSCFG0);
580 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
582 r8a66597_bset(r8a66597, irq_sense, INTENB1);
583 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
586 r8a66597_bset(r8a66597, vif | endian, PINCFG);
587 r8a66597_bset(r8a66597, HSE, SYSCFG0); /* High spd */
588 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
591 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
592 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
593 r8a66597_bset(r8a66597, USBE, SYSCFG0);
595 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
599 r8a66597_bset(r8a66597, PLLC, SYSCFG0);
603 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
605 r8a66597_bset(r8a66597, irq_sense, INTENB1);
606 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
611 static void disable_controller(struct r8a66597 *r8a66597)
613 if (r8a66597->pdata->on_chip) {
614 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
615 r8a66597_bclr(r8a66597, UTST, TESTMODE);
617 /* disable interrupts */
618 r8a66597_write(r8a66597, 0, INTENB0);
619 r8a66597_write(r8a66597, 0, INTENB1);
620 r8a66597_write(r8a66597, 0, BRDYENB);
621 r8a66597_write(r8a66597, 0, BEMPENB);
622 r8a66597_write(r8a66597, 0, NRDYENB);
625 r8a66597_write(r8a66597, 0, BRDYSTS);
626 r8a66597_write(r8a66597, 0, NRDYSTS);
627 r8a66597_write(r8a66597, 0, BEMPSTS);
629 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
630 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
633 r8a66597_bclr(r8a66597, UTST, TESTMODE);
634 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
636 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
639 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
643 static void r8a66597_start_xclock(struct r8a66597 *r8a66597)
647 if (!r8a66597->pdata->on_chip) {
648 tmp = r8a66597_read(r8a66597, SYSCFG0);
650 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
654 static struct r8a66597_request *get_request_from_ep(struct r8a66597_ep *ep)
656 return list_entry(ep->queue.next, struct r8a66597_request, queue);
659 /*-------------------------------------------------------------------------*/
660 static void transfer_complete(struct r8a66597_ep *ep,
661 struct r8a66597_request *req, int status)
662 __releases(r8a66597->lock)
663 __acquires(r8a66597->lock)
667 if (unlikely(ep->pipenum == 0)) {
668 if (ep->internal_ccpl) {
669 ep->internal_ccpl = 0;
674 list_del_init(&req->queue);
675 if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
676 req->req.status = -ESHUTDOWN;
678 req->req.status = status;
680 if (!list_empty(&ep->queue))
683 spin_unlock(&ep->r8a66597->lock);
684 req->req.complete(&ep->ep, &req->req);
685 spin_lock(&ep->r8a66597->lock);
688 req = get_request_from_ep(ep);
690 start_packet(ep, req);
694 static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
701 u16 pipenum = ep->pipenum;
702 struct r8a66597 *r8a66597 = ep->r8a66597;
704 pipe_change(r8a66597, pipenum);
705 r8a66597_bset(r8a66597, ISEL, ep->fifosel);
709 tmp = r8a66597_read(r8a66597, ep->fifoctr);
711 printk(KERN_ERR "pipe0 is busy. maybe cpu i/o bus"
712 "conflict. please power off this controller.");
716 } while ((tmp & FRDY) == 0);
718 /* prepare parameters */
719 bufsize = get_buffer_size(r8a66597, pipenum);
720 buf = req->req.buf + req->req.actual;
721 size = min(bufsize, req->req.length - req->req.actual);
726 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
727 if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
728 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
731 /* update parameters */
732 req->req.actual += size;
734 /* check transfer finish */
735 if ((!req->req.zero && (req->req.actual == req->req.length))
736 || (size % ep->ep.maxpacket)
738 disable_irq_ready(r8a66597, pipenum);
739 disable_irq_empty(r8a66597, pipenum);
741 disable_irq_ready(r8a66597, pipenum);
742 enable_irq_empty(r8a66597, pipenum);
744 pipe_start(r8a66597, pipenum);
747 static void irq_packet_write(struct r8a66597_ep *ep,
748 struct r8a66597_request *req)
754 u16 pipenum = ep->pipenum;
755 struct r8a66597 *r8a66597 = ep->r8a66597;
757 pipe_change(r8a66597, pipenum);
758 tmp = r8a66597_read(r8a66597, ep->fifoctr);
759 if (unlikely((tmp & FRDY) == 0)) {
760 pipe_stop(r8a66597, pipenum);
761 pipe_irq_disable(r8a66597, pipenum);
762 printk(KERN_ERR "write fifo not ready. pipnum=%d\n", pipenum);
766 /* prepare parameters */
767 bufsize = get_buffer_size(r8a66597, pipenum);
768 buf = req->req.buf + req->req.actual;
769 size = min(bufsize, req->req.length - req->req.actual);
773 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
775 || ((size % ep->ep.maxpacket) != 0)
776 || ((bufsize != ep->ep.maxpacket)
777 && (bufsize > size)))
778 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
781 /* update parameters */
782 req->req.actual += size;
783 /* check transfer finish */
784 if ((!req->req.zero && (req->req.actual == req->req.length))
785 || (size % ep->ep.maxpacket)
787 disable_irq_ready(r8a66597, pipenum);
788 enable_irq_empty(r8a66597, pipenum);
790 disable_irq_empty(r8a66597, pipenum);
791 pipe_irq_enable(r8a66597, pipenum);
795 static void irq_packet_read(struct r8a66597_ep *ep,
796 struct r8a66597_request *req)
799 int rcv_len, bufsize, req_len;
802 u16 pipenum = ep->pipenum;
803 struct r8a66597 *r8a66597 = ep->r8a66597;
806 pipe_change(r8a66597, pipenum);
807 tmp = r8a66597_read(r8a66597, ep->fifoctr);
808 if (unlikely((tmp & FRDY) == 0)) {
809 req->req.status = -EPIPE;
810 pipe_stop(r8a66597, pipenum);
811 pipe_irq_disable(r8a66597, pipenum);
812 printk(KERN_ERR "read fifo not ready");
816 /* prepare parameters */
817 rcv_len = tmp & DTLN;
818 bufsize = get_buffer_size(r8a66597, pipenum);
820 buf = req->req.buf + req->req.actual;
821 req_len = req->req.length - req->req.actual;
822 if (rcv_len < bufsize)
823 size = min(rcv_len, req_len);
825 size = min(bufsize, req_len);
827 /* update parameters */
828 req->req.actual += size;
830 /* check transfer finish */
831 if ((!req->req.zero && (req->req.actual == req->req.length))
832 || (size % ep->ep.maxpacket)
834 pipe_stop(r8a66597, pipenum);
835 pipe_irq_disable(r8a66597, pipenum);
842 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
844 r8a66597_read_fifo(r8a66597, ep->fifoaddr, buf, size);
848 if ((ep->pipenum != 0) && finish)
849 transfer_complete(ep, req, 0);
852 static void irq_pipe_ready(struct r8a66597 *r8a66597, u16 status, u16 enb)
856 struct r8a66597_ep *ep;
857 struct r8a66597_request *req;
859 if ((status & BRDY0) && (enb & BRDY0)) {
860 r8a66597_write(r8a66597, ~BRDY0, BRDYSTS);
861 r8a66597_mdfy(r8a66597, 0, CURPIPE, CFIFOSEL);
863 ep = &r8a66597->ep[0];
864 req = get_request_from_ep(ep);
865 irq_packet_read(ep, req);
867 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
868 check = 1 << pipenum;
869 if ((status & check) && (enb & check)) {
870 r8a66597_write(r8a66597, ~check, BRDYSTS);
871 ep = r8a66597->pipenum2ep[pipenum];
872 req = get_request_from_ep(ep);
873 if (ep->desc->bEndpointAddress & USB_DIR_IN)
874 irq_packet_write(ep, req);
876 irq_packet_read(ep, req);
882 static void irq_pipe_empty(struct r8a66597 *r8a66597, u16 status, u16 enb)
887 struct r8a66597_ep *ep;
888 struct r8a66597_request *req;
890 if ((status & BEMP0) && (enb & BEMP0)) {
891 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
893 ep = &r8a66597->ep[0];
894 req = get_request_from_ep(ep);
895 irq_ep0_write(ep, req);
897 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
898 check = 1 << pipenum;
899 if ((status & check) && (enb & check)) {
900 r8a66597_write(r8a66597, ~check, BEMPSTS);
901 tmp = control_reg_get(r8a66597, pipenum);
902 if ((tmp & INBUFM) == 0) {
903 disable_irq_empty(r8a66597, pipenum);
904 pipe_irq_disable(r8a66597, pipenum);
905 pipe_stop(r8a66597, pipenum);
906 ep = r8a66597->pipenum2ep[pipenum];
907 req = get_request_from_ep(ep);
908 if (!list_empty(&ep->queue))
909 transfer_complete(ep, req, 0);
916 static void get_status(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
917 __releases(r8a66597->lock)
918 __acquires(r8a66597->lock)
920 struct r8a66597_ep *ep;
923 u16 w_index = le16_to_cpu(ctrl->wIndex);
925 switch (ctrl->bRequestType & USB_RECIP_MASK) {
926 case USB_RECIP_DEVICE:
927 status = 1 << USB_DEVICE_SELF_POWERED;
929 case USB_RECIP_INTERFACE:
932 case USB_RECIP_ENDPOINT:
933 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
934 pid = control_reg_get_pid(r8a66597, ep->pipenum);
935 if (pid == PID_STALL)
936 status = 1 << USB_ENDPOINT_HALT;
941 pipe_stall(r8a66597, 0);
945 r8a66597->ep0_data = cpu_to_le16(status);
946 r8a66597->ep0_req->buf = &r8a66597->ep0_data;
947 r8a66597->ep0_req->length = 2;
948 /* AV: what happens if we get called again before that gets through? */
949 spin_unlock(&r8a66597->lock);
950 r8a66597_queue(r8a66597->gadget.ep0, r8a66597->ep0_req, GFP_KERNEL);
951 spin_lock(&r8a66597->lock);
954 static void clear_feature(struct r8a66597 *r8a66597,
955 struct usb_ctrlrequest *ctrl)
957 switch (ctrl->bRequestType & USB_RECIP_MASK) {
958 case USB_RECIP_DEVICE:
959 control_end(r8a66597, 1);
961 case USB_RECIP_INTERFACE:
962 control_end(r8a66597, 1);
964 case USB_RECIP_ENDPOINT: {
965 struct r8a66597_ep *ep;
966 struct r8a66597_request *req;
967 u16 w_index = le16_to_cpu(ctrl->wIndex);
969 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
971 pipe_stop(r8a66597, ep->pipenum);
972 control_reg_sqclr(r8a66597, ep->pipenum);
973 spin_unlock(&r8a66597->lock);
974 usb_ep_clear_halt(&ep->ep);
975 spin_lock(&r8a66597->lock);
978 control_end(r8a66597, 1);
980 req = get_request_from_ep(ep);
983 if (list_empty(&ep->queue))
985 start_packet(ep, req);
986 } else if (!list_empty(&ep->queue))
987 pipe_start(r8a66597, ep->pipenum);
991 pipe_stall(r8a66597, 0);
996 static void set_feature(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
1001 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1002 case USB_RECIP_DEVICE:
1003 switch (le16_to_cpu(ctrl->wValue)) {
1004 case USB_DEVICE_TEST_MODE:
1005 control_end(r8a66597, 1);
1006 /* Wait for the completion of status stage */
1008 tmp = r8a66597_read(r8a66597, INTSTS0) & CTSQ;
1010 } while (tmp != CS_IDST || timeout-- > 0);
1013 r8a66597_bset(r8a66597,
1014 le16_to_cpu(ctrl->wIndex >> 8),
1018 pipe_stall(r8a66597, 0);
1022 case USB_RECIP_INTERFACE:
1023 control_end(r8a66597, 1);
1025 case USB_RECIP_ENDPOINT: {
1026 struct r8a66597_ep *ep;
1027 u16 w_index = le16_to_cpu(ctrl->wIndex);
1029 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
1030 pipe_stall(r8a66597, ep->pipenum);
1032 control_end(r8a66597, 1);
1036 pipe_stall(r8a66597, 0);
1041 /* if return value is true, call class driver's setup() */
1042 static int setup_packet(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
1044 u16 *p = (u16 *)ctrl;
1045 unsigned long offset = USBREQ;
1049 r8a66597_write(r8a66597, ~VALID, INTSTS0);
1051 for (i = 0; i < 4; i++)
1052 p[i] = r8a66597_read(r8a66597, offset + i*2);
1055 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1056 switch (ctrl->bRequest) {
1057 case USB_REQ_GET_STATUS:
1058 get_status(r8a66597, ctrl);
1060 case USB_REQ_CLEAR_FEATURE:
1061 clear_feature(r8a66597, ctrl);
1063 case USB_REQ_SET_FEATURE:
1064 set_feature(r8a66597, ctrl);
1075 static void r8a66597_update_usb_speed(struct r8a66597 *r8a66597)
1077 u16 speed = get_usb_speed(r8a66597);
1081 r8a66597->gadget.speed = USB_SPEED_HIGH;
1084 r8a66597->gadget.speed = USB_SPEED_FULL;
1087 r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
1088 printk(KERN_ERR "USB speed unknown\n");
1092 static void irq_device_state(struct r8a66597 *r8a66597)
1096 dvsq = r8a66597_read(r8a66597, INTSTS0) & DVSQ;
1097 r8a66597_write(r8a66597, ~DVST, INTSTS0);
1099 if (dvsq == DS_DFLT) {
1101 spin_unlock(&r8a66597->lock);
1102 r8a66597->driver->disconnect(&r8a66597->gadget);
1103 spin_lock(&r8a66597->lock);
1104 r8a66597_update_usb_speed(r8a66597);
1106 if (r8a66597->old_dvsq == DS_CNFG && dvsq != DS_CNFG)
1107 r8a66597_update_usb_speed(r8a66597);
1108 if ((dvsq == DS_CNFG || dvsq == DS_ADDS)
1109 && r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1110 r8a66597_update_usb_speed(r8a66597);
1112 r8a66597->old_dvsq = dvsq;
1115 static void irq_control_stage(struct r8a66597 *r8a66597)
1116 __releases(r8a66597->lock)
1117 __acquires(r8a66597->lock)
1119 struct usb_ctrlrequest ctrl;
1122 ctsq = r8a66597_read(r8a66597, INTSTS0) & CTSQ;
1123 r8a66597_write(r8a66597, ~CTRT, INTSTS0);
1127 struct r8a66597_ep *ep;
1128 struct r8a66597_request *req;
1129 ep = &r8a66597->ep[0];
1130 req = get_request_from_ep(ep);
1131 transfer_complete(ep, req, 0);
1138 if (setup_packet(r8a66597, &ctrl)) {
1139 spin_unlock(&r8a66597->lock);
1140 if (r8a66597->driver->setup(&r8a66597->gadget, &ctrl)
1142 pipe_stall(r8a66597, 0);
1143 spin_lock(&r8a66597->lock);
1148 control_end(r8a66597, 0);
1151 printk(KERN_ERR "ctrl_stage: unexpect ctsq(%x)\n", ctsq);
1156 static irqreturn_t r8a66597_irq(int irq, void *_r8a66597)
1158 struct r8a66597 *r8a66597 = _r8a66597;
1161 u16 brdysts, nrdysts, bempsts;
1162 u16 brdyenb, nrdyenb, bempenb;
1166 spin_lock(&r8a66597->lock);
1168 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1169 intenb0 = r8a66597_read(r8a66597, INTENB0);
1171 savepipe = r8a66597_read(r8a66597, CFIFOSEL);
1173 mask0 = intsts0 & intenb0;
1175 brdysts = r8a66597_read(r8a66597, BRDYSTS);
1176 nrdysts = r8a66597_read(r8a66597, NRDYSTS);
1177 bempsts = r8a66597_read(r8a66597, BEMPSTS);
1178 brdyenb = r8a66597_read(r8a66597, BRDYENB);
1179 nrdyenb = r8a66597_read(r8a66597, NRDYENB);
1180 bempenb = r8a66597_read(r8a66597, BEMPENB);
1182 if (mask0 & VBINT) {
1183 r8a66597_write(r8a66597, 0xffff & ~VBINT,
1185 r8a66597_start_xclock(r8a66597);
1187 /* start vbus sampling */
1188 r8a66597->old_vbus = r8a66597_read(r8a66597, INTSTS0)
1190 r8a66597->scount = R8A66597_MAX_SAMPLING;
1192 mod_timer(&r8a66597->timer,
1193 jiffies + msecs_to_jiffies(50));
1196 irq_device_state(r8a66597);
1198 if ((intsts0 & BRDY) && (intenb0 & BRDYE)
1199 && (brdysts & brdyenb))
1200 irq_pipe_ready(r8a66597, brdysts, brdyenb);
1201 if ((intsts0 & BEMP) && (intenb0 & BEMPE)
1202 && (bempsts & bempenb))
1203 irq_pipe_empty(r8a66597, bempsts, bempenb);
1206 irq_control_stage(r8a66597);
1209 r8a66597_write(r8a66597, savepipe, CFIFOSEL);
1211 spin_unlock(&r8a66597->lock);
1215 static void r8a66597_timer(unsigned long _r8a66597)
1217 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1218 unsigned long flags;
1221 spin_lock_irqsave(&r8a66597->lock, flags);
1222 tmp = r8a66597_read(r8a66597, SYSCFG0);
1223 if (r8a66597->scount > 0) {
1224 tmp = r8a66597_read(r8a66597, INTSTS0) & VBSTS;
1225 if (tmp == r8a66597->old_vbus) {
1227 if (r8a66597->scount == 0) {
1229 r8a66597_usb_connect(r8a66597);
1231 r8a66597_usb_disconnect(r8a66597);
1233 mod_timer(&r8a66597->timer,
1234 jiffies + msecs_to_jiffies(50));
1237 r8a66597->scount = R8A66597_MAX_SAMPLING;
1238 r8a66597->old_vbus = tmp;
1239 mod_timer(&r8a66597->timer,
1240 jiffies + msecs_to_jiffies(50));
1243 spin_unlock_irqrestore(&r8a66597->lock, flags);
1246 /*-------------------------------------------------------------------------*/
1247 static int r8a66597_enable(struct usb_ep *_ep,
1248 const struct usb_endpoint_descriptor *desc)
1250 struct r8a66597_ep *ep;
1252 ep = container_of(_ep, struct r8a66597_ep, ep);
1253 return alloc_pipe_config(ep, desc);
1256 static int r8a66597_disable(struct usb_ep *_ep)
1258 struct r8a66597_ep *ep;
1259 struct r8a66597_request *req;
1260 unsigned long flags;
1262 ep = container_of(_ep, struct r8a66597_ep, ep);
1265 while (!list_empty(&ep->queue)) {
1266 req = get_request_from_ep(ep);
1267 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1268 transfer_complete(ep, req, -ECONNRESET);
1269 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1272 pipe_irq_disable(ep->r8a66597, ep->pipenum);
1273 return free_pipe_config(ep);
1276 static struct usb_request *r8a66597_alloc_request(struct usb_ep *_ep,
1279 struct r8a66597_request *req;
1281 req = kzalloc(sizeof(struct r8a66597_request), gfp_flags);
1285 INIT_LIST_HEAD(&req->queue);
1290 static void r8a66597_free_request(struct usb_ep *_ep, struct usb_request *_req)
1292 struct r8a66597_request *req;
1294 req = container_of(_req, struct r8a66597_request, req);
1298 static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
1301 struct r8a66597_ep *ep;
1302 struct r8a66597_request *req;
1303 unsigned long flags;
1306 ep = container_of(_ep, struct r8a66597_ep, ep);
1307 req = container_of(_req, struct r8a66597_request, req);
1309 if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1312 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1314 if (list_empty(&ep->queue))
1317 list_add_tail(&req->queue, &ep->queue);
1318 req->req.actual = 0;
1319 req->req.status = -EINPROGRESS;
1321 if (ep->desc == NULL) /* control */
1324 if (request && !ep->busy)
1325 start_packet(ep, req);
1328 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1333 static int r8a66597_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1335 struct r8a66597_ep *ep;
1336 struct r8a66597_request *req;
1337 unsigned long flags;
1339 ep = container_of(_ep, struct r8a66597_ep, ep);
1340 req = container_of(_req, struct r8a66597_request, req);
1342 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1343 if (!list_empty(&ep->queue))
1344 transfer_complete(ep, req, -ECONNRESET);
1345 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1350 static int r8a66597_set_halt(struct usb_ep *_ep, int value)
1352 struct r8a66597_ep *ep;
1353 struct r8a66597_request *req;
1354 unsigned long flags;
1357 ep = container_of(_ep, struct r8a66597_ep, ep);
1358 req = get_request_from_ep(ep);
1360 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1361 if (!list_empty(&ep->queue)) {
1367 pipe_stall(ep->r8a66597, ep->pipenum);
1371 pipe_stop(ep->r8a66597, ep->pipenum);
1375 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1379 static int r8a66597_set_wedge(struct usb_ep *_ep)
1381 struct r8a66597_ep *ep;
1382 unsigned long flags;
1384 ep = container_of(_ep, struct r8a66597_ep, ep);
1386 if (!ep || !ep->desc)
1389 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1391 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1393 return usb_ep_set_halt(_ep);
1396 static void r8a66597_fifo_flush(struct usb_ep *_ep)
1398 struct r8a66597_ep *ep;
1399 unsigned long flags;
1401 ep = container_of(_ep, struct r8a66597_ep, ep);
1402 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1403 if (list_empty(&ep->queue) && !ep->busy) {
1404 pipe_stop(ep->r8a66597, ep->pipenum);
1405 r8a66597_bclr(ep->r8a66597, BCLR, ep->fifoctr);
1407 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1410 static struct usb_ep_ops r8a66597_ep_ops = {
1411 .enable = r8a66597_enable,
1412 .disable = r8a66597_disable,
1414 .alloc_request = r8a66597_alloc_request,
1415 .free_request = r8a66597_free_request,
1417 .queue = r8a66597_queue,
1418 .dequeue = r8a66597_dequeue,
1420 .set_halt = r8a66597_set_halt,
1421 .set_wedge = r8a66597_set_wedge,
1422 .fifo_flush = r8a66597_fifo_flush,
1425 /*-------------------------------------------------------------------------*/
1426 static struct r8a66597 *the_controller;
1428 static int r8a66597_start(struct usb_gadget_driver *driver,
1429 int (*bind)(struct usb_gadget *))
1431 struct r8a66597 *r8a66597 = the_controller;
1435 || driver->speed != USB_SPEED_HIGH
1441 if (r8a66597->driver)
1444 /* hook up the driver */
1445 driver->driver.bus = NULL;
1446 r8a66597->driver = driver;
1447 r8a66597->gadget.dev.driver = &driver->driver;
1449 retval = device_add(&r8a66597->gadget.dev);
1451 printk(KERN_ERR "device_add error (%d)\n", retval);
1455 retval = bind(&r8a66597->gadget);
1457 printk(KERN_ERR "bind to driver error (%d)\n", retval);
1458 device_del(&r8a66597->gadget.dev);
1462 init_controller(r8a66597);
1463 r8a66597_bset(r8a66597, VBSE, INTENB0);
1464 if (r8a66597_read(r8a66597, INTSTS0) & VBSTS) {
1465 r8a66597_start_xclock(r8a66597);
1466 /* start vbus sampling */
1467 r8a66597->old_vbus = r8a66597_read(r8a66597,
1469 r8a66597->scount = R8A66597_MAX_SAMPLING;
1470 mod_timer(&r8a66597->timer, jiffies + msecs_to_jiffies(50));
1476 r8a66597->driver = NULL;
1477 r8a66597->gadget.dev.driver = NULL;
1482 static int r8a66597_stop(struct usb_gadget_driver *driver)
1484 struct r8a66597 *r8a66597 = the_controller;
1485 unsigned long flags;
1487 if (driver != r8a66597->driver || !driver->unbind)
1490 spin_lock_irqsave(&r8a66597->lock, flags);
1491 if (r8a66597->gadget.speed != USB_SPEED_UNKNOWN)
1492 r8a66597_usb_disconnect(r8a66597);
1493 r8a66597_bclr(r8a66597, VBSE, INTENB0);
1494 disable_controller(r8a66597);
1495 spin_unlock_irqrestore(&r8a66597->lock, flags);
1497 driver->unbind(&r8a66597->gadget);
1499 device_del(&r8a66597->gadget.dev);
1500 r8a66597->driver = NULL;
1504 /*-------------------------------------------------------------------------*/
1505 static int r8a66597_get_frame(struct usb_gadget *_gadget)
1507 struct r8a66597 *r8a66597 = gadget_to_r8a66597(_gadget);
1508 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1511 static int r8a66597_pullup(struct usb_gadget *gadget, int is_on)
1513 struct r8a66597 *r8a66597 = gadget_to_r8a66597(gadget);
1514 unsigned long flags;
1516 spin_lock_irqsave(&r8a66597->lock, flags);
1518 r8a66597_bset(r8a66597, DPRPU, SYSCFG0);
1520 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
1521 spin_unlock_irqrestore(&r8a66597->lock, flags);
1526 static struct usb_gadget_ops r8a66597_gadget_ops = {
1527 .get_frame = r8a66597_get_frame,
1528 .start = r8a66597_start,
1529 .stop = r8a66597_stop,
1530 .pullup = r8a66597_pullup,
1533 static int __exit r8a66597_remove(struct platform_device *pdev)
1535 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
1537 usb_del_gadget_udc(&r8a66597->gadget);
1538 del_timer_sync(&r8a66597->timer);
1539 iounmap(r8a66597->reg);
1540 free_irq(platform_get_irq(pdev, 0), r8a66597);
1541 r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
1542 #ifdef CONFIG_HAVE_CLK
1543 if (r8a66597->pdata->on_chip) {
1544 clk_disable(r8a66597->clk);
1545 clk_put(r8a66597->clk);
1552 static void nop_completion(struct usb_ep *ep, struct usb_request *r)
1556 static int __init r8a66597_probe(struct platform_device *pdev)
1558 #ifdef CONFIG_HAVE_CLK
1561 struct resource *res, *ires;
1563 void __iomem *reg = NULL;
1564 struct r8a66597 *r8a66597 = NULL;
1567 unsigned long irq_trigger;
1569 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1572 printk(KERN_ERR "platform_get_resource error.\n");
1576 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1578 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
1582 printk(KERN_ERR "platform_get_irq error.\n");
1586 reg = ioremap(res->start, resource_size(res));
1589 printk(KERN_ERR "ioremap error.\n");
1593 /* initialize ucd */
1594 r8a66597 = kzalloc(sizeof(struct r8a66597), GFP_KERNEL);
1595 if (r8a66597 == NULL) {
1597 printk(KERN_ERR "kzalloc error\n");
1601 spin_lock_init(&r8a66597->lock);
1602 dev_set_drvdata(&pdev->dev, r8a66597);
1603 r8a66597->pdata = pdev->dev.platform_data;
1604 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
1606 r8a66597->gadget.ops = &r8a66597_gadget_ops;
1607 device_initialize(&r8a66597->gadget.dev);
1608 dev_set_name(&r8a66597->gadget.dev, "gadget");
1609 r8a66597->gadget.is_dualspeed = 1;
1610 r8a66597->gadget.dev.parent = &pdev->dev;
1611 r8a66597->gadget.dev.dma_mask = pdev->dev.dma_mask;
1612 r8a66597->gadget.dev.release = pdev->dev.release;
1613 r8a66597->gadget.name = udc_name;
1615 init_timer(&r8a66597->timer);
1616 r8a66597->timer.function = r8a66597_timer;
1617 r8a66597->timer.data = (unsigned long)r8a66597;
1618 r8a66597->reg = reg;
1620 #ifdef CONFIG_HAVE_CLK
1621 if (r8a66597->pdata->on_chip) {
1622 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
1623 r8a66597->clk = clk_get(&pdev->dev, clk_name);
1624 if (IS_ERR(r8a66597->clk)) {
1625 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
1627 ret = PTR_ERR(r8a66597->clk);
1630 clk_enable(r8a66597->clk);
1634 disable_controller(r8a66597); /* make sure controller is disabled */
1636 ret = request_irq(irq, r8a66597_irq, IRQF_DISABLED | IRQF_SHARED,
1637 udc_name, r8a66597);
1639 printk(KERN_ERR "request_irq error (%d)\n", ret);
1643 INIT_LIST_HEAD(&r8a66597->gadget.ep_list);
1644 r8a66597->gadget.ep0 = &r8a66597->ep[0].ep;
1645 INIT_LIST_HEAD(&r8a66597->gadget.ep0->ep_list);
1646 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
1647 struct r8a66597_ep *ep = &r8a66597->ep[i];
1650 INIT_LIST_HEAD(&r8a66597->ep[i].ep.ep_list);
1651 list_add_tail(&r8a66597->ep[i].ep.ep_list,
1652 &r8a66597->gadget.ep_list);
1654 ep->r8a66597 = r8a66597;
1655 INIT_LIST_HEAD(&ep->queue);
1656 ep->ep.name = r8a66597_ep_name[i];
1657 ep->ep.ops = &r8a66597_ep_ops;
1658 ep->ep.maxpacket = 512;
1660 r8a66597->ep[0].ep.maxpacket = 64;
1661 r8a66597->ep[0].pipenum = 0;
1662 r8a66597->ep[0].fifoaddr = CFIFO;
1663 r8a66597->ep[0].fifosel = CFIFOSEL;
1664 r8a66597->ep[0].fifoctr = CFIFOCTR;
1665 r8a66597->ep[0].fifotrn = 0;
1666 r8a66597->ep[0].pipectr = get_pipectr_addr(0);
1667 r8a66597->pipenum2ep[0] = &r8a66597->ep[0];
1668 r8a66597->epaddr2ep[0] = &r8a66597->ep[0];
1670 the_controller = r8a66597;
1672 r8a66597->ep0_req = r8a66597_alloc_request(&r8a66597->ep[0].ep,
1674 if (r8a66597->ep0_req == NULL)
1676 r8a66597->ep0_req->complete = nop_completion;
1678 ret = usb_add_gadget_udc(&pdev->dev, &r8a66597->gadget);
1682 dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1686 r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
1688 free_irq(irq, r8a66597);
1690 #ifdef CONFIG_HAVE_CLK
1691 if (r8a66597->pdata->on_chip) {
1692 clk_disable(r8a66597->clk);
1693 clk_put(r8a66597->clk);
1698 if (r8a66597->ep0_req)
1699 r8a66597_free_request(&r8a66597->ep[0].ep,
1709 /*-------------------------------------------------------------------------*/
1710 static struct platform_driver r8a66597_driver = {
1711 .remove = __exit_p(r8a66597_remove),
1713 .name = (char *) udc_name,
1716 MODULE_ALIAS("platform:r8a66597_udc");
1718 static int __init r8a66597_udc_init(void)
1720 return platform_driver_probe(&r8a66597_driver, r8a66597_probe);
1722 module_init(r8a66597_udc_init);
1724 static void __exit r8a66597_udc_cleanup(void)
1726 platform_driver_unregister(&r8a66597_driver);
1728 module_exit(r8a66597_udc_cleanup);
1730 MODULE_DESCRIPTION("R8A66597 USB gadget driver");
1731 MODULE_LICENSE("GPL");
1732 MODULE_AUTHOR("Yoshihiro Shimoda");