1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale QUICC Engine USB Host Controller Driver
5 * Copyright (c) Freescale Semicondutor, Inc. 2006.
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>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/list.h>
20 #include <linux/usb.h>
21 #include <linux/usb/hcd.h>
24 #define DUMMY_BD_BUFFER 0xdeadbeef
25 #define DUMMY2_BD_BUFFER 0xbaadf00d
27 /* Transaction Descriptors bits */
28 #define TD_R 0x8000 /* ready bit */
29 #define TD_W 0x2000 /* wrap bit */
30 #define TD_I 0x1000 /* interrupt on completion */
31 #define TD_L 0x0800 /* last */
32 #define TD_TC 0x0400 /* transmit CRC */
33 #define TD_CNF 0x0200 /* CNF - Must be always 1 */
34 #define TD_LSP 0x0100 /* Low-speed transaction */
35 #define TD_PID 0x00c0 /* packet id */
36 #define TD_RXER 0x0020 /* Rx error or not */
38 #define TD_NAK 0x0010 /* No ack. */
39 #define TD_STAL 0x0008 /* Stall received */
40 #define TD_TO 0x0004 /* time out */
41 #define TD_UN 0x0002 /* underrun */
42 #define TD_NO 0x0010 /* Rx Non Octet Aligned Packet */
43 #define TD_AB 0x0008 /* Frame Aborted */
44 #define TD_CR 0x0004 /* CRC Error */
45 #define TD_OV 0x0002 /* Overrun */
46 #define TD_BOV 0x0001 /* Buffer Overrun */
48 #define TD_ERRORS (TD_NAK | TD_STAL | TD_TO | TD_UN | \
49 TD_NO | TD_AB | TD_CR | TD_OV | TD_BOV)
51 #define TD_PID_DATA0 0x0080 /* Data 0 toggle */
52 #define TD_PID_DATA1 0x00c0 /* Data 1 toggle */
53 #define TD_PID_TOGGLE 0x00c0 /* Data 0/1 toggle mask */
55 #define TD_TOK_SETUP 0x0000
56 #define TD_TOK_OUT 0x4000
57 #define TD_TOK_IN 0x8000
59 #define TD_ENDP 0x0780
60 #define TD_ADDR 0x007f
62 #define TD_ENDP_SHIFT 7
72 static struct usb_td __iomem
*next_bd(struct usb_td __iomem
*base
,
73 struct usb_td __iomem
*td
,
82 void fhci_push_dummy_bd(struct endpoint
*ep
)
84 if (!ep
->already_pushed_dummy_bd
) {
85 u16 td_status
= in_be16(&ep
->empty_td
->status
);
87 out_be32(&ep
->empty_td
->buf_ptr
, DUMMY_BD_BUFFER
);
88 /* get the next TD in the ring */
89 ep
->empty_td
= next_bd(ep
->td_base
, ep
->empty_td
, td_status
);
90 ep
->already_pushed_dummy_bd
= true;
94 /* destroy an USB endpoint */
95 void fhci_ep0_free(struct fhci_usb
*usb
)
103 cpm_muram_free(cpm_muram_offset(ep
->td_base
));
105 if (kfifo_initialized(&ep
->conf_frame_Q
)) {
106 size
= cq_howmany(&ep
->conf_frame_Q
);
107 for (; size
; size
--) {
108 struct packet
*pkt
= cq_get(&ep
->conf_frame_Q
);
112 cq_delete(&ep
->conf_frame_Q
);
115 if (kfifo_initialized(&ep
->empty_frame_Q
)) {
116 size
= cq_howmany(&ep
->empty_frame_Q
);
117 for (; size
; size
--) {
118 struct packet
*pkt
= cq_get(&ep
->empty_frame_Q
);
122 cq_delete(&ep
->empty_frame_Q
);
125 if (kfifo_initialized(&ep
->dummy_packets_Q
)) {
126 size
= cq_howmany(&ep
->dummy_packets_Q
);
127 for (; size
; size
--) {
128 u8
*buff
= cq_get(&ep
->dummy_packets_Q
);
132 cq_delete(&ep
->dummy_packets_Q
);
141 * create the endpoint structure
144 * usb A pointer to the data structure of the USB
145 * data_mem The data memory partition(BUS)
146 * ring_len TD ring length
148 u32
fhci_create_ep(struct fhci_usb
*usb
, enum fhci_mem_alloc data_mem
,
152 struct usb_td __iomem
*td
;
153 unsigned long ep_offset
;
154 char *err_for
= "endpoint PRAM";
158 /* we need at least 3 TDs in the ring */
159 if (!(ring_len
> 2)) {
160 fhci_err(usb
->fhci
, "illegal TD ring length parameters\n");
164 ep
= kzalloc(sizeof(*ep
), GFP_KERNEL
);
168 ep_mem_size
= ring_len
* sizeof(*td
) + sizeof(struct fhci_ep_pram
);
169 ep_offset
= cpm_muram_alloc(ep_mem_size
, 32);
170 if (IS_ERR_VALUE(ep_offset
))
172 ep
->td_base
= cpm_muram_addr(ep_offset
);
174 /* zero all queue pointers */
175 if (cq_new(&ep
->conf_frame_Q
, ring_len
+ 2) ||
176 cq_new(&ep
->empty_frame_Q
, ring_len
+ 2) ||
177 cq_new(&ep
->dummy_packets_Q
, ring_len
+ 2)) {
178 err_for
= "frame_queues";
182 for (i
= 0; i
< (ring_len
+ 1); i
++) {
186 pkt
= kmalloc(sizeof(*pkt
), GFP_KERNEL
);
192 buff
= kmalloc_array(1028, sizeof(*buff
), GFP_KERNEL
);
198 cq_put(&ep
->empty_frame_Q
, pkt
);
199 cq_put(&ep
->dummy_packets_Q
, buff
);
202 /* we put the endpoint parameter RAM right behind the TD ring */
203 ep
->ep_pram_ptr
= (void __iomem
*)ep
->td_base
+ sizeof(*td
) * ring_len
;
205 ep
->conf_td
= ep
->td_base
;
206 ep
->empty_td
= ep
->td_base
;
208 ep
->already_pushed_dummy_bd
= false;
212 for (i
= 0; i
< ring_len
; i
++) {
213 out_be32(&td
->buf_ptr
, 0);
214 out_be16(&td
->status
, 0);
215 out_be16(&td
->length
, 0);
216 out_be16(&td
->extra
, 0);
220 out_be16(&td
->status
, TD_W
); /* for last TD set Wrap bit */
221 out_be16(&td
->length
, 0);
223 /* endpoint structure has been created */
230 fhci_err(usb
->fhci
, "no memory for the %s\n", err_for
);
235 * initialize the endpoint register according to the given parameters
238 * usb A pointer to the data strucutre of the USB
239 * ep A pointer to the endpoint structre
240 * data_mem The data memory partition(BUS)
242 void fhci_init_ep_registers(struct fhci_usb
*usb
, struct endpoint
*ep
,
243 enum fhci_mem_alloc data_mem
)
247 /* set the endpoint registers according to the endpoint */
248 out_be16(&usb
->fhci
->regs
->usb_usep
[0],
249 USB_TRANS_CTR
| USB_EP_MF
| USB_EP_RTE
);
250 out_be16(&usb
->fhci
->pram
->ep_ptr
[0],
251 cpm_muram_offset(ep
->ep_pram_ptr
));
253 rt
= (BUS_MODE_BO_BE
| BUS_MODE_GBL
);
254 #ifdef MULTI_DATA_BUS
255 if (data_mem
== MEM_SECONDARY
)
258 out_8(&ep
->ep_pram_ptr
->rx_func_code
, rt
);
259 out_8(&ep
->ep_pram_ptr
->tx_func_code
, rt
);
260 out_be16(&ep
->ep_pram_ptr
->rx_buff_len
, 1028);
261 out_be16(&ep
->ep_pram_ptr
->rx_base
, 0);
262 out_be16(&ep
->ep_pram_ptr
->tx_base
, cpm_muram_offset(ep
->td_base
));
263 out_be16(&ep
->ep_pram_ptr
->rx_bd_ptr
, 0);
264 out_be16(&ep
->ep_pram_ptr
->tx_bd_ptr
, cpm_muram_offset(ep
->td_base
));
265 out_be32(&ep
->ep_pram_ptr
->tx_state
, 0);
269 * Collect the submitted frames and inform the application about them
270 * It is also preparing the TDs for new frames. If the Tx interrupts
271 * are disabled, the application should call that routine to get
272 * confirmation about the submitted frames. Otherwise, the routine is
273 * called from the interrupt service routine during the Tx interrupt.
274 * In that case the application is informed by calling the application
275 * specific 'fhci_transaction_confirm' routine
277 static void fhci_td_transaction_confirm(struct fhci_usb
*usb
)
279 struct endpoint
*ep
= usb
->ep0
;
281 struct usb_td __iomem
*td
;
288 * collect transmitted BDs from the chip. The routine clears all BDs
289 * with R bit = 0 and the pointer to data buffer is not NULL, that is
290 * BDs which point to the transmitted data buffer
294 td_status
= in_be16(&td
->status
);
295 td_length
= in_be16(&td
->length
);
296 buf
= in_be32(&td
->buf_ptr
);
297 extra_data
= in_be16(&td
->extra
);
299 /* check if the TD is empty */
300 if (!(!(td_status
& TD_R
) && ((td_status
& ~TD_W
) || buf
)))
302 /* check if it is a dummy buffer */
303 else if ((buf
== DUMMY_BD_BUFFER
) && !(td_status
& ~TD_W
))
306 /* mark TD as empty */
307 clrbits16(&td
->status
, ~TD_W
);
308 out_be16(&td
->length
, 0);
309 out_be32(&td
->buf_ptr
, 0);
310 out_be16(&td
->extra
, 0);
311 /* advance the TD pointer */
312 ep
->conf_td
= next_bd(ep
->td_base
, ep
->conf_td
, td_status
);
314 /* check if it is a dummy buffer(type2) */
315 if ((buf
== DUMMY2_BD_BUFFER
) && !(td_status
& ~TD_W
))
318 pkt
= cq_get(&ep
->conf_frame_Q
);
320 fhci_err(usb
->fhci
, "no frame to confirm\n");
322 if (td_status
& TD_ERRORS
) {
323 if (td_status
& TD_RXER
) {
324 if (td_status
& TD_CR
)
325 pkt
->status
= USB_TD_RX_ER_CRC
;
326 else if (td_status
& TD_AB
)
327 pkt
->status
= USB_TD_RX_ER_BITSTUFF
;
328 else if (td_status
& TD_OV
)
329 pkt
->status
= USB_TD_RX_ER_OVERUN
;
330 else if (td_status
& TD_BOV
)
331 pkt
->status
= USB_TD_RX_DATA_OVERUN
;
332 else if (td_status
& TD_NO
)
333 pkt
->status
= USB_TD_RX_ER_NONOCT
;
335 fhci_err(usb
->fhci
, "illegal error "
337 } else if (td_status
& TD_NAK
)
338 pkt
->status
= USB_TD_TX_ER_NAK
;
339 else if (td_status
& TD_TO
)
340 pkt
->status
= USB_TD_TX_ER_TIMEOUT
;
341 else if (td_status
& TD_UN
)
342 pkt
->status
= USB_TD_TX_ER_UNDERUN
;
343 else if (td_status
& TD_STAL
)
344 pkt
->status
= USB_TD_TX_ER_STALL
;
346 fhci_err(usb
->fhci
, "illegal error occurred\n");
347 } else if ((extra_data
& TD_TOK_IN
) &&
348 pkt
->len
> td_length
- CRC_SIZE
) {
349 pkt
->status
= USB_TD_RX_DATA_UNDERUN
;
352 if (extra_data
& TD_TOK_IN
)
353 pkt
->len
= td_length
- CRC_SIZE
;
354 else if (pkt
->info
& PKT_ZLP
)
357 pkt
->len
= td_length
;
359 fhci_transaction_confirm(usb
, pkt
);
364 * Submitting a data frame to a specified endpoint of a USB device
365 * The frame is put in the driver's transmit queue for this endpoint
368 * usb A pointer to the USB structure
369 * pkt A pointer to the user frame structure
370 * trans_type Transaction tyep - IN,OUT or SETUP
371 * dest_addr Device address - 0~127
372 * dest_ep Endpoint number of the device - 0~16
373 * trans_mode Pipe type - ISO,Interrupt,bulk or control
374 * dest_speed USB speed - Low speed or FULL speed
375 * data_toggle Data sequence toggle - 0 or 1
377 u32
fhci_host_transaction(struct fhci_usb
*usb
,
379 enum fhci_ta_type trans_type
,
382 enum fhci_tf_mode trans_mode
,
383 enum fhci_speed dest_speed
, u8 data_toggle
)
385 struct endpoint
*ep
= usb
->ep0
;
386 struct usb_td __iomem
*td
;
390 fhci_usb_disable_interrupt(usb
);
391 /* start from the next BD that should be filled */
393 td_status
= in_be16(&td
->status
);
395 if (td_status
& TD_R
&& in_be16(&td
->length
)) {
396 /* if the TD is not free */
397 fhci_usb_enable_interrupt(usb
);
401 /* get the next TD in the ring */
402 ep
->empty_td
= next_bd(ep
->td_base
, ep
->empty_td
, td_status
);
403 fhci_usb_enable_interrupt(usb
);
405 out_be32(&td
->buf_ptr
, virt_to_phys(pkt
->data
));
406 /* sets up transaction parameters - addr,endp,dir,and type */
407 extra_data
= (dest_ep
<< TD_ENDP_SHIFT
) | dest_addr
;
408 switch (trans_type
) {
410 extra_data
|= TD_TOK_IN
;
413 extra_data
|= TD_TOK_OUT
;
416 extra_data
|= TD_TOK_SETUP
;
419 if (trans_mode
== FHCI_TF_ISO
)
420 extra_data
|= TD_ISO
;
421 out_be16(&td
->extra
, extra_data
);
423 /* sets up the buffer descriptor */
424 td_status
= ((td_status
& TD_W
) | TD_R
| TD_L
| TD_I
| TD_CNF
);
425 if (!(pkt
->info
& PKT_NO_CRC
))
428 switch (trans_type
) {
431 pkt
->info
|= PKT_PID_DATA1
;
433 pkt
->info
|= PKT_PID_DATA0
;
437 td_status
|= TD_PID_DATA1
;
438 pkt
->info
|= PKT_PID_DATA1
;
440 td_status
|= TD_PID_DATA0
;
441 pkt
->info
|= PKT_PID_DATA0
;
446 if ((dest_speed
== FHCI_LOW_SPEED
) &&
447 (usb
->port_status
== FHCI_PORT_FULL
))
450 out_be16(&td
->status
, td_status
);
452 /* set up buffer length */
453 if (trans_type
== FHCI_TA_IN
)
454 out_be16(&td
->length
, pkt
->len
+ CRC_SIZE
);
456 out_be16(&td
->length
, pkt
->len
);
458 /* put the frame to the confirmation queue */
459 cq_put(&ep
->conf_frame_Q
, pkt
);
461 if (cq_howmany(&ep
->conf_frame_Q
) == 1)
462 out_8(&usb
->fhci
->regs
->usb_uscom
, USB_CMD_STR_FIFO
);
467 /* Reset the Tx BD ring */
468 void fhci_flush_bds(struct fhci_usb
*usb
)
473 struct usb_td __iomem
*td
;
474 struct endpoint
*ep
= usb
->ep0
;
478 td_status
= in_be16(&td
->status
);
479 buf
= in_be32(&td
->buf_ptr
);
480 extra_data
= in_be16(&td
->extra
);
482 /* if the TD is not empty - we'll confirm it as Timeout */
483 if (td_status
& TD_R
)
484 out_be16(&td
->status
, (td_status
& ~TD_R
) | TD_TO
);
485 /* if this TD is dummy - let's skip this TD */
486 else if (in_be32(&td
->buf_ptr
) == DUMMY_BD_BUFFER
)
487 out_be32(&td
->buf_ptr
, DUMMY2_BD_BUFFER
);
488 /* if this is the last TD - break */
489 if (td_status
& TD_W
)
495 fhci_td_transaction_confirm(usb
);
499 out_be16(&td
->status
, 0);
500 out_be16(&td
->length
, 0);
501 out_be32(&td
->buf_ptr
, 0);
502 out_be16(&td
->extra
, 0);
504 } while (!(in_be16(&td
->status
) & TD_W
));
505 out_be16(&td
->status
, TD_W
); /* for last TD set Wrap bit */
506 out_be16(&td
->length
, 0);
507 out_be32(&td
->buf_ptr
, 0);
508 out_be16(&td
->extra
, 0);
510 out_be16(&ep
->ep_pram_ptr
->tx_bd_ptr
,
511 in_be16(&ep
->ep_pram_ptr
->tx_base
));
512 out_be32(&ep
->ep_pram_ptr
->tx_state
, 0);
513 out_be16(&ep
->ep_pram_ptr
->tx_cnt
, 0);
514 ep
->empty_td
= ep
->td_base
;
515 ep
->conf_td
= ep
->td_base
;
519 * Flush all transmitted packets from TDs in the actual frame.
520 * This routine is called when something wrong with the controller and
521 * we want to get rid of the actual frame and start again next frame
523 void fhci_flush_actual_frame(struct fhci_usb
*usb
)
530 struct usb_td __iomem
*td
;
531 struct endpoint
*ep
= usb
->ep0
;
533 /* disable the USB controller */
534 mode
= in_8(&usb
->fhci
->regs
->usb_usmod
);
535 out_8(&usb
->fhci
->regs
->usb_usmod
, mode
& ~USB_MODE_EN
);
537 tb_ptr
= in_be16(&ep
->ep_pram_ptr
->tx_bd_ptr
);
538 td
= cpm_muram_addr(tb_ptr
);
539 td_status
= in_be16(&td
->status
);
540 buf_ptr
= in_be32(&td
->buf_ptr
);
541 extra_data
= in_be16(&td
->extra
);
543 if (td_status
& TD_R
) {
544 out_be16(&td
->status
, (td_status
& ~TD_R
) | TD_TO
);
546 out_be32(&td
->buf_ptr
, 0);
547 ep
->already_pushed_dummy_bd
= false;
551 /* advance the TD pointer */
552 td
= next_bd(ep
->td_base
, td
, td_status
);
553 td_status
= in_be16(&td
->status
);
554 buf_ptr
= in_be32(&td
->buf_ptr
);
555 extra_data
= in_be16(&td
->extra
);
556 } while ((td_status
& TD_R
) || buf_ptr
);
558 fhci_td_transaction_confirm(usb
);
560 out_be16(&ep
->ep_pram_ptr
->tx_bd_ptr
,
561 in_be16(&ep
->ep_pram_ptr
->tx_base
));
562 out_be32(&ep
->ep_pram_ptr
->tx_state
, 0);
563 out_be16(&ep
->ep_pram_ptr
->tx_cnt
, 0);
564 ep
->empty_td
= ep
->td_base
;
565 ep
->conf_td
= ep
->td_base
;
567 usb
->actual_frame
->frame_status
= FRAME_TIMER_END_TRANSMISSION
;
569 /* reset the event register */
570 out_be16(&usb
->fhci
->regs
->usb_usber
, 0xffff);
571 /* enable the USB controller */
572 out_8(&usb
->fhci
->regs
->usb_usmod
, mode
| USB_MODE_EN
);
575 /* handles Tx confirm and Tx error interrupt */
576 void fhci_tx_conf_interrupt(struct fhci_usb
*usb
)
578 fhci_td_transaction_confirm(usb
);
581 * Schedule another transaction to this frame only if we have
582 * already confirmed all transaction in the frame.
584 if (((fhci_get_sof_timer_count(usb
) < usb
->max_frame_usage
) ||
585 (usb
->actual_frame
->frame_status
& FRAME_END_TRANSMISSION
)) &&
586 (list_empty(&usb
->actual_frame
->tds_list
)))
587 fhci_schedule_transactions(usb
);
590 void fhci_host_transmit_actual_frame(struct fhci_usb
*usb
)
594 struct usb_td __iomem
*td
;
595 struct endpoint
*ep
= usb
->ep0
;
597 tb_ptr
= in_be16(&ep
->ep_pram_ptr
->tx_bd_ptr
);
598 td
= cpm_muram_addr(tb_ptr
);
600 if (in_be32(&td
->buf_ptr
) == DUMMY_BD_BUFFER
) {
601 struct usb_td __iomem
*old_td
= td
;
603 ep
->already_pushed_dummy_bd
= false;
604 td_status
= in_be16(&td
->status
);
605 /* gets the next TD in the ring */
606 td
= next_bd(ep
->td_base
, td
, td_status
);
607 tb_ptr
= cpm_muram_offset(td
);
608 out_be16(&ep
->ep_pram_ptr
->tx_bd_ptr
, tb_ptr
);
610 /* start transmit only if we have something in the TDs */
611 if (in_be16(&td
->status
) & TD_R
)
612 out_8(&usb
->fhci
->regs
->usb_uscom
, USB_CMD_STR_FIFO
);
614 if (in_be32(&ep
->conf_td
->buf_ptr
) == DUMMY_BD_BUFFER
) {
615 out_be32(&old_td
->buf_ptr
, 0);
616 ep
->conf_td
= next_bd(ep
->td_base
, ep
->conf_td
,
619 out_be32(&old_td
->buf_ptr
, DUMMY2_BD_BUFFER
);