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