pxa27x_udc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * PXA27x USB device driver for u-boot.
  3. *
  4. * Copyright (C) 2007 Rodolfo Giometti <giometti@linux.it>
  5. * Copyright (C) 2007 Eurotech S.p.A. <info@eurotech.it>
  6. * Copyright (C) 2008 Vivek Kutal <vivek.kutal@azingo.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <config.h>
  12. #include <asm/byteorder.h>
  13. #include <usbdevice.h>
  14. #include <asm/arch/hardware.h>
  15. #include <asm/io.h>
  16. #include <usb/pxa27x_udc.h>
  17. #include <usb/udc.h>
  18. #include "ep0.h"
  19. /* number of endpoints on this UDC */
  20. #define UDC_MAX_ENDPOINTS 24
  21. static struct urb *ep0_urb;
  22. static struct usb_device_instance *udc_device;
  23. static int ep0state = EP0_IDLE;
  24. #ifdef USBDDBG
  25. static void udc_dump_buffer(char *name, u8 *buf, int len)
  26. {
  27. usbdbg("%s - buf %p, len %d", name, buf, len);
  28. print_buffer(0, buf, 1, len, 0);
  29. }
  30. #else
  31. #define udc_dump_buffer(name, buf, len) /* void */
  32. #endif
  33. static inline void udc_ack_int_UDCCR(int mask)
  34. {
  35. writel(readl(USIR1) | mask, USIR1);
  36. }
  37. /*
  38. * If the endpoint has an active tx_urb, then the next packet of data from the
  39. * URB is written to the tx FIFO.
  40. * The total amount of data in the urb is given by urb->actual_length.
  41. * The maximum amount of data that can be sent in any one packet is given by
  42. * endpoint->tx_packetSize.
  43. * The number of data bytes from this URB that have already been transmitted
  44. * is given by endpoint->sent.
  45. * endpoint->last is updated by this routine with the number of data bytes
  46. * transmitted in this packet.
  47. */
  48. static int udc_write_urb(struct usb_endpoint_instance *endpoint)
  49. {
  50. struct urb *urb = endpoint->tx_urb;
  51. int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
  52. u32 *data32 = (u32 *) urb->buffer;
  53. u8 *data8 = (u8 *) urb->buffer;
  54. unsigned int i, n, w, b, is_short;
  55. int timeout = 2000; /* 2ms */
  56. if (!urb || !urb->actual_length)
  57. return -1;
  58. n = min_t(unsigned int, urb->actual_length - endpoint->sent,
  59. endpoint->tx_packetSize);
  60. if (n <= 0)
  61. return -1;
  62. usbdbg("write urb on ep %d", ep_num);
  63. #if defined(USBDDBG) && defined(USBDPARANOIA)
  64. usbdbg("urb: buf %p, buf_len %d, actual_len %d",
  65. urb->buffer, urb->buffer_length, urb->actual_length);
  66. usbdbg("endpoint: sent %d, tx_packetSize %d, last %d",
  67. endpoint->sent, endpoint->tx_packetSize, endpoint->last);
  68. #endif
  69. is_short = n != endpoint->tx_packetSize;
  70. w = n / 4;
  71. b = n % 4;
  72. usbdbg("n %d%s w %d b %d", n, is_short ? "-s" : "", w, b);
  73. udc_dump_buffer("urb write", data8 + endpoint->sent, n);
  74. /* Prepare for data send */
  75. if (ep_num)
  76. writel(UDCCSR_PC ,UDCCSN(ep_num));
  77. for (i = 0; i < w; i++)
  78. writel(data32[endpoint->sent / 4 + i], UDCDN(ep_num));
  79. for (i = 0; i < b; i++)
  80. writeb(data8[endpoint->sent + w * 4 + i], UDCDN(ep_num));
  81. /* Set "Packet Complete" if less data then tx_packetSize */
  82. if (is_short)
  83. writel(ep_num ? UDCCSR_SP : UDCCSR0_IPR, UDCCSN(ep_num));
  84. /* Wait for data sent */
  85. if (ep_num) {
  86. while (!(readl(UDCCSN(ep_num)) & UDCCSR_PC)) {
  87. if (timeout-- == 0)
  88. return -1;
  89. else
  90. udelay(1);
  91. }
  92. }
  93. endpoint->last = n;
  94. if (ep_num) {
  95. usbd_tx_complete(endpoint);
  96. } else {
  97. endpoint->sent += n;
  98. endpoint->last -= n;
  99. }
  100. if (endpoint->sent >= urb->actual_length) {
  101. urb->actual_length = 0;
  102. endpoint->sent = 0;
  103. endpoint->last = 0;
  104. }
  105. if ((endpoint->sent >= urb->actual_length) && (!ep_num)) {
  106. usbdbg("ep0 IN stage done");
  107. if (is_short)
  108. ep0state = EP0_IDLE;
  109. else
  110. ep0state = EP0_XFER_COMPLETE;
  111. }
  112. return 0;
  113. }
  114. static int udc_read_urb(struct usb_endpoint_instance *endpoint)
  115. {
  116. struct urb *urb = endpoint->rcv_urb;
  117. int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
  118. u32 *data32 = (u32 *) urb->buffer;
  119. unsigned int i, n;
  120. usbdbg("read urb on ep %d", ep_num);
  121. #if defined(USBDDBG) && defined(USBDPARANOIA)
  122. usbdbg("urb: buf %p, buf_len %d, actual_len %d",
  123. urb->buffer, urb->buffer_length, urb->actual_length);
  124. usbdbg("endpoint: rcv_packetSize %d",
  125. endpoint->rcv_packetSize);
  126. #endif
  127. if (readl(UDCCSN(ep_num)) & UDCCSR_BNE)
  128. n = readl(UDCBCN(ep_num)) & 0x3ff;
  129. else /* zlp */
  130. n = 0;
  131. usbdbg("n %d%s", n, n != endpoint->rcv_packetSize ? "-s" : "");
  132. for (i = 0; i < n; i += 4)
  133. data32[urb->actual_length / 4 + i / 4] = readl(UDCDN(ep_num));
  134. udc_dump_buffer("urb read", (u8 *) data32, urb->actual_length + n);
  135. usbd_rcv_complete(endpoint, n, 0);
  136. return 0;
  137. }
  138. static int udc_read_urb_ep0(void)
  139. {
  140. u32 *data32 = (u32 *) ep0_urb->buffer;
  141. u8 *data8 = (u8 *) ep0_urb->buffer;
  142. unsigned int i, n, w, b;
  143. usbdbg("read urb on ep 0");
  144. #if defined(USBDDBG) && defined(USBDPARANOIA)
  145. usbdbg("urb: buf %p, buf_len %d, actual_len %d",
  146. ep0_urb->buffer, ep0_urb->buffer_length, ep0_urb->actual_length);
  147. #endif
  148. n = readl(UDCBCR0);
  149. w = n / 4;
  150. b = n % 4;
  151. for (i = 0; i < w; i++) {
  152. data32[ep0_urb->actual_length / 4 + i] = readl(UDCDN(0));
  153. /* ep0_urb->actual_length += 4; */
  154. }
  155. for (i = 0; i < b; i++) {
  156. data8[ep0_urb->actual_length + w * 4 + i] = readb(UDCDN(0));
  157. /* ep0_urb->actual_length++; */
  158. }
  159. ep0_urb->actual_length += n;
  160. udc_dump_buffer("urb read", (u8 *) data32, ep0_urb->actual_length);
  161. writel(UDCCSR0_OPC | UDCCSR0_IPR, UDCCSR0);
  162. if (ep0_urb->actual_length == ep0_urb->device_request.wLength)
  163. return 1;
  164. return 0;
  165. }
  166. static void udc_handle_ep0(struct usb_endpoint_instance *endpoint)
  167. {
  168. u32 udccsr0 = readl(UDCCSR0);
  169. u32 *data = (u32 *) &ep0_urb->device_request;
  170. int i;
  171. usbdbg("udccsr0 %x", udccsr0);
  172. /* Clear stall status */
  173. if (udccsr0 & UDCCSR0_SST) {
  174. usberr("clear stall status");
  175. writel(UDCCSR0_SST, UDCCSR0);
  176. ep0state = EP0_IDLE;
  177. }
  178. /* previous request unfinished? non-error iff back-to-back ... */
  179. if ((udccsr0 & UDCCSR0_SA) != 0 && ep0state != EP0_IDLE)
  180. ep0state = EP0_IDLE;
  181. switch (ep0state) {
  182. case EP0_IDLE:
  183. udccsr0 = readl(UDCCSR0);
  184. /* Start control request? */
  185. if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE))
  186. == (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)) {
  187. /* Read SETUP packet.
  188. * SETUP packet size is 8 bytes (aka 2 words)
  189. */
  190. usbdbg("try reading SETUP packet");
  191. for (i = 0; i < 2; i++) {
  192. if ((readl(UDCCSR0) & UDCCSR0_RNE) == 0) {
  193. usberr("setup packet too short:%d", i);
  194. goto stall;
  195. }
  196. data[i] = readl(UDCDR0);
  197. }
  198. writel(readl(UDCCSR0) | UDCCSR0_OPC | UDCCSR0_SA, UDCCSR0);
  199. if ((readl(UDCCSR0) & UDCCSR0_RNE) != 0) {
  200. usberr("setup packet too long");
  201. goto stall;
  202. }
  203. udc_dump_buffer("ep0 setup read", (u8 *) data, 8);
  204. if (ep0_urb->device_request.wLength == 0) {
  205. usbdbg("Zero Data control Packet\n");
  206. if (ep0_recv_setup(ep0_urb)) {
  207. usberr("Invalid Setup Packet\n");
  208. udc_dump_buffer("ep0 setup read",
  209. (u8 *)data, 8);
  210. goto stall;
  211. }
  212. writel(UDCCSR0_IPR, UDCCSR0);
  213. ep0state = EP0_IDLE;
  214. } else {
  215. /* Check direction */
  216. if ((ep0_urb->device_request.bmRequestType &
  217. USB_REQ_DIRECTION_MASK)
  218. == USB_REQ_HOST2DEVICE) {
  219. ep0state = EP0_OUT_DATA;
  220. ep0_urb->buffer =
  221. (u8 *)ep0_urb->buffer_data;
  222. ep0_urb->buffer_length =
  223. sizeof(ep0_urb->buffer_data);
  224. ep0_urb->actual_length = 0;
  225. writel(UDCCSR0_IPR, UDCCSR0);
  226. } else {
  227. /* The ep0_recv_setup function has
  228. * already placed our response packet
  229. * data in ep0_urb->buffer and the
  230. * packet length in
  231. * ep0_urb->actual_length.
  232. */
  233. if (ep0_recv_setup(ep0_urb)) {
  234. stall:
  235. usberr("Invalid setup packet");
  236. udc_dump_buffer("ep0 setup read"
  237. , (u8 *) data, 8);
  238. ep0state = EP0_IDLE;
  239. writel(UDCCSR0_SA |
  240. UDCCSR0_OPC | UDCCSR0_FST |
  241. UDCCS0_FTF, UDCCSR0);
  242. return;
  243. }
  244. endpoint->tx_urb = ep0_urb;
  245. endpoint->sent = 0;
  246. usbdbg("EP0_IN_DATA");
  247. ep0state = EP0_IN_DATA;
  248. if (udc_write_urb(endpoint) < 0)
  249. goto stall;
  250. }
  251. }
  252. return;
  253. } else if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA))
  254. == (UDCCSR0_OPC|UDCCSR0_SA)) {
  255. usberr("Setup Active but no data. Stalling ....\n");
  256. goto stall;
  257. } else {
  258. usbdbg("random early IRQs");
  259. /* Some random early IRQs:
  260. * - we acked FST
  261. * - IPR cleared
  262. * - OPC got set, without SA (likely status stage)
  263. */
  264. writel(udccsr0 & (UDCCSR0_SA | UDCCSR0_OPC), UDCCSR0);
  265. }
  266. break;
  267. case EP0_OUT_DATA:
  268. if ((udccsr0 & UDCCSR0_OPC) && !(udccsr0 & UDCCSR0_SA)) {
  269. if (udc_read_urb_ep0()) {
  270. read_complete:
  271. ep0state = EP0_IDLE;
  272. if (ep0_recv_setup(ep0_urb)) {
  273. /* Not a setup packet, stall next
  274. * EP0 transaction
  275. */
  276. udc_dump_buffer("ep0 setup read",
  277. (u8 *) data, 8);
  278. usberr("can't parse setup packet\n");
  279. goto stall;
  280. }
  281. }
  282. } else if (!(udccsr0 & UDCCSR0_OPC) &&
  283. !(udccsr0 & UDCCSR0_IPR)) {
  284. if (ep0_urb->device_request.wLength ==
  285. ep0_urb->actual_length)
  286. goto read_complete;
  287. usberr("Premature Status\n");
  288. ep0state = EP0_IDLE;
  289. }
  290. break;
  291. case EP0_IN_DATA:
  292. /* GET_DESCRIPTOR etc */
  293. if (udccsr0 & UDCCSR0_OPC) {
  294. writel(UDCCSR0_OPC | UDCCSR0_FTF, UDCCSR0);
  295. usberr("ep0in premature status");
  296. ep0state = EP0_IDLE;
  297. } else {
  298. /* irq was IPR clearing */
  299. if (udc_write_urb(endpoint) < 0) {
  300. usberr("ep0_write_error\n");
  301. goto stall;
  302. }
  303. }
  304. break;
  305. case EP0_XFER_COMPLETE:
  306. writel(UDCCSR0_IPR, UDCCSR0);
  307. ep0state = EP0_IDLE;
  308. break;
  309. default:
  310. usbdbg("Default\n");
  311. }
  312. writel(USIR0_IR0, USIR0);
  313. }
  314. static void udc_handle_ep(struct usb_endpoint_instance *endpoint)
  315. {
  316. int ep_addr = endpoint->endpoint_address;
  317. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  318. int ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
  319. u32 flags = readl(UDCCSN(ep_num)) & (UDCCSR_SST | UDCCSR_TRN);
  320. if (flags)
  321. writel(flags, UDCCSN(ep_num));
  322. if (ep_isout)
  323. udc_read_urb(endpoint);
  324. else
  325. udc_write_urb(endpoint);
  326. writel(UDCCSR_PC, UDCCSN(ep_num));
  327. }
  328. static void udc_state_changed(void)
  329. {
  330. writel(readl(UDCCR) | UDCCR_SMAC, UDCCR);
  331. usbdbg("New UDC settings are: conf %d - inter %d - alter %d",
  332. (readl(UDCCR) & UDCCR_ACN) >> UDCCR_ACN_S,
  333. (readl(UDCCR) & UDCCR_AIN) >> UDCCR_AIN_S,
  334. (readl(UDCCR) & UDCCR_AAISN) >> UDCCR_AAISN_S);
  335. usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
  336. writel(UDCISR1_IRCC, UDCISR1);
  337. }
  338. void udc_irq(void)
  339. {
  340. int handled;
  341. struct usb_endpoint_instance *endpoint;
  342. int ep_num, i;
  343. u32 udcisr0;
  344. do {
  345. handled = 0;
  346. /* Suspend Interrupt Request */
  347. if (readl(USIR1) & UDCCR_SUSIR) {
  348. usbdbg("Suspend\n");
  349. udc_ack_int_UDCCR(UDCCR_SUSIR);
  350. handled = 1;
  351. ep0state = EP0_IDLE;
  352. }
  353. /* Resume Interrupt Request */
  354. if (readl(USIR1) & UDCCR_RESIR) {
  355. udc_ack_int_UDCCR(UDCCR_RESIR);
  356. handled = 1;
  357. usbdbg("USB resume\n");
  358. }
  359. if (readl(USIR1) & (1<<31)) {
  360. handled = 1;
  361. udc_state_changed();
  362. }
  363. /* Reset Interrupt Request */
  364. if (readl(USIR1) & UDCCR_RSTIR) {
  365. udc_ack_int_UDCCR(UDCCR_RSTIR);
  366. handled = 1;
  367. usbdbg("Reset\n");
  368. usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
  369. } else {
  370. if (readl(USIR0))
  371. usbdbg("UISR0: %x \n", readl(USIR0));
  372. if (readl(USIR0) & 0x2)
  373. writel(0x2, USIR0);
  374. /* Control traffic */
  375. if (readl(USIR0) & USIR0_IR0) {
  376. handled = 1;
  377. writel(USIR0_IR0, USIR0);
  378. udc_handle_ep0(udc_device->bus->endpoint_array);
  379. }
  380. endpoint = udc_device->bus->endpoint_array;
  381. for (i = 0; i < udc_device->bus->max_endpoints; i++) {
  382. ep_num = (endpoint[i].endpoint_address) &
  383. USB_ENDPOINT_NUMBER_MASK;
  384. if (!ep_num)
  385. continue;
  386. udcisr0 = readl(UDCISR0);
  387. if (udcisr0 &
  388. UDCISR_INT(ep_num, UDC_INT_PACKETCMP)) {
  389. writel(UDCISR_INT(ep_num, UDC_INT_PACKETCMP),
  390. UDCISR0);
  391. udc_handle_ep(&endpoint[i]);
  392. }
  393. }
  394. }
  395. } while (handled);
  396. }
  397. /* The UDCCR reg contains mask and interrupt status bits,
  398. * so using '|=' isn't safe as it may ack an interrupt.
  399. */
  400. #define UDCCR_OEN (1 << 31) /* On-the-Go Enable */
  401. #define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_UDE)
  402. static inline void udc_set_mask_UDCCR(int mask)
  403. {
  404. writel((readl(UDCCR) & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS), UDCCR);
  405. }
  406. static inline void udc_clear_mask_UDCCR(int mask)
  407. {
  408. writel((readl(UDCCR) & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS), UDCCR);
  409. }
  410. static void pio_irq_enable(int ep_num)
  411. {
  412. if (ep_num < 16)
  413. writel(readl(UDCICR0) | 3 << (ep_num * 2), UDCICR0);
  414. else {
  415. ep_num -= 16;
  416. writel(readl(UDCICR1) | 3 << (ep_num * 2), UDCICR1);
  417. }
  418. }
  419. /*
  420. * udc_set_nak
  421. *
  422. * Allow upper layers to signal lower layers should not accept more RX data
  423. */
  424. void udc_set_nak(int ep_num)
  425. {
  426. /* TODO */
  427. }
  428. /*
  429. * udc_unset_nak
  430. *
  431. * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
  432. * Switch off NAKing on this endpoint to accept more data output from host.
  433. */
  434. void udc_unset_nak(int ep_num)
  435. {
  436. /* TODO */
  437. }
  438. int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
  439. {
  440. return udc_write_urb(endpoint);
  441. }
  442. /* Associate a physical endpoint with endpoint instance */
  443. void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
  444. struct usb_endpoint_instance *endpoint)
  445. {
  446. int ep_num, ep_addr, ep_isout, ep_type, ep_size;
  447. int config, interface, alternate;
  448. u32 tmp;
  449. usbdbg("setting up endpoint id %d", id);
  450. if (!endpoint) {
  451. usberr("endpoint void!");
  452. return;
  453. }
  454. ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
  455. if (ep_num >= UDC_MAX_ENDPOINTS) {
  456. usberr("unable to setup ep %d!", ep_num);
  457. return;
  458. }
  459. pio_irq_enable(ep_num);
  460. if (ep_num == 0) {
  461. /* Done for ep0 */
  462. return;
  463. }
  464. config = 1;
  465. interface = 0;
  466. alternate = 0;
  467. usbdbg("config %d - interface %d - alternate %d",
  468. config, interface, alternate);
  469. ep_addr = endpoint->endpoint_address;
  470. ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  471. ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
  472. ep_type = ep_isout ? endpoint->rcv_attributes : endpoint->tx_attributes;
  473. ep_size = ep_isout ? endpoint->rcv_packetSize : endpoint->tx_packetSize;
  474. usbdbg("addr %x, num %d, dir %s, type %s, packet size %d",
  475. ep_addr, ep_num,
  476. ep_isout ? "out" : "in",
  477. ep_type == USB_ENDPOINT_XFER_ISOC ? "isoc" :
  478. ep_type == USB_ENDPOINT_XFER_BULK ? "bulk" :
  479. ep_type == USB_ENDPOINT_XFER_INT ? "int" : "???",
  480. ep_size
  481. );
  482. /* Configure UDCCRx */
  483. tmp = 0;
  484. tmp |= (config << UDCCONR_CN_S) & UDCCONR_CN;
  485. tmp |= (interface << UDCCONR_IN_S) & UDCCONR_IN;
  486. tmp |= (alternate << UDCCONR_AISN_S) & UDCCONR_AISN;
  487. tmp |= (ep_num << UDCCONR_EN_S) & UDCCONR_EN;
  488. tmp |= (ep_type << UDCCONR_ET_S) & UDCCONR_ET;
  489. tmp |= ep_isout ? 0 : UDCCONR_ED;
  490. tmp |= (ep_size << UDCCONR_MPS_S) & UDCCONR_MPS;
  491. tmp |= UDCCONR_EE;
  492. writel(tmp, UDCCN(ep_num));
  493. usbdbg("UDCCR%c = %x", 'A' + ep_num-1, readl(UDCCN(ep_num)));
  494. usbdbg("UDCCSR%c = %x", 'A' + ep_num-1, readl(UDCCSN(ep_num)));
  495. }
  496. /* Connect the USB device to the bus */
  497. void udc_connect(void)
  498. {
  499. usbdbg("UDC connect");
  500. #ifdef CONFIG_USB_DEV_PULLUP_GPIO
  501. /* Turn on the USB connection by enabling the pullup resistor */
  502. writel(readl(GPDR(CONFIG_USB_DEV_PULLUP_GPIO))
  503. | GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
  504. GPDR(CONFIG_USB_DEV_PULLUP_GPIO));
  505. writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
  506. #else
  507. /* Host port 2 transceiver D+ pull up enable */
  508. writel(readl(UP2OCR) | UP2OCR_DPPUE, UP2OCR);
  509. #endif
  510. }
  511. /* Disconnect the USB device to the bus */
  512. void udc_disconnect(void)
  513. {
  514. usbdbg("UDC disconnect");
  515. #ifdef CONFIG_USB_DEV_PULLUP_GPIO
  516. /* Turn off the USB connection by disabling the pullup resistor */
  517. writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
  518. #else
  519. /* Host port 2 transceiver D+ pull up disable */
  520. writel(readl(UP2OCR) & ~UP2OCR_DPPUE, UP2OCR);
  521. #endif
  522. }
  523. /* Switch on the UDC */
  524. void udc_enable(struct usb_device_instance *device)
  525. {
  526. ep0state = EP0_IDLE;
  527. /* enable endpoint 0, A, B's Packet Complete Interrupt. */
  528. writel(0xffffffff, UDCICR0);
  529. writel(0xa8000000, UDCICR1);
  530. /* clear the interrupt status/control registers */
  531. writel(0xffffffff, UDCISR0);
  532. writel(0xffffffff, UDCISR1);
  533. /* set UDC-enable */
  534. udc_set_mask_UDCCR(UDCCR_UDE);
  535. udc_device = device;
  536. if (!ep0_urb)
  537. ep0_urb = usbd_alloc_urb(udc_device,
  538. udc_device->bus->endpoint_array);
  539. else
  540. usbinfo("ep0_urb %p already allocated", ep0_urb);
  541. usbdbg("UDC Enabled\n");
  542. }
  543. /* Need to check this again */
  544. void udc_disable(void)
  545. {
  546. usbdbg("disable UDC");
  547. udc_clear_mask_UDCCR(UDCCR_UDE);
  548. /* Disable clock for USB device */
  549. writel(readl(CKEN) & ~CKEN11_USB, CKEN);
  550. /* Free ep0 URB */
  551. if (ep0_urb) {
  552. usbd_dealloc_urb(ep0_urb);
  553. ep0_urb = NULL;
  554. }
  555. /* Reset device pointer */
  556. udc_device = NULL;
  557. }
  558. /* Allow udc code to do any additional startup */
  559. void udc_startup_events(struct usb_device_instance *device)
  560. {
  561. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT */
  562. usbd_device_event_irq(device, DEVICE_INIT, 0);
  563. /* The DEVICE_CREATE event puts the USB device in the state
  564. * STATE_ATTACHED */
  565. usbd_device_event_irq(device, DEVICE_CREATE, 0);
  566. /* Some USB controller driver implementations signal
  567. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
  568. * DEVICE_HUB_CONFIGURED causes a transition to the state
  569. * STATE_POWERED, and DEVICE_RESET causes a transition to
  570. * the state STATE_DEFAULT.
  571. */
  572. udc_enable(device);
  573. }
  574. /* Initialize h/w stuff */
  575. int udc_init(void)
  576. {
  577. udc_device = NULL;
  578. usbdbg("PXA27x usbd start");
  579. /* Enable clock for USB device */
  580. writel(readl(CKEN) | CKEN11_USB, CKEN);
  581. /* Disable the UDC */
  582. udc_clear_mask_UDCCR(UDCCR_UDE);
  583. /* Disable IRQs: we don't use them */
  584. writel(0, UDCICR0);
  585. writel(0, UDCICR1);
  586. return 0;
  587. }