usbdcore_mpc8xx.c 33 KB

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