musb_hcd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * Mentor USB OTG Core host controller driver.
  3. *
  4. * Copyright (c) 2008 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. *
  21. * Author: Thomas Abraham t-abraham@ti.com, Texas Instruments
  22. */
  23. #include <common.h>
  24. #include "musb_hcd.h"
  25. /* MSC control transfers */
  26. #define USB_MSC_BBB_RESET 0xFF
  27. #define USB_MSC_BBB_GET_MAX_LUN 0xFE
  28. /* Endpoint configuration information */
  29. static struct musb_epinfo epinfo[3] = {
  30. {MUSB_BULK_EP, 1, 512}, /* EP1 - Bluk Out - 512 Bytes */
  31. {MUSB_BULK_EP, 0, 512}, /* EP1 - Bluk In - 512 Bytes */
  32. {MUSB_INTR_EP, 0, 64} /* EP2 - Interrupt IN - 64 Bytes */
  33. };
  34. /*
  35. * This function writes the data toggle value.
  36. */
  37. static void write_toggle(struct usb_device *dev, u8 ep, u8 dir_out)
  38. {
  39. u16 toggle = usb_gettoggle(dev, ep, dir_out);
  40. u16 csr;
  41. if (dir_out) {
  42. if (!toggle)
  43. writew(MUSB_TXCSR_CLRDATATOG, &musbr->txcsr);
  44. else {
  45. csr = readw(&musbr->txcsr);
  46. csr |= MUSB_TXCSR_H_WR_DATATOGGLE;
  47. writew(csr, &musbr->txcsr);
  48. csr |= (toggle << MUSB_TXCSR_H_DATATOGGLE_SHIFT);
  49. writew(csr, &musbr->txcsr);
  50. }
  51. } else {
  52. if (!toggle)
  53. writew(MUSB_RXCSR_CLRDATATOG, &musbr->rxcsr);
  54. else {
  55. csr = readw(&musbr->rxcsr);
  56. csr |= MUSB_RXCSR_H_WR_DATATOGGLE;
  57. writew(csr, &musbr->rxcsr);
  58. csr |= (toggle << MUSB_S_RXCSR_H_DATATOGGLE);
  59. writew(csr, &musbr->rxcsr);
  60. }
  61. }
  62. }
  63. /*
  64. * This function checks if RxStall has occured on the endpoint. If a RxStall
  65. * has occured, the RxStall is cleared and 1 is returned. If RxStall has
  66. * not occured, 0 is returned.
  67. */
  68. static u8 check_stall(u8 ep, u8 dir_out)
  69. {
  70. u16 csr;
  71. /* For endpoint 0 */
  72. if (!ep) {
  73. csr = readw(&musbr->txcsr);
  74. if (csr & MUSB_CSR0_H_RXSTALL) {
  75. csr &= ~MUSB_CSR0_H_RXSTALL;
  76. writew(csr, &musbr->txcsr);
  77. return 1;
  78. }
  79. } else { /* For non-ep0 */
  80. if (dir_out) { /* is it tx ep */
  81. csr = readw(&musbr->txcsr);
  82. if (csr & MUSB_TXCSR_H_RXSTALL) {
  83. csr &= ~MUSB_TXCSR_H_RXSTALL;
  84. writew(csr, &musbr->txcsr);
  85. return 1;
  86. }
  87. } else { /* is it rx ep */
  88. csr = readw(&musbr->rxcsr);
  89. if (csr & MUSB_RXCSR_H_RXSTALL) {
  90. csr &= ~MUSB_RXCSR_H_RXSTALL;
  91. writew(csr, &musbr->rxcsr);
  92. return 1;
  93. }
  94. }
  95. }
  96. return 0;
  97. }
  98. /*
  99. * waits until ep0 is ready. Returns 0 if ep is ready, -1 for timeout
  100. * error and -2 for stall.
  101. */
  102. static int wait_until_ep0_ready(struct usb_device *dev, u32 bit_mask)
  103. {
  104. u16 csr;
  105. int result = 1;
  106. int timeout = CONFIG_MUSB_TIMEOUT;
  107. while (result > 0) {
  108. csr = readw(&musbr->txcsr);
  109. if (csr & MUSB_CSR0_H_ERROR) {
  110. csr &= ~MUSB_CSR0_H_ERROR;
  111. writew(csr, &musbr->txcsr);
  112. dev->status = USB_ST_CRC_ERR;
  113. result = -1;
  114. break;
  115. }
  116. switch (bit_mask) {
  117. case MUSB_CSR0_TXPKTRDY:
  118. if (!(csr & MUSB_CSR0_TXPKTRDY)) {
  119. if (check_stall(MUSB_CONTROL_EP, 0)) {
  120. dev->status = USB_ST_STALLED;
  121. result = -2;
  122. } else
  123. result = 0;
  124. }
  125. break;
  126. case MUSB_CSR0_RXPKTRDY:
  127. if (check_stall(MUSB_CONTROL_EP, 0)) {
  128. dev->status = USB_ST_STALLED;
  129. result = -2;
  130. } else
  131. if (csr & MUSB_CSR0_RXPKTRDY)
  132. result = 0;
  133. break;
  134. case MUSB_CSR0_H_REQPKT:
  135. if (!(csr & MUSB_CSR0_H_REQPKT)) {
  136. if (check_stall(MUSB_CONTROL_EP, 0)) {
  137. dev->status = USB_ST_STALLED;
  138. result = -2;
  139. } else
  140. result = 0;
  141. }
  142. break;
  143. }
  144. /* Check the timeout */
  145. if (--timeout)
  146. udelay(1);
  147. else {
  148. dev->status = USB_ST_CRC_ERR;
  149. result = -1;
  150. break;
  151. }
  152. }
  153. return result;
  154. }
  155. /*
  156. * waits until tx ep is ready. Returns 1 when ep is ready and 0 on error.
  157. */
  158. static u8 wait_until_txep_ready(struct usb_device *dev, u8 ep)
  159. {
  160. u16 csr;
  161. int timeout = CONFIG_MUSB_TIMEOUT;
  162. do {
  163. if (check_stall(ep, 1)) {
  164. dev->status = USB_ST_STALLED;
  165. return 0;
  166. }
  167. csr = readw(&musbr->txcsr);
  168. if (csr & MUSB_TXCSR_H_ERROR) {
  169. dev->status = USB_ST_CRC_ERR;
  170. return 0;
  171. }
  172. /* Check the timeout */
  173. if (--timeout)
  174. udelay(1);
  175. else {
  176. dev->status = USB_ST_CRC_ERR;
  177. return -1;
  178. }
  179. } while (csr & MUSB_TXCSR_TXPKTRDY);
  180. return 1;
  181. }
  182. /*
  183. * waits until rx ep is ready. Returns 1 when ep is ready and 0 on error.
  184. */
  185. static u8 wait_until_rxep_ready(struct usb_device *dev, u8 ep)
  186. {
  187. u16 csr;
  188. int timeout = CONFIG_MUSB_TIMEOUT;
  189. do {
  190. if (check_stall(ep, 0)) {
  191. dev->status = USB_ST_STALLED;
  192. return 0;
  193. }
  194. csr = readw(&musbr->rxcsr);
  195. if (csr & MUSB_RXCSR_H_ERROR) {
  196. dev->status = USB_ST_CRC_ERR;
  197. return 0;
  198. }
  199. /* Check the timeout */
  200. if (--timeout)
  201. udelay(1);
  202. else {
  203. dev->status = USB_ST_CRC_ERR;
  204. return -1;
  205. }
  206. } while (!(csr & MUSB_RXCSR_RXPKTRDY));
  207. return 1;
  208. }
  209. /*
  210. * This function performs the setup phase of the control transfer
  211. */
  212. static int ctrlreq_setup_phase(struct usb_device *dev, struct devrequest *setup)
  213. {
  214. int result;
  215. u16 csr;
  216. /* write the control request to ep0 fifo */
  217. write_fifo(MUSB_CONTROL_EP, sizeof(struct devrequest), (void *)setup);
  218. /* enable transfer of setup packet */
  219. csr = readw(&musbr->txcsr);
  220. csr |= (MUSB_CSR0_TXPKTRDY|MUSB_CSR0_H_SETUPPKT);
  221. writew(csr, &musbr->txcsr);
  222. /* wait until the setup packet is transmitted */
  223. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  224. dev->act_len = 0;
  225. return result;
  226. }
  227. /*
  228. * This function handles the control transfer in data phase
  229. */
  230. static int ctrlreq_in_data_phase(struct usb_device *dev, u32 len, void *buffer)
  231. {
  232. u16 csr;
  233. u32 rxlen = 0;
  234. u32 nextlen = 0;
  235. u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
  236. u8 *rxbuff = (u8 *)buffer;
  237. u8 rxedlength;
  238. int result;
  239. while (rxlen < len) {
  240. /* Determine the next read length */
  241. nextlen = ((len-rxlen) > maxpktsize) ? maxpktsize : (len-rxlen);
  242. /* Set the ReqPkt bit */
  243. csr = readw(&musbr->txcsr);
  244. writew(csr | MUSB_CSR0_H_REQPKT, &musbr->txcsr);
  245. result = wait_until_ep0_ready(dev, MUSB_CSR0_RXPKTRDY);
  246. if (result < 0)
  247. return result;
  248. /* Actual number of bytes received by usb */
  249. rxedlength = readb(&musbr->rxcount);
  250. /* Read the data from the RxFIFO */
  251. read_fifo(MUSB_CONTROL_EP, rxedlength, &rxbuff[rxlen]);
  252. /* Clear the RxPktRdy Bit */
  253. csr = readw(&musbr->txcsr);
  254. csr &= ~MUSB_CSR0_RXPKTRDY;
  255. writew(csr, &musbr->txcsr);
  256. /* short packet? */
  257. if (rxedlength != nextlen) {
  258. dev->act_len += rxedlength;
  259. break;
  260. }
  261. rxlen += nextlen;
  262. dev->act_len = rxlen;
  263. }
  264. return 0;
  265. }
  266. /*
  267. * This function handles the control transfer out data phase
  268. */
  269. static int ctrlreq_out_data_phase(struct usb_device *dev, u32 len, void *buffer)
  270. {
  271. u16 csr;
  272. u32 txlen = 0;
  273. u32 nextlen = 0;
  274. u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
  275. u8 *txbuff = (u8 *)buffer;
  276. int result = 0;
  277. while (txlen < len) {
  278. /* Determine the next write length */
  279. nextlen = ((len-txlen) > maxpktsize) ? maxpktsize : (len-txlen);
  280. /* Load the data to send in FIFO */
  281. write_fifo(MUSB_CONTROL_EP, txlen, &txbuff[txlen]);
  282. /* Set TXPKTRDY bit */
  283. csr = readw(&musbr->txcsr);
  284. writew(csr | MUSB_CSR0_H_DIS_PING | MUSB_CSR0_TXPKTRDY,
  285. &musbr->txcsr);
  286. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  287. if (result < 0)
  288. break;
  289. txlen += nextlen;
  290. dev->act_len = txlen;
  291. }
  292. return result;
  293. }
  294. /*
  295. * This function handles the control transfer out status phase
  296. */
  297. static int ctrlreq_out_status_phase(struct usb_device *dev)
  298. {
  299. u16 csr;
  300. int result;
  301. /* Set the StatusPkt bit */
  302. csr = readw(&musbr->txcsr);
  303. csr |= (MUSB_CSR0_H_DIS_PING | MUSB_CSR0_TXPKTRDY |
  304. MUSB_CSR0_H_STATUSPKT);
  305. writew(csr, &musbr->txcsr);
  306. /* Wait until TXPKTRDY bit is cleared */
  307. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  308. return result;
  309. }
  310. /*
  311. * This function handles the control transfer in status phase
  312. */
  313. static int ctrlreq_in_status_phase(struct usb_device *dev)
  314. {
  315. u16 csr;
  316. int result;
  317. /* Set the StatusPkt bit and ReqPkt bit */
  318. csr = MUSB_CSR0_H_DIS_PING | MUSB_CSR0_H_REQPKT | MUSB_CSR0_H_STATUSPKT;
  319. writew(csr, &musbr->txcsr);
  320. result = wait_until_ep0_ready(dev, MUSB_CSR0_H_REQPKT);
  321. /* clear StatusPkt bit and RxPktRdy bit */
  322. csr = readw(&musbr->txcsr);
  323. csr &= ~(MUSB_CSR0_RXPKTRDY | MUSB_CSR0_H_STATUSPKT);
  324. writew(csr, &musbr->txcsr);
  325. return result;
  326. }
  327. /*
  328. * determines the speed of the device (High/Full/Slow)
  329. */
  330. static u8 get_dev_speed(struct usb_device *dev)
  331. {
  332. return (dev->speed & USB_SPEED_HIGH) ? MUSB_TYPE_SPEED_HIGH :
  333. ((dev->speed & USB_SPEED_LOW) ? MUSB_TYPE_SPEED_LOW :
  334. MUSB_TYPE_SPEED_FULL);
  335. }
  336. /*
  337. * configure the hub address and the port address.
  338. */
  339. static void config_hub_port(struct usb_device *dev, u8 ep)
  340. {
  341. u8 chid;
  342. u8 hub;
  343. /* Find out the nearest parent which is high speed */
  344. while (dev->parent->parent != NULL)
  345. if (get_dev_speed(dev->parent) != MUSB_TYPE_SPEED_HIGH)
  346. dev = dev->parent;
  347. else
  348. break;
  349. /* determine the port address at that hub */
  350. hub = dev->parent->devnum;
  351. for (chid = 0; chid < USB_MAXCHILDREN; chid++)
  352. if (dev->parent->children[chid] == dev)
  353. break;
  354. #ifndef MUSB_NO_MULTIPOINT
  355. /* configure the hub address and the port address */
  356. writeb(hub, &musbr->tar[ep].txhubaddr);
  357. writeb((chid + 1), &musbr->tar[ep].txhubport);
  358. writeb(hub, &musbr->tar[ep].rxhubaddr);
  359. writeb((chid + 1), &musbr->tar[ep].rxhubport);
  360. #endif
  361. }
  362. /*
  363. * do a control transfer
  364. */
  365. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  366. int len, struct devrequest *setup)
  367. {
  368. #ifndef MUSB_NO_MULTIPOINT
  369. int devnum = usb_pipedevice(pipe);
  370. #endif
  371. u16 csr;
  372. u8 devspeed;
  373. /* select control endpoint */
  374. writeb(MUSB_CONTROL_EP, &musbr->index);
  375. csr = readw(&musbr->txcsr);
  376. #ifndef MUSB_NO_MULTIPOINT
  377. /* target addr and (for multipoint) hub addr/port */
  378. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].txfuncaddr);
  379. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].rxfuncaddr);
  380. #endif
  381. /* configure the hub address and the port number as required */
  382. devspeed = get_dev_speed(dev);
  383. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  384. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  385. config_hub_port(dev, MUSB_CONTROL_EP);
  386. writeb(devspeed << 6, &musbr->txtype);
  387. } else {
  388. writeb(musb_cfg.musb_speed << 6, &musbr->txtype);
  389. #ifndef MUSB_NO_MULTIPOINT
  390. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubaddr);
  391. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubport);
  392. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubaddr);
  393. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubport);
  394. #endif
  395. }
  396. /* Control transfer setup phase */
  397. if (ctrlreq_setup_phase(dev, setup) < 0)
  398. return 0;
  399. switch (setup->request) {
  400. case USB_REQ_GET_DESCRIPTOR:
  401. case USB_REQ_GET_CONFIGURATION:
  402. case USB_REQ_GET_INTERFACE:
  403. case USB_REQ_GET_STATUS:
  404. case USB_MSC_BBB_GET_MAX_LUN:
  405. /* control transfer in-data-phase */
  406. if (ctrlreq_in_data_phase(dev, len, buffer) < 0)
  407. return 0;
  408. /* control transfer out-status-phase */
  409. if (ctrlreq_out_status_phase(dev) < 0)
  410. return 0;
  411. break;
  412. case USB_REQ_SET_ADDRESS:
  413. case USB_REQ_SET_CONFIGURATION:
  414. case USB_REQ_SET_FEATURE:
  415. case USB_REQ_SET_INTERFACE:
  416. case USB_REQ_CLEAR_FEATURE:
  417. case USB_MSC_BBB_RESET:
  418. /* control transfer in status phase */
  419. if (ctrlreq_in_status_phase(dev) < 0)
  420. return 0;
  421. break;
  422. case USB_REQ_SET_DESCRIPTOR:
  423. /* control transfer out data phase */
  424. if (ctrlreq_out_data_phase(dev, len, buffer) < 0)
  425. return 0;
  426. /* control transfer in status phase */
  427. if (ctrlreq_in_status_phase(dev) < 0)
  428. return 0;
  429. break;
  430. default:
  431. /* unhandled control transfer */
  432. return -1;
  433. }
  434. dev->status = 0;
  435. dev->act_len = len;
  436. return len;
  437. }
  438. /*
  439. * do a bulk transfer
  440. */
  441. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  442. void *buffer, int len)
  443. {
  444. int dir_out = usb_pipeout(pipe);
  445. int ep = usb_pipeendpoint(pipe);
  446. #ifndef MUSB_NO_MULTIPOINT
  447. int devnum = usb_pipedevice(pipe);
  448. #endif
  449. u8 type;
  450. u16 csr;
  451. u32 txlen = 0;
  452. u32 nextlen = 0;
  453. u8 devspeed;
  454. /* select bulk endpoint */
  455. writeb(MUSB_BULK_EP, &musbr->index);
  456. #ifndef MUSB_NO_MULTIPOINT
  457. /* write the address of the device */
  458. if (dir_out)
  459. writeb(devnum, &musbr->tar[MUSB_BULK_EP].txfuncaddr);
  460. else
  461. writeb(devnum, &musbr->tar[MUSB_BULK_EP].rxfuncaddr);
  462. #endif
  463. /* configure the hub address and the port number as required */
  464. devspeed = get_dev_speed(dev);
  465. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  466. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  467. /*
  468. * MUSB is in high speed and the destination device is full
  469. * speed device. So configure the hub address and port
  470. * address registers.
  471. */
  472. config_hub_port(dev, MUSB_BULK_EP);
  473. } else {
  474. #ifndef MUSB_NO_MULTIPOINT
  475. if (dir_out) {
  476. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubaddr);
  477. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubport);
  478. } else {
  479. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubaddr);
  480. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubport);
  481. }
  482. #endif
  483. devspeed = musb_cfg.musb_speed;
  484. }
  485. /* Write the saved toggle bit value */
  486. write_toggle(dev, ep, dir_out);
  487. if (dir_out) { /* bulk-out transfer */
  488. /* Program the TxType register */
  489. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  490. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  491. (ep & MUSB_TYPE_REMOTE_END);
  492. writeb(type, &musbr->txtype);
  493. /* Write maximum packet size to the TxMaxp register */
  494. writew(dev->epmaxpacketout[ep], &musbr->txmaxp);
  495. while (txlen < len) {
  496. nextlen = ((len-txlen) < dev->epmaxpacketout[ep]) ?
  497. (len-txlen) : dev->epmaxpacketout[ep];
  498. /* Write the data to the FIFO */
  499. write_fifo(MUSB_BULK_EP, nextlen,
  500. (void *)(((u8 *)buffer) + txlen));
  501. /* Set the TxPktRdy bit */
  502. csr = readw(&musbr->txcsr);
  503. writew(csr | MUSB_TXCSR_TXPKTRDY, &musbr->txcsr);
  504. /* Wait until the TxPktRdy bit is cleared */
  505. if (!wait_until_txep_ready(dev, MUSB_BULK_EP)) {
  506. readw(&musbr->txcsr);
  507. usb_settoggle(dev, ep, dir_out,
  508. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  509. dev->act_len = txlen;
  510. return 0;
  511. }
  512. txlen += nextlen;
  513. }
  514. /* Keep a copy of the data toggle bit */
  515. csr = readw(&musbr->txcsr);
  516. usb_settoggle(dev, ep, dir_out,
  517. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  518. } else { /* bulk-in transfer */
  519. /* Write the saved toggle bit value */
  520. write_toggle(dev, ep, dir_out);
  521. /* Program the RxType register */
  522. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  523. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  524. (ep & MUSB_TYPE_REMOTE_END);
  525. writeb(type, &musbr->rxtype);
  526. /* Write the maximum packet size to the RxMaxp register */
  527. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  528. while (txlen < len) {
  529. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  530. (len-txlen) : dev->epmaxpacketin[ep];
  531. /* Set the ReqPkt bit */
  532. csr = readw(&musbr->rxcsr);
  533. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  534. /* Wait until the RxPktRdy bit is set */
  535. if (!wait_until_rxep_ready(dev, MUSB_BULK_EP)) {
  536. csr = readw(&musbr->rxcsr);
  537. usb_settoggle(dev, ep, dir_out,
  538. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  539. csr &= ~MUSB_RXCSR_RXPKTRDY;
  540. writew(csr, &musbr->rxcsr);
  541. dev->act_len = txlen;
  542. return 0;
  543. }
  544. /* Read the data from the FIFO */
  545. read_fifo(MUSB_BULK_EP, nextlen,
  546. (void *)(((u8 *)buffer) + txlen));
  547. /* Clear the RxPktRdy bit */
  548. csr = readw(&musbr->rxcsr);
  549. csr &= ~MUSB_RXCSR_RXPKTRDY;
  550. writew(csr, &musbr->rxcsr);
  551. txlen += nextlen;
  552. }
  553. /* Keep a copy of the data toggle bit */
  554. csr = readw(&musbr->rxcsr);
  555. usb_settoggle(dev, ep, dir_out,
  556. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  557. }
  558. /* bulk transfer is complete */
  559. dev->status = 0;
  560. dev->act_len = len;
  561. return 0;
  562. }
  563. /*
  564. * This function initializes the usb controller module.
  565. */
  566. int usb_lowlevel_init(void)
  567. {
  568. u8 power;
  569. u32 timeout;
  570. if (musb_platform_init() == -1)
  571. return -1;
  572. /* Configure all the endpoint FIFO's and start usb controller */
  573. musbr = musb_cfg.regs;
  574. musb_configure_ep(&epinfo[0],
  575. sizeof(epinfo) / sizeof(struct musb_epinfo));
  576. musb_start();
  577. /*
  578. * Wait until musb is enabled in host mode with a timeout. There
  579. * should be a usb device connected.
  580. */
  581. timeout = musb_cfg.timeout;
  582. while (timeout--)
  583. if (readb(&musbr->devctl) & MUSB_DEVCTL_HM)
  584. break;
  585. /* if musb core is not in host mode, then return */
  586. if (!timeout)
  587. return -1;
  588. /* start usb bus reset */
  589. power = readb(&musbr->power);
  590. writeb(power | MUSB_POWER_RESET, &musbr->power);
  591. /* After initiating a usb reset, wait for about 20ms to 30ms */
  592. udelay(30000);
  593. /* stop usb bus reset */
  594. power = readb(&musbr->power);
  595. power &= ~MUSB_POWER_RESET;
  596. writeb(power, &musbr->power);
  597. /* Determine if the connected device is a high/full/low speed device */
  598. musb_cfg.musb_speed = (readb(&musbr->power) & MUSB_POWER_HSMODE) ?
  599. MUSB_TYPE_SPEED_HIGH :
  600. ((readb(&musbr->devctl) & MUSB_DEVCTL_FSDEV) ?
  601. MUSB_TYPE_SPEED_FULL : MUSB_TYPE_SPEED_LOW);
  602. return 0;
  603. }
  604. /*
  605. * This function stops the operation of the davinci usb module.
  606. */
  607. int usb_lowlevel_stop(void)
  608. {
  609. /* Reset the USB module */
  610. musb_platform_deinit();
  611. writeb(0, &musbr->devctl);
  612. return 0;
  613. }
  614. /*
  615. * This function supports usb interrupt transfers. Currently, usb interrupt
  616. * transfers are not supported.
  617. */
  618. int submit_int_msg(struct usb_device *dev, unsigned long pipe,
  619. void *buffer, int len, int interval)
  620. {
  621. int dir_out = usb_pipeout(pipe);
  622. int ep = usb_pipeendpoint(pipe);
  623. #ifndef MUSB_NO_MULTIPOINT
  624. int devnum = usb_pipedevice(pipe);
  625. #endif
  626. u8 type;
  627. u16 csr;
  628. u32 txlen = 0;
  629. u32 nextlen = 0;
  630. u8 devspeed;
  631. /* select interrupt endpoint */
  632. writeb(MUSB_INTR_EP, &musbr->index);
  633. #ifndef MUSB_NO_MULTIPOINT
  634. /* write the address of the device */
  635. if (dir_out)
  636. writeb(devnum, &musbr->tar[MUSB_INTR_EP].txfuncaddr);
  637. else
  638. writeb(devnum, &musbr->tar[MUSB_INTR_EP].rxfuncaddr);
  639. #endif
  640. /* configure the hub address and the port number as required */
  641. devspeed = get_dev_speed(dev);
  642. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  643. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  644. /*
  645. * MUSB is in high speed and the destination device is full
  646. * speed device. So configure the hub address and port
  647. * address registers.
  648. */
  649. config_hub_port(dev, MUSB_INTR_EP);
  650. } else {
  651. #ifndef MUSB_NO_MULTIPOINT
  652. if (dir_out) {
  653. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubaddr);
  654. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubport);
  655. } else {
  656. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubaddr);
  657. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubport);
  658. }
  659. #endif
  660. devspeed = musb_cfg.musb_speed;
  661. }
  662. /* Write the saved toggle bit value */
  663. write_toggle(dev, ep, dir_out);
  664. if (!dir_out) { /* intrrupt-in transfer */
  665. /* Write the saved toggle bit value */
  666. write_toggle(dev, ep, dir_out);
  667. writeb(interval, &musbr->rxinterval);
  668. /* Program the RxType register */
  669. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  670. (MUSB_TYPE_PROTO_INTR << MUSB_TYPE_PROTO_SHIFT) |
  671. (ep & MUSB_TYPE_REMOTE_END);
  672. writeb(type, &musbr->rxtype);
  673. /* Write the maximum packet size to the RxMaxp register */
  674. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  675. while (txlen < len) {
  676. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  677. (len-txlen) : dev->epmaxpacketin[ep];
  678. /* Set the ReqPkt bit */
  679. csr = readw(&musbr->rxcsr);
  680. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  681. /* Wait until the RxPktRdy bit is set */
  682. if (!wait_until_rxep_ready(dev, MUSB_INTR_EP)) {
  683. csr = readw(&musbr->rxcsr);
  684. usb_settoggle(dev, ep, dir_out,
  685. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  686. csr &= ~MUSB_RXCSR_RXPKTRDY;
  687. writew(csr, &musbr->rxcsr);
  688. dev->act_len = txlen;
  689. return 0;
  690. }
  691. /* Read the data from the FIFO */
  692. read_fifo(MUSB_INTR_EP, nextlen,
  693. (void *)(((u8 *)buffer) + txlen));
  694. /* Clear the RxPktRdy bit */
  695. csr = readw(&musbr->rxcsr);
  696. csr &= ~MUSB_RXCSR_RXPKTRDY;
  697. writew(csr, &musbr->rxcsr);
  698. txlen += nextlen;
  699. }
  700. /* Keep a copy of the data toggle bit */
  701. csr = readw(&musbr->rxcsr);
  702. usb_settoggle(dev, ep, dir_out,
  703. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  704. }
  705. /* interrupt transfer is complete */
  706. dev->irq_status = 0;
  707. dev->irq_act_len = len;
  708. dev->irq_handle(dev);
  709. dev->status = 0;
  710. dev->act_len = len;
  711. return 0;
  712. }
  713. #ifdef CONFIG_SYS_USB_EVENT_POLL
  714. /*
  715. * This function polls for USB keyboard data.
  716. */
  717. void usb_event_poll()
  718. {
  719. struct stdio_dev *dev;
  720. struct usb_device *usb_kbd_dev;
  721. struct usb_interface *iface;
  722. struct usb_endpoint_descriptor *ep;
  723. int pipe;
  724. int maxp;
  725. /* Get the pointer to USB Keyboard device pointer */
  726. dev = stdio_get_by_name("usbkbd");
  727. usb_kbd_dev = (struct usb_device *)dev->priv;
  728. iface = &usb_kbd_dev->config.if_desc[0];
  729. ep = &iface->ep_desc[0];
  730. pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
  731. /* Submit a interrupt transfer request */
  732. maxp = usb_maxpacket(usb_kbd_dev, pipe);
  733. usb_submit_int_msg(usb_kbd_dev, pipe, &new[0],
  734. maxp > 8 ? 8 : maxp, ep->bInterval);
  735. }
  736. #endif /* CONFIG_SYS_USB_EVENT_POLL */