xref: /OK3568_Linux_fs/kernel/drivers/usb/host/fhci-tds.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Freescale QUICC Engine USB Host Controller Driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) Freescale Semicondutor, Inc. 2006.
6*4882a593Smuzhiyun  *               Shlomi Gridish <gridish@freescale.com>
7*4882a593Smuzhiyun  *               Jerry Huang <Chang-Ming.Huang@freescale.com>
8*4882a593Smuzhiyun  * Copyright (c) Logic Product Development, Inc. 2007
9*4882a593Smuzhiyun  *               Peter Barada <peterb@logicpd.com>
10*4882a593Smuzhiyun  * Copyright (c) MontaVista Software, Inc. 2008.
11*4882a593Smuzhiyun  *               Anton Vorontsov <avorontsov@ru.mvista.com>
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/types.h>
16*4882a593Smuzhiyun #include <linux/errno.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/list.h>
19*4882a593Smuzhiyun #include <linux/io.h>
20*4882a593Smuzhiyun #include <linux/usb.h>
21*4882a593Smuzhiyun #include <linux/usb/hcd.h>
22*4882a593Smuzhiyun #include "fhci.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define DUMMY_BD_BUFFER  0xdeadbeef
25*4882a593Smuzhiyun #define DUMMY2_BD_BUFFER 0xbaadf00d
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /* Transaction Descriptors bits */
28*4882a593Smuzhiyun #define TD_R		0x8000 /* ready bit */
29*4882a593Smuzhiyun #define TD_W		0x2000 /* wrap bit */
30*4882a593Smuzhiyun #define TD_I		0x1000 /* interrupt on completion */
31*4882a593Smuzhiyun #define TD_L		0x0800 /* last */
32*4882a593Smuzhiyun #define TD_TC		0x0400 /* transmit CRC */
33*4882a593Smuzhiyun #define TD_CNF		0x0200 /* CNF - Must be always 1 */
34*4882a593Smuzhiyun #define TD_LSP		0x0100 /* Low-speed transaction */
35*4882a593Smuzhiyun #define TD_PID		0x00c0 /* packet id */
36*4882a593Smuzhiyun #define TD_RXER		0x0020 /* Rx error or not */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define TD_NAK		0x0010 /* No ack. */
39*4882a593Smuzhiyun #define TD_STAL		0x0008 /* Stall received */
40*4882a593Smuzhiyun #define TD_TO		0x0004 /* time out */
41*4882a593Smuzhiyun #define TD_UN		0x0002 /* underrun */
42*4882a593Smuzhiyun #define TD_NO		0x0010 /* Rx Non Octet Aligned Packet */
43*4882a593Smuzhiyun #define TD_AB		0x0008 /* Frame Aborted */
44*4882a593Smuzhiyun #define TD_CR		0x0004 /* CRC Error */
45*4882a593Smuzhiyun #define TD_OV		0x0002 /* Overrun */
46*4882a593Smuzhiyun #define TD_BOV		0x0001 /* Buffer Overrun */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define TD_ERRORS	(TD_NAK | TD_STAL | TD_TO | TD_UN | \
49*4882a593Smuzhiyun 			 TD_NO | TD_AB | TD_CR | TD_OV | TD_BOV)
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define TD_PID_DATA0	0x0080 /* Data 0 toggle */
52*4882a593Smuzhiyun #define TD_PID_DATA1	0x00c0 /* Data 1 toggle */
53*4882a593Smuzhiyun #define TD_PID_TOGGLE	0x00c0 /* Data 0/1 toggle mask */
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun #define TD_TOK_SETUP	0x0000
56*4882a593Smuzhiyun #define TD_TOK_OUT	0x4000
57*4882a593Smuzhiyun #define TD_TOK_IN	0x8000
58*4882a593Smuzhiyun #define TD_ISO		0x1000
59*4882a593Smuzhiyun #define TD_ENDP		0x0780
60*4882a593Smuzhiyun #define TD_ADDR		0x007f
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #define TD_ENDP_SHIFT 7
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun struct usb_td {
65*4882a593Smuzhiyun 	__be16 status;
66*4882a593Smuzhiyun 	__be16 length;
67*4882a593Smuzhiyun 	__be32 buf_ptr;
68*4882a593Smuzhiyun 	__be16 extra;
69*4882a593Smuzhiyun 	__be16 reserved;
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
next_bd(struct usb_td __iomem * base,struct usb_td __iomem * td,u16 status)72*4882a593Smuzhiyun static struct usb_td __iomem *next_bd(struct usb_td __iomem *base,
73*4882a593Smuzhiyun 				      struct usb_td __iomem *td,
74*4882a593Smuzhiyun 				      u16 status)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	if (status & TD_W)
77*4882a593Smuzhiyun 		return base;
78*4882a593Smuzhiyun 	else
79*4882a593Smuzhiyun 		return ++td;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
fhci_push_dummy_bd(struct endpoint * ep)82*4882a593Smuzhiyun void fhci_push_dummy_bd(struct endpoint *ep)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	if (!ep->already_pushed_dummy_bd) {
85*4882a593Smuzhiyun 		u16 td_status = in_be16(&ep->empty_td->status);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 		out_be32(&ep->empty_td->buf_ptr, DUMMY_BD_BUFFER);
88*4882a593Smuzhiyun 		/* get the next TD in the ring */
89*4882a593Smuzhiyun 		ep->empty_td = next_bd(ep->td_base, ep->empty_td, td_status);
90*4882a593Smuzhiyun 		ep->already_pushed_dummy_bd = true;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /* destroy an USB endpoint */
fhci_ep0_free(struct fhci_usb * usb)95*4882a593Smuzhiyun void fhci_ep0_free(struct fhci_usb *usb)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	struct endpoint *ep;
98*4882a593Smuzhiyun 	int size;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	ep = usb->ep0;
101*4882a593Smuzhiyun 	if (ep) {
102*4882a593Smuzhiyun 		if (ep->td_base)
103*4882a593Smuzhiyun 			cpm_muram_free(cpm_muram_offset(ep->td_base));
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 		if (kfifo_initialized(&ep->conf_frame_Q)) {
106*4882a593Smuzhiyun 			size = cq_howmany(&ep->conf_frame_Q);
107*4882a593Smuzhiyun 			for (; size; size--) {
108*4882a593Smuzhiyun 				struct packet *pkt = cq_get(&ep->conf_frame_Q);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 				kfree(pkt);
111*4882a593Smuzhiyun 			}
112*4882a593Smuzhiyun 			cq_delete(&ep->conf_frame_Q);
113*4882a593Smuzhiyun 		}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 		if (kfifo_initialized(&ep->empty_frame_Q)) {
116*4882a593Smuzhiyun 			size = cq_howmany(&ep->empty_frame_Q);
117*4882a593Smuzhiyun 			for (; size; size--) {
118*4882a593Smuzhiyun 				struct packet *pkt = cq_get(&ep->empty_frame_Q);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 				kfree(pkt);
121*4882a593Smuzhiyun 			}
122*4882a593Smuzhiyun 			cq_delete(&ep->empty_frame_Q);
123*4882a593Smuzhiyun 		}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 		if (kfifo_initialized(&ep->dummy_packets_Q)) {
126*4882a593Smuzhiyun 			size = cq_howmany(&ep->dummy_packets_Q);
127*4882a593Smuzhiyun 			for (; size; size--) {
128*4882a593Smuzhiyun 				u8 *buff = cq_get(&ep->dummy_packets_Q);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 				kfree(buff);
131*4882a593Smuzhiyun 			}
132*4882a593Smuzhiyun 			cq_delete(&ep->dummy_packets_Q);
133*4882a593Smuzhiyun 		}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 		kfree(ep);
136*4882a593Smuzhiyun 		usb->ep0 = NULL;
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun  * create the endpoint structure
142*4882a593Smuzhiyun  *
143*4882a593Smuzhiyun  * arguments:
144*4882a593Smuzhiyun  * usb		A pointer to the data structure of the USB
145*4882a593Smuzhiyun  * data_mem	The data memory partition(BUS)
146*4882a593Smuzhiyun  * ring_len	TD ring length
147*4882a593Smuzhiyun  */
fhci_create_ep(struct fhci_usb * usb,enum fhci_mem_alloc data_mem,u32 ring_len)148*4882a593Smuzhiyun u32 fhci_create_ep(struct fhci_usb *usb, enum fhci_mem_alloc data_mem,
149*4882a593Smuzhiyun 			   u32 ring_len)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	struct endpoint *ep;
152*4882a593Smuzhiyun 	struct usb_td __iomem *td;
153*4882a593Smuzhiyun 	unsigned long ep_offset;
154*4882a593Smuzhiyun 	char *err_for = "endpoint PRAM";
155*4882a593Smuzhiyun 	int ep_mem_size;
156*4882a593Smuzhiyun 	u32 i;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	/* we need at least 3 TDs in the ring */
159*4882a593Smuzhiyun 	if (!(ring_len > 2)) {
160*4882a593Smuzhiyun 		fhci_err(usb->fhci, "illegal TD ring length parameters\n");
161*4882a593Smuzhiyun 		return -EINVAL;
162*4882a593Smuzhiyun 	}
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
165*4882a593Smuzhiyun 	if (!ep)
166*4882a593Smuzhiyun 		return -ENOMEM;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	ep_mem_size = ring_len * sizeof(*td) + sizeof(struct fhci_ep_pram);
169*4882a593Smuzhiyun 	ep_offset = cpm_muram_alloc(ep_mem_size, 32);
170*4882a593Smuzhiyun 	if (IS_ERR_VALUE(ep_offset))
171*4882a593Smuzhiyun 		goto err;
172*4882a593Smuzhiyun 	ep->td_base = cpm_muram_addr(ep_offset);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* zero all queue pointers */
175*4882a593Smuzhiyun 	if (cq_new(&ep->conf_frame_Q, ring_len + 2) ||
176*4882a593Smuzhiyun 	    cq_new(&ep->empty_frame_Q, ring_len + 2) ||
177*4882a593Smuzhiyun 	    cq_new(&ep->dummy_packets_Q, ring_len + 2)) {
178*4882a593Smuzhiyun 		err_for = "frame_queues";
179*4882a593Smuzhiyun 		goto err;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	for (i = 0; i < (ring_len + 1); i++) {
183*4882a593Smuzhiyun 		struct packet *pkt;
184*4882a593Smuzhiyun 		u8 *buff;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 		pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
187*4882a593Smuzhiyun 		if (!pkt) {
188*4882a593Smuzhiyun 			err_for = "frame";
189*4882a593Smuzhiyun 			goto err;
190*4882a593Smuzhiyun 		}
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 		buff = kmalloc_array(1028, sizeof(*buff), GFP_KERNEL);
193*4882a593Smuzhiyun 		if (!buff) {
194*4882a593Smuzhiyun 			kfree(pkt);
195*4882a593Smuzhiyun 			err_for = "buffer";
196*4882a593Smuzhiyun 			goto err;
197*4882a593Smuzhiyun 		}
198*4882a593Smuzhiyun 		cq_put(&ep->empty_frame_Q, pkt);
199*4882a593Smuzhiyun 		cq_put(&ep->dummy_packets_Q, buff);
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/* we put the endpoint parameter RAM right behind the TD ring */
203*4882a593Smuzhiyun 	ep->ep_pram_ptr = (void __iomem *)ep->td_base + sizeof(*td) * ring_len;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	ep->conf_td = ep->td_base;
206*4882a593Smuzhiyun 	ep->empty_td = ep->td_base;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	ep->already_pushed_dummy_bd = false;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* initialize tds */
211*4882a593Smuzhiyun 	td = ep->td_base;
212*4882a593Smuzhiyun 	for (i = 0; i < ring_len; i++) {
213*4882a593Smuzhiyun 		out_be32(&td->buf_ptr, 0);
214*4882a593Smuzhiyun 		out_be16(&td->status, 0);
215*4882a593Smuzhiyun 		out_be16(&td->length, 0);
216*4882a593Smuzhiyun 		out_be16(&td->extra, 0);
217*4882a593Smuzhiyun 		td++;
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 	td--;
220*4882a593Smuzhiyun 	out_be16(&td->status, TD_W); /* for last TD set Wrap bit */
221*4882a593Smuzhiyun 	out_be16(&td->length, 0);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	/* endpoint structure has been created */
224*4882a593Smuzhiyun 	usb->ep0 = ep;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	return 0;
227*4882a593Smuzhiyun err:
228*4882a593Smuzhiyun 	fhci_ep0_free(usb);
229*4882a593Smuzhiyun 	kfree(ep);
230*4882a593Smuzhiyun 	fhci_err(usb->fhci, "no memory for the %s\n", err_for);
231*4882a593Smuzhiyun 	return -ENOMEM;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /*
235*4882a593Smuzhiyun  * initialize the endpoint register according to the given parameters
236*4882a593Smuzhiyun  *
237*4882a593Smuzhiyun  * artuments:
238*4882a593Smuzhiyun  * usb		A pointer to the data strucutre of the USB
239*4882a593Smuzhiyun  * ep		A pointer to the endpoint structre
240*4882a593Smuzhiyun  * data_mem	The data memory partition(BUS)
241*4882a593Smuzhiyun  */
fhci_init_ep_registers(struct fhci_usb * usb,struct endpoint * ep,enum fhci_mem_alloc data_mem)242*4882a593Smuzhiyun void fhci_init_ep_registers(struct fhci_usb *usb, struct endpoint *ep,
243*4882a593Smuzhiyun 			    enum fhci_mem_alloc data_mem)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	u8 rt;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/* set the endpoint registers according to the endpoint */
248*4882a593Smuzhiyun 	out_be16(&usb->fhci->regs->usb_usep[0],
249*4882a593Smuzhiyun 		 USB_TRANS_CTR | USB_EP_MF | USB_EP_RTE);
250*4882a593Smuzhiyun 	out_be16(&usb->fhci->pram->ep_ptr[0],
251*4882a593Smuzhiyun 		 cpm_muram_offset(ep->ep_pram_ptr));
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	rt = (BUS_MODE_BO_BE | BUS_MODE_GBL);
254*4882a593Smuzhiyun #ifdef MULTI_DATA_BUS
255*4882a593Smuzhiyun 	if (data_mem == MEM_SECONDARY)
256*4882a593Smuzhiyun 		rt |= BUS_MODE_DTB;
257*4882a593Smuzhiyun #endif
258*4882a593Smuzhiyun 	out_8(&ep->ep_pram_ptr->rx_func_code, rt);
259*4882a593Smuzhiyun 	out_8(&ep->ep_pram_ptr->tx_func_code, rt);
260*4882a593Smuzhiyun 	out_be16(&ep->ep_pram_ptr->rx_buff_len, 1028);
261*4882a593Smuzhiyun 	out_be16(&ep->ep_pram_ptr->rx_base, 0);
262*4882a593Smuzhiyun 	out_be16(&ep->ep_pram_ptr->tx_base, cpm_muram_offset(ep->td_base));
263*4882a593Smuzhiyun 	out_be16(&ep->ep_pram_ptr->rx_bd_ptr, 0);
264*4882a593Smuzhiyun 	out_be16(&ep->ep_pram_ptr->tx_bd_ptr, cpm_muram_offset(ep->td_base));
265*4882a593Smuzhiyun 	out_be32(&ep->ep_pram_ptr->tx_state, 0);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun /*
269*4882a593Smuzhiyun  * Collect the submitted frames and inform the application about them
270*4882a593Smuzhiyun  * It is also preparing the TDs for new frames. If the Tx interrupts
271*4882a593Smuzhiyun  * are disabled, the application should call that routine to get
272*4882a593Smuzhiyun  * confirmation about the submitted frames. Otherwise, the routine is
273*4882a593Smuzhiyun  * called from the interrupt service routine during the Tx interrupt.
274*4882a593Smuzhiyun  * In that case the application is informed by calling the application
275*4882a593Smuzhiyun  * specific 'fhci_transaction_confirm' routine
276*4882a593Smuzhiyun  */
fhci_td_transaction_confirm(struct fhci_usb * usb)277*4882a593Smuzhiyun static void fhci_td_transaction_confirm(struct fhci_usb *usb)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	struct endpoint *ep = usb->ep0;
280*4882a593Smuzhiyun 	struct packet *pkt;
281*4882a593Smuzhiyun 	struct usb_td __iomem *td;
282*4882a593Smuzhiyun 	u16 extra_data;
283*4882a593Smuzhiyun 	u16 td_status;
284*4882a593Smuzhiyun 	u16 td_length;
285*4882a593Smuzhiyun 	u32 buf;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	/*
288*4882a593Smuzhiyun 	 * collect transmitted BDs from the chip. The routine clears all BDs
289*4882a593Smuzhiyun 	 * with R bit = 0 and the pointer to data buffer is not NULL, that is
290*4882a593Smuzhiyun 	 * BDs which point to the transmitted data buffer
291*4882a593Smuzhiyun 	 */
292*4882a593Smuzhiyun 	while (1) {
293*4882a593Smuzhiyun 		td = ep->conf_td;
294*4882a593Smuzhiyun 		td_status = in_be16(&td->status);
295*4882a593Smuzhiyun 		td_length = in_be16(&td->length);
296*4882a593Smuzhiyun 		buf = in_be32(&td->buf_ptr);
297*4882a593Smuzhiyun 		extra_data = in_be16(&td->extra);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		/* check if the TD is empty */
300*4882a593Smuzhiyun 		if (!(!(td_status & TD_R) && ((td_status & ~TD_W) || buf)))
301*4882a593Smuzhiyun 			break;
302*4882a593Smuzhiyun 		/* check if it is a dummy buffer */
303*4882a593Smuzhiyun 		else if ((buf == DUMMY_BD_BUFFER) && !(td_status & ~TD_W))
304*4882a593Smuzhiyun 			break;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 		/* mark TD as empty */
307*4882a593Smuzhiyun 		clrbits16(&td->status, ~TD_W);
308*4882a593Smuzhiyun 		out_be16(&td->length, 0);
309*4882a593Smuzhiyun 		out_be32(&td->buf_ptr, 0);
310*4882a593Smuzhiyun 		out_be16(&td->extra, 0);
311*4882a593Smuzhiyun 		/* advance the TD pointer */
312*4882a593Smuzhiyun 		ep->conf_td = next_bd(ep->td_base, ep->conf_td, td_status);
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 		/* check if it is a dummy buffer(type2) */
315*4882a593Smuzhiyun 		if ((buf == DUMMY2_BD_BUFFER) && !(td_status & ~TD_W))
316*4882a593Smuzhiyun 			continue;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 		pkt = cq_get(&ep->conf_frame_Q);
319*4882a593Smuzhiyun 		if (!pkt)
320*4882a593Smuzhiyun 			fhci_err(usb->fhci, "no frame to confirm\n");
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 		if (td_status & TD_ERRORS) {
323*4882a593Smuzhiyun 			if (td_status & TD_RXER) {
324*4882a593Smuzhiyun 				if (td_status & TD_CR)
325*4882a593Smuzhiyun 					pkt->status = USB_TD_RX_ER_CRC;
326*4882a593Smuzhiyun 				else if (td_status & TD_AB)
327*4882a593Smuzhiyun 					pkt->status = USB_TD_RX_ER_BITSTUFF;
328*4882a593Smuzhiyun 				else if (td_status & TD_OV)
329*4882a593Smuzhiyun 					pkt->status = USB_TD_RX_ER_OVERUN;
330*4882a593Smuzhiyun 				else if (td_status & TD_BOV)
331*4882a593Smuzhiyun 					pkt->status = USB_TD_RX_DATA_OVERUN;
332*4882a593Smuzhiyun 				else if (td_status & TD_NO)
333*4882a593Smuzhiyun 					pkt->status = USB_TD_RX_ER_NONOCT;
334*4882a593Smuzhiyun 				else
335*4882a593Smuzhiyun 					fhci_err(usb->fhci, "illegal error "
336*4882a593Smuzhiyun 						 "occurred\n");
337*4882a593Smuzhiyun 			} else if (td_status & TD_NAK)
338*4882a593Smuzhiyun 				pkt->status = USB_TD_TX_ER_NAK;
339*4882a593Smuzhiyun 			else if (td_status & TD_TO)
340*4882a593Smuzhiyun 				pkt->status = USB_TD_TX_ER_TIMEOUT;
341*4882a593Smuzhiyun 			else if (td_status & TD_UN)
342*4882a593Smuzhiyun 				pkt->status = USB_TD_TX_ER_UNDERUN;
343*4882a593Smuzhiyun 			else if (td_status & TD_STAL)
344*4882a593Smuzhiyun 				pkt->status = USB_TD_TX_ER_STALL;
345*4882a593Smuzhiyun 			else
346*4882a593Smuzhiyun 				fhci_err(usb->fhci, "illegal error occurred\n");
347*4882a593Smuzhiyun 		} else if ((extra_data & TD_TOK_IN) &&
348*4882a593Smuzhiyun 				pkt->len > td_length - CRC_SIZE) {
349*4882a593Smuzhiyun 			pkt->status = USB_TD_RX_DATA_UNDERUN;
350*4882a593Smuzhiyun 		}
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 		if (extra_data & TD_TOK_IN)
353*4882a593Smuzhiyun 			pkt->len = td_length - CRC_SIZE;
354*4882a593Smuzhiyun 		else if (pkt->info & PKT_ZLP)
355*4882a593Smuzhiyun 			pkt->len = 0;
356*4882a593Smuzhiyun 		else
357*4882a593Smuzhiyun 			pkt->len = td_length;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 		fhci_transaction_confirm(usb, pkt);
360*4882a593Smuzhiyun 	}
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun /*
364*4882a593Smuzhiyun  * Submitting a data frame to a specified endpoint of a USB device
365*4882a593Smuzhiyun  * The frame is put in the driver's transmit queue for this endpoint
366*4882a593Smuzhiyun  *
367*4882a593Smuzhiyun  * Arguments:
368*4882a593Smuzhiyun  * usb          A pointer to the USB structure
369*4882a593Smuzhiyun  * pkt          A pointer to the user frame structure
370*4882a593Smuzhiyun  * trans_type   Transaction tyep - IN,OUT or SETUP
371*4882a593Smuzhiyun  * dest_addr    Device address - 0~127
372*4882a593Smuzhiyun  * dest_ep      Endpoint number of the device - 0~16
373*4882a593Smuzhiyun  * trans_mode   Pipe type - ISO,Interrupt,bulk or control
374*4882a593Smuzhiyun  * dest_speed   USB speed - Low speed or FULL speed
375*4882a593Smuzhiyun  * data_toggle  Data sequence toggle - 0 or 1
376*4882a593Smuzhiyun  */
fhci_host_transaction(struct fhci_usb * usb,struct packet * pkt,enum fhci_ta_type trans_type,u8 dest_addr,u8 dest_ep,enum fhci_tf_mode trans_mode,enum fhci_speed dest_speed,u8 data_toggle)377*4882a593Smuzhiyun u32 fhci_host_transaction(struct fhci_usb *usb,
378*4882a593Smuzhiyun 			  struct packet *pkt,
379*4882a593Smuzhiyun 			  enum fhci_ta_type trans_type,
380*4882a593Smuzhiyun 			  u8 dest_addr,
381*4882a593Smuzhiyun 			  u8 dest_ep,
382*4882a593Smuzhiyun 			  enum fhci_tf_mode trans_mode,
383*4882a593Smuzhiyun 			  enum fhci_speed dest_speed, u8 data_toggle)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun 	struct endpoint *ep = usb->ep0;
386*4882a593Smuzhiyun 	struct usb_td __iomem *td;
387*4882a593Smuzhiyun 	u16 extra_data;
388*4882a593Smuzhiyun 	u16 td_status;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	fhci_usb_disable_interrupt(usb);
391*4882a593Smuzhiyun 	/* start from the next BD that should be filled */
392*4882a593Smuzhiyun 	td = ep->empty_td;
393*4882a593Smuzhiyun 	td_status = in_be16(&td->status);
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	if (td_status & TD_R && in_be16(&td->length)) {
396*4882a593Smuzhiyun 		/* if the TD is not free */
397*4882a593Smuzhiyun 		fhci_usb_enable_interrupt(usb);
398*4882a593Smuzhiyun 		return -1;
399*4882a593Smuzhiyun 	}
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	/* get the next TD in the ring */
402*4882a593Smuzhiyun 	ep->empty_td = next_bd(ep->td_base, ep->empty_td, td_status);
403*4882a593Smuzhiyun 	fhci_usb_enable_interrupt(usb);
404*4882a593Smuzhiyun 	pkt->priv_data = td;
405*4882a593Smuzhiyun 	out_be32(&td->buf_ptr, virt_to_phys(pkt->data));
406*4882a593Smuzhiyun 	/* sets up transaction parameters - addr,endp,dir,and type */
407*4882a593Smuzhiyun 	extra_data = (dest_ep << TD_ENDP_SHIFT) | dest_addr;
408*4882a593Smuzhiyun 	switch (trans_type) {
409*4882a593Smuzhiyun 	case FHCI_TA_IN:
410*4882a593Smuzhiyun 		extra_data |= TD_TOK_IN;
411*4882a593Smuzhiyun 		break;
412*4882a593Smuzhiyun 	case FHCI_TA_OUT:
413*4882a593Smuzhiyun 		extra_data |= TD_TOK_OUT;
414*4882a593Smuzhiyun 		break;
415*4882a593Smuzhiyun 	case FHCI_TA_SETUP:
416*4882a593Smuzhiyun 		extra_data |= TD_TOK_SETUP;
417*4882a593Smuzhiyun 		break;
418*4882a593Smuzhiyun 	}
419*4882a593Smuzhiyun 	if (trans_mode == FHCI_TF_ISO)
420*4882a593Smuzhiyun 		extra_data |= TD_ISO;
421*4882a593Smuzhiyun 	out_be16(&td->extra, extra_data);
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	/* sets up the buffer descriptor */
424*4882a593Smuzhiyun 	td_status = ((td_status & TD_W) | TD_R | TD_L | TD_I | TD_CNF);
425*4882a593Smuzhiyun 	if (!(pkt->info & PKT_NO_CRC))
426*4882a593Smuzhiyun 		td_status |= TD_TC;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	switch (trans_type) {
429*4882a593Smuzhiyun 	case FHCI_TA_IN:
430*4882a593Smuzhiyun 		if (data_toggle)
431*4882a593Smuzhiyun 			pkt->info |= PKT_PID_DATA1;
432*4882a593Smuzhiyun 		else
433*4882a593Smuzhiyun 			pkt->info |= PKT_PID_DATA0;
434*4882a593Smuzhiyun 		break;
435*4882a593Smuzhiyun 	default:
436*4882a593Smuzhiyun 		if (data_toggle) {
437*4882a593Smuzhiyun 			td_status |= TD_PID_DATA1;
438*4882a593Smuzhiyun 			pkt->info |= PKT_PID_DATA1;
439*4882a593Smuzhiyun 		} else {
440*4882a593Smuzhiyun 			td_status |= TD_PID_DATA0;
441*4882a593Smuzhiyun 			pkt->info |= PKT_PID_DATA0;
442*4882a593Smuzhiyun 		}
443*4882a593Smuzhiyun 		break;
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	if ((dest_speed == FHCI_LOW_SPEED) &&
447*4882a593Smuzhiyun 	    (usb->port_status == FHCI_PORT_FULL))
448*4882a593Smuzhiyun 		td_status |= TD_LSP;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	out_be16(&td->status, td_status);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	/* set up buffer length */
453*4882a593Smuzhiyun 	if (trans_type == FHCI_TA_IN)
454*4882a593Smuzhiyun 		out_be16(&td->length, pkt->len + CRC_SIZE);
455*4882a593Smuzhiyun 	else
456*4882a593Smuzhiyun 		out_be16(&td->length, pkt->len);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	/* put the frame to the confirmation queue */
459*4882a593Smuzhiyun 	cq_put(&ep->conf_frame_Q, pkt);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	if (cq_howmany(&ep->conf_frame_Q) == 1)
462*4882a593Smuzhiyun 		out_8(&usb->fhci->regs->usb_uscom, USB_CMD_STR_FIFO);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	return 0;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun /* Reset the Tx BD ring */
fhci_flush_bds(struct fhci_usb * usb)468*4882a593Smuzhiyun void fhci_flush_bds(struct fhci_usb *usb)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	u16 td_status;
471*4882a593Smuzhiyun 	struct usb_td __iomem *td;
472*4882a593Smuzhiyun 	struct endpoint *ep = usb->ep0;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	td = ep->td_base;
475*4882a593Smuzhiyun 	while (1) {
476*4882a593Smuzhiyun 		td_status = in_be16(&td->status);
477*4882a593Smuzhiyun 		in_be32(&td->buf_ptr);
478*4882a593Smuzhiyun 		in_be16(&td->extra);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 		/* if the TD is not empty - we'll confirm it as Timeout */
481*4882a593Smuzhiyun 		if (td_status & TD_R)
482*4882a593Smuzhiyun 			out_be16(&td->status, (td_status & ~TD_R) | TD_TO);
483*4882a593Smuzhiyun 		/* if this TD is dummy - let's skip this TD */
484*4882a593Smuzhiyun 		else if (in_be32(&td->buf_ptr) == DUMMY_BD_BUFFER)
485*4882a593Smuzhiyun 			out_be32(&td->buf_ptr, DUMMY2_BD_BUFFER);
486*4882a593Smuzhiyun 		/* if this is the last TD - break */
487*4882a593Smuzhiyun 		if (td_status & TD_W)
488*4882a593Smuzhiyun 			break;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 		td++;
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	fhci_td_transaction_confirm(usb);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	td = ep->td_base;
496*4882a593Smuzhiyun 	do {
497*4882a593Smuzhiyun 		out_be16(&td->status, 0);
498*4882a593Smuzhiyun 		out_be16(&td->length, 0);
499*4882a593Smuzhiyun 		out_be32(&td->buf_ptr, 0);
500*4882a593Smuzhiyun 		out_be16(&td->extra, 0);
501*4882a593Smuzhiyun 		td++;
502*4882a593Smuzhiyun 	} while (!(in_be16(&td->status) & TD_W));
503*4882a593Smuzhiyun 	out_be16(&td->status, TD_W); /* for last TD set Wrap bit */
504*4882a593Smuzhiyun 	out_be16(&td->length, 0);
505*4882a593Smuzhiyun 	out_be32(&td->buf_ptr, 0);
506*4882a593Smuzhiyun 	out_be16(&td->extra, 0);
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	out_be16(&ep->ep_pram_ptr->tx_bd_ptr,
509*4882a593Smuzhiyun 		 in_be16(&ep->ep_pram_ptr->tx_base));
510*4882a593Smuzhiyun 	out_be32(&ep->ep_pram_ptr->tx_state, 0);
511*4882a593Smuzhiyun 	out_be16(&ep->ep_pram_ptr->tx_cnt, 0);
512*4882a593Smuzhiyun 	ep->empty_td = ep->td_base;
513*4882a593Smuzhiyun 	ep->conf_td = ep->td_base;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun /*
517*4882a593Smuzhiyun  * Flush all transmitted packets from TDs in the actual frame.
518*4882a593Smuzhiyun  * This routine is called when something wrong with the controller and
519*4882a593Smuzhiyun  * we want to get rid of the actual frame and start again next frame
520*4882a593Smuzhiyun  */
fhci_flush_actual_frame(struct fhci_usb * usb)521*4882a593Smuzhiyun void fhci_flush_actual_frame(struct fhci_usb *usb)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	u8 mode;
524*4882a593Smuzhiyun 	u16 tb_ptr;
525*4882a593Smuzhiyun 	u16 td_status;
526*4882a593Smuzhiyun 	u32 buf_ptr;
527*4882a593Smuzhiyun 	struct usb_td __iomem *td;
528*4882a593Smuzhiyun 	struct endpoint *ep = usb->ep0;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	/* disable the USB controller */
531*4882a593Smuzhiyun 	mode = in_8(&usb->fhci->regs->usb_usmod);
532*4882a593Smuzhiyun 	out_8(&usb->fhci->regs->usb_usmod, mode & ~USB_MODE_EN);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	tb_ptr = in_be16(&ep->ep_pram_ptr->tx_bd_ptr);
535*4882a593Smuzhiyun 	td = cpm_muram_addr(tb_ptr);
536*4882a593Smuzhiyun 	td_status = in_be16(&td->status);
537*4882a593Smuzhiyun 	buf_ptr = in_be32(&td->buf_ptr);
538*4882a593Smuzhiyun 	in_be16(&td->extra);
539*4882a593Smuzhiyun 	do {
540*4882a593Smuzhiyun 		if (td_status & TD_R) {
541*4882a593Smuzhiyun 			out_be16(&td->status, (td_status & ~TD_R) | TD_TO);
542*4882a593Smuzhiyun 		} else {
543*4882a593Smuzhiyun 			out_be32(&td->buf_ptr, 0);
544*4882a593Smuzhiyun 			ep->already_pushed_dummy_bd = false;
545*4882a593Smuzhiyun 			break;
546*4882a593Smuzhiyun 		}
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 		/* advance the TD pointer */
549*4882a593Smuzhiyun 		td = next_bd(ep->td_base, td, td_status);
550*4882a593Smuzhiyun 		td_status = in_be16(&td->status);
551*4882a593Smuzhiyun 		buf_ptr = in_be32(&td->buf_ptr);
552*4882a593Smuzhiyun 		in_be16(&td->extra);
553*4882a593Smuzhiyun 	} while ((td_status & TD_R) || buf_ptr);
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	fhci_td_transaction_confirm(usb);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	out_be16(&ep->ep_pram_ptr->tx_bd_ptr,
558*4882a593Smuzhiyun 		 in_be16(&ep->ep_pram_ptr->tx_base));
559*4882a593Smuzhiyun 	out_be32(&ep->ep_pram_ptr->tx_state, 0);
560*4882a593Smuzhiyun 	out_be16(&ep->ep_pram_ptr->tx_cnt, 0);
561*4882a593Smuzhiyun 	ep->empty_td = ep->td_base;
562*4882a593Smuzhiyun 	ep->conf_td = ep->td_base;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	usb->actual_frame->frame_status = FRAME_TIMER_END_TRANSMISSION;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	/* reset the event register */
567*4882a593Smuzhiyun 	out_be16(&usb->fhci->regs->usb_usber, 0xffff);
568*4882a593Smuzhiyun 	/* enable the USB controller */
569*4882a593Smuzhiyun 	out_8(&usb->fhci->regs->usb_usmod, mode | USB_MODE_EN);
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun /* handles Tx confirm and Tx error interrupt */
fhci_tx_conf_interrupt(struct fhci_usb * usb)573*4882a593Smuzhiyun void fhci_tx_conf_interrupt(struct fhci_usb *usb)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun 	fhci_td_transaction_confirm(usb);
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	/*
578*4882a593Smuzhiyun 	 * Schedule another transaction to this frame only if we have
579*4882a593Smuzhiyun 	 * already confirmed all transaction in the frame.
580*4882a593Smuzhiyun 	 */
581*4882a593Smuzhiyun 	if (((fhci_get_sof_timer_count(usb) < usb->max_frame_usage) ||
582*4882a593Smuzhiyun 	     (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)) &&
583*4882a593Smuzhiyun 	    (list_empty(&usb->actual_frame->tds_list)))
584*4882a593Smuzhiyun 		fhci_schedule_transactions(usb);
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
fhci_host_transmit_actual_frame(struct fhci_usb * usb)587*4882a593Smuzhiyun void fhci_host_transmit_actual_frame(struct fhci_usb *usb)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun 	u16 tb_ptr;
590*4882a593Smuzhiyun 	u16 td_status;
591*4882a593Smuzhiyun 	struct usb_td __iomem *td;
592*4882a593Smuzhiyun 	struct endpoint *ep = usb->ep0;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	tb_ptr = in_be16(&ep->ep_pram_ptr->tx_bd_ptr);
595*4882a593Smuzhiyun 	td = cpm_muram_addr(tb_ptr);
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	if (in_be32(&td->buf_ptr) == DUMMY_BD_BUFFER) {
598*4882a593Smuzhiyun 		struct usb_td __iomem *old_td = td;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 		ep->already_pushed_dummy_bd = false;
601*4882a593Smuzhiyun 		td_status = in_be16(&td->status);
602*4882a593Smuzhiyun 		/* gets the next TD in the ring */
603*4882a593Smuzhiyun 		td = next_bd(ep->td_base, td, td_status);
604*4882a593Smuzhiyun 		tb_ptr = cpm_muram_offset(td);
605*4882a593Smuzhiyun 		out_be16(&ep->ep_pram_ptr->tx_bd_ptr, tb_ptr);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 		/* start transmit only if we have something in the TDs */
608*4882a593Smuzhiyun 		if (in_be16(&td->status) & TD_R)
609*4882a593Smuzhiyun 			out_8(&usb->fhci->regs->usb_uscom, USB_CMD_STR_FIFO);
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 		if (in_be32(&ep->conf_td->buf_ptr) == DUMMY_BD_BUFFER) {
612*4882a593Smuzhiyun 			out_be32(&old_td->buf_ptr, 0);
613*4882a593Smuzhiyun 			ep->conf_td = next_bd(ep->td_base, ep->conf_td,
614*4882a593Smuzhiyun 					      td_status);
615*4882a593Smuzhiyun 		} else {
616*4882a593Smuzhiyun 			out_be32(&old_td->buf_ptr, DUMMY2_BD_BUFFER);
617*4882a593Smuzhiyun 		}
618*4882a593Smuzhiyun 	}
619*4882a593Smuzhiyun }
620