Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1 | #include <common.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 2 | #include <console.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 3 | #include <malloc.h> |
Heiko Schocher | 7a34278 | 2013-06-04 11:10:01 +0200 | [diff] [blame] | 4 | #include <watchdog.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 5 | #include <linux/err.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 6 | #include <linux/errno.h> |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 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" |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 16 | #include "musb_uboot.h" |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 17 | |
Paul Kocialkowski | 95de1e2 | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 18 | #ifdef CONFIG_USB_MUSB_HOST |
Hans de Goede | 904f2a8 | 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 | |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 24 | #if !CONFIG_IS_ENABLED(DM_USB) |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 25 | struct musb_host_data musb_host; |
Hans de Goede | 09e7ea4 | 2015-06-17 21:33:58 +0200 | [diff] [blame] | 26 | #endif |
Ilya Yanok | eb81955 | 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 | accf04c | 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 | eb81955 | 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 | accf04c | 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 | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 55 | |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 56 | urb->ep->desc.wMaxPacketSize = |
Ilya Yanok | eb81955 | 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 | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 59 | urb->ep->desc.bmAttributes = endpoint_type; |
| 60 | urb->ep->desc.bEndpointAddress = |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 61 | (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum; |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 62 | urb->ep->desc.bInterval = interval; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Ilya Yanok | eb81955 | 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 | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 69 | unsigned long timeout; |
Ilya Yanok | eb81955 | 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 | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 77 | timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe); |
Ilya Yanok | eb81955 | 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 | e8672e3 | 2015-01-11 20:34:50 +0100 | [diff] [blame] | 82 | } while (urb->status == -EINPROGRESS && |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 83 | get_timer(0) < timeout); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 84 | |
Hans de Goede | b918a0c | 2015-01-11 20:34:52 +0100 | [diff] [blame] | 85 | if (urb->status == -EINPROGRESS) |
| 86 | musb_urb_dequeue(hcd, urb, -ETIME); |
| 87 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 88 | return urb->status; |
| 89 | } |
| 90 | |
Hans de Goede | fc85d39 | 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 | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 94 | { |
Hans de Goede | fc85d39 | 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 | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 97 | |
| 98 | /* Fix speed for non hub-attached devices */ |
Hans de Goede | e740ca3 | 2015-06-17 21:33:55 +0200 | [diff] [blame] | 99 | if (!usb_dev_get_parent(dev)) |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 100 | dev->speed = host->host_speed; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 101 | |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 102 | return submit_urb(&host->hcd, &host->urb); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Hans de Goede | fc85d39 | 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 | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 107 | { |
Hans de Goede | fc85d39 | 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 | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Hans de Goede | fc85d39 | 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, |
Michal Suchanek | 3437121 | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 115 | void *buffer, int len, int interval, bool nonblock) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 116 | { |
Hans de Goede | fc85d39 | 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 | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame] | 118 | buffer, len, NULL, interval); |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 119 | return submit_urb(&host->hcd, &host->urb); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Hans de Goede | fc85d39 | 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 | 904f2a8 | 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 | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 146 | ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0); |
Hans de Goede | 904f2a8 | 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 | fc85d39 | 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 | 904f2a8 | 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 | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 164 | musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME); |
Hans de Goede | 904f2a8 | 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 | fc85d39 | 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 | 904f2a8 | 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 | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 177 | host->host->isr(0, host->host); |
Hans de Goede | 904f2a8 | 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 | fc85d39 | 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) |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 187 | { |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 188 | void *mbase = host->host->mregs; |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 189 | u8 power; |
| 190 | |
| 191 | power = musb_readb(mbase, MUSB_POWER); |
| 192 | power &= 0xf0; |
| 193 | musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power); |
| 194 | mdelay(50); |
Jagan Teki | aa29b11 | 2018-05-07 13:03:37 +0530 | [diff] [blame] | 195 | |
| 196 | if (host->host->ops->pre_root_reset_end) |
| 197 | host->host->ops->pre_root_reset_end(host->host); |
| 198 | |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 199 | power = musb_readb(mbase, MUSB_POWER); |
| 200 | musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power); |
Jagan Teki | aa29b11 | 2018-05-07 13:03:37 +0530 | [diff] [blame] | 201 | |
| 202 | if (host->host->ops->post_root_reset_end) |
| 203 | host->host->ops->post_root_reset_end(host->host); |
| 204 | |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 205 | host->host->isr(0, host->host); |
| 206 | host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ? |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 207 | USB_SPEED_HIGH : |
| 208 | (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ? |
| 209 | USB_SPEED_FULL : USB_SPEED_LOW; |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 210 | mdelay((host->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 | |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 215 | int musb_lowlevel_init(struct musb_host_data *host) |
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 | |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 222 | if (!host->host) { |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 223 | printf("MUSB host is not registered\n"); |
| 224 | return -ENODEV; |
| 225 | } |
| 226 | |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 227 | ret = musb_start(host->host); |
Hans de Goede | 1583723 | 2015-06-17 21:33:54 +0200 | [diff] [blame] | 228 | if (ret) |
| 229 | return ret; |
| 230 | |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 231 | mbase = host->host->mregs; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 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); |
Hans de Goede | bf31323 | 2016-04-02 20:46:10 +0200 | [diff] [blame] | 236 | if (get_timer(0) >= timeout) { |
| 237 | musb_stop(host->host); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 238 | return -ENODEV; |
Hans de Goede | bf31323 | 2016-04-02 20:46:10 +0200 | [diff] [blame] | 239 | } |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 240 | |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 241 | _musb_reset_root_port(host, NULL); |
| 242 | host->host->is_active = 1; |
| 243 | host->hcd.hcd_priv = host->host; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 248 | #if !CONFIG_IS_ENABLED(DM_USB) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 249 | int usb_lowlevel_stop(int index) |
| 250 | { |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 251 | if (!musb_host.host) { |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 252 | printf("MUSB host is not registered\n"); |
| 253 | return -ENODEV; |
| 254 | } |
| 255 | |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 256 | musb_stop(musb_host.host); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 257 | return 0; |
| 258 | } |
Hans de Goede | 1398252 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 259 | |
| 260 | int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, |
| 261 | void *buffer, int length) |
| 262 | { |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 263 | return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length); |
Hans de Goede | 1398252 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | int submit_control_msg(struct usb_device *dev, unsigned long pipe, |
| 267 | void *buffer, int length, struct devrequest *setup) |
| 268 | { |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 269 | return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup); |
Hans de Goede | 1398252 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | int submit_int_msg(struct usb_device *dev, unsigned long pipe, |
Michal Suchanek | 3437121 | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 273 | void *buffer, int length, int interval, bool nonblock) |
Hans de Goede | 1398252 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 274 | { |
Michal Suchanek | 3437121 | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 275 | return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length, |
| 276 | interval, nonblock); |
Hans de Goede | 1398252 | 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 | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 283 | return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize, |
Hans de Goede | 1398252 | 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 | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 289 | return _musb_poll_int_queue(&musb_host, dev, queue); |
Hans de Goede | 1398252 | 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 | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 294 | return _musb_destroy_int_queue(&musb_host, dev, queue); |
Hans de Goede | 1398252 | 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 | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 299 | return _musb_reset_root_port(&musb_host, dev); |
Hans de Goede | 1398252 | 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 | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 304 | return musb_lowlevel_init(&musb_host); |
Hans de Goede | 1398252 | 2015-06-17 21:33:56 +0200 | [diff] [blame] | 305 | } |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 306 | #endif /* !CONFIG_IS_ENABLED(DM_USB) */ |
Hans de Goede | 09e7ea4 | 2015-06-17 21:33:58 +0200 | [diff] [blame] | 307 | |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 308 | #if CONFIG_IS_ENABLED(DM_USB) |
Hans de Goede | 09e7ea4 | 2015-06-17 21:33:58 +0200 | [diff] [blame] | 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, |
Michal Suchanek | 3437121 | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 326 | int interval, bool nonblock) |
Hans de Goede | 09e7ea4 | 2015-06-17 21:33:58 +0200 | [diff] [blame] | 327 | { |
| 328 | struct musb_host_data *host = dev_get_priv(dev); |
Michal Suchanek | 3437121 | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 329 | return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval, |
| 330 | nonblock); |
Hans de Goede | 09e7ea4 | 2015-06-17 21:33:58 +0200 | [diff] [blame] | 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 | }; |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 371 | #endif /* CONFIG_IS_ENABLED(DM_USB) */ |
Paul Kocialkowski | 95de1e2 | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 372 | #endif /* CONFIG_USB_MUSB_HOST */ |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 373 | |
Jean-Jacques Hiblot | 7d98dbc | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 374 | #if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 375 | static struct musb *gadget; |
| 376 | |
Kishon Vijay Abraham I | 2d48aa6 | 2015-02-23 18:40:23 +0530 | [diff] [blame] | 377 | int usb_gadget_handle_interrupts(int index) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 378 | { |
Heiko Schocher | 7a34278 | 2013-06-04 11:10:01 +0200 | [diff] [blame] | 379 | WATCHDOG_RESET(); |
Ilya Yanok | eb81955 | 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 | 76b09b8 | 2013-03-21 05:27:49 +0000 | [diff] [blame] | 390 | if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind || |
Ilya Yanok | eb81955 | 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 | 078d730 | 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 | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 422 | return 0; |
| 423 | } |
Paul Kocialkowski | 95de1e2 | 2015-08-04 17:04:06 +0200 | [diff] [blame] | 424 | #endif /* CONFIG_USB_MUSB_GADGET */ |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 425 | |
Jagan Teki | 8b8d59f | 2018-07-20 12:43:56 +0530 | [diff] [blame] | 426 | struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata, |
| 427 | void *ctl_regs) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 428 | { |
| 429 | struct musb **musbp; |
| 430 | |
| 431 | switch (plat->mode) { |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 432 | #if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 433 | case MUSB_HOST: |
Hans de Goede | fc85d39 | 2015-06-17 21:33:57 +0200 | [diff] [blame] | 434 | musbp = &musb_host.host; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 435 | break; |
| 436 | #endif |
Jean-Jacques Hiblot | 7d98dbc | 2018-12-04 11:30:57 +0100 | [diff] [blame] | 437 | #if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 438 | case MUSB_PERIPHERAL: |
| 439 | musbp = &gadget; |
| 440 | break; |
| 441 | #endif |
| 442 | default: |
Jagan Teki | 8b8d59f | 2018-07-20 12:43:56 +0530 | [diff] [blame] | 443 | return ERR_PTR(-EINVAL); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs); |
Jagan Teki | 8b8d59f | 2018-07-20 12:43:56 +0530 | [diff] [blame] | 447 | if (IS_ERR(*musbp)) { |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 448 | printf("Failed to init the controller\n"); |
Jagan Teki | 8b8d59f | 2018-07-20 12:43:56 +0530 | [diff] [blame] | 449 | return ERR_CAST(*musbp); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Jagan Teki | 8b8d59f | 2018-07-20 12:43:56 +0530 | [diff] [blame] | 452 | return *musbp; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 453 | } |