]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/tpm/slb9635_i2c/tpm_tis_i2c.c
x86: config: Reflect the name changes of LPC TPM configs
[people/ms/u-boot.git] / drivers / tpm / slb9635_i2c / tpm_tis_i2c.c
CommitLineData
f6267998
RC
1/*
2 * Copyright (C) 2011 Infineon Technologies
3 *
4 * Authors:
5 * Peter Huewe <huewe.external@infineon.com>
6 *
7 * Description:
8 * Device driver for TCG/TCPA TPM (trusted platform module).
9 * Specifications at www.trustedcomputinggroup.org
10 *
11 * This device driver implements the TPM interface as defined in
12 * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
13 * Infineon I2C Protocol Stack Specification v0.20.
14 *
15 * It is based on the Linux kernel driver tpm.c from Leendert van
16 * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
17 *
18 * Version: 2.1.1
19 *
20 * See file CREDITS for list of people who contributed to this
21 * project.
22 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation, version 2 of the
26 * License.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 * MA 02111-1307 USA
37 */
38
39#include <common.h>
40#include <i2c.h>
41#include <linux/types.h>
42
43#include "compatibility.h"
44#include "tpm.h"
45
46/* max. buffer size supported by our tpm */
47#ifdef TPM_BUFSIZE
48#undef TPM_BUFSIZE
49#endif
50#define TPM_BUFSIZE 1260
51/* Address of the TPM on the I2C bus */
52#define TPM_I2C_ADDR 0x20
53/* max. number of iterations after i2c NAK */
54#define MAX_COUNT 3
55
56#define SLEEP_DURATION 60 /*in usec*/
57
58/* max. number of iterations after i2c NAK for 'long' commands
59 * we need this especially for sending TPM_READY, since the cleanup after the
60 * transtion to the ready state may take some time, but it is unpredictable
61 * how long it will take.
62 */
63#define MAX_COUNT_LONG 50
64
65#define SLEEP_DURATION_LONG 210 /* in usec */
66
67/* expected value for DIDVID register */
68#define TPM_TIS_I2C_DID_VID 0x000b15d1L
69
70/* Structure to store I2C TPM specific stuff */
71struct tpm_inf_dev {
72 uint addr;
73 u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */
74};
75
76static struct tpm_inf_dev tpm_dev = {
77 .addr = TPM_I2C_ADDR
78};
79
80/*
81 * iic_tpm_read() - read from TPM register
82 * @addr: register address to read from
83 * @buffer: provided by caller
84 * @len: number of bytes to read
85 *
86 * Read len bytes from TPM register and put them into
87 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
88 *
89 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
90 * values have to be swapped.
91 *
92 * Return -EIO on error, 0 on success.
93 */
94int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
95{
96 int rc;
97 int count;
98 uint myaddr = addr;
99 /* we have to use uint here, uchar hangs the board */
100
101 for (count = 0; count < MAX_COUNT; count++) {
102 rc = i2c_write(tpm_dev.addr, 0, 0, (uchar *)&myaddr, 1);
103 if (rc == 0)
104 break; /*success, break to skip sleep*/
105
106 udelay(SLEEP_DURATION);
107 }
108
109 if (rc)
110 return -rc;
111
112 /* After the TPM has successfully received the register address it needs
113 * some time, thus we're sleeping here again, before retrieving the data
114 */
115 for (count = 0; count < MAX_COUNT; count++) {
116 udelay(SLEEP_DURATION);
117 rc = i2c_read(tpm_dev.addr, 0, 0, buffer, len);
118 if (rc == 0)
119 break; /*success, break to skip sleep*/
120 }
121
122 if (rc)
123 return -rc;
124
125 return 0;
126}
127
128static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
129 unsigned int sleep_time,
130 u8 max_count)
131{
132 int rc = 0;
133 int count;
134
135 /* prepare send buffer */
136 tpm_dev.buf[0] = addr;
137 memcpy(&(tpm_dev.buf[1]), buffer, len);
138
139 for (count = 0; count < max_count; count++) {
140 rc = i2c_write(tpm_dev.addr, 0, 0, tpm_dev.buf, len + 1);
141 if (rc == 0)
142 break; /*success, break to skip sleep*/
143
144 udelay(sleep_time);
145 }
146
147 if (rc)
148 return -rc;
149
150 return 0;
151}
152
153/*
154 * iic_tpm_write() - write to TPM register
155 * @addr: register address to write to
156 * @buffer: containing data to be written
157 * @len: number of bytes to write
158 *
159 * Write len bytes from provided buffer to TPM register (little
160 * endian format, i.e. buffer[0] is written as first byte).
161 *
162 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
163 * values have to be swapped.
164 *
165 * NOTE: use this function instead of the iic_tpm_write_generic function.
166 *
167 * Return -EIO on error, 0 on success
168 */
169static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
170{
171 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
172 MAX_COUNT);
173}
174
175/*
176 * This function is needed especially for the cleanup situation after
177 * sending TPM_READY
178 * */
179static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
180{
181 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
182 MAX_COUNT_LONG);
183}
184
185#define TPM_HEADER_SIZE 10
186
187enum tis_access {
188 TPM_ACCESS_VALID = 0x80,
189 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
190 TPM_ACCESS_REQUEST_PENDING = 0x04,
191 TPM_ACCESS_REQUEST_USE = 0x02,
192};
193
194enum tis_status {
195 TPM_STS_VALID = 0x80,
196 TPM_STS_COMMAND_READY = 0x40,
197 TPM_STS_GO = 0x20,
198 TPM_STS_DATA_AVAIL = 0x10,
199 TPM_STS_DATA_EXPECT = 0x08,
200};
201
202enum tis_defaults {
203 TIS_SHORT_TIMEOUT = 750, /* ms */
204 TIS_LONG_TIMEOUT = 2000, /* 2 sec */
205};
206
207#define TPM_ACCESS(l) (0x0000 | ((l) << 4))
208#define TPM_STS(l) (0x0001 | ((l) << 4))
209#define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
210#define TPM_DID_VID(l) (0x0006 | ((l) << 4))
211
212static int check_locality(struct tpm_chip *chip, int loc)
213{
214 u8 buf;
215 int rc;
216
217 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
218 if (rc < 0)
219 return rc;
220
221 if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
222 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
223 chip->vendor.locality = loc;
224 return loc;
225 }
226
227 return -1;
228}
229
230static void release_locality(struct tpm_chip *chip, int loc, int force)
231{
232 u8 buf;
233 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
234 return;
235
236 if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
237 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
238 buf = TPM_ACCESS_ACTIVE_LOCALITY;
239 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
240 }
241}
242
243static int request_locality(struct tpm_chip *chip, int loc)
244{
245 unsigned long start, stop;
246 u8 buf = TPM_ACCESS_REQUEST_USE;
247
248 if (check_locality(chip, loc) >= 0)
249 return loc; /* we already have the locality */
250
251 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
252
253 /* wait for burstcount */
254 start = get_timer(0);
255 stop = chip->vendor.timeout_a;
256 do {
257 if (check_locality(chip, loc) >= 0)
258 return loc;
259 msleep(TPM_TIMEOUT);
260 } while (get_timer(start) < stop);
261
262 return -1;
263}
264
265static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
266{
267 /* NOTE: since i2c read may fail, return 0 in this case --> time-out */
268 u8 buf;
269 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
270 return 0;
271 else
272 return buf;
273}
274
275static void tpm_tis_i2c_ready(struct tpm_chip *chip)
276{
277 /* this causes the current command to be aborted */
278 u8 buf = TPM_STS_COMMAND_READY;
279 iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
280}
281
282static ssize_t get_burstcount(struct tpm_chip *chip)
283{
284 unsigned long start, stop;
285 ssize_t burstcnt;
286 u8 buf[3];
287
288 /* wait for burstcount */
289 /* which timeout value, spec has 2 answers (c & d) */
290 start = get_timer(0);
291 stop = chip->vendor.timeout_d;
292 do {
293 /* Note: STS is little endian */
294 if (iic_tpm_read(TPM_STS(chip->vendor.locality) + 1, buf, 3)
295 < 0)
296 burstcnt = 0;
297 else
298 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
299
300 if (burstcnt)
301 return burstcnt;
302 msleep(TPM_TIMEOUT);
303 } while (get_timer(start) < stop);
304
305 return -EBUSY;
306}
307
308static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
309 int *status)
310{
311 unsigned long start, stop;
312
313 /* check current status */
314 *status = tpm_tis_i2c_status(chip);
315 if ((*status & mask) == mask)
316 return 0;
317
318 start = get_timer(0);
319 stop = timeout;
320 do {
321 msleep(TPM_TIMEOUT);
322 *status = tpm_tis_i2c_status(chip);
323 if ((*status & mask) == mask)
324 return 0;
325
326 } while (get_timer(start) < stop);
327
328 return -ETIME;
329}
330
331static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
332{
333 size_t size = 0;
334 ssize_t burstcnt;
335 int rc;
336
337 while (size < count) {
338 burstcnt = get_burstcount(chip);
339
340 /* burstcount < 0 = tpm is busy */
341 if (burstcnt < 0)
342 return burstcnt;
343
344 /* limit received data to max. left */
345 if (burstcnt > (count - size))
346 burstcnt = count - size;
347
348 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
349 &(buf[size]),
350 burstcnt);
351 if (rc == 0)
352 size += burstcnt;
353 }
354
355 return size;
356}
357
358static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
359{
360 int size = 0;
361 int expected, status;
362
363 if (count < TPM_HEADER_SIZE) {
364 size = -EIO;
365 goto out;
366 }
367
368 /* read first 10 bytes, including tag, paramsize, and result */
369 size = recv_data(chip, buf, TPM_HEADER_SIZE);
370 if (size < TPM_HEADER_SIZE) {
371 dev_err(chip->dev, "Unable to read header\n");
372 goto out;
373 }
374
375 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
376 if ((size_t)expected > count) {
377 size = -EIO;
378 goto out;
379 }
380
381 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
382 expected - TPM_HEADER_SIZE);
383 if (size < expected) {
384 dev_err(chip->dev, "Unable to read remainder of result\n");
385 size = -ETIME;
386 goto out;
387 }
388
389 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
390 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
391 dev_err(chip->dev, "Error left over data\n");
392 size = -EIO;
393 goto out;
394 }
395
396out:
397 tpm_tis_i2c_ready(chip);
398 /* The TPM needs some time to clean up here,
399 * so we sleep rather than keeping the bus busy
400 */
401 udelay(2000);
402 release_locality(chip, chip->vendor.locality, 0);
403
404 return size;
405}
406
407static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
408{
409 int rc, status;
410 ssize_t burstcnt;
411 size_t count = 0;
412 u8 sts = TPM_STS_GO;
413
414 if (len > TPM_BUFSIZE)
415 return -E2BIG; /* command is too long for our tpm, sorry */
416
417 if (request_locality(chip, 0) < 0)
418 return -EBUSY;
419
420 status = tpm_tis_i2c_status(chip);
421 if ((status & TPM_STS_COMMAND_READY) == 0) {
422 tpm_tis_i2c_ready(chip);
423 if (wait_for_stat
424 (chip, TPM_STS_COMMAND_READY,
425 chip->vendor.timeout_b, &status) < 0) {
426 rc = -ETIME;
427 goto out_err;
428 }
429 }
430
431 while (count < len - 1) {
432 burstcnt = get_burstcount(chip);
433
434 /* burstcount < 0 = tpm is busy */
435 if (burstcnt < 0)
436 return burstcnt;
437
438 if (burstcnt > (len-1-count))
439 burstcnt = len-1-count;
440
441#ifdef CONFIG_TPM_I2C_BURST_LIMITATION
442 if (burstcnt > CONFIG_TPM_I2C_BURST_LIMITATION)
443 burstcnt = CONFIG_TPM_I2C_BURST_LIMITATION;
444#endif /* CONFIG_TPM_I2C_BURST_LIMITATION */
445
446 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
447 &(buf[count]), burstcnt);
448 if (rc == 0)
449 count += burstcnt;
450
451 wait_for_stat(chip, TPM_STS_VALID,
452 chip->vendor.timeout_c, &status);
453
454 if ((status & TPM_STS_DATA_EXPECT) == 0) {
455 rc = -EIO;
456 goto out_err;
457 }
458 }
459
460 /* write last byte */
461 iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1);
462 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
463 if ((status & TPM_STS_DATA_EXPECT) != 0) {
464 rc = -EIO;
465 goto out_err;
466 }
467
468 /* go and do it */
469 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
470
471 return len;
472out_err:
473 tpm_tis_i2c_ready(chip);
474 /* The TPM needs some time to clean up here,
475 * so we sleep rather than keeping the bus busy
476 */
477 udelay(2000);
478 release_locality(chip, chip->vendor.locality, 0);
479
480 return rc;
481}
482
483static struct tpm_vendor_specific tpm_tis_i2c = {
484 .status = tpm_tis_i2c_status,
485 .recv = tpm_tis_i2c_recv,
486 .send = tpm_tis_i2c_send,
487 .cancel = tpm_tis_i2c_ready,
488 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
489 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
490 .req_canceled = TPM_STS_COMMAND_READY,
491};
492
493/* initialisation of i2c tpm */
494
495
496int tpm_vendor_init(uint32_t dev_addr)
497{
498 u32 vendor;
499 uint old_addr;
500 int rc = 0;
501 struct tpm_chip *chip;
502
503 old_addr = tpm_dev.addr;
504 if (dev_addr != 0)
505 tpm_dev.addr = dev_addr;
506
507 chip = tpm_register_hardware(&tpm_tis_i2c);
508 if (chip < 0) {
509 rc = -ENODEV;
510 goto out_err;
511 }
512
513 /* Disable interrupts (not supported) */
514 chip->vendor.irq = 0;
515
516 /* Default timeouts */
517 chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
518 chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
519 chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
520 chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
521
522 if (request_locality(chip, 0) != 0) {
523 rc = -ENODEV;
524 goto out_err;
525 }
526
527 /* read four bytes from DID_VID register */
528 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
529 rc = -EIO;
530 goto out_release;
531 }
532
533 /* create DID_VID register value, after swapping to little-endian */
534 vendor = be32_to_cpu(vendor);
535
536 if (vendor != TPM_TIS_I2C_DID_VID) {
537 rc = -ENODEV;
538 goto out_release;
539 }
540
541 dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16);
542
543 /*
544 * A timeout query to TPM can be placed here.
545 * Standard timeout values are used so far
546 */
547
548 return 0;
549
550out_release:
551 release_locality(chip, 0, 1);
552
553out_err:
554 tpm_dev.addr = old_addr;
555 return rc;
556}
557
558void tpm_vendor_cleanup(struct tpm_chip *chip)
559{
560 release_locality(chip, chip->vendor.locality, 1);
561}