musb_udc.c 21 KB

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