blob: 0207d391a8e07400e63e25fbfae98d70ff6fd6d8 [file] [log] [blame]
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001/*
2 * Copyright (C) 2006 by Bryan O'Donoghue, CodeHermit
Wolfgang Denk386eda02006-06-14 18:14:56 +02003 * bodonoghue@CodeHermit.ie
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02004 *
5 * References
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +02006 * DasUBoot/drivers/usb/gadget/omap1510_udc.c, for design and implementation
Marcel Ziswiler7817cb22007-12-30 03:30:46 +01007 * ideas.
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02008 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020010 */
11
12/*
13 * Notes :
Wolfgang Denk386eda02006-06-14 18:14:56 +020014 * 1. #define __SIMULATE_ERROR__ to inject a CRC error into every 2nd TX
Wolfgang Denk16c8d5e2006-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 Denk386eda02006-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 Denk16c8d5e2006-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 Denk386eda02006-06-14 18:14:56 +020031 * the same size is reported for 14 bytes received as 16 bytes received
Wolfgang Denk16c8d5e2006-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 Denk386eda02006-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 Denk16c8d5e2006-06-14 17:45:53 +020043 *
44 */
45#include <common.h>
46#include <config.h>
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020047#include <commproc.h>
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020048#include <usbdevice.h>
49#include <usb/mpc8xx_udc.h>
50
51#include "ep0.h"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020052
Wolfgang Denk1218abf2007-09-15 20:48:41 +020053DECLARE_GLOBAL_DATA_PTR;
54
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020055#define ERR(fmt, args...)\
56 serial_printf("ERROR : [%s] %s:%d: "fmt,\
57 __FILE__,__FUNCTION__,__LINE__, ##args)
58#ifdef __DEBUG_UDC__
Wolfgang Denk386eda02006-06-14 18:14:56 +020059#define DBG(fmt,args...)\
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020060 serial_printf("[%s] %s:%d: "fmt,\
61 __FILE__,__FUNCTION__,__LINE__, ##args)
62#else
Wolfgang Denk386eda02006-06-14 18:14:56 +020063#define DBG(fmt,args...)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020064#endif
65
66/* Static Data */
67#ifdef __SIMULATE_ERROR__
Wolfgang Denk386eda02006-06-14 18:14:56 +020068static char err_poison_test = 0;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020069#endif
70static struct mpc8xx_ep ep_ref[MAX_ENDPOINTS];
71static u32 address_base = STATE_NOT_READY;
72static mpc8xx_udc_state_t udc_state = 0;
73static struct usb_device_instance *udc_device = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +020074static volatile usb_epb_t *endpoints[MAX_ENDPOINTS];
75static volatile cbd_t *tx_cbd[TX_RING_SIZE];
76static volatile cbd_t *rx_cbd[RX_RING_SIZE];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020077static volatile immap_t *immr = 0;
78static volatile cpm8xx_t *cp = 0;
79static volatile usb_pram_t *usb_paramp = 0;
80static volatile usb_t *usbp = 0;
81static int rx_ct = 0;
82static int tx_ct = 0;
83
84/* Static Function Declarations */
85static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
Wolfgang Denk386eda02006-06-14 18:14:56 +020086 usb_device_state_t final);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020087static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
Wolfgang Denk386eda02006-06-14 18:14:56 +020088 usb_device_state_t final);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020089static void mpc8xx_udc_stall (unsigned int ep);
Wolfgang Denk386eda02006-06-14 18:14:56 +020090static void mpc8xx_udc_flush_tx_fifo (int epid);
91static void mpc8xx_udc_flush_rx_fifo (void);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020092static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp);
Wolfgang Denk386eda02006-06-14 18:14:56 +020093static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
94 struct urb *tx_urb);
95static void mpc8xx_udc_dump_request (struct usb_device_request *request);
96static void mpc8xx_udc_clock_init (volatile immap_t * immr,
97 volatile cpm8xx_t * cp);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +020098static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi);
99static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200100static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200101static void mpc8xx_udc_cbd_init (void);
102static void mpc8xx_udc_endpoint_init (void);
103static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size);
104static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment);
105static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp);
106static void mpc8xx_udc_set_nak (unsigned int ep);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200107static short mpc8xx_udc_handle_txerr (void);
108static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200109
110/******************************************************************************
Wolfgang Denk386eda02006-06-14 18:14:56 +0200111 Global Linkage
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200112 *****************************************************************************/
113
114/* udc_init
115 *
116 * Do initial bus gluing
117 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200118int udc_init (void)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200119{
120 /* Init various pointers */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200121 immr = (immap_t *) CONFIG_SYS_IMMR;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200122 cp = (cpm8xx_t *) & (immr->im_cpm);
123 usb_paramp = (usb_pram_t *) & (cp->cp_dparam[PROFF_USB]);
124 usbp = (usb_t *) & (cp->cp_scc[0]);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200125
Wolfgang Denk386eda02006-06-14 18:14:56 +0200126 memset (ep_ref, 0x00, (sizeof (struct mpc8xx_ep) * MAX_ENDPOINTS));
127
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200128 udc_device = 0;
129 udc_state = STATE_NOT_READY;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200130
131 usbp->usmod = 0x00;
132 usbp->uscom = 0;
133
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200134 /* Set USB Frame #0, Respond at Address & Get a clock source */
135 usbp->usaddr = 0x00;
136 mpc8xx_udc_clock_init (immr, cp);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200137
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200138 /* PA15, PA14 as perhiperal USBRXD and USBOE */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200139 immr->im_ioport.iop_padir &= ~0x0003;
140 immr->im_ioport.iop_papar |= 0x0003;
141
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200142 /* PC11/PC10 as peripheral USBRXP USBRXN */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200143 immr->im_ioport.iop_pcso |= 0x0030;
144
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200145 /* PC7/PC6 as perhiperal USBTXP and USBTXN */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200146 immr->im_ioport.iop_pcdir |= 0x0300;
147 immr->im_ioport.iop_pcpar |= 0x0300;
148
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200149 /* Set the base address */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200150 address_base = (u32) (cp->cp_dpmem + CPM_USB_BASE);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200151
152 /* Initialise endpoints and circular buffers */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200153 mpc8xx_udc_endpoint_init ();
154 mpc8xx_udc_cbd_init ();
155
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200156 /* Assign allocated Dual Port Endpoint descriptors */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200157 usb_paramp->ep0ptr = (u32) endpoints[0];
158 usb_paramp->ep1ptr = (u32) endpoints[1];
159 usb_paramp->ep2ptr = (u32) endpoints[2];
160 usb_paramp->ep3ptr = (u32) endpoints[3];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200161 usb_paramp->frame_n = 0;
162
Wolfgang Denk386eda02006-06-14 18:14:56 +0200163 DBG ("ep0ptr=0x%08x ep1ptr=0x%08x ep2ptr=0x%08x ep3ptr=0x%08x\n",
164 usb_paramp->ep0ptr, usb_paramp->ep1ptr, usb_paramp->ep2ptr,
165 usb_paramp->ep3ptr);
166
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200167 return 0;
168}
169
170/* udc_irq
171 *
172 * Poll for whatever events may have occured
173 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200174void udc_irq (void)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200175{
176 int epid = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200177 volatile cbd_t *rx_cbdp = 0;
178 volatile cbd_t *rx_cbdp_base = 0;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200179
Wolfgang Denk386eda02006-06-14 18:14:56 +0200180 if (udc_state != STATE_READY) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200181 return;
182 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200183
184 if (usbp->usber & USB_E_BSY) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200185 /* This shouldn't happen. If it does then it's a bug ! */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200186 usbp->usber |= USB_E_BSY;
187 mpc8xx_udc_flush_rx_fifo ();
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200188 }
189
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200190 /* Scan all RX/Bidirectional Endpoints for RX data. */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200191 for (epid = 0; epid < MAX_ENDPOINTS; epid++) {
192 if (!ep_ref[epid].prx) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200193 continue;
194 }
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200195 rx_cbdp = rx_cbdp_base = ep_ref[epid].prx;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200196
197 do {
198 if (!(rx_cbdp->cbd_sc & RX_BD_E)) {
199
200 if (rx_cbdp->cbd_sc & 0x1F) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200201 /* Corrupt data discard it.
Wolfgang Denk386eda02006-06-14 18:14:56 +0200202 * Controller has NAK'd this packet.
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200203 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200204 mpc8xx_udc_clear_rxbd (rx_cbdp);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200205
Wolfgang Denk386eda02006-06-14 18:14:56 +0200206 } else {
207 if (!epid) {
208 mpc8xx_udc_ep0_rx (rx_cbdp);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200209
Wolfgang Denk386eda02006-06-14 18:14:56 +0200210 } else {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200211 /* Process data */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200212 mpc8xx_udc_set_nak (epid);
213 mpc8xx_udc_epn_rx (epid, rx_cbdp);
214 mpc8xx_udc_clear_rxbd (rx_cbdp);
215 }
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200216 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200217
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200218 /* Advance RX CBD pointer */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200219 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200220 ep_ref[epid].prx = rx_cbdp;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200221 } else {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200222 /* Advance RX CBD pointer */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200223 mpc8xx_udc_advance_rx (&rx_cbdp, epid);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200224 }
225
Wolfgang Denk386eda02006-06-14 18:14:56 +0200226 } while (rx_cbdp != rx_cbdp_base);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200227 }
228
229 /* Handle TX events as appropiate, the correct place to do this is
230 * in a tx routine. Perhaps TX on epn was pre-empted by ep0
231 */
232
Wolfgang Denk386eda02006-06-14 18:14:56 +0200233 if (usbp->usber & USB_E_TXB) {
234 usbp->usber |= USB_E_TXB;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200235 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200236
237 if (usbp->usber & (USB_TX_ERRMASK)) {
238 mpc8xx_udc_handle_txerr ();
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200239 }
240
241 /* Switch to the default state, respond at the default address */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200242 if (usbp->usber & USB_E_RESET) {
243 usbp->usber |= USB_E_RESET;
244 usbp->usaddr = 0x00;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200245 udc_device->device_state = STATE_DEFAULT;
246 }
247
Wolfgang Denk386eda02006-06-14 18:14:56 +0200248 /* if(usbp->usber&USB_E_IDLE){
249 We could suspend here !
250 usbp->usber|=USB_E_IDLE;
251 DBG("idle state change\n");
252 }
253 if(usbp->usbs){
254 We could resume here when IDLE is deasserted !
255 Not worth doing, so long as we are self powered though.
256 }
257 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200258
259 return;
260}
261
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200262/* udc_endpoint_write
263 *
264 * Write some data to an endpoint
265 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200266int udc_endpoint_write (struct usb_endpoint_instance *epi)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200267{
268 int ep = 0;
269 short epid = 1, unnak = 0, ret = 0;
270
Wolfgang Denk386eda02006-06-14 18:14:56 +0200271 if (udc_state != STATE_READY) {
272 ERR ("invalid udc_state != STATE_READY!\n");
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200273 return -1;
274 }
275
Wolfgang Denk386eda02006-06-14 18:14:56 +0200276 if (!udc_device || !epi) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200277 return -1;
278 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200279
280 if (udc_device->device_state != STATE_CONFIGURED) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200281 return -1;
282 }
283
284 ep = epi->endpoint_address & 0x03;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200285 if (ep >= MAX_ENDPOINTS) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200286 return -1;
287 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200288
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200289 /* Set NAK for all RX endpoints during TX */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200290 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200291
292 /* Don't set NAK on DATA IN/CONTROL endpoints */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200293 if (ep_ref[epid].sc & USB_DIR_IN) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200294 continue;
295 }
296
Wolfgang Denk386eda02006-06-14 18:14:56 +0200297 if (!(usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK))) {
298 unnak |= 1 << epid;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200299 }
300
Wolfgang Denk386eda02006-06-14 18:14:56 +0200301 mpc8xx_udc_set_nak (epid);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200302 }
303
Wolfgang Denk386eda02006-06-14 18:14:56 +0200304 mpc8xx_udc_init_tx (&udc_device->bus->endpoint_array[ep],
305 epi->tx_urb);
306 ret = mpc8xx_udc_ep_tx (&udc_device->bus->endpoint_array[ep]);
307
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200308 /* Remove temporary NAK */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200309 for (epid = 1; epid < MAX_ENDPOINTS; epid++) {
310 if (unnak & (1 << epid)) {
311 udc_unset_nak (epid);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200312 }
313 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200314
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200315 return ret;
316}
317
318/* mpc8xx_udc_assign_urb
319 *
320 * Associate a given urb to an endpoint TX or RX transmit/receive buffers
321 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200322static int mpc8xx_udc_assign_urb (int ep, char direction)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200323{
324 struct usb_endpoint_instance *epi = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200325
326 if (ep >= MAX_ENDPOINTS) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200327 goto err;
328 }
329 epi = &udc_device->bus->endpoint_array[ep];
Wolfgang Denk386eda02006-06-14 18:14:56 +0200330 if (!epi) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200331 goto err;
332 }
333
Wolfgang Denk386eda02006-06-14 18:14:56 +0200334 if (!ep_ref[ep].urb) {
335 ep_ref[ep].urb = usbd_alloc_urb (udc_device, udc_device->bus->endpoint_array);
336 if (!ep_ref[ep].urb) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200337 goto err;
338 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200339 } else {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200340 ep_ref[ep].urb->actual_length = 0;
341 }
342
Wolfgang Denk386eda02006-06-14 18:14:56 +0200343 switch (direction) {
344 case USB_DIR_IN:
345 epi->tx_urb = ep_ref[ep].urb;
346 break;
347 case USB_DIR_OUT:
348 epi->rcv_urb = ep_ref[ep].urb;
349 break;
350 default:
351 goto err;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200352 }
353 return 0;
354
Wolfgang Denk386eda02006-06-14 18:14:56 +0200355 err:
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200356 udc_state = STATE_ERROR;
357 return -1;
358}
359
360/* udc_setup_ep
361 *
362 * Associate U-Boot software endpoints to mpc8xx endpoint parameter ram
363 * Isochronous endpoints aren't yet supported!
364 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200365void udc_setup_ep (struct usb_device_instance *device, unsigned int ep,
366 struct usb_endpoint_instance *epi)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200367{
368 uchar direction = 0;
369 int ep_attrib = 0;
370
Wolfgang Denk386eda02006-06-14 18:14:56 +0200371 if (epi && (ep < MAX_ENDPOINTS)) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200372
Wolfgang Denk386eda02006-06-14 18:14:56 +0200373 if (ep == 0) {
374 if (epi->rcv_attributes != USB_ENDPOINT_XFER_CONTROL
375 || epi->tx_attributes !=
376 USB_ENDPOINT_XFER_CONTROL) {
377
378 /* ep0 must be a control endpoint */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200379 udc_state = STATE_ERROR;
380 return;
381
382 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200383 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
384 mpc8xx_udc_cbd_attach (ep, epi->tx_packetSize,
385 epi->rcv_packetSize);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200386 }
387 usbp->usep[ep] = 0x0000;
388 return;
389 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200390
391 if ((epi->endpoint_address & USB_ENDPOINT_DIR_MASK)
392 == USB_DIR_IN) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200393
394 direction = 1;
395 ep_attrib = epi->tx_attributes;
396 epi->rcv_packetSize = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200397 ep_ref[ep].sc |= USB_DIR_IN;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200398 } else {
Wolfgang Denk386eda02006-06-14 18:14:56 +0200399
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200400 direction = 0;
401 ep_attrib = epi->rcv_attributes;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200402 epi->tx_packetSize = 0;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200403 ep_ref[ep].sc &= ~USB_DIR_IN;
404 }
405
Wolfgang Denk386eda02006-06-14 18:14:56 +0200406 if (mpc8xx_udc_assign_urb (ep, epi->endpoint_address
407 & USB_ENDPOINT_DIR_MASK)) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200408 return;
409 }
410
Wolfgang Denk386eda02006-06-14 18:14:56 +0200411 switch (ep_attrib) {
412 case USB_ENDPOINT_XFER_CONTROL:
413 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
414 mpc8xx_udc_cbd_attach (ep,
415 epi->tx_packetSize,
416 epi->rcv_packetSize);
417 }
418 usbp->usep[ep] = ep << 12;
419 epi->rcv_urb = epi->tx_urb = ep_ref[ep].urb;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200420
Wolfgang Denk386eda02006-06-14 18:14:56 +0200421 break;
422 case USB_ENDPOINT_XFER_BULK:
423 case USB_ENDPOINT_XFER_INT:
424 if (!(ep_ref[ep].sc & EP_ATTACHED)) {
425 if (direction) {
426 mpc8xx_udc_cbd_attach (ep,
427 epi->tx_packetSize,
428 0);
429 } else {
430 mpc8xx_udc_cbd_attach (ep,
431 0,
432 epi->rcv_packetSize);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200433 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200434 }
435 usbp->usep[ep] = (ep << 12) | ((ep_attrib) << 8);
436
437 break;
438 case USB_ENDPOINT_XFER_ISOC:
439 default:
440 serial_printf ("Error endpoint attrib %d>3\n", ep_attrib);
441 udc_state = STATE_ERROR;
442 break;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200443 }
444 }
445
446}
447
448/* udc_connect
449 *
450 * Move state, switch on the USB
451 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200452void udc_connect (void)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200453{
Wolfgang Denk386eda02006-06-14 18:14:56 +0200454 /* Enable pull-up resistor on D+
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200455 * TODO: fit a pull-up resistor to drive SE0 for > 2.5us
456 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200457
458 if (udc_state != STATE_ERROR) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200459 udc_state = STATE_READY;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200460 usbp->usmod |= USMOD_EN;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200461 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200462}
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200463
464/* udc_disconnect
465 *
466 * Disconnect is not used but, is included for completeness
467 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200468void udc_disconnect (void)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200469{
470 /* Disable pull-up resistor on D-
471 * TODO: fix a pullup resistor to control this
472 */
473
Wolfgang Denk386eda02006-06-14 18:14:56 +0200474 if (udc_state != STATE_ERROR) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200475 udc_state = STATE_NOT_READY;
476 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200477 usbp->usmod &= ~USMOD_EN;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200478}
479
480/* udc_enable
Wolfgang Denk386eda02006-06-14 18:14:56 +0200481 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200482 * Grab an EP0 URB, register interest in a subset of USB events
483 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200484void udc_enable (struct usb_device_instance *device)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200485{
Wolfgang Denk386eda02006-06-14 18:14:56 +0200486 if (udc_state == STATE_ERROR) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200487 return;
488 }
489
490 udc_device = device;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200491
492 if (!ep_ref[0].urb) {
493 ep_ref[0].urb = usbd_alloc_urb (device, device->bus->endpoint_array);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200494 }
495
496 /* Register interest in all events except SOF, enable transceiver */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200497 usbp->usber = 0x03FF;
498 usbp->usbmr = 0x02F7;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200499
500 return;
501}
502
503/* udc_disable
504 *
505 * disable the currently hooked device
506 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200507void udc_disable (void)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200508{
509 int i = 0;
510
Wolfgang Denk386eda02006-06-14 18:14:56 +0200511 if (udc_state == STATE_ERROR) {
512 DBG ("Won't disable UDC. udc_state==STATE_ERROR !\n");
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200513 return;
514 }
515
516 udc_device = 0;
517
Wolfgang Denk386eda02006-06-14 18:14:56 +0200518 for (; i < MAX_ENDPOINTS; i++) {
519 if (ep_ref[i].urb) {
520 usbd_dealloc_urb (ep_ref[i].urb);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200521 ep_ref[i].urb = 0;
522 }
523 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200524
525 usbp->usbmr = 0x00;
526 usbp->usmod = ~USMOD_EN;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200527 udc_state = STATE_NOT_READY;
528}
529
530/* udc_startup_events
531 *
532 * Enable the specified device
533 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200534void udc_startup_events (struct usb_device_instance *device)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200535{
Wolfgang Denk386eda02006-06-14 18:14:56 +0200536 udc_enable (device);
537 if (udc_state == STATE_READY) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200538 usbd_device_event_irq (device, DEVICE_CREATE, 0);
539 }
540}
541
542/* udc_set_nak
Wolfgang Denk386eda02006-06-14 18:14:56 +0200543 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200544 * Allow upper layers to signal lower layers should not accept more RX data
545 *
546 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200547void udc_set_nak (int epid)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200548{
Wolfgang Denk386eda02006-06-14 18:14:56 +0200549 if (epid) {
550 mpc8xx_udc_set_nak (epid);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200551 }
552}
553
Wolfgang Denk386eda02006-06-14 18:14:56 +0200554/* udc_unset_nak
555 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200556 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
557 * Switch off NAKing on this endpoint to accept more data output from host.
558 *
559 */
560void udc_unset_nak (int epid)
561{
Wolfgang Denk386eda02006-06-14 18:14:56 +0200562 if (epid > MAX_ENDPOINTS) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200563 return;
564 }
565
Wolfgang Denk386eda02006-06-14 18:14:56 +0200566 if (usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK)) {
567 usbp->usep[epid] &= ~(USEP_THS_NAK | USEP_RHS_NAK);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200568 __asm__ ("eieio");
569 }
570}
571
572/******************************************************************************
Wolfgang Denk386eda02006-06-14 18:14:56 +0200573 Static Linkage
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200574******************************************************************************/
575
576/* udc_state_transition_up
577 * udc_state_transition_down
578 *
579 * Helper functions to implement device state changes. The device states and
580 * the events that transition between them are:
581 *
582 * STATE_ATTACHED
583 * || /\
584 * \/ ||
585 * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
586 * || /\
587 * \/ ||
588 * STATE_POWERED
589 * || /\
590 * \/ ||
591 * DEVICE_RESET DEVICE_POWER_INTERRUPTION
592 * || /\
593 * \/ ||
594 * STATE_DEFAULT
595 * || /\
596 * \/ ||
597 * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
598 * || /\
599 * \/ ||
600 * STATE_ADDRESSED
601 * || /\
602 * \/ ||
603 * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
604 * || /\
605 * \/ ||
606 * STATE_CONFIGURED
607 *
608 * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
609 * to STATE_CONFIGURED) from the specified initial state to the specified final
610 * state, passing through each intermediate state on the way. If the initial
611 * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
612 * no state transitions will take place.
613 *
614 * udc_state_transition_down transitions down (in the direction from
615 * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
616 * specified final state, passing through each intermediate state on the way.
617 * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
618 * state, then no state transitions will take place.
619 *
620 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200621
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200622static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200623 usb_device_state_t final)
624{
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200625 if (initial < final) {
626 switch (initial) {
627 case STATE_ATTACHED:
628 usbd_device_event_irq (udc_device,
629 DEVICE_HUB_CONFIGURED, 0);
630 if (final == STATE_POWERED)
631 break;
632 case STATE_POWERED:
633 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
634 if (final == STATE_DEFAULT)
635 break;
636 case STATE_DEFAULT:
637 usbd_device_event_irq (udc_device,
638 DEVICE_ADDRESS_ASSIGNED, 0);
639 if (final == STATE_ADDRESSED)
640 break;
641 case STATE_ADDRESSED:
642 usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
643 0);
644 case STATE_CONFIGURED:
645 break;
646 default:
647 break;
648 }
649 }
650}
651
652static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200653 usb_device_state_t final)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200654{
655 if (initial > final) {
656 switch (initial) {
657 case STATE_CONFIGURED:
Wolfgang Denk386eda02006-06-14 18:14:56 +0200658 usbd_device_event_irq (udc_device,
659 DEVICE_DE_CONFIGURED, 0);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200660 if (final == STATE_ADDRESSED)
661 break;
662 case STATE_ADDRESSED:
663 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
664 if (final == STATE_DEFAULT)
665 break;
666 case STATE_DEFAULT:
Wolfgang Denk386eda02006-06-14 18:14:56 +0200667 usbd_device_event_irq (udc_device,
668 DEVICE_POWER_INTERRUPTION, 0);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200669 if (final == STATE_POWERED)
670 break;
671 case STATE_POWERED:
672 usbd_device_event_irq (udc_device, DEVICE_HUB_RESET,
Wolfgang Denk386eda02006-06-14 18:14:56 +0200673 0);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200674 case STATE_ATTACHED:
675 break;
676 default:
677 break;
678 }
679 }
680}
681
682/* mpc8xx_udc_stall
683 *
684 * Force returning of STALL tokens on the given endpoint. Protocol or function
685 * STALL conditions are permissable here
686 */
687static void mpc8xx_udc_stall (unsigned int ep)
688{
689 usbp->usep[ep] |= STALL_BITMASK;
690}
691
692/* mpc8xx_udc_set_nak
693 *
694 * Force returning of NAK responses for the given endpoint as a kind of very
695 * simple flow control
Wolfgang Denk386eda02006-06-14 18:14:56 +0200696 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200697static void mpc8xx_udc_set_nak (unsigned int ep)
698{
699 usbp->usep[ep] |= NAK_BITMASK;
700 __asm__ ("eieio");
701}
702
703/* mpc8xx_udc_handle_txerr
704 *
705 * Handle errors relevant to TX. Return a status code to allow calling
706 * indicative of what if anything happened
707 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200708static short mpc8xx_udc_handle_txerr ()
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200709{
710 short ep = 0, ret = 0;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200711
Wolfgang Denk386eda02006-06-14 18:14:56 +0200712 for (; ep < TX_RING_SIZE; ep++) {
713 if (usbp->usber & (0x10 << ep)) {
714
715 /* Timeout or underrun */
716 if (tx_cbd[ep]->cbd_sc & 0x06) {
717 ret = 1;
718 mpc8xx_udc_flush_tx_fifo (ep);
719
720 } else {
721 if (usbp->usep[ep] & STALL_BITMASK) {
722 if (!ep) {
723 usbp->usep[ep] &= ~STALL_BITMASK;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200724 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200725 } /* else NAK */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200726 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200727 usbp->usber |= (0x10 << ep);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200728 }
729 }
730 return ret;
731}
732
733/* mpc8xx_udc_advance_rx
734 *
735 * Advance cbd rx
736 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200737static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200738{
Wolfgang Denk386eda02006-06-14 18:14:56 +0200739 if ((*rx_cbdp)->cbd_sc & RX_BD_W) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200740 *rx_cbdp = (volatile cbd_t *) (endpoints[epid]->rbase + CONFIG_SYS_IMMR);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200741
742 } else {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200743 (*rx_cbdp)++;
744 }
745}
746
747
748/* mpc8xx_udc_flush_tx_fifo
749 *
750 * Flush a given TX fifo. Assumes one tx cbd per endpoint
751 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200752static void mpc8xx_udc_flush_tx_fifo (int epid)
753{
754 volatile cbd_t *tx_cbdp = 0;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200755
Wolfgang Denk386eda02006-06-14 18:14:56 +0200756 if (epid > MAX_ENDPOINTS) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200757 return;
758 }
759
760 /* TX stop */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200761 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x1D01);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200762 __asm__ ("eieio");
Wolfgang Denk386eda02006-06-14 18:14:56 +0200763 while (immr->im_cpm.cp_cpcr & 0x01);
764
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200765 usbp->uscom = 0x40 | 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200766
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200767 /* reset ring */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200768 tx_cbdp = (cbd_t *) (endpoints[epid]->tbptr + CONFIG_SYS_IMMR);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200769 tx_cbdp->cbd_sc = (TX_BD_I | TX_BD_W);
770
Wolfgang Denk386eda02006-06-14 18:14:56 +0200771
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200772 endpoints[epid]->tptr = endpoints[epid]->tbase;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200773 endpoints[epid]->tstate = 0x00;
774 endpoints[epid]->tbcnt = 0x00;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200775
776 /* TX start */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200777 immr->im_cpm.cp_cpcr = ((epid << 2) | 0x2D01);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200778 __asm__ ("eieio");
Wolfgang Denk386eda02006-06-14 18:14:56 +0200779 while (immr->im_cpm.cp_cpcr & 0x01);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200780
781 return;
782}
783
784/* mpc8xx_udc_flush_rx_fifo
785 *
786 * For the sake of completeness of the namespace, it seems like
787 * a good-design-decision (tm) to include mpc8xx_udc_flush_rx_fifo();
788 * If RX_BD_E is true => a driver bug either here or in an upper layer
789 * not polling frequently enough. If RX_BD_E is true we have told the host
790 * we have accepted data but, the CPM found it had no-where to put that data
791 * which needless to say would be a bad thing.
792 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200793static void mpc8xx_udc_flush_rx_fifo ()
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200794{
795 int i = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200796
797 for (i = 0; i < RX_RING_SIZE; i++) {
798 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
799 ERR ("buf %p used rx data len = 0x%x sc=0x%x!\n",
800 rx_cbd[i], rx_cbd[i]->cbd_datlen,
801 rx_cbd[i]->cbd_sc);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200802
803 }
804 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200805 ERR ("BUG : Input over-run\n");
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200806}
807
808/* mpc8xx_udc_clear_rxbd
Wolfgang Denk386eda02006-06-14 18:14:56 +0200809 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200810 * Release control of RX CBD to CP.
811 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200812static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200813{
814 rx_cbdp->cbd_datlen = 0x0000;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200815 rx_cbdp->cbd_sc = ((rx_cbdp->cbd_sc & RX_BD_W) | (RX_BD_E | RX_BD_I));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200816 __asm__ ("eieio");
817}
818
819/* mpc8xx_udc_tx_irq
820 *
821 * Parse for tx timeout, control RX or USB reset/busy conditions
822 * Return -1 on timeout, -2 on fatal error, else return zero
823 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200824static int mpc8xx_udc_tx_irq (int ep)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200825{
826 int i = 0;
827
Wolfgang Denk386eda02006-06-14 18:14:56 +0200828 if (usbp->usber & (USB_TX_ERRMASK)) {
829 if (mpc8xx_udc_handle_txerr ()) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200830 /* Timeout, controlling function must retry send */
831 return -1;
832 }
833 }
834
Wolfgang Denk386eda02006-06-14 18:14:56 +0200835 if (usbp->usber & (USB_E_RESET | USB_E_BSY)) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200836 /* Fatal, abandon TX transaction */
837 return -2;
838 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200839
840 if (usbp->usber & USB_E_RXB) {
841 for (i = 0; i < RX_RING_SIZE; i++) {
842 if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) {
843 if ((rx_cbd[i] == ep_ref[0].prx) || ep) {
844 return -2;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200845 }
846 }
847 }
848 }
849
850 return 0;
851}
852
853/* mpc8xx_udc_ep_tx
854 *
855 * Transmit in a re-entrant fashion outbound USB packets.
856 * Implement retry/timeout mechanism described in USB specification
857 * Toggle DATA0/DATA1 pids as necessary
858 * Introduces non-standard tx_retry. The USB standard has no scope for slave
859 * devices to give up TX, however tx_retry stops us getting stuck in an endless
860 * TX loop.
861 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200862static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200863{
864 struct urb *urb = epi->tx_urb;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200865 volatile cbd_t *tx_cbdp = 0;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200866 unsigned int ep = 0, pkt_len = 0, x = 0, tx_retry = 0;
867 int ret = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200868
869 if (!epi || (epi->endpoint_address & 0x03) >= MAX_ENDPOINTS || !urb) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200870 return -1;
871 }
872
873 ep = epi->endpoint_address & 0x03;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200874 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200875
876 if (tx_cbdp->cbd_sc & TX_BD_R || usbp->usber & USB_E_TXB) {
877 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200878 usbp->usber |= USB_E_TXB;
879 };
880
Wolfgang Denk386eda02006-06-14 18:14:56 +0200881 while (tx_retry++ < 100) {
882 ret = mpc8xx_udc_tx_irq (ep);
883 if (ret == -1) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200884 /* ignore timeout here */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200885 } else if (ret == -2) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200886 /* Abandon TX */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200887 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200888 return -1;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200889 }
890
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200891 tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CONFIG_SYS_IMMR);
Wolfgang Denk386eda02006-06-14 18:14:56 +0200892 while (tx_cbdp->cbd_sc & TX_BD_R) {
893 };
894 tx_cbdp->cbd_sc = (tx_cbdp->cbd_sc & TX_BD_W);
895
896 pkt_len = urb->actual_length - epi->sent;
897
898 if (pkt_len > epi->tx_packetSize || pkt_len > EP_MAX_PKT) {
899 pkt_len = MIN (epi->tx_packetSize, EP_MAX_PKT);
900 }
901
902 for (x = 0; x < pkt_len; x++) {
903 *((unsigned char *) (tx_cbdp->cbd_bufaddr + x)) =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200904 urb->buffer[epi->sent + x];
905 }
906 tx_cbdp->cbd_datlen = pkt_len;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200907 tx_cbdp->cbd_sc |= (CBD_TX_BITMASK | ep_ref[ep].pid);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200908 __asm__ ("eieio");
909
Wolfgang Denk386eda02006-06-14 18:14:56 +0200910#ifdef __SIMULATE_ERROR__
911 if (++err_poison_test == 2) {
912 err_poison_test = 0;
913 tx_cbdp->cbd_sc &= ~TX_BD_TC;
914 }
915#endif
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200916
Wolfgang Denk386eda02006-06-14 18:14:56 +0200917 usbp->uscom = (USCOM_STR | ep);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200918
Wolfgang Denk386eda02006-06-14 18:14:56 +0200919 while (!(usbp->usber & USB_E_TXB)) {
920 ret = mpc8xx_udc_tx_irq (ep);
921 if (ret == -1) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200922 /* TX timeout */
923 break;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200924 } else if (ret == -2) {
925 if (usbp->usber & USB_E_TXB) {
926 usbp->usber |= USB_E_TXB;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200927 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200928 mpc8xx_udc_flush_tx_fifo (ep);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200929 return -1;
930 }
931 };
932
Wolfgang Denk386eda02006-06-14 18:14:56 +0200933 if (usbp->usber & USB_E_TXB) {
934 usbp->usber |= USB_E_TXB;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200935 }
936
937 /* ACK must be present <= 18bit times from TX */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200938 if (ret == -1) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200939 continue;
940 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200941
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200942 /* TX ACK : USB 2.0 8.7.2, Toggle PID, Advance TX */
943 epi->sent += pkt_len;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200944 epi->last = MIN (urb->actual_length - epi->sent, epi->tx_packetSize);
945 TOGGLE_TX_PID (ep_ref[ep].pid);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200946
Wolfgang Denk386eda02006-06-14 18:14:56 +0200947 if (epi->sent >= epi->tx_urb->actual_length) {
948
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200949 epi->tx_urb->actual_length = 0;
950 epi->sent = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200951
952 if (ep_ref[ep].sc & EP_SEND_ZLP) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200953 ep_ref[ep].sc &= ~EP_SEND_ZLP;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200954 } else {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200955 return 0;
956 }
957 }
958 }
Wolfgang Denk386eda02006-06-14 18:14:56 +0200959
960 ERR ("TX fail, endpoint 0x%x tx bytes 0x%x/0x%x\n", ep, epi->sent,
961 epi->tx_urb->actual_length);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200962
963 return -1;
964}
965
966/* mpc8xx_udc_dump_request
967 *
968 * Dump a control request to console
969 */
Wolfgang Denk386eda02006-06-14 18:14:56 +0200970static void mpc8xx_udc_dump_request (struct usb_device_request *request)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200971{
Wolfgang Denk386eda02006-06-14 18:14:56 +0200972 DBG ("bmRequestType:%02x bRequest:%02x wValue:%04x "
973 "wIndex:%04x wLength:%04x ?\n",
974 request->bmRequestType,
975 request->bRequest,
976 request->wValue, request->wIndex, request->wLength);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200977
978 return;
979}
980
Wolfgang Denk386eda02006-06-14 18:14:56 +0200981/* mpc8xx_udc_ep0_rx_setup
982 *
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200983 * Decode received ep0 SETUP packet. return non-zero on error
984 */
985static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp)
986{
987 unsigned int x = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +0200988 struct urb *purb = ep_ref[0].urb;
989 struct usb_endpoint_instance *epi =
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200990 &udc_device->bus->endpoint_array[0];
991
Wolfgang Denk386eda02006-06-14 18:14:56 +0200992 for (; x < rx_cbdp->cbd_datlen; x++) {
993 *(((unsigned char *) &ep_ref[0].urb->device_request) + x) =
994 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200995 }
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +0200996
Wolfgang Denk386eda02006-06-14 18:14:56 +0200997 mpc8xx_udc_clear_rxbd (rx_cbdp);
998
999 if (ep0_recv_setup (purb)) {
1000 mpc8xx_udc_dump_request (&purb->device_request);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001001 return -1;
1002 }
1003
Wolfgang Denk386eda02006-06-14 18:14:56 +02001004 if ((purb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001005 == USB_REQ_HOST2DEVICE) {
1006
Wolfgang Denk386eda02006-06-14 18:14:56 +02001007 switch (purb->device_request.bRequest) {
1008 case USB_REQ_SET_ADDRESS:
1009 /* Send the Status OUT ZLP */
1010 ep_ref[0].pid = TX_BD_PID_DATA1;
1011 purb->actual_length = 0;
1012 mpc8xx_udc_init_tx (epi, purb);
1013 mpc8xx_udc_ep_tx (epi);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001014
Wolfgang Denk386eda02006-06-14 18:14:56 +02001015 /* Move to the addressed state */
1016 usbp->usaddr = udc_device->address;
1017 mpc8xx_udc_state_transition_up (udc_device->device_state,
1018 STATE_ADDRESSED);
1019 return 0;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001020
Wolfgang Denk386eda02006-06-14 18:14:56 +02001021 case USB_REQ_SET_CONFIGURATION:
1022 if (!purb->device_request.wValue) {
1023 /* Respond at default address */
1024 usbp->usaddr = 0x00;
1025 mpc8xx_udc_state_transition_down (udc_device->device_state,
1026 STATE_ADDRESSED);
1027 } else {
1028 /* TODO: Support multiple configurations */
1029 mpc8xx_udc_state_transition_up (udc_device->device_state,
1030 STATE_CONFIGURED);
1031 for (x = 1; x < MAX_ENDPOINTS; x++) {
1032 if ((udc_device->bus->endpoint_array[x].endpoint_address & USB_ENDPOINT_DIR_MASK)
1033 == USB_DIR_IN) {
1034 ep_ref[x].pid = TX_BD_PID_DATA0;
1035 } else {
1036 ep_ref[x].pid = RX_BD_PID_DATA0;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001037 }
Wolfgang Denk386eda02006-06-14 18:14:56 +02001038 /* Set configuration must unstall endpoints */
1039 usbp->usep[x] &= ~STALL_BITMASK;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001040 }
Wolfgang Denk386eda02006-06-14 18:14:56 +02001041 }
1042 break;
1043 default:
1044 /* CDC/Vendor specific */
1045 break;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001046 }
1047
1048 /* Send ZLP as ACK in Status OUT phase */
1049 ep_ref[0].pid = TX_BD_PID_DATA1;
1050 purb->actual_length = 0;
Wolfgang Denk386eda02006-06-14 18:14:56 +02001051 mpc8xx_udc_init_tx (epi, purb);
1052 mpc8xx_udc_ep_tx (epi);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001053
Wolfgang Denk386eda02006-06-14 18:14:56 +02001054 } else {
1055
1056 if (purb->actual_length) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001057 ep_ref[0].pid = TX_BD_PID_DATA1;
Wolfgang Denk386eda02006-06-14 18:14:56 +02001058 mpc8xx_udc_init_tx (epi, purb);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001059
Wolfgang Denk386eda02006-06-14 18:14:56 +02001060 if (!(purb->actual_length % EP0_MAX_PACKET_SIZE)) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001061 ep_ref[0].sc |= EP_SEND_ZLP;
1062 }
1063
Wolfgang Denk386eda02006-06-14 18:14:56 +02001064 if (purb->device_request.wValue ==
1065 USB_DESCRIPTOR_TYPE_DEVICE) {
1066 if (le16_to_cpu (purb->device_request.wLength)
1067 > purb->actual_length) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001068 /* Send EP0_MAX_PACKET_SIZE bytes
1069 * unless correct size requested.
1070 */
Wolfgang Denk386eda02006-06-14 18:14:56 +02001071 if (purb->actual_length > epi->tx_packetSize) {
1072 purb->actual_length = epi->tx_packetSize;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001073 }
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001074 }
1075 }
Wolfgang Denk386eda02006-06-14 18:14:56 +02001076 mpc8xx_udc_ep_tx (epi);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001077
Wolfgang Denk386eda02006-06-14 18:14:56 +02001078 } else {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001079 /* Corrupt SETUP packet? */
Wolfgang Denk386eda02006-06-14 18:14:56 +02001080 ERR ("Zero length data or SETUP with DATA-IN phase ?\n");
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001081 return 1;
1082 }
1083 }
1084 return 0;
1085}
1086
1087/* mpc8xx_udc_init_tx
1088 *
1089 * Setup some basic parameters for a TX transaction
1090 */
Wolfgang Denk386eda02006-06-14 18:14:56 +02001091static void mpc8xx_udc_init_tx (struct usb_endpoint_instance *epi,
1092 struct urb *tx_urb)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001093{
1094 epi->sent = 0;
1095 epi->last = 0;
1096 epi->tx_urb = tx_urb;
1097}
1098
1099/* mpc8xx_udc_ep0_rx
1100 *
1101 * Receive ep0/control USB data. Parse and possibly send a response.
1102 */
Wolfgang Denk386eda02006-06-14 18:14:56 +02001103static void mpc8xx_udc_ep0_rx (volatile cbd_t * rx_cbdp)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001104{
Wolfgang Denk386eda02006-06-14 18:14:56 +02001105 if (rx_cbdp->cbd_sc & RX_BD_PID_SETUP) {
1106
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001107 /* Unconditionally accept SETUP packets */
Wolfgang Denk386eda02006-06-14 18:14:56 +02001108 if (mpc8xx_udc_ep0_rx_setup (rx_cbdp)) {
1109 mpc8xx_udc_stall (0);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001110 }
Wolfgang Denk386eda02006-06-14 18:14:56 +02001111
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001112 } else {
Wolfgang Denk386eda02006-06-14 18:14:56 +02001113
1114 mpc8xx_udc_clear_rxbd (rx_cbdp);
1115
1116 if ((rx_cbdp->cbd_datlen - 2)) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001117 /* SETUP with a DATA phase
Wolfgang Denk386eda02006-06-14 18:14:56 +02001118 * outside of SETUP packet.
1119 * Reply with STALL.
1120 */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001121 mpc8xx_udc_stall (0);
1122 }
1123 }
1124}
1125
1126/* mpc8xx_udc_epn_rx
1127 *
1128 * Receive some data from cbd into USB system urb data abstraction
Wolfgang Denk386eda02006-06-14 18:14:56 +02001129 * Upper layers should NAK if there is insufficient RX data space
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001130 */
1131static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp)
1132{
1133 struct usb_endpoint_instance *epi = 0;
1134 struct urb *urb = 0;
1135 unsigned int x = 0;
1136
Wolfgang Denk386eda02006-06-14 18:14:56 +02001137 if (epid >= MAX_ENDPOINTS || !rx_cbdp->cbd_datlen) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001138 return 0;
1139 }
Wolfgang Denk386eda02006-06-14 18:14:56 +02001140
1141 /* USB 2.0 PDF section 8.6.4
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001142 * Discard data with invalid PID it is a resend.
1143 */
Wolfgang Denk386eda02006-06-14 18:14:56 +02001144 if (ep_ref[epid].pid != (rx_cbdp->cbd_sc & 0xC0)) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001145 return 1;
1146 }
Wolfgang Denk386eda02006-06-14 18:14:56 +02001147 TOGGLE_RX_PID (ep_ref[epid].pid);
1148
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001149 epi = &udc_device->bus->endpoint_array[epid];
1150 urb = epi->rcv_urb;
1151
Wolfgang Denk386eda02006-06-14 18:14:56 +02001152 for (; x < (rx_cbdp->cbd_datlen - 2); x++) {
1153 *((unsigned char *) (urb->buffer + urb->actual_length + x)) =
1154 *((unsigned char *) (rx_cbdp->cbd_bufaddr + x));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001155 }
1156
Wolfgang Denk386eda02006-06-14 18:14:56 +02001157 if (x) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001158 usbd_rcv_complete (epi, x, 0);
Wolfgang Denk386eda02006-06-14 18:14:56 +02001159 if (ep_ref[epid].urb->status == RECV_ERROR) {
1160 DBG ("RX error unset NAK\n");
1161 udc_unset_nak (epid);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001162 }
Wolfgang Denk386eda02006-06-14 18:14:56 +02001163 }
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001164 return x;
1165}
1166
1167/* mpc8xx_udc_clock_init
1168 *
Wolfgang Denk386eda02006-06-14 18:14:56 +02001169 * Obtain a clock reference for Full Speed Signaling
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001170 */
Wolfgang Denk386eda02006-06-14 18:14:56 +02001171static void mpc8xx_udc_clock_init (volatile immap_t * immr,
1172 volatile cpm8xx_t * cp)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001173{
1174
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001175#if defined(CONFIG_SYS_USB_EXTC_CLK)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001176
1177 /* This has been tested with a 48MHz crystal on CLK6 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001178 switch (CONFIG_SYS_USB_EXTC_CLK) {
Wolfgang Denk386eda02006-06-14 18:14:56 +02001179 case 1:
1180 immr->im_ioport.iop_papar |= 0x0100;
1181 immr->im_ioport.iop_padir &= ~0x0100;
1182 cp->cp_sicr |= 0x24;
1183 break;
1184 case 2:
1185 immr->im_ioport.iop_papar |= 0x0200;
1186 immr->im_ioport.iop_padir &= ~0x0200;
1187 cp->cp_sicr |= 0x2D;
1188 break;
1189 case 3:
1190 immr->im_ioport.iop_papar |= 0x0400;
1191 immr->im_ioport.iop_padir &= ~0x0400;
1192 cp->cp_sicr |= 0x36;
1193 break;
1194 case 4:
1195 immr->im_ioport.iop_papar |= 0x0800;
1196 immr->im_ioport.iop_padir &= ~0x0800;
1197 cp->cp_sicr |= 0x3F;
1198 break;
1199 default:
1200 udc_state = STATE_ERROR;
1201 break;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001202 }
1203
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001204#elif defined(CONFIG_SYS_USB_BRGCLK)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001205
Wolfgang Denk386eda02006-06-14 18:14:56 +02001206 /* This has been tested with brgclk == 50MHz */
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001207 int divisor = 0;
1208
Wolfgang Denk386eda02006-06-14 18:14:56 +02001209 if (gd->cpu_clk < 48000000L) {
1210 ERR ("brgclk is too slow for full-speed USB!\n");
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001211 udc_state = STATE_ERROR;
1212 return;
1213 }
1214
Wolfgang Denk8ed44d92008-10-19 02:35:50 +02001215 /* Assume the brgclk is 'good enough', we want !(gd->cpu_clk%48MHz)
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001216 * but, can /probably/ live with close-ish alternative rates.
Wolfgang Denk386eda02006-06-14 18:14:56 +02001217 */
1218 divisor = (gd->cpu_clk / 48000000L) - 1;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001219 cp->cp_sicr &= ~0x0000003F;
Wolfgang Denk386eda02006-06-14 18:14:56 +02001220
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001221 switch (CONFIG_SYS_USB_BRGCLK) {
Wolfgang Denk386eda02006-06-14 18:14:56 +02001222 case 1:
1223 cp->cp_brgc1 |= (divisor | CPM_BRG_EN);
1224 cp->cp_sicr &= ~0x2F;
1225 break;
1226 case 2:
1227 cp->cp_brgc2 |= (divisor | CPM_BRG_EN);
1228 cp->cp_sicr |= 0x00000009;
1229 break;
1230 case 3:
1231 cp->cp_brgc3 |= (divisor | CPM_BRG_EN);
1232 cp->cp_sicr |= 0x00000012;
1233 break;
1234 case 4:
1235 cp->cp_brgc4 = (divisor | CPM_BRG_EN);
1236 cp->cp_sicr |= 0x0000001B;
1237 break;
1238 default:
1239 udc_state = STATE_ERROR;
1240 break;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001241 }
1242
1243#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001244#error "CONFIG_SYS_USB_EXTC_CLK or CONFIG_SYS_USB_BRGCLK must be defined"
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001245#endif
1246
1247}
1248
1249/* mpc8xx_udc_cbd_attach
1250 *
1251 * attach a cbd to and endpoint
1252 */
1253static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size)
1254{
Wolfgang Denk386eda02006-06-14 18:14:56 +02001255
1256 if (!tx_cbd[ep] || !rx_cbd[ep] || ep >= MAX_ENDPOINTS) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001257 udc_state = STATE_ERROR;
1258 return;
1259 }
1260
Wolfgang Denk386eda02006-06-14 18:14:56 +02001261 if (tx_size > USB_MAX_PKT || rx_size > USB_MAX_PKT ||
1262 (!tx_size && !rx_size)) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001263 udc_state = STATE_ERROR;
1264 return;
1265 }
1266
1267 /* Attach CBD to appropiate Parameter RAM Endpoint data structure */
Wolfgang Denk386eda02006-06-14 18:14:56 +02001268 if (rx_size) {
1269 endpoints[ep]->rbase = (u32) rx_cbd[rx_ct];
1270 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001271 rx_ct++;
1272
Wolfgang Denk386eda02006-06-14 18:14:56 +02001273 if (!ep) {
1274
1275 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001276 rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
1277 rx_ct++;
1278
Wolfgang Denk386eda02006-06-14 18:14:56 +02001279 } else {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001280 rx_ct += 2;
Wolfgang Denk386eda02006-06-14 18:14:56 +02001281 endpoints[ep]->rbptr = (u32) rx_cbd[rx_ct];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001282 rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
1283 rx_ct++;
1284 }
1285
1286 /* Where we expect to RX data on this endpoint */
Wolfgang Denk386eda02006-06-14 18:14:56 +02001287 ep_ref[ep].prx = rx_cbd[rx_ct - 1];
1288 } else {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001289
1290 ep_ref[ep].prx = 0;
1291 endpoints[ep]->rbase = 0;
1292 endpoints[ep]->rbptr = 0;
1293 }
1294
Wolfgang Denk386eda02006-06-14 18:14:56 +02001295 if (tx_size) {
1296 endpoints[ep]->tbase = (u32) tx_cbd[tx_ct];
1297 endpoints[ep]->tbptr = (u32) tx_cbd[tx_ct];
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001298 tx_ct++;
Wolfgang Denk386eda02006-06-14 18:14:56 +02001299 } else {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001300 endpoints[ep]->tbase = 0;
1301 endpoints[ep]->tbptr = 0;
1302 }
1303
1304 endpoints[ep]->tstate = 0;
1305 endpoints[ep]->tbcnt = 0;
1306 endpoints[ep]->mrblr = EP_MAX_PKT;
Wolfgang Denk386eda02006-06-14 18:14:56 +02001307 endpoints[ep]->rfcr = 0x18;
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001308 endpoints[ep]->tfcr = 0x18;
1309 ep_ref[ep].sc |= EP_ATTACHED;
1310
Wolfgang Denk386eda02006-06-14 18:14:56 +02001311 DBG ("ep %d rbase 0x%08x rbptr 0x%08x tbase 0x%08x tbptr 0x%08x prx = %p\n",
1312 ep, endpoints[ep]->rbase, endpoints[ep]->rbptr,
1313 endpoints[ep]->tbase, endpoints[ep]->tbptr,
1314 ep_ref[ep].prx);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001315
1316 return;
1317}
1318
1319/* mpc8xx_udc_cbd_init
1320 *
1321 * Allocate space for a cbd and allocate TX/RX data space
1322 */
1323static void mpc8xx_udc_cbd_init (void)
1324{
1325 int i = 0;
1326
Wolfgang Denk386eda02006-06-14 18:14:56 +02001327 for (; i < TX_RING_SIZE; i++) {
1328 tx_cbd[i] = (cbd_t *)
1329 mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001330 }
1331
Wolfgang Denk386eda02006-06-14 18:14:56 +02001332 for (i = 0; i < RX_RING_SIZE; i++) {
1333 rx_cbd[i] = (cbd_t *)
1334 mpc8xx_udc_alloc (sizeof (cbd_t), sizeof (int));
1335 }
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001336
Wolfgang Denk386eda02006-06-14 18:14:56 +02001337 for (i = 0; i < TX_RING_SIZE; i++) {
1338 tx_cbd[i]->cbd_bufaddr =
1339 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
1340
1341 tx_cbd[i]->cbd_sc = (TX_BD_I | TX_BD_W);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001342 tx_cbd[i]->cbd_datlen = 0x0000;
1343 }
1344
1345
Wolfgang Denk386eda02006-06-14 18:14:56 +02001346 for (i = 0; i < RX_RING_SIZE; i++) {
1347 rx_cbd[i]->cbd_bufaddr =
1348 mpc8xx_udc_alloc (EP_MAX_PKT, sizeof (int));
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001349 rx_cbd[i]->cbd_sc = (RX_BD_I | RX_BD_E);
1350 rx_cbd[i]->cbd_datlen = 0x0000;
1351
1352 }
1353
1354 return;
1355}
1356
1357/* mpc8xx_udc_endpoint_init
1358 *
1359 * Attach an endpoint to some dpram
1360 */
1361static void mpc8xx_udc_endpoint_init (void)
1362{
1363 int i = 0;
1364
Wolfgang Denk386eda02006-06-14 18:14:56 +02001365 for (; i < MAX_ENDPOINTS; i++) {
1366 endpoints[i] = (usb_epb_t *)
1367 mpc8xx_udc_alloc (sizeof (usb_epb_t), 32);
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001368 }
1369}
1370
1371/* mpc8xx_udc_alloc
1372 *
Wolfgang Denk386eda02006-06-14 18:14:56 +02001373 * Grab the address of some dpram
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001374 */
1375static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment)
1376{
1377 u32 retaddr = address_base;
Wolfgang Denk386eda02006-06-14 18:14:56 +02001378
1379 while (retaddr % alignment) {
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001380 retaddr++;
Wolfgang Denk386eda02006-06-14 18:14:56 +02001381 }
1382 address_base += data_size;
1383
Wolfgang Denk16c8d5e2006-06-14 17:45:53 +02001384 return retaddr;
1385}