GRUB-1.98 changes
[grub2/jjazz.git] / bus / usb / ohci.c
blob6d185bc7f3bbac64a1ea9e3189a7468806a309bc
1 /* ohci.c - OHCI 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/usb.h>
23 #include <grub/usbtrans.h>
24 #include <grub/misc.h>
25 #include <grub/pci.h>
26 #include <grub/cpu/pci.h>
27 #include <grub/i386/io.h>
28 #include <grub/time.h>
30 struct grub_ohci_hcca
32 /* Pointers to Interrupt Endpoint Descriptors. Not used by
33 GRUB. */
34 grub_uint32_t inttable[32];
36 /* Current frame number. */
37 grub_uint16_t framenumber;
39 grub_uint16_t pad;
41 /* List of completed TDs. */
42 grub_uint32_t donehead;
44 grub_uint8_t reserved[116];
45 } __attribute__((packed));
47 /* OHCI Endpoint Descriptor. */
48 struct grub_ohci_ed
50 grub_uint32_t target;
51 grub_uint32_t td_tail;
52 grub_uint32_t td_head;
53 grub_uint32_t next_ed;
54 } __attribute__((packed));
56 struct grub_ohci_td
58 /* Information used to construct the TOKEN packet. */
59 grub_uint32_t token;
61 grub_uint32_t buffer;
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;
69 struct grub_ohci
71 volatile grub_uint32_t *iobase;
72 volatile struct grub_ohci_hcca *hcca;
73 struct grub_ohci *next;
76 static struct grub_ohci *ohci;
78 typedef enum
80 GRUB_OHCI_REG_REVISION = 0x00,
81 GRUB_OHCI_REG_CONTROL,
82 GRUB_OHCI_REG_CMDSTATUS,
83 GRUB_OHCI_REG_INTSTATUS,
84 GRUB_OHCI_REG_INTENA,
85 GRUB_OHCI_REG_INTDIS,
86 GRUB_OHCI_REG_HCCA,
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
96 } grub_ohci_reg_t;
98 static grub_uint32_t
99 grub_ohci_readreg32 (struct grub_ohci *o, grub_ohci_reg_t reg)
101 return grub_le_to_cpu32 (*(o->iobase + reg));
104 static void
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;
120 grub_uint32_t class;
121 grub_uint32_t subclass;
122 grub_uint32_t interf;
123 grub_uint32_t base;
124 grub_pci_address_t addr;
125 struct grub_ohci *o;
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)
138 return 0;
140 /* Determine IO base address. */
141 addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
142 base = grub_pci_read (addr);
144 #if 0
145 /* Stop if there is no IO space base address defined. */
146 if (! (base & 1))
147 return 0;
148 #endif
150 /* Allocate memory for the controller and register it. */
151 o = grub_malloc (sizeof (*o));
152 if (! o)
153 return 1;
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)
167 goto fail;
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. */
174 grub_millisleep (1);
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,
186 (2 << 6));
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. */
191 o->next = ohci;
192 ohci = o;
194 return 0;
196 fail:
197 if (o)
198 grub_free ((void *) o->hcca);
199 grub_free (o);
201 return 1;
205 static void
206 grub_ohci_inithw (void)
208 grub_pci_iterate (grub_ohci_pci_iter);
213 static int
214 grub_ohci_iterate (int (*hook) (grub_usb_controller_t dev))
216 struct grub_ohci *o;
217 struct grub_usb_controller dev;
219 for (o = ohci; o; o = o->next)
221 dev.data = o;
222 if (hook (&dev))
223 return 1;
226 return 0;
229 static void
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)
234 grub_uint32_t token;
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);
241 switch (type)
243 case GRUB_USB_TRANSFER_TYPE_SETUP:
244 token = 0 << 19;
245 break;
246 case GRUB_USB_TRANSFER_TYPE_IN:
247 token = 2 << 19;
248 break;
249 case GRUB_USB_TRANSFER_TYPE_OUT:
250 token = 1 << 19;
251 break;
252 default:
253 token = 0;
254 break;
257 /* Generate no interrupts. */
258 token |= 7 << 21;
260 /* Set the token. */
261 token |= toggle << 24;
262 token |= 1 << 25;
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);
269 td->next_td = 0;
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;
278 grub_ohci_ed_t ed;
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;
285 grub_usb_err_t err;
286 int i;
288 /* Allocate an Endpoint Descriptor. */
289 ed = grub_memalign (16, sizeof (*ed));
290 if (! ed)
291 return GRUB_USB_ERR_INTERNAL;
293 td_list = grub_memalign (16, sizeof (*td_list) * (transfer->transcnt + 1));
294 if (! td_list)
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,
308 tr->size, tr->data);
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. */
353 status &= ~(1 << 2);
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. */
359 control |= 1 << 5;
360 grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROL, control);
362 /* Set BulkListFilled. */
363 status |= 1 << 2;
364 grub_ohci_writereg32 (o, GRUB_OHCI_REG_CMDSTATUS, status);
366 break;
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. */
380 status &= ~(1 << 1);
381 grub_ohci_writereg32 (o, GRUB_OHCI_REG_CMDSTATUS, status);
383 grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLHEAD,
384 (grub_uint32_t) ed);
385 grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROLHEAD+1,
386 (grub_uint32_t) ed);
388 /* Enable the Control list. */
389 control |= 1 << 4;
390 grub_ohci_writereg32 (o, GRUB_OHCI_REG_CONTROL, control);
392 /* Set ControlListFilled. */
393 status |= 1 << 1;
394 grub_ohci_writereg32 (o, GRUB_OHCI_REG_CMDSTATUS, status);
395 break;
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))
407 grub_cpu_idle ();
409 grub_dprintf ("ohci", "head=0x%02x tail=0x%02x\n", ed->td_head, ed->td_tail);
411 /* Detected a STALL. */
412 if (ed->td_head & 1)
413 break;
416 grub_dprintf ("ohci", "complete\n");
418 /* if (ed->td_head & 1) */
419 /* err = GRUB_USB_ERR_STALL; */
420 /* else if (ed->td */
423 if (ed->td_head & 1)
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;
432 switch (errcode)
434 case 0:
435 /* XXX: Should not happen! */
436 grub_error (GRUB_ERR_IO, "OHCI without reporting the reason");
437 err = GRUB_USB_ERR_INTERNAL;
438 break;
440 case 1:
441 /* XXX: CRC error. */
442 err = GRUB_USB_ERR_TIMEOUT;
443 break;
445 case 2:
446 err = GRUB_USB_ERR_BITSTUFF;
447 break;
449 case 3:
450 /* XXX: Data Toggle error. */
451 err = GRUB_USB_ERR_DATA;
452 break;
454 case 4:
455 err = GRUB_USB_ERR_STALL;
456 break;
458 case 5:
459 /* XXX: Not responding. */
460 err = GRUB_USB_ERR_TIMEOUT;
461 break;
463 case 6:
464 /* XXX: PID Check bits failed. */
465 err = GRUB_USB_ERR_BABBLE;
466 break;
468 case 7:
469 /* XXX: PID unexpected failed. */
470 err = GRUB_USB_ERR_BABBLE;
471 break;
473 case 8:
474 /* XXX: Data overrun error. */
475 err = GRUB_USB_ERR_DATA;
476 break;
478 case 9:
479 /* XXX: Data underrun error. */
480 err = GRUB_USB_ERR_DATA;
481 break;
483 case 10:
484 /* XXX: Reserved. */
485 err = GRUB_USB_ERR_NAK;
486 break;
488 case 11:
489 /* XXX: Reserved. */
490 err = GRUB_USB_ERR_NAK;
491 break;
493 case 12:
494 /* XXX: Buffer overrun. */
495 err = GRUB_USB_ERR_DATA;
496 break;
498 case 13:
499 /* XXX: Buffer underrun. */
500 err = GRUB_USB_ERR_DATA;
501 break;
503 default:
504 err = GRUB_USB_ERR_NAK;
505 break;
508 else
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);
521 /* XXX */
522 grub_free (td_list);
523 grub_free (ed);
525 return err;
528 static grub_err_t
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);
568 if (! (status & 1))
569 return GRUB_USB_SPEED_NONE;
570 else if (status & (1 << 9))
571 return GRUB_USB_SPEED_LOW;
572 else
573 return GRUB_USB_SPEED_FULL;
576 static int
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 =
594 .name = "ohci",
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
602 GRUB_MOD_INIT(ohci)
604 grub_ohci_inithw ();
605 grub_usb_controller_dev_register (&usb_controller);
608 GRUB_MOD_FINI(ohci)
610 grub_usb_controller_dev_unregister (&usb_controller);