blob: 0e311ada156045c6e60b58328cd1997abad7dc16 [file] [log] [blame]
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001/*
2 * Copyright (C) 2006 by Bryan O'Donoghue, CodeHermit
Wolfgang Denke2601822006-06-14 18:14:56 +02003 * bodonoghue@CodeHermit.ie
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02004 *
5 * References
Marcel Ziswileraea68562007-12-30 03:30:46 +01006 * DasUBoot/drivers/usb/usbdcore_omap1510.c, for design and implementation
7 * ideas.
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02008 *
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 Denke2601822006-06-14 18:14:56 +020016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020017 * 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 Denke2601822006-06-14 18:14:56 +020028 * 1. #define __SIMULATE_ERROR__ to inject a CRC error into every 2nd TX
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020029 * packet to force the USB re-transmit protocol.
30 *
31 * 2. #define __DEBUG_UDC__ to switch on debug tracing to serial console
Wolfgang Denke2601822006-06-14 18:14:56 +020032 * be careful that tracing doesn't create Hiesen-bugs with respect to
33 * response timeouts to control requests.
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020034 *
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 Denke2601822006-06-14 18:14:56 +020045 * the same size is reported for 14 bytes received as 16 bytes received
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020046 * 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 Denke2601822006-06-14 18:14:56 +020050 * 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 Denk3f0137b2006-06-14 17:45:53 +020057 *
58 */
59#include <common.h>
60#include <config.h>
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020061#include <commproc.h>
Wolfgang Denke2601822006-06-14 18:14:56 +020062#include "usbdcore.h"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020063#include "usbdcore_mpc8xx.h"
64#include "usbdcore_ep0.h"
65
Wolfgang Denkd112a2c2007-09-15 20:48:41 +020066DECLARE_GLOBAL_DATA_PTR;
67
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020068#define ERR(fmt, args...)\
69 serial_printf("ERROR : [%s] %s:%d: "fmt,\
70 __FILE__,__FUNCTION__,__LINE__, ##args)
71#ifdef __DEBUG_UDC__
Wolfgang Denke2601822006-06-14 18:14:56 +020072#define DBG(fmt,args...)\
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020073 serial_printf("[%s] %s:%d: "fmt,\
74 __FILE__,__FUNCTION__,__LINE__, ##args)
75#else
Wolfgang Denke2601822006-06-14 18:14:56 +020076#define DBG(fmt,args...)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020077#endif
78
79/* Static Data */
80#ifdef __SIMULATE_ERROR__
Wolfgang Denke2601822006-06-14 18:14:56 +020081static char err_poison_test = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020082#endif
83static struct mpc8xx_ep ep_ref[MAX_ENDPOINTS];
84static u32 address_base = STATE_NOT_READY;
85static mpc8xx_udc_state_t udc_state = 0;
86static struct usb_device_instance *udc_device = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +020087static volatile usb_epb_t *endpoints[MAX_ENDPOINTS];
88static volatile cbd_t *tx_cbd[TX_RING_SIZE];
89static volatile cbd_t *rx_cbd[RX_RING_SIZE];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020090static volatile immap_t *immr = 0;
91static volatile cpm8xx_t *cp = 0;
92static volatile usb_pram_t *usb_paramp = 0;
93static volatile usb_t *usbp = 0;
94static int rx_ct = 0;
95static int tx_ct = 0;
96
97/* Static Function Declarations */
98static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
Wolfgang Denke2601822006-06-14 18:14:56 +020099 usb_device_state_t final);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200100static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
Wolfgang Denke2601822006-06-14 18:14:56 +0200101 usb_device_state_t final);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200102static void mpc8xx_udc_stall (unsigned int ep);
Wolfgang Denke2601822006-06-14 18:14:56 +0200103static void mpc8xx_udc_flush_tx_fifo (int epid);
104static void mpc8xx_udc_flush_rx_fifo (void);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200105static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp);
Wolfgang Denke2601822006-06-14 18:14:56 +0200106static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
107 struct urb *tx_urb);
108static void mpc8xx_udc_dump_request (struct usb_device_request *request);
109static void mpc8xx_udc_clock_init (volatile immap_t * immr,
110 volatile cpm8xx_t * cp);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200111static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi);
112static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp);
Wolfgang Denke2601822006-06-14 18:14:56 +0200113static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200114static void mpc8xx_udc_cbd_init (void);
115static void mpc8xx_udc_endpoint_init (void);
116static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size);
117static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment);
118static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp);
119static void mpc8xx_udc_set_nak (unsigned int ep);
Wolfgang Denke2601822006-06-14 18:14:56 +0200120static short mpc8xx_udc_handle_txerr (void);
121static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200122
123/******************************************************************************
Wolfgang Denke2601822006-06-14 18:14:56 +0200124 Global Linkage
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200125 *****************************************************************************/
126
127/* udc_init
128 *
129 * Do initial bus gluing
130 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200131int udc_init (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200132{
133 /* Init various pointers */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200134 immr = (immap_t *) CONFIG_SYS_IMMR;
Wolfgang Denke2601822006-06-14 18:14:56 +0200135 cp = (cpm8xx_t *) & (immr->im_cpm);
136 usb_paramp = (usb_pram_t *) & (cp->cp_dparam[PROFF_USB]);
137 usbp = (usb_t *) & (cp->cp_scc[0]);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200138
Wolfgang Denke2601822006-06-14 18:14:56 +0200139 memset (ep_ref, 0x00, (sizeof (struct mpc8xx_ep) * MAX_ENDPOINTS));
140
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200141 udc_device = 0;
142 udc_state = STATE_NOT_READY;
Wolfgang Denke2601822006-06-14 18:14:56 +0200143
144 usbp->usmod = 0x00;
145 usbp->uscom = 0;
146
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200147 /* Set USB Frame #0, Respond at Address & Get a clock source */
148 usbp->usaddr = 0x00;
149 mpc8xx_udc_clock_init (immr, cp);
Wolfgang Denke2601822006-06-14 18:14:56 +0200150
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200151 /* PA15, PA14 as perhiperal USBRXD and USBOE */
Wolfgang Denke2601822006-06-14 18:14:56 +0200152 immr->im_ioport.iop_padir &= ~0x0003;
153 immr->im_ioport.iop_papar |= 0x0003;
154
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200155 /* PC11/PC10 as peripheral USBRXP USBRXN */
Wolfgang Denke2601822006-06-14 18:14:56 +0200156 immr->im_ioport.iop_pcso |= 0x0030;
157
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200158 /* PC7/PC6 as perhiperal USBTXP and USBTXN */
Wolfgang Denke2601822006-06-14 18:14:56 +0200159 immr->im_ioport.iop_pcdir |= 0x0300;
160 immr->im_ioport.iop_pcpar |= 0x0300;
161
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200162 /* Set the base address */
Wolfgang Denke2601822006-06-14 18:14:56 +0200163 address_base = (u32) (cp->cp_dpmem + CPM_USB_BASE);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200164
165 /* Initialise endpoints and circular buffers */
Wolfgang Denke2601822006-06-14 18:14:56 +0200166 mpc8xx_udc_endpoint_init ();
167 mpc8xx_udc_cbd_init ();
168
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200169 /* Assign allocated Dual Port Endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200170 usb_paramp->ep0ptr = (u32) endpoints[0];
171 usb_paramp->ep1ptr = (u32) endpoints[1];
172 usb_paramp->ep2ptr = (u32) endpoints[2];
173 usb_paramp->ep3ptr = (u32) endpoints[3];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200174 usb_paramp->frame_n = 0;
175
Wolfgang Denke2601822006-06-14 18:14:56 +0200176 DBG ("ep0ptr=0x%08x ep1ptr=0x%08x ep2ptr=0x%08x ep3ptr=0x%08x\n",
177 usb_paramp->ep0ptr, usb_paramp->ep1ptr, usb_paramp->ep2ptr,
178 usb_paramp->ep3ptr);
179
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200180 return 0;
181}
182
183/* udc_irq
184 *
185 * Poll for whatever events may have occured
186 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200187void udc_irq (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200188{
189 int epid = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200190 volatile cbd_t *rx_cbdp = 0;
191 volatile cbd_t *rx_cbdp_base = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200192
Wolfgang Denke2601822006-06-14 18:14:56 +0200193 if (udc_state != STATE_READY) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200194 return;
195 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200196
197 if (usbp->usber & USB_E_BSY) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200198 /* This shouldn't happen. If it does then it's a bug ! */
Wolfgang Denke2601822006-06-14 18:14:56 +0200199 usbp->usber |= USB_E_BSY;
200 mpc8xx_udc_flush_rx_fifo ();
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200201 }
202
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200203 /* Scan all RX/Bidirectional Endpoints for RX data. */
Wolfgang Denke2601822006-06-14 18:14:56 +0200204 for (epid = 0; epid < MAX_ENDPOINTS; epid++) {
205 if (!ep_ref[epid].prx) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200206 continue;
207 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200208 rx_cbdp = rx_cbdp_base = ep_ref[epid].prx;
Wolfgang Denke2601822006-06-14 18:14:56 +0200209
210 do {
211 if (!(rx_cbdp->cbd_sc & RX_BD_E)) {
212
213 if (rx_cbdp->cbd_sc & 0x1F) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200214 /* Corrupt data discard it.
Wolfgang Denke2601822006-06-14 18:14:56 +0200215 * Controller has NAK'd this packet.
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200216 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200217 mpc8xx_udc_clear_rxbd (rx_cbdp);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200218
Wolfgang Denke2601822006-06-14 18:14:56 +0200219 } else {
220 if (!epid) {
221 mpc8xx_udc_ep0_rx (rx_cbdp);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200222
Wolfgang Denke2601822006-06-14 18:14:56 +0200223 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200224 /* Process data */
Wolfgang Denke2601822006-06-14 18:14:56 +0200225 mpc8xx_udc_set_nak (epid);
226 mpc8xx_udc_epn_rx (epid, rx_cbdp);
227 mpc8xx_udc_clear_rxbd (rx_cbdp);
228 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200229 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200230
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200231 /* Advance RX CBD pointer */
Wolfgang Denke2601822006-06-14 18:14:56 +0200232 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200233 ep_ref[epid].prx = rx_cbdp;
Wolfgang Denke2601822006-06-14 18:14:56 +0200234 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200235 /* Advance RX CBD pointer */
Wolfgang Denke2601822006-06-14 18:14:56 +0200236 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200237 }
238
Wolfgang Denke2601822006-06-14 18:14:56 +0200239 } while (rx_cbdp != rx_cbdp_base);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200240 }
241
242 /* Handle TX events as appropiate, the correct place to do this is
243 * in a tx routine. Perhaps TX on epn was pre-empted by ep0
244 */
245
Wolfgang Denke2601822006-06-14 18:14:56 +0200246 if (usbp->usber & USB_E_TXB) {
247 usbp->usber |= USB_E_TXB;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200248 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200249
250 if (usbp->usber & (USB_TX_ERRMASK)) {
251 mpc8xx_udc_handle_txerr ();
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200252 }
253
254 /* Switch to the default state, respond at the default address */
Wolfgang Denke2601822006-06-14 18:14:56 +0200255 if (usbp->usber & USB_E_RESET) {
256 usbp->usber |= USB_E_RESET;
257 usbp->usaddr = 0x00;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200258 udc_device->device_state = STATE_DEFAULT;
259 }
260
Wolfgang Denke2601822006-06-14 18:14:56 +0200261 /* if(usbp->usber&USB_E_IDLE){
262 We could suspend here !
263 usbp->usber|=USB_E_IDLE;
264 DBG("idle state change\n");
265 }
266 if(usbp->usbs){
267 We could resume here when IDLE is deasserted !
268 Not worth doing, so long as we are self powered though.
269 }
270 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200271
272 return;
273}
274
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200275/* udc_endpoint_write
276 *
277 * Write some data to an endpoint
278 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200279int udc_endpoint_write (struct usb_endpoint_instance *epi)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200280{
281 int ep = 0;
282 short epid = 1, unnak = 0, ret = 0;
283
Wolfgang Denke2601822006-06-14 18:14:56 +0200284 if (udc_state != STATE_READY) {
285 ERR ("invalid udc_state != STATE_READY!\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200286 return -1;
287 }
288
Wolfgang Denke2601822006-06-14 18:14:56 +0200289 if (!udc_device || !epi) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200290 return -1;
291 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200292
293 if (udc_device->device_state != STATE_CONFIGURED) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200294 return -1;
295 }
296
297 ep = epi->endpoint_address & 0x03;
Wolfgang Denke2601822006-06-14 18:14:56 +0200298 if (ep >= MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200299 return -1;
300 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200301
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200302 /* Set NAK for all RX endpoints during TX */
Wolfgang Denke2601822006-06-14 18:14:56 +0200303 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200304
305 /* Don't set NAK on DATA IN/CONTROL endpoints */
Wolfgang Denke2601822006-06-14 18:14:56 +0200306 if (ep_ref[epid].sc & USB_DIR_IN) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200307 continue;
308 }
309
Wolfgang Denke2601822006-06-14 18:14:56 +0200310 if (!(usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK))) {
311 unnak |= 1 << epid;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200312 }
313
Wolfgang Denke2601822006-06-14 18:14:56 +0200314 mpc8xx_udc_set_nak (epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200315 }
316
Wolfgang Denke2601822006-06-14 18:14:56 +0200317 mpc8xx_udc_init_tx (&udc_device->bus->endpoint_array[ep],
318 epi->tx_urb);
319 ret = mpc8xx_udc_ep_tx (&udc_device->bus->endpoint_array[ep]);
320
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200321 /* Remove temporary NAK */
Wolfgang Denke2601822006-06-14 18:14:56 +0200322 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
323 if (unnak & (1 << epid)) {
324 udc_unset_nak (epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200325 }
326 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200327
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200328 return ret;
329}
330
331/* mpc8xx_udc_assign_urb
332 *
333 * Associate a given urb to an endpoint TX or RX transmit/receive buffers
334 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200335static int mpc8xx_udc_assign_urb (int ep, char direction)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200336{
337 struct usb_endpoint_instance *epi = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200338
339 if (ep >= MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200340 goto err;
341 }
342 epi = &udc_device->bus->endpoint_array[ep];
Wolfgang Denke2601822006-06-14 18:14:56 +0200343 if (!epi) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200344 goto err;
345 }
346
Wolfgang Denke2601822006-06-14 18:14:56 +0200347 if (!ep_ref[ep].urb) {
348 ep_ref[ep].urb = usbd_alloc_urb (udc_device, udc_device->bus->endpoint_array);
349 if (!ep_ref[ep].urb) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200350 goto err;
351 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200352 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200353 ep_ref[ep].urb->actual_length = 0;
354 }
355
Wolfgang Denke2601822006-06-14 18:14:56 +0200356 switch (direction) {
357 case USB_DIR_IN:
358 epi->tx_urb = ep_ref[ep].urb;
359 break;
360 case USB_DIR_OUT:
361 epi->rcv_urb = ep_ref[ep].urb;
362 break;
363 default:
364 goto err;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200365 }
366 return 0;
367
Wolfgang Denke2601822006-06-14 18:14:56 +0200368 err:
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200369 udc_state = STATE_ERROR;
370 return -1;
371}
372
373/* udc_setup_ep
374 *
375 * Associate U-Boot software endpoints to mpc8xx endpoint parameter ram
376 * Isochronous endpoints aren't yet supported!
377 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200378void udc_setup_ep (struct usb_device_instance *device, unsigned int ep,
379 struct usb_endpoint_instance *epi)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200380{
381 uchar direction = 0;
382 int ep_attrib = 0;
383
Wolfgang Denke2601822006-06-14 18:14:56 +0200384 if (epi && (ep < MAX_ENDPOINTS)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200385
Wolfgang Denke2601822006-06-14 18:14:56 +0200386 if (ep == 0) {
387 if (epi->rcv_attributes != USB_ENDPOINT_XFER_CONTROL
388 || epi->tx_attributes !=
389 USB_ENDPOINT_XFER_CONTROL) {
390
391 /* ep0 must be a control endpoint */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200392 udc_state = STATE_ERROR;
393 return;
394
395 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200396 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
397 mpc8xx_udc_cbd_attach (ep, epi->tx_packetSize,
398 epi->rcv_packetSize);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200399 }
400 usbp->usep[ep] = 0x0000;
401 return;
402 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200403
404 if ((epi->endpoint_address & USB_ENDPOINT_DIR_MASK)
405 == USB_DIR_IN) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200406
407 direction = 1;
408 ep_attrib = epi->tx_attributes;
409 epi->rcv_packetSize = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200410 ep_ref[ep].sc |= USB_DIR_IN;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200411 } else {
Wolfgang Denke2601822006-06-14 18:14:56 +0200412
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200413 direction = 0;
414 ep_attrib = epi->rcv_attributes;
Wolfgang Denke2601822006-06-14 18:14:56 +0200415 epi->tx_packetSize = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200416 ep_ref[ep].sc &= ~USB_DIR_IN;
417 }
418
Wolfgang Denke2601822006-06-14 18:14:56 +0200419 if (mpc8xx_udc_assign_urb (ep, epi->endpoint_address
420 & USB_ENDPOINT_DIR_MASK)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200421 return;
422 }
423
Wolfgang Denke2601822006-06-14 18:14:56 +0200424 switch (ep_attrib) {
425 case USB_ENDPOINT_XFER_CONTROL:
426 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
427 mpc8xx_udc_cbd_attach (ep,
428 epi->tx_packetSize,
429 epi->rcv_packetSize);
430 }
431 usbp->usep[ep] = ep << 12;
432 epi->rcv_urb = epi->tx_urb = ep_ref[ep].urb;
433
434 break;
435 case USB_ENDPOINT_XFER_BULK:
436 case USB_ENDPOINT_XFER_INT:
437 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
438 if (direction) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200439 mpc8xx_udc_cbd_attach (ep,
Wolfgang Denke2601822006-06-14 18:14:56 +0200440 epi->tx_packetSize,
441 0);
442 } else {
443 mpc8xx_udc_cbd_attach (ep,
444 0,
445 epi->rcv_packetSize);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200446 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200447 }
448 usbp->usep[ep] = (ep << 12) | ((ep_attrib) << 8);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200449
Wolfgang Denke2601822006-06-14 18:14:56 +0200450 break;
451 case USB_ENDPOINT_XFER_ISOC:
452 default:
453 serial_printf ("Error endpoint attrib %d>3\n", ep_attrib);
454 udc_state = STATE_ERROR;
455 break;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200456 }
457 }
458
459}
460
461/* udc_connect
462 *
463 * Move state, switch on the USB
464 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200465void udc_connect (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200466{
Wolfgang Denke2601822006-06-14 18:14:56 +0200467 /* Enable pull-up resistor on D+
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200468 * TODO: fit a pull-up resistor to drive SE0 for > 2.5us
469 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200470
471 if (udc_state != STATE_ERROR) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200472 udc_state = STATE_READY;
Wolfgang Denke2601822006-06-14 18:14:56 +0200473 usbp->usmod |= USMOD_EN;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200474 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200475}
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200476
477/* udc_disconnect
478 *
479 * Disconnect is not used but, is included for completeness
480 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200481void udc_disconnect (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200482{
483 /* Disable pull-up resistor on D-
484 * TODO: fix a pullup resistor to control this
485 */
486
Wolfgang Denke2601822006-06-14 18:14:56 +0200487 if (udc_state != STATE_ERROR) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200488 udc_state = STATE_NOT_READY;
489 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200490 usbp->usmod &= ~USMOD_EN;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200491}
492
493/* udc_enable
Wolfgang Denke2601822006-06-14 18:14:56 +0200494 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200495 * Grab an EP0 URB, register interest in a subset of USB events
496 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200497void udc_enable (struct usb_device_instance *device)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200498{
Wolfgang Denke2601822006-06-14 18:14:56 +0200499 if (udc_state == STATE_ERROR) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200500 return;
501 }
502
503 udc_device = device;
Wolfgang Denke2601822006-06-14 18:14:56 +0200504
505 if (!ep_ref[0].urb) {
506 ep_ref[0].urb = usbd_alloc_urb (device, device->bus->endpoint_array);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200507 }
508
509 /* Register interest in all events except SOF, enable transceiver */
Wolfgang Denke2601822006-06-14 18:14:56 +0200510 usbp->usber = 0x03FF;
511 usbp->usbmr = 0x02F7;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200512
513 return;
514}
515
516/* udc_disable
517 *
518 * disable the currently hooked device
519 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200520void udc_disable (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200521{
522 int i = 0;
523
Wolfgang Denke2601822006-06-14 18:14:56 +0200524 if (udc_state == STATE_ERROR) {
525 DBG ("Won't disable UDC. udc_state==STATE_ERROR !\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200526 return;
527 }
528
529 udc_device = 0;
530
Wolfgang Denke2601822006-06-14 18:14:56 +0200531 for (; i < MAX_ENDPOINTS; i++) {
532 if (ep_ref[i].urb) {
533 usbd_dealloc_urb (ep_ref[i].urb);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200534 ep_ref[i].urb = 0;
535 }
536 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200537
538 usbp->usbmr = 0x00;
539 usbp->usmod = ~USMOD_EN;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200540 udc_state = STATE_NOT_READY;
541}
542
543/* udc_startup_events
544 *
545 * Enable the specified device
546 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200547void udc_startup_events (struct usb_device_instance *device)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200548{
Wolfgang Denke2601822006-06-14 18:14:56 +0200549 udc_enable (device);
550 if (udc_state == STATE_READY) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200551 usbd_device_event_irq (device, DEVICE_CREATE, 0);
552 }
553}
554
555/* udc_set_nak
Wolfgang Denke2601822006-06-14 18:14:56 +0200556 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200557 * Allow upper layers to signal lower layers should not accept more RX data
558 *
559 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200560void udc_set_nak (int epid)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200561{
Wolfgang Denke2601822006-06-14 18:14:56 +0200562 if (epid) {
563 mpc8xx_udc_set_nak (epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200564 }
565}
566
Wolfgang Denke2601822006-06-14 18:14:56 +0200567/* udc_unset_nak
568 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200569 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
570 * Switch off NAKing on this endpoint to accept more data output from host.
571 *
572 */
573void udc_unset_nak (int epid)
574{
Wolfgang Denke2601822006-06-14 18:14:56 +0200575 if (epid > MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200576 return;
577 }
578
Wolfgang Denke2601822006-06-14 18:14:56 +0200579 if (usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK)) {
580 usbp->usep[epid] &= ~(USEP_THS_NAK | USEP_RHS_NAK);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200581 __asm__ ("eieio");
582 }
583}
584
585/******************************************************************************
Wolfgang Denke2601822006-06-14 18:14:56 +0200586 Static Linkage
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200587******************************************************************************/
588
589/* udc_state_transition_up
590 * udc_state_transition_down
591 *
592 * Helper functions to implement device state changes. The device states and
593 * the events that transition between them are:
594 *
595 * STATE_ATTACHED
596 * || /\
597 * \/ ||
598 * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
599 * || /\
600 * \/ ||
601 * STATE_POWERED
602 * || /\
603 * \/ ||
604 * DEVICE_RESET DEVICE_POWER_INTERRUPTION
605 * || /\
606 * \/ ||
607 * STATE_DEFAULT
608 * || /\
609 * \/ ||
610 * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
611 * || /\
612 * \/ ||
613 * STATE_ADDRESSED
614 * || /\
615 * \/ ||
616 * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
617 * || /\
618 * \/ ||
619 * STATE_CONFIGURED
620 *
621 * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
622 * to STATE_CONFIGURED) from the specified initial state to the specified final
623 * state, passing through each intermediate state on the way. If the initial
624 * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
625 * no state transitions will take place.
626 *
627 * udc_state_transition_down transitions down (in the direction from
628 * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
629 * specified final state, passing through each intermediate state on the way.
630 * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
631 * state, then no state transitions will take place.
632 *
633 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200634
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200635static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
Wolfgang Denke2601822006-06-14 18:14:56 +0200636 usb_device_state_t final)
637{
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200638 if (initial < final) {
639 switch (initial) {
640 case STATE_ATTACHED:
641 usbd_device_event_irq (udc_device,
642 DEVICE_HUB_CONFIGURED, 0);
643 if (final == STATE_POWERED)
644 break;
645 case STATE_POWERED:
646 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
647 if (final == STATE_DEFAULT)
648 break;
649 case STATE_DEFAULT:
650 usbd_device_event_irq (udc_device,
651 DEVICE_ADDRESS_ASSIGNED, 0);
652 if (final == STATE_ADDRESSED)
653 break;
654 case STATE_ADDRESSED:
655 usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
656 0);
657 case STATE_CONFIGURED:
658 break;
659 default:
660 break;
661 }
662 }
663}
664
665static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
Wolfgang Denke2601822006-06-14 18:14:56 +0200666 usb_device_state_t final)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200667{
668 if (initial > final) {
669 switch (initial) {
670 case STATE_CONFIGURED:
Wolfgang Denke2601822006-06-14 18:14:56 +0200671 usbd_device_event_irq (udc_device,
672 DEVICE_DE_CONFIGURED, 0);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200673 if (final == STATE_ADDRESSED)
674 break;
675 case STATE_ADDRESSED:
676 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
677 if (final == STATE_DEFAULT)
678 break;
679 case STATE_DEFAULT:
Wolfgang Denke2601822006-06-14 18:14:56 +0200680 usbd_device_event_irq (udc_device,
681 DEVICE_POWER_INTERRUPTION, 0);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200682 if (final == STATE_POWERED)
683 break;
684 case STATE_POWERED:
685 usbd_device_event_irq (udc_device, DEVICE_HUB_RESET,
Wolfgang Denke2601822006-06-14 18:14:56 +0200686 0);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200687 case STATE_ATTACHED:
688 break;
689 default:
690 break;
691 }
692 }
693}
694
695/* mpc8xx_udc_stall
696 *
697 * Force returning of STALL tokens on the given endpoint. Protocol or function
698 * STALL conditions are permissable here
699 */
700static void mpc8xx_udc_stall (unsigned int ep)
701{
702 usbp->usep[ep] |= STALL_BITMASK;
703}
704
705/* mpc8xx_udc_set_nak
706 *
707 * Force returning of NAK responses for the given endpoint as a kind of very
708 * simple flow control
Wolfgang Denke2601822006-06-14 18:14:56 +0200709 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200710static void mpc8xx_udc_set_nak (unsigned int ep)
711{
712 usbp->usep[ep] |= NAK_BITMASK;
713 __asm__ ("eieio");
714}
715
716/* mpc8xx_udc_handle_txerr
717 *
718 * Handle errors relevant to TX. Return a status code to allow calling
719 * indicative of what if anything happened
720 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200721static short mpc8xx_udc_handle_txerr ()
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200722{
723 short ep = 0, ret = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200724
725 for (; ep < TX_RING_SIZE; ep++) {
726 if (usbp->usber & (0x10 << ep)) {
727
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200728 /* Timeout or underrun */
Wolfgang Denke2601822006-06-14 18:14:56 +0200729 if (tx_cbd[ep]->cbd_sc & 0x06) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200730 ret = 1;
Wolfgang Denke2601822006-06-14 18:14:56 +0200731 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200732
Wolfgang Denke2601822006-06-14 18:14:56 +0200733 } else {
734 if (usbp->usep[ep] & STALL_BITMASK) {
735 if (!ep) {
736 usbp->usep[ep] &= ~STALL_BITMASK;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200737 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200738 } /* else NAK */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200739 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200740 usbp->usber |= (0x10 << ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200741 }
742 }
743 return ret;
744}
745
746/* mpc8xx_udc_advance_rx
747 *
748 * Advance cbd rx
749 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200750static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200751{
Wolfgang Denke2601822006-06-14 18:14:56 +0200752 if ((*rx_cbdp)->cbd_sc & RX_BD_W) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200753 *rx_cbdp = (volatile cbd_t *) (endpoints[epid]->rbase + CONFIG_SYS_IMMR);
Wolfgang Denke2601822006-06-14 18:14:56 +0200754
755 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200756 (*rx_cbdp)++;
757 }
758}
759
760
761/* mpc8xx_udc_flush_tx_fifo
762 *
763 * Flush a given TX fifo. Assumes one tx cbd per endpoint
764 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200765static void mpc8xx_udc_flush_tx_fifo (int epid)
766{
767 volatile cbd_t *tx_cbdp = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200768
Wolfgang Denke2601822006-06-14 18:14:56 +0200769 if (epid > MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200770 return;
771 }
772
773 /* TX stop */
Wolfgang Denke2601822006-06-14 18:14:56 +0200774 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x1D01);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200775 __asm__ ("eieio");
Wolfgang Denke2601822006-06-14 18:14:56 +0200776 while (immr->im_cpm.cp_cpcr & 0x01);
777
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200778 usbp->uscom = 0x40 | 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200779
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200780 /* reset ring */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200781 tx_cbdp = (cbd_t *) (endpoints[epid]->tbptr + CONFIG_SYS_IMMR);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200782 tx_cbdp->cbd_sc = (TX_BD_I | TX_BD_W);
783
Wolfgang Denke2601822006-06-14 18:14:56 +0200784
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200785 endpoints[epid]->tptr = endpoints[epid]->tbase;
Wolfgang Denke2601822006-06-14 18:14:56 +0200786 endpoints[epid]->tstate = 0x00;
787 endpoints[epid]->tbcnt = 0x00;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200788
789 /* TX start */
Wolfgang Denke2601822006-06-14 18:14:56 +0200790 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x2D01);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200791 __asm__ ("eieio");
Wolfgang Denke2601822006-06-14 18:14:56 +0200792 while (immr->im_cpm.cp_cpcr & 0x01);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200793
794 return;
795}
796
797/* mpc8xx_udc_flush_rx_fifo
798 *
799 * For the sake of completeness of the namespace, it seems like
800 * a good-design-decision (tm) to include mpc8xx_udc_flush_rx_fifo();
801 * If RX_BD_E is true => a driver bug either here or in an upper layer
802 * not polling frequently enough. If RX_BD_E is true we have told the host
803 * we have accepted data but, the CPM found it had no-where to put that data
804 * which needless to say would be a bad thing.
805 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200806static void mpc8xx_udc_flush_rx_fifo ()
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200807{
808 int i = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200809
810 for (i = 0; i < RX_RING_SIZE; i++) {
811 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
812 ERR ("buf %p used rx data len = 0x%x sc=0x%x!\n",
813 rx_cbd[i], rx_cbd[i]->cbd_datlen,
814 rx_cbd[i]->cbd_sc);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200815
816 }
817 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200818 ERR ("BUG : Input over-run\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200819}
820
821/* mpc8xx_udc_clear_rxbd
Wolfgang Denke2601822006-06-14 18:14:56 +0200822 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200823 * Release control of RX CBD to CP.
824 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200825static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200826{
827 rx_cbdp->cbd_datlen = 0x0000;
Wolfgang Denke2601822006-06-14 18:14:56 +0200828 rx_cbdp->cbd_sc = ((rx_cbdp->cbd_sc & RX_BD_W) | (RX_BD_E | RX_BD_I));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200829 __asm__ ("eieio");
830}
831
832/* mpc8xx_udc_tx_irq
833 *
834 * Parse for tx timeout, control RX or USB reset/busy conditions
835 * Return -1 on timeout, -2 on fatal error, else return zero
836 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200837static int mpc8xx_udc_tx_irq (int ep)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200838{
839 int i = 0;
840
Wolfgang Denke2601822006-06-14 18:14:56 +0200841 if (usbp->usber & (USB_TX_ERRMASK)) {
842 if (mpc8xx_udc_handle_txerr ()) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200843 /* Timeout, controlling function must retry send */
844 return -1;
845 }
846 }
847
Wolfgang Denke2601822006-06-14 18:14:56 +0200848 if (usbp->usber & (USB_E_RESET | USB_E_BSY)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200849 /* Fatal, abandon TX transaction */
850 return -2;
851 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200852
853 if (usbp->usber & USB_E_RXB) {
854 for (i = 0; i < RX_RING_SIZE; i++) {
855 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
856 if ((rx_cbd[i] == ep_ref[0].prx) || ep) {
857 return -2;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200858 }
859 }
860 }
861 }
862
863 return 0;
864}
865
866/* mpc8xx_udc_ep_tx
867 *
868 * Transmit in a re-entrant fashion outbound USB packets.
869 * Implement retry/timeout mechanism described in USB specification
870 * Toggle DATA0/DATA1 pids as necessary
871 * Introduces non-standard tx_retry. The USB standard has no scope for slave
872 * devices to give up TX, however tx_retry stops us getting stuck in an endless
873 * TX loop.
874 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200875static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200876{
877 struct urb *urb = epi->tx_urb;
Wolfgang Denke2601822006-06-14 18:14:56 +0200878 volatile cbd_t *tx_cbdp = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200879 unsigned int ep = 0, pkt_len = 0, x = 0, tx_retry = 0;
880 int ret = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200881
882 if (!epi || (epi->endpoint_address & 0x03) >= MAX_ENDPOINTS || !urb) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200883 return -1;
884 }
885
886 ep = epi->endpoint_address & 0x03;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200887 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
Wolfgang Denke2601822006-06-14 18:14:56 +0200888
889 if (tx_cbdp->cbd_sc & TX_BD_R || usbp->usber & USB_E_TXB) {
890 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200891 usbp->usber |= USB_E_TXB;
892 };
893
Wolfgang Denke2601822006-06-14 18:14:56 +0200894 while (tx_retry++ < 100) {
895 ret = mpc8xx_udc_tx_irq (ep);
896 if (ret == -1) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200897 /* ignore timeout here */
Wolfgang Denke2601822006-06-14 18:14:56 +0200898 } else if (ret == -2) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200899 /* Abandon TX */
Wolfgang Denke2601822006-06-14 18:14:56 +0200900 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200901 return -1;
Wolfgang Denke2601822006-06-14 18:14:56 +0200902 }
903
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200904 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
Wolfgang Denke2601822006-06-14 18:14:56 +0200905 while (tx_cbdp->cbd_sc & TX_BD_R) {
906 };
907 tx_cbdp->cbd_sc = (tx_cbdp->cbd_sc & TX_BD_W);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200908
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200909 pkt_len = urb->actual_length - epi->sent;
910
Wolfgang Denke2601822006-06-14 18:14:56 +0200911 if (pkt_len > epi->tx_packetSize || pkt_len > EP_MAX_PKT) {
912 pkt_len = MIN (epi->tx_packetSize, EP_MAX_PKT);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200913 }
914
Wolfgang Denke2601822006-06-14 18:14:56 +0200915 for (x = 0; x < pkt_len; x++) {
916 *((unsigned char *) (tx_cbdp->cbd_bufaddr + x)) =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200917 urb->buffer[epi->sent + x];
918 }
919 tx_cbdp->cbd_datlen = pkt_len;
Wolfgang Denke2601822006-06-14 18:14:56 +0200920 tx_cbdp->cbd_sc |= (CBD_TX_BITMASK | ep_ref[ep].pid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200921 __asm__ ("eieio");
922
Wolfgang Denke2601822006-06-14 18:14:56 +0200923#ifdef __SIMULATE_ERROR__
924 if (++err_poison_test == 2) {
925 err_poison_test = 0;
926 tx_cbdp->cbd_sc &= ~TX_BD_TC;
927 }
928#endif
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200929
Wolfgang Denke2601822006-06-14 18:14:56 +0200930 usbp->uscom = (USCOM_STR | ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200931
Wolfgang Denke2601822006-06-14 18:14:56 +0200932 while (!(usbp->usber & USB_E_TXB)) {
933 ret = mpc8xx_udc_tx_irq (ep);
934 if (ret == -1) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200935 /* TX timeout */
936 break;
Wolfgang Denke2601822006-06-14 18:14:56 +0200937 } else if (ret == -2) {
938 if (usbp->usber & USB_E_TXB) {
939 usbp->usber |= USB_E_TXB;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200940 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200941 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200942 return -1;
943 }
944 };
945
Wolfgang Denke2601822006-06-14 18:14:56 +0200946 if (usbp->usber & USB_E_TXB) {
947 usbp->usber |= USB_E_TXB;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200948 }
949
950 /* ACK must be present <= 18bit times from TX */
Wolfgang Denke2601822006-06-14 18:14:56 +0200951 if (ret == -1) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200952 continue;
953 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200954
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200955 /* TX ACK : USB 2.0 8.7.2, Toggle PID, Advance TX */
956 epi->sent += pkt_len;
Wolfgang Denke2601822006-06-14 18:14:56 +0200957 epi->last = MIN (urb->actual_length - epi->sent, epi->tx_packetSize);
958 TOGGLE_TX_PID (ep_ref[ep].pid);
959
960 if (epi->sent >= epi->tx_urb->actual_length) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200961
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200962 epi->tx_urb->actual_length = 0;
963 epi->sent = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200964
965 if (ep_ref[ep].sc & EP_SEND_ZLP) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200966 ep_ref[ep].sc &= ~EP_SEND_ZLP;
Wolfgang Denke2601822006-06-14 18:14:56 +0200967 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200968 return 0;
969 }
970 }
971 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200972
973 ERR ("TX fail, endpoint 0x%x tx bytes 0x%x/0x%x\n", ep, epi->sent,
974 epi->tx_urb->actual_length);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200975
976 return -1;
977}
978
979/* mpc8xx_udc_dump_request
980 *
981 * Dump a control request to console
982 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200983static void mpc8xx_udc_dump_request (struct usb_device_request *request)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200984{
Wolfgang Denke2601822006-06-14 18:14:56 +0200985 DBG ("bmRequestType:%02x bRequest:%02x wValue:%04x "
986 "wIndex:%04x wLength:%04x ?\n",
987 request->bmRequestType,
988 request->bRequest,
989 request->wValue, request->wIndex, request->wLength);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200990
991 return;
992}
993
Wolfgang Denke2601822006-06-14 18:14:56 +0200994/* mpc8xx_udc_ep0_rx_setup
995 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200996 * Decode received ep0 SETUP packet. return non-zero on error
997 */
998static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp)
999{
1000 unsigned int x = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +02001001 struct urb *purb = ep_ref[0].urb;
1002 struct usb_endpoint_instance *epi =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001003 &udc_device->bus->endpoint_array[0];
1004
Wolfgang Denke2601822006-06-14 18:14:56 +02001005 for (; x < rx_cbdp->cbd_datlen; x++) {
1006 *(((unsigned char *) &ep_ref[0].urb->device_request) + x) =
1007 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001008 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001009
Wolfgang Denke2601822006-06-14 18:14:56 +02001010 mpc8xx_udc_clear_rxbd (rx_cbdp);
1011
1012 if (ep0_recv_setup (purb)) {
1013 mpc8xx_udc_dump_request (&purb->device_request);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001014 return -1;
1015 }
1016
Wolfgang Denke2601822006-06-14 18:14:56 +02001017 if ((purb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001018 == USB_REQ_HOST2DEVICE) {
1019
Wolfgang Denke2601822006-06-14 18:14:56 +02001020 switch (purb->device_request.bRequest) {
1021 case USB_REQ_SET_ADDRESS:
1022 /* Send the Status OUT ZLP */
1023 ep_ref[0].pid = TX_BD_PID_DATA1;
1024 purb->actual_length = 0;
1025 mpc8xx_udc_init_tx (epi, purb);
1026 mpc8xx_udc_ep_tx (epi);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001027
Wolfgang Denke2601822006-06-14 18:14:56 +02001028 /* Move to the addressed state */
1029 usbp->usaddr = udc_device->address;
1030 mpc8xx_udc_state_transition_up (udc_device->device_state,
1031 STATE_ADDRESSED);
1032 return 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001033
Wolfgang Denke2601822006-06-14 18:14:56 +02001034 case USB_REQ_SET_CONFIGURATION:
1035 if (!purb->device_request.wValue) {
1036 /* Respond at default address */
1037 usbp->usaddr = 0x00;
1038 mpc8xx_udc_state_transition_down (udc_device->device_state,
1039 STATE_ADDRESSED);
1040 } else {
1041 /* TODO: Support multiple configurations */
1042 mpc8xx_udc_state_transition_up (udc_device->device_state,
1043 STATE_CONFIGURED);
1044 for (x = 1; x < MAX_ENDPOINTS; x++) {
1045 if ((udc_device->bus->endpoint_array[x].endpoint_address & USB_ENDPOINT_DIR_MASK)
1046 == USB_DIR_IN) {
1047 ep_ref[x].pid = TX_BD_PID_DATA0;
1048 } else {
1049 ep_ref[x].pid = RX_BD_PID_DATA0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001050 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001051 /* Set configuration must unstall endpoints */
1052 usbp->usep[x] &= ~STALL_BITMASK;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001053 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001054 }
1055 break;
1056 default:
1057 /* CDC/Vendor specific */
1058 break;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001059 }
1060
1061 /* Send ZLP as ACK in Status OUT phase */
1062 ep_ref[0].pid = TX_BD_PID_DATA1;
1063 purb->actual_length = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +02001064 mpc8xx_udc_init_tx (epi, purb);
1065 mpc8xx_udc_ep_tx (epi);
1066
1067 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001068
Wolfgang Denke2601822006-06-14 18:14:56 +02001069 if (purb->actual_length) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001070 ep_ref[0].pid = TX_BD_PID_DATA1;
Wolfgang Denke2601822006-06-14 18:14:56 +02001071 mpc8xx_udc_init_tx (epi, purb);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001072
Wolfgang Denke2601822006-06-14 18:14:56 +02001073 if (!(purb->actual_length % EP0_MAX_PACKET_SIZE)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001074 ep_ref[0].sc |= EP_SEND_ZLP;
1075 }
1076
Wolfgang Denke2601822006-06-14 18:14:56 +02001077 if (purb->device_request.wValue ==
1078 USB_DESCRIPTOR_TYPE_DEVICE) {
1079 if (le16_to_cpu (purb->device_request.wLength)
1080 > purb->actual_length) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001081 /* Send EP0_MAX_PACKET_SIZE bytes
1082 * unless correct size requested.
1083 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001084 if (purb->actual_length > epi->tx_packetSize) {
1085 purb->actual_length = epi->tx_packetSize;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001086 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001087 }
1088 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001089 mpc8xx_udc_ep_tx (epi);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001090
Wolfgang Denke2601822006-06-14 18:14:56 +02001091 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001092 /* Corrupt SETUP packet? */
Wolfgang Denke2601822006-06-14 18:14:56 +02001093 ERR ("Zero length data or SETUP with DATA-IN phase ?\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001094 return 1;
1095 }
1096 }
1097 return 0;
1098}
1099
1100/* mpc8xx_udc_init_tx
1101 *
1102 * Setup some basic parameters for a TX transaction
1103 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001104static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
1105 struct urb *tx_urb)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001106{
1107 epi->sent = 0;
1108 epi->last = 0;
1109 epi->tx_urb = tx_urb;
1110}
1111
1112/* mpc8xx_udc_ep0_rx
1113 *
1114 * Receive ep0/control USB data. Parse and possibly send a response.
1115 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001116static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001117{
Wolfgang Denke2601822006-06-14 18:14:56 +02001118 if (rx_cbdp->cbd_sc & RX_BD_PID_SETUP) {
1119
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001120 /* Unconditionally accept SETUP packets */
Wolfgang Denke2601822006-06-14 18:14:56 +02001121 if (mpc8xx_udc_ep0_rx_setup (rx_cbdp)) {
1122 mpc8xx_udc_stall (0);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001123 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001124
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001125 } else {
Wolfgang Denke2601822006-06-14 18:14:56 +02001126
1127 mpc8xx_udc_clear_rxbd (rx_cbdp);
1128
1129 if ((rx_cbdp->cbd_datlen - 2)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001130 /* SETUP with a DATA phase
Wolfgang Denke2601822006-06-14 18:14:56 +02001131 * outside of SETUP packet.
1132 * Reply with STALL.
1133 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001134 mpc8xx_udc_stall (0);
1135 }
1136 }
1137}
1138
1139/* mpc8xx_udc_epn_rx
1140 *
1141 * Receive some data from cbd into USB system urb data abstraction
Wolfgang Denke2601822006-06-14 18:14:56 +02001142 * Upper layers should NAK if there is insufficient RX data space
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001143 */
1144static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp)
1145{
1146 struct usb_endpoint_instance *epi = 0;
1147 struct urb *urb = 0;
1148 unsigned int x = 0;
1149
Wolfgang Denke2601822006-06-14 18:14:56 +02001150 if (epid >= MAX_ENDPOINTS || !rx_cbdp->cbd_datlen) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001151 return 0;
1152 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001153
1154 /* USB 2.0 PDF section 8.6.4
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001155 * Discard data with invalid PID it is a resend.
1156 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001157 if (ep_ref[epid].pid != (rx_cbdp->cbd_sc & 0xC0)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001158 return 1;
1159 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001160 TOGGLE_RX_PID (ep_ref[epid].pid);
1161
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001162 epi = &udc_device->bus->endpoint_array[epid];
1163 urb = epi->rcv_urb;
1164
Wolfgang Denke2601822006-06-14 18:14:56 +02001165 for (; x < (rx_cbdp->cbd_datlen - 2); x++) {
1166 *((unsigned char *) (urb->buffer + urb->actual_length + x)) =
1167 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001168 }
1169
Wolfgang Denke2601822006-06-14 18:14:56 +02001170 if (x) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001171 usbd_rcv_complete (epi, x, 0);
Wolfgang Denke2601822006-06-14 18:14:56 +02001172 if (ep_ref[epid].urb->status == RECV_ERROR) {
1173 DBG ("RX error unset NAK\n");
1174 udc_unset_nak (epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001175 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001176 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001177 return x;
1178}
1179
1180/* mpc8xx_udc_clock_init
1181 *
Wolfgang Denke2601822006-06-14 18:14:56 +02001182 * Obtain a clock reference for Full Speed Signaling
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001183 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001184static void mpc8xx_udc_clock_init (volatile immap_t * immr,
1185 volatile cpm8xx_t * cp)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001186{
1187
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001188#if defined(CONFIG_SYS_USB_EXTC_CLK)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001189
1190 /* This has been tested with a 48MHz crystal on CLK6 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001191 switch (CONFIG_SYS_USB_EXTC_CLK) {
Wolfgang Denke2601822006-06-14 18:14:56 +02001192 case 1:
1193 immr->im_ioport.iop_papar |= 0x0100;
1194 immr->im_ioport.iop_padir &= ~0x0100;
1195 cp->cp_sicr |= 0x24;
1196 break;
1197 case 2:
1198 immr->im_ioport.iop_papar |= 0x0200;
1199 immr->im_ioport.iop_padir &= ~0x0200;
1200 cp->cp_sicr |= 0x2D;
1201 break;
1202 case 3:
1203 immr->im_ioport.iop_papar |= 0x0400;
1204 immr->im_ioport.iop_padir &= ~0x0400;
1205 cp->cp_sicr |= 0x36;
1206 break;
1207 case 4:
1208 immr->im_ioport.iop_papar |= 0x0800;
1209 immr->im_ioport.iop_padir &= ~0x0800;
1210 cp->cp_sicr |= 0x3F;
1211 break;
1212 default:
1213 udc_state = STATE_ERROR;
1214 break;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001215 }
1216
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001217#elif defined(CONFIG_SYS_USB_BRGCLK)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001218
Wolfgang Denke2601822006-06-14 18:14:56 +02001219 /* This has been tested with brgclk == 50MHz */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001220 int divisor = 0;
1221
Wolfgang Denke2601822006-06-14 18:14:56 +02001222 if (gd->cpu_clk < 48000000L) {
1223 ERR ("brgclk is too slow for full-speed USB!\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001224 udc_state = STATE_ERROR;
1225 return;
1226 }
1227
Wolfgang Denkaf0501a2008-10-19 02:35:50 +02001228 /* Assume the brgclk is 'good enough', we want !(gd->cpu_clk%48MHz)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001229 * but, can /probably/ live with close-ish alternative rates.
Wolfgang Denke2601822006-06-14 18:14:56 +02001230 */
1231 divisor = (gd->cpu_clk / 48000000L) - 1;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001232 cp->cp_sicr &= ~0x0000003F;
Wolfgang Denke2601822006-06-14 18:14:56 +02001233
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001234 switch (CONFIG_SYS_USB_BRGCLK) {
Wolfgang Denke2601822006-06-14 18:14:56 +02001235 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 Denk3f0137b2006-06-14 17:45:53 +02001254 }
1255
1256#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001257#error "CONFIG_SYS_USB_EXTC_CLK or CONFIG_SYS_USB_BRGCLK must be defined"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001258#endif
1259
1260}
1261
1262/* mpc8xx_udc_cbd_attach
1263 *
1264 * attach a cbd to and endpoint
1265 */
1266static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size)
1267{
Wolfgang Denke2601822006-06-14 18:14:56 +02001268
1269 if (!tx_cbd[ep] || !rx_cbd[ep] || ep >= MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001270 udc_state = STATE_ERROR;
1271 return;
1272 }
1273
Wolfgang Denke2601822006-06-14 18:14:56 +02001274 if (tx_size > USB_MAX_PKT || rx_size > USB_MAX_PKT ||
1275 (!tx_size && !rx_size)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001276 udc_state = STATE_ERROR;
1277 return;
1278 }
1279
1280 /* Attach CBD to appropiate Parameter RAM Endpoint data structure */
Wolfgang Denke2601822006-06-14 18:14:56 +02001281 if (rx_size) {
1282 endpoints[ep]->rbase = (u32) rx_cbd[rx_ct];
1283 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001284 rx_ct++;
1285
Wolfgang Denke2601822006-06-14 18:14:56 +02001286 if (!ep) {
1287
1288 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001289 rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
1290 rx_ct++;
1291
Wolfgang Denke2601822006-06-14 18:14:56 +02001292 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001293 rx_ct += 2;
Wolfgang Denke2601822006-06-14 18:14:56 +02001294 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001295 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 Denke2601822006-06-14 18:14:56 +02001300 ep_ref[ep].prx = rx_cbd[rx_ct - 1];
1301 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001302
1303 ep_ref[ep].prx = 0;
1304 endpoints[ep]->rbase = 0;
1305 endpoints[ep]->rbptr = 0;
1306 }
1307
Wolfgang Denke2601822006-06-14 18:14:56 +02001308 if (tx_size) {
1309 endpoints[ep]->tbase = (u32) tx_cbd[tx_ct];
1310 endpoints[ep]->tbptr = (u32) tx_cbd[tx_ct];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001311 tx_ct++;
Wolfgang Denke2601822006-06-14 18:14:56 +02001312 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001313 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 Denke2601822006-06-14 18:14:56 +02001320 endpoints[ep]->rfcr = 0x18;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001321 endpoints[ep]->tfcr = 0x18;
1322 ep_ref[ep].sc |= EP_ATTACHED;
1323
Wolfgang Denke2601822006-06-14 18:14:56 +02001324 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 Denk3f0137b2006-06-14 17:45:53 +02001328
1329 return;
1330}
1331
1332/* mpc8xx_udc_cbd_init
1333 *
1334 * Allocate space for a cbd and allocate TX/RX data space
1335 */
1336static void mpc8xx_udc_cbd_init (void)
1337{
1338 int i = 0;
1339
Wolfgang Denke2601822006-06-14 18:14:56 +02001340 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 Denk3f0137b2006-06-14 17:45:53 +02001348 }
1349
Wolfgang Denke2601822006-06-14 18:14:56 +02001350 for (i = 0; i < TX_RING_SIZE; i++) {
1351 tx_cbd[i]->cbd_bufaddr =
1352 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001353
Wolfgang Denke2601822006-06-14 18:14:56 +02001354 tx_cbd[i]->cbd_sc = (TX_BD_I | TX_BD_W);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001355 tx_cbd[i]->cbd_datlen = 0x0000;
1356 }
1357
1358
Wolfgang Denke2601822006-06-14 18:14:56 +02001359 for (i = 0; i < RX_RING_SIZE; i++) {
1360 rx_cbd[i]->cbd_bufaddr =
1361 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001362 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 */
1374static void mpc8xx_udc_endpoint_init (void)
1375{
1376 int i = 0;
1377
Wolfgang Denke2601822006-06-14 18:14:56 +02001378 for (; i < MAX_ENDPOINTS; i++) {
1379 endpoints[i] = (usb_epb_t *)
1380 mpc8xx_udc_alloc (sizeof (usb_epb_t), 32);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001381 }
1382}
1383
1384/* mpc8xx_udc_alloc
1385 *
Wolfgang Denke2601822006-06-14 18:14:56 +02001386 * Grab the address of some dpram
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001387 */
1388static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment)
1389{
1390 u32 retaddr = address_base;
Wolfgang Denke2601822006-06-14 18:14:56 +02001391
1392 while (retaddr % alignment) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001393 retaddr++;
Wolfgang Denke2601822006-06-14 18:14:56 +02001394 }
1395 address_base += data_size;
1396
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001397 return retaddr;
1398}