musb_hcd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. /* configure the hub address and the port address */
  355. writeb(hub, &musbr->tar[ep].txhubaddr);
  356. writeb((chid + 1), &musbr->tar[ep].txhubport);
  357. writeb(hub, &musbr->tar[ep].rxhubaddr);
  358. writeb((chid + 1), &musbr->tar[ep].rxhubport);
  359. }
  360. /*
  361. * do a control transfer
  362. */
  363. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  364. int len, struct devrequest *setup)
  365. {
  366. int devnum = usb_pipedevice(pipe);
  367. u16 csr;
  368. u8 devspeed;
  369. /* select control endpoint */
  370. writeb(MUSB_CONTROL_EP, &musbr->index);
  371. csr = readw(&musbr->txcsr);
  372. /* target addr and (for multipoint) hub addr/port */
  373. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].txfuncaddr);
  374. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].rxfuncaddr);
  375. /* configure the hub address and the port number as required */
  376. devspeed = get_dev_speed(dev);
  377. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  378. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  379. config_hub_port(dev, MUSB_CONTROL_EP);
  380. writeb(devspeed << 6, &musbr->txtype);
  381. } else {
  382. writeb(musb_cfg.musb_speed << 6, &musbr->txtype);
  383. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubaddr);
  384. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubport);
  385. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubaddr);
  386. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubport);
  387. }
  388. /* Control transfer setup phase */
  389. if (ctrlreq_setup_phase(dev, setup) < 0)
  390. return 0;
  391. switch (setup->request) {
  392. case USB_REQ_GET_DESCRIPTOR:
  393. case USB_REQ_GET_CONFIGURATION:
  394. case USB_REQ_GET_INTERFACE:
  395. case USB_REQ_GET_STATUS:
  396. case USB_MSC_BBB_GET_MAX_LUN:
  397. /* control transfer in-data-phase */
  398. if (ctrlreq_in_data_phase(dev, len, buffer) < 0)
  399. return 0;
  400. /* control transfer out-status-phase */
  401. if (ctrlreq_out_status_phase(dev) < 0)
  402. return 0;
  403. break;
  404. case USB_REQ_SET_ADDRESS:
  405. case USB_REQ_SET_CONFIGURATION:
  406. case USB_REQ_SET_FEATURE:
  407. case USB_REQ_SET_INTERFACE:
  408. case USB_REQ_CLEAR_FEATURE:
  409. case USB_MSC_BBB_RESET:
  410. /* control transfer in status phase */
  411. if (ctrlreq_in_status_phase(dev) < 0)
  412. return 0;
  413. break;
  414. case USB_REQ_SET_DESCRIPTOR:
  415. /* control transfer out data phase */
  416. if (ctrlreq_out_data_phase(dev, len, buffer) < 0)
  417. return 0;
  418. /* control transfer in status phase */
  419. if (ctrlreq_in_status_phase(dev) < 0)
  420. return 0;
  421. break;
  422. default:
  423. /* unhandled control transfer */
  424. return -1;
  425. }
  426. dev->status = 0;
  427. dev->act_len = len;
  428. return len;
  429. }
  430. /*
  431. * do a bulk transfer
  432. */
  433. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  434. void *buffer, int len)
  435. {
  436. int dir_out = usb_pipeout(pipe);
  437. int ep = usb_pipeendpoint(pipe);
  438. int devnum = usb_pipedevice(pipe);
  439. u8 type;
  440. u16 csr;
  441. u32 txlen = 0;
  442. u32 nextlen = 0;
  443. u8 devspeed;
  444. /* select bulk endpoint */
  445. writeb(MUSB_BULK_EP, &musbr->index);
  446. /* write the address of the device */
  447. if (dir_out)
  448. writeb(devnum, &musbr->tar[MUSB_BULK_EP].txfuncaddr);
  449. else
  450. writeb(devnum, &musbr->tar[MUSB_BULK_EP].rxfuncaddr);
  451. /* configure the hub address and the port number as required */
  452. devspeed = get_dev_speed(dev);
  453. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  454. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  455. /*
  456. * MUSB is in high speed and the destination device is full
  457. * speed device. So configure the hub address and port
  458. * address registers.
  459. */
  460. config_hub_port(dev, MUSB_BULK_EP);
  461. } else {
  462. if (dir_out) {
  463. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubaddr);
  464. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubport);
  465. } else {
  466. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubaddr);
  467. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubport);
  468. }
  469. devspeed = musb_cfg.musb_speed;
  470. }
  471. /* Write the saved toggle bit value */
  472. write_toggle(dev, ep, dir_out);
  473. if (dir_out) { /* bulk-out transfer */
  474. /* Program the TxType register */
  475. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  476. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  477. (ep & MUSB_TYPE_REMOTE_END);
  478. writeb(type, &musbr->txtype);
  479. /* Write maximum packet size to the TxMaxp register */
  480. writew(dev->epmaxpacketout[ep], &musbr->txmaxp);
  481. while (txlen < len) {
  482. nextlen = ((len-txlen) < dev->epmaxpacketout[ep]) ?
  483. (len-txlen) : dev->epmaxpacketout[ep];
  484. /* Write the data to the FIFO */
  485. write_fifo(MUSB_BULK_EP, nextlen,
  486. (void *)(((u8 *)buffer) + txlen));
  487. /* Set the TxPktRdy bit */
  488. csr = readw(&musbr->txcsr);
  489. writew(csr | MUSB_TXCSR_TXPKTRDY, &musbr->txcsr);
  490. /* Wait until the TxPktRdy bit is cleared */
  491. if (!wait_until_txep_ready(dev, MUSB_BULK_EP)) {
  492. readw(&musbr->txcsr);
  493. usb_settoggle(dev, ep, dir_out,
  494. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  495. dev->act_len = txlen;
  496. return 0;
  497. }
  498. txlen += nextlen;
  499. }
  500. /* Keep a copy of the data toggle bit */
  501. csr = readw(&musbr->txcsr);
  502. usb_settoggle(dev, ep, dir_out,
  503. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  504. } else { /* bulk-in transfer */
  505. /* Write the saved toggle bit value */
  506. write_toggle(dev, ep, dir_out);
  507. /* Program the RxType register */
  508. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  509. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  510. (ep & MUSB_TYPE_REMOTE_END);
  511. writeb(type, &musbr->rxtype);
  512. /* Write the maximum packet size to the RxMaxp register */
  513. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  514. while (txlen < len) {
  515. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  516. (len-txlen) : dev->epmaxpacketin[ep];
  517. /* Set the ReqPkt bit */
  518. writew(MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  519. /* Wait until the RxPktRdy bit is set */
  520. if (!wait_until_rxep_ready(dev, MUSB_BULK_EP)) {
  521. csr = readw(&musbr->rxcsr);
  522. usb_settoggle(dev, ep, dir_out,
  523. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  524. csr &= ~MUSB_RXCSR_RXPKTRDY;
  525. writew(csr, &musbr->rxcsr);
  526. dev->act_len = txlen;
  527. return 0;
  528. }
  529. /* Read the data from the FIFO */
  530. read_fifo(MUSB_BULK_EP, nextlen,
  531. (void *)(((u8 *)buffer) + txlen));
  532. /* Clear the RxPktRdy bit */
  533. csr = readw(&musbr->rxcsr);
  534. csr &= ~MUSB_RXCSR_RXPKTRDY;
  535. writew(csr, &musbr->rxcsr);
  536. txlen += nextlen;
  537. }
  538. /* Keep a copy of the data toggle bit */
  539. csr = readw(&musbr->rxcsr);
  540. usb_settoggle(dev, ep, dir_out,
  541. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  542. }
  543. /* bulk transfer is complete */
  544. dev->status = 0;
  545. dev->act_len = len;
  546. return 0;
  547. }
  548. /*
  549. * This function initializes the usb controller module.
  550. */
  551. int usb_lowlevel_init(void)
  552. {
  553. u8 power;
  554. u32 timeout;
  555. if (musb_platform_init() == -1)
  556. return -1;
  557. /* Configure all the endpoint FIFO's and start usb controller */
  558. musbr = musb_cfg.regs;
  559. musb_configure_ep(&epinfo[0],
  560. sizeof(epinfo) / sizeof(struct musb_epinfo));
  561. musb_start();
  562. /*
  563. * Wait until musb is enabled in host mode with a timeout. There
  564. * should be a usb device connected.
  565. */
  566. timeout = musb_cfg.timeout;
  567. while (timeout--)
  568. if (readb(&musbr->devctl) & MUSB_DEVCTL_HM)
  569. break;
  570. /* if musb core is not in host mode, then return */
  571. if (!timeout)
  572. return -1;
  573. /* start usb bus reset */
  574. power = readb(&musbr->power);
  575. writeb(power | MUSB_POWER_RESET, &musbr->power);
  576. /* After initiating a usb reset, wait for about 20ms to 30ms */
  577. udelay(30000);
  578. /* stop usb bus reset */
  579. power = readb(&musbr->power);
  580. power &= ~MUSB_POWER_RESET;
  581. writeb(power, &musbr->power);
  582. /* Determine if the connected device is a high/full/low speed device */
  583. musb_cfg.musb_speed = (readb(&musbr->power) & MUSB_POWER_HSMODE) ?
  584. MUSB_TYPE_SPEED_HIGH :
  585. ((readb(&musbr->devctl) & MUSB_DEVCTL_FSDEV) ?
  586. MUSB_TYPE_SPEED_FULL : MUSB_TYPE_SPEED_LOW);
  587. return 0;
  588. }
  589. /*
  590. * This function stops the operation of the davinci usb module.
  591. */
  592. int usb_lowlevel_stop(void)
  593. {
  594. /* Reset the USB module */
  595. musb_platform_deinit();
  596. writeb(0, &musbr->devctl);
  597. return 0;
  598. }
  599. /*
  600. * This function supports usb interrupt transfers. Currently, usb interrupt
  601. * transfers are not supported.
  602. */
  603. int submit_int_msg(struct usb_device *dev, unsigned long pipe,
  604. void *buffer, int len, int interval)
  605. {
  606. int dir_out = usb_pipeout(pipe);
  607. int ep = usb_pipeendpoint(pipe);
  608. int devnum = usb_pipedevice(pipe);
  609. u8 type;
  610. u16 csr;
  611. u32 txlen = 0;
  612. u32 nextlen = 0;
  613. u8 devspeed;
  614. /* select interrupt endpoint */
  615. writeb(MUSB_INTR_EP, &musbr->index);
  616. /* write the address of the device */
  617. if (dir_out)
  618. writeb(devnum, &musbr->tar[MUSB_INTR_EP].txfuncaddr);
  619. else
  620. writeb(devnum, &musbr->tar[MUSB_INTR_EP].rxfuncaddr);
  621. /* configure the hub address and the port number as required */
  622. devspeed = get_dev_speed(dev);
  623. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  624. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  625. /*
  626. * MUSB is in high speed and the destination device is full
  627. * speed device. So configure the hub address and port
  628. * address registers.
  629. */
  630. config_hub_port(dev, MUSB_INTR_EP);
  631. } else {
  632. if (dir_out) {
  633. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubaddr);
  634. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubport);
  635. } else {
  636. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubaddr);
  637. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubport);
  638. }
  639. devspeed = musb_cfg.musb_speed;
  640. }
  641. /* Write the saved toggle bit value */
  642. write_toggle(dev, ep, dir_out);
  643. if (!dir_out) { /* intrrupt-in transfer */
  644. /* Write the saved toggle bit value */
  645. write_toggle(dev, ep, dir_out);
  646. writeb(interval, &musbr->rxinterval);
  647. /* Program the RxType register */
  648. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  649. (MUSB_TYPE_PROTO_INTR << MUSB_TYPE_PROTO_SHIFT) |
  650. (ep & MUSB_TYPE_REMOTE_END);
  651. writeb(type, &musbr->rxtype);
  652. /* Write the maximum packet size to the RxMaxp register */
  653. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  654. while (txlen < len) {
  655. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  656. (len-txlen) : dev->epmaxpacketin[ep];
  657. /* Set the ReqPkt bit */
  658. writew(MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  659. /* Wait until the RxPktRdy bit is set */
  660. if (!wait_until_rxep_ready(dev, MUSB_INTR_EP)) {
  661. csr = readw(&musbr->rxcsr);
  662. usb_settoggle(dev, ep, dir_out,
  663. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  664. csr &= ~MUSB_RXCSR_RXPKTRDY;
  665. writew(csr, &musbr->rxcsr);
  666. dev->act_len = txlen;
  667. return 0;
  668. }
  669. /* Read the data from the FIFO */
  670. read_fifo(MUSB_INTR_EP, nextlen,
  671. (void *)(((u8 *)buffer) + txlen));
  672. /* Clear the RxPktRdy bit */
  673. csr = readw(&musbr->rxcsr);
  674. csr &= ~MUSB_RXCSR_RXPKTRDY;
  675. writew(csr, &musbr->rxcsr);
  676. txlen += nextlen;
  677. }
  678. /* Keep a copy of the data toggle bit */
  679. csr = readw(&musbr->rxcsr);
  680. usb_settoggle(dev, ep, dir_out,
  681. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  682. }
  683. /* interrupt transfer is complete */
  684. dev->irq_status = 0;
  685. dev->irq_act_len = len;
  686. dev->irq_handle(dev);
  687. dev->status = 0;
  688. dev->act_len = len;
  689. return 0;
  690. }
  691. #ifdef CONFIG_SYS_USB_EVENT_POLL
  692. /*
  693. * This function polls for USB keyboard data.
  694. */
  695. void usb_event_poll()
  696. {
  697. device_t *dev;
  698. struct usb_device *usb_kbd_dev;
  699. struct usb_interface_descriptor *iface;
  700. struct usb_endpoint_descriptor *ep;
  701. int pipe;
  702. int maxp;
  703. /* Get the pointer to USB Keyboard device pointer */
  704. dev = device_get_by_name("usbkbd");
  705. usb_kbd_dev = (struct usb_device *)dev->priv;
  706. iface = &usb_kbd_dev->config.if_desc[0];
  707. ep = &iface->ep_desc[0];
  708. pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
  709. /* Submit a interrupt transfer request */
  710. maxp = usb_maxpacket(usb_kbd_dev, pipe);
  711. usb_submit_int_msg(usb_kbd_dev, pipe, &new[0],
  712. maxp > 8 ? 8 : maxp, ep->bInterval);
  713. }
  714. #endif /* CONFIG_SYS_USB_EVENT_POLL */