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