6 #include <linux/errno.h>
7 #include <linux/usb/ch9.h>
8 #include <linux/usb/gadget.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"
16 #include "musb_uboot.h"
18 #ifdef CONFIG_USB_MUSB_HOST
20 struct usb_host_endpoint hep;
24 #if !CONFIG_IS_ENABLED(DM_USB)
25 struct musb_host_data musb_host;
28 static void musb_host_complete_urb(struct urb *urb)
30 urb->dev->status &= ~USB_ST_NOT_PROC;
31 urb->dev->act_len = urb->actual_length;
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)
39 int epnum = usb_pipeendpoint(pipe);
40 int is_in = usb_pipein(pipe);
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);
47 urb->complete = musb_host_complete_urb;
48 urb->status = -EINPROGRESS;
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;
56 urb->ep->desc.wMaxPacketSize =
57 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
58 dev->epmaxpacketout[epnum]);
59 urb->ep->desc.bmAttributes = endpoint_type;
60 urb->ep->desc.bEndpointAddress =
61 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
62 urb->ep->desc.bInterval = interval;
65 static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
67 struct musb *host = hcd->hcd_priv;
69 unsigned long timeout;
71 ret = musb_urb_enqueue(hcd, urb, 0);
73 printf("Failed to enqueue URB to controller\n");
77 timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
82 } while (urb->status == -EINPROGRESS &&
83 get_timer(0) < timeout);
85 if (urb->status == -EINPROGRESS)
86 musb_urb_dequeue(hcd, urb, -ETIME);
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)
95 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
96 pipe, buffer, len, setup, 0);
98 /* Fix speed for non hub-attached devices */
99 if (!usb_dev_get_parent(dev))
100 dev->speed = host->host_speed;
102 return submit_urb(&host->hcd, &host->urb);
105 static int _musb_submit_bulk_msg(struct musb_host_data *host,
106 struct usb_device *dev, unsigned long pipe, void *buffer, int len)
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);
113 static int _musb_submit_int_msg(struct musb_host_data *host,
114 struct usb_device *dev, unsigned long pipe,
115 void *buffer, int len, int interval, bool nonblock)
117 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe,
118 buffer, len, NULL, interval);
119 return submit_urb(&host->hcd, &host->urb);
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)
126 struct int_queue *queue;
127 int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
129 if (queuesize != 1) {
130 printf("ERROR musb int-queues only support queuesize 1\n");
134 if (dev->int_pending & (1 << index)) {
135 printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
139 queue = malloc(sizeof(*queue));
143 construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
144 pipe, buffer, elementsize, NULL, interval);
146 ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
148 printf("Failed to enqueue URB to controller\n");
153 dev->int_pending |= 1 << index;
157 static int _musb_destroy_int_queue(struct musb_host_data *host,
158 struct usb_device *dev, struct int_queue *queue)
160 int index = usb_pipein(queue->urb.pipe) * 16 +
161 usb_pipeendpoint(queue->urb.pipe);
163 if (queue->urb.status == -EINPROGRESS)
164 musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);
166 dev->int_pending &= ~(1 << index);
171 static void *_musb_poll_int_queue(struct musb_host_data *host,
172 struct usb_device *dev, struct int_queue *queue)
174 if (queue->urb.status != -EINPROGRESS)
175 return NULL; /* URB has already completed in a prev. poll */
177 host->host->isr(0, host->host);
179 if (queue->urb.status != -EINPROGRESS)
180 return queue->urb.transfer_buffer; /* Done */
182 return NULL; /* URB still pending */
185 static int _musb_reset_root_port(struct musb_host_data *host,
186 struct usb_device *dev)
188 void *mbase = host->host->mregs;
191 power = musb_readb(mbase, MUSB_POWER);
193 musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
196 if (host->host->ops->pre_root_reset_end)
197 host->host->ops->pre_root_reset_end(host->host);
199 power = musb_readb(mbase, MUSB_POWER);
200 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
202 if (host->host->ops->post_root_reset_end)
203 host->host->ops->post_root_reset_end(host->host);
205 host->host->isr(0, host->host);
206 host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
208 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
209 USB_SPEED_FULL : USB_SPEED_LOW;
210 mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50);
215 int musb_lowlevel_init(struct musb_host_data *host)
218 /* USB spec says it may take up to 1 second for a device to connect */
219 unsigned long timeout = get_timer(0) + 1000;
223 printf("MUSB host is not registered\n");
227 ret = musb_start(host->host);
231 mbase = host->host->mregs;
233 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
235 } while (get_timer(0) < timeout);
236 if (get_timer(0) >= timeout) {
237 musb_stop(host->host);
241 _musb_reset_root_port(host, NULL);
242 host->host->is_active = 1;
243 host->hcd.hcd_priv = host->host;
248 #if !CONFIG_IS_ENABLED(DM_USB)
249 int usb_lowlevel_stop(int index)
251 if (!musb_host.host) {
252 printf("MUSB host is not registered\n");
256 musb_stop(musb_host.host);
260 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
261 void *buffer, int length)
263 return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
266 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
267 void *buffer, int length, struct devrequest *setup)
269 return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
272 int submit_int_msg(struct usb_device *dev, unsigned long pipe,
273 void *buffer, int length, int interval, bool nonblock)
275 return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length,
279 struct int_queue *create_int_queue(struct usb_device *dev,
280 unsigned long pipe, int queuesize, int elementsize,
281 void *buffer, int interval)
283 return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
287 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
289 return _musb_poll_int_queue(&musb_host, dev, queue);
292 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
294 return _musb_destroy_int_queue(&musb_host, dev, queue);
297 int usb_reset_root_port(struct usb_device *dev)
299 return _musb_reset_root_port(&musb_host, dev);
302 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
304 return musb_lowlevel_init(&musb_host);
306 #endif /* !CONFIG_IS_ENABLED(DM_USB) */
308 #if CONFIG_IS_ENABLED(DM_USB)
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)
313 struct musb_host_data *host = dev_get_priv(dev);
314 return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup);
317 static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
318 unsigned long pipe, void *buffer, int length)
320 struct musb_host_data *host = dev_get_priv(dev);
321 return _musb_submit_bulk_msg(host, udev, pipe, buffer, length);
324 static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev,
325 unsigned long pipe, void *buffer, int length,
326 int interval, bool nonblock)
328 struct musb_host_data *host = dev_get_priv(dev);
329 return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval,
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)
337 struct musb_host_data *host = dev_get_priv(dev);
338 return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize,
342 static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev,
343 struct int_queue *queue)
345 struct musb_host_data *host = dev_get_priv(dev);
346 return _musb_poll_int_queue(host, udev, queue);
349 static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
350 struct int_queue *queue)
352 struct musb_host_data *host = dev_get_priv(dev);
353 return _musb_destroy_int_queue(host, udev, queue);
356 static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev)
358 struct musb_host_data *host = dev_get_priv(dev);
359 return _musb_reset_root_port(host, udev);
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,
371 #endif /* CONFIG_IS_ENABLED(DM_USB) */
372 #endif /* CONFIG_USB_MUSB_HOST */
374 #if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
375 static struct musb *gadget;
377 int usb_gadget_handle_interrupts(int index)
380 if (!gadget || !gadget->isr)
383 return gadget->isr(0, gadget);
386 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
390 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
392 printf("bad parameter.\n");
397 printf("Controller uninitialized\n");
401 ret = musb_gadget_start(&gadget->g, driver);
403 printf("gadget_start failed with %d\n", ret);
407 ret = driver->bind(&gadget->g);
409 printf("bind failed with %d\n", ret);
416 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
418 if (driver->disconnect)
419 driver->disconnect(&gadget->g);
421 driver->unbind(&gadget->g);
424 #endif /* CONFIG_USB_MUSB_GADGET */
426 struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
431 switch (plat->mode) {
432 #if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB)
434 musbp = &musb_host.host;
437 #if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
438 case MUSB_PERIPHERAL:
443 return ERR_PTR(-EINVAL);
446 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
447 if (IS_ERR(*musbp)) {
448 printf("Failed to init the controller\n");
449 return ERR_CAST(*musbp);