4 #include <linux/errno.h>
5 #include <linux/usb/ch9.h>
6 #include <linux/usb/gadget.h>
9 #include "linux-compat.h"
10 #include "usb-compat.h"
11 #include "musb_core.h"
12 #include "musb_host.h"
13 #include "musb_gadget.h"
14 #include "musb_uboot.h"
16 #ifdef CONFIG_USB_MUSB_HOST
18 struct usb_host_endpoint hep;
22 #if !CONFIG_IS_ENABLED(DM_USB)
23 struct musb_host_data musb_host;
26 static void musb_host_complete_urb(struct urb *urb)
28 urb->dev->status &= ~USB_ST_NOT_PROC;
29 urb->dev->act_len = urb->actual_length;
32 static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep,
33 struct usb_device *dev, int endpoint_type,
34 unsigned long pipe, void *buffer, int len,
35 struct devrequest *setup, int interval)
37 int epnum = usb_pipeendpoint(pipe);
38 int is_in = usb_pipein(pipe);
40 memset(urb, 0, sizeof(struct urb));
41 memset(hep, 0, sizeof(struct usb_host_endpoint));
42 INIT_LIST_HEAD(&hep->urb_list);
43 INIT_LIST_HEAD(&urb->urb_list);
45 urb->complete = musb_host_complete_urb;
46 urb->status = -EINPROGRESS;
49 urb->transfer_buffer = buffer;
50 urb->transfer_dma = (unsigned long)buffer;
51 urb->transfer_buffer_length = len;
52 urb->setup_packet = (unsigned char *)setup;
54 urb->ep->desc.wMaxPacketSize =
55 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
56 dev->epmaxpacketout[epnum]);
57 urb->ep->desc.bmAttributes = endpoint_type;
58 urb->ep->desc.bEndpointAddress =
59 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
60 urb->ep->desc.bInterval = interval;
63 static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
65 struct musb *host = hcd->hcd_priv;
67 unsigned long timeout;
69 ret = musb_urb_enqueue(hcd, urb, 0);
71 printf("Failed to enqueue URB to controller\n");
75 timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
80 } while (urb->status == -EINPROGRESS &&
81 get_timer(0) < timeout);
83 if (urb->status == -EINPROGRESS)
84 musb_urb_dequeue(hcd, urb, -ETIME);
89 static int _musb_submit_control_msg(struct musb_host_data *host,
90 struct usb_device *dev, unsigned long pipe,
91 void *buffer, int len, struct devrequest *setup)
93 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
94 pipe, buffer, len, setup, 0);
96 /* Fix speed for non hub-attached devices */
97 if (!usb_dev_get_parent(dev))
98 dev->speed = host->host_speed;
100 return submit_urb(&host->hcd, &host->urb);
103 static int _musb_submit_bulk_msg(struct musb_host_data *host,
104 struct usb_device *dev, unsigned long pipe, void *buffer, int len)
106 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_BULK,
107 pipe, buffer, len, NULL, 0);
108 return submit_urb(&host->hcd, &host->urb);
111 static int _musb_submit_int_msg(struct musb_host_data *host,
112 struct usb_device *dev, unsigned long pipe,
113 void *buffer, int len, int interval)
115 construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_INT, pipe,
116 buffer, len, NULL, interval);
117 return submit_urb(&host->hcd, &host->urb);
120 static struct int_queue *_musb_create_int_queue(struct musb_host_data *host,
121 struct usb_device *dev, unsigned long pipe, int queuesize,
122 int elementsize, void *buffer, int interval)
124 struct int_queue *queue;
125 int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
127 if (queuesize != 1) {
128 printf("ERROR musb int-queues only support queuesize 1\n");
132 if (dev->int_pending & (1 << index)) {
133 printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
137 queue = malloc(sizeof(*queue));
141 construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
142 pipe, buffer, elementsize, NULL, interval);
144 ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
146 printf("Failed to enqueue URB to controller\n");
151 dev->int_pending |= 1 << index;
155 static int _musb_destroy_int_queue(struct musb_host_data *host,
156 struct usb_device *dev, struct int_queue *queue)
158 int index = usb_pipein(queue->urb.pipe) * 16 +
159 usb_pipeendpoint(queue->urb.pipe);
161 if (queue->urb.status == -EINPROGRESS)
162 musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);
164 dev->int_pending &= ~(1 << index);
169 static void *_musb_poll_int_queue(struct musb_host_data *host,
170 struct usb_device *dev, struct int_queue *queue)
172 if (queue->urb.status != -EINPROGRESS)
173 return NULL; /* URB has already completed in a prev. poll */
175 host->host->isr(0, host->host);
177 if (queue->urb.status != -EINPROGRESS)
178 return queue->urb.transfer_buffer; /* Done */
180 return NULL; /* URB still pending */
183 static int _musb_reset_root_port(struct musb_host_data *host,
184 struct usb_device *dev)
186 void *mbase = host->host->mregs;
189 power = musb_readb(mbase, MUSB_POWER);
191 musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
194 if (host->host->ops->pre_root_reset_end)
195 host->host->ops->pre_root_reset_end(host->host);
197 power = musb_readb(mbase, MUSB_POWER);
198 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
200 if (host->host->ops->post_root_reset_end)
201 host->host->ops->post_root_reset_end(host->host);
203 host->host->isr(0, host->host);
204 host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
206 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
207 USB_SPEED_FULL : USB_SPEED_LOW;
208 mdelay((host->host_speed == USB_SPEED_LOW) ? 200 : 50);
213 int musb_lowlevel_init(struct musb_host_data *host)
216 /* USB spec says it may take up to 1 second for a device to connect */
217 unsigned long timeout = get_timer(0) + 1000;
221 printf("MUSB host is not registered\n");
225 ret = musb_start(host->host);
229 mbase = host->host->mregs;
231 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
233 } while (get_timer(0) < timeout);
234 if (get_timer(0) >= timeout) {
235 musb_stop(host->host);
239 _musb_reset_root_port(host, NULL);
240 host->host->is_active = 1;
241 host->hcd.hcd_priv = host->host;
246 #if !CONFIG_IS_ENABLED(DM_USB)
247 int usb_lowlevel_stop(int index)
249 if (!musb_host.host) {
250 printf("MUSB host is not registered\n");
254 musb_stop(musb_host.host);
258 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
259 void *buffer, int length)
261 return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
264 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
265 void *buffer, int length, struct devrequest *setup)
267 return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
270 int submit_int_msg(struct usb_device *dev, unsigned long pipe,
271 void *buffer, int length, int interval)
273 return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length, interval);
276 struct int_queue *create_int_queue(struct usb_device *dev,
277 unsigned long pipe, int queuesize, int elementsize,
278 void *buffer, int interval)
280 return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
284 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
286 return _musb_poll_int_queue(&musb_host, dev, queue);
289 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
291 return _musb_destroy_int_queue(&musb_host, dev, queue);
294 int usb_reset_root_port(struct usb_device *dev)
296 return _musb_reset_root_port(&musb_host, dev);
299 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
301 return musb_lowlevel_init(&musb_host);
303 #endif /* !CONFIG_IS_ENABLED(DM_USB) */
305 #if CONFIG_IS_ENABLED(DM_USB)
306 static int musb_submit_control_msg(struct udevice *dev, struct usb_device *udev,
307 unsigned long pipe, void *buffer, int length,
308 struct devrequest *setup)
310 struct musb_host_data *host = dev_get_priv(dev);
311 return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup);
314 static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
315 unsigned long pipe, void *buffer, int length)
317 struct musb_host_data *host = dev_get_priv(dev);
318 return _musb_submit_bulk_msg(host, udev, pipe, buffer, length);
321 static int musb_submit_int_msg(struct udevice *dev, struct usb_device *udev,
322 unsigned long pipe, void *buffer, int length,
325 struct musb_host_data *host = dev_get_priv(dev);
326 return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval);
329 static struct int_queue *musb_create_int_queue(struct udevice *dev,
330 struct usb_device *udev, unsigned long pipe, int queuesize,
331 int elementsize, void *buffer, int interval)
333 struct musb_host_data *host = dev_get_priv(dev);
334 return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize,
338 static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev,
339 struct int_queue *queue)
341 struct musb_host_data *host = dev_get_priv(dev);
342 return _musb_poll_int_queue(host, udev, queue);
345 static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
346 struct int_queue *queue)
348 struct musb_host_data *host = dev_get_priv(dev);
349 return _musb_destroy_int_queue(host, udev, queue);
352 static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev)
354 struct musb_host_data *host = dev_get_priv(dev);
355 return _musb_reset_root_port(host, udev);
358 struct dm_usb_ops musb_usb_ops = {
359 .control = musb_submit_control_msg,
360 .bulk = musb_submit_bulk_msg,
361 .interrupt = musb_submit_int_msg,
362 .create_int_queue = musb_create_int_queue,
363 .poll_int_queue = musb_poll_int_queue,
364 .destroy_int_queue = musb_destroy_int_queue,
365 .reset_root_port = musb_reset_root_port,
367 #endif /* CONFIG_IS_ENABLED(DM_USB) */
368 #endif /* CONFIG_USB_MUSB_HOST */
370 #if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
371 static struct musb *gadget;
373 int usb_gadget_handle_interrupts(int index)
376 if (!gadget || !gadget->isr)
379 return gadget->isr(0, gadget);
382 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
386 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
388 printf("bad parameter.\n");
393 printf("Controller uninitialized\n");
397 ret = musb_gadget_start(&gadget->g, driver);
399 printf("gadget_start failed with %d\n", ret);
403 ret = driver->bind(&gadget->g);
405 printf("bind failed with %d\n", ret);
412 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
414 if (driver->disconnect)
415 driver->disconnect(&gadget->g);
417 driver->unbind(&gadget->g);
420 #endif /* CONFIG_USB_MUSB_GADGET */
422 struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
427 switch (plat->mode) {
428 #if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB)
430 musbp = &musb_host.host;
433 #if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
434 case MUSB_PERIPHERAL:
439 return ERR_PTR(-EINVAL);
442 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
443 if (IS_ERR(*musbp)) {
444 printf("Failed to init the controller\n");
445 return ERR_CAST(*musbp);