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