akaros/kern/arch/x86/usb.c
<<
>>
Prefs
   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
  27static 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 ehci_hcc_regs;
  33        uint32_t hccparams;
  34
  35        ehci_hcc_regs = (uintptr_t)pci_get_mmio_bar_kva(pcidev, 0);
  36        assert(ehci_hcc_regs);
  37        hccparams = read_mmreg32(ehci_hcc_regs + 0x08);
  38
  39        ptr = (hccparams >> 8) & ((1 << 8) - 1);
  40
  41        for (; ptr != 0; ptr = pcidev_read8(pcidev, ptr + 1)) {
  42                if (ptr < 0x40 || (ptr & ~0xFC))
  43                        break;
  44                cap = pcidev_read8(pcidev, ptr);
  45                if (cap != Clegacy)
  46                        continue;
  47                sem = pcidev_read8(pcidev, ptr + CLbiossem);
  48                if (sem == 0)
  49                        continue;
  50                pcidev_write8(pcidev, ptr + CLossem, 1);
  51                for (i = 0; i < 100; i++) {
  52                        if (pcidev_read8(pcidev, ptr + CLbiossem) == 0)
  53                                break;
  54                        udelay(10);
  55                }
  56                if (i == 100)
  57                        printk("PCI EHCI %x:%x:%x: bios timed out\n",
  58                               pcidev->bus, pcidev->dev, pcidev->func);
  59                /* bit 29 could be left on, in case we want to give it back */
  60                pcidev_write32(pcidev, ptr + CLcontrol, 0);     /* no SMIs */
  61                //ctlr->opio->config = 0;
  62                //coherence();
  63                printk("PCI EHCI %x:%x:%x: disabled legacy USB\n",
  64                       pcidev->bus, pcidev->dev, pcidev->func);
  65                return;
  66        }
  67        printk("PCI EHCI %x:%x:%x: couldn't find legacy capability\n",
  68               pcidev->bus, pcidev->dev, pcidev->func);
  69}
  70
  71#define XHCI_USBLEGSUP 1
  72
  73static void xhci_disable_leg(struct pci_device *pcidev)
  74{
  75        uintptr_t xhci_hcc_regs, xecp;
  76        uint32_t hccparams, val;
  77        int i;
  78
  79        xhci_hcc_regs = (uintptr_t)pci_get_mmio_bar_kva(pcidev, 0);
  80        assert(xhci_hcc_regs);
  81        hccparams = read_mmreg32(xhci_hcc_regs + 0x10);
  82        xecp = (hccparams >> 16) & 0xffff;
  83
  84        /* xecp is the rel offset, in 32 bit words, from the base to the
  85         * extended capabilities pointer. */
  86        for (/* xecp set */; xecp; xecp = (read_mmreg32(xecp) >> 8) & 0xff) {
  87                xecp = xhci_hcc_regs + (xecp << 2);
  88                val = read_mmreg32(xecp);
  89
  90                if ((val & 0xff) != XHCI_USBLEGSUP)
  91                        continue;
  92                /* bios already does not own it */
  93                if (!(val & (1 << 16)))
  94                        return;
  95                /* take ownership.  Note we're allowed to do byte-width writes
  96                 * here. */
  97                write_mmreg8(xecp + 3, 1);
  98                /* book says to wait up to a second, though i regularly see it
  99                 * time out on my machines. */
 100                for (i = 0; i < 100000; i++) {
 101                        if (!(read_mmreg32(xecp) & (1 << 16)))
 102                                break;
 103                        udelay(10);
 104                }
 105                if (i == 100000) {
 106                        printk("PCI XHCI %x:%x:%x: bios timed out\n",
 107                               pcidev->bus, pcidev->dev, pcidev->func);
 108                        /* Force the bios's byte clear */
 109                        write_mmreg8(xecp + 2, 0);
 110                }
 111                /* Turn off settings in USBLEGCTLSTS.  Not sure if any of this
 112                 * is necessary. */
 113                val = read_mmreg32(xecp + 4);
 114                val &= ~((1 << 0) | (1 << 4) | (0x7 << 13));
 115                /* These are write-to-clear. */
 116                val |= 0x7 << 29;
 117                write_mmreg32(xecp + 4, val);
 118                printk("PCI XHCI %x:%x:%x: disabled legacy USB\n",
 119                       pcidev->bus, pcidev->dev, pcidev->func);
 120                return;
 121        }
 122        printk("PCI XHCI %x:%x:%x: couldn't find legacy capability\n",
 123               pcidev->bus, pcidev->dev, pcidev->func);
 124}
 125
 126static void uhci_disable_leg(struct pci_device *pcidev)
 127{
 128        pcidev_write16(pcidev, 0xc0, 0x2000);
 129        printk("PCI UHCI %x:%x:%x: disabled legacy USB\n",
 130               pcidev->bus, pcidev->dev, pcidev->func);
 131}
 132
 133void usb_disable_legacy(void)
 134{
 135        struct pci_device *i;
 136
 137        STAILQ_FOREACH(i, &pci_devices, all_dev) {
 138                if ((i->class == 0x0c) && (i->subclass == 0x03)) {
 139                        switch (i->progif) {
 140                        case 0x00:
 141                                uhci_disable_leg(i);
 142                                break;
 143                        case 0x20:
 144                                ehci_disable_leg(i);
 145                                break;
 146                        case 0x30:
 147                                xhci_disable_leg(i);
 148                                break;
 149                        default:
 150                                /* TODO: ohci */
 151                                printk("PCI USB %x:%x:%x unknown progif 0x%x\n",
 152                                       i->bus, i->dev, i->func, i->progif);
 153                        }
 154                }
 155        }
 156}
 157