musb_hcd.c 27 KB

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