aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget/r8a66597-udc.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/gadget/r8a66597-udc.c')
-rw-r--r--drivers/usb/gadget/r8a66597-udc.c516
1 files changed, 415 insertions, 101 deletions
diff --git a/drivers/usb/gadget/r8a66597-udc.c b/drivers/usb/gadget/r8a66597-udc.c
index 50991e5bd5e..68a826a1b86 100644
--- a/drivers/usb/gadget/r8a66597-udc.c
+++ b/drivers/usb/gadget/r8a66597-udc.c
@@ -8,16 +8,6 @@
8 * This program is free software; you can redistribute it and/or modify 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 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License. 10 * the Free Software Foundation; version 2 of the License.
11 *
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.
16 *
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
20 *
21 */ 11 */
22 12
23#include <linux/module.h> 13#include <linux/module.h>
@@ -28,13 +18,14 @@
28#include <linux/clk.h> 18#include <linux/clk.h>
29#include <linux/err.h> 19#include <linux/err.h>
30#include <linux/slab.h> 20#include <linux/slab.h>
21#include <linux/dma-mapping.h>
31 22
32#include <linux/usb/ch9.h> 23#include <linux/usb/ch9.h>
33#include <linux/usb/gadget.h> 24#include <linux/usb/gadget.h>
34 25
35#include "r8a66597-udc.h" 26#include "r8a66597-udc.h"
36 27
37#define DRIVER_VERSION "2009-08-18" 28#define DRIVER_VERSION "2011-09-26"
38 29
39static const char udc_name[] = "r8a66597_udc"; 30static const char udc_name[] = "r8a66597_udc";
40static const char *r8a66597_ep_name[] = { 31static const char *r8a66597_ep_name[] = {
@@ -115,13 +106,15 @@ static inline u16 control_reg_get_pid(struct r8a66597 *r8a66597, u16 pipenum)
115 u16 pid = 0; 106 u16 pid = 0;
116 unsigned long offset; 107 unsigned long offset;
117 108
118 if (pipenum == 0) 109 if (pipenum == 0) {
119 pid = r8a66597_read(r8a66597, DCPCTR) & PID; 110 pid = r8a66597_read(r8a66597, DCPCTR) & PID;
120 else if (pipenum < R8A66597_MAX_NUM_PIPE) { 111 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
121 offset = get_pipectr_addr(pipenum); 112 offset = get_pipectr_addr(pipenum);
122 pid = r8a66597_read(r8a66597, offset) & PID; 113 pid = r8a66597_read(r8a66597, offset) & PID;
123 } else 114 } else {
124 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum); 115 dev_err(r8a66597_to_dev(r8a66597), "unexpect pipe num (%d)\n",
116 pipenum);
117 }
125 118
126 return pid; 119 return pid;
127} 120}
@@ -131,13 +124,15 @@ static inline void control_reg_set_pid(struct r8a66597 *r8a66597, u16 pipenum,
131{ 124{
132 unsigned long offset; 125 unsigned long offset;
133 126
134 if (pipenum == 0) 127 if (pipenum == 0) {
135 r8a66597_mdfy(r8a66597, pid, PID, DCPCTR); 128 r8a66597_mdfy(r8a66597, pid, PID, DCPCTR);
136 else if (pipenum < R8A66597_MAX_NUM_PIPE) { 129 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
137 offset = get_pipectr_addr(pipenum); 130 offset = get_pipectr_addr(pipenum);
138 r8a66597_mdfy(r8a66597, pid, PID, offset); 131 r8a66597_mdfy(r8a66597, pid, PID, offset);
139 } else 132 } else {
140 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum); 133 dev_err(r8a66597_to_dev(r8a66597), "unexpect pipe num (%d)\n",
134 pipenum);
135 }
141} 136}
142 137
143static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum) 138static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum)
@@ -160,13 +155,15 @@ static inline u16 control_reg_get(struct r8a66597 *r8a66597, u16 pipenum)
160 u16 ret = 0; 155 u16 ret = 0;
161 unsigned long offset; 156 unsigned long offset;
162 157
163 if (pipenum == 0) 158 if (pipenum == 0) {
164 ret = r8a66597_read(r8a66597, DCPCTR); 159 ret = r8a66597_read(r8a66597, DCPCTR);
165 else if (pipenum < R8A66597_MAX_NUM_PIPE) { 160 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
166 offset = get_pipectr_addr(pipenum); 161 offset = get_pipectr_addr(pipenum);
167 ret = r8a66597_read(r8a66597, offset); 162 ret = r8a66597_read(r8a66597, offset);
168 } else 163 } else {
169 printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum); 164 dev_err(r8a66597_to_dev(r8a66597), "unexpect pipe num (%d)\n",
165 pipenum);
166 }
170 167
171 return ret; 168 return ret;
172} 169}
@@ -177,13 +174,63 @@ static inline void control_reg_sqclr(struct r8a66597 *r8a66597, u16 pipenum)
177 174
178 pipe_stop(r8a66597, pipenum); 175 pipe_stop(r8a66597, pipenum);
179 176
180 if (pipenum == 0) 177 if (pipenum == 0) {
181 r8a66597_bset(r8a66597, SQCLR, DCPCTR); 178 r8a66597_bset(r8a66597, SQCLR, DCPCTR);
182 else if (pipenum < R8A66597_MAX_NUM_PIPE) { 179 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
183 offset = get_pipectr_addr(pipenum); 180 offset = get_pipectr_addr(pipenum);
184 r8a66597_bset(r8a66597, SQCLR, offset); 181 r8a66597_bset(r8a66597, SQCLR, offset);
185 } else 182 } else {
186 printk(KERN_ERR "unexpect pipe num(%d)\n", pipenum); 183 dev_err(r8a66597_to_dev(r8a66597), "unexpect pipe num (%d)\n",
184 pipenum);
185 }
186}
187
188static void control_reg_sqset(struct r8a66597 *r8a66597, u16 pipenum)
189{
190 unsigned long offset;
191
192 pipe_stop(r8a66597, pipenum);
193
194 if (pipenum == 0) {
195 r8a66597_bset(r8a66597, SQSET, DCPCTR);
196 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
197 offset = get_pipectr_addr(pipenum);
198 r8a66597_bset(r8a66597, SQSET, offset);
199 } else {
200 dev_err(r8a66597_to_dev(r8a66597),
201 "unexpect pipe num(%d)\n", pipenum);
202 }
203}
204
205static u16 control_reg_sqmon(struct r8a66597 *r8a66597, u16 pipenum)
206{
207 unsigned long offset;
208
209 if (pipenum == 0) {
210 return r8a66597_read(r8a66597, DCPCTR) & SQMON;
211 } else if (pipenum < R8A66597_MAX_NUM_PIPE) {
212 offset = get_pipectr_addr(pipenum);
213 return r8a66597_read(r8a66597, offset) & SQMON;
214 } else {
215 dev_err(r8a66597_to_dev(r8a66597),
216 "unexpect pipe num(%d)\n", pipenum);
217 }
218
219 return 0;
220}
221
222static u16 save_usb_toggle(struct r8a66597 *r8a66597, u16 pipenum)
223{
224 return control_reg_sqmon(r8a66597, pipenum);
225}
226
227static void restore_usb_toggle(struct r8a66597 *r8a66597, u16 pipenum,
228 u16 toggle)
229{
230 if (toggle)
231 control_reg_sqset(r8a66597, pipenum);
232 else
233 control_reg_sqclr(r8a66597, pipenum);
187} 234}
188 235
189static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum) 236static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum)
@@ -222,18 +269,51 @@ static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
222 return MBW_16; 269 return MBW_16;
223} 270}
224 271
272static void r8a66597_change_curpipe(struct r8a66597 *r8a66597, u16 pipenum,
273 u16 isel, u16 fifosel)
274{
275 u16 tmp, mask, loop;
276 int i = 0;
277
278 if (!pipenum) {
279 mask = ISEL | CURPIPE;
280 loop = isel;
281 } else {
282 mask = CURPIPE;
283 loop = pipenum;
284 }
285 r8a66597_mdfy(r8a66597, loop, mask, fifosel);
286
287 do {
288 tmp = r8a66597_read(r8a66597, fifosel);
289 if (i++ > 1000000) {
290 dev_err(r8a66597_to_dev(r8a66597),
291 "r8a66597: register%x, loop %x "
292 "is timeout\n", fifosel, loop);
293 break;
294 }
295 ndelay(1);
296 } while ((tmp & mask) != loop);
297}
298
225static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum) 299static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum)
226{ 300{
227 struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum]; 301 struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
228 302
229 if (ep->use_dma) 303 if (ep->use_dma)
230 return; 304 r8a66597_bclr(r8a66597, DREQE, ep->fifosel);
231 305
232 r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel); 306 r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel);
233 307
234 ndelay(450); 308 ndelay(450);
235 309
236 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel); 310 if (r8a66597_is_sudmac(r8a66597) && ep->use_dma)
311 r8a66597_bclr(r8a66597, mbw_value(r8a66597), ep->fifosel);
312 else
313 r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
314
315 if (ep->use_dma)
316 r8a66597_bset(r8a66597, DREQE, ep->fifosel);
237} 317}
238 318
239static int pipe_buffer_setting(struct r8a66597 *r8a66597, 319static int pipe_buffer_setting(struct r8a66597 *r8a66597,
@@ -297,17 +377,18 @@ static void pipe_buffer_release(struct r8a66597 *r8a66597,
297 if (info->pipe == 0) 377 if (info->pipe == 0)
298 return; 378 return;
299 379
300 if (is_bulk_pipe(info->pipe)) 380 if (is_bulk_pipe(info->pipe)) {
301 r8a66597->bulk--; 381 r8a66597->bulk--;
302 else if (is_interrupt_pipe(info->pipe)) 382 } else if (is_interrupt_pipe(info->pipe)) {
303 r8a66597->interrupt--; 383 r8a66597->interrupt--;
304 else if (is_isoc_pipe(info->pipe)) { 384 } else if (is_isoc_pipe(info->pipe)) {
305 r8a66597->isochronous--; 385 r8a66597->isochronous--;
306 if (info->type == R8A66597_BULK) 386 if (info->type == R8A66597_BULK)
307 r8a66597->bulk--; 387 r8a66597->bulk--;
308 } else 388 } else {
309 printk(KERN_ERR "ep_release: unexpect pipenum (%d)\n", 389 dev_err(r8a66597_to_dev(r8a66597),
310 info->pipe); 390 "ep_release: unexpect pipenum (%d)\n", info->pipe);
391 }
311} 392}
312 393
313static void pipe_initialize(struct r8a66597_ep *ep) 394static void pipe_initialize(struct r8a66597_ep *ep)
@@ -337,11 +418,17 @@ static void r8a66597_ep_setting(struct r8a66597 *r8a66597,
337 ep->fifoaddr = CFIFO; 418 ep->fifoaddr = CFIFO;
338 ep->fifosel = CFIFOSEL; 419 ep->fifosel = CFIFOSEL;
339 ep->fifoctr = CFIFOCTR; 420 ep->fifoctr = CFIFOCTR;
340 ep->fifotrn = 0;
341 421
342 ep->pipectr = get_pipectr_addr(pipenum); 422 ep->pipectr = get_pipectr_addr(pipenum);
423 if (is_bulk_pipe(pipenum) || is_isoc_pipe(pipenum)) {
424 ep->pipetre = get_pipetre_addr(pipenum);
425 ep->pipetrn = get_pipetrn_addr(pipenum);
426 } else {
427 ep->pipetre = 0;
428 ep->pipetrn = 0;
429 }
343 ep->pipenum = pipenum; 430 ep->pipenum = pipenum;
344 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize); 431 ep->ep.maxpacket = usb_endpoint_maxp(desc);
345 r8a66597->pipenum2ep[pipenum] = ep; 432 r8a66597->pipenum2ep[pipenum] = ep;
346 r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK] 433 r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK]
347 = ep; 434 = ep;
@@ -381,7 +468,8 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
381 case USB_ENDPOINT_XFER_BULK: 468 case USB_ENDPOINT_XFER_BULK:
382 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) { 469 if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) {
383 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) { 470 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
384 printk(KERN_ERR "bulk pipe is insufficient\n"); 471 dev_err(r8a66597_to_dev(r8a66597),
472 "bulk pipe is insufficient\n");
385 return -ENODEV; 473 return -ENODEV;
386 } else { 474 } else {
387 info.pipe = R8A66597_BASE_PIPENUM_ISOC 475 info.pipe = R8A66597_BASE_PIPENUM_ISOC
@@ -397,7 +485,8 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
397 break; 485 break;
398 case USB_ENDPOINT_XFER_INT: 486 case USB_ENDPOINT_XFER_INT:
399 if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) { 487 if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) {
400 printk(KERN_ERR "interrupt pipe is insufficient\n"); 488 dev_err(r8a66597_to_dev(r8a66597),
489 "interrupt pipe is insufficient\n");
401 return -ENODEV; 490 return -ENODEV;
402 } 491 }
403 info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt; 492 info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt;
@@ -406,7 +495,8 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
406 break; 495 break;
407 case USB_ENDPOINT_XFER_ISOC: 496 case USB_ENDPOINT_XFER_ISOC:
408 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) { 497 if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
409 printk(KERN_ERR "isochronous pipe is insufficient\n"); 498 dev_err(r8a66597_to_dev(r8a66597),
499 "isochronous pipe is insufficient\n");
410 return -ENODEV; 500 return -ENODEV;
411 } 501 }
412 info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous; 502 info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous;
@@ -414,13 +504,13 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
414 counter = &r8a66597->isochronous; 504 counter = &r8a66597->isochronous;
415 break; 505 break;
416 default: 506 default:
417 printk(KERN_ERR "unexpect xfer type\n"); 507 dev_err(r8a66597_to_dev(r8a66597), "unexpect xfer type\n");
418 return -EINVAL; 508 return -EINVAL;
419 } 509 }
420 ep->type = info.type; 510 ep->type = info.type;
421 511
422 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; 512 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
423 info.maxpacket = le16_to_cpu(desc->wMaxPacketSize); 513 info.maxpacket = usb_endpoint_maxp(desc);
424 info.interval = desc->bInterval; 514 info.interval = desc->bInterval;
425 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) 515 if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
426 info.dir_in = 1; 516 info.dir_in = 1;
@@ -429,7 +519,8 @@ static int alloc_pipe_config(struct r8a66597_ep *ep,
429 519
430 ret = pipe_buffer_setting(r8a66597, &info); 520 ret = pipe_buffer_setting(r8a66597, &info);
431 if (ret < 0) { 521 if (ret < 0) {
432 printk(KERN_ERR "pipe_buffer_setting fail\n"); 522 dev_err(r8a66597_to_dev(r8a66597),
523 "pipe_buffer_setting fail\n");
433 return ret; 524 return ret;
434 } 525 }
435 526
@@ -495,6 +586,124 @@ static void start_ep0_write(struct r8a66597_ep *ep,
495 } 586 }
496} 587}
497 588
589static void disable_fifosel(struct r8a66597 *r8a66597, u16 pipenum,
590 u16 fifosel)
591{
592 u16 tmp;
593
594 tmp = r8a66597_read(r8a66597, fifosel) & CURPIPE;
595 if (tmp == pipenum)
596 r8a66597_change_curpipe(r8a66597, 0, 0, fifosel);
597}
598
599static void change_bfre_mode(struct r8a66597 *r8a66597, u16 pipenum,
600 int enable)
601{
602 struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
603 u16 tmp, toggle;
604
605 /* check current BFRE bit */
606 r8a66597_write(r8a66597, pipenum, PIPESEL);
607 tmp = r8a66597_read(r8a66597, PIPECFG) & R8A66597_BFRE;
608 if ((enable && tmp) || (!enable && !tmp))
609 return;
610
611 /* change BFRE bit */
612 pipe_stop(r8a66597, pipenum);
613 disable_fifosel(r8a66597, pipenum, CFIFOSEL);
614 disable_fifosel(r8a66597, pipenum, D0FIFOSEL);
615 disable_fifosel(r8a66597, pipenum, D1FIFOSEL);
616
617 toggle = save_usb_toggle(r8a66597, pipenum);
618
619 r8a66597_write(r8a66597, pipenum, PIPESEL);
620 if (enable)
621 r8a66597_bset(r8a66597, R8A66597_BFRE, PIPECFG);
622 else
623 r8a66597_bclr(r8a66597, R8A66597_BFRE, PIPECFG);
624
625 /* initialize for internal BFRE flag */
626 r8a66597_bset(r8a66597, ACLRM, ep->pipectr);
627 r8a66597_bclr(r8a66597, ACLRM, ep->pipectr);
628
629 restore_usb_toggle(r8a66597, pipenum, toggle);
630}
631
632static int sudmac_alloc_channel(struct r8a66597 *r8a66597,
633 struct r8a66597_ep *ep,
634 struct r8a66597_request *req)
635{
636 struct r8a66597_dma *dma;
637
638 if (!r8a66597_is_sudmac(r8a66597))
639 return -ENODEV;
640
641 /* Check transfer type */
642 if (!is_bulk_pipe(ep->pipenum))
643 return -EIO;
644
645 if (r8a66597->dma.used)
646 return -EBUSY;
647
648 /* set SUDMAC parameters */
649 dma = &r8a66597->dma;
650 dma->used = 1;
651 if (ep->desc->bEndpointAddress & USB_DIR_IN) {
652 dma->dir = 1;
653 } else {
654 dma->dir = 0;
655 change_bfre_mode(r8a66597, ep->pipenum, 1);
656 }
657
658 /* set r8a66597_ep paramters */
659 ep->use_dma = 1;
660 ep->dma = dma;
661 ep->fifoaddr = D0FIFO;
662 ep->fifosel = D0FIFOSEL;
663 ep->fifoctr = D0FIFOCTR;
664
665 /* dma mapping */
666 req->req.dma = dma_map_single(r8a66597_to_dev(ep->r8a66597),
667 req->req.buf, req->req.length,
668 dma->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
669
670 return 0;
671}
672
673static void sudmac_free_channel(struct r8a66597 *r8a66597,
674 struct r8a66597_ep *ep,
675 struct r8a66597_request *req)
676{
677 if (!r8a66597_is_sudmac(r8a66597))
678 return;
679
680 dma_unmap_single(r8a66597_to_dev(ep->r8a66597),
681 req->req.dma, req->req.length,
682 ep->dma->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
683
684 r8a66597_bclr(r8a66597, DREQE, ep->fifosel);
685 r8a66597_change_curpipe(r8a66597, 0, 0, ep->fifosel);
686
687 ep->dma->used = 0;
688 ep->use_dma = 0;
689 ep->fifoaddr = CFIFO;
690 ep->fifosel = CFIFOSEL;
691 ep->fifoctr = CFIFOCTR;
692}
693
694static void sudmac_start(struct r8a66597 *r8a66597, struct r8a66597_ep *ep,
695 struct r8a66597_request *req)
696{
697 BUG_ON(req->req.length == 0);
698
699 r8a66597_sudmac_write(r8a66597, LBA_WAIT, CH0CFG);
700 r8a66597_sudmac_write(r8a66597, req->req.dma, CH0BA);
701 r8a66597_sudmac_write(r8a66597, req->req.length, CH0BBC);
702 r8a66597_sudmac_write(r8a66597, CH0ENDE, DINTCTRL);
703
704 r8a66597_sudmac_write(r8a66597, DEN, CH0DEN);
705}
706
498static void start_packet_write(struct r8a66597_ep *ep, 707static void start_packet_write(struct r8a66597_ep *ep,
499 struct r8a66597_request *req) 708 struct r8a66597_request *req)
500{ 709{
@@ -505,11 +714,29 @@ static void start_packet_write(struct r8a66597_ep *ep,
505 disable_irq_empty(r8a66597, ep->pipenum); 714 disable_irq_empty(r8a66597, ep->pipenum);
506 pipe_start(r8a66597, ep->pipenum); 715 pipe_start(r8a66597, ep->pipenum);
507 716
508 tmp = r8a66597_read(r8a66597, ep->fifoctr); 717 if (req->req.length == 0) {
509 if (unlikely((tmp & FRDY) == 0)) 718 transfer_complete(ep, req, 0);
510 pipe_irq_enable(r8a66597, ep->pipenum); 719 } else {
511 else 720 r8a66597_write(r8a66597, ~(1 << ep->pipenum), BRDYSTS);
512 irq_packet_write(ep, req); 721 if (sudmac_alloc_channel(r8a66597, ep, req) < 0) {
722 /* PIO mode */
723 pipe_change(r8a66597, ep->pipenum);
724 disable_irq_empty(r8a66597, ep->pipenum);
725 pipe_start(r8a66597, ep->pipenum);
726 tmp = r8a66597_read(r8a66597, ep->fifoctr);
727 if (unlikely((tmp & FRDY) == 0))
728 pipe_irq_enable(r8a66597, ep->pipenum);
729 else
730 irq_packet_write(ep, req);
731 } else {
732 /* DMA mode */
733 pipe_change(r8a66597, ep->pipenum);
734 disable_irq_nrdy(r8a66597, ep->pipenum);
735 pipe_start(r8a66597, ep->pipenum);
736 enable_irq_nrdy(r8a66597, ep->pipenum);
737 sudmac_start(r8a66597, ep, req);
738 }
739 }
513} 740}
514 741
515static void start_packet_read(struct r8a66597_ep *ep, 742static void start_packet_read(struct r8a66597_ep *ep,
@@ -524,17 +751,26 @@ static void start_packet_read(struct r8a66597_ep *ep,
524 pipe_start(r8a66597, pipenum); 751 pipe_start(r8a66597, pipenum);
525 pipe_irq_enable(r8a66597, pipenum); 752 pipe_irq_enable(r8a66597, pipenum);
526 } else { 753 } else {
527 if (ep->use_dma) { 754 pipe_stop(r8a66597, pipenum);
528 r8a66597_bset(r8a66597, TRCLR, ep->fifosel); 755 if (ep->pipetre) {
529 pipe_change(r8a66597, pipenum); 756 enable_irq_nrdy(r8a66597, pipenum);
530 r8a66597_bset(r8a66597, TRENB, ep->fifosel); 757 r8a66597_write(r8a66597, TRCLR, ep->pipetre);
531 r8a66597_write(r8a66597, 758 r8a66597_write(r8a66597,
532 (req->req.length + ep->ep.maxpacket - 1) 759 DIV_ROUND_UP(req->req.length, ep->ep.maxpacket),
533 / ep->ep.maxpacket, 760 ep->pipetrn);
534 ep->fifotrn); 761 r8a66597_bset(r8a66597, TRENB, ep->pipetre);
762 }
763
764 if (sudmac_alloc_channel(r8a66597, ep, req) < 0) {
765 /* PIO mode */
766 change_bfre_mode(r8a66597, ep->pipenum, 0);
767 pipe_start(r8a66597, pipenum); /* trigger once */
768 pipe_irq_enable(r8a66597, pipenum);
769 } else {
770 pipe_change(r8a66597, pipenum);
771 sudmac_start(r8a66597, ep, req);
772 pipe_start(r8a66597, pipenum); /* trigger once */
535 } 773 }
536 pipe_start(r8a66597, pipenum); /* trigger once */
537 pipe_irq_enable(r8a66597, pipenum);
538 } 774 }
539} 775}
540 776
@@ -564,7 +800,8 @@ static void start_ep0(struct r8a66597_ep *ep, struct r8a66597_request *req)
564 control_end(ep->r8a66597, 0); 800 control_end(ep->r8a66597, 0);
565 break; 801 break;
566 default: 802 default:
567 printk(KERN_ERR "start_ep0: unexpect ctsq(%x)\n", ctsq); 803 dev_err(r8a66597_to_dev(ep->r8a66597),
804 "start_ep0: unexpect ctsq(%x)\n", ctsq);
568 break; 805 break;
569 } 806 }
570} 807}
@@ -690,6 +927,9 @@ __acquires(r8a66597->lock)
690 if (!list_empty(&ep->queue)) 927 if (!list_empty(&ep->queue))
691 restart = 1; 928 restart = 1;
692 929
930 if (ep->use_dma)
931 sudmac_free_channel(ep->r8a66597, ep, req);
932
693 spin_unlock(&ep->r8a66597->lock); 933 spin_unlock(&ep->r8a66597->lock);
694 req->req.complete(&ep->ep, &req->req); 934 req->req.complete(&ep->ep, &req->req);
695 spin_lock(&ep->r8a66597->lock); 935 spin_lock(&ep->r8a66597->lock);
@@ -718,7 +958,8 @@ static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
718 do { 958 do {
719 tmp = r8a66597_read(r8a66597, ep->fifoctr); 959 tmp = r8a66597_read(r8a66597, ep->fifoctr);
720 if (i++ > 100000) { 960 if (i++ > 100000) {
721 printk(KERN_ERR "pipe0 is busy. maybe cpu i/o bus" 961 dev_err(r8a66597_to_dev(r8a66597),
962 "pipe0 is busy. maybe cpu i/o bus "
722 "conflict. please power off this controller."); 963 "conflict. please power off this controller.");
723 return; 964 return;
724 } 965 }
@@ -733,7 +974,7 @@ static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
733 /* write fifo */ 974 /* write fifo */
734 if (req->req.buf) { 975 if (req->req.buf) {
735 if (size > 0) 976 if (size > 0)
736 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size); 977 r8a66597_write_fifo(r8a66597, ep, buf, size);
737 if ((size == 0) || ((size % ep->ep.maxpacket) != 0)) 978 if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
738 r8a66597_bset(r8a66597, BVAL, ep->fifoctr); 979 r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
739 } 980 }
@@ -769,7 +1010,8 @@ static void irq_packet_write(struct r8a66597_ep *ep,
769 if (unlikely((tmp & FRDY) == 0)) { 1010 if (unlikely((tmp & FRDY) == 0)) {
770 pipe_stop(r8a66597, pipenum); 1011 pipe_stop(r8a66597, pipenum);
771 pipe_irq_disable(r8a66597, pipenum); 1012 pipe_irq_disable(r8a66597, pipenum);
772 printk(KERN_ERR "write fifo not ready. pipnum=%d\n", pipenum); 1013 dev_err(r8a66597_to_dev(r8a66597),
1014 "write fifo not ready. pipnum=%d\n", pipenum);
773 return; 1015 return;
774 } 1016 }
775 1017
@@ -780,7 +1022,7 @@ static void irq_packet_write(struct r8a66597_ep *ep,
780 1022
781 /* write fifo */ 1023 /* write fifo */
782 if (req->req.buf) { 1024 if (req->req.buf) {
783 r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size); 1025 r8a66597_write_fifo(r8a66597, ep, buf, size);
784 if ((size == 0) 1026 if ((size == 0)
785 || ((size % ep->ep.maxpacket) != 0) 1027 || ((size % ep->ep.maxpacket) != 0)
786 || ((bufsize != ep->ep.maxpacket) 1028 || ((bufsize != ep->ep.maxpacket)
@@ -819,7 +1061,7 @@ static void irq_packet_read(struct r8a66597_ep *ep,
819 req->req.status = -EPIPE; 1061 req->req.status = -EPIPE;
820 pipe_stop(r8a66597, pipenum); 1062 pipe_stop(r8a66597, pipenum);
821 pipe_irq_disable(r8a66597, pipenum); 1063 pipe_irq_disable(r8a66597, pipenum);
822 printk(KERN_ERR "read fifo not ready"); 1064 dev_err(r8a66597_to_dev(r8a66597), "read fifo not ready");
823 return; 1065 return;
824 } 1066 }
825 1067
@@ -1095,7 +1337,7 @@ static void r8a66597_update_usb_speed(struct r8a66597 *r8a66597)
1095 break; 1337 break;
1096 default: 1338 default:
1097 r8a66597->gadget.speed = USB_SPEED_UNKNOWN; 1339 r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
1098 printk(KERN_ERR "USB speed unknown\n"); 1340 dev_err(r8a66597_to_dev(r8a66597), "USB speed unknown\n");
1099 } 1341 }
1100} 1342}
1101 1343
@@ -1158,11 +1400,71 @@ __acquires(r8a66597->lock)
1158 control_end(r8a66597, 0); 1400 control_end(r8a66597, 0);
1159 break; 1401 break;
1160 default: 1402 default:
1161 printk(KERN_ERR "ctrl_stage: unexpect ctsq(%x)\n", ctsq); 1403 dev_err(r8a66597_to_dev(r8a66597),
1404 "ctrl_stage: unexpect ctsq(%x)\n", ctsq);
1162 break; 1405 break;
1163 } 1406 }
1164} 1407}
1165 1408
1409static void sudmac_finish(struct r8a66597 *r8a66597, struct r8a66597_ep *ep)
1410{
1411 u16 pipenum;
1412 struct r8a66597_request *req;
1413 u32 len;
1414 int i = 0;
1415
1416 pipenum = ep->pipenum;
1417 pipe_change(r8a66597, pipenum);
1418
1419 while (!(r8a66597_read(r8a66597, ep->fifoctr) & FRDY)) {
1420 udelay(1);
1421 if (unlikely(i++ >= 10000)) { /* timeout = 10 msec */
1422 dev_err(r8a66597_to_dev(r8a66597),
1423 "%s: FRDY was not set (%d)\n",
1424 __func__, pipenum);
1425 return;
1426 }
1427 }
1428
1429 r8a66597_bset(r8a66597, BCLR, ep->fifoctr);
1430 req = get_request_from_ep(ep);
1431
1432 /* prepare parameters */
1433 len = r8a66597_sudmac_read(r8a66597, CH0CBC);
1434 req->req.actual += len;
1435
1436 /* clear */
1437 r8a66597_sudmac_write(r8a66597, CH0STCLR, DSTSCLR);
1438
1439 /* check transfer finish */
1440 if ((!req->req.zero && (req->req.actual == req->req.length))
1441 || (len % ep->ep.maxpacket)) {
1442 if (ep->dma->dir) {
1443 disable_irq_ready(r8a66597, pipenum);
1444 enable_irq_empty(r8a66597, pipenum);
1445 } else {
1446 /* Clear the interrupt flag for next transfer */
1447 r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS);
1448 transfer_complete(ep, req, 0);
1449 }
1450 }
1451}
1452
1453static void r8a66597_sudmac_irq(struct r8a66597 *r8a66597)
1454{
1455 u32 irqsts;
1456 struct r8a66597_ep *ep;
1457 u16 pipenum;
1458
1459 irqsts = r8a66597_sudmac_read(r8a66597, DINTSTS);
1460 if (irqsts & CH0ENDS) {
1461 r8a66597_sudmac_write(r8a66597, CH0ENDC, DINTSTSCLR);
1462 pipenum = (r8a66597_read(r8a66597, D0FIFOSEL) & CURPIPE);
1463 ep = r8a66597->pipenum2ep[pipenum];
1464 sudmac_finish(r8a66597, ep);
1465 }
1466}
1467
1166static irqreturn_t r8a66597_irq(int irq, void *_r8a66597) 1468static irqreturn_t r8a66597_irq(int irq, void *_r8a66597)
1167{ 1469{
1168 struct r8a66597 *r8a66597 = _r8a66597; 1470 struct r8a66597 *r8a66597 = _r8a66597;
@@ -1173,6 +1475,9 @@ static irqreturn_t r8a66597_irq(int irq, void *_r8a66597)
1173 u16 savepipe; 1475 u16 savepipe;
1174 u16 mask0; 1476 u16 mask0;
1175 1477
1478 if (r8a66597_is_sudmac(r8a66597))
1479 r8a66597_sudmac_irq(r8a66597);
1480
1176 spin_lock(&r8a66597->lock); 1481 spin_lock(&r8a66597->lock);
1177 1482
1178 intsts0 = r8a66597_read(r8a66597, INTSTS0); 1483 intsts0 = r8a66597_read(r8a66597, INTSTS0);
@@ -1433,23 +1738,18 @@ static struct usb_ep_ops r8a66597_ep_ops = {
1433}; 1738};
1434 1739
1435/*-------------------------------------------------------------------------*/ 1740/*-------------------------------------------------------------------------*/
1436static struct r8a66597 *the_controller; 1741static int r8a66597_start(struct usb_gadget *gadget,
1437 1742 struct usb_gadget_driver *driver)
1438static int r8a66597_start(struct usb_gadget_driver *driver,
1439 int (*bind)(struct usb_gadget *))
1440{ 1743{
1441 struct r8a66597 *r8a66597 = the_controller; 1744 struct r8a66597 *r8a66597 = gadget_to_r8a66597(gadget);
1442 int retval; 1745 int retval;
1443 1746
1444 if (!driver 1747 if (!driver
1445 || driver->speed != USB_SPEED_HIGH 1748 || driver->speed != USB_SPEED_HIGH
1446 || !bind
1447 || !driver->setup) 1749 || !driver->setup)
1448 return -EINVAL; 1750 return -EINVAL;
1449 if (!r8a66597) 1751 if (!r8a66597)
1450 return -ENODEV; 1752 return -ENODEV;
1451 if (r8a66597->driver)
1452 return -EBUSY;
1453 1753
1454 /* hook up the driver */ 1754 /* hook up the driver */
1455 driver->driver.bus = NULL; 1755 driver->driver.bus = NULL;
@@ -1458,14 +1758,8 @@ static int r8a66597_start(struct usb_gadget_driver *driver,
1458 1758
1459 retval = device_add(&r8a66597->gadget.dev); 1759 retval = device_add(&r8a66597->gadget.dev);
1460 if (retval) { 1760 if (retval) {
1461 printk(KERN_ERR "device_add error (%d)\n", retval); 1761 dev_err(r8a66597_to_dev(r8a66597), "device_add error (%d)\n",
1462 goto error; 1762 retval);
1463 }
1464
1465 retval = bind(&r8a66597->gadget);
1466 if (retval) {
1467 printk(KERN_ERR "bind to driver error (%d)\n", retval);
1468 device_del(&r8a66597->gadget.dev);
1469 goto error; 1763 goto error;
1470 } 1764 }
1471 1765
@@ -1489,23 +1783,17 @@ error:
1489 return retval; 1783 return retval;
1490} 1784}
1491 1785
1492static int r8a66597_stop(struct usb_gadget_driver *driver) 1786static int r8a66597_stop(struct usb_gadget *gadget,
1787 struct usb_gadget_driver *driver)
1493{ 1788{
1494 struct r8a66597 *r8a66597 = the_controller; 1789 struct r8a66597 *r8a66597 = gadget_to_r8a66597(gadget);
1495 unsigned long flags; 1790 unsigned long flags;
1496 1791
1497 if (driver != r8a66597->driver || !driver->unbind)
1498 return -EINVAL;
1499
1500 spin_lock_irqsave(&r8a66597->lock, flags); 1792 spin_lock_irqsave(&r8a66597->lock, flags);
1501 if (r8a66597->gadget.speed != USB_SPEED_UNKNOWN)
1502 r8a66597_usb_disconnect(r8a66597);
1503 r8a66597_bclr(r8a66597, VBSE, INTENB0); 1793 r8a66597_bclr(r8a66597, VBSE, INTENB0);
1504 disable_controller(r8a66597); 1794 disable_controller(r8a66597);
1505 spin_unlock_irqrestore(&r8a66597->lock, flags); 1795 spin_unlock_irqrestore(&r8a66597->lock, flags);
1506 1796
1507 driver->unbind(&r8a66597->gadget);
1508
1509 device_del(&r8a66597->gadget.dev); 1797 device_del(&r8a66597->gadget.dev);
1510 r8a66597->driver = NULL; 1798 r8a66597->driver = NULL;
1511 return 0; 1799 return 0;
@@ -1535,8 +1823,8 @@ static int r8a66597_pullup(struct usb_gadget *gadget, int is_on)
1535 1823
1536static struct usb_gadget_ops r8a66597_gadget_ops = { 1824static struct usb_gadget_ops r8a66597_gadget_ops = {
1537 .get_frame = r8a66597_get_frame, 1825 .get_frame = r8a66597_get_frame,
1538 .start = r8a66597_start, 1826 .udc_start = r8a66597_start,
1539 .stop = r8a66597_stop, 1827 .udc_stop = r8a66597_stop,
1540 .pullup = r8a66597_pullup, 1828 .pullup = r8a66597_pullup,
1541}; 1829};
1542 1830
@@ -1547,6 +1835,8 @@ static int __exit r8a66597_remove(struct platform_device *pdev)
1547 usb_del_gadget_udc(&r8a66597->gadget); 1835 usb_del_gadget_udc(&r8a66597->gadget);
1548 del_timer_sync(&r8a66597->timer); 1836 del_timer_sync(&r8a66597->timer);
1549 iounmap(r8a66597->reg); 1837 iounmap(r8a66597->reg);
1838 if (r8a66597->pdata->sudmac)
1839 iounmap(r8a66597->sudmac_reg);
1550 free_irq(platform_get_irq(pdev, 0), r8a66597); 1840 free_irq(platform_get_irq(pdev, 0), r8a66597);
1551 r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req); 1841 r8a66597_free_request(&r8a66597->ep[0].ep, r8a66597->ep0_req);
1552#ifdef CONFIG_HAVE_CLK 1842#ifdef CONFIG_HAVE_CLK
@@ -1563,6 +1853,26 @@ static void nop_completion(struct usb_ep *ep, struct usb_request *r)
1563{ 1853{
1564} 1854}
1565 1855
1856static int __init r8a66597_sudmac_ioremap(struct r8a66597 *r8a66597,
1857 struct platform_device *pdev)
1858{
1859 struct resource *res;
1860
1861 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sudmac");
1862 if (!res) {
1863 dev_err(&pdev->dev, "platform_get_resource error(sudmac).\n");
1864 return -ENODEV;
1865 }
1866
1867 r8a66597->sudmac_reg = ioremap(res->start, resource_size(res));
1868 if (r8a66597->sudmac_reg == NULL) {
1869 dev_err(&pdev->dev, "ioremap error(sudmac).\n");
1870 return -ENOMEM;
1871 }
1872
1873 return 0;
1874}
1875
1566static int __init r8a66597_probe(struct platform_device *pdev) 1876static int __init r8a66597_probe(struct platform_device *pdev)
1567{ 1877{
1568#ifdef CONFIG_HAVE_CLK 1878#ifdef CONFIG_HAVE_CLK
@@ -1579,7 +1889,7 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1579 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1889 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1580 if (!res) { 1890 if (!res) {
1581 ret = -ENODEV; 1891 ret = -ENODEV;
1582 printk(KERN_ERR "platform_get_resource error.\n"); 1892 dev_err(&pdev->dev, "platform_get_resource error.\n");
1583 goto clean_up; 1893 goto clean_up;
1584 } 1894 }
1585 1895
@@ -1589,14 +1899,14 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1589 1899
1590 if (irq < 0) { 1900 if (irq < 0) {
1591 ret = -ENODEV; 1901 ret = -ENODEV;
1592 printk(KERN_ERR "platform_get_irq error.\n"); 1902 dev_err(&pdev->dev, "platform_get_irq error.\n");
1593 goto clean_up; 1903 goto clean_up;
1594 } 1904 }
1595 1905
1596 reg = ioremap(res->start, resource_size(res)); 1906 reg = ioremap(res->start, resource_size(res));
1597 if (reg == NULL) { 1907 if (reg == NULL) {
1598 ret = -ENOMEM; 1908 ret = -ENOMEM;
1599 printk(KERN_ERR "ioremap error.\n"); 1909 dev_err(&pdev->dev, "ioremap error.\n");
1600 goto clean_up; 1910 goto clean_up;
1601 } 1911 }
1602 1912
@@ -1604,7 +1914,7 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1604 r8a66597 = kzalloc(sizeof(struct r8a66597), GFP_KERNEL); 1914 r8a66597 = kzalloc(sizeof(struct r8a66597), GFP_KERNEL);
1605 if (r8a66597 == NULL) { 1915 if (r8a66597 == NULL) {
1606 ret = -ENOMEM; 1916 ret = -ENOMEM;
1607 printk(KERN_ERR "kzalloc error\n"); 1917 dev_err(&pdev->dev, "kzalloc error\n");
1608 goto clean_up; 1918 goto clean_up;
1609 } 1919 }
1610 1920
@@ -1640,13 +1950,18 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1640 clk_enable(r8a66597->clk); 1950 clk_enable(r8a66597->clk);
1641 } 1951 }
1642#endif 1952#endif
1953 if (r8a66597->pdata->sudmac) {
1954 ret = r8a66597_sudmac_ioremap(r8a66597, pdev);
1955 if (ret < 0)
1956 goto clean_up2;
1957 }
1643 1958
1644 disable_controller(r8a66597); /* make sure controller is disabled */ 1959 disable_controller(r8a66597); /* make sure controller is disabled */
1645 1960
1646 ret = request_irq(irq, r8a66597_irq, IRQF_DISABLED | IRQF_SHARED, 1961 ret = request_irq(irq, r8a66597_irq, IRQF_SHARED,
1647 udc_name, r8a66597); 1962 udc_name, r8a66597);
1648 if (ret < 0) { 1963 if (ret < 0) {
1649 printk(KERN_ERR "request_irq error (%d)\n", ret); 1964 dev_err(&pdev->dev, "request_irq error (%d)\n", ret);
1650 goto clean_up2; 1965 goto clean_up2;
1651 } 1966 }
1652 1967
@@ -1672,13 +1987,10 @@ static int __init r8a66597_probe(struct platform_device *pdev)
1672 r8a66597->ep[0].fifoaddr = CFIFO; 1987 r8a66597->ep[0].fifoaddr = CFIFO;
1673 r8a66597->ep[0].fifosel = CFIFOSEL; 1988 r8a66597->ep[0].fifosel = CFIFOSEL;
1674 r8a66597->ep[0].fifoctr = CFIFOCTR; 1989 r8a66597->ep[0].fifoctr = CFIFOCTR;
1675 r8a66597->ep[0].fifotrn = 0;
1676 r8a66597->ep[0].pipectr = get_pipectr_addr(0); 1990 r8a66597->ep[0].pipectr = get_pipectr_addr(0);
1677 r8a66597->pipenum2ep[0] = &r8a66597->ep[0]; 1991 r8a66597->pipenum2ep[0] = &r8a66597->ep[0];
1678 r8a66597->epaddr2ep[0] = &r8a66597->ep[0]; 1992 r8a66597->epaddr2ep[0] = &r8a66597->ep[0];
1679 1993
1680 the_controller = r8a66597;
1681
1682 r8a66597->ep0_req = r8a66597_alloc_request(&r8a66597->ep[0].ep, 1994 r8a66597->ep0_req = r8a66597_alloc_request(&r8a66597->ep[0].ep,
1683 GFP_KERNEL); 1995 GFP_KERNEL);
1684 if (r8a66597->ep0_req == NULL) 1996 if (r8a66597->ep0_req == NULL)
@@ -1705,6 +2017,8 @@ clean_up2:
1705#endif 2017#endif
1706clean_up: 2018clean_up:
1707 if (r8a66597) { 2019 if (r8a66597) {
2020 if (r8a66597->sudmac_reg)
2021 iounmap(r8a66597->sudmac_reg);
1708 if (r8a66597->ep0_req) 2022 if (r8a66597->ep0_req)
1709 r8a66597_free_request(&r8a66597->ep[0].ep, 2023 r8a66597_free_request(&r8a66597->ep[0].ep,
1710 r8a66597->ep0_req); 2024 r8a66597->ep0_req);