mpc8xx_udc.c 32 KB

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