musb_hcd.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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. #ifdef CONFIG_USB_BLACKFIN
  772. /* Set the transfer data size */
  773. writew(nextlen, &musbr->txcount);
  774. #endif
  775. /* Write the data to the FIFO */
  776. write_fifo(MUSB_BULK_EP, nextlen,
  777. (void *)(((u8 *)buffer) + txlen));
  778. /* Set the TxPktRdy bit */
  779. csr = readw(&musbr->txcsr);
  780. writew(csr | MUSB_TXCSR_TXPKTRDY, &musbr->txcsr);
  781. /* Wait until the TxPktRdy bit is cleared */
  782. if (wait_until_txep_ready(dev, MUSB_BULK_EP) != 1) {
  783. readw(&musbr->txcsr);
  784. usb_settoggle(dev, ep, dir_out,
  785. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  786. dev->act_len = txlen;
  787. return 0;
  788. }
  789. txlen += nextlen;
  790. }
  791. /* Keep a copy of the data toggle bit */
  792. csr = readw(&musbr->txcsr);
  793. usb_settoggle(dev, ep, dir_out,
  794. (csr >> MUSB_TXCSR_H_DATATOGGLE_SHIFT) & 1);
  795. } else { /* bulk-in transfer */
  796. /* Write the saved toggle bit value */
  797. write_toggle(dev, ep, dir_out);
  798. /* Program the RxType register */
  799. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  800. (MUSB_TYPE_PROTO_BULK << MUSB_TYPE_PROTO_SHIFT) |
  801. (ep & MUSB_TYPE_REMOTE_END);
  802. writeb(type, &musbr->rxtype);
  803. /* Write the maximum packet size to the RxMaxp register */
  804. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  805. while (txlen < len) {
  806. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  807. (len-txlen) : dev->epmaxpacketin[ep];
  808. /* Set the ReqPkt bit */
  809. csr = readw(&musbr->rxcsr);
  810. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  811. /* Wait until the RxPktRdy bit is set */
  812. if (wait_until_rxep_ready(dev, MUSB_BULK_EP) != 1) {
  813. csr = readw(&musbr->rxcsr);
  814. usb_settoggle(dev, ep, dir_out,
  815. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  816. csr &= ~MUSB_RXCSR_RXPKTRDY;
  817. writew(csr, &musbr->rxcsr);
  818. dev->act_len = txlen;
  819. return 0;
  820. }
  821. /* Read the data from the FIFO */
  822. read_fifo(MUSB_BULK_EP, nextlen,
  823. (void *)(((u8 *)buffer) + txlen));
  824. /* Clear the RxPktRdy bit */
  825. csr = readw(&musbr->rxcsr);
  826. csr &= ~MUSB_RXCSR_RXPKTRDY;
  827. writew(csr, &musbr->rxcsr);
  828. txlen += nextlen;
  829. }
  830. /* Keep a copy of the data toggle bit */
  831. csr = readw(&musbr->rxcsr);
  832. usb_settoggle(dev, ep, dir_out,
  833. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  834. }
  835. /* bulk transfer is complete */
  836. dev->status = 0;
  837. dev->act_len = len;
  838. return 0;
  839. }
  840. /*
  841. * This function initializes the usb controller module.
  842. */
  843. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  844. {
  845. u8 power;
  846. u32 timeout;
  847. musb_rh_init();
  848. if (musb_platform_init() == -1)
  849. return -1;
  850. /* Configure all the endpoint FIFO's and start usb controller */
  851. musbr = musb_cfg.regs;
  852. musb_configure_ep(&epinfo[0], ARRAY_SIZE(epinfo));
  853. musb_start();
  854. /*
  855. * Wait until musb is enabled in host mode with a timeout. There
  856. * should be a usb device connected.
  857. */
  858. timeout = musb_cfg.timeout;
  859. while (--timeout)
  860. if (readb(&musbr->devctl) & MUSB_DEVCTL_HM)
  861. break;
  862. /* if musb core is not in host mode, then return */
  863. if (!timeout)
  864. return -1;
  865. /* start usb bus reset */
  866. power = readb(&musbr->power);
  867. writeb(power | MUSB_POWER_RESET, &musbr->power);
  868. /* After initiating a usb reset, wait for about 20ms to 30ms */
  869. udelay(30000);
  870. /* stop usb bus reset */
  871. power = readb(&musbr->power);
  872. power &= ~MUSB_POWER_RESET;
  873. writeb(power, &musbr->power);
  874. /* Determine if the connected device is a high/full/low speed device */
  875. musb_cfg.musb_speed = (readb(&musbr->power) & MUSB_POWER_HSMODE) ?
  876. MUSB_TYPE_SPEED_HIGH :
  877. ((readb(&musbr->devctl) & MUSB_DEVCTL_FSDEV) ?
  878. MUSB_TYPE_SPEED_FULL : MUSB_TYPE_SPEED_LOW);
  879. return 0;
  880. }
  881. /*
  882. * This function stops the operation of the davinci usb module.
  883. */
  884. int usb_lowlevel_stop(int index)
  885. {
  886. /* Reset the USB module */
  887. musb_platform_deinit();
  888. writeb(0, &musbr->devctl);
  889. return 0;
  890. }
  891. /*
  892. * This function supports usb interrupt transfers. Currently, usb interrupt
  893. * transfers are not supported.
  894. */
  895. int submit_int_msg(struct usb_device *dev, unsigned long pipe,
  896. void *buffer, int len, int interval)
  897. {
  898. int dir_out = usb_pipeout(pipe);
  899. int ep = usb_pipeendpoint(pipe);
  900. #ifndef MUSB_NO_MULTIPOINT
  901. int devnum = usb_pipedevice(pipe);
  902. #endif
  903. u8 type;
  904. u16 csr;
  905. u32 txlen = 0;
  906. u32 nextlen = 0;
  907. u8 devspeed;
  908. /* select interrupt endpoint */
  909. writeb(MUSB_INTR_EP, &musbr->index);
  910. #ifndef MUSB_NO_MULTIPOINT
  911. /* write the address of the device */
  912. if (dir_out)
  913. writeb(devnum, &musbr->tar[MUSB_INTR_EP].txfuncaddr);
  914. else
  915. writeb(devnum, &musbr->tar[MUSB_INTR_EP].rxfuncaddr);
  916. #endif
  917. /* configure the hub address and the port number as required */
  918. devspeed = get_dev_speed(dev);
  919. if ((musb_ishighspeed()) && (dev->parent != NULL) &&
  920. (devspeed != MUSB_TYPE_SPEED_HIGH)) {
  921. /*
  922. * MUSB is in high speed and the destination device is full
  923. * speed device. So configure the hub address and port
  924. * address registers.
  925. */
  926. config_hub_port(dev, MUSB_INTR_EP);
  927. } else {
  928. #ifndef MUSB_NO_MULTIPOINT
  929. if (dir_out) {
  930. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubaddr);
  931. writeb(0, &musbr->tar[MUSB_INTR_EP].txhubport);
  932. } else {
  933. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubaddr);
  934. writeb(0, &musbr->tar[MUSB_INTR_EP].rxhubport);
  935. }
  936. #endif
  937. devspeed = musb_cfg.musb_speed;
  938. }
  939. /* Write the saved toggle bit value */
  940. write_toggle(dev, ep, dir_out);
  941. if (!dir_out) { /* intrrupt-in transfer */
  942. /* Write the saved toggle bit value */
  943. write_toggle(dev, ep, dir_out);
  944. writeb(interval, &musbr->rxinterval);
  945. /* Program the RxType register */
  946. type = (devspeed << MUSB_TYPE_SPEED_SHIFT) |
  947. (MUSB_TYPE_PROTO_INTR << MUSB_TYPE_PROTO_SHIFT) |
  948. (ep & MUSB_TYPE_REMOTE_END);
  949. writeb(type, &musbr->rxtype);
  950. /* Write the maximum packet size to the RxMaxp register */
  951. writew(dev->epmaxpacketin[ep], &musbr->rxmaxp);
  952. while (txlen < len) {
  953. nextlen = ((len-txlen) < dev->epmaxpacketin[ep]) ?
  954. (len-txlen) : dev->epmaxpacketin[ep];
  955. /* Set the ReqPkt bit */
  956. csr = readw(&musbr->rxcsr);
  957. writew(csr | MUSB_RXCSR_H_REQPKT, &musbr->rxcsr);
  958. /* Wait until the RxPktRdy bit is set */
  959. if (wait_until_rxep_ready(dev, MUSB_INTR_EP) != 1) {
  960. csr = readw(&musbr->rxcsr);
  961. usb_settoggle(dev, ep, dir_out,
  962. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  963. csr &= ~MUSB_RXCSR_RXPKTRDY;
  964. writew(csr, &musbr->rxcsr);
  965. dev->act_len = txlen;
  966. return 0;
  967. }
  968. /* Read the data from the FIFO */
  969. read_fifo(MUSB_INTR_EP, nextlen,
  970. (void *)(((u8 *)buffer) + txlen));
  971. /* Clear the RxPktRdy bit */
  972. csr = readw(&musbr->rxcsr);
  973. csr &= ~MUSB_RXCSR_RXPKTRDY;
  974. writew(csr, &musbr->rxcsr);
  975. txlen += nextlen;
  976. }
  977. /* Keep a copy of the data toggle bit */
  978. csr = readw(&musbr->rxcsr);
  979. usb_settoggle(dev, ep, dir_out,
  980. (csr >> MUSB_S_RXCSR_H_DATATOGGLE) & 1);
  981. }
  982. /* interrupt transfer is complete */
  983. dev->irq_status = 0;
  984. dev->irq_act_len = len;
  985. dev->irq_handle(dev);
  986. dev->status = 0;
  987. dev->act_len = len;
  988. return 0;
  989. }