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;
100 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
102 data = TPM_STS_COMMAND_READY;
103 tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
104 } /* st33zp24_cancel() */
107 * st33zp24_status return the TPM_STS register
108 * @param: chip, the tpm chip description
109 * @return: the TPM_STS register value.
111 static u8 st33zp24_status(struct tpm_chip *chip)
113 struct st33zp24_dev *tpm_dev;
116 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
118 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1);
120 } /* st33zp24_status() */
123 * check_locality if the locality is active
124 * @param: chip, the tpm chip description
125 * @return: the active locality or -EACCESS.
127 static int check_locality(struct tpm_chip *chip)
129 struct st33zp24_dev *tpm_dev;
133 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
135 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
136 if (status && (data &
137 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
138 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
139 return chip->vendor.locality;
142 } /* check_locality() */
145 * request_locality request the TPM locality
146 * @param: chip, the chip description
147 * @return: the active locality or negative value.
149 static int request_locality(struct tpm_chip *chip)
153 struct st33zp24_dev *tpm_dev;
156 if (check_locality(chip) == chip->vendor.locality)
157 return chip->vendor.locality;
159 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
161 data = TPM_ACCESS_REQUEST_USE;
162 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
166 stop = jiffies + chip->vendor.timeout_a;
168 /* Request locality is usually effective after the request */
170 if (check_locality(chip) >= 0)
171 return chip->vendor.locality;
173 } while (time_before(jiffies, stop));
175 /* could not get locality */
177 } /* request_locality() */
180 * release_locality release the active locality
181 * @param: chip, the tpm chip description.
183 static void release_locality(struct tpm_chip *chip)
185 struct st33zp24_dev *tpm_dev;
188 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
189 data = TPM_ACCESS_ACTIVE_LOCALITY;
191 tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
195 * get_burstcount return the burstcount value
196 * @param: chip, the chip description
197 * return: the burstcount or negative value.
199 static int get_burstcount(struct tpm_chip *chip)
202 int burstcnt, status;
204 struct st33zp24_dev *tpm_dev;
206 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
208 stop = jiffies + chip->vendor.timeout_d;
210 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 1,
216 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 2,
221 burstcnt |= temp << 8;
225 } while (time_before(jiffies, stop));
227 } /* get_burstcount() */
231 * wait_for_tpm_stat_cond
232 * @param: chip, chip description
233 * @param: mask, expected mask value
234 * @param: check_cancel, does the command expected to be canceled ?
235 * @param: canceled, did we received a cancel request ?
236 * @return: true if status == mask or if the command is canceled.
237 * false in other cases.
239 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
240 bool check_cancel, bool *canceled)
242 u8 status = chip->ops->status(chip);
245 if ((status & mask) == mask)
247 if (check_cancel && chip->ops->req_canceled(chip, status)) {
255 * wait_for_stat wait for a TPM_STS value
256 * @param: chip, the tpm chip description
257 * @param: mask, the value mask to wait
258 * @param: timeout, the timeout
259 * @param: queue, the wait queue.
260 * @param: check_cancel, does the command can be cancelled ?
261 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
263 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
264 wait_queue_head_t *queue, bool check_cancel)
268 bool canceled = false;
272 struct st33zp24_dev *tpm_dev;
274 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
276 /* check current status */
277 status = st33zp24_status(chip);
278 if ((status & mask) == mask)
281 stop = jiffies + timeout;
283 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
284 cur_intrs = tpm_dev->intrs;
285 clear_interruption(tpm_dev);
286 enable_irq(tpm_dev->irq);
289 if (ret == -ERESTARTSYS && freezing(current))
290 clear_thread_flag(TIF_SIGPENDING);
292 timeout = stop - jiffies;
293 if ((long) timeout <= 0)
296 ret = wait_event_interruptible_timeout(*queue,
297 cur_intrs != tpm_dev->intrs,
299 clear_interruption(tpm_dev);
300 condition = wait_for_tpm_stat_cond(chip, mask,
301 check_cancel, &canceled);
302 if (ret >= 0 && condition) {
307 } while (ret == -ERESTARTSYS && freezing(current));
309 disable_irq_nosync(tpm_dev->irq);
314 status = chip->ops->status(chip);
315 if ((status & mask) == mask)
317 } while (time_before(jiffies, stop));
321 } /* wait_for_stat() */
324 * recv_data receive data
325 * @param: chip, the tpm chip description
326 * @param: buf, the buffer where the data are received
327 * @param: count, the number of data to receive
328 * @return: the number of bytes read from TPM FIFO.
330 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
332 int size = 0, burstcnt, len, ret;
333 struct st33zp24_dev *tpm_dev;
335 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
337 while (size < count &&
339 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
340 chip->vendor.timeout_c,
341 &chip->vendor.read_queue, true) == 0) {
342 burstcnt = get_burstcount(chip);
345 len = min_t(int, burstcnt, count - size);
346 ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO,
357 * tpm_ioserirq_handler the serirq irq handler
358 * @param: irq, the tpm chip description
359 * @param: dev_id, the description of the chip
360 * @return: the status of the handler.
362 static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
364 struct tpm_chip *chip = dev_id;
365 struct st33zp24_dev *tpm_dev;
367 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
370 wake_up_interruptible(&chip->vendor.read_queue);
371 disable_irq_nosync(tpm_dev->irq);
374 } /* tpm_ioserirq_handler() */
377 * st33zp24_send send TPM commands through the I2C bus.
379 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
380 * @param: buf, the buffer to send.
381 * @param: count, the number of bytes to send.
382 * @return: In case of success the number of bytes sent.
383 * In other case, a < 0 value describing the issue.
385 static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf,
388 u32 status, i, size, ordinal;
392 struct st33zp24_dev *tpm_dev;
396 if (len < TPM_HEADER_SIZE)
399 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
401 ret = request_locality(chip);
405 status = st33zp24_status(chip);
406 if ((status & TPM_STS_COMMAND_READY) == 0) {
407 st33zp24_cancel(chip);
409 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
410 &chip->vendor.read_queue, false) < 0) {
416 for (i = 0; i < len - 1;) {
417 burstcnt = get_burstcount(chip);
420 size = min_t(int, len - i - 1, burstcnt);
421 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
429 status = st33zp24_status(chip);
430 if ((status & TPM_STS_DATA_EXPECT) == 0) {
435 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
440 status = st33zp24_status(chip);
441 if ((status & TPM_STS_DATA_EXPECT) != 0) {
447 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
451 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
452 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
454 ret = wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
455 tpm_calc_ordinal_duration(chip, ordinal),
456 &chip->vendor.read_queue, false);
463 st33zp24_cancel(chip);
464 release_locality(chip);
469 * st33zp24_recv received TPM response through TPM phy.
470 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
471 * @param: buf, the buffer to store datas.
472 * @param: count, the number of bytes to send.
473 * @return: In case of success the number of bytes received.
474 * In other case, a < 0 value describing the issue.
476 static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf,
485 if (count < TPM_HEADER_SIZE) {
490 size = recv_data(chip, buf, TPM_HEADER_SIZE);
491 if (size < TPM_HEADER_SIZE) {
492 dev_err(&chip->dev, "Unable to read header\n");
496 expected = be32_to_cpu(*(__be32 *)(buf + 2));
497 if (expected > count) {
502 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
503 expected - TPM_HEADER_SIZE);
504 if (size < expected) {
505 dev_err(&chip->dev, "Unable to read remainder of result\n");
510 st33zp24_cancel(chip);
511 release_locality(chip);
516 * st33zp24_req_canceled
517 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
518 * @param: status, the TPM status.
519 * @return: Does TPM ready to compute a new command ? true.
521 static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status)
523 return (status == TPM_STS_COMMAND_READY);
526 static const struct tpm_class_ops st33zp24_tpm = {
527 .send = st33zp24_send,
528 .recv = st33zp24_recv,
529 .cancel = st33zp24_cancel,
530 .status = st33zp24_status,
531 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
532 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
533 .req_canceled = st33zp24_req_canceled,
537 * st33zp24_probe initialize the TPM device
538 * @param: client, the i2c_client drescription (TPM I2C description).
539 * @param: id, the i2c_device_id struct.
540 * @return: 0 in case of success.
543 int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
544 struct device *dev, int irq, int io_lpcpd)
548 struct tpm_chip *chip;
549 struct st33zp24_dev *tpm_dev;
551 chip = tpmm_chip_alloc(dev, &st33zp24_tpm);
553 return PTR_ERR(chip);
555 tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev),
560 TPM_VPRIV(chip) = tpm_dev;
561 tpm_dev->phy_id = phy_id;
564 chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
565 chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
566 chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
567 chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
569 chip->vendor.locality = LOCALITY0;
572 /* INTERRUPT Setup */
573 init_waitqueue_head(&chip->vendor.read_queue);
576 if (request_locality(chip) != LOCALITY0) {
578 goto _tpm_clean_answer;
581 clear_interruption(tpm_dev);
582 ret = devm_request_irq(dev, irq, tpm_ioserirq_handler,
583 IRQF_TRIGGER_HIGH, "TPM SERIRQ management",
586 dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n",
588 goto _tpm_clean_answer;
591 intmask |= TPM_INTF_CMD_READY_INT
592 | TPM_INTF_STS_VALID_INT
593 | TPM_INTF_DATA_AVAIL_INT;
595 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE,
598 goto _tpm_clean_answer;
600 intmask = TPM_GLOBAL_INT_ENABLE;
601 ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3),
604 goto _tpm_clean_answer;
607 chip->flags |= TPM_CHIP_FLAG_IRQ;
609 disable_irq_nosync(tpm_dev->irq);
611 tpm_gen_interrupt(chip);
614 tpm_get_timeouts(chip);
615 tpm_do_selftest(chip);
617 return tpm_chip_register(chip);
619 dev_info(&chip->dev, "TPM initialization fail\n");
622 EXPORT_SYMBOL(st33zp24_probe);
625 * st33zp24_remove remove the TPM device
626 * @param: tpm_data, the tpm phy.
627 * @return: 0 in case of success.
629 int st33zp24_remove(struct tpm_chip *chip)
631 tpm_chip_unregister(chip);
634 EXPORT_SYMBOL(st33zp24_remove);
636 #ifdef CONFIG_PM_SLEEP
638 * st33zp24_pm_suspend suspend the TPM device
639 * @param: tpm_data, the tpm phy.
640 * @param: mesg, the power management message.
641 * @return: 0 in case of success.
643 int st33zp24_pm_suspend(struct device *dev)
645 struct tpm_chip *chip = dev_get_drvdata(dev);
646 struct st33zp24_dev *tpm_dev;
649 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
651 if (gpio_is_valid(tpm_dev->io_lpcpd))
652 gpio_set_value(tpm_dev->io_lpcpd, 0);
654 ret = tpm_pm_suspend(dev);
657 } /* st33zp24_pm_suspend() */
658 EXPORT_SYMBOL(st33zp24_pm_suspend);
661 * st33zp24_pm_resume resume the TPM device
662 * @param: tpm_data, the tpm phy.
663 * @return: 0 in case of success.
665 int st33zp24_pm_resume(struct device *dev)
667 struct tpm_chip *chip = dev_get_drvdata(dev);
668 struct st33zp24_dev *tpm_dev;
671 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
673 if (gpio_is_valid(tpm_dev->io_lpcpd)) {
674 gpio_set_value(tpm_dev->io_lpcpd, 1);
675 ret = wait_for_stat(chip,
676 TPM_STS_VALID, chip->vendor.timeout_b,
677 &chip->vendor.read_queue, false);
679 ret = tpm_pm_resume(dev);
681 tpm_do_selftest(chip);
684 } /* st33zp24_pm_resume() */
685 EXPORT_SYMBOL(st33zp24_pm_resume);
688 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
689 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
690 MODULE_VERSION("1.3.0");
691 MODULE_LICENSE("GPL");