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