musb_udc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2009 Wind River Systems, Inc.
  4. * Tom Rix <Tom.Rix@windriver.com>
  5. *
  6. * This file is a rewrite of the usb device part of
  7. * repository git.omapzoom.org/repo/u-boot.git, branch master,
  8. * file cpu/omap3/fastboot.c
  9. *
  10. * This is the unique part of its copyright :
  11. *
  12. * -------------------------------------------------------------------------
  13. *
  14. * (C) Copyright 2008 - 2009
  15. * Windriver, <www.windriver.com>
  16. * Tom Rix <Tom.Rix@windriver.com>
  17. *
  18. * -------------------------------------------------------------------------
  19. *
  20. * The details of connecting the device to the uboot usb device subsystem
  21. * came from the old omap3 repository www.sakoman.net/u-boot-omap3.git,
  22. * branch omap3-dev-usb, file drivers/usb/usbdcore_musb.c
  23. *
  24. * This is the unique part of its copyright :
  25. *
  26. * -------------------------------------------------------------------------
  27. *
  28. * (C) Copyright 2008 Texas Instruments Incorporated.
  29. *
  30. * Based on
  31. * u-boot OMAP1510 USB drivers (drivers/usbdcore_omap1510.c)
  32. * twl4030 init based on linux (drivers/i2c/chips/twl4030_usb.c)
  33. *
  34. * Author: Diego Dompe (diego.dompe@ridgerun.com)
  35. * Atin Malaviya (atin.malaviya@gmail.com)
  36. *
  37. * -------------------------------------------------------------------------
  38. */
  39. #include <common.h>
  40. #include <hang.h>
  41. #include <serial.h>
  42. #include <usbdevice.h>
  43. #include <linux/delay.h>
  44. #include <usb/udc.h>
  45. #include "../gadget/ep0.h"
  46. #include "musb_core.h"
  47. #if defined(CONFIG_USB_OMAP3)
  48. #include "omap3.h"
  49. #elif defined(CONFIG_USB_AM35X)
  50. #include "am35x.h"
  51. #endif
  52. /* Define MUSB_DEBUG for debugging */
  53. /* #define MUSB_DEBUG */
  54. #include "musb_debug.h"
  55. #define MAX_ENDPOINT 15
  56. #define GET_ENDPOINT(dev,ep) \
  57. (((struct usb_device_instance *)(dev))->bus->endpoint_array + ep)
  58. #define SET_EP0_STATE(s) \
  59. do { \
  60. if ((0 <= (s)) && (SET_ADDRESS >= (s))) { \
  61. if ((s) != ep0_state) { \
  62. if ((debug_setup) && (debug_level > 1)) \
  63. serial_printf("INFO : Changing state " \
  64. "from %s to %s in %s at " \
  65. "line %d\n", \
  66. ep0_state_strings[ep0_state],\
  67. ep0_state_strings[s], \
  68. __PRETTY_FUNCTION__, \
  69. __LINE__); \
  70. ep0_state = s; \
  71. } \
  72. } else { \
  73. if (debug_level > 0) \
  74. serial_printf("Error at %s %d with setting " \
  75. "state %d is invalid\n", \
  76. __PRETTY_FUNCTION__, __LINE__, s); \
  77. } \
  78. } while (0)
  79. /* static implies these initialized to 0 or NULL */
  80. static int debug_setup;
  81. static int debug_level;
  82. static struct musb_epinfo epinfo[MAX_ENDPOINT * 2 + 2];
  83. static enum ep0_state_enum {
  84. IDLE = 0,
  85. TX,
  86. RX,
  87. SET_ADDRESS
  88. } ep0_state = IDLE;
  89. static char *ep0_state_strings[4] = {
  90. "IDLE",
  91. "TX",
  92. "RX",
  93. "SET_ADDRESS",
  94. };
  95. static struct urb *ep0_urb;
  96. struct usb_endpoint_instance *ep0_endpoint;
  97. static struct usb_device_instance *udc_device;
  98. static int enabled;
  99. static u16 pending_intrrx;
  100. #ifdef MUSB_DEBUG
  101. static void musb_db_regs(void)
  102. {
  103. u8 b;
  104. u16 w;
  105. b = readb(&musbr->faddr);
  106. serial_printf("\tfaddr 0x%2.2x\n", b);
  107. b = readb(&musbr->power);
  108. musb_print_pwr(b);
  109. w = readw(&musbr->ep[0].ep0.csr0);
  110. musb_print_csr0(w);
  111. b = readb(&musbr->devctl);
  112. musb_print_devctl(b);
  113. b = readb(&musbr->ep[0].ep0.configdata);
  114. musb_print_config(b);
  115. w = readw(&musbr->frame);
  116. serial_printf("\tframe 0x%4.4x\n", w);
  117. b = readb(&musbr->index);
  118. serial_printf("\tindex 0x%2.2x\n", b);
  119. w = readw(&musbr->ep[1].epN.rxmaxp);
  120. musb_print_rxmaxp(w);
  121. w = readw(&musbr->ep[1].epN.rxcsr);
  122. musb_print_rxcsr(w);
  123. w = readw(&musbr->ep[1].epN.txmaxp);
  124. musb_print_txmaxp(w);
  125. w = readw(&musbr->ep[1].epN.txcsr);
  126. musb_print_txcsr(w);
  127. }
  128. #else
  129. #define musb_db_regs()
  130. #endif /* DEBUG_MUSB */
  131. static void musb_peri_softconnect(void)
  132. {
  133. u8 power, devctl;
  134. /* Power off MUSB */
  135. power = readb(&musbr->power);
  136. power &= ~MUSB_POWER_SOFTCONN;
  137. writeb(power, &musbr->power);
  138. /* Read intr to clear */
  139. readb(&musbr->intrusb);
  140. readw(&musbr->intrrx);
  141. readw(&musbr->intrtx);
  142. udelay(1000 * 1000); /* 1 sec */
  143. /* Power on MUSB */
  144. power = readb(&musbr->power);
  145. power |= MUSB_POWER_SOFTCONN;
  146. /*
  147. * The usb device interface is usb 1.1
  148. * Disable 2.0 high speed by clearring the hsenable bit.
  149. */
  150. power &= ~MUSB_POWER_HSENAB;
  151. writeb(power, &musbr->power);
  152. /* Check if device is in b-peripheral mode */
  153. devctl = readb(&musbr->devctl);
  154. if (!(devctl & MUSB_DEVCTL_BDEVICE) ||
  155. (devctl & MUSB_DEVCTL_HM)) {
  156. serial_printf("ERROR : Unsupport USB mode\n");
  157. serial_printf("Check that mini-B USB cable is attached "
  158. "to the device\n");
  159. }
  160. if (debug_setup && (debug_level > 1))
  161. musb_db_regs();
  162. }
  163. static void musb_peri_reset(void)
  164. {
  165. if ((debug_setup) && (debug_level > 1))
  166. serial_printf("INFO : %s reset\n", __PRETTY_FUNCTION__);
  167. if (ep0_endpoint)
  168. ep0_endpoint->endpoint_address = 0xff;
  169. /* Sync sw and hw addresses */
  170. writeb(udc_device->address, &musbr->faddr);
  171. SET_EP0_STATE(IDLE);
  172. }
  173. static void musb_peri_resume(void)
  174. {
  175. /* noop */
  176. }
  177. static void musb_peri_ep0_stall(void)
  178. {
  179. u16 csr0;
  180. csr0 = readw(&musbr->ep[0].ep0.csr0);
  181. csr0 |= MUSB_CSR0_P_SENDSTALL;
  182. writew(csr0, &musbr->ep[0].ep0.csr0);
  183. if ((debug_setup) && (debug_level > 1))
  184. serial_printf("INFO : %s stall\n", __PRETTY_FUNCTION__);
  185. }
  186. static void musb_peri_ep0_ack_req(void)
  187. {
  188. u16 csr0;
  189. csr0 = readw(&musbr->ep[0].ep0.csr0);
  190. csr0 |= MUSB_CSR0_P_SVDRXPKTRDY;
  191. writew(csr0, &musbr->ep[0].ep0.csr0);
  192. }
  193. static void musb_ep0_tx_ready(void)
  194. {
  195. u16 csr0;
  196. csr0 = readw(&musbr->ep[0].ep0.csr0);
  197. csr0 |= MUSB_CSR0_TXPKTRDY;
  198. writew(csr0, &musbr->ep[0].ep0.csr0);
  199. }
  200. static void musb_ep0_tx_ready_and_last(void)
  201. {
  202. u16 csr0;
  203. csr0 = readw(&musbr->ep[0].ep0.csr0);
  204. csr0 |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_P_DATAEND);
  205. writew(csr0, &musbr->ep[0].ep0.csr0);
  206. }
  207. static void musb_peri_ep0_last(void)
  208. {
  209. u16 csr0;
  210. csr0 = readw(&musbr->ep[0].ep0.csr0);
  211. csr0 |= MUSB_CSR0_P_DATAEND;
  212. writew(csr0, &musbr->ep[0].ep0.csr0);
  213. }
  214. static void musb_peri_ep0_set_address(void)
  215. {
  216. u8 faddr;
  217. writeb(udc_device->address, &musbr->faddr);
  218. /* Verify */
  219. faddr = readb(&musbr->faddr);
  220. if (udc_device->address == faddr) {
  221. SET_EP0_STATE(IDLE);
  222. usbd_device_event_irq(udc_device, DEVICE_ADDRESS_ASSIGNED, 0);
  223. if ((debug_setup) && (debug_level > 1))
  224. serial_printf("INFO : %s Address set to %d\n",
  225. __PRETTY_FUNCTION__, udc_device->address);
  226. } else {
  227. if (debug_level > 0)
  228. serial_printf("ERROR : %s Address missmatch "
  229. "sw %d vs hw %d\n",
  230. __PRETTY_FUNCTION__,
  231. udc_device->address, faddr);
  232. }
  233. }
  234. static void musb_peri_rx_ack(unsigned int ep)
  235. {
  236. u16 peri_rxcsr;
  237. peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
  238. peri_rxcsr &= ~MUSB_RXCSR_RXPKTRDY;
  239. writew(peri_rxcsr, &musbr->ep[ep].epN.rxcsr);
  240. }
  241. static void musb_peri_tx_ready(unsigned int ep)
  242. {
  243. u16 peri_txcsr;
  244. peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
  245. peri_txcsr |= MUSB_TXCSR_TXPKTRDY;
  246. writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
  247. }
  248. static void musb_peri_ep0_zero_data_request(int err)
  249. {
  250. musb_peri_ep0_ack_req();
  251. if (err) {
  252. musb_peri_ep0_stall();
  253. SET_EP0_STATE(IDLE);
  254. } else {
  255. musb_peri_ep0_last();
  256. /* USBD state */
  257. switch (ep0_urb->device_request.bRequest) {
  258. case USB_REQ_SET_ADDRESS:
  259. if ((debug_setup) && (debug_level > 1))
  260. serial_printf("INFO : %s received set "
  261. "address\n", __PRETTY_FUNCTION__);
  262. break;
  263. case USB_REQ_SET_CONFIGURATION:
  264. if ((debug_setup) && (debug_level > 1))
  265. serial_printf("INFO : %s Configured\n",
  266. __PRETTY_FUNCTION__);
  267. usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
  268. break;
  269. }
  270. /* EP0 state */
  271. if (USB_REQ_SET_ADDRESS == ep0_urb->device_request.bRequest) {
  272. SET_EP0_STATE(SET_ADDRESS);
  273. } else {
  274. SET_EP0_STATE(IDLE);
  275. }
  276. }
  277. }
  278. static void musb_peri_ep0_rx_data_request(void)
  279. {
  280. /*
  281. * This is the completion of the data OUT / RX
  282. *
  283. * Host is sending data to ep0 that is not
  284. * part of setup. This comes from the cdc_recv_setup
  285. * op that is device specific.
  286. *
  287. */
  288. musb_peri_ep0_ack_req();
  289. ep0_endpoint->rcv_urb = ep0_urb;
  290. ep0_urb->actual_length = 0;
  291. SET_EP0_STATE(RX);
  292. }
  293. static void musb_peri_ep0_tx_data_request(int err)
  294. {
  295. if (err) {
  296. musb_peri_ep0_stall();
  297. SET_EP0_STATE(IDLE);
  298. } else {
  299. musb_peri_ep0_ack_req();
  300. ep0_endpoint->tx_urb = ep0_urb;
  301. ep0_endpoint->sent = 0;
  302. SET_EP0_STATE(TX);
  303. }
  304. }
  305. static void musb_peri_ep0_idle(void)
  306. {
  307. u16 count0;
  308. int err;
  309. u16 csr0;
  310. /*
  311. * Verify addresses
  312. * A lot of confusion can be caused if the address
  313. * in software, udc layer, does not agree with the
  314. * hardware. Since the setting of the hardware address
  315. * must be set after the set address request, the
  316. * usb state machine is out of sync for a few frame.
  317. * It is a good idea to run this check when changes
  318. * are made to the state machine.
  319. */
  320. if ((debug_level > 0) &&
  321. (ep0_state != SET_ADDRESS)) {
  322. u8 faddr;
  323. faddr = readb(&musbr->faddr);
  324. if (udc_device->address != faddr) {
  325. serial_printf("ERROR : %s addresses do not"
  326. "match sw %d vs hw %d\n",
  327. __PRETTY_FUNCTION__,
  328. udc_device->address, faddr);
  329. udelay(1000 * 1000);
  330. hang();
  331. }
  332. }
  333. csr0 = readw(&musbr->ep[0].ep0.csr0);
  334. if (!(MUSB_CSR0_RXPKTRDY & csr0))
  335. goto end;
  336. count0 = readw(&musbr->ep[0].ep0.count0);
  337. if (count0 == 0)
  338. goto end;
  339. if (count0 != 8) {
  340. if ((debug_setup) && (debug_level > 1))
  341. serial_printf("WARN : %s SETUP incorrect size %d\n",
  342. __PRETTY_FUNCTION__, count0);
  343. musb_peri_ep0_stall();
  344. goto end;
  345. }
  346. read_fifo(0, count0, &ep0_urb->device_request);
  347. if (debug_level > 2)
  348. print_usb_device_request(&ep0_urb->device_request);
  349. if (ep0_urb->device_request.wLength == 0) {
  350. err = ep0_recv_setup(ep0_urb);
  351. /* Zero data request */
  352. musb_peri_ep0_zero_data_request(err);
  353. } else {
  354. /* Is data coming or going ? */
  355. u8 reqType = ep0_urb->device_request.bmRequestType;
  356. if (USB_REQ_DEVICE2HOST == (reqType & USB_REQ_DIRECTION_MASK)) {
  357. err = ep0_recv_setup(ep0_urb);
  358. /* Device to host */
  359. musb_peri_ep0_tx_data_request(err);
  360. } else {
  361. /*
  362. * Host to device
  363. *
  364. * The RX routine will call ep0_recv_setup
  365. * when the data packet has arrived.
  366. */
  367. musb_peri_ep0_rx_data_request();
  368. }
  369. }
  370. end:
  371. return;
  372. }
  373. static void musb_peri_ep0_rx(void)
  374. {
  375. /*
  376. * This is the completion of the data OUT / RX
  377. *
  378. * Host is sending data to ep0 that is not
  379. * part of setup. This comes from the cdc_recv_setup
  380. * op that is device specific.
  381. *
  382. * Pass the data back to driver ep0_recv_setup which
  383. * should give the cdc_recv_setup the chance to handle
  384. * the rx
  385. */
  386. u16 csr0;
  387. u16 count0;
  388. if (debug_level > 3) {
  389. if (0 != ep0_urb->actual_length) {
  390. serial_printf("%s finished ? %d of %d\n",
  391. __PRETTY_FUNCTION__,
  392. ep0_urb->actual_length,
  393. ep0_urb->device_request.wLength);
  394. }
  395. }
  396. if (ep0_urb->device_request.wLength == ep0_urb->actual_length) {
  397. musb_peri_ep0_last();
  398. SET_EP0_STATE(IDLE);
  399. ep0_recv_setup(ep0_urb);
  400. return;
  401. }
  402. csr0 = readw(&musbr->ep[0].ep0.csr0);
  403. if (!(MUSB_CSR0_RXPKTRDY & csr0))
  404. return;
  405. count0 = readw(&musbr->ep[0].ep0.count0);
  406. if (count0) {
  407. struct usb_endpoint_instance *endpoint;
  408. u32 length;
  409. u8 *data;
  410. endpoint = ep0_endpoint;
  411. if (endpoint && endpoint->rcv_urb) {
  412. struct urb *urb = endpoint->rcv_urb;
  413. unsigned int remaining_space = urb->buffer_length -
  414. urb->actual_length;
  415. if (remaining_space) {
  416. int urb_bad = 0; /* urb is good */
  417. if (count0 > remaining_space)
  418. length = remaining_space;
  419. else
  420. length = count0;
  421. data = (u8 *) urb->buffer_data;
  422. data += urb->actual_length;
  423. /* The common musb fifo reader */
  424. read_fifo(0, length, data);
  425. musb_peri_ep0_ack_req();
  426. /*
  427. * urb's actual_length is updated in
  428. * usbd_rcv_complete
  429. */
  430. usbd_rcv_complete(endpoint, length, urb_bad);
  431. } else {
  432. if (debug_level > 0)
  433. serial_printf("ERROR : %s no space in "
  434. "rcv buffer\n",
  435. __PRETTY_FUNCTION__);
  436. }
  437. } else {
  438. if (debug_level > 0)
  439. serial_printf("ERROR : %s problem with "
  440. "endpoint\n",
  441. __PRETTY_FUNCTION__);
  442. }
  443. } else {
  444. if (debug_level > 0)
  445. serial_printf("ERROR : %s with nothing to do\n",
  446. __PRETTY_FUNCTION__);
  447. }
  448. }
  449. static void musb_peri_ep0_tx(void)
  450. {
  451. u16 csr0;
  452. int transfer_size = 0;
  453. unsigned int p, pm;
  454. csr0 = readw(&musbr->ep[0].ep0.csr0);
  455. /* Check for pending tx */
  456. if (csr0 & MUSB_CSR0_TXPKTRDY)
  457. goto end;
  458. /* Check if this is the last packet sent */
  459. if (ep0_endpoint->sent >= ep0_urb->actual_length) {
  460. SET_EP0_STATE(IDLE);
  461. goto end;
  462. }
  463. transfer_size = ep0_urb->actual_length - ep0_endpoint->sent;
  464. /* Is the transfer size negative ? */
  465. if (transfer_size <= 0) {
  466. if (debug_level > 0)
  467. serial_printf("ERROR : %s problem with the"
  468. " transfer size %d\n",
  469. __PRETTY_FUNCTION__,
  470. transfer_size);
  471. SET_EP0_STATE(IDLE);
  472. goto end;
  473. }
  474. /* Truncate large transfers to the fifo size */
  475. if (transfer_size > ep0_endpoint->tx_packetSize)
  476. transfer_size = ep0_endpoint->tx_packetSize;
  477. write_fifo(0, transfer_size, &ep0_urb->buffer[ep0_endpoint->sent]);
  478. ep0_endpoint->sent += transfer_size;
  479. /* Done or more to send ? */
  480. if (ep0_endpoint->sent >= ep0_urb->actual_length)
  481. musb_ep0_tx_ready_and_last();
  482. else
  483. musb_ep0_tx_ready();
  484. /* Wait a bit */
  485. pm = 10;
  486. for (p = 0; p < pm; p++) {
  487. csr0 = readw(&musbr->ep[0].ep0.csr0);
  488. if (!(csr0 & MUSB_CSR0_TXPKTRDY))
  489. break;
  490. /* Double the delay. */
  491. udelay(1 << pm);
  492. }
  493. if ((ep0_endpoint->sent >= ep0_urb->actual_length) && (p < pm))
  494. SET_EP0_STATE(IDLE);
  495. end:
  496. return;
  497. }
  498. static void musb_peri_ep0(void)
  499. {
  500. u16 csr0;
  501. if (SET_ADDRESS == ep0_state)
  502. return;
  503. csr0 = readw(&musbr->ep[0].ep0.csr0);
  504. /* Error conditions */
  505. if (MUSB_CSR0_P_SENTSTALL & csr0) {
  506. csr0 &= ~MUSB_CSR0_P_SENTSTALL;
  507. writew(csr0, &musbr->ep[0].ep0.csr0);
  508. SET_EP0_STATE(IDLE);
  509. }
  510. if (MUSB_CSR0_P_SETUPEND & csr0) {
  511. csr0 |= MUSB_CSR0_P_SVDSETUPEND;
  512. writew(csr0, &musbr->ep[0].ep0.csr0);
  513. SET_EP0_STATE(IDLE);
  514. if ((debug_setup) && (debug_level > 1))
  515. serial_printf("WARN: %s SETUPEND\n",
  516. __PRETTY_FUNCTION__);
  517. }
  518. /* Normal states */
  519. if (IDLE == ep0_state)
  520. musb_peri_ep0_idle();
  521. if (TX == ep0_state)
  522. musb_peri_ep0_tx();
  523. if (RX == ep0_state)
  524. musb_peri_ep0_rx();
  525. }
  526. static void musb_peri_rx_ep(unsigned int ep)
  527. {
  528. u16 peri_rxcount;
  529. u16 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
  530. if (!(peri_rxcsr & MUSB_RXCSR_RXPKTRDY)) {
  531. if (debug_level > 0)
  532. serial_printf("ERROR : %s %d without MUSB_RXCSR_RXPKTRDY set\n",
  533. __PRETTY_FUNCTION__, ep);
  534. return;
  535. }
  536. peri_rxcount = readw(&musbr->ep[ep].epN.rxcount);
  537. if (peri_rxcount) {
  538. struct usb_endpoint_instance *endpoint;
  539. u32 length;
  540. u8 *data;
  541. endpoint = GET_ENDPOINT(udc_device, ep);
  542. if (endpoint && endpoint->rcv_urb) {
  543. struct urb *urb = endpoint->rcv_urb;
  544. unsigned int remaining_space = urb->buffer_length -
  545. urb->actual_length;
  546. if (remaining_space) {
  547. int urb_bad = 0; /* urb is good */
  548. if (peri_rxcount > remaining_space)
  549. length = remaining_space;
  550. else
  551. length = peri_rxcount;
  552. data = (u8 *) urb->buffer_data;
  553. data += urb->actual_length;
  554. /* The common musb fifo reader */
  555. read_fifo(ep, length, data);
  556. if (length == peri_rxcount)
  557. musb_peri_rx_ack(ep);
  558. else
  559. pending_intrrx |= (1 << ep);
  560. /*
  561. * urb's actual_length is updated in
  562. * usbd_rcv_complete
  563. */
  564. usbd_rcv_complete(endpoint, length, urb_bad);
  565. } else {
  566. if (debug_level > 0)
  567. serial_printf("ERROR : %s %d no space "
  568. "in rcv buffer\n",
  569. __PRETTY_FUNCTION__, ep);
  570. pending_intrrx |= (1 << ep);
  571. }
  572. } else {
  573. if (debug_level > 0)
  574. serial_printf("ERROR : %s %d problem with "
  575. "endpoint\n",
  576. __PRETTY_FUNCTION__, ep);
  577. pending_intrrx |= (1 << ep);
  578. }
  579. } else {
  580. if (debug_level > 0)
  581. serial_printf("ERROR : %s %d with nothing to do\n",
  582. __PRETTY_FUNCTION__, ep);
  583. musb_peri_rx_ack(ep);
  584. }
  585. }
  586. static void musb_peri_rx(u16 intr)
  587. {
  588. unsigned int ep;
  589. /* First bit is reserved and does not indicate interrupt for EP0 */
  590. for (ep = 1; ep < 16; ep++) {
  591. if ((1 << ep) & intr)
  592. musb_peri_rx_ep(ep);
  593. }
  594. }
  595. static void musb_peri_tx(u16 intr)
  596. {
  597. unsigned int ep;
  598. /* Check for EP0: first bit indicates interrupt for both RX and TX */
  599. if (0x01 & intr)
  600. musb_peri_ep0();
  601. for (ep = 1; ep < 16; ep++) {
  602. if ((1 << ep) & intr)
  603. udc_endpoint_write(GET_ENDPOINT(udc_device, ep));
  604. }
  605. }
  606. void udc_irq(void)
  607. {
  608. /* This is a high freq called function */
  609. if (enabled) {
  610. u8 intrusb;
  611. intrusb = readb(&musbr->intrusb);
  612. /*
  613. * See drivers/usb/gadget/mpc8xx_udc.c for
  614. * state diagram going from detached through
  615. * configuration.
  616. */
  617. if (MUSB_INTR_RESUME & intrusb) {
  618. usbd_device_event_irq(udc_device,
  619. DEVICE_BUS_ACTIVITY, 0);
  620. musb_peri_resume();
  621. }
  622. if (MUSB_INTR_RESET & intrusb) {
  623. usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
  624. musb_peri_reset();
  625. }
  626. if (MUSB_INTR_DISCONNECT & intrusb) {
  627. /* cable unplugged from hub/host */
  628. usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
  629. musb_peri_reset();
  630. usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
  631. }
  632. if (MUSB_INTR_SOF & intrusb) {
  633. usbd_device_event_irq(udc_device,
  634. DEVICE_BUS_ACTIVITY, 0);
  635. musb_peri_resume();
  636. }
  637. if (MUSB_INTR_SUSPEND & intrusb) {
  638. usbd_device_event_irq(udc_device,
  639. DEVICE_BUS_INACTIVE, 0);
  640. }
  641. if (ep0_state != SET_ADDRESS) {
  642. u16 intrrx, intrtx;
  643. intrrx = readw(&musbr->intrrx);
  644. intrtx = readw(&musbr->intrtx);
  645. intrrx |= pending_intrrx;
  646. pending_intrrx = 0;
  647. if (intrrx)
  648. musb_peri_rx(intrrx);
  649. if (intrtx)
  650. musb_peri_tx(intrtx);
  651. } else {
  652. if (readw(&musbr->intrtx) & 0x1) {
  653. u8 faddr;
  654. faddr = readb(&musbr->faddr);
  655. /*
  656. * Setting of the address can fail.
  657. * Normally it succeeds the second time.
  658. */
  659. if (udc_device->address != faddr)
  660. musb_peri_ep0_set_address();
  661. }
  662. }
  663. }
  664. }
  665. void udc_set_nak(int ep_num)
  666. {
  667. /* noop */
  668. }
  669. void udc_unset_nak(int ep_num)
  670. {
  671. /* noop */
  672. }
  673. int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
  674. {
  675. int ret = 0;
  676. /* Transmit only if the hardware is available */
  677. if (endpoint->tx_urb && endpoint->state == 0) {
  678. unsigned int ep = endpoint->endpoint_address &
  679. USB_ENDPOINT_NUMBER_MASK;
  680. u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
  681. /* Error conditions */
  682. if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
  683. peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
  684. writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
  685. }
  686. if (debug_level > 1)
  687. musb_print_txcsr(peri_txcsr);
  688. /* Check if a packet is waiting to be sent */
  689. if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
  690. u32 length;
  691. u8 *data;
  692. struct urb *urb = endpoint->tx_urb;
  693. unsigned int remaining_packet = urb->actual_length -
  694. endpoint->sent;
  695. if (endpoint->tx_packetSize < remaining_packet)
  696. length = endpoint->tx_packetSize;
  697. else
  698. length = remaining_packet;
  699. data = (u8 *) urb->buffer;
  700. data += endpoint->sent;
  701. /* common musb fifo function */
  702. write_fifo(ep, length, data);
  703. musb_peri_tx_ready(ep);
  704. endpoint->last = length;
  705. /* usbd_tx_complete will take care of updating 'sent' */
  706. usbd_tx_complete(endpoint);
  707. }
  708. } else {
  709. if (debug_level > 0)
  710. serial_printf("ERROR : %s Problem with urb %p "
  711. "or ep state %d\n",
  712. __PRETTY_FUNCTION__,
  713. endpoint->tx_urb, endpoint->state);
  714. }
  715. return ret;
  716. }
  717. void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
  718. struct usb_endpoint_instance *endpoint)
  719. {
  720. if (0 == id) {
  721. /* EP0 */
  722. ep0_endpoint = endpoint;
  723. ep0_endpoint->endpoint_address = 0xff;
  724. ep0_urb = usbd_alloc_urb(device, endpoint);
  725. } else if (MAX_ENDPOINT >= id) {
  726. epinfo[(id * 2) + 0].epsize = endpoint->rcv_packetSize;
  727. epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
  728. musb_configure_ep(&epinfo[0], ARRAY_SIZE(epinfo));
  729. } else {
  730. if (debug_level > 0)
  731. serial_printf("ERROR : %s endpoint request %d "
  732. "exceeds maximum %d\n",
  733. __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
  734. }
  735. }
  736. void udc_connect(void)
  737. {
  738. /* noop */
  739. }
  740. void udc_disconnect(void)
  741. {
  742. /* noop */
  743. }
  744. void udc_enable(struct usb_device_instance *device)
  745. {
  746. /* Save the device structure pointer */
  747. udc_device = device;
  748. enabled = 1;
  749. }
  750. void udc_disable(void)
  751. {
  752. enabled = 0;
  753. }
  754. void udc_startup_events(struct usb_device_instance *device)
  755. {
  756. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
  757. usbd_device_event_irq(device, DEVICE_INIT, 0);
  758. /*
  759. * The DEVICE_CREATE event puts the USB device in the state
  760. * STATE_ATTACHED.
  761. */
  762. usbd_device_event_irq(device, DEVICE_CREATE, 0);
  763. /* Resets the address to 0 */
  764. usbd_device_event_irq(device, DEVICE_RESET, 0);
  765. udc_enable(device);
  766. }
  767. int udc_init(void)
  768. {
  769. int ret;
  770. int ep_loop;
  771. ret = musb_platform_init();
  772. if (ret < 0)
  773. goto end;
  774. /* Configure all the endpoint FIFO's and start usb controller */
  775. musbr = musb_cfg.regs;
  776. /* Initialize the endpoints */
  777. for (ep_loop = 0; ep_loop <= MAX_ENDPOINT * 2; ep_loop++) {
  778. epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
  779. epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
  780. epinfo[ep_loop].epsize = 0;
  781. }
  782. musb_peri_softconnect();
  783. ret = 0;
  784. end:
  785. return ret;
  786. }