blob: 72f14b93438780411e7d2cd334443db9f8b51925 [file] [log] [blame]
Ilya Yanokeb819552012-11-06 13:48:21 +00001#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -07002#include <console.h>
Simon Glass336d4612020-02-03 07:36:16 -07003#include <malloc.h>
Heiko Schocher7a342782013-06-04 11:10:01 +02004#include <watchdog.h>
Simon Glass61b29b82020-02-03 07:36:15 -07005#include <linux/err.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +09006#include <linux/errno.h>
Ilya Yanokeb819552012-11-06 13:48:21 +00007#include <linux/usb/ch9.h>
8#include <linux/usb/gadget.h>
9
Ilya Yanokeb819552012-11-06 13:48:21 +000010#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 Goedefc85d392015-06-17 21:33:57 +020016#include "musb_uboot.h"
Ilya Yanokeb819552012-11-06 13:48:21 +000017
Paul Kocialkowski95de1e22015-08-04 17:04:06 +020018#ifdef CONFIG_USB_MUSB_HOST
Hans de Goede904f2a82015-01-11 20:34:54 +010019struct int_queue {
20 struct usb_host_endpoint hep;
21 struct urb urb;
22};
23
Sven Schwermerfd09c202018-11-21 08:43:56 +010024#if !CONFIG_IS_ENABLED(DM_USB)
Hans de Goedefc85d392015-06-17 21:33:57 +020025struct musb_host_data musb_host;
Hans de Goede09e7ea42015-06-17 21:33:58 +020026#endif
Ilya Yanokeb819552012-11-06 13:48:21 +000027
28static 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 Goedeaccf04c2015-01-11 20:34:53 +010034static 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 Yanokeb819552012-11-06 13:48:21 +000038{
39 int epnum = usb_pipeendpoint(pipe);
40 int is_in = usb_pipein(pipe);
41
Hans de Goedeaccf04c2015-01-11 20:34:53 +010042 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 Yanokeb819552012-11-06 13:48:21 +000055
Hans de Goedeaccf04c2015-01-11 20:34:53 +010056 urb->ep->desc.wMaxPacketSize =
Ilya Yanokeb819552012-11-06 13:48:21 +000057 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
58 dev->epmaxpacketout[epnum]);
Hans de Goedeaccf04c2015-01-11 20:34:53 +010059 urb->ep->desc.bmAttributes = endpoint_type;
60 urb->ep->desc.bEndpointAddress =
Ilya Yanokeb819552012-11-06 13:48:21 +000061 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
Hans de Goedeaccf04c2015-01-11 20:34:53 +010062 urb->ep->desc.bInterval = interval;
Ilya Yanokeb819552012-11-06 13:48:21 +000063}
64
Ilya Yanokeb819552012-11-06 13:48:21 +000065static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
66{
67 struct musb *host = hcd->hcd_priv;
68 int ret;
Hans de Goededc9a3912015-01-11 20:34:49 +010069 unsigned long timeout;
Ilya Yanokeb819552012-11-06 13:48:21 +000070
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 Goededc9a3912015-01-11 20:34:49 +010077 timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
Ilya Yanokeb819552012-11-06 13:48:21 +000078 do {
79 if (ctrlc())
80 return -EIO;
81 host->isr(0, host);
Hans de Goedee8672e32015-01-11 20:34:50 +010082 } while (urb->status == -EINPROGRESS &&
Hans de Goededc9a3912015-01-11 20:34:49 +010083 get_timer(0) < timeout);
Ilya Yanokeb819552012-11-06 13:48:21 +000084
Hans de Goedeb918a0c2015-01-11 20:34:52 +010085 if (urb->status == -EINPROGRESS)
86 musb_urb_dequeue(hcd, urb, -ETIME);
87
Ilya Yanokeb819552012-11-06 13:48:21 +000088 return urb->status;
89}
90
Hans de Goedefc85d392015-06-17 21:33:57 +020091static 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 Yanokeb819552012-11-06 13:48:21 +000094{
Hans de Goedefc85d392015-06-17 21:33:57 +020095 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
96 pipe, buffer, len, setup, 0);
Ilya Yanokeb819552012-11-06 13:48:21 +000097
98 /* Fix speed for non hub-attached devices */
Hans de Goedee740ca32015-06-17 21:33:55 +020099 if (!usb_dev_get_parent(dev))
Hans de Goedefc85d392015-06-17 21:33:57 +0200100 dev->speed = host->host_speed;
Ilya Yanokeb819552012-11-06 13:48:21 +0000101
Hans de Goedefc85d392015-06-17 21:33:57 +0200102 return submit_urb(&host->hcd, &host->urb);
Ilya Yanokeb819552012-11-06 13:48:21 +0000103}
104
Hans de Goedefc85d392015-06-17 21:33:57 +0200105static int _musb_submit_bulk_msg(struct musb_host_data *host,
106 struct usb_device *dev, unsigned long pipe, void *buffer, int len)
Ilya Yanokeb819552012-11-06 13:48:21 +0000107{
Hans de Goedefc85d392015-06-17 21:33:57 +0200108 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 Yanokeb819552012-11-06 13:48:21 +0000111}
112
Hans de Goedefc85d392015-06-17 21:33:57 +0200113static int _musb_submit_int_msg(struct musb_host_data *host,
114 struct usb_device *dev, unsigned long pipe,
Michal Suchanek34371212019-08-18 10:55:27 +0200115 void *buffer, int len, int interval, bool nonblock)
Ilya Yanokeb819552012-11-06 13:48:21 +0000116{
Hans de Goedefc85d392015-06-17 21:33:57 +0200117 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe,
Hans de Goedeaccf04c2015-01-11 20:34:53 +0100118 buffer, len, NULL, interval);
Hans de Goedefc85d392015-06-17 21:33:57 +0200119 return submit_urb(&host->hcd, &host->urb);
Ilya Yanokeb819552012-11-06 13:48:21 +0000120}
121
Hans de Goedefc85d392015-06-17 21:33:57 +0200122static 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 Goede904f2a82015-01-11 20:34:54 +0100125{
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 Goedefc85d392015-06-17 21:33:57 +0200146 ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
Hans de Goede904f2a82015-01-11 20:34:54 +0100147 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 Goedefc85d392015-06-17 21:33:57 +0200157static int _musb_destroy_int_queue(struct musb_host_data *host,
158 struct usb_device *dev, struct int_queue *queue)
Hans de Goede904f2a82015-01-11 20:34:54 +0100159{
160 int index = usb_pipein(queue->urb.pipe) * 16 +
161 usb_pipeendpoint(queue->urb.pipe);
162
163 if (queue->urb.status == -EINPROGRESS)
Hans de Goedefc85d392015-06-17 21:33:57 +0200164 musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);
Hans de Goede904f2a82015-01-11 20:34:54 +0100165
166 dev->int_pending &= ~(1 << index);
167 free(queue);
168 return 0;
169}
170
Hans de Goedefc85d392015-06-17 21:33:57 +0200171static void *_musb_poll_int_queue(struct musb_host_data *host,
172 struct usb_device *dev, struct int_queue *queue)
Hans de Goede904f2a82015-01-11 20:34:54 +0100173{
174 if (queue->urb.status != -EINPROGRESS)
175 return NULL; /* URB has already completed in a prev. poll */
176
Hans de Goedefc85d392015-06-17 21:33:57 +0200177 host->host->isr(0, host->host);
Hans de Goede904f2a82015-01-11 20:34:54 +0100178
179 if (queue->urb.status != -EINPROGRESS)
180 return queue->urb.transfer_buffer; /* Done */
181
182 return NULL; /* URB still pending */
183}
184
Hans de Goedefc85d392015-06-17 21:33:57 +0200185static int _musb_reset_root_port(struct musb_host_data *host,
186 struct usb_device *dev)
Hans de Goede90cdc102015-01-11 20:34:51 +0100187{
Hans de Goedefc85d392015-06-17 21:33:57 +0200188 void *mbase = host->host->mregs;
Hans de Goede90cdc102015-01-11 20:34:51 +0100189 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 Tekiaa29b112018-05-07 13:03:37 +0530195
196 if (host->host->ops->pre_root_reset_end)
197 host->host->ops->pre_root_reset_end(host->host);
198
Hans de Goede90cdc102015-01-11 20:34:51 +0100199 power = musb_readb(mbase, MUSB_POWER);
200 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
Jagan Tekiaa29b112018-05-07 13:03:37 +0530201
202 if (host->host->ops->post_root_reset_end)
203 host->host->ops->post_root_reset_end(host->host);
204
Hans de Goedefc85d392015-06-17 21:33:57 +0200205 host->host->isr(0, host->host);
206 host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
Hans de Goede90cdc102015-01-11 20:34:51 +0100207 USB_SPEED_HIGH :
208 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
209 USB_SPEED_FULL : USB_SPEED_LOW;
Hans de Goedefc85d392015-06-17 21:33:57 +0200210 mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50);
Simon Glassde312132015-03-25 12:21:59 -0600211
212 return 0;
Hans de Goede90cdc102015-01-11 20:34:51 +0100213}
214
Hans de Goedefc85d392015-06-17 21:33:57 +0200215int musb_lowlevel_init(struct musb_host_data *host)
Ilya Yanokeb819552012-11-06 13:48:21 +0000216{
Ilya Yanokeb819552012-11-06 13:48:21 +0000217 void *mbase;
Hans de Goededc9a3912015-01-11 20:34:49 +0100218 /* 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 Goede15837232015-06-17 21:33:54 +0200220 int ret;
Ilya Yanokeb819552012-11-06 13:48:21 +0000221
Hans de Goedefc85d392015-06-17 21:33:57 +0200222 if (!host->host) {
Ilya Yanokeb819552012-11-06 13:48:21 +0000223 printf("MUSB host is not registered\n");
224 return -ENODEV;
225 }
226
Hans de Goedefc85d392015-06-17 21:33:57 +0200227 ret = musb_start(host->host);
Hans de Goede15837232015-06-17 21:33:54 +0200228 if (ret)
229 return ret;
230
Hans de Goedefc85d392015-06-17 21:33:57 +0200231 mbase = host->host->mregs;
Ilya Yanokeb819552012-11-06 13:48:21 +0000232 do {
233 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
234 break;
Hans de Goededc9a3912015-01-11 20:34:49 +0100235 } while (get_timer(0) < timeout);
Hans de Goedebf313232016-04-02 20:46:10 +0200236 if (get_timer(0) >= timeout) {
237 musb_stop(host->host);
Ilya Yanokeb819552012-11-06 13:48:21 +0000238 return -ENODEV;
Hans de Goedebf313232016-04-02 20:46:10 +0200239 }
Ilya Yanokeb819552012-11-06 13:48:21 +0000240
Hans de Goedefc85d392015-06-17 21:33:57 +0200241 _musb_reset_root_port(host, NULL);
242 host->host->is_active = 1;
243 host->hcd.hcd_priv = host->host;
Ilya Yanokeb819552012-11-06 13:48:21 +0000244
245 return 0;
246}
247
Sven Schwermerfd09c202018-11-21 08:43:56 +0100248#if !CONFIG_IS_ENABLED(DM_USB)
Ilya Yanokeb819552012-11-06 13:48:21 +0000249int usb_lowlevel_stop(int index)
250{
Hans de Goedefc85d392015-06-17 21:33:57 +0200251 if (!musb_host.host) {
Ilya Yanokeb819552012-11-06 13:48:21 +0000252 printf("MUSB host is not registered\n");
253 return -ENODEV;
254 }
255
Hans de Goedefc85d392015-06-17 21:33:57 +0200256 musb_stop(musb_host.host);
Ilya Yanokeb819552012-11-06 13:48:21 +0000257 return 0;
258}
Hans de Goede13982522015-06-17 21:33:56 +0200259
260int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
261 void *buffer, int length)
262{
Hans de Goedefc85d392015-06-17 21:33:57 +0200263 return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
Hans de Goede13982522015-06-17 21:33:56 +0200264}
265
266int submit_control_msg(struct usb_device *dev, unsigned long pipe,
267 void *buffer, int length, struct devrequest *setup)
268{
Hans de Goedefc85d392015-06-17 21:33:57 +0200269 return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
Hans de Goede13982522015-06-17 21:33:56 +0200270}
271
272int submit_int_msg(struct usb_device *dev, unsigned long pipe,
Michal Suchanek34371212019-08-18 10:55:27 +0200273 void *buffer, int length, int interval, bool nonblock)
Hans de Goede13982522015-06-17 21:33:56 +0200274{
Michal Suchanek34371212019-08-18 10:55:27 +0200275 return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length,
276 interval, nonblock);
Hans de Goede13982522015-06-17 21:33:56 +0200277}
278
279struct 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 Goedefc85d392015-06-17 21:33:57 +0200283 return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
Hans de Goede13982522015-06-17 21:33:56 +0200284 buffer, interval);
285}
286
287void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
288{
Hans de Goedefc85d392015-06-17 21:33:57 +0200289 return _musb_poll_int_queue(&musb_host, dev, queue);
Hans de Goede13982522015-06-17 21:33:56 +0200290}
291
292int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
293{
Hans de Goedefc85d392015-06-17 21:33:57 +0200294 return _musb_destroy_int_queue(&musb_host, dev, queue);
Hans de Goede13982522015-06-17 21:33:56 +0200295}
296
297int usb_reset_root_port(struct usb_device *dev)
298{
Hans de Goedefc85d392015-06-17 21:33:57 +0200299 return _musb_reset_root_port(&musb_host, dev);
Hans de Goede13982522015-06-17 21:33:56 +0200300}
301
302int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
303{
Hans de Goedefc85d392015-06-17 21:33:57 +0200304 return musb_lowlevel_init(&musb_host);
Hans de Goede13982522015-06-17 21:33:56 +0200305}
Sven Schwermerfd09c202018-11-21 08:43:56 +0100306#endif /* !CONFIG_IS_ENABLED(DM_USB) */
Hans de Goede09e7ea42015-06-17 21:33:58 +0200307
Sven Schwermerfd09c202018-11-21 08:43:56 +0100308#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goede09e7ea42015-06-17 21:33:58 +0200309static 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
317static 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
324static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev,
325 unsigned long pipe, void *buffer, int length,
Michal Suchanek34371212019-08-18 10:55:27 +0200326 int interval, bool nonblock)
Hans de Goede09e7ea42015-06-17 21:33:58 +0200327{
328 struct musb_host_data *host = dev_get_priv(dev);
Michal Suchanek34371212019-08-18 10:55:27 +0200329 return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval,
330 nonblock);
Hans de Goede09e7ea42015-06-17 21:33:58 +0200331}
332
333static 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
342static 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
349static 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
356static 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
362struct 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 Schwermerfd09c202018-11-21 08:43:56 +0100371#endif /* CONFIG_IS_ENABLED(DM_USB) */
Paul Kocialkowski95de1e22015-08-04 17:04:06 +0200372#endif /* CONFIG_USB_MUSB_HOST */
Ilya Yanokeb819552012-11-06 13:48:21 +0000373
Jean-Jacques Hiblot7d98dbc2018-12-04 11:30:57 +0100374#if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
Ilya Yanokeb819552012-11-06 13:48:21 +0000375static struct musb *gadget;
376
Kishon Vijay Abraham I2d48aa62015-02-23 18:40:23 +0530377int usb_gadget_handle_interrupts(int index)
Ilya Yanokeb819552012-11-06 13:48:21 +0000378{
Heiko Schocher7a342782013-06-04 11:10:01 +0200379 WATCHDOG_RESET();
Ilya Yanokeb819552012-11-06 13:48:21 +0000380 if (!gadget || !gadget->isr)
381 return -EINVAL;
382
383 return gadget->isr(0, gadget);
384}
385
386int usb_gadget_register_driver(struct usb_gadget_driver *driver)
387{
388 int ret;
389
Bin Liu76b09b82013-03-21 05:27:49 +0000390 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
Ilya Yanokeb819552012-11-06 13:48:21 +0000391 !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
416int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
417{
Rob Herring078d7302014-04-18 08:54:30 -0500418 if (driver->disconnect)
419 driver->disconnect(&gadget->g);
420 if (driver->unbind)
421 driver->unbind(&gadget->g);
Ilya Yanokeb819552012-11-06 13:48:21 +0000422 return 0;
423}
Paul Kocialkowski95de1e22015-08-04 17:04:06 +0200424#endif /* CONFIG_USB_MUSB_GADGET */
Ilya Yanokeb819552012-11-06 13:48:21 +0000425
Jagan Teki8b8d59f2018-07-20 12:43:56 +0530426struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
427 void *ctl_regs)
Ilya Yanokeb819552012-11-06 13:48:21 +0000428{
429 struct musb **musbp;
430
431 switch (plat->mode) {
Sven Schwermerfd09c202018-11-21 08:43:56 +0100432#if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB)
Ilya Yanokeb819552012-11-06 13:48:21 +0000433 case MUSB_HOST:
Hans de Goedefc85d392015-06-17 21:33:57 +0200434 musbp = &musb_host.host;
Ilya Yanokeb819552012-11-06 13:48:21 +0000435 break;
436#endif
Jean-Jacques Hiblot7d98dbc2018-12-04 11:30:57 +0100437#if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
Ilya Yanokeb819552012-11-06 13:48:21 +0000438 case MUSB_PERIPHERAL:
439 musbp = &gadget;
440 break;
441#endif
442 default:
Jagan Teki8b8d59f2018-07-20 12:43:56 +0530443 return ERR_PTR(-EINVAL);
Ilya Yanokeb819552012-11-06 13:48:21 +0000444 }
445
446 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
Jagan Teki8b8d59f2018-07-20 12:43:56 +0530447 if (IS_ERR(*musbp)) {
Ilya Yanokeb819552012-11-06 13:48:21 +0000448 printf("Failed to init the controller\n");
Jagan Teki8b8d59f2018-07-20 12:43:56 +0530449 return ERR_CAST(*musbp);
Ilya Yanokeb819552012-11-06 13:48:21 +0000450 }
451
Jagan Teki8b8d59f2018-07-20 12:43:56 +0530452 return *musbp;
Ilya Yanokeb819552012-11-06 13:48:21 +0000453}