mpc8xx_udc.c 33 KB

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