1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/spinlock.h>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
20#include <linux/delay.h>
21#include <linux/io.h>
22#include <linux/slab.h>
23#include <linux/clk.h>
24#include <linux/err.h>
25#include <linux/usb/ch9.h>
26#include <linux/usb/gadget.h>
27#include <linux/usb/otg.h>
28#include <linux/prefetch.h>
29#include <linux/platform_data/s3c-hsudc.h>
30#include <linux/regulator/consumer.h>
31#include <linux/pm_runtime.h>
32
33#define S3C_HSUDC_REG(x) (x)
34
35
36#define S3C_IR S3C_HSUDC_REG(0x00)
37#define S3C_EIR S3C_HSUDC_REG(0x04)
38#define S3C_EIR_EP0 (1<<0)
39#define S3C_EIER S3C_HSUDC_REG(0x08)
40#define S3C_FAR S3C_HSUDC_REG(0x0c)
41#define S3C_FNR S3C_HSUDC_REG(0x10)
42#define S3C_EDR S3C_HSUDC_REG(0x14)
43#define S3C_TR S3C_HSUDC_REG(0x18)
44#define S3C_SSR S3C_HSUDC_REG(0x1c)
45#define S3C_SSR_DTZIEN_EN (0xff8f)
46#define S3C_SSR_ERR (0xff80)
47#define S3C_SSR_VBUSON (1 << 8)
48#define S3C_SSR_HSP (1 << 4)
49#define S3C_SSR_SDE (1 << 3)
50#define S3C_SSR_RESUME (1 << 2)
51#define S3C_SSR_SUSPEND (1 << 1)
52#define S3C_SSR_RESET (1 << 0)
53#define S3C_SCR S3C_HSUDC_REG(0x20)
54#define S3C_SCR_DTZIEN_EN (1 << 14)
55#define S3C_SCR_RRD_EN (1 << 5)
56#define S3C_SCR_SUS_EN (1 << 1)
57#define S3C_SCR_RST_EN (1 << 0)
58#define S3C_EP0SR S3C_HSUDC_REG(0x24)
59#define S3C_EP0SR_EP0_LWO (1 << 6)
60#define S3C_EP0SR_STALL (1 << 4)
61#define S3C_EP0SR_TX_SUCCESS (1 << 1)
62#define S3C_EP0SR_RX_SUCCESS (1 << 0)
63#define S3C_EP0CR S3C_HSUDC_REG(0x28)
64#define S3C_BR(_x) S3C_HSUDC_REG(0x60 + (_x * 4))
65
66
67#define S3C_ESR S3C_HSUDC_REG(0x2c)
68#define S3C_ESR_FLUSH (1 << 6)
69#define S3C_ESR_STALL (1 << 5)
70#define S3C_ESR_LWO (1 << 4)
71#define S3C_ESR_PSIF_ONE (1 << 2)
72#define S3C_ESR_PSIF_TWO (2 << 2)
73#define S3C_ESR_TX_SUCCESS (1 << 1)
74#define S3C_ESR_RX_SUCCESS (1 << 0)
75#define S3C_ECR S3C_HSUDC_REG(0x30)
76#define S3C_ECR_DUEN (1 << 7)
77#define S3C_ECR_FLUSH (1 << 6)
78#define S3C_ECR_STALL (1 << 1)
79#define S3C_ECR_IEMS (1 << 0)
80#define S3C_BRCR S3C_HSUDC_REG(0x34)
81#define S3C_BWCR S3C_HSUDC_REG(0x38)
82#define S3C_MPR S3C_HSUDC_REG(0x3c)
83
84#define WAIT_FOR_SETUP (0)
85#define DATA_STATE_XMIT (1)
86#define DATA_STATE_RECV (2)
87
88static const char * const s3c_hsudc_supply_names[] = {
89 "vdda",
90 "vddi",
91 "vddosc",
92};
93
94
95
96
97
98
99
100
101
102
103
104
105struct s3c_hsudc_ep {
106 struct usb_ep ep;
107 char name[20];
108 struct s3c_hsudc *dev;
109 struct list_head queue;
110 u8 stopped;
111 u8 wedge;
112 u8 bEndpointAddress;
113 void __iomem *fifo;
114};
115
116
117
118
119
120
121struct s3c_hsudc_req {
122 struct usb_request req;
123 struct list_head queue;
124};
125
126
127
128
129
130
131
132
133
134
135
136
137
138struct s3c_hsudc {
139 struct usb_gadget gadget;
140 struct usb_gadget_driver *driver;
141 struct device *dev;
142 struct s3c24xx_hsudc_platdata *pd;
143 struct usb_phy *transceiver;
144 struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsudc_supply_names)];
145 spinlock_t lock;
146 void __iomem *regs;
147 int irq;
148 struct clk *uclk;
149 int ep0state;
150 struct s3c_hsudc_ep ep[];
151};
152
153#define ep_maxpacket(_ep) ((_ep)->ep.maxpacket)
154#define ep_is_in(_ep) ((_ep)->bEndpointAddress & USB_DIR_IN)
155#define ep_index(_ep) ((_ep)->bEndpointAddress & \
156 USB_ENDPOINT_NUMBER_MASK)
157
158static const char driver_name[] = "s3c-udc";
159static const char ep0name[] = "ep0-control";
160
161static inline struct s3c_hsudc_req *our_req(struct usb_request *req)
162{
163 return container_of(req, struct s3c_hsudc_req, req);
164}
165
166static inline struct s3c_hsudc_ep *our_ep(struct usb_ep *ep)
167{
168 return container_of(ep, struct s3c_hsudc_ep, ep);
169}
170
171static inline struct s3c_hsudc *to_hsudc(struct usb_gadget *gadget)
172{
173 return container_of(gadget, struct s3c_hsudc, gadget);
174}
175
176static inline void set_index(struct s3c_hsudc *hsudc, int ep_addr)
177{
178 ep_addr &= USB_ENDPOINT_NUMBER_MASK;
179 writel(ep_addr, hsudc->regs + S3C_IR);
180}
181
182static inline void __orr32(void __iomem *ptr, u32 val)
183{
184 writel(readl(ptr) | val, ptr);
185}
186
187
188
189
190
191
192
193static void s3c_hsudc_complete_request(struct s3c_hsudc_ep *hsep,
194 struct s3c_hsudc_req *hsreq, int status)
195{
196 unsigned int stopped = hsep->stopped;
197 struct s3c_hsudc *hsudc = hsep->dev;
198
199 list_del_init(&hsreq->queue);
200 hsreq->req.status = status;
201
202 if (!ep_index(hsep)) {
203 hsudc->ep0state = WAIT_FOR_SETUP;
204 hsep->bEndpointAddress &= ~USB_DIR_IN;
205 }
206
207 hsep->stopped = 1;
208 spin_unlock(&hsudc->lock);
209 usb_gadget_giveback_request(&hsep->ep, &hsreq->req);
210 spin_lock(&hsudc->lock);
211 hsep->stopped = stopped;
212}
213
214
215
216
217
218
219static void s3c_hsudc_nuke_ep(struct s3c_hsudc_ep *hsep, int status)
220{
221 struct s3c_hsudc_req *hsreq;
222
223 while (!list_empty(&hsep->queue)) {
224 hsreq = list_entry(hsep->queue.next,
225 struct s3c_hsudc_req, queue);
226 s3c_hsudc_complete_request(hsep, hsreq, status);
227 }
228}
229
230
231
232
233
234
235
236
237static void s3c_hsudc_stop_activity(struct s3c_hsudc *hsudc)
238{
239 struct s3c_hsudc_ep *hsep;
240 int epnum;
241
242 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
243
244 for (epnum = 0; epnum < hsudc->pd->epnum; epnum++) {
245 hsep = &hsudc->ep[epnum];
246 hsep->stopped = 1;
247 s3c_hsudc_nuke_ep(hsep, -ESHUTDOWN);
248 }
249}
250
251
252
253
254
255
256
257
258
259
260static void s3c_hsudc_read_setup_pkt(struct s3c_hsudc *hsudc, u16 *buf)
261{
262 int count;
263
264 count = readl(hsudc->regs + S3C_BRCR);
265 while (count--)
266 *buf++ = (u16)readl(hsudc->regs + S3C_BR(0));
267
268 writel(S3C_EP0SR_RX_SUCCESS, hsudc->regs + S3C_EP0SR);
269}
270
271
272
273
274
275
276
277
278
279static int s3c_hsudc_write_fifo(struct s3c_hsudc_ep *hsep,
280 struct s3c_hsudc_req *hsreq)
281{
282 u16 *buf;
283 u32 max = ep_maxpacket(hsep);
284 u32 count, length;
285 bool is_last;
286 void __iomem *fifo = hsep->fifo;
287
288 buf = hsreq->req.buf + hsreq->req.actual;
289 prefetch(buf);
290
291 length = hsreq->req.length - hsreq->req.actual;
292 length = min(length, max);
293 hsreq->req.actual += length;
294
295 writel(length, hsep->dev->regs + S3C_BWCR);
296 for (count = 0; count < length; count += 2)
297 writel(*buf++, fifo);
298
299 if (count != max) {
300 is_last = true;
301 } else {
302 if (hsreq->req.length != hsreq->req.actual || hsreq->req.zero)
303 is_last = false;
304 else
305 is_last = true;
306 }
307
308 if (is_last) {
309 s3c_hsudc_complete_request(hsep, hsreq, 0);
310 return 1;
311 }
312
313 return 0;
314}
315
316
317
318
319
320
321
322
323
324
325static int s3c_hsudc_read_fifo(struct s3c_hsudc_ep *hsep,
326 struct s3c_hsudc_req *hsreq)
327{
328 struct s3c_hsudc *hsudc = hsep->dev;
329 u32 csr, offset;
330 u16 *buf, word;
331 u32 buflen, rcnt, rlen;
332 void __iomem *fifo = hsep->fifo;
333 u32 is_short = 0;
334
335 offset = (ep_index(hsep)) ? S3C_ESR : S3C_EP0SR;
336 csr = readl(hsudc->regs + offset);
337 if (!(csr & S3C_ESR_RX_SUCCESS))
338 return -EINVAL;
339
340 buf = hsreq->req.buf + hsreq->req.actual;
341 prefetchw(buf);
342 buflen = hsreq->req.length - hsreq->req.actual;
343
344 rcnt = readl(hsudc->regs + S3C_BRCR);
345 rlen = (csr & S3C_ESR_LWO) ? (rcnt * 2 - 1) : (rcnt * 2);
346
347 hsreq->req.actual += min(rlen, buflen);
348 is_short = (rlen < hsep->ep.maxpacket);
349
350 while (rcnt-- != 0) {
351 word = (u16)readl(fifo);
352 if (buflen) {
353 *buf++ = word;
354 buflen--;
355 } else {
356 hsreq->req.status = -EOVERFLOW;
357 }
358 }
359
360 writel(S3C_ESR_RX_SUCCESS, hsudc->regs + offset);
361
362 if (is_short || hsreq->req.actual == hsreq->req.length) {
363 s3c_hsudc_complete_request(hsep, hsreq, 0);
364 return 1;
365 }
366
367 return 0;
368}
369
370
371
372
373
374
375
376
377
378static void s3c_hsudc_epin_intr(struct s3c_hsudc *hsudc, u32 ep_idx)
379{
380 struct s3c_hsudc_ep *hsep = &hsudc->ep[ep_idx];
381 struct s3c_hsudc_req *hsreq;
382 u32 csr;
383
384 csr = readl(hsudc->regs + S3C_ESR);
385 if (csr & S3C_ESR_STALL) {
386 writel(S3C_ESR_STALL, hsudc->regs + S3C_ESR);
387 return;
388 }
389
390 if (csr & S3C_ESR_TX_SUCCESS) {
391 writel(S3C_ESR_TX_SUCCESS, hsudc->regs + S3C_ESR);
392 if (list_empty(&hsep->queue))
393 return;
394
395 hsreq = list_entry(hsep->queue.next,
396 struct s3c_hsudc_req, queue);
397 if ((s3c_hsudc_write_fifo(hsep, hsreq) == 0) &&
398 (csr & S3C_ESR_PSIF_TWO))
399 s3c_hsudc_write_fifo(hsep, hsreq);
400 }
401}
402
403
404
405
406
407
408
409
410
411static void s3c_hsudc_epout_intr(struct s3c_hsudc *hsudc, u32 ep_idx)
412{
413 struct s3c_hsudc_ep *hsep = &hsudc->ep[ep_idx];
414 struct s3c_hsudc_req *hsreq;
415 u32 csr;
416
417 csr = readl(hsudc->regs + S3C_ESR);
418 if (csr & S3C_ESR_STALL) {
419 writel(S3C_ESR_STALL, hsudc->regs + S3C_ESR);
420 return;
421 }
422
423 if (csr & S3C_ESR_FLUSH) {
424 __orr32(hsudc->regs + S3C_ECR, S3C_ECR_FLUSH);
425 return;
426 }
427
428 if (csr & S3C_ESR_RX_SUCCESS) {
429 if (list_empty(&hsep->queue))
430 return;
431
432 hsreq = list_entry(hsep->queue.next,
433 struct s3c_hsudc_req, queue);
434 if (((s3c_hsudc_read_fifo(hsep, hsreq)) == 0) &&
435 (csr & S3C_ESR_PSIF_TWO))
436 s3c_hsudc_read_fifo(hsep, hsreq);
437 }
438}
439
440
441
442
443
444
445
446
447
448static int s3c_hsudc_set_halt(struct usb_ep *_ep, int value)
449{
450 struct s3c_hsudc_ep *hsep = our_ep(_ep);
451 struct s3c_hsudc *hsudc = hsep->dev;
452 struct s3c_hsudc_req *hsreq;
453 unsigned long irqflags;
454 u32 ecr;
455 u32 offset;
456
457 if (value && ep_is_in(hsep) && !list_empty(&hsep->queue))
458 return -EAGAIN;
459
460 spin_lock_irqsave(&hsudc->lock, irqflags);
461 set_index(hsudc, ep_index(hsep));
462 offset = (ep_index(hsep)) ? S3C_ECR : S3C_EP0CR;
463 ecr = readl(hsudc->regs + offset);
464
465 if (value) {
466 ecr |= S3C_ECR_STALL;
467 if (ep_index(hsep))
468 ecr |= S3C_ECR_FLUSH;
469 hsep->stopped = 1;
470 } else {
471 ecr &= ~S3C_ECR_STALL;
472 hsep->stopped = hsep->wedge = 0;
473 }
474 writel(ecr, hsudc->regs + offset);
475
476 if (ep_is_in(hsep) && !list_empty(&hsep->queue) && !value) {
477 hsreq = list_entry(hsep->queue.next,
478 struct s3c_hsudc_req, queue);
479 if (hsreq)
480 s3c_hsudc_write_fifo(hsep, hsreq);
481 }
482
483 spin_unlock_irqrestore(&hsudc->lock, irqflags);
484 return 0;
485}
486
487
488
489
490
491
492static int s3c_hsudc_set_wedge(struct usb_ep *_ep)
493{
494 struct s3c_hsudc_ep *hsep = our_ep(_ep);
495
496 if (!hsep)
497 return -EINVAL;
498
499 hsep->wedge = 1;
500 return usb_ep_set_halt(_ep);
501}
502
503
504
505
506
507
508
509static int s3c_hsudc_handle_reqfeat(struct s3c_hsudc *hsudc,
510 struct usb_ctrlrequest *ctrl)
511{
512 struct s3c_hsudc_ep *hsep;
513 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
514 u8 ep_num = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
515
516 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
517 hsep = &hsudc->ep[ep_num];
518 switch (le16_to_cpu(ctrl->wValue)) {
519 case USB_ENDPOINT_HALT:
520 if (set || !hsep->wedge)
521 s3c_hsudc_set_halt(&hsep->ep, set);
522 return 0;
523 }
524 }
525
526 return -ENOENT;
527}
528
529
530
531
532
533
534
535
536static void s3c_hsudc_process_req_status(struct s3c_hsudc *hsudc,
537 struct usb_ctrlrequest *ctrl)
538{
539 struct s3c_hsudc_ep *hsep0 = &hsudc->ep[0];
540 struct s3c_hsudc_req hsreq;
541 struct s3c_hsudc_ep *hsep;
542 __le16 reply;
543 u8 epnum;
544
545 switch (ctrl->bRequestType & USB_RECIP_MASK) {
546 case USB_RECIP_DEVICE:
547 reply = cpu_to_le16(0);
548 break;
549
550 case USB_RECIP_INTERFACE:
551 reply = cpu_to_le16(0);
552 break;
553
554 case USB_RECIP_ENDPOINT:
555 epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
556 hsep = &hsudc->ep[epnum];
557 reply = cpu_to_le16(hsep->stopped ? 1 : 0);
558 break;
559 }
560
561 INIT_LIST_HEAD(&hsreq.queue);
562 hsreq.req.length = 2;
563 hsreq.req.buf = &reply;
564 hsreq.req.actual = 0;
565 hsreq.req.complete = NULL;
566 s3c_hsudc_write_fifo(hsep0, &hsreq);
567}
568
569
570
571
572
573
574
575
576static void s3c_hsudc_process_setup(struct s3c_hsudc *hsudc)
577{
578 struct s3c_hsudc_ep *hsep = &hsudc->ep[0];
579 struct usb_ctrlrequest ctrl = {0};
580 int ret;
581
582 s3c_hsudc_nuke_ep(hsep, -EPROTO);
583 s3c_hsudc_read_setup_pkt(hsudc, (u16 *)&ctrl);
584
585 if (ctrl.bRequestType & USB_DIR_IN) {
586 hsep->bEndpointAddress |= USB_DIR_IN;
587 hsudc->ep0state = DATA_STATE_XMIT;
588 } else {
589 hsep->bEndpointAddress &= ~USB_DIR_IN;
590 hsudc->ep0state = DATA_STATE_RECV;
591 }
592
593 switch (ctrl.bRequest) {
594 case USB_REQ_SET_ADDRESS:
595 if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
596 break;
597 hsudc->ep0state = WAIT_FOR_SETUP;
598 return;
599
600 case USB_REQ_GET_STATUS:
601 if ((ctrl.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
602 break;
603 s3c_hsudc_process_req_status(hsudc, &ctrl);
604 return;
605
606 case USB_REQ_SET_FEATURE:
607 case USB_REQ_CLEAR_FEATURE:
608 if ((ctrl.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
609 break;
610 s3c_hsudc_handle_reqfeat(hsudc, &ctrl);
611 hsudc->ep0state = WAIT_FOR_SETUP;
612 return;
613 }
614
615 if (hsudc->driver) {
616 spin_unlock(&hsudc->lock);
617 ret = hsudc->driver->setup(&hsudc->gadget, &ctrl);
618 spin_lock(&hsudc->lock);
619
620 if (ctrl.bRequest == USB_REQ_SET_CONFIGURATION) {
621 hsep->bEndpointAddress &= ~USB_DIR_IN;
622 hsudc->ep0state = WAIT_FOR_SETUP;
623 }
624
625 if (ret < 0) {
626 dev_err(hsudc->dev, "setup failed, returned %d\n",
627 ret);
628 s3c_hsudc_set_halt(&hsep->ep, 1);
629 hsudc->ep0state = WAIT_FOR_SETUP;
630 hsep->bEndpointAddress &= ~USB_DIR_IN;
631 }
632 }
633}
634
635
636
637
638
639
640
641
642static void s3c_hsudc_handle_ep0_intr(struct s3c_hsudc *hsudc)
643{
644 struct s3c_hsudc_ep *hsep = &hsudc->ep[0];
645 struct s3c_hsudc_req *hsreq;
646 u32 csr = readl(hsudc->regs + S3C_EP0SR);
647 u32 ecr;
648
649 if (csr & S3C_EP0SR_STALL) {
650 ecr = readl(hsudc->regs + S3C_EP0CR);
651 ecr &= ~(S3C_ECR_STALL | S3C_ECR_FLUSH);
652 writel(ecr, hsudc->regs + S3C_EP0CR);
653
654 writel(S3C_EP0SR_STALL, hsudc->regs + S3C_EP0SR);
655 hsep->stopped = 0;
656
657 s3c_hsudc_nuke_ep(hsep, -ECONNABORTED);
658 hsudc->ep0state = WAIT_FOR_SETUP;
659 hsep->bEndpointAddress &= ~USB_DIR_IN;
660 return;
661 }
662
663 if (csr & S3C_EP0SR_TX_SUCCESS) {
664 writel(S3C_EP0SR_TX_SUCCESS, hsudc->regs + S3C_EP0SR);
665 if (ep_is_in(hsep)) {
666 if (list_empty(&hsep->queue))
667 return;
668
669 hsreq = list_entry(hsep->queue.next,
670 struct s3c_hsudc_req, queue);
671 s3c_hsudc_write_fifo(hsep, hsreq);
672 }
673 }
674
675 if (csr & S3C_EP0SR_RX_SUCCESS) {
676 if (hsudc->ep0state == WAIT_FOR_SETUP)
677 s3c_hsudc_process_setup(hsudc);
678 else {
679 if (!ep_is_in(hsep)) {
680 if (list_empty(&hsep->queue))
681 return;
682 hsreq = list_entry(hsep->queue.next,
683 struct s3c_hsudc_req, queue);
684 s3c_hsudc_read_fifo(hsep, hsreq);
685 }
686 }
687 }
688}
689
690
691
692
693
694
695
696
697
698
699static int s3c_hsudc_ep_enable(struct usb_ep *_ep,
700 const struct usb_endpoint_descriptor *desc)
701{
702 struct s3c_hsudc_ep *hsep;
703 struct s3c_hsudc *hsudc;
704 unsigned long flags;
705 u32 ecr = 0;
706
707 hsep = our_ep(_ep);
708 if (!_ep || !desc || _ep->name == ep0name
709 || desc->bDescriptorType != USB_DT_ENDPOINT
710 || hsep->bEndpointAddress != desc->bEndpointAddress
711 || ep_maxpacket(hsep) < usb_endpoint_maxp(desc))
712 return -EINVAL;
713
714 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
715 && usb_endpoint_maxp(desc) != ep_maxpacket(hsep))
716 || !desc->wMaxPacketSize)
717 return -ERANGE;
718
719 hsudc = hsep->dev;
720 if (!hsudc->driver || hsudc->gadget.speed == USB_SPEED_UNKNOWN)
721 return -ESHUTDOWN;
722
723 spin_lock_irqsave(&hsudc->lock, flags);
724
725 set_index(hsudc, hsep->bEndpointAddress);
726 ecr |= ((usb_endpoint_xfer_int(desc)) ? S3C_ECR_IEMS : S3C_ECR_DUEN);
727 writel(ecr, hsudc->regs + S3C_ECR);
728
729 hsep->stopped = hsep->wedge = 0;
730 hsep->ep.desc = desc;
731 hsep->ep.maxpacket = usb_endpoint_maxp(desc);
732
733 s3c_hsudc_set_halt(_ep, 0);
734 __set_bit(ep_index(hsep), hsudc->regs + S3C_EIER);
735
736 spin_unlock_irqrestore(&hsudc->lock, flags);
737 return 0;
738}
739
740
741
742
743
744
745
746
747static int s3c_hsudc_ep_disable(struct usb_ep *_ep)
748{
749 struct s3c_hsudc_ep *hsep = our_ep(_ep);
750 struct s3c_hsudc *hsudc = hsep->dev;
751 unsigned long flags;
752
753 if (!_ep || !hsep->ep.desc)
754 return -EINVAL;
755
756 spin_lock_irqsave(&hsudc->lock, flags);
757
758 set_index(hsudc, hsep->bEndpointAddress);
759 __clear_bit(ep_index(hsep), hsudc->regs + S3C_EIER);
760
761 s3c_hsudc_nuke_ep(hsep, -ESHUTDOWN);
762
763 hsep->ep.desc = NULL;
764 hsep->stopped = 1;
765
766 spin_unlock_irqrestore(&hsudc->lock, flags);
767 return 0;
768}
769
770
771
772
773
774
775
776
777static struct usb_request *s3c_hsudc_alloc_request(struct usb_ep *_ep,
778 gfp_t gfp_flags)
779{
780 struct s3c_hsudc_req *hsreq;
781
782 hsreq = kzalloc(sizeof(*hsreq), gfp_flags);
783 if (!hsreq)
784 return NULL;
785
786 INIT_LIST_HEAD(&hsreq->queue);
787 return &hsreq->req;
788}
789
790
791
792
793
794
795
796
797static void s3c_hsudc_free_request(struct usb_ep *ep, struct usb_request *_req)
798{
799 struct s3c_hsudc_req *hsreq;
800
801 hsreq = our_req(_req);
802 WARN_ON(!list_empty(&hsreq->queue));
803 kfree(hsreq);
804}
805
806
807
808
809
810
811
812
813
814static int s3c_hsudc_queue(struct usb_ep *_ep, struct usb_request *_req,
815 gfp_t gfp_flags)
816{
817 struct s3c_hsudc_req *hsreq;
818 struct s3c_hsudc_ep *hsep;
819 struct s3c_hsudc *hsudc;
820 unsigned long flags;
821 u32 offset;
822 u32 csr;
823
824 hsreq = our_req(_req);
825 if ((!_req || !_req->complete || !_req->buf ||
826 !list_empty(&hsreq->queue)))
827 return -EINVAL;
828
829 hsep = our_ep(_ep);
830 hsudc = hsep->dev;
831 if (!hsudc->driver || hsudc->gadget.speed == USB_SPEED_UNKNOWN)
832 return -ESHUTDOWN;
833
834 spin_lock_irqsave(&hsudc->lock, flags);
835 set_index(hsudc, hsep->bEndpointAddress);
836
837 _req->status = -EINPROGRESS;
838 _req->actual = 0;
839
840 if (!ep_index(hsep) && _req->length == 0) {
841 hsudc->ep0state = WAIT_FOR_SETUP;
842 s3c_hsudc_complete_request(hsep, hsreq, 0);
843 spin_unlock_irqrestore(&hsudc->lock, flags);
844 return 0;
845 }
846
847 if (list_empty(&hsep->queue) && !hsep->stopped) {
848 offset = (ep_index(hsep)) ? S3C_ESR : S3C_EP0SR;
849 if (ep_is_in(hsep)) {
850 csr = readl(hsudc->regs + offset);
851 if (!(csr & S3C_ESR_TX_SUCCESS) &&
852 (s3c_hsudc_write_fifo(hsep, hsreq) == 1))
853 hsreq = NULL;
854 } else {
855 csr = readl(hsudc->regs + offset);
856 if ((csr & S3C_ESR_RX_SUCCESS)
857 && (s3c_hsudc_read_fifo(hsep, hsreq) == 1))
858 hsreq = NULL;
859 }
860 }
861
862 if (hsreq)
863 list_add_tail(&hsreq->queue, &hsep->queue);
864
865 spin_unlock_irqrestore(&hsudc->lock, flags);
866 return 0;
867}
868
869
870
871
872
873
874
875
876static int s3c_hsudc_dequeue(struct usb_ep *_ep, struct usb_request *_req)
877{
878 struct s3c_hsudc_ep *hsep = our_ep(_ep);
879 struct s3c_hsudc *hsudc = hsep->dev;
880 struct s3c_hsudc_req *hsreq;
881 unsigned long flags;
882
883 hsep = our_ep(_ep);
884 if (!_ep || hsep->ep.name == ep0name)
885 return -EINVAL;
886
887 spin_lock_irqsave(&hsudc->lock, flags);
888
889 list_for_each_entry(hsreq, &hsep->queue, queue) {
890 if (&hsreq->req == _req)
891 break;
892 }
893 if (&hsreq->req != _req) {
894 spin_unlock_irqrestore(&hsudc->lock, flags);
895 return -EINVAL;
896 }
897
898 set_index(hsudc, hsep->bEndpointAddress);
899 s3c_hsudc_complete_request(hsep, hsreq, -ECONNRESET);
900
901 spin_unlock_irqrestore(&hsudc->lock, flags);
902 return 0;
903}
904
905static const struct usb_ep_ops s3c_hsudc_ep_ops = {
906 .enable = s3c_hsudc_ep_enable,
907 .disable = s3c_hsudc_ep_disable,
908 .alloc_request = s3c_hsudc_alloc_request,
909 .free_request = s3c_hsudc_free_request,
910 .queue = s3c_hsudc_queue,
911 .dequeue = s3c_hsudc_dequeue,
912 .set_halt = s3c_hsudc_set_halt,
913 .set_wedge = s3c_hsudc_set_wedge,
914};
915
916
917
918
919
920
921
922
923
924static void s3c_hsudc_initep(struct s3c_hsudc *hsudc,
925 struct s3c_hsudc_ep *hsep, int epnum)
926{
927 char *dir;
928
929 if ((epnum % 2) == 0) {
930 dir = "out";
931 } else {
932 dir = "in";
933 hsep->bEndpointAddress = USB_DIR_IN;
934 }
935
936 hsep->bEndpointAddress |= epnum;
937 if (epnum)
938 snprintf(hsep->name, sizeof(hsep->name), "ep%d%s", epnum, dir);
939 else
940 snprintf(hsep->name, sizeof(hsep->name), "%s", ep0name);
941
942 INIT_LIST_HEAD(&hsep->queue);
943 INIT_LIST_HEAD(&hsep->ep.ep_list);
944 if (epnum)
945 list_add_tail(&hsep->ep.ep_list, &hsudc->gadget.ep_list);
946
947 hsep->dev = hsudc;
948 hsep->ep.name = hsep->name;
949 usb_ep_set_maxpacket_limit(&hsep->ep, epnum ? 512 : 64);
950 hsep->ep.ops = &s3c_hsudc_ep_ops;
951 hsep->fifo = hsudc->regs + S3C_BR(epnum);
952 hsep->ep.desc = NULL;
953 hsep->stopped = 0;
954 hsep->wedge = 0;
955
956 if (epnum == 0) {
957 hsep->ep.caps.type_control = true;
958 hsep->ep.caps.dir_in = true;
959 hsep->ep.caps.dir_out = true;
960 } else {
961 hsep->ep.caps.type_iso = true;
962 hsep->ep.caps.type_bulk = true;
963 hsep->ep.caps.type_int = true;
964 }
965
966 if (epnum & 1)
967 hsep->ep.caps.dir_in = true;
968 else
969 hsep->ep.caps.dir_out = true;
970
971 set_index(hsudc, epnum);
972 writel(hsep->ep.maxpacket, hsudc->regs + S3C_MPR);
973}
974
975
976
977
978
979
980
981static void s3c_hsudc_setup_ep(struct s3c_hsudc *hsudc)
982{
983 int epnum;
984
985 hsudc->ep0state = WAIT_FOR_SETUP;
986 INIT_LIST_HEAD(&hsudc->gadget.ep_list);
987 for (epnum = 0; epnum < hsudc->pd->epnum; epnum++)
988 s3c_hsudc_initep(hsudc, &hsudc->ep[epnum], epnum);
989}
990
991
992
993
994
995
996
997static void s3c_hsudc_reconfig(struct s3c_hsudc *hsudc)
998{
999 writel(0xAA, hsudc->regs + S3C_EDR);
1000 writel(1, hsudc->regs + S3C_EIER);
1001 writel(0, hsudc->regs + S3C_TR);
1002 writel(S3C_SCR_DTZIEN_EN | S3C_SCR_RRD_EN | S3C_SCR_SUS_EN |
1003 S3C_SCR_RST_EN, hsudc->regs + S3C_SCR);
1004 writel(0, hsudc->regs + S3C_EP0CR);
1005
1006 s3c_hsudc_setup_ep(hsudc);
1007}
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017static irqreturn_t s3c_hsudc_irq(int irq, void *_dev)
1018{
1019 struct s3c_hsudc *hsudc = _dev;
1020 struct s3c_hsudc_ep *hsep;
1021 u32 ep_intr;
1022 u32 sys_status;
1023 u32 ep_idx;
1024
1025 spin_lock(&hsudc->lock);
1026
1027 sys_status = readl(hsudc->regs + S3C_SSR);
1028 ep_intr = readl(hsudc->regs + S3C_EIR) & 0x3FF;
1029
1030 if (!ep_intr && !(sys_status & S3C_SSR_DTZIEN_EN)) {
1031 spin_unlock(&hsudc->lock);
1032 return IRQ_HANDLED;
1033 }
1034
1035 if (sys_status) {
1036 if (sys_status & S3C_SSR_VBUSON)
1037 writel(S3C_SSR_VBUSON, hsudc->regs + S3C_SSR);
1038
1039 if (sys_status & S3C_SSR_ERR)
1040 writel(S3C_SSR_ERR, hsudc->regs + S3C_SSR);
1041
1042 if (sys_status & S3C_SSR_SDE) {
1043 writel(S3C_SSR_SDE, hsudc->regs + S3C_SSR);
1044 hsudc->gadget.speed = (sys_status & S3C_SSR_HSP) ?
1045 USB_SPEED_HIGH : USB_SPEED_FULL;
1046 }
1047
1048 if (sys_status & S3C_SSR_SUSPEND) {
1049 writel(S3C_SSR_SUSPEND, hsudc->regs + S3C_SSR);
1050 if (hsudc->gadget.speed != USB_SPEED_UNKNOWN
1051 && hsudc->driver && hsudc->driver->suspend)
1052 hsudc->driver->suspend(&hsudc->gadget);
1053 }
1054
1055 if (sys_status & S3C_SSR_RESUME) {
1056 writel(S3C_SSR_RESUME, hsudc->regs + S3C_SSR);
1057 if (hsudc->gadget.speed != USB_SPEED_UNKNOWN
1058 && hsudc->driver && hsudc->driver->resume)
1059 hsudc->driver->resume(&hsudc->gadget);
1060 }
1061
1062 if (sys_status & S3C_SSR_RESET) {
1063 writel(S3C_SSR_RESET, hsudc->regs + S3C_SSR);
1064 for (ep_idx = 0; ep_idx < hsudc->pd->epnum; ep_idx++) {
1065 hsep = &hsudc->ep[ep_idx];
1066 hsep->stopped = 1;
1067 s3c_hsudc_nuke_ep(hsep, -ECONNRESET);
1068 }
1069 s3c_hsudc_reconfig(hsudc);
1070 hsudc->ep0state = WAIT_FOR_SETUP;
1071 }
1072 }
1073
1074 if (ep_intr & S3C_EIR_EP0) {
1075 writel(S3C_EIR_EP0, hsudc->regs + S3C_EIR);
1076 set_index(hsudc, 0);
1077 s3c_hsudc_handle_ep0_intr(hsudc);
1078 }
1079
1080 ep_intr >>= 1;
1081 ep_idx = 1;
1082 while (ep_intr) {
1083 if (ep_intr & 1) {
1084 hsep = &hsudc->ep[ep_idx];
1085 set_index(hsudc, ep_idx);
1086 writel(1 << ep_idx, hsudc->regs + S3C_EIR);
1087 if (ep_is_in(hsep))
1088 s3c_hsudc_epin_intr(hsudc, ep_idx);
1089 else
1090 s3c_hsudc_epout_intr(hsudc, ep_idx);
1091 }
1092 ep_intr >>= 1;
1093 ep_idx++;
1094 }
1095
1096 spin_unlock(&hsudc->lock);
1097 return IRQ_HANDLED;
1098}
1099
1100static int s3c_hsudc_start(struct usb_gadget *gadget,
1101 struct usb_gadget_driver *driver)
1102{
1103 struct s3c_hsudc *hsudc = to_hsudc(gadget);
1104 int ret;
1105
1106 if (!driver
1107 || driver->max_speed < USB_SPEED_FULL
1108 || !driver->setup)
1109 return -EINVAL;
1110
1111 if (!hsudc)
1112 return -ENODEV;
1113
1114 if (hsudc->driver)
1115 return -EBUSY;
1116
1117 hsudc->driver = driver;
1118
1119 ret = regulator_bulk_enable(ARRAY_SIZE(hsudc->supplies),
1120 hsudc->supplies);
1121 if (ret != 0) {
1122 dev_err(hsudc->dev, "failed to enable supplies: %d\n", ret);
1123 goto err_supplies;
1124 }
1125
1126
1127 if (!IS_ERR_OR_NULL(hsudc->transceiver)) {
1128 ret = otg_set_peripheral(hsudc->transceiver->otg,
1129 &hsudc->gadget);
1130 if (ret) {
1131 dev_err(hsudc->dev, "%s: can't bind to transceiver\n",
1132 hsudc->gadget.name);
1133 goto err_otg;
1134 }
1135 }
1136
1137 enable_irq(hsudc->irq);
1138 s3c_hsudc_reconfig(hsudc);
1139
1140 pm_runtime_get_sync(hsudc->dev);
1141
1142 if (hsudc->pd->phy_init)
1143 hsudc->pd->phy_init();
1144 if (hsudc->pd->gpio_init)
1145 hsudc->pd->gpio_init();
1146
1147 return 0;
1148err_otg:
1149 regulator_bulk_disable(ARRAY_SIZE(hsudc->supplies), hsudc->supplies);
1150err_supplies:
1151 hsudc->driver = NULL;
1152 return ret;
1153}
1154
1155static int s3c_hsudc_stop(struct usb_gadget *gadget)
1156{
1157 struct s3c_hsudc *hsudc = to_hsudc(gadget);
1158 unsigned long flags;
1159
1160 if (!hsudc)
1161 return -ENODEV;
1162
1163 spin_lock_irqsave(&hsudc->lock, flags);
1164 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
1165 if (hsudc->pd->phy_uninit)
1166 hsudc->pd->phy_uninit();
1167
1168 pm_runtime_put(hsudc->dev);
1169
1170 if (hsudc->pd->gpio_uninit)
1171 hsudc->pd->gpio_uninit();
1172 s3c_hsudc_stop_activity(hsudc);
1173 spin_unlock_irqrestore(&hsudc->lock, flags);
1174
1175 if (!IS_ERR_OR_NULL(hsudc->transceiver))
1176 (void) otg_set_peripheral(hsudc->transceiver->otg, NULL);
1177
1178 disable_irq(hsudc->irq);
1179
1180 regulator_bulk_disable(ARRAY_SIZE(hsudc->supplies), hsudc->supplies);
1181 hsudc->driver = NULL;
1182
1183 return 0;
1184}
1185
1186static inline u32 s3c_hsudc_read_frameno(struct s3c_hsudc *hsudc)
1187{
1188 return readl(hsudc->regs + S3C_FNR) & 0x3FF;
1189}
1190
1191static int s3c_hsudc_gadget_getframe(struct usb_gadget *gadget)
1192{
1193 return s3c_hsudc_read_frameno(to_hsudc(gadget));
1194}
1195
1196static int s3c_hsudc_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1197{
1198 struct s3c_hsudc *hsudc = to_hsudc(gadget);
1199
1200 if (!hsudc)
1201 return -ENODEV;
1202
1203 if (!IS_ERR_OR_NULL(hsudc->transceiver))
1204 return usb_phy_set_power(hsudc->transceiver, mA);
1205
1206 return -EOPNOTSUPP;
1207}
1208
1209static const struct usb_gadget_ops s3c_hsudc_gadget_ops = {
1210 .get_frame = s3c_hsudc_gadget_getframe,
1211 .udc_start = s3c_hsudc_start,
1212 .udc_stop = s3c_hsudc_stop,
1213 .vbus_draw = s3c_hsudc_vbus_draw,
1214};
1215
1216static int s3c_hsudc_probe(struct platform_device *pdev)
1217{
1218 struct device *dev = &pdev->dev;
1219 struct s3c_hsudc *hsudc;
1220 struct s3c24xx_hsudc_platdata *pd = dev_get_platdata(&pdev->dev);
1221 int ret, i;
1222
1223 hsudc = devm_kzalloc(&pdev->dev, struct_size(hsudc, ep, pd->epnum),
1224 GFP_KERNEL);
1225 if (!hsudc)
1226 return -ENOMEM;
1227
1228 platform_set_drvdata(pdev, dev);
1229 hsudc->dev = dev;
1230 hsudc->pd = dev_get_platdata(&pdev->dev);
1231
1232 hsudc->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
1233
1234 for (i = 0; i < ARRAY_SIZE(hsudc->supplies); i++)
1235 hsudc->supplies[i].supply = s3c_hsudc_supply_names[i];
1236
1237 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsudc->supplies),
1238 hsudc->supplies);
1239 if (ret != 0) {
1240 if (ret != -EPROBE_DEFER)
1241 dev_err(dev, "failed to request supplies: %d\n", ret);
1242 goto err_supplies;
1243 }
1244
1245 hsudc->regs = devm_platform_ioremap_resource(pdev, 0);
1246 if (IS_ERR(hsudc->regs)) {
1247 ret = PTR_ERR(hsudc->regs);
1248 goto err_res;
1249 }
1250
1251 spin_lock_init(&hsudc->lock);
1252
1253 hsudc->gadget.max_speed = USB_SPEED_HIGH;
1254 hsudc->gadget.ops = &s3c_hsudc_gadget_ops;
1255 hsudc->gadget.name = dev_name(dev);
1256 hsudc->gadget.ep0 = &hsudc->ep[0].ep;
1257 hsudc->gadget.is_otg = 0;
1258 hsudc->gadget.is_a_peripheral = 0;
1259 hsudc->gadget.speed = USB_SPEED_UNKNOWN;
1260
1261 s3c_hsudc_setup_ep(hsudc);
1262
1263 ret = platform_get_irq(pdev, 0);
1264 if (ret < 0)
1265 goto err_res;
1266 hsudc->irq = ret;
1267
1268 ret = devm_request_irq(&pdev->dev, hsudc->irq, s3c_hsudc_irq, 0,
1269 driver_name, hsudc);
1270 if (ret < 0) {
1271 dev_err(dev, "irq request failed\n");
1272 goto err_res;
1273 }
1274
1275 hsudc->uclk = devm_clk_get(&pdev->dev, "usb-device");
1276 if (IS_ERR(hsudc->uclk)) {
1277 dev_err(dev, "failed to find usb-device clock source\n");
1278 ret = PTR_ERR(hsudc->uclk);
1279 goto err_res;
1280 }
1281 clk_enable(hsudc->uclk);
1282
1283 local_irq_disable();
1284
1285 disable_irq(hsudc->irq);
1286 local_irq_enable();
1287
1288 ret = usb_add_gadget_udc(&pdev->dev, &hsudc->gadget);
1289 if (ret)
1290 goto err_add_udc;
1291
1292 pm_runtime_enable(dev);
1293
1294 return 0;
1295err_add_udc:
1296 clk_disable(hsudc->uclk);
1297err_res:
1298 if (!IS_ERR_OR_NULL(hsudc->transceiver))
1299 usb_put_phy(hsudc->transceiver);
1300
1301err_supplies:
1302 return ret;
1303}
1304
1305static struct platform_driver s3c_hsudc_driver = {
1306 .driver = {
1307 .name = "s3c-hsudc",
1308 },
1309 .probe = s3c_hsudc_probe,
1310};
1311
1312module_platform_driver(s3c_hsudc_driver);
1313
1314MODULE_DESCRIPTION("Samsung S3C24XX USB high-speed controller driver");
1315MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
1316MODULE_LICENSE("GPL");
1317MODULE_ALIAS("platform:s3c-hsudc");
1318