2 * Copyright (C) 2006 by Bryan O'Donoghue, CodeHermit
3 * bodonoghue@CodeHermit.ie
6 * DasUBoot/drivers/usb/usbdcore_omap1510.c, for design and implementation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the
21 * Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * 1. #define __SIMULATE_ERROR__ to inject a CRC error into every 2nd TX
29 * packet to force the USB re-transmit protocol.
31 * 2. #define __DEBUG_UDC__ to switch on debug tracing to serial console
32 * be careful that tracing doesn't create Hiesen-bugs with respect to
33 * response timeouts to control requests.
35 * 3. This driver should be able to support any higher level driver that
36 * that wants to do either of the two standard UDC implementations
37 * Control-Bulk-Interrupt or Bulk-IN/Bulk-Out standards. Hence
38 * gserial and cdc_acm should work with this code.
40 * 4. NAK events never actually get raised at all, the documentation
43 * 5. For some reason, cbd_datlen is *always* +2 the value it should be.
44 * this means that having an RX cbd of 16 bytes is not possible, since
45 * the same size is reported for 14 bytes received as 16 bytes received
46 * until we can find out why this happens, RX cbds must be limited to 8
47 * bytes. TODO: check errata for this behaviour.
49 * 6. Right now this code doesn't support properly powering up with the USB
50 * cable attached to the USB host my development board the Adder87x doesn't
51 * have a pull-up fitted to allow this, so it is necessary to power the
52 * board and *then* attached the USB cable to the host. However somebody
53 * with a different design in their board may be able to keep the cable
54 * constantly connected and simply enable/disable a pull-up re
55 * figure 31.1 in MPC885RM.pdf instead of having to power up the board and
56 * then attach the cable !
63 #include "usbdcore_mpc8xx.h"
64 #include "usbdcore_ep0.h"
66 DECLARE_GLOBAL_DATA_PTR;
68 #define ERR(fmt, args...)\
69 serial_printf("ERROR : [%s] %s:%d: "fmt,\
70 __FILE__,__FUNCTION__,__LINE__, ##args)
72 #define DBG(fmt,args...)\
73 serial_printf("[%s] %s:%d: "fmt,\
74 __FILE__,__FUNCTION__,__LINE__, ##args)
76 #define DBG(fmt,args...)
80 #ifdef __SIMULATE_ERROR__
81 static char err_poison_test = 0;
83 static struct mpc8xx_ep ep_ref[MAX_ENDPOINTS];
84 static u32 address_base = STATE_NOT_READY;
85 static mpc8xx_udc_state_t udc_state = 0;
86 static struct usb_device_instance *udc_device = 0;
87 static volatile usb_epb_t *endpoints[MAX_ENDPOINTS];
88 static volatile cbd_t *tx_cbd[TX_RING_SIZE];
89 static volatile cbd_t *rx_cbd[RX_RING_SIZE];
90 static volatile immap_t *immr = 0;
91 static volatile cpm8xx_t *cp = 0;
92 static volatile usb_pram_t *usb_paramp = 0;
93 static volatile usb_t *usbp = 0;
97 /* Static Function Declarations */
98 static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
99 usb_device_state_t final);
100 static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
101 usb_device_state_t final);
102 static void mpc8xx_udc_stall (unsigned int ep);
103 static void mpc8xx_udc_flush_tx_fifo (int epid);
104 static void mpc8xx_udc_flush_rx_fifo (void);
105 static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp);
106 static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
108 static void mpc8xx_udc_dump_request (struct usb_device_request *request);
109 static void mpc8xx_udc_clock_init (volatile immap_t * immr,
110 volatile cpm8xx_t * cp);
111 static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi);
112 static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp);
113 static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp);
114 static void mpc8xx_udc_cbd_init (void);
115 static void mpc8xx_udc_endpoint_init (void);
116 static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size);
117 static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment);
118 static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp);
119 static void mpc8xx_udc_set_nak (unsigned int ep);
120 static short mpc8xx_udc_handle_txerr (void);
121 static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid);
123 /******************************************************************************
125 *****************************************************************************/
129 * Do initial bus gluing
133 /* Init various pointers */
134 immr = (immap_t *) CONFIG_SYS_IMMR;
135 cp = (cpm8xx_t *) & (immr->im_cpm);
136 usb_paramp = (usb_pram_t *) & (cp->cp_dparam[PROFF_USB]);
137 usbp = (usb_t *) & (cp->cp_scc[0]);
139 memset (ep_ref, 0x00, (sizeof (struct mpc8xx_ep) * MAX_ENDPOINTS));
142 udc_state = STATE_NOT_READY;
147 /* Set USB Frame #0, Respond at Address & Get a clock source */
149 mpc8xx_udc_clock_init (immr, cp);
151 /* PA15, PA14 as perhiperal USBRXD and USBOE */
152 immr->im_ioport.iop_padir &= ~0x0003;
153 immr->im_ioport.iop_papar |= 0x0003;
155 /* PC11/PC10 as peripheral USBRXP USBRXN */
156 immr->im_ioport.iop_pcso |= 0x0030;
158 /* PC7/PC6 as perhiperal USBTXP and USBTXN */
159 immr->im_ioport.iop_pcdir |= 0x0300;
160 immr->im_ioport.iop_pcpar |= 0x0300;
162 /* Set the base address */
163 address_base = (u32) (cp->cp_dpmem + CPM_USB_BASE);
165 /* Initialise endpoints and circular buffers */
166 mpc8xx_udc_endpoint_init ();
167 mpc8xx_udc_cbd_init ();
169 /* Assign allocated Dual Port Endpoint descriptors */
170 usb_paramp->ep0ptr = (u32) endpoints[0];
171 usb_paramp->ep1ptr = (u32) endpoints[1];
172 usb_paramp->ep2ptr = (u32) endpoints[2];
173 usb_paramp->ep3ptr = (u32) endpoints[3];
174 usb_paramp->frame_n = 0;
176 DBG ("ep0ptr=0x%08x ep1ptr=0x%08x ep2ptr=0x%08x ep3ptr=0x%08x\n",
177 usb_paramp->ep0ptr, usb_paramp->ep1ptr, usb_paramp->ep2ptr,
185 * Poll for whatever events may have occured
190 volatile cbd_t *rx_cbdp = 0;
191 volatile cbd_t *rx_cbdp_base = 0;
193 if (udc_state != STATE_READY) {
197 if (usbp->usber & USB_E_BSY) {
198 /* This shouldn't happen. If it does then it's a bug ! */
199 usbp->usber |= USB_E_BSY;
200 mpc8xx_udc_flush_rx_fifo ();
203 /* Scan all RX/Bidirectional Endpoints for RX data. */
204 for (epid = 0; epid < MAX_ENDPOINTS; epid++) {
205 if (!ep_ref[epid].prx) {
208 rx_cbdp = rx_cbdp_base = ep_ref[epid].prx;
211 if (!(rx_cbdp->cbd_sc & RX_BD_E)) {
213 if (rx_cbdp->cbd_sc & 0x1F) {
214 /* Corrupt data discard it.
215 * Controller has NAK'd this packet.
217 mpc8xx_udc_clear_rxbd (rx_cbdp);
221 mpc8xx_udc_ep0_rx (rx_cbdp);
225 mpc8xx_udc_set_nak (epid);
226 mpc8xx_udc_epn_rx (epid, rx_cbdp);
227 mpc8xx_udc_clear_rxbd (rx_cbdp);
231 /* Advance RX CBD pointer */
232 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
233 ep_ref[epid].prx = rx_cbdp;
235 /* Advance RX CBD pointer */
236 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
239 } while (rx_cbdp != rx_cbdp_base);
242 /* Handle TX events as appropiate, the correct place to do this is
243 * in a tx routine. Perhaps TX on epn was pre-empted by ep0
246 if (usbp->usber & USB_E_TXB) {
247 usbp->usber |= USB_E_TXB;
250 if (usbp->usber & (USB_TX_ERRMASK)) {
251 mpc8xx_udc_handle_txerr ();
254 /* Switch to the default state, respond at the default address */
255 if (usbp->usber & USB_E_RESET) {
256 usbp->usber |= USB_E_RESET;
258 udc_device->device_state = STATE_DEFAULT;
261 /* if(usbp->usber&USB_E_IDLE){
262 We could suspend here !
263 usbp->usber|=USB_E_IDLE;
264 DBG("idle state change\n");
267 We could resume here when IDLE is deasserted !
268 Not worth doing, so long as we are self powered though.
275 /* udc_endpoint_write
277 * Write some data to an endpoint
279 int udc_endpoint_write (struct usb_endpoint_instance *epi)
282 short epid = 1, unnak = 0, ret = 0;
284 if (udc_state != STATE_READY) {
285 ERR ("invalid udc_state != STATE_READY!\n");
289 if (!udc_device || !epi) {
293 if (udc_device->device_state != STATE_CONFIGURED) {
297 ep = epi->endpoint_address & 0x03;
298 if (ep >= MAX_ENDPOINTS) {
302 /* Set NAK for all RX endpoints during TX */
303 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
305 /* Don't set NAK on DATA IN/CONTROL endpoints */
306 if (ep_ref[epid].sc & USB_DIR_IN) {
310 if (!(usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK))) {
314 mpc8xx_udc_set_nak (epid);
317 mpc8xx_udc_init_tx (&udc_device->bus->endpoint_array[ep],
319 ret = mpc8xx_udc_ep_tx (&udc_device->bus->endpoint_array[ep]);
321 /* Remove temporary NAK */
322 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
323 if (unnak & (1 << epid)) {
324 udc_unset_nak (epid);
331 /* mpc8xx_udc_assign_urb
333 * Associate a given urb to an endpoint TX or RX transmit/receive buffers
335 static int mpc8xx_udc_assign_urb (int ep, char direction)
337 struct usb_endpoint_instance *epi = 0;
339 if (ep >= MAX_ENDPOINTS) {
342 epi = &udc_device->bus->endpoint_array[ep];
347 if (!ep_ref[ep].urb) {
348 ep_ref[ep].urb = usbd_alloc_urb (udc_device, udc_device->bus->endpoint_array);
349 if (!ep_ref[ep].urb) {
353 ep_ref[ep].urb->actual_length = 0;
358 epi->tx_urb = ep_ref[ep].urb;
361 epi->rcv_urb = ep_ref[ep].urb;
369 udc_state = STATE_ERROR;
375 * Associate U-Boot software endpoints to mpc8xx endpoint parameter ram
376 * Isochronous endpoints aren't yet supported!
378 void udc_setup_ep (struct usb_device_instance *device, unsigned int ep,
379 struct usb_endpoint_instance *epi)
384 if (epi && (ep < MAX_ENDPOINTS)) {
387 if (epi->rcv_attributes != USB_ENDPOINT_XFER_CONTROL
388 || epi->tx_attributes !=
389 USB_ENDPOINT_XFER_CONTROL) {
391 /* ep0 must be a control endpoint */
392 udc_state = STATE_ERROR;
396 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
397 mpc8xx_udc_cbd_attach (ep, epi->tx_packetSize,
398 epi->rcv_packetSize);
400 usbp->usep[ep] = 0x0000;
404 if ((epi->endpoint_address & USB_ENDPOINT_DIR_MASK)
408 ep_attrib = epi->tx_attributes;
409 epi->rcv_packetSize = 0;
410 ep_ref[ep].sc |= USB_DIR_IN;
414 ep_attrib = epi->rcv_attributes;
415 epi->tx_packetSize = 0;
416 ep_ref[ep].sc &= ~USB_DIR_IN;
419 if (mpc8xx_udc_assign_urb (ep, epi->endpoint_address
420 & USB_ENDPOINT_DIR_MASK)) {
425 case USB_ENDPOINT_XFER_CONTROL:
426 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
427 mpc8xx_udc_cbd_attach (ep,
429 epi->rcv_packetSize);
431 usbp->usep[ep] = ep << 12;
432 epi->rcv_urb = epi->tx_urb = ep_ref[ep].urb;
435 case USB_ENDPOINT_XFER_BULK:
436 case USB_ENDPOINT_XFER_INT:
437 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
439 mpc8xx_udc_cbd_attach (ep,
443 mpc8xx_udc_cbd_attach (ep,
445 epi->rcv_packetSize);
448 usbp->usep[ep] = (ep << 12) | ((ep_attrib) << 8);
451 case USB_ENDPOINT_XFER_ISOC:
453 serial_printf ("Error endpoint attrib %d>3\n", ep_attrib);
454 udc_state = STATE_ERROR;
463 * Move state, switch on the USB
465 void udc_connect (void)
467 /* Enable pull-up resistor on D+
468 * TODO: fit a pull-up resistor to drive SE0 for > 2.5us
471 if (udc_state != STATE_ERROR) {
472 udc_state = STATE_READY;
473 usbp->usmod |= USMOD_EN;
479 * Disconnect is not used but, is included for completeness
481 void udc_disconnect (void)
483 /* Disable pull-up resistor on D-
484 * TODO: fix a pullup resistor to control this
487 if (udc_state != STATE_ERROR) {
488 udc_state = STATE_NOT_READY;
490 usbp->usmod &= ~USMOD_EN;
495 * Grab an EP0 URB, register interest in a subset of USB events
497 void udc_enable (struct usb_device_instance *device)
499 if (udc_state == STATE_ERROR) {
505 if (!ep_ref[0].urb) {
506 ep_ref[0].urb = usbd_alloc_urb (device, device->bus->endpoint_array);
509 /* Register interest in all events except SOF, enable transceiver */
510 usbp->usber = 0x03FF;
511 usbp->usbmr = 0x02F7;
518 * disable the currently hooked device
520 void udc_disable (void)
524 if (udc_state == STATE_ERROR) {
525 DBG ("Won't disable UDC. udc_state==STATE_ERROR !\n");
531 for (; i < MAX_ENDPOINTS; i++) {
533 usbd_dealloc_urb (ep_ref[i].urb);
539 usbp->usmod = ~USMOD_EN;
540 udc_state = STATE_NOT_READY;
543 /* udc_startup_events
545 * Enable the specified device
547 void udc_startup_events (struct usb_device_instance *device)
550 if (udc_state == STATE_READY) {
551 usbd_device_event_irq (device, DEVICE_CREATE, 0);
557 * Allow upper layers to signal lower layers should not accept more RX data
560 void udc_set_nak (int epid)
563 mpc8xx_udc_set_nak (epid);
569 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
570 * Switch off NAKing on this endpoint to accept more data output from host.
573 void udc_unset_nak (int epid)
575 if (epid > MAX_ENDPOINTS) {
579 if (usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK)) {
580 usbp->usep[epid] &= ~(USEP_THS_NAK | USEP_RHS_NAK);
585 /******************************************************************************
587 ******************************************************************************/
589 /* udc_state_transition_up
590 * udc_state_transition_down
592 * Helper functions to implement device state changes. The device states and
593 * the events that transition between them are:
598 * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
604 * DEVICE_RESET DEVICE_POWER_INTERRUPTION
610 * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
616 * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
621 * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
622 * to STATE_CONFIGURED) from the specified initial state to the specified final
623 * state, passing through each intermediate state on the way. If the initial
624 * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
625 * no state transitions will take place.
627 * udc_state_transition_down transitions down (in the direction from
628 * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
629 * specified final state, passing through each intermediate state on the way.
630 * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
631 * state, then no state transitions will take place.
635 static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
636 usb_device_state_t final)
638 if (initial < final) {
641 usbd_device_event_irq (udc_device,
642 DEVICE_HUB_CONFIGURED, 0);
643 if (final == STATE_POWERED)
646 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
647 if (final == STATE_DEFAULT)
650 usbd_device_event_irq (udc_device,
651 DEVICE_ADDRESS_ASSIGNED, 0);
652 if (final == STATE_ADDRESSED)
654 case STATE_ADDRESSED:
655 usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
657 case STATE_CONFIGURED:
665 static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
666 usb_device_state_t final)
668 if (initial > final) {
670 case STATE_CONFIGURED:
671 usbd_device_event_irq (udc_device,
672 DEVICE_DE_CONFIGURED, 0);
673 if (final == STATE_ADDRESSED)
675 case STATE_ADDRESSED:
676 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
677 if (final == STATE_DEFAULT)
680 usbd_device_event_irq (udc_device,
681 DEVICE_POWER_INTERRUPTION, 0);
682 if (final == STATE_POWERED)
685 usbd_device_event_irq (udc_device, DEVICE_HUB_RESET,
697 * Force returning of STALL tokens on the given endpoint. Protocol or function
698 * STALL conditions are permissable here
700 static void mpc8xx_udc_stall (unsigned int ep)
702 usbp->usep[ep] |= STALL_BITMASK;
705 /* mpc8xx_udc_set_nak
707 * Force returning of NAK responses for the given endpoint as a kind of very
708 * simple flow control
710 static void mpc8xx_udc_set_nak (unsigned int ep)
712 usbp->usep[ep] |= NAK_BITMASK;
716 /* mpc8xx_udc_handle_txerr
718 * Handle errors relevant to TX. Return a status code to allow calling
719 * indicative of what if anything happened
721 static short mpc8xx_udc_handle_txerr ()
723 short ep = 0, ret = 0;
725 for (; ep < TX_RING_SIZE; ep++) {
726 if (usbp->usber & (0x10 << ep)) {
728 /* Timeout or underrun */
729 if (tx_cbd[ep]->cbd_sc & 0x06) {
731 mpc8xx_udc_flush_tx_fifo (ep);
734 if (usbp->usep[ep] & STALL_BITMASK) {
736 usbp->usep[ep] &= ~STALL_BITMASK;
740 usbp->usber |= (0x10 << ep);
746 /* mpc8xx_udc_advance_rx
750 static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid)
752 if ((*rx_cbdp)->cbd_sc & RX_BD_W) {
753 *rx_cbdp = (volatile cbd_t *) (endpoints[epid]->rbase + CONFIG_SYS_IMMR);
761 /* mpc8xx_udc_flush_tx_fifo
763 * Flush a given TX fifo. Assumes one tx cbd per endpoint
765 static void mpc8xx_udc_flush_tx_fifo (int epid)
767 volatile cbd_t *tx_cbdp = 0;
769 if (epid > MAX_ENDPOINTS) {
774 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x1D01);
776 while (immr->im_cpm.cp_cpcr & 0x01);
778 usbp->uscom = 0x40 | 0;
781 tx_cbdp = (cbd_t *) (endpoints[epid]->tbptr + CONFIG_SYS_IMMR);
782 tx_cbdp->cbd_sc = (TX_BD_I | TX_BD_W);
785 endpoints[epid]->tptr = endpoints[epid]->tbase;
786 endpoints[epid]->tstate = 0x00;
787 endpoints[epid]->tbcnt = 0x00;
790 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x2D01);
792 while (immr->im_cpm.cp_cpcr & 0x01);
797 /* mpc8xx_udc_flush_rx_fifo
799 * For the sake of completeness of the namespace, it seems like
800 * a good-design-decision (tm) to include mpc8xx_udc_flush_rx_fifo();
801 * If RX_BD_E is true => a driver bug either here or in an upper layer
802 * not polling frequently enough. If RX_BD_E is true we have told the host
803 * we have accepted data but, the CPM found it had no-where to put that data
804 * which needless to say would be a bad thing.
806 static void mpc8xx_udc_flush_rx_fifo ()
810 for (i = 0; i < RX_RING_SIZE; i++) {
811 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
812 ERR ("buf %p used rx data len = 0x%x sc=0x%x!\n",
813 rx_cbd[i], rx_cbd[i]->cbd_datlen,
818 ERR ("BUG : Input over-run\n");
821 /* mpc8xx_udc_clear_rxbd
823 * Release control of RX CBD to CP.
825 static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp)
827 rx_cbdp->cbd_datlen = 0x0000;
828 rx_cbdp->cbd_sc = ((rx_cbdp->cbd_sc & RX_BD_W) | (RX_BD_E | RX_BD_I));
834 * Parse for tx timeout, control RX or USB reset/busy conditions
835 * Return -1 on timeout, -2 on fatal error, else return zero
837 static int mpc8xx_udc_tx_irq (int ep)
841 if (usbp->usber & (USB_TX_ERRMASK)) {
842 if (mpc8xx_udc_handle_txerr ()) {
843 /* Timeout, controlling function must retry send */
848 if (usbp->usber & (USB_E_RESET | USB_E_BSY)) {
849 /* Fatal, abandon TX transaction */
853 if (usbp->usber & USB_E_RXB) {
854 for (i = 0; i < RX_RING_SIZE; i++) {
855 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
856 if ((rx_cbd[i] == ep_ref[0].prx) || ep) {
868 * Transmit in a re-entrant fashion outbound USB packets.
869 * Implement retry/timeout mechanism described in USB specification
870 * Toggle DATA0/DATA1 pids as necessary
871 * Introduces non-standard tx_retry. The USB standard has no scope for slave
872 * devices to give up TX, however tx_retry stops us getting stuck in an endless
875 static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi)
877 struct urb *urb = epi->tx_urb;
878 volatile cbd_t *tx_cbdp = 0;
879 unsigned int ep = 0, pkt_len = 0, x = 0, tx_retry = 0;
882 if (!epi || (epi->endpoint_address & 0x03) >= MAX_ENDPOINTS || !urb) {
886 ep = epi->endpoint_address & 0x03;
887 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
889 if (tx_cbdp->cbd_sc & TX_BD_R || usbp->usber & USB_E_TXB) {
890 mpc8xx_udc_flush_tx_fifo (ep);
891 usbp->usber |= USB_E_TXB;
894 while (tx_retry++ < 100) {
895 ret = mpc8xx_udc_tx_irq (ep);
897 /* ignore timeout here */
898 } else if (ret == -2) {
900 mpc8xx_udc_flush_tx_fifo (ep);
904 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
905 while (tx_cbdp->cbd_sc & TX_BD_R) {
907 tx_cbdp->cbd_sc = (tx_cbdp->cbd_sc & TX_BD_W);
909 pkt_len = urb->actual_length - epi->sent;
911 if (pkt_len > epi->tx_packetSize || pkt_len > EP_MAX_PKT) {
912 pkt_len = MIN (epi->tx_packetSize, EP_MAX_PKT);
915 for (x = 0; x < pkt_len; x++) {
916 *((unsigned char *) (tx_cbdp->cbd_bufaddr + x)) =
917 urb->buffer[epi->sent + x];
919 tx_cbdp->cbd_datlen = pkt_len;
920 tx_cbdp->cbd_sc |= (CBD_TX_BITMASK | ep_ref[ep].pid);
923 #ifdef __SIMULATE_ERROR__
924 if (++err_poison_test == 2) {
926 tx_cbdp->cbd_sc &= ~TX_BD_TC;
930 usbp->uscom = (USCOM_STR | ep);
932 while (!(usbp->usber & USB_E_TXB)) {
933 ret = mpc8xx_udc_tx_irq (ep);
937 } else if (ret == -2) {
938 if (usbp->usber & USB_E_TXB) {
939 usbp->usber |= USB_E_TXB;
941 mpc8xx_udc_flush_tx_fifo (ep);
946 if (usbp->usber & USB_E_TXB) {
947 usbp->usber |= USB_E_TXB;
950 /* ACK must be present <= 18bit times from TX */
955 /* TX ACK : USB 2.0 8.7.2, Toggle PID, Advance TX */
956 epi->sent += pkt_len;
957 epi->last = MIN (urb->actual_length - epi->sent, epi->tx_packetSize);
958 TOGGLE_TX_PID (ep_ref[ep].pid);
960 if (epi->sent >= epi->tx_urb->actual_length) {
962 epi->tx_urb->actual_length = 0;
965 if (ep_ref[ep].sc & EP_SEND_ZLP) {
966 ep_ref[ep].sc &= ~EP_SEND_ZLP;
973 ERR ("TX fail, endpoint 0x%x tx bytes 0x%x/0x%x\n", ep, epi->sent,
974 epi->tx_urb->actual_length);
979 /* mpc8xx_udc_dump_request
981 * Dump a control request to console
983 static void mpc8xx_udc_dump_request (struct usb_device_request *request)
985 DBG ("bmRequestType:%02x bRequest:%02x wValue:%04x "
986 "wIndex:%04x wLength:%04x ?\n",
987 request->bmRequestType,
989 request->wValue, request->wIndex, request->wLength);
994 /* mpc8xx_udc_ep0_rx_setup
996 * Decode received ep0 SETUP packet. return non-zero on error
998 static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp)
1001 struct urb *purb = ep_ref[0].urb;
1002 struct usb_endpoint_instance *epi =
1003 &udc_device->bus->endpoint_array[0];
1005 for (; x < rx_cbdp->cbd_datlen; x++) {
1006 *(((unsigned char *) &ep_ref[0].urb->device_request) + x) =
1007 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
1010 mpc8xx_udc_clear_rxbd (rx_cbdp);
1012 if (ep0_recv_setup (purb)) {
1013 mpc8xx_udc_dump_request (&purb->device_request);
1017 if ((purb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
1018 == USB_REQ_HOST2DEVICE) {
1020 switch (purb->device_request.bRequest) {
1021 case USB_REQ_SET_ADDRESS:
1022 /* Send the Status OUT ZLP */
1023 ep_ref[0].pid = TX_BD_PID_DATA1;
1024 purb->actual_length = 0;
1025 mpc8xx_udc_init_tx (epi, purb);
1026 mpc8xx_udc_ep_tx (epi);
1028 /* Move to the addressed state */
1029 usbp->usaddr = udc_device->address;
1030 mpc8xx_udc_state_transition_up (udc_device->device_state,
1034 case USB_REQ_SET_CONFIGURATION:
1035 if (!purb->device_request.wValue) {
1036 /* Respond at default address */
1037 usbp->usaddr = 0x00;
1038 mpc8xx_udc_state_transition_down (udc_device->device_state,
1041 /* TODO: Support multiple configurations */
1042 mpc8xx_udc_state_transition_up (udc_device->device_state,
1044 for (x = 1; x < MAX_ENDPOINTS; x++) {
1045 if ((udc_device->bus->endpoint_array[x].endpoint_address & USB_ENDPOINT_DIR_MASK)
1047 ep_ref[x].pid = TX_BD_PID_DATA0;
1049 ep_ref[x].pid = RX_BD_PID_DATA0;
1051 /* Set configuration must unstall endpoints */
1052 usbp->usep[x] &= ~STALL_BITMASK;
1057 /* CDC/Vendor specific */
1061 /* Send ZLP as ACK in Status OUT phase */
1062 ep_ref[0].pid = TX_BD_PID_DATA1;
1063 purb->actual_length = 0;
1064 mpc8xx_udc_init_tx (epi, purb);
1065 mpc8xx_udc_ep_tx (epi);
1069 if (purb->actual_length) {
1070 ep_ref[0].pid = TX_BD_PID_DATA1;
1071 mpc8xx_udc_init_tx (epi, purb);
1073 if (!(purb->actual_length % EP0_MAX_PACKET_SIZE)) {
1074 ep_ref[0].sc |= EP_SEND_ZLP;
1077 if (purb->device_request.wValue ==
1078 USB_DESCRIPTOR_TYPE_DEVICE) {
1079 if (le16_to_cpu (purb->device_request.wLength)
1080 > purb->actual_length) {
1081 /* Send EP0_MAX_PACKET_SIZE bytes
1082 * unless correct size requested.
1084 if (purb->actual_length > epi->tx_packetSize) {
1085 purb->actual_length = epi->tx_packetSize;
1089 mpc8xx_udc_ep_tx (epi);
1092 /* Corrupt SETUP packet? */
1093 ERR ("Zero length data or SETUP with DATA-IN phase ?\n");
1100 /* mpc8xx_udc_init_tx
1102 * Setup some basic parameters for a TX transaction
1104 static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
1109 epi->tx_urb = tx_urb;
1112 /* mpc8xx_udc_ep0_rx
1114 * Receive ep0/control USB data. Parse and possibly send a response.
1116 static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp)
1118 if (rx_cbdp->cbd_sc & RX_BD_PID_SETUP) {
1120 /* Unconditionally accept SETUP packets */
1121 if (mpc8xx_udc_ep0_rx_setup (rx_cbdp)) {
1122 mpc8xx_udc_stall (0);
1127 mpc8xx_udc_clear_rxbd (rx_cbdp);
1129 if ((rx_cbdp->cbd_datlen - 2)) {
1130 /* SETUP with a DATA phase
1131 * outside of SETUP packet.
1134 mpc8xx_udc_stall (0);
1139 /* mpc8xx_udc_epn_rx
1141 * Receive some data from cbd into USB system urb data abstraction
1142 * Upper layers should NAK if there is insufficient RX data space
1144 static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp)
1146 struct usb_endpoint_instance *epi = 0;
1147 struct urb *urb = 0;
1150 if (epid >= MAX_ENDPOINTS || !rx_cbdp->cbd_datlen) {
1154 /* USB 2.0 PDF section 8.6.4
1155 * Discard data with invalid PID it is a resend.
1157 if (ep_ref[epid].pid != (rx_cbdp->cbd_sc & 0xC0)) {
1160 TOGGLE_RX_PID (ep_ref[epid].pid);
1162 epi = &udc_device->bus->endpoint_array[epid];
1165 for (; x < (rx_cbdp->cbd_datlen - 2); x++) {
1166 *((unsigned char *) (urb->buffer + urb->actual_length + x)) =
1167 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
1171 usbd_rcv_complete (epi, x, 0);
1172 if (ep_ref[epid].urb->status == RECV_ERROR) {
1173 DBG ("RX error unset NAK\n");
1174 udc_unset_nak (epid);
1180 /* mpc8xx_udc_clock_init
1182 * Obtain a clock reference for Full Speed Signaling
1184 static void mpc8xx_udc_clock_init (volatile immap_t * immr,
1185 volatile cpm8xx_t * cp)
1188 #if defined(CONFIG_SYS_USB_EXTC_CLK)
1190 /* This has been tested with a 48MHz crystal on CLK6 */
1191 switch (CONFIG_SYS_USB_EXTC_CLK) {
1193 immr->im_ioport.iop_papar |= 0x0100;
1194 immr->im_ioport.iop_padir &= ~0x0100;
1195 cp->cp_sicr |= 0x24;
1198 immr->im_ioport.iop_papar |= 0x0200;
1199 immr->im_ioport.iop_padir &= ~0x0200;
1200 cp->cp_sicr |= 0x2D;
1203 immr->im_ioport.iop_papar |= 0x0400;
1204 immr->im_ioport.iop_padir &= ~0x0400;
1205 cp->cp_sicr |= 0x36;
1208 immr->im_ioport.iop_papar |= 0x0800;
1209 immr->im_ioport.iop_padir &= ~0x0800;
1210 cp->cp_sicr |= 0x3F;
1213 udc_state = STATE_ERROR;
1217 #elif defined(CONFIG_SYS_USB_BRGCLK)
1219 /* This has been tested with brgclk == 50MHz */
1222 if (gd->cpu_clk < 48000000L) {
1223 ERR ("brgclk is too slow for full-speed USB!\n");
1224 udc_state = STATE_ERROR;
1228 /* Assume the brgclk is 'good enough', we want !(gd->cpu_clk%48MHz)
1229 * but, can /probably/ live with close-ish alternative rates.
1231 divisor = (gd->cpu_clk / 48000000L) - 1;
1232 cp->cp_sicr &= ~0x0000003F;
1234 switch (CONFIG_SYS_USB_BRGCLK) {
1236 cp->cp_brgc1 |= (divisor | CPM_BRG_EN);
1237 cp->cp_sicr &= ~0x2F;
1240 cp->cp_brgc2 |= (divisor | CPM_BRG_EN);
1241 cp->cp_sicr |= 0x00000009;
1244 cp->cp_brgc3 |= (divisor | CPM_BRG_EN);
1245 cp->cp_sicr |= 0x00000012;
1248 cp->cp_brgc4 = (divisor | CPM_BRG_EN);
1249 cp->cp_sicr |= 0x0000001B;
1252 udc_state = STATE_ERROR;
1257 #error "CONFIG_SYS_USB_EXTC_CLK or CONFIG_SYS_USB_BRGCLK must be defined"
1262 /* mpc8xx_udc_cbd_attach
1264 * attach a cbd to and endpoint
1266 static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size)
1269 if (!tx_cbd[ep] || !rx_cbd[ep] || ep >= MAX_ENDPOINTS) {
1270 udc_state = STATE_ERROR;
1274 if (tx_size > USB_MAX_PKT || rx_size > USB_MAX_PKT ||
1275 (!tx_size && !rx_size)) {
1276 udc_state = STATE_ERROR;
1280 /* Attach CBD to appropiate Parameter RAM Endpoint data structure */
1282 endpoints[ep]->rbase = (u32) rx_cbd[rx_ct];
1283 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
1288 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
1289 rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
1294 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
1295 rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
1299 /* Where we expect to RX data on this endpoint */
1300 ep_ref[ep].prx = rx_cbd[rx_ct - 1];
1304 endpoints[ep]->rbase = 0;
1305 endpoints[ep]->rbptr = 0;
1309 endpoints[ep]->tbase = (u32) tx_cbd[tx_ct];
1310 endpoints[ep]->tbptr = (u32) tx_cbd[tx_ct];
1313 endpoints[ep]->tbase = 0;
1314 endpoints[ep]->tbptr = 0;
1317 endpoints[ep]->tstate = 0;
1318 endpoints[ep]->tbcnt = 0;
1319 endpoints[ep]->mrblr = EP_MAX_PKT;
1320 endpoints[ep]->rfcr = 0x18;
1321 endpoints[ep]->tfcr = 0x18;
1322 ep_ref[ep].sc |= EP_ATTACHED;
1324 DBG ("ep %d rbase 0x%08x rbptr 0x%08x tbase 0x%08x tbptr 0x%08x prx = %p\n",
1325 ep, endpoints[ep]->rbase, endpoints[ep]->rbptr,
1326 endpoints[ep]->tbase, endpoints[ep]->tbptr,
1332 /* mpc8xx_udc_cbd_init
1334 * Allocate space for a cbd and allocate TX/RX data space
1336 static void mpc8xx_udc_cbd_init (void)
1340 for (; i < TX_RING_SIZE; i++) {
1341 tx_cbd[i] = (cbd_t *)
1342 mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int));
1345 for (i = 0; i < RX_RING_SIZE; i++) {
1346 rx_cbd[i] = (cbd_t *)
1347 mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int));
1350 for (i = 0; i < TX_RING_SIZE; i++) {
1351 tx_cbd[i]->cbd_bufaddr =
1352 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
1354 tx_cbd[i]->cbd_sc = (TX_BD_I | TX_BD_W);
1355 tx_cbd[i]->cbd_datlen = 0x0000;
1359 for (i = 0; i < RX_RING_SIZE; i++) {
1360 rx_cbd[i]->cbd_bufaddr =
1361 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
1362 rx_cbd[i]->cbd_sc = (RX_BD_I | RX_BD_E);
1363 rx_cbd[i]->cbd_datlen = 0x0000;
1370 /* mpc8xx_udc_endpoint_init
1372 * Attach an endpoint to some dpram
1374 static void mpc8xx_udc_endpoint_init (void)
1378 for (; i < MAX_ENDPOINTS; i++) {
1379 endpoints[i] = (usb_epb_t *)
1380 mpc8xx_udc_alloc (sizeof (usb_epb_t), 32);
1386 * Grab the address of some dpram
1388 static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment)
1390 u32 retaddr = address_base;
1392 while (retaddr % alignment) {
1395 address_base += data_size;