musb_udc.c 21 KB

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