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 | |
| 8 | #include <common.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> |
| 12 | #include <asm/unaligned.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Dirk Eibach | 2048909 | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 14 | |
Christophe Ricard | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 15 | #include "tpm_internal.h" |
| 16 | |
Dirk Eibach | 2048909 | 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 | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 21 | * tpm_atmel_twi_open() |
Dirk Eibach | 2048909 | 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 | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 28 | static int tpm_atmel_twi_open(struct udevice *dev) |
Dirk Eibach | 2048909 | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 29 | { |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | /* |
Christophe Ricard | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 34 | * tpm_atmel_twi_close() |
Dirk Eibach | 2048909 | 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 | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 40 | static int tpm_atmel_twi_close(struct udevice *dev) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | /* |
| 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 |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 51 | * Return: length of string, or -ENOSPC it no space |
Christophe Ricard | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 52 | */ |
| 53 | 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] | 54 | { |
Mathew McBride | 9b4cd47 | 2021-11-11 04:06:30 +0000 | [diff] [blame] | 55 | if (size < 50) |
| 56 | return -ENOSPC; |
| 57 | |
| 58 | 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] | 59 | } |
| 60 | |
| 61 | /* |
Christophe Ricard | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 62 | * tpm_atmel_twi_xfer() |
Dirk Eibach | 2048909 | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 63 | * |
| 64 | * Send the requested data to the TPM and then try to get its response |
| 65 | * |
| 66 | * @sendbuf - buffer of the data to send |
| 67 | * @send_size size of the data to send |
| 68 | * @recvbuf - memory to save the response to |
| 69 | * @recv_len - pointer to the size of the response buffer |
| 70 | * |
| 71 | * Returns 0 on success (and places the number of response bytes at recv_len) |
| 72 | * or -1 on failure. |
| 73 | */ |
Christophe Ricard | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 74 | static int tpm_atmel_twi_xfer(struct udevice *dev, |
| 75 | const uint8_t *sendbuf, size_t send_size, |
| 76 | uint8_t *recvbuf, size_t *recv_len) |
Dirk Eibach | 2048909 | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 77 | { |
| 78 | int res; |
| 79 | unsigned long start; |
| 80 | |
| 81 | #ifdef DEBUG |
| 82 | memset(recvbuf, 0xcc, *recv_len); |
| 83 | printf("send to TPM (%d bytes, recv_len=%d):\n", send_size, *recv_len); |
| 84 | print_buffer(0, (void *)sendbuf, 1, send_size, 0); |
| 85 | #endif |
| 86 | |
mario.six@gdsys.cc | ec951ad | 2016-07-18 13:47:45 +0200 | [diff] [blame] | 87 | res = dm_i2c_write(dev, 0, sendbuf, send_size); |
Dirk Eibach | 2048909 | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 88 | if (res) { |
| 89 | printf("i2c_write returned %d\n", res); |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | start = get_timer(0); |
Mathew McBride | 16bfe89 | 2021-11-11 04:06:28 +0000 | [diff] [blame] | 94 | |
mario.six@gdsys.cc | ec951ad | 2016-07-18 13:47:45 +0200 | [diff] [blame] | 95 | while ((res = dm_i2c_read(dev, 0, recvbuf, 10))) |
mario.six@gdsys.cc | ec951ad | 2016-07-18 13:47:45 +0200 | [diff] [blame] | 96 | { |
Christophe Ricard | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 97 | /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */ |
Dirk Eibach | 2048909 | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 98 | if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) { |
| 99 | puts("tpm timed out\n"); |
| 100 | return -1; |
| 101 | } |
| 102 | udelay(100); |
| 103 | } |
| 104 | if (!res) { |
Jeremy Boone | 10e3d76 | 2018-02-12 17:56:37 -0500 | [diff] [blame] | 105 | unsigned int hdr_recv_len; |
| 106 | hdr_recv_len = get_unaligned_be32(recvbuf + 2); |
| 107 | if (hdr_recv_len < 10) { |
| 108 | puts("tpm response header too small\n"); |
| 109 | return -1; |
| 110 | } else if (hdr_recv_len > *recv_len) { |
| 111 | puts("tpm response length is bigger than receive buffer\n"); |
| 112 | return -1; |
| 113 | } else { |
| 114 | *recv_len = hdr_recv_len; |
mario.six@gdsys.cc | ec951ad | 2016-07-18 13:47:45 +0200 | [diff] [blame] | 115 | res = dm_i2c_read(dev, 0, recvbuf, *recv_len); |
Jeremy Boone | 10e3d76 | 2018-02-12 17:56:37 -0500 | [diff] [blame] | 116 | } |
Dirk Eibach | 2048909 | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 117 | } |
| 118 | if (res) { |
Mathew McBride | 7a4038b | 2021-11-11 04:06:31 +0000 | [diff] [blame] | 119 | printf("i2c_read returned %d (rlen=%zu)\n", res, *recv_len); |
Dirk Eibach | 2048909 | 2013-06-26 15:55:15 +0200 | [diff] [blame] | 120 | #ifdef DEBUG |
| 121 | print_buffer(0, recvbuf, 1, *recv_len, 0); |
| 122 | #endif |
| 123 | } |
| 124 | |
| 125 | #ifdef DEBUG |
| 126 | if (!res) { |
| 127 | printf("read from TPM (%d bytes):\n", *recv_len); |
| 128 | print_buffer(0, recvbuf, 1, *recv_len, 0); |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | return res; |
| 133 | } |
Christophe Ricard | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 134 | |
| 135 | static int tpm_atmel_twi_probe(struct udevice *dev) |
| 136 | { |
Mathew McBride | 175802c | 2021-11-11 04:06:29 +0000 | [diff] [blame] | 137 | i2c_set_chip_offset_len(dev, 0); |
Christophe Ricard | 587bf7a | 2015-10-06 22:54:42 +0200 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static const struct udevice_id tpm_atmel_twi_ids[] = { |
| 142 | { .compatible = "atmel,at97sc3204t"}, |
| 143 | { } |
| 144 | }; |
| 145 | |
| 146 | static const struct tpm_ops tpm_atmel_twi_ops = { |
| 147 | .open = tpm_atmel_twi_open, |
| 148 | .close = tpm_atmel_twi_close, |
| 149 | .xfer = tpm_atmel_twi_xfer, |
| 150 | .get_desc = tpm_atmel_twi_get_desc, |
| 151 | }; |
| 152 | |
| 153 | U_BOOT_DRIVER(tpm_atmel_twi) = { |
| 154 | .name = "tpm_atmel_twi", |
| 155 | .id = UCLASS_TPM, |
| 156 | .of_match = tpm_atmel_twi_ids, |
| 157 | .ops = &tpm_atmel_twi_ops, |
| 158 | .probe = tpm_atmel_twi_probe, |
| 159 | }; |