blob: b3e178abefb457cd801ee78994b54c4dd95b51b7 [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
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +02006 * DasUBoot/drivers/usb/gadget/omap1510_udc.c, for design and implementation
Marcel Ziswileraea68562007-12-30 03:30:46 +01007 * ideas.
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02008 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020010 */
11
12/*
13 * Notes :
Wolfgang Denke2601822006-06-14 18:14:56 +020014 * 1. #define __SIMULATE_ERROR__ to inject a CRC error into every 2nd TX
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020015 * packet to force the USB re-transmit protocol.
16 *
17 * 2. #define __DEBUG_UDC__ to switch on debug tracing to serial console
Wolfgang Denke2601822006-06-14 18:14:56 +020018 * be careful that tracing doesn't create Hiesen-bugs with respect to
19 * response timeouts to control requests.
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020020 *
21 * 3. This driver should be able to support any higher level driver that
22 * that wants to do either of the two standard UDC implementations
23 * Control-Bulk-Interrupt or Bulk-IN/Bulk-Out standards. Hence
24 * gserial and cdc_acm should work with this code.
25 *
26 * 4. NAK events never actually get raised at all, the documentation
27 * is just wrong !
28 *
29 * 5. For some reason, cbd_datlen is *always* +2 the value it should be.
30 * this means that having an RX cbd of 16 bytes is not possible, since
Wolfgang Denke2601822006-06-14 18:14:56 +020031 * the same size is reported for 14 bytes received as 16 bytes received
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020032 * until we can find out why this happens, RX cbds must be limited to 8
33 * bytes. TODO: check errata for this behaviour.
34 *
35 * 6. Right now this code doesn't support properly powering up with the USB
Wolfgang Denke2601822006-06-14 18:14:56 +020036 * cable attached to the USB host my development board the Adder87x doesn't
37 * have a pull-up fitted to allow this, so it is necessary to power the
38 * board and *then* attached the USB cable to the host. However somebody
39 * with a different design in their board may be able to keep the cable
40 * constantly connected and simply enable/disable a pull-up re
41 * figure 31.1 in MPC885RM.pdf instead of having to power up the board and
42 * then attach the cable !
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020043 *
44 */
45#include <common.h>
46#include <config.h>
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020047#include <commproc.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020048#include <usbdevice.h>
49#include <usb/mpc8xx_udc.h>
Troy Kiskydf446f52013-10-10 15:28:04 -070050#include <usb/udc.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020051
52#include "ep0.h"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020053
Wolfgang Denkd112a2c2007-09-15 20:48:41 +020054DECLARE_GLOBAL_DATA_PTR;
55
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020056#define ERR(fmt, args...)\
57 serial_printf("ERROR : [%s] %s:%d: "fmt,\
58 __FILE__,__FUNCTION__,__LINE__, ##args)
59#ifdef __DEBUG_UDC__
Wolfgang Denke2601822006-06-14 18:14:56 +020060#define DBG(fmt,args...)\
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020061 serial_printf("[%s] %s:%d: "fmt,\
62 __FILE__,__FUNCTION__,__LINE__, ##args)
63#else
Wolfgang Denke2601822006-06-14 18:14:56 +020064#define DBG(fmt,args...)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020065#endif
66
67/* Static Data */
68#ifdef __SIMULATE_ERROR__
Wolfgang Denke2601822006-06-14 18:14:56 +020069static char err_poison_test = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020070#endif
71static struct mpc8xx_ep ep_ref[MAX_ENDPOINTS];
72static u32 address_base = STATE_NOT_READY;
73static mpc8xx_udc_state_t udc_state = 0;
74static struct usb_device_instance *udc_device = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +020075static volatile usb_epb_t *endpoints[MAX_ENDPOINTS];
76static volatile cbd_t *tx_cbd[TX_RING_SIZE];
77static volatile cbd_t *rx_cbd[RX_RING_SIZE];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020078static volatile immap_t *immr = 0;
79static volatile cpm8xx_t *cp = 0;
80static volatile usb_pram_t *usb_paramp = 0;
81static volatile usb_t *usbp = 0;
82static int rx_ct = 0;
83static int tx_ct = 0;
84
85/* Static Function Declarations */
86static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
Wolfgang Denke2601822006-06-14 18:14:56 +020087 usb_device_state_t final);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020088static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
Wolfgang Denke2601822006-06-14 18:14:56 +020089 usb_device_state_t final);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020090static void mpc8xx_udc_stall (unsigned int ep);
Wolfgang Denke2601822006-06-14 18:14:56 +020091static void mpc8xx_udc_flush_tx_fifo (int epid);
92static void mpc8xx_udc_flush_rx_fifo (void);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020093static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp);
Wolfgang Denke2601822006-06-14 18:14:56 +020094static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
95 struct urb *tx_urb);
96static void mpc8xx_udc_dump_request (struct usb_device_request *request);
97static void mpc8xx_udc_clock_init (volatile immap_t * immr,
98 volatile cpm8xx_t * cp);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +020099static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi);
100static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp);
Wolfgang Denke2601822006-06-14 18:14:56 +0200101static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200102static void mpc8xx_udc_cbd_init (void);
103static void mpc8xx_udc_endpoint_init (void);
104static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size);
105static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment);
106static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp);
107static void mpc8xx_udc_set_nak (unsigned int ep);
Wolfgang Denke2601822006-06-14 18:14:56 +0200108static short mpc8xx_udc_handle_txerr (void);
109static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200110
111/******************************************************************************
Wolfgang Denke2601822006-06-14 18:14:56 +0200112 Global Linkage
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200113 *****************************************************************************/
114
115/* udc_init
116 *
117 * Do initial bus gluing
118 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200119int udc_init (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200120{
121 /* Init various pointers */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200122 immr = (immap_t *) CONFIG_SYS_IMMR;
Wolfgang Denke2601822006-06-14 18:14:56 +0200123 cp = (cpm8xx_t *) & (immr->im_cpm);
124 usb_paramp = (usb_pram_t *) & (cp->cp_dparam[PROFF_USB]);
125 usbp = (usb_t *) & (cp->cp_scc[0]);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200126
Wolfgang Denke2601822006-06-14 18:14:56 +0200127 memset (ep_ref, 0x00, (sizeof (struct mpc8xx_ep) * MAX_ENDPOINTS));
128
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200129 udc_device = 0;
130 udc_state = STATE_NOT_READY;
Wolfgang Denke2601822006-06-14 18:14:56 +0200131
132 usbp->usmod = 0x00;
133 usbp->uscom = 0;
134
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200135 /* Set USB Frame #0, Respond at Address & Get a clock source */
136 usbp->usaddr = 0x00;
137 mpc8xx_udc_clock_init (immr, cp);
Wolfgang Denke2601822006-06-14 18:14:56 +0200138
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200139 /* PA15, PA14 as perhiperal USBRXD and USBOE */
Wolfgang Denke2601822006-06-14 18:14:56 +0200140 immr->im_ioport.iop_padir &= ~0x0003;
141 immr->im_ioport.iop_papar |= 0x0003;
142
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200143 /* PC11/PC10 as peripheral USBRXP USBRXN */
Wolfgang Denke2601822006-06-14 18:14:56 +0200144 immr->im_ioport.iop_pcso |= 0x0030;
145
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200146 /* PC7/PC6 as perhiperal USBTXP and USBTXN */
Wolfgang Denke2601822006-06-14 18:14:56 +0200147 immr->im_ioport.iop_pcdir |= 0x0300;
148 immr->im_ioport.iop_pcpar |= 0x0300;
149
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200150 /* Set the base address */
Wolfgang Denke2601822006-06-14 18:14:56 +0200151 address_base = (u32) (cp->cp_dpmem + CPM_USB_BASE);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200152
153 /* Initialise endpoints and circular buffers */
Wolfgang Denke2601822006-06-14 18:14:56 +0200154 mpc8xx_udc_endpoint_init ();
155 mpc8xx_udc_cbd_init ();
156
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200157 /* Assign allocated Dual Port Endpoint descriptors */
Wolfgang Denke2601822006-06-14 18:14:56 +0200158 usb_paramp->ep0ptr = (u32) endpoints[0];
159 usb_paramp->ep1ptr = (u32) endpoints[1];
160 usb_paramp->ep2ptr = (u32) endpoints[2];
161 usb_paramp->ep3ptr = (u32) endpoints[3];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200162 usb_paramp->frame_n = 0;
163
Wolfgang Denke2601822006-06-14 18:14:56 +0200164 DBG ("ep0ptr=0x%08x ep1ptr=0x%08x ep2ptr=0x%08x ep3ptr=0x%08x\n",
165 usb_paramp->ep0ptr, usb_paramp->ep1ptr, usb_paramp->ep2ptr,
166 usb_paramp->ep3ptr);
167
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200168 return 0;
169}
170
171/* udc_irq
172 *
173 * Poll for whatever events may have occured
174 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200175void udc_irq (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200176{
177 int epid = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200178 volatile cbd_t *rx_cbdp = 0;
179 volatile cbd_t *rx_cbdp_base = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200180
Wolfgang Denke2601822006-06-14 18:14:56 +0200181 if (udc_state != STATE_READY) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200182 return;
183 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200184
185 if (usbp->usber & USB_E_BSY) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200186 /* This shouldn't happen. If it does then it's a bug ! */
Wolfgang Denke2601822006-06-14 18:14:56 +0200187 usbp->usber |= USB_E_BSY;
188 mpc8xx_udc_flush_rx_fifo ();
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200189 }
190
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200191 /* Scan all RX/Bidirectional Endpoints for RX data. */
Wolfgang Denke2601822006-06-14 18:14:56 +0200192 for (epid = 0; epid < MAX_ENDPOINTS; epid++) {
193 if (!ep_ref[epid].prx) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200194 continue;
195 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200196 rx_cbdp = rx_cbdp_base = ep_ref[epid].prx;
Wolfgang Denke2601822006-06-14 18:14:56 +0200197
198 do {
199 if (!(rx_cbdp->cbd_sc & RX_BD_E)) {
200
201 if (rx_cbdp->cbd_sc & 0x1F) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200202 /* Corrupt data discard it.
Wolfgang Denke2601822006-06-14 18:14:56 +0200203 * Controller has NAK'd this packet.
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200204 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200205 mpc8xx_udc_clear_rxbd (rx_cbdp);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200206
Wolfgang Denke2601822006-06-14 18:14:56 +0200207 } else {
208 if (!epid) {
209 mpc8xx_udc_ep0_rx (rx_cbdp);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200210
Wolfgang Denke2601822006-06-14 18:14:56 +0200211 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200212 /* Process data */
Wolfgang Denke2601822006-06-14 18:14:56 +0200213 mpc8xx_udc_set_nak (epid);
214 mpc8xx_udc_epn_rx (epid, rx_cbdp);
215 mpc8xx_udc_clear_rxbd (rx_cbdp);
216 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200217 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200218
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200219 /* Advance RX CBD pointer */
Wolfgang Denke2601822006-06-14 18:14:56 +0200220 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200221 ep_ref[epid].prx = rx_cbdp;
Wolfgang Denke2601822006-06-14 18:14:56 +0200222 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200223 /* Advance RX CBD pointer */
Wolfgang Denke2601822006-06-14 18:14:56 +0200224 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200225 }
226
Wolfgang Denke2601822006-06-14 18:14:56 +0200227 } while (rx_cbdp != rx_cbdp_base);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200228 }
229
230 /* Handle TX events as appropiate, the correct place to do this is
231 * in a tx routine. Perhaps TX on epn was pre-empted by ep0
232 */
233
Wolfgang Denke2601822006-06-14 18:14:56 +0200234 if (usbp->usber & USB_E_TXB) {
235 usbp->usber |= USB_E_TXB;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200236 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200237
238 if (usbp->usber & (USB_TX_ERRMASK)) {
239 mpc8xx_udc_handle_txerr ();
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200240 }
241
242 /* Switch to the default state, respond at the default address */
Wolfgang Denke2601822006-06-14 18:14:56 +0200243 if (usbp->usber & USB_E_RESET) {
244 usbp->usber |= USB_E_RESET;
245 usbp->usaddr = 0x00;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200246 udc_device->device_state = STATE_DEFAULT;
247 }
248
Wolfgang Denke2601822006-06-14 18:14:56 +0200249 /* if(usbp->usber&USB_E_IDLE){
250 We could suspend here !
251 usbp->usber|=USB_E_IDLE;
252 DBG("idle state change\n");
253 }
254 if(usbp->usbs){
255 We could resume here when IDLE is deasserted !
256 Not worth doing, so long as we are self powered though.
257 }
258 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200259
260 return;
261}
262
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200263/* udc_endpoint_write
264 *
265 * Write some data to an endpoint
266 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200267int udc_endpoint_write (struct usb_endpoint_instance *epi)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200268{
269 int ep = 0;
270 short epid = 1, unnak = 0, ret = 0;
271
Wolfgang Denke2601822006-06-14 18:14:56 +0200272 if (udc_state != STATE_READY) {
273 ERR ("invalid udc_state != STATE_READY!\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200274 return -1;
275 }
276
Wolfgang Denke2601822006-06-14 18:14:56 +0200277 if (!udc_device || !epi) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200278 return -1;
279 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200280
281 if (udc_device->device_state != STATE_CONFIGURED) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200282 return -1;
283 }
284
285 ep = epi->endpoint_address & 0x03;
Wolfgang Denke2601822006-06-14 18:14:56 +0200286 if (ep >= MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200287 return -1;
288 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200289
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200290 /* Set NAK for all RX endpoints during TX */
Wolfgang Denke2601822006-06-14 18:14:56 +0200291 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200292
293 /* Don't set NAK on DATA IN/CONTROL endpoints */
Wolfgang Denke2601822006-06-14 18:14:56 +0200294 if (ep_ref[epid].sc & USB_DIR_IN) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200295 continue;
296 }
297
Wolfgang Denke2601822006-06-14 18:14:56 +0200298 if (!(usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK))) {
299 unnak |= 1 << epid;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200300 }
301
Wolfgang Denke2601822006-06-14 18:14:56 +0200302 mpc8xx_udc_set_nak (epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200303 }
304
Wolfgang Denke2601822006-06-14 18:14:56 +0200305 mpc8xx_udc_init_tx (&udc_device->bus->endpoint_array[ep],
306 epi->tx_urb);
307 ret = mpc8xx_udc_ep_tx (&udc_device->bus->endpoint_array[ep]);
308
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200309 /* Remove temporary NAK */
Wolfgang Denke2601822006-06-14 18:14:56 +0200310 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
311 if (unnak & (1 << epid)) {
312 udc_unset_nak (epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200313 }
314 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200315
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200316 return ret;
317}
318
319/* mpc8xx_udc_assign_urb
320 *
321 * Associate a given urb to an endpoint TX or RX transmit/receive buffers
322 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200323static int mpc8xx_udc_assign_urb (int ep, char direction)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200324{
325 struct usb_endpoint_instance *epi = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200326
327 if (ep >= MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200328 goto err;
329 }
330 epi = &udc_device->bus->endpoint_array[ep];
Wolfgang Denke2601822006-06-14 18:14:56 +0200331 if (!epi) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200332 goto err;
333 }
334
Wolfgang Denke2601822006-06-14 18:14:56 +0200335 if (!ep_ref[ep].urb) {
336 ep_ref[ep].urb = usbd_alloc_urb (udc_device, udc_device->bus->endpoint_array);
337 if (!ep_ref[ep].urb) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200338 goto err;
339 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200340 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200341 ep_ref[ep].urb->actual_length = 0;
342 }
343
Wolfgang Denke2601822006-06-14 18:14:56 +0200344 switch (direction) {
345 case USB_DIR_IN:
346 epi->tx_urb = ep_ref[ep].urb;
347 break;
348 case USB_DIR_OUT:
349 epi->rcv_urb = ep_ref[ep].urb;
350 break;
351 default:
352 goto err;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200353 }
354 return 0;
355
Wolfgang Denke2601822006-06-14 18:14:56 +0200356 err:
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200357 udc_state = STATE_ERROR;
358 return -1;
359}
360
361/* udc_setup_ep
362 *
363 * Associate U-Boot software endpoints to mpc8xx endpoint parameter ram
364 * Isochronous endpoints aren't yet supported!
365 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200366void udc_setup_ep (struct usb_device_instance *device, unsigned int ep,
367 struct usb_endpoint_instance *epi)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200368{
369 uchar direction = 0;
370 int ep_attrib = 0;
371
Wolfgang Denke2601822006-06-14 18:14:56 +0200372 if (epi && (ep < MAX_ENDPOINTS)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200373
Wolfgang Denke2601822006-06-14 18:14:56 +0200374 if (ep == 0) {
375 if (epi->rcv_attributes != USB_ENDPOINT_XFER_CONTROL
376 || epi->tx_attributes !=
377 USB_ENDPOINT_XFER_CONTROL) {
378
379 /* ep0 must be a control endpoint */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200380 udc_state = STATE_ERROR;
381 return;
382
383 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200384 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
385 mpc8xx_udc_cbd_attach (ep, epi->tx_packetSize,
386 epi->rcv_packetSize);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200387 }
388 usbp->usep[ep] = 0x0000;
389 return;
390 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200391
392 if ((epi->endpoint_address & USB_ENDPOINT_DIR_MASK)
393 == USB_DIR_IN) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200394
395 direction = 1;
396 ep_attrib = epi->tx_attributes;
397 epi->rcv_packetSize = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200398 ep_ref[ep].sc |= USB_DIR_IN;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200399 } else {
Wolfgang Denke2601822006-06-14 18:14:56 +0200400
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200401 direction = 0;
402 ep_attrib = epi->rcv_attributes;
Wolfgang Denke2601822006-06-14 18:14:56 +0200403 epi->tx_packetSize = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200404 ep_ref[ep].sc &= ~USB_DIR_IN;
405 }
406
Wolfgang Denke2601822006-06-14 18:14:56 +0200407 if (mpc8xx_udc_assign_urb (ep, epi->endpoint_address
408 & USB_ENDPOINT_DIR_MASK)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200409 return;
410 }
411
Wolfgang Denke2601822006-06-14 18:14:56 +0200412 switch (ep_attrib) {
413 case USB_ENDPOINT_XFER_CONTROL:
414 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
415 mpc8xx_udc_cbd_attach (ep,
416 epi->tx_packetSize,
417 epi->rcv_packetSize);
418 }
419 usbp->usep[ep] = ep << 12;
420 epi->rcv_urb = epi->tx_urb = ep_ref[ep].urb;
421
422 break;
423 case USB_ENDPOINT_XFER_BULK:
424 case USB_ENDPOINT_XFER_INT:
425 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
426 if (direction) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200427 mpc8xx_udc_cbd_attach (ep,
Wolfgang Denke2601822006-06-14 18:14:56 +0200428 epi->tx_packetSize,
429 0);
430 } else {
431 mpc8xx_udc_cbd_attach (ep,
432 0,
433 epi->rcv_packetSize);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200434 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200435 }
436 usbp->usep[ep] = (ep << 12) | ((ep_attrib) << 8);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200437
Wolfgang Denke2601822006-06-14 18:14:56 +0200438 break;
439 case USB_ENDPOINT_XFER_ISOC:
440 default:
441 serial_printf ("Error endpoint attrib %d>3\n", ep_attrib);
442 udc_state = STATE_ERROR;
443 break;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200444 }
445 }
446
447}
448
449/* udc_connect
450 *
451 * Move state, switch on the USB
452 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200453void udc_connect (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200454{
Wolfgang Denke2601822006-06-14 18:14:56 +0200455 /* Enable pull-up resistor on D+
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200456 * TODO: fit a pull-up resistor to drive SE0 for > 2.5us
457 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200458
459 if (udc_state != STATE_ERROR) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200460 udc_state = STATE_READY;
Wolfgang Denke2601822006-06-14 18:14:56 +0200461 usbp->usmod |= USMOD_EN;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200462 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200463}
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200464
465/* udc_disconnect
466 *
467 * Disconnect is not used but, is included for completeness
468 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200469void udc_disconnect (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200470{
471 /* Disable pull-up resistor on D-
472 * TODO: fix a pullup resistor to control this
473 */
474
Wolfgang Denke2601822006-06-14 18:14:56 +0200475 if (udc_state != STATE_ERROR) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200476 udc_state = STATE_NOT_READY;
477 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200478 usbp->usmod &= ~USMOD_EN;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200479}
480
481/* udc_enable
Wolfgang Denke2601822006-06-14 18:14:56 +0200482 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200483 * Grab an EP0 URB, register interest in a subset of USB events
484 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200485void udc_enable (struct usb_device_instance *device)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200486{
Wolfgang Denke2601822006-06-14 18:14:56 +0200487 if (udc_state == STATE_ERROR) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200488 return;
489 }
490
491 udc_device = device;
Wolfgang Denke2601822006-06-14 18:14:56 +0200492
493 if (!ep_ref[0].urb) {
494 ep_ref[0].urb = usbd_alloc_urb (device, device->bus->endpoint_array);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200495 }
496
497 /* Register interest in all events except SOF, enable transceiver */
Wolfgang Denke2601822006-06-14 18:14:56 +0200498 usbp->usber = 0x03FF;
499 usbp->usbmr = 0x02F7;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200500
501 return;
502}
503
504/* udc_disable
505 *
506 * disable the currently hooked device
507 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200508void udc_disable (void)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200509{
510 int i = 0;
511
Wolfgang Denke2601822006-06-14 18:14:56 +0200512 if (udc_state == STATE_ERROR) {
513 DBG ("Won't disable UDC. udc_state==STATE_ERROR !\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200514 return;
515 }
516
517 udc_device = 0;
518
Wolfgang Denke2601822006-06-14 18:14:56 +0200519 for (; i < MAX_ENDPOINTS; i++) {
520 if (ep_ref[i].urb) {
521 usbd_dealloc_urb (ep_ref[i].urb);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200522 ep_ref[i].urb = 0;
523 }
524 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200525
526 usbp->usbmr = 0x00;
527 usbp->usmod = ~USMOD_EN;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200528 udc_state = STATE_NOT_READY;
529}
530
531/* udc_startup_events
532 *
533 * Enable the specified device
534 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200535void udc_startup_events (struct usb_device_instance *device)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200536{
Wolfgang Denke2601822006-06-14 18:14:56 +0200537 udc_enable (device);
538 if (udc_state == STATE_READY) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200539 usbd_device_event_irq (device, DEVICE_CREATE, 0);
540 }
541}
542
543/* udc_set_nak
Wolfgang Denke2601822006-06-14 18:14:56 +0200544 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200545 * Allow upper layers to signal lower layers should not accept more RX data
546 *
547 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200548void udc_set_nak (int epid)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200549{
Wolfgang Denke2601822006-06-14 18:14:56 +0200550 if (epid) {
551 mpc8xx_udc_set_nak (epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200552 }
553}
554
Wolfgang Denke2601822006-06-14 18:14:56 +0200555/* udc_unset_nak
556 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200557 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
558 * Switch off NAKing on this endpoint to accept more data output from host.
559 *
560 */
561void udc_unset_nak (int epid)
562{
Wolfgang Denke2601822006-06-14 18:14:56 +0200563 if (epid > MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200564 return;
565 }
566
Wolfgang Denke2601822006-06-14 18:14:56 +0200567 if (usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK)) {
568 usbp->usep[epid] &= ~(USEP_THS_NAK | USEP_RHS_NAK);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200569 __asm__ ("eieio");
570 }
571}
572
573/******************************************************************************
Wolfgang Denke2601822006-06-14 18:14:56 +0200574 Static Linkage
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200575******************************************************************************/
576
577/* udc_state_transition_up
578 * udc_state_transition_down
579 *
580 * Helper functions to implement device state changes. The device states and
581 * the events that transition between them are:
582 *
583 * STATE_ATTACHED
584 * || /\
585 * \/ ||
586 * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
587 * || /\
588 * \/ ||
589 * STATE_POWERED
590 * || /\
591 * \/ ||
592 * DEVICE_RESET DEVICE_POWER_INTERRUPTION
593 * || /\
594 * \/ ||
595 * STATE_DEFAULT
596 * || /\
597 * \/ ||
598 * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
599 * || /\
600 * \/ ||
601 * STATE_ADDRESSED
602 * || /\
603 * \/ ||
604 * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
605 * || /\
606 * \/ ||
607 * STATE_CONFIGURED
608 *
609 * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
610 * to STATE_CONFIGURED) from the specified initial state to the specified final
611 * state, passing through each intermediate state on the way. If the initial
612 * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
613 * no state transitions will take place.
614 *
615 * udc_state_transition_down transitions down (in the direction from
616 * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
617 * specified final state, passing through each intermediate state on the way.
618 * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
619 * state, then no state transitions will take place.
620 *
621 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200622
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200623static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
Wolfgang Denke2601822006-06-14 18:14:56 +0200624 usb_device_state_t final)
625{
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200626 if (initial < final) {
627 switch (initial) {
628 case STATE_ATTACHED:
629 usbd_device_event_irq (udc_device,
630 DEVICE_HUB_CONFIGURED, 0);
631 if (final == STATE_POWERED)
632 break;
633 case STATE_POWERED:
634 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
635 if (final == STATE_DEFAULT)
636 break;
637 case STATE_DEFAULT:
638 usbd_device_event_irq (udc_device,
639 DEVICE_ADDRESS_ASSIGNED, 0);
640 if (final == STATE_ADDRESSED)
641 break;
642 case STATE_ADDRESSED:
643 usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
644 0);
645 case STATE_CONFIGURED:
646 break;
647 default:
648 break;
649 }
650 }
651}
652
653static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
Wolfgang Denke2601822006-06-14 18:14:56 +0200654 usb_device_state_t final)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200655{
656 if (initial > final) {
657 switch (initial) {
658 case STATE_CONFIGURED:
Wolfgang Denke2601822006-06-14 18:14:56 +0200659 usbd_device_event_irq (udc_device,
660 DEVICE_DE_CONFIGURED, 0);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200661 if (final == STATE_ADDRESSED)
662 break;
663 case STATE_ADDRESSED:
664 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
665 if (final == STATE_DEFAULT)
666 break;
667 case STATE_DEFAULT:
Wolfgang Denke2601822006-06-14 18:14:56 +0200668 usbd_device_event_irq (udc_device,
669 DEVICE_POWER_INTERRUPTION, 0);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200670 if (final == STATE_POWERED)
671 break;
672 case STATE_POWERED:
673 usbd_device_event_irq (udc_device, DEVICE_HUB_RESET,
Wolfgang Denke2601822006-06-14 18:14:56 +0200674 0);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200675 case STATE_ATTACHED:
676 break;
677 default:
678 break;
679 }
680 }
681}
682
683/* mpc8xx_udc_stall
684 *
685 * Force returning of STALL tokens on the given endpoint. Protocol or function
686 * STALL conditions are permissable here
687 */
688static void mpc8xx_udc_stall (unsigned int ep)
689{
690 usbp->usep[ep] |= STALL_BITMASK;
691}
692
693/* mpc8xx_udc_set_nak
694 *
695 * Force returning of NAK responses for the given endpoint as a kind of very
696 * simple flow control
Wolfgang Denke2601822006-06-14 18:14:56 +0200697 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200698static void mpc8xx_udc_set_nak (unsigned int ep)
699{
700 usbp->usep[ep] |= NAK_BITMASK;
701 __asm__ ("eieio");
702}
703
704/* mpc8xx_udc_handle_txerr
705 *
706 * Handle errors relevant to TX. Return a status code to allow calling
707 * indicative of what if anything happened
708 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200709static short mpc8xx_udc_handle_txerr ()
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200710{
711 short ep = 0, ret = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200712
713 for (; ep < TX_RING_SIZE; ep++) {
714 if (usbp->usber & (0x10 << ep)) {
715
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200716 /* Timeout or underrun */
Wolfgang Denke2601822006-06-14 18:14:56 +0200717 if (tx_cbd[ep]->cbd_sc & 0x06) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200718 ret = 1;
Wolfgang Denke2601822006-06-14 18:14:56 +0200719 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200720
Wolfgang Denke2601822006-06-14 18:14:56 +0200721 } else {
722 if (usbp->usep[ep] & STALL_BITMASK) {
723 if (!ep) {
724 usbp->usep[ep] &= ~STALL_BITMASK;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200725 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200726 } /* else NAK */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200727 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200728 usbp->usber |= (0x10 << ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200729 }
730 }
731 return ret;
732}
733
734/* mpc8xx_udc_advance_rx
735 *
736 * Advance cbd rx
737 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200738static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200739{
Wolfgang Denke2601822006-06-14 18:14:56 +0200740 if ((*rx_cbdp)->cbd_sc & RX_BD_W) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200741 *rx_cbdp = (volatile cbd_t *) (endpoints[epid]->rbase + CONFIG_SYS_IMMR);
Wolfgang Denke2601822006-06-14 18:14:56 +0200742
743 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200744 (*rx_cbdp)++;
745 }
746}
747
748
749/* mpc8xx_udc_flush_tx_fifo
750 *
751 * Flush a given TX fifo. Assumes one tx cbd per endpoint
752 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200753static void mpc8xx_udc_flush_tx_fifo (int epid)
754{
755 volatile cbd_t *tx_cbdp = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200756
Wolfgang Denke2601822006-06-14 18:14:56 +0200757 if (epid > MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200758 return;
759 }
760
761 /* TX stop */
Wolfgang Denke2601822006-06-14 18:14:56 +0200762 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x1D01);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200763 __asm__ ("eieio");
Wolfgang Denke2601822006-06-14 18:14:56 +0200764 while (immr->im_cpm.cp_cpcr & 0x01);
765
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200766 usbp->uscom = 0x40 | 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200767
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200768 /* reset ring */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200769 tx_cbdp = (cbd_t *) (endpoints[epid]->tbptr + CONFIG_SYS_IMMR);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200770 tx_cbdp->cbd_sc = (TX_BD_I | TX_BD_W);
771
Wolfgang Denke2601822006-06-14 18:14:56 +0200772
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200773 endpoints[epid]->tptr = endpoints[epid]->tbase;
Wolfgang Denke2601822006-06-14 18:14:56 +0200774 endpoints[epid]->tstate = 0x00;
775 endpoints[epid]->tbcnt = 0x00;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200776
777 /* TX start */
Wolfgang Denke2601822006-06-14 18:14:56 +0200778 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x2D01);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200779 __asm__ ("eieio");
Wolfgang Denke2601822006-06-14 18:14:56 +0200780 while (immr->im_cpm.cp_cpcr & 0x01);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200781
782 return;
783}
784
785/* mpc8xx_udc_flush_rx_fifo
786 *
787 * For the sake of completeness of the namespace, it seems like
788 * a good-design-decision (tm) to include mpc8xx_udc_flush_rx_fifo();
789 * If RX_BD_E is true => a driver bug either here or in an upper layer
790 * not polling frequently enough. If RX_BD_E is true we have told the host
791 * we have accepted data but, the CPM found it had no-where to put that data
792 * which needless to say would be a bad thing.
793 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200794static void mpc8xx_udc_flush_rx_fifo ()
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200795{
796 int i = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200797
798 for (i = 0; i < RX_RING_SIZE; i++) {
799 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
800 ERR ("buf %p used rx data len = 0x%x sc=0x%x!\n",
801 rx_cbd[i], rx_cbd[i]->cbd_datlen,
802 rx_cbd[i]->cbd_sc);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200803
804 }
805 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200806 ERR ("BUG : Input over-run\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200807}
808
809/* mpc8xx_udc_clear_rxbd
Wolfgang Denke2601822006-06-14 18:14:56 +0200810 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200811 * Release control of RX CBD to CP.
812 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200813static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200814{
815 rx_cbdp->cbd_datlen = 0x0000;
Wolfgang Denke2601822006-06-14 18:14:56 +0200816 rx_cbdp->cbd_sc = ((rx_cbdp->cbd_sc & RX_BD_W) | (RX_BD_E | RX_BD_I));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200817 __asm__ ("eieio");
818}
819
820/* mpc8xx_udc_tx_irq
821 *
822 * Parse for tx timeout, control RX or USB reset/busy conditions
823 * Return -1 on timeout, -2 on fatal error, else return zero
824 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200825static int mpc8xx_udc_tx_irq (int ep)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200826{
827 int i = 0;
828
Wolfgang Denke2601822006-06-14 18:14:56 +0200829 if (usbp->usber & (USB_TX_ERRMASK)) {
830 if (mpc8xx_udc_handle_txerr ()) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200831 /* Timeout, controlling function must retry send */
832 return -1;
833 }
834 }
835
Wolfgang Denke2601822006-06-14 18:14:56 +0200836 if (usbp->usber & (USB_E_RESET | USB_E_BSY)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200837 /* Fatal, abandon TX transaction */
838 return -2;
839 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200840
841 if (usbp->usber & USB_E_RXB) {
842 for (i = 0; i < RX_RING_SIZE; i++) {
843 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
844 if ((rx_cbd[i] == ep_ref[0].prx) || ep) {
845 return -2;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200846 }
847 }
848 }
849 }
850
851 return 0;
852}
853
854/* mpc8xx_udc_ep_tx
855 *
856 * Transmit in a re-entrant fashion outbound USB packets.
857 * Implement retry/timeout mechanism described in USB specification
858 * Toggle DATA0/DATA1 pids as necessary
859 * Introduces non-standard tx_retry. The USB standard has no scope for slave
860 * devices to give up TX, however tx_retry stops us getting stuck in an endless
861 * TX loop.
862 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200863static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200864{
865 struct urb *urb = epi->tx_urb;
Wolfgang Denke2601822006-06-14 18:14:56 +0200866 volatile cbd_t *tx_cbdp = 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200867 unsigned int ep = 0, pkt_len = 0, x = 0, tx_retry = 0;
868 int ret = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200869
870 if (!epi || (epi->endpoint_address & 0x03) >= MAX_ENDPOINTS || !urb) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200871 return -1;
872 }
873
874 ep = epi->endpoint_address & 0x03;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200875 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
Wolfgang Denke2601822006-06-14 18:14:56 +0200876
877 if (tx_cbdp->cbd_sc & TX_BD_R || usbp->usber & USB_E_TXB) {
878 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200879 usbp->usber |= USB_E_TXB;
880 };
881
Wolfgang Denke2601822006-06-14 18:14:56 +0200882 while (tx_retry++ < 100) {
883 ret = mpc8xx_udc_tx_irq (ep);
884 if (ret == -1) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200885 /* ignore timeout here */
Wolfgang Denke2601822006-06-14 18:14:56 +0200886 } else if (ret == -2) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200887 /* Abandon TX */
Wolfgang Denke2601822006-06-14 18:14:56 +0200888 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200889 return -1;
Wolfgang Denke2601822006-06-14 18:14:56 +0200890 }
891
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200892 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
Wolfgang Denke2601822006-06-14 18:14:56 +0200893 while (tx_cbdp->cbd_sc & TX_BD_R) {
894 };
895 tx_cbdp->cbd_sc = (tx_cbdp->cbd_sc & TX_BD_W);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200896
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200897 pkt_len = urb->actual_length - epi->sent;
898
Wolfgang Denke2601822006-06-14 18:14:56 +0200899 if (pkt_len > epi->tx_packetSize || pkt_len > EP_MAX_PKT) {
Masahiro Yamadab62b39b2014-09-18 13:28:06 +0900900 pkt_len = min(epi->tx_packetSize, EP_MAX_PKT);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200901 }
902
Wolfgang Denke2601822006-06-14 18:14:56 +0200903 for (x = 0; x < pkt_len; x++) {
904 *((unsigned char *) (tx_cbdp->cbd_bufaddr + x)) =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200905 urb->buffer[epi->sent + x];
906 }
907 tx_cbdp->cbd_datlen = pkt_len;
Wolfgang Denke2601822006-06-14 18:14:56 +0200908 tx_cbdp->cbd_sc |= (CBD_TX_BITMASK | ep_ref[ep].pid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200909 __asm__ ("eieio");
910
Wolfgang Denke2601822006-06-14 18:14:56 +0200911#ifdef __SIMULATE_ERROR__
912 if (++err_poison_test == 2) {
913 err_poison_test = 0;
914 tx_cbdp->cbd_sc &= ~TX_BD_TC;
915 }
916#endif
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200917
Wolfgang Denke2601822006-06-14 18:14:56 +0200918 usbp->uscom = (USCOM_STR | ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200919
Wolfgang Denke2601822006-06-14 18:14:56 +0200920 while (!(usbp->usber & USB_E_TXB)) {
921 ret = mpc8xx_udc_tx_irq (ep);
922 if (ret == -1) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200923 /* TX timeout */
924 break;
Wolfgang Denke2601822006-06-14 18:14:56 +0200925 } else if (ret == -2) {
926 if (usbp->usber & USB_E_TXB) {
927 usbp->usber |= USB_E_TXB;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200928 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200929 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200930 return -1;
931 }
932 };
933
Wolfgang Denke2601822006-06-14 18:14:56 +0200934 if (usbp->usber & USB_E_TXB) {
935 usbp->usber |= USB_E_TXB;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200936 }
937
938 /* ACK must be present <= 18bit times from TX */
Wolfgang Denke2601822006-06-14 18:14:56 +0200939 if (ret == -1) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200940 continue;
941 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200942
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200943 /* TX ACK : USB 2.0 8.7.2, Toggle PID, Advance TX */
944 epi->sent += pkt_len;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +0900945 epi->last = min(urb->actual_length - epi->sent, epi->tx_packetSize);
Wolfgang Denke2601822006-06-14 18:14:56 +0200946 TOGGLE_TX_PID (ep_ref[ep].pid);
947
948 if (epi->sent >= epi->tx_urb->actual_length) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200949
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200950 epi->tx_urb->actual_length = 0;
951 epi->sent = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200952
953 if (ep_ref[ep].sc & EP_SEND_ZLP) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200954 ep_ref[ep].sc &= ~EP_SEND_ZLP;
Wolfgang Denke2601822006-06-14 18:14:56 +0200955 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200956 return 0;
957 }
958 }
959 }
Wolfgang Denke2601822006-06-14 18:14:56 +0200960
961 ERR ("TX fail, endpoint 0x%x tx bytes 0x%x/0x%x\n", ep, epi->sent,
962 epi->tx_urb->actual_length);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200963
964 return -1;
965}
966
967/* mpc8xx_udc_dump_request
968 *
969 * Dump a control request to console
970 */
Wolfgang Denke2601822006-06-14 18:14:56 +0200971static void mpc8xx_udc_dump_request (struct usb_device_request *request)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200972{
Wolfgang Denke2601822006-06-14 18:14:56 +0200973 DBG ("bmRequestType:%02x bRequest:%02x wValue:%04x "
974 "wIndex:%04x wLength:%04x ?\n",
975 request->bmRequestType,
976 request->bRequest,
977 request->wValue, request->wIndex, request->wLength);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200978
979 return;
980}
981
Wolfgang Denke2601822006-06-14 18:14:56 +0200982/* mpc8xx_udc_ep0_rx_setup
983 *
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200984 * Decode received ep0 SETUP packet. return non-zero on error
985 */
986static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp)
987{
988 unsigned int x = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +0200989 struct urb *purb = ep_ref[0].urb;
990 struct usb_endpoint_instance *epi =
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200991 &udc_device->bus->endpoint_array[0];
992
Wolfgang Denke2601822006-06-14 18:14:56 +0200993 for (; x < rx_cbdp->cbd_datlen; x++) {
994 *(((unsigned char *) &ep_ref[0].urb->device_request) + x) =
995 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200996 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +0200997
Wolfgang Denke2601822006-06-14 18:14:56 +0200998 mpc8xx_udc_clear_rxbd (rx_cbdp);
999
1000 if (ep0_recv_setup (purb)) {
1001 mpc8xx_udc_dump_request (&purb->device_request);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001002 return -1;
1003 }
1004
Wolfgang Denke2601822006-06-14 18:14:56 +02001005 if ((purb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001006 == USB_REQ_HOST2DEVICE) {
1007
Wolfgang Denke2601822006-06-14 18:14:56 +02001008 switch (purb->device_request.bRequest) {
1009 case USB_REQ_SET_ADDRESS:
1010 /* Send the Status OUT ZLP */
1011 ep_ref[0].pid = TX_BD_PID_DATA1;
1012 purb->actual_length = 0;
1013 mpc8xx_udc_init_tx (epi, purb);
1014 mpc8xx_udc_ep_tx (epi);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001015
Wolfgang Denke2601822006-06-14 18:14:56 +02001016 /* Move to the addressed state */
1017 usbp->usaddr = udc_device->address;
1018 mpc8xx_udc_state_transition_up (udc_device->device_state,
1019 STATE_ADDRESSED);
1020 return 0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001021
Wolfgang Denke2601822006-06-14 18:14:56 +02001022 case USB_REQ_SET_CONFIGURATION:
1023 if (!purb->device_request.wValue) {
1024 /* Respond at default address */
1025 usbp->usaddr = 0x00;
1026 mpc8xx_udc_state_transition_down (udc_device->device_state,
1027 STATE_ADDRESSED);
1028 } else {
1029 /* TODO: Support multiple configurations */
1030 mpc8xx_udc_state_transition_up (udc_device->device_state,
1031 STATE_CONFIGURED);
1032 for (x = 1; x < MAX_ENDPOINTS; x++) {
1033 if ((udc_device->bus->endpoint_array[x].endpoint_address & USB_ENDPOINT_DIR_MASK)
1034 == USB_DIR_IN) {
1035 ep_ref[x].pid = TX_BD_PID_DATA0;
1036 } else {
1037 ep_ref[x].pid = RX_BD_PID_DATA0;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001038 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001039 /* Set configuration must unstall endpoints */
1040 usbp->usep[x] &= ~STALL_BITMASK;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001041 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001042 }
1043 break;
1044 default:
1045 /* CDC/Vendor specific */
1046 break;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001047 }
1048
1049 /* Send ZLP as ACK in Status OUT phase */
1050 ep_ref[0].pid = TX_BD_PID_DATA1;
1051 purb->actual_length = 0;
Wolfgang Denke2601822006-06-14 18:14:56 +02001052 mpc8xx_udc_init_tx (epi, purb);
1053 mpc8xx_udc_ep_tx (epi);
1054
1055 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001056
Wolfgang Denke2601822006-06-14 18:14:56 +02001057 if (purb->actual_length) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001058 ep_ref[0].pid = TX_BD_PID_DATA1;
Wolfgang Denke2601822006-06-14 18:14:56 +02001059 mpc8xx_udc_init_tx (epi, purb);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001060
Wolfgang Denke2601822006-06-14 18:14:56 +02001061 if (!(purb->actual_length % EP0_MAX_PACKET_SIZE)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001062 ep_ref[0].sc |= EP_SEND_ZLP;
1063 }
1064
Wolfgang Denke2601822006-06-14 18:14:56 +02001065 if (purb->device_request.wValue ==
1066 USB_DESCRIPTOR_TYPE_DEVICE) {
1067 if (le16_to_cpu (purb->device_request.wLength)
1068 > purb->actual_length) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001069 /* Send EP0_MAX_PACKET_SIZE bytes
1070 * unless correct size requested.
1071 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001072 if (purb->actual_length > epi->tx_packetSize) {
1073 purb->actual_length = epi->tx_packetSize;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001074 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001075 }
1076 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001077 mpc8xx_udc_ep_tx (epi);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001078
Wolfgang Denke2601822006-06-14 18:14:56 +02001079 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001080 /* Corrupt SETUP packet? */
Wolfgang Denke2601822006-06-14 18:14:56 +02001081 ERR ("Zero length data or SETUP with DATA-IN phase ?\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001082 return 1;
1083 }
1084 }
1085 return 0;
1086}
1087
1088/* mpc8xx_udc_init_tx
1089 *
1090 * Setup some basic parameters for a TX transaction
1091 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001092static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
1093 struct urb *tx_urb)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001094{
1095 epi->sent = 0;
1096 epi->last = 0;
1097 epi->tx_urb = tx_urb;
1098}
1099
1100/* mpc8xx_udc_ep0_rx
1101 *
1102 * Receive ep0/control USB data. Parse and possibly send a response.
1103 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001104static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001105{
Wolfgang Denke2601822006-06-14 18:14:56 +02001106 if (rx_cbdp->cbd_sc & RX_BD_PID_SETUP) {
1107
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001108 /* Unconditionally accept SETUP packets */
Wolfgang Denke2601822006-06-14 18:14:56 +02001109 if (mpc8xx_udc_ep0_rx_setup (rx_cbdp)) {
1110 mpc8xx_udc_stall (0);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001111 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001112
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001113 } else {
Wolfgang Denke2601822006-06-14 18:14:56 +02001114
1115 mpc8xx_udc_clear_rxbd (rx_cbdp);
1116
1117 if ((rx_cbdp->cbd_datlen - 2)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001118 /* SETUP with a DATA phase
Wolfgang Denke2601822006-06-14 18:14:56 +02001119 * outside of SETUP packet.
1120 * Reply with STALL.
1121 */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001122 mpc8xx_udc_stall (0);
1123 }
1124 }
1125}
1126
1127/* mpc8xx_udc_epn_rx
1128 *
1129 * Receive some data from cbd into USB system urb data abstraction
Wolfgang Denke2601822006-06-14 18:14:56 +02001130 * Upper layers should NAK if there is insufficient RX data space
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001131 */
1132static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp)
1133{
1134 struct usb_endpoint_instance *epi = 0;
1135 struct urb *urb = 0;
1136 unsigned int x = 0;
1137
Wolfgang Denke2601822006-06-14 18:14:56 +02001138 if (epid >= MAX_ENDPOINTS || !rx_cbdp->cbd_datlen) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001139 return 0;
1140 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001141
1142 /* USB 2.0 PDF section 8.6.4
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001143 * Discard data with invalid PID it is a resend.
1144 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001145 if (ep_ref[epid].pid != (rx_cbdp->cbd_sc & 0xC0)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001146 return 1;
1147 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001148 TOGGLE_RX_PID (ep_ref[epid].pid);
1149
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001150 epi = &udc_device->bus->endpoint_array[epid];
1151 urb = epi->rcv_urb;
1152
Wolfgang Denke2601822006-06-14 18:14:56 +02001153 for (; x < (rx_cbdp->cbd_datlen - 2); x++) {
1154 *((unsigned char *) (urb->buffer + urb->actual_length + x)) =
1155 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001156 }
1157
Wolfgang Denke2601822006-06-14 18:14:56 +02001158 if (x) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001159 usbd_rcv_complete (epi, x, 0);
Wolfgang Denke2601822006-06-14 18:14:56 +02001160 if (ep_ref[epid].urb->status == RECV_ERROR) {
1161 DBG ("RX error unset NAK\n");
1162 udc_unset_nak (epid);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001163 }
Wolfgang Denke2601822006-06-14 18:14:56 +02001164 }
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001165 return x;
1166}
1167
1168/* mpc8xx_udc_clock_init
1169 *
Wolfgang Denke2601822006-06-14 18:14:56 +02001170 * Obtain a clock reference for Full Speed Signaling
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001171 */
Wolfgang Denke2601822006-06-14 18:14:56 +02001172static void mpc8xx_udc_clock_init (volatile immap_t * immr,
1173 volatile cpm8xx_t * cp)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001174{
1175
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001176#if defined(CONFIG_SYS_USB_EXTC_CLK)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001177
1178 /* This has been tested with a 48MHz crystal on CLK6 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001179 switch (CONFIG_SYS_USB_EXTC_CLK) {
Wolfgang Denke2601822006-06-14 18:14:56 +02001180 case 1:
1181 immr->im_ioport.iop_papar |= 0x0100;
1182 immr->im_ioport.iop_padir &= ~0x0100;
1183 cp->cp_sicr |= 0x24;
1184 break;
1185 case 2:
1186 immr->im_ioport.iop_papar |= 0x0200;
1187 immr->im_ioport.iop_padir &= ~0x0200;
1188 cp->cp_sicr |= 0x2D;
1189 break;
1190 case 3:
1191 immr->im_ioport.iop_papar |= 0x0400;
1192 immr->im_ioport.iop_padir &= ~0x0400;
1193 cp->cp_sicr |= 0x36;
1194 break;
1195 case 4:
1196 immr->im_ioport.iop_papar |= 0x0800;
1197 immr->im_ioport.iop_padir &= ~0x0800;
1198 cp->cp_sicr |= 0x3F;
1199 break;
1200 default:
1201 udc_state = STATE_ERROR;
1202 break;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001203 }
1204
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001205#elif defined(CONFIG_SYS_USB_BRGCLK)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001206
Wolfgang Denke2601822006-06-14 18:14:56 +02001207 /* This has been tested with brgclk == 50MHz */
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001208 int divisor = 0;
1209
Wolfgang Denke2601822006-06-14 18:14:56 +02001210 if (gd->cpu_clk < 48000000L) {
1211 ERR ("brgclk is too slow for full-speed USB!\n");
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001212 udc_state = STATE_ERROR;
1213 return;
1214 }
1215
Wolfgang Denkaf0501a2008-10-19 02:35:50 +02001216 /* Assume the brgclk is 'good enough', we want !(gd->cpu_clk%48MHz)
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001217 * but, can /probably/ live with close-ish alternative rates.
Wolfgang Denke2601822006-06-14 18:14:56 +02001218 */
1219 divisor = (gd->cpu_clk / 48000000L) - 1;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001220 cp->cp_sicr &= ~0x0000003F;
Wolfgang Denke2601822006-06-14 18:14:56 +02001221
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001222 switch (CONFIG_SYS_USB_BRGCLK) {
Wolfgang Denke2601822006-06-14 18:14:56 +02001223 case 1:
1224 cp->cp_brgc1 |= (divisor | CPM_BRG_EN);
1225 cp->cp_sicr &= ~0x2F;
1226 break;
1227 case 2:
1228 cp->cp_brgc2 |= (divisor | CPM_BRG_EN);
1229 cp->cp_sicr |= 0x00000009;
1230 break;
1231 case 3:
1232 cp->cp_brgc3 |= (divisor | CPM_BRG_EN);
1233 cp->cp_sicr |= 0x00000012;
1234 break;
1235 case 4:
1236 cp->cp_brgc4 = (divisor | CPM_BRG_EN);
1237 cp->cp_sicr |= 0x0000001B;
1238 break;
1239 default:
1240 udc_state = STATE_ERROR;
1241 break;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001242 }
1243
1244#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001245#error "CONFIG_SYS_USB_EXTC_CLK or CONFIG_SYS_USB_BRGCLK must be defined"
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001246#endif
1247
1248}
1249
1250/* mpc8xx_udc_cbd_attach
1251 *
1252 * attach a cbd to and endpoint
1253 */
1254static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size)
1255{
Wolfgang Denke2601822006-06-14 18:14:56 +02001256
1257 if (!tx_cbd[ep] || !rx_cbd[ep] || ep >= MAX_ENDPOINTS) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001258 udc_state = STATE_ERROR;
1259 return;
1260 }
1261
Wolfgang Denke2601822006-06-14 18:14:56 +02001262 if (tx_size > USB_MAX_PKT || rx_size > USB_MAX_PKT ||
1263 (!tx_size && !rx_size)) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001264 udc_state = STATE_ERROR;
1265 return;
1266 }
1267
1268 /* Attach CBD to appropiate Parameter RAM Endpoint data structure */
Wolfgang Denke2601822006-06-14 18:14:56 +02001269 if (rx_size) {
1270 endpoints[ep]->rbase = (u32) rx_cbd[rx_ct];
1271 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001272 rx_ct++;
1273
Wolfgang Denke2601822006-06-14 18:14:56 +02001274 if (!ep) {
1275
1276 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001277 rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
1278 rx_ct++;
1279
Wolfgang Denke2601822006-06-14 18:14:56 +02001280 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001281 rx_ct += 2;
Wolfgang Denke2601822006-06-14 18:14:56 +02001282 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001283 rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
1284 rx_ct++;
1285 }
1286
1287 /* Where we expect to RX data on this endpoint */
Wolfgang Denke2601822006-06-14 18:14:56 +02001288 ep_ref[ep].prx = rx_cbd[rx_ct - 1];
1289 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001290
1291 ep_ref[ep].prx = 0;
1292 endpoints[ep]->rbase = 0;
1293 endpoints[ep]->rbptr = 0;
1294 }
1295
Wolfgang Denke2601822006-06-14 18:14:56 +02001296 if (tx_size) {
1297 endpoints[ep]->tbase = (u32) tx_cbd[tx_ct];
1298 endpoints[ep]->tbptr = (u32) tx_cbd[tx_ct];
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001299 tx_ct++;
Wolfgang Denke2601822006-06-14 18:14:56 +02001300 } else {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001301 endpoints[ep]->tbase = 0;
1302 endpoints[ep]->tbptr = 0;
1303 }
1304
1305 endpoints[ep]->tstate = 0;
1306 endpoints[ep]->tbcnt = 0;
1307 endpoints[ep]->mrblr = EP_MAX_PKT;
Wolfgang Denke2601822006-06-14 18:14:56 +02001308 endpoints[ep]->rfcr = 0x18;
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001309 endpoints[ep]->tfcr = 0x18;
1310 ep_ref[ep].sc |= EP_ATTACHED;
1311
Wolfgang Denke2601822006-06-14 18:14:56 +02001312 DBG ("ep %d rbase 0x%08x rbptr 0x%08x tbase 0x%08x tbptr 0x%08x prx = %p\n",
1313 ep, endpoints[ep]->rbase, endpoints[ep]->rbptr,
1314 endpoints[ep]->tbase, endpoints[ep]->tbptr,
1315 ep_ref[ep].prx);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001316
1317 return;
1318}
1319
1320/* mpc8xx_udc_cbd_init
1321 *
1322 * Allocate space for a cbd and allocate TX/RX data space
1323 */
1324static void mpc8xx_udc_cbd_init (void)
1325{
1326 int i = 0;
1327
Wolfgang Denke2601822006-06-14 18:14:56 +02001328 for (; i < TX_RING_SIZE; i++) {
1329 tx_cbd[i] = (cbd_t *)
1330 mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int));
1331 }
1332
1333 for (i = 0; i < RX_RING_SIZE; i++) {
1334 rx_cbd[i] = (cbd_t *)
1335 mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001336 }
1337
Wolfgang Denke2601822006-06-14 18:14:56 +02001338 for (i = 0; i < TX_RING_SIZE; i++) {
1339 tx_cbd[i]->cbd_bufaddr =
1340 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001341
Wolfgang Denke2601822006-06-14 18:14:56 +02001342 tx_cbd[i]->cbd_sc = (TX_BD_I | TX_BD_W);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001343 tx_cbd[i]->cbd_datlen = 0x0000;
1344 }
1345
1346
Wolfgang Denke2601822006-06-14 18:14:56 +02001347 for (i = 0; i < RX_RING_SIZE; i++) {
1348 rx_cbd[i]->cbd_bufaddr =
1349 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001350 rx_cbd[i]->cbd_sc = (RX_BD_I | RX_BD_E);
1351 rx_cbd[i]->cbd_datlen = 0x0000;
1352
1353 }
1354
1355 return;
1356}
1357
1358/* mpc8xx_udc_endpoint_init
1359 *
1360 * Attach an endpoint to some dpram
1361 */
1362static void mpc8xx_udc_endpoint_init (void)
1363{
1364 int i = 0;
1365
Wolfgang Denke2601822006-06-14 18:14:56 +02001366 for (; i < MAX_ENDPOINTS; i++) {
1367 endpoints[i] = (usb_epb_t *)
1368 mpc8xx_udc_alloc (sizeof (usb_epb_t), 32);
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001369 }
1370}
1371
1372/* mpc8xx_udc_alloc
1373 *
Wolfgang Denke2601822006-06-14 18:14:56 +02001374 * Grab the address of some dpram
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001375 */
1376static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment)
1377{
1378 u32 retaddr = address_base;
Wolfgang Denke2601822006-06-14 18:14:56 +02001379
1380 while (retaddr % alignment) {
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001381 retaddr++;
Wolfgang Denke2601822006-06-14 18:14:56 +02001382 }
1383 address_base += data_size;
1384
Wolfgang Denk3f0137b2006-06-14 17:45:53 +02001385 return retaddr;
1386}