musb_udc.c 21 KB

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