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