1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2020 Google Inc.
4 *
5 * Based on Infineon TPM driver by Peter Huewe.
6 *
7 * cr50 is a firmware for H1 secure modules that requires special
8 * handling for the I2C interface.
9 *
10 * - Use an interrupt for transaction status instead of hardcoded delays.
11 * - Must use write+wait+read read protocol.
12 * - All 4 bytes of status register must be read/written at once.
13 * - Burst count max is 63 bytes, and burst count behaves slightly differently
14 * than other I2C TPMs.
15 * - When reading from FIFO the full burstcnt must be read instead of just
16 * reading header and determining the remainder.
17 */
18
19 #include <linux/acpi.h>
20 #include <linux/bug.h>
21 #include <linux/completion.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/pm.h>
26 #include <linux/slab.h>
27 #include <linux/wait.h>
28
29 #include "tpm_tis_core.h"
30
31 #define TPM_CR50_MAX_BUFSIZE 64
32 #define TPM_CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
33 #define TPM_CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
34 #define TPM_CR50_I2C_DID_VID 0x00281ae0L /* Device and vendor ID for Cr50 H1 */
35 #define TPM_TI50_DT_I2C_DID_VID 0x504a6666L /* Device and vendor ID for Ti50 DT */
36 #define TPM_TI50_OT_I2C_DID_VID 0x50666666L /* Device and vendor ID for TI50 OT */
37 #define TPM_CR50_I2C_MAX_RETRIES 3 /* Max retries due to I2C errors */
38 #define TPM_CR50_I2C_RETRY_DELAY_LO 55 /* Min usecs between retries on I2C */
39 #define TPM_CR50_I2C_RETRY_DELAY_HI 65 /* Max usecs between retries on I2C */
40 #define TPM_CR50_I2C_DEFAULT_LOC 0
41
42 #define TPM_I2C_ACCESS(l) (0x0000 | ((l) << 4))
43 #define TPM_I2C_STS(l) (0x0001 | ((l) << 4))
44 #define TPM_I2C_DATA_FIFO(l) (0x0005 | ((l) << 4))
45 #define TPM_I2C_DID_VID(l) (0x0006 | ((l) << 4))
46
47 /**
48 * struct tpm_i2c_cr50_priv_data - Driver private data.
49 * @irq: Irq number used for this chip.
50 * If irq <= 0, then a fixed timeout is used instead of waiting for irq.
51 * @tpm_ready: Struct used by irq handler to signal R/W readiness.
52 * @buf: Buffer used for i2c writes, with i2c address prepended to content.
53 *
54 * Private driver struct used by kernel threads and interrupt context.
55 */
56 struct tpm_i2c_cr50_priv_data {
57 int irq;
58 struct completion tpm_ready;
59 u8 buf[TPM_CR50_MAX_BUFSIZE];
60 };
61
62 /**
63 * tpm_cr50_i2c_int_handler() - cr50 interrupt handler.
64 * @dummy: Unused parameter.
65 * @tpm_info: TPM chip information.
66 *
67 * The cr50 interrupt handler signals waiting threads that the
68 * interrupt has been asserted. It does not do any interrupt triggered
69 * processing but is instead used to avoid fixed delays.
70 *
71 * Return:
72 * IRQ_HANDLED signifies irq was handled by this device.
73 */
tpm_cr50_i2c_int_handler(int dummy,void * tpm_info)74 static irqreturn_t tpm_cr50_i2c_int_handler(int dummy, void *tpm_info)
75 {
76 struct tpm_chip *chip = tpm_info;
77 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
78
79 complete(&priv->tpm_ready);
80
81 return IRQ_HANDLED;
82 }
83
84 /**
85 * tpm_cr50_i2c_wait_tpm_ready() - Wait for tpm to signal ready.
86 * @chip: A TPM chip.
87 *
88 * Wait for completion interrupt if available, otherwise use a fixed
89 * delay for the TPM to be ready.
90 *
91 * Return:
92 * - 0: Success.
93 * - -errno: A POSIX error code.
94 */
tpm_cr50_i2c_wait_tpm_ready(struct tpm_chip * chip)95 static int tpm_cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
96 {
97 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
98
99 /* Use a safe fixed delay if interrupt is not supported */
100 if (priv->irq <= 0) {
101 msleep(TPM_CR50_TIMEOUT_NOIRQ_MS);
102 return 0;
103 }
104
105 /* Wait for interrupt to indicate TPM is ready to respond */
106 if (!wait_for_completion_timeout(&priv->tpm_ready, chip->timeout_a)) {
107 dev_warn(&chip->dev, "Timeout waiting for TPM ready\n");
108 return -ETIMEDOUT;
109 }
110
111 return 0;
112 }
113
114 /**
115 * tpm_cr50_i2c_enable_tpm_irq() - Enable TPM irq.
116 * @chip: A TPM chip.
117 */
tpm_cr50_i2c_enable_tpm_irq(struct tpm_chip * chip)118 static void tpm_cr50_i2c_enable_tpm_irq(struct tpm_chip *chip)
119 {
120 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
121
122 if (priv->irq > 0) {
123 reinit_completion(&priv->tpm_ready);
124 enable_irq(priv->irq);
125 }
126 }
127
128 /**
129 * tpm_cr50_i2c_disable_tpm_irq() - Disable TPM irq.
130 * @chip: A TPM chip.
131 */
tpm_cr50_i2c_disable_tpm_irq(struct tpm_chip * chip)132 static void tpm_cr50_i2c_disable_tpm_irq(struct tpm_chip *chip)
133 {
134 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
135
136 if (priv->irq > 0)
137 disable_irq(priv->irq);
138 }
139
140 /**
141 * tpm_cr50_i2c_transfer_message() - Transfer a message over i2c.
142 * @dev: Device information.
143 * @adapter: I2C adapter.
144 * @msg: Message to transfer.
145 *
146 * Call unlocked i2c transfer routine with the provided parameters and
147 * retry in case of bus errors.
148 *
149 * Return:
150 * - 0: Success.
151 * - -errno: A POSIX error code.
152 */
tpm_cr50_i2c_transfer_message(struct device * dev,struct i2c_adapter * adapter,struct i2c_msg * msg)153 static int tpm_cr50_i2c_transfer_message(struct device *dev,
154 struct i2c_adapter *adapter,
155 struct i2c_msg *msg)
156 {
157 unsigned int try;
158 int rc;
159
160 for (try = 0; try < TPM_CR50_I2C_MAX_RETRIES; try++) {
161 rc = __i2c_transfer(adapter, msg, 1);
162 if (rc == 1)
163 return 0; /* Successfully transferred the message */
164 if (try)
165 dev_warn(dev, "i2c transfer failed (attempt %d/%d): %d\n",
166 try + 1, TPM_CR50_I2C_MAX_RETRIES, rc);
167 usleep_range(TPM_CR50_I2C_RETRY_DELAY_LO, TPM_CR50_I2C_RETRY_DELAY_HI);
168 }
169
170 /* No i2c message transferred */
171 return -EIO;
172 }
173
174 /**
175 * tpm_cr50_i2c_read() - Read from TPM register.
176 * @chip: A TPM chip.
177 * @addr: Register address to read from.
178 * @buffer: Read destination, provided by caller.
179 * @len: Number of bytes to read.
180 *
181 * Sends the register address byte to the TPM, then waits until TPM
182 * is ready via interrupt signal or timeout expiration, then 'len'
183 * bytes are read from TPM response into the provided 'buffer'.
184 *
185 * Return:
186 * - 0: Success.
187 * - -errno: A POSIX error code.
188 */
tpm_cr50_i2c_read(struct tpm_chip * chip,u8 addr,u8 * buffer,size_t len)189 static int tpm_cr50_i2c_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t len)
190 {
191 struct i2c_client *client = to_i2c_client(chip->dev.parent);
192 struct i2c_msg msg_reg_addr = {
193 .addr = client->addr,
194 .len = 1,
195 .buf = &addr
196 };
197 struct i2c_msg msg_response = {
198 .addr = client->addr,
199 .flags = I2C_M_RD,
200 .len = len,
201 .buf = buffer
202 };
203 int rc;
204
205 /* Prepare for completion interrupt */
206 tpm_cr50_i2c_enable_tpm_irq(chip);
207
208 /* Send the register address byte to the TPM */
209 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_reg_addr);
210 if (rc < 0)
211 goto out;
212
213 /* Wait for TPM to be ready with response data */
214 rc = tpm_cr50_i2c_wait_tpm_ready(chip);
215 if (rc < 0)
216 goto out;
217
218 /* Read response data from the TPM */
219 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_response);
220
221 out:
222 tpm_cr50_i2c_disable_tpm_irq(chip);
223
224 if (rc < 0)
225 return rc;
226
227 return 0;
228 }
229
230 /**
231 * tpm_cr50_i2c_write()- Write to TPM register.
232 * @chip: A TPM chip.
233 * @addr: Register address to write to.
234 * @buffer: Data to write.
235 * @len: Number of bytes to write.
236 *
237 * The provided address is prepended to the data in 'buffer', the
238 * combined address+data is sent to the TPM, then wait for TPM to
239 * indicate it is done writing.
240 *
241 * Return:
242 * - 0: Success.
243 * - -errno: A POSIX error code.
244 */
tpm_cr50_i2c_write(struct tpm_chip * chip,u8 addr,u8 * buffer,size_t len)245 static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
246 size_t len)
247 {
248 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
249 struct i2c_client *client = to_i2c_client(chip->dev.parent);
250 struct i2c_msg msg = {
251 .addr = client->addr,
252 .len = len + 1,
253 .buf = priv->buf
254 };
255 int rc;
256
257 if (len > TPM_CR50_MAX_BUFSIZE - 1)
258 return -EINVAL;
259
260 /* Prepend the 'register address' to the buffer */
261 priv->buf[0] = addr;
262 memcpy(priv->buf + 1, buffer, len);
263
264 /* Prepare for completion interrupt */
265 tpm_cr50_i2c_enable_tpm_irq(chip);
266
267 /* Send write request buffer with address */
268 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg);
269 if (rc < 0)
270 goto out;
271
272 /* Wait for TPM to be ready, ignore timeout */
273 tpm_cr50_i2c_wait_tpm_ready(chip);
274
275 out:
276 tpm_cr50_i2c_disable_tpm_irq(chip);
277
278 if (rc < 0)
279 return rc;
280
281 return 0;
282 }
283
284 /**
285 * tpm_cr50_check_locality() - Verify if required TPM locality is active.
286 * @chip: A TPM chip.
287 * @loc: Locality to be verified
288 *
289 * Return:
290 * - loc: Success.
291 * - -errno: A POSIX error code.
292 */
tpm_cr50_check_locality(struct tpm_chip * chip,int loc)293 static int tpm_cr50_check_locality(struct tpm_chip *chip, int loc)
294 {
295 u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
296 u8 buf;
297 int rc;
298
299 rc = tpm_cr50_i2c_read(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
300 if (rc < 0)
301 return rc;
302
303 if ((buf & mask) == mask)
304 return loc;
305
306 return -EIO;
307 }
308
309 /**
310 * tpm_cr50_release_locality() - Release TPM locality.
311 * @chip: A TPM chip.
312 * @loc: Locality to be released
313 *
314 * Return:
315 * - 0: Success.
316 * - -errno: A POSIX error code.
317 */
tpm_cr50_release_locality(struct tpm_chip * chip,int loc)318 static int tpm_cr50_release_locality(struct tpm_chip *chip, int loc)
319 {
320 struct i2c_client *client = to_i2c_client(chip->dev.parent);
321 u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
322 u8 addr = TPM_I2C_ACCESS(loc);
323 u8 buf;
324 int rc;
325
326 rc = tpm_cr50_i2c_read(chip, addr, &buf, sizeof(buf));
327 if (rc < 0)
328 goto unlock_out;
329
330 if ((buf & mask) == mask) {
331 buf = TPM_ACCESS_ACTIVE_LOCALITY;
332 rc = tpm_cr50_i2c_write(chip, addr, &buf, sizeof(buf));
333 }
334
335 unlock_out:
336 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
337 return rc;
338 }
339
340 /**
341 * tpm_cr50_request_locality() - Request TPM locality.
342 * @chip: A TPM chip.
343 * @loc: Locality to be requested.
344 *
345 * Return:
346 * - loc: Success.
347 * - -errno: A POSIX error code.
348 */
tpm_cr50_request_locality(struct tpm_chip * chip,int loc)349 static int tpm_cr50_request_locality(struct tpm_chip *chip, int loc)
350 {
351 struct i2c_client *client = to_i2c_client(chip->dev.parent);
352 u8 buf = TPM_ACCESS_REQUEST_USE;
353 unsigned long stop;
354 int rc;
355
356 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
357
358 if (tpm_cr50_check_locality(chip, loc) == loc)
359 return loc;
360
361 rc = tpm_cr50_i2c_write(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
362 if (rc < 0)
363 goto unlock_out;
364
365 stop = jiffies + chip->timeout_a;
366 do {
367 if (tpm_cr50_check_locality(chip, loc) == loc)
368 return loc;
369
370 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
371 } while (time_before(jiffies, stop));
372
373 rc = -ETIMEDOUT;
374
375 unlock_out:
376 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
377 return rc;
378 }
379
380 /**
381 * tpm_cr50_i2c_tis_status() - Read cr50 tis status.
382 * @chip: A TPM chip.
383 *
384 * cr50 requires all 4 bytes of status register to be read.
385 *
386 * Return:
387 * TPM status byte.
388 */
tpm_cr50_i2c_tis_status(struct tpm_chip * chip)389 static u8 tpm_cr50_i2c_tis_status(struct tpm_chip *chip)
390 {
391 u8 buf[4];
392
393 if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf)) < 0)
394 return 0;
395
396 return buf[0];
397 }
398
399 /**
400 * tpm_cr50_i2c_tis_set_ready() - Set status register to ready.
401 * @chip: A TPM chip.
402 *
403 * cr50 requires all 4 bytes of status register to be written.
404 */
tpm_cr50_i2c_tis_set_ready(struct tpm_chip * chip)405 static void tpm_cr50_i2c_tis_set_ready(struct tpm_chip *chip)
406 {
407 u8 buf[4] = { TPM_STS_COMMAND_READY };
408
409 tpm_cr50_i2c_write(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf));
410 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
411 }
412
413 /**
414 * tpm_cr50_i2c_get_burst_and_status() - Get burst count and status.
415 * @chip: A TPM chip.
416 * @mask: Status mask.
417 * @burst: Return value for burst.
418 * @status: Return value for status.
419 *
420 * cr50 uses bytes 3:2 of status register for burst count and
421 * all 4 bytes must be read.
422 *
423 * Return:
424 * - 0: Success.
425 * - -errno: A POSIX error code.
426 */
tpm_cr50_i2c_get_burst_and_status(struct tpm_chip * chip,u8 mask,size_t * burst,u32 * status)427 static int tpm_cr50_i2c_get_burst_and_status(struct tpm_chip *chip, u8 mask,
428 size_t *burst, u32 *status)
429 {
430 unsigned long stop;
431 u8 buf[4];
432
433 *status = 0;
434
435 /* wait for burstcount */
436 stop = jiffies + chip->timeout_b;
437
438 do {
439 if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf)) < 0) {
440 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
441 continue;
442 }
443
444 *status = *buf;
445 *burst = le16_to_cpup((__le16 *)(buf + 1));
446
447 if ((*status & mask) == mask &&
448 *burst > 0 && *burst <= TPM_CR50_MAX_BUFSIZE - 1)
449 return 0;
450
451 msleep(TPM_CR50_TIMEOUT_SHORT_MS);
452 } while (time_before(jiffies, stop));
453
454 dev_err(&chip->dev, "Timeout reading burst and status\n");
455 return -ETIMEDOUT;
456 }
457
458 /**
459 * tpm_cr50_i2c_tis_recv() - TPM reception callback.
460 * @chip: A TPM chip.
461 * @buf: Reception buffer.
462 * @buf_len: Buffer length to read.
463 *
464 * Return:
465 * - >= 0: Number of read bytes.
466 * - -errno: A POSIX error code.
467 */
tpm_cr50_i2c_tis_recv(struct tpm_chip * chip,u8 * buf,size_t buf_len)468 static int tpm_cr50_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t buf_len)
469 {
470
471 u8 mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
472 size_t burstcnt, cur, len, expected;
473 u8 addr = TPM_I2C_DATA_FIFO(chip->locality);
474 u32 status;
475 int rc;
476
477 if (buf_len < TPM_HEADER_SIZE)
478 return -EINVAL;
479
480 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
481 if (rc < 0)
482 goto out_err;
483
484 if (burstcnt > buf_len || burstcnt < TPM_HEADER_SIZE) {
485 dev_err(&chip->dev,
486 "Unexpected burstcnt: %zu (max=%zu, min=%d)\n",
487 burstcnt, buf_len, TPM_HEADER_SIZE);
488 rc = -EIO;
489 goto out_err;
490 }
491
492 /* Read first chunk of burstcnt bytes */
493 rc = tpm_cr50_i2c_read(chip, addr, buf, burstcnt);
494 if (rc < 0) {
495 dev_err(&chip->dev, "Read of first chunk failed\n");
496 goto out_err;
497 }
498
499 /* Determine expected data in the return buffer */
500 expected = be32_to_cpup((__be32 *)(buf + 2));
501 if (expected > buf_len) {
502 dev_err(&chip->dev, "Buffer too small to receive i2c data\n");
503 rc = -E2BIG;
504 goto out_err;
505 }
506
507 /* Now read the rest of the data */
508 cur = burstcnt;
509 while (cur < expected) {
510 /* Read updated burst count and check status */
511 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
512 if (rc < 0)
513 goto out_err;
514
515 len = min_t(size_t, burstcnt, expected - cur);
516 rc = tpm_cr50_i2c_read(chip, addr, buf + cur, len);
517 if (rc < 0) {
518 dev_err(&chip->dev, "Read failed\n");
519 goto out_err;
520 }
521
522 cur += len;
523 }
524
525 /* Ensure TPM is done reading data */
526 rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status);
527 if (rc < 0)
528 goto out_err;
529 if (status & TPM_STS_DATA_AVAIL) {
530 dev_err(&chip->dev, "Data still available\n");
531 rc = -EIO;
532 goto out_err;
533 }
534
535 return cur;
536
537 out_err:
538 /* Abort current transaction if still pending */
539 if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
540 tpm_cr50_i2c_tis_set_ready(chip);
541
542 return rc;
543 }
544
545 /**
546 * tpm_cr50_i2c_tis_send() - TPM transmission callback.
547 * @chip: A TPM chip.
548 * @buf: Buffer to send.
549 * @len: Buffer length.
550 *
551 * Return:
552 * - 0: Success.
553 * - -errno: A POSIX error code.
554 */
tpm_cr50_i2c_tis_send(struct tpm_chip * chip,u8 * buf,size_t len)555 static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
556 {
557 size_t burstcnt, limit, sent = 0;
558 u8 tpm_go[4] = { TPM_STS_GO };
559 unsigned long stop;
560 u32 status;
561 int rc;
562
563 /* Wait until TPM is ready for a command */
564 stop = jiffies + chip->timeout_b;
565 while (!(tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
566 if (time_after(jiffies, stop)) {
567 rc = -ETIMEDOUT;
568 goto out_err;
569 }
570
571 tpm_cr50_i2c_tis_set_ready(chip);
572 }
573
574 while (len > 0) {
575 u8 mask = TPM_STS_VALID;
576
577 /* Wait for data if this is not the first chunk */
578 if (sent > 0)
579 mask |= TPM_STS_DATA_EXPECT;
580
581 /* Read burst count and check status */
582 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
583 if (rc < 0)
584 goto out_err;
585
586 /*
587 * Use burstcnt - 1 to account for the address byte
588 * that is inserted by tpm_cr50_i2c_write()
589 */
590 limit = min_t(size_t, burstcnt - 1, len);
591 rc = tpm_cr50_i2c_write(chip, TPM_I2C_DATA_FIFO(chip->locality),
592 &buf[sent], limit);
593 if (rc < 0) {
594 dev_err(&chip->dev, "Write failed\n");
595 goto out_err;
596 }
597
598 sent += limit;
599 len -= limit;
600 }
601
602 /* Ensure TPM is not expecting more data */
603 rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status);
604 if (rc < 0)
605 goto out_err;
606 if (status & TPM_STS_DATA_EXPECT) {
607 dev_err(&chip->dev, "Data still expected\n");
608 rc = -EIO;
609 goto out_err;
610 }
611
612 /* Start the TPM command */
613 rc = tpm_cr50_i2c_write(chip, TPM_I2C_STS(chip->locality), tpm_go,
614 sizeof(tpm_go));
615 if (rc < 0) {
616 dev_err(&chip->dev, "Start command failed\n");
617 goto out_err;
618 }
619 return 0;
620
621 out_err:
622 /* Abort current transaction if still pending */
623 if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
624 tpm_cr50_i2c_tis_set_ready(chip);
625
626 return rc;
627 }
628
629 /**
630 * tpm_cr50_i2c_req_canceled() - Callback to notify a request cancel.
631 * @chip: A TPM chip.
632 * @status: Status given by the cancel callback.
633 *
634 * Return:
635 * True if command is ready, False otherwise.
636 */
tpm_cr50_i2c_req_canceled(struct tpm_chip * chip,u8 status)637 static bool tpm_cr50_i2c_req_canceled(struct tpm_chip *chip, u8 status)
638 {
639 return status == TPM_STS_COMMAND_READY;
640 }
641
tpm_cr50_i2c_is_firmware_power_managed(struct device * dev)642 static bool tpm_cr50_i2c_is_firmware_power_managed(struct device *dev)
643 {
644 u8 val;
645 int ret;
646
647 /* This flag should default true when the device property is not present */
648 ret = device_property_read_u8(dev, "firmware-power-managed", &val);
649 if (ret)
650 return true;
651
652 return val;
653 }
654
655 static const struct tpm_class_ops cr50_i2c = {
656 .flags = TPM_OPS_AUTO_STARTUP,
657 .status = &tpm_cr50_i2c_tis_status,
658 .recv = &tpm_cr50_i2c_tis_recv,
659 .send = &tpm_cr50_i2c_tis_send,
660 .cancel = &tpm_cr50_i2c_tis_set_ready,
661 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
662 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
663 .req_canceled = &tpm_cr50_i2c_req_canceled,
664 .request_locality = &tpm_cr50_request_locality,
665 .relinquish_locality = &tpm_cr50_release_locality,
666 };
667
668 #ifdef CONFIG_ACPI
669 static const struct acpi_device_id cr50_i2c_acpi_id[] = {
670 { "GOOG0005", 0 },
671 {}
672 };
673 MODULE_DEVICE_TABLE(acpi, cr50_i2c_acpi_id);
674 #endif
675
676 #ifdef CONFIG_OF
677 static const struct of_device_id of_cr50_i2c_match[] = {
678 { .compatible = "google,cr50", },
679 {}
680 };
681 MODULE_DEVICE_TABLE(of, of_cr50_i2c_match);
682 #endif
683
684 /**
685 * tpm_cr50_vid_to_name() - Maps VID to name.
686 * @vendor: Vendor identifier to map to name
687 *
688 * Return:
689 * A valid string for the vendor or empty string
690 */
tpm_cr50_vid_to_name(u32 vendor)691 static const char *tpm_cr50_vid_to_name(u32 vendor)
692 {
693 switch (vendor) {
694 case TPM_CR50_I2C_DID_VID:
695 return "cr50";
696 case TPM_TI50_DT_I2C_DID_VID:
697 return "ti50 DT";
698 case TPM_TI50_OT_I2C_DID_VID:
699 return "ti50 OT";
700 default:
701 return "unknown";
702 }
703 }
704
705 /**
706 * tpm_cr50_i2c_probe() - Driver probe function.
707 * @client: I2C client information.
708 *
709 * Return:
710 * - 0: Success.
711 * - -errno: A POSIX error code.
712 */
tpm_cr50_i2c_probe(struct i2c_client * client)713 static int tpm_cr50_i2c_probe(struct i2c_client *client)
714 {
715 struct tpm_i2c_cr50_priv_data *priv;
716 struct device *dev = &client->dev;
717 struct tpm_chip *chip;
718 u32 vendor;
719 u8 buf[4];
720 int rc;
721 int loc;
722
723 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
724 return -ENODEV;
725
726 chip = tpmm_chip_alloc(dev, &cr50_i2c);
727 if (IS_ERR(chip))
728 return PTR_ERR(chip);
729
730 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
731 if (!priv)
732 return -ENOMEM;
733
734 /* cr50 is a TPM 2.0 chip */
735 chip->flags |= TPM_CHIP_FLAG_TPM2;
736 if (tpm_cr50_i2c_is_firmware_power_managed(dev))
737 chip->flags |= TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED;
738
739 /* Default timeouts */
740 chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
741 chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
742 chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
743 chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
744
745 dev_set_drvdata(&chip->dev, priv);
746 init_completion(&priv->tpm_ready);
747
748 if (client->irq > 0) {
749 rc = devm_request_irq(dev, client->irq, tpm_cr50_i2c_int_handler,
750 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
751 IRQF_NO_AUTOEN,
752 dev->driver->name, chip);
753 if (rc < 0) {
754 dev_err(dev, "Failed to probe IRQ %d\n", client->irq);
755 return rc;
756 }
757
758 priv->irq = client->irq;
759 } else {
760 dev_warn(dev, "No IRQ, will use %ums delay for TPM ready\n",
761 TPM_CR50_TIMEOUT_NOIRQ_MS);
762 }
763
764 loc = tpm_cr50_request_locality(chip, TPM_CR50_I2C_DEFAULT_LOC);
765 if (loc < 0) {
766 dev_err(dev, "Could not request locality\n");
767 return loc;
768 }
769
770 /* Read four bytes from DID_VID register */
771 rc = tpm_cr50_i2c_read(chip, TPM_I2C_DID_VID(loc), buf, sizeof(buf));
772 if (rc < 0) {
773 dev_err(dev, "Could not read vendor id\n");
774 if (tpm_cr50_release_locality(chip, loc))
775 dev_err(dev, "Could not release locality\n");
776 return rc;
777 }
778
779 rc = tpm_cr50_release_locality(chip, loc);
780 if (rc) {
781 dev_err(dev, "Could not release locality\n");
782 return rc;
783 }
784
785 vendor = le32_to_cpup((__le32 *)buf);
786 if (vendor != TPM_CR50_I2C_DID_VID &&
787 vendor != TPM_TI50_DT_I2C_DID_VID &&
788 vendor != TPM_TI50_OT_I2C_DID_VID) {
789 dev_err(dev, "Vendor ID did not match! ID was %08x\n", vendor);
790 return -ENODEV;
791 }
792
793 dev_info(dev, "%s TPM 2.0 (i2c 0x%02x irq %d id 0x%x)\n",
794 tpm_cr50_vid_to_name(vendor),
795 client->addr, client->irq, vendor >> 16);
796 return tpm_chip_register(chip);
797 }
798
799 /**
800 * tpm_cr50_i2c_remove() - Driver remove function.
801 * @client: I2C client information.
802 *
803 * Return:
804 * - 0: Success.
805 * - -errno: A POSIX error code.
806 */
tpm_cr50_i2c_remove(struct i2c_client * client)807 static void tpm_cr50_i2c_remove(struct i2c_client *client)
808 {
809 struct tpm_chip *chip = i2c_get_clientdata(client);
810 struct device *dev = &client->dev;
811
812 if (!chip) {
813 dev_crit(dev, "Could not get client data at remove, memory corruption ahead\n");
814 return;
815 }
816
817 tpm_chip_unregister(chip);
818 }
819
820 static SIMPLE_DEV_PM_OPS(cr50_i2c_pm, tpm_pm_suspend, tpm_pm_resume);
821
822 static struct i2c_driver cr50_i2c_driver = {
823 .probe = tpm_cr50_i2c_probe,
824 .remove = tpm_cr50_i2c_remove,
825 .driver = {
826 .name = "cr50_i2c",
827 .pm = &cr50_i2c_pm,
828 .acpi_match_table = ACPI_PTR(cr50_i2c_acpi_id),
829 .of_match_table = of_match_ptr(of_cr50_i2c_match),
830 },
831 };
832
833 module_i2c_driver(cr50_i2c_driver);
834
835 MODULE_DESCRIPTION("cr50 TPM I2C Driver");
836 MODULE_LICENSE("GPL");
837