xref: /freebsd/sys/dev/tpm/tpm_bus.c (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
1 /*-
2  * Copyright (c) 2023 Juniper Networks, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/types.h>
29 #include <sys/bus.h>
30 #include "tpm_if.h"
31 #include "tpm20.h"
32 
33 /* Override accessors */
34 static uint8_t
35 tpm_read_1(device_t dev, bus_size_t off)
36 {
37 	struct tpm_sc *sc = device_get_softc(dev);
38 
39 	return (bus_read_1(sc->mem_res, off));
40 }
41 
42 static uint32_t
43 tpm_read_4(device_t dev, bus_size_t off)
44 {
45 	struct tpm_sc *sc = device_get_softc(dev);
46 
47 	return (bus_read_4(sc->mem_res, off));
48 }
49 
50 /*
51  * Only i386 is missing bus_space_read_8.
52  */
53 #ifndef __i386__
54 static uint64_t
55 tpm_read_8(device_t dev, bus_size_t off)
56 {
57 	struct tpm_sc *sc = device_get_softc(dev);
58 
59 	return (bus_read_8(sc->mem_res, off));
60 }
61 #endif
62 
63 static void
64 tpm_write_1(device_t dev, bus_size_t off, uint8_t val)
65 {
66 	struct tpm_sc *sc = device_get_softc(dev);
67 
68 	bus_write_1(sc->mem_res, off, val);
69 }
70 
71 static void
72 tpm_write_4(device_t dev, bus_size_t off, uint32_t val)
73 {
74 	struct tpm_sc *sc = device_get_softc(dev);
75 
76 	bus_write_4(sc->mem_res, off, val);
77 }
78 
79 static void
80 tpm_write_barrier(device_t dev, bus_size_t off, bus_size_t length)
81 {
82 	struct tpm_sc *sc = device_get_softc(dev);
83 
84 	bus_barrier(sc->mem_res, off, length, BUS_SPACE_BARRIER_WRITE);
85 }
86 
87 static device_method_t tpm_bus_methods[] = {
88 	DEVMETHOD(tpm_read_1,	tpm_read_1),
89 	DEVMETHOD(tpm_read_4,	tpm_read_4),
90 #ifndef __i386__
91 	DEVMETHOD(tpm_read_8,	tpm_read_8),
92 #endif
93 	DEVMETHOD(tpm_write_1,	tpm_write_1),
94 	DEVMETHOD(tpm_write_4,	tpm_write_4),
95 	DEVMETHOD(tpm_write_barrier,	tpm_write_barrier),
96 	DEVMETHOD_END
97 };
98 
99 DEFINE_CLASS_0(tpm_lbc, tpm_bus_driver, tpm_bus_methods, sizeof(struct tpm_sc));
100