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