pxa27x_udc.c 18 KB

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