1 #include <common.h> 2 #include <console.h> 3 #include <watchdog.h> 4 #ifdef CONFIG_ARCH_SUNXI 5 #include <asm/arch/usb_phy.h> 6 #endif 7 #include <linux/errno.h> 8 #include <linux/usb/ch9.h> 9 #include <linux/usb/gadget.h> 10 11 #include <usb.h> 12 #include "linux-compat.h" 13 #include "usb-compat.h" 14 #include "musb_core.h" 15 #include "musb_host.h" 16 #include "musb_gadget.h" 17 #include "musb_uboot.h" 18 19 #ifdef CONFIG_USB_MUSB_HOST 20 struct int_queue { 21 struct usb_host_endpoint hep; 22 struct urb urb; 23 }; 24 25 #if !CONFIG_IS_ENABLED(DM_USB) 26 struct musb_host_data musb_host; 27 #endif 28 29 static void musb_host_complete_urb(struct urb *urb) 30 { 31 urb->dev->status &= ~USB_ST_NOT_PROC; 32 urb->dev->act_len = urb->actual_length; 33 } 34 35 static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep, 36 struct usb_device *dev, int endpoint_type, 37 unsigned long pipe, void *buffer, int len, 38 struct devrequest *setup, int interval) 39 { 40 int epnum = usb_pipeendpoint(pipe); 41 int is_in = usb_pipein(pipe); 42 43 memset(urb, 0, sizeof(struct urb)); 44 memset(hep, 0, sizeof(struct usb_host_endpoint)); 45 INIT_LIST_HEAD(&hep->urb_list); 46 INIT_LIST_HEAD(&urb->urb_list); 47 urb->ep = hep; 48 urb->complete = musb_host_complete_urb; 49 urb->status = -EINPROGRESS; 50 urb->dev = dev; 51 urb->pipe = pipe; 52 urb->transfer_buffer = buffer; 53 urb->transfer_dma = (unsigned long)buffer; 54 urb->transfer_buffer_length = len; 55 urb->setup_packet = (unsigned char *)setup; 56 57 urb->ep->desc.wMaxPacketSize = 58 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] : 59 dev->epmaxpacketout[epnum]); 60 urb->ep->desc.bmAttributes = endpoint_type; 61 urb->ep->desc.bEndpointAddress = 62 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum; 63 urb->ep->desc.bInterval = interval; 64 } 65 66 static int submit_urb(struct usb_hcd *hcd, struct urb *urb) 67 { 68 struct musb *host = hcd->hcd_priv; 69 int ret; 70 unsigned long timeout; 71 72 ret = musb_urb_enqueue(hcd, urb, 0); 73 if (ret < 0) { 74 printf("Failed to enqueue URB to controller\n"); 75 return ret; 76 } 77 78 timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe); 79 do { 80 if (ctrlc()) 81 return -EIO; 82 host->isr(0, host); 83 } while (urb->status == -EINPROGRESS && 84 get_timer(0) < timeout); 85 86 if (urb->status == -EINPROGRESS) 87 musb_urb_dequeue(hcd, urb, -ETIME); 88 89 return urb->status; 90 } 91 92 static int _musb_submit_control_msg(struct musb_host_data *host, 93 struct usb_device *dev, unsigned long pipe, 94 void *buffer, int len, struct devrequest *setup) 95 { 96 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL, 97 pipe, buffer, len, setup, 0); 98 99 /* Fix speed for non hub-attached devices */ 100 if (!usb_dev_get_parent(dev)) 101 dev->speed = host->host_speed; 102 103 return submit_urb(&host->hcd, &host->urb); 104 } 105 106 static int _musb_submit_bulk_msg(struct musb_host_data *host, 107 struct usb_device *dev, unsigned long pipe, void *buffer, int len) 108 { 109 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_BULK, 110 pipe, buffer, len, NULL, 0); 111 return submit_urb(&host->hcd, &host->urb); 112 } 113 114 static int _musb_submit_int_msg(struct musb_host_data *host, 115 struct usb_device *dev, unsigned long pipe, 116 void *buffer, int len, int interval, bool nonblock) 117 { 118 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe, 119 buffer, len, NULL, interval); 120 return submit_urb(&host->hcd, &host->urb); 121 } 122 123 static struct int_queue *_musb_create_int_queue(struct musb_host_data *host, 124 struct usb_device *dev, unsigned long pipe, int queuesize, 125 int elementsize, void *buffer, int interval) 126 { 127 struct int_queue *queue; 128 int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe); 129 130 if (queuesize != 1) { 131 printf("ERROR musb int-queues only support queuesize 1\n"); 132 return NULL; 133 } 134 135 if (dev->int_pending & (1 << index)) { 136 printf("ERROR int-urb is already pending on pipe %lx\n", pipe); 137 return NULL; 138 } 139 140 queue = malloc(sizeof(*queue)); 141 if (!queue) 142 return NULL; 143 144 construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT, 145 pipe, buffer, elementsize, NULL, interval); 146 147 ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0); 148 if (ret < 0) { 149 printf("Failed to enqueue URB to controller\n"); 150 free(queue); 151 return NULL; 152 } 153 154 dev->int_pending |= 1 << index; 155 return queue; 156 } 157 158 static int _musb_destroy_int_queue(struct musb_host_data *host, 159 struct usb_device *dev, struct int_queue *queue) 160 { 161 int index = usb_pipein(queue->urb.pipe) * 16 + 162 usb_pipeendpoint(queue->urb.pipe); 163 164 if (queue->urb.status == -EINPROGRESS) 165 musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME); 166 167 dev->int_pending &= ~(1 << index); 168 free(queue); 169 return 0; 170 } 171 172 static void *_musb_poll_int_queue(struct musb_host_data *host, 173 struct usb_device *dev, struct int_queue *queue) 174 { 175 if (queue->urb.status != -EINPROGRESS) 176 return NULL; /* URB has already completed in a prev. poll */ 177 178 host->host->isr(0, host->host); 179 180 if (queue->urb.status != -EINPROGRESS) 181 return queue->urb.transfer_buffer; /* Done */ 182 183 return NULL; /* URB still pending */ 184 } 185 186 static int _musb_reset_root_port(struct musb_host_data *host, 187 struct usb_device *dev) 188 { 189 void *mbase = host->host->mregs; 190 u8 power; 191 192 power = musb_readb(mbase, MUSB_POWER); 193 power &= 0xf0; 194 musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power); 195 mdelay(50); 196 #ifdef CONFIG_ARCH_SUNXI 197 /* 198 * sunxi phy has a bug and it will wrongly detect high speed squelch 199 * when clearing reset on low-speed devices, temporary disable 200 * squelch detection to work around this. 201 */ 202 sunxi_usb_phy_enable_squelch_detect(0, 0); 203 #endif 204 power = musb_readb(mbase, MUSB_POWER); 205 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power); 206 #ifdef CONFIG_ARCH_SUNXI 207 sunxi_usb_phy_enable_squelch_detect(0, 1); 208 #endif 209 host->host->isr(0, host->host); 210 host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ? 211 USB_SPEED_HIGH : 212 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ? 213 USB_SPEED_FULL : USB_SPEED_LOW; 214 mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50); 215 216 return 0; 217 } 218 219 int musb_lowlevel_init(struct musb_host_data *host) 220 { 221 void *mbase; 222 /* USB spec says it may take up to 1 second for a device to connect */ 223 unsigned long timeout = get_timer(0) + 1000; 224 int ret; 225 226 if (!host->host) { 227 printf("MUSB host is not registered\n"); 228 return -ENODEV; 229 } 230 231 ret = musb_start(host->host); 232 if (ret) 233 return ret; 234 235 mbase = host->host->mregs; 236 do { 237 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM) 238 break; 239 } while (get_timer(0) < timeout); 240 if (get_timer(0) >= timeout) { 241 musb_stop(host->host); 242 return -ENODEV; 243 } 244 245 _musb_reset_root_port(host, NULL); 246 host->host->is_active = 1; 247 host->hcd.hcd_priv = host->host; 248 249 return 0; 250 } 251 252 #if !CONFIG_IS_ENABLED(DM_USB) 253 int usb_lowlevel_stop(int index) 254 { 255 if (!musb_host.host) { 256 printf("MUSB host is not registered\n"); 257 return -ENODEV; 258 } 259 260 musb_stop(musb_host.host); 261 return 0; 262 } 263 264 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, 265 void *buffer, int length) 266 { 267 return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length); 268 } 269 270 int submit_control_msg(struct usb_device *dev, unsigned long pipe, 271 void *buffer, int length, struct devrequest *setup) 272 { 273 return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup); 274 } 275 276 int submit_int_msg(struct usb_device *dev, unsigned long pipe, 277 void *buffer, int length, int interval, bool nonblock) 278 { 279 return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length, 280 interval, nonblock); 281 } 282 283 struct int_queue *create_int_queue(struct usb_device *dev, 284 unsigned long pipe, int queuesize, int elementsize, 285 void *buffer, int interval) 286 { 287 return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize, 288 buffer, interval); 289 } 290 291 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue) 292 { 293 return _musb_poll_int_queue(&musb_host, dev, queue); 294 } 295 296 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue) 297 { 298 return _musb_destroy_int_queue(&musb_host, dev, queue); 299 } 300 301 int usb_reset_root_port(struct usb_device *dev) 302 { 303 return _musb_reset_root_port(&musb_host, dev); 304 } 305 306 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) 307 { 308 return musb_lowlevel_init(&musb_host); 309 } 310 #endif /* !CONFIG_IS_ENABLED(DM_USB) */ 311 312 #if CONFIG_IS_ENABLED(DM_USB) 313 static int musb_submit_control_msg(struct udevice *dev, struct usb_device *udev, 314 unsigned long pipe, void *buffer, int length, 315 struct devrequest *setup) 316 { 317 struct musb_host_data *host = dev_get_priv(dev); 318 return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup); 319 } 320 321 static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev, 322 unsigned long pipe, void *buffer, int length) 323 { 324 struct musb_host_data *host = dev_get_priv(dev); 325 return _musb_submit_bulk_msg(host, udev, pipe, buffer, length); 326 } 327 328 static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev, 329 unsigned long pipe, void *buffer, int length, 330 int interval, bool nonblock) 331 { 332 struct musb_host_data *host = dev_get_priv(dev); 333 return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval, 334 nonblock); 335 } 336 337 static struct int_queue *musb_create_int_queue(struct udevice *dev, 338 struct usb_device *udev, unsigned long pipe, int queuesize, 339 int elementsize, void *buffer, int interval) 340 { 341 struct musb_host_data *host = dev_get_priv(dev); 342 return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize, 343 buffer, interval); 344 } 345 346 static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev, 347 struct int_queue *queue) 348 { 349 struct musb_host_data *host = dev_get_priv(dev); 350 return _musb_poll_int_queue(host, udev, queue); 351 } 352 353 static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev, 354 struct int_queue *queue) 355 { 356 struct musb_host_data *host = dev_get_priv(dev); 357 return _musb_destroy_int_queue(host, udev, queue); 358 } 359 360 static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev) 361 { 362 struct musb_host_data *host = dev_get_priv(dev); 363 return _musb_reset_root_port(host, udev); 364 } 365 366 struct dm_usb_ops musb_usb_ops = { 367 .control = musb_submit_control_msg, 368 .bulk = musb_submit_bulk_msg, 369 .interrupt = musb_submit_int_msg, 370 .create_int_queue = musb_create_int_queue, 371 .poll_int_queue = musb_poll_int_queue, 372 .destroy_int_queue = musb_destroy_int_queue, 373 .reset_root_port = musb_reset_root_port, 374 }; 375 #endif /* CONFIG_IS_ENABLED(DM_USB) */ 376 #endif /* CONFIG_USB_MUSB_HOST */ 377 378 #ifdef CONFIG_USB_MUSB_GADGET 379 static struct musb *gadget; 380 381 int usb_gadget_handle_interrupts(int index) 382 { 383 WATCHDOG_RESET(); 384 if (!gadget || !gadget->isr) 385 return -EINVAL; 386 387 return gadget->isr(0, gadget); 388 } 389 390 int usb_gadget_register_driver(struct usb_gadget_driver *driver) 391 { 392 int ret; 393 394 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind || 395 !driver->setup) { 396 printf("bad parameter.\n"); 397 return -EINVAL; 398 } 399 400 if (!gadget) { 401 printf("Controller uninitialized\n"); 402 return -ENXIO; 403 } 404 405 ret = musb_gadget_start(&gadget->g, driver); 406 if (ret < 0) { 407 printf("gadget_start failed with %d\n", ret); 408 return ret; 409 } 410 411 ret = driver->bind(&gadget->g); 412 if (ret < 0) { 413 printf("bind failed with %d\n", ret); 414 return ret; 415 } 416 417 return 0; 418 } 419 420 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) 421 { 422 if (driver->disconnect) 423 driver->disconnect(&gadget->g); 424 if (driver->unbind) 425 driver->unbind(&gadget->g); 426 return 0; 427 } 428 #endif /* CONFIG_USB_MUSB_GADGET */ 429 430 int musb_register(struct musb_hdrc_platform_data *plat, void *bdata, 431 void *ctl_regs) 432 { 433 struct musb **musbp; 434 435 switch (plat->mode) { 436 #if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB) 437 case MUSB_HOST: 438 musbp = &musb_host.host; 439 break; 440 #endif 441 #ifdef CONFIG_USB_MUSB_GADGET 442 case MUSB_PERIPHERAL: 443 musbp = &gadget; 444 break; 445 #endif 446 default: 447 return -EINVAL; 448 } 449 450 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs); 451 if (!*musbp) { 452 printf("Failed to init the controller\n"); 453 return -EIO; 454 } 455 456 return 0; 457 } 458