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