musb_hcd.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Mentor USB OTG Core host controller driver.
  4. *
  5. * Copyright (c) 2008 Texas Instruments
  6. *
  7. * Author: Thomas Abraham t-abraham@ti.com, Texas Instruments
  8. */
  9. #include <common.h>
  10. #include <usb.h>
  11. #include "musb_hcd.h"
  12. /* MSC control transfers */
  13. #define USB_MSC_BBB_RESET 0xFF
  14. #define USB_MSC_BBB_GET_MAX_LUN 0xFE
  15. /* Endpoint configuration information */
  16. static const struct musb_epinfo epinfo[3] = {
  17. {MUSB_BULK_EP, 1, 512}, /* EP1 - Bluk Out - 512 Bytes */
  18. {MUSB_BULK_EP, 0, 512}, /* EP1 - Bluk In - 512 Bytes */
  19. {MUSB_INTR_EP, 0, 64} /* EP2 - Interrupt IN - 64 Bytes */
  20. };
  21. /* --- Virtual Root Hub ---------------------------------------------------- */
  22. #ifdef MUSB_NO_MULTIPOINT
  23. static int rh_devnum;
  24. static u32 port_status;
  25. #include <usbroothubdes.h>
  26. #endif
  27. /*
  28. * This function writes the data toggle value.
  29. */
  30. static void write_toggle(struct usb_device *dev, u8 ep, u8 dir_out)
  31. {
  32. u16 toggle = usb_gettoggle(dev, ep, dir_out);
  33. u16 csr;
  34. if (dir_out) {
  35. csr = readw(&musbr->txcsr);
  36. if (!toggle) {
  37. #ifdef CONFIG_ARK_MUSB
  38. csr |= MUSB_TXCSR_CLRDATATOG | MUSB_TXCSR_MODE;
  39. #else
  40. if (csr & MUSB_TXCSR_MODE)
  41. csr = MUSB_TXCSR_CLRDATATOG;
  42. else
  43. csr = 0;
  44. #endif /* CONFIG_ARK_MUSB */
  45. writew(csr, &musbr->txcsr);
  46. } else {
  47. csr |= MUSB_TXCSR_H_WR_DATATOGGLE;
  48. writew(csr, &musbr->txcsr);
  49. csr |= (toggle << MUSB_TXCSR_H_DATATOGGLE_SHIFT);
  50. writew(csr, &musbr->txcsr);
  51. }
  52. } else {
  53. if (!toggle) {
  54. csr = readw(&musbr->txcsr);
  55. #ifdef CONFIG_ARK_MUSB
  56. if (csr & MUSB_TXCSR_MODE)
  57. csr |= MUSB_RXCSR_CLRDATATOG;
  58. else
  59. csr |= 0;
  60. #else
  61. if (csr & MUSB_TXCSR_MODE)
  62. csr = MUSB_RXCSR_CLRDATATOG;
  63. else
  64. csr = 0;
  65. #endif /* CONFIG_ARK_MUSB */
  66. writew(csr, &musbr->rxcsr);
  67. } else {
  68. csr = readw(&musbr->rxcsr);
  69. csr |= MUSB_RXCSR_H_WR_DATATOGGLE;
  70. writew(csr, &musbr->rxcsr);
  71. csr |= (toggle << MUSB_S_RXCSR_H_DATATOGGLE);
  72. writew(csr, &musbr->rxcsr);
  73. }
  74. }
  75. }
  76. /*
  77. * This function checks if RxStall has occurred on the endpoint. If a RxStall
  78. * has occurred, the RxStall is cleared and 1 is returned. If RxStall has
  79. * not occurred, 0 is returned.
  80. */
  81. static u8 check_stall(u8 ep, u8 dir_out)
  82. {
  83. u16 csr;
  84. /* For endpoint 0 */
  85. if (!ep) {
  86. csr = readw(&musbr->txcsr);
  87. if (csr & MUSB_CSR0_H_RXSTALL) {
  88. csr &= ~MUSB_CSR0_H_RXSTALL;
  89. writew(csr, &musbr->txcsr);
  90. return 1;
  91. }
  92. } else { /* For non-ep0 */
  93. if (dir_out) { /* is it tx ep */
  94. csr = readw(&musbr->txcsr);
  95. if (csr & MUSB_TXCSR_H_RXSTALL) {
  96. csr &= ~MUSB_TXCSR_H_RXSTALL;
  97. writew(csr, &musbr->txcsr);
  98. return 1;
  99. }
  100. } else { /* is it rx ep */
  101. csr = readw(&musbr->rxcsr);
  102. if (csr & MUSB_RXCSR_H_RXSTALL) {
  103. csr &= ~MUSB_RXCSR_H_RXSTALL;
  104. writew(csr, &musbr->rxcsr);
  105. return 1;
  106. }
  107. }
  108. }
  109. return 0;
  110. }
  111. /*
  112. * waits until ep0 is ready. Returns 0 if ep is ready, -1 for timeout
  113. * error and -2 for stall.
  114. */
  115. static int wait_until_ep0_ready(struct usb_device *dev, u32 bit_mask)
  116. {
  117. u16 csr;
  118. int result = 1;
  119. int timeout = CONFIG_USB_MUSB_TIMEOUT;
  120. while (result > 0) {
  121. csr = readw(&musbr->txcsr);
  122. if (csr & MUSB_CSR0_H_ERROR) {
  123. csr &= ~MUSB_CSR0_H_ERROR;
  124. writew(csr, &musbr->txcsr);
  125. dev->status = USB_ST_CRC_ERR;
  126. result = -1;
  127. break;
  128. }
  129. switch (bit_mask) {
  130. case MUSB_CSR0_TXPKTRDY:
  131. if (!(csr & MUSB_CSR0_TXPKTRDY)) {
  132. if (check_stall(MUSB_CONTROL_EP, 0)) {
  133. dev->status = USB_ST_STALLED;
  134. result = -2;
  135. } else
  136. result = 0;
  137. }
  138. break;
  139. case MUSB_CSR0_RXPKTRDY:
  140. if (check_stall(MUSB_CONTROL_EP, 0)) {
  141. dev->status = USB_ST_STALLED;
  142. result = -2;
  143. } else
  144. if (csr & MUSB_CSR0_RXPKTRDY)
  145. result = 0;
  146. break;
  147. case MUSB_CSR0_H_REQPKT:
  148. if (!(csr & MUSB_CSR0_H_REQPKT)) {
  149. if (check_stall(MUSB_CONTROL_EP, 0)) {
  150. dev->status = USB_ST_STALLED;
  151. result = -2;
  152. } else
  153. result = 0;
  154. }
  155. break;
  156. }
  157. #ifdef CONFIG_ARK_MUSB
  158. if(!(readb(&musbr->devctl) & (1<<2)))
  159. {
  160. result = -1;
  161. break;
  162. }
  163. #endif
  164. /* Check the timeout */
  165. if (--timeout)
  166. udelay(1);
  167. else {
  168. dev->status = USB_ST_CRC_ERR;
  169. result = -1;
  170. break;
  171. }
  172. }
  173. return result;
  174. }
  175. /*
  176. * waits until tx ep is ready. Returns 1 when ep is ready and 0 on error.
  177. */
  178. static int wait_until_txep_ready(struct usb_device *dev, u8 ep)
  179. {
  180. u16 csr;
  181. int timeout = CONFIG_USB_MUSB_TIMEOUT;
  182. do {
  183. if (check_stall(ep, 1)) {
  184. dev->status = USB_ST_STALLED;
  185. return 0;
  186. }
  187. csr = readw(&musbr->txcsr);
  188. #ifdef CONFIG_ARK_MUSB
  189. if (csr & MUSB_TXCSR_H_ERROR) {
  190. csr &= ~MUSB_TXCSR_H_ERROR;
  191. writew(csr, &musbr->txcsr);
  192. dev->status = USB_ST_CRC_ERR;
  193. return 0;
  194. }
  195. if(!(readb(&musbr->devctl) & (1<<2)))
  196. {
  197. dev->status = USB_ST_NOT_PROC;
  198. return 0;
  199. }
  200. /* Check the timeout */
  201. if (--timeout)
  202. udelay(1);
  203. else
  204. {
  205. printf("=== wait_until_txep_ready timeout ===\n");
  206. dev->status = USB_ST_CRC_ERR;
  207. return 0;
  208. }
  209. } while ((csr & MUSB_TXCSR_TXPKTRDY) || (csr & MUSB_TXCSR_FIFONOTEMPTY));
  210. #else
  211. if (csr & MUSB_TXCSR_H_ERROR) {
  212. dev->status = USB_ST_CRC_ERR;
  213. return 0;
  214. }
  215. /* Check the timeout */
  216. if (--timeout)
  217. udelay(1);
  218. else {
  219. dev->status = USB_ST_CRC_ERR;
  220. return 0;
  221. }
  222. } while (csr & MUSB_TXCSR_TXPKTRDY);
  223. #endif
  224. return 1;
  225. }
  226. /*
  227. * waits until rx ep is ready. Returns 1 when ep is ready and 0 on error.
  228. */
  229. static int wait_until_rxep_ready(struct usb_device *dev, u8 ep)
  230. {
  231. u16 csr;
  232. int timeout = CONFIG_USB_MUSB_TIMEOUT;
  233. do {
  234. if (check_stall(ep, 0)) {
  235. dev->status = USB_ST_STALLED;
  236. return 0;
  237. }
  238. csr = readw(&musbr->rxcsr);
  239. if (csr & MUSB_RXCSR_H_ERROR) {
  240. csr &= ~MUSB_RXCSR_H_ERROR;
  241. writew(csr, &musbr->rxcsr);
  242. dev->status = USB_ST_CRC_ERR;
  243. return 0;
  244. }
  245. //Check if usb is plug out
  246. if(!(readb(&musbr->devctl) & (1<<2)))
  247. {
  248. dev->status = USB_ST_NOT_PROC;
  249. return 0;
  250. }
  251. /* Check the timeout */
  252. if (--timeout)
  253. {
  254. //udelay(1);
  255. udelay(50);
  256. }
  257. else
  258. {
  259. printf("=== wait_until_rxep_ready timeout ===\n");
  260. dev->status = USB_ST_CRC_ERR;
  261. return 0;
  262. }
  263. } while (!(csr & MUSB_RXCSR_RXPKTRDY));
  264. return 1;
  265. }
  266. /*
  267. * This function performs the setup phase of the control transfer
  268. */
  269. static int ctrlreq_setup_phase(struct usb_device *dev, struct devrequest *setup)
  270. {
  271. int result;
  272. u16 csr;
  273. /* write the control request to ep0 fifo */
  274. write_fifo(MUSB_CONTROL_EP, sizeof(struct devrequest), (void *)setup);
  275. /* enable transfer of setup packet */
  276. csr = readw(&musbr->txcsr);
  277. udelay(100000);
  278. csr |= (MUSB_CSR0_TXPKTRDY|MUSB_CSR0_H_SETUPPKT);
  279. writew(csr, &musbr->txcsr);
  280. /* wait until the setup packet is transmitted */
  281. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  282. dev->act_len = 0;
  283. return result;
  284. }
  285. /*
  286. * This function handles the control transfer in data phase
  287. */
  288. static int ctrlreq_in_data_phase(struct usb_device *dev, u32 len, void *buffer)
  289. {
  290. u16 csr;
  291. u32 rxlen = 0;
  292. u32 nextlen = 0;
  293. u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
  294. u8 *rxbuff = (u8 *)buffer;
  295. u8 rxedlength;
  296. int result;
  297. while (rxlen < len) {
  298. /* Determine the next read length */
  299. nextlen = ((len-rxlen) > maxpktsize) ? maxpktsize : (len-rxlen);
  300. /* Set the ReqPkt bit */
  301. csr = readw(&musbr->txcsr);
  302. writew(csr | MUSB_CSR0_H_REQPKT, &musbr->txcsr);
  303. result = wait_until_ep0_ready(dev, MUSB_CSR0_RXPKTRDY);
  304. if (result < 0)
  305. return result;
  306. /* Actual number of bytes received by usb */
  307. rxedlength = readb(&musbr->rxcount);
  308. /* Read the data from the RxFIFO */
  309. read_fifo(MUSB_CONTROL_EP, rxedlength, &rxbuff[rxlen]);
  310. /* Clear the RxPktRdy Bit */
  311. csr = readw(&musbr->txcsr);
  312. csr &= ~MUSB_CSR0_RXPKTRDY;
  313. writew(csr, &musbr->txcsr);
  314. /* short packet? */
  315. if (rxedlength != nextlen) {
  316. dev->act_len += rxedlength;
  317. break;
  318. }
  319. rxlen += nextlen;
  320. dev->act_len = rxlen;
  321. }
  322. return 0;
  323. }
  324. /*
  325. * This function handles the control transfer out data phase
  326. */
  327. static int ctrlreq_out_data_phase(struct usb_device *dev, u32 len, void *buffer)
  328. {
  329. u16 csr;
  330. u32 txlen = 0;
  331. u32 nextlen = 0;
  332. u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
  333. u8 *txbuff = (u8 *)buffer;
  334. int result = 0;
  335. while (txlen < len) {
  336. /* Determine the next write length */
  337. nextlen = ((len-txlen) > maxpktsize) ? maxpktsize : (len-txlen);
  338. /* Load the data to send in FIFO */
  339. write_fifo(MUSB_CONTROL_EP, txlen, &txbuff[txlen]);
  340. /* Set TXPKTRDY bit */
  341. csr = readw(&musbr->txcsr);
  342. csr |= MUSB_CSR0_TXPKTRDY;
  343. #if !defined(CONFIG_SOC_DM365)
  344. csr |= MUSB_CSR0_H_DIS_PING;
  345. #endif
  346. writew(csr, &musbr->txcsr);
  347. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  348. if (result < 0)
  349. break;
  350. txlen += nextlen;
  351. dev->act_len = txlen;
  352. }
  353. return result;
  354. }
  355. /*
  356. * This function handles the control transfer out status phase
  357. */
  358. static int ctrlreq_out_status_phase(struct usb_device *dev)
  359. {
  360. u16 csr;
  361. int result;
  362. /* Set the StatusPkt bit */
  363. csr = readw(&musbr->txcsr);
  364. csr |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_H_STATUSPKT);
  365. #if !defined(CONFIG_SOC_DM365)
  366. csr |= MUSB_CSR0_H_DIS_PING;
  367. #endif
  368. writew(csr, &musbr->txcsr);
  369. /* Wait until TXPKTRDY bit is cleared */
  370. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  371. return result;
  372. }
  373. /*
  374. * This function handles the control transfer in status phase
  375. */
  376. static int ctrlreq_in_status_phase(struct usb_device *dev)
  377. {
  378. u16 csr;
  379. int result;
  380. /* Set the StatusPkt bit and ReqPkt bit */
  381. csr = MUSB_CSR0_H_REQPKT | MUSB_CSR0_H_STATUSPKT;
  382. #if !defined(CONFIG_SOC_DM365)
  383. csr |= MUSB_CSR0_H_DIS_PING;
  384. #endif
  385. writew(csr, &musbr->txcsr);
  386. result = wait_until_ep0_ready(dev, MUSB_CSR0_H_REQPKT);
  387. /* clear StatusPkt bit and RxPktRdy bit */
  388. csr = readw(&musbr->txcsr);
  389. csr &= ~(MUSB_CSR0_RXPKTRDY | MUSB_CSR0_H_STATUSPKT);
  390. writew(csr, &musbr->txcsr);
  391. return result;
  392. }
  393. /*
  394. * determines the speed of the device (High/Full/Slow)
  395. */
  396. static u8 get_dev_speed(struct usb_device *dev)
  397. {
  398. return (dev->speed == USB_SPEED_HIGH) ? MUSB_TYPE_SPEED_HIGH :
  399. ((dev->speed == USB_SPEED_LOW) ? MUSB_TYPE_SPEED_LOW :
  400. MUSB_TYPE_SPEED_FULL);
  401. }
  402. /*
  403. * configure the hub address and the port address.
  404. */
  405. static void config_hub_port(struct usb_device *dev, u8 ep)
  406. {
  407. u8 chid;
  408. u8 hub;
  409. /* Find out the nearest parent which is high speed */
  410. while (dev->parent->parent != NULL)
  411. if (get_dev_speed(dev->parent) != MUSB_TYPE_SPEED_HIGH)
  412. dev = dev->parent;
  413. else
  414. break;
  415. /* determine the port address at that hub */
  416. hub = dev->parent->devnum;
  417. for (chid = 0; chid < USB_MAXCHILDREN; chid++)
  418. if (dev->parent->children[chid] == dev)
  419. break;
  420. #ifndef MUSB_NO_MULTIPOINT
  421. /* configure the hub address and the port address */
  422. writeb(hub, &musbr->tar[ep].txhubaddr);
  423. writeb((chid + 1), &musbr->tar[ep].txhubport);
  424. writeb(hub, &musbr->tar[ep].rxhubaddr);
  425. writeb((chid + 1), &musbr->tar[ep].rxhubport);
  426. #endif
  427. }
  428. #ifdef MUSB_NO_MULTIPOINT
  429. static void musb_port_reset(int do_reset)
  430. {
  431. u8 power = readb(&musbr->power);
  432. if (do_reset) {
  433. power &= 0xf0;
  434. writeb(power | MUSB_POWER_RESET, &musbr->power);
  435. port_status |= USB_PORT_STAT_RESET;
  436. port_status &= ~USB_PORT_STAT_ENABLE;
  437. udelay(500000);
  438. } else {
  439. writeb(power & ~MUSB_POWER_RESET, &musbr->power);
  440. power = readb(&musbr->power);
  441. if (power & MUSB_POWER_HSMODE)
  442. port_status |= USB_PORT_STAT_HIGH_SPEED;
  443. port_status &= ~(USB_PORT_STAT_RESET | (USB_PORT_STAT_C_CONNECTION << 16));
  444. port_status |= USB_PORT_STAT_ENABLE
  445. | (USB_PORT_STAT_C_RESET << 16)
  446. | (USB_PORT_STAT_C_ENABLE << 16);
  447. }
  448. }
  449. /*
  450. * root hub control
  451. */
  452. static int musb_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
  453. void *buffer, int transfer_len,
  454. struct devrequest *cmd)
  455. {
  456. int leni = transfer_len;
  457. int len = 0;
  458. int stat = 0;
  459. u32 datab[4];
  460. const u8 *data_buf = (u8 *) datab;
  461. u16 bmRType_bReq;
  462. u16 wValue;
  463. u16 wIndex;
  464. u16 wLength;
  465. u16 int_usb;
  466. if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
  467. debug("Root-Hub submit IRQ: NOT implemented\n");
  468. return 0;
  469. }
  470. bmRType_bReq = cmd->requesttype | (cmd->request << 8);
  471. wValue = swap_16(cmd->value);
  472. wIndex = swap_16(cmd->index);
  473. wLength = swap_16(cmd->length);
  474. debug("--- HUB ----------------------------------------\n");
  475. debug("submit rh urb, req=%x val=%#x index=%#x len=%d\n",
  476. bmRType_bReq, wValue, wIndex, wLength);
  477. debug("------------------------------------------------\n");
  478. switch (bmRType_bReq) {
  479. case RH_GET_STATUS:
  480. debug("RH_GET_STATUS\n");
  481. *(__u16 *) data_buf = swap_16(1);
  482. len = 2;
  483. break;
  484. case RH_GET_STATUS | RH_INTERFACE:
  485. debug("RH_GET_STATUS | RH_INTERFACE\n");
  486. *(__u16 *) data_buf = swap_16(0);
  487. len = 2;
  488. break;
  489. case RH_GET_STATUS | RH_ENDPOINT:
  490. debug("RH_GET_STATUS | RH_ENDPOINT\n");
  491. *(__u16 *) data_buf = swap_16(0);
  492. len = 2;
  493. break;
  494. case RH_GET_STATUS | RH_CLASS:
  495. debug("RH_GET_STATUS | RH_CLASS\n");
  496. *(__u32 *) data_buf = swap_32(0);
  497. len = 4;
  498. break;
  499. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  500. debug("RH_GET_STATUS | RH_OTHER | RH_CLASS\n");
  501. int_usb = readw(&musbr->intrusb);
  502. if (int_usb & MUSB_INTR_CONNECT) {
  503. port_status |= USB_PORT_STAT_CONNECTION
  504. | (USB_PORT_STAT_C_CONNECTION << 16);
  505. port_status |= USB_PORT_STAT_HIGH_SPEED
  506. | USB_PORT_STAT_ENABLE;
  507. }
  508. if (port_status & USB_PORT_STAT_RESET)
  509. musb_port_reset(0);
  510. *(__u32 *) data_buf = swap_32(port_status);
  511. len = 4;
  512. break;
  513. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  514. debug("RH_CLEAR_FEATURE | RH_ENDPOINT\n");
  515. switch (wValue) {
  516. case RH_ENDPOINT_STALL:
  517. debug("C_HUB_ENDPOINT_STALL\n");
  518. len = 0;
  519. break;
  520. }
  521. port_status &= ~(1 << wValue);
  522. break;
  523. case RH_CLEAR_FEATURE | RH_CLASS:
  524. debug("RH_CLEAR_FEATURE | RH_CLASS\n");
  525. switch (wValue) {
  526. case RH_C_HUB_LOCAL_POWER:
  527. debug("C_HUB_LOCAL_POWER\n");
  528. len = 0;
  529. break;
  530. case RH_C_HUB_OVER_CURRENT:
  531. debug("C_HUB_OVER_CURRENT\n");
  532. len = 0;
  533. break;
  534. }
  535. port_status &= ~(1 << wValue);
  536. break;
  537. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  538. debug("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS\n");
  539. switch (wValue) {
  540. case RH_PORT_ENABLE:
  541. len = 0;
  542. break;
  543. case RH_PORT_SUSPEND:
  544. len = 0;
  545. break;
  546. case RH_PORT_POWER:
  547. len = 0;
  548. break;
  549. case RH_C_PORT_CONNECTION:
  550. len = 0;
  551. break;
  552. case RH_C_PORT_ENABLE:
  553. len = 0;
  554. break;
  555. case RH_C_PORT_SUSPEND:
  556. len = 0;
  557. break;
  558. case RH_C_PORT_OVER_CURRENT:
  559. len = 0;
  560. break;
  561. case RH_C_PORT_RESET:
  562. len = 0;
  563. break;
  564. default:
  565. debug("invalid wValue\n");
  566. stat = USB_ST_STALLED;
  567. }
  568. port_status &= ~(1 << wValue);
  569. break;
  570. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  571. debug("RH_SET_FEATURE | RH_OTHER | RH_CLASS\n");
  572. switch (wValue) {
  573. case RH_PORT_SUSPEND:
  574. len = 0;
  575. break;
  576. case RH_PORT_RESET:
  577. musb_port_reset(1);
  578. len = 0;
  579. break;
  580. case RH_PORT_POWER:
  581. len = 0;
  582. break;
  583. case RH_PORT_ENABLE:
  584. len = 0;
  585. break;
  586. default:
  587. debug("invalid wValue\n");
  588. stat = USB_ST_STALLED;
  589. }
  590. port_status |= 1 << wValue;
  591. break;
  592. case RH_SET_ADDRESS:
  593. debug("RH_SET_ADDRESS\n");
  594. rh_devnum = wValue;
  595. len = 0;
  596. break;
  597. case RH_GET_DESCRIPTOR:
  598. debug("RH_GET_DESCRIPTOR: %x, %d\n", wValue, wLength);
  599. switch (wValue) {
  600. case (USB_DT_DEVICE << 8): /* device descriptor */
  601. len = min_t(unsigned int,
  602. leni, min_t(unsigned int,
  603. sizeof(root_hub_dev_des),
  604. wLength));
  605. data_buf = root_hub_dev_des;
  606. break;
  607. case (USB_DT_CONFIG << 8): /* configuration descriptor */
  608. len = min_t(unsigned int,
  609. leni, min_t(unsigned int,
  610. sizeof(root_hub_config_des),
  611. wLength));
  612. data_buf = root_hub_config_des;
  613. break;
  614. case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
  615. len = min_t(unsigned int,
  616. leni, min_t(unsigned int,
  617. sizeof(root_hub_str_index0),
  618. wLength));
  619. data_buf = root_hub_str_index0;
  620. break;
  621. case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
  622. len = min_t(unsigned int,
  623. leni, min_t(unsigned int,
  624. sizeof(root_hub_str_index1),
  625. wLength));
  626. data_buf = root_hub_str_index1;
  627. break;
  628. default:
  629. debug("invalid wValue\n");
  630. stat = USB_ST_STALLED;
  631. }
  632. break;
  633. case RH_GET_DESCRIPTOR | RH_CLASS: {
  634. u8 *_data_buf = (u8 *) datab;
  635. debug("RH_GET_DESCRIPTOR | RH_CLASS\n");
  636. _data_buf[0] = 0x09; /* min length; */
  637. _data_buf[1] = 0x29;
  638. _data_buf[2] = 0x1; /* 1 port */
  639. _data_buf[3] = 0x01; /* per-port power switching */
  640. _data_buf[3] |= 0x10; /* no overcurrent reporting */
  641. /* Corresponds to data_buf[4-7] */
  642. _data_buf[4] = 0;
  643. _data_buf[5] = 5;
  644. _data_buf[6] = 0;
  645. _data_buf[7] = 0x02;
  646. _data_buf[8] = 0xff;
  647. len = min_t(unsigned int, leni,
  648. min_t(unsigned int, data_buf[0], wLength));
  649. break;
  650. }
  651. case RH_GET_CONFIGURATION:
  652. debug("RH_GET_CONFIGURATION\n");
  653. *(__u8 *) data_buf = 0x01;
  654. len = 1;
  655. break;
  656. case RH_SET_CONFIGURATION:
  657. debug("RH_SET_CONFIGURATION\n");
  658. len = 0;
  659. break;
  660. default:
  661. debug("*** *** *** unsupported root hub command *** *** ***\n");
  662. stat = USB_ST_STALLED;
  663. }
  664. len = min_t(int, len, leni);
  665. if (buffer != data_buf)
  666. memcpy(buffer, data_buf, len);
  667. dev->act_len = len;
  668. dev->status = stat;
  669. debug("dev act_len %d, status %lu\n", dev->act_len, dev->status);
  670. return stat;
  671. }
  672. static void musb_rh_init(void)
  673. {
  674. rh_devnum = 0;
  675. port_status = 0;
  676. }
  677. #else
  678. static void musb_rh_init(void) {}
  679. #endif
  680. /*
  681. * do a control transfer
  682. */
  683. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  684. int len, struct devrequest *setup)
  685. {
  686. int devnum = usb_pipedevice(pipe);
  687. u8 devspeed;
  688. if(!(readb(&musbr->devctl) & (1<<2)))
  689. {
  690. return -1;
  691. }
  692. #ifdef MUSB_NO_MULTIPOINT
  693. /* Control message is for the HUB? */
  694. if (devnum == rh_devnum) {
  695. int stat = musb_submit_rh_msg(dev, pipe, buffer, len, setup);
  696. if (stat)
  697. return stat;
  698. }
  699. #endif
  700. /* select control endpoint */
  701. writeb(MUSB_CONTROL_EP, &musbr->index);
  702. readw(&musbr->txcsr);
  703. #ifndef MUSB_NO_MULTIPOINT
  704. /* target addr and (for multipoint) hub addr/port */
  705. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].txfuncaddr);
  706. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].rxfuncaddr);
  707. #endif
  708. /* configure the hub address and the port number as required */
  709. devspeed = get_dev_speed(dev);
  710. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  711. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  712. config_hub_port(dev, MUSB_CONTROL_EP);
  713. writeb(devspeed << 6, &musbr->txtype);
  714. } else {
  715. writeb(musb_cfg.musb_speed << 6, &musbr->txtype);
  716. #ifndef MUSB_NO_MULTIPOINT
  717. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubaddr);
  718. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubport);
  719. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubaddr);
  720. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubport);
  721. #endif
  722. }
  723. /* Control transfer setup phase */
  724. if (ctrlreq_setup_phase(dev, setup) < 0)
  725. return 0;
  726. switch (setup->request) {
  727. case USB_REQ_GET_DESCRIPTOR:
  728. case USB_REQ_GET_CONFIGURATION:
  729. case USB_REQ_GET_INTERFACE:
  730. case USB_REQ_GET_STATUS:
  731. case USB_MSC_BBB_GET_MAX_LUN:
  732. /* control transfer in-data-phase */
  733. if (ctrlreq_in_data_phase(dev, len, buffer) < 0)
  734. return 0;
  735. /* control transfer out-status-phase */
  736. if (ctrlreq_out_status_phase(dev) < 0)
  737. return 0;
  738. break;
  739. case USB_REQ_SET_ADDRESS:
  740. case USB_REQ_SET_CONFIGURATION:
  741. case USB_REQ_SET_FEATURE:
  742. case USB_REQ_SET_INTERFACE:
  743. case USB_REQ_CLEAR_FEATURE:
  744. case USB_MSC_BBB_RESET:
  745. /* control transfer in status phase */
  746. if (ctrlreq_in_status_phase(dev) < 0)
  747. return 0;
  748. break;
  749. case USB_REQ_SET_DESCRIPTOR:
  750. /* control transfer out data phase */
  751. if (ctrlreq_out_data_phase(dev, len, buffer) < 0)
  752. return 0;
  753. /* control transfer in status phase */
  754. if (ctrlreq_in_status_phase(dev) < 0)
  755. return 0;
  756. break;
  757. default:
  758. /* unhandled control transfer */
  759. return -1;
  760. }
  761. dev->status = 0;
  762. dev->act_len = len;
  763. #ifdef MUSB_NO_MULTIPOINT
  764. /* Set device address to USB_FADDR register */
  765. if (setup->request == USB_REQ_SET_ADDRESS)
  766. writeb(dev->devnum, &musbr->faddr);
  767. #endif
  768. return len;
  769. }
  770. /*
  771. * do a bulk transfer
  772. */
  773. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  774. void *buffer, int len)
  775. {
  776. int dir_out = usb_pipeout(pipe);
  777. int ep = usb_pipeendpoint(pipe);
  778. #ifndef MUSB_NO_MULTIPOINT
  779. int devnum = usb_pipedevice(pipe);
  780. #endif
  781. u8 type;
  782. u16 csr;
  783. u32 txlen = 0;
  784. u32 nextlen = 0;
  785. u8 devspeed;
  786. int ret = 0;
  787. u32 retry = 0;
  788. if(!(readb(&musbr->devctl) & (1<<2)))
  789. {
  790. return -1;
  791. }
  792. /* select bulk endpoint */
  793. writeb(MUSB_BULK_EP, &musbr->index);
  794. #ifndef MUSB_NO_MULTIPOINT
  795. /* write the address of the device */
  796. if (dir_out)
  797. writeb(devnum, &musbr->tar[MUSB_BULK_EP].txfuncaddr);
  798. else
  799. writeb(devnum, &musbr->tar[MUSB_BULK_EP].rxfuncaddr);
  800. #endif
  801. /* configure the hub address and the port number as required */
  802. devspeed = get_dev_speed(dev);
  803. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  804. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  805. /*
  806. * MUSB is in high speed and the destination device is full
  807. * speed device. So configure the hub address and port
  808. * address registers.
  809. */
  810. config_hub_port(dev, MUSB_BULK_EP);
  811. } else {
  812. #ifndef MUSB_NO_MULTIPOINT
  813. if (dir_out) {
  814. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubaddr);
  815. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubport);
  816. } else {
  817. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubaddr);
  818. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubport);
  819. }
  820. #endif
  821. devspeed = musb_cfg.musb_speed;
  822. }
  823. #ifdef CONFIG_ARK_MUSB
  824. if (dir_out) {
  825. csr = readw(&musbr->txcsr);
  826. /* flush fifo */
  827. while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  828. csr |= MUSB_TXCSR_FLUSHFIFO;
  829. csr |= MUSB_TXCSR_TXPKTRDY;
  830. writew(csr, &musbr->txcsr);
  831. csr = readw(&musbr->txcsr);
  832. if(!(readb(&musbr->devctl) & (1<<2)))
  833. {
  834. ret = -1;
  835. break;
  836. }
  837. }
  838. /*
  839. * We must not clear the DMAMODE bit before or in
  840. * the same cycle with the DMAENAB bit, so we clear
  841. * the latter first...
  842. */
  843. csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
  844. | MUSB_TXCSR_AUTOSET
  845. | MUSB_TXCSR_DMAENAB
  846. | MUSB_TXCSR_FRCDATATOG
  847. | MUSB_TXCSR_H_RXSTALL
  848. | MUSB_TXCSR_H_ERROR
  849. | MUSB_TXCSR_TXPKTRDY
  850. );
  851. csr |= MUSB_TXCSR_MODE;
  852. writew(csr, &musbr->txcsr);
  853. } else {
  854. csr = readw(&musbr->txcsr);
  855. /* Clear TX FIFO */
  856. while(csr & MUSB_TXCSR_FIFONOTEMPTY) {
  857. csr |= MUSB_TXCSR_FLUSHFIFO;
  858. csr |= MUSB_TXCSR_TXPKTRDY;
  859. writew(csr, &musbr->txcsr);
  860. csr = readw(&musbr->txcsr);
  861. if(!(readb(&musbr->devctl) & (1<<2)))
  862. {
  863. ret = -1;
  864. break;
  865. }
  866. }
  867. csr = readw(&musbr->txcsr);
  868. csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
  869. | MUSB_TXCSR_AUTOSET
  870. | MUSB_TXCSR_DMAENAB
  871. | MUSB_TXCSR_FRCDATATOG
  872. | MUSB_TXCSR_H_RXSTALL
  873. | MUSB_TXCSR_H_ERROR
  874. | MUSB_TXCSR_TXPKTRDY
  875. | MUSB_TXCSR_MODE
  876. );
  877. writew(csr, &musbr->txcsr);
  878. csr = readw(&musbr->txcsr);
  879. writew(csr | MUSB_TXCSR_FRCDATATOG, &musbr->txcsr);
  880. }
  881. #endif /* CONFIG_ARK_MUSB */
  882. if (dir_out) { /* bulk-out transfer */
  883. /* Write the saved toggle bit value */
  884. write_toggle(dev, ep, dir_out);
  885. /* Program the TxType register */
  886. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  887. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  888. (ep & MUSB_TYPE_REMOTE_END);
  889. writeb(type, &musbr->txtype);
  890. writeb(0, &musbr->txinterval);
  891. /* Write maximum packet size to the TxMaxp register */
  892. writew(dev->epmaxpacketout[ep], &musbr->txmaxp);
  893. while (txlen < len) {
  894. nextlen = ((len-txlen) < dev->epmaxpacketout[ep]) ?
  895. (len-txlen) : dev->epmaxpacketout[ep];
  896. /* Write the data to the FIFO */
  897. write_fifo(MUSB_BULK_EP, nextlen,
  898. (void *)(((u8 *)buffer) + txlen));
  899. /* Set the TxPktRdy bit */
  900. csr = readw(&musbr->txcsr);
  901. writew(csr | MUSB_TXCSR_TXPKTRDY, &musbr->txcsr);
  902. /* Wait until the TxPktRdy bit is cleared */
  903. if (wait_until_txep_ready(dev, MUSB_BULK_EP) != 1) {
  904. csr = readw(&musbr->txcsr);
  905. csr |= MUSB_TXCSR_FLUSHFIFO;
  906. //csr &= ~MUSB_TXCSR_TXPKTRDY;
  907. csr |= MUSB_TXCSR_TXPKTRDY; //FlushFIFO has no effect unless TXPKTRDY is set
  908. writew(csr, &musbr->txcsr);
  909. csr = readw(&musbr->txcsr);
  910. usb_settoggle(dev, ep, dir_out,
  911. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  912. dev->act_len = txlen;
  913. return 0;
  914. }
  915. txlen += nextlen;
  916. }
  917. /* Keep a copy of the data toggle bit */
  918. csr = readw(&musbr->txcsr);
  919. usb_settoggle(dev, ep, dir_out,
  920. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  921. } else { /* bulk-in transfer */
  922. /* Write the saved toggle bit value */
  923. write_toggle(dev, ep, dir_out);
  924. /* Program the RxType register */
  925. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  926. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  927. (ep & MUSB_TYPE_REMOTE_END);
  928. writeb(type, &musbr->rxtype);
  929. writeb(0, &musbr->rxinterval);
  930. /* Write the maximum packet size to the RxMaxp register */
  931. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  932. while (txlen < len) {
  933. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  934. (len-txlen) : dev->epmaxpacketin[ep];
  935. #if 0 /* ReqPkt failed, no retry */
  936. /* Set the ReqPkt bit */
  937. csr = readw(&musbr->rxcsr);
  938. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  939. /* Wait until the RxPktRdy bit is set */
  940. if (!wait_until_rxep_ready(dev, MUSB_BULK_EP) != 1) {
  941. csr = readw(&musbr->rxcsr);
  942. csr |= MUSB_RXCSR_FLUSHFIFO;
  943. //csr &= ~MUSB_RXCSR_RXPKTRDY;
  944. csr |= MUSB_RXCSR_RXPKTRDY;
  945. writew(csr, &musbr->rxcsr);
  946. usb_settoggle(dev, ep, dir_out,
  947. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  948. dev->act_len = txlen;
  949. return 0;
  950. }
  951. #else /* ReqPkt failed, then retry */
  952. retry = 100;
  953. reqpkt_again:
  954. /* Set the ReqPkt bit */
  955. csr = readw(&musbr->rxcsr);
  956. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  957. /* Wait until the RxPktRdy bit is set */
  958. if (!wait_until_rxep_ready(dev, MUSB_BULK_EP)) {
  959. //printf("###########retry:%d\n",101-retry);
  960. // Data err or NAK timeout
  961. if(dev->status == USB_ST_CRC_ERR)
  962. {
  963. udelay(1);
  964. if(--retry)
  965. goto reqpkt_again;
  966. }
  967. //Flush RxFIFO
  968. csr = readw(&musbr->rxcsr);
  969. csr |= MUSB_RXCSR_FLUSHFIFO;
  970. //csr &= ~MUSB_RXCSR_RXPKTRDY;
  971. csr |= MUSB_RXCSR_RXPKTRDY; //FlushFIFO has no effect unless rXPKTRDY is set
  972. writew(csr, &musbr->rxcsr);
  973. csr = readw(&musbr->rxcsr);
  974. usb_settoggle(dev, ep, dir_out,
  975. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  976. dev->act_len = txlen;
  977. //Check if usb is plug out
  978. if(!(readb(&musbr->devctl) & (1<<2)))
  979. {
  980. dev->status = USB_ST_NOT_PROC;
  981. return -1;
  982. }
  983. return 0;
  984. }
  985. #endif
  986. /* Read the data from the FIFO */
  987. read_fifo(MUSB_BULK_EP, nextlen,
  988. (void *)(((u8 *)buffer) + txlen));
  989. #if 0
  990. int i;
  991. printf("read_fifo:");
  992. for(i=0; i<nextlen; i++)
  993. printf("0x%x ", *((((u8 *)buffer) + txlen)+i));
  994. printf("\n");
  995. #endif
  996. /* Clear the RxPktRdy bit */
  997. csr = readw(&musbr->rxcsr);
  998. csr &= ~MUSB_RXCSR_RXPKTRDY;
  999. writew(csr, &musbr->rxcsr);
  1000. txlen += nextlen;
  1001. }
  1002. /* Keep a copy of the data toggle bit */
  1003. csr = readw(&musbr->rxcsr);
  1004. usb_settoggle(dev, ep, dir_out,
  1005. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  1006. }
  1007. /* bulk transfer is complete */
  1008. dev->status = 0;
  1009. dev->act_len = len;
  1010. return ret;
  1011. }
  1012. /*
  1013. * This function initializes the usb controller module.
  1014. */
  1015. extern int musb_platform_init(void);
  1016. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  1017. {
  1018. u8 power;
  1019. u32 timeout;
  1020. musb_rh_init();
  1021. if (musb_platform_init() == -1)
  1022. return -1;
  1023. /* Configure all the endpoint FIFO's and start usb controller */
  1024. musbr = musb_cfg.regs;
  1025. musb_configure_ep(&epinfo[0], ARRAY_SIZE(epinfo));
  1026. musb_start();
  1027. /*
  1028. * Wait until musb is enabled in host mode with a timeout. There
  1029. * should be a usb device connected.
  1030. */
  1031. timeout = musb_cfg.timeout;
  1032. while (--timeout) {
  1033. if (readb(&musbr->devctl) & MUSB_DEVCTL_HM)
  1034. break;
  1035. udelay(1);
  1036. }
  1037. if(timeout)
  1038. printf("=============== usb_lowlevel_init time is %d us===============\r\n", musb_cfg.timeout-timeout);
  1039. else
  1040. printf("=============== usb_lowlevel_init %d us timeout===============\r\n", musb_cfg.timeout);
  1041. /* if musb core is not in host mode, then return */
  1042. if (!timeout)
  1043. return -1;
  1044. /* start usb bus reset */
  1045. power = readb(&musbr->power);
  1046. writeb(power | MUSB_POWER_RESET, &musbr->power);
  1047. /* After initiating a usb reset, wait for about 20ms to 30ms */
  1048. udelay(25000);
  1049. /* stop usb bus reset */
  1050. power = readb(&musbr->power);
  1051. power &= ~MUSB_POWER_RESET;
  1052. writeb(power, &musbr->power);
  1053. /* Determine if the connected device is a high/full/low speed device */
  1054. musb_cfg.musb_speed = (readb(&musbr->power) & MUSB_POWER_HSMODE) ?
  1055. MUSB_TYPE_SPEED_HIGH :
  1056. ((readb(&musbr->devctl) & MUSB_DEVCTL_FSDEV) ?
  1057. MUSB_TYPE_SPEED_FULL : MUSB_TYPE_SPEED_LOW);
  1058. printf("=============== usb_lowlevel_init device is %s===============\r\n",
  1059. (readb(&musbr->power) & MUSB_POWER_HSMODE)?"high speed":"no high speed");
  1060. return 0;
  1061. }
  1062. /*
  1063. * This function stops the operation of the davinci usb module.
  1064. */
  1065. int usb_lowlevel_stop(int index)
  1066. {
  1067. /* Reset the USB module */
  1068. musb_platform_deinit();
  1069. writeb(0, &musbr->devctl);
  1070. return 0;
  1071. }
  1072. /*
  1073. * This function supports usb interrupt transfers. Currently, usb interrupt
  1074. * transfers are not supported.
  1075. */
  1076. int submit_int_msg(struct usb_device *dev, unsigned long pipe,
  1077. void *buffer, int len, int interval)
  1078. {
  1079. int dir_out = usb_pipeout(pipe);
  1080. int ep = usb_pipeendpoint(pipe);
  1081. #ifndef MUSB_NO_MULTIPOINT
  1082. int devnum = usb_pipedevice(pipe);
  1083. #endif
  1084. u8 type;
  1085. u16 csr;
  1086. u32 txlen = 0;
  1087. u32 nextlen = 0;
  1088. u8 devspeed;
  1089. /* select interrupt endpoint */
  1090. writeb(MUSB_INTR_EP, &musbr->index);
  1091. #ifndef MUSB_NO_MULTIPOINT
  1092. /* write the address of the device */
  1093. if (dir_out)
  1094. writeb(devnum, &musbr->tar[MUSB_INTR_EP].txfuncaddr);
  1095. else
  1096. writeb(devnum, &musbr->tar[MUSB_INTR_EP].rxfuncaddr);
  1097. #endif
  1098. /* configure the hub address and the port number as required */
  1099. devspeed = get_dev_speed(dev);
  1100. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  1101. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  1102. /*
  1103. * MUSB is in high speed and the destination device is full
  1104. * speed device. So configure the hub address and port
  1105. * address registers.
  1106. */
  1107. config_hub_port(dev, MUSB_INTR_EP);
  1108. } else {
  1109. #ifndef MUSB_NO_MULTIPOINT
  1110. if (dir_out) {
  1111. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubaddr);
  1112. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubport);
  1113. } else {
  1114. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubaddr);
  1115. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubport);
  1116. }
  1117. #endif
  1118. devspeed = musb_cfg.musb_speed;
  1119. }
  1120. #ifdef CONFIG_ARK_MUSB
  1121. if (dir_out) {
  1122. csr = readw(&musbr->txcsr);
  1123. /* flush fifo */
  1124. while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1125. csr |= MUSB_TXCSR_FLUSHFIFO;
  1126. writeb(csr, &musbr->txcsr);
  1127. csr = readw(&musbr->txcsr);
  1128. }
  1129. /*
  1130. * We must not clear the DMAMODE bit before or in
  1131. * the same cycle with the DMAENAB bit, so we clear
  1132. * the latter first...
  1133. */
  1134. csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
  1135. | MUSB_TXCSR_AUTOSET
  1136. | MUSB_TXCSR_DMAENAB
  1137. | MUSB_TXCSR_FRCDATATOG
  1138. | MUSB_TXCSR_H_RXSTALL
  1139. | MUSB_TXCSR_H_ERROR
  1140. | MUSB_TXCSR_TXPKTRDY
  1141. );
  1142. csr |= MUSB_TXCSR_MODE;
  1143. writew(csr, &musbr->txcsr);
  1144. } else {
  1145. csr = readw(&musbr->txcsr);
  1146. /* Clear TX FIFO */
  1147. while(csr & MUSB_TXCSR_FIFONOTEMPTY) {
  1148. csr |= MUSB_TXCSR_FLUSHFIFO;
  1149. csr = readw(&musbr->txcsr);
  1150. }
  1151. csr = readw(&musbr->txcsr);
  1152. csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
  1153. | MUSB_TXCSR_AUTOSET
  1154. | MUSB_TXCSR_DMAENAB
  1155. | MUSB_TXCSR_FRCDATATOG
  1156. | MUSB_TXCSR_H_RXSTALL
  1157. | MUSB_TXCSR_H_ERROR
  1158. | MUSB_TXCSR_TXPKTRDY
  1159. | MUSB_TXCSR_MODE
  1160. );
  1161. writew(csr, &musbr->txcsr);
  1162. csr = readw(&musbr->txcsr);
  1163. writew(csr | MUSB_TXCSR_FRCDATATOG, &musbr->txcsr);
  1164. }
  1165. #endif /* CONFIG_ARK_MUSB */
  1166. /* Write the saved toggle bit value */
  1167. write_toggle(dev, ep, dir_out);
  1168. if (!dir_out) { /* intrrupt-in transfer */
  1169. /* Write the saved toggle bit value */
  1170. write_toggle(dev, ep, dir_out);
  1171. writeb(interval, &musbr->rxinterval);
  1172. /* Program the RxType register */
  1173. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  1174. (MUSB_TYPE_PROTO_INTR << MUSB_TYPE_PROTO_SHIFT) |
  1175. (ep & MUSB_TYPE_REMOTE_END);
  1176. writeb(type, &musbr->rxtype);
  1177. /* Write the maximum packet size to the RxMaxp register */
  1178. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  1179. while (txlen < len) {
  1180. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  1181. (len-txlen) : dev->epmaxpacketin[ep];
  1182. /* Set the ReqPkt bit */
  1183. csr = readw(&musbr->rxcsr);
  1184. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  1185. /* Wait until the RxPktRdy bit is set */
  1186. if (wait_until_rxep_ready(dev, MUSB_INTR_EP) != 1) {
  1187. csr = readw(&musbr->rxcsr);
  1188. usb_settoggle(dev, ep, dir_out,
  1189. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  1190. csr &= ~MUSB_RXCSR_RXPKTRDY;
  1191. writew(csr, &musbr->rxcsr);
  1192. dev->act_len = txlen;
  1193. return 0;
  1194. }
  1195. /* Read the data from the FIFO */
  1196. read_fifo(MUSB_INTR_EP, nextlen,
  1197. (void *)(((u8 *)buffer) + txlen));
  1198. /* Clear the RxPktRdy bit */
  1199. csr = readw(&musbr->rxcsr);
  1200. csr &= ~MUSB_RXCSR_RXPKTRDY;
  1201. writew(csr, &musbr->rxcsr);
  1202. txlen += nextlen;
  1203. }
  1204. /* Keep a copy of the data toggle bit */
  1205. csr = readw(&musbr->rxcsr);
  1206. usb_settoggle(dev, ep, dir_out,
  1207. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  1208. }
  1209. /* interrupt transfer is complete */
  1210. dev->irq_status = 0;
  1211. dev->irq_act_len = len;
  1212. dev->irq_handle(dev);
  1213. dev->status = 0;
  1214. dev->act_len = len;
  1215. return 0;
  1216. }