designware_udc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. if (!endpoint)
  442. return;
  443. /*
  444. * We need to transmit a terminating zero-length packet now if
  445. * we have sent all of the data in this URB and the transfer
  446. * size was an exact multiple of the packet size.
  447. */
  448. if (endpoint->tx_urb &&
  449. (endpoint->last == endpoint->tx_packetSize) &&
  450. (endpoint->tx_urb->actual_length - endpoint->sent -
  451. endpoint->last == 0)) {
  452. /* handle zero length packet here */
  453. writel(0x0, &inep_regs_p[ep].write_done);
  454. }
  455. if (endpoint->tx_urb && endpoint->tx_urb->actual_length) {
  456. /* retire the data that was just sent */
  457. usbd_tx_complete(endpoint);
  458. /*
  459. * Check to see if we have more data ready to transmit
  460. * now.
  461. */
  462. if (endpoint->tx_urb && endpoint->tx_urb->actual_length) {
  463. /* write data to FIFO */
  464. dw_write_noniso_tx_fifo(endpoint);
  465. writel(0x0, &inep_regs_p[ep].write_done);
  466. } else if (endpoint->tx_urb
  467. && (endpoint->tx_urb->actual_length == 0)) {
  468. /* udc_set_nak(ep); */
  469. }
  470. }
  471. }
  472. /*
  473. * Start of public functions.
  474. */
  475. /* Called to start packet transmission. */
  476. int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
  477. {
  478. udc_unset_nak(endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK);
  479. return 0;
  480. }
  481. /* Start to initialize h/w stuff */
  482. int udc_init(void)
  483. {
  484. int i;
  485. u32 plug_st;
  486. udc_device = NULL;
  487. UDCDBG("starting");
  488. readl(&plug_regs_p->plug_pending);
  489. for (i = 0; i < UDC_INIT_MDELAY; i++)
  490. udelay(1000);
  491. plug_st = readl(&plug_regs_p->plug_state);
  492. writel(plug_st | PLUG_STATUS_EN, &plug_regs_p->plug_state);
  493. writel(~0x0, &udc_regs_p->endp_int);
  494. writel(~0x0, &udc_regs_p->dev_int_mask);
  495. writel(~0x0, &udc_regs_p->endp_int_mask);
  496. #ifndef CONFIG_USBD_HS
  497. writel(DEV_CONF_FS_SPEED | DEV_CONF_REMWAKEUP | DEV_CONF_SELFPOW |
  498. DEV_CONF_PHYINT_16, &udc_regs_p->dev_conf);
  499. #else
  500. writel(DEV_CONF_HS_SPEED | DEV_CONF_REMWAKEUP | DEV_CONF_SELFPOW |
  501. DEV_CONF_PHYINT_16, &udc_regs_p->dev_conf);
  502. #endif
  503. writel(DEV_CNTL_SOFTDISCONNECT, &udc_regs_p->dev_cntl);
  504. /* Clear all interrupts pending */
  505. writel(DEV_INT_MSK, &udc_regs_p->dev_int);
  506. return 0;
  507. }
  508. int is_usbd_high_speed(void)
  509. {
  510. return (readl(&udc_regs_p->dev_stat) & DEV_STAT_ENUM) ? 0 : 1;
  511. }
  512. /*
  513. * udc_setup_ep - setup endpoint
  514. * Associate a physical endpoint with endpoint_instance
  515. */
  516. void udc_setup_ep(struct usb_device_instance *device,
  517. u32 ep, struct usb_endpoint_instance *endpoint)
  518. {
  519. UDCDBGA("setting up endpoint addr %x", endpoint->endpoint_address);
  520. int ep_addr;
  521. int ep_num, ep_type;
  522. int packet_size;
  523. int buffer_size;
  524. int attributes;
  525. char *tt;
  526. u32 endp_intmask;
  527. if ((ep != 0) && (udc_device->device_state < STATE_ADDRESSED))
  528. return;
  529. tt = getenv("usbtty");
  530. if (!tt)
  531. tt = "generic";
  532. ep_addr = endpoint->endpoint_address;
  533. ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  534. if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  535. /* IN endpoint */
  536. packet_size = endpoint->tx_packetSize;
  537. buffer_size = packet_size * 2;
  538. attributes = endpoint->tx_attributes;
  539. } else {
  540. /* OUT endpoint */
  541. packet_size = endpoint->rcv_packetSize;
  542. buffer_size = packet_size * 2;
  543. attributes = endpoint->rcv_attributes;
  544. }
  545. switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
  546. case USB_ENDPOINT_XFER_CONTROL:
  547. ep_type = ENDP_EPTYPE_CNTL;
  548. break;
  549. case USB_ENDPOINT_XFER_BULK:
  550. default:
  551. ep_type = ENDP_EPTYPE_BULK;
  552. break;
  553. case USB_ENDPOINT_XFER_INT:
  554. ep_type = ENDP_EPTYPE_INT;
  555. break;
  556. case USB_ENDPOINT_XFER_ISOC:
  557. ep_type = ENDP_EPTYPE_ISO;
  558. break;
  559. }
  560. struct udc_endp_regs *out_p = &outep_regs_p[ep_num];
  561. struct udc_endp_regs *in_p = &inep_regs_p[ep_num];
  562. if (!ep_addr) {
  563. /* Setup endpoint 0 */
  564. buffer_size = packet_size;
  565. writel(readl(&in_p->endp_cntl) | ENDP_CNTL_CNAK,
  566. &in_p->endp_cntl);
  567. writel(readl(&out_p->endp_cntl) | ENDP_CNTL_CNAK,
  568. &out_p->endp_cntl);
  569. writel(ENDP_CNTL_CONTROL | ENDP_CNTL_FLUSH, &in_p->endp_cntl);
  570. writel(buffer_size / sizeof(int), &in_p->endp_bsorfn);
  571. writel(packet_size, &in_p->endp_maxpacksize);
  572. writel(ENDP_CNTL_CONTROL | ENDP_CNTL_RRDY, &out_p->endp_cntl);
  573. writel(packet_size | ((buffer_size / sizeof(int)) << 16),
  574. &out_p->endp_maxpacksize);
  575. } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
  576. /* Setup the IN endpoint */
  577. writel(0x0, &in_p->endp_status);
  578. writel((ep_type << 4) | ENDP_CNTL_RRDY, &in_p->endp_cntl);
  579. writel(buffer_size / sizeof(int), &in_p->endp_bsorfn);
  580. writel(packet_size, &in_p->endp_maxpacksize);
  581. if (!strcmp(tt, "cdc_acm")) {
  582. if (ep_type == ENDP_EPTYPE_INT) {
  583. /* Conf no. 1 Interface no. 0 */
  584. writel((packet_size << 19) |
  585. ENDP_EPDIR_IN | (1 << 7) |
  586. (0 << 11) | (ep_type << 5) | ep_num,
  587. &udc_regs_p->udc_endp_reg[ep_num]);
  588. } else {
  589. /* Conf no. 1 Interface no. 1 */
  590. writel((packet_size << 19) |
  591. ENDP_EPDIR_IN | (1 << 7) |
  592. (1 << 11) | (ep_type << 5) | ep_num,
  593. &udc_regs_p->udc_endp_reg[ep_num]);
  594. }
  595. } else {
  596. /* Conf no. 1 Interface no. 0 */
  597. writel((packet_size << 19) |
  598. ENDP_EPDIR_IN | (1 << 7) |
  599. (0 << 11) | (ep_type << 5) | ep_num,
  600. &udc_regs_p->udc_endp_reg[ep_num]);
  601. }
  602. } else {
  603. /* Setup the OUT endpoint */
  604. writel(0x0, &out_p->endp_status);
  605. writel((ep_type << 4) | ENDP_CNTL_RRDY, &out_p->endp_cntl);
  606. writel(packet_size | ((buffer_size / sizeof(int)) << 16),
  607. &out_p->endp_maxpacksize);
  608. if (!strcmp(tt, "cdc_acm")) {
  609. writel((packet_size << 19) |
  610. ENDP_EPDIR_OUT | (1 << 7) |
  611. (1 << 11) | (ep_type << 5) | ep_num,
  612. &udc_regs_p->udc_endp_reg[ep_num]);
  613. } else {
  614. writel((packet_size << 19) |
  615. ENDP_EPDIR_OUT | (1 << 7) |
  616. (0 << 11) | (ep_type << 5) | ep_num,
  617. &udc_regs_p->udc_endp_reg[ep_num]);
  618. }
  619. }
  620. endp_intmask = readl(&udc_regs_p->endp_int_mask);
  621. endp_intmask &= ~((1 << ep_num) | 0x10000 << ep_num);
  622. writel(endp_intmask, &udc_regs_p->endp_int_mask);
  623. }
  624. /* Turn on the USB connection by enabling the pullup resistor */
  625. void udc_connect(void)
  626. {
  627. u32 plug_st, dev_cntl;
  628. dev_cntl = readl(&udc_regs_p->dev_cntl);
  629. dev_cntl |= DEV_CNTL_SOFTDISCONNECT;
  630. writel(dev_cntl, &udc_regs_p->dev_cntl);
  631. udelay(1000);
  632. dev_cntl = readl(&udc_regs_p->dev_cntl);
  633. dev_cntl &= ~DEV_CNTL_SOFTDISCONNECT;
  634. writel(dev_cntl, &udc_regs_p->dev_cntl);
  635. plug_st = readl(&plug_regs_p->plug_state);
  636. plug_st &= ~(PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
  637. writel(plug_st, &plug_regs_p->plug_state);
  638. }
  639. /* Turn off the USB connection by disabling the pullup resistor */
  640. void udc_disconnect(void)
  641. {
  642. u32 plug_st;
  643. writel(DEV_CNTL_SOFTDISCONNECT, &udc_regs_p->dev_cntl);
  644. plug_st = readl(&plug_regs_p->plug_state);
  645. plug_st |= (PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
  646. writel(plug_st, &plug_regs_p->plug_state);
  647. }
  648. /* Switch on the UDC */
  649. void udc_enable(struct usb_device_instance *device)
  650. {
  651. UDCDBGA("enable device %p, status %d", device, device->status);
  652. /* Save the device structure pointer */
  653. udc_device = device;
  654. /* Setup ep0 urb */
  655. if (!ep0_urb) {
  656. ep0_urb =
  657. usbd_alloc_urb(udc_device, udc_device->bus->endpoint_array);
  658. } else {
  659. serial_printf("udc_enable: ep0_urb already allocated %p\n",
  660. ep0_urb);
  661. }
  662. writel(DEV_INT_SOF, &udc_regs_p->dev_int_mask);
  663. }
  664. /**
  665. * udc_startup - allow udc code to do any additional startup
  666. */
  667. void udc_startup_events(struct usb_device_instance *device)
  668. {
  669. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
  670. usbd_device_event_irq(device, DEVICE_INIT, 0);
  671. /*
  672. * The DEVICE_CREATE event puts the USB device in the state
  673. * STATE_ATTACHED.
  674. */
  675. usbd_device_event_irq(device, DEVICE_CREATE, 0);
  676. /*
  677. * Some USB controller driver implementations signal
  678. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
  679. * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
  680. * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
  681. * The DW USB client controller has the capability to detect when the
  682. * USB cable is connected to a powered USB bus, so we will defer the
  683. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events until later.
  684. */
  685. udc_enable(device);
  686. }
  687. /*
  688. * Plug detection interrupt handling
  689. */
  690. void dw_udc_plug_irq(void)
  691. {
  692. if (readl(&plug_regs_p->plug_state) & PLUG_STATUS_ATTACHED) {
  693. /*
  694. * USB cable attached
  695. * Turn off PHY reset bit (PLUG detect).
  696. * Switch PHY opmode to normal operation (PLUG detect).
  697. */
  698. udc_connect();
  699. writel(DEV_INT_SOF, &udc_regs_p->dev_int_mask);
  700. UDCDBG("device attached and powered");
  701. udc_state_transition(udc_device->device_state, STATE_POWERED);
  702. } else {
  703. writel(~0x0, &udc_regs_p->dev_int_mask);
  704. UDCDBG("device detached or unpowered");
  705. udc_state_transition(udc_device->device_state, STATE_ATTACHED);
  706. }
  707. }
  708. /*
  709. * Device interrupt handling
  710. */
  711. void dw_udc_dev_irq(void)
  712. {
  713. if (readl(&udc_regs_p->dev_int) & DEV_INT_USBRESET) {
  714. writel(~0x0, &udc_regs_p->endp_int_mask);
  715. writel(readl(&inep_regs_p[0].endp_cntl) | ENDP_CNTL_FLUSH,
  716. &inep_regs_p[0].endp_cntl);
  717. writel(DEV_INT_USBRESET, &udc_regs_p->dev_int);
  718. /*
  719. * This endpoint0 specific register can be programmed only
  720. * after the phy clock is initialized
  721. */
  722. writel((EP0_MAX_PACKET_SIZE << 19) | ENDP_EPTYPE_CNTL,
  723. &udc_regs_p->udc_endp_reg[0]);
  724. UDCDBG("device reset in progess");
  725. udc_state_transition(udc_device->device_state, STATE_DEFAULT);
  726. }
  727. /* Device Enumeration completed */
  728. if (readl(&udc_regs_p->dev_int) & DEV_INT_ENUM) {
  729. writel(DEV_INT_ENUM, &udc_regs_p->dev_int);
  730. /* Endpoint interrupt enabled for Ctrl IN & Ctrl OUT */
  731. writel(readl(&udc_regs_p->endp_int_mask) & ~0x10001,
  732. &udc_regs_p->endp_int_mask);
  733. UDCDBG("default -> addressed");
  734. udc_state_transition(udc_device->device_state, STATE_ADDRESSED);
  735. }
  736. /* The USB will be in SUSPEND in 3 ms */
  737. if (readl(&udc_regs_p->dev_int) & DEV_INT_INACTIVE) {
  738. writel(DEV_INT_INACTIVE, &udc_regs_p->dev_int);
  739. UDCDBG("entering inactive state");
  740. /* usbd_device_event_irq(udc_device, DEVICE_BUS_INACTIVE, 0); */
  741. }
  742. /* SetConfiguration command received */
  743. if (readl(&udc_regs_p->dev_int) & DEV_INT_SETCFG) {
  744. writel(DEV_INT_SETCFG, &udc_regs_p->dev_int);
  745. UDCDBG("entering configured state");
  746. udc_state_transition(udc_device->device_state,
  747. STATE_CONFIGURED);
  748. }
  749. /* SetInterface command received */
  750. if (readl(&udc_regs_p->dev_int) & DEV_INT_SETINTF)
  751. writel(DEV_INT_SETINTF, &udc_regs_p->dev_int);
  752. /* USB Suspend detected on cable */
  753. if (readl(&udc_regs_p->dev_int) & DEV_INT_SUSPUSB) {
  754. writel(DEV_INT_SUSPUSB, &udc_regs_p->dev_int);
  755. UDCDBG("entering suspended state");
  756. usbd_device_event_irq(udc_device, DEVICE_BUS_INACTIVE, 0);
  757. }
  758. /* USB Start-Of-Frame detected on cable */
  759. if (readl(&udc_regs_p->dev_int) & DEV_INT_SOF)
  760. writel(DEV_INT_SOF, &udc_regs_p->dev_int);
  761. }
  762. /*
  763. * Endpoint interrupt handling
  764. */
  765. void dw_udc_endpoint_irq(void)
  766. {
  767. while (readl(&udc_regs_p->endp_int) & ENDP0_INT_CTRLOUT) {
  768. writel(ENDP0_INT_CTRLOUT, &udc_regs_p->endp_int);
  769. if ((readl(&outep_regs_p[0].endp_status) & ENDP_STATUS_OUTMSK)
  770. == ENDP_STATUS_OUT_SETUP) {
  771. dw_udc_setup(udc_device->bus->endpoint_array + 0);
  772. writel(ENDP_STATUS_OUT_SETUP,
  773. &outep_regs_p[0].endp_status);
  774. } else if ((readl(&outep_regs_p[0].endp_status) &
  775. ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_DATA) {
  776. dw_udc_ep0_rx(udc_device->bus->endpoint_array + 0);
  777. writel(ENDP_STATUS_OUT_DATA,
  778. &outep_regs_p[0].endp_status);
  779. } else if ((readl(&outep_regs_p[0].endp_status) &
  780. ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_NONE) {
  781. /* NONE received */
  782. }
  783. writel(0x0, &outep_regs_p[0].endp_status);
  784. }
  785. if (readl(&udc_regs_p->endp_int) & ENDP0_INT_CTRLIN) {
  786. dw_udc_ep0_tx(udc_device->bus->endpoint_array + 0);
  787. writel(ENDP_STATUS_IN, &inep_regs_p[0].endp_status);
  788. writel(ENDP0_INT_CTRLIN, &udc_regs_p->endp_int);
  789. }
  790. if (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOOUT_MSK) {
  791. u32 epnum = 0;
  792. u32 ep_int = readl(&udc_regs_p->endp_int) &
  793. ENDP_INT_NONISOOUT_MSK;
  794. ep_int >>= 16;
  795. while (0x0 == (ep_int & 0x1)) {
  796. ep_int >>= 1;
  797. epnum++;
  798. }
  799. writel((1 << 16) << epnum, &udc_regs_p->endp_int);
  800. if ((readl(&outep_regs_p[epnum].endp_status) &
  801. ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_DATA) {
  802. dw_udc_epn_rx(epnum);
  803. writel(ENDP_STATUS_OUT_DATA,
  804. &outep_regs_p[epnum].endp_status);
  805. } else if ((readl(&outep_regs_p[epnum].endp_status) &
  806. ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_NONE) {
  807. writel(0x0, &outep_regs_p[epnum].endp_status);
  808. }
  809. }
  810. if (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOIN_MSK) {
  811. u32 epnum = 0;
  812. u32 ep_int = readl(&udc_regs_p->endp_int) &
  813. ENDP_INT_NONISOIN_MSK;
  814. while (0x0 == (ep_int & 0x1)) {
  815. ep_int >>= 1;
  816. epnum++;
  817. }
  818. if (readl(&inep_regs_p[epnum].endp_status) & ENDP_STATUS_IN) {
  819. writel(ENDP_STATUS_IN,
  820. &outep_regs_p[epnum].endp_status);
  821. dw_udc_epn_tx(epnum);
  822. writel(ENDP_STATUS_IN,
  823. &outep_regs_p[epnum].endp_status);
  824. }
  825. writel((1 << epnum), &udc_regs_p->endp_int);
  826. }
  827. }
  828. /*
  829. * UDC interrupts
  830. */
  831. void udc_irq(void)
  832. {
  833. /*
  834. * Loop while we have interrupts.
  835. * If we don't do this, the input chain
  836. * polling delay is likely to miss
  837. * host requests.
  838. */
  839. while (readl(&plug_regs_p->plug_pending))
  840. dw_udc_plug_irq();
  841. while (readl(&udc_regs_p->dev_int))
  842. dw_udc_dev_irq();
  843. if (readl(&udc_regs_p->endp_int))
  844. dw_udc_endpoint_irq();
  845. }
  846. /* Flow control */
  847. void udc_set_nak(int epid)
  848. {
  849. writel(readl(&inep_regs_p[epid].endp_cntl) | ENDP_CNTL_SNAK,
  850. &inep_regs_p[epid].endp_cntl);
  851. writel(readl(&outep_regs_p[epid].endp_cntl) | ENDP_CNTL_SNAK,
  852. &outep_regs_p[epid].endp_cntl);
  853. }
  854. void udc_unset_nak(int epid)
  855. {
  856. u32 val;
  857. val = readl(&inep_regs_p[epid].endp_cntl);
  858. val &= ~ENDP_CNTL_SNAK;
  859. val |= ENDP_CNTL_CNAK;
  860. writel(val, &inep_regs_p[epid].endp_cntl);
  861. val = readl(&outep_regs_p[epid].endp_cntl);
  862. val &= ~ENDP_CNTL_SNAK;
  863. val |= ENDP_CNTL_CNAK;
  864. writel(val, &outep_regs_p[epid].endp_cntl);
  865. }