usbdcore_mpc8xx.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /*
  2. * Copyright (C) 2006 by Bryan O'Donoghue, CodeHermit
  3. * bodonoghue@CodeHermit.ie
  4. *
  5. * References
  6. * DasUBoot/drivers/usbdcore_omap1510.c, for design and implementation ideas.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the
  20. * Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. /*
  25. * Notes :
  26. * 1. #define __SIMULATE_ERROR__ to inject a CRC error into every 2nd TX
  27. * packet to force the USB re-transmit protocol.
  28. *
  29. * 2. #define __DEBUG_UDC__ to switch on debug tracing to serial console
  30. * be careful that tracing doesn't create Hiesen-bugs with respect to
  31. * response timeouts to control requests.
  32. *
  33. * 3. This driver should be able to support any higher level driver that
  34. * that wants to do either of the two standard UDC implementations
  35. * Control-Bulk-Interrupt or Bulk-IN/Bulk-Out standards. Hence
  36. * gserial and cdc_acm should work with this code.
  37. *
  38. * 4. NAK events never actually get raised at all, the documentation
  39. * is just wrong !
  40. *
  41. * 5. For some reason, cbd_datlen is *always* +2 the value it should be.
  42. * this means that having an RX cbd of 16 bytes is not possible, since
  43. * the same size is reported for 14 bytes received as 16 bytes received
  44. * until we can find out why this happens, RX cbds must be limited to 8
  45. * bytes. TODO: check errata for this behaviour.
  46. *
  47. * 6. Right now this code doesn't support properly powering up with the USB
  48. * cable attached to the USB host my development board the Adder87x doesn't
  49. * have a pull-up fitted to allow this, so it is necessary to power the
  50. * board and *then* attached the USB cable to the host. However somebody
  51. * with a different design in their board may be able to keep the cable
  52. * constantly connected and simply enable/disable a pull-up re
  53. * figure 31.1 in MPC885RM.pdf instead of having to power up the board and
  54. * then attach the cable !
  55. *
  56. */
  57. #include <common.h>
  58. #include <config.h>
  59. #if defined(CONFIG_MPC885_FAMILY) && defined(CONFIG_USB_DEVICE)
  60. #include <commproc.h>
  61. #include "usbdcore.h"
  62. #include "usbdcore_mpc8xx.h"
  63. #include "usbdcore_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 *) CFG_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 + CFG_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 + CFG_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 + CFG_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 + CFG_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(CFG_USB_EXTC_CLK)
  1025. /* This has been tested with a 48MHz crystal on CLK6 */
  1026. switch (CFG_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(CFG_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 (CFG_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 "CFG_USB_EXTC_CLK or CFG_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. }
  1200. #endif /* CONFIG_MPC885_FAMILY && CONFIG_USB_DEVICE) */