xref: /freebsd/sys/dev/intel/spi_pci.c (revision fdafd315)
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/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/proc.h>
36 #include <sys/rman.h>
37 
38 #include <dev/intel/spi.h>
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcivar.h>
41 
42 #include "spibus_if.h"
43 
44 static struct intelspi_pci_device {
45 	uint32_t devid;
46 	enum intelspi_vers vers;
47 	const char *desc;
48 } intelspi_pci_devices[] = {
49 	{ 0x9c658086, SPI_LYNXPOINT, "Intel Lynx Point-LP SPI Controller-0" },
50 	{ 0x9c668086, SPI_LYNXPOINT, "Intel Lynx Point-LP SPI Controller-1" },
51 	{ 0x9ce58086, SPI_LYNXPOINT, "Intel Wildcat Point SPI Controller-0" },
52 	{ 0x9ce68086, SPI_LYNXPOINT, "Intel Wildcat Point SPI Controller-1" },
53 	{ 0x9d298086, SPI_SUNRISEPOINT, "Intel Sunrise Point-LP SPI Controller-0" },
54 	{ 0x9d2a8086, SPI_SUNRISEPOINT, "Intel Sunrise Point-LP SPI Controller-1" },
55 	{ 0xa1298086, SPI_SUNRISEPOINT, "Intel Sunrise Point-H SPI Controller-0" },
56 	{ 0xa12a8086, SPI_SUNRISEPOINT, "Intel Sunrise Point-H SPI Controller-1" },
57 	{ 0xa2a98086, SPI_SUNRISEPOINT, "Intel Kaby Lake-H SPI Controller-0" },
58 	{ 0xa2aa8086, SPI_SUNRISEPOINT, "Intel Kaby Lake-H SPI Controller-1" },
59 	{ 0xa3a98086, SPI_SUNRISEPOINT, "Intel Comet Lake-V SPI Controller-0" },
60 	{ 0xa3aa8086, SPI_SUNRISEPOINT, "Intel Comet Lake-V SPI Controller-1" },
61 };
62 
63 static int
intelspi_pci_probe(device_t dev)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_pci_devices[i].desc);
77 			return (BUS_PROBE_DEFAULT);
78 		}
79 	}
80 
81 	return (ENXIO);
82 }
83 
84 static int
intelspi_pci_attach(device_t dev)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
intelspi_pci_detach(device_t dev)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 	/* Bus interface */
123 	DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
124 	DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
125 	DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
126 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
127 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
128 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
129 	DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
130 
131 	/* SPI interface */
132 	DEVMETHOD(spibus_transfer, intelspi_transfer),
133 
134 	DEVMETHOD_END
135 };
136 
137 static driver_t intelspi_pci_driver = {
138 	"spi",
139 	intelspi_pci_methods,
140 	sizeof(struct intelspi_softc),
141 };
142 
143 DRIVER_MODULE(intelspi, pci, intelspi_pci_driver, 0, 0);
144 MODULE_DEPEND(intelspi, pci, 1, 1, 1);
145 MODULE_DEPEND(intelspi, spibus, 1, 1, 1);
146 MODULE_PNP_INFO("W32:vendor/device", pci, intelspi, intelspi_pci_devices,
147     nitems(intelspi_pci_devices));
148