1 // SPDX-License-Identifier: GPL-2.0+
3 * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
5 * epn.c - Generic endpoints management
7 * Copyright 2017 IBM Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/delay.h>
19 #include <linux/ioport.h>
20 #include <linux/slab.h>
21 #include <linux/errno.h>
22 #include <linux/list.h>
23 #include <linux/interrupt.h>
24 #include <linux/proc_fs.h>
25 #include <linux/prefetch.h>
26 #include <linux/clk.h>
27 #include <linux/usb/gadget.h>
29 #include <linux/of_gpio.h>
30 #include <linux/regmap.h>
31 #include <linux/dma-mapping.h>
38 #define CHECK(ep, expr, fmt...) \
40 if (!(expr)) EPDBG(ep, "CHECK:" fmt); \
43 #define CHECK(ep, expr, fmt...) do { } while(0)
46 static void ast_vhub_epn_kick(struct ast_vhub_ep
*ep
, struct ast_vhub_req
*req
)
48 unsigned int act
= req
->req
.actual
;
49 unsigned int len
= req
->req
.length
;
52 /* There should be no DMA ongoing */
55 /* Calculate next chunk size */
57 if (chunk
> ep
->ep
.maxpacket
)
58 chunk
= ep
->ep
.maxpacket
;
59 else if ((chunk
< ep
->ep
.maxpacket
) || !req
->req
.zero
)
62 EPVDBG(ep
, "kick req %p act=%d/%d chunk=%d last=%d\n",
63 req
, act
, len
, chunk
, req
->last_desc
);
65 /* If DMA unavailable, using staging EP buffer */
68 /* For IN transfers, copy data over first */
70 memcpy(ep
->buf
, req
->req
.buf
+ act
, chunk
);
71 vhub_dma_workaround(ep
->buf
);
73 writel(ep
->buf_dma
, ep
->epn
.regs
+ AST_VHUB_EP_DESC_BASE
);
76 vhub_dma_workaround(req
->req
.buf
);
77 writel(req
->req
.dma
+ act
, ep
->epn
.regs
+ AST_VHUB_EP_DESC_BASE
);
82 writel(VHUB_EP_DMA_SET_TX_SIZE(chunk
),
83 ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
);
84 writel(VHUB_EP_DMA_SET_TX_SIZE(chunk
) | VHUB_EP_DMA_SINGLE_KICK
,
85 ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
);
88 static void ast_vhub_epn_handle_ack(struct ast_vhub_ep
*ep
)
90 struct ast_vhub_req
*req
;
95 stat
= readl(ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
);
97 /* Grab current request if any */
98 req
= list_first_entry_or_null(&ep
->queue
, struct ast_vhub_req
, queue
);
100 EPVDBG(ep
, "ACK status=%08x is_in=%d, req=%p (active=%d)\n",
101 stat
, ep
->epn
.is_in
, req
, req
? req
->active
: 0);
103 /* In absence of a request, bail out, must have been dequeued */
108 * Request not active, move on to processing queue, active request
109 * was probably dequeued
114 /* Check if HW has moved on */
115 if (VHUB_EP_DMA_RPTR(stat
) != 0) {
116 EPDBG(ep
, "DMA read pointer not 0 !\n");
120 /* No current DMA ongoing */
123 /* Grab lenght out of HW */
124 len
= VHUB_EP_DMA_TX_SIZE(stat
);
126 /* If not using DMA, copy data out if needed */
127 if (!req
->req
.dma
&& !ep
->epn
.is_in
&& len
)
128 memcpy(req
->req
.buf
+ req
->req
.actual
, ep
->buf
, len
);
131 req
->req
.actual
+= len
;
133 /* Check for short packet */
134 if (len
< ep
->ep
.maxpacket
)
137 /* That's it ? complete the request and pick a new one */
138 if (req
->last_desc
>= 0) {
139 ast_vhub_done(ep
, req
, 0);
140 req
= list_first_entry_or_null(&ep
->queue
, struct ast_vhub_req
,
144 * Due to lock dropping inside "done" the next request could
145 * already be active, so check for that and bail if needed.
147 if (!req
|| req
->active
)
152 ast_vhub_epn_kick(ep
, req
);
155 static inline unsigned int ast_vhub_count_free_descs(struct ast_vhub_ep
*ep
)
158 * d_next == d_last means descriptor list empty to HW,
159 * thus we can only have AST_VHUB_DESCS_COUNT-1 descriptors
162 return (ep
->epn
.d_last
+ AST_VHUB_DESCS_COUNT
- ep
->epn
.d_next
- 1) &
163 (AST_VHUB_DESCS_COUNT
- 1);
166 static void ast_vhub_epn_kick_desc(struct ast_vhub_ep
*ep
,
167 struct ast_vhub_req
*req
)
169 struct ast_vhub_desc
*desc
= NULL
;
170 unsigned int act
= req
->act_count
;
171 unsigned int len
= req
->req
.length
;
174 /* Mark request active if not already */
177 /* If the request was already completely written, do nothing */
178 if (req
->last_desc
>= 0)
181 EPVDBG(ep
, "kick act=%d/%d chunk_max=%d free_descs=%d\n",
182 act
, len
, ep
->epn
.chunk_max
, ast_vhub_count_free_descs(ep
));
184 /* While we can create descriptors */
185 while (ast_vhub_count_free_descs(ep
) && req
->last_desc
< 0) {
188 /* Grab next free descriptor */
189 d_num
= ep
->epn
.d_next
;
190 desc
= &ep
->epn
.descs
[d_num
];
191 ep
->epn
.d_next
= (d_num
+ 1) & (AST_VHUB_DESCS_COUNT
- 1);
193 /* Calculate next chunk size */
195 if (chunk
<= ep
->epn
.chunk_max
) {
197 * Is this the last packet ? Because of having up to 8
198 * packets in a descriptor we can't just compare "chunk"
199 * with ep.maxpacket. We have to see if it's a multiple
200 * of it to know if we have to send a zero packet.
201 * Sadly that involves a modulo which is a bit expensive
202 * but probably still better than not doing it.
204 if (!chunk
|| !req
->req
.zero
|| (chunk
% ep
->ep
.maxpacket
) != 0)
205 req
->last_desc
= d_num
;
207 chunk
= ep
->epn
.chunk_max
;
210 EPVDBG(ep
, " chunk: act=%d/%d chunk=%d last=%d desc=%d free=%d\n",
211 act
, len
, chunk
, req
->last_desc
, d_num
,
212 ast_vhub_count_free_descs(ep
));
214 /* Populate descriptor */
215 desc
->w0
= cpu_to_le32(req
->req
.dma
+ act
);
217 /* Interrupt if end of request or no more descriptors */
220 * TODO: Be smarter about it, if we don't have enough
221 * descriptors request an interrupt before queue empty
222 * or so in order to be able to populate more before
223 * the HW runs out. This isn't a problem at the moment
224 * as we use 256 descriptors and only put at most one
225 * request in the ring.
227 desc
->w1
= cpu_to_le32(VHUB_DSC1_IN_SET_LEN(chunk
));
228 if (req
->last_desc
>= 0 || !ast_vhub_count_free_descs(ep
))
229 desc
->w1
|= cpu_to_le32(VHUB_DSC1_IN_INTERRUPT
);
232 req
->act_count
= act
= act
+ chunk
;
236 vhub_dma_workaround(desc
);
238 /* Tell HW about new descriptors */
239 writel(VHUB_EP_DMA_SET_CPU_WPTR(ep
->epn
.d_next
),
240 ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
);
242 EPVDBG(ep
, "HW kicked, d_next=%d dstat=%08x\n",
243 ep
->epn
.d_next
, readl(ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
));
246 static void ast_vhub_epn_handle_ack_desc(struct ast_vhub_ep
*ep
)
248 struct ast_vhub_req
*req
;
249 unsigned int len
, d_last
;
252 /* Read EP status, workaround HW race */
254 stat
= readl(ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
);
255 stat1
= readl(ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
);
256 } while(stat
!= stat1
);
259 d_last
= VHUB_EP_DMA_RPTR(stat
);
261 /* Grab current request if any */
262 req
= list_first_entry_or_null(&ep
->queue
, struct ast_vhub_req
, queue
);
264 EPVDBG(ep
, "ACK status=%08x is_in=%d ep->d_last=%d..%d\n",
265 stat
, ep
->epn
.is_in
, ep
->epn
.d_last
, d_last
);
267 /* Check all completed descriptors */
268 while (ep
->epn
.d_last
!= d_last
) {
269 struct ast_vhub_desc
*desc
;
273 /* Grab next completed descriptor */
274 d_num
= ep
->epn
.d_last
;
275 desc
= &ep
->epn
.descs
[d_num
];
276 ep
->epn
.d_last
= (d_num
+ 1) & (AST_VHUB_DESCS_COUNT
- 1);
278 /* Grab len out of descriptor */
279 len
= VHUB_DSC1_IN_LEN(le32_to_cpu(desc
->w1
));
281 EPVDBG(ep
, " desc %d len=%d req=%p (act=%d)\n",
282 d_num
, len
, req
, req
? req
->active
: 0);
284 /* If no active request pending, move on */
285 if (!req
|| !req
->active
)
289 req
->req
.actual
+= len
;
291 /* Is that the last chunk ? */
292 is_last_desc
= req
->last_desc
== d_num
;
293 CHECK(ep
, is_last_desc
== (len
< ep
->ep
.maxpacket
||
294 (req
->req
.actual
>= req
->req
.length
&&
296 "Last packet discrepancy: last_desc=%d len=%d r.act=%d "
297 "r.len=%d r.zero=%d mp=%d\n",
298 is_last_desc
, len
, req
->req
.actual
, req
->req
.length
,
299 req
->req
.zero
, ep
->ep
.maxpacket
);
303 * Because we can only have one request at a time
304 * in our descriptor list in this implementation,
305 * d_last and ep->d_last should now be equal
307 CHECK(ep
, d_last
== ep
->epn
.d_last
,
308 "DMA read ptr mismatch %d vs %d\n",
309 d_last
, ep
->epn
.d_last
);
311 /* Note: done will drop and re-acquire the lock */
312 ast_vhub_done(ep
, req
, 0);
313 req
= list_first_entry_or_null(&ep
->queue
,
322 ast_vhub_epn_kick_desc(ep
, req
);
325 void ast_vhub_epn_ack_irq(struct ast_vhub_ep
*ep
)
327 if (ep
->epn
.desc_mode
)
328 ast_vhub_epn_handle_ack_desc(ep
);
330 ast_vhub_epn_handle_ack(ep
);
333 static int ast_vhub_epn_queue(struct usb_ep
* u_ep
, struct usb_request
*u_req
,
336 struct ast_vhub_req
*req
= to_ast_req(u_req
);
337 struct ast_vhub_ep
*ep
= to_ast_ep(u_ep
);
338 struct ast_vhub
*vhub
= ep
->vhub
;
343 /* Paranoid checks */
344 if (!u_req
|| !u_req
->complete
|| !u_req
->buf
) {
345 dev_warn(&vhub
->pdev
->dev
, "Bogus EPn request ! u_req=%p\n", u_req
);
347 dev_warn(&vhub
->pdev
->dev
, "complete=%p internal=%d\n",
348 u_req
->complete
, req
->internal
);
353 /* Endpoint enabled ? */
354 if (!ep
->epn
.enabled
|| !u_ep
->desc
|| !ep
->dev
|| !ep
->d_idx
||
355 !ep
->dev
->enabled
|| ep
->dev
->suspended
) {
356 EPDBG(ep
, "Enqueuing request on wrong or disabled EP\n");
360 /* Map request for DMA if possible. For now, the rule for DMA is
363 * * For single stage mode (no descriptors):
365 * - The buffer is aligned to a 8 bytes boundary (HW requirement)
366 * - For a OUT endpoint, the request size is a multiple of the EP
367 * packet size (otherwise the controller will DMA past the end
368 * of the buffer if the host is sending a too long packet).
370 * * For descriptor mode (tx only for now), always.
372 * We could relax the latter by making the decision to use the bounce
373 * buffer based on the size of a given *segment* of the request rather
374 * than the whole request.
376 if (ep
->epn
.desc_mode
||
377 ((((unsigned long)u_req
->buf
& 7) == 0) &&
378 (ep
->epn
.is_in
|| !(u_req
->length
& (u_ep
->maxpacket
- 1))))) {
379 rc
= usb_gadget_map_request(&ep
->dev
->gadget
, u_req
,
382 dev_warn(&vhub
->pdev
->dev
,
383 "Request mapping failure %d\n", rc
);
389 EPVDBG(ep
, "enqueue req @%p\n", req
);
390 EPVDBG(ep
, " l=%d dma=0x%x zero=%d noshort=%d noirq=%d is_in=%d\n",
391 u_req
->length
, (u32
)u_req
->dma
, u_req
->zero
,
392 u_req
->short_not_ok
, u_req
->no_interrupt
,
395 /* Initialize request progress fields */
396 u_req
->status
= -EINPROGRESS
;
401 spin_lock_irqsave(&vhub
->lock
, flags
);
402 empty
= list_empty(&ep
->queue
);
404 /* Add request to list and kick processing if empty */
405 list_add_tail(&req
->queue
, &ep
->queue
);
407 if (ep
->epn
.desc_mode
)
408 ast_vhub_epn_kick_desc(ep
, req
);
410 ast_vhub_epn_kick(ep
, req
);
412 spin_unlock_irqrestore(&vhub
->lock
, flags
);
417 static void ast_vhub_stop_active_req(struct ast_vhub_ep
*ep
,
420 u32 state
, reg
, loops
;
422 /* Stop DMA activity */
423 writel(0, ep
->epn
.regs
+ AST_VHUB_EP_DMA_CTLSTAT
);
425 /* Wait for it to complete */
426 for (loops
= 0; loops
< 1000; loops
++) {
427 state
= readl(ep
->epn
.regs
+ AST_VHUB_EP_DMA_CTLSTAT
);
428 state
= VHUB_EP_DMA_PROC_STATUS(state
);
429 if (state
== EP_DMA_PROC_RX_IDLE
||
430 state
== EP_DMA_PROC_TX_IDLE
)
435 dev_warn(&ep
->vhub
->pdev
->dev
, "Timeout waiting for DMA\n");
437 /* If we don't have to restart the endpoint, that's it */
441 /* Restart the endpoint */
442 if (ep
->epn
.desc_mode
) {
444 * Take out descriptors by resetting the DMA read
445 * pointer to be equal to the CPU write pointer.
447 * Note: If we ever support creating descriptors for
448 * requests that aren't the head of the queue, we
449 * may have to do something more complex here,
450 * especially if the request being taken out is
451 * not the current head descriptors.
453 reg
= VHUB_EP_DMA_SET_RPTR(ep
->epn
.d_next
) |
454 VHUB_EP_DMA_SET_CPU_WPTR(ep
->epn
.d_next
);
455 writel(reg
, ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
);
457 /* Then turn it back on */
458 writel(ep
->epn
.dma_conf
,
459 ep
->epn
.regs
+ AST_VHUB_EP_DMA_CTLSTAT
);
461 /* Single mode: just turn it back on */
462 writel(ep
->epn
.dma_conf
,
463 ep
->epn
.regs
+ AST_VHUB_EP_DMA_CTLSTAT
);
467 static int ast_vhub_epn_dequeue(struct usb_ep
* u_ep
, struct usb_request
*u_req
)
469 struct ast_vhub_ep
*ep
= to_ast_ep(u_ep
);
470 struct ast_vhub
*vhub
= ep
->vhub
;
471 struct ast_vhub_req
*req
;
475 spin_lock_irqsave(&vhub
->lock
, flags
);
477 /* Make sure it's actually queued on this endpoint */
478 list_for_each_entry (req
, &ep
->queue
, queue
) {
479 if (&req
->req
== u_req
)
483 if (&req
->req
== u_req
) {
484 EPVDBG(ep
, "dequeue req @%p active=%d\n",
487 ast_vhub_stop_active_req(ep
, true);
488 ast_vhub_done(ep
, req
, -ECONNRESET
);
492 spin_unlock_irqrestore(&vhub
->lock
, flags
);
496 void ast_vhub_update_epn_stall(struct ast_vhub_ep
*ep
)
500 if (WARN_ON(ep
->d_idx
== 0))
502 reg
= readl(ep
->epn
.regs
+ AST_VHUB_EP_CONFIG
);
503 if (ep
->epn
.stalled
|| ep
->epn
.wedged
)
504 reg
|= VHUB_EP_CFG_STALL_CTRL
;
506 reg
&= ~VHUB_EP_CFG_STALL_CTRL
;
507 writel(reg
, ep
->epn
.regs
+ AST_VHUB_EP_CONFIG
);
509 if (!ep
->epn
.stalled
&& !ep
->epn
.wedged
)
510 writel(VHUB_EP_TOGGLE_SET_EPNUM(ep
->epn
.g_idx
),
511 ep
->vhub
->regs
+ AST_VHUB_EP_TOGGLE
);
514 static int ast_vhub_set_halt_and_wedge(struct usb_ep
* u_ep
, bool halt
,
517 struct ast_vhub_ep
*ep
= to_ast_ep(u_ep
);
518 struct ast_vhub
*vhub
= ep
->vhub
;
521 EPDBG(ep
, "Set halt (%d) & wedge (%d)\n", halt
, wedge
);
523 if (!u_ep
|| !u_ep
->desc
)
530 spin_lock_irqsave(&vhub
->lock
, flags
);
532 /* Fail with still-busy IN endpoints */
533 if (halt
&& ep
->epn
.is_in
&& !list_empty(&ep
->queue
)) {
534 spin_unlock_irqrestore(&vhub
->lock
, flags
);
537 ep
->epn
.stalled
= halt
;
538 ep
->epn
.wedged
= wedge
;
539 ast_vhub_update_epn_stall(ep
);
541 spin_unlock_irqrestore(&vhub
->lock
, flags
);
546 static int ast_vhub_epn_set_halt(struct usb_ep
*u_ep
, int value
)
548 return ast_vhub_set_halt_and_wedge(u_ep
, value
!= 0, false);
551 static int ast_vhub_epn_set_wedge(struct usb_ep
*u_ep
)
553 return ast_vhub_set_halt_and_wedge(u_ep
, true, true);
556 static int ast_vhub_epn_disable(struct usb_ep
* u_ep
)
558 struct ast_vhub_ep
*ep
= to_ast_ep(u_ep
);
559 struct ast_vhub
*vhub
= ep
->vhub
;
563 EPDBG(ep
, "Disabling !\n");
565 spin_lock_irqsave(&vhub
->lock
, flags
);
567 ep
->epn
.enabled
= false;
569 /* Stop active DMA if any */
570 ast_vhub_stop_active_req(ep
, false);
572 /* Disable endpoint */
573 writel(0, ep
->epn
.regs
+ AST_VHUB_EP_CONFIG
);
575 /* Disable ACK interrupt */
576 imask
= VHUB_EP_IRQ(ep
->epn
.g_idx
);
577 ep_ier
= readl(vhub
->regs
+ AST_VHUB_EP_ACK_IER
);
579 writel(ep_ier
, vhub
->regs
+ AST_VHUB_EP_ACK_IER
);
580 writel(imask
, vhub
->regs
+ AST_VHUB_EP_ACK_ISR
);
582 /* Nuke all pending requests */
583 ast_vhub_nuke(ep
, -ESHUTDOWN
);
585 /* No more descriptor associated with request */
588 spin_unlock_irqrestore(&vhub
->lock
, flags
);
593 static int ast_vhub_epn_enable(struct usb_ep
* u_ep
,
594 const struct usb_endpoint_descriptor
*desc
)
596 static const char *ep_type_string
[] __maybe_unused
= { "ctrl",
600 struct ast_vhub_ep
*ep
= to_ast_ep(u_ep
);
601 struct ast_vhub_dev
*dev
;
602 struct ast_vhub
*vhub
;
605 u32 ep_conf
, ep_ier
, imask
;
607 /* Check arguments */
611 maxpacket
= usb_endpoint_maxp(desc
);
612 if (!ep
->d_idx
|| !ep
->dev
||
613 desc
->bDescriptorType
!= USB_DT_ENDPOINT
||
614 maxpacket
== 0 || maxpacket
> ep
->ep
.maxpacket
) {
615 EPDBG(ep
, "Invalid EP enable,d_idx=%d,dev=%p,type=%d,mp=%d/%d\n",
616 ep
->d_idx
, ep
->dev
, desc
->bDescriptorType
,
617 maxpacket
, ep
->ep
.maxpacket
);
620 if (ep
->d_idx
!= usb_endpoint_num(desc
)) {
621 EPDBG(ep
, "EP number mismatch !\n");
625 if (ep
->epn
.enabled
) {
626 EPDBG(ep
, "Already enabled\n");
632 /* Check device state */
634 EPDBG(ep
, "Bogus device state: driver=%p speed=%d\n",
635 dev
->driver
, dev
->gadget
.speed
);
639 /* Grab some info from the descriptor */
640 ep
->epn
.is_in
= usb_endpoint_dir_in(desc
);
641 ep
->ep
.maxpacket
= maxpacket
;
642 type
= usb_endpoint_type(desc
);
643 ep
->epn
.d_next
= ep
->epn
.d_last
= 0;
644 ep
->epn
.is_iso
= false;
645 ep
->epn
.stalled
= false;
646 ep
->epn
.wedged
= false;
648 EPDBG(ep
, "Enabling [%s] %s num %d maxpacket=%d\n",
649 ep
->epn
.is_in
? "in" : "out", ep_type_string
[type
],
650 usb_endpoint_num(desc
), maxpacket
);
652 /* Can we use DMA descriptor mode ? */
653 ep
->epn
.desc_mode
= ep
->epn
.descs
&& ep
->epn
.is_in
;
654 if (ep
->epn
.desc_mode
)
655 memset(ep
->epn
.descs
, 0, 8 * AST_VHUB_DESCS_COUNT
);
658 * Large send function can send up to 8 packets from
659 * one descriptor with a limit of 4095 bytes.
661 ep
->epn
.chunk_max
= ep
->ep
.maxpacket
;
663 ep
->epn
.chunk_max
<<= 3;
664 while (ep
->epn
.chunk_max
> 4095)
665 ep
->epn
.chunk_max
-= ep
->ep
.maxpacket
;
669 case USB_ENDPOINT_XFER_CONTROL
:
670 EPDBG(ep
, "Only one control endpoint\n");
672 case USB_ENDPOINT_XFER_INT
:
673 ep_conf
= VHUB_EP_CFG_SET_TYPE(EP_TYPE_INT
);
675 case USB_ENDPOINT_XFER_BULK
:
676 ep_conf
= VHUB_EP_CFG_SET_TYPE(EP_TYPE_BULK
);
678 case USB_ENDPOINT_XFER_ISOC
:
679 ep_conf
= VHUB_EP_CFG_SET_TYPE(EP_TYPE_ISO
);
680 ep
->epn
.is_iso
= true;
686 /* Encode the rest of the EP config register */
687 if (maxpacket
< 1024)
688 ep_conf
|= VHUB_EP_CFG_SET_MAX_PKT(maxpacket
);
690 ep_conf
|= VHUB_EP_CFG_DIR_OUT
;
691 ep_conf
|= VHUB_EP_CFG_SET_EP_NUM(usb_endpoint_num(desc
));
692 ep_conf
|= VHUB_EP_CFG_ENABLE
;
693 ep_conf
|= VHUB_EP_CFG_SET_DEV(dev
->index
+ 1);
694 EPVDBG(ep
, "config=%08x\n", ep_conf
);
696 spin_lock_irqsave(&vhub
->lock
, flags
);
698 /* Disable HW and reset DMA */
699 writel(0, ep
->epn
.regs
+ AST_VHUB_EP_CONFIG
);
700 writel(VHUB_EP_DMA_CTRL_RESET
,
701 ep
->epn
.regs
+ AST_VHUB_EP_DMA_CTLSTAT
);
703 /* Configure and enable */
704 writel(ep_conf
, ep
->epn
.regs
+ AST_VHUB_EP_CONFIG
);
706 if (ep
->epn
.desc_mode
) {
707 /* Clear DMA status, including the DMA read ptr */
708 writel(0, ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
);
710 /* Set descriptor base */
711 writel(ep
->epn
.descs_dma
,
712 ep
->epn
.regs
+ AST_VHUB_EP_DESC_BASE
);
714 /* Set base DMA config value */
715 ep
->epn
.dma_conf
= VHUB_EP_DMA_DESC_MODE
;
717 ep
->epn
.dma_conf
|= VHUB_EP_DMA_IN_LONG_MODE
;
719 /* First reset and disable all operations */
720 writel(ep
->epn
.dma_conf
| VHUB_EP_DMA_CTRL_RESET
,
721 ep
->epn
.regs
+ AST_VHUB_EP_DMA_CTLSTAT
);
723 /* Enable descriptor mode */
724 writel(ep
->epn
.dma_conf
,
725 ep
->epn
.regs
+ AST_VHUB_EP_DMA_CTLSTAT
);
727 /* Set base DMA config value */
728 ep
->epn
.dma_conf
= VHUB_EP_DMA_SINGLE_STAGE
;
730 /* Reset and switch to single stage mode */
731 writel(ep
->epn
.dma_conf
| VHUB_EP_DMA_CTRL_RESET
,
732 ep
->epn
.regs
+ AST_VHUB_EP_DMA_CTLSTAT
);
733 writel(ep
->epn
.dma_conf
,
734 ep
->epn
.regs
+ AST_VHUB_EP_DMA_CTLSTAT
);
735 writel(0, ep
->epn
.regs
+ AST_VHUB_EP_DESC_STATUS
);
738 /* Cleanup data toggle just in case */
739 writel(VHUB_EP_TOGGLE_SET_EPNUM(ep
->epn
.g_idx
),
740 vhub
->regs
+ AST_VHUB_EP_TOGGLE
);
742 /* Cleanup and enable ACK interrupt */
743 imask
= VHUB_EP_IRQ(ep
->epn
.g_idx
);
744 writel(imask
, vhub
->regs
+ AST_VHUB_EP_ACK_ISR
);
745 ep_ier
= readl(vhub
->regs
+ AST_VHUB_EP_ACK_IER
);
747 writel(ep_ier
, vhub
->regs
+ AST_VHUB_EP_ACK_IER
);
749 /* Woot, we are online ! */
750 ep
->epn
.enabled
= true;
752 spin_unlock_irqrestore(&vhub
->lock
, flags
);
757 static void ast_vhub_epn_dispose(struct usb_ep
*u_ep
)
759 struct ast_vhub_ep
*ep
= to_ast_ep(u_ep
);
761 if (WARN_ON(!ep
->dev
|| !ep
->d_idx
))
764 EPDBG(ep
, "Releasing endpoint\n");
766 /* Take it out of the EP list */
767 list_del_init(&ep
->ep
.ep_list
);
769 /* Mark the address free in the device */
770 ep
->dev
->epns
[ep
->d_idx
- 1] = NULL
;
772 /* Free name & DMA buffers */
775 dma_free_coherent(&ep
->vhub
->pdev
->dev
,
776 AST_VHUB_EPn_MAX_PACKET
+
777 8 * AST_VHUB_DESCS_COUNT
,
778 ep
->buf
, ep
->buf_dma
);
780 ep
->epn
.descs
= NULL
;
786 static const struct usb_ep_ops ast_vhub_epn_ops
= {
787 .enable
= ast_vhub_epn_enable
,
788 .disable
= ast_vhub_epn_disable
,
789 .dispose
= ast_vhub_epn_dispose
,
790 .queue
= ast_vhub_epn_queue
,
791 .dequeue
= ast_vhub_epn_dequeue
,
792 .set_halt
= ast_vhub_epn_set_halt
,
793 .set_wedge
= ast_vhub_epn_set_wedge
,
794 .alloc_request
= ast_vhub_alloc_request
,
795 .free_request
= ast_vhub_free_request
,
798 struct ast_vhub_ep
*ast_vhub_alloc_epn(struct ast_vhub_dev
*d
, u8 addr
)
800 struct ast_vhub
*vhub
= d
->vhub
;
801 struct ast_vhub_ep
*ep
;
805 /* Find a free one (no device) */
806 spin_lock_irqsave(&vhub
->lock
, flags
);
807 for (i
= 0; i
< AST_VHUB_NUM_GEN_EPs
; i
++)
808 if (vhub
->epns
[i
].dev
== NULL
)
810 if (i
>= AST_VHUB_NUM_GEN_EPs
) {
811 spin_unlock_irqrestore(&vhub
->lock
, flags
);
818 spin_unlock_irqrestore(&vhub
->lock
, flags
);
820 DDBG(d
, "Allocating gen EP %d for addr %d\n", i
, addr
);
821 INIT_LIST_HEAD(&ep
->queue
);
824 ep
->ep
.ops
= &ast_vhub_epn_ops
;
825 ep
->ep
.name
= kasprintf(GFP_KERNEL
, "ep%d", addr
);
826 d
->epns
[addr
-1] = ep
;
828 ep
->epn
.regs
= vhub
->regs
+ 0x200 + (i
* 0x10);
830 ep
->buf
= dma_alloc_coherent(&vhub
->pdev
->dev
,
831 AST_VHUB_EPn_MAX_PACKET
+
832 8 * AST_VHUB_DESCS_COUNT
,
833 &ep
->buf_dma
, GFP_KERNEL
);
839 ep
->epn
.descs
= ep
->buf
+ AST_VHUB_EPn_MAX_PACKET
;
840 ep
->epn
.descs_dma
= ep
->buf_dma
+ AST_VHUB_EPn_MAX_PACKET
;
842 usb_ep_set_maxpacket_limit(&ep
->ep
, AST_VHUB_EPn_MAX_PACKET
);
843 list_add_tail(&ep
->ep
.ep_list
, &d
->gadget
.ep_list
);
844 ep
->ep
.caps
.type_iso
= true;
845 ep
->ep
.caps
.type_bulk
= true;
846 ep
->ep
.caps
.type_int
= true;
847 ep
->ep
.caps
.dir_in
= true;
848 ep
->ep
.caps
.dir_out
= true;