blob: 78f7aba2dcf489e1f28b688dda57b0d96842d751 [file] [log] [blame]
Patrick Georgid21f68b2008-09-02 16:06:22 +00001/*
2 * This file is part of the libpayload project.
3 *
Stefan Reinauerb56f2d02010-03-25 22:17:36 +00004 * Copyright (C) 2008-2010 coresystems GmbH
Patrick Georgid21f68b2008-09-02 16:06:22 +00005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
Stefan Reinauerb56f2d02010-03-25 22:17:36 +000030//#define USB_DEBUG
31
Patrick Georgid78691d2010-06-07 13:58:17 +000032#include <arch/virtual.h>
Jordan Crouse29061a52008-09-11 17:29:00 +000033#include <usb/usb.h>
Patrick Georgid21f68b2008-09-02 16:06:22 +000034#include "uhci.h"
Patrick Georgid78691d2010-06-07 13:58:17 +000035#include "uhci_private.h"
Patrick Georgid21f68b2008-09-02 16:06:22 +000036
37static void uhci_start (hci_t *controller);
38static void uhci_stop (hci_t *controller);
39static void uhci_reset (hci_t *controller);
40static void uhci_shutdown (hci_t *controller);
Patrick Georgid21f68b2008-09-02 16:06:22 +000041static int uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize);
Patrick Georgid78691d2010-06-07 13:58:17 +000042static int uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq,
Patrick Georgid21f68b2008-09-02 16:06:22 +000043 int dalen, u8 *data);
Patrick Georgi4727c072008-10-16 19:20:51 +000044static void* uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming);
45static void uhci_destroy_intr_queue (endpoint_t *ep, void *queue);
46static u8* uhci_poll_intr_queue (void *queue);
Patrick Georgid21f68b2008-09-02 16:06:22 +000047
48#if 0
49/* dump uhci */
50static void
51uhci_dump (hci_t *controller)
52{
Mathias Krausec4716b42011-06-08 15:36:55 +020053 debug ("dump:\nUSBCMD: %x\n", uhci_reg_read16 (controller, USBCMD));
54 debug ("USBSTS: %x\n", uhci_reg_read16 (controller, USBSTS));
55 debug ("USBINTR: %x\n", uhci_reg_read16 (controller, USBINTR));
56 debug ("FRNUM: %x\n", uhci_reg_read16 (controller, FRNUM));
57 debug ("FLBASEADD: %x\n", uhci_reg_read32 (controller, FLBASEADD));
58 debug ("SOFMOD: %x\n", uhci_reg_read8 (controller, SOFMOD));
59 debug ("PORTSC1: %x\n", uhci_reg_read16 (controller, PORTSC1));
60 debug ("PORTSC2: %x\n", uhci_reg_read16 (controller, PORTSC2));
Patrick Georgid21f68b2008-09-02 16:06:22 +000061}
62#endif
63
64static void
65td_dump (td_t *td)
66{
Stefan Reinauerd233f362009-04-30 16:46:12 +000067 char td_value[3];
Patrick Georgi7f965832011-04-21 18:57:16 +020068 const char *td_type;
Patrick Georgib0b4a522011-11-24 11:55:46 +010069 switch (td->token & TD_PID_MASK) {
Patrick Georgid78691d2010-06-07 13:58:17 +000070 case UHCI_SETUP:
Stefan Reinauerd233f362009-04-30 16:46:12 +000071 td_type="SETUP";
72 break;
Patrick Georgid78691d2010-06-07 13:58:17 +000073 case UHCI_IN:
Stefan Reinauerd233f362009-04-30 16:46:12 +000074 td_type="IN";
75 break;
Patrick Georgid78691d2010-06-07 13:58:17 +000076 case UHCI_OUT:
Stefan Reinauerd233f362009-04-30 16:46:12 +000077 td_type="OUT";
78 break;
79 default:
Patrick Georgib0b4a522011-11-24 11:55:46 +010080 sprintf(td_value, "%x", td->token & TD_PID_MASK);
Stefan Reinauerd233f362009-04-30 16:46:12 +000081 td_type=td_value;
82 }
Mathias Krausec4716b42011-06-08 15:36:55 +020083 debug ("%s packet (at %lx) to %x.%x failed\n", td_type,
Patrick Georgib0b4a522011-11-24 11:55:46 +010084 virt_to_phys (td), (td->token & TD_DEVADDR_MASK) >> TD_DEVADDR_SHIFT,
85 (td->token & TD_EP_MASK) >> TD_EP_SHIFT);
86 debug ("td (counter at %x) returns: ", td->ctrlsts >> TD_COUNTER_SHIFT);
87 debug (" bitstuff err: %x, ", !!(td->ctrlsts & TD_STATUS_BITSTUFF_ERR));
88 debug (" CRC err: %x, ", !!(td->ctrlsts & TD_STATUS_CRC_ERR));
89 debug (" NAK rcvd: %x, ", !!(td->ctrlsts & TD_STATUS_NAK_RCVD));
90 debug (" Babble: %x, ", !!(td->ctrlsts & TD_STATUS_BABBLE));
91 debug (" Data Buffer err: %x, ", !!(td->ctrlsts & TD_STATUS_DATABUF_ERR));
92 debug (" Stalled: %x, ", !!(td->ctrlsts & TD_STATUS_STALLED));
93 debug (" Active: %x\n", !!(td->ctrlsts & TD_STATUS_ACTIVE));
94 if (td->ctrlsts & TD_STATUS_BABBLE)
Mathias Krausec4716b42011-06-08 15:36:55 +020095 debug (" Babble because of %s\n",
Patrick Georgib0b4a522011-11-24 11:55:46 +010096 (td->ctrlsts & TD_STATUS_BITSTUFF_ERR) ? "host" : "device");
97 if (td->ctrlsts & TD_STATUS_ACTIVE)
Mathias Krausec4716b42011-06-08 15:36:55 +020098 debug (" still active - timeout?\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +000099}
100
101static void
102uhci_reset (hci_t *controller)
103{
104 /* reset */
Nico Huberbb1c42b2012-05-21 14:23:03 +0200105 uhci_reg_write16 (controller, USBCMD, 4); /* Global Reset */
106 mdelay (50); /* uhci spec 2.1.1: at least 10ms */
Patrick Georgid21f68b2008-09-02 16:06:22 +0000107 uhci_reg_write16 (controller, USBCMD, 0);
108 mdelay (10);
Nico Huberbb1c42b2012-05-21 14:23:03 +0200109 uhci_reg_write16 (controller, USBCMD, 2); /* Host Controller Reset */
110 /* wait for controller to finish reset */
111 /* TOTEST: how long to wait? 100ms for now */
112 int timeout = 200; /* time out after 200 * 500us == 100ms */
113 while (((uhci_reg_read16 (controller, USBCMD) & 2) != 0) && timeout--)
114 udelay (500);
115 if (timeout < 0)
116 debug ("Warning: uhci: host controller reset timed out.\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000117
118 uhci_reg_write32 (controller, FLBASEADD,
119 (u32) virt_to_phys (UHCI_INST (controller)->
120 framelistptr));
Mathias Krausec4716b42011-06-08 15:36:55 +0200121 //debug ("framelist at %p\n",UHCI_INST(controller)->framelistptr);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000122
123 /* disable irqs */
124 uhci_reg_write16 (controller, USBINTR, 0);
125
126 /* reset framelist index */
127 uhci_reg_write16 (controller, FRNUM, 0);
128
Patrick Georgif42fdab2011-11-18 14:44:16 +0100129 uhci_reg_write16(controller, USBCMD,
130 uhci_reg_read16(controller, USBCMD) | 0xc0); // max packets, configure flag
Patrick Georgid21f68b2008-09-02 16:06:22 +0000131
132 uhci_start (controller);
133}
134
135hci_t *
136uhci_init (pcidev_t addr)
137{
138 int i;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000139 u16 reg16;
140
Patrick Georgid21f68b2008-09-02 16:06:22 +0000141 hci_t *controller = new_controller ();
142
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000143 if (!controller)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100144 fatal("Could not create USB controller instance.\n");
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000145
Patrick Georgid21f68b2008-09-02 16:06:22 +0000146 controller->instance = malloc (sizeof (uhci_t));
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000147 if(!controller->instance)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100148 fatal("Not enough memory creating USB controller instance.\n");
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000149
Patrick Georgid21f68b2008-09-02 16:06:22 +0000150 controller->start = uhci_start;
151 controller->stop = uhci_stop;
152 controller->reset = uhci_reset;
153 controller->shutdown = uhci_shutdown;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000154 controller->bulk = uhci_bulk;
155 controller->control = uhci_control;
Patrick Georgi4727c072008-10-16 19:20:51 +0000156 controller->create_intr_queue = uhci_create_intr_queue;
157 controller->destroy_intr_queue = uhci_destroy_intr_queue;
158 controller->poll_intr_queue = uhci_poll_intr_queue;
Stefan Reinauer219cece2009-07-18 15:17:40 +0000159 for (i = 0; i < 128; i++) {
Patrick Georgi4727c072008-10-16 19:20:51 +0000160 controller->devices[i] = 0;
161 }
162 init_device_entry (controller, 0);
163 UHCI_INST (controller)->roothub = controller->devices[0];
Patrick Georgid21f68b2008-09-02 16:06:22 +0000164
165 controller->bus_address = addr;
166 controller->reg_base = pci_read_config32 (controller->bus_address, 0x20) & ~1; /* ~1 clears the register type indicator that is set to 1 for IO space */
167
168 /* kill legacy support handler */
169 uhci_stop (controller);
170 mdelay (1);
171 uhci_reg_write16 (controller, USBSTS, 0x3f);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000172 reg16 = pci_read_config16(controller->bus_address, 0xc0);
173 reg16 &= 0xdf80;
174 pci_write_config16 (controller->bus_address, 0xc0, reg16);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000175
176 UHCI_INST (controller)->framelistptr = memalign (0x1000, 1024 * sizeof (flistp_t *)); /* 4kb aligned to 4kb */
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000177 if (! UHCI_INST (controller)->framelistptr)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100178 fatal("Not enough memory for USB frame list pointer.\n");
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000179
Patrick Georgid21f68b2008-09-02 16:06:22 +0000180 memset (UHCI_INST (controller)->framelistptr, 0,
181 1024 * sizeof (flistp_t));
182
Patrick Georgi4727c072008-10-16 19:20:51 +0000183 /* According to the *BSD UHCI code, this one is needed on some
184 PIIX chips, because otherwise they misbehave. It must be
185 added to the last chain.
186
187 FIXME: this leaks, if the driver should ever be reinited
188 for some reason. Not a problem now.
189 */
190 td_t *antiberserk = memalign(16, sizeof(td_t));
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000191 if (!antiberserk)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100192 fatal("Not enough memory for chipset workaround.\n");
Patrick Georgi4727c072008-10-16 19:20:51 +0000193 memset(antiberserk, 0, sizeof(td_t));
194
195 UHCI_INST (controller)->qh_prei = memalign (16, sizeof (qh_t));
Patrick Georgid21f68b2008-09-02 16:06:22 +0000196 UHCI_INST (controller)->qh_intr = memalign (16, sizeof (qh_t));
197 UHCI_INST (controller)->qh_data = memalign (16, sizeof (qh_t));
198 UHCI_INST (controller)->qh_last = memalign (16, sizeof (qh_t));
199
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000200 if (! UHCI_INST (controller)->qh_prei ||
201 ! UHCI_INST (controller)->qh_intr ||
202 ! UHCI_INST (controller)->qh_data ||
203 ! UHCI_INST (controller)->qh_last)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100204 fatal("Not enough memory for USB controller queues.\n");
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000205
Patrick Georgib0b4a522011-11-24 11:55:46 +0100206 UHCI_INST (controller)->qh_prei->headlinkptr =
207 virt_to_phys (UHCI_INST (controller)->qh_intr) | FLISTP_QH;
208 UHCI_INST (controller)->qh_prei->elementlinkptr = 0 | FLISTP_TERMINATE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000209
Patrick Georgib0b4a522011-11-24 11:55:46 +0100210 UHCI_INST (controller)->qh_intr->headlinkptr =
211 virt_to_phys (UHCI_INST (controller)->qh_data) | FLISTP_QH;
212 UHCI_INST (controller)->qh_intr->elementlinkptr = 0 | FLISTP_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000213
Patrick Georgib0b4a522011-11-24 11:55:46 +0100214 UHCI_INST (controller)->qh_data->headlinkptr =
215 virt_to_phys (UHCI_INST (controller)->qh_last) | FLISTP_QH;
216 UHCI_INST (controller)->qh_data->elementlinkptr = 0 | FLISTP_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000217
Patrick Georgib0b4a522011-11-24 11:55:46 +0100218 UHCI_INST (controller)->qh_last->headlinkptr = virt_to_phys (UHCI_INST (controller)->qh_data) | FLISTP_TERMINATE;
219 UHCI_INST (controller)->qh_last->elementlinkptr = virt_to_phys (antiberserk) | FLISTP_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000220
221 for (i = 0; i < 1024; i++) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100222 UHCI_INST (controller)->framelistptr[i] =
223 virt_to_phys (UHCI_INST (controller)->qh_prei) | FLISTP_QH;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000224 }
Patrick Georgi4727c072008-10-16 19:20:51 +0000225 controller->devices[0]->controller = controller;
226 controller->devices[0]->init = uhci_rh_init;
227 controller->devices[0]->init (controller->devices[0]);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000228 uhci_reset (controller);
229 return controller;
230}
231
232static void
233uhci_shutdown (hci_t *controller)
234{
235 if (controller == 0)
236 return;
237 detach_controller (controller);
238 UHCI_INST (controller)->roothub->destroy (UHCI_INST (controller)->
239 roothub);
Patrick Georgif42fdab2011-11-18 14:44:16 +0100240 uhci_reg_write16(controller, USBCMD,
241 uhci_reg_read16(controller, USBCMD) & 0); // stop work
Patrick Georgid21f68b2008-09-02 16:06:22 +0000242 free (UHCI_INST (controller)->framelistptr);
Patrick Georgi4727c072008-10-16 19:20:51 +0000243 free (UHCI_INST (controller)->qh_prei);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000244 free (UHCI_INST (controller)->qh_intr);
245 free (UHCI_INST (controller)->qh_data);
246 free (UHCI_INST (controller)->qh_last);
247 free (UHCI_INST (controller));
248 free (controller);
249}
250
251static void
252uhci_start (hci_t *controller)
253{
Patrick Georgif42fdab2011-11-18 14:44:16 +0100254 uhci_reg_write16(controller, USBCMD,
255 uhci_reg_read16(controller, USBCMD) | 1); // start work on schedule
Patrick Georgid21f68b2008-09-02 16:06:22 +0000256}
257
258static void
259uhci_stop (hci_t *controller)
260{
Patrick Georgif42fdab2011-11-18 14:44:16 +0100261 uhci_reg_write16(controller, USBCMD,
262 uhci_reg_read16(controller, USBCMD) & ~1); // stop work on schedule
Patrick Georgid21f68b2008-09-02 16:06:22 +0000263}
264
265#define GET_TD(x) ((void*)(((unsigned int)(x))&~0xf))
266
267static td_t *
268wait_for_completed_qh (hci_t *controller, qh_t *qh)
269{
Mathias Krause7b7b5662012-05-29 16:19:19 +0200270 int timeout = 1000; /* max 30 ms. */
Patrick Georgib0b4a522011-11-24 11:55:46 +0100271 void *current = GET_TD (qh->elementlinkptr);
272 while (((qh->elementlinkptr & FLISTP_TERMINATE) == 0) && (timeout-- > 0)) {
273 if (current != GET_TD (qh->elementlinkptr)) {
274 current = GET_TD (qh->elementlinkptr);
Mathias Krause7b7b5662012-05-29 16:19:19 +0200275 timeout = 1000;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000276 }
Patrick Georgif42fdab2011-11-18 14:44:16 +0100277 uhci_reg_write16(controller, USBSTS,
278 uhci_reg_read16(controller, USBSTS) | 0); // clear resettable registers
Patrick Georgid21f68b2008-09-02 16:06:22 +0000279 udelay (30);
280 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100281 return (GET_TD (qh->elementlinkptr) ==
282 0) ? 0 : GET_TD (phys_to_virt (qh->elementlinkptr));
Patrick Georgid21f68b2008-09-02 16:06:22 +0000283}
284
Patrick Georgid21f68b2008-09-02 16:06:22 +0000285static int
286maxlen (int size)
287{
288 return (size - 1) & 0x7ff;
289}
290
291static int
292min (int a, int b)
293{
294 if (a < b)
295 return a;
296 else
297 return b;
298}
299
300static int
Patrick Georgid78691d2010-06-07 13:58:17 +0000301uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, int dalen,
Patrick Georgid21f68b2008-09-02 16:06:22 +0000302 unsigned char *data)
303{
304 int endp = 0; /* this is control: always 0 */
305 int mlen = dev->endpoints[0].maxpacketsize;
306 int count = (2 + (dalen + mlen - 1) / mlen);
307 unsigned short req = ((unsigned short *) devreq)[0];
308 int i;
309 td_t *tds = memalign (16, sizeof (td_t) * count);
310 memset (tds, 0, sizeof (td_t) * count);
311 count--; /* to compensate for 0-indexed array */
312 for (i = 0; i < count; i++) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100313 tds[i].ptr = virt_to_phys (&tds[i + 1]) | TD_DEPTH_FIRST;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000314 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100315 tds[count].ptr = 0 | TD_DEPTH_FIRST | TD_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000316
Patrick Georgib0b4a522011-11-24 11:55:46 +0100317 tds[0].token = UHCI_SETUP |
318 dev->address << TD_DEVADDR_SHIFT |
319 endp << TD_EP_SHIFT |
320 TD_TOGGLE_DATA0 |
321 maxlen(drlen) << TD_MAXLEN_SHIFT;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000322 tds[0].bufptr = virt_to_phys (devreq);
Patrick Georgib0b4a522011-11-24 11:55:46 +0100323 tds[0].ctrlsts = (3 << TD_COUNTER_SHIFT) |
324 (dev->speed?TD_LOWSPEED:0) |
325 TD_STATUS_ACTIVE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000326
327 int toggle = 1;
328 for (i = 1; i < count; i++) {
Patrick Georgid78691d2010-06-07 13:58:17 +0000329 switch (dir) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100330 case SETUP: tds[i].token = UHCI_SETUP; break;
331 case IN: tds[i].token = UHCI_IN; break;
332 case OUT: tds[i].token = UHCI_OUT; break;
Patrick Georgid78691d2010-06-07 13:58:17 +0000333 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100334 tds[i].token |= dev->address << TD_DEVADDR_SHIFT |
335 endp << TD_EP_SHIFT |
336 maxlen (min (mlen, dalen)) << TD_MAXLEN_SHIFT |
337 toggle << TD_TOGGLE_SHIFT;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000338 tds[i].bufptr = virt_to_phys (data);
Patrick Georgib0b4a522011-11-24 11:55:46 +0100339 tds[i].ctrlsts = (3 << TD_COUNTER_SHIFT) |
340 (dev->speed?TD_LOWSPEED:0) |
341 TD_STATUS_ACTIVE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000342 toggle ^= 1;
343 dalen -= mlen;
344 data += mlen;
345 }
346
Nico Hubercefec0e2012-05-16 15:04:27 +0200347 tds[count].token = ((dir == OUT) ? UHCI_IN : UHCI_OUT) |
Patrick Georgib0b4a522011-11-24 11:55:46 +0100348 dev->address << TD_DEVADDR_SHIFT |
349 endp << TD_EP_SHIFT |
350 maxlen(0) << TD_MAXLEN_SHIFT |
351 TD_TOGGLE_DATA1;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000352 tds[count].bufptr = 0;
Nico Hubercefec0e2012-05-16 15:04:27 +0200353 tds[count].ctrlsts = (0 << TD_COUNTER_SHIFT) | /* as Linux 2.4.10 does */
Patrick Georgib0b4a522011-11-24 11:55:46 +0100354 (dev->speed?TD_LOWSPEED:0) |
355 TD_STATUS_ACTIVE;
356 UHCI_INST (dev->controller)->qh_data->elementlinkptr =
357 virt_to_phys (tds) & ~(FLISTP_QH | FLISTP_TERMINATE);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000358 td_t *td = wait_for_completed_qh (dev->controller,
359 UHCI_INST (dev->controller)->
360 qh_data);
361 int result;
362 if (td == 0) {
363 result = 0;
364 } else {
Mathias Krausec4716b42011-06-08 15:36:55 +0200365 debug ("control packet, req %x\n", req);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000366 td_dump (td);
367 result = 1;
368 }
369 free (tds);
370 return result;
371}
372
Patrick Georgid21f68b2008-09-02 16:06:22 +0000373static td_t *
374create_schedule (int numpackets)
375{
376 if (numpackets == 0)
377 return 0;
378 td_t *tds = memalign (16, sizeof (td_t) * numpackets);
379 memset (tds, 0, sizeof (td_t) * numpackets);
380 int i;
381 for (i = 0; i < numpackets; i++) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100382 tds[i].ptr = virt_to_phys (&tds[i + 1]) | TD_DEPTH_FIRST;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000383 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100384 tds[numpackets - 1].ptr = 0 | TD_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000385 return tds;
386}
387
388static void
389fill_schedule (td_t *td, endpoint_t *ep, int length, unsigned char *data,
390 int *toggle)
391{
Patrick Georgicfaa0812010-06-11 14:25:40 +0000392 switch (ep->direction) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100393 case IN: td->token = UHCI_IN; break;
394 case OUT: td->token = UHCI_OUT; break;
395 case SETUP: td->token = UHCI_SETUP; break;
Patrick Georgicfaa0812010-06-11 14:25:40 +0000396 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100397 td->token |= ep->dev->address << TD_DEVADDR_SHIFT |
398 (ep->endpoint & 0xf) << TD_EP_SHIFT |
399 maxlen (length) << TD_MAXLEN_SHIFT |
400 (*toggle & 1) << TD_TOGGLE_SHIFT;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000401 td->bufptr = virt_to_phys (data);
Patrick Georgib0b4a522011-11-24 11:55:46 +0100402 td->ctrlsts = ((ep->direction == SETUP?3:0) << TD_COUNTER_SHIFT) |
Nico Hubercefec0e2012-05-16 15:04:27 +0200403 (ep->dev->speed?TD_LOWSPEED:0) |
Patrick Georgib0b4a522011-11-24 11:55:46 +0100404 TD_STATUS_ACTIVE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000405 *toggle ^= 1;
406}
407
408static int
409run_schedule (usbdev_t *dev, td_t *td)
410{
Patrick Georgib0b4a522011-11-24 11:55:46 +0100411 UHCI_INST (dev->controller)->qh_data->elementlinkptr =
412 virt_to_phys (td) | ~(FLISTP_QH | FLISTP_TERMINATE);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000413 td = wait_for_completed_qh (dev->controller,
414 UHCI_INST (dev->controller)->qh_data);
415 if (td == 0) {
416 return 0;
417 } else {
418 td_dump (td);
419 return 1;
420 }
421}
422
423/* finalize == 1: if data is of packet aligned size, add a zero length packet */
424static int
425uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize)
426{
427 int maxpsize = ep->maxpacketsize;
428 if (maxpsize == 0)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100429 fatal("MaxPacketSize == 0!!!");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000430 int numpackets = (size + maxpsize - 1 + finalize) / maxpsize;
431 if (numpackets == 0)
432 return 0;
433 td_t *tds = create_schedule (numpackets);
434 int i = 0, toggle = ep->toggle;
435 while ((size > 0) || ((size == 0) && (finalize != 0))) {
436 fill_schedule (&tds[i], ep, min (size, maxpsize), data,
437 &toggle);
438 i++;
439 data += maxpsize;
440 size -= maxpsize;
441 }
442 if (run_schedule (ep->dev, tds) == 1) {
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000443 debug("Stalled. Trying to clean up.\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000444 clear_stall (ep);
445 free (tds);
446 return 1;
447 }
448 ep->toggle = toggle;
449 free (tds);
450 return 0;
451}
452
Patrick Georgi4727c072008-10-16 19:20:51 +0000453typedef struct {
454 qh_t *qh;
455 td_t *tds;
456 td_t *last_td;
457 u8 *data;
458 int lastread;
459 int total;
460 int reqsize;
461} intr_q;
462
463/* create and hook-up an intr queue into device schedule */
464static void*
465uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming)
466{
467 u8 *data = malloc(reqsize*reqcount);
468 td_t *tds = memalign(16, sizeof(td_t) * reqcount);
469 qh_t *qh = memalign(16, sizeof(qh_t));
470
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000471 if (!data || !tds || !qh)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100472 fatal("Not enough memory to create USB intr queue prerequisites.\n");
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000473
Patrick Georgib0b4a522011-11-24 11:55:46 +0100474 qh->elementlinkptr = virt_to_phys(tds);
Patrick Georgi4727c072008-10-16 19:20:51 +0000475
476 intr_q *q = malloc(sizeof(intr_q));
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000477 if (!q)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100478 fatal("Not enough memory to create USB intr queue.\n");
Patrick Georgi4727c072008-10-16 19:20:51 +0000479 q->qh = qh;
480 q->tds = tds;
481 q->data = data;
482 q->lastread = 0;
483 q->total = reqcount;
484 q->reqsize = reqsize;
485 q->last_td = &tds[reqcount - 1];
486
487 memset (tds, 0, sizeof (td_t) * reqcount);
488 int i;
489 for (i = 0; i < reqcount; i++) {
490 tds[i].ptr = virt_to_phys (&tds[i + 1]);
Patrick Georgi4727c072008-10-16 19:20:51 +0000491
Patrick Georgicfaa0812010-06-11 14:25:40 +0000492 switch (ep->direction) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100493 case IN: tds[i].token = UHCI_IN; break;
494 case OUT: tds[i].token = UHCI_OUT; break;
495 case SETUP: tds[i].token = UHCI_SETUP; break;
Patrick Georgicfaa0812010-06-11 14:25:40 +0000496 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100497 tds[i].token |= ep->dev->address << TD_DEVADDR_SHIFT |
498 (ep->endpoint & 0xf) << TD_EP_SHIFT |
499 maxlen (reqsize) << TD_MAXLEN_SHIFT |
500 (ep->toggle & 1) << TD_TOGGLE_SHIFT;
Patrick Georgi4727c072008-10-16 19:20:51 +0000501 tds[i].bufptr = virt_to_phys (data);
Patrick Georgib0b4a522011-11-24 11:55:46 +0100502 tds[i].ctrlsts = (0 << TD_COUNTER_SHIFT) |
Nico Hubercefec0e2012-05-16 15:04:27 +0200503 (ep->dev->speed?TD_LOWSPEED:0) |
Patrick Georgib0b4a522011-11-24 11:55:46 +0100504 TD_STATUS_ACTIVE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000505 ep->toggle ^= 1;
506 data += reqsize;
507 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100508 tds[reqcount - 1].ptr = 0 | TD_TERMINATE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000509 for (i = reqtiming; i < 1024; i += reqtiming) {
510 /* FIXME: wrap in another qh, one for each occurance of the qh in the framelist */
Patrick Georgib0b4a522011-11-24 11:55:46 +0100511 qh->headlinkptr = UHCI_INST (ep->dev->controller)->framelistptr[i] & ~FLISTP_TERMINATE;
512 UHCI_INST (ep->dev->controller)->framelistptr[i] = virt_to_phys(qh) | FLISTP_QH;
Patrick Georgi4727c072008-10-16 19:20:51 +0000513 }
514 return q;
515}
516
517/* remove queue from device schedule, dropping all data that came in */
518static void
519uhci_destroy_intr_queue (endpoint_t *ep, void *q_)
520{
521 intr_q *q = (intr_q*)q_;
522 u32 val = virt_to_phys (q->qh);
523 u32 end = virt_to_phys (UHCI_INST (ep->dev->controller)->qh_intr);
524 int i;
525 for (i=0; i<1024; i++) {
526 u32 oldptr = 0;
Patrick Georgib0b4a522011-11-24 11:55:46 +0100527 u32 ptr = UHCI_INST (ep->dev->controller)->framelistptr[i];
Patrick Georgi4727c072008-10-16 19:20:51 +0000528 while (ptr != end) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100529 if (((qh_t*)phys_to_virt(ptr))->elementlinkptr == val) {
530 ((qh_t*)phys_to_virt(oldptr))->headlinkptr = ((qh_t*)phys_to_virt(ptr))->headlinkptr;
Patrick Georgi4727c072008-10-16 19:20:51 +0000531 free(phys_to_virt(ptr));
532 break;
533 }
534 oldptr = ptr;
Patrick Georgib0b4a522011-11-24 11:55:46 +0100535 ptr = ((qh_t*)phys_to_virt(ptr))->headlinkptr;
Patrick Georgi4727c072008-10-16 19:20:51 +0000536 }
537 }
538 free(q->data);
539 free(q->tds);
540 free(q->qh);
541 free(q);
542}
543
544/* read one intr-packet from queue, if available. extend the queue for new input.
545 return NULL if nothing new available.
546 Recommended use: while (data=poll_intr_queue(q)) process(data);
547 */
548static u8*
549uhci_poll_intr_queue (void *q_)
550{
551 intr_q *q = (intr_q*)q_;
Patrick Georgib0b4a522011-11-24 11:55:46 +0100552 if ((q->tds[q->lastread].ctrlsts & TD_STATUS_ACTIVE) == 0) {
Patrick Georgi4727c072008-10-16 19:20:51 +0000553 /* FIXME: handle errors */
554 int current = q->lastread;
555 int previous;
556 if (q->lastread == 0) {
557 previous = q->total - 1;
558 } else {
559 previous = q->lastread - 1;
560 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100561 q->tds[previous].ctrlsts &= ~TD_STATUS_MASK;
562 q->tds[previous].ptr = 0 | TD_TERMINATE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000563 if (q->last_td != &q->tds[previous]) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100564 q->last_td->ptr = virt_to_phys(&q->tds[previous]) & ~TD_TERMINATE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000565 q->last_td = &q->tds[previous];
566 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100567 q->tds[previous].ctrlsts |= TD_STATUS_ACTIVE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000568 q->lastread = (q->lastread + 1) % q->total;
569 return &q->data[current*q->reqsize];
570 }
571 return NULL;
572}
573
Patrick Georgid21f68b2008-09-02 16:06:22 +0000574void
575uhci_reg_write32 (hci_t *ctrl, usbreg reg, u32 value)
576{
577 outl (value, ctrl->reg_base + reg);
578}
579
580u32
581uhci_reg_read32 (hci_t *ctrl, usbreg reg)
582{
583 return inl (ctrl->reg_base + reg);
584}
585
586void
587uhci_reg_write16 (hci_t *ctrl, usbreg reg, u16 value)
588{
589 outw (value, ctrl->reg_base + reg);
590}
591
592u16
593uhci_reg_read16 (hci_t *ctrl, usbreg reg)
594{
595 return inw (ctrl->reg_base + reg);
596}
597
598void
599uhci_reg_write8 (hci_t *ctrl, usbreg reg, u8 value)
600{
601 outb (value, ctrl->reg_base + reg);
602}
603
604u8
605uhci_reg_read8 (hci_t *ctrl, usbreg reg)
606{
607 return inb (ctrl->reg_base + reg);
608}