Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 1 | /* |
| 2 | * R8A66597 HCD (Host Controller Driver) for u-boot |
| 3 | * |
| 4 | * Copyright (C) 2008 Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com> |
| 5 | * |
Simon Glass | e62b526 | 2015-07-06 16:47:42 -0600 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0 |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <usb.h> |
| 11 | #include <asm/io.h> |
| 12 | |
| 13 | #include "r8a66597.h" |
| 14 | |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 15 | #ifdef R8A66597_DEBUG |
| 16 | #define R8A66597_DPRINT printf |
| 17 | #else |
| 18 | #define R8A66597_DPRINT(...) |
| 19 | #endif |
| 20 | |
| 21 | static const char hcd_name[] = "r8a66597_hcd"; |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 22 | static struct r8a66597 gr8a66597; |
| 23 | |
Yoshihiro Shimoda | 60ece6d | 2008-10-29 20:05:18 +0900 | [diff] [blame] | 24 | static void get_hub_data(struct usb_device *dev, u16 *hub_devnum, u16 *hubport) |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 25 | { |
Yoshihiro Shimoda | 60ece6d | 2008-10-29 20:05:18 +0900 | [diff] [blame] | 26 | int i; |
| 27 | |
| 28 | *hub_devnum = 0; |
| 29 | *hubport = 0; |
| 30 | |
| 31 | /* check a device connected to root_hub */ |
| 32 | if ((dev->parent && dev->parent->devnum == 1) || |
| 33 | (dev->devnum == 1)) |
| 34 | return; |
| 35 | |
| 36 | for (i = 0; i < USB_MAXCHILDREN; i++) { |
| 37 | if (dev->parent->children[i] == dev) { |
| 38 | *hub_devnum = (u8)dev->parent->devnum; |
| 39 | *hubport = i; |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | printf("get_hub_data error.\n"); |
| 45 | } |
| 46 | |
| 47 | static void set_devadd(struct r8a66597 *r8a66597, u8 r8a66597_address, |
| 48 | struct usb_device *dev, int port) |
| 49 | { |
| 50 | u16 val, usbspd, upphub, hubport; |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 51 | unsigned long devadd_reg = get_devadd_addr(r8a66597_address); |
| 52 | |
Yoshihiro Shimoda | 60ece6d | 2008-10-29 20:05:18 +0900 | [diff] [blame] | 53 | get_hub_data(dev, &upphub, &hubport); |
| 54 | usbspd = r8a66597->speed; |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 55 | val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001); |
| 56 | r8a66597_write(r8a66597, val, devadd_reg); |
| 57 | } |
| 58 | |
| 59 | static int r8a66597_clock_enable(struct r8a66597 *r8a66597) |
| 60 | { |
| 61 | u16 tmp; |
| 62 | int i = 0; |
| 63 | |
| 64 | #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) |
| 65 | do { |
| 66 | r8a66597_write(r8a66597, SCKE, SYSCFG0); |
| 67 | tmp = r8a66597_read(r8a66597, SYSCFG0); |
| 68 | if (i++ > 1000) { |
| 69 | printf("register access fail.\n"); |
| 70 | return -1; |
| 71 | } |
| 72 | } while ((tmp & SCKE) != SCKE); |
| 73 | r8a66597_write(r8a66597, 0x04, 0x02); |
| 74 | #else |
| 75 | do { |
| 76 | r8a66597_write(r8a66597, USBE, SYSCFG0); |
| 77 | tmp = r8a66597_read(r8a66597, SYSCFG0); |
| 78 | if (i++ > 1000) { |
| 79 | printf("register access fail.\n"); |
| 80 | return -1; |
| 81 | } |
| 82 | } while ((tmp & USBE) != USBE); |
| 83 | r8a66597_bclr(r8a66597, USBE, SYSCFG0); |
Nobuhiro Iwamatsu | 9d03420 | 2012-03-20 20:23:29 +0000 | [diff] [blame] | 84 | r8a66597_mdfy(r8a66597, CONFIG_R8A66597_XTAL, XTAL, SYSCFG0); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 85 | |
| 86 | i = 0; |
| 87 | r8a66597_bset(r8a66597, XCKE, SYSCFG0); |
| 88 | do { |
| 89 | udelay(1000); |
| 90 | tmp = r8a66597_read(r8a66597, SYSCFG0); |
| 91 | if (i++ > 500) { |
| 92 | printf("register access fail.\n"); |
| 93 | return -1; |
| 94 | } |
| 95 | } while ((tmp & SCKE) != SCKE); |
| 96 | #endif /* #if defined(CONFIG_SUPERH_ON_CHIP_R8A66597) */ |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void r8a66597_clock_disable(struct r8a66597 *r8a66597) |
| 102 | { |
| 103 | r8a66597_bclr(r8a66597, SCKE, SYSCFG0); |
| 104 | udelay(1); |
| 105 | #if !defined(CONFIG_SUPERH_ON_CHIP_R8A66597) |
| 106 | r8a66597_bclr(r8a66597, PLLC, SYSCFG0); |
| 107 | r8a66597_bclr(r8a66597, XCKE, SYSCFG0); |
| 108 | r8a66597_bclr(r8a66597, USBE, SYSCFG0); |
| 109 | #endif |
| 110 | } |
| 111 | |
| 112 | static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port) |
| 113 | { |
| 114 | u16 val; |
| 115 | |
| 116 | val = port ? DRPD : DCFM | DRPD; |
| 117 | r8a66597_bset(r8a66597, val, get_syscfg_reg(port)); |
| 118 | r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port)); |
| 119 | |
| 120 | r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port)); |
| 121 | } |
| 122 | |
| 123 | static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port) |
| 124 | { |
| 125 | u16 val, tmp; |
| 126 | |
| 127 | r8a66597_write(r8a66597, 0, get_intenb_reg(port)); |
| 128 | r8a66597_write(r8a66597, 0, get_intsts_reg(port)); |
| 129 | |
| 130 | r8a66597_port_power(r8a66597, port, 0); |
| 131 | |
| 132 | do { |
| 133 | tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS; |
| 134 | udelay(640); |
| 135 | } while (tmp == EDGESTS); |
| 136 | |
| 137 | val = port ? DRPD : DCFM | DRPD; |
| 138 | r8a66597_bclr(r8a66597, val, get_syscfg_reg(port)); |
| 139 | r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port)); |
| 140 | } |
| 141 | |
| 142 | static int enable_controller(struct r8a66597 *r8a66597) |
| 143 | { |
| 144 | int ret, port; |
| 145 | |
| 146 | ret = r8a66597_clock_enable(r8a66597); |
| 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | |
Nobuhiro Iwamatsu | 9d03420 | 2012-03-20 20:23:29 +0000 | [diff] [blame] | 150 | r8a66597_bset(r8a66597, CONFIG_R8A66597_LDRV & LDRV, PINCFG); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 151 | r8a66597_bset(r8a66597, USBE, SYSCFG0); |
| 152 | |
| 153 | r8a66597_bset(r8a66597, INTL, SOFCFG); |
| 154 | r8a66597_write(r8a66597, 0, INTENB0); |
yasuhisa umano | 8ecdce7 | 2014-04-18 11:33:08 +0900 | [diff] [blame] | 155 | for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) |
| 156 | r8a66597_write(r8a66597, 0, get_intenb_reg(port)); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 157 | |
Nobuhiro Iwamatsu | 9d03420 | 2012-03-20 20:23:29 +0000 | [diff] [blame] | 158 | r8a66597_bset(r8a66597, CONFIG_R8A66597_ENDIAN & BIGEND, CFIFOSEL); |
| 159 | r8a66597_bset(r8a66597, CONFIG_R8A66597_ENDIAN & BIGEND, D0FIFOSEL); |
| 160 | r8a66597_bset(r8a66597, CONFIG_R8A66597_ENDIAN & BIGEND, D1FIFOSEL); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 161 | r8a66597_bset(r8a66597, TRNENSEL, SOFCFG); |
| 162 | |
| 163 | for (port = 0; port < R8A66597_MAX_ROOT_HUB; port++) |
| 164 | r8a66597_enable_port(r8a66597, port); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static void disable_controller(struct r8a66597 *r8a66597) |
| 170 | { |
| 171 | int i; |
| 172 | |
| 173 | if (!(r8a66597_read(r8a66597, SYSCFG0) & USBE)) |
| 174 | return; |
| 175 | |
| 176 | r8a66597_write(r8a66597, 0, INTENB0); |
| 177 | r8a66597_write(r8a66597, 0, INTSTS0); |
| 178 | |
| 179 | r8a66597_write(r8a66597, 0, D0FIFOSEL); |
| 180 | r8a66597_write(r8a66597, 0, D1FIFOSEL); |
| 181 | r8a66597_write(r8a66597, 0, DCPCFG); |
| 182 | r8a66597_write(r8a66597, 0x40, DCPMAXP); |
| 183 | r8a66597_write(r8a66597, 0, DCPCTR); |
| 184 | |
| 185 | for (i = 0; i <= 10; i++) |
| 186 | r8a66597_write(r8a66597, 0, get_devadd_addr(i)); |
| 187 | for (i = 1; i <= 5; i++) { |
| 188 | r8a66597_write(r8a66597, 0, get_pipetre_addr(i)); |
| 189 | r8a66597_write(r8a66597, 0, get_pipetrn_addr(i)); |
| 190 | } |
| 191 | for (i = 1; i < R8A66597_MAX_NUM_PIPE; i++) { |
| 192 | r8a66597_write(r8a66597, 0, get_pipectr_addr(i)); |
| 193 | r8a66597_write(r8a66597, i, PIPESEL); |
| 194 | r8a66597_write(r8a66597, 0, PIPECFG); |
| 195 | r8a66597_write(r8a66597, 0, PIPEBUF); |
| 196 | r8a66597_write(r8a66597, 0, PIPEMAXP); |
| 197 | r8a66597_write(r8a66597, 0, PIPEPERI); |
| 198 | } |
| 199 | |
| 200 | for (i = 0; i < R8A66597_MAX_ROOT_HUB; i++) |
| 201 | r8a66597_disable_port(r8a66597, i); |
| 202 | |
| 203 | r8a66597_clock_disable(r8a66597); |
| 204 | } |
| 205 | |
| 206 | static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg, |
| 207 | u16 mask, u16 loop) |
| 208 | { |
| 209 | u16 tmp; |
| 210 | int i = 0; |
| 211 | |
| 212 | do { |
| 213 | tmp = r8a66597_read(r8a66597, reg); |
| 214 | if (i++ > 1000000) { |
| 215 | printf("register%lx, loop %x is timeout\n", reg, loop); |
| 216 | break; |
| 217 | } |
| 218 | } while ((tmp & mask) != loop); |
| 219 | } |
| 220 | |
| 221 | static void pipe_buffer_setting(struct r8a66597 *r8a66597, |
| 222 | struct usb_device *dev, unsigned long pipe) |
| 223 | { |
| 224 | u16 val = 0; |
| 225 | u16 pipenum, bufnum, maxpacket; |
| 226 | |
| 227 | if (usb_pipein(pipe)) { |
| 228 | pipenum = BULK_IN_PIPENUM; |
| 229 | bufnum = BULK_IN_BUFNUM; |
| 230 | maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)]; |
| 231 | } else { |
| 232 | pipenum = BULK_OUT_PIPENUM; |
| 233 | bufnum = BULK_OUT_BUFNUM; |
| 234 | maxpacket = dev->epmaxpacketout[usb_pipeendpoint(pipe)]; |
| 235 | } |
| 236 | |
| 237 | if (r8a66597->pipe_config & (1 << pipenum)) |
| 238 | return; |
| 239 | r8a66597->pipe_config |= (1 << pipenum); |
| 240 | |
| 241 | r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(pipenum)); |
| 242 | r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(pipenum)); |
| 243 | r8a66597_write(r8a66597, pipenum, PIPESEL); |
| 244 | |
| 245 | /* FIXME: This driver support bulk transfer only. */ |
| 246 | if (!usb_pipein(pipe)) |
| 247 | val |= R8A66597_DIR; |
| 248 | else |
| 249 | val |= R8A66597_SHTNAK; |
| 250 | val |= R8A66597_BULK | R8A66597_DBLB | usb_pipeendpoint(pipe); |
| 251 | r8a66597_write(r8a66597, val, PIPECFG); |
| 252 | |
| 253 | r8a66597_write(r8a66597, (8 << 10) | bufnum, PIPEBUF); |
| 254 | r8a66597_write(r8a66597, make_devsel(usb_pipedevice(pipe)) | |
| 255 | maxpacket, PIPEMAXP); |
| 256 | r8a66597_write(r8a66597, 0, PIPEPERI); |
| 257 | r8a66597_write(r8a66597, SQCLR, get_pipectr_addr(pipenum)); |
| 258 | } |
| 259 | |
| 260 | static int send_setup_packet(struct r8a66597 *r8a66597, struct usb_device *dev, |
| 261 | struct devrequest *setup) |
| 262 | { |
| 263 | int i; |
| 264 | unsigned short *p = (unsigned short *)setup; |
| 265 | unsigned long setup_addr = USBREQ; |
| 266 | u16 intsts1; |
| 267 | int timeout = 3000; |
| 268 | u16 devsel = setup->request == USB_REQ_SET_ADDRESS ? 0 : dev->devnum; |
| 269 | |
| 270 | r8a66597_write(r8a66597, make_devsel(devsel) | |
| 271 | (8 << dev->maxpacketsize), DCPMAXP); |
| 272 | r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1); |
| 273 | |
| 274 | for (i = 0; i < 4; i++) { |
| 275 | r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr); |
| 276 | setup_addr += 2; |
| 277 | } |
| 278 | r8a66597_write(r8a66597, ~0x0001, BRDYSTS); |
| 279 | r8a66597_write(r8a66597, SUREQ, DCPCTR); |
| 280 | |
| 281 | while (1) { |
| 282 | intsts1 = r8a66597_read(r8a66597, INTSTS1); |
| 283 | if (intsts1 & SACK) |
| 284 | break; |
| 285 | if (intsts1 & SIGN) { |
| 286 | printf("setup packet send error\n"); |
| 287 | return -1; |
| 288 | } |
| 289 | if (timeout-- < 0) { |
| 290 | printf("setup packet timeout\n"); |
| 291 | return -1; |
| 292 | } |
| 293 | udelay(500); |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int send_bulk_packet(struct r8a66597 *r8a66597, struct usb_device *dev, |
| 300 | unsigned long pipe, void *buffer, int transfer_len) |
| 301 | { |
| 302 | u16 tmp, bufsize; |
| 303 | u16 *buf; |
| 304 | size_t size; |
| 305 | |
| 306 | R8A66597_DPRINT("%s\n", __func__); |
| 307 | |
| 308 | r8a66597_mdfy(r8a66597, MBW | BULK_OUT_PIPENUM, |
| 309 | MBW | CURPIPE, CFIFOSEL); |
| 310 | r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, BULK_OUT_PIPENUM); |
| 311 | tmp = r8a66597_read(r8a66597, CFIFOCTR); |
| 312 | if ((tmp & FRDY) == 0) { |
| 313 | printf("%s FRDY is not set (%x)\n", __func__, tmp); |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | /* prepare parameters */ |
| 318 | bufsize = dev->epmaxpacketout[usb_pipeendpoint(pipe)]; |
| 319 | buf = (u16 *)(buffer + dev->act_len); |
| 320 | size = min((int)bufsize, transfer_len - dev->act_len); |
| 321 | |
| 322 | /* write fifo */ |
| 323 | r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS); |
| 324 | if (buffer) { |
| 325 | r8a66597_write_fifo(r8a66597, CFIFO, buf, size); |
| 326 | r8a66597_write(r8a66597, BVAL, CFIFOCTR); |
| 327 | } |
| 328 | |
| 329 | /* update parameters */ |
| 330 | dev->act_len += size; |
| 331 | |
| 332 | r8a66597_mdfy(r8a66597, PID_BUF, PID, |
| 333 | get_pipectr_addr(BULK_OUT_PIPENUM)); |
| 334 | |
| 335 | while (!(r8a66597_read(r8a66597, BEMPSTS) & (1 << BULK_OUT_PIPENUM))) |
| 336 | if (ctrlc()) |
| 337 | return -1; |
| 338 | r8a66597_write(r8a66597, ~(1 << BULK_OUT_PIPENUM), BEMPSTS); |
| 339 | |
| 340 | if (dev->act_len >= transfer_len) |
| 341 | r8a66597_mdfy(r8a66597, PID_NAK, PID, |
| 342 | get_pipectr_addr(BULK_OUT_PIPENUM)); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static int receive_bulk_packet(struct r8a66597 *r8a66597, |
| 348 | struct usb_device *dev, |
| 349 | unsigned long pipe, |
| 350 | void *buffer, int transfer_len) |
| 351 | { |
| 352 | u16 tmp; |
| 353 | u16 *buf; |
| 354 | const u16 pipenum = BULK_IN_PIPENUM; |
| 355 | int rcv_len; |
| 356 | int maxpacket = dev->epmaxpacketin[usb_pipeendpoint(pipe)]; |
| 357 | |
| 358 | R8A66597_DPRINT("%s\n", __func__); |
| 359 | |
| 360 | /* prepare */ |
| 361 | if (dev->act_len == 0) { |
| 362 | r8a66597_mdfy(r8a66597, PID_NAK, PID, |
| 363 | get_pipectr_addr(pipenum)); |
| 364 | r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS); |
| 365 | |
| 366 | r8a66597_write(r8a66597, TRCLR, get_pipetre_addr(pipenum)); |
| 367 | r8a66597_write(r8a66597, |
| 368 | (transfer_len + maxpacket - 1) / maxpacket, |
| 369 | get_pipetrn_addr(pipenum)); |
| 370 | r8a66597_bset(r8a66597, TRENB, get_pipetre_addr(pipenum)); |
| 371 | |
| 372 | r8a66597_mdfy(r8a66597, PID_BUF, PID, |
| 373 | get_pipectr_addr(pipenum)); |
| 374 | } |
| 375 | |
| 376 | r8a66597_mdfy(r8a66597, MBW | pipenum, MBW | CURPIPE, CFIFOSEL); |
| 377 | r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum); |
| 378 | |
| 379 | while (!(r8a66597_read(r8a66597, BRDYSTS) & (1 << pipenum))) |
| 380 | if (ctrlc()) |
| 381 | return -1; |
| 382 | r8a66597_write(r8a66597, ~(1 << pipenum), BRDYSTS); |
| 383 | |
| 384 | tmp = r8a66597_read(r8a66597, CFIFOCTR); |
| 385 | if ((tmp & FRDY) == 0) { |
| 386 | printf("%s FRDY is not set. (%x)\n", __func__, tmp); |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | buf = (u16 *)(buffer + dev->act_len); |
| 391 | rcv_len = tmp & DTLN; |
| 392 | dev->act_len += rcv_len; |
| 393 | |
| 394 | if (buffer) { |
| 395 | if (rcv_len == 0) |
| 396 | r8a66597_write(r8a66597, BCLR, CFIFOCTR); |
| 397 | else |
| 398 | r8a66597_read_fifo(r8a66597, CFIFO, buf, rcv_len); |
| 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static int receive_control_packet(struct r8a66597 *r8a66597, |
| 405 | struct usb_device *dev, |
| 406 | void *buffer, int transfer_len) |
| 407 | { |
| 408 | u16 tmp; |
| 409 | int rcv_len; |
| 410 | |
| 411 | /* FIXME: limit transfer size : 64byte or less */ |
| 412 | |
| 413 | r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG); |
| 414 | r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL); |
| 415 | r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0); |
| 416 | r8a66597_bset(r8a66597, SQSET, DCPCTR); |
| 417 | r8a66597_write(r8a66597, BCLR, CFIFOCTR); |
| 418 | r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR); |
| 419 | |
| 420 | while (!(r8a66597_read(r8a66597, BRDYSTS) & 0x0001)) |
| 421 | if (ctrlc()) |
| 422 | return -1; |
| 423 | r8a66597_write(r8a66597, ~0x0001, BRDYSTS); |
| 424 | |
| 425 | r8a66597_mdfy(r8a66597, MBW, MBW | CURPIPE, CFIFOSEL); |
| 426 | r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0); |
| 427 | |
| 428 | tmp = r8a66597_read(r8a66597, CFIFOCTR); |
| 429 | if ((tmp & FRDY) == 0) { |
| 430 | printf("%s FRDY is not set. (%x)\n", __func__, tmp); |
| 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | rcv_len = tmp & DTLN; |
| 435 | dev->act_len += rcv_len; |
| 436 | |
| 437 | r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR); |
| 438 | |
| 439 | if (buffer) { |
| 440 | if (rcv_len == 0) |
| 441 | r8a66597_write(r8a66597, BCLR, DCPCTR); |
| 442 | else |
| 443 | r8a66597_read_fifo(r8a66597, CFIFO, buffer, rcv_len); |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int send_status_packet(struct r8a66597 *r8a66597, |
| 450 | unsigned long pipe) |
| 451 | { |
| 452 | r8a66597_bset(r8a66597, SQSET, DCPCTR); |
| 453 | r8a66597_mdfy(r8a66597, PID_NAK, PID, DCPCTR); |
| 454 | |
| 455 | if (usb_pipein(pipe)) { |
| 456 | r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG); |
| 457 | r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL); |
| 458 | r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0); |
| 459 | r8a66597_write(r8a66597, ~BEMP0, BEMPSTS); |
| 460 | r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR); |
| 461 | } else { |
| 462 | r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG); |
| 463 | r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL); |
| 464 | r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0); |
| 465 | r8a66597_write(r8a66597, BCLR, CFIFOCTR); |
| 466 | } |
| 467 | r8a66597_mdfy(r8a66597, PID_BUF, PID, DCPCTR); |
| 468 | |
| 469 | while (!(r8a66597_read(r8a66597, BEMPSTS) & 0x0001)) |
| 470 | if (ctrlc()) |
| 471 | return -1; |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port) |
| 477 | { |
| 478 | int count = R8A66597_MAX_SAMPLING; |
| 479 | unsigned short syssts, old_syssts; |
| 480 | |
| 481 | R8A66597_DPRINT("%s\n", __func__); |
| 482 | |
| 483 | old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST); |
| 484 | while (count > 0) { |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 485 | mdelay(R8A66597_RH_POLL_TIME); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 486 | |
| 487 | syssts = r8a66597_read(r8a66597, get_syssts_reg(port) & LNST); |
| 488 | if (syssts == old_syssts) { |
| 489 | count--; |
| 490 | } else { |
| 491 | count = R8A66597_MAX_SAMPLING; |
| 492 | old_syssts = syssts; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | static void r8a66597_bus_reset(struct r8a66597 *r8a66597, int port) |
| 498 | { |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 499 | mdelay(10); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 500 | r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT, get_dvstctr_reg(port)); |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 501 | mdelay(50); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 502 | r8a66597_mdfy(r8a66597, UACT, USBRST | UACT, get_dvstctr_reg(port)); |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 503 | mdelay(50); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static int check_usb_device_connecting(struct r8a66597 *r8a66597) |
| 507 | { |
| 508 | int timeout = 10000; /* 100usec * 10000 = 1sec */ |
| 509 | int i; |
| 510 | |
| 511 | for (i = 0; i < 5; i++) { |
| 512 | /* check a usb cable connect */ |
| 513 | while (!(r8a66597_read(r8a66597, INTSTS1) & ATTCH)) { |
| 514 | if (timeout-- < 0) { |
| 515 | printf("%s timeout.\n", __func__); |
| 516 | return -1; |
| 517 | } |
| 518 | udelay(100); |
| 519 | } |
| 520 | |
| 521 | /* check a data line */ |
| 522 | r8a66597_check_syssts(r8a66597, 0); |
| 523 | |
| 524 | r8a66597_bus_reset(r8a66597, 0); |
| 525 | r8a66597->speed = get_rh_usb_speed(r8a66597, 0); |
| 526 | |
| 527 | if (!(r8a66597_read(r8a66597, INTSTS1) & DTCH)) { |
| 528 | r8a66597->port_change = USB_PORT_STAT_C_CONNECTION; |
| 529 | r8a66597->port_status = USB_PORT_STAT_CONNECTION | |
| 530 | USB_PORT_STAT_ENABLE; |
| 531 | return 0; /* success */ |
| 532 | } |
| 533 | |
| 534 | R8A66597_DPRINT("USB device has detached. retry = %d\n", i); |
| 535 | r8a66597_write(r8a66597, ~DTCH, INTSTS1); |
| 536 | } |
| 537 | |
| 538 | return -1; /* fail */ |
| 539 | } |
| 540 | |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 541 | /*-------------------------------------------------------------------------* |
| 542 | * Virtual Root Hub |
| 543 | *-------------------------------------------------------------------------*/ |
| 544 | |
Stephen Warren | eb838e7 | 2014-02-13 21:15:18 -0700 | [diff] [blame] | 545 | #include <usbroothubdes.h> |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 546 | |
| 547 | static int r8a66597_submit_rh_msg(struct usb_device *dev, unsigned long pipe, |
| 548 | void *buffer, int transfer_len, struct devrequest *cmd) |
| 549 | { |
| 550 | struct r8a66597 *r8a66597 = &gr8a66597; |
| 551 | int leni = transfer_len; |
| 552 | int len = 0; |
| 553 | int stat = 0; |
| 554 | __u16 bmRType_bReq; |
| 555 | __u16 wValue; |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 556 | __u16 wLength; |
| 557 | unsigned char data[32]; |
| 558 | |
| 559 | R8A66597_DPRINT("%s\n", __func__); |
| 560 | |
Remy Bohmer | 9dbc366 | 2008-10-10 10:23:22 +0200 | [diff] [blame] | 561 | if (usb_pipeint(pipe)) { |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 562 | printf("Root-Hub submit IRQ: NOT implemented"); |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | bmRType_bReq = cmd->requesttype | (cmd->request << 8); |
| 567 | wValue = cpu_to_le16 (cmd->value); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 568 | wLength = cpu_to_le16 (cmd->length); |
| 569 | |
| 570 | switch (bmRType_bReq) { |
| 571 | case RH_GET_STATUS: |
| 572 | *(__u16 *)buffer = cpu_to_le16(1); |
| 573 | len = 2; |
| 574 | break; |
| 575 | case RH_GET_STATUS | RH_INTERFACE: |
| 576 | *(__u16 *)buffer = cpu_to_le16(0); |
| 577 | len = 2; |
| 578 | break; |
| 579 | case RH_GET_STATUS | RH_ENDPOINT: |
| 580 | *(__u16 *)buffer = cpu_to_le16(0); |
| 581 | len = 2; |
| 582 | break; |
| 583 | case RH_GET_STATUS | RH_CLASS: |
| 584 | *(__u32 *)buffer = cpu_to_le32(0); |
| 585 | len = 4; |
| 586 | break; |
| 587 | case RH_GET_STATUS | RH_OTHER | RH_CLASS: |
| 588 | *(__u32 *)buffer = cpu_to_le32(r8a66597->port_status | |
| 589 | (r8a66597->port_change << 16)); |
| 590 | len = 4; |
| 591 | break; |
| 592 | case RH_CLEAR_FEATURE | RH_ENDPOINT: |
| 593 | case RH_CLEAR_FEATURE | RH_CLASS: |
| 594 | break; |
| 595 | |
| 596 | case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS: |
| 597 | switch (wValue) { |
| 598 | case RH_C_PORT_CONNECTION: |
| 599 | r8a66597->port_change &= ~USB_PORT_STAT_C_CONNECTION; |
| 600 | break; |
| 601 | } |
| 602 | break; |
| 603 | |
| 604 | case RH_SET_FEATURE | RH_OTHER | RH_CLASS: |
| 605 | switch (wValue) { |
| 606 | case (RH_PORT_SUSPEND): |
| 607 | break; |
| 608 | case (RH_PORT_RESET): |
| 609 | r8a66597_bus_reset(r8a66597, 0); |
| 610 | break; |
| 611 | case (RH_PORT_POWER): |
| 612 | break; |
| 613 | case (RH_PORT_ENABLE): |
| 614 | break; |
| 615 | } |
| 616 | break; |
| 617 | case RH_SET_ADDRESS: |
| 618 | gr8a66597.rh_devnum = wValue; |
| 619 | break; |
| 620 | case RH_GET_DESCRIPTOR: |
| 621 | switch ((wValue & 0xff00) >> 8) { |
| 622 | case (0x01): /* device descriptor */ |
| 623 | len = min_t(unsigned int, |
| 624 | leni, |
| 625 | min_t(unsigned int, |
| 626 | sizeof(root_hub_dev_des), |
| 627 | wLength)); |
| 628 | memcpy(buffer, root_hub_dev_des, len); |
| 629 | break; |
| 630 | case (0x02): /* configuration descriptor */ |
| 631 | len = min_t(unsigned int, |
| 632 | leni, |
| 633 | min_t(unsigned int, |
| 634 | sizeof(root_hub_config_des), |
| 635 | wLength)); |
| 636 | memcpy(buffer, root_hub_config_des, len); |
| 637 | break; |
| 638 | case (0x03): /* string descriptors */ |
| 639 | if (wValue == 0x0300) { |
| 640 | len = min_t(unsigned int, |
| 641 | leni, |
| 642 | min_t(unsigned int, |
| 643 | sizeof(root_hub_str_index0), |
| 644 | wLength)); |
| 645 | memcpy(buffer, root_hub_str_index0, len); |
| 646 | } |
| 647 | if (wValue == 0x0301) { |
| 648 | len = min_t(unsigned int, |
| 649 | leni, |
| 650 | min_t(unsigned int, |
| 651 | sizeof(root_hub_str_index1), |
| 652 | wLength)); |
| 653 | memcpy(buffer, root_hub_str_index1, len); |
| 654 | } |
| 655 | break; |
| 656 | default: |
| 657 | stat = USB_ST_STALLED; |
| 658 | } |
| 659 | break; |
| 660 | |
| 661 | case RH_GET_DESCRIPTOR | RH_CLASS: |
| 662 | { |
| 663 | __u32 temp = 0x00000001; |
| 664 | |
| 665 | data[0] = 9; /* min length; */ |
| 666 | data[1] = 0x29; |
| 667 | data[2] = temp & RH_A_NDP; |
| 668 | data[3] = 0; |
| 669 | if (temp & RH_A_PSM) |
| 670 | data[3] |= 0x1; |
| 671 | if (temp & RH_A_NOCP) |
| 672 | data[3] |= 0x10; |
| 673 | else if (temp & RH_A_OCPM) |
| 674 | data[3] |= 0x8; |
| 675 | |
| 676 | /* corresponds to data[4-7] */ |
| 677 | data[5] = (temp & RH_A_POTPGT) >> 24; |
| 678 | data[7] = temp & RH_B_DR; |
| 679 | if (data[2] < 7) { |
| 680 | data[8] = 0xff; |
| 681 | } else { |
| 682 | data[0] += 2; |
| 683 | data[8] = (temp & RH_B_DR) >> 8; |
| 684 | data[10] = data[9] = 0xff; |
| 685 | } |
| 686 | |
| 687 | len = min_t(unsigned int, leni, |
| 688 | min_t(unsigned int, data[0], wLength)); |
| 689 | memcpy(buffer, data, len); |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | case RH_GET_CONFIGURATION: |
| 694 | *(__u8 *) buffer = 0x01; |
| 695 | len = 1; |
| 696 | break; |
| 697 | case RH_SET_CONFIGURATION: |
| 698 | break; |
| 699 | default: |
Nobuhiro Iwamatsu | e58c41e | 2008-09-18 20:13:08 +0900 | [diff] [blame] | 700 | R8A66597_DPRINT("unsupported root hub command"); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 701 | stat = USB_ST_STALLED; |
| 702 | } |
| 703 | |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 704 | mdelay(1); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 705 | |
| 706 | len = min_t(int, len, leni); |
| 707 | |
| 708 | dev->act_len = len; |
| 709 | dev->status = stat; |
| 710 | |
| 711 | return stat; |
| 712 | } |
| 713 | |
| 714 | int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 715 | int transfer_len) |
| 716 | { |
| 717 | struct r8a66597 *r8a66597 = &gr8a66597; |
| 718 | int ret = 0; |
| 719 | |
| 720 | R8A66597_DPRINT("%s\n", __func__); |
| 721 | R8A66597_DPRINT("pipe = %08x, buffer = %p, len = %d, devnum = %d\n", |
| 722 | pipe, buffer, transfer_len, dev->devnum); |
| 723 | |
Yoshihiro Shimoda | 60ece6d | 2008-10-29 20:05:18 +0900 | [diff] [blame] | 724 | set_devadd(r8a66597, dev->devnum, dev, 0); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 725 | |
| 726 | pipe_buffer_setting(r8a66597, dev, pipe); |
| 727 | |
| 728 | dev->act_len = 0; |
| 729 | while (dev->act_len < transfer_len && ret == 0) { |
| 730 | if (ctrlc()) |
| 731 | return -1; |
| 732 | |
| 733 | if (usb_pipein(pipe)) |
| 734 | ret = receive_bulk_packet(r8a66597, dev, pipe, buffer, |
| 735 | transfer_len); |
| 736 | else |
| 737 | ret = send_bulk_packet(r8a66597, dev, pipe, buffer, |
| 738 | transfer_len); |
| 739 | } |
| 740 | |
| 741 | if (ret == 0) |
| 742 | dev->status = 0; |
| 743 | |
| 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | int submit_control_msg(struct usb_device *dev, unsigned long pipe, |
| 748 | void *buffer, int transfer_len, struct devrequest *setup) |
| 749 | { |
| 750 | struct r8a66597 *r8a66597 = &gr8a66597; |
| 751 | u16 r8a66597_address = setup->request == USB_REQ_SET_ADDRESS ? |
| 752 | 0 : dev->devnum; |
| 753 | |
| 754 | R8A66597_DPRINT("%s\n", __func__); |
| 755 | if (usb_pipedevice(pipe) == r8a66597->rh_devnum) |
| 756 | return r8a66597_submit_rh_msg(dev, pipe, buffer, transfer_len, |
| 757 | setup); |
| 758 | |
| 759 | R8A66597_DPRINT("%s: setup\n", __func__); |
Yoshihiro Shimoda | 60ece6d | 2008-10-29 20:05:18 +0900 | [diff] [blame] | 760 | set_devadd(r8a66597, r8a66597_address, dev, 0); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 761 | |
| 762 | if (send_setup_packet(r8a66597, dev, setup) < 0) { |
| 763 | printf("setup packet send error\n"); |
| 764 | return -1; |
| 765 | } |
| 766 | |
Yoshihiro Shimoda | 60ece6d | 2008-10-29 20:05:18 +0900 | [diff] [blame] | 767 | dev->act_len = 0; |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 768 | if (usb_pipein(pipe)) |
| 769 | if (receive_control_packet(r8a66597, dev, buffer, |
| 770 | transfer_len) < 0) |
| 771 | return -1; |
| 772 | |
| 773 | if (send_status_packet(r8a66597, pipe) < 0) |
| 774 | return -1; |
| 775 | |
| 776 | dev->status = 0; |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 782 | int transfer_len, int interval) |
| 783 | { |
| 784 | /* no implement */ |
| 785 | R8A66597_DPRINT("%s\n", __func__); |
| 786 | return 0; |
| 787 | } |
| 788 | |
Troy Kisky | 06d513e | 2013-10-10 15:27:56 -0700 | [diff] [blame] | 789 | int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 790 | { |
| 791 | struct r8a66597 *r8a66597 = &gr8a66597; |
| 792 | |
| 793 | R8A66597_DPRINT("%s\n", __func__); |
| 794 | |
Yasuhisa Umano | 198c5f9 | 2014-04-18 11:33:15 +0900 | [diff] [blame] | 795 | memset(r8a66597, 0, sizeof(*r8a66597)); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 796 | r8a66597->reg = CONFIG_R8A66597_BASE_ADDR; |
| 797 | |
| 798 | disable_controller(r8a66597); |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 799 | mdelay(100); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 800 | |
| 801 | enable_controller(r8a66597); |
| 802 | r8a66597_port_power(r8a66597, 0 , 1); |
| 803 | |
| 804 | /* check usb device */ |
| 805 | check_usb_device_connecting(r8a66597); |
| 806 | |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 807 | mdelay(50); |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
Lucas Stach | c7e3b2b | 2012-09-26 00:14:34 +0200 | [diff] [blame] | 812 | int usb_lowlevel_stop(int index) |
Yoshihiro Shimoda | fd0f2f3 | 2008-07-09 21:07:38 +0900 | [diff] [blame] | 813 | { |
| 814 | disable_controller(&gr8a66597); |
| 815 | |
| 816 | return 0; |
| 817 | } |