blob: 942355528084b0022c475b5e35d3b136f6957e2c [file] [log] [blame]
Remy Bohmer3ccbfb22009-04-05 11:43:28 +02001/*
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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Remy Bohmer3ccbfb22009-04-05 11:43:28 +02009 */
10
11
12#include <common.h>
13#include <config.h>
14#include <asm/byteorder.h>
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020015#include <usbdevice.h>
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020016#include <asm/arch/hardware.h>
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020017#include <asm/io.h>
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020018#include <usb/pxa27x_udc.h>
Troy Kisky449697f2013-10-10 15:28:04 -070019#include <usb/udc.h>
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020020
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020021#include "ep0.h"
22
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020023/* number of endpoints on this UDC */
24#define UDC_MAX_ENDPOINTS 24
25
26static struct urb *ep0_urb;
27static struct usb_device_instance *udc_device;
28static int ep0state = EP0_IDLE;
29
30#ifdef USBDDBG
31static void udc_dump_buffer(char *name, u8 *buf, int len)
32{
33 usbdbg("%s - buf %p, len %d", name, buf, len);
34 print_buffer(0, buf, 1, len, 0);
35}
36#else
37#define udc_dump_buffer(name, buf, len) /* void */
38#endif
39
40static inline void udc_ack_int_UDCCR(int mask)
41{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020042 writel(readl(USIR1) | mask, USIR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020043}
44
45/*
46 * If the endpoint has an active tx_urb, then the next packet of data from the
47 * URB is written to the tx FIFO.
48 * The total amount of data in the urb is given by urb->actual_length.
49 * The maximum amount of data that can be sent in any one packet is given by
50 * endpoint->tx_packetSize.
51 * The number of data bytes from this URB that have already been transmitted
52 * is given by endpoint->sent.
53 * endpoint->last is updated by this routine with the number of data bytes
54 * transmitted in this packet.
55 */
56static int udc_write_urb(struct usb_endpoint_instance *endpoint)
57{
58 struct urb *urb = endpoint->tx_urb;
59 int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020060 u32 *data32 = (u32 *) urb->buffer;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020061 u8 *data8 = (u8 *) urb->buffer;
62 unsigned int i, n, w, b, is_short;
63 int timeout = 2000; /* 2ms */
64
65 if (!urb || !urb->actual_length)
66 return -1;
67
Masahiro Yamadab4141192014-11-07 03:03:31 +090068 n = min_t(unsigned int, urb->actual_length - endpoint->sent,
69 endpoint->tx_packetSize);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020070 if (n <= 0)
71 return -1;
72
73 usbdbg("write urb on ep %d", ep_num);
74#if defined(USBDDBG) && defined(USBDPARANOIA)
75 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
76 urb->buffer, urb->buffer_length, urb->actual_length);
77 usbdbg("endpoint: sent %d, tx_packetSize %d, last %d",
78 endpoint->sent, endpoint->tx_packetSize, endpoint->last);
79#endif
80
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
87 /* Prepare for data send */
88 if (ep_num)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020089 writel(UDCCSR_PC ,UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020090
91 for (i = 0; i < w; i++)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020092 writel(data32[endpoint->sent / 4 + i], UDCDN(ep_num));
93
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020094 for (i = 0; i < b; i++)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020095 writeb(data8[endpoint->sent + w * 4 + i], UDCDN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020096
97 /* Set "Packet Complete" if less data then tx_packetSize */
98 if (is_short)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020099 writel(ep_num ? UDCCSR_SP : UDCCSR0_IPR, UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200100
101 /* Wait for data sent */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200102 if (ep_num) {
103 while (!(readl(UDCCSN(ep_num)) & UDCCSR_PC)) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200104 if (timeout-- == 0)
105 return -1;
106 else
107 udelay(1);
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200108 }
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200109 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200110
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200111 endpoint->last = n;
112
113 if (ep_num) {
114 usbd_tx_complete(endpoint);
115 } else {
116 endpoint->sent += n;
117 endpoint->last -= n;
118 }
119
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200120 if (endpoint->sent >= urb->actual_length) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200121 urb->actual_length = 0;
122 endpoint->sent = 0;
123 endpoint->last = 0;
124 }
125
126 if ((endpoint->sent >= urb->actual_length) && (!ep_num)) {
127 usbdbg("ep0 IN stage done");
128 if (is_short)
129 ep0state = EP0_IDLE;
130 else
131 ep0state = EP0_XFER_COMPLETE;
132 }
133
134 return 0;
135}
136
137static int udc_read_urb(struct usb_endpoint_instance *endpoint)
138{
139 struct urb *urb = endpoint->rcv_urb;
140 int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200141 u32 *data32 = (u32 *) urb->buffer;
Mike Dunnecc8edb2013-06-26 12:33:54 -0700142 unsigned int i, n;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200143
144 usbdbg("read urb on ep %d", ep_num);
145#if defined(USBDDBG) && defined(USBDPARANOIA)
146 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
147 urb->buffer, urb->buffer_length, urb->actual_length);
148 usbdbg("endpoint: rcv_packetSize %d",
149 endpoint->rcv_packetSize);
150#endif
151
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200152 if (readl(UDCCSN(ep_num)) & UDCCSR_BNE)
153 n = readl(UDCBCN(ep_num)) & 0x3ff;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200154 else /* zlp */
155 n = 0;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200156
Mike Dunnecc8edb2013-06-26 12:33:54 -0700157 usbdbg("n %d%s", n, n != endpoint->rcv_packetSize ? "-s" : "");
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200158 for (i = 0; i < n; i += 4)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200159 data32[urb->actual_length / 4 + i / 4] = readl(UDCDN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200160
161 udc_dump_buffer("urb read", (u8 *) data32, urb->actual_length + n);
162 usbd_rcv_complete(endpoint, n, 0);
163
164 return 0;
165}
166
167static int udc_read_urb_ep0(void)
168{
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200169 u32 *data32 = (u32 *) ep0_urb->buffer;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200170 u8 *data8 = (u8 *) ep0_urb->buffer;
171 unsigned int i, n, w, b;
172
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200173 usbdbg("read urb on ep 0");
174#if defined(USBDDBG) && defined(USBDPARANOIA)
175 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
176 ep0_urb->buffer, ep0_urb->buffer_length, ep0_urb->actual_length);
177#endif
178
179 n = readl(UDCBCR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200180 w = n / 4;
181 b = n % 4;
182
183 for (i = 0; i < w; i++) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200184 data32[ep0_urb->actual_length / 4 + i] = readl(UDCDN(0));
Wolfgang Denk10879aa2011-12-19 11:52:36 +0100185 /* ep0_urb->actual_length += 4; */
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200186 }
187
188 for (i = 0; i < b; i++) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200189 data8[ep0_urb->actual_length + w * 4 + i] = readb(UDCDN(0));
Wolfgang Denk10879aa2011-12-19 11:52:36 +0100190 /* ep0_urb->actual_length++; */
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200191 }
192
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200193 ep0_urb->actual_length += n;
194
195 udc_dump_buffer("urb read", (u8 *) data32, ep0_urb->actual_length);
196
197 writel(UDCCSR0_OPC | UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200198 if (ep0_urb->actual_length == ep0_urb->device_request.wLength)
199 return 1;
200
201 return 0;
202}
203
204static void udc_handle_ep0(struct usb_endpoint_instance *endpoint)
205{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200206 u32 udccsr0 = readl(UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200207 u32 *data = (u32 *) &ep0_urb->device_request;
208 int i;
209
210 usbdbg("udccsr0 %x", udccsr0);
211
212 /* Clear stall status */
213 if (udccsr0 & UDCCSR0_SST) {
214 usberr("clear stall status");
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200215 writel(UDCCSR0_SST, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200216 ep0state = EP0_IDLE;
217 }
218
219 /* previous request unfinished? non-error iff back-to-back ... */
220 if ((udccsr0 & UDCCSR0_SA) != 0 && ep0state != EP0_IDLE)
221 ep0state = EP0_IDLE;
222
223 switch (ep0state) {
224
225 case EP0_IDLE:
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200226 udccsr0 = readl(UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200227 /* Start control request? */
228 if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE))
229 == (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)) {
230
231 /* Read SETUP packet.
232 * SETUP packet size is 8 bytes (aka 2 words)
233 */
234 usbdbg("try reading SETUP packet");
235 for (i = 0; i < 2; i++) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200236 if ((readl(UDCCSR0) & UDCCSR0_RNE) == 0) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200237 usberr("setup packet too short:%d", i);
238 goto stall;
239 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200240 data[i] = readl(UDCDR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200241 }
242
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200243 writel(readl(UDCCSR0) | UDCCSR0_OPC | UDCCSR0_SA, UDCCSR0);
244 if ((readl(UDCCSR0) & UDCCSR0_RNE) != 0) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200245 usberr("setup packet too long");
246 goto stall;
247 }
248
249 udc_dump_buffer("ep0 setup read", (u8 *) data, 8);
250
251 if (ep0_urb->device_request.wLength == 0) {
252 usbdbg("Zero Data control Packet\n");
253 if (ep0_recv_setup(ep0_urb)) {
254 usberr("Invalid Setup Packet\n");
255 udc_dump_buffer("ep0 setup read",
256 (u8 *)data, 8);
257 goto stall;
258 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200259 writel(UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200260 ep0state = EP0_IDLE;
261 } else {
262 /* Check direction */
263 if ((ep0_urb->device_request.bmRequestType &
264 USB_REQ_DIRECTION_MASK)
265 == USB_REQ_HOST2DEVICE) {
266 ep0state = EP0_OUT_DATA;
267 ep0_urb->buffer =
268 (u8 *)ep0_urb->buffer_data;
269 ep0_urb->buffer_length =
270 sizeof(ep0_urb->buffer_data);
271 ep0_urb->actual_length = 0;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200272 writel(UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200273 } else {
274 /* The ep0_recv_setup function has
275 * already placed our response packet
276 * data in ep0_urb->buffer and the
277 * packet length in
278 * ep0_urb->actual_length.
279 */
280 if (ep0_recv_setup(ep0_urb)) {
281stall:
282 usberr("Invalid setup packet");
283 udc_dump_buffer("ep0 setup read"
284 , (u8 *) data, 8);
285 ep0state = EP0_IDLE;
286
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200287 writel(UDCCSR0_SA |
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200288 UDCCSR0_OPC | UDCCSR0_FST |
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200289 UDCCS0_FTF, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200290
291 return;
292 }
293
294 endpoint->tx_urb = ep0_urb;
295 endpoint->sent = 0;
296 usbdbg("EP0_IN_DATA");
297 ep0state = EP0_IN_DATA;
298 if (udc_write_urb(endpoint) < 0)
299 goto stall;
300
301 }
302 }
303 return;
304 } else if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA))
305 == (UDCCSR0_OPC|UDCCSR0_SA)) {
306 usberr("Setup Active but no data. Stalling ....\n");
307 goto stall;
308 } else {
309 usbdbg("random early IRQs");
310 /* Some random early IRQs:
311 * - we acked FST
312 * - IPR cleared
313 * - OPC got set, without SA (likely status stage)
314 */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200315 writel(udccsr0 & (UDCCSR0_SA | UDCCSR0_OPC), UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200316 }
317 break;
318
319 case EP0_OUT_DATA:
320
321 if ((udccsr0 & UDCCSR0_OPC) && !(udccsr0 & UDCCSR0_SA)) {
322 if (udc_read_urb_ep0()) {
323read_complete:
324 ep0state = EP0_IDLE;
325 if (ep0_recv_setup(ep0_urb)) {
326 /* Not a setup packet, stall next
327 * EP0 transaction
328 */
329 udc_dump_buffer("ep0 setup read",
330 (u8 *) data, 8);
331 usberr("can't parse setup packet\n");
332 goto stall;
333 }
334 }
335 } else if (!(udccsr0 & UDCCSR0_OPC) &&
336 !(udccsr0 & UDCCSR0_IPR)) {
337 if (ep0_urb->device_request.wLength ==
338 ep0_urb->actual_length)
339 goto read_complete;
340
341 usberr("Premature Status\n");
342 ep0state = EP0_IDLE;
343 }
344 break;
345
346 case EP0_IN_DATA:
347 /* GET_DESCRIPTOR etc */
348 if (udccsr0 & UDCCSR0_OPC) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200349 writel(UDCCSR0_OPC | UDCCSR0_FTF, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200350 usberr("ep0in premature status");
351 ep0state = EP0_IDLE;
352 } else {
353 /* irq was IPR clearing */
354 if (udc_write_urb(endpoint) < 0) {
355 usberr("ep0_write_error\n");
356 goto stall;
357 }
358 }
359 break;
360
361 case EP0_XFER_COMPLETE:
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200362 writel(UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200363 ep0state = EP0_IDLE;
364 break;
365
366 default:
367 usbdbg("Default\n");
368 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200369 writel(USIR0_IR0, USIR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200370}
371
372static void udc_handle_ep(struct usb_endpoint_instance *endpoint)
373{
374 int ep_addr = endpoint->endpoint_address;
375 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
376 int ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
377
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200378 u32 flags = readl(UDCCSN(ep_num)) & (UDCCSR_SST | UDCCSR_TRN);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200379 if (flags)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200380 writel(flags, UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200381
382 if (ep_isout)
383 udc_read_urb(endpoint);
384 else
385 udc_write_urb(endpoint);
386
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200387 writel(UDCCSR_PC, UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200388}
389
390static void udc_state_changed(void)
391{
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200392
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200393 writel(readl(UDCCR) | UDCCR_SMAC, UDCCR);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200394
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200395 usbdbg("New UDC settings are: conf %d - inter %d - alter %d",
Mike Dunnecc8edb2013-06-26 12:33:54 -0700396 (readl(UDCCR) & UDCCR_ACN) >> UDCCR_ACN_S,
397 (readl(UDCCR) & UDCCR_AIN) >> UDCCR_AIN_S,
398 (readl(UDCCR) & UDCCR_AAISN) >> UDCCR_AAISN_S);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200399
400 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200401 writel(UDCISR1_IRCC, UDCISR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200402}
403
404void udc_irq(void)
405{
406 int handled;
407 struct usb_endpoint_instance *endpoint;
408 int ep_num, i;
409 u32 udcisr0;
410
411 do {
412 handled = 0;
413 /* Suspend Interrupt Request */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200414 if (readl(USIR1) & UDCCR_SUSIR) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200415 usbdbg("Suspend\n");
416 udc_ack_int_UDCCR(UDCCR_SUSIR);
417 handled = 1;
418 ep0state = EP0_IDLE;
419 }
420
421 /* Resume Interrupt Request */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200422 if (readl(USIR1) & UDCCR_RESIR) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200423 udc_ack_int_UDCCR(UDCCR_RESIR);
424 handled = 1;
425 usbdbg("USB resume\n");
426 }
427
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200428 if (readl(USIR1) & (1<<31)) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200429 handled = 1;
430 udc_state_changed();
431 }
432
433 /* Reset Interrupt Request */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200434 if (readl(USIR1) & UDCCR_RSTIR) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200435 udc_ack_int_UDCCR(UDCCR_RSTIR);
436 handled = 1;
437 usbdbg("Reset\n");
438 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
439 } else {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200440 if (readl(USIR0))
441 usbdbg("UISR0: %x \n", readl(USIR0));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200442
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200443 if (readl(USIR0) & 0x2)
444 writel(0x2, USIR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200445
446 /* Control traffic */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200447 if (readl(USIR0) & USIR0_IR0) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200448 handled = 1;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200449 writel(USIR0_IR0, USIR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200450 udc_handle_ep0(udc_device->bus->endpoint_array);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200451 }
452
453 endpoint = udc_device->bus->endpoint_array;
454 for (i = 0; i < udc_device->bus->max_endpoints; i++) {
455 ep_num = (endpoint[i].endpoint_address) &
456 USB_ENDPOINT_NUMBER_MASK;
457 if (!ep_num)
458 continue;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200459 udcisr0 = readl(UDCISR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200460 if (udcisr0 &
461 UDCISR_INT(ep_num, UDC_INT_PACKETCMP)) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200462 writel(UDCISR_INT(ep_num, UDC_INT_PACKETCMP),
463 UDCISR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200464 udc_handle_ep(&endpoint[i]);
465 }
466 }
467 }
468
469 } while (handled);
470}
471
472/* The UDCCR reg contains mask and interrupt status bits,
473 * so using '|=' isn't safe as it may ack an interrupt.
474 */
475#define UDCCR_OEN (1 << 31) /* On-the-Go Enable */
476#define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_UDE)
477
478static inline void udc_set_mask_UDCCR(int mask)
479{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200480 writel((readl(UDCCR) & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS), UDCCR);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200481}
482
483static inline void udc_clear_mask_UDCCR(int mask)
484{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200485 writel((readl(UDCCR) & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS), UDCCR);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200486}
487
488static void pio_irq_enable(int ep_num)
489{
490 if (ep_num < 16)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200491 writel(readl(UDCICR0) | 3 << (ep_num * 2), UDCICR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200492 else {
493 ep_num -= 16;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200494 writel(readl(UDCICR1) | 3 << (ep_num * 2), UDCICR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200495 }
496}
497
498/*
499 * udc_set_nak
500 *
501 * Allow upper layers to signal lower layers should not accept more RX data
502 */
503void udc_set_nak(int ep_num)
504{
505 /* TODO */
506}
507
508/*
509 * udc_unset_nak
510 *
511 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
512 * Switch off NAKing on this endpoint to accept more data output from host.
513 */
514void udc_unset_nak(int ep_num)
515{
516 /* TODO */
517}
518
519int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
520{
521 return udc_write_urb(endpoint);
522}
523
524/* Associate a physical endpoint with endpoint instance */
525void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
526 struct usb_endpoint_instance *endpoint)
527{
528 int ep_num, ep_addr, ep_isout, ep_type, ep_size;
529 int config, interface, alternate;
530 u32 tmp;
531
532 usbdbg("setting up endpoint id %d", id);
533
534 if (!endpoint) {
535 usberr("endpoint void!");
536 return;
537 }
538
539 ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
540 if (ep_num >= UDC_MAX_ENDPOINTS) {
541 usberr("unable to setup ep %d!", ep_num);
542 return;
543 }
544
545 pio_irq_enable(ep_num);
546 if (ep_num == 0) {
547 /* Done for ep0 */
548 return;
549 }
550
551 config = 1;
552 interface = 0;
553 alternate = 0;
554
555 usbdbg("config %d - interface %d - alternate %d",
556 config, interface, alternate);
557
558 ep_addr = endpoint->endpoint_address;
559 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
560 ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
561 ep_type = ep_isout ? endpoint->rcv_attributes : endpoint->tx_attributes;
562 ep_size = ep_isout ? endpoint->rcv_packetSize : endpoint->tx_packetSize;
563
564 usbdbg("addr %x, num %d, dir %s, type %s, packet size %d",
565 ep_addr, ep_num,
566 ep_isout ? "out" : "in",
567 ep_type == USB_ENDPOINT_XFER_ISOC ? "isoc" :
568 ep_type == USB_ENDPOINT_XFER_BULK ? "bulk" :
569 ep_type == USB_ENDPOINT_XFER_INT ? "int" : "???",
570 ep_size
571 );
572
573 /* Configure UDCCRx */
574 tmp = 0;
575 tmp |= (config << UDCCONR_CN_S) & UDCCONR_CN;
576 tmp |= (interface << UDCCONR_IN_S) & UDCCONR_IN;
577 tmp |= (alternate << UDCCONR_AISN_S) & UDCCONR_AISN;
578 tmp |= (ep_num << UDCCONR_EN_S) & UDCCONR_EN;
579 tmp |= (ep_type << UDCCONR_ET_S) & UDCCONR_ET;
580 tmp |= ep_isout ? 0 : UDCCONR_ED;
581 tmp |= (ep_size << UDCCONR_MPS_S) & UDCCONR_MPS;
582 tmp |= UDCCONR_EE;
583
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200584 writel(tmp, UDCCN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200585
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200586 usbdbg("UDCCR%c = %x", 'A' + ep_num-1, readl(UDCCN(ep_num)));
587 usbdbg("UDCCSR%c = %x", 'A' + ep_num-1, readl(UDCCSN(ep_num)));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200588}
589
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200590/* Connect the USB device to the bus */
591void udc_connect(void)
592{
593 usbdbg("UDC connect");
594
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200595#ifdef CONFIG_USB_DEV_PULLUP_GPIO
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200596 /* Turn on the USB connection by enabling the pullup resistor */
Mike Dunne0e89e22013-04-12 11:59:15 -0700597 writel(readl(GPDR(CONFIG_USB_DEV_PULLUP_GPIO))
598 | GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
599 GPDR(CONFIG_USB_DEV_PULLUP_GPIO));
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200600 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
601#else
602 /* Host port 2 transceiver D+ pull up enable */
603 writel(readl(UP2OCR) | UP2OCR_DPPUE, UP2OCR);
604#endif
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200605}
606
607/* Disconnect the USB device to the bus */
608void udc_disconnect(void)
609{
610 usbdbg("UDC disconnect");
611
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200612#ifdef CONFIG_USB_DEV_PULLUP_GPIO
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200613 /* Turn off the USB connection by disabling the pullup resistor */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200614 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
615#else
616 /* Host port 2 transceiver D+ pull up disable */
617 writel(readl(UP2OCR) & ~UP2OCR_DPPUE, UP2OCR);
618#endif
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200619}
620
621/* Switch on the UDC */
622void udc_enable(struct usb_device_instance *device)
623{
624
625 ep0state = EP0_IDLE;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200626
627 /* enable endpoint 0, A, B's Packet Complete Interrupt. */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200628 writel(0xffffffff, UDCICR0);
629 writel(0xa8000000, UDCICR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200630
631 /* clear the interrupt status/control registers */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200632 writel(0xffffffff, UDCISR0);
633 writel(0xffffffff, UDCISR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200634
635 /* set UDC-enable */
636 udc_set_mask_UDCCR(UDCCR_UDE);
637
638 udc_device = device;
639 if (!ep0_urb)
640 ep0_urb = usbd_alloc_urb(udc_device,
641 udc_device->bus->endpoint_array);
642 else
643 usbinfo("ep0_urb %p already allocated", ep0_urb);
644
645 usbdbg("UDC Enabled\n");
646}
647
648/* Need to check this again */
649void udc_disable(void)
650{
651 usbdbg("disable UDC");
652
653 udc_clear_mask_UDCCR(UDCCR_UDE);
654
655 /* Disable clock for USB device */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200656 writel(readl(CKEN) & ~CKEN11_USB, CKEN);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200657
658 /* Free ep0 URB */
659 if (ep0_urb) {
660 usbd_dealloc_urb(ep0_urb);
661 ep0_urb = NULL;
662 }
663
664 /* Reset device pointer */
665 udc_device = NULL;
666}
667
668/* Allow udc code to do any additional startup */
669void udc_startup_events(struct usb_device_instance *device)
670{
671 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT */
672 usbd_device_event_irq(device, DEVICE_INIT, 0);
673
674 /* The DEVICE_CREATE event puts the USB device in the state
675 * STATE_ATTACHED */
676 usbd_device_event_irq(device, DEVICE_CREATE, 0);
677
678 /* Some USB controller driver implementations signal
679 * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
680 * DEVICE_HUB_CONFIGURED causes a transition to the state
681 * STATE_POWERED, and DEVICE_RESET causes a transition to
682 * the state STATE_DEFAULT.
683 */
684 udc_enable(device);
685}
686
687/* Initialize h/w stuff */
688int udc_init(void)
689{
690 udc_device = NULL;
691 usbdbg("PXA27x usbd start");
692
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200693 /* Enable clock for USB device */
694 writel(readl(CKEN) | CKEN11_USB, CKEN);
695
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200696 /* Disable the UDC */
697 udc_clear_mask_UDCCR(UDCCR_UDE);
698
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200699 /* Disable IRQs: we don't use them */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200700 writel(0, UDCICR0);
701 writel(0, UDCICR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200702
703 return 0;
704}