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