blob: 6e58ddf02cc34b67b4550b9f752d5a00c2d0615c [file] [log] [blame]
Ilya Yanok06bb9202012-11-06 13:48:21 +00001#include <common.h>
Heiko Schocherc3ffe502013-06-04 11:10:01 +02002#include <watchdog.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +00003#include <asm/errno.h>
4#include <linux/usb/ch9.h>
5#include <linux/usb/gadget.h>
6
Ilya Yanok06bb9202012-11-06 13:48:21 +00007#include <usb.h>
8#include "linux-compat.h"
9#include "usb-compat.h"
10#include "musb_core.h"
11#include "musb_host.h"
12#include "musb_gadget.h"
13
14#ifdef CONFIG_MUSB_HOST
Hans de Goede83b18972015-01-11 20:34:54 +010015struct int_queue {
16 struct usb_host_endpoint hep;
17 struct urb urb;
18};
19
Ilya Yanok06bb9202012-11-06 13:48:21 +000020static struct musb *host;
21static struct usb_hcd hcd;
22static enum usb_device_speed host_speed;
23
24static void musb_host_complete_urb(struct urb *urb)
25{
26 urb->dev->status &= ~USB_ST_NOT_PROC;
27 urb->dev->act_len = urb->actual_length;
28}
29
30static struct usb_host_endpoint hep;
31static struct urb urb;
32
Hans de Goede39b08452015-01-11 20:34:53 +010033static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep,
34 struct usb_device *dev, int endpoint_type,
35 unsigned long pipe, void *buffer, int len,
36 struct devrequest *setup, int interval)
Ilya Yanok06bb9202012-11-06 13:48:21 +000037{
38 int epnum = usb_pipeendpoint(pipe);
39 int is_in = usb_pipein(pipe);
40
Hans de Goede39b08452015-01-11 20:34:53 +010041 memset(urb, 0, sizeof(struct urb));
42 memset(hep, 0, sizeof(struct usb_host_endpoint));
43 INIT_LIST_HEAD(&hep->urb_list);
44 INIT_LIST_HEAD(&urb->urb_list);
45 urb->ep = hep;
46 urb->complete = musb_host_complete_urb;
47 urb->status = -EINPROGRESS;
48 urb->dev = dev;
49 urb->pipe = pipe;
50 urb->transfer_buffer = buffer;
51 urb->transfer_dma = (unsigned long)buffer;
52 urb->transfer_buffer_length = len;
53 urb->setup_packet = (unsigned char *)setup;
Ilya Yanok06bb9202012-11-06 13:48:21 +000054
Hans de Goede39b08452015-01-11 20:34:53 +010055 urb->ep->desc.wMaxPacketSize =
Ilya Yanok06bb9202012-11-06 13:48:21 +000056 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
57 dev->epmaxpacketout[epnum]);
Hans de Goede39b08452015-01-11 20:34:53 +010058 urb->ep->desc.bmAttributes = endpoint_type;
59 urb->ep->desc.bEndpointAddress =
Ilya Yanok06bb9202012-11-06 13:48:21 +000060 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
Hans de Goede39b08452015-01-11 20:34:53 +010061 urb->ep->desc.bInterval = interval;
Ilya Yanok06bb9202012-11-06 13:48:21 +000062}
63
Ilya Yanok06bb9202012-11-06 13:48:21 +000064static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
65{
66 struct musb *host = hcd->hcd_priv;
67 int ret;
Hans de Goede8de4ded2015-01-11 20:34:49 +010068 unsigned long timeout;
Ilya Yanok06bb9202012-11-06 13:48:21 +000069
70 ret = musb_urb_enqueue(hcd, urb, 0);
71 if (ret < 0) {
72 printf("Failed to enqueue URB to controller\n");
73 return ret;
74 }
75
Hans de Goede8de4ded2015-01-11 20:34:49 +010076 timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
Ilya Yanok06bb9202012-11-06 13:48:21 +000077 do {
78 if (ctrlc())
79 return -EIO;
80 host->isr(0, host);
Hans de Goede24fd7772015-01-11 20:34:50 +010081 } while (urb->status == -EINPROGRESS &&
Hans de Goede8de4ded2015-01-11 20:34:49 +010082 get_timer(0) < timeout);
Ilya Yanok06bb9202012-11-06 13:48:21 +000083
Hans de Goede0e4da952015-01-11 20:34:52 +010084 if (urb->status == -EINPROGRESS)
85 musb_urb_dequeue(hcd, urb, -ETIME);
86
Ilya Yanok06bb9202012-11-06 13:48:21 +000087 return urb->status;
88}
89
90int submit_control_msg(struct usb_device *dev, unsigned long pipe,
91 void *buffer, int len, struct devrequest *setup)
92{
Hans de Goede39b08452015-01-11 20:34:53 +010093 construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_CONTROL, pipe,
94 buffer, len, setup, 0);
Ilya Yanok06bb9202012-11-06 13:48:21 +000095
96 /* Fix speed for non hub-attached devices */
97 if (!dev->parent)
98 dev->speed = host_speed;
99
Hans de Goede39b08452015-01-11 20:34:53 +0100100 return submit_urb(&hcd, &urb);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000101}
102
103
104int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
105 void *buffer, int len)
106{
Hans de Goede39b08452015-01-11 20:34:53 +0100107 construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_BULK, pipe,
108 buffer, len, NULL, 0);
109 return submit_urb(&hcd, &urb);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000110}
111
112int submit_int_msg(struct usb_device *dev, unsigned long pipe,
113 void *buffer, int len, int interval)
114{
Hans de Goede39b08452015-01-11 20:34:53 +0100115 construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_INT, pipe,
116 buffer, len, NULL, interval);
117 return submit_urb(&hcd, &urb);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000118}
119
Hans de Goede83b18972015-01-11 20:34:54 +0100120struct int_queue *create_int_queue(struct usb_device *dev, unsigned long pipe,
121 int queuesize, int elementsize, void *buffer, int interval)
122{
123 struct int_queue *queue;
124 int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
125
126 if (queuesize != 1) {
127 printf("ERROR musb int-queues only support queuesize 1\n");
128 return NULL;
129 }
130
131 if (dev->int_pending & (1 << index)) {
132 printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
133 return NULL;
134 }
135
136 queue = malloc(sizeof(*queue));
137 if (!queue)
138 return NULL;
139
140 construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
141 pipe, buffer, elementsize, NULL, interval);
142
143 ret = musb_urb_enqueue(&hcd, &queue->urb, 0);
144 if (ret < 0) {
145 printf("Failed to enqueue URB to controller\n");
146 free(queue);
147 return NULL;
148 }
149
150 dev->int_pending |= 1 << index;
151 return queue;
152}
153
154int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
155{
156 int index = usb_pipein(queue->urb.pipe) * 16 +
157 usb_pipeendpoint(queue->urb.pipe);
158
159 if (queue->urb.status == -EINPROGRESS)
160 musb_urb_dequeue(&hcd, &queue->urb, -ETIME);
161
162 dev->int_pending &= ~(1 << index);
163 free(queue);
164 return 0;
165}
166
167void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
168{
169 if (queue->urb.status != -EINPROGRESS)
170 return NULL; /* URB has already completed in a prev. poll */
171
172 host->isr(0, host);
173
174 if (queue->urb.status != -EINPROGRESS)
175 return queue->urb.transfer_buffer; /* Done */
176
177 return NULL; /* URB still pending */
178}
179
Hans de Goede498ab952015-01-11 20:34:51 +0100180void usb_reset_root_port(void)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000181{
Hans de Goede498ab952015-01-11 20:34:51 +0100182 void *mbase = host->mregs;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000183 u8 power;
Hans de Goede498ab952015-01-11 20:34:51 +0100184
185 power = musb_readb(mbase, MUSB_POWER);
186 power &= 0xf0;
187 musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
188 mdelay(50);
189 power = musb_readb(mbase, MUSB_POWER);
190 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
191 host->isr(0, host);
192 host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
193 USB_SPEED_HIGH :
194 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
195 USB_SPEED_FULL : USB_SPEED_LOW;
196 mdelay((host_speed == USB_SPEED_LOW) ? 200 : 50);
197}
198
199int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
200{
Ilya Yanok06bb9202012-11-06 13:48:21 +0000201 void *mbase;
Hans de Goede8de4ded2015-01-11 20:34:49 +0100202 /* USB spec says it may take up to 1 second for a device to connect */
203 unsigned long timeout = get_timer(0) + 1000;
Ilya Yanok06bb9202012-11-06 13:48:21 +0000204
205 if (!host) {
206 printf("MUSB host is not registered\n");
207 return -ENODEV;
208 }
209
210 musb_start(host);
211 mbase = host->mregs;
212 do {
213 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
214 break;
Hans de Goede8de4ded2015-01-11 20:34:49 +0100215 } while (get_timer(0) < timeout);
216 if (get_timer(0) >= timeout)
Ilya Yanok06bb9202012-11-06 13:48:21 +0000217 return -ENODEV;
218
Hans de Goede498ab952015-01-11 20:34:51 +0100219 usb_reset_root_port();
Ilya Yanok06bb9202012-11-06 13:48:21 +0000220 host->is_active = 1;
221 hcd.hcd_priv = host;
222
223 return 0;
224}
225
226int usb_lowlevel_stop(int index)
227{
228 if (!host) {
229 printf("MUSB host is not registered\n");
230 return -ENODEV;
231 }
232
233 musb_stop(host);
234 return 0;
235}
236#endif /* CONFIG_MUSB_HOST */
237
238#ifdef CONFIG_MUSB_GADGET
239static struct musb *gadget;
240
241int usb_gadget_handle_interrupts(void)
242{
Heiko Schocherc3ffe502013-06-04 11:10:01 +0200243 WATCHDOG_RESET();
Ilya Yanok06bb9202012-11-06 13:48:21 +0000244 if (!gadget || !gadget->isr)
245 return -EINVAL;
246
247 return gadget->isr(0, gadget);
248}
249
250int usb_gadget_register_driver(struct usb_gadget_driver *driver)
251{
252 int ret;
253
Bin Liu28f2f352013-03-21 05:27:49 +0000254 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
Ilya Yanok06bb9202012-11-06 13:48:21 +0000255 !driver->setup) {
256 printf("bad parameter.\n");
257 return -EINVAL;
258 }
259
260 if (!gadget) {
261 printf("Controller uninitialized\n");
262 return -ENXIO;
263 }
264
265 ret = musb_gadget_start(&gadget->g, driver);
266 if (ret < 0) {
267 printf("gadget_start failed with %d\n", ret);
268 return ret;
269 }
270
271 ret = driver->bind(&gadget->g);
272 if (ret < 0) {
273 printf("bind failed with %d\n", ret);
274 return ret;
275 }
276
277 return 0;
278}
279
280int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
281{
Rob Herring15175e72014-04-18 08:54:30 -0500282 if (driver->disconnect)
283 driver->disconnect(&gadget->g);
284 if (driver->unbind)
285 driver->unbind(&gadget->g);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000286 return 0;
287}
288#endif /* CONFIG_MUSB_GADGET */
289
290int musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
291 void *ctl_regs)
292{
293 struct musb **musbp;
294
295 switch (plat->mode) {
296#ifdef CONFIG_MUSB_HOST
297 case MUSB_HOST:
298 musbp = &host;
299 break;
300#endif
301#ifdef CONFIG_MUSB_GADGET
302 case MUSB_PERIPHERAL:
303 musbp = &gadget;
304 break;
305#endif
306 default:
307 return -EINVAL;
308 }
309
310 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
311 if (!musbp) {
312 printf("Failed to init the controller\n");
313 return -EIO;
314 }
315
316 return 0;
317}