1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (c) 2021 Aspeed Technology Inc. 4 */ 5 6 #include <linux/clk.h> 7 #include <linux/delay.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/interrupt.h> 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/platform_device.h> 14 #include <linux/prefetch.h> 15 #include <linux/usb/ch9.h> 16 #include <linux/usb/gadget.h> 17 #include <linux/slab.h> 18 19 #define AST_UDC_NUM_ENDPOINTS (1 + 4) 20 #define AST_UDC_EP0_MAX_PACKET 64 /* EP0's max packet size */ 21 #define AST_UDC_EPn_MAX_PACKET 1024 /* Generic EPs max packet size */ 22 #define AST_UDC_DESCS_COUNT 256 /* Use 256 stages descriptor mode (32/256) */ 23 #define AST_UDC_DESC_MODE 1 /* Single/Multiple Stage(s) Descriptor Mode */ 24 25 #define AST_UDC_EP_DMA_SIZE (AST_UDC_EPn_MAX_PACKET + 8 * AST_UDC_DESCS_COUNT) 26 27 /***************************** 28 * * 29 * UDC register definitions * 30 * * 31 *****************************/ 32 33 #define AST_UDC_FUNC_CTRL 0x00 /* Root Function Control & Status Register */ 34 #define AST_UDC_CONFIG 0x04 /* Root Configuration Setting Register */ 35 #define AST_UDC_IER 0x08 /* Interrupt Control Register */ 36 #define AST_UDC_ISR 0x0C /* Interrupt Status Register */ 37 #define AST_UDC_EP_ACK_IER 0x10 /* Programmable ep Pool ACK Interrupt Enable Reg */ 38 #define AST_UDC_EP_NAK_IER 0x14 /* Programmable ep Pool NAK Interrupt Enable Reg */ 39 #define AST_UDC_EP_ACK_ISR 0x18 /* Programmable ep Pool ACK Interrupt Status Reg */ 40 #define AST_UDC_EP_NAK_ISR 0x1C /* Programmable ep Pool NAK Interrupt Status Reg */ 41 #define AST_UDC_DEV_RESET 0x20 /* Device Controller Soft Reset Enable Register */ 42 #define AST_UDC_STS 0x24 /* USB Status Register */ 43 #define AST_VHUB_EP_DATA 0x28 /* Programmable ep Pool Data Toggle Value Set */ 44 #define AST_VHUB_ISO_TX_FAIL 0x2C /* Isochronous Transaction Fail Accumulator */ 45 #define AST_UDC_EP0_CTRL 0x30 /* Endpoint 0 Control/Status Register */ 46 #define AST_UDC_EP0_DATA_BUFF 0x34 /* Base Address of ep0 IN/OUT Data Buffer Reg */ 47 #define AST_UDC_SETUP0 0x80 /* Root Device Setup Data Buffer0 */ 48 #define AST_UDC_SETUP1 0x84 /* Root Device Setup Data Buffer1 */ 49 50 51 /* Main control reg */ 52 #define USB_PHY_CLK_EN BIT(31) 53 #define USB_FIFO_DYN_PWRD_EN BIT(19) 54 #define USB_EP_LONG_DESC BIT(18) 55 #define USB_BIST_TEST_PASS BIT(13) 56 #define USB_BIST_TURN_ON BIT(12) 57 #define USB_PHY_RESET_DIS BIT(11) 58 #define USB_TEST_MODE(x) ((x) << 8) 59 #define USB_FORCE_TIMER_HS BIT(7) 60 #define USB_FORCE_HS BIT(6) 61 #define USB_REMOTE_WAKEUP_12MS BIT(5) 62 #define USB_REMOTE_WAKEUP_EN BIT(4) 63 #define USB_AUTO_REMOTE_WAKEUP_EN BIT(3) 64 #define USB_STOP_CLK_IN_SUPEND BIT(2) 65 #define USB_UPSTREAM_FS BIT(1) 66 #define USB_UPSTREAM_EN BIT(0) 67 68 /* Main config reg */ 69 #define UDC_CFG_SET_ADDR(x) ((x) & 0x3f) 70 #define UDC_CFG_ADDR_MASK (0x3f) 71 72 /* Interrupt ctrl & status reg */ 73 #define UDC_IRQ_EP_POOL_NAK BIT(17) 74 #define UDC_IRQ_EP_POOL_ACK_STALL BIT(16) 75 #define UDC_IRQ_BUS_RESUME BIT(8) 76 #define UDC_IRQ_BUS_SUSPEND BIT(7) 77 #define UDC_IRQ_BUS_RESET BIT(6) 78 #define UDC_IRQ_EP0_IN_DATA_NAK BIT(4) 79 #define UDC_IRQ_EP0_IN_ACK_STALL BIT(3) 80 #define UDC_IRQ_EP0_OUT_NAK BIT(2) 81 #define UDC_IRQ_EP0_OUT_ACK_STALL BIT(1) 82 #define UDC_IRQ_EP0_SETUP BIT(0) 83 #define UDC_IRQ_ACK_ALL (0x1ff) 84 85 /* EP isr reg */ 86 #define USB_EP3_ISR BIT(3) 87 #define USB_EP2_ISR BIT(2) 88 #define USB_EP1_ISR BIT(1) 89 #define USB_EP0_ISR BIT(0) 90 #define UDC_IRQ_EP_ACK_ALL (0xf) 91 92 /*Soft reset reg */ 93 #define ROOT_UDC_SOFT_RESET BIT(0) 94 95 /* USB status reg */ 96 #define UDC_STS_HIGHSPEED BIT(27) 97 98 /* Programmable EP data toggle */ 99 #define EP_TOGGLE_SET_EPNUM(x) ((x) & 0x3) 100 101 /* EP0 ctrl reg */ 102 #define EP0_GET_RX_LEN(x) ((x >> 16) & 0x7f) 103 #define EP0_TX_LEN(x) ((x & 0x7f) << 8) 104 #define EP0_RX_BUFF_RDY BIT(2) 105 #define EP0_TX_BUFF_RDY BIT(1) 106 #define EP0_STALL BIT(0) 107 108 /************************************* 109 * * 110 * per-endpoint register definitions * 111 * * 112 *************************************/ 113 114 #define AST_UDC_EP_CONFIG 0x00 /* Endpoint Configuration Register */ 115 #define AST_UDC_EP_DMA_CTRL 0x04 /* DMA Descriptor List Control/Status Register */ 116 #define AST_UDC_EP_DMA_BUFF 0x08 /* DMA Descriptor/Buffer Base Address */ 117 #define AST_UDC_EP_DMA_STS 0x0C /* DMA Descriptor List R/W Pointer and Status */ 118 119 #define AST_UDC_EP_BASE 0x200 120 #define AST_UDC_EP_OFFSET 0x10 121 122 /* EP config reg */ 123 #define EP_SET_MAX_PKT(x) ((x & 0x3ff) << 16) 124 #define EP_DATA_FETCH_CTRL(x) ((x & 0x3) << 14) 125 #define EP_AUTO_DATA_DISABLE (0x1 << 13) 126 #define EP_SET_EP_STALL (0x1 << 12) 127 #define EP_SET_EP_NUM(x) ((x & 0xf) << 8) 128 #define EP_SET_TYPE_MASK(x) ((x) << 5) 129 #define EP_TYPE_BULK (0x1) 130 #define EP_TYPE_INT (0x2) 131 #define EP_TYPE_ISO (0x3) 132 #define EP_DIR_OUT (0x1 << 4) 133 #define EP_ALLOCATED_MASK (0x7 << 1) 134 #define EP_ENABLE BIT(0) 135 136 /* EP DMA ctrl reg */ 137 #define EP_DMA_CTRL_GET_PROC_STS(x) ((x >> 4) & 0xf) 138 #define EP_DMA_CTRL_STS_RX_IDLE 0x0 139 #define EP_DMA_CTRL_STS_TX_IDLE 0x8 140 #define EP_DMA_CTRL_IN_LONG_MODE (0x1 << 3) 141 #define EP_DMA_CTRL_RESET (0x1 << 2) 142 #define EP_DMA_SINGLE_STAGE (0x1 << 1) 143 #define EP_DMA_DESC_MODE (0x1 << 0) 144 145 /* EP DMA status reg */ 146 #define EP_DMA_SET_TX_SIZE(x) ((x & 0x7ff) << 16) 147 #define EP_DMA_GET_TX_SIZE(x) (((x) >> 16) & 0x7ff) 148 #define EP_DMA_GET_RPTR(x) (((x) >> 8) & 0xff) 149 #define EP_DMA_GET_WPTR(x) ((x) & 0xff) 150 #define EP_DMA_SINGLE_KICK (1 << 0) /* WPTR = 1 for single mode */ 151 152 /* EP desc reg */ 153 #define AST_EP_DMA_DESC_INTR_ENABLE BIT(31) 154 #define AST_EP_DMA_DESC_PID_DATA0 (0 << 14) 155 #define AST_EP_DMA_DESC_PID_DATA2 BIT(14) 156 #define AST_EP_DMA_DESC_PID_DATA1 (2 << 14) 157 #define AST_EP_DMA_DESC_PID_MDATA (3 << 14) 158 #define EP_DESC1_IN_LEN(x) ((x) & 0x1fff) 159 #define AST_EP_DMA_DESC_MAX_LEN (7680) /* Max packet length for trasmit in 1 desc */ 160 161 struct ast_udc_request { 162 struct usb_request req; 163 struct list_head queue; 164 unsigned mapped:1; 165 unsigned int actual_dma_length; 166 u32 saved_dma_wptr; 167 }; 168 169 #define to_ast_req(__req) container_of(__req, struct ast_udc_request, req) 170 171 struct ast_dma_desc { 172 u32 des_0; 173 u32 des_1; 174 }; 175 176 struct ast_udc_ep { 177 struct usb_ep ep; 178 179 /* Request queue */ 180 struct list_head queue; 181 182 struct ast_udc_dev *udc; 183 void __iomem *ep_reg; 184 void *epn_buf; 185 dma_addr_t epn_buf_dma; 186 const struct usb_endpoint_descriptor *desc; 187 188 /* DMA Descriptors */ 189 struct ast_dma_desc *descs; 190 dma_addr_t descs_dma; 191 u32 descs_wptr; 192 u32 chunk_max; 193 194 bool dir_in:1; 195 unsigned stopped:1; 196 bool desc_mode:1; 197 }; 198 199 #define to_ast_ep(__ep) container_of(__ep, struct ast_udc_ep, ep) 200 201 struct ast_udc_dev { 202 struct platform_device *pdev; 203 void __iomem *reg; 204 int irq; 205 spinlock_t lock; 206 struct clk *clk; 207 struct work_struct wake_work; 208 209 /* EP0 DMA buffers allocated in one chunk */ 210 void *ep0_buf; 211 dma_addr_t ep0_buf_dma; 212 struct ast_udc_ep ep[AST_UDC_NUM_ENDPOINTS]; 213 214 struct usb_gadget gadget; 215 struct usb_gadget_driver *driver; 216 void __iomem *creq; 217 enum usb_device_state suspended_from; 218 int desc_mode; 219 220 /* Force full speed only */ 221 bool force_usb1:1; 222 unsigned is_control_tx:1; 223 bool wakeup_en:1; 224 }; 225 226 #define to_ast_dev(__g) container_of(__g, struct ast_udc_dev, gadget) 227 228 static const char * const ast_ep_name[] = { 229 "ep0", "ep1", "ep2", "ep3", "ep4" 230 }; 231 232 #ifdef AST_UDC_DEBUG_ALL 233 #define AST_UDC_DEBUG 234 #define AST_SETUP_DEBUG 235 #define AST_EP_DEBUG 236 #define AST_ISR_DEBUG 237 #endif 238 239 #ifdef AST_SETUP_DEBUG 240 #define SETUP_DBG(u, fmt, ...) \ 241 dev_dbg(&(u)->pdev->dev, "%s() " fmt, __func__, ##__VA_ARGS__) 242 #else 243 #define SETUP_DBG(u, fmt, ...) 244 #endif 245 246 #ifdef AST_EP_DEBUG 247 #define EP_DBG(e, fmt, ...) \ 248 dev_dbg(&(e)->udc->pdev->dev, "%s():%s " fmt, __func__, \ 249 (e)->ep.name, ##__VA_ARGS__) 250 #else 251 #define EP_DBG(ep, fmt, ...) ((void)(ep)) 252 #endif 253 254 #ifdef AST_UDC_DEBUG 255 #define UDC_DBG(u, fmt, ...) \ 256 dev_dbg(&(u)->pdev->dev, "%s() " fmt, __func__, ##__VA_ARGS__) 257 #else 258 #define UDC_DBG(u, fmt, ...) 259 #endif 260 261 #ifdef AST_ISR_DEBUG 262 #define ISR_DBG(u, fmt, ...) \ 263 dev_dbg(&(u)->pdev->dev, "%s() " fmt, __func__, ##__VA_ARGS__) 264 #else 265 #define ISR_DBG(u, fmt, ...) 266 #endif 267 268 /*-------------------------------------------------------------------------*/ 269 #define ast_udc_read(udc, offset) \ 270 readl((udc)->reg + (offset)) 271 #define ast_udc_write(udc, val, offset) \ 272 writel((val), (udc)->reg + (offset)) 273 274 #define ast_ep_read(ep, reg) \ 275 readl((ep)->ep_reg + (reg)) 276 #define ast_ep_write(ep, val, reg) \ 277 writel((val), (ep)->ep_reg + (reg)) 278 279 /*-------------------------------------------------------------------------*/ 280 281 static void ast_udc_done(struct ast_udc_ep *ep, struct ast_udc_request *req, 282 int status) 283 { 284 struct ast_udc_dev *udc = ep->udc; 285 286 EP_DBG(ep, "req @%p, len (%d/%d), buf:0x%x, dir:0x%x\n", 287 req, req->req.actual, req->req.length, 288 (u32)req->req.buf, ep->dir_in); 289 290 list_del(&req->queue); 291 292 if (req->req.status == -EINPROGRESS) 293 req->req.status = status; 294 else 295 status = req->req.status; 296 297 if (status && status != -ESHUTDOWN) 298 EP_DBG(ep, "done req:%p, status:%d\n", req, status); 299 300 spin_unlock(&udc->lock); 301 usb_gadget_giveback_request(&ep->ep, &req->req); 302 spin_lock(&udc->lock); 303 } 304 305 static void ast_udc_nuke(struct ast_udc_ep *ep, int status) 306 { 307 int count = 0; 308 309 while (!list_empty(&ep->queue)) { 310 struct ast_udc_request *req; 311 312 req = list_entry(ep->queue.next, struct ast_udc_request, 313 queue); 314 ast_udc_done(ep, req, status); 315 count++; 316 } 317 318 if (count) 319 EP_DBG(ep, "Nuked %d request(s)\n", count); 320 } 321 322 /* 323 * Stop activity on all endpoints. 324 * Device controller for which EP activity is to be stopped. 325 * 326 * All the endpoints are stopped and any pending transfer requests if any on 327 * the endpoint are terminated. 328 */ 329 static void ast_udc_stop_activity(struct ast_udc_dev *udc) 330 { 331 struct ast_udc_ep *ep; 332 int i; 333 334 for (i = 0; i < AST_UDC_NUM_ENDPOINTS; i++) { 335 ep = &udc->ep[i]; 336 ep->stopped = 1; 337 ast_udc_nuke(ep, -ESHUTDOWN); 338 } 339 } 340 341 static int ast_udc_ep_enable(struct usb_ep *_ep, 342 const struct usb_endpoint_descriptor *desc) 343 { 344 u16 maxpacket = usb_endpoint_maxp(desc); 345 struct ast_udc_ep *ep = to_ast_ep(_ep); 346 struct ast_udc_dev *udc = ep->udc; 347 u8 epnum = usb_endpoint_num(desc); 348 unsigned long flags; 349 u32 ep_conf = 0; 350 u8 dir_in; 351 u8 type; 352 353 if (!_ep || !ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT || 354 maxpacket == 0 || maxpacket > ep->ep.maxpacket) { 355 EP_DBG(ep, "Failed, invalid EP enable param\n"); 356 return -EINVAL; 357 } 358 359 if (!udc->driver) { 360 EP_DBG(ep, "bogus device state\n"); 361 return -ESHUTDOWN; 362 } 363 364 EP_DBG(ep, "maxpacket:0x%x\n", maxpacket); 365 366 spin_lock_irqsave(&udc->lock, flags); 367 368 ep->desc = desc; 369 ep->stopped = 0; 370 ep->ep.maxpacket = maxpacket; 371 ep->chunk_max = AST_EP_DMA_DESC_MAX_LEN; 372 373 if (maxpacket < AST_UDC_EPn_MAX_PACKET) 374 ep_conf = EP_SET_MAX_PKT(maxpacket); 375 376 ep_conf |= EP_SET_EP_NUM(epnum); 377 378 type = usb_endpoint_type(desc); 379 dir_in = usb_endpoint_dir_in(desc); 380 ep->dir_in = dir_in; 381 if (!ep->dir_in) 382 ep_conf |= EP_DIR_OUT; 383 384 EP_DBG(ep, "type %d, dir_in %d\n", type, dir_in); 385 switch (type) { 386 case USB_ENDPOINT_XFER_ISOC: 387 ep_conf |= EP_SET_TYPE_MASK(EP_TYPE_ISO); 388 break; 389 390 case USB_ENDPOINT_XFER_BULK: 391 ep_conf |= EP_SET_TYPE_MASK(EP_TYPE_BULK); 392 break; 393 394 case USB_ENDPOINT_XFER_INT: 395 ep_conf |= EP_SET_TYPE_MASK(EP_TYPE_INT); 396 break; 397 } 398 399 ep->desc_mode = udc->desc_mode && ep->descs_dma && ep->dir_in; 400 if (ep->desc_mode) { 401 ast_ep_write(ep, EP_DMA_CTRL_RESET, AST_UDC_EP_DMA_CTRL); 402 ast_ep_write(ep, 0, AST_UDC_EP_DMA_STS); 403 ast_ep_write(ep, ep->descs_dma, AST_UDC_EP_DMA_BUFF); 404 405 /* Enable Long Descriptor Mode */ 406 ast_ep_write(ep, EP_DMA_CTRL_IN_LONG_MODE | EP_DMA_DESC_MODE, 407 AST_UDC_EP_DMA_CTRL); 408 409 ep->descs_wptr = 0; 410 411 } else { 412 ast_ep_write(ep, EP_DMA_CTRL_RESET, AST_UDC_EP_DMA_CTRL); 413 ast_ep_write(ep, EP_DMA_SINGLE_STAGE, AST_UDC_EP_DMA_CTRL); 414 ast_ep_write(ep, 0, AST_UDC_EP_DMA_STS); 415 } 416 417 /* Cleanup data toggle just in case */ 418 ast_udc_write(udc, EP_TOGGLE_SET_EPNUM(epnum), AST_VHUB_EP_DATA); 419 420 /* Enable EP */ 421 ast_ep_write(ep, ep_conf | EP_ENABLE, AST_UDC_EP_CONFIG); 422 423 EP_DBG(ep, "ep_config: 0x%x\n", ast_ep_read(ep, AST_UDC_EP_CONFIG)); 424 425 spin_unlock_irqrestore(&udc->lock, flags); 426 427 return 0; 428 } 429 430 static int ast_udc_ep_disable(struct usb_ep *_ep) 431 { 432 struct ast_udc_ep *ep = to_ast_ep(_ep); 433 struct ast_udc_dev *udc = ep->udc; 434 unsigned long flags; 435 436 spin_lock_irqsave(&udc->lock, flags); 437 438 ep->ep.desc = NULL; 439 ep->stopped = 1; 440 441 ast_udc_nuke(ep, -ESHUTDOWN); 442 ast_ep_write(ep, 0, AST_UDC_EP_CONFIG); 443 444 spin_unlock_irqrestore(&udc->lock, flags); 445 446 return 0; 447 } 448 449 static struct usb_request *ast_udc_ep_alloc_request(struct usb_ep *_ep, 450 gfp_t gfp_flags) 451 { 452 struct ast_udc_ep *ep = to_ast_ep(_ep); 453 struct ast_udc_request *req; 454 455 req = kzalloc(sizeof(struct ast_udc_request), gfp_flags); 456 if (!req) { 457 EP_DBG(ep, "request allocation failed\n"); 458 return NULL; 459 } 460 461 INIT_LIST_HEAD(&req->queue); 462 463 return &req->req; 464 } 465 466 static void ast_udc_ep_free_request(struct usb_ep *_ep, 467 struct usb_request *_req) 468 { 469 struct ast_udc_request *req = to_ast_req(_req); 470 471 kfree(req); 472 } 473 474 static int ast_dma_descriptor_setup(struct ast_udc_ep *ep, u32 dma_buf, 475 u16 tx_len, struct ast_udc_request *req) 476 { 477 struct ast_udc_dev *udc = ep->udc; 478 struct device *dev = &udc->pdev->dev; 479 u32 offset, chunk; 480 int count, last; 481 482 if (!ep->descs) { 483 dev_warn(dev, "%s: Empty DMA descs list failure\n", 484 ep->ep.name); 485 return -EINVAL; 486 } 487 488 chunk = tx_len; 489 offset = count = last = 0; 490 491 EP_DBG(ep, "req @%p, %s:%d, %s:0x%x, %s:0x%x\n", req, 492 "wptr", ep->descs_wptr, "dma_buf", dma_buf, 493 "tx_len", tx_len); 494 495 /* Create Descriptor Lists */ 496 while (chunk >= 0 && !last && count < AST_UDC_DESCS_COUNT) { 497 498 ep->descs[ep->descs_wptr].des_0 = dma_buf + offset; 499 500 if (chunk <= ep->chunk_max) { 501 ep->descs[ep->descs_wptr].des_1 = chunk; 502 last = 1; 503 } else { 504 ep->descs[ep->descs_wptr].des_1 = ep->chunk_max; 505 chunk -= ep->chunk_max; 506 } 507 508 EP_DBG(ep, "descs[%d]: 0x%x 0x%x, last:%d\n", 509 ep->descs_wptr, 510 ep->descs[ep->descs_wptr].des_0, 511 ep->descs[ep->descs_wptr].des_1, 512 last); 513 514 if (count == 0) 515 req->saved_dma_wptr = ep->descs_wptr; 516 517 ep->descs_wptr++; 518 count++; 519 520 if (ep->descs_wptr >= AST_UDC_DESCS_COUNT) 521 ep->descs_wptr = 0; 522 523 offset = ep->chunk_max * count; 524 } 525 526 return 0; 527 } 528 529 static void ast_udc_epn_kick(struct ast_udc_ep *ep, struct ast_udc_request *req) 530 { 531 u32 tx_len; 532 u32 last; 533 534 last = req->req.length - req->req.actual; 535 tx_len = last > ep->ep.maxpacket ? ep->ep.maxpacket : last; 536 537 EP_DBG(ep, "kick req @%p, len:%d, dir:%d\n", 538 req, tx_len, ep->dir_in); 539 540 ast_ep_write(ep, req->req.dma + req->req.actual, AST_UDC_EP_DMA_BUFF); 541 542 /* Start DMA */ 543 ast_ep_write(ep, EP_DMA_SET_TX_SIZE(tx_len), AST_UDC_EP_DMA_STS); 544 ast_ep_write(ep, EP_DMA_SET_TX_SIZE(tx_len) | EP_DMA_SINGLE_KICK, 545 AST_UDC_EP_DMA_STS); 546 } 547 548 static void ast_udc_epn_kick_desc(struct ast_udc_ep *ep, 549 struct ast_udc_request *req) 550 { 551 u32 descs_max_size; 552 u32 tx_len; 553 u32 last; 554 555 descs_max_size = AST_EP_DMA_DESC_MAX_LEN * AST_UDC_DESCS_COUNT; 556 557 last = req->req.length - req->req.actual; 558 tx_len = last > descs_max_size ? descs_max_size : last; 559 560 EP_DBG(ep, "kick req @%p, %s:%d, %s:0x%x, %s:0x%x (%d/%d), %s:0x%x\n", 561 req, "tx_len", tx_len, "dir_in", ep->dir_in, 562 "dma", req->req.dma + req->req.actual, 563 req->req.actual, req->req.length, 564 "descs_max_size", descs_max_size); 565 566 if (!ast_dma_descriptor_setup(ep, req->req.dma + req->req.actual, 567 tx_len, req)) 568 req->actual_dma_length += tx_len; 569 570 /* make sure CPU done everything before triggering DMA */ 571 mb(); 572 573 ast_ep_write(ep, ep->descs_wptr, AST_UDC_EP_DMA_STS); 574 575 EP_DBG(ep, "descs_wptr:%d, dstat:0x%x, dctrl:0x%x\n", 576 ep->descs_wptr, 577 ast_ep_read(ep, AST_UDC_EP_DMA_STS), 578 ast_ep_read(ep, AST_UDC_EP_DMA_CTRL)); 579 } 580 581 static void ast_udc_ep0_queue(struct ast_udc_ep *ep, 582 struct ast_udc_request *req) 583 { 584 struct ast_udc_dev *udc = ep->udc; 585 u32 tx_len; 586 u32 last; 587 588 last = req->req.length - req->req.actual; 589 tx_len = last > ep->ep.maxpacket ? ep->ep.maxpacket : last; 590 591 ast_udc_write(udc, req->req.dma + req->req.actual, 592 AST_UDC_EP0_DATA_BUFF); 593 594 if (ep->dir_in) { 595 /* IN requests, send data */ 596 SETUP_DBG(udc, "IN: %s:0x%x, %s:0x%x, %s:%d (%d/%d), %s:%d\n", 597 "buf", (u32)req->req.buf, 598 "dma", req->req.dma + req->req.actual, 599 "tx_len", tx_len, 600 req->req.actual, req->req.length, 601 "dir_in", ep->dir_in); 602 603 req->req.actual += tx_len; 604 ast_udc_write(udc, EP0_TX_LEN(tx_len), AST_UDC_EP0_CTRL); 605 ast_udc_write(udc, EP0_TX_LEN(tx_len) | EP0_TX_BUFF_RDY, 606 AST_UDC_EP0_CTRL); 607 608 } else { 609 /* OUT requests, receive data */ 610 SETUP_DBG(udc, "OUT: %s:%x, %s:%x, %s:(%d/%d), %s:%d\n", 611 "buf", (u32)req->req.buf, 612 "dma", req->req.dma + req->req.actual, 613 "len", req->req.actual, req->req.length, 614 "dir_in", ep->dir_in); 615 616 if (!req->req.length) { 617 /* 0 len request, send tx as completion */ 618 ast_udc_write(udc, EP0_TX_BUFF_RDY, AST_UDC_EP0_CTRL); 619 ep->dir_in = 0x1; 620 } else 621 ast_udc_write(udc, EP0_RX_BUFF_RDY, AST_UDC_EP0_CTRL); 622 } 623 } 624 625 static int ast_udc_ep_queue(struct usb_ep *_ep, struct usb_request *_req, 626 gfp_t gfp_flags) 627 { 628 struct ast_udc_request *req = to_ast_req(_req); 629 struct ast_udc_ep *ep = to_ast_ep(_ep); 630 struct ast_udc_dev *udc = ep->udc; 631 struct device *dev = &udc->pdev->dev; 632 unsigned long flags; 633 int rc; 634 635 if (unlikely(!_req || !_req->complete || !_req->buf || !_ep)) { 636 dev_warn(dev, "Invalid EP request !\n"); 637 return -EINVAL; 638 } 639 640 if (ep->stopped) { 641 dev_warn(dev, "%s is already stopped !\n", _ep->name); 642 return -ESHUTDOWN; 643 } 644 645 spin_lock_irqsave(&udc->lock, flags); 646 647 list_add_tail(&req->queue, &ep->queue); 648 649 req->req.actual = 0; 650 req->req.status = -EINPROGRESS; 651 req->actual_dma_length = 0; 652 653 rc = usb_gadget_map_request(&udc->gadget, &req->req, ep->dir_in); 654 if (rc) { 655 EP_DBG(ep, "Request mapping failure %d\n", rc); 656 dev_warn(dev, "Request mapping failure %d\n", rc); 657 goto end; 658 } 659 660 EP_DBG(ep, "enqueue req @%p\n", req); 661 EP_DBG(ep, "l=%d, dma:0x%x, zero:%d, is_in:%d\n", 662 _req->length, _req->dma, _req->zero, ep->dir_in); 663 664 /* EP0 request enqueue */ 665 if (ep->ep.desc == NULL) { 666 if ((req->req.dma % 4) != 0) { 667 dev_warn(dev, "EP0 req dma alignment error\n"); 668 rc = -ESHUTDOWN; 669 goto end; 670 } 671 672 ast_udc_ep0_queue(ep, req); 673 goto end; 674 } 675 676 /* EPn request enqueue */ 677 if (list_is_singular(&ep->queue)) { 678 if (ep->desc_mode) 679 ast_udc_epn_kick_desc(ep, req); 680 else 681 ast_udc_epn_kick(ep, req); 682 } 683 684 end: 685 spin_unlock_irqrestore(&udc->lock, flags); 686 687 return rc; 688 } 689 690 static int ast_udc_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req) 691 { 692 struct ast_udc_ep *ep = to_ast_ep(_ep); 693 struct ast_udc_dev *udc = ep->udc; 694 struct ast_udc_request *req; 695 unsigned long flags; 696 int rc = 0; 697 698 spin_lock_irqsave(&udc->lock, flags); 699 700 /* make sure it's actually queued on this endpoint */ 701 list_for_each_entry(req, &ep->queue, queue) { 702 if (&req->req == _req) { 703 list_del_init(&req->queue); 704 ast_udc_done(ep, req, -ESHUTDOWN); 705 _req->status = -ECONNRESET; 706 break; 707 } 708 } 709 710 /* dequeue request not found */ 711 if (&req->req != _req) 712 rc = -EINVAL; 713 714 spin_unlock_irqrestore(&udc->lock, flags); 715 716 return rc; 717 } 718 719 static int ast_udc_ep_set_halt(struct usb_ep *_ep, int value) 720 { 721 struct ast_udc_ep *ep = to_ast_ep(_ep); 722 struct ast_udc_dev *udc = ep->udc; 723 unsigned long flags; 724 int epnum; 725 u32 ctrl; 726 727 EP_DBG(ep, "val:%d\n", value); 728 729 spin_lock_irqsave(&udc->lock, flags); 730 731 epnum = usb_endpoint_num(ep->desc); 732 733 /* EP0 */ 734 if (epnum == 0) { 735 ctrl = ast_udc_read(udc, AST_UDC_EP0_CTRL); 736 if (value) 737 ctrl |= EP0_STALL; 738 else 739 ctrl &= ~EP0_STALL; 740 741 ast_udc_write(udc, ctrl, AST_UDC_EP0_CTRL); 742 743 } else { 744 /* EPn */ 745 ctrl = ast_udc_read(udc, AST_UDC_EP_CONFIG); 746 if (value) 747 ctrl |= EP_SET_EP_STALL; 748 else 749 ctrl &= ~EP_SET_EP_STALL; 750 751 ast_ep_write(ep, ctrl, AST_UDC_EP_CONFIG); 752 753 /* only epn is stopped and waits for clear */ 754 ep->stopped = value ? 1 : 0; 755 } 756 757 spin_unlock_irqrestore(&udc->lock, flags); 758 759 return 0; 760 } 761 762 static const struct usb_ep_ops ast_udc_ep_ops = { 763 .enable = ast_udc_ep_enable, 764 .disable = ast_udc_ep_disable, 765 .alloc_request = ast_udc_ep_alloc_request, 766 .free_request = ast_udc_ep_free_request, 767 .queue = ast_udc_ep_queue, 768 .dequeue = ast_udc_ep_dequeue, 769 .set_halt = ast_udc_ep_set_halt, 770 /* there's only imprecise fifo status reporting */ 771 }; 772 773 static void ast_udc_ep0_rx(struct ast_udc_dev *udc) 774 { 775 ast_udc_write(udc, udc->ep0_buf_dma, AST_UDC_EP0_DATA_BUFF); 776 ast_udc_write(udc, EP0_RX_BUFF_RDY, AST_UDC_EP0_CTRL); 777 } 778 779 static void ast_udc_ep0_tx(struct ast_udc_dev *udc) 780 { 781 ast_udc_write(udc, udc->ep0_buf_dma, AST_UDC_EP0_DATA_BUFF); 782 ast_udc_write(udc, EP0_TX_BUFF_RDY, AST_UDC_EP0_CTRL); 783 } 784 785 static void ast_udc_ep0_out(struct ast_udc_dev *udc) 786 { 787 struct device *dev = &udc->pdev->dev; 788 struct ast_udc_ep *ep = &udc->ep[0]; 789 struct ast_udc_request *req; 790 u16 rx_len; 791 792 if (list_empty(&ep->queue)) 793 return; 794 795 req = list_entry(ep->queue.next, struct ast_udc_request, queue); 796 797 rx_len = EP0_GET_RX_LEN(ast_udc_read(udc, AST_UDC_EP0_CTRL)); 798 req->req.actual += rx_len; 799 800 SETUP_DBG(udc, "req %p (%d/%d)\n", req, 801 req->req.actual, req->req.length); 802 803 if ((rx_len < ep->ep.maxpacket) || 804 (req->req.actual == req->req.length)) { 805 ast_udc_ep0_tx(udc); 806 if (!ep->dir_in) 807 ast_udc_done(ep, req, 0); 808 809 } else { 810 if (rx_len > req->req.length) { 811 // Issue Fix 812 dev_warn(dev, "Something wrong (%d/%d)\n", 813 req->req.actual, req->req.length); 814 ast_udc_ep0_tx(udc); 815 ast_udc_done(ep, req, 0); 816 return; 817 } 818 819 ep->dir_in = 0; 820 821 /* More works */ 822 ast_udc_ep0_queue(ep, req); 823 } 824 } 825 826 static void ast_udc_ep0_in(struct ast_udc_dev *udc) 827 { 828 struct ast_udc_ep *ep = &udc->ep[0]; 829 struct ast_udc_request *req; 830 831 if (list_empty(&ep->queue)) { 832 if (udc->is_control_tx) { 833 ast_udc_ep0_rx(udc); 834 udc->is_control_tx = 0; 835 } 836 837 return; 838 } 839 840 req = list_entry(ep->queue.next, struct ast_udc_request, queue); 841 842 SETUP_DBG(udc, "req %p (%d/%d)\n", req, 843 req->req.actual, req->req.length); 844 845 if (req->req.length == req->req.actual) { 846 if (req->req.length) 847 ast_udc_ep0_rx(udc); 848 849 if (ep->dir_in) 850 ast_udc_done(ep, req, 0); 851 852 } else { 853 /* More works */ 854 ast_udc_ep0_queue(ep, req); 855 } 856 } 857 858 static void ast_udc_epn_handle(struct ast_udc_dev *udc, u16 ep_num) 859 { 860 struct ast_udc_ep *ep = &udc->ep[ep_num]; 861 struct ast_udc_request *req; 862 u16 len = 0; 863 864 if (list_empty(&ep->queue)) 865 return; 866 867 req = list_first_entry(&ep->queue, struct ast_udc_request, queue); 868 869 len = EP_DMA_GET_TX_SIZE(ast_ep_read(ep, AST_UDC_EP_DMA_STS)); 870 req->req.actual += len; 871 872 EP_DBG(ep, "req @%p, length:(%d/%d), %s:0x%x\n", req, 873 req->req.actual, req->req.length, "len", len); 874 875 /* Done this request */ 876 if (req->req.length == req->req.actual) { 877 ast_udc_done(ep, req, 0); 878 req = list_first_entry_or_null(&ep->queue, 879 struct ast_udc_request, 880 queue); 881 882 } else { 883 /* Check for short packet */ 884 if (len < ep->ep.maxpacket) { 885 ast_udc_done(ep, req, 0); 886 req = list_first_entry_or_null(&ep->queue, 887 struct ast_udc_request, 888 queue); 889 } 890 } 891 892 /* More requests */ 893 if (req) 894 ast_udc_epn_kick(ep, req); 895 } 896 897 static void ast_udc_epn_handle_desc(struct ast_udc_dev *udc, u16 ep_num) 898 { 899 struct ast_udc_ep *ep = &udc->ep[ep_num]; 900 struct device *dev = &udc->pdev->dev; 901 struct ast_udc_request *req; 902 u32 proc_sts, wr_ptr, rd_ptr; 903 u32 len_in_desc, ctrl; 904 u16 total_len = 0; 905 int i; 906 907 if (list_empty(&ep->queue)) { 908 dev_warn(dev, "%s reqest queue empty !\n", ep->ep.name); 909 return; 910 } 911 912 req = list_first_entry(&ep->queue, struct ast_udc_request, queue); 913 914 ctrl = ast_ep_read(ep, AST_UDC_EP_DMA_CTRL); 915 proc_sts = EP_DMA_CTRL_GET_PROC_STS(ctrl); 916 917 /* Check processing status is idle */ 918 if (proc_sts != EP_DMA_CTRL_STS_RX_IDLE && 919 proc_sts != EP_DMA_CTRL_STS_TX_IDLE) { 920 dev_warn(dev, "EP DMA CTRL: 0x%x, PS:0x%x\n", 921 ast_ep_read(ep, AST_UDC_EP_DMA_CTRL), 922 proc_sts); 923 return; 924 } 925 926 ctrl = ast_ep_read(ep, AST_UDC_EP_DMA_STS); 927 rd_ptr = EP_DMA_GET_RPTR(ctrl); 928 wr_ptr = EP_DMA_GET_WPTR(ctrl); 929 930 if (rd_ptr != wr_ptr) { 931 dev_warn(dev, "desc list is not empty ! %s:%d, %s:%d\n", 932 "rptr", rd_ptr, "wptr", wr_ptr); 933 return; 934 } 935 936 EP_DBG(ep, "rd_ptr:%d, wr_ptr:%d\n", rd_ptr, wr_ptr); 937 i = req->saved_dma_wptr; 938 939 do { 940 len_in_desc = EP_DESC1_IN_LEN(ep->descs[i].des_1); 941 EP_DBG(ep, "desc[%d] len: %d\n", i, len_in_desc); 942 total_len += len_in_desc; 943 i++; 944 if (i >= AST_UDC_DESCS_COUNT) 945 i = 0; 946 947 } while (i != wr_ptr); 948 949 req->req.actual += total_len; 950 951 EP_DBG(ep, "req @%p, length:(%d/%d), %s:0x%x\n", req, 952 req->req.actual, req->req.length, "len", total_len); 953 954 /* Done this request */ 955 if (req->req.length == req->req.actual) { 956 ast_udc_done(ep, req, 0); 957 req = list_first_entry_or_null(&ep->queue, 958 struct ast_udc_request, 959 queue); 960 961 } else { 962 /* Check for short packet */ 963 if (total_len < ep->ep.maxpacket) { 964 ast_udc_done(ep, req, 0); 965 req = list_first_entry_or_null(&ep->queue, 966 struct ast_udc_request, 967 queue); 968 } 969 } 970 971 /* More requests & dma descs not setup yet */ 972 if (req && (req->actual_dma_length == req->req.actual)) { 973 EP_DBG(ep, "More requests\n"); 974 ast_udc_epn_kick_desc(ep, req); 975 } 976 } 977 978 static void ast_udc_ep0_data_tx(struct ast_udc_dev *udc, u8 *tx_data, u32 len) 979 { 980 if (len) { 981 memcpy(udc->ep0_buf, tx_data, len); 982 983 ast_udc_write(udc, udc->ep0_buf_dma, AST_UDC_EP0_DATA_BUFF); 984 ast_udc_write(udc, EP0_TX_LEN(len), AST_UDC_EP0_CTRL); 985 ast_udc_write(udc, EP0_TX_LEN(len) | EP0_TX_BUFF_RDY, 986 AST_UDC_EP0_CTRL); 987 udc->is_control_tx = 1; 988 989 } else 990 ast_udc_write(udc, EP0_TX_BUFF_RDY, AST_UDC_EP0_CTRL); 991 } 992 993 static void ast_udc_getstatus(struct ast_udc_dev *udc) 994 { 995 struct usb_ctrlrequest crq; 996 struct ast_udc_ep *ep; 997 u16 status = 0; 998 u16 epnum = 0; 999 1000 memcpy_fromio(&crq, udc->creq, sizeof(crq)); 1001 1002 switch (crq.bRequestType & USB_RECIP_MASK) { 1003 case USB_RECIP_DEVICE: 1004 /* Get device status */ 1005 status = 1 << USB_DEVICE_SELF_POWERED; 1006 break; 1007 case USB_RECIP_INTERFACE: 1008 break; 1009 case USB_RECIP_ENDPOINT: 1010 epnum = crq.wIndex & USB_ENDPOINT_NUMBER_MASK; 1011 status = udc->ep[epnum].stopped; 1012 break; 1013 default: 1014 goto stall; 1015 } 1016 1017 ep = &udc->ep[epnum]; 1018 EP_DBG(ep, "status: 0x%x\n", status); 1019 ast_udc_ep0_data_tx(udc, (u8 *)&status, sizeof(status)); 1020 1021 return; 1022 1023 stall: 1024 EP_DBG(ep, "Can't respond request\n"); 1025 ast_udc_write(udc, ast_udc_read(udc, AST_UDC_EP0_CTRL) | EP0_STALL, 1026 AST_UDC_EP0_CTRL); 1027 } 1028 1029 static void ast_udc_ep0_handle_setup(struct ast_udc_dev *udc) 1030 { 1031 struct ast_udc_ep *ep = &udc->ep[0]; 1032 struct ast_udc_request *req; 1033 struct usb_ctrlrequest crq; 1034 int req_num = 0; 1035 int rc = 0; 1036 u32 reg; 1037 1038 memcpy_fromio(&crq, udc->creq, sizeof(crq)); 1039 1040 SETUP_DBG(udc, "SETEUP packet: %02x/%02x/%04x/%04x/%04x\n", 1041 crq.bRequestType, crq.bRequest, le16_to_cpu(crq.wValue), 1042 le16_to_cpu(crq.wIndex), le16_to_cpu(crq.wLength)); 1043 1044 /* 1045 * Cleanup ep0 request(s) in queue because 1046 * there is a new control setup comes. 1047 */ 1048 list_for_each_entry(req, &udc->ep[0].queue, queue) { 1049 req_num++; 1050 EP_DBG(ep, "there is req %p in ep0 queue !\n", req); 1051 } 1052 1053 if (req_num) 1054 ast_udc_nuke(&udc->ep[0], -ETIMEDOUT); 1055 1056 udc->ep[0].dir_in = crq.bRequestType & USB_DIR_IN; 1057 1058 if ((crq.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { 1059 switch (crq.bRequest) { 1060 case USB_REQ_SET_ADDRESS: 1061 if (ast_udc_read(udc, AST_UDC_STS) & UDC_STS_HIGHSPEED) 1062 udc->gadget.speed = USB_SPEED_HIGH; 1063 else 1064 udc->gadget.speed = USB_SPEED_FULL; 1065 1066 SETUP_DBG(udc, "set addr: 0x%x\n", crq.wValue); 1067 reg = ast_udc_read(udc, AST_UDC_CONFIG); 1068 reg &= ~UDC_CFG_ADDR_MASK; 1069 reg |= UDC_CFG_SET_ADDR(crq.wValue); 1070 ast_udc_write(udc, reg, AST_UDC_CONFIG); 1071 goto req_complete; 1072 1073 case USB_REQ_CLEAR_FEATURE: 1074 SETUP_DBG(udc, "ep0: CLEAR FEATURE\n"); 1075 goto req_driver; 1076 1077 case USB_REQ_SET_FEATURE: 1078 SETUP_DBG(udc, "ep0: SET FEATURE\n"); 1079 goto req_driver; 1080 1081 case USB_REQ_GET_STATUS: 1082 ast_udc_getstatus(udc); 1083 return; 1084 1085 default: 1086 goto req_driver; 1087 } 1088 1089 } 1090 1091 req_driver: 1092 if (udc->driver) { 1093 SETUP_DBG(udc, "Forwarding %s to gadget...\n", 1094 udc->gadget.name); 1095 1096 spin_unlock(&udc->lock); 1097 rc = udc->driver->setup(&udc->gadget, &crq); 1098 spin_lock(&udc->lock); 1099 1100 } else { 1101 SETUP_DBG(udc, "No gadget for request !\n"); 1102 } 1103 1104 if (rc >= 0) 1105 return; 1106 1107 /* Stall if gadget failed */ 1108 SETUP_DBG(udc, "Stalling, rc:0x%x\n", rc); 1109 ast_udc_write(udc, ast_udc_read(udc, AST_UDC_EP0_CTRL) | EP0_STALL, 1110 AST_UDC_EP0_CTRL); 1111 return; 1112 1113 req_complete: 1114 SETUP_DBG(udc, "ep0: Sending IN status without data\n"); 1115 ast_udc_write(udc, EP0_TX_BUFF_RDY, AST_UDC_EP0_CTRL); 1116 } 1117 1118 static irqreturn_t ast_udc_isr(int irq, void *data) 1119 { 1120 struct ast_udc_dev *udc = (struct ast_udc_dev *)data; 1121 struct ast_udc_ep *ep; 1122 u32 isr, ep_isr; 1123 int i; 1124 1125 spin_lock(&udc->lock); 1126 1127 isr = ast_udc_read(udc, AST_UDC_ISR); 1128 if (!isr) 1129 goto done; 1130 1131 /* Ack interrupts */ 1132 ast_udc_write(udc, isr, AST_UDC_ISR); 1133 1134 if (isr & UDC_IRQ_BUS_RESET) { 1135 ISR_DBG(udc, "UDC_IRQ_BUS_RESET\n"); 1136 udc->gadget.speed = USB_SPEED_UNKNOWN; 1137 1138 ep = &udc->ep[1]; 1139 EP_DBG(ep, "dctrl:0x%x\n", 1140 ast_ep_read(ep, AST_UDC_EP_DMA_CTRL)); 1141 1142 if (udc->driver && udc->driver->reset) { 1143 spin_unlock(&udc->lock); 1144 udc->driver->reset(&udc->gadget); 1145 spin_lock(&udc->lock); 1146 } 1147 } 1148 1149 if (isr & UDC_IRQ_BUS_SUSPEND) { 1150 ISR_DBG(udc, "UDC_IRQ_BUS_SUSPEND\n"); 1151 udc->suspended_from = udc->gadget.state; 1152 usb_gadget_set_state(&udc->gadget, USB_STATE_SUSPENDED); 1153 1154 if (udc->driver && udc->driver->suspend) { 1155 spin_unlock(&udc->lock); 1156 udc->driver->suspend(&udc->gadget); 1157 spin_lock(&udc->lock); 1158 } 1159 } 1160 1161 if (isr & UDC_IRQ_BUS_RESUME) { 1162 ISR_DBG(udc, "UDC_IRQ_BUS_RESUME\n"); 1163 usb_gadget_set_state(&udc->gadget, udc->suspended_from); 1164 1165 if (udc->driver && udc->driver->resume) { 1166 spin_unlock(&udc->lock); 1167 udc->driver->resume(&udc->gadget); 1168 spin_lock(&udc->lock); 1169 } 1170 } 1171 1172 if (isr & UDC_IRQ_EP0_IN_ACK_STALL) { 1173 ISR_DBG(udc, "UDC_IRQ_EP0_IN_ACK_STALL\n"); 1174 ast_udc_ep0_in(udc); 1175 } 1176 1177 if (isr & UDC_IRQ_EP0_OUT_ACK_STALL) { 1178 ISR_DBG(udc, "UDC_IRQ_EP0_OUT_ACK_STALL\n"); 1179 ast_udc_ep0_out(udc); 1180 } 1181 1182 if (isr & UDC_IRQ_EP0_SETUP) { 1183 ISR_DBG(udc, "UDC_IRQ_EP0_SETUP\n"); 1184 ast_udc_ep0_handle_setup(udc); 1185 } 1186 1187 if (isr & UDC_IRQ_EP_POOL_ACK_STALL) { 1188 ISR_DBG(udc, "UDC_IRQ_EP_POOL_ACK_STALL\n"); 1189 ep_isr = ast_udc_read(udc, AST_UDC_EP_ACK_ISR); 1190 1191 /* Ack EP interrupts */ 1192 ast_udc_write(udc, ep_isr, AST_UDC_EP_ACK_ISR); 1193 1194 /* Handle each EP */ 1195 for (i = 0; i < AST_UDC_NUM_ENDPOINTS - 1; i++) { 1196 if (ep_isr & (0x1 << i)) { 1197 ep = &udc->ep[i + 1]; 1198 if (ep->desc_mode) 1199 ast_udc_epn_handle_desc(udc, i + 1); 1200 else 1201 ast_udc_epn_handle(udc, i + 1); 1202 } 1203 } 1204 } 1205 1206 done: 1207 spin_unlock(&udc->lock); 1208 return IRQ_HANDLED; 1209 } 1210 1211 static int ast_udc_gadget_getframe(struct usb_gadget *gadget) 1212 { 1213 struct ast_udc_dev *udc = to_ast_dev(gadget); 1214 1215 return (ast_udc_read(udc, AST_UDC_STS) >> 16) & 0x7ff; 1216 } 1217 1218 static void ast_udc_wake_work(struct work_struct *work) 1219 { 1220 struct ast_udc_dev *udc = container_of(work, struct ast_udc_dev, 1221 wake_work); 1222 unsigned long flags; 1223 u32 ctrl; 1224 1225 spin_lock_irqsave(&udc->lock, flags); 1226 1227 UDC_DBG(udc, "Wakeup Host !\n"); 1228 ctrl = ast_udc_read(udc, AST_UDC_FUNC_CTRL); 1229 ast_udc_write(udc, ctrl | USB_REMOTE_WAKEUP_EN, AST_UDC_FUNC_CTRL); 1230 1231 spin_unlock_irqrestore(&udc->lock, flags); 1232 } 1233 1234 static void ast_udc_wakeup_all(struct ast_udc_dev *udc) 1235 { 1236 /* 1237 * A device is trying to wake the world, because this 1238 * can recurse into the device, we break the call chain 1239 * using a work queue 1240 */ 1241 schedule_work(&udc->wake_work); 1242 } 1243 1244 static int ast_udc_wakeup(struct usb_gadget *gadget) 1245 { 1246 struct ast_udc_dev *udc = to_ast_dev(gadget); 1247 unsigned long flags; 1248 int rc = 0; 1249 1250 spin_lock_irqsave(&udc->lock, flags); 1251 1252 if (!udc->wakeup_en) { 1253 UDC_DBG(udc, "Remote Wakeup is disabled\n"); 1254 rc = -EINVAL; 1255 goto err; 1256 } 1257 1258 UDC_DBG(udc, "Device initiated wakeup\n"); 1259 ast_udc_wakeup_all(udc); 1260 1261 err: 1262 spin_unlock_irqrestore(&udc->lock, flags); 1263 return rc; 1264 } 1265 1266 /* 1267 * Activate/Deactivate link with host 1268 */ 1269 static int ast_udc_pullup(struct usb_gadget *gadget, int is_on) 1270 { 1271 struct ast_udc_dev *udc = to_ast_dev(gadget); 1272 unsigned long flags; 1273 u32 ctrl; 1274 1275 spin_lock_irqsave(&udc->lock, flags); 1276 1277 UDC_DBG(udc, "is_on: %d\n", is_on); 1278 if (is_on) 1279 ctrl = ast_udc_read(udc, AST_UDC_FUNC_CTRL) | USB_UPSTREAM_EN; 1280 else 1281 ctrl = ast_udc_read(udc, AST_UDC_FUNC_CTRL) & ~USB_UPSTREAM_EN; 1282 1283 ast_udc_write(udc, ctrl, AST_UDC_FUNC_CTRL); 1284 1285 spin_unlock_irqrestore(&udc->lock, flags); 1286 1287 return 0; 1288 } 1289 1290 static int ast_udc_start(struct usb_gadget *gadget, 1291 struct usb_gadget_driver *driver) 1292 { 1293 struct ast_udc_dev *udc = to_ast_dev(gadget); 1294 struct ast_udc_ep *ep; 1295 unsigned long flags; 1296 int i; 1297 1298 spin_lock_irqsave(&udc->lock, flags); 1299 1300 UDC_DBG(udc, "\n"); 1301 udc->driver = driver; 1302 udc->gadget.dev.of_node = udc->pdev->dev.of_node; 1303 1304 for (i = 0; i < AST_UDC_NUM_ENDPOINTS; i++) { 1305 ep = &udc->ep[i]; 1306 ep->stopped = 0; 1307 } 1308 1309 spin_unlock_irqrestore(&udc->lock, flags); 1310 1311 return 0; 1312 } 1313 1314 static int ast_udc_stop(struct usb_gadget *gadget) 1315 { 1316 struct ast_udc_dev *udc = to_ast_dev(gadget); 1317 unsigned long flags; 1318 u32 ctrl; 1319 1320 spin_lock_irqsave(&udc->lock, flags); 1321 1322 UDC_DBG(udc, "\n"); 1323 ctrl = ast_udc_read(udc, AST_UDC_FUNC_CTRL) & ~USB_UPSTREAM_EN; 1324 ast_udc_write(udc, ctrl, AST_UDC_FUNC_CTRL); 1325 1326 udc->gadget.speed = USB_SPEED_UNKNOWN; 1327 udc->driver = NULL; 1328 1329 ast_udc_stop_activity(udc); 1330 usb_gadget_set_state(&udc->gadget, USB_STATE_NOTATTACHED); 1331 1332 spin_unlock_irqrestore(&udc->lock, flags); 1333 1334 return 0; 1335 } 1336 1337 static const struct usb_gadget_ops ast_udc_ops = { 1338 .get_frame = ast_udc_gadget_getframe, 1339 .wakeup = ast_udc_wakeup, 1340 .pullup = ast_udc_pullup, 1341 .udc_start = ast_udc_start, 1342 .udc_stop = ast_udc_stop, 1343 }; 1344 1345 /* 1346 * Support 1 Control Endpoint. 1347 * Support multiple programmable endpoints that can be configured to 1348 * Bulk IN/OUT, Interrupt IN/OUT, and Isochronous IN/OUT type endpoint. 1349 */ 1350 static void ast_udc_init_ep(struct ast_udc_dev *udc) 1351 { 1352 struct ast_udc_ep *ep; 1353 int i; 1354 1355 for (i = 0; i < AST_UDC_NUM_ENDPOINTS; i++) { 1356 ep = &udc->ep[i]; 1357 ep->ep.name = ast_ep_name[i]; 1358 if (i == 0) { 1359 ep->ep.caps.type_control = true; 1360 } else { 1361 ep->ep.caps.type_iso = true; 1362 ep->ep.caps.type_bulk = true; 1363 ep->ep.caps.type_int = true; 1364 } 1365 ep->ep.caps.dir_in = true; 1366 ep->ep.caps.dir_out = true; 1367 1368 ep->ep.ops = &ast_udc_ep_ops; 1369 ep->udc = udc; 1370 1371 INIT_LIST_HEAD(&ep->queue); 1372 1373 if (i == 0) { 1374 usb_ep_set_maxpacket_limit(&ep->ep, 1375 AST_UDC_EP0_MAX_PACKET); 1376 continue; 1377 } 1378 1379 ep->ep_reg = udc->reg + AST_UDC_EP_BASE + 1380 (AST_UDC_EP_OFFSET * (i - 1)); 1381 1382 ep->epn_buf = udc->ep0_buf + (i * AST_UDC_EP_DMA_SIZE); 1383 ep->epn_buf_dma = udc->ep0_buf_dma + (i * AST_UDC_EP_DMA_SIZE); 1384 usb_ep_set_maxpacket_limit(&ep->ep, AST_UDC_EPn_MAX_PACKET); 1385 1386 ep->descs = ep->epn_buf + AST_UDC_EPn_MAX_PACKET; 1387 ep->descs_dma = ep->epn_buf_dma + AST_UDC_EPn_MAX_PACKET; 1388 ep->descs_wptr = 0; 1389 1390 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list); 1391 } 1392 } 1393 1394 static void ast_udc_init_dev(struct ast_udc_dev *udc) 1395 { 1396 INIT_WORK(&udc->wake_work, ast_udc_wake_work); 1397 } 1398 1399 static void ast_udc_init_hw(struct ast_udc_dev *udc) 1400 { 1401 u32 ctrl; 1402 1403 /* Enable PHY */ 1404 ctrl = USB_PHY_CLK_EN | USB_PHY_RESET_DIS; 1405 ast_udc_write(udc, ctrl, AST_UDC_FUNC_CTRL); 1406 1407 udelay(1); 1408 ast_udc_write(udc, 0, AST_UDC_DEV_RESET); 1409 1410 /* Set descriptor ring size */ 1411 if (AST_UDC_DESCS_COUNT == 256) { 1412 ctrl |= USB_EP_LONG_DESC; 1413 ast_udc_write(udc, ctrl, AST_UDC_FUNC_CTRL); 1414 } 1415 1416 /* Mask & ack all interrupts before installing the handler */ 1417 ast_udc_write(udc, 0, AST_UDC_IER); 1418 ast_udc_write(udc, UDC_IRQ_ACK_ALL, AST_UDC_ISR); 1419 1420 /* Enable some interrupts */ 1421 ctrl = UDC_IRQ_EP_POOL_ACK_STALL | UDC_IRQ_BUS_RESUME | 1422 UDC_IRQ_BUS_SUSPEND | UDC_IRQ_BUS_RESET | 1423 UDC_IRQ_EP0_IN_ACK_STALL | UDC_IRQ_EP0_OUT_ACK_STALL | 1424 UDC_IRQ_EP0_SETUP; 1425 ast_udc_write(udc, ctrl, AST_UDC_IER); 1426 1427 /* Cleanup and enable ep ACK interrupts */ 1428 ast_udc_write(udc, UDC_IRQ_EP_ACK_ALL, AST_UDC_EP_ACK_IER); 1429 ast_udc_write(udc, UDC_IRQ_EP_ACK_ALL, AST_UDC_EP_ACK_ISR); 1430 1431 ast_udc_write(udc, 0, AST_UDC_EP0_CTRL); 1432 } 1433 1434 static int ast_udc_remove(struct platform_device *pdev) 1435 { 1436 struct ast_udc_dev *udc = platform_get_drvdata(pdev); 1437 unsigned long flags; 1438 u32 ctrl; 1439 1440 usb_del_gadget_udc(&udc->gadget); 1441 if (udc->driver) 1442 return -EBUSY; 1443 1444 spin_lock_irqsave(&udc->lock, flags); 1445 1446 /* Disable upstream port connection */ 1447 ctrl = ast_udc_read(udc, AST_UDC_FUNC_CTRL) & ~USB_UPSTREAM_EN; 1448 ast_udc_write(udc, ctrl, AST_UDC_FUNC_CTRL); 1449 1450 clk_disable_unprepare(udc->clk); 1451 1452 spin_unlock_irqrestore(&udc->lock, flags); 1453 1454 if (udc->ep0_buf) 1455 dma_free_coherent(&pdev->dev, 1456 AST_UDC_EP_DMA_SIZE * AST_UDC_NUM_ENDPOINTS, 1457 udc->ep0_buf, 1458 udc->ep0_buf_dma); 1459 1460 udc->ep0_buf = NULL; 1461 1462 return 0; 1463 } 1464 1465 static int ast_udc_probe(struct platform_device *pdev) 1466 { 1467 enum usb_device_speed max_speed; 1468 struct device *dev = &pdev->dev; 1469 struct ast_udc_dev *udc; 1470 struct resource *res; 1471 int rc; 1472 1473 udc = devm_kzalloc(&pdev->dev, sizeof(struct ast_udc_dev), GFP_KERNEL); 1474 if (!udc) 1475 return -ENOMEM; 1476 1477 udc->gadget.dev.parent = dev; 1478 udc->pdev = pdev; 1479 spin_lock_init(&udc->lock); 1480 1481 udc->gadget.ops = &ast_udc_ops; 1482 udc->gadget.ep0 = &udc->ep[0].ep; 1483 udc->gadget.name = "aspeed-udc"; 1484 udc->gadget.dev.init_name = "gadget"; 1485 1486 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1487 udc->reg = devm_ioremap_resource(&pdev->dev, res); 1488 if (IS_ERR(udc->reg)) { 1489 dev_err(&pdev->dev, "Failed to map resources\n"); 1490 return PTR_ERR(udc->reg); 1491 } 1492 1493 platform_set_drvdata(pdev, udc); 1494 1495 udc->clk = devm_clk_get(&pdev->dev, NULL); 1496 if (IS_ERR(udc->clk)) { 1497 rc = PTR_ERR(udc->clk); 1498 goto err; 1499 } 1500 rc = clk_prepare_enable(udc->clk); 1501 if (rc) { 1502 dev_err(&pdev->dev, "Failed to enable clock (0x%x)\n", rc); 1503 goto err; 1504 } 1505 1506 /* Check if we need to limit the HW to USB1 */ 1507 max_speed = usb_get_maximum_speed(&pdev->dev); 1508 if (max_speed != USB_SPEED_UNKNOWN && max_speed < USB_SPEED_HIGH) 1509 udc->force_usb1 = true; 1510 1511 /* 1512 * Allocate DMA buffers for all EPs in one chunk 1513 */ 1514 udc->ep0_buf = dma_alloc_coherent(&pdev->dev, 1515 AST_UDC_EP_DMA_SIZE * 1516 AST_UDC_NUM_ENDPOINTS, 1517 &udc->ep0_buf_dma, GFP_KERNEL); 1518 1519 udc->gadget.speed = USB_SPEED_UNKNOWN; 1520 udc->gadget.max_speed = USB_SPEED_HIGH; 1521 udc->creq = udc->reg + AST_UDC_SETUP0; 1522 1523 /* 1524 * Support single stage mode or 32/256 stages descriptor mode. 1525 * Set default as Descriptor Mode. 1526 */ 1527 udc->desc_mode = AST_UDC_DESC_MODE; 1528 1529 dev_info(&pdev->dev, "DMA %s\n", udc->desc_mode ? 1530 "descriptor mode" : "single mode"); 1531 1532 INIT_LIST_HEAD(&udc->gadget.ep_list); 1533 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list); 1534 1535 /* Initialized udc ep */ 1536 ast_udc_init_ep(udc); 1537 1538 /* Initialized udc device */ 1539 ast_udc_init_dev(udc); 1540 1541 /* Initialized udc hardware */ 1542 ast_udc_init_hw(udc); 1543 1544 /* Find interrupt and install handler */ 1545 udc->irq = platform_get_irq(pdev, 0); 1546 if (udc->irq < 0) { 1547 dev_err(&pdev->dev, "Failed to get interrupt\n"); 1548 rc = udc->irq; 1549 goto err; 1550 } 1551 1552 rc = devm_request_irq(&pdev->dev, udc->irq, ast_udc_isr, 0, 1553 KBUILD_MODNAME, udc); 1554 if (rc) { 1555 dev_err(&pdev->dev, "Failed to request interrupt\n"); 1556 goto err; 1557 } 1558 1559 rc = usb_add_gadget_udc(&pdev->dev, &udc->gadget); 1560 if (rc) { 1561 dev_err(&pdev->dev, "Failed to add gadget udc\n"); 1562 goto err; 1563 } 1564 1565 dev_info(&pdev->dev, "Initialized udc in USB%s mode\n", 1566 udc->force_usb1 ? "1" : "2"); 1567 1568 return 0; 1569 1570 err: 1571 dev_err(&pdev->dev, "Failed to udc probe, rc:0x%x\n", rc); 1572 ast_udc_remove(pdev); 1573 1574 return rc; 1575 } 1576 1577 static const struct of_device_id ast_udc_of_dt_ids[] = { 1578 { .compatible = "aspeed,ast2600-udc", }, 1579 {} 1580 }; 1581 1582 MODULE_DEVICE_TABLE(of, ast_udc_of_dt_ids); 1583 1584 static struct platform_driver ast_udc_driver = { 1585 .probe = ast_udc_probe, 1586 .remove = ast_udc_remove, 1587 .driver = { 1588 .name = KBUILD_MODNAME, 1589 .of_match_table = ast_udc_of_dt_ids, 1590 }, 1591 }; 1592 1593 module_platform_driver(ast_udc_driver); 1594 1595 MODULE_DESCRIPTION("ASPEED UDC driver"); 1596 MODULE_AUTHOR("Neal Liu <neal_liu@aspeedtech.com>"); 1597 MODULE_LICENSE("GPL"); 1598