xref: /rk3399_rockchip-uboot/drivers/usb/host/xhci-ring.c (revision 1422d140fb12f38f941f6f1712fb738a0261c4d3)
15853e133SVivek Gautam /*
25853e133SVivek Gautam  * USB HOST XHCI Controller stack
35853e133SVivek Gautam  *
45853e133SVivek Gautam  * Based on xHCI host controller driver in linux-kernel
55853e133SVivek Gautam  * by Sarah Sharp.
65853e133SVivek Gautam  *
75853e133SVivek Gautam  * Copyright (C) 2008 Intel Corp.
85853e133SVivek Gautam  * Author: Sarah Sharp
95853e133SVivek Gautam  *
105853e133SVivek Gautam  * Copyright (C) 2013 Samsung Electronics Co.Ltd
115853e133SVivek Gautam  * Authors: Vivek Gautam <gautam.vivek@samsung.com>
125853e133SVivek Gautam  *	    Vikas Sajjan <vikas.sajjan@samsung.com>
135853e133SVivek Gautam  *
145853e133SVivek Gautam  * SPDX-License-Identifier:	GPL-2.0+
155853e133SVivek Gautam  */
165853e133SVivek Gautam 
175853e133SVivek Gautam #include <common.h>
185853e133SVivek Gautam #include <asm/byteorder.h>
195853e133SVivek Gautam #include <usb.h>
205853e133SVivek Gautam #include <asm/unaligned.h>
215d97dff0SMasahiro Yamada #include <linux/errno.h>
225853e133SVivek Gautam 
23143fc13bSJean-Jacques Hiblot #include <usb/xhci.h>
245853e133SVivek Gautam 
255853e133SVivek Gautam /**
265853e133SVivek Gautam  * Is this TRB a link TRB or was the last TRB the last TRB in this event ring
275853e133SVivek Gautam  * segment?  I.e. would the updated event TRB pointer step off the end of the
285853e133SVivek Gautam  * event seg ?
295853e133SVivek Gautam  *
305853e133SVivek Gautam  * @param ctrl	Host controller data structure
315853e133SVivek Gautam  * @param ring	pointer to the ring
325853e133SVivek Gautam  * @param seg	poniter to the segment to which TRB belongs
335853e133SVivek Gautam  * @param trb	poniter to the ring trb
345853e133SVivek Gautam  * @return 1 if this TRB a link TRB else 0
355853e133SVivek Gautam  */
last_trb(struct xhci_ctrl * ctrl,struct xhci_ring * ring,struct xhci_segment * seg,union xhci_trb * trb)365853e133SVivek Gautam static int last_trb(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
375853e133SVivek Gautam 			struct xhci_segment *seg, union xhci_trb *trb)
385853e133SVivek Gautam {
395853e133SVivek Gautam 	if (ring == ctrl->event_ring)
405853e133SVivek Gautam 		return trb == &seg->trbs[TRBS_PER_SEGMENT];
415853e133SVivek Gautam 	else
425853e133SVivek Gautam 		return TRB_TYPE_LINK_LE32(trb->link.control);
435853e133SVivek Gautam }
445853e133SVivek Gautam 
455853e133SVivek Gautam /**
465853e133SVivek Gautam  * Does this link TRB point to the first segment in a ring,
475853e133SVivek Gautam  * or was the previous TRB the last TRB on the last segment in the ERST?
485853e133SVivek Gautam  *
495853e133SVivek Gautam  * @param ctrl	Host controller data structure
505853e133SVivek Gautam  * @param ring	pointer to the ring
515853e133SVivek Gautam  * @param seg	poniter to the segment to which TRB belongs
525853e133SVivek Gautam  * @param trb	poniter to the ring trb
535853e133SVivek Gautam  * @return 1 if this TRB is the last TRB on the last segment else 0
545853e133SVivek Gautam  */
last_trb_on_last_seg(struct xhci_ctrl * ctrl,struct xhci_ring * ring,struct xhci_segment * seg,union xhci_trb * trb)555853e133SVivek Gautam static bool last_trb_on_last_seg(struct xhci_ctrl *ctrl,
565853e133SVivek Gautam 				 struct xhci_ring *ring,
575853e133SVivek Gautam 				 struct xhci_segment *seg,
585853e133SVivek Gautam 				 union xhci_trb *trb)
595853e133SVivek Gautam {
605853e133SVivek Gautam 	if (ring == ctrl->event_ring)
615853e133SVivek Gautam 		return ((trb == &seg->trbs[TRBS_PER_SEGMENT]) &&
625853e133SVivek Gautam 			(seg->next == ring->first_seg));
635853e133SVivek Gautam 	else
645853e133SVivek Gautam 		return le32_to_cpu(trb->link.control) & LINK_TOGGLE;
655853e133SVivek Gautam }
665853e133SVivek Gautam 
675853e133SVivek Gautam /**
685853e133SVivek Gautam  * See Cycle bit rules. SW is the consumer for the event ring only.
695853e133SVivek Gautam  * Don't make a ring full of link TRBs.  That would be dumb and this would loop.
705853e133SVivek Gautam  *
715853e133SVivek Gautam  * If we've just enqueued a TRB that is in the middle of a TD (meaning the
725853e133SVivek Gautam  * chain bit is set), then set the chain bit in all the following link TRBs.
735853e133SVivek Gautam  * If we've enqueued the last TRB in a TD, make sure the following link TRBs
745853e133SVivek Gautam  * have their chain bit cleared (so that each Link TRB is a separate TD).
755853e133SVivek Gautam  *
765853e133SVivek Gautam  * Section 6.4.4.1 of the 0.95 spec says link TRBs cannot have the chain bit
775853e133SVivek Gautam  * set, but other sections talk about dealing with the chain bit set.  This was
785853e133SVivek Gautam  * fixed in the 0.96 specification errata, but we have to assume that all 0.95
795853e133SVivek Gautam  * xHCI hardware can't handle the chain bit being cleared on a link TRB.
805853e133SVivek Gautam  *
815853e133SVivek Gautam  * @param ctrl	Host controller data structure
825853e133SVivek Gautam  * @param ring	pointer to the ring
835853e133SVivek Gautam  * @param more_trbs_coming	flag to indicate whether more trbs
845853e133SVivek Gautam  *				are expected or NOT.
855853e133SVivek Gautam  *				Will you enqueue more TRBs before calling
865853e133SVivek Gautam  *				prepare_ring()?
875853e133SVivek Gautam  * @return none
885853e133SVivek Gautam  */
inc_enq(struct xhci_ctrl * ctrl,struct xhci_ring * ring,bool more_trbs_coming)895853e133SVivek Gautam static void inc_enq(struct xhci_ctrl *ctrl, struct xhci_ring *ring,
905853e133SVivek Gautam 						bool more_trbs_coming)
915853e133SVivek Gautam {
925853e133SVivek Gautam 	u32 chain;
935853e133SVivek Gautam 	union xhci_trb *next;
945853e133SVivek Gautam 
955853e133SVivek Gautam 	chain = le32_to_cpu(ring->enqueue->generic.field[3]) & TRB_CHAIN;
965853e133SVivek Gautam 	next = ++(ring->enqueue);
975853e133SVivek Gautam 
985853e133SVivek Gautam 	/*
995853e133SVivek Gautam 	 * Update the dequeue pointer further if that was a link TRB or we're at
1005853e133SVivek Gautam 	 * the end of an event ring segment (which doesn't have link TRBS)
1015853e133SVivek Gautam 	 */
1025853e133SVivek Gautam 	while (last_trb(ctrl, ring, ring->enq_seg, next)) {
1035853e133SVivek Gautam 		if (ring != ctrl->event_ring) {
1045853e133SVivek Gautam 			/*
1055853e133SVivek Gautam 			 * If the caller doesn't plan on enqueueing more
1065853e133SVivek Gautam 			 * TDs before ringing the doorbell, then we
1075853e133SVivek Gautam 			 * don't want to give the link TRB to the
1085853e133SVivek Gautam 			 * hardware just yet.  We'll give the link TRB
1095853e133SVivek Gautam 			 * back in prepare_ring() just before we enqueue
1105853e133SVivek Gautam 			 * the TD at the top of the ring.
1115853e133SVivek Gautam 			 */
1125853e133SVivek Gautam 			if (!chain && !more_trbs_coming)
1135853e133SVivek Gautam 				break;
1145853e133SVivek Gautam 
1155853e133SVivek Gautam 			/*
1165853e133SVivek Gautam 			 * If we're not dealing with 0.95 hardware or
1175853e133SVivek Gautam 			 * isoc rings on AMD 0.96 host,
1185853e133SVivek Gautam 			 * carry over the chain bit of the previous TRB
1195853e133SVivek Gautam 			 * (which may mean the chain bit is cleared).
1205853e133SVivek Gautam 			 */
1215853e133SVivek Gautam 			next->link.control &= cpu_to_le32(~TRB_CHAIN);
1225853e133SVivek Gautam 			next->link.control |= cpu_to_le32(chain);
1235853e133SVivek Gautam 
1245853e133SVivek Gautam 			next->link.control ^= cpu_to_le32(TRB_CYCLE);
125421a5a0cSSergey Temerkhanov 			xhci_flush_cache((uintptr_t)next,
1265853e133SVivek Gautam 					 sizeof(union xhci_trb));
1275853e133SVivek Gautam 		}
1285853e133SVivek Gautam 		/* Toggle the cycle bit after the last ring segment. */
1295853e133SVivek Gautam 		if (last_trb_on_last_seg(ctrl, ring,
1305853e133SVivek Gautam 					ring->enq_seg, next))
1315853e133SVivek Gautam 			ring->cycle_state = (ring->cycle_state ? 0 : 1);
1325853e133SVivek Gautam 
1335853e133SVivek Gautam 		ring->enq_seg = ring->enq_seg->next;
1345853e133SVivek Gautam 		ring->enqueue = ring->enq_seg->trbs;
1355853e133SVivek Gautam 		next = ring->enqueue;
1365853e133SVivek Gautam 	}
1375853e133SVivek Gautam }
1385853e133SVivek Gautam 
1395853e133SVivek Gautam /**
1405853e133SVivek Gautam  * See Cycle bit rules. SW is the consumer for the event ring only.
1415853e133SVivek Gautam  * Don't make a ring full of link TRBs.  That would be dumb and this would loop.
1425853e133SVivek Gautam  *
1435853e133SVivek Gautam  * @param ctrl	Host controller data structure
1445853e133SVivek Gautam  * @param ring	Ring whose Dequeue TRB pointer needs to be incremented.
1455853e133SVivek Gautam  * return none
1465853e133SVivek Gautam  */
inc_deq(struct xhci_ctrl * ctrl,struct xhci_ring * ring)1475853e133SVivek Gautam static void inc_deq(struct xhci_ctrl *ctrl, struct xhci_ring *ring)
1485853e133SVivek Gautam {
1495853e133SVivek Gautam 	do {
1505853e133SVivek Gautam 		/*
1515853e133SVivek Gautam 		 * Update the dequeue pointer further if that was a link TRB or
1525853e133SVivek Gautam 		 * we're at the end of an event ring segment (which doesn't have
1535853e133SVivek Gautam 		 * link TRBS)
1545853e133SVivek Gautam 		 */
1555853e133SVivek Gautam 		if (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue)) {
1565853e133SVivek Gautam 			if (ring == ctrl->event_ring &&
1575853e133SVivek Gautam 					last_trb_on_last_seg(ctrl, ring,
1585853e133SVivek Gautam 						ring->deq_seg, ring->dequeue)) {
1595853e133SVivek Gautam 				ring->cycle_state = (ring->cycle_state ? 0 : 1);
1605853e133SVivek Gautam 			}
1615853e133SVivek Gautam 			ring->deq_seg = ring->deq_seg->next;
1625853e133SVivek Gautam 			ring->dequeue = ring->deq_seg->trbs;
1635853e133SVivek Gautam 		} else {
1645853e133SVivek Gautam 			ring->dequeue++;
1655853e133SVivek Gautam 		}
1665853e133SVivek Gautam 	} while (last_trb(ctrl, ring, ring->deq_seg, ring->dequeue));
1675853e133SVivek Gautam }
1685853e133SVivek Gautam 
1695853e133SVivek Gautam /**
1705853e133SVivek Gautam  * Generic function for queueing a TRB on a ring.
1715853e133SVivek Gautam  * The caller must have checked to make sure there's room on the ring.
1725853e133SVivek Gautam  *
1735853e133SVivek Gautam  * @param	more_trbs_coming:   Will you enqueue more TRBs before calling
1745853e133SVivek Gautam  *				prepare_ring()?
1755853e133SVivek Gautam  * @param ctrl	Host controller data structure
1765853e133SVivek Gautam  * @param ring	pointer to the ring
1775853e133SVivek Gautam  * @param more_trbs_coming	flag to indicate whether more trbs
1785853e133SVivek Gautam  * @param trb_fields	pointer to trb field array containing TRB contents
1795853e133SVivek Gautam  * @return pointer to the enqueued trb
1805853e133SVivek Gautam  */
queue_trb(struct xhci_ctrl * ctrl,struct xhci_ring * ring,bool more_trbs_coming,unsigned int * trb_fields)1815853e133SVivek Gautam static struct xhci_generic_trb *queue_trb(struct xhci_ctrl *ctrl,
1825853e133SVivek Gautam 					  struct xhci_ring *ring,
1835853e133SVivek Gautam 					  bool more_trbs_coming,
1845853e133SVivek Gautam 					  unsigned int *trb_fields)
1855853e133SVivek Gautam {
1865853e133SVivek Gautam 	struct xhci_generic_trb *trb;
1875853e133SVivek Gautam 	int i;
1885853e133SVivek Gautam 
1895853e133SVivek Gautam 	trb = &ring->enqueue->generic;
1905853e133SVivek Gautam 
1915853e133SVivek Gautam 	for (i = 0; i < 4; i++)
1925853e133SVivek Gautam 		trb->field[i] = cpu_to_le32(trb_fields[i]);
1935853e133SVivek Gautam 
194421a5a0cSSergey Temerkhanov 	xhci_flush_cache((uintptr_t)trb, sizeof(struct xhci_generic_trb));
1955853e133SVivek Gautam 
1965853e133SVivek Gautam 	inc_enq(ctrl, ring, more_trbs_coming);
1975853e133SVivek Gautam 
1985853e133SVivek Gautam 	return trb;
1995853e133SVivek Gautam }
2005853e133SVivek Gautam 
2015853e133SVivek Gautam /**
2025853e133SVivek Gautam  * Does various checks on the endpoint ring, and makes it ready
2035853e133SVivek Gautam  * to queue num_trbs.
2045853e133SVivek Gautam  *
2055853e133SVivek Gautam  * @param ctrl		Host controller data structure
2065853e133SVivek Gautam  * @param ep_ring	pointer to the EP Transfer Ring
2075853e133SVivek Gautam  * @param ep_state	State of the End Point
2085853e133SVivek Gautam  * @return error code in case of invalid ep_state, 0 on success
2095853e133SVivek Gautam  */
prepare_ring(struct xhci_ctrl * ctrl,struct xhci_ring * ep_ring,u32 ep_state)2105853e133SVivek Gautam static int prepare_ring(struct xhci_ctrl *ctrl, struct xhci_ring *ep_ring,
2115853e133SVivek Gautam 							u32 ep_state)
2125853e133SVivek Gautam {
2135853e133SVivek Gautam 	union xhci_trb *next = ep_ring->enqueue;
2145853e133SVivek Gautam 
2155853e133SVivek Gautam 	/* Make sure the endpoint has been added to xHC schedule */
2165853e133SVivek Gautam 	switch (ep_state) {
2175853e133SVivek Gautam 	case EP_STATE_DISABLED:
2185853e133SVivek Gautam 		/*
2195853e133SVivek Gautam 		 * USB core changed config/interfaces without notifying us,
2205853e133SVivek Gautam 		 * or hardware is reporting the wrong state.
2215853e133SVivek Gautam 		 */
2225853e133SVivek Gautam 		puts("WARN urb submitted to disabled ep\n");
2235853e133SVivek Gautam 		return -ENOENT;
2245853e133SVivek Gautam 	case EP_STATE_ERROR:
2255853e133SVivek Gautam 		puts("WARN waiting for error on ep to be cleared\n");
2265853e133SVivek Gautam 		return -EINVAL;
2275853e133SVivek Gautam 	case EP_STATE_HALTED:
2285853e133SVivek Gautam 		puts("WARN halted endpoint, queueing URB anyway.\n");
2295853e133SVivek Gautam 	case EP_STATE_STOPPED:
2305853e133SVivek Gautam 	case EP_STATE_RUNNING:
2315853e133SVivek Gautam 		debug("EP STATE RUNNING.\n");
2325853e133SVivek Gautam 		break;
2335853e133SVivek Gautam 	default:
2345853e133SVivek Gautam 		puts("ERROR unknown endpoint state for ep\n");
2355853e133SVivek Gautam 		return -EINVAL;
2365853e133SVivek Gautam 	}
2375853e133SVivek Gautam 
2385853e133SVivek Gautam 	while (last_trb(ctrl, ep_ring, ep_ring->enq_seg, next)) {
2395853e133SVivek Gautam 		/*
2405853e133SVivek Gautam 		 * If we're not dealing with 0.95 hardware or isoc rings
2415853e133SVivek Gautam 		 * on AMD 0.96 host, clear the chain bit.
2425853e133SVivek Gautam 		 */
2435853e133SVivek Gautam 		next->link.control &= cpu_to_le32(~TRB_CHAIN);
2445853e133SVivek Gautam 
2455853e133SVivek Gautam 		next->link.control ^= cpu_to_le32(TRB_CYCLE);
2465853e133SVivek Gautam 
247421a5a0cSSergey Temerkhanov 		xhci_flush_cache((uintptr_t)next, sizeof(union xhci_trb));
2485853e133SVivek Gautam 
2495853e133SVivek Gautam 		/* Toggle the cycle bit after the last ring segment. */
2505853e133SVivek Gautam 		if (last_trb_on_last_seg(ctrl, ep_ring,
2515853e133SVivek Gautam 					ep_ring->enq_seg, next))
2525853e133SVivek Gautam 			ep_ring->cycle_state = (ep_ring->cycle_state ? 0 : 1);
2535853e133SVivek Gautam 		ep_ring->enq_seg = ep_ring->enq_seg->next;
2545853e133SVivek Gautam 		ep_ring->enqueue = ep_ring->enq_seg->trbs;
2555853e133SVivek Gautam 		next = ep_ring->enqueue;
2565853e133SVivek Gautam 	}
2575853e133SVivek Gautam 
2585853e133SVivek Gautam 	return 0;
2595853e133SVivek Gautam }
2605853e133SVivek Gautam 
2615853e133SVivek Gautam /**
2625853e133SVivek Gautam  * Generic function for queueing a command TRB on the command ring.
2635853e133SVivek Gautam  * Check to make sure there's room on the command ring for one command TRB.
2645853e133SVivek Gautam  *
2655853e133SVivek Gautam  * @param ctrl		Host controller data structure
2665853e133SVivek Gautam  * @param ptr		Pointer address to write in the first two fields (opt.)
2675853e133SVivek Gautam  * @param slot_id	Slot ID to encode in the flags field (opt.)
2685853e133SVivek Gautam  * @param ep_index	Endpoint index to encode in the flags field (opt.)
2695853e133SVivek Gautam  * @param cmd		Command type to enqueue
2705853e133SVivek Gautam  * @return none
2715853e133SVivek Gautam  */
xhci_queue_command(struct xhci_ctrl * ctrl,u8 * ptr,u32 slot_id,u32 ep_index,trb_type cmd)2725853e133SVivek Gautam void xhci_queue_command(struct xhci_ctrl *ctrl, u8 *ptr, u32 slot_id,
2735853e133SVivek Gautam 			u32 ep_index, trb_type cmd)
2745853e133SVivek Gautam {
2755853e133SVivek Gautam 	u32 fields[4];
2765853e133SVivek Gautam 	u64 val_64 = (uintptr_t)ptr;
2775853e133SVivek Gautam 
2785853e133SVivek Gautam 	BUG_ON(prepare_ring(ctrl, ctrl->cmd_ring, EP_STATE_RUNNING));
2795853e133SVivek Gautam 
2805853e133SVivek Gautam 	fields[0] = lower_32_bits(val_64);
2815853e133SVivek Gautam 	fields[1] = upper_32_bits(val_64);
2825853e133SVivek Gautam 	fields[2] = 0;
28343eb0d44SBin Meng 	fields[3] = TRB_TYPE(cmd) | SLOT_ID_FOR_TRB(slot_id) |
28443eb0d44SBin Meng 		    ctrl->cmd_ring->cycle_state;
28543eb0d44SBin Meng 
28643eb0d44SBin Meng 	/*
28743eb0d44SBin Meng 	 * Only 'reset endpoint', 'stop endpoint' and 'set TR dequeue pointer'
28843eb0d44SBin Meng 	 * commands need endpoint id encoded.
28943eb0d44SBin Meng 	 */
29043eb0d44SBin Meng 	if (cmd >= TRB_RESET_EP && cmd <= TRB_SET_DEQ)
29143eb0d44SBin Meng 		fields[3] |= EP_ID_FOR_TRB(ep_index);
2925853e133SVivek Gautam 
2935853e133SVivek Gautam 	queue_trb(ctrl, ctrl->cmd_ring, false, fields);
2945853e133SVivek Gautam 
2955853e133SVivek Gautam 	/* Ring the command ring doorbell */
2965853e133SVivek Gautam 	xhci_writel(&ctrl->dba->doorbell[0], DB_VALUE_HOST);
2975853e133SVivek Gautam }
2985853e133SVivek Gautam 
2995853e133SVivek Gautam /**
3005853e133SVivek Gautam  * The TD size is the number of bytes remaining in the TD (including this TRB),
3015853e133SVivek Gautam  * right shifted by 10.
3025853e133SVivek Gautam  * It must fit in bits 21:17, so it can't be bigger than 31.
3035853e133SVivek Gautam  *
3045853e133SVivek Gautam  * @param remainder	remaining packets to be sent
3055853e133SVivek Gautam  * @return remainder if remainder is less than max else max
3065853e133SVivek Gautam  */
xhci_td_remainder(unsigned int remainder)3075853e133SVivek Gautam static u32 xhci_td_remainder(unsigned int remainder)
3085853e133SVivek Gautam {
3095853e133SVivek Gautam 	u32 max = (1 << (21 - 17 + 1)) - 1;
3105853e133SVivek Gautam 
3115853e133SVivek Gautam 	if ((remainder >> 10) >= max)
3125853e133SVivek Gautam 		return max << 17;
3135853e133SVivek Gautam 	else
3145853e133SVivek Gautam 		return (remainder >> 10) << 17;
3155853e133SVivek Gautam }
3165853e133SVivek Gautam 
3175853e133SVivek Gautam /**
3185853e133SVivek Gautam  * Finds out the remanining packets to be sent
3195853e133SVivek Gautam  *
3205853e133SVivek Gautam  * @param running_total	total size sent so far
3215853e133SVivek Gautam  * @param trb_buff_len	length of the TRB Buffer
3225853e133SVivek Gautam  * @param total_packet_count	total packet count
3235853e133SVivek Gautam  * @param maxpacketsize		max packet size of current pipe
3245853e133SVivek Gautam  * @param num_trbs_left		number of TRBs left to be processed
3255853e133SVivek Gautam  * @return 0 if running_total or trb_buff_len is 0, else remainder
3265853e133SVivek Gautam  */
xhci_v1_0_td_remainder(int running_total,int trb_buff_len,unsigned int total_packet_count,int maxpacketsize,unsigned int num_trbs_left)3275853e133SVivek Gautam static u32 xhci_v1_0_td_remainder(int running_total,
3285853e133SVivek Gautam 				int trb_buff_len,
3295853e133SVivek Gautam 				unsigned int total_packet_count,
3305853e133SVivek Gautam 				int maxpacketsize,
3315853e133SVivek Gautam 				unsigned int num_trbs_left)
3325853e133SVivek Gautam {
3335853e133SVivek Gautam 	int packets_transferred;
3345853e133SVivek Gautam 
3355853e133SVivek Gautam 	/* One TRB with a zero-length data packet. */
3365853e133SVivek Gautam 	if (num_trbs_left == 0 || (running_total == 0 && trb_buff_len == 0))
3375853e133SVivek Gautam 		return 0;
3385853e133SVivek Gautam 
3395853e133SVivek Gautam 	/*
3405853e133SVivek Gautam 	 * All the TRB queueing functions don't count the current TRB in
3415853e133SVivek Gautam 	 * running_total.
3425853e133SVivek Gautam 	 */
3435853e133SVivek Gautam 	packets_transferred = (running_total + trb_buff_len) / maxpacketsize;
3445853e133SVivek Gautam 
3455853e133SVivek Gautam 	if ((total_packet_count - packets_transferred) > 31)
3465853e133SVivek Gautam 		return 31 << 17;
3475853e133SVivek Gautam 	return (total_packet_count - packets_transferred) << 17;
3485853e133SVivek Gautam }
3495853e133SVivek Gautam 
3505853e133SVivek Gautam /**
3515853e133SVivek Gautam  * Ring the doorbell of the End Point
3525853e133SVivek Gautam  *
3535853e133SVivek Gautam  * @param udev		pointer to the USB device structure
3545853e133SVivek Gautam  * @param ep_index	index of the endpoint
3555853e133SVivek Gautam  * @param start_cycle	cycle flag of the first TRB
3565853e133SVivek Gautam  * @param start_trb	pionter to the first TRB
3575853e133SVivek Gautam  * @return none
3585853e133SVivek Gautam  */
giveback_first_trb(struct usb_device * udev,int ep_index,int start_cycle,struct xhci_generic_trb * start_trb)3595853e133SVivek Gautam static void giveback_first_trb(struct usb_device *udev, int ep_index,
3605853e133SVivek Gautam 				int start_cycle,
3615853e133SVivek Gautam 				struct xhci_generic_trb *start_trb)
3625853e133SVivek Gautam {
3637c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
3645853e133SVivek Gautam 
3655853e133SVivek Gautam 	/*
3665853e133SVivek Gautam 	 * Pass all the TRBs to the hardware at once and make sure this write
3675853e133SVivek Gautam 	 * isn't reordered.
3685853e133SVivek Gautam 	 */
3695853e133SVivek Gautam 	if (start_cycle)
3705853e133SVivek Gautam 		start_trb->field[3] |= cpu_to_le32(start_cycle);
3715853e133SVivek Gautam 	else
3725853e133SVivek Gautam 		start_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
3735853e133SVivek Gautam 
374421a5a0cSSergey Temerkhanov 	xhci_flush_cache((uintptr_t)start_trb, sizeof(struct xhci_generic_trb));
3755853e133SVivek Gautam 
3765853e133SVivek Gautam 	/* Ringing EP doorbell here */
3775853e133SVivek Gautam 	xhci_writel(&ctrl->dba->doorbell[udev->slot_id],
3785853e133SVivek Gautam 				DB_VALUE(ep_index, 0));
3795853e133SVivek Gautam 
3805853e133SVivek Gautam 	return;
3815853e133SVivek Gautam }
3825853e133SVivek Gautam 
3835853e133SVivek Gautam /**** POLLING mechanism for XHCI ****/
3845853e133SVivek Gautam 
3855853e133SVivek Gautam /**
3865853e133SVivek Gautam  * Finalizes a handled event TRB by advancing our dequeue pointer and giving
3875853e133SVivek Gautam  * the TRB back to the hardware for recycling. Must call this exactly once at
3885853e133SVivek Gautam  * the end of each event handler, and not touch the TRB again afterwards.
3895853e133SVivek Gautam  *
3905853e133SVivek Gautam  * @param ctrl	Host controller data structure
3915853e133SVivek Gautam  * @return none
3925853e133SVivek Gautam  */
xhci_acknowledge_event(struct xhci_ctrl * ctrl)3935853e133SVivek Gautam void xhci_acknowledge_event(struct xhci_ctrl *ctrl)
3945853e133SVivek Gautam {
3955853e133SVivek Gautam 	/* Advance our dequeue pointer to the next event */
3965853e133SVivek Gautam 	inc_deq(ctrl, ctrl->event_ring);
3975853e133SVivek Gautam 
3985853e133SVivek Gautam 	/* Inform the hardware */
3995853e133SVivek Gautam 	xhci_writeq(&ctrl->ir_set->erst_dequeue,
4005853e133SVivek Gautam 		(uintptr_t)ctrl->event_ring->dequeue | ERST_EHB);
4015853e133SVivek Gautam }
4025853e133SVivek Gautam 
4035853e133SVivek Gautam /**
4045853e133SVivek Gautam  * Checks if there is a new event to handle on the event ring.
4055853e133SVivek Gautam  *
4065853e133SVivek Gautam  * @param ctrl	Host controller data structure
4075853e133SVivek Gautam  * @return 0 if failure else 1 on success
4085853e133SVivek Gautam  */
event_ready(struct xhci_ctrl * ctrl)4095853e133SVivek Gautam static int event_ready(struct xhci_ctrl *ctrl)
4105853e133SVivek Gautam {
4115853e133SVivek Gautam 	union xhci_trb *event;
4125853e133SVivek Gautam 
413421a5a0cSSergey Temerkhanov 	xhci_inval_cache((uintptr_t)ctrl->event_ring->dequeue,
4145853e133SVivek Gautam 			 sizeof(union xhci_trb));
4155853e133SVivek Gautam 
4165853e133SVivek Gautam 	event = ctrl->event_ring->dequeue;
4175853e133SVivek Gautam 
4185853e133SVivek Gautam 	/* Does the HC or OS own the TRB? */
4195853e133SVivek Gautam 	if ((le32_to_cpu(event->event_cmd.flags) & TRB_CYCLE) !=
4205853e133SVivek Gautam 		ctrl->event_ring->cycle_state)
4215853e133SVivek Gautam 		return 0;
4225853e133SVivek Gautam 
4235853e133SVivek Gautam 	return 1;
4245853e133SVivek Gautam }
4255853e133SVivek Gautam 
4265853e133SVivek Gautam /**
4275853e133SVivek Gautam  * Waits for a specific type of event and returns it. Discards unexpected
4285853e133SVivek Gautam  * events. Caller *must* call xhci_acknowledge_event() after it is finished
4295853e133SVivek Gautam  * processing the event, and must not access the returned pointer afterwards.
4305853e133SVivek Gautam  *
4315853e133SVivek Gautam  * @param ctrl		Host controller data structure
4325853e133SVivek Gautam  * @param expected	TRB type expected from Event TRB
4335853e133SVivek Gautam  * @return pointer to event trb
4345853e133SVivek Gautam  */
xhci_wait_for_event(struct xhci_ctrl * ctrl,trb_type expected)4355853e133SVivek Gautam union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected)
4365853e133SVivek Gautam {
4375853e133SVivek Gautam 	trb_type type;
4385853e133SVivek Gautam 	unsigned long ts = get_timer(0);
4395853e133SVivek Gautam 
4405853e133SVivek Gautam 	do {
4415853e133SVivek Gautam 		union xhci_trb *event = ctrl->event_ring->dequeue;
4425853e133SVivek Gautam 
4435853e133SVivek Gautam 		if (!event_ready(ctrl))
4445853e133SVivek Gautam 			continue;
4455853e133SVivek Gautam 
4465853e133SVivek Gautam 		type = TRB_FIELD_TO_TYPE(le32_to_cpu(event->event_cmd.flags));
4475853e133SVivek Gautam 		if (type == expected)
4485853e133SVivek Gautam 			return event;
4495853e133SVivek Gautam 
4505853e133SVivek Gautam 		if (type == TRB_PORT_STATUS)
4515853e133SVivek Gautam 		/* TODO: remove this once enumeration has been reworked */
4525853e133SVivek Gautam 			/*
4535853e133SVivek Gautam 			 * Port status change events always have a
4545853e133SVivek Gautam 			 * successful completion code
4555853e133SVivek Gautam 			 */
4565853e133SVivek Gautam 			BUG_ON(GET_COMP_CODE(
4575853e133SVivek Gautam 				le32_to_cpu(event->generic.field[2])) !=
4585853e133SVivek Gautam 								COMP_SUCCESS);
4595853e133SVivek Gautam 		else
4605853e133SVivek Gautam 			printf("Unexpected XHCI event TRB, skipping... "
4615853e133SVivek Gautam 				"(%08x %08x %08x %08x)\n",
4625853e133SVivek Gautam 				le32_to_cpu(event->generic.field[0]),
4635853e133SVivek Gautam 				le32_to_cpu(event->generic.field[1]),
4645853e133SVivek Gautam 				le32_to_cpu(event->generic.field[2]),
4655853e133SVivek Gautam 				le32_to_cpu(event->generic.field[3]));
4665853e133SVivek Gautam 
4675853e133SVivek Gautam 		xhci_acknowledge_event(ctrl);
4685853e133SVivek Gautam 	} while (get_timer(ts) < XHCI_TIMEOUT);
4695853e133SVivek Gautam 
4705853e133SVivek Gautam 	if (expected == TRB_TRANSFER)
4715853e133SVivek Gautam 		return NULL;
4725853e133SVivek Gautam 
4735853e133SVivek Gautam 	printf("XHCI timeout on event type %d... cannot recover.\n", expected);
4745853e133SVivek Gautam 	BUG();
4755853e133SVivek Gautam }
4765853e133SVivek Gautam 
4775853e133SVivek Gautam /*
4780c195942SStefan Agner  * Send reset endpoint command for given endpoint. This recovers from a
4790c195942SStefan Agner  * halted endpoint (e.g. due to a stall error).
4800c195942SStefan Agner  */
reset_ep(struct usb_device * udev,int ep_index)4810c195942SStefan Agner static void reset_ep(struct usb_device *udev, int ep_index)
4820c195942SStefan Agner {
4830c195942SStefan Agner 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
4840c195942SStefan Agner 	struct xhci_ring *ring =  ctrl->devs[udev->slot_id]->eps[ep_index].ring;
4850c195942SStefan Agner 	union xhci_trb *event;
4860c195942SStefan Agner 	u32 field;
4870c195942SStefan Agner 
4880c195942SStefan Agner 	printf("Resetting EP %d...\n", ep_index);
4890c195942SStefan Agner 	xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_RESET_EP);
4900c195942SStefan Agner 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
491*1422d140SHector Martin 	if (!event)
492*1422d140SHector Martin 		return;
493*1422d140SHector Martin 
4940c195942SStefan Agner 	field = le32_to_cpu(event->trans_event.flags);
4950c195942SStefan Agner 	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
4960c195942SStefan Agner 	xhci_acknowledge_event(ctrl);
4970c195942SStefan Agner 
4980c195942SStefan Agner 	xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue |
4990c195942SStefan Agner 		ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ);
5000c195942SStefan Agner 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
501*1422d140SHector Martin 	if (!event)
502*1422d140SHector Martin 		return;
503*1422d140SHector Martin 
5040c195942SStefan Agner 	BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
5050c195942SStefan Agner 		!= udev->slot_id || GET_COMP_CODE(le32_to_cpu(
5060c195942SStefan Agner 		event->event_cmd.status)) != COMP_SUCCESS);
5070c195942SStefan Agner 	xhci_acknowledge_event(ctrl);
5080c195942SStefan Agner }
5090c195942SStefan Agner 
5100c195942SStefan Agner /*
5115853e133SVivek Gautam  * Stops transfer processing for an endpoint and throws away all unprocessed
5125853e133SVivek Gautam  * TRBs by setting the xHC's dequeue pointer to our enqueue pointer. The next
5135853e133SVivek Gautam  * xhci_bulk_tx/xhci_ctrl_tx on this enpoint will add new transfers there and
5145853e133SVivek Gautam  * ring the doorbell, causing this endpoint to start working again.
5155853e133SVivek Gautam  * (Careful: This will BUG() when there was no transfer in progress. Shouldn't
5165853e133SVivek Gautam  * happen in practice for current uses and is too complicated to fix right now.)
5175853e133SVivek Gautam  */
abort_td(struct usb_device * udev,int ep_index)5185853e133SVivek Gautam static void abort_td(struct usb_device *udev, int ep_index)
5195853e133SVivek Gautam {
5207c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
5215853e133SVivek Gautam 	struct xhci_ring *ring =  ctrl->devs[udev->slot_id]->eps[ep_index].ring;
5225853e133SVivek Gautam 	union xhci_trb *event;
5235853e133SVivek Gautam 	u32 field;
5245853e133SVivek Gautam 
5255853e133SVivek Gautam 	xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_STOP_RING);
5265853e133SVivek Gautam 
5275853e133SVivek Gautam 	event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
528*1422d140SHector Martin 	if (!event)
529*1422d140SHector Martin 		return;
530*1422d140SHector Martin 
5315853e133SVivek Gautam 	field = le32_to_cpu(event->trans_event.flags);
5325853e133SVivek Gautam 	BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id);
5335853e133SVivek Gautam 	BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
5345853e133SVivek Gautam 	BUG_ON(GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len
5355853e133SVivek Gautam 		!= COMP_STOP)));
5365853e133SVivek Gautam 	xhci_acknowledge_event(ctrl);
5375853e133SVivek Gautam 
5385853e133SVivek Gautam 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
539*1422d140SHector Martin 	if (!event)
540*1422d140SHector Martin 		return;
541*1422d140SHector Martin 
5425853e133SVivek Gautam 	BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
5435853e133SVivek Gautam 		!= udev->slot_id || GET_COMP_CODE(le32_to_cpu(
5445853e133SVivek Gautam 		event->event_cmd.status)) != COMP_SUCCESS);
5455853e133SVivek Gautam 	xhci_acknowledge_event(ctrl);
5465853e133SVivek Gautam 
5475853e133SVivek Gautam 	xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue |
5485853e133SVivek Gautam 		ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ);
5495853e133SVivek Gautam 	event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
550*1422d140SHector Martin 	if (!event)
551*1422d140SHector Martin 		return;
552*1422d140SHector Martin 
5535853e133SVivek Gautam 	BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
5545853e133SVivek Gautam 		!= udev->slot_id || GET_COMP_CODE(le32_to_cpu(
5555853e133SVivek Gautam 		event->event_cmd.status)) != COMP_SUCCESS);
5565853e133SVivek Gautam 	xhci_acknowledge_event(ctrl);
5575853e133SVivek Gautam }
5585853e133SVivek Gautam 
record_transfer_result(struct usb_device * udev,union xhci_trb * event,int length)5595853e133SVivek Gautam static void record_transfer_result(struct usb_device *udev,
5605853e133SVivek Gautam 				   union xhci_trb *event, int length)
5615853e133SVivek Gautam {
5625853e133SVivek Gautam 	udev->act_len = min(length, length -
563b4141195SMasahiro Yamada 		(int)EVENT_TRB_LEN(le32_to_cpu(event->trans_event.transfer_len)));
5645853e133SVivek Gautam 
5655853e133SVivek Gautam 	switch (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))) {
5665853e133SVivek Gautam 	case COMP_SUCCESS:
5675853e133SVivek Gautam 		BUG_ON(udev->act_len != length);
5685853e133SVivek Gautam 		/* fallthrough */
5695853e133SVivek Gautam 	case COMP_SHORT_TX:
5705853e133SVivek Gautam 		udev->status = 0;
5715853e133SVivek Gautam 		break;
5725853e133SVivek Gautam 	case COMP_STALL:
5735853e133SVivek Gautam 		udev->status = USB_ST_STALLED;
5745853e133SVivek Gautam 		break;
5755853e133SVivek Gautam 	case COMP_DB_ERR:
5765853e133SVivek Gautam 	case COMP_TRB_ERR:
5775853e133SVivek Gautam 		udev->status = USB_ST_BUF_ERR;
5785853e133SVivek Gautam 		break;
5795853e133SVivek Gautam 	case COMP_BABBLE:
5805853e133SVivek Gautam 		udev->status = USB_ST_BABBLE_DET;
5815853e133SVivek Gautam 		break;
5825853e133SVivek Gautam 	default:
5835853e133SVivek Gautam 		udev->status = 0x80;  /* USB_ST_TOO_LAZY_TO_MAKE_A_NEW_MACRO */
5845853e133SVivek Gautam 	}
5855853e133SVivek Gautam }
5865853e133SVivek Gautam 
5875853e133SVivek Gautam /**** Bulk and Control transfer methods ****/
5885853e133SVivek Gautam /**
5895853e133SVivek Gautam  * Queues up the BULK Request
5905853e133SVivek Gautam  *
5915853e133SVivek Gautam  * @param udev		pointer to the USB device structure
5925853e133SVivek Gautam  * @param pipe		contains the DIR_IN or OUT , devnum
5935853e133SVivek Gautam  * @param length	length of the buffer
5945853e133SVivek Gautam  * @param buffer	buffer to be read/written based on the request
5955853e133SVivek Gautam  * @return returns 0 if successful else -1 on failure
5965853e133SVivek Gautam  */
xhci_bulk_tx(struct usb_device * udev,unsigned long pipe,int length,void * buffer)5975853e133SVivek Gautam int xhci_bulk_tx(struct usb_device *udev, unsigned long pipe,
5985853e133SVivek Gautam 			int length, void *buffer)
5995853e133SVivek Gautam {
6005853e133SVivek Gautam 	int num_trbs = 0;
6015853e133SVivek Gautam 	struct xhci_generic_trb *start_trb;
6020b717d3eSGustavo A. R. Silva 	bool first_trb = false;
6035853e133SVivek Gautam 	int start_cycle;
6045853e133SVivek Gautam 	u32 field = 0;
6055853e133SVivek Gautam 	u32 length_field = 0;
6067c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
6075853e133SVivek Gautam 	int slot_id = udev->slot_id;
6085853e133SVivek Gautam 	int ep_index;
6095853e133SVivek Gautam 	struct xhci_virt_device *virt_dev;
6105853e133SVivek Gautam 	struct xhci_ep_ctx *ep_ctx;
6115853e133SVivek Gautam 	struct xhci_ring *ring;		/* EP transfer ring */
6125853e133SVivek Gautam 	union xhci_trb *event;
6135853e133SVivek Gautam 
6145853e133SVivek Gautam 	int running_total, trb_buff_len;
6155853e133SVivek Gautam 	unsigned int total_packet_count;
6165853e133SVivek Gautam 	int maxpacketsize;
6175853e133SVivek Gautam 	u64 addr;
6185853e133SVivek Gautam 	int ret;
6195853e133SVivek Gautam 	u32 trb_fields[4];
6205853e133SVivek Gautam 	u64 val_64 = (uintptr_t)buffer;
6215853e133SVivek Gautam 
6225853e133SVivek Gautam 	debug("dev=%p, pipe=%lx, buffer=%p, length=%d\n",
6235853e133SVivek Gautam 		udev, pipe, buffer, length);
6245853e133SVivek Gautam 
6255853e133SVivek Gautam 	ep_index = usb_pipe_ep_index(pipe);
6265853e133SVivek Gautam 	virt_dev = ctrl->devs[slot_id];
6275853e133SVivek Gautam 
628421a5a0cSSergey Temerkhanov 	xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
6295853e133SVivek Gautam 			 virt_dev->out_ctx->size);
6305853e133SVivek Gautam 
6315853e133SVivek Gautam 	ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index);
6325853e133SVivek Gautam 
6335853e133SVivek Gautam 	ring = virt_dev->eps[ep_index].ring;
6345853e133SVivek Gautam 	/*
6355853e133SVivek Gautam 	 * How much data is (potentially) left before the 64KB boundary?
6365853e133SVivek Gautam 	 * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec)
6375853e133SVivek Gautam 	 * that the buffer should not span 64KB boundary. if so
6385853e133SVivek Gautam 	 * we send request in more than 1 TRB by chaining them.
6395853e133SVivek Gautam 	 */
6405853e133SVivek Gautam 	running_total = TRB_MAX_BUFF_SIZE -
6415853e133SVivek Gautam 			(lower_32_bits(val_64) & (TRB_MAX_BUFF_SIZE - 1));
6425853e133SVivek Gautam 	trb_buff_len = running_total;
6435853e133SVivek Gautam 	running_total &= TRB_MAX_BUFF_SIZE - 1;
6445853e133SVivek Gautam 
6455853e133SVivek Gautam 	/*
6465853e133SVivek Gautam 	 * If there's some data on this 64KB chunk, or we have to send a
6475853e133SVivek Gautam 	 * zero-length transfer, we need at least one TRB
6485853e133SVivek Gautam 	 */
6495853e133SVivek Gautam 	if (running_total != 0 || length == 0)
6505853e133SVivek Gautam 		num_trbs++;
6515853e133SVivek Gautam 
6525853e133SVivek Gautam 	/* How many more 64KB chunks to transfer, how many more TRBs? */
6535853e133SVivek Gautam 	while (running_total < length) {
6545853e133SVivek Gautam 		num_trbs++;
6555853e133SVivek Gautam 		running_total += TRB_MAX_BUFF_SIZE;
6565853e133SVivek Gautam 	}
6575853e133SVivek Gautam 
6585853e133SVivek Gautam 	/*
6595853e133SVivek Gautam 	 * XXX: Calling routine prepare_ring() called in place of
6605853e133SVivek Gautam 	 * prepare_trasfer() as there in 'Linux' since we are not
6615853e133SVivek Gautam 	 * maintaining multiple TDs/transfer at the same time.
6625853e133SVivek Gautam 	 */
6635853e133SVivek Gautam 	ret = prepare_ring(ctrl, ring,
6645853e133SVivek Gautam 			   le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK);
6655853e133SVivek Gautam 	if (ret < 0)
6665853e133SVivek Gautam 		return ret;
6675853e133SVivek Gautam 
6685853e133SVivek Gautam 	/*
6695853e133SVivek Gautam 	 * Don't give the first TRB to the hardware (by toggling the cycle bit)
6705853e133SVivek Gautam 	 * until we've finished creating all the other TRBs.  The ring's cycle
6715853e133SVivek Gautam 	 * state may change as we enqueue the other TRBs, so save it too.
6725853e133SVivek Gautam 	 */
6735853e133SVivek Gautam 	start_trb = &ring->enqueue->generic;
6745853e133SVivek Gautam 	start_cycle = ring->cycle_state;
6755853e133SVivek Gautam 
6765853e133SVivek Gautam 	running_total = 0;
6775853e133SVivek Gautam 	maxpacketsize = usb_maxpacket(udev, pipe);
6785853e133SVivek Gautam 
6795853e133SVivek Gautam 	total_packet_count = DIV_ROUND_UP(length, maxpacketsize);
6805853e133SVivek Gautam 
6815853e133SVivek Gautam 	/* How much data is in the first TRB? */
6825853e133SVivek Gautam 	/*
6835853e133SVivek Gautam 	 * How much data is (potentially) left before the 64KB boundary?
6845853e133SVivek Gautam 	 * XHCI Spec puts restriction( TABLE 49 and 6.4.1 section of XHCI Spec)
6855853e133SVivek Gautam 	 * that the buffer should not span 64KB boundary. if so
6865853e133SVivek Gautam 	 * we send request in more than 1 TRB by chaining them.
6875853e133SVivek Gautam 	 */
6885853e133SVivek Gautam 	addr = val_64;
6895853e133SVivek Gautam 
6905853e133SVivek Gautam 	if (trb_buff_len > length)
6915853e133SVivek Gautam 		trb_buff_len = length;
6925853e133SVivek Gautam 
6935853e133SVivek Gautam 	first_trb = true;
6945853e133SVivek Gautam 
6955853e133SVivek Gautam 	/* flush the buffer before use */
696421a5a0cSSergey Temerkhanov 	xhci_flush_cache((uintptr_t)buffer, length);
6975853e133SVivek Gautam 
6985853e133SVivek Gautam 	/* Queue the first TRB, even if it's zero-length */
6995853e133SVivek Gautam 	do {
7005853e133SVivek Gautam 		u32 remainder = 0;
7015853e133SVivek Gautam 		field = 0;
7025853e133SVivek Gautam 		/* Don't change the cycle bit of the first TRB until later */
7035853e133SVivek Gautam 		if (first_trb) {
7045853e133SVivek Gautam 			first_trb = false;
7055853e133SVivek Gautam 			if (start_cycle == 0)
7065853e133SVivek Gautam 				field |= TRB_CYCLE;
7075853e133SVivek Gautam 		} else {
7085853e133SVivek Gautam 			field |= ring->cycle_state;
7095853e133SVivek Gautam 		}
7105853e133SVivek Gautam 
7115853e133SVivek Gautam 		/*
7125853e133SVivek Gautam 		 * Chain all the TRBs together; clear the chain bit in the last
7135853e133SVivek Gautam 		 * TRB to indicate it's the last TRB in the chain.
7145853e133SVivek Gautam 		 */
7155853e133SVivek Gautam 		if (num_trbs > 1)
7165853e133SVivek Gautam 			field |= TRB_CHAIN;
7175853e133SVivek Gautam 		else
7185853e133SVivek Gautam 			field |= TRB_IOC;
7195853e133SVivek Gautam 
7205853e133SVivek Gautam 		/* Only set interrupt on short packet for IN endpoints */
7215853e133SVivek Gautam 		if (usb_pipein(pipe))
7225853e133SVivek Gautam 			field |= TRB_ISP;
7235853e133SVivek Gautam 
7245853e133SVivek Gautam 		/* Set the TRB length, TD size, and interrupter fields. */
7255853e133SVivek Gautam 		if (HC_VERSION(xhci_readl(&ctrl->hccr->cr_capbase)) < 0x100)
7265853e133SVivek Gautam 			remainder = xhci_td_remainder(length - running_total);
7275853e133SVivek Gautam 		else
7285853e133SVivek Gautam 			remainder = xhci_v1_0_td_remainder(running_total,
7295853e133SVivek Gautam 							   trb_buff_len,
7305853e133SVivek Gautam 							   total_packet_count,
7315853e133SVivek Gautam 							   maxpacketsize,
7325853e133SVivek Gautam 							   num_trbs - 1);
7335853e133SVivek Gautam 
7345853e133SVivek Gautam 		length_field = ((trb_buff_len & TRB_LEN_MASK) |
7355853e133SVivek Gautam 				remainder |
7365853e133SVivek Gautam 				((0 & TRB_INTR_TARGET_MASK) <<
7375853e133SVivek Gautam 				TRB_INTR_TARGET_SHIFT));
7385853e133SVivek Gautam 
7395853e133SVivek Gautam 		trb_fields[0] = lower_32_bits(addr);
7405853e133SVivek Gautam 		trb_fields[1] = upper_32_bits(addr);
7415853e133SVivek Gautam 		trb_fields[2] = length_field;
7425853e133SVivek Gautam 		trb_fields[3] = field | (TRB_NORMAL << TRB_TYPE_SHIFT);
7435853e133SVivek Gautam 
7445853e133SVivek Gautam 		queue_trb(ctrl, ring, (num_trbs > 1), trb_fields);
7455853e133SVivek Gautam 
7465853e133SVivek Gautam 		--num_trbs;
7475853e133SVivek Gautam 
7485853e133SVivek Gautam 		running_total += trb_buff_len;
7495853e133SVivek Gautam 
7505853e133SVivek Gautam 		/* Calculate length for next transfer */
7515853e133SVivek Gautam 		addr += trb_buff_len;
7525853e133SVivek Gautam 		trb_buff_len = min((length - running_total), TRB_MAX_BUFF_SIZE);
7535853e133SVivek Gautam 	} while (running_total < length);
7545853e133SVivek Gautam 
7555853e133SVivek Gautam 	giveback_first_trb(udev, ep_index, start_cycle, start_trb);
7565853e133SVivek Gautam 
7575853e133SVivek Gautam 	event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
7585853e133SVivek Gautam 	if (!event) {
7595853e133SVivek Gautam 		debug("XHCI bulk transfer timed out, aborting...\n");
7605853e133SVivek Gautam 		abort_td(udev, ep_index);
7615853e133SVivek Gautam 		udev->status = USB_ST_NAK_REC;  /* closest thing to a timeout */
7625853e133SVivek Gautam 		udev->act_len = 0;
7635853e133SVivek Gautam 		return -ETIMEDOUT;
7645853e133SVivek Gautam 	}
7655853e133SVivek Gautam 	field = le32_to_cpu(event->trans_event.flags);
7665853e133SVivek Gautam 
7675853e133SVivek Gautam 	BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
7685853e133SVivek Gautam 	BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
7695853e133SVivek Gautam 	BUG_ON(*(void **)(uintptr_t)le64_to_cpu(event->trans_event.buffer) -
7705853e133SVivek Gautam 		buffer > (size_t)length);
7715853e133SVivek Gautam 
7725853e133SVivek Gautam 	record_transfer_result(udev, event, length);
7735853e133SVivek Gautam 	xhci_acknowledge_event(ctrl);
774421a5a0cSSergey Temerkhanov 	xhci_inval_cache((uintptr_t)buffer, length);
7755853e133SVivek Gautam 
7765853e133SVivek Gautam 	return (udev->status != USB_ST_NOT_PROC) ? 0 : -1;
7775853e133SVivek Gautam }
7785853e133SVivek Gautam 
7795853e133SVivek Gautam /**
7805853e133SVivek Gautam  * Queues up the Control Transfer Request
7815853e133SVivek Gautam  *
7825853e133SVivek Gautam  * @param udev	pointer to the USB device structure
7835853e133SVivek Gautam  * @param pipe		contains the DIR_IN or OUT , devnum
7845853e133SVivek Gautam  * @param req		request type
7855853e133SVivek Gautam  * @param length	length of the buffer
7865853e133SVivek Gautam  * @param buffer	buffer to be read/written based on the request
7875853e133SVivek Gautam  * @return returns 0 if successful else error code on failure
7885853e133SVivek Gautam  */
xhci_ctrl_tx(struct usb_device * udev,unsigned long pipe,struct devrequest * req,int length,void * buffer)7895853e133SVivek Gautam int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe,
7905853e133SVivek Gautam 			struct devrequest *req,	int length,
7915853e133SVivek Gautam 			void *buffer)
7925853e133SVivek Gautam {
7935853e133SVivek Gautam 	int ret;
7945853e133SVivek Gautam 	int start_cycle;
7955853e133SVivek Gautam 	int num_trbs;
7965853e133SVivek Gautam 	u32 field;
7975853e133SVivek Gautam 	u32 length_field;
7985853e133SVivek Gautam 	u64 buf_64 = 0;
7995853e133SVivek Gautam 	struct xhci_generic_trb *start_trb;
8007c1deec0SSimon Glass 	struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
8015853e133SVivek Gautam 	int slot_id = udev->slot_id;
8025853e133SVivek Gautam 	int ep_index;
8035853e133SVivek Gautam 	u32 trb_fields[4];
8045853e133SVivek Gautam 	struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
8055853e133SVivek Gautam 	struct xhci_ring *ep_ring;
8065853e133SVivek Gautam 	union xhci_trb *event;
8075853e133SVivek Gautam 
8085853e133SVivek Gautam 	debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
8095853e133SVivek Gautam 		req->request, req->request,
8105853e133SVivek Gautam 		req->requesttype, req->requesttype,
8115853e133SVivek Gautam 		le16_to_cpu(req->value), le16_to_cpu(req->value),
8125853e133SVivek Gautam 		le16_to_cpu(req->index));
8135853e133SVivek Gautam 
8145853e133SVivek Gautam 	ep_index = usb_pipe_ep_index(pipe);
8155853e133SVivek Gautam 
8165853e133SVivek Gautam 	ep_ring = virt_dev->eps[ep_index].ring;
8175853e133SVivek Gautam 
8185853e133SVivek Gautam 	/*
8195853e133SVivek Gautam 	 * Check to see if the max packet size for the default control
8205853e133SVivek Gautam 	 * endpoint changed during FS device enumeration
8215853e133SVivek Gautam 	 */
8225853e133SVivek Gautam 	if (udev->speed == USB_SPEED_FULL) {
8235853e133SVivek Gautam 		ret = xhci_check_maxpacket(udev);
8245853e133SVivek Gautam 		if (ret < 0)
8255853e133SVivek Gautam 			return ret;
8265853e133SVivek Gautam 	}
8275853e133SVivek Gautam 
828421a5a0cSSergey Temerkhanov 	xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
8295853e133SVivek Gautam 			 virt_dev->out_ctx->size);
8305853e133SVivek Gautam 
8315853e133SVivek Gautam 	struct xhci_ep_ctx *ep_ctx = NULL;
8325853e133SVivek Gautam 	ep_ctx = xhci_get_ep_ctx(ctrl, virt_dev->out_ctx, ep_index);
8335853e133SVivek Gautam 
8345853e133SVivek Gautam 	/* 1 TRB for setup, 1 for status */
8355853e133SVivek Gautam 	num_trbs = 2;
8365853e133SVivek Gautam 	/*
8375853e133SVivek Gautam 	 * Don't need to check if we need additional event data and normal TRBs,
8385853e133SVivek Gautam 	 * since data in control transfers will never get bigger than 16MB
8395853e133SVivek Gautam 	 * XXX: can we get a buffer that crosses 64KB boundaries?
8405853e133SVivek Gautam 	 */
8415853e133SVivek Gautam 
8425853e133SVivek Gautam 	if (length > 0)
8435853e133SVivek Gautam 		num_trbs++;
8445853e133SVivek Gautam 	/*
8455853e133SVivek Gautam 	 * XXX: Calling routine prepare_ring() called in place of
8465853e133SVivek Gautam 	 * prepare_trasfer() as there in 'Linux' since we are not
8475853e133SVivek Gautam 	 * maintaining multiple TDs/transfer at the same time.
8485853e133SVivek Gautam 	 */
8495853e133SVivek Gautam 	ret = prepare_ring(ctrl, ep_ring,
8505853e133SVivek Gautam 				le32_to_cpu(ep_ctx->ep_info) & EP_STATE_MASK);
8515853e133SVivek Gautam 
8525853e133SVivek Gautam 	if (ret < 0)
8535853e133SVivek Gautam 		return ret;
8545853e133SVivek Gautam 
8555853e133SVivek Gautam 	/*
8565853e133SVivek Gautam 	 * Don't give the first TRB to the hardware (by toggling the cycle bit)
8575853e133SVivek Gautam 	 * until we've finished creating all the other TRBs.  The ring's cycle
8585853e133SVivek Gautam 	 * state may change as we enqueue the other TRBs, so save it too.
8595853e133SVivek Gautam 	 */
8605853e133SVivek Gautam 	start_trb = &ep_ring->enqueue->generic;
8615853e133SVivek Gautam 	start_cycle = ep_ring->cycle_state;
8625853e133SVivek Gautam 
8635853e133SVivek Gautam 	debug("start_trb %p, start_cycle %d\n", start_trb, start_cycle);
8645853e133SVivek Gautam 
8655853e133SVivek Gautam 	/* Queue setup TRB - see section 6.4.1.2.1 */
8665853e133SVivek Gautam 	/* FIXME better way to translate setup_packet into two u32 fields? */
8675853e133SVivek Gautam 	field = 0;
8685853e133SVivek Gautam 	field |= TRB_IDT | (TRB_SETUP << TRB_TYPE_SHIFT);
8695853e133SVivek Gautam 	if (start_cycle == 0)
8705853e133SVivek Gautam 		field |= 0x1;
8715853e133SVivek Gautam 
8725853e133SVivek Gautam 	/* xHCI 1.0 6.4.1.2.1: Transfer Type field */
87358693cd5SChunfeng Yun 	if (HC_VERSION(xhci_readl(&ctrl->hccr->cr_capbase)) >= 0x100) {
8745853e133SVivek Gautam 		if (length > 0) {
8755853e133SVivek Gautam 			if (req->requesttype & USB_DIR_IN)
8765853e133SVivek Gautam 				field |= (TRB_DATA_IN << TRB_TX_TYPE_SHIFT);
8775853e133SVivek Gautam 			else
8785853e133SVivek Gautam 				field |= (TRB_DATA_OUT << TRB_TX_TYPE_SHIFT);
8795853e133SVivek Gautam 		}
8805853e133SVivek Gautam 	}
8815853e133SVivek Gautam 
8825853e133SVivek Gautam 	debug("req->requesttype = %d, req->request = %d,"
8835853e133SVivek Gautam 		"le16_to_cpu(req->value) = %d,"
8845853e133SVivek Gautam 		"le16_to_cpu(req->index) = %d,"
8855853e133SVivek Gautam 		"le16_to_cpu(req->length) = %d\n",
8865853e133SVivek Gautam 		req->requesttype, req->request, le16_to_cpu(req->value),
8875853e133SVivek Gautam 		le16_to_cpu(req->index), le16_to_cpu(req->length));
8885853e133SVivek Gautam 
8895853e133SVivek Gautam 	trb_fields[0] = req->requesttype | req->request << 8 |
8905853e133SVivek Gautam 				le16_to_cpu(req->value) << 16;
8915853e133SVivek Gautam 	trb_fields[1] = le16_to_cpu(req->index) |
8925853e133SVivek Gautam 			le16_to_cpu(req->length) << 16;
8935853e133SVivek Gautam 	/* TRB_LEN | (TRB_INTR_TARGET) */
8945853e133SVivek Gautam 	trb_fields[2] = (8 | ((0 & TRB_INTR_TARGET_MASK) <<
8955853e133SVivek Gautam 			TRB_INTR_TARGET_SHIFT));
8965853e133SVivek Gautam 	/* Immediate data in pointer */
8975853e133SVivek Gautam 	trb_fields[3] = field;
8985853e133SVivek Gautam 	queue_trb(ctrl, ep_ring, true, trb_fields);
8995853e133SVivek Gautam 
9005853e133SVivek Gautam 	/* Re-initializing field to zero */
9015853e133SVivek Gautam 	field = 0;
9025853e133SVivek Gautam 	/* If there's data, queue data TRBs */
9035853e133SVivek Gautam 	/* Only set interrupt on short packet for IN endpoints */
9045853e133SVivek Gautam 	if (usb_pipein(pipe))
9055853e133SVivek Gautam 		field = TRB_ISP | (TRB_DATA << TRB_TYPE_SHIFT);
9065853e133SVivek Gautam 	else
9075853e133SVivek Gautam 		field = (TRB_DATA << TRB_TYPE_SHIFT);
9085853e133SVivek Gautam 
9095853e133SVivek Gautam 	length_field = (length & TRB_LEN_MASK) | xhci_td_remainder(length) |
9105853e133SVivek Gautam 			((0 & TRB_INTR_TARGET_MASK) << TRB_INTR_TARGET_SHIFT);
9115853e133SVivek Gautam 	debug("length_field = %d, length = %d,"
9125853e133SVivek Gautam 		"xhci_td_remainder(length) = %d , TRB_INTR_TARGET(0) = %d\n",
9135853e133SVivek Gautam 		length_field, (length & TRB_LEN_MASK),
9145853e133SVivek Gautam 		xhci_td_remainder(length), 0);
9155853e133SVivek Gautam 
9165853e133SVivek Gautam 	if (length > 0) {
9175853e133SVivek Gautam 		if (req->requesttype & USB_DIR_IN)
9185853e133SVivek Gautam 			field |= TRB_DIR_IN;
9195853e133SVivek Gautam 		buf_64 = (uintptr_t)buffer;
9205853e133SVivek Gautam 
9215853e133SVivek Gautam 		trb_fields[0] = lower_32_bits(buf_64);
9225853e133SVivek Gautam 		trb_fields[1] = upper_32_bits(buf_64);
9235853e133SVivek Gautam 		trb_fields[2] = length_field;
9245853e133SVivek Gautam 		trb_fields[3] = field | ep_ring->cycle_state;
9255853e133SVivek Gautam 
926421a5a0cSSergey Temerkhanov 		xhci_flush_cache((uintptr_t)buffer, length);
9275853e133SVivek Gautam 		queue_trb(ctrl, ep_ring, true, trb_fields);
9285853e133SVivek Gautam 	}
9295853e133SVivek Gautam 
9305853e133SVivek Gautam 	/*
9315853e133SVivek Gautam 	 * Queue status TRB -
9325853e133SVivek Gautam 	 * see Table 7 and sections 4.11.2.2 and 6.4.1.2.3
9335853e133SVivek Gautam 	 */
9345853e133SVivek Gautam 
9355853e133SVivek Gautam 	/* If the device sent data, the status stage is an OUT transfer */
9365853e133SVivek Gautam 	field = 0;
9375853e133SVivek Gautam 	if (length > 0 && req->requesttype & USB_DIR_IN)
9385853e133SVivek Gautam 		field = 0;
9395853e133SVivek Gautam 	else
9405853e133SVivek Gautam 		field = TRB_DIR_IN;
9415853e133SVivek Gautam 
9425853e133SVivek Gautam 	trb_fields[0] = 0;
9435853e133SVivek Gautam 	trb_fields[1] = 0;
9445853e133SVivek Gautam 	trb_fields[2] = ((0 & TRB_INTR_TARGET_MASK) << TRB_INTR_TARGET_SHIFT);
9455853e133SVivek Gautam 		/* Event on completion */
9465853e133SVivek Gautam 	trb_fields[3] = field | TRB_IOC |
9475853e133SVivek Gautam 			(TRB_STATUS << TRB_TYPE_SHIFT) |
9485853e133SVivek Gautam 			ep_ring->cycle_state;
9495853e133SVivek Gautam 
9505853e133SVivek Gautam 	queue_trb(ctrl, ep_ring, false, trb_fields);
9515853e133SVivek Gautam 
9525853e133SVivek Gautam 	giveback_first_trb(udev, ep_index, start_cycle, start_trb);
9535853e133SVivek Gautam 
9545853e133SVivek Gautam 	event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
9555853e133SVivek Gautam 	if (!event)
9565853e133SVivek Gautam 		goto abort;
9575853e133SVivek Gautam 	field = le32_to_cpu(event->trans_event.flags);
9585853e133SVivek Gautam 
9595853e133SVivek Gautam 	BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
9605853e133SVivek Gautam 	BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
9615853e133SVivek Gautam 
9625853e133SVivek Gautam 	record_transfer_result(udev, event, length);
9635853e133SVivek Gautam 	xhci_acknowledge_event(ctrl);
9640c195942SStefan Agner 	if (udev->status == USB_ST_STALLED) {
9650c195942SStefan Agner 		reset_ep(udev, ep_index);
9660c195942SStefan Agner 		return -EPIPE;
9670c195942SStefan Agner 	}
9685853e133SVivek Gautam 
9695853e133SVivek Gautam 	/* Invalidate buffer to make it available to usb-core */
9705853e133SVivek Gautam 	if (length > 0)
971421a5a0cSSergey Temerkhanov 		xhci_inval_cache((uintptr_t)buffer, length);
9725853e133SVivek Gautam 
9735853e133SVivek Gautam 	if (GET_COMP_CODE(le32_to_cpu(event->trans_event.transfer_len))
9745853e133SVivek Gautam 			== COMP_SHORT_TX) {
9755853e133SVivek Gautam 		/* Short data stage, clear up additional status stage event */
9765853e133SVivek Gautam 		event = xhci_wait_for_event(ctrl, TRB_TRANSFER);
9775853e133SVivek Gautam 		if (!event)
9785853e133SVivek Gautam 			goto abort;
9795853e133SVivek Gautam 		BUG_ON(TRB_TO_SLOT_ID(field) != slot_id);
9805853e133SVivek Gautam 		BUG_ON(TRB_TO_EP_INDEX(field) != ep_index);
9815853e133SVivek Gautam 		xhci_acknowledge_event(ctrl);
9825853e133SVivek Gautam 	}
9835853e133SVivek Gautam 
9845853e133SVivek Gautam 	return (udev->status != USB_ST_NOT_PROC) ? 0 : -1;
9855853e133SVivek Gautam 
9865853e133SVivek Gautam abort:
9875853e133SVivek Gautam 	debug("XHCI control transfer timed out, aborting...\n");
9885853e133SVivek Gautam 	abort_td(udev, ep_index);
9895853e133SVivek Gautam 	udev->status = USB_ST_NAK_REC;
9905853e133SVivek Gautam 	udev->act_len = 0;
9915853e133SVivek Gautam 	return -ETIMEDOUT;
9925853e133SVivek Gautam }
993