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