13 #include <linux/module.h>
18 #include <linux/slab.h>
20 #include <linux/usb/ch9.h>
30 #define DRIVER_VERSION "21 July 2009"
32 static const char udc_name[] =
"m66592_udc";
33 static const char *m66592_ep_name[] = {
34 "ep0",
"ep1",
"ep2",
"ep3",
"ep4",
"ep5",
"ep6",
"ep7"
43 static void transfer_complete(
struct m66592_ep *ep,
52 static void enable_pipe_irq(
struct m66592 *m66592,
u16 pipenum,
64 static void disable_pipe_irq(
struct m66592 *m66592,
u16 pipenum,
76 static void m66592_usb_connect(
struct m66592 *m66592)
86 static void m66592_usb_disconnect(
struct m66592 *m66592)
97 spin_unlock(&m66592->
lock);
99 spin_lock(&m66592->
lock);
101 disable_controller(m66592);
102 INIT_LIST_HEAD(&m66592->
ep[0].queue);
105 static inline u16 control_reg_get_pid(
struct m66592 *m66592,
u16 pipenum)
114 pid = m66592_read(m66592, offset) &
M66592_PID;
116 pr_err(
"unexpect pipe num (%d)\n", pipenum);
121 static inline void control_reg_set_pid(
struct m66592 *m66592,
u16 pipenum,
132 pr_err(
"unexpect pipe num (%d)\n", pipenum);
135 static inline void pipe_start(
struct m66592 *m66592,
u16 pipenum)
140 static inline void pipe_stop(
struct m66592 *m66592,
u16 pipenum)
145 static inline void pipe_stall(
struct m66592 *m66592,
u16 pipenum)
150 static inline u16 control_reg_get(
struct m66592 *m66592,
u16 pipenum)
159 ret = m66592_read(m66592, offset);
161 pr_err(
"unexpect pipe num (%d)\n", pipenum);
166 static inline void control_reg_sqclr(
struct m66592 *m66592,
u16 pipenum)
170 pipe_stop(m66592, pipenum);
178 pr_err(
"unexpect pipe num(%d)\n", pipenum);
181 static inline int get_buffer_size(
struct m66592 *m66592,
u16 pipenum)
199 size = ((tmp >> 10) + 1) * 64;
209 static inline void pipe_change(
struct m66592 *m66592,
u16 pipenum)
221 if (m66592->
pdata->on_chip)
229 static int pipe_buffer_setting(
struct m66592 *m66592,
232 u16 bufnum = 0, buf_bsize = 0;
242 pipecfg |= info->
type;
243 pipecfg |= info->
epnum;
244 switch (info->
type) {
270 pr_err(
"m66592 pipe memory is insufficient\n");
275 m66592_write(m66592, (buf_bsize << 10) | (bufnum),
M66592_PIPEBUF);
284 static void pipe_buffer_release(
struct m66592 *m66592,
299 pr_err(
"ep_release: unexpect pipenum (%d)\n",
303 static void pipe_initialize(
struct m66592_ep *ep)
305 struct m66592 *m66592 = ep->
m66592;
311 m66592_write(m66592, 0, ep->
pipectr);
318 if (m66592->
pdata->on_chip)
327 static void m66592_ep_setting(
struct m66592 *m66592,
struct m66592_ep *ep,
331 if ((pipenum != 0) && dma) {
339 }
else if (!m66592->
pdata->on_chip && m66592->
num_dma == 1) {
363 ep->
ep.maxpacket = usb_endpoint_maxp(desc);
366 INIT_LIST_HEAD(&ep->
queue);
369 static void m66592_ep_release(
struct m66592_ep *ep)
371 struct m66592 *m66592 = ep->
m66592;
384 static int alloc_pipe_config(
struct m66592_ep *ep,
387 struct m66592 *m66592 = ep->
m66592;
401 pr_err(
"bulk pipe is insufficient\n");
410 counter = &m66592->
bulk;
417 pr_err(
"interrupt pipe is insufficient\n");
426 pr_err(
"isochronous pipe is insufficient\n");
434 pr_err(
"unexpect xfer type\n");
440 info.
maxpacket = usb_endpoint_maxp(desc);
447 ret = pipe_buffer_setting(m66592, &info);
449 pr_err(
"pipe_buffer_setting fail\n");
457 m66592_ep_setting(m66592, ep, desc, info.
pipe, dma);
463 static int free_pipe_config(
struct m66592_ep *ep)
465 struct m66592 *m66592 = ep->
m66592;
470 pipe_buffer_release(m66592, &info);
471 m66592_ep_release(ep);
477 static void pipe_irq_enable(
struct m66592 *m66592,
u16 pipenum)
483 static void pipe_irq_disable(
struct m66592 *m66592,
u16 pipenum)
490 static void control_end(
struct m66592 *m66592,
unsigned ccpl)
492 m66592->
ep[0].internal_ccpl = ccpl;
493 pipe_start(m66592, 0);
499 struct m66592 *m66592 = ep->
m66592;
501 pipe_change(m66592, ep->
pipenum);
506 if (req->
req.length == 0) {
508 pipe_start(m66592, 0);
509 transfer_complete(ep, req, 0);
512 irq_ep0_write(ep, req);
518 struct m66592 *m66592 = ep->
m66592;
521 pipe_change(m66592, ep->
pipenum);
523 pipe_start(m66592, ep->
pipenum);
525 tmp = m66592_read(m66592, ep->
fifoctr);
527 pipe_irq_enable(m66592, ep->
pipenum);
529 irq_packet_write(ep, req);
534 struct m66592 *m66592 = ep->
m66592;
542 pipe_start(m66592, pipenum);
543 pipe_irq_enable(m66592, pipenum);
547 pipe_change(m66592, pipenum);
550 (req->
req.length + ep->
ep.maxpacket - 1)
554 pipe_start(m66592, pipenum);
555 pipe_irq_enable(m66592, pipenum);
562 start_packet_write(ep, req);
564 start_packet_read(ep, req);
575 start_ep0_write(ep, req);
578 start_packet_read(ep, req);
582 control_end(ep->
m66592, 0);
585 pr_err(
"start_ep0: unexpect ctsq(%x)\n", ctsq);
590 static void init_controller(
struct m66592 *m66592)
594 if (m66592->
pdata->on_chip) {
595 if (m66592->
pdata->endian)
617 unsigned int clock, vif, irq_sense;
619 if (m66592->
pdata->endian)
624 if (m66592->
pdata->vif)
629 switch (m66592->
pdata->xtal) {
640 pr_warning(
"m66592-udc: xtal configuration error\n");
652 pr_warning(
"m66592-udc: irq trigger config error\n");
660 m66592_mdfy(m66592, clock &
M66592_XTAL, M66592_XTAL,
682 static void disable_controller(
struct m66592 *m66592)
685 if (!m66592->
pdata->on_chip) {
696 static void m66592_start_xclock(
struct m66592 *m66592)
700 if (!m66592->
pdata->on_chip) {
708 static void transfer_complete(
struct m66592_ep *ep,
722 list_del_init(&req->
queue);
728 if (!list_empty(&ep->
queue))
731 spin_unlock(&ep->
m66592->lock);
732 req->
req.complete(&ep->
ep, &req->
req);
733 spin_lock(&ep->
m66592->lock);
738 start_packet(ep, req);
750 struct m66592 *m66592 = ep->
m66592;
752 pipe_change(m66592, pipenum);
757 tmp = m66592_read(m66592, ep->
fifoctr);
759 pr_err(
"pipe0 is busy. maybe cpu i/o bus "
760 "conflict. please power off this controller.");
767 bufsize = get_buffer_size(m66592, pipenum);
768 buf = req->
req.buf + req->
req.actual;
769 size =
min(bufsize, req->
req.length - req->
req.actual);
774 m66592_write_fifo(m66592, ep, buf, size);
775 if ((size == 0) || ((size % ep->
ep.maxpacket) != 0))
783 if ((!req->
req.zero && (req->
req.actual == req->
req.length))
784 || (size % ep->
ep.maxpacket)
792 pipe_start(m66592, pipenum);
802 struct m66592 *m66592 = ep->
m66592;
804 pipe_change(m66592, pipenum);
805 tmp = m66592_read(m66592, ep->
fifoctr);
806 if (
unlikely((tmp & M66592_FRDY) == 0)) {
807 pipe_stop(m66592, pipenum);
808 pipe_irq_disable(m66592, pipenum);
809 pr_err(
"write fifo not ready. pipnum=%d\n", pipenum);
814 bufsize = get_buffer_size(m66592, pipenum);
815 buf = req->
req.buf + req->
req.actual;
816 size =
min(bufsize, req->
req.length - req->
req.actual);
820 m66592_write_fifo(m66592, ep, buf, size);
822 || ((size % ep->
ep.maxpacket) != 0)
823 || ((bufsize != ep->
ep.maxpacket)
824 && (bufsize > size)))
831 if ((!req->
req.zero && (req->
req.actual == req->
req.length))
832 || (size % ep->
ep.maxpacket)
838 pipe_irq_enable(m66592, pipenum);
849 struct m66592 *m66592 = ep->
m66592;
852 pipe_change(m66592, pipenum);
853 tmp = m66592_read(m66592, ep->
fifoctr);
854 if (
unlikely((tmp & M66592_FRDY) == 0)) {
856 pipe_stop(m66592, pipenum);
857 pipe_irq_disable(m66592, pipenum);
858 pr_err(
"read fifo not ready");
864 bufsize = get_buffer_size(m66592, pipenum);
866 buf = req->
req.buf + req->
req.actual;
867 req_len = req->
req.length - req->
req.actual;
868 if (rcv_len < bufsize)
869 size =
min(rcv_len, req_len);
871 size =
min(bufsize, req_len);
877 if ((!req->
req.zero && (req->
req.actual == req->
req.length))
878 || (size % ep->
ep.maxpacket)
880 pipe_stop(m66592, pipenum);
881 pipe_irq_disable(m66592, pipenum);
890 m66592_read_fifo(m66592, ep->
fifoaddr, buf, size);
893 if ((ep->
pipenum != 0) && finish)
894 transfer_complete(ep, req, 0);
897 static void irq_pipe_ready(
struct m66592 *m66592,
u16 status,
u16 enb)
911 irq_packet_read(ep, req);
914 check = 1 << pipenum;
915 if ((status & check) && (enb & check)) {
921 irq_packet_write(ep, req);
923 irq_packet_read(ep, req);
929 static void irq_pipe_empty(
struct m66592 *m66592,
u16 status,
u16 enb)
942 irq_ep0_write(ep, req);
945 check = 1 << pipenum;
946 if ((status & check) && (enb & check)) {
948 tmp = control_reg_get(m66592, pipenum);
951 pipe_irq_disable(m66592, pipenum);
952 pipe_stop(m66592, pipenum);
957 if (!list_empty(&ep->
queue))
958 transfer_complete(ep, req, 0);
983 pid = control_reg_get_pid(m66592, ep->
pipenum);
990 pipe_stall(m66592, 0);
998 spin_unlock(&m66592->
lock);
1000 spin_lock(&m66592->
lock);
1007 control_end(m66592, 1);
1010 control_end(m66592, 1);
1018 pipe_stop(m66592, ep->
pipenum);
1019 control_reg_sqclr(m66592, ep->
pipenum);
1021 control_end(m66592, 1);
1027 if (list_empty(&ep->
queue))
1029 start_packet(ep, req);
1030 }
else if (!list_empty(&ep->
queue))
1031 pipe_start(m66592, ep->
pipenum);
1035 pipe_stall(m66592, 0);
1040 static void set_feature(
struct m66592 *m66592,
struct usb_ctrlrequest *ctrl)
1049 control_end(m66592, 1);
1063 pipe_stall(m66592, 0);
1068 control_end(m66592, 1);
1075 pipe_stall(m66592, ep->
pipenum);
1077 control_end(m66592, 1);
1081 pipe_stall(m66592, 0);
1087 static int setup_packet(
struct m66592 *m66592,
struct usb_ctrlrequest *ctrl)
1096 for (i = 0; i < 4; i++)
1097 p[i] = m66592_read(m66592, offset + i*2);
1106 clear_feature(m66592, ctrl);
1109 set_feature(m66592, ctrl);
1120 static void m66592_update_usb_speed(
struct m66592 *m66592)
1133 pr_err(
"USB speed unknown\n");
1137 static void irq_device_state(
struct m66592 *m66592)
1146 m66592_update_usb_speed(m66592);
1149 m66592_update_usb_speed(m66592);
1152 m66592_update_usb_speed(m66592);
1157 static void irq_control_stage(
struct m66592 *m66592)
1171 ep = &m66592->
ep[0];
1173 transfer_complete(ep, req, 0);
1180 if (setup_packet(m66592, &ctrl)) {
1181 spin_unlock(&m66592->
lock);
1183 pipe_stall(m66592, 0);
1184 spin_lock(&m66592->
lock);
1189 control_end(m66592, 0);
1192 pr_err(
"ctrl_stage: unexpect ctsq(%x)\n", ctsq);
1197 static irqreturn_t m66592_irq(
int irq,
void *_m66592)
1199 struct m66592 *m66592 = _m66592;
1202 u16 brdysts, nrdysts, bempsts;
1203 u16 brdyenb, nrdyenb, bempenb;
1207 spin_lock(&m66592->
lock);
1212 if (m66592->
pdata->on_chip && !intsts0 && !intenb0) {
1218 m66592_start_xclock(m66592);
1225 mask0 = intsts0 & intenb0;
1235 m66592_write(m66592, 0xffff & ~M66592_VBINT,
1237 m66592_start_xclock(m66592);
1248 irq_device_state(m66592);
1251 && (brdysts & brdyenb)) {
1252 irq_pipe_ready(m66592, brdysts, brdyenb);
1255 && (bempsts & bempenb)) {
1256 irq_pipe_empty(m66592, bempsts, bempenb);
1260 irq_control_stage(m66592);
1265 spin_unlock(&m66592->
lock);
1269 static void m66592_timer(
unsigned long _m66592)
1271 struct m66592 *m66592 = (
struct m66592 *)_m66592;
1272 unsigned long flags;
1282 if (m66592->
scount > 0) {
1286 if (m66592->
scount == 0) {
1288 m66592_usb_connect(m66592);
1290 m66592_usb_disconnect(m66592);
1302 spin_unlock_irqrestore(&m66592->
lock, flags);
1306 static int m66592_enable(
struct usb_ep *_ep,
1312 return alloc_pipe_config(ep, desc);
1315 static int m66592_disable(
struct usb_ep *_ep)
1319 unsigned long flags;
1324 while (!list_empty(&ep->
queue)) {
1328 spin_unlock_irqrestore(&ep->
m66592->lock, flags);
1332 return free_pipe_config(ep);
1344 INIT_LIST_HEAD(&req->
queue);
1362 unsigned long flags;
1373 if (list_empty(&ep->
queue))
1377 req->
req.actual = 0;
1383 if (request && !ep->
busy)
1384 start_packet(ep, req);
1387 spin_unlock_irqrestore(&ep->
m66592->lock, flags);
1396 unsigned long flags;
1402 if (!list_empty(&ep->
queue))
1404 spin_unlock_irqrestore(&ep->
m66592->lock, flags);
1409 static int m66592_set_halt(
struct usb_ep *_ep,
int value)
1413 unsigned long flags;
1420 if (!list_empty(&ep->
queue)) {
1433 spin_unlock_irqrestore(&ep->
m66592->lock, flags);
1437 static void m66592_fifo_flush(
struct usb_ep *_ep)
1440 unsigned long flags;
1444 if (list_empty(&ep->
queue) && !ep->
busy) {
1448 spin_unlock_irqrestore(&ep->
m66592->lock, flags);
1452 .enable = m66592_enable,
1453 .disable = m66592_disable,
1455 .alloc_request = m66592_alloc_request,
1456 .free_request = m66592_free_request,
1458 .queue = m66592_queue,
1459 .dequeue = m66592_dequeue,
1461 .set_halt = m66592_set_halt,
1462 .fifo_flush = m66592_fifo_flush,
1491 pr_err(
"device_add error (%d)\n", retval);
1495 retval = bind(&m66592->
gadget, driver);
1497 pr_err(
"bind to driver error (%d)\n", retval);
1504 m66592_start_xclock(m66592);
1506 m66592->
old_vbus = m66592_read(m66592,
1524 unsigned long flags;
1531 m66592_usb_disconnect(m66592);
1532 spin_unlock_irqrestore(&m66592->
lock, flags);
1539 init_controller(m66592);
1540 disable_controller(m66592);
1548 static int m66592_get_frame(
struct usb_gadget *_gadget)
1557 unsigned long flags;
1564 spin_unlock_irqrestore(&m66592->
lock, flags);
1570 .get_frame = m66592_get_frame,
1571 .start = m66592_start,
1572 .stop = m66592_stop,
1573 .pullup = m66592_pullup,
1585 m66592_free_request(&m66592->
ep[0].ep, m66592->
ep0_req);
1586 if (m66592->
pdata->on_chip) {
1602 struct m66592 *m66592 =
NULL;
1610 pr_err(
"platform_get_resource error.\n");
1618 "platform_get_resource IORESOURCE_IRQ error.\n");
1625 pr_err(
"ioremap error.\n");
1629 if (pdev->
dev.platform_data ==
NULL) {
1636 m66592 = kzalloc(
sizeof(
struct m66592),
GFP_KERNEL);
1637 if (m66592 ==
NULL) {
1639 pr_err(
"kzalloc error\n");
1643 m66592->
pdata = pdev->
dev.platform_data;
1649 m66592->
gadget.ops = &m66592_gadget_ops;
1654 m66592->
gadget.dev.dma_mask = pdev->
dev.dma_mask;
1655 m66592->
gadget.dev.release = pdev->
dev.release;
1656 m66592->
gadget.name = udc_name;
1659 m66592->
timer.function = m66592_timer;
1660 m66592->
timer.data = (
unsigned long)m66592;
1666 pr_err(
"request_irq error (%d)\n", ret);
1670 if (m66592->
pdata->on_chip) {
1671 snprintf(clk_name,
sizeof(clk_name),
"usbf%d", pdev->
id);
1673 if (IS_ERR(m66592->
clk)) {
1674 dev_err(&pdev->
dev,
"cannot get clock \"%s\"\n",
1676 ret = PTR_ERR(m66592->
clk);
1682 INIT_LIST_HEAD(&m66592->
gadget.ep_list);
1683 m66592->
gadget.ep0 = &m66592->
ep[0].ep;
1684 INIT_LIST_HEAD(&m66592->
gadget.ep0->ep_list);
1689 INIT_LIST_HEAD(&m66592->
ep[i].ep.ep_list);
1691 &m66592->
gadget.ep_list);
1694 INIT_LIST_HEAD(&ep->
queue);
1695 ep->
ep.name = m66592_ep_name[
i];
1696 ep->
ep.ops = &m66592_ep_ops;
1697 ep->
ep.maxpacket = 512;
1699 m66592->
ep[0].ep.maxpacket = 64;
1700 m66592->
ep[0].pipenum = 0;
1704 m66592->
ep[0].fifotrn = 0;
1714 m66592->
ep0_req->complete = nop_completion;
1716 init_controller(m66592);
1726 m66592_free_request(&m66592->
ep[0].ep, m66592->
ep0_req);
1729 if (m66592->
pdata->on_chip) {
1738 m66592_free_request(&m66592->
ep[0].ep, m66592->
ep0_req);
1751 .name = (
char *) udc_name,
1756 static int __init m66592_udc_init(
void)
1762 static void __exit m66592_udc_cleanup(
void)