usbdcore_mpc8xx.c 33 KB

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