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