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