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 *pBuf, u32 length)
561 union usb_reg_access *pBuf32 = (union usb_reg_access *)pBuf;
563 /*------------------------------------------------------------*/
565 iWordLength = length / sizeof(u32);
567 /*------------------------------------------------------------*/
570 for (i = 0; i < iWordLength; i++) {
571 pBuf32->dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
574 nret = iWordLength * sizeof(u32);
580 /*-------------------------------------------------------------------------*/
581 /* Endpoint 0 OUT Transfer (PIO, OverBytes) */
582 static int EP0_out_OverBytes(struct nbu2ss_udc *udc, u8 *pBuf, u32 length)
586 union usb_reg_access Temp32;
587 union usb_reg_access *pBuf32 = (union usb_reg_access *)pBuf;
589 if ((length > 0) && (length < sizeof(u32))) {
590 Temp32.dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
591 for (i = 0 ; i < length ; i++)
592 pBuf32->byte.DATA[i] = Temp32.byte.DATA[i];
599 /*-------------------------------------------------------------------------*/
600 /* Endpoint 0 IN Transfer (PIO) */
601 static int EP0_in_PIO(struct nbu2ss_udc *udc, u8 *pBuf, u32 length)
604 u32 iMaxLength = EP0_PACKETSIZE;
606 u32 iWriteLength = 0;
607 union usb_reg_access *pBuf32 = (union usb_reg_access *)pBuf;
609 /*------------------------------------------------------------*/
610 /* Transfer Length */
611 if (iMaxLength < length)
612 iWordLength = iMaxLength / sizeof(u32);
614 iWordLength = length / sizeof(u32);
616 /*------------------------------------------------------------*/
618 for (i = 0; i < iWordLength; i++) {
619 _nbu2ss_writel(&udc->p_regs->EP0_WRITE, pBuf32->dw);
621 iWriteLength += sizeof(u32);
627 /*-------------------------------------------------------------------------*/
628 /* Endpoint 0 IN Transfer (PIO, OverBytes) */
629 static int EP0_in_OverBytes(struct nbu2ss_udc *udc, u8 *pBuf, u32 iRemainSize)
632 union usb_reg_access Temp32;
633 union usb_reg_access *pBuf32 = (union usb_reg_access *)pBuf;
635 if ((iRemainSize > 0) && (iRemainSize < sizeof(u32))) {
636 for (i = 0 ; i < iRemainSize ; i++)
637 Temp32.byte.DATA[i] = pBuf32->byte.DATA[i];
638 _nbu2ss_ep_in_end(udc, 0, Temp32.dw, iRemainSize);
646 /*-------------------------------------------------------------------------*/
647 /* Transfer NULL Packet (Epndoint 0) */
648 static int EP0_send_NULL(struct nbu2ss_udc *udc, bool pid_flag)
652 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
653 data &= ~(u32)EP0_INAK;
656 data |= (EP0_INAK_EN | EP0_PIDCLR | EP0_DEND);
658 data |= (EP0_INAK_EN | EP0_DEND);
660 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
665 /*-------------------------------------------------------------------------*/
666 /* Receive NULL Packet (Endpoint 0) */
667 static int EP0_receive_NULL(struct nbu2ss_udc *udc, bool pid_flag)
671 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
672 data &= ~(u32)EP0_ONAK;
677 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
682 /*-------------------------------------------------------------------------*/
683 static int _nbu2ss_ep0_in_transfer(
684 struct nbu2ss_udc *udc,
685 struct nbu2ss_req *req
688 u8 *pBuffer; /* IN Data Buffer */
693 /*-------------------------------------------------------------*/
694 /* End confirmation */
695 if (req->req.actual == req->req.length) {
696 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
699 EP0_send_NULL(udc, FALSE);
704 return 0; /* Transfer End */
707 /*-------------------------------------------------------------*/
709 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
711 data &= ~(u32)EP0_INAK;
712 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
714 iRemainSize = req->req.length - req->req.actual;
715 pBuffer = (u8 *)req->req.buf;
716 pBuffer += req->req.actual;
718 /*-------------------------------------------------------------*/
720 result = EP0_in_PIO(udc, pBuffer, iRemainSize);
722 req->div_len = result;
723 iRemainSize -= result;
725 if (iRemainSize == 0) {
726 EP0_send_NULL(udc, FALSE);
730 if ((iRemainSize < sizeof(u32)) && (result != EP0_PACKETSIZE)) {
732 result += EP0_in_OverBytes(udc, pBuffer, iRemainSize);
733 req->div_len = result;
739 /*-------------------------------------------------------------------------*/
740 static int _nbu2ss_ep0_out_transfer(
741 struct nbu2ss_udc *udc,
742 struct nbu2ss_req *req
751 /*-------------------------------------------------------------*/
752 /* Receive data confirmation */
753 iRecvLength = _nbu2ss_readl(&udc->p_regs->EP0_LENGTH) & EP0_LDATA;
754 if (iRecvLength != 0) {
757 iRemainSize = req->req.length - req->req.actual;
758 pBuffer = (u8 *)req->req.buf;
759 pBuffer += req->req.actual;
761 result = EP0_out_PIO(udc, pBuffer
762 , min(iRemainSize, iRecvLength));
766 req->req.actual += result;
767 iRecvLength -= result;
769 if ((iRecvLength > 0) && (iRecvLength < sizeof(u32))) {
771 iRemainSize -= result;
773 result = EP0_out_OverBytes(udc, pBuffer
774 , min(iRemainSize, iRecvLength));
775 req->req.actual += result;
781 /*-------------------------------------------------------------*/
782 /* End confirmation */
783 if (req->req.actual == req->req.length) {
784 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
787 EP0_receive_NULL(udc, FALSE);
792 return 0; /* Transfer End */
795 if ((req->req.actual % EP0_PACKETSIZE) != 0)
796 return 0; /* Short Packet Transfer End */
798 if (req->req.actual > req->req.length) {
799 dev_err(udc->dev, " *** Overrun Error\n");
804 iRemainSize = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
805 if (iRemainSize & EP0_ONAK) {
806 /*---------------------------------------------------*/
808 _nbu2ss_bitclr(&udc->p_regs->EP0_CONTROL, EP0_ONAK);
816 /*-------------------------------------------------------------------------*/
817 static int _nbu2ss_out_dma(
818 struct nbu2ss_udc *udc,
819 struct nbu2ss_req *req,
830 int result = -EINVAL;
831 struct fc_regs *preg = udc->p_regs;
834 return 1; /* DMA is forwarded */
836 req->dma_flag = TRUE;
837 pBuffer = req->req.dma;
838 pBuffer += req->req.actual;
841 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)pBuffer);
843 /* Number of transfer packets */
844 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPn_MPKT;
845 dmacnt = length / mpkt;
846 lmpkt = (length % mpkt) & ~(u32)0x03;
848 if (dmacnt > DMA_MAX_COUNT) {
849 dmacnt = DMA_MAX_COUNT;
851 } else if (lmpkt != 0) {
853 burst = 0; /* Burst OFF */
857 data = mpkt | (lmpkt << 16);
858 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
860 data = ((dmacnt & 0xff) << 16) | DCR1_EPn_DIR0 | DCR1_EPn_REQEN;
861 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
864 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, 0);
865 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_BURST_SET);
867 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT
869 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_BURST_SET);
871 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_DMA_EN);
873 result = length & ~(u32)0x03;
874 req->div_len = result;
879 /*-------------------------------------------------------------------------*/
880 static int _nbu2ss_epn_out_pio(
881 struct nbu2ss_udc *udc,
882 struct nbu2ss_ep *ep,
883 struct nbu2ss_req *req,
891 union usb_reg_access Temp32;
892 union usb_reg_access *pBuf32;
894 struct fc_regs *preg = udc->p_regs;
897 return 1; /* DMA is forwarded */
902 pBuffer = (u8 *)req->req.buf;
903 pBuf32 = (union usb_reg_access *)(pBuffer + req->req.actual);
905 iWordLength = length / sizeof(u32);
906 if (iWordLength > 0) {
907 /*---------------------------------------------------------*/
908 /* Copy of every four bytes */
909 for (i = 0; i < iWordLength; i++) {
911 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
914 result = iWordLength * sizeof(u32);
917 data = length - result;
919 /*---------------------------------------------------------*/
920 /* Copy of fraction byte */
921 Temp32.dw = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
922 for (i = 0 ; i < data ; i++)
923 pBuf32->byte.DATA[i] = Temp32.byte.DATA[i];
927 req->req.actual += result;
929 if ((req->req.actual == req->req.length) ||
930 ((req->req.actual % ep->ep.maxpacket) != 0)) {
937 /*-------------------------------------------------------------------------*/
938 static int _nbu2ss_epn_out_data(
939 struct nbu2ss_udc *udc,
940 struct nbu2ss_ep *ep,
941 struct nbu2ss_req *req,
954 iBufSize = min((req->req.length - req->req.actual), data_size);
956 if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
957 (iBufSize >= sizeof(u32))) {
958 nret = _nbu2ss_out_dma(udc, req, num, iBufSize);
960 iBufSize = min_t(u32, iBufSize, ep->ep.maxpacket);
961 nret = _nbu2ss_epn_out_pio(udc, ep, req, iBufSize);
967 /*-------------------------------------------------------------------------*/
968 static int _nbu2ss_epn_out_transfer(
969 struct nbu2ss_udc *udc,
970 struct nbu2ss_ep *ep,
971 struct nbu2ss_req *req
977 struct fc_regs *preg = udc->p_regs;
984 /*-------------------------------------------------------------*/
987 = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT) & EPn_LDATA;
989 if (iRecvLength != 0) {
990 result = _nbu2ss_epn_out_data(udc, ep, req, iRecvLength);
991 if (iRecvLength < ep->ep.maxpacket) {
992 if (iRecvLength == result) {
993 req->req.actual += result;
998 if ((req->req.actual == req->req.length) ||
999 ((req->req.actual % ep->ep.maxpacket) != 0)) {
1005 if ((req->req.actual % ep->ep.maxpacket) == 0) {
1013 if (req->req.actual > req->req.length) {
1014 dev_err(udc->dev, " Overrun Error\n");
1015 dev_err(udc->dev, " actual = %d, length = %d\n",
1016 req->req.actual, req->req.length);
1017 result = -EOVERFLOW;
1023 /*-------------------------------------------------------------------------*/
1024 static int _nbu2ss_in_dma(
1025 struct nbu2ss_udc *udc,
1026 struct nbu2ss_ep *ep,
1027 struct nbu2ss_req *req,
1033 u32 mpkt; /* MaxPacketSize */
1034 u32 lmpkt; /* Last Packet Data Size */
1035 u32 dmacnt; /* IN Data Size */
1038 int result = -EINVAL;
1039 struct fc_regs *preg = udc->p_regs;
1042 return 1; /* DMA is forwarded */
1045 if (req->req.actual == 0)
1046 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_IN);
1048 req->dma_flag = TRUE;
1050 /* MAX Packet Size */
1051 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPn_MPKT;
1053 if ((DMA_MAX_COUNT * mpkt) < length)
1054 iWriteLength = DMA_MAX_COUNT * mpkt;
1056 iWriteLength = length;
1058 /*------------------------------------------------------------*/
1059 /* Number of transmission packets */
1060 if (mpkt < iWriteLength) {
1061 dmacnt = iWriteLength / mpkt;
1062 lmpkt = (iWriteLength % mpkt) & ~(u32)0x3;
1066 lmpkt = mpkt & ~(u32)0x3;
1070 lmpkt = iWriteLength & ~(u32)0x3;
1073 /* Packet setting */
1074 data = mpkt | (lmpkt << 16);
1075 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
1077 /* Address setting */
1078 pBuffer = req->req.dma;
1079 pBuffer += req->req.actual;
1080 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)pBuffer);
1082 /* Packet and DMA setting */
1083 data = ((dmacnt & 0xff) << 16) | DCR1_EPn_REQEN;
1084 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
1086 /* Packet setting of EPC */
1087 data = dmacnt << 16;
1088 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, data);
1090 /*DMA setting of EPC */
1091 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_DMA_EN);
1093 result = iWriteLength & ~(u32)0x3;
1094 req->div_len = result;
1099 /*-------------------------------------------------------------------------*/
1100 static int _nbu2ss_epn_in_pio(
1101 struct nbu2ss_udc *udc,
1102 struct nbu2ss_ep *ep,
1103 struct nbu2ss_req *req,
1111 union usb_reg_access Temp32;
1112 union usb_reg_access *pBuf32 = NULL;
1114 struct fc_regs *preg = udc->p_regs;
1117 return 1; /* DMA is forwarded */
1120 pBuffer = (u8 *)req->req.buf;
1121 pBuf32 = (union usb_reg_access *)(pBuffer + req->req.actual);
1123 iWordLength = length / sizeof(u32);
1124 if (iWordLength > 0) {
1125 for (i = 0; i < iWordLength; i++) {
1127 &preg->EP_REGS[ep->epnum - 1].EP_WRITE
1133 result = iWordLength * sizeof(u32);
1137 if (result != ep->ep.maxpacket) {
1138 data = length - result;
1140 for (i = 0 ; i < data ; i++)
1141 Temp32.byte.DATA[i] = pBuf32->byte.DATA[i];
1143 _nbu2ss_ep_in_end(udc, ep->epnum, Temp32.dw, data);
1147 req->div_len = result;
1152 /*-------------------------------------------------------------------------*/
1153 static int _nbu2ss_epn_in_data(
1154 struct nbu2ss_udc *udc,
1155 struct nbu2ss_ep *ep,
1156 struct nbu2ss_req *req,
1166 num = ep->epnum - 1;
1168 if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
1169 (data_size >= sizeof(u32))) {
1170 nret = _nbu2ss_in_dma(udc, ep, req, num, data_size);
1172 data_size = min_t(u32, data_size, ep->ep.maxpacket);
1173 nret = _nbu2ss_epn_in_pio(udc, ep, req, data_size);
1179 /*-------------------------------------------------------------------------*/
1180 static int _nbu2ss_epn_in_transfer(
1181 struct nbu2ss_udc *udc,
1182 struct nbu2ss_ep *ep,
1183 struct nbu2ss_req *req
1194 num = ep->epnum - 1;
1196 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
1198 /*-------------------------------------------------------------*/
1199 /* State confirmation of FIFO */
1200 if (req->req.actual == 0) {
1201 if ((status & EPn_IN_EMPTY) == 0)
1202 return 1; /* Not Empty */
1205 if ((status & EPn_IN_FULL) != 0)
1206 return 1; /* Not Empty */
1209 /*-------------------------------------------------------------*/
1210 /* Start transfer */
1211 iBufSize = req->req.length - req->req.actual;
1213 result = _nbu2ss_epn_in_data(udc, ep, req, iBufSize);
1214 else if (req->req.length == 0)
1215 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1220 /*-------------------------------------------------------------------------*/
1221 static int _nbu2ss_start_transfer(
1222 struct nbu2ss_udc *udc,
1223 struct nbu2ss_ep *ep,
1224 struct nbu2ss_req *req,
1229 req->dma_flag = FALSE;
1232 if (req->req.length == 0) {
1235 if ((req->req.length % ep->ep.maxpacket) == 0)
1236 req->zero = req->req.zero;
1241 if (ep->epnum == 0) {
1243 switch (udc->ep0state) {
1244 case EP0_IN_DATA_PHASE:
1245 nret = _nbu2ss_ep0_in_transfer(udc, req);
1248 case EP0_OUT_DATA_PHASE:
1249 nret = _nbu2ss_ep0_out_transfer(udc, req);
1252 case EP0_IN_STATUS_PHASE:
1253 nret = EP0_send_NULL(udc, TRUE);
1262 if (ep->direct == USB_DIR_OUT) {
1265 nret = _nbu2ss_epn_out_transfer(udc, ep, req);
1268 nret = _nbu2ss_epn_in_transfer(udc, ep, req);
1275 /*-------------------------------------------------------------------------*/
1276 static void _nbu2ss_restert_transfer(struct nbu2ss_ep *ep)
1280 struct nbu2ss_req *req;
1282 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1286 if (ep->epnum > 0) {
1287 length = _nbu2ss_readl(
1288 &ep->udc->p_regs->EP_REGS[ep->epnum - 1].EP_LEN_DCNT);
1290 length &= EPn_LDATA;
1291 if (length < ep->ep.maxpacket)
1295 _nbu2ss_start_transfer(ep->udc, ep, req, bflag);
1298 /*-------------------------------------------------------------------------*/
1299 /* Endpoint Toggle Reset */
1300 static void _nbu2ss_endpoint_toggle_reset(
1301 struct nbu2ss_udc *udc,
1307 if ((ep_adrs == 0) || (ep_adrs == 0x80))
1310 num = (ep_adrs & 0x7F) - 1;
1312 if (ep_adrs & USB_DIR_IN)
1315 data = EPn_BCLR | EPn_OPIDCLR;
1317 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
1320 /*-------------------------------------------------------------------------*/
1321 /* Endpoint STALL set */
1322 static void _nbu2ss_set_endpoint_stall(
1323 struct nbu2ss_udc *udc,
1329 struct nbu2ss_ep *ep;
1330 struct fc_regs *preg = udc->p_regs;
1332 if ((ep_adrs == 0) || (ep_adrs == 0x80)) {
1335 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_STL);
1338 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_STL);
1341 epnum = ep_adrs & USB_ENDPOINT_NUMBER_MASK;
1343 ep = &udc->ep[epnum];
1349 if (ep_adrs & USB_DIR_IN)
1350 data = EPn_BCLR | EPn_ISTL;
1352 data = EPn_OSTL_EN | EPn_OSTL;
1354 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
1357 ep->stalled = FALSE;
1358 if (ep_adrs & USB_DIR_IN) {
1359 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL
1363 _nbu2ss_readl(&preg->EP_REGS[num].EP_CONTROL);
1366 data |= EPn_OSTL_EN;
1368 _nbu2ss_writel(&preg->EP_REGS[num].EP_CONTROL
1372 ep->stalled = FALSE;
1375 _nbu2ss_restert_transfer(ep);
1381 /*-------------------------------------------------------------------------*/
1382 /* Device Descriptor */
1383 static struct usb_device_descriptor device_desc = {
1384 .bLength = sizeof(device_desc),
1385 .bDescriptorType = USB_DT_DEVICE,
1386 .bcdUSB = cpu_to_le16(0x0200),
1387 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
1388 .bDeviceSubClass = 0x00,
1389 .bDeviceProtocol = 0x00,
1390 .bMaxPacketSize0 = 64,
1391 .idVendor = cpu_to_le16(0x0409),
1392 .idProduct = cpu_to_le16(0xfff0),
1393 .bcdDevice = 0xffff,
1394 .iManufacturer = 0x00,
1396 .iSerialNumber = 0x00,
1397 .bNumConfigurations = 0x01,
1400 /*-------------------------------------------------------------------------*/
1401 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *udc, u32 mode)
1405 if (mode > MAX_TEST_MODE_NUM)
1408 dev_info(udc->dev, "SET FEATURE : test mode = %d\n", mode);
1410 data = _nbu2ss_readl(&udc->p_regs->USB_CONTROL);
1411 data &= ~TEST_FORCE_ENABLE;
1412 data |= mode << TEST_MODE_SHIFT;
1414 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, data);
1415 _nbu2ss_bitset(&udc->p_regs->TEST_CONTROL, CS_TESTMODEEN);
1418 /*-------------------------------------------------------------------------*/
1419 static int _nbu2ss_set_feature_device(
1420 struct nbu2ss_udc *udc,
1425 int result = -EOPNOTSUPP;
1428 case USB_DEVICE_REMOTE_WAKEUP:
1429 if (wIndex == 0x0000) {
1430 udc->remote_wakeup = U2F_ENABLE;
1435 case USB_DEVICE_TEST_MODE:
1437 if (wIndex <= MAX_TEST_MODE_NUM)
1448 /*-------------------------------------------------------------------------*/
1449 static int _nbu2ss_get_ep_stall(struct nbu2ss_udc *udc, u8 ep_adrs)
1452 u32 data = 0, bit_data;
1453 struct fc_regs *preg = udc->p_regs;
1455 epnum = ep_adrs & ~USB_ENDPOINT_DIR_MASK;
1457 data = _nbu2ss_readl(&preg->EP0_CONTROL);
1461 data = _nbu2ss_readl(&preg->EP_REGS[epnum - 1].EP_CONTROL);
1462 if ((data & EPn_EN) == 0)
1465 if (ep_adrs & USB_ENDPOINT_DIR_MASK)
1466 bit_data = EPn_ISTL;
1468 bit_data = EPn_OSTL;
1471 if ((data & bit_data) == 0)
1476 /*-------------------------------------------------------------------------*/
1477 static inline int _nbu2ss_req_feature(struct nbu2ss_udc *udc, bool bset)
1479 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1480 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1481 u16 selector = udc->ctrl.wValue;
1482 u16 wIndex = udc->ctrl.wIndex;
1484 int result = -EOPNOTSUPP;
1486 if ((udc->ctrl.wLength != 0x0000) ||
1487 (direction != USB_DIR_OUT)) {
1491 switch (recipient) {
1492 case USB_RECIP_DEVICE:
1495 _nbu2ss_set_feature_device(udc, selector, wIndex);
1498 case USB_RECIP_ENDPOINT:
1499 if (0x0000 == (wIndex & 0xFF70)) {
1500 if (selector == USB_ENDPOINT_HALT) {
1501 ep_adrs = wIndex & 0xFF;
1503 _nbu2ss_endpoint_toggle_reset(
1507 _nbu2ss_set_endpoint_stall(
1508 udc, ep_adrs, bset);
1520 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1525 /*-------------------------------------------------------------------------*/
1526 static inline enum usb_device_speed _nbu2ss_get_speed(struct nbu2ss_udc *udc)
1529 enum usb_device_speed speed = USB_SPEED_FULL;
1531 data = _nbu2ss_readl(&udc->p_regs->USB_STATUS);
1532 if (data & HIGH_SPEED)
1533 speed = USB_SPEED_HIGH;
1538 /*-------------------------------------------------------------------------*/
1539 static void _nbu2ss_epn_set_stall(
1540 struct nbu2ss_udc *udc,
1541 struct nbu2ss_ep *ep
1548 struct fc_regs *preg = udc->p_regs;
1550 if (ep->direct == USB_DIR_IN) {
1552 ; limit_cnt < IN_DATA_EMPTY_COUNT
1554 regdata = _nbu2ss_readl(
1555 &preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1557 if ((regdata & EPn_IN_DATA) == 0)
1564 ep_adrs = ep->epnum | ep->direct;
1565 _nbu2ss_set_endpoint_stall(udc, ep_adrs, 1);
1568 /*-------------------------------------------------------------------------*/
1569 static int std_req_get_status(struct nbu2ss_udc *udc)
1572 u16 status_data = 0;
1573 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1574 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1576 int result = -EINVAL;
1578 if ((udc->ctrl.wValue != 0x0000) || (direction != USB_DIR_IN))
1581 length = min_t(u16, udc->ctrl.wLength, sizeof(status_data));
1583 switch (recipient) {
1584 case USB_RECIP_DEVICE:
1585 if (udc->ctrl.wIndex == 0x0000) {
1586 if (udc->gadget.is_selfpowered)
1587 status_data |= (1 << USB_DEVICE_SELF_POWERED);
1589 if (udc->remote_wakeup)
1590 status_data |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1596 case USB_RECIP_ENDPOINT:
1597 if (0x0000 == (udc->ctrl.wIndex & 0xFF70)) {
1598 ep_adrs = (u8)(udc->ctrl.wIndex & 0xFF);
1599 result = _nbu2ss_get_ep_stall(udc, ep_adrs);
1602 status_data |= (1 << USB_ENDPOINT_HALT);
1611 memcpy(udc->ep0_buf, &status_data, length);
1612 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, length);
1613 _nbu2ss_ep0_in_transfer(udc, &udc->ep0_req);
1616 dev_err(udc->dev, " Error GET_STATUS\n");
1622 /*-------------------------------------------------------------------------*/
1623 static int std_req_clear_feature(struct nbu2ss_udc *udc)
1625 return _nbu2ss_req_feature(udc, FALSE);
1628 /*-------------------------------------------------------------------------*/
1629 static int std_req_set_feature(struct nbu2ss_udc *udc)
1631 return _nbu2ss_req_feature(udc, TRUE);
1634 /*-------------------------------------------------------------------------*/
1635 static int std_req_set_address(struct nbu2ss_udc *udc)
1638 u32 wValue = udc->ctrl.wValue;
1640 if ((udc->ctrl.bRequestType != 0x00) ||
1641 (udc->ctrl.wIndex != 0x0000) ||
1642 (udc->ctrl.wLength != 0x0000)) {
1646 if (wValue != (wValue & 0x007F))
1649 wValue <<= USB_ADRS_SHIFT;
1651 _nbu2ss_writel(&udc->p_regs->USB_ADDRESS, wValue);
1652 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1657 /*-------------------------------------------------------------------------*/
1658 static int std_req_set_configuration(struct nbu2ss_udc *udc)
1660 u32 ConfigValue = (u32)(udc->ctrl.wValue & 0x00ff);
1662 if ((udc->ctrl.wIndex != 0x0000) ||
1663 (udc->ctrl.wLength != 0x0000) ||
1664 (udc->ctrl.bRequestType != 0x00)) {
1668 udc->curr_config = ConfigValue;
1670 if (ConfigValue > 0) {
1671 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, CONF);
1672 udc->devstate = USB_STATE_CONFIGURED;
1675 _nbu2ss_bitclr(&udc->p_regs->USB_CONTROL, CONF);
1676 udc->devstate = USB_STATE_ADDRESS;
1682 /*-------------------------------------------------------------------------*/
1683 static inline void _nbu2ss_read_request_data(struct nbu2ss_udc *udc, u32 *pdata)
1685 if ((!udc) && (!pdata))
1688 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA0);
1690 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA1);
1693 /*-------------------------------------------------------------------------*/
1694 static inline int _nbu2ss_decode_request(struct nbu2ss_udc *udc)
1696 bool bcall_back = TRUE;
1698 struct usb_ctrlrequest *p_ctrl;
1700 p_ctrl = &udc->ctrl;
1701 _nbu2ss_read_request_data(udc, (u32 *)p_ctrl);
1703 /* ep0 state control */
1704 if (p_ctrl->wLength == 0) {
1705 udc->ep0state = EP0_IN_STATUS_PHASE;
1708 if (p_ctrl->bRequestType & USB_DIR_IN)
1709 udc->ep0state = EP0_IN_DATA_PHASE;
1711 udc->ep0state = EP0_OUT_DATA_PHASE;
1714 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1715 switch (p_ctrl->bRequest) {
1716 case USB_REQ_GET_STATUS:
1717 nret = std_req_get_status(udc);
1721 case USB_REQ_CLEAR_FEATURE:
1722 nret = std_req_clear_feature(udc);
1726 case USB_REQ_SET_FEATURE:
1727 nret = std_req_set_feature(udc);
1731 case USB_REQ_SET_ADDRESS:
1732 nret = std_req_set_address(udc);
1736 case USB_REQ_SET_CONFIGURATION:
1737 nret = std_req_set_configuration(udc);
1746 if (udc->ep0state == EP0_IN_STATUS_PHASE) {
1748 /*--------------------------------------*/
1750 nret = EP0_send_NULL(udc, TRUE);
1755 spin_unlock(&udc->lock);
1756 nret = udc->driver->setup(&udc->gadget, &udc->ctrl);
1757 spin_lock(&udc->lock);
1761 udc->ep0state = EP0_IDLE;
1766 /*-------------------------------------------------------------------------*/
1767 static inline int _nbu2ss_ep0_in_data_stage(struct nbu2ss_udc *udc)
1770 struct nbu2ss_req *req;
1771 struct nbu2ss_ep *ep = &udc->ep[0];
1773 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1775 req = &udc->ep0_req;
1777 req->req.actual += req->div_len;
1780 nret = _nbu2ss_ep0_in_transfer(udc, req);
1782 udc->ep0state = EP0_OUT_STATUS_PAHSE;
1783 EP0_receive_NULL(udc, TRUE);
1789 /*-------------------------------------------------------------------------*/
1790 static inline int _nbu2ss_ep0_out_data_stage(struct nbu2ss_udc *udc)
1793 struct nbu2ss_req *req;
1794 struct nbu2ss_ep *ep = &udc->ep[0];
1796 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1798 req = &udc->ep0_req;
1800 nret = _nbu2ss_ep0_out_transfer(udc, req);
1802 udc->ep0state = EP0_IN_STATUS_PHASE;
1803 EP0_send_NULL(udc, TRUE);
1805 } else if (nret < 0) {
1806 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, EP0_BCLR);
1807 req->req.status = nret;
1813 /*-------------------------------------------------------------------------*/
1814 static inline int _nbu2ss_ep0_status_stage(struct nbu2ss_udc *udc)
1816 struct nbu2ss_req *req;
1817 struct nbu2ss_ep *ep = &udc->ep[0];
1819 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1821 req = &udc->ep0_req;
1822 if (req->req.complete)
1823 req->req.complete(&ep->ep, &req->req);
1826 if (req->req.complete)
1827 _nbu2ss_ep_done(ep, req, 0);
1830 udc->ep0state = EP0_IDLE;
1835 /*-------------------------------------------------------------------------*/
1836 static inline void _nbu2ss_ep0_int(struct nbu2ss_udc *udc)
1843 status = _nbu2ss_readl(&udc->p_regs->EP0_STATUS);
1844 intr = status & EP0_STATUS_RW_BIT;
1845 _nbu2ss_writel(&udc->p_regs->EP0_STATUS, ~intr);
1847 status &= (SETUP_INT | EP0_IN_INT | EP0_OUT_INT
1848 | STG_END_INT | EP0_OUT_NULL_INT);
1851 dev_info(udc->dev, "%s Not Decode Interrupt\n", __func__);
1852 dev_info(udc->dev, "EP0_STATUS = 0x%08x\n", intr);
1856 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1857 udc->gadget.speed = _nbu2ss_get_speed(udc);
1859 for (i = 0; i < EP0_END_XFER; i++) {
1860 switch (udc->ep0state) {
1862 if (status & SETUP_INT) {
1864 nret = _nbu2ss_decode_request(udc);
1868 case EP0_IN_DATA_PHASE:
1869 if (status & EP0_IN_INT) {
1870 status &= ~EP0_IN_INT;
1871 nret = _nbu2ss_ep0_in_data_stage(udc);
1875 case EP0_OUT_DATA_PHASE:
1876 if (status & EP0_OUT_INT) {
1877 status &= ~EP0_OUT_INT;
1878 nret = _nbu2ss_ep0_out_data_stage(udc);
1882 case EP0_IN_STATUS_PHASE:
1883 if ((status & STG_END_INT) || (status & SETUP_INT)) {
1884 status &= ~(STG_END_INT | EP0_IN_INT);
1885 nret = _nbu2ss_ep0_status_stage(udc);
1889 case EP0_OUT_STATUS_PAHSE:
1890 if ((status & STG_END_INT) || (status & SETUP_INT) ||
1891 (status & EP0_OUT_NULL_INT)) {
1892 status &= ~(STG_END_INT
1894 | EP0_OUT_NULL_INT);
1896 nret = _nbu2ss_ep0_status_stage(udc);
1912 _nbu2ss_set_endpoint_stall(udc, 0, TRUE);
1916 /*-------------------------------------------------------------------------*/
1917 static void _nbu2ss_ep_done(
1918 struct nbu2ss_ep *ep,
1919 struct nbu2ss_req *req,
1922 struct nbu2ss_udc *udc = ep->udc;
1924 list_del_init(&req->queue);
1926 if (status == -ECONNRESET)
1927 _nbu2ss_fifo_flush(udc, ep);
1929 if (likely(req->req.status == -EINPROGRESS))
1930 req->req.status = status;
1933 _nbu2ss_epn_set_stall(udc, ep);
1935 if (!list_empty(&ep->queue))
1936 _nbu2ss_restert_transfer(ep);
1940 if ((ep->direct == USB_DIR_OUT) && (ep->epnum > 0) &&
1941 (req->req.dma != 0))
1942 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_OUT);
1945 spin_unlock(&udc->lock);
1946 req->req.complete(&ep->ep, &req->req);
1947 spin_lock(&udc->lock);
1950 /*-------------------------------------------------------------------------*/
1951 static inline void _nbu2ss_epn_in_int(
1952 struct nbu2ss_udc *udc,
1953 struct nbu2ss_ep *ep,
1954 struct nbu2ss_req *req)
1959 struct fc_regs *preg = udc->p_regs;
1962 return; /* DMA is forwarded */
1964 req->req.actual += req->div_len;
1967 if (req->req.actual != req->req.length) {
1968 /*---------------------------------------------------------*/
1969 /* remainder of data */
1970 result = _nbu2ss_epn_in_transfer(udc, ep, req);
1973 if (req->zero && ((req->req.actual % ep->ep.maxpacket) == 0)) {
1975 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1977 if ((status & EPn_IN_FULL) == 0) {
1978 /*-----------------------------------------*/
1979 /* 0 Length Packet */
1981 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1988 /*---------------------------------------------------------*/
1990 _nbu2ss_ep_done(ep, req, result);
1994 /*-------------------------------------------------------------------------*/
1995 static inline void _nbu2ss_epn_out_int(
1996 struct nbu2ss_udc *udc,
1997 struct nbu2ss_ep *ep,
1998 struct nbu2ss_req *req)
2002 result = _nbu2ss_epn_out_transfer(udc, ep, req);
2004 _nbu2ss_ep_done(ep, req, result);
2007 /*-------------------------------------------------------------------------*/
2008 static inline void _nbu2ss_epn_in_dma_int(
2009 struct nbu2ss_udc *udc,
2010 struct nbu2ss_ep *ep,
2011 struct nbu2ss_req *req)
2015 struct usb_request *preq;
2022 preq->actual += req->div_len;
2024 req->dma_flag = FALSE;
2027 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_IN);
2030 if (preq->actual != preq->length) {
2031 _nbu2ss_epn_in_transfer(udc, ep, req);
2033 mpkt = ep->ep.maxpacket;
2034 size = preq->actual % mpkt;
2036 if (((preq->actual & 0x03) == 0) && (size < mpkt))
2037 _nbu2ss_ep_in_end(udc, ep->epnum, 0, 0);
2039 _nbu2ss_epn_in_int(udc, ep, req);
2044 /*-------------------------------------------------------------------------*/
2045 static inline void _nbu2ss_epn_out_dma_int(
2046 struct nbu2ss_udc *udc,
2047 struct nbu2ss_ep *ep,
2048 struct nbu2ss_req *req)
2052 u32 dmacnt, ep_dmacnt;
2054 struct fc_regs *preg = udc->p_regs;
2056 num = ep->epnum - 1;
2058 if (req->req.actual == req->req.length) {
2059 if ((req->req.length % ep->ep.maxpacket) && !req->zero) {
2061 req->dma_flag = FALSE;
2062 _nbu2ss_ep_done(ep, req, 0);
2067 ep_dmacnt = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT)
2071 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2072 dmacnt = _nbu2ss_readl(&preg->EP_DCR[num].EP_DCR1)
2075 if (ep_dmacnt == dmacnt)
2079 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPn_REQEN);
2082 mpkt = ep->ep.maxpacket;
2083 if ((req->div_len % mpkt) == 0)
2084 req->div_len -= mpkt * dmacnt;
2087 if ((req->req.actual % ep->ep.maxpacket) > 0) {
2088 if (req->req.actual == req->div_len) {
2090 req->dma_flag = FALSE;
2091 _nbu2ss_ep_done(ep, req, 0);
2096 req->req.actual += req->div_len;
2098 req->dma_flag = FALSE;
2100 _nbu2ss_epn_out_int(udc, ep, req);
2103 /*-------------------------------------------------------------------------*/
2104 static inline void _nbu2ss_epn_int(struct nbu2ss_udc *udc, u32 epnum)
2109 struct nbu2ss_req *req;
2110 struct nbu2ss_ep *ep = &udc->ep[epnum];
2114 /* Interrupt Status */
2115 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
2117 /* Interrupt Clear */
2118 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_STATUS, ~status);
2120 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
2122 /* pr_warn("=== %s(%d) req == NULL\n", __func__, epnum); */
2126 if (status & EPn_OUT_END_INT) {
2127 status &= ~EPn_OUT_INT;
2128 _nbu2ss_epn_out_dma_int(udc, ep, req);
2131 if (status & EPn_OUT_INT)
2132 _nbu2ss_epn_out_int(udc, ep, req);
2134 if (status & EPn_IN_END_INT) {
2135 status &= ~EPn_IN_INT;
2136 _nbu2ss_epn_in_dma_int(udc, ep, req);
2139 if (status & EPn_IN_INT)
2140 _nbu2ss_epn_in_int(udc, ep, req);
2143 /*-------------------------------------------------------------------------*/
2144 static inline void _nbu2ss_ep_int(struct nbu2ss_udc *udc, u32 epnum)
2147 _nbu2ss_ep0_int(udc);
2149 _nbu2ss_epn_int(udc, epnum);
2152 /*-------------------------------------------------------------------------*/
2153 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *udc)
2155 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, (EP0_AUTO | EP0_BCLR));
2156 _nbu2ss_writel(&udc->p_regs->EP0_INT_ENA, EP0_INT_EN_BIT);
2159 /*-------------------------------------------------------------------------*/
2160 static int _nbu2ss_nuke(struct nbu2ss_udc *udc,
2161 struct nbu2ss_ep *ep,
2164 struct nbu2ss_req *req;
2166 /* Endpoint Disable */
2167 _nbu2ss_epn_exit(udc, ep);
2170 _nbu2ss_ep_dma_exit(udc, ep);
2172 if (list_empty(&ep->queue))
2175 /* called with irqs blocked */
2176 list_for_each_entry(req, &ep->queue, queue) {
2177 _nbu2ss_ep_done(ep, req, status);
2183 /*-------------------------------------------------------------------------*/
2184 static void _nbu2ss_quiesce(struct nbu2ss_udc *udc)
2186 struct nbu2ss_ep *ep;
2188 udc->gadget.speed = USB_SPEED_UNKNOWN;
2190 _nbu2ss_nuke(udc, &udc->ep[0], -ESHUTDOWN);
2193 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2194 _nbu2ss_nuke(udc, ep, -ESHUTDOWN);
2198 /*-------------------------------------------------------------------------*/
2199 static int _nbu2ss_pullup(struct nbu2ss_udc *udc, int is_on)
2203 if (udc->vbus_active == 0)
2209 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL)
2210 | PUE2) & ~(u32)CONNECTB;
2212 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2217 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL) | CONNECTB)
2220 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2221 udc->gadget.speed = USB_SPEED_UNKNOWN;
2227 /*-------------------------------------------------------------------------*/
2228 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
2230 struct fc_regs *p = udc->p_regs;
2232 if (udc->vbus_active == 0)
2235 if (ep->epnum == 0) {
2237 _nbu2ss_bitset(&p->EP0_CONTROL, EP0_BCLR);
2241 _nbu2ss_ep_dma_abort(udc, ep);
2242 _nbu2ss_bitset(&p->EP_REGS[ep->epnum - 1].EP_CONTROL, EPn_BCLR);
2246 /*-------------------------------------------------------------------------*/
2247 static int _nbu2ss_enable_controller(struct nbu2ss_udc *udc)
2251 if (udc->udc_enabled)
2255 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2256 udelay(EPC_RST_DISABLE_TIME); /* 1us wait */
2258 _nbu2ss_bitclr(&udc->p_regs->EPCTR, DIRPD);
2259 mdelay(EPC_DIRPD_DISABLE_TIME); /* 1ms wait */
2261 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2263 _nbu2ss_writel(&udc->p_regs->AHBSCTR, WAIT_MODE);
2265 _nbu2ss_writel(&udc->p_regs->AHBMCTR,
2266 HBUSREQ_MODE | HTRANS_MODE | WBURST_TYPE);
2268 while (!(_nbu2ss_readl(&udc->p_regs->EPCTR) & PLL_LOCK)) {
2270 udelay(1); /* 1us wait */
2271 if (waitcnt == EPC_PLL_LOCK_COUNT) {
2272 dev_err(udc->dev, "*** Reset Cancel failed\n");
2277 _nbu2ss_bitset(&udc->p_regs->UTMI_CHARACTER_1, USB_SQUSET);
2279 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, (INT_SEL | SOF_RCV));
2282 _nbu2ss_ep0_enable(udc);
2284 /* USB Interrupt Enable */
2285 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, USB_INT_EN_BIT);
2287 udc->udc_enabled = TRUE;
2292 /*-------------------------------------------------------------------------*/
2293 static void _nbu2ss_reset_controller(struct nbu2ss_udc *udc)
2295 _nbu2ss_bitset(&udc->p_regs->EPCTR, EPC_RST);
2296 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2299 /*-------------------------------------------------------------------------*/
2300 static void _nbu2ss_disable_controller(struct nbu2ss_udc *udc)
2302 if (udc->udc_enabled) {
2303 udc->udc_enabled = FALSE;
2304 _nbu2ss_reset_controller(udc);
2305 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2309 /*-------------------------------------------------------------------------*/
2310 static inline void _nbu2ss_check_vbus(struct nbu2ss_udc *udc)
2316 mdelay(VBUS_CHATTERING_MDELAY); /* wait (ms) */
2319 reg_dt = gpio_get_value(VBUS_VALUE);
2321 udc->linux_suspended = 0;
2323 _nbu2ss_reset_controller(udc);
2324 dev_info(udc->dev, " ----- VBUS OFF\n");
2326 if (udc->vbus_active == 1) {
2328 udc->vbus_active = 0;
2329 if (udc->usb_suspended) {
2330 udc->usb_suspended = 0;
2331 /* _nbu2ss_reset_controller(udc); */
2333 udc->devstate = USB_STATE_NOTATTACHED;
2335 _nbu2ss_quiesce(udc);
2337 spin_unlock(&udc->lock);
2338 udc->driver->disconnect(&udc->gadget);
2339 spin_lock(&udc->lock);
2342 _nbu2ss_disable_controller(udc);
2345 mdelay(5); /* wait (5ms) */
2346 reg_dt = gpio_get_value(VBUS_VALUE);
2350 dev_info(udc->dev, " ----- VBUS ON\n");
2352 if (udc->linux_suspended)
2355 if (udc->vbus_active == 0) {
2357 udc->vbus_active = 1;
2358 udc->devstate = USB_STATE_POWERED;
2360 nret = _nbu2ss_enable_controller(udc);
2362 _nbu2ss_disable_controller(udc);
2363 udc->vbus_active = 0;
2367 _nbu2ss_pullup(udc, 1);
2369 #ifdef UDC_DEBUG_DUMP
2370 _nbu2ss_dump_register(udc);
2371 #endif /* UDC_DEBUG_DUMP */
2374 if (udc->devstate == USB_STATE_POWERED)
2375 _nbu2ss_pullup(udc, 1);
2380 /*-------------------------------------------------------------------------*/
2381 static inline void _nbu2ss_int_bus_reset(struct nbu2ss_udc *udc)
2383 udc->devstate = USB_STATE_DEFAULT;
2384 udc->remote_wakeup = 0;
2386 _nbu2ss_quiesce(udc);
2388 udc->ep0state = EP0_IDLE;
2391 /*-------------------------------------------------------------------------*/
2392 static inline void _nbu2ss_int_usb_resume(struct nbu2ss_udc *udc)
2394 if (udc->usb_suspended == 1) {
2395 udc->usb_suspended = 0;
2396 if (udc->driver && udc->driver->resume) {
2397 spin_unlock(&udc->lock);
2398 udc->driver->resume(&udc->gadget);
2399 spin_lock(&udc->lock);
2404 /*-------------------------------------------------------------------------*/
2405 static inline void _nbu2ss_int_usb_suspend(struct nbu2ss_udc *udc)
2409 if (udc->usb_suspended == 0) {
2410 reg_dt = gpio_get_value(VBUS_VALUE);
2415 udc->usb_suspended = 1;
2416 if (udc->driver && udc->driver->suspend) {
2417 spin_unlock(&udc->lock);
2418 udc->driver->suspend(&udc->gadget);
2419 spin_lock(&udc->lock);
2422 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, SUSPEND);
2426 /*-------------------------------------------------------------------------*/
2427 /* VBUS (GPIO153) Interrupt */
2428 static irqreturn_t _nbu2ss_vbus_irq(int irq, void *_udc)
2430 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2432 spin_lock(&udc->lock);
2433 _nbu2ss_check_vbus(udc);
2434 spin_unlock(&udc->lock);
2439 /*-------------------------------------------------------------------------*/
2440 /* Interrupt (udc) */
2441 static irqreturn_t _nbu2ss_udc_irq(int irq, void *_udc)
2443 u8 suspend_flag = 0;
2447 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2448 struct fc_regs *preg = udc->p_regs;
2450 if (gpio_get_value(VBUS_VALUE) == 0) {
2451 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2452 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2456 spin_lock(&udc->lock);
2459 if (gpio_get_value(VBUS_VALUE) == 0) {
2460 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2461 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2464 status = _nbu2ss_readl(&preg->USB_INT_STA);
2470 _nbu2ss_writel(&preg->USB_INT_STA, ~(status & USB_INT_STA_RW));
2472 if (status & USB_RST_INT) {
2474 _nbu2ss_int_bus_reset(udc);
2477 if (status & RSUM_INT) {
2479 _nbu2ss_int_usb_resume(udc);
2482 if (status & SPND_INT) {
2487 if (status & EPn_INT) {
2489 int_bit = status >> 8;
2491 for (epnum = 0; epnum < NUM_ENDPOINTS; epnum++) {
2493 _nbu2ss_ep_int(udc, epnum);
2504 _nbu2ss_int_usb_suspend(udc);
2506 spin_unlock(&udc->lock);
2511 /*-------------------------------------------------------------------------*/
2513 static int nbu2ss_ep_enable(
2515 const struct usb_endpoint_descriptor *desc)
2518 unsigned long flags;
2520 struct nbu2ss_ep *ep;
2521 struct nbu2ss_udc *udc;
2523 if ((!_ep) || (!desc)) {
2524 pr_err(" *** %s, bad param\n", __func__);
2528 ep = container_of(_ep, struct nbu2ss_ep, ep);
2529 if ((!ep) || (!ep->udc)) {
2530 pr_err(" *** %s, ep == NULL !!\n", __func__);
2534 ep_type = usb_endpoint_type(desc);
2535 if ((ep_type == USB_ENDPOINT_XFER_CONTROL) ||
2536 (ep_type == USB_ENDPOINT_XFER_ISOC)) {
2537 pr_err(" *** %s, bat bmAttributes\n", __func__);
2542 if (udc->vbus_active == 0)
2545 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
2546 dev_err(ep->udc->dev, " *** %s, udc !!\n", __func__);
2550 spin_lock_irqsave(&udc->lock, flags);
2553 ep->epnum = usb_endpoint_num(desc);
2554 ep->direct = desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2555 ep->ep_type = ep_type;
2558 ep->stalled = FALSE;
2560 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2563 _nbu2ss_ep_dma_init(udc, ep);
2565 /* Endpoint setting */
2566 _nbu2ss_ep_init(udc, ep);
2568 spin_unlock_irqrestore(&udc->lock, flags);
2573 /*-------------------------------------------------------------------------*/
2574 static int nbu2ss_ep_disable(struct usb_ep *_ep)
2576 struct nbu2ss_ep *ep;
2577 struct nbu2ss_udc *udc;
2578 unsigned long flags;
2581 pr_err(" *** %s, bad param\n", __func__);
2585 ep = container_of(_ep, struct nbu2ss_ep, ep);
2586 if ((!ep) || (!ep->udc)) {
2587 pr_err("udc: *** %s, ep == NULL !!\n", __func__);
2592 if (udc->vbus_active == 0)
2595 spin_lock_irqsave(&udc->lock, flags);
2596 _nbu2ss_nuke(udc, ep, -EINPROGRESS); /* dequeue request */
2597 spin_unlock_irqrestore(&udc->lock, flags);
2602 /*-------------------------------------------------------------------------*/
2603 static struct usb_request *nbu2ss_ep_alloc_request(
2607 struct nbu2ss_req *req;
2609 req = kzalloc(sizeof(*req), gfp_flags);
2614 req->req.dma = DMA_ADDR_INVALID;
2616 INIT_LIST_HEAD(&req->queue);
2621 /*-------------------------------------------------------------------------*/
2622 static void nbu2ss_ep_free_request(
2624 struct usb_request *_req)
2626 struct nbu2ss_req *req;
2629 req = container_of(_req, struct nbu2ss_req, req);
2635 /*-------------------------------------------------------------------------*/
2636 static int nbu2ss_ep_queue(
2638 struct usb_request *_req,
2641 struct nbu2ss_req *req;
2642 struct nbu2ss_ep *ep;
2643 struct nbu2ss_udc *udc;
2644 unsigned long flags;
2646 int result = -EINVAL;
2648 /* catch various bogus parameters */
2649 if ((!_ep) || (!_req)) {
2651 pr_err("udc: %s --- _ep == NULL\n", __func__);
2654 pr_err("udc: %s --- _req == NULL\n", __func__);
2659 req = container_of(_req, struct nbu2ss_req, req);
2660 if (unlikely(!_req->complete || !_req->buf || !list_empty(&req->queue))) {
2661 if (!_req->complete)
2662 pr_err("udc: %s --- !_req->complete\n", __func__);
2665 pr_err("udc:%s --- !_req->buf\n", __func__);
2667 if (!list_empty(&req->queue))
2668 pr_err("%s --- !list_empty(&req->queue)\n", __func__);
2673 ep = container_of(_ep, struct nbu2ss_ep, ep);
2676 if (udc->vbus_active == 0) {
2677 dev_info(udc->dev, "Can't ep_queue (VBUS OFF)\n");
2681 if (unlikely(!udc->driver)) {
2682 dev_err(udc->dev, "%s, bogus device state %p\n", __func__,
2687 spin_lock_irqsave(&udc->lock, flags);
2690 if ((uintptr_t)req->req.buf & 0x3)
2691 req->unaligned = TRUE;
2693 req->unaligned = FALSE;
2695 if (req->unaligned) {
2697 ep->virt_buf = (u8 *)dma_alloc_coherent(
2699 &ep->phys_buf, GFP_ATOMIC | GFP_DMA);
2700 if (ep->epnum > 0) {
2701 if (ep->direct == USB_DIR_IN)
2702 memcpy(ep->virt_buf, req->req.buf,
2707 if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT) &&
2708 (req->req.dma != 0))
2709 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_OUT);
2712 _req->status = -EINPROGRESS;
2715 bflag = list_empty(&ep->queue);
2716 list_add_tail(&req->queue, &ep->queue);
2718 if (bflag && !ep->stalled) {
2719 result = _nbu2ss_start_transfer(udc, ep, req, FALSE);
2721 dev_err(udc->dev, " *** %s, result = %d\n", __func__,
2723 list_del(&req->queue);
2724 } else if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT)) {
2726 if (req->req.length < 4 &&
2727 req->req.length == req->req.actual)
2729 if (req->req.length == req->req.actual)
2731 _nbu2ss_ep_done(ep, req, result);
2735 spin_unlock_irqrestore(&udc->lock, flags);
2740 /*-------------------------------------------------------------------------*/
2741 static int nbu2ss_ep_dequeue(
2743 struct usb_request *_req)
2745 struct nbu2ss_req *req;
2746 struct nbu2ss_ep *ep;
2747 struct nbu2ss_udc *udc;
2748 unsigned long flags;
2750 /* catch various bogus parameters */
2751 if ((!_ep) || (!_req)) {
2752 /* pr_err("%s, bad param(1)\n", __func__); */
2756 ep = container_of(_ep, struct nbu2ss_ep, ep);
2758 pr_err("%s, ep == NULL !!\n", __func__);
2766 spin_lock_irqsave(&udc->lock, flags);
2768 /* make sure it's actually queued on this endpoint */
2769 list_for_each_entry(req, &ep->queue, queue) {
2770 if (&req->req == _req)
2773 if (&req->req != _req) {
2774 spin_unlock_irqrestore(&udc->lock, flags);
2775 pr_debug("%s no queue(EINVAL)\n", __func__);
2779 _nbu2ss_ep_done(ep, req, -ECONNRESET);
2781 spin_unlock_irqrestore(&udc->lock, flags);
2786 /*-------------------------------------------------------------------------*/
2787 static int nbu2ss_ep_set_halt(struct usb_ep *_ep, int value)
2790 unsigned long flags;
2792 struct nbu2ss_ep *ep;
2793 struct nbu2ss_udc *udc;
2796 pr_err("%s, bad param\n", __func__);
2800 ep = container_of(_ep, struct nbu2ss_ep, ep);
2802 pr_err("%s, bad ep\n", __func__);
2808 dev_err(ep->udc->dev, " *** %s, bad udc\n", __func__);
2812 spin_lock_irqsave(&udc->lock, flags);
2814 ep_adrs = ep->epnum | ep->direct;
2816 _nbu2ss_set_endpoint_stall(udc, ep_adrs, value);
2817 ep->stalled = FALSE;
2819 if (list_empty(&ep->queue))
2820 _nbu2ss_epn_set_stall(udc, ep);
2828 spin_unlock_irqrestore(&udc->lock, flags);
2833 static int nbu2ss_ep_set_wedge(struct usb_ep *_ep)
2835 return nbu2ss_ep_set_halt(_ep, 1);
2838 /*-------------------------------------------------------------------------*/
2839 static int nbu2ss_ep_fifo_status(struct usb_ep *_ep)
2842 struct nbu2ss_ep *ep;
2843 struct nbu2ss_udc *udc;
2844 unsigned long flags;
2845 struct fc_regs *preg;
2848 pr_err("%s, bad param\n", __func__);
2852 ep = container_of(_ep, struct nbu2ss_ep, ep);
2854 pr_err("%s, bad ep\n", __func__);
2860 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2866 data = gpio_get_value(VBUS_VALUE);
2870 spin_lock_irqsave(&udc->lock, flags);
2872 if (ep->epnum == 0) {
2873 data = _nbu2ss_readl(&preg->EP0_LENGTH) & EP0_LDATA;
2876 data = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_LEN_DCNT)
2880 spin_unlock_irqrestore(&udc->lock, flags);
2885 /*-------------------------------------------------------------------------*/
2886 static void nbu2ss_ep_fifo_flush(struct usb_ep *_ep)
2889 struct nbu2ss_ep *ep;
2890 struct nbu2ss_udc *udc;
2891 unsigned long flags;
2894 pr_err("udc: %s, bad param\n", __func__);
2898 ep = container_of(_ep, struct nbu2ss_ep, ep);
2900 pr_err("udc: %s, bad ep\n", __func__);
2906 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2910 data = gpio_get_value(VBUS_VALUE);
2914 spin_lock_irqsave(&udc->lock, flags);
2915 _nbu2ss_fifo_flush(udc, ep);
2916 spin_unlock_irqrestore(&udc->lock, flags);
2919 /*-------------------------------------------------------------------------*/
2920 static const struct usb_ep_ops nbu2ss_ep_ops = {
2921 .enable = nbu2ss_ep_enable,
2922 .disable = nbu2ss_ep_disable,
2924 .alloc_request = nbu2ss_ep_alloc_request,
2925 .free_request = nbu2ss_ep_free_request,
2927 .queue = nbu2ss_ep_queue,
2928 .dequeue = nbu2ss_ep_dequeue,
2930 .set_halt = nbu2ss_ep_set_halt,
2931 .set_wedge = nbu2ss_ep_set_wedge,
2933 .fifo_status = nbu2ss_ep_fifo_status,
2934 .fifo_flush = nbu2ss_ep_fifo_flush,
2937 /*-------------------------------------------------------------------------*/
2938 /* usb_gadget_ops */
2940 /*-------------------------------------------------------------------------*/
2941 static int nbu2ss_gad_get_frame(struct usb_gadget *pgadget)
2944 struct nbu2ss_udc *udc;
2947 pr_err("udc: %s, bad param\n", __func__);
2951 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2953 dev_err(&pgadget->dev, "%s, udc == NULL\n", __func__);
2957 data = gpio_get_value(VBUS_VALUE);
2961 return _nbu2ss_readl(&udc->p_regs->USB_ADDRESS) & FRAME;
2964 /*-------------------------------------------------------------------------*/
2965 static int nbu2ss_gad_wakeup(struct usb_gadget *pgadget)
2970 struct nbu2ss_udc *udc;
2973 pr_err("%s, bad param\n", __func__);
2977 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2979 dev_err(&pgadget->dev, "%s, udc == NULL\n", __func__);
2983 data = gpio_get_value(VBUS_VALUE);
2985 dev_warn(&pgadget->dev, "VBUS LEVEL = %d\n", data);
2989 _nbu2ss_bitset(&udc->p_regs->EPCTR, PLL_RESUME);
2991 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2992 data = _nbu2ss_readl(&udc->p_regs->EPCTR);
2994 if (data & PLL_LOCK)
2998 _nbu2ss_bitclr(&udc->p_regs->EPCTR, PLL_RESUME);
3003 /*-------------------------------------------------------------------------*/
3004 static int nbu2ss_gad_set_selfpowered(struct usb_gadget *pgadget,
3007 struct nbu2ss_udc *udc;
3008 unsigned long flags;
3011 pr_err("%s, bad param\n", __func__);
3015 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3017 spin_lock_irqsave(&udc->lock, flags);
3018 pgadget->is_selfpowered = (is_selfpowered != 0);
3019 spin_unlock_irqrestore(&udc->lock, flags);
3024 /*-------------------------------------------------------------------------*/
3025 static int nbu2ss_gad_vbus_session(struct usb_gadget *pgadget, int is_active)
3030 /*-------------------------------------------------------------------------*/
3031 static int nbu2ss_gad_vbus_draw(struct usb_gadget *pgadget, unsigned int mA)
3033 struct nbu2ss_udc *udc;
3034 unsigned long flags;
3037 pr_err("%s, bad param\n", __func__);
3041 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3043 spin_lock_irqsave(&udc->lock, flags);
3045 spin_unlock_irqrestore(&udc->lock, flags);
3050 /*-------------------------------------------------------------------------*/
3051 static int nbu2ss_gad_pullup(struct usb_gadget *pgadget, int is_on)
3053 struct nbu2ss_udc *udc;
3054 unsigned long flags;
3057 pr_err("%s, bad param\n", __func__);
3061 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3064 pr_warn("%s, Not Regist Driver\n", __func__);
3068 if (udc->vbus_active == 0)
3071 spin_lock_irqsave(&udc->lock, flags);
3072 _nbu2ss_pullup(udc, is_on);
3073 spin_unlock_irqrestore(&udc->lock, flags);
3078 /*-------------------------------------------------------------------------*/
3079 static int nbu2ss_gad_ioctl(
3080 struct usb_gadget *pgadget,
3082 unsigned long param)
3087 static const struct usb_gadget_ops nbu2ss_gadget_ops = {
3088 .get_frame = nbu2ss_gad_get_frame,
3089 .wakeup = nbu2ss_gad_wakeup,
3090 .set_selfpowered = nbu2ss_gad_set_selfpowered,
3091 .vbus_session = nbu2ss_gad_vbus_session,
3092 .vbus_draw = nbu2ss_gad_vbus_draw,
3093 .pullup = nbu2ss_gad_pullup,
3094 .ioctl = nbu2ss_gad_ioctl,
3097 static const struct {
3099 const struct usb_ep_caps caps;
3100 } ep_info[NUM_ENDPOINTS] = {
3101 #define EP_INFO(_name, _caps) \
3108 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
3110 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3112 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3113 EP_INFO("ep3in-int",
3114 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3116 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3118 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3120 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3122 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3123 EP_INFO("ep8in-int",
3124 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3126 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3128 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3130 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3132 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3133 EP_INFO("epdin-int",
3134 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3139 /*-------------------------------------------------------------------------*/
3140 static void __init nbu2ss_drv_ep_init(struct nbu2ss_udc *udc)
3144 INIT_LIST_HEAD(&udc->gadget.ep_list);
3145 udc->gadget.ep0 = &udc->ep[0].ep;
3147 for (i = 0; i < NUM_ENDPOINTS; i++) {
3148 struct nbu2ss_ep *ep = &udc->ep[i];
3153 ep->ep.driver_data = NULL;
3154 ep->ep.name = ep_info[i].name;
3155 ep->ep.caps = ep_info[i].caps;
3156 ep->ep.ops = &nbu2ss_ep_ops;
3158 usb_ep_set_maxpacket_limit(&ep->ep,
3159 i == 0 ? EP0_PACKETSIZE
3162 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
3163 INIT_LIST_HEAD(&ep->queue);
3166 list_del_init(&udc->ep[0].ep.ep_list);
3169 /*-------------------------------------------------------------------------*/
3170 /* platform_driver */
3171 static int __init nbu2ss_drv_contest_init(
3172 struct platform_device *pdev,
3173 struct nbu2ss_udc *udc)
3175 spin_lock_init(&udc->lock);
3176 udc->dev = &pdev->dev;
3178 udc->gadget.is_selfpowered = 1;
3179 udc->devstate = USB_STATE_NOTATTACHED;
3183 udc->pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3186 nbu2ss_drv_ep_init(udc);
3189 udc->gadget.ops = &nbu2ss_gadget_ops;
3190 udc->gadget.ep0 = &udc->ep[0].ep;
3191 udc->gadget.speed = USB_SPEED_UNKNOWN;
3192 udc->gadget.name = driver_name;
3193 /* udc->gadget.is_dualspeed = 1; */
3195 device_initialize(&udc->gadget.dev);
3197 dev_set_name(&udc->gadget.dev, "gadget");
3198 udc->gadget.dev.parent = &pdev->dev;
3199 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
3205 * probe - binds to the platform device
3207 static int nbu2ss_drv_probe(struct platform_device *pdev)
3209 int status = -ENODEV;
3210 struct nbu2ss_udc *udc;
3213 void __iomem *mmio_base;
3215 udc = &udc_controller;
3216 memset(udc, 0, sizeof(struct nbu2ss_udc));
3218 platform_set_drvdata(pdev, udc);
3220 /* require I/O memory and IRQ to be provided as resources */
3221 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3222 mmio_base = devm_ioremap_resource(&pdev->dev, r);
3223 if (IS_ERR(mmio_base))
3224 return PTR_ERR(mmio_base);
3226 irq = platform_get_irq(pdev, 0);
3228 dev_err(&pdev->dev, "failed to get IRQ\n");
3231 status = devm_request_irq(&pdev->dev, irq, _nbu2ss_udc_irq,
3232 0, driver_name, udc);
3235 udc->p_regs = (struct fc_regs *)mmio_base;
3237 /* USB Function Controller Interrupt */
3239 dev_err(udc->dev, "request_irq(USB_UDC_IRQ_1) failed\n");
3243 /* Driver Initialization */
3244 status = nbu2ss_drv_contest_init(pdev, udc);
3250 /* VBUS Interrupt */
3251 irq_set_irq_type(INT_VBUS, IRQ_TYPE_EDGE_BOTH);
3252 status = request_irq(INT_VBUS,
3253 _nbu2ss_vbus_irq, IRQF_SHARED, driver_name, udc);
3256 dev_err(udc->dev, "request_irq(INT_VBUS) failed\n");
3263 /*-------------------------------------------------------------------------*/
3264 static void nbu2ss_drv_shutdown(struct platform_device *pdev)
3266 struct nbu2ss_udc *udc;
3268 udc = platform_get_drvdata(pdev);
3272 _nbu2ss_disable_controller(udc);
3275 /*-------------------------------------------------------------------------*/
3276 static int nbu2ss_drv_remove(struct platform_device *pdev)
3278 struct nbu2ss_udc *udc;
3279 struct nbu2ss_ep *ep;
3282 udc = &udc_controller;
3284 for (i = 0; i < NUM_ENDPOINTS; i++) {
3287 dma_free_coherent(NULL, PAGE_SIZE, (void *)ep->virt_buf,
3291 /* Interrupt Handler - Release */
3292 free_irq(INT_VBUS, udc);
3297 /*-------------------------------------------------------------------------*/
3298 static int nbu2ss_drv_suspend(struct platform_device *pdev, pm_message_t state)
3300 struct nbu2ss_udc *udc;
3302 udc = platform_get_drvdata(pdev);
3306 if (udc->vbus_active) {
3307 udc->vbus_active = 0;
3308 udc->devstate = USB_STATE_NOTATTACHED;
3309 udc->linux_suspended = 1;
3311 if (udc->usb_suspended) {
3312 udc->usb_suspended = 0;
3313 _nbu2ss_reset_controller(udc);
3316 _nbu2ss_quiesce(udc);
3318 _nbu2ss_disable_controller(udc);
3323 /*-------------------------------------------------------------------------*/
3324 static int nbu2ss_drv_resume(struct platform_device *pdev)
3327 struct nbu2ss_udc *udc;
3329 udc = platform_get_drvdata(pdev);
3333 data = gpio_get_value(VBUS_VALUE);
3335 udc->vbus_active = 1;
3336 udc->devstate = USB_STATE_POWERED;
3337 _nbu2ss_enable_controller(udc);
3338 _nbu2ss_pullup(udc, 1);
3341 udc->linux_suspended = 0;
3346 static struct platform_driver udc_driver = {
3347 .probe = nbu2ss_drv_probe,
3348 .shutdown = nbu2ss_drv_shutdown,
3349 .remove = nbu2ss_drv_remove,
3350 .suspend = nbu2ss_drv_suspend,
3351 .resume = nbu2ss_drv_resume,
3353 .name = driver_name,
3357 module_platform_driver(udc_driver);
3359 MODULE_DESCRIPTION(DRIVER_DESC);
3360 MODULE_AUTHOR("Renesas Electronics Corporation");
3361 MODULE_LICENSE("GPL");