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
;
376 if (len
< TPM_HEADER_SIZE
)
379 ret
= request_locality(chip
);
383 status
= st33zp24_status(chip
);
384 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
385 st33zp24_cancel(chip
);
387 (chip
, TPM_STS_COMMAND_READY
, chip
->timeout_b
,
388 &tpm_dev
->read_queue
, false) < 0) {
394 for (i
= 0; i
< len
- 1;) {
395 burstcnt
= get_burstcount(chip
);
398 size
= min_t(int, len
- i
- 1, burstcnt
);
399 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
407 status
= st33zp24_status(chip
);
408 if ((status
& TPM_STS_DATA_EXPECT
) == 0) {
413 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
418 status
= st33zp24_status(chip
);
419 if ((status
& TPM_STS_DATA_EXPECT
) != 0) {
425 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
429 if (chip
->flags
& TPM_CHIP_FLAG_IRQ
) {
430 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
432 ret
= wait_for_stat(chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
433 tpm_calc_ordinal_duration(chip
, ordinal
),
434 &tpm_dev
->read_queue
, false);
441 st33zp24_cancel(chip
);
442 release_locality(chip
);
447 * st33zp24_recv received TPM response through TPM phy.
448 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
449 * @param: buf, the buffer to store datas.
450 * @param: count, the number of bytes to send.
451 * @return: In case of success the number of bytes received.
452 * In other case, a < 0 value describing the issue.
454 static int st33zp24_recv(struct tpm_chip
*chip
, unsigned char *buf
,
463 if (count
< TPM_HEADER_SIZE
) {
468 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
469 if (size
< TPM_HEADER_SIZE
) {
470 dev_err(&chip
->dev
, "Unable to read header\n");
474 expected
= be32_to_cpu(*(__be32
*)(buf
+ 2));
475 if (expected
> count
|| expected
< TPM_HEADER_SIZE
) {
480 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
481 expected
- TPM_HEADER_SIZE
);
482 if (size
< expected
) {
483 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
488 st33zp24_cancel(chip
);
489 release_locality(chip
);
494 * st33zp24_req_canceled
495 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
496 * @param: status, the TPM status.
497 * @return: Does TPM ready to compute a new command ? true.
499 static bool st33zp24_req_canceled(struct tpm_chip
*chip
, u8 status
)
501 return (status
== TPM_STS_COMMAND_READY
);
504 static const struct tpm_class_ops st33zp24_tpm
= {
505 .flags
= TPM_OPS_AUTO_STARTUP
,
506 .send
= st33zp24_send
,
507 .recv
= st33zp24_recv
,
508 .cancel
= st33zp24_cancel
,
509 .status
= st33zp24_status
,
510 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
511 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
512 .req_canceled
= st33zp24_req_canceled
,
516 * st33zp24_probe initialize the TPM device
517 * @param: client, the i2c_client drescription (TPM I2C description).
518 * @param: id, the i2c_device_id struct.
519 * @return: 0 in case of success.
522 int st33zp24_probe(void *phy_id
, const struct st33zp24_phy_ops
*ops
,
523 struct device
*dev
, int irq
, int io_lpcpd
)
527 struct tpm_chip
*chip
;
528 struct st33zp24_dev
*tpm_dev
;
530 chip
= tpmm_chip_alloc(dev
, &st33zp24_tpm
);
532 return PTR_ERR(chip
);
534 tpm_dev
= devm_kzalloc(dev
, sizeof(struct st33zp24_dev
),
539 tpm_dev
->phy_id
= phy_id
;
541 dev_set_drvdata(&chip
->dev
, tpm_dev
);
543 chip
->timeout_a
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
544 chip
->timeout_b
= msecs_to_jiffies(TIS_LONG_TIMEOUT
);
545 chip
->timeout_c
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
546 chip
->timeout_d
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
548 tpm_dev
->locality
= LOCALITY0
;
551 /* INTERRUPT Setup */
552 init_waitqueue_head(&tpm_dev
->read_queue
);
555 if (request_locality(chip
) != LOCALITY0
) {
557 goto _tpm_clean_answer
;
560 clear_interruption(tpm_dev
);
561 ret
= devm_request_irq(dev
, irq
, tpm_ioserirq_handler
,
562 IRQF_TRIGGER_HIGH
, "TPM SERIRQ management",
565 dev_err(&chip
->dev
, "TPM SERIRQ signals %d not available\n",
567 goto _tpm_clean_answer
;
570 intmask
|= TPM_INTF_CMD_READY_INT
571 | TPM_INTF_STS_VALID_INT
572 | TPM_INTF_DATA_AVAIL_INT
;
574 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_INT_ENABLE
,
577 goto _tpm_clean_answer
;
579 intmask
= TPM_GLOBAL_INT_ENABLE
;
580 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, (TPM_INT_ENABLE
+ 3),
583 goto _tpm_clean_answer
;
586 chip
->flags
|= TPM_CHIP_FLAG_IRQ
;
588 disable_irq_nosync(tpm_dev
->irq
);
591 return tpm_chip_register(chip
);
593 dev_info(&chip
->dev
, "TPM initialization fail\n");
596 EXPORT_SYMBOL(st33zp24_probe
);
599 * st33zp24_remove remove the TPM device
600 * @param: tpm_data, the tpm phy.
601 * @return: 0 in case of success.
603 int st33zp24_remove(struct tpm_chip
*chip
)
605 tpm_chip_unregister(chip
);
608 EXPORT_SYMBOL(st33zp24_remove
);
610 #ifdef CONFIG_PM_SLEEP
612 * st33zp24_pm_suspend suspend the TPM device
613 * @param: tpm_data, the tpm phy.
614 * @param: mesg, the power management message.
615 * @return: 0 in case of success.
617 int st33zp24_pm_suspend(struct device
*dev
)
619 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
620 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
624 if (gpio_is_valid(tpm_dev
->io_lpcpd
))
625 gpio_set_value(tpm_dev
->io_lpcpd
, 0);
627 ret
= tpm_pm_suspend(dev
);
630 } /* st33zp24_pm_suspend() */
631 EXPORT_SYMBOL(st33zp24_pm_suspend
);
634 * st33zp24_pm_resume resume the TPM device
635 * @param: tpm_data, the tpm phy.
636 * @return: 0 in case of success.
638 int st33zp24_pm_resume(struct device
*dev
)
640 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
641 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
644 if (gpio_is_valid(tpm_dev
->io_lpcpd
)) {
645 gpio_set_value(tpm_dev
->io_lpcpd
, 1);
646 ret
= wait_for_stat(chip
,
647 TPM_STS_VALID
, chip
->timeout_b
,
648 &tpm_dev
->read_queue
, false);
650 ret
= tpm_pm_resume(dev
);
652 tpm_do_selftest(chip
);
655 } /* st33zp24_pm_resume() */
656 EXPORT_SYMBOL(st33zp24_pm_resume
);
659 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
660 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
661 MODULE_VERSION("1.3.0");
662 MODULE_LICENSE("GPL");