2 * R8A66597 UDC (USB gadget)
4 * Copyright (C) 2006-2009 Renesas Solutions Corp.
6 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
27 #include <linux/platform_device.h>
28 #include <linux/clk.h>
29 #include <linux/err.h>
30 #include <linux/slab.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
35 #include "r8a66597-udc.h"
37 #define DRIVER_VERSION "2009-08-18"
39 static const char udc_name[] = "r8a66597_udc";
40 static const char *r8a66597_ep_name[] = {
41 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7",
45 static void disable_controller(struct r8a66597 *r8a66597);
46 static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req);
47 static void irq_packet_write(struct r8a66597_ep *ep,
48 struct r8a66597_request *req);
49 static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
52 static void transfer_complete(struct r8a66597_ep *ep,
53 struct r8a66597_request *req, int status);
55 /*-------------------------------------------------------------------------*/
56 static inline u16 get_usb_speed(struct r8a66597 *r8a66597)
58 return r8a66597_read(r8a66597, DVSTCTR0) & RHST;
61 static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
66 tmp = r8a66597_read(r8a66597, INTENB0);
67 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
69 r8a66597_bset(r8a66597, (1 << pipenum), reg);
70 r8a66597_write(r8a66597, tmp, INTENB0);
73 static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
78 tmp = r8a66597_read(r8a66597, INTENB0);
79 r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
81 r8a66597_bclr(r8a66597, (1 << pipenum), reg);
82 r8a66597_write(r8a66597, tmp, INTENB0);
85 static void r8a66597_usb_connect(struct r8a66597 *r8a66597)
87 r8a66597_bset(r8a66597, CTRE, INTENB0);
88 r8a66597_bset(r8a66597, BEMPE | BRDYE, INTENB0);
90 r8a66597_bset(r8a66597, DPRPU, SYSCFG0);
93 static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597)
94 __releases(r8a66597->lock)
95 __acquires(r8a66597->lock)
97 r8a66597_bclr(r8a66597, CTRE, INTENB0);
98 r8a66597_bclr(r8a66597, BEMPE | BRDYE, INTENB0);
99 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
101 r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
102 spin_unlock(&r8a66597->lock);
103 r8a66597->driver->disconnect(&r8a66597->gadget);
104 spin_lock(&r8a66597->lock);
106 disable_controller(r8a66597);
107 INIT_LIST_HEAD(&r8a66597->ep[0].queue);
110 static inline u16 control_reg_get_pid(struct r8a66597 *r8a66597, u16 pipenum)
113 unsigned long offset;
116 pid = r8a66597_read(r8a66597, DCPCTR) & PID;
117 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
118 offset = get_pipectr_addr(pipenum);
119 pid = r8a66597_read(r8a66597, offset) & PID;
121 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
126 static inline void control_reg_set_pid(struct r8a66597 *r8a66597, u16 pipenum,
129 unsigned long offset;
132 r8a66597_mdfy(r8a66597, pid, PID, DCPCTR);
133 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
134 offset = get_pipectr_addr(pipenum);
135 r8a66597_mdfy(r8a66597, pid, PID, offset);
137 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
140 static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum)
142 control_reg_set_pid(r8a66597, pipenum, PID_BUF);
145 static inline void pipe_stop(struct r8a66597 *r8a66597, u16 pipenum)
147 control_reg_set_pid(r8a66597, pipenum, PID_NAK);
150 static inline void pipe_stall(struct r8a66597 *r8a66597, u16 pipenum)
152 control_reg_set_pid(r8a66597, pipenum, PID_STALL);
155 static inline u16 control_reg_get(struct r8a66597 *r8a66597, u16 pipenum)
158 unsigned long offset;
161 ret = r8a66597_read(r8a66597, DCPCTR);
162 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
163 offset = get_pipectr_addr(pipenum);
164 ret = r8a66597_read(r8a66597, offset);
166 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
171 static inline void control_reg_sqclr(struct r8a66597 *r8a66597, u16 pipenum)
173 unsigned long offset;
175 pipe_stop(r8a66597, pipenum);
178 r8a66597_bset(r8a66597, SQCLR, DCPCTR);
179 else if (pipenum < R8A66597_MAX_NUM_PIPE) {
180 offset = get_pipectr_addr(pipenum);
181 r8a66597_bset(r8a66597, SQCLR, offset);
183 printk(KERN_ERR "unexpect pipe num(%d)\n", pipenum);
186 static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum)
192 tmp = r8a66597_read(r8a66597, DCPCFG);
193 if ((tmp & R8A66597_CNTMD) != 0)
196 tmp = r8a66597_read(r8a66597, DCPMAXP);
200 r8a66597_write(r8a66597, pipenum, PIPESEL);
201 tmp = r8a66597_read(r8a66597, PIPECFG);
202 if ((tmp & R8A66597_CNTMD) != 0) {
203 tmp = r8a66597_read(r8a66597, PIPEBUF);
204 size = ((tmp >> 10) + 1) * 64;
206 tmp = r8a66597_read(r8a66597, PIPEMAXP);
214 static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
216 if (r8a66597->pdata->on_chip)
222 static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum)
224 struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
229 r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel);
233 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
236 static int pipe_buffer_setting(struct r8a66597 *r8a66597,
237 struct r8a66597_pipe_info *info)
239 u16 bufnum = 0, buf_bsize = 0;
245 r8a66597_write(r8a66597, info->pipe, PIPESEL);
248 pipecfg |= R8A66597_DIR;
249 pipecfg |= info->type;
250 pipecfg |= info->epnum;
251 switch (info->type) {
253 bufnum = 4 + (info->pipe - R8A66597_BASE_PIPENUM_INT);
257 /* isochronous pipes may be used as bulk pipes */
258 if (info->pipe > R8A66597_BASE_PIPENUM_BULK)
259 bufnum = info->pipe - R8A66597_BASE_PIPENUM_BULK;
261 bufnum = info->pipe - R8A66597_BASE_PIPENUM_ISOC;
263 bufnum = R8A66597_BASE_BUFNUM + (bufnum * 16);
265 pipecfg |= R8A66597_DBLB;
267 pipecfg |= R8A66597_SHTNAK;
270 bufnum = R8A66597_BASE_BUFNUM +
271 (info->pipe - R8A66597_BASE_PIPENUM_ISOC) * 16;
276 if (buf_bsize && ((bufnum + 16) >= R8A66597_MAX_BUFNUM)) {
277 pr_err(KERN_ERR "r8a66597 pipe memory is insufficient\n");
281 r8a66597_write(r8a66597, pipecfg, PIPECFG);
282 r8a66597_write(r8a66597, (buf_bsize << 10) | (bufnum), PIPEBUF);
283 r8a66597_write(r8a66597, info->maxpacket, PIPEMAXP);
286 r8a66597_write(r8a66597, info->interval, PIPEPERI);
291 static void pipe_buffer_release(struct r8a66597 *r8a66597,
292 struct r8a66597_pipe_info *info)
297 if (is_bulk_pipe(info->pipe))
299 else if (is_interrupt_pipe(info->pipe))
300 r8a66597->interrupt--;
301 else if (is_isoc_pipe(info->pipe)) {
302 r8a66597->isochronous--;
303 if (info->type == R8A66597_BULK)
306 printk(KERN_ERR "ep_release: unexpect pipenum (%d)\n",
310 static void pipe_initialize(struct r8a66597_ep *ep)
312 struct r8a66597 *r8a66597 = ep->r8a66597;
314 r8a66597_mdfy(r8a66597, 0, CURPIPE, ep->fifosel);
316 r8a66597_write(r8a66597, ACLRM, ep->pipectr);
317 r8a66597_write(r8a66597, 0, ep->pipectr);
318 r8a66597_write(r8a66597, SQCLR, ep->pipectr);
320 r8a66597_mdfy(r8a66597, ep->pipenum, CURPIPE, ep->fifosel);
324 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
328 static void r8a66597_ep_setting(struct r8a66597 *r8a66597,
329 struct r8a66597_ep *ep,
330 const struct usb_endpoint_descriptor *desc,
331 u16 pipenum, int dma)
334 ep->fifoaddr = CFIFO;
335 ep->fifosel = CFIFOSEL;
336 ep->fifoctr = CFIFOCTR;
339 ep->pipectr = get_pipectr_addr(pipenum);
340 ep->pipenum = pipenum;
341 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
342 r8a66597->pipenum2ep[pipenum] = ep;
343 r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK]
345 INIT_LIST_HEAD(&ep->queue);
348 static void r8a66597_ep_release(struct r8a66597_ep *ep)
350 struct r8a66597 *r8a66597 = ep->r8a66597;
351 u16 pipenum = ep->pipenum;
363 static int alloc_pipe_config(struct r8a66597_ep *ep,
364 const struct usb_endpoint_descriptor *desc)
366 struct r8a66597 *r8a66597 = ep->r8a66597;
367 struct r8a66597_pipe_info info;
369 unsigned char *counter;
374 if (ep->pipenum) /* already allocated pipe */
377 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
378 case USB_ENDPOINT_XFER_BULK:
379 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) {
380 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
381 printk(KERN_ERR "bulk pipe is insufficient\n");
384 info.pipe = R8A66597_BASE_PIPENUM_ISOC
385 + r8a66597->isochronous;
386 counter = &r8a66597->isochronous;
389 info.pipe = R8A66597_BASE_PIPENUM_BULK + r8a66597->bulk;
390 counter = &r8a66597->bulk;
392 info.type = R8A66597_BULK;
395 case USB_ENDPOINT_XFER_INT:
396 if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) {
397 printk(KERN_ERR "interrupt pipe is insufficient\n");
400 info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt;
401 info.type = R8A66597_INT;
402 counter = &r8a66597->interrupt;
404 case USB_ENDPOINT_XFER_ISOC:
405 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
406 printk(KERN_ERR "isochronous pipe is insufficient\n");
409 info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous;
410 info.type = R8A66597_ISO;
411 counter = &r8a66597->isochronous;
414 printk(KERN_ERR "unexpect xfer type\n");
417 ep->type = info.type;
419 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
420 info.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
421 info.interval = desc->bInterval;
422 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
427 ret = pipe_buffer_setting(r8a66597, &info);
429 printk(KERN_ERR "pipe_buffer_setting fail\n");
434 if ((counter == &r8a66597->isochronous) && info.type == R8A66597_BULK)
437 r8a66597_ep_setting(r8a66597, ep, desc, info.pipe, dma);
443 static int free_pipe_config(struct r8a66597_ep *ep)
445 struct r8a66597 *r8a66597 = ep->r8a66597;
446 struct r8a66597_pipe_info info;
448 info.pipe = ep->pipenum;
449 info.type = ep->type;
450 pipe_buffer_release(r8a66597, &info);
451 r8a66597_ep_release(ep);
456 /*-------------------------------------------------------------------------*/
457 static void pipe_irq_enable(struct r8a66597 *r8a66597, u16 pipenum)
459 enable_irq_ready(r8a66597, pipenum);
460 enable_irq_nrdy(r8a66597, pipenum);
463 static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
465 disable_irq_ready(r8a66597, pipenum);
466 disable_irq_nrdy(r8a66597, pipenum);
469 /* if complete is true, gadget driver complete function is not call */
470 static void control_end(struct r8a66597 *r8a66597, unsigned ccpl)
472 r8a66597->ep[0].internal_ccpl = ccpl;
473 pipe_start(r8a66597, 0);
474 r8a66597_bset(r8a66597, CCPL, DCPCTR);
477 static void start_ep0_write(struct r8a66597_ep *ep,
478 struct r8a66597_request *req)
480 struct r8a66597 *r8a66597 = ep->r8a66597;
482 pipe_change(r8a66597, ep->pipenum);
483 r8a66597_mdfy(r8a66597, ISEL, (ISEL | CURPIPE), CFIFOSEL);
484 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
485 if (req->req.length == 0) {
486 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
487 pipe_start(r8a66597, 0);
488 transfer_complete(ep, req, 0);
490 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
491 irq_ep0_write(ep, req);
495 static void start_packet_write(struct r8a66597_ep *ep,
496 struct r8a66597_request *req)
498 struct r8a66597 *r8a66597 = ep->r8a66597;
501 pipe_change(r8a66597, ep->pipenum);
502 disable_irq_empty(r8a66597, ep->pipenum);
503 pipe_start(r8a66597, ep->pipenum);
505 tmp = r8a66597_read(r8a66597, ep->fifoctr);
506 if (unlikely((tmp & FRDY) == 0))
507 pipe_irq_enable(r8a66597, ep->pipenum);
509 irq_packet_write(ep, req);
512 static void start_packet_read(struct r8a66597_ep *ep,
513 struct r8a66597_request *req)
515 struct r8a66597 *r8a66597 = ep->r8a66597;
516 u16 pipenum = ep->pipenum;
518 if (ep->pipenum == 0) {
519 r8a66597_mdfy(r8a66597, 0, (ISEL | CURPIPE), CFIFOSEL);
520 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
521 pipe_start(r8a66597, pipenum);
522 pipe_irq_enable(r8a66597, pipenum);
525 r8a66597_bset(r8a66597, TRCLR, ep->fifosel);
526 pipe_change(r8a66597, pipenum);
527 r8a66597_bset(r8a66597, TRENB, ep->fifosel);
528 r8a66597_write(r8a66597,
529 (req->req.length + ep->ep.maxpacket - 1)
533 pipe_start(r8a66597, pipenum); /* trigger once */
534 pipe_irq_enable(r8a66597, pipenum);
538 static void start_packet(struct r8a66597_ep *ep, struct r8a66597_request *req)
540 if (ep->desc->bEndpointAddress & USB_DIR_IN)
541 start_packet_write(ep, req);
543 start_packet_read(ep, req);
546 static void start_ep0(struct r8a66597_ep *ep, struct r8a66597_request *req)
550 ctsq = r8a66597_read(ep->r8a66597, INTSTS0) & CTSQ;
554 start_ep0_write(ep, req);
557 start_packet_read(ep, req);
561 control_end(ep->r8a66597, 0);
564 printk(KERN_ERR "start_ep0: unexpect ctsq(%x)\n", ctsq);
569 static void init_controller(struct r8a66597 *r8a66597)
571 u16 vif = r8a66597->pdata->vif ? LDRV : 0;
572 u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
573 u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
575 if (r8a66597->pdata->on_chip) {
576 r8a66597_bset(r8a66597, 0x04, SYSCFG1);
577 r8a66597_bset(r8a66597, HSE, SYSCFG0);
579 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
580 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
581 r8a66597_bset(r8a66597, USBE, SYSCFG0);
583 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
585 r8a66597_bset(r8a66597, irq_sense, INTENB1);
586 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
589 r8a66597_bset(r8a66597, vif | endian, PINCFG);
590 r8a66597_bset(r8a66597, HSE, SYSCFG0); /* High spd */
591 r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
594 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
595 r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
596 r8a66597_bset(r8a66597, USBE, SYSCFG0);
598 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
602 r8a66597_bset(r8a66597, PLLC, SYSCFG0);
606 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
608 r8a66597_bset(r8a66597, irq_sense, INTENB1);
609 r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
614 static void disable_controller(struct r8a66597 *r8a66597)
616 if (r8a66597->pdata->on_chip) {
617 r8a66597_bset(r8a66597, SCKE, SYSCFG0);
619 /* disable interrupts */
620 r8a66597_write(r8a66597, 0, INTENB0);
621 r8a66597_write(r8a66597, 0, INTENB1);
622 r8a66597_write(r8a66597, 0, BRDYENB);
623 r8a66597_write(r8a66597, 0, BEMPENB);
624 r8a66597_write(r8a66597, 0, NRDYENB);
627 r8a66597_write(r8a66597, 0, BRDYSTS);
628 r8a66597_write(r8a66597, 0, NRDYSTS);
629 r8a66597_write(r8a66597, 0, BEMPSTS);
631 r8a66597_bclr(r8a66597, USBE, SYSCFG0);
632 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
635 r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
637 r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
640 r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
644 static void r8a66597_start_xclock(struct r8a66597 *r8a66597)
648 if (!r8a66597->pdata->on_chip) {
649 tmp = r8a66597_read(r8a66597, SYSCFG0);
651 r8a66597_bset(r8a66597, XCKE, SYSCFG0);
655 static struct r8a66597_request *get_request_from_ep(struct r8a66597_ep *ep)
657 return list_entry(ep->queue.next, struct r8a66597_request, queue);
660 /*-------------------------------------------------------------------------*/
661 static void transfer_complete(struct r8a66597_ep *ep,
662 struct r8a66597_request *req, int status)
663 __releases(r8a66597->lock)
664 __acquires(r8a66597->lock)
668 if (unlikely(ep->pipenum == 0)) {
669 if (ep->internal_ccpl) {
670 ep->internal_ccpl = 0;
675 list_del_init(&req->queue);
676 if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
677 req->req.status = -ESHUTDOWN;
679 req->req.status = status;
681 if (!list_empty(&ep->queue))
684 spin_unlock(&ep->r8a66597->lock);
685 req->req.complete(&ep->ep, &req->req);
686 spin_lock(&ep->r8a66597->lock);
689 req = get_request_from_ep(ep);
691 start_packet(ep, req);
695 static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
702 u16 pipenum = ep->pipenum;
703 struct r8a66597 *r8a66597 = ep->r8a66597;
705 pipe_change(r8a66597, pipenum);
706 r8a66597_bset(r8a66597, ISEL, ep->fifosel);
710 tmp = r8a66597_read(r8a66597, ep->fifoctr);
712 printk(KERN_ERR "pipe0 is busy. maybe cpu i/o bus"
713 "conflict. please power off this controller.");
717 } while ((tmp & FRDY) == 0);
719 /* prepare parameters */
720 bufsize = get_buffer_size(r8a66597, pipenum);
721 buf = req->req.buf + req->req.actual;
722 size = min(bufsize, req->req.length - req->req.actual);
727 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
728 if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
729 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
732 /* update parameters */
733 req->req.actual += size;
735 /* check transfer finish */
736 if ((!req->req.zero && (req->req.actual == req->req.length))
737 || (size % ep->ep.maxpacket)
739 disable_irq_ready(r8a66597, pipenum);
740 disable_irq_empty(r8a66597, pipenum);
742 disable_irq_ready(r8a66597, pipenum);
743 enable_irq_empty(r8a66597, pipenum);
745 pipe_start(r8a66597, pipenum);
748 static void irq_packet_write(struct r8a66597_ep *ep,
749 struct r8a66597_request *req)
755 u16 pipenum = ep->pipenum;
756 struct r8a66597 *r8a66597 = ep->r8a66597;
758 pipe_change(r8a66597, pipenum);
759 tmp = r8a66597_read(r8a66597, ep->fifoctr);
760 if (unlikely((tmp & FRDY) == 0)) {
761 pipe_stop(r8a66597, pipenum);
762 pipe_irq_disable(r8a66597, pipenum);
763 printk(KERN_ERR "write fifo not ready. pipnum=%d\n", pipenum);
767 /* prepare parameters */
768 bufsize = get_buffer_size(r8a66597, pipenum);
769 buf = req->req.buf + req->req.actual;
770 size = min(bufsize, req->req.length - req->req.actual);
774 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
776 || ((size % ep->ep.maxpacket) != 0)
777 || ((bufsize != ep->ep.maxpacket)
778 && (bufsize > size)))
779 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
782 /* update parameters */
783 req->req.actual += size;
784 /* check transfer finish */
785 if ((!req->req.zero && (req->req.actual == req->req.length))
786 || (size % ep->ep.maxpacket)
788 disable_irq_ready(r8a66597, pipenum);
789 enable_irq_empty(r8a66597, pipenum);
791 disable_irq_empty(r8a66597, pipenum);
792 pipe_irq_enable(r8a66597, pipenum);
796 static void irq_packet_read(struct r8a66597_ep *ep,
797 struct r8a66597_request *req)
800 int rcv_len, bufsize, req_len;
803 u16 pipenum = ep->pipenum;
804 struct r8a66597 *r8a66597 = ep->r8a66597;
807 pipe_change(r8a66597, pipenum);
808 tmp = r8a66597_read(r8a66597, ep->fifoctr);
809 if (unlikely((tmp & FRDY) == 0)) {
810 req->req.status = -EPIPE;
811 pipe_stop(r8a66597, pipenum);
812 pipe_irq_disable(r8a66597, pipenum);
813 printk(KERN_ERR "read fifo not ready");
817 /* prepare parameters */
818 rcv_len = tmp & DTLN;
819 bufsize = get_buffer_size(r8a66597, pipenum);
821 buf = req->req.buf + req->req.actual;
822 req_len = req->req.length - req->req.actual;
823 if (rcv_len < bufsize)
824 size = min(rcv_len, req_len);
826 size = min(bufsize, req_len);
828 /* update parameters */
829 req->req.actual += size;
831 /* check transfer finish */
832 if ((!req->req.zero && (req->req.actual == req->req.length))
833 || (size % ep->ep.maxpacket)
835 pipe_stop(r8a66597, pipenum);
836 pipe_irq_disable(r8a66597, pipenum);
843 r8a66597_write(r8a66597, BCLR, ep->fifoctr);
845 r8a66597_read_fifo(r8a66597, ep->fifoaddr, buf, size);
849 if ((ep->pipenum != 0) && finish)
850 transfer_complete(ep, req, 0);
853 static void irq_pipe_ready(struct r8a66597 *r8a66597, u16 status, u16 enb)
857 struct r8a66597_ep *ep;
858 struct r8a66597_request *req;
860 if ((status & BRDY0) && (enb & BRDY0)) {
861 r8a66597_write(r8a66597, ~BRDY0, BRDYSTS);
862 r8a66597_mdfy(r8a66597, 0, CURPIPE, CFIFOSEL);
864 ep = &r8a66597->ep[0];
865 req = get_request_from_ep(ep);
866 irq_packet_read(ep, req);
868 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
869 check = 1 << pipenum;
870 if ((status & check) && (enb & check)) {
871 r8a66597_write(r8a66597, ~check, BRDYSTS);
872 ep = r8a66597->pipenum2ep[pipenum];
873 req = get_request_from_ep(ep);
874 if (ep->desc->bEndpointAddress & USB_DIR_IN)
875 irq_packet_write(ep, req);
877 irq_packet_read(ep, req);
883 static void irq_pipe_empty(struct r8a66597 *r8a66597, u16 status, u16 enb)
888 struct r8a66597_ep *ep;
889 struct r8a66597_request *req;
891 if ((status & BEMP0) && (enb & BEMP0)) {
892 r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
894 ep = &r8a66597->ep[0];
895 req = get_request_from_ep(ep);
896 irq_ep0_write(ep, req);
898 for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
899 check = 1 << pipenum;
900 if ((status & check) && (enb & check)) {
901 r8a66597_write(r8a66597, ~check, BEMPSTS);
902 tmp = control_reg_get(r8a66597, pipenum);
903 if ((tmp & INBUFM) == 0) {
904 disable_irq_empty(r8a66597, pipenum);
905 pipe_irq_disable(r8a66597, pipenum);
906 pipe_stop(r8a66597, pipenum);
907 ep = r8a66597->pipenum2ep[pipenum];
908 req = get_request_from_ep(ep);
909 if (!list_empty(&ep->queue))
910 transfer_complete(ep, req, 0);
917 static void get_status(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
918 __releases(r8a66597->lock)
919 __acquires(r8a66597->lock)
921 struct r8a66597_ep *ep;
924 u16 w_index = le16_to_cpu(ctrl->wIndex);
926 switch (ctrl->bRequestType & USB_RECIP_MASK) {
927 case USB_RECIP_DEVICE:
928 status = 1 << USB_DEVICE_SELF_POWERED;
930 case USB_RECIP_INTERFACE:
933 case USB_RECIP_ENDPOINT:
934 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
935 pid = control_reg_get_pid(r8a66597, ep->pipenum);
936 if (pid == PID_STALL)
937 status = 1 << USB_ENDPOINT_HALT;
942 pipe_stall(r8a66597, 0);
946 r8a66597->ep0_data = cpu_to_le16(status);
947 r8a66597->ep0_req->buf = &r8a66597->ep0_data;
948 r8a66597->ep0_req->length = 2;
949 /* AV: what happens if we get called again before that gets through? */
950 spin_unlock(&r8a66597->lock);
951 r8a66597_queue(r8a66597->gadget.ep0, r8a66597->ep0_req, GFP_KERNEL);
952 spin_lock(&r8a66597->lock);
955 static void clear_feature(struct r8a66597 *r8a66597,
956 struct usb_ctrlrequest *ctrl)
958 switch (ctrl->bRequestType & USB_RECIP_MASK) {
959 case USB_RECIP_DEVICE:
960 control_end(r8a66597, 1);
962 case USB_RECIP_INTERFACE:
963 control_end(r8a66597, 1);
965 case USB_RECIP_ENDPOINT: {
966 struct r8a66597_ep *ep;
967 struct r8a66597_request *req;
968 u16 w_index = le16_to_cpu(ctrl->wIndex);
970 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
972 pipe_stop(r8a66597, ep->pipenum);
973 control_reg_sqclr(r8a66597, ep->pipenum);
974 spin_unlock(&r8a66597->lock);
975 usb_ep_clear_halt(&ep->ep);
976 spin_lock(&r8a66597->lock);
979 control_end(r8a66597, 1);
981 req = get_request_from_ep(ep);
984 if (list_empty(&ep->queue))
986 start_packet(ep, req);
987 } else if (!list_empty(&ep->queue))
988 pipe_start(r8a66597, ep->pipenum);
992 pipe_stall(r8a66597, 0);
997 static void set_feature(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
1000 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1001 case USB_RECIP_DEVICE:
1002 control_end(r8a66597, 1);
1004 case USB_RECIP_INTERFACE:
1005 control_end(r8a66597, 1);
1007 case USB_RECIP_ENDPOINT: {
1008 struct r8a66597_ep *ep;
1009 u16 w_index = le16_to_cpu(ctrl->wIndex);
1011 ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
1012 pipe_stall(r8a66597, ep->pipenum);
1014 control_end(r8a66597, 1);
1018 pipe_stall(r8a66597, 0);
1023 /* if return value is true, call class driver's setup() */
1024 static int setup_packet(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
1026 u16 *p = (u16 *)ctrl;
1027 unsigned long offset = USBREQ;
1031 r8a66597_write(r8a66597, ~VALID, INTSTS0);
1033 for (i = 0; i < 4; i++)
1034 p[i] = r8a66597_read(r8a66597, offset + i*2);
1037 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1038 switch (ctrl->bRequest) {
1039 case USB_REQ_GET_STATUS:
1040 get_status(r8a66597, ctrl);
1042 case USB_REQ_CLEAR_FEATURE:
1043 clear_feature(r8a66597, ctrl);
1045 case USB_REQ_SET_FEATURE:
1046 set_feature(r8a66597, ctrl);
1057 static void r8a66597_update_usb_speed(struct r8a66597 *r8a66597)
1059 u16 speed = get_usb_speed(r8a66597);
1063 r8a66597->gadget.speed = USB_SPEED_HIGH;
1066 r8a66597->gadget.speed = USB_SPEED_FULL;
1069 r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
1070 printk(KERN_ERR "USB speed unknown\n");
1074 static void irq_device_state(struct r8a66597 *r8a66597)
1078 dvsq = r8a66597_read(r8a66597, INTSTS0) & DVSQ;
1079 r8a66597_write(r8a66597, ~DVST, INTSTS0);
1081 if (dvsq == DS_DFLT) {
1083 r8a66597->driver->disconnect(&r8a66597->gadget);
1084 r8a66597_update_usb_speed(r8a66597);
1086 if (r8a66597->old_dvsq == DS_CNFG && dvsq != DS_CNFG)
1087 r8a66597_update_usb_speed(r8a66597);
1088 if ((dvsq == DS_CNFG || dvsq == DS_ADDS)
1089 && r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1090 r8a66597_update_usb_speed(r8a66597);
1092 r8a66597->old_dvsq = dvsq;
1095 static void irq_control_stage(struct r8a66597 *r8a66597)
1096 __releases(r8a66597->lock)
1097 __acquires(r8a66597->lock)
1099 struct usb_ctrlrequest ctrl;
1102 ctsq = r8a66597_read(r8a66597, INTSTS0) & CTSQ;
1103 r8a66597_write(r8a66597, ~CTRT, INTSTS0);
1107 struct r8a66597_ep *ep;
1108 struct r8a66597_request *req;
1109 ep = &r8a66597->ep[0];
1110 req = get_request_from_ep(ep);
1111 transfer_complete(ep, req, 0);
1118 if (setup_packet(r8a66597, &ctrl)) {
1119 spin_unlock(&r8a66597->lock);
1120 if (r8a66597->driver->setup(&r8a66597->gadget, &ctrl)
1122 pipe_stall(r8a66597, 0);
1123 spin_lock(&r8a66597->lock);
1128 control_end(r8a66597, 0);
1131 printk(KERN_ERR "ctrl_stage: unexpect ctsq(%x)\n", ctsq);
1136 static irqreturn_t r8a66597_irq(int irq, void *_r8a66597)
1138 struct r8a66597 *r8a66597 = _r8a66597;
1141 u16 brdysts, nrdysts, bempsts;
1142 u16 brdyenb, nrdyenb, bempenb;
1146 spin_lock(&r8a66597->lock);
1148 intsts0 = r8a66597_read(r8a66597, INTSTS0);
1149 intenb0 = r8a66597_read(r8a66597, INTENB0);
1151 savepipe = r8a66597_read(r8a66597, CFIFOSEL);
1153 mask0 = intsts0 & intenb0;
1155 brdysts = r8a66597_read(r8a66597, BRDYSTS);
1156 nrdysts = r8a66597_read(r8a66597, NRDYSTS);
1157 bempsts = r8a66597_read(r8a66597, BEMPSTS);
1158 brdyenb = r8a66597_read(r8a66597, BRDYENB);
1159 nrdyenb = r8a66597_read(r8a66597, NRDYENB);
1160 bempenb = r8a66597_read(r8a66597, BEMPENB);
1162 if (mask0 & VBINT) {
1163 r8a66597_write(r8a66597, 0xffff & ~VBINT,
1165 r8a66597_start_xclock(r8a66597);
1167 /* start vbus sampling */
1168 r8a66597->old_vbus = r8a66597_read(r8a66597, INTSTS0)
1170 r8a66597->scount = R8A66597_MAX_SAMPLING;
1172 mod_timer(&r8a66597->timer,
1173 jiffies + msecs_to_jiffies(50));
1176 irq_device_state(r8a66597);
1178 if ((intsts0 & BRDY) && (intenb0 & BRDYE)
1179 && (brdysts & brdyenb))
1180 irq_pipe_ready(r8a66597, brdysts, brdyenb);
1181 if ((intsts0 & BEMP) && (intenb0 & BEMPE)
1182 && (bempsts & bempenb))
1183 irq_pipe_empty(r8a66597, bempsts, bempenb);
1186 irq_control_stage(r8a66597);
1189 r8a66597_write(r8a66597, savepipe, CFIFOSEL);
1191 spin_unlock(&r8a66597->lock);
1195 static void r8a66597_timer(unsigned long _r8a66597)
1197 struct r8a66597 *r8a66597 = (struct r8a66597 *)_r8a66597;
1198 unsigned long flags;
1201 spin_lock_irqsave(&r8a66597->lock, flags);
1202 tmp = r8a66597_read(r8a66597, SYSCFG0);
1203 if (r8a66597->scount > 0) {
1204 tmp = r8a66597_read(r8a66597, INTSTS0) & VBSTS;
1205 if (tmp == r8a66597->old_vbus) {
1207 if (r8a66597->scount == 0) {
1209 r8a66597_usb_connect(r8a66597);
1211 r8a66597_usb_disconnect(r8a66597);
1213 mod_timer(&r8a66597->timer,
1214 jiffies + msecs_to_jiffies(50));
1217 r8a66597->scount = R8A66597_MAX_SAMPLING;
1218 r8a66597->old_vbus = tmp;
1219 mod_timer(&r8a66597->timer,
1220 jiffies + msecs_to_jiffies(50));
1223 spin_unlock_irqrestore(&r8a66597->lock, flags);
1226 /*-------------------------------------------------------------------------*/
1227 static int r8a66597_enable(struct usb_ep *_ep,
1228 const struct usb_endpoint_descriptor *desc)
1230 struct r8a66597_ep *ep;
1232 ep = container_of(_ep, struct r8a66597_ep, ep);
1233 return alloc_pipe_config(ep, desc);
1236 static int r8a66597_disable(struct usb_ep *_ep)
1238 struct r8a66597_ep *ep;
1239 struct r8a66597_request *req;
1240 unsigned long flags;
1242 ep = container_of(_ep, struct r8a66597_ep, ep);
1245 while (!list_empty(&ep->queue)) {
1246 req = get_request_from_ep(ep);
1247 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1248 transfer_complete(ep, req, -ECONNRESET);
1249 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1252 pipe_irq_disable(ep->r8a66597, ep->pipenum);
1253 return free_pipe_config(ep);
1256 static struct usb_request *r8a66597_alloc_request(struct usb_ep *_ep,
1259 struct r8a66597_request *req;
1261 req = kzalloc(sizeof(struct r8a66597_request), gfp_flags);
1265 INIT_LIST_HEAD(&req->queue);
1270 static void r8a66597_free_request(struct usb_ep *_ep, struct usb_request *_req)
1272 struct r8a66597_request *req;
1274 req = container_of(_req, struct r8a66597_request, req);
1278 static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
1281 struct r8a66597_ep *ep;
1282 struct r8a66597_request *req;
1283 unsigned long flags;
1286 ep = container_of(_ep, struct r8a66597_ep, ep);
1287 req = container_of(_req, struct r8a66597_request, req);
1289 if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
1292 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1294 if (list_empty(&ep->queue))
1297 list_add_tail(&req->queue, &ep->queue);
1298 req->req.actual = 0;
1299 req->req.status = -EINPROGRESS;
1301 if (ep->desc == NULL) /* control */
1304 if (request && !ep->busy)
1305 start_packet(ep, req);
1308 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1313 static int r8a66597_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1315 struct r8a66597_ep *ep;
1316 struct r8a66597_request *req;
1317 unsigned long flags;
1319 ep = container_of(_ep, struct r8a66597_ep, ep);
1320 req = container_of(_req, struct r8a66597_request, req);
1322 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1323 if (!list_empty(&ep->queue))
1324 transfer_complete(ep, req, -ECONNRESET);
1325 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1330 static int r8a66597_set_halt(struct usb_ep *_ep, int value)
1332 struct r8a66597_ep *ep;
1333 struct r8a66597_request *req;
1334 unsigned long flags;
1337 ep = container_of(_ep, struct r8a66597_ep, ep);
1338 req = get_request_from_ep(ep);
1340 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1341 if (!list_empty(&ep->queue)) {
1347 pipe_stall(ep->r8a66597, ep->pipenum);
1351 pipe_stop(ep->r8a66597, ep->pipenum);
1355 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1359 static int r8a66597_set_wedge(struct usb_ep *_ep)
1361 struct r8a66597_ep *ep;
1362 unsigned long flags;
1364 ep = container_of(_ep, struct r8a66597_ep, ep);
1366 if (!ep || !ep->desc)
1369 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1371 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1373 return usb_ep_set_halt(_ep);
1376 static void r8a66597_fifo_flush(struct usb_ep *_ep)
1378 struct r8a66597_ep *ep;
1379 unsigned long flags;
1381 ep = container_of(_ep, struct r8a66597_ep, ep);
1382 spin_lock_irqsave(&ep->r8a66597->lock, flags);
1383 if (list_empty(&ep->queue) && !ep->busy) {
1384 pipe_stop(ep->r8a66597, ep->pipenum);
1385 r8a66597_bclr(ep->r8a66597, BCLR, ep->fifoctr);
1387 spin_unlock_irqrestore(&ep->r8a66597->lock, flags);
1390 static struct usb_ep_ops r8a66597_ep_ops = {
1391 .enable = r8a66597_enable,
1392 .disable = r8a66597_disable,
1394 .alloc_request = r8a66597_alloc_request,
1395 .free_request = r8a66597_free_request,
1397 .queue = r8a66597_queue,
1398 .dequeue = r8a66597_dequeue,
1400 .set_halt = r8a66597_set_halt,
1401 .set_wedge = r8a66597_set_wedge,
1402 .fifo_flush = r8a66597_fifo_flush,
1405 /*-------------------------------------------------------------------------*/
1406 static struct r8a66597 *the_controller;
1408 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1410 struct r8a66597 *r8a66597 = the_controller;
1414 || driver->speed != USB_SPEED_HIGH
1420 if (r8a66597->driver)
1423 /* hook up the driver */
1424 driver->driver.bus = NULL;
1425 r8a66597->driver = driver;
1426 r8a66597->gadget.dev.driver = &driver->driver;
1428 retval = device_add(&r8a66597->gadget.dev);
1430 printk(KERN_ERR "device_add error (%d)\n", retval);
1434 retval = driver->bind(&r8a66597->gadget);
1436 printk(KERN_ERR "bind to driver error (%d)\n", retval);
1437 device_del(&r8a66597->gadget.dev);
1441 r8a66597_bset(r8a66597, VBSE, INTENB0);
1442 if (r8a66597_read(r8a66597, INTSTS0) & VBSTS) {
1443 r8a66597_start_xclock(r8a66597);
1444 /* start vbus sampling */
1445 r8a66597->old_vbus = r8a66597_read(r8a66597,
1447 r8a66597->scount = R8A66597_MAX_SAMPLING;
1448 mod_timer(&r8a66597->timer, jiffies + msecs_to_jiffies(50));
1454 r8a66597->driver = NULL;
1455 r8a66597->gadget.dev.driver = NULL;
1459 EXPORT_SYMBOL(usb_gadget_register_driver);
1461 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1463 struct r8a66597 *r8a66597 = the_controller;
1464 unsigned long flags;
1466 if (driver != r8a66597->driver || !driver->unbind)
1469 spin_lock_irqsave(&r8a66597->lock, flags);
1470 if (r8a66597->gadget.speed != USB_SPEED_UNKNOWN)
1471 r8a66597_usb_disconnect(r8a66597);
1472 spin_unlock_irqrestore(&r8a66597->lock, flags);
1474 r8a66597_bclr(r8a66597, VBSE, INTENB0);
1476 driver->unbind(&r8a66597->gadget);
1478 init_controller(r8a66597);
1479 disable_controller(r8a66597);
1481 device_del(&r8a66597->gadget.dev);
1482 r8a66597->driver = NULL;
1485 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1487 /*-------------------------------------------------------------------------*/
1488 static int r8a66597_get_frame(struct usb_gadget *_gadget)
1490 struct r8a66597 *r8a66597 = gadget_to_r8a66597(_gadget);
1491 return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
1494 static struct usb_gadget_ops r8a66597_gadget_ops = {
1495 .get_frame = r8a66597_get_frame,
1498 static int __exit r8a66597_remove(struct platform_device *pdev)
1500 struct r8a66597 *r8a66597 = dev_get_drvdata(&pdev->dev);
1502 del_timer_sync(&r8a66597->timer);
1503 iounmap((void *)r8a66597->reg);
1504 free_irq(platform_get_irq(pdev, 0), r8a66597);
1505 r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
1506 #ifdef CONFIG_HAVE_CLK
1507 if (r8a66597->pdata->on_chip) {
1508 clk_disable(r8a66597->clk);
1509 clk_put(r8a66597->clk);
1516 static void nop_completion(struct usb_ep *ep, struct usb_request *r)
1520 static int __init r8a66597_probe(struct platform_device *pdev)
1522 #ifdef CONFIG_HAVE_CLK
1525 struct resource *res, *ires;
1527 void __iomem *reg = NULL;
1528 struct r8a66597 *r8a66597 = NULL;
1531 unsigned long irq_trigger;
1533 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1536 printk(KERN_ERR "platform_get_resource error.\n");
1540 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1542 irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
1546 printk(KERN_ERR "platform_get_irq error.\n");
1550 reg = ioremap(res->start, resource_size(res));
1553 printk(KERN_ERR "ioremap error.\n");
1557 /* initialize ucd */
1558 r8a66597 = kzalloc(sizeof(struct r8a66597), GFP_KERNEL);
1559 if (r8a66597 == NULL) {
1560 printk(KERN_ERR "kzalloc error\n");
1564 spin_lock_init(&r8a66597->lock);
1565 dev_set_drvdata(&pdev->dev, r8a66597);
1566 r8a66597->pdata = pdev->dev.platform_data;
1567 r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
1569 r8a66597->gadget.ops = &r8a66597_gadget_ops;
1570 device_initialize(&r8a66597->gadget.dev);
1571 dev_set_name(&r8a66597->gadget.dev, "gadget");
1572 r8a66597->gadget.is_dualspeed = 1;
1573 r8a66597->gadget.dev.parent = &pdev->dev;
1574 r8a66597->gadget.dev.dma_mask = pdev->dev.dma_mask;
1575 r8a66597->gadget.dev.release = pdev->dev.release;
1576 r8a66597->gadget.name = udc_name;
1578 init_timer(&r8a66597->timer);
1579 r8a66597->timer.function = r8a66597_timer;
1580 r8a66597->timer.data = (unsigned long)r8a66597;
1581 r8a66597->reg = (unsigned long)reg;
1583 #ifdef CONFIG_HAVE_CLK
1584 if (r8a66597->pdata->on_chip) {
1585 snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
1586 r8a66597->clk = clk_get(&pdev->dev, clk_name);
1587 if (IS_ERR(r8a66597->clk)) {
1588 dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
1590 ret = PTR_ERR(r8a66597->clk);
1593 clk_enable(r8a66597->clk);
1597 disable_controller(r8a66597); /* make sure controller is disabled */
1599 ret = request_irq(irq, r8a66597_irq, IRQF_DISABLED | IRQF_SHARED,
1600 udc_name, r8a66597);
1602 printk(KERN_ERR "request_irq error (%d)\n", ret);
1606 INIT_LIST_HEAD(&r8a66597->gadget.ep_list);
1607 r8a66597->gadget.ep0 = &r8a66597->ep[0].ep;
1608 INIT_LIST_HEAD(&r8a66597->gadget.ep0->ep_list);
1609 for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
1610 struct r8a66597_ep *ep = &r8a66597->ep[i];
1613 INIT_LIST_HEAD(&r8a66597->ep[i].ep.ep_list);
1614 list_add_tail(&r8a66597->ep[i].ep.ep_list,
1615 &r8a66597->gadget.ep_list);
1617 ep->r8a66597 = r8a66597;
1618 INIT_LIST_HEAD(&ep->queue);
1619 ep->ep.name = r8a66597_ep_name[i];
1620 ep->ep.ops = &r8a66597_ep_ops;
1621 ep->ep.maxpacket = 512;
1623 r8a66597->ep[0].ep.maxpacket = 64;
1624 r8a66597->ep[0].pipenum = 0;
1625 r8a66597->ep[0].fifoaddr = CFIFO;
1626 r8a66597->ep[0].fifosel = CFIFOSEL;
1627 r8a66597->ep[0].fifoctr = CFIFOCTR;
1628 r8a66597->ep[0].fifotrn = 0;
1629 r8a66597->ep[0].pipectr = get_pipectr_addr(0);
1630 r8a66597->pipenum2ep[0] = &r8a66597->ep[0];
1631 r8a66597->epaddr2ep[0] = &r8a66597->ep[0];
1633 the_controller = r8a66597;
1635 r8a66597->ep0_req = r8a66597_alloc_request(&r8a66597->ep[0].ep,
1637 if (r8a66597->ep0_req == NULL)
1639 r8a66597->ep0_req->complete = nop_completion;
1641 init_controller(r8a66597);
1643 dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1647 free_irq(irq, r8a66597);
1649 #ifdef CONFIG_HAVE_CLK
1650 if (r8a66597->pdata->on_chip) {
1651 clk_disable(r8a66597->clk);
1652 clk_put(r8a66597->clk);
1657 if (r8a66597->ep0_req)
1658 r8a66597_free_request(&r8a66597->ep[0].ep,
1668 /*-------------------------------------------------------------------------*/
1669 static struct platform_driver r8a66597_driver = {
1670 .remove = __exit_p(r8a66597_remove),
1672 .name = (char *) udc_name,
1676 static int __init r8a66597_udc_init(void)
1678 return platform_driver_probe(&r8a66597_driver, r8a66597_probe);
1680 module_init(r8a66597_udc_init);
1682 static void __exit r8a66597_udc_cleanup(void)
1684 platform_driver_unregister(&r8a66597_driver);
1686 module_exit(r8a66597_udc_cleanup);
1688 MODULE_DESCRIPTION("R8A66597 USB gadget driver");
1689 MODULE_LICENSE("GPL");
1690 MODULE_AUTHOR("Yoshihiro Shimoda");