blob: 245218fc075b95e95b230f3d9acab8f4b0670f01 [file] [log] [blame]
Christophe Ricard88249232016-01-21 23:27:13 +01001/*
2 * STMicroelectronics TPM ST33ZP24 I2C UBOOT driver
3 *
Patrice Chotard27e90942017-10-23 09:54:01 +02004 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5 * Author(s): Christophe Ricard <christophe-h.ricard@st.com> for STMicroelectronics.
Christophe Ricard88249232016-01-21 23:27:13 +01006 *
7 * Description: Device driver for ST33ZP24 I2C TPM TCG.
8 *
9 * This device driver implements the TPM interface as defined in
10 * the TCG TPM Interface Spec version 1.21, revision 1.0 and the
11 * STMicroelectronics Protocol Stack Specification version 1.2.0.
12 *
13 * SPDX-License-Identifier: GPL-2.0+
14 */
15
16#include <common.h>
17#include <dm.h>
18#include <fdtdec.h>
19#include <i2c.h>
20#include <tpm.h>
21#include <errno.h>
22#include <linux/types.h>
23#include <asm/unaligned.h>
24
25#include "tpm_tis.h"
26#include "tpm_internal.h"
27
28#define TPM_ACCESS 0x0
29#define TPM_STS 0x18
30#define TPM_DATA_FIFO 0x24
31
32#define LOCALITY0 0
33
34#define TPM_DUMMY_BYTE 0xAA
35#define TPM_ST33ZP24_I2C_SLAVE_ADDR 0x13
36
37#define TPM_WRITE_DIRECTION 0x80
38
39/*
40 * st33zp24_i2c_write8_reg
41 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
42 * @param: tpm_register, the tpm tis register where the data should be written
43 * @param: tpm_data, the tpm_data to write inside the tpm_register
44 * @param: tpm_size, The length of the data
45 * @return: Number of byte written successfully else an error code.
46 */
47static int st33zp24_i2c_write8_reg(struct udevice *dev, u8 tpm_register,
48 const u8 *tpm_data, size_t tpm_size)
49{
50 struct tpm_chip_priv *chip_priv = dev_get_uclass_priv(dev);
51
52 chip_priv->buf[0] = tpm_register;
53 memcpy(chip_priv->buf + 1, tpm_data, tpm_size);
54
55 return dm_i2c_write(dev, 0, chip_priv->buf, tpm_size + 1);
56}
57
58/*
59* st33zp24_i2c_read8_reg
60* Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
61* @param: tpm_register, the tpm tis register where the data should be read
62* @param: tpm_data, the TPM response
63* @param: tpm_size, tpm TPM response size to read.
64* @return: Number of byte read successfully else an error code.
65*/
66static int st33zp24_i2c_read8_reg(struct udevice *dev, u8 tpm_register,
67 u8 *tpm_data, size_t tpm_size)
68{
69 int status;
70 u8 data;
71
72 data = TPM_DUMMY_BYTE;
73 status = st33zp24_i2c_write8_reg(dev, tpm_register, &data, 1);
74 if (status < 0)
75 return status;
76
77 return dm_i2c_read(dev, 0, tpm_data, tpm_size);
78}
79
80/*
81 * st33zp24_i2c_write
82 * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
83 * @param: phy_id, the phy description
84 * @param: tpm_register, the tpm tis register where the data should be written
85 * @param: tpm_data, the tpm_data to write inside the tpm_register
86 * @param: tpm_size, the length of the data
87 * @return: number of byte written successfully: should be one if success.
88 */
89static int st33zp24_i2c_write(struct udevice *dev, u8 tpm_register,
90 const u8 *tpm_data, size_t tpm_size)
91{
92 return st33zp24_i2c_write8_reg(dev, tpm_register | TPM_WRITE_DIRECTION,
93 tpm_data, tpm_size);
94}
95
96/*
97 * st33zp24_i2c_read
98 * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
99 * @param: phy_id, the phy description
100 * @param: tpm_register, the tpm tis register where the data should be read
101 * @param: tpm_data, the TPM response
102 * @param: tpm_size, tpm TPM response size to read.
103 * @return: number of byte read successfully: should be one if success.
104 */
105static int st33zp24_i2c_read(struct udevice *dev, u8 tpm_register,
106 u8 *tpm_data, size_t tpm_size)
107{
108 return st33zp24_i2c_read8_reg(dev, tpm_register, tpm_data, tpm_size);
109}
110
111/*
112 * st33zp24_i2c_release_locality release the active locality
113 * @param: chip, the tpm chip description.
114 */
115static void st33zp24_i2c_release_locality(struct udevice *dev)
116{
117 u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
118
119 st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
120}
121
122/*
123 * st33zp24_i2c_check_locality if the locality is active
124 * @param: chip, the tpm chip description
125 * @return: the active locality or -EACCES.
126 */
127static int st33zp24_i2c_check_locality(struct udevice *dev)
128{
129 struct tpm_chip *chip = dev_get_priv(dev);
130 u8 data;
131 u8 status;
132
133 status = st33zp24_i2c_read(dev, TPM_ACCESS, &data, 1);
134 if (!status && (data &
135 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
136 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
137 return chip->locality;
138
139 return -EACCES;
140}
141
142/*
143 * st33zp24_i2c_request_locality request the TPM locality
144 * @param: chip, the chip description
145 * @return: the active locality or negative value.
146 */
147static int st33zp24_i2c_request_locality(struct udevice *dev)
148{
149 struct tpm_chip *chip = dev_get_priv(dev);
150 unsigned long start, stop;
151 long ret;
152 u8 data;
153
154 if (st33zp24_i2c_check_locality(dev) == chip->locality)
155 return chip->locality;
156
157 data = TPM_ACCESS_REQUEST_USE;
158 ret = st33zp24_i2c_write(dev, TPM_ACCESS, &data, 1);
159 if (ret < 0)
160 return ret;
161
162 /* wait for locality activated */
163 start = get_timer(0);
164 stop = chip->timeout_a;
165 do {
166 if (st33zp24_i2c_check_locality(dev) >= 0)
167 return chip->locality;
168 udelay(TPM_TIMEOUT_MS * 1000);
169 } while (get_timer(start) < stop);
170
171 return -EACCES;
172}
173
174/*
175 * st33zp24_i2c_status return the TPM_STS register
176 * @param: chip, the tpm chip description
177 * @return: the TPM_STS register value.
178 */
179static u8 st33zp24_i2c_status(struct udevice *dev)
180{
181 u8 data;
182
183 st33zp24_i2c_read(dev, TPM_STS, &data, 1);
184
185 return data;
186}
187
188/*
189 * st33zp24_i2c_get_burstcount return the burstcount address 0x19 0x1A
190 * @param: chip, the chip description
191 * return: the burstcount or -TPM_DRIVER_ERR in case of error.
192 */
193static int st33zp24_i2c_get_burstcount(struct udevice *dev)
194{
195 struct tpm_chip *chip = dev_get_priv(dev);
196 unsigned long start, stop;
197 int burstcnt, status;
198 u8 tpm_reg, temp;
199
200 /* wait for burstcount */
201 start = get_timer(0);
202 stop = chip->timeout_d;
203 do {
204 tpm_reg = TPM_STS + 1;
205 status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
206 if (status < 0)
207 return -EBUSY;
208
209 tpm_reg = TPM_STS + 2;
210 burstcnt = temp;
211 status = st33zp24_i2c_read(dev, tpm_reg, &temp, 1);
212 if (status < 0)
213 return -EBUSY;
214
215 burstcnt |= temp << 8;
216 if (burstcnt)
217 return burstcnt;
218 udelay(TIS_SHORT_TIMEOUT_MS * 1000);
219 } while (get_timer(start) < stop);
220
221 return -EBUSY;
222}
223
224/*
225 * st33zp24_i2c_cancel, cancel the current command execution or
226 * set STS to COMMAND READY.
227 * @param: chip, tpm_chip description.
228 */
229static void st33zp24_i2c_cancel(struct udevice *dev)
230{
231 u8 data;
232
233 data = TPM_STS_COMMAND_READY;
234 st33zp24_i2c_write(dev, TPM_STS, &data, 1);
235}
236
237/*
238 * st33zp24_i2c_wait_for_stat wait for a TPM_STS value
239 * @param: chip, the tpm chip description
240 * @param: mask, the value mask to wait
241 * @param: timeout, the timeout
242 * @param: status,
243 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
244 */
245static int st33zp24_i2c_wait_for_stat(struct udevice *dev, u8 mask,
246 unsigned long timeout, int *status)
247{
248 unsigned long start, stop;
249
250 /* Check current status */
251 *status = st33zp24_i2c_status(dev);
252 if ((*status & mask) == mask)
253 return 0;
254
255 start = get_timer(0);
256 stop = timeout;
257 do {
258 udelay(TPM_TIMEOUT_MS * 1000);
259 *status = st33zp24_i2c_status(dev);
260 if ((*status & mask) == mask)
261 return 0;
262 } while (get_timer(start) < stop);
263
264 return -ETIME;
265}
266
267/*
268 * st33zp24_i2c_recv_data receive data
269 * @param: chip, the tpm chip description
270 * @param: buf, the buffer where the data are received
271 * @param: count, the number of data to receive
272 * @return: the number of bytes read from TPM FIFO.
273 */
274static int st33zp24_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
275{
276 struct tpm_chip *chip = dev_get_priv(dev);
277 int size = 0, burstcnt, len, ret, status;
278
279 while (size < count &&
280 st33zp24_i2c_wait_for_stat(dev, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
281 chip->timeout_c, &status) == 0) {
282 burstcnt = st33zp24_i2c_get_burstcount(dev);
283 if (burstcnt < 0)
284 return burstcnt;
285 len = min_t(int, burstcnt, count - size);
286 ret = st33zp24_i2c_read(dev, TPM_DATA_FIFO, buf + size, len);
287 if (ret < 0)
288 return ret;
289
290 size += len;
291 }
292
293 return size;
294}
295
296/*
297 * st33zp24_i2c_recv received TPM response through TPM phy.
298 * @param: chip, tpm_chip description.
299 * @param: buf, the buffer to store data.
300 * @param: count, the number of bytes that can received (sizeof buf).
301 * @return: Returns zero in case of success else -EIO.
302 */
303static int st33zp24_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
304{
305 struct tpm_chip *chip = dev_get_priv(dev);
Jeremy Boone8c4b0622018-02-12 17:56:35 -0500306 int size;
307 unsigned int expected;
Christophe Ricard88249232016-01-21 23:27:13 +0100308
309 if (!chip)
310 return -ENODEV;
311
312 if (count < TPM_HEADER_SIZE) {
313 size = -EIO;
314 goto out;
315 }
316
317 size = st33zp24_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
318 if (size < TPM_HEADER_SIZE) {
319 debug("TPM error, unable to read header\n");
320 goto out;
321 }
322
323 expected = get_unaligned_be32(buf + 2);
Jeremy Boone8c4b0622018-02-12 17:56:35 -0500324 if (expected > count || expected < TPM_HEADER_SIZE) {
Christophe Ricard88249232016-01-21 23:27:13 +0100325 size = -EIO;
326 goto out;
327 }
328
329 size += st33zp24_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
330 expected - TPM_HEADER_SIZE);
331 if (size < expected) {
332 debug("TPM error, unable to read remaining bytes of result\n");
333 size = -EIO;
334 goto out;
335 }
336
337out:
338 st33zp24_i2c_cancel(dev);
339 st33zp24_i2c_release_locality(dev);
340
341 return size;
342}
343
344/*
345 * st33zp24_i2c_send send TPM commands through TPM phy.
346 * @param: chip, tpm_chip description.
347 * @param: buf, the buffer to send.
348 * @param: len, the number of bytes to send.
349 * @return: Returns zero in case of success else the negative error code.
350 */
351static int st33zp24_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
352{
353 struct tpm_chip *chip = dev_get_priv(dev);
354 u32 i, size;
355 int burstcnt, ret, status;
356 u8 data, tpm_stat;
357
358 if (!chip)
359 return -ENODEV;
360 if (len < TPM_HEADER_SIZE)
361 return -EIO;
362
363 ret = st33zp24_i2c_request_locality(dev);
364 if (ret < 0)
365 return ret;
366
367 tpm_stat = st33zp24_i2c_status(dev);
368 if ((tpm_stat & TPM_STS_COMMAND_READY) == 0) {
369 st33zp24_i2c_cancel(dev);
370 if (st33zp24_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
371 chip->timeout_b, &status) < 0) {
372 ret = -ETIME;
373 goto out_err;
374 }
375 }
376
377 for (i = 0; i < len - 1;) {
378 burstcnt = st33zp24_i2c_get_burstcount(dev);
379 if (burstcnt < 0)
380 return burstcnt;
381
382 size = min_t(int, len - i - 1, burstcnt);
383 ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + i, size);
384 if (ret < 0)
385 goto out_err;
386
387 i += size;
388 }
389
390 tpm_stat = st33zp24_i2c_status(dev);
391 if ((tpm_stat & TPM_STS_DATA_EXPECT) == 0) {
392 ret = -EIO;
393 goto out_err;
394 }
395
396 ret = st33zp24_i2c_write(dev, TPM_DATA_FIFO, buf + len - 1, 1);
397 if (ret < 0)
398 goto out_err;
399
400 tpm_stat = st33zp24_i2c_status(dev);
401 if ((tpm_stat & TPM_STS_DATA_EXPECT) != 0) {
402 ret = -EIO;
403 goto out_err;
404 }
405
406 data = TPM_STS_GO;
407 ret = st33zp24_i2c_write(dev, TPM_STS, &data, 1);
408 if (ret < 0)
409 goto out_err;
410
411 return len;
412
413out_err:
414 st33zp24_i2c_cancel(dev);
415 st33zp24_i2c_release_locality(dev);
416
417 return ret;
418}
419
420static int st33zp24_i2c_cleanup(struct udevice *dev)
421{
422 st33zp24_i2c_cancel(dev);
423 /*
424 * The TPM needs some time to clean up here,
425 * so we sleep rather than keeping the bus busy
426 */
427 mdelay(2);
428 st33zp24_i2c_release_locality(dev);
429
430 return 0;
431}
432
433static int st33zp24_i2c_init(struct udevice *dev)
434{
435 struct tpm_chip *chip = dev_get_priv(dev);
436
437 chip->is_open = 1;
438
439 /* Default timeouts - these could move to the device tree */
440 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
441 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
442 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
443 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
444
445 chip->locality = LOCALITY0;
446
447 /*
448 * A timeout query to TPM can be placed here.
449 * Standard timeout values are used so far
450 */
451
452 return 0;
453}
454
455static int st33zp24_i2c_open(struct udevice *dev)
456{
457 struct tpm_chip *chip = dev_get_priv(dev);
458 int rc;
459
460 debug("%s: start\n", __func__);
461 if (chip->is_open)
462 return -EBUSY;
463
464 rc = st33zp24_i2c_init(dev);
465 if (rc < 0)
466 chip->is_open = 0;
467
468 return rc;
469}
470
471static int st33zp24_i2c_close(struct udevice *dev)
472{
473 struct tpm_chip *chip = dev_get_priv(dev);
474
475 if (chip->is_open) {
476 st33zp24_i2c_release_locality(dev);
477 chip->is_open = 0;
478 chip->vend_dev = 0;
479 }
480
481 return 0;
482}
483
484static int st33zp24_i2c_get_desc(struct udevice *dev, char *buf, int size)
485{
486 struct tpm_chip *chip = dev_get_priv(dev);
487
488 if (size < 50)
489 return -ENOSPC;
490
491 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
492 chip->is_open ? "open" : "closed",
493 dev->name,
494 chip->vend_dev >> 16);
495}
496
497static const struct tpm_ops st33zp24_i2c_tpm_ops = {
498 .open = st33zp24_i2c_open,
499 .close = st33zp24_i2c_close,
500 .recv = st33zp24_i2c_recv,
501 .send = st33zp24_i2c_send,
502 .cleanup = st33zp24_i2c_cleanup,
503 .get_desc = st33zp24_i2c_get_desc,
504};
505
506static int st33zp24_i2c_probe(struct udevice *dev)
507{
508 struct tpm_chip *chip = dev_get_priv(dev);
509
510 /* Default timeouts */
511 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
512 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
513 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
514 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
515
516 chip->locality = LOCALITY0;
517
518 i2c_set_chip_offset_len(dev, 0);
519
520 debug("ST33ZP24 I2C TPM from STMicroelectronics found\n");
521
522 return 0;
523}
524
525static int st33zp24_i2c_remove(struct udevice *dev)
526{
527 st33zp24_i2c_release_locality(dev);
528
529 return 0;
530}
531
532static const struct udevice_id st33zp24_i2c_ids[] = {
533 { .compatible = "st,st33zp24-i2c" },
534 { }
535};
536
537U_BOOT_DRIVER(st33zp24_i2c) = {
538 .name = "st33zp24-i2c",
539 .id = UCLASS_TPM,
540 .of_match = of_match_ptr(st33zp24_i2c_ids),
541 .probe = st33zp24_i2c_probe,
542 .remove = st33zp24_i2c_remove,
543 .ops = &st33zp24_i2c_tpm_ops,
544 .priv_auto_alloc_size = sizeof(struct tpm_chip),
545};