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