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