blob: 733558def7d0659e2047453b74372588aba73e9f [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
68 n = MIN(urb->actual_length - endpoint->sent, endpoint->tx_packetSize);
69 if (n <= 0)
70 return -1;
71
72 usbdbg("write urb on ep %d", ep_num);
73#if defined(USBDDBG) && defined(USBDPARANOIA)
74 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
75 urb->buffer, urb->buffer_length, urb->actual_length);
76 usbdbg("endpoint: sent %d, tx_packetSize %d, last %d",
77 endpoint->sent, endpoint->tx_packetSize, endpoint->last);
78#endif
79
80 is_short = n != endpoint->tx_packetSize;
81 w = n / 4;
82 b = n % 4;
83 usbdbg("n %d%s w %d b %d", n, is_short ? "-s" : "", w, b);
84 udc_dump_buffer("urb write", data8 + endpoint->sent, n);
85
86 /* Prepare for data send */
87 if (ep_num)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020088 writel(UDCCSR_PC ,UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020089
90 for (i = 0; i < w; i++)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020091 writel(data32[endpoint->sent / 4 + i], UDCDN(ep_num));
92
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020093 for (i = 0; i < b; i++)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020094 writeb(data8[endpoint->sent + w * 4 + i], UDCDN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020095
96 /* Set "Packet Complete" if less data then tx_packetSize */
97 if (is_short)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +020098 writel(ep_num ? UDCCSR_SP : UDCCSR0_IPR, UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +020099
100 /* Wait for data sent */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200101 if (ep_num) {
102 while (!(readl(UDCCSN(ep_num)) & UDCCSR_PC)) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200103 if (timeout-- == 0)
104 return -1;
105 else
106 udelay(1);
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200107 }
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200108 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200109
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200110 endpoint->last = n;
111
112 if (ep_num) {
113 usbd_tx_complete(endpoint);
114 } else {
115 endpoint->sent += n;
116 endpoint->last -= n;
117 }
118
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200119 if (endpoint->sent >= urb->actual_length) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200120 urb->actual_length = 0;
121 endpoint->sent = 0;
122 endpoint->last = 0;
123 }
124
125 if ((endpoint->sent >= urb->actual_length) && (!ep_num)) {
126 usbdbg("ep0 IN stage done");
127 if (is_short)
128 ep0state = EP0_IDLE;
129 else
130 ep0state = EP0_XFER_COMPLETE;
131 }
132
133 return 0;
134}
135
136static int udc_read_urb(struct usb_endpoint_instance *endpoint)
137{
138 struct urb *urb = endpoint->rcv_urb;
139 int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200140 u32 *data32 = (u32 *) urb->buffer;
Mike Dunnecc8edb2013-06-26 12:33:54 -0700141 unsigned int i, n;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200142
143 usbdbg("read urb on ep %d", ep_num);
144#if defined(USBDDBG) && defined(USBDPARANOIA)
145 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
146 urb->buffer, urb->buffer_length, urb->actual_length);
147 usbdbg("endpoint: rcv_packetSize %d",
148 endpoint->rcv_packetSize);
149#endif
150
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200151 if (readl(UDCCSN(ep_num)) & UDCCSR_BNE)
152 n = readl(UDCBCN(ep_num)) & 0x3ff;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200153 else /* zlp */
154 n = 0;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200155
Mike Dunnecc8edb2013-06-26 12:33:54 -0700156 usbdbg("n %d%s", n, n != endpoint->rcv_packetSize ? "-s" : "");
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200157 for (i = 0; i < n; i += 4)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200158 data32[urb->actual_length / 4 + i / 4] = readl(UDCDN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200159
160 udc_dump_buffer("urb read", (u8 *) data32, urb->actual_length + n);
161 usbd_rcv_complete(endpoint, n, 0);
162
163 return 0;
164}
165
166static int udc_read_urb_ep0(void)
167{
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200168 u32 *data32 = (u32 *) ep0_urb->buffer;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200169 u8 *data8 = (u8 *) ep0_urb->buffer;
170 unsigned int i, n, w, b;
171
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200172 usbdbg("read urb on ep 0");
173#if defined(USBDDBG) && defined(USBDPARANOIA)
174 usbdbg("urb: buf %p, buf_len %d, actual_len %d",
175 ep0_urb->buffer, ep0_urb->buffer_length, ep0_urb->actual_length);
176#endif
177
178 n = readl(UDCBCR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200179 w = n / 4;
180 b = n % 4;
181
182 for (i = 0; i < w; i++) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200183 data32[ep0_urb->actual_length / 4 + i] = readl(UDCDN(0));
Wolfgang Denk10879aa2011-12-19 11:52:36 +0100184 /* ep0_urb->actual_length += 4; */
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200185 }
186
187 for (i = 0; i < b; i++) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200188 data8[ep0_urb->actual_length + w * 4 + i] = readb(UDCDN(0));
Wolfgang Denk10879aa2011-12-19 11:52:36 +0100189 /* ep0_urb->actual_length++; */
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200190 }
191
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200192 ep0_urb->actual_length += n;
193
194 udc_dump_buffer("urb read", (u8 *) data32, ep0_urb->actual_length);
195
196 writel(UDCCSR0_OPC | UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200197 if (ep0_urb->actual_length == ep0_urb->device_request.wLength)
198 return 1;
199
200 return 0;
201}
202
203static void udc_handle_ep0(struct usb_endpoint_instance *endpoint)
204{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200205 u32 udccsr0 = readl(UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200206 u32 *data = (u32 *) &ep0_urb->device_request;
207 int i;
208
209 usbdbg("udccsr0 %x", udccsr0);
210
211 /* Clear stall status */
212 if (udccsr0 & UDCCSR0_SST) {
213 usberr("clear stall status");
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200214 writel(UDCCSR0_SST, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200215 ep0state = EP0_IDLE;
216 }
217
218 /* previous request unfinished? non-error iff back-to-back ... */
219 if ((udccsr0 & UDCCSR0_SA) != 0 && ep0state != EP0_IDLE)
220 ep0state = EP0_IDLE;
221
222 switch (ep0state) {
223
224 case EP0_IDLE:
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200225 udccsr0 = readl(UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200226 /* Start control request? */
227 if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE))
228 == (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)) {
229
230 /* Read SETUP packet.
231 * SETUP packet size is 8 bytes (aka 2 words)
232 */
233 usbdbg("try reading SETUP packet");
234 for (i = 0; i < 2; i++) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200235 if ((readl(UDCCSR0) & UDCCSR0_RNE) == 0) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200236 usberr("setup packet too short:%d", i);
237 goto stall;
238 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200239 data[i] = readl(UDCDR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200240 }
241
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200242 writel(readl(UDCCSR0) | UDCCSR0_OPC | UDCCSR0_SA, UDCCSR0);
243 if ((readl(UDCCSR0) & UDCCSR0_RNE) != 0) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200244 usberr("setup packet too long");
245 goto stall;
246 }
247
248 udc_dump_buffer("ep0 setup read", (u8 *) data, 8);
249
250 if (ep0_urb->device_request.wLength == 0) {
251 usbdbg("Zero Data control Packet\n");
252 if (ep0_recv_setup(ep0_urb)) {
253 usberr("Invalid Setup Packet\n");
254 udc_dump_buffer("ep0 setup read",
255 (u8 *)data, 8);
256 goto stall;
257 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200258 writel(UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200259 ep0state = EP0_IDLE;
260 } else {
261 /* Check direction */
262 if ((ep0_urb->device_request.bmRequestType &
263 USB_REQ_DIRECTION_MASK)
264 == USB_REQ_HOST2DEVICE) {
265 ep0state = EP0_OUT_DATA;
266 ep0_urb->buffer =
267 (u8 *)ep0_urb->buffer_data;
268 ep0_urb->buffer_length =
269 sizeof(ep0_urb->buffer_data);
270 ep0_urb->actual_length = 0;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200271 writel(UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200272 } else {
273 /* The ep0_recv_setup function has
274 * already placed our response packet
275 * data in ep0_urb->buffer and the
276 * packet length in
277 * ep0_urb->actual_length.
278 */
279 if (ep0_recv_setup(ep0_urb)) {
280stall:
281 usberr("Invalid setup packet");
282 udc_dump_buffer("ep0 setup read"
283 , (u8 *) data, 8);
284 ep0state = EP0_IDLE;
285
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200286 writel(UDCCSR0_SA |
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200287 UDCCSR0_OPC | UDCCSR0_FST |
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200288 UDCCS0_FTF, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200289
290 return;
291 }
292
293 endpoint->tx_urb = ep0_urb;
294 endpoint->sent = 0;
295 usbdbg("EP0_IN_DATA");
296 ep0state = EP0_IN_DATA;
297 if (udc_write_urb(endpoint) < 0)
298 goto stall;
299
300 }
301 }
302 return;
303 } else if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA))
304 == (UDCCSR0_OPC|UDCCSR0_SA)) {
305 usberr("Setup Active but no data. Stalling ....\n");
306 goto stall;
307 } else {
308 usbdbg("random early IRQs");
309 /* Some random early IRQs:
310 * - we acked FST
311 * - IPR cleared
312 * - OPC got set, without SA (likely status stage)
313 */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200314 writel(udccsr0 & (UDCCSR0_SA | UDCCSR0_OPC), UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200315 }
316 break;
317
318 case EP0_OUT_DATA:
319
320 if ((udccsr0 & UDCCSR0_OPC) && !(udccsr0 & UDCCSR0_SA)) {
321 if (udc_read_urb_ep0()) {
322read_complete:
323 ep0state = EP0_IDLE;
324 if (ep0_recv_setup(ep0_urb)) {
325 /* Not a setup packet, stall next
326 * EP0 transaction
327 */
328 udc_dump_buffer("ep0 setup read",
329 (u8 *) data, 8);
330 usberr("can't parse setup packet\n");
331 goto stall;
332 }
333 }
334 } else if (!(udccsr0 & UDCCSR0_OPC) &&
335 !(udccsr0 & UDCCSR0_IPR)) {
336 if (ep0_urb->device_request.wLength ==
337 ep0_urb->actual_length)
338 goto read_complete;
339
340 usberr("Premature Status\n");
341 ep0state = EP0_IDLE;
342 }
343 break;
344
345 case EP0_IN_DATA:
346 /* GET_DESCRIPTOR etc */
347 if (udccsr0 & UDCCSR0_OPC) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200348 writel(UDCCSR0_OPC | UDCCSR0_FTF, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200349 usberr("ep0in premature status");
350 ep0state = EP0_IDLE;
351 } else {
352 /* irq was IPR clearing */
353 if (udc_write_urb(endpoint) < 0) {
354 usberr("ep0_write_error\n");
355 goto stall;
356 }
357 }
358 break;
359
360 case EP0_XFER_COMPLETE:
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200361 writel(UDCCSR0_IPR, UDCCSR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200362 ep0state = EP0_IDLE;
363 break;
364
365 default:
366 usbdbg("Default\n");
367 }
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200368 writel(USIR0_IR0, USIR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200369}
370
371static void udc_handle_ep(struct usb_endpoint_instance *endpoint)
372{
373 int ep_addr = endpoint->endpoint_address;
374 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
375 int ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
376
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200377 u32 flags = readl(UDCCSN(ep_num)) & (UDCCSR_SST | UDCCSR_TRN);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200378 if (flags)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200379 writel(flags, UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200380
381 if (ep_isout)
382 udc_read_urb(endpoint);
383 else
384 udc_write_urb(endpoint);
385
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200386 writel(UDCCSR_PC, UDCCSN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200387}
388
389static void udc_state_changed(void)
390{
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200391
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200392 writel(readl(UDCCR) | UDCCR_SMAC, UDCCR);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200393
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200394 usbdbg("New UDC settings are: conf %d - inter %d - alter %d",
Mike Dunnecc8edb2013-06-26 12:33:54 -0700395 (readl(UDCCR) & UDCCR_ACN) >> UDCCR_ACN_S,
396 (readl(UDCCR) & UDCCR_AIN) >> UDCCR_AIN_S,
397 (readl(UDCCR) & UDCCR_AAISN) >> UDCCR_AAISN_S);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200398
399 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200400 writel(UDCISR1_IRCC, UDCISR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200401}
402
403void udc_irq(void)
404{
405 int handled;
406 struct usb_endpoint_instance *endpoint;
407 int ep_num, i;
408 u32 udcisr0;
409
410 do {
411 handled = 0;
412 /* Suspend Interrupt Request */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200413 if (readl(USIR1) & UDCCR_SUSIR) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200414 usbdbg("Suspend\n");
415 udc_ack_int_UDCCR(UDCCR_SUSIR);
416 handled = 1;
417 ep0state = EP0_IDLE;
418 }
419
420 /* Resume Interrupt Request */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200421 if (readl(USIR1) & UDCCR_RESIR) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200422 udc_ack_int_UDCCR(UDCCR_RESIR);
423 handled = 1;
424 usbdbg("USB resume\n");
425 }
426
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200427 if (readl(USIR1) & (1<<31)) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200428 handled = 1;
429 udc_state_changed();
430 }
431
432 /* Reset Interrupt Request */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200433 if (readl(USIR1) & UDCCR_RSTIR) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200434 udc_ack_int_UDCCR(UDCCR_RSTIR);
435 handled = 1;
436 usbdbg("Reset\n");
437 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
438 } else {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200439 if (readl(USIR0))
440 usbdbg("UISR0: %x \n", readl(USIR0));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200441
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200442 if (readl(USIR0) & 0x2)
443 writel(0x2, USIR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200444
445 /* Control traffic */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200446 if (readl(USIR0) & USIR0_IR0) {
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200447 handled = 1;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200448 writel(USIR0_IR0, USIR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200449 udc_handle_ep0(udc_device->bus->endpoint_array);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200450 }
451
452 endpoint = udc_device->bus->endpoint_array;
453 for (i = 0; i < udc_device->bus->max_endpoints; i++) {
454 ep_num = (endpoint[i].endpoint_address) &
455 USB_ENDPOINT_NUMBER_MASK;
456 if (!ep_num)
457 continue;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200458 udcisr0 = readl(UDCISR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200459 if (udcisr0 &
460 UDCISR_INT(ep_num, UDC_INT_PACKETCMP)) {
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200461 writel(UDCISR_INT(ep_num, UDC_INT_PACKETCMP),
462 UDCISR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200463 udc_handle_ep(&endpoint[i]);
464 }
465 }
466 }
467
468 } while (handled);
469}
470
471/* The UDCCR reg contains mask and interrupt status bits,
472 * so using '|=' isn't safe as it may ack an interrupt.
473 */
474#define UDCCR_OEN (1 << 31) /* On-the-Go Enable */
475#define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_UDE)
476
477static inline void udc_set_mask_UDCCR(int mask)
478{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200479 writel((readl(UDCCR) & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS), UDCCR);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200480}
481
482static inline void udc_clear_mask_UDCCR(int mask)
483{
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200484 writel((readl(UDCCR) & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS), UDCCR);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200485}
486
487static void pio_irq_enable(int ep_num)
488{
489 if (ep_num < 16)
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200490 writel(readl(UDCICR0) | 3 << (ep_num * 2), UDCICR0);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200491 else {
492 ep_num -= 16;
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200493 writel(readl(UDCICR1) | 3 << (ep_num * 2), UDCICR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200494 }
495}
496
497/*
498 * udc_set_nak
499 *
500 * Allow upper layers to signal lower layers should not accept more RX data
501 */
502void udc_set_nak(int ep_num)
503{
504 /* TODO */
505}
506
507/*
508 * udc_unset_nak
509 *
510 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
511 * Switch off NAKing on this endpoint to accept more data output from host.
512 */
513void udc_unset_nak(int ep_num)
514{
515 /* TODO */
516}
517
518int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
519{
520 return udc_write_urb(endpoint);
521}
522
523/* Associate a physical endpoint with endpoint instance */
524void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
525 struct usb_endpoint_instance *endpoint)
526{
527 int ep_num, ep_addr, ep_isout, ep_type, ep_size;
528 int config, interface, alternate;
529 u32 tmp;
530
531 usbdbg("setting up endpoint id %d", id);
532
533 if (!endpoint) {
534 usberr("endpoint void!");
535 return;
536 }
537
538 ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
539 if (ep_num >= UDC_MAX_ENDPOINTS) {
540 usberr("unable to setup ep %d!", ep_num);
541 return;
542 }
543
544 pio_irq_enable(ep_num);
545 if (ep_num == 0) {
546 /* Done for ep0 */
547 return;
548 }
549
550 config = 1;
551 interface = 0;
552 alternate = 0;
553
554 usbdbg("config %d - interface %d - alternate %d",
555 config, interface, alternate);
556
557 ep_addr = endpoint->endpoint_address;
558 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
559 ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
560 ep_type = ep_isout ? endpoint->rcv_attributes : endpoint->tx_attributes;
561 ep_size = ep_isout ? endpoint->rcv_packetSize : endpoint->tx_packetSize;
562
563 usbdbg("addr %x, num %d, dir %s, type %s, packet size %d",
564 ep_addr, ep_num,
565 ep_isout ? "out" : "in",
566 ep_type == USB_ENDPOINT_XFER_ISOC ? "isoc" :
567 ep_type == USB_ENDPOINT_XFER_BULK ? "bulk" :
568 ep_type == USB_ENDPOINT_XFER_INT ? "int" : "???",
569 ep_size
570 );
571
572 /* Configure UDCCRx */
573 tmp = 0;
574 tmp |= (config << UDCCONR_CN_S) & UDCCONR_CN;
575 tmp |= (interface << UDCCONR_IN_S) & UDCCONR_IN;
576 tmp |= (alternate << UDCCONR_AISN_S) & UDCCONR_AISN;
577 tmp |= (ep_num << UDCCONR_EN_S) & UDCCONR_EN;
578 tmp |= (ep_type << UDCCONR_ET_S) & UDCCONR_ET;
579 tmp |= ep_isout ? 0 : UDCCONR_ED;
580 tmp |= (ep_size << UDCCONR_MPS_S) & UDCCONR_MPS;
581 tmp |= UDCCONR_EE;
582
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200583 writel(tmp, UDCCN(ep_num));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200584
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200585 usbdbg("UDCCR%c = %x", 'A' + ep_num-1, readl(UDCCN(ep_num)));
586 usbdbg("UDCCSR%c = %x", 'A' + ep_num-1, readl(UDCCSN(ep_num)));
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200587}
588
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200589/* Connect the USB device to the bus */
590void udc_connect(void)
591{
592 usbdbg("UDC connect");
593
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200594#ifdef CONFIG_USB_DEV_PULLUP_GPIO
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200595 /* Turn on the USB connection by enabling the pullup resistor */
Mike Dunne0e89e22013-04-12 11:59:15 -0700596 writel(readl(GPDR(CONFIG_USB_DEV_PULLUP_GPIO))
597 | GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
598 GPDR(CONFIG_USB_DEV_PULLUP_GPIO));
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200599 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
600#else
601 /* Host port 2 transceiver D+ pull up enable */
602 writel(readl(UP2OCR) | UP2OCR_DPPUE, UP2OCR);
603#endif
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200604}
605
606/* Disconnect the USB device to the bus */
607void udc_disconnect(void)
608{
609 usbdbg("UDC disconnect");
610
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200611#ifdef CONFIG_USB_DEV_PULLUP_GPIO
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200612 /* Turn off the USB connection by disabling the pullup resistor */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200613 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO), GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
614#else
615 /* Host port 2 transceiver D+ pull up disable */
616 writel(readl(UP2OCR) & ~UP2OCR_DPPUE, UP2OCR);
617#endif
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200618}
619
620/* Switch on the UDC */
621void udc_enable(struct usb_device_instance *device)
622{
623
624 ep0state = EP0_IDLE;
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200625
626 /* enable endpoint 0, A, B's Packet Complete Interrupt. */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200627 writel(0xffffffff, UDCICR0);
628 writel(0xa8000000, UDCICR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200629
630 /* clear the interrupt status/control registers */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200631 writel(0xffffffff, UDCISR0);
632 writel(0xffffffff, UDCISR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200633
634 /* set UDC-enable */
635 udc_set_mask_UDCCR(UDCCR_UDE);
636
637 udc_device = device;
638 if (!ep0_urb)
639 ep0_urb = usbd_alloc_urb(udc_device,
640 udc_device->bus->endpoint_array);
641 else
642 usbinfo("ep0_urb %p already allocated", ep0_urb);
643
644 usbdbg("UDC Enabled\n");
645}
646
647/* Need to check this again */
648void udc_disable(void)
649{
650 usbdbg("disable UDC");
651
652 udc_clear_mask_UDCCR(UDCCR_UDE);
653
654 /* Disable clock for USB device */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200655 writel(readl(CKEN) & ~CKEN11_USB, CKEN);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200656
657 /* Free ep0 URB */
658 if (ep0_urb) {
659 usbd_dealloc_urb(ep0_urb);
660 ep0_urb = NULL;
661 }
662
663 /* Reset device pointer */
664 udc_device = NULL;
665}
666
667/* Allow udc code to do any additional startup */
668void udc_startup_events(struct usb_device_instance *device)
669{
670 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT */
671 usbd_device_event_irq(device, DEVICE_INIT, 0);
672
673 /* The DEVICE_CREATE event puts the USB device in the state
674 * STATE_ATTACHED */
675 usbd_device_event_irq(device, DEVICE_CREATE, 0);
676
677 /* Some USB controller driver implementations signal
678 * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
679 * DEVICE_HUB_CONFIGURED causes a transition to the state
680 * STATE_POWERED, and DEVICE_RESET causes a transition to
681 * the state STATE_DEFAULT.
682 */
683 udc_enable(device);
684}
685
686/* Initialize h/w stuff */
687int udc_init(void)
688{
689 udc_device = NULL;
690 usbdbg("PXA27x usbd start");
691
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200692 /* Enable clock for USB device */
693 writel(readl(CKEN) | CKEN11_USB, CKEN);
694
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200695 /* Disable the UDC */
696 udc_clear_mask_UDCCR(UDCCR_UDE);
697
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200698 /* Disable IRQs: we don't use them */
Stefan Herbrechtsmeierbdbcdc82011-10-17 17:22:48 +0200699 writel(0, UDCICR0);
700 writel(0, UDCICR1);
Remy Bohmer3ccbfb22009-04-05 11:43:28 +0200701
702 return 0;
703}