Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1 | #include <common.h> |
Heiko Schocher | 7a34278 | 2013-06-04 11:10:01 +0200 | [diff] [blame] | 2 | #include <watchdog.h> |
Hans de Goede | 246e3b8 | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 3 | #ifdef CONFIG_ARCH_SUNXI |
Hans de Goede | 2aacc42 | 2015-04-27 15:05:10 +0200 | [diff] [blame] | 4 | #include <asm/arch/usb_phy.h> |
Hans de Goede | 246e3b8 | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 5 | #endif |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 6 | #include <asm/errno.h> |
| 7 | #include <linux/usb/ch9.h> |
| 8 | #include <linux/usb/gadget.h> |
| 9 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 10 | #include <usb.h> |
| 11 | #include "linux-compat.h" |
| 12 | #include "usb-compat.h" |
| 13 | #include "musb_core.h" |
| 14 | #include "musb_host.h" |
| 15 | #include "musb_gadget.h" |
| 16 | |
| 17 | #ifdef CONFIG_MUSB_HOST |
Hans de Goede | 904f2a8 | 2015-01-11 20:34:54 +0100 | [diff] [blame] | 18 | struct int_queue { |
| 19 | struct usb_host_endpoint hep; |
| 20 | struct urb urb; |
| 21 | }; |
| 22 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 23 | static struct musb *host; |
| 24 | static struct usb_hcd hcd; |
| 25 | static enum usb_device_speed host_speed; |
| 26 | |
| 27 | static void musb_host_complete_urb(struct urb *urb) |
| 28 | { |
| 29 | urb->dev->status &= ~USB_ST_NOT_PROC; |
| 30 | urb->dev->act_len = urb->actual_length; |
| 31 | } |
| 32 | |
| 33 | static struct usb_host_endpoint hep; |
| 34 | static struct urb urb; |
| 35 | |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 36 | static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep, |
| 37 | struct usb_device *dev, int endpoint_type, |
| 38 | unsigned long pipe, void *buffer, int len, |
| 39 | struct devrequest *setup, int interval) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 40 | { |
| 41 | int epnum = usb_pipeendpoint(pipe); |
| 42 | int is_in = usb_pipein(pipe); |
| 43 | |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 44 | memset(urb, 0, sizeof(struct urb)); |
| 45 | memset(hep, 0, sizeof(struct usb_host_endpoint)); |
| 46 | INIT_LIST_HEAD(&hep->urb_list); |
| 47 | INIT_LIST_HEAD(&urb->urb_list); |
| 48 | urb->ep = hep; |
| 49 | urb->complete = musb_host_complete_urb; |
| 50 | urb->status = -EINPROGRESS; |
| 51 | urb->dev = dev; |
| 52 | urb->pipe = pipe; |
| 53 | urb->transfer_buffer = buffer; |
| 54 | urb->transfer_dma = (unsigned long)buffer; |
| 55 | urb->transfer_buffer_length = len; |
| 56 | urb->setup_packet = (unsigned char *)setup; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 57 | |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 58 | urb->ep->desc.wMaxPacketSize = |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 59 | __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] : |
| 60 | dev->epmaxpacketout[epnum]); |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 61 | urb->ep->desc.bmAttributes = endpoint_type; |
| 62 | urb->ep->desc.bEndpointAddress = |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 63 | (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum; |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 64 | urb->ep->desc.bInterval = interval; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 67 | static int submit_urb(struct usb_hcd *hcd, struct urb *urb) |
| 68 | { |
| 69 | struct musb *host = hcd->hcd_priv; |
| 70 | int ret; |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 71 | unsigned long timeout; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 72 | |
| 73 | ret = musb_urb_enqueue(hcd, urb, 0); |
| 74 | if (ret < 0) { |
| 75 | printf("Failed to enqueue URB to controller\n"); |
| 76 | return ret; |
| 77 | } |
| 78 | |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 79 | timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 80 | do { |
| 81 | if (ctrlc()) |
| 82 | return -EIO; |
| 83 | host->isr(0, host); |
Hans de Goede | e8672e3 | 2015-01-11 20:34:50 +0100 | [diff] [blame] | 84 | } while (urb->status == -EINPROGRESS && |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 85 | get_timer(0) < timeout); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 86 | |
Hans de Goede | b918a0c | 2015-01-11 20:34:52 +0100 | [diff] [blame] | 87 | if (urb->status == -EINPROGRESS) |
| 88 | musb_urb_dequeue(hcd, urb, -ETIME); |
| 89 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 90 | return urb->status; |
| 91 | } |
| 92 | |
| 93 | int submit_control_msg(struct usb_device *dev, unsigned long pipe, |
| 94 | void *buffer, int len, struct devrequest *setup) |
| 95 | { |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 96 | construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_CONTROL, pipe, |
| 97 | buffer, len, setup, 0); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 98 | |
| 99 | /* Fix speed for non hub-attached devices */ |
Hans de Goede | e740ca3 | 2015-06-17 21:33:55 +0200 | [diff] [blame^] | 100 | if (!usb_dev_get_parent(dev)) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 101 | dev->speed = host_speed; |
| 102 | |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 103 | return submit_urb(&hcd, &urb); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, |
| 108 | void *buffer, int len) |
| 109 | { |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 110 | construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_BULK, pipe, |
| 111 | buffer, len, NULL, 0); |
| 112 | return submit_urb(&hcd, &urb); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | int submit_int_msg(struct usb_device *dev, unsigned long pipe, |
| 116 | void *buffer, int len, int interval) |
| 117 | { |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 118 | construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_INT, pipe, |
| 119 | buffer, len, NULL, interval); |
| 120 | return submit_urb(&hcd, &urb); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Hans de Goede | 904f2a8 | 2015-01-11 20:34:54 +0100 | [diff] [blame] | 123 | struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe, |
| 124 | int queuesize, int elementsize, void *buffer, int interval) |
| 125 | { |
| 126 | struct int_queue *queue; |
| 127 | int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe); |
| 128 | |
| 129 | if (queuesize != 1) { |
| 130 | printf("ERROR musb int-queues only support queuesize 1\n"); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | if (dev->int_pending & (1 << index)) { |
| 135 | printf("ERROR int-urb is already pending on pipe %lx\n", pipe); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | queue = malloc(sizeof(*queue)); |
| 140 | if (!queue) |
| 141 | return NULL; |
| 142 | |
| 143 | construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT, |
| 144 | pipe, buffer, elementsize, NULL, interval); |
| 145 | |
| 146 | ret = musb_urb_enqueue(&hcd, &queue->urb, 0); |
| 147 | if (ret < 0) { |
| 148 | printf("Failed to enqueue URB to controller\n"); |
| 149 | free(queue); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | dev->int_pending |= 1 << index; |
| 154 | return queue; |
| 155 | } |
| 156 | |
| 157 | int destroy_int_queue(struct usb_device *dev, struct int_queue *queue) |
| 158 | { |
| 159 | int index = usb_pipein(queue->urb.pipe) * 16 + |
| 160 | usb_pipeendpoint(queue->urb.pipe); |
| 161 | |
| 162 | if (queue->urb.status == -EINPROGRESS) |
| 163 | musb_urb_dequeue(&hcd, &queue->urb, -ETIME); |
| 164 | |
| 165 | dev->int_pending &= ~(1 << index); |
| 166 | free(queue); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | void *poll_int_queue(struct usb_device *dev, struct int_queue *queue) |
| 171 | { |
| 172 | if (queue->urb.status != -EINPROGRESS) |
| 173 | return NULL; /* URB has already completed in a prev. poll */ |
| 174 | |
| 175 | host->isr(0, host); |
| 176 | |
| 177 | if (queue->urb.status != -EINPROGRESS) |
| 178 | return queue->urb.transfer_buffer; /* Done */ |
| 179 | |
| 180 | return NULL; /* URB still pending */ |
| 181 | } |
| 182 | |
Hans de Goede | 8802f56 | 2015-06-17 21:33:48 +0200 | [diff] [blame] | 183 | int usb_reset_root_port(struct usb_device *dev) |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 184 | { |
| 185 | void *mbase = host->mregs; |
| 186 | u8 power; |
| 187 | |
| 188 | power = musb_readb(mbase, MUSB_POWER); |
| 189 | power &= 0xf0; |
| 190 | musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power); |
| 191 | mdelay(50); |
Hans de Goede | 246e3b8 | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 192 | #ifdef CONFIG_ARCH_SUNXI |
| 193 | /* |
| 194 | * sunxi phy has a bug and it will wrongly detect high speed squelch |
| 195 | * when clearing reset on low-speed devices, temporary disable |
| 196 | * squelch detection to work around this. |
| 197 | */ |
Hans de Goede | 7b79865 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 198 | sunxi_usb_phy_enable_squelch_detect(0, 0); |
Hans de Goede | 246e3b8 | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 199 | #endif |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 200 | power = musb_readb(mbase, MUSB_POWER); |
| 201 | musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power); |
Hans de Goede | 246e3b8 | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 202 | #ifdef CONFIG_ARCH_SUNXI |
Hans de Goede | 7b79865 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 203 | sunxi_usb_phy_enable_squelch_detect(0, 1); |
Hans de Goede | 246e3b8 | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 204 | #endif |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 205 | host->isr(0, host); |
| 206 | host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ? |
| 207 | USB_SPEED_HIGH : |
| 208 | (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ? |
| 209 | USB_SPEED_FULL : USB_SPEED_LOW; |
| 210 | mdelay((host_speed == USB_SPEED_LOW) ? 200 : 50); |
Simon Glass | de31213 | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 211 | |
| 212 | return 0; |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Troy Kisky | 06d513e | 2013-10-10 15:27:56 -0700 | [diff] [blame] | 215 | int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 216 | { |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 217 | void *mbase; |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 218 | /* USB spec says it may take up to 1 second for a device to connect */ |
| 219 | unsigned long timeout = get_timer(0) + 1000; |
Hans de Goede | 1583723 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 220 | int ret; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 221 | |
| 222 | if (!host) { |
| 223 | printf("MUSB host is not registered\n"); |
| 224 | return -ENODEV; |
| 225 | } |
| 226 | |
Hans de Goede | 1583723 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 227 | ret = musb_start(host); |
| 228 | if (ret) |
| 229 | return ret; |
| 230 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 231 | mbase = host->mregs; |
| 232 | do { |
| 233 | if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM) |
| 234 | break; |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 235 | } while (get_timer(0) < timeout); |
| 236 | if (get_timer(0) >= timeout) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 237 | return -ENODEV; |
| 238 | |
Hans de Goede | 8802f56 | 2015-06-17 21:33:48 +0200 | [diff] [blame] | 239 | usb_reset_root_port(NULL); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 240 | host->is_active = 1; |
| 241 | hcd.hcd_priv = host; |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | int usb_lowlevel_stop(int index) |
| 247 | { |
| 248 | if (!host) { |
| 249 | printf("MUSB host is not registered\n"); |
| 250 | return -ENODEV; |
| 251 | } |
| 252 | |
| 253 | musb_stop(host); |
| 254 | return 0; |
| 255 | } |
| 256 | #endif /* CONFIG_MUSB_HOST */ |
| 257 | |
| 258 | #ifdef CONFIG_MUSB_GADGET |
| 259 | static struct musb *gadget; |
| 260 | |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 261 | int usb_gadget_handle_interrupts(int index) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 262 | { |
Heiko Schocher | 7a34278 | 2013-06-04 11:10:01 +0200 | [diff] [blame] | 263 | WATCHDOG_RESET(); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 264 | if (!gadget || !gadget->isr) |
| 265 | return -EINVAL; |
| 266 | |
| 267 | return gadget->isr(0, gadget); |
| 268 | } |
| 269 | |
| 270 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 271 | { |
| 272 | int ret; |
| 273 | |
Bin Liu | 76b09b8 | 2013-03-21 05:27:49 +0000 | [diff] [blame] | 274 | if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind || |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 275 | !driver->setup) { |
| 276 | printf("bad parameter.\n"); |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | if (!gadget) { |
| 281 | printf("Controller uninitialized\n"); |
| 282 | return -ENXIO; |
| 283 | } |
| 284 | |
| 285 | ret = musb_gadget_start(&gadget->g, driver); |
| 286 | if (ret < 0) { |
| 287 | printf("gadget_start failed with %d\n", ret); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | ret = driver->bind(&gadget->g); |
| 292 | if (ret < 0) { |
| 293 | printf("bind failed with %d\n", ret); |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 301 | { |
Rob Herring | 078d730 | 2014-04-18 08:54:30 -0500 | [diff] [blame] | 302 | if (driver->disconnect) |
| 303 | driver->disconnect(&gadget->g); |
| 304 | if (driver->unbind) |
| 305 | driver->unbind(&gadget->g); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | #endif /* CONFIG_MUSB_GADGET */ |
| 309 | |
| 310 | int musb_register(struct musb_hdrc_platform_data *plat, void *bdata, |
| 311 | void *ctl_regs) |
| 312 | { |
| 313 | struct musb **musbp; |
| 314 | |
| 315 | switch (plat->mode) { |
| 316 | #ifdef CONFIG_MUSB_HOST |
| 317 | case MUSB_HOST: |
| 318 | musbp = &host; |
| 319 | break; |
| 320 | #endif |
| 321 | #ifdef CONFIG_MUSB_GADGET |
| 322 | case MUSB_PERIPHERAL: |
| 323 | musbp = &gadget; |
| 324 | break; |
| 325 | #endif |
| 326 | default: |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | |
| 330 | *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs); |
| 331 | if (!musbp) { |
| 332 | printf("Failed to init the controller\n"); |
| 333 | return -EIO; |
| 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |