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