2 * M66592 UDC (USB gadget)
4 * Copyright (C) 2006-2007 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>
29 #include <linux/usb/ch9.h>
30 #include <linux/usb/gadget.h>
32 #include "m66592-udc.h"
35 MODULE_DESCRIPTION("M66592 USB gadget driver");
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("Yoshihiro Shimoda");
38 MODULE_ALIAS("platform:m66592_udc");
40 #define DRIVER_VERSION "18 Oct 2007"
42 /* module parameters */
43 #if defined(CONFIG_SUPERH_BUILT_IN_M66592)
44 static unsigned short endian
= M66592_LITTLE
;
45 module_param(endian
, ushort
, 0644);
46 MODULE_PARM_DESC(endian
, "data endian: big=0, little=0 (default=0)");
48 static unsigned short clock
= M66592_XTAL24
;
49 module_param(clock
, ushort
, 0644);
50 MODULE_PARM_DESC(clock
, "input clock: 48MHz=32768, 24MHz=16384, 12MHz=0 "
53 static unsigned short vif
= M66592_LDRV
;
54 module_param(vif
, ushort
, 0644);
55 MODULE_PARM_DESC(vif
, "input VIF: 3.3V=32768, 1.5V=0 (default=32768)");
57 static unsigned short endian
;
58 module_param(endian
, ushort
, 0644);
59 MODULE_PARM_DESC(endian
, "data endian: big=256, little=0 (default=0)");
61 static unsigned short irq_sense
= M66592_INTL
;
62 module_param(irq_sense
, ushort
, 0644);
63 MODULE_PARM_DESC(irq_sense
, "IRQ sense: low level=2, falling edge=0 "
67 static const char udc_name
[] = "m66592_udc";
68 static const char *m66592_ep_name
[] = {
69 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7"
72 static void disable_controller(struct m66592
*m66592
);
73 static void irq_ep0_write(struct m66592_ep
*ep
, struct m66592_request
*req
);
74 static void irq_packet_write(struct m66592_ep
*ep
, struct m66592_request
*req
);
75 static int m66592_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
78 static void transfer_complete(struct m66592_ep
*ep
,
79 struct m66592_request
*req
, int status
);
81 /*-------------------------------------------------------------------------*/
82 static inline u16
get_usb_speed(struct m66592
*m66592
)
84 return (m66592_read(m66592
, M66592_DVSTCTR
) & M66592_RHST
);
87 static void enable_pipe_irq(struct m66592
*m66592
, u16 pipenum
,
92 tmp
= m66592_read(m66592
, M66592_INTENB0
);
93 m66592_bclr(m66592
, M66592_BEMPE
| M66592_NRDYE
| M66592_BRDYE
,
95 m66592_bset(m66592
, (1 << pipenum
), reg
);
96 m66592_write(m66592
, tmp
, M66592_INTENB0
);
99 static void disable_pipe_irq(struct m66592
*m66592
, u16 pipenum
,
104 tmp
= m66592_read(m66592
, M66592_INTENB0
);
105 m66592_bclr(m66592
, M66592_BEMPE
| M66592_NRDYE
| M66592_BRDYE
,
107 m66592_bclr(m66592
, (1 << pipenum
), reg
);
108 m66592_write(m66592
, tmp
, M66592_INTENB0
);
111 static void m66592_usb_connect(struct m66592
*m66592
)
113 m66592_bset(m66592
, M66592_CTRE
, M66592_INTENB0
);
114 m66592_bset(m66592
, M66592_WDST
| M66592_RDST
| M66592_CMPL
,
116 m66592_bset(m66592
, M66592_BEMPE
| M66592_BRDYE
, M66592_INTENB0
);
118 m66592_bset(m66592
, M66592_DPRPU
, M66592_SYSCFG
);
121 static void m66592_usb_disconnect(struct m66592
*m66592
)
122 __releases(m66592
->lock
)
123 __acquires(m66592
->lock
)
125 m66592_bclr(m66592
, M66592_CTRE
, M66592_INTENB0
);
126 m66592_bclr(m66592
, M66592_WDST
| M66592_RDST
| M66592_CMPL
,
128 m66592_bclr(m66592
, M66592_BEMPE
| M66592_BRDYE
, M66592_INTENB0
);
129 m66592_bclr(m66592
, M66592_DPRPU
, M66592_SYSCFG
);
131 m66592
->gadget
.speed
= USB_SPEED_UNKNOWN
;
132 spin_unlock(&m66592
->lock
);
133 m66592
->driver
->disconnect(&m66592
->gadget
);
134 spin_lock(&m66592
->lock
);
136 disable_controller(m66592
);
137 INIT_LIST_HEAD(&m66592
->ep
[0].queue
);
140 static inline u16
control_reg_get_pid(struct m66592
*m66592
, u16 pipenum
)
143 unsigned long offset
;
146 pid
= m66592_read(m66592
, M66592_DCPCTR
) & M66592_PID
;
147 else if (pipenum
< M66592_MAX_NUM_PIPE
) {
148 offset
= get_pipectr_addr(pipenum
);
149 pid
= m66592_read(m66592
, offset
) & M66592_PID
;
151 pr_err("unexpect pipe num (%d)\n", pipenum
);
156 static inline void control_reg_set_pid(struct m66592
*m66592
, u16 pipenum
,
159 unsigned long offset
;
162 m66592_mdfy(m66592
, pid
, M66592_PID
, M66592_DCPCTR
);
163 else if (pipenum
< M66592_MAX_NUM_PIPE
) {
164 offset
= get_pipectr_addr(pipenum
);
165 m66592_mdfy(m66592
, pid
, M66592_PID
, offset
);
167 pr_err("unexpect pipe num (%d)\n", pipenum
);
170 static inline void pipe_start(struct m66592
*m66592
, u16 pipenum
)
172 control_reg_set_pid(m66592
, pipenum
, M66592_PID_BUF
);
175 static inline void pipe_stop(struct m66592
*m66592
, u16 pipenum
)
177 control_reg_set_pid(m66592
, pipenum
, M66592_PID_NAK
);
180 static inline void pipe_stall(struct m66592
*m66592
, u16 pipenum
)
182 control_reg_set_pid(m66592
, pipenum
, M66592_PID_STALL
);
185 static inline u16
control_reg_get(struct m66592
*m66592
, u16 pipenum
)
188 unsigned long offset
;
191 ret
= m66592_read(m66592
, M66592_DCPCTR
);
192 else if (pipenum
< M66592_MAX_NUM_PIPE
) {
193 offset
= get_pipectr_addr(pipenum
);
194 ret
= m66592_read(m66592
, offset
);
196 pr_err("unexpect pipe num (%d)\n", pipenum
);
201 static inline void control_reg_sqclr(struct m66592
*m66592
, u16 pipenum
)
203 unsigned long offset
;
205 pipe_stop(m66592
, pipenum
);
208 m66592_bset(m66592
, M66592_SQCLR
, M66592_DCPCTR
);
209 else if (pipenum
< M66592_MAX_NUM_PIPE
) {
210 offset
= get_pipectr_addr(pipenum
);
211 m66592_bset(m66592
, M66592_SQCLR
, offset
);
213 pr_err("unexpect pipe num(%d)\n", pipenum
);
216 static inline int get_buffer_size(struct m66592
*m66592
, u16 pipenum
)
222 tmp
= m66592_read(m66592
, M66592_DCPCFG
);
223 if ((tmp
& M66592_CNTMD
) != 0)
226 tmp
= m66592_read(m66592
, M66592_DCPMAXP
);
227 size
= tmp
& M66592_MAXP
;
230 m66592_write(m66592
, pipenum
, M66592_PIPESEL
);
231 tmp
= m66592_read(m66592
, M66592_PIPECFG
);
232 if ((tmp
& M66592_CNTMD
) != 0) {
233 tmp
= m66592_read(m66592
, M66592_PIPEBUF
);
234 size
= ((tmp
>> 10) + 1) * 64;
236 tmp
= m66592_read(m66592
, M66592_PIPEMAXP
);
237 size
= tmp
& M66592_MXPS
;
244 static inline void pipe_change(struct m66592
*m66592
, u16 pipenum
)
246 struct m66592_ep
*ep
= m66592
->pipenum2ep
[pipenum
];
251 m66592_mdfy(m66592
, pipenum
, M66592_CURPIPE
, ep
->fifosel
);
255 m66592_bset(m66592
, M66592_MBW
, ep
->fifosel
);
258 static int pipe_buffer_setting(struct m66592
*m66592
,
259 struct m66592_pipe_info
*info
)
261 u16 bufnum
= 0, buf_bsize
= 0;
267 m66592_write(m66592
, info
->pipe
, M66592_PIPESEL
);
270 pipecfg
|= M66592_DIR
;
271 pipecfg
|= info
->type
;
272 pipecfg
|= info
->epnum
;
273 switch (info
->type
) {
275 bufnum
= 4 + (info
->pipe
- M66592_BASE_PIPENUM_INT
);
279 bufnum
= m66592
->bi_bufnum
+
280 (info
->pipe
- M66592_BASE_PIPENUM_BULK
) * 16;
281 m66592
->bi_bufnum
+= 16;
283 pipecfg
|= M66592_DBLB
;
285 pipecfg
|= M66592_SHTNAK
;
288 bufnum
= m66592
->bi_bufnum
+
289 (info
->pipe
- M66592_BASE_PIPENUM_ISOC
) * 16;
290 m66592
->bi_bufnum
+= 16;
294 if (m66592
->bi_bufnum
> M66592_MAX_BUFNUM
) {
295 pr_err("m66592 pipe memory is insufficient(%d)\n",
300 m66592_write(m66592
, pipecfg
, M66592_PIPECFG
);
301 m66592_write(m66592
, (buf_bsize
<< 10) | (bufnum
), M66592_PIPEBUF
);
302 m66592_write(m66592
, info
->maxpacket
, M66592_PIPEMAXP
);
305 m66592_write(m66592
, info
->interval
, M66592_PIPEPERI
);
310 static void pipe_buffer_release(struct m66592
*m66592
,
311 struct m66592_pipe_info
*info
)
316 switch (info
->type
) {
318 if (is_bulk_pipe(info
->pipe
))
319 m66592
->bi_bufnum
-= 16;
322 if (is_isoc_pipe(info
->pipe
))
323 m66592
->bi_bufnum
-= 16;
327 if (is_bulk_pipe(info
->pipe
)) {
329 } else if (is_interrupt_pipe(info
->pipe
))
331 else if (is_isoc_pipe(info
->pipe
)) {
332 m66592
->isochronous
--;
333 if (info
->type
== M66592_BULK
)
336 pr_err("ep_release: unexpect pipenum (%d)\n",
340 static void pipe_initialize(struct m66592_ep
*ep
)
342 struct m66592
*m66592
= ep
->m66592
;
344 m66592_mdfy(m66592
, 0, M66592_CURPIPE
, ep
->fifosel
);
346 m66592_write(m66592
, M66592_ACLRM
, ep
->pipectr
);
347 m66592_write(m66592
, 0, ep
->pipectr
);
348 m66592_write(m66592
, M66592_SQCLR
, ep
->pipectr
);
350 m66592_mdfy(m66592
, ep
->pipenum
, M66592_CURPIPE
, ep
->fifosel
);
354 m66592_bset(m66592
, M66592_MBW
, ep
->fifosel
);
358 static void m66592_ep_setting(struct m66592
*m66592
, struct m66592_ep
*ep
,
359 const struct usb_endpoint_descriptor
*desc
,
360 u16 pipenum
, int dma
)
362 if ((pipenum
!= 0) && dma
) {
363 if (m66592
->num_dma
== 0) {
366 ep
->fifoaddr
= M66592_D0FIFO
;
367 ep
->fifosel
= M66592_D0FIFOSEL
;
368 ep
->fifoctr
= M66592_D0FIFOCTR
;
369 ep
->fifotrn
= M66592_D0FIFOTRN
;
370 #if !defined(CONFIG_SUPERH_BUILT_IN_M66592)
371 } else if (m66592
->num_dma
== 1) {
374 ep
->fifoaddr
= M66592_D1FIFO
;
375 ep
->fifosel
= M66592_D1FIFOSEL
;
376 ep
->fifoctr
= M66592_D1FIFOCTR
;
377 ep
->fifotrn
= M66592_D1FIFOTRN
;
381 ep
->fifoaddr
= M66592_CFIFO
;
382 ep
->fifosel
= M66592_CFIFOSEL
;
383 ep
->fifoctr
= M66592_CFIFOCTR
;
388 ep
->fifoaddr
= M66592_CFIFO
;
389 ep
->fifosel
= M66592_CFIFOSEL
;
390 ep
->fifoctr
= M66592_CFIFOCTR
;
394 ep
->pipectr
= get_pipectr_addr(pipenum
);
395 ep
->pipenum
= pipenum
;
396 ep
->ep
.maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
);
397 m66592
->pipenum2ep
[pipenum
] = ep
;
398 m66592
->epaddr2ep
[desc
->bEndpointAddress
&USB_ENDPOINT_NUMBER_MASK
] = ep
;
399 INIT_LIST_HEAD(&ep
->queue
);
402 static void m66592_ep_release(struct m66592_ep
*ep
)
404 struct m66592
*m66592
= ep
->m66592
;
405 u16 pipenum
= ep
->pipenum
;
417 static int alloc_pipe_config(struct m66592_ep
*ep
,
418 const struct usb_endpoint_descriptor
*desc
)
420 struct m66592
*m66592
= ep
->m66592
;
421 struct m66592_pipe_info info
;
430 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
431 case USB_ENDPOINT_XFER_BULK
:
432 if (m66592
->bulk
>= M66592_MAX_NUM_BULK
) {
433 if (m66592
->isochronous
>= M66592_MAX_NUM_ISOC
) {
434 pr_err("bulk pipe is insufficient\n");
437 info
.pipe
= M66592_BASE_PIPENUM_ISOC
438 + m66592
->isochronous
;
439 counter
= &m66592
->isochronous
;
442 info
.pipe
= M66592_BASE_PIPENUM_BULK
+ m66592
->bulk
;
443 counter
= &m66592
->bulk
;
445 info
.type
= M66592_BULK
;
448 case USB_ENDPOINT_XFER_INT
:
449 if (m66592
->interrupt
>= M66592_MAX_NUM_INT
) {
450 pr_err("interrupt pipe is insufficient\n");
453 info
.pipe
= M66592_BASE_PIPENUM_INT
+ m66592
->interrupt
;
454 info
.type
= M66592_INT
;
455 counter
= &m66592
->interrupt
;
457 case USB_ENDPOINT_XFER_ISOC
:
458 if (m66592
->isochronous
>= M66592_MAX_NUM_ISOC
) {
459 pr_err("isochronous pipe is insufficient\n");
462 info
.pipe
= M66592_BASE_PIPENUM_ISOC
+ m66592
->isochronous
;
463 info
.type
= M66592_ISO
;
464 counter
= &m66592
->isochronous
;
467 pr_err("unexpect xfer type\n");
470 ep
->type
= info
.type
;
472 info
.epnum
= desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
473 info
.maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
);
474 info
.interval
= desc
->bInterval
;
475 if (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
480 ret
= pipe_buffer_setting(m66592
, &info
);
482 pr_err("pipe_buffer_setting fail\n");
487 if ((counter
== &m66592
->isochronous
) && info
.type
== M66592_BULK
)
490 m66592_ep_setting(m66592
, ep
, desc
, info
.pipe
, dma
);
496 static int free_pipe_config(struct m66592_ep
*ep
)
498 struct m66592
*m66592
= ep
->m66592
;
499 struct m66592_pipe_info info
;
501 info
.pipe
= ep
->pipenum
;
502 info
.type
= ep
->type
;
503 pipe_buffer_release(m66592
, &info
);
504 m66592_ep_release(ep
);
509 /*-------------------------------------------------------------------------*/
510 static void pipe_irq_enable(struct m66592
*m66592
, u16 pipenum
)
512 enable_irq_ready(m66592
, pipenum
);
513 enable_irq_nrdy(m66592
, pipenum
);
516 static void pipe_irq_disable(struct m66592
*m66592
, u16 pipenum
)
518 disable_irq_ready(m66592
, pipenum
);
519 disable_irq_nrdy(m66592
, pipenum
);
522 /* if complete is true, gadget driver complete function is not call */
523 static void control_end(struct m66592
*m66592
, unsigned ccpl
)
525 m66592
->ep
[0].internal_ccpl
= ccpl
;
526 pipe_start(m66592
, 0);
527 m66592_bset(m66592
, M66592_CCPL
, M66592_DCPCTR
);
530 static void start_ep0_write(struct m66592_ep
*ep
, struct m66592_request
*req
)
532 struct m66592
*m66592
= ep
->m66592
;
534 pipe_change(m66592
, ep
->pipenum
);
535 m66592_mdfy(m66592
, M66592_ISEL
| M66592_PIPE0
,
536 (M66592_ISEL
| M66592_CURPIPE
),
538 m66592_write(m66592
, M66592_BCLR
, ep
->fifoctr
);
539 if (req
->req
.length
== 0) {
540 m66592_bset(m66592
, M66592_BVAL
, ep
->fifoctr
);
541 pipe_start(m66592
, 0);
542 transfer_complete(ep
, req
, 0);
544 m66592_write(m66592
, ~M66592_BEMP0
, M66592_BEMPSTS
);
545 irq_ep0_write(ep
, req
);
549 static void start_packet_write(struct m66592_ep
*ep
, struct m66592_request
*req
)
551 struct m66592
*m66592
= ep
->m66592
;
554 pipe_change(m66592
, ep
->pipenum
);
555 disable_irq_empty(m66592
, ep
->pipenum
);
556 pipe_start(m66592
, ep
->pipenum
);
558 tmp
= m66592_read(m66592
, ep
->fifoctr
);
559 if (unlikely((tmp
& M66592_FRDY
) == 0))
560 pipe_irq_enable(m66592
, ep
->pipenum
);
562 irq_packet_write(ep
, req
);
565 static void start_packet_read(struct m66592_ep
*ep
, struct m66592_request
*req
)
567 struct m66592
*m66592
= ep
->m66592
;
568 u16 pipenum
= ep
->pipenum
;
570 if (ep
->pipenum
== 0) {
571 m66592_mdfy(m66592
, M66592_PIPE0
,
572 (M66592_ISEL
| M66592_CURPIPE
),
574 m66592_write(m66592
, M66592_BCLR
, ep
->fifoctr
);
575 pipe_start(m66592
, pipenum
);
576 pipe_irq_enable(m66592
, pipenum
);
579 m66592_bset(m66592
, M66592_TRCLR
, ep
->fifosel
);
580 pipe_change(m66592
, pipenum
);
581 m66592_bset(m66592
, M66592_TRENB
, ep
->fifosel
);
583 (req
->req
.length
+ ep
->ep
.maxpacket
- 1)
587 pipe_start(m66592
, pipenum
); /* trigger once */
588 pipe_irq_enable(m66592
, pipenum
);
592 static void start_packet(struct m66592_ep
*ep
, struct m66592_request
*req
)
594 if (ep
->desc
->bEndpointAddress
& USB_DIR_IN
)
595 start_packet_write(ep
, req
);
597 start_packet_read(ep
, req
);
600 static void start_ep0(struct m66592_ep
*ep
, struct m66592_request
*req
)
604 ctsq
= m66592_read(ep
->m66592
, M66592_INTSTS0
) & M66592_CTSQ
;
608 start_ep0_write(ep
, req
);
611 start_packet_read(ep
, req
);
615 control_end(ep
->m66592
, 0);
618 pr_err("start_ep0: unexpect ctsq(%x)\n", ctsq
);
623 #if defined(CONFIG_SUPERH_BUILT_IN_M66592)
624 static void init_controller(struct m66592
*m66592
)
626 m66592_bset(m66592
, M66592_HSE
, M66592_SYSCFG
); /* High spd */
627 m66592_bclr(m66592
, M66592_USBE
, M66592_SYSCFG
);
628 m66592_bclr(m66592
, M66592_DPRPU
, M66592_SYSCFG
);
629 m66592_bset(m66592
, M66592_USBE
, M66592_SYSCFG
);
631 /* This is a workaound for SH7722 2nd cut */
632 m66592_bset(m66592
, 0x8000, M66592_DVSTCTR
);
633 m66592_bset(m66592
, 0x1000, M66592_TESTMODE
);
634 m66592_bclr(m66592
, 0x8000, M66592_DVSTCTR
);
636 m66592_bset(m66592
, M66592_INTL
, M66592_INTENB1
);
638 m66592_write(m66592
, 0, M66592_CFBCFG
);
639 m66592_write(m66592
, 0, M66592_D0FBCFG
);
640 m66592_bset(m66592
, endian
, M66592_CFBCFG
);
641 m66592_bset(m66592
, endian
, M66592_D0FBCFG
);
643 #else /* #if defined(CONFIG_SUPERH_BUILT_IN_M66592) */
644 static void init_controller(struct m66592
*m66592
)
646 m66592_bset(m66592
, (vif
& M66592_LDRV
) | (endian
& M66592_BIGEND
),
648 m66592_bset(m66592
, M66592_HSE
, M66592_SYSCFG
); /* High spd */
649 m66592_mdfy(m66592
, clock
& M66592_XTAL
, M66592_XTAL
, M66592_SYSCFG
);
651 m66592_bclr(m66592
, M66592_USBE
, M66592_SYSCFG
);
652 m66592_bclr(m66592
, M66592_DPRPU
, M66592_SYSCFG
);
653 m66592_bset(m66592
, M66592_USBE
, M66592_SYSCFG
);
655 m66592_bset(m66592
, M66592_XCKE
, M66592_SYSCFG
);
659 m66592_bset(m66592
, M66592_RCKE
| M66592_PLLC
, M66592_SYSCFG
);
663 m66592_bset(m66592
, M66592_SCKE
, M66592_SYSCFG
);
665 m66592_bset(m66592
, irq_sense
& M66592_INTL
, M66592_INTENB1
);
666 m66592_write(m66592
, M66592_BURST
| M66592_CPU_ADR_RD_WR
,
669 #endif /* #if defined(CONFIG_SUPERH_BUILT_IN_M66592) */
671 static void disable_controller(struct m66592
*m66592
)
673 #if !defined(CONFIG_SUPERH_BUILT_IN_M66592)
674 m66592_bclr(m66592
, M66592_SCKE
, M66592_SYSCFG
);
676 m66592_bclr(m66592
, M66592_PLLC
, M66592_SYSCFG
);
678 m66592_bclr(m66592
, M66592_RCKE
, M66592_SYSCFG
);
680 m66592_bclr(m66592
, M66592_XCKE
, M66592_SYSCFG
);
684 static void m66592_start_xclock(struct m66592
*m66592
)
686 #if !defined(CONFIG_SUPERH_BUILT_IN_M66592)
689 tmp
= m66592_read(m66592
, M66592_SYSCFG
);
690 if (!(tmp
& M66592_XCKE
))
691 m66592_bset(m66592
, M66592_XCKE
, M66592_SYSCFG
);
695 /*-------------------------------------------------------------------------*/
696 static void transfer_complete(struct m66592_ep
*ep
,
697 struct m66592_request
*req
, int status
)
698 __releases(m66592
->lock
)
699 __acquires(m66592
->lock
)
703 if (unlikely(ep
->pipenum
== 0)) {
704 if (ep
->internal_ccpl
) {
705 ep
->internal_ccpl
= 0;
710 list_del_init(&req
->queue
);
711 if (ep
->m66592
->gadget
.speed
== USB_SPEED_UNKNOWN
)
712 req
->req
.status
= -ESHUTDOWN
;
714 req
->req
.status
= status
;
716 if (!list_empty(&ep
->queue
))
719 spin_unlock(&ep
->m66592
->lock
);
720 req
->req
.complete(&ep
->ep
, &req
->req
);
721 spin_lock(&ep
->m66592
->lock
);
724 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
726 start_packet(ep
, req
);
730 static void irq_ep0_write(struct m66592_ep
*ep
, struct m66592_request
*req
)
737 u16 pipenum
= ep
->pipenum
;
738 struct m66592
*m66592
= ep
->m66592
;
740 pipe_change(m66592
, pipenum
);
741 m66592_bset(m66592
, M66592_ISEL
, ep
->fifosel
);
745 tmp
= m66592_read(m66592
, ep
->fifoctr
);
747 pr_err("pipe0 is busy. maybe cpu i/o bus "
748 "conflict. please power off this controller.");
752 } while ((tmp
& M66592_FRDY
) == 0);
754 /* prepare parameters */
755 bufsize
= get_buffer_size(m66592
, pipenum
);
756 buf
= req
->req
.buf
+ req
->req
.actual
;
757 size
= min(bufsize
, req
->req
.length
- req
->req
.actual
);
762 m66592_write_fifo(m66592
, ep
->fifoaddr
, buf
, size
);
763 if ((size
== 0) || ((size
% ep
->ep
.maxpacket
) != 0))
764 m66592_bset(m66592
, M66592_BVAL
, ep
->fifoctr
);
767 /* update parameters */
768 req
->req
.actual
+= size
;
770 /* check transfer finish */
771 if ((!req
->req
.zero
&& (req
->req
.actual
== req
->req
.length
))
772 || (size
% ep
->ep
.maxpacket
)
774 disable_irq_ready(m66592
, pipenum
);
775 disable_irq_empty(m66592
, pipenum
);
777 disable_irq_ready(m66592
, pipenum
);
778 enable_irq_empty(m66592
, pipenum
);
780 pipe_start(m66592
, pipenum
);
783 static void irq_packet_write(struct m66592_ep
*ep
, struct m66592_request
*req
)
789 u16 pipenum
= ep
->pipenum
;
790 struct m66592
*m66592
= ep
->m66592
;
792 pipe_change(m66592
, pipenum
);
793 tmp
= m66592_read(m66592
, ep
->fifoctr
);
794 if (unlikely((tmp
& M66592_FRDY
) == 0)) {
795 pipe_stop(m66592
, pipenum
);
796 pipe_irq_disable(m66592
, pipenum
);
797 pr_err("write fifo not ready. pipnum=%d\n", pipenum
);
801 /* prepare parameters */
802 bufsize
= get_buffer_size(m66592
, pipenum
);
803 buf
= req
->req
.buf
+ req
->req
.actual
;
804 size
= min(bufsize
, req
->req
.length
- req
->req
.actual
);
808 m66592_write_fifo(m66592
, ep
->fifoaddr
, buf
, size
);
810 || ((size
% ep
->ep
.maxpacket
) != 0)
811 || ((bufsize
!= ep
->ep
.maxpacket
)
812 && (bufsize
> size
)))
813 m66592_bset(m66592
, M66592_BVAL
, ep
->fifoctr
);
816 /* update parameters */
817 req
->req
.actual
+= size
;
818 /* check transfer finish */
819 if ((!req
->req
.zero
&& (req
->req
.actual
== req
->req
.length
))
820 || (size
% ep
->ep
.maxpacket
)
822 disable_irq_ready(m66592
, pipenum
);
823 enable_irq_empty(m66592
, pipenum
);
825 disable_irq_empty(m66592
, pipenum
);
826 pipe_irq_enable(m66592
, pipenum
);
830 static void irq_packet_read(struct m66592_ep
*ep
, struct m66592_request
*req
)
833 int rcv_len
, bufsize
, req_len
;
836 u16 pipenum
= ep
->pipenum
;
837 struct m66592
*m66592
= ep
->m66592
;
840 pipe_change(m66592
, pipenum
);
841 tmp
= m66592_read(m66592
, ep
->fifoctr
);
842 if (unlikely((tmp
& M66592_FRDY
) == 0)) {
843 req
->req
.status
= -EPIPE
;
844 pipe_stop(m66592
, pipenum
);
845 pipe_irq_disable(m66592
, pipenum
);
846 pr_err("read fifo not ready");
850 /* prepare parameters */
851 rcv_len
= tmp
& M66592_DTLN
;
852 bufsize
= get_buffer_size(m66592
, pipenum
);
854 buf
= req
->req
.buf
+ req
->req
.actual
;
855 req_len
= req
->req
.length
- req
->req
.actual
;
856 if (rcv_len
< bufsize
)
857 size
= min(rcv_len
, req_len
);
859 size
= min(bufsize
, req_len
);
861 /* update parameters */
862 req
->req
.actual
+= size
;
864 /* check transfer finish */
865 if ((!req
->req
.zero
&& (req
->req
.actual
== req
->req
.length
))
866 || (size
% ep
->ep
.maxpacket
)
868 pipe_stop(m66592
, pipenum
);
869 pipe_irq_disable(m66592
, pipenum
);
876 m66592_write(m66592
, M66592_BCLR
, ep
->fifoctr
);
878 m66592_read_fifo(m66592
, ep
->fifoaddr
, buf
, size
);
881 if ((ep
->pipenum
!= 0) && finish
)
882 transfer_complete(ep
, req
, 0);
885 static void irq_pipe_ready(struct m66592
*m66592
, u16 status
, u16 enb
)
889 struct m66592_ep
*ep
;
890 struct m66592_request
*req
;
892 if ((status
& M66592_BRDY0
) && (enb
& M66592_BRDY0
)) {
893 m66592_write(m66592
, ~M66592_BRDY0
, M66592_BRDYSTS
);
894 m66592_mdfy(m66592
, M66592_PIPE0
, M66592_CURPIPE
,
898 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
899 irq_packet_read(ep
, req
);
901 for (pipenum
= 1; pipenum
< M66592_MAX_NUM_PIPE
; pipenum
++) {
902 check
= 1 << pipenum
;
903 if ((status
& check
) && (enb
& check
)) {
904 m66592_write(m66592
, ~check
, M66592_BRDYSTS
);
905 ep
= m66592
->pipenum2ep
[pipenum
];
906 req
= list_entry(ep
->queue
.next
,
907 struct m66592_request
, queue
);
908 if (ep
->desc
->bEndpointAddress
& USB_DIR_IN
)
909 irq_packet_write(ep
, req
);
911 irq_packet_read(ep
, req
);
917 static void irq_pipe_empty(struct m66592
*m66592
, u16 status
, u16 enb
)
922 struct m66592_ep
*ep
;
923 struct m66592_request
*req
;
925 if ((status
& M66592_BEMP0
) && (enb
& M66592_BEMP0
)) {
926 m66592_write(m66592
, ~M66592_BEMP0
, M66592_BEMPSTS
);
929 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
930 irq_ep0_write(ep
, req
);
932 for (pipenum
= 1; pipenum
< M66592_MAX_NUM_PIPE
; pipenum
++) {
933 check
= 1 << pipenum
;
934 if ((status
& check
) && (enb
& check
)) {
935 m66592_write(m66592
, ~check
, M66592_BEMPSTS
);
936 tmp
= control_reg_get(m66592
, pipenum
);
937 if ((tmp
& M66592_INBUFM
) == 0) {
938 disable_irq_empty(m66592
, pipenum
);
939 pipe_irq_disable(m66592
, pipenum
);
940 pipe_stop(m66592
, pipenum
);
941 ep
= m66592
->pipenum2ep
[pipenum
];
942 req
= list_entry(ep
->queue
.next
,
943 struct m66592_request
,
945 if (!list_empty(&ep
->queue
))
946 transfer_complete(ep
, req
, 0);
953 static void get_status(struct m66592
*m66592
, struct usb_ctrlrequest
*ctrl
)
954 __releases(m66592
->lock
)
955 __acquires(m66592
->lock
)
957 struct m66592_ep
*ep
;
960 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
962 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
963 case USB_RECIP_DEVICE
:
964 status
= 1 << USB_DEVICE_SELF_POWERED
;
966 case USB_RECIP_INTERFACE
:
969 case USB_RECIP_ENDPOINT
:
970 ep
= m66592
->epaddr2ep
[w_index
& USB_ENDPOINT_NUMBER_MASK
];
971 pid
= control_reg_get_pid(m66592
, ep
->pipenum
);
972 if (pid
== M66592_PID_STALL
)
973 status
= 1 << USB_ENDPOINT_HALT
;
978 pipe_stall(m66592
, 0);
982 m66592
->ep0_data
= cpu_to_le16(status
);
983 m66592
->ep0_req
->buf
= &m66592
->ep0_data
;
984 m66592
->ep0_req
->length
= 2;
985 /* AV: what happens if we get called again before that gets through? */
986 spin_unlock(&m66592
->lock
);
987 m66592_queue(m66592
->gadget
.ep0
, m66592
->ep0_req
, GFP_KERNEL
);
988 spin_lock(&m66592
->lock
);
991 static void clear_feature(struct m66592
*m66592
, struct usb_ctrlrequest
*ctrl
)
993 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
994 case USB_RECIP_DEVICE
:
995 control_end(m66592
, 1);
997 case USB_RECIP_INTERFACE
:
998 control_end(m66592
, 1);
1000 case USB_RECIP_ENDPOINT
: {
1001 struct m66592_ep
*ep
;
1002 struct m66592_request
*req
;
1003 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1005 ep
= m66592
->epaddr2ep
[w_index
& USB_ENDPOINT_NUMBER_MASK
];
1006 pipe_stop(m66592
, ep
->pipenum
);
1007 control_reg_sqclr(m66592
, ep
->pipenum
);
1009 control_end(m66592
, 1);
1011 req
= list_entry(ep
->queue
.next
,
1012 struct m66592_request
, queue
);
1015 if (list_empty(&ep
->queue
))
1017 start_packet(ep
, req
);
1018 } else if (!list_empty(&ep
->queue
))
1019 pipe_start(m66592
, ep
->pipenum
);
1023 pipe_stall(m66592
, 0);
1028 static void set_feature(struct m66592
*m66592
, struct usb_ctrlrequest
*ctrl
)
1031 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1032 case USB_RECIP_DEVICE
:
1033 control_end(m66592
, 1);
1035 case USB_RECIP_INTERFACE
:
1036 control_end(m66592
, 1);
1038 case USB_RECIP_ENDPOINT
: {
1039 struct m66592_ep
*ep
;
1040 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1042 ep
= m66592
->epaddr2ep
[w_index
& USB_ENDPOINT_NUMBER_MASK
];
1043 pipe_stall(m66592
, ep
->pipenum
);
1045 control_end(m66592
, 1);
1049 pipe_stall(m66592
, 0);
1054 /* if return value is true, call class driver's setup() */
1055 static int setup_packet(struct m66592
*m66592
, struct usb_ctrlrequest
*ctrl
)
1057 u16
*p
= (u16
*)ctrl
;
1058 unsigned long offset
= M66592_USBREQ
;
1062 m66592_write(m66592
, ~M66592_VALID
, M66592_INTSTS0
);
1064 for (i
= 0; i
< 4; i
++)
1065 p
[i
] = m66592_read(m66592
, offset
+ i
*2);
1068 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1069 switch (ctrl
->bRequest
) {
1070 case USB_REQ_GET_STATUS
:
1071 get_status(m66592
, ctrl
);
1073 case USB_REQ_CLEAR_FEATURE
:
1074 clear_feature(m66592
, ctrl
);
1076 case USB_REQ_SET_FEATURE
:
1077 set_feature(m66592
, ctrl
);
1088 static void m66592_update_usb_speed(struct m66592
*m66592
)
1090 u16 speed
= get_usb_speed(m66592
);
1094 m66592
->gadget
.speed
= USB_SPEED_HIGH
;
1097 m66592
->gadget
.speed
= USB_SPEED_FULL
;
1100 m66592
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1101 pr_err("USB speed unknown\n");
1105 static void irq_device_state(struct m66592
*m66592
)
1109 dvsq
= m66592_read(m66592
, M66592_INTSTS0
) & M66592_DVSQ
;
1110 m66592_write(m66592
, ~M66592_DVST
, M66592_INTSTS0
);
1112 if (dvsq
== M66592_DS_DFLT
) { /* bus reset */
1113 m66592
->driver
->disconnect(&m66592
->gadget
);
1114 m66592_update_usb_speed(m66592
);
1116 if (m66592
->old_dvsq
== M66592_DS_CNFG
&& dvsq
!= M66592_DS_CNFG
)
1117 m66592_update_usb_speed(m66592
);
1118 if ((dvsq
== M66592_DS_CNFG
|| dvsq
== M66592_DS_ADDS
)
1119 && m66592
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1120 m66592_update_usb_speed(m66592
);
1122 m66592
->old_dvsq
= dvsq
;
1125 static void irq_control_stage(struct m66592
*m66592
)
1126 __releases(m66592
->lock
)
1127 __acquires(m66592
->lock
)
1129 struct usb_ctrlrequest ctrl
;
1132 ctsq
= m66592_read(m66592
, M66592_INTSTS0
) & M66592_CTSQ
;
1133 m66592_write(m66592
, ~M66592_CTRT
, M66592_INTSTS0
);
1136 case M66592_CS_IDST
: {
1137 struct m66592_ep
*ep
;
1138 struct m66592_request
*req
;
1139 ep
= &m66592
->ep
[0];
1140 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
1141 transfer_complete(ep
, req
, 0);
1145 case M66592_CS_RDDS
:
1146 case M66592_CS_WRDS
:
1147 case M66592_CS_WRND
:
1148 if (setup_packet(m66592
, &ctrl
)) {
1149 spin_unlock(&m66592
->lock
);
1150 if (m66592
->driver
->setup(&m66592
->gadget
, &ctrl
) < 0)
1151 pipe_stall(m66592
, 0);
1152 spin_lock(&m66592
->lock
);
1155 case M66592_CS_RDSS
:
1156 case M66592_CS_WRSS
:
1157 control_end(m66592
, 0);
1160 pr_err("ctrl_stage: unexpect ctsq(%x)\n", ctsq
);
1165 static irqreturn_t
m66592_irq(int irq
, void *_m66592
)
1167 struct m66592
*m66592
= _m66592
;
1170 u16 brdysts
, nrdysts
, bempsts
;
1171 u16 brdyenb
, nrdyenb
, bempenb
;
1175 spin_lock(&m66592
->lock
);
1177 intsts0
= m66592_read(m66592
, M66592_INTSTS0
);
1178 intenb0
= m66592_read(m66592
, M66592_INTENB0
);
1180 #if defined(CONFIG_SUPERH_BUILT_IN_M66592)
1181 if (!intsts0
&& !intenb0
) {
1183 * When USB clock stops, it cannot read register. Even if a
1184 * clock stops, the interrupt occurs. So this driver turn on
1185 * a clock by this timing and do re-reading of register.
1187 m66592_start_xclock(m66592
);
1188 intsts0
= m66592_read(m66592
, M66592_INTSTS0
);
1189 intenb0
= m66592_read(m66592
, M66592_INTENB0
);
1193 savepipe
= m66592_read(m66592
, M66592_CFIFOSEL
);
1195 mask0
= intsts0
& intenb0
;
1197 brdysts
= m66592_read(m66592
, M66592_BRDYSTS
);
1198 nrdysts
= m66592_read(m66592
, M66592_NRDYSTS
);
1199 bempsts
= m66592_read(m66592
, M66592_BEMPSTS
);
1200 brdyenb
= m66592_read(m66592
, M66592_BRDYENB
);
1201 nrdyenb
= m66592_read(m66592
, M66592_NRDYENB
);
1202 bempenb
= m66592_read(m66592
, M66592_BEMPENB
);
1204 if (mask0
& M66592_VBINT
) {
1205 m66592_write(m66592
, 0xffff & ~M66592_VBINT
,
1207 m66592_start_xclock(m66592
);
1209 /* start vbus sampling */
1210 m66592
->old_vbus
= m66592_read(m66592
, M66592_INTSTS0
)
1212 m66592
->scount
= M66592_MAX_SAMPLING
;
1214 mod_timer(&m66592
->timer
,
1215 jiffies
+ msecs_to_jiffies(50));
1217 if (intsts0
& M66592_DVSQ
)
1218 irq_device_state(m66592
);
1220 if ((intsts0
& M66592_BRDY
) && (intenb0
& M66592_BRDYE
)
1221 && (brdysts
& brdyenb
)) {
1222 irq_pipe_ready(m66592
, brdysts
, brdyenb
);
1224 if ((intsts0
& M66592_BEMP
) && (intenb0
& M66592_BEMPE
)
1225 && (bempsts
& bempenb
)) {
1226 irq_pipe_empty(m66592
, bempsts
, bempenb
);
1229 if (intsts0
& M66592_CTRT
)
1230 irq_control_stage(m66592
);
1233 m66592_write(m66592
, savepipe
, M66592_CFIFOSEL
);
1235 spin_unlock(&m66592
->lock
);
1239 static void m66592_timer(unsigned long _m66592
)
1241 struct m66592
*m66592
= (struct m66592
*)_m66592
;
1242 unsigned long flags
;
1245 spin_lock_irqsave(&m66592
->lock
, flags
);
1246 tmp
= m66592_read(m66592
, M66592_SYSCFG
);
1247 if (!(tmp
& M66592_RCKE
)) {
1248 m66592_bset(m66592
, M66592_RCKE
| M66592_PLLC
, M66592_SYSCFG
);
1250 m66592_bset(m66592
, M66592_SCKE
, M66592_SYSCFG
);
1252 if (m66592
->scount
> 0) {
1253 tmp
= m66592_read(m66592
, M66592_INTSTS0
) & M66592_VBSTS
;
1254 if (tmp
== m66592
->old_vbus
) {
1256 if (m66592
->scount
== 0) {
1257 if (tmp
== M66592_VBSTS
)
1258 m66592_usb_connect(m66592
);
1260 m66592_usb_disconnect(m66592
);
1262 mod_timer(&m66592
->timer
,
1263 jiffies
+ msecs_to_jiffies(50));
1266 m66592
->scount
= M66592_MAX_SAMPLING
;
1267 m66592
->old_vbus
= tmp
;
1268 mod_timer(&m66592
->timer
,
1269 jiffies
+ msecs_to_jiffies(50));
1272 spin_unlock_irqrestore(&m66592
->lock
, flags
);
1275 /*-------------------------------------------------------------------------*/
1276 static int m66592_enable(struct usb_ep
*_ep
,
1277 const struct usb_endpoint_descriptor
*desc
)
1279 struct m66592_ep
*ep
;
1281 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1282 return alloc_pipe_config(ep
, desc
);
1285 static int m66592_disable(struct usb_ep
*_ep
)
1287 struct m66592_ep
*ep
;
1288 struct m66592_request
*req
;
1289 unsigned long flags
;
1291 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1294 while (!list_empty(&ep
->queue
)) {
1295 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
1296 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1297 transfer_complete(ep
, req
, -ECONNRESET
);
1298 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1301 pipe_irq_disable(ep
->m66592
, ep
->pipenum
);
1302 return free_pipe_config(ep
);
1305 static struct usb_request
*m66592_alloc_request(struct usb_ep
*_ep
,
1308 struct m66592_request
*req
;
1310 req
= kzalloc(sizeof(struct m66592_request
), gfp_flags
);
1314 INIT_LIST_HEAD(&req
->queue
);
1319 static void m66592_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1321 struct m66592_request
*req
;
1323 req
= container_of(_req
, struct m66592_request
, req
);
1327 static int m66592_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1330 struct m66592_ep
*ep
;
1331 struct m66592_request
*req
;
1332 unsigned long flags
;
1335 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1336 req
= container_of(_req
, struct m66592_request
, req
);
1338 if (ep
->m66592
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1341 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1343 if (list_empty(&ep
->queue
))
1346 list_add_tail(&req
->queue
, &ep
->queue
);
1347 req
->req
.actual
= 0;
1348 req
->req
.status
= -EINPROGRESS
;
1350 if (ep
->desc
== NULL
) /* control */
1353 if (request
&& !ep
->busy
)
1354 start_packet(ep
, req
);
1357 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1362 static int m66592_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1364 struct m66592_ep
*ep
;
1365 struct m66592_request
*req
;
1366 unsigned long flags
;
1368 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1369 req
= container_of(_req
, struct m66592_request
, req
);
1371 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1372 if (!list_empty(&ep
->queue
))
1373 transfer_complete(ep
, req
, -ECONNRESET
);
1374 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1379 static int m66592_set_halt(struct usb_ep
*_ep
, int value
)
1381 struct m66592_ep
*ep
;
1382 struct m66592_request
*req
;
1383 unsigned long flags
;
1386 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1387 req
= list_entry(ep
->queue
.next
, struct m66592_request
, queue
);
1389 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1390 if (!list_empty(&ep
->queue
)) {
1396 pipe_stall(ep
->m66592
, ep
->pipenum
);
1399 pipe_stop(ep
->m66592
, ep
->pipenum
);
1403 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1407 static void m66592_fifo_flush(struct usb_ep
*_ep
)
1409 struct m66592_ep
*ep
;
1410 unsigned long flags
;
1412 ep
= container_of(_ep
, struct m66592_ep
, ep
);
1413 spin_lock_irqsave(&ep
->m66592
->lock
, flags
);
1414 if (list_empty(&ep
->queue
) && !ep
->busy
) {
1415 pipe_stop(ep
->m66592
, ep
->pipenum
);
1416 m66592_bclr(ep
->m66592
, M66592_BCLR
, ep
->fifoctr
);
1418 spin_unlock_irqrestore(&ep
->m66592
->lock
, flags
);
1421 static struct usb_ep_ops m66592_ep_ops
= {
1422 .enable
= m66592_enable
,
1423 .disable
= m66592_disable
,
1425 .alloc_request
= m66592_alloc_request
,
1426 .free_request
= m66592_free_request
,
1428 .queue
= m66592_queue
,
1429 .dequeue
= m66592_dequeue
,
1431 .set_halt
= m66592_set_halt
,
1432 .fifo_flush
= m66592_fifo_flush
,
1435 /*-------------------------------------------------------------------------*/
1436 static struct m66592
*the_controller
;
1438 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1440 struct m66592
*m66592
= the_controller
;
1444 || driver
->speed
!= USB_SPEED_HIGH
1453 /* hook up the driver */
1454 driver
->driver
.bus
= NULL
;
1455 m66592
->driver
= driver
;
1456 m66592
->gadget
.dev
.driver
= &driver
->driver
;
1458 retval
= device_add(&m66592
->gadget
.dev
);
1460 pr_err("device_add error (%d)\n", retval
);
1464 retval
= driver
->bind (&m66592
->gadget
);
1466 pr_err("bind to driver error (%d)\n", retval
);
1467 device_del(&m66592
->gadget
.dev
);
1471 m66592_bset(m66592
, M66592_VBSE
| M66592_URST
, M66592_INTENB0
);
1472 if (m66592_read(m66592
, M66592_INTSTS0
) & M66592_VBSTS
) {
1473 m66592_start_xclock(m66592
);
1474 /* start vbus sampling */
1475 m66592
->old_vbus
= m66592_read(m66592
,
1476 M66592_INTSTS0
) & M66592_VBSTS
;
1477 m66592
->scount
= M66592_MAX_SAMPLING
;
1478 mod_timer(&m66592
->timer
, jiffies
+ msecs_to_jiffies(50));
1484 m66592
->driver
= NULL
;
1485 m66592
->gadget
.dev
.driver
= NULL
;
1489 EXPORT_SYMBOL(usb_gadget_register_driver
);
1491 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1493 struct m66592
*m66592
= the_controller
;
1494 unsigned long flags
;
1496 if (driver
!= m66592
->driver
|| !driver
->unbind
)
1499 spin_lock_irqsave(&m66592
->lock
, flags
);
1500 if (m66592
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
1501 m66592_usb_disconnect(m66592
);
1502 spin_unlock_irqrestore(&m66592
->lock
, flags
);
1504 m66592_bclr(m66592
, M66592_VBSE
| M66592_URST
, M66592_INTENB0
);
1506 driver
->unbind(&m66592
->gadget
);
1507 m66592
->gadget
.dev
.driver
= NULL
;
1509 init_controller(m66592
);
1510 disable_controller(m66592
);
1512 device_del(&m66592
->gadget
.dev
);
1513 m66592
->driver
= NULL
;
1516 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1518 /*-------------------------------------------------------------------------*/
1519 static int m66592_get_frame(struct usb_gadget
*_gadget
)
1521 struct m66592
*m66592
= gadget_to_m66592(_gadget
);
1522 return m66592_read(m66592
, M66592_FRMNUM
) & 0x03FF;
1525 static struct usb_gadget_ops m66592_gadget_ops
= {
1526 .get_frame
= m66592_get_frame
,
1529 static int __exit
m66592_remove(struct platform_device
*pdev
)
1531 struct m66592
*m66592
= dev_get_drvdata(&pdev
->dev
);
1533 del_timer_sync(&m66592
->timer
);
1534 iounmap(m66592
->reg
);
1535 free_irq(platform_get_irq(pdev
, 0), m66592
);
1536 m66592_free_request(&m66592
->ep
[0].ep
, m66592
->ep0_req
);
1537 #if defined(CONFIG_SUPERH_BUILT_IN_M66592) && defined(CONFIG_HAVE_CLK)
1538 clk_disable(m66592
->clk
);
1539 clk_put(m66592
->clk
);
1545 static void nop_completion(struct usb_ep
*ep
, struct usb_request
*r
)
1549 static int __init
m66592_probe(struct platform_device
*pdev
)
1551 struct resource
*res
;
1553 void __iomem
*reg
= NULL
;
1554 struct m66592
*m66592
= NULL
;
1555 #if defined(CONFIG_SUPERH_BUILT_IN_M66592) && defined(CONFIG_HAVE_CLK)
1561 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1564 pr_err("platform_get_resource error.\n");
1568 irq
= platform_get_irq(pdev
, 0);
1571 pr_err("platform_get_irq error.\n");
1575 reg
= ioremap(res
->start
, resource_size(res
));
1578 pr_err("ioremap error.\n");
1582 /* initialize ucd */
1583 m66592
= kzalloc(sizeof(struct m66592
), GFP_KERNEL
);
1584 if (m66592
== NULL
) {
1585 pr_err("kzalloc error\n");
1589 spin_lock_init(&m66592
->lock
);
1590 dev_set_drvdata(&pdev
->dev
, m66592
);
1592 m66592
->gadget
.ops
= &m66592_gadget_ops
;
1593 device_initialize(&m66592
->gadget
.dev
);
1594 dev_set_name(&m66592
->gadget
.dev
, "gadget");
1595 m66592
->gadget
.is_dualspeed
= 1;
1596 m66592
->gadget
.dev
.parent
= &pdev
->dev
;
1597 m66592
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1598 m66592
->gadget
.dev
.release
= pdev
->dev
.release
;
1599 m66592
->gadget
.name
= udc_name
;
1601 init_timer(&m66592
->timer
);
1602 m66592
->timer
.function
= m66592_timer
;
1603 m66592
->timer
.data
= (unsigned long)m66592
;
1606 m66592
->bi_bufnum
= M66592_BASE_BUFNUM
;
1608 ret
= request_irq(irq
, m66592_irq
, IRQF_DISABLED
| IRQF_SHARED
,
1611 pr_err("request_irq error (%d)\n", ret
);
1615 #if defined(CONFIG_SUPERH_BUILT_IN_M66592) && defined(CONFIG_HAVE_CLK)
1616 snprintf(clk_name
, sizeof(clk_name
), "usbf%d", pdev
->id
);
1617 m66592
->clk
= clk_get(&pdev
->dev
, clk_name
);
1618 if (IS_ERR(m66592
->clk
)) {
1619 dev_err(&pdev
->dev
, "cannot get clock \"%s\"\n", clk_name
);
1620 ret
= PTR_ERR(m66592
->clk
);
1623 clk_enable(m66592
->clk
);
1625 INIT_LIST_HEAD(&m66592
->gadget
.ep_list
);
1626 m66592
->gadget
.ep0
= &m66592
->ep
[0].ep
;
1627 INIT_LIST_HEAD(&m66592
->gadget
.ep0
->ep_list
);
1628 for (i
= 0; i
< M66592_MAX_NUM_PIPE
; i
++) {
1629 struct m66592_ep
*ep
= &m66592
->ep
[i
];
1632 INIT_LIST_HEAD(&m66592
->ep
[i
].ep
.ep_list
);
1633 list_add_tail(&m66592
->ep
[i
].ep
.ep_list
,
1634 &m66592
->gadget
.ep_list
);
1636 ep
->m66592
= m66592
;
1637 INIT_LIST_HEAD(&ep
->queue
);
1638 ep
->ep
.name
= m66592_ep_name
[i
];
1639 ep
->ep
.ops
= &m66592_ep_ops
;
1640 ep
->ep
.maxpacket
= 512;
1642 m66592
->ep
[0].ep
.maxpacket
= 64;
1643 m66592
->ep
[0].pipenum
= 0;
1644 m66592
->ep
[0].fifoaddr
= M66592_CFIFO
;
1645 m66592
->ep
[0].fifosel
= M66592_CFIFOSEL
;
1646 m66592
->ep
[0].fifoctr
= M66592_CFIFOCTR
;
1647 m66592
->ep
[0].fifotrn
= 0;
1648 m66592
->ep
[0].pipectr
= get_pipectr_addr(0);
1649 m66592
->pipenum2ep
[0] = &m66592
->ep
[0];
1650 m66592
->epaddr2ep
[0] = &m66592
->ep
[0];
1652 the_controller
= m66592
;
1654 m66592
->ep0_req
= m66592_alloc_request(&m66592
->ep
[0].ep
, GFP_KERNEL
);
1655 if (m66592
->ep0_req
== NULL
)
1657 m66592
->ep0_req
->complete
= nop_completion
;
1659 init_controller(m66592
);
1661 dev_info(&pdev
->dev
, "version %s\n", DRIVER_VERSION
);
1665 #if defined(CONFIG_SUPERH_BUILT_IN_M66592) && defined(CONFIG_HAVE_CLK)
1666 clk_disable(m66592
->clk
);
1667 clk_put(m66592
->clk
);
1670 free_irq(irq
, m66592
);
1673 if (m66592
->ep0_req
)
1674 m66592_free_request(&m66592
->ep
[0].ep
, m66592
->ep0_req
);
1683 /*-------------------------------------------------------------------------*/
1684 static struct platform_driver m66592_driver
= {
1685 .remove
= __exit_p(m66592_remove
),
1687 .name
= (char *) udc_name
,
1688 .owner
= THIS_MODULE
,
1692 static int __init
m66592_udc_init(void)
1694 return platform_driver_probe(&m66592_driver
, m66592_probe
);
1696 module_init(m66592_udc_init
);
1698 static void __exit
m66592_udc_cleanup(void)
1700 platform_driver_unregister(&m66592_driver
);
1702 module_exit(m66592_udc_cleanup
);