2009-06-10 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / bus / usb / uhci.c
blob84cd48dafb733bfd4d2094fe7a473d45614e4964
1 /* uhci.c - UHCI Support. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/dl.h>
21 #include <grub/mm.h>
22 #include <grub/misc.h>
23 #include <grub/usb.h>
24 #include <grub/usbtrans.h>
25 #include <grub/pci.h>
26 #include <grub/cpu/pci.h>
27 #include <grub/i386/io.h>
28 #include <grub/time.h>
30 #define GRUB_UHCI_IOMASK (0x7FF << 5)
32 typedef enum
34 GRUB_UHCI_REG_USBCMD = 0x00,
35 GRUB_UHCI_REG_FLBASEADD = 0x08,
36 GRUB_UHCI_REG_PORTSC1 = 0x10,
37 GRUB_UHCI_REG_PORTSC2 = 0x12
38 } grub_uhci_reg_t;
40 #define GRUB_UHCI_LINK_TERMINATE 1
41 #define GRUB_UHCI_LINK_QUEUE_HEAD 2
44 /* UHCI Queue Head. */
45 struct grub_uhci_qh
47 /* Queue head link pointer which points to the next queue head. */
48 grub_uint32_t linkptr;
50 /* Queue element link pointer which points to the first data object
51 within the queue. */
52 grub_uint32_t elinkptr;
54 /* Queue heads are aligned on 16 bytes, pad so a queue head is 16
55 bytes so we can store many in a 4K page. */
56 grub_uint8_t pad[8];
57 } __attribute__ ((packed));
59 /* UHCI Transfer Descriptor. */
60 struct grub_uhci_td
62 /* Pointer to the next TD in the list. */
63 grub_uint32_t linkptr;
65 /* Control and status bits. */
66 grub_uint32_t ctrl_status;
68 /* All information required to transfer the Token packet. */
69 grub_uint32_t token;
71 /* A pointer to the data buffer, UHCI requires this pointer to be 32
72 bits. */
73 grub_uint32_t buffer;
75 /* Another linkptr that is not overwritten by the Host Controller.
76 This is GRUB specific. */
77 grub_uint32_t linkptr2;
79 /* 3 additional 32 bits words reserved for the Host Controller Driver. */
80 grub_uint32_t data[3];
81 } __attribute__ ((packed));
83 typedef volatile struct grub_uhci_td *grub_uhci_td_t;
84 typedef volatile struct grub_uhci_qh *grub_uhci_qh_t;
86 struct grub_uhci
88 int iobase;
89 grub_uint32_t *framelist;
91 /* 256 Queue Heads. */
92 grub_uhci_qh_t qh;
94 /* 256 Transfer Descriptors. */
95 grub_uhci_td_t td;
97 /* Free Transfer Descriptors. */
98 grub_uhci_td_t tdfree;
100 struct grub_uhci *next;
103 static struct grub_uhci *uhci;
105 static grub_uint16_t
106 grub_uhci_readreg16 (struct grub_uhci *u, grub_uhci_reg_t reg)
108 return grub_inw (u->iobase + reg);
111 #if 0
112 static grub_uint32_t
113 grub_uhci_readreg32 (struct grub_uhci *u, grub_uhci_reg_t reg)
115 return grub_inl (u->iobase + reg);
117 #endif
119 static void
120 grub_uhci_writereg16 (struct grub_uhci *u,
121 grub_uhci_reg_t reg, grub_uint16_t val)
123 grub_outw (val, u->iobase + reg);
126 static void
127 grub_uhci_writereg32 (struct grub_uhci *u,
128 grub_uhci_reg_t reg, grub_uint32_t val)
130 grub_outl (val, u->iobase + reg);
133 static grub_err_t
134 grub_uhci_portstatus (grub_usb_controller_t dev,
135 unsigned int port, unsigned int enable);
138 /* Iterate over all PCI devices. Determine if a device is an UHCI
139 controller. If this is the case, initialize it. */
140 static int NESTED_FUNC_ATTR
141 grub_uhci_pci_iter (int bus, int device, int func,
142 grub_pci_id_t pciid __attribute__((unused)))
144 grub_uint32_t class_code;
145 grub_uint32_t class;
146 grub_uint32_t subclass;
147 grub_uint32_t interf;
148 grub_uint32_t base;
149 grub_uint32_t fp;
150 grub_pci_address_t addr;
151 struct grub_uhci *u;
152 int i;
154 addr = grub_pci_make_address (bus, device, func, 2);
155 class_code = grub_pci_read (addr) >> 8;
157 interf = class_code & 0xFF;
158 subclass = (class_code >> 8) & 0xFF;
159 class = class_code >> 16;
161 /* If this is not an UHCI controller, just return. */
162 if (class != 0x0c || subclass != 0x03 || interf != 0x00)
163 return 0;
165 /* Determine IO base address. */
166 addr = grub_pci_make_address (bus, device, func, 8);
167 base = grub_pci_read (addr);
168 /* Stop if there is no IO space base address defined. */
169 if (! (base & 1))
170 return 0;
172 /* Allocate memory for the controller and register it. */
173 u = grub_malloc (sizeof (*u));
174 if (! u)
175 return 1;
177 u->iobase = base & GRUB_UHCI_IOMASK;
178 u->framelist = 0;
179 u->qh = 0;
180 u->td = 0;
181 grub_dprintf ("uhci", "class=0x%02x 0x%02x interface 0x%02x base=0x%x\n",
182 class, subclass, interf, u->iobase);
184 /* Reserve a page for the frame list. */
185 u->framelist = grub_memalign (4096, 4096);
186 if (! u->framelist)
187 goto fail;
189 /* The framelist pointer of UHCI is only 32 bits, make sure this
190 code works on on 64 bits architectures. */
191 #if GRUB_CPU_SIZEOF_VOID_P == 8
192 if ((grub_uint64_t) u->framelist >> 32)
194 grub_error (GRUB_ERR_OUT_OF_MEMORY,
195 "allocated frame list memory not <4GB");
196 goto fail;
198 #endif
200 /* The QH pointer of UHCI is only 32 bits, make sure this
201 code works on on 64 bits architectures. */
202 u->qh = (grub_uhci_qh_t) grub_memalign (4096, 4096);
203 if (! u->qh)
204 goto fail;
206 #if GRUB_CPU_SIZEOF_VOID_P == 8
207 if ((grub_uint64_t) u->qh >> 32)
209 grub_error (GRUB_ERR_OUT_OF_MEMORY, "allocated QH memory not <4GB");
210 goto fail;
212 #endif
214 /* The TD pointer of UHCI is only 32 bits, make sure this
215 code works on on 64 bits architectures. */
216 u->td = (grub_uhci_td_t) grub_memalign (4096, 4096*2);
217 if (! u->td)
218 goto fail;
220 #if GRUB_CPU_SIZEOF_VOID_P == 8
221 if ((grub_uint64_t) u->td >> 32)
223 grub_error (GRUB_ERR_OUT_OF_MEMORY, "allocated TD memory not <4GB");
224 goto fail;
226 #endif
228 /* Link all Transfer Descriptors in a list of available Transfer
229 Descriptors. */
230 for (i = 0; i < 256; i++)
231 u->td[i].linkptr = (grub_uint32_t) &u->td[i + 1];
232 u->td[255 - 1].linkptr = 0;
233 u->tdfree = u->td;
235 /* Make sure UHCI is disabled! */
236 grub_uhci_writereg16 (u, GRUB_UHCI_REG_USBCMD, 0);
238 /* Setup the frame list pointers. Since no isochronous transfers
239 are and will be supported, they all point to the (same!) queue
240 head. */
241 fp = (grub_uint32_t) u->qh & (~15);
242 /* Mark this as a queue head. */
243 fp |= 2;
244 for (i = 0; i < 1024; i++)
245 u->framelist[i] = fp;
246 /* Program the framelist address into the UHCI controller. */
247 grub_uhci_writereg32 (u, GRUB_UHCI_REG_FLBASEADD,
248 (grub_uint32_t) u->framelist);
250 /* Make the Queue Heads point to each other. */
251 for (i = 0; i < 256; i++)
253 /* Point to the next QH. */
254 u->qh[i].linkptr = (grub_uint32_t) (&u->qh[i + 1]) & (~15);
256 /* This is a QH. */
257 u->qh[i].linkptr |= GRUB_UHCI_LINK_QUEUE_HEAD;
259 /* For the moment, do not point to a Transfer Descriptor. These
260 are set at transfer time, so just terminate it. */
261 u->qh[i].elinkptr = 1;
264 /* The last Queue Head should terminate. 256 are too many QHs so
265 just use 50. */
266 u->qh[50 - 1].linkptr = 1;
268 /* Enable UHCI again. */
269 grub_uhci_writereg16 (u, GRUB_UHCI_REG_USBCMD, 1 | (1 << 7));
271 /* UHCI is initialized and ready for transfers. */
272 grub_dprintf ("uhci", "UHCI initialized\n");
275 #if 0
277 int i;
278 for (i = 0; i < 10; i++)
280 grub_uint16_t frnum;
282 frnum = grub_uhci_readreg16 (u, 6);
283 grub_dprintf ("uhci", "Framenum=%d\n", frnum);
284 grub_millisleep (100);
287 #endif
289 /* Link to uhci now that initialisation is successful. */
290 u->next = uhci;
291 uhci = u;
293 return 0;
295 fail:
296 if (u)
298 grub_free ((void *) u->qh);
299 grub_free (u->framelist);
301 grub_free (u);
303 return 1;
306 static void
307 grub_uhci_inithw (void)
309 grub_pci_iterate (grub_uhci_pci_iter);
312 static grub_uhci_td_t
313 grub_alloc_td (struct grub_uhci *u)
315 grub_uhci_td_t ret;
317 /* Check if there is a Transfer Descriptor available. */
318 if (! u->tdfree)
319 return NULL;
321 ret = u->tdfree;
322 u->tdfree = (grub_uhci_td_t) u->tdfree->linkptr;
324 return ret;
327 static void
328 grub_free_td (struct grub_uhci *u, grub_uhci_td_t td)
330 td->linkptr = (grub_uint32_t) u->tdfree;
331 u->tdfree = td;
334 static void
335 grub_free_queue (struct grub_uhci *u, grub_uhci_td_t td)
337 /* Free the TDs in this queue. */
338 while (td)
340 grub_uhci_td_t tdprev;
342 /* Unlink the queue. */
343 tdprev = td;
344 td = (grub_uhci_td_t) td->linkptr2;
346 /* Free the TD. */
347 grub_free_td (u, tdprev);
351 static grub_uhci_qh_t
352 grub_alloc_qh (struct grub_uhci *u,
353 grub_transaction_type_t tr __attribute__((unused)))
355 int i;
356 grub_uhci_qh_t qh;
358 /* Look for a Queue Head for this transfer. Skip the first QH if
359 this is a Interrupt Transfer. */
360 #if 0
361 if (tr == GRUB_USB_TRANSACTION_TYPE_INTERRUPT)
362 i = 0;
363 else
364 #endif
365 i = 1;
367 for (; i < 255; i++)
369 if (u->qh[i].elinkptr & 1)
370 break;
372 qh = &u->qh[i];
373 if (! (qh->elinkptr & 1))
375 grub_error (GRUB_ERR_OUT_OF_MEMORY,
376 "no free queue heads available");
377 return NULL;
380 return qh;
383 static grub_uhci_td_t
384 grub_uhci_transaction (struct grub_uhci *u, unsigned int endp,
385 grub_transfer_type_t type, unsigned int addr,
386 unsigned int toggle, grub_size_t size,
387 char *data)
389 grub_uhci_td_t td;
390 static const unsigned int tf[] = { 0x69, 0xE1, 0x2D };
392 /* XXX: Check if data is <4GB. If it isn't, just copy stuff around.
393 This is only relevant for 64 bits architectures. */
395 /* Grab a free Transfer Descriptor and initialize it. */
396 td = grub_alloc_td (u);
397 if (! td)
399 grub_error (GRUB_ERR_OUT_OF_MEMORY,
400 "no transfer descriptors available for UHCI transfer");
401 return 0;
404 grub_dprintf ("uhci",
405 "transaction: endp=%d, type=%d, addr=%d, toggle=%d, size=%d data=%p td=%p\n",
406 endp, type, addr, toggle, size, data, td);
408 /* Don't point to any TD, just terminate. */
409 td->linkptr = 1;
411 /* Active! Only retry a transfer 3 times. */
412 td->ctrl_status = (1 << 23) | (3 << 27);
414 /* If zero bytes are transmitted, size is 0x7FF. Otherwise size is
415 size-1. */
416 if (size == 0)
417 size = 0x7FF;
418 else
419 size = size - 1;
421 /* Setup whatever is required for the token packet. */
422 td->token = ((size << 21) | (toggle << 19) | (endp << 15)
423 | (addr << 8) | tf[type]);
425 td->buffer = (grub_uint32_t) data;
427 return td;
430 static grub_usb_err_t
431 grub_uhci_transfer (grub_usb_controller_t dev,
432 grub_usb_transfer_t transfer)
434 struct grub_uhci *u = (struct grub_uhci *) dev->data;
435 grub_uhci_qh_t qh;
436 grub_uhci_td_t td;
437 grub_uhci_td_t td_first = NULL;
438 grub_uhci_td_t td_prev = NULL;
439 grub_usb_err_t err = GRUB_USB_ERR_NONE;
440 int i;
442 /* Allocate a queue head for the transfer queue. */
443 qh = grub_alloc_qh (u, GRUB_USB_TRANSACTION_TYPE_CONTROL);
444 if (! qh)
445 return grub_errno;
447 for (i = 0; i < transfer->transcnt; i++)
449 grub_usb_transaction_t tr = &transfer->transactions[i];
451 td = grub_uhci_transaction (u, transfer->endpoint, tr->pid,
452 transfer->devaddr, tr->toggle,
453 tr->size, tr->data);
454 if (! td)
456 /* Terminate and free. */
457 td_prev->linkptr2 = 0;
458 td_prev->linkptr = 1;
460 if (td_first)
461 grub_free_queue (u, td_first);
463 return GRUB_USB_ERR_INTERNAL;
466 if (! td_first)
467 td_first = td;
468 else
470 td_prev->linkptr2 = (grub_uint32_t) td;
471 td_prev->linkptr = (grub_uint32_t) td;
472 td_prev->linkptr |= 4;
474 td_prev = td;
476 td_prev->linkptr2 = 0;
477 td_prev->linkptr = 1;
479 grub_dprintf ("uhci", "setup transaction %d\n", transfer->type);
481 /* Link it into the queue and terminate. Now the transaction can
482 take place. */
483 qh->elinkptr = (grub_uint32_t) td_first;
485 grub_dprintf ("uhci", "initiate transaction\n");
487 /* Wait until either the transaction completed or an error
488 occurred. */
489 for (;;)
491 grub_uhci_td_t errtd;
493 errtd = (grub_uhci_td_t) (qh->elinkptr & ~0x0f);
495 grub_dprintf ("uhci", ">t status=0x%02x data=0x%02x td=%p\n",
496 errtd->ctrl_status, errtd->buffer & (~15), errtd);
498 /* Check if the transaction completed. */
499 if (qh->elinkptr & 1)
500 break;
502 grub_dprintf ("uhci", "t status=0x%02x\n", errtd->ctrl_status);
504 /* Check if the TD is not longer active. */
505 if (! (errtd->ctrl_status & (1 << 23)))
507 grub_dprintf ("uhci", ">>t status=0x%02x\n", errtd->ctrl_status);
509 /* Check if the endpoint is stalled. */
510 if (errtd->ctrl_status & (1 << 22))
511 err = GRUB_USB_ERR_STALL;
513 /* Check if an error related to the data buffer occurred. */
514 if (errtd->ctrl_status & (1 << 21))
515 err = GRUB_USB_ERR_DATA;
517 /* Check if a babble error occurred. */
518 if (errtd->ctrl_status & (1 << 20))
519 err = GRUB_USB_ERR_BABBLE;
521 /* Check if a NAK occurred. */
522 if (errtd->ctrl_status & (1 << 19))
523 err = GRUB_USB_ERR_NAK;
525 /* Check if a timeout occurred. */
526 if (errtd->ctrl_status & (1 << 18))
527 err = GRUB_USB_ERR_TIMEOUT;
529 /* Check if a bitstuff error occurred. */
530 if (errtd->ctrl_status & (1 << 17))
531 err = GRUB_USB_ERR_BITSTUFF;
533 if (err)
534 goto fail;
536 /* Fall through, no errors occurred, so the QH might be
537 updated. */
538 grub_dprintf ("uhci", "transaction fallthrough\n");
542 grub_dprintf ("uhci", "transaction complete\n");
544 fail:
546 grub_dprintf ("uhci", "transaction failed\n");
548 /* Place the QH back in the free list and deallocate the associated
549 TDs. */
550 qh->elinkptr = 1;
551 grub_free_queue (u, td_first);
553 return err;
556 static int
557 grub_uhci_iterate (int (*hook) (grub_usb_controller_t dev))
559 struct grub_uhci *u;
560 struct grub_usb_controller dev;
562 for (u = uhci; u; u = u->next)
564 dev.data = u;
565 if (hook (&dev))
566 return 1;
569 return 0;
572 static grub_err_t
573 grub_uhci_portstatus (grub_usb_controller_t dev,
574 unsigned int port, unsigned int enable)
576 struct grub_uhci *u = (struct grub_uhci *) dev->data;
577 int reg;
578 unsigned int status;
580 grub_dprintf ("uhci", "enable=%d port=%d\n", enable, port);
582 if (port == 0)
583 reg = GRUB_UHCI_REG_PORTSC1;
584 else if (port == 1)
585 reg = GRUB_UHCI_REG_PORTSC2;
586 else
587 return grub_error (GRUB_ERR_OUT_OF_RANGE,
588 "UHCI Root Hub port does not exist");
590 status = grub_uhci_readreg16 (u, reg);
591 grub_dprintf ("uhci", "detect=0x%02x\n", status);
593 /* Reset the port. */
594 grub_uhci_writereg16 (u, reg, enable << 9);
596 /* Wait for the reset to complete. XXX: How long exactly? */
597 grub_millisleep (10);
598 status = grub_uhci_readreg16 (u, reg);
599 grub_uhci_writereg16 (u, reg, status & ~(1 << 9));
600 grub_dprintf ("uhci", "reset completed\n");
602 /* Enable the port. */
603 grub_uhci_writereg16 (u, reg, enable << 2);
604 grub_millisleep (10);
606 grub_dprintf ("uhci", "waiting for the port to be enabled\n");
608 while (! (grub_uhci_readreg16 (u, reg) & (1 << 2)));
610 status = grub_uhci_readreg16 (u, reg);
611 grub_dprintf ("uhci", ">3detect=0x%02x\n", status);
614 return GRUB_ERR_NONE;
617 static grub_usb_speed_t
618 grub_uhci_detect_dev (grub_usb_controller_t dev, int port)
620 struct grub_uhci *u = (struct grub_uhci *) dev->data;
621 int reg;
622 unsigned int status;
624 if (port == 0)
625 reg = GRUB_UHCI_REG_PORTSC1;
626 else if (port == 1)
627 reg = GRUB_UHCI_REG_PORTSC2;
628 else
629 return grub_error (GRUB_ERR_OUT_OF_RANGE,
630 "UHCI Root Hub port does not exist");
632 status = grub_uhci_readreg16 (u, reg);
634 grub_dprintf ("uhci", "detect=0x%02x port=%d\n", status, port);
636 if (! (status & 1))
637 return GRUB_USB_SPEED_NONE;
638 else if (status & (1 << 8))
639 return GRUB_USB_SPEED_LOW;
640 else
641 return GRUB_USB_SPEED_FULL;
644 static int
645 grub_uhci_hubports (grub_usb_controller_t dev __attribute__((unused)))
647 /* The root hub has exactly two ports. */
648 return 2;
652 static struct grub_usb_controller_dev usb_controller =
654 .name = "uhci",
655 .iterate = grub_uhci_iterate,
656 .transfer = grub_uhci_transfer,
657 .hubports = grub_uhci_hubports,
658 .portstatus = grub_uhci_portstatus,
659 .detect_dev = grub_uhci_detect_dev
662 GRUB_MOD_INIT(uhci)
664 grub_uhci_inithw ();
665 grub_usb_controller_dev_register (&usb_controller);
666 grub_dprintf ("uhci", "registered\n");
669 GRUB_MOD_FINI(uhci)
671 struct grub_uhci *u;
673 /* Disable all UHCI controllers. */
674 for (u = uhci; u; u = u->next)
675 grub_uhci_writereg16 (u, GRUB_UHCI_REG_USBCMD, 0);
677 /* Unregister the controller. */
678 grub_usb_controller_dev_unregister (&usb_controller);