musb_udc.c 21 KB

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