1 /* uhci.c - UHCI Support. */
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/>.
22 #include <grub/misc.h>
24 #include <grub/usbtrans.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)
34 GRUB_UHCI_REG_USBCMD
= 0x00,
35 GRUB_UHCI_REG_FLBASEADD
= 0x08,
36 GRUB_UHCI_REG_PORTSC1
= 0x10,
37 GRUB_UHCI_REG_PORTSC2
= 0x12
40 #define GRUB_UHCI_LINK_TERMINATE 1
41 #define GRUB_UHCI_LINK_QUEUE_HEAD 2
44 /* UHCI Queue Head. */
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
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. */
57 } __attribute__ ((packed
));
59 /* UHCI Transfer Descriptor. */
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. */
71 /* A pointer to the data buffer, UHCI requires this pointer to be 32
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
;
89 grub_uint32_t
*framelist
;
91 /* 256 Queue Heads. */
94 /* 256 Transfer Descriptors. */
97 /* Free Transfer Descriptors. */
98 grub_uhci_td_t tdfree
;
100 struct grub_uhci
*next
;
103 static struct grub_uhci
*uhci
;
106 grub_uhci_readreg16 (struct grub_uhci
*u
, grub_uhci_reg_t reg
)
108 return grub_inw (u
->iobase
+ reg
);
113 grub_uhci_readreg32 (struct grub_uhci
*u
, grub_uhci_reg_t reg
)
115 return grub_inl (u
->iobase
+ reg
);
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
);
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
);
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
;
146 grub_uint32_t subclass
;
147 grub_uint32_t interf
;
150 grub_pci_address_t addr
;
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)
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. */
172 /* Allocate memory for the controller and register it. */
173 u
= grub_malloc (sizeof (*u
));
177 u
->iobase
= base
& GRUB_UHCI_IOMASK
;
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);
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");
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);
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");
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);
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");
228 /* Link all Transfer Descriptors in a list of available Transfer
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;
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
241 fp
= (grub_uint32_t
) u
->qh
& (~15);
242 /* Mark this as a queue head. */
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);
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
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");
278 for (i
= 0; i
< 10; i
++)
282 frnum
= grub_uhci_readreg16 (u
, 6);
283 grub_dprintf ("uhci", "Framenum=%d\n", frnum
);
284 grub_millisleep (100);
289 /* Link to uhci now that initialisation is successful. */
298 grub_free ((void *) u
->qh
);
299 grub_free (u
->framelist
);
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
)
317 /* Check if there is a Transfer Descriptor available. */
322 u
->tdfree
= (grub_uhci_td_t
) u
->tdfree
->linkptr
;
328 grub_free_td (struct grub_uhci
*u
, grub_uhci_td_t td
)
330 td
->linkptr
= (grub_uint32_t
) u
->tdfree
;
335 grub_free_queue (struct grub_uhci
*u
, grub_uhci_td_t td
)
337 /* Free the TDs in this queue. */
340 grub_uhci_td_t tdprev
;
342 /* Unlink the queue. */
344 td
= (grub_uhci_td_t
) td
->linkptr2
;
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
)))
358 /* Look for a Queue Head for this transfer. Skip the first QH if
359 this is a Interrupt Transfer. */
361 if (tr
== GRUB_USB_TRANSACTION_TYPE_INTERRUPT
)
369 if (u
->qh
[i
].elinkptr
& 1)
373 if (! (qh
->elinkptr
& 1))
375 grub_error (GRUB_ERR_OUT_OF_MEMORY
,
376 "no free queue heads available");
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
,
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
);
399 grub_error (GRUB_ERR_OUT_OF_MEMORY
,
400 "no transfer descriptors available for UHCI transfer");
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. */
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
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
;
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
;
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
;
442 /* Allocate a queue head for the transfer queue. */
443 qh
= grub_alloc_qh (u
, GRUB_USB_TRANSACTION_TYPE_CONTROL
);
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
,
456 /* Terminate and free. */
457 td_prev
->linkptr2
= 0;
458 td_prev
->linkptr
= 1;
461 grub_free_queue (u
, td_first
);
463 return GRUB_USB_ERR_INTERNAL
;
470 td_prev
->linkptr2
= (grub_uint32_t
) td
;
471 td_prev
->linkptr
= (grub_uint32_t
) td
;
472 td_prev
->linkptr
|= 4;
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
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
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)
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
;
536 /* Fall through, no errors occurred, so the QH might be
538 grub_dprintf ("uhci", "transaction fallthrough\n");
542 grub_dprintf ("uhci", "transaction complete\n");
546 grub_dprintf ("uhci", "transaction failed\n");
548 /* Place the QH back in the free list and deallocate the associated
551 grub_free_queue (u
, td_first
);
557 grub_uhci_iterate (int (*hook
) (grub_usb_controller_t dev
))
560 struct grub_usb_controller dev
;
562 for (u
= uhci
; u
; u
= u
->next
)
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
;
580 grub_dprintf ("uhci", "enable=%d port=%d\n", enable
, port
);
583 reg
= GRUB_UHCI_REG_PORTSC1
;
585 reg
= GRUB_UHCI_REG_PORTSC2
;
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
;
625 reg
= GRUB_UHCI_REG_PORTSC1
;
627 reg
= GRUB_UHCI_REG_PORTSC2
;
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
);
637 return GRUB_USB_SPEED_NONE
;
638 else if (status
& (1 << 8))
639 return GRUB_USB_SPEED_LOW
;
641 return GRUB_USB_SPEED_FULL
;
645 grub_uhci_hubports (grub_usb_controller_t dev
__attribute__((unused
)))
647 /* The root hub has exactly two ports. */
652 static struct grub_usb_controller_dev usb_controller
=
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
665 grub_usb_controller_dev_register (&usb_controller
);
666 grub_dprintf ("uhci", "registered\n");
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
);