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