blob: 8ac2f0a78adb9587b2e2c55e2271dd561932116e [file] [log] [blame]
Ilya Yanok06bb9202012-11-06 13:48:21 +00001#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -07002#include <console.h>
Simon Glass61adc042020-07-19 10:15:55 -06003#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07004#include <malloc.h>
Heiko Schocherc3ffe502013-06-04 11:10:01 +02005#include <watchdog.h>
Simon Glassdbd79542020-05-10 11:40:11 -06006#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -07007#include <linux/err.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +09008#include <linux/errno.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +00009#include <linux/usb/ch9.h>
10#include <linux/usb/gadget.h>
11
Ilya Yanok06bb9202012-11-06 13:48:21 +000012#include <usb.h>
13#include "linux-compat.h"
14#include "usb-compat.h"
15#include "musb_core.h"
16#include "musb_host.h"
17#include "musb_gadget.h"
Hans de Goede59627482015-06-17 21:33:57 +020018#include "musb_uboot.h"
Ilya Yanok06bb9202012-11-06 13:48:21 +000019
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +020020#ifdef CONFIG_USB_MUSB_HOST
Hans de Goede83b18972015-01-11 20:34:54 +010021struct int_queue {
22 struct usb_host_endpoint hep;
23 struct urb urb;
24};
25
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010026#if !CONFIG_IS_ENABLED(DM_USB)
Hans de Goede59627482015-06-17 21:33:57 +020027struct musb_host_data musb_host;
Hans de Goedefc649612015-06-17 21:33:58 +020028#endif
Ilya Yanok06bb9202012-11-06 13:48:21 +000029
30static void musb_host_complete_urb(struct urb *urb)
31{
32 urb->dev->status &= ~USB_ST_NOT_PROC;
33 urb->dev->act_len = urb->actual_length;
34}
35
Hans de Goede39b08452015-01-11 20:34:53 +010036static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep,
37 struct usb_device *dev, int endpoint_type,
38 unsigned long pipe, void *buffer, int len,
39 struct devrequest *setup, int interval)
Ilya Yanok06bb9202012-11-06 13:48:21 +000040{
41 int epnum = usb_pipeendpoint(pipe);
42 int is_in = usb_pipein(pipe);
43
Hans de Goede39b08452015-01-11 20:34:53 +010044 memset(urb, 0, sizeof(struct urb));
45 memset(hep, 0, sizeof(struct usb_host_endpoint));
46 INIT_LIST_HEAD(&hep->urb_list);
47 INIT_LIST_HEAD(&urb->urb_list);
48 urb->ep = hep;
49 urb->complete = musb_host_complete_urb;
50 urb->status = -EINPROGRESS;
51 urb->dev = dev;
52 urb->pipe = pipe;
53 urb->transfer_buffer = buffer;
54 urb->transfer_dma = (unsigned long)buffer;
55 urb->transfer_buffer_length = len;
56 urb->setup_packet = (unsigned char *)setup;
Ilya Yanok06bb9202012-11-06 13:48:21 +000057
Hans de Goede39b08452015-01-11 20:34:53 +010058 urb->ep->desc.wMaxPacketSize =
Ilya Yanok06bb9202012-11-06 13:48:21 +000059 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
60 dev->epmaxpacketout[epnum]);
Hans de Goede39b08452015-01-11 20:34:53 +010061 urb->ep->desc.bmAttributes = endpoint_type;
62 urb->ep->desc.bEndpointAddress =
Ilya Yanok06bb9202012-11-06 13:48:21 +000063 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
Hans de Goede39b08452015-01-11 20:34:53 +010064 urb->ep->desc.bInterval = interval;
Ilya Yanok06bb9202012-11-06 13:48:21 +000065}
66
Ilya Yanok06bb9202012-11-06 13:48:21 +000067static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
68{
69 struct musb *host = hcd->hcd_priv;
70 int ret;
Hans de Goede8de4ded2015-01-11 20:34:49 +010071 unsigned long timeout;
Ilya Yanok06bb9202012-11-06 13:48:21 +000072
73 ret = musb_urb_enqueue(hcd, urb, 0);
74 if (ret < 0) {
75 printf("Failed to enqueue URB to controller\n");
76 return ret;
77 }
78
Hans de Goede8de4ded2015-01-11 20:34:49 +010079 timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
Ilya Yanok06bb9202012-11-06 13:48:21 +000080 do {
81 if (ctrlc())
82 return -EIO;
83 host->isr(0, host);
Hans de Goede24fd7772015-01-11 20:34:50 +010084 } while (urb->status == -EINPROGRESS &&
Hans de Goede8de4ded2015-01-11 20:34:49 +010085 get_timer(0) < timeout);
Ilya Yanok06bb9202012-11-06 13:48:21 +000086
Hans de Goede0e4da952015-01-11 20:34:52 +010087 if (urb->status == -EINPROGRESS)
88 musb_urb_dequeue(hcd, urb, -ETIME);
89
Ilya Yanok06bb9202012-11-06 13:48:21 +000090 return urb->status;
91}
92
Hans de Goede59627482015-06-17 21:33:57 +020093static int _musb_submit_control_msg(struct musb_host_data *host,
94 struct usb_device *dev, unsigned long pipe,
95 void *buffer, int len, struct devrequest *setup)
Ilya Yanok06bb9202012-11-06 13:48:21 +000096{
Hans de Goede59627482015-06-17 21:33:57 +020097 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
98 pipe, buffer, len, setup, 0);
Ilya Yanok06bb9202012-11-06 13:48:21 +000099
100 /* Fix speed for non hub-attached devices */
Hans de Goede4d8b8df2015-06-17 21:33:55 +0200101 if (!usb_dev_get_parent(dev))
Hans de Goede59627482015-06-17 21:33:57 +0200102 dev->speed = host->host_speed;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000103
Hans de Goede59627482015-06-17 21:33:57 +0200104 return submit_urb(&host->hcd, &host->urb);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000105}
106
Hans de Goede59627482015-06-17 21:33:57 +0200107static int _musb_submit_bulk_msg(struct musb_host_data *host,
108 struct usb_device *dev, unsigned long pipe, void *buffer, int len)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000109{
Hans de Goede59627482015-06-17 21:33:57 +0200110 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_BULK,
111 pipe, buffer, len, NULL, 0);
112 return submit_urb(&host->hcd, &host->urb);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000113}
114
Hans de Goede59627482015-06-17 21:33:57 +0200115static int _musb_submit_int_msg(struct musb_host_data *host,
116 struct usb_device *dev, unsigned long pipe,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200117 void *buffer, int len, int interval, bool nonblock)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000118{
Hans de Goede59627482015-06-17 21:33:57 +0200119 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe,
Hans de Goede39b08452015-01-11 20:34:53 +0100120 buffer, len, NULL, interval);
Hans de Goede59627482015-06-17 21:33:57 +0200121 return submit_urb(&host->hcd, &host->urb);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000122}
123
Hans de Goede59627482015-06-17 21:33:57 +0200124static struct int_queue *_musb_create_int_queue(struct musb_host_data *host,
125 struct usb_device *dev, unsigned long pipe, int queuesize,
126 int elementsize, void *buffer, int interval)
Hans de Goede83b18972015-01-11 20:34:54 +0100127{
128 struct int_queue *queue;
129 int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
130
131 if (queuesize != 1) {
132 printf("ERROR musb int-queues only support queuesize 1\n");
133 return NULL;
134 }
135
136 if (dev->int_pending & (1 << index)) {
137 printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
138 return NULL;
139 }
140
141 queue = malloc(sizeof(*queue));
142 if (!queue)
143 return NULL;
144
145 construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
146 pipe, buffer, elementsize, NULL, interval);
147
Hans de Goede59627482015-06-17 21:33:57 +0200148 ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
Hans de Goede83b18972015-01-11 20:34:54 +0100149 if (ret < 0) {
150 printf("Failed to enqueue URB to controller\n");
151 free(queue);
152 return NULL;
153 }
154
155 dev->int_pending |= 1 << index;
156 return queue;
157}
158
Hans de Goede59627482015-06-17 21:33:57 +0200159static int _musb_destroy_int_queue(struct musb_host_data *host,
160 struct usb_device *dev, struct int_queue *queue)
Hans de Goede83b18972015-01-11 20:34:54 +0100161{
162 int index = usb_pipein(queue->urb.pipe) * 16 +
163 usb_pipeendpoint(queue->urb.pipe);
164
165 if (queue->urb.status == -EINPROGRESS)
Hans de Goede59627482015-06-17 21:33:57 +0200166 musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);
Hans de Goede83b18972015-01-11 20:34:54 +0100167
168 dev->int_pending &= ~(1 << index);
169 free(queue);
170 return 0;
171}
172
Hans de Goede59627482015-06-17 21:33:57 +0200173static void *_musb_poll_int_queue(struct musb_host_data *host,
174 struct usb_device *dev, struct int_queue *queue)
Hans de Goede83b18972015-01-11 20:34:54 +0100175{
176 if (queue->urb.status != -EINPROGRESS)
177 return NULL; /* URB has already completed in a prev. poll */
178
Hans de Goede59627482015-06-17 21:33:57 +0200179 host->host->isr(0, host->host);
Hans de Goede83b18972015-01-11 20:34:54 +0100180
181 if (queue->urb.status != -EINPROGRESS)
182 return queue->urb.transfer_buffer; /* Done */
183
184 return NULL; /* URB still pending */
185}
186
Hans de Goede59627482015-06-17 21:33:57 +0200187static int _musb_reset_root_port(struct musb_host_data *host,
188 struct usb_device *dev)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000189{
Hans de Goede59627482015-06-17 21:33:57 +0200190 void *mbase = host->host->mregs;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000191 u8 power;
Hans de Goede498ab952015-01-11 20:34:51 +0100192
193 power = musb_readb(mbase, MUSB_POWER);
194 power &= 0xf0;
195 musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
196 mdelay(50);
Jagan Teki37671e12018-05-07 13:03:37 +0530197
198 if (host->host->ops->pre_root_reset_end)
199 host->host->ops->pre_root_reset_end(host->host);
200
Hans de Goede498ab952015-01-11 20:34:51 +0100201 power = musb_readb(mbase, MUSB_POWER);
202 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
Jagan Teki37671e12018-05-07 13:03:37 +0530203
204 if (host->host->ops->post_root_reset_end)
205 host->host->ops->post_root_reset_end(host->host);
206
Hans de Goede59627482015-06-17 21:33:57 +0200207 host->host->isr(0, host->host);
208 host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
Hans de Goede498ab952015-01-11 20:34:51 +0100209 USB_SPEED_HIGH :
210 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
211 USB_SPEED_FULL : USB_SPEED_LOW;
Hans de Goede59627482015-06-17 21:33:57 +0200212 mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600213
214 return 0;
Hans de Goede498ab952015-01-11 20:34:51 +0100215}
216
Hans de Goede59627482015-06-17 21:33:57 +0200217int musb_lowlevel_init(struct musb_host_data *host)
Hans de Goede498ab952015-01-11 20:34:51 +0100218{
Ilya Yanok06bb9202012-11-06 13:48:21 +0000219 void *mbase;
Hans de Goede8de4ded2015-01-11 20:34:49 +0100220 /* USB spec says it may take up to 1 second for a device to connect */
221 unsigned long timeout = get_timer(0) + 1000;
Hans de Goede81c49982015-06-17 21:33:54 +0200222 int ret;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000223
Hans de Goede59627482015-06-17 21:33:57 +0200224 if (!host->host) {
Ilya Yanok06bb9202012-11-06 13:48:21 +0000225 printf("MUSB host is not registered\n");
226 return -ENODEV;
227 }
228
Hans de Goede59627482015-06-17 21:33:57 +0200229 ret = musb_start(host->host);
Hans de Goede81c49982015-06-17 21:33:54 +0200230 if (ret)
231 return ret;
232
Hans de Goede59627482015-06-17 21:33:57 +0200233 mbase = host->host->mregs;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000234 do {
235 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
236 break;
Hans de Goede8de4ded2015-01-11 20:34:49 +0100237 } while (get_timer(0) < timeout);
Hans de Goede2ec5be62016-04-02 20:46:10 +0200238 if (get_timer(0) >= timeout) {
239 musb_stop(host->host);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000240 return -ENODEV;
Hans de Goede2ec5be62016-04-02 20:46:10 +0200241 }
Ilya Yanok06bb9202012-11-06 13:48:21 +0000242
Hans de Goede59627482015-06-17 21:33:57 +0200243 _musb_reset_root_port(host, NULL);
244 host->host->is_active = 1;
245 host->hcd.hcd_priv = host->host;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000246
247 return 0;
248}
249
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100250#if !CONFIG_IS_ENABLED(DM_USB)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000251int usb_lowlevel_stop(int index)
252{
Hans de Goede59627482015-06-17 21:33:57 +0200253 if (!musb_host.host) {
Ilya Yanok06bb9202012-11-06 13:48:21 +0000254 printf("MUSB host is not registered\n");
255 return -ENODEV;
256 }
257
Hans de Goede59627482015-06-17 21:33:57 +0200258 musb_stop(musb_host.host);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000259 return 0;
260}
Hans de Goede52666e22015-06-17 21:33:56 +0200261
262int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
263 void *buffer, int length)
264{
Hans de Goede59627482015-06-17 21:33:57 +0200265 return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
Hans de Goede52666e22015-06-17 21:33:56 +0200266}
267
268int submit_control_msg(struct usb_device *dev, unsigned long pipe,
269 void *buffer, int length, struct devrequest *setup)
270{
Hans de Goede59627482015-06-17 21:33:57 +0200271 return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
Hans de Goede52666e22015-06-17 21:33:56 +0200272}
273
274int submit_int_msg(struct usb_device *dev, unsigned long pipe,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200275 void *buffer, int length, int interval, bool nonblock)
Hans de Goede52666e22015-06-17 21:33:56 +0200276{
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200277 return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length,
278 interval, nonblock);
Hans de Goede52666e22015-06-17 21:33:56 +0200279}
280
281struct int_queue *create_int_queue(struct usb_device *dev,
282 unsigned long pipe, int queuesize, int elementsize,
283 void *buffer, int interval)
284{
Hans de Goede59627482015-06-17 21:33:57 +0200285 return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
Hans de Goede52666e22015-06-17 21:33:56 +0200286 buffer, interval);
287}
288
289void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
290{
Hans de Goede59627482015-06-17 21:33:57 +0200291 return _musb_poll_int_queue(&musb_host, dev, queue);
Hans de Goede52666e22015-06-17 21:33:56 +0200292}
293
294int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
295{
Hans de Goede59627482015-06-17 21:33:57 +0200296 return _musb_destroy_int_queue(&musb_host, dev, queue);
Hans de Goede52666e22015-06-17 21:33:56 +0200297}
298
299int usb_reset_root_port(struct usb_device *dev)
300{
Hans de Goede59627482015-06-17 21:33:57 +0200301 return _musb_reset_root_port(&musb_host, dev);
Hans de Goede52666e22015-06-17 21:33:56 +0200302}
303
304int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
305{
Hans de Goede59627482015-06-17 21:33:57 +0200306 return musb_lowlevel_init(&musb_host);
Hans de Goede52666e22015-06-17 21:33:56 +0200307}
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100308#endif /* !CONFIG_IS_ENABLED(DM_USB) */
Hans de Goedefc649612015-06-17 21:33:58 +0200309
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100310#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goedefc649612015-06-17 21:33:58 +0200311static int musb_submit_control_msg(struct udevice *dev, struct usb_device *udev,
312 unsigned long pipe, void *buffer, int length,
313 struct devrequest *setup)
314{
315 struct musb_host_data *host = dev_get_priv(dev);
316 return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup);
317}
318
319static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
320 unsigned long pipe, void *buffer, int length)
321{
322 struct musb_host_data *host = dev_get_priv(dev);
323 return _musb_submit_bulk_msg(host, udev, pipe, buffer, length);
324}
325
326static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev,
327 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200328 int interval, bool nonblock)
Hans de Goedefc649612015-06-17 21:33:58 +0200329{
330 struct musb_host_data *host = dev_get_priv(dev);
Michal Suchanek1c95b9f2019-08-18 10:55:27 +0200331 return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval,
332 nonblock);
Hans de Goedefc649612015-06-17 21:33:58 +0200333}
334
335static struct int_queue *musb_create_int_queue(struct udevice *dev,
336 struct usb_device *udev, unsigned long pipe, int queuesize,
337 int elementsize, void *buffer, int interval)
338{
339 struct musb_host_data *host = dev_get_priv(dev);
340 return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize,
341 buffer, interval);
342}
343
344static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev,
345 struct int_queue *queue)
346{
347 struct musb_host_data *host = dev_get_priv(dev);
348 return _musb_poll_int_queue(host, udev, queue);
349}
350
351static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
352 struct int_queue *queue)
353{
354 struct musb_host_data *host = dev_get_priv(dev);
355 return _musb_destroy_int_queue(host, udev, queue);
356}
357
358static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev)
359{
360 struct musb_host_data *host = dev_get_priv(dev);
361 return _musb_reset_root_port(host, udev);
362}
363
364struct dm_usb_ops musb_usb_ops = {
365 .control = musb_submit_control_msg,
366 .bulk = musb_submit_bulk_msg,
367 .interrupt = musb_submit_int_msg,
368 .create_int_queue = musb_create_int_queue,
369 .poll_int_queue = musb_poll_int_queue,
370 .destroy_int_queue = musb_destroy_int_queue,
371 .reset_root_port = musb_reset_root_port,
372};
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100373#endif /* CONFIG_IS_ENABLED(DM_USB) */
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200374#endif /* CONFIG_USB_MUSB_HOST */
Ilya Yanok06bb9202012-11-06 13:48:21 +0000375
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100376#if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000377static struct musb *gadget;
378
Kishon Vijay Abraham I4763e162015-02-23 18:40:23 +0530379int usb_gadget_handle_interrupts(int index)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000380{
Heiko Schocherc3ffe502013-06-04 11:10:01 +0200381 WATCHDOG_RESET();
Ilya Yanok06bb9202012-11-06 13:48:21 +0000382 if (!gadget || !gadget->isr)
383 return -EINVAL;
384
385 return gadget->isr(0, gadget);
386}
387
388int usb_gadget_register_driver(struct usb_gadget_driver *driver)
389{
390 int ret;
391
Bin Liu28f2f352013-03-21 05:27:49 +0000392 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
Ilya Yanok06bb9202012-11-06 13:48:21 +0000393 !driver->setup) {
394 printf("bad parameter.\n");
395 return -EINVAL;
396 }
397
398 if (!gadget) {
399 printf("Controller uninitialized\n");
400 return -ENXIO;
401 }
402
403 ret = musb_gadget_start(&gadget->g, driver);
404 if (ret < 0) {
405 printf("gadget_start failed with %d\n", ret);
406 return ret;
407 }
408
409 ret = driver->bind(&gadget->g);
410 if (ret < 0) {
411 printf("bind failed with %d\n", ret);
412 return ret;
413 }
414
415 return 0;
416}
417
418int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
419{
Rob Herring15175e72014-04-18 08:54:30 -0500420 if (driver->disconnect)
421 driver->disconnect(&gadget->g);
422 if (driver->unbind)
423 driver->unbind(&gadget->g);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000424 return 0;
425}
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200426#endif /* CONFIG_USB_MUSB_GADGET */
Ilya Yanok06bb9202012-11-06 13:48:21 +0000427
Jagan Tekif0a6d792018-07-20 12:43:56 +0530428struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
429 void *ctl_regs)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000430{
431 struct musb **musbp;
432
433 switch (plat->mode) {
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100434#if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000435 case MUSB_HOST:
Hans de Goede59627482015-06-17 21:33:57 +0200436 musbp = &musb_host.host;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000437 break;
438#endif
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +0100439#if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000440 case MUSB_PERIPHERAL:
441 musbp = &gadget;
442 break;
443#endif
444 default:
Jagan Tekif0a6d792018-07-20 12:43:56 +0530445 return ERR_PTR(-EINVAL);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000446 }
447
448 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
Jagan Tekif0a6d792018-07-20 12:43:56 +0530449 if (IS_ERR(*musbp)) {
Ilya Yanok06bb9202012-11-06 13:48:21 +0000450 printf("Failed to init the controller\n");
Jagan Tekif0a6d792018-07-20 12:43:56 +0530451 return ERR_CAST(*musbp);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000452 }
453
Jagan Tekif0a6d792018-07-20 12:43:56 +0530454 return *musbp;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000455}
Simon Glass61adc042020-07-19 10:15:55 -0600456
457#if CONFIG_IS_ENABLED(DM_USB)
458struct usb_device *usb_dev_get_parent(struct usb_device *udev)
459{
460 struct udevice *parent = udev->dev->parent;
461
462 /*
463 * When called from usb-uclass.c: usb_scan_device() udev->dev points
464 * to the parent udevice, not the actual udevice belonging to the
465 * udev as the device is not instantiated yet.
466 *
467 * If dev is an usb-bus, then we are called from usb_scan_device() for
468 * an usb-device plugged directly into the root port, return NULL.
469 */
470 if (device_get_uclass_id(udev->dev) == UCLASS_USB)
471 return NULL;
472
473 /*
474 * If these 2 are not the same we are being called from
475 * usb_scan_device() and udev itself is the parent.
476 */
477 if (dev_get_parent_priv(udev->dev) != udev)
478 return udev;
479
480 /* We are being called normally, use the parent pointer */
481 if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
482 return dev_get_parent_priv(parent);
483
484 return NULL;
485}
486#else
487struct usb_device *usb_dev_get_parent(struct usb_device *udev)
488{
489 return udev->parent;
490}
491#endif