Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 by Bryan O'Donoghue, CodeHermit |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 3 | * bodonoghue@CodeHermit.ie |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 4 | * |
| 5 | * References |
Jean-Christophe PLAGNIOL-VILLARD | 8f6bcf4 | 2009-04-03 12:46:58 +0200 | [diff] [blame] | 6 | * DasUBoot/drivers/usb/gadget/omap1510_udc.c, for design and implementation |
Marcel Ziswiler | aea6856 | 2007-12-30 03:30:46 +0100 | [diff] [blame] | 7 | * ideas. |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 8 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * Notes : |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 14 | * 1. #define __SIMULATE_ERROR__ to inject a CRC error into every 2nd TX |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 15 | * packet to force the USB re-transmit protocol. |
| 16 | * |
| 17 | * 2. #define __DEBUG_UDC__ to switch on debug tracing to serial console |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 18 | * be careful that tracing doesn't create Hiesen-bugs with respect to |
| 19 | * response timeouts to control requests. |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 20 | * |
| 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. |
| 25 | * |
| 26 | * 4. NAK events never actually get raised at all, the documentation |
| 27 | * is just wrong ! |
| 28 | * |
| 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 |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 31 | * the same size is reported for 14 bytes received as 16 bytes received |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 32 | * until we can find out why this happens, RX cbds must be limited to 8 |
| 33 | * bytes. TODO: check errata for this behaviour. |
| 34 | * |
| 35 | * 6. Right now this code doesn't support properly powering up with the USB |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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 ! |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 43 | * |
| 44 | */ |
| 45 | #include <common.h> |
| 46 | #include <config.h> |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 47 | #include <commproc.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8f6bcf4 | 2009-04-03 12:46:58 +0200 | [diff] [blame] | 48 | #include <usbdevice.h> |
| 49 | #include <usb/mpc8xx_udc.h> |
Troy Kisky | df446f5 | 2013-10-10 15:28:04 -0700 | [diff] [blame] | 50 | #include <usb/udc.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8f6bcf4 | 2009-04-03 12:46:58 +0200 | [diff] [blame] | 51 | |
| 52 | #include "ep0.h" |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 53 | |
Wolfgang Denk | d112a2c | 2007-09-15 20:48:41 +0200 | [diff] [blame] | 54 | DECLARE_GLOBAL_DATA_PTR; |
| 55 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 56 | #define ERR(fmt, args...)\ |
| 57 | serial_printf("ERROR : [%s] %s:%d: "fmt,\ |
| 58 | __FILE__,__FUNCTION__,__LINE__, ##args) |
| 59 | #ifdef __DEBUG_UDC__ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 60 | #define DBG(fmt,args...)\ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 61 | serial_printf("[%s] %s:%d: "fmt,\ |
| 62 | __FILE__,__FUNCTION__,__LINE__, ##args) |
| 63 | #else |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 64 | #define DBG(fmt,args...) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | /* Static Data */ |
| 68 | #ifdef __SIMULATE_ERROR__ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 69 | static char err_poison_test = 0; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 70 | #endif |
| 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; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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]; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 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; |
| 82 | static int rx_ct = 0; |
| 83 | static int tx_ct = 0; |
| 84 | |
| 85 | /* Static Function Declarations */ |
| 86 | static void mpc8xx_udc_state_transition_up (usb_device_state_t initial, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 87 | usb_device_state_t final); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 88 | static void mpc8xx_udc_state_transition_down (usb_device_state_t initial, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 89 | usb_device_state_t final); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 90 | static void mpc8xx_udc_stall (unsigned int ep); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 91 | static void mpc8xx_udc_flush_tx_fifo (int epid); |
| 92 | static void mpc8xx_udc_flush_rx_fifo (void); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 93 | static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 94 | static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi, |
| 95 | struct urb *tx_urb); |
| 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); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 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); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 101 | static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 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); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 108 | static short mpc8xx_udc_handle_txerr (void); |
| 109 | static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 110 | |
| 111 | /****************************************************************************** |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 112 | Global Linkage |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 113 | *****************************************************************************/ |
| 114 | |
| 115 | /* udc_init |
| 116 | * |
| 117 | * Do initial bus gluing |
| 118 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 119 | int udc_init (void) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 120 | { |
| 121 | /* Init various pointers */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 122 | immr = (immap_t *) CONFIG_SYS_IMMR; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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]); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 126 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 127 | memset (ep_ref, 0x00, (sizeof (struct mpc8xx_ep) * MAX_ENDPOINTS)); |
| 128 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 129 | udc_device = 0; |
| 130 | udc_state = STATE_NOT_READY; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 131 | |
| 132 | usbp->usmod = 0x00; |
| 133 | usbp->uscom = 0; |
| 134 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 135 | /* Set USB Frame #0, Respond at Address & Get a clock source */ |
| 136 | usbp->usaddr = 0x00; |
| 137 | mpc8xx_udc_clock_init (immr, cp); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 138 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 139 | /* PA15, PA14 as perhiperal USBRXD and USBOE */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 140 | immr->im_ioport.iop_padir &= ~0x0003; |
| 141 | immr->im_ioport.iop_papar |= 0x0003; |
| 142 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 143 | /* PC11/PC10 as peripheral USBRXP USBRXN */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 144 | immr->im_ioport.iop_pcso |= 0x0030; |
| 145 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 146 | /* PC7/PC6 as perhiperal USBTXP and USBTXN */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 147 | immr->im_ioport.iop_pcdir |= 0x0300; |
| 148 | immr->im_ioport.iop_pcpar |= 0x0300; |
| 149 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 150 | /* Set the base address */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 151 | address_base = (u32) (cp->cp_dpmem + CPM_USB_BASE); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 152 | |
| 153 | /* Initialise endpoints and circular buffers */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 154 | mpc8xx_udc_endpoint_init (); |
| 155 | mpc8xx_udc_cbd_init (); |
| 156 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 157 | /* Assign allocated Dual Port Endpoint descriptors */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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]; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 162 | usb_paramp->frame_n = 0; |
| 163 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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, |
| 166 | usb_paramp->ep3ptr); |
| 167 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* udc_irq |
| 172 | * |
Vagrant Cascadian | edfdb99 | 2016-04-30 19:18:00 -0700 | [diff] [blame] | 173 | * Poll for whatever events may have occurred |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 174 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 175 | void udc_irq (void) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 176 | { |
| 177 | int epid = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 178 | volatile cbd_t *rx_cbdp = 0; |
| 179 | volatile cbd_t *rx_cbdp_base = 0; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 180 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 181 | if (udc_state != STATE_READY) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 182 | return; |
| 183 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 184 | |
| 185 | if (usbp->usber & USB_E_BSY) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 186 | /* This shouldn't happen. If it does then it's a bug ! */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 187 | usbp->usber |= USB_E_BSY; |
| 188 | mpc8xx_udc_flush_rx_fifo (); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 191 | /* Scan all RX/Bidirectional Endpoints for RX data. */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 192 | for (epid = 0; epid < MAX_ENDPOINTS; epid++) { |
| 193 | if (!ep_ref[epid].prx) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 194 | continue; |
| 195 | } |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 196 | rx_cbdp = rx_cbdp_base = ep_ref[epid].prx; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 197 | |
| 198 | do { |
| 199 | if (!(rx_cbdp->cbd_sc & RX_BD_E)) { |
| 200 | |
| 201 | if (rx_cbdp->cbd_sc & 0x1F) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 202 | /* Corrupt data discard it. |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 203 | * Controller has NAK'd this packet. |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 204 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 205 | mpc8xx_udc_clear_rxbd (rx_cbdp); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 206 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 207 | } else { |
| 208 | if (!epid) { |
| 209 | mpc8xx_udc_ep0_rx (rx_cbdp); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 210 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 211 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 212 | /* Process data */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 213 | mpc8xx_udc_set_nak (epid); |
| 214 | mpc8xx_udc_epn_rx (epid, rx_cbdp); |
| 215 | mpc8xx_udc_clear_rxbd (rx_cbdp); |
| 216 | } |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 217 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 218 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 219 | /* Advance RX CBD pointer */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 220 | mpc8xx_udc_advance_rx (&rx_cbdp, epid); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 221 | ep_ref[epid].prx = rx_cbdp; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 222 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 223 | /* Advance RX CBD pointer */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 224 | mpc8xx_udc_advance_rx (&rx_cbdp, epid); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 227 | } while (rx_cbdp != rx_cbdp_base); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 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 |
| 232 | */ |
| 233 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 234 | if (usbp->usber & USB_E_TXB) { |
| 235 | usbp->usber |= USB_E_TXB; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 236 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 237 | |
| 238 | if (usbp->usber & (USB_TX_ERRMASK)) { |
| 239 | mpc8xx_udc_handle_txerr (); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* Switch to the default state, respond at the default address */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 243 | if (usbp->usber & USB_E_RESET) { |
| 244 | usbp->usber |= USB_E_RESET; |
| 245 | usbp->usaddr = 0x00; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 246 | udc_device->device_state = STATE_DEFAULT; |
| 247 | } |
| 248 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 249 | /* if(usbp->usber&USB_E_IDLE){ |
| 250 | We could suspend here ! |
| 251 | usbp->usber|=USB_E_IDLE; |
| 252 | DBG("idle state change\n"); |
| 253 | } |
| 254 | if(usbp->usbs){ |
| 255 | We could resume here when IDLE is deasserted ! |
| 256 | Not worth doing, so long as we are self powered though. |
| 257 | } |
| 258 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 259 | |
| 260 | return; |
| 261 | } |
| 262 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 263 | /* udc_endpoint_write |
| 264 | * |
| 265 | * Write some data to an endpoint |
| 266 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 267 | int udc_endpoint_write (struct usb_endpoint_instance *epi) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 268 | { |
| 269 | int ep = 0; |
| 270 | short epid = 1, unnak = 0, ret = 0; |
| 271 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 272 | if (udc_state != STATE_READY) { |
| 273 | ERR ("invalid udc_state != STATE_READY!\n"); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 277 | if (!udc_device || !epi) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 278 | return -1; |
| 279 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 280 | |
| 281 | if (udc_device->device_state != STATE_CONFIGURED) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | ep = epi->endpoint_address & 0x03; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 286 | if (ep >= MAX_ENDPOINTS) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 287 | return -1; |
| 288 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 289 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 290 | /* Set NAK for all RX endpoints during TX */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 291 | for (epid = 1; epid < MAX_ENDPOINTS; epid++) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 292 | |
| 293 | /* Don't set NAK on DATA IN/CONTROL endpoints */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 294 | if (ep_ref[epid].sc & USB_DIR_IN) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 295 | continue; |
| 296 | } |
| 297 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 298 | if (!(usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK))) { |
| 299 | unnak |= 1 << epid; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 302 | mpc8xx_udc_set_nak (epid); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 305 | mpc8xx_udc_init_tx (&udc_device->bus->endpoint_array[ep], |
| 306 | epi->tx_urb); |
| 307 | ret = mpc8xx_udc_ep_tx (&udc_device->bus->endpoint_array[ep]); |
| 308 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 309 | /* Remove temporary NAK */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 310 | for (epid = 1; epid < MAX_ENDPOINTS; epid++) { |
| 311 | if (unnak & (1 << epid)) { |
| 312 | udc_unset_nak (epid); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 313 | } |
| 314 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 315 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | /* mpc8xx_udc_assign_urb |
| 320 | * |
| 321 | * Associate a given urb to an endpoint TX or RX transmit/receive buffers |
| 322 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 323 | static int mpc8xx_udc_assign_urb (int ep, char direction) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 324 | { |
| 325 | struct usb_endpoint_instance *epi = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 326 | |
| 327 | if (ep >= MAX_ENDPOINTS) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 328 | goto err; |
| 329 | } |
| 330 | epi = &udc_device->bus->endpoint_array[ep]; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 331 | if (!epi) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 332 | goto err; |
| 333 | } |
| 334 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 338 | goto err; |
| 339 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 340 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 341 | ep_ref[ep].urb->actual_length = 0; |
| 342 | } |
| 343 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 344 | switch (direction) { |
| 345 | case USB_DIR_IN: |
| 346 | epi->tx_urb = ep_ref[ep].urb; |
| 347 | break; |
| 348 | case USB_DIR_OUT: |
| 349 | epi->rcv_urb = ep_ref[ep].urb; |
| 350 | break; |
| 351 | default: |
| 352 | goto err; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 353 | } |
| 354 | return 0; |
| 355 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 356 | err: |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 357 | udc_state = STATE_ERROR; |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | /* udc_setup_ep |
| 362 | * |
| 363 | * Associate U-Boot software endpoints to mpc8xx endpoint parameter ram |
| 364 | * Isochronous endpoints aren't yet supported! |
| 365 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 366 | void udc_setup_ep (struct usb_device_instance *device, unsigned int ep, |
| 367 | struct usb_endpoint_instance *epi) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 368 | { |
| 369 | uchar direction = 0; |
| 370 | int ep_attrib = 0; |
| 371 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 372 | if (epi && (ep < MAX_ENDPOINTS)) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 373 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 374 | if (ep == 0) { |
| 375 | if (epi->rcv_attributes != USB_ENDPOINT_XFER_CONTROL |
| 376 | || epi->tx_attributes != |
| 377 | USB_ENDPOINT_XFER_CONTROL) { |
| 378 | |
| 379 | /* ep0 must be a control endpoint */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 380 | udc_state = STATE_ERROR; |
| 381 | return; |
| 382 | |
| 383 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 384 | if (!(ep_ref[ep].sc & EP_ATTACHED)) { |
| 385 | mpc8xx_udc_cbd_attach (ep, epi->tx_packetSize, |
| 386 | epi->rcv_packetSize); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 387 | } |
| 388 | usbp->usep[ep] = 0x0000; |
| 389 | return; |
| 390 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 391 | |
| 392 | if ((epi->endpoint_address & USB_ENDPOINT_DIR_MASK) |
| 393 | == USB_DIR_IN) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 394 | |
| 395 | direction = 1; |
| 396 | ep_attrib = epi->tx_attributes; |
| 397 | epi->rcv_packetSize = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 398 | ep_ref[ep].sc |= USB_DIR_IN; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 399 | } else { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 400 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 401 | direction = 0; |
| 402 | ep_attrib = epi->rcv_attributes; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 403 | epi->tx_packetSize = 0; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 404 | ep_ref[ep].sc &= ~USB_DIR_IN; |
| 405 | } |
| 406 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 407 | if (mpc8xx_udc_assign_urb (ep, epi->endpoint_address |
| 408 | & USB_ENDPOINT_DIR_MASK)) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 409 | return; |
| 410 | } |
| 411 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 412 | switch (ep_attrib) { |
| 413 | case USB_ENDPOINT_XFER_CONTROL: |
| 414 | if (!(ep_ref[ep].sc & EP_ATTACHED)) { |
| 415 | mpc8xx_udc_cbd_attach (ep, |
| 416 | epi->tx_packetSize, |
| 417 | epi->rcv_packetSize); |
| 418 | } |
| 419 | usbp->usep[ep] = ep << 12; |
| 420 | epi->rcv_urb = epi->tx_urb = ep_ref[ep].urb; |
| 421 | |
| 422 | break; |
| 423 | case USB_ENDPOINT_XFER_BULK: |
| 424 | case USB_ENDPOINT_XFER_INT: |
| 425 | if (!(ep_ref[ep].sc & EP_ATTACHED)) { |
| 426 | if (direction) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 427 | mpc8xx_udc_cbd_attach (ep, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 428 | epi->tx_packetSize, |
| 429 | 0); |
| 430 | } else { |
| 431 | mpc8xx_udc_cbd_attach (ep, |
| 432 | 0, |
| 433 | epi->rcv_packetSize); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 434 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 435 | } |
| 436 | usbp->usep[ep] = (ep << 12) | ((ep_attrib) << 8); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 437 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 438 | break; |
| 439 | case USB_ENDPOINT_XFER_ISOC: |
| 440 | default: |
| 441 | serial_printf ("Error endpoint attrib %d>3\n", ep_attrib); |
| 442 | udc_state = STATE_ERROR; |
| 443 | break; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | } |
| 448 | |
| 449 | /* udc_connect |
| 450 | * |
| 451 | * Move state, switch on the USB |
| 452 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 453 | void udc_connect (void) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 454 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 455 | /* Enable pull-up resistor on D+ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 456 | * TODO: fit a pull-up resistor to drive SE0 for > 2.5us |
| 457 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 458 | |
| 459 | if (udc_state != STATE_ERROR) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 460 | udc_state = STATE_READY; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 461 | usbp->usmod |= USMOD_EN; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 462 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 463 | } |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 464 | |
| 465 | /* udc_disconnect |
| 466 | * |
| 467 | * Disconnect is not used but, is included for completeness |
| 468 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 469 | void udc_disconnect (void) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 470 | { |
| 471 | /* Disable pull-up resistor on D- |
| 472 | * TODO: fix a pullup resistor to control this |
| 473 | */ |
| 474 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 475 | if (udc_state != STATE_ERROR) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 476 | udc_state = STATE_NOT_READY; |
| 477 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 478 | usbp->usmod &= ~USMOD_EN; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* udc_enable |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 482 | * |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 483 | * Grab an EP0 URB, register interest in a subset of USB events |
| 484 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 485 | void udc_enable (struct usb_device_instance *device) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 486 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 487 | if (udc_state == STATE_ERROR) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
| 491 | udc_device = device; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 492 | |
| 493 | if (!ep_ref[0].urb) { |
| 494 | ep_ref[0].urb = usbd_alloc_urb (device, device->bus->endpoint_array); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* Register interest in all events except SOF, enable transceiver */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 498 | usbp->usber = 0x03FF; |
| 499 | usbp->usbmr = 0x02F7; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 500 | |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | /* udc_disable |
| 505 | * |
| 506 | * disable the currently hooked device |
| 507 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 508 | void udc_disable (void) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 509 | { |
| 510 | int i = 0; |
| 511 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 512 | if (udc_state == STATE_ERROR) { |
| 513 | DBG ("Won't disable UDC. udc_state==STATE_ERROR !\n"); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 514 | return; |
| 515 | } |
| 516 | |
| 517 | udc_device = 0; |
| 518 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 519 | for (; i < MAX_ENDPOINTS; i++) { |
| 520 | if (ep_ref[i].urb) { |
| 521 | usbd_dealloc_urb (ep_ref[i].urb); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 522 | ep_ref[i].urb = 0; |
| 523 | } |
| 524 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 525 | |
| 526 | usbp->usbmr = 0x00; |
| 527 | usbp->usmod = ~USMOD_EN; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 528 | udc_state = STATE_NOT_READY; |
| 529 | } |
| 530 | |
| 531 | /* udc_startup_events |
| 532 | * |
| 533 | * Enable the specified device |
| 534 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 535 | void udc_startup_events (struct usb_device_instance *device) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 536 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 537 | udc_enable (device); |
| 538 | if (udc_state == STATE_READY) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 539 | usbd_device_event_irq (device, DEVICE_CREATE, 0); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /* udc_set_nak |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 544 | * |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 545 | * Allow upper layers to signal lower layers should not accept more RX data |
| 546 | * |
| 547 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 548 | void udc_set_nak (int epid) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 549 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 550 | if (epid) { |
| 551 | mpc8xx_udc_set_nak (epid); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 555 | /* udc_unset_nak |
| 556 | * |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 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. |
| 559 | * |
| 560 | */ |
| 561 | void udc_unset_nak (int epid) |
| 562 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 563 | if (epid > MAX_ENDPOINTS) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 564 | return; |
| 565 | } |
| 566 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 567 | if (usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK)) { |
| 568 | usbp->usep[epid] &= ~(USEP_THS_NAK | USEP_RHS_NAK); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 569 | __asm__ ("eieio"); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /****************************************************************************** |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 574 | Static Linkage |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 575 | ******************************************************************************/ |
| 576 | |
| 577 | /* udc_state_transition_up |
| 578 | * udc_state_transition_down |
| 579 | * |
| 580 | * Helper functions to implement device state changes. The device states and |
| 581 | * the events that transition between them are: |
| 582 | * |
| 583 | * STATE_ATTACHED |
| 584 | * || /\ |
| 585 | * \/ || |
| 586 | * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET |
| 587 | * || /\ |
| 588 | * \/ || |
| 589 | * STATE_POWERED |
| 590 | * || /\ |
| 591 | * \/ || |
| 592 | * DEVICE_RESET DEVICE_POWER_INTERRUPTION |
| 593 | * || /\ |
| 594 | * \/ || |
| 595 | * STATE_DEFAULT |
| 596 | * || /\ |
| 597 | * \/ || |
| 598 | * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET |
| 599 | * || /\ |
| 600 | * \/ || |
| 601 | * STATE_ADDRESSED |
| 602 | * || /\ |
| 603 | * \/ || |
| 604 | * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED |
| 605 | * || /\ |
| 606 | * \/ || |
| 607 | * STATE_CONFIGURED |
| 608 | * |
| 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. |
| 614 | * |
| 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. |
| 620 | * |
| 621 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 622 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 623 | static void mpc8xx_udc_state_transition_up (usb_device_state_t initial, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 624 | usb_device_state_t final) |
| 625 | { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 626 | if (initial < final) { |
| 627 | switch (initial) { |
| 628 | case STATE_ATTACHED: |
| 629 | usbd_device_event_irq (udc_device, |
| 630 | DEVICE_HUB_CONFIGURED, 0); |
| 631 | if (final == STATE_POWERED) |
| 632 | break; |
| 633 | case STATE_POWERED: |
| 634 | usbd_device_event_irq (udc_device, DEVICE_RESET, 0); |
| 635 | if (final == STATE_DEFAULT) |
| 636 | break; |
| 637 | case STATE_DEFAULT: |
| 638 | usbd_device_event_irq (udc_device, |
| 639 | DEVICE_ADDRESS_ASSIGNED, 0); |
| 640 | if (final == STATE_ADDRESSED) |
| 641 | break; |
| 642 | case STATE_ADDRESSED: |
| 643 | usbd_device_event_irq (udc_device, DEVICE_CONFIGURED, |
| 644 | 0); |
| 645 | case STATE_CONFIGURED: |
| 646 | break; |
| 647 | default: |
| 648 | break; |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | static void mpc8xx_udc_state_transition_down (usb_device_state_t initial, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 654 | usb_device_state_t final) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 655 | { |
| 656 | if (initial > final) { |
| 657 | switch (initial) { |
| 658 | case STATE_CONFIGURED: |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 659 | usbd_device_event_irq (udc_device, |
| 660 | DEVICE_DE_CONFIGURED, 0); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 661 | if (final == STATE_ADDRESSED) |
| 662 | break; |
| 663 | case STATE_ADDRESSED: |
| 664 | usbd_device_event_irq (udc_device, DEVICE_RESET, 0); |
| 665 | if (final == STATE_DEFAULT) |
| 666 | break; |
| 667 | case STATE_DEFAULT: |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 668 | usbd_device_event_irq (udc_device, |
| 669 | DEVICE_POWER_INTERRUPTION, 0); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 670 | if (final == STATE_POWERED) |
| 671 | break; |
| 672 | case STATE_POWERED: |
| 673 | usbd_device_event_irq (udc_device, DEVICE_HUB_RESET, |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 674 | 0); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 675 | case STATE_ATTACHED: |
| 676 | break; |
| 677 | default: |
| 678 | break; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /* mpc8xx_udc_stall |
| 684 | * |
| 685 | * Force returning of STALL tokens on the given endpoint. Protocol or function |
| 686 | * STALL conditions are permissable here |
| 687 | */ |
| 688 | static void mpc8xx_udc_stall (unsigned int ep) |
| 689 | { |
| 690 | usbp->usep[ep] |= STALL_BITMASK; |
| 691 | } |
| 692 | |
| 693 | /* mpc8xx_udc_set_nak |
| 694 | * |
| 695 | * Force returning of NAK responses for the given endpoint as a kind of very |
| 696 | * simple flow control |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 697 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 698 | static void mpc8xx_udc_set_nak (unsigned int ep) |
| 699 | { |
| 700 | usbp->usep[ep] |= NAK_BITMASK; |
| 701 | __asm__ ("eieio"); |
| 702 | } |
| 703 | |
| 704 | /* mpc8xx_udc_handle_txerr |
| 705 | * |
| 706 | * Handle errors relevant to TX. Return a status code to allow calling |
| 707 | * indicative of what if anything happened |
| 708 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 709 | static short mpc8xx_udc_handle_txerr () |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 710 | { |
| 711 | short ep = 0, ret = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 712 | |
| 713 | for (; ep < TX_RING_SIZE; ep++) { |
| 714 | if (usbp->usber & (0x10 << ep)) { |
| 715 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 716 | /* Timeout or underrun */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 717 | if (tx_cbd[ep]->cbd_sc & 0x06) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 718 | ret = 1; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 719 | mpc8xx_udc_flush_tx_fifo (ep); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 720 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 721 | } else { |
| 722 | if (usbp->usep[ep] & STALL_BITMASK) { |
| 723 | if (!ep) { |
| 724 | usbp->usep[ep] &= ~STALL_BITMASK; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 725 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 726 | } /* else NAK */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 727 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 728 | usbp->usber |= (0x10 << ep); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | return ret; |
| 732 | } |
| 733 | |
| 734 | /* mpc8xx_udc_advance_rx |
| 735 | * |
| 736 | * Advance cbd rx |
| 737 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 738 | static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 739 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 740 | if ((*rx_cbdp)->cbd_sc & RX_BD_W) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 741 | *rx_cbdp = (volatile cbd_t *) (endpoints[epid]->rbase + CONFIG_SYS_IMMR); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 742 | |
| 743 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 744 | (*rx_cbdp)++; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | |
| 749 | /* mpc8xx_udc_flush_tx_fifo |
| 750 | * |
| 751 | * Flush a given TX fifo. Assumes one tx cbd per endpoint |
| 752 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 753 | static void mpc8xx_udc_flush_tx_fifo (int epid) |
| 754 | { |
| 755 | volatile cbd_t *tx_cbdp = 0; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 756 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 757 | if (epid > MAX_ENDPOINTS) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 758 | return; |
| 759 | } |
| 760 | |
| 761 | /* TX stop */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 762 | immr->im_cpm.cp_cpcr = ((epid << 2) | 0x1D01); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 763 | __asm__ ("eieio"); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 764 | while (immr->im_cpm.cp_cpcr & 0x01); |
| 765 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 766 | usbp->uscom = 0x40 | 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 767 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 768 | /* reset ring */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 769 | tx_cbdp = (cbd_t *) (endpoints[epid]->tbptr + CONFIG_SYS_IMMR); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 770 | tx_cbdp->cbd_sc = (TX_BD_I | TX_BD_W); |
| 771 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 772 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 773 | endpoints[epid]->tptr = endpoints[epid]->tbase; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 774 | endpoints[epid]->tstate = 0x00; |
| 775 | endpoints[epid]->tbcnt = 0x00; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 776 | |
| 777 | /* TX start */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 778 | immr->im_cpm.cp_cpcr = ((epid << 2) | 0x2D01); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 779 | __asm__ ("eieio"); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 780 | while (immr->im_cpm.cp_cpcr & 0x01); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 781 | |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | /* mpc8xx_udc_flush_rx_fifo |
| 786 | * |
| 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. |
| 793 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 794 | static void mpc8xx_udc_flush_rx_fifo () |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 795 | { |
| 796 | int i = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 797 | |
| 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, |
| 802 | rx_cbd[i]->cbd_sc); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 803 | |
| 804 | } |
| 805 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 806 | ERR ("BUG : Input over-run\n"); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* mpc8xx_udc_clear_rxbd |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 810 | * |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 811 | * Release control of RX CBD to CP. |
| 812 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 813 | static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 814 | { |
| 815 | rx_cbdp->cbd_datlen = 0x0000; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 816 | rx_cbdp->cbd_sc = ((rx_cbdp->cbd_sc & RX_BD_W) | (RX_BD_E | RX_BD_I)); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 817 | __asm__ ("eieio"); |
| 818 | } |
| 819 | |
| 820 | /* mpc8xx_udc_tx_irq |
| 821 | * |
| 822 | * Parse for tx timeout, control RX or USB reset/busy conditions |
| 823 | * Return -1 on timeout, -2 on fatal error, else return zero |
| 824 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 825 | static int mpc8xx_udc_tx_irq (int ep) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 826 | { |
| 827 | int i = 0; |
| 828 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 829 | if (usbp->usber & (USB_TX_ERRMASK)) { |
| 830 | if (mpc8xx_udc_handle_txerr ()) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 831 | /* Timeout, controlling function must retry send */ |
| 832 | return -1; |
| 833 | } |
| 834 | } |
| 835 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 836 | if (usbp->usber & (USB_E_RESET | USB_E_BSY)) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 837 | /* Fatal, abandon TX transaction */ |
| 838 | return -2; |
| 839 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 840 | |
| 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) { |
| 845 | return -2; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | /* mpc8xx_udc_ep_tx |
| 855 | * |
| 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 |
| 861 | * TX loop. |
| 862 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 863 | static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 864 | { |
| 865 | struct urb *urb = epi->tx_urb; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 866 | volatile cbd_t *tx_cbdp = 0; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 867 | unsigned int ep = 0, pkt_len = 0, x = 0, tx_retry = 0; |
| 868 | int ret = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 869 | |
| 870 | if (!epi || (epi->endpoint_address & 0x03) >= MAX_ENDPOINTS || !urb) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 871 | return -1; |
| 872 | } |
| 873 | |
| 874 | ep = epi->endpoint_address & 0x03; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 875 | tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 876 | |
| 877 | if (tx_cbdp->cbd_sc & TX_BD_R || usbp->usber & USB_E_TXB) { |
| 878 | mpc8xx_udc_flush_tx_fifo (ep); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 879 | usbp->usber |= USB_E_TXB; |
| 880 | }; |
| 881 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 882 | while (tx_retry++ < 100) { |
| 883 | ret = mpc8xx_udc_tx_irq (ep); |
| 884 | if (ret == -1) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 885 | /* ignore timeout here */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 886 | } else if (ret == -2) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 887 | /* Abandon TX */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 888 | mpc8xx_udc_flush_tx_fifo (ep); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 889 | return -1; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 892 | tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 893 | while (tx_cbdp->cbd_sc & TX_BD_R) { |
| 894 | }; |
| 895 | tx_cbdp->cbd_sc = (tx_cbdp->cbd_sc & TX_BD_W); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 896 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 897 | pkt_len = urb->actual_length - epi->sent; |
| 898 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 899 | if (pkt_len > epi->tx_packetSize || pkt_len > EP_MAX_PKT) { |
Masahiro Yamada | b62b39b | 2014-09-18 13:28:06 +0900 | [diff] [blame] | 900 | pkt_len = min(epi->tx_packetSize, EP_MAX_PKT); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 901 | } |
| 902 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 903 | for (x = 0; x < pkt_len; x++) { |
| 904 | *((unsigned char *) (tx_cbdp->cbd_bufaddr + x)) = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 905 | urb->buffer[epi->sent + x]; |
| 906 | } |
| 907 | tx_cbdp->cbd_datlen = pkt_len; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 908 | tx_cbdp->cbd_sc |= (CBD_TX_BITMASK | ep_ref[ep].pid); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 909 | __asm__ ("eieio"); |
| 910 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 911 | #ifdef __SIMULATE_ERROR__ |
| 912 | if (++err_poison_test == 2) { |
| 913 | err_poison_test = 0; |
| 914 | tx_cbdp->cbd_sc &= ~TX_BD_TC; |
| 915 | } |
| 916 | #endif |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 917 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 918 | usbp->uscom = (USCOM_STR | ep); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 919 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 920 | while (!(usbp->usber & USB_E_TXB)) { |
| 921 | ret = mpc8xx_udc_tx_irq (ep); |
| 922 | if (ret == -1) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 923 | /* TX timeout */ |
| 924 | break; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 925 | } else if (ret == -2) { |
| 926 | if (usbp->usber & USB_E_TXB) { |
| 927 | usbp->usber |= USB_E_TXB; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 928 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 929 | mpc8xx_udc_flush_tx_fifo (ep); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 930 | return -1; |
| 931 | } |
| 932 | }; |
| 933 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 934 | if (usbp->usber & USB_E_TXB) { |
| 935 | usbp->usber |= USB_E_TXB; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /* ACK must be present <= 18bit times from TX */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 939 | if (ret == -1) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 940 | continue; |
| 941 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 942 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 943 | /* TX ACK : USB 2.0 8.7.2, Toggle PID, Advance TX */ |
| 944 | epi->sent += pkt_len; |
Masahiro Yamada | b62b39b | 2014-09-18 13:28:06 +0900 | [diff] [blame] | 945 | epi->last = min(urb->actual_length - epi->sent, epi->tx_packetSize); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 946 | TOGGLE_TX_PID (ep_ref[ep].pid); |
| 947 | |
| 948 | if (epi->sent >= epi->tx_urb->actual_length) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 949 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 950 | epi->tx_urb->actual_length = 0; |
| 951 | epi->sent = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 952 | |
| 953 | if (ep_ref[ep].sc & EP_SEND_ZLP) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 954 | ep_ref[ep].sc &= ~EP_SEND_ZLP; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 955 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 956 | return 0; |
| 957 | } |
| 958 | } |
| 959 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 960 | |
| 961 | ERR ("TX fail, endpoint 0x%x tx bytes 0x%x/0x%x\n", ep, epi->sent, |
| 962 | epi->tx_urb->actual_length); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 963 | |
| 964 | return -1; |
| 965 | } |
| 966 | |
| 967 | /* mpc8xx_udc_dump_request |
| 968 | * |
| 969 | * Dump a control request to console |
| 970 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 971 | static void mpc8xx_udc_dump_request (struct usb_device_request *request) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 972 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 973 | DBG ("bmRequestType:%02x bRequest:%02x wValue:%04x " |
| 974 | "wIndex:%04x wLength:%04x ?\n", |
| 975 | request->bmRequestType, |
| 976 | request->bRequest, |
| 977 | request->wValue, request->wIndex, request->wLength); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 978 | |
| 979 | return; |
| 980 | } |
| 981 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 982 | /* mpc8xx_udc_ep0_rx_setup |
| 983 | * |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 984 | * Decode received ep0 SETUP packet. return non-zero on error |
| 985 | */ |
| 986 | static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp) |
| 987 | { |
| 988 | unsigned int x = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 989 | struct urb *purb = ep_ref[0].urb; |
| 990 | struct usb_endpoint_instance *epi = |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 991 | &udc_device->bus->endpoint_array[0]; |
| 992 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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)); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 996 | } |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 997 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 998 | mpc8xx_udc_clear_rxbd (rx_cbdp); |
| 999 | |
| 1000 | if (ep0_recv_setup (purb)) { |
| 1001 | mpc8xx_udc_dump_request (&purb->device_request); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1002 | return -1; |
| 1003 | } |
| 1004 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1005 | if ((purb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1006 | == USB_REQ_HOST2DEVICE) { |
| 1007 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1015 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1016 | /* Move to the addressed state */ |
| 1017 | usbp->usaddr = udc_device->address; |
| 1018 | mpc8xx_udc_state_transition_up (udc_device->device_state, |
| 1019 | STATE_ADDRESSED); |
| 1020 | return 0; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1021 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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, |
| 1027 | STATE_ADDRESSED); |
| 1028 | } else { |
| 1029 | /* TODO: Support multiple configurations */ |
| 1030 | mpc8xx_udc_state_transition_up (udc_device->device_state, |
| 1031 | STATE_CONFIGURED); |
| 1032 | for (x = 1; x < MAX_ENDPOINTS; x++) { |
| 1033 | if ((udc_device->bus->endpoint_array[x].endpoint_address & USB_ENDPOINT_DIR_MASK) |
| 1034 | == USB_DIR_IN) { |
| 1035 | ep_ref[x].pid = TX_BD_PID_DATA0; |
| 1036 | } else { |
| 1037 | ep_ref[x].pid = RX_BD_PID_DATA0; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1038 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1039 | /* Set configuration must unstall endpoints */ |
| 1040 | usbp->usep[x] &= ~STALL_BITMASK; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1041 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1042 | } |
| 1043 | break; |
| 1044 | default: |
| 1045 | /* CDC/Vendor specific */ |
| 1046 | break; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /* Send ZLP as ACK in Status OUT phase */ |
| 1050 | ep_ref[0].pid = TX_BD_PID_DATA1; |
| 1051 | purb->actual_length = 0; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1052 | mpc8xx_udc_init_tx (epi, purb); |
| 1053 | mpc8xx_udc_ep_tx (epi); |
| 1054 | |
| 1055 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1056 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1057 | if (purb->actual_length) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1058 | ep_ref[0].pid = TX_BD_PID_DATA1; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1059 | mpc8xx_udc_init_tx (epi, purb); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1060 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1061 | if (!(purb->actual_length % EP0_MAX_PACKET_SIZE)) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1062 | ep_ref[0].sc |= EP_SEND_ZLP; |
| 1063 | } |
| 1064 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1065 | if (purb->device_request.wValue == |
| 1066 | USB_DESCRIPTOR_TYPE_DEVICE) { |
| 1067 | if (le16_to_cpu (purb->device_request.wLength) |
| 1068 | > purb->actual_length) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1069 | /* Send EP0_MAX_PACKET_SIZE bytes |
| 1070 | * unless correct size requested. |
| 1071 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1072 | if (purb->actual_length > epi->tx_packetSize) { |
| 1073 | purb->actual_length = epi->tx_packetSize; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1074 | } |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1075 | } |
| 1076 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1077 | mpc8xx_udc_ep_tx (epi); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1078 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1079 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1080 | /* Corrupt SETUP packet? */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1081 | ERR ("Zero length data or SETUP with DATA-IN phase ?\n"); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1082 | return 1; |
| 1083 | } |
| 1084 | } |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | /* mpc8xx_udc_init_tx |
| 1089 | * |
| 1090 | * Setup some basic parameters for a TX transaction |
| 1091 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1092 | static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi, |
| 1093 | struct urb *tx_urb) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1094 | { |
| 1095 | epi->sent = 0; |
| 1096 | epi->last = 0; |
| 1097 | epi->tx_urb = tx_urb; |
| 1098 | } |
| 1099 | |
| 1100 | /* mpc8xx_udc_ep0_rx |
| 1101 | * |
| 1102 | * Receive ep0/control USB data. Parse and possibly send a response. |
| 1103 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1104 | static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1105 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1106 | if (rx_cbdp->cbd_sc & RX_BD_PID_SETUP) { |
| 1107 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1108 | /* Unconditionally accept SETUP packets */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1109 | if (mpc8xx_udc_ep0_rx_setup (rx_cbdp)) { |
| 1110 | mpc8xx_udc_stall (0); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1111 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1112 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1113 | } else { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1114 | |
| 1115 | mpc8xx_udc_clear_rxbd (rx_cbdp); |
| 1116 | |
| 1117 | if ((rx_cbdp->cbd_datlen - 2)) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1118 | /* SETUP with a DATA phase |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1119 | * outside of SETUP packet. |
| 1120 | * Reply with STALL. |
| 1121 | */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1122 | mpc8xx_udc_stall (0); |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | /* mpc8xx_udc_epn_rx |
| 1128 | * |
| 1129 | * Receive some data from cbd into USB system urb data abstraction |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1130 | * Upper layers should NAK if there is insufficient RX data space |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1131 | */ |
| 1132 | static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp) |
| 1133 | { |
| 1134 | struct usb_endpoint_instance *epi = 0; |
| 1135 | struct urb *urb = 0; |
| 1136 | unsigned int x = 0; |
| 1137 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1138 | if (epid >= MAX_ENDPOINTS || !rx_cbdp->cbd_datlen) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1141 | |
| 1142 | /* USB 2.0 PDF section 8.6.4 |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1143 | * Discard data with invalid PID it is a resend. |
| 1144 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1145 | if (ep_ref[epid].pid != (rx_cbdp->cbd_sc & 0xC0)) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1146 | return 1; |
| 1147 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1148 | TOGGLE_RX_PID (ep_ref[epid].pid); |
| 1149 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1150 | epi = &udc_device->bus->endpoint_array[epid]; |
| 1151 | urb = epi->rcv_urb; |
| 1152 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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)); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1158 | if (x) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1159 | usbd_rcv_complete (epi, x, 0); |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1160 | if (ep_ref[epid].urb->status == RECV_ERROR) { |
| 1161 | DBG ("RX error unset NAK\n"); |
| 1162 | udc_unset_nak (epid); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1163 | } |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1164 | } |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1165 | return x; |
| 1166 | } |
| 1167 | |
| 1168 | /* mpc8xx_udc_clock_init |
| 1169 | * |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1170 | * Obtain a clock reference for Full Speed Signaling |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1171 | */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1172 | static void mpc8xx_udc_clock_init (volatile immap_t * immr, |
| 1173 | volatile cpm8xx_t * cp) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1174 | { |
| 1175 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1176 | #if defined(CONFIG_SYS_USB_EXTC_CLK) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1177 | |
| 1178 | /* This has been tested with a 48MHz crystal on CLK6 */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1179 | switch (CONFIG_SYS_USB_EXTC_CLK) { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1180 | case 1: |
| 1181 | immr->im_ioport.iop_papar |= 0x0100; |
| 1182 | immr->im_ioport.iop_padir &= ~0x0100; |
| 1183 | cp->cp_sicr |= 0x24; |
| 1184 | break; |
| 1185 | case 2: |
| 1186 | immr->im_ioport.iop_papar |= 0x0200; |
| 1187 | immr->im_ioport.iop_padir &= ~0x0200; |
| 1188 | cp->cp_sicr |= 0x2D; |
| 1189 | break; |
| 1190 | case 3: |
| 1191 | immr->im_ioport.iop_papar |= 0x0400; |
| 1192 | immr->im_ioport.iop_padir &= ~0x0400; |
| 1193 | cp->cp_sicr |= 0x36; |
| 1194 | break; |
| 1195 | case 4: |
| 1196 | immr->im_ioport.iop_papar |= 0x0800; |
| 1197 | immr->im_ioport.iop_padir &= ~0x0800; |
| 1198 | cp->cp_sicr |= 0x3F; |
| 1199 | break; |
| 1200 | default: |
| 1201 | udc_state = STATE_ERROR; |
| 1202 | break; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1205 | #elif defined(CONFIG_SYS_USB_BRGCLK) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1206 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1207 | /* This has been tested with brgclk == 50MHz */ |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1208 | int divisor = 0; |
| 1209 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1210 | if (gd->cpu_clk < 48000000L) { |
| 1211 | ERR ("brgclk is too slow for full-speed USB!\n"); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1212 | udc_state = STATE_ERROR; |
| 1213 | return; |
| 1214 | } |
| 1215 | |
Wolfgang Denk | af0501a | 2008-10-19 02:35:50 +0200 | [diff] [blame] | 1216 | /* Assume the brgclk is 'good enough', we want !(gd->cpu_clk%48MHz) |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1217 | * but, can /probably/ live with close-ish alternative rates. |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1218 | */ |
| 1219 | divisor = (gd->cpu_clk / 48000000L) - 1; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1220 | cp->cp_sicr &= ~0x0000003F; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1221 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1222 | switch (CONFIG_SYS_USB_BRGCLK) { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1223 | case 1: |
| 1224 | cp->cp_brgc1 |= (divisor | CPM_BRG_EN); |
| 1225 | cp->cp_sicr &= ~0x2F; |
| 1226 | break; |
| 1227 | case 2: |
| 1228 | cp->cp_brgc2 |= (divisor | CPM_BRG_EN); |
| 1229 | cp->cp_sicr |= 0x00000009; |
| 1230 | break; |
| 1231 | case 3: |
| 1232 | cp->cp_brgc3 |= (divisor | CPM_BRG_EN); |
| 1233 | cp->cp_sicr |= 0x00000012; |
| 1234 | break; |
| 1235 | case 4: |
| 1236 | cp->cp_brgc4 = (divisor | CPM_BRG_EN); |
| 1237 | cp->cp_sicr |= 0x0000001B; |
| 1238 | break; |
| 1239 | default: |
| 1240 | udc_state = STATE_ERROR; |
| 1241 | break; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1245 | #error "CONFIG_SYS_USB_EXTC_CLK or CONFIG_SYS_USB_BRGCLK must be defined" |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1246 | #endif |
| 1247 | |
| 1248 | } |
| 1249 | |
| 1250 | /* mpc8xx_udc_cbd_attach |
| 1251 | * |
| 1252 | * attach a cbd to and endpoint |
| 1253 | */ |
| 1254 | static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size) |
| 1255 | { |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1256 | |
| 1257 | if (!tx_cbd[ep] || !rx_cbd[ep] || ep >= MAX_ENDPOINTS) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1258 | udc_state = STATE_ERROR; |
| 1259 | return; |
| 1260 | } |
| 1261 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1262 | if (tx_size > USB_MAX_PKT || rx_size > USB_MAX_PKT || |
| 1263 | (!tx_size && !rx_size)) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1264 | udc_state = STATE_ERROR; |
| 1265 | return; |
| 1266 | } |
| 1267 | |
| 1268 | /* Attach CBD to appropiate Parameter RAM Endpoint data structure */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1269 | if (rx_size) { |
| 1270 | endpoints[ep]->rbase = (u32) rx_cbd[rx_ct]; |
| 1271 | endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct]; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1272 | rx_ct++; |
| 1273 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1274 | if (!ep) { |
| 1275 | |
| 1276 | endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct]; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1277 | rx_cbd[rx_ct]->cbd_sc |= RX_BD_W; |
| 1278 | rx_ct++; |
| 1279 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1280 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1281 | rx_ct += 2; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1282 | endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct]; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1283 | rx_cbd[rx_ct]->cbd_sc |= RX_BD_W; |
| 1284 | rx_ct++; |
| 1285 | } |
| 1286 | |
| 1287 | /* Where we expect to RX data on this endpoint */ |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1288 | ep_ref[ep].prx = rx_cbd[rx_ct - 1]; |
| 1289 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1290 | |
| 1291 | ep_ref[ep].prx = 0; |
| 1292 | endpoints[ep]->rbase = 0; |
| 1293 | endpoints[ep]->rbptr = 0; |
| 1294 | } |
| 1295 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1296 | if (tx_size) { |
| 1297 | endpoints[ep]->tbase = (u32) tx_cbd[tx_ct]; |
| 1298 | endpoints[ep]->tbptr = (u32) tx_cbd[tx_ct]; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1299 | tx_ct++; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1300 | } else { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1301 | endpoints[ep]->tbase = 0; |
| 1302 | endpoints[ep]->tbptr = 0; |
| 1303 | } |
| 1304 | |
| 1305 | endpoints[ep]->tstate = 0; |
| 1306 | endpoints[ep]->tbcnt = 0; |
| 1307 | endpoints[ep]->mrblr = EP_MAX_PKT; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1308 | endpoints[ep]->rfcr = 0x18; |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1309 | endpoints[ep]->tfcr = 0x18; |
| 1310 | ep_ref[ep].sc |= EP_ATTACHED; |
| 1311 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 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, |
| 1315 | ep_ref[ep].prx); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1316 | |
| 1317 | return; |
| 1318 | } |
| 1319 | |
| 1320 | /* mpc8xx_udc_cbd_init |
| 1321 | * |
| 1322 | * Allocate space for a cbd and allocate TX/RX data space |
| 1323 | */ |
| 1324 | static void mpc8xx_udc_cbd_init (void) |
| 1325 | { |
| 1326 | int i = 0; |
| 1327 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1328 | for (; i < TX_RING_SIZE; i++) { |
| 1329 | tx_cbd[i] = (cbd_t *) |
| 1330 | mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int)); |
| 1331 | } |
| 1332 | |
| 1333 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 1334 | rx_cbd[i] = (cbd_t *) |
| 1335 | mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int)); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1336 | } |
| 1337 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1338 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 1339 | tx_cbd[i]->cbd_bufaddr = |
| 1340 | mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int)); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1341 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1342 | tx_cbd[i]->cbd_sc = (TX_BD_I | TX_BD_W); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1343 | tx_cbd[i]->cbd_datlen = 0x0000; |
| 1344 | } |
| 1345 | |
| 1346 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1347 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 1348 | rx_cbd[i]->cbd_bufaddr = |
| 1349 | mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int)); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1350 | rx_cbd[i]->cbd_sc = (RX_BD_I | RX_BD_E); |
| 1351 | rx_cbd[i]->cbd_datlen = 0x0000; |
| 1352 | |
| 1353 | } |
| 1354 | |
| 1355 | return; |
| 1356 | } |
| 1357 | |
| 1358 | /* mpc8xx_udc_endpoint_init |
| 1359 | * |
| 1360 | * Attach an endpoint to some dpram |
| 1361 | */ |
| 1362 | static void mpc8xx_udc_endpoint_init (void) |
| 1363 | { |
| 1364 | int i = 0; |
| 1365 | |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1366 | for (; i < MAX_ENDPOINTS; i++) { |
| 1367 | endpoints[i] = (usb_epb_t *) |
| 1368 | mpc8xx_udc_alloc (sizeof (usb_epb_t), 32); |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | /* mpc8xx_udc_alloc |
| 1373 | * |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1374 | * Grab the address of some dpram |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1375 | */ |
| 1376 | static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment) |
| 1377 | { |
| 1378 | u32 retaddr = address_base; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1379 | |
| 1380 | while (retaddr % alignment) { |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1381 | retaddr++; |
Wolfgang Denk | e260182 | 2006-06-14 18:14:56 +0200 | [diff] [blame] | 1382 | } |
| 1383 | address_base += data_size; |
| 1384 | |
Wolfgang Denk | 3f0137b | 2006-06-14 17:45:53 +0200 | [diff] [blame] | 1385 | return retaddr; |
| 1386 | } |