blob: 43ab3245e5c3245540b3b980e3ae5ca4fa93ff8a [file] [log] [blame]
Simon Glassa73bda42015-11-08 23:47:45 -07001#include <console.h>
Simon Glass61adc042020-07-19 10:15:55 -06002#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07003#include <malloc.h>
Heiko Schocherc3ffe502013-06-04 11:10:01 +02004#include <watchdog.h>
Simon Glassdbd79542020-05-10 11:40:11 -06005#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -07006#include <linux/err.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +09007#include <linux/errno.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +00008#include <linux/usb/ch9.h>
9#include <linux/usb/gadget.h>
Rui Miguel Silvafb0c70c2022-06-29 11:06:14 +010010#include <linux/usb/usb_urb_compat.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +000011
Ilya Yanok06bb9202012-11-06 13:48:21 +000012#include <usb.h>
13#include "linux-compat.h"
Ilya Yanok06bb9202012-11-06 13:48:21 +000014#include "musb_core.h"
15#include "musb_host.h"
16#include "musb_gadget.h"
Hans de Goede59627482015-06-17 21:33:57 +020017#include "musb_uboot.h"
Ilya Yanok06bb9202012-11-06 13:48:21 +000018
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +020019#ifdef CONFIG_USB_MUSB_HOST
Hans de Goede83b18972015-01-11 20:34:54 +010020struct int_queue {
21 struct usb_host_endpoint hep;
22 struct urb urb;
23};
24
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010025#if !CONFIG_IS_ENABLED(DM_USB)
Hans de Goede59627482015-06-17 21:33:57 +020026struct musb_host_data musb_host;
Hans de Goedefc649612015-06-17 21:33:58 +020027#endif
Ilya Yanok06bb9202012-11-06 13:48:21 +000028
29static void musb_host_complete_urb(struct urb *urb)
30{
31 urb->dev->status &= ~USB_ST_NOT_PROC;
32 urb->dev->act_len = urb->actual_length;
33}
34
Hans de Goede39b08452015-01-11 20:34:53 +010035static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep,
36 struct usb_device *dev, int endpoint_type,
37 unsigned long pipe, void *buffer, int len,
38 struct devrequest *setup, int interval)
Ilya Yanok06bb9202012-11-06 13:48:21 +000039{
40 int epnum = usb_pipeendpoint(pipe);
41 int is_in = usb_pipein(pipe);
42
Hans de Goede39b08452015-01-11 20:34:53 +010043 memset(urb, 0, sizeof(struct urb));
44 memset(hep, 0, sizeof(struct usb_host_endpoint));
45 INIT_LIST_HEAD(&hep->urb_list);
46 INIT_LIST_HEAD(&urb->urb_list);
47 urb->ep = hep;
48 urb->complete = musb_host_complete_urb;
49 urb->status = -EINPROGRESS;
50 urb->dev = dev;
51 urb->pipe = pipe;
52 urb->transfer_buffer = buffer;
53 urb->transfer_dma = (unsigned long)buffer;
54 urb->transfer_buffer_length = len;
55 urb->setup_packet = (unsigned char *)setup;
Ilya Yanok06bb9202012-11-06 13:48:21 +000056
Hans de Goede39b08452015-01-11 20:34:53 +010057 urb->ep->desc.wMaxPacketSize =
Ilya Yanok06bb9202012-11-06 13:48:21 +000058 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
59 dev->epmaxpacketout[epnum]);
Hans de Goede39b08452015-01-11 20:34:53 +010060 urb->ep->desc.bmAttributes = endpoint_type;
61 urb->ep->desc.bEndpointAddress =
Ilya Yanok06bb9202012-11-06 13:48:21 +000062 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
Hans de Goede39b08452015-01-11 20:34:53 +010063 urb->ep->desc.bInterval = interval;
Ilya Yanok06bb9202012-11-06 13:48:21 +000064}
65
Ilya Yanok06bb9202012-11-06 13:48:21 +000066static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
67{
68 struct musb *host = hcd->hcd_priv;
69 int ret;
Hans de Goede8de4ded2015-01-11 20:34:49 +010070 unsigned long timeout;
Ilya Yanok06bb9202012-11-06 13:48:21 +000071
72 ret = musb_urb_enqueue(hcd, urb, 0);
73 if (ret < 0) {
74 printf("Failed to enqueue URB to controller\n");
75 return ret;
76 }
77
Hans de Goede8de4ded2015-01-11 20:34:49 +010078 timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
Ilya Yanok06bb9202012-11-06 13:48:21 +000079 do {
80 if (ctrlc())
81 return -EIO;
82 host->isr(0, host);
Hans de Goede24fd7772015-01-11 20:34:50 +010083 } while (urb->status == -EINPROGRESS &&
Hans de Goede8de4ded2015-01-11 20:34:49 +010084 get_timer(0) < timeout);
Ilya Yanok06bb9202012-11-06 13:48:21 +000085
Hans de Goede0e4da952015-01-11 20:34:52 +010086 if (urb->status == -EINPROGRESS)
87 musb_urb_dequeue(hcd, urb, -ETIME);
88
Ilya Yanok06bb9202012-11-06 13:48:21 +000089 return urb->status;
90}
91
Hans de Goede59627482015-06-17 21:33:57 +020092static int _musb_submit_control_msg(struct musb_host_data *host,
93 struct usb_device *dev, unsigned long pipe,
94 void *buffer, int len, struct devrequest *setup)
Ilya Yanok06bb9202012-11-06 13:48:21 +000095{
Hans de Goede59627482015-06-17 21:33:57 +020096 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
97 pipe, buffer, len, setup, 0);
Ilya Yanok06bb9202012-11-06 13:48:21 +000098
99 /* Fix speed for non hub-attached devices */
Hans de Goede4d8b8df2015-06-17 21:33:55 +0200100 if (!usb_dev_get_parent(dev))
Hans de Goede59627482015-06-17 21:33:57 +0200101 dev->speed = host->host_speed;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000102
Hans de Goede59627482015-06-17 21:33:57 +0200103 return submit_urb(&host->hcd, &host->urb);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000104}
105
Hans de Goede59627482015-06-17 21:33:57 +0200106static int _musb_submit_bulk_msg(struct musb_host_data *host,
107 struct usb_device *dev, unsigned long pipe, void *buffer, int len)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000108{
Hans de Goede59627482015-06-17 21:33:57 +0200109 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_BULK,
110 pipe, buffer, len, NULL, 0);
111 return submit_urb(&host->hcd, &host->urb);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000112}
113
Hans de Goede59627482015-06-17 21:33:57 +0200114static int _musb_submit_int_msg(struct musb_host_data *host,
115 struct usb_device *dev, unsigned long pipe,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200116 void *buffer, int len, int interval, bool nonblock)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000117{
Hans de Goede59627482015-06-17 21:33:57 +0200118 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe,
Hans de Goede39b08452015-01-11 20:34:53 +0100119 buffer, len, NULL, interval);
Hans de Goede59627482015-06-17 21:33:57 +0200120 return submit_urb(&host->hcd, &host->urb);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000121}
122
Hans de Goede59627482015-06-17 21:33:57 +0200123static struct int_queue *_musb_create_int_queue(struct musb_host_data *host,
124 struct usb_device *dev, unsigned long pipe, int queuesize,
125 int elementsize, void *buffer, int interval)
Hans de Goede83b18972015-01-11 20:34:54 +0100126{
127 struct int_queue *queue;
128 int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
129
130 if (queuesize != 1) {
131 printf("ERROR musb int-queues only support queuesize 1\n");
132 return NULL;
133 }
134
135 if (dev->int_pending & (1 << index)) {
136 printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
137 return NULL;
138 }
139
140 queue = malloc(sizeof(*queue));
141 if (!queue)
142 return NULL;
143
144 construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
145 pipe, buffer, elementsize, NULL, interval);
146
Hans de Goede59627482015-06-17 21:33:57 +0200147 ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
Hans de Goede83b18972015-01-11 20:34:54 +0100148 if (ret < 0) {
149 printf("Failed to enqueue URB to controller\n");
150 free(queue);
151 return NULL;
152 }
153
154 dev->int_pending |= 1 << index;
155 return queue;
156}
157
Hans de Goede59627482015-06-17 21:33:57 +0200158static int _musb_destroy_int_queue(struct musb_host_data *host,
159 struct usb_device *dev, struct int_queue *queue)
Hans de Goede83b18972015-01-11 20:34:54 +0100160{
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200161 int index = usb_pipein(queue->urb.pipe) * 16 +
Hans de Goede83b18972015-01-11 20:34:54 +0100162 usb_pipeendpoint(queue->urb.pipe);
163
164 if (queue->urb.status == -EINPROGRESS)
Hans de Goede59627482015-06-17 21:33:57 +0200165 musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);
Hans de Goede83b18972015-01-11 20:34:54 +0100166
167 dev->int_pending &= ~(1 << index);
168 free(queue);
169 return 0;
170}
171
Hans de Goede59627482015-06-17 21:33:57 +0200172static void *_musb_poll_int_queue(struct musb_host_data *host,
173 struct usb_device *dev, struct int_queue *queue)
Hans de Goede83b18972015-01-11 20:34:54 +0100174{
175 if (queue->urb.status != -EINPROGRESS)
176 return NULL; /* URB has already completed in a prev. poll */
177
Hans de Goede59627482015-06-17 21:33:57 +0200178 host->host->isr(0, host->host);
Hans de Goede83b18972015-01-11 20:34:54 +0100179
180 if (queue->urb.status != -EINPROGRESS)
181 return queue->urb.transfer_buffer; /* Done */
182
183 return NULL; /* URB still pending */
184}
185
Hans de Goede59627482015-06-17 21:33:57 +0200186static int _musb_reset_root_port(struct musb_host_data *host,
187 struct usb_device *dev)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000188{
Hans de Goede59627482015-06-17 21:33:57 +0200189 void *mbase = host->host->mregs;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000190 u8 power;
Hans de Goede498ab952015-01-11 20:34:51 +0100191
192 power = musb_readb(mbase, MUSB_POWER);
193 power &= 0xf0;
194 musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
195 mdelay(50);
Jagan Teki37671e12018-05-07 13:03:37 +0530196
197 if (host->host->ops->pre_root_reset_end)
198 host->host->ops->pre_root_reset_end(host->host);
199
Hans de Goede498ab952015-01-11 20:34:51 +0100200 power = musb_readb(mbase, MUSB_POWER);
201 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
Jagan Teki37671e12018-05-07 13:03:37 +0530202
203 if (host->host->ops->post_root_reset_end)
204 host->host->ops->post_root_reset_end(host->host);
205
Hans de Goede59627482015-06-17 21:33:57 +0200206 host->host->isr(0, host->host);
207 host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
Hans de Goede498ab952015-01-11 20:34:51 +0100208 USB_SPEED_HIGH :
209 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
210 USB_SPEED_FULL : USB_SPEED_LOW;
Hans de Goede59627482015-06-17 21:33:57 +0200211 mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600212
213 return 0;
Hans de Goede498ab952015-01-11 20:34:51 +0100214}
215
Hans de Goede59627482015-06-17 21:33:57 +0200216int musb_lowlevel_init(struct musb_host_data *host)
Hans de Goede498ab952015-01-11 20:34:51 +0100217{
Ilya Yanok06bb9202012-11-06 13:48:21 +0000218 void *mbase;
Hans de Goede8de4ded2015-01-11 20:34:49 +0100219 /* USB spec says it may take up to 1 second for a device to connect */
220 unsigned long timeout = get_timer(0) + 1000;
Hans de Goede81c49982015-06-17 21:33:54 +0200221 int ret;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000222
Hans de Goede59627482015-06-17 21:33:57 +0200223 if (!host->host) {
Ilya Yanok06bb9202012-11-06 13:48:21 +0000224 printf("MUSB host is not registered\n");
225 return -ENODEV;
226 }
227
Hans de Goede59627482015-06-17 21:33:57 +0200228 ret = musb_start(host->host);
Hans de Goede81c49982015-06-17 21:33:54 +0200229 if (ret)
230 return ret;
231
Hans de Goede59627482015-06-17 21:33:57 +0200232 mbase = host->host->mregs;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000233 do {
234 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
235 break;
Hans de Goede8de4ded2015-01-11 20:34:49 +0100236 } while (get_timer(0) < timeout);
Hans de Goede2ec5be62016-04-02 20:46:10 +0200237 if (get_timer(0) >= timeout) {
238 musb_stop(host->host);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000239 return -ENODEV;
Hans de Goede2ec5be62016-04-02 20:46:10 +0200240 }
Ilya Yanok06bb9202012-11-06 13:48:21 +0000241
Hans de Goede59627482015-06-17 21:33:57 +0200242 _musb_reset_root_port(host, NULL);
243 host->host->is_active = 1;
244 host->hcd.hcd_priv = host->host;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000245
246 return 0;
247}
248
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100249#if !CONFIG_IS_ENABLED(DM_USB)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000250int usb_lowlevel_stop(int index)
251{
Hans de Goede59627482015-06-17 21:33:57 +0200252 if (!musb_host.host) {
Ilya Yanok06bb9202012-11-06 13:48:21 +0000253 printf("MUSB host is not registered\n");
254 return -ENODEV;
255 }
256
Hans de Goede59627482015-06-17 21:33:57 +0200257 musb_stop(musb_host.host);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000258 return 0;
259}
Hans de Goede52666e22015-06-17 21:33:56 +0200260
261int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
262 void *buffer, int length)
263{
Hans de Goede59627482015-06-17 21:33:57 +0200264 return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
Hans de Goede52666e22015-06-17 21:33:56 +0200265}
266
267int submit_control_msg(struct usb_device *dev, unsigned long pipe,
268 void *buffer, int length, struct devrequest *setup)
269{
Hans de Goede59627482015-06-17 21:33:57 +0200270 return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
Hans de Goede52666e22015-06-17 21:33:56 +0200271}
272
273int submit_int_msg(struct usb_device *dev, unsigned long pipe,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200274 void *buffer, int length, int interval, bool nonblock)
Hans de Goede52666e22015-06-17 21:33:56 +0200275{
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200276 return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length,
277 interval, nonblock);
Hans de Goede52666e22015-06-17 21:33:56 +0200278}
279
280struct int_queue *create_int_queue(struct usb_device *dev,
281 unsigned long pipe, int queuesize, int elementsize,
282 void *buffer, int interval)
283{
Hans de Goede59627482015-06-17 21:33:57 +0200284 return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
Hans de Goede52666e22015-06-17 21:33:56 +0200285 buffer, interval);
286}
287
288void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
289{
Hans de Goede59627482015-06-17 21:33:57 +0200290 return _musb_poll_int_queue(&musb_host, dev, queue);
Hans de Goede52666e22015-06-17 21:33:56 +0200291}
292
293int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
294{
Hans de Goede59627482015-06-17 21:33:57 +0200295 return _musb_destroy_int_queue(&musb_host, dev, queue);
Hans de Goede52666e22015-06-17 21:33:56 +0200296}
297
298int usb_reset_root_port(struct usb_device *dev)
299{
Hans de Goede59627482015-06-17 21:33:57 +0200300 return _musb_reset_root_port(&musb_host, dev);
Hans de Goede52666e22015-06-17 21:33:56 +0200301}
302
303int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
304{
Hans de Goede59627482015-06-17 21:33:57 +0200305 return musb_lowlevel_init(&musb_host);
Hans de Goede52666e22015-06-17 21:33:56 +0200306}
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100307#endif /* !CONFIG_IS_ENABLED(DM_USB) */
Hans de Goedefc649612015-06-17 21:33:58 +0200308
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100309#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goedefc649612015-06-17 21:33:58 +0200310static int musb_submit_control_msg(struct udevice *dev, struct usb_device *udev,
311 unsigned long pipe, void *buffer, int length,
312 struct devrequest *setup)
313{
314 struct musb_host_data *host = dev_get_priv(dev);
315 return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup);
316}
317
318static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
319 unsigned long pipe, void *buffer, int length)
320{
321 struct musb_host_data *host = dev_get_priv(dev);
322 return _musb_submit_bulk_msg(host, udev, pipe, buffer, length);
323}
324
325static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev,
326 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200327 int interval, bool nonblock)
Hans de Goedefc649612015-06-17 21:33:58 +0200328{
329 struct musb_host_data *host = dev_get_priv(dev);
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200330 return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval,
331 nonblock);
Hans de Goedefc649612015-06-17 21:33:58 +0200332}
333
334static struct int_queue *musb_create_int_queue(struct udevice *dev,
335 struct usb_device *udev, unsigned long pipe, int queuesize,
336 int elementsize, void *buffer, int interval)
337{
338 struct musb_host_data *host = dev_get_priv(dev);
339 return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize,
340 buffer, interval);
341}
342
343static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev,
344 struct int_queue *queue)
345{
346 struct musb_host_data *host = dev_get_priv(dev);
347 return _musb_poll_int_queue(host, udev, queue);
348}
349
350static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
351 struct int_queue *queue)
352{
353 struct musb_host_data *host = dev_get_priv(dev);
354 return _musb_destroy_int_queue(host, udev, queue);
355}
356
357static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev)
358{
359 struct musb_host_data *host = dev_get_priv(dev);
360 return _musb_reset_root_port(host, udev);
361}
362
363struct dm_usb_ops musb_usb_ops = {
364 .control = musb_submit_control_msg,
365 .bulk = musb_submit_bulk_msg,
366 .interrupt = musb_submit_int_msg,
367 .create_int_queue = musb_create_int_queue,
368 .poll_int_queue = musb_poll_int_queue,
369 .destroy_int_queue = musb_destroy_int_queue,
370 .reset_root_port = musb_reset_root_port,
371};
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100372#endif /* CONFIG_IS_ENABLED(DM_USB) */
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200373#endif /* CONFIG_USB_MUSB_HOST */
Ilya Yanok06bb9202012-11-06 13:48:21 +0000374
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100375#if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000376static struct musb *gadget;
377
Marek Vasut283bbec2023-09-01 11:50:03 +0200378int dm_usb_gadget_handle_interrupts(struct udevice *dev)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000379{
Stefan Roese80877fa2022-09-02 14:10:46 +0200380 schedule();
Ilya Yanok06bb9202012-11-06 13:48:21 +0000381 if (!gadget || !gadget->isr)
382 return -EINVAL;
383
384 return gadget->isr(0, gadget);
385}
386
387int usb_gadget_register_driver(struct usb_gadget_driver *driver)
388{
389 int ret;
390
Bin Liu28f2f352013-03-21 05:27:49 +0000391 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
Ilya Yanok06bb9202012-11-06 13:48:21 +0000392 !driver->setup) {
393 printf("bad parameter.\n");
394 return -EINVAL;
395 }
396
397 if (!gadget) {
398 printf("Controller uninitialized\n");
399 return -ENXIO;
400 }
401
402 ret = musb_gadget_start(&gadget->g, driver);
403 if (ret < 0) {
404 printf("gadget_start failed with %d\n", ret);
405 return ret;
406 }
407
408 ret = driver->bind(&gadget->g);
409 if (ret < 0) {
410 printf("bind failed with %d\n", ret);
411 return ret;
412 }
413
414 return 0;
415}
416
417int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
418{
Rob Herring15175e72014-04-18 08:54:30 -0500419 if (driver->disconnect)
420 driver->disconnect(&gadget->g);
421 if (driver->unbind)
422 driver->unbind(&gadget->g);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000423 return 0;
424}
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200425#endif /* CONFIG_USB_MUSB_GADGET */
Ilya Yanok06bb9202012-11-06 13:48:21 +0000426
Jagan Tekif0a6d792018-07-20 12:43:56 +0530427struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
428 void *ctl_regs)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000429{
430 struct musb **musbp;
431
432 switch (plat->mode) {
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100433#if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000434 case MUSB_HOST:
Hans de Goede59627482015-06-17 21:33:57 +0200435 musbp = &musb_host.host;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000436 break;
437#endif
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100438#if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000439 case MUSB_PERIPHERAL:
440 musbp = &gadget;
441 break;
442#endif
443 default:
Jagan Tekif0a6d792018-07-20 12:43:56 +0530444 return ERR_PTR(-EINVAL);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000445 }
446
447 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
Jagan Tekif0a6d792018-07-20 12:43:56 +0530448 if (IS_ERR(*musbp)) {
Ilya Yanok06bb9202012-11-06 13:48:21 +0000449 printf("Failed to init the controller\n");
Jagan Tekif0a6d792018-07-20 12:43:56 +0530450 return ERR_CAST(*musbp);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000451 }
452
Jagan Tekif0a6d792018-07-20 12:43:56 +0530453 return *musbp;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000454}