xref: /freebsd/sys/dev/tpm/tpm_tis_spibus.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1 /*
2  * Copyright (c) 2023 Juniper Networks, Inc.
3  * All rights reserved.
4  */
5 /*-
6  * Copyright (c) 2018 Stormshield.
7  * Copyright (c) 2018 Semihalf.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /* Based *heavily* on the tpm_tis driver. */
33 
34 #include "opt_platform.h"
35 
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/endian.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/module.h>
44 #include <sys/sx.h>
45 
46 #include <dev/spibus/spi.h>
47 #include <dev/tpm/tpm20.h>
48 
49 #include "spibus_if.h"
50 #include "tpm_if.h"
51 
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 
55 static struct ofw_compat_data compatible_data[] = {
56 	{"infineon,slb9670",	true},
57 	{"tcg,tpm_tis-spi",	true},
58 	{NULL,			false}
59 };
60 
61 static int
62 tpm_spi_probe(device_t dev)
63 {
64 	if (!ofw_bus_status_okay(dev))
65 		return (ENXIO);
66 
67 	if (!ofw_bus_search_compatible(dev, compatible_data)->ocd_data)
68 		return (ENXIO);
69 
70 	device_set_desc(dev, "Trusted Platform Module 2.0, SPI Mode");
71 
72 	return (BUS_PROBE_DEFAULT);
73 }
74 
75 static device_method_t tpm_methods[] = {
76 	DEVMETHOD(device_probe,		tpm_spi_probe),
77 	DEVMETHOD_END
78 };
79 
80 DEFINE_CLASS_2(tpm, tpm_driver, tpm_methods, sizeof(struct tpm_sc),
81     tpmtis_driver, tpm_spi_driver);
82 
83 #if __FreeBSD_version < 1400067
84 static devclass_t tpm_devclass;
85 
86 DRIVER_MODULE(tpm, spibus, tpm_driver, tpm_devclass, NULL, NULL);
87 #else
88 DRIVER_MODULE(tpm, spibus, tpm_driver, NULL, NULL);
89 #endif
90 MODULE_DEPEND(tpm, spibus, 1, 1, 1);
91 MODULE_VERSION(tpm, 1);
92