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/ch9.h>
33 #include <linux/usb/gadget.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/dmapool.h>
36 #include <linux/debugfs.h>
37 #include <linux/seq_file.h>
38 #include <linux/of_platform.h>
39 #include <linux/of_irq.h>
40 #include <linux/of_address.h>
42 #include <asm/byteorder.h>
46 #define DRIVER_NAME "gr_udc"
47 #define DRIVER_DESC "Aeroflex Gaisler GRUSBDC USB Peripheral Controller"
49 static const char driver_name
[] = DRIVER_NAME
;
50 static const char driver_desc
[] = DRIVER_DESC
;
52 #define gr_read32(x) (ioread32be((x)))
53 #define gr_write32(x, v) (iowrite32be((v), (x)))
55 /* USB speed and corresponding string calculated from status register value */
56 #define GR_SPEED(status) \
57 ((status & GR_STATUS_SP) ? USB_SPEED_FULL : USB_SPEED_HIGH)
58 #define GR_SPEED_STR(status) usb_speed_string(GR_SPEED(status))
60 /* Size of hardware buffer calculated from epctrl register value */
61 #define GR_BUFFER_SIZE(epctrl) \
62 ((((epctrl) & GR_EPCTRL_BUFSZ_MASK) >> GR_EPCTRL_BUFSZ_POS) * \
63 GR_EPCTRL_BUFSZ_SCALER)
65 /* ---------------------------------------------------------------------- */
66 /* Debug printout functionality */
68 static const char * const gr_modestring
[] = {"control", "iso", "bulk", "int"};
70 static const char *gr_ep0state_string(enum gr_ep0state state
)
72 static const char *const names
[] = {
73 [GR_EP0_DISCONNECT
] = "disconnect",
74 [GR_EP0_SETUP
] = "setup",
75 [GR_EP0_IDATA
] = "idata",
76 [GR_EP0_ODATA
] = "odata",
77 [GR_EP0_ISTATUS
] = "istatus",
78 [GR_EP0_OSTATUS
] = "ostatus",
79 [GR_EP0_STALL
] = "stall",
80 [GR_EP0_SUSPEND
] = "suspend",
83 if (state
< 0 || state
>= ARRAY_SIZE(names
))
91 static void gr_dbgprint_request(const char *str
, struct gr_ep
*ep
,
92 struct gr_request
*req
)
94 int buflen
= ep
->is_in
? req
->req
.length
: req
->req
.actual
;
96 int plen
= min(rowlen
, buflen
);
98 dev_dbg(ep
->dev
->dev
, "%s: 0x%p, %d bytes data%s:\n", str
, req
, buflen
,
99 (buflen
> plen
? " (truncated)" : ""));
100 print_hex_dump_debug(" ", DUMP_PREFIX_NONE
,
101 rowlen
, 4, req
->req
.buf
, plen
, false);
104 static void gr_dbgprint_devreq(struct gr_udc
*dev
, u8 type
, u8 request
,
105 u16 value
, u16 index
, u16 length
)
107 dev_vdbg(dev
->dev
, "REQ: %02x.%02x v%04x i%04x l%04x\n",
108 type
, request
, value
, index
, length
);
110 #else /* !VERBOSE_DEBUG */
112 static void gr_dbgprint_request(const char *str
, struct gr_ep
*ep
,
113 struct gr_request
*req
) {}
115 static void gr_dbgprint_devreq(struct gr_udc
*dev
, u8 type
, u8 request
,
116 u16 value
, u16 index
, u16 length
) {}
118 #endif /* VERBOSE_DEBUG */
120 /* ---------------------------------------------------------------------- */
121 /* Debugfs functionality */
123 #ifdef CONFIG_USB_GADGET_DEBUG_FS
125 static void gr_seq_ep_show(struct seq_file
*seq
, struct gr_ep
*ep
)
127 u32 epctrl
= gr_read32(&ep
->regs
->epctrl
);
128 u32 epstat
= gr_read32(&ep
->regs
->epstat
);
129 int mode
= (epctrl
& GR_EPCTRL_TT_MASK
) >> GR_EPCTRL_TT_POS
;
130 struct gr_request
*req
;
132 seq_printf(seq
, "%s:\n", ep
->ep
.name
);
133 seq_printf(seq
, " mode = %s\n", gr_modestring
[mode
]);
134 seq_printf(seq
, " halted: %d\n", !!(epctrl
& GR_EPCTRL_EH
));
135 seq_printf(seq
, " disabled: %d\n", !!(epctrl
& GR_EPCTRL_ED
));
136 seq_printf(seq
, " valid: %d\n", !!(epctrl
& GR_EPCTRL_EV
));
137 seq_printf(seq
, " dma_start = %d\n", ep
->dma_start
);
138 seq_printf(seq
, " stopped = %d\n", ep
->stopped
);
139 seq_printf(seq
, " wedged = %d\n", ep
->wedged
);
140 seq_printf(seq
, " callback = %d\n", ep
->callback
);
141 seq_printf(seq
, " maxpacket = %d\n", ep
->ep
.maxpacket
);
142 seq_printf(seq
, " maxpacket_limit = %d\n", ep
->ep
.maxpacket_limit
);
143 seq_printf(seq
, " bytes_per_buffer = %d\n", ep
->bytes_per_buffer
);
144 if (mode
== 1 || mode
== 3)
145 seq_printf(seq
, " nt = %d\n",
146 (epctrl
& GR_EPCTRL_NT_MASK
) >> GR_EPCTRL_NT_POS
);
148 seq_printf(seq
, " Buffer 0: %s %s%d\n",
149 epstat
& GR_EPSTAT_B0
? "valid" : "invalid",
150 epstat
& GR_EPSTAT_BS
? " " : "selected ",
151 (epstat
& GR_EPSTAT_B0CNT_MASK
) >> GR_EPSTAT_B0CNT_POS
);
152 seq_printf(seq
, " Buffer 1: %s %s%d\n",
153 epstat
& GR_EPSTAT_B1
? "valid" : "invalid",
154 epstat
& GR_EPSTAT_BS
? "selected " : " ",
155 (epstat
& GR_EPSTAT_B1CNT_MASK
) >> GR_EPSTAT_B1CNT_POS
);
157 if (list_empty(&ep
->queue
)) {
158 seq_puts(seq
, " Queue: empty\n\n");
162 seq_puts(seq
, " Queue:\n");
163 list_for_each_entry(req
, &ep
->queue
, queue
) {
164 struct gr_dma_desc
*desc
;
165 struct gr_dma_desc
*next
;
167 seq_printf(seq
, " 0x%p: 0x%p %d %d\n", req
,
168 &req
->req
.buf
, req
->req
.actual
, req
->req
.length
);
170 next
= req
->first_desc
;
173 next
= desc
->next_desc
;
174 seq_printf(seq
, " %c 0x%p (0x%08x): 0x%05x 0x%08x\n",
175 desc
== req
->curr_desc
? 'c' : ' ',
176 desc
, desc
->paddr
, desc
->ctrl
, desc
->data
);
177 } while (desc
!= req
->last_desc
);
183 static int gr_seq_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
);
207 static int gr_dfs_open(struct inode
*inode
, struct file
*file
)
209 return single_open(file
, gr_seq_show
, inode
->i_private
);
212 static const struct file_operations gr_dfs_fops
= {
213 .owner
= THIS_MODULE
,
217 .release
= single_release
,
220 static void gr_dfs_create(struct gr_udc
*dev
)
222 const char *name
= "gr_udc_state";
224 dev
->dfs_root
= debugfs_create_dir(dev_name(dev
->dev
), NULL
);
225 dev
->dfs_state
= debugfs_create_file(name
, 0444, dev
->dfs_root
, dev
,
229 static void gr_dfs_delete(struct gr_udc
*dev
)
231 /* Handles NULL and ERR pointers internally */
232 debugfs_remove(dev
->dfs_state
);
233 debugfs_remove(dev
->dfs_root
);
236 #else /* !CONFIG_USB_GADGET_DEBUG_FS */
238 static void gr_dfs_create(struct gr_udc
*dev
) {}
239 static void gr_dfs_delete(struct gr_udc
*dev
) {}
241 #endif /* CONFIG_USB_GADGET_DEBUG_FS */
243 /* ---------------------------------------------------------------------- */
244 /* DMA and request handling */
246 /* Allocates a new struct gr_dma_desc, sets paddr and zeroes the rest */
247 static struct gr_dma_desc
*gr_alloc_dma_desc(struct gr_ep
*ep
, gfp_t gfp_flags
)
250 struct gr_dma_desc
*dma_desc
;
252 dma_desc
= dma_pool_zalloc(ep
->dev
->desc_pool
, gfp_flags
, &paddr
);
254 dev_err(ep
->dev
->dev
, "Could not allocate from DMA pool\n");
258 dma_desc
->paddr
= paddr
;
263 static inline void gr_free_dma_desc(struct gr_udc
*dev
,
264 struct gr_dma_desc
*desc
)
266 dma_pool_free(dev
->desc_pool
, desc
, (dma_addr_t
)desc
->paddr
);
269 /* Frees the chain of struct gr_dma_desc for the given request */
270 static void gr_free_dma_desc_chain(struct gr_udc
*dev
, struct gr_request
*req
)
272 struct gr_dma_desc
*desc
;
273 struct gr_dma_desc
*next
;
275 next
= req
->first_desc
;
281 next
= desc
->next_desc
;
282 gr_free_dma_desc(dev
, desc
);
283 } while (desc
!= req
->last_desc
);
285 req
->first_desc
= NULL
;
286 req
->curr_desc
= NULL
;
287 req
->last_desc
= NULL
;
290 static void gr_ep0_setup(struct gr_udc
*dev
, struct gr_request
*req
);
293 * Frees allocated resources and calls the appropriate completion function/setup
294 * package handler for a finished request.
296 * Must be called with dev->lock held and irqs disabled.
298 static void gr_finish_request(struct gr_ep
*ep
, struct gr_request
*req
,
300 __releases(&dev
->lock
)
301 __acquires(&dev
->lock
)
305 list_del_init(&req
->queue
);
307 if (likely(req
->req
.status
== -EINPROGRESS
))
308 req
->req
.status
= status
;
310 status
= req
->req
.status
;
313 usb_gadget_unmap_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
314 gr_free_dma_desc_chain(dev
, req
);
316 if (ep
->is_in
) { /* For OUT, req->req.actual gets updated bit by bit */
317 req
->req
.actual
= req
->req
.length
;
318 } else if (req
->oddlen
&& req
->req
.actual
> req
->evenlen
) {
320 * Copy to user buffer in this case where length was not evenly
321 * divisible by ep->ep.maxpacket and the last descriptor was
324 char *buftail
= ((char *)req
->req
.buf
+ req
->evenlen
);
326 memcpy(buftail
, ep
->tailbuf
, req
->oddlen
);
328 if (req
->req
.actual
> req
->req
.length
) {
329 /* We got more data than was requested */
330 dev_dbg(ep
->dev
->dev
, "Overflow for ep %s\n",
332 gr_dbgprint_request("OVFL", ep
, req
);
333 req
->req
.status
= -EOVERFLOW
;
339 gr_dbgprint_request("SENT", ep
, req
);
341 gr_dbgprint_request("RECV", ep
, req
);
344 /* Prevent changes to ep->queue during callback */
346 if (req
== dev
->ep0reqo
&& !status
) {
348 gr_ep0_setup(dev
, req
);
351 "Unexpected non setup packet on ep0in\n");
352 } else if (req
->req
.complete
) {
353 spin_unlock(&dev
->lock
);
355 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
357 spin_lock(&dev
->lock
);
362 static struct usb_request
*gr_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
364 struct gr_request
*req
;
366 req
= kzalloc(sizeof(*req
), gfp_flags
);
370 INIT_LIST_HEAD(&req
->queue
);
376 * Starts DMA for endpoint ep if there are requests in the queue.
378 * Must be called with dev->lock held and with !ep->stopped.
380 static void gr_start_dma(struct gr_ep
*ep
)
382 struct gr_request
*req
;
385 if (list_empty(&ep
->queue
)) {
390 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
392 /* A descriptor should already have been allocated */
393 BUG_ON(!req
->curr_desc
);
396 * The DMA controller can not handle smaller OUT buffers than
397 * ep->ep.maxpacket. It could lead to buffer overruns if an unexpectedly
398 * long packet are received. Therefore an internal bounce buffer gets
399 * used when such a request gets enabled.
401 if (!ep
->is_in
&& req
->oddlen
)
402 req
->last_desc
->data
= ep
->tailbuf_paddr
;
404 wmb(); /* Make sure all is settled before handing it over to DMA */
406 /* Set the descriptor pointer in the hardware */
407 gr_write32(&ep
->regs
->dmaaddr
, req
->curr_desc
->paddr
);
409 /* Announce available descriptors */
410 dmactrl
= gr_read32(&ep
->regs
->dmactrl
);
411 gr_write32(&ep
->regs
->dmactrl
, dmactrl
| GR_DMACTRL_DA
);
417 * Finishes the first request in the ep's queue and, if available, starts the
418 * next request in queue.
420 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
422 static void gr_dma_advance(struct gr_ep
*ep
, int status
)
424 struct gr_request
*req
;
426 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
427 gr_finish_request(ep
, req
, status
);
428 gr_start_dma(ep
); /* Regardless of ep->dma_start */
432 * Abort DMA for an endpoint. Sets the abort DMA bit which causes an ongoing DMA
433 * transfer to be canceled and clears GR_DMACTRL_DA.
435 * Must be called with dev->lock held.
437 static void gr_abort_dma(struct gr_ep
*ep
)
441 dmactrl
= gr_read32(&ep
->regs
->dmactrl
);
442 gr_write32(&ep
->regs
->dmactrl
, dmactrl
| GR_DMACTRL_AD
);
446 * Allocates and sets up a struct gr_dma_desc and putting it on the descriptor
449 * Size is not used for OUT endpoints. Hardware can not be instructed to handle
450 * smaller buffer than MAXPL in the OUT direction.
452 static int gr_add_dma_desc(struct gr_ep
*ep
, struct gr_request
*req
,
453 dma_addr_t data
, unsigned size
, gfp_t gfp_flags
)
455 struct gr_dma_desc
*desc
;
457 desc
= gr_alloc_dma_desc(ep
, gfp_flags
);
464 (GR_DESC_IN_CTRL_LEN_MASK
& size
) | GR_DESC_IN_CTRL_EN
;
466 desc
->ctrl
= GR_DESC_OUT_CTRL_IE
;
468 if (!req
->first_desc
) {
469 req
->first_desc
= desc
;
470 req
->curr_desc
= desc
;
472 req
->last_desc
->next_desc
= desc
;
473 req
->last_desc
->next
= desc
->paddr
;
474 req
->last_desc
->ctrl
|= GR_DESC_OUT_CTRL_NX
;
476 req
->last_desc
= desc
;
482 * Sets up a chain of struct gr_dma_descriptors pointing to buffers that
483 * together covers req->req.length bytes of the buffer at DMA address
484 * req->req.dma for the OUT direction.
486 * The first descriptor in the chain is enabled, the rest disabled. The
487 * interrupt handler will later enable them one by one when needed so we can
488 * find out when the transfer is finished. For OUT endpoints, all descriptors
489 * therefore generate interrutps.
491 static int gr_setup_out_desc_list(struct gr_ep
*ep
, struct gr_request
*req
,
494 u16 bytes_left
; /* Bytes left to provide descriptors for */
495 u16 bytes_used
; /* Bytes accommodated for */
498 req
->first_desc
= NULL
; /* Signals that no allocation is done yet */
499 bytes_left
= req
->req
.length
;
501 while (bytes_left
> 0) {
502 dma_addr_t start
= req
->req
.dma
+ bytes_used
;
503 u16 size
= min(bytes_left
, ep
->bytes_per_buffer
);
505 if (size
< ep
->bytes_per_buffer
) {
506 /* Prepare using bounce buffer */
507 req
->evenlen
= req
->req
.length
- bytes_left
;
511 ret
= gr_add_dma_desc(ep
, req
, start
, size
, gfp_flags
);
519 req
->first_desc
->ctrl
|= GR_DESC_OUT_CTRL_EN
;
524 gr_free_dma_desc_chain(ep
->dev
, req
);
530 * Sets up a chain of struct gr_dma_descriptors pointing to buffers that
531 * together covers req->req.length bytes of the buffer at DMA address
532 * req->req.dma for the IN direction.
534 * When more data is provided than the maximum payload size, the hardware splits
535 * this up into several payloads automatically. Moreover, ep->bytes_per_buffer
536 * is always set to a multiple of the maximum payload (restricted to the valid
537 * number of maximum payloads during high bandwidth isochronous or interrupt
540 * All descriptors are enabled from the beginning and we only generate an
541 * interrupt for the last one indicating that the entire request has been pushed
544 static int gr_setup_in_desc_list(struct gr_ep
*ep
, struct gr_request
*req
,
547 u16 bytes_left
; /* Bytes left in req to provide descriptors for */
548 u16 bytes_used
; /* Bytes in req accommodated for */
551 req
->first_desc
= NULL
; /* Signals that no allocation is done yet */
552 bytes_left
= req
->req
.length
;
554 do { /* Allow for zero length packets */
555 dma_addr_t start
= req
->req
.dma
+ bytes_used
;
556 u16 size
= min(bytes_left
, ep
->bytes_per_buffer
);
558 ret
= gr_add_dma_desc(ep
, req
, start
, size
, gfp_flags
);
564 } while (bytes_left
> 0);
567 * Send an extra zero length packet to indicate that no more data is
568 * available when req->req.zero is set and the data length is even
569 * multiples of ep->ep.maxpacket.
571 if (req
->req
.zero
&& (req
->req
.length
% ep
->ep
.maxpacket
== 0)) {
572 ret
= gr_add_dma_desc(ep
, req
, 0, 0, gfp_flags
);
578 * For IN packets we only want to know when the last packet has been
579 * transmitted (not just put into internal buffers).
581 req
->last_desc
->ctrl
|= GR_DESC_IN_CTRL_PI
;
586 gr_free_dma_desc_chain(ep
->dev
, req
);
591 /* Must be called with dev->lock held */
592 static int gr_queue(struct gr_ep
*ep
, struct gr_request
*req
, gfp_t gfp_flags
)
594 struct gr_udc
*dev
= ep
->dev
;
597 if (unlikely(!ep
->ep
.desc
&& ep
->num
!= 0)) {
598 dev_err(dev
->dev
, "No ep descriptor for %s\n", ep
->ep
.name
);
602 if (unlikely(!req
->req
.buf
|| !list_empty(&req
->queue
))) {
604 "Invalid request for %s: buf=%p list_empty=%d\n",
605 ep
->ep
.name
, req
->req
.buf
, list_empty(&req
->queue
));
609 if (unlikely(!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
610 dev_err(dev
->dev
, "-ESHUTDOWN");
614 /* Can't touch registers when suspended */
615 if (dev
->ep0state
== GR_EP0_SUSPEND
) {
616 dev_err(dev
->dev
, "-EBUSY");
620 /* Set up DMA mapping in case the caller didn't */
621 ret
= usb_gadget_map_request(&dev
->gadget
, &req
->req
, ep
->is_in
);
623 dev_err(dev
->dev
, "usb_gadget_map_request");
628 ret
= gr_setup_in_desc_list(ep
, req
, gfp_flags
);
630 ret
= gr_setup_out_desc_list(ep
, req
, gfp_flags
);
634 req
->req
.status
= -EINPROGRESS
;
636 list_add_tail(&req
->queue
, &ep
->queue
);
638 /* Start DMA if not started, otherwise interrupt handler handles it */
639 if (!ep
->dma_start
&& likely(!ep
->stopped
))
646 * Queue a request from within the driver.
648 * Must be called with dev->lock held.
650 static inline int gr_queue_int(struct gr_ep
*ep
, struct gr_request
*req
,
654 gr_dbgprint_request("RESP", ep
, req
);
656 return gr_queue(ep
, req
, gfp_flags
);
659 /* ---------------------------------------------------------------------- */
660 /* General helper functions */
663 * Dequeue ALL requests.
665 * Must be called with dev->lock held and irqs disabled.
667 static void gr_ep_nuke(struct gr_ep
*ep
)
669 struct gr_request
*req
;
675 while (!list_empty(&ep
->queue
)) {
676 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
677 gr_finish_request(ep
, req
, -ESHUTDOWN
);
682 * Reset the hardware state of this endpoint.
684 * Must be called with dev->lock held.
686 static void gr_ep_reset(struct gr_ep
*ep
)
688 gr_write32(&ep
->regs
->epctrl
, 0);
689 gr_write32(&ep
->regs
->dmactrl
, 0);
691 ep
->ep
.maxpacket
= MAX_CTRL_PL_SIZE
;
698 * Generate STALL on ep0in/out.
700 * Must be called with dev->lock held.
702 static void gr_control_stall(struct gr_udc
*dev
)
706 epctrl
= gr_read32(&dev
->epo
[0].regs
->epctrl
);
707 gr_write32(&dev
->epo
[0].regs
->epctrl
, epctrl
| GR_EPCTRL_CS
);
708 epctrl
= gr_read32(&dev
->epi
[0].regs
->epctrl
);
709 gr_write32(&dev
->epi
[0].regs
->epctrl
, epctrl
| GR_EPCTRL_CS
);
711 dev
->ep0state
= GR_EP0_STALL
;
715 * Halts, halts and wedges, or clears halt for an endpoint.
717 * Must be called with dev->lock held.
719 static int gr_ep_halt_wedge(struct gr_ep
*ep
, int halt
, int wedge
, int fromhost
)
724 if (ep
->num
&& !ep
->ep
.desc
)
727 if (ep
->num
&& ep
->ep
.desc
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
)
730 /* Never actually halt ep0, and therefore never clear halt for ep0 */
732 if (halt
&& !fromhost
) {
733 /* ep0 halt from gadget - generate protocol stall */
734 gr_control_stall(ep
->dev
);
735 dev_dbg(ep
->dev
->dev
, "EP: stall ep0\n");
741 dev_dbg(ep
->dev
->dev
, "EP: %s halt %s\n",
742 (halt
? (wedge
? "wedge" : "set") : "clear"), ep
->ep
.name
);
744 epctrl
= gr_read32(&ep
->regs
->epctrl
);
747 gr_write32(&ep
->regs
->epctrl
, epctrl
| GR_EPCTRL_EH
);
752 gr_write32(&ep
->regs
->epctrl
, epctrl
& ~GR_EPCTRL_EH
);
756 /* Things might have been queued up in the meantime */
764 /* Must be called with dev->lock held */
765 static inline void gr_set_ep0state(struct gr_udc
*dev
, enum gr_ep0state value
)
767 if (dev
->ep0state
!= value
)
768 dev_vdbg(dev
->dev
, "STATE: ep0state=%s\n",
769 gr_ep0state_string(value
));
770 dev
->ep0state
= value
;
774 * Should only be called when endpoints can not generate interrupts.
776 * Must be called with dev->lock held.
778 static void gr_disable_interrupts_and_pullup(struct gr_udc
*dev
)
780 gr_write32(&dev
->regs
->control
, 0);
781 wmb(); /* Make sure that we do not deny one of our interrupts */
782 dev
->irq_enabled
= 0;
786 * Stop all device activity and disable data line pullup.
788 * Must be called with dev->lock held and irqs disabled.
790 static void gr_stop_activity(struct gr_udc
*dev
)
794 list_for_each_entry(ep
, &dev
->ep_list
, ep_list
)
797 gr_disable_interrupts_and_pullup(dev
);
799 gr_set_ep0state(dev
, GR_EP0_DISCONNECT
);
800 usb_gadget_set_state(&dev
->gadget
, USB_STATE_NOTATTACHED
);
803 /* ---------------------------------------------------------------------- */
804 /* ep0 setup packet handling */
806 static void gr_ep0_testmode_complete(struct usb_ep
*_ep
,
807 struct usb_request
*_req
)
813 ep
= container_of(_ep
, struct gr_ep
, ep
);
816 spin_lock(&dev
->lock
);
818 control
= gr_read32(&dev
->regs
->control
);
819 control
|= GR_CONTROL_TM
| (dev
->test_mode
<< GR_CONTROL_TS_POS
);
820 gr_write32(&dev
->regs
->control
, control
);
822 spin_unlock(&dev
->lock
);
825 static void gr_ep0_dummy_complete(struct usb_ep
*_ep
, struct usb_request
*_req
)
827 /* Nothing needs to be done here */
831 * Queue a response on ep0in.
833 * Must be called with dev->lock held.
835 static int gr_ep0_respond(struct gr_udc
*dev
, u8
*buf
, int length
,
836 void (*complete
)(struct usb_ep
*ep
,
837 struct usb_request
*req
))
839 u8
*reqbuf
= dev
->ep0reqi
->req
.buf
;
843 for (i
= 0; i
< length
; i
++)
845 dev
->ep0reqi
->req
.length
= length
;
846 dev
->ep0reqi
->req
.complete
= complete
;
848 status
= gr_queue_int(&dev
->epi
[0], dev
->ep0reqi
, GFP_ATOMIC
);
851 "Could not queue ep0in setup response: %d\n", status
);
857 * Queue a 2 byte response on ep0in.
859 * Must be called with dev->lock held.
861 static inline int gr_ep0_respond_u16(struct gr_udc
*dev
, u16 response
)
863 __le16 le_response
= cpu_to_le16(response
);
865 return gr_ep0_respond(dev
, (u8
*)&le_response
, 2,
866 gr_ep0_dummy_complete
);
870 * Queue a ZLP response on ep0in.
872 * Must be called with dev->lock held.
874 static inline int gr_ep0_respond_empty(struct gr_udc
*dev
)
876 return gr_ep0_respond(dev
, NULL
, 0, gr_ep0_dummy_complete
);
880 * This is run when a SET_ADDRESS request is received. First writes
881 * the new address to the control register which is updated internally
882 * when the next IN packet is ACKED.
884 * Must be called with dev->lock held.
886 static void gr_set_address(struct gr_udc
*dev
, u8 address
)
890 control
= gr_read32(&dev
->regs
->control
) & ~GR_CONTROL_UA_MASK
;
891 control
|= (address
<< GR_CONTROL_UA_POS
) & GR_CONTROL_UA_MASK
;
892 control
|= GR_CONTROL_SU
;
893 gr_write32(&dev
->regs
->control
, control
);
897 * Returns negative for STALL, 0 for successful handling and positive for
900 * Must be called with dev->lock held.
902 static int gr_device_request(struct gr_udc
*dev
, u8 type
, u8 request
,
903 u16 value
, u16 index
)
909 case USB_REQ_SET_ADDRESS
:
910 dev_dbg(dev
->dev
, "STATUS: address %d\n", value
& 0xff);
911 gr_set_address(dev
, value
& 0xff);
913 usb_gadget_set_state(&dev
->gadget
, USB_STATE_ADDRESS
);
915 usb_gadget_set_state(&dev
->gadget
, USB_STATE_DEFAULT
);
916 return gr_ep0_respond_empty(dev
);
918 case USB_REQ_GET_STATUS
:
919 /* Self powered | remote wakeup */
920 response
= 0x0001 | (dev
->remote_wakeup
? 0x0002 : 0);
921 return gr_ep0_respond_u16(dev
, response
);
923 case USB_REQ_SET_FEATURE
:
925 case USB_DEVICE_REMOTE_WAKEUP
:
926 /* Allow remote wakeup */
927 dev
->remote_wakeup
= 1;
928 return gr_ep0_respond_empty(dev
);
930 case USB_DEVICE_TEST_MODE
:
931 /* The hardware does not support TEST_FORCE_EN */
933 if (test
>= TEST_J
&& test
<= TEST_PACKET
) {
934 dev
->test_mode
= test
;
935 return gr_ep0_respond(dev
, NULL
, 0,
936 gr_ep0_testmode_complete
);
941 case USB_REQ_CLEAR_FEATURE
:
943 case USB_DEVICE_REMOTE_WAKEUP
:
944 /* Disallow remote wakeup */
945 dev
->remote_wakeup
= 0;
946 return gr_ep0_respond_empty(dev
);
951 return 1; /* Delegate the rest */
955 * Returns negative for STALL, 0 for successful handling and positive for
958 * Must be called with dev->lock held.
960 static int gr_interface_request(struct gr_udc
*dev
, u8 type
, u8 request
,
961 u16 value
, u16 index
)
963 if (dev
->gadget
.state
!= USB_STATE_CONFIGURED
)
967 * Should return STALL for invalid interfaces, but udc driver does not
968 * know anything about that. However, many gadget drivers do not handle
969 * GET_STATUS so we need to take care of that.
973 case USB_REQ_GET_STATUS
:
974 return gr_ep0_respond_u16(dev
, 0x0000);
976 case USB_REQ_SET_FEATURE
:
977 case USB_REQ_CLEAR_FEATURE
:
979 * No possible valid standard requests. Still let gadget drivers
985 return 1; /* Delegate the rest */
989 * Returns negative for STALL, 0 for successful handling and positive for
992 * Must be called with dev->lock held.
994 static int gr_endpoint_request(struct gr_udc
*dev
, u8 type
, u8 request
,
995 u16 value
, u16 index
)
1000 u8 epnum
= index
& USB_ENDPOINT_NUMBER_MASK
;
1001 u8 is_in
= index
& USB_ENDPOINT_DIR_MASK
;
1003 if ((is_in
&& epnum
>= dev
->nepi
) || (!is_in
&& epnum
>= dev
->nepo
))
1006 if (dev
->gadget
.state
!= USB_STATE_CONFIGURED
&& epnum
!= 0)
1009 ep
= (is_in
? &dev
->epi
[epnum
] : &dev
->epo
[epnum
]);
1012 case USB_REQ_GET_STATUS
:
1013 halted
= gr_read32(&ep
->regs
->epctrl
) & GR_EPCTRL_EH
;
1014 return gr_ep0_respond_u16(dev
, halted
? 0x0001 : 0);
1016 case USB_REQ_SET_FEATURE
:
1018 case USB_ENDPOINT_HALT
:
1019 status
= gr_ep_halt_wedge(ep
, 1, 0, 1);
1021 status
= gr_ep0_respond_empty(dev
);
1026 case USB_REQ_CLEAR_FEATURE
:
1028 case USB_ENDPOINT_HALT
:
1031 status
= gr_ep_halt_wedge(ep
, 0, 0, 1);
1033 status
= gr_ep0_respond_empty(dev
);
1039 return 1; /* Delegate the rest */
1042 /* Must be called with dev->lock held */
1043 static void gr_ep0out_requeue(struct gr_udc
*dev
)
1045 int ret
= gr_queue_int(&dev
->epo
[0], dev
->ep0reqo
, GFP_ATOMIC
);
1048 dev_err(dev
->dev
, "Could not queue ep0out setup request: %d\n",
1053 * The main function dealing with setup requests on ep0.
1055 * Must be called with dev->lock held and irqs disabled
1057 static void gr_ep0_setup(struct gr_udc
*dev
, struct gr_request
*req
)
1058 __releases(&dev
->lock
)
1059 __acquires(&dev
->lock
)
1062 struct usb_ctrlrequest ctrl
;
1074 /* Restore from ep0 halt */
1075 if (dev
->ep0state
== GR_EP0_STALL
) {
1076 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1077 if (!req
->req
.actual
)
1081 if (dev
->ep0state
== GR_EP0_ISTATUS
) {
1082 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1083 if (req
->req
.actual
> 0)
1085 "Unexpected setup packet at state %s\n",
1086 gr_ep0state_string(GR_EP0_ISTATUS
));
1088 goto out
; /* Got expected ZLP */
1089 } else if (dev
->ep0state
!= GR_EP0_SETUP
) {
1091 "Unexpected ep0out request at state %s - stalling\n",
1092 gr_ep0state_string(dev
->ep0state
));
1093 gr_control_stall(dev
);
1094 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1096 } else if (!req
->req
.actual
) {
1097 dev_dbg(dev
->dev
, "Unexpected ZLP at state %s\n",
1098 gr_ep0state_string(dev
->ep0state
));
1102 /* Handle SETUP packet */
1103 for (i
= 0; i
< req
->req
.actual
; i
++)
1104 u
.raw
[i
] = ((u8
*)req
->req
.buf
)[i
];
1106 type
= u
.ctrl
.bRequestType
;
1107 request
= u
.ctrl
.bRequest
;
1108 value
= le16_to_cpu(u
.ctrl
.wValue
);
1109 index
= le16_to_cpu(u
.ctrl
.wIndex
);
1110 length
= le16_to_cpu(u
.ctrl
.wLength
);
1112 gr_dbgprint_devreq(dev
, type
, request
, value
, index
, length
);
1114 /* Check for data stage */
1116 if (type
& USB_DIR_IN
)
1117 gr_set_ep0state(dev
, GR_EP0_IDATA
);
1119 gr_set_ep0state(dev
, GR_EP0_ODATA
);
1122 status
= 1; /* Positive status flags delegation */
1123 if ((type
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1124 switch (type
& USB_RECIP_MASK
) {
1125 case USB_RECIP_DEVICE
:
1126 status
= gr_device_request(dev
, type
, request
,
1129 case USB_RECIP_ENDPOINT
:
1130 status
= gr_endpoint_request(dev
, type
, request
,
1133 case USB_RECIP_INTERFACE
:
1134 status
= gr_interface_request(dev
, type
, request
,
1141 spin_unlock(&dev
->lock
);
1143 dev_vdbg(dev
->dev
, "DELEGATE\n");
1144 status
= dev
->driver
->setup(&dev
->gadget
, &u
.ctrl
);
1146 spin_lock(&dev
->lock
);
1149 /* Generate STALL on both ep0out and ep0in if requested */
1150 if (unlikely(status
< 0)) {
1151 dev_vdbg(dev
->dev
, "STALL\n");
1152 gr_control_stall(dev
);
1155 if ((type
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
&&
1156 request
== USB_REQ_SET_CONFIGURATION
) {
1158 dev_dbg(dev
->dev
, "STATUS: deconfigured\n");
1159 usb_gadget_set_state(&dev
->gadget
, USB_STATE_ADDRESS
);
1160 } else if (status
>= 0) {
1161 /* Not configured unless gadget OK:s it */
1162 dev_dbg(dev
->dev
, "STATUS: configured: %d\n", value
);
1163 usb_gadget_set_state(&dev
->gadget
,
1164 USB_STATE_CONFIGURED
);
1168 /* Get ready for next stage */
1169 if (dev
->ep0state
== GR_EP0_ODATA
)
1170 gr_set_ep0state(dev
, GR_EP0_OSTATUS
);
1171 else if (dev
->ep0state
== GR_EP0_IDATA
)
1172 gr_set_ep0state(dev
, GR_EP0_ISTATUS
);
1174 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1177 gr_ep0out_requeue(dev
);
1180 /* ---------------------------------------------------------------------- */
1181 /* VBUS and USB reset handling */
1183 /* Must be called with dev->lock held and irqs disabled */
1184 static void gr_vbus_connected(struct gr_udc
*dev
, u32 status
)
1188 dev
->gadget
.speed
= GR_SPEED(status
);
1189 usb_gadget_set_state(&dev
->gadget
, USB_STATE_POWERED
);
1191 /* Turn on full interrupts and pullup */
1192 control
= (GR_CONTROL_SI
| GR_CONTROL_UI
| GR_CONTROL_VI
|
1193 GR_CONTROL_SP
| GR_CONTROL_EP
);
1194 gr_write32(&dev
->regs
->control
, control
);
1197 /* Must be called with dev->lock held */
1198 static void gr_enable_vbus_detect(struct gr_udc
*dev
)
1202 dev
->irq_enabled
= 1;
1203 wmb(); /* Make sure we do not ignore an interrupt */
1204 gr_write32(&dev
->regs
->control
, GR_CONTROL_VI
);
1206 /* Take care of the case we are already plugged in at this point */
1207 status
= gr_read32(&dev
->regs
->status
);
1208 if (status
& GR_STATUS_VB
)
1209 gr_vbus_connected(dev
, status
);
1212 /* Must be called with dev->lock held and irqs disabled */
1213 static void gr_vbus_disconnected(struct gr_udc
*dev
)
1215 gr_stop_activity(dev
);
1217 /* Report disconnect */
1218 if (dev
->driver
&& dev
->driver
->disconnect
) {
1219 spin_unlock(&dev
->lock
);
1221 dev
->driver
->disconnect(&dev
->gadget
);
1223 spin_lock(&dev
->lock
);
1226 gr_enable_vbus_detect(dev
);
1229 /* Must be called with dev->lock held and irqs disabled */
1230 static void gr_udc_usbreset(struct gr_udc
*dev
, u32 status
)
1232 gr_set_address(dev
, 0);
1233 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1234 usb_gadget_set_state(&dev
->gadget
, USB_STATE_DEFAULT
);
1235 dev
->gadget
.speed
= GR_SPEED(status
);
1237 gr_ep_nuke(&dev
->epo
[0]);
1238 gr_ep_nuke(&dev
->epi
[0]);
1239 dev
->epo
[0].stopped
= 0;
1240 dev
->epi
[0].stopped
= 0;
1241 gr_ep0out_requeue(dev
);
1244 /* ---------------------------------------------------------------------- */
1248 * Handles interrupts from in endpoints. Returns whether something was handled.
1250 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1252 static int gr_handle_in_ep(struct gr_ep
*ep
)
1254 struct gr_request
*req
;
1256 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
1257 if (!req
->last_desc
)
1260 if (READ_ONCE(req
->last_desc
->ctrl
) & GR_DESC_IN_CTRL_EN
)
1261 return 0; /* Not put in hardware buffers yet */
1263 if (gr_read32(&ep
->regs
->epstat
) & (GR_EPSTAT_B1
| GR_EPSTAT_B0
))
1264 return 0; /* Not transmitted yet, still in hardware buffers */
1266 /* Write complete */
1267 gr_dma_advance(ep
, 0);
1273 * Handles interrupts from out endpoints. Returns whether something was handled.
1275 * Must be called with dev->lock held, irqs disabled and with !ep->stopped.
1277 static int gr_handle_out_ep(struct gr_ep
*ep
)
1282 struct gr_request
*req
;
1283 struct gr_udc
*dev
= ep
->dev
;
1285 req
= list_first_entry(&ep
->queue
, struct gr_request
, queue
);
1286 if (!req
->curr_desc
)
1289 ctrl
= READ_ONCE(req
->curr_desc
->ctrl
);
1290 if (ctrl
& GR_DESC_OUT_CTRL_EN
)
1291 return 0; /* Not received yet */
1294 len
= ctrl
& GR_DESC_OUT_CTRL_LEN_MASK
;
1295 req
->req
.actual
+= len
;
1296 if (ctrl
& GR_DESC_OUT_CTRL_SE
)
1299 if (len
< ep
->ep
.maxpacket
|| req
->req
.actual
>= req
->req
.length
) {
1300 /* Short packet or >= expected size - we are done */
1302 if ((ep
== &dev
->epo
[0]) && (dev
->ep0state
== GR_EP0_OSTATUS
)) {
1304 * Send a status stage ZLP to ack the DATA stage in the
1305 * OUT direction. This needs to be done before
1306 * gr_dma_advance as that can lead to a call to
1307 * ep0_setup that can change dev->ep0state.
1309 gr_ep0_respond_empty(dev
);
1310 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1313 gr_dma_advance(ep
, 0);
1315 /* Not done yet. Enable the next descriptor to receive more. */
1316 req
->curr_desc
= req
->curr_desc
->next_desc
;
1317 req
->curr_desc
->ctrl
|= GR_DESC_OUT_CTRL_EN
;
1319 ep_dmactrl
= gr_read32(&ep
->regs
->dmactrl
);
1320 gr_write32(&ep
->regs
->dmactrl
, ep_dmactrl
| GR_DMACTRL_DA
);
1327 * Handle state changes. Returns whether something was handled.
1329 * Must be called with dev->lock held and irqs disabled.
1331 static int gr_handle_state_changes(struct gr_udc
*dev
)
1333 u32 status
= gr_read32(&dev
->regs
->status
);
1335 int powstate
= !(dev
->gadget
.state
== USB_STATE_NOTATTACHED
||
1336 dev
->gadget
.state
== USB_STATE_ATTACHED
);
1338 /* VBUS valid detected */
1339 if (!powstate
&& (status
& GR_STATUS_VB
)) {
1340 dev_dbg(dev
->dev
, "STATUS: vbus valid detected\n");
1341 gr_vbus_connected(dev
, status
);
1346 if (powstate
&& !(status
& GR_STATUS_VB
)) {
1347 dev_dbg(dev
->dev
, "STATUS: vbus invalid detected\n");
1348 gr_vbus_disconnected(dev
);
1352 /* USB reset detected */
1353 if (status
& GR_STATUS_UR
) {
1354 dev_dbg(dev
->dev
, "STATUS: USB reset - speed is %s\n",
1355 GR_SPEED_STR(status
));
1356 gr_write32(&dev
->regs
->status
, GR_STATUS_UR
);
1357 gr_udc_usbreset(dev
, status
);
1362 if (dev
->gadget
.speed
!= GR_SPEED(status
)) {
1363 dev_dbg(dev
->dev
, "STATUS: USB Speed change to %s\n",
1364 GR_SPEED_STR(status
));
1365 dev
->gadget
.speed
= GR_SPEED(status
);
1369 /* Going into suspend */
1370 if ((dev
->ep0state
!= GR_EP0_SUSPEND
) && !(status
& GR_STATUS_SU
)) {
1371 dev_dbg(dev
->dev
, "STATUS: USB suspend\n");
1372 gr_set_ep0state(dev
, GR_EP0_SUSPEND
);
1373 dev
->suspended_from
= dev
->gadget
.state
;
1374 usb_gadget_set_state(&dev
->gadget
, USB_STATE_SUSPENDED
);
1376 if ((dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
) &&
1377 dev
->driver
&& dev
->driver
->suspend
) {
1378 spin_unlock(&dev
->lock
);
1380 dev
->driver
->suspend(&dev
->gadget
);
1382 spin_lock(&dev
->lock
);
1387 /* Coming out of suspend */
1388 if ((dev
->ep0state
== GR_EP0_SUSPEND
) && (status
& GR_STATUS_SU
)) {
1389 dev_dbg(dev
->dev
, "STATUS: USB resume\n");
1390 if (dev
->suspended_from
== USB_STATE_POWERED
)
1391 gr_set_ep0state(dev
, GR_EP0_DISCONNECT
);
1393 gr_set_ep0state(dev
, GR_EP0_SETUP
);
1394 usb_gadget_set_state(&dev
->gadget
, dev
->suspended_from
);
1396 if ((dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
) &&
1397 dev
->driver
&& dev
->driver
->resume
) {
1398 spin_unlock(&dev
->lock
);
1400 dev
->driver
->resume(&dev
->gadget
);
1402 spin_lock(&dev
->lock
);
1410 /* Non-interrupt context irq handler */
1411 static irqreturn_t
gr_irq_handler(int irq
, void *_dev
)
1413 struct gr_udc
*dev
= _dev
;
1417 unsigned long flags
;
1419 spin_lock_irqsave(&dev
->lock
, flags
);
1421 if (!dev
->irq_enabled
)
1425 * Check IN ep interrupts. We check these before the OUT eps because
1426 * some gadgets reuse the request that might already be currently
1427 * outstanding and needs to be completed (mainly setup requests).
1429 for (i
= 0; i
< dev
->nepi
; i
++) {
1431 if (!ep
->stopped
&& !ep
->callback
&& !list_empty(&ep
->queue
))
1432 handled
= gr_handle_in_ep(ep
) || handled
;
1435 /* Check OUT ep interrupts */
1436 for (i
= 0; i
< dev
->nepo
; i
++) {
1438 if (!ep
->stopped
&& !ep
->callback
&& !list_empty(&ep
->queue
))
1439 handled
= gr_handle_out_ep(ep
) || handled
;
1442 /* Check status interrupts */
1443 handled
= gr_handle_state_changes(dev
) || handled
;
1446 * Check AMBA DMA errors. Only check if we didn't find anything else to
1447 * handle because this shouldn't happen if we did everything right.
1450 list_for_each_entry(ep
, &dev
->ep_list
, ep_list
) {
1451 if (gr_read32(&ep
->regs
->dmactrl
) & GR_DMACTRL_AE
) {
1453 "AMBA Error occurred for %s\n",
1461 spin_unlock_irqrestore(&dev
->lock
, flags
);
1463 return handled
? IRQ_HANDLED
: IRQ_NONE
;
1466 /* Interrupt context irq handler */
1467 static irqreturn_t
gr_irq(int irq
, void *_dev
)
1469 struct gr_udc
*dev
= _dev
;
1471 if (!dev
->irq_enabled
)
1474 return IRQ_WAKE_THREAD
;
1477 /* ---------------------------------------------------------------------- */
1480 /* Enable endpoint. Not for ep0in and ep0out that are handled separately. */
1481 static int gr_ep_enable(struct usb_ep
*_ep
,
1482 const struct usb_endpoint_descriptor
*desc
)
1489 u16 buffer_size
= 0;
1492 ep
= container_of(_ep
, struct gr_ep
, ep
);
1493 if (!_ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
)
1498 /* 'ep0' IN and OUT are reserved */
1499 if (ep
== &dev
->epo
[0] || ep
== &dev
->epi
[0])
1502 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1505 /* Make sure we are clear for enabling */
1506 epctrl
= gr_read32(&ep
->regs
->epctrl
);
1507 if (epctrl
& GR_EPCTRL_EV
)
1510 /* Check that directions match */
1511 if (!ep
->is_in
!= !usb_endpoint_dir_in(desc
))
1515 if ((!ep
->is_in
&& ep
->num
>= dev
->nepo
) ||
1516 (ep
->is_in
&& ep
->num
>= dev
->nepi
))
1519 if (usb_endpoint_xfer_control(desc
)) {
1521 } else if (usb_endpoint_xfer_isoc(desc
)) {
1523 } else if (usb_endpoint_xfer_bulk(desc
)) {
1525 } else if (usb_endpoint_xfer_int(desc
)) {
1528 dev_err(dev
->dev
, "Unknown transfer type for %s\n",
1534 * Bits 10-0 set the max payload. 12-11 set the number of
1535 * additional transactions.
1537 max
= usb_endpoint_maxp(desc
);
1538 nt
= usb_endpoint_maxp_mult(desc
) - 1;
1539 buffer_size
= GR_BUFFER_SIZE(epctrl
);
1540 if (nt
&& (mode
== 0 || mode
== 2)) {
1542 "%s mode: multiple trans./microframe not valid\n",
1543 (mode
== 2 ? "Bulk" : "Control"));
1545 } else if (nt
== 0x3) {
1547 "Invalid value 0x3 for additional trans./microframe\n");
1549 } else if ((nt
+ 1) * max
> buffer_size
) {
1550 dev_err(dev
->dev
, "Hw buffer size %d < max payload %d * %d\n",
1551 buffer_size
, (nt
+ 1), max
);
1553 } else if (max
== 0) {
1554 dev_err(dev
->dev
, "Max payload cannot be set to 0\n");
1556 } else if (max
> ep
->ep
.maxpacket_limit
) {
1557 dev_err(dev
->dev
, "Requested max payload %d > limit %d\n",
1558 max
, ep
->ep
.maxpacket_limit
);
1562 spin_lock(&ep
->dev
->lock
);
1565 spin_unlock(&ep
->dev
->lock
);
1572 ep
->ep
.maxpacket
= max
;
1578 * Maximum possible size of all payloads in one microframe
1579 * regardless of direction when using high-bandwidth mode.
1581 ep
->bytes_per_buffer
= (nt
+ 1) * max
;
1582 } else if (ep
->is_in
) {
1584 * The biggest multiple of maximum packet size that fits into
1585 * the buffer. The hardware will split up into many packets in
1588 ep
->bytes_per_buffer
= (buffer_size
/ max
) * max
;
1591 * Only single packets will be placed the buffers in the OUT
1594 ep
->bytes_per_buffer
= max
;
1597 epctrl
= (max
<< GR_EPCTRL_MAXPL_POS
)
1598 | (nt
<< GR_EPCTRL_NT_POS
)
1599 | (mode
<< GR_EPCTRL_TT_POS
)
1602 epctrl
|= GR_EPCTRL_PI
;
1603 gr_write32(&ep
->regs
->epctrl
, epctrl
);
1605 gr_write32(&ep
->regs
->dmactrl
, GR_DMACTRL_IE
| GR_DMACTRL_AI
);
1607 spin_unlock(&ep
->dev
->lock
);
1609 dev_dbg(ep
->dev
->dev
, "EP: %s enabled - %s with %d bytes/buffer\n",
1610 ep
->ep
.name
, gr_modestring
[mode
], ep
->bytes_per_buffer
);
1614 /* Disable endpoint. Not for ep0in and ep0out that are handled separately. */
1615 static int gr_ep_disable(struct usb_ep
*_ep
)
1619 unsigned long flags
;
1621 ep
= container_of(_ep
, struct gr_ep
, ep
);
1622 if (!_ep
|| !ep
->ep
.desc
)
1627 /* 'ep0' IN and OUT are reserved */
1628 if (ep
== &dev
->epo
[0] || ep
== &dev
->epi
[0])
1631 if (dev
->ep0state
== GR_EP0_SUSPEND
)
1634 dev_dbg(ep
->dev
->dev
, "EP: disable %s\n", ep
->ep
.name
);
1636 spin_lock_irqsave(&dev
->lock
, flags
);
1642 spin_unlock_irqrestore(&dev
->lock
, flags
);
1648 * Frees a request, but not any DMA buffers associated with it
1649 * (gr_finish_request should already have taken care of that).
1651 static void gr_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1653 struct gr_request
*req
;
1657 req
= container_of(_req
, struct gr_request
, req
);
1659 /* Leads to memory leak */
1660 WARN(!list_empty(&req
->queue
),
1661 "request not dequeued properly before freeing\n");
1666 /* Queue a request from the gadget */
1667 static int gr_queue_ext(struct usb_ep
*_ep
, struct usb_request
*_req
,
1671 struct gr_request
*req
;
1675 if (unlikely(!_ep
|| !_req
))
1678 ep
= container_of(_ep
, struct gr_ep
, ep
);
1679 req
= container_of(_req
, struct gr_request
, req
);
1682 spin_lock(&ep
->dev
->lock
);
1685 * The ep0 pointer in the gadget struct is used both for ep0in and
1686 * ep0out. In a data stage in the out direction ep0out needs to be used
1687 * instead of the default ep0in. Completion functions might use
1688 * driver_data, so that needs to be copied as well.
1690 if ((ep
== &dev
->epi
[0]) && (dev
->ep0state
== GR_EP0_ODATA
)) {
1692 ep
->ep
.driver_data
= dev
->epi
[0].ep
.driver_data
;
1696 gr_dbgprint_request("EXTERN", ep
, req
);
1698 ret
= gr_queue(ep
, req
, GFP_ATOMIC
);
1700 spin_unlock(&ep
->dev
->lock
);
1705 /* Dequeue JUST ONE request */
1706 static int gr_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1708 struct gr_request
*req
;
1712 unsigned long flags
;
1714 ep
= container_of(_ep
, struct gr_ep
, ep
);
1715 if (!_ep
|| !_req
|| (!ep
->ep
.desc
&& ep
->num
!= 0))
1721 /* We can't touch (DMA) registers when suspended */
1722 if (dev
->ep0state
== GR_EP0_SUSPEND
)
1725 spin_lock_irqsave(&dev
->lock
, flags
);
1727 /* Make sure it's actually queued on this endpoint */
1728 list_for_each_entry(req
, &ep
->queue
, queue
) {
1729 if (&req
->req
== _req
)
1732 if (&req
->req
!= _req
) {
1737 if (list_first_entry(&ep
->queue
, struct gr_request
, queue
) == req
) {
1738 /* This request is currently being processed */
1741 gr_finish_request(ep
, req
, -ECONNRESET
);
1743 gr_dma_advance(ep
, -ECONNRESET
);
1744 } else if (!list_empty(&req
->queue
)) {
1745 /* Not being processed - gr_finish_request dequeues it */
1746 gr_finish_request(ep
, req
, -ECONNRESET
);
1752 spin_unlock_irqrestore(&dev
->lock
, flags
);
1757 /* Helper for gr_set_halt and gr_set_wedge */
1758 static int gr_set_halt_wedge(struct usb_ep
*_ep
, int halt
, int wedge
)
1765 ep
= container_of(_ep
, struct gr_ep
, ep
);
1767 spin_lock(&ep
->dev
->lock
);
1769 /* Halting an IN endpoint should fail if queue is not empty */
1770 if (halt
&& ep
->is_in
&& !list_empty(&ep
->queue
)) {
1775 ret
= gr_ep_halt_wedge(ep
, halt
, wedge
, 0);
1778 spin_unlock(&ep
->dev
->lock
);
1784 static int gr_set_halt(struct usb_ep
*_ep
, int halt
)
1786 return gr_set_halt_wedge(_ep
, halt
, 0);
1789 /* Halt and wedge endpoint */
1790 static int gr_set_wedge(struct usb_ep
*_ep
)
1792 return gr_set_halt_wedge(_ep
, 1, 1);
1796 * Return the total number of bytes currently stored in the internal buffers of
1799 static int gr_fifo_status(struct usb_ep
*_ep
)
1807 ep
= container_of(_ep
, struct gr_ep
, ep
);
1809 epstat
= gr_read32(&ep
->regs
->epstat
);
1811 if (epstat
& GR_EPSTAT_B0
)
1812 bytes
+= (epstat
& GR_EPSTAT_B0CNT_MASK
) >> GR_EPSTAT_B0CNT_POS
;
1813 if (epstat
& GR_EPSTAT_B1
)
1814 bytes
+= (epstat
& GR_EPSTAT_B1CNT_MASK
) >> GR_EPSTAT_B1CNT_POS
;
1820 /* Empty data from internal buffers of an endpoint. */
1821 static void gr_fifo_flush(struct usb_ep
*_ep
)
1828 ep
= container_of(_ep
, struct gr_ep
, ep
);
1829 dev_vdbg(ep
->dev
->dev
, "EP: flush fifo %s\n", ep
->ep
.name
);
1831 spin_lock(&ep
->dev
->lock
);
1833 epctrl
= gr_read32(&ep
->regs
->epctrl
);
1834 epctrl
|= GR_EPCTRL_CB
;
1835 gr_write32(&ep
->regs
->epctrl
, epctrl
);
1837 spin_unlock(&ep
->dev
->lock
);
1840 static const struct usb_ep_ops gr_ep_ops
= {
1841 .enable
= gr_ep_enable
,
1842 .disable
= gr_ep_disable
,
1844 .alloc_request
= gr_alloc_request
,
1845 .free_request
= gr_free_request
,
1847 .queue
= gr_queue_ext
,
1848 .dequeue
= gr_dequeue
,
1850 .set_halt
= gr_set_halt
,
1851 .set_wedge
= gr_set_wedge
,
1852 .fifo_status
= gr_fifo_status
,
1853 .fifo_flush
= gr_fifo_flush
,
1856 /* ---------------------------------------------------------------------- */
1857 /* USB Gadget ops */
1859 static int gr_get_frame(struct usb_gadget
*_gadget
)
1865 dev
= container_of(_gadget
, struct gr_udc
, gadget
);
1866 return gr_read32(&dev
->regs
->status
) & GR_STATUS_FN_MASK
;
1869 static int gr_wakeup(struct usb_gadget
*_gadget
)
1875 dev
= container_of(_gadget
, struct gr_udc
, gadget
);
1877 /* Remote wakeup feature not enabled by host*/
1878 if (!dev
->remote_wakeup
)
1881 spin_lock(&dev
->lock
);
1883 gr_write32(&dev
->regs
->control
,
1884 gr_read32(&dev
->regs
->control
) | GR_CONTROL_RW
);
1886 spin_unlock(&dev
->lock
);
1891 static int gr_pullup(struct usb_gadget
*_gadget
, int is_on
)
1898 dev
= container_of(_gadget
, struct gr_udc
, gadget
);
1900 spin_lock(&dev
->lock
);
1902 control
= gr_read32(&dev
->regs
->control
);
1904 control
|= GR_CONTROL_EP
;
1906 control
&= ~GR_CONTROL_EP
;
1907 gr_write32(&dev
->regs
->control
, control
);
1909 spin_unlock(&dev
->lock
);
1914 static int gr_udc_start(struct usb_gadget
*gadget
,
1915 struct usb_gadget_driver
*driver
)
1917 struct gr_udc
*dev
= to_gr_udc(gadget
);
1919 spin_lock(&dev
->lock
);
1921 /* Hook up the driver */
1922 driver
->driver
.bus
= NULL
;
1923 dev
->driver
= driver
;
1925 /* Get ready for host detection */
1926 gr_enable_vbus_detect(dev
);
1928 spin_unlock(&dev
->lock
);
1933 static int gr_udc_stop(struct usb_gadget
*gadget
)
1935 struct gr_udc
*dev
= to_gr_udc(gadget
);
1936 unsigned long flags
;
1938 spin_lock_irqsave(&dev
->lock
, flags
);
1941 gr_stop_activity(dev
);
1943 spin_unlock_irqrestore(&dev
->lock
, flags
);
1948 static const struct usb_gadget_ops gr_ops
= {
1949 .get_frame
= gr_get_frame
,
1950 .wakeup
= gr_wakeup
,
1951 .pullup
= gr_pullup
,
1952 .udc_start
= gr_udc_start
,
1953 .udc_stop
= gr_udc_stop
,
1954 /* Other operations not supported */
1957 /* ---------------------------------------------------------------------- */
1958 /* Module probe, removal and of-matching */
1960 static const char * const onames
[] = {
1961 "ep0out", "ep1out", "ep2out", "ep3out", "ep4out", "ep5out",
1962 "ep6out", "ep7out", "ep8out", "ep9out", "ep10out", "ep11out",
1963 "ep12out", "ep13out", "ep14out", "ep15out"
1966 static const char * const inames
[] = {
1967 "ep0in", "ep1in", "ep2in", "ep3in", "ep4in", "ep5in",
1968 "ep6in", "ep7in", "ep8in", "ep9in", "ep10in", "ep11in",
1969 "ep12in", "ep13in", "ep14in", "ep15in"
1972 /* Must be called with dev->lock held */
1973 static int gr_ep_init(struct gr_udc
*dev
, int num
, int is_in
, u32 maxplimit
)
1976 struct gr_request
*req
;
1977 struct usb_request
*_req
;
1981 ep
= &dev
->epi
[num
];
1982 ep
->ep
.name
= inames
[num
];
1983 ep
->regs
= &dev
->regs
->epi
[num
];
1985 ep
= &dev
->epo
[num
];
1986 ep
->ep
.name
= onames
[num
];
1987 ep
->regs
= &dev
->regs
->epo
[num
];
1994 ep
->ep
.ops
= &gr_ep_ops
;
1995 INIT_LIST_HEAD(&ep
->queue
);
1998 _req
= gr_alloc_request(&ep
->ep
, GFP_ATOMIC
);
1999 buf
= devm_kzalloc(dev
->dev
, PAGE_SIZE
, GFP_DMA
| GFP_ATOMIC
);
2000 if (!_req
|| !buf
) {
2001 /* possible _req freed by gr_probe via gr_remove */
2005 req
= container_of(_req
, struct gr_request
, req
);
2007 req
->req
.length
= MAX_CTRL_PL_SIZE
;
2010 dev
->ep0reqi
= req
; /* Complete gets set as used */
2012 dev
->ep0reqo
= req
; /* Completion treated separately */
2014 usb_ep_set_maxpacket_limit(&ep
->ep
, MAX_CTRL_PL_SIZE
);
2015 ep
->bytes_per_buffer
= MAX_CTRL_PL_SIZE
;
2017 ep
->ep
.caps
.type_control
= true;
2019 usb_ep_set_maxpacket_limit(&ep
->ep
, (u16
)maxplimit
);
2020 list_add_tail(&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
2022 ep
->ep
.caps
.type_iso
= true;
2023 ep
->ep
.caps
.type_bulk
= true;
2024 ep
->ep
.caps
.type_int
= true;
2026 list_add_tail(&ep
->ep_list
, &dev
->ep_list
);
2029 ep
->ep
.caps
.dir_in
= true;
2031 ep
->ep
.caps
.dir_out
= true;
2033 ep
->tailbuf
= dma_alloc_coherent(dev
->dev
, ep
->ep
.maxpacket_limit
,
2034 &ep
->tailbuf_paddr
, GFP_ATOMIC
);
2041 /* Must be called with dev->lock held */
2042 static int gr_udc_init(struct gr_udc
*dev
)
2044 struct device_node
*np
= dev
->dev
->of_node
;
2051 gr_set_address(dev
, 0);
2053 INIT_LIST_HEAD(&dev
->gadget
.ep_list
);
2054 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2055 dev
->gadget
.ep0
= &dev
->epi
[0].ep
;
2057 INIT_LIST_HEAD(&dev
->ep_list
);
2058 gr_set_ep0state(dev
, GR_EP0_DISCONNECT
);
2060 for (i
= 0; i
< dev
->nepo
; i
++) {
2061 if (of_property_read_u32_index(np
, "epobufsizes", i
, &bufsize
))
2063 ret
= gr_ep_init(dev
, i
, 0, bufsize
);
2068 for (i
= 0; i
< dev
->nepi
; i
++) {
2069 if (of_property_read_u32_index(np
, "epibufsizes", i
, &bufsize
))
2071 ret
= gr_ep_init(dev
, i
, 1, bufsize
);
2076 /* Must be disabled by default */
2077 dev
->remote_wakeup
= 0;
2079 /* Enable ep0out and ep0in */
2080 epctrl_val
= (MAX_CTRL_PL_SIZE
<< GR_EPCTRL_MAXPL_POS
) | GR_EPCTRL_EV
;
2081 dmactrl_val
= GR_DMACTRL_IE
| GR_DMACTRL_AI
;
2082 gr_write32(&dev
->epo
[0].regs
->epctrl
, epctrl_val
);
2083 gr_write32(&dev
->epi
[0].regs
->epctrl
, epctrl_val
| GR_EPCTRL_PI
);
2084 gr_write32(&dev
->epo
[0].regs
->dmactrl
, dmactrl_val
);
2085 gr_write32(&dev
->epi
[0].regs
->dmactrl
, dmactrl_val
);
2090 static void gr_ep_remove(struct gr_udc
*dev
, int num
, int is_in
)
2095 ep
= &dev
->epi
[num
];
2097 ep
= &dev
->epo
[num
];
2100 dma_free_coherent(dev
->dev
, ep
->ep
.maxpacket_limit
,
2101 ep
->tailbuf
, ep
->tailbuf_paddr
);
2104 static int gr_remove(struct platform_device
*pdev
)
2106 struct gr_udc
*dev
= platform_get_drvdata(pdev
);
2110 usb_del_gadget_udc(&dev
->gadget
); /* Shuts everything down */
2115 dma_pool_destroy(dev
->desc_pool
);
2116 platform_set_drvdata(pdev
, NULL
);
2118 gr_free_request(&dev
->epi
[0].ep
, &dev
->ep0reqi
->req
);
2119 gr_free_request(&dev
->epo
[0].ep
, &dev
->ep0reqo
->req
);
2121 for (i
= 0; i
< dev
->nepo
; i
++)
2122 gr_ep_remove(dev
, i
, 0);
2123 for (i
= 0; i
< dev
->nepi
; i
++)
2124 gr_ep_remove(dev
, i
, 1);
2128 static int gr_request_irq(struct gr_udc
*dev
, int irq
)
2130 return devm_request_threaded_irq(dev
->dev
, irq
, gr_irq
, gr_irq_handler
,
2131 IRQF_SHARED
, driver_name
, dev
);
2134 static int gr_probe(struct platform_device
*pdev
)
2137 struct resource
*res
;
2138 struct gr_regs __iomem
*regs
;
2142 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
2145 dev
->dev
= &pdev
->dev
;
2147 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2148 regs
= devm_ioremap_resource(dev
->dev
, res
);
2150 return PTR_ERR(regs
);
2152 dev
->irq
= platform_get_irq(pdev
, 0);
2153 if (dev
->irq
<= 0) {
2154 dev_err(dev
->dev
, "No irq found\n");
2158 /* Some core configurations has separate irqs for IN and OUT events */
2159 dev
->irqi
= platform_get_irq(pdev
, 1);
2160 if (dev
->irqi
> 0) {
2161 dev
->irqo
= platform_get_irq(pdev
, 2);
2162 if (dev
->irqo
<= 0) {
2163 dev_err(dev
->dev
, "Found irqi but not irqo\n");
2170 dev
->gadget
.name
= driver_name
;
2171 dev
->gadget
.max_speed
= USB_SPEED_HIGH
;
2172 dev
->gadget
.ops
= &gr_ops
;
2174 spin_lock_init(&dev
->lock
);
2177 platform_set_drvdata(pdev
, dev
);
2179 /* Determine number of endpoints and data interface mode */
2180 status
= gr_read32(&dev
->regs
->status
);
2181 dev
->nepi
= ((status
& GR_STATUS_NEPI_MASK
) >> GR_STATUS_NEPI_POS
) + 1;
2182 dev
->nepo
= ((status
& GR_STATUS_NEPO_MASK
) >> GR_STATUS_NEPO_POS
) + 1;
2184 if (!(status
& GR_STATUS_DM
)) {
2185 dev_err(dev
->dev
, "Slave mode cores are not supported\n");
2189 /* --- Effects of the following calls might need explicit cleanup --- */
2191 /* Create DMA pool for descriptors */
2192 dev
->desc_pool
= dma_pool_create("desc_pool", dev
->dev
,
2193 sizeof(struct gr_dma_desc
), 4, 0);
2194 if (!dev
->desc_pool
) {
2195 dev_err(dev
->dev
, "Could not allocate DMA pool");
2199 spin_lock(&dev
->lock
);
2201 /* Inside lock so that no gadget can use this udc until probe is done */
2202 retval
= usb_add_gadget_udc(dev
->dev
, &dev
->gadget
);
2204 dev_err(dev
->dev
, "Could not add gadget udc");
2209 retval
= gr_udc_init(dev
);
2215 /* Clear all interrupt enables that might be left on since last boot */
2216 gr_disable_interrupts_and_pullup(dev
);
2218 retval
= gr_request_irq(dev
, dev
->irq
);
2220 dev_err(dev
->dev
, "Failed to request irq %d\n", dev
->irq
);
2225 retval
= gr_request_irq(dev
, dev
->irqi
);
2227 dev_err(dev
->dev
, "Failed to request irqi %d\n",
2231 retval
= gr_request_irq(dev
, dev
->irqo
);
2233 dev_err(dev
->dev
, "Failed to request irqo %d\n",
2240 dev_info(dev
->dev
, "regs: %p, irqs %d, %d, %d\n", dev
->regs
,
2241 dev
->irq
, dev
->irqi
, dev
->irqo
);
2243 dev_info(dev
->dev
, "regs: %p, irq %d\n", dev
->regs
, dev
->irq
);
2246 spin_unlock(&dev
->lock
);
2254 static const struct of_device_id gr_match
[] = {
2255 {.name
= "GAISLER_USBDC"},
2259 MODULE_DEVICE_TABLE(of
, gr_match
);
2261 static struct platform_driver gr_driver
= {
2263 .name
= DRIVER_NAME
,
2264 .of_match_table
= gr_match
,
2267 .remove
= gr_remove
,
2269 module_platform_driver(gr_driver
);
2271 MODULE_AUTHOR("Aeroflex Gaisler AB.");
2272 MODULE_DESCRIPTION(DRIVER_DESC
);
2273 MODULE_LICENSE("GPL");