Blame view
drivers/tpm/tpm_tis_infineon.c
15.2 KB
f62679981 tpm: Add Infineon... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/* * Copyright (C) 2011 Infineon Technologies * * Authors: * Peter Huewe <huewe.external@infineon.com> * * Description: * Device driver for TCG/TCPA TPM (trusted platform module). * Specifications at www.trustedcomputinggroup.org * * This device driver implements the TPM interface as defined in * the TCG TPM Interface Spec version 1.2, revision 1.0 and the * Infineon I2C Protocol Stack Specification v0.20. * * It is based on the Linux kernel driver tpm.c from Leendert van * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall. * * Version: 2.1.1 * |
07470d6f5 tpm: Convert driv... |
20 |
* SPDX-License-Identifier: GPL-2.0 |
f62679981 tpm: Add Infineon... |
21 22 23 |
*/ #include <common.h> |
f90acf1ad tpm: Support usin... |
24 |
#include <dm.h> |
ec34fa5e4 tpm: Add support ... |
25 |
#include <fdtdec.h> |
f62679981 tpm: Add Infineon... |
26 |
#include <i2c.h> |
1b393db58 tpm: Reorganize t... |
27 |
#include <tpm.h> |
5d97dff04 treewide: replace... |
28 |
#include <linux/errno.h> |
b697e0ff5 dm: tpm: Convert ... |
29 |
#include <linux/compiler.h> |
f62679981 tpm: Add Infineon... |
30 |
#include <linux/types.h> |
1b393db58 tpm: Reorganize t... |
31 |
#include <linux/unaligned/be_byteshift.h> |
f62679981 tpm: Add Infineon... |
32 |
|
1259dcd79 tpm: Rename tpm_t... |
33 |
#include "tpm_tis.h" |
f255d31f9 dm: tpm: Add a uc... |
34 |
#include "tpm_internal.h" |
f62679981 tpm: Add Infineon... |
35 |
|
ec34fa5e4 tpm: Add support ... |
36 |
DECLARE_GLOBAL_DATA_PTR; |
1259dcd79 tpm: Rename tpm_t... |
37 38 39 40 41 42 43 44 45 |
enum i2c_chip_type { SLB9635, SLB9645, UNKNOWN, }; /* expected value for DIDVID register */ #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L |
ec34fa5e4 tpm: Add support ... |
46 47 48 49 50 |
static const char * const chip_name[] = { [SLB9635] = "slb9635tt", [SLB9645] = "slb9645tt", [UNKNOWN] = "unknown/fallback to slb9635", }; |
f62679981 tpm: Add Infineon... |
51 |
|
1259dcd79 tpm: Rename tpm_t... |
52 53 54 55 |
#define TPM_ACCESS(l) (0x0000 | ((l) << 4)) #define TPM_STS(l) (0x0001 | ((l) << 4)) #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) #define TPM_DID_VID(l) (0x0006 | ((l) << 4)) |
f62679981 tpm: Add Infineon... |
56 |
/* |
e56e20eb8 tpm: tpm_tis_i2c:... |
57 |
* tpm_tis_i2c_read() - read from TPM register |
f62679981 tpm: Add Infineon... |
58 59 60 61 62 63 64 65 66 67 68 69 |
* @addr: register address to read from * @buffer: provided by caller * @len: number of bytes to read * * Read len bytes from TPM register and put them into * buffer (little-endian format, i.e. first byte is put into buffer[0]). * * NOTE: TPM is big-endian for multi-byte values. Multi-byte * values have to be swapped. * * Return -EIO on error, 0 on success. */ |
b697e0ff5 dm: tpm: Convert ... |
70 71 |
static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer, size_t len) |
f62679981 tpm: Add Infineon... |
72 |
{ |
b697e0ff5 dm: tpm: Convert ... |
73 |
struct tpm_chip *chip = dev_get_priv(dev); |
f62679981 tpm: Add Infineon... |
74 75 |
int rc; int count; |
1b393db58 tpm: Reorganize t... |
76 |
uint32_t addrbuf = addr; |
f62679981 tpm: Add Infineon... |
77 |
|
b697e0ff5 dm: tpm: Convert ... |
78 |
if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) { |
ec34fa5e4 tpm: Add support ... |
79 80 |
/* slb9635 protocol should work in both cases */ for (count = 0; count < MAX_COUNT; count++) { |
b697e0ff5 dm: tpm: Convert ... |
81 |
rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1); |
ec34fa5e4 tpm: Add support ... |
82 |
if (rc == 0) |
1b393db58 tpm: Reorganize t... |
83 |
break; /* Success, break to skip sleep */ |
42c8ec56c tpm: tpm_tis_i2c:... |
84 |
udelay(SLEEP_DURATION_US); |
ec34fa5e4 tpm: Add support ... |
85 |
} |
ec34fa5e4 tpm: Add support ... |
86 |
if (rc) |
b697e0ff5 dm: tpm: Convert ... |
87 |
return rc; |
ec34fa5e4 tpm: Add support ... |
88 89 90 91 92 93 |
/* After the TPM has successfully received the register address * it needs some time, thus we're sleeping here again, before * retrieving the data */ for (count = 0; count < MAX_COUNT; count++) { |
42c8ec56c tpm: tpm_tis_i2c:... |
94 |
udelay(SLEEP_DURATION_US); |
b697e0ff5 dm: tpm: Convert ... |
95 |
rc = dm_i2c_read(dev, 0, buffer, len); |
ec34fa5e4 tpm: Add support ... |
96 97 98 99 |
if (rc == 0) break; /* success, break to skip sleep */ } } else { |
1b393db58 tpm: Reorganize t... |
100 101 102 |
/* * Use a combined read for newer chips. * Unfortunately the smbus functions are not suitable due to |
ec34fa5e4 tpm: Add support ... |
103 |
* the 32 byte limit of the smbus. |
1b393db58 tpm: Reorganize t... |
104 |
* Retries should usually not be needed, but are kept just to |
ec34fa5e4 tpm: Add support ... |
105 106 107 |
* be safe on the safe side. */ for (count = 0; count < MAX_COUNT; count++) { |
b697e0ff5 dm: tpm: Convert ... |
108 |
rc = dm_i2c_read(dev, addr, buffer, len); |
ec34fa5e4 tpm: Add support ... |
109 110 |
if (rc == 0) break; /* break here to skip sleep */ |
42c8ec56c tpm: tpm_tis_i2c:... |
111 |
udelay(SLEEP_DURATION_US); |
ec34fa5e4 tpm: Add support ... |
112 |
} |
f62679981 tpm: Add Infineon... |
113 |
} |
1b393db58 tpm: Reorganize t... |
114 |
/* Take care of 'guard time' */ |
42c8ec56c tpm: tpm_tis_i2c:... |
115 |
udelay(SLEEP_DURATION_US); |
f62679981 tpm: Add Infineon... |
116 |
if (rc) |
b697e0ff5 dm: tpm: Convert ... |
117 |
return rc; |
f62679981 tpm: Add Infineon... |
118 119 120 |
return 0; } |
b697e0ff5 dm: tpm: Convert ... |
121 122 |
static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr, const u8 *buffer, size_t len, |
42c8ec56c tpm: tpm_tis_i2c:... |
123 |
unsigned int sleep_time_us, u8 max_count) |
f62679981 tpm: Add Infineon... |
124 |
{ |
b697e0ff5 dm: tpm: Convert ... |
125 126 |
struct tpm_chip_priv *priv = dev_get_uclass_priv(dev); struct tpm_chip *chip = dev_get_priv(dev); |
f62679981 tpm: Add Infineon... |
127 128 |
int rc = 0; int count; |
b697e0ff5 dm: tpm: Convert ... |
129 130 131 132 133 134 135 136 |
if (chip->chip_type == SLB9635) { /* Prepare send buffer to include the address */ priv->buf[0] = addr; memcpy(&(priv->buf[1]), buffer, len); buffer = priv->buf; len++; addr = 0; } |
f62679981 tpm: Add Infineon... |
137 |
for (count = 0; count < max_count; count++) { |
b697e0ff5 dm: tpm: Convert ... |
138 |
rc = dm_i2c_write(dev, addr, buffer, len); |
f62679981 tpm: Add Infineon... |
139 |
if (rc == 0) |
1b393db58 tpm: Reorganize t... |
140 |
break; /* Success, break to skip sleep */ |
42c8ec56c tpm: tpm_tis_i2c:... |
141 |
udelay(sleep_time_us); |
f62679981 tpm: Add Infineon... |
142 |
} |
ec34fa5e4 tpm: Add support ... |
143 |
/* take care of 'guard time' */ |
42c8ec56c tpm: tpm_tis_i2c:... |
144 |
udelay(sleep_time_us); |
f62679981 tpm: Add Infineon... |
145 |
if (rc) |
b697e0ff5 dm: tpm: Convert ... |
146 |
return rc; |
f62679981 tpm: Add Infineon... |
147 148 149 150 151 |
return 0; } /* |
e56e20eb8 tpm: tpm_tis_i2c:... |
152 |
* tpm_tis_i2c_write() - write to TPM register |
f62679981 tpm: Add Infineon... |
153 154 155 156 157 158 159 160 161 162 |
* @addr: register address to write to * @buffer: containing data to be written * @len: number of bytes to write * * Write len bytes from provided buffer to TPM register (little * endian format, i.e. buffer[0] is written as first byte). * * NOTE: TPM is big-endian for multi-byte values. Multi-byte * values have to be swapped. * |
e56e20eb8 tpm: tpm_tis_i2c:... |
163 |
* NOTE: use this function instead of the tpm_tis_i2c_write_generic function. |
f62679981 tpm: Add Infineon... |
164 165 166 |
* * Return -EIO on error, 0 on success */ |
b697e0ff5 dm: tpm: Convert ... |
167 168 |
static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer, size_t len) |
f62679981 tpm: Add Infineon... |
169 |
{ |
b697e0ff5 dm: tpm: Convert ... |
170 171 |
return tpm_tis_i2c_write_generic(dev, addr, buffer, len, SLEEP_DURATION_US, MAX_COUNT); |
f62679981 tpm: Add Infineon... |
172 173 174 175 176 |
} /* * This function is needed especially for the cleanup situation after * sending TPM_READY |
1b393db58 tpm: Reorganize t... |
177 |
*/ |
b697e0ff5 dm: tpm: Convert ... |
178 179 |
static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer, size_t len) |
f62679981 tpm: Add Infineon... |
180 |
{ |
b697e0ff5 dm: tpm: Convert ... |
181 |
return tpm_tis_i2c_write_generic(dev, addr, buffer, len, |
42c8ec56c tpm: tpm_tis_i2c:... |
182 183 |
SLEEP_DURATION_LONG_US, MAX_COUNT_LONG); |
f62679981 tpm: Add Infineon... |
184 |
} |
b697e0ff5 dm: tpm: Convert ... |
185 |
static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc) |
f62679981 tpm: Add Infineon... |
186 |
{ |
1b393db58 tpm: Reorganize t... |
187 |
const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID; |
b697e0ff5 dm: tpm: Convert ... |
188 |
struct tpm_chip *chip = dev_get_priv(dev); |
f62679981 tpm: Add Infineon... |
189 190 |
u8 buf; int rc; |
b697e0ff5 dm: tpm: Convert ... |
191 |
rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1); |
f62679981 tpm: Add Infineon... |
192 193 |
if (rc < 0) return rc; |
1b393db58 tpm: Reorganize t... |
194 |
if ((buf & mask) == mask) { |
7c73537e8 tpm: tpm_tis_i2c:... |
195 |
chip->locality = loc; |
f62679981 tpm: Add Infineon... |
196 197 |
return loc; } |
b697e0ff5 dm: tpm: Convert ... |
198 |
return -ENOENT; |
f62679981 tpm: Add Infineon... |
199 |
} |
b697e0ff5 dm: tpm: Convert ... |
200 |
static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc, |
e56e20eb8 tpm: tpm_tis_i2c:... |
201 |
int force) |
f62679981 tpm: Add Infineon... |
202 |
{ |
1b393db58 tpm: Reorganize t... |
203 |
const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID; |
f62679981 tpm: Add Infineon... |
204 |
u8 buf; |
1b393db58 tpm: Reorganize t... |
205 |
|
b697e0ff5 dm: tpm: Convert ... |
206 |
if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0) |
f62679981 tpm: Add Infineon... |
207 |
return; |
1b393db58 tpm: Reorganize t... |
208 |
if (force || (buf & mask) == mask) { |
f62679981 tpm: Add Infineon... |
209 |
buf = TPM_ACCESS_ACTIVE_LOCALITY; |
b697e0ff5 dm: tpm: Convert ... |
210 |
tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1); |
f62679981 tpm: Add Infineon... |
211 212 |
} } |
b697e0ff5 dm: tpm: Convert ... |
213 |
static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc) |
f62679981 tpm: Add Infineon... |
214 |
{ |
b697e0ff5 dm: tpm: Convert ... |
215 |
struct tpm_chip *chip = dev_get_priv(dev); |
f62679981 tpm: Add Infineon... |
216 217 |
unsigned long start, stop; u8 buf = TPM_ACCESS_REQUEST_USE; |
f90acf1ad tpm: Support usin... |
218 |
int rc; |
f62679981 tpm: Add Infineon... |
219 |
|
b697e0ff5 dm: tpm: Convert ... |
220 221 222 223 |
rc = tpm_tis_i2c_check_locality(dev, loc); if (rc >= 0) { debug("%s: Already have locality ", __func__); |
1b393db58 tpm: Reorganize t... |
224 |
return loc; /* We already have the locality */ |
b697e0ff5 dm: tpm: Convert ... |
225 226 227 228 229 |
} else if (rc != -ENOENT) { debug("%s: Failed to get locality: %d ", __func__, rc); return rc; } |
f62679981 tpm: Add Infineon... |
230 |
|
b697e0ff5 dm: tpm: Convert ... |
231 232 233 234 |
rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1); if (rc) { debug("%s: Failed to write to TPM: %d ", __func__, rc); |
f90acf1ad tpm: Support usin... |
235 |
return rc; |
b697e0ff5 dm: tpm: Convert ... |
236 |
} |
f62679981 tpm: Add Infineon... |
237 |
|
1b393db58 tpm: Reorganize t... |
238 |
/* Wait for burstcount */ |
f62679981 tpm: Add Infineon... |
239 |
start = get_timer(0); |
7c73537e8 tpm: tpm_tis_i2c:... |
240 |
stop = chip->timeout_a; |
f62679981 tpm: Add Infineon... |
241 |
do { |
b697e0ff5 dm: tpm: Convert ... |
242 243 244 245 |
rc = tpm_tis_i2c_check_locality(dev, loc); if (rc >= 0) { debug("%s: Have locality ", __func__); |
f62679981 tpm: Add Infineon... |
246 |
return loc; |
b697e0ff5 dm: tpm: Convert ... |
247 248 249 250 251 |
} else if (rc != -ENOENT) { debug("%s: Failed to get locality: %d ", __func__, rc); return rc; } |
42c8ec56c tpm: tpm_tis_i2c:... |
252 |
mdelay(TPM_TIMEOUT_MS); |
f62679981 tpm: Add Infineon... |
253 |
} while (get_timer(start) < stop); |
b697e0ff5 dm: tpm: Convert ... |
254 255 |
debug("%s: Timeout getting locality: %d ", __func__, rc); |
f62679981 tpm: Add Infineon... |
256 |
|
b697e0ff5 dm: tpm: Convert ... |
257 |
return rc; |
f62679981 tpm: Add Infineon... |
258 |
} |
b697e0ff5 dm: tpm: Convert ... |
259 |
static u8 tpm_tis_i2c_status(struct udevice *dev) |
f62679981 tpm: Add Infineon... |
260 |
{ |
b697e0ff5 dm: tpm: Convert ... |
261 |
struct tpm_chip *chip = dev_get_priv(dev); |
1b393db58 tpm: Reorganize t... |
262 |
/* NOTE: Since i2c read may fail, return 0 in this case --> time-out */ |
f62679981 tpm: Add Infineon... |
263 |
u8 buf; |
1b393db58 tpm: Reorganize t... |
264 |
|
b697e0ff5 dm: tpm: Convert ... |
265 |
if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0) |
f62679981 tpm: Add Infineon... |
266 267 268 269 |
return 0; else return buf; } |
b697e0ff5 dm: tpm: Convert ... |
270 |
static int tpm_tis_i2c_ready(struct udevice *dev) |
f62679981 tpm: Add Infineon... |
271 |
{ |
b697e0ff5 dm: tpm: Convert ... |
272 |
struct tpm_chip *chip = dev_get_priv(dev); |
f90acf1ad tpm: Support usin... |
273 |
int rc; |
1b393db58 tpm: Reorganize t... |
274 |
/* This causes the current command to be aborted */ |
f62679981 tpm: Add Infineon... |
275 |
u8 buf = TPM_STS_COMMAND_READY; |
1b393db58 tpm: Reorganize t... |
276 |
|
f90acf1ad tpm: Support usin... |
277 278 |
debug("%s ", __func__); |
b697e0ff5 dm: tpm: Convert ... |
279 |
rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1); |
f90acf1ad tpm: Support usin... |
280 281 282 |
if (rc) debug("%s: rc=%d ", __func__, rc); |
b697e0ff5 dm: tpm: Convert ... |
283 284 |
return rc; |
f62679981 tpm: Add Infineon... |
285 |
} |
b697e0ff5 dm: tpm: Convert ... |
286 |
static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev) |
f62679981 tpm: Add Infineon... |
287 |
{ |
b697e0ff5 dm: tpm: Convert ... |
288 |
struct tpm_chip *chip = dev_get_priv(dev); |
f62679981 tpm: Add Infineon... |
289 290 |
unsigned long start, stop; ssize_t burstcnt; |
1b393db58 tpm: Reorganize t... |
291 |
u8 addr, buf[3]; |
f62679981 tpm: Add Infineon... |
292 |
|
1b393db58 tpm: Reorganize t... |
293 294 |
/* Wait for burstcount */ /* XXX: Which timeout value? Spec has 2 answers (c & d) */ |
f62679981 tpm: Add Infineon... |
295 |
start = get_timer(0); |
7c73537e8 tpm: tpm_tis_i2c:... |
296 |
stop = chip->timeout_d; |
f62679981 tpm: Add Infineon... |
297 298 |
do { /* Note: STS is little endian */ |
7c73537e8 tpm: tpm_tis_i2c:... |
299 |
addr = TPM_STS(chip->locality) + 1; |
b697e0ff5 dm: tpm: Convert ... |
300 |
if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0) |
f62679981 tpm: Add Infineon... |
301 302 303 304 305 306 |
burstcnt = 0; else burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; if (burstcnt) return burstcnt; |
42c8ec56c tpm: tpm_tis_i2c:... |
307 |
mdelay(TPM_TIMEOUT_MS); |
f62679981 tpm: Add Infineon... |
308 309 310 311 |
} while (get_timer(start) < stop); return -EBUSY; } |
b697e0ff5 dm: tpm: Convert ... |
312 |
static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask, |
e56e20eb8 tpm: tpm_tis_i2c:... |
313 |
unsigned long timeout, int *status) |
f62679981 tpm: Add Infineon... |
314 315 |
{ unsigned long start, stop; |
1b393db58 tpm: Reorganize t... |
316 |
/* Check current status */ |
b697e0ff5 dm: tpm: Convert ... |
317 |
*status = tpm_tis_i2c_status(dev); |
f62679981 tpm: Add Infineon... |
318 319 320 321 322 323 |
if ((*status & mask) == mask) return 0; start = get_timer(0); stop = timeout; do { |
42c8ec56c tpm: tpm_tis_i2c:... |
324 |
mdelay(TPM_TIMEOUT_MS); |
b697e0ff5 dm: tpm: Convert ... |
325 |
*status = tpm_tis_i2c_status(dev); |
f62679981 tpm: Add Infineon... |
326 327 |
if ((*status & mask) == mask) return 0; |
f62679981 tpm: Add Infineon... |
328 |
} while (get_timer(start) < stop); |
b697e0ff5 dm: tpm: Convert ... |
329 |
return -ETIMEDOUT; |
f62679981 tpm: Add Infineon... |
330 |
} |
b697e0ff5 dm: tpm: Convert ... |
331 |
static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count) |
f62679981 tpm: Add Infineon... |
332 |
{ |
b697e0ff5 dm: tpm: Convert ... |
333 |
struct tpm_chip *chip = dev_get_priv(dev); |
f62679981 tpm: Add Infineon... |
334 335 336 337 338 |
size_t size = 0; ssize_t burstcnt; int rc; while (size < count) { |
b697e0ff5 dm: tpm: Convert ... |
339 |
burstcnt = tpm_tis_i2c_get_burstcount(dev); |
f62679981 tpm: Add Infineon... |
340 |
|
1b393db58 tpm: Reorganize t... |
341 |
/* burstcount < 0 -> tpm is busy */ |
f62679981 tpm: Add Infineon... |
342 343 |
if (burstcnt < 0) return burstcnt; |
1b393db58 tpm: Reorganize t... |
344 |
/* Limit received data to max left */ |
f62679981 tpm: Add Infineon... |
345 346 |
if (burstcnt > (count - size)) burstcnt = count - size; |
b697e0ff5 dm: tpm: Convert ... |
347 348 |
rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality), &(buf[size]), burstcnt); |
f62679981 tpm: Add Infineon... |
349 350 351 352 353 354 |
if (rc == 0) size += burstcnt; } return size; } |
b697e0ff5 dm: tpm: Convert ... |
355 |
static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count) |
f62679981 tpm: Add Infineon... |
356 |
{ |
b697e0ff5 dm: tpm: Convert ... |
357 |
struct tpm_chip *chip = dev_get_priv(dev); |
f62679981 tpm: Add Infineon... |
358 |
int size = 0; |
afe0e6bdd Infineon TPM: Fix... |
359 360 |
int status; unsigned int expected; |
b697e0ff5 dm: tpm: Convert ... |
361 |
int rc; |
f62679981 tpm: Add Infineon... |
362 |
|
b697e0ff5 dm: tpm: Convert ... |
363 364 365 366 367 368 369 370 371 |
status = tpm_tis_i2c_status(dev); if (status == TPM_STS_COMMAND_READY) return -EINTR; if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) != (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) return -EAGAIN; debug("...got it; "); |
f62679981 tpm: Add Infineon... |
372 |
|
1b393db58 tpm: Reorganize t... |
373 |
/* Read first 10 bytes, including tag, paramsize, and result */ |
b697e0ff5 dm: tpm: Convert ... |
374 |
size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE); |
f62679981 tpm: Add Infineon... |
375 |
if (size < TPM_HEADER_SIZE) { |
b697e0ff5 dm: tpm: Convert ... |
376 377 378 |
debug("Unable to read header "); return size < 0 ? size : -EIO; |
f62679981 tpm: Add Infineon... |
379 380 381 |
} expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE); |
afe0e6bdd Infineon TPM: Fix... |
382 |
if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) { |
b697e0ff5 dm: tpm: Convert ... |
383 384 |
debug("Error size=%x, expected=%x, count=%x ", size, expected, |
f90acf1ad tpm: Support usin... |
385 |
count); |
b697e0ff5 dm: tpm: Convert ... |
386 |
return -ENOSPC; |
f62679981 tpm: Add Infineon... |
387 |
} |
b697e0ff5 dm: tpm: Convert ... |
388 |
size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE], |
e56e20eb8 tpm: tpm_tis_i2c:... |
389 |
expected - TPM_HEADER_SIZE); |
f62679981 tpm: Add Infineon... |
390 |
if (size < expected) { |
b697e0ff5 dm: tpm: Convert ... |
391 392 393 |
debug("Unable to read remainder of result "); return -ETIMEDOUT; |
f62679981 tpm: Add Infineon... |
394 |
} |
b697e0ff5 dm: tpm: Convert ... |
395 396 397 398 |
rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c, &status); if (rc) return rc; |
1b393db58 tpm: Reorganize t... |
399 |
if (status & TPM_STS_DATA_AVAIL) { /* Retry? */ |
b697e0ff5 dm: tpm: Convert ... |
400 401 402 |
debug("Error left over data "); return -EIO; |
f62679981 tpm: Add Infineon... |
403 |
} |
f62679981 tpm: Add Infineon... |
404 405 |
return size; } |
b697e0ff5 dm: tpm: Convert ... |
406 |
static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len) |
f62679981 tpm: Add Infineon... |
407 |
{ |
b697e0ff5 dm: tpm: Convert ... |
408 |
struct tpm_chip *chip = dev_get_priv(dev); |
f62679981 tpm: Add Infineon... |
409 |
int rc, status; |
f90acf1ad tpm: Support usin... |
410 |
size_t burstcnt; |
f62679981 tpm: Add Infineon... |
411 |
size_t count = 0; |
1b393db58 tpm: Reorganize t... |
412 |
int retry = 0; |
f62679981 tpm: Add Infineon... |
413 |
u8 sts = TPM_STS_GO; |
f90acf1ad tpm: Support usin... |
414 415 |
debug("%s: len=%d ", __func__, len); |
1b393db58 tpm: Reorganize t... |
416 417 |
if (len > TPM_DEV_BUFSIZE) return -E2BIG; /* Command is too long for our tpm, sorry */ |
f62679981 tpm: Add Infineon... |
418 |
|
b697e0ff5 dm: tpm: Convert ... |
419 |
if (tpm_tis_i2c_request_locality(dev, 0) < 0) |
f62679981 tpm: Add Infineon... |
420 |
return -EBUSY; |
b697e0ff5 dm: tpm: Convert ... |
421 |
status = tpm_tis_i2c_status(dev); |
f62679981 tpm: Add Infineon... |
422 |
if ((status & TPM_STS_COMMAND_READY) == 0) { |
b697e0ff5 dm: tpm: Convert ... |
423 424 425 426 427 428 429 |
rc = tpm_tis_i2c_ready(dev); if (rc) return rc; rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY, chip->timeout_b, &status); if (rc) return rc; |
f62679981 tpm: Add Infineon... |
430 |
} |
b697e0ff5 dm: tpm: Convert ... |
431 |
burstcnt = tpm_tis_i2c_get_burstcount(dev); |
f62679981 tpm: Add Infineon... |
432 |
|
1b393db58 tpm: Reorganize t... |
433 434 435 |
/* burstcount < 0 -> tpm is busy */ if (burstcnt < 0) return burstcnt; |
f62679981 tpm: Add Infineon... |
436 |
|
f90acf1ad tpm: Support usin... |
437 438 439 440 |
while (count < len) { udelay(300); if (burstcnt > len - count) burstcnt = len - count; |
f62679981 tpm: Add Infineon... |
441 |
|
1b393db58 tpm: Reorganize t... |
442 |
#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION |
b697e0ff5 dm: tpm: Convert ... |
443 444 |
if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN) burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN; |
1b393db58 tpm: Reorganize t... |
445 |
#endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */ |
f62679981 tpm: Add Infineon... |
446 |
|
b697e0ff5 dm: tpm: Convert ... |
447 448 |
rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality), &(buf[count]), burstcnt); |
f62679981 tpm: Add Infineon... |
449 450 |
if (rc == 0) count += burstcnt; |
1b393db58 tpm: Reorganize t... |
451 |
else { |
f90acf1ad tpm: Support usin... |
452 453 |
debug("%s: error ", __func__); |
b697e0ff5 dm: tpm: Convert ... |
454 455 456 |
if (retry++ > 10) return -EIO; rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, |
e56e20eb8 tpm: tpm_tis_i2c:... |
457 458 |
chip->timeout_c, &status); |
f90acf1ad tpm: Support usin... |
459 |
if (rc) |
b697e0ff5 dm: tpm: Convert ... |
460 |
return rc; |
1b393db58 tpm: Reorganize t... |
461 |
|
b697e0ff5 dm: tpm: Convert ... |
462 463 |
if ((status & TPM_STS_DATA_EXPECT) == 0) return -EIO; |
f62679981 tpm: Add Infineon... |
464 465 |
} } |
1b393db58 tpm: Reorganize t... |
466 |
/* Go and do it */ |
b697e0ff5 dm: tpm: Convert ... |
467 468 469 470 471 |
rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1); if (rc < 0) return rc; debug("%s: done, rc=%d ", __func__, rc); |
f62679981 tpm: Add Infineon... |
472 473 |
return len; |
b697e0ff5 dm: tpm: Convert ... |
474 475 476 477 478 |
} static int tpm_tis_i2c_cleanup(struct udevice *dev) { struct tpm_chip *chip = dev_get_priv(dev); |
1b393db58 tpm: Reorganize t... |
479 |
|
b697e0ff5 dm: tpm: Convert ... |
480 |
tpm_tis_i2c_ready(dev); |
1b393db58 tpm: Reorganize t... |
481 482 |
/* * The TPM needs some time to clean up here, |
f62679981 tpm: Add Infineon... |
483 484 |
* so we sleep rather than keeping the bus busy */ |
42c8ec56c tpm: tpm_tis_i2c:... |
485 |
mdelay(2); |
b697e0ff5 dm: tpm: Convert ... |
486 |
tpm_tis_i2c_release_locality(dev, chip->locality, 0); |
f62679981 tpm: Add Infineon... |
487 |
|
b697e0ff5 dm: tpm: Convert ... |
488 |
return 0; |
ec34fa5e4 tpm: Add support ... |
489 |
} |
13932b09b tpm: tpm_tis_i2c:... |
490 |
static int tpm_tis_i2c_init(struct udevice *dev) |
f62679981 tpm: Add Infineon... |
491 |
{ |
b697e0ff5 dm: tpm: Convert ... |
492 |
struct tpm_chip *chip = dev_get_priv(dev); |
f62679981 tpm: Add Infineon... |
493 |
u32 vendor; |
ec34fa5e4 tpm: Add support ... |
494 |
u32 expected_did_vid; |
b697e0ff5 dm: tpm: Convert ... |
495 |
int rc; |
f62679981 tpm: Add Infineon... |
496 |
|
7c73537e8 tpm: tpm_tis_i2c:... |
497 |
chip->is_open = 1; |
f62679981 tpm: Add Infineon... |
498 |
|
e56e20eb8 tpm: tpm_tis_i2c:... |
499 |
/* Default timeouts - these could move to the device tree */ |
42c8ec56c tpm: tpm_tis_i2c:... |
500 501 502 503 |
chip->timeout_a = TIS_SHORT_TIMEOUT_MS; chip->timeout_b = TIS_LONG_TIMEOUT_MS; chip->timeout_c = TIS_SHORT_TIMEOUT_MS; chip->timeout_d = TIS_SHORT_TIMEOUT_MS; |
f62679981 tpm: Add Infineon... |
504 |
|
b697e0ff5 dm: tpm: Convert ... |
505 506 507 |
rc = tpm_tis_i2c_request_locality(dev, 0); if (rc < 0) return rc; |
f62679981 tpm: Add Infineon... |
508 |
|
1b393db58 tpm: Reorganize t... |
509 |
/* Read four bytes from DID_VID register */ |
b697e0ff5 dm: tpm: Convert ... |
510 511 |
if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) { tpm_tis_i2c_release_locality(dev, 0, 1); |
f90acf1ad tpm: Support usin... |
512 |
return -EIO; |
f62679981 tpm: Add Infineon... |
513 |
} |
b697e0ff5 dm: tpm: Convert ... |
514 |
if (chip->chip_type == SLB9635) { |
ec34fa5e4 tpm: Add support ... |
515 516 517 518 519 520 |
vendor = be32_to_cpu(vendor); expected_did_vid = TPM_TIS_I2C_DID_VID_9635; } else { /* device id and byte order has changed for newer i2c tpms */ expected_did_vid = TPM_TIS_I2C_DID_VID_9645; } |
f62679981 tpm: Add Infineon... |
521 |
|
b697e0ff5 dm: tpm: Convert ... |
522 |
if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) { |
9b643e312 treewide: replace... |
523 524 |
pr_err("Vendor id did not match! ID was %08x ", vendor); |
f90acf1ad tpm: Support usin... |
525 |
return -ENODEV; |
f62679981 tpm: Add Infineon... |
526 |
} |
b697e0ff5 dm: tpm: Convert ... |
527 |
chip->vend_dev = vendor; |
1b393db58 tpm: Reorganize t... |
528 529 |
debug("1.2 TPM (chip type %s device-id 0x%X) ", |
b697e0ff5 dm: tpm: Convert ... |
530 |
chip_name[chip->chip_type], vendor >> 16); |
f62679981 tpm: Add Infineon... |
531 532 533 534 535 536 537 |
/* * A timeout query to TPM can be placed here. * Standard timeout values are used so far */ return 0; |
f90acf1ad tpm: Support usin... |
538 |
} |
b697e0ff5 dm: tpm: Convert ... |
539 |
static int tpm_tis_i2c_open(struct udevice *dev) |
4cd7b7834 tpm: Move the I2C... |
540 |
{ |
b697e0ff5 dm: tpm: Convert ... |
541 |
struct tpm_chip *chip = dev_get_priv(dev); |
4cd7b7834 tpm: Move the I2C... |
542 |
int rc; |
4cd7b7834 tpm: Move the I2C... |
543 |
|
b697e0ff5 dm: tpm: Convert ... |
544 545 546 547 548 |
debug("%s: start ", __func__); if (chip->is_open) return -EBUSY; rc = tpm_tis_i2c_init(dev); |
4cd7b7834 tpm: Move the I2C... |
549 |
if (rc < 0) |
b697e0ff5 dm: tpm: Convert ... |
550 |
chip->is_open = 0; |
4cd7b7834 tpm: Move the I2C... |
551 |
|
4cd7b7834 tpm: Move the I2C... |
552 553 |
return rc; } |
b697e0ff5 dm: tpm: Convert ... |
554 |
static int tpm_tis_i2c_close(struct udevice *dev) |
4cd7b7834 tpm: Move the I2C... |
555 |
{ |
b697e0ff5 dm: tpm: Convert ... |
556 |
struct tpm_chip *chip = dev_get_priv(dev); |
4cd7b7834 tpm: Move the I2C... |
557 |
|
b697e0ff5 dm: tpm: Convert ... |
558 559 560 561 |
if (chip->is_open) { tpm_tis_i2c_release_locality(dev, chip->locality, 1); chip->is_open = 0; chip->vend_dev = 0; |
4cd7b7834 tpm: Move the I2C... |
562 563 564 565 |
} return 0; } |
b697e0ff5 dm: tpm: Convert ... |
566 |
static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size) |
4cd7b7834 tpm: Move the I2C... |
567 |
{ |
b697e0ff5 dm: tpm: Convert ... |
568 |
struct tpm_chip *chip = dev_get_priv(dev); |
4cd7b7834 tpm: Move the I2C... |
569 |
|
b697e0ff5 dm: tpm: Convert ... |
570 571 |
if (size < 50) return -ENOSPC; |
4cd7b7834 tpm: Move the I2C... |
572 |
|
b697e0ff5 dm: tpm: Convert ... |
573 574 575 576 |
return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)", chip->is_open ? "open" : "closed", chip_name[chip->chip_type], chip->vend_dev >> 16); |
4cd7b7834 tpm: Move the I2C... |
577 |
} |
b697e0ff5 dm: tpm: Convert ... |
578 |
static int tpm_tis_i2c_probe(struct udevice *dev) |
4cd7b7834 tpm: Move the I2C... |
579 |
{ |
b697e0ff5 dm: tpm: Convert ... |
580 581 |
struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev); struct tpm_chip *chip = dev_get_priv(dev); |
4cd7b7834 tpm: Move the I2C... |
582 |
|
b697e0ff5 dm: tpm: Convert ... |
583 |
chip->chip_type = dev_get_driver_data(dev); |
4cd7b7834 tpm: Move the I2C... |
584 |
|
b697e0ff5 dm: tpm: Convert ... |
585 586 587 588 589 |
/* TODO: These need to be checked and tuned */ uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS; uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS; uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS; uc_priv->retry_time_ms = TPM_TIMEOUT_MS; |
4cd7b7834 tpm: Move the I2C... |
590 591 592 |
return 0; } |
b697e0ff5 dm: tpm: Convert ... |
593 594 595 596 597 598 599 600 |
static const struct tpm_ops tpm_tis_i2c_ops = { .open = tpm_tis_i2c_open, .close = tpm_tis_i2c_close, .get_desc = tpm_tis_get_desc, .send = tpm_tis_i2c_send, .recv = tpm_tis_i2c_recv, .cleanup = tpm_tis_i2c_cleanup, }; |
4cd7b7834 tpm: Move the I2C... |
601 |
|
b697e0ff5 dm: tpm: Convert ... |
602 603 604 605 606 |
static const struct udevice_id tpm_tis_i2c_ids[] = { { .compatible = "infineon,slb9635tt", .data = SLB9635 }, { .compatible = "infineon,slb9645tt", .data = SLB9645 }, { } }; |
4cd7b7834 tpm: Move the I2C... |
607 |
|
b697e0ff5 dm: tpm: Convert ... |
608 |
U_BOOT_DRIVER(tpm_tis_i2c) = { |
0766ad2f7 dm: tpm: Move tpm... |
609 |
.name = "tpm_tis_infineon", |
b697e0ff5 dm: tpm: Convert ... |
610 611 612 613 614 615 |
.id = UCLASS_TPM, .of_match = tpm_tis_i2c_ids, .ops = &tpm_tis_i2c_ops, .probe = tpm_tis_i2c_probe, .priv_auto_alloc_size = sizeof(struct tpm_chip), }; |