Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 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/io.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include "./common.h" |
| 21 | #include "./pipe.h" |
| 22 | |
| 23 | /* |
| 24 | * macros |
| 25 | */ |
| 26 | #define usbhsp_priv_to_pipeinfo(pr) (&(pr)->pipe_info) |
| 27 | #define usbhsp_pipe_to_priv(p) ((p)->priv) |
| 28 | |
| 29 | #define usbhsp_addr_offset(p) ((usbhs_pipe_number(p) - 1) * 2) |
| 30 | |
| 31 | #define usbhsp_is_dcp(p) ((p)->priv->pipe_info.pipe == (p)) |
| 32 | |
| 33 | #define usbhsp_flags_set(p, f) ((p)->flags |= USBHS_PIPE_FLAGS_##f) |
| 34 | #define usbhsp_flags_clr(p, f) ((p)->flags &= ~USBHS_PIPE_FLAGS_##f) |
| 35 | #define usbhsp_flags_has(p, f) ((p)->flags & USBHS_PIPE_FLAGS_##f) |
| 36 | #define usbhsp_flags_init(p) do {(p)->flags = 0; } while (0) |
| 37 | |
| 38 | #define usbhsp_type(p) ((p)->pipe_type) |
| 39 | #define usbhsp_type_is(p, t) ((p)->pipe_type == t) |
| 40 | |
| 41 | /* |
| 42 | * for debug |
| 43 | */ |
| 44 | static char *usbhsp_pipe_name[] = { |
| 45 | [USB_ENDPOINT_XFER_CONTROL] = "DCP", |
| 46 | [USB_ENDPOINT_XFER_BULK] = "BULK", |
| 47 | [USB_ENDPOINT_XFER_INT] = "INT", |
| 48 | [USB_ENDPOINT_XFER_ISOC] = "ISO", |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * usb request functions |
| 53 | */ |
| 54 | void usbhs_usbreq_get_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req) |
| 55 | { |
| 56 | u16 val; |
| 57 | |
| 58 | val = usbhs_read(priv, USBREQ); |
| 59 | req->bRequest = (val >> 8) & 0xFF; |
| 60 | req->bRequestType = (val >> 0) & 0xFF; |
| 61 | |
| 62 | req->wValue = usbhs_read(priv, USBVAL); |
| 63 | req->wIndex = usbhs_read(priv, USBINDX); |
| 64 | req->wLength = usbhs_read(priv, USBLENG); |
| 65 | } |
| 66 | |
| 67 | void usbhs_usbreq_set_val(struct usbhs_priv *priv, struct usb_ctrlrequest *req) |
| 68 | { |
| 69 | usbhs_write(priv, USBREQ, (req->bRequest << 8) | req->bRequestType); |
| 70 | usbhs_write(priv, USBVAL, req->wValue); |
| 71 | usbhs_write(priv, USBINDX, req->wIndex); |
| 72 | usbhs_write(priv, USBLENG, req->wLength); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * DCPCTR/PIPEnCTR functions |
| 77 | */ |
| 78 | static void usbhsp_pipectrl_set(struct usbhs_pipe *pipe, u16 mask, u16 val) |
| 79 | { |
| 80 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 81 | int offset = usbhsp_addr_offset(pipe); |
| 82 | |
| 83 | if (usbhsp_is_dcp(pipe)) |
| 84 | usbhs_bset(priv, DCPCTR, mask, val); |
| 85 | else |
| 86 | usbhs_bset(priv, PIPEnCTR + offset, mask, val); |
| 87 | } |
| 88 | |
| 89 | static u16 usbhsp_pipectrl_get(struct usbhs_pipe *pipe) |
| 90 | { |
| 91 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 92 | int offset = usbhsp_addr_offset(pipe); |
| 93 | |
| 94 | if (usbhsp_is_dcp(pipe)) |
| 95 | return usbhs_read(priv, DCPCTR); |
| 96 | else |
| 97 | return usbhs_read(priv, PIPEnCTR + offset); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * DCP/PIPE functions |
| 102 | */ |
| 103 | static void __usbhsp_pipe_xxx_set(struct usbhs_pipe *pipe, |
| 104 | u16 dcp_reg, u16 pipe_reg, |
| 105 | u16 mask, u16 val) |
| 106 | { |
| 107 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 108 | |
| 109 | if (usbhsp_is_dcp(pipe)) |
| 110 | usbhs_bset(priv, dcp_reg, mask, val); |
| 111 | else |
| 112 | usbhs_bset(priv, pipe_reg, mask, val); |
| 113 | } |
| 114 | |
| 115 | static u16 __usbhsp_pipe_xxx_get(struct usbhs_pipe *pipe, |
| 116 | u16 dcp_reg, u16 pipe_reg) |
| 117 | { |
| 118 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 119 | |
| 120 | if (usbhsp_is_dcp(pipe)) |
| 121 | return usbhs_read(priv, dcp_reg); |
| 122 | else |
| 123 | return usbhs_read(priv, pipe_reg); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * DCPCFG/PIPECFG functions |
| 128 | */ |
| 129 | static void usbhsp_pipe_cfg_set(struct usbhs_pipe *pipe, u16 mask, u16 val) |
| 130 | { |
| 131 | __usbhsp_pipe_xxx_set(pipe, DCPCFG, PIPECFG, mask, val); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * PIPEBUF |
| 136 | */ |
| 137 | static void usbhsp_pipe_buf_set(struct usbhs_pipe *pipe, u16 mask, u16 val) |
| 138 | { |
| 139 | if (usbhsp_is_dcp(pipe)) |
| 140 | return; |
| 141 | |
| 142 | __usbhsp_pipe_xxx_set(pipe, 0, PIPEBUF, mask, val); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * DCPMAXP/PIPEMAXP |
| 147 | */ |
| 148 | static void usbhsp_pipe_maxp_set(struct usbhs_pipe *pipe, u16 mask, u16 val) |
| 149 | { |
| 150 | __usbhsp_pipe_xxx_set(pipe, DCPMAXP, PIPEMAXP, mask, val); |
| 151 | } |
| 152 | |
| 153 | static u16 usbhsp_pipe_maxp_get(struct usbhs_pipe *pipe) |
| 154 | { |
| 155 | return __usbhsp_pipe_xxx_get(pipe, DCPMAXP, PIPEMAXP); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * pipe control functions |
| 160 | */ |
| 161 | static void usbhsp_pipe_select(struct usbhs_pipe *pipe) |
| 162 | { |
| 163 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 164 | |
| 165 | /* |
| 166 | * On pipe, this is necessary before |
| 167 | * accesses to below registers. |
| 168 | * |
| 169 | * PIPESEL : usbhsp_pipe_select |
| 170 | * PIPECFG : usbhsp_pipe_cfg_xxx |
| 171 | * PIPEBUF : usbhsp_pipe_buf_xxx |
| 172 | * PIPEMAXP : usbhsp_pipe_maxp_xxx |
| 173 | * PIPEPERI |
| 174 | */ |
| 175 | |
| 176 | /* |
| 177 | * if pipe is dcp, no pipe is selected. |
| 178 | * it is no problem, because dcp have its register |
| 179 | */ |
| 180 | usbhs_write(priv, PIPESEL, 0xF & usbhs_pipe_number(pipe)); |
| 181 | } |
| 182 | |
| 183 | static int usbhsp_pipe_barrier(struct usbhs_pipe *pipe) |
| 184 | { |
| 185 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 186 | struct device *dev = usbhs_priv_to_dev(priv); |
| 187 | int timeout = 1024; |
| 188 | u16 val; |
| 189 | |
| 190 | /* |
| 191 | * make sure.... |
| 192 | * |
| 193 | * Modify these bits when CSSTS = 0, PID = NAK, and no pipe number is |
| 194 | * specified by the CURPIPE bits. |
| 195 | * When changing the setting of this bit after changing |
| 196 | * the PID bits for the selected pipe from BUF to NAK, |
| 197 | * check that CSSTS = 0 and PBUSY = 0. |
| 198 | */ |
| 199 | |
| 200 | /* |
| 201 | * CURPIPE bit = 0 |
| 202 | * |
| 203 | * see also |
| 204 | * "Operation" |
| 205 | * - "Pipe Control" |
| 206 | * - "Pipe Control Registers Switching Procedure" |
| 207 | */ |
| 208 | usbhs_write(priv, CFIFOSEL, 0); |
| 209 | |
| 210 | do { |
| 211 | val = usbhsp_pipectrl_get(pipe); |
| 212 | val &= CSSTS | PID_MASK; |
| 213 | if (!val) |
| 214 | return 0; |
| 215 | |
| 216 | udelay(10); |
| 217 | |
| 218 | } while (timeout--); |
| 219 | |
| 220 | /* |
| 221 | * force NAK |
| 222 | */ |
| 223 | timeout = 1024; |
| 224 | usbhs_fifo_disable(pipe); |
| 225 | do { |
| 226 | val = usbhsp_pipectrl_get(pipe); |
| 227 | val &= PBUSY; |
| 228 | if (!val) |
| 229 | return 0; |
| 230 | |
| 231 | } while (timeout--); |
| 232 | |
| 233 | dev_err(dev, "pipe barrier failed\n"); |
| 234 | |
| 235 | return -EBUSY; |
| 236 | } |
| 237 | |
| 238 | static int usbhsp_pipe_is_accessible(struct usbhs_pipe *pipe) |
| 239 | { |
| 240 | u16 val; |
| 241 | |
| 242 | val = usbhsp_pipectrl_get(pipe); |
| 243 | if (val & BSTS) |
| 244 | return 0; |
| 245 | |
| 246 | return -EBUSY; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * PID ctrl |
| 251 | */ |
| 252 | static void __usbhsp_pid_try_nak_if_stall(struct usbhs_pipe *pipe) |
| 253 | { |
| 254 | u16 pid = usbhsp_pipectrl_get(pipe); |
| 255 | |
| 256 | pid &= PID_MASK; |
| 257 | |
| 258 | /* |
| 259 | * see |
| 260 | * "Pipe n Control Register" - "PID" |
| 261 | */ |
| 262 | switch (pid) { |
| 263 | case PID_STALL11: |
| 264 | usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10); |
| 265 | /* fall-through */ |
| 266 | case PID_STALL10: |
| 267 | usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void usbhs_fifo_disable(struct usbhs_pipe *pipe) |
| 272 | { |
| 273 | /* see "Pipe n Control Register" - "PID" */ |
| 274 | __usbhsp_pid_try_nak_if_stall(pipe); |
| 275 | |
| 276 | usbhsp_pipectrl_set(pipe, PID_MASK, PID_NAK); |
| 277 | } |
| 278 | |
| 279 | void usbhs_fifo_enable(struct usbhs_pipe *pipe) |
| 280 | { |
| 281 | /* see "Pipe n Control Register" - "PID" */ |
| 282 | __usbhsp_pid_try_nak_if_stall(pipe); |
| 283 | |
| 284 | usbhsp_pipectrl_set(pipe, PID_MASK, PID_BUF); |
| 285 | } |
| 286 | |
| 287 | void usbhs_fifo_stall(struct usbhs_pipe *pipe) |
| 288 | { |
| 289 | u16 pid = usbhsp_pipectrl_get(pipe); |
| 290 | |
| 291 | pid &= PID_MASK; |
| 292 | |
| 293 | /* |
| 294 | * see |
| 295 | * "Pipe n Control Register" - "PID" |
| 296 | */ |
| 297 | switch (pid) { |
| 298 | case PID_NAK: |
| 299 | usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL10); |
| 300 | break; |
| 301 | case PID_BUF: |
| 302 | usbhsp_pipectrl_set(pipe, PID_MASK, PID_STALL11); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * CFIFO ctrl |
| 309 | */ |
| 310 | void usbhs_fifo_send_terminator(struct usbhs_pipe *pipe) |
| 311 | { |
| 312 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 313 | |
| 314 | usbhs_bset(priv, CFIFOCTR, BVAL, BVAL); |
| 315 | } |
| 316 | |
| 317 | static void usbhsp_fifo_clear(struct usbhs_pipe *pipe) |
| 318 | { |
| 319 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 320 | |
| 321 | usbhs_write(priv, CFIFOCTR, BCLR); |
| 322 | } |
| 323 | |
| 324 | static int usbhsp_fifo_barrier(struct usbhs_priv *priv) |
| 325 | { |
| 326 | int timeout = 1024; |
| 327 | |
| 328 | do { |
| 329 | /* The FIFO port is accessible */ |
| 330 | if (usbhs_read(priv, CFIFOCTR) & FRDY) |
| 331 | return 0; |
| 332 | |
| 333 | udelay(10); |
| 334 | } while (timeout--); |
| 335 | |
| 336 | return -EBUSY; |
| 337 | } |
| 338 | |
| 339 | static int usbhsp_fifo_rcv_len(struct usbhs_priv *priv) |
| 340 | { |
| 341 | return usbhs_read(priv, CFIFOCTR) & DTLN_MASK; |
| 342 | } |
| 343 | |
| 344 | static int usbhsp_fifo_select(struct usbhs_pipe *pipe, int write) |
| 345 | { |
| 346 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 347 | struct device *dev = usbhs_priv_to_dev(priv); |
| 348 | int timeout = 1024; |
| 349 | u16 mask = ((1 << 5) | 0xF); /* mask of ISEL | CURPIPE */ |
| 350 | u16 base = usbhs_pipe_number(pipe); /* CURPIPE */ |
| 351 | |
| 352 | if (usbhsp_is_dcp(pipe)) |
| 353 | base |= (1 == write) << 5; /* ISEL */ |
| 354 | |
| 355 | /* "base" will be used below */ |
| 356 | usbhs_write(priv, CFIFOSEL, base | MBW_32); |
| 357 | |
| 358 | /* check ISEL and CURPIPE value */ |
| 359 | while (timeout--) { |
| 360 | if (base == (mask & usbhs_read(priv, CFIFOSEL))) |
| 361 | return 0; |
| 362 | udelay(10); |
| 363 | } |
| 364 | |
| 365 | dev_err(dev, "fifo select error\n"); |
| 366 | |
| 367 | return -EIO; |
| 368 | } |
| 369 | |
| 370 | int usbhs_fifo_prepare_write(struct usbhs_pipe *pipe) |
| 371 | { |
Kuninori Morimoto | 45e13e6 | 2011-04-21 14:09:58 +0900 | [diff] [blame] | 372 | return usbhsp_fifo_select(pipe, 1); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | int usbhs_fifo_write(struct usbhs_pipe *pipe, u8 *buf, int len) |
| 376 | { |
| 377 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 378 | void __iomem *addr = priv->base + CFIFO; |
| 379 | int maxp = usbhs_pipe_get_maxpacket(pipe); |
| 380 | int total_len; |
| 381 | int i, ret; |
| 382 | |
| 383 | ret = usbhsp_pipe_is_accessible(pipe); |
| 384 | if (ret < 0) |
| 385 | return ret; |
| 386 | |
Kuninori Morimoto | 45e13e6 | 2011-04-21 14:09:58 +0900 | [diff] [blame] | 387 | ret = usbhsp_fifo_select(pipe, 1); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 388 | if (ret < 0) |
| 389 | return ret; |
| 390 | |
| 391 | ret = usbhsp_fifo_barrier(priv); |
| 392 | if (ret < 0) |
| 393 | return ret; |
| 394 | |
| 395 | len = min(len, maxp); |
| 396 | total_len = len; |
| 397 | |
| 398 | /* |
| 399 | * FIXME |
| 400 | * |
| 401 | * 32-bit access only |
| 402 | */ |
| 403 | if (len >= 4 && |
| 404 | !((unsigned long)buf & 0x03)) { |
| 405 | iowrite32_rep(addr, buf, len / 4); |
| 406 | len %= 4; |
| 407 | buf += total_len - len; |
| 408 | } |
| 409 | |
| 410 | /* the rest operation */ |
| 411 | for (i = 0; i < len; i++) |
| 412 | iowrite8(buf[i], addr + (0x03 - (i & 0x03))); |
| 413 | |
| 414 | if (total_len < maxp) |
| 415 | usbhs_fifo_send_terminator(pipe); |
| 416 | |
| 417 | return total_len; |
| 418 | } |
| 419 | |
| 420 | int usbhs_fifo_prepare_read(struct usbhs_pipe *pipe) |
| 421 | { |
| 422 | int ret; |
| 423 | |
| 424 | /* |
| 425 | * select pipe and enable it to prepare packet receive |
| 426 | */ |
| 427 | ret = usbhsp_fifo_select(pipe, 0); |
| 428 | if (ret < 0) |
| 429 | return ret; |
| 430 | |
| 431 | usbhs_fifo_enable(pipe); |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | int usbhs_fifo_read(struct usbhs_pipe *pipe, u8 *buf, int len) |
| 437 | { |
| 438 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 439 | void __iomem *addr = priv->base + CFIFO; |
| 440 | int rcv_len; |
| 441 | int i, ret; |
| 442 | int total_len; |
| 443 | u32 data = 0; |
| 444 | |
| 445 | ret = usbhsp_fifo_select(pipe, 0); |
| 446 | if (ret < 0) |
| 447 | return ret; |
| 448 | |
| 449 | ret = usbhsp_fifo_barrier(priv); |
| 450 | if (ret < 0) |
| 451 | return ret; |
| 452 | |
| 453 | rcv_len = usbhsp_fifo_rcv_len(priv); |
| 454 | |
| 455 | /* |
| 456 | * Buffer clear if Zero-Length packet |
| 457 | * |
| 458 | * see |
| 459 | * "Operation" - "FIFO Buffer Memory" - "FIFO Port Function" |
| 460 | */ |
| 461 | if (0 == rcv_len) { |
| 462 | usbhsp_fifo_clear(pipe); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | len = min(rcv_len, len); |
| 467 | total_len = len; |
| 468 | |
| 469 | /* |
| 470 | * FIXME |
| 471 | * |
| 472 | * 32-bit access only |
| 473 | */ |
| 474 | if (len >= 4 && |
| 475 | !((unsigned long)buf & 0x03)) { |
| 476 | ioread32_rep(addr, buf, len / 4); |
| 477 | len %= 4; |
| 478 | buf += rcv_len - len; |
| 479 | } |
| 480 | |
| 481 | /* the rest operation */ |
| 482 | for (i = 0; i < len; i++) { |
| 483 | if (!(i & 0x03)) |
| 484 | data = ioread32(addr); |
| 485 | |
| 486 | buf[i] = (data >> ((i & 0x03) * 8)) & 0xff; |
| 487 | } |
| 488 | |
| 489 | return total_len; |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * pipe setup |
| 494 | */ |
| 495 | static int usbhsp_possible_double_buffer(struct usbhs_pipe *pipe) |
| 496 | { |
| 497 | /* |
| 498 | * only ISO / BULK pipe can use double buffer |
| 499 | */ |
| 500 | if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) || |
| 501 | usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC)) |
| 502 | return 1; |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static u16 usbhsp_setup_pipecfg(struct usbhs_pipe *pipe, |
| 508 | const struct usb_endpoint_descriptor *desc, |
| 509 | int is_host) |
| 510 | { |
| 511 | u16 type = 0; |
| 512 | u16 bfre = 0; |
| 513 | u16 dblb = 0; |
| 514 | u16 cntmd = 0; |
| 515 | u16 dir = 0; |
| 516 | u16 epnum = 0; |
| 517 | u16 shtnak = 0; |
| 518 | u16 type_array[] = { |
| 519 | [USB_ENDPOINT_XFER_BULK] = TYPE_BULK, |
| 520 | [USB_ENDPOINT_XFER_INT] = TYPE_INT, |
| 521 | [USB_ENDPOINT_XFER_ISOC] = TYPE_ISO, |
| 522 | }; |
| 523 | int is_double = usbhsp_possible_double_buffer(pipe); |
| 524 | |
| 525 | if (usbhsp_is_dcp(pipe)) |
| 526 | return -EINVAL; |
| 527 | |
| 528 | /* |
| 529 | * PIPECFG |
| 530 | * |
| 531 | * see |
| 532 | * - "Register Descriptions" - "PIPECFG" register |
| 533 | * - "Features" - "Pipe configuration" |
| 534 | * - "Operation" - "Pipe Control" |
| 535 | */ |
| 536 | |
| 537 | /* TYPE */ |
| 538 | type = type_array[usbhsp_type(pipe)]; |
| 539 | |
| 540 | /* BFRE */ |
| 541 | if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) || |
| 542 | usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK)) |
| 543 | bfre = 0; /* FIXME */ |
| 544 | |
| 545 | /* DBLB */ |
| 546 | if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_ISOC) || |
| 547 | usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK)) |
| 548 | dblb = (is_double) ? DBLB : 0; |
| 549 | |
| 550 | /* CNTMD */ |
| 551 | if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK)) |
| 552 | cntmd = 0; /* FIXME */ |
| 553 | |
| 554 | /* DIR */ |
| 555 | if (usb_endpoint_dir_in(desc)) |
| 556 | usbhsp_flags_set(pipe, IS_DIR_IN); |
| 557 | |
| 558 | if ((is_host && usb_endpoint_dir_out(desc)) || |
| 559 | (!is_host && usb_endpoint_dir_in(desc))) |
| 560 | dir |= DIR_OUT; |
| 561 | |
| 562 | /* SHTNAK */ |
| 563 | if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_BULK) && |
| 564 | !dir) |
| 565 | shtnak = SHTNAK; |
| 566 | |
| 567 | /* EPNUM */ |
| 568 | epnum = 0xF & usb_endpoint_num(desc); |
| 569 | |
| 570 | return type | |
| 571 | bfre | |
| 572 | dblb | |
| 573 | cntmd | |
| 574 | dir | |
| 575 | shtnak | |
| 576 | epnum; |
| 577 | } |
| 578 | |
| 579 | static u16 usbhsp_setup_pipemaxp(struct usbhs_pipe *pipe, |
| 580 | const struct usb_endpoint_descriptor *desc, |
| 581 | int is_host) |
| 582 | { |
| 583 | /* host should set DEVSEL */ |
| 584 | |
| 585 | /* reutn MXPS */ |
| 586 | return PIPE_MAXP_MASK & le16_to_cpu(desc->wMaxPacketSize); |
| 587 | } |
| 588 | |
| 589 | static u16 usbhsp_setup_pipebuff(struct usbhs_pipe *pipe, |
| 590 | const struct usb_endpoint_descriptor *desc, |
| 591 | int is_host) |
| 592 | { |
| 593 | struct usbhs_priv *priv = usbhsp_pipe_to_priv(pipe); |
| 594 | struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv); |
| 595 | struct device *dev = usbhs_priv_to_dev(priv); |
| 596 | int pipe_num = usbhs_pipe_number(pipe); |
| 597 | int is_double = usbhsp_possible_double_buffer(pipe); |
| 598 | u16 buff_size; |
| 599 | u16 bufnmb; |
| 600 | u16 bufnmb_cnt; |
| 601 | |
| 602 | /* |
| 603 | * PIPEBUF |
| 604 | * |
| 605 | * see |
| 606 | * - "Register Descriptions" - "PIPEBUF" register |
| 607 | * - "Features" - "Pipe configuration" |
| 608 | * - "Operation" - "FIFO Buffer Memory" |
| 609 | * - "Operation" - "Pipe Control" |
| 610 | * |
| 611 | * ex) if pipe6 - pipe9 are USB_ENDPOINT_XFER_INT (SH7724) |
| 612 | * |
| 613 | * BUFNMB: PIPE |
| 614 | * 0: pipe0 (DCP 256byte) |
| 615 | * 1: - |
| 616 | * 2: - |
| 617 | * 3: - |
| 618 | * 4: pipe6 (INT 64byte) |
| 619 | * 5: pipe7 (INT 64byte) |
| 620 | * 6: pipe8 (INT 64byte) |
| 621 | * 7: pipe9 (INT 64byte) |
| 622 | * 8 - xx: free (for BULK, ISOC) |
| 623 | */ |
| 624 | |
| 625 | /* |
| 626 | * FIXME |
| 627 | * |
| 628 | * it doesn't have good buffer allocator |
| 629 | * |
| 630 | * DCP : 256 byte |
| 631 | * BULK: 512 byte |
| 632 | * INT : 64 byte |
| 633 | * ISOC: 512 byte |
| 634 | */ |
| 635 | if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_CONTROL)) |
| 636 | buff_size = 256; |
| 637 | else if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) |
| 638 | buff_size = 64; |
| 639 | else |
| 640 | buff_size = 512; |
| 641 | |
| 642 | /* change buff_size to register value */ |
| 643 | bufnmb_cnt = (buff_size / 64) - 1; |
| 644 | |
| 645 | /* BUFNMB has been reserved for INT pipe |
| 646 | * see above */ |
| 647 | if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) { |
| 648 | bufnmb = pipe_num - 2; |
| 649 | } else { |
| 650 | bufnmb = info->bufnmb_last; |
| 651 | info->bufnmb_last += bufnmb_cnt + 1; |
| 652 | |
| 653 | /* |
| 654 | * double buffer |
| 655 | */ |
| 656 | if (is_double) |
| 657 | info->bufnmb_last += bufnmb_cnt + 1; |
| 658 | } |
| 659 | |
| 660 | dev_dbg(dev, "pipe : %d : buff_size 0x%x: bufnmb 0x%x\n", |
| 661 | pipe_num, buff_size, bufnmb); |
| 662 | |
| 663 | return (0x1f & bufnmb_cnt) << 10 | |
| 664 | (0xff & bufnmb) << 0; |
| 665 | } |
| 666 | |
| 667 | /* |
| 668 | * pipe control |
| 669 | */ |
| 670 | int usbhs_pipe_get_maxpacket(struct usbhs_pipe *pipe) |
| 671 | { |
| 672 | u16 mask = usbhsp_is_dcp(pipe) ? DCP_MAXP_MASK : PIPE_MAXP_MASK; |
| 673 | |
| 674 | usbhsp_pipe_select(pipe); |
| 675 | |
| 676 | return (int)(usbhsp_pipe_maxp_get(pipe) & mask); |
| 677 | } |
| 678 | |
| 679 | int usbhs_pipe_is_dir_in(struct usbhs_pipe *pipe) |
| 680 | { |
| 681 | return usbhsp_flags_has(pipe, IS_DIR_IN); |
| 682 | } |
| 683 | |
| 684 | void usbhs_pipe_clear_sequence(struct usbhs_pipe *pipe) |
| 685 | { |
| 686 | usbhsp_pipectrl_set(pipe, SQCLR, SQCLR); |
| 687 | } |
| 688 | |
| 689 | static struct usbhs_pipe *usbhsp_get_pipe(struct usbhs_priv *priv, u32 type) |
| 690 | { |
| 691 | struct usbhs_pipe *pos, *pipe; |
| 692 | int i; |
| 693 | |
| 694 | /* |
| 695 | * find target pipe |
| 696 | */ |
| 697 | pipe = NULL; |
| 698 | usbhs_for_each_pipe_with_dcp(pos, priv, i) { |
| 699 | if (!usbhsp_type_is(pos, type)) |
| 700 | continue; |
| 701 | if (usbhsp_flags_has(pos, IS_USED)) |
| 702 | continue; |
| 703 | |
| 704 | pipe = pos; |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | if (!pipe) |
| 709 | return NULL; |
| 710 | |
| 711 | /* |
| 712 | * initialize pipe flags |
| 713 | */ |
| 714 | usbhsp_flags_init(pipe); |
| 715 | usbhsp_flags_set(pipe, IS_USED); |
| 716 | |
| 717 | return pipe; |
| 718 | } |
| 719 | |
| 720 | void usbhs_pipe_init(struct usbhs_priv *priv) |
| 721 | { |
| 722 | struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv); |
| 723 | struct usbhs_pipe *pipe; |
| 724 | int i; |
| 725 | |
| 726 | /* |
| 727 | * FIXME |
| 728 | * |
| 729 | * driver needs good allocator. |
| 730 | * |
| 731 | * find first free buffer area (BULK, ISOC) |
| 732 | * (DCP, INT area is fixed) |
| 733 | * |
| 734 | * buffer number 0 - 3 have been reserved for DCP |
| 735 | * see |
| 736 | * usbhsp_to_bufnmb |
| 737 | */ |
| 738 | info->bufnmb_last = 4; |
| 739 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 740 | if (usbhsp_type_is(pipe, USB_ENDPOINT_XFER_INT)) |
| 741 | info->bufnmb_last++; |
| 742 | |
| 743 | usbhsp_flags_init(pipe); |
| 744 | pipe->mod_private = NULL; |
Kuninori Morimoto | 45e13e6 | 2011-04-21 14:09:58 +0900 | [diff] [blame] | 745 | |
| 746 | usbhsp_fifo_clear(pipe); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
| 750 | struct usbhs_pipe *usbhs_pipe_malloc(struct usbhs_priv *priv, |
| 751 | const struct usb_endpoint_descriptor *desc) |
| 752 | { |
| 753 | struct device *dev = usbhs_priv_to_dev(priv); |
| 754 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); |
| 755 | struct usbhs_pipe *pipe; |
| 756 | int is_host = usbhs_mod_is_host(priv, mod); |
| 757 | int ret; |
| 758 | u16 pipecfg, pipebuf, pipemaxp; |
| 759 | |
| 760 | pipe = usbhsp_get_pipe(priv, usb_endpoint_type(desc)); |
Kuninori Morimoto | f429ea3 | 2011-04-21 14:10:24 +0900 | [diff] [blame^] | 761 | if (!pipe) { |
| 762 | dev_err(dev, "can't get pipe (%s)\n", |
| 763 | usbhsp_pipe_name[usb_endpoint_type(desc)]); |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 764 | return NULL; |
Kuninori Morimoto | f429ea3 | 2011-04-21 14:10:24 +0900 | [diff] [blame^] | 765 | } |
Kuninori Morimoto | f1407d5 | 2011-04-04 13:44:59 +0900 | [diff] [blame] | 766 | |
| 767 | usbhs_fifo_disable(pipe); |
| 768 | |
| 769 | /* make sure pipe is not busy */ |
| 770 | ret = usbhsp_pipe_barrier(pipe); |
| 771 | if (ret < 0) { |
| 772 | dev_err(dev, "pipe setup failed %d\n", usbhs_pipe_number(pipe)); |
| 773 | return NULL; |
| 774 | } |
| 775 | |
| 776 | pipecfg = usbhsp_setup_pipecfg(pipe, desc, is_host); |
| 777 | pipebuf = usbhsp_setup_pipebuff(pipe, desc, is_host); |
| 778 | pipemaxp = usbhsp_setup_pipemaxp(pipe, desc, is_host); |
| 779 | |
| 780 | /* buffer clear |
| 781 | * see PIPECFG :: BFRE */ |
| 782 | usbhsp_pipectrl_set(pipe, ACLRM, ACLRM); |
| 783 | usbhsp_pipectrl_set(pipe, ACLRM, 0); |
| 784 | |
| 785 | usbhsp_pipe_select(pipe); |
| 786 | usbhsp_pipe_cfg_set(pipe, 0xFFFF, pipecfg); |
| 787 | usbhsp_pipe_buf_set(pipe, 0xFFFF, pipebuf); |
| 788 | usbhsp_pipe_maxp_set(pipe, 0xFFFF, pipemaxp); |
| 789 | |
| 790 | usbhs_pipe_clear_sequence(pipe); |
| 791 | |
| 792 | dev_dbg(dev, "enable pipe %d : %s (%s)\n", |
| 793 | usbhs_pipe_number(pipe), |
| 794 | usbhsp_pipe_name[usb_endpoint_type(desc)], |
| 795 | usbhs_pipe_is_dir_in(pipe) ? "in" : "out"); |
| 796 | |
| 797 | return pipe; |
| 798 | } |
| 799 | |
| 800 | /* |
| 801 | * dcp control |
| 802 | */ |
| 803 | struct usbhs_pipe *usbhs_dcp_malloc(struct usbhs_priv *priv) |
| 804 | { |
| 805 | struct usbhs_pipe *pipe; |
| 806 | |
| 807 | pipe = usbhsp_get_pipe(priv, USB_ENDPOINT_XFER_CONTROL); |
| 808 | if (!pipe) |
| 809 | return NULL; |
| 810 | |
| 811 | /* |
| 812 | * dcpcfg : default |
| 813 | * dcpmaxp : default |
| 814 | * pipebuf : nothing to do |
| 815 | */ |
| 816 | |
| 817 | usbhsp_pipe_select(pipe); |
| 818 | usbhs_pipe_clear_sequence(pipe); |
| 819 | |
| 820 | return pipe; |
| 821 | } |
| 822 | |
| 823 | void usbhs_dcp_control_transfer_done(struct usbhs_pipe *pipe) |
| 824 | { |
| 825 | WARN_ON(!usbhsp_is_dcp(pipe)); |
| 826 | |
| 827 | usbhs_fifo_enable(pipe); |
| 828 | usbhsp_pipectrl_set(pipe, CCPL, CCPL); |
| 829 | } |
| 830 | |
| 831 | |
| 832 | /* |
| 833 | * pipe module function |
| 834 | */ |
| 835 | int usbhs_pipe_probe(struct usbhs_priv *priv) |
| 836 | { |
| 837 | struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv); |
| 838 | struct usbhs_pipe *pipe; |
| 839 | struct device *dev = usbhs_priv_to_dev(priv); |
| 840 | u32 *pipe_type = usbhs_get_dparam(priv, pipe_type); |
| 841 | int pipe_size = usbhs_get_dparam(priv, pipe_size); |
| 842 | int i; |
| 843 | |
| 844 | /* This driver expects 1st pipe is DCP */ |
| 845 | if (pipe_type[0] != USB_ENDPOINT_XFER_CONTROL) { |
| 846 | dev_err(dev, "1st PIPE is not DCP\n"); |
| 847 | return -EINVAL; |
| 848 | } |
| 849 | |
| 850 | info->pipe = kzalloc(sizeof(struct usbhs_pipe) * pipe_size, GFP_KERNEL); |
| 851 | if (!info->pipe) { |
| 852 | dev_err(dev, "Could not allocate pipe\n"); |
| 853 | return -ENOMEM; |
| 854 | } |
| 855 | |
| 856 | info->size = pipe_size; |
| 857 | |
| 858 | /* |
| 859 | * init pipe |
| 860 | */ |
| 861 | usbhs_for_each_pipe_with_dcp(pipe, priv, i) { |
| 862 | pipe->priv = priv; |
| 863 | usbhsp_type(pipe) = pipe_type[i] & USB_ENDPOINT_XFERTYPE_MASK; |
| 864 | |
| 865 | dev_dbg(dev, "pipe %x\t: %s\n", |
| 866 | i, usbhsp_pipe_name[pipe_type[i]]); |
| 867 | } |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | void usbhs_pipe_remove(struct usbhs_priv *priv) |
| 873 | { |
| 874 | struct usbhs_pipe_info *info = usbhsp_priv_to_pipeinfo(priv); |
| 875 | |
| 876 | kfree(info->pipe); |
| 877 | } |