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/kernel.h>
22 #include <linux/delay.h>
23 #include <linux/wait.h>
24 #include <linux/freezer.h>
25 #include <linux/string.h>
26 #include <linux/interrupt.h>
27 #include <linux/gpio.h>
28 #include <linux/sched.h>
29 #include <linux/uaccess.h>
31 #include <linux/slab.h>
36 #define TPM_ACCESS 0x0
38 #define TPM_DATA_FIFO 0x24
39 #define TPM_INTF_CAPABILITY 0x14
40 #define TPM_INT_STATUS 0x10
41 #define TPM_INT_ENABLE 0x08
45 enum st33zp24_access
{
46 TPM_ACCESS_VALID
= 0x80,
47 TPM_ACCESS_ACTIVE_LOCALITY
= 0x20,
48 TPM_ACCESS_REQUEST_PENDING
= 0x04,
49 TPM_ACCESS_REQUEST_USE
= 0x02,
52 enum st33zp24_status
{
54 TPM_STS_COMMAND_READY
= 0x40,
56 TPM_STS_DATA_AVAIL
= 0x10,
57 TPM_STS_DATA_EXPECT
= 0x08,
60 enum st33zp24_int_flags
{
61 TPM_GLOBAL_INT_ENABLE
= 0x80,
62 TPM_INTF_CMD_READY_INT
= 0x080,
63 TPM_INTF_FIFO_AVALAIBLE_INT
= 0x040,
64 TPM_INTF_WAKE_UP_READY_INT
= 0x020,
65 TPM_INTF_LOCALITY_CHANGE_INT
= 0x004,
66 TPM_INTF_STS_VALID_INT
= 0x002,
67 TPM_INTF_DATA_AVAIL_INT
= 0x001,
71 TIS_SHORT_TIMEOUT
= 750,
72 TIS_LONG_TIMEOUT
= 2000,
76 * clear_interruption clear the pending interrupt.
77 * @param: tpm_dev, the tpm device device.
78 * @return: the interrupt status value.
80 static u8
clear_interruption(struct st33zp24_dev
*tpm_dev
)
84 tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_INT_STATUS
, &interrupt
, 1);
85 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_INT_STATUS
, &interrupt
, 1);
87 } /* clear_interruption() */
90 * st33zp24_cancel, cancel the current command execution or
91 * set STS to COMMAND READY.
92 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
94 static void st33zp24_cancel(struct tpm_chip
*chip
)
96 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
99 data
= TPM_STS_COMMAND_READY
;
100 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
101 } /* st33zp24_cancel() */
104 * st33zp24_status return the TPM_STS register
105 * @param: chip, the tpm chip description
106 * @return: the TPM_STS register value.
108 static u8
st33zp24_status(struct tpm_chip
*chip
)
110 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
113 tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
115 } /* st33zp24_status() */
118 * check_locality if the locality is active
119 * @param: chip, the tpm chip description
120 * @return: true if LOCALITY0 is active, otherwise false
122 static bool check_locality(struct tpm_chip
*chip
)
124 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
128 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
129 if (status
&& (data
&
130 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
131 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
))
135 } /* check_locality() */
138 * request_locality request the TPM locality
139 * @param: chip, the chip description
140 * @return: the active locality or negative value.
142 static int request_locality(struct tpm_chip
*chip
)
144 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
149 if (check_locality(chip
))
150 return tpm_dev
->locality
;
152 data
= TPM_ACCESS_REQUEST_USE
;
153 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
157 stop
= jiffies
+ chip
->timeout_a
;
159 /* Request locality is usually effective after the request */
161 if (check_locality(chip
))
162 return tpm_dev
->locality
;
164 } while (time_before(jiffies
, stop
));
166 /* could not get locality */
168 } /* request_locality() */
171 * release_locality release the active locality
172 * @param: chip, the tpm chip description.
174 static void release_locality(struct tpm_chip
*chip
)
176 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
179 data
= TPM_ACCESS_ACTIVE_LOCALITY
;
181 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
185 * get_burstcount return the burstcount value
186 * @param: chip, the chip description
187 * return: the burstcount or negative value.
189 static int get_burstcount(struct tpm_chip
*chip
)
191 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
193 int burstcnt
, status
;
196 stop
= jiffies
+ chip
->timeout_d
;
198 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
+ 1,
204 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
+ 2,
209 burstcnt
|= temp
<< 8;
213 } while (time_before(jiffies
, stop
));
215 } /* get_burstcount() */
219 * wait_for_tpm_stat_cond
220 * @param: chip, chip description
221 * @param: mask, expected mask value
222 * @param: check_cancel, does the command expected to be canceled ?
223 * @param: canceled, did we received a cancel request ?
224 * @return: true if status == mask or if the command is canceled.
225 * false in other cases.
227 static bool wait_for_tpm_stat_cond(struct tpm_chip
*chip
, u8 mask
,
228 bool check_cancel
, bool *canceled
)
230 u8 status
= chip
->ops
->status(chip
);
233 if ((status
& mask
) == mask
)
235 if (check_cancel
&& chip
->ops
->req_canceled(chip
, status
)) {
243 * wait_for_stat wait for a TPM_STS value
244 * @param: chip, the tpm chip description
245 * @param: mask, the value mask to wait
246 * @param: timeout, the timeout
247 * @param: queue, the wait queue.
248 * @param: check_cancel, does the command can be cancelled ?
249 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
251 static int wait_for_stat(struct tpm_chip
*chip
, u8 mask
, unsigned long timeout
,
252 wait_queue_head_t
*queue
, bool check_cancel
)
254 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
257 bool canceled
= false;
262 /* check current status */
263 status
= st33zp24_status(chip
);
264 if ((status
& mask
) == mask
)
267 stop
= jiffies
+ timeout
;
269 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
270 cur_intrs
= tpm_dev
->intrs
;
271 clear_interruption(tpm_dev
);
272 enable_irq(tpm_dev
->irq
);
275 if (ret
== -ERESTARTSYS
&& freezing(current
))
276 clear_thread_flag(TIF_SIGPENDING
);
278 timeout
= stop
- jiffies
;
279 if ((long) timeout
<= 0)
282 ret
= wait_event_interruptible_timeout(*queue
,
283 cur_intrs
!= tpm_dev
->intrs
,
285 clear_interruption(tpm_dev
);
286 condition
= wait_for_tpm_stat_cond(chip
, mask
,
287 check_cancel
, &canceled
);
288 if (ret
>= 0 && condition
) {
293 } while (ret
== -ERESTARTSYS
&& freezing(current
));
295 disable_irq_nosync(tpm_dev
->irq
);
300 status
= chip
->ops
->status(chip
);
301 if ((status
& mask
) == mask
)
303 } while (time_before(jiffies
, stop
));
307 } /* wait_for_stat() */
310 * recv_data receive data
311 * @param: chip, the tpm chip description
312 * @param: buf, the buffer where the data are received
313 * @param: count, the number of data to receive
314 * @return: the number of bytes read from TPM FIFO.
316 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
318 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
319 int size
= 0, burstcnt
, len
, ret
;
321 while (size
< count
&&
323 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
325 &tpm_dev
->read_queue
, true) == 0) {
326 burstcnt
= get_burstcount(chip
);
329 len
= min_t(int, burstcnt
, count
- size
);
330 ret
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
341 * tpm_ioserirq_handler the serirq irq handler
342 * @param: irq, the tpm chip description
343 * @param: dev_id, the description of the chip
344 * @return: the status of the handler.
346 static irqreturn_t
tpm_ioserirq_handler(int irq
, void *dev_id
)
348 struct tpm_chip
*chip
= dev_id
;
349 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
352 wake_up_interruptible(&tpm_dev
->read_queue
);
353 disable_irq_nosync(tpm_dev
->irq
);
356 } /* tpm_ioserirq_handler() */
359 * st33zp24_send send TPM commands through the I2C bus.
361 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
362 * @param: buf, the buffer to send.
363 * @param: count, the number of bytes to send.
364 * @return: In case of success the number of bytes sent.
365 * In other case, a < 0 value describing the issue.
367 static int st33zp24_send(struct tpm_chip
*chip
, unsigned char *buf
,
370 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
371 u32 status
, i
, size
, ordinal
;
378 if (len
< TPM_HEADER_SIZE
)
381 ret
= request_locality(chip
);
385 status
= st33zp24_status(chip
);
386 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
387 st33zp24_cancel(chip
);
389 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
390 &tpm_dev
->read_queue
, false) < 0) {
396 for (i
= 0; i
< len
- 1;) {
397 burstcnt
= get_burstcount(chip
);
400 size
= min_t(int, len
- i
- 1, burstcnt
);
401 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
409 status
= st33zp24_status(chip
);
410 if ((status
& TPM_STS_DATA_EXPECT
) == 0) {
415 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
420 status
= st33zp24_status(chip
);
421 if ((status
& TPM_STS_DATA_EXPECT
) != 0) {
427 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
431 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
432 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
434 ret
= wait_for_stat(chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
435 tpm_calc_ordinal_duration(chip
, ordinal
),
436 &tpm_dev
->read_queue
, false);
443 st33zp24_cancel(chip
);
444 release_locality(chip
);
449 * st33zp24_recv received TPM response through TPM phy.
450 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
451 * @param: buf, the buffer to store datas.
452 * @param: count, the number of bytes to send.
453 * @return: In case of success the number of bytes received.
454 * In other case, a < 0 value describing the issue.
456 static int st33zp24_recv(struct tpm_chip
*chip
, unsigned char *buf
,
465 if (count
< TPM_HEADER_SIZE
) {
470 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
471 if (size
< TPM_HEADER_SIZE
) {
472 dev_err(&chip
->dev
, "Unable to read header\n");
476 expected
= be32_to_cpu(*(__be32
*)(buf
+ 2));
477 if (expected
> count
) {
482 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
483 expected
- TPM_HEADER_SIZE
);
484 if (size
< expected
) {
485 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
490 st33zp24_cancel(chip
);
491 release_locality(chip
);
496 * st33zp24_req_canceled
497 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
498 * @param: status, the TPM status.
499 * @return: Does TPM ready to compute a new command ? true.
501 static bool st33zp24_req_canceled(struct tpm_chip
*chip
, u8 status
)
503 return (status
== TPM_STS_COMMAND_READY
);
506 static const struct tpm_class_ops st33zp24_tpm
= {
507 .flags
= TPM_OPS_AUTO_STARTUP
,
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
);
593 return tpm_chip_register(chip
);
595 dev_info(&chip
->dev
, "TPM initialization fail\n");
598 EXPORT_SYMBOL(st33zp24_probe
);
601 * st33zp24_remove remove the TPM device
602 * @param: tpm_data, the tpm phy.
603 * @return: 0 in case of success.
605 int st33zp24_remove(struct tpm_chip
*chip
)
607 tpm_chip_unregister(chip
);
610 EXPORT_SYMBOL(st33zp24_remove
);
612 #ifdef CONFIG_PM_SLEEP
614 * st33zp24_pm_suspend suspend the TPM device
615 * @param: tpm_data, the tpm phy.
616 * @param: mesg, the power management message.
617 * @return: 0 in case of success.
619 int st33zp24_pm_suspend(struct device
*dev
)
621 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
622 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
626 if (gpio_is_valid(tpm_dev
->io_lpcpd
))
627 gpio_set_value(tpm_dev
->io_lpcpd
, 0);
629 ret
= tpm_pm_suspend(dev
);
632 } /* st33zp24_pm_suspend() */
633 EXPORT_SYMBOL(st33zp24_pm_suspend
);
636 * st33zp24_pm_resume resume the TPM device
637 * @param: tpm_data, the tpm phy.
638 * @return: 0 in case of success.
640 int st33zp24_pm_resume(struct device
*dev
)
642 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
643 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
646 if (gpio_is_valid(tpm_dev
->io_lpcpd
)) {
647 gpio_set_value(tpm_dev
->io_lpcpd
, 1);
648 ret
= wait_for_stat(chip
,
649 TPM_STS_VALID
, chip
->timeout_b
,
650 &tpm_dev
->read_queue
, false);
652 ret
= tpm_pm_resume(dev
);
654 tpm_do_selftest(chip
);
657 } /* st33zp24_pm_resume() */
658 EXPORT_SYMBOL(st33zp24_pm_resume
);
661 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
662 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
663 MODULE_VERSION("1.3.0");
664 MODULE_LICENSE("GPL");