musb_udc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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. /* Power off MUSB */
  146. power = readb(&musbr->power);
  147. power &= ~MUSB_POWER_SOFTCONN;
  148. writeb(power, &musbr->power);
  149. /* Read intr to clear */
  150. readb(&musbr->intrusb);
  151. readw(&musbr->intrrx);
  152. 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;
  540. u8 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
  541. if (!(peri_rxcsr & MUSB_RXCSR_RXPKTRDY)) {
  542. if (debug_level > 0)
  543. serial_printf("ERROR : %s %d without MUSB_RXCSR_RXPKTRDY set\n",
  544. __PRETTY_FUNCTION__, ep);
  545. return;
  546. }
  547. peri_rxcount = readw(&musbr->ep[ep].epN.rxcount);
  548. if (peri_rxcount) {
  549. struct usb_endpoint_instance *endpoint;
  550. u32 length;
  551. u8 *data;
  552. endpoint = GET_ENDPOINT(udc_device, ep);
  553. if (endpoint && endpoint->rcv_urb) {
  554. struct urb *urb = endpoint->rcv_urb;
  555. unsigned int remaining_space = urb->buffer_length -
  556. urb->actual_length;
  557. if (remaining_space) {
  558. int urb_bad = 0; /* urb is good */
  559. if (peri_rxcount > remaining_space)
  560. length = remaining_space;
  561. else
  562. length = peri_rxcount;
  563. data = (u8 *) urb->buffer_data;
  564. data += urb->actual_length;
  565. /* The common musb fifo reader */
  566. read_fifo(ep, length, data);
  567. musb_peri_rx_ack(ep);
  568. /*
  569. * urb's actual_length is updated in
  570. * usbd_rcv_complete
  571. */
  572. usbd_rcv_complete(endpoint, length, urb_bad);
  573. } else {
  574. if (debug_level > 0)
  575. serial_printf("ERROR : %s %d no space "
  576. "in rcv buffer\n",
  577. __PRETTY_FUNCTION__, ep);
  578. }
  579. } else {
  580. if (debug_level > 0)
  581. serial_printf("ERROR : %s %d problem with "
  582. "endpoint\n",
  583. __PRETTY_FUNCTION__, ep);
  584. }
  585. } else {
  586. if (debug_level > 0)
  587. serial_printf("ERROR : %s %d with nothing to do\n",
  588. __PRETTY_FUNCTION__, ep);
  589. }
  590. }
  591. static void musb_peri_rx(u16 intr)
  592. {
  593. unsigned int ep;
  594. /* Check for EP0 */
  595. if (0x01 & intr)
  596. musb_peri_ep0();
  597. for (ep = 1; ep < 16; ep++) {
  598. if ((1 << ep) & intr)
  599. musb_peri_rx_ep(ep);
  600. }
  601. }
  602. static void musb_peri_tx(u16 intr)
  603. {
  604. /* Check for EP0 */
  605. if (0x01 & intr)
  606. musb_peri_ep0_tx();
  607. /*
  608. * Use this in the future when handling epN tx
  609. *
  610. * u8 ep;
  611. *
  612. * for (ep = 1; ep < 16; ep++) {
  613. * if ((1 << ep) & intr) {
  614. * / * handle tx for this endpoint * /
  615. * }
  616. * }
  617. */
  618. }
  619. void udc_irq(void)
  620. {
  621. /* This is a high freq called function */
  622. if (enabled) {
  623. u8 intrusb;
  624. intrusb = readb(&musbr->intrusb);
  625. /*
  626. * See drivers/usb/gadget/mpc8xx_udc.c for
  627. * state diagram going from detached through
  628. * configuration.
  629. */
  630. if (MUSB_INTR_RESUME & intrusb) {
  631. usbd_device_event_irq(udc_device,
  632. DEVICE_BUS_ACTIVITY, 0);
  633. musb_peri_resume();
  634. }
  635. musb_peri_ep0();
  636. if (MUSB_INTR_RESET & intrusb) {
  637. usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
  638. musb_peri_reset();
  639. }
  640. if (MUSB_INTR_DISCONNECT & intrusb) {
  641. /* cable unplugged from hub/host */
  642. usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
  643. musb_peri_reset();
  644. usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
  645. }
  646. if (MUSB_INTR_SOF & intrusb) {
  647. usbd_device_event_irq(udc_device,
  648. DEVICE_BUS_ACTIVITY, 0);
  649. musb_peri_resume();
  650. }
  651. if (MUSB_INTR_SUSPEND & intrusb) {
  652. usbd_device_event_irq(udc_device,
  653. DEVICE_BUS_INACTIVE, 0);
  654. }
  655. if (ep0_state != SET_ADDRESS) {
  656. u16 intrrx, intrtx;
  657. intrrx = readw(&musbr->intrrx);
  658. intrtx = readw(&musbr->intrtx);
  659. if (intrrx)
  660. musb_peri_rx(intrrx);
  661. if (intrtx)
  662. musb_peri_tx(intrtx);
  663. } else {
  664. if (MUSB_INTR_SOF & intrusb) {
  665. u8 faddr;
  666. faddr = readb(&musbr->faddr);
  667. /*
  668. * Setting of the address can fail.
  669. * Normally it succeeds the second time.
  670. */
  671. if (udc_device->address != faddr)
  672. musb_peri_ep0_set_address();
  673. }
  674. }
  675. }
  676. }
  677. void udc_set_nak(int ep_num)
  678. {
  679. /* noop */
  680. }
  681. void udc_unset_nak(int ep_num)
  682. {
  683. /* noop */
  684. }
  685. int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
  686. {
  687. int ret = 0;
  688. /* Transmit only if the hardware is available */
  689. if (endpoint->tx_urb && endpoint->state == 0) {
  690. unsigned int ep = endpoint->endpoint_address &
  691. USB_ENDPOINT_NUMBER_MASK;
  692. u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
  693. /* Error conditions */
  694. if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
  695. peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
  696. writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
  697. }
  698. if (debug_level > 1)
  699. musb_print_txcsr(peri_txcsr);
  700. /* Check if a packet is waiting to be sent */
  701. if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
  702. u32 length;
  703. u8 *data;
  704. struct urb *urb = endpoint->tx_urb;
  705. unsigned int remaining_packet = urb->actual_length -
  706. endpoint->sent;
  707. if (endpoint->tx_packetSize < remaining_packet)
  708. length = endpoint->tx_packetSize;
  709. else
  710. length = remaining_packet;
  711. data = (u8 *) urb->buffer;
  712. data += endpoint->sent;
  713. /* common musb fifo function */
  714. write_fifo(ep, length, data);
  715. musb_peri_tx_ready(ep);
  716. endpoint->last = length;
  717. /* usbd_tx_complete will take care of updating 'sent' */
  718. usbd_tx_complete(endpoint);
  719. }
  720. } else {
  721. if (debug_level > 0)
  722. serial_printf("ERROR : %s Problem with urb %p "
  723. "or ep state %d\n",
  724. __PRETTY_FUNCTION__,
  725. endpoint->tx_urb, endpoint->state);
  726. }
  727. return ret;
  728. }
  729. void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
  730. struct usb_endpoint_instance *endpoint)
  731. {
  732. if (0 == id) {
  733. /* EP0 */
  734. ep0_endpoint = endpoint;
  735. ep0_endpoint->endpoint_address = 0xff;
  736. ep0_urb = usbd_alloc_urb(device, endpoint);
  737. } else if (MAX_ENDPOINT >= id) {
  738. int ep_addr;
  739. /* Check the direction */
  740. ep_addr = endpoint->endpoint_address;
  741. if (USB_DIR_IN == (ep_addr & USB_ENDPOINT_DIR_MASK)) {
  742. /* IN */
  743. epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
  744. } else {
  745. /* OUT */
  746. epinfo[id * 2].epsize = endpoint->rcv_packetSize;
  747. }
  748. musb_configure_ep(&epinfo[0],
  749. sizeof(epinfo) / sizeof(struct musb_epinfo));
  750. } else {
  751. if (debug_level > 0)
  752. serial_printf("ERROR : %s endpoint request %d "
  753. "exceeds maximum %d\n",
  754. __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
  755. }
  756. }
  757. void udc_connect(void)
  758. {
  759. /* noop */
  760. }
  761. void udc_disconnect(void)
  762. {
  763. /* noop */
  764. }
  765. void udc_enable(struct usb_device_instance *device)
  766. {
  767. /* Save the device structure pointer */
  768. udc_device = device;
  769. enabled = 1;
  770. }
  771. void udc_disable(void)
  772. {
  773. enabled = 0;
  774. }
  775. void udc_startup_events(struct usb_device_instance *device)
  776. {
  777. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
  778. usbd_device_event_irq(device, DEVICE_INIT, 0);
  779. /*
  780. * The DEVICE_CREATE event puts the USB device in the state
  781. * STATE_ATTACHED.
  782. */
  783. usbd_device_event_irq(device, DEVICE_CREATE, 0);
  784. /* Resets the address to 0 */
  785. usbd_device_event_irq(device, DEVICE_RESET, 0);
  786. udc_enable(device);
  787. }
  788. int udc_init(void)
  789. {
  790. int ret;
  791. int ep_loop;
  792. ret = musb_platform_init();
  793. if (ret < 0)
  794. goto end;
  795. /* Configure all the endpoint FIFO's and start usb controller */
  796. musbr = musb_cfg.regs;
  797. /* Initialize the endpoints */
  798. for (ep_loop = 0; ep_loop < MAX_ENDPOINT * 2; ep_loop++) {
  799. epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
  800. epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
  801. epinfo[ep_loop].epsize = 0;
  802. }
  803. musb_peri_softconnect();
  804. ret = 0;
  805. end:
  806. return ret;
  807. }