72f14b93438780411e7d2cd334443db9f8b51925
[oweals/u-boot.git] / drivers / usb / musb-new / musb_uboot.c
1 #include <common.h>
2 #include <console.h>
3 #include <malloc.h>
4 #include <watchdog.h>
5 #include <linux/err.h>
6 #include <linux/errno.h>
7 #include <linux/usb/ch9.h>
8 #include <linux/usb/gadget.h>
9
10 #include <usb.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"
17
18 #ifdef CONFIG_USB_MUSB_HOST
19 struct int_queue {
20         struct usb_host_endpoint hep;
21         struct urb urb;
22 };
23
24 #if !CONFIG_IS_ENABLED(DM_USB)
25 struct musb_host_data musb_host;
26 #endif
27
28 static void musb_host_complete_urb(struct urb *urb)
29 {
30         urb->dev->status &= ~USB_ST_NOT_PROC;
31         urb->dev->act_len = urb->actual_length;
32 }
33
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)
38 {
39         int epnum = usb_pipeendpoint(pipe);
40         int is_in = usb_pipein(pipe);
41
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);
46         urb->ep = hep;
47         urb->complete = musb_host_complete_urb;
48         urb->status = -EINPROGRESS;
49         urb->dev = dev;
50         urb->pipe = pipe;
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;
55
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;
63 }
64
65 static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
66 {
67         struct musb *host = hcd->hcd_priv;
68         int ret;
69         unsigned long timeout;
70
71         ret = musb_urb_enqueue(hcd, urb, 0);
72         if (ret < 0) {
73                 printf("Failed to enqueue URB to controller\n");
74                 return ret;
75         }
76
77         timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
78         do {
79                 if (ctrlc())
80                         return -EIO;
81                 host->isr(0, host);
82         } while (urb->status == -EINPROGRESS &&
83                  get_timer(0) < timeout);
84
85         if (urb->status == -EINPROGRESS)
86                 musb_urb_dequeue(hcd, urb, -ETIME);
87
88         return urb->status;
89 }
90
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)
94 {
95         construct_urb(&host->urb, &host->hep, dev, USB_ENDPOINT_XFER_CONTROL,
96                       pipe, buffer, len, setup, 0);
97
98         /* Fix speed for non hub-attached devices */
99         if (!usb_dev_get_parent(dev))
100                 dev->speed = host->host_speed;
101
102         return submit_urb(&host->hcd, &host->urb);
103 }
104
105 static int _musb_submit_bulk_msg(struct musb_host_data *host,
106         struct usb_device *dev, unsigned long pipe, void *buffer, int len)
107 {
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);
111 }
112
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)
116 {
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);
120 }
121
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)
125 {
126         struct int_queue *queue;
127         int ret, index = usb_pipein(pipe) * 16 + usb_pipeendpoint(pipe);
128
129         if (queuesize != 1) {
130                 printf("ERROR musb int-queues only support queuesize 1\n");
131                 return NULL;
132         }
133
134         if (dev->int_pending & (1 << index)) {
135                 printf("ERROR int-urb is already pending on pipe %lx\n", pipe);
136                 return NULL;
137         }
138
139         queue = malloc(sizeof(*queue));
140         if (!queue)
141                 return NULL;
142
143         construct_urb(&queue->urb, &queue->hep, dev, USB_ENDPOINT_XFER_INT,
144                       pipe, buffer, elementsize, NULL, interval);
145
146         ret = musb_urb_enqueue(&host->hcd, &queue->urb, 0);
147         if (ret < 0) {
148                 printf("Failed to enqueue URB to controller\n");
149                 free(queue);
150                 return NULL;
151         }
152
153         dev->int_pending |= 1 << index;
154         return queue;
155 }
156
157 static int _musb_destroy_int_queue(struct musb_host_data *host,
158         struct usb_device *dev, struct int_queue *queue)
159 {
160         int index = usb_pipein(queue->urb.pipe) * 16 + 
161                     usb_pipeendpoint(queue->urb.pipe);
162
163         if (queue->urb.status == -EINPROGRESS)
164                 musb_urb_dequeue(&host->hcd, &queue->urb, -ETIME);
165
166         dev->int_pending &= ~(1 << index);
167         free(queue);
168         return 0;
169 }
170
171 static void *_musb_poll_int_queue(struct musb_host_data *host,
172         struct usb_device *dev, struct int_queue *queue)
173 {
174         if (queue->urb.status != -EINPROGRESS)
175                 return NULL; /* URB has already completed in a prev. poll */
176
177         host->host->isr(0, host->host);
178
179         if (queue->urb.status != -EINPROGRESS)
180                 return queue->urb.transfer_buffer; /* Done */
181
182         return NULL; /* URB still pending */
183 }
184
185 static int _musb_reset_root_port(struct musb_host_data *host,
186         struct usb_device *dev)
187 {
188         void *mbase = host->host->mregs;
189         u8 power;
190
191         power = musb_readb(mbase, MUSB_POWER);
192         power &= 0xf0;
193         musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
194         mdelay(50);
195
196         if (host->host->ops->pre_root_reset_end)
197                 host->host->ops->pre_root_reset_end(host->host);
198
199         power = musb_readb(mbase, MUSB_POWER);
200         musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
201
202         if (host->host->ops->post_root_reset_end)
203                 host->host->ops->post_root_reset_end(host->host);
204
205         host->host->isr(0, host->host);
206         host->host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
207                         USB_SPEED_HIGH :
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);
211
212         return 0;
213 }
214
215 int musb_lowlevel_init(struct musb_host_data *host)
216 {
217         void *mbase;
218         /* USB spec says it may take up to 1 second for a device to connect */
219         unsigned long timeout = get_timer(0) + 1000;
220         int ret;
221
222         if (!host->host) {
223                 printf("MUSB host is not registered\n");
224                 return -ENODEV;
225         }
226
227         ret = musb_start(host->host);
228         if (ret)
229                 return ret;
230
231         mbase = host->host->mregs;
232         do {
233                 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
234                         break;
235         } while (get_timer(0) < timeout);
236         if (get_timer(0) >= timeout) {
237                 musb_stop(host->host);
238                 return -ENODEV;
239         }
240
241         _musb_reset_root_port(host, NULL);
242         host->host->is_active = 1;
243         host->hcd.hcd_priv = host->host;
244
245         return 0;
246 }
247
248 #if !CONFIG_IS_ENABLED(DM_USB)
249 int usb_lowlevel_stop(int index)
250 {
251         if (!musb_host.host) {
252                 printf("MUSB host is not registered\n");
253                 return -ENODEV;
254         }
255
256         musb_stop(musb_host.host);
257         return 0;
258 }
259
260 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
261                             void *buffer, int length)
262 {
263         return _musb_submit_bulk_msg(&musb_host, dev, pipe, buffer, length);
264 }
265
266 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
267                        void *buffer, int length, struct devrequest *setup)
268 {
269         return _musb_submit_control_msg(&musb_host, dev, pipe, buffer, length, setup);
270 }
271
272 int submit_int_msg(struct usb_device *dev, unsigned long pipe,
273                    void *buffer, int length, int interval, bool nonblock)
274 {
275         return _musb_submit_int_msg(&musb_host, dev, pipe, buffer, length,
276                                     interval, nonblock);
277 }
278
279 struct int_queue *create_int_queue(struct usb_device *dev,
280                 unsigned long pipe, int queuesize, int elementsize,
281                 void *buffer, int interval)
282 {
283         return _musb_create_int_queue(&musb_host, dev, pipe, queuesize, elementsize,
284                                       buffer, interval);
285 }
286
287 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
288 {
289         return _musb_poll_int_queue(&musb_host, dev, queue);
290 }
291
292 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
293 {
294         return _musb_destroy_int_queue(&musb_host, dev, queue);
295 }
296
297 int usb_reset_root_port(struct usb_device *dev)
298 {
299         return _musb_reset_root_port(&musb_host, dev);
300 }
301
302 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
303 {
304         return musb_lowlevel_init(&musb_host);
305 }
306 #endif /* !CONFIG_IS_ENABLED(DM_USB) */
307
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)
312 {
313         struct musb_host_data *host = dev_get_priv(dev);
314         return _musb_submit_control_msg(host, udev, pipe, buffer, length, setup);
315 }
316
317 static int musb_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
318                                 unsigned long pipe, void *buffer, int length)
319 {
320         struct musb_host_data *host = dev_get_priv(dev);
321         return _musb_submit_bulk_msg(host, udev, pipe, buffer, length);
322 }
323
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)
327 {
328         struct musb_host_data *host = dev_get_priv(dev);
329         return _musb_submit_int_msg(host, udev, pipe, buffer, length, interval,
330                                     nonblock);
331 }
332
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)
336 {
337         struct musb_host_data *host = dev_get_priv(dev);
338         return _musb_create_int_queue(host, udev, pipe, queuesize, elementsize,
339                                       buffer, interval);
340 }
341
342 static void *musb_poll_int_queue(struct udevice *dev, struct usb_device *udev,
343                                  struct int_queue *queue)
344 {
345         struct musb_host_data *host = dev_get_priv(dev);
346         return _musb_poll_int_queue(host, udev, queue);
347 }
348
349 static int musb_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
350                                   struct int_queue *queue)
351 {
352         struct musb_host_data *host = dev_get_priv(dev);
353         return _musb_destroy_int_queue(host, udev, queue);
354 }
355
356 static int musb_reset_root_port(struct udevice *dev, struct usb_device *udev)
357 {
358         struct musb_host_data *host = dev_get_priv(dev);
359         return _musb_reset_root_port(host, udev);
360 }
361
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,
370 };
371 #endif /* CONFIG_IS_ENABLED(DM_USB) */
372 #endif /* CONFIG_USB_MUSB_HOST */
373
374 #if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
375 static struct musb *gadget;
376
377 int usb_gadget_handle_interrupts(int index)
378 {
379         WATCHDOG_RESET();
380         if (!gadget || !gadget->isr)
381                 return -EINVAL;
382
383         return gadget->isr(0, gadget);
384 }
385
386 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
387 {
388         int ret;
389
390         if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
391             !driver->setup) {
392                 printf("bad parameter.\n");
393                 return -EINVAL;
394         }
395
396         if (!gadget) {
397                 printf("Controller uninitialized\n");
398                 return -ENXIO;
399         }
400
401         ret = musb_gadget_start(&gadget->g, driver);
402         if (ret < 0) {
403                 printf("gadget_start failed with %d\n", ret);
404                 return ret;
405         }
406
407         ret = driver->bind(&gadget->g);
408         if (ret < 0) {
409                 printf("bind failed with %d\n", ret);
410                 return ret;
411         }
412
413         return 0;
414 }
415
416 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
417 {
418         if (driver->disconnect)
419                 driver->disconnect(&gadget->g);
420         if (driver->unbind)
421                 driver->unbind(&gadget->g);
422         return 0;
423 }
424 #endif /* CONFIG_USB_MUSB_GADGET */
425
426 struct musb *musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
427                            void *ctl_regs)
428 {
429         struct musb **musbp;
430
431         switch (plat->mode) {
432 #if defined(CONFIG_USB_MUSB_HOST) && !CONFIG_IS_ENABLED(DM_USB)
433         case MUSB_HOST:
434                 musbp = &musb_host.host;
435                 break;
436 #endif
437 #if defined(CONFIG_USB_MUSB_GADGET) && !CONFIG_IS_ENABLED(DM_USB_GADGET)
438         case MUSB_PERIPHERAL:
439                 musbp = &gadget;
440                 break;
441 #endif
442         default:
443                 return ERR_PTR(-EINVAL);
444         }
445
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);
450         }
451
452         return *musbp;
453 }