-
Notifications
You must be signed in to change notification settings - Fork 2
/
r8a66597-udc.c
1691 lines (1425 loc) · 41.1 KB
/
r8a66597-udc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* R8A66597 UDC (USB gadget)
*
* Copyright (C) 2006-2009 Renesas Solutions Corp.
*
* Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
#include "r8a66597-udc.h"
#define DRIVER_VERSION "2009-08-18"
static const char udc_name[] = "r8a66597_udc";
static const char *r8a66597_ep_name[] = {
"ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7",
"ep8", "ep9",
};
static void disable_controller(struct r8a66597 *r8a66597);
static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req);
static void irq_packet_write(struct r8a66597_ep *ep,
struct r8a66597_request *req);
static int r8a66597_queue(struct usb_ep *_ep, struct usb_request *_req,
gfp_t gfp_flags);
static void transfer_complete(struct r8a66597_ep *ep,
struct r8a66597_request *req, int status);
/*-------------------------------------------------------------------------*/
static inline u16 get_usb_speed(struct r8a66597 *r8a66597)
{
return r8a66597_read(r8a66597, DVSTCTR0) & RHST;
}
static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
unsigned long reg)
{
u16 tmp;
tmp = r8a66597_read(r8a66597, INTENB0);
r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
INTENB0);
r8a66597_bset(r8a66597, (1 << pipenum), reg);
r8a66597_write(r8a66597, tmp, INTENB0);
}
static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
unsigned long reg)
{
u16 tmp;
tmp = r8a66597_read(r8a66597, INTENB0);
r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE,
INTENB0);
r8a66597_bclr(r8a66597, (1 << pipenum), reg);
r8a66597_write(r8a66597, tmp, INTENB0);
}
static void r8a66597_usb_connect(struct r8a66597 *r8a66597)
{
r8a66597_bset(r8a66597, CTRE, INTENB0);
r8a66597_bset(r8a66597, BEMPE | BRDYE, INTENB0);
r8a66597_bset(r8a66597, DPRPU, SYSCFG0);
}
static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597)
__releases(r8a66597->lock)
__acquires(r8a66597->lock)
{
r8a66597_bclr(r8a66597, CTRE, INTENB0);
r8a66597_bclr(r8a66597, BEMPE | BRDYE, INTENB0);
r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
r8a66597->gadget.speed = USB_SPEED_UNKNOWN;
spin_unlock(&r8a66597->lock);
r8a66597->driver->disconnect(&r8a66597->gadget);
spin_lock(&r8a66597->lock);
disable_controller(r8a66597);
INIT_LIST_HEAD(&r8a66597->ep[0].queue);
}
static inline u16 control_reg_get_pid(struct r8a66597 *r8a66597, u16 pipenum)
{
u16 pid = 0;
unsigned long offset;
if (pipenum == 0)
pid = r8a66597_read(r8a66597, DCPCTR) & PID;
else if (pipenum < R8A66597_MAX_NUM_PIPE) {
offset = get_pipectr_addr(pipenum);
pid = r8a66597_read(r8a66597, offset) & PID;
} else
printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
return pid;
}
static inline void control_reg_set_pid(struct r8a66597 *r8a66597, u16 pipenum,
u16 pid)
{
unsigned long offset;
if (pipenum == 0)
r8a66597_mdfy(r8a66597, pid, PID, DCPCTR);
else if (pipenum < R8A66597_MAX_NUM_PIPE) {
offset = get_pipectr_addr(pipenum);
r8a66597_mdfy(r8a66597, pid, PID, offset);
} else
printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
}
static inline void pipe_start(struct r8a66597 *r8a66597, u16 pipenum)
{
control_reg_set_pid(r8a66597, pipenum, PID_BUF);
}
static inline void pipe_stop(struct r8a66597 *r8a66597, u16 pipenum)
{
control_reg_set_pid(r8a66597, pipenum, PID_NAK);
}
static inline void pipe_stall(struct r8a66597 *r8a66597, u16 pipenum)
{
control_reg_set_pid(r8a66597, pipenum, PID_STALL);
}
static inline u16 control_reg_get(struct r8a66597 *r8a66597, u16 pipenum)
{
u16 ret = 0;
unsigned long offset;
if (pipenum == 0)
ret = r8a66597_read(r8a66597, DCPCTR);
else if (pipenum < R8A66597_MAX_NUM_PIPE) {
offset = get_pipectr_addr(pipenum);
ret = r8a66597_read(r8a66597, offset);
} else
printk(KERN_ERR "unexpect pipe num (%d)\n", pipenum);
return ret;
}
static inline void control_reg_sqclr(struct r8a66597 *r8a66597, u16 pipenum)
{
unsigned long offset;
pipe_stop(r8a66597, pipenum);
if (pipenum == 0)
r8a66597_bset(r8a66597, SQCLR, DCPCTR);
else if (pipenum < R8A66597_MAX_NUM_PIPE) {
offset = get_pipectr_addr(pipenum);
r8a66597_bset(r8a66597, SQCLR, offset);
} else
printk(KERN_ERR "unexpect pipe num(%d)\n", pipenum);
}
static inline int get_buffer_size(struct r8a66597 *r8a66597, u16 pipenum)
{
u16 tmp;
int size;
if (pipenum == 0) {
tmp = r8a66597_read(r8a66597, DCPCFG);
if ((tmp & R8A66597_CNTMD) != 0)
size = 256;
else {
tmp = r8a66597_read(r8a66597, DCPMAXP);
size = tmp & MAXP;
}
} else {
r8a66597_write(r8a66597, pipenum, PIPESEL);
tmp = r8a66597_read(r8a66597, PIPECFG);
if ((tmp & R8A66597_CNTMD) != 0) {
tmp = r8a66597_read(r8a66597, PIPEBUF);
size = ((tmp >> 10) + 1) * 64;
} else {
tmp = r8a66597_read(r8a66597, PIPEMAXP);
size = tmp & MXPS;
}
}
return size;
}
static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
{
if (r8a66597->pdata->on_chip)
return MBW_32;
else
return MBW_16;
}
static inline void pipe_change(struct r8a66597 *r8a66597, u16 pipenum)
{
struct r8a66597_ep *ep = r8a66597->pipenum2ep[pipenum];
if (ep->use_dma)
return;
r8a66597_mdfy(r8a66597, pipenum, CURPIPE, ep->fifosel);
ndelay(450);
r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
}
static int pipe_buffer_setting(struct r8a66597 *r8a66597,
struct r8a66597_pipe_info *info)
{
u16 bufnum = 0, buf_bsize = 0;
u16 pipecfg = 0;
if (info->pipe == 0)
return -EINVAL;
r8a66597_write(r8a66597, info->pipe, PIPESEL);
if (info->dir_in)
pipecfg |= R8A66597_DIR;
pipecfg |= info->type;
pipecfg |= info->epnum;
switch (info->type) {
case R8A66597_INT:
bufnum = 4 + (info->pipe - R8A66597_BASE_PIPENUM_INT);
buf_bsize = 0;
break;
case R8A66597_BULK:
/* isochronous pipes may be used as bulk pipes */
if (info->pipe > R8A66597_BASE_PIPENUM_BULK)
bufnum = info->pipe - R8A66597_BASE_PIPENUM_BULK;
else
bufnum = info->pipe - R8A66597_BASE_PIPENUM_ISOC;
bufnum = R8A66597_BASE_BUFNUM + (bufnum * 16);
buf_bsize = 7;
pipecfg |= R8A66597_DBLB;
if (!info->dir_in)
pipecfg |= R8A66597_SHTNAK;
break;
case R8A66597_ISO:
bufnum = R8A66597_BASE_BUFNUM +
(info->pipe - R8A66597_BASE_PIPENUM_ISOC) * 16;
buf_bsize = 7;
break;
}
if (buf_bsize && ((bufnum + 16) >= R8A66597_MAX_BUFNUM)) {
pr_err(KERN_ERR "r8a66597 pipe memory is insufficient\n");
return -ENOMEM;
}
r8a66597_write(r8a66597, pipecfg, PIPECFG);
r8a66597_write(r8a66597, (buf_bsize << 10) | (bufnum), PIPEBUF);
r8a66597_write(r8a66597, info->maxpacket, PIPEMAXP);
if (info->interval)
info->interval--;
r8a66597_write(r8a66597, info->interval, PIPEPERI);
return 0;
}
static void pipe_buffer_release(struct r8a66597 *r8a66597,
struct r8a66597_pipe_info *info)
{
if (info->pipe == 0)
return;
if (is_bulk_pipe(info->pipe))
r8a66597->bulk--;
else if (is_interrupt_pipe(info->pipe))
r8a66597->interrupt--;
else if (is_isoc_pipe(info->pipe)) {
r8a66597->isochronous--;
if (info->type == R8A66597_BULK)
r8a66597->bulk--;
} else
printk(KERN_ERR "ep_release: unexpect pipenum (%d)\n",
info->pipe);
}
static void pipe_initialize(struct r8a66597_ep *ep)
{
struct r8a66597 *r8a66597 = ep->r8a66597;
r8a66597_mdfy(r8a66597, 0, CURPIPE, ep->fifosel);
r8a66597_write(r8a66597, ACLRM, ep->pipectr);
r8a66597_write(r8a66597, 0, ep->pipectr);
r8a66597_write(r8a66597, SQCLR, ep->pipectr);
if (ep->use_dma) {
r8a66597_mdfy(r8a66597, ep->pipenum, CURPIPE, ep->fifosel);
ndelay(450);
r8a66597_bset(r8a66597, mbw_value(r8a66597), ep->fifosel);
}
}
static void r8a66597_ep_setting(struct r8a66597 *r8a66597,
struct r8a66597_ep *ep,
const struct usb_endpoint_descriptor *desc,
u16 pipenum, int dma)
{
ep->use_dma = 0;
ep->fifoaddr = CFIFO;
ep->fifosel = CFIFOSEL;
ep->fifoctr = CFIFOCTR;
ep->fifotrn = 0;
ep->pipectr = get_pipectr_addr(pipenum);
ep->pipenum = pipenum;
ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
r8a66597->pipenum2ep[pipenum] = ep;
r8a66597->epaddr2ep[desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK]
= ep;
INIT_LIST_HEAD(&ep->queue);
}
static void r8a66597_ep_release(struct r8a66597_ep *ep)
{
struct r8a66597 *r8a66597 = ep->r8a66597;
u16 pipenum = ep->pipenum;
if (pipenum == 0)
return;
if (ep->use_dma)
r8a66597->num_dma--;
ep->pipenum = 0;
ep->busy = 0;
ep->use_dma = 0;
}
static int alloc_pipe_config(struct r8a66597_ep *ep,
const struct usb_endpoint_descriptor *desc)
{
struct r8a66597 *r8a66597 = ep->r8a66597;
struct r8a66597_pipe_info info;
int dma = 0;
unsigned char *counter;
int ret;
ep->desc = desc;
if (ep->pipenum) /* already allocated pipe */
return 0;
switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
case USB_ENDPOINT_XFER_BULK:
if (r8a66597->bulk >= R8A66597_MAX_NUM_BULK) {
if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
printk(KERN_ERR "bulk pipe is insufficient\n");
return -ENODEV;
} else {
info.pipe = R8A66597_BASE_PIPENUM_ISOC
+ r8a66597->isochronous;
counter = &r8a66597->isochronous;
}
} else {
info.pipe = R8A66597_BASE_PIPENUM_BULK + r8a66597->bulk;
counter = &r8a66597->bulk;
}
info.type = R8A66597_BULK;
dma = 1;
break;
case USB_ENDPOINT_XFER_INT:
if (r8a66597->interrupt >= R8A66597_MAX_NUM_INT) {
printk(KERN_ERR "interrupt pipe is insufficient\n");
return -ENODEV;
}
info.pipe = R8A66597_BASE_PIPENUM_INT + r8a66597->interrupt;
info.type = R8A66597_INT;
counter = &r8a66597->interrupt;
break;
case USB_ENDPOINT_XFER_ISOC:
if (r8a66597->isochronous >= R8A66597_MAX_NUM_ISOC) {
printk(KERN_ERR "isochronous pipe is insufficient\n");
return -ENODEV;
}
info.pipe = R8A66597_BASE_PIPENUM_ISOC + r8a66597->isochronous;
info.type = R8A66597_ISO;
counter = &r8a66597->isochronous;
break;
default:
printk(KERN_ERR "unexpect xfer type\n");
return -EINVAL;
}
ep->type = info.type;
info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
info.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
info.interval = desc->bInterval;
if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
info.dir_in = 1;
else
info.dir_in = 0;
ret = pipe_buffer_setting(r8a66597, &info);
if (ret < 0) {
printk(KERN_ERR "pipe_buffer_setting fail\n");
return ret;
}
(*counter)++;
if ((counter == &r8a66597->isochronous) && info.type == R8A66597_BULK)
r8a66597->bulk++;
r8a66597_ep_setting(r8a66597, ep, desc, info.pipe, dma);
pipe_initialize(ep);
return 0;
}
static int free_pipe_config(struct r8a66597_ep *ep)
{
struct r8a66597 *r8a66597 = ep->r8a66597;
struct r8a66597_pipe_info info;
info.pipe = ep->pipenum;
info.type = ep->type;
pipe_buffer_release(r8a66597, &info);
r8a66597_ep_release(ep);
return 0;
}
/*-------------------------------------------------------------------------*/
static void pipe_irq_enable(struct r8a66597 *r8a66597, u16 pipenum)
{
enable_irq_ready(r8a66597, pipenum);
enable_irq_nrdy(r8a66597, pipenum);
}
static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
{
disable_irq_ready(r8a66597, pipenum);
disable_irq_nrdy(r8a66597, pipenum);
}
/* if complete is true, gadget driver complete function is not call */
static void control_end(struct r8a66597 *r8a66597, unsigned ccpl)
{
r8a66597->ep[0].internal_ccpl = ccpl;
pipe_start(r8a66597, 0);
r8a66597_bset(r8a66597, CCPL, DCPCTR);
}
static void start_ep0_write(struct r8a66597_ep *ep,
struct r8a66597_request *req)
{
struct r8a66597 *r8a66597 = ep->r8a66597;
pipe_change(r8a66597, ep->pipenum);
r8a66597_mdfy(r8a66597, ISEL, (ISEL | CURPIPE), CFIFOSEL);
r8a66597_write(r8a66597, BCLR, ep->fifoctr);
if (req->req.length == 0) {
r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
pipe_start(r8a66597, 0);
transfer_complete(ep, req, 0);
} else {
r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
irq_ep0_write(ep, req);
}
}
static void start_packet_write(struct r8a66597_ep *ep,
struct r8a66597_request *req)
{
struct r8a66597 *r8a66597 = ep->r8a66597;
u16 tmp;
pipe_change(r8a66597, ep->pipenum);
disable_irq_empty(r8a66597, ep->pipenum);
pipe_start(r8a66597, ep->pipenum);
tmp = r8a66597_read(r8a66597, ep->fifoctr);
if (unlikely((tmp & FRDY) == 0))
pipe_irq_enable(r8a66597, ep->pipenum);
else
irq_packet_write(ep, req);
}
static void start_packet_read(struct r8a66597_ep *ep,
struct r8a66597_request *req)
{
struct r8a66597 *r8a66597 = ep->r8a66597;
u16 pipenum = ep->pipenum;
if (ep->pipenum == 0) {
r8a66597_mdfy(r8a66597, 0, (ISEL | CURPIPE), CFIFOSEL);
r8a66597_write(r8a66597, BCLR, ep->fifoctr);
pipe_start(r8a66597, pipenum);
pipe_irq_enable(r8a66597, pipenum);
} else {
if (ep->use_dma) {
r8a66597_bset(r8a66597, TRCLR, ep->fifosel);
pipe_change(r8a66597, pipenum);
r8a66597_bset(r8a66597, TRENB, ep->fifosel);
r8a66597_write(r8a66597,
(req->req.length + ep->ep.maxpacket - 1)
/ ep->ep.maxpacket,
ep->fifotrn);
}
pipe_start(r8a66597, pipenum); /* trigger once */
pipe_irq_enable(r8a66597, pipenum);
}
}
static void start_packet(struct r8a66597_ep *ep, struct r8a66597_request *req)
{
if (ep->desc->bEndpointAddress & USB_DIR_IN)
start_packet_write(ep, req);
else
start_packet_read(ep, req);
}
static void start_ep0(struct r8a66597_ep *ep, struct r8a66597_request *req)
{
u16 ctsq;
ctsq = r8a66597_read(ep->r8a66597, INTSTS0) & CTSQ;
switch (ctsq) {
case CS_RDDS:
start_ep0_write(ep, req);
break;
case CS_WRDS:
start_packet_read(ep, req);
break;
case CS_WRND:
control_end(ep->r8a66597, 0);
break;
default:
printk(KERN_ERR "start_ep0: unexpect ctsq(%x)\n", ctsq);
break;
}
}
static void init_controller(struct r8a66597 *r8a66597)
{
u16 vif = r8a66597->pdata->vif ? LDRV : 0;
u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
if (r8a66597->pdata->on_chip) {
r8a66597_bset(r8a66597, 0x04, SYSCFG1);
r8a66597_bset(r8a66597, HSE, SYSCFG0);
r8a66597_bclr(r8a66597, USBE, SYSCFG0);
r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
r8a66597_bset(r8a66597, USBE, SYSCFG0);
r8a66597_bset(r8a66597, SCKE, SYSCFG0);
r8a66597_bset(r8a66597, irq_sense, INTENB1);
r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
DMA0CFG);
} else {
r8a66597_bset(r8a66597, vif | endian, PINCFG);
r8a66597_bset(r8a66597, HSE, SYSCFG0); /* High spd */
r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
XTAL, SYSCFG0);
r8a66597_bclr(r8a66597, USBE, SYSCFG0);
r8a66597_bclr(r8a66597, DPRPU, SYSCFG0);
r8a66597_bset(r8a66597, USBE, SYSCFG0);
r8a66597_bset(r8a66597, XCKE, SYSCFG0);
msleep(3);
r8a66597_bset(r8a66597, PLLC, SYSCFG0);
msleep(1);
r8a66597_bset(r8a66597, SCKE, SYSCFG0);
r8a66597_bset(r8a66597, irq_sense, INTENB1);
r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR,
DMA0CFG);
}
}
static void disable_controller(struct r8a66597 *r8a66597)
{
if (r8a66597->pdata->on_chip) {
r8a66597_bset(r8a66597, SCKE, SYSCFG0);
/* disable interrupts */
r8a66597_write(r8a66597, 0, INTENB0);
r8a66597_write(r8a66597, 0, INTENB1);
r8a66597_write(r8a66597, 0, BRDYENB);
r8a66597_write(r8a66597, 0, BEMPENB);
r8a66597_write(r8a66597, 0, NRDYENB);
/* clear status */
r8a66597_write(r8a66597, 0, BRDYSTS);
r8a66597_write(r8a66597, 0, NRDYSTS);
r8a66597_write(r8a66597, 0, BEMPSTS);
r8a66597_bclr(r8a66597, USBE, SYSCFG0);
r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
} else {
r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
udelay(1);
r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
udelay(1);
udelay(1);
r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
}
}
static void r8a66597_start_xclock(struct r8a66597 *r8a66597)
{
u16 tmp;
if (!r8a66597->pdata->on_chip) {
tmp = r8a66597_read(r8a66597, SYSCFG0);
if (!(tmp & XCKE))
r8a66597_bset(r8a66597, XCKE, SYSCFG0);
}
}
static struct r8a66597_request *get_request_from_ep(struct r8a66597_ep *ep)
{
return list_entry(ep->queue.next, struct r8a66597_request, queue);
}
/*-------------------------------------------------------------------------*/
static void transfer_complete(struct r8a66597_ep *ep,
struct r8a66597_request *req, int status)
__releases(r8a66597->lock)
__acquires(r8a66597->lock)
{
int restart = 0;
if (unlikely(ep->pipenum == 0)) {
if (ep->internal_ccpl) {
ep->internal_ccpl = 0;
return;
}
}
list_del_init(&req->queue);
if (ep->r8a66597->gadget.speed == USB_SPEED_UNKNOWN)
req->req.status = -ESHUTDOWN;
else
req->req.status = status;
if (!list_empty(&ep->queue))
restart = 1;
spin_unlock(&ep->r8a66597->lock);
req->req.complete(&ep->ep, &req->req);
spin_lock(&ep->r8a66597->lock);
if (restart) {
req = get_request_from_ep(ep);
if (ep->desc)
start_packet(ep, req);
}
}
static void irq_ep0_write(struct r8a66597_ep *ep, struct r8a66597_request *req)
{
int i;
u16 tmp;
unsigned bufsize;
size_t size;
void *buf;
u16 pipenum = ep->pipenum;
struct r8a66597 *r8a66597 = ep->r8a66597;
pipe_change(r8a66597, pipenum);
r8a66597_bset(r8a66597, ISEL, ep->fifosel);
i = 0;
do {
tmp = r8a66597_read(r8a66597, ep->fifoctr);
if (i++ > 100000) {
printk(KERN_ERR "pipe0 is busy. maybe cpu i/o bus"
"conflict. please power off this controller.");
return;
}
ndelay(1);
} while ((tmp & FRDY) == 0);
/* prepare parameters */
bufsize = get_buffer_size(r8a66597, pipenum);
buf = req->req.buf + req->req.actual;
size = min(bufsize, req->req.length - req->req.actual);
/* write fifo */
if (req->req.buf) {
if (size > 0)
r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
}
/* update parameters */
req->req.actual += size;
/* check transfer finish */
if ((!req->req.zero && (req->req.actual == req->req.length))
|| (size % ep->ep.maxpacket)
|| (size == 0)) {
disable_irq_ready(r8a66597, pipenum);
disable_irq_empty(r8a66597, pipenum);
} else {
disable_irq_ready(r8a66597, pipenum);
enable_irq_empty(r8a66597, pipenum);
}
pipe_start(r8a66597, pipenum);
}
static void irq_packet_write(struct r8a66597_ep *ep,
struct r8a66597_request *req)
{
u16 tmp;
unsigned bufsize;
size_t size;
void *buf;
u16 pipenum = ep->pipenum;
struct r8a66597 *r8a66597 = ep->r8a66597;
pipe_change(r8a66597, pipenum);
tmp = r8a66597_read(r8a66597, ep->fifoctr);
if (unlikely((tmp & FRDY) == 0)) {
pipe_stop(r8a66597, pipenum);
pipe_irq_disable(r8a66597, pipenum);
printk(KERN_ERR "write fifo not ready. pipnum=%d\n", pipenum);
return;
}
/* prepare parameters */
bufsize = get_buffer_size(r8a66597, pipenum);
buf = req->req.buf + req->req.actual;
size = min(bufsize, req->req.length - req->req.actual);
/* write fifo */
if (req->req.buf) {
r8a66597_write_fifo(r8a66597, ep->fifoaddr, buf, size);
if ((size == 0)
|| ((size % ep->ep.maxpacket) != 0)
|| ((bufsize != ep->ep.maxpacket)
&& (bufsize > size)))
r8a66597_bset(r8a66597, BVAL, ep->fifoctr);
}
/* update parameters */
req->req.actual += size;
/* check transfer finish */
if ((!req->req.zero && (req->req.actual == req->req.length))
|| (size % ep->ep.maxpacket)
|| (size == 0)) {
disable_irq_ready(r8a66597, pipenum);
enable_irq_empty(r8a66597, pipenum);
} else {
disable_irq_empty(r8a66597, pipenum);
pipe_irq_enable(r8a66597, pipenum);
}
}
static void irq_packet_read(struct r8a66597_ep *ep,
struct r8a66597_request *req)
{
u16 tmp;
int rcv_len, bufsize, req_len;
int size;
void *buf;
u16 pipenum = ep->pipenum;
struct r8a66597 *r8a66597 = ep->r8a66597;
int finish = 0;
pipe_change(r8a66597, pipenum);
tmp = r8a66597_read(r8a66597, ep->fifoctr);
if (unlikely((tmp & FRDY) == 0)) {
req->req.status = -EPIPE;
pipe_stop(r8a66597, pipenum);
pipe_irq_disable(r8a66597, pipenum);
printk(KERN_ERR "read fifo not ready");
return;
}
/* prepare parameters */
rcv_len = tmp & DTLN;
bufsize = get_buffer_size(r8a66597, pipenum);
buf = req->req.buf + req->req.actual;
req_len = req->req.length - req->req.actual;
if (rcv_len < bufsize)
size = min(rcv_len, req_len);
else
size = min(bufsize, req_len);
/* update parameters */
req->req.actual += size;
/* check transfer finish */
if ((!req->req.zero && (req->req.actual == req->req.length))
|| (size % ep->ep.maxpacket)
|| (size == 0)) {
pipe_stop(r8a66597, pipenum);
pipe_irq_disable(r8a66597, pipenum);
finish = 1;
}
/* read fifo */
if (req->req.buf) {
if (size == 0)
r8a66597_write(r8a66597, BCLR, ep->fifoctr);
else
r8a66597_read_fifo(r8a66597, ep->fifoaddr, buf, size);
}
if ((ep->pipenum != 0) && finish)
transfer_complete(ep, req, 0);
}
static void irq_pipe_ready(struct r8a66597 *r8a66597, u16 status, u16 enb)
{
u16 check;
u16 pipenum;
struct r8a66597_ep *ep;
struct r8a66597_request *req;
if ((status & BRDY0) && (enb & BRDY0)) {
r8a66597_write(r8a66597, ~BRDY0, BRDYSTS);
r8a66597_mdfy(r8a66597, 0, CURPIPE, CFIFOSEL);
ep = &r8a66597->ep[0];
req = get_request_from_ep(ep);
irq_packet_read(ep, req);
} else {
for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
check = 1 << pipenum;
if ((status & check) && (enb & check)) {
r8a66597_write(r8a66597, ~check, BRDYSTS);
ep = r8a66597->pipenum2ep[pipenum];
req = get_request_from_ep(ep);
if (ep->desc->bEndpointAddress & USB_DIR_IN)
irq_packet_write(ep, req);
else
irq_packet_read(ep, req);
}
}
}
}
static void irq_pipe_empty(struct r8a66597 *r8a66597, u16 status, u16 enb)
{
u16 tmp;
u16 check;
u16 pipenum;
struct r8a66597_ep *ep;
struct r8a66597_request *req;
if ((status & BEMP0) && (enb & BEMP0)) {
r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
ep = &r8a66597->ep[0];
req = get_request_from_ep(ep);
irq_ep0_write(ep, req);
} else {
for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
check = 1 << pipenum;
if ((status & check) && (enb & check)) {
r8a66597_write(r8a66597, ~check, BEMPSTS);
tmp = control_reg_get(r8a66597, pipenum);
if ((tmp & INBUFM) == 0) {
disable_irq_empty(r8a66597, pipenum);
pipe_irq_disable(r8a66597, pipenum);
pipe_stop(r8a66597, pipenum);
ep = r8a66597->pipenum2ep[pipenum];
req = get_request_from_ep(ep);
if (!list_empty(&ep->queue))
transfer_complete(ep, req, 0);
}
}
}
}
}
static void get_status(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
__releases(r8a66597->lock)
__acquires(r8a66597->lock)
{
struct r8a66597_ep *ep;
u16 pid;
u16 status = 0;
u16 w_index = le16_to_cpu(ctrl->wIndex);
switch (ctrl->bRequestType & USB_RECIP_MASK) {
case USB_RECIP_DEVICE:
status = 1 << USB_DEVICE_SELF_POWERED;
break;
case USB_RECIP_INTERFACE:
status = 0;
break;
case USB_RECIP_ENDPOINT:
ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
pid = control_reg_get_pid(r8a66597, ep->pipenum);
if (pid == PID_STALL)
status = 1 << USB_ENDPOINT_HALT;
else
status = 0;
break;
default:
pipe_stall(r8a66597, 0);
return; /* exit */
}
r8a66597->ep0_data = cpu_to_le16(status);
r8a66597->ep0_req->buf = &r8a66597->ep0_data;
r8a66597->ep0_req->length = 2;
/* AV: what happens if we get called again before that gets through? */
spin_unlock(&r8a66597->lock);
r8a66597_queue(r8a66597->gadget.ep0, r8a66597->ep0_req, GFP_KERNEL);
spin_lock(&r8a66597->lock);
}
static void clear_feature(struct r8a66597 *r8a66597,
struct usb_ctrlrequest *ctrl)
{
switch (ctrl->bRequestType & USB_RECIP_MASK) {
case USB_RECIP_DEVICE:
control_end(r8a66597, 1);
break;
case USB_RECIP_INTERFACE:
control_end(r8a66597, 1);
break;
case USB_RECIP_ENDPOINT: {
struct r8a66597_ep *ep;
struct r8a66597_request *req;
u16 w_index = le16_to_cpu(ctrl->wIndex);
ep = r8a66597->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
if (!ep->wedge) {
pipe_stop(r8a66597, ep->pipenum);
control_reg_sqclr(r8a66597, ep->pipenum);
spin_unlock(&r8a66597->lock);
usb_ep_clear_halt(&ep->ep);
spin_lock(&r8a66597->lock);
}
control_end(r8a66597, 1);
req = get_request_from_ep(ep);
if (ep->busy) {
ep->busy = 0;
if (list_empty(&ep->queue))
break;
start_packet(ep, req);
} else if (!list_empty(&ep->queue))
pipe_start(r8a66597, ep->pipenum);
}
break;
default:
pipe_stall(r8a66597, 0);
break;
}
}
static void set_feature(struct r8a66597 *r8a66597, struct usb_ctrlrequest *ctrl)
{
switch (ctrl->bRequestType & USB_RECIP_MASK) {