xref: /freebsd/sys/dev/intel/spi_acpi.c (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
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 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/proc.h>
35 #include <sys/rman.h>
36 
37 #include <dev/intel/spi.h>
38 
39 #include "spibus_if.h"
40 
41 static const struct intelspi_acpi_device {
42 	const char *hid;
43 	enum intelspi_vers vers;
44 	const char *desc;
45 } intelspi_acpi_devices[] = {
46 	{ "80860F0E", SPI_BAYTRAIL, "Intel Bay Trail SPI Controller" },
47 	{ "8086228E", SPI_BRASWELL, "Intel Braswell SPI Controller" },
48 };
49 
50 static char *intelspi_ids[] = { "80860F0E", "8086228E", NULL };
51 
52 static int
53 intelspi_acpi_probe(device_t dev)
54 {
55 	struct intelspi_softc *sc = device_get_softc(dev);
56 	char *hid;
57 	int i;
58 
59 	if (acpi_disabled("spi"))
60 		return (ENXIO);
61 
62 	if (ACPI_ID_PROBE(device_get_parent(dev), dev, intelspi_ids, &hid) > 0)
63 		return (ENXIO);
64 
65 	for (i = 0; i < nitems(intelspi_acpi_devices); i++) {
66 		if (strcmp(intelspi_acpi_devices[i].hid, hid) == 0) {
67 			sc->sc_vers = intelspi_acpi_devices[i].vers;
68 			sc->sc_handle = acpi_get_handle(dev);
69 			device_set_desc(dev, intelspi_acpi_devices[i].desc);
70 			return (BUS_PROBE_DEFAULT);
71 		}
72 	}
73 
74 	return (ENXIO);
75 }
76 
77 static int
78 intelspi_acpi_attach(device_t dev)
79 {
80 	struct intelspi_softc *sc = device_get_softc(dev);
81 
82 	sc->sc_mem_rid = 0;
83 	sc->sc_irq_rid = 0;
84 
85 	return (intelspi_attach(dev));
86 }
87 
88 static device_method_t intelspi_acpi_methods[] = {
89 	/* Device interface */
90 	DEVMETHOD(device_probe, intelspi_acpi_probe),
91 	DEVMETHOD(device_attach, intelspi_acpi_attach),
92 	DEVMETHOD(device_detach, intelspi_detach),
93 	DEVMETHOD(device_suspend, intelspi_suspend),
94 	DEVMETHOD(device_resume, intelspi_resume),
95 
96 	/* Bus interface */
97 	DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
98 	DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
99 	DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
100 	DEVMETHOD(bus_release_resource, bus_generic_release_resource),
101 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
102 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
103 	DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
104 
105 	/* SPI interface */
106 	DEVMETHOD(spibus_transfer, intelspi_transfer),
107 
108 	DEVMETHOD_END
109 };
110 
111 static driver_t intelspi_acpi_driver = {
112 	"spi",
113 	intelspi_acpi_methods,
114 	sizeof(struct intelspi_softc),
115 };
116 
117 DRIVER_MODULE(intelspi, acpi, intelspi_acpi_driver, 0, 0);
118 MODULE_DEPEND(intelspi, acpi, 1, 1, 1);
119 MODULE_DEPEND(intelspi, spibus, 1, 1, 1);
120 ACPI_PNP_INFO(intelspi_ids);
121