2 * STMicroelectronics TPM Linux driver for TPM ST33ZP24
3 * Copyright (C) 2009 - 2016 STMicroelectronics
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <linux/module.h>
21 #include <linux/miscdevice.h>
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 #include <linux/wait.h>
25 #include <linux/freezer.h>
26 #include <linux/string.h>
27 #include <linux/interrupt.h>
28 #include <linux/gpio.h>
29 #include <linux/sched.h>
30 #include <linux/uaccess.h>
32 #include <linux/slab.h>
37 #define TPM_ACCESS 0x0
39 #define TPM_DATA_FIFO 0x24
40 #define TPM_INTF_CAPABILITY 0x14
41 #define TPM_INT_STATUS 0x10
42 #define TPM_INT_ENABLE 0x08
46 enum st33zp24_access {
47 TPM_ACCESS_VALID = 0x80,
48 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
49 TPM_ACCESS_REQUEST_PENDING = 0x04,
50 TPM_ACCESS_REQUEST_USE = 0x02,
53 enum st33zp24_status {
55 TPM_STS_COMMAND_READY = 0x40,
57 TPM_STS_DATA_AVAIL = 0x10,
58 TPM_STS_DATA_EXPECT = 0x08,
61 enum st33zp24_int_flags {
62 TPM_GLOBAL_INT_ENABLE = 0x80,
63 TPM_INTF_CMD_READY_INT = 0x080,
64 TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
65 TPM_INTF_WAKE_UP_READY_INT = 0x020,
66 TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
67 TPM_INTF_STS_VALID_INT = 0x002,
68 TPM_INTF_DATA_AVAIL_INT = 0x001,
72 TIS_SHORT_TIMEOUT = 750,
73 TIS_LONG_TIMEOUT = 2000,
77 * clear_interruption clear the pending interrupt.
78 * @param: tpm_dev, the tpm device device.
79 * @return: the interrupt status value.
81 static u8 clear_interruption(struct st33zp24_dev *tpm_dev)
85 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
86 tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
88 } /* clear_interruption() */
91 * st33zp24_cancel, cancel the current command execution or
92 * set STS to COMMAND READY.
93 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
95 static void st33zp24_cancel(struct tpm_chip *chip)
97 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
100 data = TPM_STS_COMMAND_READY;
101 tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
102 } /* st33zp24_cancel() */
105 * st33zp24_status return the TPM_STS register
106 * @param: chip, the tpm chip description
107 * @return: the TPM_STS register value.
109 static u8 st33zp24_status(struct tpm_chip *chip)
111 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
114 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1);
116 } /* st33zp24_status() */
119 * check_locality if the locality is active
120 * @param: chip, the tpm chip description
121 * @return: the active locality or -EACCESS.
123 static int check_locality(struct tpm_chip *chip)
125 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
129 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
130 if (status && (data &
131 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
132 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
133 return tpm_dev->locality;
136 } /* check_locality() */
139 * request_locality request the TPM locality
140 * @param: chip, the chip description
141 * @return: the active locality or negative value.
143 static int request_locality(struct tpm_chip *chip)
145 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
150 if (check_locality(chip) == tpm_dev->locality)
151 return tpm_dev->locality;
153 data = TPM_ACCESS_REQUEST_USE;
154 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
158 stop = jiffies + chip->timeout_a;
160 /* Request locality is usually effective after the request */
162 if (check_locality(chip) >= 0)
163 return tpm_dev->locality;
165 } while (time_before(jiffies, stop));
167 /* could not get locality */
169 } /* request_locality() */
172 * release_locality release the active locality
173 * @param: chip, the tpm chip description.
175 static void release_locality(struct tpm_chip *chip)
177 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
180 data = TPM_ACCESS_ACTIVE_LOCALITY;
182 tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
186 * get_burstcount return the burstcount value
187 * @param: chip, the chip description
188 * return: the burstcount or negative value.
190 static int get_burstcount(struct tpm_chip *chip)
192 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
194 int burstcnt, status;
197 stop = jiffies + chip->timeout_d;
199 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 1,
205 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 2,
210 burstcnt |= temp << 8;
214 } while (time_before(jiffies, stop));
216 } /* get_burstcount() */
220 * wait_for_tpm_stat_cond
221 * @param: chip, chip description
222 * @param: mask, expected mask value
223 * @param: check_cancel, does the command expected to be canceled ?
224 * @param: canceled, did we received a cancel request ?
225 * @return: true if status == mask or if the command is canceled.
226 * false in other cases.
228 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
229 bool check_cancel, bool *canceled)
231 u8 status = chip->ops->status(chip);
234 if ((status & mask) == mask)
236 if (check_cancel && chip->ops->req_canceled(chip, status)) {
244 * wait_for_stat wait for a TPM_STS value
245 * @param: chip, the tpm chip description
246 * @param: mask, the value mask to wait
247 * @param: timeout, the timeout
248 * @param: queue, the wait queue.
249 * @param: check_cancel, does the command can be cancelled ?
250 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
252 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
253 wait_queue_head_t *queue, bool check_cancel)
255 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
258 bool canceled = false;
263 /* check current status */
264 status = st33zp24_status(chip);
265 if ((status & mask) == mask)
268 stop = jiffies + timeout;
270 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
271 cur_intrs = tpm_dev->intrs;
272 clear_interruption(tpm_dev);
273 enable_irq(tpm_dev->irq);
276 if (ret == -ERESTARTSYS && freezing(current))
277 clear_thread_flag(TIF_SIGPENDING);
279 timeout = stop - jiffies;
280 if ((long) timeout <= 0)
283 ret = wait_event_interruptible_timeout(*queue,
284 cur_intrs != tpm_dev->intrs,
286 clear_interruption(tpm_dev);
287 condition = wait_for_tpm_stat_cond(chip, mask,
288 check_cancel, &canceled);
289 if (ret >= 0 && condition) {
294 } while (ret == -ERESTARTSYS && freezing(current));
296 disable_irq_nosync(tpm_dev->irq);
301 status = chip->ops->status(chip);
302 if ((status & mask) == mask)
304 } while (time_before(jiffies, stop));
308 } /* wait_for_stat() */
311 * recv_data receive data
312 * @param: chip, the tpm chip description
313 * @param: buf, the buffer where the data are received
314 * @param: count, the number of data to receive
315 * @return: the number of bytes read from TPM FIFO.
317 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
319 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
320 int size = 0, burstcnt, len, ret;
322 while (size < count &&
324 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
326 &tpm_dev->read_queue, true) == 0) {
327 burstcnt = get_burstcount(chip);
330 len = min_t(int, burstcnt, count - size);
331 ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO,
342 * tpm_ioserirq_handler the serirq irq handler
343 * @param: irq, the tpm chip description
344 * @param: dev_id, the description of the chip
345 * @return: the status of the handler.
347 static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
349 struct tpm_chip *chip = dev_id;
350 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
353 wake_up_interruptible(&tpm_dev->read_queue);
354 disable_irq_nosync(tpm_dev->irq);
357 } /* tpm_ioserirq_handler() */
360 * st33zp24_send send TPM commands through the I2C bus.
362 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
363 * @param: buf, the buffer to send.
364 * @param: count, the number of bytes to send.
365 * @return: In case of success the number of bytes sent.
366 * In other case, a < 0 value describing the issue.
368 static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf,
371 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
372 u32 status, i, size, ordinal;
379 if (len < TPM_HEADER_SIZE)
382 ret = request_locality(chip);
386 status = st33zp24_status(chip);
387 if ((status & TPM_STS_COMMAND_READY) == 0) {
388 st33zp24_cancel(chip);
390 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
391 &tpm_dev->read_queue, false) < 0) {
397 for (i = 0; i < len - 1;) {
398 burstcnt = get_burstcount(chip);
401 size = min_t(int, len - i - 1, burstcnt);
402 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
410 status = st33zp24_status(chip);
411 if ((status & TPM_STS_DATA_EXPECT) == 0) {
416 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
421 status = st33zp24_status(chip);
422 if ((status & TPM_STS_DATA_EXPECT) != 0) {
428 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
432 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
433 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
435 ret = wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
436 tpm_calc_ordinal_duration(chip, ordinal),
437 &tpm_dev->read_queue, false);
444 st33zp24_cancel(chip);
445 release_locality(chip);
450 * st33zp24_recv received TPM response through TPM phy.
451 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
452 * @param: buf, the buffer to store datas.
453 * @param: count, the number of bytes to send.
454 * @return: In case of success the number of bytes received.
455 * In other case, a < 0 value describing the issue.
457 static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf,
466 if (count < TPM_HEADER_SIZE) {
471 size = recv_data(chip, buf, TPM_HEADER_SIZE);
472 if (size < TPM_HEADER_SIZE) {
473 dev_err(&chip->dev, "Unable to read header\n");
477 expected = be32_to_cpu(*(__be32 *)(buf + 2));
478 if (expected > count) {
483 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
484 expected - TPM_HEADER_SIZE);
485 if (size < expected) {
486 dev_err(&chip->dev, "Unable to read remainder of result\n");
491 st33zp24_cancel(chip);
492 release_locality(chip);
497 * st33zp24_req_canceled
498 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
499 * @param: status, the TPM status.
500 * @return: Does TPM ready to compute a new command ? true.
502 static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status)
504 return (status == TPM_STS_COMMAND_READY);
507 static const struct tpm_class_ops st33zp24_tpm = {
508 .send = st33zp24_send,
509 .recv = st33zp24_recv,
510 .cancel = st33zp24_cancel,
511 .status = st33zp24_status,
512 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
513 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
514 .req_canceled = st33zp24_req_canceled,
518 * st33zp24_probe initialize the TPM device
519 * @param: client, the i2c_client drescription (TPM I2C description).
520 * @param: id, the i2c_device_id struct.
521 * @return: 0 in case of success.
524 int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
525 struct device *dev, int irq, int io_lpcpd)
529 struct tpm_chip *chip;
530 struct st33zp24_dev *tpm_dev;
532 chip = tpmm_chip_alloc(dev, &st33zp24_tpm);
534 return PTR_ERR(chip);
536 tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev),
541 tpm_dev->phy_id = phy_id;
543 dev_set_drvdata(&chip->dev, tpm_dev);
545 chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
546 chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
547 chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
548 chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
550 tpm_dev->locality = LOCALITY0;
553 /* INTERRUPT Setup */
554 init_waitqueue_head(&tpm_dev->read_queue);
557 if (request_locality(chip) != LOCALITY0) {
559 goto _tpm_clean_answer;
562 clear_interruption(tpm_dev);
563 ret = devm_request_irq(dev, irq, tpm_ioserirq_handler,
564 IRQF_TRIGGER_HIGH, "TPM SERIRQ management",
567 dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n",
569 goto _tpm_clean_answer;
572 intmask |= TPM_INTF_CMD_READY_INT
573 | TPM_INTF_STS_VALID_INT
574 | TPM_INTF_DATA_AVAIL_INT;
576 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE,
579 goto _tpm_clean_answer;
581 intmask = TPM_GLOBAL_INT_ENABLE;
582 ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3),
585 goto _tpm_clean_answer;
588 chip->flags |= TPM_CHIP_FLAG_IRQ;
590 disable_irq_nosync(tpm_dev->irq);
592 tpm_gen_interrupt(chip);
595 tpm_get_timeouts(chip);
596 tpm_do_selftest(chip);
598 return tpm_chip_register(chip);
600 dev_info(&chip->dev, "TPM initialization fail\n");
603 EXPORT_SYMBOL(st33zp24_probe);
606 * st33zp24_remove remove the TPM device
607 * @param: tpm_data, the tpm phy.
608 * @return: 0 in case of success.
610 int st33zp24_remove(struct tpm_chip *chip)
612 tpm_chip_unregister(chip);
615 EXPORT_SYMBOL(st33zp24_remove);
617 #ifdef CONFIG_PM_SLEEP
619 * st33zp24_pm_suspend suspend the TPM device
620 * @param: tpm_data, the tpm phy.
621 * @param: mesg, the power management message.
622 * @return: 0 in case of success.
624 int st33zp24_pm_suspend(struct device *dev)
626 struct tpm_chip *chip = dev_get_drvdata(dev);
627 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
631 if (gpio_is_valid(tpm_dev->io_lpcpd))
632 gpio_set_value(tpm_dev->io_lpcpd, 0);
634 ret = tpm_pm_suspend(dev);
637 } /* st33zp24_pm_suspend() */
638 EXPORT_SYMBOL(st33zp24_pm_suspend);
641 * st33zp24_pm_resume resume the TPM device
642 * @param: tpm_data, the tpm phy.
643 * @return: 0 in case of success.
645 int st33zp24_pm_resume(struct device *dev)
647 struct tpm_chip *chip = dev_get_drvdata(dev);
648 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
651 if (gpio_is_valid(tpm_dev->io_lpcpd)) {
652 gpio_set_value(tpm_dev->io_lpcpd, 1);
653 ret = wait_for_stat(chip,
654 TPM_STS_VALID, chip->timeout_b,
655 &tpm_dev->read_queue, false);
657 ret = tpm_pm_resume(dev);
659 tpm_do_selftest(chip);
662 } /* st33zp24_pm_resume() */
663 EXPORT_SYMBOL(st33zp24_pm_resume);
666 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
667 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
668 MODULE_VERSION("1.3.0");
669 MODULE_LICENSE("GPL");