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 struct gpio_desc *vbus_gpio;
40 static const char driver_name[] = "emxx_udc";
42 /*===========================================================================*/
44 static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *, struct nbu2ss_ep *);
45 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *);
46 /*static void _nbu2ss_ep0_disable(struct nbu2ss_udc *);*/
47 static void _nbu2ss_ep_done(struct nbu2ss_ep *, struct nbu2ss_req *, int);
48 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *, u32 mode);
49 static void _nbu2ss_endpoint_toggle_reset(struct nbu2ss_udc *udc, u8 ep_adrs);
51 static int _nbu2ss_pullup(struct nbu2ss_udc *, int);
52 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *, struct nbu2ss_ep *);
54 /*===========================================================================*/
56 #define _nbu2ss_zero_len_pkt(udc, epnum) \
57 _nbu2ss_ep_in_end(udc, epnum, 0, 0)
59 /*===========================================================================*/
61 static struct nbu2ss_udc udc_controller;
63 /*-------------------------------------------------------------------------*/
65 static inline u32 _nbu2ss_readl(void __iomem *address)
67 return __raw_readl(address);
70 /*-------------------------------------------------------------------------*/
72 static inline void _nbu2ss_writel(void __iomem *address, u32 udata)
74 __raw_writel(udata, address);
77 /*-------------------------------------------------------------------------*/
79 static inline void _nbu2ss_bitset(void __iomem *address, u32 udata)
81 u32 reg_dt = __raw_readl(address) | (udata);
83 __raw_writel(reg_dt, address);
86 /*-------------------------------------------------------------------------*/
88 static inline void _nbu2ss_bitclr(void __iomem *address, u32 udata)
90 u32 reg_dt = __raw_readl(address) & ~(udata);
92 __raw_writel(reg_dt, address);
96 /*-------------------------------------------------------------------------*/
97 static void _nbu2ss_dump_register(struct nbu2ss_udc *udc)
102 pr_info("=== %s()\n", __func__);
105 pr_err("%s udc == NULL\n", __func__);
109 spin_unlock(&udc->lock);
111 dev_dbg(&udc->dev, "\n-USB REG-\n");
112 for (i = 0x0 ; i < USB_BASE_SIZE ; i += 16) {
113 reg_data = _nbu2ss_readl(IO_ADDRESS(USB_BASE_ADDRESS + i));
114 dev_dbg(&udc->dev, "USB%04x =%08x", i, (int)reg_data);
116 reg_data = _nbu2ss_readl(IO_ADDRESS(USB_BASE_ADDRESS + i + 4));
117 dev_dbg(&udc->dev, " %08x", (int)reg_data);
119 reg_data = _nbu2ss_readl(IO_ADDRESS(USB_BASE_ADDRESS + i + 8));
120 dev_dbg(&udc->dev, " %08x", (int)reg_data);
122 reg_data = _nbu2ss_readl(IO_ADDRESS(USB_BASE_ADDRESS + i + 12));
123 dev_dbg(&udc->dev, " %08x\n", (int)reg_data);
126 spin_lock(&udc->lock);
128 #endif /* UDC_DEBUG_DUMP */
130 /*-------------------------------------------------------------------------*/
131 /* Endpoint 0 Callback (Complete) */
132 static void _nbu2ss_ep0_complete(struct usb_ep *_ep, struct usb_request *_req)
138 struct usb_ctrlrequest *p_ctrl;
139 struct nbu2ss_udc *udc;
144 udc = (struct nbu2ss_udc *)_req->context;
146 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
147 if (p_ctrl->bRequest == USB_REQ_SET_FEATURE) {
148 /*-------------------------------------------------*/
150 recipient = (u8)(p_ctrl->bRequestType & USB_RECIP_MASK);
151 selector = le16_to_cpu(p_ctrl->wValue);
152 if ((recipient == USB_RECIP_DEVICE) &&
153 (selector == USB_DEVICE_TEST_MODE)) {
154 wIndex = le16_to_cpu(p_ctrl->wIndex);
155 test_mode = (u32)(wIndex >> 8);
156 _nbu2ss_set_test_mode(udc, test_mode);
162 /*-------------------------------------------------------------------------*/
163 /* Initialization usb_request */
164 static void _nbu2ss_create_ep0_packet(struct nbu2ss_udc *udc,
165 void *p_buf, unsigned int length)
167 udc->ep0_req.req.buf = p_buf;
168 udc->ep0_req.req.length = length;
169 udc->ep0_req.req.dma = 0;
170 udc->ep0_req.req.zero = true;
171 udc->ep0_req.req.complete = _nbu2ss_ep0_complete;
172 udc->ep0_req.req.status = -EINPROGRESS;
173 udc->ep0_req.req.context = udc;
174 udc->ep0_req.req.actual = 0;
177 /*-------------------------------------------------------------------------*/
178 /* Acquisition of the first address of RAM(FIFO) */
179 static u32 _nbu2ss_get_begin_ram_address(struct nbu2ss_udc *udc)
182 u32 data, last_ram_adr, use_ram_size;
184 struct ep_regs __iomem *p_ep_regs;
186 last_ram_adr = (D_RAM_SIZE_CTRL / sizeof(u32)) * 2;
189 for (num = 0; num < NUM_ENDPOINTS - 1; num++) {
190 p_ep_regs = &udc->p_regs->EP_REGS[num];
191 data = _nbu2ss_readl(&p_ep_regs->EP_PCKT_ADRS);
192 buf_type = _nbu2ss_readl(&p_ep_regs->EP_CONTROL) & EPN_BUF_TYPE;
195 use_ram_size += (data & EPN_MPKT) / sizeof(u32);
198 use_ram_size += ((data & EPN_MPKT) / sizeof(u32)) * 2;
201 if ((data >> 16) > last_ram_adr)
202 last_ram_adr = data >> 16;
205 return last_ram_adr + use_ram_size;
208 /*-------------------------------------------------------------------------*/
209 /* Construction of Endpoint */
210 static int _nbu2ss_ep_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
221 /*-------------------------------------------------------------*/
222 /* RAM Transfer Address */
223 begin_adrs = _nbu2ss_get_begin_ram_address(udc);
224 data = (begin_adrs << 16) | ep->ep.maxpacket;
225 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, data);
227 /*-------------------------------------------------------------*/
228 /* Interrupt Enable */
229 data = 1 << (ep->epnum + 8);
230 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, data);
232 /*-------------------------------------------------------------*/
233 /* Endpoint Type(Mode) */
234 /* Bulk, Interrupt, ISO */
235 switch (ep->ep_type) {
236 case USB_ENDPOINT_XFER_BULK:
240 case USB_ENDPOINT_XFER_INT:
241 data = EPN_BUF_SINGLE | EPN_INTERRUPT;
244 case USB_ENDPOINT_XFER_ISOC:
253 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
254 _nbu2ss_endpoint_toggle_reset(udc, (ep->epnum | ep->direct));
256 if (ep->direct == USB_DIR_OUT) {
257 /*---------------------------------------------------------*/
259 data = EPN_EN | EPN_BCLR | EPN_DIR0;
260 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
262 data = EPN_ONAK | EPN_OSTL_EN | EPN_OSTL;
263 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
265 data = EPN_OUT_EN | EPN_OUT_END_EN;
266 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
268 /*---------------------------------------------------------*/
270 data = EPN_EN | EPN_BCLR | EPN_AUTO;
271 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
274 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
276 data = EPN_IN_EN | EPN_IN_END_EN;
277 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
283 /*-------------------------------------------------------------------------*/
284 /* Release of Endpoint */
285 static int _nbu2ss_epn_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
290 if ((ep->epnum == 0) || (udc->vbus_active == 0))
295 /*-------------------------------------------------------------*/
296 /* RAM Transfer Address */
297 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, 0);
299 /*-------------------------------------------------------------*/
300 /* Interrupt Disable */
301 data = 1 << (ep->epnum + 8);
302 _nbu2ss_bitclr(&udc->p_regs->USB_INT_ENA, data);
304 if (ep->direct == USB_DIR_OUT) {
305 /*---------------------------------------------------------*/
307 data = EPN_ONAK | EPN_BCLR;
308 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
310 data = EPN_EN | EPN_DIR0;
311 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
313 data = EPN_OUT_EN | EPN_OUT_END_EN;
314 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
316 /*---------------------------------------------------------*/
319 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
321 data = EPN_EN | EPN_AUTO;
322 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
324 data = EPN_IN_EN | EPN_IN_END_EN;
325 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
331 /*-------------------------------------------------------------------------*/
332 /* DMA setting (without Endpoint 0) */
333 static void _nbu2ss_ep_dma_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
338 data = _nbu2ss_readl(&udc->p_regs->USBSSCONF);
339 if (((ep->epnum == 0) || (data & (1 << ep->epnum)) == 0))
340 return; /* Not Support DMA */
344 if (ep->direct == USB_DIR_OUT) {
345 /*---------------------------------------------------------*/
347 data = ep->ep.maxpacket;
348 _nbu2ss_writel(&udc->p_regs->EP_DCR[num].EP_DCR2, data);
350 /*---------------------------------------------------------*/
351 /* Transfer Direct */
352 data = DCR1_EPN_DIR0;
353 _nbu2ss_bitset(&udc->p_regs->EP_DCR[num].EP_DCR1, data);
355 /*---------------------------------------------------------*/
357 data = EPN_STOP_MODE | EPN_STOP_SET | EPN_DMAMODE0;
358 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
360 /*---------------------------------------------------------*/
362 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, EPN_AUTO);
364 /*---------------------------------------------------------*/
366 data = EPN_BURST_SET | EPN_DMAMODE0;
367 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
371 /*-------------------------------------------------------------------------*/
372 /* DMA setting release */
373 static void _nbu2ss_ep_dma_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
377 struct fc_regs __iomem *preg = udc->p_regs;
379 if (udc->vbus_active == 0)
380 return; /* VBUS OFF */
382 data = _nbu2ss_readl(&preg->USBSSCONF);
383 if ((ep->epnum == 0) || ((data & (1 << ep->epnum)) == 0))
384 return; /* Not Support DMA */
388 _nbu2ss_ep_dma_abort(udc, ep);
390 if (ep->direct == USB_DIR_OUT) {
391 /*---------------------------------------------------------*/
393 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, 0);
394 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPN_DIR0);
395 _nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
397 /*---------------------------------------------------------*/
399 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPN_AUTO);
400 _nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
404 /*-------------------------------------------------------------------------*/
406 static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
408 struct fc_regs __iomem *preg = udc->p_regs;
410 _nbu2ss_bitclr(&preg->EP_DCR[ep->epnum - 1].EP_DCR1, DCR1_EPN_REQEN);
411 mdelay(DMA_DISABLE_TIME); /* DCR1_EPN_REQEN Clear */
412 _nbu2ss_bitclr(&preg->EP_REGS[ep->epnum - 1].EP_DMA_CTRL, EPN_DMA_EN);
415 /*-------------------------------------------------------------------------*/
416 /* Start IN Transfer */
417 static void _nbu2ss_ep_in_end(struct nbu2ss_udc *udc,
418 u32 epnum, u32 data32, u32 length)
422 struct fc_regs __iomem *preg = udc->p_regs;
424 if (length >= sizeof(u32))
428 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_AUTO);
430 /* Writing of 1-4 bytes */
432 _nbu2ss_writel(&preg->EP0_WRITE, data32);
434 data = ((length << 5) & EP0_DW) | EP0_DEND;
435 _nbu2ss_writel(&preg->EP0_CONTROL, data);
437 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_AUTO);
441 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPN_AUTO);
443 /* Writing of 1-4 bytes */
445 _nbu2ss_writel(&preg->EP_REGS[num].EP_WRITE, data32);
447 data = (((length) << 5) & EPN_DW) | EPN_DEND;
448 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
450 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, EPN_AUTO);
455 /*-------------------------------------------------------------------------*/
456 static void _nbu2ss_dma_map_single(struct nbu2ss_udc *udc,
457 struct nbu2ss_ep *ep,
458 struct nbu2ss_req *req, u8 direct)
460 if (req->req.dma == DMA_ADDR_INVALID) {
461 if (req->unaligned) {
462 req->req.dma = ep->phys_buf;
464 req->req.dma = dma_map_single(udc->gadget.dev.parent,
467 (direct == USB_DIR_IN)
474 dma_sync_single_for_device(udc->gadget.dev.parent,
477 (direct == USB_DIR_IN)
485 /*-------------------------------------------------------------------------*/
486 static void _nbu2ss_dma_unmap_single(struct nbu2ss_udc *udc,
487 struct nbu2ss_ep *ep,
488 struct nbu2ss_req *req, u8 direct)
494 if (direct == USB_DIR_OUT) {
495 count = req->req.actual % 4;
498 p += (req->req.actual - count);
499 memcpy(data, p, count);
504 if (req->unaligned) {
505 if (direct == USB_DIR_OUT)
506 memcpy(req->req.buf, ep->virt_buf,
507 req->req.actual & 0xfffffffc);
509 dma_unmap_single(udc->gadget.dev.parent,
510 req->req.dma, req->req.length,
511 (direct == USB_DIR_IN)
515 req->req.dma = DMA_ADDR_INVALID;
519 dma_sync_single_for_cpu(udc->gadget.dev.parent,
520 req->req.dma, req->req.length,
521 (direct == USB_DIR_IN)
528 p += (req->req.actual - count);
529 memcpy(p, data, count);
534 /*-------------------------------------------------------------------------*/
535 /* Endpoint 0 OUT Transfer (PIO) */
536 static int ep0_out_pio(struct nbu2ss_udc *udc, u8 *buf, u32 length)
539 u32 numreads = length / sizeof(u32);
540 union usb_reg_access *buf32 = (union usb_reg_access *)buf;
546 for (i = 0; i < numreads; i++) {
547 buf32->dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
551 return numreads * sizeof(u32);
554 /*-------------------------------------------------------------------------*/
555 /* Endpoint 0 OUT Transfer (PIO, OverBytes) */
556 static int ep0_out_overbytes(struct nbu2ss_udc *udc, u8 *p_buf, u32 length)
560 union usb_reg_access temp_32;
561 union usb_reg_access *p_buf_32 = (union usb_reg_access *)p_buf;
563 if ((length > 0) && (length < sizeof(u32))) {
564 temp_32.dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
565 for (i = 0 ; i < length ; i++)
566 p_buf_32->byte.DATA[i] = temp_32.byte.DATA[i];
567 i_read_size += length;
573 /*-------------------------------------------------------------------------*/
574 /* Endpoint 0 IN Transfer (PIO) */
575 static int EP0_in_PIO(struct nbu2ss_udc *udc, u8 *p_buf, u32 length)
578 u32 i_max_length = EP0_PACKETSIZE;
579 u32 i_word_length = 0;
580 u32 i_write_length = 0;
581 union usb_reg_access *p_buf_32 = (union usb_reg_access *)p_buf;
583 /*------------------------------------------------------------*/
584 /* Transfer Length */
585 if (i_max_length < length)
586 i_word_length = i_max_length / sizeof(u32);
588 i_word_length = length / sizeof(u32);
590 /*------------------------------------------------------------*/
592 for (i = 0; i < i_word_length; i++) {
593 _nbu2ss_writel(&udc->p_regs->EP0_WRITE, p_buf_32->dw);
595 i_write_length += sizeof(u32);
598 return i_write_length;
601 /*-------------------------------------------------------------------------*/
602 /* Endpoint 0 IN Transfer (PIO, OverBytes) */
603 static int ep0_in_overbytes(struct nbu2ss_udc *udc,
608 union usb_reg_access temp_32;
609 union usb_reg_access *p_buf_32 = (union usb_reg_access *)p_buf;
611 if ((i_remain_size > 0) && (i_remain_size < sizeof(u32))) {
612 for (i = 0 ; i < i_remain_size ; i++)
613 temp_32.byte.DATA[i] = p_buf_32->byte.DATA[i];
614 _nbu2ss_ep_in_end(udc, 0, temp_32.dw, i_remain_size);
616 return i_remain_size;
622 /*-------------------------------------------------------------------------*/
623 /* Transfer NULL Packet (Epndoint 0) */
624 static int EP0_send_NULL(struct nbu2ss_udc *udc, bool pid_flag)
628 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
629 data &= ~(u32)EP0_INAK;
632 data |= (EP0_INAK_EN | EP0_PIDCLR | EP0_DEND);
634 data |= (EP0_INAK_EN | EP0_DEND);
636 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
641 /*-------------------------------------------------------------------------*/
642 /* Receive NULL Packet (Endpoint 0) */
643 static int EP0_receive_NULL(struct nbu2ss_udc *udc, bool pid_flag)
647 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
648 data &= ~(u32)EP0_ONAK;
653 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
658 /*-------------------------------------------------------------------------*/
659 static int _nbu2ss_ep0_in_transfer(struct nbu2ss_udc *udc,
660 struct nbu2ss_req *req)
662 u8 *p_buffer; /* IN Data Buffer */
664 u32 i_remain_size = 0;
667 /*-------------------------------------------------------------*/
668 /* End confirmation */
669 if (req->req.actual == req->req.length) {
670 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
673 EP0_send_NULL(udc, false);
678 return 0; /* Transfer End */
681 /*-------------------------------------------------------------*/
683 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
685 data &= ~(u32)EP0_INAK;
686 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
688 i_remain_size = req->req.length - req->req.actual;
689 p_buffer = (u8 *)req->req.buf;
690 p_buffer += req->req.actual;
692 /*-------------------------------------------------------------*/
694 result = EP0_in_PIO(udc, p_buffer, i_remain_size);
696 req->div_len = result;
697 i_remain_size -= result;
699 if (i_remain_size == 0) {
700 EP0_send_NULL(udc, false);
704 if ((i_remain_size < sizeof(u32)) && (result != EP0_PACKETSIZE)) {
706 result += ep0_in_overbytes(udc, p_buffer, i_remain_size);
707 req->div_len = result;
713 /*-------------------------------------------------------------------------*/
714 static int _nbu2ss_ep0_out_transfer(struct nbu2ss_udc *udc,
715 struct nbu2ss_req *req)
723 /*-------------------------------------------------------------*/
724 /* Receive data confirmation */
725 i_recv_length = _nbu2ss_readl(&udc->p_regs->EP0_LENGTH) & EP0_LDATA;
726 if (i_recv_length != 0) {
729 i_remain_size = req->req.length - req->req.actual;
730 p_buffer = (u8 *)req->req.buf;
731 p_buffer += req->req.actual;
733 result = ep0_out_pio(udc, p_buffer
734 , min(i_remain_size, i_recv_length));
738 req->req.actual += result;
739 i_recv_length -= result;
741 if ((i_recv_length > 0) && (i_recv_length < sizeof(u32))) {
743 i_remain_size -= result;
745 result = ep0_out_overbytes(udc, p_buffer
746 , min(i_remain_size, i_recv_length));
747 req->req.actual += result;
753 /*-------------------------------------------------------------*/
754 /* End confirmation */
755 if (req->req.actual == req->req.length) {
756 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
759 EP0_receive_NULL(udc, false);
764 return 0; /* Transfer End */
767 if ((req->req.actual % EP0_PACKETSIZE) != 0)
768 return 0; /* Short Packet Transfer End */
770 if (req->req.actual > req->req.length) {
771 dev_err(udc->dev, " *** Overrun Error\n");
775 if (f_rcv_zero != 0) {
776 i_remain_size = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
777 if (i_remain_size & EP0_ONAK) {
778 /*---------------------------------------------------*/
780 _nbu2ss_bitclr(&udc->p_regs->EP0_CONTROL, EP0_ONAK);
788 /*-------------------------------------------------------------------------*/
789 static int _nbu2ss_out_dma(struct nbu2ss_udc *udc, struct nbu2ss_req *req,
799 struct fc_regs __iomem *preg = udc->p_regs;
802 return 1; /* DMA is forwarded */
804 req->dma_flag = true;
805 p_buffer = req->req.dma;
806 p_buffer += req->req.actual;
809 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)p_buffer);
811 /* Number of transfer packets */
812 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPN_MPKT;
813 dmacnt = length / mpkt;
814 lmpkt = (length % mpkt) & ~(u32)0x03;
816 if (dmacnt > DMA_MAX_COUNT) {
817 dmacnt = DMA_MAX_COUNT;
819 } else if (lmpkt != 0) {
821 burst = 0; /* Burst OFF */
825 data = mpkt | (lmpkt << 16);
826 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
828 data = ((dmacnt & 0xff) << 16) | DCR1_EPN_DIR0 | DCR1_EPN_REQEN;
829 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
832 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, 0);
833 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_BURST_SET);
835 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT
837 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_BURST_SET);
839 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_DMA_EN);
841 result = length & ~(u32)0x03;
842 req->div_len = result;
847 /*-------------------------------------------------------------------------*/
848 static int _nbu2ss_epn_out_pio(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
849 struct nbu2ss_req *req, u32 length)
855 union usb_reg_access temp_32;
856 union usb_reg_access *p_buf_32;
858 struct fc_regs __iomem *preg = udc->p_regs;
861 return 1; /* DMA is forwarded */
866 p_buffer = (u8 *)req->req.buf;
867 p_buf_32 = (union usb_reg_access *)(p_buffer + req->req.actual);
869 i_word_length = length / sizeof(u32);
870 if (i_word_length > 0) {
871 /*---------------------------------------------------------*/
872 /* Copy of every four bytes */
873 for (i = 0; i < i_word_length; i++) {
875 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
878 result = i_word_length * sizeof(u32);
881 data = length - result;
883 /*---------------------------------------------------------*/
884 /* Copy of fraction byte */
886 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_READ);
887 for (i = 0 ; i < data ; i++)
888 p_buf_32->byte.DATA[i] = temp_32.byte.DATA[i];
892 req->req.actual += result;
894 if ((req->req.actual == req->req.length) ||
895 ((req->req.actual % ep->ep.maxpacket) != 0)) {
902 /*-------------------------------------------------------------------------*/
903 static int _nbu2ss_epn_out_data(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
904 struct nbu2ss_req *req, u32 data_size)
915 i_buf_size = min((req->req.length - req->req.actual), data_size);
917 if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
918 (i_buf_size >= sizeof(u32))) {
919 nret = _nbu2ss_out_dma(udc, req, num, i_buf_size);
921 i_buf_size = min_t(u32, i_buf_size, ep->ep.maxpacket);
922 nret = _nbu2ss_epn_out_pio(udc, ep, req, i_buf_size);
928 /*-------------------------------------------------------------------------*/
929 static int _nbu2ss_epn_out_transfer(struct nbu2ss_udc *udc,
930 struct nbu2ss_ep *ep,
931 struct nbu2ss_req *req)
936 struct fc_regs __iomem *preg = udc->p_regs;
943 /*-------------------------------------------------------------*/
946 _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT) & EPN_LDATA;
948 if (i_recv_length != 0) {
949 result = _nbu2ss_epn_out_data(udc, ep, req, i_recv_length);
950 if (i_recv_length < ep->ep.maxpacket) {
951 if (i_recv_length == result) {
952 req->req.actual += result;
957 if ((req->req.actual == req->req.length) ||
958 ((req->req.actual % ep->ep.maxpacket) != 0)) {
964 if ((req->req.actual % ep->ep.maxpacket) == 0) {
972 if (req->req.actual > req->req.length) {
973 dev_err(udc->dev, " Overrun Error\n");
974 dev_err(udc->dev, " actual = %d, length = %d\n",
975 req->req.actual, req->req.length);
982 /*-------------------------------------------------------------------------*/
983 static int _nbu2ss_in_dma(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
984 struct nbu2ss_req *req, u32 num, u32 length)
987 u32 mpkt; /* MaxPacketSize */
988 u32 lmpkt; /* Last Packet Data Size */
989 u32 dmacnt; /* IN Data Size */
992 int result = -EINVAL;
993 struct fc_regs __iomem *preg = udc->p_regs;
996 return 1; /* DMA is forwarded */
999 if (req->req.actual == 0)
1000 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_IN);
1002 req->dma_flag = true;
1004 /* MAX Packet Size */
1005 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPN_MPKT;
1007 if ((DMA_MAX_COUNT * mpkt) < length)
1008 i_write_length = DMA_MAX_COUNT * mpkt;
1010 i_write_length = length;
1012 /*------------------------------------------------------------*/
1013 /* Number of transmission packets */
1014 if (mpkt < i_write_length) {
1015 dmacnt = i_write_length / mpkt;
1016 lmpkt = (i_write_length % mpkt) & ~(u32)0x3;
1020 lmpkt = mpkt & ~(u32)0x3;
1024 lmpkt = i_write_length & ~(u32)0x3;
1027 /* Packet setting */
1028 data = mpkt | (lmpkt << 16);
1029 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
1031 /* Address setting */
1032 p_buffer = req->req.dma;
1033 p_buffer += req->req.actual;
1034 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)p_buffer);
1036 /* Packet and DMA setting */
1037 data = ((dmacnt & 0xff) << 16) | DCR1_EPN_REQEN;
1038 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
1040 /* Packet setting of EPC */
1041 data = dmacnt << 16;
1042 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, data);
1044 /*DMA setting of EPC */
1045 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_DMA_EN);
1047 result = i_write_length & ~(u32)0x3;
1048 req->div_len = result;
1053 /*-------------------------------------------------------------------------*/
1054 static int _nbu2ss_epn_in_pio(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
1055 struct nbu2ss_req *req, u32 length)
1061 union usb_reg_access temp_32;
1062 union usb_reg_access *p_buf_32 = NULL;
1064 struct fc_regs __iomem *preg = udc->p_regs;
1067 return 1; /* DMA is forwarded */
1070 p_buffer = (u8 *)req->req.buf;
1071 p_buf_32 = (union usb_reg_access *)(p_buffer + req->req.actual);
1073 i_word_length = length / sizeof(u32);
1074 if (i_word_length > 0) {
1075 for (i = 0; i < i_word_length; i++) {
1076 _nbu2ss_writel(&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(&ep->udc->p_regs->EP_REGS[ep->epnum - 1].EP_LEN_DCNT);
1229 length &= EPN_LDATA;
1230 if (length < ep->ep.maxpacket)
1234 _nbu2ss_start_transfer(ep->udc, ep, req, bflag);
1237 /*-------------------------------------------------------------------------*/
1238 /* Endpoint Toggle Reset */
1239 static void _nbu2ss_endpoint_toggle_reset(struct nbu2ss_udc *udc, u8 ep_adrs)
1244 if ((ep_adrs == 0) || (ep_adrs == 0x80))
1247 num = (ep_adrs & 0x7F) - 1;
1249 if (ep_adrs & USB_DIR_IN)
1252 data = EPN_BCLR | EPN_OPIDCLR;
1254 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
1257 /*-------------------------------------------------------------------------*/
1258 /* Endpoint STALL set */
1259 static void _nbu2ss_set_endpoint_stall(struct nbu2ss_udc *udc,
1260 u8 ep_adrs, bool bstall)
1264 struct nbu2ss_ep *ep;
1265 struct fc_regs __iomem *preg = udc->p_regs;
1267 if ((ep_adrs == 0) || (ep_adrs == 0x80)) {
1270 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_STL);
1273 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_STL);
1276 epnum = ep_adrs & USB_ENDPOINT_NUMBER_MASK;
1278 ep = &udc->ep[epnum];
1284 if (ep_adrs & USB_DIR_IN)
1285 data = EPN_BCLR | EPN_ISTL;
1287 data = EPN_OSTL_EN | EPN_OSTL;
1289 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
1291 if (ep_adrs & USB_DIR_IN) {
1292 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL
1296 _nbu2ss_readl(&preg->EP_REGS[num].EP_CONTROL);
1299 data |= EPN_OSTL_EN;
1301 _nbu2ss_writel(&preg->EP_REGS[num].EP_CONTROL
1306 ep->stalled = false;
1309 _nbu2ss_restert_transfer(ep);
1315 /*-------------------------------------------------------------------------*/
1316 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *udc, u32 mode)
1320 if (mode > MAX_TEST_MODE_NUM)
1323 dev_info(udc->dev, "SET FEATURE : test mode = %d\n", mode);
1325 data = _nbu2ss_readl(&udc->p_regs->USB_CONTROL);
1326 data &= ~TEST_FORCE_ENABLE;
1327 data |= mode << TEST_MODE_SHIFT;
1329 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, data);
1330 _nbu2ss_bitset(&udc->p_regs->TEST_CONTROL, CS_TESTMODEEN);
1333 /*-------------------------------------------------------------------------*/
1334 static int _nbu2ss_set_feature_device(struct nbu2ss_udc *udc,
1335 u16 selector, u16 wIndex)
1337 int result = -EOPNOTSUPP;
1340 case USB_DEVICE_REMOTE_WAKEUP:
1341 if (wIndex == 0x0000) {
1342 udc->remote_wakeup = U2F_ENABLE;
1347 case USB_DEVICE_TEST_MODE:
1349 if (wIndex <= MAX_TEST_MODE_NUM)
1360 /*-------------------------------------------------------------------------*/
1361 static int _nbu2ss_get_ep_stall(struct nbu2ss_udc *udc, u8 ep_adrs)
1364 u32 data = 0, bit_data;
1365 struct fc_regs __iomem *preg = udc->p_regs;
1367 epnum = ep_adrs & ~USB_ENDPOINT_DIR_MASK;
1369 data = _nbu2ss_readl(&preg->EP0_CONTROL);
1373 data = _nbu2ss_readl(&preg->EP_REGS[epnum - 1].EP_CONTROL);
1374 if ((data & EPN_EN) == 0)
1377 if (ep_adrs & USB_ENDPOINT_DIR_MASK)
1378 bit_data = EPN_ISTL;
1380 bit_data = EPN_OSTL;
1383 if ((data & bit_data) == 0)
1388 /*-------------------------------------------------------------------------*/
1389 static inline int _nbu2ss_req_feature(struct nbu2ss_udc *udc, bool bset)
1391 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1392 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1393 u16 selector = le16_to_cpu(udc->ctrl.wValue);
1394 u16 wIndex = le16_to_cpu(udc->ctrl.wIndex);
1396 int result = -EOPNOTSUPP;
1398 if ((udc->ctrl.wLength != 0x0000) ||
1399 (direction != USB_DIR_OUT)) {
1403 switch (recipient) {
1404 case USB_RECIP_DEVICE:
1407 _nbu2ss_set_feature_device(udc, selector, wIndex);
1410 case USB_RECIP_ENDPOINT:
1411 if (0x0000 == (wIndex & 0xFF70)) {
1412 if (selector == USB_ENDPOINT_HALT) {
1413 ep_adrs = wIndex & 0xFF;
1415 _nbu2ss_endpoint_toggle_reset(udc,
1419 _nbu2ss_set_endpoint_stall(udc, ep_adrs, bset);
1431 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1436 /*-------------------------------------------------------------------------*/
1437 static inline enum usb_device_speed _nbu2ss_get_speed(struct nbu2ss_udc *udc)
1440 enum usb_device_speed speed = USB_SPEED_FULL;
1442 data = _nbu2ss_readl(&udc->p_regs->USB_STATUS);
1443 if (data & HIGH_SPEED)
1444 speed = USB_SPEED_HIGH;
1449 /*-------------------------------------------------------------------------*/
1450 static void _nbu2ss_epn_set_stall(struct nbu2ss_udc *udc,
1451 struct nbu2ss_ep *ep)
1457 struct fc_regs __iomem *preg = udc->p_regs;
1459 if (ep->direct == USB_DIR_IN) {
1461 ; limit_cnt < IN_DATA_EMPTY_COUNT
1463 regdata = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1465 if ((regdata & EPN_IN_DATA) == 0)
1472 ep_adrs = ep->epnum | ep->direct;
1473 _nbu2ss_set_endpoint_stall(udc, ep_adrs, 1);
1476 /*-------------------------------------------------------------------------*/
1477 static int std_req_get_status(struct nbu2ss_udc *udc)
1480 u16 status_data = 0;
1481 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1482 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1484 int result = -EINVAL;
1486 if ((udc->ctrl.wValue != 0x0000) || (direction != USB_DIR_IN))
1490 min_t(u16, le16_to_cpu(udc->ctrl.wLength), sizeof(status_data));
1491 switch (recipient) {
1492 case USB_RECIP_DEVICE:
1493 if (udc->ctrl.wIndex == 0x0000) {
1494 if (udc->gadget.is_selfpowered)
1495 status_data |= BIT(USB_DEVICE_SELF_POWERED);
1497 if (udc->remote_wakeup)
1498 status_data |= BIT(USB_DEVICE_REMOTE_WAKEUP);
1504 case USB_RECIP_ENDPOINT:
1505 if (0x0000 == (le16_to_cpu(udc->ctrl.wIndex) & 0xFF70)) {
1506 ep_adrs = (u8)(le16_to_cpu(udc->ctrl.wIndex) & 0xFF);
1507 result = _nbu2ss_get_ep_stall(udc, ep_adrs);
1510 status_data |= BIT(USB_ENDPOINT_HALT);
1519 memcpy(udc->ep0_buf, &status_data, length);
1520 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, length);
1521 _nbu2ss_ep0_in_transfer(udc, &udc->ep0_req);
1524 dev_err(udc->dev, " Error GET_STATUS\n");
1530 /*-------------------------------------------------------------------------*/
1531 static int std_req_clear_feature(struct nbu2ss_udc *udc)
1533 return _nbu2ss_req_feature(udc, false);
1536 /*-------------------------------------------------------------------------*/
1537 static int std_req_set_feature(struct nbu2ss_udc *udc)
1539 return _nbu2ss_req_feature(udc, true);
1542 /*-------------------------------------------------------------------------*/
1543 static int std_req_set_address(struct nbu2ss_udc *udc)
1546 u32 wValue = le16_to_cpu(udc->ctrl.wValue);
1548 if ((udc->ctrl.bRequestType != 0x00) ||
1549 (udc->ctrl.wIndex != 0x0000) ||
1550 (udc->ctrl.wLength != 0x0000)) {
1554 if (wValue != (wValue & 0x007F))
1557 wValue <<= USB_ADRS_SHIFT;
1559 _nbu2ss_writel(&udc->p_regs->USB_ADDRESS, wValue);
1560 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1565 /*-------------------------------------------------------------------------*/
1566 static int std_req_set_configuration(struct nbu2ss_udc *udc)
1568 u32 config_value = (u32)(le16_to_cpu(udc->ctrl.wValue) & 0x00ff);
1570 if ((udc->ctrl.wIndex != 0x0000) ||
1571 (udc->ctrl.wLength != 0x0000) ||
1572 (udc->ctrl.bRequestType != 0x00)) {
1576 udc->curr_config = config_value;
1578 if (config_value > 0) {
1579 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, CONF);
1580 udc->devstate = USB_STATE_CONFIGURED;
1583 _nbu2ss_bitclr(&udc->p_regs->USB_CONTROL, CONF);
1584 udc->devstate = USB_STATE_ADDRESS;
1590 /*-------------------------------------------------------------------------*/
1591 static inline void _nbu2ss_read_request_data(struct nbu2ss_udc *udc, u32 *pdata)
1593 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA0);
1595 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA1);
1598 /*-------------------------------------------------------------------------*/
1599 static inline int _nbu2ss_decode_request(struct nbu2ss_udc *udc)
1601 bool bcall_back = true;
1603 struct usb_ctrlrequest *p_ctrl;
1605 p_ctrl = &udc->ctrl;
1606 _nbu2ss_read_request_data(udc, (u32 *)p_ctrl);
1608 /* ep0 state control */
1609 if (p_ctrl->wLength == 0) {
1610 udc->ep0state = EP0_IN_STATUS_PHASE;
1613 if (p_ctrl->bRequestType & USB_DIR_IN)
1614 udc->ep0state = EP0_IN_DATA_PHASE;
1616 udc->ep0state = EP0_OUT_DATA_PHASE;
1619 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1620 switch (p_ctrl->bRequest) {
1621 case USB_REQ_GET_STATUS:
1622 nret = std_req_get_status(udc);
1626 case USB_REQ_CLEAR_FEATURE:
1627 nret = std_req_clear_feature(udc);
1631 case USB_REQ_SET_FEATURE:
1632 nret = std_req_set_feature(udc);
1636 case USB_REQ_SET_ADDRESS:
1637 nret = std_req_set_address(udc);
1641 case USB_REQ_SET_CONFIGURATION:
1642 nret = std_req_set_configuration(udc);
1651 if (udc->ep0state == EP0_IN_STATUS_PHASE) {
1653 /*--------------------------------------*/
1655 nret = EP0_send_NULL(udc, true);
1660 spin_unlock(&udc->lock);
1661 nret = udc->driver->setup(&udc->gadget, &udc->ctrl);
1662 spin_lock(&udc->lock);
1666 udc->ep0state = EP0_IDLE;
1671 /*-------------------------------------------------------------------------*/
1672 static inline int _nbu2ss_ep0_in_data_stage(struct nbu2ss_udc *udc)
1675 struct nbu2ss_req *req;
1676 struct nbu2ss_ep *ep = &udc->ep[0];
1678 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1680 req = &udc->ep0_req;
1682 req->req.actual += req->div_len;
1685 nret = _nbu2ss_ep0_in_transfer(udc, req);
1687 udc->ep0state = EP0_OUT_STATUS_PAHSE;
1688 EP0_receive_NULL(udc, true);
1694 /*-------------------------------------------------------------------------*/
1695 static inline int _nbu2ss_ep0_out_data_stage(struct nbu2ss_udc *udc)
1698 struct nbu2ss_req *req;
1699 struct nbu2ss_ep *ep = &udc->ep[0];
1701 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1703 req = &udc->ep0_req;
1705 nret = _nbu2ss_ep0_out_transfer(udc, req);
1707 udc->ep0state = EP0_IN_STATUS_PHASE;
1708 EP0_send_NULL(udc, true);
1710 } else if (nret < 0) {
1711 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, EP0_BCLR);
1712 req->req.status = nret;
1718 /*-------------------------------------------------------------------------*/
1719 static inline int _nbu2ss_ep0_status_stage(struct nbu2ss_udc *udc)
1721 struct nbu2ss_req *req;
1722 struct nbu2ss_ep *ep = &udc->ep[0];
1724 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1726 req = &udc->ep0_req;
1727 if (req->req.complete)
1728 req->req.complete(&ep->ep, &req->req);
1731 if (req->req.complete)
1732 _nbu2ss_ep_done(ep, req, 0);
1735 udc->ep0state = EP0_IDLE;
1740 /*-------------------------------------------------------------------------*/
1741 static inline void _nbu2ss_ep0_int(struct nbu2ss_udc *udc)
1748 status = _nbu2ss_readl(&udc->p_regs->EP0_STATUS);
1749 intr = status & EP0_STATUS_RW_BIT;
1750 _nbu2ss_writel(&udc->p_regs->EP0_STATUS, ~intr);
1752 status &= (SETUP_INT | EP0_IN_INT | EP0_OUT_INT
1753 | STG_END_INT | EP0_OUT_NULL_INT);
1756 dev_info(udc->dev, "%s Not Decode Interrupt\n", __func__);
1757 dev_info(udc->dev, "EP0_STATUS = 0x%08x\n", intr);
1761 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1762 udc->gadget.speed = _nbu2ss_get_speed(udc);
1764 for (i = 0; i < EP0_END_XFER; i++) {
1765 switch (udc->ep0state) {
1767 if (status & SETUP_INT) {
1769 nret = _nbu2ss_decode_request(udc);
1773 case EP0_IN_DATA_PHASE:
1774 if (status & EP0_IN_INT) {
1775 status &= ~EP0_IN_INT;
1776 nret = _nbu2ss_ep0_in_data_stage(udc);
1780 case EP0_OUT_DATA_PHASE:
1781 if (status & EP0_OUT_INT) {
1782 status &= ~EP0_OUT_INT;
1783 nret = _nbu2ss_ep0_out_data_stage(udc);
1787 case EP0_IN_STATUS_PHASE:
1788 if ((status & STG_END_INT) || (status & SETUP_INT)) {
1789 status &= ~(STG_END_INT | EP0_IN_INT);
1790 nret = _nbu2ss_ep0_status_stage(udc);
1794 case EP0_OUT_STATUS_PAHSE:
1795 if ((status & STG_END_INT) || (status & SETUP_INT) ||
1796 (status & EP0_OUT_NULL_INT)) {
1797 status &= ~(STG_END_INT
1799 | EP0_OUT_NULL_INT);
1801 nret = _nbu2ss_ep0_status_stage(udc);
1817 _nbu2ss_set_endpoint_stall(udc, 0, true);
1821 /*-------------------------------------------------------------------------*/
1822 static void _nbu2ss_ep_done(struct nbu2ss_ep *ep,
1823 struct nbu2ss_req *req,
1826 struct nbu2ss_udc *udc = ep->udc;
1828 list_del_init(&req->queue);
1830 if (status == -ECONNRESET)
1831 _nbu2ss_fifo_flush(udc, ep);
1833 if (likely(req->req.status == -EINPROGRESS))
1834 req->req.status = status;
1837 _nbu2ss_epn_set_stall(udc, ep);
1839 if (!list_empty(&ep->queue))
1840 _nbu2ss_restert_transfer(ep);
1844 if ((ep->direct == USB_DIR_OUT) && (ep->epnum > 0) &&
1845 (req->req.dma != 0))
1846 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_OUT);
1849 spin_unlock(&udc->lock);
1850 req->req.complete(&ep->ep, &req->req);
1851 spin_lock(&udc->lock);
1854 /*-------------------------------------------------------------------------*/
1855 static inline void _nbu2ss_epn_in_int(struct nbu2ss_udc *udc,
1856 struct nbu2ss_ep *ep,
1857 struct nbu2ss_req *req)
1862 struct fc_regs __iomem *preg = udc->p_regs;
1865 return; /* DMA is forwarded */
1867 req->req.actual += req->div_len;
1870 if (req->req.actual != req->req.length) {
1871 /*---------------------------------------------------------*/
1872 /* remainder of data */
1873 result = _nbu2ss_epn_in_transfer(udc, ep, req);
1876 if (req->zero && ((req->req.actual % ep->ep.maxpacket) == 0)) {
1878 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1880 if ((status & EPN_IN_FULL) == 0) {
1881 /*-----------------------------------------*/
1882 /* 0 Length Packet */
1884 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1891 /*---------------------------------------------------------*/
1893 _nbu2ss_ep_done(ep, req, result);
1897 /*-------------------------------------------------------------------------*/
1898 static inline void _nbu2ss_epn_out_int(struct nbu2ss_udc *udc,
1899 struct nbu2ss_ep *ep,
1900 struct nbu2ss_req *req)
1904 result = _nbu2ss_epn_out_transfer(udc, ep, req);
1906 _nbu2ss_ep_done(ep, req, result);
1909 /*-------------------------------------------------------------------------*/
1910 static inline void _nbu2ss_epn_in_dma_int(struct nbu2ss_udc *udc,
1911 struct nbu2ss_ep *ep,
1912 struct nbu2ss_req *req)
1916 struct usb_request *preq;
1923 preq->actual += req->div_len;
1925 req->dma_flag = false;
1928 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_IN);
1931 if (preq->actual != preq->length) {
1932 _nbu2ss_epn_in_transfer(udc, ep, req);
1934 mpkt = ep->ep.maxpacket;
1935 size = preq->actual % mpkt;
1937 if (((preq->actual & 0x03) == 0) && (size < mpkt))
1938 _nbu2ss_ep_in_end(udc, ep->epnum, 0, 0);
1940 _nbu2ss_epn_in_int(udc, ep, req);
1945 /*-------------------------------------------------------------------------*/
1946 static inline void _nbu2ss_epn_out_dma_int(struct nbu2ss_udc *udc,
1947 struct nbu2ss_ep *ep,
1948 struct nbu2ss_req *req)
1952 u32 dmacnt, ep_dmacnt;
1954 struct fc_regs __iomem *preg = udc->p_regs;
1956 num = ep->epnum - 1;
1958 if (req->req.actual == req->req.length) {
1959 if ((req->req.length % ep->ep.maxpacket) && !req->zero) {
1961 req->dma_flag = false;
1962 _nbu2ss_ep_done(ep, req, 0);
1967 ep_dmacnt = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT)
1971 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
1972 dmacnt = _nbu2ss_readl(&preg->EP_DCR[num].EP_DCR1)
1975 if (ep_dmacnt == dmacnt)
1979 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPN_REQEN);
1982 mpkt = ep->ep.maxpacket;
1983 if ((req->div_len % mpkt) == 0)
1984 req->div_len -= mpkt * dmacnt;
1987 if ((req->req.actual % ep->ep.maxpacket) > 0) {
1988 if (req->req.actual == req->div_len) {
1990 req->dma_flag = false;
1991 _nbu2ss_ep_done(ep, req, 0);
1996 req->req.actual += req->div_len;
1998 req->dma_flag = false;
2000 _nbu2ss_epn_out_int(udc, ep, req);
2003 /*-------------------------------------------------------------------------*/
2004 static inline void _nbu2ss_epn_int(struct nbu2ss_udc *udc, u32 epnum)
2009 struct nbu2ss_req *req;
2010 struct nbu2ss_ep *ep = &udc->ep[epnum];
2014 /* Interrupt Status */
2015 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
2017 /* Interrupt Clear */
2018 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_STATUS, ~status);
2020 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
2022 /* pr_warn("=== %s(%d) req == NULL\n", __func__, epnum); */
2026 if (status & EPN_OUT_END_INT) {
2027 status &= ~EPN_OUT_INT;
2028 _nbu2ss_epn_out_dma_int(udc, ep, req);
2031 if (status & EPN_OUT_INT)
2032 _nbu2ss_epn_out_int(udc, ep, req);
2034 if (status & EPN_IN_END_INT) {
2035 status &= ~EPN_IN_INT;
2036 _nbu2ss_epn_in_dma_int(udc, ep, req);
2039 if (status & EPN_IN_INT)
2040 _nbu2ss_epn_in_int(udc, ep, req);
2043 /*-------------------------------------------------------------------------*/
2044 static inline void _nbu2ss_ep_int(struct nbu2ss_udc *udc, u32 epnum)
2047 _nbu2ss_ep0_int(udc);
2049 _nbu2ss_epn_int(udc, epnum);
2052 /*-------------------------------------------------------------------------*/
2053 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *udc)
2055 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, (EP0_AUTO | EP0_BCLR));
2056 _nbu2ss_writel(&udc->p_regs->EP0_INT_ENA, EP0_INT_EN_BIT);
2059 /*-------------------------------------------------------------------------*/
2060 static int _nbu2ss_nuke(struct nbu2ss_udc *udc,
2061 struct nbu2ss_ep *ep,
2064 struct nbu2ss_req *req, *n;
2066 /* Endpoint Disable */
2067 _nbu2ss_epn_exit(udc, ep);
2070 _nbu2ss_ep_dma_exit(udc, ep);
2072 if (list_empty(&ep->queue))
2075 /* called with irqs blocked */
2076 list_for_each_entry_safe(req, n, &ep->queue, queue) {
2077 _nbu2ss_ep_done(ep, req, status);
2083 /*-------------------------------------------------------------------------*/
2084 static void _nbu2ss_quiesce(struct nbu2ss_udc *udc)
2086 struct nbu2ss_ep *ep;
2088 udc->gadget.speed = USB_SPEED_UNKNOWN;
2090 _nbu2ss_nuke(udc, &udc->ep[0], -ESHUTDOWN);
2093 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2094 _nbu2ss_nuke(udc, ep, -ESHUTDOWN);
2098 /*-------------------------------------------------------------------------*/
2099 static int _nbu2ss_pullup(struct nbu2ss_udc *udc, int is_on)
2103 if (udc->vbus_active == 0)
2109 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL)
2110 | PUE2) & ~(u32)CONNECTB;
2112 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2117 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL) | CONNECTB)
2120 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2121 udc->gadget.speed = USB_SPEED_UNKNOWN;
2127 /*-------------------------------------------------------------------------*/
2128 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
2130 struct fc_regs __iomem *p = udc->p_regs;
2132 if (udc->vbus_active == 0)
2135 if (ep->epnum == 0) {
2137 _nbu2ss_bitset(&p->EP0_CONTROL, EP0_BCLR);
2141 _nbu2ss_ep_dma_abort(udc, ep);
2142 _nbu2ss_bitset(&p->EP_REGS[ep->epnum - 1].EP_CONTROL, EPN_BCLR);
2146 /*-------------------------------------------------------------------------*/
2147 static int _nbu2ss_enable_controller(struct nbu2ss_udc *udc)
2151 if (udc->udc_enabled)
2155 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2156 udelay(EPC_RST_DISABLE_TIME); /* 1us wait */
2158 _nbu2ss_bitclr(&udc->p_regs->EPCTR, DIRPD);
2159 mdelay(EPC_DIRPD_DISABLE_TIME); /* 1ms wait */
2161 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2163 _nbu2ss_writel(&udc->p_regs->AHBSCTR, WAIT_MODE);
2165 _nbu2ss_writel(&udc->p_regs->AHBMCTR,
2166 HBUSREQ_MODE | HTRANS_MODE | WBURST_TYPE);
2168 while (!(_nbu2ss_readl(&udc->p_regs->EPCTR) & PLL_LOCK)) {
2170 udelay(1); /* 1us wait */
2171 if (waitcnt == EPC_PLL_LOCK_COUNT) {
2172 dev_err(udc->dev, "*** Reset Cancel failed\n");
2177 _nbu2ss_bitset(&udc->p_regs->UTMI_CHARACTER_1, USB_SQUSET);
2179 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, (INT_SEL | SOF_RCV));
2182 _nbu2ss_ep0_enable(udc);
2184 /* USB Interrupt Enable */
2185 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, USB_INT_EN_BIT);
2187 udc->udc_enabled = true;
2192 /*-------------------------------------------------------------------------*/
2193 static void _nbu2ss_reset_controller(struct nbu2ss_udc *udc)
2195 _nbu2ss_bitset(&udc->p_regs->EPCTR, EPC_RST);
2196 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2199 /*-------------------------------------------------------------------------*/
2200 static void _nbu2ss_disable_controller(struct nbu2ss_udc *udc)
2202 if (udc->udc_enabled) {
2203 udc->udc_enabled = false;
2204 _nbu2ss_reset_controller(udc);
2205 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2209 /*-------------------------------------------------------------------------*/
2210 static inline void _nbu2ss_check_vbus(struct nbu2ss_udc *udc)
2216 mdelay(VBUS_CHATTERING_MDELAY); /* wait (ms) */
2219 reg_dt = gpiod_get_value(vbus_gpio);
2221 udc->linux_suspended = 0;
2223 _nbu2ss_reset_controller(udc);
2224 dev_info(udc->dev, " ----- VBUS OFF\n");
2226 if (udc->vbus_active == 1) {
2228 udc->vbus_active = 0;
2229 if (udc->usb_suspended) {
2230 udc->usb_suspended = 0;
2231 /* _nbu2ss_reset_controller(udc); */
2233 udc->devstate = USB_STATE_NOTATTACHED;
2235 _nbu2ss_quiesce(udc);
2237 spin_unlock(&udc->lock);
2238 udc->driver->disconnect(&udc->gadget);
2239 spin_lock(&udc->lock);
2242 _nbu2ss_disable_controller(udc);
2245 mdelay(5); /* wait (5ms) */
2246 reg_dt = gpiod_get_value(vbus_gpio);
2250 dev_info(udc->dev, " ----- VBUS ON\n");
2252 if (udc->linux_suspended)
2255 if (udc->vbus_active == 0) {
2257 udc->vbus_active = 1;
2258 udc->devstate = USB_STATE_POWERED;
2260 nret = _nbu2ss_enable_controller(udc);
2262 _nbu2ss_disable_controller(udc);
2263 udc->vbus_active = 0;
2267 _nbu2ss_pullup(udc, 1);
2269 #ifdef UDC_DEBUG_DUMP
2270 _nbu2ss_dump_register(udc);
2271 #endif /* UDC_DEBUG_DUMP */
2274 if (udc->devstate == USB_STATE_POWERED)
2275 _nbu2ss_pullup(udc, 1);
2280 /*-------------------------------------------------------------------------*/
2281 static inline void _nbu2ss_int_bus_reset(struct nbu2ss_udc *udc)
2283 udc->devstate = USB_STATE_DEFAULT;
2284 udc->remote_wakeup = 0;
2286 _nbu2ss_quiesce(udc);
2288 udc->ep0state = EP0_IDLE;
2291 /*-------------------------------------------------------------------------*/
2292 static inline void _nbu2ss_int_usb_resume(struct nbu2ss_udc *udc)
2294 if (udc->usb_suspended == 1) {
2295 udc->usb_suspended = 0;
2296 if (udc->driver && udc->driver->resume) {
2297 spin_unlock(&udc->lock);
2298 udc->driver->resume(&udc->gadget);
2299 spin_lock(&udc->lock);
2304 /*-------------------------------------------------------------------------*/
2305 static inline void _nbu2ss_int_usb_suspend(struct nbu2ss_udc *udc)
2309 if (udc->usb_suspended == 0) {
2310 reg_dt = gpiod_get_value(vbus_gpio);
2315 udc->usb_suspended = 1;
2316 if (udc->driver && udc->driver->suspend) {
2317 spin_unlock(&udc->lock);
2318 udc->driver->suspend(&udc->gadget);
2319 spin_lock(&udc->lock);
2322 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, SUSPEND);
2326 /*-------------------------------------------------------------------------*/
2327 /* VBUS (GPIO153) Interrupt */
2328 static irqreturn_t _nbu2ss_vbus_irq(int irq, void *_udc)
2330 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2332 spin_lock(&udc->lock);
2333 _nbu2ss_check_vbus(udc);
2334 spin_unlock(&udc->lock);
2339 /*-------------------------------------------------------------------------*/
2340 /* Interrupt (udc) */
2341 static irqreturn_t _nbu2ss_udc_irq(int irq, void *_udc)
2343 u8 suspend_flag = 0;
2347 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2348 struct fc_regs __iomem *preg = udc->p_regs;
2350 if (gpiod_get_value(vbus_gpio) == 0) {
2351 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2352 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2356 spin_lock(&udc->lock);
2359 if (gpiod_get_value(vbus_gpio) == 0) {
2360 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2361 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2364 status = _nbu2ss_readl(&preg->USB_INT_STA);
2370 _nbu2ss_writel(&preg->USB_INT_STA, ~(status & USB_INT_STA_RW));
2372 if (status & USB_RST_INT) {
2374 _nbu2ss_int_bus_reset(udc);
2377 if (status & RSUM_INT) {
2379 _nbu2ss_int_usb_resume(udc);
2382 if (status & SPND_INT) {
2387 if (status & EPN_INT) {
2389 int_bit = status >> 8;
2391 for (epnum = 0; epnum < NUM_ENDPOINTS; epnum++) {
2393 _nbu2ss_ep_int(udc, epnum);
2404 _nbu2ss_int_usb_suspend(udc);
2406 spin_unlock(&udc->lock);
2411 /*-------------------------------------------------------------------------*/
2413 static int nbu2ss_ep_enable(struct usb_ep *_ep,
2414 const struct usb_endpoint_descriptor *desc)
2417 unsigned long flags;
2419 struct nbu2ss_ep *ep;
2420 struct nbu2ss_udc *udc;
2422 if (!_ep || !desc) {
2423 pr_err(" *** %s, bad param\n", __func__);
2427 ep = container_of(_ep, struct nbu2ss_ep, ep);
2429 pr_err(" *** %s, ep == NULL !!\n", __func__);
2433 ep_type = usb_endpoint_type(desc);
2434 if ((ep_type == USB_ENDPOINT_XFER_CONTROL) ||
2435 (ep_type == USB_ENDPOINT_XFER_ISOC)) {
2436 pr_err(" *** %s, bat bmAttributes\n", __func__);
2441 if (udc->vbus_active == 0)
2444 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
2445 dev_err(ep->udc->dev, " *** %s, udc !!\n", __func__);
2449 spin_lock_irqsave(&udc->lock, flags);
2452 ep->epnum = usb_endpoint_num(desc);
2453 ep->direct = desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2454 ep->ep_type = ep_type;
2457 ep->stalled = false;
2459 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2462 _nbu2ss_ep_dma_init(udc, ep);
2464 /* Endpoint setting */
2465 _nbu2ss_ep_init(udc, ep);
2467 spin_unlock_irqrestore(&udc->lock, flags);
2472 /*-------------------------------------------------------------------------*/
2473 static int nbu2ss_ep_disable(struct usb_ep *_ep)
2475 struct nbu2ss_ep *ep;
2476 struct nbu2ss_udc *udc;
2477 unsigned long flags;
2480 pr_err(" *** %s, bad param\n", __func__);
2484 ep = container_of(_ep, struct nbu2ss_ep, ep);
2486 pr_err("udc: *** %s, ep == NULL !!\n", __func__);
2491 if (udc->vbus_active == 0)
2494 spin_lock_irqsave(&udc->lock, flags);
2495 _nbu2ss_nuke(udc, ep, -EINPROGRESS); /* dequeue request */
2496 spin_unlock_irqrestore(&udc->lock, flags);
2501 /*-------------------------------------------------------------------------*/
2502 static struct usb_request *nbu2ss_ep_alloc_request(struct usb_ep *ep,
2505 struct nbu2ss_req *req;
2507 req = kzalloc(sizeof(*req), gfp_flags);
2512 req->req.dma = DMA_ADDR_INVALID;
2514 INIT_LIST_HEAD(&req->queue);
2519 /*-------------------------------------------------------------------------*/
2520 static void nbu2ss_ep_free_request(struct usb_ep *_ep,
2521 struct usb_request *_req)
2523 struct nbu2ss_req *req;
2526 req = container_of(_req, struct nbu2ss_req, req);
2532 /*-------------------------------------------------------------------------*/
2533 static int nbu2ss_ep_queue(struct usb_ep *_ep,
2534 struct usb_request *_req, gfp_t gfp_flags)
2536 struct nbu2ss_req *req;
2537 struct nbu2ss_ep *ep;
2538 struct nbu2ss_udc *udc;
2539 unsigned long flags;
2541 int result = -EINVAL;
2543 /* catch various bogus parameters */
2544 if (!_ep || !_req) {
2546 pr_err("udc: %s --- _ep == NULL\n", __func__);
2549 pr_err("udc: %s --- _req == NULL\n", __func__);
2554 req = container_of(_req, struct nbu2ss_req, req);
2555 if (unlikely(!_req->complete ||
2557 !list_empty(&req->queue))) {
2558 if (!_req->complete)
2559 pr_err("udc: %s --- !_req->complete\n", __func__);
2562 pr_err("udc:%s --- !_req->buf\n", __func__);
2564 if (!list_empty(&req->queue))
2565 pr_err("%s --- !list_empty(&req->queue)\n", __func__);
2570 ep = container_of(_ep, struct nbu2ss_ep, ep);
2573 if (udc->vbus_active == 0) {
2574 dev_info(udc->dev, "Can't ep_queue (VBUS OFF)\n");
2578 if (unlikely(!udc->driver)) {
2579 dev_err(udc->dev, "%s, bogus device state %p\n", __func__,
2584 spin_lock_irqsave(&udc->lock, flags);
2587 if ((uintptr_t)req->req.buf & 0x3)
2588 req->unaligned = true;
2590 req->unaligned = false;
2592 if (req->unaligned) {
2594 ep->virt_buf = dma_alloc_coherent(udc->dev, PAGE_SIZE,
2596 GFP_ATOMIC | GFP_DMA);
2597 if (ep->epnum > 0) {
2598 if (ep->direct == USB_DIR_IN)
2599 memcpy(ep->virt_buf, req->req.buf,
2604 if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT) &&
2605 (req->req.dma != 0))
2606 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_OUT);
2609 _req->status = -EINPROGRESS;
2612 bflag = list_empty(&ep->queue);
2613 list_add_tail(&req->queue, &ep->queue);
2615 if (bflag && !ep->stalled) {
2616 result = _nbu2ss_start_transfer(udc, ep, req, false);
2618 dev_err(udc->dev, " *** %s, result = %d\n", __func__,
2620 list_del(&req->queue);
2621 } else if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT)) {
2623 if (req->req.length < 4 &&
2624 req->req.length == req->req.actual)
2626 if (req->req.length == req->req.actual)
2628 _nbu2ss_ep_done(ep, req, result);
2632 spin_unlock_irqrestore(&udc->lock, flags);
2637 /*-------------------------------------------------------------------------*/
2638 static int nbu2ss_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2640 struct nbu2ss_req *req;
2641 struct nbu2ss_ep *ep;
2642 struct nbu2ss_udc *udc;
2643 unsigned long flags;
2645 /* catch various bogus parameters */
2646 if (!_ep || !_req) {
2647 /* pr_err("%s, bad param(1)\n", __func__); */
2651 ep = container_of(_ep, struct nbu2ss_ep, ep);
2657 spin_lock_irqsave(&udc->lock, flags);
2659 /* make sure it's actually queued on this endpoint */
2660 list_for_each_entry(req, &ep->queue, queue) {
2661 if (&req->req == _req) {
2662 _nbu2ss_ep_done(ep, req, -ECONNRESET);
2663 spin_unlock_irqrestore(&udc->lock, flags);
2668 spin_unlock_irqrestore(&udc->lock, flags);
2670 pr_debug("%s no queue(EINVAL)\n", __func__);
2675 /*-------------------------------------------------------------------------*/
2676 static int nbu2ss_ep_set_halt(struct usb_ep *_ep, int value)
2679 unsigned long flags;
2681 struct nbu2ss_ep *ep;
2682 struct nbu2ss_udc *udc;
2685 pr_err("%s, bad param\n", __func__);
2689 ep = container_of(_ep, struct nbu2ss_ep, ep);
2693 dev_err(ep->udc->dev, " *** %s, bad udc\n", __func__);
2697 spin_lock_irqsave(&udc->lock, flags);
2699 ep_adrs = ep->epnum | ep->direct;
2701 _nbu2ss_set_endpoint_stall(udc, ep_adrs, value);
2702 ep->stalled = false;
2704 if (list_empty(&ep->queue))
2705 _nbu2ss_epn_set_stall(udc, ep);
2713 spin_unlock_irqrestore(&udc->lock, flags);
2718 static int nbu2ss_ep_set_wedge(struct usb_ep *_ep)
2720 return nbu2ss_ep_set_halt(_ep, 1);
2723 /*-------------------------------------------------------------------------*/
2724 static int nbu2ss_ep_fifo_status(struct usb_ep *_ep)
2727 struct nbu2ss_ep *ep;
2728 struct nbu2ss_udc *udc;
2729 unsigned long flags;
2730 struct fc_regs __iomem *preg;
2733 pr_err("%s, bad param\n", __func__);
2737 ep = container_of(_ep, struct nbu2ss_ep, ep);
2741 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2747 data = gpiod_get_value(vbus_gpio);
2751 spin_lock_irqsave(&udc->lock, flags);
2753 if (ep->epnum == 0) {
2754 data = _nbu2ss_readl(&preg->EP0_LENGTH) & EP0_LDATA;
2757 data = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_LEN_DCNT)
2761 spin_unlock_irqrestore(&udc->lock, flags);
2766 /*-------------------------------------------------------------------------*/
2767 static void nbu2ss_ep_fifo_flush(struct usb_ep *_ep)
2770 struct nbu2ss_ep *ep;
2771 struct nbu2ss_udc *udc;
2772 unsigned long flags;
2775 pr_err("udc: %s, bad param\n", __func__);
2779 ep = container_of(_ep, struct nbu2ss_ep, ep);
2783 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2787 data = gpiod_get_value(vbus_gpio);
2791 spin_lock_irqsave(&udc->lock, flags);
2792 _nbu2ss_fifo_flush(udc, ep);
2793 spin_unlock_irqrestore(&udc->lock, flags);
2796 /*-------------------------------------------------------------------------*/
2797 static const struct usb_ep_ops nbu2ss_ep_ops = {
2798 .enable = nbu2ss_ep_enable,
2799 .disable = nbu2ss_ep_disable,
2801 .alloc_request = nbu2ss_ep_alloc_request,
2802 .free_request = nbu2ss_ep_free_request,
2804 .queue = nbu2ss_ep_queue,
2805 .dequeue = nbu2ss_ep_dequeue,
2807 .set_halt = nbu2ss_ep_set_halt,
2808 .set_wedge = nbu2ss_ep_set_wedge,
2810 .fifo_status = nbu2ss_ep_fifo_status,
2811 .fifo_flush = nbu2ss_ep_fifo_flush,
2814 /*-------------------------------------------------------------------------*/
2815 /* usb_gadget_ops */
2817 /*-------------------------------------------------------------------------*/
2818 static int nbu2ss_gad_get_frame(struct usb_gadget *pgadget)
2821 struct nbu2ss_udc *udc;
2824 pr_err("udc: %s, bad param\n", __func__);
2828 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2829 data = gpiod_get_value(vbus_gpio);
2833 return _nbu2ss_readl(&udc->p_regs->USB_ADDRESS) & FRAME;
2836 /*-------------------------------------------------------------------------*/
2837 static int nbu2ss_gad_wakeup(struct usb_gadget *pgadget)
2842 struct nbu2ss_udc *udc;
2845 pr_err("%s, bad param\n", __func__);
2849 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2851 data = gpiod_get_value(vbus_gpio);
2853 dev_warn(&pgadget->dev, "VBUS LEVEL = %d\n", data);
2857 _nbu2ss_bitset(&udc->p_regs->EPCTR, PLL_RESUME);
2859 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2860 data = _nbu2ss_readl(&udc->p_regs->EPCTR);
2862 if (data & PLL_LOCK)
2866 _nbu2ss_bitclr(&udc->p_regs->EPCTR, PLL_RESUME);
2871 /*-------------------------------------------------------------------------*/
2872 static int nbu2ss_gad_set_selfpowered(struct usb_gadget *pgadget,
2875 struct nbu2ss_udc *udc;
2876 unsigned long flags;
2879 pr_err("%s, bad param\n", __func__);
2883 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2885 spin_lock_irqsave(&udc->lock, flags);
2886 pgadget->is_selfpowered = (is_selfpowered != 0);
2887 spin_unlock_irqrestore(&udc->lock, flags);
2892 /*-------------------------------------------------------------------------*/
2893 static int nbu2ss_gad_vbus_session(struct usb_gadget *pgadget, int is_active)
2898 /*-------------------------------------------------------------------------*/
2899 static int nbu2ss_gad_vbus_draw(struct usb_gadget *pgadget, unsigned int mA)
2901 struct nbu2ss_udc *udc;
2902 unsigned long flags;
2905 pr_err("%s, bad param\n", __func__);
2909 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2911 spin_lock_irqsave(&udc->lock, flags);
2913 spin_unlock_irqrestore(&udc->lock, flags);
2918 /*-------------------------------------------------------------------------*/
2919 static int nbu2ss_gad_pullup(struct usb_gadget *pgadget, int is_on)
2921 struct nbu2ss_udc *udc;
2922 unsigned long flags;
2925 pr_err("%s, bad param\n", __func__);
2929 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2932 pr_warn("%s, Not Regist Driver\n", __func__);
2936 if (udc->vbus_active == 0)
2939 spin_lock_irqsave(&udc->lock, flags);
2940 _nbu2ss_pullup(udc, is_on);
2941 spin_unlock_irqrestore(&udc->lock, flags);
2946 /*-------------------------------------------------------------------------*/
2947 static int nbu2ss_gad_ioctl(struct usb_gadget *pgadget,
2948 unsigned int code, unsigned long param)
2953 static const struct usb_gadget_ops nbu2ss_gadget_ops = {
2954 .get_frame = nbu2ss_gad_get_frame,
2955 .wakeup = nbu2ss_gad_wakeup,
2956 .set_selfpowered = nbu2ss_gad_set_selfpowered,
2957 .vbus_session = nbu2ss_gad_vbus_session,
2958 .vbus_draw = nbu2ss_gad_vbus_draw,
2959 .pullup = nbu2ss_gad_pullup,
2960 .ioctl = nbu2ss_gad_ioctl,
2963 static const struct {
2965 const struct usb_ep_caps caps;
2966 } ep_info[NUM_ENDPOINTS] = {
2967 #define EP_INFO(_name, _caps) \
2974 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
2976 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2978 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2979 EP_INFO("ep3in-int",
2980 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
2982 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2984 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2986 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2988 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2989 EP_INFO("ep8in-int",
2990 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
2992 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2994 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2996 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2998 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2999 EP_INFO("epdin-int",
3000 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3005 /*-------------------------------------------------------------------------*/
3006 static void nbu2ss_drv_ep_init(struct nbu2ss_udc *udc)
3010 INIT_LIST_HEAD(&udc->gadget.ep_list);
3011 udc->gadget.ep0 = &udc->ep[0].ep;
3013 for (i = 0; i < NUM_ENDPOINTS; i++) {
3014 struct nbu2ss_ep *ep = &udc->ep[i];
3019 ep->ep.driver_data = NULL;
3020 ep->ep.name = ep_info[i].name;
3021 ep->ep.caps = ep_info[i].caps;
3022 ep->ep.ops = &nbu2ss_ep_ops;
3024 usb_ep_set_maxpacket_limit(&ep->ep,
3025 i == 0 ? EP0_PACKETSIZE
3028 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
3029 INIT_LIST_HEAD(&ep->queue);
3032 list_del_init(&udc->ep[0].ep.ep_list);
3035 /*-------------------------------------------------------------------------*/
3036 /* platform_driver */
3037 static int nbu2ss_drv_contest_init(struct platform_device *pdev,
3038 struct nbu2ss_udc *udc)
3040 spin_lock_init(&udc->lock);
3041 udc->dev = &pdev->dev;
3043 udc->gadget.is_selfpowered = 1;
3044 udc->devstate = USB_STATE_NOTATTACHED;
3048 udc->pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3051 nbu2ss_drv_ep_init(udc);
3054 udc->gadget.ops = &nbu2ss_gadget_ops;
3055 udc->gadget.ep0 = &udc->ep[0].ep;
3056 udc->gadget.speed = USB_SPEED_UNKNOWN;
3057 udc->gadget.name = driver_name;
3058 /* udc->gadget.is_dualspeed = 1; */
3060 device_initialize(&udc->gadget.dev);
3062 dev_set_name(&udc->gadget.dev, "gadget");
3063 udc->gadget.dev.parent = &pdev->dev;
3064 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
3070 * probe - binds to the platform device
3072 static int nbu2ss_drv_probe(struct platform_device *pdev)
3075 struct nbu2ss_udc *udc;
3077 void __iomem *mmio_base;
3079 udc = &udc_controller;
3080 memset(udc, 0, sizeof(struct nbu2ss_udc));
3082 platform_set_drvdata(pdev, udc);
3084 /* require I/O memory and IRQ to be provided as resources */
3085 mmio_base = devm_platform_ioremap_resource(pdev, 0);
3086 if (IS_ERR(mmio_base))
3087 return PTR_ERR(mmio_base);
3089 irq = platform_get_irq(pdev, 0);
3092 status = devm_request_irq(&pdev->dev, irq, _nbu2ss_udc_irq,
3093 0, driver_name, udc);
3096 udc->p_regs = (struct fc_regs __iomem *)mmio_base;
3098 /* USB Function Controller Interrupt */
3100 dev_err(udc->dev, "request_irq(USB_UDC_IRQ_1) failed\n");
3104 /* Driver Initialization */
3105 status = nbu2ss_drv_contest_init(pdev, udc);
3111 /* VBUS Interrupt */
3112 vbus_irq = gpiod_to_irq(vbus_gpio);
3113 irq_set_irq_type(vbus_irq, IRQ_TYPE_EDGE_BOTH);
3114 status = request_irq(vbus_irq,
3115 _nbu2ss_vbus_irq, IRQF_SHARED, driver_name, udc);
3118 dev_err(udc->dev, "request_irq(vbus_irq) failed\n");
3125 /*-------------------------------------------------------------------------*/
3126 static void nbu2ss_drv_shutdown(struct platform_device *pdev)
3128 struct nbu2ss_udc *udc;
3130 udc = platform_get_drvdata(pdev);
3134 _nbu2ss_disable_controller(udc);
3137 /*-------------------------------------------------------------------------*/
3138 static int nbu2ss_drv_remove(struct platform_device *pdev)
3140 struct nbu2ss_udc *udc;
3141 struct nbu2ss_ep *ep;
3144 udc = &udc_controller;
3146 for (i = 0; i < NUM_ENDPOINTS; i++) {
3149 dma_free_coherent(udc->dev, PAGE_SIZE, (void *)ep->virt_buf,
3153 /* Interrupt Handler - Release */
3154 free_irq(vbus_irq, udc);
3159 /*-------------------------------------------------------------------------*/
3160 static int nbu2ss_drv_suspend(struct platform_device *pdev, pm_message_t state)
3162 struct nbu2ss_udc *udc;
3164 udc = platform_get_drvdata(pdev);
3168 if (udc->vbus_active) {
3169 udc->vbus_active = 0;
3170 udc->devstate = USB_STATE_NOTATTACHED;
3171 udc->linux_suspended = 1;
3173 if (udc->usb_suspended) {
3174 udc->usb_suspended = 0;
3175 _nbu2ss_reset_controller(udc);
3178 _nbu2ss_quiesce(udc);
3180 _nbu2ss_disable_controller(udc);
3185 /*-------------------------------------------------------------------------*/
3186 static int nbu2ss_drv_resume(struct platform_device *pdev)
3189 struct nbu2ss_udc *udc;
3191 udc = platform_get_drvdata(pdev);
3195 data = gpiod_get_value(vbus_gpio);
3197 udc->vbus_active = 1;
3198 udc->devstate = USB_STATE_POWERED;
3199 _nbu2ss_enable_controller(udc);
3200 _nbu2ss_pullup(udc, 1);
3203 udc->linux_suspended = 0;
3208 static struct platform_driver udc_driver = {
3209 .probe = nbu2ss_drv_probe,
3210 .shutdown = nbu2ss_drv_shutdown,
3211 .remove = nbu2ss_drv_remove,
3212 .suspend = nbu2ss_drv_suspend,
3213 .resume = nbu2ss_drv_resume,
3215 .name = driver_name,
3219 module_platform_driver(udc_driver);
3221 MODULE_DESCRIPTION(DRIVER_DESC);
3222 MODULE_AUTHOR("Renesas Electronics Corporation");
3223 MODULE_LICENSE("GPL");