blob: cdba3de1019bd39f0333e58108066b0856ee5d0a [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{
Gabe Black93ded592012-11-01 15:44:10 -070053 usb_debug ("dump:\nUSBCMD: %x\n", uhci_reg_read16 (controller, USBCMD));
54 usb_debug ("USBSTS: %x\n", uhci_reg_read16 (controller, USBSTS));
55 usb_debug ("USBINTR: %x\n", uhci_reg_read16 (controller, USBINTR));
56 usb_debug ("FRNUM: %x\n", uhci_reg_read16 (controller, FRNUM));
57 usb_debug ("FLBASEADD: %x\n", uhci_reg_read32 (controller, FLBASEADD));
58 usb_debug ("SOFMOD: %x\n", uhci_reg_read8 (controller, SOFMOD));
59 usb_debug ("PORTSC1: %x\n", uhci_reg_read16 (controller, PORTSC1));
60 usb_debug ("PORTSC2: %x\n", uhci_reg_read16 (controller, PORTSC2));
Patrick Georgid21f68b2008-09-02 16:06:22 +000061}
62#endif
63
Anton Kochkovc62b69c2012-12-19 13:49:20 +040064static void td_dump(td_t *td)
Patrick Georgid21f68b2008-09-02 16:06:22 +000065{
Anton Kochkovc62b69c2012-12-19 13:49:20 +040066 usb_debug("+---------------------------------------------------+\n");
67 if ((td->token & TD_PID_MASK) == UHCI_SETUP)
68 usb_debug("|..[SETUP]..........................................|\n");
69 else if ((td->token & TD_PID_MASK) == UHCI_IN)
70 usb_debug("|..[IN].............................................|\n");
71 else if ((td->token & TD_PID_MASK) == UHCI_OUT)
72 usb_debug("|..[OUT]............................................|\n");
73 else
74 usb_debug("|..[]...............................................|\n");
75 usb_debug("|:|============ UHCI TD at [0x%08lx] ==========|:|\n", virt_to_phys(td));
76 usb_debug("|:+-----------------------------------------------+:|\n");
77 usb_debug("|:| Next TD/QH [0x%08lx] |:|\n", td->ptr & ~0xFUL);
78 usb_debug("|:+-----------------------------------------------+:|\n");
79 usb_debug("|:| Depth/Breath [%lx] | QH/TD [%lx] | TERMINATE [%lx] |:|\n",
80 (td->ptr & (1UL << 2)) >> 2, (td->ptr & (1UL << 1)) >> 1, td->ptr & 1UL);
81 usb_debug("|:+-----------------------------------------------+:|\n");
82 usb_debug("|:| T | Maximum Length | [%04lx] |:|\n", (td->token & (0x7FFUL << 21)) >> 21);
83 usb_debug("|:| O | PID CODE | [%04lx] |:|\n", td->token & 0xFF);
84 usb_debug("|:| K | Endpoint | [%04lx] |:|\n", (td->token & TD_EP_MASK) >> TD_EP_SHIFT);
85 usb_debug("|:| E | Device Address | [%04lx] |:|\n", (td->token & (0x7FUL << 8)) >> 8);
86 usb_debug("|:| N | Data Toggle | [%lx] |:|\n", (td->token & (1UL << 19)) >> 19);
87 usb_debug("|:+-----------------------------------------------+:|\n");
88 usb_debug("|:| C | Short Packet Detector | [%lx] |:|\n", (td->ctrlsts & (1UL << 29)) >> 29);
89 usb_debug("|:| O | Error Counter | [%lx] |:|\n",
90 (td->ctrlsts & (3UL << TD_COUNTER_SHIFT)) >> TD_COUNTER_SHIFT);
91 usb_debug("|:| N | Low Speed Device | [%lx] |:|\n", (td->ctrlsts & (1UL << 26)) >> 26);
92 usb_debug("|:| T | Isochronous Select | [%lx] |:|\n", (td->ctrlsts & (1UL << 25)) >> 25);
93 usb_debug("|:| R | Interrupt on Complete (IOC) | [%lx] |:|\n", (td->ctrlsts & (1UL << 24)) >> 24);
94 usb_debug("|:+ O ----------------------------------------+:|\n");
95 usb_debug("|:| L | Active | [%lx] |:|\n", (td->ctrlsts & (1UL << 23)) >> 23);
96 usb_debug("|:| & | Stalled | [%lx] |:|\n", (td->ctrlsts & (1UL << 22)) >> 22);
97 usb_debug("|:| S | Data Buffer Error | [%lx] |:|\n", (td->ctrlsts & (1UL << 21)) >> 21);
98 usb_debug("|:| T | Bubble Detected | [%lx] |:|\n", (td->ctrlsts & (1UL << 20)) >> 20);
99 usb_debug("|:| A | NAK Received | [%lx] |:|\n", (td->ctrlsts & (1UL << 19)) >> 19);
100 usb_debug("|:| T | CRC/Timeout Error | [%lx] |:|\n", (td->ctrlsts & (1UL << 18)) >> 18);
101 usb_debug("|:| U | Bitstuff Error | [%lx] |:|\n", (td->ctrlsts & (1UL << 17)) >> 17);
102 usb_debug("|:| S ----------------------------------------|:|\n");
103 usb_debug("|:| | Actual Length | [%04lx] |:|\n", td->ctrlsts & 0x7FFUL);
104 usb_debug("|:+-----------------------------------------------+:|\n");
105 usb_debug("|:| Buffer pointer [0x%08lx] |:|\n", td->bufptr);
106 usb_debug("|:|-----------------------------------------------|:|\n");
107 usb_debug("|...................................................|\n");
108 usb_debug("+---------------------------------------------------+\n");
Patrick Georgid21f68b2008-09-02 16:06:22 +0000109}
110
111static void
112uhci_reset (hci_t *controller)
113{
114 /* reset */
Nico Huberbb1c42b2012-05-21 14:23:03 +0200115 uhci_reg_write16 (controller, USBCMD, 4); /* Global Reset */
116 mdelay (50); /* uhci spec 2.1.1: at least 10ms */
Patrick Georgid21f68b2008-09-02 16:06:22 +0000117 uhci_reg_write16 (controller, USBCMD, 0);
118 mdelay (10);
Nico Huberbb1c42b2012-05-21 14:23:03 +0200119 uhci_reg_write16 (controller, USBCMD, 2); /* Host Controller Reset */
120 /* wait for controller to finish reset */
121 /* TOTEST: how long to wait? 100ms for now */
122 int timeout = 200; /* time out after 200 * 500us == 100ms */
123 while (((uhci_reg_read16 (controller, USBCMD) & 2) != 0) && timeout--)
124 udelay (500);
125 if (timeout < 0)
Gabe Black93ded592012-11-01 15:44:10 -0700126 usb_debug ("Warning: uhci: host controller reset timed out.\n");
Nico Huber6e711c62012-11-12 16:20:32 +0100127}
Patrick Georgid21f68b2008-09-02 16:06:22 +0000128
Nico Huber6e711c62012-11-12 16:20:32 +0100129static void
130uhci_reinit (hci_t *controller)
131{
Patrick Georgid21f68b2008-09-02 16:06:22 +0000132 uhci_reg_write32 (controller, FLBASEADD,
133 (u32) virt_to_phys (UHCI_INST (controller)->
134 framelistptr));
Gabe Black93ded592012-11-01 15:44:10 -0700135 //usb_debug ("framelist at %p\n",UHCI_INST(controller)->framelistptr);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000136
137 /* disable irqs */
138 uhci_reg_write16 (controller, USBINTR, 0);
139
140 /* reset framelist index */
141 uhci_reg_write16 (controller, FRNUM, 0);
142
Patrick Georgif42fdab2011-11-18 14:44:16 +0100143 uhci_reg_write16(controller, USBCMD,
144 uhci_reg_read16(controller, USBCMD) | 0xc0); // max packets, configure flag
Patrick Georgid21f68b2008-09-02 16:06:22 +0000145
146 uhci_start (controller);
147}
148
149hci_t *
Stefan Reinauer8992e532013-05-02 16:16:41 -0700150uhci_pci_init (pcidev_t addr)
Patrick Georgid21f68b2008-09-02 16:06:22 +0000151{
152 int i;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000153 u16 reg16;
154
Patrick Georgid21f68b2008-09-02 16:06:22 +0000155 hci_t *controller = new_controller ();
Julius Werner7234d602014-04-08 12:54:25 -0700156 controller->instance = xzalloc(sizeof (uhci_t));
Anton Kochkov1c36ead2012-06-28 08:30:15 +0400157 controller->type = UHCI;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000158 controller->start = uhci_start;
159 controller->stop = uhci_stop;
160 controller->reset = uhci_reset;
Nico Huber6e711c62012-11-12 16:20:32 +0100161 controller->init = uhci_reinit;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000162 controller->shutdown = uhci_shutdown;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000163 controller->bulk = uhci_bulk;
164 controller->control = uhci_control;
Patrick Georgi482af6d2013-05-24 15:48:56 +0200165 controller->set_address = generic_set_address;
166 controller->finish_device_config = NULL;
167 controller->destroy_device = NULL;
Patrick Georgi4727c072008-10-16 19:20:51 +0000168 controller->create_intr_queue = uhci_create_intr_queue;
169 controller->destroy_intr_queue = uhci_destroy_intr_queue;
170 controller->poll_intr_queue = uhci_poll_intr_queue;
Patrick Georgi4727c072008-10-16 19:20:51 +0000171 init_device_entry (controller, 0);
172 UHCI_INST (controller)->roothub = controller->devices[0];
Patrick Georgid21f68b2008-09-02 16:06:22 +0000173
Stefan Reinauer8992e532013-05-02 16:16:41 -0700174 /* ~1 clears the register type indicator that is set to 1
175 * for IO space */
176 controller->reg_base = pci_read_config32 (addr, 0x20) & ~1;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000177
178 /* kill legacy support handler */
179 uhci_stop (controller);
180 mdelay (1);
181 uhci_reg_write16 (controller, USBSTS, 0x3f);
Stefan Reinauer8992e532013-05-02 16:16:41 -0700182 reg16 = pci_read_config16(addr, 0xc0);
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000183 reg16 &= 0xdf80;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700184 pci_write_config16 (addr, 0xc0, reg16);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000185
Anton Kochkovf6c80802012-09-20 10:24:01 +0200186 UHCI_INST (controller)->framelistptr = memalign (0x1000, 1024 * sizeof (flistp_t)); /* 4kb aligned to 4kb */
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000187 if (! UHCI_INST (controller)->framelistptr)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100188 fatal("Not enough memory for USB frame list pointer.\n");
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000189
Patrick Georgid21f68b2008-09-02 16:06:22 +0000190 memset (UHCI_INST (controller)->framelistptr, 0,
191 1024 * sizeof (flistp_t));
192
Patrick Georgi4727c072008-10-16 19:20:51 +0000193 /* According to the *BSD UHCI code, this one is needed on some
194 PIIX chips, because otherwise they misbehave. It must be
195 added to the last chain.
196
197 FIXME: this leaks, if the driver should ever be reinited
198 for some reason. Not a problem now.
199 */
200 td_t *antiberserk = memalign(16, sizeof(td_t));
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000201 if (!antiberserk)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100202 fatal("Not enough memory for chipset workaround.\n");
Patrick Georgi4727c072008-10-16 19:20:51 +0000203 memset(antiberserk, 0, sizeof(td_t));
204
205 UHCI_INST (controller)->qh_prei = memalign (16, sizeof (qh_t));
Patrick Georgid21f68b2008-09-02 16:06:22 +0000206 UHCI_INST (controller)->qh_intr = memalign (16, sizeof (qh_t));
207 UHCI_INST (controller)->qh_data = memalign (16, sizeof (qh_t));
208 UHCI_INST (controller)->qh_last = memalign (16, sizeof (qh_t));
209
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000210 if (! UHCI_INST (controller)->qh_prei ||
211 ! UHCI_INST (controller)->qh_intr ||
212 ! UHCI_INST (controller)->qh_data ||
213 ! UHCI_INST (controller)->qh_last)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100214 fatal("Not enough memory for USB controller queues.\n");
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000215
Patrick Georgib0b4a522011-11-24 11:55:46 +0100216 UHCI_INST (controller)->qh_prei->headlinkptr =
217 virt_to_phys (UHCI_INST (controller)->qh_intr) | FLISTP_QH;
218 UHCI_INST (controller)->qh_prei->elementlinkptr = 0 | FLISTP_TERMINATE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000219
Patrick Georgib0b4a522011-11-24 11:55:46 +0100220 UHCI_INST (controller)->qh_intr->headlinkptr =
221 virt_to_phys (UHCI_INST (controller)->qh_data) | FLISTP_QH;
222 UHCI_INST (controller)->qh_intr->elementlinkptr = 0 | FLISTP_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000223
Patrick Georgib0b4a522011-11-24 11:55:46 +0100224 UHCI_INST (controller)->qh_data->headlinkptr =
225 virt_to_phys (UHCI_INST (controller)->qh_last) | FLISTP_QH;
226 UHCI_INST (controller)->qh_data->elementlinkptr = 0 | FLISTP_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000227
Patrick Georgib0b4a522011-11-24 11:55:46 +0100228 UHCI_INST (controller)->qh_last->headlinkptr = virt_to_phys (UHCI_INST (controller)->qh_data) | FLISTP_TERMINATE;
229 UHCI_INST (controller)->qh_last->elementlinkptr = virt_to_phys (antiberserk) | FLISTP_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000230
231 for (i = 0; i < 1024; i++) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100232 UHCI_INST (controller)->framelistptr[i] =
233 virt_to_phys (UHCI_INST (controller)->qh_prei) | FLISTP_QH;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000234 }
Patrick Georgi4727c072008-10-16 19:20:51 +0000235 controller->devices[0]->controller = controller;
236 controller->devices[0]->init = uhci_rh_init;
237 controller->devices[0]->init (controller->devices[0]);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000238 uhci_reset (controller);
Nico Huber6e711c62012-11-12 16:20:32 +0100239 uhci_reinit (controller);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000240 return controller;
241}
242
243static void
244uhci_shutdown (hci_t *controller)
245{
246 if (controller == 0)
247 return;
248 detach_controller (controller);
Patrick Georgif42fdab2011-11-18 14:44:16 +0100249 uhci_reg_write16(controller, USBCMD,
250 uhci_reg_read16(controller, USBCMD) & 0); // stop work
Patrick Georgid21f68b2008-09-02 16:06:22 +0000251 free (UHCI_INST (controller)->framelistptr);
Patrick Georgi4727c072008-10-16 19:20:51 +0000252 free (UHCI_INST (controller)->qh_prei);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000253 free (UHCI_INST (controller)->qh_intr);
254 free (UHCI_INST (controller)->qh_data);
255 free (UHCI_INST (controller)->qh_last);
256 free (UHCI_INST (controller));
257 free (controller);
258}
259
260static void
261uhci_start (hci_t *controller)
262{
Patrick Georgif42fdab2011-11-18 14:44:16 +0100263 uhci_reg_write16(controller, USBCMD,
264 uhci_reg_read16(controller, USBCMD) | 1); // start work on schedule
Patrick Georgid21f68b2008-09-02 16:06:22 +0000265}
266
267static void
268uhci_stop (hci_t *controller)
269{
Patrick Georgif42fdab2011-11-18 14:44:16 +0100270 uhci_reg_write16(controller, USBCMD,
271 uhci_reg_read16(controller, USBCMD) & ~1); // stop work on schedule
Patrick Georgid21f68b2008-09-02 16:06:22 +0000272}
273
274#define GET_TD(x) ((void*)(((unsigned int)(x))&~0xf))
275
276static td_t *
277wait_for_completed_qh (hci_t *controller, qh_t *qh)
278{
Mathias Krause7b7b5662012-05-29 16:19:19 +0200279 int timeout = 1000; /* max 30 ms. */
Patrick Georgib0b4a522011-11-24 11:55:46 +0100280 void *current = GET_TD (qh->elementlinkptr);
281 while (((qh->elementlinkptr & FLISTP_TERMINATE) == 0) && (timeout-- > 0)) {
282 if (current != GET_TD (qh->elementlinkptr)) {
283 current = GET_TD (qh->elementlinkptr);
Mathias Krause7b7b5662012-05-29 16:19:19 +0200284 timeout = 1000;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000285 }
Patrick Georgif42fdab2011-11-18 14:44:16 +0100286 uhci_reg_write16(controller, USBSTS,
287 uhci_reg_read16(controller, USBSTS) | 0); // clear resettable registers
Patrick Georgid21f68b2008-09-02 16:06:22 +0000288 udelay (30);
289 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100290 return (GET_TD (qh->elementlinkptr) ==
291 0) ? 0 : GET_TD (phys_to_virt (qh->elementlinkptr));
Patrick Georgid21f68b2008-09-02 16:06:22 +0000292}
293
Patrick Georgid21f68b2008-09-02 16:06:22 +0000294static int
295maxlen (int size)
296{
297 return (size - 1) & 0x7ff;
298}
299
300static int
301min (int a, int b)
302{
303 if (a < b)
304 return a;
305 else
306 return b;
307}
308
309static int
Patrick Georgid78691d2010-06-07 13:58:17 +0000310uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, int dalen,
Patrick Georgid21f68b2008-09-02 16:06:22 +0000311 unsigned char *data)
312{
313 int endp = 0; /* this is control: always 0 */
314 int mlen = dev->endpoints[0].maxpacketsize;
315 int count = (2 + (dalen + mlen - 1) / mlen);
316 unsigned short req = ((unsigned short *) devreq)[0];
317 int i;
318 td_t *tds = memalign (16, sizeof (td_t) * count);
319 memset (tds, 0, sizeof (td_t) * count);
320 count--; /* to compensate for 0-indexed array */
321 for (i = 0; i < count; i++) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100322 tds[i].ptr = virt_to_phys (&tds[i + 1]) | TD_DEPTH_FIRST;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000323 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100324 tds[count].ptr = 0 | TD_DEPTH_FIRST | TD_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000325
Patrick Georgib0b4a522011-11-24 11:55:46 +0100326 tds[0].token = UHCI_SETUP |
327 dev->address << TD_DEVADDR_SHIFT |
328 endp << TD_EP_SHIFT |
329 TD_TOGGLE_DATA0 |
330 maxlen(drlen) << TD_MAXLEN_SHIFT;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000331 tds[0].bufptr = virt_to_phys (devreq);
Patrick Georgib0b4a522011-11-24 11:55:46 +0100332 tds[0].ctrlsts = (3 << TD_COUNTER_SHIFT) |
333 (dev->speed?TD_LOWSPEED:0) |
334 TD_STATUS_ACTIVE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000335
336 int toggle = 1;
337 for (i = 1; i < count; i++) {
Patrick Georgid78691d2010-06-07 13:58:17 +0000338 switch (dir) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100339 case SETUP: tds[i].token = UHCI_SETUP; break;
340 case IN: tds[i].token = UHCI_IN; break;
341 case OUT: tds[i].token = UHCI_OUT; break;
Patrick Georgid78691d2010-06-07 13:58:17 +0000342 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100343 tds[i].token |= dev->address << TD_DEVADDR_SHIFT |
344 endp << TD_EP_SHIFT |
345 maxlen (min (mlen, dalen)) << TD_MAXLEN_SHIFT |
346 toggle << TD_TOGGLE_SHIFT;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000347 tds[i].bufptr = virt_to_phys (data);
Patrick Georgib0b4a522011-11-24 11:55:46 +0100348 tds[i].ctrlsts = (3 << TD_COUNTER_SHIFT) |
349 (dev->speed?TD_LOWSPEED:0) |
350 TD_STATUS_ACTIVE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000351 toggle ^= 1;
352 dalen -= mlen;
353 data += mlen;
354 }
355
Nico Hubercefec0e2012-05-16 15:04:27 +0200356 tds[count].token = ((dir == OUT) ? UHCI_IN : UHCI_OUT) |
Patrick Georgib0b4a522011-11-24 11:55:46 +0100357 dev->address << TD_DEVADDR_SHIFT |
358 endp << TD_EP_SHIFT |
359 maxlen(0) << TD_MAXLEN_SHIFT |
360 TD_TOGGLE_DATA1;
Stefan Reinauerb56f2d02010-03-25 22:17:36 +0000361 tds[count].bufptr = 0;
Nico Hubercefec0e2012-05-16 15:04:27 +0200362 tds[count].ctrlsts = (0 << TD_COUNTER_SHIFT) | /* as Linux 2.4.10 does */
Patrick Georgib0b4a522011-11-24 11:55:46 +0100363 (dev->speed?TD_LOWSPEED:0) |
364 TD_STATUS_ACTIVE;
365 UHCI_INST (dev->controller)->qh_data->elementlinkptr =
366 virt_to_phys (tds) & ~(FLISTP_QH | FLISTP_TERMINATE);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000367 td_t *td = wait_for_completed_qh (dev->controller,
368 UHCI_INST (dev->controller)->
369 qh_data);
370 int result;
371 if (td == 0) {
372 result = 0;
373 } else {
Gabe Black93ded592012-11-01 15:44:10 -0700374 usb_debug ("control packet, req %x\n", req);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000375 td_dump (td);
Julius Wernere9738db2013-02-21 13:41:40 -0800376 result = -1;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000377 }
378 free (tds);
379 return result;
380}
381
Patrick Georgid21f68b2008-09-02 16:06:22 +0000382static td_t *
383create_schedule (int numpackets)
384{
385 if (numpackets == 0)
386 return 0;
387 td_t *tds = memalign (16, sizeof (td_t) * numpackets);
388 memset (tds, 0, sizeof (td_t) * numpackets);
389 int i;
390 for (i = 0; i < numpackets; i++) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100391 tds[i].ptr = virt_to_phys (&tds[i + 1]) | TD_DEPTH_FIRST;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000392 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100393 tds[numpackets - 1].ptr = 0 | TD_TERMINATE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000394 return tds;
395}
396
397static void
398fill_schedule (td_t *td, endpoint_t *ep, int length, unsigned char *data,
399 int *toggle)
400{
Patrick Georgicfaa0812010-06-11 14:25:40 +0000401 switch (ep->direction) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100402 case IN: td->token = UHCI_IN; break;
403 case OUT: td->token = UHCI_OUT; break;
404 case SETUP: td->token = UHCI_SETUP; break;
Patrick Georgicfaa0812010-06-11 14:25:40 +0000405 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100406 td->token |= ep->dev->address << TD_DEVADDR_SHIFT |
407 (ep->endpoint & 0xf) << TD_EP_SHIFT |
408 maxlen (length) << TD_MAXLEN_SHIFT |
409 (*toggle & 1) << TD_TOGGLE_SHIFT;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000410 td->bufptr = virt_to_phys (data);
Patrick Georgib0b4a522011-11-24 11:55:46 +0100411 td->ctrlsts = ((ep->direction == SETUP?3:0) << TD_COUNTER_SHIFT) |
Nico Hubercefec0e2012-05-16 15:04:27 +0200412 (ep->dev->speed?TD_LOWSPEED:0) |
Patrick Georgib0b4a522011-11-24 11:55:46 +0100413 TD_STATUS_ACTIVE;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000414 *toggle ^= 1;
415}
416
417static int
418run_schedule (usbdev_t *dev, td_t *td)
419{
Patrick Georgib0b4a522011-11-24 11:55:46 +0100420 UHCI_INST (dev->controller)->qh_data->elementlinkptr =
Anton Kochkovefcb8de2012-10-02 00:04:29 +0400421 virt_to_phys (td) & ~(FLISTP_QH | FLISTP_TERMINATE);
Patrick Georgid21f68b2008-09-02 16:06:22 +0000422 td = wait_for_completed_qh (dev->controller,
423 UHCI_INST (dev->controller)->qh_data);
424 if (td == 0) {
425 return 0;
426 } else {
427 td_dump (td);
428 return 1;
429 }
430}
431
432/* finalize == 1: if data is of packet aligned size, add a zero length packet */
433static int
434uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize)
435{
436 int maxpsize = ep->maxpacketsize;
437 if (maxpsize == 0)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100438 fatal("MaxPacketSize == 0!!!");
Patrick Georgid2cb7ea2012-10-03 08:23:56 +0200439 int numpackets = (size + maxpsize - 1) / maxpsize;
440 if (finalize && ((size % maxpsize) == 0)) {
441 numpackets++;
442 }
Patrick Georgid21f68b2008-09-02 16:06:22 +0000443 if (numpackets == 0)
444 return 0;
445 td_t *tds = create_schedule (numpackets);
446 int i = 0, toggle = ep->toggle;
447 while ((size > 0) || ((size == 0) && (finalize != 0))) {
448 fill_schedule (&tds[i], ep, min (size, maxpsize), data,
449 &toggle);
450 i++;
451 data += maxpsize;
452 size -= maxpsize;
453 }
454 if (run_schedule (ep->dev, tds) == 1) {
Patrick Georgid21f68b2008-09-02 16:06:22 +0000455 free (tds);
Julius Wernere9738db2013-02-21 13:41:40 -0800456 return -1;
Patrick Georgid21f68b2008-09-02 16:06:22 +0000457 }
458 ep->toggle = toggle;
459 free (tds);
460 return 0;
461}
462
Patrick Georgi4727c072008-10-16 19:20:51 +0000463typedef struct {
464 qh_t *qh;
465 td_t *tds;
466 td_t *last_td;
467 u8 *data;
468 int lastread;
469 int total;
470 int reqsize;
471} intr_q;
472
473/* create and hook-up an intr queue into device schedule */
474static void*
475uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming)
476{
477 u8 *data = malloc(reqsize*reqcount);
478 td_t *tds = memalign(16, sizeof(td_t) * reqcount);
479 qh_t *qh = memalign(16, sizeof(qh_t));
480
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000481 if (!data || !tds || !qh)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100482 fatal("Not enough memory to create USB intr queue prerequisites.\n");
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000483
Patrick Georgib0b4a522011-11-24 11:55:46 +0100484 qh->elementlinkptr = virt_to_phys(tds);
Patrick Georgi4727c072008-10-16 19:20:51 +0000485
486 intr_q *q = malloc(sizeof(intr_q));
Stefan Reinauer5fe6e232009-07-31 11:39:55 +0000487 if (!q)
Patrick Georgi2e768e72011-11-04 11:50:03 +0100488 fatal("Not enough memory to create USB intr queue.\n");
Patrick Georgi4727c072008-10-16 19:20:51 +0000489 q->qh = qh;
490 q->tds = tds;
491 q->data = data;
492 q->lastread = 0;
493 q->total = reqcount;
494 q->reqsize = reqsize;
495 q->last_td = &tds[reqcount - 1];
496
497 memset (tds, 0, sizeof (td_t) * reqcount);
498 int i;
499 for (i = 0; i < reqcount; i++) {
500 tds[i].ptr = virt_to_phys (&tds[i + 1]);
Patrick Georgi4727c072008-10-16 19:20:51 +0000501
Patrick Georgicfaa0812010-06-11 14:25:40 +0000502 switch (ep->direction) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100503 case IN: tds[i].token = UHCI_IN; break;
504 case OUT: tds[i].token = UHCI_OUT; break;
505 case SETUP: tds[i].token = UHCI_SETUP; break;
Patrick Georgicfaa0812010-06-11 14:25:40 +0000506 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100507 tds[i].token |= ep->dev->address << TD_DEVADDR_SHIFT |
508 (ep->endpoint & 0xf) << TD_EP_SHIFT |
509 maxlen (reqsize) << TD_MAXLEN_SHIFT |
510 (ep->toggle & 1) << TD_TOGGLE_SHIFT;
Patrick Georgi4727c072008-10-16 19:20:51 +0000511 tds[i].bufptr = virt_to_phys (data);
Patrick Georgib0b4a522011-11-24 11:55:46 +0100512 tds[i].ctrlsts = (0 << TD_COUNTER_SHIFT) |
Nico Hubercefec0e2012-05-16 15:04:27 +0200513 (ep->dev->speed?TD_LOWSPEED:0) |
Patrick Georgib0b4a522011-11-24 11:55:46 +0100514 TD_STATUS_ACTIVE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000515 ep->toggle ^= 1;
516 data += reqsize;
517 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100518 tds[reqcount - 1].ptr = 0 | TD_TERMINATE;
Nico Huberce407e42012-11-20 17:27:46 +0100519
520 /* insert QH into framelist */
521 uhci_t *const uhcic = UHCI_INST(ep->dev->controller);
522 const u32 def_ptr = virt_to_phys(uhcic->qh_prei) | FLISTP_QH;
523 int nothing_placed = 1;
524 qh->headlinkptr = def_ptr;
525 for (i = 0; i < 1024; i += reqtiming) {
526 /* advance to the next free position */
527 while ((i < 1024) && (uhcic->framelistptr[i] != def_ptr)) ++i;
528 if (i < 1024) {
529 uhcic->framelistptr[i] = virt_to_phys(qh) | FLISTP_QH;
530 nothing_placed = 0;
531 }
Patrick Georgi4727c072008-10-16 19:20:51 +0000532 }
Nico Huberce407e42012-11-20 17:27:46 +0100533 if (nothing_placed) {
Dave Frodin6bf11cf2012-12-11 13:08:07 -0700534 usb_debug("Error: Failed to place UHCI interrupt queue "
Nico Huberce407e42012-11-20 17:27:46 +0100535 "head into framelist: no space left\n");
536 uhci_destroy_intr_queue(ep, q);
537 return NULL;
538 }
539
Patrick Georgi4727c072008-10-16 19:20:51 +0000540 return q;
541}
542
543/* remove queue from device schedule, dropping all data that came in */
544static void
545uhci_destroy_intr_queue (endpoint_t *ep, void *q_)
546{
Nico Huberce407e42012-11-20 17:27:46 +0100547 intr_q *const q = (intr_q*)q_;
548
549 /* remove QH from framelist */
550 uhci_t *const uhcic = UHCI_INST(ep->dev->controller);
551 const u32 qh_ptr = virt_to_phys(q->qh) | FLISTP_QH;
552 const u32 def_ptr = virt_to_phys(uhcic->qh_prei) | FLISTP_QH;
Patrick Georgi4727c072008-10-16 19:20:51 +0000553 int i;
Nico Huberce407e42012-11-20 17:27:46 +0100554 for (i = 0; i < 1024; ++i) {
555 if (uhcic->framelistptr[i] == qh_ptr)
556 uhcic->framelistptr[i] = def_ptr;
Patrick Georgi4727c072008-10-16 19:20:51 +0000557 }
Nico Huberce407e42012-11-20 17:27:46 +0100558
Patrick Georgi4727c072008-10-16 19:20:51 +0000559 free(q->data);
560 free(q->tds);
561 free(q->qh);
562 free(q);
563}
564
565/* read one intr-packet from queue, if available. extend the queue for new input.
566 return NULL if nothing new available.
567 Recommended use: while (data=poll_intr_queue(q)) process(data);
568 */
569static u8*
570uhci_poll_intr_queue (void *q_)
571{
572 intr_q *q = (intr_q*)q_;
Patrick Georgib0b4a522011-11-24 11:55:46 +0100573 if ((q->tds[q->lastread].ctrlsts & TD_STATUS_ACTIVE) == 0) {
Patrick Georgi4727c072008-10-16 19:20:51 +0000574 int current = q->lastread;
575 int previous;
576 if (q->lastread == 0) {
577 previous = q->total - 1;
578 } else {
579 previous = q->lastread - 1;
580 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100581 q->tds[previous].ctrlsts &= ~TD_STATUS_MASK;
582 q->tds[previous].ptr = 0 | TD_TERMINATE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000583 if (q->last_td != &q->tds[previous]) {
Patrick Georgib0b4a522011-11-24 11:55:46 +0100584 q->last_td->ptr = virt_to_phys(&q->tds[previous]) & ~TD_TERMINATE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000585 q->last_td = &q->tds[previous];
586 }
Patrick Georgib0b4a522011-11-24 11:55:46 +0100587 q->tds[previous].ctrlsts |= TD_STATUS_ACTIVE;
Patrick Georgi4727c072008-10-16 19:20:51 +0000588 q->lastread = (q->lastread + 1) % q->total;
Nico Huberbe58fee2012-11-22 11:12:13 +0100589 if (!(q->tds[current].ctrlsts & TD_STATUS_MASK))
590 return &q->data[current*q->reqsize];
Patrick Georgi4727c072008-10-16 19:20:51 +0000591 }
Nico Huber8c4d2f32012-11-20 17:49:00 +0100592 /* reset queue if we fully processed it after underrun */
593 else if (q->qh->elementlinkptr & FLISTP_TERMINATE) {
594 usb_debug("resetting underrun uhci interrupt queue.\n");
595 q->qh->elementlinkptr = virt_to_phys(q->tds + q->lastread);
596 }
Patrick Georgi4727c072008-10-16 19:20:51 +0000597 return NULL;
598}
599
Patrick Georgid21f68b2008-09-02 16:06:22 +0000600void
601uhci_reg_write32 (hci_t *ctrl, usbreg reg, u32 value)
602{
603 outl (value, ctrl->reg_base + reg);
604}
605
606u32
607uhci_reg_read32 (hci_t *ctrl, usbreg reg)
608{
609 return inl (ctrl->reg_base + reg);
610}
611
612void
613uhci_reg_write16 (hci_t *ctrl, usbreg reg, u16 value)
614{
615 outw (value, ctrl->reg_base + reg);
616}
617
618u16
619uhci_reg_read16 (hci_t *ctrl, usbreg reg)
620{
621 return inw (ctrl->reg_base + reg);
622}
623
624void
625uhci_reg_write8 (hci_t *ctrl, usbreg reg, u8 value)
626{
627 outb (value, ctrl->reg_base + reg);
628}
629
630u8
631uhci_reg_read8 (hci_t *ctrl, usbreg reg)
632{
633 return inb (ctrl->reg_base + reg);
634}