blob: 598923dfc8670f685d9988f8abe252665fc81dfc [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 *
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
25
26#include <common.h>
27#include <config.h>
28#include <asm/byteorder.h>
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020029#include <usbdevice.h>
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020030#include <asm/arch/hardware.h>
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020031#include <asm/io.h>
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020032#include <usb/pxa27x_udc.h>
33
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020034#include "ep0.h"
35
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020036/* number of endpoints on this UDC */
37#define UDC_MAX_ENDPOINTS 24
38
39static struct urb *ep0_urb;
40static struct usb_device_instance *udc_device;
41static int ep0state = EP0_IDLE;
42
43#ifdef USBDDBG
44static void udc_dump_buffer(char *name, u8 *buf, int len)
45{
46 usbdbg("%s - buf %p, len %d", name, buf, len);
47 print_buffer(0, buf, 1, len, 0);
48}
49#else
50#define udc_dump_buffer(name, buf, len) /* void */
51#endif
52
53static inline void udc_ack_int_UDCCR(int mask)
54{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020055 writel(readl(USIR1) | mask, USIR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020056}
57
58/*
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.
68 */
69static int udc_write_urb(struct usb_endpoint_instance *endpoint)
70{
71 struct urb *urb = endpoint->tx_urb;
72 int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020073 u32 *data32 = (u32 *) urb->buffer;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020074 u8 *data8 = (u8 *) urb->buffer;
75 unsigned int i, n, w, b, is_short;
76 int timeout = 2000; /* 2ms */
77
78 if (!urb || !urb->actual_length)
79 return -1;
80
81 n = MIN(urb->actual_length - endpoint->sent, endpoint->tx_packetSize);
82 if (n <= 0)
83 return -1;
84
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);
91#endif
92
93 is_short = n != endpoint->tx_packetSize;
94 w = n / 4;
95 b = n % 4;
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);
98
99 /* Prepare for data send */
100 if (ep_num)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200101 writel(UDCCSR_PC ,UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200102
103 for (i = 0; i < w; i++)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200104 writel(data32[endpoint->sent / 4 + i], UDCDN(ep_num));
105
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200106 for (i = 0; i < b; i++)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200107 writeb(data8[endpoint->sent + w * 4 + i], UDCDN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200108
109 /* Set "Packet Complete" if less data then tx_packetSize */
110 if (is_short)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200111 writel(ep_num ? UDCCSR_SP : UDCCSR0_IPR, UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200112
113 /* Wait for data sent */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200114 if (ep_num) {
115 while (!(readl(UDCCSN(ep_num)) & UDCCSR_PC)) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200116 if (timeout-- == 0)
117 return -1;
118 else
119 udelay(1);
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200120 }
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200121 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200122
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200123 endpoint->last = n;
124
125 if (ep_num) {
126 usbd_tx_complete(endpoint);
127 } else {
128 endpoint->sent += n;
129 endpoint->last -= n;
130 }
131
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200132 if (endpoint->sent >= urb->actual_length) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200133 urb->actual_length = 0;
134 endpoint->sent = 0;
135 endpoint->last = 0;
136 }
137
138 if ((endpoint->sent >= urb->actual_length) && (!ep_num)) {
139 usbdbg("ep0 IN stage done");
140 if (is_short)
141 ep0state = EP0_IDLE;
142 else
143 ep0state = EP0_XFER_COMPLETE;
144 }
145
146 return 0;
147}
148
149static int udc_read_urb(struct usb_endpoint_instance *endpoint)
150{
151 struct urb *urb = endpoint->rcv_urb;
152 int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200153 u32 *data32 = (u32 *) urb->buffer;
Mike Dunnecc8edb2013-06-26 12:33:54 -0700154 unsigned int i, n;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200155
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);
162#endif
163
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200164 if (readl(UDCCSN(ep_num)) & UDCCSR_BNE)
165 n = readl(UDCBCN(ep_num)) & 0x3ff;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200166 else /* zlp */
167 n = 0;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200168
Mike Dunnecc8edb2013-06-26 12:33:54 -0700169 usbdbg("n %d%s", n, n != endpoint->rcv_packetSize ? "-s" : "");
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200170 for (i = 0; i < n; i += 4)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200171 data32[urb->actual_length / 4 + i / 4] = readl(UDCDN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200172
173 udc_dump_buffer("urb read", (u8 *) data32, urb->actual_length + n);
174 usbd_rcv_complete(endpoint, n, 0);
175
176 return 0;
177}
178
179static int udc_read_urb_ep0(void)
180{
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200181 u32 *data32 = (u32 *) ep0_urb->buffer;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200182 u8 *data8 = (u8 *) ep0_urb->buffer;
183 unsigned int i, n, w, b;
184
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200185 usbdbg("read urb on ep 0");
186#if defined(USBDDBG) && defined(USBDPARANOIA)
187 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
188 ep0_urb->buffer, ep0_urb->buffer_length, ep0_urb->actual_length);
189#endif
190
191 n = readl(UDCBCR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200192 w = n / 4;
193 b = n % 4;
194
195 for (i = 0; i < w; i++) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200196 data32[ep0_urb->actual_length / 4 + i] = readl(UDCDN(0));
Wolfgang Denk10879aa2011-12-19 11:52:36 +0100197 /* ep0_urb->actual_length += 4; */
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200198 }
199
200 for (i = 0; i < b; i++) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200201 data8[ep0_urb->actual_length + w * 4 + i] = readb(UDCDN(0));
Wolfgang Denk10879aa2011-12-19 11:52:36 +0100202 /* ep0_urb->actual_length++; */
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200203 }
204
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200205 ep0_urb->actual_length += n;
206
207 udc_dump_buffer("urb read", (u8 *) data32, ep0_urb->actual_length);
208
209 writel(UDCCSR0_OPC | UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200210 if (ep0_urb->actual_length == ep0_urb->device_request.wLength)
211 return 1;
212
213 return 0;
214}
215
216static void udc_handle_ep0(struct usb_endpoint_instance *endpoint)
217{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200218 u32 udccsr0 = readl(UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200219 u32 *data = (u32 *) &ep0_urb->device_request;
220 int i;
221
222 usbdbg("udccsr0 %x", udccsr0);
223
224 /* Clear stall status */
225 if (udccsr0 & UDCCSR0_SST) {
226 usberr("clear stall status");
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200227 writel(UDCCSR0_SST, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200228 ep0state = EP0_IDLE;
229 }
230
231 /* previous request unfinished? non-error iff back-to-back ... */
232 if ((udccsr0 & UDCCSR0_SA) != 0 && ep0state != EP0_IDLE)
233 ep0state = EP0_IDLE;
234
235 switch (ep0state) {
236
237 case EP0_IDLE:
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200238 udccsr0 = readl(UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200239 /* Start control request? */
240 if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE))
241 == (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)) {
242
243 /* Read SETUP packet.
244 * SETUP packet size is 8 bytes (aka 2 words)
245 */
246 usbdbg("try reading SETUP packet");
247 for (i = 0; i < 2; i++) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200248 if ((readl(UDCCSR0) & UDCCSR0_RNE) == 0) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200249 usberr("setup packet too short:%d", i);
250 goto stall;
251 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200252 data[i] = readl(UDCDR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200253 }
254
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200255 writel(readl(UDCCSR0) | UDCCSR0_OPC | UDCCSR0_SA, UDCCSR0);
256 if ((readl(UDCCSR0) & UDCCSR0_RNE) != 0) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200257 usberr("setup packet too long");
258 goto stall;
259 }
260
261 udc_dump_buffer("ep0 setup read", (u8 *) data, 8);
262
263 if (ep0_urb->device_request.wLength == 0) {
264 usbdbg("Zero Data control Packet\n");
265 if (ep0_recv_setup(ep0_urb)) {
266 usberr("Invalid Setup Packet\n");
267 udc_dump_buffer("ep0 setup read",
268 (u8 *)data, 8);
269 goto stall;
270 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200271 writel(UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200272 ep0state = EP0_IDLE;
273 } else {
274 /* Check direction */
275 if ((ep0_urb->device_request.bmRequestType &
276 USB_REQ_DIRECTION_MASK)
277 == USB_REQ_HOST2DEVICE) {
278 ep0state = EP0_OUT_DATA;
279 ep0_urb->buffer =
280 (u8 *)ep0_urb->buffer_data;
281 ep0_urb->buffer_length =
282 sizeof(ep0_urb->buffer_data);
283 ep0_urb->actual_length = 0;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200284 writel(UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200285 } else {
286 /* The ep0_recv_setup function has
287 * already placed our response packet
288 * data in ep0_urb->buffer and the
289 * packet length in
290 * ep0_urb->actual_length.
291 */
292 if (ep0_recv_setup(ep0_urb)) {
293stall:
294 usberr("Invalid setup packet");
295 udc_dump_buffer("ep0 setup read"
296 , (u8 *) data, 8);
297 ep0state = EP0_IDLE;
298
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200299 writel(UDCCSR0_SA |
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200300 UDCCSR0_OPC | UDCCSR0_FST |
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200301 UDCCS0_FTF, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200302
303 return;
304 }
305
306 endpoint->tx_urb = ep0_urb;
307 endpoint->sent = 0;
308 usbdbg("EP0_IN_DATA");
309 ep0state = EP0_IN_DATA;
310 if (udc_write_urb(endpoint) < 0)
311 goto stall;
312
313 }
314 }
315 return;
316 } else if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA))
317 == (UDCCSR0_OPC|UDCCSR0_SA)) {
318 usberr("Setup Active but no data. Stalling ....\n");
319 goto stall;
320 } else {
321 usbdbg("random early IRQs");
322 /* Some random early IRQs:
323 * - we acked FST
324 * - IPR cleared
325 * - OPC got set, without SA (likely status stage)
326 */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200327 writel(udccsr0 & (UDCCSR0_SA | UDCCSR0_OPC), UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200328 }
329 break;
330
331 case EP0_OUT_DATA:
332
333 if ((udccsr0 & UDCCSR0_OPC) && !(udccsr0 & UDCCSR0_SA)) {
334 if (udc_read_urb_ep0()) {
335read_complete:
336 ep0state = EP0_IDLE;
337 if (ep0_recv_setup(ep0_urb)) {
338 /* Not a setup packet, stall next
339 * EP0 transaction
340 */
341 udc_dump_buffer("ep0 setup read",
342 (u8 *) data, 8);
343 usberr("can't parse setup packet\n");
344 goto stall;
345 }
346 }
347 } else if (!(udccsr0 & UDCCSR0_OPC) &&
348 !(udccsr0 & UDCCSR0_IPR)) {
349 if (ep0_urb->device_request.wLength ==
350 ep0_urb->actual_length)
351 goto read_complete;
352
353 usberr("Premature Status\n");
354 ep0state = EP0_IDLE;
355 }
356 break;
357
358 case EP0_IN_DATA:
359 /* GET_DESCRIPTOR etc */
360 if (udccsr0 & UDCCSR0_OPC) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200361 writel(UDCCSR0_OPC | UDCCSR0_FTF, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200362 usberr("ep0in premature status");
363 ep0state = EP0_IDLE;
364 } else {
365 /* irq was IPR clearing */
366 if (udc_write_urb(endpoint) < 0) {
367 usberr("ep0_write_error\n");
368 goto stall;
369 }
370 }
371 break;
372
373 case EP0_XFER_COMPLETE:
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200374 writel(UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200375 ep0state = EP0_IDLE;
376 break;
377
378 default:
379 usbdbg("Default\n");
380 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200381 writel(USIR0_IR0, USIR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200382}
383
384static void udc_handle_ep(struct usb_endpoint_instance *endpoint)
385{
386 int ep_addr = endpoint->endpoint_address;
387 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
388 int ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
389
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200390 u32 flags = readl(UDCCSN(ep_num)) & (UDCCSR_SST | UDCCSR_TRN);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200391 if (flags)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200392 writel(flags, UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200393
394 if (ep_isout)
395 udc_read_urb(endpoint);
396 else
397 udc_write_urb(endpoint);
398
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200399 writel(UDCCSR_PC, UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200400}
401
402static void udc_state_changed(void)
403{
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200404
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200405 writel(readl(UDCCR) | UDCCR_SMAC, UDCCR);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200406
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200407 usbdbg("New UDC settings are: conf %d - inter %d - alter %d",
Mike Dunnecc8edb2013-06-26 12:33:54 -0700408 (readl(UDCCR) & UDCCR_ACN) >> UDCCR_ACN_S,
409 (readl(UDCCR) & UDCCR_AIN) >> UDCCR_AIN_S,
410 (readl(UDCCR) & UDCCR_AAISN) >> UDCCR_AAISN_S);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200411
412 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200413 writel(UDCISR1_IRCC, UDCISR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200414}
415
416void udc_irq(void)
417{
418 int handled;
419 struct usb_endpoint_instance *endpoint;
420 int ep_num, i;
421 u32 udcisr0;
422
423 do {
424 handled = 0;
425 /* Suspend Interrupt Request */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200426 if (readl(USIR1) & UDCCR_SUSIR) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200427 usbdbg("Suspend\n");
428 udc_ack_int_UDCCR(UDCCR_SUSIR);
429 handled = 1;
430 ep0state = EP0_IDLE;
431 }
432
433 /* Resume Interrupt Request */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200434 if (readl(USIR1) & UDCCR_RESIR) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200435 udc_ack_int_UDCCR(UDCCR_RESIR);
436 handled = 1;
437 usbdbg("USB resume\n");
438 }
439
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200440 if (readl(USIR1) & (1<<31)) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200441 handled = 1;
442 udc_state_changed();
443 }
444
445 /* Reset Interrupt Request */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200446 if (readl(USIR1) & UDCCR_RSTIR) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200447 udc_ack_int_UDCCR(UDCCR_RSTIR);
448 handled = 1;
449 usbdbg("Reset\n");
450 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
451 } else {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200452 if (readl(USIR0))
453 usbdbg("UISR0: %x \n", readl(USIR0));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200454
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200455 if (readl(USIR0) & 0x2)
456 writel(0x2, USIR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200457
458 /* Control traffic */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200459 if (readl(USIR0) & USIR0_IR0) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200460 handled = 1;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200461 writel(USIR0_IR0, USIR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200462 udc_handle_ep0(udc_device->bus->endpoint_array);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200463 }
464
465 endpoint = udc_device->bus->endpoint_array;
466 for (i = 0; i < udc_device->bus->max_endpoints; i++) {
467 ep_num = (endpoint[i].endpoint_address) &
468 USB_ENDPOINT_NUMBER_MASK;
469 if (!ep_num)
470 continue;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200471 udcisr0 = readl(UDCISR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200472 if (udcisr0 &
473 UDCISR_INT(ep_num, UDC_INT_PACKETCMP)) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200474 writel(UDCISR_INT(ep_num, UDC_INT_PACKETCMP),
475 UDCISR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200476 udc_handle_ep(&endpoint[i]);
477 }
478 }
479 }
480
481 } while (handled);
482}
483
484/* The UDCCR reg contains mask and interrupt status bits,
485 * so using '|=' isn't safe as it may ack an interrupt.
486 */
487#define UDCCR_OEN (1 << 31) /* On-the-Go Enable */
488#define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_UDE)
489
490static inline void udc_set_mask_UDCCR(int mask)
491{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200492 writel((readl(UDCCR) & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS), UDCCR);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200493}
494
495static inline void udc_clear_mask_UDCCR(int mask)
496{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200497 writel((readl(UDCCR) & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS), UDCCR);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200498}
499
500static void pio_irq_enable(int ep_num)
501{
502 if (ep_num < 16)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200503 writel(readl(UDCICR0) | 3 << (ep_num * 2), UDCICR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200504 else {
505 ep_num -= 16;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200506 writel(readl(UDCICR1) | 3 << (ep_num * 2), UDCICR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200507 }
508}
509
510/*
511 * udc_set_nak
512 *
513 * Allow upper layers to signal lower layers should not accept more RX data
514 */
515void udc_set_nak(int ep_num)
516{
517 /* TODO */
518}
519
520/*
521 * udc_unset_nak
522 *
523 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
524 * Switch off NAKing on this endpoint to accept more data output from host.
525 */
526void udc_unset_nak(int ep_num)
527{
528 /* TODO */
529}
530
531int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
532{
533 return udc_write_urb(endpoint);
534}
535
536/* Associate a physical endpoint with endpoint instance */
537void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
538 struct usb_endpoint_instance *endpoint)
539{
540 int ep_num, ep_addr, ep_isout, ep_type, ep_size;
541 int config, interface, alternate;
542 u32 tmp;
543
544 usbdbg("setting up endpoint id %d", id);
545
546 if (!endpoint) {
547 usberr("endpoint void!");
548 return;
549 }
550
551 ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
552 if (ep_num >= UDC_MAX_ENDPOINTS) {
553 usberr("unable to setup ep %d!", ep_num);
554 return;
555 }
556
557 pio_irq_enable(ep_num);
558 if (ep_num == 0) {
559 /* Done for ep0 */
560 return;
561 }
562
563 config = 1;
564 interface = 0;
565 alternate = 0;
566
567 usbdbg("config %d - interface %d - alternate %d",
568 config, interface, alternate);
569
570 ep_addr = endpoint->endpoint_address;
571 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
572 ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
573 ep_type = ep_isout ? endpoint->rcv_attributes : endpoint->tx_attributes;
574 ep_size = ep_isout ? endpoint->rcv_packetSize : endpoint->tx_packetSize;
575
576 usbdbg("addr %x, num %d, dir %s, type %s, packet size %d",
577 ep_addr, ep_num,
578 ep_isout ? "out" : "in",
579 ep_type == USB_ENDPOINT_XFER_ISOC ? "isoc" :
580 ep_type == USB_ENDPOINT_XFER_BULK ? "bulk" :
581 ep_type == USB_ENDPOINT_XFER_INT ? "int" : "???",
582 ep_size
583 );
584
585 /* Configure UDCCRx */
586 tmp = 0;
587 tmp |= (config << UDCCONR_CN_S) & UDCCONR_CN;
588 tmp |= (interface << UDCCONR_IN_S) & UDCCONR_IN;
589 tmp |= (alternate << UDCCONR_AISN_S) & UDCCONR_AISN;
590 tmp |= (ep_num << UDCCONR_EN_S) & UDCCONR_EN;
591 tmp |= (ep_type << UDCCONR_ET_S) & UDCCONR_ET;
592 tmp |= ep_isout ? 0 : UDCCONR_ED;
593 tmp |= (ep_size << UDCCONR_MPS_S) & UDCCONR_MPS;
594 tmp |= UDCCONR_EE;
595
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200596 writel(tmp, UDCCN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200597
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200598 usbdbg("UDCCR%c = %x", 'A' + ep_num-1, readl(UDCCN(ep_num)));
599 usbdbg("UDCCSR%c = %x", 'A' + ep_num-1, readl(UDCCSN(ep_num)));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200600}
601
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200602/* Connect the USB device to the bus */
603void udc_connect(void)
604{
605 usbdbg("UDC connect");
606
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200607#ifdef CONFIG_USB_DEV_PULLUP_GPIO
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200608 /* Turn on the USB connection by enabling the pullup resistor */
Mike Dunne0e89e22013-04-12 11:59:15 -0700609 writel(readl(GPDR(CONFIG_USB_DEV_PULLUP_GPIO))
610 | GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
611 GPDR(CONFIG_USB_DEV_PULLUP_GPIO));
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200612 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
613#else
614 /* Host port 2 transceiver D+ pull up enable */
615 writel(readl(UP2OCR) | UP2OCR_DPPUE, UP2OCR);
616#endif
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200617}
618
619/* Disconnect the USB device to the bus */
620void udc_disconnect(void)
621{
622 usbdbg("UDC disconnect");
623
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200624#ifdef CONFIG_USB_DEV_PULLUP_GPIO
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200625 /* Turn off the USB connection by disabling the pullup resistor */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200626 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
627#else
628 /* Host port 2 transceiver D+ pull up disable */
629 writel(readl(UP2OCR) & ~UP2OCR_DPPUE, UP2OCR);
630#endif
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200631}
632
633/* Switch on the UDC */
634void udc_enable(struct usb_device_instance *device)
635{
636
637 ep0state = EP0_IDLE;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200638
639 /* enable endpoint 0, A, B's Packet Complete Interrupt. */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200640 writel(0xffffffff, UDCICR0);
641 writel(0xa8000000, UDCICR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200642
643 /* clear the interrupt status/control registers */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200644 writel(0xffffffff, UDCISR0);
645 writel(0xffffffff, UDCISR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200646
647 /* set UDC-enable */
648 udc_set_mask_UDCCR(UDCCR_UDE);
649
650 udc_device = device;
651 if (!ep0_urb)
652 ep0_urb = usbd_alloc_urb(udc_device,
653 udc_device->bus->endpoint_array);
654 else
655 usbinfo("ep0_urb %p already allocated", ep0_urb);
656
657 usbdbg("UDC Enabled\n");
658}
659
660/* Need to check this again */
661void udc_disable(void)
662{
663 usbdbg("disable UDC");
664
665 udc_clear_mask_UDCCR(UDCCR_UDE);
666
667 /* Disable clock for USB device */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200668 writel(readl(CKEN) & ~CKEN11_USB, CKEN);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200669
670 /* Free ep0 URB */
671 if (ep0_urb) {
672 usbd_dealloc_urb(ep0_urb);
673 ep0_urb = NULL;
674 }
675
676 /* Reset device pointer */
677 udc_device = NULL;
678}
679
680/* Allow udc code to do any additional startup */
681void udc_startup_events(struct usb_device_instance *device)
682{
683 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT */
684 usbd_device_event_irq(device, DEVICE_INIT, 0);
685
686 /* The DEVICE_CREATE event puts the USB device in the state
687 * STATE_ATTACHED */
688 usbd_device_event_irq(device, DEVICE_CREATE, 0);
689
690 /* Some USB controller driver implementations signal
691 * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
692 * DEVICE_HUB_CONFIGURED causes a transition to the state
693 * STATE_POWERED, and DEVICE_RESET causes a transition to
694 * the state STATE_DEFAULT.
695 */
696 udc_enable(device);
697}
698
699/* Initialize h/w stuff */
700int udc_init(void)
701{
702 udc_device = NULL;
703 usbdbg("PXA27x usbd start");
704
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200705 /* Enable clock for USB device */
706 writel(readl(CKEN) | CKEN11_USB, CKEN);
707
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200708 /* Disable the UDC */
709 udc_clear_mask_UDCCR(UDCCR_UDE);
710
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200711 /* Disable IRQs: we don't use them */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200712 writel(0, UDCICR0);
713 writel(0, UDCICR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200714
715 return 0;
716}