2 * PXA27x USB device driver for u-boot.
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>
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.
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.
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,
28 #include <asm/byteorder.h>
29 #include <usbdevice.h>
30 #include <asm/arch/hardware.h>
32 #include <usb/pxa27x_udc.h>
36 /* number of endpoints on this UDC */
37 #define UDC_MAX_ENDPOINTS 24
39 static struct urb *ep0_urb;
40 static struct usb_device_instance *udc_device;
41 static int ep0state = EP0_IDLE;
44 static void udc_dump_buffer(char *name, u8 *buf, int len)
46 usbdbg("%s - buf %p, len %d", name, buf, len);
47 print_buffer(0, buf, 1, len, 0);
50 #define udc_dump_buffer(name, buf, len) /* void */
53 static inline void udc_ack_int_UDCCR(int mask)
55 writel(readl(USIR1) | mask, USIR1);
59 * If the endpoint has an active tx_urb, then the next packet of data from the
60 * URB is written to the tx FIFO.
61 * The total amount of data in the urb is given by urb->actual_length.
62 * The maximum amount of data that can be sent in any one packet is given by
63 * endpoint->tx_packetSize.
64 * The number of data bytes from this URB that have already been transmitted
65 * is given by endpoint->sent.
66 * endpoint->last is updated by this routine with the number of data bytes
67 * transmitted in this packet.
69 static int udc_write_urb(struct usb_endpoint_instance *endpoint)
71 struct urb *urb = endpoint->tx_urb;
72 int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
73 u32 *data32 = (u32 *) urb->buffer;
74 u8 *data8 = (u8 *) urb->buffer;
75 unsigned int i, n, w, b, is_short;
76 int timeout = 2000; /* 2ms */
78 if (!urb || !urb->actual_length)
81 n = MIN(urb->actual_length - endpoint->sent, endpoint->tx_packetSize);
85 usbdbg("write urb on ep %d", ep_num);
86 #if defined(USBDDBG) && defined(USBDPARANOIA)
87 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
88 urb->buffer, urb->buffer_length, urb->actual_length);
89 usbdbg("endpoint: sent %d, tx_packetSize %d, last %d",
90 endpoint->sent, endpoint->tx_packetSize, endpoint->last);
93 is_short = n != endpoint->tx_packetSize;
96 usbdbg("n %d%s w %d b %d", n, is_short ? "-s" : "", w, b);
97 udc_dump_buffer("urb write", data8 + endpoint->sent, n);
99 /* Prepare for data send */
101 writel(UDCCSR_PC ,UDCCSN(ep_num));
103 for (i = 0; i < w; i++)
104 writel(data32[endpoint->sent / 4 + i], UDCDN(ep_num));
106 for (i = 0; i < b; i++)
107 writeb(data8[endpoint->sent + w * 4 + i], UDCDN(ep_num));
109 /* Set "Packet Complete" if less data then tx_packetSize */
111 writel(ep_num ? UDCCSR_SP : UDCCSR0_IPR, UDCCSN(ep_num));
113 /* Wait for data sent */
115 while (!(readl(UDCCSN(ep_num)) & UDCCSR_PC)) {
126 usbd_tx_complete(endpoint);
132 if (endpoint->sent >= urb->actual_length) {
133 urb->actual_length = 0;
138 if ((endpoint->sent >= urb->actual_length) && (!ep_num)) {
139 usbdbg("ep0 IN stage done");
143 ep0state = EP0_XFER_COMPLETE;
149 static int udc_read_urb(struct usb_endpoint_instance *endpoint)
151 struct urb *urb = endpoint->rcv_urb;
152 int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
153 u32 *data32 = (u32 *) urb->buffer;
154 unsigned int i, n, is_short ;
156 usbdbg("read urb on ep %d", ep_num);
157 #if defined(USBDDBG) && defined(USBDPARANOIA)
158 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
159 urb->buffer, urb->buffer_length, urb->actual_length);
160 usbdbg("endpoint: rcv_packetSize %d",
161 endpoint->rcv_packetSize);
164 if (readl(UDCCSN(ep_num)) & UDCCSR_BNE)
165 n = readl(UDCBCN(ep_num)) & 0x3ff;
168 is_short = n != endpoint->rcv_packetSize;
170 usbdbg("n %d%s", n, is_short ? "-s" : "");
171 for (i = 0; i < n; i += 4)
172 data32[urb->actual_length / 4 + i / 4] = readl(UDCDN(ep_num));
174 udc_dump_buffer("urb read", (u8 *) data32, urb->actual_length + n);
175 usbd_rcv_complete(endpoint, n, 0);
180 static int udc_read_urb_ep0(void)
182 u32 *data32 = (u32 *) ep0_urb->buffer;
183 u8 *data8 = (u8 *) ep0_urb->buffer;
184 unsigned int i, n, w, b;
186 usbdbg("read urb on ep 0");
187 #if defined(USBDDBG) && defined(USBDPARANOIA)
188 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
189 ep0_urb->buffer, ep0_urb->buffer_length, ep0_urb->actual_length);
196 for (i = 0; i < w; i++) {
197 data32[ep0_urb->actual_length / 4 + i] = readl(UDCDN(0));
198 /* ep0_urb->actual_length += 4; */
201 for (i = 0; i < b; i++) {
202 data8[ep0_urb->actual_length + w * 4 + i] = readb(UDCDN(0));
203 /* ep0_urb->actual_length++; */
206 ep0_urb->actual_length += n;
208 udc_dump_buffer("urb read", (u8 *) data32, ep0_urb->actual_length);
210 writel(UDCCSR0_OPC | UDCCSR0_IPR, UDCCSR0);
211 if (ep0_urb->actual_length == ep0_urb->device_request.wLength)
217 static void udc_handle_ep0(struct usb_endpoint_instance *endpoint)
219 u32 udccsr0 = readl(UDCCSR0);
220 u32 *data = (u32 *) &ep0_urb->device_request;
223 usbdbg("udccsr0 %x", udccsr0);
225 /* Clear stall status */
226 if (udccsr0 & UDCCSR0_SST) {
227 usberr("clear stall status");
228 writel(UDCCSR0_SST, UDCCSR0);
232 /* previous request unfinished? non-error iff back-to-back ... */
233 if ((udccsr0 & UDCCSR0_SA) != 0 && ep0state != EP0_IDLE)
239 udccsr0 = readl(UDCCSR0);
240 /* Start control request? */
241 if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE))
242 == (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)) {
244 /* Read SETUP packet.
245 * SETUP packet size is 8 bytes (aka 2 words)
247 usbdbg("try reading SETUP packet");
248 for (i = 0; i < 2; i++) {
249 if ((readl(UDCCSR0) & UDCCSR0_RNE) == 0) {
250 usberr("setup packet too short:%d", i);
253 data[i] = readl(UDCDR0);
256 writel(readl(UDCCSR0) | UDCCSR0_OPC | UDCCSR0_SA, UDCCSR0);
257 if ((readl(UDCCSR0) & UDCCSR0_RNE) != 0) {
258 usberr("setup packet too long");
262 udc_dump_buffer("ep0 setup read", (u8 *) data, 8);
264 if (ep0_urb->device_request.wLength == 0) {
265 usbdbg("Zero Data control Packet\n");
266 if (ep0_recv_setup(ep0_urb)) {
267 usberr("Invalid Setup Packet\n");
268 udc_dump_buffer("ep0 setup read",
272 writel(UDCCSR0_IPR, UDCCSR0);
275 /* Check direction */
276 if ((ep0_urb->device_request.bmRequestType &
277 USB_REQ_DIRECTION_MASK)
278 == USB_REQ_HOST2DEVICE) {
279 ep0state = EP0_OUT_DATA;
281 (u8 *)ep0_urb->buffer_data;
282 ep0_urb->buffer_length =
283 sizeof(ep0_urb->buffer_data);
284 ep0_urb->actual_length = 0;
285 writel(UDCCSR0_IPR, UDCCSR0);
287 /* The ep0_recv_setup function has
288 * already placed our response packet
289 * data in ep0_urb->buffer and the
291 * ep0_urb->actual_length.
293 if (ep0_recv_setup(ep0_urb)) {
295 usberr("Invalid setup packet");
296 udc_dump_buffer("ep0 setup read"
301 UDCCSR0_OPC | UDCCSR0_FST |
302 UDCCS0_FTF, UDCCSR0);
307 endpoint->tx_urb = ep0_urb;
309 usbdbg("EP0_IN_DATA");
310 ep0state = EP0_IN_DATA;
311 if (udc_write_urb(endpoint) < 0)
317 } else if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA))
318 == (UDCCSR0_OPC|UDCCSR0_SA)) {
319 usberr("Setup Active but no data. Stalling ....\n");
322 usbdbg("random early IRQs");
323 /* Some random early IRQs:
326 * - OPC got set, without SA (likely status stage)
328 writel(udccsr0 & (UDCCSR0_SA | UDCCSR0_OPC), UDCCSR0);
334 if ((udccsr0 & UDCCSR0_OPC) && !(udccsr0 & UDCCSR0_SA)) {
335 if (udc_read_urb_ep0()) {
338 if (ep0_recv_setup(ep0_urb)) {
339 /* Not a setup packet, stall next
342 udc_dump_buffer("ep0 setup read",
344 usberr("can't parse setup packet\n");
348 } else if (!(udccsr0 & UDCCSR0_OPC) &&
349 !(udccsr0 & UDCCSR0_IPR)) {
350 if (ep0_urb->device_request.wLength ==
351 ep0_urb->actual_length)
354 usberr("Premature Status\n");
360 /* GET_DESCRIPTOR etc */
361 if (udccsr0 & UDCCSR0_OPC) {
362 writel(UDCCSR0_OPC | UDCCSR0_FTF, UDCCSR0);
363 usberr("ep0in premature status");
366 /* irq was IPR clearing */
367 if (udc_write_urb(endpoint) < 0) {
368 usberr("ep0_write_error\n");
374 case EP0_XFER_COMPLETE:
375 writel(UDCCSR0_IPR, UDCCSR0);
382 writel(USIR0_IR0, USIR0);
385 static void udc_handle_ep(struct usb_endpoint_instance *endpoint)
387 int ep_addr = endpoint->endpoint_address;
388 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
389 int ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
391 u32 flags = readl(UDCCSN(ep_num)) & (UDCCSR_SST | UDCCSR_TRN);
393 writel(flags, UDCCSN(ep_num));
396 udc_read_urb(endpoint);
398 udc_write_urb(endpoint);
400 writel(UDCCSR_PC, UDCCSN(ep_num));
403 static void udc_state_changed(void)
405 int config, interface, alternate;
407 writel(readl(UDCCR) | UDCCR_SMAC, UDCCR);
409 config = (readl(UDCCR) & UDCCR_ACN) >> UDCCR_ACN_S;
410 interface = (readl(UDCCR) & UDCCR_AIN) >> UDCCR_AIN_S;
411 alternate = (readl(UDCCR) & UDCCR_AAISN) >> UDCCR_AAISN_S;
413 usbdbg("New UDC settings are: conf %d - inter %d - alter %d",
414 config, interface, alternate);
416 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
417 writel(UDCISR1_IRCC, UDCISR1);
423 struct usb_endpoint_instance *endpoint;
429 /* Suspend Interrupt Request */
430 if (readl(USIR1) & UDCCR_SUSIR) {
432 udc_ack_int_UDCCR(UDCCR_SUSIR);
437 /* Resume Interrupt Request */
438 if (readl(USIR1) & UDCCR_RESIR) {
439 udc_ack_int_UDCCR(UDCCR_RESIR);
441 usbdbg("USB resume\n");
444 if (readl(USIR1) & (1<<31)) {
449 /* Reset Interrupt Request */
450 if (readl(USIR1) & UDCCR_RSTIR) {
451 udc_ack_int_UDCCR(UDCCR_RSTIR);
454 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
457 usbdbg("UISR0: %x \n", readl(USIR0));
459 if (readl(USIR0) & 0x2)
462 /* Control traffic */
463 if (readl(USIR0) & USIR0_IR0) {
465 writel(USIR0_IR0, USIR0);
466 udc_handle_ep0(udc_device->bus->endpoint_array);
469 endpoint = udc_device->bus->endpoint_array;
470 for (i = 0; i < udc_device->bus->max_endpoints; i++) {
471 ep_num = (endpoint[i].endpoint_address) &
472 USB_ENDPOINT_NUMBER_MASK;
475 udcisr0 = readl(UDCISR0);
477 UDCISR_INT(ep_num, UDC_INT_PACKETCMP)) {
478 writel(UDCISR_INT(ep_num, UDC_INT_PACKETCMP),
480 udc_handle_ep(&endpoint[i]);
488 /* The UDCCR reg contains mask and interrupt status bits,
489 * so using '|=' isn't safe as it may ack an interrupt.
491 #define UDCCR_OEN (1 << 31) /* On-the-Go Enable */
492 #define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_UDE)
494 static inline void udc_set_mask_UDCCR(int mask)
496 writel((readl(UDCCR) & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS), UDCCR);
499 static inline void udc_clear_mask_UDCCR(int mask)
501 writel((readl(UDCCR) & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS), UDCCR);
504 static void pio_irq_enable(int ep_num)
507 writel(readl(UDCICR0) | 3 << (ep_num * 2), UDCICR0);
510 writel(readl(UDCICR1) | 3 << (ep_num * 2), UDCICR1);
517 * Allow upper layers to signal lower layers should not accept more RX data
519 void udc_set_nak(int ep_num)
527 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
528 * Switch off NAKing on this endpoint to accept more data output from host.
530 void udc_unset_nak(int ep_num)
535 int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
537 return udc_write_urb(endpoint);
540 /* Associate a physical endpoint with endpoint instance */
541 void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
542 struct usb_endpoint_instance *endpoint)
544 int ep_num, ep_addr, ep_isout, ep_type, ep_size;
545 int config, interface, alternate;
548 usbdbg("setting up endpoint id %d", id);
551 usberr("endpoint void!");
555 ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
556 if (ep_num >= UDC_MAX_ENDPOINTS) {
557 usberr("unable to setup ep %d!", ep_num);
561 pio_irq_enable(ep_num);
571 usbdbg("config %d - interface %d - alternate %d",
572 config, interface, alternate);
574 ep_addr = endpoint->endpoint_address;
575 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
576 ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
577 ep_type = ep_isout ? endpoint->rcv_attributes : endpoint->tx_attributes;
578 ep_size = ep_isout ? endpoint->rcv_packetSize : endpoint->tx_packetSize;
580 usbdbg("addr %x, num %d, dir %s, type %s, packet size %d",
582 ep_isout ? "out" : "in",
583 ep_type == USB_ENDPOINT_XFER_ISOC ? "isoc" :
584 ep_type == USB_ENDPOINT_XFER_BULK ? "bulk" :
585 ep_type == USB_ENDPOINT_XFER_INT ? "int" : "???",
589 /* Configure UDCCRx */
591 tmp |= (config << UDCCONR_CN_S) & UDCCONR_CN;
592 tmp |= (interface << UDCCONR_IN_S) & UDCCONR_IN;
593 tmp |= (alternate << UDCCONR_AISN_S) & UDCCONR_AISN;
594 tmp |= (ep_num << UDCCONR_EN_S) & UDCCONR_EN;
595 tmp |= (ep_type << UDCCONR_ET_S) & UDCCONR_ET;
596 tmp |= ep_isout ? 0 : UDCCONR_ED;
597 tmp |= (ep_size << UDCCONR_MPS_S) & UDCCONR_MPS;
600 writel(tmp, UDCCN(ep_num));
602 usbdbg("UDCCR%c = %x", 'A' + ep_num-1, readl(UDCCN(ep_num)));
603 usbdbg("UDCCSR%c = %x", 'A' + ep_num-1, readl(UDCCSN(ep_num)));
606 /* Connect the USB device to the bus */
607 void udc_connect(void)
609 usbdbg("UDC connect");
611 #ifdef CONFIG_USB_DEV_PULLUP_GPIO
612 /* Turn on the USB connection by enabling the pullup resistor */
613 writel(readl(GPDR(CONFIG_USB_DEV_PULLUP_GPIO))
614 | GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
615 GPDR(CONFIG_USB_DEV_PULLUP_GPIO));
616 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
618 /* Host port 2 transceiver D+ pull up enable */
619 writel(readl(UP2OCR) | UP2OCR_DPPUE, UP2OCR);
623 /* Disconnect the USB device to the bus */
624 void udc_disconnect(void)
626 usbdbg("UDC disconnect");
628 #ifdef CONFIG_USB_DEV_PULLUP_GPIO
629 /* Turn off the USB connection by disabling the pullup resistor */
630 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
632 /* Host port 2 transceiver D+ pull up disable */
633 writel(readl(UP2OCR) & ~UP2OCR_DPPUE, UP2OCR);
637 /* Switch on the UDC */
638 void udc_enable(struct usb_device_instance *device)
643 /* enable endpoint 0, A, B's Packet Complete Interrupt. */
644 writel(0xffffffff, UDCICR0);
645 writel(0xa8000000, UDCICR1);
647 /* clear the interrupt status/control registers */
648 writel(0xffffffff, UDCISR0);
649 writel(0xffffffff, UDCISR1);
652 udc_set_mask_UDCCR(UDCCR_UDE);
656 ep0_urb = usbd_alloc_urb(udc_device,
657 udc_device->bus->endpoint_array);
659 usbinfo("ep0_urb %p already allocated", ep0_urb);
661 usbdbg("UDC Enabled\n");
664 /* Need to check this again */
665 void udc_disable(void)
667 usbdbg("disable UDC");
669 udc_clear_mask_UDCCR(UDCCR_UDE);
671 /* Disable clock for USB device */
672 writel(readl(CKEN) & ~CKEN11_USB, CKEN);
676 usbd_dealloc_urb(ep0_urb);
680 /* Reset device pointer */
684 /* Allow udc code to do any additional startup */
685 void udc_startup_events(struct usb_device_instance *device)
687 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT */
688 usbd_device_event_irq(device, DEVICE_INIT, 0);
690 /* The DEVICE_CREATE event puts the USB device in the state
692 usbd_device_event_irq(device, DEVICE_CREATE, 0);
694 /* Some USB controller driver implementations signal
695 * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
696 * DEVICE_HUB_CONFIGURED causes a transition to the state
697 * STATE_POWERED, and DEVICE_RESET causes a transition to
698 * the state STATE_DEFAULT.
703 /* Initialize h/w stuff */
707 usbdbg("PXA27x usbd start");
709 /* Enable clock for USB device */
710 writel(readl(CKEN) | CKEN11_USB, CKEN);
712 /* Disable the UDC */
713 udc_clear_mask_UDCCR(UDCCR_UDE);
715 /* Disable IRQs: we don't use them */