1 // SPDX-License-Identifier: GPL-2.0+
3 * USB Peripheral Controller driver for Aeroflex Gaisler GRUSBDC.
5 * 2013 (c) Aeroflex Gaisler AB
7 * This driver supports GRUSBDC USB Device Controller cores available in the
8 * GRLIB VHDL IP core library.
10 * Full documentation of the GRUSBDC core can be found here:
11 * http://www.gaisler.com/products/grlib/grip.pdf
14 * - Andreas Larsson <andreas@gaisler.com>
19 * A GRUSBDC core can have up to 16 IN endpoints and 16 OUT endpoints each
20 * individually configurable to any of the four USB transfer types. This driver
21 * only supports cores in DMA mode.
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/errno.h>
29 #include <linux/list.h>
30 #include <linux/interrupt.h>
31 #include <linux/device.h>
32 #include <linux/usb.h>
33 #include <linux/usb/ch9.h>
34 #include <linux/usb/gadget.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/dmapool.h>
37 #include <linux/debugfs.h>
38 #include <linux/seq_file.h>
39 #include <linux/of_platform.h>
40 #include <linux/of_irq.h>
41 #include <linux/of_address.h>
43 #include <asm/byteorder.h>
47 #define DRIVER_NAME "gr_udc"
48 #define DRIVER_DESC "Aeroflex Gaisler GRUSBDC USB Peripheral Controller"
50 static const char driver_name
[] = DRIVER_NAME
;
51 static const char driver_desc
[] = DRIVER_DESC
;
53 #define gr_read32(x) (ioread32be((x)))
54 #define gr_write32(x, v) (iowrite32be((v), (x)))
56 /* USB speed and corresponding string calculated from status register value */
57 #define GR_SPEED(status) \
58 ((status & GR_STATUS_SP) ? USB_SPEED_FULL : USB_SPEED_HIGH)
59 #define GR_SPEED_STR(status) usb_speed_string(GR_SPEED(status))
61 /* Size of hardware buffer calculated from epctrl register value */
62 #define GR_BUFFER_SIZE(epctrl) \
63 ((((epctrl) & GR_EPCTRL_BUFSZ_MASK) >> GR_EPCTRL_BUFSZ_POS) * \
64 GR_EPCTRL_BUFSZ_SCALER)
66 /* ---------------------------------------------------------------------- */
67 /* Debug printout functionality */
69 static const char * const gr_modestring
[] = {"control", "iso", "bulk", "int"};
71 static const char *gr_ep0state_string(enum gr_ep0state state
)
73 static const char *const names
[] = {
74 [GR_EP0_DISCONNECT
] = "disconnect",
75 [GR_EP0_SETUP
] = "setup",
76 [GR_EP0_IDATA
] = "idata",
77 [GR_EP0_ODATA
] = "odata",
78 [GR_EP0_ISTATUS
] = "istatus",
79 [GR_EP0_OSTATUS
] = "ostatus",
80 [GR_EP0_STALL
] = "stall",
81 [GR_EP0_SUSPEND
] = "suspend",
84 if (state
< 0 || state
>= ARRAY_SIZE(names
))
92 static void gr_dbgprint_request(const char *str
, struct gr_ep
*ep
,
93 struct gr_request
*req
)
95 int buflen
= ep
->is_in
? req
->req
.length
: req
->req
.actual
;
97 int plen
= min(rowlen
, buflen
);
99 dev_dbg(ep
->dev
->dev
, "%s: 0x%p, %d bytes data%s:\n", str
, req
, buflen
,
100 (buflen
> plen
? " (truncated)" : ""));
101 print_hex_dump_debug(" ", DUMP_PREFIX_NONE
,
102 rowlen
, 4, req
->req
.buf
, plen
, false);
105 static void gr_dbgprint_devreq(struct gr_udc
*dev
, u8 type
, u8 request
,
106 u16 value
, u16 index
, u16 length
)
108 dev_vdbg(dev
->dev
, "REQ: %02x.%02x v%04x i%04x l%04x\n",
109 type
, request
, value
, index
, length
);
111 #else /* !VERBOSE_DEBUG */
113 static void gr_dbgprint_request(const char *str
, struct gr_ep
*ep
,
114 struct gr_request
*req
) {}
116 static void gr_dbgprint_devreq(struct gr_udc
*dev
, u8 type
, u8 request
,
117 u16 value
, u16 index
, u16 length
) {}
119 #endif /* VERBOSE_DEBUG */
121 /* ---------------------------------------------------------------------- */
122 /* Debugfs functionality */
124 #ifdef CONFIG_USB_GADGET_DEBUG_FS
126 static void gr_seq_ep_show(struct seq_file
*seq
, struct gr_ep
*ep
)
128 u32 epctrl
= gr_read32(&ep
->regs
->epctrl
);
129 u32 epstat
= gr_read32(&ep
->regs
->epstat
);
130 int mode
= (epctrl
& GR_EPCTRL_TT_MASK
) >> GR_EPCTRL_TT_POS
;
131 struct gr_request
*req
;
133 seq_printf(seq
, "%s:\n", ep
->ep
.name
);
134 seq_printf(seq
, " mode = %s\n", gr_modestring
[mode
]);
135 seq_printf(seq
, " halted: %d\n", !!(epctrl
& GR_EPCTRL_EH
));
136 seq_printf(seq
, " disabled: %d\n", !!(epctrl
& GR_EPCTRL_ED
));
137 seq_printf(seq
, " valid: %d\n", !!(epctrl
& GR_EPCTRL_EV
));
138 seq_printf(seq
, " dma_start = %d\n", ep
->dma_start
);
139 seq_printf(seq
, " stopped = %d\n", ep
->stopped
);
140 seq_printf(seq
, " wedged = %d\n", ep
->wedged
);
141 seq_printf(seq
, " callback = %d\n", ep
->callback
);
142 seq_printf(seq
, " maxpacket = %d\n", ep
->ep
.maxpacket
);
143 seq_printf(seq
, " maxpacket_limit = %d\n", ep
->ep
.maxpacket_limit
);
144 seq_printf(seq
, " bytes_per_buffer = %d\n", ep
->bytes_per_buffer
);
145 if (mode
== 1 || mode
== 3)
146 seq_printf(seq
, " nt = %d\n",
147 (epctrl
& GR_EPCTRL_NT_MASK
) >> GR_EPCTRL_NT_POS
);
149 seq_printf(seq
, " Buffer 0: %s %s%d\n",
150 epstat
& GR_EPSTAT_B0
? "valid" : "invalid",
151 epstat
& GR_EPSTAT_BS
? " " : "selected ",
152 (epstat
& GR_EPSTAT_B0CNT_MASK
) >> GR_EPSTAT_B0CNT_POS
);
153 seq_printf(seq
, " Buffer 1: %s %s%d\n",
154 epstat
& GR_EPSTAT_B1
? "valid" : "invalid",
155 epstat
& GR_EPSTAT_BS
? "selected " : " ",
156 (epstat
& GR_EPSTAT_B1CNT_MASK
) >> GR_EPSTAT_B1CNT_POS
);
158 if (list_empty(&ep
->queue
)) {
159 seq_puts(seq
, " Queue: empty\n\n");
163 seq_puts(seq
, " Queue:\n");
164 list_for_each_entry(req
, &ep
->queue
, queue
) {
165 struct gr_dma_desc
*desc
;
166 struct gr_dma_desc
*next
;
168 seq_printf(seq
, " 0x%p: 0x%p %d %d\n", req
,
169 &req
->req
.buf
, req
->req
.actual
, req
->req
.length
);
171 next
= req
->first_desc
;
174 next
= desc
->next_desc
;
175 seq_printf(seq
, " %c 0x%p (0x%08x): 0x%05x 0x%08x\n",
176 desc
== req
->curr_desc
? 'c' : ' ',
177 desc
, desc
->paddr
, desc
->ctrl
, desc
->data
);
178 } while (desc
!= req
->last_desc
);
183 static int gr_dfs_show(struct seq_file
*seq
, void *v
)
185 struct gr_udc
*dev
= seq
->private;
186 u32 control
= gr_read32(&dev
->regs
->control
);
187 u32 status
= gr_read32(&dev
->regs
->status
);
190 seq_printf(seq
, "usb state = %s\n",
191 usb_state_string(dev
->gadget
.state
));
192 seq_printf(seq
, "address = %d\n",
193 (control
& GR_CONTROL_UA_MASK
) >> GR_CONTROL_UA_POS
);
194 seq_printf(seq
, "speed = %s\n", GR_SPEED_STR(status
));
195 seq_printf(seq
, "ep0state = %s\n", gr_ep0state_string(dev
->ep0state
));
196 seq_printf(seq
, "irq_enabled = %d\n", dev
->irq_enabled
);
197 seq_printf(seq
, "remote_wakeup = %d\n", dev
->remote_wakeup
);
198 seq_printf(seq
, "test_mode = %d\n", dev
->test_mode
);
201 list_for_each_entry(ep
, &dev
->ep_list
, ep_list
)
202 gr_seq_ep_show(seq
, ep
);
206 DEFINE_SHOW_ATTRIBUTE(gr_dfs
);
208 static void gr_dfs_create(struct gr_udc
*dev
)
210 const char *name
= "gr_udc_state";
212 dev
->dfs_root
= debugfs_create_dir(dev_name(dev
->dev
), usb_debug_root
);
213 debugfs_create_file(name
, 0444, dev
->dfs_root
, dev
, &gr_dfs_fops
);
216 static void gr_dfs_delete(struct gr_udc
*dev
)
218 debugfs_remove_recursive(dev
->dfs_root
);
221 #else /* !CONFIG_USB_GADGET_DEBUG_FS */
223 static void gr_dfs_create(struct gr_udc
*dev
) {}
224 static void gr_dfs_delete(struct gr_udc
*dev
) {}
226 #endif /* CONFIG_USB_GADGET_DEBUG_FS */
228 /* ---------------------------------------------------------------------- */
229 /* DMA and request handling */
231 /* Allocates a new struct gr_dma_desc, sets paddr and zeroes the rest */
232 static struct gr_dma_desc
*gr_alloc_dma_desc(struct gr_ep
*ep
, gfp_t gfp_flags
)
235 struct gr_dma_desc
*dma_desc
;
237 dma_desc
= dma_pool_zalloc(ep
->dev
->desc_pool
, gfp_flags
, &paddr
);
239 dev_err(ep
->dev
->dev
, "Could not allocate from DMA pool\n");
243 dma_desc
->paddr
= paddr
;
248 static inline void gr_free_dma_desc(struct gr_udc
*dev
,
249 struct gr_dma_desc
*desc
)
251 dma_pool_free(dev
->desc_pool
, desc
, (dma_addr_t
)desc
->paddr
);
254 /* Frees the chain of struct gr_dma_desc for the given request */
255 static void gr_free_dma_desc_chain(struct gr_udc
*dev
, struct gr_request
*req
)
257 struct gr_dma_desc
*desc
;
258 struct gr_dma_desc
*next
;
260 next
= req
->first_desc
;
266 next
= desc
->next_desc
;
267 gr_free_dma_desc(dev
, desc
);
268 } while (desc
!= req
->last_desc
);
270 req
->first_desc
= NULL
;
271 req
->curr_desc
= NULL
;
272 req
->last_desc
= NULL
;
275 static void gr_ep0_setup(struct gr_udc
*dev
, struct gr_request
*req
);
278 * Frees allocated resources and calls the appropriate completion function/setup
279 * package handler for a finished request.
281 * Must be called with dev->lock held and irqs disabled.
283 static void gr_finish_request(struct gr_ep
*ep
, struct gr_request
*req
,
285 __releases(&dev
->lock
)
286 __acquires(&dev
->lock
)
290 list_del_init(&req
->queue
);
292 if (likely(req
->req
.status
== -EINPROGRESS
))
293 req
->req
.status
= status
;
295 status
= req
->req
.status
;
298 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
299 gr_free_dma_desc_chain(dev
, req
);
301 if (ep
->is_in
) { /* For OUT, req->req.actual gets updated bit by bit */
302 req
->req
.actual
= req
->req
.length
;
303 } else if (req
->oddlen
&& req
->req
.actual
> req
->evenlen
) {
305 * Copy to user buffer in this case where length was not evenly
306 * divisible by ep->ep.maxpacket and the last descriptor was
309 char *buftail
= ((char *)req
->req
.buf
+ req
->evenlen
);
311 memcpy(buftail
, ep
->tailbuf
, req
->oddlen
);
313 if (req
->req
.actual
> req
->req
.length
) {
314 /* We got more data than was requested */
315 dev_dbg(ep
->dev
->dev
, "Overflow for ep %s\n",
317 gr_dbgprint_request("OVFL", ep
, req
);
318 req
->req
.status
= -EOVERFLOW
;
324 gr_dbgprint_request("SENT", ep
, req
);
326 gr_dbgprint_request("RECV", ep
, req
);
329 /* Prevent changes to ep->queue during callback */
331 if (req
== dev
->ep0reqo
&& !status
) {
333 gr_ep0_setup(dev
, req
);
336 "Unexpected non setup packet on ep0in\n");
337 } else if (req
->req
.complete
) {
338 spin_unlock(&dev
->lock
);
340 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
342 spin_lock(&dev
->lock
);
347 static struct usb_request
*gr_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
349 struct gr_request
*req
;
351 req
= kzalloc(sizeof(*req
), gfp_flags
);
355 INIT_LIST_HEAD(&req
->queue
);
361 * Starts DMA for endpoint ep if there are requests in the queue.
363 * Must be called with dev->lock held and with !ep->stopped.
365 static void gr_start_dma(struct gr_ep
*ep
)
367 struct gr_request
*req
;
370 if (list_empty(&ep
->queue
)) {
375 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
377 /* A descriptor should already have been allocated */
378 BUG_ON(!req
->curr_desc
);
381 * The DMA controller can not handle smaller OUT buffers than
382 * ep->ep.maxpacket. It could lead to buffer overruns if an unexpectedly
383 * long packet are received. Therefore an internal bounce buffer gets
384 * used when such a request gets enabled.
386 if (!ep
->is_in
&& req
->oddlen
)
387 req
->last_desc
->data
= ep
->tailbuf_paddr
;
389 wmb(); /* Make sure all is settled before handing it over to DMA */
391 /* Set the descriptor pointer in the hardware */
392 gr_write32(&ep
->regs
->dmaaddr
, req
->curr_desc
->paddr
);
394 /* Announce available descriptors */
395 dmactrl
= gr_read32(&ep
->regs
->dmactrl
);
396 gr_write32(&ep
->regs
->dmactrl
, dmactrl
| GR_DMACTRL_DA
);
402 * Finishes the first request in the ep's queue and, if available, starts the
403 * next request in queue.
405 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
407 static void gr_dma_advance(struct gr_ep
*ep
, int status
)
409 struct gr_request
*req
;
411 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
412 gr_finish_request(ep
, req
, status
);
413 gr_start_dma(ep
); /* Regardless of ep->dma_start */
417 * Abort DMA for an endpoint. Sets the abort DMA bit which causes an ongoing DMA
418 * transfer to be canceled and clears GR_DMACTRL_DA.
420 * Must be called with dev->lock held.
422 static void gr_abort_dma(struct gr_ep
*ep
)
426 dmactrl
= gr_read32(&ep
->regs
->dmactrl
);
427 gr_write32(&ep
->regs
->dmactrl
, dmactrl
| GR_DMACTRL_AD
);
431 * Allocates and sets up a struct gr_dma_desc and putting it on the descriptor
434 * Size is not used for OUT endpoints. Hardware can not be instructed to handle
435 * smaller buffer than MAXPL in the OUT direction.
437 static int gr_add_dma_desc(struct gr_ep
*ep
, struct gr_request
*req
,
438 dma_addr_t data
, unsigned size
, gfp_t gfp_flags
)
440 struct gr_dma_desc
*desc
;
442 desc
= gr_alloc_dma_desc(ep
, gfp_flags
);
449 (GR_DESC_IN_CTRL_LEN_MASK
& size
) | GR_DESC_IN_CTRL_EN
;
451 desc
->ctrl
= GR_DESC_OUT_CTRL_IE
;
453 if (!req
->first_desc
) {
454 req
->first_desc
= desc
;
455 req
->curr_desc
= desc
;
457 req
->last_desc
->next_desc
= desc
;
458 req
->last_desc
->next
= desc
->paddr
;
459 req
->last_desc
->ctrl
|= GR_DESC_OUT_CTRL_NX
;
461 req
->last_desc
= desc
;
467 * Sets up a chain of struct gr_dma_descriptors pointing to buffers that
468 * together covers req->req.length bytes of the buffer at DMA address
469 * req->req.dma for the OUT direction.
471 * The first descriptor in the chain is enabled, the rest disabled. The
472 * interrupt handler will later enable them one by one when needed so we can
473 * find out when the transfer is finished. For OUT endpoints, all descriptors
474 * therefore generate interrutps.
476 static int gr_setup_out_desc_list(struct gr_ep
*ep
, struct gr_request
*req
,
479 u16 bytes_left
; /* Bytes left to provide descriptors for */
480 u16 bytes_used
; /* Bytes accommodated for */
483 req
->first_desc
= NULL
; /* Signals that no allocation is done yet */
484 bytes_left
= req
->req
.length
;
486 while (bytes_left
> 0) {
487 dma_addr_t start
= req
->req
.dma
+ bytes_used
;
488 u16 size
= min(bytes_left
, ep
->bytes_per_buffer
);
490 if (size
< ep
->bytes_per_buffer
) {
491 /* Prepare using bounce buffer */
492 req
->evenlen
= req
->req
.length
- bytes_left
;
496 ret
= gr_add_dma_desc(ep
, req
, start
, size
, gfp_flags
);
504 req
->first_desc
->ctrl
|= GR_DESC_OUT_CTRL_EN
;
509 gr_free_dma_desc_chain(ep
->dev
, req
);
515 * Sets up a chain of struct gr_dma_descriptors pointing to buffers that
516 * together covers req->req.length bytes of the buffer at DMA address
517 * req->req.dma for the IN direction.
519 * When more data is provided than the maximum payload size, the hardware splits
520 * this up into several payloads automatically. Moreover, ep->bytes_per_buffer
521 * is always set to a multiple of the maximum payload (restricted to the valid
522 * number of maximum payloads during high bandwidth isochronous or interrupt
525 * All descriptors are enabled from the beginning and we only generate an
526 * interrupt for the last one indicating that the entire request has been pushed
529 static int gr_setup_in_desc_list(struct gr_ep
*ep
, struct gr_request
*req
,
532 u16 bytes_left
; /* Bytes left in req to provide descriptors for */
533 u16 bytes_used
; /* Bytes in req accommodated for */
536 req
->first_desc
= NULL
; /* Signals that no allocation is done yet */
537 bytes_left
= req
->req
.length
;
539 do { /* Allow for zero length packets */
540 dma_addr_t start
= req
->req
.dma
+ bytes_used
;
541 u16 size
= min(bytes_left
, ep
->bytes_per_buffer
);
543 ret
= gr_add_dma_desc(ep
, req
, start
, size
, gfp_flags
);
549 } while (bytes_left
> 0);
552 * Send an extra zero length packet to indicate that no more data is
553 * available when req->req.zero is set and the data length is even
554 * multiples of ep->ep.maxpacket.
556 if (req
->req
.zero
&& (req
->req
.length
% ep
->ep
.maxpacket
== 0)) {
557 ret
= gr_add_dma_desc(ep
, req
, 0, 0, gfp_flags
);
563 * For IN packets we only want to know when the last packet has been
564 * transmitted (not just put into internal buffers).
566 req
->last_desc
->ctrl
|= GR_DESC_IN_CTRL_PI
;
571 gr_free_dma_desc_chain(ep
->dev
, req
);
576 /* Must be called with dev->lock held */
577 static int gr_queue(struct gr_ep
*ep
, struct gr_request
*req
, gfp_t gfp_flags
)
579 struct gr_udc
*dev
= ep
->dev
;
582 if (unlikely(!ep
->ep
.desc
&& ep
->num
!= 0)) {
583 dev_err(dev
->dev
, "No ep descriptor for %s\n", ep
->ep
.name
);
587 if (unlikely(!req
->req
.buf
|| !list_empty(&req
->queue
))) {
589 "Invalid request for %s: buf=%p list_empty=%d\n",
590 ep
->ep
.name
, req
->req
.buf
, list_empty(&req
->queue
));
594 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
595 dev_err(dev
->dev
, "-ESHUTDOWN");
599 /* Can't touch registers when suspended */
600 if (dev
->ep0state
== GR_EP0_SUSPEND
) {
601 dev_err(dev
->dev
, "-EBUSY");
605 /* Set up DMA mapping in case the caller didn't */
606 ret
= usb_gadget_map_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
608 dev_err(dev
->dev
, "usb_gadget_map_request");
613 ret
= gr_setup_in_desc_list(ep
, req
, gfp_flags
);
615 ret
= gr_setup_out_desc_list(ep
, req
, gfp_flags
);
619 req
->req
.status
= -EINPROGRESS
;
621 list_add_tail(&req
->queue
, &ep
->queue
);
623 /* Start DMA if not started, otherwise interrupt handler handles it */
624 if (!ep
->dma_start
&& likely(!ep
->stopped
))
631 * Queue a request from within the driver.
633 * Must be called with dev->lock held.
635 static inline int gr_queue_int(struct gr_ep
*ep
, struct gr_request
*req
,
639 gr_dbgprint_request("RESP", ep
, req
);
641 return gr_queue(ep
, req
, gfp_flags
);
644 /* ---------------------------------------------------------------------- */
645 /* General helper functions */
648 * Dequeue ALL requests.
650 * Must be called with dev->lock held and irqs disabled.
652 static void gr_ep_nuke(struct gr_ep
*ep
)
654 struct gr_request
*req
;
660 while (!list_empty(&ep
->queue
)) {
661 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
662 gr_finish_request(ep
, req
, -ESHUTDOWN
);
667 * Reset the hardware state of this endpoint.
669 * Must be called with dev->lock held.
671 static void gr_ep_reset(struct gr_ep
*ep
)
673 gr_write32(&ep
->regs
->epctrl
, 0);
674 gr_write32(&ep
->regs
->dmactrl
, 0);
676 ep
->ep
.maxpacket
= MAX_CTRL_PL_SIZE
;
683 * Generate STALL on ep0in/out.
685 * Must be called with dev->lock held.
687 static void gr_control_stall(struct gr_udc
*dev
)
691 epctrl
= gr_read32(&dev
->epo
[0].regs
->epctrl
);
692 gr_write32(&dev
->epo
[0].regs
->epctrl
, epctrl
| GR_EPCTRL_CS
);
693 epctrl
= gr_read32(&dev
->epi
[0].regs
->epctrl
);
694 gr_write32(&dev
->epi
[0].regs
->epctrl
, epctrl
| GR_EPCTRL_CS
);
696 dev
->ep0state
= GR_EP0_STALL
;
700 * Halts, halts and wedges, or clears halt for an endpoint.
702 * Must be called with dev->lock held.
704 static int gr_ep_halt_wedge(struct gr_ep
*ep
, int halt
, int wedge
, int fromhost
)
709 if (ep
->num
&& !ep
->ep
.desc
)
712 if (ep
->num
&& ep
->ep
.desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
)
715 /* Never actually halt ep0, and therefore never clear halt for ep0 */
717 if (halt
&& !fromhost
) {
718 /* ep0 halt from gadget - generate protocol stall */
719 gr_control_stall(ep
->dev
);
720 dev_dbg(ep
->dev
->dev
, "EP: stall ep0\n");
726 dev_dbg(ep
->dev
->dev
, "EP: %s halt %s\n",
727 (halt
? (wedge
? "wedge" : "set") : "clear"), ep
->ep
.name
);
729 epctrl
= gr_read32(&ep
->regs
->epctrl
);
732 gr_write32(&ep
->regs
->epctrl
, epctrl
| GR_EPCTRL_EH
);
737 gr_write32(&ep
->regs
->epctrl
, epctrl
& ~GR_EPCTRL_EH
);
741 /* Things might have been queued up in the meantime */
749 /* Must be called with dev->lock held */
750 static inline void gr_set_ep0state(struct gr_udc
*dev
, enum gr_ep0state value
)
752 if (dev
->ep0state
!= value
)
753 dev_vdbg(dev
->dev
, "STATE: ep0state=%s\n",
754 gr_ep0state_string(value
));
755 dev
->ep0state
= value
;
759 * Should only be called when endpoints can not generate interrupts.
761 * Must be called with dev->lock held.
763 static void gr_disable_interrupts_and_pullup(struct gr_udc
*dev
)
765 gr_write32(&dev
->regs
->control
, 0);
766 wmb(); /* Make sure that we do not deny one of our interrupts */
767 dev
->irq_enabled
= 0;
771 * Stop all device activity and disable data line pullup.
773 * Must be called with dev->lock held and irqs disabled.
775 static void gr_stop_activity(struct gr_udc
*dev
)
779 list_for_each_entry(ep
, &dev
->ep_list
, ep_list
)
782 gr_disable_interrupts_and_pullup(dev
);
784 gr_set_ep0state(dev
, GR_EP0_DISCONNECT
);
785 usb_gadget_set_state(&dev
->gadget
, USB_STATE_NOTATTACHED
);
788 /* ---------------------------------------------------------------------- */
789 /* ep0 setup packet handling */
791 static void gr_ep0_testmode_complete(struct usb_ep
*_ep
,
792 struct usb_request
*_req
)
798 ep
= container_of(_ep
, struct gr_ep
, ep
);
801 spin_lock(&dev
->lock
);
803 control
= gr_read32(&dev
->regs
->control
);
804 control
|= GR_CONTROL_TM
| (dev
->test_mode
<< GR_CONTROL_TS_POS
);
805 gr_write32(&dev
->regs
->control
, control
);
807 spin_unlock(&dev
->lock
);
810 static void gr_ep0_dummy_complete(struct usb_ep
*_ep
, struct usb_request
*_req
)
812 /* Nothing needs to be done here */
816 * Queue a response on ep0in.
818 * Must be called with dev->lock held.
820 static int gr_ep0_respond(struct gr_udc
*dev
, u8
*buf
, int length
,
821 void (*complete
)(struct usb_ep
*ep
,
822 struct usb_request
*req
))
824 u8
*reqbuf
= dev
->ep0reqi
->req
.buf
;
828 for (i
= 0; i
< length
; i
++)
830 dev
->ep0reqi
->req
.length
= length
;
831 dev
->ep0reqi
->req
.complete
= complete
;
833 status
= gr_queue_int(&dev
->epi
[0], dev
->ep0reqi
, GFP_ATOMIC
);
836 "Could not queue ep0in setup response: %d\n", status
);
842 * Queue a 2 byte response on ep0in.
844 * Must be called with dev->lock held.
846 static inline int gr_ep0_respond_u16(struct gr_udc
*dev
, u16 response
)
848 __le16 le_response
= cpu_to_le16(response
);
850 return gr_ep0_respond(dev
, (u8
*)&le_response
, 2,
851 gr_ep0_dummy_complete
);
855 * Queue a ZLP response on ep0in.
857 * Must be called with dev->lock held.
859 static inline int gr_ep0_respond_empty(struct gr_udc
*dev
)
861 return gr_ep0_respond(dev
, NULL
, 0, gr_ep0_dummy_complete
);
865 * This is run when a SET_ADDRESS request is received. First writes
866 * the new address to the control register which is updated internally
867 * when the next IN packet is ACKED.
869 * Must be called with dev->lock held.
871 static void gr_set_address(struct gr_udc
*dev
, u8 address
)
875 control
= gr_read32(&dev
->regs
->control
) & ~GR_CONTROL_UA_MASK
;
876 control
|= (address
<< GR_CONTROL_UA_POS
) & GR_CONTROL_UA_MASK
;
877 control
|= GR_CONTROL_SU
;
878 gr_write32(&dev
->regs
->control
, control
);
882 * Returns negative for STALL, 0 for successful handling and positive for
885 * Must be called with dev->lock held.
887 static int gr_device_request(struct gr_udc
*dev
, u8 type
, u8 request
,
888 u16 value
, u16 index
)
894 case USB_REQ_SET_ADDRESS
:
895 dev_dbg(dev
->dev
, "STATUS: address %d\n", value
& 0xff);
896 gr_set_address(dev
, value
& 0xff);
898 usb_gadget_set_state(&dev
->gadget
, USB_STATE_ADDRESS
);
900 usb_gadget_set_state(&dev
->gadget
, USB_STATE_DEFAULT
);
901 return gr_ep0_respond_empty(dev
);
903 case USB_REQ_GET_STATUS
:
904 /* Self powered | remote wakeup */
905 response
= 0x0001 | (dev
->remote_wakeup
? 0x0002 : 0);
906 return gr_ep0_respond_u16(dev
, response
);
908 case USB_REQ_SET_FEATURE
:
910 case USB_DEVICE_REMOTE_WAKEUP
:
911 /* Allow remote wakeup */
912 dev
->remote_wakeup
= 1;
913 return gr_ep0_respond_empty(dev
);
915 case USB_DEVICE_TEST_MODE
:
916 /* The hardware does not support TEST_FORCE_EN */
918 if (test
>= TEST_J
&& test
<= TEST_PACKET
) {
919 dev
->test_mode
= test
;
920 return gr_ep0_respond(dev
, NULL
, 0,
921 gr_ep0_testmode_complete
);
926 case USB_REQ_CLEAR_FEATURE
:
928 case USB_DEVICE_REMOTE_WAKEUP
:
929 /* Disallow remote wakeup */
930 dev
->remote_wakeup
= 0;
931 return gr_ep0_respond_empty(dev
);
936 return 1; /* Delegate the rest */
940 * Returns negative for STALL, 0 for successful handling and positive for
943 * Must be called with dev->lock held.
945 static int gr_interface_request(struct gr_udc
*dev
, u8 type
, u8 request
,
946 u16 value
, u16 index
)
948 if (dev
->gadget
.state
!= USB_STATE_CONFIGURED
)
952 * Should return STALL for invalid interfaces, but udc driver does not
953 * know anything about that. However, many gadget drivers do not handle
954 * GET_STATUS so we need to take care of that.
958 case USB_REQ_GET_STATUS
:
959 return gr_ep0_respond_u16(dev
, 0x0000);
961 case USB_REQ_SET_FEATURE
:
962 case USB_REQ_CLEAR_FEATURE
:
964 * No possible valid standard requests. Still let gadget drivers
970 return 1; /* Delegate the rest */
974 * Returns negative for STALL, 0 for successful handling and positive for
977 * Must be called with dev->lock held.
979 static int gr_endpoint_request(struct gr_udc
*dev
, u8 type
, u8 request
,
980 u16 value
, u16 index
)
985 u8 epnum
= index
& USB_ENDPOINT_NUMBER_MASK
;
986 u8 is_in
= index
& USB_ENDPOINT_DIR_MASK
;
988 if ((is_in
&& epnum
>= dev
->nepi
) || (!is_in
&& epnum
>= dev
->nepo
))
991 if (dev
->gadget
.state
!= USB_STATE_CONFIGURED
&& epnum
!= 0)
994 ep
= (is_in
? &dev
->epi
[epnum
] : &dev
->epo
[epnum
]);
997 case USB_REQ_GET_STATUS
:
998 halted
= gr_read32(&ep
->regs
->epctrl
) & GR_EPCTRL_EH
;
999 return gr_ep0_respond_u16(dev
, halted
? 0x0001 : 0);
1001 case USB_REQ_SET_FEATURE
:
1003 case USB_ENDPOINT_HALT
:
1004 status
= gr_ep_halt_wedge(ep
, 1, 0, 1);
1006 status
= gr_ep0_respond_empty(dev
);
1011 case USB_REQ_CLEAR_FEATURE
:
1013 case USB_ENDPOINT_HALT
:
1016 status
= gr_ep_halt_wedge(ep
, 0, 0, 1);
1018 status
= gr_ep0_respond_empty(dev
);
1024 return 1; /* Delegate the rest */
1027 /* Must be called with dev->lock held */
1028 static void gr_ep0out_requeue(struct gr_udc
*dev
)
1030 int ret
= gr_queue_int(&dev
->epo
[0], dev
->ep0reqo
, GFP_ATOMIC
);
1033 dev_err(dev
->dev
, "Could not queue ep0out setup request: %d\n",
1038 * The main function dealing with setup requests on ep0.
1040 * Must be called with dev->lock held and irqs disabled
1042 static void gr_ep0_setup(struct gr_udc
*dev
, struct gr_request
*req
)
1043 __releases(&dev
->lock
)
1044 __acquires(&dev
->lock
)
1047 struct usb_ctrlrequest ctrl
;
1059 /* Restore from ep0 halt */
1060 if (dev
->ep0state
== GR_EP0_STALL
) {
1061 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1062 if (!req
->req
.actual
)
1066 if (dev
->ep0state
== GR_EP0_ISTATUS
) {
1067 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1068 if (req
->req
.actual
> 0)
1070 "Unexpected setup packet at state %s\n",
1071 gr_ep0state_string(GR_EP0_ISTATUS
));
1073 goto out
; /* Got expected ZLP */
1074 } else if (dev
->ep0state
!= GR_EP0_SETUP
) {
1076 "Unexpected ep0out request at state %s - stalling\n",
1077 gr_ep0state_string(dev
->ep0state
));
1078 gr_control_stall(dev
);
1079 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1081 } else if (!req
->req
.actual
) {
1082 dev_dbg(dev
->dev
, "Unexpected ZLP at state %s\n",
1083 gr_ep0state_string(dev
->ep0state
));
1087 /* Handle SETUP packet */
1088 for (i
= 0; i
< req
->req
.actual
; i
++)
1089 u
.raw
[i
] = ((u8
*)req
->req
.buf
)[i
];
1091 type
= u
.ctrl
.bRequestType
;
1092 request
= u
.ctrl
.bRequest
;
1093 value
= le16_to_cpu(u
.ctrl
.wValue
);
1094 index
= le16_to_cpu(u
.ctrl
.wIndex
);
1095 length
= le16_to_cpu(u
.ctrl
.wLength
);
1097 gr_dbgprint_devreq(dev
, type
, request
, value
, index
, length
);
1099 /* Check for data stage */
1101 if (type
& USB_DIR_IN
)
1102 gr_set_ep0state(dev
, GR_EP0_IDATA
);
1104 gr_set_ep0state(dev
, GR_EP0_ODATA
);
1107 status
= 1; /* Positive status flags delegation */
1108 if ((type
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1109 switch (type
& USB_RECIP_MASK
) {
1110 case USB_RECIP_DEVICE
:
1111 status
= gr_device_request(dev
, type
, request
,
1114 case USB_RECIP_ENDPOINT
:
1115 status
= gr_endpoint_request(dev
, type
, request
,
1118 case USB_RECIP_INTERFACE
:
1119 status
= gr_interface_request(dev
, type
, request
,
1126 spin_unlock(&dev
->lock
);
1128 dev_vdbg(dev
->dev
, "DELEGATE\n");
1129 status
= dev
->driver
->setup(&dev
->gadget
, &u
.ctrl
);
1131 spin_lock(&dev
->lock
);
1134 /* Generate STALL on both ep0out and ep0in if requested */
1135 if (unlikely(status
< 0)) {
1136 dev_vdbg(dev
->dev
, "STALL\n");
1137 gr_control_stall(dev
);
1140 if ((type
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
&&
1141 request
== USB_REQ_SET_CONFIGURATION
) {
1143 dev_dbg(dev
->dev
, "STATUS: deconfigured\n");
1144 usb_gadget_set_state(&dev
->gadget
, USB_STATE_ADDRESS
);
1145 } else if (status
>= 0) {
1146 /* Not configured unless gadget OK:s it */
1147 dev_dbg(dev
->dev
, "STATUS: configured: %d\n", value
);
1148 usb_gadget_set_state(&dev
->gadget
,
1149 USB_STATE_CONFIGURED
);
1153 /* Get ready for next stage */
1154 if (dev
->ep0state
== GR_EP0_ODATA
)
1155 gr_set_ep0state(dev
, GR_EP0_OSTATUS
);
1156 else if (dev
->ep0state
== GR_EP0_IDATA
)
1157 gr_set_ep0state(dev
, GR_EP0_ISTATUS
);
1159 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1162 gr_ep0out_requeue(dev
);
1165 /* ---------------------------------------------------------------------- */
1166 /* VBUS and USB reset handling */
1168 /* Must be called with dev->lock held and irqs disabled */
1169 static void gr_vbus_connected(struct gr_udc
*dev
, u32 status
)
1173 dev
->gadget
.speed
= GR_SPEED(status
);
1174 usb_gadget_set_state(&dev
->gadget
, USB_STATE_POWERED
);
1176 /* Turn on full interrupts and pullup */
1177 control
= (GR_CONTROL_SI
| GR_CONTROL_UI
| GR_CONTROL_VI
|
1178 GR_CONTROL_SP
| GR_CONTROL_EP
);
1179 gr_write32(&dev
->regs
->control
, control
);
1182 /* Must be called with dev->lock held */
1183 static void gr_enable_vbus_detect(struct gr_udc
*dev
)
1187 dev
->irq_enabled
= 1;
1188 wmb(); /* Make sure we do not ignore an interrupt */
1189 gr_write32(&dev
->regs
->control
, GR_CONTROL_VI
);
1191 /* Take care of the case we are already plugged in at this point */
1192 status
= gr_read32(&dev
->regs
->status
);
1193 if (status
& GR_STATUS_VB
)
1194 gr_vbus_connected(dev
, status
);
1197 /* Must be called with dev->lock held and irqs disabled */
1198 static void gr_vbus_disconnected(struct gr_udc
*dev
)
1200 gr_stop_activity(dev
);
1202 /* Report disconnect */
1203 if (dev
->driver
&& dev
->driver
->disconnect
) {
1204 spin_unlock(&dev
->lock
);
1206 dev
->driver
->disconnect(&dev
->gadget
);
1208 spin_lock(&dev
->lock
);
1211 gr_enable_vbus_detect(dev
);
1214 /* Must be called with dev->lock held and irqs disabled */
1215 static void gr_udc_usbreset(struct gr_udc
*dev
, u32 status
)
1217 gr_set_address(dev
, 0);
1218 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1219 usb_gadget_set_state(&dev
->gadget
, USB_STATE_DEFAULT
);
1220 dev
->gadget
.speed
= GR_SPEED(status
);
1222 gr_ep_nuke(&dev
->epo
[0]);
1223 gr_ep_nuke(&dev
->epi
[0]);
1224 dev
->epo
[0].stopped
= 0;
1225 dev
->epi
[0].stopped
= 0;
1226 gr_ep0out_requeue(dev
);
1229 /* ---------------------------------------------------------------------- */
1233 * Handles interrupts from in endpoints. Returns whether something was handled.
1235 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1237 static int gr_handle_in_ep(struct gr_ep
*ep
)
1239 struct gr_request
*req
;
1241 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
1242 if (!req
->last_desc
)
1245 if (READ_ONCE(req
->last_desc
->ctrl
) & GR_DESC_IN_CTRL_EN
)
1246 return 0; /* Not put in hardware buffers yet */
1248 if (gr_read32(&ep
->regs
->epstat
) & (GR_EPSTAT_B1
| GR_EPSTAT_B0
))
1249 return 0; /* Not transmitted yet, still in hardware buffers */
1251 /* Write complete */
1252 gr_dma_advance(ep
, 0);
1258 * Handles interrupts from out endpoints. Returns whether something was handled.
1260 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1262 static int gr_handle_out_ep(struct gr_ep
*ep
)
1267 struct gr_request
*req
;
1268 struct gr_udc
*dev
= ep
->dev
;
1270 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
1271 if (!req
->curr_desc
)
1274 ctrl
= READ_ONCE(req
->curr_desc
->ctrl
);
1275 if (ctrl
& GR_DESC_OUT_CTRL_EN
)
1276 return 0; /* Not received yet */
1279 len
= ctrl
& GR_DESC_OUT_CTRL_LEN_MASK
;
1280 req
->req
.actual
+= len
;
1281 if (ctrl
& GR_DESC_OUT_CTRL_SE
)
1284 if (len
< ep
->ep
.maxpacket
|| req
->req
.actual
>= req
->req
.length
) {
1285 /* Short packet or >= expected size - we are done */
1287 if ((ep
== &dev
->epo
[0]) && (dev
->ep0state
== GR_EP0_OSTATUS
)) {
1289 * Send a status stage ZLP to ack the DATA stage in the
1290 * OUT direction. This needs to be done before
1291 * gr_dma_advance as that can lead to a call to
1292 * ep0_setup that can change dev->ep0state.
1294 gr_ep0_respond_empty(dev
);
1295 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1298 gr_dma_advance(ep
, 0);
1300 /* Not done yet. Enable the next descriptor to receive more. */
1301 req
->curr_desc
= req
->curr_desc
->next_desc
;
1302 req
->curr_desc
->ctrl
|= GR_DESC_OUT_CTRL_EN
;
1304 ep_dmactrl
= gr_read32(&ep
->regs
->dmactrl
);
1305 gr_write32(&ep
->regs
->dmactrl
, ep_dmactrl
| GR_DMACTRL_DA
);
1312 * Handle state changes. Returns whether something was handled.
1314 * Must be called with dev->lock held and irqs disabled.
1316 static int gr_handle_state_changes(struct gr_udc
*dev
)
1318 u32 status
= gr_read32(&dev
->regs
->status
);
1320 int powstate
= !(dev
->gadget
.state
== USB_STATE_NOTATTACHED
||
1321 dev
->gadget
.state
== USB_STATE_ATTACHED
);
1323 /* VBUS valid detected */
1324 if (!powstate
&& (status
& GR_STATUS_VB
)) {
1325 dev_dbg(dev
->dev
, "STATUS: vbus valid detected\n");
1326 gr_vbus_connected(dev
, status
);
1331 if (powstate
&& !(status
& GR_STATUS_VB
)) {
1332 dev_dbg(dev
->dev
, "STATUS: vbus invalid detected\n");
1333 gr_vbus_disconnected(dev
);
1337 /* USB reset detected */
1338 if (status
& GR_STATUS_UR
) {
1339 dev_dbg(dev
->dev
, "STATUS: USB reset - speed is %s\n",
1340 GR_SPEED_STR(status
));
1341 gr_write32(&dev
->regs
->status
, GR_STATUS_UR
);
1342 gr_udc_usbreset(dev
, status
);
1347 if (dev
->gadget
.speed
!= GR_SPEED(status
)) {
1348 dev_dbg(dev
->dev
, "STATUS: USB Speed change to %s\n",
1349 GR_SPEED_STR(status
));
1350 dev
->gadget
.speed
= GR_SPEED(status
);
1354 /* Going into suspend */
1355 if ((dev
->ep0state
!= GR_EP0_SUSPEND
) && !(status
& GR_STATUS_SU
)) {
1356 dev_dbg(dev
->dev
, "STATUS: USB suspend\n");
1357 gr_set_ep0state(dev
, GR_EP0_SUSPEND
);
1358 dev
->suspended_from
= dev
->gadget
.state
;
1359 usb_gadget_set_state(&dev
->gadget
, USB_STATE_SUSPENDED
);
1361 if ((dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
) &&
1362 dev
->driver
&& dev
->driver
->suspend
) {
1363 spin_unlock(&dev
->lock
);
1365 dev
->driver
->suspend(&dev
->gadget
);
1367 spin_lock(&dev
->lock
);
1372 /* Coming out of suspend */
1373 if ((dev
->ep0state
== GR_EP0_SUSPEND
) && (status
& GR_STATUS_SU
)) {
1374 dev_dbg(dev
->dev
, "STATUS: USB resume\n");
1375 if (dev
->suspended_from
== USB_STATE_POWERED
)
1376 gr_set_ep0state(dev
, GR_EP0_DISCONNECT
);
1378 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1379 usb_gadget_set_state(&dev
->gadget
, dev
->suspended_from
);
1381 if ((dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
) &&
1382 dev
->driver
&& dev
->driver
->resume
) {
1383 spin_unlock(&dev
->lock
);
1385 dev
->driver
->resume(&dev
->gadget
);
1387 spin_lock(&dev
->lock
);
1395 /* Non-interrupt context irq handler */
1396 static irqreturn_t
gr_irq_handler(int irq
, void *_dev
)
1398 struct gr_udc
*dev
= _dev
;
1402 unsigned long flags
;
1404 spin_lock_irqsave(&dev
->lock
, flags
);
1406 if (!dev
->irq_enabled
)
1410 * Check IN ep interrupts. We check these before the OUT eps because
1411 * some gadgets reuse the request that might already be currently
1412 * outstanding and needs to be completed (mainly setup requests).
1414 for (i
= 0; i
< dev
->nepi
; i
++) {
1416 if (!ep
->stopped
&& !ep
->callback
&& !list_empty(&ep
->queue
))
1417 handled
= gr_handle_in_ep(ep
) || handled
;
1420 /* Check OUT ep interrupts */
1421 for (i
= 0; i
< dev
->nepo
; i
++) {
1423 if (!ep
->stopped
&& !ep
->callback
&& !list_empty(&ep
->queue
))
1424 handled
= gr_handle_out_ep(ep
) || handled
;
1427 /* Check status interrupts */
1428 handled
= gr_handle_state_changes(dev
) || handled
;
1431 * Check AMBA DMA errors. Only check if we didn't find anything else to
1432 * handle because this shouldn't happen if we did everything right.
1435 list_for_each_entry(ep
, &dev
->ep_list
, ep_list
) {
1436 if (gr_read32(&ep
->regs
->dmactrl
) & GR_DMACTRL_AE
) {
1438 "AMBA Error occurred for %s\n",
1446 spin_unlock_irqrestore(&dev
->lock
, flags
);
1448 return handled
? IRQ_HANDLED
: IRQ_NONE
;
1451 /* Interrupt context irq handler */
1452 static irqreturn_t
gr_irq(int irq
, void *_dev
)
1454 struct gr_udc
*dev
= _dev
;
1456 if (!dev
->irq_enabled
)
1459 return IRQ_WAKE_THREAD
;
1462 /* ---------------------------------------------------------------------- */
1465 /* Enable endpoint. Not for ep0in and ep0out that are handled separately. */
1466 static int gr_ep_enable(struct usb_ep
*_ep
,
1467 const struct usb_endpoint_descriptor
*desc
)
1474 u16 buffer_size
= 0;
1477 ep
= container_of(_ep
, struct gr_ep
, ep
);
1478 if (!_ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
1483 /* 'ep0' IN and OUT are reserved */
1484 if (ep
== &dev
->epo
[0] || ep
== &dev
->epi
[0])
1487 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1490 /* Make sure we are clear for enabling */
1491 epctrl
= gr_read32(&ep
->regs
->epctrl
);
1492 if (epctrl
& GR_EPCTRL_EV
)
1495 /* Check that directions match */
1496 if (!ep
->is_in
!= !usb_endpoint_dir_in(desc
))
1500 if ((!ep
->is_in
&& ep
->num
>= dev
->nepo
) ||
1501 (ep
->is_in
&& ep
->num
>= dev
->nepi
))
1504 if (usb_endpoint_xfer_control(desc
)) {
1506 } else if (usb_endpoint_xfer_isoc(desc
)) {
1508 } else if (usb_endpoint_xfer_bulk(desc
)) {
1510 } else if (usb_endpoint_xfer_int(desc
)) {
1513 dev_err(dev
->dev
, "Unknown transfer type for %s\n",
1519 * Bits 10-0 set the max payload. 12-11 set the number of
1520 * additional transactions.
1522 max
= usb_endpoint_maxp(desc
);
1523 nt
= usb_endpoint_maxp_mult(desc
) - 1;
1524 buffer_size
= GR_BUFFER_SIZE(epctrl
);
1525 if (nt
&& (mode
== 0 || mode
== 2)) {
1527 "%s mode: multiple trans./microframe not valid\n",
1528 (mode
== 2 ? "Bulk" : "Control"));
1530 } else if (nt
== 0x3) {
1532 "Invalid value 0x3 for additional trans./microframe\n");
1534 } else if ((nt
+ 1) * max
> buffer_size
) {
1535 dev_err(dev
->dev
, "Hw buffer size %d < max payload %d * %d\n",
1536 buffer_size
, (nt
+ 1), max
);
1538 } else if (max
== 0) {
1539 dev_err(dev
->dev
, "Max payload cannot be set to 0\n");
1541 } else if (max
> ep
->ep
.maxpacket_limit
) {
1542 dev_err(dev
->dev
, "Requested max payload %d > limit %d\n",
1543 max
, ep
->ep
.maxpacket_limit
);
1547 spin_lock(&ep
->dev
->lock
);
1550 spin_unlock(&ep
->dev
->lock
);
1557 ep
->ep
.maxpacket
= max
;
1563 * Maximum possible size of all payloads in one microframe
1564 * regardless of direction when using high-bandwidth mode.
1566 ep
->bytes_per_buffer
= (nt
+ 1) * max
;
1567 } else if (ep
->is_in
) {
1569 * The biggest multiple of maximum packet size that fits into
1570 * the buffer. The hardware will split up into many packets in
1573 ep
->bytes_per_buffer
= (buffer_size
/ max
) * max
;
1576 * Only single packets will be placed the buffers in the OUT
1579 ep
->bytes_per_buffer
= max
;
1582 epctrl
= (max
<< GR_EPCTRL_MAXPL_POS
)
1583 | (nt
<< GR_EPCTRL_NT_POS
)
1584 | (mode
<< GR_EPCTRL_TT_POS
)
1587 epctrl
|= GR_EPCTRL_PI
;
1588 gr_write32(&ep
->regs
->epctrl
, epctrl
);
1590 gr_write32(&ep
->regs
->dmactrl
, GR_DMACTRL_IE
| GR_DMACTRL_AI
);
1592 spin_unlock(&ep
->dev
->lock
);
1594 dev_dbg(ep
->dev
->dev
, "EP: %s enabled - %s with %d bytes/buffer\n",
1595 ep
->ep
.name
, gr_modestring
[mode
], ep
->bytes_per_buffer
);
1599 /* Disable endpoint. Not for ep0in and ep0out that are handled separately. */
1600 static int gr_ep_disable(struct usb_ep
*_ep
)
1604 unsigned long flags
;
1606 ep
= container_of(_ep
, struct gr_ep
, ep
);
1607 if (!_ep
|| !ep
->ep
.desc
)
1612 /* 'ep0' IN and OUT are reserved */
1613 if (ep
== &dev
->epo
[0] || ep
== &dev
->epi
[0])
1616 if (dev
->ep0state
== GR_EP0_SUSPEND
)
1619 dev_dbg(ep
->dev
->dev
, "EP: disable %s\n", ep
->ep
.name
);
1621 spin_lock_irqsave(&dev
->lock
, flags
);
1627 spin_unlock_irqrestore(&dev
->lock
, flags
);
1633 * Frees a request, but not any DMA buffers associated with it
1634 * (gr_finish_request should already have taken care of that).
1636 static void gr_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1638 struct gr_request
*req
;
1642 req
= container_of(_req
, struct gr_request
, req
);
1644 /* Leads to memory leak */
1645 WARN(!list_empty(&req
->queue
),
1646 "request not dequeued properly before freeing\n");
1651 /* Queue a request from the gadget */
1652 static int gr_queue_ext(struct usb_ep
*_ep
, struct usb_request
*_req
,
1656 struct gr_request
*req
;
1660 if (unlikely(!_ep
|| !_req
))
1663 ep
= container_of(_ep
, struct gr_ep
, ep
);
1664 req
= container_of(_req
, struct gr_request
, req
);
1667 spin_lock(&ep
->dev
->lock
);
1670 * The ep0 pointer in the gadget struct is used both for ep0in and
1671 * ep0out. In a data stage in the out direction ep0out needs to be used
1672 * instead of the default ep0in. Completion functions might use
1673 * driver_data, so that needs to be copied as well.
1675 if ((ep
== &dev
->epi
[0]) && (dev
->ep0state
== GR_EP0_ODATA
)) {
1677 ep
->ep
.driver_data
= dev
->epi
[0].ep
.driver_data
;
1681 gr_dbgprint_request("EXTERN", ep
, req
);
1683 ret
= gr_queue(ep
, req
, GFP_ATOMIC
);
1685 spin_unlock(&ep
->dev
->lock
);
1690 /* Dequeue JUST ONE request */
1691 static int gr_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1693 struct gr_request
*req
;
1697 unsigned long flags
;
1699 ep
= container_of(_ep
, struct gr_ep
, ep
);
1700 if (!_ep
|| !_req
|| (!ep
->ep
.desc
&& ep
->num
!= 0))
1706 /* We can't touch (DMA) registers when suspended */
1707 if (dev
->ep0state
== GR_EP0_SUSPEND
)
1710 spin_lock_irqsave(&dev
->lock
, flags
);
1712 /* Make sure it's actually queued on this endpoint */
1713 list_for_each_entry(req
, &ep
->queue
, queue
) {
1714 if (&req
->req
== _req
)
1717 if (&req
->req
!= _req
) {
1722 if (list_first_entry(&ep
->queue
, struct gr_request
, queue
) == req
) {
1723 /* This request is currently being processed */
1726 gr_finish_request(ep
, req
, -ECONNRESET
);
1728 gr_dma_advance(ep
, -ECONNRESET
);
1729 } else if (!list_empty(&req
->queue
)) {
1730 /* Not being processed - gr_finish_request dequeues it */
1731 gr_finish_request(ep
, req
, -ECONNRESET
);
1737 spin_unlock_irqrestore(&dev
->lock
, flags
);
1742 /* Helper for gr_set_halt and gr_set_wedge */
1743 static int gr_set_halt_wedge(struct usb_ep
*_ep
, int halt
, int wedge
)
1750 ep
= container_of(_ep
, struct gr_ep
, ep
);
1752 spin_lock(&ep
->dev
->lock
);
1754 /* Halting an IN endpoint should fail if queue is not empty */
1755 if (halt
&& ep
->is_in
&& !list_empty(&ep
->queue
)) {
1760 ret
= gr_ep_halt_wedge(ep
, halt
, wedge
, 0);
1763 spin_unlock(&ep
->dev
->lock
);
1769 static int gr_set_halt(struct usb_ep
*_ep
, int halt
)
1771 return gr_set_halt_wedge(_ep
, halt
, 0);
1774 /* Halt and wedge endpoint */
1775 static int gr_set_wedge(struct usb_ep
*_ep
)
1777 return gr_set_halt_wedge(_ep
, 1, 1);
1781 * Return the total number of bytes currently stored in the internal buffers of
1784 static int gr_fifo_status(struct usb_ep
*_ep
)
1792 ep
= container_of(_ep
, struct gr_ep
, ep
);
1794 epstat
= gr_read32(&ep
->regs
->epstat
);
1796 if (epstat
& GR_EPSTAT_B0
)
1797 bytes
+= (epstat
& GR_EPSTAT_B0CNT_MASK
) >> GR_EPSTAT_B0CNT_POS
;
1798 if (epstat
& GR_EPSTAT_B1
)
1799 bytes
+= (epstat
& GR_EPSTAT_B1CNT_MASK
) >> GR_EPSTAT_B1CNT_POS
;
1805 /* Empty data from internal buffers of an endpoint. */
1806 static void gr_fifo_flush(struct usb_ep
*_ep
)
1813 ep
= container_of(_ep
, struct gr_ep
, ep
);
1814 dev_vdbg(ep
->dev
->dev
, "EP: flush fifo %s\n", ep
->ep
.name
);
1816 spin_lock(&ep
->dev
->lock
);
1818 epctrl
= gr_read32(&ep
->regs
->epctrl
);
1819 epctrl
|= GR_EPCTRL_CB
;
1820 gr_write32(&ep
->regs
->epctrl
, epctrl
);
1822 spin_unlock(&ep
->dev
->lock
);
1825 static const struct usb_ep_ops gr_ep_ops
= {
1826 .enable
= gr_ep_enable
,
1827 .disable
= gr_ep_disable
,
1829 .alloc_request
= gr_alloc_request
,
1830 .free_request
= gr_free_request
,
1832 .queue
= gr_queue_ext
,
1833 .dequeue
= gr_dequeue
,
1835 .set_halt
= gr_set_halt
,
1836 .set_wedge
= gr_set_wedge
,
1837 .fifo_status
= gr_fifo_status
,
1838 .fifo_flush
= gr_fifo_flush
,
1841 /* ---------------------------------------------------------------------- */
1842 /* USB Gadget ops */
1844 static int gr_get_frame(struct usb_gadget
*_gadget
)
1850 dev
= container_of(_gadget
, struct gr_udc
, gadget
);
1851 return gr_read32(&dev
->regs
->status
) & GR_STATUS_FN_MASK
;
1854 static int gr_wakeup(struct usb_gadget
*_gadget
)
1860 dev
= container_of(_gadget
, struct gr_udc
, gadget
);
1862 /* Remote wakeup feature not enabled by host*/
1863 if (!dev
->remote_wakeup
)
1866 spin_lock(&dev
->lock
);
1868 gr_write32(&dev
->regs
->control
,
1869 gr_read32(&dev
->regs
->control
) | GR_CONTROL_RW
);
1871 spin_unlock(&dev
->lock
);
1876 static int gr_pullup(struct usb_gadget
*_gadget
, int is_on
)
1883 dev
= container_of(_gadget
, struct gr_udc
, gadget
);
1885 spin_lock(&dev
->lock
);
1887 control
= gr_read32(&dev
->regs
->control
);
1889 control
|= GR_CONTROL_EP
;
1891 control
&= ~GR_CONTROL_EP
;
1892 gr_write32(&dev
->regs
->control
, control
);
1894 spin_unlock(&dev
->lock
);
1899 static int gr_udc_start(struct usb_gadget
*gadget
,
1900 struct usb_gadget_driver
*driver
)
1902 struct gr_udc
*dev
= to_gr_udc(gadget
);
1904 spin_lock(&dev
->lock
);
1906 /* Hook up the driver */
1907 driver
->driver
.bus
= NULL
;
1908 dev
->driver
= driver
;
1910 /* Get ready for host detection */
1911 gr_enable_vbus_detect(dev
);
1913 spin_unlock(&dev
->lock
);
1918 static int gr_udc_stop(struct usb_gadget
*gadget
)
1920 struct gr_udc
*dev
= to_gr_udc(gadget
);
1921 unsigned long flags
;
1923 spin_lock_irqsave(&dev
->lock
, flags
);
1926 gr_stop_activity(dev
);
1928 spin_unlock_irqrestore(&dev
->lock
, flags
);
1933 static const struct usb_gadget_ops gr_ops
= {
1934 .get_frame
= gr_get_frame
,
1935 .wakeup
= gr_wakeup
,
1936 .pullup
= gr_pullup
,
1937 .udc_start
= gr_udc_start
,
1938 .udc_stop
= gr_udc_stop
,
1939 /* Other operations not supported */
1942 /* ---------------------------------------------------------------------- */
1943 /* Module probe, removal and of-matching */
1945 static const char * const onames
[] = {
1946 "ep0out", "ep1out", "ep2out", "ep3out", "ep4out", "ep5out",
1947 "ep6out", "ep7out", "ep8out", "ep9out", "ep10out", "ep11out",
1948 "ep12out", "ep13out", "ep14out", "ep15out"
1951 static const char * const inames
[] = {
1952 "ep0in", "ep1in", "ep2in", "ep3in", "ep4in", "ep5in",
1953 "ep6in", "ep7in", "ep8in", "ep9in", "ep10in", "ep11in",
1954 "ep12in", "ep13in", "ep14in", "ep15in"
1957 /* Must be called with dev->lock held */
1958 static int gr_ep_init(struct gr_udc
*dev
, int num
, int is_in
, u32 maxplimit
)
1961 struct gr_request
*req
;
1962 struct usb_request
*_req
;
1966 ep
= &dev
->epi
[num
];
1967 ep
->ep
.name
= inames
[num
];
1968 ep
->regs
= &dev
->regs
->epi
[num
];
1970 ep
= &dev
->epo
[num
];
1971 ep
->ep
.name
= onames
[num
];
1972 ep
->regs
= &dev
->regs
->epo
[num
];
1979 ep
->ep
.ops
= &gr_ep_ops
;
1980 INIT_LIST_HEAD(&ep
->queue
);
1983 _req
= gr_alloc_request(&ep
->ep
, GFP_ATOMIC
);
1984 buf
= devm_kzalloc(dev
->dev
, PAGE_SIZE
, GFP_DMA
| GFP_ATOMIC
);
1985 if (!_req
|| !buf
) {
1986 /* possible _req freed by gr_probe via gr_remove */
1990 req
= container_of(_req
, struct gr_request
, req
);
1992 req
->req
.length
= MAX_CTRL_PL_SIZE
;
1995 dev
->ep0reqi
= req
; /* Complete gets set as used */
1997 dev
->ep0reqo
= req
; /* Completion treated separately */
1999 usb_ep_set_maxpacket_limit(&ep
->ep
, MAX_CTRL_PL_SIZE
);
2000 ep
->bytes_per_buffer
= MAX_CTRL_PL_SIZE
;
2002 ep
->ep
.caps
.type_control
= true;
2004 usb_ep_set_maxpacket_limit(&ep
->ep
, (u16
)maxplimit
);
2005 list_add_tail(&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
2007 ep
->ep
.caps
.type_iso
= true;
2008 ep
->ep
.caps
.type_bulk
= true;
2009 ep
->ep
.caps
.type_int
= true;
2011 list_add_tail(&ep
->ep_list
, &dev
->ep_list
);
2014 ep
->ep
.caps
.dir_in
= true;
2016 ep
->ep
.caps
.dir_out
= true;
2018 ep
->tailbuf
= dma_alloc_coherent(dev
->dev
, ep
->ep
.maxpacket_limit
,
2019 &ep
->tailbuf_paddr
, GFP_ATOMIC
);
2026 /* Must be called with dev->lock held */
2027 static int gr_udc_init(struct gr_udc
*dev
)
2029 struct device_node
*np
= dev
->dev
->of_node
;
2036 gr_set_address(dev
, 0);
2038 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
2039 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2040 dev
->gadget
.ep0
= &dev
->epi
[0].ep
;
2042 INIT_LIST_HEAD(&dev
->ep_list
);
2043 gr_set_ep0state(dev
, GR_EP0_DISCONNECT
);
2045 for (i
= 0; i
< dev
->nepo
; i
++) {
2046 if (of_property_read_u32_index(np
, "epobufsizes", i
, &bufsize
))
2048 ret
= gr_ep_init(dev
, i
, 0, bufsize
);
2053 for (i
= 0; i
< dev
->nepi
; i
++) {
2054 if (of_property_read_u32_index(np
, "epibufsizes", i
, &bufsize
))
2056 ret
= gr_ep_init(dev
, i
, 1, bufsize
);
2061 /* Must be disabled by default */
2062 dev
->remote_wakeup
= 0;
2064 /* Enable ep0out and ep0in */
2065 epctrl_val
= (MAX_CTRL_PL_SIZE
<< GR_EPCTRL_MAXPL_POS
) | GR_EPCTRL_EV
;
2066 dmactrl_val
= GR_DMACTRL_IE
| GR_DMACTRL_AI
;
2067 gr_write32(&dev
->epo
[0].regs
->epctrl
, epctrl_val
);
2068 gr_write32(&dev
->epi
[0].regs
->epctrl
, epctrl_val
| GR_EPCTRL_PI
);
2069 gr_write32(&dev
->epo
[0].regs
->dmactrl
, dmactrl_val
);
2070 gr_write32(&dev
->epi
[0].regs
->dmactrl
, dmactrl_val
);
2075 static void gr_ep_remove(struct gr_udc
*dev
, int num
, int is_in
)
2080 ep
= &dev
->epi
[num
];
2082 ep
= &dev
->epo
[num
];
2085 dma_free_coherent(dev
->dev
, ep
->ep
.maxpacket_limit
,
2086 ep
->tailbuf
, ep
->tailbuf_paddr
);
2089 static int gr_remove(struct platform_device
*pdev
)
2091 struct gr_udc
*dev
= platform_get_drvdata(pdev
);
2095 usb_del_gadget_udc(&dev
->gadget
); /* Shuts everything down */
2100 dma_pool_destroy(dev
->desc_pool
);
2101 platform_set_drvdata(pdev
, NULL
);
2103 gr_free_request(&dev
->epi
[0].ep
, &dev
->ep0reqi
->req
);
2104 gr_free_request(&dev
->epo
[0].ep
, &dev
->ep0reqo
->req
);
2106 for (i
= 0; i
< dev
->nepo
; i
++)
2107 gr_ep_remove(dev
, i
, 0);
2108 for (i
= 0; i
< dev
->nepi
; i
++)
2109 gr_ep_remove(dev
, i
, 1);
2113 static int gr_request_irq(struct gr_udc
*dev
, int irq
)
2115 return devm_request_threaded_irq(dev
->dev
, irq
, gr_irq
, gr_irq_handler
,
2116 IRQF_SHARED
, driver_name
, dev
);
2119 static int gr_probe(struct platform_device
*pdev
)
2122 struct gr_regs __iomem
*regs
;
2126 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
2129 dev
->dev
= &pdev
->dev
;
2131 regs
= devm_platform_ioremap_resource(pdev
, 0);
2133 return PTR_ERR(regs
);
2135 dev
->irq
= platform_get_irq(pdev
, 0);
2139 /* Some core configurations has separate irqs for IN and OUT events */
2140 dev
->irqi
= platform_get_irq(pdev
, 1);
2141 if (dev
->irqi
> 0) {
2142 dev
->irqo
= platform_get_irq(pdev
, 2);
2149 dev
->gadget
.name
= driver_name
;
2150 dev
->gadget
.max_speed
= USB_SPEED_HIGH
;
2151 dev
->gadget
.ops
= &gr_ops
;
2153 spin_lock_init(&dev
->lock
);
2156 platform_set_drvdata(pdev
, dev
);
2158 /* Determine number of endpoints and data interface mode */
2159 status
= gr_read32(&dev
->regs
->status
);
2160 dev
->nepi
= ((status
& GR_STATUS_NEPI_MASK
) >> GR_STATUS_NEPI_POS
) + 1;
2161 dev
->nepo
= ((status
& GR_STATUS_NEPO_MASK
) >> GR_STATUS_NEPO_POS
) + 1;
2163 if (!(status
& GR_STATUS_DM
)) {
2164 dev_err(dev
->dev
, "Slave mode cores are not supported\n");
2168 /* --- Effects of the following calls might need explicit cleanup --- */
2170 /* Create DMA pool for descriptors */
2171 dev
->desc_pool
= dma_pool_create("desc_pool", dev
->dev
,
2172 sizeof(struct gr_dma_desc
), 4, 0);
2173 if (!dev
->desc_pool
) {
2174 dev_err(dev
->dev
, "Could not allocate DMA pool");
2178 /* Inside lock so that no gadget can use this udc until probe is done */
2179 retval
= usb_add_gadget_udc(dev
->dev
, &dev
->gadget
);
2181 dev_err(dev
->dev
, "Could not add gadget udc");
2186 spin_lock(&dev
->lock
);
2188 retval
= gr_udc_init(dev
);
2190 spin_unlock(&dev
->lock
);
2194 /* Clear all interrupt enables that might be left on since last boot */
2195 gr_disable_interrupts_and_pullup(dev
);
2197 spin_unlock(&dev
->lock
);
2201 retval
= gr_request_irq(dev
, dev
->irq
);
2203 dev_err(dev
->dev
, "Failed to request irq %d\n", dev
->irq
);
2208 retval
= gr_request_irq(dev
, dev
->irqi
);
2210 dev_err(dev
->dev
, "Failed to request irqi %d\n",
2214 retval
= gr_request_irq(dev
, dev
->irqo
);
2216 dev_err(dev
->dev
, "Failed to request irqo %d\n",
2223 dev_info(dev
->dev
, "regs: %p, irqs %d, %d, %d\n", dev
->regs
,
2224 dev
->irq
, dev
->irqi
, dev
->irqo
);
2226 dev_info(dev
->dev
, "regs: %p, irq %d\n", dev
->regs
, dev
->irq
);
2235 static const struct of_device_id gr_match
[] = {
2236 {.name
= "GAISLER_USBDC"},
2240 MODULE_DEVICE_TABLE(of
, gr_match
);
2242 static struct platform_driver gr_driver
= {
2244 .name
= DRIVER_NAME
,
2245 .of_match_table
= gr_match
,
2248 .remove
= gr_remove
,
2250 module_platform_driver(gr_driver
);
2252 MODULE_AUTHOR("Aeroflex Gaisler AB.");
2253 MODULE_DESCRIPTION(DRIVER_DESC
);
2254 MODULE_LICENSE("GPL");