1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * STMicroelectronics TPM Linux driver for TPM ST33ZP24
4 * Copyright (C) 2009 - 2016 STMicroelectronics
7 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/delay.h>
11 #include <linux/wait.h>
12 #include <linux/freezer.h>
13 #include <linux/string.h>
14 #include <linux/interrupt.h>
15 #include <linux/gpio.h>
16 #include <linux/sched.h>
17 #include <linux/uaccess.h>
19 #include <linux/slab.h>
24 #define TPM_ACCESS 0x0
26 #define TPM_DATA_FIFO 0x24
27 #define TPM_INTF_CAPABILITY 0x14
28 #define TPM_INT_STATUS 0x10
29 #define TPM_INT_ENABLE 0x08
33 enum st33zp24_access
{
34 TPM_ACCESS_VALID
= 0x80,
35 TPM_ACCESS_ACTIVE_LOCALITY
= 0x20,
36 TPM_ACCESS_REQUEST_PENDING
= 0x04,
37 TPM_ACCESS_REQUEST_USE
= 0x02,
40 enum st33zp24_status
{
42 TPM_STS_COMMAND_READY
= 0x40,
44 TPM_STS_DATA_AVAIL
= 0x10,
45 TPM_STS_DATA_EXPECT
= 0x08,
48 enum st33zp24_int_flags
{
49 TPM_GLOBAL_INT_ENABLE
= 0x80,
50 TPM_INTF_CMD_READY_INT
= 0x080,
51 TPM_INTF_FIFO_AVALAIBLE_INT
= 0x040,
52 TPM_INTF_WAKE_UP_READY_INT
= 0x020,
53 TPM_INTF_LOCALITY_CHANGE_INT
= 0x004,
54 TPM_INTF_STS_VALID_INT
= 0x002,
55 TPM_INTF_DATA_AVAIL_INT
= 0x001,
59 TIS_SHORT_TIMEOUT
= 750,
60 TIS_LONG_TIMEOUT
= 2000,
64 * clear_interruption clear the pending interrupt.
65 * @param: tpm_dev, the tpm device device.
66 * @return: the interrupt status value.
68 static u8
clear_interruption(struct st33zp24_dev
*tpm_dev
)
72 tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_INT_STATUS
, &interrupt
, 1);
73 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_INT_STATUS
, &interrupt
, 1);
75 } /* clear_interruption() */
78 * st33zp24_cancel, cancel the current command execution or
79 * set STS to COMMAND READY.
80 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
82 static void st33zp24_cancel(struct tpm_chip
*chip
)
84 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
87 data
= TPM_STS_COMMAND_READY
;
88 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
89 } /* st33zp24_cancel() */
92 * st33zp24_status return the TPM_STS register
93 * @param: chip, the tpm chip description
94 * @return: the TPM_STS register value.
96 static u8
st33zp24_status(struct tpm_chip
*chip
)
98 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
101 tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
103 } /* st33zp24_status() */
106 * check_locality if the locality is active
107 * @param: chip, the tpm chip description
108 * @return: true if LOCALITY0 is active, otherwise false
110 static bool check_locality(struct tpm_chip
*chip
)
112 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
116 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
117 if (status
&& (data
&
118 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
119 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
))
123 } /* check_locality() */
126 * request_locality request the TPM locality
127 * @param: chip, the chip description
128 * @return: the active locality or negative value.
130 static int request_locality(struct tpm_chip
*chip
)
132 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
137 if (check_locality(chip
))
138 return tpm_dev
->locality
;
140 data
= TPM_ACCESS_REQUEST_USE
;
141 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
145 stop
= jiffies
+ chip
->timeout_a
;
147 /* Request locality is usually effective after the request */
149 if (check_locality(chip
))
150 return tpm_dev
->locality
;
152 } while (time_before(jiffies
, stop
));
154 /* could not get locality */
156 } /* request_locality() */
159 * release_locality release the active locality
160 * @param: chip, the tpm chip description.
162 static void release_locality(struct tpm_chip
*chip
)
164 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
167 data
= TPM_ACCESS_ACTIVE_LOCALITY
;
169 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
173 * get_burstcount return the burstcount value
174 * @param: chip, the chip description
175 * return: the burstcount or negative value.
177 static int get_burstcount(struct tpm_chip
*chip
)
179 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
181 int burstcnt
, status
;
184 stop
= jiffies
+ chip
->timeout_d
;
186 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
+ 1,
192 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
+ 2,
197 burstcnt
|= temp
<< 8;
201 } while (time_before(jiffies
, stop
));
203 } /* get_burstcount() */
207 * wait_for_tpm_stat_cond
208 * @param: chip, chip description
209 * @param: mask, expected mask value
210 * @param: check_cancel, does the command expected to be canceled ?
211 * @param: canceled, did we received a cancel request ?
212 * @return: true if status == mask or if the command is canceled.
213 * false in other cases.
215 static bool wait_for_tpm_stat_cond(struct tpm_chip
*chip
, u8 mask
,
216 bool check_cancel
, bool *canceled
)
218 u8 status
= chip
->ops
->status(chip
);
221 if ((status
& mask
) == mask
)
223 if (check_cancel
&& chip
->ops
->req_canceled(chip
, status
)) {
231 * wait_for_stat wait for a TPM_STS value
232 * @param: chip, the tpm chip description
233 * @param: mask, the value mask to wait
234 * @param: timeout, the timeout
235 * @param: queue, the wait queue.
236 * @param: check_cancel, does the command can be cancelled ?
237 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
239 static int wait_for_stat(struct tpm_chip
*chip
, u8 mask
, unsigned long timeout
,
240 wait_queue_head_t
*queue
, bool check_cancel
)
242 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
245 bool canceled
= false;
250 /* check current status */
251 status
= st33zp24_status(chip
);
252 if ((status
& mask
) == mask
)
255 stop
= jiffies
+ timeout
;
257 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
258 cur_intrs
= tpm_dev
->intrs
;
259 clear_interruption(tpm_dev
);
260 enable_irq(tpm_dev
->irq
);
263 if (ret
== -ERESTARTSYS
&& freezing(current
))
264 clear_thread_flag(TIF_SIGPENDING
);
266 timeout
= stop
- jiffies
;
267 if ((long) timeout
<= 0)
270 ret
= wait_event_interruptible_timeout(*queue
,
271 cur_intrs
!= tpm_dev
->intrs
,
273 clear_interruption(tpm_dev
);
274 condition
= wait_for_tpm_stat_cond(chip
, mask
,
275 check_cancel
, &canceled
);
276 if (ret
>= 0 && condition
) {
281 } while (ret
== -ERESTARTSYS
&& freezing(current
));
283 disable_irq_nosync(tpm_dev
->irq
);
288 status
= chip
->ops
->status(chip
);
289 if ((status
& mask
) == mask
)
291 } while (time_before(jiffies
, stop
));
295 } /* wait_for_stat() */
298 * recv_data receive data
299 * @param: chip, the tpm chip description
300 * @param: buf, the buffer where the data are received
301 * @param: count, the number of data to receive
302 * @return: the number of bytes read from TPM FIFO.
304 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
306 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
307 int size
= 0, burstcnt
, len
, ret
;
309 while (size
< count
&&
311 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
313 &tpm_dev
->read_queue
, true) == 0) {
314 burstcnt
= get_burstcount(chip
);
317 len
= min_t(int, burstcnt
, count
- size
);
318 ret
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
329 * tpm_ioserirq_handler the serirq irq handler
330 * @param: irq, the tpm chip description
331 * @param: dev_id, the description of the chip
332 * @return: the status of the handler.
334 static irqreturn_t
tpm_ioserirq_handler(int irq
, void *dev_id
)
336 struct tpm_chip
*chip
= dev_id
;
337 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
340 wake_up_interruptible(&tpm_dev
->read_queue
);
341 disable_irq_nosync(tpm_dev
->irq
);
344 } /* tpm_ioserirq_handler() */
347 * st33zp24_send send TPM commands through the I2C bus.
349 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
350 * @param: buf, the buffer to send.
351 * @param: count, the number of bytes to send.
352 * @return: In case of success the number of bytes sent.
353 * In other case, a < 0 value describing the issue.
355 static int st33zp24_send(struct tpm_chip
*chip
, unsigned char *buf
,
358 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
359 u32 status
, i
, size
, ordinal
;
364 if (len
< TPM_HEADER_SIZE
)
367 ret
= request_locality(chip
);
371 status
= st33zp24_status(chip
);
372 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
373 st33zp24_cancel(chip
);
375 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
376 &tpm_dev
->read_queue
, false) < 0) {
382 for (i
= 0; i
< len
- 1;) {
383 burstcnt
= get_burstcount(chip
);
386 size
= min_t(int, len
- i
- 1, burstcnt
);
387 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
395 status
= st33zp24_status(chip
);
396 if ((status
& TPM_STS_DATA_EXPECT
) == 0) {
401 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
406 status
= st33zp24_status(chip
);
407 if ((status
& TPM_STS_DATA_EXPECT
) != 0) {
413 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
417 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
418 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
420 ret
= wait_for_stat(chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
421 tpm_calc_ordinal_duration(chip
, ordinal
),
422 &tpm_dev
->read_queue
, false);
429 st33zp24_cancel(chip
);
430 release_locality(chip
);
435 * st33zp24_recv received TPM response through TPM phy.
436 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
437 * @param: buf, the buffer to store datas.
438 * @param: count, the number of bytes to send.
439 * @return: In case of success the number of bytes received.
440 * In other case, a < 0 value describing the issue.
442 static int st33zp24_recv(struct tpm_chip
*chip
, unsigned char *buf
,
451 if (count
< TPM_HEADER_SIZE
) {
456 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
457 if (size
< TPM_HEADER_SIZE
) {
458 dev_err(&chip
->dev
, "Unable to read header\n");
462 expected
= be32_to_cpu(*(__be32
*)(buf
+ 2));
463 if (expected
> count
|| expected
< TPM_HEADER_SIZE
) {
468 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
469 expected
- TPM_HEADER_SIZE
);
470 if (size
< expected
) {
471 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
476 st33zp24_cancel(chip
);
477 release_locality(chip
);
482 * st33zp24_req_canceled
483 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
484 * @param: status, the TPM status.
485 * @return: Does TPM ready to compute a new command ? true.
487 static bool st33zp24_req_canceled(struct tpm_chip
*chip
, u8 status
)
489 return (status
== TPM_STS_COMMAND_READY
);
492 static const struct tpm_class_ops st33zp24_tpm
= {
493 .flags
= TPM_OPS_AUTO_STARTUP
,
494 .send
= st33zp24_send
,
495 .recv
= st33zp24_recv
,
496 .cancel
= st33zp24_cancel
,
497 .status
= st33zp24_status
,
498 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
499 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
500 .req_canceled
= st33zp24_req_canceled
,
504 * st33zp24_probe initialize the TPM device
505 * @param: client, the i2c_client description (TPM I2C description).
506 * @param: id, the i2c_device_id struct.
507 * @return: 0 in case of success.
510 int st33zp24_probe(void *phy_id
, const struct st33zp24_phy_ops
*ops
,
511 struct device
*dev
, int irq
, int io_lpcpd
)
515 struct tpm_chip
*chip
;
516 struct st33zp24_dev
*tpm_dev
;
518 chip
= tpmm_chip_alloc(dev
, &st33zp24_tpm
);
520 return PTR_ERR(chip
);
522 tpm_dev
= devm_kzalloc(dev
, sizeof(struct st33zp24_dev
),
527 tpm_dev
->phy_id
= phy_id
;
529 dev_set_drvdata(&chip
->dev
, tpm_dev
);
531 chip
->timeout_a
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
532 chip
->timeout_b
= msecs_to_jiffies(TIS_LONG_TIMEOUT
);
533 chip
->timeout_c
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
534 chip
->timeout_d
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
536 tpm_dev
->locality
= LOCALITY0
;
539 /* INTERRUPT Setup */
540 init_waitqueue_head(&tpm_dev
->read_queue
);
543 if (request_locality(chip
) != LOCALITY0
) {
545 goto _tpm_clean_answer
;
548 clear_interruption(tpm_dev
);
549 ret
= devm_request_irq(dev
, irq
, tpm_ioserirq_handler
,
550 IRQF_TRIGGER_HIGH
, "TPM SERIRQ management",
553 dev_err(&chip
->dev
, "TPM SERIRQ signals %d not available\n",
555 goto _tpm_clean_answer
;
558 intmask
|= TPM_INTF_CMD_READY_INT
559 | TPM_INTF_STS_VALID_INT
560 | TPM_INTF_DATA_AVAIL_INT
;
562 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_INT_ENABLE
,
565 goto _tpm_clean_answer
;
567 intmask
= TPM_GLOBAL_INT_ENABLE
;
568 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, (TPM_INT_ENABLE
+ 3),
571 goto _tpm_clean_answer
;
574 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
576 disable_irq_nosync(tpm_dev
->irq
);
579 return tpm_chip_register(chip
);
581 dev_info(&chip
->dev
, "TPM initialization fail\n");
584 EXPORT_SYMBOL(st33zp24_probe
);
587 * st33zp24_remove remove the TPM device
588 * @param: tpm_data, the tpm phy.
589 * @return: 0 in case of success.
591 int st33zp24_remove(struct tpm_chip
*chip
)
593 tpm_chip_unregister(chip
);
596 EXPORT_SYMBOL(st33zp24_remove
);
598 #ifdef CONFIG_PM_SLEEP
600 * st33zp24_pm_suspend suspend the TPM device
601 * @param: tpm_data, the tpm phy.
602 * @param: mesg, the power management message.
603 * @return: 0 in case of success.
605 int st33zp24_pm_suspend(struct device
*dev
)
607 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
608 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
612 if (gpio_is_valid(tpm_dev
->io_lpcpd
))
613 gpio_set_value(tpm_dev
->io_lpcpd
, 0);
615 ret
= tpm_pm_suspend(dev
);
618 } /* st33zp24_pm_suspend() */
619 EXPORT_SYMBOL(st33zp24_pm_suspend
);
622 * st33zp24_pm_resume resume the TPM device
623 * @param: tpm_data, the tpm phy.
624 * @return: 0 in case of success.
626 int st33zp24_pm_resume(struct device
*dev
)
628 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
629 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
632 if (gpio_is_valid(tpm_dev
->io_lpcpd
)) {
633 gpio_set_value(tpm_dev
->io_lpcpd
, 1);
634 ret
= wait_for_stat(chip
,
635 TPM_STS_VALID
, chip
->timeout_b
,
636 &tpm_dev
->read_queue
, false);
638 ret
= tpm_pm_resume(dev
);
640 tpm1_do_selftest(chip
);
643 } /* st33zp24_pm_resume() */
644 EXPORT_SYMBOL(st33zp24_pm_resume
);
647 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
648 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
649 MODULE_VERSION("1.3.0");
650 MODULE_LICENSE("GPL");