uboot/drivers/tpm/tpm_tis.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   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.
  16 */
  17
  18#ifndef _TPM_TIS_I2C_H
  19#define _TPM_TIS_I2C_H
  20
  21#include <linux/compiler.h>
  22#include <linux/types.h>
  23
  24/**
  25 * struct tpm_tis_phy_ops - low-level TPM bus operations
  26 */
  27struct 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
  70enum 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
  91enum tpm_timeout {
  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,
  97};
  98
  99/* Size of external transmit buffer (used in tpm_transmit)*/
 100#define TPM_BUFSIZE 4096
 101
 102/* Index of Count field in TPM response buffer */
 103#define TPM_RSP_SIZE_BYTE       2
 104#define TPM_RSP_RC_BYTE         6
 105
 106struct tpm_chip {
 107        int is_open;
 108        int locality;
 109        u32 vend_dev;
 110        u8 rid;
 111        unsigned long timeout_a, timeout_b, timeout_c, timeout_d;  /* msec */
 112        ulong chip_type;
 113        struct tpm_tis_phy_ops *phy_ops;
 114};
 115
 116struct tpm_input_header {
 117        __be16 tag;
 118        __be32 length;
 119        __be32 ordinal;
 120} __packed;
 121
 122struct tpm_output_header {
 123        __be16 tag;
 124        __be32 length;
 125        __be32 return_code;
 126} __packed;
 127
 128struct timeout_t {
 129        __be32 a;
 130        __be32 b;
 131        __be32 c;
 132        __be32 d;
 133} __packed;
 134
 135struct duration_t {
 136        __be32 tpm_short;
 137        __be32 tpm_medium;
 138        __be32 tpm_long;
 139} __packed;
 140
 141union cap_t {
 142        struct timeout_t timeout;
 143        struct duration_t duration;
 144};
 145
 146struct tpm_getcap_params_in {
 147        __be32 cap;
 148        __be32 subcap_size;
 149        __be32 subcap;
 150} __packed;
 151
 152struct tpm_getcap_params_out {
 153        __be32 cap_size;
 154        union cap_t cap;
 155} __packed;
 156
 157union tpm_cmd_header {
 158        struct tpm_input_header in;
 159        struct tpm_output_header out;
 160};
 161
 162union tpm_cmd_params {
 163        struct tpm_getcap_params_out getcap_out;
 164        struct tpm_getcap_params_in getcap_in;
 165};
 166
 167struct tpm_cmd_t {
 168        union tpm_cmd_header header;
 169        union tpm_cmd_params params;
 170} __packed;
 171
 172/* Max number of iterations after i2c NAK */
 173#define MAX_COUNT               3
 174
 175#ifndef __TPM_V2_H
 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
 185enum 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
 192enum 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};
 199#endif
 200
 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 */
 208int 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 */
 216int 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 */
 223int 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 */
 233int 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 */
 243int 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 */
 253int 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 */
 261int 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 */
 268void tpm_tis_ops_register(struct udevice *udev, struct tpm_tis_phy_ops *ops);
 269#endif
 270