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