Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008-2010 coresystems GmbH |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 5 | * |
| 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 Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 30 | //#define USB_DEBUG |
| 31 | |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 32 | #include <arch/virtual.h> |
Jordan Crouse | 29061a5 | 2008-09-11 17:29:00 +0000 | [diff] [blame] | 33 | #include <usb/usb.h> |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 34 | #include "uhci.h" |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 35 | #include "uhci_private.h" |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 36 | |
| 37 | static void uhci_start (hci_t *controller); |
| 38 | static void uhci_stop (hci_t *controller); |
| 39 | static void uhci_reset (hci_t *controller); |
| 40 | static void uhci_shutdown (hci_t *controller); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 41 | static int uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize); |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 42 | static int uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 43 | int dalen, u8 *data); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 44 | static void* uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming); |
| 45 | static void uhci_destroy_intr_queue (endpoint_t *ep, void *queue); |
| 46 | static u8* uhci_poll_intr_queue (void *queue); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 47 | |
| 48 | #if 0 |
| 49 | /* dump uhci */ |
| 50 | static void |
| 51 | uhci_dump (hci_t *controller) |
| 52 | { |
| 53 | printf ("dump:\nUSBCMD: %x\n", uhci_reg_read16 (controller, USBCMD)); |
| 54 | printf ("USBSTS: %x\n", uhci_reg_read16 (controller, USBSTS)); |
| 55 | printf ("USBINTR: %x\n", uhci_reg_read16 (controller, USBINTR)); |
| 56 | printf ("FRNUM: %x\n", uhci_reg_read16 (controller, FRNUM)); |
| 57 | printf ("FLBASEADD: %x\n", uhci_reg_read32 (controller, FLBASEADD)); |
| 58 | printf ("SOFMOD: %x\n", uhci_reg_read8 (controller, SOFMOD)); |
| 59 | printf ("PORTSC1: %x\n", uhci_reg_read16 (controller, PORTSC1)); |
| 60 | printf ("PORTSC2: %x\n", uhci_reg_read16 (controller, PORTSC2)); |
| 61 | } |
| 62 | #endif |
| 63 | |
| 64 | static void |
| 65 | td_dump (td_t *td) |
| 66 | { |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 67 | char td_value[3]; |
| 68 | char *td_type; |
| 69 | switch (td->pid) { |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 70 | case UHCI_SETUP: |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 71 | td_type="SETUP"; |
| 72 | break; |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 73 | case UHCI_IN: |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 74 | td_type="IN"; |
| 75 | break; |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 76 | case UHCI_OUT: |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 77 | td_type="OUT"; |
| 78 | break; |
| 79 | default: |
| 80 | sprintf(td_value, "%x", td->pid); |
| 81 | td_type=td_value; |
| 82 | } |
| 83 | printf ("%s packet (at %lx) to %x.%x failed\n", td_type, |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 84 | virt_to_phys (td), td->dev_addr, td->endp); |
| 85 | printf ("td (counter at %x) returns: ", td->counter); |
| 86 | printf (" bitstuff err: %x, ", td->status_bitstuff_err); |
| 87 | printf (" CRC err: %x, ", td->status_crc_err); |
| 88 | printf (" NAK rcvd: %x, ", td->status_nakrcvd); |
| 89 | printf (" Babble: %x, ", td->status_babble); |
| 90 | printf (" Data Buffer err: %x, ", td->status_databuf_err); |
| 91 | printf (" Stalled: %x, ", td->status_stalled); |
| 92 | printf (" Active: %x\n", td->status_active); |
| 93 | if (td->status_babble) |
| 94 | printf (" Babble because of %s\n", |
| 95 | td->status_bitstuff_err ? "host" : "device"); |
| 96 | if (td->status_active) |
| 97 | printf (" still active - timeout?\n"); |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | uhci_reset (hci_t *controller) |
| 102 | { |
| 103 | /* reset */ |
| 104 | uhci_reg_write16 (controller, USBCMD, 4); |
| 105 | mdelay (50); |
| 106 | uhci_reg_write16 (controller, USBCMD, 0); |
| 107 | mdelay (10); |
| 108 | uhci_reg_write16 (controller, USBCMD, 2); |
| 109 | while ((uhci_reg_read16 (controller, USBCMD) & 2) != 0) |
| 110 | mdelay (1); |
| 111 | |
| 112 | uhci_reg_write32 (controller, FLBASEADD, |
| 113 | (u32) virt_to_phys (UHCI_INST (controller)-> |
| 114 | framelistptr)); |
| 115 | //printf ("framelist at %p\n",UHCI_INST(controller)->framelistptr); |
| 116 | |
| 117 | /* disable irqs */ |
| 118 | uhci_reg_write16 (controller, USBINTR, 0); |
| 119 | |
| 120 | /* reset framelist index */ |
| 121 | uhci_reg_write16 (controller, FRNUM, 0); |
| 122 | |
| 123 | uhci_reg_mask16 (controller, USBCMD, ~0, 0xc0); // max packets, configure flag |
| 124 | |
| 125 | uhci_start (controller); |
| 126 | } |
| 127 | |
| 128 | hci_t * |
| 129 | uhci_init (pcidev_t addr) |
| 130 | { |
| 131 | int i; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 132 | u16 reg16; |
| 133 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 134 | hci_t *controller = new_controller (); |
| 135 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 136 | if (!controller) |
| 137 | usb_fatal("Could not create USB controller instance.\n"); |
| 138 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 139 | controller->instance = malloc (sizeof (uhci_t)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 140 | if(!controller->instance) |
| 141 | usb_fatal("Not enough memory creating USB controller instance.\n"); |
| 142 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 143 | controller->start = uhci_start; |
| 144 | controller->stop = uhci_stop; |
| 145 | controller->reset = uhci_reset; |
| 146 | controller->shutdown = uhci_shutdown; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 147 | controller->bulk = uhci_bulk; |
| 148 | controller->control = uhci_control; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 149 | controller->create_intr_queue = uhci_create_intr_queue; |
| 150 | controller->destroy_intr_queue = uhci_destroy_intr_queue; |
| 151 | controller->poll_intr_queue = uhci_poll_intr_queue; |
Stefan Reinauer | 219cece | 2009-07-18 15:17:40 +0000 | [diff] [blame] | 152 | for (i = 0; i < 128; i++) { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 153 | controller->devices[i] = 0; |
| 154 | } |
| 155 | init_device_entry (controller, 0); |
| 156 | UHCI_INST (controller)->roothub = controller->devices[0]; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 157 | |
| 158 | controller->bus_address = addr; |
| 159 | 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 */ |
| 160 | |
| 161 | /* kill legacy support handler */ |
| 162 | uhci_stop (controller); |
| 163 | mdelay (1); |
| 164 | uhci_reg_write16 (controller, USBSTS, 0x3f); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 165 | reg16 = pci_read_config16(controller->bus_address, 0xc0); |
| 166 | reg16 &= 0xdf80; |
| 167 | pci_write_config16 (controller->bus_address, 0xc0, reg16); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 168 | |
| 169 | UHCI_INST (controller)->framelistptr = memalign (0x1000, 1024 * sizeof (flistp_t *)); /* 4kb aligned to 4kb */ |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 170 | if (! UHCI_INST (controller)->framelistptr) |
| 171 | usb_fatal("Not enough memory for USB frame list pointer.\n"); |
| 172 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 173 | memset (UHCI_INST (controller)->framelistptr, 0, |
| 174 | 1024 * sizeof (flistp_t)); |
| 175 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 176 | /* According to the *BSD UHCI code, this one is needed on some |
| 177 | PIIX chips, because otherwise they misbehave. It must be |
| 178 | added to the last chain. |
| 179 | |
| 180 | FIXME: this leaks, if the driver should ever be reinited |
| 181 | for some reason. Not a problem now. |
| 182 | */ |
| 183 | td_t *antiberserk = memalign(16, sizeof(td_t)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 184 | if (!antiberserk) |
| 185 | usb_fatal("Not enough memory for chipset workaround.\n"); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 186 | memset(antiberserk, 0, sizeof(td_t)); |
| 187 | |
| 188 | UHCI_INST (controller)->qh_prei = memalign (16, sizeof (qh_t)); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 189 | UHCI_INST (controller)->qh_intr = memalign (16, sizeof (qh_t)); |
| 190 | UHCI_INST (controller)->qh_data = memalign (16, sizeof (qh_t)); |
| 191 | UHCI_INST (controller)->qh_last = memalign (16, sizeof (qh_t)); |
| 192 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 193 | if (! UHCI_INST (controller)->qh_prei || |
| 194 | ! UHCI_INST (controller)->qh_intr || |
| 195 | ! UHCI_INST (controller)->qh_data || |
| 196 | ! UHCI_INST (controller)->qh_last) |
| 197 | usb_fatal ("Not enough memory for USB controller queues.\n"); |
| 198 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 199 | UHCI_INST (controller)->qh_prei->headlinkptr.ptr = |
| 200 | virt_to_phys (UHCI_INST (controller)->qh_intr); |
| 201 | UHCI_INST (controller)->qh_prei->headlinkptr.queue_head = 1; |
| 202 | UHCI_INST (controller)->qh_prei->elementlinkptr.ptr = 0; |
| 203 | UHCI_INST (controller)->qh_prei->elementlinkptr.terminate = 1; |
| 204 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 205 | UHCI_INST (controller)->qh_intr->headlinkptr.ptr = |
| 206 | virt_to_phys (UHCI_INST (controller)->qh_data); |
| 207 | UHCI_INST (controller)->qh_intr->headlinkptr.queue_head = 1; |
| 208 | UHCI_INST (controller)->qh_intr->elementlinkptr.ptr = 0; |
| 209 | UHCI_INST (controller)->qh_intr->elementlinkptr.terminate = 1; |
| 210 | |
| 211 | UHCI_INST (controller)->qh_data->headlinkptr.ptr = |
| 212 | virt_to_phys (UHCI_INST (controller)->qh_last); |
| 213 | UHCI_INST (controller)->qh_data->headlinkptr.queue_head = 1; |
| 214 | UHCI_INST (controller)->qh_data->elementlinkptr.ptr = 0; |
| 215 | UHCI_INST (controller)->qh_data->elementlinkptr.terminate = 1; |
| 216 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 217 | UHCI_INST (controller)->qh_last->headlinkptr.ptr = virt_to_phys (UHCI_INST (controller)->qh_data); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 218 | UHCI_INST (controller)->qh_last->headlinkptr.terminate = 1; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 219 | UHCI_INST (controller)->qh_last->elementlinkptr.ptr = virt_to_phys (antiberserk); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 220 | UHCI_INST (controller)->qh_last->elementlinkptr.terminate = 1; |
| 221 | |
| 222 | for (i = 0; i < 1024; i++) { |
| 223 | UHCI_INST (controller)->framelistptr[i].ptr = |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 224 | virt_to_phys (UHCI_INST (controller)->qh_prei); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 225 | UHCI_INST (controller)->framelistptr[i].terminate = 0; |
| 226 | UHCI_INST (controller)->framelistptr[i].queue_head = 1; |
| 227 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 228 | controller->devices[0]->controller = controller; |
| 229 | controller->devices[0]->init = uhci_rh_init; |
| 230 | controller->devices[0]->init (controller->devices[0]); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 231 | uhci_reset (controller); |
| 232 | return controller; |
| 233 | } |
| 234 | |
| 235 | static void |
| 236 | uhci_shutdown (hci_t *controller) |
| 237 | { |
| 238 | if (controller == 0) |
| 239 | return; |
| 240 | detach_controller (controller); |
| 241 | UHCI_INST (controller)->roothub->destroy (UHCI_INST (controller)-> |
| 242 | roothub); |
| 243 | uhci_reg_mask16 (controller, USBCMD, 0, 0); // stop work |
| 244 | free (UHCI_INST (controller)->framelistptr); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 245 | free (UHCI_INST (controller)->qh_prei); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 246 | free (UHCI_INST (controller)->qh_intr); |
| 247 | free (UHCI_INST (controller)->qh_data); |
| 248 | free (UHCI_INST (controller)->qh_last); |
| 249 | free (UHCI_INST (controller)); |
| 250 | free (controller); |
| 251 | } |
| 252 | |
| 253 | static void |
| 254 | uhci_start (hci_t *controller) |
| 255 | { |
| 256 | uhci_reg_mask16 (controller, USBCMD, ~0, 1); // start work on schedule |
| 257 | } |
| 258 | |
| 259 | static void |
| 260 | uhci_stop (hci_t *controller) |
| 261 | { |
| 262 | uhci_reg_mask16 (controller, USBCMD, ~1, 0); // stop work on schedule |
| 263 | } |
| 264 | |
| 265 | #define GET_TD(x) ((void*)(((unsigned int)(x))&~0xf)) |
| 266 | |
| 267 | static td_t * |
| 268 | wait_for_completed_qh (hci_t *controller, qh_t *qh) |
| 269 | { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 270 | int timeout = 1000000; /* max 30 ms. */ |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 271 | void *current = GET_TD (qh->elementlinkptr.ptr); |
| 272 | while ((qh->elementlinkptr.terminate == 0) && (timeout-- > 0)) { |
| 273 | if (current != GET_TD (qh->elementlinkptr.ptr)) { |
| 274 | current = GET_TD (qh->elementlinkptr.ptr); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 275 | timeout = 1000000; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 276 | } |
| 277 | uhci_reg_mask16 (controller, USBSTS, ~0, 0); // clear resettable registers |
| 278 | udelay (30); |
| 279 | } |
| 280 | return (GET_TD (qh->elementlinkptr.ptr) == |
| 281 | 0) ? 0 : GET_TD (phys_to_virt (qh->elementlinkptr.ptr)); |
| 282 | } |
| 283 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 284 | static int |
| 285 | maxlen (int size) |
| 286 | { |
| 287 | return (size - 1) & 0x7ff; |
| 288 | } |
| 289 | |
| 290 | static int |
| 291 | min (int a, int b) |
| 292 | { |
| 293 | if (a < b) |
| 294 | return a; |
| 295 | else |
| 296 | return b; |
| 297 | } |
| 298 | |
| 299 | static int |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 300 | uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, int dalen, |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 301 | unsigned char *data) |
| 302 | { |
| 303 | int endp = 0; /* this is control: always 0 */ |
| 304 | int mlen = dev->endpoints[0].maxpacketsize; |
| 305 | int count = (2 + (dalen + mlen - 1) / mlen); |
| 306 | unsigned short req = ((unsigned short *) devreq)[0]; |
| 307 | int i; |
| 308 | td_t *tds = memalign (16, sizeof (td_t) * count); |
| 309 | memset (tds, 0, sizeof (td_t) * count); |
| 310 | count--; /* to compensate for 0-indexed array */ |
| 311 | for (i = 0; i < count; i++) { |
| 312 | tds[i].ptr = virt_to_phys (&tds[i + 1]); |
| 313 | tds[i].depth_first = 1; |
| 314 | tds[i].terminate = 0; |
| 315 | } |
| 316 | tds[count].ptr = 0; |
| 317 | tds[count].depth_first = 1; |
| 318 | tds[count].terminate = 1; |
| 319 | |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 320 | tds[0].pid = UHCI_SETUP; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 321 | tds[0].dev_addr = dev->address; |
| 322 | tds[0].endp = endp; |
| 323 | tds[0].maxlen = maxlen (drlen); |
| 324 | tds[0].counter = 3; |
| 325 | tds[0].data_toggle = 0; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 326 | tds[0].lowspeed = dev->speed; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 327 | tds[0].bufptr = virt_to_phys (devreq); |
| 328 | tds[0].status_active = 1; |
| 329 | |
| 330 | int toggle = 1; |
| 331 | for (i = 1; i < count; i++) { |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 332 | switch (dir) { |
| 333 | case SETUP: tds[i].pid = UHCI_SETUP; break; |
| 334 | case IN: tds[i].pid = UHCI_IN; break; |
| 335 | case OUT: tds[i].pid = UHCI_OUT; break; |
| 336 | } |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 337 | tds[i].dev_addr = dev->address; |
| 338 | tds[i].endp = endp; |
| 339 | tds[i].maxlen = maxlen (min (mlen, dalen)); |
| 340 | tds[i].counter = 3; |
| 341 | tds[i].data_toggle = toggle; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 342 | tds[i].lowspeed = dev->speed; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 343 | tds[i].bufptr = virt_to_phys (data); |
| 344 | tds[i].status_active = 1; |
| 345 | toggle ^= 1; |
| 346 | dalen -= mlen; |
| 347 | data += mlen; |
| 348 | } |
| 349 | |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame^] | 350 | tds[count].pid = (dir == OUT) ? UHCI_IN : UHCI_OUT; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 351 | tds[count].dev_addr = dev->address; |
| 352 | tds[count].endp = endp; |
| 353 | tds[count].maxlen = maxlen (0); |
| 354 | tds[count].counter = 0; /* as per linux 2.4.10 */ |
| 355 | tds[count].data_toggle = 1; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 356 | tds[count].lowspeed = dev->speed; |
| 357 | tds[count].bufptr = 0; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 358 | tds[count].status_active = 1; |
| 359 | UHCI_INST (dev->controller)->qh_data->elementlinkptr.ptr = |
| 360 | virt_to_phys (tds); |
| 361 | UHCI_INST (dev->controller)->qh_data->elementlinkptr.queue_head = 0; |
| 362 | UHCI_INST (dev->controller)->qh_data->elementlinkptr.terminate = 0; |
| 363 | td_t *td = wait_for_completed_qh (dev->controller, |
| 364 | UHCI_INST (dev->controller)-> |
| 365 | qh_data); |
| 366 | int result; |
| 367 | if (td == 0) { |
| 368 | result = 0; |
| 369 | } else { |
| 370 | printf ("control packet, req %x\n", req); |
| 371 | td_dump (td); |
| 372 | result = 1; |
| 373 | } |
| 374 | free (tds); |
| 375 | return result; |
| 376 | } |
| 377 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 378 | static td_t * |
| 379 | create_schedule (int numpackets) |
| 380 | { |
| 381 | if (numpackets == 0) |
| 382 | return 0; |
| 383 | td_t *tds = memalign (16, sizeof (td_t) * numpackets); |
| 384 | memset (tds, 0, sizeof (td_t) * numpackets); |
| 385 | int i; |
| 386 | for (i = 0; i < numpackets; i++) { |
| 387 | tds[i].ptr = virt_to_phys (&tds[i + 1]); |
| 388 | tds[i].terminate = 0; |
| 389 | tds[i].queue_head = 0; |
| 390 | tds[i].depth_first = 1; |
| 391 | } |
| 392 | tds[numpackets - 1].ptr = 0; |
| 393 | tds[numpackets - 1].terminate = 1; |
| 394 | tds[numpackets - 1].queue_head = 0; |
| 395 | tds[numpackets - 1].depth_first = 0; |
| 396 | return tds; |
| 397 | } |
| 398 | |
| 399 | static void |
| 400 | fill_schedule (td_t *td, endpoint_t *ep, int length, unsigned char *data, |
| 401 | int *toggle) |
| 402 | { |
| 403 | td->pid = ep->direction; |
| 404 | td->dev_addr = ep->dev->address; |
| 405 | td->endp = ep->endpoint & 0xf; |
| 406 | td->maxlen = maxlen (length); |
| 407 | if (ep->direction == SETUP) |
| 408 | td->counter = 3; |
| 409 | else |
| 410 | td->counter = 0; |
| 411 | td->data_toggle = *toggle & 1; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 412 | td->lowspeed = ep->dev->speed; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 413 | td->bufptr = virt_to_phys (data); |
| 414 | |
| 415 | td->status_active = 1; |
| 416 | *toggle ^= 1; |
| 417 | } |
| 418 | |
| 419 | static int |
| 420 | run_schedule (usbdev_t *dev, td_t *td) |
| 421 | { |
| 422 | UHCI_INST (dev->controller)->qh_data->elementlinkptr.ptr = |
| 423 | virt_to_phys (td); |
| 424 | UHCI_INST (dev->controller)->qh_data->elementlinkptr.queue_head = 0; |
| 425 | UHCI_INST (dev->controller)->qh_data->elementlinkptr.terminate = 0; |
| 426 | td = wait_for_completed_qh (dev->controller, |
| 427 | UHCI_INST (dev->controller)->qh_data); |
| 428 | if (td == 0) { |
| 429 | return 0; |
| 430 | } else { |
| 431 | td_dump (td); |
| 432 | return 1; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* finalize == 1: if data is of packet aligned size, add a zero length packet */ |
| 437 | static int |
| 438 | uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize) |
| 439 | { |
| 440 | int maxpsize = ep->maxpacketsize; |
| 441 | if (maxpsize == 0) |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 442 | usb_fatal ("MaxPacketSize == 0!!!"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 443 | int numpackets = (size + maxpsize - 1 + finalize) / maxpsize; |
| 444 | if (numpackets == 0) |
| 445 | return 0; |
| 446 | td_t *tds = create_schedule (numpackets); |
| 447 | int i = 0, toggle = ep->toggle; |
| 448 | while ((size > 0) || ((size == 0) && (finalize != 0))) { |
| 449 | fill_schedule (&tds[i], ep, min (size, maxpsize), data, |
| 450 | &toggle); |
| 451 | i++; |
| 452 | data += maxpsize; |
| 453 | size -= maxpsize; |
| 454 | } |
| 455 | if (run_schedule (ep->dev, tds) == 1) { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 456 | debug("Stalled. Trying to clean up.\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 457 | clear_stall (ep); |
| 458 | free (tds); |
| 459 | return 1; |
| 460 | } |
| 461 | ep->toggle = toggle; |
| 462 | free (tds); |
| 463 | return 0; |
| 464 | } |
| 465 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 466 | typedef struct { |
| 467 | qh_t *qh; |
| 468 | td_t *tds; |
| 469 | td_t *last_td; |
| 470 | u8 *data; |
| 471 | int lastread; |
| 472 | int total; |
| 473 | int reqsize; |
| 474 | } intr_q; |
| 475 | |
| 476 | /* create and hook-up an intr queue into device schedule */ |
| 477 | static void* |
| 478 | uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming) |
| 479 | { |
| 480 | u8 *data = malloc(reqsize*reqcount); |
| 481 | td_t *tds = memalign(16, sizeof(td_t) * reqcount); |
| 482 | qh_t *qh = memalign(16, sizeof(qh_t)); |
| 483 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 484 | if (!data || !tds || !qh) |
| 485 | usb_fatal ("Not enough memory to create USB intr queue prerequisites.\n"); |
| 486 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 487 | qh->elementlinkptr.ptr = virt_to_phys(tds); |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 488 | qh->elementlinkptr.queue_head = 0; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 489 | qh->elementlinkptr.terminate = 0; |
| 490 | |
| 491 | intr_q *q = malloc(sizeof(intr_q)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 492 | if (!q) |
| 493 | usb_fatal ("Not enough memory to create USB intr queue.\n"); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 494 | q->qh = qh; |
| 495 | q->tds = tds; |
| 496 | q->data = data; |
| 497 | q->lastread = 0; |
| 498 | q->total = reqcount; |
| 499 | q->reqsize = reqsize; |
| 500 | q->last_td = &tds[reqcount - 1]; |
| 501 | |
| 502 | memset (tds, 0, sizeof (td_t) * reqcount); |
| 503 | int i; |
| 504 | for (i = 0; i < reqcount; i++) { |
| 505 | tds[i].ptr = virt_to_phys (&tds[i + 1]); |
| 506 | tds[i].terminate = 0; |
| 507 | tds[i].queue_head = 0; |
| 508 | tds[i].depth_first = 0; |
| 509 | |
| 510 | tds[i].pid = ep->direction; |
| 511 | tds[i].dev_addr = ep->dev->address; |
| 512 | tds[i].endp = ep->endpoint & 0xf; |
| 513 | tds[i].maxlen = maxlen (reqsize); |
| 514 | tds[i].counter = 0; |
| 515 | tds[i].data_toggle = ep->toggle & 1; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 516 | tds[i].lowspeed = ep->dev->speed; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 517 | tds[i].bufptr = virt_to_phys (data); |
| 518 | tds[i].status_active = 1; |
| 519 | ep->toggle ^= 1; |
| 520 | data += reqsize; |
| 521 | } |
| 522 | tds[reqcount - 1].ptr = 0; |
| 523 | tds[reqcount - 1].terminate = 1; |
| 524 | tds[reqcount - 1].queue_head = 0; |
| 525 | tds[reqcount - 1].depth_first = 0; |
| 526 | for (i = reqtiming; i < 1024; i += reqtiming) { |
| 527 | /* FIXME: wrap in another qh, one for each occurance of the qh in the framelist */ |
| 528 | qh->headlinkptr.ptr = UHCI_INST (ep->dev->controller)->framelistptr[i].ptr; |
| 529 | qh->headlinkptr.terminate = 0; |
| 530 | UHCI_INST (ep->dev->controller)->framelistptr[i].ptr = virt_to_phys(qh); |
| 531 | UHCI_INST (ep->dev->controller)->framelistptr[i].terminate = 0; |
| 532 | UHCI_INST (ep->dev->controller)->framelistptr[i].queue_head = 1; |
| 533 | } |
| 534 | return q; |
| 535 | } |
| 536 | |
| 537 | /* remove queue from device schedule, dropping all data that came in */ |
| 538 | static void |
| 539 | uhci_destroy_intr_queue (endpoint_t *ep, void *q_) |
| 540 | { |
| 541 | intr_q *q = (intr_q*)q_; |
| 542 | u32 val = virt_to_phys (q->qh); |
| 543 | u32 end = virt_to_phys (UHCI_INST (ep->dev->controller)->qh_intr); |
| 544 | int i; |
| 545 | for (i=0; i<1024; i++) { |
| 546 | u32 oldptr = 0; |
| 547 | u32 ptr = UHCI_INST (ep->dev->controller)->framelistptr[i].ptr; |
| 548 | while (ptr != end) { |
| 549 | if (((qh_t*)phys_to_virt(ptr))->elementlinkptr.ptr == val) { |
| 550 | ((qh_t*)phys_to_virt(oldptr))->headlinkptr.ptr = ((qh_t*)phys_to_virt(ptr))->headlinkptr.ptr; |
| 551 | free(phys_to_virt(ptr)); |
| 552 | break; |
| 553 | } |
| 554 | oldptr = ptr; |
| 555 | ptr = ((qh_t*)phys_to_virt(ptr))->headlinkptr.ptr; |
| 556 | } |
| 557 | } |
| 558 | free(q->data); |
| 559 | free(q->tds); |
| 560 | free(q->qh); |
| 561 | free(q); |
| 562 | } |
| 563 | |
| 564 | /* read one intr-packet from queue, if available. extend the queue for new input. |
| 565 | return NULL if nothing new available. |
| 566 | Recommended use: while (data=poll_intr_queue(q)) process(data); |
| 567 | */ |
| 568 | static u8* |
| 569 | uhci_poll_intr_queue (void *q_) |
| 570 | { |
| 571 | intr_q *q = (intr_q*)q_; |
| 572 | if (q->tds[q->lastread].status_active == 0) { |
| 573 | /* FIXME: handle errors */ |
| 574 | 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 | } |
| 581 | q->tds[previous].status = 0; |
| 582 | q->tds[previous].ptr = 0; |
| 583 | q->tds[previous].terminate = 1; |
| 584 | if (q->last_td != &q->tds[previous]) { |
| 585 | q->last_td->ptr = virt_to_phys(&q->tds[previous]); |
| 586 | q->last_td->terminate = 0; |
| 587 | q->last_td = &q->tds[previous]; |
| 588 | } |
| 589 | q->tds[previous].status_active = 1; |
| 590 | q->lastread = (q->lastread + 1) % q->total; |
| 591 | return &q->data[current*q->reqsize]; |
| 592 | } |
| 593 | return NULL; |
| 594 | } |
| 595 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 596 | void |
| 597 | uhci_reg_write32 (hci_t *ctrl, usbreg reg, u32 value) |
| 598 | { |
| 599 | outl (value, ctrl->reg_base + reg); |
| 600 | } |
| 601 | |
| 602 | u32 |
| 603 | uhci_reg_read32 (hci_t *ctrl, usbreg reg) |
| 604 | { |
| 605 | return inl (ctrl->reg_base + reg); |
| 606 | } |
| 607 | |
| 608 | void |
| 609 | uhci_reg_write16 (hci_t *ctrl, usbreg reg, u16 value) |
| 610 | { |
| 611 | outw (value, ctrl->reg_base + reg); |
| 612 | } |
| 613 | |
| 614 | u16 |
| 615 | uhci_reg_read16 (hci_t *ctrl, usbreg reg) |
| 616 | { |
| 617 | return inw (ctrl->reg_base + reg); |
| 618 | } |
| 619 | |
| 620 | void |
| 621 | uhci_reg_write8 (hci_t *ctrl, usbreg reg, u8 value) |
| 622 | { |
| 623 | outb (value, ctrl->reg_base + reg); |
| 624 | } |
| 625 | |
| 626 | u8 |
| 627 | uhci_reg_read8 (hci_t *ctrl, usbreg reg) |
| 628 | { |
| 629 | return inb (ctrl->reg_base + reg); |
| 630 | } |
| 631 | |
| 632 | void |
| 633 | uhci_reg_mask32 (hci_t *ctrl, usbreg reg, u32 andmask, u32 ormask) |
| 634 | { |
| 635 | uhci_reg_write32 (ctrl, reg, |
| 636 | (uhci_reg_read32 (ctrl, reg) & andmask) | ormask); |
| 637 | } |
| 638 | |
| 639 | void |
| 640 | uhci_reg_mask16 (hci_t *ctrl, usbreg reg, u16 andmask, u16 ormask) |
| 641 | { |
| 642 | uhci_reg_write16 (ctrl, reg, |
| 643 | (uhci_reg_read16 (ctrl, reg) & andmask) | ormask); |
| 644 | } |
| 645 | |
| 646 | void |
| 647 | uhci_reg_mask8 (hci_t *ctrl, usbreg reg, u8 andmask, u8 ormask) |
| 648 | { |
| 649 | uhci_reg_write8 (ctrl, reg, |
| 650 | (uhci_reg_read8 (ctrl, reg) & andmask) | ormask); |
| 651 | } |