Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

staging: emxx_udc: Add Emma Mobile USB Gadget driver

Add the emxx_udc driver to staging based on an old linux-2.6.35.7
android tree. The driver has been brushed up slightly to complile
but it is still in great need of cleanup.

At this point DT bindings are clearly lacking and I doubt that the
driver even can run with multiple instances (global variables, hurray!).

Signed-off-by: Magnus Damm <damm+renesas@opensource.se>
Acked-by: Simon Horman <horms+renesas@verge.net.au>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Magnus Damm and committed by
Greg Kroah-Hartman
33aa8d45 22b53714

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