designware_udc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*
  2. * Based on drivers/usb/gadget/omap1510_udc.c
  3. * TI OMAP1510 USB bus interface driver
  4. *
  5. * (C) Copyright 2009
  6. * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <asm/io.h>
  28. #include <usbdevice.h>
  29. #include "ep0.h"
  30. #include <usb/designware_udc.h>
  31. #include <asm/arch/hardware.h>
  32. #define UDC_INIT_MDELAY 80 /* Device settle delay */
  33. /* Some kind of debugging output... */
  34. #ifndef DEBUG_DWUSBTTY
  35. #define UDCDBG(str)
  36. #define UDCDBGA(fmt, args...)
  37. #else
  38. #define UDCDBG(str) serial_printf(str "\n")
  39. #define UDCDBGA(fmt, args...) serial_printf(fmt "\n", ##args)
  40. #endif
  41. static struct urb *ep0_urb;
  42. static struct usb_device_instance *udc_device;
  43. static struct plug_regs *const plug_regs_p =
  44. (struct plug_regs * const)CONFIG_SYS_PLUG_BASE;
  45. static struct udc_regs *const udc_regs_p =
  46. (struct udc_regs * const)CONFIG_SYS_USBD_BASE;
  47. static struct udc_endp_regs *const outep_regs_p =
  48. &((struct udc_regs * const)CONFIG_SYS_USBD_BASE)->out_regs[0];
  49. static struct udc_endp_regs *const inep_regs_p =
  50. &((struct udc_regs * const)CONFIG_SYS_USBD_BASE)->in_regs[0];
  51. /*
  52. * udc_state_transition - Write the next packet to TxFIFO.
  53. * @initial: Initial state.
  54. * @final: Final state.
  55. *
  56. * Helper function to implement device state changes. The device states and
  57. * the events that transition between them are:
  58. *
  59. * STATE_ATTACHED
  60. * || /\
  61. * \/ ||
  62. * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
  63. * || /\
  64. * \/ ||
  65. * STATE_POWERED
  66. * || /\
  67. * \/ ||
  68. * DEVICE_RESET DEVICE_POWER_INTERRUPTION
  69. * || /\
  70. * \/ ||
  71. * STATE_DEFAULT
  72. * || /\
  73. * \/ ||
  74. * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
  75. * || /\
  76. * \/ ||
  77. * STATE_ADDRESSED
  78. * || /\
  79. * \/ ||
  80. * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
  81. * || /\
  82. * \/ ||
  83. * STATE_CONFIGURED
  84. *
  85. * udc_state_transition transitions up (in the direction from STATE_ATTACHED
  86. * to STATE_CONFIGURED) from the specified initial state to the specified final
  87. * state, passing through each intermediate state on the way. If the initial
  88. * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
  89. * no state transitions will take place.
  90. *
  91. * udc_state_transition also transitions down (in the direction from
  92. * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
  93. * specified final state, passing through each intermediate state on the way.
  94. * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
  95. * state, then no state transitions will take place.
  96. *
  97. * This function must only be called with interrupts disabled.
  98. */
  99. static void udc_state_transition(usb_device_state_t initial,
  100. usb_device_state_t final)
  101. {
  102. if (initial < final) {
  103. switch (initial) {
  104. case STATE_ATTACHED:
  105. usbd_device_event_irq(udc_device,
  106. DEVICE_HUB_CONFIGURED, 0);
  107. if (final == STATE_POWERED)
  108. break;
  109. case STATE_POWERED:
  110. usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
  111. if (final == STATE_DEFAULT)
  112. break;
  113. case STATE_DEFAULT:
  114. usbd_device_event_irq(udc_device,
  115. DEVICE_ADDRESS_ASSIGNED, 0);
  116. if (final == STATE_ADDRESSED)
  117. break;
  118. case STATE_ADDRESSED:
  119. usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
  120. case STATE_CONFIGURED:
  121. break;
  122. default:
  123. break;
  124. }
  125. } else if (initial > final) {
  126. switch (initial) {
  127. case STATE_CONFIGURED:
  128. usbd_device_event_irq(udc_device,
  129. DEVICE_DE_CONFIGURED, 0);
  130. if (final == STATE_ADDRESSED)
  131. break;
  132. case STATE_ADDRESSED:
  133. usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
  134. if (final == STATE_DEFAULT)
  135. break;
  136. case STATE_DEFAULT:
  137. usbd_device_event_irq(udc_device,
  138. DEVICE_POWER_INTERRUPTION, 0);
  139. if (final == STATE_POWERED)
  140. break;
  141. case STATE_POWERED:
  142. usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
  143. case STATE_ATTACHED:
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. }
  150. /* Stall endpoint */
  151. static void udc_stall_ep(u32 ep_num)
  152. {
  153. writel(readl(&inep_regs_p[ep_num].endp_cntl) | ENDP_CNTL_STALL,
  154. &inep_regs_p[ep_num].endp_cntl);
  155. writel(readl(&outep_regs_p[ep_num].endp_cntl) | ENDP_CNTL_STALL,
  156. &outep_regs_p[ep_num].endp_cntl);
  157. }
  158. static void *get_fifo(int ep_num, int in)
  159. {
  160. u32 *fifo_ptr = (u32 *)CONFIG_SYS_FIFO_BASE;
  161. switch (ep_num) {
  162. case UDC_EP3:
  163. fifo_ptr += readl(&inep_regs_p[1].endp_bsorfn);
  164. /* break intentionally left out */
  165. case UDC_EP1:
  166. fifo_ptr += readl(&inep_regs_p[0].endp_bsorfn);
  167. /* break intentionally left out */
  168. case UDC_EP0:
  169. default:
  170. if (in) {
  171. fifo_ptr +=
  172. readl(&outep_regs_p[2].endp_maxpacksize) >> 16;
  173. /* break intentionally left out */
  174. } else {
  175. break;
  176. }
  177. case UDC_EP2:
  178. fifo_ptr += readl(&outep_regs_p[0].endp_maxpacksize) >> 16;
  179. /* break intentionally left out */
  180. }
  181. return (void *)fifo_ptr;
  182. }
  183. static int usbgetpckfromfifo(int epNum, u8 *bufp, u32 len)
  184. {
  185. u8 *fifo_ptr = (u8 *)get_fifo(epNum, 0);
  186. u32 i, nw, nb;
  187. u32 *wrdp;
  188. u8 *bytp;
  189. if (readl(&udc_regs_p->dev_stat) & DEV_STAT_RXFIFO_EMPTY)
  190. return -1;
  191. nw = len / sizeof(u32);
  192. nb = len % sizeof(u32);
  193. wrdp = (u32 *)bufp;
  194. for (i = 0; i < nw; i++) {
  195. writel(readl(fifo_ptr), wrdp);
  196. wrdp++;
  197. }
  198. bytp = (u8 *)wrdp;
  199. for (i = 0; i < nb; i++) {
  200. writeb(readb(fifo_ptr), bytp);
  201. fifo_ptr++;
  202. bytp++;
  203. }
  204. readl(&outep_regs_p[epNum].write_done);
  205. return 0;
  206. }
  207. static void usbputpcktofifo(int epNum, u8 *bufp, u32 len)
  208. {
  209. u32 i, nw, nb;
  210. u32 *wrdp;
  211. u8 *bytp;
  212. u8 *fifo_ptr = get_fifo(epNum, 1);
  213. nw = len / sizeof(int);
  214. nb = len % sizeof(int);
  215. wrdp = (u32 *)bufp;
  216. for (i = 0; i < nw; i++) {
  217. writel(*wrdp, fifo_ptr);
  218. wrdp++;
  219. }
  220. bytp = (u8 *)wrdp;
  221. for (i = 0; i < nb; i++) {
  222. writeb(*bytp, fifo_ptr);
  223. fifo_ptr++;
  224. bytp++;
  225. }
  226. }
  227. /*
  228. * dw_write_noniso_tx_fifo - Write the next packet to TxFIFO.
  229. * @endpoint: Endpoint pointer.
  230. *
  231. * If the endpoint has an active tx_urb, then the next packet of data from the
  232. * URB is written to the tx FIFO. The total amount of data in the urb is given
  233. * by urb->actual_length. The maximum amount of data that can be sent in any
  234. * one packet is given by endpoint->tx_packetSize. The number of data bytes
  235. * from this URB that have already been transmitted is given by endpoint->sent.
  236. * endpoint->last is updated by this routine with the number of data bytes
  237. * transmitted in this packet.
  238. *
  239. */
  240. static void dw_write_noniso_tx_fifo(struct usb_endpoint_instance
  241. *endpoint)
  242. {
  243. struct urb *urb = endpoint->tx_urb;
  244. int align;
  245. if (urb) {
  246. u32 last;
  247. UDCDBGA("urb->buffer %p, buffer_length %d, actual_length %d",
  248. urb->buffer, urb->buffer_length, urb->actual_length);
  249. last = MIN(urb->actual_length - endpoint->sent,
  250. endpoint->tx_packetSize);
  251. if (last) {
  252. u8 *cp = urb->buffer + endpoint->sent;
  253. /*
  254. * This ensures that USBD packet fifo is accessed
  255. * - through word aligned pointer or
  256. * - through non word aligned pointer but only
  257. * with a max length to make the next packet
  258. * word aligned
  259. */
  260. align = ((ulong)cp % sizeof(int));
  261. if (align)
  262. last = MIN(last, sizeof(int) - align);
  263. UDCDBGA("endpoint->sent %d, tx_packetSize %d, last %d",
  264. endpoint->sent, endpoint->tx_packetSize, last);
  265. usbputpcktofifo(endpoint->endpoint_address &
  266. USB_ENDPOINT_NUMBER_MASK, cp, last);
  267. }
  268. endpoint->last = last;
  269. }
  270. }
  271. /*
  272. * Handle SETUP USB interrupt.
  273. * This function implements TRM Figure 14-14.
  274. */
  275. static void dw_udc_setup(struct usb_endpoint_instance *endpoint)
  276. {
  277. u8 *datap = (u8 *)&ep0_urb->device_request;
  278. int ep_addr = endpoint->endpoint_address;
  279. UDCDBG("-> Entering device setup");
  280. usbgetpckfromfifo(ep_addr, datap, 8);
  281. /* Try to process setup packet */
  282. if (ep0_recv_setup(ep0_urb)) {
  283. /* Not a setup packet, stall next EP0 transaction */
  284. udc_stall_ep(0);
  285. UDCDBG("can't parse setup packet, still waiting for setup");
  286. return;
  287. }
  288. /* Check direction */
  289. if ((ep0_urb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
  290. == USB_REQ_HOST2DEVICE) {
  291. UDCDBG("control write on EP0");
  292. if (le16_to_cpu(ep0_urb->device_request.wLength)) {
  293. /* Stall this request */
  294. UDCDBG("Stalling unsupported EP0 control write data "
  295. "stage.");
  296. udc_stall_ep(0);
  297. }
  298. } else {
  299. UDCDBG("control read on EP0");
  300. /*
  301. * The ep0_recv_setup function has already placed our response
  302. * packet data in ep0_urb->buffer and the packet length in
  303. * ep0_urb->actual_length.
  304. */
  305. endpoint->tx_urb = ep0_urb;
  306. endpoint->sent = 0;
  307. /*
  308. * Write packet data to the FIFO. dw_write_noniso_tx_fifo
  309. * will update endpoint->last with the number of bytes written
  310. * to the FIFO.
  311. */
  312. dw_write_noniso_tx_fifo(endpoint);
  313. writel(0x0, &inep_regs_p[ep_addr].write_done);
  314. }
  315. udc_unset_nak(endpoint->endpoint_address);
  316. UDCDBG("<- Leaving device setup");
  317. }
  318. /*
  319. * Handle endpoint 0 RX interrupt
  320. */
  321. static void dw_udc_ep0_rx(struct usb_endpoint_instance *endpoint)
  322. {
  323. u8 dummy[64];
  324. UDCDBG("RX on EP0");
  325. /* Check direction */
  326. if ((ep0_urb->device_request.bmRequestType
  327. & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
  328. /*
  329. * This rx interrupt must be for a control write data
  330. * stage packet.
  331. *
  332. * We don't support control write data stages.
  333. * We should never end up here.
  334. */
  335. UDCDBG("Stalling unexpected EP0 control write "
  336. "data stage packet");
  337. udc_stall_ep(0);
  338. } else {
  339. /*
  340. * This rx interrupt must be for a control read status
  341. * stage packet.
  342. */
  343. UDCDBG("ACK on EP0 control read status stage packet");
  344. u32 len = (readl(&outep_regs_p[0].endp_status) >> 11) & 0xfff;
  345. usbgetpckfromfifo(0, dummy, len);
  346. }
  347. }
  348. /*
  349. * Handle endpoint 0 TX interrupt
  350. */
  351. static void dw_udc_ep0_tx(struct usb_endpoint_instance *endpoint)
  352. {
  353. struct usb_device_request *request = &ep0_urb->device_request;
  354. int ep_addr;
  355. UDCDBG("TX on EP0");
  356. /* Check direction */
  357. if ((request->bmRequestType & USB_REQ_DIRECTION_MASK) ==
  358. USB_REQ_HOST2DEVICE) {
  359. /*
  360. * This tx interrupt must be for a control write status
  361. * stage packet.
  362. */
  363. UDCDBG("ACK on EP0 control write status stage packet");
  364. } else {
  365. /*
  366. * This tx interrupt must be for a control read data
  367. * stage packet.
  368. */
  369. int wLength = le16_to_cpu(request->wLength);
  370. /*
  371. * Update our count of bytes sent so far in this
  372. * transfer.
  373. */
  374. endpoint->sent += endpoint->last;
  375. /*
  376. * We are finished with this transfer if we have sent
  377. * all of the bytes in our tx urb (urb->actual_length)
  378. * unless we need a zero-length terminating packet. We
  379. * need a zero-length terminating packet if we returned
  380. * fewer bytes than were requested (wLength) by the host,
  381. * and the number of bytes we returned is an exact
  382. * multiple of the packet size endpoint->tx_packetSize.
  383. */
  384. if ((endpoint->sent == ep0_urb->actual_length) &&
  385. ((ep0_urb->actual_length == wLength) ||
  386. (endpoint->last != endpoint->tx_packetSize))) {
  387. /* Done with control read data stage. */
  388. UDCDBG("control read data stage complete");
  389. } else {
  390. /*
  391. * We still have another packet of data to send
  392. * in this control read data stage or else we
  393. * need a zero-length terminating packet.
  394. */
  395. UDCDBG("ACK control read data stage packet");
  396. dw_write_noniso_tx_fifo(endpoint);
  397. ep_addr = endpoint->endpoint_address;
  398. writel(0x0, &inep_regs_p[ep_addr].write_done);
  399. }
  400. }
  401. }
  402. static struct usb_endpoint_instance *dw_find_ep(int ep)
  403. {
  404. int i;
  405. for (i = 0; i < udc_device->bus->max_endpoints; i++) {
  406. if ((udc_device->bus->endpoint_array[i].endpoint_address &
  407. USB_ENDPOINT_NUMBER_MASK) == ep)
  408. return &udc_device->bus->endpoint_array[i];
  409. }
  410. return NULL;
  411. }
  412. /*
  413. * Handle RX transaction on non-ISO endpoint.
  414. * The ep argument is a physical endpoint number for a non-ISO IN endpoint
  415. * in the range 1 to 15.
  416. */
  417. static void dw_udc_epn_rx(int ep)
  418. {
  419. int nbytes = 0;
  420. struct urb *urb;
  421. struct usb_endpoint_instance *endpoint = dw_find_ep(ep);
  422. if (endpoint) {
  423. urb = endpoint->rcv_urb;
  424. if (urb) {
  425. u8 *cp = urb->buffer + urb->actual_length;
  426. nbytes = (readl(&outep_regs_p[ep].endp_status) >> 11) &
  427. 0xfff;
  428. usbgetpckfromfifo(ep, cp, nbytes);
  429. usbd_rcv_complete(endpoint, nbytes, 0);
  430. }
  431. }
  432. }
  433. /*
  434. * Handle TX transaction on non-ISO endpoint.
  435. * The ep argument is a physical endpoint number for a non-ISO IN endpoint
  436. * in the range 16 to 30.
  437. */
  438. static void dw_udc_epn_tx(int ep)
  439. {
  440. struct usb_endpoint_instance *endpoint = dw_find_ep(ep);
  441. /*
  442. * We need to transmit a terminating zero-length packet now if
  443. * we have sent all of the data in this URB and the transfer
  444. * size was an exact multiple of the packet size.
  445. */
  446. if (endpoint && endpoint->tx_urb && endpoint->tx_urb->actual_length) {
  447. if (endpoint->last == endpoint->tx_packetSize) {
  448. /* handle zero length packet here */
  449. writel(0x0, &inep_regs_p[ep].write_done);
  450. }
  451. /* retire the data that was just sent */
  452. usbd_tx_complete(endpoint);
  453. /*
  454. * Check to see if we have more data ready to transmit
  455. * now.
  456. */
  457. if (endpoint->tx_urb && endpoint->tx_urb->actual_length) {
  458. /* write data to FIFO */
  459. dw_write_noniso_tx_fifo(endpoint);
  460. writel(0x0, &inep_regs_p[ep].write_done);
  461. } else if (endpoint->tx_urb
  462. && (endpoint->tx_urb->actual_length == 0)) {
  463. /* udc_set_nak(ep); */
  464. }
  465. }
  466. }
  467. /*
  468. * Start of public functions.
  469. */
  470. /* Called to start packet transmission. */
  471. int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
  472. {
  473. udc_unset_nak(endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK);
  474. return 0;
  475. }
  476. /* Start to initialize h/w stuff */
  477. int udc_init(void)
  478. {
  479. int i;
  480. u32 plug_st;
  481. udc_device = NULL;
  482. UDCDBG("starting");
  483. readl(&plug_regs_p->plug_pending);
  484. udc_disconnect();
  485. for (i = 0; i < UDC_INIT_MDELAY; i++)
  486. udelay(1000);
  487. plug_st = readl(&plug_regs_p->plug_state);
  488. writel(plug_st | PLUG_STATUS_EN, &plug_regs_p->plug_state);
  489. writel(~0x0, &udc_regs_p->endp_int);
  490. writel(~0x0, &udc_regs_p->dev_int_mask);
  491. writel(~0x0, &udc_regs_p->endp_int_mask);
  492. writel(DEV_CONF_FS_SPEED | DEV_CONF_REMWAKEUP | DEV_CONF_SELFPOW |
  493. /* Dev_Conf_SYNCFRAME | */
  494. DEV_CONF_PHYINT_16, &udc_regs_p->dev_conf);
  495. writel(0x0, &udc_regs_p->dev_cntl);
  496. /* Clear all interrupts pending */
  497. writel(DEV_INT_MSK, &udc_regs_p->dev_int);
  498. return 0;
  499. }
  500. /*
  501. * udc_setup_ep - setup endpoint
  502. * Associate a physical endpoint with endpoint_instance
  503. */
  504. void udc_setup_ep(struct usb_device_instance *device,
  505. u32 ep, struct usb_endpoint_instance *endpoint)
  506. {
  507. UDCDBGA("setting up endpoint addr %x", endpoint->endpoint_address);
  508. int ep_addr;
  509. int ep_num, ep_type;
  510. int packet_size;
  511. int buffer_size;
  512. int attributes;
  513. char *tt;
  514. u32 endp_intmask;
  515. tt = getenv("usbtty");
  516. if (!tt)
  517. tt = "generic";
  518. ep_addr = endpoint->endpoint_address;
  519. ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  520. if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  521. /* IN endpoint */
  522. packet_size = endpoint->tx_packetSize;
  523. buffer_size = packet_size * 2;
  524. attributes = endpoint->tx_attributes;
  525. } else {
  526. /* OUT endpoint */
  527. packet_size = endpoint->rcv_packetSize;
  528. buffer_size = packet_size * 2;
  529. attributes = endpoint->rcv_attributes;
  530. }
  531. switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
  532. case USB_ENDPOINT_XFER_CONTROL:
  533. ep_type = ENDP_EPTYPE_CNTL;
  534. break;
  535. case USB_ENDPOINT_XFER_BULK:
  536. default:
  537. ep_type = ENDP_EPTYPE_BULK;
  538. break;
  539. case USB_ENDPOINT_XFER_INT:
  540. ep_type = ENDP_EPTYPE_INT;
  541. break;
  542. case USB_ENDPOINT_XFER_ISOC:
  543. ep_type = ENDP_EPTYPE_ISO;
  544. break;
  545. }
  546. struct udc_endp_regs *out_p = &outep_regs_p[ep_num];
  547. struct udc_endp_regs *in_p = &inep_regs_p[ep_num];
  548. if (!ep_addr) {
  549. /* Setup endpoint 0 */
  550. buffer_size = packet_size;
  551. writel(readl(&in_p->endp_cntl) | ENDP_CNTL_CNAK,
  552. &in_p->endp_cntl);
  553. writel(readl(&out_p->endp_cntl) | ENDP_CNTL_CNAK,
  554. &out_p->endp_cntl);
  555. writel(ENDP_CNTL_CONTROL | ENDP_CNTL_FLUSH, &in_p->endp_cntl);
  556. writel(buffer_size / sizeof(int), &in_p->endp_bsorfn);
  557. writel(packet_size, &in_p->endp_maxpacksize);
  558. writel(ENDP_CNTL_CONTROL | ENDP_CNTL_RRDY, &out_p->endp_cntl);
  559. writel(packet_size | ((buffer_size / sizeof(int)) << 16),
  560. &out_p->endp_maxpacksize);
  561. writel((packet_size << 19) | ENDP_EPTYPE_CNTL,
  562. &udc_regs_p->udc_endp_reg[ep_num]);
  563. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  564. /* Setup the IN endpoint */
  565. writel(0x0, &in_p->endp_status);
  566. writel((ep_type << 4) | ENDP_CNTL_RRDY, &in_p->endp_cntl);
  567. writel(buffer_size / sizeof(int), &in_p->endp_bsorfn);
  568. writel(packet_size, &in_p->endp_maxpacksize);
  569. if (!strcmp(tt, "cdc_acm")) {
  570. if (ep_type == ENDP_EPTYPE_INT) {
  571. /* Conf no. 1 Interface no. 0 */
  572. writel((packet_size << 19) |
  573. ENDP_EPDIR_IN | (1 << 7) |
  574. (0 << 11) | (ep_type << 5) | ep_num,
  575. &udc_regs_p->udc_endp_reg[ep_num]);
  576. } else {
  577. /* Conf no. 1 Interface no. 1 */
  578. writel((packet_size << 19) |
  579. ENDP_EPDIR_IN | (1 << 7) |
  580. (1 << 11) | (ep_type << 5) | ep_num,
  581. &udc_regs_p->udc_endp_reg[ep_num]);
  582. }
  583. } else {
  584. /* Conf no. 1 Interface no. 0 */
  585. writel((packet_size << 19) |
  586. ENDP_EPDIR_IN | (1 << 7) |
  587. (0 << 11) | (ep_type << 5) | ep_num,
  588. &udc_regs_p->udc_endp_reg[ep_num]);
  589. }
  590. } else {
  591. /* Setup the OUT endpoint */
  592. writel(0x0, &out_p->endp_status);
  593. writel((ep_type << 4) | ENDP_CNTL_RRDY, &out_p->endp_cntl);
  594. writel(packet_size | ((buffer_size / sizeof(int)) << 16),
  595. &out_p->endp_maxpacksize);
  596. if (!strcmp(tt, "cdc_acm")) {
  597. writel((packet_size << 19) |
  598. ENDP_EPDIR_OUT | (1 << 7) |
  599. (1 << 11) | (ep_type << 5) | ep_num,
  600. &udc_regs_p->udc_endp_reg[ep_num]);
  601. } else {
  602. writel((packet_size << 19) |
  603. ENDP_EPDIR_OUT | (1 << 7) |
  604. (0 << 11) | (ep_type << 5) | ep_num,
  605. &udc_regs_p->udc_endp_reg[ep_num]);
  606. }
  607. }
  608. endp_intmask = readl(&udc_regs_p->endp_int_mask);
  609. endp_intmask &= ~((1 << ep_num) | 0x10000 << ep_num);
  610. writel(endp_intmask, &udc_regs_p->endp_int_mask);
  611. }
  612. /* Turn on the USB connection by enabling the pullup resistor */
  613. void udc_connect(void)
  614. {
  615. u32 plug_st;
  616. plug_st = readl(&plug_regs_p->plug_state);
  617. plug_st &= ~(PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
  618. writel(plug_st, &plug_regs_p->plug_state);
  619. }
  620. /* Turn off the USB connection by disabling the pullup resistor */
  621. void udc_disconnect(void)
  622. {
  623. u32 plug_st;
  624. plug_st = readl(&plug_regs_p->plug_state);
  625. plug_st |= (PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
  626. writel(plug_st, &plug_regs_p->plug_state);
  627. }
  628. /* Switch on the UDC */
  629. void udc_enable(struct usb_device_instance *device)
  630. {
  631. UDCDBGA("enable device %p, status %d", device, device->status);
  632. /* Save the device structure pointer */
  633. udc_device = device;
  634. /* Setup ep0 urb */
  635. if (!ep0_urb) {
  636. ep0_urb =
  637. usbd_alloc_urb(udc_device, udc_device->bus->endpoint_array);
  638. } else {
  639. serial_printf("udc_enable: ep0_urb already allocated %p\n",
  640. ep0_urb);
  641. }
  642. writel(DEV_INT_SOF, &udc_regs_p->dev_int_mask);
  643. }
  644. /**
  645. * udc_startup - allow udc code to do any additional startup
  646. */
  647. void udc_startup_events(struct usb_device_instance *device)
  648. {
  649. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
  650. usbd_device_event_irq(device, DEVICE_INIT, 0);
  651. /*
  652. * The DEVICE_CREATE event puts the USB device in the state
  653. * STATE_ATTACHED.
  654. */
  655. usbd_device_event_irq(device, DEVICE_CREATE, 0);
  656. /*
  657. * Some USB controller driver implementations signal
  658. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
  659. * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
  660. * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
  661. * The DW USB client controller has the capability to detect when the
  662. * USB cable is connected to a powered USB bus, so we will defer the
  663. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events until later.
  664. */
  665. udc_enable(device);
  666. }
  667. /*
  668. * Plug detection interrupt handling
  669. */
  670. void dw_udc_plug_irq(void)
  671. {
  672. if (readl(&plug_regs_p->plug_state) & PLUG_STATUS_ATTACHED) {
  673. /*
  674. * USB cable attached
  675. * Turn off PHY reset bit (PLUG detect).
  676. * Switch PHY opmode to normal operation (PLUG detect).
  677. */
  678. udc_connect();
  679. writel(DEV_INT_SOF, &udc_regs_p->dev_int_mask);
  680. UDCDBG("device attached and powered");
  681. udc_state_transition(udc_device->device_state, STATE_POWERED);
  682. } else {
  683. /*
  684. * USB cable detached
  685. * Reset the PHY and switch the mode.
  686. */
  687. udc_disconnect();
  688. writel(~0x0, &udc_regs_p->dev_int_mask);
  689. UDCDBG("device detached or unpowered");
  690. udc_state_transition(udc_device->device_state, STATE_ATTACHED);
  691. }
  692. }
  693. /*
  694. * Device interrupt handling
  695. */
  696. void dw_udc_dev_irq(void)
  697. {
  698. if (readl(&udc_regs_p->dev_int) & DEV_INT_USBRESET) {
  699. writel(~0x0, &udc_regs_p->endp_int_mask);
  700. udc_connect();
  701. writel(readl(&inep_regs_p[0].endp_cntl) | ENDP_CNTL_FLUSH,
  702. &inep_regs_p[0].endp_cntl);
  703. writel(DEV_INT_USBRESET, &udc_regs_p->dev_int);
  704. UDCDBG("device reset in progess");
  705. udc_state_transition(udc_device->device_state, STATE_DEFAULT);
  706. }
  707. /* Device Enumeration completed */
  708. if (readl(&udc_regs_p->dev_int) & DEV_INT_ENUM) {
  709. writel(DEV_INT_ENUM, &udc_regs_p->dev_int);
  710. /* Endpoint interrupt enabled for Ctrl IN & Ctrl OUT */
  711. writel(readl(&udc_regs_p->endp_int_mask) & ~0x10001,
  712. &udc_regs_p->endp_int_mask);
  713. UDCDBG("default -> addressed");
  714. udc_state_transition(udc_device->device_state, STATE_ADDRESSED);
  715. }
  716. /* The USB will be in SUSPEND in 3 ms */
  717. if (readl(&udc_regs_p->dev_int) & DEV_INT_INACTIVE) {
  718. writel(DEV_INT_INACTIVE, &udc_regs_p->dev_int);
  719. UDCDBG("entering inactive state");
  720. /* usbd_device_event_irq(udc_device, DEVICE_BUS_INACTIVE, 0); */
  721. }
  722. /* SetConfiguration command received */
  723. if (readl(&udc_regs_p->dev_int) & DEV_INT_SETCFG) {
  724. writel(DEV_INT_SETCFG, &udc_regs_p->dev_int);
  725. UDCDBG("entering configured state");
  726. udc_state_transition(udc_device->device_state,
  727. STATE_CONFIGURED);
  728. }
  729. /* SetInterface command received */
  730. if (readl(&udc_regs_p->dev_int) & DEV_INT_SETINTF)
  731. writel(DEV_INT_SETINTF, &udc_regs_p->dev_int);
  732. /* USB Suspend detected on cable */
  733. if (readl(&udc_regs_p->dev_int) & DEV_INT_SUSPUSB) {
  734. writel(DEV_INT_SUSPUSB, &udc_regs_p->dev_int);
  735. UDCDBG("entering suspended state");
  736. usbd_device_event_irq(udc_device, DEVICE_BUS_INACTIVE, 0);
  737. }
  738. /* USB Start-Of-Frame detected on cable */
  739. if (readl(&udc_regs_p->dev_int) & DEV_INT_SOF)
  740. writel(DEV_INT_SOF, &udc_regs_p->dev_int);
  741. }
  742. /*
  743. * Endpoint interrupt handling
  744. */
  745. void dw_udc_endpoint_irq(void)
  746. {
  747. while (readl(&udc_regs_p->endp_int) & ENDP0_INT_CTRLOUT) {
  748. writel(ENDP0_INT_CTRLOUT, &udc_regs_p->endp_int);
  749. if ((readl(&outep_regs_p[0].endp_status) & ENDP_STATUS_OUTMSK)
  750. == ENDP_STATUS_OUT_SETUP) {
  751. dw_udc_setup(udc_device->bus->endpoint_array + 0);
  752. writel(ENDP_STATUS_OUT_SETUP,
  753. &outep_regs_p[0].endp_status);
  754. } else if ((readl(&outep_regs_p[0].endp_status) &
  755. ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_DATA) {
  756. dw_udc_ep0_rx(udc_device->bus->endpoint_array + 0);
  757. writel(ENDP_STATUS_OUT_DATA,
  758. &outep_regs_p[0].endp_status);
  759. } else if ((readl(&outep_regs_p[0].endp_status) &
  760. ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_NONE) {
  761. /* NONE received */
  762. }
  763. writel(0x0, &outep_regs_p[0].endp_status);
  764. }
  765. if (readl(&udc_regs_p->endp_int) & ENDP0_INT_CTRLIN) {
  766. dw_udc_ep0_tx(udc_device->bus->endpoint_array + 0);
  767. writel(ENDP_STATUS_IN, &inep_regs_p[0].endp_status);
  768. writel(ENDP0_INT_CTRLIN, &udc_regs_p->endp_int);
  769. }
  770. while (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOOUT_MSK) {
  771. u32 epnum = 0;
  772. u32 ep_int = readl(&udc_regs_p->endp_int) &
  773. ENDP_INT_NONISOOUT_MSK;
  774. ep_int >>= 16;
  775. while (0x0 == (ep_int & 0x1)) {
  776. ep_int >>= 1;
  777. epnum++;
  778. }
  779. writel((1 << 16) << epnum, &udc_regs_p->endp_int);
  780. if ((readl(&outep_regs_p[epnum].endp_status) &
  781. ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_DATA) {
  782. dw_udc_epn_rx(epnum);
  783. writel(ENDP_STATUS_OUT_DATA,
  784. &outep_regs_p[epnum].endp_status);
  785. } else if ((readl(&outep_regs_p[epnum].endp_status) &
  786. ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_NONE) {
  787. writel(0x0, &outep_regs_p[epnum].endp_status);
  788. }
  789. }
  790. if (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOIN_MSK) {
  791. u32 epnum = 0;
  792. u32 ep_int = readl(&udc_regs_p->endp_int) &
  793. ENDP_INT_NONISOIN_MSK;
  794. while (0x0 == (ep_int & 0x1)) {
  795. ep_int >>= 1;
  796. epnum++;
  797. }
  798. if (readl(&inep_regs_p[epnum].endp_status) & ENDP_STATUS_IN) {
  799. writel(ENDP_STATUS_IN,
  800. &outep_regs_p[epnum].endp_status);
  801. dw_udc_epn_tx(epnum);
  802. writel(ENDP_STATUS_IN,
  803. &outep_regs_p[epnum].endp_status);
  804. }
  805. writel((1 << epnum), &udc_regs_p->endp_int);
  806. }
  807. }
  808. /*
  809. * UDC interrupts
  810. */
  811. void udc_irq(void)
  812. {
  813. /*
  814. * Loop while we have interrupts.
  815. * If we don't do this, the input chain
  816. * polling delay is likely to miss
  817. * host requests.
  818. */
  819. while (readl(&plug_regs_p->plug_pending))
  820. dw_udc_plug_irq();
  821. while (readl(&udc_regs_p->dev_int))
  822. dw_udc_dev_irq();
  823. if (readl(&udc_regs_p->endp_int))
  824. dw_udc_endpoint_irq();
  825. }
  826. /* Flow control */
  827. void udc_set_nak(int epid)
  828. {
  829. writel(readl(&inep_regs_p[epid].endp_cntl) | ENDP_CNTL_SNAK,
  830. &inep_regs_p[epid].endp_cntl);
  831. writel(readl(&outep_regs_p[epid].endp_cntl) | ENDP_CNTL_SNAK,
  832. &outep_regs_p[epid].endp_cntl);
  833. }
  834. void udc_unset_nak(int epid)
  835. {
  836. u32 val;
  837. val = readl(&inep_regs_p[epid].endp_cntl);
  838. val &= ~ENDP_CNTL_SNAK;
  839. val |= ENDP_CNTL_CNAK;
  840. writel(val, &inep_regs_p[epid].endp_cntl);
  841. val = readl(&outep_regs_p[epid].endp_cntl);
  842. val &= ~ENDP_CNTL_SNAK;
  843. val |= ENDP_CNTL_CNAK;
  844. writel(val, &outep_regs_p[epid].endp_cntl);
  845. }