designware_udc.c 26 KB

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