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>
53 DECLARE_GLOBAL_DATA_PTR;
55 #define ERR(fmt, args...)\
56 serial_printf("ERROR : [%s] %s:%d: "fmt,\
57 __FILE__,__FUNCTION__,__LINE__, ##args)
59 #define DBG(fmt,args...)\
60 serial_printf("[%s] %s:%d: "fmt,\
61 __FILE__,__FUNCTION__,__LINE__, ##args)
63 #define DBG(fmt,args...)
67 #ifdef __SIMULATE_ERROR__
68 static char err_poison_test = 0;
70 static struct mpc8xx_ep ep_ref[MAX_ENDPOINTS];
71 static u32 address_base = STATE_NOT_READY;
72 static mpc8xx_udc_state_t udc_state = 0;
73 static struct usb_device_instance *udc_device = 0;
74 static volatile usb_epb_t *endpoints[MAX_ENDPOINTS];
75 static volatile cbd_t *tx_cbd[TX_RING_SIZE];
76 static volatile cbd_t *rx_cbd[RX_RING_SIZE];
77 static volatile immap_t *immr = 0;
78 static volatile cpm8xx_t *cp = 0;
79 static volatile usb_pram_t *usb_paramp = 0;
80 static volatile usb_t *usbp = 0;
84 /* Static Function Declarations */
85 static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
86 usb_device_state_t final);
87 static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
88 usb_device_state_t final);
89 static void mpc8xx_udc_stall (unsigned int ep);
90 static void mpc8xx_udc_flush_tx_fifo (int epid);
91 static void mpc8xx_udc_flush_rx_fifo (void);
92 static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp);
93 static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
95 static void mpc8xx_udc_dump_request (struct usb_device_request *request);
96 static void mpc8xx_udc_clock_init (volatile immap_t * immr,
97 volatile cpm8xx_t * cp);
98 static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi);
99 static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp);
100 static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp);
101 static void mpc8xx_udc_cbd_init (void);
102 static void mpc8xx_udc_endpoint_init (void);
103 static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size);
104 static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment);
105 static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp);
106 static void mpc8xx_udc_set_nak (unsigned int ep);
107 static short mpc8xx_udc_handle_txerr (void);
108 static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid);
110 /******************************************************************************
112 *****************************************************************************/
116 * Do initial bus gluing
120 /* Init various pointers */
121 immr = (immap_t *) CONFIG_SYS_IMMR;
122 cp = (cpm8xx_t *) & (immr->im_cpm);
123 usb_paramp = (usb_pram_t *) & (cp->cp_dparam[PROFF_USB]);
124 usbp = (usb_t *) & (cp->cp_scc[0]);
126 memset (ep_ref, 0x00, (sizeof (struct mpc8xx_ep) * MAX_ENDPOINTS));
129 udc_state = STATE_NOT_READY;
134 /* Set USB Frame #0, Respond at Address & Get a clock source */
136 mpc8xx_udc_clock_init (immr, cp);
138 /* PA15, PA14 as perhiperal USBRXD and USBOE */
139 immr->im_ioport.iop_padir &= ~0x0003;
140 immr->im_ioport.iop_papar |= 0x0003;
142 /* PC11/PC10 as peripheral USBRXP USBRXN */
143 immr->im_ioport.iop_pcso |= 0x0030;
145 /* PC7/PC6 as perhiperal USBTXP and USBTXN */
146 immr->im_ioport.iop_pcdir |= 0x0300;
147 immr->im_ioport.iop_pcpar |= 0x0300;
149 /* Set the base address */
150 address_base = (u32) (cp->cp_dpmem + CPM_USB_BASE);
152 /* Initialise endpoints and circular buffers */
153 mpc8xx_udc_endpoint_init ();
154 mpc8xx_udc_cbd_init ();
156 /* Assign allocated Dual Port Endpoint descriptors */
157 usb_paramp->ep0ptr = (u32) endpoints[0];
158 usb_paramp->ep1ptr = (u32) endpoints[1];
159 usb_paramp->ep2ptr = (u32) endpoints[2];
160 usb_paramp->ep3ptr = (u32) endpoints[3];
161 usb_paramp->frame_n = 0;
163 DBG ("ep0ptr=0x%08x ep1ptr=0x%08x ep2ptr=0x%08x ep3ptr=0x%08x\n",
164 usb_paramp->ep0ptr, usb_paramp->ep1ptr, usb_paramp->ep2ptr,
172 * Poll for whatever events may have occured
177 volatile cbd_t *rx_cbdp = 0;
178 volatile cbd_t *rx_cbdp_base = 0;
180 if (udc_state != STATE_READY) {
184 if (usbp->usber & USB_E_BSY) {
185 /* This shouldn't happen. If it does then it's a bug ! */
186 usbp->usber |= USB_E_BSY;
187 mpc8xx_udc_flush_rx_fifo ();
190 /* Scan all RX/Bidirectional Endpoints for RX data. */
191 for (epid = 0; epid < MAX_ENDPOINTS; epid++) {
192 if (!ep_ref[epid].prx) {
195 rx_cbdp = rx_cbdp_base = ep_ref[epid].prx;
198 if (!(rx_cbdp->cbd_sc & RX_BD_E)) {
200 if (rx_cbdp->cbd_sc & 0x1F) {
201 /* Corrupt data discard it.
202 * Controller has NAK'd this packet.
204 mpc8xx_udc_clear_rxbd (rx_cbdp);
208 mpc8xx_udc_ep0_rx (rx_cbdp);
212 mpc8xx_udc_set_nak (epid);
213 mpc8xx_udc_epn_rx (epid, rx_cbdp);
214 mpc8xx_udc_clear_rxbd (rx_cbdp);
218 /* Advance RX CBD pointer */
219 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
220 ep_ref[epid].prx = rx_cbdp;
222 /* Advance RX CBD pointer */
223 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
226 } while (rx_cbdp != rx_cbdp_base);
229 /* Handle TX events as appropiate, the correct place to do this is
230 * in a tx routine. Perhaps TX on epn was pre-empted by ep0
233 if (usbp->usber & USB_E_TXB) {
234 usbp->usber |= USB_E_TXB;
237 if (usbp->usber & (USB_TX_ERRMASK)) {
238 mpc8xx_udc_handle_txerr ();
241 /* Switch to the default state, respond at the default address */
242 if (usbp->usber & USB_E_RESET) {
243 usbp->usber |= USB_E_RESET;
245 udc_device->device_state = STATE_DEFAULT;
248 /* if(usbp->usber&USB_E_IDLE){
249 We could suspend here !
250 usbp->usber|=USB_E_IDLE;
251 DBG("idle state change\n");
254 We could resume here when IDLE is deasserted !
255 Not worth doing, so long as we are self powered though.
262 /* udc_endpoint_write
264 * Write some data to an endpoint
266 int udc_endpoint_write (struct usb_endpoint_instance *epi)
269 short epid = 1, unnak = 0, ret = 0;
271 if (udc_state != STATE_READY) {
272 ERR ("invalid udc_state != STATE_READY!\n");
276 if (!udc_device || !epi) {
280 if (udc_device->device_state != STATE_CONFIGURED) {
284 ep = epi->endpoint_address & 0x03;
285 if (ep >= MAX_ENDPOINTS) {
289 /* Set NAK for all RX endpoints during TX */
290 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
292 /* Don't set NAK on DATA IN/CONTROL endpoints */
293 if (ep_ref[epid].sc & USB_DIR_IN) {
297 if (!(usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK))) {
301 mpc8xx_udc_set_nak (epid);
304 mpc8xx_udc_init_tx (&udc_device->bus->endpoint_array[ep],
306 ret = mpc8xx_udc_ep_tx (&udc_device->bus->endpoint_array[ep]);
308 /* Remove temporary NAK */
309 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
310 if (unnak & (1 << epid)) {
311 udc_unset_nak (epid);
318 /* mpc8xx_udc_assign_urb
320 * Associate a given urb to an endpoint TX or RX transmit/receive buffers
322 static int mpc8xx_udc_assign_urb (int ep, char direction)
324 struct usb_endpoint_instance *epi = 0;
326 if (ep >= MAX_ENDPOINTS) {
329 epi = &udc_device->bus->endpoint_array[ep];
334 if (!ep_ref[ep].urb) {
335 ep_ref[ep].urb = usbd_alloc_urb (udc_device, udc_device->bus->endpoint_array);
336 if (!ep_ref[ep].urb) {
340 ep_ref[ep].urb->actual_length = 0;
345 epi->tx_urb = ep_ref[ep].urb;
348 epi->rcv_urb = ep_ref[ep].urb;
356 udc_state = STATE_ERROR;
362 * Associate U-Boot software endpoints to mpc8xx endpoint parameter ram
363 * Isochronous endpoints aren't yet supported!
365 void udc_setup_ep (struct usb_device_instance *device, unsigned int ep,
366 struct usb_endpoint_instance *epi)
371 if (epi && (ep < MAX_ENDPOINTS)) {
374 if (epi->rcv_attributes != USB_ENDPOINT_XFER_CONTROL
375 || epi->tx_attributes !=
376 USB_ENDPOINT_XFER_CONTROL) {
378 /* ep0 must be a control endpoint */
379 udc_state = STATE_ERROR;
383 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
384 mpc8xx_udc_cbd_attach (ep, epi->tx_packetSize,
385 epi->rcv_packetSize);
387 usbp->usep[ep] = 0x0000;
391 if ((epi->endpoint_address & USB_ENDPOINT_DIR_MASK)
395 ep_attrib = epi->tx_attributes;
396 epi->rcv_packetSize = 0;
397 ep_ref[ep].sc |= USB_DIR_IN;
401 ep_attrib = epi->rcv_attributes;
402 epi->tx_packetSize = 0;
403 ep_ref[ep].sc &= ~USB_DIR_IN;
406 if (mpc8xx_udc_assign_urb (ep, epi->endpoint_address
407 & USB_ENDPOINT_DIR_MASK)) {
412 case USB_ENDPOINT_XFER_CONTROL:
413 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
414 mpc8xx_udc_cbd_attach (ep,
416 epi->rcv_packetSize);
418 usbp->usep[ep] = ep << 12;
419 epi->rcv_urb = epi->tx_urb = ep_ref[ep].urb;
422 case USB_ENDPOINT_XFER_BULK:
423 case USB_ENDPOINT_XFER_INT:
424 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
426 mpc8xx_udc_cbd_attach (ep,
430 mpc8xx_udc_cbd_attach (ep,
432 epi->rcv_packetSize);
435 usbp->usep[ep] = (ep << 12) | ((ep_attrib) << 8);
438 case USB_ENDPOINT_XFER_ISOC:
440 serial_printf ("Error endpoint attrib %d>3\n", ep_attrib);
441 udc_state = STATE_ERROR;
450 * Move state, switch on the USB
452 void udc_connect (void)
454 /* Enable pull-up resistor on D+
455 * TODO: fit a pull-up resistor to drive SE0 for > 2.5us
458 if (udc_state != STATE_ERROR) {
459 udc_state = STATE_READY;
460 usbp->usmod |= USMOD_EN;
466 * Disconnect is not used but, is included for completeness
468 void udc_disconnect (void)
470 /* Disable pull-up resistor on D-
471 * TODO: fix a pullup resistor to control this
474 if (udc_state != STATE_ERROR) {
475 udc_state = STATE_NOT_READY;
477 usbp->usmod &= ~USMOD_EN;
482 * Grab an EP0 URB, register interest in a subset of USB events
484 void udc_enable (struct usb_device_instance *device)
486 if (udc_state == STATE_ERROR) {
492 if (!ep_ref[0].urb) {
493 ep_ref[0].urb = usbd_alloc_urb (device, device->bus->endpoint_array);
496 /* Register interest in all events except SOF, enable transceiver */
497 usbp->usber = 0x03FF;
498 usbp->usbmr = 0x02F7;
505 * disable the currently hooked device
507 void udc_disable (void)
511 if (udc_state == STATE_ERROR) {
512 DBG ("Won't disable UDC. udc_state==STATE_ERROR !\n");
518 for (; i < MAX_ENDPOINTS; i++) {
520 usbd_dealloc_urb (ep_ref[i].urb);
526 usbp->usmod = ~USMOD_EN;
527 udc_state = STATE_NOT_READY;
530 /* udc_startup_events
532 * Enable the specified device
534 void udc_startup_events (struct usb_device_instance *device)
537 if (udc_state == STATE_READY) {
538 usbd_device_event_irq (device, DEVICE_CREATE, 0);
544 * Allow upper layers to signal lower layers should not accept more RX data
547 void udc_set_nak (int epid)
550 mpc8xx_udc_set_nak (epid);
556 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
557 * Switch off NAKing on this endpoint to accept more data output from host.
560 void udc_unset_nak (int epid)
562 if (epid > MAX_ENDPOINTS) {
566 if (usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK)) {
567 usbp->usep[epid] &= ~(USEP_THS_NAK | USEP_RHS_NAK);
572 /******************************************************************************
574 ******************************************************************************/
576 /* udc_state_transition_up
577 * udc_state_transition_down
579 * Helper functions to implement device state changes. The device states and
580 * the events that transition between them are:
585 * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
591 * DEVICE_RESET DEVICE_POWER_INTERRUPTION
597 * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
603 * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
608 * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
609 * to STATE_CONFIGURED) from the specified initial state to the specified final
610 * state, passing through each intermediate state on the way. If the initial
611 * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
612 * no state transitions will take place.
614 * udc_state_transition_down transitions down (in the direction from
615 * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
616 * specified final state, passing through each intermediate state on the way.
617 * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
618 * state, then no state transitions will take place.
622 static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
623 usb_device_state_t final)
625 if (initial < final) {
628 usbd_device_event_irq (udc_device,
629 DEVICE_HUB_CONFIGURED, 0);
630 if (final == STATE_POWERED)
633 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
634 if (final == STATE_DEFAULT)
637 usbd_device_event_irq (udc_device,
638 DEVICE_ADDRESS_ASSIGNED, 0);
639 if (final == STATE_ADDRESSED)
641 case STATE_ADDRESSED:
642 usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
644 case STATE_CONFIGURED:
652 static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
653 usb_device_state_t final)
655 if (initial > final) {
657 case STATE_CONFIGURED:
658 usbd_device_event_irq (udc_device,
659 DEVICE_DE_CONFIGURED, 0);
660 if (final == STATE_ADDRESSED)
662 case STATE_ADDRESSED:
663 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
664 if (final == STATE_DEFAULT)
667 usbd_device_event_irq (udc_device,
668 DEVICE_POWER_INTERRUPTION, 0);
669 if (final == STATE_POWERED)
672 usbd_device_event_irq (udc_device, DEVICE_HUB_RESET,
684 * Force returning of STALL tokens on the given endpoint. Protocol or function
685 * STALL conditions are permissable here
687 static void mpc8xx_udc_stall (unsigned int ep)
689 usbp->usep[ep] |= STALL_BITMASK;
692 /* mpc8xx_udc_set_nak
694 * Force returning of NAK responses for the given endpoint as a kind of very
695 * simple flow control
697 static void mpc8xx_udc_set_nak (unsigned int ep)
699 usbp->usep[ep] |= NAK_BITMASK;
703 /* mpc8xx_udc_handle_txerr
705 * Handle errors relevant to TX. Return a status code to allow calling
706 * indicative of what if anything happened
708 static short mpc8xx_udc_handle_txerr ()
710 short ep = 0, ret = 0;
712 for (; ep < TX_RING_SIZE; ep++) {
713 if (usbp->usber & (0x10 << ep)) {
715 /* Timeout or underrun */
716 if (tx_cbd[ep]->cbd_sc & 0x06) {
718 mpc8xx_udc_flush_tx_fifo (ep);
721 if (usbp->usep[ep] & STALL_BITMASK) {
723 usbp->usep[ep] &= ~STALL_BITMASK;
727 usbp->usber |= (0x10 << ep);
733 /* mpc8xx_udc_advance_rx
737 static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid)
739 if ((*rx_cbdp)->cbd_sc & RX_BD_W) {
740 *rx_cbdp = (volatile cbd_t *) (endpoints[epid]->rbase + CONFIG_SYS_IMMR);
748 /* mpc8xx_udc_flush_tx_fifo
750 * Flush a given TX fifo. Assumes one tx cbd per endpoint
752 static void mpc8xx_udc_flush_tx_fifo (int epid)
754 volatile cbd_t *tx_cbdp = 0;
756 if (epid > MAX_ENDPOINTS) {
761 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x1D01);
763 while (immr->im_cpm.cp_cpcr & 0x01);
765 usbp->uscom = 0x40 | 0;
768 tx_cbdp = (cbd_t *) (endpoints[epid]->tbptr + CONFIG_SYS_IMMR);
769 tx_cbdp->cbd_sc = (TX_BD_I | TX_BD_W);
772 endpoints[epid]->tptr = endpoints[epid]->tbase;
773 endpoints[epid]->tstate = 0x00;
774 endpoints[epid]->tbcnt = 0x00;
777 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x2D01);
779 while (immr->im_cpm.cp_cpcr & 0x01);
784 /* mpc8xx_udc_flush_rx_fifo
786 * For the sake of completeness of the namespace, it seems like
787 * a good-design-decision (tm) to include mpc8xx_udc_flush_rx_fifo();
788 * If RX_BD_E is true => a driver bug either here or in an upper layer
789 * not polling frequently enough. If RX_BD_E is true we have told the host
790 * we have accepted data but, the CPM found it had no-where to put that data
791 * which needless to say would be a bad thing.
793 static void mpc8xx_udc_flush_rx_fifo ()
797 for (i = 0; i < RX_RING_SIZE; i++) {
798 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
799 ERR ("buf %p used rx data len = 0x%x sc=0x%x!\n",
800 rx_cbd[i], rx_cbd[i]->cbd_datlen,
805 ERR ("BUG : Input over-run\n");
808 /* mpc8xx_udc_clear_rxbd
810 * Release control of RX CBD to CP.
812 static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp)
814 rx_cbdp->cbd_datlen = 0x0000;
815 rx_cbdp->cbd_sc = ((rx_cbdp->cbd_sc & RX_BD_W) | (RX_BD_E | RX_BD_I));
821 * Parse for tx timeout, control RX or USB reset/busy conditions
822 * Return -1 on timeout, -2 on fatal error, else return zero
824 static int mpc8xx_udc_tx_irq (int ep)
828 if (usbp->usber & (USB_TX_ERRMASK)) {
829 if (mpc8xx_udc_handle_txerr ()) {
830 /* Timeout, controlling function must retry send */
835 if (usbp->usber & (USB_E_RESET | USB_E_BSY)) {
836 /* Fatal, abandon TX transaction */
840 if (usbp->usber & USB_E_RXB) {
841 for (i = 0; i < RX_RING_SIZE; i++) {
842 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
843 if ((rx_cbd[i] == ep_ref[0].prx) || ep) {
855 * Transmit in a re-entrant fashion outbound USB packets.
856 * Implement retry/timeout mechanism described in USB specification
857 * Toggle DATA0/DATA1 pids as necessary
858 * Introduces non-standard tx_retry. The USB standard has no scope for slave
859 * devices to give up TX, however tx_retry stops us getting stuck in an endless
862 static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi)
864 struct urb *urb = epi->tx_urb;
865 volatile cbd_t *tx_cbdp = 0;
866 unsigned int ep = 0, pkt_len = 0, x = 0, tx_retry = 0;
869 if (!epi || (epi->endpoint_address & 0x03) >= MAX_ENDPOINTS || !urb) {
873 ep = epi->endpoint_address & 0x03;
874 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
876 if (tx_cbdp->cbd_sc & TX_BD_R || usbp->usber & USB_E_TXB) {
877 mpc8xx_udc_flush_tx_fifo (ep);
878 usbp->usber |= USB_E_TXB;
881 while (tx_retry++ < 100) {
882 ret = mpc8xx_udc_tx_irq (ep);
884 /* ignore timeout here */
885 } else if (ret == -2) {
887 mpc8xx_udc_flush_tx_fifo (ep);
891 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
892 while (tx_cbdp->cbd_sc & TX_BD_R) {
894 tx_cbdp->cbd_sc = (tx_cbdp->cbd_sc & TX_BD_W);
896 pkt_len = urb->actual_length - epi->sent;
898 if (pkt_len > epi->tx_packetSize || pkt_len > EP_MAX_PKT) {
899 pkt_len = MIN (epi->tx_packetSize, EP_MAX_PKT);
902 for (x = 0; x < pkt_len; x++) {
903 *((unsigned char *) (tx_cbdp->cbd_bufaddr + x)) =
904 urb->buffer[epi->sent + x];
906 tx_cbdp->cbd_datlen = pkt_len;
907 tx_cbdp->cbd_sc |= (CBD_TX_BITMASK | ep_ref[ep].pid);
910 #ifdef __SIMULATE_ERROR__
911 if (++err_poison_test == 2) {
913 tx_cbdp->cbd_sc &= ~TX_BD_TC;
917 usbp->uscom = (USCOM_STR | ep);
919 while (!(usbp->usber & USB_E_TXB)) {
920 ret = mpc8xx_udc_tx_irq (ep);
924 } else if (ret == -2) {
925 if (usbp->usber & USB_E_TXB) {
926 usbp->usber |= USB_E_TXB;
928 mpc8xx_udc_flush_tx_fifo (ep);
933 if (usbp->usber & USB_E_TXB) {
934 usbp->usber |= USB_E_TXB;
937 /* ACK must be present <= 18bit times from TX */
942 /* TX ACK : USB 2.0 8.7.2, Toggle PID, Advance TX */
943 epi->sent += pkt_len;
944 epi->last = MIN (urb->actual_length - epi->sent, epi->tx_packetSize);
945 TOGGLE_TX_PID (ep_ref[ep].pid);
947 if (epi->sent >= epi->tx_urb->actual_length) {
949 epi->tx_urb->actual_length = 0;
952 if (ep_ref[ep].sc & EP_SEND_ZLP) {
953 ep_ref[ep].sc &= ~EP_SEND_ZLP;
960 ERR ("TX fail, endpoint 0x%x tx bytes 0x%x/0x%x\n", ep, epi->sent,
961 epi->tx_urb->actual_length);
966 /* mpc8xx_udc_dump_request
968 * Dump a control request to console
970 static void mpc8xx_udc_dump_request (struct usb_device_request *request)
972 DBG ("bmRequestType:%02x bRequest:%02x wValue:%04x "
973 "wIndex:%04x wLength:%04x ?\n",
974 request->bmRequestType,
976 request->wValue, request->wIndex, request->wLength);
981 /* mpc8xx_udc_ep0_rx_setup
983 * Decode received ep0 SETUP packet. return non-zero on error
985 static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp)
988 struct urb *purb = ep_ref[0].urb;
989 struct usb_endpoint_instance *epi =
990 &udc_device->bus->endpoint_array[0];
992 for (; x < rx_cbdp->cbd_datlen; x++) {
993 *(((unsigned char *) &ep_ref[0].urb->device_request) + x) =
994 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
997 mpc8xx_udc_clear_rxbd (rx_cbdp);
999 if (ep0_recv_setup (purb)) {
1000 mpc8xx_udc_dump_request (&purb->device_request);
1004 if ((purb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
1005 == USB_REQ_HOST2DEVICE) {
1007 switch (purb->device_request.bRequest) {
1008 case USB_REQ_SET_ADDRESS:
1009 /* Send the Status OUT ZLP */
1010 ep_ref[0].pid = TX_BD_PID_DATA1;
1011 purb->actual_length = 0;
1012 mpc8xx_udc_init_tx (epi, purb);
1013 mpc8xx_udc_ep_tx (epi);
1015 /* Move to the addressed state */
1016 usbp->usaddr = udc_device->address;
1017 mpc8xx_udc_state_transition_up (udc_device->device_state,
1021 case USB_REQ_SET_CONFIGURATION:
1022 if (!purb->device_request.wValue) {
1023 /* Respond at default address */
1024 usbp->usaddr = 0x00;
1025 mpc8xx_udc_state_transition_down (udc_device->device_state,
1028 /* TODO: Support multiple configurations */
1029 mpc8xx_udc_state_transition_up (udc_device->device_state,
1031 for (x = 1; x < MAX_ENDPOINTS; x++) {
1032 if ((udc_device->bus->endpoint_array[x].endpoint_address & USB_ENDPOINT_DIR_MASK)
1034 ep_ref[x].pid = TX_BD_PID_DATA0;
1036 ep_ref[x].pid = RX_BD_PID_DATA0;
1038 /* Set configuration must unstall endpoints */
1039 usbp->usep[x] &= ~STALL_BITMASK;
1044 /* CDC/Vendor specific */
1048 /* Send ZLP as ACK in Status OUT phase */
1049 ep_ref[0].pid = TX_BD_PID_DATA1;
1050 purb->actual_length = 0;
1051 mpc8xx_udc_init_tx (epi, purb);
1052 mpc8xx_udc_ep_tx (epi);
1056 if (purb->actual_length) {
1057 ep_ref[0].pid = TX_BD_PID_DATA1;
1058 mpc8xx_udc_init_tx (epi, purb);
1060 if (!(purb->actual_length % EP0_MAX_PACKET_SIZE)) {
1061 ep_ref[0].sc |= EP_SEND_ZLP;
1064 if (purb->device_request.wValue ==
1065 USB_DESCRIPTOR_TYPE_DEVICE) {
1066 if (le16_to_cpu (purb->device_request.wLength)
1067 > purb->actual_length) {
1068 /* Send EP0_MAX_PACKET_SIZE bytes
1069 * unless correct size requested.
1071 if (purb->actual_length > epi->tx_packetSize) {
1072 purb->actual_length = epi->tx_packetSize;
1076 mpc8xx_udc_ep_tx (epi);
1079 /* Corrupt SETUP packet? */
1080 ERR ("Zero length data or SETUP with DATA-IN phase ?\n");
1087 /* mpc8xx_udc_init_tx
1089 * Setup some basic parameters for a TX transaction
1091 static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
1096 epi->tx_urb = tx_urb;
1099 /* mpc8xx_udc_ep0_rx
1101 * Receive ep0/control USB data. Parse and possibly send a response.
1103 static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp)
1105 if (rx_cbdp->cbd_sc & RX_BD_PID_SETUP) {
1107 /* Unconditionally accept SETUP packets */
1108 if (mpc8xx_udc_ep0_rx_setup (rx_cbdp)) {
1109 mpc8xx_udc_stall (0);
1114 mpc8xx_udc_clear_rxbd (rx_cbdp);
1116 if ((rx_cbdp->cbd_datlen - 2)) {
1117 /* SETUP with a DATA phase
1118 * outside of SETUP packet.
1121 mpc8xx_udc_stall (0);
1126 /* mpc8xx_udc_epn_rx
1128 * Receive some data from cbd into USB system urb data abstraction
1129 * Upper layers should NAK if there is insufficient RX data space
1131 static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp)
1133 struct usb_endpoint_instance *epi = 0;
1134 struct urb *urb = 0;
1137 if (epid >= MAX_ENDPOINTS || !rx_cbdp->cbd_datlen) {
1141 /* USB 2.0 PDF section 8.6.4
1142 * Discard data with invalid PID it is a resend.
1144 if (ep_ref[epid].pid != (rx_cbdp->cbd_sc & 0xC0)) {
1147 TOGGLE_RX_PID (ep_ref[epid].pid);
1149 epi = &udc_device->bus->endpoint_array[epid];
1152 for (; x < (rx_cbdp->cbd_datlen - 2); x++) {
1153 *((unsigned char *) (urb->buffer + urb->actual_length + x)) =
1154 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
1158 usbd_rcv_complete (epi, x, 0);
1159 if (ep_ref[epid].urb->status == RECV_ERROR) {
1160 DBG ("RX error unset NAK\n");
1161 udc_unset_nak (epid);
1167 /* mpc8xx_udc_clock_init
1169 * Obtain a clock reference for Full Speed Signaling
1171 static void mpc8xx_udc_clock_init (volatile immap_t * immr,
1172 volatile cpm8xx_t * cp)
1175 #if defined(CONFIG_SYS_USB_EXTC_CLK)
1177 /* This has been tested with a 48MHz crystal on CLK6 */
1178 switch (CONFIG_SYS_USB_EXTC_CLK) {
1180 immr->im_ioport.iop_papar |= 0x0100;
1181 immr->im_ioport.iop_padir &= ~0x0100;
1182 cp->cp_sicr |= 0x24;
1185 immr->im_ioport.iop_papar |= 0x0200;
1186 immr->im_ioport.iop_padir &= ~0x0200;
1187 cp->cp_sicr |= 0x2D;
1190 immr->im_ioport.iop_papar |= 0x0400;
1191 immr->im_ioport.iop_padir &= ~0x0400;
1192 cp->cp_sicr |= 0x36;
1195 immr->im_ioport.iop_papar |= 0x0800;
1196 immr->im_ioport.iop_padir &= ~0x0800;
1197 cp->cp_sicr |= 0x3F;
1200 udc_state = STATE_ERROR;
1204 #elif defined(CONFIG_SYS_USB_BRGCLK)
1206 /* This has been tested with brgclk == 50MHz */
1209 if (gd->cpu_clk < 48000000L) {
1210 ERR ("brgclk is too slow for full-speed USB!\n");
1211 udc_state = STATE_ERROR;
1215 /* Assume the brgclk is 'good enough', we want !(gd->cpu_clk%48MHz)
1216 * but, can /probably/ live with close-ish alternative rates.
1218 divisor = (gd->cpu_clk / 48000000L) - 1;
1219 cp->cp_sicr &= ~0x0000003F;
1221 switch (CONFIG_SYS_USB_BRGCLK) {
1223 cp->cp_brgc1 |= (divisor | CPM_BRG_EN);
1224 cp->cp_sicr &= ~0x2F;
1227 cp->cp_brgc2 |= (divisor | CPM_BRG_EN);
1228 cp->cp_sicr |= 0x00000009;
1231 cp->cp_brgc3 |= (divisor | CPM_BRG_EN);
1232 cp->cp_sicr |= 0x00000012;
1235 cp->cp_brgc4 = (divisor | CPM_BRG_EN);
1236 cp->cp_sicr |= 0x0000001B;
1239 udc_state = STATE_ERROR;
1244 #error "CONFIG_SYS_USB_EXTC_CLK or CONFIG_SYS_USB_BRGCLK must be defined"
1249 /* mpc8xx_udc_cbd_attach
1251 * attach a cbd to and endpoint
1253 static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size)
1256 if (!tx_cbd[ep] || !rx_cbd[ep] || ep >= MAX_ENDPOINTS) {
1257 udc_state = STATE_ERROR;
1261 if (tx_size > USB_MAX_PKT || rx_size > USB_MAX_PKT ||
1262 (!tx_size && !rx_size)) {
1263 udc_state = STATE_ERROR;
1267 /* Attach CBD to appropiate Parameter RAM Endpoint data structure */
1269 endpoints[ep]->rbase = (u32) rx_cbd[rx_ct];
1270 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
1275 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
1276 rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
1281 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
1282 rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
1286 /* Where we expect to RX data on this endpoint */
1287 ep_ref[ep].prx = rx_cbd[rx_ct - 1];
1291 endpoints[ep]->rbase = 0;
1292 endpoints[ep]->rbptr = 0;
1296 endpoints[ep]->tbase = (u32) tx_cbd[tx_ct];
1297 endpoints[ep]->tbptr = (u32) tx_cbd[tx_ct];
1300 endpoints[ep]->tbase = 0;
1301 endpoints[ep]->tbptr = 0;
1304 endpoints[ep]->tstate = 0;
1305 endpoints[ep]->tbcnt = 0;
1306 endpoints[ep]->mrblr = EP_MAX_PKT;
1307 endpoints[ep]->rfcr = 0x18;
1308 endpoints[ep]->tfcr = 0x18;
1309 ep_ref[ep].sc |= EP_ATTACHED;
1311 DBG ("ep %d rbase 0x%08x rbptr 0x%08x tbase 0x%08x tbptr 0x%08x prx = %p\n",
1312 ep, endpoints[ep]->rbase, endpoints[ep]->rbptr,
1313 endpoints[ep]->tbase, endpoints[ep]->tbptr,
1319 /* mpc8xx_udc_cbd_init
1321 * Allocate space for a cbd and allocate TX/RX data space
1323 static void mpc8xx_udc_cbd_init (void)
1327 for (; i < TX_RING_SIZE; i++) {
1328 tx_cbd[i] = (cbd_t *)
1329 mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int));
1332 for (i = 0; i < RX_RING_SIZE; i++) {
1333 rx_cbd[i] = (cbd_t *)
1334 mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int));
1337 for (i = 0; i < TX_RING_SIZE; i++) {
1338 tx_cbd[i]->cbd_bufaddr =
1339 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
1341 tx_cbd[i]->cbd_sc = (TX_BD_I | TX_BD_W);
1342 tx_cbd[i]->cbd_datlen = 0x0000;
1346 for (i = 0; i < RX_RING_SIZE; i++) {
1347 rx_cbd[i]->cbd_bufaddr =
1348 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
1349 rx_cbd[i]->cbd_sc = (RX_BD_I | RX_BD_E);
1350 rx_cbd[i]->cbd_datlen = 0x0000;
1357 /* mpc8xx_udc_endpoint_init
1359 * Attach an endpoint to some dpram
1361 static void mpc8xx_udc_endpoint_init (void)
1365 for (; i < MAX_ENDPOINTS; i++) {
1366 endpoints[i] = (usb_epb_t *)
1367 mpc8xx_udc_alloc (sizeof (usb_epb_t), 32);
1373 * Grab the address of some dpram
1375 static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment)
1377 u32 retaddr = address_base;
1379 while (retaddr % alignment) {
1382 address_base += data_size;