designware_udc.c 26 KB

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