1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#undef VERBOSE
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/ioport.h>
25#include <linux/types.h>
26#include <linux/errno.h>
27#include <linux/err.h>
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/list.h>
31#include <linux/interrupt.h>
32#include <linux/proc_fs.h>
33#include <linux/mm.h>
34#include <linux/moduleparam.h>
35#include <linux/device.h>
36#include <linux/usb/ch9.h>
37#include <linux/usb/gadget.h>
38#include <linux/usb/otg.h>
39#include <linux/dma-mapping.h>
40#include <linux/platform_device.h>
41#include <linux/fsl_devices.h>
42#include <linux/dmapool.h>
43#include <linux/delay.h>
44#include <linux/of_device.h>
45
46#include <asm/byteorder.h>
47#include <asm/io.h>
48#include <asm/unaligned.h>
49#include <asm/dma.h>
50
51#include "fsl_usb2_udc.h"
52
53#define DRIVER_DESC "Freescale High-Speed USB SOC Device Controller driver"
54#define DRIVER_AUTHOR "Li Yang/Jiang Bo"
55#define DRIVER_VERSION "Apr 20, 2007"
56
57#define DMA_ADDR_INVALID (~(dma_addr_t)0)
58
59static const char driver_name[] = "fsl-usb2-udc";
60static const char driver_desc[] = DRIVER_DESC;
61
62static struct usb_dr_device *dr_regs;
63
64static struct usb_sys_interface *usb_sys_regs;
65
66
67static struct fsl_udc *udc_controller = NULL;
68
69static const struct usb_endpoint_descriptor
70fsl_ep0_desc = {
71 .bLength = USB_DT_ENDPOINT_SIZE,
72 .bDescriptorType = USB_DT_ENDPOINT,
73 .bEndpointAddress = 0,
74 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
75 .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
76};
77
78static void fsl_ep_fifo_flush(struct usb_ep *_ep);
79
80#ifdef CONFIG_PPC32
81
82
83
84
85
86
87
88
89
90
91
92
93
94static u32 _fsl_readl_be(const unsigned __iomem *p)
95{
96 return in_be32(p);
97}
98
99static u32 _fsl_readl_le(const unsigned __iomem *p)
100{
101 return in_le32(p);
102}
103
104static void _fsl_writel_be(u32 v, unsigned __iomem *p)
105{
106 out_be32(p, v);
107}
108
109static void _fsl_writel_le(u32 v, unsigned __iomem *p)
110{
111 out_le32(p, v);
112}
113
114static u32 (*_fsl_readl)(const unsigned __iomem *p);
115static void (*_fsl_writel)(u32 v, unsigned __iomem *p);
116
117#define fsl_readl(p) (*_fsl_readl)((p))
118#define fsl_writel(v, p) (*_fsl_writel)((v), (p))
119
120static inline void fsl_set_accessors(struct fsl_usb2_platform_data *pdata)
121{
122 if (pdata->big_endian_mmio) {
123 _fsl_readl = _fsl_readl_be;
124 _fsl_writel = _fsl_writel_be;
125 } else {
126 _fsl_readl = _fsl_readl_le;
127 _fsl_writel = _fsl_writel_le;
128 }
129}
130
131static inline u32 cpu_to_hc32(const u32 x)
132{
133 return udc_controller->pdata->big_endian_desc
134 ? (__force u32)cpu_to_be32(x)
135 : (__force u32)cpu_to_le32(x);
136}
137
138static inline u32 hc32_to_cpu(const u32 x)
139{
140 return udc_controller->pdata->big_endian_desc
141 ? be32_to_cpu((__force __be32)x)
142 : le32_to_cpu((__force __le32)x);
143}
144#else
145static inline void fsl_set_accessors(struct fsl_usb2_platform_data *pdata) {}
146
147#define fsl_readl(addr) readl(addr)
148#define fsl_writel(val32, addr) writel(val32, addr)
149#define cpu_to_hc32(x) cpu_to_le32(x)
150#define hc32_to_cpu(x) le32_to_cpu(x)
151#endif
152
153
154
155
156
157
158
159
160
161static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
162{
163 struct fsl_udc *udc = NULL;
164 unsigned char stopped = ep->stopped;
165 struct ep_td_struct *curr_td, *next_td;
166 int j;
167
168 udc = (struct fsl_udc *)ep->udc;
169
170 list_del_init(&req->queue);
171
172
173 if (req->req.status == -EINPROGRESS)
174 req->req.status = status;
175 else
176 status = req->req.status;
177
178
179 next_td = req->head;
180 for (j = 0; j < req->dtd_count; j++) {
181 curr_td = next_td;
182 if (j != req->dtd_count - 1) {
183 next_td = curr_td->next_td_virt;
184 }
185 dma_pool_free(udc->td_pool, curr_td, curr_td->td_dma);
186 }
187
188 usb_gadget_unmap_request(&ep->udc->gadget, &req->req, ep_is_in(ep));
189
190 if (status && (status != -ESHUTDOWN))
191 VDBG("complete %s req %p stat %d len %u/%u",
192 ep->ep.name, &req->req, status,
193 req->req.actual, req->req.length);
194
195 ep->stopped = 1;
196
197 spin_unlock(&ep->udc->lock);
198
199
200 if (req->req.complete)
201 req->req.complete(&ep->ep, &req->req);
202
203 spin_lock(&ep->udc->lock);
204 ep->stopped = stopped;
205}
206
207
208
209
210
211static void nuke(struct fsl_ep *ep, int status)
212{
213 ep->stopped = 1;
214
215
216 fsl_ep_fifo_flush(&ep->ep);
217
218
219 while (!list_empty(&ep->queue)) {
220 struct fsl_req *req = NULL;
221
222 req = list_entry(ep->queue.next, struct fsl_req, queue);
223 done(ep, req, status);
224 }
225}
226
227
228
229
230
231static int dr_controller_setup(struct fsl_udc *udc)
232{
233 unsigned int tmp, portctrl, ep_num;
234 unsigned int max_no_of_ep;
235 unsigned int ctrl;
236 unsigned long timeout;
237
238#define FSL_UDC_RESET_TIMEOUT 1000
239
240
241 portctrl = fsl_readl(&dr_regs->portsc1);
242 portctrl &= ~(PORTSCX_PHY_TYPE_SEL | PORTSCX_PORT_WIDTH);
243 switch (udc->phy_mode) {
244 case FSL_USB2_PHY_ULPI:
245 if (udc->pdata->have_sysif_regs) {
246 if (udc->pdata->controller_ver) {
247
248 ctrl = __raw_readl(&usb_sys_regs->control);
249 ctrl &= ~USB_CTRL_UTMI_PHY_EN;
250 ctrl |= USB_CTRL_USB_EN;
251 __raw_writel(ctrl, &usb_sys_regs->control);
252 }
253 }
254 portctrl |= PORTSCX_PTS_ULPI;
255 break;
256 case FSL_USB2_PHY_UTMI_WIDE:
257 portctrl |= PORTSCX_PTW_16BIT;
258
259 case FSL_USB2_PHY_UTMI:
260 if (udc->pdata->have_sysif_regs) {
261 if (udc->pdata->controller_ver) {
262
263 ctrl = __raw_readl(&usb_sys_regs->control);
264 ctrl |= (USB_CTRL_UTMI_PHY_EN |
265 USB_CTRL_USB_EN);
266 __raw_writel(ctrl, &usb_sys_regs->control);
267 mdelay(FSL_UTMI_PHY_DLY);
268
269 }
270 }
271 portctrl |= PORTSCX_PTS_UTMI;
272 break;
273 case FSL_USB2_PHY_SERIAL:
274 portctrl |= PORTSCX_PTS_FSLS;
275 break;
276 default:
277 return -EINVAL;
278 }
279 fsl_writel(portctrl, &dr_regs->portsc1);
280
281
282 tmp = fsl_readl(&dr_regs->usbcmd);
283 tmp &= ~USB_CMD_RUN_STOP;
284 fsl_writel(tmp, &dr_regs->usbcmd);
285
286 tmp = fsl_readl(&dr_regs->usbcmd);
287 tmp |= USB_CMD_CTRL_RESET;
288 fsl_writel(tmp, &dr_regs->usbcmd);
289
290
291 timeout = jiffies + FSL_UDC_RESET_TIMEOUT;
292 while (fsl_readl(&dr_regs->usbcmd) & USB_CMD_CTRL_RESET) {
293 if (time_after(jiffies, timeout)) {
294 ERR("udc reset timeout!\n");
295 return -ETIMEDOUT;
296 }
297 cpu_relax();
298 }
299
300
301 tmp = fsl_readl(&dr_regs->usbmode);
302 tmp &= ~USB_MODE_CTRL_MODE_MASK;
303 tmp |= USB_MODE_CTRL_MODE_DEVICE;
304
305 tmp |= USB_MODE_SETUP_LOCK_OFF;
306 if (udc->pdata->es)
307 tmp |= USB_MODE_ES;
308 fsl_writel(tmp, &dr_regs->usbmode);
309
310
311 fsl_writel(0, &dr_regs->usbsts);
312
313 tmp = udc->ep_qh_dma;
314 tmp &= USB_EP_LIST_ADDRESS_MASK;
315 fsl_writel(tmp, &dr_regs->endpointlistaddr);
316
317 VDBG("vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
318 udc->ep_qh, (int)tmp,
319 fsl_readl(&dr_regs->endpointlistaddr));
320
321 max_no_of_ep = (0x0000001F & fsl_readl(&dr_regs->dccparams));
322 for (ep_num = 1; ep_num < max_no_of_ep; ep_num++) {
323 tmp = fsl_readl(&dr_regs->endptctrl[ep_num]);
324 tmp &= ~(EPCTRL_TX_TYPE | EPCTRL_RX_TYPE);
325 tmp |= (EPCTRL_EP_TYPE_BULK << EPCTRL_TX_EP_TYPE_SHIFT)
326 | (EPCTRL_EP_TYPE_BULK << EPCTRL_RX_EP_TYPE_SHIFT);
327 fsl_writel(tmp, &dr_regs->endptctrl[ep_num]);
328 }
329
330#ifndef CONFIG_ARCH_MXC
331 if (udc->pdata->have_sysif_regs) {
332 ctrl = __raw_readl(&usb_sys_regs->control);
333 ctrl |= USB_CTRL_IOENB;
334 __raw_writel(ctrl, &usb_sys_regs->control);
335 }
336#endif
337
338#if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
339
340
341
342 if (udc->pdata->have_sysif_regs) {
343
344 tmp = SNOOP_SIZE_2GB;
345 __raw_writel(tmp, &usb_sys_regs->snoop1);
346 tmp |= 0x80000000;
347 __raw_writel(tmp, &usb_sys_regs->snoop2);
348 }
349#endif
350
351 return 0;
352}
353
354
355static void dr_controller_run(struct fsl_udc *udc)
356{
357 u32 temp;
358
359
360 temp = USB_INTR_INT_EN | USB_INTR_ERR_INT_EN
361 | USB_INTR_PTC_DETECT_EN | USB_INTR_RESET_EN
362 | USB_INTR_DEVICE_SUSPEND | USB_INTR_SYS_ERR_EN;
363
364 fsl_writel(temp, &dr_regs->usbintr);
365
366
367 udc->stopped = 0;
368
369
370 temp = fsl_readl(&dr_regs->usbmode);
371 temp |= USB_MODE_CTRL_MODE_DEVICE;
372 fsl_writel(temp, &dr_regs->usbmode);
373
374
375 temp = fsl_readl(&dr_regs->usbcmd);
376 temp |= USB_CMD_RUN_STOP;
377 fsl_writel(temp, &dr_regs->usbcmd);
378}
379
380static void dr_controller_stop(struct fsl_udc *udc)
381{
382 unsigned int tmp;
383
384 pr_debug("%s\n", __func__);
385
386
387
388
389
390 if (udc->gadget.is_otg) {
391 if (!(fsl_readl(&dr_regs->otgsc) & OTGSC_STS_USB_ID)) {
392 pr_debug("udc: Leaving early\n");
393 return;
394 }
395 }
396
397
398 fsl_writel(0, &dr_regs->usbintr);
399
400
401 udc->stopped = 1;
402
403
404
405
406
407 tmp = fsl_readl(&dr_regs->usbcmd);
408 tmp &= ~USB_CMD_RUN_STOP;
409 fsl_writel(tmp, &dr_regs->usbcmd);
410}
411
412static void dr_ep_setup(unsigned char ep_num, unsigned char dir,
413 unsigned char ep_type)
414{
415 unsigned int tmp_epctrl = 0;
416
417 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
418 if (dir) {
419 if (ep_num)
420 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
421 tmp_epctrl |= EPCTRL_TX_ENABLE;
422 tmp_epctrl &= ~EPCTRL_TX_TYPE;
423 tmp_epctrl |= ((unsigned int)(ep_type)
424 << EPCTRL_TX_EP_TYPE_SHIFT);
425 } else {
426 if (ep_num)
427 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
428 tmp_epctrl |= EPCTRL_RX_ENABLE;
429 tmp_epctrl &= ~EPCTRL_RX_TYPE;
430 tmp_epctrl |= ((unsigned int)(ep_type)
431 << EPCTRL_RX_EP_TYPE_SHIFT);
432 }
433
434 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
435}
436
437static void
438dr_ep_change_stall(unsigned char ep_num, unsigned char dir, int value)
439{
440 u32 tmp_epctrl = 0;
441
442 tmp_epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
443
444 if (value) {
445
446 if (dir)
447 tmp_epctrl |= EPCTRL_TX_EP_STALL;
448 else
449 tmp_epctrl |= EPCTRL_RX_EP_STALL;
450 } else {
451
452 if (dir) {
453 tmp_epctrl &= ~EPCTRL_TX_EP_STALL;
454 tmp_epctrl |= EPCTRL_TX_DATA_TOGGLE_RST;
455 } else {
456 tmp_epctrl &= ~EPCTRL_RX_EP_STALL;
457 tmp_epctrl |= EPCTRL_RX_DATA_TOGGLE_RST;
458 }
459 }
460 fsl_writel(tmp_epctrl, &dr_regs->endptctrl[ep_num]);
461}
462
463
464
465static int dr_ep_get_stall(unsigned char ep_num, unsigned char dir)
466{
467 u32 epctrl;
468
469 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
470 if (dir)
471 return (epctrl & EPCTRL_TX_EP_STALL) ? 1 : 0;
472 else
473 return (epctrl & EPCTRL_RX_EP_STALL) ? 1 : 0;
474}
475
476
477
478
479
480
481
482
483
484
485static void struct_ep_qh_setup(struct fsl_udc *udc, unsigned char ep_num,
486 unsigned char dir, unsigned char ep_type,
487 unsigned int max_pkt_len,
488 unsigned int zlt, unsigned char mult)
489{
490 struct ep_queue_head *p_QH = &udc->ep_qh[2 * ep_num + dir];
491 unsigned int tmp = 0;
492
493
494 switch (ep_type) {
495 case USB_ENDPOINT_XFER_CONTROL:
496
497 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
498 | EP_QUEUE_HEAD_IOS;
499 break;
500 case USB_ENDPOINT_XFER_ISOC:
501 tmp = (max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
502 | (mult << EP_QUEUE_HEAD_MULT_POS);
503 break;
504 case USB_ENDPOINT_XFER_BULK:
505 case USB_ENDPOINT_XFER_INT:
506 tmp = max_pkt_len << EP_QUEUE_HEAD_MAX_PKT_LEN_POS;
507 break;
508 default:
509 VDBG("error ep type is %d", ep_type);
510 return;
511 }
512 if (zlt)
513 tmp |= EP_QUEUE_HEAD_ZLT_SEL;
514
515 p_QH->max_pkt_length = cpu_to_hc32(tmp);
516 p_QH->next_dtd_ptr = 1;
517 p_QH->size_ioc_int_sts = 0;
518}
519
520
521static void ep0_setup(struct fsl_udc *udc)
522{
523
524
525 struct_ep_qh_setup(udc, 0, USB_RECV, USB_ENDPOINT_XFER_CONTROL,
526 USB_MAX_CTRL_PAYLOAD, 0, 0);
527 struct_ep_qh_setup(udc, 0, USB_SEND, USB_ENDPOINT_XFER_CONTROL,
528 USB_MAX_CTRL_PAYLOAD, 0, 0);
529 dr_ep_setup(0, USB_RECV, USB_ENDPOINT_XFER_CONTROL);
530 dr_ep_setup(0, USB_SEND, USB_ENDPOINT_XFER_CONTROL);
531
532 return;
533
534}
535
536
537
538
539
540
541
542
543
544
545
546static int fsl_ep_enable(struct usb_ep *_ep,
547 const struct usb_endpoint_descriptor *desc)
548{
549 struct fsl_udc *udc = NULL;
550 struct fsl_ep *ep = NULL;
551 unsigned short max = 0;
552 unsigned char mult = 0, zlt;
553 int retval = -EINVAL;
554 unsigned long flags = 0;
555
556 ep = container_of(_ep, struct fsl_ep, ep);
557
558
559 if (!_ep || !desc
560 || (desc->bDescriptorType != USB_DT_ENDPOINT))
561 return -EINVAL;
562
563 udc = ep->udc;
564
565 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
566 return -ESHUTDOWN;
567
568 max = usb_endpoint_maxp(desc);
569
570
571
572
573 zlt = 1;
574
575
576 switch (desc->bmAttributes & 0x03) {
577 case USB_ENDPOINT_XFER_CONTROL:
578 case USB_ENDPOINT_XFER_BULK:
579 case USB_ENDPOINT_XFER_INT:
580
581
582
583
584 mult = 0;
585 break;
586 case USB_ENDPOINT_XFER_ISOC:
587
588 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
589 max = max & 0x7ff;
590
591 if (mult > 3)
592 goto en_done;
593 break;
594 default:
595 goto en_done;
596 }
597
598 spin_lock_irqsave(&udc->lock, flags);
599 ep->ep.maxpacket = max;
600 ep->ep.desc = desc;
601 ep->stopped = 0;
602
603
604
605
606 struct_ep_qh_setup(udc, (unsigned char) ep_index(ep),
607 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
608 ? USB_SEND : USB_RECV),
609 (unsigned char) (desc->bmAttributes
610 & USB_ENDPOINT_XFERTYPE_MASK),
611 max, zlt, mult);
612
613
614 dr_ep_setup((unsigned char) ep_index(ep),
615 (unsigned char) ((desc->bEndpointAddress & USB_DIR_IN)
616 ? USB_SEND : USB_RECV),
617 (unsigned char) (desc->bmAttributes
618 & USB_ENDPOINT_XFERTYPE_MASK));
619
620 spin_unlock_irqrestore(&udc->lock, flags);
621 retval = 0;
622
623 VDBG("enabled %s (ep%d%s) maxpacket %d",ep->ep.name,
624 ep->ep.desc->bEndpointAddress & 0x0f,
625 (desc->bEndpointAddress & USB_DIR_IN)
626 ? "in" : "out", max);
627en_done:
628 return retval;
629}
630
631
632
633
634
635static int fsl_ep_disable(struct usb_ep *_ep)
636{
637 struct fsl_udc *udc = NULL;
638 struct fsl_ep *ep = NULL;
639 unsigned long flags = 0;
640 u32 epctrl;
641 int ep_num;
642
643 ep = container_of(_ep, struct fsl_ep, ep);
644 if (!_ep || !ep->ep.desc) {
645 VDBG("%s not enabled", _ep ? ep->ep.name : NULL);
646 return -EINVAL;
647 }
648
649
650 ep_num = ep_index(ep);
651 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
652 if (ep_is_in(ep)) {
653 epctrl &= ~(EPCTRL_TX_ENABLE | EPCTRL_TX_TYPE);
654 epctrl |= EPCTRL_EP_TYPE_BULK << EPCTRL_TX_EP_TYPE_SHIFT;
655 } else {
656 epctrl &= ~(EPCTRL_RX_ENABLE | EPCTRL_TX_TYPE);
657 epctrl |= EPCTRL_EP_TYPE_BULK << EPCTRL_RX_EP_TYPE_SHIFT;
658 }
659 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
660
661 udc = (struct fsl_udc *)ep->udc;
662 spin_lock_irqsave(&udc->lock, flags);
663
664
665 nuke(ep, -ESHUTDOWN);
666
667 ep->ep.desc = NULL;
668 ep->stopped = 1;
669 spin_unlock_irqrestore(&udc->lock, flags);
670
671 VDBG("disabled %s OK", _ep->name);
672 return 0;
673}
674
675
676
677
678
679
680static struct usb_request *
681fsl_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
682{
683 struct fsl_req *req = NULL;
684
685 req = kzalloc(sizeof *req, gfp_flags);
686 if (!req)
687 return NULL;
688
689 req->req.dma = DMA_ADDR_INVALID;
690 INIT_LIST_HEAD(&req->queue);
691
692 return &req->req;
693}
694
695static void fsl_free_request(struct usb_ep *_ep, struct usb_request *_req)
696{
697 struct fsl_req *req = NULL;
698
699 req = container_of(_req, struct fsl_req, req);
700
701 if (_req)
702 kfree(req);
703}
704
705
706static void fsl_prime_ep(struct fsl_ep *ep, struct ep_td_struct *td)
707{
708 struct ep_queue_head *qh = get_qh_by_ep(ep);
709
710
711 qh->next_dtd_ptr = cpu_to_hc32(td->td_dma
712 & EP_QUEUE_HEAD_NEXT_POINTER_MASK);
713
714
715 qh->size_ioc_int_sts &= cpu_to_hc32(~(EP_QUEUE_HEAD_STATUS_ACTIVE
716 | EP_QUEUE_HEAD_STATUS_HALT));
717
718
719 wmb();
720
721
722 fsl_writel(ep_is_in(ep) ? (1 << (ep_index(ep) + 16))
723 : (1 << (ep_index(ep))), &dr_regs->endpointprime);
724}
725
726
727static void fsl_queue_td(struct fsl_ep *ep, struct fsl_req *req)
728{
729 u32 temp, bitmask, tmp_stat;
730
731
732
733
734 bitmask = ep_is_in(ep)
735 ? (1 << (ep_index(ep) + 16))
736 : (1 << (ep_index(ep)));
737
738
739 if (!(list_empty(&ep->queue)) && !(ep_index(ep) == 0)) {
740
741 struct fsl_req *lastreq;
742 lastreq = list_entry(ep->queue.prev, struct fsl_req, queue);
743 lastreq->tail->next_td_ptr =
744 cpu_to_hc32(req->head->td_dma & DTD_ADDR_MASK);
745
746 wmb();
747
748 if (fsl_readl(&dr_regs->endpointprime) & bitmask)
749 return;
750
751 do {
752
753 temp = fsl_readl(&dr_regs->usbcmd);
754 fsl_writel(temp | USB_CMD_ATDTW, &dr_regs->usbcmd);
755
756
757 tmp_stat = fsl_readl(&dr_regs->endptstatus) & bitmask;
758
759 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_ATDTW));
760
761
762 temp = fsl_readl(&dr_regs->usbcmd);
763 fsl_writel(temp & ~USB_CMD_ATDTW, &dr_regs->usbcmd);
764
765 if (tmp_stat)
766 return;
767 }
768
769 fsl_prime_ep(ep, req->head);
770}
771
772
773
774
775
776
777
778static struct ep_td_struct *fsl_build_dtd(struct fsl_req *req, unsigned *length,
779 dma_addr_t *dma, int *is_last, gfp_t gfp_flags)
780{
781 u32 swap_temp;
782 struct ep_td_struct *dtd;
783
784
785 *length = min(req->req.length - req->req.actual,
786 (unsigned)EP_MAX_LENGTH_TRANSFER);
787
788 dtd = dma_pool_alloc(udc_controller->td_pool, gfp_flags, dma);
789 if (dtd == NULL)
790 return dtd;
791
792 dtd->td_dma = *dma;
793
794 swap_temp = hc32_to_cpu(dtd->size_ioc_sts);
795 swap_temp &= ~DTD_RESERVED_FIELDS;
796 dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
797
798
799 swap_temp = (u32) (req->req.dma + req->req.actual);
800 dtd->buff_ptr0 = cpu_to_hc32(swap_temp);
801 dtd->buff_ptr1 = cpu_to_hc32(swap_temp + 0x1000);
802 dtd->buff_ptr2 = cpu_to_hc32(swap_temp + 0x2000);
803 dtd->buff_ptr3 = cpu_to_hc32(swap_temp + 0x3000);
804 dtd->buff_ptr4 = cpu_to_hc32(swap_temp + 0x4000);
805
806 req->req.actual += *length;
807
808
809 if (req->req.zero) {
810 if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
811 *is_last = 1;
812 else
813 *is_last = 0;
814 } else if (req->req.length == req->req.actual)
815 *is_last = 1;
816 else
817 *is_last = 0;
818
819 if ((*is_last) == 0)
820 VDBG("multi-dtd request!");
821
822 swap_temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
823
824
825 if (*is_last && !req->req.no_interrupt)
826 swap_temp |= DTD_IOC;
827
828 dtd->size_ioc_sts = cpu_to_hc32(swap_temp);
829
830 mb();
831
832 VDBG("length = %d address= 0x%x", *length, (int)*dma);
833
834 return dtd;
835}
836
837
838static int fsl_req_to_dtd(struct fsl_req *req, gfp_t gfp_flags)
839{
840 unsigned count;
841 int is_last;
842 int is_first =1;
843 struct ep_td_struct *last_dtd = NULL, *dtd;
844 dma_addr_t dma;
845
846 do {
847 dtd = fsl_build_dtd(req, &count, &dma, &is_last, gfp_flags);
848 if (dtd == NULL)
849 return -ENOMEM;
850
851 if (is_first) {
852 is_first = 0;
853 req->head = dtd;
854 } else {
855 last_dtd->next_td_ptr = cpu_to_hc32(dma);
856 last_dtd->next_td_virt = dtd;
857 }
858 last_dtd = dtd;
859
860 req->dtd_count++;
861 } while (!is_last);
862
863 dtd->next_td_ptr = cpu_to_hc32(DTD_NEXT_TERMINATE);
864
865 req->tail = dtd;
866
867 return 0;
868}
869
870
871static int
872fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
873{
874 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
875 struct fsl_req *req = container_of(_req, struct fsl_req, req);
876 struct fsl_udc *udc;
877 unsigned long flags;
878 int ret;
879
880
881 if (!_req || !req->req.complete || !req->req.buf
882 || !list_empty(&req->queue)) {
883 VDBG("%s, bad params", __func__);
884 return -EINVAL;
885 }
886 if (unlikely(!_ep || !ep->ep.desc)) {
887 VDBG("%s, bad ep", __func__);
888 return -EINVAL;
889 }
890 if (usb_endpoint_xfer_isoc(ep->ep.desc)) {
891 if (req->req.length > ep->ep.maxpacket)
892 return -EMSGSIZE;
893 }
894
895 udc = ep->udc;
896 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
897 return -ESHUTDOWN;
898
899 req->ep = ep;
900
901 ret = usb_gadget_map_request(&ep->udc->gadget, &req->req, ep_is_in(ep));
902 if (ret)
903 return ret;
904
905 req->req.status = -EINPROGRESS;
906 req->req.actual = 0;
907 req->dtd_count = 0;
908
909
910 if (!fsl_req_to_dtd(req, gfp_flags)) {
911 spin_lock_irqsave(&udc->lock, flags);
912 fsl_queue_td(ep, req);
913 } else {
914 return -ENOMEM;
915 }
916
917
918 if (req != NULL)
919 list_add_tail(&req->queue, &ep->queue);
920 spin_unlock_irqrestore(&udc->lock, flags);
921
922 return 0;
923}
924
925
926static int fsl_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
927{
928 struct fsl_ep *ep = container_of(_ep, struct fsl_ep, ep);
929 struct fsl_req *req;
930 unsigned long flags;
931 int ep_num, stopped, ret = 0;
932 u32 epctrl;
933
934 if (!_ep || !_req)
935 return -EINVAL;
936
937 spin_lock_irqsave(&ep->udc->lock, flags);
938 stopped = ep->stopped;
939
940
941 ep->stopped = 1;
942 ep_num = ep_index(ep);
943 epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
944 if (ep_is_in(ep))
945 epctrl &= ~EPCTRL_TX_ENABLE;
946 else
947 epctrl &= ~EPCTRL_RX_ENABLE;
948 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
949
950
951 list_for_each_entry(req, &ep->queue, queue) {
952 if (&req->req == _req)
953 break;
954 }
955 if (&req->req != _req) {
956 ret = -EINVAL;
957 goto out;
958 }
959
960
961 if (ep->queue.next == &req->queue) {
962 _req->status = -ECONNRESET;
963 fsl_ep_fifo_flush(_ep);
964
965
966 if (req->queue.next != &ep->queue) {
967 struct fsl_req *next_req;
968
969 next_req = list_entry(req->queue.next, struct fsl_req,
970 queue);
971
972
973 fsl_prime_ep(ep, next_req->head);
974 }
975
976 } else {
977 struct fsl_req *prev_req;
978
979 prev_req = list_entry(req->queue.prev, struct fsl_req, queue);
980 prev_req->tail->next_td_ptr = req->tail->next_td_ptr;
981 }
982
983 done(ep, req, -ECONNRESET);
984
985
986out: epctrl = fsl_readl(&dr_regs->endptctrl[ep_num]);
987 if (ep_is_in(ep))
988 epctrl |= EPCTRL_TX_ENABLE;
989 else
990 epctrl |= EPCTRL_RX_ENABLE;
991 fsl_writel(epctrl, &dr_regs->endptctrl[ep_num]);
992 ep->stopped = stopped;
993
994 spin_unlock_irqrestore(&ep->udc->lock, flags);
995 return ret;
996}
997
998
999
1000
1001
1002
1003
1004
1005
1006static int fsl_ep_set_halt(struct usb_ep *_ep, int value)
1007{
1008 struct fsl_ep *ep = NULL;
1009 unsigned long flags = 0;
1010 int status = -EOPNOTSUPP;
1011 unsigned char ep_dir = 0, ep_num = 0;
1012 struct fsl_udc *udc = NULL;
1013
1014 ep = container_of(_ep, struct fsl_ep, ep);
1015 udc = ep->udc;
1016 if (!_ep || !ep->ep.desc) {
1017 status = -EINVAL;
1018 goto out;
1019 }
1020
1021 if (usb_endpoint_xfer_isoc(ep->ep.desc)) {
1022 status = -EOPNOTSUPP;
1023 goto out;
1024 }
1025
1026
1027
1028 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
1029 status = -EAGAIN;
1030 goto out;
1031 }
1032
1033 status = 0;
1034 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
1035 ep_num = (unsigned char)(ep_index(ep));
1036 spin_lock_irqsave(&ep->udc->lock, flags);
1037 dr_ep_change_stall(ep_num, ep_dir, value);
1038 spin_unlock_irqrestore(&ep->udc->lock, flags);
1039
1040 if (ep_index(ep) == 0) {
1041 udc->ep0_state = WAIT_FOR_SETUP;
1042 udc->ep0_dir = 0;
1043 }
1044out:
1045 VDBG(" %s %s halt stat %d", ep->ep.name,
1046 value ? "set" : "clear", status);
1047
1048 return status;
1049}
1050
1051static int fsl_ep_fifo_status(struct usb_ep *_ep)
1052{
1053 struct fsl_ep *ep;
1054 struct fsl_udc *udc;
1055 int size = 0;
1056 u32 bitmask;
1057 struct ep_queue_head *qh;
1058
1059 ep = container_of(_ep, struct fsl_ep, ep);
1060 if (!_ep || (!ep->ep.desc && ep_index(ep) != 0))
1061 return -ENODEV;
1062
1063 udc = (struct fsl_udc *)ep->udc;
1064
1065 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
1066 return -ESHUTDOWN;
1067
1068 qh = get_qh_by_ep(ep);
1069
1070 bitmask = (ep_is_in(ep)) ? (1 << (ep_index(ep) + 16)) :
1071 (1 << (ep_index(ep)));
1072
1073 if (fsl_readl(&dr_regs->endptstatus) & bitmask)
1074 size = (qh->size_ioc_int_sts & DTD_PACKET_SIZE)
1075 >> DTD_LENGTH_BIT_POS;
1076
1077 pr_debug("%s %u\n", __func__, size);
1078 return size;
1079}
1080
1081static void fsl_ep_fifo_flush(struct usb_ep *_ep)
1082{
1083 struct fsl_ep *ep;
1084 int ep_num, ep_dir;
1085 u32 bits;
1086 unsigned long timeout;
1087#define FSL_UDC_FLUSH_TIMEOUT 1000
1088
1089 if (!_ep) {
1090 return;
1091 } else {
1092 ep = container_of(_ep, struct fsl_ep, ep);
1093 if (!ep->ep.desc)
1094 return;
1095 }
1096 ep_num = ep_index(ep);
1097 ep_dir = ep_is_in(ep) ? USB_SEND : USB_RECV;
1098
1099 if (ep_num == 0)
1100 bits = (1 << 16) | 1;
1101 else if (ep_dir == USB_SEND)
1102 bits = 1 << (16 + ep_num);
1103 else
1104 bits = 1 << ep_num;
1105
1106 timeout = jiffies + FSL_UDC_FLUSH_TIMEOUT;
1107 do {
1108 fsl_writel(bits, &dr_regs->endptflush);
1109
1110
1111 while (fsl_readl(&dr_regs->endptflush)) {
1112 if (time_after(jiffies, timeout)) {
1113 ERR("ep flush timeout\n");
1114 return;
1115 }
1116 cpu_relax();
1117 }
1118
1119 } while (fsl_readl(&dr_regs->endptstatus) & bits);
1120}
1121
1122static struct usb_ep_ops fsl_ep_ops = {
1123 .enable = fsl_ep_enable,
1124 .disable = fsl_ep_disable,
1125
1126 .alloc_request = fsl_alloc_request,
1127 .free_request = fsl_free_request,
1128
1129 .queue = fsl_ep_queue,
1130 .dequeue = fsl_ep_dequeue,
1131
1132 .set_halt = fsl_ep_set_halt,
1133 .fifo_status = fsl_ep_fifo_status,
1134 .fifo_flush = fsl_ep_fifo_flush,
1135};
1136
1137
1138
1139
1140
1141
1142
1143
1144static int fsl_get_frame(struct usb_gadget *gadget)
1145{
1146 return (int)(fsl_readl(&dr_regs->frindex) & USB_FRINDEX_MASKS);
1147}
1148
1149
1150
1151
1152static int fsl_wakeup(struct usb_gadget *gadget)
1153{
1154 struct fsl_udc *udc = container_of(gadget, struct fsl_udc, gadget);
1155 u32 portsc;
1156
1157
1158 if (!udc->remote_wakeup)
1159 return -ENOTSUPP;
1160
1161 portsc = fsl_readl(&dr_regs->portsc1);
1162
1163 if (!(portsc & PORTSCX_PORT_SUSPEND))
1164 return 0;
1165
1166 portsc |= PORTSCX_PORT_FORCE_RESUME;
1167 fsl_writel(portsc, &dr_regs->portsc1);
1168 return 0;
1169}
1170
1171static int can_pullup(struct fsl_udc *udc)
1172{
1173 return udc->driver && udc->softconnect && udc->vbus_active;
1174}
1175
1176
1177
1178static int fsl_vbus_session(struct usb_gadget *gadget, int is_active)
1179{
1180 struct fsl_udc *udc;
1181 unsigned long flags;
1182
1183 udc = container_of(gadget, struct fsl_udc, gadget);
1184 spin_lock_irqsave(&udc->lock, flags);
1185 VDBG("VBUS %s", is_active ? "on" : "off");
1186 udc->vbus_active = (is_active != 0);
1187 if (can_pullup(udc))
1188 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1189 &dr_regs->usbcmd);
1190 else
1191 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1192 &dr_regs->usbcmd);
1193 spin_unlock_irqrestore(&udc->lock, flags);
1194 return 0;
1195}
1196
1197
1198
1199
1200
1201
1202
1203
1204static int fsl_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1205{
1206 struct fsl_udc *udc;
1207
1208 udc = container_of(gadget, struct fsl_udc, gadget);
1209 if (!IS_ERR_OR_NULL(udc->transceiver))
1210 return usb_phy_set_power(udc->transceiver, mA);
1211 return -ENOTSUPP;
1212}
1213
1214
1215
1216
1217static int fsl_pullup(struct usb_gadget *gadget, int is_on)
1218{
1219 struct fsl_udc *udc;
1220
1221 udc = container_of(gadget, struct fsl_udc, gadget);
1222 udc->softconnect = (is_on != 0);
1223 if (can_pullup(udc))
1224 fsl_writel((fsl_readl(&dr_regs->usbcmd) | USB_CMD_RUN_STOP),
1225 &dr_regs->usbcmd);
1226 else
1227 fsl_writel((fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP),
1228 &dr_regs->usbcmd);
1229
1230 return 0;
1231}
1232
1233static int fsl_udc_start(struct usb_gadget *g,
1234 struct usb_gadget_driver *driver);
1235static int fsl_udc_stop(struct usb_gadget *g,
1236 struct usb_gadget_driver *driver);
1237
1238static const struct usb_gadget_ops fsl_gadget_ops = {
1239 .get_frame = fsl_get_frame,
1240 .wakeup = fsl_wakeup,
1241
1242 .vbus_session = fsl_vbus_session,
1243 .vbus_draw = fsl_vbus_draw,
1244 .pullup = fsl_pullup,
1245 .udc_start = fsl_udc_start,
1246 .udc_stop = fsl_udc_stop,
1247};
1248
1249
1250
1251static void ep0stall(struct fsl_udc *udc)
1252{
1253 u32 tmp;
1254
1255
1256 tmp = fsl_readl(&dr_regs->endptctrl[0]);
1257 tmp |= EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL;
1258 fsl_writel(tmp, &dr_regs->endptctrl[0]);
1259 udc->ep0_state = WAIT_FOR_SETUP;
1260 udc->ep0_dir = 0;
1261}
1262
1263
1264static int ep0_prime_status(struct fsl_udc *udc, int direction)
1265{
1266 struct fsl_req *req = udc->status_req;
1267 struct fsl_ep *ep;
1268 int ret;
1269
1270 if (direction == EP_DIR_IN)
1271 udc->ep0_dir = USB_DIR_IN;
1272 else
1273 udc->ep0_dir = USB_DIR_OUT;
1274
1275 ep = &udc->eps[0];
1276 if (udc->ep0_state != DATA_STATE_XMIT)
1277 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1278
1279 req->ep = ep;
1280 req->req.length = 0;
1281 req->req.status = -EINPROGRESS;
1282 req->req.actual = 0;
1283 req->req.complete = NULL;
1284 req->dtd_count = 0;
1285
1286 ret = usb_gadget_map_request(&ep->udc->gadget, &req->req, ep_is_in(ep));
1287 if (ret)
1288 return ret;
1289
1290 if (fsl_req_to_dtd(req, GFP_ATOMIC) == 0)
1291 fsl_queue_td(ep, req);
1292 else
1293 return -ENOMEM;
1294
1295 list_add_tail(&req->queue, &ep->queue);
1296
1297 return 0;
1298}
1299
1300static void udc_reset_ep_queue(struct fsl_udc *udc, u8 pipe)
1301{
1302 struct fsl_ep *ep = get_ep_by_pipe(udc, pipe);
1303
1304 if (ep->name)
1305 nuke(ep, -ESHUTDOWN);
1306}
1307
1308
1309
1310
1311static void ch9setaddress(struct fsl_udc *udc, u16 value, u16 index, u16 length)
1312{
1313
1314 udc->device_address = (u8) value;
1315
1316 udc->usb_state = USB_STATE_ADDRESS;
1317
1318 if (ep0_prime_status(udc, EP_DIR_IN))
1319 ep0stall(udc);
1320}
1321
1322
1323
1324
1325static void ch9getstatus(struct fsl_udc *udc, u8 request_type, u16 value,
1326 u16 index, u16 length)
1327{
1328 u16 tmp = 0;
1329 struct fsl_req *req;
1330 struct fsl_ep *ep;
1331 int ret;
1332
1333 ep = &udc->eps[0];
1334
1335 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1336
1337 tmp = 1 << USB_DEVICE_SELF_POWERED;
1338 tmp |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
1339 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
1340
1341
1342 tmp = 0;
1343 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
1344
1345 struct fsl_ep *target_ep;
1346
1347 target_ep = get_ep_by_pipe(udc, get_pipe_by_windex(index));
1348
1349
1350 if (!target_ep->ep.desc)
1351 goto stall;
1352 tmp = dr_ep_get_stall(ep_index(target_ep), ep_is_in(target_ep))
1353 << USB_ENDPOINT_HALT;
1354 }
1355
1356 udc->ep0_dir = USB_DIR_IN;
1357
1358 req = udc->status_req;
1359
1360 *((u16 *) req->req.buf) = cpu_to_le16(tmp);
1361
1362 req->ep = ep;
1363 req->req.length = 2;
1364 req->req.status = -EINPROGRESS;
1365 req->req.actual = 0;
1366 req->req.complete = NULL;
1367 req->dtd_count = 0;
1368
1369 ret = usb_gadget_map_request(&ep->udc->gadget, &req->req, ep_is_in(ep));
1370 if (ret)
1371 goto stall;
1372
1373
1374 if ((fsl_req_to_dtd(req, GFP_ATOMIC) == 0))
1375 fsl_queue_td(ep, req);
1376 else
1377 goto stall;
1378
1379 list_add_tail(&req->queue, &ep->queue);
1380 udc->ep0_state = DATA_STATE_XMIT;
1381 if (ep0_prime_status(udc, EP_DIR_OUT))
1382 ep0stall(udc);
1383
1384 return;
1385stall:
1386 ep0stall(udc);
1387}
1388
1389static void setup_received_irq(struct fsl_udc *udc,
1390 struct usb_ctrlrequest *setup)
1391{
1392 u16 wValue = le16_to_cpu(setup->wValue);
1393 u16 wIndex = le16_to_cpu(setup->wIndex);
1394 u16 wLength = le16_to_cpu(setup->wLength);
1395
1396 udc_reset_ep_queue(udc, 0);
1397
1398
1399 switch (setup->bRequest) {
1400 case USB_REQ_GET_STATUS:
1401
1402 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
1403 != (USB_DIR_IN | USB_TYPE_STANDARD))
1404 break;
1405 ch9getstatus(udc, setup->bRequestType, wValue, wIndex, wLength);
1406 return;
1407
1408 case USB_REQ_SET_ADDRESS:
1409
1410 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD
1411 | USB_RECIP_DEVICE))
1412 break;
1413 ch9setaddress(udc, wValue, wIndex, wLength);
1414 return;
1415
1416 case USB_REQ_CLEAR_FEATURE:
1417 case USB_REQ_SET_FEATURE:
1418
1419 {
1420 int rc = -EOPNOTSUPP;
1421 u16 ptc = 0;
1422
1423 if ((setup->bRequestType & (USB_RECIP_MASK | USB_TYPE_MASK))
1424 == (USB_RECIP_ENDPOINT | USB_TYPE_STANDARD)) {
1425 int pipe = get_pipe_by_windex(wIndex);
1426 struct fsl_ep *ep;
1427
1428 if (wValue != 0 || wLength != 0 || pipe >= udc->max_ep)
1429 break;
1430 ep = get_ep_by_pipe(udc, pipe);
1431
1432 spin_unlock(&udc->lock);
1433 rc = fsl_ep_set_halt(&ep->ep,
1434 (setup->bRequest == USB_REQ_SET_FEATURE)
1435 ? 1 : 0);
1436 spin_lock(&udc->lock);
1437
1438 } else if ((setup->bRequestType & (USB_RECIP_MASK
1439 | USB_TYPE_MASK)) == (USB_RECIP_DEVICE
1440 | USB_TYPE_STANDARD)) {
1441
1442
1443 if (wValue == USB_DEVICE_TEST_MODE)
1444 ptc = wIndex >> 8;
1445 else if (gadget_is_otg(&udc->gadget)) {
1446 if (setup->bRequest ==
1447 USB_DEVICE_B_HNP_ENABLE)
1448 udc->gadget.b_hnp_enable = 1;
1449 else if (setup->bRequest ==
1450 USB_DEVICE_A_HNP_SUPPORT)
1451 udc->gadget.a_hnp_support = 1;
1452 else if (setup->bRequest ==
1453 USB_DEVICE_A_ALT_HNP_SUPPORT)
1454 udc->gadget.a_alt_hnp_support = 1;
1455 }
1456 rc = 0;
1457 } else
1458 break;
1459
1460 if (rc == 0) {
1461 if (ep0_prime_status(udc, EP_DIR_IN))
1462 ep0stall(udc);
1463 }
1464 if (ptc) {
1465 u32 tmp;
1466
1467 mdelay(10);
1468 tmp = fsl_readl(&dr_regs->portsc1) | (ptc << 16);
1469 fsl_writel(tmp, &dr_regs->portsc1);
1470 printk(KERN_INFO "udc: switch to test mode %d.\n", ptc);
1471 }
1472
1473 return;
1474 }
1475
1476 default:
1477 break;
1478 }
1479
1480
1481 if (wLength) {
1482
1483 udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
1484 ? USB_DIR_IN : USB_DIR_OUT;
1485 spin_unlock(&udc->lock);
1486 if (udc->driver->setup(&udc->gadget,
1487 &udc->local_setup_buff) < 0)
1488 ep0stall(udc);
1489 spin_lock(&udc->lock);
1490 udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
1491 ? DATA_STATE_XMIT : DATA_STATE_RECV;
1492
1493
1494
1495
1496 if (udc->ep0_state == DATA_STATE_XMIT)
1497 if (ep0_prime_status(udc, EP_DIR_OUT))
1498 ep0stall(udc);
1499
1500 } else {
1501
1502 udc->ep0_dir = USB_DIR_IN;
1503 spin_unlock(&udc->lock);
1504 if (udc->driver->setup(&udc->gadget,
1505 &udc->local_setup_buff) < 0)
1506 ep0stall(udc);
1507 spin_lock(&udc->lock);
1508 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1509 }
1510}
1511
1512
1513
1514static void ep0_req_complete(struct fsl_udc *udc, struct fsl_ep *ep0,
1515 struct fsl_req *req)
1516{
1517 if (udc->usb_state == USB_STATE_ADDRESS) {
1518
1519 u32 new_address = (u32) udc->device_address;
1520 fsl_writel(new_address << USB_DEVICE_ADDRESS_BIT_POS,
1521 &dr_regs->deviceaddr);
1522 }
1523
1524 done(ep0, req, 0);
1525
1526 switch (udc->ep0_state) {
1527 case DATA_STATE_XMIT:
1528
1529 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1530 break;
1531 case DATA_STATE_RECV:
1532
1533 if (ep0_prime_status(udc, EP_DIR_IN))
1534 ep0stall(udc);
1535 break;
1536 case WAIT_FOR_OUT_STATUS:
1537 udc->ep0_state = WAIT_FOR_SETUP;
1538 break;
1539 case WAIT_FOR_SETUP:
1540 ERR("Unexpect ep0 packets\n");
1541 break;
1542 default:
1543 ep0stall(udc);
1544 break;
1545 }
1546}
1547
1548
1549
1550static void tripwire_handler(struct fsl_udc *udc, u8 ep_num, u8 *buffer_ptr)
1551{
1552 u32 temp;
1553 struct ep_queue_head *qh;
1554 struct fsl_usb2_platform_data *pdata = udc->pdata;
1555
1556 qh = &udc->ep_qh[ep_num * 2 + EP_DIR_OUT];
1557
1558
1559 temp = fsl_readl(&dr_regs->endptsetupstat);
1560 fsl_writel(temp | (1 << ep_num), &dr_regs->endptsetupstat);
1561
1562
1563 do {
1564
1565 temp = fsl_readl(&dr_regs->usbcmd);
1566 fsl_writel(temp | USB_CMD_SUTW, &dr_regs->usbcmd);
1567
1568
1569 if (pdata->le_setup_buf) {
1570 u32 *p = (u32 *)buffer_ptr;
1571 u32 *s = (u32 *)qh->setup_buffer;
1572
1573
1574 *p++ = le32_to_cpu(*s++);
1575 *p = le32_to_cpu(*s);
1576 } else {
1577 memcpy(buffer_ptr, (u8 *) qh->setup_buffer, 8);
1578 }
1579 } while (!(fsl_readl(&dr_regs->usbcmd) & USB_CMD_SUTW));
1580
1581
1582 temp = fsl_readl(&dr_regs->usbcmd);
1583 fsl_writel(temp & ~USB_CMD_SUTW, &dr_regs->usbcmd);
1584}
1585
1586
1587static int process_ep_req(struct fsl_udc *udc, int pipe,
1588 struct fsl_req *curr_req)
1589{
1590 struct ep_td_struct *curr_td;
1591 int td_complete, actual, remaining_length, j, tmp;
1592 int status = 0;
1593 int errors = 0;
1594 struct ep_queue_head *curr_qh = &udc->ep_qh[pipe];
1595 int direction = pipe % 2;
1596
1597 curr_td = curr_req->head;
1598 td_complete = 0;
1599 actual = curr_req->req.length;
1600
1601 for (j = 0; j < curr_req->dtd_count; j++) {
1602 remaining_length = (hc32_to_cpu(curr_td->size_ioc_sts)
1603 & DTD_PACKET_SIZE)
1604 >> DTD_LENGTH_BIT_POS;
1605 actual -= remaining_length;
1606
1607 errors = hc32_to_cpu(curr_td->size_ioc_sts);
1608 if (errors & DTD_ERROR_MASK) {
1609 if (errors & DTD_STATUS_HALTED) {
1610 ERR("dTD error %08x QH=%d\n", errors, pipe);
1611
1612 tmp = hc32_to_cpu(curr_qh->size_ioc_int_sts);
1613 tmp &= ~errors;
1614 curr_qh->size_ioc_int_sts = cpu_to_hc32(tmp);
1615 status = -EPIPE;
1616
1617
1618 break;
1619 }
1620 if (errors & DTD_STATUS_DATA_BUFF_ERR) {
1621 VDBG("Transfer overflow");
1622 status = -EPROTO;
1623 break;
1624 } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
1625 VDBG("ISO error");
1626 status = -EILSEQ;
1627 break;
1628 } else
1629 ERR("Unknown error has occurred (0x%x)!\n",
1630 errors);
1631
1632 } else if (hc32_to_cpu(curr_td->size_ioc_sts)
1633 & DTD_STATUS_ACTIVE) {
1634 VDBG("Request not complete");
1635 status = REQ_UNCOMPLETE;
1636 return status;
1637 } else if (remaining_length) {
1638 if (direction) {
1639 VDBG("Transmit dTD remaining length not zero");
1640 status = -EPROTO;
1641 break;
1642 } else {
1643 td_complete++;
1644 break;
1645 }
1646 } else {
1647 td_complete++;
1648 VDBG("dTD transmitted successful");
1649 }
1650
1651 if (j != curr_req->dtd_count - 1)
1652 curr_td = (struct ep_td_struct *)curr_td->next_td_virt;
1653 }
1654
1655 if (status)
1656 return status;
1657
1658 curr_req->req.actual = actual;
1659
1660 return 0;
1661}
1662
1663
1664static void dtd_complete_irq(struct fsl_udc *udc)
1665{
1666 u32 bit_pos;
1667 int i, ep_num, direction, bit_mask, status;
1668 struct fsl_ep *curr_ep;
1669 struct fsl_req *curr_req, *temp_req;
1670
1671
1672 bit_pos = fsl_readl(&dr_regs->endptcomplete);
1673 fsl_writel(bit_pos, &dr_regs->endptcomplete);
1674
1675 if (!bit_pos)
1676 return;
1677
1678 for (i = 0; i < udc->max_ep; i++) {
1679 ep_num = i >> 1;
1680 direction = i % 2;
1681
1682 bit_mask = 1 << (ep_num + 16 * direction);
1683
1684 if (!(bit_pos & bit_mask))
1685 continue;
1686
1687 curr_ep = get_ep_by_pipe(udc, i);
1688
1689
1690 if (curr_ep->name == NULL) {
1691 WARNING("Invalid EP?");
1692 continue;
1693 }
1694
1695
1696 list_for_each_entry_safe(curr_req, temp_req, &curr_ep->queue,
1697 queue) {
1698 status = process_ep_req(udc, i, curr_req);
1699
1700 VDBG("status of process_ep_req= %d, ep = %d",
1701 status, ep_num);
1702 if (status == REQ_UNCOMPLETE)
1703 break;
1704
1705 curr_req->req.status = status;
1706
1707 if (ep_num == 0) {
1708 ep0_req_complete(udc, curr_ep, curr_req);
1709 break;
1710 } else
1711 done(curr_ep, curr_req, status);
1712 }
1713 }
1714}
1715
1716static inline enum usb_device_speed portscx_device_speed(u32 reg)
1717{
1718 switch (reg & PORTSCX_PORT_SPEED_MASK) {
1719 case PORTSCX_PORT_SPEED_HIGH:
1720 return USB_SPEED_HIGH;
1721 case PORTSCX_PORT_SPEED_FULL:
1722 return USB_SPEED_FULL;
1723 case PORTSCX_PORT_SPEED_LOW:
1724 return USB_SPEED_LOW;
1725 default:
1726 return USB_SPEED_UNKNOWN;
1727 }
1728}
1729
1730
1731static void port_change_irq(struct fsl_udc *udc)
1732{
1733 if (udc->bus_reset)
1734 udc->bus_reset = 0;
1735
1736
1737 if (!(fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET))
1738
1739 udc->gadget.speed =
1740 portscx_device_speed(fsl_readl(&dr_regs->portsc1));
1741
1742
1743 if (!udc->resume_state)
1744 udc->usb_state = USB_STATE_DEFAULT;
1745}
1746
1747
1748static void suspend_irq(struct fsl_udc *udc)
1749{
1750 udc->resume_state = udc->usb_state;
1751 udc->usb_state = USB_STATE_SUSPENDED;
1752
1753
1754 if (udc->driver->suspend)
1755 udc->driver->suspend(&udc->gadget);
1756}
1757
1758static void bus_resume(struct fsl_udc *udc)
1759{
1760 udc->usb_state = udc->resume_state;
1761 udc->resume_state = 0;
1762
1763
1764 if (udc->driver->resume)
1765 udc->driver->resume(&udc->gadget);
1766}
1767
1768
1769static int reset_queues(struct fsl_udc *udc)
1770{
1771 u8 pipe;
1772
1773 for (pipe = 0; pipe < udc->max_pipes; pipe++)
1774 udc_reset_ep_queue(udc, pipe);
1775
1776
1777 spin_unlock(&udc->lock);
1778 udc->driver->disconnect(&udc->gadget);
1779 spin_lock(&udc->lock);
1780
1781 return 0;
1782}
1783
1784
1785static void reset_irq(struct fsl_udc *udc)
1786{
1787 u32 temp;
1788 unsigned long timeout;
1789
1790
1791 temp = fsl_readl(&dr_regs->deviceaddr);
1792 fsl_writel(temp & ~USB_DEVICE_ADDRESS_MASK, &dr_regs->deviceaddr);
1793
1794 udc->device_address = 0;
1795
1796
1797 udc->resume_state = 0;
1798 udc->ep0_dir = 0;
1799 udc->ep0_state = WAIT_FOR_SETUP;
1800 udc->remote_wakeup = 0;
1801 udc->gadget.b_hnp_enable = 0;
1802 udc->gadget.a_hnp_support = 0;
1803 udc->gadget.a_alt_hnp_support = 0;
1804
1805
1806 temp = fsl_readl(&dr_regs->endptsetupstat);
1807 fsl_writel(temp, &dr_regs->endptsetupstat);
1808
1809
1810 temp = fsl_readl(&dr_regs->endptcomplete);
1811 fsl_writel(temp, &dr_regs->endptcomplete);
1812
1813 timeout = jiffies + 100;
1814 while (fsl_readl(&dr_regs->endpointprime)) {
1815
1816 if (time_after(jiffies, timeout)) {
1817 ERR("Timeout for reset\n");
1818 break;
1819 }
1820 cpu_relax();
1821 }
1822
1823
1824 fsl_writel(0xffffffff, &dr_regs->endptflush);
1825
1826 if (fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_RESET) {
1827 VDBG("Bus reset");
1828
1829 udc->bus_reset = 1;
1830
1831
1832 reset_queues(udc);
1833 udc->usb_state = USB_STATE_DEFAULT;
1834 } else {
1835 VDBG("Controller reset");
1836
1837
1838 dr_controller_setup(udc);
1839
1840
1841 reset_queues(udc);
1842
1843 ep0_setup(udc);
1844
1845
1846 dr_controller_run(udc);
1847 udc->usb_state = USB_STATE_ATTACHED;
1848 }
1849}
1850
1851
1852
1853
1854static irqreturn_t fsl_udc_irq(int irq, void *_udc)
1855{
1856 struct fsl_udc *udc = _udc;
1857 u32 irq_src;
1858 irqreturn_t status = IRQ_NONE;
1859 unsigned long flags;
1860
1861
1862 if (udc->stopped)
1863 return IRQ_NONE;
1864 spin_lock_irqsave(&udc->lock, flags);
1865 irq_src = fsl_readl(&dr_regs->usbsts) & fsl_readl(&dr_regs->usbintr);
1866
1867 fsl_writel(irq_src, &dr_regs->usbsts);
1868
1869
1870
1871
1872 if (udc->usb_state == USB_STATE_SUSPENDED)
1873 if ((fsl_readl(&dr_regs->portsc1) & PORTSCX_PORT_SUSPEND) == 0)
1874 bus_resume(udc);
1875
1876
1877 if (irq_src & USB_STS_INT) {
1878 VDBG("Packet int");
1879
1880 if (fsl_readl(&dr_regs->endptsetupstat) & EP_SETUP_STATUS_EP0) {
1881 tripwire_handler(udc, 0,
1882 (u8 *) (&udc->local_setup_buff));
1883 setup_received_irq(udc, &udc->local_setup_buff);
1884 status = IRQ_HANDLED;
1885 }
1886
1887
1888 if (fsl_readl(&dr_regs->endptcomplete)) {
1889 dtd_complete_irq(udc);
1890 status = IRQ_HANDLED;
1891 }
1892 }
1893
1894
1895 if (irq_src & USB_STS_SOF) {
1896 status = IRQ_HANDLED;
1897 }
1898
1899
1900 if (irq_src & USB_STS_PORT_CHANGE) {
1901 port_change_irq(udc);
1902 status = IRQ_HANDLED;
1903 }
1904
1905
1906 if (irq_src & USB_STS_RESET) {
1907 VDBG("reset int");
1908 reset_irq(udc);
1909 status = IRQ_HANDLED;
1910 }
1911
1912
1913 if (irq_src & USB_STS_SUSPEND) {
1914 suspend_irq(udc);
1915 status = IRQ_HANDLED;
1916 }
1917
1918 if (irq_src & (USB_STS_ERR | USB_STS_SYS_ERR)) {
1919 VDBG("Error IRQ %x", irq_src);
1920 }
1921
1922 spin_unlock_irqrestore(&udc->lock, flags);
1923 return status;
1924}
1925
1926
1927
1928
1929
1930static int fsl_udc_start(struct usb_gadget *g,
1931 struct usb_gadget_driver *driver)
1932{
1933 int retval = 0;
1934 unsigned long flags = 0;
1935
1936
1937 spin_lock_irqsave(&udc_controller->lock, flags);
1938
1939 driver->driver.bus = NULL;
1940
1941 udc_controller->driver = driver;
1942 spin_unlock_irqrestore(&udc_controller->lock, flags);
1943
1944 if (!IS_ERR_OR_NULL(udc_controller->transceiver)) {
1945
1946 udc_controller->stopped = 1;
1947 printk(KERN_INFO "Suspend udc for OTG auto detect\n");
1948
1949
1950 if (!IS_ERR_OR_NULL(udc_controller->transceiver)) {
1951 retval = otg_set_peripheral(
1952 udc_controller->transceiver->otg,
1953 &udc_controller->gadget);
1954 if (retval < 0) {
1955 ERR("can't bind to transceiver\n");
1956 driver->unbind(&udc_controller->gadget);
1957 udc_controller->driver = 0;
1958 return retval;
1959 }
1960 }
1961 } else {
1962
1963 dr_controller_run(udc_controller);
1964 udc_controller->usb_state = USB_STATE_ATTACHED;
1965 udc_controller->ep0_state = WAIT_FOR_SETUP;
1966 udc_controller->ep0_dir = 0;
1967 }
1968
1969 return retval;
1970}
1971
1972
1973static int fsl_udc_stop(struct usb_gadget *g,
1974 struct usb_gadget_driver *driver)
1975{
1976 struct fsl_ep *loop_ep;
1977 unsigned long flags;
1978
1979 if (!IS_ERR_OR_NULL(udc_controller->transceiver))
1980 otg_set_peripheral(udc_controller->transceiver->otg, NULL);
1981
1982
1983 dr_controller_stop(udc_controller);
1984
1985
1986 udc_controller->usb_state = USB_STATE_ATTACHED;
1987 udc_controller->ep0_state = WAIT_FOR_SETUP;
1988 udc_controller->ep0_dir = 0;
1989
1990
1991 spin_lock_irqsave(&udc_controller->lock, flags);
1992 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
1993 nuke(&udc_controller->eps[0], -ESHUTDOWN);
1994 list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
1995 ep.ep_list)
1996 nuke(loop_ep, -ESHUTDOWN);
1997 spin_unlock_irqrestore(&udc_controller->lock, flags);
1998
1999 udc_controller->driver = NULL;
2000
2001 return 0;
2002}
2003
2004
2005
2006
2007#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2008
2009#include <linux/seq_file.h>
2010
2011static const char proc_filename[] = "driver/fsl_usb2_udc";
2012
2013static int fsl_proc_read(struct seq_file *m, void *v)
2014{
2015 unsigned long flags;
2016 int i;
2017 u32 tmp_reg;
2018 struct fsl_ep *ep = NULL;
2019 struct fsl_req *req;
2020
2021 struct fsl_udc *udc = udc_controller;
2022
2023 spin_lock_irqsave(&udc->lock, flags);
2024
2025
2026 seq_printf(m,
2027 DRIVER_DESC "\n"
2028 "%s version: %s\n"
2029 "Gadget driver: %s\n\n",
2030 driver_name, DRIVER_VERSION,
2031 udc->driver ? udc->driver->driver.name : "(none)");
2032
2033
2034 tmp_reg = fsl_readl(&dr_regs->usbcmd);
2035 seq_printf(m,
2036 "USBCMD reg:\n"
2037 "SetupTW: %d\n"
2038 "Run/Stop: %s\n\n",
2039 (tmp_reg & USB_CMD_SUTW) ? 1 : 0,
2040 (tmp_reg & USB_CMD_RUN_STOP) ? "Run" : "Stop");
2041
2042 tmp_reg = fsl_readl(&dr_regs->usbsts);
2043 seq_printf(m,
2044 "USB Status Reg:\n"
2045 "Dr Suspend: %d Reset Received: %d System Error: %s "
2046 "USB Error Interrupt: %s\n\n",
2047 (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
2048 (tmp_reg & USB_STS_RESET) ? 1 : 0,
2049 (tmp_reg & USB_STS_SYS_ERR) ? "Err" : "Normal",
2050 (tmp_reg & USB_STS_ERR) ? "Err detected" : "No err");
2051
2052 tmp_reg = fsl_readl(&dr_regs->usbintr);
2053 seq_printf(m,
2054 "USB Interrupt Enable Reg:\n"
2055 "Sleep Enable: %d SOF Received Enable: %d "
2056 "Reset Enable: %d\n"
2057 "System Error Enable: %d "
2058 "Port Change Dectected Enable: %d\n"
2059 "USB Error Intr Enable: %d USB Intr Enable: %d\n\n",
2060 (tmp_reg & USB_INTR_DEVICE_SUSPEND) ? 1 : 0,
2061 (tmp_reg & USB_INTR_SOF_EN) ? 1 : 0,
2062 (tmp_reg & USB_INTR_RESET_EN) ? 1 : 0,
2063 (tmp_reg & USB_INTR_SYS_ERR_EN) ? 1 : 0,
2064 (tmp_reg & USB_INTR_PTC_DETECT_EN) ? 1 : 0,
2065 (tmp_reg & USB_INTR_ERR_INT_EN) ? 1 : 0,
2066 (tmp_reg & USB_INTR_INT_EN) ? 1 : 0);
2067
2068 tmp_reg = fsl_readl(&dr_regs->frindex);
2069 seq_printf(m,
2070 "USB Frame Index Reg: Frame Number is 0x%x\n\n",
2071 (tmp_reg & USB_FRINDEX_MASKS));
2072
2073 tmp_reg = fsl_readl(&dr_regs->deviceaddr);
2074 seq_printf(m,
2075 "USB Device Address Reg: Device Addr is 0x%x\n\n",
2076 (tmp_reg & USB_DEVICE_ADDRESS_MASK));
2077
2078 tmp_reg = fsl_readl(&dr_regs->endpointlistaddr);
2079 seq_printf(m,
2080 "USB Endpoint List Address Reg: "
2081 "Device Addr is 0x%x\n\n",
2082 (tmp_reg & USB_EP_LIST_ADDRESS_MASK));
2083
2084 tmp_reg = fsl_readl(&dr_regs->portsc1);
2085 seq_printf(m,
2086 "USB Port Status&Control Reg:\n"
2087 "Port Transceiver Type : %s Port Speed: %s\n"
2088 "PHY Low Power Suspend: %s Port Reset: %s "
2089 "Port Suspend Mode: %s\n"
2090 "Over-current Change: %s "
2091 "Port Enable/Disable Change: %s\n"
2092 "Port Enabled/Disabled: %s "
2093 "Current Connect Status: %s\n\n", ( {
2094 const char *s;
2095 switch (tmp_reg & PORTSCX_PTS_FSLS) {
2096 case PORTSCX_PTS_UTMI:
2097 s = "UTMI"; break;
2098 case PORTSCX_PTS_ULPI:
2099 s = "ULPI "; break;
2100 case PORTSCX_PTS_FSLS:
2101 s = "FS/LS Serial"; break;
2102 default:
2103 s = "None"; break;
2104 }
2105 s;} ),
2106 usb_speed_string(portscx_device_speed(tmp_reg)),
2107 (tmp_reg & PORTSCX_PHY_LOW_POWER_SPD) ?
2108 "Normal PHY mode" : "Low power mode",
2109 (tmp_reg & PORTSCX_PORT_RESET) ? "In Reset" :
2110 "Not in Reset",
2111 (tmp_reg & PORTSCX_PORT_SUSPEND) ? "In " : "Not in",
2112 (tmp_reg & PORTSCX_OVER_CURRENT_CHG) ? "Dected" :
2113 "No",
2114 (tmp_reg & PORTSCX_PORT_EN_DIS_CHANGE) ? "Disable" :
2115 "Not change",
2116 (tmp_reg & PORTSCX_PORT_ENABLE) ? "Enable" :
2117 "Not correct",
2118 (tmp_reg & PORTSCX_CURRENT_CONNECT_STATUS) ?
2119 "Attached" : "Not-Att");
2120
2121 tmp_reg = fsl_readl(&dr_regs->usbmode);
2122 seq_printf(m,
2123 "USB Mode Reg: Controller Mode is: %s\n\n", ( {
2124 const char *s;
2125 switch (tmp_reg & USB_MODE_CTRL_MODE_HOST) {
2126 case USB_MODE_CTRL_MODE_IDLE:
2127 s = "Idle"; break;
2128 case USB_MODE_CTRL_MODE_DEVICE:
2129 s = "Device Controller"; break;
2130 case USB_MODE_CTRL_MODE_HOST:
2131 s = "Host Controller"; break;
2132 default:
2133 s = "None"; break;
2134 }
2135 s;
2136 } ));
2137
2138 tmp_reg = fsl_readl(&dr_regs->endptsetupstat);
2139 seq_printf(m,
2140 "Endpoint Setup Status Reg: SETUP on ep 0x%x\n\n",
2141 (tmp_reg & EP_SETUP_STATUS_MASK));
2142
2143 for (i = 0; i < udc->max_ep / 2; i++) {
2144 tmp_reg = fsl_readl(&dr_regs->endptctrl[i]);
2145 seq_printf(m, "EP Ctrl Reg [0x%x]: = [0x%x]\n", i, tmp_reg);
2146 }
2147 tmp_reg = fsl_readl(&dr_regs->endpointprime);
2148 seq_printf(m, "EP Prime Reg = [0x%x]\n\n", tmp_reg);
2149
2150#ifndef CONFIG_ARCH_MXC
2151 if (udc->pdata->have_sysif_regs) {
2152 tmp_reg = usb_sys_regs->snoop1;
2153 seq_printf(m, "Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
2154
2155 tmp_reg = usb_sys_regs->control;
2156 seq_printf(m, "General Control Reg : = [0x%x]\n\n", tmp_reg);
2157 }
2158#endif
2159
2160
2161 ep = &udc->eps[0];
2162 seq_printf(m, "For %s Maxpkt is 0x%x index is 0x%x\n",
2163 ep->ep.name, ep_maxpacket(ep), ep_index(ep));
2164
2165 if (list_empty(&ep->queue)) {
2166 seq_puts(m, "its req queue is empty\n\n");
2167 } else {
2168 list_for_each_entry(req, &ep->queue, queue) {
2169 seq_printf(m,
2170 "req %p actual 0x%x length 0x%x buf %p\n",
2171 &req->req, req->req.actual,
2172 req->req.length, req->req.buf);
2173 }
2174 }
2175
2176 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2177 if (ep->ep.desc) {
2178 seq_printf(m,
2179 "\nFor %s Maxpkt is 0x%x "
2180 "index is 0x%x\n",
2181 ep->ep.name, ep_maxpacket(ep),
2182 ep_index(ep));
2183
2184 if (list_empty(&ep->queue)) {
2185 seq_puts(m, "its req queue is empty\n\n");
2186 } else {
2187 list_for_each_entry(req, &ep->queue, queue) {
2188 seq_printf(m,
2189 "req %p actual 0x%x length "
2190 "0x%x buf %p\n",
2191 &req->req, req->req.actual,
2192 req->req.length, req->req.buf);
2193 }
2194 }
2195 }
2196 }
2197
2198 spin_unlock_irqrestore(&udc->lock, flags);
2199 return 0;
2200}
2201
2202
2203
2204
2205static int fsl_proc_open(struct inode *inode, struct file *file)
2206{
2207 return single_open(file, fsl_proc_read, NULL);
2208}
2209
2210static const struct file_operations fsl_proc_fops = {
2211 .open = fsl_proc_open,
2212 .read = seq_read,
2213 .llseek = seq_lseek,
2214 .release = single_release,
2215};
2216
2217#define create_proc_file() proc_create(proc_filename, 0, NULL, &fsl_proc_fops)
2218#define remove_proc_file() remove_proc_entry(proc_filename, NULL)
2219
2220#else
2221
2222#define create_proc_file() do {} while (0)
2223#define remove_proc_file() do {} while (0)
2224
2225#endif
2226
2227
2228
2229
2230static void fsl_udc_release(struct device *dev)
2231{
2232 complete(udc_controller->done);
2233 dma_free_coherent(dev->parent, udc_controller->ep_qh_size,
2234 udc_controller->ep_qh, udc_controller->ep_qh_dma);
2235 kfree(udc_controller);
2236}
2237
2238
2239
2240
2241
2242
2243
2244
2245static int __init struct_udc_setup(struct fsl_udc *udc,
2246 struct platform_device *pdev)
2247{
2248 struct fsl_usb2_platform_data *pdata;
2249 size_t size;
2250
2251 pdata = dev_get_platdata(&pdev->dev);
2252 udc->phy_mode = pdata->phy_mode;
2253
2254 udc->eps = kzalloc(sizeof(struct fsl_ep) * udc->max_ep, GFP_KERNEL);
2255 if (!udc->eps) {
2256 ERR("malloc fsl_ep failed\n");
2257 return -1;
2258 }
2259
2260
2261 size = udc->max_ep * sizeof(struct ep_queue_head);
2262 if (size < QH_ALIGNMENT)
2263 size = QH_ALIGNMENT;
2264 else if ((size % QH_ALIGNMENT) != 0) {
2265 size += QH_ALIGNMENT + 1;
2266 size &= ~(QH_ALIGNMENT - 1);
2267 }
2268 udc->ep_qh = dma_alloc_coherent(&pdev->dev, size,
2269 &udc->ep_qh_dma, GFP_KERNEL);
2270 if (!udc->ep_qh) {
2271 ERR("malloc QHs for udc failed\n");
2272 kfree(udc->eps);
2273 return -1;
2274 }
2275
2276 udc->ep_qh_size = size;
2277
2278
2279
2280 udc->status_req = container_of(fsl_alloc_request(NULL, GFP_KERNEL),
2281 struct fsl_req, req);
2282
2283 udc->status_req->req.buf = kmalloc(8, GFP_KERNEL);
2284
2285 udc->resume_state = USB_STATE_NOTATTACHED;
2286 udc->usb_state = USB_STATE_POWERED;
2287 udc->ep0_dir = 0;
2288 udc->remote_wakeup = 0;
2289
2290 return 0;
2291}
2292
2293
2294
2295
2296
2297
2298
2299static int __init struct_ep_setup(struct fsl_udc *udc, unsigned char index,
2300 char *name, int link)
2301{
2302 struct fsl_ep *ep = &udc->eps[index];
2303
2304 ep->udc = udc;
2305 strcpy(ep->name, name);
2306 ep->ep.name = ep->name;
2307
2308 ep->ep.ops = &fsl_ep_ops;
2309 ep->stopped = 0;
2310
2311
2312
2313
2314 usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
2315
2316
2317 INIT_LIST_HEAD(&ep->queue);
2318
2319
2320 if (link)
2321 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2322 ep->gadget = &udc->gadget;
2323 ep->qh = &udc->ep_qh[index];
2324
2325 return 0;
2326}
2327
2328
2329
2330
2331
2332static int __init fsl_udc_probe(struct platform_device *pdev)
2333{
2334 struct fsl_usb2_platform_data *pdata;
2335 struct resource *res;
2336 int ret = -ENODEV;
2337 unsigned int i;
2338 u32 dccparams;
2339
2340 udc_controller = kzalloc(sizeof(struct fsl_udc), GFP_KERNEL);
2341 if (udc_controller == NULL) {
2342 ERR("malloc udc failed\n");
2343 return -ENOMEM;
2344 }
2345
2346 pdata = dev_get_platdata(&pdev->dev);
2347 udc_controller->pdata = pdata;
2348 spin_lock_init(&udc_controller->lock);
2349 udc_controller->stopped = 1;
2350
2351#ifdef CONFIG_USB_OTG
2352 if (pdata->operating_mode == FSL_USB2_DR_OTG) {
2353 udc_controller->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
2354 if (IS_ERR_OR_NULL(udc_controller->transceiver)) {
2355 ERR("Can't find OTG driver!\n");
2356 ret = -ENODEV;
2357 goto err_kfree;
2358 }
2359 }
2360#endif
2361
2362 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2363 if (!res) {
2364 ret = -ENXIO;
2365 goto err_kfree;
2366 }
2367
2368 if (pdata->operating_mode == FSL_USB2_DR_DEVICE) {
2369 if (!request_mem_region(res->start, resource_size(res),
2370 driver_name)) {
2371 ERR("request mem region for %s failed\n", pdev->name);
2372 ret = -EBUSY;
2373 goto err_kfree;
2374 }
2375 }
2376
2377 dr_regs = ioremap(res->start, resource_size(res));
2378 if (!dr_regs) {
2379 ret = -ENOMEM;
2380 goto err_release_mem_region;
2381 }
2382
2383 pdata->regs = (void *)dr_regs;
2384
2385
2386
2387
2388 if (pdata->init && pdata->init(pdev)) {
2389 ret = -ENODEV;
2390 goto err_iounmap_noclk;
2391 }
2392
2393
2394 fsl_set_accessors(pdata);
2395
2396#ifndef CONFIG_ARCH_MXC
2397 if (pdata->have_sysif_regs)
2398 usb_sys_regs = (void *)dr_regs + USB_DR_SYS_OFFSET;
2399#endif
2400
2401
2402 ret = fsl_udc_clk_init(pdev);
2403 if (ret < 0)
2404 goto err_iounmap_noclk;
2405
2406
2407 dccparams = fsl_readl(&dr_regs->dccparams);
2408 if (!(dccparams & DCCPARAMS_DC)) {
2409 ERR("This SOC doesn't support device role\n");
2410 ret = -ENODEV;
2411 goto err_iounmap;
2412 }
2413
2414
2415 udc_controller->max_ep = (dccparams & DCCPARAMS_DEN_MASK) * 2;
2416
2417 udc_controller->irq = platform_get_irq(pdev, 0);
2418 if (!udc_controller->irq) {
2419 ret = -ENODEV;
2420 goto err_iounmap;
2421 }
2422
2423 ret = request_irq(udc_controller->irq, fsl_udc_irq, IRQF_SHARED,
2424 driver_name, udc_controller);
2425 if (ret != 0) {
2426 ERR("cannot request irq %d err %d\n",
2427 udc_controller->irq, ret);
2428 goto err_iounmap;
2429 }
2430
2431
2432 if (struct_udc_setup(udc_controller, pdev)) {
2433 ERR("Can't initialize udc data structure\n");
2434 ret = -ENOMEM;
2435 goto err_free_irq;
2436 }
2437
2438 if (IS_ERR_OR_NULL(udc_controller->transceiver)) {
2439
2440
2441 dr_controller_setup(udc_controller);
2442 }
2443
2444 ret = fsl_udc_clk_finalize(pdev);
2445 if (ret)
2446 goto err_free_irq;
2447
2448
2449 udc_controller->gadget.ops = &fsl_gadget_ops;
2450 udc_controller->gadget.max_speed = USB_SPEED_HIGH;
2451 udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
2452 INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
2453 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
2454 udc_controller->gadget.name = driver_name;
2455
2456
2457 dev_set_name(&udc_controller->gadget.dev, "gadget");
2458 udc_controller->gadget.dev.of_node = pdev->dev.of_node;
2459
2460 if (!IS_ERR_OR_NULL(udc_controller->transceiver))
2461 udc_controller->gadget.is_otg = 1;
2462
2463
2464 ep0_setup(udc_controller);
2465
2466
2467 struct_ep_setup(udc_controller, 0, "ep0", 0);
2468
2469
2470
2471 udc_controller->eps[0].ep.desc = &fsl_ep0_desc;
2472 usb_ep_set_maxpacket_limit(&udc_controller->eps[0].ep,
2473 USB_MAX_CTRL_PAYLOAD);
2474
2475
2476
2477 for (i = 1; i < (int)(udc_controller->max_ep / 2); i++) {
2478 char name[14];
2479
2480 sprintf(name, "ep%dout", i);
2481 struct_ep_setup(udc_controller, i * 2, name, 1);
2482 sprintf(name, "ep%din", i);
2483 struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
2484 }
2485
2486
2487 udc_controller->td_pool = dma_pool_create("udc_td", &pdev->dev,
2488 sizeof(struct ep_td_struct),
2489 DTD_ALIGNMENT, UDC_DMA_BOUNDARY);
2490 if (udc_controller->td_pool == NULL) {
2491 ret = -ENOMEM;
2492 goto err_free_irq;
2493 }
2494
2495 ret = usb_add_gadget_udc_release(&pdev->dev, &udc_controller->gadget,
2496 fsl_udc_release);
2497 if (ret)
2498 goto err_del_udc;
2499
2500 create_proc_file();
2501 return 0;
2502
2503err_del_udc:
2504 dma_pool_destroy(udc_controller->td_pool);
2505err_free_irq:
2506 free_irq(udc_controller->irq, udc_controller);
2507err_iounmap:
2508 if (pdata->exit)
2509 pdata->exit(pdev);
2510 fsl_udc_clk_release();
2511err_iounmap_noclk:
2512 iounmap(dr_regs);
2513err_release_mem_region:
2514 if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
2515 release_mem_region(res->start, resource_size(res));
2516err_kfree:
2517 kfree(udc_controller);
2518 udc_controller = NULL;
2519 return ret;
2520}
2521
2522
2523
2524
2525static int __exit fsl_udc_remove(struct platform_device *pdev)
2526{
2527 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2528 struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
2529
2530 DECLARE_COMPLETION(done);
2531
2532 if (!udc_controller)
2533 return -ENODEV;
2534
2535 usb_del_gadget_udc(&udc_controller->gadget);
2536 udc_controller->done = &done;
2537
2538 fsl_udc_clk_release();
2539
2540
2541 remove_proc_file();
2542
2543
2544 kfree(udc_controller->status_req->req.buf);
2545 kfree(udc_controller->status_req);
2546 kfree(udc_controller->eps);
2547
2548 dma_pool_destroy(udc_controller->td_pool);
2549 free_irq(udc_controller->irq, udc_controller);
2550 iounmap(dr_regs);
2551 if (pdata->operating_mode == FSL_USB2_DR_DEVICE)
2552 release_mem_region(res->start, resource_size(res));
2553
2554
2555 wait_for_completion(&done);
2556
2557
2558
2559
2560
2561 if (pdata->exit)
2562 pdata->exit(pdev);
2563
2564 return 0;
2565}
2566
2567
2568
2569
2570
2571static int fsl_udc_suspend(struct platform_device *pdev, pm_message_t state)
2572{
2573 dr_controller_stop(udc_controller);
2574 return 0;
2575}
2576
2577
2578
2579
2580
2581static int fsl_udc_resume(struct platform_device *pdev)
2582{
2583
2584 if (udc_controller->stopped) {
2585 dr_controller_setup(udc_controller);
2586 dr_controller_run(udc_controller);
2587 }
2588 udc_controller->usb_state = USB_STATE_ATTACHED;
2589 udc_controller->ep0_state = WAIT_FOR_SETUP;
2590 udc_controller->ep0_dir = 0;
2591 return 0;
2592}
2593
2594static int fsl_udc_otg_suspend(struct device *dev, pm_message_t state)
2595{
2596 struct fsl_udc *udc = udc_controller;
2597 u32 mode, usbcmd;
2598
2599 mode = fsl_readl(&dr_regs->usbmode) & USB_MODE_CTRL_MODE_MASK;
2600
2601 pr_debug("%s(): mode 0x%x stopped %d\n", __func__, mode, udc->stopped);
2602
2603
2604
2605
2606
2607
2608 if (udc->stopped) {
2609 pr_debug("gadget already stopped, leaving early\n");
2610 udc->already_stopped = 1;
2611 return 0;
2612 }
2613
2614 if (mode != USB_MODE_CTRL_MODE_DEVICE) {
2615 pr_debug("gadget not in device mode, leaving early\n");
2616 return 0;
2617 }
2618
2619
2620 usbcmd = fsl_readl(&dr_regs->usbcmd) & ~USB_CMD_RUN_STOP;
2621 fsl_writel(usbcmd, &dr_regs->usbcmd);
2622
2623 udc->stopped = 1;
2624
2625 pr_info("USB Gadget suspended\n");
2626
2627 return 0;
2628}
2629
2630static int fsl_udc_otg_resume(struct device *dev)
2631{
2632 pr_debug("%s(): stopped %d already_stopped %d\n", __func__,
2633 udc_controller->stopped, udc_controller->already_stopped);
2634
2635
2636
2637
2638
2639 if (udc_controller->already_stopped) {
2640 udc_controller->already_stopped = 0;
2641 pr_debug("gadget was already stopped, leaving early\n");
2642 return 0;
2643 }
2644
2645 pr_info("USB Gadget resume\n");
2646
2647 return fsl_udc_resume(NULL);
2648}
2649
2650
2651
2652static const struct platform_device_id fsl_udc_devtype[] = {
2653 {
2654 .name = "imx-udc-mx27",
2655 }, {
2656 .name = "imx-udc-mx51",
2657 }, {
2658
2659 }
2660};
2661MODULE_DEVICE_TABLE(platform, fsl_udc_devtype);
2662static struct platform_driver udc_driver = {
2663 .remove = __exit_p(fsl_udc_remove),
2664
2665 .id_table = fsl_udc_devtype,
2666
2667 .suspend = fsl_udc_suspend,
2668 .resume = fsl_udc_resume,
2669 .driver = {
2670 .name = driver_name,
2671 .owner = THIS_MODULE,
2672
2673 .suspend = fsl_udc_otg_suspend,
2674 .resume = fsl_udc_otg_resume,
2675 },
2676};
2677
2678module_platform_driver_probe(udc_driver, fsl_udc_probe);
2679
2680MODULE_DESCRIPTION(DRIVER_DESC);
2681MODULE_AUTHOR(DRIVER_AUTHOR);
2682MODULE_LICENSE("GPL");
2683MODULE_ALIAS("platform:fsl-usb2-udc");
2684