designware_udc.c 27 KB

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