2 * drivers/usb/gadget/emxx_udc.c
3 * EMXX FCD (Function Controller Driver) for USB.
5 * Copyright (C) 2010 Renesas Electronics Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/delay.h>
21 #include <linux/ioport.h>
22 #include <linux/slab.h>
23 #include <linux/errno.h>
24 #include <linux/list.h>
25 #include <linux/interrupt.h>
26 #include <linux/proc_fs.h>
27 #include <linux/clk.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/workqueue.h>
32 #include <linux/device.h>
34 #include <linux/usb/ch9.h>
35 #include <linux/usb/gadget.h>
37 #include <linux/irq.h>
38 #include <linux/gpio.h>
42 #define DRIVER_DESC "EMXX UDC driver"
43 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
45 static const char driver_name[] = "emxx_udc";
46 static const char driver_desc[] = DRIVER_DESC;
48 /*===========================================================================*/
50 static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *, struct nbu2ss_ep *);
51 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *);
52 /*static void _nbu2ss_ep0_disable(struct nbu2ss_udc *);*/
53 static void _nbu2ss_ep_done(struct nbu2ss_ep *, struct nbu2ss_req *, int);
54 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *, u32 mode);
55 static void _nbu2ss_endpoint_toggle_reset(struct nbu2ss_udc *udc, u8 ep_adrs);
57 static int _nbu2ss_pullup(struct nbu2ss_udc *, int);
58 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *, struct nbu2ss_ep *);
60 /*===========================================================================*/
62 #define _nbu2ss_zero_len_pkt(udc, epnum) \
63 _nbu2ss_ep_in_end(udc, epnum, 0, 0)
65 /*===========================================================================*/
67 struct nbu2ss_udc udc_controller;
69 /*-------------------------------------------------------------------------*/
71 static inline u32 _nbu2ss_readl(void *address)
73 return __raw_readl(address);
76 /*-------------------------------------------------------------------------*/
78 static inline void _nbu2ss_writel(void *address, u32 udata)
80 __raw_writel(udata, address);
83 /*-------------------------------------------------------------------------*/
85 static inline void _nbu2ss_bitset(void *address, u32 udata)
87 u32 reg_dt = __raw_readl(address) | (udata);
89 __raw_writel(reg_dt, address);
92 /*-------------------------------------------------------------------------*/
94 static inline void _nbu2ss_bitclr(void *address, u32 udata)
96 u32 reg_dt = __raw_readl(address) & ~(udata);
98 __raw_writel(reg_dt, address);
101 #ifdef UDC_DEBUG_DUMP
102 /*-------------------------------------------------------------------------*/
103 static void _nbu2ss_dump_register(struct nbu2ss_udc *udc)
108 pr_info("=== %s()\n", __func__);
111 pr_err("%s udc == NULL\n", __func__);
115 spin_unlock(&udc->lock);
117 dev_dbg(&udc->dev, "\n-USB REG-\n");
118 for (i = 0x0 ; i < USB_BASE_SIZE ; i += 16) {
119 reg_data = _nbu2ss_readl(
120 (u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i));
121 dev_dbg(&udc->dev, "USB%04x =%08x", i, (int)reg_data);
123 reg_data = _nbu2ss_readl(
124 (u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i + 4));
125 dev_dbg(&udc->dev, " %08x", (int)reg_data);
127 reg_data = _nbu2ss_readl(
128 (u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i + 8));
129 dev_dbg(&udc->dev, " %08x", (int)reg_data);
131 reg_data = _nbu2ss_readl(
132 (u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i + 12));
133 dev_dbg(&udc->dev, " %08x\n", (int)reg_data);
136 spin_lock(&udc->lock);
138 #endif /* UDC_DEBUG_DUMP */
140 /*-------------------------------------------------------------------------*/
141 /* Endpoint 0 Callback (Complete) */
142 static void _nbu2ss_ep0_complete(struct usb_ep *_ep, struct usb_request *_req)
147 struct usb_ctrlrequest *p_ctrl;
148 struct nbu2ss_udc *udc;
150 if ((!_ep) || (!_req))
153 udc = (struct nbu2ss_udc *)_req->context;
155 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
156 if (p_ctrl->bRequest == USB_REQ_SET_FEATURE) {
157 /*-------------------------------------------------*/
159 recipient = (u8)(p_ctrl->bRequestType & USB_RECIP_MASK);
160 selector = p_ctrl->wValue;
161 if ((recipient == USB_RECIP_DEVICE) &&
162 (selector == USB_DEVICE_TEST_MODE)) {
163 test_mode = (u32)(p_ctrl->wIndex >> 8);
164 _nbu2ss_set_test_mode(udc, test_mode);
170 /*-------------------------------------------------------------------------*/
171 /* Initialization usb_request */
172 static void _nbu2ss_create_ep0_packet(
173 struct nbu2ss_udc *udc,
178 udc->ep0_req.req.buf = p_buf;
179 udc->ep0_req.req.length = length;
180 udc->ep0_req.req.dma = 0;
181 udc->ep0_req.req.zero = TRUE;
182 udc->ep0_req.req.complete = _nbu2ss_ep0_complete;
183 udc->ep0_req.req.status = -EINPROGRESS;
184 udc->ep0_req.req.context = udc;
185 udc->ep0_req.req.actual = 0;
188 /*-------------------------------------------------------------------------*/
189 /* Acquisition of the first address of RAM(FIFO) */
190 static u32 _nbu2ss_get_begin_ram_address(struct nbu2ss_udc *udc)
193 u32 data, last_ram_adr, use_ram_size;
195 struct ep_regs *p_ep_regs;
197 last_ram_adr = (D_RAM_SIZE_CTRL / sizeof(u32)) * 2;
200 for (num = 0; num < NUM_ENDPOINTS - 1; num++) {
201 p_ep_regs = &udc->p_regs->EP_REGS[num];
202 data = _nbu2ss_readl(&p_ep_regs->EP_PCKT_ADRS);
203 buf_type = _nbu2ss_readl(&p_ep_regs->EP_CONTROL) & EPn_BUF_TYPE;
206 use_ram_size += (data & EPn_MPKT) / sizeof(u32);
209 use_ram_size += ((data & EPn_MPKT) / sizeof(u32)) * 2;
212 if ((data >> 16) > last_ram_adr)
213 last_ram_adr = data >> 16;
216 return last_ram_adr + use_ram_size;
219 /*-------------------------------------------------------------------------*/
220 /* Construction of Endpoint */
221 static int _nbu2ss_ep_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
232 /*-------------------------------------------------------------*/
233 /* RAM Transfer Address */
234 begin_adrs = _nbu2ss_get_begin_ram_address(udc);
235 data = (begin_adrs << 16) | ep->ep.maxpacket;
236 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, data);
238 /*-------------------------------------------------------------*/
239 /* Interrupt Enable */
240 data = 1 << (ep->epnum + 8);
241 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, data);
243 /*-------------------------------------------------------------*/
244 /* Endpoint Type(Mode) */
245 /* Bulk, Interrupt, ISO */
246 switch (ep->ep_type) {
247 case USB_ENDPOINT_XFER_BULK:
251 case USB_ENDPOINT_XFER_INT:
252 data = EPn_BUF_SINGLE | EPn_INTERRUPT;
255 case USB_ENDPOINT_XFER_ISOC:
264 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
265 _nbu2ss_endpoint_toggle_reset(udc, (ep->epnum | ep->direct));
267 if (ep->direct == USB_DIR_OUT) {
268 /*---------------------------------------------------------*/
270 data = EPn_EN | EPn_BCLR | EPn_DIR0;
271 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
273 data = EPn_ONAK | EPn_OSTL_EN | EPn_OSTL;
274 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
276 data = EPn_OUT_EN | EPn_OUT_END_EN;
277 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
279 /*---------------------------------------------------------*/
281 data = EPn_EN | EPn_BCLR | EPn_AUTO;
282 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
285 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
287 data = EPn_IN_EN | EPn_IN_END_EN;
288 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
294 /*-------------------------------------------------------------------------*/
295 /* Release of Endpoint */
296 static int _nbu2ss_epn_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
301 if ((ep->epnum == 0) || (udc->vbus_active == 0))
306 /*-------------------------------------------------------------*/
307 /* RAM Transfer Address */
308 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, 0);
310 /*-------------------------------------------------------------*/
311 /* Interrupt Disable */
312 data = 1 << (ep->epnum + 8);
313 _nbu2ss_bitclr(&udc->p_regs->USB_INT_ENA, data);
315 if (ep->direct == USB_DIR_OUT) {
316 /*---------------------------------------------------------*/
318 data = EPn_ONAK | EPn_BCLR;
319 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
321 data = EPn_EN | EPn_DIR0;
322 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
324 data = EPn_OUT_EN | EPn_OUT_END_EN;
325 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
327 /*---------------------------------------------------------*/
330 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
332 data = EPn_EN | EPn_AUTO;
333 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
335 data = EPn_IN_EN | EPn_IN_END_EN;
336 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
342 /*-------------------------------------------------------------------------*/
343 /* DMA setting (without Endpoint 0) */
344 static void _nbu2ss_ep_dma_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
349 data = _nbu2ss_readl(&udc->p_regs->USBSSCONF);
350 if (((ep->epnum == 0) || (data & (1 << ep->epnum)) == 0))
351 return; /* Not Support DMA */
355 if (ep->direct == USB_DIR_OUT) {
356 /*---------------------------------------------------------*/
358 data = ep->ep.maxpacket;
359 _nbu2ss_writel(&udc->p_regs->EP_DCR[num].EP_DCR2, data);
361 /*---------------------------------------------------------*/
362 /* Transfer Direct */
363 data = DCR1_EPn_DIR0;
364 _nbu2ss_bitset(&udc->p_regs->EP_DCR[num].EP_DCR1, data);
366 /*---------------------------------------------------------*/
368 data = EPn_STOP_MODE | EPn_STOP_SET | EPn_DMAMODE0;
369 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
371 /*---------------------------------------------------------*/
373 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, EPn_AUTO);
375 /*---------------------------------------------------------*/
377 data = EPn_BURST_SET | EPn_DMAMODE0;
378 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
382 /*-------------------------------------------------------------------------*/
383 /* DMA setting release */
384 static void _nbu2ss_ep_dma_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
388 struct fc_regs *preg = udc->p_regs;
390 if (udc->vbus_active == 0)
391 return; /* VBUS OFF */
393 data = _nbu2ss_readl(&preg->USBSSCONF);
394 if ((ep->epnum == 0) || ((data & (1 << ep->epnum)) == 0))
395 return; /* Not Support DMA */
399 _nbu2ss_ep_dma_abort(udc, ep);
401 if (ep->direct == USB_DIR_OUT) {
402 /*---------------------------------------------------------*/
404 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, 0);
405 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPn_DIR0);
406 _nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
408 /*---------------------------------------------------------*/
410 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPn_AUTO);
411 _nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
415 /*-------------------------------------------------------------------------*/
417 static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
419 struct fc_regs *preg = udc->p_regs;
421 _nbu2ss_bitclr(&preg->EP_DCR[ep->epnum - 1].EP_DCR1, DCR1_EPn_REQEN);
422 mdelay(DMA_DISABLE_TIME); /* DCR1_EPn_REQEN Clear */
423 _nbu2ss_bitclr(&preg->EP_REGS[ep->epnum - 1].EP_DMA_CTRL, EPn_DMA_EN);
426 /*-------------------------------------------------------------------------*/
427 /* Start IN Transfer */
428 static void _nbu2ss_ep_in_end(
429 struct nbu2ss_udc *udc,
437 struct fc_regs *preg = udc->p_regs;
439 if (length >= sizeof(u32))
443 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_AUTO);
445 /* Writing of 1-4 bytes */
447 _nbu2ss_writel(&preg->EP0_WRITE, data32);
449 data = ((length << 5) & EP0_DW) | EP0_DEND;
450 _nbu2ss_writel(&preg->EP0_CONTROL, data);
452 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_AUTO);
456 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPn_AUTO);
458 /* Writing of 1-4 bytes */
460 _nbu2ss_writel(&preg->EP_REGS[num].EP_WRITE, data32);
462 data = (((length) << 5) & EPn_DW) | EPn_DEND;
463 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
465 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, EPn_AUTO);
470 /*-------------------------------------------------------------------------*/
471 static void _nbu2ss_dma_map_single(
472 struct nbu2ss_udc *udc,
473 struct nbu2ss_ep *ep,
474 struct nbu2ss_req *req,
478 if (req->req.dma == DMA_ADDR_INVALID) {
479 if (req->unaligned) {
480 req->req.dma = ep->phys_buf;
482 req->req.dma = dma_map_single(
483 udc->gadget.dev.parent,
486 (direct == USB_DIR_IN)
487 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
492 dma_sync_single_for_device(
493 udc->gadget.dev.parent,
496 (direct == USB_DIR_IN)
497 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
503 /*-------------------------------------------------------------------------*/
504 static void _nbu2ss_dma_unmap_single(
505 struct nbu2ss_udc *udc,
506 struct nbu2ss_ep *ep,
507 struct nbu2ss_req *req,
515 if (direct == USB_DIR_OUT) {
516 count = req->req.actual % 4;
519 p += (req->req.actual - count);
520 memcpy(data, p, count);
525 if (req->unaligned) {
526 if (direct == USB_DIR_OUT)
527 memcpy(req->req.buf, ep->virt_buf,
528 req->req.actual & 0xfffffffc);
530 dma_unmap_single(udc->gadget.dev.parent,
531 req->req.dma, req->req.length,
532 (direct == USB_DIR_IN)
535 req->req.dma = DMA_ADDR_INVALID;
539 dma_sync_single_for_cpu(udc->gadget.dev.parent,
540 req->req.dma, req->req.length,
541 (direct == USB_DIR_IN)
548 p += (req->req.actual - count);
549 memcpy(p, data, count);
554 /*-------------------------------------------------------------------------*/
555 /* Endpoint 0 OUT Transfer (PIO) */
556 static int ep0_out_pio(struct nbu2ss_udc *udc, u8 *buf, u32 length)
559 u32 numreads = length / sizeof(u32);
560 union usb_reg_access *buf32 = (union usb_reg_access *)buf;
566 for (i = 0; i < numreads; i++) {
567 buf32->dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
571 return numreads * sizeof(u32);
574 /*-------------------------------------------------------------------------*/
575 /* Endpoint 0 OUT Transfer (PIO, OverBytes) */
576 static int EP0_out_OverBytes(struct nbu2ss_udc *udc, u8 *pBuf, u32 length)
580 union usb_reg_access Temp32;
581 union usb_reg_access *pBuf32 = (union usb_reg_access *)pBuf;
583 if ((length > 0) && (length < sizeof(u32))) {
584 Temp32.dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
585 for (i = 0 ; i < length ; i++)
586 pBuf32->byte.DATA[i] = Temp32.byte.DATA[i];
593 /*-------------------------------------------------------------------------*/
594 /* Endpoint 0 IN Transfer (PIO) */
595 static int EP0_in_PIO(struct nbu2ss_udc *udc, u8 *pBuf, u32 length)
598 u32 iMaxLength = EP0_PACKETSIZE;
600 u32 iWriteLength = 0;
601 union usb_reg_access *pBuf32 = (union usb_reg_access *)pBuf;
603 /*------------------------------------------------------------*/
604 /* Transfer Length */
605 if (iMaxLength < length)
606 iWordLength = iMaxLength / sizeof(u32);
608 iWordLength = length / sizeof(u32);
610 /*------------------------------------------------------------*/
612 for (i = 0; i < iWordLength; i++) {
613 _nbu2ss_writel(&udc->p_regs->EP0_WRITE, pBuf32->dw);
615 iWriteLength += sizeof(u32);
621 /*-------------------------------------------------------------------------*/
622 /* Endpoint 0 IN Transfer (PIO, OverBytes) */
623 static int EP0_in_OverBytes(struct nbu2ss_udc *udc, u8 *pBuf, u32 iRemainSize)
626 union usb_reg_access Temp32;
627 union usb_reg_access *pBuf32 = (union usb_reg_access *)pBuf;
629 if ((iRemainSize > 0) && (iRemainSize < sizeof(u32))) {
630 for (i = 0 ; i < iRemainSize ; i++)
631 Temp32.byte.DATA[i] = pBuf32->byte.DATA[i];
632 _nbu2ss_ep_in_end(udc, 0, Temp32.dw, iRemainSize);
640 /*-------------------------------------------------------------------------*/
641 /* Transfer NULL Packet (Epndoint 0) */
642 static int EP0_send_NULL(struct nbu2ss_udc *udc, bool pid_flag)
646 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
647 data &= ~(u32)EP0_INAK;
650 data |= (EP0_INAK_EN | EP0_PIDCLR | EP0_DEND);
652 data |= (EP0_INAK_EN | EP0_DEND);
654 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
659 /*-------------------------------------------------------------------------*/
660 /* Receive NULL Packet (Endpoint 0) */
661 static int EP0_receive_NULL(struct nbu2ss_udc *udc, bool pid_flag)
665 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
666 data &= ~(u32)EP0_ONAK;
671 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
676 /*-------------------------------------------------------------------------*/
677 static int _nbu2ss_ep0_in_transfer(
678 struct nbu2ss_udc *udc,
679 struct nbu2ss_req *req
682 u8 *pBuffer; /* IN Data Buffer */
687 /*-------------------------------------------------------------*/
688 /* End confirmation */
689 if (req->req.actual == req->req.length) {
690 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
693 EP0_send_NULL(udc, FALSE);
698 return 0; /* Transfer End */
701 /*-------------------------------------------------------------*/
703 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
705 data &= ~(u32)EP0_INAK;
706 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
708 iRemainSize = req->req.length - req->req.actual;
709 pBuffer = (u8 *)req->req.buf;
710 pBuffer += req->req.actual;
712 /*-------------------------------------------------------------*/
714 result = EP0_in_PIO(udc, pBuffer, iRemainSize);
716 req->div_len = result;
717 iRemainSize -= result;
719 if (iRemainSize == 0) {
720 EP0_send_NULL(udc, FALSE);
724 if ((iRemainSize < sizeof(u32)) && (result != EP0_PACKETSIZE)) {
726 result += EP0_in_OverBytes(udc, pBuffer, iRemainSize);
727 req->div_len = result;
733 /*-------------------------------------------------------------------------*/
734 static int _nbu2ss_ep0_out_transfer(
735 struct nbu2ss_udc *udc,
736 struct nbu2ss_req *req
745 /*-------------------------------------------------------------*/
746 /* Receive data confirmation */
747 iRecvLength = _nbu2ss_readl(&udc->p_regs->EP0_LENGTH) & EP0_LDATA;
748 if (iRecvLength != 0) {
751 iRemainSize = req->req.length - req->req.actual;
752 pBuffer = (u8 *)req->req.buf;
753 pBuffer += req->req.actual;
755 result = ep0_out_pio(udc, pBuffer
756 , min(iRemainSize, iRecvLength));
760 req->req.actual += result;
761 iRecvLength -= result;
763 if ((iRecvLength > 0) && (iRecvLength < sizeof(u32))) {
765 iRemainSize -= result;
767 result = EP0_out_OverBytes(udc, pBuffer
768 , min(iRemainSize, iRecvLength));
769 req->req.actual += result;
775 /*-------------------------------------------------------------*/
776 /* End confirmation */
777 if (req->req.actual == req->req.length) {
778 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
781 EP0_receive_NULL(udc, FALSE);
786 return 0; /* Transfer End */
789 if ((req->req.actual % EP0_PACKETSIZE) != 0)
790 return 0; /* Short Packet Transfer End */
792 if (req->req.actual > req->req.length) {
793 dev_err(udc->dev, " *** Overrun Error\n");
798 iRemainSize = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
799 if (iRemainSize & EP0_ONAK) {
800 /*---------------------------------------------------*/
802 _nbu2ss_bitclr(&udc->p_regs->EP0_CONTROL, EP0_ONAK);
810 /*-------------------------------------------------------------------------*/
811 static int _nbu2ss_out_dma(
812 struct nbu2ss_udc *udc,
813 struct nbu2ss_req *req,
824 int result = -EINVAL;
825 struct fc_regs *preg = udc->p_regs;
828 return 1; /* DMA is forwarded */
830 req->dma_flag = TRUE;
831 pBuffer = req->req.dma;
832 pBuffer += req->req.actual;
835 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)pBuffer);
837 /* Number of transfer packets */
838 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPn_MPKT;
839 dmacnt = length / mpkt;
840 lmpkt = (length % mpkt) & ~(u32)0x03;
842 if (dmacnt > DMA_MAX_COUNT) {
843 dmacnt = DMA_MAX_COUNT;
845 } else if (lmpkt != 0) {
847 burst = 0; /* Burst OFF */
851 data = mpkt | (lmpkt << 16);
852 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
854 data = ((dmacnt & 0xff) << 16) | DCR1_EPn_DIR0 | DCR1_EPn_REQEN;
855 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
858 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, 0);
859 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_BURST_SET);
861 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT
863 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_BURST_SET);
865 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_DMA_EN);
867 result = length & ~(u32)0x03;
868 req->div_len = result;
873 /*-------------------------------------------------------------------------*/
874 static int _nbu2ss_epn_out_pio(
875 struct nbu2ss_udc *udc,
876 struct nbu2ss_ep *ep,
877 struct nbu2ss_req *req,
885 union usb_reg_access Temp32;
886 union usb_reg_access *pBuf32;
888 struct fc_regs *preg = udc->p_regs;
891 return 1; /* DMA is forwarded */
896 pBuffer = (u8 *)req->req.buf;
897 pBuf32 = (union usb_reg_access *)(pBuffer + req->req.actual);
899 iWordLength = length / sizeof(u32);
900 if (iWordLength > 0) {
901 /*---------------------------------------------------------*/
902 /* Copy of every four bytes */
903 for (i = 0; i < iWordLength; i++) {
905 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
908 result = iWordLength * sizeof(u32);
911 data = length - result;
913 /*---------------------------------------------------------*/
914 /* Copy of fraction byte */
915 Temp32.dw = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
916 for (i = 0 ; i < data ; i++)
917 pBuf32->byte.DATA[i] = Temp32.byte.DATA[i];
921 req->req.actual += result;
923 if ((req->req.actual == req->req.length) ||
924 ((req->req.actual % ep->ep.maxpacket) != 0)) {
931 /*-------------------------------------------------------------------------*/
932 static int _nbu2ss_epn_out_data(
933 struct nbu2ss_udc *udc,
934 struct nbu2ss_ep *ep,
935 struct nbu2ss_req *req,
948 iBufSize = min((req->req.length - req->req.actual), data_size);
950 if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
951 (iBufSize >= sizeof(u32))) {
952 nret = _nbu2ss_out_dma(udc, req, num, iBufSize);
954 iBufSize = min_t(u32, iBufSize, ep->ep.maxpacket);
955 nret = _nbu2ss_epn_out_pio(udc, ep, req, iBufSize);
961 /*-------------------------------------------------------------------------*/
962 static int _nbu2ss_epn_out_transfer(
963 struct nbu2ss_udc *udc,
964 struct nbu2ss_ep *ep,
965 struct nbu2ss_req *req
971 struct fc_regs *preg = udc->p_regs;
978 /*-------------------------------------------------------------*/
981 = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT) & EPn_LDATA;
983 if (iRecvLength != 0) {
984 result = _nbu2ss_epn_out_data(udc, ep, req, iRecvLength);
985 if (iRecvLength < ep->ep.maxpacket) {
986 if (iRecvLength == result) {
987 req->req.actual += result;
992 if ((req->req.actual == req->req.length) ||
993 ((req->req.actual % ep->ep.maxpacket) != 0)) {
999 if ((req->req.actual % ep->ep.maxpacket) == 0) {
1007 if (req->req.actual > req->req.length) {
1008 dev_err(udc->dev, " Overrun Error\n");
1009 dev_err(udc->dev, " actual = %d, length = %d\n",
1010 req->req.actual, req->req.length);
1011 result = -EOVERFLOW;
1017 /*-------------------------------------------------------------------------*/
1018 static int _nbu2ss_in_dma(
1019 struct nbu2ss_udc *udc,
1020 struct nbu2ss_ep *ep,
1021 struct nbu2ss_req *req,
1027 u32 mpkt; /* MaxPacketSize */
1028 u32 lmpkt; /* Last Packet Data Size */
1029 u32 dmacnt; /* IN Data Size */
1032 int result = -EINVAL;
1033 struct fc_regs *preg = udc->p_regs;
1036 return 1; /* DMA is forwarded */
1039 if (req->req.actual == 0)
1040 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_IN);
1042 req->dma_flag = TRUE;
1044 /* MAX Packet Size */
1045 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPn_MPKT;
1047 if ((DMA_MAX_COUNT * mpkt) < length)
1048 iWriteLength = DMA_MAX_COUNT * mpkt;
1050 iWriteLength = length;
1052 /*------------------------------------------------------------*/
1053 /* Number of transmission packets */
1054 if (mpkt < iWriteLength) {
1055 dmacnt = iWriteLength / mpkt;
1056 lmpkt = (iWriteLength % mpkt) & ~(u32)0x3;
1060 lmpkt = mpkt & ~(u32)0x3;
1064 lmpkt = iWriteLength & ~(u32)0x3;
1067 /* Packet setting */
1068 data = mpkt | (lmpkt << 16);
1069 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
1071 /* Address setting */
1072 pBuffer = req->req.dma;
1073 pBuffer += req->req.actual;
1074 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)pBuffer);
1076 /* Packet and DMA setting */
1077 data = ((dmacnt & 0xff) << 16) | DCR1_EPn_REQEN;
1078 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
1080 /* Packet setting of EPC */
1081 data = dmacnt << 16;
1082 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, data);
1084 /*DMA setting of EPC */
1085 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_DMA_EN);
1087 result = iWriteLength & ~(u32)0x3;
1088 req->div_len = result;
1093 /*-------------------------------------------------------------------------*/
1094 static int _nbu2ss_epn_in_pio(
1095 struct nbu2ss_udc *udc,
1096 struct nbu2ss_ep *ep,
1097 struct nbu2ss_req *req,
1105 union usb_reg_access Temp32;
1106 union usb_reg_access *pBuf32 = NULL;
1108 struct fc_regs *preg = udc->p_regs;
1111 return 1; /* DMA is forwarded */
1114 pBuffer = (u8 *)req->req.buf;
1115 pBuf32 = (union usb_reg_access *)(pBuffer + req->req.actual);
1117 iWordLength = length / sizeof(u32);
1118 if (iWordLength > 0) {
1119 for (i = 0; i < iWordLength; i++) {
1121 &preg->EP_REGS[ep->epnum - 1].EP_WRITE
1127 result = iWordLength * sizeof(u32);
1131 if (result != ep->ep.maxpacket) {
1132 data = length - result;
1134 for (i = 0 ; i < data ; i++)
1135 Temp32.byte.DATA[i] = pBuf32->byte.DATA[i];
1137 _nbu2ss_ep_in_end(udc, ep->epnum, Temp32.dw, data);
1141 req->div_len = result;
1146 /*-------------------------------------------------------------------------*/
1147 static int _nbu2ss_epn_in_data(
1148 struct nbu2ss_udc *udc,
1149 struct nbu2ss_ep *ep,
1150 struct nbu2ss_req *req,
1160 num = ep->epnum - 1;
1162 if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
1163 (data_size >= sizeof(u32))) {
1164 nret = _nbu2ss_in_dma(udc, ep, req, num, data_size);
1166 data_size = min_t(u32, data_size, ep->ep.maxpacket);
1167 nret = _nbu2ss_epn_in_pio(udc, ep, req, data_size);
1173 /*-------------------------------------------------------------------------*/
1174 static int _nbu2ss_epn_in_transfer(
1175 struct nbu2ss_udc *udc,
1176 struct nbu2ss_ep *ep,
1177 struct nbu2ss_req *req
1188 num = ep->epnum - 1;
1190 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
1192 /*-------------------------------------------------------------*/
1193 /* State confirmation of FIFO */
1194 if (req->req.actual == 0) {
1195 if ((status & EPn_IN_EMPTY) == 0)
1196 return 1; /* Not Empty */
1199 if ((status & EPn_IN_FULL) != 0)
1200 return 1; /* Not Empty */
1203 /*-------------------------------------------------------------*/
1204 /* Start transfer */
1205 iBufSize = req->req.length - req->req.actual;
1207 result = _nbu2ss_epn_in_data(udc, ep, req, iBufSize);
1208 else if (req->req.length == 0)
1209 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1214 /*-------------------------------------------------------------------------*/
1215 static int _nbu2ss_start_transfer(
1216 struct nbu2ss_udc *udc,
1217 struct nbu2ss_ep *ep,
1218 struct nbu2ss_req *req,
1223 req->dma_flag = FALSE;
1226 if (req->req.length == 0) {
1229 if ((req->req.length % ep->ep.maxpacket) == 0)
1230 req->zero = req->req.zero;
1235 if (ep->epnum == 0) {
1237 switch (udc->ep0state) {
1238 case EP0_IN_DATA_PHASE:
1239 nret = _nbu2ss_ep0_in_transfer(udc, req);
1242 case EP0_OUT_DATA_PHASE:
1243 nret = _nbu2ss_ep0_out_transfer(udc, req);
1246 case EP0_IN_STATUS_PHASE:
1247 nret = EP0_send_NULL(udc, TRUE);
1256 if (ep->direct == USB_DIR_OUT) {
1259 nret = _nbu2ss_epn_out_transfer(udc, ep, req);
1262 nret = _nbu2ss_epn_in_transfer(udc, ep, req);
1269 /*-------------------------------------------------------------------------*/
1270 static void _nbu2ss_restert_transfer(struct nbu2ss_ep *ep)
1274 struct nbu2ss_req *req;
1276 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1280 if (ep->epnum > 0) {
1281 length = _nbu2ss_readl(
1282 &ep->udc->p_regs->EP_REGS[ep->epnum - 1].EP_LEN_DCNT);
1284 length &= EPn_LDATA;
1285 if (length < ep->ep.maxpacket)
1289 _nbu2ss_start_transfer(ep->udc, ep, req, bflag);
1292 /*-------------------------------------------------------------------------*/
1293 /* Endpoint Toggle Reset */
1294 static void _nbu2ss_endpoint_toggle_reset(
1295 struct nbu2ss_udc *udc,
1301 if ((ep_adrs == 0) || (ep_adrs == 0x80))
1304 num = (ep_adrs & 0x7F) - 1;
1306 if (ep_adrs & USB_DIR_IN)
1309 data = EPn_BCLR | EPn_OPIDCLR;
1311 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
1314 /*-------------------------------------------------------------------------*/
1315 /* Endpoint STALL set */
1316 static void _nbu2ss_set_endpoint_stall(
1317 struct nbu2ss_udc *udc,
1323 struct nbu2ss_ep *ep;
1324 struct fc_regs *preg = udc->p_regs;
1326 if ((ep_adrs == 0) || (ep_adrs == 0x80)) {
1329 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_STL);
1332 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_STL);
1335 epnum = ep_adrs & USB_ENDPOINT_NUMBER_MASK;
1337 ep = &udc->ep[epnum];
1343 if (ep_adrs & USB_DIR_IN)
1344 data = EPn_BCLR | EPn_ISTL;
1346 data = EPn_OSTL_EN | EPn_OSTL;
1348 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
1351 ep->stalled = FALSE;
1352 if (ep_adrs & USB_DIR_IN) {
1353 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL
1357 _nbu2ss_readl(&preg->EP_REGS[num].EP_CONTROL);
1360 data |= EPn_OSTL_EN;
1362 _nbu2ss_writel(&preg->EP_REGS[num].EP_CONTROL
1366 ep->stalled = FALSE;
1369 _nbu2ss_restert_transfer(ep);
1375 /*-------------------------------------------------------------------------*/
1376 /* Device Descriptor */
1377 static struct usb_device_descriptor device_desc = {
1378 .bLength = sizeof(device_desc),
1379 .bDescriptorType = USB_DT_DEVICE,
1380 .bcdUSB = cpu_to_le16(0x0200),
1381 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
1382 .bDeviceSubClass = 0x00,
1383 .bDeviceProtocol = 0x00,
1384 .bMaxPacketSize0 = 64,
1385 .idVendor = cpu_to_le16(0x0409),
1386 .idProduct = cpu_to_le16(0xfff0),
1387 .bcdDevice = 0xffff,
1388 .iManufacturer = 0x00,
1390 .iSerialNumber = 0x00,
1391 .bNumConfigurations = 0x01,
1394 /*-------------------------------------------------------------------------*/
1395 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *udc, u32 mode)
1399 if (mode > MAX_TEST_MODE_NUM)
1402 dev_info(udc->dev, "SET FEATURE : test mode = %d\n", mode);
1404 data = _nbu2ss_readl(&udc->p_regs->USB_CONTROL);
1405 data &= ~TEST_FORCE_ENABLE;
1406 data |= mode << TEST_MODE_SHIFT;
1408 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, data);
1409 _nbu2ss_bitset(&udc->p_regs->TEST_CONTROL, CS_TESTMODEEN);
1412 /*-------------------------------------------------------------------------*/
1413 static int _nbu2ss_set_feature_device(
1414 struct nbu2ss_udc *udc,
1419 int result = -EOPNOTSUPP;
1422 case USB_DEVICE_REMOTE_WAKEUP:
1423 if (wIndex == 0x0000) {
1424 udc->remote_wakeup = U2F_ENABLE;
1429 case USB_DEVICE_TEST_MODE:
1431 if (wIndex <= MAX_TEST_MODE_NUM)
1442 /*-------------------------------------------------------------------------*/
1443 static int _nbu2ss_get_ep_stall(struct nbu2ss_udc *udc, u8 ep_adrs)
1446 u32 data = 0, bit_data;
1447 struct fc_regs *preg = udc->p_regs;
1449 epnum = ep_adrs & ~USB_ENDPOINT_DIR_MASK;
1451 data = _nbu2ss_readl(&preg->EP0_CONTROL);
1455 data = _nbu2ss_readl(&preg->EP_REGS[epnum - 1].EP_CONTROL);
1456 if ((data & EPn_EN) == 0)
1459 if (ep_adrs & USB_ENDPOINT_DIR_MASK)
1460 bit_data = EPn_ISTL;
1462 bit_data = EPn_OSTL;
1465 if ((data & bit_data) == 0)
1470 /*-------------------------------------------------------------------------*/
1471 static inline int _nbu2ss_req_feature(struct nbu2ss_udc *udc, bool bset)
1473 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1474 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1475 u16 selector = udc->ctrl.wValue;
1476 u16 wIndex = udc->ctrl.wIndex;
1478 int result = -EOPNOTSUPP;
1480 if ((udc->ctrl.wLength != 0x0000) ||
1481 (direction != USB_DIR_OUT)) {
1485 switch (recipient) {
1486 case USB_RECIP_DEVICE:
1489 _nbu2ss_set_feature_device(udc, selector, wIndex);
1492 case USB_RECIP_ENDPOINT:
1493 if (0x0000 == (wIndex & 0xFF70)) {
1494 if (selector == USB_ENDPOINT_HALT) {
1495 ep_adrs = wIndex & 0xFF;
1497 _nbu2ss_endpoint_toggle_reset(
1501 _nbu2ss_set_endpoint_stall(
1502 udc, ep_adrs, bset);
1514 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1519 /*-------------------------------------------------------------------------*/
1520 static inline enum usb_device_speed _nbu2ss_get_speed(struct nbu2ss_udc *udc)
1523 enum usb_device_speed speed = USB_SPEED_FULL;
1525 data = _nbu2ss_readl(&udc->p_regs->USB_STATUS);
1526 if (data & HIGH_SPEED)
1527 speed = USB_SPEED_HIGH;
1532 /*-------------------------------------------------------------------------*/
1533 static void _nbu2ss_epn_set_stall(
1534 struct nbu2ss_udc *udc,
1535 struct nbu2ss_ep *ep
1542 struct fc_regs *preg = udc->p_regs;
1544 if (ep->direct == USB_DIR_IN) {
1546 ; limit_cnt < IN_DATA_EMPTY_COUNT
1548 regdata = _nbu2ss_readl(
1549 &preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1551 if ((regdata & EPn_IN_DATA) == 0)
1558 ep_adrs = ep->epnum | ep->direct;
1559 _nbu2ss_set_endpoint_stall(udc, ep_adrs, 1);
1562 /*-------------------------------------------------------------------------*/
1563 static int std_req_get_status(struct nbu2ss_udc *udc)
1566 u16 status_data = 0;
1567 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1568 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1570 int result = -EINVAL;
1572 if ((udc->ctrl.wValue != 0x0000) || (direction != USB_DIR_IN))
1575 length = min_t(u16, udc->ctrl.wLength, sizeof(status_data));
1577 switch (recipient) {
1578 case USB_RECIP_DEVICE:
1579 if (udc->ctrl.wIndex == 0x0000) {
1580 if (udc->gadget.is_selfpowered)
1581 status_data |= (1 << USB_DEVICE_SELF_POWERED);
1583 if (udc->remote_wakeup)
1584 status_data |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1590 case USB_RECIP_ENDPOINT:
1591 if (0x0000 == (udc->ctrl.wIndex & 0xFF70)) {
1592 ep_adrs = (u8)(udc->ctrl.wIndex & 0xFF);
1593 result = _nbu2ss_get_ep_stall(udc, ep_adrs);
1596 status_data |= (1 << USB_ENDPOINT_HALT);
1605 memcpy(udc->ep0_buf, &status_data, length);
1606 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, length);
1607 _nbu2ss_ep0_in_transfer(udc, &udc->ep0_req);
1610 dev_err(udc->dev, " Error GET_STATUS\n");
1616 /*-------------------------------------------------------------------------*/
1617 static int std_req_clear_feature(struct nbu2ss_udc *udc)
1619 return _nbu2ss_req_feature(udc, FALSE);
1622 /*-------------------------------------------------------------------------*/
1623 static int std_req_set_feature(struct nbu2ss_udc *udc)
1625 return _nbu2ss_req_feature(udc, TRUE);
1628 /*-------------------------------------------------------------------------*/
1629 static int std_req_set_address(struct nbu2ss_udc *udc)
1632 u32 wValue = udc->ctrl.wValue;
1634 if ((udc->ctrl.bRequestType != 0x00) ||
1635 (udc->ctrl.wIndex != 0x0000) ||
1636 (udc->ctrl.wLength != 0x0000)) {
1640 if (wValue != (wValue & 0x007F))
1643 wValue <<= USB_ADRS_SHIFT;
1645 _nbu2ss_writel(&udc->p_regs->USB_ADDRESS, wValue);
1646 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1651 /*-------------------------------------------------------------------------*/
1652 static int std_req_set_configuration(struct nbu2ss_udc *udc)
1654 u32 ConfigValue = (u32)(udc->ctrl.wValue & 0x00ff);
1656 if ((udc->ctrl.wIndex != 0x0000) ||
1657 (udc->ctrl.wLength != 0x0000) ||
1658 (udc->ctrl.bRequestType != 0x00)) {
1662 udc->curr_config = ConfigValue;
1664 if (ConfigValue > 0) {
1665 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, CONF);
1666 udc->devstate = USB_STATE_CONFIGURED;
1669 _nbu2ss_bitclr(&udc->p_regs->USB_CONTROL, CONF);
1670 udc->devstate = USB_STATE_ADDRESS;
1676 /*-------------------------------------------------------------------------*/
1677 static inline void _nbu2ss_read_request_data(struct nbu2ss_udc *udc, u32 *pdata)
1679 if ((!udc) && (!pdata))
1682 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA0);
1684 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA1);
1687 /*-------------------------------------------------------------------------*/
1688 static inline int _nbu2ss_decode_request(struct nbu2ss_udc *udc)
1690 bool bcall_back = TRUE;
1692 struct usb_ctrlrequest *p_ctrl;
1694 p_ctrl = &udc->ctrl;
1695 _nbu2ss_read_request_data(udc, (u32 *)p_ctrl);
1697 /* ep0 state control */
1698 if (p_ctrl->wLength == 0) {
1699 udc->ep0state = EP0_IN_STATUS_PHASE;
1702 if (p_ctrl->bRequestType & USB_DIR_IN)
1703 udc->ep0state = EP0_IN_DATA_PHASE;
1705 udc->ep0state = EP0_OUT_DATA_PHASE;
1708 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1709 switch (p_ctrl->bRequest) {
1710 case USB_REQ_GET_STATUS:
1711 nret = std_req_get_status(udc);
1715 case USB_REQ_CLEAR_FEATURE:
1716 nret = std_req_clear_feature(udc);
1720 case USB_REQ_SET_FEATURE:
1721 nret = std_req_set_feature(udc);
1725 case USB_REQ_SET_ADDRESS:
1726 nret = std_req_set_address(udc);
1730 case USB_REQ_SET_CONFIGURATION:
1731 nret = std_req_set_configuration(udc);
1740 if (udc->ep0state == EP0_IN_STATUS_PHASE) {
1742 /*--------------------------------------*/
1744 nret = EP0_send_NULL(udc, TRUE);
1749 spin_unlock(&udc->lock);
1750 nret = udc->driver->setup(&udc->gadget, &udc->ctrl);
1751 spin_lock(&udc->lock);
1755 udc->ep0state = EP0_IDLE;
1760 /*-------------------------------------------------------------------------*/
1761 static inline int _nbu2ss_ep0_in_data_stage(struct nbu2ss_udc *udc)
1764 struct nbu2ss_req *req;
1765 struct nbu2ss_ep *ep = &udc->ep[0];
1767 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1769 req = &udc->ep0_req;
1771 req->req.actual += req->div_len;
1774 nret = _nbu2ss_ep0_in_transfer(udc, req);
1776 udc->ep0state = EP0_OUT_STATUS_PAHSE;
1777 EP0_receive_NULL(udc, TRUE);
1783 /*-------------------------------------------------------------------------*/
1784 static inline int _nbu2ss_ep0_out_data_stage(struct nbu2ss_udc *udc)
1787 struct nbu2ss_req *req;
1788 struct nbu2ss_ep *ep = &udc->ep[0];
1790 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1792 req = &udc->ep0_req;
1794 nret = _nbu2ss_ep0_out_transfer(udc, req);
1796 udc->ep0state = EP0_IN_STATUS_PHASE;
1797 EP0_send_NULL(udc, TRUE);
1799 } else if (nret < 0) {
1800 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, EP0_BCLR);
1801 req->req.status = nret;
1807 /*-------------------------------------------------------------------------*/
1808 static inline int _nbu2ss_ep0_status_stage(struct nbu2ss_udc *udc)
1810 struct nbu2ss_req *req;
1811 struct nbu2ss_ep *ep = &udc->ep[0];
1813 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1815 req = &udc->ep0_req;
1816 if (req->req.complete)
1817 req->req.complete(&ep->ep, &req->req);
1820 if (req->req.complete)
1821 _nbu2ss_ep_done(ep, req, 0);
1824 udc->ep0state = EP0_IDLE;
1829 /*-------------------------------------------------------------------------*/
1830 static inline void _nbu2ss_ep0_int(struct nbu2ss_udc *udc)
1837 status = _nbu2ss_readl(&udc->p_regs->EP0_STATUS);
1838 intr = status & EP0_STATUS_RW_BIT;
1839 _nbu2ss_writel(&udc->p_regs->EP0_STATUS, ~intr);
1841 status &= (SETUP_INT | EP0_IN_INT | EP0_OUT_INT
1842 | STG_END_INT | EP0_OUT_NULL_INT);
1845 dev_info(udc->dev, "%s Not Decode Interrupt\n", __func__);
1846 dev_info(udc->dev, "EP0_STATUS = 0x%08x\n", intr);
1850 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1851 udc->gadget.speed = _nbu2ss_get_speed(udc);
1853 for (i = 0; i < EP0_END_XFER; i++) {
1854 switch (udc->ep0state) {
1856 if (status & SETUP_INT) {
1858 nret = _nbu2ss_decode_request(udc);
1862 case EP0_IN_DATA_PHASE:
1863 if (status & EP0_IN_INT) {
1864 status &= ~EP0_IN_INT;
1865 nret = _nbu2ss_ep0_in_data_stage(udc);
1869 case EP0_OUT_DATA_PHASE:
1870 if (status & EP0_OUT_INT) {
1871 status &= ~EP0_OUT_INT;
1872 nret = _nbu2ss_ep0_out_data_stage(udc);
1876 case EP0_IN_STATUS_PHASE:
1877 if ((status & STG_END_INT) || (status & SETUP_INT)) {
1878 status &= ~(STG_END_INT | EP0_IN_INT);
1879 nret = _nbu2ss_ep0_status_stage(udc);
1883 case EP0_OUT_STATUS_PAHSE:
1884 if ((status & STG_END_INT) || (status & SETUP_INT) ||
1885 (status & EP0_OUT_NULL_INT)) {
1886 status &= ~(STG_END_INT
1888 | EP0_OUT_NULL_INT);
1890 nret = _nbu2ss_ep0_status_stage(udc);
1906 _nbu2ss_set_endpoint_stall(udc, 0, TRUE);
1910 /*-------------------------------------------------------------------------*/
1911 static void _nbu2ss_ep_done(
1912 struct nbu2ss_ep *ep,
1913 struct nbu2ss_req *req,
1916 struct nbu2ss_udc *udc = ep->udc;
1918 list_del_init(&req->queue);
1920 if (status == -ECONNRESET)
1921 _nbu2ss_fifo_flush(udc, ep);
1923 if (likely(req->req.status == -EINPROGRESS))
1924 req->req.status = status;
1927 _nbu2ss_epn_set_stall(udc, ep);
1929 if (!list_empty(&ep->queue))
1930 _nbu2ss_restert_transfer(ep);
1934 if ((ep->direct == USB_DIR_OUT) && (ep->epnum > 0) &&
1935 (req->req.dma != 0))
1936 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_OUT);
1939 spin_unlock(&udc->lock);
1940 req->req.complete(&ep->ep, &req->req);
1941 spin_lock(&udc->lock);
1944 /*-------------------------------------------------------------------------*/
1945 static inline void _nbu2ss_epn_in_int(
1946 struct nbu2ss_udc *udc,
1947 struct nbu2ss_ep *ep,
1948 struct nbu2ss_req *req)
1953 struct fc_regs *preg = udc->p_regs;
1956 return; /* DMA is forwarded */
1958 req->req.actual += req->div_len;
1961 if (req->req.actual != req->req.length) {
1962 /*---------------------------------------------------------*/
1963 /* remainder of data */
1964 result = _nbu2ss_epn_in_transfer(udc, ep, req);
1967 if (req->zero && ((req->req.actual % ep->ep.maxpacket) == 0)) {
1969 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1971 if ((status & EPn_IN_FULL) == 0) {
1972 /*-----------------------------------------*/
1973 /* 0 Length Packet */
1975 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1982 /*---------------------------------------------------------*/
1984 _nbu2ss_ep_done(ep, req, result);
1988 /*-------------------------------------------------------------------------*/
1989 static inline void _nbu2ss_epn_out_int(
1990 struct nbu2ss_udc *udc,
1991 struct nbu2ss_ep *ep,
1992 struct nbu2ss_req *req)
1996 result = _nbu2ss_epn_out_transfer(udc, ep, req);
1998 _nbu2ss_ep_done(ep, req, result);
2001 /*-------------------------------------------------------------------------*/
2002 static inline void _nbu2ss_epn_in_dma_int(
2003 struct nbu2ss_udc *udc,
2004 struct nbu2ss_ep *ep,
2005 struct nbu2ss_req *req)
2009 struct usb_request *preq;
2016 preq->actual += req->div_len;
2018 req->dma_flag = FALSE;
2021 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_IN);
2024 if (preq->actual != preq->length) {
2025 _nbu2ss_epn_in_transfer(udc, ep, req);
2027 mpkt = ep->ep.maxpacket;
2028 size = preq->actual % mpkt;
2030 if (((preq->actual & 0x03) == 0) && (size < mpkt))
2031 _nbu2ss_ep_in_end(udc, ep->epnum, 0, 0);
2033 _nbu2ss_epn_in_int(udc, ep, req);
2038 /*-------------------------------------------------------------------------*/
2039 static inline void _nbu2ss_epn_out_dma_int(
2040 struct nbu2ss_udc *udc,
2041 struct nbu2ss_ep *ep,
2042 struct nbu2ss_req *req)
2046 u32 dmacnt, ep_dmacnt;
2048 struct fc_regs *preg = udc->p_regs;
2050 num = ep->epnum - 1;
2052 if (req->req.actual == req->req.length) {
2053 if ((req->req.length % ep->ep.maxpacket) && !req->zero) {
2055 req->dma_flag = FALSE;
2056 _nbu2ss_ep_done(ep, req, 0);
2061 ep_dmacnt = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT)
2065 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2066 dmacnt = _nbu2ss_readl(&preg->EP_DCR[num].EP_DCR1)
2069 if (ep_dmacnt == dmacnt)
2073 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPn_REQEN);
2076 mpkt = ep->ep.maxpacket;
2077 if ((req->div_len % mpkt) == 0)
2078 req->div_len -= mpkt * dmacnt;
2081 if ((req->req.actual % ep->ep.maxpacket) > 0) {
2082 if (req->req.actual == req->div_len) {
2084 req->dma_flag = FALSE;
2085 _nbu2ss_ep_done(ep, req, 0);
2090 req->req.actual += req->div_len;
2092 req->dma_flag = FALSE;
2094 _nbu2ss_epn_out_int(udc, ep, req);
2097 /*-------------------------------------------------------------------------*/
2098 static inline void _nbu2ss_epn_int(struct nbu2ss_udc *udc, u32 epnum)
2103 struct nbu2ss_req *req;
2104 struct nbu2ss_ep *ep = &udc->ep[epnum];
2108 /* Interrupt Status */
2109 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
2111 /* Interrupt Clear */
2112 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_STATUS, ~status);
2114 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
2116 /* pr_warn("=== %s(%d) req == NULL\n", __func__, epnum); */
2120 if (status & EPn_OUT_END_INT) {
2121 status &= ~EPn_OUT_INT;
2122 _nbu2ss_epn_out_dma_int(udc, ep, req);
2125 if (status & EPn_OUT_INT)
2126 _nbu2ss_epn_out_int(udc, ep, req);
2128 if (status & EPn_IN_END_INT) {
2129 status &= ~EPn_IN_INT;
2130 _nbu2ss_epn_in_dma_int(udc, ep, req);
2133 if (status & EPn_IN_INT)
2134 _nbu2ss_epn_in_int(udc, ep, req);
2137 /*-------------------------------------------------------------------------*/
2138 static inline void _nbu2ss_ep_int(struct nbu2ss_udc *udc, u32 epnum)
2141 _nbu2ss_ep0_int(udc);
2143 _nbu2ss_epn_int(udc, epnum);
2146 /*-------------------------------------------------------------------------*/
2147 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *udc)
2149 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, (EP0_AUTO | EP0_BCLR));
2150 _nbu2ss_writel(&udc->p_regs->EP0_INT_ENA, EP0_INT_EN_BIT);
2153 /*-------------------------------------------------------------------------*/
2154 static int _nbu2ss_nuke(struct nbu2ss_udc *udc,
2155 struct nbu2ss_ep *ep,
2158 struct nbu2ss_req *req;
2160 /* Endpoint Disable */
2161 _nbu2ss_epn_exit(udc, ep);
2164 _nbu2ss_ep_dma_exit(udc, ep);
2166 if (list_empty(&ep->queue))
2169 /* called with irqs blocked */
2170 list_for_each_entry(req, &ep->queue, queue) {
2171 _nbu2ss_ep_done(ep, req, status);
2177 /*-------------------------------------------------------------------------*/
2178 static void _nbu2ss_quiesce(struct nbu2ss_udc *udc)
2180 struct nbu2ss_ep *ep;
2182 udc->gadget.speed = USB_SPEED_UNKNOWN;
2184 _nbu2ss_nuke(udc, &udc->ep[0], -ESHUTDOWN);
2187 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2188 _nbu2ss_nuke(udc, ep, -ESHUTDOWN);
2192 /*-------------------------------------------------------------------------*/
2193 static int _nbu2ss_pullup(struct nbu2ss_udc *udc, int is_on)
2197 if (udc->vbus_active == 0)
2203 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL)
2204 | PUE2) & ~(u32)CONNECTB;
2206 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2211 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL) | CONNECTB)
2214 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2215 udc->gadget.speed = USB_SPEED_UNKNOWN;
2221 /*-------------------------------------------------------------------------*/
2222 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
2224 struct fc_regs *p = udc->p_regs;
2226 if (udc->vbus_active == 0)
2229 if (ep->epnum == 0) {
2231 _nbu2ss_bitset(&p->EP0_CONTROL, EP0_BCLR);
2235 _nbu2ss_ep_dma_abort(udc, ep);
2236 _nbu2ss_bitset(&p->EP_REGS[ep->epnum - 1].EP_CONTROL, EPn_BCLR);
2240 /*-------------------------------------------------------------------------*/
2241 static int _nbu2ss_enable_controller(struct nbu2ss_udc *udc)
2245 if (udc->udc_enabled)
2249 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2250 udelay(EPC_RST_DISABLE_TIME); /* 1us wait */
2252 _nbu2ss_bitclr(&udc->p_regs->EPCTR, DIRPD);
2253 mdelay(EPC_DIRPD_DISABLE_TIME); /* 1ms wait */
2255 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2257 _nbu2ss_writel(&udc->p_regs->AHBSCTR, WAIT_MODE);
2259 _nbu2ss_writel(&udc->p_regs->AHBMCTR,
2260 HBUSREQ_MODE | HTRANS_MODE | WBURST_TYPE);
2262 while (!(_nbu2ss_readl(&udc->p_regs->EPCTR) & PLL_LOCK)) {
2264 udelay(1); /* 1us wait */
2265 if (waitcnt == EPC_PLL_LOCK_COUNT) {
2266 dev_err(udc->dev, "*** Reset Cancel failed\n");
2271 _nbu2ss_bitset(&udc->p_regs->UTMI_CHARACTER_1, USB_SQUSET);
2273 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, (INT_SEL | SOF_RCV));
2276 _nbu2ss_ep0_enable(udc);
2278 /* USB Interrupt Enable */
2279 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, USB_INT_EN_BIT);
2281 udc->udc_enabled = TRUE;
2286 /*-------------------------------------------------------------------------*/
2287 static void _nbu2ss_reset_controller(struct nbu2ss_udc *udc)
2289 _nbu2ss_bitset(&udc->p_regs->EPCTR, EPC_RST);
2290 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2293 /*-------------------------------------------------------------------------*/
2294 static void _nbu2ss_disable_controller(struct nbu2ss_udc *udc)
2296 if (udc->udc_enabled) {
2297 udc->udc_enabled = FALSE;
2298 _nbu2ss_reset_controller(udc);
2299 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2303 /*-------------------------------------------------------------------------*/
2304 static inline void _nbu2ss_check_vbus(struct nbu2ss_udc *udc)
2310 mdelay(VBUS_CHATTERING_MDELAY); /* wait (ms) */
2313 reg_dt = gpio_get_value(VBUS_VALUE);
2315 udc->linux_suspended = 0;
2317 _nbu2ss_reset_controller(udc);
2318 dev_info(udc->dev, " ----- VBUS OFF\n");
2320 if (udc->vbus_active == 1) {
2322 udc->vbus_active = 0;
2323 if (udc->usb_suspended) {
2324 udc->usb_suspended = 0;
2325 /* _nbu2ss_reset_controller(udc); */
2327 udc->devstate = USB_STATE_NOTATTACHED;
2329 _nbu2ss_quiesce(udc);
2331 spin_unlock(&udc->lock);
2332 udc->driver->disconnect(&udc->gadget);
2333 spin_lock(&udc->lock);
2336 _nbu2ss_disable_controller(udc);
2339 mdelay(5); /* wait (5ms) */
2340 reg_dt = gpio_get_value(VBUS_VALUE);
2344 dev_info(udc->dev, " ----- VBUS ON\n");
2346 if (udc->linux_suspended)
2349 if (udc->vbus_active == 0) {
2351 udc->vbus_active = 1;
2352 udc->devstate = USB_STATE_POWERED;
2354 nret = _nbu2ss_enable_controller(udc);
2356 _nbu2ss_disable_controller(udc);
2357 udc->vbus_active = 0;
2361 _nbu2ss_pullup(udc, 1);
2363 #ifdef UDC_DEBUG_DUMP
2364 _nbu2ss_dump_register(udc);
2365 #endif /* UDC_DEBUG_DUMP */
2368 if (udc->devstate == USB_STATE_POWERED)
2369 _nbu2ss_pullup(udc, 1);
2374 /*-------------------------------------------------------------------------*/
2375 static inline void _nbu2ss_int_bus_reset(struct nbu2ss_udc *udc)
2377 udc->devstate = USB_STATE_DEFAULT;
2378 udc->remote_wakeup = 0;
2380 _nbu2ss_quiesce(udc);
2382 udc->ep0state = EP0_IDLE;
2385 /*-------------------------------------------------------------------------*/
2386 static inline void _nbu2ss_int_usb_resume(struct nbu2ss_udc *udc)
2388 if (udc->usb_suspended == 1) {
2389 udc->usb_suspended = 0;
2390 if (udc->driver && udc->driver->resume) {
2391 spin_unlock(&udc->lock);
2392 udc->driver->resume(&udc->gadget);
2393 spin_lock(&udc->lock);
2398 /*-------------------------------------------------------------------------*/
2399 static inline void _nbu2ss_int_usb_suspend(struct nbu2ss_udc *udc)
2403 if (udc->usb_suspended == 0) {
2404 reg_dt = gpio_get_value(VBUS_VALUE);
2409 udc->usb_suspended = 1;
2410 if (udc->driver && udc->driver->suspend) {
2411 spin_unlock(&udc->lock);
2412 udc->driver->suspend(&udc->gadget);
2413 spin_lock(&udc->lock);
2416 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, SUSPEND);
2420 /*-------------------------------------------------------------------------*/
2421 /* VBUS (GPIO153) Interrupt */
2422 static irqreturn_t _nbu2ss_vbus_irq(int irq, void *_udc)
2424 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2426 spin_lock(&udc->lock);
2427 _nbu2ss_check_vbus(udc);
2428 spin_unlock(&udc->lock);
2433 /*-------------------------------------------------------------------------*/
2434 /* Interrupt (udc) */
2435 static irqreturn_t _nbu2ss_udc_irq(int irq, void *_udc)
2437 u8 suspend_flag = 0;
2441 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2442 struct fc_regs *preg = udc->p_regs;
2444 if (gpio_get_value(VBUS_VALUE) == 0) {
2445 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2446 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2450 spin_lock(&udc->lock);
2453 if (gpio_get_value(VBUS_VALUE) == 0) {
2454 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2455 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2458 status = _nbu2ss_readl(&preg->USB_INT_STA);
2464 _nbu2ss_writel(&preg->USB_INT_STA, ~(status & USB_INT_STA_RW));
2466 if (status & USB_RST_INT) {
2468 _nbu2ss_int_bus_reset(udc);
2471 if (status & RSUM_INT) {
2473 _nbu2ss_int_usb_resume(udc);
2476 if (status & SPND_INT) {
2481 if (status & EPn_INT) {
2483 int_bit = status >> 8;
2485 for (epnum = 0; epnum < NUM_ENDPOINTS; epnum++) {
2487 _nbu2ss_ep_int(udc, epnum);
2498 _nbu2ss_int_usb_suspend(udc);
2500 spin_unlock(&udc->lock);
2505 /*-------------------------------------------------------------------------*/
2507 static int nbu2ss_ep_enable(
2509 const struct usb_endpoint_descriptor *desc)
2512 unsigned long flags;
2514 struct nbu2ss_ep *ep;
2515 struct nbu2ss_udc *udc;
2517 if ((!_ep) || (!desc)) {
2518 pr_err(" *** %s, bad param\n", __func__);
2522 ep = container_of(_ep, struct nbu2ss_ep, ep);
2523 if ((!ep) || (!ep->udc)) {
2524 pr_err(" *** %s, ep == NULL !!\n", __func__);
2528 ep_type = usb_endpoint_type(desc);
2529 if ((ep_type == USB_ENDPOINT_XFER_CONTROL) ||
2530 (ep_type == USB_ENDPOINT_XFER_ISOC)) {
2531 pr_err(" *** %s, bat bmAttributes\n", __func__);
2536 if (udc->vbus_active == 0)
2539 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
2540 dev_err(ep->udc->dev, " *** %s, udc !!\n", __func__);
2544 spin_lock_irqsave(&udc->lock, flags);
2547 ep->epnum = usb_endpoint_num(desc);
2548 ep->direct = desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2549 ep->ep_type = ep_type;
2552 ep->stalled = FALSE;
2554 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2557 _nbu2ss_ep_dma_init(udc, ep);
2559 /* Endpoint setting */
2560 _nbu2ss_ep_init(udc, ep);
2562 spin_unlock_irqrestore(&udc->lock, flags);
2567 /*-------------------------------------------------------------------------*/
2568 static int nbu2ss_ep_disable(struct usb_ep *_ep)
2570 struct nbu2ss_ep *ep;
2571 struct nbu2ss_udc *udc;
2572 unsigned long flags;
2575 pr_err(" *** %s, bad param\n", __func__);
2579 ep = container_of(_ep, struct nbu2ss_ep, ep);
2580 if ((!ep) || (!ep->udc)) {
2581 pr_err("udc: *** %s, ep == NULL !!\n", __func__);
2586 if (udc->vbus_active == 0)
2589 spin_lock_irqsave(&udc->lock, flags);
2590 _nbu2ss_nuke(udc, ep, -EINPROGRESS); /* dequeue request */
2591 spin_unlock_irqrestore(&udc->lock, flags);
2596 /*-------------------------------------------------------------------------*/
2597 static struct usb_request *nbu2ss_ep_alloc_request(
2601 struct nbu2ss_req *req;
2603 req = kzalloc(sizeof(*req), gfp_flags);
2608 req->req.dma = DMA_ADDR_INVALID;
2610 INIT_LIST_HEAD(&req->queue);
2615 /*-------------------------------------------------------------------------*/
2616 static void nbu2ss_ep_free_request(
2618 struct usb_request *_req)
2620 struct nbu2ss_req *req;
2623 req = container_of(_req, struct nbu2ss_req, req);
2629 /*-------------------------------------------------------------------------*/
2630 static int nbu2ss_ep_queue(
2632 struct usb_request *_req,
2635 struct nbu2ss_req *req;
2636 struct nbu2ss_ep *ep;
2637 struct nbu2ss_udc *udc;
2638 unsigned long flags;
2640 int result = -EINVAL;
2642 /* catch various bogus parameters */
2643 if ((!_ep) || (!_req)) {
2645 pr_err("udc: %s --- _ep == NULL\n", __func__);
2648 pr_err("udc: %s --- _req == NULL\n", __func__);
2653 req = container_of(_req, struct nbu2ss_req, req);
2654 if (unlikely(!_req->complete || !_req->buf || !list_empty(&req->queue))) {
2655 if (!_req->complete)
2656 pr_err("udc: %s --- !_req->complete\n", __func__);
2659 pr_err("udc:%s --- !_req->buf\n", __func__);
2661 if (!list_empty(&req->queue))
2662 pr_err("%s --- !list_empty(&req->queue)\n", __func__);
2667 ep = container_of(_ep, struct nbu2ss_ep, ep);
2670 if (udc->vbus_active == 0) {
2671 dev_info(udc->dev, "Can't ep_queue (VBUS OFF)\n");
2675 if (unlikely(!udc->driver)) {
2676 dev_err(udc->dev, "%s, bogus device state %p\n", __func__,
2681 spin_lock_irqsave(&udc->lock, flags);
2684 if ((uintptr_t)req->req.buf & 0x3)
2685 req->unaligned = TRUE;
2687 req->unaligned = FALSE;
2689 if (req->unaligned) {
2691 ep->virt_buf = (u8 *)dma_alloc_coherent(
2693 &ep->phys_buf, GFP_ATOMIC | GFP_DMA);
2694 if (ep->epnum > 0) {
2695 if (ep->direct == USB_DIR_IN)
2696 memcpy(ep->virt_buf, req->req.buf,
2701 if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT) &&
2702 (req->req.dma != 0))
2703 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_OUT);
2706 _req->status = -EINPROGRESS;
2709 bflag = list_empty(&ep->queue);
2710 list_add_tail(&req->queue, &ep->queue);
2712 if (bflag && !ep->stalled) {
2713 result = _nbu2ss_start_transfer(udc, ep, req, FALSE);
2715 dev_err(udc->dev, " *** %s, result = %d\n", __func__,
2717 list_del(&req->queue);
2718 } else if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT)) {
2720 if (req->req.length < 4 &&
2721 req->req.length == req->req.actual)
2723 if (req->req.length == req->req.actual)
2725 _nbu2ss_ep_done(ep, req, result);
2729 spin_unlock_irqrestore(&udc->lock, flags);
2734 /*-------------------------------------------------------------------------*/
2735 static int nbu2ss_ep_dequeue(
2737 struct usb_request *_req)
2739 struct nbu2ss_req *req;
2740 struct nbu2ss_ep *ep;
2741 struct nbu2ss_udc *udc;
2742 unsigned long flags;
2744 /* catch various bogus parameters */
2745 if ((!_ep) || (!_req)) {
2746 /* pr_err("%s, bad param(1)\n", __func__); */
2750 ep = container_of(_ep, struct nbu2ss_ep, ep);
2752 pr_err("%s, ep == NULL !!\n", __func__);
2760 spin_lock_irqsave(&udc->lock, flags);
2762 /* make sure it's actually queued on this endpoint */
2763 list_for_each_entry(req, &ep->queue, queue) {
2764 if (&req->req == _req)
2767 if (&req->req != _req) {
2768 spin_unlock_irqrestore(&udc->lock, flags);
2769 pr_debug("%s no queue(EINVAL)\n", __func__);
2773 _nbu2ss_ep_done(ep, req, -ECONNRESET);
2775 spin_unlock_irqrestore(&udc->lock, flags);
2780 /*-------------------------------------------------------------------------*/
2781 static int nbu2ss_ep_set_halt(struct usb_ep *_ep, int value)
2784 unsigned long flags;
2786 struct nbu2ss_ep *ep;
2787 struct nbu2ss_udc *udc;
2790 pr_err("%s, bad param\n", __func__);
2794 ep = container_of(_ep, struct nbu2ss_ep, ep);
2796 pr_err("%s, bad ep\n", __func__);
2802 dev_err(ep->udc->dev, " *** %s, bad udc\n", __func__);
2806 spin_lock_irqsave(&udc->lock, flags);
2808 ep_adrs = ep->epnum | ep->direct;
2810 _nbu2ss_set_endpoint_stall(udc, ep_adrs, value);
2811 ep->stalled = FALSE;
2813 if (list_empty(&ep->queue))
2814 _nbu2ss_epn_set_stall(udc, ep);
2822 spin_unlock_irqrestore(&udc->lock, flags);
2827 static int nbu2ss_ep_set_wedge(struct usb_ep *_ep)
2829 return nbu2ss_ep_set_halt(_ep, 1);
2832 /*-------------------------------------------------------------------------*/
2833 static int nbu2ss_ep_fifo_status(struct usb_ep *_ep)
2836 struct nbu2ss_ep *ep;
2837 struct nbu2ss_udc *udc;
2838 unsigned long flags;
2839 struct fc_regs *preg;
2842 pr_err("%s, bad param\n", __func__);
2846 ep = container_of(_ep, struct nbu2ss_ep, ep);
2848 pr_err("%s, bad ep\n", __func__);
2854 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2860 data = gpio_get_value(VBUS_VALUE);
2864 spin_lock_irqsave(&udc->lock, flags);
2866 if (ep->epnum == 0) {
2867 data = _nbu2ss_readl(&preg->EP0_LENGTH) & EP0_LDATA;
2870 data = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_LEN_DCNT)
2874 spin_unlock_irqrestore(&udc->lock, flags);
2879 /*-------------------------------------------------------------------------*/
2880 static void nbu2ss_ep_fifo_flush(struct usb_ep *_ep)
2883 struct nbu2ss_ep *ep;
2884 struct nbu2ss_udc *udc;
2885 unsigned long flags;
2888 pr_err("udc: %s, bad param\n", __func__);
2892 ep = container_of(_ep, struct nbu2ss_ep, ep);
2894 pr_err("udc: %s, bad ep\n", __func__);
2900 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2904 data = gpio_get_value(VBUS_VALUE);
2908 spin_lock_irqsave(&udc->lock, flags);
2909 _nbu2ss_fifo_flush(udc, ep);
2910 spin_unlock_irqrestore(&udc->lock, flags);
2913 /*-------------------------------------------------------------------------*/
2914 static const struct usb_ep_ops nbu2ss_ep_ops = {
2915 .enable = nbu2ss_ep_enable,
2916 .disable = nbu2ss_ep_disable,
2918 .alloc_request = nbu2ss_ep_alloc_request,
2919 .free_request = nbu2ss_ep_free_request,
2921 .queue = nbu2ss_ep_queue,
2922 .dequeue = nbu2ss_ep_dequeue,
2924 .set_halt = nbu2ss_ep_set_halt,
2925 .set_wedge = nbu2ss_ep_set_wedge,
2927 .fifo_status = nbu2ss_ep_fifo_status,
2928 .fifo_flush = nbu2ss_ep_fifo_flush,
2931 /*-------------------------------------------------------------------------*/
2932 /* usb_gadget_ops */
2934 /*-------------------------------------------------------------------------*/
2935 static int nbu2ss_gad_get_frame(struct usb_gadget *pgadget)
2938 struct nbu2ss_udc *udc;
2941 pr_err("udc: %s, bad param\n", __func__);
2945 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2947 dev_err(&pgadget->dev, "%s, udc == NULL\n", __func__);
2951 data = gpio_get_value(VBUS_VALUE);
2955 return _nbu2ss_readl(&udc->p_regs->USB_ADDRESS) & FRAME;
2958 /*-------------------------------------------------------------------------*/
2959 static int nbu2ss_gad_wakeup(struct usb_gadget *pgadget)
2964 struct nbu2ss_udc *udc;
2967 pr_err("%s, bad param\n", __func__);
2971 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2973 dev_err(&pgadget->dev, "%s, udc == NULL\n", __func__);
2977 data = gpio_get_value(VBUS_VALUE);
2979 dev_warn(&pgadget->dev, "VBUS LEVEL = %d\n", data);
2983 _nbu2ss_bitset(&udc->p_regs->EPCTR, PLL_RESUME);
2985 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2986 data = _nbu2ss_readl(&udc->p_regs->EPCTR);
2988 if (data & PLL_LOCK)
2992 _nbu2ss_bitclr(&udc->p_regs->EPCTR, PLL_RESUME);
2997 /*-------------------------------------------------------------------------*/
2998 static int nbu2ss_gad_set_selfpowered(struct usb_gadget *pgadget,
3001 struct nbu2ss_udc *udc;
3002 unsigned long flags;
3005 pr_err("%s, bad param\n", __func__);
3009 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3011 spin_lock_irqsave(&udc->lock, flags);
3012 pgadget->is_selfpowered = (is_selfpowered != 0);
3013 spin_unlock_irqrestore(&udc->lock, flags);
3018 /*-------------------------------------------------------------------------*/
3019 static int nbu2ss_gad_vbus_session(struct usb_gadget *pgadget, int is_active)
3024 /*-------------------------------------------------------------------------*/
3025 static int nbu2ss_gad_vbus_draw(struct usb_gadget *pgadget, unsigned int mA)
3027 struct nbu2ss_udc *udc;
3028 unsigned long flags;
3031 pr_err("%s, bad param\n", __func__);
3035 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3037 spin_lock_irqsave(&udc->lock, flags);
3039 spin_unlock_irqrestore(&udc->lock, flags);
3044 /*-------------------------------------------------------------------------*/
3045 static int nbu2ss_gad_pullup(struct usb_gadget *pgadget, int is_on)
3047 struct nbu2ss_udc *udc;
3048 unsigned long flags;
3051 pr_err("%s, bad param\n", __func__);
3055 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3058 pr_warn("%s, Not Regist Driver\n", __func__);
3062 if (udc->vbus_active == 0)
3065 spin_lock_irqsave(&udc->lock, flags);
3066 _nbu2ss_pullup(udc, is_on);
3067 spin_unlock_irqrestore(&udc->lock, flags);
3072 /*-------------------------------------------------------------------------*/
3073 static int nbu2ss_gad_ioctl(
3074 struct usb_gadget *pgadget,
3076 unsigned long param)
3081 static const struct usb_gadget_ops nbu2ss_gadget_ops = {
3082 .get_frame = nbu2ss_gad_get_frame,
3083 .wakeup = nbu2ss_gad_wakeup,
3084 .set_selfpowered = nbu2ss_gad_set_selfpowered,
3085 .vbus_session = nbu2ss_gad_vbus_session,
3086 .vbus_draw = nbu2ss_gad_vbus_draw,
3087 .pullup = nbu2ss_gad_pullup,
3088 .ioctl = nbu2ss_gad_ioctl,
3091 static const struct {
3093 const struct usb_ep_caps caps;
3094 } ep_info[NUM_ENDPOINTS] = {
3095 #define EP_INFO(_name, _caps) \
3102 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
3104 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3106 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3107 EP_INFO("ep3in-int",
3108 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3110 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3112 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3114 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3116 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3117 EP_INFO("ep8in-int",
3118 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3120 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3122 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3124 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3126 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3127 EP_INFO("epdin-int",
3128 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3133 /*-------------------------------------------------------------------------*/
3134 static void nbu2ss_drv_ep_init(struct nbu2ss_udc *udc)
3138 INIT_LIST_HEAD(&udc->gadget.ep_list);
3139 udc->gadget.ep0 = &udc->ep[0].ep;
3141 for (i = 0; i < NUM_ENDPOINTS; i++) {
3142 struct nbu2ss_ep *ep = &udc->ep[i];
3147 ep->ep.driver_data = NULL;
3148 ep->ep.name = ep_info[i].name;
3149 ep->ep.caps = ep_info[i].caps;
3150 ep->ep.ops = &nbu2ss_ep_ops;
3152 usb_ep_set_maxpacket_limit(&ep->ep,
3153 i == 0 ? EP0_PACKETSIZE
3156 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
3157 INIT_LIST_HEAD(&ep->queue);
3160 list_del_init(&udc->ep[0].ep.ep_list);
3163 /*-------------------------------------------------------------------------*/
3164 /* platform_driver */
3165 static int nbu2ss_drv_contest_init(
3166 struct platform_device *pdev,
3167 struct nbu2ss_udc *udc)
3169 spin_lock_init(&udc->lock);
3170 udc->dev = &pdev->dev;
3172 udc->gadget.is_selfpowered = 1;
3173 udc->devstate = USB_STATE_NOTATTACHED;
3177 udc->pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3180 nbu2ss_drv_ep_init(udc);
3183 udc->gadget.ops = &nbu2ss_gadget_ops;
3184 udc->gadget.ep0 = &udc->ep[0].ep;
3185 udc->gadget.speed = USB_SPEED_UNKNOWN;
3186 udc->gadget.name = driver_name;
3187 /* udc->gadget.is_dualspeed = 1; */
3189 device_initialize(&udc->gadget.dev);
3191 dev_set_name(&udc->gadget.dev, "gadget");
3192 udc->gadget.dev.parent = &pdev->dev;
3193 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
3199 * probe - binds to the platform device
3201 static int nbu2ss_drv_probe(struct platform_device *pdev)
3203 int status = -ENODEV;
3204 struct nbu2ss_udc *udc;
3207 void __iomem *mmio_base;
3209 udc = &udc_controller;
3210 memset(udc, 0, sizeof(struct nbu2ss_udc));
3212 platform_set_drvdata(pdev, udc);
3214 /* require I/O memory and IRQ to be provided as resources */
3215 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3216 mmio_base = devm_ioremap_resource(&pdev->dev, r);
3217 if (IS_ERR(mmio_base))
3218 return PTR_ERR(mmio_base);
3220 irq = platform_get_irq(pdev, 0);
3222 dev_err(&pdev->dev, "failed to get IRQ\n");
3225 status = devm_request_irq(&pdev->dev, irq, _nbu2ss_udc_irq,
3226 0, driver_name, udc);
3229 udc->p_regs = (struct fc_regs *)mmio_base;
3231 /* USB Function Controller Interrupt */
3233 dev_err(udc->dev, "request_irq(USB_UDC_IRQ_1) failed\n");
3237 /* Driver Initialization */
3238 status = nbu2ss_drv_contest_init(pdev, udc);
3244 /* VBUS Interrupt */
3245 irq_set_irq_type(INT_VBUS, IRQ_TYPE_EDGE_BOTH);
3246 status = request_irq(INT_VBUS,
3247 _nbu2ss_vbus_irq, IRQF_SHARED, driver_name, udc);
3250 dev_err(udc->dev, "request_irq(INT_VBUS) failed\n");
3257 /*-------------------------------------------------------------------------*/
3258 static void nbu2ss_drv_shutdown(struct platform_device *pdev)
3260 struct nbu2ss_udc *udc;
3262 udc = platform_get_drvdata(pdev);
3266 _nbu2ss_disable_controller(udc);
3269 /*-------------------------------------------------------------------------*/
3270 static int nbu2ss_drv_remove(struct platform_device *pdev)
3272 struct nbu2ss_udc *udc;
3273 struct nbu2ss_ep *ep;
3276 udc = &udc_controller;
3278 for (i = 0; i < NUM_ENDPOINTS; i++) {
3281 dma_free_coherent(NULL, PAGE_SIZE, (void *)ep->virt_buf,
3285 /* Interrupt Handler - Release */
3286 free_irq(INT_VBUS, udc);
3291 /*-------------------------------------------------------------------------*/
3292 static int nbu2ss_drv_suspend(struct platform_device *pdev, pm_message_t state)
3294 struct nbu2ss_udc *udc;
3296 udc = platform_get_drvdata(pdev);
3300 if (udc->vbus_active) {
3301 udc->vbus_active = 0;
3302 udc->devstate = USB_STATE_NOTATTACHED;
3303 udc->linux_suspended = 1;
3305 if (udc->usb_suspended) {
3306 udc->usb_suspended = 0;
3307 _nbu2ss_reset_controller(udc);
3310 _nbu2ss_quiesce(udc);
3312 _nbu2ss_disable_controller(udc);
3317 /*-------------------------------------------------------------------------*/
3318 static int nbu2ss_drv_resume(struct platform_device *pdev)
3321 struct nbu2ss_udc *udc;
3323 udc = platform_get_drvdata(pdev);
3327 data = gpio_get_value(VBUS_VALUE);
3329 udc->vbus_active = 1;
3330 udc->devstate = USB_STATE_POWERED;
3331 _nbu2ss_enable_controller(udc);
3332 _nbu2ss_pullup(udc, 1);
3335 udc->linux_suspended = 0;
3340 static struct platform_driver udc_driver = {
3341 .probe = nbu2ss_drv_probe,
3342 .shutdown = nbu2ss_drv_shutdown,
3343 .remove = nbu2ss_drv_remove,
3344 .suspend = nbu2ss_drv_suspend,
3345 .resume = nbu2ss_drv_resume,
3347 .name = driver_name,
3351 module_platform_driver(udc_driver);
3353 MODULE_DESCRIPTION(DRIVER_DESC);
3354 MODULE_AUTHOR("Renesas Electronics Corporation");
3355 MODULE_LICENSE("GPL");