Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame^] | 2 | #include <console.h> |
Heiko Schocher | c3ffe50 | 2013-06-04 11:10:01 +0200 | [diff] [blame] | 3 | #include <watchdog.h> |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 4 | #ifdef CONFIG_ARCH_SUNXI |
Hans de Goede | 26a9005 | 2015-04-27 15:05:10 +0200 | [diff] [blame] | 5 | #include <asm/arch/usb_phy.h> |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 6 | #endif |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 7 | #include <asm/errno.h> |
| 8 | #include <linux/usb/ch9.h> |
| 9 | #include <linux/usb/gadget.h> |
| 10 | |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 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" |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 17 | #include "musb_uboot.h" |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 18 | |
Paul Kocialkowski | f34dfcb | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 19 | #ifdef CONFIG_USB_MUSB_HOST |
Hans de Goede | 83b1897 | 2015-01-11 20:34:54 +0100 | [diff] [blame] | 20 | struct int_queue { |
| 21 | struct usb_host_endpoint hep; |
| 22 | struct urb urb; |
| 23 | }; |
| 24 | |
Hans de Goede | fc64961 | 2015-06-17 21:33:58 +0200 | [diff] [blame] | 25 | #ifndef CONFIG_DM_USB |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 26 | struct musb_host_data musb_host; |
Hans de Goede | fc64961 | 2015-06-17 21:33:58 +0200 | [diff] [blame] | 27 | #endif |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 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 | |
Hans de Goede | 39b0845 | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 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) |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 39 | { |
| 40 | int epnum = usb_pipeendpoint(pipe); |
| 41 | int is_in = usb_pipein(pipe); |
| 42 | |
Hans de Goede | 39b0845 | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 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; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 56 | |
Hans de Goede | 39b0845 | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 57 | urb->ep->desc.wMaxPacketSize = |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 58 | __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] : |
| 59 | dev->epmaxpacketout[epnum]); |
Hans de Goede | 39b0845 | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 60 | urb->ep->desc.bmAttributes = endpoint_type; |
| 61 | urb->ep->desc.bEndpointAddress = |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 62 | (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum; |
Hans de Goede | 39b0845 | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 63 | urb->ep->desc.bInterval = interval; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 66 | static int submit_urb(struct usb_hcd *hcd, struct urb *urb) |
| 67 | { |
| 68 | struct musb *host = hcd->hcd_priv; |
| 69 | int ret; |
Hans de Goede | 8de4ded | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 70 | unsigned long timeout; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 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 | |
Hans de Goede | 8de4ded | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 78 | timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe); |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 79 | do { |
| 80 | if (ctrlc()) |
| 81 | return -EIO; |
| 82 | host->isr(0, host); |
Hans de Goede | 24fd777 | 2015-01-11 20:34:50 +0100 | [diff] [blame] | 83 | } while (urb->status == -EINPROGRESS && |
Hans de Goede | 8de4ded | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 84 | get_timer(0) < timeout); |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 85 | |
Hans de Goede | 0e4da95 | 2015-01-11 20:34:52 +0100 | [diff] [blame] | 86 | if (urb->status == -EINPROGRESS) |
| 87 | musb_urb_dequeue(hcd, urb, -ETIME); |
| 88 | |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 89 | return urb->status; |
| 90 | } |
| 91 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 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) |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 95 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 96 | construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL, |
| 97 | pipe, buffer, len, setup, 0); |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 98 | |
| 99 | /* Fix speed for non hub-attached devices */ |
Hans de Goede | 4d8b8df | 2015-06-17 21:33:55 +0200 | [diff] [blame] | 100 | if (!usb_dev_get_parent(dev)) |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 101 | dev->speed = host->host_speed; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 102 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 103 | return submit_urb(&host->hcd, &host->urb); |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 106 | static int _musb_submit_bulk_msg(struct musb_host_data *host, |
| 107 | struct usb_device *dev, unsigned long pipe, void *buffer, int len) |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 108 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 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); |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 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) |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 117 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 118 | construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe, |
Hans de Goede | 39b0845 | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 119 | buffer, len, NULL, interval); |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 120 | return submit_urb(&host->hcd, &host->urb); |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 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) |
Hans de Goede | 83b1897 | 2015-01-11 20:34:54 +0100 | [diff] [blame] | 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 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 147 | ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0); |
Hans de Goede | 83b1897 | 2015-01-11 20:34:54 +0100 | [diff] [blame] | 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 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 158 | static int _musb_destroy_int_queue(struct musb_host_data *host, |
| 159 | struct usb_device *dev, struct int_queue *queue) |
Hans de Goede | 83b1897 | 2015-01-11 20:34:54 +0100 | [diff] [blame] | 160 | { |
| 161 | int index = usb_pipein(queue->urb.pipe) * 16 + |
| 162 | usb_pipeendpoint(queue->urb.pipe); |
| 163 | |
| 164 | if (queue->urb.status == -EINPROGRESS) |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 165 | musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME); |
Hans de Goede | 83b1897 | 2015-01-11 20:34:54 +0100 | [diff] [blame] | 166 | |
| 167 | dev->int_pending &= ~(1 << index); |
| 168 | free(queue); |
| 169 | return 0; |
| 170 | } |
| 171 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 172 | static void *_musb_poll_int_queue(struct musb_host_data *host, |
| 173 | struct usb_device *dev, struct int_queue *queue) |
Hans de Goede | 83b1897 | 2015-01-11 20:34:54 +0100 | [diff] [blame] | 174 | { |
| 175 | if (queue->urb.status != -EINPROGRESS) |
| 176 | return NULL; /* URB has already completed in a prev. poll */ |
| 177 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 178 | host->host->isr(0, host->host); |
Hans de Goede | 83b1897 | 2015-01-11 20:34:54 +0100 | [diff] [blame] | 179 | |
| 180 | if (queue->urb.status != -EINPROGRESS) |
| 181 | return queue->urb.transfer_buffer; /* Done */ |
| 182 | |
| 183 | return NULL; /* URB still pending */ |
| 184 | } |
| 185 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 186 | static int _musb_reset_root_port(struct musb_host_data *host, |
| 187 | struct usb_device *dev) |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 188 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 189 | void *mbase = host->host->mregs; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 190 | u8 power; |
Hans de Goede | 498ab95 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 191 | |
| 192 | power = musb_readb(mbase, MUSB_POWER); |
| 193 | power &= 0xf0; |
| 194 | musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power); |
| 195 | mdelay(50); |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 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 | */ |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 202 | sunxi_usb_phy_enable_squelch_detect(0, 0); |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 203 | #endif |
Hans de Goede | 498ab95 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 204 | power = musb_readb(mbase, MUSB_POWER); |
| 205 | musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power); |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 206 | #ifdef CONFIG_ARCH_SUNXI |
Hans de Goede | 8697909 | 2015-04-27 14:54:47 +0200 | [diff] [blame] | 207 | sunxi_usb_phy_enable_squelch_detect(0, 1); |
Hans de Goede | ab721ad | 2015-03-27 20:54:25 +0100 | [diff] [blame] | 208 | #endif |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 209 | host->host->isr(0, host->host); |
| 210 | host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ? |
Hans de Goede | 498ab95 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 211 | USB_SPEED_HIGH : |
| 212 | (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ? |
| 213 | USB_SPEED_FULL : USB_SPEED_LOW; |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 214 | mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50); |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 215 | |
| 216 | return 0; |
Hans de Goede | 498ab95 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 219 | int musb_lowlevel_init(struct musb_host_data *host) |
Hans de Goede | 498ab95 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 220 | { |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 221 | void *mbase; |
Hans de Goede | 8de4ded | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 222 | /* USB spec says it may take up to 1 second for a device to connect */ |
| 223 | unsigned long timeout = get_timer(0) + 1000; |
Hans de Goede | 81c4998 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 224 | int ret; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 225 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 226 | if (!host->host) { |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 227 | printf("MUSB host is not registered\n"); |
| 228 | return -ENODEV; |
| 229 | } |
| 230 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 231 | ret = musb_start(host->host); |
Hans de Goede | 81c4998 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 232 | if (ret) |
| 233 | return ret; |
| 234 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 235 | mbase = host->host->mregs; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 236 | do { |
| 237 | if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM) |
| 238 | break; |
Hans de Goede | 8de4ded | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 239 | } while (get_timer(0) < timeout); |
| 240 | if (get_timer(0) >= timeout) |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 241 | return -ENODEV; |
| 242 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 243 | _musb_reset_root_port(host, NULL); |
| 244 | host->host->is_active = 1; |
| 245 | host->hcd.hcd_priv = host->host; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Hans de Goede | fc64961 | 2015-06-17 21:33:58 +0200 | [diff] [blame] | 250 | #ifndef CONFIG_DM_USB |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 251 | int usb_lowlevel_stop(int index) |
| 252 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 253 | if (!musb_host.host) { |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 254 | printf("MUSB host is not registered\n"); |
| 255 | return -ENODEV; |
| 256 | } |
| 257 | |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 258 | musb_stop(musb_host.host); |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 259 | return 0; |
| 260 | } |
Hans de Goede | 52666e2 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 261 | |
| 262 | int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, |
| 263 | void *buffer, int length) |
| 264 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 265 | return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length); |
Hans de Goede | 52666e2 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int submit_control_msg(struct usb_device *dev, unsigned long pipe, |
| 269 | void *buffer, int length, struct devrequest *setup) |
| 270 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 271 | return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup); |
Hans de Goede | 52666e2 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | int submit_int_msg(struct usb_device *dev, unsigned long pipe, |
| 275 | void *buffer, int length, int interval) |
| 276 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 277 | return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length, interval); |
Hans de Goede | 52666e2 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | struct int_queue *create_int_queue(struct usb_device *dev, |
| 281 | unsigned long pipe, int queuesize, int elementsize, |
| 282 | void *buffer, int interval) |
| 283 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 284 | return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize, |
Hans de Goede | 52666e2 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 285 | buffer, interval); |
| 286 | } |
| 287 | |
| 288 | void *poll_int_queue(struct usb_device *dev, struct int_queue *queue) |
| 289 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 290 | return _musb_poll_int_queue(&musb_host, dev, queue); |
Hans de Goede | 52666e2 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | int destroy_int_queue(struct usb_device *dev, struct int_queue *queue) |
| 294 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 295 | return _musb_destroy_int_queue(&musb_host, dev, queue); |
Hans de Goede | 52666e2 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | int usb_reset_root_port(struct usb_device *dev) |
| 299 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 300 | return _musb_reset_root_port(&musb_host, dev); |
Hans de Goede | 52666e2 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) |
| 304 | { |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 305 | return musb_lowlevel_init(&musb_host); |
Hans de Goede | 52666e2 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 306 | } |
Hans de Goede | fc64961 | 2015-06-17 21:33:58 +0200 | [diff] [blame] | 307 | #endif /* !CONFIG_DM_USB */ |
| 308 | |
| 309 | #ifdef CONFIG_DM_USB |
| 310 | static int musb_submit_control_msg(struct udevice *dev, struct usb_device *udev, |
| 311 | unsigned long pipe, void *buffer, int length, |
| 312 | struct devrequest *setup) |
| 313 | { |
| 314 | struct musb_host_data *host = dev_get_priv(dev); |
| 315 | return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup); |
| 316 | } |
| 317 | |
| 318 | static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev, |
| 319 | unsigned long pipe, void *buffer, int length) |
| 320 | { |
| 321 | struct musb_host_data *host = dev_get_priv(dev); |
| 322 | return _musb_submit_bulk_msg(host, udev, pipe, buffer, length); |
| 323 | } |
| 324 | |
| 325 | static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev, |
| 326 | unsigned long pipe, void *buffer, int length, |
| 327 | int interval) |
| 328 | { |
| 329 | struct musb_host_data *host = dev_get_priv(dev); |
| 330 | return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval); |
| 331 | } |
| 332 | |
| 333 | static struct int_queue *musb_create_int_queue(struct udevice *dev, |
| 334 | struct usb_device *udev, unsigned long pipe, int queuesize, |
| 335 | int elementsize, void *buffer, int interval) |
| 336 | { |
| 337 | struct musb_host_data *host = dev_get_priv(dev); |
| 338 | return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize, |
| 339 | buffer, interval); |
| 340 | } |
| 341 | |
| 342 | static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev, |
| 343 | struct int_queue *queue) |
| 344 | { |
| 345 | struct musb_host_data *host = dev_get_priv(dev); |
| 346 | return _musb_poll_int_queue(host, udev, queue); |
| 347 | } |
| 348 | |
| 349 | static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev, |
| 350 | struct int_queue *queue) |
| 351 | { |
| 352 | struct musb_host_data *host = dev_get_priv(dev); |
| 353 | return _musb_destroy_int_queue(host, udev, queue); |
| 354 | } |
| 355 | |
| 356 | static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev) |
| 357 | { |
| 358 | struct musb_host_data *host = dev_get_priv(dev); |
| 359 | return _musb_reset_root_port(host, udev); |
| 360 | } |
| 361 | |
| 362 | struct dm_usb_ops musb_usb_ops = { |
| 363 | .control = musb_submit_control_msg, |
| 364 | .bulk = musb_submit_bulk_msg, |
| 365 | .interrupt = musb_submit_int_msg, |
| 366 | .create_int_queue = musb_create_int_queue, |
| 367 | .poll_int_queue = musb_poll_int_queue, |
| 368 | .destroy_int_queue = musb_destroy_int_queue, |
| 369 | .reset_root_port = musb_reset_root_port, |
| 370 | }; |
| 371 | #endif /* CONFIG_DM_USB */ |
Paul Kocialkowski | f34dfcb | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 372 | #endif /* CONFIG_USB_MUSB_HOST */ |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 373 | |
Paul Kocialkowski | f34dfcb | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 374 | #ifdef CONFIG_USB_MUSB_GADGET |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 375 | static struct musb *gadget; |
| 376 | |
Kishon Vijay Abraham I | 4763e16 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 377 | int usb_gadget_handle_interrupts(int index) |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 378 | { |
Heiko Schocher | c3ffe50 | 2013-06-04 11:10:01 +0200 | [diff] [blame] | 379 | WATCHDOG_RESET(); |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 380 | if (!gadget || !gadget->isr) |
| 381 | return -EINVAL; |
| 382 | |
| 383 | return gadget->isr(0, gadget); |
| 384 | } |
| 385 | |
| 386 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 387 | { |
| 388 | int ret; |
| 389 | |
Bin Liu | 28f2f35 | 2013-03-21 05:27:49 +0000 | [diff] [blame] | 390 | if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind || |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 391 | !driver->setup) { |
| 392 | printf("bad parameter.\n"); |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
| 396 | if (!gadget) { |
| 397 | printf("Controller uninitialized\n"); |
| 398 | return -ENXIO; |
| 399 | } |
| 400 | |
| 401 | ret = musb_gadget_start(&gadget->g, driver); |
| 402 | if (ret < 0) { |
| 403 | printf("gadget_start failed with %d\n", ret); |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | ret = driver->bind(&gadget->g); |
| 408 | if (ret < 0) { |
| 409 | printf("bind failed with %d\n", ret); |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 417 | { |
Rob Herring | 15175e7 | 2014-04-18 08:54:30 -0500 | [diff] [blame] | 418 | if (driver->disconnect) |
| 419 | driver->disconnect(&gadget->g); |
| 420 | if (driver->unbind) |
| 421 | driver->unbind(&gadget->g); |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 422 | return 0; |
| 423 | } |
Paul Kocialkowski | f34dfcb | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 424 | #endif /* CONFIG_USB_MUSB_GADGET */ |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 425 | |
| 426 | int musb_register(struct musb_hdrc_platform_data *plat, void *bdata, |
| 427 | void *ctl_regs) |
| 428 | { |
| 429 | struct musb **musbp; |
| 430 | |
| 431 | switch (plat->mode) { |
Paul Kocialkowski | f34dfcb | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 432 | #if defined(CONFIG_USB_MUSB_HOST) && !defined(CONFIG_DM_USB) |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 433 | case MUSB_HOST: |
Hans de Goede | 5962748 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 434 | musbp = &musb_host.host; |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 435 | break; |
| 436 | #endif |
Paul Kocialkowski | f34dfcb | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 437 | #ifdef CONFIG_USB_MUSB_GADGET |
Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 438 | case MUSB_PERIPHERAL: |
| 439 | musbp = &gadget; |
| 440 | break; |
| 441 | #endif |
| 442 | default: |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | |
| 446 | *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs); |
| 447 | if (!musbp) { |
| 448 | printf("Failed to init the controller\n"); |
| 449 | return -EIO; |
| 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |