xref: /freebsd/sys/dev/intel/spi_pci.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 	const char *desc;
49 } intelspi_pci_devices[] = {
50 	{ 0x9c658086, SPI_LYNXPOINT, "Intel Lynx Point-LP SPI Controller-0" },
51 	{ 0x9c668086, SPI_LYNXPOINT, "Intel Lynx Point-LP SPI Controller-1" },
52 	{ 0x9ce58086, SPI_LYNXPOINT, "Intel Wildcat Point SPI Controller-0" },
53 	{ 0x9ce68086, SPI_LYNXPOINT, "Intel Wildcat Point SPI Controller-1" },
54 	{ 0x9d298086, SPI_SUNRISEPOINT, "Intel Sunrise Point-LP SPI Controller-0" },
55 	{ 0x9d2a8086, SPI_SUNRISEPOINT, "Intel Sunrise Point-LP SPI Controller-1" },
56 	{ 0xa1298086, SPI_SUNRISEPOINT, "Intel Sunrise Point-H SPI Controller-0" },
57 	{ 0xa12a8086, SPI_SUNRISEPOINT, "Intel Sunrise Point-H SPI Controller-1" },
58 	{ 0xa2a98086, SPI_SUNRISEPOINT, "Intel Kaby Lake-H SPI Controller-0" },
59 	{ 0xa2aa8086, SPI_SUNRISEPOINT, "Intel Kaby Lake-H SPI Controller-1" },
60 	{ 0xa3a98086, SPI_SUNRISEPOINT, "Intel Comet Lake-V SPI Controller-0" },
61 	{ 0xa3aa8086, SPI_SUNRISEPOINT, "Intel Comet Lake-V SPI Controller-1" },
62 };
63 
64 static int
65 intelspi_pci_probe(device_t dev)
66 {
67 	struct intelspi_softc *sc = device_get_softc(dev);
68 	uint32_t devid = pci_get_devid(dev);
69 	int i;
70 
71 	for (i = 0; i < nitems(intelspi_pci_devices); i++) {
72 		if (intelspi_pci_devices[i].devid == devid) {
73 			sc->sc_vers = intelspi_pci_devices[i].vers;
74 			/* The PCI device is listed in ACPI too.
75 			 * Not that we use the handle for anything... */
76 			sc->sc_handle = acpi_get_handle(dev);
77 			device_set_desc(dev, intelspi_pci_devices[i].desc);
78 			return (BUS_PROBE_DEFAULT);
79 		}
80 	}
81 
82 	return (ENXIO);
83 }
84 
85 static int
86 intelspi_pci_attach(device_t dev)
87 {
88 	struct intelspi_softc *sc = device_get_softc(dev);
89 
90 	sc->sc_mem_rid = PCIR_BAR(0);
91 	sc->sc_irq_rid = 0;
92 	if (pci_alloc_msi(dev, &sc->sc_irq_rid)) {
93 		device_printf(dev, "Using MSI\n");
94 	}
95 
96 	return (intelspi_attach(dev));
97 }
98 
99 static int
100 intelspi_pci_detach(device_t dev)
101 {
102 	struct intelspi_softc *sc = device_get_softc(dev);
103 	int err;
104 
105 	err = intelspi_detach(dev);
106 	if (err)
107 		return (err);
108 
109 	if (sc->sc_irq_rid != 0)
110 		pci_release_msi(dev);
111 
112 	return (0);
113 }
114 
115 static device_method_t intelspi_pci_methods[] = {
116 	/* Device interface */
117 	DEVMETHOD(device_probe, intelspi_pci_probe),
118 	DEVMETHOD(device_attach, intelspi_pci_attach),
119 	DEVMETHOD(device_detach, intelspi_pci_detach),
120 	DEVMETHOD(device_suspend, intelspi_suspend),
121 	DEVMETHOD(device_resume, intelspi_resume),
122 
123 	/* Bus interface */
124 	DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
125 	DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
126 	DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
127 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
128 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
129 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
130 	DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
131 
132 	/* SPI interface */
133 	DEVMETHOD(spibus_transfer, intelspi_transfer),
134 
135 	DEVMETHOD_END
136 };
137 
138 static driver_t intelspi_pci_driver = {
139 	"spi",
140 	intelspi_pci_methods,
141 	sizeof(struct intelspi_softc),
142 };
143 
144 DRIVER_MODULE(intelspi, pci, intelspi_pci_driver, 0, 0);
145 MODULE_DEPEND(intelspi, pci, 1, 1, 1);
146 MODULE_DEPEND(intelspi, spibus, 1, 1, 1);
147 MODULE_PNP_INFO("W32:vendor/device", pci, intelspi, intelspi_pci_devices,
148     nitems(intelspi_pci_devices));
149