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