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 i_write_length = min(DMA_MAX_COUNT * mpkt, length);
1009 /*------------------------------------------------------------*/
1010 /* Number of transmission packets */
1011 if (mpkt < i_write_length) {
1012 dmacnt = i_write_length / mpkt;
1013 lmpkt = (i_write_length % mpkt) & ~(u32)0x3;
1017 lmpkt = mpkt & ~(u32)0x3;
1021 lmpkt = i_write_length & ~(u32)0x3;
1024 /* Packet setting */
1025 data = mpkt | (lmpkt << 16);
1026 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
1028 /* Address setting */
1029 p_buffer = req->req.dma;
1030 p_buffer += req->req.actual;
1031 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)p_buffer);
1033 /* Packet and DMA setting */
1034 data = ((dmacnt & 0xff) << 16) | DCR1_EPN_REQEN;
1035 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
1037 /* Packet setting of EPC */
1038 data = dmacnt << 16;
1039 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, data);
1041 /*DMA setting of EPC */
1042 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPN_DMA_EN);
1044 result = i_write_length & ~(u32)0x3;
1045 req->div_len = result;
1050 /*-------------------------------------------------------------------------*/
1051 static int _nbu2ss_epn_in_pio(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
1052 struct nbu2ss_req *req, u32 length)
1058 union usb_reg_access temp_32;
1059 union usb_reg_access *p_buf_32 = NULL;
1061 struct fc_regs __iomem *preg = udc->p_regs;
1064 return 1; /* DMA is forwarded */
1067 p_buffer = (u8 *)req->req.buf;
1068 p_buf_32 = (union usb_reg_access *)(p_buffer + req->req.actual);
1070 i_word_length = length / sizeof(u32);
1071 if (i_word_length > 0) {
1072 for (i = 0; i < i_word_length; i++) {
1073 _nbu2ss_writel(&preg->EP_REGS[ep->epnum - 1].EP_WRITE,
1078 result = i_word_length * sizeof(u32);
1082 if (result != ep->ep.maxpacket) {
1083 data = length - result;
1085 for (i = 0 ; i < data ; i++)
1086 temp_32.byte.DATA[i] = p_buf_32->byte.DATA[i];
1088 _nbu2ss_ep_in_end(udc, ep->epnum, temp_32.dw, data);
1092 req->div_len = result;
1097 /*-------------------------------------------------------------------------*/
1098 static int _nbu2ss_epn_in_data(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep,
1099 struct nbu2ss_req *req, u32 data_size)
1107 num = ep->epnum - 1;
1109 if ((ep->ep_type != USB_ENDPOINT_XFER_INT) && (req->req.dma != 0) &&
1110 (data_size >= sizeof(u32))) {
1111 nret = _nbu2ss_in_dma(udc, ep, req, num, data_size);
1113 data_size = min_t(u32, data_size, ep->ep.maxpacket);
1114 nret = _nbu2ss_epn_in_pio(udc, ep, req, data_size);
1120 /*-------------------------------------------------------------------------*/
1121 static int _nbu2ss_epn_in_transfer(struct nbu2ss_udc *udc,
1122 struct nbu2ss_ep *ep, struct nbu2ss_req *req)
1132 num = ep->epnum - 1;
1134 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
1136 /*-------------------------------------------------------------*/
1137 /* State confirmation of FIFO */
1138 if (req->req.actual == 0) {
1139 if ((status & EPN_IN_EMPTY) == 0)
1140 return 1; /* Not Empty */
1143 if ((status & EPN_IN_FULL) != 0)
1144 return 1; /* Not Empty */
1147 /*-------------------------------------------------------------*/
1148 /* Start transfer */
1149 i_buf_size = req->req.length - req->req.actual;
1151 result = _nbu2ss_epn_in_data(udc, ep, req, i_buf_size);
1152 else if (req->req.length == 0)
1153 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1158 /*-------------------------------------------------------------------------*/
1159 static int _nbu2ss_start_transfer(struct nbu2ss_udc *udc,
1160 struct nbu2ss_ep *ep,
1161 struct nbu2ss_req *req,
1166 req->dma_flag = false;
1169 if (req->req.length == 0) {
1172 if ((req->req.length % ep->ep.maxpacket) == 0)
1173 req->zero = req->req.zero;
1178 if (ep->epnum == 0) {
1180 switch (udc->ep0state) {
1181 case EP0_IN_DATA_PHASE:
1182 nret = _nbu2ss_ep0_in_transfer(udc, req);
1185 case EP0_OUT_DATA_PHASE:
1186 nret = _nbu2ss_ep0_out_transfer(udc, req);
1189 case EP0_IN_STATUS_PHASE:
1190 nret = EP0_send_NULL(udc, true);
1199 if (ep->direct == USB_DIR_OUT) {
1202 nret = _nbu2ss_epn_out_transfer(udc, ep, req);
1205 nret = _nbu2ss_epn_in_transfer(udc, ep, req);
1212 /*-------------------------------------------------------------------------*/
1213 static void _nbu2ss_restert_transfer(struct nbu2ss_ep *ep)
1217 struct nbu2ss_req *req;
1219 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1223 if (ep->epnum > 0) {
1224 length = _nbu2ss_readl(&ep->udc->p_regs->EP_REGS[ep->epnum - 1].EP_LEN_DCNT);
1226 length &= EPN_LDATA;
1227 if (length < ep->ep.maxpacket)
1231 _nbu2ss_start_transfer(ep->udc, ep, req, bflag);
1234 /*-------------------------------------------------------------------------*/
1235 /* Endpoint Toggle Reset */
1236 static void _nbu2ss_endpoint_toggle_reset(struct nbu2ss_udc *udc, u8 ep_adrs)
1241 if ((ep_adrs == 0) || (ep_adrs == 0x80))
1244 num = (ep_adrs & 0x7F) - 1;
1246 if (ep_adrs & USB_DIR_IN)
1249 data = EPN_BCLR | EPN_OPIDCLR;
1251 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
1254 /*-------------------------------------------------------------------------*/
1255 /* Endpoint STALL set */
1256 static void _nbu2ss_set_endpoint_stall(struct nbu2ss_udc *udc,
1257 u8 ep_adrs, bool bstall)
1261 struct nbu2ss_ep *ep;
1262 struct fc_regs __iomem *preg = udc->p_regs;
1264 if ((ep_adrs == 0) || (ep_adrs == 0x80)) {
1267 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_STL);
1270 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_STL);
1273 epnum = ep_adrs & USB_ENDPOINT_NUMBER_MASK;
1275 ep = &udc->ep[epnum];
1281 if (ep_adrs & USB_DIR_IN)
1282 data = EPN_BCLR | EPN_ISTL;
1284 data = EPN_OSTL_EN | EPN_OSTL;
1286 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
1288 if (ep_adrs & USB_DIR_IN) {
1289 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL
1293 _nbu2ss_readl(&preg->EP_REGS[num].EP_CONTROL);
1296 data |= EPN_OSTL_EN;
1298 _nbu2ss_writel(&preg->EP_REGS[num].EP_CONTROL
1303 ep->stalled = false;
1306 _nbu2ss_restert_transfer(ep);
1312 /*-------------------------------------------------------------------------*/
1313 static void _nbu2ss_set_test_mode(struct nbu2ss_udc *udc, u32 mode)
1317 if (mode > MAX_TEST_MODE_NUM)
1320 dev_info(udc->dev, "SET FEATURE : test mode = %d\n", mode);
1322 data = _nbu2ss_readl(&udc->p_regs->USB_CONTROL);
1323 data &= ~TEST_FORCE_ENABLE;
1324 data |= mode << TEST_MODE_SHIFT;
1326 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, data);
1327 _nbu2ss_bitset(&udc->p_regs->TEST_CONTROL, CS_TESTMODEEN);
1330 /*-------------------------------------------------------------------------*/
1331 static int _nbu2ss_set_feature_device(struct nbu2ss_udc *udc,
1332 u16 selector, u16 wIndex)
1334 int result = -EOPNOTSUPP;
1337 case USB_DEVICE_REMOTE_WAKEUP:
1338 if (wIndex == 0x0000) {
1339 udc->remote_wakeup = U2F_ENABLE;
1344 case USB_DEVICE_TEST_MODE:
1346 if (wIndex <= MAX_TEST_MODE_NUM)
1357 /*-------------------------------------------------------------------------*/
1358 static int _nbu2ss_get_ep_stall(struct nbu2ss_udc *udc, u8 ep_adrs)
1361 u32 data = 0, bit_data;
1362 struct fc_regs __iomem *preg = udc->p_regs;
1364 epnum = ep_adrs & ~USB_ENDPOINT_DIR_MASK;
1366 data = _nbu2ss_readl(&preg->EP0_CONTROL);
1370 data = _nbu2ss_readl(&preg->EP_REGS[epnum - 1].EP_CONTROL);
1371 if ((data & EPN_EN) == 0)
1374 if (ep_adrs & USB_ENDPOINT_DIR_MASK)
1375 bit_data = EPN_ISTL;
1377 bit_data = EPN_OSTL;
1380 if ((data & bit_data) == 0)
1385 /*-------------------------------------------------------------------------*/
1386 static inline int _nbu2ss_req_feature(struct nbu2ss_udc *udc, bool bset)
1388 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1389 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1390 u16 selector = le16_to_cpu(udc->ctrl.wValue);
1391 u16 wIndex = le16_to_cpu(udc->ctrl.wIndex);
1393 int result = -EOPNOTSUPP;
1395 if ((udc->ctrl.wLength != 0x0000) ||
1396 (direction != USB_DIR_OUT)) {
1400 switch (recipient) {
1401 case USB_RECIP_DEVICE:
1404 _nbu2ss_set_feature_device(udc, selector, wIndex);
1407 case USB_RECIP_ENDPOINT:
1408 if (0x0000 == (wIndex & 0xFF70)) {
1409 if (selector == USB_ENDPOINT_HALT) {
1410 ep_adrs = wIndex & 0xFF;
1412 _nbu2ss_endpoint_toggle_reset(udc,
1416 _nbu2ss_set_endpoint_stall(udc, ep_adrs, bset);
1428 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1433 /*-------------------------------------------------------------------------*/
1434 static inline enum usb_device_speed _nbu2ss_get_speed(struct nbu2ss_udc *udc)
1437 enum usb_device_speed speed = USB_SPEED_FULL;
1439 data = _nbu2ss_readl(&udc->p_regs->USB_STATUS);
1440 if (data & HIGH_SPEED)
1441 speed = USB_SPEED_HIGH;
1446 /*-------------------------------------------------------------------------*/
1447 static void _nbu2ss_epn_set_stall(struct nbu2ss_udc *udc,
1448 struct nbu2ss_ep *ep)
1454 struct fc_regs __iomem *preg = udc->p_regs;
1456 if (ep->direct == USB_DIR_IN) {
1458 ; limit_cnt < IN_DATA_EMPTY_COUNT
1460 regdata = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1462 if ((regdata & EPN_IN_DATA) == 0)
1469 ep_adrs = ep->epnum | ep->direct;
1470 _nbu2ss_set_endpoint_stall(udc, ep_adrs, 1);
1473 /*-------------------------------------------------------------------------*/
1474 static int std_req_get_status(struct nbu2ss_udc *udc)
1477 u16 status_data = 0;
1478 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1479 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1481 int result = -EINVAL;
1483 if ((udc->ctrl.wValue != 0x0000) || (direction != USB_DIR_IN))
1487 min_t(u16, le16_to_cpu(udc->ctrl.wLength), sizeof(status_data));
1488 switch (recipient) {
1489 case USB_RECIP_DEVICE:
1490 if (udc->ctrl.wIndex == 0x0000) {
1491 if (udc->gadget.is_selfpowered)
1492 status_data |= BIT(USB_DEVICE_SELF_POWERED);
1494 if (udc->remote_wakeup)
1495 status_data |= BIT(USB_DEVICE_REMOTE_WAKEUP);
1501 case USB_RECIP_ENDPOINT:
1502 if (0x0000 == (le16_to_cpu(udc->ctrl.wIndex) & 0xFF70)) {
1503 ep_adrs = (u8)(le16_to_cpu(udc->ctrl.wIndex) & 0xFF);
1504 result = _nbu2ss_get_ep_stall(udc, ep_adrs);
1507 status_data |= BIT(USB_ENDPOINT_HALT);
1516 memcpy(udc->ep0_buf, &status_data, length);
1517 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, length);
1518 _nbu2ss_ep0_in_transfer(udc, &udc->ep0_req);
1521 dev_err(udc->dev, " Error GET_STATUS\n");
1527 /*-------------------------------------------------------------------------*/
1528 static int std_req_clear_feature(struct nbu2ss_udc *udc)
1530 return _nbu2ss_req_feature(udc, false);
1533 /*-------------------------------------------------------------------------*/
1534 static int std_req_set_feature(struct nbu2ss_udc *udc)
1536 return _nbu2ss_req_feature(udc, true);
1539 /*-------------------------------------------------------------------------*/
1540 static int std_req_set_address(struct nbu2ss_udc *udc)
1543 u32 wValue = le16_to_cpu(udc->ctrl.wValue);
1545 if ((udc->ctrl.bRequestType != 0x00) ||
1546 (udc->ctrl.wIndex != 0x0000) ||
1547 (udc->ctrl.wLength != 0x0000)) {
1551 if (wValue != (wValue & 0x007F))
1554 wValue <<= USB_ADRS_SHIFT;
1556 _nbu2ss_writel(&udc->p_regs->USB_ADDRESS, wValue);
1557 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1562 /*-------------------------------------------------------------------------*/
1563 static int std_req_set_configuration(struct nbu2ss_udc *udc)
1565 u32 config_value = (u32)(le16_to_cpu(udc->ctrl.wValue) & 0x00ff);
1567 if ((udc->ctrl.wIndex != 0x0000) ||
1568 (udc->ctrl.wLength != 0x0000) ||
1569 (udc->ctrl.bRequestType != 0x00)) {
1573 udc->curr_config = config_value;
1575 if (config_value > 0) {
1576 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, CONF);
1577 udc->devstate = USB_STATE_CONFIGURED;
1580 _nbu2ss_bitclr(&udc->p_regs->USB_CONTROL, CONF);
1581 udc->devstate = USB_STATE_ADDRESS;
1587 /*-------------------------------------------------------------------------*/
1588 static inline void _nbu2ss_read_request_data(struct nbu2ss_udc *udc, u32 *pdata)
1590 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA0);
1592 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA1);
1595 /*-------------------------------------------------------------------------*/
1596 static inline int _nbu2ss_decode_request(struct nbu2ss_udc *udc)
1598 bool bcall_back = true;
1600 struct usb_ctrlrequest *p_ctrl;
1602 p_ctrl = &udc->ctrl;
1603 _nbu2ss_read_request_data(udc, (u32 *)p_ctrl);
1605 /* ep0 state control */
1606 if (p_ctrl->wLength == 0) {
1607 udc->ep0state = EP0_IN_STATUS_PHASE;
1610 if (p_ctrl->bRequestType & USB_DIR_IN)
1611 udc->ep0state = EP0_IN_DATA_PHASE;
1613 udc->ep0state = EP0_OUT_DATA_PHASE;
1616 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1617 switch (p_ctrl->bRequest) {
1618 case USB_REQ_GET_STATUS:
1619 nret = std_req_get_status(udc);
1623 case USB_REQ_CLEAR_FEATURE:
1624 nret = std_req_clear_feature(udc);
1628 case USB_REQ_SET_FEATURE:
1629 nret = std_req_set_feature(udc);
1633 case USB_REQ_SET_ADDRESS:
1634 nret = std_req_set_address(udc);
1638 case USB_REQ_SET_CONFIGURATION:
1639 nret = std_req_set_configuration(udc);
1648 if (udc->ep0state == EP0_IN_STATUS_PHASE) {
1650 /*--------------------------------------*/
1652 nret = EP0_send_NULL(udc, true);
1657 spin_unlock(&udc->lock);
1658 nret = udc->driver->setup(&udc->gadget, &udc->ctrl);
1659 spin_lock(&udc->lock);
1663 udc->ep0state = EP0_IDLE;
1668 /*-------------------------------------------------------------------------*/
1669 static inline int _nbu2ss_ep0_in_data_stage(struct nbu2ss_udc *udc)
1672 struct nbu2ss_req *req;
1673 struct nbu2ss_ep *ep = &udc->ep[0];
1675 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1677 req = &udc->ep0_req;
1679 req->req.actual += req->div_len;
1682 nret = _nbu2ss_ep0_in_transfer(udc, req);
1684 udc->ep0state = EP0_OUT_STATUS_PAHSE;
1685 EP0_receive_NULL(udc, true);
1691 /*-------------------------------------------------------------------------*/
1692 static inline int _nbu2ss_ep0_out_data_stage(struct nbu2ss_udc *udc)
1695 struct nbu2ss_req *req;
1696 struct nbu2ss_ep *ep = &udc->ep[0];
1698 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1700 req = &udc->ep0_req;
1702 nret = _nbu2ss_ep0_out_transfer(udc, req);
1704 udc->ep0state = EP0_IN_STATUS_PHASE;
1705 EP0_send_NULL(udc, true);
1707 } else if (nret < 0) {
1708 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, EP0_BCLR);
1709 req->req.status = nret;
1715 /*-------------------------------------------------------------------------*/
1716 static inline int _nbu2ss_ep0_status_stage(struct nbu2ss_udc *udc)
1718 struct nbu2ss_req *req;
1719 struct nbu2ss_ep *ep = &udc->ep[0];
1721 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
1723 req = &udc->ep0_req;
1724 if (req->req.complete)
1725 req->req.complete(&ep->ep, &req->req);
1728 if (req->req.complete)
1729 _nbu2ss_ep_done(ep, req, 0);
1732 udc->ep0state = EP0_IDLE;
1737 /*-------------------------------------------------------------------------*/
1738 static inline void _nbu2ss_ep0_int(struct nbu2ss_udc *udc)
1745 status = _nbu2ss_readl(&udc->p_regs->EP0_STATUS);
1746 intr = status & EP0_STATUS_RW_BIT;
1747 _nbu2ss_writel(&udc->p_regs->EP0_STATUS, ~intr);
1749 status &= (SETUP_INT | EP0_IN_INT | EP0_OUT_INT
1750 | STG_END_INT | EP0_OUT_NULL_INT);
1753 dev_info(udc->dev, "%s Not Decode Interrupt\n", __func__);
1754 dev_info(udc->dev, "EP0_STATUS = 0x%08x\n", intr);
1758 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1759 udc->gadget.speed = _nbu2ss_get_speed(udc);
1761 for (i = 0; i < EP0_END_XFER; i++) {
1762 switch (udc->ep0state) {
1764 if (status & SETUP_INT) {
1766 nret = _nbu2ss_decode_request(udc);
1770 case EP0_IN_DATA_PHASE:
1771 if (status & EP0_IN_INT) {
1772 status &= ~EP0_IN_INT;
1773 nret = _nbu2ss_ep0_in_data_stage(udc);
1777 case EP0_OUT_DATA_PHASE:
1778 if (status & EP0_OUT_INT) {
1779 status &= ~EP0_OUT_INT;
1780 nret = _nbu2ss_ep0_out_data_stage(udc);
1784 case EP0_IN_STATUS_PHASE:
1785 if ((status & STG_END_INT) || (status & SETUP_INT)) {
1786 status &= ~(STG_END_INT | EP0_IN_INT);
1787 nret = _nbu2ss_ep0_status_stage(udc);
1791 case EP0_OUT_STATUS_PAHSE:
1792 if ((status & STG_END_INT) || (status & SETUP_INT) ||
1793 (status & EP0_OUT_NULL_INT)) {
1794 status &= ~(STG_END_INT
1796 | EP0_OUT_NULL_INT);
1798 nret = _nbu2ss_ep0_status_stage(udc);
1814 _nbu2ss_set_endpoint_stall(udc, 0, true);
1818 /*-------------------------------------------------------------------------*/
1819 static void _nbu2ss_ep_done(struct nbu2ss_ep *ep,
1820 struct nbu2ss_req *req,
1823 struct nbu2ss_udc *udc = ep->udc;
1825 list_del_init(&req->queue);
1827 if (status == -ECONNRESET)
1828 _nbu2ss_fifo_flush(udc, ep);
1830 if (likely(req->req.status == -EINPROGRESS))
1831 req->req.status = status;
1834 _nbu2ss_epn_set_stall(udc, ep);
1836 if (!list_empty(&ep->queue))
1837 _nbu2ss_restert_transfer(ep);
1841 if ((ep->direct == USB_DIR_OUT) && (ep->epnum > 0) &&
1842 (req->req.dma != 0))
1843 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_OUT);
1846 spin_unlock(&udc->lock);
1847 req->req.complete(&ep->ep, &req->req);
1848 spin_lock(&udc->lock);
1851 /*-------------------------------------------------------------------------*/
1852 static inline void _nbu2ss_epn_in_int(struct nbu2ss_udc *udc,
1853 struct nbu2ss_ep *ep,
1854 struct nbu2ss_req *req)
1859 struct fc_regs __iomem *preg = udc->p_regs;
1862 return; /* DMA is forwarded */
1864 req->req.actual += req->div_len;
1867 if (req->req.actual != req->req.length) {
1868 /*---------------------------------------------------------*/
1869 /* remainder of data */
1870 result = _nbu2ss_epn_in_transfer(udc, ep, req);
1873 if (req->zero && ((req->req.actual % ep->ep.maxpacket) == 0)) {
1875 _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_STATUS);
1877 if ((status & EPN_IN_FULL) == 0) {
1878 /*-----------------------------------------*/
1879 /* 0 Length Packet */
1881 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1888 /*---------------------------------------------------------*/
1890 _nbu2ss_ep_done(ep, req, result);
1894 /*-------------------------------------------------------------------------*/
1895 static inline void _nbu2ss_epn_out_int(struct nbu2ss_udc *udc,
1896 struct nbu2ss_ep *ep,
1897 struct nbu2ss_req *req)
1901 result = _nbu2ss_epn_out_transfer(udc, ep, req);
1903 _nbu2ss_ep_done(ep, req, result);
1906 /*-------------------------------------------------------------------------*/
1907 static inline void _nbu2ss_epn_in_dma_int(struct nbu2ss_udc *udc,
1908 struct nbu2ss_ep *ep,
1909 struct nbu2ss_req *req)
1913 struct usb_request *preq;
1920 preq->actual += req->div_len;
1922 req->dma_flag = false;
1925 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_IN);
1928 if (preq->actual != preq->length) {
1929 _nbu2ss_epn_in_transfer(udc, ep, req);
1931 mpkt = ep->ep.maxpacket;
1932 size = preq->actual % mpkt;
1934 if (((preq->actual & 0x03) == 0) && (size < mpkt))
1935 _nbu2ss_ep_in_end(udc, ep->epnum, 0, 0);
1937 _nbu2ss_epn_in_int(udc, ep, req);
1942 /*-------------------------------------------------------------------------*/
1943 static inline void _nbu2ss_epn_out_dma_int(struct nbu2ss_udc *udc,
1944 struct nbu2ss_ep *ep,
1945 struct nbu2ss_req *req)
1949 u32 dmacnt, ep_dmacnt;
1951 struct fc_regs __iomem *preg = udc->p_regs;
1953 num = ep->epnum - 1;
1955 if (req->req.actual == req->req.length) {
1956 if ((req->req.length % ep->ep.maxpacket) && !req->zero) {
1958 req->dma_flag = false;
1959 _nbu2ss_ep_done(ep, req, 0);
1964 ep_dmacnt = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT)
1968 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
1969 dmacnt = _nbu2ss_readl(&preg->EP_DCR[num].EP_DCR1)
1972 if (ep_dmacnt == dmacnt)
1976 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPN_REQEN);
1979 mpkt = ep->ep.maxpacket;
1980 if ((req->div_len % mpkt) == 0)
1981 req->div_len -= mpkt * dmacnt;
1984 if ((req->req.actual % ep->ep.maxpacket) > 0) {
1985 if (req->req.actual == req->div_len) {
1987 req->dma_flag = false;
1988 _nbu2ss_ep_done(ep, req, 0);
1993 req->req.actual += req->div_len;
1995 req->dma_flag = false;
1997 _nbu2ss_epn_out_int(udc, ep, req);
2000 /*-------------------------------------------------------------------------*/
2001 static inline void _nbu2ss_epn_int(struct nbu2ss_udc *udc, u32 epnum)
2006 struct nbu2ss_req *req;
2007 struct nbu2ss_ep *ep = &udc->ep[epnum];
2011 /* Interrupt Status */
2012 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
2014 /* Interrupt Clear */
2015 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_STATUS, ~status);
2017 req = list_first_entry_or_null(&ep->queue, struct nbu2ss_req, queue);
2019 /* pr_warn("=== %s(%d) req == NULL\n", __func__, epnum); */
2023 if (status & EPN_OUT_END_INT) {
2024 status &= ~EPN_OUT_INT;
2025 _nbu2ss_epn_out_dma_int(udc, ep, req);
2028 if (status & EPN_OUT_INT)
2029 _nbu2ss_epn_out_int(udc, ep, req);
2031 if (status & EPN_IN_END_INT) {
2032 status &= ~EPN_IN_INT;
2033 _nbu2ss_epn_in_dma_int(udc, ep, req);
2036 if (status & EPN_IN_INT)
2037 _nbu2ss_epn_in_int(udc, ep, req);
2040 /*-------------------------------------------------------------------------*/
2041 static inline void _nbu2ss_ep_int(struct nbu2ss_udc *udc, u32 epnum)
2044 _nbu2ss_ep0_int(udc);
2046 _nbu2ss_epn_int(udc, epnum);
2049 /*-------------------------------------------------------------------------*/
2050 static void _nbu2ss_ep0_enable(struct nbu2ss_udc *udc)
2052 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, (EP0_AUTO | EP0_BCLR));
2053 _nbu2ss_writel(&udc->p_regs->EP0_INT_ENA, EP0_INT_EN_BIT);
2056 /*-------------------------------------------------------------------------*/
2057 static int _nbu2ss_nuke(struct nbu2ss_udc *udc,
2058 struct nbu2ss_ep *ep,
2061 struct nbu2ss_req *req, *n;
2063 /* Endpoint Disable */
2064 _nbu2ss_epn_exit(udc, ep);
2067 _nbu2ss_ep_dma_exit(udc, ep);
2069 if (list_empty(&ep->queue))
2072 /* called with irqs blocked */
2073 list_for_each_entry_safe(req, n, &ep->queue, queue) {
2074 _nbu2ss_ep_done(ep, req, status);
2080 /*-------------------------------------------------------------------------*/
2081 static void _nbu2ss_quiesce(struct nbu2ss_udc *udc)
2083 struct nbu2ss_ep *ep;
2085 udc->gadget.speed = USB_SPEED_UNKNOWN;
2087 _nbu2ss_nuke(udc, &udc->ep[0], -ESHUTDOWN);
2090 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2091 _nbu2ss_nuke(udc, ep, -ESHUTDOWN);
2095 /*-------------------------------------------------------------------------*/
2096 static int _nbu2ss_pullup(struct nbu2ss_udc *udc, int is_on)
2100 if (udc->vbus_active == 0)
2106 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL)
2107 | PUE2) & ~(u32)CONNECTB;
2109 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2114 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL) | CONNECTB)
2117 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2118 udc->gadget.speed = USB_SPEED_UNKNOWN;
2124 /*-------------------------------------------------------------------------*/
2125 static void _nbu2ss_fifo_flush(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
2127 struct fc_regs __iomem *p = udc->p_regs;
2129 if (udc->vbus_active == 0)
2132 if (ep->epnum == 0) {
2134 _nbu2ss_bitset(&p->EP0_CONTROL, EP0_BCLR);
2138 _nbu2ss_ep_dma_abort(udc, ep);
2139 _nbu2ss_bitset(&p->EP_REGS[ep->epnum - 1].EP_CONTROL, EPN_BCLR);
2143 /*-------------------------------------------------------------------------*/
2144 static int _nbu2ss_enable_controller(struct nbu2ss_udc *udc)
2148 if (udc->udc_enabled)
2152 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2153 udelay(EPC_RST_DISABLE_TIME); /* 1us wait */
2155 _nbu2ss_bitclr(&udc->p_regs->EPCTR, DIRPD);
2156 mdelay(EPC_DIRPD_DISABLE_TIME); /* 1ms wait */
2158 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2160 _nbu2ss_writel(&udc->p_regs->AHBSCTR, WAIT_MODE);
2162 _nbu2ss_writel(&udc->p_regs->AHBMCTR,
2163 HBUSREQ_MODE | HTRANS_MODE | WBURST_TYPE);
2165 while (!(_nbu2ss_readl(&udc->p_regs->EPCTR) & PLL_LOCK)) {
2167 udelay(1); /* 1us wait */
2168 if (waitcnt == EPC_PLL_LOCK_COUNT) {
2169 dev_err(udc->dev, "*** Reset Cancel failed\n");
2174 _nbu2ss_bitset(&udc->p_regs->UTMI_CHARACTER_1, USB_SQUSET);
2176 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, (INT_SEL | SOF_RCV));
2179 _nbu2ss_ep0_enable(udc);
2181 /* USB Interrupt Enable */
2182 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, USB_INT_EN_BIT);
2184 udc->udc_enabled = true;
2189 /*-------------------------------------------------------------------------*/
2190 static void _nbu2ss_reset_controller(struct nbu2ss_udc *udc)
2192 _nbu2ss_bitset(&udc->p_regs->EPCTR, EPC_RST);
2193 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2196 /*-------------------------------------------------------------------------*/
2197 static void _nbu2ss_disable_controller(struct nbu2ss_udc *udc)
2199 if (udc->udc_enabled) {
2200 udc->udc_enabled = false;
2201 _nbu2ss_reset_controller(udc);
2202 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2206 /*-------------------------------------------------------------------------*/
2207 static inline void _nbu2ss_check_vbus(struct nbu2ss_udc *udc)
2213 mdelay(VBUS_CHATTERING_MDELAY); /* wait (ms) */
2216 reg_dt = gpiod_get_value(vbus_gpio);
2218 udc->linux_suspended = 0;
2220 _nbu2ss_reset_controller(udc);
2221 dev_info(udc->dev, " ----- VBUS OFF\n");
2223 if (udc->vbus_active == 1) {
2225 udc->vbus_active = 0;
2226 if (udc->usb_suspended) {
2227 udc->usb_suspended = 0;
2228 /* _nbu2ss_reset_controller(udc); */
2230 udc->devstate = USB_STATE_NOTATTACHED;
2232 _nbu2ss_quiesce(udc);
2234 spin_unlock(&udc->lock);
2235 udc->driver->disconnect(&udc->gadget);
2236 spin_lock(&udc->lock);
2239 _nbu2ss_disable_controller(udc);
2242 mdelay(5); /* wait (5ms) */
2243 reg_dt = gpiod_get_value(vbus_gpio);
2247 dev_info(udc->dev, " ----- VBUS ON\n");
2249 if (udc->linux_suspended)
2252 if (udc->vbus_active == 0) {
2254 udc->vbus_active = 1;
2255 udc->devstate = USB_STATE_POWERED;
2257 nret = _nbu2ss_enable_controller(udc);
2259 _nbu2ss_disable_controller(udc);
2260 udc->vbus_active = 0;
2264 _nbu2ss_pullup(udc, 1);
2266 #ifdef UDC_DEBUG_DUMP
2267 _nbu2ss_dump_register(udc);
2268 #endif /* UDC_DEBUG_DUMP */
2271 if (udc->devstate == USB_STATE_POWERED)
2272 _nbu2ss_pullup(udc, 1);
2277 /*-------------------------------------------------------------------------*/
2278 static inline void _nbu2ss_int_bus_reset(struct nbu2ss_udc *udc)
2280 udc->devstate = USB_STATE_DEFAULT;
2281 udc->remote_wakeup = 0;
2283 _nbu2ss_quiesce(udc);
2285 udc->ep0state = EP0_IDLE;
2288 /*-------------------------------------------------------------------------*/
2289 static inline void _nbu2ss_int_usb_resume(struct nbu2ss_udc *udc)
2291 if (udc->usb_suspended == 1) {
2292 udc->usb_suspended = 0;
2293 if (udc->driver && udc->driver->resume) {
2294 spin_unlock(&udc->lock);
2295 udc->driver->resume(&udc->gadget);
2296 spin_lock(&udc->lock);
2301 /*-------------------------------------------------------------------------*/
2302 static inline void _nbu2ss_int_usb_suspend(struct nbu2ss_udc *udc)
2306 if (udc->usb_suspended == 0) {
2307 reg_dt = gpiod_get_value(vbus_gpio);
2312 udc->usb_suspended = 1;
2313 if (udc->driver && udc->driver->suspend) {
2314 spin_unlock(&udc->lock);
2315 udc->driver->suspend(&udc->gadget);
2316 spin_lock(&udc->lock);
2319 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, SUSPEND);
2323 /*-------------------------------------------------------------------------*/
2324 /* VBUS (GPIO153) Interrupt */
2325 static irqreturn_t _nbu2ss_vbus_irq(int irq, void *_udc)
2327 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2329 spin_lock(&udc->lock);
2330 _nbu2ss_check_vbus(udc);
2331 spin_unlock(&udc->lock);
2336 /*-------------------------------------------------------------------------*/
2337 /* Interrupt (udc) */
2338 static irqreturn_t _nbu2ss_udc_irq(int irq, void *_udc)
2340 u8 suspend_flag = 0;
2344 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2345 struct fc_regs __iomem *preg = udc->p_regs;
2347 if (gpiod_get_value(vbus_gpio) == 0) {
2348 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2349 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2353 spin_lock(&udc->lock);
2356 if (gpiod_get_value(vbus_gpio) == 0) {
2357 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2358 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2361 status = _nbu2ss_readl(&preg->USB_INT_STA);
2367 _nbu2ss_writel(&preg->USB_INT_STA, ~(status & USB_INT_STA_RW));
2369 if (status & USB_RST_INT) {
2371 _nbu2ss_int_bus_reset(udc);
2374 if (status & RSUM_INT) {
2376 _nbu2ss_int_usb_resume(udc);
2379 if (status & SPND_INT) {
2384 if (status & EPN_INT) {
2386 int_bit = status >> 8;
2388 for (epnum = 0; epnum < NUM_ENDPOINTS; epnum++) {
2390 _nbu2ss_ep_int(udc, epnum);
2401 _nbu2ss_int_usb_suspend(udc);
2403 spin_unlock(&udc->lock);
2408 /*-------------------------------------------------------------------------*/
2410 static int nbu2ss_ep_enable(struct usb_ep *_ep,
2411 const struct usb_endpoint_descriptor *desc)
2414 unsigned long flags;
2416 struct nbu2ss_ep *ep;
2417 struct nbu2ss_udc *udc;
2419 if (!_ep || !desc) {
2420 pr_err(" *** %s, bad param\n", __func__);
2424 ep = container_of(_ep, struct nbu2ss_ep, ep);
2426 pr_err(" *** %s, ep == NULL !!\n", __func__);
2430 ep_type = usb_endpoint_type(desc);
2431 if ((ep_type == USB_ENDPOINT_XFER_CONTROL) ||
2432 (ep_type == USB_ENDPOINT_XFER_ISOC)) {
2433 pr_err(" *** %s, bat bmAttributes\n", __func__);
2438 if (udc->vbus_active == 0)
2441 if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
2442 dev_err(ep->udc->dev, " *** %s, udc !!\n", __func__);
2446 spin_lock_irqsave(&udc->lock, flags);
2449 ep->epnum = usb_endpoint_num(desc);
2450 ep->direct = desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2451 ep->ep_type = ep_type;
2454 ep->stalled = false;
2456 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2459 _nbu2ss_ep_dma_init(udc, ep);
2461 /* Endpoint setting */
2462 _nbu2ss_ep_init(udc, ep);
2464 spin_unlock_irqrestore(&udc->lock, flags);
2469 /*-------------------------------------------------------------------------*/
2470 static int nbu2ss_ep_disable(struct usb_ep *_ep)
2472 struct nbu2ss_ep *ep;
2473 struct nbu2ss_udc *udc;
2474 unsigned long flags;
2477 pr_err(" *** %s, bad param\n", __func__);
2481 ep = container_of(_ep, struct nbu2ss_ep, ep);
2483 pr_err("udc: *** %s, ep == NULL !!\n", __func__);
2488 if (udc->vbus_active == 0)
2491 spin_lock_irqsave(&udc->lock, flags);
2492 _nbu2ss_nuke(udc, ep, -EINPROGRESS); /* dequeue request */
2493 spin_unlock_irqrestore(&udc->lock, flags);
2498 /*-------------------------------------------------------------------------*/
2499 static struct usb_request *nbu2ss_ep_alloc_request(struct usb_ep *ep,
2502 struct nbu2ss_req *req;
2504 req = kzalloc(sizeof(*req), gfp_flags);
2509 req->req.dma = DMA_ADDR_INVALID;
2511 INIT_LIST_HEAD(&req->queue);
2516 /*-------------------------------------------------------------------------*/
2517 static void nbu2ss_ep_free_request(struct usb_ep *_ep,
2518 struct usb_request *_req)
2520 struct nbu2ss_req *req;
2523 req = container_of(_req, struct nbu2ss_req, req);
2529 /*-------------------------------------------------------------------------*/
2530 static int nbu2ss_ep_queue(struct usb_ep *_ep,
2531 struct usb_request *_req, gfp_t gfp_flags)
2533 struct nbu2ss_req *req;
2534 struct nbu2ss_ep *ep;
2535 struct nbu2ss_udc *udc;
2536 unsigned long flags;
2538 int result = -EINVAL;
2540 /* catch various bogus parameters */
2541 if (!_ep || !_req) {
2543 pr_err("udc: %s --- _ep == NULL\n", __func__);
2546 pr_err("udc: %s --- _req == NULL\n", __func__);
2551 req = container_of(_req, struct nbu2ss_req, req);
2552 if (unlikely(!_req->complete ||
2554 !list_empty(&req->queue))) {
2555 if (!_req->complete)
2556 pr_err("udc: %s --- !_req->complete\n", __func__);
2559 pr_err("udc:%s --- !_req->buf\n", __func__);
2561 if (!list_empty(&req->queue))
2562 pr_err("%s --- !list_empty(&req->queue)\n", __func__);
2567 ep = container_of(_ep, struct nbu2ss_ep, ep);
2570 if (udc->vbus_active == 0) {
2571 dev_info(udc->dev, "Can't ep_queue (VBUS OFF)\n");
2575 if (unlikely(!udc->driver)) {
2576 dev_err(udc->dev, "%s, bogus device state %p\n", __func__,
2581 spin_lock_irqsave(&udc->lock, flags);
2584 if ((uintptr_t)req->req.buf & 0x3)
2585 req->unaligned = true;
2587 req->unaligned = false;
2589 if (req->unaligned) {
2590 if (!ep->virt_buf) {
2591 ep->virt_buf = dma_alloc_coherent(udc->dev, PAGE_SIZE,
2593 GFP_ATOMIC | GFP_DMA);
2594 if (!ep->virt_buf) {
2595 spin_unlock_irqrestore(&udc->lock, flags);
2599 if (ep->epnum > 0) {
2600 if (ep->direct == USB_DIR_IN)
2601 memcpy(ep->virt_buf, req->req.buf,
2606 if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT) &&
2607 (req->req.dma != 0))
2608 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_OUT);
2611 _req->status = -EINPROGRESS;
2614 bflag = list_empty(&ep->queue);
2615 list_add_tail(&req->queue, &ep->queue);
2617 if (bflag && !ep->stalled) {
2618 result = _nbu2ss_start_transfer(udc, ep, req, false);
2620 dev_err(udc->dev, " *** %s, result = %d\n", __func__,
2622 list_del(&req->queue);
2623 } else if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT)) {
2625 if (req->req.length < 4 &&
2626 req->req.length == req->req.actual)
2628 if (req->req.length == req->req.actual)
2630 _nbu2ss_ep_done(ep, req, result);
2634 spin_unlock_irqrestore(&udc->lock, flags);
2639 /*-------------------------------------------------------------------------*/
2640 static int nbu2ss_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
2642 struct nbu2ss_req *req;
2643 struct nbu2ss_ep *ep;
2644 struct nbu2ss_udc *udc;
2645 unsigned long flags;
2647 /* catch various bogus parameters */
2648 if (!_ep || !_req) {
2649 /* pr_err("%s, bad param(1)\n", __func__); */
2653 ep = container_of(_ep, struct nbu2ss_ep, ep);
2659 spin_lock_irqsave(&udc->lock, flags);
2661 /* make sure it's actually queued on this endpoint */
2662 list_for_each_entry(req, &ep->queue, queue) {
2663 if (&req->req == _req) {
2664 _nbu2ss_ep_done(ep, req, -ECONNRESET);
2665 spin_unlock_irqrestore(&udc->lock, flags);
2670 spin_unlock_irqrestore(&udc->lock, flags);
2672 pr_debug("%s no queue(EINVAL)\n", __func__);
2677 /*-------------------------------------------------------------------------*/
2678 static int nbu2ss_ep_set_halt(struct usb_ep *_ep, int value)
2681 unsigned long flags;
2683 struct nbu2ss_ep *ep;
2684 struct nbu2ss_udc *udc;
2687 pr_err("%s, bad param\n", __func__);
2691 ep = container_of(_ep, struct nbu2ss_ep, ep);
2695 dev_err(ep->udc->dev, " *** %s, bad udc\n", __func__);
2699 spin_lock_irqsave(&udc->lock, flags);
2701 ep_adrs = ep->epnum | ep->direct;
2703 _nbu2ss_set_endpoint_stall(udc, ep_adrs, value);
2704 ep->stalled = false;
2706 if (list_empty(&ep->queue))
2707 _nbu2ss_epn_set_stall(udc, ep);
2715 spin_unlock_irqrestore(&udc->lock, flags);
2720 static int nbu2ss_ep_set_wedge(struct usb_ep *_ep)
2722 return nbu2ss_ep_set_halt(_ep, 1);
2725 /*-------------------------------------------------------------------------*/
2726 static int nbu2ss_ep_fifo_status(struct usb_ep *_ep)
2729 struct nbu2ss_ep *ep;
2730 struct nbu2ss_udc *udc;
2731 unsigned long flags;
2732 struct fc_regs __iomem *preg;
2735 pr_err("%s, bad param\n", __func__);
2739 ep = container_of(_ep, struct nbu2ss_ep, ep);
2743 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2749 data = gpiod_get_value(vbus_gpio);
2753 spin_lock_irqsave(&udc->lock, flags);
2755 if (ep->epnum == 0) {
2756 data = _nbu2ss_readl(&preg->EP0_LENGTH) & EP0_LDATA;
2759 data = _nbu2ss_readl(&preg->EP_REGS[ep->epnum - 1].EP_LEN_DCNT)
2763 spin_unlock_irqrestore(&udc->lock, flags);
2768 /*-------------------------------------------------------------------------*/
2769 static void nbu2ss_ep_fifo_flush(struct usb_ep *_ep)
2772 struct nbu2ss_ep *ep;
2773 struct nbu2ss_udc *udc;
2774 unsigned long flags;
2777 pr_err("udc: %s, bad param\n", __func__);
2781 ep = container_of(_ep, struct nbu2ss_ep, ep);
2785 dev_err(ep->udc->dev, "%s, bad udc\n", __func__);
2789 data = gpiod_get_value(vbus_gpio);
2793 spin_lock_irqsave(&udc->lock, flags);
2794 _nbu2ss_fifo_flush(udc, ep);
2795 spin_unlock_irqrestore(&udc->lock, flags);
2798 /*-------------------------------------------------------------------------*/
2799 static const struct usb_ep_ops nbu2ss_ep_ops = {
2800 .enable = nbu2ss_ep_enable,
2801 .disable = nbu2ss_ep_disable,
2803 .alloc_request = nbu2ss_ep_alloc_request,
2804 .free_request = nbu2ss_ep_free_request,
2806 .queue = nbu2ss_ep_queue,
2807 .dequeue = nbu2ss_ep_dequeue,
2809 .set_halt = nbu2ss_ep_set_halt,
2810 .set_wedge = nbu2ss_ep_set_wedge,
2812 .fifo_status = nbu2ss_ep_fifo_status,
2813 .fifo_flush = nbu2ss_ep_fifo_flush,
2816 /*-------------------------------------------------------------------------*/
2817 /* usb_gadget_ops */
2819 /*-------------------------------------------------------------------------*/
2820 static int nbu2ss_gad_get_frame(struct usb_gadget *pgadget)
2823 struct nbu2ss_udc *udc;
2826 pr_err("udc: %s, bad param\n", __func__);
2830 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2831 data = gpiod_get_value(vbus_gpio);
2835 return _nbu2ss_readl(&udc->p_regs->USB_ADDRESS) & FRAME;
2838 /*-------------------------------------------------------------------------*/
2839 static int nbu2ss_gad_wakeup(struct usb_gadget *pgadget)
2844 struct nbu2ss_udc *udc;
2847 pr_err("%s, bad param\n", __func__);
2851 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2853 data = gpiod_get_value(vbus_gpio);
2855 dev_warn(&pgadget->dev, "VBUS LEVEL = %d\n", data);
2859 _nbu2ss_bitset(&udc->p_regs->EPCTR, PLL_RESUME);
2861 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2862 data = _nbu2ss_readl(&udc->p_regs->EPCTR);
2864 if (data & PLL_LOCK)
2868 _nbu2ss_bitclr(&udc->p_regs->EPCTR, PLL_RESUME);
2873 /*-------------------------------------------------------------------------*/
2874 static int nbu2ss_gad_set_selfpowered(struct usb_gadget *pgadget,
2877 struct nbu2ss_udc *udc;
2878 unsigned long flags;
2881 pr_err("%s, bad param\n", __func__);
2885 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2887 spin_lock_irqsave(&udc->lock, flags);
2888 pgadget->is_selfpowered = (is_selfpowered != 0);
2889 spin_unlock_irqrestore(&udc->lock, flags);
2894 /*-------------------------------------------------------------------------*/
2895 static int nbu2ss_gad_vbus_session(struct usb_gadget *pgadget, int is_active)
2900 /*-------------------------------------------------------------------------*/
2901 static int nbu2ss_gad_vbus_draw(struct usb_gadget *pgadget, unsigned int mA)
2903 struct nbu2ss_udc *udc;
2904 unsigned long flags;
2907 pr_err("%s, bad param\n", __func__);
2911 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2913 spin_lock_irqsave(&udc->lock, flags);
2915 spin_unlock_irqrestore(&udc->lock, flags);
2920 /*-------------------------------------------------------------------------*/
2921 static int nbu2ss_gad_pullup(struct usb_gadget *pgadget, int is_on)
2923 struct nbu2ss_udc *udc;
2924 unsigned long flags;
2927 pr_err("%s, bad param\n", __func__);
2931 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
2934 pr_warn("%s, Not Regist Driver\n", __func__);
2938 if (udc->vbus_active == 0)
2941 spin_lock_irqsave(&udc->lock, flags);
2942 _nbu2ss_pullup(udc, is_on);
2943 spin_unlock_irqrestore(&udc->lock, flags);
2948 /*-------------------------------------------------------------------------*/
2949 static int nbu2ss_gad_ioctl(struct usb_gadget *pgadget,
2950 unsigned int code, unsigned long param)
2955 static const struct usb_gadget_ops nbu2ss_gadget_ops = {
2956 .get_frame = nbu2ss_gad_get_frame,
2957 .wakeup = nbu2ss_gad_wakeup,
2958 .set_selfpowered = nbu2ss_gad_set_selfpowered,
2959 .vbus_session = nbu2ss_gad_vbus_session,
2960 .vbus_draw = nbu2ss_gad_vbus_draw,
2961 .pullup = nbu2ss_gad_pullup,
2962 .ioctl = nbu2ss_gad_ioctl,
2965 static const struct {
2967 const struct usb_ep_caps caps;
2968 } ep_info[NUM_ENDPOINTS] = {
2969 #define EP_INFO(_name, _caps) \
2976 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
2978 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2980 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2981 EP_INFO("ep3in-int",
2982 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
2984 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2986 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2988 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2990 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
2991 EP_INFO("ep8in-int",
2992 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
2994 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2996 USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO, USB_EP_CAPS_DIR_ALL)),
2998 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3000 USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_ALL)),
3001 EP_INFO("epdin-int",
3002 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_IN)),
3007 /*-------------------------------------------------------------------------*/
3008 static void nbu2ss_drv_ep_init(struct nbu2ss_udc *udc)
3012 INIT_LIST_HEAD(&udc->gadget.ep_list);
3013 udc->gadget.ep0 = &udc->ep[0].ep;
3015 for (i = 0; i < NUM_ENDPOINTS; i++) {
3016 struct nbu2ss_ep *ep = &udc->ep[i];
3021 ep->ep.driver_data = NULL;
3022 ep->ep.name = ep_info[i].name;
3023 ep->ep.caps = ep_info[i].caps;
3024 ep->ep.ops = &nbu2ss_ep_ops;
3026 usb_ep_set_maxpacket_limit(&ep->ep,
3027 i == 0 ? EP0_PACKETSIZE
3030 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
3031 INIT_LIST_HEAD(&ep->queue);
3034 list_del_init(&udc->ep[0].ep.ep_list);
3037 /*-------------------------------------------------------------------------*/
3038 /* platform_driver */
3039 static int nbu2ss_drv_contest_init(struct platform_device *pdev,
3040 struct nbu2ss_udc *udc)
3042 spin_lock_init(&udc->lock);
3043 udc->dev = &pdev->dev;
3045 udc->gadget.is_selfpowered = 1;
3046 udc->devstate = USB_STATE_NOTATTACHED;
3050 udc->pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3053 nbu2ss_drv_ep_init(udc);
3056 udc->gadget.ops = &nbu2ss_gadget_ops;
3057 udc->gadget.ep0 = &udc->ep[0].ep;
3058 udc->gadget.speed = USB_SPEED_UNKNOWN;
3059 udc->gadget.name = driver_name;
3060 /* udc->gadget.is_dualspeed = 1; */
3062 device_initialize(&udc->gadget.dev);
3064 dev_set_name(&udc->gadget.dev, "gadget");
3065 udc->gadget.dev.parent = &pdev->dev;
3066 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
3072 * probe - binds to the platform device
3074 static int nbu2ss_drv_probe(struct platform_device *pdev)
3077 struct nbu2ss_udc *udc;
3079 void __iomem *mmio_base;
3081 udc = &udc_controller;
3082 memset(udc, 0, sizeof(struct nbu2ss_udc));
3084 platform_set_drvdata(pdev, udc);
3086 /* require I/O memory and IRQ to be provided as resources */
3087 mmio_base = devm_platform_ioremap_resource(pdev, 0);
3088 if (IS_ERR(mmio_base))
3089 return PTR_ERR(mmio_base);
3091 irq = platform_get_irq(pdev, 0);
3094 status = devm_request_irq(&pdev->dev, irq, _nbu2ss_udc_irq,
3095 0, driver_name, udc);
3098 udc->p_regs = (struct fc_regs __iomem *)mmio_base;
3100 /* USB Function Controller Interrupt */
3102 dev_err(udc->dev, "request_irq(USB_UDC_IRQ_1) failed\n");
3106 /* Driver Initialization */
3107 status = nbu2ss_drv_contest_init(pdev, udc);
3113 /* VBUS Interrupt */
3114 vbus_irq = gpiod_to_irq(vbus_gpio);
3115 irq_set_irq_type(vbus_irq, IRQ_TYPE_EDGE_BOTH);
3116 status = request_irq(vbus_irq,
3117 _nbu2ss_vbus_irq, IRQF_SHARED, driver_name, udc);
3120 dev_err(udc->dev, "request_irq(vbus_irq) failed\n");
3127 /*-------------------------------------------------------------------------*/
3128 static void nbu2ss_drv_shutdown(struct platform_device *pdev)
3130 struct nbu2ss_udc *udc;
3132 udc = platform_get_drvdata(pdev);
3136 _nbu2ss_disable_controller(udc);
3139 /*-------------------------------------------------------------------------*/
3140 static void nbu2ss_drv_remove(struct platform_device *pdev)
3142 struct nbu2ss_udc *udc;
3143 struct nbu2ss_ep *ep;
3146 udc = &udc_controller;
3148 for (i = 0; i < NUM_ENDPOINTS; i++) {
3151 dma_free_coherent(udc->dev, PAGE_SIZE, (void *)ep->virt_buf,
3155 /* Interrupt Handler - Release */
3156 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_new = 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");