musb_hcd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. while (result > 0) {
  107. csr = readw(&musbr->txcsr);
  108. if (csr & MUSB_CSR0_H_ERROR) {
  109. csr &= ~MUSB_CSR0_H_ERROR;
  110. writew(csr, &musbr->txcsr);
  111. dev->status = USB_ST_CRC_ERR;
  112. result = -1;
  113. break;
  114. }
  115. switch (bit_mask) {
  116. case MUSB_CSR0_TXPKTRDY:
  117. if (!(csr & MUSB_CSR0_TXPKTRDY)) {
  118. if (check_stall(MUSB_CONTROL_EP, 0)) {
  119. dev->status = USB_ST_STALLED;
  120. result = -2;
  121. } else
  122. result = 0;
  123. }
  124. break;
  125. case MUSB_CSR0_RXPKTRDY:
  126. if (check_stall(MUSB_CONTROL_EP, 0)) {
  127. dev->status = USB_ST_STALLED;
  128. result = -2;
  129. } else
  130. if (csr & MUSB_CSR0_RXPKTRDY)
  131. result = 0;
  132. break;
  133. case MUSB_CSR0_H_REQPKT:
  134. if (!(csr & MUSB_CSR0_H_REQPKT)) {
  135. if (check_stall(MUSB_CONTROL_EP, 0)) {
  136. dev->status = USB_ST_STALLED;
  137. result = -2;
  138. } else
  139. result = 0;
  140. }
  141. break;
  142. }
  143. }
  144. return result;
  145. }
  146. /*
  147. * waits until tx ep is ready. Returns 1 when ep is ready and 0 on error.
  148. */
  149. static u8 wait_until_txep_ready(struct usb_device *dev, u8 ep)
  150. {
  151. u16 csr;
  152. do {
  153. if (check_stall(ep, 1)) {
  154. dev->status = USB_ST_STALLED;
  155. return 0;
  156. }
  157. csr = readw(&musbr->txcsr);
  158. if (csr & MUSB_TXCSR_H_ERROR) {
  159. dev->status = USB_ST_CRC_ERR;
  160. return 0;
  161. }
  162. } while (csr & MUSB_TXCSR_TXPKTRDY);
  163. return 1;
  164. }
  165. /*
  166. * waits until rx ep is ready. Returns 1 when ep is ready and 0 on error.
  167. */
  168. static u8 wait_until_rxep_ready(struct usb_device *dev, u8 ep)
  169. {
  170. u16 csr;
  171. do {
  172. if (check_stall(ep, 0)) {
  173. dev->status = USB_ST_STALLED;
  174. return 0;
  175. }
  176. csr = readw(&musbr->rxcsr);
  177. if (csr & MUSB_RXCSR_H_ERROR) {
  178. dev->status = USB_ST_CRC_ERR;
  179. return 0;
  180. }
  181. } while (!(csr & MUSB_RXCSR_RXPKTRDY));
  182. return 1;
  183. }
  184. /*
  185. * This function performs the setup phase of the control transfer
  186. */
  187. static int ctrlreq_setup_phase(struct usb_device *dev, struct devrequest *setup)
  188. {
  189. int result;
  190. u16 csr;
  191. /* write the control request to ep0 fifo */
  192. write_fifo(MUSB_CONTROL_EP, sizeof(struct devrequest), (void *)setup);
  193. /* enable transfer of setup packet */
  194. csr = readw(&musbr->txcsr);
  195. csr |= (MUSB_CSR0_TXPKTRDY|MUSB_CSR0_H_SETUPPKT);
  196. writew(csr, &musbr->txcsr);
  197. /* wait until the setup packet is transmitted */
  198. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  199. dev->act_len = 0;
  200. return result;
  201. }
  202. /*
  203. * This function handles the control transfer in data phase
  204. */
  205. static int ctrlreq_in_data_phase(struct usb_device *dev, u32 len, void *buffer)
  206. {
  207. u16 csr;
  208. u32 rxlen = 0;
  209. u32 nextlen = 0;
  210. u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
  211. u8 *rxbuff = (u8 *)buffer;
  212. u8 rxedlength;
  213. int result;
  214. while (rxlen < len) {
  215. /* Determine the next read length */
  216. nextlen = ((len-rxlen) > maxpktsize) ? maxpktsize : (len-rxlen);
  217. /* Set the ReqPkt bit */
  218. csr = readw(&musbr->txcsr);
  219. writew(csr | MUSB_CSR0_H_REQPKT, &musbr->txcsr);
  220. result = wait_until_ep0_ready(dev, MUSB_CSR0_RXPKTRDY);
  221. if (result < 0)
  222. return result;
  223. /* Actual number of bytes received by usb */
  224. rxedlength = readb(&musbr->rxcount);
  225. /* Read the data from the RxFIFO */
  226. read_fifo(MUSB_CONTROL_EP, rxedlength, &rxbuff[rxlen]);
  227. /* Clear the RxPktRdy Bit */
  228. csr = readw(&musbr->txcsr);
  229. csr &= ~MUSB_CSR0_RXPKTRDY;
  230. writew(csr, &musbr->txcsr);
  231. /* short packet? */
  232. if (rxedlength != nextlen) {
  233. dev->act_len += rxedlength;
  234. break;
  235. }
  236. rxlen += nextlen;
  237. dev->act_len = rxlen;
  238. }
  239. return 0;
  240. }
  241. /*
  242. * This function handles the control transfer out data phase
  243. */
  244. static int ctrlreq_out_data_phase(struct usb_device *dev, u32 len, void *buffer)
  245. {
  246. u16 csr;
  247. u32 txlen = 0;
  248. u32 nextlen = 0;
  249. u8 maxpktsize = (1 << dev->maxpacketsize) * 8;
  250. u8 *txbuff = (u8 *)buffer;
  251. int result = 0;
  252. while (txlen < len) {
  253. /* Determine the next write length */
  254. nextlen = ((len-txlen) > maxpktsize) ? maxpktsize : (len-txlen);
  255. /* Load the data to send in FIFO */
  256. write_fifo(MUSB_CONTROL_EP, txlen, &txbuff[txlen]);
  257. /* Set TXPKTRDY bit */
  258. csr = readw(&musbr->txcsr);
  259. writew(csr | MUSB_CSR0_H_DIS_PING | MUSB_CSR0_TXPKTRDY,
  260. &musbr->txcsr);
  261. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  262. if (result < 0)
  263. break;
  264. txlen += nextlen;
  265. dev->act_len = txlen;
  266. }
  267. return result;
  268. }
  269. /*
  270. * This function handles the control transfer out status phase
  271. */
  272. static int ctrlreq_out_status_phase(struct usb_device *dev)
  273. {
  274. u16 csr;
  275. int result;
  276. /* Set the StatusPkt bit */
  277. csr = readw(&musbr->txcsr);
  278. csr |= (MUSB_CSR0_H_DIS_PING | MUSB_CSR0_TXPKTRDY |
  279. MUSB_CSR0_H_STATUSPKT);
  280. writew(csr, &musbr->txcsr);
  281. /* Wait until TXPKTRDY bit is cleared */
  282. result = wait_until_ep0_ready(dev, MUSB_CSR0_TXPKTRDY);
  283. return result;
  284. }
  285. /*
  286. * This function handles the control transfer in status phase
  287. */
  288. static int ctrlreq_in_status_phase(struct usb_device *dev)
  289. {
  290. u16 csr;
  291. int result;
  292. /* Set the StatusPkt bit and ReqPkt bit */
  293. csr = MUSB_CSR0_H_DIS_PING | MUSB_CSR0_H_REQPKT | MUSB_CSR0_H_STATUSPKT;
  294. writew(csr, &musbr->txcsr);
  295. result = wait_until_ep0_ready(dev, MUSB_CSR0_H_REQPKT);
  296. /* clear StatusPkt bit and RxPktRdy bit */
  297. csr = readw(&musbr->txcsr);
  298. csr &= ~(MUSB_CSR0_RXPKTRDY | MUSB_CSR0_H_STATUSPKT);
  299. writew(csr, &musbr->txcsr);
  300. return result;
  301. }
  302. /*
  303. * determines the speed of the device (High/Full/Slow)
  304. */
  305. static u8 get_dev_speed(struct usb_device *dev)
  306. {
  307. return (dev->speed & USB_SPEED_HIGH) ? MUSB_TYPE_SPEED_HIGH :
  308. ((dev->speed & USB_SPEED_LOW) ? MUSB_TYPE_SPEED_LOW :
  309. MUSB_TYPE_SPEED_FULL);
  310. }
  311. /*
  312. * configure the hub address and the port address.
  313. */
  314. static void config_hub_port(struct usb_device *dev, u8 ep)
  315. {
  316. u8 chid;
  317. u8 hub;
  318. /* Find out the nearest parent which is high speed */
  319. while (dev->parent->parent != NULL)
  320. if (get_dev_speed(dev->parent) != MUSB_TYPE_SPEED_HIGH)
  321. dev = dev->parent;
  322. else
  323. break;
  324. /* determine the port address at that hub */
  325. hub = dev->parent->devnum;
  326. for (chid = 0; chid < USB_MAXCHILDREN; chid++)
  327. if (dev->parent->children[chid] == dev)
  328. break;
  329. /* configure the hub address and the port address */
  330. writeb(hub, &musbr->tar[ep].txhubaddr);
  331. writeb((chid + 1), &musbr->tar[ep].txhubport);
  332. writeb(hub, &musbr->tar[ep].rxhubaddr);
  333. writeb((chid + 1), &musbr->tar[ep].rxhubport);
  334. }
  335. /*
  336. * do a control transfer
  337. */
  338. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  339. int len, struct devrequest *setup)
  340. {
  341. int devnum = usb_pipedevice(pipe);
  342. u16 csr;
  343. u8 devspeed;
  344. /* select control endpoint */
  345. writeb(MUSB_CONTROL_EP, &musbr->index);
  346. csr = readw(&musbr->txcsr);
  347. /* target addr and (for multipoint) hub addr/port */
  348. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].txfuncaddr);
  349. writeb(devnum, &musbr->tar[MUSB_CONTROL_EP].rxfuncaddr);
  350. /* configure the hub address and the port number as required */
  351. devspeed = get_dev_speed(dev);
  352. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  353. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  354. config_hub_port(dev, MUSB_CONTROL_EP);
  355. writeb(devspeed << 6, &musbr->txtype);
  356. } else {
  357. writeb(musb_cfg.musb_speed << 6, &musbr->txtype);
  358. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubaddr);
  359. writeb(0, &musbr->tar[MUSB_CONTROL_EP].txhubport);
  360. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubaddr);
  361. writeb(0, &musbr->tar[MUSB_CONTROL_EP].rxhubport);
  362. }
  363. /* Control transfer setup phase */
  364. if (ctrlreq_setup_phase(dev, setup) < 0)
  365. return 0;
  366. switch (setup->request) {
  367. case USB_REQ_GET_DESCRIPTOR:
  368. case USB_REQ_GET_CONFIGURATION:
  369. case USB_REQ_GET_INTERFACE:
  370. case USB_REQ_GET_STATUS:
  371. case USB_MSC_BBB_GET_MAX_LUN:
  372. /* control transfer in-data-phase */
  373. if (ctrlreq_in_data_phase(dev, len, buffer) < 0)
  374. return 0;
  375. /* control transfer out-status-phase */
  376. if (ctrlreq_out_status_phase(dev) < 0)
  377. return 0;
  378. break;
  379. case USB_REQ_SET_ADDRESS:
  380. case USB_REQ_SET_CONFIGURATION:
  381. case USB_REQ_SET_FEATURE:
  382. case USB_REQ_SET_INTERFACE:
  383. case USB_REQ_CLEAR_FEATURE:
  384. case USB_MSC_BBB_RESET:
  385. /* control transfer in status phase */
  386. if (ctrlreq_in_status_phase(dev) < 0)
  387. return 0;
  388. break;
  389. case USB_REQ_SET_DESCRIPTOR:
  390. /* control transfer out data phase */
  391. if (ctrlreq_out_data_phase(dev, len, buffer) < 0)
  392. return 0;
  393. /* control transfer in status phase */
  394. if (ctrlreq_in_status_phase(dev) < 0)
  395. return 0;
  396. break;
  397. default:
  398. /* unhandled control transfer */
  399. return -1;
  400. }
  401. dev->status = 0;
  402. dev->act_len = len;
  403. return len;
  404. }
  405. /*
  406. * do a bulk transfer
  407. */
  408. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
  409. void *buffer, int len)
  410. {
  411. int dir_out = usb_pipeout(pipe);
  412. int ep = usb_pipeendpoint(pipe);
  413. int devnum = usb_pipedevice(pipe);
  414. u8 type;
  415. u16 csr;
  416. u32 txlen = 0;
  417. u32 nextlen = 0;
  418. u8 devspeed;
  419. /* select bulk endpoint */
  420. writeb(MUSB_BULK_EP, &musbr->index);
  421. /* write the address of the device */
  422. if (dir_out)
  423. writeb(devnum, &musbr->tar[MUSB_BULK_EP].txfuncaddr);
  424. else
  425. writeb(devnum, &musbr->tar[MUSB_BULK_EP].rxfuncaddr);
  426. /* configure the hub address and the port number as required */
  427. devspeed = get_dev_speed(dev);
  428. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  429. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  430. /*
  431. * MUSB is in high speed and the destination device is full
  432. * speed device. So configure the hub address and port
  433. * address registers.
  434. */
  435. config_hub_port(dev, MUSB_BULK_EP);
  436. } else {
  437. if (dir_out) {
  438. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubaddr);
  439. writeb(0, &musbr->tar[MUSB_BULK_EP].txhubport);
  440. } else {
  441. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubaddr);
  442. writeb(0, &musbr->tar[MUSB_BULK_EP].rxhubport);
  443. }
  444. devspeed = musb_cfg.musb_speed;
  445. }
  446. /* Write the saved toggle bit value */
  447. write_toggle(dev, ep, dir_out);
  448. if (dir_out) { /* bulk-out transfer */
  449. /* Program the TxType register */
  450. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  451. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  452. (ep & MUSB_TYPE_REMOTE_END);
  453. writeb(type, &musbr->txtype);
  454. /* Write maximum packet size to the TxMaxp register */
  455. writew(dev->epmaxpacketout[ep], &musbr->txmaxp);
  456. while (txlen < len) {
  457. nextlen = ((len-txlen) < dev->epmaxpacketout[ep]) ?
  458. (len-txlen) : dev->epmaxpacketout[ep];
  459. /* Write the data to the FIFO */
  460. write_fifo(MUSB_BULK_EP, nextlen,
  461. (void *)(((u8 *)buffer) + txlen));
  462. /* Set the TxPktRdy bit */
  463. csr = readw(&musbr->txcsr);
  464. writew(csr | MUSB_TXCSR_TXPKTRDY, &musbr->txcsr);
  465. /* Wait until the TxPktRdy bit is cleared */
  466. if (!wait_until_txep_ready(dev, MUSB_BULK_EP)) {
  467. readw(&musbr->txcsr);
  468. usb_settoggle(dev, ep, dir_out,
  469. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  470. dev->act_len = txlen;
  471. return 0;
  472. }
  473. txlen += nextlen;
  474. }
  475. /* Keep a copy of the data toggle bit */
  476. csr = readw(&musbr->txcsr);
  477. usb_settoggle(dev, ep, dir_out,
  478. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  479. } else { /* bulk-in transfer */
  480. /* Write the saved toggle bit value */
  481. write_toggle(dev, ep, dir_out);
  482. /* Program the RxType register */
  483. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  484. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  485. (ep & MUSB_TYPE_REMOTE_END);
  486. writeb(type, &musbr->rxtype);
  487. /* Write the maximum packet size to the RxMaxp register */
  488. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  489. while (txlen < len) {
  490. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  491. (len-txlen) : dev->epmaxpacketin[ep];
  492. /* Set the ReqPkt bit */
  493. writew(MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  494. /* Wait until the RxPktRdy bit is set */
  495. if (!wait_until_rxep_ready(dev, MUSB_BULK_EP)) {
  496. csr = readw(&musbr->rxcsr);
  497. usb_settoggle(dev, ep, dir_out,
  498. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  499. csr &= ~MUSB_RXCSR_RXPKTRDY;
  500. writew(csr, &musbr->rxcsr);
  501. dev->act_len = txlen;
  502. return 0;
  503. }
  504. /* Read the data from the FIFO */
  505. read_fifo(MUSB_BULK_EP, nextlen,
  506. (void *)(((u8 *)buffer) + txlen));
  507. /* Clear the RxPktRdy bit */
  508. csr = readw(&musbr->rxcsr);
  509. csr &= ~MUSB_RXCSR_RXPKTRDY;
  510. writew(csr, &musbr->rxcsr);
  511. txlen += nextlen;
  512. }
  513. /* Keep a copy of the data toggle bit */
  514. csr = readw(&musbr->rxcsr);
  515. usb_settoggle(dev, ep, dir_out,
  516. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  517. }
  518. /* bulk transfer is complete */
  519. dev->status = 0;
  520. dev->act_len = len;
  521. return 0;
  522. }
  523. /*
  524. * This function initializes the usb controller module.
  525. */
  526. int usb_lowlevel_init(void)
  527. {
  528. u8 power;
  529. u32 timeout;
  530. if (musb_platform_init() == -1)
  531. return -1;
  532. /* Configure all the endpoint FIFO's and start usb controller */
  533. musbr = musb_cfg.regs;
  534. musb_configure_ep(&epinfo[0],
  535. sizeof(epinfo) / sizeof(struct musb_epinfo));
  536. musb_start();
  537. /*
  538. * Wait until musb is enabled in host mode with a timeout. There
  539. * should be a usb device connected.
  540. */
  541. timeout = musb_cfg.timeout;
  542. while (timeout--)
  543. if (readb(&musbr->devctl) & MUSB_DEVCTL_HM)
  544. break;
  545. /* if musb core is not in host mode, then return */
  546. if (!timeout)
  547. return -1;
  548. /* start usb bus reset */
  549. power = readb(&musbr->power);
  550. writeb(power | MUSB_POWER_RESET, &musbr->power);
  551. /* After initiating a usb reset, wait for about 20ms to 30ms */
  552. udelay(30000);
  553. /* stop usb bus reset */
  554. power = readb(&musbr->power);
  555. power &= ~MUSB_POWER_RESET;
  556. writeb(power, &musbr->power);
  557. /* Determine if the connected device is a high/full/low speed device */
  558. musb_cfg.musb_speed = (readb(&musbr->power) & MUSB_POWER_HSMODE) ?
  559. MUSB_TYPE_SPEED_HIGH :
  560. ((readb(&musbr->devctl) & MUSB_DEVCTL_FSDEV) ?
  561. MUSB_TYPE_SPEED_FULL : MUSB_TYPE_SPEED_LOW);
  562. return 0;
  563. }
  564. /*
  565. * This function stops the operation of the davinci usb module.
  566. */
  567. int usb_lowlevel_stop(void)
  568. {
  569. /* Reset the USB module */
  570. musb_platform_deinit();
  571. writeb(0, &musbr->devctl);
  572. return 0;
  573. }
  574. /*
  575. * This function supports usb interrupt transfers. Currently, usb interrupt
  576. * transfers are not supported.
  577. */
  578. int submit_int_msg(struct usb_device *dev, unsigned long pipe,
  579. void *buffer, int len, int interval)
  580. {
  581. int dir_out = usb_pipeout(pipe);
  582. int ep = usb_pipeendpoint(pipe);
  583. int devnum = usb_pipedevice(pipe);
  584. u8 type;
  585. u16 csr;
  586. u32 txlen = 0;
  587. u32 nextlen = 0;
  588. u8 devspeed;
  589. /* select interrupt endpoint */
  590. writeb(MUSB_INTR_EP, &musbr->index);
  591. /* write the address of the device */
  592. if (dir_out)
  593. writeb(devnum, &musbr->tar[MUSB_INTR_EP].txfuncaddr);
  594. else
  595. writeb(devnum, &musbr->tar[MUSB_INTR_EP].rxfuncaddr);
  596. /* configure the hub address and the port number as required */
  597. devspeed = get_dev_speed(dev);
  598. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  599. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  600. /*
  601. * MUSB is in high speed and the destination device is full
  602. * speed device. So configure the hub address and port
  603. * address registers.
  604. */
  605. config_hub_port(dev, MUSB_INTR_EP);
  606. } else {
  607. if (dir_out) {
  608. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubaddr);
  609. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubport);
  610. } else {
  611. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubaddr);
  612. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubport);
  613. }
  614. devspeed = musb_cfg.musb_speed;
  615. }
  616. /* Write the saved toggle bit value */
  617. write_toggle(dev, ep, dir_out);
  618. if (!dir_out) { /* intrrupt-in transfer */
  619. /* Write the saved toggle bit value */
  620. write_toggle(dev, ep, dir_out);
  621. writeb(interval, &musbr->rxinterval);
  622. /* Program the RxType register */
  623. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  624. (MUSB_TYPE_PROTO_INTR << MUSB_TYPE_PROTO_SHIFT) |
  625. (ep & MUSB_TYPE_REMOTE_END);
  626. writeb(type, &musbr->rxtype);
  627. /* Write the maximum packet size to the RxMaxp register */
  628. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  629. while (txlen < len) {
  630. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  631. (len-txlen) : dev->epmaxpacketin[ep];
  632. /* Set the ReqPkt bit */
  633. writew(MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  634. /* Wait until the RxPktRdy bit is set */
  635. if (!wait_until_rxep_ready(dev, MUSB_INTR_EP)) {
  636. csr = readw(&musbr->rxcsr);
  637. usb_settoggle(dev, ep, dir_out,
  638. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  639. csr &= ~MUSB_RXCSR_RXPKTRDY;
  640. writew(csr, &musbr->rxcsr);
  641. dev->act_len = txlen;
  642. return 0;
  643. }
  644. /* Read the data from the FIFO */
  645. read_fifo(MUSB_INTR_EP, nextlen,
  646. (void *)(((u8 *)buffer) + txlen));
  647. /* Clear the RxPktRdy bit */
  648. csr = readw(&musbr->rxcsr);
  649. csr &= ~MUSB_RXCSR_RXPKTRDY;
  650. writew(csr, &musbr->rxcsr);
  651. txlen += nextlen;
  652. }
  653. /* Keep a copy of the data toggle bit */
  654. csr = readw(&musbr->rxcsr);
  655. usb_settoggle(dev, ep, dir_out,
  656. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  657. }
  658. /* interrupt transfer is complete */
  659. dev->irq_status = 0;
  660. dev->irq_act_len = len;
  661. dev->irq_handle(dev);
  662. dev->status = 0;
  663. dev->act_len = len;
  664. return 0;
  665. }
  666. #ifdef CONFIG_SYS_USB_EVENT_POLL
  667. /*
  668. * This function polls for USB keyboard data.
  669. */
  670. void usb_event_poll()
  671. {
  672. device_t *dev;
  673. struct usb_device *usb_kbd_dev;
  674. struct usb_interface_descriptor *iface;
  675. struct usb_endpoint_descriptor *ep;
  676. int pipe;
  677. int maxp;
  678. /* Get the pointer to USB Keyboard device pointer */
  679. dev = device_get_by_name("usbkbd");
  680. usb_kbd_dev = (struct usb_device *)dev->priv;
  681. iface = &usb_kbd_dev->config.if_desc[0];
  682. ep = &iface->ep_desc[0];
  683. pipe = usb_rcvintpipe(usb_kbd_dev, ep->bEndpointAddress);
  684. /* Submit a interrupt transfer request */
  685. maxp = usb_maxpacket(usb_kbd_dev, pipe);
  686. usb_submit_int_msg(usb_kbd_dev, pipe, &new[0],
  687. maxp > 8 ? 8 : maxp, ep->bInterval);
  688. }
  689. #endif /* CONFIG_SYS_USB_EVENT_POLL */