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