Commit | Line | Data |
---|---|---|
aad628c1 | 1 | /* |
c61c86dd | 2 | * Copyright (C) 2012,2013 Infineon Technologies |
aad628c1 PH |
3 | * |
4 | * Authors: | |
5 | * Peter Huewe <peter.huewe@infineon.com> | |
6 | * | |
7 | * Device driver for TCG/TCPA TPM (trusted platform module). | |
8 | * Specifications at www.trustedcomputinggroup.org | |
9 | * | |
10 | * This device driver implements the TPM interface as defined in | |
11 | * the TCG TPM Interface Spec version 1.2, revision 1.0 and the | |
12 | * Infineon I2C Protocol Stack Specification v0.20. | |
13 | * | |
14 | * It is based on the original tpm_tis device driver from Leendert van | |
15 | * Dorn and Kyleen Hall. | |
16 | * | |
17 | * This program is free software; you can redistribute it and/or | |
18 | * modify it under the terms of the GNU General Public License as | |
19 | * published by the Free Software Foundation, version 2 of the | |
20 | * License. | |
21 | * | |
22 | * | |
23 | */ | |
aad628c1 PH |
24 | #include <linux/i2c.h> |
25 | #include <linux/module.h> | |
aad628c1 PH |
26 | #include <linux/wait.h> |
27 | #include "tpm.h" | |
28 | ||
29 | /* max. buffer size supported by our TPM */ | |
30 | #define TPM_BUFSIZE 1260 | |
31 | ||
32 | /* max. number of iterations after I2C NAK */ | |
33 | #define MAX_COUNT 3 | |
34 | ||
35 | #define SLEEP_DURATION_LOW 55 | |
36 | #define SLEEP_DURATION_HI 65 | |
37 | ||
38 | /* max. number of iterations after I2C NAK for 'long' commands | |
39 | * we need this especially for sending TPM_READY, since the cleanup after the | |
40 | * transtion to the ready state may take some time, but it is unpredictable | |
41 | * how long it will take. | |
42 | */ | |
43 | #define MAX_COUNT_LONG 50 | |
44 | ||
45 | #define SLEEP_DURATION_LONG_LOW 200 | |
46 | #define SLEEP_DURATION_LONG_HI 220 | |
47 | ||
48 | /* After sending TPM_READY to 'reset' the TPM we have to sleep even longer */ | |
49 | #define SLEEP_DURATION_RESET_LOW 2400 | |
50 | #define SLEEP_DURATION_RESET_HI 2600 | |
51 | ||
52 | /* we want to use usleep_range instead of msleep for the 5ms TPM_TIMEOUT */ | |
53 | #define TPM_TIMEOUT_US_LOW (TPM_TIMEOUT * 1000) | |
54 | #define TPM_TIMEOUT_US_HI (TPM_TIMEOUT_US_LOW + 2000) | |
55 | ||
56 | /* expected value for DIDVID register */ | |
c61c86dd PH |
57 | #define TPM_TIS_I2C_DID_VID_9635 0xd1150b00L |
58 | #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L | |
59 | ||
60 | enum i2c_chip_type { | |
61 | SLB9635, | |
62 | SLB9645, | |
63 | UNKNOWN, | |
64 | }; | |
aad628c1 PH |
65 | |
66 | /* Structure to store I2C TPM specific stuff */ | |
67 | struct tpm_inf_dev { | |
68 | struct i2c_client *client; | |
56671c89 | 69 | int locality; |
aad628c1 PH |
70 | u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */ |
71 | struct tpm_chip *chip; | |
c61c86dd | 72 | enum i2c_chip_type chip_type; |
d8c3eab5 | 73 | unsigned int adapterlimit; |
aad628c1 PH |
74 | }; |
75 | ||
76 | static struct tpm_inf_dev tpm_dev; | |
aad628c1 PH |
77 | |
78 | /* | |
79 | * iic_tpm_read() - read from TPM register | |
80 | * @addr: register address to read from | |
81 | * @buffer: provided by caller | |
82 | * @len: number of bytes to read | |
83 | * | |
84 | * Read len bytes from TPM register and put them into | |
85 | * buffer (little-endian format, i.e. first byte is put into buffer[0]). | |
86 | * | |
87 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte | |
88 | * values have to be swapped. | |
89 | * | |
90 | * NOTE: We can't unfortunately use the combined read/write functions | |
91 | * provided by the i2c core as the TPM currently does not support the | |
92 | * repeated start condition and due to it's special requirements. | |
93 | * The i2c_smbus* functions do not work for this chip. | |
94 | * | |
95 | * Return -EIO on error, 0 on success. | |
96 | */ | |
97 | static int iic_tpm_read(u8 addr, u8 *buffer, size_t len) | |
98 | { | |
99 | ||
eef8b629 SD |
100 | struct i2c_msg msg1 = { |
101 | .addr = tpm_dev.client->addr, | |
102 | .len = 1, | |
103 | .buf = &addr | |
104 | }; | |
105 | struct i2c_msg msg2 = { | |
106 | .addr = tpm_dev.client->addr, | |
107 | .flags = I2C_M_RD, | |
108 | .len = len, | |
109 | .buf = buffer | |
110 | }; | |
c61c86dd | 111 | struct i2c_msg msgs[] = {msg1, msg2}; |
aad628c1 | 112 | |
c61c86dd | 113 | int rc = 0; |
aad628c1 | 114 | int count; |
d8c3eab5 | 115 | unsigned int msglen = len; |
aad628c1 PH |
116 | |
117 | /* Lock the adapter for the duration of the whole sequence. */ | |
118 | if (!tpm_dev.client->adapter->algo->master_xfer) | |
119 | return -EOPNOTSUPP; | |
120 | i2c_lock_adapter(tpm_dev.client->adapter); | |
121 | ||
c61c86dd PH |
122 | if (tpm_dev.chip_type == SLB9645) { |
123 | /* use a combined read for newer chips | |
124 | * unfortunately the smbus functions are not suitable due to | |
125 | * the 32 byte limit of the smbus. | |
126 | * retries should usually not be needed, but are kept just to | |
127 | * be on the safe side. | |
128 | */ | |
129 | for (count = 0; count < MAX_COUNT; count++) { | |
130 | rc = __i2c_transfer(tpm_dev.client->adapter, msgs, 2); | |
131 | if (rc > 0) | |
132 | break; /* break here to skip sleep */ | |
133 | usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); | |
134 | } | |
135 | } else { | |
d8c3eab5 BF |
136 | /* Expect to send one command message and one data message, but |
137 | * support looping over each or both if necessary. | |
c61c86dd | 138 | */ |
d8c3eab5 BF |
139 | while (len > 0) { |
140 | /* slb9635 protocol should work in all cases */ | |
141 | for (count = 0; count < MAX_COUNT; count++) { | |
142 | rc = __i2c_transfer(tpm_dev.client->adapter, | |
143 | &msg1, 1); | |
144 | if (rc > 0) | |
145 | break; /* break here to skip sleep */ | |
146 | ||
147 | usleep_range(SLEEP_DURATION_LOW, | |
148 | SLEEP_DURATION_HI); | |
149 | } | |
150 | ||
151 | if (rc <= 0) | |
152 | goto out; | |
153 | ||
154 | /* After the TPM has successfully received the register | |
155 | * address it needs some time, thus we're sleeping here | |
156 | * again, before retrieving the data | |
157 | */ | |
158 | for (count = 0; count < MAX_COUNT; count++) { | |
159 | if (tpm_dev.adapterlimit) { | |
160 | msglen = min_t(unsigned int, | |
161 | tpm_dev.adapterlimit, | |
162 | len); | |
163 | msg2.len = msglen; | |
164 | } | |
165 | usleep_range(SLEEP_DURATION_LOW, | |
166 | SLEEP_DURATION_HI); | |
167 | rc = __i2c_transfer(tpm_dev.client->adapter, | |
168 | &msg2, 1); | |
169 | if (rc > 0) { | |
170 | /* Since len is unsigned, make doubly | |
171 | * sure we do not underflow it. | |
172 | */ | |
173 | if (msglen > len) | |
174 | len = 0; | |
175 | else | |
176 | len -= msglen; | |
177 | msg2.buf += msglen; | |
178 | break; | |
179 | } | |
180 | /* If the I2C adapter rejected the request (e.g | |
181 | * when the quirk read_max_len < len) fall back | |
182 | * to a sane minimum value and try again. | |
183 | */ | |
184 | if (rc == -EOPNOTSUPP) | |
185 | tpm_dev.adapterlimit = | |
186 | I2C_SMBUS_BLOCK_MAX; | |
187 | } | |
188 | ||
189 | if (rc <= 0) | |
190 | goto out; | |
c61c86dd | 191 | } |
aad628c1 PH |
192 | } |
193 | ||
194 | out: | |
195 | i2c_unlock_adapter(tpm_dev.client->adapter); | |
c61c86dd PH |
196 | /* take care of 'guard time' */ |
197 | usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); | |
198 | ||
6aa4ef4d PH |
199 | /* __i2c_transfer returns the number of successfully transferred |
200 | * messages. | |
201 | * So rc should be greater than 0 here otherwise we have an error. | |
202 | */ | |
aad628c1 PH |
203 | if (rc <= 0) |
204 | return -EIO; | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
209 | static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len, | |
210 | unsigned int sleep_low, | |
211 | unsigned int sleep_hi, u8 max_count) | |
212 | { | |
213 | int rc = -EIO; | |
214 | int count; | |
215 | ||
eef8b629 SD |
216 | struct i2c_msg msg1 = { |
217 | .addr = tpm_dev.client->addr, | |
218 | .len = len + 1, | |
219 | .buf = tpm_dev.buf | |
220 | }; | |
aad628c1 PH |
221 | |
222 | if (len > TPM_BUFSIZE) | |
223 | return -EINVAL; | |
224 | ||
225 | if (!tpm_dev.client->adapter->algo->master_xfer) | |
226 | return -EOPNOTSUPP; | |
227 | i2c_lock_adapter(tpm_dev.client->adapter); | |
228 | ||
229 | /* prepend the 'register address' to the buffer */ | |
230 | tpm_dev.buf[0] = addr; | |
231 | memcpy(&(tpm_dev.buf[1]), buffer, len); | |
232 | ||
233 | /* | |
234 | * NOTE: We have to use these special mechanisms here and unfortunately | |
235 | * cannot rely on the standard behavior of i2c_transfer. | |
c61c86dd PH |
236 | * Even for newer chips the smbus functions are not |
237 | * suitable due to the 32 byte limit of the smbus. | |
aad628c1 PH |
238 | */ |
239 | for (count = 0; count < max_count; count++) { | |
240 | rc = __i2c_transfer(tpm_dev.client->adapter, &msg1, 1); | |
241 | if (rc > 0) | |
242 | break; | |
aad628c1 PH |
243 | usleep_range(sleep_low, sleep_hi); |
244 | } | |
245 | ||
246 | i2c_unlock_adapter(tpm_dev.client->adapter); | |
c61c86dd PH |
247 | /* take care of 'guard time' */ |
248 | usleep_range(SLEEP_DURATION_LOW, SLEEP_DURATION_HI); | |
6aa4ef4d PH |
249 | |
250 | /* __i2c_transfer returns the number of successfully transferred | |
251 | * messages. | |
252 | * So rc should be greater than 0 here otherwise we have an error. | |
253 | */ | |
aad628c1 PH |
254 | if (rc <= 0) |
255 | return -EIO; | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | /* | |
261 | * iic_tpm_write() - write to TPM register | |
262 | * @addr: register address to write to | |
263 | * @buffer: containing data to be written | |
264 | * @len: number of bytes to write | |
265 | * | |
266 | * Write len bytes from provided buffer to TPM register (little | |
267 | * endian format, i.e. buffer[0] is written as first byte). | |
268 | * | |
269 | * NOTE: TPM is big-endian for multi-byte values. Multi-byte | |
270 | * values have to be swapped. | |
271 | * | |
272 | * NOTE: use this function instead of the iic_tpm_write_generic function. | |
273 | * | |
274 | * Return -EIO on error, 0 on success | |
275 | */ | |
276 | static int iic_tpm_write(u8 addr, u8 *buffer, size_t len) | |
277 | { | |
278 | return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LOW, | |
279 | SLEEP_DURATION_HI, MAX_COUNT); | |
280 | } | |
281 | ||
282 | /* | |
283 | * This function is needed especially for the cleanup situation after | |
284 | * sending TPM_READY | |
285 | * */ | |
286 | static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len) | |
287 | { | |
288 | return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG_LOW, | |
289 | SLEEP_DURATION_LONG_HI, MAX_COUNT_LONG); | |
290 | } | |
291 | ||
292 | enum tis_access { | |
293 | TPM_ACCESS_VALID = 0x80, | |
294 | TPM_ACCESS_ACTIVE_LOCALITY = 0x20, | |
295 | TPM_ACCESS_REQUEST_PENDING = 0x04, | |
296 | TPM_ACCESS_REQUEST_USE = 0x02, | |
297 | }; | |
298 | ||
299 | enum tis_status { | |
300 | TPM_STS_VALID = 0x80, | |
301 | TPM_STS_COMMAND_READY = 0x40, | |
302 | TPM_STS_GO = 0x20, | |
303 | TPM_STS_DATA_AVAIL = 0x10, | |
304 | TPM_STS_DATA_EXPECT = 0x08, | |
305 | }; | |
306 | ||
307 | enum tis_defaults { | |
308 | TIS_SHORT_TIMEOUT = 750, /* ms */ | |
309 | TIS_LONG_TIMEOUT = 2000, /* 2 sec */ | |
310 | }; | |
311 | ||
312 | #define TPM_ACCESS(l) (0x0000 | ((l) << 4)) | |
313 | #define TPM_STS(l) (0x0001 | ((l) << 4)) | |
314 | #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4)) | |
315 | #define TPM_DID_VID(l) (0x0006 | ((l) << 4)) | |
316 | ||
84d25940 | 317 | static bool check_locality(struct tpm_chip *chip, int loc) |
aad628c1 PH |
318 | { |
319 | u8 buf; | |
320 | int rc; | |
321 | ||
322 | rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1); | |
323 | if (rc < 0) | |
84d25940 | 324 | return false; |
aad628c1 PH |
325 | |
326 | if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == | |
327 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) { | |
56671c89 | 328 | tpm_dev.locality = loc; |
84d25940 | 329 | return true; |
aad628c1 PH |
330 | } |
331 | ||
84d25940 | 332 | return false; |
aad628c1 PH |
333 | } |
334 | ||
335 | /* implementation similar to tpm_tis */ | |
336 | static void release_locality(struct tpm_chip *chip, int loc, int force) | |
337 | { | |
338 | u8 buf; | |
339 | if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0) | |
340 | return; | |
341 | ||
342 | if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) == | |
343 | (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) { | |
344 | buf = TPM_ACCESS_ACTIVE_LOCALITY; | |
345 | iic_tpm_write(TPM_ACCESS(loc), &buf, 1); | |
346 | } | |
347 | } | |
348 | ||
349 | static int request_locality(struct tpm_chip *chip, int loc) | |
350 | { | |
351 | unsigned long stop; | |
352 | u8 buf = TPM_ACCESS_REQUEST_USE; | |
353 | ||
84d25940 | 354 | if (check_locality(chip, loc)) |
aad628c1 PH |
355 | return loc; |
356 | ||
357 | iic_tpm_write(TPM_ACCESS(loc), &buf, 1); | |
358 | ||
359 | /* wait for burstcount */ | |
af782f33 | 360 | stop = jiffies + chip->timeout_a; |
aad628c1 | 361 | do { |
84d25940 | 362 | if (check_locality(chip, loc)) |
aad628c1 PH |
363 | return loc; |
364 | usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); | |
365 | } while (time_before(jiffies, stop)); | |
366 | ||
367 | return -ETIME; | |
368 | } | |
369 | ||
370 | static u8 tpm_tis_i2c_status(struct tpm_chip *chip) | |
371 | { | |
372 | /* NOTE: since I2C read may fail, return 0 in this case --> time-out */ | |
c61c86dd PH |
373 | u8 buf = 0xFF; |
374 | u8 i = 0; | |
375 | ||
376 | do { | |
56671c89 | 377 | if (iic_tpm_read(TPM_STS(tpm_dev.locality), &buf, 1) < 0) |
c61c86dd PH |
378 | return 0; |
379 | ||
380 | i++; | |
381 | /* if locallity is set STS should not be 0xFF */ | |
382 | } while ((buf == 0xFF) && i < 10); | |
383 | ||
384 | return buf; | |
aad628c1 PH |
385 | } |
386 | ||
387 | static void tpm_tis_i2c_ready(struct tpm_chip *chip) | |
388 | { | |
389 | /* this causes the current command to be aborted */ | |
390 | u8 buf = TPM_STS_COMMAND_READY; | |
56671c89 | 391 | iic_tpm_write_long(TPM_STS(tpm_dev.locality), &buf, 1); |
aad628c1 PH |
392 | } |
393 | ||
394 | static ssize_t get_burstcount(struct tpm_chip *chip) | |
395 | { | |
396 | unsigned long stop; | |
397 | ssize_t burstcnt; | |
398 | u8 buf[3]; | |
399 | ||
400 | /* wait for burstcount */ | |
401 | /* which timeout value, spec has 2 answers (c & d) */ | |
af782f33 | 402 | stop = jiffies + chip->timeout_d; |
aad628c1 PH |
403 | do { |
404 | /* Note: STS is little endian */ | |
56671c89 | 405 | if (iic_tpm_read(TPM_STS(tpm_dev.locality)+1, buf, 3) < 0) |
aad628c1 PH |
406 | burstcnt = 0; |
407 | else | |
408 | burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0]; | |
409 | ||
410 | if (burstcnt) | |
411 | return burstcnt; | |
412 | ||
413 | usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); | |
414 | } while (time_before(jiffies, stop)); | |
415 | return -EBUSY; | |
416 | } | |
417 | ||
418 | static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, | |
419 | int *status) | |
420 | { | |
421 | unsigned long stop; | |
422 | ||
423 | /* check current status */ | |
424 | *status = tpm_tis_i2c_status(chip); | |
c61c86dd | 425 | if ((*status != 0xFF) && (*status & mask) == mask) |
aad628c1 PH |
426 | return 0; |
427 | ||
428 | stop = jiffies + timeout; | |
429 | do { | |
430 | /* since we just checked the status, give the TPM some time */ | |
431 | usleep_range(TPM_TIMEOUT_US_LOW, TPM_TIMEOUT_US_HI); | |
432 | *status = tpm_tis_i2c_status(chip); | |
433 | if ((*status & mask) == mask) | |
434 | return 0; | |
435 | ||
436 | } while (time_before(jiffies, stop)); | |
437 | ||
438 | return -ETIME; | |
439 | } | |
440 | ||
441 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) | |
442 | { | |
443 | size_t size = 0; | |
444 | ssize_t burstcnt; | |
445 | u8 retries = 0; | |
446 | int rc; | |
447 | ||
448 | while (size < count) { | |
449 | burstcnt = get_burstcount(chip); | |
450 | ||
451 | /* burstcnt < 0 = TPM is busy */ | |
452 | if (burstcnt < 0) | |
453 | return burstcnt; | |
454 | ||
455 | /* limit received data to max. left */ | |
456 | if (burstcnt > (count - size)) | |
457 | burstcnt = count - size; | |
458 | ||
56671c89 | 459 | rc = iic_tpm_read(TPM_DATA_FIFO(tpm_dev.locality), |
aad628c1 PH |
460 | &(buf[size]), burstcnt); |
461 | if (rc == 0) | |
462 | size += burstcnt; | |
463 | else if (rc < 0) | |
464 | retries++; | |
465 | ||
466 | /* avoid endless loop in case of broken HW */ | |
467 | if (retries > MAX_COUNT_LONG) | |
468 | return -EIO; | |
aad628c1 PH |
469 | } |
470 | return size; | |
471 | } | |
472 | ||
473 | static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count) | |
474 | { | |
475 | int size = 0; | |
476 | int expected, status; | |
477 | ||
478 | if (count < TPM_HEADER_SIZE) { | |
479 | size = -EIO; | |
480 | goto out; | |
481 | } | |
482 | ||
483 | /* read first 10 bytes, including tag, paramsize, and result */ | |
484 | size = recv_data(chip, buf, TPM_HEADER_SIZE); | |
485 | if (size < TPM_HEADER_SIZE) { | |
8cfffc9d | 486 | dev_err(&chip->dev, "Unable to read header\n"); |
aad628c1 PH |
487 | goto out; |
488 | } | |
489 | ||
490 | expected = be32_to_cpu(*(__be32 *)(buf + 2)); | |
491 | if ((size_t) expected > count) { | |
492 | size = -EIO; | |
493 | goto out; | |
494 | } | |
495 | ||
496 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], | |
497 | expected - TPM_HEADER_SIZE); | |
498 | if (size < expected) { | |
8cfffc9d | 499 | dev_err(&chip->dev, "Unable to read remainder of result\n"); |
aad628c1 PH |
500 | size = -ETIME; |
501 | goto out; | |
502 | } | |
503 | ||
af782f33 | 504 | wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status); |
aad628c1 | 505 | if (status & TPM_STS_DATA_AVAIL) { /* retry? */ |
8cfffc9d | 506 | dev_err(&chip->dev, "Error left over data\n"); |
aad628c1 PH |
507 | size = -EIO; |
508 | goto out; | |
509 | } | |
510 | ||
511 | out: | |
512 | tpm_tis_i2c_ready(chip); | |
513 | /* The TPM needs some time to clean up here, | |
514 | * so we sleep rather than keeping the bus busy | |
515 | */ | |
516 | usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); | |
56671c89 | 517 | release_locality(chip, tpm_dev.locality, 0); |
aad628c1 PH |
518 | return size; |
519 | } | |
520 | ||
521 | static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len) | |
522 | { | |
523 | int rc, status; | |
524 | ssize_t burstcnt; | |
525 | size_t count = 0; | |
526 | u8 retries = 0; | |
527 | u8 sts = TPM_STS_GO; | |
528 | ||
529 | if (len > TPM_BUFSIZE) | |
530 | return -E2BIG; /* command is too long for our tpm, sorry */ | |
531 | ||
532 | if (request_locality(chip, 0) < 0) | |
533 | return -EBUSY; | |
534 | ||
535 | status = tpm_tis_i2c_status(chip); | |
536 | if ((status & TPM_STS_COMMAND_READY) == 0) { | |
537 | tpm_tis_i2c_ready(chip); | |
538 | if (wait_for_stat | |
539 | (chip, TPM_STS_COMMAND_READY, | |
af782f33 | 540 | chip->timeout_b, &status) < 0) { |
aad628c1 PH |
541 | rc = -ETIME; |
542 | goto out_err; | |
543 | } | |
544 | } | |
545 | ||
546 | while (count < len - 1) { | |
547 | burstcnt = get_burstcount(chip); | |
548 | ||
549 | /* burstcnt < 0 = TPM is busy */ | |
550 | if (burstcnt < 0) | |
551 | return burstcnt; | |
552 | ||
553 | if (burstcnt > (len - 1 - count)) | |
554 | burstcnt = len - 1 - count; | |
555 | ||
56671c89 | 556 | rc = iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality), |
aad628c1 PH |
557 | &(buf[count]), burstcnt); |
558 | if (rc == 0) | |
559 | count += burstcnt; | |
560 | else if (rc < 0) | |
561 | retries++; | |
562 | ||
563 | /* avoid endless loop in case of broken HW */ | |
564 | if (retries > MAX_COUNT_LONG) { | |
565 | rc = -EIO; | |
566 | goto out_err; | |
567 | } | |
568 | ||
569 | wait_for_stat(chip, TPM_STS_VALID, | |
af782f33 | 570 | chip->timeout_c, &status); |
aad628c1 PH |
571 | |
572 | if ((status & TPM_STS_DATA_EXPECT) == 0) { | |
573 | rc = -EIO; | |
574 | goto out_err; | |
575 | } | |
aad628c1 PH |
576 | } |
577 | ||
578 | /* write last byte */ | |
56671c89 | 579 | iic_tpm_write(TPM_DATA_FIFO(tpm_dev.locality), &(buf[count]), 1); |
af782f33 | 580 | wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status); |
aad628c1 PH |
581 | if ((status & TPM_STS_DATA_EXPECT) != 0) { |
582 | rc = -EIO; | |
583 | goto out_err; | |
584 | } | |
585 | ||
586 | /* go and do it */ | |
56671c89 | 587 | iic_tpm_write(TPM_STS(tpm_dev.locality), &sts, 1); |
aad628c1 PH |
588 | |
589 | return len; | |
590 | out_err: | |
591 | tpm_tis_i2c_ready(chip); | |
592 | /* The TPM needs some time to clean up here, | |
593 | * so we sleep rather than keeping the bus busy | |
594 | */ | |
595 | usleep_range(SLEEP_DURATION_RESET_LOW, SLEEP_DURATION_RESET_HI); | |
56671c89 | 596 | release_locality(chip, tpm_dev.locality, 0); |
aad628c1 PH |
597 | return rc; |
598 | } | |
599 | ||
1f866057 SB |
600 | static bool tpm_tis_i2c_req_canceled(struct tpm_chip *chip, u8 status) |
601 | { | |
602 | return (status == TPM_STS_COMMAND_READY); | |
603 | } | |
604 | ||
01ad1fa7 | 605 | static const struct tpm_class_ops tpm_tis_i2c = { |
cae8b441 | 606 | .flags = TPM_OPS_AUTO_STARTUP, |
aad628c1 PH |
607 | .status = tpm_tis_i2c_status, |
608 | .recv = tpm_tis_i2c_recv, | |
609 | .send = tpm_tis_i2c_send, | |
610 | .cancel = tpm_tis_i2c_ready, | |
611 | .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, | |
612 | .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, | |
1f866057 | 613 | .req_canceled = tpm_tis_i2c_req_canceled, |
aad628c1 PH |
614 | }; |
615 | ||
afc6d369 | 616 | static int tpm_tis_i2c_init(struct device *dev) |
aad628c1 PH |
617 | { |
618 | u32 vendor; | |
619 | int rc = 0; | |
620 | struct tpm_chip *chip; | |
621 | ||
afb5abc2 JS |
622 | chip = tpmm_chip_alloc(dev, &tpm_tis_i2c); |
623 | if (IS_ERR(chip)) | |
624 | return PTR_ERR(chip); | |
aad628c1 | 625 | |
aad628c1 | 626 | /* Default timeouts */ |
af782f33 CR |
627 | chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
628 | chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); | |
629 | chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | |
630 | chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | |
aad628c1 PH |
631 | |
632 | if (request_locality(chip, 0) != 0) { | |
c61c86dd | 633 | dev_err(dev, "could not request locality\n"); |
aad628c1 | 634 | rc = -ENODEV; |
afb5abc2 | 635 | goto out_err; |
aad628c1 PH |
636 | } |
637 | ||
638 | /* read four bytes from DID_VID register */ | |
639 | if (iic_tpm_read(TPM_DID_VID(0), (u8 *)&vendor, 4) < 0) { | |
c61c86dd | 640 | dev_err(dev, "could not read vendor id\n"); |
aad628c1 PH |
641 | rc = -EIO; |
642 | goto out_release; | |
643 | } | |
644 | ||
c61c86dd PH |
645 | if (vendor == TPM_TIS_I2C_DID_VID_9645) { |
646 | tpm_dev.chip_type = SLB9645; | |
647 | } else if (vendor == TPM_TIS_I2C_DID_VID_9635) { | |
648 | tpm_dev.chip_type = SLB9635; | |
649 | } else { | |
650 | dev_err(dev, "vendor id did not match! ID was %08x\n", vendor); | |
aad628c1 PH |
651 | rc = -ENODEV; |
652 | goto out_release; | |
653 | } | |
654 | ||
655 | dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16); | |
656 | ||
aad628c1 PH |
657 | tpm_dev.chip = chip; |
658 | ||
afb5abc2 | 659 | return tpm_chip_register(chip); |
aad628c1 | 660 | out_release: |
56671c89 | 661 | release_locality(chip, tpm_dev.locality, 1); |
aad628c1 | 662 | tpm_dev.client = NULL; |
aad628c1 PH |
663 | out_err: |
664 | return rc; | |
665 | } | |
666 | ||
667 | static const struct i2c_device_id tpm_tis_i2c_table[] = { | |
b4f20826 JMC |
668 | {"tpm_i2c_infineon"}, |
669 | {"slb9635tt"}, | |
670 | {"slb9645tt"}, | |
aad628c1 PH |
671 | {}, |
672 | }; | |
673 | ||
674 | MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_table); | |
c61c86dd PH |
675 | |
676 | #ifdef CONFIG_OF | |
677 | static const struct of_device_id tpm_tis_i2c_of_match[] = { | |
b4f20826 JMC |
678 | {.compatible = "infineon,tpm_i2c_infineon"}, |
679 | {.compatible = "infineon,slb9635tt"}, | |
680 | {.compatible = "infineon,slb9645tt"}, | |
c61c86dd PH |
681 | {}, |
682 | }; | |
683 | MODULE_DEVICE_TABLE(of, tpm_tis_i2c_of_match); | |
684 | #endif | |
685 | ||
aad628c1 PH |
686 | static SIMPLE_DEV_PM_OPS(tpm_tis_i2c_ops, tpm_pm_suspend, tpm_pm_resume); |
687 | ||
afc6d369 | 688 | static int tpm_tis_i2c_probe(struct i2c_client *client, |
aad628c1 PH |
689 | const struct i2c_device_id *id) |
690 | { | |
691 | int rc; | |
c61c86dd PH |
692 | struct device *dev = &(client->dev); |
693 | ||
694 | if (tpm_dev.client != NULL) { | |
695 | dev_err(dev, "This driver only supports one client at a time\n"); | |
aad628c1 | 696 | return -EBUSY; /* We only support one client */ |
c61c86dd | 697 | } |
aad628c1 PH |
698 | |
699 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
c61c86dd | 700 | dev_err(dev, "no algorithms associated to the i2c bus\n"); |
aad628c1 PH |
701 | return -ENODEV; |
702 | } | |
703 | ||
aad628c1 PH |
704 | tpm_dev.client = client; |
705 | rc = tpm_tis_i2c_init(&client->dev); | |
706 | if (rc != 0) { | |
aad628c1 PH |
707 | tpm_dev.client = NULL; |
708 | rc = -ENODEV; | |
709 | } | |
710 | return rc; | |
711 | } | |
712 | ||
39af33fc | 713 | static int tpm_tis_i2c_remove(struct i2c_client *client) |
aad628c1 PH |
714 | { |
715 | struct tpm_chip *chip = tpm_dev.chip; | |
aad628c1 | 716 | |
afb5abc2 | 717 | tpm_chip_unregister(chip); |
56671c89 | 718 | release_locality(chip, tpm_dev.locality, 1); |
aad628c1 | 719 | tpm_dev.client = NULL; |
aad628c1 PH |
720 | |
721 | return 0; | |
722 | } | |
723 | ||
724 | static struct i2c_driver tpm_tis_i2c_driver = { | |
aad628c1 PH |
725 | .id_table = tpm_tis_i2c_table, |
726 | .probe = tpm_tis_i2c_probe, | |
727 | .remove = tpm_tis_i2c_remove, | |
728 | .driver = { | |
729 | .name = "tpm_i2c_infineon", | |
aad628c1 | 730 | .pm = &tpm_tis_i2c_ops, |
c61c86dd | 731 | .of_match_table = of_match_ptr(tpm_tis_i2c_of_match), |
aad628c1 PH |
732 | }, |
733 | }; | |
734 | ||
735 | module_i2c_driver(tpm_tis_i2c_driver); | |
736 | MODULE_AUTHOR("Peter Huewe <peter.huewe@infineon.com>"); | |
737 | MODULE_DESCRIPTION("TPM TIS I2C Infineon Driver"); | |
c61c86dd | 738 | MODULE_VERSION("2.2.0"); |
aad628c1 | 739 | MODULE_LICENSE("GPL"); |