pxa27x_udc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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, is_short ;
  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. is_short = n != endpoint->rcv_packetSize;
  144. usbdbg("n %d%s", n, is_short ? "-s" : "");
  145. for (i = 0; i < n; i += 4)
  146. data32[urb->actual_length / 4 + i / 4] = readl(UDCDN(ep_num));
  147. udc_dump_buffer("urb read", (u8 *) data32, urb->actual_length + n);
  148. usbd_rcv_complete(endpoint, n, 0);
  149. return 0;
  150. }
  151. static int udc_read_urb_ep0(void)
  152. {
  153. u32 *data32 = (u32 *) ep0_urb->buffer;
  154. u8 *data8 = (u8 *) ep0_urb->buffer;
  155. unsigned int i, n, w, b;
  156. usbdbg("read urb on ep 0");
  157. #if defined(USBDDBG) && defined(USBDPARANOIA)
  158. usbdbg("urb: buf %p, buf_len %d, actual_len %d",
  159. ep0_urb->buffer, ep0_urb->buffer_length, ep0_urb->actual_length);
  160. #endif
  161. n = readl(UDCBCR0);
  162. w = n / 4;
  163. b = n % 4;
  164. for (i = 0; i < w; i++) {
  165. data32[ep0_urb->actual_length / 4 + i] = readl(UDCDN(0));
  166. /* ep0_urb->actual_length += 4; */
  167. }
  168. for (i = 0; i < b; i++) {
  169. data8[ep0_urb->actual_length + w * 4 + i] = readb(UDCDN(0));
  170. /* ep0_urb->actual_length++; */
  171. }
  172. ep0_urb->actual_length += n;
  173. udc_dump_buffer("urb read", (u8 *) data32, ep0_urb->actual_length);
  174. writel(UDCCSR0_OPC | UDCCSR0_IPR, UDCCSR0);
  175. if (ep0_urb->actual_length == ep0_urb->device_request.wLength)
  176. return 1;
  177. return 0;
  178. }
  179. static void udc_handle_ep0(struct usb_endpoint_instance *endpoint)
  180. {
  181. u32 udccsr0 = readl(UDCCSR0);
  182. u32 *data = (u32 *) &ep0_urb->device_request;
  183. int i;
  184. usbdbg("udccsr0 %x", udccsr0);
  185. /* Clear stall status */
  186. if (udccsr0 & UDCCSR0_SST) {
  187. usberr("clear stall status");
  188. writel(UDCCSR0_SST, UDCCSR0);
  189. ep0state = EP0_IDLE;
  190. }
  191. /* previous request unfinished? non-error iff back-to-back ... */
  192. if ((udccsr0 & UDCCSR0_SA) != 0 && ep0state != EP0_IDLE)
  193. ep0state = EP0_IDLE;
  194. switch (ep0state) {
  195. case EP0_IDLE:
  196. udccsr0 = readl(UDCCSR0);
  197. /* Start control request? */
  198. if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE))
  199. == (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)) {
  200. /* Read SETUP packet.
  201. * SETUP packet size is 8 bytes (aka 2 words)
  202. */
  203. usbdbg("try reading SETUP packet");
  204. for (i = 0; i < 2; i++) {
  205. if ((readl(UDCCSR0) & UDCCSR0_RNE) == 0) {
  206. usberr("setup packet too short:%d", i);
  207. goto stall;
  208. }
  209. data[i] = readl(UDCDR0);
  210. }
  211. writel(readl(UDCCSR0) | UDCCSR0_OPC | UDCCSR0_SA, UDCCSR0);
  212. if ((readl(UDCCSR0) & UDCCSR0_RNE) != 0) {
  213. usberr("setup packet too long");
  214. goto stall;
  215. }
  216. udc_dump_buffer("ep0 setup read", (u8 *) data, 8);
  217. if (ep0_urb->device_request.wLength == 0) {
  218. usbdbg("Zero Data control Packet\n");
  219. if (ep0_recv_setup(ep0_urb)) {
  220. usberr("Invalid Setup Packet\n");
  221. udc_dump_buffer("ep0 setup read",
  222. (u8 *)data, 8);
  223. goto stall;
  224. }
  225. writel(UDCCSR0_IPR, UDCCSR0);
  226. ep0state = EP0_IDLE;
  227. } else {
  228. /* Check direction */
  229. if ((ep0_urb->device_request.bmRequestType &
  230. USB_REQ_DIRECTION_MASK)
  231. == USB_REQ_HOST2DEVICE) {
  232. ep0state = EP0_OUT_DATA;
  233. ep0_urb->buffer =
  234. (u8 *)ep0_urb->buffer_data;
  235. ep0_urb->buffer_length =
  236. sizeof(ep0_urb->buffer_data);
  237. ep0_urb->actual_length = 0;
  238. writel(UDCCSR0_IPR, UDCCSR0);
  239. } else {
  240. /* The ep0_recv_setup function has
  241. * already placed our response packet
  242. * data in ep0_urb->buffer and the
  243. * packet length in
  244. * ep0_urb->actual_length.
  245. */
  246. if (ep0_recv_setup(ep0_urb)) {
  247. stall:
  248. usberr("Invalid setup packet");
  249. udc_dump_buffer("ep0 setup read"
  250. , (u8 *) data, 8);
  251. ep0state = EP0_IDLE;
  252. writel(UDCCSR0_SA |
  253. UDCCSR0_OPC | UDCCSR0_FST |
  254. UDCCS0_FTF, UDCCSR0);
  255. return;
  256. }
  257. endpoint->tx_urb = ep0_urb;
  258. endpoint->sent = 0;
  259. usbdbg("EP0_IN_DATA");
  260. ep0state = EP0_IN_DATA;
  261. if (udc_write_urb(endpoint) < 0)
  262. goto stall;
  263. }
  264. }
  265. return;
  266. } else if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA))
  267. == (UDCCSR0_OPC|UDCCSR0_SA)) {
  268. usberr("Setup Active but no data. Stalling ....\n");
  269. goto stall;
  270. } else {
  271. usbdbg("random early IRQs");
  272. /* Some random early IRQs:
  273. * - we acked FST
  274. * - IPR cleared
  275. * - OPC got set, without SA (likely status stage)
  276. */
  277. writel(udccsr0 & (UDCCSR0_SA | UDCCSR0_OPC), UDCCSR0);
  278. }
  279. break;
  280. case EP0_OUT_DATA:
  281. if ((udccsr0 & UDCCSR0_OPC) && !(udccsr0 & UDCCSR0_SA)) {
  282. if (udc_read_urb_ep0()) {
  283. read_complete:
  284. ep0state = EP0_IDLE;
  285. if (ep0_recv_setup(ep0_urb)) {
  286. /* Not a setup packet, stall next
  287. * EP0 transaction
  288. */
  289. udc_dump_buffer("ep0 setup read",
  290. (u8 *) data, 8);
  291. usberr("can't parse setup packet\n");
  292. goto stall;
  293. }
  294. }
  295. } else if (!(udccsr0 & UDCCSR0_OPC) &&
  296. !(udccsr0 & UDCCSR0_IPR)) {
  297. if (ep0_urb->device_request.wLength ==
  298. ep0_urb->actual_length)
  299. goto read_complete;
  300. usberr("Premature Status\n");
  301. ep0state = EP0_IDLE;
  302. }
  303. break;
  304. case EP0_IN_DATA:
  305. /* GET_DESCRIPTOR etc */
  306. if (udccsr0 & UDCCSR0_OPC) {
  307. writel(UDCCSR0_OPC | UDCCSR0_FTF, UDCCSR0);
  308. usberr("ep0in premature status");
  309. ep0state = EP0_IDLE;
  310. } else {
  311. /* irq was IPR clearing */
  312. if (udc_write_urb(endpoint) < 0) {
  313. usberr("ep0_write_error\n");
  314. goto stall;
  315. }
  316. }
  317. break;
  318. case EP0_XFER_COMPLETE:
  319. writel(UDCCSR0_IPR, UDCCSR0);
  320. ep0state = EP0_IDLE;
  321. break;
  322. default:
  323. usbdbg("Default\n");
  324. }
  325. writel(USIR0_IR0, USIR0);
  326. }
  327. static void udc_handle_ep(struct usb_endpoint_instance *endpoint)
  328. {
  329. int ep_addr = endpoint->endpoint_address;
  330. int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  331. int ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
  332. u32 flags = readl(UDCCSN(ep_num)) & (UDCCSR_SST | UDCCSR_TRN);
  333. if (flags)
  334. writel(flags, UDCCSN(ep_num));
  335. if (ep_isout)
  336. udc_read_urb(endpoint);
  337. else
  338. udc_write_urb(endpoint);
  339. writel(UDCCSR_PC, UDCCSN(ep_num));
  340. }
  341. static void udc_state_changed(void)
  342. {
  343. int config, interface, alternate;
  344. writel(readl(UDCCR) | UDCCR_SMAC, UDCCR);
  345. config = (readl(UDCCR) & UDCCR_ACN) >> UDCCR_ACN_S;
  346. interface = (readl(UDCCR) & UDCCR_AIN) >> UDCCR_AIN_S;
  347. alternate = (readl(UDCCR) & UDCCR_AAISN) >> UDCCR_AAISN_S;
  348. usbdbg("New UDC settings are: conf %d - inter %d - alter %d",
  349. config, interface, alternate);
  350. usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
  351. writel(UDCISR1_IRCC, UDCISR1);
  352. }
  353. void udc_irq(void)
  354. {
  355. int handled;
  356. struct usb_endpoint_instance *endpoint;
  357. int ep_num, i;
  358. u32 udcisr0;
  359. do {
  360. handled = 0;
  361. /* Suspend Interrupt Request */
  362. if (readl(USIR1) & UDCCR_SUSIR) {
  363. usbdbg("Suspend\n");
  364. udc_ack_int_UDCCR(UDCCR_SUSIR);
  365. handled = 1;
  366. ep0state = EP0_IDLE;
  367. }
  368. /* Resume Interrupt Request */
  369. if (readl(USIR1) & UDCCR_RESIR) {
  370. udc_ack_int_UDCCR(UDCCR_RESIR);
  371. handled = 1;
  372. usbdbg("USB resume\n");
  373. }
  374. if (readl(USIR1) & (1<<31)) {
  375. handled = 1;
  376. udc_state_changed();
  377. }
  378. /* Reset Interrupt Request */
  379. if (readl(USIR1) & UDCCR_RSTIR) {
  380. udc_ack_int_UDCCR(UDCCR_RSTIR);
  381. handled = 1;
  382. usbdbg("Reset\n");
  383. usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
  384. } else {
  385. if (readl(USIR0))
  386. usbdbg("UISR0: %x \n", readl(USIR0));
  387. if (readl(USIR0) & 0x2)
  388. writel(0x2, USIR0);
  389. /* Control traffic */
  390. if (readl(USIR0) & USIR0_IR0) {
  391. handled = 1;
  392. writel(USIR0_IR0, USIR0);
  393. udc_handle_ep0(udc_device->bus->endpoint_array);
  394. }
  395. endpoint = udc_device->bus->endpoint_array;
  396. for (i = 0; i < udc_device->bus->max_endpoints; i++) {
  397. ep_num = (endpoint[i].endpoint_address) &
  398. USB_ENDPOINT_NUMBER_MASK;
  399. if (!ep_num)
  400. continue;
  401. udcisr0 = readl(UDCISR0);
  402. if (udcisr0 &
  403. UDCISR_INT(ep_num, UDC_INT_PACKETCMP)) {
  404. writel(UDCISR_INT(ep_num, UDC_INT_PACKETCMP),
  405. UDCISR0);
  406. udc_handle_ep(&endpoint[i]);
  407. }
  408. }
  409. }
  410. } while (handled);
  411. }
  412. /* The UDCCR reg contains mask and interrupt status bits,
  413. * so using '|=' isn't safe as it may ack an interrupt.
  414. */
  415. #define UDCCR_OEN (1 << 31) /* On-the-Go Enable */
  416. #define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_UDE)
  417. static inline void udc_set_mask_UDCCR(int mask)
  418. {
  419. writel((readl(UDCCR) & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS), UDCCR);
  420. }
  421. static inline void udc_clear_mask_UDCCR(int mask)
  422. {
  423. writel((readl(UDCCR) & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS), UDCCR);
  424. }
  425. static void pio_irq_enable(int ep_num)
  426. {
  427. if (ep_num < 16)
  428. writel(readl(UDCICR0) | 3 << (ep_num * 2), UDCICR0);
  429. else {
  430. ep_num -= 16;
  431. writel(readl(UDCICR1) | 3 << (ep_num * 2), UDCICR1);
  432. }
  433. }
  434. /*
  435. * udc_set_nak
  436. *
  437. * Allow upper layers to signal lower layers should not accept more RX data
  438. */
  439. void udc_set_nak(int ep_num)
  440. {
  441. /* TODO */
  442. }
  443. /*
  444. * udc_unset_nak
  445. *
  446. * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
  447. * Switch off NAKing on this endpoint to accept more data output from host.
  448. */
  449. void udc_unset_nak(int ep_num)
  450. {
  451. /* TODO */
  452. }
  453. int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
  454. {
  455. return udc_write_urb(endpoint);
  456. }
  457. /* Associate a physical endpoint with endpoint instance */
  458. void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
  459. struct usb_endpoint_instance *endpoint)
  460. {
  461. int ep_num, ep_addr, ep_isout, ep_type, ep_size;
  462. int config, interface, alternate;
  463. u32 tmp;
  464. usbdbg("setting up endpoint id %d", id);
  465. if (!endpoint) {
  466. usberr("endpoint void!");
  467. return;
  468. }
  469. ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
  470. if (ep_num >= UDC_MAX_ENDPOINTS) {
  471. usberr("unable to setup ep %d!", ep_num);
  472. return;
  473. }
  474. pio_irq_enable(ep_num);
  475. if (ep_num == 0) {
  476. /* Done for ep0 */
  477. return;
  478. }
  479. config = 1;
  480. interface = 0;
  481. alternate = 0;
  482. usbdbg("config %d - interface %d - alternate %d",
  483. config, interface, alternate);
  484. ep_addr = endpoint->endpoint_address;
  485. ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
  486. ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
  487. ep_type = ep_isout ? endpoint->rcv_attributes : endpoint->tx_attributes;
  488. ep_size = ep_isout ? endpoint->rcv_packetSize : endpoint->tx_packetSize;
  489. usbdbg("addr %x, num %d, dir %s, type %s, packet size %d",
  490. ep_addr, ep_num,
  491. ep_isout ? "out" : "in",
  492. ep_type == USB_ENDPOINT_XFER_ISOC ? "isoc" :
  493. ep_type == USB_ENDPOINT_XFER_BULK ? "bulk" :
  494. ep_type == USB_ENDPOINT_XFER_INT ? "int" : "???",
  495. ep_size
  496. );
  497. /* Configure UDCCRx */
  498. tmp = 0;
  499. tmp |= (config << UDCCONR_CN_S) & UDCCONR_CN;
  500. tmp |= (interface << UDCCONR_IN_S) & UDCCONR_IN;
  501. tmp |= (alternate << UDCCONR_AISN_S) & UDCCONR_AISN;
  502. tmp |= (ep_num << UDCCONR_EN_S) & UDCCONR_EN;
  503. tmp |= (ep_type << UDCCONR_ET_S) & UDCCONR_ET;
  504. tmp |= ep_isout ? 0 : UDCCONR_ED;
  505. tmp |= (ep_size << UDCCONR_MPS_S) & UDCCONR_MPS;
  506. tmp |= UDCCONR_EE;
  507. writel(tmp, UDCCN(ep_num));
  508. usbdbg("UDCCR%c = %x", 'A' + ep_num-1, readl(UDCCN(ep_num)));
  509. usbdbg("UDCCSR%c = %x", 'A' + ep_num-1, readl(UDCCSN(ep_num)));
  510. }
  511. /* Connect the USB device to the bus */
  512. void udc_connect(void)
  513. {
  514. usbdbg("UDC connect");
  515. #ifdef CONFIG_USB_DEV_PULLUP_GPIO
  516. /* Turn on the USB connection by enabling the pullup resistor */
  517. writel(readl(GPDR(CONFIG_USB_DEV_PULLUP_GPIO))
  518. | GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
  519. GPDR(CONFIG_USB_DEV_PULLUP_GPIO));
  520. writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
  521. #else
  522. /* Host port 2 transceiver D+ pull up enable */
  523. writel(readl(UP2OCR) | UP2OCR_DPPUE, UP2OCR);
  524. #endif
  525. }
  526. /* Disconnect the USB device to the bus */
  527. void udc_disconnect(void)
  528. {
  529. usbdbg("UDC disconnect");
  530. #ifdef CONFIG_USB_DEV_PULLUP_GPIO
  531. /* Turn off the USB connection by disabling the pullup resistor */
  532. writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
  533. #else
  534. /* Host port 2 transceiver D+ pull up disable */
  535. writel(readl(UP2OCR) & ~UP2OCR_DPPUE, UP2OCR);
  536. #endif
  537. }
  538. /* Switch on the UDC */
  539. void udc_enable(struct usb_device_instance *device)
  540. {
  541. ep0state = EP0_IDLE;
  542. /* enable endpoint 0, A, B's Packet Complete Interrupt. */
  543. writel(0xffffffff, UDCICR0);
  544. writel(0xa8000000, UDCICR1);
  545. /* clear the interrupt status/control registers */
  546. writel(0xffffffff, UDCISR0);
  547. writel(0xffffffff, UDCISR1);
  548. /* set UDC-enable */
  549. udc_set_mask_UDCCR(UDCCR_UDE);
  550. udc_device = device;
  551. if (!ep0_urb)
  552. ep0_urb = usbd_alloc_urb(udc_device,
  553. udc_device->bus->endpoint_array);
  554. else
  555. usbinfo("ep0_urb %p already allocated", ep0_urb);
  556. usbdbg("UDC Enabled\n");
  557. }
  558. /* Need to check this again */
  559. void udc_disable(void)
  560. {
  561. usbdbg("disable UDC");
  562. udc_clear_mask_UDCCR(UDCCR_UDE);
  563. /* Disable clock for USB device */
  564. writel(readl(CKEN) & ~CKEN11_USB, CKEN);
  565. /* Free ep0 URB */
  566. if (ep0_urb) {
  567. usbd_dealloc_urb(ep0_urb);
  568. ep0_urb = NULL;
  569. }
  570. /* Reset device pointer */
  571. udc_device = NULL;
  572. }
  573. /* Allow udc code to do any additional startup */
  574. void udc_startup_events(struct usb_device_instance *device)
  575. {
  576. /* The DEVICE_INIT event puts the USB device in the state STATE_INIT */
  577. usbd_device_event_irq(device, DEVICE_INIT, 0);
  578. /* The DEVICE_CREATE event puts the USB device in the state
  579. * STATE_ATTACHED */
  580. usbd_device_event_irq(device, DEVICE_CREATE, 0);
  581. /* Some USB controller driver implementations signal
  582. * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
  583. * DEVICE_HUB_CONFIGURED causes a transition to the state
  584. * STATE_POWERED, and DEVICE_RESET causes a transition to
  585. * the state STATE_DEFAULT.
  586. */
  587. udc_enable(device);
  588. }
  589. /* Initialize h/w stuff */
  590. int udc_init(void)
  591. {
  592. udc_device = NULL;
  593. usbdbg("PXA27x usbd start");
  594. /* Enable clock for USB device */
  595. writel(readl(CKEN) | CKEN11_USB, CKEN);
  596. /* Disable the UDC */
  597. udc_clear_mask_UDCCR(UDCCR_UDE);
  598. /* Disable IRQs: we don't use them */
  599. writel(0, UDCICR0);
  600. writel(0, UDCICR1);
  601. return 0;
  602. }