blob: b2eaf0108e465feeaf6b018caec2d6e97f0fdc0e [file] [log] [blame]
Magnus Damm33aa8d42014-06-06 19:44:17 +09001/*
2 * drivers/usb/gadget/emxx_udc.c
3 * EMXX FCD (Function Controller Driver) for USB.
4 *
5 * Copyright (C) 2010 Renesas Electronics Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
24#include <linux/delay.h>
25#include <linux/ioport.h>
26#include <linux/slab.h>
27#include <linux/errno.h>
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/interrupt.h>
31#include <linux/proc_fs.h>
32#include <linux/clk.h>
33#include <linux/ctype.h>
34#include <linux/string.h>
35#include <linux/dma-mapping.h>
36#include <linux/workqueue.h>
Sachin Kamata790ebc2014-06-23 11:43:08 +053037#include <linux/device.h>
Magnus Damm33aa8d42014-06-06 19:44:17 +090038
39#include <linux/usb/ch9.h>
40#include <linux/usb/gadget.h>
41
42#include <linux/irq.h>
43#include <linux/gpio.h>
44
45#include "emxx_udc.h"
46
47#define DRIVER_DESC "EMXX UDC driver"
48#define DMA_ADDR_INVALID (~(dma_addr_t)0)
49
50static const char driver_name[] = "emxx_udc";
51static const char driver_desc[] = DRIVER_DESC;
52
53/*===========================================================================*/
54/* Prototype */
55static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *, struct nbu2ss_ep *);
56static void _nbu2ss_ep0_enable(struct nbu2ss_udc *);
57/*static void _nbu2ss_ep0_disable(struct nbu2ss_udc *);*/
58static void _nbu2ss_ep_done(struct nbu2ss_ep *, struct nbu2ss_req *, int);
59static void _nbu2ss_set_test_mode(struct nbu2ss_udc *, u32 mode);
60static void _nbu2ss_endpoint_toggle_reset(struct nbu2ss_udc *udc, u8 ep_adrs);
61
62static int _nbu2ss_pullup(struct nbu2ss_udc *, int);
63static void _nbu2ss_fifo_flush(struct nbu2ss_udc *, struct nbu2ss_ep *);
64
65/*===========================================================================*/
66/* Macro */
67#define _nbu2ss_zero_len_pkt(udc, epnum) \
68 _nbu2ss_ep_in_end(udc, epnum, 0, 0)
69
70
71/*===========================================================================*/
72/* Global */
73struct nbu2ss_udc udc_controller;
74
75
76/*-------------------------------------------------------------------------*/
77/* Read */
78static inline u32 _nbu2ss_readl(void *address)
79{
80 return __raw_readl(address) ;
81}
82
83/*-------------------------------------------------------------------------*/
84/* Write */
85static inline void _nbu2ss_writel(void *address, u32 udata)
86{
87 __raw_writel(udata, address) ;
88}
89
90/*-------------------------------------------------------------------------*/
91/* Set Bit */
92static inline void _nbu2ss_bitset(void *address, u32 udata)
93{
94 u32 reg_dt = __raw_readl(address) | (udata);
95 __raw_writel(reg_dt, address) ;
96}
97
98/*-------------------------------------------------------------------------*/
99/* Clear Bit */
100static inline void _nbu2ss_bitclr(void *address, u32 udata)
101{
102 u32 reg_dt = __raw_readl(address) & ~(udata);
103 __raw_writel(reg_dt, address) ;
104}
105
106#ifdef UDC_DEBUG_DUMP
107/*-------------------------------------------------------------------------*/
108static void _nbu2ss_dump_register(struct nbu2ss_udc *udc)
109{
110 int i;
111 u32 reg_data;
112
113 pr_info("=== %s()\n", __func__);
114
115 if (udc == NULL) {
116 ERR("%s udc == NULL\n", __func__);
117 return;
118 }
119
120 spin_unlock(&udc->lock);
121
122 printk(KERN_DEBUG "\n-USB REG-\n");
123 for (i = 0x0 ; i < USB_BASE_SIZE ; i += 16) {
124 reg_data = _nbu2ss_readl(
125 (u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i));
126 printk(KERN_DEBUG "USB%04x =%08x", i, (int)reg_data);
127
128 reg_data = _nbu2ss_readl(
129 (u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i + 4));
130 printk(KERN_DEBUG " %08x", (int)reg_data);
131
132 reg_data = _nbu2ss_readl(
133 (u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i + 8));
134 printk(KERN_DEBUG " %08x", (int)reg_data);
135
136 reg_data = _nbu2ss_readl(
137 (u32 *)IO_ADDRESS(USB_BASE_ADDRESS + i + 12));
138 printk(KERN_DEBUG " %08x\n", (int)reg_data);
139
140 }
141
142 spin_lock(&udc->lock);
143}
144#endif /* UDC_DEBUG_DUMP */
145
146/*-------------------------------------------------------------------------*/
147/* Endpoint 0 Callback (Complete) */
148static void _nbu2ss_ep0_complete(struct usb_ep *_ep, struct usb_request *_req)
149{
150 u8 recipient;
151 u16 selector;
152 u32 test_mode;
153 struct usb_ctrlrequest *p_ctrl;
154 struct nbu2ss_udc *udc;
155
156 if ((_ep == NULL) || (_req == NULL))
157 return;
158
159 udc = (struct nbu2ss_udc *)_req->context;
160 p_ctrl = &udc->ctrl;
161 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
162
163 if (p_ctrl->bRequest == USB_REQ_SET_FEATURE) {
164 /*-------------------------------------------------*/
165 /* SET_FEATURE */
166 recipient = (u8)(p_ctrl->bRequestType & USB_RECIP_MASK);
167 selector = p_ctrl->wValue;
168 if ((recipient == USB_RECIP_DEVICE) &&
169 (selector == USB_DEVICE_TEST_MODE)) {
170 test_mode = (u32)(p_ctrl->wIndex >> 8);
171 _nbu2ss_set_test_mode(udc, test_mode);
172 }
173 }
174 }
175}
176
177/*-------------------------------------------------------------------------*/
178/* Initialization usb_request */
179static void _nbu2ss_create_ep0_packet(
180 struct nbu2ss_udc *udc,
181 void *p_buf,
182 unsigned length
183)
184{
185 udc->ep0_req.req.buf = p_buf;
186 udc->ep0_req.req.length = length;
187 udc->ep0_req.req.dma = 0;
188 udc->ep0_req.req.zero = TRUE;
189 udc->ep0_req.req.complete = _nbu2ss_ep0_complete;
190 udc->ep0_req.req.status = -EINPROGRESS;
191 udc->ep0_req.req.context = udc;
192 udc->ep0_req.req.actual = 0;
193}
194
195/*-------------------------------------------------------------------------*/
196/* Acquisition of the first address of RAM(FIFO) */
197static u32 _nbu2ss_get_begin_ram_address(struct nbu2ss_udc *udc)
198{
199 u32 num, buf_type;
200 u32 data, last_ram_adr, use_ram_size;
201
202 PT_EP_REGS p_ep_regs;
203
204 last_ram_adr = (D_RAM_SIZE_CTRL / sizeof(u32)) * 2;
205 use_ram_size = 0;
206
207 for (num = 0; num < NUM_ENDPOINTS - 1; num++) {
208 p_ep_regs = &udc->p_regs->EP_REGS[num];
209 data = _nbu2ss_readl(&p_ep_regs->EP_PCKT_ADRS);
210 buf_type = _nbu2ss_readl(&p_ep_regs->EP_CONTROL) & EPn_BUF_TYPE;
211 if (buf_type == 0) {
212 /* Single Buffer */
213 use_ram_size += (data & EPn_MPKT) / sizeof(u32);
214 } else {
215 /* Double Buffer */
216 use_ram_size += ((data & EPn_MPKT) / sizeof(u32)) * 2;
217 }
218
219 if ((data >> 16) > last_ram_adr)
220 last_ram_adr = data>>16;
221 }
222
223 return last_ram_adr + use_ram_size;
224}
225
226/*-------------------------------------------------------------------------*/
227/* Construction of Endpoint */
228static int _nbu2ss_ep_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
229{
230 u32 num;
231 u32 data;
232 u32 begin_adrs;
233
234 if (ep->epnum == 0)
235 return -EINVAL;
236
237 num = ep->epnum - 1;
238
239 /*-------------------------------------------------------------*/
240 /* RAM Transfer Address */
241 begin_adrs = _nbu2ss_get_begin_ram_address(udc);
242 data = (begin_adrs << 16) | ep->ep.maxpacket;
243 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, data);
244
245 /*-------------------------------------------------------------*/
246 /* Interrupt Enable */
247 data = 1 << (ep->epnum + 8);
248 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, data);
249
250 /*-------------------------------------------------------------*/
251 /* Endpoint Type(Mode) */
252 /* Bulk, Interrupt, ISO */
253 switch (ep->ep_type) {
254 case USB_ENDPOINT_XFER_BULK:
255 data = EPn_BULK;
256 break;
257
258 case USB_ENDPOINT_XFER_INT:
259 data = EPn_BUF_SINGLE | EPn_INTERRUPT;
260 break;
261
262 case USB_ENDPOINT_XFER_ISOC:
263 data = EPn_ISO;
264 break;
265
266 default:
267 data = 0;
268 break;
269 }
270
271 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
272 _nbu2ss_endpoint_toggle_reset(udc, (ep->epnum|ep->direct));
273
274 if (ep->direct == USB_DIR_OUT) {
275 /*---------------------------------------------------------*/
276 /* OUT */
277 data = EPn_EN | EPn_BCLR | EPn_DIR0;
278 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
279
280 data = (EPn_ONAK | EPn_OSTL_EN | EPn_OSTL);
281 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
282
283 data = (EPn_OUT_EN | EPn_OUT_END_EN);
284 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
285 } else {
286 /*---------------------------------------------------------*/
287 /* IN */
288 data = (EPn_EN | EPn_BCLR | EPn_AUTO);
289 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
290
291 data = (EPn_ISTL);
292 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
293
294 data = (EPn_IN_EN | EPn_IN_END_EN);
295 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
296 }
297
298 return 0;
299}
300
301/*-------------------------------------------------------------------------*/
302/* Release of Endpoint */
303static int _nbu2ss_epn_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
304{
305 u32 num;
306 u32 data;
307
308 if ((ep->epnum == 0) || (udc->vbus_active == 0))
309 return -EINVAL;
310
311 num = ep->epnum - 1;
312
313 /*-------------------------------------------------------------*/
314 /* RAM Transfer Address */
315 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_PCKT_ADRS, 0);
316
317 /*-------------------------------------------------------------*/
318 /* Interrupt Disable */
319 data = 1 << (ep->epnum + 8);
320 _nbu2ss_bitclr(&udc->p_regs->USB_INT_ENA, data);
321
322 if (ep->direct == USB_DIR_OUT) {
323 /*---------------------------------------------------------*/
324 /* OUT */
325 data = EPn_ONAK | EPn_BCLR;
326 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
327
328 data = EPn_EN | EPn_DIR0;
329 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
330
331 data = EPn_OUT_EN | EPn_OUT_END_EN;
332 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
333 } else {
334 /*---------------------------------------------------------*/
335 /* IN */
336 data = EPn_BCLR;
337 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
338
339 data = EPn_EN | EPn_AUTO;
340 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
341
342 data = EPn_IN_EN | EPn_IN_END_EN;
343 _nbu2ss_bitclr(&udc->p_regs->EP_REGS[num].EP_INT_ENA, data);
344 }
345
346 return 0;
347}
348
349/*-------------------------------------------------------------------------*/
350/* DMA setting (without Endpoint 0) */
351static void _nbu2ss_ep_dma_init(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
352{
353 u32 num;
354 u32 data;
355
356 data = _nbu2ss_readl(&udc->p_regs->USBSSCONF);
357 if (((ep->epnum == 0) || (data & (1 << ep->epnum)) == 0))
358 return; /* Not Support DMA */
359
360 num = ep->epnum - 1;
361
362 if (ep->direct == USB_DIR_OUT) {
363 /*---------------------------------------------------------*/
364 /* OUT */
365 data = ep->ep.maxpacket;
366 _nbu2ss_writel(&udc->p_regs->EP_DCR[num].EP_DCR2, data);
367
368 /*---------------------------------------------------------*/
369 /* Transfer Direct */
370 data = DCR1_EPn_DIR0;
371 _nbu2ss_bitset(&udc->p_regs->EP_DCR[num].EP_DCR1, data);
372
373 /*---------------------------------------------------------*/
374 /* DMA Mode etc. */
375 data = EPn_STOP_MODE | EPn_STOP_SET | EPn_DMAMODE0;
376 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
377 } else {
378 /*---------------------------------------------------------*/
379 /* IN */
380 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, EPn_AUTO);
381
382 /*---------------------------------------------------------*/
383 /* DMA Mode etc. */
384 data = EPn_BURST_SET | EPn_DMAMODE0;
385 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_DMA_CTRL, data);
386 }
387}
388
389/*-------------------------------------------------------------------------*/
390/* DMA setting release */
391static void _nbu2ss_ep_dma_exit(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
392{
393 u32 num;
394 u32 data;
395 PT_FC_REGS preg = udc->p_regs;
396
397 if (udc->vbus_active == 0)
398 return; /* VBUS OFF */
399
400 data = _nbu2ss_readl(&preg->USBSSCONF);
401 if ((ep->epnum == 0) || ((data & (1 << ep->epnum)) == 0))
402 return; /* Not Support DMA */
403
404 num = ep->epnum - 1;
405
406 _nbu2ss_ep_dma_abort(udc, ep);
407
408 if (ep->direct == USB_DIR_OUT) {
409 /*---------------------------------------------------------*/
410 /* OUT */
411 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, 0);
412 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPn_DIR0);
413 _nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
414 } else {
415 /*---------------------------------------------------------*/
416 /* IN */
417 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPn_AUTO);
418 _nbu2ss_writel(&preg->EP_REGS[num].EP_DMA_CTRL, 0);
419 }
420}
421
422/*-------------------------------------------------------------------------*/
423/* Abort DMA */
424static void _nbu2ss_ep_dma_abort(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
425{
426 PT_FC_REGS preg = udc->p_regs;
427
428 _nbu2ss_bitclr(&preg->EP_DCR[ep->epnum-1].EP_DCR1, DCR1_EPn_REQEN);
429 mdelay(DMA_DISABLE_TIME); /* DCR1_EPn_REQEN Clear */
430 _nbu2ss_bitclr(&preg->EP_REGS[ep->epnum-1].EP_DMA_CTRL, EPn_DMA_EN);
431}
432
433/*-------------------------------------------------------------------------*/
434/* Start IN Transfer */
435static void _nbu2ss_ep_in_end(
436 struct nbu2ss_udc *udc,
437 u32 epnum,
438 u32 data32,
439 u32 length
440)
441{
442 u32 data;
443 u32 num;
444 PT_FC_REGS preg = udc->p_regs;
445
446 if (length >= sizeof(u32))
447 return;
448
449 if (epnum == 0) {
450 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_AUTO);
451
452 /* Writing of 1-4 bytes */
453 if (length)
454 _nbu2ss_writel(&preg->EP0_WRITE, data32);
455
456 data = ((length << 5) & EP0_DW) | EP0_DEND;
457 _nbu2ss_writel(&preg->EP0_CONTROL, data);
458
459 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_AUTO);
460 } else {
461 num = epnum - 1;
462
463 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL, EPn_AUTO);
464
465 /* Writing of 1-4 bytes */
466 if (length)
467 _nbu2ss_writel(&preg->EP_REGS[num].EP_WRITE, data32);
468
469 data = (((((u32)length) << 5) & EPn_DW) | EPn_DEND);
470 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
471
472 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, EPn_AUTO);
473 }
474
475 return;
476}
477
478#ifdef USE_DMA
479/*-------------------------------------------------------------------------*/
480static void _nbu2ss_dma_map_single(
481 struct nbu2ss_udc *udc,
482 struct nbu2ss_ep *ep,
483 struct nbu2ss_req *req,
484 u8 direct
485)
486{
487 if (req->req.dma == DMA_ADDR_INVALID) {
488 if (req->unaligned)
489 req->req.dma = ep->phys_buf;
490 else {
491 req->req.dma = dma_map_single(
492 udc->gadget.dev.parent,
493 req->req.buf,
494 req->req.length,
495 (direct == USB_DIR_IN)
496 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
497 }
498 req->mapped = 1;
499 } else {
500 if (!req->unaligned)
501 dma_sync_single_for_device(
502 udc->gadget.dev.parent,
503 req->req.dma,
504 req->req.length,
505 (direct == USB_DIR_IN)
506 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
507
508 req->mapped = 0;
509 }
510}
511
512/*-------------------------------------------------------------------------*/
513static void _nbu2ss_dma_unmap_single(
514 struct nbu2ss_udc *udc,
515 struct nbu2ss_ep *ep,
516 struct nbu2ss_req *req,
517 u8 direct
518)
519{
520 u8 data[4];
521 u8 *p;
522 u32 count = 0;
523
524 if (direct == USB_DIR_OUT) {
525 count = req->req.actual % 4;
526 if (count) {
527 p = req->req.buf;
528 p += (req->req.actual - count);
529 memcpy(data, p, count);
530 }
531 }
532
533 if (req->mapped) {
534 if (req->unaligned) {
535 if (direct == USB_DIR_OUT)
536 memcpy(req->req.buf, ep->virt_buf,
537 req->req.actual & 0xfffffffc);
538 } else
539 dma_unmap_single(udc->gadget.dev.parent,
540 req->req.dma, req->req.length,
541 (direct == USB_DIR_IN)
542 ? DMA_TO_DEVICE
543 : DMA_FROM_DEVICE);
544 req->req.dma = DMA_ADDR_INVALID;
545 req->mapped = 0;
546 } else {
547 if (!req->unaligned)
548 dma_sync_single_for_cpu(udc->gadget.dev.parent,
549 req->req.dma, req->req.length,
550 (direct == USB_DIR_IN)
551 ? DMA_TO_DEVICE
552 : DMA_FROM_DEVICE);
553 }
554
555 if (count) {
556 p = req->req.buf;
557 p += (req->req.actual - count);
558 memcpy(p, data, count);
559 }
560}
561#endif
562
563/*-------------------------------------------------------------------------*/
564/* Endpoint 0 OUT Transfer (PIO) */
KANG Yuxuana2c14e92014-07-16 10:45:01 +0800565static int EP0_out_PIO(struct nbu2ss_udc *udc, u8 *pBuf, u32 length)
Magnus Damm33aa8d42014-06-06 19:44:17 +0900566{
567 u32 i;
568 int nret = 0;
569 u32 iWordLength = 0;
KANG Yuxuana2c14e92014-07-16 10:45:01 +0800570 USB_REG_ACCESS *pBuf32 = (USB_REG_ACCESS *)pBuf;
Magnus Damm33aa8d42014-06-06 19:44:17 +0900571
572 /*------------------------------------------------------------*/
573 /* Read Length */
574 iWordLength = length / sizeof(u32);
575
576 /*------------------------------------------------------------*/
577 /* PIO Read */
578 if (iWordLength) {
579 for (i = 0; i < iWordLength; i++) {
580 pBuf32->dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
581 pBuf32++;
582 }
583 nret = iWordLength * sizeof(u32);
584 }
585
586 return nret;
587}
588
589/*-------------------------------------------------------------------------*/
590/* Endpoint 0 OUT Transfer (PIO, OverBytes) */
KANG Yuxuana2c14e92014-07-16 10:45:01 +0800591static int EP0_out_OverBytes(struct nbu2ss_udc *udc, u8 *pBuf, u32 length)
Magnus Damm33aa8d42014-06-06 19:44:17 +0900592{
593 u32 i;
594 u32 iReadSize = 0;
595 USB_REG_ACCESS Temp32;
KANG Yuxuana2c14e92014-07-16 10:45:01 +0800596 USB_REG_ACCESS *pBuf32 = (USB_REG_ACCESS *)pBuf;
Magnus Damm33aa8d42014-06-06 19:44:17 +0900597
598 if ((0 < length) && (length < sizeof(u32))) {
599 Temp32.dw = _nbu2ss_readl(&udc->p_regs->EP0_READ);
600 for (i = 0 ; i < length ; i++)
601 pBuf32->byte.DATA[i] = Temp32.byte.DATA[i];
602 iReadSize += length;
603 }
604
605 return iReadSize;
606}
607
608/*-------------------------------------------------------------------------*/
609/* Endpoint 0 IN Transfer (PIO) */
610static int EP0_in_PIO(struct nbu2ss_udc *udc, u8 *pBuf, u32 length)
611{
612 u32 i;
613 u32 iMaxLength = EP0_PACKETSIZE;
614 u32 iWordLength = 0;
615 u32 iWriteLength = 0;
KANG Yuxuana2c14e92014-07-16 10:45:01 +0800616 USB_REG_ACCESS *pBuf32 = (USB_REG_ACCESS *)pBuf;
Magnus Damm33aa8d42014-06-06 19:44:17 +0900617
618 /*------------------------------------------------------------*/
619 /* Transfer Length */
620 if (iMaxLength < length)
621 iWordLength = iMaxLength / sizeof(u32);
622 else
623 iWordLength = length / sizeof(u32);
624
625 /*------------------------------------------------------------*/
626 /* PIO */
627 for (i = 0; i < iWordLength; i++) {
628 _nbu2ss_writel(&udc->p_regs->EP0_WRITE, pBuf32->dw);
629 pBuf32++;
630 iWriteLength += sizeof(u32);
631 }
632
633 return iWriteLength;
634}
635
636/*-------------------------------------------------------------------------*/
637/* Endpoint 0 IN Transfer (PIO, OverBytes) */
638static int EP0_in_OverBytes(struct nbu2ss_udc *udc, u8 *pBuf, u32 iRemainSize)
639{
640 u32 i;
641 USB_REG_ACCESS Temp32;
KANG Yuxuana2c14e92014-07-16 10:45:01 +0800642 USB_REG_ACCESS *pBuf32 = (USB_REG_ACCESS *)pBuf;
Magnus Damm33aa8d42014-06-06 19:44:17 +0900643
644 if ((0 < iRemainSize) && (iRemainSize < sizeof(u32))) {
645 for (i = 0 ; i < iRemainSize ; i++)
646 Temp32.byte.DATA[i] = pBuf32->byte.DATA[i];
647 _nbu2ss_ep_in_end(udc, 0, Temp32.dw, iRemainSize);
648
649 return iRemainSize;
650 }
651
652 return 0;
653}
654
655/*-------------------------------------------------------------------------*/
656/* Transfer NULL Packet (Epndoint 0) */
657static int EP0_send_NULL(struct nbu2ss_udc *udc, bool pid_flag)
658{
659 u32 data;
660
661 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
662 data &= ~(u32)EP0_INAK;
663
664 if (pid_flag)
665 data |= (EP0_INAK_EN | EP0_PIDCLR | EP0_DEND);
666 else
667 data |= (EP0_INAK_EN | EP0_DEND);
668
669 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
670
671 return 0;
672}
673
674/*-------------------------------------------------------------------------*/
675/* Receive NULL Packet (Endpoint 0) */
676static int EP0_receive_NULL(struct nbu2ss_udc *udc, bool pid_flag)
677{
678 u32 data;
679
680 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
681 data &= ~(u32)EP0_ONAK;
682
683 if (pid_flag)
684 data |= EP0_PIDCLR;
685
686 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
687
688 return 0;
689}
690
691/*-------------------------------------------------------------------------*/
692static int _nbu2ss_ep0_in_transfer(
693 struct nbu2ss_udc *udc,
694 struct nbu2ss_ep *ep,
695 struct nbu2ss_req *req
696)
697{
698 u8 *pBuffer; /* IN Data Buffer */
699 u32 data;
700 u32 iRemainSize = 0;
701 int result = 0;
702
703 /*-------------------------------------------------------------*/
704 /* End confirmation */
705 if (req->req.actual == req->req.length) {
706 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
707 if (req->zero) {
708 req->zero = 0;
709 EP0_send_NULL(udc, FALSE);
710 return 1;
711 }
712 }
713
714 return 0; /* Transfer End */
715 }
716
717 /*-------------------------------------------------------------*/
718 /* NAK release */
719 data = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
720 data |= EP0_INAK_EN;
721 data &= ~(u32)EP0_INAK;
722 _nbu2ss_writel(&udc->p_regs->EP0_CONTROL, data);
723
724 iRemainSize = req->req.length - req->req.actual;
725 pBuffer = (u8 *)req->req.buf;
726 pBuffer += req->req.actual;
727
728 /*-------------------------------------------------------------*/
729 /* Data transfer */
730 result = EP0_in_PIO(udc, pBuffer, iRemainSize);
731
732 req->div_len = result;
733 iRemainSize -= result;
734
735 if (iRemainSize == 0) {
736 EP0_send_NULL(udc, FALSE);
737 return result;
738 }
739
740 if ((iRemainSize < sizeof(u32)) && (result != EP0_PACKETSIZE)) {
741 pBuffer += result;
742 result += EP0_in_OverBytes(udc, pBuffer, iRemainSize);
743 req->div_len = result;
744 }
745
746 return result;
747}
748
749/*-------------------------------------------------------------------------*/
750static int _nbu2ss_ep0_out_transfer(
751 struct nbu2ss_udc *udc,
752 struct nbu2ss_ep *ep,
753 struct nbu2ss_req *req
754)
755{
756 u8 *pBuffer;
757 u32 iRemainSize;
758 u32 iRecvLength;
759 int result = 0;
760 int fRcvZero;
761
762 /*-------------------------------------------------------------*/
763 /* Receive data confirmation */
764 iRecvLength = _nbu2ss_readl(&udc->p_regs->EP0_LENGTH) & EP0_LDATA;
765 if (iRecvLength != 0) {
766
767 fRcvZero = 0;
768
769 iRemainSize = req->req.length - req->req.actual;
770 pBuffer = (u8 *)req->req.buf;
771 pBuffer += req->req.actual;
772
773 result = EP0_out_PIO(udc, pBuffer
774 , min(iRemainSize, iRecvLength));
775 if (result < 0)
776 return result;
777
778 req->req.actual += result;
779 iRecvLength -= result;
780
781 if ((0 < iRecvLength) && (iRecvLength < sizeof(u32))) {
782 pBuffer += result;
783 iRemainSize -= result;
784
785 result = EP0_out_OverBytes(udc, pBuffer
786 , min(iRemainSize, iRecvLength));
787 req->req.actual += result;
788 }
789 } else {
790 fRcvZero = 1;
791 }
792
793 /*-------------------------------------------------------------*/
794 /* End confirmation */
795 if (req->req.actual == req->req.length) {
796 if ((req->req.actual % EP0_PACKETSIZE) == 0) {
797 if (req->zero) {
798 req->zero = 0;
799 EP0_receive_NULL(udc, FALSE);
800 return 1;
801 }
802 }
803
804 return 0; /* Transfer End */
805 }
806
807 if ((req->req.actual % EP0_PACKETSIZE) != 0)
808 return 0; /* Short Packet Transfer End */
809
810 if (req->req.actual > req->req.length) {
811 ERR(" *** Overrun Error\n");
812 return -EOVERFLOW;
813 }
814
815 if (fRcvZero != 0) {
816 iRemainSize = _nbu2ss_readl(&udc->p_regs->EP0_CONTROL);
817 if (iRemainSize & EP0_ONAK) {
818 /*---------------------------------------------------*/
819 /* NACK release */
820 _nbu2ss_bitclr(&udc->p_regs->EP0_CONTROL, EP0_ONAK);
821 }
822 result = 1;
823 }
824
825 return result;
826}
827
828/*-------------------------------------------------------------------------*/
829static int _nbu2ss_out_dma(
830 struct nbu2ss_udc *udc,
831 struct nbu2ss_req *req,
832 u32 num,
833 u32 length
834)
835{
836 u8 *pBuffer;
837 u32 mpkt;
838 u32 lmpkt;
839 u32 dmacnt;
840 u32 burst = 1;
841 u32 data;
842 int result = -EINVAL;
843 PT_FC_REGS preg = udc->p_regs;
844
845 if (req->dma_flag)
846 return 1; /* DMA is forwarded */
847
848 req->dma_flag = TRUE;
849 pBuffer = (u8 *)req->req.dma;
850 pBuffer += req->req.actual;
851
852 /* DMA Address */
853 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)pBuffer);
854
855 /* Number of transfer packets */
856 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPn_MPKT;
857 dmacnt = (length / mpkt);
858 lmpkt = (length % mpkt) & ~(u32)0x03;
859
860 if (DMA_MAX_COUNT < dmacnt) {
861 dmacnt = DMA_MAX_COUNT;
862 lmpkt = 0;
863 } else if (0 != lmpkt) {
864 if (0 == dmacnt)
865 burst = 0; /* Burst OFF */
866 dmacnt++;
867 }
868
869 data = mpkt | (lmpkt << 16);
870 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
871
872 data = ((dmacnt & 0xff) << 16) | DCR1_EPn_DIR0 | DCR1_EPn_REQEN;
873 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
874
875 if (0 == burst) {
876 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, 0);
877 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_BURST_SET);
878 } else {
879 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT
880 , (dmacnt << 16));
881 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_BURST_SET);
882 }
883 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_DMA_EN);
884
885 result = length & ~(u32)0x03;
886 req->div_len = result;
887
888 return result;
889}
890
891/*-------------------------------------------------------------------------*/
892static int _nbu2ss_epn_out_pio(
893 struct nbu2ss_udc *udc,
894 struct nbu2ss_ep *ep,
895 struct nbu2ss_req *req,
896 u32 length
897)
898{
899 u8 *pBuffer;
900 u32 i;
901 u32 data;
902 u32 iWordLength;
903 USB_REG_ACCESS Temp32;
904 USB_REG_ACCESS *pBuf32;
905 int result = 0;
906 PT_FC_REGS preg = udc->p_regs;
907
908 if (req->dma_flag)
909 return 1; /* DMA is forwarded */
910
911 if (length == 0)
912 return 0;
913
914 pBuffer = (u8 *)req->req.buf;
915 pBuf32 = (USB_REG_ACCESS *)(pBuffer + req->req.actual);
916
917 iWordLength = length / sizeof(u32);
918 if (iWordLength > 0) {
919 /*---------------------------------------------------------*/
920 /* Copy of every four bytes */
921 for (i = 0; i < iWordLength; i++) {
922 pBuf32->dw =
923 _nbu2ss_readl(&preg->EP_REGS[ep->epnum-1].EP_READ);
924 pBuf32++;
925 }
926 result = iWordLength * sizeof(u32);
927 }
928
929 data = length - result;
930 if (data > 0) {
931 /*---------------------------------------------------------*/
932 /* Copy of fraction byte */
933 Temp32.dw = _nbu2ss_readl(&preg->EP_REGS[ep->epnum-1].EP_READ);
934 for (i = 0 ; i < data ; i++)
935 pBuf32->byte.DATA[i] = Temp32.byte.DATA[i];
936 result += data;
937 }
938
939 req->req.actual += result;
940
941 if ((req->req.actual == req->req.length)
942 || ((req->req.actual % ep->ep.maxpacket) != 0)) {
943
944 result = 0;
945 }
946
947 return result;
948}
949
950/*-------------------------------------------------------------------------*/
951static int _nbu2ss_epn_out_data(
952 struct nbu2ss_udc *udc,
953 struct nbu2ss_ep *ep,
954 struct nbu2ss_req *req,
955 u32 data_size
956)
957{
958 u32 num;
959 u32 iBufSize;
960 int nret = 1;
961
962 if (ep->epnum == 0)
963 return -EINVAL;
964
965 num = ep->epnum - 1;
966
967 iBufSize = min((req->req.length - req->req.actual), data_size);
968
969 if ((ep->ep_type != USB_ENDPOINT_XFER_INT)
970 && (req->req.dma != 0)
971 && (iBufSize >= sizeof(u32))) {
972 nret = _nbu2ss_out_dma(udc, req, num, iBufSize);
973 } else {
974 iBufSize = min(iBufSize, (u32)ep->ep.maxpacket);
975 nret = _nbu2ss_epn_out_pio(udc, ep, req, iBufSize);
976 }
977
978 return nret;
979}
980
981/*-------------------------------------------------------------------------*/
982static int _nbu2ss_epn_out_transfer(
983 struct nbu2ss_udc *udc,
984 struct nbu2ss_ep *ep,
985 struct nbu2ss_req *req
986)
987{
988 u32 num;
989 u32 iRecvLength;
990 int result = 1;
991 PT_FC_REGS preg = udc->p_regs;
992
993 if (ep->epnum == 0)
994 return -EINVAL;
995
996 num = ep->epnum - 1;
997
998 /*-------------------------------------------------------------*/
999 /* Receive Length */
1000 iRecvLength
1001 = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT) & EPn_LDATA;
1002
1003 if (iRecvLength != 0) {
1004 result = _nbu2ss_epn_out_data(udc, ep, req, iRecvLength);
1005 if (iRecvLength < ep->ep.maxpacket) {
1006 if (iRecvLength == result) {
1007 req->req.actual += result;
1008 result = 0;
1009 }
1010 }
1011 } else {
1012 if ((req->req.actual == req->req.length)
1013 || ((req->req.actual % ep->ep.maxpacket) != 0)) {
1014
1015 result = 0;
1016 }
1017 }
1018
1019 if (result == 0) {
1020 if ((req->req.actual % ep->ep.maxpacket) == 0) {
1021 if (req->zero) {
1022 req->zero = 0;
1023 return 1;
1024 }
1025 }
1026 }
1027
1028 if (req->req.actual > req->req.length) {
1029 ERR(" *** Overrun Error\n");
1030 ERR(" *** actual = %d, length = %d\n",
1031 req->req.actual, req->req.length);
1032 result = -EOVERFLOW;
1033 }
1034
1035 return result;
1036}
1037
1038/*-------------------------------------------------------------------------*/
1039static int _nbu2ss_in_dma(
1040 struct nbu2ss_udc *udc,
1041 struct nbu2ss_ep *ep,
1042 struct nbu2ss_req *req,
1043 u32 num,
1044 u32 length
1045)
1046{
1047 u8 *pBuffer;
1048 u32 mpkt; /* MaxPacketSize */
1049 u32 lmpkt; /* Last Packet Data Size */
1050 u32 dmacnt; /* IN Data Size */
1051 u32 iWriteLength;
1052 u32 data;
1053 int result = -EINVAL;
1054 PT_FC_REGS preg = udc->p_regs;
1055
1056 if (req->dma_flag)
1057 return 1; /* DMA is forwarded */
1058
1059#ifdef USE_DMA
1060 if (req->req.actual == 0)
1061 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_IN);
1062#endif
1063 req->dma_flag = TRUE;
1064
1065 /* MAX Packet Size */
1066 mpkt = _nbu2ss_readl(&preg->EP_REGS[num].EP_PCKT_ADRS) & EPn_MPKT;
1067
1068 if ((DMA_MAX_COUNT * mpkt) < length)
1069 iWriteLength = DMA_MAX_COUNT * mpkt;
1070 else
1071 iWriteLength = length;
1072
1073 /*------------------------------------------------------------*/
1074 /* Number of transmission packets */
1075 if (mpkt < iWriteLength) {
1076 dmacnt = iWriteLength / mpkt;
1077 lmpkt = (iWriteLength % mpkt) & ~(u32)0x3;
1078 if (lmpkt != 0)
1079 dmacnt++;
1080 else
1081 lmpkt = mpkt & ~(u32)0x3;
1082
1083 } else {
1084 dmacnt = 1;
1085 lmpkt = iWriteLength & ~(u32)0x3;
1086 }
1087
1088 /* Packet setting */
1089 data = mpkt | (lmpkt << 16);
1090 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR2, data);
1091
1092 /* Address setting */
1093 pBuffer = (u8 *)req->req.dma;
1094 pBuffer += req->req.actual;
1095 _nbu2ss_writel(&preg->EP_DCR[num].EP_TADR, (u32)pBuffer);
1096
1097 /* Packet and DMA setting */
1098 data = ((dmacnt & 0xff) << 16) | DCR1_EPn_REQEN;
1099 _nbu2ss_writel(&preg->EP_DCR[num].EP_DCR1, data);
1100
1101 /* Packet setting of EPC */
1102 data = dmacnt << 16;
1103 _nbu2ss_writel(&preg->EP_REGS[num].EP_LEN_DCNT, data);
1104
1105 /*DMA setting of EPC */
1106 _nbu2ss_bitset(&preg->EP_REGS[num].EP_DMA_CTRL, EPn_DMA_EN);
1107
1108 result = iWriteLength & ~(u32)0x3;
1109 req->div_len = result;
1110
1111 return result;
1112}
1113
1114/*-------------------------------------------------------------------------*/
1115static int _nbu2ss_epn_in_pio(
1116 struct nbu2ss_udc *udc,
1117 struct nbu2ss_ep *ep,
1118 struct nbu2ss_req *req,
1119 u32 length
1120)
1121{
1122 u8 *pBuffer;
1123 u32 i;
1124 u32 data;
1125 u32 iWordLength;
1126 USB_REG_ACCESS Temp32;
1127 USB_REG_ACCESS *pBuf32 = NULL;
1128 int result = 0;
1129 PT_FC_REGS preg = udc->p_regs;
1130
1131 if (req->dma_flag)
1132 return 1; /* DMA is forwarded */
1133
1134 if (length > 0) {
1135 pBuffer = (u8 *)req->req.buf;
1136 pBuf32 = (USB_REG_ACCESS *)(pBuffer + req->req.actual);
1137
1138 iWordLength = length / sizeof(u32);
1139 if (iWordLength > 0) {
1140 for (i = 0; i < iWordLength; i++) {
1141 _nbu2ss_writel(
1142 &preg->EP_REGS[ep->epnum-1].EP_WRITE
1143 , pBuf32->dw
1144 );
1145
1146 pBuf32++;
1147 }
1148 result = iWordLength * sizeof(u32);
1149 }
1150 }
1151
1152 if (result != ep->ep.maxpacket) {
1153 data = length - result;
1154 Temp32.dw = 0;
1155 for (i = 0 ; i < data ; i++)
1156 Temp32.byte.DATA[i] = pBuf32->byte.DATA[i];
1157
1158 _nbu2ss_ep_in_end(udc, ep->epnum, Temp32.dw, data);
1159 result += data;
1160 }
1161
1162 req->div_len = result;
1163
1164 return result;
1165}
1166
1167/*-------------------------------------------------------------------------*/
1168static int _nbu2ss_epn_in_data(
1169 struct nbu2ss_udc *udc,
1170 struct nbu2ss_ep *ep,
1171 struct nbu2ss_req *req,
1172 u32 data_size
1173)
1174{
1175 u32 num;
1176 int nret = 1;
1177
1178 if (ep->epnum == 0)
1179 return -EINVAL;
1180
1181 num = ep->epnum - 1;
1182
1183 if ((ep->ep_type != USB_ENDPOINT_XFER_INT)
1184 && (req->req.dma != 0)
1185 && (data_size >= sizeof(u32))) {
1186 nret = _nbu2ss_in_dma(udc, ep, req, num, data_size);
1187 } else {
1188 data_size = min(data_size, (u32)ep->ep.maxpacket);
1189 nret = _nbu2ss_epn_in_pio(udc, ep, req, data_size);
1190 }
1191
1192 return nret;
1193}
1194
1195/*-------------------------------------------------------------------------*/
1196static int _nbu2ss_epn_in_transfer(
1197 struct nbu2ss_udc *udc,
1198 struct nbu2ss_ep *ep,
1199 struct nbu2ss_req *req
1200)
1201{
1202 u32 num;
1203 u32 iBufSize;
1204 int result = 0;
1205 u32 status;
1206
1207 if (ep->epnum == 0)
1208 return -EINVAL;
1209
1210 num = ep->epnum - 1;
1211
1212 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
1213
1214 /*-------------------------------------------------------------*/
1215 /* State confirmation of FIFO */
1216 if (req->req.actual == 0) {
1217 if ((status & EPn_IN_EMPTY) == 0)
1218 return 1; /* Not Empty */
1219
1220 } else {
1221 if ((status & EPn_IN_FULL) != 0)
1222 return 1; /* Not Empty */
1223 }
1224
1225 /*-------------------------------------------------------------*/
1226 /* Start tranfer */
1227 iBufSize = req->req.length - req->req.actual;
1228 if (iBufSize > 0)
1229 result = _nbu2ss_epn_in_data(udc, ep, req, iBufSize);
1230 else if (req->req.length == 0)
1231 _nbu2ss_zero_len_pkt(udc, ep->epnum);
1232
1233 return result;
1234}
1235
1236/*-------------------------------------------------------------------------*/
1237static int _nbu2ss_start_transfer(
1238 struct nbu2ss_udc *udc,
1239 struct nbu2ss_ep *ep,
1240 struct nbu2ss_req *req,
1241 bool bflag)
1242{
1243 int nret = -EINVAL;
1244
1245 req->dma_flag = FALSE;
1246 req->div_len = 0;
1247
1248 if (req->req.length == 0)
1249 req->zero = 0;
1250 else {
1251 if ((req->req.length % ep->ep.maxpacket) == 0)
1252 req->zero = req->req.zero;
1253 else
1254 req->zero = 0;
1255 }
1256
1257 if (ep->epnum == 0) {
1258 /* EP0 */
1259 switch (udc->ep0state) {
1260 case EP0_IN_DATA_PHASE:
1261 nret = _nbu2ss_ep0_in_transfer(udc, ep, req);
1262 break;
1263
1264 case EP0_OUT_DATA_PHASE:
1265 nret = _nbu2ss_ep0_out_transfer(udc, ep, req);
1266 break;
1267
1268 case EP0_IN_STATUS_PHASE:
1269 nret = EP0_send_NULL(udc, TRUE);
1270 break;
1271
1272 default:
1273 break;
1274 }
1275
1276 } else {
1277 /* EPn */
1278 if (ep->direct == USB_DIR_OUT) {
1279 /* OUT */
1280 if (bflag == FALSE)
1281 nret = _nbu2ss_epn_out_transfer(udc, ep, req);
1282 } else {
1283 /* IN */
1284 nret = _nbu2ss_epn_in_transfer(udc, ep, req);
1285 }
1286 }
1287
1288 return nret;
1289}
1290
1291/*-------------------------------------------------------------------------*/
1292static void _nbu2ss_restert_transfer(struct nbu2ss_ep *ep)
1293{
1294 u32 length;
1295 bool bflag = FALSE;
1296 struct nbu2ss_req *req;
1297
1298 if (list_empty(&ep->queue))
1299 req = NULL;
1300 else
1301 req = list_entry(ep->queue.next, struct nbu2ss_req, queue);
1302
1303 if (req == NULL)
1304 return;
1305
1306 if (ep->epnum > 0) {
1307 length = _nbu2ss_readl(
1308 &ep->udc->p_regs->EP_REGS[ep->epnum-1].EP_LEN_DCNT);
1309
1310 length &= EPn_LDATA;
1311 if (length < ep->ep.maxpacket)
1312 bflag = TRUE;
1313 }
1314
1315 _nbu2ss_start_transfer(ep->udc, ep, req, bflag);
1316}
1317
1318/*-------------------------------------------------------------------------*/
1319/* Endpoint Toggle Reset */
1320static void _nbu2ss_endpoint_toggle_reset(
1321 struct nbu2ss_udc *udc,
1322 u8 ep_adrs)
1323{
1324 u8 num;
1325 u32 data;
1326
1327 if ((ep_adrs == 0) || (ep_adrs == 0x80))
1328 return;
1329
1330 num = (ep_adrs & 0x7F) - 1;
1331
1332 if (ep_adrs & USB_DIR_IN)
1333 data = EPn_IPIDCLR;
1334 else
1335 data = EPn_BCLR | EPn_OPIDCLR;
1336
1337 _nbu2ss_bitset(&udc->p_regs->EP_REGS[num].EP_CONTROL, data);
1338}
1339
1340/*-------------------------------------------------------------------------*/
1341/* Endpoint STALL set */
1342static void _nbu2ss_set_endpoint_stall(
1343 struct nbu2ss_udc *udc,
1344 u8 ep_adrs,
1345 bool bstall)
1346{
1347 u8 num, epnum;
1348 u32 data;
1349 struct nbu2ss_ep *ep;
1350 PT_FC_REGS preg = udc->p_regs;
1351
1352 if ((ep_adrs == 0) || (ep_adrs == 0x80)) {
1353 if (bstall) {
1354 /* Set STALL */
1355 _nbu2ss_bitset(&preg->EP0_CONTROL, EP0_STL);
1356 } else {
1357 /* Clear STALL */
1358 _nbu2ss_bitclr(&preg->EP0_CONTROL, EP0_STL);
1359 }
1360 } else {
1361 epnum = ep_adrs & USB_ENDPOINT_NUMBER_MASK;
1362 num = epnum - 1;
1363 ep = &udc->ep[epnum];
1364
1365 if (bstall) {
1366 /* Set STALL */
1367 ep->halted = TRUE;
1368
1369 if (ep_adrs & USB_DIR_IN)
1370 data = EPn_BCLR | EPn_ISTL;
1371 else
1372 data = EPn_OSTL_EN | EPn_OSTL;
1373
1374 _nbu2ss_bitset(&preg->EP_REGS[num].EP_CONTROL, data);
1375 } else {
1376 /* Clear STALL */
1377 ep->stalled = FALSE;
1378 if (ep_adrs & USB_DIR_IN) {
1379 _nbu2ss_bitclr(&preg->EP_REGS[num].EP_CONTROL
1380 , EPn_ISTL);
1381 } else {
1382 data =
1383 _nbu2ss_readl(&preg->EP_REGS[num].EP_CONTROL);
1384
1385 data &= ~EPn_OSTL;
1386 data |= EPn_OSTL_EN;
1387
1388 _nbu2ss_writel(&preg->EP_REGS[num].EP_CONTROL
1389 , data);
1390 }
1391
1392 ep->stalled = FALSE;
1393 if (ep->halted) {
1394 ep->halted = FALSE;
1395 _nbu2ss_restert_transfer(ep);
1396 }
1397 }
1398 }
1399
1400 return;
1401}
1402
1403
1404/*-------------------------------------------------------------------------*/
1405/* Device Descriptor */
1406static struct usb_device_descriptor device_desc = {
1407 .bLength = sizeof(device_desc),
1408 .bDescriptorType = USB_DT_DEVICE,
1409 .bcdUSB = __constant_cpu_to_le16(0x0200),
1410 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
1411 .bDeviceSubClass = 0x00,
1412 .bDeviceProtocol = 0x00,
1413 .bMaxPacketSize0 = 64,
1414 .idVendor = __constant_cpu_to_le16 (0x0409),
1415 .idProduct = __constant_cpu_to_le16 (0xfff0),
1416 .bcdDevice = 0xffff,
1417 .iManufacturer = 0x00,
1418 .iProduct = 0x00,
1419 .iSerialNumber = 0x00,
1420 .bNumConfigurations = 0x01,
1421};
1422
1423/*-------------------------------------------------------------------------*/
1424static void _nbu2ss_set_test_mode(struct nbu2ss_udc *udc, u32 mode)
1425{
1426 u32 data;
1427
1428 if (mode > MAX_TEST_MODE_NUM)
1429 return;
1430
1431 pr_info("SET FEATURE : test mode = %d\n", mode);
1432
1433 data = _nbu2ss_readl(&udc->p_regs->USB_CONTROL);
1434 data &= ~TEST_FORCE_ENABLE;
1435 data |= mode << TEST_MODE_SHIFT;
1436
1437 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, data);
1438 _nbu2ss_bitset(&udc->p_regs->TEST_CONTROL, CS_TESTMODEEN);
1439}
1440
1441/*-------------------------------------------------------------------------*/
1442static int _nbu2ss_set_feature_device(
1443 struct nbu2ss_udc *udc,
1444 u16 selector,
1445 u16 wIndex
1446)
1447{
1448 int result = -EOPNOTSUPP;
1449
1450 switch (selector) {
1451 case USB_DEVICE_REMOTE_WAKEUP:
1452 if (0x0000 == wIndex) {
1453 udc->remote_wakeup = U2F_ENABLE;
1454 result = 0;
1455 }
1456 break;
1457
1458 case USB_DEVICE_TEST_MODE:
1459 wIndex = wIndex >> 8;
1460 if (wIndex <= MAX_TEST_MODE_NUM)
1461 result = 0;
1462 break;
1463
1464 default:
1465 break;
1466 }
1467
1468 return result;
1469}
1470
1471/*-------------------------------------------------------------------------*/
1472static int _nbu2ss_get_ep_stall(struct nbu2ss_udc *udc, u8 ep_adrs)
1473{
1474 u8 epnum;
1475 u32 data = 0, bit_data;
1476 PT_FC_REGS preg = udc->p_regs;
1477
1478 epnum = ep_adrs & ~USB_ENDPOINT_DIR_MASK;
1479 if (epnum == 0) {
1480 data = _nbu2ss_readl(&preg->EP0_CONTROL);
1481 bit_data = EP0_STL;
1482
1483 } else {
1484 data = _nbu2ss_readl(&preg->EP_REGS[epnum-1].EP_CONTROL);
1485 if ((data & EPn_EN) == 0)
1486 return -1;
1487
1488 if (ep_adrs & USB_ENDPOINT_DIR_MASK)
1489 bit_data = EPn_ISTL;
1490 else
1491 bit_data = EPn_OSTL;
1492 }
1493
1494 if ((data & bit_data) == 0)
1495 return 0;
1496 else
1497 return 1;
1498}
1499
1500/*-------------------------------------------------------------------------*/
1501static inline int _nbu2ss_req_feature(struct nbu2ss_udc *udc, bool bset)
1502{
1503 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1504 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1505 u16 selector = udc->ctrl.wValue;
1506 u16 wIndex = udc->ctrl.wIndex;
1507 u8 ep_adrs;
1508 int result = -EOPNOTSUPP;
1509
1510 if ((0x0000 != udc->ctrl.wLength) ||
1511 (USB_DIR_OUT != direction)) {
1512 return -EINVAL;
1513 }
1514
1515 switch (recipient) {
1516 case USB_RECIP_DEVICE:
1517 if (bset)
1518 result =
1519 _nbu2ss_set_feature_device(udc, selector, wIndex);
1520 break;
1521
1522 case USB_RECIP_ENDPOINT:
1523 if (0x0000 == (wIndex & 0xFF70)) {
1524 if (USB_ENDPOINT_HALT == selector) {
1525 ep_adrs = wIndex & 0xFF;
1526 if (bset == FALSE) {
1527 _nbu2ss_endpoint_toggle_reset(
1528 udc, ep_adrs);
1529 }
1530
1531 _nbu2ss_set_endpoint_stall(
1532 udc, ep_adrs, bset);
1533
1534 result = 0;
1535 }
1536 }
1537 break;
1538
1539 default:
1540 break;
1541 }
1542
1543 if (result >= 0)
1544 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1545
1546 return result;
1547}
1548
1549/*-------------------------------------------------------------------------*/
1550static inline enum usb_device_speed _nbu2ss_get_speed(struct nbu2ss_udc *udc)
1551{
1552 u32 data;
1553 enum usb_device_speed speed = USB_SPEED_FULL;
1554
1555 data = _nbu2ss_readl(&udc->p_regs->USB_STATUS);
1556 if (data & HIGH_SPEED)
1557 speed = USB_SPEED_HIGH;
1558
1559 return speed;
1560}
1561
1562/*-------------------------------------------------------------------------*/
1563static void _nbu2ss_epn_set_stall(
1564 struct nbu2ss_udc *udc,
1565 struct nbu2ss_ep *ep
1566)
1567{
1568 u8 ep_adrs;
1569 u32 regdata;
1570 int limit_cnt = 0;
1571
1572 PT_FC_REGS preg = udc->p_regs;
1573
1574 if (ep->direct == USB_DIR_IN) {
1575 for (limit_cnt = 0
1576 ; limit_cnt < IN_DATA_EMPTY_COUNT
1577 ; limit_cnt++) {
1578
1579 regdata = _nbu2ss_readl(
1580 &preg->EP_REGS[ep->epnum-1].EP_STATUS);
1581
1582 if ((regdata & EPn_IN_DATA) == 0)
1583 break;
1584
1585 mdelay(1);
1586 }
1587 }
1588
1589 ep_adrs = ep->epnum | ep->direct;
1590 _nbu2ss_set_endpoint_stall(udc, ep_adrs, 1);
1591}
1592
1593/*-------------------------------------------------------------------------*/
1594static int std_req_get_status(struct nbu2ss_udc *udc)
1595{
1596 u32 length;
1597 u16 status_data = 0;
1598 u8 recipient = (u8)(udc->ctrl.bRequestType & USB_RECIP_MASK);
1599 u8 direction = (u8)(udc->ctrl.bRequestType & USB_DIR_IN);
1600 u8 ep_adrs;
1601 int result = -EINVAL;
1602
1603 if ((0x0000 != udc->ctrl.wValue)
1604 || (USB_DIR_IN != direction)) {
1605
1606 return result;
1607 }
1608
1609 length = min(udc->ctrl.wLength, (u16)sizeof(status_data));
1610
1611 switch (recipient) {
1612 case USB_RECIP_DEVICE:
1613 if (udc->ctrl.wIndex == 0x0000) {
1614 if (udc->self_powered)
1615 status_data |= (1 << USB_DEVICE_SELF_POWERED);
1616
1617 if (udc->remote_wakeup)
1618 status_data |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1619
1620 result = 0;
1621 }
1622 break;
1623
1624 case USB_RECIP_ENDPOINT:
1625 if (0x0000 == (udc->ctrl.wIndex & 0xFF70)) {
1626 ep_adrs = (u8)(udc->ctrl.wIndex & 0xFF);
1627 result = _nbu2ss_get_ep_stall(udc, ep_adrs);
1628
1629 if (result > 0)
1630 status_data |= (1 << USB_ENDPOINT_HALT);
1631 }
1632 break;
1633
1634 default:
1635 break;
1636 }
1637
1638 if (result >= 0) {
1639 memcpy(udc->ep0_buf, &status_data, length);
1640 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, length);
1641 _nbu2ss_ep0_in_transfer(udc, &udc->ep[0], &udc->ep0_req);
1642
1643 } else {
1644 ERR("*** Error GET_STATUS\n");
1645 }
1646
1647 return result;
1648}
1649
1650/*-------------------------------------------------------------------------*/
1651static int std_req_clear_feature(struct nbu2ss_udc *udc)
1652{
1653 return _nbu2ss_req_feature(udc, FALSE);
1654}
1655
1656/*-------------------------------------------------------------------------*/
1657static int std_req_set_feature(struct nbu2ss_udc *udc)
1658{
1659 return _nbu2ss_req_feature(udc, TRUE);
1660}
1661
1662/*-------------------------------------------------------------------------*/
1663static int std_req_set_address(struct nbu2ss_udc *udc)
1664{
1665 int result = 0;
1666 u32 wValue = udc->ctrl.wValue;
1667
1668 if ((0x00 != udc->ctrl.bRequestType) ||
1669 (0x0000 != udc->ctrl.wIndex) ||
1670 (0x0000 != udc->ctrl.wLength)) {
1671 return -EINVAL;
1672 }
1673
1674 if (wValue != (wValue & 0x007F))
1675 return -EINVAL;
1676
1677 wValue = wValue << USB_ADRS_SHIFT;
1678
1679 _nbu2ss_writel(&udc->p_regs->USB_ADDRESS, wValue);
1680 _nbu2ss_create_ep0_packet(udc, udc->ep0_buf, 0);
1681
1682 return result;
1683}
1684
1685/*-------------------------------------------------------------------------*/
1686static int std_req_set_configuration(struct nbu2ss_udc *udc)
1687{
1688 u32 ConfigValue = (u32)(udc->ctrl.wValue & 0x00ff);
1689
1690 if ((0x0000 != udc->ctrl.wIndex) ||
1691 (0x0000 != udc->ctrl.wLength) ||
1692 (0x00 != udc->ctrl.bRequestType)) {
1693 return -EINVAL;
1694 }
1695
1696 udc->curr_config = ConfigValue;
1697
1698 if (ConfigValue > 0) {
1699 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, CONF);
1700 udc->devstate = USB_STATE_CONFIGURED;
1701
1702 } else {
1703 _nbu2ss_bitclr(&udc->p_regs->USB_CONTROL, CONF);
1704 udc->devstate = USB_STATE_ADDRESS;
1705 }
1706
1707 return 0;
1708}
1709
1710/*-------------------------------------------------------------------------*/
1711static inline void _nbu2ss_read_request_data(struct nbu2ss_udc *udc, u32 *pdata)
1712{
1713 if ((udc == NULL) && (pdata == NULL))
1714 return;
1715
1716 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA0);
1717 pdata++;
1718 *pdata = _nbu2ss_readl(&udc->p_regs->SETUP_DATA1);
1719}
1720
1721/*-------------------------------------------------------------------------*/
1722static inline int _nbu2ss_decode_request(struct nbu2ss_udc *udc)
1723{
1724 bool bcall_back = TRUE;
1725 int nret = -EINVAL;
1726 struct usb_ctrlrequest *p_ctrl;
1727
1728 p_ctrl = &udc->ctrl;
1729 _nbu2ss_read_request_data(udc, (u32 *)p_ctrl);
1730
1731 /* ep0 state control */
1732 if (p_ctrl->wLength == 0) {
1733 udc->ep0state = EP0_IN_STATUS_PHASE;
1734
1735 } else {
1736 if (p_ctrl->bRequestType & USB_DIR_IN)
1737 udc->ep0state = EP0_IN_DATA_PHASE;
1738 else
1739 udc->ep0state = EP0_OUT_DATA_PHASE;
1740 }
1741
1742 if ((p_ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1743 switch (p_ctrl->bRequest) {
1744 case USB_REQ_GET_STATUS:
1745 nret = std_req_get_status(udc);
1746 bcall_back = FALSE;
1747 break;
1748
1749 case USB_REQ_CLEAR_FEATURE:
1750 nret = std_req_clear_feature(udc);
1751 bcall_back = FALSE;
1752 break;
1753
1754 case USB_REQ_SET_FEATURE:
1755 nret = std_req_set_feature(udc);
1756 bcall_back = FALSE;
1757 break;
1758
1759 case USB_REQ_SET_ADDRESS:
1760 nret = std_req_set_address(udc);
1761 bcall_back = FALSE;
1762 break;
1763
1764 case USB_REQ_SET_CONFIGURATION:
1765 nret = std_req_set_configuration(udc);
1766 break;
1767
1768 default:
1769 break;
1770 }
1771 }
1772
1773 if (bcall_back == FALSE) {
1774 if (udc->ep0state == EP0_IN_STATUS_PHASE) {
1775 if (nret >= 0) {
1776 /*--------------------------------------*/
1777 /* Status Stage */
1778 nret = EP0_send_NULL(udc, TRUE);
1779 }
1780 }
1781
1782 } else {
1783 spin_unlock(&udc->lock);
1784 nret = udc->driver->setup(&udc->gadget, &udc->ctrl);
1785 spin_lock(&udc->lock);
1786 }
1787
1788 if (nret < 0)
1789 udc->ep0state = EP0_IDLE;
1790
1791 return nret;
1792}
1793
1794/*-------------------------------------------------------------------------*/
1795static inline int _nbu2ss_ep0_in_data_stage(struct nbu2ss_udc *udc)
1796{
1797 int nret;
1798 struct nbu2ss_req *req;
1799 struct nbu2ss_ep *ep = &udc->ep[0];
1800
1801 if (list_empty(&ep->queue))
1802 req = NULL;
1803 else
1804 req = list_entry(ep->queue.next, struct nbu2ss_req, queue);
1805
1806 if (req == NULL)
1807 req = &udc->ep0_req;
1808
1809 req->req.actual += req->div_len;
1810 req->div_len = 0;
1811
1812 nret = _nbu2ss_ep0_in_transfer(udc, ep, req);
1813 if (nret == 0) {
1814 udc->ep0state = EP0_OUT_STATUS_PAHSE;
1815 EP0_receive_NULL(udc, TRUE);
1816 }
1817
1818 return 0;
1819}
1820
1821/*-------------------------------------------------------------------------*/
1822static inline int _nbu2ss_ep0_out_data_stage(struct nbu2ss_udc *udc)
1823{
1824 int nret;
1825 struct nbu2ss_req *req;
1826 struct nbu2ss_ep *ep = &udc->ep[0];
1827
1828 if (list_empty(&ep->queue))
1829 req = NULL;
1830 else
1831 req = list_entry(ep->queue.next, struct nbu2ss_req, queue);
1832
1833 if (req == NULL)
1834 req = &udc->ep0_req;
1835
1836 nret = _nbu2ss_ep0_out_transfer(udc, ep, req);
1837 if (nret == 0) {
1838 udc->ep0state = EP0_IN_STATUS_PHASE;
1839 EP0_send_NULL(udc, TRUE);
1840
1841 } else if (nret < 0) {
1842 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, EP0_BCLR);
1843 req->req.status = nret;
1844 }
1845
1846 return 0;
1847}
1848
1849/*-------------------------------------------------------------------------*/
1850static inline int _nbu2ss_ep0_status_stage(struct nbu2ss_udc *udc)
1851{
1852 struct nbu2ss_req *req;
1853 struct nbu2ss_ep *ep = &udc->ep[0];
1854
1855 if (list_empty(&ep->queue))
1856 req = NULL;
1857 else
1858 req = list_entry(ep->queue.next, struct nbu2ss_req, queue);
1859
1860 if (req == NULL) {
1861 req = &udc->ep0_req;
1862 if (req->req.complete)
1863 req->req.complete(&ep->ep, &req->req);
1864
1865 } else {
1866 if (req->req.complete)
1867 _nbu2ss_ep_done(ep, req, 0);
1868 }
1869
1870 udc->ep0state = EP0_IDLE;
1871
1872 return 0;
1873}
1874
1875/*-------------------------------------------------------------------------*/
1876static inline void _nbu2ss_ep0_int(struct nbu2ss_udc *udc)
1877{
1878 int i;
1879 u32 status;
1880 u32 intr;
1881 int nret = -1;
1882
1883 status = _nbu2ss_readl(&udc->p_regs->EP0_STATUS);
1884 intr = status & EP0_STATUS_RW_BIT;
1885 _nbu2ss_writel(&udc->p_regs->EP0_STATUS, ~(u32)intr);
1886
1887 status &= (SETUP_INT | EP0_IN_INT | EP0_OUT_INT
1888 | STG_END_INT | EP0_OUT_NULL_INT);
1889
1890 if (status == 0) {
1891 pr_info("--- %s Not Decode Interrupt\n", __func__);
1892 pr_info("--- EP0_STATUS = 0x%08x\n", intr);
1893 return;
1894 }
1895
1896 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
1897 udc->gadget.speed = _nbu2ss_get_speed(udc);
1898
1899 for (i = 0; i < EP0_END_XFER; i++) {
1900 switch (udc->ep0state) {
1901 case EP0_IDLE:
1902 if (status & SETUP_INT) {
1903 status = 0;
1904 nret = _nbu2ss_decode_request(udc);
1905 }
1906 break;
1907
1908 case EP0_IN_DATA_PHASE:
1909 if (status & EP0_IN_INT) {
1910 status &= ~EP0_IN_INT;
1911 nret = _nbu2ss_ep0_in_data_stage(udc);
1912 }
1913 break;
1914
1915 case EP0_OUT_DATA_PHASE:
1916 if (status & EP0_OUT_INT) {
1917 status &= ~EP0_OUT_INT;
1918 nret = _nbu2ss_ep0_out_data_stage(udc);
1919 }
1920 break;
1921
1922 case EP0_IN_STATUS_PHASE:
1923 if ((status & STG_END_INT) || (status & SETUP_INT)) {
1924 status &= ~(STG_END_INT | EP0_IN_INT);
1925 nret = _nbu2ss_ep0_status_stage(udc);
1926 }
1927 break;
1928
1929 case EP0_OUT_STATUS_PAHSE:
1930 if ((status & STG_END_INT)
1931 || (status & SETUP_INT)
1932 || (status & EP0_OUT_NULL_INT)) {
1933 status &= ~(STG_END_INT
1934 | EP0_OUT_INT
1935 | EP0_OUT_NULL_INT);
1936
1937 nret = _nbu2ss_ep0_status_stage(udc);
1938 }
1939
1940 break;
1941
1942 default:
1943 status = 0;
1944 break;
1945 }
1946
1947 if (status == 0)
1948 break;
1949 }
1950
1951 if (nret < 0) {
1952 /* Send Stall */
1953 _nbu2ss_set_endpoint_stall(udc, 0, TRUE);
1954 }
1955}
1956
1957/*-------------------------------------------------------------------------*/
1958static void _nbu2ss_ep_done(
1959 struct nbu2ss_ep *ep,
1960 struct nbu2ss_req *req,
1961 int status)
1962{
1963 struct nbu2ss_udc *udc = ep->udc;
1964
1965 list_del_init(&req->queue);
1966
1967 if (status == -ECONNRESET)
1968 _nbu2ss_fifo_flush(udc, ep);
1969
1970 if (likely(req->req.status == -EINPROGRESS))
1971 req->req.status = status;
1972
1973 if (ep->stalled)
1974 _nbu2ss_epn_set_stall(udc, ep);
1975 else {
1976 if (!list_empty(&ep->queue))
1977 _nbu2ss_restert_transfer(ep);
1978 }
1979
1980#ifdef USE_DMA
1981 if ((ep->direct == USB_DIR_OUT) && (ep->epnum > 0) &&
1982 (req->req.dma != 0))
1983 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_OUT);
1984#endif
1985
1986 spin_unlock(&udc->lock);
1987 req->req.complete(&ep->ep, &req->req);
1988 spin_lock(&udc->lock);
1989}
1990
1991/*-------------------------------------------------------------------------*/
1992static inline void _nbu2ss_epn_in_int(
1993 struct nbu2ss_udc *udc,
1994 struct nbu2ss_ep *ep,
1995 struct nbu2ss_req *req)
1996{
1997 int result = 0;
1998 u32 status;
1999
2000 PT_FC_REGS preg = udc->p_regs;
2001
2002 if (req->dma_flag)
2003 return; /* DMA is forwarded */
2004
2005 req->req.actual += req->div_len;
2006 req->div_len = 0;
2007
2008 if (req->req.actual != req->req.length) {
2009 /*---------------------------------------------------------*/
2010 /* remainder of data */
2011 result = _nbu2ss_epn_in_transfer(udc, ep, req);
2012
2013 } else {
2014 if ((req->zero != 0)
2015 && ((req->req.actual % ep->ep.maxpacket) == 0)) {
2016
2017 status =
2018 _nbu2ss_readl(&preg->EP_REGS[ep->epnum-1].EP_STATUS);
2019
2020 if ((status & EPn_IN_FULL) == 0) {
2021 /*-----------------------------------------*/
2022 /* 0 Length Packet */
2023 req->zero = 0;
2024 _nbu2ss_zero_len_pkt(udc, ep->epnum);
2025 }
2026 return;
2027 }
2028 }
2029
2030 if (result <= 0) {
2031 /*---------------------------------------------------------*/
2032 /* Complete */
2033 _nbu2ss_ep_done(ep, req, result);
2034 }
2035}
2036
2037/*-------------------------------------------------------------------------*/
2038static inline void _nbu2ss_epn_out_int(
2039 struct nbu2ss_udc *udc,
2040 struct nbu2ss_ep *ep,
2041 struct nbu2ss_req *req)
2042{
2043 int result;
2044
2045 result = _nbu2ss_epn_out_transfer(udc, ep, req);
2046 if (result <= 0)
2047 _nbu2ss_ep_done(ep, req, result);
2048
2049 return;
2050}
2051
2052/*-------------------------------------------------------------------------*/
2053static inline void _nbu2ss_epn_in_dma_int(
2054 struct nbu2ss_udc *udc,
2055 struct nbu2ss_ep *ep,
2056 struct nbu2ss_req *req)
2057{
2058 u32 mpkt;
2059 u32 size;
2060 struct usb_request *preq;
2061
2062 preq = &req->req;
2063
2064 if (req->dma_flag == FALSE)
2065 return;
2066
2067 preq->actual += req->div_len;
2068 req->div_len = 0;
2069 req->dma_flag = FALSE;
2070
2071#ifdef USE_DMA
2072 _nbu2ss_dma_unmap_single(udc, ep, req, USB_DIR_IN);
2073#endif
2074
2075 if (preq->actual != preq->length) {
2076 _nbu2ss_epn_in_transfer(udc, ep, req);
2077 } else {
2078 mpkt = ep->ep.maxpacket;
2079 size = preq->actual % mpkt;
2080 if (size > 0) {
2081 if (((preq->actual & 0x03) == 0) && (size < mpkt))
2082 _nbu2ss_ep_in_end(udc, ep->epnum, 0, 0);
2083 } else {
2084 _nbu2ss_epn_in_int(udc, ep, req);
2085 }
2086 }
2087
2088 return;
2089}
2090
2091/*-------------------------------------------------------------------------*/
2092static inline void _nbu2ss_epn_out_dma_int(
2093 struct nbu2ss_udc *udc,
2094 struct nbu2ss_ep *ep,
2095 struct nbu2ss_req *req)
2096{
2097 int i;
2098 u32 num;
2099 u32 dmacnt, ep_dmacnt;
2100 u32 mpkt;
2101 PT_FC_REGS preg = udc->p_regs;
2102
2103 num = ep->epnum - 1;
2104
2105 if (req->req.actual == req->req.length) {
2106 if ((req->req.length % ep->ep.maxpacket)
2107 && (req->zero == 0)) {
2108 req->div_len = 0;
2109 req->dma_flag = FALSE;
2110 _nbu2ss_ep_done(ep, req, 0);
2111 return;
2112 }
2113 }
2114
2115 ep_dmacnt = _nbu2ss_readl(&preg->EP_REGS[num].EP_LEN_DCNT)
2116 & EPn_DMACNT;
2117 ep_dmacnt >>= 16;
2118
2119 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
2120 dmacnt = _nbu2ss_readl(&preg->EP_DCR[num].EP_DCR1)
2121 & DCR1_EPn_DMACNT;
2122 dmacnt >>= 16;
2123 if (ep_dmacnt == dmacnt)
2124 break;
2125 }
2126
2127 _nbu2ss_bitclr(&preg->EP_DCR[num].EP_DCR1, DCR1_EPn_REQEN);
2128
2129 if (dmacnt != 0) {
2130 mpkt = ep->ep.maxpacket;
2131 if ((req->div_len % mpkt) == 0)
2132 req->div_len -= mpkt * dmacnt;
2133 }
2134
2135 if ((req->req.actual % ep->ep.maxpacket) > 0) {
2136 if (req->req.actual == req->div_len) {
2137 req->div_len = 0;
2138 req->dma_flag = FALSE;
2139 _nbu2ss_ep_done(ep, req, 0);
2140 return;
2141 }
2142 }
2143
2144 req->req.actual += req->div_len;
2145 req->div_len = 0;
2146 req->dma_flag = FALSE;
2147
2148 _nbu2ss_epn_out_int(udc, ep, req);
2149}
2150
2151/*-------------------------------------------------------------------------*/
2152static inline void _nbu2ss_epn_int(struct nbu2ss_udc *udc, u32 epnum)
2153{
2154 u32 num;
2155 u32 status;
2156
2157 struct nbu2ss_req *req;
2158 struct nbu2ss_ep *ep = &udc->ep[epnum];
2159
2160 num = epnum - 1;
2161
2162 /* Interrupt Status */
2163 status = _nbu2ss_readl(&udc->p_regs->EP_REGS[num].EP_STATUS);
2164
2165 /* Interrupt Clear */
2166 _nbu2ss_writel(&udc->p_regs->EP_REGS[num].EP_STATUS, ~(u32)status);
2167
2168 if (list_empty(&ep->queue))
2169 req = NULL;
2170 else
2171 req = list_entry(ep->queue.next, struct nbu2ss_req, queue);
2172
2173 if (req == NULL) {
2174 /* pr_warning("=== %s(%d) req == NULL\n", __func__, epnum); */
2175 return;
2176 }
2177
2178 if (status & EPn_OUT_END_INT) {
2179 status &= ~EPn_OUT_INT;
2180 _nbu2ss_epn_out_dma_int(udc, ep, req);
2181 }
2182
2183 if (status & EPn_OUT_INT)
2184 _nbu2ss_epn_out_int(udc, ep, req);
2185
2186 if (status & EPn_IN_END_INT) {
2187 status &= ~EPn_IN_INT;
2188 _nbu2ss_epn_in_dma_int(udc, ep, req);
2189 }
2190
2191 if (status & EPn_IN_INT)
2192 _nbu2ss_epn_in_int(udc, ep, req);
2193}
2194
2195/*-------------------------------------------------------------------------*/
2196static inline void _nbu2ss_ep_int(struct nbu2ss_udc *udc, u32 epnum)
2197{
2198 if (epnum == 0)
2199 _nbu2ss_ep0_int(udc);
2200 else
2201 _nbu2ss_epn_int(udc, epnum);
2202}
2203
2204/*-------------------------------------------------------------------------*/
2205static void _nbu2ss_ep0_enable(struct nbu2ss_udc *udc)
2206{
2207 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL, (EP0_AUTO | EP0_BCLR));
2208 _nbu2ss_writel(&udc->p_regs->EP0_INT_ENA, EP0_INT_EN_BIT);
2209
2210 return;
2211}
2212
2213#if 0
2214/*-------------------------------------------------------------------------*/
2215static void _nbu2ss_ep0_disable(struct nbu2ss_udc *udc)
2216{
2217 _nbu2ss_bitclr(&udc->p_regs->EP0_INT_ENA, EP0_INT_EN_BIT);
2218
2219 _nbu2ss_bitset(&udc->p_regs->EP0_CONTROL
2220 , (EP0_BCLR | EP0_INAK | EP0_ONAK | EP0_BCLR));
2221
2222 _nbu2ss_bitclr(&udc->p_regs->EP0_CONTROL, EP0_AUTO);
2223
2224 return;
2225}
2226#endif
2227
2228/*-------------------------------------------------------------------------*/
2229static int _nbu2ss_nuke(struct nbu2ss_udc *udc,
2230 struct nbu2ss_ep *ep,
2231 int status)
2232{
2233 struct nbu2ss_req *req;
2234
2235 /* Endpoint Disable */
2236 _nbu2ss_epn_exit(udc, ep);
2237
2238 /* DMA Disable */
2239 _nbu2ss_ep_dma_exit(udc, ep);
2240
2241 if (list_empty(&ep->queue))
2242 return 0;
2243
2244 /* called with irqs blocked */
2245 while (!list_empty(&ep->queue)) {
2246 req = list_entry(ep->queue.next, struct nbu2ss_req, queue);
2247 _nbu2ss_ep_done(ep, req, status);
2248 }
2249
2250 return 0;
2251}
2252
2253/*-------------------------------------------------------------------------*/
2254static void _nbu2ss_quiesce(struct nbu2ss_udc *udc)
2255{
2256 struct nbu2ss_ep *ep;
2257
2258 udc->gadget.speed = USB_SPEED_UNKNOWN;
2259
2260 _nbu2ss_nuke(udc, &udc->ep[0], -ESHUTDOWN);
2261
2262 /* Endpoint n */
2263 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2264 _nbu2ss_nuke(udc, ep, -ESHUTDOWN);
2265 }
2266}
2267
2268/*-------------------------------------------------------------------------*/
2269static int _nbu2ss_pullup(struct nbu2ss_udc *udc, int is_on)
2270{
2271 u32 reg_dt;
2272
2273 if (!udc) {
2274 ERR("%s, bad param\n", __func__);
2275 return -EINVAL;
2276 }
2277
2278 if (udc->vbus_active == 0)
2279 return -ESHUTDOWN;
2280
2281 if (is_on) {
2282 /* D+ Pullup */
2283/* INFO(" --- D+ Pullup\n"); */
2284
2285 if (udc->driver) {
2286 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL)
2287 | PUE2) & ~(u32)CONNECTB;
2288
2289 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2290 }
2291
2292 } else {
2293 /* D+ Pulldown */
2294/* INFO(" --- D+ Pulldown\n"); */
2295
2296 reg_dt = (_nbu2ss_readl(&udc->p_regs->USB_CONTROL) | CONNECTB)
2297 & ~(u32)PUE2;
2298
2299 _nbu2ss_writel(&udc->p_regs->USB_CONTROL, reg_dt);
2300 udc->gadget.speed = USB_SPEED_UNKNOWN;
2301 }
2302
2303 return 0;
2304}
2305
2306/*-------------------------------------------------------------------------*/
2307static void _nbu2ss_fifo_flush(struct nbu2ss_udc *udc, struct nbu2ss_ep *ep)
2308{
2309 PT_FC_REGS p = udc->p_regs;
2310
2311 if (udc->vbus_active == 0)
2312 return;
2313
2314 if (ep->epnum == 0) {
2315 /* EP0 */
2316 _nbu2ss_bitset(&p->EP0_CONTROL, EP0_BCLR);
2317
2318 } else {
2319 /* EPn */
2320 _nbu2ss_ep_dma_abort(udc, ep);
2321 _nbu2ss_bitset(&p->EP_REGS[ep->epnum - 1].EP_CONTROL, EPn_BCLR);
2322 }
2323}
2324
2325/*-------------------------------------------------------------------------*/
2326static int _nbu2ss_enable_controller(struct nbu2ss_udc *udc)
2327{
2328 int waitcnt = 0;
2329
2330 if (udc->udc_enabled)
2331 return 0;
2332
2333#if 0
2334 emxx_open_clockgate(EMXX_CLK_USB1);
2335 /* emxx_clkctrl_off(EMXX_CLKCTRL_USB1); */
2336 /* emxx_clkctrl_on(EMXX_CLKCTRL_USB1); */
2337 emxx_unreset_device(EMXX_RST_USB1);
2338#endif
2339 /*
2340 Reset
2341 */
2342 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2343 udelay(EPC_RST_DISABLE_TIME); /* 1us wait */
2344
2345 _nbu2ss_bitclr(&udc->p_regs->EPCTR, DIRPD);
2346 mdelay(EPC_DIRPD_DISABLE_TIME); /* 1ms wait */
2347
2348 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2349
2350 _nbu2ss_writel(&udc->p_regs->AHBSCTR, WAIT_MODE);
2351
2352#if 0
2353 /* DMA Mode Setting */
2354 if ((system_rev & EMXX_REV_MASK) == EMXX_REV_ES1) {
2355 _nbu2ss_bitset(&udc->p_regs->AHBMCTR, BURST_TYPE);
2356 _nbu2ss_bitclr(&udc->p_regs->AHBMCTR, HTRANS_MODE);
2357 } else
2358#endif
2359 _nbu2ss_writel(&udc->p_regs->AHBMCTR,
2360 HBUSREQ_MODE | HTRANS_MODE | WBURST_TYPE);
2361
2362 while (!(_nbu2ss_readl(&udc->p_regs->EPCTR) & PLL_LOCK)) {
2363 waitcnt++;
2364 udelay(1); /* 1us wait */
2365 if (waitcnt == EPC_PLL_LOCK_COUNT) {
2366 ERR("*** Reset Cancel failed\n");
2367 return -EINVAL;
2368 }
2369 };
2370
2371#if 0
2372 if ((system_rev & EMXX_REV_MASK) < EMXX_REV_ES3)
2373#endif
2374 _nbu2ss_bitset(&udc->p_regs->UTMI_CHARACTER_1, USB_SQUSET);
2375
2376 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, (INT_SEL | SOF_RCV));
2377
2378 /* EP0 */
2379 _nbu2ss_ep0_enable(udc);
2380
2381 /* USB Interrupt Enable */
2382 _nbu2ss_bitset(&udc->p_regs->USB_INT_ENA, USB_INT_EN_BIT);
2383
2384 udc->udc_enabled = TRUE;
2385
2386 return 0;
2387}
2388
2389
2390/*-------------------------------------------------------------------------*/
2391static void _nbu2ss_reset_controller(struct nbu2ss_udc *udc)
2392{
2393 _nbu2ss_bitset(&udc->p_regs->EPCTR, EPC_RST);
2394 _nbu2ss_bitclr(&udc->p_regs->EPCTR, EPC_RST);
2395}
2396
2397/*-------------------------------------------------------------------------*/
2398static void _nbu2ss_disable_controller(struct nbu2ss_udc *udc)
2399{
2400 if (udc->udc_enabled) {
2401 udc->udc_enabled = FALSE;
2402 _nbu2ss_reset_controller(udc);
2403 _nbu2ss_bitset(&udc->p_regs->EPCTR, (DIRPD | EPC_RST));
2404 }
2405#if 0
2406 emxx_reset_device(EMXX_RST_USB1);
2407 /* emxx_clkctrl_on(EMXX_CLKCTRL_USB1); */
2408 emxx_close_clockgate(EMXX_CLK_USB1);
2409#endif
2410}
2411
2412/*-------------------------------------------------------------------------*/
2413static inline void _nbu2ss_check_vbus(struct nbu2ss_udc *udc)
2414{
2415 int nret;
2416 u32 reg_dt;
2417
2418 /* chattering */
2419 mdelay(VBUS_CHATTERING_MDELAY); /* wait (ms) */
2420
2421 /* VBUS ON Check*/
2422 reg_dt = gpio_get_value(VBUS_VALUE);
2423 if (reg_dt == 0) {
2424
2425 udc->linux_suspended = 0;
2426
2427 _nbu2ss_reset_controller(udc);
2428 pr_info(" ----- VBUS OFF\n");
2429
2430 if (udc->vbus_active == 1) {
2431 /* VBUS OFF */
2432 udc->vbus_active = 0;
2433 if (udc->usb_suspended) {
2434 udc->usb_suspended = 0;
2435 /* _nbu2ss_reset_controller(udc); */
2436 }
2437 udc->devstate = USB_STATE_NOTATTACHED;
2438
2439 _nbu2ss_quiesce(udc);
2440 if (udc->driver) {
2441 spin_unlock(&udc->lock);
2442 udc->driver->disconnect(&udc->gadget);
2443 spin_lock(&udc->lock);
2444 }
2445
2446 _nbu2ss_disable_controller(udc);
2447 }
2448 } else {
2449 mdelay(5); /* wait (5ms) */
2450 reg_dt = gpio_get_value(VBUS_VALUE);
2451 if (reg_dt == 0)
2452 return;
2453
2454 pr_info(" ----- VBUS ON\n");
2455
2456 if (udc->linux_suspended)
2457 return;
2458
2459 if (udc->vbus_active == 0) {
2460 /* VBUS ON */
2461 udc->vbus_active = 1;
2462 udc->devstate = USB_STATE_POWERED;
2463
2464 nret = _nbu2ss_enable_controller(udc);
2465 if (nret < 0) {
2466 _nbu2ss_disable_controller(udc);
2467 udc->vbus_active = 0;
2468 return;
2469 }
2470
2471 _nbu2ss_pullup(udc, 1);
2472
2473#ifdef UDC_DEBUG_DUMP
2474 _nbu2ss_dump_register(udc);
2475#endif /* UDC_DEBUG_DUMP */
2476
2477 } else {
2478 if (udc->devstate == USB_STATE_POWERED)
2479 _nbu2ss_pullup(udc, 1);
2480 }
2481 }
2482
2483 return;
2484}
2485
2486/*-------------------------------------------------------------------------*/
2487static inline void _nbu2ss_int_bus_reset(struct nbu2ss_udc *udc)
2488{
2489 udc->devstate = USB_STATE_DEFAULT;
2490 udc->remote_wakeup = 0;
2491
2492 _nbu2ss_quiesce(udc);
2493
2494 udc->ep0state = EP0_IDLE;
2495}
2496
2497/*-------------------------------------------------------------------------*/
2498static inline void _nbu2ss_int_usb_resume(struct nbu2ss_udc *udc)
2499{
2500 if (udc->usb_suspended == 1) {
2501 udc->usb_suspended = 0;
2502 if (udc->driver && udc->driver->resume) {
2503 spin_unlock(&udc->lock);
2504 udc->driver->resume(&udc->gadget);
2505 spin_lock(&udc->lock);
2506 }
2507 }
2508}
2509
2510/*-------------------------------------------------------------------------*/
2511static inline void _nbu2ss_int_usb_suspend(struct nbu2ss_udc *udc)
2512{
2513 u32 reg_dt;
2514
2515 if (udc->usb_suspended == 0) {
2516 reg_dt = gpio_get_value(VBUS_VALUE);
2517
2518 if (reg_dt == 0)
2519 return;
2520
2521 udc->usb_suspended = 1;
2522 if (udc->driver && udc->driver->suspend) {
2523 spin_unlock(&udc->lock);
2524 udc->driver->suspend(&udc->gadget);
2525 spin_lock(&udc->lock);
2526 }
2527
2528 _nbu2ss_bitset(&udc->p_regs->USB_CONTROL, SUSPEND);
2529 }
2530}
2531
2532/*-------------------------------------------------------------------------*/
2533/* VBUS (GPIO153) Interrupt */
2534static irqreturn_t _nbu2ss_vbus_irq(int irq, void *_udc)
2535{
2536 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2537
2538 spin_lock(&udc->lock);
2539 _nbu2ss_check_vbus(udc);
2540 spin_unlock(&udc->lock);
2541
2542 return IRQ_HANDLED;
2543}
2544
2545/*-------------------------------------------------------------------------*/
2546/* Interrupt (udc) */
2547static irqreturn_t _nbu2ss_udc_irq(int irq, void *_udc)
2548{
2549 u8 suspend_flag = 0;
2550 u32 status;
2551 u32 epnum, int_bit;
2552
2553 struct nbu2ss_udc *udc = (struct nbu2ss_udc *)_udc;
2554 PT_FC_REGS preg = udc->p_regs;
2555
2556 if (gpio_get_value(VBUS_VALUE) == 0) {
2557 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2558 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2559 return IRQ_HANDLED;
2560 }
2561
2562 spin_lock(&udc->lock);
2563
2564 for (;;) {
2565 if (gpio_get_value(VBUS_VALUE) == 0) {
2566 _nbu2ss_writel(&preg->USB_INT_STA, ~USB_INT_STA_RW);
2567 _nbu2ss_writel(&preg->USB_INT_ENA, 0);
2568 status = 0;
2569 } else
2570 status = _nbu2ss_readl(&preg->USB_INT_STA);
2571
2572 if (status == 0)
2573 break;
2574
2575 _nbu2ss_writel(&preg->USB_INT_STA, ~(status & USB_INT_STA_RW));
2576
2577 if (status & USB_RST_INT) {
2578 /* USB Reset */
2579 _nbu2ss_int_bus_reset(udc);
2580 }
2581
2582 if (status & RSUM_INT) {
2583 /* Resume */
2584 _nbu2ss_int_usb_resume(udc);
2585 }
2586
2587 if (status & SPND_INT) {
2588 /* Suspend */
2589 suspend_flag = 1;
2590 }
2591
2592 if (status & EPn_INT) {
2593 /* EP INT */
2594 int_bit = status >> 8;
2595
2596 for (epnum = 0; epnum < NUM_ENDPOINTS; epnum++) {
2597
2598 if (0x01 & int_bit)
2599 _nbu2ss_ep_int(udc, epnum);
2600
2601 int_bit >>= 1;
2602
2603 if (int_bit == 0)
2604 break;
2605 }
2606 }
2607 }
2608
2609 if (suspend_flag)
2610 _nbu2ss_int_usb_suspend(udc);
2611
2612 spin_unlock(&udc->lock);
2613
2614 return IRQ_HANDLED;
2615}
2616
2617/*-------------------------------------------------------------------------*/
2618/* usb_ep_ops */
2619static int nbu2ss_ep_enable(
2620 struct usb_ep *_ep,
2621 const struct usb_endpoint_descriptor *desc)
2622{
2623 u8 ep_type;
2624 unsigned long flags;
2625
2626 struct nbu2ss_ep *ep;
2627 struct nbu2ss_udc *udc;
2628
2629 if ((_ep == NULL) || (desc == NULL)) {
2630 ERR(" *** %s, bad param\n", __func__);
2631 return -EINVAL;
2632 }
2633
2634 ep = container_of(_ep, struct nbu2ss_ep, ep);
2635 if ((ep == NULL) || (ep->udc == NULL)) {
2636 ERR(" *** %s, ep == NULL !!\n", __func__);
2637 return -EINVAL;
2638 }
2639
2640 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2641 if ((ep_type == USB_ENDPOINT_XFER_CONTROL)
2642 || (ep_type == USB_ENDPOINT_XFER_ISOC)) {
2643
2644 ERR(" *** %s, bat bmAttributes\n", __func__);
2645 return -EINVAL;
2646 }
2647
2648 udc = ep->udc;
2649 if (udc->vbus_active == 0)
2650 return -ESHUTDOWN;
2651
2652 if ((udc->driver == NULL)
2653 || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
2654
2655 ERR(" *** %s, udc !!\n", __func__);
2656 return -ESHUTDOWN;
2657 }
2658
2659 spin_lock_irqsave(&udc->lock, flags);
2660
2661 ep->desc = desc;
2662 ep->epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2663 ep->direct = desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
2664 ep->ep_type = ep_type;
2665 ep->wedged = 0;
2666 ep->halted = FALSE;
2667 ep->stalled = FALSE;
2668
2669 ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
2670
2671 /* DMA setting */
2672 _nbu2ss_ep_dma_init(udc, ep);
2673
2674 /* Endpoint setting */
2675 _nbu2ss_ep_init(udc, ep);
2676
2677 spin_unlock_irqrestore(&udc->lock, flags);
2678
2679 return 0;
2680}
2681
2682/*-------------------------------------------------------------------------*/
2683static int nbu2ss_ep_disable(struct usb_ep *_ep)
2684{
2685 struct nbu2ss_ep *ep;
2686 struct nbu2ss_udc *udc;
2687 unsigned long flags;
2688
2689 if (_ep == NULL) {
2690 ERR(" *** %s, bad param\n", __func__);
2691 return -EINVAL;
2692 }
2693
2694 ep = container_of(_ep, struct nbu2ss_ep, ep);
2695 if ((ep == NULL) || (ep->udc == NULL)) {
2696 ERR(" *** %s, ep == NULL !!\n", __func__);
2697 return -EINVAL;
2698 }
2699
2700 udc = ep->udc;
2701 if (udc->vbus_active == 0)
2702 return -ESHUTDOWN;
2703
2704 spin_lock_irqsave(&udc->lock, flags);
2705 _nbu2ss_nuke(udc, ep, -EINPROGRESS); /* dequeue request */
2706 spin_unlock_irqrestore(&udc->lock, flags);
2707
2708 return 0;
2709}
2710
2711/*-------------------------------------------------------------------------*/
2712static struct usb_request *nbu2ss_ep_alloc_request(
2713 struct usb_ep *ep,
2714 gfp_t gfp_flags)
2715{
2716 struct nbu2ss_req *req;
2717
2718 req = kzalloc(sizeof(*req), gfp_flags);
2719 if (!req)
2720 return 0;
2721
2722#ifdef USE_DMA
2723 req->req.dma = DMA_ADDR_INVALID;
2724#endif
2725 INIT_LIST_HEAD(&req->queue);
2726
2727 return &req->req;
2728}
2729
2730/*-------------------------------------------------------------------------*/
2731static void nbu2ss_ep_free_request(
2732 struct usb_ep *_ep,
2733 struct usb_request *_req)
2734{
2735 struct nbu2ss_req *req;
2736
2737 if (_req != NULL) {
2738 req = container_of(_req, struct nbu2ss_req, req);
2739
2740 if (req != NULL)
2741 kfree(req);
2742 }
2743}
2744
2745/*-------------------------------------------------------------------------*/
2746static int nbu2ss_ep_queue(
2747 struct usb_ep *_ep,
2748 struct usb_request *_req,
2749 gfp_t gfp_flags)
2750{
2751 struct nbu2ss_req *req;
2752 struct nbu2ss_ep *ep;
2753 struct nbu2ss_udc *udc;
2754 unsigned long flags;
2755 bool bflag;
2756 int result = -EINVAL;
2757
2758 /* catch various bogus parameters */
2759 if ((_ep == NULL) || (_req == NULL)) {
2760 if (_ep == NULL)
2761 ERR("*** %s --- _ep == NULL\n", __func__);
2762
2763 if (_req == NULL)
2764 ERR("*** %s --- _req == NULL\n", __func__);
2765
2766 return -EINVAL;
2767 }
2768
2769 req = container_of(_req, struct nbu2ss_req, req);
2770 if (unlikely
2771 (!_req->complete || !_req->buf
2772 || !list_empty(&req->queue))) {
2773
2774 if (!_req->complete)
2775 ERR("*** %s --- !_req->complete\n", __func__);
2776
2777 if (!_req->buf)
2778 ERR("*** %s --- !_req->buf\n", __func__);
2779
2780 if (!list_empty(&req->queue))
2781 ERR("*** %s --- !list_empty(&req->queue)\n", __func__);
2782
2783 return -EINVAL;
2784 }
2785
2786 ep = container_of(_ep, struct nbu2ss_ep, ep);
2787 udc = ep->udc;
2788
2789/* INFO("=== %s(ep%d), zero=%d\n", __func__, ep->epnum, _req->zero); */
2790
2791 if (udc->vbus_active == 0) {
2792 pr_info("Can't ep_queue (VBUS OFF)\n");
2793 return -ESHUTDOWN;
2794 }
2795
2796 if (unlikely(!udc->driver)) {
2797 ERR("%s, bogus device state %p\n", __func__, udc->driver);
2798 return -ESHUTDOWN;
2799 }
2800
2801 spin_lock_irqsave(&udc->lock, flags);
2802
2803#ifdef USE_DMA
2804 if ((u32)req->req.buf & 0x3)
2805 req->unaligned = TRUE;
2806 else
2807 req->unaligned = FALSE;
2808
2809 if (req->unaligned) {
2810 if (ep->virt_buf == NULL)
2811 ep->virt_buf = (u8 *)dma_alloc_coherent(
2812 NULL, PAGE_SIZE,
2813 &ep->phys_buf, GFP_KERNEL | GFP_DMA);
2814 if (ep->epnum > 0) {
2815 if (ep->direct == USB_DIR_IN)
2816 memcpy(ep->virt_buf, req->req.buf,
2817 req->req.length);
2818 }
2819 }
2820
2821 if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT) &&
2822 (req->req.dma != 0))
2823 _nbu2ss_dma_map_single(udc, ep, req, USB_DIR_OUT);
2824#endif
2825
2826 _req->status = -EINPROGRESS;
2827 _req->actual = 0;
2828
2829 bflag = list_empty(&ep->queue);
2830 list_add_tail(&req->queue, &ep->queue);
2831
2832 if ((bflag != FALSE) && (ep->stalled == FALSE)) {
2833
2834 result = _nbu2ss_start_transfer(udc, ep, req, FALSE);
2835 if (result < 0) {
2836 ERR(" *** %s, result = %d\n", __func__, result);
2837 list_del(&req->queue);
2838 } else if ((ep->epnum > 0) && (ep->direct == USB_DIR_OUT)) {
2839#ifdef USE_DMA
2840 if (req->req.length < 4 &&
2841 req->req.length == req->req.actual)
2842#else
2843 if (req->req.length == req->req.actual)
2844#endif
2845 _nbu2ss_ep_done(ep, req, result);
2846 }
2847 }
2848
2849 spin_unlock_irqrestore(&udc->lock, flags);
2850
2851 return 0;
2852}
2853
2854/*-------------------------------------------------------------------------*/
2855static int nbu2ss_ep_dequeue(
2856 struct usb_ep *_ep,
2857 struct usb_request *_req)
2858{
2859 struct nbu2ss_req *req;
2860 struct nbu2ss_ep *ep;
2861 struct nbu2ss_udc *udc;
2862 unsigned long flags;
2863
2864 /*INFO("=== %s()\n", __func__);*/
2865
2866 /* catch various bogus parameters */
2867 if ((_ep == NULL) || (_req == NULL)) {
2868 /* ERR("%s, bad param(1)\n", __func__); */
2869 return -EINVAL;
2870 }
2871
2872 ep = container_of(_ep, struct nbu2ss_ep, ep);
2873 if (!ep) {
2874 ERR("%s, ep == NULL !!\n", __func__);
2875 return -EINVAL;
2876 }
2877
2878 udc = ep->udc;
2879 if (udc == NULL)
2880 return -EINVAL;
2881
2882 spin_lock_irqsave(&udc->lock, flags);
2883
2884 /* make sure it's actually queued on this endpoint */
2885 list_for_each_entry(req, &ep->queue, queue) {
2886 if (&req->req == _req)
2887 break;
2888 }
2889 if (&req->req != _req) {
2890 spin_unlock_irqrestore(&udc->lock, flags);
2891 pr_debug("%s no queue(EINVAL)\n", __func__);
2892 return -EINVAL;
2893 }
2894
2895 _nbu2ss_ep_done(ep, req, -ECONNRESET);
2896
2897 spin_unlock_irqrestore(&udc->lock, flags);
2898
2899 return 0;
2900}
2901
2902/*-------------------------------------------------------------------------*/
2903static int nbu2ss_ep_set_halt(struct usb_ep *_ep, int value)
2904{
2905 u8 ep_adrs;
2906 unsigned long flags;
2907
2908 struct nbu2ss_ep *ep;
2909 struct nbu2ss_udc *udc;
2910
2911/* INFO("=== %s()\n", __func__); */
2912
2913 if (!_ep) {
2914 ERR("%s, bad param\n", __func__);
2915 return -EINVAL;
2916 }
2917
2918 ep = container_of(_ep, struct nbu2ss_ep, ep);
2919 if (!ep) {
2920 ERR("%s, bad ep\n", __func__);
2921 return -EINVAL;
2922 }
2923
2924 udc = ep->udc;
2925 if (!udc) {
2926 ERR(" *** %s, bad udc\n", __func__);
2927 return -EINVAL;
2928 }
2929
2930 spin_lock_irqsave(&udc->lock, flags);
2931
2932 ep_adrs = ep->epnum | ep->direct;
2933 if (value == 0) {
2934 _nbu2ss_set_endpoint_stall(udc, ep_adrs, value);
2935 ep->stalled = FALSE;
2936 } else {
2937 if (list_empty(&ep->queue))
2938 _nbu2ss_epn_set_stall(udc, ep);
2939 else
2940 ep->stalled = TRUE;
2941 }
2942
2943 if (value == 0)
2944 ep->wedged = 0;
2945
2946 spin_unlock_irqrestore(&udc->lock, flags);
2947
2948 return 0;
2949}
2950
2951static int nbu2ss_ep_set_wedge(struct usb_ep *_ep)
2952{
2953 return nbu2ss_ep_set_halt(_ep, 1);
2954}
2955
2956/*-------------------------------------------------------------------------*/
2957static int nbu2ss_ep_fifo_status(struct usb_ep *_ep)
2958{
2959 u32 data;
2960 struct nbu2ss_ep *ep;
2961 struct nbu2ss_udc *udc;
2962 unsigned long flags;
2963 PT_FC_REGS preg;
2964
2965/* INFO("=== %s()\n", __func__); */
2966
2967 if (!_ep) {
2968 ERR("%s, bad param\n", __func__);
2969 return -EINVAL;
2970 }
2971
2972 ep = container_of(_ep, struct nbu2ss_ep, ep);
2973 if (!ep) {
2974 ERR("%s, bad ep\n", __func__);
2975 return -EINVAL;
2976 }
2977
2978 udc = ep->udc;
2979 if (!udc) {
2980 ERR("%s, bad udc\n", __func__);
2981 return -EINVAL;
2982 }
2983
2984 preg = udc->p_regs;
2985
2986 data = gpio_get_value(VBUS_VALUE);
2987 if (data == 0)
2988 return -EINVAL;
2989
2990 spin_lock_irqsave(&udc->lock, flags);
2991
2992 if (ep->epnum == 0) {
2993 data = _nbu2ss_readl(&preg->EP0_LENGTH) & EP0_LDATA;
2994
2995 } else {
2996 data = _nbu2ss_readl(&preg->EP_REGS[ep->epnum-1].EP_LEN_DCNT)
2997 & EPn_LDATA;
2998 }
2999
3000 spin_unlock_irqrestore(&udc->lock, flags);
3001
3002 return 0;
3003}
3004
3005/*-------------------------------------------------------------------------*/
3006static void nbu2ss_ep_fifo_flush(struct usb_ep *_ep)
3007{
3008 u32 data;
3009 struct nbu2ss_ep *ep;
3010 struct nbu2ss_udc *udc;
3011 unsigned long flags;
3012
3013/* INFO("=== %s()\n", __func__); */
3014
3015 if (!_ep) {
3016 ERR("%s, bad param\n", __func__);
3017 return;
3018 }
3019
3020 ep = container_of(_ep, struct nbu2ss_ep, ep);
3021 if (!_ep) {
3022 ERR("%s, bad ep\n", __func__);
3023 return;
3024 }
3025
3026 udc = ep->udc;
3027 if (!udc) {
3028 ERR("%s, bad udc\n", __func__);
3029 return;
3030 }
3031
3032 data = gpio_get_value(VBUS_VALUE);
3033 if (data == 0)
3034 return;
3035
3036 spin_lock_irqsave(&udc->lock, flags);
3037 _nbu2ss_fifo_flush(udc, ep);
3038 spin_unlock_irqrestore(&udc->lock, flags);
3039}
3040
3041/*-------------------------------------------------------------------------*/
3042static struct usb_ep_ops nbu2ss_ep_ops = {
3043 .enable = nbu2ss_ep_enable,
3044 .disable = nbu2ss_ep_disable,
3045
3046 .alloc_request = nbu2ss_ep_alloc_request,
3047 .free_request = nbu2ss_ep_free_request,
3048
3049 .queue = nbu2ss_ep_queue,
3050 .dequeue = nbu2ss_ep_dequeue,
3051
3052 .set_halt = nbu2ss_ep_set_halt,
3053 .set_wedge = nbu2ss_ep_set_wedge,
3054
3055 .fifo_status = nbu2ss_ep_fifo_status,
3056 .fifo_flush = nbu2ss_ep_fifo_flush,
3057};
3058
3059
3060/*-------------------------------------------------------------------------*/
3061/* usb_gadget_ops */
3062
3063/*-------------------------------------------------------------------------*/
3064static int nbu2ss_gad_get_frame(struct usb_gadget *pgadget)
3065{
3066 u32 data;
3067 struct nbu2ss_udc *udc;
3068
3069/* INFO("=== %s()\n", __func__); */
3070
3071 if (pgadget == NULL) {
3072 ERR("%s, bad param\n", __func__);
3073 return -EINVAL;
3074 }
3075
3076 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3077 if (udc == NULL) {
3078 ERR("%s, udc == NULL\n", __func__);
3079 return -EINVAL;
3080 }
3081
3082 data = gpio_get_value(VBUS_VALUE);
3083 if (data == 0)
3084 return -EINVAL;
3085
3086 data = _nbu2ss_readl(&udc->p_regs->USB_ADDRESS) & FRAME;
3087
3088 return data;
3089}
3090
3091/*-------------------------------------------------------------------------*/
3092static int nbu2ss_gad_wakeup(struct usb_gadget *pgadget)
3093{
3094 int i;
3095 u32 data;
3096
3097 struct nbu2ss_udc *udc;
3098
3099/* INFO("=== %s()\n", __func__); */
3100
3101 if (pgadget == NULL) {
3102 ERR("%s, bad param\n", __func__);
3103 return -EINVAL;
3104 }
3105
3106 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3107 if (udc == NULL) {
3108 ERR("%s, udc == NULL\n", __func__);
3109 return -EINVAL;
3110 }
3111
3112 data = gpio_get_value(VBUS_VALUE);
3113 if (data == 0) {
3114 pr_warning("VBUS LEVEL = %d\n", data);
3115 return -EINVAL;
3116 }
3117
3118 _nbu2ss_bitset(&udc->p_regs->EPCTR, PLL_RESUME);
3119
3120 for (i = 0; i < EPC_PLL_LOCK_COUNT; i++) {
3121 data = _nbu2ss_readl(&udc->p_regs->EPCTR);
3122
3123 if (data & PLL_LOCK)
3124 break;
3125 }
3126
3127 _nbu2ss_bitclr(&udc->p_regs->EPCTR, PLL_RESUME);
3128
3129 return 0;
3130}
3131
3132/*-------------------------------------------------------------------------*/
3133static int nbu2ss_gad_set_selfpowered(struct usb_gadget *pgadget,
3134 int is_selfpowered)
3135{
3136 struct nbu2ss_udc *udc;
3137 unsigned long flags;
3138
3139/* INFO("=== %s()\n", __func__); */
3140
3141 if (pgadget == NULL) {
3142 ERR("%s, bad param\n", __func__);
3143 return -EINVAL;
3144 }
3145
3146 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3147
3148 spin_lock_irqsave(&udc->lock, flags);
3149 udc->self_powered = (is_selfpowered != 0);
3150 spin_unlock_irqrestore(&udc->lock, flags);
3151
3152 return 0;
3153}
3154
3155/*-------------------------------------------------------------------------*/
3156static int nbu2ss_gad_vbus_session(struct usb_gadget *pgadget, int is_active)
3157{
3158/* INFO("=== %s()\n", __func__); */
3159 return 0;
3160}
3161
3162/*-------------------------------------------------------------------------*/
3163static int nbu2ss_gad_vbus_draw(struct usb_gadget *pgadget, unsigned mA)
3164{
3165 struct nbu2ss_udc *udc;
3166 unsigned long flags;
3167
3168/* INFO("=== %s()\n", __func__); */
3169
3170 if (pgadget == NULL) {
3171 ERR("%s, bad param\n", __func__);
3172 return -EINVAL;
3173 }
3174
3175 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3176
3177 spin_lock_irqsave(&udc->lock, flags);
3178 udc->mA = mA;
3179 spin_unlock_irqrestore(&udc->lock, flags);
3180
3181 return 0;
3182}
3183
3184/*-------------------------------------------------------------------------*/
3185static int nbu2ss_gad_pullup(struct usb_gadget *pgadget, int is_on)
3186{
3187 struct nbu2ss_udc *udc;
3188 unsigned long flags;
3189
3190/* INFO("=== %s()\n", __func__); */
3191
3192 if (pgadget == NULL) {
3193 ERR("%s, bad param\n", __func__);
3194 return -EINVAL;
3195 }
3196
3197 udc = container_of(pgadget, struct nbu2ss_udc, gadget);
3198
3199 if (udc->driver == NULL) {
3200 pr_warning("%s, Not Regist Driver\n", __func__);
3201 return -EINVAL;
3202 }
3203
3204 if (udc->vbus_active == 0)
3205 return -ESHUTDOWN;
3206
3207 spin_lock_irqsave(&udc->lock, flags);
3208 _nbu2ss_pullup(udc, is_on);
3209 spin_unlock_irqrestore(&udc->lock, flags);
3210
3211 return 0;
3212}
3213
3214/*-------------------------------------------------------------------------*/
3215static int nbu2ss_gad_ioctl(
3216 struct usb_gadget *pgadget,
3217 unsigned code,
3218 unsigned long param)
3219{
3220/* INFO("=== %s()\n", __func__); */
3221 return 0;
3222}
3223
3224
3225static const struct usb_gadget_ops nbu2ss_gadget_ops = {
3226 .get_frame = nbu2ss_gad_get_frame,
3227 .wakeup = nbu2ss_gad_wakeup,
3228 .set_selfpowered = nbu2ss_gad_set_selfpowered,
3229 .vbus_session = nbu2ss_gad_vbus_session,
3230 .vbus_draw = nbu2ss_gad_vbus_draw,
3231 .pullup = nbu2ss_gad_pullup,
3232 .ioctl = nbu2ss_gad_ioctl,
3233};
3234
3235static char g_ep0_name[] = "ep0";
3236static char g_ep1_name[] = "ep1-bulk";
3237static char g_ep2_name[] = "ep2-bulk";
3238static char g_ep3_name[] = "ep3in-int";
3239static char g_ep4_name[] = "ep4-iso";
3240static char g_ep5_name[] = "ep5-iso";
3241static char g_ep6_name[] = "ep6-bulk";
3242static char g_ep7_name[] = "ep7-bulk";
3243static char g_ep8_name[] = "ep8in-int";
3244static char g_ep9_name[] = "ep9-iso";
3245static char g_epa_name[] = "epa-iso";
3246static char g_epb_name[] = "epb-bulk";
3247static char g_epc_name[] = "epc-nulk";
3248static char g_epd_name[] = "epdin-int";
3249
3250static char *gp_ep_name[NUM_ENDPOINTS] = {
3251 g_ep0_name,
3252 g_ep1_name,
3253 g_ep2_name,
3254 g_ep3_name,
3255 g_ep4_name,
3256 g_ep5_name,
3257 g_ep6_name,
3258 g_ep7_name,
3259 g_ep8_name,
3260 g_ep9_name,
3261 g_epa_name,
3262 g_epb_name,
3263 g_epc_name,
3264 g_epd_name,
3265};
3266
3267/*-------------------------------------------------------------------------*/
3268static void __init nbu2ss_drv_set_ep_info(
3269 struct nbu2ss_udc *udc,
3270 struct nbu2ss_ep *ep,
3271 u8 *name)
3272{
3273 ep->udc = udc;
3274 ep->desc = NULL;
3275
3276 ep->ep.driver_data = NULL;
3277 ep->ep.name = name;
3278 ep->ep.ops = &nbu2ss_ep_ops;
3279
3280 if (isdigit(name[2])) {
3281
3282 long num;
3283 int res;
3284 char tempbuf[2];
3285
3286 tempbuf[0] = name[2];
3287 tempbuf[1] = '\0';
Sachin Kamat6de2a1a2014-06-30 13:46:40 +05303288 res = kstrtol(tempbuf, 16, &num);
Magnus Damm33aa8d42014-06-06 19:44:17 +09003289
3290 if (num == 0)
3291 ep->ep.maxpacket = EP0_PACKETSIZE;
3292 else
3293 ep->ep.maxpacket = EP_PACKETSIZE;
3294
3295 } else {
3296 ep->ep.maxpacket = EP_PACKETSIZE;
3297 }
3298
3299 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
3300 INIT_LIST_HEAD(&ep->queue);
3301}
3302
3303/*-------------------------------------------------------------------------*/
3304static void __init nbu2ss_drv_ep_init(struct nbu2ss_udc *udc)
3305{
3306 int i;
3307
3308 INIT_LIST_HEAD(&udc->gadget.ep_list);
3309 udc->gadget.ep0 = &udc->ep[0].ep;
3310
3311
3312 for (i = 0; i < NUM_ENDPOINTS; i++)
3313 nbu2ss_drv_set_ep_info(udc, &udc->ep[i], gp_ep_name[i]);
3314
3315 list_del_init(&udc->ep[0].ep.ep_list);
3316}
3317
3318/*-------------------------------------------------------------------------*/
3319/* platform_driver */
3320static int __init nbu2ss_drv_contest_init(
3321 struct platform_device *pdev,
3322 struct nbu2ss_udc *udc)
3323{
3324 spin_lock_init(&udc->lock);
3325 udc->dev = &pdev->dev;
3326
3327 udc->self_powered = 1;
3328 udc->devstate = USB_STATE_NOTATTACHED;
3329 udc->pdev = pdev;
3330 udc->mA = 0;
3331
3332 udc->pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3333
3334 /* init Endpoint */
3335 nbu2ss_drv_ep_init(udc);
3336
3337 /* init Gadget */
3338 udc->gadget.ops = &nbu2ss_gadget_ops;
3339 udc->gadget.ep0 = &udc->ep[0].ep;
3340 udc->gadget.speed = USB_SPEED_UNKNOWN;
3341 udc->gadget.name = driver_name;
KANG Yuxuana2c14e92014-07-16 10:45:01 +08003342 /* udc->gadget.is_dualspeed = 1; */
Magnus Damm33aa8d42014-06-06 19:44:17 +09003343
3344 device_initialize(&udc->gadget.dev);
3345
3346 dev_set_name(&udc->gadget.dev, "gadget");
3347 udc->gadget.dev.parent = &pdev->dev;
3348 udc->gadget.dev.dma_mask = pdev->dev.dma_mask;
3349
3350 return 0;
3351}
3352
3353/*
3354 * probe - binds to the platform device
3355 */
3356static int nbu2ss_drv_probe(struct platform_device *pdev)
3357{
3358 int status = -ENODEV;
3359 struct nbu2ss_udc *udc;
Magnus Damm96b29ca2014-06-06 19:44:26 +09003360 struct resource *r;
3361 int irq;
3362 void __iomem *mmio_base;
Magnus Damm33aa8d42014-06-06 19:44:17 +09003363
3364 udc = &udc_controller;
3365 memset(udc, 0, sizeof(struct nbu2ss_udc));
3366
3367 platform_set_drvdata(pdev, udc);
3368
Magnus Damm96b29ca2014-06-06 19:44:26 +09003369 /* require I/O memory and IRQ to be provided as resources */
3370 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sachin Kamata790ebc2014-06-23 11:43:08 +05303371 mmio_base = devm_ioremap_resource(&pdev->dev, r);
3372 if (IS_ERR(mmio_base))
Magnus Damm96b29ca2014-06-06 19:44:26 +09003373 return PTR_ERR(mmio_base);
Magnus Damm33aa8d42014-06-06 19:44:17 +09003374
Magnus Damm96b29ca2014-06-06 19:44:26 +09003375 irq = platform_get_irq(pdev, 0);
3376 if (irq < 0) {
3377 dev_err(&pdev->dev, "failed to get IRQ\n");
3378 return irq;
3379 }
3380 status = devm_request_irq(&pdev->dev, irq, _nbu2ss_udc_irq,
3381 0, driver_name, udc);
3382
Magnus Damm33aa8d42014-06-06 19:44:17 +09003383 /* IO Memory */
Magnus Damm96b29ca2014-06-06 19:44:26 +09003384 udc->p_regs = (PT_FC_REGS)mmio_base;
Magnus Damm33aa8d42014-06-06 19:44:17 +09003385
3386 /* USB Function Controller Interrupt */
Magnus Damm33aa8d42014-06-06 19:44:17 +09003387 if (status != 0) {
3388 ERR("request_irq(USB_UDC_IRQ_1) failed\n");
Magnus Damm96b29ca2014-06-06 19:44:26 +09003389 goto cleanup1;
Magnus Damm33aa8d42014-06-06 19:44:17 +09003390 }
3391
3392 /* Driver Initialization */
3393 status = nbu2ss_drv_contest_init(pdev, udc);
3394 if (status < 0) {
3395 /* Error */
3396 goto cleanup1;
3397 }
3398
3399 /* VBUS Interrupt */
3400 irq_set_irq_type(INT_VBUS, IRQ_TYPE_EDGE_BOTH);
3401 status = request_irq(INT_VBUS,
3402 _nbu2ss_vbus_irq,
3403 IRQF_SHARED,
3404 driver_name,
3405 udc);
3406
3407 if (status != 0) {
3408 ERR("request_irq(INT_VBUS) failed\n");
3409 goto cleanup1;
3410 }
3411
3412 return status;
3413
3414cleanup1:
Magnus Damm33aa8d42014-06-06 19:44:17 +09003415 return status;
3416}
3417
3418/*-------------------------------------------------------------------------*/
3419static void nbu2ss_drv_shutdown(struct platform_device *pdev)
3420{
3421 struct nbu2ss_udc *udc;
3422
3423 udc = platform_get_drvdata(pdev);
3424 if (udc == NULL)
3425 return;
3426
3427 _nbu2ss_disable_controller(udc);
3428}
3429
3430/*-------------------------------------------------------------------------*/
3431static int __exit nbu2ss_drv_remove(struct platform_device *pdev)
3432{
3433 struct nbu2ss_udc *udc;
3434 struct nbu2ss_ep *ep;
3435 int i;
3436
3437 udc = &udc_controller;
3438
3439 for (i = 0; i < NUM_ENDPOINTS; i++) {
3440 ep = &udc->ep[i];
3441 if (ep->virt_buf)
3442 dma_free_coherent(NULL, PAGE_SIZE,
3443 (void *)ep->virt_buf, ep->phys_buf);
3444 }
3445
3446 /* Interrupt Handler - Release */
Magnus Damm33aa8d42014-06-06 19:44:17 +09003447 free_irq(INT_VBUS, udc);
3448
Magnus Damm33aa8d42014-06-06 19:44:17 +09003449 return 0;
3450}
3451
3452/*-------------------------------------------------------------------------*/
3453static int nbu2ss_drv_suspend(struct platform_device *pdev, pm_message_t state)
3454{
3455 struct nbu2ss_udc *udc;
3456
3457 udc = platform_get_drvdata(pdev);
3458 if (udc == NULL)
3459 return 0;
3460
3461 if (udc->vbus_active) {
3462 udc->vbus_active = 0;
3463 udc->devstate = USB_STATE_NOTATTACHED;
3464 udc->linux_suspended = 1;
3465
3466 if (udc->usb_suspended) {
3467 udc->usb_suspended = 0;
3468 _nbu2ss_reset_controller(udc);
3469 }
3470
3471 _nbu2ss_quiesce(udc);
3472 }
3473 _nbu2ss_disable_controller(udc);
3474
3475 return 0;
3476}
3477
3478/*-------------------------------------------------------------------------*/
3479static int nbu2ss_drv_resume(struct platform_device *pdev)
3480{
3481 u32 data;
3482 struct nbu2ss_udc *udc;
3483
3484 udc = platform_get_drvdata(pdev);
3485 if (udc == NULL)
3486 return 0;
3487
3488 data = gpio_get_value(VBUS_VALUE);
3489 if (data) {
3490 udc->vbus_active = 1;
3491 udc->devstate = USB_STATE_POWERED;
3492 _nbu2ss_enable_controller(udc);
3493 _nbu2ss_pullup(udc, 1);
3494 }
3495
3496 udc->linux_suspended = 0;
3497
3498 return 0;
3499}
3500
3501
3502static struct platform_driver udc_driver = {
3503 .probe = nbu2ss_drv_probe,
3504 .shutdown = nbu2ss_drv_shutdown,
3505 .remove = __exit_p(nbu2ss_drv_remove),
3506 .suspend = nbu2ss_drv_suspend,
3507 .resume = nbu2ss_drv_resume,
3508 .driver = {
3509 .owner = THIS_MODULE,
3510 .name = driver_name,
3511 },
3512};
3513
Sachin Kamat464cad22014-06-23 11:55:32 +05303514module_platform_driver(udc_driver);
Magnus Damm33aa8d42014-06-06 19:44:17 +09003515
3516MODULE_DESCRIPTION(DRIVER_DESC);
3517MODULE_AUTHOR("Renesas Electronics Corporation");
3518MODULE_LICENSE("GPL");
3519
3520