1 /* 2 * Renesas USB driver 3 * 4 * Copyright (C) 2011 Renesas Solutions Corp. 5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 15 * 16 */ 17 #include <linux/delay.h> 18 #include <linux/slab.h> 19 #include "./common.h" 20 #include "./pipe.h" 21 22 /* 23 * macros 24 */ 25 #define usbhsp_addr_offset(p) ((usbhs_pipe_number(p) - 1) * 2) 26 27 #define usbhsp_flags_set(p, f) ((p)->flags |= USBHS_PIPE_FLAGS_##f) 28 #define usbhsp_flags_clr(p, f) ((p)->flags &= ~USBHS_PIPE_FLAGS_##f) 29 #define usbhsp_flags_has(p, f) ((p)->flags & USBHS_PIPE_FLAGS_##f) 30 #define usbhsp_flags_init(p) do {(p)->flags = 0; } while (0) 31 32 /* 33 * for debug 34 */ 35 static char *usbhsp_pipe_name[] = { 36 [USB_ENDPOINT_XFER_CONTROL] = "DCP", 37 [USB_ENDPOINT_XFER_BULK] = "BULK", 38 [USB_ENDPOINT_XFER_INT] = "INT", 39 [USB_ENDPOINT_XFER_ISOC] = "ISO", 40 }; 41 42 char *usbhs_pipe_name(struct usbhs_pipe *pipe) 43 { 44 return usbhsp_pipe_name[usbhs_pipe_type(pipe)]; 45 } 46 47 /* 48 * DCPCTR/PIPEnCTR functions 49 */ 50 static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val) 51 { 52 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 53 int offset = usbhsp_addr_offset(pipe); 54 55 if (usbhs_pipe_is_dcp(pipe)) 56 usbhs_bset(priv, DCPCTR, mask, val); 57 else 58 usbhs_bset(priv, PIPEnCTR + offset, mask, val); 59 } 60 61 static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe) 62 { 63 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 64 int offset = usbhsp_addr_offset(pipe); 65 66 if (usbhs_pipe_is_dcp(pipe)) 67 return usbhs_read(priv, DCPCTR); 68 else 69 return usbhs_read(priv, PIPEnCTR + offset); 70 } 71 72 /* 73 * DCP/PIPE functions 74 */ 75 static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe, 76 u16 dcp_reg, u16 pipe_reg, 77 u16 mask, u16 val) 78 { 79 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 80 81 if (usbhs_pipe_is_dcp(pipe)) 82 usbhs_bset(priv, dcp_reg, mask, val); 83 else 84 usbhs_bset(priv, pipe_reg, mask, val); 85 } 86 87 /* 88 * DCPCFG/PIPECFG functions 89 */ 90 static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val) 91 { 92 __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val); 93 } 94 95 /* 96 * PIPEBUF 97 */ 98 static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val) 99 { 100 if (usbhs_pipe_is_dcp(pipe)) 101 return; 102 103 __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val); 104 } 105 106 /* 107 * DCPMAXP/PIPEMAXP 108 */ 109 static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val) 110 { 111 __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val); 112 } 113 114 /* 115 * pipe control functions 116 */ 117 static void usbhsp_pipe_select(struct usbhs_pipe *pipe) 118 { 119 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 120 121 /* 122 * On pipe, this is necessary before 123 * accesses to below registers. 124 * 125 * PIPESEL : usbhsp_pipe_select 126 * PIPECFG : usbhsp_pipe_cfg_xxx 127 * PIPEBUF : usbhsp_pipe_buf_xxx 128 * PIPEMAXP : usbhsp_pipe_maxp_xxx 129 * PIPEPERI 130 */ 131 132 /* 133 * if pipe is dcp, no pipe is selected. 134 * it is no problem, because dcp have its register 135 */ 136 usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe)); 137 } 138 139 static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe) 140 { 141 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 142 int timeout = 1024; 143 u16 val; 144 145 /* 146 * make sure.... 147 * 148 * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is 149 * specified by the CURPIPE bits. 150 * When changing the setting of this bit after changing 151 * the PID bits for the selected pipe from BUF to NAK, 152 * check that CSSTS = 0 and PBUSY = 0. 153 */ 154 155 /* 156 * CURPIPE bit = 0 157 * 158 * see also 159 * "Operation" 160 * - "Pipe Control" 161 * - "Pipe Control Registers Switching Procedure" 162 */ 163 usbhs_write(priv, CFIFOSEL, 0); 164 usbhs_pipe_disable(pipe); 165 166 do { 167 val = usbhsp_pipectrl_get(pipe); 168 val &= CSSTS | PID_MASK; 169 if (!val) 170 return 0; 171 172 udelay(10); 173 174 } while (timeout--); 175 176 return -EBUSY; 177 } 178 179 int usbhs_pipe_is_accessible(struct usbhs_pipe *pipe) 180 { 181 u16 val; 182 183 val = usbhsp_pipectrl_get(pipe); 184 if (val & BSTS) 185 return 0; 186 187 return -EBUSY; 188 } 189 190 /* 191 * PID ctrl 192 */ 193 static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe) 194 { 195 u16 pid = usbhsp_pipectrl_get(pipe); 196 197 pid &= PID_MASK; 198 199 /* 200 * see 201 * "Pipe n Control Register" - "PID" 202 */ 203 switch (pid) { 204 case PID_STALL11: 205 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10); 206 /* fall-through */ 207 case PID_STALL10: 208 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK); 209 } 210 } 211 212 void usbhs_pipe_disable(struct usbhs_pipe *pipe) 213 { 214 int timeout = 1024; 215 u16 val; 216 217 /* see "Pipe n Control Register" - "PID" */ 218 __usbhsp_pid_try_nak_if_stall(pipe); 219 220 usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK); 221 222 do { 223 val = usbhsp_pipectrl_get(pipe); 224 val &= PBUSY; 225 if (!val) 226 break; 227 228 udelay(10); 229 } while (timeout--); 230 } 231 232 void usbhs_pipe_enable(struct usbhs_pipe *pipe) 233 { 234 /* see "Pipe n Control Register" - "PID" */ 235 __usbhsp_pid_try_nak_if_stall(pipe); 236 237 usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF); 238 } 239 240 void usbhs_pipe_stall(struct usbhs_pipe *pipe) 241 { 242 u16 pid = usbhsp_pipectrl_get(pipe); 243 244 pid &= PID_MASK; 245 246 /* 247 * see 248 * "Pipe n Control Register" - "PID" 249 */ 250 switch (pid) { 251 case PID_NAK: 252 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10); 253 break; 254 case PID_BUF: 255 usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11); 256 break; 257 } 258 } 259 260 /* 261 * pipe setup 262 */ 263 static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe) 264 { 265 /* 266 * only ISO / BULK pipe can use double buffer 267 */ 268 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) || 269 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC)) 270 return 1; 271 272 return 0; 273 } 274 275 static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe, 276 int is_host, 277 int dir_in) 278 { 279 u16 type = 0; 280 u16 bfre = 0; 281 u16 dblb = 0; 282 u16 cntmd = 0; 283 u16 dir = 0; 284 u16 epnum = 0; 285 u16 shtnak = 0; 286 u16 type_array[] = { 287 [USB_ENDPOINT_XFER_BULK] = TYPE_BULK, 288 [USB_ENDPOINT_XFER_INT] = TYPE_INT, 289 [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO, 290 }; 291 int is_double = usbhsp_possible_double_buffer(pipe); 292 293 if (usbhs_pipe_is_dcp(pipe)) 294 return -EINVAL; 295 296 /* 297 * PIPECFG 298 * 299 * see 300 * - "Register Descriptions" - "PIPECFG" register 301 * - "Features" - "Pipe configuration" 302 * - "Operation" - "Pipe Control" 303 */ 304 305 /* TYPE */ 306 type = type_array[usbhs_pipe_type(pipe)]; 307 308 /* BFRE */ 309 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) || 310 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK)) 311 bfre = 0; /* FIXME */ 312 313 /* DBLB */ 314 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_ISOC) || 315 usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK)) 316 dblb = (is_double) ? DBLB : 0; 317 318 /* CNTMD */ 319 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK)) 320 cntmd = 0; /* FIXME */ 321 322 /* DIR */ 323 if (dir_in) 324 usbhsp_flags_set(pipe, IS_DIR_HOST); 325 326 if ((is_host && !dir_in) || 327 (!is_host && dir_in)) 328 dir |= DIR_OUT; 329 330 if (!dir) 331 usbhsp_flags_set(pipe, IS_DIR_IN); 332 333 /* SHTNAK */ 334 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_BULK) && 335 !dir) 336 shtnak = SHTNAK; 337 338 /* EPNUM */ 339 epnum = 0; /* see usbhs_pipe_config_update() */ 340 341 return type | 342 bfre | 343 dblb | 344 cntmd | 345 dir | 346 shtnak | 347 epnum; 348 } 349 350 static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe) 351 { 352 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 353 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); 354 struct device *dev = usbhs_priv_to_dev(priv); 355 int pipe_num = usbhs_pipe_number(pipe); 356 int is_double = usbhsp_possible_double_buffer(pipe); 357 u16 buff_size; 358 u16 bufnmb; 359 u16 bufnmb_cnt; 360 361 /* 362 * PIPEBUF 363 * 364 * see 365 * - "Register Descriptions" - "PIPEBUF" register 366 * - "Features" - "Pipe configuration" 367 * - "Operation" - "FIFO Buffer Memory" 368 * - "Operation" - "Pipe Control" 369 * 370 * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724) 371 * 372 * BUFNMB: PIPE 373 * 0: pipe0 (DCP 256byte) 374 * 1: - 375 * 2: - 376 * 3: - 377 * 4: pipe6 (INT 64byte) 378 * 5: pipe7 (INT 64byte) 379 * 6: pipe8 (INT 64byte) 380 * 7: pipe9 (INT 64byte) 381 * 8 - xx: free (for BULK, ISOC) 382 */ 383 384 /* 385 * FIXME 386 * 387 * it doesn't have good buffer allocator 388 * 389 * DCP : 256 byte 390 * BULK: 512 byte 391 * INT : 64 byte 392 * ISOC: 512 byte 393 */ 394 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_CONTROL)) 395 buff_size = 256; 396 else if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT)) 397 buff_size = 64; 398 else 399 buff_size = 512; 400 401 /* change buff_size to register value */ 402 bufnmb_cnt = (buff_size / 64) - 1; 403 404 /* BUFNMB has been reserved for INT pipe 405 * see above */ 406 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT)) { 407 bufnmb = pipe_num - 2; 408 } else { 409 bufnmb = info->bufnmb_last; 410 info->bufnmb_last += bufnmb_cnt + 1; 411 412 /* 413 * double buffer 414 */ 415 if (is_double) 416 info->bufnmb_last += bufnmb_cnt + 1; 417 } 418 419 dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n", 420 pipe_num, buff_size, bufnmb); 421 422 return (0x1f & bufnmb_cnt) << 10 | 423 (0xff & bufnmb) << 0; 424 } 425 426 void usbhs_pipe_config_update(struct usbhs_pipe *pipe, u16 devsel, 427 u16 epnum, u16 maxp) 428 { 429 if (devsel > 0xA) { 430 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 431 struct device *dev = usbhs_priv_to_dev(priv); 432 433 dev_err(dev, "devsel error %d\n", devsel); 434 435 devsel = 0; 436 } 437 438 usbhsp_pipe_barrier(pipe); 439 440 pipe->maxp = maxp; 441 442 usbhsp_pipe_select(pipe); 443 usbhsp_pipe_maxp_set(pipe, 0xFFFF, 444 (devsel << 12) | 445 maxp); 446 447 if (!usbhs_pipe_is_dcp(pipe)) 448 usbhsp_pipe_cfg_set(pipe, 0x000F, epnum); 449 } 450 451 /* 452 * pipe control 453 */ 454 int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe) 455 { 456 /* 457 * see 458 * usbhs_pipe_config_update() 459 * usbhs_dcp_malloc() 460 */ 461 return pipe->maxp; 462 } 463 464 int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe) 465 { 466 return usbhsp_flags_has(pipe, IS_DIR_IN); 467 } 468 469 int usbhs_pipe_is_dir_host(struct usbhs_pipe *pipe) 470 { 471 return usbhsp_flags_has(pipe, IS_DIR_HOST); 472 } 473 474 void usbhs_pipe_data_sequence(struct usbhs_pipe *pipe, int data) 475 { 476 u16 mask = (SQCLR | SQSET); 477 u16 val = (data) ? SQSET : SQCLR; 478 479 usbhsp_pipectrl_set(pipe, mask, val); 480 } 481 482 void usbhs_pipe_clear(struct usbhs_pipe *pipe) 483 { 484 usbhsp_pipectrl_set(pipe, ACLRM, ACLRM); 485 usbhsp_pipectrl_set(pipe, ACLRM, 0); 486 } 487 488 static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type) 489 { 490 struct usbhs_pipe *pos, *pipe; 491 int i; 492 493 /* 494 * find target pipe 495 */ 496 pipe = NULL; 497 usbhs_for_each_pipe_with_dcp(pos, priv, i) { 498 if (!usbhs_pipe_type_is(pos, type)) 499 continue; 500 if (usbhsp_flags_has(pos, IS_USED)) 501 continue; 502 503 pipe = pos; 504 break; 505 } 506 507 if (!pipe) 508 return NULL; 509 510 /* 511 * initialize pipe flags 512 */ 513 usbhsp_flags_init(pipe); 514 usbhsp_flags_set(pipe, IS_USED); 515 516 return pipe; 517 } 518 519 void usbhs_pipe_init(struct usbhs_priv *priv, 520 int (*dma_map_ctrl)(struct usbhs_pkt *pkt, int map)) 521 { 522 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); 523 struct usbhs_pipe *pipe; 524 int i; 525 526 /* 527 * FIXME 528 * 529 * driver needs good allocator. 530 * 531 * find first free buffer area (BULK, ISOC) 532 * (DCP, INT area is fixed) 533 * 534 * buffer number 0 - 3 have been reserved for DCP 535 * see 536 * usbhsp_to_bufnmb 537 */ 538 info->bufnmb_last = 4; 539 usbhs_for_each_pipe_with_dcp(pipe, priv, i) { 540 if (usbhs_pipe_type_is(pipe, USB_ENDPOINT_XFER_INT)) 541 info->bufnmb_last++; 542 543 usbhsp_flags_init(pipe); 544 pipe->fifo = NULL; 545 pipe->mod_private = NULL; 546 INIT_LIST_HEAD(&pipe->list); 547 548 /* pipe force init */ 549 usbhs_pipe_clear(pipe); 550 } 551 552 info->dma_map_ctrl = dma_map_ctrl; 553 } 554 555 struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv, 556 int endpoint_type, 557 int dir_in) 558 { 559 struct device *dev = usbhs_priv_to_dev(priv); 560 struct usbhs_pipe *pipe; 561 int is_host = usbhs_mod_is_host(priv); 562 int ret; 563 u16 pipecfg, pipebuf; 564 565 pipe = usbhsp_get_pipe(priv, endpoint_type); 566 if (!pipe) { 567 dev_err(dev, "can't get pipe (%s)\n", 568 usbhsp_pipe_name[endpoint_type]); 569 return NULL; 570 } 571 572 INIT_LIST_HEAD(&pipe->list); 573 574 usbhs_pipe_disable(pipe); 575 576 /* make sure pipe is not busy */ 577 ret = usbhsp_pipe_barrier(pipe); 578 if (ret < 0) { 579 dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe)); 580 return NULL; 581 } 582 583 pipecfg = usbhsp_setup_pipecfg(pipe, is_host, dir_in); 584 pipebuf = usbhsp_setup_pipebuff(pipe); 585 586 usbhsp_pipe_select(pipe); 587 usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg); 588 usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf); 589 590 usbhs_pipe_sequence_data0(pipe); 591 592 dev_dbg(dev, "enable pipe %d : %s (%s)\n", 593 usbhs_pipe_number(pipe), 594 usbhs_pipe_name(pipe), 595 usbhs_pipe_is_dir_in(pipe) ? "in" : "out"); 596 597 /* 598 * epnum / maxp are still not set to this pipe. 599 * call usbhs_pipe_config_update() after this function !! 600 */ 601 602 return pipe; 603 } 604 605 void usbhs_pipe_select_fifo(struct usbhs_pipe *pipe, struct usbhs_fifo *fifo) 606 { 607 if (pipe->fifo) 608 pipe->fifo->pipe = NULL; 609 610 pipe->fifo = fifo; 611 612 if (fifo) 613 fifo->pipe = pipe; 614 } 615 616 617 /* 618 * dcp control 619 */ 620 struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv) 621 { 622 struct usbhs_pipe *pipe; 623 624 pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL); 625 if (!pipe) 626 return NULL; 627 628 INIT_LIST_HEAD(&pipe->list); 629 630 /* 631 * call usbhs_pipe_config_update() after this function !! 632 */ 633 634 return pipe; 635 } 636 637 void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe) 638 { 639 struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); 640 641 WARN_ON(!usbhs_pipe_is_dcp(pipe)); 642 643 usbhs_pipe_enable(pipe); 644 645 if (!usbhs_mod_is_host(priv)) /* funconly */ 646 usbhsp_pipectrl_set(pipe, CCPL, CCPL); 647 } 648 649 void usbhs_dcp_dir_for_host(struct usbhs_pipe *pipe, int dir_out) 650 { 651 usbhsp_pipe_cfg_set(pipe, DIR_OUT, 652 dir_out ? DIR_OUT : 0); 653 } 654 655 /* 656 * pipe module function 657 */ 658 int usbhs_pipe_probe(struct usbhs_priv *priv) 659 { 660 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); 661 struct usbhs_pipe *pipe; 662 struct device *dev = usbhs_priv_to_dev(priv); 663 u32 *pipe_type = usbhs_get_dparam(priv, pipe_type); 664 int pipe_size = usbhs_get_dparam(priv, pipe_size); 665 int i; 666 667 /* This driver expects 1st pipe is DCP */ 668 if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) { 669 dev_err(dev, "1st PIPE is not DCP\n"); 670 return -EINVAL; 671 } 672 673 info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL); 674 if (!info->pipe) { 675 dev_err(dev, "Could not allocate pipe\n"); 676 return -ENOMEM; 677 } 678 679 info->size = pipe_size; 680 681 /* 682 * init pipe 683 */ 684 usbhs_for_each_pipe_with_dcp(pipe, priv, i) { 685 pipe->priv = priv; 686 687 usbhs_pipe_type(pipe) = 688 pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK; 689 690 dev_dbg(dev, "pipe %x\t: %s\n", 691 i, usbhsp_pipe_name[pipe_type[i]]); 692 } 693 694 return 0; 695 } 696 697 void usbhs_pipe_remove(struct usbhs_priv *priv) 698 { 699 struct usbhs_pipe_info *info = usbhs_priv_to_pipeinfo(priv); 700 701 kfree(info->pipe); 702 } 703