blob: 99a37ed03e278429a08513e39a6dd6446af53d27 [file] [log] [blame]
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01001/*
2 * USB Peripheral Controller driver for Aeroflex Gaisler GRUSBDC.
3 *
4 * 2013 (c) Aeroflex Gaisler AB
5 *
6 * This driver supports GRUSBDC USB Device Controller cores available in the
7 * GRLIB VHDL IP core library.
8 *
9 * Full documentation of the GRUSBDC core can be found here:
10 * http://www.gaisler.com/products/grlib/grip.pdf
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 * Contributors:
18 * - Andreas Larsson <andreas@gaisler.com>
19 * - Marko Isomaki
20 */
21
22/*
23 * A GRUSBDC core can have up to 16 IN endpoints and 16 OUT endpoints each
24 * individually configurable to any of the four USB transfer types. This driver
25 * only supports cores in DMA mode.
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/slab.h>
31#include <linux/spinlock.h>
32#include <linux/errno.h>
Andreas Larsson27e9dcc2013-12-23 21:25:49 +010033#include <linux/list.h>
34#include <linux/interrupt.h>
35#include <linux/device.h>
36#include <linux/usb/ch9.h>
37#include <linux/usb/gadget.h>
38#include <linux/dma-mapping.h>
39#include <linux/dmapool.h>
40#include <linux/debugfs.h>
41#include <linux/seq_file.h>
42#include <linux/of_platform.h>
43#include <linux/of_irq.h>
44#include <linux/of_address.h>
45
46#include <asm/byteorder.h>
47
48#include "gr_udc.h"
49
50#define DRIVER_NAME "gr_udc"
51#define DRIVER_DESC "Aeroflex Gaisler GRUSBDC USB Peripheral Controller"
52
53static const char driver_name[] = DRIVER_NAME;
54static const char driver_desc[] = DRIVER_DESC;
55
56#define gr_read32(x) (ioread32be((x)))
57#define gr_write32(x, v) (iowrite32be((v), (x)))
58
59/* USB speed and corresponding string calculated from status register value */
60#define GR_SPEED(status) \
61 ((status & GR_STATUS_SP) ? USB_SPEED_FULL : USB_SPEED_HIGH)
62#define GR_SPEED_STR(status) usb_speed_string(GR_SPEED(status))
63
64/* Size of hardware buffer calculated from epctrl register value */
65#define GR_BUFFER_SIZE(epctrl) \
66 ((((epctrl) & GR_EPCTRL_BUFSZ_MASK) >> GR_EPCTRL_BUFSZ_POS) * \
67 GR_EPCTRL_BUFSZ_SCALER)
68
69/* ---------------------------------------------------------------------- */
70/* Debug printout functionality */
71
72static const char * const gr_modestring[] = {"control", "iso", "bulk", "int"};
73
74static const char *gr_ep0state_string(enum gr_ep0state state)
75{
76 static const char *const names[] = {
77 [GR_EP0_DISCONNECT] = "disconnect",
78 [GR_EP0_SETUP] = "setup",
79 [GR_EP0_IDATA] = "idata",
80 [GR_EP0_ODATA] = "odata",
81 [GR_EP0_ISTATUS] = "istatus",
82 [GR_EP0_OSTATUS] = "ostatus",
83 [GR_EP0_STALL] = "stall",
84 [GR_EP0_SUSPEND] = "suspend",
85 };
86
87 if (state < 0 || state >= ARRAY_SIZE(names))
88 return "UNKNOWN";
89
90 return names[state];
91}
92
93#ifdef VERBOSE_DEBUG
94
95static void gr_dbgprint_request(const char *str, struct gr_ep *ep,
96 struct gr_request *req)
97{
98 int buflen = ep->is_in ? req->req.length : req->req.actual;
99 int rowlen = 32;
100 int plen = min(rowlen, buflen);
101
102 dev_dbg(ep->dev->dev, "%s: 0x%p, %d bytes data%s:\n", str, req, buflen,
103 (buflen > plen ? " (truncated)" : ""));
104 print_hex_dump_debug(" ", DUMP_PREFIX_NONE,
105 rowlen, 4, req->req.buf, plen, false);
106}
107
108static void gr_dbgprint_devreq(struct gr_udc *dev, u8 type, u8 request,
109 u16 value, u16 index, u16 length)
110{
111 dev_vdbg(dev->dev, "REQ: %02x.%02x v%04x i%04x l%04x\n",
112 type, request, value, index, length);
113}
114#else /* !VERBOSE_DEBUG */
115
116static void gr_dbgprint_request(const char *str, struct gr_ep *ep,
117 struct gr_request *req) {}
118
119static void gr_dbgprint_devreq(struct gr_udc *dev, u8 type, u8 request,
120 u16 value, u16 index, u16 length) {}
121
122#endif /* VERBOSE_DEBUG */
123
124/* ---------------------------------------------------------------------- */
125/* Debugfs functionality */
126
127#ifdef CONFIG_USB_GADGET_DEBUG_FS
128
129static void gr_seq_ep_show(struct seq_file *seq, struct gr_ep *ep)
130{
131 u32 epctrl = gr_read32(&ep->regs->epctrl);
132 u32 epstat = gr_read32(&ep->regs->epstat);
133 int mode = (epctrl & GR_EPCTRL_TT_MASK) >> GR_EPCTRL_TT_POS;
134 struct gr_request *req;
135
136 seq_printf(seq, "%s:\n", ep->ep.name);
137 seq_printf(seq, " mode = %s\n", gr_modestring[mode]);
138 seq_printf(seq, " halted: %d\n", !!(epctrl & GR_EPCTRL_EH));
139 seq_printf(seq, " disabled: %d\n", !!(epctrl & GR_EPCTRL_ED));
140 seq_printf(seq, " valid: %d\n", !!(epctrl & GR_EPCTRL_EV));
141 seq_printf(seq, " dma_start = %d\n", ep->dma_start);
142 seq_printf(seq, " stopped = %d\n", ep->stopped);
143 seq_printf(seq, " wedged = %d\n", ep->wedged);
144 seq_printf(seq, " callback = %d\n", ep->callback);
145 seq_printf(seq, " maxpacket = %d\n", ep->ep.maxpacket);
Andreas Larsson5bddbd72014-03-27 16:15:57 +0100146 seq_printf(seq, " maxpacket_limit = %d\n", ep->ep.maxpacket_limit);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +0100147 seq_printf(seq, " bytes_per_buffer = %d\n", ep->bytes_per_buffer);
148 if (mode == 1 || mode == 3)
149 seq_printf(seq, " nt = %d\n",
150 (epctrl & GR_EPCTRL_NT_MASK) >> GR_EPCTRL_NT_POS);
151
152 seq_printf(seq, " Buffer 0: %s %s%d\n",
153 epstat & GR_EPSTAT_B0 ? "valid" : "invalid",
154 epstat & GR_EPSTAT_BS ? " " : "selected ",
155 (epstat & GR_EPSTAT_B0CNT_MASK) >> GR_EPSTAT_B0CNT_POS);
156 seq_printf(seq, " Buffer 1: %s %s%d\n",
157 epstat & GR_EPSTAT_B1 ? "valid" : "invalid",
158 epstat & GR_EPSTAT_BS ? "selected " : " ",
159 (epstat & GR_EPSTAT_B1CNT_MASK) >> GR_EPSTAT_B1CNT_POS);
160
161 if (list_empty(&ep->queue)) {
162 seq_puts(seq, " Queue: empty\n\n");
163 return;
164 }
165
166 seq_puts(seq, " Queue:\n");
167 list_for_each_entry(req, &ep->queue, queue) {
168 struct gr_dma_desc *desc;
169 struct gr_dma_desc *next;
170
171 seq_printf(seq, " 0x%p: 0x%p %d %d\n", req,
172 &req->req.buf, req->req.actual, req->req.length);
173
174 next = req->first_desc;
175 do {
176 desc = next;
177 next = desc->next_desc;
178 seq_printf(seq, " %c 0x%p (0x%08x): 0x%05x 0x%08x\n",
179 desc == req->curr_desc ? 'c' : ' ',
180 desc, desc->paddr, desc->ctrl, desc->data);
181 } while (desc != req->last_desc);
182 }
183 seq_puts(seq, "\n");
184}
185
186
187static int gr_seq_show(struct seq_file *seq, void *v)
188{
189 struct gr_udc *dev = seq->private;
190 u32 control = gr_read32(&dev->regs->control);
191 u32 status = gr_read32(&dev->regs->status);
192 struct gr_ep *ep;
193
194 seq_printf(seq, "usb state = %s\n",
195 usb_state_string(dev->gadget.state));
196 seq_printf(seq, "address = %d\n",
197 (control & GR_CONTROL_UA_MASK) >> GR_CONTROL_UA_POS);
198 seq_printf(seq, "speed = %s\n", GR_SPEED_STR(status));
199 seq_printf(seq, "ep0state = %s\n", gr_ep0state_string(dev->ep0state));
200 seq_printf(seq, "irq_enabled = %d\n", dev->irq_enabled);
201 seq_printf(seq, "remote_wakeup = %d\n", dev->remote_wakeup);
202 seq_printf(seq, "test_mode = %d\n", dev->test_mode);
203 seq_puts(seq, "\n");
204
205 list_for_each_entry(ep, &dev->ep_list, ep_list)
206 gr_seq_ep_show(seq, ep);
207
208 return 0;
209}
210
211static int gr_dfs_open(struct inode *inode, struct file *file)
212{
213 return single_open(file, gr_seq_show, inode->i_private);
214}
215
216static const struct file_operations gr_dfs_fops = {
217 .owner = THIS_MODULE,
218 .open = gr_dfs_open,
219 .read = seq_read,
220 .llseek = seq_lseek,
221 .release = single_release,
222};
223
224static void gr_dfs_create(struct gr_udc *dev)
225{
226 const char *name = "gr_udc_state";
227
228 dev->dfs_root = debugfs_create_dir(dev_name(dev->dev), NULL);
Dan Carpenter798a2462014-01-17 16:34:11 +0300229 dev->dfs_state = debugfs_create_file(name, 0444, dev->dfs_root, dev,
230 &gr_dfs_fops);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +0100231}
232
233static void gr_dfs_delete(struct gr_udc *dev)
234{
235 /* Handles NULL and ERR pointers internally */
236 debugfs_remove(dev->dfs_state);
237 debugfs_remove(dev->dfs_root);
238}
239
240#else /* !CONFIG_USB_GADGET_DEBUG_FS */
241
242static void gr_dfs_create(struct gr_udc *dev) {}
243static void gr_dfs_delete(struct gr_udc *dev) {}
244
245#endif /* CONFIG_USB_GADGET_DEBUG_FS */
246
247/* ---------------------------------------------------------------------- */
248/* DMA and request handling */
249
250/* Allocates a new struct gr_dma_desc, sets paddr and zeroes the rest */
251static struct gr_dma_desc *gr_alloc_dma_desc(struct gr_ep *ep, gfp_t gfp_flags)
252{
253 dma_addr_t paddr;
254 struct gr_dma_desc *dma_desc;
255
256 dma_desc = dma_pool_alloc(ep->dev->desc_pool, gfp_flags, &paddr);
257 if (!dma_desc) {
258 dev_err(ep->dev->dev, "Could not allocate from DMA pool\n");
259 return NULL;
260 }
261
262 memset(dma_desc, 0, sizeof(*dma_desc));
263 dma_desc->paddr = paddr;
264
265 return dma_desc;
266}
267
268static inline void gr_free_dma_desc(struct gr_udc *dev,
269 struct gr_dma_desc *desc)
270{
271 dma_pool_free(dev->desc_pool, desc, (dma_addr_t)desc->paddr);
272}
273
274/* Frees the chain of struct gr_dma_desc for the given request */
275static void gr_free_dma_desc_chain(struct gr_udc *dev, struct gr_request *req)
276{
277 struct gr_dma_desc *desc;
278 struct gr_dma_desc *next;
279
280 next = req->first_desc;
281 if (!next)
282 return;
283
284 do {
285 desc = next;
286 next = desc->next_desc;
287 gr_free_dma_desc(dev, desc);
288 } while (desc != req->last_desc);
289
290 req->first_desc = NULL;
291 req->curr_desc = NULL;
292 req->last_desc = NULL;
293}
294
295static void gr_ep0_setup(struct gr_udc *dev, struct gr_request *req);
296
297/*
298 * Frees allocated resources and calls the appropriate completion function/setup
299 * package handler for a finished request.
300 *
301 * Must be called with dev->lock held and irqs disabled.
302 */
303static void gr_finish_request(struct gr_ep *ep, struct gr_request *req,
304 int status)
305 __releases(&dev->lock)
306 __acquires(&dev->lock)
307{
308 struct gr_udc *dev;
309
310 list_del_init(&req->queue);
311
312 if (likely(req->req.status == -EINPROGRESS))
313 req->req.status = status;
314 else
315 status = req->req.status;
316
317 dev = ep->dev;
318 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
319 gr_free_dma_desc_chain(dev, req);
320
321 if (ep->is_in) /* For OUT, actual gets updated bit by bit */
322 req->req.actual = req->req.length;
323
324 if (!status) {
325 if (ep->is_in)
326 gr_dbgprint_request("SENT", ep, req);
327 else
328 gr_dbgprint_request("RECV", ep, req);
329 }
330
331 /* Prevent changes to ep->queue during callback */
332 ep->callback = 1;
333 if (req == dev->ep0reqo && !status) {
334 if (req->setup)
335 gr_ep0_setup(dev, req);
336 else
337 dev_err(dev->dev,
338 "Unexpected non setup packet on ep0in\n");
339 } else if (req->req.complete) {
340 spin_unlock(&dev->lock);
341
342 req->req.complete(&ep->ep, &req->req);
343
344 spin_lock(&dev->lock);
345 }
346 ep->callback = 0;
347}
348
349static struct usb_request *gr_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
350{
351 struct gr_request *req;
352
353 req = kzalloc(sizeof(*req), gfp_flags);
354 if (!req)
355 return NULL;
356
357 INIT_LIST_HEAD(&req->queue);
358
359 return &req->req;
360}
361
362/*
363 * Starts DMA for endpoint ep if there are requests in the queue.
364 *
365 * Must be called with dev->lock held and with !ep->stopped.
366 */
367static void gr_start_dma(struct gr_ep *ep)
368{
369 struct gr_request *req;
370 u32 dmactrl;
371
372 if (list_empty(&ep->queue)) {
373 ep->dma_start = 0;
374 return;
375 }
376
377 req = list_first_entry(&ep->queue, struct gr_request, queue);
378
379 /* A descriptor should already have been allocated */
380 BUG_ON(!req->curr_desc);
381
382 wmb(); /* Make sure all is settled before handing it over to DMA */
383
384 /* Set the descriptor pointer in the hardware */
385 gr_write32(&ep->regs->dmaaddr, req->curr_desc->paddr);
386
387 /* Announce available descriptors */
388 dmactrl = gr_read32(&ep->regs->dmactrl);
389 gr_write32(&ep->regs->dmactrl, dmactrl | GR_DMACTRL_DA);
390
391 ep->dma_start = 1;
392}
393
394/*
395 * Finishes the first request in the ep's queue and, if available, starts the
396 * next request in queue.
397 *
398 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
399 */
400static void gr_dma_advance(struct gr_ep *ep, int status)
401{
402 struct gr_request *req;
403
404 req = list_first_entry(&ep->queue, struct gr_request, queue);
405 gr_finish_request(ep, req, status);
406 gr_start_dma(ep); /* Regardless of ep->dma_start */
407}
408
409/*
410 * Abort DMA for an endpoint. Sets the abort DMA bit which causes an ongoing DMA
411 * transfer to be canceled and clears GR_DMACTRL_DA.
412 *
413 * Must be called with dev->lock held.
414 */
415static void gr_abort_dma(struct gr_ep *ep)
416{
417 u32 dmactrl;
418
419 dmactrl = gr_read32(&ep->regs->dmactrl);
420 gr_write32(&ep->regs->dmactrl, dmactrl | GR_DMACTRL_AD);
421}
422
423/*
424 * Allocates and sets up a struct gr_dma_desc and putting it on the descriptor
425 * chain.
426 *
427 * Size is not used for OUT endpoints. Hardware can not be instructed to handle
428 * smaller buffer than MAXPL in the OUT direction.
429 */
430static int gr_add_dma_desc(struct gr_ep *ep, struct gr_request *req,
431 dma_addr_t data, unsigned size, gfp_t gfp_flags)
432{
433 struct gr_dma_desc *desc;
434
435 desc = gr_alloc_dma_desc(ep, gfp_flags);
436 if (!desc)
437 return -ENOMEM;
438
439 desc->data = data;
440 if (ep->is_in)
441 desc->ctrl =
442 (GR_DESC_IN_CTRL_LEN_MASK & size) | GR_DESC_IN_CTRL_EN;
443 else
444 desc->ctrl = GR_DESC_OUT_CTRL_IE;
445
446 if (!req->first_desc) {
447 req->first_desc = desc;
448 req->curr_desc = desc;
449 } else {
450 req->last_desc->next_desc = desc;
451 req->last_desc->next = desc->paddr;
452 req->last_desc->ctrl |= GR_DESC_OUT_CTRL_NX;
453 }
454 req->last_desc = desc;
455
456 return 0;
457}
458
459/*
460 * Sets up a chain of struct gr_dma_descriptors pointing to buffers that
461 * together covers req->req.length bytes of the buffer at DMA address
462 * req->req.dma for the OUT direction.
463 *
464 * The first descriptor in the chain is enabled, the rest disabled. The
465 * interrupt handler will later enable them one by one when needed so we can
466 * find out when the transfer is finished. For OUT endpoints, all descriptors
467 * therefore generate interrutps.
468 */
469static int gr_setup_out_desc_list(struct gr_ep *ep, struct gr_request *req,
470 gfp_t gfp_flags)
471{
472 u16 bytes_left; /* Bytes left to provide descriptors for */
473 u16 bytes_used; /* Bytes accommodated for */
474 int ret = 0;
475
476 req->first_desc = NULL; /* Signals that no allocation is done yet */
477 bytes_left = req->req.length;
478 bytes_used = 0;
479 while (bytes_left > 0) {
480 dma_addr_t start = req->req.dma + bytes_used;
481 u16 size = min(bytes_left, ep->bytes_per_buffer);
482
483 /* Should not happen however - gr_queue stops such lengths */
484 if (size < ep->bytes_per_buffer)
485 dev_warn(ep->dev->dev,
486 "Buffer overrun risk: %u < %u bytes/buffer\n",
487 size, ep->bytes_per_buffer);
488
489 ret = gr_add_dma_desc(ep, req, start, size, gfp_flags);
490 if (ret)
491 goto alloc_err;
492
493 bytes_left -= size;
494 bytes_used += size;
495 }
496
497 req->first_desc->ctrl |= GR_DESC_OUT_CTRL_EN;
498
499 return 0;
500
501alloc_err:
502 gr_free_dma_desc_chain(ep->dev, req);
503
504 return ret;
505}
506
507/*
508 * Sets up a chain of struct gr_dma_descriptors pointing to buffers that
509 * together covers req->req.length bytes of the buffer at DMA address
510 * req->req.dma for the IN direction.
511 *
512 * When more data is provided than the maximum payload size, the hardware splits
513 * this up into several payloads automatically. Moreover, ep->bytes_per_buffer
514 * is always set to a multiple of the maximum payload (restricted to the valid
515 * number of maximum payloads during high bandwidth isochronous or interrupt
516 * transfers)
517 *
518 * All descriptors are enabled from the beginning and we only generate an
519 * interrupt for the last one indicating that the entire request has been pushed
520 * to hardware.
521 */
522static int gr_setup_in_desc_list(struct gr_ep *ep, struct gr_request *req,
523 gfp_t gfp_flags)
524{
525 u16 bytes_left; /* Bytes left in req to provide descriptors for */
526 u16 bytes_used; /* Bytes in req accommodated for */
527 int ret = 0;
528
529 req->first_desc = NULL; /* Signals that no allocation is done yet */
530 bytes_left = req->req.length;
531 bytes_used = 0;
532 do { /* Allow for zero length packets */
533 dma_addr_t start = req->req.dma + bytes_used;
534 u16 size = min(bytes_left, ep->bytes_per_buffer);
535
536 ret = gr_add_dma_desc(ep, req, start, size, gfp_flags);
537 if (ret)
538 goto alloc_err;
539
540 bytes_left -= size;
541 bytes_used += size;
542 } while (bytes_left > 0);
543
544 /*
545 * Send an extra zero length packet to indicate that no more data is
546 * available when req->req.zero is set and the data length is even
547 * multiples of ep->ep.maxpacket.
548 */
549 if (req->req.zero && (req->req.length % ep->ep.maxpacket == 0)) {
550 ret = gr_add_dma_desc(ep, req, 0, 0, gfp_flags);
551 if (ret)
552 goto alloc_err;
553 }
554
555 /*
556 * For IN packets we only want to know when the last packet has been
557 * transmitted (not just put into internal buffers).
558 */
559 req->last_desc->ctrl |= GR_DESC_IN_CTRL_PI;
560
561 return 0;
562
563alloc_err:
564 gr_free_dma_desc_chain(ep->dev, req);
565
566 return ret;
567}
568
569/* Must be called with dev->lock held */
570static int gr_queue(struct gr_ep *ep, struct gr_request *req, gfp_t gfp_flags)
571{
572 struct gr_udc *dev = ep->dev;
573 int ret;
574
575 if (unlikely(!ep->ep.desc && ep->num != 0)) {
576 dev_err(dev->dev, "No ep descriptor for %s\n", ep->ep.name);
577 return -EINVAL;
578 }
579
580 if (unlikely(!req->req.buf || !list_empty(&req->queue))) {
581 dev_err(dev->dev,
582 "Invalid request for %s: buf=%p list_empty=%d\n",
583 ep->ep.name, req->req.buf, list_empty(&req->queue));
584 return -EINVAL;
585 }
586
587 /*
588 * The DMA controller can not handle smaller OUT buffers than
589 * maxpacket. It could lead to buffer overruns if unexpectedly long
590 * packet are received.
591 */
592 if (!ep->is_in && (req->req.length % ep->ep.maxpacket) != 0) {
593 dev_err(dev->dev,
594 "OUT request length %d is not multiple of maxpacket\n",
595 req->req.length);
596 return -EMSGSIZE;
597 }
598
599 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
600 dev_err(dev->dev, "-ESHUTDOWN");
601 return -ESHUTDOWN;
602 }
603
604 /* Can't touch registers when suspended */
605 if (dev->ep0state == GR_EP0_SUSPEND) {
606 dev_err(dev->dev, "-EBUSY");
607 return -EBUSY;
608 }
609
610 /* Set up DMA mapping in case the caller didn't */
611 ret = usb_gadget_map_request(&dev->gadget, &req->req, ep->is_in);
612 if (ret) {
613 dev_err(dev->dev, "usb_gadget_map_request");
614 return ret;
615 }
616
617 if (ep->is_in)
618 ret = gr_setup_in_desc_list(ep, req, gfp_flags);
619 else
620 ret = gr_setup_out_desc_list(ep, req, gfp_flags);
621 if (ret)
622 return ret;
623
624 req->req.status = -EINPROGRESS;
625 req->req.actual = 0;
626 list_add_tail(&req->queue, &ep->queue);
627
628 /* Start DMA if not started, otherwise interrupt handler handles it */
629 if (!ep->dma_start && likely(!ep->stopped))
630 gr_start_dma(ep);
631
632 return 0;
633}
634
635/*
636 * Queue a request from within the driver.
637 *
638 * Must be called with dev->lock held.
639 */
640static inline int gr_queue_int(struct gr_ep *ep, struct gr_request *req,
641 gfp_t gfp_flags)
642{
643 if (ep->is_in)
644 gr_dbgprint_request("RESP", ep, req);
645
646 return gr_queue(ep, req, gfp_flags);
647}
648
649/* ---------------------------------------------------------------------- */
650/* General helper functions */
651
652/*
653 * Dequeue ALL requests.
654 *
655 * Must be called with dev->lock held and irqs disabled.
656 */
657static void gr_ep_nuke(struct gr_ep *ep)
658{
659 struct gr_request *req;
Andreas Larsson27e9dcc2013-12-23 21:25:49 +0100660
661 ep->stopped = 1;
662 ep->dma_start = 0;
663 gr_abort_dma(ep);
664
665 while (!list_empty(&ep->queue)) {
666 req = list_first_entry(&ep->queue, struct gr_request, queue);
667 gr_finish_request(ep, req, -ESHUTDOWN);
668 }
669}
670
671/*
672 * Reset the hardware state of this endpoint.
673 *
674 * Must be called with dev->lock held.
675 */
676static void gr_ep_reset(struct gr_ep *ep)
677{
678 gr_write32(&ep->regs->epctrl, 0);
679 gr_write32(&ep->regs->dmactrl, 0);
680
681 ep->ep.maxpacket = MAX_CTRL_PL_SIZE;
682 ep->ep.desc = NULL;
683 ep->stopped = 1;
684 ep->dma_start = 0;
685}
686
687/*
688 * Generate STALL on ep0in/out.
689 *
690 * Must be called with dev->lock held.
691 */
692static void gr_control_stall(struct gr_udc *dev)
693{
694 u32 epctrl;
695
696 epctrl = gr_read32(&dev->epo[0].regs->epctrl);
697 gr_write32(&dev->epo[0].regs->epctrl, epctrl | GR_EPCTRL_CS);
698 epctrl = gr_read32(&dev->epi[0].regs->epctrl);
699 gr_write32(&dev->epi[0].regs->epctrl, epctrl | GR_EPCTRL_CS);
700
701 dev->ep0state = GR_EP0_STALL;
702}
703
704/*
705 * Halts, halts and wedges, or clears halt for an endpoint.
706 *
707 * Must be called with dev->lock held.
708 */
709static int gr_ep_halt_wedge(struct gr_ep *ep, int halt, int wedge, int fromhost)
710{
711 u32 epctrl;
712 int retval = 0;
713
714 if (ep->num && !ep->ep.desc)
715 return -EINVAL;
716
717 if (ep->num && ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC)
718 return -EOPNOTSUPP;
719
720 /* Never actually halt ep0, and therefore never clear halt for ep0 */
721 if (!ep->num) {
722 if (halt && !fromhost) {
723 /* ep0 halt from gadget - generate protocol stall */
724 gr_control_stall(ep->dev);
725 dev_dbg(ep->dev->dev, "EP: stall ep0\n");
726 return 0;
727 }
728 return -EINVAL;
729 }
730
731 dev_dbg(ep->dev->dev, "EP: %s halt %s\n",
732 (halt ? (wedge ? "wedge" : "set") : "clear"), ep->ep.name);
733
734 epctrl = gr_read32(&ep->regs->epctrl);
735 if (halt) {
736 /* Set HALT */
737 gr_write32(&ep->regs->epctrl, epctrl | GR_EPCTRL_EH);
738 ep->stopped = 1;
739 if (wedge)
740 ep->wedged = 1;
741 } else {
742 gr_write32(&ep->regs->epctrl, epctrl & ~GR_EPCTRL_EH);
743 ep->stopped = 0;
744 ep->wedged = 0;
745
746 /* Things might have been queued up in the meantime */
747 if (!ep->dma_start)
748 gr_start_dma(ep);
749 }
750
751 return retval;
752}
753
754/* Must be called with dev->lock held */
755static inline void gr_set_ep0state(struct gr_udc *dev, enum gr_ep0state value)
756{
757 if (dev->ep0state != value)
758 dev_vdbg(dev->dev, "STATE: ep0state=%s\n",
759 gr_ep0state_string(value));
760 dev->ep0state = value;
761}
762
763/*
764 * Should only be called when endpoints can not generate interrupts.
765 *
766 * Must be called with dev->lock held.
767 */
768static void gr_disable_interrupts_and_pullup(struct gr_udc *dev)
769{
770 gr_write32(&dev->regs->control, 0);
771 wmb(); /* Make sure that we do not deny one of our interrupts */
772 dev->irq_enabled = 0;
773}
774
775/*
776 * Stop all device activity and disable data line pullup.
777 *
778 * Must be called with dev->lock held and irqs disabled.
779 */
780static void gr_stop_activity(struct gr_udc *dev)
781{
782 struct gr_ep *ep;
783
784 list_for_each_entry(ep, &dev->ep_list, ep_list)
785 gr_ep_nuke(ep);
786
787 gr_disable_interrupts_and_pullup(dev);
788
789 gr_set_ep0state(dev, GR_EP0_DISCONNECT);
790 usb_gadget_set_state(&dev->gadget, USB_STATE_NOTATTACHED);
791}
792
793/* ---------------------------------------------------------------------- */
794/* ep0 setup packet handling */
795
796static void gr_ep0_testmode_complete(struct usb_ep *_ep,
797 struct usb_request *_req)
798{
799 struct gr_ep *ep;
800 struct gr_udc *dev;
801 u32 control;
802
803 ep = container_of(_ep, struct gr_ep, ep);
804 dev = ep->dev;
805
806 spin_lock(&dev->lock);
807
808 control = gr_read32(&dev->regs->control);
809 control |= GR_CONTROL_TM | (dev->test_mode << GR_CONTROL_TS_POS);
810 gr_write32(&dev->regs->control, control);
811
812 spin_unlock(&dev->lock);
813}
814
815static void gr_ep0_dummy_complete(struct usb_ep *_ep, struct usb_request *_req)
816{
817 /* Nothing needs to be done here */
818}
819
820/*
821 * Queue a response on ep0in.
822 *
823 * Must be called with dev->lock held.
824 */
825static int gr_ep0_respond(struct gr_udc *dev, u8 *buf, int length,
826 void (*complete)(struct usb_ep *ep,
827 struct usb_request *req))
828{
829 u8 *reqbuf = dev->ep0reqi->req.buf;
830 int status;
831 int i;
832
833 for (i = 0; i < length; i++)
834 reqbuf[i] = buf[i];
835 dev->ep0reqi->req.length = length;
836 dev->ep0reqi->req.complete = complete;
837
838 status = gr_queue_int(&dev->epi[0], dev->ep0reqi, GFP_ATOMIC);
839 if (status < 0)
840 dev_err(dev->dev,
841 "Could not queue ep0in setup response: %d\n", status);
842
843 return status;
844}
845
846/*
847 * Queue a 2 byte response on ep0in.
848 *
849 * Must be called with dev->lock held.
850 */
851static inline int gr_ep0_respond_u16(struct gr_udc *dev, u16 response)
852{
853 __le16 le_response = cpu_to_le16(response);
854
855 return gr_ep0_respond(dev, (u8 *)&le_response, 2,
856 gr_ep0_dummy_complete);
857}
858
859/*
860 * Queue a ZLP response on ep0in.
861 *
862 * Must be called with dev->lock held.
863 */
864static inline int gr_ep0_respond_empty(struct gr_udc *dev)
865{
866 return gr_ep0_respond(dev, NULL, 0, gr_ep0_dummy_complete);
867}
868
869/*
870 * This is run when a SET_ADDRESS request is received. First writes
871 * the new address to the control register which is updated internally
872 * when the next IN packet is ACKED.
873 *
874 * Must be called with dev->lock held.
875 */
876static void gr_set_address(struct gr_udc *dev, u8 address)
877{
878 u32 control;
879
880 control = gr_read32(&dev->regs->control) & ~GR_CONTROL_UA_MASK;
881 control |= (address << GR_CONTROL_UA_POS) & GR_CONTROL_UA_MASK;
882 control |= GR_CONTROL_SU;
883 gr_write32(&dev->regs->control, control);
884}
885
886/*
887 * Returns negative for STALL, 0 for successful handling and positive for
888 * delegation.
889 *
890 * Must be called with dev->lock held.
891 */
892static int gr_device_request(struct gr_udc *dev, u8 type, u8 request,
893 u16 value, u16 index)
894{
895 u16 response;
896 u8 test;
897
898 switch (request) {
899 case USB_REQ_SET_ADDRESS:
900 dev_dbg(dev->dev, "STATUS: address %d\n", value & 0xff);
901 gr_set_address(dev, value & 0xff);
902 if (value)
903 usb_gadget_set_state(&dev->gadget, USB_STATE_ADDRESS);
904 else
905 usb_gadget_set_state(&dev->gadget, USB_STATE_DEFAULT);
906 return gr_ep0_respond_empty(dev);
907
908 case USB_REQ_GET_STATUS:
909 /* Self powered | remote wakeup */
910 response = 0x0001 | (dev->remote_wakeup ? 0x0002 : 0);
911 return gr_ep0_respond_u16(dev, response);
912
913 case USB_REQ_SET_FEATURE:
914 switch (value) {
915 case USB_DEVICE_REMOTE_WAKEUP:
916 /* Allow remote wakeup */
917 dev->remote_wakeup = 1;
918 return gr_ep0_respond_empty(dev);
919
920 case USB_DEVICE_TEST_MODE:
921 /* The hardware does not support TEST_FORCE_EN */
922 test = index >> 8;
923 if (test >= TEST_J && test <= TEST_PACKET) {
924 dev->test_mode = test;
925 return gr_ep0_respond(dev, NULL, 0,
926 gr_ep0_testmode_complete);
927 }
928 }
929 break;
930
931 case USB_REQ_CLEAR_FEATURE:
932 switch (value) {
933 case USB_DEVICE_REMOTE_WAKEUP:
934 /* Disallow remote wakeup */
935 dev->remote_wakeup = 0;
936 return gr_ep0_respond_empty(dev);
937 }
938 break;
939 }
940
941 return 1; /* Delegate the rest */
942}
943
944/*
945 * Returns negative for STALL, 0 for successful handling and positive for
946 * delegation.
947 *
948 * Must be called with dev->lock held.
949 */
950static int gr_interface_request(struct gr_udc *dev, u8 type, u8 request,
951 u16 value, u16 index)
952{
953 if (dev->gadget.state != USB_STATE_CONFIGURED)
954 return -1;
955
956 /*
957 * Should return STALL for invalid interfaces, but udc driver does not
958 * know anything about that. However, many gadget drivers do not handle
959 * GET_STATUS so we need to take care of that.
960 */
961
962 switch (request) {
963 case USB_REQ_GET_STATUS:
964 return gr_ep0_respond_u16(dev, 0x0000);
965
966 case USB_REQ_SET_FEATURE:
967 case USB_REQ_CLEAR_FEATURE:
968 /*
969 * No possible valid standard requests. Still let gadget drivers
970 * have a go at it.
971 */
972 break;
973 }
974
975 return 1; /* Delegate the rest */
976}
977
978/*
979 * Returns negative for STALL, 0 for successful handling and positive for
980 * delegation.
981 *
982 * Must be called with dev->lock held.
983 */
984static int gr_endpoint_request(struct gr_udc *dev, u8 type, u8 request,
985 u16 value, u16 index)
986{
987 struct gr_ep *ep;
988 int status;
989 int halted;
990 u8 epnum = index & USB_ENDPOINT_NUMBER_MASK;
991 u8 is_in = index & USB_ENDPOINT_DIR_MASK;
992
993 if ((is_in && epnum >= dev->nepi) || (!is_in && epnum >= dev->nepo))
994 return -1;
995
996 if (dev->gadget.state != USB_STATE_CONFIGURED && epnum != 0)
997 return -1;
998
999 ep = (is_in ? &dev->epi[epnum] : &dev->epo[epnum]);
1000
1001 switch (request) {
1002 case USB_REQ_GET_STATUS:
1003 halted = gr_read32(&ep->regs->epctrl) & GR_EPCTRL_EH;
1004 return gr_ep0_respond_u16(dev, halted ? 0x0001 : 0);
1005
1006 case USB_REQ_SET_FEATURE:
1007 switch (value) {
1008 case USB_ENDPOINT_HALT:
1009 status = gr_ep_halt_wedge(ep, 1, 0, 1);
1010 if (status >= 0)
1011 status = gr_ep0_respond_empty(dev);
1012 return status;
1013 }
1014 break;
1015
1016 case USB_REQ_CLEAR_FEATURE:
1017 switch (value) {
1018 case USB_ENDPOINT_HALT:
1019 if (ep->wedged)
1020 return -1;
1021 status = gr_ep_halt_wedge(ep, 0, 0, 1);
1022 if (status >= 0)
1023 status = gr_ep0_respond_empty(dev);
1024 return status;
1025 }
1026 break;
1027 }
1028
1029 return 1; /* Delegate the rest */
1030}
1031
1032/* Must be called with dev->lock held */
1033static void gr_ep0out_requeue(struct gr_udc *dev)
1034{
1035 int ret = gr_queue_int(&dev->epo[0], dev->ep0reqo, GFP_ATOMIC);
1036
1037 if (ret)
1038 dev_err(dev->dev, "Could not queue ep0out setup request: %d\n",
1039 ret);
1040}
1041
1042/*
1043 * The main function dealing with setup requests on ep0.
1044 *
1045 * Must be called with dev->lock held and irqs disabled
1046 */
1047static void gr_ep0_setup(struct gr_udc *dev, struct gr_request *req)
1048 __releases(&dev->lock)
1049 __acquires(&dev->lock)
1050{
1051 union {
1052 struct usb_ctrlrequest ctrl;
1053 u8 raw[8];
1054 u32 word[2];
1055 } u;
1056 u8 type;
1057 u8 request;
1058 u16 value;
1059 u16 index;
1060 u16 length;
1061 int i;
1062 int status;
1063
1064 /* Restore from ep0 halt */
1065 if (dev->ep0state == GR_EP0_STALL) {
1066 gr_set_ep0state(dev, GR_EP0_SETUP);
1067 if (!req->req.actual)
1068 goto out;
1069 }
1070
1071 if (dev->ep0state == GR_EP0_ISTATUS) {
1072 gr_set_ep0state(dev, GR_EP0_SETUP);
1073 if (req->req.actual > 0)
1074 dev_dbg(dev->dev,
1075 "Unexpected setup packet at state %s\n",
1076 gr_ep0state_string(GR_EP0_ISTATUS));
1077 else
1078 goto out; /* Got expected ZLP */
1079 } else if (dev->ep0state != GR_EP0_SETUP) {
1080 dev_info(dev->dev,
1081 "Unexpected ep0out request at state %s - stalling\n",
1082 gr_ep0state_string(dev->ep0state));
1083 gr_control_stall(dev);
1084 gr_set_ep0state(dev, GR_EP0_SETUP);
1085 goto out;
1086 } else if (!req->req.actual) {
1087 dev_dbg(dev->dev, "Unexpected ZLP at state %s\n",
1088 gr_ep0state_string(dev->ep0state));
1089 goto out;
1090 }
1091
1092 /* Handle SETUP packet */
1093 for (i = 0; i < req->req.actual; i++)
1094 u.raw[i] = ((u8 *)req->req.buf)[i];
1095
1096 type = u.ctrl.bRequestType;
1097 request = u.ctrl.bRequest;
1098 value = le16_to_cpu(u.ctrl.wValue);
1099 index = le16_to_cpu(u.ctrl.wIndex);
1100 length = le16_to_cpu(u.ctrl.wLength);
1101
1102 gr_dbgprint_devreq(dev, type, request, value, index, length);
1103
1104 /* Check for data stage */
1105 if (length) {
1106 if (type & USB_DIR_IN)
1107 gr_set_ep0state(dev, GR_EP0_IDATA);
1108 else
1109 gr_set_ep0state(dev, GR_EP0_ODATA);
1110 }
1111
1112 status = 1; /* Positive status flags delegation */
1113 if ((type & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1114 switch (type & USB_RECIP_MASK) {
1115 case USB_RECIP_DEVICE:
1116 status = gr_device_request(dev, type, request,
1117 value, index);
1118 break;
1119 case USB_RECIP_ENDPOINT:
1120 status = gr_endpoint_request(dev, type, request,
1121 value, index);
1122 break;
1123 case USB_RECIP_INTERFACE:
1124 status = gr_interface_request(dev, type, request,
1125 value, index);
1126 break;
1127 }
1128 }
1129
1130 if (status > 0) {
1131 spin_unlock(&dev->lock);
1132
1133 dev_vdbg(dev->dev, "DELEGATE\n");
1134 status = dev->driver->setup(&dev->gadget, &u.ctrl);
1135
1136 spin_lock(&dev->lock);
1137 }
1138
1139 /* Generate STALL on both ep0out and ep0in if requested */
1140 if (unlikely(status < 0)) {
1141 dev_vdbg(dev->dev, "STALL\n");
1142 gr_control_stall(dev);
1143 }
1144
1145 if ((type & USB_TYPE_MASK) == USB_TYPE_STANDARD &&
1146 request == USB_REQ_SET_CONFIGURATION) {
1147 if (!value) {
1148 dev_dbg(dev->dev, "STATUS: deconfigured\n");
1149 usb_gadget_set_state(&dev->gadget, USB_STATE_ADDRESS);
1150 } else if (status >= 0) {
1151 /* Not configured unless gadget OK:s it */
1152 dev_dbg(dev->dev, "STATUS: configured: %d\n", value);
1153 usb_gadget_set_state(&dev->gadget,
1154 USB_STATE_CONFIGURED);
1155 }
1156 }
1157
1158 /* Get ready for next stage */
1159 if (dev->ep0state == GR_EP0_ODATA)
1160 gr_set_ep0state(dev, GR_EP0_OSTATUS);
1161 else if (dev->ep0state == GR_EP0_IDATA)
1162 gr_set_ep0state(dev, GR_EP0_ISTATUS);
1163 else
1164 gr_set_ep0state(dev, GR_EP0_SETUP);
1165
1166out:
1167 gr_ep0out_requeue(dev);
1168}
1169
1170/* ---------------------------------------------------------------------- */
1171/* VBUS and USB reset handling */
1172
1173/* Must be called with dev->lock held and irqs disabled */
1174static void gr_vbus_connected(struct gr_udc *dev, u32 status)
1175{
1176 u32 control;
1177
1178 dev->gadget.speed = GR_SPEED(status);
1179 usb_gadget_set_state(&dev->gadget, USB_STATE_POWERED);
1180
1181 /* Turn on full interrupts and pullup */
1182 control = (GR_CONTROL_SI | GR_CONTROL_UI | GR_CONTROL_VI |
1183 GR_CONTROL_SP | GR_CONTROL_EP);
1184 gr_write32(&dev->regs->control, control);
1185}
1186
1187/* Must be called with dev->lock held */
1188static void gr_enable_vbus_detect(struct gr_udc *dev)
1189{
1190 u32 status;
1191
1192 dev->irq_enabled = 1;
1193 wmb(); /* Make sure we do not ignore an interrupt */
1194 gr_write32(&dev->regs->control, GR_CONTROL_VI);
1195
1196 /* Take care of the case we are already plugged in at this point */
1197 status = gr_read32(&dev->regs->status);
1198 if (status & GR_STATUS_VB)
1199 gr_vbus_connected(dev, status);
1200}
1201
1202/* Must be called with dev->lock held and irqs disabled */
1203static void gr_vbus_disconnected(struct gr_udc *dev)
1204{
1205 gr_stop_activity(dev);
1206
1207 /* Report disconnect */
1208 if (dev->driver && dev->driver->disconnect) {
1209 spin_unlock(&dev->lock);
1210
1211 dev->driver->disconnect(&dev->gadget);
1212
1213 spin_lock(&dev->lock);
1214 }
1215
1216 gr_enable_vbus_detect(dev);
1217}
1218
1219/* Must be called with dev->lock held and irqs disabled */
1220static void gr_udc_usbreset(struct gr_udc *dev, u32 status)
1221{
1222 gr_set_address(dev, 0);
1223 gr_set_ep0state(dev, GR_EP0_SETUP);
1224 usb_gadget_set_state(&dev->gadget, USB_STATE_DEFAULT);
1225 dev->gadget.speed = GR_SPEED(status);
1226
1227 gr_ep_nuke(&dev->epo[0]);
1228 gr_ep_nuke(&dev->epi[0]);
1229 dev->epo[0].stopped = 0;
1230 dev->epi[0].stopped = 0;
1231 gr_ep0out_requeue(dev);
1232}
1233
1234/* ---------------------------------------------------------------------- */
1235/* Irq handling */
1236
1237/*
1238 * Handles interrupts from in endpoints. Returns whether something was handled.
1239 *
1240 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1241 */
1242static int gr_handle_in_ep(struct gr_ep *ep)
1243{
1244 struct gr_request *req;
1245
1246 req = list_first_entry(&ep->queue, struct gr_request, queue);
1247 if (!req->last_desc)
1248 return 0;
1249
1250 if (ACCESS_ONCE(req->last_desc->ctrl) & GR_DESC_IN_CTRL_EN)
1251 return 0; /* Not put in hardware buffers yet */
1252
1253 if (gr_read32(&ep->regs->epstat) & (GR_EPSTAT_B1 | GR_EPSTAT_B0))
1254 return 0; /* Not transmitted yet, still in hardware buffers */
1255
1256 /* Write complete */
1257 gr_dma_advance(ep, 0);
1258
1259 return 1;
1260}
1261
1262/*
1263 * Handles interrupts from out endpoints. Returns whether something was handled.
1264 *
1265 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1266 */
1267static int gr_handle_out_ep(struct gr_ep *ep)
1268{
1269 u32 ep_dmactrl;
1270 u32 ctrl;
1271 u16 len;
1272 struct gr_request *req;
1273 struct gr_udc *dev = ep->dev;
1274
1275 req = list_first_entry(&ep->queue, struct gr_request, queue);
1276 if (!req->curr_desc)
1277 return 0;
1278
1279 ctrl = ACCESS_ONCE(req->curr_desc->ctrl);
1280 if (ctrl & GR_DESC_OUT_CTRL_EN)
1281 return 0; /* Not received yet */
1282
1283 /* Read complete */
1284 len = ctrl & GR_DESC_OUT_CTRL_LEN_MASK;
1285 req->req.actual += len;
1286 if (ctrl & GR_DESC_OUT_CTRL_SE)
1287 req->setup = 1;
1288
1289 if (len < ep->ep.maxpacket || req->req.actual == req->req.length) {
1290 /* Short packet or the expected size - we are done */
1291
1292 if ((ep == &dev->epo[0]) && (dev->ep0state == GR_EP0_OSTATUS)) {
1293 /*
1294 * Send a status stage ZLP to ack the DATA stage in the
1295 * OUT direction. This needs to be done before
1296 * gr_dma_advance as that can lead to a call to
1297 * ep0_setup that can change dev->ep0state.
1298 */
1299 gr_ep0_respond_empty(dev);
1300 gr_set_ep0state(dev, GR_EP0_SETUP);
1301 }
1302
1303 gr_dma_advance(ep, 0);
1304 } else {
1305 /* Not done yet. Enable the next descriptor to receive more. */
1306 req->curr_desc = req->curr_desc->next_desc;
1307 req->curr_desc->ctrl |= GR_DESC_OUT_CTRL_EN;
1308
1309 ep_dmactrl = gr_read32(&ep->regs->dmactrl);
1310 gr_write32(&ep->regs->dmactrl, ep_dmactrl | GR_DMACTRL_DA);
1311 }
1312
1313 return 1;
1314}
1315
1316/*
1317 * Handle state changes. Returns whether something was handled.
1318 *
1319 * Must be called with dev->lock held and irqs disabled.
1320 */
1321static int gr_handle_state_changes(struct gr_udc *dev)
1322{
1323 u32 status = gr_read32(&dev->regs->status);
1324 int handled = 0;
1325 int powstate = !(dev->gadget.state == USB_STATE_NOTATTACHED ||
1326 dev->gadget.state == USB_STATE_ATTACHED);
1327
1328 /* VBUS valid detected */
1329 if (!powstate && (status & GR_STATUS_VB)) {
1330 dev_dbg(dev->dev, "STATUS: vbus valid detected\n");
1331 gr_vbus_connected(dev, status);
1332 handled = 1;
1333 }
1334
1335 /* Disconnect */
1336 if (powstate && !(status & GR_STATUS_VB)) {
1337 dev_dbg(dev->dev, "STATUS: vbus invalid detected\n");
1338 gr_vbus_disconnected(dev);
1339 handled = 1;
1340 }
1341
1342 /* USB reset detected */
1343 if (status & GR_STATUS_UR) {
1344 dev_dbg(dev->dev, "STATUS: USB reset - speed is %s\n",
1345 GR_SPEED_STR(status));
1346 gr_write32(&dev->regs->status, GR_STATUS_UR);
1347 gr_udc_usbreset(dev, status);
1348 handled = 1;
1349 }
1350
1351 /* Speed change */
1352 if (dev->gadget.speed != GR_SPEED(status)) {
1353 dev_dbg(dev->dev, "STATUS: USB Speed change to %s\n",
1354 GR_SPEED_STR(status));
1355 dev->gadget.speed = GR_SPEED(status);
1356 handled = 1;
1357 }
1358
1359 /* Going into suspend */
1360 if ((dev->ep0state != GR_EP0_SUSPEND) && !(status & GR_STATUS_SU)) {
1361 dev_dbg(dev->dev, "STATUS: USB suspend\n");
1362 gr_set_ep0state(dev, GR_EP0_SUSPEND);
1363 dev->suspended_from = dev->gadget.state;
1364 usb_gadget_set_state(&dev->gadget, USB_STATE_SUSPENDED);
1365
1366 if ((dev->gadget.speed != USB_SPEED_UNKNOWN) &&
1367 dev->driver && dev->driver->suspend) {
1368 spin_unlock(&dev->lock);
1369
1370 dev->driver->suspend(&dev->gadget);
1371
1372 spin_lock(&dev->lock);
1373 }
1374 handled = 1;
1375 }
1376
1377 /* Coming out of suspend */
1378 if ((dev->ep0state == GR_EP0_SUSPEND) && (status & GR_STATUS_SU)) {
1379 dev_dbg(dev->dev, "STATUS: USB resume\n");
1380 if (dev->suspended_from == USB_STATE_POWERED)
1381 gr_set_ep0state(dev, GR_EP0_DISCONNECT);
1382 else
1383 gr_set_ep0state(dev, GR_EP0_SETUP);
1384 usb_gadget_set_state(&dev->gadget, dev->suspended_from);
1385
1386 if ((dev->gadget.speed != USB_SPEED_UNKNOWN) &&
1387 dev->driver && dev->driver->resume) {
1388 spin_unlock(&dev->lock);
1389
1390 dev->driver->resume(&dev->gadget);
1391
1392 spin_lock(&dev->lock);
1393 }
1394 handled = 1;
1395 }
1396
1397 return handled;
1398}
1399
1400/* Non-interrupt context irq handler */
1401static irqreturn_t gr_irq_handler(int irq, void *_dev)
1402{
1403 struct gr_udc *dev = _dev;
1404 struct gr_ep *ep;
1405 int handled = 0;
1406 int i;
1407 unsigned long flags;
1408
1409 spin_lock_irqsave(&dev->lock, flags);
1410
1411 if (!dev->irq_enabled)
1412 goto out;
1413
1414 /*
1415 * Check IN ep interrupts. We check these before the OUT eps because
1416 * some gadgets reuse the request that might already be currently
1417 * outstanding and needs to be completed (mainly setup requests).
1418 */
1419 for (i = 0; i < dev->nepi; i++) {
1420 ep = &dev->epi[i];
1421 if (!ep->stopped && !ep->callback && !list_empty(&ep->queue))
1422 handled = gr_handle_in_ep(ep) || handled;
1423 }
1424
1425 /* Check OUT ep interrupts */
1426 for (i = 0; i < dev->nepo; i++) {
1427 ep = &dev->epo[i];
1428 if (!ep->stopped && !ep->callback && !list_empty(&ep->queue))
1429 handled = gr_handle_out_ep(ep) || handled;
1430 }
1431
1432 /* Check status interrupts */
1433 handled = gr_handle_state_changes(dev) || handled;
1434
1435 /*
1436 * Check AMBA DMA errors. Only check if we didn't find anything else to
1437 * handle because this shouldn't happen if we did everything right.
1438 */
1439 if (!handled) {
1440 list_for_each_entry(ep, &dev->ep_list, ep_list) {
1441 if (gr_read32(&ep->regs->dmactrl) & GR_DMACTRL_AE) {
1442 dev_err(dev->dev,
1443 "AMBA Error occurred for %s\n",
1444 ep->ep.name);
1445 handled = 1;
1446 }
1447 }
1448 }
1449
1450out:
1451 spin_unlock_irqrestore(&dev->lock, flags);
1452
1453 return handled ? IRQ_HANDLED : IRQ_NONE;
1454}
1455
1456/* Interrupt context irq handler */
1457static irqreturn_t gr_irq(int irq, void *_dev)
1458{
1459 struct gr_udc *dev = _dev;
1460
1461 if (!dev->irq_enabled)
1462 return IRQ_NONE;
1463
1464 return IRQ_WAKE_THREAD;
1465}
1466
1467/* ---------------------------------------------------------------------- */
1468/* USB ep ops */
1469
1470/* Enable endpoint. Not for ep0in and ep0out that are handled separately. */
1471static int gr_ep_enable(struct usb_ep *_ep,
1472 const struct usb_endpoint_descriptor *desc)
1473{
1474 struct gr_udc *dev;
1475 struct gr_ep *ep;
1476 u8 mode;
1477 u8 nt;
1478 u16 max;
1479 u16 buffer_size = 0;
1480 u32 epctrl;
1481
1482 ep = container_of(_ep, struct gr_ep, ep);
1483 if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT)
1484 return -EINVAL;
1485
1486 dev = ep->dev;
1487
1488 /* 'ep0' IN and OUT are reserved */
1489 if (ep == &dev->epo[0] || ep == &dev->epi[0])
1490 return -EINVAL;
1491
1492 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
1493 return -ESHUTDOWN;
1494
1495 /* Make sure we are clear for enabling */
1496 epctrl = gr_read32(&ep->regs->epctrl);
1497 if (epctrl & GR_EPCTRL_EV)
1498 return -EBUSY;
1499
1500 /* Check that directions match */
1501 if (!ep->is_in != !usb_endpoint_dir_in(desc))
1502 return -EINVAL;
1503
1504 /* Check ep num */
1505 if ((!ep->is_in && ep->num >= dev->nepo) ||
1506 (ep->is_in && ep->num >= dev->nepi))
1507 return -EINVAL;
1508
1509 if (usb_endpoint_xfer_control(desc)) {
1510 mode = 0;
1511 } else if (usb_endpoint_xfer_isoc(desc)) {
1512 mode = 1;
1513 } else if (usb_endpoint_xfer_bulk(desc)) {
1514 mode = 2;
1515 } else if (usb_endpoint_xfer_int(desc)) {
1516 mode = 3;
1517 } else {
1518 dev_err(dev->dev, "Unknown transfer type for %s\n",
1519 ep->ep.name);
1520 return -EINVAL;
1521 }
1522
1523 /*
1524 * Bits 10-0 set the max payload. 12-11 set the number of
1525 * additional transactions.
1526 */
1527 max = 0x7ff & usb_endpoint_maxp(desc);
1528 nt = 0x3 & (usb_endpoint_maxp(desc) >> 11);
1529 buffer_size = GR_BUFFER_SIZE(epctrl);
1530 if (nt && (mode == 0 || mode == 2)) {
1531 dev_err(dev->dev,
1532 "%s mode: multiple trans./microframe not valid\n",
1533 (mode == 2 ? "Bulk" : "Control"));
1534 return -EINVAL;
1535 } else if (nt == 0x11) {
1536 dev_err(dev->dev, "Invalid value for trans./microframe\n");
1537 return -EINVAL;
1538 } else if ((nt + 1) * max > buffer_size) {
1539 dev_err(dev->dev, "Hw buffer size %d < max payload %d * %d\n",
1540 buffer_size, (nt + 1), max);
1541 return -EINVAL;
1542 } else if (max == 0) {
1543 dev_err(dev->dev, "Max payload cannot be set to 0\n");
1544 return -EINVAL;
Andreas Larssonb38d27e2014-03-27 16:15:58 +01001545 } else if (max > ep->ep.maxpacket_limit) {
1546 dev_err(dev->dev, "Requested max payload %d > limit %d\n",
1547 max, ep->ep.maxpacket_limit);
1548 return -EINVAL;
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01001549 }
1550
1551 spin_lock(&ep->dev->lock);
1552
1553 if (!ep->stopped) {
1554 spin_unlock(&ep->dev->lock);
1555 return -EBUSY;
1556 }
1557
1558 ep->stopped = 0;
1559 ep->wedged = 0;
1560 ep->ep.desc = desc;
1561 ep->ep.maxpacket = max;
1562 ep->dma_start = 0;
1563
1564
1565 if (nt) {
1566 /*
1567 * Maximum possible size of all payloads in one microframe
1568 * regardless of direction when using high-bandwidth mode.
1569 */
1570 ep->bytes_per_buffer = (nt + 1) * max;
1571 } else if (ep->is_in) {
1572 /*
1573 * The biggest multiple of maximum packet size that fits into
1574 * the buffer. The hardware will split up into many packets in
1575 * the IN direction.
1576 */
1577 ep->bytes_per_buffer = (buffer_size / max) * max;
1578 } else {
1579 /*
1580 * Only single packets will be placed the buffers in the OUT
1581 * direction.
1582 */
1583 ep->bytes_per_buffer = max;
1584 }
1585
1586 epctrl = (max << GR_EPCTRL_MAXPL_POS)
1587 | (nt << GR_EPCTRL_NT_POS)
1588 | (mode << GR_EPCTRL_TT_POS)
1589 | GR_EPCTRL_EV;
1590 if (ep->is_in)
1591 epctrl |= GR_EPCTRL_PI;
1592 gr_write32(&ep->regs->epctrl, epctrl);
1593
1594 gr_write32(&ep->regs->dmactrl, GR_DMACTRL_IE | GR_DMACTRL_AI);
1595
1596 spin_unlock(&ep->dev->lock);
1597
1598 dev_dbg(ep->dev->dev, "EP: %s enabled - %s with %d bytes/buffer\n",
1599 ep->ep.name, gr_modestring[mode], ep->bytes_per_buffer);
1600 return 0;
1601}
1602
1603/* Disable endpoint. Not for ep0in and ep0out that are handled separately. */
1604static int gr_ep_disable(struct usb_ep *_ep)
1605{
1606 struct gr_ep *ep;
1607 struct gr_udc *dev;
1608 unsigned long flags;
1609
1610 ep = container_of(_ep, struct gr_ep, ep);
1611 if (!_ep || !ep->ep.desc)
1612 return -ENODEV;
1613
1614 dev = ep->dev;
1615
1616 /* 'ep0' IN and OUT are reserved */
1617 if (ep == &dev->epo[0] || ep == &dev->epi[0])
1618 return -EINVAL;
1619
1620 if (dev->ep0state == GR_EP0_SUSPEND)
1621 return -EBUSY;
1622
1623 dev_dbg(ep->dev->dev, "EP: disable %s\n", ep->ep.name);
1624
1625 spin_lock_irqsave(&dev->lock, flags);
1626
1627 gr_ep_nuke(ep);
1628 gr_ep_reset(ep);
1629 ep->ep.desc = NULL;
1630
1631 spin_unlock_irqrestore(&dev->lock, flags);
1632
1633 return 0;
1634}
1635
1636/*
1637 * Frees a request, but not any DMA buffers associated with it
1638 * (gr_finish_request should already have taken care of that).
1639 */
1640static void gr_free_request(struct usb_ep *_ep, struct usb_request *_req)
1641{
1642 struct gr_request *req;
1643
1644 if (!_ep || !_req)
1645 return;
1646 req = container_of(_req, struct gr_request, req);
1647
1648 /* Leads to memory leak */
1649 WARN(!list_empty(&req->queue),
1650 "request not dequeued properly before freeing\n");
1651
1652 kfree(req);
1653}
1654
1655/* Queue a request from the gadget */
1656static int gr_queue_ext(struct usb_ep *_ep, struct usb_request *_req,
1657 gfp_t gfp_flags)
1658{
1659 struct gr_ep *ep;
1660 struct gr_request *req;
1661 struct gr_udc *dev;
1662 int ret;
1663
1664 if (unlikely(!_ep || !_req))
1665 return -EINVAL;
1666
1667 ep = container_of(_ep, struct gr_ep, ep);
1668 req = container_of(_req, struct gr_request, req);
1669 dev = ep->dev;
1670
1671 spin_lock(&ep->dev->lock);
1672
1673 /*
1674 * The ep0 pointer in the gadget struct is used both for ep0in and
1675 * ep0out. In a data stage in the out direction ep0out needs to be used
1676 * instead of the default ep0in. Completion functions might use
1677 * driver_data, so that needs to be copied as well.
1678 */
1679 if ((ep == &dev->epi[0]) && (dev->ep0state == GR_EP0_ODATA)) {
1680 ep = &dev->epo[0];
1681 ep->ep.driver_data = dev->epi[0].ep.driver_data;
1682 }
1683
1684 if (ep->is_in)
1685 gr_dbgprint_request("EXTERN", ep, req);
1686
Alexey Khoroshilov966036f2014-05-08 00:26:52 +04001687 ret = gr_queue(ep, req, GFP_ATOMIC);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01001688
1689 spin_unlock(&ep->dev->lock);
1690
1691 return ret;
1692}
1693
1694/* Dequeue JUST ONE request */
1695static int gr_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1696{
1697 struct gr_request *req;
1698 struct gr_ep *ep;
1699 struct gr_udc *dev;
1700 int ret = 0;
1701 unsigned long flags;
1702
1703 ep = container_of(_ep, struct gr_ep, ep);
1704 if (!_ep || !_req || (!ep->ep.desc && ep->num != 0))
1705 return -EINVAL;
1706 dev = ep->dev;
1707 if (!dev->driver)
1708 return -ESHUTDOWN;
1709
1710 /* We can't touch (DMA) registers when suspended */
1711 if (dev->ep0state == GR_EP0_SUSPEND)
1712 return -EBUSY;
1713
1714 spin_lock_irqsave(&dev->lock, flags);
1715
1716 /* Make sure it's actually queued on this endpoint */
1717 list_for_each_entry(req, &ep->queue, queue) {
1718 if (&req->req == _req)
1719 break;
1720 }
1721 if (&req->req != _req) {
1722 ret = -EINVAL;
1723 goto out;
1724 }
1725
1726 if (list_first_entry(&ep->queue, struct gr_request, queue) == req) {
1727 /* This request is currently being processed */
1728 gr_abort_dma(ep);
1729 if (ep->stopped)
1730 gr_finish_request(ep, req, -ECONNRESET);
1731 else
1732 gr_dma_advance(ep, -ECONNRESET);
1733 } else if (!list_empty(&req->queue)) {
1734 /* Not being processed - gr_finish_request dequeues it */
1735 gr_finish_request(ep, req, -ECONNRESET);
1736 } else {
1737 ret = -EOPNOTSUPP;
1738 }
1739
1740out:
1741 spin_unlock_irqrestore(&dev->lock, flags);
1742
1743 return ret;
1744}
1745
1746/* Helper for gr_set_halt and gr_set_wedge */
1747static int gr_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge)
1748{
1749 int ret;
1750 struct gr_ep *ep;
1751
1752 if (!_ep)
1753 return -ENODEV;
1754 ep = container_of(_ep, struct gr_ep, ep);
1755
1756 spin_lock(&ep->dev->lock);
1757
1758 /* Halting an IN endpoint should fail if queue is not empty */
1759 if (halt && ep->is_in && !list_empty(&ep->queue)) {
1760 ret = -EAGAIN;
1761 goto out;
1762 }
1763
1764 ret = gr_ep_halt_wedge(ep, halt, wedge, 0);
1765
1766out:
1767 spin_unlock(&ep->dev->lock);
1768
1769 return ret;
1770}
1771
1772/* Halt endpoint */
1773static int gr_set_halt(struct usb_ep *_ep, int halt)
1774{
1775 return gr_set_halt_wedge(_ep, halt, 0);
1776}
1777
1778/* Halt and wedge endpoint */
1779static int gr_set_wedge(struct usb_ep *_ep)
1780{
1781 return gr_set_halt_wedge(_ep, 1, 1);
1782}
1783
1784/*
1785 * Return the total number of bytes currently stored in the internal buffers of
1786 * the endpoint.
1787 */
1788static int gr_fifo_status(struct usb_ep *_ep)
1789{
1790 struct gr_ep *ep;
1791 u32 epstat;
1792 u32 bytes = 0;
1793
1794 if (!_ep)
1795 return -ENODEV;
1796 ep = container_of(_ep, struct gr_ep, ep);
1797
1798 epstat = gr_read32(&ep->regs->epstat);
1799
1800 if (epstat & GR_EPSTAT_B0)
1801 bytes += (epstat & GR_EPSTAT_B0CNT_MASK) >> GR_EPSTAT_B0CNT_POS;
1802 if (epstat & GR_EPSTAT_B1)
1803 bytes += (epstat & GR_EPSTAT_B1CNT_MASK) >> GR_EPSTAT_B1CNT_POS;
1804
1805 return bytes;
1806}
1807
1808
1809/* Empty data from internal buffers of an endpoint. */
1810static void gr_fifo_flush(struct usb_ep *_ep)
1811{
1812 struct gr_ep *ep;
1813 u32 epctrl;
1814
1815 if (!_ep)
1816 return;
1817 ep = container_of(_ep, struct gr_ep, ep);
1818 dev_vdbg(ep->dev->dev, "EP: flush fifo %s\n", ep->ep.name);
1819
1820 spin_lock(&ep->dev->lock);
1821
1822 epctrl = gr_read32(&ep->regs->epctrl);
1823 epctrl |= GR_EPCTRL_CB;
1824 gr_write32(&ep->regs->epctrl, epctrl);
1825
1826 spin_unlock(&ep->dev->lock);
1827}
1828
1829static struct usb_ep_ops gr_ep_ops = {
1830 .enable = gr_ep_enable,
1831 .disable = gr_ep_disable,
1832
1833 .alloc_request = gr_alloc_request,
1834 .free_request = gr_free_request,
1835
1836 .queue = gr_queue_ext,
1837 .dequeue = gr_dequeue,
1838
1839 .set_halt = gr_set_halt,
1840 .set_wedge = gr_set_wedge,
1841 .fifo_status = gr_fifo_status,
1842 .fifo_flush = gr_fifo_flush,
1843};
1844
1845/* ---------------------------------------------------------------------- */
1846/* USB Gadget ops */
1847
1848static int gr_get_frame(struct usb_gadget *_gadget)
1849{
1850 struct gr_udc *dev;
1851
1852 if (!_gadget)
1853 return -ENODEV;
1854 dev = container_of(_gadget, struct gr_udc, gadget);
1855 return gr_read32(&dev->regs->status) & GR_STATUS_FN_MASK;
1856}
1857
1858static int gr_wakeup(struct usb_gadget *_gadget)
1859{
1860 struct gr_udc *dev;
1861
1862 if (!_gadget)
1863 return -ENODEV;
1864 dev = container_of(_gadget, struct gr_udc, gadget);
1865
1866 /* Remote wakeup feature not enabled by host*/
1867 if (!dev->remote_wakeup)
1868 return -EINVAL;
1869
1870 spin_lock(&dev->lock);
1871
1872 gr_write32(&dev->regs->control,
1873 gr_read32(&dev->regs->control) | GR_CONTROL_RW);
1874
1875 spin_unlock(&dev->lock);
1876
1877 return 0;
1878}
1879
1880static int gr_pullup(struct usb_gadget *_gadget, int is_on)
1881{
1882 struct gr_udc *dev;
1883 u32 control;
1884
1885 if (!_gadget)
1886 return -ENODEV;
1887 dev = container_of(_gadget, struct gr_udc, gadget);
1888
1889 spin_lock(&dev->lock);
1890
1891 control = gr_read32(&dev->regs->control);
1892 if (is_on)
1893 control |= GR_CONTROL_EP;
1894 else
1895 control &= ~GR_CONTROL_EP;
1896 gr_write32(&dev->regs->control, control);
1897
1898 spin_unlock(&dev->lock);
1899
1900 return 0;
1901}
1902
1903static int gr_udc_start(struct usb_gadget *gadget,
1904 struct usb_gadget_driver *driver)
1905{
1906 struct gr_udc *dev = to_gr_udc(gadget);
1907
1908 spin_lock(&dev->lock);
1909
1910 /* Hook up the driver */
1911 driver->driver.bus = NULL;
1912 dev->driver = driver;
1913
1914 /* Get ready for host detection */
1915 gr_enable_vbus_detect(dev);
1916
1917 spin_unlock(&dev->lock);
1918
1919 dev_info(dev->dev, "Started with gadget driver '%s'\n",
1920 driver->driver.name);
1921
1922 return 0;
1923}
1924
1925static int gr_udc_stop(struct usb_gadget *gadget,
1926 struct usb_gadget_driver *driver)
1927{
1928 struct gr_udc *dev = to_gr_udc(gadget);
1929 unsigned long flags;
1930
1931 spin_lock_irqsave(&dev->lock, flags);
1932
1933 dev->driver = NULL;
1934 gr_stop_activity(dev);
1935
1936 spin_unlock_irqrestore(&dev->lock, flags);
1937
1938 dev_info(dev->dev, "Stopped\n");
1939
1940 return 0;
1941}
1942
1943static const struct usb_gadget_ops gr_ops = {
1944 .get_frame = gr_get_frame,
1945 .wakeup = gr_wakeup,
1946 .pullup = gr_pullup,
1947 .udc_start = gr_udc_start,
1948 .udc_stop = gr_udc_stop,
1949 /* Other operations not supported */
1950};
1951
1952/* ---------------------------------------------------------------------- */
1953/* Module probe, removal and of-matching */
1954
1955static const char * const onames[] = {
1956 "ep0out", "ep1out", "ep2out", "ep3out", "ep4out", "ep5out",
1957 "ep6out", "ep7out", "ep8out", "ep9out", "ep10out", "ep11out",
1958 "ep12out", "ep13out", "ep14out", "ep15out"
1959};
1960
1961static const char * const inames[] = {
1962 "ep0in", "ep1in", "ep2in", "ep3in", "ep4in", "ep5in",
1963 "ep6in", "ep7in", "ep8in", "ep9in", "ep10in", "ep11in",
1964 "ep12in", "ep13in", "ep14in", "ep15in"
1965};
1966
1967/* Must be called with dev->lock held */
1968static int gr_ep_init(struct gr_udc *dev, int num, int is_in, u32 maxplimit)
1969{
1970 struct gr_ep *ep;
1971 struct gr_request *req;
1972 struct usb_request *_req;
1973 void *buf;
1974
1975 if (is_in) {
1976 ep = &dev->epi[num];
1977 ep->ep.name = inames[num];
1978 ep->regs = &dev->regs->epi[num];
1979 } else {
1980 ep = &dev->epo[num];
1981 ep->ep.name = onames[num];
1982 ep->regs = &dev->regs->epo[num];
1983 }
1984
1985 gr_ep_reset(ep);
1986 ep->num = num;
1987 ep->is_in = is_in;
1988 ep->dev = dev;
1989 ep->ep.ops = &gr_ep_ops;
1990 INIT_LIST_HEAD(&ep->queue);
1991
1992 if (num == 0) {
Andreas Larsson8652bcb2014-04-01 12:15:17 +02001993 _req = gr_alloc_request(&ep->ep, GFP_ATOMIC);
1994 buf = devm_kzalloc(dev->dev, PAGE_SIZE, GFP_DMA | GFP_ATOMIC);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01001995 if (!_req || !buf) {
1996 /* possible _req freed by gr_probe via gr_remove */
1997 return -ENOMEM;
1998 }
1999
2000 req = container_of(_req, struct gr_request, req);
2001 req->req.buf = buf;
2002 req->req.length = MAX_CTRL_PL_SIZE;
2003
2004 if (is_in)
2005 dev->ep0reqi = req; /* Complete gets set as used */
2006 else
2007 dev->ep0reqo = req; /* Completion treated separately */
2008
2009 usb_ep_set_maxpacket_limit(&ep->ep, MAX_CTRL_PL_SIZE);
2010 ep->bytes_per_buffer = MAX_CTRL_PL_SIZE;
2011 } else {
2012 usb_ep_set_maxpacket_limit(&ep->ep, (u16)maxplimit);
2013 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
2014 }
2015 list_add_tail(&ep->ep_list, &dev->ep_list);
2016
2017 return 0;
2018}
2019
2020/* Must be called with dev->lock held */
2021static int gr_udc_init(struct gr_udc *dev)
2022{
2023 struct device_node *np = dev->dev->of_node;
2024 u32 epctrl_val;
2025 u32 dmactrl_val;
2026 int i;
2027 int ret = 0;
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002028 u32 bufsize;
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002029
2030 gr_set_address(dev, 0);
2031
2032 INIT_LIST_HEAD(&dev->gadget.ep_list);
2033 dev->gadget.speed = USB_SPEED_UNKNOWN;
2034 dev->gadget.ep0 = &dev->epi[0].ep;
2035
2036 INIT_LIST_HEAD(&dev->ep_list);
2037 gr_set_ep0state(dev, GR_EP0_DISCONNECT);
2038
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002039 for (i = 0; i < dev->nepo; i++) {
Andreas Larsson73e1c092014-03-27 16:15:56 +01002040 if (of_property_read_u32_index(np, "epobufsizes", i, &bufsize))
2041 bufsize = 1024;
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002042 ret = gr_ep_init(dev, i, 0, bufsize);
2043 if (ret)
2044 return ret;
2045 }
2046
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002047 for (i = 0; i < dev->nepi; i++) {
Andreas Larsson73e1c092014-03-27 16:15:56 +01002048 if (of_property_read_u32_index(np, "epibufsizes", i, &bufsize))
2049 bufsize = 1024;
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002050 ret = gr_ep_init(dev, i, 1, bufsize);
2051 if (ret)
2052 return ret;
2053 }
2054
2055 /* Must be disabled by default */
2056 dev->remote_wakeup = 0;
2057
2058 /* Enable ep0out and ep0in */
2059 epctrl_val = (MAX_CTRL_PL_SIZE << GR_EPCTRL_MAXPL_POS) | GR_EPCTRL_EV;
2060 dmactrl_val = GR_DMACTRL_IE | GR_DMACTRL_AI;
2061 gr_write32(&dev->epo[0].regs->epctrl, epctrl_val);
2062 gr_write32(&dev->epi[0].regs->epctrl, epctrl_val | GR_EPCTRL_PI);
2063 gr_write32(&dev->epo[0].regs->dmactrl, dmactrl_val);
2064 gr_write32(&dev->epi[0].regs->dmactrl, dmactrl_val);
2065
2066 return 0;
2067}
2068
Andreas Larsson22876262014-03-27 16:15:53 +01002069static int gr_remove(struct platform_device *pdev)
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002070{
Andreas Larsson22876262014-03-27 16:15:53 +01002071 struct gr_udc *dev = platform_get_drvdata(pdev);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002072
2073 if (dev->added)
2074 usb_del_gadget_udc(&dev->gadget); /* Shuts everything down */
2075 if (dev->driver)
2076 return -EBUSY;
2077
2078 gr_dfs_delete(dev);
2079 if (dev->desc_pool)
2080 dma_pool_destroy(dev->desc_pool);
Andreas Larsson22876262014-03-27 16:15:53 +01002081 platform_set_drvdata(pdev, NULL);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002082
2083 gr_free_request(&dev->epi[0].ep, &dev->ep0reqi->req);
2084 gr_free_request(&dev->epo[0].ep, &dev->ep0reqo->req);
2085
2086 return 0;
2087}
2088static int gr_request_irq(struct gr_udc *dev, int irq)
2089{
2090 return devm_request_threaded_irq(dev->dev, irq, gr_irq, gr_irq_handler,
2091 IRQF_SHARED, driver_name, dev);
2092}
2093
Andreas Larsson22876262014-03-27 16:15:53 +01002094static int gr_probe(struct platform_device *pdev)
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002095{
2096 struct gr_udc *dev;
2097 struct resource *res;
2098 struct gr_regs __iomem *regs;
2099 int retval;
2100 u32 status;
2101
Andreas Larsson22876262014-03-27 16:15:53 +01002102 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002103 if (!dev)
2104 return -ENOMEM;
Andreas Larsson22876262014-03-27 16:15:53 +01002105 dev->dev = &pdev->dev;
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002106
Andreas Larsson22876262014-03-27 16:15:53 +01002107 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002108 regs = devm_ioremap_resource(dev->dev, res);
2109 if (IS_ERR(regs))
2110 return PTR_ERR(regs);
2111
Andreas Larsson196800d2014-03-27 16:15:55 +01002112 dev->irq = platform_get_irq(pdev, 0);
2113 if (dev->irq <= 0) {
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002114 dev_err(dev->dev, "No irq found\n");
2115 return -ENODEV;
2116 }
2117
2118 /* Some core configurations has separate irqs for IN and OUT events */
Andreas Larsson196800d2014-03-27 16:15:55 +01002119 dev->irqi = platform_get_irq(pdev, 1);
2120 if (dev->irqi > 0) {
2121 dev->irqo = platform_get_irq(pdev, 2);
2122 if (dev->irqo <= 0) {
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002123 dev_err(dev->dev, "Found irqi but not irqo\n");
2124 return -ENODEV;
2125 }
Andreas Larsson196800d2014-03-27 16:15:55 +01002126 } else {
2127 dev->irqi = 0;
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002128 }
2129
2130 dev->gadget.name = driver_name;
2131 dev->gadget.max_speed = USB_SPEED_HIGH;
2132 dev->gadget.ops = &gr_ops;
2133 dev->gadget.quirk_ep_out_aligned_size = true;
2134
2135 spin_lock_init(&dev->lock);
2136 dev->regs = regs;
2137
Andreas Larsson22876262014-03-27 16:15:53 +01002138 platform_set_drvdata(pdev, dev);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002139
2140 /* Determine number of endpoints and data interface mode */
2141 status = gr_read32(&dev->regs->status);
2142 dev->nepi = ((status & GR_STATUS_NEPI_MASK) >> GR_STATUS_NEPI_POS) + 1;
2143 dev->nepo = ((status & GR_STATUS_NEPO_MASK) >> GR_STATUS_NEPO_POS) + 1;
2144
2145 if (!(status & GR_STATUS_DM)) {
2146 dev_err(dev->dev, "Slave mode cores are not supported\n");
2147 return -ENODEV;
2148 }
2149
2150 /* --- Effects of the following calls might need explicit cleanup --- */
2151
2152 /* Create DMA pool for descriptors */
2153 dev->desc_pool = dma_pool_create("desc_pool", dev->dev,
2154 sizeof(struct gr_dma_desc), 4, 0);
2155 if (!dev->desc_pool) {
2156 dev_err(dev->dev, "Could not allocate DMA pool");
2157 return -ENOMEM;
2158 }
2159
2160 spin_lock(&dev->lock);
2161
2162 /* Inside lock so that no gadget can use this udc until probe is done */
2163 retval = usb_add_gadget_udc(dev->dev, &dev->gadget);
2164 if (retval) {
2165 dev_err(dev->dev, "Could not add gadget udc");
2166 goto out;
2167 }
2168 dev->added = 1;
2169
2170 retval = gr_udc_init(dev);
2171 if (retval)
2172 goto out;
2173
2174 gr_dfs_create(dev);
2175
2176 /* Clear all interrupt enables that might be left on since last boot */
2177 gr_disable_interrupts_and_pullup(dev);
2178
2179 retval = gr_request_irq(dev, dev->irq);
2180 if (retval) {
2181 dev_err(dev->dev, "Failed to request irq %d\n", dev->irq);
2182 goto out;
2183 }
2184
2185 if (dev->irqi) {
2186 retval = gr_request_irq(dev, dev->irqi);
2187 if (retval) {
2188 dev_err(dev->dev, "Failed to request irqi %d\n",
2189 dev->irqi);
2190 goto out;
2191 }
2192 retval = gr_request_irq(dev, dev->irqo);
2193 if (retval) {
2194 dev_err(dev->dev, "Failed to request irqo %d\n",
2195 dev->irqo);
2196 goto out;
2197 }
2198 }
2199
2200 if (dev->irqi)
2201 dev_info(dev->dev, "regs: %p, irqs %d, %d, %d\n", dev->regs,
2202 dev->irq, dev->irqi, dev->irqo);
2203 else
2204 dev_info(dev->dev, "regs: %p, irq %d\n", dev->regs, dev->irq);
2205
2206out:
2207 spin_unlock(&dev->lock);
2208
2209 if (retval)
Andreas Larsson22876262014-03-27 16:15:53 +01002210 gr_remove(pdev);
Andreas Larsson27e9dcc2013-12-23 21:25:49 +01002211
2212 return retval;
2213}
2214
2215static struct of_device_id gr_match[] = {
2216 {.name = "GAISLER_USBDC"},
2217 {.name = "01_021"},
2218 {},
2219};
2220MODULE_DEVICE_TABLE(of, gr_match);
2221
2222static struct platform_driver gr_driver = {
2223 .driver = {
2224 .name = DRIVER_NAME,
2225 .owner = THIS_MODULE,
2226 .of_match_table = gr_match,
2227 },
2228 .probe = gr_probe,
2229 .remove = gr_remove,
2230};
2231module_platform_driver(gr_driver);
2232
2233MODULE_AUTHOR("Aeroflex Gaisler AB.");
2234MODULE_DESCRIPTION(DRIVER_DESC);
2235MODULE_LICENSE("GPL");