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