2 * Intel Langwell USB Device Controller driver
3 * Copyright (C) 2008-2009, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 /* #undef VERBOSE_DEBUG */
24 #if defined(CONFIG_USB_LANGWELL_OTG)
25 #define OTG_TRANSCEIVER
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/kernel.h>
33 #include <linux/delay.h>
34 #include <linux/ioport.h>
35 #include <linux/sched.h>
36 #include <linux/slab.h>
37 #include <linux/errno.h>
38 #include <linux/init.h>
39 #include <linux/timer.h>
40 #include <linux/list.h>
41 #include <linux/interrupt.h>
42 #include <linux/moduleparam.h>
43 #include <linux/device.h>
44 #include <linux/usb/ch9.h>
45 #include <linux/usb/gadget.h>
46 #include <linux/usb/otg.h>
49 #include <linux/irq.h>
50 #include <asm/system.h>
51 #include <asm/unaligned.h>
53 #include "langwell_udc.h"
56 #define DRIVER_DESC "Intel Langwell USB Device Controller driver"
57 #define DRIVER_VERSION "16 May 2009"
59 static const char driver_name
[] = "langwell_udc";
60 static const char driver_desc
[] = DRIVER_DESC
;
63 /* controller device global variable */
64 static struct langwell_udc
*the_controller
;
66 /* for endpoint 0 operations */
67 static const struct usb_endpoint_descriptor
69 .bLength
= USB_DT_ENDPOINT_SIZE
,
70 .bDescriptorType
= USB_DT_ENDPOINT
,
71 .bEndpointAddress
= 0,
72 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
73 .wMaxPacketSize
= EP0_MAX_PKT_SIZE
,
77 /*-------------------------------------------------------------------------*/
81 static inline void print_all_registers(struct langwell_udc
*dev
)
85 /* Capability Registers */
86 dev_dbg(&dev
->pdev
->dev
,
87 "Capability Registers (offset: 0x%04x, length: 0x%08x)\n",
88 CAP_REG_OFFSET
, (u32
)sizeof(struct langwell_cap_regs
));
89 dev_dbg(&dev
->pdev
->dev
, "caplength=0x%02x\n",
90 readb(&dev
->cap_regs
->caplength
));
91 dev_dbg(&dev
->pdev
->dev
, "hciversion=0x%04x\n",
92 readw(&dev
->cap_regs
->hciversion
));
93 dev_dbg(&dev
->pdev
->dev
, "hcsparams=0x%08x\n",
94 readl(&dev
->cap_regs
->hcsparams
));
95 dev_dbg(&dev
->pdev
->dev
, "hccparams=0x%08x\n",
96 readl(&dev
->cap_regs
->hccparams
));
97 dev_dbg(&dev
->pdev
->dev
, "dciversion=0x%04x\n",
98 readw(&dev
->cap_regs
->dciversion
));
99 dev_dbg(&dev
->pdev
->dev
, "dccparams=0x%08x\n",
100 readl(&dev
->cap_regs
->dccparams
));
102 /* Operational Registers */
103 dev_dbg(&dev
->pdev
->dev
,
104 "Operational Registers (offset: 0x%04x, length: 0x%08x)\n",
105 OP_REG_OFFSET
, (u32
)sizeof(struct langwell_op_regs
));
106 dev_dbg(&dev
->pdev
->dev
, "extsts=0x%08x\n",
107 readl(&dev
->op_regs
->extsts
));
108 dev_dbg(&dev
->pdev
->dev
, "extintr=0x%08x\n",
109 readl(&dev
->op_regs
->extintr
));
110 dev_dbg(&dev
->pdev
->dev
, "usbcmd=0x%08x\n",
111 readl(&dev
->op_regs
->usbcmd
));
112 dev_dbg(&dev
->pdev
->dev
, "usbsts=0x%08x\n",
113 readl(&dev
->op_regs
->usbsts
));
114 dev_dbg(&dev
->pdev
->dev
, "usbintr=0x%08x\n",
115 readl(&dev
->op_regs
->usbintr
));
116 dev_dbg(&dev
->pdev
->dev
, "frindex=0x%08x\n",
117 readl(&dev
->op_regs
->frindex
));
118 dev_dbg(&dev
->pdev
->dev
, "ctrldssegment=0x%08x\n",
119 readl(&dev
->op_regs
->ctrldssegment
));
120 dev_dbg(&dev
->pdev
->dev
, "deviceaddr=0x%08x\n",
121 readl(&dev
->op_regs
->deviceaddr
));
122 dev_dbg(&dev
->pdev
->dev
, "endpointlistaddr=0x%08x\n",
123 readl(&dev
->op_regs
->endpointlistaddr
));
124 dev_dbg(&dev
->pdev
->dev
, "ttctrl=0x%08x\n",
125 readl(&dev
->op_regs
->ttctrl
));
126 dev_dbg(&dev
->pdev
->dev
, "burstsize=0x%08x\n",
127 readl(&dev
->op_regs
->burstsize
));
128 dev_dbg(&dev
->pdev
->dev
, "txfilltuning=0x%08x\n",
129 readl(&dev
->op_regs
->txfilltuning
));
130 dev_dbg(&dev
->pdev
->dev
, "txttfilltuning=0x%08x\n",
131 readl(&dev
->op_regs
->txttfilltuning
));
132 dev_dbg(&dev
->pdev
->dev
, "ic_usb=0x%08x\n",
133 readl(&dev
->op_regs
->ic_usb
));
134 dev_dbg(&dev
->pdev
->dev
, "ulpi_viewport=0x%08x\n",
135 readl(&dev
->op_regs
->ulpi_viewport
));
136 dev_dbg(&dev
->pdev
->dev
, "configflag=0x%08x\n",
137 readl(&dev
->op_regs
->configflag
));
138 dev_dbg(&dev
->pdev
->dev
, "portsc1=0x%08x\n",
139 readl(&dev
->op_regs
->portsc1
));
140 dev_dbg(&dev
->pdev
->dev
, "devlc=0x%08x\n",
141 readl(&dev
->op_regs
->devlc
));
142 dev_dbg(&dev
->pdev
->dev
, "otgsc=0x%08x\n",
143 readl(&dev
->op_regs
->otgsc
));
144 dev_dbg(&dev
->pdev
->dev
, "usbmode=0x%08x\n",
145 readl(&dev
->op_regs
->usbmode
));
146 dev_dbg(&dev
->pdev
->dev
, "endptnak=0x%08x\n",
147 readl(&dev
->op_regs
->endptnak
));
148 dev_dbg(&dev
->pdev
->dev
, "endptnaken=0x%08x\n",
149 readl(&dev
->op_regs
->endptnaken
));
150 dev_dbg(&dev
->pdev
->dev
, "endptsetupstat=0x%08x\n",
151 readl(&dev
->op_regs
->endptsetupstat
));
152 dev_dbg(&dev
->pdev
->dev
, "endptprime=0x%08x\n",
153 readl(&dev
->op_regs
->endptprime
));
154 dev_dbg(&dev
->pdev
->dev
, "endptflush=0x%08x\n",
155 readl(&dev
->op_regs
->endptflush
));
156 dev_dbg(&dev
->pdev
->dev
, "endptstat=0x%08x\n",
157 readl(&dev
->op_regs
->endptstat
));
158 dev_dbg(&dev
->pdev
->dev
, "endptcomplete=0x%08x\n",
159 readl(&dev
->op_regs
->endptcomplete
));
161 for (i
= 0; i
< dev
->ep_max
/ 2; i
++) {
162 dev_dbg(&dev
->pdev
->dev
, "endptctrl[%d]=0x%08x\n",
163 i
, readl(&dev
->op_regs
->endptctrl
[i
]));
168 #define print_all_registers(dev) do { } while (0)
170 #endif /* VERBOSE_DEBUG */
173 /*-------------------------------------------------------------------------*/
175 #define is_in(ep) (((ep)->ep_num == 0) ? ((ep)->dev->ep0_dir == \
176 USB_DIR_IN) : (usb_endpoint_dir_in((ep)->desc)))
178 #define DIR_STRING(ep) (is_in(ep) ? "in" : "out")
181 static char *type_string(const struct usb_endpoint_descriptor
*desc
)
183 switch (usb_endpoint_type(desc
)) {
184 case USB_ENDPOINT_XFER_BULK
:
186 case USB_ENDPOINT_XFER_ISOC
:
188 case USB_ENDPOINT_XFER_INT
:
196 /* configure endpoint control registers */
197 static void ep_reset(struct langwell_ep
*ep
, unsigned char ep_num
,
198 unsigned char is_in
, unsigned char ep_type
)
200 struct langwell_udc
*dev
;
204 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
206 endptctrl
= readl(&dev
->op_regs
->endptctrl
[ep_num
]);
207 if (is_in
) { /* TX */
209 endptctrl
|= EPCTRL_TXR
;
210 endptctrl
|= EPCTRL_TXE
;
211 endptctrl
|= ep_type
<< EPCTRL_TXT_SHIFT
;
214 endptctrl
|= EPCTRL_RXR
;
215 endptctrl
|= EPCTRL_RXE
;
216 endptctrl
|= ep_type
<< EPCTRL_RXT_SHIFT
;
219 writel(endptctrl
, &dev
->op_regs
->endptctrl
[ep_num
]);
221 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
225 /* reset ep0 dQH and endptctrl */
226 static void ep0_reset(struct langwell_udc
*dev
)
228 struct langwell_ep
*ep
;
231 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
234 for (i
= 0; i
< 2; i
++) {
239 ep
->dqh
= &dev
->ep_dqh
[i
];
241 /* configure ep0 endpoint capabilities in dQH */
242 ep
->dqh
->dqh_ios
= 1;
243 ep
->dqh
->dqh_mpl
= EP0_MAX_PKT_SIZE
;
245 /* enable ep0-in HW zero length termination select */
247 ep
->dqh
->dqh_zlt
= 0;
248 ep
->dqh
->dqh_mult
= 0;
250 ep
->dqh
->dtd_next
= DTD_TERM
;
252 /* configure ep0 control registers */
253 ep_reset(&dev
->ep
[0], 0, i
, USB_ENDPOINT_XFER_CONTROL
);
256 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
260 /*-------------------------------------------------------------------------*/
262 /* endpoints operations */
264 /* configure endpoint, making it usable */
265 static int langwell_ep_enable(struct usb_ep
*_ep
,
266 const struct usb_endpoint_descriptor
*desc
)
268 struct langwell_udc
*dev
;
269 struct langwell_ep
*ep
;
273 unsigned char zlt
, ios
= 0, mult
= 0;
275 ep
= container_of(_ep
, struct langwell_ep
, ep
);
277 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
279 if (!_ep
|| !desc
|| ep
->desc
280 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
283 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
286 max
= le16_to_cpu(desc
->wMaxPacketSize
);
289 * disable HW zero length termination select
290 * driver handles zero length packet through req->req.zero
295 * sanity check type, direction, address, and then
296 * initialize the endpoint capabilities fields in dQH
298 switch (usb_endpoint_type(desc
)) {
299 case USB_ENDPOINT_XFER_CONTROL
:
302 case USB_ENDPOINT_XFER_BULK
:
303 if ((dev
->gadget
.speed
== USB_SPEED_HIGH
305 || (dev
->gadget
.speed
== USB_SPEED_FULL
310 case USB_ENDPOINT_XFER_INT
:
311 if (strstr(ep
->ep
.name
, "-iso")) /* bulk is ok */
314 switch (dev
->gadget
.speed
) {
327 case USB_ENDPOINT_XFER_ISOC
:
328 if (strstr(ep
->ep
.name
, "-bulk")
329 || strstr(ep
->ep
.name
, "-int"))
332 switch (dev
->gadget
.speed
) {
344 * calculate transactions needed for high bandwidth iso
346 mult
= (unsigned char)(1 + ((max
>> 11) & 0x03));
347 max
= max
& 0x8ff; /* bit 0~10 */
348 /* 3 transactions at most */
356 spin_lock_irqsave(&dev
->lock
, flags
);
358 ep
->ep
.maxpacket
= max
;
361 ep
->ep_num
= usb_endpoint_num(desc
);
364 ep
->ep_type
= usb_endpoint_type(desc
);
366 /* configure endpoint control registers */
367 ep_reset(ep
, ep
->ep_num
, is_in(ep
), ep
->ep_type
);
369 /* configure endpoint capabilities in dQH */
370 i
= ep
->ep_num
* 2 + is_in(ep
);
371 ep
->dqh
= &dev
->ep_dqh
[i
];
372 ep
->dqh
->dqh_ios
= ios
;
373 ep
->dqh
->dqh_mpl
= cpu_to_le16(max
);
374 ep
->dqh
->dqh_zlt
= zlt
;
375 ep
->dqh
->dqh_mult
= mult
;
376 ep
->dqh
->dtd_next
= DTD_TERM
;
378 dev_dbg(&dev
->pdev
->dev
, "enabled %s (ep%d%s-%s), max %04x\n",
385 spin_unlock_irqrestore(&dev
->lock
, flags
);
387 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
392 /*-------------------------------------------------------------------------*/
394 /* retire a request */
395 static void done(struct langwell_ep
*ep
, struct langwell_request
*req
,
398 struct langwell_udc
*dev
= ep
->dev
;
399 unsigned stopped
= ep
->stopped
;
400 struct langwell_dtd
*curr_dtd
, *next_dtd
;
403 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
405 /* remove the req from ep->queue */
406 list_del_init(&req
->queue
);
408 if (req
->req
.status
== -EINPROGRESS
)
409 req
->req
.status
= status
;
411 status
= req
->req
.status
;
413 /* free dTD for the request */
414 next_dtd
= req
->head
;
415 for (i
= 0; i
< req
->dtd_count
; i
++) {
417 if (i
!= req
->dtd_count
- 1)
418 next_dtd
= curr_dtd
->next_dtd_virt
;
419 dma_pool_free(dev
->dtd_pool
, curr_dtd
, curr_dtd
->dtd_dma
);
423 dma_unmap_single(&dev
->pdev
->dev
,
424 req
->req
.dma
, req
->req
.length
,
425 is_in(ep
) ? PCI_DMA_TODEVICE
: PCI_DMA_FROMDEVICE
);
426 req
->req
.dma
= DMA_ADDR_INVALID
;
429 dma_sync_single_for_cpu(&dev
->pdev
->dev
, req
->req
.dma
,
431 is_in(ep
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
433 if (status
!= -ESHUTDOWN
)
434 dev_dbg(&dev
->pdev
->dev
,
435 "complete %s, req %p, stat %d, len %u/%u\n",
436 ep
->ep
.name
, &req
->req
, status
,
437 req
->req
.actual
, req
->req
.length
);
439 /* don't modify queue heads during completion callback */
442 spin_unlock(&dev
->lock
);
443 /* complete routine from gadget driver */
444 if (req
->req
.complete
)
445 req
->req
.complete(&ep
->ep
, &req
->req
);
447 spin_lock(&dev
->lock
);
448 ep
->stopped
= stopped
;
450 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
454 static void langwell_ep_fifo_flush(struct usb_ep
*_ep
);
456 /* delete all endpoint requests, called with spinlock held */
457 static void nuke(struct langwell_ep
*ep
, int status
)
459 /* called with spinlock held */
462 /* endpoint fifo flush */
463 if (&ep
->ep
&& ep
->desc
)
464 langwell_ep_fifo_flush(&ep
->ep
);
466 while (!list_empty(&ep
->queue
)) {
467 struct langwell_request
*req
= NULL
;
468 req
= list_entry(ep
->queue
.next
, struct langwell_request
,
470 done(ep
, req
, status
);
475 /*-------------------------------------------------------------------------*/
477 /* endpoint is no longer usable */
478 static int langwell_ep_disable(struct usb_ep
*_ep
)
480 struct langwell_ep
*ep
;
482 struct langwell_udc
*dev
;
486 ep
= container_of(_ep
, struct langwell_ep
, ep
);
488 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
490 if (!_ep
|| !ep
->desc
)
493 spin_lock_irqsave(&dev
->lock
, flags
);
495 /* disable endpoint control register */
497 endptctrl
= readl(&dev
->op_regs
->endptctrl
[ep_num
]);
499 endptctrl
&= ~EPCTRL_TXE
;
501 endptctrl
&= ~EPCTRL_RXE
;
502 writel(endptctrl
, &dev
->op_regs
->endptctrl
[ep_num
]);
504 /* nuke all pending requests (does flush) */
505 nuke(ep
, -ESHUTDOWN
);
510 spin_unlock_irqrestore(&dev
->lock
, flags
);
512 dev_dbg(&dev
->pdev
->dev
, "disabled %s\n", _ep
->name
);
513 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
519 /* allocate a request object to use with this endpoint */
520 static struct usb_request
*langwell_alloc_request(struct usb_ep
*_ep
,
523 struct langwell_ep
*ep
;
524 struct langwell_udc
*dev
;
525 struct langwell_request
*req
= NULL
;
530 ep
= container_of(_ep
, struct langwell_ep
, ep
);
532 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
534 req
= kzalloc(sizeof(*req
), gfp_flags
);
538 req
->req
.dma
= DMA_ADDR_INVALID
;
539 INIT_LIST_HEAD(&req
->queue
);
541 dev_vdbg(&dev
->pdev
->dev
, "alloc request for %s\n", _ep
->name
);
542 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
547 /* free a request object */
548 static void langwell_free_request(struct usb_ep
*_ep
,
549 struct usb_request
*_req
)
551 struct langwell_ep
*ep
;
552 struct langwell_udc
*dev
;
553 struct langwell_request
*req
= NULL
;
555 ep
= container_of(_ep
, struct langwell_ep
, ep
);
557 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
562 req
= container_of(_req
, struct langwell_request
, req
);
563 WARN_ON(!list_empty(&req
->queue
));
568 dev_vdbg(&dev
->pdev
->dev
, "free request for %s\n", _ep
->name
);
569 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
573 /*-------------------------------------------------------------------------*/
575 /* queue dTD and PRIME endpoint */
576 static int queue_dtd(struct langwell_ep
*ep
, struct langwell_request
*req
)
578 u32 bit_mask
, usbcmd
, endptstat
, dtd_dma
;
581 struct langwell_dqh
*dqh
;
582 struct langwell_udc
*dev
;
585 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
587 i
= ep
->ep_num
* 2 + is_in(ep
);
588 dqh
= &dev
->ep_dqh
[i
];
591 dev_vdbg(&dev
->pdev
->dev
, "%s\n", ep
->name
);
594 dev_vdbg(&dev
->pdev
->dev
, "%s-%s\n", ep
->name
, DIR_STRING(ep
));
596 dev_vdbg(&dev
->pdev
->dev
, "ep_dqh[%d] addr: 0x%08x\n",
597 i
, (u32
)&(dev
->ep_dqh
[i
]));
599 bit_mask
= is_in(ep
) ?
600 (1 << (ep
->ep_num
+ 16)) : (1 << (ep
->ep_num
));
602 dev_vdbg(&dev
->pdev
->dev
, "bit_mask = 0x%08x\n", bit_mask
);
604 /* check if the pipe is empty */
605 if (!(list_empty(&ep
->queue
))) {
606 /* add dTD to the end of linked list */
607 struct langwell_request
*lastreq
;
608 lastreq
= list_entry(ep
->queue
.prev
,
609 struct langwell_request
, queue
);
611 lastreq
->tail
->dtd_next
=
612 cpu_to_le32(req
->head
->dtd_dma
& DTD_NEXT_MASK
);
614 /* read prime bit, if 1 goto out */
615 if (readl(&dev
->op_regs
->endptprime
) & bit_mask
)
619 /* set ATDTW bit in USBCMD */
620 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
621 writel(usbcmd
| CMD_ATDTW
, &dev
->op_regs
->usbcmd
);
623 /* read correct status bit */
624 endptstat
= readl(&dev
->op_regs
->endptstat
) & bit_mask
;
626 } while (!(readl(&dev
->op_regs
->usbcmd
) & CMD_ATDTW
));
628 /* write ATDTW bit to 0 */
629 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
630 writel(usbcmd
& ~CMD_ATDTW
, &dev
->op_regs
->usbcmd
);
636 /* write dQH next pointer and terminate bit to 0 */
637 dtd_dma
= req
->head
->dtd_dma
& DTD_NEXT_MASK
;
638 dqh
->dtd_next
= cpu_to_le32(dtd_dma
);
640 /* clear active and halt bit */
641 dtd_status
= (u8
) ~(DTD_STS_ACTIVE
| DTD_STS_HALTED
);
642 dqh
->dtd_status
&= dtd_status
;
643 dev_vdbg(&dev
->pdev
->dev
, "dqh->dtd_status = 0x%x\n", dqh
->dtd_status
);
645 /* ensure that updates to the dQH will occur before priming */
648 /* write 1 to endptprime register to PRIME endpoint */
649 bit_mask
= is_in(ep
) ? (1 << (ep
->ep_num
+ 16)) : (1 << ep
->ep_num
);
650 dev_vdbg(&dev
->pdev
->dev
, "endprime bit_mask = 0x%08x\n", bit_mask
);
651 writel(bit_mask
, &dev
->op_regs
->endptprime
);
653 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
658 /* fill in the dTD structure to build a transfer descriptor */
659 static struct langwell_dtd
*build_dtd(struct langwell_request
*req
,
660 unsigned *length
, dma_addr_t
*dma
, int *is_last
)
663 struct langwell_dtd
*dtd
;
664 struct langwell_udc
*dev
;
668 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
670 /* the maximum transfer length, up to 16k bytes */
671 *length
= min(req
->req
.length
- req
->req
.actual
,
672 (unsigned)DTD_MAX_TRANSFER_LENGTH
);
674 /* create dTD dma_pool resource */
675 dtd
= dma_pool_alloc(dev
->dtd_pool
, GFP_KERNEL
, dma
);
680 /* initialize buffer page pointers */
681 buf_ptr
= (u32
)(req
->req
.dma
+ req
->req
.actual
);
682 for (i
= 0; i
< 5; i
++)
683 dtd
->dtd_buf
[i
] = cpu_to_le32(buf_ptr
+ i
* PAGE_SIZE
);
685 req
->req
.actual
+= *length
;
687 /* fill in total bytes with transfer size */
688 dtd
->dtd_total
= cpu_to_le16(*length
);
689 dev_vdbg(&dev
->pdev
->dev
, "dtd->dtd_total = %d\n", dtd
->dtd_total
);
691 /* set is_last flag if req->req.zero is set or not */
693 if (*length
== 0 || (*length
% req
->ep
->ep
.maxpacket
) != 0)
697 } else if (req
->req
.length
== req
->req
.actual
) {
703 dev_vdbg(&dev
->pdev
->dev
, "multi-dtd request!\n");
705 /* set interrupt on complete bit for the last dTD */
706 if (*is_last
&& !req
->req
.no_interrupt
)
709 /* set multiplier override 0 for non-ISO and non-TX endpoint */
712 /* set the active bit of status field to 1 */
713 dtd
->dtd_status
= DTD_STS_ACTIVE
;
714 dev_vdbg(&dev
->pdev
->dev
, "dtd->dtd_status = 0x%02x\n",
717 dev_vdbg(&dev
->pdev
->dev
, "length = %d, dma addr= 0x%08x\n",
719 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
724 /* generate dTD linked list for a request */
725 static int req_to_dtd(struct langwell_request
*req
)
728 int is_last
, is_first
= 1;
729 struct langwell_dtd
*dtd
, *last_dtd
= NULL
;
730 struct langwell_udc
*dev
;
734 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
736 dtd
= build_dtd(req
, &count
, &dma
, &is_last
);
744 last_dtd
->dtd_next
= cpu_to_le32(dma
);
745 last_dtd
->next_dtd_virt
= dtd
;
751 /* set terminate bit to 1 for the last dTD */
752 dtd
->dtd_next
= DTD_TERM
;
756 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
760 /*-------------------------------------------------------------------------*/
762 /* queue (submits) an I/O requests to an endpoint */
763 static int langwell_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
766 struct langwell_request
*req
;
767 struct langwell_ep
*ep
;
768 struct langwell_udc
*dev
;
770 int is_iso
= 0, zlflag
= 0;
772 /* always require a cpu-view buffer */
773 req
= container_of(_req
, struct langwell_request
, req
);
774 ep
= container_of(_ep
, struct langwell_ep
, ep
);
776 if (!_req
|| !_req
->complete
|| !_req
->buf
777 || !list_empty(&req
->queue
)) {
781 if (unlikely(!_ep
|| !ep
->desc
))
786 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
788 if (usb_endpoint_xfer_isoc(ep
->desc
)) {
789 if (req
->req
.length
> ep
->ep
.maxpacket
)
794 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
))
797 /* set up dma mapping in case the caller didn't */
798 if (_req
->dma
== DMA_ADDR_INVALID
) {
799 /* WORKAROUND: WARN_ON(size == 0) */
800 if (_req
->length
== 0) {
801 dev_vdbg(&dev
->pdev
->dev
, "req->length: 0->1\n");
806 _req
->dma
= dma_map_single(&dev
->pdev
->dev
,
807 _req
->buf
, _req
->length
,
808 is_in(ep
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
809 if (zlflag
&& (_req
->length
== 1)) {
810 dev_vdbg(&dev
->pdev
->dev
, "req->length: 1->0\n");
816 dev_vdbg(&dev
->pdev
->dev
, "req->mapped = 1\n");
818 dma_sync_single_for_device(&dev
->pdev
->dev
,
819 _req
->dma
, _req
->length
,
820 is_in(ep
) ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
822 dev_vdbg(&dev
->pdev
->dev
, "req->mapped = 0\n");
825 dev_dbg(&dev
->pdev
->dev
,
826 "%s queue req %p, len %u, buf %p, dma 0x%08x\n",
828 _req
, _req
->length
, _req
->buf
, (int)_req
->dma
);
830 _req
->status
= -EINPROGRESS
;
834 spin_lock_irqsave(&dev
->lock
, flags
);
836 /* build and put dTDs to endpoint queue */
837 if (!req_to_dtd(req
)) {
840 spin_unlock_irqrestore(&dev
->lock
, flags
);
844 /* update ep0 state */
846 dev
->ep0_state
= DATA_STATE_XMIT
;
848 if (likely(req
!= NULL
)) {
849 list_add_tail(&req
->queue
, &ep
->queue
);
850 dev_vdbg(&dev
->pdev
->dev
, "list_add_tail()\n");
853 spin_unlock_irqrestore(&dev
->lock
, flags
);
855 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
860 /* dequeue (cancels, unlinks) an I/O request from an endpoint */
861 static int langwell_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
863 struct langwell_ep
*ep
;
864 struct langwell_udc
*dev
;
865 struct langwell_request
*req
;
867 int stopped
, ep_num
, retval
= 0;
870 ep
= container_of(_ep
, struct langwell_ep
, ep
);
872 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
874 if (!_ep
|| !ep
->desc
|| !_req
)
880 spin_lock_irqsave(&dev
->lock
, flags
);
881 stopped
= ep
->stopped
;
883 /* quiesce dma while we patch the queue */
887 /* disable endpoint control register */
888 endptctrl
= readl(&dev
->op_regs
->endptctrl
[ep_num
]);
890 endptctrl
&= ~EPCTRL_TXE
;
892 endptctrl
&= ~EPCTRL_RXE
;
893 writel(endptctrl
, &dev
->op_regs
->endptctrl
[ep_num
]);
895 /* make sure it's still queued on this endpoint */
896 list_for_each_entry(req
, &ep
->queue
, queue
) {
897 if (&req
->req
== _req
)
901 if (&req
->req
!= _req
) {
906 /* queue head may be partially complete. */
907 if (ep
->queue
.next
== &req
->queue
) {
908 dev_dbg(&dev
->pdev
->dev
, "unlink (%s) dma\n", _ep
->name
);
909 _req
->status
= -ECONNRESET
;
910 langwell_ep_fifo_flush(&ep
->ep
);
912 /* not the last request in endpoint queue */
913 if (likely(ep
->queue
.next
== &req
->queue
)) {
914 struct langwell_dqh
*dqh
;
915 struct langwell_request
*next_req
;
918 next_req
= list_entry(req
->queue
.next
,
919 struct langwell_request
, queue
);
921 /* point the dQH to the first dTD of next request */
922 writel((u32
) next_req
->head
, &dqh
->dqh_current
);
925 struct langwell_request
*prev_req
;
927 prev_req
= list_entry(req
->queue
.prev
,
928 struct langwell_request
, queue
);
929 writel(readl(&req
->tail
->dtd_next
),
930 &prev_req
->tail
->dtd_next
);
933 done(ep
, req
, -ECONNRESET
);
936 /* enable endpoint again */
937 endptctrl
= readl(&dev
->op_regs
->endptctrl
[ep_num
]);
939 endptctrl
|= EPCTRL_TXE
;
941 endptctrl
|= EPCTRL_RXE
;
942 writel(endptctrl
, &dev
->op_regs
->endptctrl
[ep_num
]);
944 ep
->stopped
= stopped
;
945 spin_unlock_irqrestore(&dev
->lock
, flags
);
947 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
952 /*-------------------------------------------------------------------------*/
954 /* endpoint set/clear halt */
955 static void ep_set_halt(struct langwell_ep
*ep
, int value
)
959 struct langwell_udc
*dev
= ep
->dev
;
960 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
963 endptctrl
= readl(&dev
->op_regs
->endptctrl
[ep_num
]);
965 /* value: 1 - set halt, 0 - clear halt */
967 /* set the stall bit */
969 endptctrl
|= EPCTRL_TXS
;
971 endptctrl
|= EPCTRL_RXS
;
973 /* clear the stall bit and reset data toggle */
975 endptctrl
&= ~EPCTRL_TXS
;
976 endptctrl
|= EPCTRL_TXR
;
978 endptctrl
&= ~EPCTRL_RXS
;
979 endptctrl
|= EPCTRL_RXR
;
983 writel(endptctrl
, &dev
->op_regs
->endptctrl
[ep_num
]);
985 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
989 /* set the endpoint halt feature */
990 static int langwell_ep_set_halt(struct usb_ep
*_ep
, int value
)
992 struct langwell_ep
*ep
;
993 struct langwell_udc
*dev
;
997 ep
= container_of(_ep
, struct langwell_ep
, ep
);
1000 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1002 if (!_ep
|| !ep
->desc
)
1005 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1008 if (usb_endpoint_xfer_isoc(ep
->desc
))
1011 spin_lock_irqsave(&dev
->lock
, flags
);
1014 * attempt to halt IN ep will fail if any transfer requests
1017 if (!list_empty(&ep
->queue
) && is_in(ep
) && value
) {
1018 /* IN endpoint FIFO holds bytes */
1019 dev_dbg(&dev
->pdev
->dev
, "%s FIFO holds bytes\n", _ep
->name
);
1024 /* endpoint set/clear halt */
1026 ep_set_halt(ep
, value
);
1027 } else { /* endpoint 0 */
1028 dev
->ep0_state
= WAIT_FOR_SETUP
;
1029 dev
->ep0_dir
= USB_DIR_OUT
;
1032 spin_unlock_irqrestore(&dev
->lock
, flags
);
1033 dev_dbg(&dev
->pdev
->dev
, "%s %s halt\n",
1034 _ep
->name
, value
? "set" : "clear");
1035 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1040 /* set the halt feature and ignores clear requests */
1041 static int langwell_ep_set_wedge(struct usb_ep
*_ep
)
1043 struct langwell_ep
*ep
;
1044 struct langwell_udc
*dev
;
1046 ep
= container_of(_ep
, struct langwell_ep
, ep
);
1049 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1051 if (!_ep
|| !ep
->desc
)
1054 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1055 return usb_ep_set_halt(_ep
);
1059 /* flush contents of a fifo */
1060 static void langwell_ep_fifo_flush(struct usb_ep
*_ep
)
1062 struct langwell_ep
*ep
;
1063 struct langwell_udc
*dev
;
1065 unsigned long timeout
;
1067 ep
= container_of(_ep
, struct langwell_ep
, ep
);
1070 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1072 if (!_ep
|| !ep
->desc
) {
1073 dev_vdbg(&dev
->pdev
->dev
, "ep or ep->desc is NULL\n");
1074 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1078 dev_vdbg(&dev
->pdev
->dev
, "%s-%s fifo flush\n",
1079 _ep
->name
, DIR_STRING(ep
));
1081 /* flush endpoint buffer */
1082 if (ep
->ep_num
== 0)
1083 flush_bit
= (1 << 16) | 1;
1085 flush_bit
= 1 << (ep
->ep_num
+ 16); /* TX */
1087 flush_bit
= 1 << ep
->ep_num
; /* RX */
1089 /* wait until flush complete */
1090 timeout
= jiffies
+ FLUSH_TIMEOUT
;
1092 writel(flush_bit
, &dev
->op_regs
->endptflush
);
1093 while (readl(&dev
->op_regs
->endptflush
)) {
1094 if (time_after(jiffies
, timeout
)) {
1095 dev_err(&dev
->pdev
->dev
, "ep flush timeout\n");
1100 } while (readl(&dev
->op_regs
->endptstat
) & flush_bit
);
1102 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1106 /* endpoints operations structure */
1107 static const struct usb_ep_ops langwell_ep_ops
= {
1109 /* configure endpoint, making it usable */
1110 .enable
= langwell_ep_enable
,
1112 /* endpoint is no longer usable */
1113 .disable
= langwell_ep_disable
,
1115 /* allocate a request object to use with this endpoint */
1116 .alloc_request
= langwell_alloc_request
,
1118 /* free a request object */
1119 .free_request
= langwell_free_request
,
1121 /* queue (submits) an I/O requests to an endpoint */
1122 .queue
= langwell_ep_queue
,
1124 /* dequeue (cancels, unlinks) an I/O request from an endpoint */
1125 .dequeue
= langwell_ep_dequeue
,
1127 /* set the endpoint halt feature */
1128 .set_halt
= langwell_ep_set_halt
,
1130 /* set the halt feature and ignores clear requests */
1131 .set_wedge
= langwell_ep_set_wedge
,
1133 /* flush contents of a fifo */
1134 .fifo_flush
= langwell_ep_fifo_flush
,
1138 /*-------------------------------------------------------------------------*/
1140 /* device controller usb_gadget_ops structure */
1142 /* returns the current frame number */
1143 static int langwell_get_frame(struct usb_gadget
*_gadget
)
1145 struct langwell_udc
*dev
;
1151 dev
= container_of(_gadget
, struct langwell_udc
, gadget
);
1152 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1154 retval
= readl(&dev
->op_regs
->frindex
) & FRINDEX_MASK
;
1156 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1161 /* enter or exit PHY low power state */
1162 static void langwell_phy_low_power(struct langwell_udc
*dev
, bool flag
)
1166 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1168 devlc
= readl(&dev
->op_regs
->devlc
);
1169 dev_vdbg(&dev
->pdev
->dev
, "devlc = 0x%08x\n", devlc
);
1176 /* FIXME: workaround for Langwell A1/A2/A3 sighting */
1177 devlc_byte2
= (devlc
>> 16) & 0xff;
1178 writeb(devlc_byte2
, (u8
*)&dev
->op_regs
->devlc
+ 2);
1180 devlc
= readl(&dev
->op_regs
->devlc
);
1181 dev_vdbg(&dev
->pdev
->dev
,
1182 "%s PHY low power suspend, devlc = 0x%08x\n",
1183 flag
? "enter" : "exit", devlc
);
1187 /* tries to wake up the host connected to this gadget */
1188 static int langwell_wakeup(struct usb_gadget
*_gadget
)
1190 struct langwell_udc
*dev
;
1192 unsigned long flags
;
1197 dev
= container_of(_gadget
, struct langwell_udc
, gadget
);
1198 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1200 /* remote wakeup feature not enabled by host */
1201 if (!dev
->remote_wakeup
) {
1202 dev_info(&dev
->pdev
->dev
, "remote wakeup is disabled\n");
1206 spin_lock_irqsave(&dev
->lock
, flags
);
1208 portsc1
= readl(&dev
->op_regs
->portsc1
);
1209 if (!(portsc1
& PORTS_SUSP
)) {
1210 spin_unlock_irqrestore(&dev
->lock
, flags
);
1214 /* LPM L1 to L0 or legacy remote wakeup */
1215 if (dev
->lpm
&& dev
->lpm_state
== LPM_L1
)
1216 dev_info(&dev
->pdev
->dev
, "LPM L1 to L0 remote wakeup\n");
1218 dev_info(&dev
->pdev
->dev
, "device remote wakeup\n");
1220 /* exit PHY low power suspend */
1221 if (dev
->pdev
->device
!= 0x0829)
1222 langwell_phy_low_power(dev
, 0);
1224 /* force port resume */
1225 portsc1
|= PORTS_FPR
;
1226 writel(portsc1
, &dev
->op_regs
->portsc1
);
1228 spin_unlock_irqrestore(&dev
->lock
, flags
);
1230 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1235 /* notify controller that VBUS is powered or not */
1236 static int langwell_vbus_session(struct usb_gadget
*_gadget
, int is_active
)
1238 struct langwell_udc
*dev
;
1239 unsigned long flags
;
1245 dev
= container_of(_gadget
, struct langwell_udc
, gadget
);
1246 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1248 spin_lock_irqsave(&dev
->lock
, flags
);
1249 dev_vdbg(&dev
->pdev
->dev
, "VBUS status: %s\n",
1250 is_active
? "on" : "off");
1252 dev
->vbus_active
= (is_active
!= 0);
1253 if (dev
->driver
&& dev
->softconnected
&& dev
->vbus_active
) {
1254 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
1255 usbcmd
|= CMD_RUNSTOP
;
1256 writel(usbcmd
, &dev
->op_regs
->usbcmd
);
1258 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
1259 usbcmd
&= ~CMD_RUNSTOP
;
1260 writel(usbcmd
, &dev
->op_regs
->usbcmd
);
1263 spin_unlock_irqrestore(&dev
->lock
, flags
);
1265 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1270 /* constrain controller's VBUS power usage */
1271 static int langwell_vbus_draw(struct usb_gadget
*_gadget
, unsigned mA
)
1273 struct langwell_udc
*dev
;
1278 dev
= container_of(_gadget
, struct langwell_udc
, gadget
);
1279 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1281 if (dev
->transceiver
) {
1282 dev_vdbg(&dev
->pdev
->dev
, "otg_set_power\n");
1283 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1284 return otg_set_power(dev
->transceiver
, mA
);
1287 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1292 /* D+ pullup, software-controlled connect/disconnect to USB host */
1293 static int langwell_pullup(struct usb_gadget
*_gadget
, int is_on
)
1295 struct langwell_udc
*dev
;
1297 unsigned long flags
;
1302 dev
= container_of(_gadget
, struct langwell_udc
, gadget
);
1304 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1306 spin_lock_irqsave(&dev
->lock
, flags
);
1307 dev
->softconnected
= (is_on
!= 0);
1309 if (dev
->driver
&& dev
->softconnected
&& dev
->vbus_active
) {
1310 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
1311 usbcmd
|= CMD_RUNSTOP
;
1312 writel(usbcmd
, &dev
->op_regs
->usbcmd
);
1314 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
1315 usbcmd
&= ~CMD_RUNSTOP
;
1316 writel(usbcmd
, &dev
->op_regs
->usbcmd
);
1318 spin_unlock_irqrestore(&dev
->lock
, flags
);
1320 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1325 /* device controller usb_gadget_ops structure */
1326 static const struct usb_gadget_ops langwell_ops
= {
1328 /* returns the current frame number */
1329 .get_frame
= langwell_get_frame
,
1331 /* tries to wake up the host connected to this gadget */
1332 .wakeup
= langwell_wakeup
,
1334 /* set the device selfpowered feature, always selfpowered */
1335 /* .set_selfpowered = langwell_set_selfpowered, */
1337 /* notify controller that VBUS is powered or not */
1338 .vbus_session
= langwell_vbus_session
,
1340 /* constrain controller's VBUS power usage */
1341 .vbus_draw
= langwell_vbus_draw
,
1343 /* D+ pullup, software-controlled connect/disconnect to USB host */
1344 .pullup
= langwell_pullup
,
1348 /*-------------------------------------------------------------------------*/
1350 /* device controller operations */
1352 /* reset device controller */
1353 static int langwell_udc_reset(struct langwell_udc
*dev
)
1355 u32 usbcmd
, usbmode
, devlc
, endpointlistaddr
;
1356 u8 devlc_byte0
, devlc_byte2
;
1357 unsigned long timeout
;
1362 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1364 /* set controller to stop state */
1365 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
1366 usbcmd
&= ~CMD_RUNSTOP
;
1367 writel(usbcmd
, &dev
->op_regs
->usbcmd
);
1369 /* reset device controller */
1370 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
1372 writel(usbcmd
, &dev
->op_regs
->usbcmd
);
1374 /* wait for reset to complete */
1375 timeout
= jiffies
+ RESET_TIMEOUT
;
1376 while (readl(&dev
->op_regs
->usbcmd
) & CMD_RST
) {
1377 if (time_after(jiffies
, timeout
)) {
1378 dev_err(&dev
->pdev
->dev
, "device reset timeout\n");
1384 /* set controller to device mode */
1385 usbmode
= readl(&dev
->op_regs
->usbmode
);
1386 usbmode
|= MODE_DEVICE
;
1388 /* turn setup lockout off, require setup tripwire in usbcmd */
1389 usbmode
|= MODE_SLOM
;
1391 writel(usbmode
, &dev
->op_regs
->usbmode
);
1392 usbmode
= readl(&dev
->op_regs
->usbmode
);
1393 dev_vdbg(&dev
->pdev
->dev
, "usbmode=0x%08x\n", usbmode
);
1395 /* Write-Clear setup status */
1396 writel(0, &dev
->op_regs
->usbsts
);
1398 /* if support USB LPM, ACK all LPM token */
1400 devlc
= readl(&dev
->op_regs
->devlc
);
1401 dev_vdbg(&dev
->pdev
->dev
, "devlc = 0x%08x\n", devlc
);
1402 /* FIXME: workaround for Langwell A1/A2/A3 sighting */
1403 devlc
&= ~LPM_STL
; /* don't STALL LPM token */
1404 devlc
&= ~LPM_NYT_ACK
; /* ACK LPM token */
1405 devlc_byte0
= devlc
& 0xff;
1406 devlc_byte2
= (devlc
>> 16) & 0xff;
1407 writeb(devlc_byte0
, (u8
*)&dev
->op_regs
->devlc
);
1408 writeb(devlc_byte2
, (u8
*)&dev
->op_regs
->devlc
+ 2);
1409 devlc
= readl(&dev
->op_regs
->devlc
);
1410 dev_vdbg(&dev
->pdev
->dev
,
1411 "ACK LPM token, devlc = 0x%08x\n", devlc
);
1414 /* fill endpointlistaddr register */
1415 endpointlistaddr
= dev
->ep_dqh_dma
;
1416 endpointlistaddr
&= ENDPOINTLISTADDR_MASK
;
1417 writel(endpointlistaddr
, &dev
->op_regs
->endpointlistaddr
);
1419 dev_vdbg(&dev
->pdev
->dev
,
1420 "dQH base (vir: %p, phy: 0x%08x), endpointlistaddr=0x%08x\n",
1421 dev
->ep_dqh
, endpointlistaddr
,
1422 readl(&dev
->op_regs
->endpointlistaddr
));
1423 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1428 /* reinitialize device controller endpoints */
1429 static int eps_reinit(struct langwell_udc
*dev
)
1431 struct langwell_ep
*ep
;
1435 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1437 /* initialize ep0 */
1440 strncpy(ep
->name
, "ep0", sizeof(ep
->name
));
1441 ep
->ep
.name
= ep
->name
;
1442 ep
->ep
.ops
= &langwell_ep_ops
;
1444 ep
->ep
.maxpacket
= EP0_MAX_PKT_SIZE
;
1446 ep
->desc
= &langwell_ep0_desc
;
1447 INIT_LIST_HEAD(&ep
->queue
);
1449 ep
->ep_type
= USB_ENDPOINT_XFER_CONTROL
;
1451 /* initialize other endpoints */
1452 for (i
= 2; i
< dev
->ep_max
; i
++) {
1455 snprintf(name
, sizeof(name
), "ep%din", i
/ 2);
1457 snprintf(name
, sizeof(name
), "ep%dout", i
/ 2);
1459 strncpy(ep
->name
, name
, sizeof(ep
->name
));
1460 ep
->ep
.name
= ep
->name
;
1462 ep
->ep
.ops
= &langwell_ep_ops
;
1464 ep
->ep
.maxpacket
= (unsigned short) ~0;
1467 INIT_LIST_HEAD(&ep
->queue
);
1468 list_add_tail(&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1471 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1476 /* enable interrupt and set controller to run state */
1477 static void langwell_udc_start(struct langwell_udc
*dev
)
1479 u32 usbintr
, usbcmd
;
1480 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1482 /* enable interrupts */
1483 usbintr
= INTR_ULPIE
/* ULPI */
1484 | INTR_SLE
/* suspend */
1485 /* | INTR_SRE SOF received */
1486 | INTR_URE
/* USB reset */
1487 | INTR_AAE
/* async advance */
1488 | INTR_SEE
/* system error */
1489 | INTR_FRE
/* frame list rollover */
1490 | INTR_PCE
/* port change detect */
1491 | INTR_UEE
/* USB error interrupt */
1492 | INTR_UE
; /* USB interrupt */
1493 writel(usbintr
, &dev
->op_regs
->usbintr
);
1495 /* clear stopped bit */
1498 /* set controller to run */
1499 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
1500 usbcmd
|= CMD_RUNSTOP
;
1501 writel(usbcmd
, &dev
->op_regs
->usbcmd
);
1503 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1507 /* disable interrupt and set controller to stop state */
1508 static void langwell_udc_stop(struct langwell_udc
*dev
)
1512 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1514 /* disable all interrupts */
1515 writel(0, &dev
->op_regs
->usbintr
);
1517 /* set stopped bit */
1520 /* set controller to stop state */
1521 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
1522 usbcmd
&= ~CMD_RUNSTOP
;
1523 writel(usbcmd
, &dev
->op_regs
->usbcmd
);
1525 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1529 /* stop all USB activities */
1530 static void stop_activity(struct langwell_udc
*dev
,
1531 struct usb_gadget_driver
*driver
)
1533 struct langwell_ep
*ep
;
1534 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1536 nuke(&dev
->ep
[0], -ESHUTDOWN
);
1538 list_for_each_entry(ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
1539 nuke(ep
, -ESHUTDOWN
);
1542 /* report disconnect; the driver is already quiesced */
1544 spin_unlock(&dev
->lock
);
1545 driver
->disconnect(&dev
->gadget
);
1546 spin_lock(&dev
->lock
);
1549 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1553 /*-------------------------------------------------------------------------*/
1555 /* device "function" sysfs attribute file */
1556 static ssize_t
show_function(struct device
*_dev
,
1557 struct device_attribute
*attr
, char *buf
)
1559 struct langwell_udc
*dev
= the_controller
;
1561 if (!dev
->driver
|| !dev
->driver
->function
1562 || strlen(dev
->driver
->function
) > PAGE_SIZE
)
1565 return scnprintf(buf
, PAGE_SIZE
, "%s\n", dev
->driver
->function
);
1567 static DEVICE_ATTR(function
, S_IRUGO
, show_function
, NULL
);
1570 /* device "langwell_udc" sysfs attribute file */
1571 static ssize_t
show_langwell_udc(struct device
*_dev
,
1572 struct device_attribute
*attr
, char *buf
)
1574 struct langwell_udc
*dev
= the_controller
;
1575 struct langwell_request
*req
;
1576 struct langwell_ep
*ep
= NULL
;
1581 unsigned long flags
;
1586 spin_lock_irqsave(&dev
->lock
, flags
);
1588 /* driver basic information */
1589 t
= scnprintf(next
, size
,
1592 "Gadget driver: %s\n\n",
1593 driver_name
, DRIVER_VERSION
,
1594 dev
->driver
? dev
->driver
->driver
.name
: "(none)");
1598 /* device registers */
1599 tmp_reg
= readl(&dev
->op_regs
->usbcmd
);
1600 t
= scnprintf(next
, size
,
1604 (tmp_reg
& CMD_SUTW
) ? 1 : 0,
1605 (tmp_reg
& CMD_RUNSTOP
) ? "Run" : "Stop");
1609 tmp_reg
= readl(&dev
->op_regs
->usbsts
);
1610 t
= scnprintf(next
, size
,
1612 "Device Suspend: %d\n"
1613 "Reset Received: %d\n"
1614 "System Error: %s\n"
1615 "USB Error Interrupt: %s\n\n",
1616 (tmp_reg
& STS_SLI
) ? 1 : 0,
1617 (tmp_reg
& STS_URI
) ? 1 : 0,
1618 (tmp_reg
& STS_SEI
) ? "Error" : "No error",
1619 (tmp_reg
& STS_UEI
) ? "Error detected" : "No error");
1623 tmp_reg
= readl(&dev
->op_regs
->usbintr
);
1624 t
= scnprintf(next
, size
,
1625 "USB Intrrupt Enable Reg:\n"
1626 "Sleep Enable: %d\n"
1627 "SOF Received Enable: %d\n"
1628 "Reset Enable: %d\n"
1629 "System Error Enable: %d\n"
1630 "Port Change Dectected Enable: %d\n"
1631 "USB Error Intr Enable: %d\n"
1632 "USB Intr Enable: %d\n\n",
1633 (tmp_reg
& INTR_SLE
) ? 1 : 0,
1634 (tmp_reg
& INTR_SRE
) ? 1 : 0,
1635 (tmp_reg
& INTR_URE
) ? 1 : 0,
1636 (tmp_reg
& INTR_SEE
) ? 1 : 0,
1637 (tmp_reg
& INTR_PCE
) ? 1 : 0,
1638 (tmp_reg
& INTR_UEE
) ? 1 : 0,
1639 (tmp_reg
& INTR_UE
) ? 1 : 0);
1643 tmp_reg
= readl(&dev
->op_regs
->frindex
);
1644 t
= scnprintf(next
, size
,
1645 "USB Frame Index Reg:\n"
1646 "Frame Number is 0x%08x\n\n",
1647 (tmp_reg
& FRINDEX_MASK
));
1651 tmp_reg
= readl(&dev
->op_regs
->deviceaddr
);
1652 t
= scnprintf(next
, size
,
1653 "USB Device Address Reg:\n"
1654 "Device Addr is 0x%x\n\n",
1659 tmp_reg
= readl(&dev
->op_regs
->endpointlistaddr
);
1660 t
= scnprintf(next
, size
,
1661 "USB Endpoint List Address Reg:\n"
1662 "Endpoint List Pointer is 0x%x\n\n",
1667 tmp_reg
= readl(&dev
->op_regs
->portsc1
);
1668 t
= scnprintf(next
, size
,
1669 "USB Port Status & Control Reg:\n"
1671 "Port Suspend Mode: %s\n"
1672 "Over-current Change: %s\n"
1673 "Port Enable/Disable Change: %s\n"
1674 "Port Enabled/Disabled: %s\n"
1675 "Current Connect Status: %s\n"
1676 "LPM Suspend Status: %s\n\n",
1677 (tmp_reg
& PORTS_PR
) ? "Reset" : "Not Reset",
1678 (tmp_reg
& PORTS_SUSP
) ? "Suspend " : "Not Suspend",
1679 (tmp_reg
& PORTS_OCC
) ? "Detected" : "No",
1680 (tmp_reg
& PORTS_PEC
) ? "Changed" : "Not Changed",
1681 (tmp_reg
& PORTS_PE
) ? "Enable" : "Not Correct",
1682 (tmp_reg
& PORTS_CCS
) ? "Attached" : "Not Attached",
1683 (tmp_reg
& PORTS_SLP
) ? "LPM L1" : "LPM L0");
1687 tmp_reg
= readl(&dev
->op_regs
->devlc
);
1688 t
= scnprintf(next
, size
,
1689 "Device LPM Control Reg:\n"
1690 "Parallel Transceiver : %d\n"
1691 "Serial Transceiver : %d\n"
1693 "Port Force Full Speed Connenct: %s\n"
1694 "PHY Low Power Suspend Clock: %s\n"
1695 "BmAttributes: %d\n\n",
1697 (tmp_reg
& LPM_STS
) ? 1 : 0,
1700 switch (LPM_PSPD(tmp_reg
)) {
1701 case LPM_SPEED_FULL
:
1702 s
= "Full Speed"; break;
1704 s
= "Low Speed"; break;
1705 case LPM_SPEED_HIGH
:
1706 s
= "High Speed"; break;
1708 s
= "Unknown Speed"; break;
1712 (tmp_reg
& LPM_PFSC
) ? "Force Full Speed" : "Not Force",
1713 (tmp_reg
& LPM_PHCD
) ? "Disabled" : "Enabled",
1718 tmp_reg
= readl(&dev
->op_regs
->usbmode
);
1719 t
= scnprintf(next
, size
,
1721 "Controller Mode is : %s\n\n", ({
1723 switch (MODE_CM(tmp_reg
)) {
1727 s
= "Device Controller"; break;
1729 s
= "Host Controller"; break;
1738 tmp_reg
= readl(&dev
->op_regs
->endptsetupstat
);
1739 t
= scnprintf(next
, size
,
1740 "Endpoint Setup Status Reg:\n"
1741 "SETUP on ep 0x%04x\n\n",
1742 tmp_reg
& SETUPSTAT_MASK
);
1746 for (i
= 0; i
< dev
->ep_max
/ 2; i
++) {
1747 tmp_reg
= readl(&dev
->op_regs
->endptctrl
[i
]);
1748 t
= scnprintf(next
, size
, "EP Ctrl Reg [%d]: 0x%08x\n",
1753 tmp_reg
= readl(&dev
->op_regs
->endptprime
);
1754 t
= scnprintf(next
, size
, "EP Prime Reg: 0x%08x\n\n", tmp_reg
);
1758 /* langwell_udc, langwell_ep, langwell_request structure information */
1760 t
= scnprintf(next
, size
, "%s MaxPacketSize: 0x%x, ep_num: %d\n",
1761 ep
->ep
.name
, ep
->ep
.maxpacket
, ep
->ep_num
);
1765 if (list_empty(&ep
->queue
)) {
1766 t
= scnprintf(next
, size
, "its req queue is empty\n\n");
1770 list_for_each_entry(req
, &ep
->queue
, queue
) {
1771 t
= scnprintf(next
, size
,
1772 "req %p actual 0x%x length 0x%x buf %p\n",
1773 &req
->req
, req
->req
.actual
,
1774 req
->req
.length
, req
->req
.buf
);
1779 /* other gadget->eplist ep */
1780 list_for_each_entry(ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
1782 t
= scnprintf(next
, size
,
1783 "\n%s MaxPacketSize: 0x%x, "
1785 ep
->ep
.name
, ep
->ep
.maxpacket
,
1790 if (list_empty(&ep
->queue
)) {
1791 t
= scnprintf(next
, size
,
1792 "its req queue is empty\n\n");
1796 list_for_each_entry(req
, &ep
->queue
, queue
) {
1797 t
= scnprintf(next
, size
,
1798 "req %p actual 0x%x length "
1800 &req
->req
, req
->req
.actual
,
1801 req
->req
.length
, req
->req
.buf
);
1809 spin_unlock_irqrestore(&dev
->lock
, flags
);
1810 return PAGE_SIZE
- size
;
1812 static DEVICE_ATTR(langwell_udc
, S_IRUGO
, show_langwell_udc
, NULL
);
1815 /* device "remote_wakeup" sysfs attribute file */
1816 static ssize_t
store_remote_wakeup(struct device
*_dev
,
1817 struct device_attribute
*attr
, const char *buf
, size_t count
)
1819 struct langwell_udc
*dev
= the_controller
;
1820 unsigned long flags
;
1826 if (count
> 0 && buf
[count
-1] == '\n')
1827 ((char *) buf
)[count
-1] = 0;
1832 /* force remote wakeup enabled in case gadget driver doesn't support */
1833 spin_lock_irqsave(&dev
->lock
, flags
);
1834 dev
->remote_wakeup
= 1;
1835 dev
->dev_status
|= (1 << USB_DEVICE_REMOTE_WAKEUP
);
1836 spin_unlock_irqrestore(&dev
->lock
, flags
);
1838 langwell_wakeup(&dev
->gadget
);
1842 static DEVICE_ATTR(remote_wakeup
, S_IWUSR
, NULL
, store_remote_wakeup
);
1845 /*-------------------------------------------------------------------------*/
1848 * when a driver is successfully registered, it will receive
1849 * control requests including set_configuration(), which enables
1850 * non-control requests. then usb traffic follows until a
1851 * disconnect is reported. then a host may connect again, or
1852 * the driver might get unbound.
1855 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
,
1856 int (*bind
)(struct usb_gadget
*))
1858 struct langwell_udc
*dev
= the_controller
;
1859 unsigned long flags
;
1865 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1870 spin_lock_irqsave(&dev
->lock
, flags
);
1872 /* hook up the driver ... */
1873 driver
->driver
.bus
= NULL
;
1874 dev
->driver
= driver
;
1875 dev
->gadget
.dev
.driver
= &driver
->driver
;
1877 spin_unlock_irqrestore(&dev
->lock
, flags
);
1879 retval
= bind(&dev
->gadget
);
1881 dev_dbg(&dev
->pdev
->dev
, "bind to driver %s --> %d\n",
1882 driver
->driver
.name
, retval
);
1884 dev
->gadget
.dev
.driver
= NULL
;
1888 retval
= device_create_file(&dev
->pdev
->dev
, &dev_attr_function
);
1892 dev
->usb_state
= USB_STATE_ATTACHED
;
1893 dev
->ep0_state
= WAIT_FOR_SETUP
;
1894 dev
->ep0_dir
= USB_DIR_OUT
;
1896 /* enable interrupt and set controller to run state */
1898 langwell_udc_start(dev
);
1900 dev_vdbg(&dev
->pdev
->dev
,
1901 "After langwell_udc_start(), print all registers:\n");
1902 print_all_registers(dev
);
1904 dev_info(&dev
->pdev
->dev
, "register driver: %s\n",
1905 driver
->driver
.name
);
1906 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1910 driver
->unbind(&dev
->gadget
);
1911 dev
->gadget
.dev
.driver
= NULL
;
1914 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1917 EXPORT_SYMBOL(usb_gadget_probe_driver
);
1920 /* unregister gadget driver */
1921 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1923 struct langwell_udc
*dev
= the_controller
;
1924 unsigned long flags
;
1929 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1931 if (unlikely(!driver
|| !driver
->unbind
))
1934 /* exit PHY low power suspend */
1935 if (dev
->pdev
->device
!= 0x0829)
1936 langwell_phy_low_power(dev
, 0);
1938 /* unbind OTG transceiver */
1939 if (dev
->transceiver
)
1940 (void)otg_set_peripheral(dev
->transceiver
, 0);
1942 /* disable interrupt and set controller to stop state */
1943 langwell_udc_stop(dev
);
1945 dev
->usb_state
= USB_STATE_ATTACHED
;
1946 dev
->ep0_state
= WAIT_FOR_SETUP
;
1947 dev
->ep0_dir
= USB_DIR_OUT
;
1949 spin_lock_irqsave(&dev
->lock
, flags
);
1951 /* stop all usb activities */
1952 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1953 stop_activity(dev
, driver
);
1954 spin_unlock_irqrestore(&dev
->lock
, flags
);
1956 /* unbind gadget driver */
1957 driver
->unbind(&dev
->gadget
);
1958 dev
->gadget
.dev
.driver
= NULL
;
1961 device_remove_file(&dev
->pdev
->dev
, &dev_attr_function
);
1963 dev_info(&dev
->pdev
->dev
, "unregistered driver '%s'\n",
1964 driver
->driver
.name
);
1965 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
1968 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1971 /*-------------------------------------------------------------------------*/
1974 * setup tripwire is used as a semaphore to ensure that the setup data
1975 * payload is extracted from a dQH without being corrupted
1977 static void setup_tripwire(struct langwell_udc
*dev
)
1981 unsigned long timeout
;
1982 struct langwell_dqh
*dqh
;
1984 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
1987 dqh
= &dev
->ep_dqh
[EP_DIR_OUT
];
1989 /* Write-Clear endptsetupstat */
1990 endptsetupstat
= readl(&dev
->op_regs
->endptsetupstat
);
1991 writel(endptsetupstat
, &dev
->op_regs
->endptsetupstat
);
1993 /* wait until endptsetupstat is cleared */
1994 timeout
= jiffies
+ SETUPSTAT_TIMEOUT
;
1995 while (readl(&dev
->op_regs
->endptsetupstat
)) {
1996 if (time_after(jiffies
, timeout
)) {
1997 dev_err(&dev
->pdev
->dev
, "setup_tripwire timeout\n");
2003 /* while a hazard exists when setup packet arrives */
2005 /* set setup tripwire bit */
2006 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
2007 writel(usbcmd
| CMD_SUTW
, &dev
->op_regs
->usbcmd
);
2009 /* copy the setup packet to local buffer */
2010 memcpy(&dev
->local_setup_buff
, &dqh
->dqh_setup
, 8);
2011 } while (!(readl(&dev
->op_regs
->usbcmd
) & CMD_SUTW
));
2013 /* Write-Clear setup tripwire bit */
2014 usbcmd
= readl(&dev
->op_regs
->usbcmd
);
2015 writel(usbcmd
& ~CMD_SUTW
, &dev
->op_regs
->usbcmd
);
2017 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2021 /* protocol ep0 stall, will automatically be cleared on new transaction */
2022 static void ep0_stall(struct langwell_udc
*dev
)
2026 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2028 /* set TX and RX to stall */
2029 endptctrl
= readl(&dev
->op_regs
->endptctrl
[0]);
2030 endptctrl
|= EPCTRL_TXS
| EPCTRL_RXS
;
2031 writel(endptctrl
, &dev
->op_regs
->endptctrl
[0]);
2033 /* update ep0 state */
2034 dev
->ep0_state
= WAIT_FOR_SETUP
;
2035 dev
->ep0_dir
= USB_DIR_OUT
;
2037 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2041 /* PRIME a status phase for ep0 */
2042 static int prime_status_phase(struct langwell_udc
*dev
, int dir
)
2044 struct langwell_request
*req
;
2045 struct langwell_ep
*ep
;
2048 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2050 if (dir
== EP_DIR_IN
)
2051 dev
->ep0_dir
= USB_DIR_IN
;
2053 dev
->ep0_dir
= USB_DIR_OUT
;
2056 dev
->ep0_state
= WAIT_FOR_OUT_STATUS
;
2058 req
= dev
->status_req
;
2061 req
->req
.length
= 0;
2062 req
->req
.status
= -EINPROGRESS
;
2063 req
->req
.actual
= 0;
2064 req
->req
.complete
= NULL
;
2067 if (!req_to_dtd(req
))
2068 status
= queue_dtd(ep
, req
);
2073 dev_err(&dev
->pdev
->dev
, "can't queue ep0 status request\n");
2075 list_add_tail(&req
->queue
, &ep
->queue
);
2077 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2082 /* SET_ADDRESS request routine */
2083 static void set_address(struct langwell_udc
*dev
, u16 value
,
2084 u16 index
, u16 length
)
2086 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2088 /* save the new address to device struct */
2089 dev
->dev_addr
= (u8
) value
;
2090 dev_vdbg(&dev
->pdev
->dev
, "dev->dev_addr = %d\n", dev
->dev_addr
);
2092 /* update usb state */
2093 dev
->usb_state
= USB_STATE_ADDRESS
;
2096 if (prime_status_phase(dev
, EP_DIR_IN
))
2099 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2103 /* return endpoint by windex */
2104 static struct langwell_ep
*get_ep_by_windex(struct langwell_udc
*dev
,
2107 struct langwell_ep
*ep
;
2108 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2110 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) == 0)
2113 list_for_each_entry(ep
, &dev
->gadget
.ep_list
, ep
.ep_list
) {
2114 u8 bEndpointAddress
;
2118 bEndpointAddress
= ep
->desc
->bEndpointAddress
;
2119 if ((wIndex
^ bEndpointAddress
) & USB_DIR_IN
)
2122 if ((wIndex
& USB_ENDPOINT_NUMBER_MASK
)
2123 == (bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
))
2127 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2132 /* return whether endpoint is stalled, 0: not stalled; 1: stalled */
2133 static int ep_is_stall(struct langwell_ep
*ep
)
2135 struct langwell_udc
*dev
= ep
->dev
;
2139 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2141 endptctrl
= readl(&dev
->op_regs
->endptctrl
[ep
->ep_num
]);
2143 retval
= endptctrl
& EPCTRL_TXS
? 1 : 0;
2145 retval
= endptctrl
& EPCTRL_RXS
? 1 : 0;
2147 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2152 /* GET_STATUS request routine */
2153 static void get_status(struct langwell_udc
*dev
, u8 request_type
, u16 value
,
2154 u16 index
, u16 length
)
2156 struct langwell_request
*req
;
2157 struct langwell_ep
*ep
;
2158 u16 status_data
= 0; /* 16 bits cpu view status data */
2161 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2165 if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
2166 /* get device status */
2167 status_data
= dev
->dev_status
;
2168 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_INTERFACE
) {
2169 /* get interface status */
2171 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_ENDPOINT
) {
2172 /* get endpoint status */
2173 struct langwell_ep
*epn
;
2174 epn
= get_ep_by_windex(dev
, index
);
2175 /* stall if endpoint doesn't exist */
2179 status_data
= ep_is_stall(epn
) << USB_ENDPOINT_HALT
;
2182 dev_dbg(&dev
->pdev
->dev
, "get status data: 0x%04x\n", status_data
);
2184 dev
->ep0_dir
= USB_DIR_IN
;
2186 /* borrow the per device status_req */
2187 req
= dev
->status_req
;
2189 /* fill in the reqest structure */
2190 *((u16
*) req
->req
.buf
) = cpu_to_le16(status_data
);
2192 req
->req
.length
= 2;
2193 req
->req
.status
= -EINPROGRESS
;
2194 req
->req
.actual
= 0;
2195 req
->req
.complete
= NULL
;
2198 /* prime the data phase */
2199 if (!req_to_dtd(req
))
2200 status
= queue_dtd(ep
, req
);
2205 dev_err(&dev
->pdev
->dev
,
2206 "response error on GET_STATUS request\n");
2210 list_add_tail(&req
->queue
, &ep
->queue
);
2211 dev
->ep0_state
= DATA_STATE_XMIT
;
2213 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2217 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2221 /* setup packet interrupt handler */
2222 static void handle_setup_packet(struct langwell_udc
*dev
,
2223 struct usb_ctrlrequest
*setup
)
2225 u16 wValue
= le16_to_cpu(setup
->wValue
);
2226 u16 wIndex
= le16_to_cpu(setup
->wIndex
);
2227 u16 wLength
= le16_to_cpu(setup
->wLength
);
2230 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2232 /* ep0 fifo flush */
2233 nuke(&dev
->ep
[0], -ESHUTDOWN
);
2235 dev_dbg(&dev
->pdev
->dev
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
2236 setup
->bRequestType
, setup
->bRequest
,
2237 wValue
, wIndex
, wLength
);
2239 /* RNDIS gadget delegate */
2240 if ((setup
->bRequestType
== 0x21) && (setup
->bRequest
== 0x00)) {
2241 /* USB_CDC_SEND_ENCAPSULATED_COMMAND */
2245 /* USB_CDC_GET_ENCAPSULATED_RESPONSE */
2246 if ((setup
->bRequestType
== 0xa1) && (setup
->bRequest
== 0x01)) {
2247 /* USB_CDC_GET_ENCAPSULATED_RESPONSE */
2251 /* We process some stardard setup requests here */
2252 switch (setup
->bRequest
) {
2253 case USB_REQ_GET_STATUS
:
2254 dev_dbg(&dev
->pdev
->dev
, "SETUP: USB_REQ_GET_STATUS\n");
2255 /* get status, DATA and STATUS phase */
2256 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
2257 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
2259 get_status(dev
, setup
->bRequestType
, wValue
, wIndex
, wLength
);
2262 case USB_REQ_SET_ADDRESS
:
2263 dev_dbg(&dev
->pdev
->dev
, "SETUP: USB_REQ_SET_ADDRESS\n");
2265 if (setup
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
2266 | USB_RECIP_DEVICE
))
2268 set_address(dev
, wValue
, wIndex
, wLength
);
2271 case USB_REQ_CLEAR_FEATURE
:
2272 case USB_REQ_SET_FEATURE
:
2275 int rc
= -EOPNOTSUPP
;
2276 if (setup
->bRequest
== USB_REQ_SET_FEATURE
)
2277 dev_dbg(&dev
->pdev
->dev
,
2278 "SETUP: USB_REQ_SET_FEATURE\n");
2279 else if (setup
->bRequest
== USB_REQ_CLEAR_FEATURE
)
2280 dev_dbg(&dev
->pdev
->dev
,
2281 "SETUP: USB_REQ_CLEAR_FEATURE\n");
2283 if ((setup
->bRequestType
& (USB_RECIP_MASK
| USB_TYPE_MASK
))
2284 == (USB_RECIP_ENDPOINT
| USB_TYPE_STANDARD
)) {
2285 struct langwell_ep
*epn
;
2286 epn
= get_ep_by_windex(dev
, wIndex
);
2287 /* stall if endpoint doesn't exist */
2293 if (wValue
!= 0 || wLength
!= 0
2294 || epn
->ep_num
> dev
->ep_max
)
2297 spin_unlock(&dev
->lock
);
2298 rc
= langwell_ep_set_halt(&epn
->ep
,
2299 (setup
->bRequest
== USB_REQ_SET_FEATURE
)
2301 spin_lock(&dev
->lock
);
2303 } else if ((setup
->bRequestType
& (USB_RECIP_MASK
2304 | USB_TYPE_MASK
)) == (USB_RECIP_DEVICE
2305 | USB_TYPE_STANDARD
)) {
2308 case USB_DEVICE_REMOTE_WAKEUP
:
2309 if (setup
->bRequest
== USB_REQ_SET_FEATURE
) {
2310 dev
->remote_wakeup
= 1;
2311 dev
->dev_status
|= (1 << wValue
);
2313 dev
->remote_wakeup
= 0;
2314 dev
->dev_status
&= ~(1 << wValue
);
2317 case USB_DEVICE_TEST_MODE
:
2318 dev_dbg(&dev
->pdev
->dev
, "SETUP: TEST MODE\n");
2319 if ((wIndex
& 0xff) ||
2320 (dev
->gadget
.speed
!= USB_SPEED_HIGH
))
2323 switch (wIndex
>> 8) {
2329 if (prime_status_phase(dev
, EP_DIR_IN
))
2331 portsc1
= readl(&dev
->op_regs
->portsc1
);
2332 portsc1
|= (wIndex
& 0xf00) << 8;
2333 writel(portsc1
, &dev
->op_regs
->portsc1
);
2344 if (!gadget_is_otg(&dev
->gadget
))
2346 else if (setup
->bRequest
== USB_DEVICE_B_HNP_ENABLE
) {
2347 dev
->gadget
.b_hnp_enable
= 1;
2348 #ifdef OTG_TRANSCEIVER
2349 if (!dev
->lotg
->otg
.default_a
)
2350 dev
->lotg
->hsm
.b_hnp_enable
= 1;
2352 } else if (setup
->bRequest
== USB_DEVICE_A_HNP_SUPPORT
)
2353 dev
->gadget
.a_hnp_support
= 1;
2354 else if (setup
->bRequest
==
2355 USB_DEVICE_A_ALT_HNP_SUPPORT
)
2356 dev
->gadget
.a_alt_hnp_support
= 1;
2363 if (prime_status_phase(dev
, EP_DIR_IN
))
2369 case USB_REQ_GET_DESCRIPTOR
:
2370 dev_dbg(&dev
->pdev
->dev
,
2371 "SETUP: USB_REQ_GET_DESCRIPTOR\n");
2374 case USB_REQ_SET_DESCRIPTOR
:
2375 dev_dbg(&dev
->pdev
->dev
,
2376 "SETUP: USB_REQ_SET_DESCRIPTOR unsupported\n");
2379 case USB_REQ_GET_CONFIGURATION
:
2380 dev_dbg(&dev
->pdev
->dev
,
2381 "SETUP: USB_REQ_GET_CONFIGURATION\n");
2384 case USB_REQ_SET_CONFIGURATION
:
2385 dev_dbg(&dev
->pdev
->dev
,
2386 "SETUP: USB_REQ_SET_CONFIGURATION\n");
2389 case USB_REQ_GET_INTERFACE
:
2390 dev_dbg(&dev
->pdev
->dev
,
2391 "SETUP: USB_REQ_GET_INTERFACE\n");
2394 case USB_REQ_SET_INTERFACE
:
2395 dev_dbg(&dev
->pdev
->dev
,
2396 "SETUP: USB_REQ_SET_INTERFACE\n");
2399 case USB_REQ_SYNCH_FRAME
:
2400 dev_dbg(&dev
->pdev
->dev
,
2401 "SETUP: USB_REQ_SYNCH_FRAME unsupported\n");
2405 /* delegate USB standard requests to the gadget driver */
2408 /* USB requests handled by gadget */
2410 /* DATA phase from gadget, STATUS phase from udc */
2411 dev
->ep0_dir
= (setup
->bRequestType
& USB_DIR_IN
)
2412 ? USB_DIR_IN
: USB_DIR_OUT
;
2413 dev_vdbg(&dev
->pdev
->dev
,
2414 "dev->ep0_dir = 0x%x, wLength = %d\n",
2415 dev
->ep0_dir
, wLength
);
2416 spin_unlock(&dev
->lock
);
2417 if (dev
->driver
->setup(&dev
->gadget
,
2418 &dev
->local_setup_buff
) < 0)
2420 spin_lock(&dev
->lock
);
2421 dev
->ep0_state
= (setup
->bRequestType
& USB_DIR_IN
)
2422 ? DATA_STATE_XMIT
: DATA_STATE_RECV
;
2424 /* no DATA phase, IN STATUS phase from gadget */
2425 dev
->ep0_dir
= USB_DIR_IN
;
2426 dev_vdbg(&dev
->pdev
->dev
,
2427 "dev->ep0_dir = 0x%x, wLength = %d\n",
2428 dev
->ep0_dir
, wLength
);
2429 spin_unlock(&dev
->lock
);
2430 if (dev
->driver
->setup(&dev
->gadget
,
2431 &dev
->local_setup_buff
) < 0)
2433 spin_lock(&dev
->lock
);
2434 dev
->ep0_state
= WAIT_FOR_OUT_STATUS
;
2439 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2443 /* transfer completion, process endpoint request and free the completed dTDs
2446 static int process_ep_req(struct langwell_udc
*dev
, int index
,
2447 struct langwell_request
*curr_req
)
2449 struct langwell_dtd
*curr_dtd
;
2450 struct langwell_dqh
*curr_dqh
;
2451 int td_complete
, actual
, remaining_length
;
2456 curr_dqh
= &dev
->ep_dqh
[index
];
2459 curr_dtd
= curr_req
->head
;
2461 actual
= curr_req
->req
.length
;
2463 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2465 for (i
= 0; i
< curr_req
->dtd_count
; i
++) {
2467 /* command execution states by dTD */
2468 dtd_status
= curr_dtd
->dtd_status
;
2471 remaining_length
= le16_to_cpu(curr_dtd
->dtd_total
);
2472 actual
-= remaining_length
;
2475 /* transfers completed successfully */
2476 if (!remaining_length
) {
2478 dev_vdbg(&dev
->pdev
->dev
,
2479 "dTD transmitted successfully\n");
2482 dev_vdbg(&dev
->pdev
->dev
,
2483 "TX dTD remains data\n");
2493 /* transfers completed with errors */
2494 if (dtd_status
& DTD_STS_ACTIVE
) {
2495 dev_dbg(&dev
->pdev
->dev
,
2496 "dTD status ACTIVE dQH[%d]\n", index
);
2499 } else if (dtd_status
& DTD_STS_HALTED
) {
2500 dev_err(&dev
->pdev
->dev
,
2501 "dTD error %08x dQH[%d]\n",
2503 /* clear the errors and halt condition */
2504 curr_dqh
->dtd_status
= 0;
2507 } else if (dtd_status
& DTD_STS_DBE
) {
2508 dev_dbg(&dev
->pdev
->dev
,
2509 "data buffer (overflow) error\n");
2512 } else if (dtd_status
& DTD_STS_TRE
) {
2513 dev_dbg(&dev
->pdev
->dev
,
2514 "transaction(ISO) error\n");
2518 dev_err(&dev
->pdev
->dev
,
2519 "unknown error (0x%x)!\n",
2523 if (i
!= curr_req
->dtd_count
- 1)
2524 curr_dtd
= (struct langwell_dtd
*)
2525 curr_dtd
->next_dtd_virt
;
2531 curr_req
->req
.actual
= actual
;
2533 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2538 /* complete DATA or STATUS phase of ep0 prime status phase if needed */
2539 static void ep0_req_complete(struct langwell_udc
*dev
,
2540 struct langwell_ep
*ep0
, struct langwell_request
*req
)
2543 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2545 if (dev
->usb_state
== USB_STATE_ADDRESS
) {
2546 /* set the new address */
2547 new_addr
= (u32
)dev
->dev_addr
;
2548 writel(new_addr
<< USBADR_SHIFT
, &dev
->op_regs
->deviceaddr
);
2550 new_addr
= USBADR(readl(&dev
->op_regs
->deviceaddr
));
2551 dev_vdbg(&dev
->pdev
->dev
, "new_addr = %d\n", new_addr
);
2556 switch (dev
->ep0_state
) {
2557 case DATA_STATE_XMIT
:
2558 /* receive status phase */
2559 if (prime_status_phase(dev
, EP_DIR_OUT
))
2562 case DATA_STATE_RECV
:
2563 /* send status phase */
2564 if (prime_status_phase(dev
, EP_DIR_IN
))
2567 case WAIT_FOR_OUT_STATUS
:
2568 dev
->ep0_state
= WAIT_FOR_SETUP
;
2570 case WAIT_FOR_SETUP
:
2571 dev_err(&dev
->pdev
->dev
, "unexpect ep0 packets\n");
2578 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2582 /* USB transfer completion interrupt */
2583 static void handle_trans_complete(struct langwell_udc
*dev
)
2586 int i
, ep_num
, dir
, bit_mask
, status
;
2587 struct langwell_ep
*epn
;
2588 struct langwell_request
*curr_req
, *temp_req
;
2590 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2592 complete_bits
= readl(&dev
->op_regs
->endptcomplete
);
2593 dev_vdbg(&dev
->pdev
->dev
, "endptcomplete register: 0x%08x\n",
2596 /* Write-Clear the bits in endptcomplete register */
2597 writel(complete_bits
, &dev
->op_regs
->endptcomplete
);
2599 if (!complete_bits
) {
2600 dev_dbg(&dev
->pdev
->dev
, "complete_bits = 0\n");
2604 for (i
= 0; i
< dev
->ep_max
; i
++) {
2608 bit_mask
= 1 << (ep_num
+ 16 * dir
);
2610 if (!(complete_bits
& bit_mask
))
2619 if (epn
->name
== NULL
) {
2620 dev_warn(&dev
->pdev
->dev
, "invalid endpoint\n");
2625 /* ep0 in and out */
2626 dev_dbg(&dev
->pdev
->dev
, "%s-%s transfer completed\n",
2628 is_in(epn
) ? "in" : "out");
2630 dev_dbg(&dev
->pdev
->dev
, "%s transfer completed\n",
2633 /* process the req queue until an uncomplete request */
2634 list_for_each_entry_safe(curr_req
, temp_req
,
2635 &epn
->queue
, queue
) {
2636 status
= process_ep_req(dev
, i
, curr_req
);
2637 dev_vdbg(&dev
->pdev
->dev
, "%s req status: %d\n",
2643 /* write back status to req */
2644 curr_req
->req
.status
= status
;
2646 /* ep0 request completion */
2648 ep0_req_complete(dev
, epn
, curr_req
);
2651 done(epn
, curr_req
, status
);
2656 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2660 /* port change detect interrupt handler */
2661 static void handle_port_change(struct langwell_udc
*dev
)
2666 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2671 portsc1
= readl(&dev
->op_regs
->portsc1
);
2672 devlc
= readl(&dev
->op_regs
->devlc
);
2673 dev_vdbg(&dev
->pdev
->dev
, "portsc1 = 0x%08x, devlc = 0x%08x\n",
2676 /* bus reset is finished */
2677 if (!(portsc1
& PORTS_PR
)) {
2679 speed
= LPM_PSPD(devlc
);
2681 case LPM_SPEED_HIGH
:
2682 dev
->gadget
.speed
= USB_SPEED_HIGH
;
2684 case LPM_SPEED_FULL
:
2685 dev
->gadget
.speed
= USB_SPEED_FULL
;
2688 dev
->gadget
.speed
= USB_SPEED_LOW
;
2691 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2694 dev_vdbg(&dev
->pdev
->dev
,
2695 "speed = %d, dev->gadget.speed = %d\n",
2696 speed
, dev
->gadget
.speed
);
2700 if (dev
->lpm
&& dev
->lpm_state
== LPM_L0
)
2701 if (portsc1
& PORTS_SUSP
&& portsc1
& PORTS_SLP
) {
2702 dev_info(&dev
->pdev
->dev
, "LPM L0 to L1\n");
2703 dev
->lpm_state
= LPM_L1
;
2706 /* LPM L1 to L0, force resume or remote wakeup finished */
2707 if (dev
->lpm
&& dev
->lpm_state
== LPM_L1
)
2708 if (!(portsc1
& PORTS_SUSP
)) {
2709 dev_info(&dev
->pdev
->dev
, "LPM L1 to L0\n");
2710 dev
->lpm_state
= LPM_L0
;
2713 /* update USB state */
2714 if (!dev
->resume_state
)
2715 dev
->usb_state
= USB_STATE_DEFAULT
;
2717 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2721 /* USB reset interrupt handler */
2722 static void handle_usb_reset(struct langwell_udc
*dev
)
2727 unsigned long timeout
;
2729 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2731 /* Write-Clear the device address */
2732 deviceaddr
= readl(&dev
->op_regs
->deviceaddr
);
2733 writel(deviceaddr
& ~USBADR_MASK
, &dev
->op_regs
->deviceaddr
);
2737 /* clear usb state */
2738 dev
->resume_state
= 0;
2740 /* LPM L1 to L0, reset */
2742 dev
->lpm_state
= LPM_L0
;
2744 dev
->ep0_dir
= USB_DIR_OUT
;
2745 dev
->ep0_state
= WAIT_FOR_SETUP
;
2747 /* remote wakeup reset to 0 when the device is reset */
2748 dev
->remote_wakeup
= 0;
2749 dev
->dev_status
= 1 << USB_DEVICE_SELF_POWERED
;
2750 dev
->gadget
.b_hnp_enable
= 0;
2751 dev
->gadget
.a_hnp_support
= 0;
2752 dev
->gadget
.a_alt_hnp_support
= 0;
2754 /* Write-Clear all the setup token semaphores */
2755 endptsetupstat
= readl(&dev
->op_regs
->endptsetupstat
);
2756 writel(endptsetupstat
, &dev
->op_regs
->endptsetupstat
);
2758 /* Write-Clear all the endpoint complete status bits */
2759 endptcomplete
= readl(&dev
->op_regs
->endptcomplete
);
2760 writel(endptcomplete
, &dev
->op_regs
->endptcomplete
);
2762 /* wait until all endptprime bits cleared */
2763 timeout
= jiffies
+ PRIME_TIMEOUT
;
2764 while (readl(&dev
->op_regs
->endptprime
)) {
2765 if (time_after(jiffies
, timeout
)) {
2766 dev_err(&dev
->pdev
->dev
, "USB reset timeout\n");
2772 /* write 1s to endptflush register to clear any primed buffers */
2773 writel((u32
) ~0, &dev
->op_regs
->endptflush
);
2775 if (readl(&dev
->op_regs
->portsc1
) & PORTS_PR
) {
2776 dev_vdbg(&dev
->pdev
->dev
, "USB bus reset\n");
2777 /* bus is reseting */
2780 /* reset all the queues, stop all USB activities */
2781 stop_activity(dev
, dev
->driver
);
2782 dev
->usb_state
= USB_STATE_DEFAULT
;
2784 dev_vdbg(&dev
->pdev
->dev
, "device controller reset\n");
2785 /* controller reset */
2786 langwell_udc_reset(dev
);
2788 /* reset all the queues, stop all USB activities */
2789 stop_activity(dev
, dev
->driver
);
2791 /* reset ep0 dQH and endptctrl */
2794 /* enable interrupt and set controller to run state */
2795 langwell_udc_start(dev
);
2797 dev
->usb_state
= USB_STATE_ATTACHED
;
2800 #ifdef OTG_TRANSCEIVER
2801 /* refer to USB OTG 6.6.2.3 b_hnp_en is cleared */
2802 if (!dev
->lotg
->otg
.default_a
)
2803 dev
->lotg
->hsm
.b_hnp_enable
= 0;
2806 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2810 /* USB bus suspend/resume interrupt */
2811 static void handle_bus_suspend(struct langwell_udc
*dev
)
2813 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2815 dev
->resume_state
= dev
->usb_state
;
2816 dev
->usb_state
= USB_STATE_SUSPENDED
;
2818 #ifdef OTG_TRANSCEIVER
2819 if (dev
->lotg
->otg
.default_a
) {
2820 if (dev
->lotg
->hsm
.b_bus_suspend_vld
== 1) {
2821 dev
->lotg
->hsm
.b_bus_suspend
= 1;
2822 /* notify transceiver the state changes */
2823 if (spin_trylock(&dev
->lotg
->wq_lock
)) {
2824 langwell_update_transceiver();
2825 spin_unlock(&dev
->lotg
->wq_lock
);
2828 dev
->lotg
->hsm
.b_bus_suspend_vld
++;
2830 if (!dev
->lotg
->hsm
.a_bus_suspend
) {
2831 dev
->lotg
->hsm
.a_bus_suspend
= 1;
2832 /* notify transceiver the state changes */
2833 if (spin_trylock(&dev
->lotg
->wq_lock
)) {
2834 langwell_update_transceiver();
2835 spin_unlock(&dev
->lotg
->wq_lock
);
2841 /* report suspend to the driver */
2843 if (dev
->driver
->suspend
) {
2844 spin_unlock(&dev
->lock
);
2845 dev
->driver
->suspend(&dev
->gadget
);
2846 spin_lock(&dev
->lock
);
2847 dev_dbg(&dev
->pdev
->dev
, "suspend %s\n",
2848 dev
->driver
->driver
.name
);
2852 /* enter PHY low power suspend */
2853 if (dev
->pdev
->device
!= 0x0829)
2854 langwell_phy_low_power(dev
, 0);
2856 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2860 static void handle_bus_resume(struct langwell_udc
*dev
)
2862 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2864 dev
->usb_state
= dev
->resume_state
;
2865 dev
->resume_state
= 0;
2867 /* exit PHY low power suspend */
2868 if (dev
->pdev
->device
!= 0x0829)
2869 langwell_phy_low_power(dev
, 0);
2871 #ifdef OTG_TRANSCEIVER
2872 if (dev
->lotg
->otg
.default_a
== 0)
2873 dev
->lotg
->hsm
.a_bus_suspend
= 0;
2876 /* report resume to the driver */
2878 if (dev
->driver
->resume
) {
2879 spin_unlock(&dev
->lock
);
2880 dev
->driver
->resume(&dev
->gadget
);
2881 spin_lock(&dev
->lock
);
2882 dev_dbg(&dev
->pdev
->dev
, "resume %s\n",
2883 dev
->driver
->driver
.name
);
2887 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2891 /* USB device controller interrupt handler */
2892 static irqreturn_t
langwell_irq(int irq
, void *_dev
)
2894 struct langwell_udc
*dev
= _dev
;
2900 dev_vdbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
2903 dev_vdbg(&dev
->pdev
->dev
, "handle IRQ_NONE\n");
2904 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2908 spin_lock(&dev
->lock
);
2911 usbsts
= readl(&dev
->op_regs
->usbsts
);
2913 /* USB interrupt enable */
2914 usbintr
= readl(&dev
->op_regs
->usbintr
);
2916 irq_sts
= usbsts
& usbintr
;
2917 dev_vdbg(&dev
->pdev
->dev
,
2918 "usbsts = 0x%08x, usbintr = 0x%08x, irq_sts = 0x%08x\n",
2919 usbsts
, usbintr
, irq_sts
);
2922 dev_vdbg(&dev
->pdev
->dev
, "handle IRQ_NONE\n");
2923 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2924 spin_unlock(&dev
->lock
);
2928 /* Write-Clear interrupt status bits */
2929 writel(irq_sts
, &dev
->op_regs
->usbsts
);
2931 /* resume from suspend */
2932 portsc1
= readl(&dev
->op_regs
->portsc1
);
2933 if (dev
->usb_state
== USB_STATE_SUSPENDED
)
2934 if (!(portsc1
& PORTS_SUSP
))
2935 handle_bus_resume(dev
);
2938 if (irq_sts
& STS_UI
) {
2939 dev_vdbg(&dev
->pdev
->dev
, "USB interrupt\n");
2941 /* setup packet received from ep0 */
2942 if (readl(&dev
->op_regs
->endptsetupstat
)
2943 & EP0SETUPSTAT_MASK
) {
2944 dev_vdbg(&dev
->pdev
->dev
,
2945 "USB SETUP packet received interrupt\n");
2946 /* setup tripwire semaphone */
2947 setup_tripwire(dev
);
2948 handle_setup_packet(dev
, &dev
->local_setup_buff
);
2951 /* USB transfer completion */
2952 if (readl(&dev
->op_regs
->endptcomplete
)) {
2953 dev_vdbg(&dev
->pdev
->dev
,
2954 "USB transfer completion interrupt\n");
2955 handle_trans_complete(dev
);
2959 /* SOF received interrupt (for ISO transfer) */
2960 if (irq_sts
& STS_SRI
) {
2962 /* dev_vdbg(&dev->pdev->dev, "SOF received interrupt\n"); */
2965 /* port change detect interrupt */
2966 if (irq_sts
& STS_PCI
) {
2967 dev_vdbg(&dev
->pdev
->dev
, "port change detect interrupt\n");
2968 handle_port_change(dev
);
2971 /* suspend interrrupt */
2972 if (irq_sts
& STS_SLI
) {
2973 dev_vdbg(&dev
->pdev
->dev
, "suspend interrupt\n");
2974 handle_bus_suspend(dev
);
2977 /* USB reset interrupt */
2978 if (irq_sts
& STS_URI
) {
2979 dev_vdbg(&dev
->pdev
->dev
, "USB reset interrupt\n");
2980 handle_usb_reset(dev
);
2983 /* USB error or system error interrupt */
2984 if (irq_sts
& (STS_UEI
| STS_SEI
)) {
2986 dev_warn(&dev
->pdev
->dev
, "error IRQ, irq_sts: %x\n", irq_sts
);
2989 spin_unlock(&dev
->lock
);
2991 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
2996 /*-------------------------------------------------------------------------*/
2998 /* release device structure */
2999 static void gadget_release(struct device
*_dev
)
3001 struct langwell_udc
*dev
= the_controller
;
3003 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
3005 complete(dev
->done
);
3007 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
3012 /* enable SRAM caching if SRAM detected */
3013 static void sram_init(struct langwell_udc
*dev
)
3015 struct pci_dev
*pdev
= dev
->pdev
;
3017 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
3019 dev
->sram_addr
= pci_resource_start(pdev
, 1);
3020 dev
->sram_size
= pci_resource_len(pdev
, 1);
3021 dev_info(&dev
->pdev
->dev
, "Found private SRAM at %x size:%x\n",
3022 dev
->sram_addr
, dev
->sram_size
);
3025 if (pci_request_region(pdev
, 1, kobject_name(&pdev
->dev
.kobj
))) {
3026 dev_warn(&dev
->pdev
->dev
, "SRAM request failed\n");
3028 } else if (!dma_declare_coherent_memory(&pdev
->dev
, dev
->sram_addr
,
3029 dev
->sram_addr
, dev
->sram_size
, DMA_MEMORY_MAP
)) {
3030 dev_warn(&dev
->pdev
->dev
, "SRAM DMA declare failed\n");
3031 pci_release_region(pdev
, 1);
3035 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
3039 /* release SRAM caching */
3040 static void sram_deinit(struct langwell_udc
*dev
)
3042 struct pci_dev
*pdev
= dev
->pdev
;
3044 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
3046 dma_release_declared_memory(&pdev
->dev
);
3047 pci_release_region(pdev
, 1);
3051 dev_info(&dev
->pdev
->dev
, "release SRAM caching\n");
3052 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
3056 /* tear down the binding between this driver and the pci device */
3057 static void langwell_udc_remove(struct pci_dev
*pdev
)
3059 struct langwell_udc
*dev
= the_controller
;
3061 DECLARE_COMPLETION(done
);
3063 BUG_ON(dev
->driver
);
3064 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
3068 #ifndef OTG_TRANSCEIVER
3069 /* free dTD dma_pool and dQH */
3071 dma_pool_destroy(dev
->dtd_pool
);
3074 dma_free_coherent(&pdev
->dev
, dev
->ep_dqh_size
,
3075 dev
->ep_dqh
, dev
->ep_dqh_dma
);
3077 /* release SRAM caching */
3078 if (dev
->has_sram
&& dev
->got_sram
)
3082 if (dev
->status_req
) {
3083 kfree(dev
->status_req
->req
.buf
);
3084 kfree(dev
->status_req
);
3089 /* disable IRQ handler */
3091 free_irq(pdev
->irq
, dev
);
3093 #ifndef OTG_TRANSCEIVER
3095 iounmap(dev
->cap_regs
);
3098 release_mem_region(pci_resource_start(pdev
, 0),
3099 pci_resource_len(pdev
, 0));
3102 pci_disable_device(pdev
);
3104 if (dev
->transceiver
) {
3105 otg_put_transceiver(dev
->transceiver
);
3106 dev
->transceiver
= NULL
;
3111 dev
->cap_regs
= NULL
;
3113 dev_info(&dev
->pdev
->dev
, "unbind\n");
3114 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
3116 device_unregister(&dev
->gadget
.dev
);
3117 device_remove_file(&pdev
->dev
, &dev_attr_langwell_udc
);
3118 device_remove_file(&pdev
->dev
, &dev_attr_remote_wakeup
);
3120 #ifndef OTG_TRANSCEIVER
3121 pci_set_drvdata(pdev
, NULL
);
3124 /* free dev, wait for the release() finished */
3125 wait_for_completion(&done
);
3127 the_controller
= NULL
;
3132 * wrap this driver around the specified device, but
3133 * don't respond over USB until a gadget driver binds to us.
3135 static int langwell_udc_probe(struct pci_dev
*pdev
,
3136 const struct pci_device_id
*id
)
3138 struct langwell_udc
*dev
;
3139 #ifndef OTG_TRANSCEIVER
3140 unsigned long resource
, len
;
3142 void __iomem
*base
= NULL
;
3146 if (the_controller
) {
3147 dev_warn(&pdev
->dev
, "ignoring\n");
3151 /* alloc, and start init */
3152 dev
= kzalloc(sizeof *dev
, GFP_KERNEL
);
3158 /* initialize device spinlock */
3159 spin_lock_init(&dev
->lock
);
3162 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
3164 #ifdef OTG_TRANSCEIVER
3165 /* PCI device is already enabled by otg_transceiver driver */
3168 /* mem region and register base */
3170 dev
->transceiver
= otg_get_transceiver();
3171 dev
->lotg
= otg_to_langwell(dev
->transceiver
);
3172 base
= dev
->lotg
->regs
;
3174 pci_set_drvdata(pdev
, dev
);
3176 /* now all the pci goodies ... */
3177 if (pci_enable_device(pdev
) < 0) {
3183 /* control register: BAR 0 */
3184 resource
= pci_resource_start(pdev
, 0);
3185 len
= pci_resource_len(pdev
, 0);
3186 if (!request_mem_region(resource
, len
, driver_name
)) {
3187 dev_err(&dev
->pdev
->dev
, "controller already in use\n");
3193 base
= ioremap_nocache(resource
, len
);
3196 dev_err(&dev
->pdev
->dev
, "can't map memory\n");
3201 dev
->cap_regs
= (struct langwell_cap_regs __iomem
*) base
;
3202 dev_vdbg(&dev
->pdev
->dev
, "dev->cap_regs: %p\n", dev
->cap_regs
);
3203 dev
->op_regs
= (struct langwell_op_regs __iomem
*)
3204 (base
+ OP_REG_OFFSET
);
3205 dev_vdbg(&dev
->pdev
->dev
, "dev->op_regs: %p\n", dev
->op_regs
);
3207 /* irq setup after old hardware is cleaned up */
3209 dev_err(&dev
->pdev
->dev
, "No IRQ. Check PCI setup!\n");
3216 dev_vdbg(&dev
->pdev
->dev
, "dev->has_sram: %d\n", dev
->has_sram
);
3218 #ifndef OTG_TRANSCEIVER
3219 /* enable SRAM caching if detected */
3220 if (dev
->has_sram
&& !dev
->got_sram
)
3223 dev_info(&dev
->pdev
->dev
,
3224 "irq %d, io mem: 0x%08lx, len: 0x%08lx, pci mem 0x%p\n",
3225 pdev
->irq
, resource
, len
, base
);
3226 /* enables bus-mastering for device dev */
3227 pci_set_master(pdev
);
3229 if (request_irq(pdev
->irq
, langwell_irq
, IRQF_SHARED
,
3230 driver_name
, dev
) != 0) {
3231 dev_err(&dev
->pdev
->dev
,
3232 "request interrupt %d failed\n", pdev
->irq
);
3239 /* set stopped bit */
3242 /* capabilities and endpoint number */
3243 dev
->lpm
= (readl(&dev
->cap_regs
->hccparams
) & HCC_LEN
) ? 1 : 0;
3244 dev
->dciversion
= readw(&dev
->cap_regs
->dciversion
);
3245 dev
->devcap
= (readl(&dev
->cap_regs
->dccparams
) & DEVCAP
) ? 1 : 0;
3246 dev_vdbg(&dev
->pdev
->dev
, "dev->lpm: %d\n", dev
->lpm
);
3247 dev_vdbg(&dev
->pdev
->dev
, "dev->dciversion: 0x%04x\n",
3249 dev_vdbg(&dev
->pdev
->dev
, "dccparams: 0x%08x\n",
3250 readl(&dev
->cap_regs
->dccparams
));
3251 dev_vdbg(&dev
->pdev
->dev
, "dev->devcap: %d\n", dev
->devcap
);
3253 dev_err(&dev
->pdev
->dev
, "can't support device mode\n");
3258 /* a pair of endpoints (out/in) for each address */
3259 dev
->ep_max
= DEN(readl(&dev
->cap_regs
->dccparams
)) * 2;
3260 dev_vdbg(&dev
->pdev
->dev
, "dev->ep_max: %d\n", dev
->ep_max
);
3262 /* allocate endpoints memory */
3263 dev
->ep
= kzalloc(sizeof(struct langwell_ep
) * dev
->ep_max
,
3266 dev_err(&dev
->pdev
->dev
, "allocate endpoints memory failed\n");
3271 /* allocate device dQH memory */
3272 size
= dev
->ep_max
* sizeof(struct langwell_dqh
);
3273 dev_vdbg(&dev
->pdev
->dev
, "orig size = %d\n", size
);
3274 if (size
< DQH_ALIGNMENT
)
3275 size
= DQH_ALIGNMENT
;
3276 else if ((size
% DQH_ALIGNMENT
) != 0) {
3277 size
+= DQH_ALIGNMENT
+ 1;
3278 size
&= ~(DQH_ALIGNMENT
- 1);
3280 dev
->ep_dqh
= dma_alloc_coherent(&pdev
->dev
, size
,
3281 &dev
->ep_dqh_dma
, GFP_KERNEL
);
3283 dev_err(&dev
->pdev
->dev
, "allocate dQH memory failed\n");
3287 dev
->ep_dqh_size
= size
;
3288 dev_vdbg(&dev
->pdev
->dev
, "ep_dqh_size = %d\n", dev
->ep_dqh_size
);
3290 /* initialize ep0 status request structure */
3291 dev
->status_req
= kzalloc(sizeof(struct langwell_request
), GFP_KERNEL
);
3292 if (!dev
->status_req
) {
3293 dev_err(&dev
->pdev
->dev
,
3294 "allocate status_req memory failed\n");
3298 INIT_LIST_HEAD(&dev
->status_req
->queue
);
3300 /* allocate a small amount of memory to get valid address */
3301 dev
->status_req
->req
.buf
= kmalloc(8, GFP_KERNEL
);
3302 dev
->status_req
->req
.dma
= virt_to_phys(dev
->status_req
->req
.buf
);
3304 dev
->resume_state
= USB_STATE_NOTATTACHED
;
3305 dev
->usb_state
= USB_STATE_POWERED
;
3306 dev
->ep0_dir
= USB_DIR_OUT
;
3308 /* remote wakeup reset to 0 when the device is reset */
3309 dev
->remote_wakeup
= 0;
3310 dev
->dev_status
= 1 << USB_DEVICE_SELF_POWERED
;
3312 #ifndef OTG_TRANSCEIVER
3313 /* reset device controller */
3314 langwell_udc_reset(dev
);
3317 /* initialize gadget structure */
3318 dev
->gadget
.ops
= &langwell_ops
; /* usb_gadget_ops */
3319 dev
->gadget
.ep0
= &dev
->ep
[0].ep
; /* gadget ep0 */
3320 INIT_LIST_HEAD(&dev
->gadget
.ep_list
); /* ep_list */
3321 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
; /* speed */
3322 dev
->gadget
.is_dualspeed
= 1; /* support dual speed */
3323 #ifdef OTG_TRANSCEIVER
3324 dev
->gadget
.is_otg
= 1; /* support otg mode */
3327 /* the "gadget" abstracts/virtualizes the controller */
3328 dev_set_name(&dev
->gadget
.dev
, "gadget");
3329 dev
->gadget
.dev
.parent
= &pdev
->dev
;
3330 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
3331 dev
->gadget
.dev
.release
= gadget_release
;
3332 dev
->gadget
.name
= driver_name
; /* gadget name */
3334 /* controller endpoints reinit */
3337 #ifndef OTG_TRANSCEIVER
3338 /* reset ep0 dQH and endptctrl */
3342 /* create dTD dma_pool resource */
3343 dev
->dtd_pool
= dma_pool_create("langwell_dtd",
3345 sizeof(struct langwell_dtd
),
3349 if (!dev
->dtd_pool
) {
3355 dev_info(&dev
->pdev
->dev
, "%s\n", driver_desc
);
3356 dev_info(&dev
->pdev
->dev
, "irq %d, pci mem %p\n", pdev
->irq
, base
);
3357 dev_info(&dev
->pdev
->dev
, "Driver version: " DRIVER_VERSION
"\n");
3358 dev_info(&dev
->pdev
->dev
, "Support (max) %d endpoints\n", dev
->ep_max
);
3359 dev_info(&dev
->pdev
->dev
, "Device interface version: 0x%04x\n",
3361 dev_info(&dev
->pdev
->dev
, "Controller mode: %s\n",
3362 dev
->devcap
? "Device" : "Host");
3363 dev_info(&dev
->pdev
->dev
, "Support USB LPM: %s\n",
3364 dev
->lpm
? "Yes" : "No");
3366 dev_vdbg(&dev
->pdev
->dev
,
3367 "After langwell_udc_probe(), print all registers:\n");
3368 print_all_registers(dev
);
3370 the_controller
= dev
;
3372 retval
= device_register(&dev
->gadget
.dev
);
3376 retval
= device_create_file(&pdev
->dev
, &dev_attr_langwell_udc
);
3380 retval
= device_create_file(&pdev
->dev
, &dev_attr_remote_wakeup
);
3384 dev_vdbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
3388 device_remove_file(&pdev
->dev
, &dev_attr_langwell_udc
);
3391 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
3392 langwell_udc_remove(pdev
);
3399 /* device controller suspend */
3400 static int langwell_udc_suspend(struct pci_dev
*pdev
, pm_message_t state
)
3402 struct langwell_udc
*dev
= the_controller
;
3404 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
3406 /* disable interrupt and set controller to stop state */
3407 langwell_udc_stop(dev
);
3409 /* disable IRQ handler */
3411 free_irq(pdev
->irq
, dev
);
3414 /* save PCI state */
3415 pci_save_state(pdev
);
3417 spin_lock_irq(&dev
->lock
);
3418 /* stop all usb activities */
3419 stop_activity(dev
, dev
->driver
);
3420 spin_unlock_irq(&dev
->lock
);
3422 /* free dTD dma_pool and dQH */
3424 dma_pool_destroy(dev
->dtd_pool
);
3427 dma_free_coherent(&pdev
->dev
, dev
->ep_dqh_size
,
3428 dev
->ep_dqh
, dev
->ep_dqh_dma
);
3430 /* release SRAM caching */
3431 if (dev
->has_sram
&& dev
->got_sram
)
3434 /* set device power state */
3435 pci_set_power_state(pdev
, PCI_D3hot
);
3437 /* enter PHY low power suspend */
3438 if (dev
->pdev
->device
!= 0x0829)
3439 langwell_phy_low_power(dev
, 1);
3441 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
3446 /* device controller resume */
3447 static int langwell_udc_resume(struct pci_dev
*pdev
)
3449 struct langwell_udc
*dev
= the_controller
;
3452 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
3454 /* exit PHY low power suspend */
3455 if (dev
->pdev
->device
!= 0x0829)
3456 langwell_phy_low_power(dev
, 0);
3458 /* set device D0 power state */
3459 pci_set_power_state(pdev
, PCI_D0
);
3461 /* enable SRAM caching if detected */
3462 if (dev
->has_sram
&& !dev
->got_sram
)
3465 /* allocate device dQH memory */
3466 size
= dev
->ep_max
* sizeof(struct langwell_dqh
);
3467 dev_vdbg(&dev
->pdev
->dev
, "orig size = %d\n", size
);
3468 if (size
< DQH_ALIGNMENT
)
3469 size
= DQH_ALIGNMENT
;
3470 else if ((size
% DQH_ALIGNMENT
) != 0) {
3471 size
+= DQH_ALIGNMENT
+ 1;
3472 size
&= ~(DQH_ALIGNMENT
- 1);
3474 dev
->ep_dqh
= dma_alloc_coherent(&pdev
->dev
, size
,
3475 &dev
->ep_dqh_dma
, GFP_KERNEL
);
3477 dev_err(&dev
->pdev
->dev
, "allocate dQH memory failed\n");
3480 dev
->ep_dqh_size
= size
;
3481 dev_vdbg(&dev
->pdev
->dev
, "ep_dqh_size = %d\n", dev
->ep_dqh_size
);
3483 /* create dTD dma_pool resource */
3484 dev
->dtd_pool
= dma_pool_create("langwell_dtd",
3486 sizeof(struct langwell_dtd
),
3493 /* restore PCI state */
3494 pci_restore_state(pdev
);
3496 /* enable IRQ handler */
3497 if (request_irq(pdev
->irq
, langwell_irq
, IRQF_SHARED
,
3498 driver_name
, dev
) != 0) {
3499 dev_err(&dev
->pdev
->dev
, "request interrupt %d failed\n",
3505 /* reset and start controller to run state */
3507 /* reset device controller */
3508 langwell_udc_reset(dev
);
3510 /* reset ep0 dQH and endptctrl */
3513 /* start device if gadget is loaded */
3515 langwell_udc_start(dev
);
3518 /* reset USB status */
3519 dev
->usb_state
= USB_STATE_ATTACHED
;
3520 dev
->ep0_state
= WAIT_FOR_SETUP
;
3521 dev
->ep0_dir
= USB_DIR_OUT
;
3523 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
3528 /* pci driver shutdown */
3529 static void langwell_udc_shutdown(struct pci_dev
*pdev
)
3531 struct langwell_udc
*dev
= the_controller
;
3534 dev_dbg(&dev
->pdev
->dev
, "---> %s()\n", __func__
);
3536 /* reset controller mode to IDLE */
3537 usbmode
= readl(&dev
->op_regs
->usbmode
);
3538 dev_dbg(&dev
->pdev
->dev
, "usbmode = 0x%08x\n", usbmode
);
3539 usbmode
&= (~3 | MODE_IDLE
);
3540 writel(usbmode
, &dev
->op_regs
->usbmode
);
3542 dev_dbg(&dev
->pdev
->dev
, "<--- %s()\n", __func__
);
3545 /*-------------------------------------------------------------------------*/
3547 static const struct pci_device_id pci_ids
[] = { {
3548 .class = ((PCI_CLASS_SERIAL_USB
<< 8) | 0xfe),
3552 .subvendor
= PCI_ANY_ID
,
3553 .subdevice
= PCI_ANY_ID
,
3554 }, { /* end: all zeroes */ }
3557 MODULE_DEVICE_TABLE(pci
, pci_ids
);
3560 static struct pci_driver langwell_pci_driver
= {
3561 .name
= (char *) driver_name
,
3562 .id_table
= pci_ids
,
3564 .probe
= langwell_udc_probe
,
3565 .remove
= langwell_udc_remove
,
3567 /* device controller suspend/resume */
3568 .suspend
= langwell_udc_suspend
,
3569 .resume
= langwell_udc_resume
,
3571 .shutdown
= langwell_udc_shutdown
,
3575 static int __init
init(void)
3577 #ifdef OTG_TRANSCEIVER
3578 return langwell_register_peripheral(&langwell_pci_driver
);
3580 return pci_register_driver(&langwell_pci_driver
);
3586 static void __exit
cleanup(void)
3588 #ifdef OTG_TRANSCEIVER
3589 return langwell_unregister_peripheral(&langwell_pci_driver
);
3591 pci_unregister_driver(&langwell_pci_driver
);
3594 module_exit(cleanup
);
3597 MODULE_DESCRIPTION(DRIVER_DESC
);
3598 MODULE_AUTHOR("Xiaochen Shen <xiaochen.shen@intel.com>");
3599 MODULE_VERSION(DRIVER_VERSION
);
3600 MODULE_LICENSE("GPL");