usbdcore_mpc8xx.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. return;
  236. }
  237. /* udc_endpoint_write
  238. *
  239. * Write some data to an endpoint
  240. */
  241. int udc_endpoint_write(struct usb_endpoint_instance *epi)
  242. {
  243. int ep = 0;
  244. short epid = 1, unnak = 0, ret = 0;
  245. if(udc_state != STATE_READY){
  246. ERR("invalid udc_state != STATE_READY!\n");
  247. return -1;
  248. }
  249. if(!udc_device || !epi){
  250. return -1;
  251. }
  252. if(udc_device->device_state!=STATE_CONFIGURED){
  253. return -1;
  254. }
  255. ep = epi->endpoint_address & 0x03;
  256. if(ep >= MAX_ENDPOINTS){
  257. return -1;
  258. }
  259. /* Set NAK for all RX endpoints during TX */
  260. for(epid = 1; epid<MAX_ENDPOINTS; epid++){
  261. /* Don't set NAK on DATA IN/CONTROL endpoints */
  262. if(ep_ref[epid].sc & USB_DIR_IN){
  263. continue;
  264. }
  265. if(!(usbp->usep[epid]&( USEP_THS_NAK | USEP_RHS_NAK ))){
  266. unnak |= 1<<epid;
  267. }
  268. mpc8xx_udc_set_nak(epid);
  269. }
  270. mpc8xx_udc_init_tx(&udc_device->bus->endpoint_array[ep],epi->tx_urb);
  271. ret = mpc8xx_udc_ep_tx(&udc_device->bus->endpoint_array[ep]);
  272. /* Remove temporary NAK */
  273. for(epid = 1; epid<MAX_ENDPOINTS; epid++){
  274. if(unnak&(1<<epid)){
  275. udc_unset_nak(epid);
  276. }
  277. }
  278. return ret;
  279. }
  280. /* mpc8xx_udc_assign_urb
  281. *
  282. * Associate a given urb to an endpoint TX or RX transmit/receive buffers
  283. */
  284. static int mpc8xx_udc_assign_urb(int ep, char direction)
  285. {
  286. struct usb_endpoint_instance *epi = 0;
  287. if(ep >= MAX_ENDPOINTS){
  288. goto err;
  289. }
  290. epi = &udc_device->bus->endpoint_array[ep];
  291. if(!epi){
  292. goto err;
  293. }
  294. if(!ep_ref[ep].urb){
  295. ep_ref[ep].urb = usbd_alloc_urb(udc_device,
  296. udc_device->bus->endpoint_array);
  297. if(!ep_ref[ep].urb){
  298. goto err;
  299. }
  300. }else{
  301. ep_ref[ep].urb->actual_length = 0;
  302. }
  303. switch(direction){
  304. case USB_DIR_IN:
  305. epi->tx_urb = ep_ref[ep].urb;
  306. break;
  307. case USB_DIR_OUT:
  308. epi->rcv_urb = ep_ref[ep].urb;
  309. break;
  310. default:
  311. goto err;
  312. }
  313. return 0;
  314. err:
  315. udc_state = STATE_ERROR;
  316. return -1;
  317. }
  318. /* udc_setup_ep
  319. *
  320. * Associate U-Boot software endpoints to mpc8xx endpoint parameter ram
  321. * Isochronous endpoints aren't yet supported!
  322. */
  323. void udc_setup_ep(struct usb_device_instance *device, unsigned int ep,
  324. struct usb_endpoint_instance *epi)
  325. {
  326. uchar direction = 0;
  327. int ep_attrib = 0;
  328. if(epi && (ep < MAX_ENDPOINTS)){
  329. if(ep == 0){
  330. if (epi->rcv_attributes!=USB_ENDPOINT_XFER_CONTROL
  331. ||epi->tx_attributes!=
  332. USB_ENDPOINT_XFER_CONTROL){
  333. /* ep0 must be a control endpoint*/
  334. udc_state = STATE_ERROR;
  335. return;
  336. }
  337. if(!(ep_ref[ep].sc & EP_ATTACHED)){
  338. mpc8xx_udc_cbd_attach (ep, epi->tx_packetSize,
  339. epi->rcv_packetSize);
  340. }
  341. usbp->usep[ep] = 0x0000;
  342. return;
  343. }
  344. if ((epi->endpoint_address & USB_ENDPOINT_DIR_MASK)
  345. == USB_DIR_IN) {
  346. direction = 1;
  347. ep_attrib = epi->tx_attributes;
  348. epi->rcv_packetSize = 0;
  349. ep_ref[ep].sc |= USB_DIR_IN;
  350. } else {
  351. direction = 0;
  352. ep_attrib = epi->rcv_attributes;
  353. epi->tx_packetSize = 0;
  354. ep_ref[ep].sc &= ~USB_DIR_IN;
  355. }
  356. if(mpc8xx_udc_assign_urb(ep, epi->endpoint_address
  357. &USB_ENDPOINT_DIR_MASK)){
  358. return;
  359. }
  360. switch(ep_attrib){
  361. case USB_ENDPOINT_XFER_CONTROL:
  362. if(!(ep_ref[ep].sc & EP_ATTACHED)){
  363. mpc8xx_udc_cbd_attach (ep,
  364. epi->tx_packetSize,
  365. epi->rcv_packetSize);
  366. }
  367. usbp->usep[ep] = ep<<12;
  368. epi->rcv_urb = epi->tx_urb = ep_ref[ep].urb;
  369. break;
  370. case USB_ENDPOINT_XFER_BULK :
  371. case USB_ENDPOINT_XFER_INT:
  372. if(!(ep_ref[ep].sc & EP_ATTACHED)){
  373. if(direction){
  374. mpc8xx_udc_cbd_attach (ep,
  375. epi->tx_packetSize, 0);
  376. }else{
  377. mpc8xx_udc_cbd_attach (ep,
  378. 0, epi->rcv_packetSize);
  379. }
  380. }
  381. usbp->usep[ep]= (ep<<12)|((ep_attrib)<<8);
  382. break;
  383. case USB_ENDPOINT_XFER_ISOC:
  384. default:
  385. serial_printf("Error endpoint attrib %d>3\n",
  386. ep_attrib);
  387. udc_state = STATE_ERROR;
  388. break;
  389. }
  390. }
  391. }
  392. /* udc_connect
  393. *
  394. * Move state, switch on the USB
  395. */
  396. void udc_connect(void)
  397. {
  398. /* Enable pull-up resistor on D+
  399. * TODO: fit a pull-up resistor to drive SE0 for > 2.5us
  400. */
  401. if(udc_state!=STATE_ERROR){
  402. udc_state = STATE_READY;
  403. usbp->usmod|= USMOD_EN;
  404. }
  405. }
  406. /* udc_disconnect
  407. *
  408. * Disconnect is not used but, is included for completeness
  409. */
  410. void udc_disconnect(void)
  411. {
  412. /* Disable pull-up resistor on D-
  413. * TODO: fix a pullup resistor to control this
  414. */
  415. if(udc_state!=STATE_ERROR){
  416. udc_state = STATE_NOT_READY;
  417. }
  418. usbp->usmod&=~USMOD_EN;
  419. }
  420. /* udc_enable
  421. *
  422. * Grab an EP0 URB, register interest in a subset of USB events
  423. */
  424. void udc_enable(struct usb_device_instance *device)
  425. {
  426. if(udc_state == STATE_ERROR){
  427. return;
  428. }
  429. udc_device = device;
  430. if(!ep_ref[0].urb){
  431. ep_ref[0].urb = usbd_alloc_urb(device,
  432. device->bus->endpoint_array);
  433. }
  434. /* Register interest in all events except SOF, enable transceiver */
  435. usbp->usber= 0x03FF;
  436. usbp->usbmr= 0x02F7;
  437. return;
  438. }
  439. /* udc_disable
  440. *
  441. * disable the currently hooked device
  442. */
  443. void udc_disable(void)
  444. {
  445. int i = 0;
  446. if(udc_state == STATE_ERROR){
  447. DBG("Won't disable UDC. udc_state==STATE_ERROR !\n");
  448. return;
  449. }
  450. udc_device = 0;
  451. for(;i<MAX_ENDPOINTS; i++){
  452. if(ep_ref[i].urb){
  453. usbd_dealloc_urb(ep_ref[i].urb);
  454. ep_ref[i].urb = 0;
  455. }
  456. }
  457. usbp->usbmr= 0x00;
  458. usbp->usmod= ~USMOD_EN;
  459. udc_state = STATE_NOT_READY;
  460. }
  461. /* udc_startup_events
  462. *
  463. * Enable the specified device
  464. */
  465. void udc_startup_events(struct usb_device_instance *device)
  466. {
  467. udc_enable(device);
  468. if(udc_state == STATE_READY){
  469. usbd_device_event_irq (device, DEVICE_CREATE, 0);
  470. }
  471. }
  472. /* udc_set_nak
  473. *
  474. * Allow upper layers to signal lower layers should not accept more RX data
  475. *
  476. */
  477. void udc_set_nak(int epid)
  478. {
  479. if(epid){
  480. mpc8xx_udc_set_nak(epid);
  481. }
  482. }
  483. /* udc_unset_nak
  484. *
  485. * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
  486. * Switch off NAKing on this endpoint to accept more data output from host.
  487. *
  488. */
  489. void udc_unset_nak (int epid)
  490. {
  491. if(epid > MAX_ENDPOINTS){
  492. return;
  493. }
  494. if(usbp->usep[epid]&(USEP_THS_NAK | USEP_RHS_NAK)){
  495. usbp->usep[epid]&= ~(USEP_THS_NAK | USEP_RHS_NAK);
  496. __asm__ ("eieio");
  497. }
  498. }
  499. /******************************************************************************
  500. Static Linkage
  501. ******************************************************************************/
  502. /* udc_state_transition_up
  503. * udc_state_transition_down
  504. *
  505. * Helper functions to implement device state changes. The device states and
  506. * the events that transition between them are:
  507. *
  508. * STATE_ATTACHED
  509. * || /\
  510. * \/ ||
  511. * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
  512. * || /\
  513. * \/ ||
  514. * STATE_POWERED
  515. * || /\
  516. * \/ ||
  517. * DEVICE_RESET DEVICE_POWER_INTERRUPTION
  518. * || /\
  519. * \/ ||
  520. * STATE_DEFAULT
  521. * || /\
  522. * \/ ||
  523. * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
  524. * || /\
  525. * \/ ||
  526. * STATE_ADDRESSED
  527. * || /\
  528. * \/ ||
  529. * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
  530. * || /\
  531. * \/ ||
  532. * STATE_CONFIGURED
  533. *
  534. * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
  535. * to STATE_CONFIGURED) from the specified initial state to the specified final
  536. * state, passing through each intermediate state on the way. If the initial
  537. * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
  538. * no state transitions will take place.
  539. *
  540. * udc_state_transition_down transitions down (in the direction from
  541. * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
  542. * specified final state, passing through each intermediate state on the way.
  543. * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
  544. * state, then no state transitions will take place.
  545. *
  546. */
  547. static void mpc8xx_udc_state_transition_up (usb_device_state_t initial,
  548. usb_device_state_t final)
  549. {
  550. if (initial < final) {
  551. switch (initial) {
  552. case STATE_ATTACHED:
  553. usbd_device_event_irq (udc_device,
  554. DEVICE_HUB_CONFIGURED, 0);
  555. if (final == STATE_POWERED)
  556. break;
  557. case STATE_POWERED:
  558. usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
  559. if (final == STATE_DEFAULT)
  560. break;
  561. case STATE_DEFAULT:
  562. usbd_device_event_irq (udc_device,
  563. DEVICE_ADDRESS_ASSIGNED, 0);
  564. if (final == STATE_ADDRESSED)
  565. break;
  566. case STATE_ADDRESSED:
  567. usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
  568. 0);
  569. case STATE_CONFIGURED:
  570. break;
  571. default:
  572. break;
  573. }
  574. }
  575. }
  576. static void mpc8xx_udc_state_transition_down (usb_device_state_t initial,
  577. usb_device_state_t final)
  578. {
  579. if (initial > final) {
  580. switch (initial) {
  581. case STATE_CONFIGURED:
  582. usbd_device_event_irq (udc_device,
  583. DEVICE_DE_CONFIGURED, 0);
  584. if (final == STATE_ADDRESSED)
  585. break;
  586. case STATE_ADDRESSED:
  587. usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
  588. if (final == STATE_DEFAULT)
  589. break;
  590. case STATE_DEFAULT:
  591. usbd_device_event_irq (udc_device,
  592. DEVICE_POWER_INTERRUPTION, 0);
  593. if (final == STATE_POWERED)
  594. break;
  595. case STATE_POWERED:
  596. usbd_device_event_irq (udc_device, DEVICE_HUB_RESET,
  597. 0);
  598. case STATE_ATTACHED:
  599. break;
  600. default:
  601. break;
  602. }
  603. }
  604. }
  605. /* mpc8xx_udc_stall
  606. *
  607. * Force returning of STALL tokens on the given endpoint. Protocol or function
  608. * STALL conditions are permissable here
  609. */
  610. static void mpc8xx_udc_stall (unsigned int ep)
  611. {
  612. usbp->usep[ep] |= STALL_BITMASK;
  613. }
  614. /* mpc8xx_udc_set_nak
  615. *
  616. * Force returning of NAK responses for the given endpoint as a kind of very
  617. * simple flow control
  618. */
  619. static void mpc8xx_udc_set_nak (unsigned int ep)
  620. {
  621. usbp->usep[ep] |= NAK_BITMASK;
  622. __asm__ ("eieio");
  623. }
  624. /* mpc8xx_udc_handle_txerr
  625. *
  626. * Handle errors relevant to TX. Return a status code to allow calling
  627. * indicative of what if anything happened
  628. */
  629. static short mpc8xx_udc_handle_txerr()
  630. {
  631. short ep = 0, ret = 0;
  632. for(; ep<TX_RING_SIZE; ep++){
  633. if(usbp->usber&(0x10<<ep)){
  634. /* Timeout or underrun */
  635. if(tx_cbd[ep]->cbd_sc&0x06){
  636. ret = 1;
  637. mpc8xx_udc_flush_tx_fifo(ep);
  638. }else{
  639. if(usbp->usep[ep]&STALL_BITMASK){
  640. if(!ep){
  641. usbp->usep[ep]&=
  642. ~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*)
  659. (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. 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,
  828. 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(
  851. "bmRequestType:%02x bRequest:%02x wValue:%04x "
  852. "wIndex:%04x wLength:%04x ?\n",
  853. request->bmRequestType,
  854. request->bRequest,
  855. request->wValue,
  856. request->wIndex,
  857. request->wLength);
  858. return;
  859. }
  860. /* mpc8xx_udc_ep0_rx_setup
  861. *
  862. * Decode received ep0 SETUP packet. return non-zero on error
  863. */
  864. static int mpc8xx_udc_ep0_rx_setup (volatile cbd_t * rx_cbdp)
  865. {
  866. unsigned int x = 0;
  867. struct urb * purb = ep_ref[0].urb;
  868. struct usb_endpoint_instance *epi =
  869. &udc_device->bus->endpoint_array[0];
  870. for(; x<rx_cbdp->cbd_datlen; x++){
  871. *(((unsigned char*)&ep_ref[0].urb->device_request)+x) =
  872. *((unsigned char*)(rx_cbdp->cbd_bufaddr+x));
  873. }
  874. mpc8xx_udc_clear_rxbd(rx_cbdp);
  875. if (ep0_recv_setup(purb)) {
  876. mpc8xx_udc_dump_request(&purb->device_request);
  877. return -1;
  878. }
  879. if ((purb->device_request.bmRequestType&USB_REQ_DIRECTION_MASK)
  880. == USB_REQ_HOST2DEVICE) {
  881. switch (purb->device_request.bRequest){
  882. case USB_REQ_SET_ADDRESS:
  883. /* Send the Status OUT ZLP */
  884. ep_ref[0].pid = TX_BD_PID_DATA1;
  885. purb->actual_length = 0;
  886. mpc8xx_udc_init_tx(epi,purb);
  887. mpc8xx_udc_ep_tx(epi);
  888. /* Move to the addressed state */
  889. usbp->usaddr = udc_device->address;
  890. mpc8xx_udc_state_transition_up(udc_device->device_state,
  891. STATE_ADDRESSED);
  892. return 0;
  893. case USB_REQ_SET_CONFIGURATION:
  894. if(!purb->device_request.wValue){
  895. /* Respond at default address */
  896. usbp->usaddr = 0x00;
  897. mpc8xx_udc_state_transition_down(udc_device->device_state,
  898. STATE_ADDRESSED);
  899. } else {
  900. /* TODO: Support multiple configurations */
  901. mpc8xx_udc_state_transition_up(udc_device->device_state,STATE_CONFIGURED);
  902. for(x=1; x<MAX_ENDPOINTS; x++){
  903. if((udc_device->bus->endpoint_array[x].endpoint_address&USB_ENDPOINT_DIR_MASK)
  904. == USB_DIR_IN){
  905. ep_ref[x].pid = TX_BD_PID_DATA0;
  906. }else{
  907. ep_ref[x].pid = RX_BD_PID_DATA0;
  908. }
  909. /* Set configuration must unstall endpoints */
  910. usbp->usep[x]&=~STALL_BITMASK;
  911. }
  912. }
  913. break;
  914. default:
  915. /* CDC/Vendor specific */
  916. break;
  917. }
  918. /* Send ZLP as ACK in Status OUT phase */
  919. ep_ref[0].pid = TX_BD_PID_DATA1;
  920. purb->actual_length = 0;
  921. mpc8xx_udc_init_tx(epi,purb);
  922. mpc8xx_udc_ep_tx(epi);
  923. }else{
  924. if(purb->actual_length){
  925. ep_ref[0].pid = TX_BD_PID_DATA1;
  926. mpc8xx_udc_init_tx(epi,purb);
  927. if(!(purb->actual_length%EP0_MAX_PACKET_SIZE)){
  928. ep_ref[0].sc |= EP_SEND_ZLP;
  929. }
  930. if(purb->device_request.wValue==
  931. USB_DESCRIPTOR_TYPE_DEVICE){
  932. if(le16_to_cpu(purb->device_request.wLength)>
  933. purb->actual_length){
  934. /* Send EP0_MAX_PACKET_SIZE bytes
  935. * unless correct size requested.
  936. */
  937. if(purb->actual_length >
  938. epi->tx_packetSize){
  939. purb->actual_length =
  940. epi->tx_packetSize;
  941. }
  942. }
  943. }
  944. mpc8xx_udc_ep_tx(epi);
  945. }else{
  946. /* Corrupt SETUP packet? */
  947. ERR("Zero length data or SETUP with DATA-IN phase ?\n");
  948. return 1;
  949. }
  950. }
  951. return 0;
  952. }
  953. /* mpc8xx_udc_init_tx
  954. *
  955. * Setup some basic parameters for a TX transaction
  956. */
  957. static void mpc8xx_udc_init_tx(struct usb_endpoint_instance *epi,
  958. struct urb * tx_urb)
  959. {
  960. epi->sent = 0;
  961. epi->last = 0;
  962. epi->tx_urb = tx_urb;
  963. }
  964. /* mpc8xx_udc_ep0_rx
  965. *
  966. * Receive ep0/control USB data. Parse and possibly send a response.
  967. */
  968. static void mpc8xx_udc_ep0_rx(volatile cbd_t * rx_cbdp)
  969. {
  970. if(rx_cbdp->cbd_sc&RX_BD_PID_SETUP){
  971. /* Unconditionally accept SETUP packets */
  972. if(mpc8xx_udc_ep0_rx_setup(rx_cbdp)){
  973. mpc8xx_udc_stall (0);
  974. }
  975. } else {
  976. mpc8xx_udc_clear_rxbd(rx_cbdp);
  977. if((rx_cbdp->cbd_datlen-2)){
  978. /* SETUP with a DATA phase
  979. * outside of SETUP packet.
  980. * Reply with STALL.
  981. */
  982. mpc8xx_udc_stall (0);
  983. }
  984. }
  985. }
  986. /* mpc8xx_udc_epn_rx
  987. *
  988. * Receive some data from cbd into USB system urb data abstraction
  989. * Upper layers should NAK if there is insufficient RX data space
  990. */
  991. static int mpc8xx_udc_epn_rx (unsigned int epid, volatile cbd_t * rx_cbdp)
  992. {
  993. struct usb_endpoint_instance *epi = 0;
  994. struct urb *urb = 0;
  995. unsigned int x = 0;
  996. if(epid >= MAX_ENDPOINTS || !rx_cbdp->cbd_datlen){
  997. return 0;
  998. }
  999. /* USB 2.0 PDF section 8.6.4
  1000. * Discard data with invalid PID it is a resend.
  1001. */
  1002. if(ep_ref[epid].pid!=(rx_cbdp->cbd_sc&0xC0)){
  1003. return 1;
  1004. }
  1005. TOGGLE_RX_PID(ep_ref[epid].pid);
  1006. epi = &udc_device->bus->endpoint_array[epid];
  1007. urb = epi->rcv_urb;
  1008. for(; x<(rx_cbdp->cbd_datlen-2); x++){
  1009. *((unsigned char*)(urb->buffer + urb->actual_length +x)) =
  1010. *((unsigned char*)(rx_cbdp->cbd_bufaddr+x));
  1011. }
  1012. if(x){
  1013. usbd_rcv_complete (epi, x, 0);
  1014. if(ep_ref[epid].urb->status == RECV_ERROR){
  1015. DBG("RX error unset NAK\n");
  1016. udc_unset_nak(epid);
  1017. }
  1018. }
  1019. return x;
  1020. }
  1021. /* mpc8xx_udc_clock_init
  1022. *
  1023. * Obtain a clock reference for Full Speed Signaling
  1024. */
  1025. static void mpc8xx_udc_clock_init (volatile immap_t * immr,
  1026. volatile cpm8xx_t * cp)
  1027. {
  1028. #if defined(CFG_USB_EXTC_CLK)
  1029. /* This has been tested with a 48MHz crystal on CLK6 */
  1030. switch(CFG_USB_EXTC_CLK){
  1031. case 1:
  1032. immr->im_ioport.iop_papar|= 0x0100;
  1033. immr->im_ioport.iop_padir&= ~0x0100;
  1034. cp->cp_sicr|= 0x24;
  1035. break;
  1036. case 2:
  1037. immr->im_ioport.iop_papar|= 0x0200;
  1038. immr->im_ioport.iop_padir&= ~0x0200;
  1039. cp->cp_sicr|= 0x2D;
  1040. break;
  1041. case 3:
  1042. immr->im_ioport.iop_papar|= 0x0400;
  1043. immr->im_ioport.iop_padir&= ~0x0400;
  1044. cp->cp_sicr|= 0x36;
  1045. break;
  1046. case 4:
  1047. immr->im_ioport.iop_papar|= 0x0800;
  1048. immr->im_ioport.iop_padir&= ~0x0800;
  1049. cp->cp_sicr|= 0x3F;
  1050. break;
  1051. default:
  1052. udc_state = STATE_ERROR;
  1053. break;
  1054. }
  1055. #elif defined(CFG_USB_BRGCLK)
  1056. /* This has been tested with brgclk == 50MHz */
  1057. DECLARE_GLOBAL_DATA_PTR;
  1058. int divisor = 0;
  1059. if(gd->cpu_clk<48000000L){
  1060. ERR("brgclk is too slow for full-speed USB!\n");
  1061. udc_state = STATE_ERROR;
  1062. return;
  1063. }
  1064. /* Assume the brgclk is 'good enough', we want !(gd->cpu_clk%48Mhz)
  1065. * but, can /probably/ live with close-ish alternative rates.
  1066. */
  1067. divisor = (gd->cpu_clk/48000000L)-1;
  1068. cp->cp_sicr &= ~0x0000003F;
  1069. switch(CFG_USB_BRGCLK){
  1070. case 1:
  1071. cp->cp_brgc1 |= (divisor|CPM_BRG_EN);
  1072. cp->cp_sicr &= ~0x2F;
  1073. break;
  1074. case 2:
  1075. cp->cp_brgc2 |= (divisor|CPM_BRG_EN);
  1076. cp->cp_sicr |= 0x00000009;
  1077. break;
  1078. case 3:
  1079. cp->cp_brgc3 |= (divisor|CPM_BRG_EN);
  1080. cp->cp_sicr |= 0x00000012;
  1081. break;
  1082. case 4:
  1083. cp->cp_brgc4 = (divisor|CPM_BRG_EN);
  1084. cp->cp_sicr |= 0x0000001B;
  1085. break;
  1086. default:
  1087. udc_state = STATE_ERROR;
  1088. break;
  1089. }
  1090. #else
  1091. #error "CFG_USB_EXTC_CLK or CFG_USB_BRGCLK must be defined"
  1092. #endif
  1093. }
  1094. /* mpc8xx_udc_cbd_attach
  1095. *
  1096. * attach a cbd to and endpoint
  1097. */
  1098. static void mpc8xx_udc_cbd_attach (int ep, uchar tx_size, uchar rx_size)
  1099. {
  1100. if (!tx_cbd[ep] || !rx_cbd[ep] || ep >= MAX_ENDPOINTS){
  1101. udc_state = STATE_ERROR;
  1102. return;
  1103. }
  1104. if (tx_size>USB_MAX_PKT || rx_size>USB_MAX_PKT ||
  1105. (!tx_size && !rx_size)){
  1106. udc_state = STATE_ERROR;
  1107. return;
  1108. }
  1109. /* Attach CBD to appropiate Parameter RAM Endpoint data structure */
  1110. if(rx_size){
  1111. endpoints[ep]->rbase = (u32)rx_cbd[rx_ct];
  1112. endpoints[ep]->rbptr = (u32)rx_cbd[rx_ct];
  1113. rx_ct++;
  1114. if(!ep){
  1115. endpoints[ep]->rbptr = (u32)rx_cbd[rx_ct];
  1116. rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
  1117. rx_ct++;
  1118. }else{
  1119. rx_ct += 2;
  1120. endpoints[ep]->rbptr = (u32)rx_cbd[rx_ct];
  1121. rx_cbd[rx_ct]->cbd_sc |= RX_BD_W;
  1122. rx_ct++;
  1123. }
  1124. /* Where we expect to RX data on this endpoint */
  1125. ep_ref[ep].prx = rx_cbd[rx_ct-1];
  1126. }else{
  1127. ep_ref[ep].prx = 0;
  1128. endpoints[ep]->rbase = 0;
  1129. endpoints[ep]->rbptr = 0;
  1130. }
  1131. if(tx_size){
  1132. endpoints[ep]->tbase = (u32)tx_cbd[tx_ct];
  1133. endpoints[ep]->tbptr = (u32)tx_cbd[tx_ct];
  1134. tx_ct++;
  1135. }else{
  1136. endpoints[ep]->tbase = 0;
  1137. endpoints[ep]->tbptr = 0;
  1138. }
  1139. endpoints[ep]->tstate = 0;
  1140. endpoints[ep]->tbcnt = 0;
  1141. endpoints[ep]->mrblr = EP_MAX_PKT;
  1142. endpoints[ep]->rfcr = 0x18;
  1143. endpoints[ep]->tfcr = 0x18;
  1144. ep_ref[ep].sc |= EP_ATTACHED;
  1145. DBG("ep %d rbase 0x%08x rbptr 0x%08x tbase 0x%08x tbptr 0x%08x prx = %p\n",
  1146. ep, endpoints[ep]->rbase, endpoints[ep]->rbptr, endpoints[ep]->tbase,
  1147. endpoints[ep]->tbptr, ep_ref[ep].prx);
  1148. return;
  1149. }
  1150. /* mpc8xx_udc_cbd_init
  1151. *
  1152. * Allocate space for a cbd and allocate TX/RX data space
  1153. */
  1154. static void mpc8xx_udc_cbd_init (void)
  1155. {
  1156. int i = 0;
  1157. for(; i<TX_RING_SIZE; i++){
  1158. tx_cbd[i]= (cbd_t*)
  1159. mpc8xx_udc_alloc(sizeof(cbd_t), sizeof(int));
  1160. }
  1161. for(i=0; i<RX_RING_SIZE; i++){
  1162. rx_cbd[i]= (cbd_t*)
  1163. mpc8xx_udc_alloc(sizeof(cbd_t),sizeof(int));
  1164. }
  1165. for(i=0; i< TX_RING_SIZE; i++){
  1166. tx_cbd[i]->cbd_bufaddr =
  1167. mpc8xx_udc_alloc(EP_MAX_PKT, sizeof(int));
  1168. tx_cbd[i]->cbd_sc = (TX_BD_I | TX_BD_W);
  1169. tx_cbd[i]->cbd_datlen = 0x0000;
  1170. }
  1171. for(i=0; i< RX_RING_SIZE; i++){
  1172. rx_cbd[i]->cbd_bufaddr =
  1173. mpc8xx_udc_alloc(EP_MAX_PKT, sizeof(int));
  1174. rx_cbd[i]->cbd_sc = (RX_BD_I | RX_BD_E);
  1175. rx_cbd[i]->cbd_datlen = 0x0000;
  1176. }
  1177. return;
  1178. }
  1179. /* mpc8xx_udc_endpoint_init
  1180. *
  1181. * Attach an endpoint to some dpram
  1182. */
  1183. static void mpc8xx_udc_endpoint_init (void)
  1184. {
  1185. int i = 0;
  1186. for(; i<MAX_ENDPOINTS; i++){
  1187. endpoints[i]= (usb_epb_t*)
  1188. mpc8xx_udc_alloc(sizeof(usb_epb_t) , 32);
  1189. }
  1190. }
  1191. /* mpc8xx_udc_alloc
  1192. *
  1193. * Grab the address of some dpram
  1194. */
  1195. static u32 mpc8xx_udc_alloc (u32 data_size, u32 alignment)
  1196. {
  1197. u32 retaddr = address_base;
  1198. while(retaddr%alignment){
  1199. retaddr++;
  1200. }
  1201. address_base+= data_size;
  1202. return retaddr;
  1203. }
  1204. #endif /* CONFIG_MPC885_FAMILY && CONFIG_USB_DEVICE) */