1 /* ohci.c - OHCI 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/>.
23 #include <grub/usbtrans.h>
24 #include <grub/misc.h>
26 #include <grub/cpu/pci.h>
27 #include <grub/i386/io.h>
28 #include <grub/time.h>
32 /* Pointers to Interrupt Endpoint Descriptors. Not used by
34 grub_uint32_t inttable
[32];
36 /* Current frame number. */
37 grub_uint16_t framenumber
;
41 /* List of completed TDs. */
42 grub_uint32_t donehead
;
44 grub_uint8_t reserved
[116];
45 } __attribute__((packed
));
47 /* OHCI Endpoint Descriptor. */
51 grub_uint32_t td_tail
;
52 grub_uint32_t td_head
;
53 grub_uint32_t next_ed
;
54 } __attribute__((packed
));
58 /* Information used to construct the TOKEN packet. */
62 grub_uint32_t next_td
;
63 grub_uint32_t buffer_end
;
64 } __attribute__((packed
));
66 typedef struct grub_ohci_td
*grub_ohci_td_t
;
67 typedef struct grub_ohci_ed
*grub_ohci_ed_t
;
71 volatile grub_uint32_t
*iobase
;
72 volatile struct grub_ohci_hcca
*hcca
;
73 struct grub_ohci
*next
;
76 static struct grub_ohci
*ohci
;
80 GRUB_OHCI_REG_REVISION
= 0x00,
81 GRUB_OHCI_REG_CONTROL
,
82 GRUB_OHCI_REG_CMDSTATUS
,
83 GRUB_OHCI_REG_INTSTATUS
,
87 GRUB_OHCI_REG_PERIODIC
,
88 GRUB_OHCI_REG_CONTROLHEAD
,
89 GRUB_OHCI_REG_CONTROLCURR
,
90 GRUB_OHCI_REG_BULKHEAD
,
91 GRUB_OHCI_REG_BULKCURR
,
92 GRUB_OHCI_REG_DONEHEAD
,
93 GRUB_OHCI_REG_FRAME_INTERVAL
,
94 GRUB_OHCI_REG_RHUBA
= 18,
95 GRUB_OHCI_REG_RHUBPORT
= 21
99 grub_ohci_readreg32 (struct grub_ohci
*o
, grub_ohci_reg_t reg
)
101 return grub_le_to_cpu32 (*(o
->iobase
+ reg
));
105 grub_ohci_writereg32 (struct grub_ohci
*o
,
106 grub_ohci_reg_t reg
, grub_uint32_t val
)
108 *(o
->iobase
+ reg
) = grub_cpu_to_le32 (val
);
113 /* Iterate over all PCI devices. Determine if a device is an OHCI
114 controller. If this is the case, initialize it. */
115 static int NESTED_FUNC_ATTR
116 grub_ohci_pci_iter (grub_pci_device_t dev
,
117 grub_pci_id_t pciid
__attribute__((unused
)))
119 grub_uint32_t class_code
;
121 grub_uint32_t subclass
;
122 grub_uint32_t interf
;
124 grub_pci_address_t addr
;
126 grub_uint32_t revision
;
127 grub_uint32_t frame_interval
;
129 addr
= grub_pci_make_address (dev
, GRUB_PCI_REG_CLASS
);
130 class_code
= grub_pci_read (addr
) >> 8;
132 interf
= class_code
& 0xFF;
133 subclass
= (class_code
>> 8) & 0xFF;
134 class = class_code
>> 16;
136 /* If this is not an OHCI controller, just return. */
137 if (class != 0x0c || subclass
!= 0x03 || interf
!= 0x10)
140 /* Determine IO base address. */
141 addr
= grub_pci_make_address (dev
, GRUB_PCI_REG_ADDRESS_REG0
);
142 base
= grub_pci_read (addr
);
145 /* Stop if there is no IO space base address defined. */
150 /* Allocate memory for the controller and register it. */
151 o
= grub_malloc (sizeof (*o
));
155 o
->iobase
= (grub_uint32_t
*) base
;
157 /* Reserve memory for the HCCA. */
158 o
->hcca
= (struct grub_ohci_hcca
*) grub_memalign (256, 256);
160 grub_dprintf ("ohci", "class=0x%02x 0x%02x interface 0x%02x base=%p\n",
161 class, subclass
, interf
, o
->iobase
);
163 /* Check if the OHCI revision is actually 1.0 as supported. */
164 revision
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_REVISION
);
165 grub_dprintf ("ohci", "OHCI revision=0x%02x\n", revision
& 0xFF);
166 if ((revision
& 0xFF) != 0x10)
169 /* Backup the frame interval register. */
170 frame_interval
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_FRAME_INTERVAL
);
172 /* Suspend the OHCI by issuing a reset. */
173 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CMDSTATUS
, 1); /* XXX: Magic. */
175 grub_dprintf ("ohci", "OHCI reset\n");
177 /* Restore the frame interval register. */
178 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_FRAME_INTERVAL
, frame_interval
);
180 /* Setup the HCCA. */
181 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_HCCA
, (grub_uint32_t
) o
->hcca
);
182 grub_dprintf ("ohci", "OHCI HCCA\n");
184 /* Enable the OHCI. */
185 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CONTROL
,
187 grub_dprintf ("ohci", "OHCI enable: 0x%02x\n",
188 (grub_ohci_readreg32 (o
, GRUB_OHCI_REG_CONTROL
) >> 6) & 3);
190 /* Link to ohci now that initialisation is successful. */
198 grub_free ((void *) o
->hcca
);
206 grub_ohci_inithw (void)
208 grub_pci_iterate (grub_ohci_pci_iter
);
214 grub_ohci_iterate (int (*hook
) (grub_usb_controller_t dev
))
217 struct grub_usb_controller dev
;
219 for (o
= ohci
; o
; o
= o
->next
)
230 grub_ohci_transaction (grub_ohci_td_t td
,
231 grub_transfer_type_t type
, unsigned int toggle
,
232 grub_size_t size
, char *data
)
235 grub_uint32_t buffer
;
236 grub_uint32_t buffer_end
;
238 grub_dprintf ("ohci", "OHCI transaction td=%p type=%d, toggle=%d, size=%d\n",
239 td
, type
, toggle
, size
);
243 case GRUB_USB_TRANSFER_TYPE_SETUP
:
246 case GRUB_USB_TRANSFER_TYPE_IN
:
249 case GRUB_USB_TRANSFER_TYPE_OUT
:
257 /* Generate no interrupts. */
261 token
|= toggle
<< 24;
264 buffer
= (grub_uint32_t
) data
;
265 buffer_end
= buffer
+ size
- 1;
267 td
->token
= grub_cpu_to_le32 (token
);
268 td
->buffer
= grub_cpu_to_le32 (buffer
);
270 td
->buffer_end
= grub_cpu_to_le32 (buffer_end
);
273 static grub_usb_err_t
274 grub_ohci_transfer (grub_usb_controller_t dev
,
275 grub_usb_transfer_t transfer
)
277 struct grub_ohci
*o
= (struct grub_ohci
*) dev
->data
;
279 grub_ohci_td_t td_list
;
280 grub_uint32_t target
;
281 grub_uint32_t td_tail
;
282 grub_uint32_t td_head
;
283 grub_uint32_t status
;
284 grub_uint32_t control
;
288 /* Allocate an Endpoint Descriptor. */
289 ed
= grub_memalign (16, sizeof (*ed
));
291 return GRUB_USB_ERR_INTERNAL
;
293 td_list
= grub_memalign (16, sizeof (*td_list
) * (transfer
->transcnt
+ 1));
296 grub_free ((void *) ed
);
297 return GRUB_USB_ERR_INTERNAL
;
300 grub_dprintf ("ohci", "alloc=%p\n", td_list
);
302 /* Setup all Transfer Descriptors. */
303 for (i
= 0; i
< transfer
->transcnt
; i
++)
305 grub_usb_transaction_t tr
= &transfer
->transactions
[i
];
307 grub_ohci_transaction (&td_list
[i
], tr
->pid
, tr
->toggle
,
310 td_list
[i
].next_td
= grub_cpu_to_le32 (&td_list
[i
+ 1]);
313 /* Setup the Endpoint Descriptor. */
315 /* Set the device address. */
316 target
= transfer
->devaddr
;
318 /* Set the endpoint. */
319 target
|= transfer
->endpoint
<< 7;
321 /* Set the device speed. */
322 target
|= (transfer
->dev
->speed
== GRUB_USB_SPEED_LOW
) << 13;
324 /* Set the maximum packet size. */
325 target
|= transfer
->max
<< 16;
327 td_head
= (grub_uint32_t
) td_list
;
329 td_tail
= (grub_uint32_t
) &td_list
[transfer
->transcnt
];
331 ed
->target
= grub_cpu_to_le32 (target
);
332 ed
->td_head
= grub_cpu_to_le32 (td_head
);
333 ed
->td_tail
= grub_cpu_to_le32 (td_tail
);
334 ed
->next_ed
= grub_cpu_to_le32 (0);
336 grub_dprintf ("ohci", "program OHCI\n");
338 /* Program the OHCI to actually transfer. */
339 switch (transfer
->type
)
341 case GRUB_USB_TRANSACTION_TYPE_BULK
:
343 grub_dprintf ("ohci", "add to bulk list\n");
345 status
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_CMDSTATUS
);
346 control
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_CONTROL
);
348 /* Disable the Control and Bulk lists. */
349 control
&= ~(3 << 4);
350 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CONTROL
, control
);
352 /* Clear BulkListFilled. */
354 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CMDSTATUS
, status
);
356 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_BULKHEAD
, (grub_uint32_t
) ed
);
358 /* Enable the Bulk list. */
360 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CONTROL
, control
);
362 /* Set BulkListFilled. */
364 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CMDSTATUS
, status
);
369 case GRUB_USB_TRANSACTION_TYPE_CONTROL
:
371 grub_dprintf ("ohci", "add to control list\n");
372 status
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_CMDSTATUS
);
373 control
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_CONTROL
);
375 /* Disable the Control and Bulk lists. */
376 control
&= ~(3 << 4);
377 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CONTROL
, control
);
379 /* Clear ControlListFilled. */
381 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CMDSTATUS
, status
);
383 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CONTROLHEAD
,
385 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CONTROLHEAD
+1,
388 /* Enable the Control list. */
390 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CONTROL
, control
);
392 /* Set ControlListFilled. */
394 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CMDSTATUS
, status
);
399 grub_dprintf ("ohci", "wait for completion\n");
400 grub_dprintf ("ohci", "control=0x%02x status=0x%02x\n",
401 grub_ohci_readreg32 (o
, GRUB_OHCI_REG_CONTROL
),
402 grub_ohci_readreg32 (o
, GRUB_OHCI_REG_CMDSTATUS
));
404 /* Wait until the transfer is completed or STALLs. */
405 while ((ed
->td_head
& ~0xf) != (ed
->td_tail
& ~0xf))
409 grub_dprintf ("ohci", "head=0x%02x tail=0x%02x\n", ed
->td_head
, ed
->td_tail
);
411 /* Detected a STALL. */
416 grub_dprintf ("ohci", "complete\n");
418 /* if (ed->td_head & 1) */
419 /* err = GRUB_USB_ERR_STALL; */
420 /* else if (ed->td */
425 grub_uint8_t errcode
;
426 grub_ohci_td_t tderr
;
428 tderr
= (grub_ohci_td_t
) grub_ohci_readreg32 (o
,
429 GRUB_OHCI_REG_DONEHEAD
);
430 errcode
= tderr
->token
>> 28;
435 /* XXX: Should not happen! */
436 grub_error (GRUB_ERR_IO
, "OHCI without reporting the reason");
437 err
= GRUB_USB_ERR_INTERNAL
;
441 /* XXX: CRC error. */
442 err
= GRUB_USB_ERR_TIMEOUT
;
446 err
= GRUB_USB_ERR_BITSTUFF
;
450 /* XXX: Data Toggle error. */
451 err
= GRUB_USB_ERR_DATA
;
455 err
= GRUB_USB_ERR_STALL
;
459 /* XXX: Not responding. */
460 err
= GRUB_USB_ERR_TIMEOUT
;
464 /* XXX: PID Check bits failed. */
465 err
= GRUB_USB_ERR_BABBLE
;
469 /* XXX: PID unexpected failed. */
470 err
= GRUB_USB_ERR_BABBLE
;
474 /* XXX: Data overrun error. */
475 err
= GRUB_USB_ERR_DATA
;
479 /* XXX: Data underrun error. */
480 err
= GRUB_USB_ERR_DATA
;
485 err
= GRUB_USB_ERR_NAK
;
490 err
= GRUB_USB_ERR_NAK
;
494 /* XXX: Buffer overrun. */
495 err
= GRUB_USB_ERR_DATA
;
499 /* XXX: Buffer underrun. */
500 err
= GRUB_USB_ERR_DATA
;
504 err
= GRUB_USB_ERR_NAK
;
509 err
= GRUB_USB_ERR_NONE
;
511 /* Disable the Control and Bulk lists. */
512 control
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_CONTROL
);
513 control
&= ~(3 << 4);
514 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CONTROL
, control
);
516 /* Clear BulkListFilled and ControlListFilled. */
517 status
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_CMDSTATUS
);
518 status
&= ~((1 << 2) | (1 << 3));
519 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_CMDSTATUS
, status
);
529 grub_ohci_portstatus (grub_usb_controller_t dev
,
530 unsigned int port
, unsigned int enable
)
532 struct grub_ohci
*o
= (struct grub_ohci
*) dev
->data
;
533 grub_uint32_t status
;
535 /* Reset the port. */
536 status
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_RHUBPORT
+ port
);
537 status
|= (1 << 4); /* XXX: Magic. */
538 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_RHUBPORT
+ port
, status
);
539 grub_millisleep (100);
541 /* End the reset signaling. */
542 status
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_RHUBPORT
+ port
);
543 status
|= (1 << 20); /* XXX: Magic. */
544 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_RHUBPORT
+ port
, status
);
545 grub_millisleep (10);
547 /* Enable the port. */
548 status
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_RHUBPORT
+ port
);
549 status
|= (enable
<< 1); /* XXX: Magic. */
550 grub_ohci_writereg32 (o
, GRUB_OHCI_REG_RHUBPORT
+ port
, status
);
552 status
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_RHUBPORT
+ port
);
553 grub_dprintf ("ohci", "portstatus=0x%02x\n", status
);
555 return GRUB_ERR_NONE
;
558 static grub_usb_speed_t
559 grub_ohci_detect_dev (grub_usb_controller_t dev
, int port
)
561 struct grub_ohci
*o
= (struct grub_ohci
*) dev
->data
;
562 grub_uint32_t status
;
564 status
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_RHUBPORT
+ port
);
566 grub_dprintf ("ohci", "detect_dev status=0x%02x\n", status
);
569 return GRUB_USB_SPEED_NONE
;
570 else if (status
& (1 << 9))
571 return GRUB_USB_SPEED_LOW
;
573 return GRUB_USB_SPEED_FULL
;
577 grub_ohci_hubports (grub_usb_controller_t dev
)
579 struct grub_ohci
*o
= (struct grub_ohci
*) dev
->data
;
580 grub_uint32_t portinfo
;
582 portinfo
= grub_ohci_readreg32 (o
, GRUB_OHCI_REG_RHUBA
);
584 grub_dprintf ("ohci", "root hub ports=%d\n", portinfo
& 0xFF);
586 /* The root hub has exactly two ports. */
587 return portinfo
& 0xFF;
592 static struct grub_usb_controller_dev usb_controller
=
595 .iterate
= grub_ohci_iterate
,
596 .transfer
= grub_ohci_transfer
,
597 .hubports
= grub_ohci_hubports
,
598 .portstatus
= grub_ohci_portstatus
,
599 .detect_dev
= grub_ohci_detect_dev
605 grub_usb_controller_dev_register (&usb_controller
);
610 grub_usb_controller_dev_unregister (&usb_controller
);