Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 2 | /* |
Simon Glass | 07470d6 | 2015-08-22 18:31:21 -0600 | [diff] [blame] | 3 | * Copyright (C) 2013 Guntermann & Drunck, GmbH |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 4 | * |
Mario Six | d38826a | 2018-03-06 08:04:58 +0100 | [diff] [blame] | 5 | * Written by Dirk Eibach <dirk.eibach@gdsys.cc> |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 9 | #include <dm.h> |
Miquel Raynal | d677bfe | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 10 | #include <tpm-v1.h> |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 11 | #include <i2c.h> |
| 12 | #include <asm/unaligned.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 14 | |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 15 | #include "tpm_internal.h" |
| 16 | |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 17 | #define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but |
| 18 | generating/exporting keys */ |
| 19 | |
| 20 | /* |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 21 | * tpm_atmel_twi_open() |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 22 | * |
| 23 | * Requests access to locality 0 for the caller. After all commands have been |
| 24 | * completed the caller is supposed to call tis_close(). |
| 25 | * |
| 26 | * Returns 0 on success, -1 on failure. |
| 27 | */ |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 28 | static int tpm_atmel_twi_open(struct udevice *dev) |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 29 | { |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | /* |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 34 | * tpm_atmel_twi_close() |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 35 | * |
| 36 | * terminate the currect session with the TPM by releasing the locked |
| 37 | * locality. Returns 0 on success of -1 on failure (in case lock |
| 38 | * removal did not succeed). |
| 39 | */ |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 40 | static int tpm_atmel_twi_close(struct udevice *dev) |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /* |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 46 | * tpm_atmel_twi_get_desc() |
| 47 | * |
| 48 | * @dev: Device to check |
| 49 | * @buf: Buffer to put the string |
| 50 | * @size: Maximum size of buffer |
| 51 | * @return length of string, or -ENOSPC it no space |
| 52 | */ |
| 53 | static int tpm_atmel_twi_get_desc(struct udevice *dev, char *buf, int size) |
| 54 | { |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * tpm_atmel_twi_xfer() |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 60 | * |
| 61 | * Send the requested data to the TPM and then try to get its response |
| 62 | * |
| 63 | * @sendbuf - buffer of the data to send |
| 64 | * @send_size size of the data to send |
| 65 | * @recvbuf - memory to save the response to |
| 66 | * @recv_len - pointer to the size of the response buffer |
| 67 | * |
| 68 | * Returns 0 on success (and places the number of response bytes at recv_len) |
| 69 | * or -1 on failure. |
| 70 | */ |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 71 | static int tpm_atmel_twi_xfer(struct udevice *dev, |
| 72 | const uint8_t *sendbuf, size_t send_size, |
| 73 | uint8_t *recvbuf, size_t *recv_len) |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 74 | { |
| 75 | int res; |
| 76 | unsigned long start; |
| 77 | |
| 78 | #ifdef DEBUG |
| 79 | memset(recvbuf, 0xcc, *recv_len); |
| 80 | printf("send to TPM (%d bytes, recv_len=%d):\n", send_size, *recv_len); |
| 81 | print_buffer(0, (void *)sendbuf, 1, send_size, 0); |
| 82 | #endif |
| 83 | |
Igor Opaniuk | 2147a16 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 84 | #if !CONFIG_IS_ENABLED(DM_I2C) |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 85 | res = i2c_write(0x29, 0, 0, (uchar *)sendbuf, send_size); |
mario.six@gdsys.cc | 03dcd41 | 2016-07-18 13:47:45 +0200 | [diff] [blame] | 86 | #else |
| 87 | res = dm_i2c_write(dev, 0, sendbuf, send_size); |
| 88 | #endif |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 89 | if (res) { |
| 90 | printf("i2c_write returned %d\n", res); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | start = get_timer(0); |
Igor Opaniuk | 2147a16 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 95 | #if !CONFIG_IS_ENABLED(DM_I2C) |
mario.six@gdsys.cc | 03dcd41 | 2016-07-18 13:47:45 +0200 | [diff] [blame] | 96 | while ((res = i2c_read(0x29, 0, 0, recvbuf, 10))) |
| 97 | #else |
| 98 | while ((res = dm_i2c_read(dev, 0, recvbuf, 10))) |
| 99 | #endif |
| 100 | { |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 101 | /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */ |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 102 | if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) { |
| 103 | puts("tpm timed out\n"); |
| 104 | return -1; |
| 105 | } |
| 106 | udelay(100); |
| 107 | } |
| 108 | if (!res) { |
Jeremy Boone | b3f4070 | 2018-02-12 17:56:37 -0500 | [diff] [blame] | 109 | unsigned int hdr_recv_len; |
| 110 | hdr_recv_len = get_unaligned_be32(recvbuf + 2); |
| 111 | if (hdr_recv_len < 10) { |
| 112 | puts("tpm response header too small\n"); |
| 113 | return -1; |
| 114 | } else if (hdr_recv_len > *recv_len) { |
| 115 | puts("tpm response length is bigger than receive buffer\n"); |
| 116 | return -1; |
| 117 | } else { |
| 118 | *recv_len = hdr_recv_len; |
Igor Opaniuk | 2147a16 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 119 | #if !CONFIG_IS_ENABLED(DM_I2C) |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 120 | res = i2c_read(0x29, 0, 0, recvbuf, *recv_len); |
mario.six@gdsys.cc | 03dcd41 | 2016-07-18 13:47:45 +0200 | [diff] [blame] | 121 | #else |
| 122 | res = dm_i2c_read(dev, 0, recvbuf, *recv_len); |
| 123 | #endif |
Jeremy Boone | b3f4070 | 2018-02-12 17:56:37 -0500 | [diff] [blame] | 124 | |
| 125 | } |
Dirk Eibach | c01939c | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 126 | } |
| 127 | if (res) { |
| 128 | printf("i2c_read returned %d (rlen=%d)\n", res, *recv_len); |
| 129 | #ifdef DEBUG |
| 130 | print_buffer(0, recvbuf, 1, *recv_len, 0); |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | #ifdef DEBUG |
| 135 | if (!res) { |
| 136 | printf("read from TPM (%d bytes):\n", *recv_len); |
| 137 | print_buffer(0, recvbuf, 1, *recv_len, 0); |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | return res; |
| 142 | } |
Christophe Ricard | 302c5db | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 143 | |
| 144 | static int tpm_atmel_twi_probe(struct udevice *dev) |
| 145 | { |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static const struct udevice_id tpm_atmel_twi_ids[] = { |
| 150 | { .compatible = "atmel,at97sc3204t"}, |
| 151 | { } |
| 152 | }; |
| 153 | |
| 154 | static const struct tpm_ops tpm_atmel_twi_ops = { |
| 155 | .open = tpm_atmel_twi_open, |
| 156 | .close = tpm_atmel_twi_close, |
| 157 | .xfer = tpm_atmel_twi_xfer, |
| 158 | .get_desc = tpm_atmel_twi_get_desc, |
| 159 | }; |
| 160 | |
| 161 | U_BOOT_DRIVER(tpm_atmel_twi) = { |
| 162 | .name = "tpm_atmel_twi", |
| 163 | .id = UCLASS_TPM, |
| 164 | .of_match = tpm_atmel_twi_ids, |
| 165 | .ops = &tpm_atmel_twi_ops, |
| 166 | .probe = tpm_atmel_twi_probe, |
| 167 | }; |