x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / char / tpm / tpm_tis_core.c
blob183a5f54d875d072b98e7aead6563f3dd3d95a62
1 /*
2 * Copyright (C) 2005, 2006 IBM Corporation
3 * Copyright (C) 2014, 2015 Intel Corporation
5 * Authors:
6 * Leendert van Doorn <leendert@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11 * Device driver for TCG/TCPA TPM (trusted platform module).
12 * Specifications at www.trustedcomputinggroup.org
14 * This device driver implements the TPM interface as defined in
15 * the TCG TPM Interface Spec version 1.2, revision 1.0.
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.
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/pnp.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/wait.h>
29 #include <linux/acpi.h>
30 #include <linux/freezer.h>
31 #include "tpm.h"
32 #include "tpm_tis_core.h"
34 /* This is a polling delay to check for status and burstcount.
35 * As per ddwg input, expectation is that status check and burstcount
36 * check should return within few usecs.
38 #define TPM_POLL_SLEEP 1 /* msec */
40 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value);
42 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
43 bool check_cancel, bool *canceled)
45 u8 status = chip->ops->status(chip);
47 *canceled = false;
48 if ((status & mask) == mask)
49 return true;
50 if (check_cancel && chip->ops->req_canceled(chip, status)) {
51 *canceled = true;
52 return true;
54 return false;
57 static int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask,
58 unsigned long timeout, wait_queue_head_t *queue,
59 bool check_cancel)
61 unsigned long stop;
62 long rc;
63 u8 status;
64 bool canceled = false;
66 /* check current status */
67 status = chip->ops->status(chip);
68 if ((status & mask) == mask)
69 return 0;
71 stop = jiffies + timeout;
73 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
74 again:
75 timeout = stop - jiffies;
76 if ((long)timeout <= 0)
77 return -ETIME;
78 rc = wait_event_interruptible_timeout(*queue,
79 wait_for_tpm_stat_cond(chip, mask, check_cancel,
80 &canceled),
81 timeout);
82 if (rc > 0) {
83 if (canceled)
84 return -ECANCELED;
85 return 0;
87 if (rc == -ERESTARTSYS && freezing(current)) {
88 clear_thread_flag(TIF_SIGPENDING);
89 goto again;
91 } else {
92 do {
93 tpm_msleep(TPM_POLL_SLEEP);
94 status = chip->ops->status(chip);
95 if ((status & mask) == mask)
96 return 0;
97 } while (time_before(jiffies, stop));
99 return -ETIME;
102 /* Before we attempt to access the TPM we must see that the valid bit is set.
103 * The specification says that this bit is 0 at reset and remains 0 until the
104 * 'TPM has gone through its self test and initialization and has established
105 * correct values in the other bits.'
107 static int wait_startup(struct tpm_chip *chip, int l)
109 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
110 unsigned long stop = jiffies + chip->timeout_a;
112 do {
113 int rc;
114 u8 access;
116 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
117 if (rc < 0)
118 return rc;
120 if (access & TPM_ACCESS_VALID)
121 return 0;
122 tpm_msleep(TPM_TIMEOUT);
123 } while (time_before(jiffies, stop));
124 return -1;
127 static bool check_locality(struct tpm_chip *chip, int l)
129 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
130 int rc;
131 u8 access;
133 rc = tpm_tis_read8(priv, TPM_ACCESS(l), &access);
134 if (rc < 0)
135 return false;
137 if ((access & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
138 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
139 priv->locality = l;
140 return true;
143 return false;
146 static void release_locality(struct tpm_chip *chip, int l)
148 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
150 tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_ACTIVE_LOCALITY);
153 static int request_locality(struct tpm_chip *chip, int l)
155 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
156 unsigned long stop, timeout;
157 long rc;
159 if (check_locality(chip, l))
160 return l;
162 rc = tpm_tis_write8(priv, TPM_ACCESS(l), TPM_ACCESS_REQUEST_USE);
163 if (rc < 0)
164 return rc;
166 stop = jiffies + chip->timeout_a;
168 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
169 again:
170 timeout = stop - jiffies;
171 if ((long)timeout <= 0)
172 return -1;
173 rc = wait_event_interruptible_timeout(priv->int_queue,
174 (check_locality
175 (chip, l)),
176 timeout);
177 if (rc > 0)
178 return l;
179 if (rc == -ERESTARTSYS && freezing(current)) {
180 clear_thread_flag(TIF_SIGPENDING);
181 goto again;
183 } else {
184 /* wait for burstcount */
185 do {
186 if (check_locality(chip, l))
187 return l;
188 tpm_msleep(TPM_TIMEOUT);
189 } while (time_before(jiffies, stop));
191 return -1;
194 static u8 tpm_tis_status(struct tpm_chip *chip)
196 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
197 int rc;
198 u8 status;
200 rc = tpm_tis_read8(priv, TPM_STS(priv->locality), &status);
201 if (rc < 0)
202 return 0;
204 return status;
207 static void tpm_tis_ready(struct tpm_chip *chip)
209 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
211 /* this causes the current command to be aborted */
212 tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_COMMAND_READY);
215 static int get_burstcount(struct tpm_chip *chip)
217 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
218 unsigned long stop;
219 int burstcnt, rc;
220 u32 value;
222 /* wait for burstcount */
223 if (chip->flags & TPM_CHIP_FLAG_TPM2)
224 stop = jiffies + chip->timeout_a;
225 else
226 stop = jiffies + chip->timeout_d;
227 do {
228 rc = tpm_tis_read32(priv, TPM_STS(priv->locality), &value);
229 if (rc < 0)
230 return rc;
232 burstcnt = (value >> 8) & 0xFFFF;
233 if (burstcnt)
234 return burstcnt;
235 tpm_msleep(TPM_POLL_SLEEP);
236 } while (time_before(jiffies, stop));
237 return -EBUSY;
240 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
242 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
243 int size = 0, burstcnt, rc;
245 while (size < count) {
246 rc = wait_for_tpm_stat(chip,
247 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
248 chip->timeout_c,
249 &priv->read_queue, true);
250 if (rc < 0)
251 return rc;
252 burstcnt = get_burstcount(chip);
253 if (burstcnt < 0) {
254 dev_err(&chip->dev, "Unable to read burstcount\n");
255 return burstcnt;
257 burstcnt = min_t(int, burstcnt, count - size);
259 rc = tpm_tis_read_bytes(priv, TPM_DATA_FIFO(priv->locality),
260 burstcnt, buf + size);
261 if (rc < 0)
262 return rc;
264 size += burstcnt;
266 return size;
269 static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
271 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
272 int size = 0;
273 int expected, status;
275 if (count < TPM_HEADER_SIZE) {
276 size = -EIO;
277 goto out;
280 size = recv_data(chip, buf, TPM_HEADER_SIZE);
281 /* read first 10 bytes, including tag, paramsize, and result */
282 if (size < TPM_HEADER_SIZE) {
283 dev_err(&chip->dev, "Unable to read header\n");
284 goto out;
287 expected = be32_to_cpu(*(__be32 *) (buf + 2));
288 if (expected > count) {
289 size = -EIO;
290 goto out;
293 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
294 expected - TPM_HEADER_SIZE);
295 if (size < expected) {
296 dev_err(&chip->dev, "Unable to read remainder of result\n");
297 size = -ETIME;
298 goto out;
301 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
302 &priv->int_queue, false) < 0) {
303 size = -ETIME;
304 goto out;
306 status = tpm_tis_status(chip);
307 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
308 dev_err(&chip->dev, "Error left over data\n");
309 size = -EIO;
310 goto out;
313 out:
314 tpm_tis_ready(chip);
315 return size;
319 * If interrupts are used (signaled by an irq set in the vendor structure)
320 * tpm.c can skip polling for the data to be available as the interrupt is
321 * waited for here
323 static int tpm_tis_send_data(struct tpm_chip *chip, const u8 *buf, size_t len)
325 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
326 int rc, status, burstcnt;
327 size_t count = 0;
328 bool itpm = priv->flags & TPM_TIS_ITPM_WORKAROUND;
330 status = tpm_tis_status(chip);
331 if ((status & TPM_STS_COMMAND_READY) == 0) {
332 tpm_tis_ready(chip);
333 if (wait_for_tpm_stat
334 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
335 &priv->int_queue, false) < 0) {
336 rc = -ETIME;
337 goto out_err;
341 while (count < len - 1) {
342 burstcnt = get_burstcount(chip);
343 if (burstcnt < 0) {
344 dev_err(&chip->dev, "Unable to read burstcount\n");
345 rc = burstcnt;
346 goto out_err;
348 burstcnt = min_t(int, burstcnt, len - count - 1);
349 rc = tpm_tis_write_bytes(priv, TPM_DATA_FIFO(priv->locality),
350 burstcnt, buf + count);
351 if (rc < 0)
352 goto out_err;
354 count += burstcnt;
356 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
357 &priv->int_queue, false) < 0) {
358 rc = -ETIME;
359 goto out_err;
361 status = tpm_tis_status(chip);
362 if (!itpm && (status & TPM_STS_DATA_EXPECT) == 0) {
363 rc = -EIO;
364 goto out_err;
368 /* write last byte */
369 rc = tpm_tis_write8(priv, TPM_DATA_FIFO(priv->locality), buf[count]);
370 if (rc < 0)
371 goto out_err;
373 if (wait_for_tpm_stat(chip, TPM_STS_VALID, chip->timeout_c,
374 &priv->int_queue, false) < 0) {
375 rc = -ETIME;
376 goto out_err;
378 status = tpm_tis_status(chip);
379 if (!itpm && (status & TPM_STS_DATA_EXPECT) != 0) {
380 rc = -EIO;
381 goto out_err;
384 return 0;
386 out_err:
387 tpm_tis_ready(chip);
388 return rc;
391 static void disable_interrupts(struct tpm_chip *chip)
393 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
394 u32 intmask;
395 int rc;
397 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
398 if (rc < 0)
399 intmask = 0;
401 intmask &= ~TPM_GLOBAL_INT_ENABLE;
402 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
404 devm_free_irq(chip->dev.parent, priv->irq, chip);
405 priv->irq = 0;
406 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
410 * If interrupts are used (signaled by an irq set in the vendor structure)
411 * tpm.c can skip polling for the data to be available as the interrupt is
412 * waited for here
414 static int tpm_tis_send_main(struct tpm_chip *chip, const u8 *buf, size_t len)
416 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
417 int rc;
418 u32 ordinal;
419 unsigned long dur;
421 rc = tpm_tis_send_data(chip, buf, len);
422 if (rc < 0)
423 return rc;
425 /* go and do it */
426 rc = tpm_tis_write8(priv, TPM_STS(priv->locality), TPM_STS_GO);
427 if (rc < 0)
428 goto out_err;
430 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
431 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
433 if (chip->flags & TPM_CHIP_FLAG_TPM2)
434 dur = tpm2_calc_ordinal_duration(chip, ordinal);
435 else
436 dur = tpm_calc_ordinal_duration(chip, ordinal);
438 if (wait_for_tpm_stat
439 (chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, dur,
440 &priv->read_queue, false) < 0) {
441 rc = -ETIME;
442 goto out_err;
445 return len;
446 out_err:
447 tpm_tis_ready(chip);
448 return rc;
451 static int tpm_tis_send(struct tpm_chip *chip, u8 *buf, size_t len)
453 int rc, irq;
454 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
456 if (!(chip->flags & TPM_CHIP_FLAG_IRQ) || priv->irq_tested)
457 return tpm_tis_send_main(chip, buf, len);
459 /* Verify receipt of the expected IRQ */
460 irq = priv->irq;
461 priv->irq = 0;
462 chip->flags &= ~TPM_CHIP_FLAG_IRQ;
463 rc = tpm_tis_send_main(chip, buf, len);
464 priv->irq = irq;
465 chip->flags |= TPM_CHIP_FLAG_IRQ;
466 if (!priv->irq_tested)
467 tpm_msleep(1);
468 if (!priv->irq_tested)
469 disable_interrupts(chip);
470 priv->irq_tested = true;
471 return rc;
474 struct tis_vendor_timeout_override {
475 u32 did_vid;
476 unsigned long timeout_us[4];
479 static const struct tis_vendor_timeout_override vendor_timeout_overrides[] = {
480 /* Atmel 3204 */
481 { 0x32041114, { (TIS_SHORT_TIMEOUT*1000), (TIS_LONG_TIMEOUT*1000),
482 (TIS_SHORT_TIMEOUT*1000), (TIS_SHORT_TIMEOUT*1000) } },
485 static bool tpm_tis_update_timeouts(struct tpm_chip *chip,
486 unsigned long *timeout_cap)
488 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
489 int i, rc;
490 u32 did_vid;
492 if (chip->ops->clk_enable != NULL)
493 chip->ops->clk_enable(chip, true);
495 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &did_vid);
496 if (rc < 0)
497 goto out;
499 for (i = 0; i != ARRAY_SIZE(vendor_timeout_overrides); i++) {
500 if (vendor_timeout_overrides[i].did_vid != did_vid)
501 continue;
502 memcpy(timeout_cap, vendor_timeout_overrides[i].timeout_us,
503 sizeof(vendor_timeout_overrides[i].timeout_us));
504 rc = true;
507 rc = false;
509 out:
510 if (chip->ops->clk_enable != NULL)
511 chip->ops->clk_enable(chip, false);
513 return rc;
517 * Early probing for iTPM with STS_DATA_EXPECT flaw.
518 * Try sending command without itpm flag set and if that
519 * fails, repeat with itpm flag set.
521 static int probe_itpm(struct tpm_chip *chip)
523 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
524 int rc = 0;
525 static const u8 cmd_getticks[] = {
526 0x00, 0xc1, 0x00, 0x00, 0x00, 0x0a,
527 0x00, 0x00, 0x00, 0xf1
529 size_t len = sizeof(cmd_getticks);
530 u16 vendor;
532 if (priv->flags & TPM_TIS_ITPM_WORKAROUND)
533 return 0;
535 rc = tpm_tis_read16(priv, TPM_DID_VID(0), &vendor);
536 if (rc < 0)
537 return rc;
539 /* probe only iTPMS */
540 if (vendor != TPM_VID_INTEL)
541 return 0;
543 if (request_locality(chip, 0) != 0)
544 return -EBUSY;
546 rc = tpm_tis_send_data(chip, cmd_getticks, len);
547 if (rc == 0)
548 goto out;
550 tpm_tis_ready(chip);
552 priv->flags |= TPM_TIS_ITPM_WORKAROUND;
554 rc = tpm_tis_send_data(chip, cmd_getticks, len);
555 if (rc == 0)
556 dev_info(&chip->dev, "Detected an iTPM.\n");
557 else {
558 priv->flags &= ~TPM_TIS_ITPM_WORKAROUND;
559 rc = -EFAULT;
562 out:
563 tpm_tis_ready(chip);
564 release_locality(chip, priv->locality);
566 return rc;
569 static bool tpm_tis_req_canceled(struct tpm_chip *chip, u8 status)
571 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
573 switch (priv->manufacturer_id) {
574 case TPM_VID_WINBOND:
575 return ((status == TPM_STS_VALID) ||
576 (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY)));
577 case TPM_VID_STM:
578 return (status == (TPM_STS_VALID | TPM_STS_COMMAND_READY));
579 default:
580 return (status == TPM_STS_COMMAND_READY);
584 static irqreturn_t tis_int_handler(int dummy, void *dev_id)
586 struct tpm_chip *chip = dev_id;
587 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
588 u32 interrupt;
589 int i, rc;
591 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
592 if (rc < 0)
593 return IRQ_NONE;
595 if (interrupt == 0)
596 return IRQ_NONE;
598 priv->irq_tested = true;
599 if (interrupt & TPM_INTF_DATA_AVAIL_INT)
600 wake_up_interruptible(&priv->read_queue);
601 if (interrupt & TPM_INTF_LOCALITY_CHANGE_INT)
602 for (i = 0; i < 5; i++)
603 if (check_locality(chip, i))
604 break;
605 if (interrupt &
606 (TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_STS_VALID_INT |
607 TPM_INTF_CMD_READY_INT))
608 wake_up_interruptible(&priv->int_queue);
610 /* Clear interrupts handled with TPM_EOI */
611 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), interrupt);
612 if (rc < 0)
613 return IRQ_NONE;
615 tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &interrupt);
616 return IRQ_HANDLED;
619 static int tpm_tis_gen_interrupt(struct tpm_chip *chip)
621 const char *desc = "attempting to generate an interrupt";
622 u32 cap2;
623 cap_t cap;
625 if (chip->flags & TPM_CHIP_FLAG_TPM2)
626 return tpm2_get_tpm_pt(chip, 0x100, &cap2, desc);
627 else
628 return tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, desc,
632 /* Register the IRQ and issue a command that will cause an interrupt. If an
633 * irq is seen then leave the chip setup for IRQ operation, otherwise reverse
634 * everything and leave in polling mode. Returns 0 on success.
636 static int tpm_tis_probe_irq_single(struct tpm_chip *chip, u32 intmask,
637 int flags, int irq)
639 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
640 u8 original_int_vec;
641 int rc;
642 u32 int_status;
644 if (devm_request_irq(chip->dev.parent, irq, tis_int_handler, flags,
645 dev_name(&chip->dev), chip) != 0) {
646 dev_info(&chip->dev, "Unable to request irq: %d for probe\n",
647 irq);
648 return -1;
650 priv->irq = irq;
652 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
653 &original_int_vec);
654 if (rc < 0)
655 return rc;
657 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), irq);
658 if (rc < 0)
659 return rc;
661 rc = tpm_tis_read32(priv, TPM_INT_STATUS(priv->locality), &int_status);
662 if (rc < 0)
663 return rc;
665 /* Clear all existing */
666 rc = tpm_tis_write32(priv, TPM_INT_STATUS(priv->locality), int_status);
667 if (rc < 0)
668 return rc;
670 /* Turn on */
671 rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality),
672 intmask | TPM_GLOBAL_INT_ENABLE);
673 if (rc < 0)
674 return rc;
676 priv->irq_tested = false;
678 /* Generate an interrupt by having the core call through to
679 * tpm_tis_send
681 rc = tpm_tis_gen_interrupt(chip);
682 if (rc < 0)
683 return rc;
685 /* tpm_tis_send will either confirm the interrupt is working or it
686 * will call disable_irq which undoes all of the above.
688 if (!(chip->flags & TPM_CHIP_FLAG_IRQ)) {
689 rc = tpm_tis_write8(priv, original_int_vec,
690 TPM_INT_VECTOR(priv->locality));
691 if (rc < 0)
692 return rc;
694 return 1;
697 return 0;
700 /* Try to find the IRQ the TPM is using. This is for legacy x86 systems that
701 * do not have ACPI/etc. We typically expect the interrupt to be declared if
702 * present.
704 static void tpm_tis_probe_irq(struct tpm_chip *chip, u32 intmask)
706 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
707 u8 original_int_vec;
708 int i, rc;
710 rc = tpm_tis_read8(priv, TPM_INT_VECTOR(priv->locality),
711 &original_int_vec);
712 if (rc < 0)
713 return;
715 if (!original_int_vec) {
716 if (IS_ENABLED(CONFIG_X86))
717 for (i = 3; i <= 15; i++)
718 if (!tpm_tis_probe_irq_single(chip, intmask, 0,
720 return;
721 } else if (!tpm_tis_probe_irq_single(chip, intmask, 0,
722 original_int_vec))
723 return;
726 void tpm_tis_remove(struct tpm_chip *chip)
728 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
729 u32 reg = TPM_INT_ENABLE(priv->locality);
730 u32 interrupt;
731 int rc;
733 tpm_tis_clkrun_enable(chip, true);
735 rc = tpm_tis_read32(priv, reg, &interrupt);
736 if (rc < 0)
737 interrupt = 0;
739 tpm_tis_write32(priv, reg, ~TPM_GLOBAL_INT_ENABLE & interrupt);
741 tpm_tis_clkrun_enable(chip, false);
743 if (priv->ilb_base_addr)
744 iounmap(priv->ilb_base_addr);
746 EXPORT_SYMBOL_GPL(tpm_tis_remove);
749 * tpm_tis_clkrun_enable() - Keep clkrun protocol disabled for entire duration
750 * of a single TPM command
751 * @chip: TPM chip to use
752 * @value: 1 - Disable CLKRUN protocol, so that clocks are free running
753 * 0 - Enable CLKRUN protocol
754 * Call this function directly in tpm_tis_remove() in error or driver removal
755 * path, since the chip->ops is set to NULL in tpm_chip_unregister().
757 static void tpm_tis_clkrun_enable(struct tpm_chip *chip, bool value)
759 struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
760 u32 clkrun_val;
762 if (!IS_ENABLED(CONFIG_X86) || !is_bsw() ||
763 !data->ilb_base_addr)
764 return;
766 if (value) {
767 data->clkrun_enabled++;
768 if (data->clkrun_enabled > 1)
769 return;
770 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
772 /* Disable LPC CLKRUN# */
773 clkrun_val &= ~LPC_CLKRUN_EN;
774 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
777 * Write any random value on port 0x80 which is on LPC, to make
778 * sure LPC clock is running before sending any TPM command.
780 outb(0xCC, 0x80);
781 } else {
782 data->clkrun_enabled--;
783 if (data->clkrun_enabled)
784 return;
786 clkrun_val = ioread32(data->ilb_base_addr + LPC_CNTRL_OFFSET);
788 /* Enable LPC CLKRUN# */
789 clkrun_val |= LPC_CLKRUN_EN;
790 iowrite32(clkrun_val, data->ilb_base_addr + LPC_CNTRL_OFFSET);
793 * Write any random value on port 0x80 which is on LPC, to make
794 * sure LPC clock is running before sending any TPM command.
796 outb(0xCC, 0x80);
800 static const struct tpm_class_ops tpm_tis = {
801 .flags = TPM_OPS_AUTO_STARTUP,
802 .status = tpm_tis_status,
803 .recv = tpm_tis_recv,
804 .send = tpm_tis_send,
805 .cancel = tpm_tis_ready,
806 .update_timeouts = tpm_tis_update_timeouts,
807 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
808 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
809 .req_canceled = tpm_tis_req_canceled,
810 .request_locality = request_locality,
811 .relinquish_locality = release_locality,
812 .clk_enable = tpm_tis_clkrun_enable,
815 int tpm_tis_core_init(struct device *dev, struct tpm_tis_data *priv, int irq,
816 const struct tpm_tis_phy_ops *phy_ops,
817 acpi_handle acpi_dev_handle)
819 u32 vendor;
820 u32 intfcaps;
821 u32 intmask;
822 u32 clkrun_val;
823 u8 rid;
824 int rc, probe;
825 struct tpm_chip *chip;
827 chip = tpmm_chip_alloc(dev, &tpm_tis);
828 if (IS_ERR(chip))
829 return PTR_ERR(chip);
831 #ifdef CONFIG_ACPI
832 chip->acpi_dev_handle = acpi_dev_handle;
833 #endif
835 /* Maximum timeouts */
836 chip->timeout_a = msecs_to_jiffies(TIS_TIMEOUT_A_MAX);
837 chip->timeout_b = msecs_to_jiffies(TIS_TIMEOUT_B_MAX);
838 chip->timeout_c = msecs_to_jiffies(TIS_TIMEOUT_C_MAX);
839 chip->timeout_d = msecs_to_jiffies(TIS_TIMEOUT_D_MAX);
840 priv->phy_ops = phy_ops;
841 dev_set_drvdata(&chip->dev, priv);
843 if (is_bsw()) {
844 priv->ilb_base_addr = ioremap(INTEL_LEGACY_BLK_BASE_ADDR,
845 ILB_REMAP_SIZE);
846 if (!priv->ilb_base_addr)
847 return -ENOMEM;
849 clkrun_val = ioread32(priv->ilb_base_addr + LPC_CNTRL_OFFSET);
850 /* Check if CLKRUN# is already not enabled in the LPC bus */
851 if (!(clkrun_val & LPC_CLKRUN_EN)) {
852 iounmap(priv->ilb_base_addr);
853 priv->ilb_base_addr = NULL;
857 if (chip->ops->clk_enable != NULL)
858 chip->ops->clk_enable(chip, true);
860 if (wait_startup(chip, 0) != 0) {
861 rc = -ENODEV;
862 goto out_err;
865 /* Take control of the TPM's interrupt hardware and shut it off */
866 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
867 if (rc < 0)
868 goto out_err;
870 intmask |= TPM_INTF_CMD_READY_INT | TPM_INTF_LOCALITY_CHANGE_INT |
871 TPM_INTF_DATA_AVAIL_INT | TPM_INTF_STS_VALID_INT;
872 intmask &= ~TPM_GLOBAL_INT_ENABLE;
873 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
875 rc = tpm2_probe(chip);
876 if (rc)
877 goto out_err;
879 rc = tpm_tis_read32(priv, TPM_DID_VID(0), &vendor);
880 if (rc < 0)
881 goto out_err;
883 priv->manufacturer_id = vendor;
885 rc = tpm_tis_read8(priv, TPM_RID(0), &rid);
886 if (rc < 0)
887 goto out_err;
889 dev_info(dev, "%s TPM (device-id 0x%X, rev-id %d)\n",
890 (chip->flags & TPM_CHIP_FLAG_TPM2) ? "2.0" : "1.2",
891 vendor >> 16, rid);
893 probe = probe_itpm(chip);
894 if (probe < 0) {
895 rc = -ENODEV;
896 goto out_err;
899 /* Figure out the capabilities */
900 rc = tpm_tis_read32(priv, TPM_INTF_CAPS(priv->locality), &intfcaps);
901 if (rc < 0)
902 goto out_err;
904 dev_dbg(dev, "TPM interface capabilities (0x%x):\n",
905 intfcaps);
906 if (intfcaps & TPM_INTF_BURST_COUNT_STATIC)
907 dev_dbg(dev, "\tBurst Count Static\n");
908 if (intfcaps & TPM_INTF_CMD_READY_INT)
909 dev_dbg(dev, "\tCommand Ready Int Support\n");
910 if (intfcaps & TPM_INTF_INT_EDGE_FALLING)
911 dev_dbg(dev, "\tInterrupt Edge Falling\n");
912 if (intfcaps & TPM_INTF_INT_EDGE_RISING)
913 dev_dbg(dev, "\tInterrupt Edge Rising\n");
914 if (intfcaps & TPM_INTF_INT_LEVEL_LOW)
915 dev_dbg(dev, "\tInterrupt Level Low\n");
916 if (intfcaps & TPM_INTF_INT_LEVEL_HIGH)
917 dev_dbg(dev, "\tInterrupt Level High\n");
918 if (intfcaps & TPM_INTF_LOCALITY_CHANGE_INT)
919 dev_dbg(dev, "\tLocality Change Int Support\n");
920 if (intfcaps & TPM_INTF_STS_VALID_INT)
921 dev_dbg(dev, "\tSts Valid Int Support\n");
922 if (intfcaps & TPM_INTF_DATA_AVAIL_INT)
923 dev_dbg(dev, "\tData Avail Int Support\n");
925 /* INTERRUPT Setup */
926 init_waitqueue_head(&priv->read_queue);
927 init_waitqueue_head(&priv->int_queue);
928 if (irq != -1) {
929 /* Before doing irq testing issue a command to the TPM in polling mode
930 * to make sure it works. May as well use that command to set the
931 * proper timeouts for the driver.
933 if (tpm_get_timeouts(chip)) {
934 dev_err(dev, "Could not get TPM timeouts and durations\n");
935 rc = -ENODEV;
936 goto out_err;
939 if (irq) {
940 tpm_tis_probe_irq_single(chip, intmask, IRQF_SHARED,
941 irq);
942 if (!(chip->flags & TPM_CHIP_FLAG_IRQ))
943 dev_err(&chip->dev, FW_BUG
944 "TPM interrupt not working, polling instead\n");
945 } else {
946 tpm_tis_probe_irq(chip, intmask);
950 rc = tpm_chip_register(chip);
951 if (rc)
952 goto out_err;
954 if (chip->ops->clk_enable != NULL)
955 chip->ops->clk_enable(chip, false);
957 return 0;
958 out_err:
959 if ((chip->ops != NULL) && (chip->ops->clk_enable != NULL))
960 chip->ops->clk_enable(chip, false);
962 tpm_tis_remove(chip);
964 return rc;
966 EXPORT_SYMBOL_GPL(tpm_tis_core_init);
968 #ifdef CONFIG_PM_SLEEP
969 static void tpm_tis_reenable_interrupts(struct tpm_chip *chip)
971 struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
972 u32 intmask;
973 int rc;
975 if (chip->ops->clk_enable != NULL)
976 chip->ops->clk_enable(chip, true);
978 /* reenable interrupts that device may have lost or
979 * BIOS/firmware may have disabled
981 rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq);
982 if (rc < 0)
983 goto out;
985 rc = tpm_tis_read32(priv, TPM_INT_ENABLE(priv->locality), &intmask);
986 if (rc < 0)
987 goto out;
989 intmask |= TPM_INTF_CMD_READY_INT
990 | TPM_INTF_LOCALITY_CHANGE_INT | TPM_INTF_DATA_AVAIL_INT
991 | TPM_INTF_STS_VALID_INT | TPM_GLOBAL_INT_ENABLE;
993 tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask);
995 out:
996 if (chip->ops->clk_enable != NULL)
997 chip->ops->clk_enable(chip, false);
999 return;
1002 int tpm_tis_resume(struct device *dev)
1004 struct tpm_chip *chip = dev_get_drvdata(dev);
1005 int ret;
1007 if (chip->flags & TPM_CHIP_FLAG_IRQ)
1008 tpm_tis_reenable_interrupts(chip);
1010 ret = tpm_pm_resume(dev);
1011 if (ret)
1012 return ret;
1014 /* TPM 1.2 requires self-test on resume. This function actually returns
1015 * an error code but for unknown reason it isn't handled.
1017 if (!(chip->flags & TPM_CHIP_FLAG_TPM2))
1018 tpm_do_selftest(chip);
1020 return 0;
1022 EXPORT_SYMBOL_GPL(tpm_tis_resume);
1023 #endif
1025 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1026 MODULE_DESCRIPTION("TPM Driver");
1027 MODULE_VERSION("2.0");
1028 MODULE_LICENSE("GPL");