1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2021 Val Packett <val@packett.cool> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include "opt_acpi.h" 29 #include "opt_pci.h" 30 31 #include <sys/cdefs.h> 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/proc.h> 37 #include <sys/rman.h> 38 39 #include <dev/intel/spi.h> 40 #include <dev/pci/pcireg.h> 41 #include <dev/pci/pcivar.h> 42 43 #include "spibus_if.h" 44 45 static struct intelspi_pci_device { 46 uint32_t devid; 47 enum intelspi_vers vers; 48 } intelspi_pci_devices[] = { 49 { 0x9c658086, SPI_LYNXPOINT }, 50 { 0x9c668086, SPI_LYNXPOINT }, 51 { 0x9ce58086, SPI_LYNXPOINT }, 52 { 0x9ce68086, SPI_LYNXPOINT }, 53 { 0x9d298086, SPI_SUNRISEPOINT }, 54 { 0x9d2a8086, SPI_SUNRISEPOINT }, 55 { 0xa1298086, SPI_SUNRISEPOINT }, 56 { 0xa12a8086, SPI_SUNRISEPOINT }, 57 { 0xa2a98086, SPI_SUNRISEPOINT }, 58 { 0xa2aa8086, SPI_SUNRISEPOINT }, 59 { 0xa3a98086, SPI_SUNRISEPOINT }, 60 { 0xa3aa8086, SPI_SUNRISEPOINT }, 61 }; 62 63 static int 64 intelspi_pci_probe(device_t dev) 65 { 66 struct intelspi_softc *sc = device_get_softc(dev); 67 uint32_t devid = pci_get_devid(dev); 68 int i; 69 70 for (i = 0; i < nitems(intelspi_pci_devices); i++) { 71 if (intelspi_pci_devices[i].devid == devid) { 72 sc->sc_vers = intelspi_pci_devices[i].vers; 73 /* The PCI device is listed in ACPI too. 74 * Not that we use the handle for anything... */ 75 sc->sc_handle = acpi_get_handle(dev); 76 device_set_desc(dev, intelspi_infos[sc->sc_vers].desc); 77 return (BUS_PROBE_DEFAULT); 78 } 79 } 80 81 return (ENXIO); 82 } 83 84 static int 85 intelspi_pci_attach(device_t dev) 86 { 87 struct intelspi_softc *sc = device_get_softc(dev); 88 89 sc->sc_mem_rid = PCIR_BAR(0); 90 sc->sc_irq_rid = 0; 91 if (pci_alloc_msi(dev, &sc->sc_irq_rid)) { 92 device_printf(dev, "Using MSI\n"); 93 } 94 95 return (intelspi_attach(dev)); 96 } 97 98 static int 99 intelspi_pci_detach(device_t dev) 100 { 101 struct intelspi_softc *sc = device_get_softc(dev); 102 int err; 103 104 err = intelspi_detach(dev); 105 if (err) 106 return (err); 107 108 if (sc->sc_irq_rid != 0) 109 pci_release_msi(dev); 110 111 return (0); 112 } 113 114 static device_method_t intelspi_pci_methods[] = { 115 /* Device interface */ 116 DEVMETHOD(device_probe, intelspi_pci_probe), 117 DEVMETHOD(device_attach, intelspi_pci_attach), 118 DEVMETHOD(device_detach, intelspi_pci_detach), 119 DEVMETHOD(device_suspend, intelspi_suspend), 120 DEVMETHOD(device_resume, intelspi_resume), 121 122 /* SPI interface */ 123 DEVMETHOD(spibus_transfer, intelspi_transfer), 124 125 DEVMETHOD_END 126 }; 127 128 static driver_t intelspi_pci_driver = { 129 "spi", 130 intelspi_pci_methods, 131 sizeof(struct intelspi_softc), 132 }; 133 134 DRIVER_MODULE(intelspi, pci, intelspi_pci_driver, 0, 0); 135 MODULE_DEPEND(intelspi, pci, 1, 1, 1); 136 MODULE_DEPEND(intelspi, spibus, 1, 1, 1); 137 MODULE_PNP_INFO("W32:vendor/device", pci, intelspi, intelspi_pci_devices, 138 nitems(intelspi_pci_devices)); 139