1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/usb/gadget/emxx_udc.c
4 * EMXX FCD (Function Controller Driver) for USB.
6 * Copyright (C) 2010 Renesas Electronics Corporation
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/delay.h>
13 #include <linux/ioport.h>
14 #include <linux/slab.h>
15 #include <linux/errno.h>
16 #include <linux/list.h>
17 #include <linux/interrupt.h>
18 #include <linux/proc_fs.h>
19 #include <linux/clk.h>
20 #include <linux/ctype.h>
21 #include <linux/string.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/workqueue.h>
24 #include <linux/device.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
29 #include <linux/irq.h>
30 #include <linux/gpio/consumer.h>
34 #define DRIVER_DESC "EMXX UDC driver"
35 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
37 static const char driver_name[] = "emxx_udc";
38 static const char driver_desc[] = DRIVER_DESC;
40 /*===========================================================================*/
42 static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *, struct nbu2ss_ep *);
43 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *);
44 /*static void _nbu2ss_ep0_disable(struct nbu2ss_udc *);*/
45 static void _nbu2ss_ep_done(struct nbu2ss_ep *, struct nbu2ss_req *, int);
46 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *, u32 mode);
47 static void _nbu2ss_endpoint_toggle_reset(struct nbu2ss_udc *udc, u8 ep_adrs);
49 static int _nbu2ss_pullup(struct nbu2ss_udc *, int);
50 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *, struct nbu2ss_ep *);
52 /*===========================================================================*/
54 #define _nbu2ss_zero_len_pkt(udc, epnum) \
55 _nbu2ss_ep_in_end(udc, epnum, 0, 0)
57 /*===========================================================================*/
59 static struct nbu2ss_udc udc_controller;
61 /*-------------------------------------------------------------------------*/
63 static inline u32 _nbu2ss_readl(void __iomem *address)
65 return __raw_readl(address);
68 /*-------------------------------------------------------------------------*/
70 static inline void _nbu2ss_writel(void __iomem *address, u32 udata)
72 __raw_writel(udata, address);
75 /*-------------------------------------------------------------------------*/
77 static inline void _nbu2ss_bitset(void __iomem *address, u32 udata)
79 u32 reg_dt = __raw_readl(address) | (udata);
81 __raw_writel(reg_dt, address);
84 /*-------------------------------------------------------------------------*/
86 static inline void _nbu2ss_bitclr(void __iomem *address, u32 udata)
88 u32 reg_dt = __raw_readl(address) & ~(udata);
90 __raw_writel(reg_dt, address);
94 /*-------------------------------------------------------------------------*/
95 static void _nbu2ss_dump_register(struct nbu2ss_udc *udc)
100 pr_info("=== %s()\n", __func__);
103 pr_err("%s udc == NULL\n", __func__);
107 spin_unlock(&udc->lock);
109 dev_dbg(&udc->dev, "\n-USB REG-\n");
110 for (i = 0x0 ; i < USB_BASE_SIZE ; i += 16) {
111 reg_data = _nbu2ss_readl(IO_ADDRESS(USB_BASE_ADDRESS + i));
112 dev_dbg(&udc->dev, "USB%04x =%08x", i, (int)reg_data);
114 reg_data = _nbu2ss_readl(IO_ADDRESS(USB_BASE_ADDRESS + i + 4));
115 dev_dbg(&udc->dev, " %08x", (int)reg_data);
117 reg_data = _nbu2ss_readl(IO_ADDRESS(USB_BASE_ADDRESS + i + 8));
118 dev_dbg(&udc->dev, " %08x", (int)reg_data);
120 reg_data = _nbu2ss_readl(IO_ADDRESS(USB_BASE_ADDRESS + i + 12));
121 dev_dbg(&udc->dev, " %08x\n", (int)reg_data);
124 spin_lock(&udc->lock);
126 #endif /* UDC_DEBUG_DUMP */
128 /*-------------------------------------------------------------------------*/
129 /* Endpoint 0 Callback (Complete) */
130 static void _nbu2ss_ep0_complete(struct usb_ep *_ep, struct usb_request *_req)
136 struct usb_ctrlrequest *p_ctrl;
137 struct nbu2ss_udc *udc;
142 udc = (struct nbu2ss_udc *)_req->context;
144 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
145 if (p_ctrl->bRequest == USB_REQ_SET_FEATURE) {
146 /*-------------------------------------------------*/
148 recipient = (u8)(p_ctrl->bRequestType & USB_RECIP_MASK);
149 selector = le16_to_cpu(p_ctrl->wValue);
150 if ((recipient == USB_RECIP_DEVICE) &&
151 (selector == USB_DEVICE_TEST_MODE)) {
152 wIndex = le16_to_cpu(p_ctrl->wIndex);
153 test_mode = (u32)(wIndex >> 8);
154 _nbu2ss_set_test_mode(udc, test_mode);
160 /*-------------------------------------------------------------------------*/
161 /* Initialization usb_request */
162 static void _nbu2ss_create_ep0_packet(struct nbu2ss_udc *udc,
163 void *p_buf, unsigned int length)
165 udc->ep0_req.req.buf = p_buf;
166 udc->ep0_req.req.length = length;
167 udc->ep0_req.req.dma = 0;
168 udc->ep0_req.req.zero = true;
169 udc->ep0_req.req.complete = _nbu2ss_ep0_complete;
170 udc->ep0_req.req.status = -EINPROGRESS;
171 udc->ep0_req.req.context = udc;
172 udc->ep0_req.req.actual = 0;
175 /*-------------------------------------------------------------------------*/
176 /* Acquisition of the first address of RAM(FIFO) */
177 static u32 _nbu2ss_get_begin_ram_address(struct nbu2ss_udc *udc)
180 u32 data, last_ram_adr, use_ram_size;
182 struct ep_regs __iomem *p_ep_regs;
184 last_ram_adr = (D_RAM_SIZE_CTRL / sizeof(u32)) * 2;
187 for (num = 0; num < NUM_ENDPOINTS - 1; num++) {
188 p_ep_regs = &udc->p_regs->EP_REGS[num];
189 data = _nbu2ss_readl(&p_ep_regs->EP_PCKT_ADRS);
190 buf_type = _nbu2ss_readl(&p_ep_regs->EP_CONTROL) & EPN_BUF_TYPE;
193 use_ram_size += (data & EPN_MPKT) / sizeof(u32);
196 use_ram_size += ((data & EPN_MPKT) / sizeof(u32)) * 2;
199 if ((data >> 16) > last_ram_adr)
200 last_ram_adr = data >> 16;
203 return last_ram_adr + use_ram_size;
206 /*-------------------------------------------------------------------------*/
207 /* Construction of Endpoint */
208 static int _nbu2ss_ep_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
219 /*-------------------------------------------------------------*/
220 /* RAM Transfer Address */
221 begin_adrs = _nbu2ss_get_begin_ram_address(udc);
222 data = (begin_adrs << 16) | ep->ep.maxpacket;
223 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, data);
225 /*-------------------------------------------------------------*/
226 /* Interrupt Enable */
227 data = 1 << (ep->epnum + 8);
228 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, data);
230 /*-------------------------------------------------------------*/
231 /* Endpoint Type(Mode) */
232 /* Bulk, Interrupt, ISO */
233 switch (ep->ep_type) {
234 case USB_ENDPOINT_XFER_BULK:
238 case USB_ENDPOINT_XFER_INT:
239 data = EPN_BUF_SINGLE | EPN_INTERRUPT;
242 case USB_ENDPOINT_XFER_ISOC:
251 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
252 _nbu2ss_endpoint_toggle_reset(udc, (ep->epnum | ep->direct));
254 if (ep->direct == USB_DIR_OUT) {
255 /*---------------------------------------------------------*/
257 data = EPN_EN | EPN_BCLR | EPN_DIR0;
258 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
260 data = EPN_ONAK | EPN_OSTL_EN | EPN_OSTL;
261 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
263 data = EPN_OUT_EN | EPN_OUT_END_EN;
264 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
266 /*---------------------------------------------------------*/
268 data = EPN_EN | EPN_BCLR | EPN_AUTO;
269 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
272 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
274 data = EPN_IN_EN | EPN_IN_END_EN;
275 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
281 /*-------------------------------------------------------------------------*/
282 /* Release of Endpoint */
283 static int _nbu2ss_epn_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
288 if ((ep->epnum == 0) || (udc->vbus_active == 0))
293 /*-------------------------------------------------------------*/
294 /* RAM Transfer Address */
295 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, 0);
297 /*-------------------------------------------------------------*/
298 /* Interrupt Disable */
299 data = 1 << (ep->epnum + 8);
300 _nbu2ss_bitclr(&udc->p_regs->USB_INT_ENA, data);
302 if (ep->direct == USB_DIR_OUT) {
303 /*---------------------------------------------------------*/
305 data = EPN_ONAK | EPN_BCLR;
306 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
308 data = EPN_EN | EPN_DIR0;
309 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
311 data = EPN_OUT_EN | EPN_OUT_END_EN;
312 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
314 /*---------------------------------------------------------*/
317 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
319 data = EPN_EN | EPN_AUTO;
320 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
322 data = EPN_IN_EN | EPN_IN_END_EN;
323 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
329 /*-------------------------------------------------------------------------*/
330 /* DMA setting (without Endpoint 0) */
331 static void _nbu2ss_ep_dma_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
336 data = _nbu2ss_readl(&udc->p_regs->USBSSCONF);
337 if (((ep->epnum == 0) || (data & (1 << ep->epnum)) == 0))
338 return; /* Not Support DMA */
342 if (ep->direct == USB_DIR_OUT) {
343 /*---------------------------------------------------------*/
345 data = ep->ep.maxpacket;
346 _nbu2ss_writel(&udc->p_regs->EP_DCR[num].EP_DCR2, data);
348 /*---------------------------------------------------------*/
349 /* Transfer Direct */
350 data = DCR1_EPN_DIR0;
351 _nbu2ss_bitset(&udc->p_regs->EP_DCR[num].EP_DCR1, data);
353 /*---------------------------------------------------------*/
355 data = EPN_STOP_MODE | EPN_STOP_SET | EPN_DMAMODE0;
356 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
358 /*---------------------------------------------------------*/
360 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, EPN_AUTO);
362 /*---------------------------------------------------------*/
364 data = EPN_BURST_SET | EPN_DMAMODE0;
365 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
369 /*-------------------------------------------------------------------------*/
370 /* DMA setting release */
371 static void _nbu2ss_ep_dma_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
375 struct fc_regs __iomem *preg = udc->p_regs;
377 if (udc->vbus_active == 0)
378 return; /* VBUS OFF */
380 data = _nbu2ss_readl(&preg->USBSSCONF);
381 if ((ep->epnum == 0) || ((data & (1 << ep->epnum)) == 0))
382 return; /* Not Support DMA */
386 _nbu2ss_ep_dma_abort(udc, ep);
388 if (ep->direct == USB_DIR_OUT) {
389 /*---------------------------------------------------------*/
391 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, 0);
392 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPN_DIR0);
393 _nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
395 /*---------------------------------------------------------*/
397 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPN_AUTO);
398 _nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
402 /*-------------------------------------------------------------------------*/
404 static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
406 struct fc_regs __iomem *preg = udc->p_regs;
408 _nbu2ss_bitclr(&preg->EP_DCR[ep->epnum - 1].EP_DCR1, DCR1_EPN_REQEN);
409 mdelay(DMA_DISABLE_TIME); /* DCR1_EPN_REQEN Clear */
410 _nbu2ss_bitclr(&preg->EP_REGS[ep->epnum - 1].EP_DMA_CTRL, EPN_DMA_EN);
413 /*-------------------------------------------------------------------------*/
414 /* Start IN Transfer */
415 static void _nbu2ss_ep_in_end(struct nbu2ss_udc *udc,
416 u32 epnum, u32 data32, u32 length)
420 struct fc_regs __iomem *preg = udc->p_regs;
422 if (length >= sizeof(u32))
426 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_AUTO);
428 /* Writing of 1-4 bytes */
430 _nbu2ss_writel(&preg->EP0_WRITE, data32);
432 data = ((length << 5) & EP0_DW) | EP0_DEND;
433 _nbu2ss_writel(&preg->EP0_CONTROL, data);
435 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_AUTO);
439 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPN_AUTO);
441 /* Writing of 1-4 bytes */
443 _nbu2ss_writel(&preg->EP_REGS[num].EP_WRITE, data32);
445 data = (((length) << 5) & EPN_DW) | EPN_DEND;
446 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
448 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, EPN_AUTO);
453 /*-------------------------------------------------------------------------*/
454 static void _nbu2ss_dma_map_single(struct nbu2ss_udc *udc,
455 struct nbu2ss_ep *ep,
456 struct nbu2ss_req *req, u8 direct)
458 if (req->req.dma == DMA_ADDR_INVALID) {
459 if (req->unaligned) {
460 req->req.dma = ep->phys_buf;
462 req->req.dma = dma_map_single(udc->gadget.dev.parent,
465 (direct == USB_DIR_IN)
472 dma_sync_single_for_device(udc->gadget.dev.parent,
475 (direct == USB_DIR_IN)
483 /*-------------------------------------------------------------------------*/
484 static void _nbu2ss_dma_unmap_single(struct nbu2ss_udc *udc,
485 struct nbu2ss_ep *ep,
486 struct nbu2ss_req *req, u8 direct)
492 if (direct == USB_DIR_OUT) {
493 count = req->req.actual % 4;
496 p += (req->req.actual - count);
497 memcpy(data, p, count);
502 if (req->unaligned) {
503 if (direct == USB_DIR_OUT)
504 memcpy(req->req.buf, ep->virt_buf,
505 req->req.actual & 0xfffffffc);
507 dma_unmap_single(udc->gadget.dev.parent,
508 req->req.dma, req->req.length,
509 (direct == USB_DIR_IN)
513 req->req.dma = DMA_ADDR_INVALID;
517 dma_sync_single_for_cpu(udc->gadget.dev.parent,
518 req->req.dma, req->req.length,
519 (direct == USB_DIR_IN)
526 p += (req->req.actual - count);
527 memcpy(p, data, count);
532 /*-------------------------------------------------------------------------*/
533 /* Endpoint 0 OUT Transfer (PIO) */
534 static int ep0_out_pio(struct nbu2ss_udc *udc, u8 *buf, u32 length)
537 u32 numreads = length / sizeof(u32);
538 union usb_reg_access *buf32 = (union usb_reg_access *)buf;
544 for (i = 0; i < numreads; i++) {
545 buf32->dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
549 return numreads * sizeof(u32);
552 /*-------------------------------------------------------------------------*/
553 /* Endpoint 0 OUT Transfer (PIO, OverBytes) */
554 static int ep0_out_overbytes(struct nbu2ss_udc *udc, u8 *p_buf, u32 length)
558 union usb_reg_access temp_32;
559 union usb_reg_access *p_buf_32 = (union usb_reg_access *)p_buf;
561 if ((length > 0) && (length < sizeof(u32))) {
562 temp_32.dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
563 for (i = 0 ; i < length ; i++)
564 p_buf_32->byte.DATA[i] = temp_32.byte.DATA[i];
565 i_read_size += length;
571 /*-------------------------------------------------------------------------*/
572 /* Endpoint 0 IN Transfer (PIO) */
573 static int EP0_in_PIO(struct nbu2ss_udc *udc, u8 *p_buf, u32 length)
576 u32 i_max_length = EP0_PACKETSIZE;
577 u32 i_word_length = 0;
578 u32 i_write_length = 0;
579 union usb_reg_access *p_buf_32 = (union usb_reg_access *)p_buf;
581 /*------------------------------------------------------------*/
582 /* Transfer Length */
583 if (i_max_length < length)
584 i_word_length = i_max_length / sizeof(u32);
586 i_word_length = length / sizeof(u32);
588 /*------------------------------------------------------------*/
590 for (i = 0; i < i_word_length; i++) {
591 _nbu2ss_writel(&udc->p_regs->EP0_WRITE, p_buf_32->dw);
593 i_write_length += sizeof(u32);
596 return i_write_length;
599 /*-------------------------------------------------------------------------*/
600 /* Endpoint 0 IN Transfer (PIO, OverBytes) */
601 static int ep0_in_overbytes(struct nbu2ss_udc *udc,
606 union usb_reg_access temp_32;
607 union usb_reg_access *p_buf_32 = (union usb_reg_access *)p_buf;
609 if ((i_remain_size > 0) && (i_remain_size < sizeof(u32))) {
610 for (i = 0 ; i < i_remain_size ; i++)
611 temp_32.byte.DATA[i] = p_buf_32->byte.DATA[i];
612 _nbu2ss_ep_in_end(udc, 0, temp_32.dw, i_remain_size);
614 return i_remain_size;
620 /*-------------------------------------------------------------------------*/
621 /* Transfer NULL Packet (Epndoint 0) */
622 static int EP0_send_NULL(struct nbu2ss_udc *udc, bool pid_flag)
626 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
627 data &= ~(u32)EP0_INAK;
630 data |= (EP0_INAK_EN | EP0_PIDCLR | EP0_DEND);
632 data |= (EP0_INAK_EN | EP0_DEND);
634 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
639 /*-------------------------------------------------------------------------*/
640 /* Receive NULL Packet (Endpoint 0) */
641 static int EP0_receive_NULL(struct nbu2ss_udc *udc, bool pid_flag)
645 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
646 data &= ~(u32)EP0_ONAK;
651 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
656 /*-------------------------------------------------------------------------*/
657 static int _nbu2ss_ep0_in_transfer(struct nbu2ss_udc *udc,
658 struct nbu2ss_req *req)
660 u8 *p_buffer; /* IN Data Buffer */
662 u32 i_remain_size = 0;
665 /*-------------------------------------------------------------*/
666 /* End confirmation */
667 if (req->req.actual == req->req.length) {
668 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
671 EP0_send_NULL(udc, false);
676 return 0; /* Transfer End */
679 /*-------------------------------------------------------------*/
681 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
683 data &= ~(u32)EP0_INAK;
684 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
686 i_remain_size = req->req.length - req->req.actual;
687 p_buffer = (u8 *)req->req.buf;
688 p_buffer += req->req.actual;
690 /*-------------------------------------------------------------*/
692 result = EP0_in_PIO(udc, p_buffer, i_remain_size);
694 req->div_len = result;
695 i_remain_size -= result;
697 if (i_remain_size == 0) {
698 EP0_send_NULL(udc, false);
702 if ((i_remain_size < sizeof(u32)) && (result != EP0_PACKETSIZE)) {
704 result += ep0_in_overbytes(udc, p_buffer, i_remain_size);
705 req->div_len = result;
711 /*-------------------------------------------------------------------------*/
712 static int _nbu2ss_ep0_out_transfer(struct nbu2ss_udc *udc,
713 struct nbu2ss_req *req)
721 /*-------------------------------------------------------------*/
722 /* Receive data confirmation */
723 i_recv_length = _nbu2ss_readl(&udc->p_regs->EP0_LENGTH) & EP0_LDATA;
724 if (i_recv_length != 0) {
727 i_remain_size = req->req.length - req->req.actual;
728 p_buffer = (u8 *)req->req.buf;
729 p_buffer += req->req.actual;
731 result = ep0_out_pio(udc, p_buffer
732 , min(i_remain_size, i_recv_length));
736 req->req.actual += result;
737 i_recv_length -= result;
739 if ((i_recv_length > 0) && (i_recv_length < sizeof(u32))) {
741 i_remain_size -= result;
743 result = ep0_out_overbytes(udc, p_buffer
744 , min(i_remain_size, i_recv_length));
745 req->req.actual += result;
751 /*-------------------------------------------------------------*/
752 /* End confirmation */
753 if (req->req.actual == req->req.length) {
754 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
757 EP0_receive_NULL(udc, false);
762 return 0; /* Transfer End */
765 if ((req->req.actual % EP0_PACKETSIZE) != 0)
766 return 0; /* Short Packet Transfer End */
768 if (req->req.actual > req->req.length) {
769 dev_err(udc->dev, " *** Overrun Error\n");
773 if (f_rcv_zero != 0) {
774 i_remain_size = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
775 if (i_remain_size & EP0_ONAK) {
776 /*---------------------------------------------------*/
778 _nbu2ss_bitclr(&udc->p_regs->EP0_CONTROL, EP0_ONAK);
786 /*-------------------------------------------------------------------------*/
787 static int _nbu2ss_out_dma(struct nbu2ss_udc *udc, struct nbu2ss_req *req,
796 int result = -EINVAL;
797 struct fc_regs __iomem *preg = udc->p_regs;
800 return 1; /* DMA is forwarded */
802 req->dma_flag = true;
803 p_buffer = req->req.dma;
804 p_buffer += req->req.actual;
807 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)p_buffer);
809 /* Number of transfer packets */
810 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPN_MPKT;
811 dmacnt = length / mpkt;
812 lmpkt = (length % mpkt) & ~(u32)0x03;
814 if (dmacnt > DMA_MAX_COUNT) {
815 dmacnt = DMA_MAX_COUNT;
817 } else if (lmpkt != 0) {
819 burst = 0; /* Burst OFF */
823 data = mpkt | (lmpkt << 16);
824 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
826 data = ((dmacnt & 0xff) << 16) | DCR1_EPN_DIR0 | DCR1_EPN_REQEN;
827 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
830 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, 0);
831 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_BURST_SET);
833 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT
835 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_BURST_SET);
837 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_DMA_EN);
839 result = length & ~(u32)0x03;
840 req->div_len = result;
845 /*-------------------------------------------------------------------------*/
846 static int _nbu2ss_epn_out_pio(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
847 struct nbu2ss_req *req, u32 length)
853 union usb_reg_access temp_32;
854 union usb_reg_access *p_buf_32;
856 struct fc_regs __iomem *preg = udc->p_regs;
859 return 1; /* DMA is forwarded */
864 p_buffer = (u8 *)req->req.buf;
865 p_buf_32 = (union usb_reg_access *)(p_buffer + req->req.actual);
867 i_word_length = length / sizeof(u32);
868 if (i_word_length > 0) {
869 /*---------------------------------------------------------*/
870 /* Copy of every four bytes */
871 for (i = 0; i < i_word_length; i++) {
873 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
876 result = i_word_length * sizeof(u32);
879 data = length - result;
881 /*---------------------------------------------------------*/
882 /* Copy of fraction byte */
884 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
885 for (i = 0 ; i < data ; i++)
886 p_buf_32->byte.DATA[i] = temp_32.byte.DATA[i];
890 req->req.actual += result;
892 if ((req->req.actual == req->req.length) ||
893 ((req->req.actual % ep->ep.maxpacket) != 0)) {
900 /*-------------------------------------------------------------------------*/
901 static int _nbu2ss_epn_out_data(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
902 struct nbu2ss_req *req, u32 data_size)
913 i_buf_size = min((req->req.length - req->req.actual), data_size);
915 if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
916 (i_buf_size >= sizeof(u32))) {
917 nret = _nbu2ss_out_dma(udc, req, num, i_buf_size);
919 i_buf_size = min_t(u32, i_buf_size, ep->ep.maxpacket);
920 nret = _nbu2ss_epn_out_pio(udc, ep, req, i_buf_size);
926 /*-------------------------------------------------------------------------*/
927 static int _nbu2ss_epn_out_transfer(struct nbu2ss_udc *udc,
928 struct nbu2ss_ep *ep,
929 struct nbu2ss_req *req)
934 struct fc_regs __iomem *preg = udc->p_regs;
941 /*-------------------------------------------------------------*/
944 _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT) & EPN_LDATA;
946 if (i_recv_length != 0) {
947 result = _nbu2ss_epn_out_data(udc, ep, req, i_recv_length);
948 if (i_recv_length < ep->ep.maxpacket) {
949 if (i_recv_length == result) {
950 req->req.actual += result;
955 if ((req->req.actual == req->req.length) ||
956 ((req->req.actual % ep->ep.maxpacket) != 0)) {
962 if ((req->req.actual % ep->ep.maxpacket) == 0) {
970 if (req->req.actual > req->req.length) {
971 dev_err(udc->dev, " Overrun Error\n");
972 dev_err(udc->dev, " actual = %d, length = %d\n",
973 req->req.actual, req->req.length);
980 /*-------------------------------------------------------------------------*/
981 static int _nbu2ss_in_dma(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
982 struct nbu2ss_req *req, u32 num, u32 length)
985 u32 mpkt; /* MaxPacketSize */
986 u32 lmpkt; /* Last Packet Data Size */
987 u32 dmacnt; /* IN Data Size */
990 int result = -EINVAL;
991 struct fc_regs __iomem *preg = udc->p_regs;
994 return 1; /* DMA is forwarded */
997 if (req->req.actual == 0)
998 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_IN);
1000 req->dma_flag = true;
1002 /* MAX Packet Size */
1003 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPN_MPKT;
1005 if ((DMA_MAX_COUNT * mpkt) < length)
1006 i_write_length = DMA_MAX_COUNT * mpkt;
1008 i_write_length = length;
1010 /*------------------------------------------------------------*/
1011 /* Number of transmission packets */
1012 if (mpkt < i_write_length) {
1013 dmacnt = i_write_length / mpkt;
1014 lmpkt = (i_write_length % mpkt) & ~(u32)0x3;
1018 lmpkt = mpkt & ~(u32)0x3;
1022 lmpkt = i_write_length & ~(u32)0x3;
1025 /* Packet setting */
1026 data = mpkt | (lmpkt << 16);
1027 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
1029 /* Address setting */
1030 p_buffer = req->req.dma;
1031 p_buffer += req->req.actual;
1032 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)p_buffer);
1034 /* Packet and DMA setting */
1035 data = ((dmacnt & 0xff) << 16) | DCR1_EPN_REQEN;
1036 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
1038 /* Packet setting of EPC */
1039 data = dmacnt << 16;
1040 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, data);
1042 /*DMA setting of EPC */
1043 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_DMA_EN);
1045 result = i_write_length & ~(u32)0x3;
1046 req->div_len = result;
1051 /*-------------------------------------------------------------------------*/
1052 static int _nbu2ss_epn_in_pio(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
1053 struct nbu2ss_req *req, u32 length)
1059 union usb_reg_access temp_32;
1060 union usb_reg_access *p_buf_32 = NULL;
1062 struct fc_regs __iomem *preg = udc->p_regs;
1065 return 1; /* DMA is forwarded */
1068 p_buffer = (u8 *)req->req.buf;
1069 p_buf_32 = (union usb_reg_access *)(p_buffer + req->req.actual);
1071 i_word_length = length / sizeof(u32);
1072 if (i_word_length > 0) {
1073 for (i = 0; i < i_word_length; i++) {
1075 &preg->EP_REGS[ep->epnum - 1].EP_WRITE
1081 result = i_word_length * sizeof(u32);
1085 if (result != ep->ep.maxpacket) {
1086 data = length - result;
1088 for (i = 0 ; i < data ; i++)
1089 temp_32.byte.DATA[i] = p_buf_32->byte.DATA[i];
1091 _nbu2ss_ep_in_end(udc, ep->epnum, temp_32.dw, data);
1095 req->div_len = result;
1100 /*-------------------------------------------------------------------------*/
1101 static int _nbu2ss_epn_in_data(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
1102 struct nbu2ss_req *req, u32 data_size)
1110 num = ep->epnum - 1;
1112 if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
1113 (data_size >= sizeof(u32))) {
1114 nret = _nbu2ss_in_dma(udc, ep, req, num, data_size);
1116 data_size = min_t(u32, data_size, ep->ep.maxpacket);
1117 nret = _nbu2ss_epn_in_pio(udc, ep, req, data_size);
1123 /*-------------------------------------------------------------------------*/
1124 static int _nbu2ss_epn_in_transfer(struct nbu2ss_udc *udc,
1125 struct nbu2ss_ep *ep, struct nbu2ss_req *req)
1135 num = ep->epnum - 1;
1137 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
1139 /*-------------------------------------------------------------*/
1140 /* State confirmation of FIFO */
1141 if (req->req.actual == 0) {
1142 if ((status & EPN_IN_EMPTY) == 0)
1143 return 1; /* Not Empty */
1146 if ((status & EPN_IN_FULL) != 0)
1147 return 1; /* Not Empty */
1150 /*-------------------------------------------------------------*/
1151 /* Start transfer */
1152 i_buf_size = req->req.length - req->req.actual;
1154 result = _nbu2ss_epn_in_data(udc, ep, req, i_buf_size);
1155 else if (req->req.length == 0)
1156 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1161 /*-------------------------------------------------------------------------*/
1162 static int _nbu2ss_start_transfer(struct nbu2ss_udc *udc,
1163 struct nbu2ss_ep *ep,
1164 struct nbu2ss_req *req,
1169 req->dma_flag = false;
1172 if (req->req.length == 0) {
1175 if ((req->req.length % ep->ep.maxpacket) == 0)
1176 req->zero = req->req.zero;
1181 if (ep->epnum == 0) {
1183 switch (udc->ep0state) {
1184 case EP0_IN_DATA_PHASE:
1185 nret = _nbu2ss_ep0_in_transfer(udc, req);
1188 case EP0_OUT_DATA_PHASE:
1189 nret = _nbu2ss_ep0_out_transfer(udc, req);
1192 case EP0_IN_STATUS_PHASE:
1193 nret = EP0_send_NULL(udc, true);
1202 if (ep->direct == USB_DIR_OUT) {
1205 nret = _nbu2ss_epn_out_transfer(udc, ep, req);
1208 nret = _nbu2ss_epn_in_transfer(udc, ep, req);
1215 /*-------------------------------------------------------------------------*/
1216 static void _nbu2ss_restert_transfer(struct nbu2ss_ep *ep)
1220 struct nbu2ss_req *req;
1222 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1226 if (ep->epnum > 0) {
1227 length = _nbu2ss_readl(
1228 &ep->udc->p_regs->EP_REGS[ep->epnum - 1].EP_LEN_DCNT);
1230 length &= EPN_LDATA;
1231 if (length < ep->ep.maxpacket)
1235 _nbu2ss_start_transfer(ep->udc, ep, req, bflag);
1238 /*-------------------------------------------------------------------------*/
1239 /* Endpoint Toggle Reset */
1240 static void _nbu2ss_endpoint_toggle_reset(struct nbu2ss_udc *udc, u8 ep_adrs)
1245 if ((ep_adrs == 0) || (ep_adrs == 0x80))
1248 num = (ep_adrs & 0x7F) - 1;
1250 if (ep_adrs & USB_DIR_IN)
1253 data = EPN_BCLR | EPN_OPIDCLR;
1255 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
1258 /*-------------------------------------------------------------------------*/
1259 /* Endpoint STALL set */
1260 static void _nbu2ss_set_endpoint_stall(struct nbu2ss_udc *udc,
1261 u8 ep_adrs, bool bstall)
1265 struct nbu2ss_ep *ep;
1266 struct fc_regs __iomem *preg = udc->p_regs;
1268 if ((ep_adrs == 0) || (ep_adrs == 0x80)) {
1271 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_STL);
1274 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_STL);
1277 epnum = ep_adrs & USB_ENDPOINT_NUMBER_MASK;
1279 ep = &udc->ep[epnum];
1285 if (ep_adrs & USB_DIR_IN)
1286 data = EPN_BCLR | EPN_ISTL;
1288 data = EPN_OSTL_EN | EPN_OSTL;
1290 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
1293 ep->stalled = false;
1294 if (ep_adrs & USB_DIR_IN) {
1295 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL
1299 _nbu2ss_readl(&preg->EP_REGS[num].EP_CONTROL);
1302 data |= EPN_OSTL_EN;
1304 _nbu2ss_writel(&preg->EP_REGS[num].EP_CONTROL
1308 ep->stalled = false;
1311 _nbu2ss_restert_transfer(ep);
1317 /*-------------------------------------------------------------------------*/
1318 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *udc, u32 mode)
1322 if (mode > MAX_TEST_MODE_NUM)
1325 dev_info(udc->dev, "SET FEATURE : test mode = %d\n", mode);
1327 data = _nbu2ss_readl(&udc->p_regs->USB_CONTROL);
1328 data &= ~TEST_FORCE_ENABLE;
1329 data |= mode << TEST_MODE_SHIFT;
1331 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, data);
1332 _nbu2ss_bitset(&udc->p_regs->TEST_CONTROL, CS_TESTMODEEN);
1335 /*-------------------------------------------------------------------------*/
1336 static int _nbu2ss_set_feature_device(struct nbu2ss_udc *udc,
1337 u16 selector, u16 wIndex)
1339 int result = -EOPNOTSUPP;
1342 case USB_DEVICE_REMOTE_WAKEUP:
1343 if (wIndex == 0x0000) {
1344 udc->remote_wakeup = U2F_ENABLE;
1349 case USB_DEVICE_TEST_MODE:
1351 if (wIndex <= MAX_TEST_MODE_NUM)
1362 /*-------------------------------------------------------------------------*/
1363 static int _nbu2ss_get_ep_stall(struct nbu2ss_udc *udc, u8 ep_adrs)
1366 u32 data = 0, bit_data;
1367 struct fc_regs __iomem *preg = udc->p_regs;
1369 epnum = ep_adrs & ~USB_ENDPOINT_DIR_MASK;
1371 data = _nbu2ss_readl(&preg->EP0_CONTROL);
1375 data = _nbu2ss_readl(&preg->EP_REGS[epnum - 1].EP_CONTROL);
1376 if ((data & EPN_EN) == 0)
1379 if (ep_adrs & USB_ENDPOINT_DIR_MASK)
1380 bit_data = EPN_ISTL;
1382 bit_data = EPN_OSTL;
1385 if ((data & bit_data) == 0)
1390 /*-------------------------------------------------------------------------*/
1391 static inline int _nbu2ss_req_feature(struct nbu2ss_udc *udc, bool bset)
1393 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1394 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1395 u16 selector = le16_to_cpu(udc->ctrl.wValue);
1396 u16 wIndex = le16_to_cpu(udc->ctrl.wIndex);
1398 int result = -EOPNOTSUPP;
1400 if ((udc->ctrl.wLength != 0x0000) ||
1401 (direction != USB_DIR_OUT)) {
1405 switch (recipient) {
1406 case USB_RECIP_DEVICE:
1409 _nbu2ss_set_feature_device(udc, selector, wIndex);
1412 case USB_RECIP_ENDPOINT:
1413 if (0x0000 == (wIndex & 0xFF70)) {
1414 if (selector == USB_ENDPOINT_HALT) {
1415 ep_adrs = wIndex & 0xFF;
1417 _nbu2ss_endpoint_toggle_reset(udc,
1421 _nbu2ss_set_endpoint_stall(udc, ep_adrs, bset);
1433 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1438 /*-------------------------------------------------------------------------*/
1439 static inline enum usb_device_speed _nbu2ss_get_speed(struct nbu2ss_udc *udc)
1442 enum usb_device_speed speed = USB_SPEED_FULL;
1444 data = _nbu2ss_readl(&udc->p_regs->USB_STATUS);
1445 if (data & HIGH_SPEED)
1446 speed = USB_SPEED_HIGH;
1451 /*-------------------------------------------------------------------------*/
1452 static void _nbu2ss_epn_set_stall(struct nbu2ss_udc *udc,
1453 struct nbu2ss_ep *ep)
1459 struct fc_regs __iomem *preg = udc->p_regs;
1461 if (ep->direct == USB_DIR_IN) {
1463 ; limit_cnt < IN_DATA_EMPTY_COUNT
1465 regdata = _nbu2ss_readl(
1466 &preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1468 if ((regdata & EPN_IN_DATA) == 0)
1475 ep_adrs = ep->epnum | ep->direct;
1476 _nbu2ss_set_endpoint_stall(udc, ep_adrs, 1);
1479 /*-------------------------------------------------------------------------*/
1480 static int std_req_get_status(struct nbu2ss_udc *udc)
1483 u16 status_data = 0;
1484 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1485 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1487 int result = -EINVAL;
1489 if ((udc->ctrl.wValue != 0x0000) || (direction != USB_DIR_IN))
1493 min_t(u16, le16_to_cpu(udc->ctrl.wLength), sizeof(status_data));
1494 switch (recipient) {
1495 case USB_RECIP_DEVICE:
1496 if (udc->ctrl.wIndex == 0x0000) {
1497 if (udc->gadget.is_selfpowered)
1498 status_data |= BIT(USB_DEVICE_SELF_POWERED);
1500 if (udc->remote_wakeup)
1501 status_data |= BIT(USB_DEVICE_REMOTE_WAKEUP);
1507 case USB_RECIP_ENDPOINT:
1508 if (0x0000 == (le16_to_cpu(udc->ctrl.wIndex) & 0xFF70)) {
1509 ep_adrs = (u8)(le16_to_cpu(udc->ctrl.wIndex) & 0xFF);
1510 result = _nbu2ss_get_ep_stall(udc, ep_adrs);
1513 status_data |= BIT(USB_ENDPOINT_HALT);
1522 memcpy(udc->ep0_buf, &status_data, length);
1523 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, length);
1524 _nbu2ss_ep0_in_transfer(udc, &udc->ep0_req);
1527 dev_err(udc->dev, " Error GET_STATUS\n");
1533 /*-------------------------------------------------------------------------*/
1534 static int std_req_clear_feature(struct nbu2ss_udc *udc)
1536 return _nbu2ss_req_feature(udc, false);
1539 /*-------------------------------------------------------------------------*/
1540 static int std_req_set_feature(struct nbu2ss_udc *udc)
1542 return _nbu2ss_req_feature(udc, true);
1545 /*-------------------------------------------------------------------------*/
1546 static int std_req_set_address(struct nbu2ss_udc *udc)
1549 u32 wValue = le16_to_cpu(udc->ctrl.wValue);
1551 if ((udc->ctrl.bRequestType != 0x00) ||
1552 (udc->ctrl.wIndex != 0x0000) ||
1553 (udc->ctrl.wLength != 0x0000)) {
1557 if (wValue != (wValue & 0x007F))
1560 wValue <<= USB_ADRS_SHIFT;
1562 _nbu2ss_writel(&udc->p_regs->USB_ADDRESS, wValue);
1563 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1568 /*-------------------------------------------------------------------------*/
1569 static int std_req_set_configuration(struct nbu2ss_udc *udc)
1571 u32 config_value = (u32)(le16_to_cpu(udc->ctrl.wValue) & 0x00ff);
1573 if ((udc->ctrl.wIndex != 0x0000) ||
1574 (udc->ctrl.wLength != 0x0000) ||
1575 (udc->ctrl.bRequestType != 0x00)) {
1579 udc->curr_config = config_value;
1581 if (config_value > 0) {
1582 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, CONF);
1583 udc->devstate = USB_STATE_CONFIGURED;
1586 _nbu2ss_bitclr(&udc->p_regs->USB_CONTROL, CONF);
1587 udc->devstate = USB_STATE_ADDRESS;
1593 /*-------------------------------------------------------------------------*/
1594 static inline void _nbu2ss_read_request_data(struct nbu2ss_udc *udc, u32 *pdata)
1596 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA0);
1598 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA1);
1601 /*-------------------------------------------------------------------------*/
1602 static inline int _nbu2ss_decode_request(struct nbu2ss_udc *udc)
1604 bool bcall_back = true;
1606 struct usb_ctrlrequest *p_ctrl;
1608 p_ctrl = &udc->ctrl;
1609 _nbu2ss_read_request_data(udc, (u32 *)p_ctrl);
1611 /* ep0 state control */
1612 if (p_ctrl->wLength == 0) {
1613 udc->ep0state = EP0_IN_STATUS_PHASE;
1616 if (p_ctrl->bRequestType & USB_DIR_IN)
1617 udc->ep0state = EP0_IN_DATA_PHASE;
1619 udc->ep0state = EP0_OUT_DATA_PHASE;
1622 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1623 switch (p_ctrl->bRequest) {
1624 case USB_REQ_GET_STATUS:
1625 nret = std_req_get_status(udc);
1629 case USB_REQ_CLEAR_FEATURE:
1630 nret = std_req_clear_feature(udc);
1634 case USB_REQ_SET_FEATURE:
1635 nret = std_req_set_feature(udc);
1639 case USB_REQ_SET_ADDRESS:
1640 nret = std_req_set_address(udc);
1644 case USB_REQ_SET_CONFIGURATION:
1645 nret = std_req_set_configuration(udc);
1654 if (udc->ep0state == EP0_IN_STATUS_PHASE) {
1656 /*--------------------------------------*/
1658 nret = EP0_send_NULL(udc, true);
1663 spin_unlock(&udc->lock);
1664 nret = udc->driver->setup(&udc->gadget, &udc->ctrl);
1665 spin_lock(&udc->lock);
1669 udc->ep0state = EP0_IDLE;
1674 /*-------------------------------------------------------------------------*/
1675 static inline int _nbu2ss_ep0_in_data_stage(struct nbu2ss_udc *udc)
1678 struct nbu2ss_req *req;
1679 struct nbu2ss_ep *ep = &udc->ep[0];
1681 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1683 req = &udc->ep0_req;
1685 req->req.actual += req->div_len;
1688 nret = _nbu2ss_ep0_in_transfer(udc, req);
1690 udc->ep0state = EP0_OUT_STATUS_PAHSE;
1691 EP0_receive_NULL(udc, true);
1697 /*-------------------------------------------------------------------------*/
1698 static inline int _nbu2ss_ep0_out_data_stage(struct nbu2ss_udc *udc)
1701 struct nbu2ss_req *req;
1702 struct nbu2ss_ep *ep = &udc->ep[0];
1704 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1706 req = &udc->ep0_req;
1708 nret = _nbu2ss_ep0_out_transfer(udc, req);
1710 udc->ep0state = EP0_IN_STATUS_PHASE;
1711 EP0_send_NULL(udc, true);
1713 } else if (nret < 0) {
1714 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, EP0_BCLR);
1715 req->req.status = nret;
1721 /*-------------------------------------------------------------------------*/
1722 static inline int _nbu2ss_ep0_status_stage(struct nbu2ss_udc *udc)
1724 struct nbu2ss_req *req;
1725 struct nbu2ss_ep *ep = &udc->ep[0];
1727 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1729 req = &udc->ep0_req;
1730 if (req->req.complete)
1731 req->req.complete(&ep->ep, &req->req);
1734 if (req->req.complete)
1735 _nbu2ss_ep_done(ep, req, 0);
1738 udc->ep0state = EP0_IDLE;
1743 /*-------------------------------------------------------------------------*/
1744 static inline void _nbu2ss_ep0_int(struct nbu2ss_udc *udc)
1751 status = _nbu2ss_readl(&udc->p_regs->EP0_STATUS);
1752 intr = status & EP0_STATUS_RW_BIT;
1753 _nbu2ss_writel(&udc->p_regs->EP0_STATUS, ~intr);
1755 status &= (SETUP_INT | EP0_IN_INT | EP0_OUT_INT
1756 | STG_END_INT | EP0_OUT_NULL_INT);
1759 dev_info(udc->dev, "%s Not Decode Interrupt\n", __func__);
1760 dev_info(udc->dev, "EP0_STATUS = 0x%08x\n", intr);
1764 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1765 udc->gadget.speed = _nbu2ss_get_speed(udc);
1767 for (i = 0; i < EP0_END_XFER; i++) {
1768 switch (udc->ep0state) {
1770 if (status & SETUP_INT) {
1772 nret = _nbu2ss_decode_request(udc);
1776 case EP0_IN_DATA_PHASE:
1777 if (status & EP0_IN_INT) {
1778 status &= ~EP0_IN_INT;
1779 nret = _nbu2ss_ep0_in_data_stage(udc);
1783 case EP0_OUT_DATA_PHASE:
1784 if (status & EP0_OUT_INT) {
1785 status &= ~EP0_OUT_INT;
1786 nret = _nbu2ss_ep0_out_data_stage(udc);
1790 case EP0_IN_STATUS_PHASE:
1791 if ((status & STG_END_INT) || (status & SETUP_INT)) {
1792 status &= ~(STG_END_INT | EP0_IN_INT);
1793 nret = _nbu2ss_ep0_status_stage(udc);
1797 case EP0_OUT_STATUS_PAHSE:
1798 if ((status & STG_END_INT) || (status & SETUP_INT) ||
1799 (status & EP0_OUT_NULL_INT)) {
1800 status &= ~(STG_END_INT
1802 | EP0_OUT_NULL_INT);
1804 nret = _nbu2ss_ep0_status_stage(udc);
1820 _nbu2ss_set_endpoint_stall(udc, 0, true);
1824 /*-------------------------------------------------------------------------*/
1825 static void _nbu2ss_ep_done(struct nbu2ss_ep *ep,
1826 struct nbu2ss_req *req,
1829 struct nbu2ss_udc *udc = ep->udc;
1831 list_del_init(&req->queue);
1833 if (status == -ECONNRESET)
1834 _nbu2ss_fifo_flush(udc, ep);
1836 if (likely(req->req.status == -EINPROGRESS))
1837 req->req.status = status;
1840 _nbu2ss_epn_set_stall(udc, ep);
1842 if (!list_empty(&ep->queue))
1843 _nbu2ss_restert_transfer(ep);
1847 if ((ep->direct == USB_DIR_OUT) && (ep->epnum > 0) &&
1848 (req->req.dma != 0))
1849 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_OUT);
1852 spin_unlock(&udc->lock);
1853 req->req.complete(&ep->ep, &req->req);
1854 spin_lock(&udc->lock);
1857 /*-------------------------------------------------------------------------*/
1858 static inline void _nbu2ss_epn_in_int(struct nbu2ss_udc *udc,
1859 struct nbu2ss_ep *ep,
1860 struct nbu2ss_req *req)
1865 struct fc_regs __iomem *preg = udc->p_regs;
1868 return; /* DMA is forwarded */
1870 req->req.actual += req->div_len;
1873 if (req->req.actual != req->req.length) {
1874 /*---------------------------------------------------------*/
1875 /* remainder of data */
1876 result = _nbu2ss_epn_in_transfer(udc, ep, req);
1879 if (req->zero && ((req->req.actual % ep->ep.maxpacket) == 0)) {
1881 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1883 if ((status & EPN_IN_FULL) == 0) {
1884 /*-----------------------------------------*/
1885 /* 0 Length Packet */
1887 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1894 /*---------------------------------------------------------*/
1896 _nbu2ss_ep_done(ep, req, result);
1900 /*-------------------------------------------------------------------------*/
1901 static inline void _nbu2ss_epn_out_int(struct nbu2ss_udc *udc,
1902 struct nbu2ss_ep *ep,
1903 struct nbu2ss_req *req)
1907 result = _nbu2ss_epn_out_transfer(udc, ep, req);
1909 _nbu2ss_ep_done(ep, req, result);
1912 /*-------------------------------------------------------------------------*/
1913 static inline void _nbu2ss_epn_in_dma_int(struct nbu2ss_udc *udc,
1914 struct nbu2ss_ep *ep,
1915 struct nbu2ss_req *req)
1919 struct usb_request *preq;
1926 preq->actual += req->div_len;
1928 req->dma_flag = false;
1931 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_IN);
1934 if (preq->actual != preq->length) {
1935 _nbu2ss_epn_in_transfer(udc, ep, req);
1937 mpkt = ep->ep.maxpacket;
1938 size = preq->actual % mpkt;
1940 if (((preq->actual & 0x03) == 0) && (size < mpkt))
1941 _nbu2ss_ep_in_end(udc, ep->epnum, 0, 0);
1943 _nbu2ss_epn_in_int(udc, ep, req);
1948 /*-------------------------------------------------------------------------*/
1949 static inline void _nbu2ss_epn_out_dma_int(struct nbu2ss_udc *udc,
1950 struct nbu2ss_ep *ep,
1951 struct nbu2ss_req *req)
1955 u32 dmacnt, ep_dmacnt;
1957 struct fc_regs __iomem *preg = udc->p_regs;
1959 num = ep->epnum - 1;
1961 if (req->req.actual == req->req.length) {
1962 if ((req->req.length % ep->ep.maxpacket) && !req->zero) {
1964 req->dma_flag = false;
1965 _nbu2ss_ep_done(ep, req, 0);
1970 ep_dmacnt = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT)
1974 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
1975 dmacnt = _nbu2ss_readl(&preg->EP_DCR[num].EP_DCR1)
1978 if (ep_dmacnt == dmacnt)
1982 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPN_REQEN);
1985 mpkt = ep->ep.maxpacket;
1986 if ((req->div_len % mpkt) == 0)
1987 req->div_len -= mpkt * dmacnt;
1990 if ((req->req.actual % ep->ep.maxpacket) > 0) {
1991 if (req->req.actual == req->div_len) {
1993 req->dma_flag = false;
1994 _nbu2ss_ep_done(ep, req, 0);
1999 req->req.actual += req->div_len;
2001 req->dma_flag = false;
2003 _nbu2ss_epn_out_int(udc, ep, req);
2006 /*-------------------------------------------------------------------------*/
2007 static inline void _nbu2ss_epn_int(struct nbu2ss_udc *udc, u32 epnum)
2012 struct nbu2ss_req *req;
2013 struct nbu2ss_ep *ep = &udc->ep[epnum];
2017 /* Interrupt Status */
2018 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
2020 /* Interrupt Clear */
2021 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_STATUS, ~status);
2023 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
2025 /* pr_warn("=== %s(%d) req == NULL\n", __func__, epnum); */
2029 if (status & EPN_OUT_END_INT) {
2030 status &= ~EPN_OUT_INT;
2031 _nbu2ss_epn_out_dma_int(udc, ep, req);
2034 if (status & EPN_OUT_INT)
2035 _nbu2ss_epn_out_int(udc, ep, req);
2037 if (status & EPN_IN_END_INT) {
2038 status &= ~EPN_IN_INT;
2039 _nbu2ss_epn_in_dma_int(udc, ep, req);
2042 if (status & EPN_IN_INT)
2043 _nbu2ss_epn_in_int(udc, ep, req);
2046 /*-------------------------------------------------------------------------*/
2047 static inline void _nbu2ss_ep_int(struct nbu2ss_udc *udc, u32 epnum)
2050 _nbu2ss_ep0_int(udc);
2052 _nbu2ss_epn_int(udc, epnum);
2055 /*-------------------------------------------------------------------------*/
2056 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *udc)
2058 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, (EP0_AUTO | EP0_BCLR));
2059 _nbu2ss_writel(&udc->p_regs->EP0_INT_ENA, EP0_INT_EN_BIT);
2062 /*-------------------------------------------------------------------------*/
2063 static int _nbu2ss_nuke(struct nbu2ss_udc *udc,
2064 struct nbu2ss_ep *ep,
2067 struct nbu2ss_req *req;
2069 /* Endpoint Disable */
2070 _nbu2ss_epn_exit(udc, ep);
2073 _nbu2ss_ep_dma_exit(udc, ep);
2075 if (list_empty(&ep->queue))
2078 /* called with irqs blocked */
2079 list_for_each_entry(req, &ep->queue, queue) {
2080 _nbu2ss_ep_done(ep, req, status);
2086 /*-------------------------------------------------------------------------*/
2087 static void _nbu2ss_quiesce(struct nbu2ss_udc *udc)
2089 struct nbu2ss_ep *ep;
2091 udc->gadget.speed = USB_SPEED_UNKNOWN;
2093 _nbu2ss_nuke(udc, &udc->ep[0], -ESHUTDOWN);
2096 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2097 _nbu2ss_nuke(udc, ep, -ESHUTDOWN);
2101 /*-------------------------------------------------------------------------*/
2102 static int _nbu2ss_pullup(struct nbu2ss_udc *udc, int is_on)
2106 if (udc->vbus_active == 0)
2112 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL)
2113 | PUE2) & ~(u32)CONNECTB;
2115 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2120 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL) | CONNECTB)
2123 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2124 udc->gadget.speed = USB_SPEED_UNKNOWN;
2130 /*-------------------------------------------------------------------------*/
2131 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
2133 struct fc_regs __iomem *p = udc->p_regs;
2135 if (udc->vbus_active == 0)
2138 if (ep->epnum == 0) {
2140 _nbu2ss_bitset(&p->EP0_CONTROL, EP0_BCLR);
2144 _nbu2ss_ep_dma_abort(udc, ep);
2145 _nbu2ss_bitset(&p->EP_REGS[ep->epnum - 1].EP_CONTROL, EPN_BCLR);
2149 /*-------------------------------------------------------------------------*/
2150 static int _nbu2ss_enable_controller(struct nbu2ss_udc *udc)
2154 if (udc->udc_enabled)
2158 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2159 udelay(EPC_RST_DISABLE_TIME); /* 1us wait */
2161 _nbu2ss_bitclr(&udc->p_regs->EPCTR, DIRPD);
2162 mdelay(EPC_DIRPD_DISABLE_TIME); /* 1ms wait */
2164 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2166 _nbu2ss_writel(&udc->p_regs->AHBSCTR, WAIT_MODE);
2168 _nbu2ss_writel(&udc->p_regs->AHBMCTR,
2169 HBUSREQ_MODE | HTRANS_MODE | WBURST_TYPE);
2171 while (!(_nbu2ss_readl(&udc->p_regs->EPCTR) & PLL_LOCK)) {
2173 udelay(1); /* 1us wait */
2174 if (waitcnt == EPC_PLL_LOCK_COUNT) {
2175 dev_err(udc->dev, "*** Reset Cancel failed\n");
2180 _nbu2ss_bitset(&udc->p_regs->UTMI_CHARACTER_1, USB_SQUSET);
2182 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, (INT_SEL | SOF_RCV));
2185 _nbu2ss_ep0_enable(udc);
2187 /* USB Interrupt Enable */
2188 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, USB_INT_EN_BIT);
2190 udc->udc_enabled = true;
2195 /*-------------------------------------------------------------------------*/
2196 static void _nbu2ss_reset_controller(struct nbu2ss_udc *udc)
2198 _nbu2ss_bitset(&udc->p_regs->EPCTR, EPC_RST);
2199 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2202 /*-------------------------------------------------------------------------*/
2203 static void _nbu2ss_disable_controller(struct nbu2ss_udc *udc)
2205 if (udc->udc_enabled) {
2206 udc->udc_enabled = false;
2207 _nbu2ss_reset_controller(udc);
2208 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2212 /*-------------------------------------------------------------------------*/
2213 static inline void _nbu2ss_check_vbus(struct nbu2ss_udc *udc)
2219 mdelay(VBUS_CHATTERING_MDELAY); /* wait (ms) */
2222 reg_dt = gpiod_get_value(vbus_gpio);
2224 udc->linux_suspended = 0;
2226 _nbu2ss_reset_controller(udc);
2227 dev_info(udc->dev, " ----- VBUS OFF\n");
2229 if (udc->vbus_active == 1) {
2231 udc->vbus_active = 0;
2232 if (udc->usb_suspended) {
2233 udc->usb_suspended = 0;
2234 /* _nbu2ss_reset_controller(udc); */
2236 udc->devstate = USB_STATE_NOTATTACHED;
2238 _nbu2ss_quiesce(udc);
2240 spin_unlock(&udc->lock);
2241 udc->driver->disconnect(&udc->gadget);
2242 spin_lock(&udc->lock);
2245 _nbu2ss_disable_controller(udc);
2248 mdelay(5); /* wait (5ms) */
2249 reg_dt = gpiod_get_value(vbus_gpio);
2253 dev_info(udc->dev, " ----- VBUS ON\n");
2255 if (udc->linux_suspended)
2258 if (udc->vbus_active == 0) {
2260 udc->vbus_active = 1;
2261 udc->devstate = USB_STATE_POWERED;
2263 nret = _nbu2ss_enable_controller(udc);
2265 _nbu2ss_disable_controller(udc);
2266 udc->vbus_active = 0;
2270 _nbu2ss_pullup(udc, 1);
2272 #ifdef UDC_DEBUG_DUMP
2273 _nbu2ss_dump_register(udc);
2274 #endif /* UDC_DEBUG_DUMP */
2277 if (udc->devstate == USB_STATE_POWERED)
2278 _nbu2ss_pullup(udc, 1);
2283 /*-------------------------------------------------------------------------*/
2284 static inline void _nbu2ss_int_bus_reset(struct nbu2ss_udc *udc)
2286 udc->devstate = USB_STATE_DEFAULT;
2287 udc->remote_wakeup = 0;
2289 _nbu2ss_quiesce(udc);
2291 udc->ep0state = EP0_IDLE;
2294 /*-------------------------------------------------------------------------*/
2295 static inline void _nbu2ss_int_usb_resume(struct nbu2ss_udc *udc)
2297 if (udc->usb_suspended == 1) {
2298 udc->usb_suspended = 0;
2299 if (udc->driver && udc->driver->resume) {
2300 spin_unlock(&udc->lock);
2301 udc->driver->resume(&udc->gadget);
2302 spin_lock(&udc->lock);
2307 /*-------------------------------------------------------------------------*/
2308 static inline void _nbu2ss_int_usb_suspend(struct nbu2ss_udc *udc)
2312 if (udc->usb_suspended == 0) {
2313 reg_dt = gpiod_get_value(vbus_gpio);
2318 udc->usb_suspended = 1;
2319 if (udc->driver && udc->driver->suspend) {
2320 spin_unlock(&udc->lock);
2321 udc->driver->suspend(&udc->gadget);
2322 spin_lock(&udc->lock);
2325 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, SUSPEND);
2329 /*-------------------------------------------------------------------------*/
2330 /* VBUS (GPIO153) Interrupt */
2331 static irqreturn_t _nbu2ss_vbus_irq(int irq, void *_udc)
2333 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2335 spin_lock(&udc->lock);
2336 _nbu2ss_check_vbus(udc);
2337 spin_unlock(&udc->lock);
2342 /*-------------------------------------------------------------------------*/
2343 /* Interrupt (udc) */
2344 static irqreturn_t _nbu2ss_udc_irq(int irq, void *_udc)
2346 u8 suspend_flag = 0;
2350 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2351 struct fc_regs __iomem *preg = udc->p_regs;
2353 if (gpiod_get_value(vbus_gpio) == 0) {
2354 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2355 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2359 spin_lock(&udc->lock);
2362 if (gpiod_get_value(vbus_gpio) == 0) {
2363 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2364 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2367 status = _nbu2ss_readl(&preg->USB_INT_STA);
2373 _nbu2ss_writel(&preg->USB_INT_STA, ~(status & USB_INT_STA_RW));
2375 if (status & USB_RST_INT) {
2377 _nbu2ss_int_bus_reset(udc);
2380 if (status & RSUM_INT) {
2382 _nbu2ss_int_usb_resume(udc);
2385 if (status & SPND_INT) {
2390 if (status & EPN_INT) {
2392 int_bit = status >> 8;
2394 for (epnum = 0; epnum < NUM_ENDPOINTS; epnum++) {
2396 _nbu2ss_ep_int(udc, epnum);
2407 _nbu2ss_int_usb_suspend(udc);
2409 spin_unlock(&udc->lock);
2414 /*-------------------------------------------------------------------------*/
2416 static int nbu2ss_ep_enable(struct usb_ep *_ep,
2417 const struct usb_endpoint_descriptor *desc)
2420 unsigned long flags;
2422 struct nbu2ss_ep *ep;
2423 struct nbu2ss_udc *udc;
2425 if (!_ep || !desc) {
2426 pr_err(" *** %s, bad param\n", __func__);
2430 ep = container_of(_ep, struct nbu2ss_ep, ep);
2432 pr_err(" *** %s, ep == NULL !!\n", __func__);
2436 ep_type = usb_endpoint_type(desc);
2437 if ((ep_type == USB_ENDPOINT_XFER_CONTROL) ||
2438 (ep_type == USB_ENDPOINT_XFER_ISOC)) {
2439 pr_err(" *** %s, bat bmAttributes\n", __func__);
2444 if (udc->vbus_active == 0)
2447 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
2448 dev_err(ep->udc->dev, " *** %s, udc !!\n", __func__);
2452 spin_lock_irqsave(&udc->lock, flags);
2455 ep->epnum = usb_endpoint_num(desc);
2456 ep->direct = desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2457 ep->ep_type = ep_type;
2460 ep->stalled = false;
2462 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2465 _nbu2ss_ep_dma_init(udc, ep);
2467 /* Endpoint setting */
2468 _nbu2ss_ep_init(udc, ep);
2470 spin_unlock_irqrestore(&udc->lock, flags);
2475 /*-------------------------------------------------------------------------*/
2476 static int nbu2ss_ep_disable(struct usb_ep *_ep)
2478 struct nbu2ss_ep *ep;
2479 struct nbu2ss_udc *udc;
2480 unsigned long flags;
2483 pr_err(" *** %s, bad param\n", __func__);
2487 ep = container_of(_ep, struct nbu2ss_ep, ep);
2489 pr_err("udc: *** %s, ep == NULL !!\n", __func__);
2494 if (udc->vbus_active == 0)
2497 spin_lock_irqsave(&udc->lock, flags);
2498 _nbu2ss_nuke(udc, ep, -EINPROGRESS); /* dequeue request */
2499 spin_unlock_irqrestore(&udc->lock, flags);
2504 /*-------------------------------------------------------------------------*/
2505 static struct usb_request *nbu2ss_ep_alloc_request(struct usb_ep *ep,
2508 struct nbu2ss_req *req;
2510 req = kzalloc(sizeof(*req), gfp_flags);
2515 req->req.dma = DMA_ADDR_INVALID;
2517 INIT_LIST_HEAD(&req->queue);
2522 /*-------------------------------------------------------------------------*/
2523 static void nbu2ss_ep_free_request(struct usb_ep *_ep,
2524 struct usb_request *_req)
2526 struct nbu2ss_req *req;
2529 req = container_of(_req, struct nbu2ss_req, req);
2535 /*-------------------------------------------------------------------------*/
2536 static int nbu2ss_ep_queue(struct usb_ep *_ep,
2537 struct usb_request *_req, gfp_t gfp_flags)
2539 struct nbu2ss_req *req;
2540 struct nbu2ss_ep *ep;
2541 struct nbu2ss_udc *udc;
2542 unsigned long flags;
2544 int result = -EINVAL;
2546 /* catch various bogus parameters */
2547 if (!_ep || !_req) {
2549 pr_err("udc: %s --- _ep == NULL\n", __func__);
2552 pr_err("udc: %s --- _req == NULL\n", __func__);
2557 req = container_of(_req, struct nbu2ss_req, req);
2558 if (unlikely(!_req->complete ||
2560 !list_empty(&req->queue))) {
2561 if (!_req->complete)
2562 pr_err("udc: %s --- !_req->complete\n", __func__);
2565 pr_err("udc:%s --- !_req->buf\n", __func__);
2567 if (!list_empty(&req->queue))
2568 pr_err("%s --- !list_empty(&req->queue)\n", __func__);
2573 ep = container_of(_ep, struct nbu2ss_ep, ep);
2576 if (udc->vbus_active == 0) {
2577 dev_info(udc->dev, "Can't ep_queue (VBUS OFF)\n");
2581 if (unlikely(!udc->driver)) {
2582 dev_err(udc->dev, "%s, bogus device state %p\n", __func__,
2587 spin_lock_irqsave(&udc->lock, flags);
2590 if ((uintptr_t)req->req.buf & 0x3)
2591 req->unaligned = true;
2593 req->unaligned = false;
2595 if (req->unaligned) {
2597 ep->virt_buf = dma_alloc_coherent(NULL, PAGE_SIZE,
2599 GFP_ATOMIC | GFP_DMA);
2600 if (ep->epnum > 0) {
2601 if (ep->direct == USB_DIR_IN)
2602 memcpy(ep->virt_buf, req->req.buf,
2607 if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT) &&
2608 (req->req.dma != 0))
2609 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_OUT);
2612 _req->status = -EINPROGRESS;
2615 bflag = list_empty(&ep->queue);
2616 list_add_tail(&req->queue, &ep->queue);
2618 if (bflag && !ep->stalled) {
2619 result = _nbu2ss_start_transfer(udc, ep, req, false);
2621 dev_err(udc->dev, " *** %s, result = %d\n", __func__,
2623 list_del(&req->queue);
2624 } else if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT)) {
2626 if (req->req.length < 4 &&
2627 req->req.length == req->req.actual)
2629 if (req->req.length == req->req.actual)
2631 _nbu2ss_ep_done(ep, req, result);
2635 spin_unlock_irqrestore(&udc->lock, flags);
2640 /*-------------------------------------------------------------------------*/
2641 static int nbu2ss_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2643 struct nbu2ss_req *req;
2644 struct nbu2ss_ep *ep;
2645 struct nbu2ss_udc *udc;
2646 unsigned long flags;
2648 /* catch various bogus parameters */
2649 if (!_ep || !_req) {
2650 /* pr_err("%s, bad param(1)\n", __func__); */
2654 ep = container_of(_ep, struct nbu2ss_ep, ep);
2660 spin_lock_irqsave(&udc->lock, flags);
2662 /* make sure it's actually queued on this endpoint */
2663 list_for_each_entry(req, &ep->queue, queue) {
2664 if (&req->req == _req)
2667 if (&req->req != _req) {
2668 spin_unlock_irqrestore(&udc->lock, flags);
2669 pr_debug("%s no queue(EINVAL)\n", __func__);
2673 _nbu2ss_ep_done(ep, req, -ECONNRESET);
2675 spin_unlock_irqrestore(&udc->lock, flags);
2680 /*-------------------------------------------------------------------------*/
2681 static int nbu2ss_ep_set_halt(struct usb_ep *_ep, int value)
2684 unsigned long flags;
2686 struct nbu2ss_ep *ep;
2687 struct nbu2ss_udc *udc;
2690 pr_err("%s, bad param\n", __func__);
2694 ep = container_of(_ep, struct nbu2ss_ep, ep);
2698 dev_err(ep->udc->dev, " *** %s, bad udc\n", __func__);
2702 spin_lock_irqsave(&udc->lock, flags);
2704 ep_adrs = ep->epnum | ep->direct;
2706 _nbu2ss_set_endpoint_stall(udc, ep_adrs, value);
2707 ep->stalled = false;
2709 if (list_empty(&ep->queue))
2710 _nbu2ss_epn_set_stall(udc, ep);
2718 spin_unlock_irqrestore(&udc->lock, flags);
2723 static int nbu2ss_ep_set_wedge(struct usb_ep *_ep)
2725 return nbu2ss_ep_set_halt(_ep, 1);
2728 /*-------------------------------------------------------------------------*/
2729 static int nbu2ss_ep_fifo_status(struct usb_ep *_ep)
2732 struct nbu2ss_ep *ep;
2733 struct nbu2ss_udc *udc;
2734 unsigned long flags;
2735 struct fc_regs __iomem *preg;
2738 pr_err("%s, bad param\n", __func__);
2742 ep = container_of(_ep, struct nbu2ss_ep, ep);
2746 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2752 data = gpiod_get_value(vbus_gpio);
2756 spin_lock_irqsave(&udc->lock, flags);
2758 if (ep->epnum == 0) {
2759 data = _nbu2ss_readl(&preg->EP0_LENGTH) & EP0_LDATA;
2762 data = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_LEN_DCNT)
2766 spin_unlock_irqrestore(&udc->lock, flags);
2771 /*-------------------------------------------------------------------------*/
2772 static void nbu2ss_ep_fifo_flush(struct usb_ep *_ep)
2775 struct nbu2ss_ep *ep;
2776 struct nbu2ss_udc *udc;
2777 unsigned long flags;
2780 pr_err("udc: %s, bad param\n", __func__);
2784 ep = container_of(_ep, struct nbu2ss_ep, ep);
2788 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2792 data = gpiod_get_value(vbus_gpio);
2796 spin_lock_irqsave(&udc->lock, flags);
2797 _nbu2ss_fifo_flush(udc, ep);
2798 spin_unlock_irqrestore(&udc->lock, flags);
2801 /*-------------------------------------------------------------------------*/
2802 static const struct usb_ep_ops nbu2ss_ep_ops = {
2803 .enable = nbu2ss_ep_enable,
2804 .disable = nbu2ss_ep_disable,
2806 .alloc_request = nbu2ss_ep_alloc_request,
2807 .free_request = nbu2ss_ep_free_request,
2809 .queue = nbu2ss_ep_queue,
2810 .dequeue = nbu2ss_ep_dequeue,
2812 .set_halt = nbu2ss_ep_set_halt,
2813 .set_wedge = nbu2ss_ep_set_wedge,
2815 .fifo_status = nbu2ss_ep_fifo_status,
2816 .fifo_flush = nbu2ss_ep_fifo_flush,
2819 /*-------------------------------------------------------------------------*/
2820 /* usb_gadget_ops */
2822 /*-------------------------------------------------------------------------*/
2823 static int nbu2ss_gad_get_frame(struct usb_gadget *pgadget)
2826 struct nbu2ss_udc *udc;
2829 pr_err("udc: %s, bad param\n", __func__);
2833 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2834 data = gpiod_get_value(vbus_gpio);
2838 return _nbu2ss_readl(&udc->p_regs->USB_ADDRESS) & FRAME;
2841 /*-------------------------------------------------------------------------*/
2842 static int nbu2ss_gad_wakeup(struct usb_gadget *pgadget)
2847 struct nbu2ss_udc *udc;
2850 pr_err("%s, bad param\n", __func__);
2854 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2856 data = gpiod_get_value(vbus_gpio);
2858 dev_warn(&pgadget->dev, "VBUS LEVEL = %d\n", data);
2862 _nbu2ss_bitset(&udc->p_regs->EPCTR, PLL_RESUME);
2864 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2865 data = _nbu2ss_readl(&udc->p_regs->EPCTR);
2867 if (data & PLL_LOCK)
2871 _nbu2ss_bitclr(&udc->p_regs->EPCTR, PLL_RESUME);
2876 /*-------------------------------------------------------------------------*/
2877 static int nbu2ss_gad_set_selfpowered(struct usb_gadget *pgadget,
2880 struct nbu2ss_udc *udc;
2881 unsigned long flags;
2884 pr_err("%s, bad param\n", __func__);
2888 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2890 spin_lock_irqsave(&udc->lock, flags);
2891 pgadget->is_selfpowered = (is_selfpowered != 0);
2892 spin_unlock_irqrestore(&udc->lock, flags);
2897 /*-------------------------------------------------------------------------*/
2898 static int nbu2ss_gad_vbus_session(struct usb_gadget *pgadget, int is_active)
2903 /*-------------------------------------------------------------------------*/
2904 static int nbu2ss_gad_vbus_draw(struct usb_gadget *pgadget, unsigned int mA)
2906 struct nbu2ss_udc *udc;
2907 unsigned long flags;
2910 pr_err("%s, bad param\n", __func__);
2914 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2916 spin_lock_irqsave(&udc->lock, flags);
2918 spin_unlock_irqrestore(&udc->lock, flags);
2923 /*-------------------------------------------------------------------------*/
2924 static int nbu2ss_gad_pullup(struct usb_gadget *pgadget, int is_on)
2926 struct nbu2ss_udc *udc;
2927 unsigned long flags;
2930 pr_err("%s, bad param\n", __func__);
2934 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2937 pr_warn("%s, Not Regist Driver\n", __func__);
2941 if (udc->vbus_active == 0)
2944 spin_lock_irqsave(&udc->lock, flags);
2945 _nbu2ss_pullup(udc, is_on);
2946 spin_unlock_irqrestore(&udc->lock, flags);
2951 /*-------------------------------------------------------------------------*/
2952 static int nbu2ss_gad_ioctl(struct usb_gadget *pgadget,
2953 unsigned int code, unsigned long param)
2958 static const struct usb_gadget_ops nbu2ss_gadget_ops = {
2959 .get_frame = nbu2ss_gad_get_frame,
2960 .wakeup = nbu2ss_gad_wakeup,
2961 .set_selfpowered = nbu2ss_gad_set_selfpowered,
2962 .vbus_session = nbu2ss_gad_vbus_session,
2963 .vbus_draw = nbu2ss_gad_vbus_draw,
2964 .pullup = nbu2ss_gad_pullup,
2965 .ioctl = nbu2ss_gad_ioctl,
2968 static const struct {
2970 const struct usb_ep_caps caps;
2971 } ep_info[NUM_ENDPOINTS] = {
2972 #define EP_INFO(_name, _caps) \
2979 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
2981 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2983 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2984 EP_INFO("ep3in-int",
2985 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
2987 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2989 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2991 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2993 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2994 EP_INFO("ep8in-int",
2995 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
2997 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2999 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
3001 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3003 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3004 EP_INFO("epdin-int",
3005 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3010 /*-------------------------------------------------------------------------*/
3011 static void nbu2ss_drv_ep_init(struct nbu2ss_udc *udc)
3015 INIT_LIST_HEAD(&udc->gadget.ep_list);
3016 udc->gadget.ep0 = &udc->ep[0].ep;
3018 for (i = 0; i < NUM_ENDPOINTS; i++) {
3019 struct nbu2ss_ep *ep = &udc->ep[i];
3024 ep->ep.driver_data = NULL;
3025 ep->ep.name = ep_info[i].name;
3026 ep->ep.caps = ep_info[i].caps;
3027 ep->ep.ops = &nbu2ss_ep_ops;
3029 usb_ep_set_maxpacket_limit(&ep->ep,
3030 i == 0 ? EP0_PACKETSIZE
3033 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
3034 INIT_LIST_HEAD(&ep->queue);
3037 list_del_init(&udc->ep[0].ep.ep_list);
3040 /*-------------------------------------------------------------------------*/
3041 /* platform_driver */
3042 static int nbu2ss_drv_contest_init(struct platform_device *pdev,
3043 struct nbu2ss_udc *udc)
3045 spin_lock_init(&udc->lock);
3046 udc->dev = &pdev->dev;
3048 udc->gadget.is_selfpowered = 1;
3049 udc->devstate = USB_STATE_NOTATTACHED;
3053 udc->pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3056 nbu2ss_drv_ep_init(udc);
3059 udc->gadget.ops = &nbu2ss_gadget_ops;
3060 udc->gadget.ep0 = &udc->ep[0].ep;
3061 udc->gadget.speed = USB_SPEED_UNKNOWN;
3062 udc->gadget.name = driver_name;
3063 /* udc->gadget.is_dualspeed = 1; */
3065 device_initialize(&udc->gadget.dev);
3067 dev_set_name(&udc->gadget.dev, "gadget");
3068 udc->gadget.dev.parent = &pdev->dev;
3069 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
3075 * probe - binds to the platform device
3077 static int nbu2ss_drv_probe(struct platform_device *pdev)
3079 int status = -ENODEV;
3080 struct nbu2ss_udc *udc;
3083 void __iomem *mmio_base;
3085 udc = &udc_controller;
3086 memset(udc, 0, sizeof(struct nbu2ss_udc));
3088 platform_set_drvdata(pdev, udc);
3090 /* require I/O memory and IRQ to be provided as resources */
3091 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3092 mmio_base = devm_ioremap_resource(&pdev->dev, r);
3093 if (IS_ERR(mmio_base))
3094 return PTR_ERR(mmio_base);
3096 irq = platform_get_irq(pdev, 0);
3099 status = devm_request_irq(&pdev->dev, irq, _nbu2ss_udc_irq,
3100 0, driver_name, udc);
3103 udc->p_regs = (struct fc_regs __iomem *)mmio_base;
3105 /* USB Function Controller Interrupt */
3107 dev_err(udc->dev, "request_irq(USB_UDC_IRQ_1) failed\n");
3111 /* Driver Initialization */
3112 status = nbu2ss_drv_contest_init(pdev, udc);
3118 /* VBUS Interrupt */
3119 vbus_irq = gpiod_to_irq(vbus_gpio);
3120 irq_set_irq_type(vbus_irq, IRQ_TYPE_EDGE_BOTH);
3121 status = request_irq(vbus_irq,
3122 _nbu2ss_vbus_irq, IRQF_SHARED, driver_name, udc);
3125 dev_err(udc->dev, "request_irq(vbus_irq) failed\n");
3132 /*-------------------------------------------------------------------------*/
3133 static void nbu2ss_drv_shutdown(struct platform_device *pdev)
3135 struct nbu2ss_udc *udc;
3137 udc = platform_get_drvdata(pdev);
3141 _nbu2ss_disable_controller(udc);
3144 /*-------------------------------------------------------------------------*/
3145 static int nbu2ss_drv_remove(struct platform_device *pdev)
3147 struct nbu2ss_udc *udc;
3148 struct nbu2ss_ep *ep;
3151 udc = &udc_controller;
3153 for (i = 0; i < NUM_ENDPOINTS; i++) {
3156 dma_free_coherent(NULL, PAGE_SIZE, (void *)ep->virt_buf,
3160 /* Interrupt Handler - Release */
3161 free_irq(vbus_irq, udc);
3166 /*-------------------------------------------------------------------------*/
3167 static int nbu2ss_drv_suspend(struct platform_device *pdev, pm_message_t state)
3169 struct nbu2ss_udc *udc;
3171 udc = platform_get_drvdata(pdev);
3175 if (udc->vbus_active) {
3176 udc->vbus_active = 0;
3177 udc->devstate = USB_STATE_NOTATTACHED;
3178 udc->linux_suspended = 1;
3180 if (udc->usb_suspended) {
3181 udc->usb_suspended = 0;
3182 _nbu2ss_reset_controller(udc);
3185 _nbu2ss_quiesce(udc);
3187 _nbu2ss_disable_controller(udc);
3192 /*-------------------------------------------------------------------------*/
3193 static int nbu2ss_drv_resume(struct platform_device *pdev)
3196 struct nbu2ss_udc *udc;
3198 udc = platform_get_drvdata(pdev);
3202 data = gpiod_get_value(vbus_gpio);
3204 udc->vbus_active = 1;
3205 udc->devstate = USB_STATE_POWERED;
3206 _nbu2ss_enable_controller(udc);
3207 _nbu2ss_pullup(udc, 1);
3210 udc->linux_suspended = 0;
3215 static struct platform_driver udc_driver = {
3216 .probe = nbu2ss_drv_probe,
3217 .shutdown = nbu2ss_drv_shutdown,
3218 .remove = nbu2ss_drv_remove,
3219 .suspend = nbu2ss_drv_suspend,
3220 .resume = nbu2ss_drv_resume,
3222 .name = driver_name,
3226 module_platform_driver(udc_driver);
3228 MODULE_DESCRIPTION(DRIVER_DESC);
3229 MODULE_AUTHOR("Renesas Electronics Corporation");
3230 MODULE_LICENSE("GPL");