BXE: min->MIN, plus an spatch
[akaros.git] / kern / arch / x86 / usb.c
1 /* 
2  * This file is part of the UCB release of Plan 9. It is subject to the license
3  * terms in the LICENSE file found in the top-level directory of this
4  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
5  * part of the UCB release of Plan 9, including this file, may be copied,
6  * modified, propagated, or distributed except according to the terms contained
7  * in the LICENSE file.
8  *
9  * Adapted from usbehcipc.c and usbuhci.c
10  */
11
12 #define Clegacy                                 1
13 #define CLbiossem                               2
14 #define CLossem                                 3
15 #define CLcontrol                               4
16
17 #include <arch/x86.h>
18 #include <arch/pci.h>
19 #include <trap.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <kmalloc.h>
24 #include <time.h>
25 #include <mm.h>
26
27 static void ehci_disable_leg(struct pci_device *pcidev)
28 {
29         int i, ptr, cap, sem;
30
31         //ptr = (ctlr->capio->capparms >> Ceecpshift) & Ceecpmask;
32         uintptr_t bar0 = pci_get_membar(pcidev, 0);
33         assert(bar0);
34         uintptr_t ehci_hcc_regs = vmap_pmem_nocache(bar0, pcidev->bar[0].mmio_sz);
35         uint32_t hccparams = read_mmreg32(ehci_hcc_regs + 0x08);
36         ptr = (hccparams >> 8) & ((1 << 8) - 1);
37
38         for(; ptr != 0; ptr = pcidev_read8(pcidev, ptr + 1)) {
39                 if (ptr < 0x40 || (ptr & ~0xFC))
40                         break;
41                 cap = pcidev_read8(pcidev, ptr);
42                 if (cap != Clegacy)
43                         continue;
44                 sem = pcidev_read8(pcidev, ptr + CLbiossem);
45                 if (sem == 0)
46                         continue;
47                 pcidev_write8(pcidev, ptr + CLossem, 1);
48                 for (i = 0; i < 100; i++) {
49                         if (pcidev_read8(pcidev, ptr + CLbiossem) == 0)
50                                 break;
51                         udelay(10);
52                 }
53                 if (i == 100)
54                         printk("PCI EHCI %x:%x:%x: bios timed out\n",
55                                pcidev->bus, pcidev->dev, pcidev->func);
56                 /* bit 29 could be left on, in case we want to give it back */
57                 pcidev_write32(pcidev, ptr + CLcontrol, 0);     /* no SMIs */
58                 //ctlr->opio->config = 0;
59                 //coherence();
60                 printk("PCI EHCI %x:%x:%x: disabled legacy USB\n",
61                        pcidev->bus, pcidev->dev, pcidev->func);
62                 return;
63         }
64         printk("PCI EHCI %x:%x:%x: couldn't find legacy capability\n",
65                pcidev->bus, pcidev->dev, pcidev->func);
66 }
67
68 static void uhci_disable_leg(struct pci_device *pcidev)
69 {
70         pcidev_write16(pcidev, 0xc0, 0x2000);
71         printk("PCI UHCI %x:%x:%x: disabled legacy USB\n",
72                pcidev->bus, pcidev->dev, pcidev->func);
73 }
74
75 void usb_disable_legacy()
76 {
77         struct pci_device *i;
78
79         STAILQ_FOREACH(i, &pci_devices, all_dev) {
80                 if ((i->class == 0x0c) && (i->subclass == 0x03)) {
81                         switch (i->progif) {
82                                 case 0x00:
83                                         uhci_disable_leg(i);
84                                         break;
85                                 case 0x20:
86                                         ehci_disable_leg(i);
87                                         break;
88                                 default:
89                                         /* TODO: ohci */
90                                         printk("PCI USB %x:%x:%x, unknown progif 0x%x\n",
91                                                i->bus, i->dev, i->func, i->progif);
92                         }
93                 }
94         }
95 }