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/acpi.h>
8 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/delay.h>
12 #include <linux/wait.h>
13 #include <linux/freezer.h>
14 #include <linux/string.h>
15 #include <linux/interrupt.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/sched.h>
18 #include <linux/uaccess.h>
20 #include <linux/slab.h>
25 #define TPM_ACCESS 0x0
27 #define TPM_DATA_FIFO 0x24
28 #define TPM_INTF_CAPABILITY 0x14
29 #define TPM_INT_STATUS 0x10
30 #define TPM_INT_ENABLE 0x08
34 enum st33zp24_access
{
35 TPM_ACCESS_VALID
= 0x80,
36 TPM_ACCESS_ACTIVE_LOCALITY
= 0x20,
37 TPM_ACCESS_REQUEST_PENDING
= 0x04,
38 TPM_ACCESS_REQUEST_USE
= 0x02,
41 enum st33zp24_status
{
43 TPM_STS_COMMAND_READY
= 0x40,
45 TPM_STS_DATA_AVAIL
= 0x10,
46 TPM_STS_DATA_EXPECT
= 0x08,
49 enum st33zp24_int_flags
{
50 TPM_GLOBAL_INT_ENABLE
= 0x80,
51 TPM_INTF_CMD_READY_INT
= 0x080,
52 TPM_INTF_FIFO_AVALAIBLE_INT
= 0x040,
53 TPM_INTF_WAKE_UP_READY_INT
= 0x020,
54 TPM_INTF_LOCALITY_CHANGE_INT
= 0x004,
55 TPM_INTF_STS_VALID_INT
= 0x002,
56 TPM_INTF_DATA_AVAIL_INT
= 0x001,
60 TIS_SHORT_TIMEOUT
= 750,
61 TIS_LONG_TIMEOUT
= 2000,
65 * clear the pending interrupt.
67 static u8
clear_interruption(struct st33zp24_dev
*tpm_dev
)
71 tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_INT_STATUS
, &interrupt
, 1);
72 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_INT_STATUS
, &interrupt
, 1);
77 * cancel the current command execution or set STS to COMMAND READY.
79 static void st33zp24_cancel(struct tpm_chip
*chip
)
81 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
84 data
= TPM_STS_COMMAND_READY
;
85 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
89 * return the TPM_STS register
91 static u8
st33zp24_status(struct tpm_chip
*chip
)
93 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
96 tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
101 * if the locality is active
103 static bool check_locality(struct tpm_chip
*chip
)
105 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
109 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
110 if (status
&& (data
&
111 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
112 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
))
118 static int request_locality(struct tpm_chip
*chip
)
120 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
125 if (check_locality(chip
))
126 return tpm_dev
->locality
;
128 data
= TPM_ACCESS_REQUEST_USE
;
129 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
133 stop
= jiffies
+ chip
->timeout_a
;
135 /* Request locality is usually effective after the request */
137 if (check_locality(chip
))
138 return tpm_dev
->locality
;
140 } while (time_before(jiffies
, stop
));
142 /* could not get locality */
146 static void release_locality(struct tpm_chip
*chip
)
148 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
151 data
= TPM_ACCESS_ACTIVE_LOCALITY
;
153 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
157 * get_burstcount return the burstcount value
159 static int get_burstcount(struct tpm_chip
*chip
)
161 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
163 int burstcnt
, status
;
166 stop
= jiffies
+ chip
->timeout_d
;
168 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
+ 1,
174 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
+ 2,
179 burstcnt
|= temp
<< 8;
183 } while (time_before(jiffies
, stop
));
187 static bool wait_for_tpm_stat_cond(struct tpm_chip
*chip
, u8 mask
,
188 bool check_cancel
, bool *canceled
)
190 u8 status
= chip
->ops
->status(chip
);
193 if ((status
& mask
) == mask
)
195 if (check_cancel
&& chip
->ops
->req_canceled(chip
, status
)) {
203 * wait for a TPM_STS value
205 static int wait_for_stat(struct tpm_chip
*chip
, u8 mask
, unsigned long timeout
,
206 wait_queue_head_t
*queue
, bool check_cancel
)
208 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
211 bool canceled
= false;
216 /* check current status */
217 status
= st33zp24_status(chip
);
218 if ((status
& mask
) == mask
)
221 stop
= jiffies
+ timeout
;
223 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
224 cur_intrs
= tpm_dev
->intrs
;
225 clear_interruption(tpm_dev
);
226 enable_irq(tpm_dev
->irq
);
229 if (ret
== -ERESTARTSYS
&& freezing(current
))
230 clear_thread_flag(TIF_SIGPENDING
);
232 timeout
= stop
- jiffies
;
233 if ((long) timeout
<= 0)
236 ret
= wait_event_interruptible_timeout(*queue
,
237 cur_intrs
!= tpm_dev
->intrs
,
239 clear_interruption(tpm_dev
);
240 condition
= wait_for_tpm_stat_cond(chip
, mask
,
241 check_cancel
, &canceled
);
242 if (ret
>= 0 && condition
) {
247 } while (ret
== -ERESTARTSYS
&& freezing(current
));
249 disable_irq_nosync(tpm_dev
->irq
);
254 status
= chip
->ops
->status(chip
);
255 if ((status
& mask
) == mask
)
257 } while (time_before(jiffies
, stop
));
263 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
265 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
266 int size
= 0, burstcnt
, len
, ret
;
268 while (size
< count
&&
270 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
272 &tpm_dev
->read_queue
, true) == 0) {
273 burstcnt
= get_burstcount(chip
);
276 len
= min_t(int, burstcnt
, count
- size
);
277 ret
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
287 static irqreturn_t
tpm_ioserirq_handler(int irq
, void *dev_id
)
289 struct tpm_chip
*chip
= dev_id
;
290 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
293 wake_up_interruptible(&tpm_dev
->read_queue
);
294 disable_irq_nosync(tpm_dev
->irq
);
300 * send TPM commands through the I2C bus.
302 static int st33zp24_send(struct tpm_chip
*chip
, unsigned char *buf
,
305 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
306 u32 status
, i
, size
, ordinal
;
311 if (len
< TPM_HEADER_SIZE
)
314 ret
= request_locality(chip
);
318 status
= st33zp24_status(chip
);
319 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
320 st33zp24_cancel(chip
);
322 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
323 &tpm_dev
->read_queue
, false) < 0) {
329 for (i
= 0; i
< len
- 1;) {
330 burstcnt
= get_burstcount(chip
);
333 size
= min_t(int, len
- i
- 1, burstcnt
);
334 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
342 status
= st33zp24_status(chip
);
343 if ((status
& TPM_STS_DATA_EXPECT
) == 0) {
348 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
353 status
= st33zp24_status(chip
);
354 if ((status
& TPM_STS_DATA_EXPECT
) != 0) {
360 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
364 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
365 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
367 ret
= wait_for_stat(chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
368 tpm_calc_ordinal_duration(chip
, ordinal
),
369 &tpm_dev
->read_queue
, false);
376 st33zp24_cancel(chip
);
377 release_locality(chip
);
381 static int st33zp24_recv(struct tpm_chip
*chip
, unsigned char *buf
,
390 if (count
< TPM_HEADER_SIZE
) {
395 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
396 if (size
< TPM_HEADER_SIZE
) {
397 dev_err(&chip
->dev
, "Unable to read header\n");
401 expected
= be32_to_cpu(*(__be32
*)(buf
+ 2));
402 if (expected
> count
|| expected
< TPM_HEADER_SIZE
) {
407 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
408 expected
- TPM_HEADER_SIZE
);
409 if (size
< expected
) {
410 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
415 st33zp24_cancel(chip
);
416 release_locality(chip
);
420 static bool st33zp24_req_canceled(struct tpm_chip
*chip
, u8 status
)
422 return (status
== TPM_STS_COMMAND_READY
);
425 static const struct tpm_class_ops st33zp24_tpm
= {
426 .flags
= TPM_OPS_AUTO_STARTUP
,
427 .send
= st33zp24_send
,
428 .recv
= st33zp24_recv
,
429 .cancel
= st33zp24_cancel
,
430 .status
= st33zp24_status
,
431 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
432 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
433 .req_canceled
= st33zp24_req_canceled
,
436 static const struct acpi_gpio_params lpcpd_gpios
= { 1, 0, false };
438 static const struct acpi_gpio_mapping acpi_st33zp24_gpios
[] = {
439 { "lpcpd-gpios", &lpcpd_gpios
, 1 },
444 * initialize the TPM device
446 int st33zp24_probe(void *phy_id
, const struct st33zp24_phy_ops
*ops
,
447 struct device
*dev
, int irq
)
451 struct tpm_chip
*chip
;
452 struct st33zp24_dev
*tpm_dev
;
454 chip
= tpmm_chip_alloc(dev
, &st33zp24_tpm
);
456 return PTR_ERR(chip
);
458 tpm_dev
= devm_kzalloc(dev
, sizeof(struct st33zp24_dev
),
463 tpm_dev
->phy_id
= phy_id
;
465 dev_set_drvdata(&chip
->dev
, tpm_dev
);
467 chip
->timeout_a
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
468 chip
->timeout_b
= msecs_to_jiffies(TIS_LONG_TIMEOUT
);
469 chip
->timeout_c
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
470 chip
->timeout_d
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
472 tpm_dev
->locality
= LOCALITY0
;
474 if (ACPI_COMPANION(dev
)) {
475 ret
= devm_acpi_dev_add_driver_gpios(dev
, acpi_st33zp24_gpios
);
481 * Get LPCPD GPIO. If lpcpd pin is not specified. This is not an
482 * issue as power management can be also managed by TPM specific
485 tpm_dev
->io_lpcpd
= devm_gpiod_get_optional(dev
, "lpcpd",
487 ret
= PTR_ERR_OR_ZERO(tpm_dev
->io_lpcpd
);
489 dev_err(dev
, "failed to request lpcpd gpio: %d\n", ret
);
494 /* INTERRUPT Setup */
495 init_waitqueue_head(&tpm_dev
->read_queue
);
498 if (request_locality(chip
) != LOCALITY0
) {
500 goto _tpm_clean_answer
;
503 clear_interruption(tpm_dev
);
504 ret
= devm_request_irq(dev
, irq
, tpm_ioserirq_handler
,
505 IRQF_TRIGGER_HIGH
, "TPM SERIRQ management",
508 dev_err(&chip
->dev
, "TPM SERIRQ signals %d not available\n",
510 goto _tpm_clean_answer
;
513 intmask
|= TPM_INTF_CMD_READY_INT
514 | TPM_INTF_STS_VALID_INT
515 | TPM_INTF_DATA_AVAIL_INT
;
517 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_INT_ENABLE
,
520 goto _tpm_clean_answer
;
522 intmask
= TPM_GLOBAL_INT_ENABLE
;
523 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, (TPM_INT_ENABLE
+ 3),
526 goto _tpm_clean_answer
;
529 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
531 disable_irq_nosync(tpm_dev
->irq
);
534 return tpm_chip_register(chip
);
536 dev_info(&chip
->dev
, "TPM initialization fail\n");
539 EXPORT_SYMBOL(st33zp24_probe
);
541 void st33zp24_remove(struct tpm_chip
*chip
)
543 tpm_chip_unregister(chip
);
545 EXPORT_SYMBOL(st33zp24_remove
);
547 #ifdef CONFIG_PM_SLEEP
548 int st33zp24_pm_suspend(struct device
*dev
)
550 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
551 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
555 if (tpm_dev
->io_lpcpd
)
556 gpiod_set_value_cansleep(tpm_dev
->io_lpcpd
, 0);
558 ret
= tpm_pm_suspend(dev
);
562 EXPORT_SYMBOL(st33zp24_pm_suspend
);
564 int st33zp24_pm_resume(struct device
*dev
)
566 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
567 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
570 if (tpm_dev
->io_lpcpd
) {
571 gpiod_set_value_cansleep(tpm_dev
->io_lpcpd
, 1);
572 ret
= wait_for_stat(chip
,
573 TPM_STS_VALID
, chip
->timeout_b
,
574 &tpm_dev
->read_queue
, false);
576 ret
= tpm_pm_resume(dev
);
578 tpm1_do_selftest(chip
);
582 EXPORT_SYMBOL(st33zp24_pm_resume
);
585 MODULE_AUTHOR("TPM support <TPMsupport@list.st.com>");
586 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
587 MODULE_VERSION("1.3.0");
588 MODULE_LICENSE("GPL");