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