pxa27x_udc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 <usbdcore.h>
  28. #include <usbdcore_ep0.h>
  29. #include <asm/arch/hardware.h>
  30. #include <usb/pxa27x_udc.h>
  31. /* number of endpoints on this UDC */
  32. #define UDC_MAX_ENDPOINTS 24
  33. static struct urb *ep0_urb;
  34. static struct usb_device_instance *udc_device;
  35. static int ep0state = EP0_IDLE;
  36. #ifdef USBDDBG
  37. static void udc_dump_buffer(char *name, u8 *buf, int len)
  38. {
  39. usbdbg("%s - buf %p, len %d", name, buf, len);
  40. print_buffer(0, buf, 1, len, 0);
  41. }
  42. #else
  43. #define udc_dump_buffer(name, buf, len) /* void */
  44. #endif
  45. static inline void udc_ack_int_UDCCR(int mask)
  46. {
  47. USIR1 = mask | USIR1;
  48. }
  49. /*
  50. * If the endpoint has an active tx_urb, then the next packet of data from the
  51. * URB is written to the tx FIFO.
  52. * The total amount of data in the urb is given by urb->actual_length.
  53. * The maximum amount of data that can be sent in any one packet is given by
  54. * endpoint->tx_packetSize.
  55. * The number of data bytes from this URB that have already been transmitted
  56. * is given by endpoint->sent.
  57. * endpoint->last is updated by this routine with the number of data bytes
  58. * transmitted in this packet.
  59. */
  60. static int udc_write_urb(struct usb_endpoint_instance *endpoint)
  61. {
  62. struct urb *urb = endpoint->tx_urb;
  63. int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
  64. u32 *addr32 = (u32 *) &UDCDN(ep_num);
  65. u32 *data32 = (u32 *) urb->buffer;
  66. u8 *addr8 = (u8 *) &UDCDN(ep_num);
  67. u8 *data8 = (u8 *) urb->buffer;
  68. unsigned int i, n, w, b, is_short;
  69. int timeout = 2000; /* 2ms */
  70. if (!urb || !urb->actual_length)
  71. return -1;
  72. n = MIN(urb->actual_length - endpoint->sent, endpoint->tx_packetSize);
  73. if (n <= 0)
  74. return -1;
  75. usbdbg("write urb on ep %d", ep_num);
  76. #if defined(USBDDBG) && defined(USBDPARANOIA)
  77. usbdbg("urb: buf %p, buf_len %d, actual_len %d",
  78. urb->buffer, urb->buffer_length, urb->actual_length);
  79. usbdbg("endpoint: sent %d, tx_packetSize %d, last %d",
  80. endpoint->sent, endpoint->tx_packetSize, endpoint->last);
  81. #endif
  82. is_short = n != endpoint->tx_packetSize;
  83. w = n / 4;
  84. b = n % 4;
  85. usbdbg("n %d%s w %d b %d", n, is_short ? "-s" : "", w, b);
  86. udc_dump_buffer("urb write", data8 + endpoint->sent, n);
  87. /* Prepare for data send */
  88. if (ep_num)
  89. UDCCSN(ep_num) = UDCCSR_PC;
  90. for (i = 0; i < w; i++)
  91. *addr32 = data32[endpoint->sent/4 + i];
  92. for (i = 0; i < b; i++)
  93. *addr8 = data8[endpoint->sent + w*4 + i];
  94. /* Set "Packet Complete" if less data then tx_packetSize */
  95. if (is_short)
  96. UDCCSN(ep_num) = ep_num ? UDCCSR_SP : UDCCSR0_IPR;
  97. /* Wait for data sent */
  98. while (!(UDCCSN(ep_num) & (ep_num ? UDCCSR_PC : UDCCSR0_IPR))) {
  99. if (ep_num) {
  100. if (timeout-- == 0)
  101. return -1;
  102. else
  103. udelay(1);
  104. };
  105. }
  106. endpoint->last = n;
  107. if (ep_num) {
  108. usbd_tx_complete(endpoint);
  109. } else {
  110. endpoint->sent += n;
  111. endpoint->last -= n;
  112. }
  113. if ((endpoint->tx_urb->actual_length - endpoint->sent) <= 0) {
  114. urb->actual_length = 0;
  115. endpoint->sent = 0;
  116. endpoint->last = 0;
  117. }
  118. if ((endpoint->sent >= urb->actual_length) && (!ep_num)) {
  119. usbdbg("ep0 IN stage done");
  120. if (is_short)
  121. ep0state = EP0_IDLE;
  122. else
  123. ep0state = EP0_XFER_COMPLETE;
  124. }
  125. return 0;
  126. }
  127. static int udc_read_urb(struct usb_endpoint_instance *endpoint)
  128. {
  129. struct urb *urb = endpoint->rcv_urb;
  130. int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
  131. u32 *addr32 = (u32 *) &UDCDN(ep_num);
  132. u32 *data32 = (u32 *) urb->buffer;
  133. unsigned int i, n, is_short ;
  134. usbdbg("read urb on ep %d", ep_num);
  135. #if defined(USBDDBG) && defined(USBDPARANOIA)
  136. usbdbg("urb: buf %p, buf_len %d, actual_len %d",
  137. urb->buffer, urb->buffer_length, urb->actual_length);
  138. usbdbg("endpoint: rcv_packetSize %d",
  139. endpoint->rcv_packetSize);
  140. #endif
  141. if (UDCCSN(ep_num) & UDCCSR_BNE)
  142. n = UDCBCN(ep_num) & 0x3ff;
  143. else /* zlp */
  144. n = 0;
  145. is_short = n != endpoint->rcv_packetSize;
  146. usbdbg("n %d%s", n, is_short ? "-s" : "");
  147. for (i = 0; i < n; i += 4)
  148. data32[urb->actual_length/4 + i/4] = *addr32;
  149. udc_dump_buffer("urb read", (u8 *) data32, urb->actual_length + n);
  150. usbd_rcv_complete(endpoint, n, 0);
  151. return 0;
  152. }
  153. static int udc_read_urb_ep0(void)
  154. {
  155. u32 *addr32 = (u32 *) &UDCDN(0);
  156. u32 *data32 = (u32 *) ep0_urb->buffer;
  157. u8 *addr8 = (u8 *) &UDCDN(0);
  158. u8 *data8 = (u8 *) ep0_urb->buffer;
  159. unsigned int i, n, w, b;
  160. n = UDCBCR0;
  161. w = n / 4;
  162. b = n % 4;
  163. for (i = 0; i < w; i++) {
  164. data32[ep0_urb->actual_length/4 + i] = *addr32;
  165. ep0_urb->actual_length += 4;
  166. }
  167. for (i = 0; i < b; i++) {
  168. data8[ep0_urb->actual_length + w*4 + i] = *addr8;
  169. ep0_urb->actual_length++;
  170. }
  171. UDCCSR0 = UDCCSR0_OPC | UDCCSR0_IPR;
  172. if (ep0_urb->actual_length == ep0_urb->device_request.wLength)
  173. return 1;
  174. return 0;
  175. }
  176. static void udc_handle_ep0(struct usb_endpoint_instance *endpoint)
  177. {
  178. u32 udccsr0 = UDCCSR0;
  179. u32 *data = (u32 *) &ep0_urb->device_request;
  180. int i;
  181. usbdbg("udccsr0 %x", udccsr0);
  182. /* Clear stall status */
  183. if (udccsr0 & UDCCSR0_SST) {
  184. usberr("clear stall status");
  185. UDCCSR0 = UDCCSR0_SST;
  186. ep0state = EP0_IDLE;
  187. }
  188. /* previous request unfinished? non-error iff back-to-back ... */
  189. if ((udccsr0 & UDCCSR0_SA) != 0 && ep0state != EP0_IDLE)
  190. ep0state = EP0_IDLE;
  191. switch (ep0state) {
  192. case EP0_IDLE:
  193. udccsr0 = UDCCSR0;
  194. /* Start control request? */
  195. if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE))
  196. == (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)) {
  197. /* Read SETUP packet.
  198. * SETUP packet size is 8 bytes (aka 2 words)
  199. */
  200. usbdbg("try reading SETUP packet");
  201. for (i = 0; i < 2; i++) {
  202. if ((UDCCSR0 & UDCCSR0_RNE) == 0) {
  203. usberr("setup packet too short:%d", i);
  204. goto stall;
  205. }
  206. data[i] = UDCDR0;
  207. }
  208. UDCCSR0 |= (UDCCSR0_OPC | UDCCSR0_SA);
  209. if ((UDCCSR0 & UDCCSR0_RNE) != 0) {
  210. usberr("setup packet too long");
  211. goto stall;
  212. }
  213. udc_dump_buffer("ep0 setup read", (u8 *) data, 8);
  214. if (ep0_urb->device_request.wLength == 0) {
  215. usbdbg("Zero Data control Packet\n");
  216. if (ep0_recv_setup(ep0_urb)) {
  217. usberr("Invalid Setup Packet\n");
  218. udc_dump_buffer("ep0 setup read",
  219. (u8 *)data, 8);
  220. goto stall;
  221. }
  222. UDCCSR0 = UDCCSR0_IPR;
  223. ep0state = EP0_IDLE;
  224. } else {
  225. /* Check direction */
  226. if ((ep0_urb->device_request.bmRequestType &
  227. USB_REQ_DIRECTION_MASK)
  228. == USB_REQ_HOST2DEVICE) {
  229. ep0state = EP0_OUT_DATA;
  230. ep0_urb->buffer =
  231. (u8 *)ep0_urb->buffer_data;
  232. ep0_urb->buffer_length =
  233. sizeof(ep0_urb->buffer_data);
  234. ep0_urb->actual_length = 0;
  235. UDCCSR0 = UDCCSR0_IPR;
  236. } else {
  237. /* The ep0_recv_setup function has
  238. * already placed our response packet
  239. * data in ep0_urb->buffer and the
  240. * packet length in
  241. * ep0_urb->actual_length.
  242. */
  243. if (ep0_recv_setup(ep0_urb)) {
  244. stall:
  245. usberr("Invalid setup packet");
  246. udc_dump_buffer("ep0 setup read"
  247. , (u8 *) data, 8);
  248. ep0state = EP0_IDLE;
  249. UDCCSR0 = UDCCSR0_SA |
  250. UDCCSR0_OPC | UDCCSR0_FST |
  251. UDCCS0_FTF;
  252. return;
  253. }
  254. endpoint->tx_urb = ep0_urb;
  255. endpoint->sent = 0;
  256. usbdbg("EP0_IN_DATA");
  257. ep0state = EP0_IN_DATA;
  258. if (udc_write_urb(endpoint) < 0)
  259. goto stall;
  260. }
  261. }
  262. return;
  263. } else if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA))
  264. == (UDCCSR0_OPC|UDCCSR0_SA)) {
  265. usberr("Setup Active but no data. Stalling ....\n");
  266. goto stall;
  267. } else {
  268. usbdbg("random early IRQs");
  269. /* Some random early IRQs:
  270. * - we acked FST
  271. * - IPR cleared
  272. * - OPC got set, without SA (likely status stage)
  273. */
  274. UDCCSR0 = udccsr0 & (UDCCSR0_SA | UDCCSR0_OPC);
  275. }
  276. break;
  277. case EP0_OUT_DATA:
  278. if ((udccsr0 & UDCCSR0_OPC) && !(udccsr0 & UDCCSR0_SA)) {
  279. if (udc_read_urb_ep0()) {
  280. read_complete:
  281. ep0state = EP0_IDLE;
  282. if (ep0_recv_setup(ep0_urb)) {
  283. /* Not a setup packet, stall next
  284. * EP0 transaction
  285. */
  286. udc_dump_buffer("ep0 setup read",
  287. (u8 *) data, 8);
  288. usberr("can't parse setup packet\n");
  289. goto stall;
  290. }
  291. }
  292. } else if (!(udccsr0 & UDCCSR0_OPC) &&
  293. !(udccsr0 & UDCCSR0_IPR)) {
  294. if (ep0_urb->device_request.wLength ==
  295. ep0_urb->actual_length)
  296. goto read_complete;
  297. usberr("Premature Status\n");
  298. ep0state = EP0_IDLE;
  299. }
  300. break;
  301. case EP0_IN_DATA:
  302. /* GET_DESCRIPTOR etc */
  303. if (udccsr0 & UDCCSR0_OPC) {
  304. UDCCSR0 = UDCCSR0_OPC | UDCCSR0_FTF;
  305. usberr("ep0in premature status");
  306. ep0state = EP0_IDLE;
  307. } else {
  308. /* irq was IPR clearing */
  309. if (udc_write_urb(endpoint) < 0) {
  310. usberr("ep0_write_error\n");
  311. goto stall;
  312. }
  313. }
  314. break;
  315. case EP0_XFER_COMPLETE:
  316. UDCCSR0 = UDCCSR0_IPR;
  317. ep0state = EP0_IDLE;
  318. break;
  319. default:
  320. usbdbg("Default\n");
  321. }
  322. USIR0 = USIR0_IR0;
  323. }
  324. static void udc_handle_ep(struct usb_endpoint_instance *endpoint)
  325. {
  326. int ep_addr = endpoint->endpoint_address;
  327. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  328. int ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
  329. u32 flags = UDCCSN(ep_num) & (UDCCSR_SST | UDCCSR_TRN);
  330. if (flags)
  331. UDCCSN(ep_num) = flags;
  332. if (ep_isout)
  333. udc_read_urb(endpoint);
  334. else
  335. udc_write_urb(endpoint);
  336. UDCCSN(ep_num) = UDCCSR_PC;
  337. }
  338. static void udc_state_changed(void)
  339. {
  340. int config, interface, alternate;
  341. UDCCR |= UDCCR_SMAC;
  342. config = (UDCCR & UDCCR_ACN) >> UDCCR_ACN_S;
  343. interface = (UDCCR & UDCCR_AIN) >> UDCCR_AIN_S;
  344. alternate = (UDCCR & UDCCR_AAISN) >> UDCCR_AAISN_S;
  345. usbdbg("New UDC settings are: conf %d - inter %d - alter %d",
  346. config, interface, alternate);
  347. usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
  348. UDCISR1 = UDCISR1_IRCC;
  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 (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 (USIR1 & UDCCR_RESIR) {
  367. udc_ack_int_UDCCR(UDCCR_RESIR);
  368. handled = 1;
  369. usbdbg("USB resume\n");
  370. }
  371. if (USIR1 & (1<<31)) {
  372. handled = 1;
  373. udc_state_changed();
  374. }
  375. /* Reset Interrupt Request */
  376. if (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 (USIR0)
  383. usbdbg("UISR0: %x \n", USIR0);
  384. if (USIR0 & 0x2)
  385. USIR0 = 0x2;
  386. /* Control traffic */
  387. if (USIR0 & USIR0_IR0) {
  388. handled = 1;
  389. udc_handle_ep0(udc_device->bus->endpoint_array);
  390. USIR0 = USIR0_IR0;
  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 = UDCISR0;
  399. if (udcisr0 &
  400. UDCISR_INT(ep_num, UDC_INT_PACKETCMP)) {
  401. UDCISR0 = UDCISR_INT(ep_num,
  402. UDC_INT_PACKETCMP);
  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. UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
  417. }
  418. static inline void udc_clear_mask_UDCCR(int mask)
  419. {
  420. UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
  421. }
  422. static void pio_irq_enable(int ep_num)
  423. {
  424. if (ep_num < 16)
  425. UDCICR0 |= 3 << (ep_num * 2);
  426. else {
  427. ep_num -= 16;
  428. UDCICR1 |= 3 << (ep_num * 2);
  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. UDCCN(ep_num) = tmp;
  505. usbdbg("UDCCR%c = %x", 'A' + ep_num-1, UDCCN(ep_num));
  506. usbdbg("UDCCSR%c = %x", 'A' + ep_num-1, UDCCSN(ep_num));
  507. }
  508. #define CONFIG_USB_DEV_PULLUP_GPIO 87
  509. /* Connect the USB device to the bus */
  510. void udc_connect(void)
  511. {
  512. usbdbg("UDC connect");
  513. /* Turn on the USB connection by enabling the pullup resistor */
  514. set_GPIO_mode(CONFIG_USB_DEV_PULLUP_GPIO | GPIO_OUT);
  515. GPSR(CONFIG_USB_DEV_PULLUP_GPIO) = GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO);
  516. }
  517. /* Disconnect the USB device to the bus */
  518. void udc_disconnect(void)
  519. {
  520. usbdbg("UDC disconnect");
  521. /* Turn off the USB connection by disabling the pullup resistor */
  522. GPCR(CONFIG_USB_DEV_PULLUP_GPIO) = GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO);
  523. }
  524. /* Switch on the UDC */
  525. void udc_enable(struct usb_device_instance *device)
  526. {
  527. ep0state = EP0_IDLE;
  528. CKEN |= CKEN11_USB;
  529. /* enable endpoint 0, A, B's Packet Complete Interrupt. */
  530. UDCICR0 = 0x0000003f;
  531. UDCICR1 = 0xa8000000;
  532. /* clear the interrupt status/control registers */
  533. UDCISR0 = 0xffffffff;
  534. UDCISR1 = 0xffffffff;
  535. /* set UDC-enable */
  536. udc_set_mask_UDCCR(UDCCR_UDE);
  537. udc_device = device;
  538. if (!ep0_urb)
  539. ep0_urb = usbd_alloc_urb(udc_device,
  540. udc_device->bus->endpoint_array);
  541. else
  542. usbinfo("ep0_urb %p already allocated", ep0_urb);
  543. usbdbg("UDC Enabled\n");
  544. }
  545. /* Need to check this again */
  546. void udc_disable(void)
  547. {
  548. usbdbg("disable UDC");
  549. udc_clear_mask_UDCCR(UDCCR_UDE);
  550. /* Disable clock for USB device */
  551. CKEN &= ~CKEN11_USB;
  552. /* Free ep0 URB */
  553. if (ep0_urb) {
  554. usbd_dealloc_urb(ep0_urb);
  555. ep0_urb = NULL;
  556. }
  557. /* Reset device pointer */
  558. udc_device = NULL;
  559. }
  560. /* Allow udc code to do any additional startup */
  561. void udc_startup_events(struct usb_device_instance *device)
  562. {
  563. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT */
  564. usbd_device_event_irq(device, DEVICE_INIT, 0);
  565. /* The DEVICE_CREATE event puts the USB device in the state
  566. * STATE_ATTACHED */
  567. usbd_device_event_irq(device, DEVICE_CREATE, 0);
  568. /* Some USB controller driver implementations signal
  569. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
  570. * DEVICE_HUB_CONFIGURED causes a transition to the state
  571. * STATE_POWERED, and DEVICE_RESET causes a transition to
  572. * the state STATE_DEFAULT.
  573. */
  574. udc_enable(device);
  575. }
  576. /* Initialize h/w stuff */
  577. int udc_init(void)
  578. {
  579. udc_device = NULL;
  580. usbdbg("PXA27x usbd start");
  581. /* Disable the UDC */
  582. udc_clear_mask_UDCCR(UDCCR_UDE);
  583. /* Disable clock for USB device */
  584. CKEN &= ~CKEN11_USB;
  585. /* Disable IRQs: we don't use them */
  586. UDCICR0 = UDCICR1 = 0;
  587. return 0;
  588. }