2 * Copyright (C) 2006 by Bryan O'Donoghue, CodeHermit
3 * bodonoghue@CodeHermit.ie
6 * DasUBoot/drivers/usb/gadget/omap1510_udc.c, for design and implementation
9 * SPDX-License-Identifier: GPL-2.0+
14 * 1. #define __SIMULATE_ERROR__ to inject a CRC error into every 2nd TX
15 * packet to force the USB re-transmit protocol.
17 * 2. #define __DEBUG_UDC__ to switch on debug tracing to serial console
18 * be careful that tracing doesn't create Hiesen-bugs with respect to
19 * response timeouts to control requests.
21 * 3. This driver should be able to support any higher level driver that
22 * that wants to do either of the two standard UDC implementations
23 * Control-Bulk-Interrupt or Bulk-IN/Bulk-Out standards. Hence
24 * gserial and cdc_acm should work with this code.
26 * 4. NAK events never actually get raised at all, the documentation
29 * 5. For some reason, cbd_datlen is *always* +2 the value it should be.
30 * this means that having an RX cbd of 16 bytes is not possible, since
31 * the same size is reported for 14 bytes received as 16 bytes received
32 * until we can find out why this happens, RX cbds must be limited to 8
33 * bytes. TODO: check errata for this behaviour.
35 * 6. Right now this code doesn't support properly powering up with the USB
36 * cable attached to the USB host my development board the Adder87x doesn't
37 * have a pull-up fitted to allow this, so it is necessary to power the
38 * board and *then* attached the USB cable to the host. However somebody
39 * with a different design in their board may be able to keep the cable
40 * constantly connected and simply enable/disable a pull-up re
41 * figure 31.1 in MPC885RM.pdf instead of having to power up the board and
42 * then attach the cable !
48 #include <usbdevice.h>
49 #include <usb/mpc8xx_udc.h>
54 DECLARE_GLOBAL_DATA_PTR
;
56 #define ERR(fmt, args...)\
57 serial_printf("ERROR : [%s] %s:%d: "fmt,\
58 __FILE__,__FUNCTION__,__LINE__, ##args)
60 #define DBG(fmt,args...)\
61 serial_printf("[%s] %s:%d: "fmt,\
62 __FILE__,__FUNCTION__,__LINE__, ##args)
64 #define DBG(fmt,args...)
68 #ifdef __SIMULATE_ERROR__
69 static char err_poison_test
= 0;
71 static struct mpc8xx_ep ep_ref
[MAX_ENDPOINTS
];
72 static u32 address_base
= STATE_NOT_READY
;
73 static mpc8xx_udc_state_t udc_state
= 0;
74 static struct usb_device_instance
*udc_device
= 0;
75 static volatile usb_epb_t
*endpoints
[MAX_ENDPOINTS
];
76 static volatile cbd_t
*tx_cbd
[TX_RING_SIZE
];
77 static volatile cbd_t
*rx_cbd
[RX_RING_SIZE
];
78 static volatile immap_t
*immr
= 0;
79 static volatile cpm8xx_t
*cp
= 0;
80 static volatile usb_pram_t
*usb_paramp
= 0;
81 static volatile usb_t
*usbp
= 0;
85 /* Static Function Declarations */
86 static void mpc8xx_udc_state_transition_up (usb_device_state_t initial
,
87 usb_device_state_t final
);
88 static void mpc8xx_udc_state_transition_down (usb_device_state_t initial
,
89 usb_device_state_t final
);
90 static void mpc8xx_udc_stall (unsigned int ep
);
91 static void mpc8xx_udc_flush_tx_fifo (int epid
);
92 static void mpc8xx_udc_flush_rx_fifo (void);
93 static void mpc8xx_udc_clear_rxbd (volatile cbd_t
* rx_cbdp
);
94 static void mpc8xx_udc_init_tx (struct usb_endpoint_instance
*epi
,
96 static void mpc8xx_udc_dump_request (struct usb_device_request
*request
);
97 static void mpc8xx_udc_clock_init (volatile immap_t
* immr
,
98 volatile cpm8xx_t
* cp
);
99 static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance
*epi
);
100 static int mpc8xx_udc_epn_rx (unsigned int epid
, volatile cbd_t
* rx_cbdp
);
101 static void mpc8xx_udc_ep0_rx (volatile cbd_t
* rx_cbdp
);
102 static void mpc8xx_udc_cbd_init (void);
103 static void mpc8xx_udc_endpoint_init (void);
104 static void mpc8xx_udc_cbd_attach (int ep
, uchar tx_size
, uchar rx_size
);
105 static u32
mpc8xx_udc_alloc (u32 data_size
, u32 alignment
);
106 static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t
* rx_cbdp
);
107 static void mpc8xx_udc_set_nak (unsigned int ep
);
108 static short mpc8xx_udc_handle_txerr (void);
109 static void mpc8xx_udc_advance_rx (volatile cbd_t
** rx_cbdp
, int epid
);
111 /******************************************************************************
113 *****************************************************************************/
117 * Do initial bus gluing
121 /* Init various pointers */
122 immr
= (immap_t
*) CONFIG_SYS_IMMR
;
123 cp
= (cpm8xx_t
*) & (immr
->im_cpm
);
124 usb_paramp
= (usb_pram_t
*) & (cp
->cp_dparam
[PROFF_USB
]);
125 usbp
= (usb_t
*) & (cp
->cp_scc
[0]);
127 memset (ep_ref
, 0x00, (sizeof (struct mpc8xx_ep
) * MAX_ENDPOINTS
));
130 udc_state
= STATE_NOT_READY
;
135 /* Set USB Frame #0, Respond at Address & Get a clock source */
137 mpc8xx_udc_clock_init (immr
, cp
);
139 /* PA15, PA14 as perhiperal USBRXD and USBOE */
140 immr
->im_ioport
.iop_padir
&= ~0x0003;
141 immr
->im_ioport
.iop_papar
|= 0x0003;
143 /* PC11/PC10 as peripheral USBRXP USBRXN */
144 immr
->im_ioport
.iop_pcso
|= 0x0030;
146 /* PC7/PC6 as perhiperal USBTXP and USBTXN */
147 immr
->im_ioport
.iop_pcdir
|= 0x0300;
148 immr
->im_ioport
.iop_pcpar
|= 0x0300;
150 /* Set the base address */
151 address_base
= (u32
) (cp
->cp_dpmem
+ CPM_USB_BASE
);
153 /* Initialise endpoints and circular buffers */
154 mpc8xx_udc_endpoint_init ();
155 mpc8xx_udc_cbd_init ();
157 /* Assign allocated Dual Port Endpoint descriptors */
158 usb_paramp
->ep0ptr
= (u32
) endpoints
[0];
159 usb_paramp
->ep1ptr
= (u32
) endpoints
[1];
160 usb_paramp
->ep2ptr
= (u32
) endpoints
[2];
161 usb_paramp
->ep3ptr
= (u32
) endpoints
[3];
162 usb_paramp
->frame_n
= 0;
164 DBG ("ep0ptr=0x%08x ep1ptr=0x%08x ep2ptr=0x%08x ep3ptr=0x%08x\n",
165 usb_paramp
->ep0ptr
, usb_paramp
->ep1ptr
, usb_paramp
->ep2ptr
,
173 * Poll for whatever events may have occured
178 volatile cbd_t
*rx_cbdp
= 0;
179 volatile cbd_t
*rx_cbdp_base
= 0;
181 if (udc_state
!= STATE_READY
) {
185 if (usbp
->usber
& USB_E_BSY
) {
186 /* This shouldn't happen. If it does then it's a bug ! */
187 usbp
->usber
|= USB_E_BSY
;
188 mpc8xx_udc_flush_rx_fifo ();
191 /* Scan all RX/Bidirectional Endpoints for RX data. */
192 for (epid
= 0; epid
< MAX_ENDPOINTS
; epid
++) {
193 if (!ep_ref
[epid
].prx
) {
196 rx_cbdp
= rx_cbdp_base
= ep_ref
[epid
].prx
;
199 if (!(rx_cbdp
->cbd_sc
& RX_BD_E
)) {
201 if (rx_cbdp
->cbd_sc
& 0x1F) {
202 /* Corrupt data discard it.
203 * Controller has NAK'd this packet.
205 mpc8xx_udc_clear_rxbd (rx_cbdp
);
209 mpc8xx_udc_ep0_rx (rx_cbdp
);
213 mpc8xx_udc_set_nak (epid
);
214 mpc8xx_udc_epn_rx (epid
, rx_cbdp
);
215 mpc8xx_udc_clear_rxbd (rx_cbdp
);
219 /* Advance RX CBD pointer */
220 mpc8xx_udc_advance_rx (&rx_cbdp
, epid
);
221 ep_ref
[epid
].prx
= rx_cbdp
;
223 /* Advance RX CBD pointer */
224 mpc8xx_udc_advance_rx (&rx_cbdp
, epid
);
227 } while (rx_cbdp
!= rx_cbdp_base
);
230 /* Handle TX events as appropiate, the correct place to do this is
231 * in a tx routine. Perhaps TX on epn was pre-empted by ep0
234 if (usbp
->usber
& USB_E_TXB
) {
235 usbp
->usber
|= USB_E_TXB
;
238 if (usbp
->usber
& (USB_TX_ERRMASK
)) {
239 mpc8xx_udc_handle_txerr ();
242 /* Switch to the default state, respond at the default address */
243 if (usbp
->usber
& USB_E_RESET
) {
244 usbp
->usber
|= USB_E_RESET
;
246 udc_device
->device_state
= STATE_DEFAULT
;
249 /* if(usbp->usber&USB_E_IDLE){
250 We could suspend here !
251 usbp->usber|=USB_E_IDLE;
252 DBG("idle state change\n");
255 We could resume here when IDLE is deasserted !
256 Not worth doing, so long as we are self powered though.
263 /* udc_endpoint_write
265 * Write some data to an endpoint
267 int udc_endpoint_write (struct usb_endpoint_instance
*epi
)
270 short epid
= 1, unnak
= 0, ret
= 0;
272 if (udc_state
!= STATE_READY
) {
273 ERR ("invalid udc_state != STATE_READY!\n");
277 if (!udc_device
|| !epi
) {
281 if (udc_device
->device_state
!= STATE_CONFIGURED
) {
285 ep
= epi
->endpoint_address
& 0x03;
286 if (ep
>= MAX_ENDPOINTS
) {
290 /* Set NAK for all RX endpoints during TX */
291 for (epid
= 1; epid
< MAX_ENDPOINTS
; epid
++) {
293 /* Don't set NAK on DATA IN/CONTROL endpoints */
294 if (ep_ref
[epid
].sc
& USB_DIR_IN
) {
298 if (!(usbp
->usep
[epid
] & (USEP_THS_NAK
| USEP_RHS_NAK
))) {
302 mpc8xx_udc_set_nak (epid
);
305 mpc8xx_udc_init_tx (&udc_device
->bus
->endpoint_array
[ep
],
307 ret
= mpc8xx_udc_ep_tx (&udc_device
->bus
->endpoint_array
[ep
]);
309 /* Remove temporary NAK */
310 for (epid
= 1; epid
< MAX_ENDPOINTS
; epid
++) {
311 if (unnak
& (1 << epid
)) {
312 udc_unset_nak (epid
);
319 /* mpc8xx_udc_assign_urb
321 * Associate a given urb to an endpoint TX or RX transmit/receive buffers
323 static int mpc8xx_udc_assign_urb (int ep
, char direction
)
325 struct usb_endpoint_instance
*epi
= 0;
327 if (ep
>= MAX_ENDPOINTS
) {
330 epi
= &udc_device
->bus
->endpoint_array
[ep
];
335 if (!ep_ref
[ep
].urb
) {
336 ep_ref
[ep
].urb
= usbd_alloc_urb (udc_device
, udc_device
->bus
->endpoint_array
);
337 if (!ep_ref
[ep
].urb
) {
341 ep_ref
[ep
].urb
->actual_length
= 0;
346 epi
->tx_urb
= ep_ref
[ep
].urb
;
349 epi
->rcv_urb
= ep_ref
[ep
].urb
;
357 udc_state
= STATE_ERROR
;
363 * Associate U-Boot software endpoints to mpc8xx endpoint parameter ram
364 * Isochronous endpoints aren't yet supported!
366 void udc_setup_ep (struct usb_device_instance
*device
, unsigned int ep
,
367 struct usb_endpoint_instance
*epi
)
372 if (epi
&& (ep
< MAX_ENDPOINTS
)) {
375 if (epi
->rcv_attributes
!= USB_ENDPOINT_XFER_CONTROL
376 || epi
->tx_attributes
!=
377 USB_ENDPOINT_XFER_CONTROL
) {
379 /* ep0 must be a control endpoint */
380 udc_state
= STATE_ERROR
;
384 if (!(ep_ref
[ep
].sc
& EP_ATTACHED
)) {
385 mpc8xx_udc_cbd_attach (ep
, epi
->tx_packetSize
,
386 epi
->rcv_packetSize
);
388 usbp
->usep
[ep
] = 0x0000;
392 if ((epi
->endpoint_address
& USB_ENDPOINT_DIR_MASK
)
396 ep_attrib
= epi
->tx_attributes
;
397 epi
->rcv_packetSize
= 0;
398 ep_ref
[ep
].sc
|= USB_DIR_IN
;
402 ep_attrib
= epi
->rcv_attributes
;
403 epi
->tx_packetSize
= 0;
404 ep_ref
[ep
].sc
&= ~USB_DIR_IN
;
407 if (mpc8xx_udc_assign_urb (ep
, epi
->endpoint_address
408 & USB_ENDPOINT_DIR_MASK
)) {
413 case USB_ENDPOINT_XFER_CONTROL
:
414 if (!(ep_ref
[ep
].sc
& EP_ATTACHED
)) {
415 mpc8xx_udc_cbd_attach (ep
,
417 epi
->rcv_packetSize
);
419 usbp
->usep
[ep
] = ep
<< 12;
420 epi
->rcv_urb
= epi
->tx_urb
= ep_ref
[ep
].urb
;
423 case USB_ENDPOINT_XFER_BULK
:
424 case USB_ENDPOINT_XFER_INT
:
425 if (!(ep_ref
[ep
].sc
& EP_ATTACHED
)) {
427 mpc8xx_udc_cbd_attach (ep
,
431 mpc8xx_udc_cbd_attach (ep
,
433 epi
->rcv_packetSize
);
436 usbp
->usep
[ep
] = (ep
<< 12) | ((ep_attrib
) << 8);
439 case USB_ENDPOINT_XFER_ISOC
:
441 serial_printf ("Error endpoint attrib %d>3\n", ep_attrib
);
442 udc_state
= STATE_ERROR
;
451 * Move state, switch on the USB
453 void udc_connect (void)
455 /* Enable pull-up resistor on D+
456 * TODO: fit a pull-up resistor to drive SE0 for > 2.5us
459 if (udc_state
!= STATE_ERROR
) {
460 udc_state
= STATE_READY
;
461 usbp
->usmod
|= USMOD_EN
;
467 * Disconnect is not used but, is included for completeness
469 void udc_disconnect (void)
471 /* Disable pull-up resistor on D-
472 * TODO: fix a pullup resistor to control this
475 if (udc_state
!= STATE_ERROR
) {
476 udc_state
= STATE_NOT_READY
;
478 usbp
->usmod
&= ~USMOD_EN
;
483 * Grab an EP0 URB, register interest in a subset of USB events
485 void udc_enable (struct usb_device_instance
*device
)
487 if (udc_state
== STATE_ERROR
) {
493 if (!ep_ref
[0].urb
) {
494 ep_ref
[0].urb
= usbd_alloc_urb (device
, device
->bus
->endpoint_array
);
497 /* Register interest in all events except SOF, enable transceiver */
498 usbp
->usber
= 0x03FF;
499 usbp
->usbmr
= 0x02F7;
506 * disable the currently hooked device
508 void udc_disable (void)
512 if (udc_state
== STATE_ERROR
) {
513 DBG ("Won't disable UDC. udc_state==STATE_ERROR !\n");
519 for (; i
< MAX_ENDPOINTS
; i
++) {
521 usbd_dealloc_urb (ep_ref
[i
].urb
);
527 usbp
->usmod
= ~USMOD_EN
;
528 udc_state
= STATE_NOT_READY
;
531 /* udc_startup_events
533 * Enable the specified device
535 void udc_startup_events (struct usb_device_instance
*device
)
538 if (udc_state
== STATE_READY
) {
539 usbd_device_event_irq (device
, DEVICE_CREATE
, 0);
545 * Allow upper layers to signal lower layers should not accept more RX data
548 void udc_set_nak (int epid
)
551 mpc8xx_udc_set_nak (epid
);
557 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
558 * Switch off NAKing on this endpoint to accept more data output from host.
561 void udc_unset_nak (int epid
)
563 if (epid
> MAX_ENDPOINTS
) {
567 if (usbp
->usep
[epid
] & (USEP_THS_NAK
| USEP_RHS_NAK
)) {
568 usbp
->usep
[epid
] &= ~(USEP_THS_NAK
| USEP_RHS_NAK
);
573 /******************************************************************************
575 ******************************************************************************/
577 /* udc_state_transition_up
578 * udc_state_transition_down
580 * Helper functions to implement device state changes. The device states and
581 * the events that transition between them are:
586 * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
592 * DEVICE_RESET DEVICE_POWER_INTERRUPTION
598 * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
604 * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
609 * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
610 * to STATE_CONFIGURED) from the specified initial state to the specified final
611 * state, passing through each intermediate state on the way. If the initial
612 * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
613 * no state transitions will take place.
615 * udc_state_transition_down transitions down (in the direction from
616 * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
617 * specified final state, passing through each intermediate state on the way.
618 * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
619 * state, then no state transitions will take place.
623 static void mpc8xx_udc_state_transition_up (usb_device_state_t initial
,
624 usb_device_state_t final
)
626 if (initial
< final
) {
629 usbd_device_event_irq (udc_device
,
630 DEVICE_HUB_CONFIGURED
, 0);
631 if (final
== STATE_POWERED
)
634 usbd_device_event_irq (udc_device
, DEVICE_RESET
, 0);
635 if (final
== STATE_DEFAULT
)
638 usbd_device_event_irq (udc_device
,
639 DEVICE_ADDRESS_ASSIGNED
, 0);
640 if (final
== STATE_ADDRESSED
)
642 case STATE_ADDRESSED
:
643 usbd_device_event_irq (udc_device
, DEVICE_CONFIGURED
,
645 case STATE_CONFIGURED
:
653 static void mpc8xx_udc_state_transition_down (usb_device_state_t initial
,
654 usb_device_state_t final
)
656 if (initial
> final
) {
658 case STATE_CONFIGURED
:
659 usbd_device_event_irq (udc_device
,
660 DEVICE_DE_CONFIGURED
, 0);
661 if (final
== STATE_ADDRESSED
)
663 case STATE_ADDRESSED
:
664 usbd_device_event_irq (udc_device
, DEVICE_RESET
, 0);
665 if (final
== STATE_DEFAULT
)
668 usbd_device_event_irq (udc_device
,
669 DEVICE_POWER_INTERRUPTION
, 0);
670 if (final
== STATE_POWERED
)
673 usbd_device_event_irq (udc_device
, DEVICE_HUB_RESET
,
685 * Force returning of STALL tokens on the given endpoint. Protocol or function
686 * STALL conditions are permissable here
688 static void mpc8xx_udc_stall (unsigned int ep
)
690 usbp
->usep
[ep
] |= STALL_BITMASK
;
693 /* mpc8xx_udc_set_nak
695 * Force returning of NAK responses for the given endpoint as a kind of very
696 * simple flow control
698 static void mpc8xx_udc_set_nak (unsigned int ep
)
700 usbp
->usep
[ep
] |= NAK_BITMASK
;
704 /* mpc8xx_udc_handle_txerr
706 * Handle errors relevant to TX. Return a status code to allow calling
707 * indicative of what if anything happened
709 static short mpc8xx_udc_handle_txerr ()
711 short ep
= 0, ret
= 0;
713 for (; ep
< TX_RING_SIZE
; ep
++) {
714 if (usbp
->usber
& (0x10 << ep
)) {
716 /* Timeout or underrun */
717 if (tx_cbd
[ep
]->cbd_sc
& 0x06) {
719 mpc8xx_udc_flush_tx_fifo (ep
);
722 if (usbp
->usep
[ep
] & STALL_BITMASK
) {
724 usbp
->usep
[ep
] &= ~STALL_BITMASK
;
728 usbp
->usber
|= (0x10 << ep
);
734 /* mpc8xx_udc_advance_rx
738 static void mpc8xx_udc_advance_rx (volatile cbd_t
** rx_cbdp
, int epid
)
740 if ((*rx_cbdp
)->cbd_sc
& RX_BD_W
) {
741 *rx_cbdp
= (volatile cbd_t
*) (endpoints
[epid
]->rbase
+ CONFIG_SYS_IMMR
);
749 /* mpc8xx_udc_flush_tx_fifo
751 * Flush a given TX fifo. Assumes one tx cbd per endpoint
753 static void mpc8xx_udc_flush_tx_fifo (int epid
)
755 volatile cbd_t
*tx_cbdp
= 0;
757 if (epid
> MAX_ENDPOINTS
) {
762 immr
->im_cpm
.cp_cpcr
= ((epid
<< 2) | 0x1D01);
764 while (immr
->im_cpm
.cp_cpcr
& 0x01);
766 usbp
->uscom
= 0x40 | 0;
769 tx_cbdp
= (cbd_t
*) (endpoints
[epid
]->tbptr
+ CONFIG_SYS_IMMR
);
770 tx_cbdp
->cbd_sc
= (TX_BD_I
| TX_BD_W
);
773 endpoints
[epid
]->tptr
= endpoints
[epid
]->tbase
;
774 endpoints
[epid
]->tstate
= 0x00;
775 endpoints
[epid
]->tbcnt
= 0x00;
778 immr
->im_cpm
.cp_cpcr
= ((epid
<< 2) | 0x2D01);
780 while (immr
->im_cpm
.cp_cpcr
& 0x01);
785 /* mpc8xx_udc_flush_rx_fifo
787 * For the sake of completeness of the namespace, it seems like
788 * a good-design-decision (tm) to include mpc8xx_udc_flush_rx_fifo();
789 * If RX_BD_E is true => a driver bug either here or in an upper layer
790 * not polling frequently enough. If RX_BD_E is true we have told the host
791 * we have accepted data but, the CPM found it had no-where to put that data
792 * which needless to say would be a bad thing.
794 static void mpc8xx_udc_flush_rx_fifo ()
798 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
799 if (!(rx_cbd
[i
]->cbd_sc
& RX_BD_E
)) {
800 ERR ("buf %p used rx data len = 0x%x sc=0x%x!\n",
801 rx_cbd
[i
], rx_cbd
[i
]->cbd_datlen
,
806 ERR ("BUG : Input over-run\n");
809 /* mpc8xx_udc_clear_rxbd
811 * Release control of RX CBD to CP.
813 static void mpc8xx_udc_clear_rxbd (volatile cbd_t
* rx_cbdp
)
815 rx_cbdp
->cbd_datlen
= 0x0000;
816 rx_cbdp
->cbd_sc
= ((rx_cbdp
->cbd_sc
& RX_BD_W
) | (RX_BD_E
| RX_BD_I
));
822 * Parse for tx timeout, control RX or USB reset/busy conditions
823 * Return -1 on timeout, -2 on fatal error, else return zero
825 static int mpc8xx_udc_tx_irq (int ep
)
829 if (usbp
->usber
& (USB_TX_ERRMASK
)) {
830 if (mpc8xx_udc_handle_txerr ()) {
831 /* Timeout, controlling function must retry send */
836 if (usbp
->usber
& (USB_E_RESET
| USB_E_BSY
)) {
837 /* Fatal, abandon TX transaction */
841 if (usbp
->usber
& USB_E_RXB
) {
842 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
843 if (!(rx_cbd
[i
]->cbd_sc
& RX_BD_E
)) {
844 if ((rx_cbd
[i
] == ep_ref
[0].prx
) || ep
) {
856 * Transmit in a re-entrant fashion outbound USB packets.
857 * Implement retry/timeout mechanism described in USB specification
858 * Toggle DATA0/DATA1 pids as necessary
859 * Introduces non-standard tx_retry. The USB standard has no scope for slave
860 * devices to give up TX, however tx_retry stops us getting stuck in an endless
863 static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance
*epi
)
865 struct urb
*urb
= epi
->tx_urb
;
866 volatile cbd_t
*tx_cbdp
= 0;
867 unsigned int ep
= 0, pkt_len
= 0, x
= 0, tx_retry
= 0;
870 if (!epi
|| (epi
->endpoint_address
& 0x03) >= MAX_ENDPOINTS
|| !urb
) {
874 ep
= epi
->endpoint_address
& 0x03;
875 tx_cbdp
= (cbd_t
*) (endpoints
[ep
]->tbptr
+ CONFIG_SYS_IMMR
);
877 if (tx_cbdp
->cbd_sc
& TX_BD_R
|| usbp
->usber
& USB_E_TXB
) {
878 mpc8xx_udc_flush_tx_fifo (ep
);
879 usbp
->usber
|= USB_E_TXB
;
882 while (tx_retry
++ < 100) {
883 ret
= mpc8xx_udc_tx_irq (ep
);
885 /* ignore timeout here */
886 } else if (ret
== -2) {
888 mpc8xx_udc_flush_tx_fifo (ep
);
892 tx_cbdp
= (cbd_t
*) (endpoints
[ep
]->tbptr
+ CONFIG_SYS_IMMR
);
893 while (tx_cbdp
->cbd_sc
& TX_BD_R
) {
895 tx_cbdp
->cbd_sc
= (tx_cbdp
->cbd_sc
& TX_BD_W
);
897 pkt_len
= urb
->actual_length
- epi
->sent
;
899 if (pkt_len
> epi
->tx_packetSize
|| pkt_len
> EP_MAX_PKT
) {
900 pkt_len
= MIN (epi
->tx_packetSize
, EP_MAX_PKT
);
903 for (x
= 0; x
< pkt_len
; x
++) {
904 *((unsigned char *) (tx_cbdp
->cbd_bufaddr
+ x
)) =
905 urb
->buffer
[epi
->sent
+ x
];
907 tx_cbdp
->cbd_datlen
= pkt_len
;
908 tx_cbdp
->cbd_sc
|= (CBD_TX_BITMASK
| ep_ref
[ep
].pid
);
911 #ifdef __SIMULATE_ERROR__
912 if (++err_poison_test
== 2) {
914 tx_cbdp
->cbd_sc
&= ~TX_BD_TC
;
918 usbp
->uscom
= (USCOM_STR
| ep
);
920 while (!(usbp
->usber
& USB_E_TXB
)) {
921 ret
= mpc8xx_udc_tx_irq (ep
);
925 } else if (ret
== -2) {
926 if (usbp
->usber
& USB_E_TXB
) {
927 usbp
->usber
|= USB_E_TXB
;
929 mpc8xx_udc_flush_tx_fifo (ep
);
934 if (usbp
->usber
& USB_E_TXB
) {
935 usbp
->usber
|= USB_E_TXB
;
938 /* ACK must be present <= 18bit times from TX */
943 /* TX ACK : USB 2.0 8.7.2, Toggle PID, Advance TX */
944 epi
->sent
+= pkt_len
;
945 epi
->last
= MIN (urb
->actual_length
- epi
->sent
, epi
->tx_packetSize
);
946 TOGGLE_TX_PID (ep_ref
[ep
].pid
);
948 if (epi
->sent
>= epi
->tx_urb
->actual_length
) {
950 epi
->tx_urb
->actual_length
= 0;
953 if (ep_ref
[ep
].sc
& EP_SEND_ZLP
) {
954 ep_ref
[ep
].sc
&= ~EP_SEND_ZLP
;
961 ERR ("TX fail, endpoint 0x%x tx bytes 0x%x/0x%x\n", ep
, epi
->sent
,
962 epi
->tx_urb
->actual_length
);
967 /* mpc8xx_udc_dump_request
969 * Dump a control request to console
971 static void mpc8xx_udc_dump_request (struct usb_device_request
*request
)
973 DBG ("bmRequestType:%02x bRequest:%02x wValue:%04x "
974 "wIndex:%04x wLength:%04x ?\n",
975 request
->bmRequestType
,
977 request
->wValue
, request
->wIndex
, request
->wLength
);
982 /* mpc8xx_udc_ep0_rx_setup
984 * Decode received ep0 SETUP packet. return non-zero on error
986 static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t
* rx_cbdp
)
989 struct urb
*purb
= ep_ref
[0].urb
;
990 struct usb_endpoint_instance
*epi
=
991 &udc_device
->bus
->endpoint_array
[0];
993 for (; x
< rx_cbdp
->cbd_datlen
; x
++) {
994 *(((unsigned char *) &ep_ref
[0].urb
->device_request
) + x
) =
995 *((unsigned char *) (rx_cbdp
->cbd_bufaddr
+ x
));
998 mpc8xx_udc_clear_rxbd (rx_cbdp
);
1000 if (ep0_recv_setup (purb
)) {
1001 mpc8xx_udc_dump_request (&purb
->device_request
);
1005 if ((purb
->device_request
.bmRequestType
& USB_REQ_DIRECTION_MASK
)
1006 == USB_REQ_HOST2DEVICE
) {
1008 switch (purb
->device_request
.bRequest
) {
1009 case USB_REQ_SET_ADDRESS
:
1010 /* Send the Status OUT ZLP */
1011 ep_ref
[0].pid
= TX_BD_PID_DATA1
;
1012 purb
->actual_length
= 0;
1013 mpc8xx_udc_init_tx (epi
, purb
);
1014 mpc8xx_udc_ep_tx (epi
);
1016 /* Move to the addressed state */
1017 usbp
->usaddr
= udc_device
->address
;
1018 mpc8xx_udc_state_transition_up (udc_device
->device_state
,
1022 case USB_REQ_SET_CONFIGURATION
:
1023 if (!purb
->device_request
.wValue
) {
1024 /* Respond at default address */
1025 usbp
->usaddr
= 0x00;
1026 mpc8xx_udc_state_transition_down (udc_device
->device_state
,
1029 /* TODO: Support multiple configurations */
1030 mpc8xx_udc_state_transition_up (udc_device
->device_state
,
1032 for (x
= 1; x
< MAX_ENDPOINTS
; x
++) {
1033 if ((udc_device
->bus
->endpoint_array
[x
].endpoint_address
& USB_ENDPOINT_DIR_MASK
)
1035 ep_ref
[x
].pid
= TX_BD_PID_DATA0
;
1037 ep_ref
[x
].pid
= RX_BD_PID_DATA0
;
1039 /* Set configuration must unstall endpoints */
1040 usbp
->usep
[x
] &= ~STALL_BITMASK
;
1045 /* CDC/Vendor specific */
1049 /* Send ZLP as ACK in Status OUT phase */
1050 ep_ref
[0].pid
= TX_BD_PID_DATA1
;
1051 purb
->actual_length
= 0;
1052 mpc8xx_udc_init_tx (epi
, purb
);
1053 mpc8xx_udc_ep_tx (epi
);
1057 if (purb
->actual_length
) {
1058 ep_ref
[0].pid
= TX_BD_PID_DATA1
;
1059 mpc8xx_udc_init_tx (epi
, purb
);
1061 if (!(purb
->actual_length
% EP0_MAX_PACKET_SIZE
)) {
1062 ep_ref
[0].sc
|= EP_SEND_ZLP
;
1065 if (purb
->device_request
.wValue
==
1066 USB_DESCRIPTOR_TYPE_DEVICE
) {
1067 if (le16_to_cpu (purb
->device_request
.wLength
)
1068 > purb
->actual_length
) {
1069 /* Send EP0_MAX_PACKET_SIZE bytes
1070 * unless correct size requested.
1072 if (purb
->actual_length
> epi
->tx_packetSize
) {
1073 purb
->actual_length
= epi
->tx_packetSize
;
1077 mpc8xx_udc_ep_tx (epi
);
1080 /* Corrupt SETUP packet? */
1081 ERR ("Zero length data or SETUP with DATA-IN phase ?\n");
1088 /* mpc8xx_udc_init_tx
1090 * Setup some basic parameters for a TX transaction
1092 static void mpc8xx_udc_init_tx (struct usb_endpoint_instance
*epi
,
1097 epi
->tx_urb
= tx_urb
;
1100 /* mpc8xx_udc_ep0_rx
1102 * Receive ep0/control USB data. Parse and possibly send a response.
1104 static void mpc8xx_udc_ep0_rx (volatile cbd_t
* rx_cbdp
)
1106 if (rx_cbdp
->cbd_sc
& RX_BD_PID_SETUP
) {
1108 /* Unconditionally accept SETUP packets */
1109 if (mpc8xx_udc_ep0_rx_setup (rx_cbdp
)) {
1110 mpc8xx_udc_stall (0);
1115 mpc8xx_udc_clear_rxbd (rx_cbdp
);
1117 if ((rx_cbdp
->cbd_datlen
- 2)) {
1118 /* SETUP with a DATA phase
1119 * outside of SETUP packet.
1122 mpc8xx_udc_stall (0);
1127 /* mpc8xx_udc_epn_rx
1129 * Receive some data from cbd into USB system urb data abstraction
1130 * Upper layers should NAK if there is insufficient RX data space
1132 static int mpc8xx_udc_epn_rx (unsigned int epid
, volatile cbd_t
* rx_cbdp
)
1134 struct usb_endpoint_instance
*epi
= 0;
1135 struct urb
*urb
= 0;
1138 if (epid
>= MAX_ENDPOINTS
|| !rx_cbdp
->cbd_datlen
) {
1142 /* USB 2.0 PDF section 8.6.4
1143 * Discard data with invalid PID it is a resend.
1145 if (ep_ref
[epid
].pid
!= (rx_cbdp
->cbd_sc
& 0xC0)) {
1148 TOGGLE_RX_PID (ep_ref
[epid
].pid
);
1150 epi
= &udc_device
->bus
->endpoint_array
[epid
];
1153 for (; x
< (rx_cbdp
->cbd_datlen
- 2); x
++) {
1154 *((unsigned char *) (urb
->buffer
+ urb
->actual_length
+ x
)) =
1155 *((unsigned char *) (rx_cbdp
->cbd_bufaddr
+ x
));
1159 usbd_rcv_complete (epi
, x
, 0);
1160 if (ep_ref
[epid
].urb
->status
== RECV_ERROR
) {
1161 DBG ("RX error unset NAK\n");
1162 udc_unset_nak (epid
);
1168 /* mpc8xx_udc_clock_init
1170 * Obtain a clock reference for Full Speed Signaling
1172 static void mpc8xx_udc_clock_init (volatile immap_t
* immr
,
1173 volatile cpm8xx_t
* cp
)
1176 #if defined(CONFIG_SYS_USB_EXTC_CLK)
1178 /* This has been tested with a 48MHz crystal on CLK6 */
1179 switch (CONFIG_SYS_USB_EXTC_CLK
) {
1181 immr
->im_ioport
.iop_papar
|= 0x0100;
1182 immr
->im_ioport
.iop_padir
&= ~0x0100;
1183 cp
->cp_sicr
|= 0x24;
1186 immr
->im_ioport
.iop_papar
|= 0x0200;
1187 immr
->im_ioport
.iop_padir
&= ~0x0200;
1188 cp
->cp_sicr
|= 0x2D;
1191 immr
->im_ioport
.iop_papar
|= 0x0400;
1192 immr
->im_ioport
.iop_padir
&= ~0x0400;
1193 cp
->cp_sicr
|= 0x36;
1196 immr
->im_ioport
.iop_papar
|= 0x0800;
1197 immr
->im_ioport
.iop_padir
&= ~0x0800;
1198 cp
->cp_sicr
|= 0x3F;
1201 udc_state
= STATE_ERROR
;
1205 #elif defined(CONFIG_SYS_USB_BRGCLK)
1207 /* This has been tested with brgclk == 50MHz */
1210 if (gd
->cpu_clk
< 48000000L) {
1211 ERR ("brgclk is too slow for full-speed USB!\n");
1212 udc_state
= STATE_ERROR
;
1216 /* Assume the brgclk is 'good enough', we want !(gd->cpu_clk%48MHz)
1217 * but, can /probably/ live with close-ish alternative rates.
1219 divisor
= (gd
->cpu_clk
/ 48000000L) - 1;
1220 cp
->cp_sicr
&= ~0x0000003F;
1222 switch (CONFIG_SYS_USB_BRGCLK
) {
1224 cp
->cp_brgc1
|= (divisor
| CPM_BRG_EN
);
1225 cp
->cp_sicr
&= ~0x2F;
1228 cp
->cp_brgc2
|= (divisor
| CPM_BRG_EN
);
1229 cp
->cp_sicr
|= 0x00000009;
1232 cp
->cp_brgc3
|= (divisor
| CPM_BRG_EN
);
1233 cp
->cp_sicr
|= 0x00000012;
1236 cp
->cp_brgc4
= (divisor
| CPM_BRG_EN
);
1237 cp
->cp_sicr
|= 0x0000001B;
1240 udc_state
= STATE_ERROR
;
1245 #error "CONFIG_SYS_USB_EXTC_CLK or CONFIG_SYS_USB_BRGCLK must be defined"
1250 /* mpc8xx_udc_cbd_attach
1252 * attach a cbd to and endpoint
1254 static void mpc8xx_udc_cbd_attach (int ep
, uchar tx_size
, uchar rx_size
)
1257 if (!tx_cbd
[ep
] || !rx_cbd
[ep
] || ep
>= MAX_ENDPOINTS
) {
1258 udc_state
= STATE_ERROR
;
1262 if (tx_size
> USB_MAX_PKT
|| rx_size
> USB_MAX_PKT
||
1263 (!tx_size
&& !rx_size
)) {
1264 udc_state
= STATE_ERROR
;
1268 /* Attach CBD to appropiate Parameter RAM Endpoint data structure */
1270 endpoints
[ep
]->rbase
= (u32
) rx_cbd
[rx_ct
];
1271 endpoints
[ep
]->rbptr
= (u32
) rx_cbd
[rx_ct
];
1276 endpoints
[ep
]->rbptr
= (u32
) rx_cbd
[rx_ct
];
1277 rx_cbd
[rx_ct
]->cbd_sc
|= RX_BD_W
;
1282 endpoints
[ep
]->rbptr
= (u32
) rx_cbd
[rx_ct
];
1283 rx_cbd
[rx_ct
]->cbd_sc
|= RX_BD_W
;
1287 /* Where we expect to RX data on this endpoint */
1288 ep_ref
[ep
].prx
= rx_cbd
[rx_ct
- 1];
1292 endpoints
[ep
]->rbase
= 0;
1293 endpoints
[ep
]->rbptr
= 0;
1297 endpoints
[ep
]->tbase
= (u32
) tx_cbd
[tx_ct
];
1298 endpoints
[ep
]->tbptr
= (u32
) tx_cbd
[tx_ct
];
1301 endpoints
[ep
]->tbase
= 0;
1302 endpoints
[ep
]->tbptr
= 0;
1305 endpoints
[ep
]->tstate
= 0;
1306 endpoints
[ep
]->tbcnt
= 0;
1307 endpoints
[ep
]->mrblr
= EP_MAX_PKT
;
1308 endpoints
[ep
]->rfcr
= 0x18;
1309 endpoints
[ep
]->tfcr
= 0x18;
1310 ep_ref
[ep
].sc
|= EP_ATTACHED
;
1312 DBG ("ep %d rbase 0x%08x rbptr 0x%08x tbase 0x%08x tbptr 0x%08x prx = %p\n",
1313 ep
, endpoints
[ep
]->rbase
, endpoints
[ep
]->rbptr
,
1314 endpoints
[ep
]->tbase
, endpoints
[ep
]->tbptr
,
1320 /* mpc8xx_udc_cbd_init
1322 * Allocate space for a cbd and allocate TX/RX data space
1324 static void mpc8xx_udc_cbd_init (void)
1328 for (; i
< TX_RING_SIZE
; i
++) {
1329 tx_cbd
[i
] = (cbd_t
*)
1330 mpc8xx_udc_alloc (sizeof (cbd_t
), sizeof (int));
1333 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1334 rx_cbd
[i
] = (cbd_t
*)
1335 mpc8xx_udc_alloc (sizeof (cbd_t
), sizeof (int));
1338 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
1339 tx_cbd
[i
]->cbd_bufaddr
=
1340 mpc8xx_udc_alloc (EP_MAX_PKT
, sizeof (int));
1342 tx_cbd
[i
]->cbd_sc
= (TX_BD_I
| TX_BD_W
);
1343 tx_cbd
[i
]->cbd_datlen
= 0x0000;
1347 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1348 rx_cbd
[i
]->cbd_bufaddr
=
1349 mpc8xx_udc_alloc (EP_MAX_PKT
, sizeof (int));
1350 rx_cbd
[i
]->cbd_sc
= (RX_BD_I
| RX_BD_E
);
1351 rx_cbd
[i
]->cbd_datlen
= 0x0000;
1358 /* mpc8xx_udc_endpoint_init
1360 * Attach an endpoint to some dpram
1362 static void mpc8xx_udc_endpoint_init (void)
1366 for (; i
< MAX_ENDPOINTS
; i
++) {
1367 endpoints
[i
] = (usb_epb_t
*)
1368 mpc8xx_udc_alloc (sizeof (usb_epb_t
), 32);
1374 * Grab the address of some dpram
1376 static u32
mpc8xx_udc_alloc (u32 data_size
, u32 alignment
)
1378 u32 retaddr
= address_base
;
1380 while (retaddr
% alignment
) {
1383 address_base
+= data_size
;