Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * Freescale QUICC Engine USB Host Controller Driver
  3 *
  4 * Copyright (c) Freescale Semicondutor, Inc. 2006.
  5 *               Shlomi Gridish <gridish@freescale.com>
  6 *               Jerry Huang <Chang-Ming.Huang@freescale.com>
  7 * Copyright (c) Logic Product Development, Inc. 2007
  8 *               Peter Barada <peterb@logicpd.com>
  9 * Copyright (c) MontaVista Software, Inc. 2008.
 10 *               Anton Vorontsov <avorontsov@ru.mvista.com>
 11 *
 12 * This program is free software; you can redistribute  it and/or modify it
 13 * under  the terms of  the GNU General  Public License as published by the
 14 * Free Software Foundation;  either version 2 of the  License, or (at your
 15 * option) any later version.
 16 */
 17
 18#include <linux/kernel.h>
 19#include <linux/types.h>
 20#include <linux/spinlock.h>
 21#include <linux/delay.h>
 22#include <linux/errno.h>
 23#include <linux/list.h>
 24#include <linux/interrupt.h>
 25#include <linux/io.h>
 26#include <linux/usb.h>
 27#include <linux/usb/hcd.h>
 28#include <asm/qe.h>
 29#include <asm/fsl_gtm.h>
 30#include "fhci.h"
 31
 32static void recycle_frame(struct fhci_usb *usb, struct packet *pkt)
 33{
 34	pkt->data = NULL;
 35	pkt->len = 0;
 36	pkt->status = USB_TD_OK;
 37	pkt->info = 0;
 38	pkt->priv_data = NULL;
 39
 40	cq_put(&usb->ep0->empty_frame_Q, pkt);
 41}
 42
 43/* confirm submitted packet */
 44void fhci_transaction_confirm(struct fhci_usb *usb, struct packet *pkt)
 45{
 46	struct td *td;
 47	struct packet *td_pkt;
 48	struct ed *ed;
 49	u32 trans_len;
 50	bool td_done = false;
 51
 52	td = fhci_remove_td_from_frame(usb->actual_frame);
 53	td_pkt = td->pkt;
 54	trans_len = pkt->len;
 55	td->status = pkt->status;
 56	if (td->type == FHCI_TA_IN && td_pkt->info & PKT_DUMMY_PACKET) {
 57		if ((td->data + td->actual_len) && trans_len)
 58			memcpy(td->data + td->actual_len, pkt->data,
 59			       trans_len);
 60		cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
 61	}
 62
 63	recycle_frame(usb, pkt);
 64
 65	ed = td->ed;
 66	if (ed->mode == FHCI_TF_ISO) {
 67		if (ed->td_list.next->next != &ed->td_list) {
 68			struct td *td_next =
 69			    list_entry(ed->td_list.next->next, struct td,
 70				       node);
 71
 72			td_next->start_frame = usb->actual_frame->frame_num;
 73		}
 74		td->actual_len = trans_len;
 75		td_done = true;
 76	} else if ((td->status & USB_TD_ERROR) &&
 77			!(td->status & USB_TD_TX_ER_NAK)) {
 78		/*
 79		 * There was an error on the transaction (but not NAK).
 80		 * If it is fatal error (data underrun, stall, bad pid or 3
 81		 * errors exceeded), mark this TD as done.
 82		 */
 83		if ((td->status & USB_TD_RX_DATA_UNDERUN) ||
 84				(td->status & USB_TD_TX_ER_STALL) ||
 85				(td->status & USB_TD_RX_ER_PID) ||
 86				(++td->error_cnt >= 3)) {
 87			ed->state = FHCI_ED_HALTED;
 88			td_done = true;
 89
 90			if (td->status & USB_TD_RX_DATA_UNDERUN) {
 91				fhci_dbg(usb->fhci, "td err fu\n");
 92				td->toggle = !td->toggle;
 93				td->actual_len += trans_len;
 94			} else {
 95				fhci_dbg(usb->fhci, "td err f!u\n");
 96			}
 97		} else {
 98			fhci_dbg(usb->fhci, "td err !f\n");
 99			/* it is not a fatal error -retry this transaction */
100			td->nak_cnt = 0;
101			td->error_cnt++;
102			td->status = USB_TD_OK;
103		}
104	} else if (td->status & USB_TD_TX_ER_NAK) {
105		/* there was a NAK response */
106		fhci_vdbg(usb->fhci, "td nack\n");
107		td->nak_cnt++;
108		td->error_cnt = 0;
109		td->status = USB_TD_OK;
110	} else {
111		/* there was no error on transaction */
112		td->error_cnt = 0;
113		td->nak_cnt = 0;
114		td->toggle = !td->toggle;
115		td->actual_len += trans_len;
116
117		if (td->len == td->actual_len)
118			td_done = true;
119	}
120
121	if (td_done)
122		fhci_move_td_from_ed_to_done_list(usb, ed);
123}
124
125/*
126 * Flush all transmitted packets from BDs
127 * This routine is called when disabling the USB port to flush all
128 * transmissions that are already scheduled in the BDs
129 */
130void fhci_flush_all_transmissions(struct fhci_usb *usb)
131{
132	u8 mode;
133	struct td *td;
134
135	mode = in_8(&usb->fhci->regs->usb_mod);
136	clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_EN);
137
138	fhci_flush_bds(usb);
139
140	while ((td = fhci_peek_td_from_frame(usb->actual_frame)) != NULL) {
141		struct packet *pkt = td->pkt;
142
143		pkt->status = USB_TD_TX_ER_TIMEOUT;
144		fhci_transaction_confirm(usb, pkt);
145	}
146
147	usb->actual_frame->frame_status = FRAME_END_TRANSMISSION;
148
149	/* reset the event register */
150	out_be16(&usb->fhci->regs->usb_event, 0xffff);
151	/* enable the USB controller */
152	out_8(&usb->fhci->regs->usb_mod, mode | USB_MODE_EN);
153}
154
155/*
156 * This function forms the packet and transmit the packet. This function
157 * will handle all endpoint type:ISO,interrupt,control and bulk
158 */
159static int add_packet(struct fhci_usb *usb, struct ed *ed, struct td *td)
160{
161	u32 fw_transaction_time, len = 0;
162	struct packet *pkt;
163	u8 *data = NULL;
164
165	/* calcalate data address,len and toggle and then add the transaction */
166	if (td->toggle == USB_TD_TOGGLE_CARRY)
167		td->toggle = ed->toggle_carry;
168
169	switch (ed->mode) {
170	case FHCI_TF_ISO:
171		len = td->len;
172		if (td->type != FHCI_TA_IN)
173			data = td->data;
174		break;
175	case FHCI_TF_CTRL:
176	case FHCI_TF_BULK:
177		len = min(td->len - td->actual_len, ed->max_pkt_size);
178		if (!((td->type == FHCI_TA_IN) &&
179		      ((len + td->actual_len) == td->len)))
180			data = td->data + td->actual_len;
181		break;
182	case FHCI_TF_INTR:
183		len = min(td->len, ed->max_pkt_size);
184		if (!((td->type == FHCI_TA_IN) &&
185		      ((td->len + CRC_SIZE) >= ed->max_pkt_size)))
186			data = td->data;
187		break;
188	default:
189		break;
190	}
191
192	if (usb->port_status == FHCI_PORT_FULL)
193		fw_transaction_time = (((len + PROTOCOL_OVERHEAD) * 11) >> 4);
194	else
195		fw_transaction_time = ((len + PROTOCOL_OVERHEAD) * 6);
196
197	/* check if there's enough space in this frame to submit this TD */
198	if (usb->actual_frame->total_bytes + len + PROTOCOL_OVERHEAD >=
199			usb->max_bytes_per_frame) {
200		fhci_vdbg(usb->fhci, "not enough space in this frame: "
201			  "%d %d %d\n", usb->actual_frame->total_bytes, len,
202			  usb->max_bytes_per_frame);
203		return -1;
204	}
205
206	/* check if there's enough time in this frame to submit this TD */
207	if (usb->actual_frame->frame_status != FRAME_IS_PREPARED &&
208	    (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION ||
209	     (fw_transaction_time + usb->sw_transaction_time >=
210	      1000 - fhci_get_sof_timer_count(usb)))) {
211		fhci_dbg(usb->fhci, "not enough time in this frame\n");
212		return -1;
213	}
214
215	/* update frame object fields before transmitting */
216	pkt = cq_get(&usb->ep0->empty_frame_Q);
217	if (!pkt) {
218		fhci_dbg(usb->fhci, "there is no empty frame\n");
219		return -1;
220	}
221	td->pkt = pkt;
222
223	pkt->info = 0;
224	if (data == NULL) {
225		data = cq_get(&usb->ep0->dummy_packets_Q);
226		BUG_ON(!data);
227		pkt->info = PKT_DUMMY_PACKET;
228	}
229	pkt->data = data;
230	pkt->len = len;
231	pkt->status = USB_TD_OK;
232	/* update TD status field before transmitting */
233	td->status = USB_TD_INPROGRESS;
234	/* update actual frame time object with the actual transmission */
235	usb->actual_frame->total_bytes += (len + PROTOCOL_OVERHEAD);
236	fhci_add_td_to_frame(usb->actual_frame, td);
237
238	if (usb->port_status != FHCI_PORT_FULL &&
239			usb->port_status != FHCI_PORT_LOW) {
240		pkt->status = USB_TD_TX_ER_TIMEOUT;
241		pkt->len = 0;
242		fhci_transaction_confirm(usb, pkt);
243	} else if (fhci_host_transaction(usb, pkt, td->type, ed->dev_addr,
244			ed->ep_addr, ed->mode, ed->speed, td->toggle)) {
245		/* remove TD from actual frame */
246		list_del_init(&td->frame_lh);
247		td->status = USB_TD_OK;
248		if (pkt->info & PKT_DUMMY_PACKET)
249			cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
250		recycle_frame(usb, pkt);
251		usb->actual_frame->total_bytes -= (len + PROTOCOL_OVERHEAD);
252		fhci_err(usb->fhci, "host transaction failed\n");
253		return -1;
254	}
255
256	return len;
257}
258
259static void move_head_to_tail(struct list_head *list)
260{
261	struct list_head *node = list->next;
262
263	if (!list_empty(list)) {
264		list_del(node);
265		list_add_tail(node, list);
266	}
267}
268
269/*
270 * This function goes through the endpoint list and schedules the
271 * transactions within this list
272 */
273static int scan_ed_list(struct fhci_usb *usb,
274			struct list_head *list, enum fhci_tf_mode list_type)
275{
276	static const int frame_part[4] = {
277		[FHCI_TF_CTRL] = MAX_BYTES_PER_FRAME,
278		[FHCI_TF_ISO] = (MAX_BYTES_PER_FRAME *
279				 MAX_PERIODIC_FRAME_USAGE) / 100,
280		[FHCI_TF_BULK] = MAX_BYTES_PER_FRAME,
281		[FHCI_TF_INTR] = (MAX_BYTES_PER_FRAME *
282				  MAX_PERIODIC_FRAME_USAGE) / 100
283	};
284	struct ed *ed;
285	struct td *td;
286	int ans = 1;
287	u32 save_transaction_time = usb->sw_transaction_time;
288
289	list_for_each_entry(ed, list, node) {
290		td = ed->td_head;
291
292		if (!td || (td && td->status == USB_TD_INPROGRESS))
293			continue;
294
295		if (ed->state != FHCI_ED_OPER) {
296			if (ed->state == FHCI_ED_URB_DEL) {
297				td->status = USB_TD_OK;
298				fhci_move_td_from_ed_to_done_list(usb, ed);
299				ed->state = FHCI_ED_SKIP;
300			}
301			continue;
302		}
303
304		/*
305		 * if it isn't interrupt pipe or it is not iso pipe and the
306		 * interval time passed
307		 */
308		if ((list_type == FHCI_TF_INTR || list_type == FHCI_TF_ISO) &&
309				(((usb->actual_frame->frame_num -
310				   td->start_frame) & 0x7ff) < td->interval))
311			continue;
312
313		if (add_packet(usb, ed, td) < 0)
314			continue;
315
316		/* update time stamps in the TD */
317		td->start_frame = usb->actual_frame->frame_num;
318		usb->sw_transaction_time += save_transaction_time;
319
320		if (usb->actual_frame->total_bytes >=
321					usb->max_bytes_per_frame) {
322			usb->actual_frame->frame_status =
323				FRAME_DATA_END_TRANSMISSION;
324			fhci_push_dummy_bd(usb->ep0);
325			ans = 0;
326			break;
327		}
328
329		if (usb->actual_frame->total_bytes >= frame_part[list_type])
330			break;
331	}
332
333	/* be fair to each ED(move list head around) */
334	move_head_to_tail(list);
335	usb->sw_transaction_time = save_transaction_time;
336
337	return ans;
338}
339
340static u32 rotate_frames(struct fhci_usb *usb)
341{
342	struct fhci_hcd *fhci = usb->fhci;
343
344	if (!list_empty(&usb->actual_frame->tds_list)) {
345		if ((((in_be16(&fhci->pram->frame_num) & 0x07ff) -
346		      usb->actual_frame->frame_num) & 0x7ff) > 5)
347			fhci_flush_actual_frame(usb);
348		else
349			return -EINVAL;
350	}
351
352	usb->actual_frame->frame_status = FRAME_IS_PREPARED;
353	usb->actual_frame->frame_num = in_be16(&fhci->pram->frame_num) & 0x7ff;
354	usb->actual_frame->total_bytes = 0;
355
356	return 0;
357}
358
359/*
360 * This function schedule the USB transaction and will process the
361 * endpoint in the following order: iso, interrupt, control and bulk.
362 */
363void fhci_schedule_transactions(struct fhci_usb *usb)
364{
365	int left = 1;
366
367	if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
368		if (rotate_frames(usb) != 0)
369			return;
370
371	if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
372		return;
373
374	if (usb->actual_frame->total_bytes == 0) {
375		/*
376		 * schedule the next available ISO transfer
377		 *or next stage of the ISO transfer
378		 */
379		scan_ed_list(usb, &usb->hc_list->iso_list, FHCI_TF_ISO);
380
381		/*
382		 * schedule the next available interrupt transfer or
383		 * the next stage of the interrupt transfer
384		 */
385		scan_ed_list(usb, &usb->hc_list->intr_list, FHCI_TF_INTR);
386
387		/*
388		 * schedule the next available control transfer
389		 * or the next stage of the control transfer
390		 */
391		left = scan_ed_list(usb, &usb->hc_list->ctrl_list,
392				    FHCI_TF_CTRL);
393	}
394
395	/*
396	 * schedule the next available bulk transfer or the next stage of the
397	 * bulk transfer
398	 */
399	if (left > 0)
400		scan_ed_list(usb, &usb->hc_list->bulk_list, FHCI_TF_BULK);
401}
402
403/* Handles SOF interrupt */
404static void sof_interrupt(struct fhci_hcd *fhci)
405{
406	struct fhci_usb *usb = fhci->usb_lld;
407
408	if ((usb->port_status == FHCI_PORT_DISABLED) &&
409	    (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_CONNECTION) &&
410	    !(usb->vroot_hub->port.wPortChange & USB_PORT_STAT_C_CONNECTION)) {
411		if (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_LOW_SPEED)
412			usb->port_status = FHCI_PORT_LOW;
413		else
414			usb->port_status = FHCI_PORT_FULL;
415		/* Disable IDLE */
416		usb->saved_msk &= ~USB_E_IDLE_MASK;
417		out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
418	}
419
420	gtm_set_exact_timer16(fhci->timer, usb->max_frame_usage, false);
421
422	fhci_host_transmit_actual_frame(usb);
423	usb->actual_frame->frame_status = FRAME_IS_TRANSMITTED;
424
425	fhci_schedule_transactions(usb);
426}
427
428/* Handles device disconnected interrupt on port */
429void fhci_device_disconnected_interrupt(struct fhci_hcd *fhci)
430{
431	struct fhci_usb *usb = fhci->usb_lld;
432
433	fhci_dbg(fhci, "-> %s\n", __func__);
434
435	fhci_usb_disable_interrupt(usb);
436	clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_LSS);
437	usb->port_status = FHCI_PORT_DISABLED;
438
439	fhci_stop_sof_timer(fhci);
440
441	/* Enable IDLE since we want to know if something comes along */
442	usb->saved_msk |= USB_E_IDLE_MASK;
443	out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
444
445	usb->vroot_hub->port.wPortStatus &= ~USB_PORT_STAT_CONNECTION;
446	usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_CONNECTION;
447	usb->max_bytes_per_frame = 0;
448	fhci_usb_enable_interrupt(usb);
449
450	fhci_dbg(fhci, "<- %s\n", __func__);
451}
452
453/* detect a new device connected on the USB port */
454void fhci_device_connected_interrupt(struct fhci_hcd *fhci)
455{
456
457	struct fhci_usb *usb = fhci->usb_lld;
458	int state;
459	int ret;
460
461	fhci_dbg(fhci, "-> %s\n", __func__);
462
463	fhci_usb_disable_interrupt(usb);
464	state = fhci_ioports_check_bus_state(fhci);
465
466	/* low-speed device was connected to the USB port */
467	if (state == 1) {
468		ret = qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
469		if (ret) {
470			fhci_warn(fhci, "Low-Speed device is not supported, "
471				  "try use BRGx\n");
472			goto out;
473		}
474
475		usb->port_status = FHCI_PORT_LOW;
476		setbits8(&usb->fhci->regs->usb_mod, USB_MODE_LSS);
477		usb->vroot_hub->port.wPortStatus |=
478		    (USB_PORT_STAT_LOW_SPEED |
479		     USB_PORT_STAT_CONNECTION);
480		usb->vroot_hub->port.wPortChange |=
481		    USB_PORT_STAT_C_CONNECTION;
482		usb->max_bytes_per_frame =
483		    (MAX_BYTES_PER_FRAME >> 3) - 7;
484		fhci_port_enable(usb);
485	} else if (state == 2) {
486		ret = qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
487		if (ret) {
488			fhci_warn(fhci, "Full-Speed device is not supported, "
489				  "try use CLKx\n");
490			goto out;
491		}
492
493		usb->port_status = FHCI_PORT_FULL;
494		clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_LSS);
495		usb->vroot_hub->port.wPortStatus &=
496		    ~USB_PORT_STAT_LOW_SPEED;
497		usb->vroot_hub->port.wPortStatus |=
498		    USB_PORT_STAT_CONNECTION;
499		usb->vroot_hub->port.wPortChange |=
500		    USB_PORT_STAT_C_CONNECTION;
501		usb->max_bytes_per_frame = (MAX_BYTES_PER_FRAME - 15);
502		fhci_port_enable(usb);
503	}
504out:
505	fhci_usb_enable_interrupt(usb);
506	fhci_dbg(fhci, "<- %s\n", __func__);
507}
508
509irqreturn_t fhci_frame_limit_timer_irq(int irq, void *_hcd)
510{
511	struct usb_hcd *hcd = _hcd;
512	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
513	struct fhci_usb *usb = fhci->usb_lld;
514
515	spin_lock(&fhci->lock);
516
517	gtm_set_exact_timer16(fhci->timer, 1000, false);
518
519	if (usb->actual_frame->frame_status == FRAME_IS_TRANSMITTED) {
520		usb->actual_frame->frame_status = FRAME_TIMER_END_TRANSMISSION;
521		fhci_push_dummy_bd(usb->ep0);
522	}
523
524	fhci_schedule_transactions(usb);
525
526	spin_unlock(&fhci->lock);
527
528	return IRQ_HANDLED;
529}
530
531/* Cancel transmission on the USB endpoint */
532static void abort_transmission(struct fhci_usb *usb)
533{
534	fhci_dbg(usb->fhci, "-> %s\n", __func__);
535	/* issue stop Tx command */
536	qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
537	/* flush Tx FIFOs */
538	out_8(&usb->fhci->regs->usb_comm, USB_CMD_FLUSH_FIFO | EP_ZERO);
539	udelay(1000);
540	/* reset Tx BDs */
541	fhci_flush_bds(usb);
542	/* issue restart Tx command */
543	qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
544	fhci_dbg(usb->fhci, "<- %s\n", __func__);
545}
546
547irqreturn_t fhci_irq(struct usb_hcd *hcd)
548{
549	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
550	struct fhci_usb *usb;
551	u16 usb_er = 0;
552	unsigned long flags;
553
554	spin_lock_irqsave(&fhci->lock, flags);
555
556	usb = fhci->usb_lld;
557
558	usb_er |= in_be16(&usb->fhci->regs->usb_event) &
559		  in_be16(&usb->fhci->regs->usb_mask);
560
561	/* clear event bits for next time */
562	out_be16(&usb->fhci->regs->usb_event, usb_er);
563
564	fhci_dbg_isr(fhci, usb_er);
565
566	if (usb_er & USB_E_RESET_MASK) {
567		if ((usb->port_status == FHCI_PORT_FULL) ||
568				(usb->port_status == FHCI_PORT_LOW)) {
569			fhci_device_disconnected_interrupt(fhci);
570			usb_er &= ~USB_E_IDLE_MASK;
571		} else if (usb->port_status == FHCI_PORT_WAITING) {
572			usb->port_status = FHCI_PORT_DISCONNECTING;
573
574			/* Turn on IDLE since we want to disconnect */
575			usb->saved_msk |= USB_E_IDLE_MASK;
576			out_be16(&usb->fhci->regs->usb_event,
577				 usb->saved_msk);
578		} else if (usb->port_status == FHCI_PORT_DISABLED) {
579			if (fhci_ioports_check_bus_state(fhci) == 1)
580				fhci_device_connected_interrupt(fhci);
581		}
582		usb_er &= ~USB_E_RESET_MASK;
583	}
584
585	if (usb_er & USB_E_MSF_MASK) {
586		abort_transmission(fhci->usb_lld);
587		usb_er &= ~USB_E_MSF_MASK;
588	}
589
590	if (usb_er & (USB_E_SOF_MASK | USB_E_SFT_MASK)) {
591		sof_interrupt(fhci);
592		usb_er &= ~(USB_E_SOF_MASK | USB_E_SFT_MASK);
593	}
594
595	if (usb_er & USB_E_TXB_MASK) {
596		fhci_tx_conf_interrupt(fhci->usb_lld);
597		usb_er &= ~USB_E_TXB_MASK;
598	}
599
600	if (usb_er & USB_E_TXE1_MASK) {
601		fhci_tx_conf_interrupt(fhci->usb_lld);
602		usb_er &= ~USB_E_TXE1_MASK;
603	}
604
605	if (usb_er & USB_E_IDLE_MASK) {
606		if (usb->port_status == FHCI_PORT_DISABLED) {
607			usb_er &= ~USB_E_RESET_MASK;
608			fhci_device_connected_interrupt(fhci);
609		} else if (usb->port_status ==
610				FHCI_PORT_DISCONNECTING) {
611			/* XXX usb->port_status = FHCI_PORT_WAITING; */
612			/* Disable IDLE */
613			usb->saved_msk &= ~USB_E_IDLE_MASK;
614			out_be16(&usb->fhci->regs->usb_mask,
615				 usb->saved_msk);
616		} else {
617			fhci_dbg_isr(fhci, -1);
618		}
619
620		usb_er &= ~USB_E_IDLE_MASK;
621	}
622
623	spin_unlock_irqrestore(&fhci->lock, flags);
624
625	return IRQ_HANDLED;
626}
627
628
629/*
630 * Process normal completions(error or success) and clean the schedule.
631 *
632 * This is the main path for handing urbs back to drivers. The only other patth
633 * is process_del_list(),which unlinks URBs by scanning EDs,instead of scanning
634 * the (re-reversed) done list as this does.
635 */
636static void process_done_list(unsigned long data)
637{
638	struct urb *urb;
639	struct ed *ed;
640	struct td *td;
641	struct urb_priv *urb_priv;
642	struct fhci_hcd *fhci = (struct fhci_hcd *)data;
643
644	disable_irq(fhci->timer->irq);
645	disable_irq(fhci_to_hcd(fhci)->irq);
646	spin_lock(&fhci->lock);
647
648	td = fhci_remove_td_from_done_list(fhci->hc_list);
649	while (td != NULL) {
650		urb = td->urb;
651		urb_priv = urb->hcpriv;
652		ed = td->ed;
653
654		/* update URB's length and status from TD */
655		fhci_done_td(urb, td);
656		urb_priv->tds_cnt++;
657
658		/*
659		 * if all this urb's TDs are done, call complete()
660		 * Interrupt transfers are the onley special case:
661		 * they are reissued,until "deleted" by usb_unlink_urb
662		 * (real work done in a SOF intr, by process_del_list)
663		 */
664		if (urb_priv->tds_cnt == urb_priv->num_of_tds) {
665			fhci_urb_complete_free(fhci, urb);
666		} else if (urb_priv->state == URB_DEL &&
667				ed->state == FHCI_ED_SKIP) {
668			fhci_del_ed_list(fhci, ed);
669			ed->state = FHCI_ED_OPER;
670		} else if (ed->state == FHCI_ED_HALTED) {
671			urb_priv->state = URB_DEL;
672			ed->state = FHCI_ED_URB_DEL;
673			fhci_del_ed_list(fhci, ed);
674			ed->state = FHCI_ED_OPER;
675		}
676
677		td = fhci_remove_td_from_done_list(fhci->hc_list);
678	}
679
680	spin_unlock(&fhci->lock);
681	enable_irq(fhci->timer->irq);
682	enable_irq(fhci_to_hcd(fhci)->irq);
683}
684
685DECLARE_TASKLET(fhci_tasklet, process_done_list, 0);
686
687/* transfer complted callback */
688u32 fhci_transfer_confirm_callback(struct fhci_hcd *fhci)
689{
690	if (!fhci->process_done_task->state)
691		tasklet_schedule(fhci->process_done_task);
692	return 0;
693}
694
695/*
696 * adds urb to the endpoint descriptor list
697 * arguments:
698 * fhci		data structure for the Low level host controller
699 * ep		USB Host endpoint data structure
700 * urb		USB request block data structure
701 */
702void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
703{
704	struct ed *ed = urb->ep->hcpriv;
705	struct urb_priv *urb_priv = urb->hcpriv;
706	u32 data_len = urb->transfer_buffer_length;
707	int urb_state = 0;
708	int toggle = 0;
709	struct td *td;
710	u8 *data;
711	u16 cnt = 0;
712
713	if (ed == NULL) {
714		ed = fhci_get_empty_ed(fhci);
715		ed->dev_addr = usb_pipedevice(urb->pipe);
716		ed->ep_addr = usb_pipeendpoint(urb->pipe);
717		switch (usb_pipetype(urb->pipe)) {
718		case PIPE_CONTROL:
719			ed->mode = FHCI_TF_CTRL;
720			break;
721		case PIPE_BULK:
722			ed->mode = FHCI_TF_BULK;
723			break;
724		case PIPE_INTERRUPT:
725			ed->mode = FHCI_TF_INTR;
726			break;
727		case PIPE_ISOCHRONOUS:
728			ed->mode = FHCI_TF_ISO;
729			break;
730		default:
731			break;
732		}
733		ed->speed = (urb->dev->speed == USB_SPEED_LOW) ?
734			FHCI_LOW_SPEED : FHCI_FULL_SPEED;
735		ed->max_pkt_size = usb_maxpacket(urb->dev,
736			urb->pipe, usb_pipeout(urb->pipe));
737		urb->ep->hcpriv = ed;
738		fhci_dbg(fhci, "new ep speed=%d max_pkt_size=%d\n",
739			 ed->speed, ed->max_pkt_size);
740	}
741
742	/* for ISO transfer calculate start frame index */
743	if (ed->mode == FHCI_TF_ISO && urb->transfer_flags & URB_ISO_ASAP)
744		urb->start_frame = ed->td_head ? ed->last_iso + 1 :
 
745						 get_frame_num(fhci);
 
 
 
746
747	/*
748	 * OHCI handles the DATA toggle itself,we just use the USB
749	 * toggle bits
750	 */
751	if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
752			  usb_pipeout(urb->pipe)))
753		toggle = USB_TD_TOGGLE_CARRY;
754	else {
755		toggle = USB_TD_TOGGLE_DATA0;
756		usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
757			      usb_pipeout(urb->pipe), 1);
758	}
759
760	urb_priv->tds_cnt = 0;
761	urb_priv->ed = ed;
762	if (data_len > 0)
763		data = urb->transfer_buffer;
764	else
765		data = NULL;
766
767	switch (ed->mode) {
768	case FHCI_TF_BULK:
769		if (urb->transfer_flags & URB_ZERO_PACKET &&
770				urb->transfer_buffer_length > 0 &&
771				((urb->transfer_buffer_length %
772				usb_maxpacket(urb->dev, urb->pipe,
773				usb_pipeout(urb->pipe))) == 0))
774			urb_state = US_BULK0;
775		while (data_len > 4096) {
776			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
777				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
778							 FHCI_TA_IN,
779				cnt ? USB_TD_TOGGLE_CARRY :
780				      toggle,
781				data, 4096, 0, 0, true);
782			data += 4096;
783			data_len -= 4096;
784			cnt++;
785		}
786
787		td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
788			usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
789			cnt ? USB_TD_TOGGLE_CARRY : toggle,
790			data, data_len, 0, 0, true);
791		cnt++;
792
793		if (urb->transfer_flags & URB_ZERO_PACKET &&
794				cnt < urb_priv->num_of_tds) {
795			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
796				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
797							 FHCI_TA_IN,
798				USB_TD_TOGGLE_CARRY, NULL, 0, 0, 0, true);
799			cnt++;
800		}
801		break;
802	case FHCI_TF_INTR:
803		urb->start_frame = get_frame_num(fhci) + 1;
804		td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
805			usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
806			USB_TD_TOGGLE_DATA0, data, data_len,
807			urb->interval, urb->start_frame, true);
808		break;
809	case FHCI_TF_CTRL:
810		ed->dev_addr = usb_pipedevice(urb->pipe);
811		ed->max_pkt_size = usb_maxpacket(urb->dev, urb->pipe,
812			usb_pipeout(urb->pipe));
813		td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, FHCI_TA_SETUP,
 
814			USB_TD_TOGGLE_DATA0, urb->setup_packet, 8, 0, 0, true);
815
 
816		if (data_len > 0) {
817			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
818				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
819							 FHCI_TA_IN,
820				USB_TD_TOGGLE_DATA1, data, data_len, 0, 0,
821				true);
822		}
823		td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
824			usb_pipeout(urb->pipe) ? FHCI_TA_IN : FHCI_TA_OUT,
825			USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
 
 
 
 
 
 
 
 
 
826		urb_state = US_CTRL_SETUP;
827		break;
828	case FHCI_TF_ISO:
829		for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
830			u16 frame = urb->start_frame;
831
832			/*
833			 * FIXME scheduling should handle frame counter
834			 * roll-around ... exotic case (and OHCI has
835			 * a 2^16 iso range, vs other HCs max of 2^10)
836			 */
837			frame += cnt * urb->interval;
838			frame &= 0x07ff;
839			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
840				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
841							 FHCI_TA_IN,
842				USB_TD_TOGGLE_DATA0,
843				data + urb->iso_frame_desc[cnt].offset,
844				urb->iso_frame_desc[cnt].length,
845				urb->interval, frame, true);
846		}
847		break;
848	default:
849		break;
850	}
851
852	/*
853	 * set the state of URB
854	 * control pipe:3 states -- setup,data,status
855	 * interrupt and bulk pipe:1 state -- data
856	 */
857	urb->pipe &= ~0x1f;
858	urb->pipe |= urb_state & 0x1f;
859
860	urb_priv->state = URB_INPROGRESS;
861
862	if (!ed->td_head) {
863		ed->state = FHCI_ED_OPER;
864		switch (ed->mode) {
865		case FHCI_TF_CTRL:
866			list_add(&ed->node, &fhci->hc_list->ctrl_list);
867			break;
868		case FHCI_TF_BULK:
869			list_add(&ed->node, &fhci->hc_list->bulk_list);
870			break;
871		case FHCI_TF_INTR:
872			list_add(&ed->node, &fhci->hc_list->intr_list);
873			break;
874		case FHCI_TF_ISO:
875			list_add(&ed->node, &fhci->hc_list->iso_list);
876			break;
877		default:
878			break;
879		}
880	}
881
882	fhci_add_tds_to_ed(ed, urb_priv->tds, urb_priv->num_of_tds);
883	fhci->active_urbs++;
884}
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Freescale QUICC Engine USB Host Controller Driver
  4 *
  5 * Copyright (c) Freescale Semicondutor, Inc. 2006, 2011.
  6 *               Shlomi Gridish <gridish@freescale.com>
  7 *               Jerry Huang <Chang-Ming.Huang@freescale.com>
  8 * Copyright (c) Logic Product Development, Inc. 2007
  9 *               Peter Barada <peterb@logicpd.com>
 10 * Copyright (c) MontaVista Software, Inc. 2008.
 11 *               Anton Vorontsov <avorontsov@ru.mvista.com>
 
 
 
 
 
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/types.h>
 16#include <linux/spinlock.h>
 17#include <linux/delay.h>
 18#include <linux/errno.h>
 19#include <linux/list.h>
 20#include <linux/interrupt.h>
 21#include <linux/io.h>
 22#include <linux/usb.h>
 23#include <linux/usb/hcd.h>
 24#include <soc/fsl/qe/qe.h>
 25#include <asm/fsl_gtm.h>
 26#include "fhci.h"
 27
 28static void recycle_frame(struct fhci_usb *usb, struct packet *pkt)
 29{
 30	pkt->data = NULL;
 31	pkt->len = 0;
 32	pkt->status = USB_TD_OK;
 33	pkt->info = 0;
 34	pkt->priv_data = NULL;
 35
 36	cq_put(&usb->ep0->empty_frame_Q, pkt);
 37}
 38
 39/* confirm submitted packet */
 40void fhci_transaction_confirm(struct fhci_usb *usb, struct packet *pkt)
 41{
 42	struct td *td;
 43	struct packet *td_pkt;
 44	struct ed *ed;
 45	u32 trans_len;
 46	bool td_done = false;
 47
 48	td = fhci_remove_td_from_frame(usb->actual_frame);
 49	td_pkt = td->pkt;
 50	trans_len = pkt->len;
 51	td->status = pkt->status;
 52	if (td->type == FHCI_TA_IN && td_pkt->info & PKT_DUMMY_PACKET) {
 53		if ((td->data + td->actual_len) && trans_len)
 54			memcpy(td->data + td->actual_len, pkt->data,
 55			       trans_len);
 56		cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
 57	}
 58
 59	recycle_frame(usb, pkt);
 60
 61	ed = td->ed;
 62	if (ed->mode == FHCI_TF_ISO) {
 63		if (ed->td_list.next->next != &ed->td_list) {
 64			struct td *td_next =
 65			    list_entry(ed->td_list.next->next, struct td,
 66				       node);
 67
 68			td_next->start_frame = usb->actual_frame->frame_num;
 69		}
 70		td->actual_len = trans_len;
 71		td_done = true;
 72	} else if ((td->status & USB_TD_ERROR) &&
 73			!(td->status & USB_TD_TX_ER_NAK)) {
 74		/*
 75		 * There was an error on the transaction (but not NAK).
 76		 * If it is fatal error (data underrun, stall, bad pid or 3
 77		 * errors exceeded), mark this TD as done.
 78		 */
 79		if ((td->status & USB_TD_RX_DATA_UNDERUN) ||
 80				(td->status & USB_TD_TX_ER_STALL) ||
 81				(td->status & USB_TD_RX_ER_PID) ||
 82				(++td->error_cnt >= 3)) {
 83			ed->state = FHCI_ED_HALTED;
 84			td_done = true;
 85
 86			if (td->status & USB_TD_RX_DATA_UNDERUN) {
 87				fhci_dbg(usb->fhci, "td err fu\n");
 88				td->toggle = !td->toggle;
 89				td->actual_len += trans_len;
 90			} else {
 91				fhci_dbg(usb->fhci, "td err f!u\n");
 92			}
 93		} else {
 94			fhci_dbg(usb->fhci, "td err !f\n");
 95			/* it is not a fatal error -retry this transaction */
 96			td->nak_cnt = 0;
 97			td->error_cnt++;
 98			td->status = USB_TD_OK;
 99		}
100	} else if (td->status & USB_TD_TX_ER_NAK) {
101		/* there was a NAK response */
102		fhci_vdbg(usb->fhci, "td nack\n");
103		td->nak_cnt++;
104		td->error_cnt = 0;
105		td->status = USB_TD_OK;
106	} else {
107		/* there was no error on transaction */
108		td->error_cnt = 0;
109		td->nak_cnt = 0;
110		td->toggle = !td->toggle;
111		td->actual_len += trans_len;
112
113		if (td->len == td->actual_len)
114			td_done = true;
115	}
116
117	if (td_done)
118		fhci_move_td_from_ed_to_done_list(usb, ed);
119}
120
121/*
122 * Flush all transmitted packets from BDs
123 * This routine is called when disabling the USB port to flush all
124 * transmissions that are already scheduled in the BDs
125 */
126void fhci_flush_all_transmissions(struct fhci_usb *usb)
127{
128	u8 mode;
129	struct td *td;
130
131	mode = in_8(&usb->fhci->regs->usb_usmod);
132	clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
133
134	fhci_flush_bds(usb);
135
136	while ((td = fhci_peek_td_from_frame(usb->actual_frame)) != NULL) {
137		struct packet *pkt = td->pkt;
138
139		pkt->status = USB_TD_TX_ER_TIMEOUT;
140		fhci_transaction_confirm(usb, pkt);
141	}
142
143	usb->actual_frame->frame_status = FRAME_END_TRANSMISSION;
144
145	/* reset the event register */
146	out_be16(&usb->fhci->regs->usb_usber, 0xffff);
147	/* enable the USB controller */
148	out_8(&usb->fhci->regs->usb_usmod, mode | USB_MODE_EN);
149}
150
151/*
152 * This function forms the packet and transmit the packet. This function
153 * will handle all endpoint type:ISO,interrupt,control and bulk
154 */
155static int add_packet(struct fhci_usb *usb, struct ed *ed, struct td *td)
156{
157	u32 fw_transaction_time, len = 0;
158	struct packet *pkt;
159	u8 *data = NULL;
160
161	/* calcalate data address,len and toggle and then add the transaction */
162	if (td->toggle == USB_TD_TOGGLE_CARRY)
163		td->toggle = ed->toggle_carry;
164
165	switch (ed->mode) {
166	case FHCI_TF_ISO:
167		len = td->len;
168		if (td->type != FHCI_TA_IN)
169			data = td->data;
170		break;
171	case FHCI_TF_CTRL:
172	case FHCI_TF_BULK:
173		len = min(td->len - td->actual_len, ed->max_pkt_size);
174		if (!((td->type == FHCI_TA_IN) &&
175		      ((len + td->actual_len) == td->len)))
176			data = td->data + td->actual_len;
177		break;
178	case FHCI_TF_INTR:
179		len = min(td->len, ed->max_pkt_size);
180		if (!((td->type == FHCI_TA_IN) &&
181		      ((td->len + CRC_SIZE) >= ed->max_pkt_size)))
182			data = td->data;
183		break;
184	default:
185		break;
186	}
187
188	if (usb->port_status == FHCI_PORT_FULL)
189		fw_transaction_time = (((len + PROTOCOL_OVERHEAD) * 11) >> 4);
190	else
191		fw_transaction_time = ((len + PROTOCOL_OVERHEAD) * 6);
192
193	/* check if there's enough space in this frame to submit this TD */
194	if (usb->actual_frame->total_bytes + len + PROTOCOL_OVERHEAD >=
195			usb->max_bytes_per_frame) {
196		fhci_vdbg(usb->fhci, "not enough space in this frame: "
197			  "%d %d %d\n", usb->actual_frame->total_bytes, len,
198			  usb->max_bytes_per_frame);
199		return -1;
200	}
201
202	/* check if there's enough time in this frame to submit this TD */
203	if (usb->actual_frame->frame_status != FRAME_IS_PREPARED &&
204	    (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION ||
205	     (fw_transaction_time + usb->sw_transaction_time >=
206	      1000 - fhci_get_sof_timer_count(usb)))) {
207		fhci_dbg(usb->fhci, "not enough time in this frame\n");
208		return -1;
209	}
210
211	/* update frame object fields before transmitting */
212	pkt = cq_get(&usb->ep0->empty_frame_Q);
213	if (!pkt) {
214		fhci_dbg(usb->fhci, "there is no empty frame\n");
215		return -1;
216	}
217	td->pkt = pkt;
218
219	pkt->info = 0;
220	if (data == NULL) {
221		data = cq_get(&usb->ep0->dummy_packets_Q);
222		BUG_ON(!data);
223		pkt->info = PKT_DUMMY_PACKET;
224	}
225	pkt->data = data;
226	pkt->len = len;
227	pkt->status = USB_TD_OK;
228	/* update TD status field before transmitting */
229	td->status = USB_TD_INPROGRESS;
230	/* update actual frame time object with the actual transmission */
231	usb->actual_frame->total_bytes += (len + PROTOCOL_OVERHEAD);
232	fhci_add_td_to_frame(usb->actual_frame, td);
233
234	if (usb->port_status != FHCI_PORT_FULL &&
235			usb->port_status != FHCI_PORT_LOW) {
236		pkt->status = USB_TD_TX_ER_TIMEOUT;
237		pkt->len = 0;
238		fhci_transaction_confirm(usb, pkt);
239	} else if (fhci_host_transaction(usb, pkt, td->type, ed->dev_addr,
240			ed->ep_addr, ed->mode, ed->speed, td->toggle)) {
241		/* remove TD from actual frame */
242		list_del_init(&td->frame_lh);
243		td->status = USB_TD_OK;
244		if (pkt->info & PKT_DUMMY_PACKET)
245			cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
246		recycle_frame(usb, pkt);
247		usb->actual_frame->total_bytes -= (len + PROTOCOL_OVERHEAD);
248		fhci_err(usb->fhci, "host transaction failed\n");
249		return -1;
250	}
251
252	return len;
253}
254
255static void move_head_to_tail(struct list_head *list)
256{
257	struct list_head *node = list->next;
258
259	if (!list_empty(list)) {
260		list_move_tail(node, list);
 
261	}
262}
263
264/*
265 * This function goes through the endpoint list and schedules the
266 * transactions within this list
267 */
268static int scan_ed_list(struct fhci_usb *usb,
269			struct list_head *list, enum fhci_tf_mode list_type)
270{
271	static const int frame_part[4] = {
272		[FHCI_TF_CTRL] = MAX_BYTES_PER_FRAME,
273		[FHCI_TF_ISO] = (MAX_BYTES_PER_FRAME *
274				 MAX_PERIODIC_FRAME_USAGE) / 100,
275		[FHCI_TF_BULK] = MAX_BYTES_PER_FRAME,
276		[FHCI_TF_INTR] = (MAX_BYTES_PER_FRAME *
277				  MAX_PERIODIC_FRAME_USAGE) / 100
278	};
279	struct ed *ed;
280	struct td *td;
281	int ans = 1;
282	u32 save_transaction_time = usb->sw_transaction_time;
283
284	list_for_each_entry(ed, list, node) {
285		td = ed->td_head;
286
287		if (!td || td->status == USB_TD_INPROGRESS)
288			continue;
289
290		if (ed->state != FHCI_ED_OPER) {
291			if (ed->state == FHCI_ED_URB_DEL) {
292				td->status = USB_TD_OK;
293				fhci_move_td_from_ed_to_done_list(usb, ed);
294				ed->state = FHCI_ED_SKIP;
295			}
296			continue;
297		}
298
299		/*
300		 * if it isn't interrupt pipe or it is not iso pipe and the
301		 * interval time passed
302		 */
303		if ((list_type == FHCI_TF_INTR || list_type == FHCI_TF_ISO) &&
304				(((usb->actual_frame->frame_num -
305				   td->start_frame) & 0x7ff) < td->interval))
306			continue;
307
308		if (add_packet(usb, ed, td) < 0)
309			continue;
310
311		/* update time stamps in the TD */
312		td->start_frame = usb->actual_frame->frame_num;
313		usb->sw_transaction_time += save_transaction_time;
314
315		if (usb->actual_frame->total_bytes >=
316					usb->max_bytes_per_frame) {
317			usb->actual_frame->frame_status =
318				FRAME_DATA_END_TRANSMISSION;
319			fhci_push_dummy_bd(usb->ep0);
320			ans = 0;
321			break;
322		}
323
324		if (usb->actual_frame->total_bytes >= frame_part[list_type])
325			break;
326	}
327
328	/* be fair to each ED(move list head around) */
329	move_head_to_tail(list);
330	usb->sw_transaction_time = save_transaction_time;
331
332	return ans;
333}
334
335static u32 rotate_frames(struct fhci_usb *usb)
336{
337	struct fhci_hcd *fhci = usb->fhci;
338
339	if (!list_empty(&usb->actual_frame->tds_list)) {
340		if ((((in_be16(&fhci->pram->frame_num) & 0x07ff) -
341		      usb->actual_frame->frame_num) & 0x7ff) > 5)
342			fhci_flush_actual_frame(usb);
343		else
344			return -EINVAL;
345	}
346
347	usb->actual_frame->frame_status = FRAME_IS_PREPARED;
348	usb->actual_frame->frame_num = in_be16(&fhci->pram->frame_num) & 0x7ff;
349	usb->actual_frame->total_bytes = 0;
350
351	return 0;
352}
353
354/*
355 * This function schedule the USB transaction and will process the
356 * endpoint in the following order: iso, interrupt, control and bulk.
357 */
358void fhci_schedule_transactions(struct fhci_usb *usb)
359{
360	int left = 1;
361
362	if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
363		if (rotate_frames(usb) != 0)
364			return;
365
366	if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
367		return;
368
369	if (usb->actual_frame->total_bytes == 0) {
370		/*
371		 * schedule the next available ISO transfer
372		 *or next stage of the ISO transfer
373		 */
374		scan_ed_list(usb, &usb->hc_list->iso_list, FHCI_TF_ISO);
375
376		/*
377		 * schedule the next available interrupt transfer or
378		 * the next stage of the interrupt transfer
379		 */
380		scan_ed_list(usb, &usb->hc_list->intr_list, FHCI_TF_INTR);
381
382		/*
383		 * schedule the next available control transfer
384		 * or the next stage of the control transfer
385		 */
386		left = scan_ed_list(usb, &usb->hc_list->ctrl_list,
387				    FHCI_TF_CTRL);
388	}
389
390	/*
391	 * schedule the next available bulk transfer or the next stage of the
392	 * bulk transfer
393	 */
394	if (left > 0)
395		scan_ed_list(usb, &usb->hc_list->bulk_list, FHCI_TF_BULK);
396}
397
398/* Handles SOF interrupt */
399static void sof_interrupt(struct fhci_hcd *fhci)
400{
401	struct fhci_usb *usb = fhci->usb_lld;
402
403	if ((usb->port_status == FHCI_PORT_DISABLED) &&
404	    (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_CONNECTION) &&
405	    !(usb->vroot_hub->port.wPortChange & USB_PORT_STAT_C_CONNECTION)) {
406		if (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_LOW_SPEED)
407			usb->port_status = FHCI_PORT_LOW;
408		else
409			usb->port_status = FHCI_PORT_FULL;
410		/* Disable IDLE */
411		usb->saved_msk &= ~USB_E_IDLE_MASK;
412		out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
413	}
414
415	gtm_set_exact_timer16(fhci->timer, usb->max_frame_usage, false);
416
417	fhci_host_transmit_actual_frame(usb);
418	usb->actual_frame->frame_status = FRAME_IS_TRANSMITTED;
419
420	fhci_schedule_transactions(usb);
421}
422
423/* Handles device disconnected interrupt on port */
424void fhci_device_disconnected_interrupt(struct fhci_hcd *fhci)
425{
426	struct fhci_usb *usb = fhci->usb_lld;
427
428	fhci_dbg(fhci, "-> %s\n", __func__);
429
430	fhci_usb_disable_interrupt(usb);
431	clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
432	usb->port_status = FHCI_PORT_DISABLED;
433
434	fhci_stop_sof_timer(fhci);
435
436	/* Enable IDLE since we want to know if something comes along */
437	usb->saved_msk |= USB_E_IDLE_MASK;
438	out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
439
440	usb->vroot_hub->port.wPortStatus &= ~USB_PORT_STAT_CONNECTION;
441	usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_CONNECTION;
442	usb->max_bytes_per_frame = 0;
443	fhci_usb_enable_interrupt(usb);
444
445	fhci_dbg(fhci, "<- %s\n", __func__);
446}
447
448/* detect a new device connected on the USB port */
449void fhci_device_connected_interrupt(struct fhci_hcd *fhci)
450{
451
452	struct fhci_usb *usb = fhci->usb_lld;
453	int state;
454	int ret;
455
456	fhci_dbg(fhci, "-> %s\n", __func__);
457
458	fhci_usb_disable_interrupt(usb);
459	state = fhci_ioports_check_bus_state(fhci);
460
461	/* low-speed device was connected to the USB port */
462	if (state == 1) {
463		ret = qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
464		if (ret) {
465			fhci_warn(fhci, "Low-Speed device is not supported, "
466				  "try use BRGx\n");
467			goto out;
468		}
469
470		usb->port_status = FHCI_PORT_LOW;
471		setbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
472		usb->vroot_hub->port.wPortStatus |=
473		    (USB_PORT_STAT_LOW_SPEED |
474		     USB_PORT_STAT_CONNECTION);
475		usb->vroot_hub->port.wPortChange |=
476		    USB_PORT_STAT_C_CONNECTION;
477		usb->max_bytes_per_frame =
478		    (MAX_BYTES_PER_FRAME >> 3) - 7;
479		fhci_port_enable(usb);
480	} else if (state == 2) {
481		ret = qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
482		if (ret) {
483			fhci_warn(fhci, "Full-Speed device is not supported, "
484				  "try use CLKx\n");
485			goto out;
486		}
487
488		usb->port_status = FHCI_PORT_FULL;
489		clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
490		usb->vroot_hub->port.wPortStatus &=
491		    ~USB_PORT_STAT_LOW_SPEED;
492		usb->vroot_hub->port.wPortStatus |=
493		    USB_PORT_STAT_CONNECTION;
494		usb->vroot_hub->port.wPortChange |=
495		    USB_PORT_STAT_C_CONNECTION;
496		usb->max_bytes_per_frame = (MAX_BYTES_PER_FRAME - 15);
497		fhci_port_enable(usb);
498	}
499out:
500	fhci_usb_enable_interrupt(usb);
501	fhci_dbg(fhci, "<- %s\n", __func__);
502}
503
504irqreturn_t fhci_frame_limit_timer_irq(int irq, void *_hcd)
505{
506	struct usb_hcd *hcd = _hcd;
507	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
508	struct fhci_usb *usb = fhci->usb_lld;
509
510	spin_lock(&fhci->lock);
511
512	gtm_set_exact_timer16(fhci->timer, 1000, false);
513
514	if (usb->actual_frame->frame_status == FRAME_IS_TRANSMITTED) {
515		usb->actual_frame->frame_status = FRAME_TIMER_END_TRANSMISSION;
516		fhci_push_dummy_bd(usb->ep0);
517	}
518
519	fhci_schedule_transactions(usb);
520
521	spin_unlock(&fhci->lock);
522
523	return IRQ_HANDLED;
524}
525
526/* Cancel transmission on the USB endpoint */
527static void abort_transmission(struct fhci_usb *usb)
528{
529	fhci_dbg(usb->fhci, "-> %s\n", __func__);
530	/* issue stop Tx command */
531	qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
532	/* flush Tx FIFOs */
533	out_8(&usb->fhci->regs->usb_uscom, USB_CMD_FLUSH_FIFO | EP_ZERO);
534	udelay(1000);
535	/* reset Tx BDs */
536	fhci_flush_bds(usb);
537	/* issue restart Tx command */
538	qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
539	fhci_dbg(usb->fhci, "<- %s\n", __func__);
540}
541
542irqreturn_t fhci_irq(struct usb_hcd *hcd)
543{
544	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
545	struct fhci_usb *usb;
546	u16 usb_er = 0;
547	unsigned long flags;
548
549	spin_lock_irqsave(&fhci->lock, flags);
550
551	usb = fhci->usb_lld;
552
553	usb_er |= in_be16(&usb->fhci->regs->usb_usber) &
554		  in_be16(&usb->fhci->regs->usb_usbmr);
555
556	/* clear event bits for next time */
557	out_be16(&usb->fhci->regs->usb_usber, usb_er);
558
559	fhci_dbg_isr(fhci, usb_er);
560
561	if (usb_er & USB_E_RESET_MASK) {
562		if ((usb->port_status == FHCI_PORT_FULL) ||
563				(usb->port_status == FHCI_PORT_LOW)) {
564			fhci_device_disconnected_interrupt(fhci);
565			usb_er &= ~USB_E_IDLE_MASK;
566		} else if (usb->port_status == FHCI_PORT_WAITING) {
567			usb->port_status = FHCI_PORT_DISCONNECTING;
568
569			/* Turn on IDLE since we want to disconnect */
570			usb->saved_msk |= USB_E_IDLE_MASK;
571			out_be16(&usb->fhci->regs->usb_usber,
572				 usb->saved_msk);
573		} else if (usb->port_status == FHCI_PORT_DISABLED) {
574			if (fhci_ioports_check_bus_state(fhci) == 1)
575				fhci_device_connected_interrupt(fhci);
576		}
577		usb_er &= ~USB_E_RESET_MASK;
578	}
579
580	if (usb_er & USB_E_MSF_MASK) {
581		abort_transmission(fhci->usb_lld);
582		usb_er &= ~USB_E_MSF_MASK;
583	}
584
585	if (usb_er & (USB_E_SOF_MASK | USB_E_SFT_MASK)) {
586		sof_interrupt(fhci);
587		usb_er &= ~(USB_E_SOF_MASK | USB_E_SFT_MASK);
588	}
589
590	if (usb_er & USB_E_TXB_MASK) {
591		fhci_tx_conf_interrupt(fhci->usb_lld);
592		usb_er &= ~USB_E_TXB_MASK;
593	}
594
595	if (usb_er & USB_E_TXE1_MASK) {
596		fhci_tx_conf_interrupt(fhci->usb_lld);
597		usb_er &= ~USB_E_TXE1_MASK;
598	}
599
600	if (usb_er & USB_E_IDLE_MASK) {
601		if (usb->port_status == FHCI_PORT_DISABLED) {
602			usb_er &= ~USB_E_RESET_MASK;
603			fhci_device_connected_interrupt(fhci);
604		} else if (usb->port_status ==
605				FHCI_PORT_DISCONNECTING) {
606			/* XXX usb->port_status = FHCI_PORT_WAITING; */
607			/* Disable IDLE */
608			usb->saved_msk &= ~USB_E_IDLE_MASK;
609			out_be16(&usb->fhci->regs->usb_usbmr,
610				 usb->saved_msk);
611		} else {
612			fhci_dbg_isr(fhci, -1);
613		}
614
615		usb_er &= ~USB_E_IDLE_MASK;
616	}
617
618	spin_unlock_irqrestore(&fhci->lock, flags);
619
620	return IRQ_HANDLED;
621}
622
623
624/*
625 * Process normal completions(error or success) and clean the schedule.
626 *
627 * This is the main path for handing urbs back to drivers. The only other patth
628 * is process_del_list(),which unlinks URBs by scanning EDs,instead of scanning
629 * the (re-reversed) done list as this does.
630 */
631static void process_done_list(unsigned long data)
632{
633	struct urb *urb;
634	struct ed *ed;
635	struct td *td;
636	struct urb_priv *urb_priv;
637	struct fhci_hcd *fhci = (struct fhci_hcd *)data;
638
639	disable_irq(fhci->timer->irq);
640	disable_irq(fhci_to_hcd(fhci)->irq);
641	spin_lock(&fhci->lock);
642
643	td = fhci_remove_td_from_done_list(fhci->hc_list);
644	while (td != NULL) {
645		urb = td->urb;
646		urb_priv = urb->hcpriv;
647		ed = td->ed;
648
649		/* update URB's length and status from TD */
650		fhci_done_td(urb, td);
651		urb_priv->tds_cnt++;
652
653		/*
654		 * if all this urb's TDs are done, call complete()
655		 * Interrupt transfers are the onley special case:
656		 * they are reissued,until "deleted" by usb_unlink_urb
657		 * (real work done in a SOF intr, by process_del_list)
658		 */
659		if (urb_priv->tds_cnt == urb_priv->num_of_tds) {
660			fhci_urb_complete_free(fhci, urb);
661		} else if (urb_priv->state == URB_DEL &&
662				ed->state == FHCI_ED_SKIP) {
663			fhci_del_ed_list(fhci, ed);
664			ed->state = FHCI_ED_OPER;
665		} else if (ed->state == FHCI_ED_HALTED) {
666			urb_priv->state = URB_DEL;
667			ed->state = FHCI_ED_URB_DEL;
668			fhci_del_ed_list(fhci, ed);
669			ed->state = FHCI_ED_OPER;
670		}
671
672		td = fhci_remove_td_from_done_list(fhci->hc_list);
673	}
674
675	spin_unlock(&fhci->lock);
676	enable_irq(fhci->timer->irq);
677	enable_irq(fhci_to_hcd(fhci)->irq);
678}
679
680DECLARE_TASKLET_OLD(fhci_tasklet, process_done_list);
681
682/* transfer complted callback */
683u32 fhci_transfer_confirm_callback(struct fhci_hcd *fhci)
684{
685	if (!fhci->process_done_task->state)
686		tasklet_schedule(fhci->process_done_task);
687	return 0;
688}
689
690/*
691 * adds urb to the endpoint descriptor list
692 * arguments:
693 * fhci		data structure for the Low level host controller
694 * ep		USB Host endpoint data structure
695 * urb		USB request block data structure
696 */
697void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
698{
699	struct ed *ed = urb->ep->hcpriv;
700	struct urb_priv *urb_priv = urb->hcpriv;
701	u32 data_len = urb->transfer_buffer_length;
702	int urb_state = 0;
703	int toggle = 0;
 
704	u8 *data;
705	u16 cnt = 0;
706
707	if (ed == NULL) {
708		ed = fhci_get_empty_ed(fhci);
709		ed->dev_addr = usb_pipedevice(urb->pipe);
710		ed->ep_addr = usb_pipeendpoint(urb->pipe);
711		switch (usb_pipetype(urb->pipe)) {
712		case PIPE_CONTROL:
713			ed->mode = FHCI_TF_CTRL;
714			break;
715		case PIPE_BULK:
716			ed->mode = FHCI_TF_BULK;
717			break;
718		case PIPE_INTERRUPT:
719			ed->mode = FHCI_TF_INTR;
720			break;
721		case PIPE_ISOCHRONOUS:
722			ed->mode = FHCI_TF_ISO;
723			break;
724		default:
725			break;
726		}
727		ed->speed = (urb->dev->speed == USB_SPEED_LOW) ?
728			FHCI_LOW_SPEED : FHCI_FULL_SPEED;
729		ed->max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
 
730		urb->ep->hcpriv = ed;
731		fhci_dbg(fhci, "new ep speed=%d max_pkt_size=%d\n",
732			 ed->speed, ed->max_pkt_size);
733	}
734
735	/* for ISO transfer calculate start frame index */
736	if (ed->mode == FHCI_TF_ISO) {
737		/* Ignore the possibility of underruns */
738		urb->start_frame = ed->td_head ? ed->next_iso :
739						 get_frame_num(fhci);
740		ed->next_iso = (urb->start_frame + urb->interval *
741				urb->number_of_packets) & 0x07ff;
742	}
743
744	/*
745	 * OHCI handles the DATA toggle itself,we just use the USB
746	 * toggle bits
747	 */
748	if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
749			  usb_pipeout(urb->pipe)))
750		toggle = USB_TD_TOGGLE_CARRY;
751	else {
752		toggle = USB_TD_TOGGLE_DATA0;
753		usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
754			      usb_pipeout(urb->pipe), 1);
755	}
756
757	urb_priv->tds_cnt = 0;
758	urb_priv->ed = ed;
759	if (data_len > 0)
760		data = urb->transfer_buffer;
761	else
762		data = NULL;
763
764	switch (ed->mode) {
765	case FHCI_TF_BULK:
766		if (urb->transfer_flags & URB_ZERO_PACKET &&
767				urb->transfer_buffer_length > 0 &&
768				((urb->transfer_buffer_length %
769				usb_endpoint_maxp(&urb->ep->desc)) == 0))
 
770			urb_state = US_BULK0;
771		while (data_len > 4096) {
772			fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
773				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
774							 FHCI_TA_IN,
775				cnt ? USB_TD_TOGGLE_CARRY :
776				      toggle,
777				data, 4096, 0, 0, true);
778			data += 4096;
779			data_len -= 4096;
780			cnt++;
781		}
782
783		fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
784			usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
785			cnt ? USB_TD_TOGGLE_CARRY : toggle,
786			data, data_len, 0, 0, true);
787		cnt++;
788
789		if (urb->transfer_flags & URB_ZERO_PACKET &&
790				cnt < urb_priv->num_of_tds) {
791			fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
792				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
793							 FHCI_TA_IN,
794				USB_TD_TOGGLE_CARRY, NULL, 0, 0, 0, true);
795			cnt++;
796		}
797		break;
798	case FHCI_TF_INTR:
799		urb->start_frame = get_frame_num(fhci) + 1;
800		fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
801			usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
802			USB_TD_TOGGLE_DATA0, data, data_len,
803			urb->interval, urb->start_frame, true);
804		break;
805	case FHCI_TF_CTRL:
806		ed->dev_addr = usb_pipedevice(urb->pipe);
807		ed->max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
808
809		/* setup stage */
810		fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, FHCI_TA_SETUP,
811			USB_TD_TOGGLE_DATA0, urb->setup_packet, 8, 0, 0, true);
812
813		/* data stage */
814		if (data_len > 0) {
815			fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
816				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
817							 FHCI_TA_IN,
818				USB_TD_TOGGLE_DATA1, data, data_len, 0, 0,
819				true);
820		}
821
822		/* status stage */
823		if (data_len > 0)
824			fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
825				(usb_pipeout(urb->pipe) ? FHCI_TA_IN :
826							  FHCI_TA_OUT),
827				USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
828		else
829			 fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
830				FHCI_TA_IN,
831				USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
832
833		urb_state = US_CTRL_SETUP;
834		break;
835	case FHCI_TF_ISO:
836		for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
837			u16 frame = urb->start_frame;
838
839			/*
840			 * FIXME scheduling should handle frame counter
841			 * roll-around ... exotic case (and OHCI has
842			 * a 2^16 iso range, vs other HCs max of 2^10)
843			 */
844			frame += cnt * urb->interval;
845			frame &= 0x07ff;
846			fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
847				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
848							 FHCI_TA_IN,
849				USB_TD_TOGGLE_DATA0,
850				data + urb->iso_frame_desc[cnt].offset,
851				urb->iso_frame_desc[cnt].length,
852				urb->interval, frame, true);
853		}
854		break;
855	default:
856		break;
857	}
858
859	/*
860	 * set the state of URB
861	 * control pipe:3 states -- setup,data,status
862	 * interrupt and bulk pipe:1 state -- data
863	 */
864	urb->pipe &= ~0x1f;
865	urb->pipe |= urb_state & 0x1f;
866
867	urb_priv->state = URB_INPROGRESS;
868
869	if (!ed->td_head) {
870		ed->state = FHCI_ED_OPER;
871		switch (ed->mode) {
872		case FHCI_TF_CTRL:
873			list_add(&ed->node, &fhci->hc_list->ctrl_list);
874			break;
875		case FHCI_TF_BULK:
876			list_add(&ed->node, &fhci->hc_list->bulk_list);
877			break;
878		case FHCI_TF_INTR:
879			list_add(&ed->node, &fhci->hc_list->intr_list);
880			break;
881		case FHCI_TF_ISO:
882			list_add(&ed->node, &fhci->hc_list->iso_list);
883			break;
884		default:
885			break;
886		}
887	}
888
889	fhci_add_tds_to_ed(ed, urb_priv->tds, urb_priv->num_of_tds);
890	fhci->active_urbs++;
891}