Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Infineon Technologies |
| 4 | * |
| 5 | * Authors: |
| 6 | * Peter Huewe <huewe.external@infineon.com> |
| 7 | * |
| 8 | * Version: 2.1.1 |
| 9 | * |
| 10 | * Description: |
| 11 | * Device driver for TCG/TCPA TPM (trusted platform module). |
| 12 | * Specifications at www.trustedcomputinggroup.org |
| 13 | * |
| 14 | * It is based on the Linux kernel driver tpm.c from Leendert van |
| 15 | * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall. |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Simon Glass | 4cd7b78 | 2015-08-22 18:31:22 -0600 | [diff] [blame] | 18 | #ifndef _TPM_TIS_I2C_H |
| 19 | #define _TPM_TIS_I2C_H |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 20 | |
| 21 | #include <linux/compiler.h> |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 22 | #include <linux/types.h> |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 23 | |
Ilias Apalodimas | 2c9626c | 2021-11-09 09:02:17 +0200 | [diff] [blame] | 24 | /** |
| 25 | * struct tpm_tis_phy_ops - low-level TPM bus operations |
| 26 | */ |
| 27 | struct tpm_tis_phy_ops { |
| 28 | /* read_bytes() - Read a number of bytes from the device |
| 29 | * |
| 30 | * @udev: TPM device |
| 31 | * @addr: offset from device base |
| 32 | * @len: len to read |
| 33 | * @result: data read |
| 34 | * |
| 35 | * @return: 0 on success, negative on failure |
| 36 | */ |
| 37 | int (*read_bytes)(struct udevice *udev, u32 addr, u16 len, |
| 38 | u8 *result); |
| 39 | /* write_bytes() - Read a number of bytes from the device |
| 40 | * |
| 41 | * @udev: TPM device |
| 42 | * @addr: offset from device base |
| 43 | * @len: len to read |
| 44 | * @value: data to write |
| 45 | * |
| 46 | * @return: 0 on success, negative on failure |
| 47 | */ |
| 48 | int (*write_bytes)(struct udevice *udev, u32 addr, u16 len, |
| 49 | const u8 *value); |
| 50 | /* read32() - Read a 32bit value of the device |
| 51 | * |
| 52 | * @udev: TPM device |
| 53 | * @addr: offset from device base |
| 54 | * @result: data read |
| 55 | * |
| 56 | * @return: 0 on success, negative on failure |
| 57 | */ |
| 58 | int (*read32)(struct udevice *udev, u32 addr, u32 *result); |
| 59 | /* write32() - write a 32bit value to the device |
| 60 | * |
| 61 | * @udev: TPM device |
| 62 | * @addr: offset from device base |
| 63 | * @src: data to write |
| 64 | * |
| 65 | * @return: 0 on success, negative on failure |
| 66 | */ |
| 67 | int (*write32)(struct udevice *udev, u32 addr, u32 src); |
| 68 | }; |
| 69 | |
| 70 | enum tis_int_flags { |
| 71 | TPM_GLOBAL_INT_ENABLE = 0x80000000, |
| 72 | TPM_INTF_BURST_COUNT_STATIC = 0x100, |
| 73 | TPM_INTF_CMD_READY_INT = 0x080, |
| 74 | TPM_INTF_INT_EDGE_FALLING = 0x040, |
| 75 | TPM_INTF_INT_EDGE_RISING = 0x020, |
| 76 | TPM_INTF_INT_LEVEL_LOW = 0x010, |
| 77 | TPM_INTF_INT_LEVEL_HIGH = 0x008, |
| 78 | TPM_INTF_LOCALITY_CHANGE_INT = 0x004, |
| 79 | TPM_INTF_STS_VALID_INT = 0x002, |
| 80 | TPM_INTF_DATA_AVAIL_INT = 0x001, |
| 81 | }; |
| 82 | |
| 83 | #define TPM_ACCESS(l) (0x0000 | ((l) << 12)) |
| 84 | #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12)) |
| 85 | #define TPM_STS(l) (0x0018 | ((l) << 12)) |
| 86 | #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12)) |
| 87 | #define TPM_DID_VID(l) (0x0f00 | ((l) << 12)) |
| 88 | #define TPM_RID(l) (0x0f04 | ((l) << 12)) |
| 89 | #define TPM_INTF_CAPS(l) (0x0014 | ((l) << 12)) |
| 90 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 91 | enum tpm_timeout { |
Simon Glass | 42c8ec5 | 2015-08-22 18:31:30 -0600 | [diff] [blame] | 92 | TPM_TIMEOUT_MS = 5, |
| 93 | TIS_SHORT_TIMEOUT_MS = 750, |
| 94 | TIS_LONG_TIMEOUT_MS = 2000, |
| 95 | SLEEP_DURATION_US = 60, |
| 96 | SLEEP_DURATION_LONG_US = 210, |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | /* Size of external transmit buffer (used in tpm_transmit)*/ |
| 100 | #define TPM_BUFSIZE 4096 |
| 101 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 102 | /* Index of Count field in TPM response buffer */ |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 103 | #define TPM_RSP_SIZE_BYTE 2 |
| 104 | #define TPM_RSP_RC_BYTE 6 |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 105 | |
Simon Glass | 7c73537 | 2015-08-22 18:31:24 -0600 | [diff] [blame] | 106 | struct tpm_chip { |
| 107 | int is_open; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 108 | int locality; |
Simon Glass | b697e0f | 2015-08-22 18:31:38 -0600 | [diff] [blame] | 109 | u32 vend_dev; |
Miquel Raynal | 06425aa | 2018-05-15 11:57:04 +0200 | [diff] [blame] | 110 | u8 rid; |
Tom Wai-Hong Tam | 1b393db | 2013-04-12 11:04:37 +0000 | [diff] [blame] | 111 | unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* msec */ |
Christophe Ricard | 1259dcd | 2016-01-21 23:27:12 +0100 | [diff] [blame] | 112 | ulong chip_type; |
Ilias Apalodimas | 2c9626c | 2021-11-09 09:02:17 +0200 | [diff] [blame] | 113 | struct tpm_tis_phy_ops *phy_ops; |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 116 | struct tpm_input_header { |
| 117 | __be16 tag; |
| 118 | __be32 length; |
| 119 | __be32 ordinal; |
| 120 | } __packed; |
| 121 | |
| 122 | struct tpm_output_header { |
| 123 | __be16 tag; |
| 124 | __be32 length; |
| 125 | __be32 return_code; |
| 126 | } __packed; |
| 127 | |
| 128 | struct timeout_t { |
| 129 | __be32 a; |
| 130 | __be32 b; |
| 131 | __be32 c; |
| 132 | __be32 d; |
| 133 | } __packed; |
| 134 | |
| 135 | struct duration_t { |
| 136 | __be32 tpm_short; |
| 137 | __be32 tpm_medium; |
| 138 | __be32 tpm_long; |
| 139 | } __packed; |
| 140 | |
| 141 | union cap_t { |
| 142 | struct timeout_t timeout; |
| 143 | struct duration_t duration; |
| 144 | }; |
| 145 | |
| 146 | struct tpm_getcap_params_in { |
| 147 | __be32 cap; |
| 148 | __be32 subcap_size; |
| 149 | __be32 subcap; |
| 150 | } __packed; |
| 151 | |
| 152 | struct tpm_getcap_params_out { |
| 153 | __be32 cap_size; |
| 154 | union cap_t cap; |
| 155 | } __packed; |
| 156 | |
| 157 | union tpm_cmd_header { |
| 158 | struct tpm_input_header in; |
| 159 | struct tpm_output_header out; |
| 160 | }; |
| 161 | |
| 162 | union tpm_cmd_params { |
| 163 | struct tpm_getcap_params_out getcap_out; |
| 164 | struct tpm_getcap_params_in getcap_in; |
| 165 | }; |
| 166 | |
| 167 | struct tpm_cmd_t { |
| 168 | union tpm_cmd_header header; |
| 169 | union tpm_cmd_params params; |
| 170 | } __packed; |
| 171 | |
Simon Glass | 13894bd | 2015-08-22 18:31:27 -0600 | [diff] [blame] | 172 | /* Max number of iterations after i2c NAK */ |
| 173 | #define MAX_COUNT 3 |
| 174 | |
Johannes Holland | bedbb38 | 2020-05-11 15:22:25 +0200 | [diff] [blame] | 175 | #ifndef __TPM_V2_H |
Simon Glass | 13894bd | 2015-08-22 18:31:27 -0600 | [diff] [blame] | 176 | /* |
| 177 | * Max number of iterations after i2c NAK for 'long' commands |
| 178 | * |
| 179 | * We need this especially for sending TPM_READY, since the cleanup after the |
| 180 | * transtion to the ready state may take some time, but it is unpredictable |
| 181 | * how long it will take. |
| 182 | */ |
| 183 | #define MAX_COUNT_LONG 50 |
| 184 | |
Simon Glass | 13894bd | 2015-08-22 18:31:27 -0600 | [diff] [blame] | 185 | enum tis_access { |
| 186 | TPM_ACCESS_VALID = 0x80, |
| 187 | TPM_ACCESS_ACTIVE_LOCALITY = 0x20, |
| 188 | TPM_ACCESS_REQUEST_PENDING = 0x04, |
| 189 | TPM_ACCESS_REQUEST_USE = 0x02, |
| 190 | }; |
| 191 | |
| 192 | enum tis_status { |
| 193 | TPM_STS_VALID = 0x80, |
| 194 | TPM_STS_COMMAND_READY = 0x40, |
| 195 | TPM_STS_GO = 0x20, |
| 196 | TPM_STS_DATA_AVAIL = 0x10, |
| 197 | TPM_STS_DATA_EXPECT = 0x08, |
| 198 | }; |
Johannes Holland | bedbb38 | 2020-05-11 15:22:25 +0200 | [diff] [blame] | 199 | #endif |
Simon Glass | 13894bd | 2015-08-22 18:31:27 -0600 | [diff] [blame] | 200 | |
Ilias Apalodimas | 2c9626c | 2021-11-09 09:02:17 +0200 | [diff] [blame] | 201 | /** |
| 202 | * tpm_tis_open - Open the device and request locality 0 |
| 203 | * |
| 204 | * @dev: TPM device |
| 205 | * |
| 206 | * @return: 0 on success, negative on failure |
| 207 | */ |
| 208 | int tpm_tis_open(struct udevice *udev); |
| 209 | /** |
| 210 | * tpm_tis_close - Close the device and release locality |
| 211 | * |
| 212 | * @dev: TPM device |
| 213 | * |
| 214 | * @return: 0 on success, negative on failure |
| 215 | */ |
| 216 | int tpm_tis_close(struct udevice *udev); |
| 217 | /** tpm_tis_cleanup - Get the device in ready state and release locality |
| 218 | * |
| 219 | * @dev: TPM device |
| 220 | * |
| 221 | * @return: always 0 |
| 222 | */ |
| 223 | int tpm_tis_cleanup(struct udevice *udev); |
| 224 | /** |
| 225 | * tpm_tis_send - send data to the device |
| 226 | * |
| 227 | * @dev: TPM device |
| 228 | * @buf: buffer to send |
| 229 | * @len: size of the buffer |
| 230 | * |
| 231 | * @return: number of bytes sent or negative on failure |
| 232 | */ |
| 233 | int tpm_tis_send(struct udevice *udev, const u8 *buf, size_t len); |
| 234 | /** |
| 235 | * tpm_tis_recv_data - Receive data from a device. Wrapper for tpm_tis_recv |
| 236 | * |
| 237 | * @dev: TPM device |
| 238 | * @buf: buffer to copy data |
| 239 | * @size: buffer size |
| 240 | * |
| 241 | * @return: bytes read or negative on failure |
| 242 | */ |
| 243 | int tpm_tis_recv(struct udevice *udev, u8 *buf, size_t count); |
| 244 | /** |
| 245 | * tpm_tis_get_desc - Get the TPM description |
| 246 | * |
| 247 | * @dev: TPM device |
| 248 | * @buf: buffer to fill data |
| 249 | * @size: buffer size |
| 250 | * |
| 251 | * @return: Number of characters written (or would have been written) in buffer |
| 252 | */ |
| 253 | int tpm_tis_get_desc(struct udevice *udev, char *buf, int size); |
| 254 | /** |
| 255 | * tpm_tis_init - inititalize the device |
| 256 | * |
| 257 | * @dev: TPM device |
| 258 | * |
| 259 | * @return: 0 on success, negative on failure |
| 260 | */ |
| 261 | int tpm_tis_init(struct udevice *udev); |
| 262 | /** |
| 263 | * tpm_tis_ops_register - register the PHY ops for the device |
| 264 | * |
| 265 | * @dev: TPM device |
| 266 | * @ops: tpm_tis_phy_ops ops for the device |
| 267 | */ |
| 268 | void tpm_tis_ops_register(struct udevice *udev, struct tpm_tis_phy_ops *ops); |
Rong Chang | f626799 | 2013-04-12 10:44:57 +0000 | [diff] [blame] | 269 | #endif |