2 * STMicroelectronics TPM Linux driver for TPM ST33ZP24
3 * Copyright (C) 2009 - 2015 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 struct tpm_chip
*chip
;
79 const struct st33zp24_phy_ops
*ops
;
85 * clear_interruption clear the pending interrupt.
86 * @param: tpm_dev, the tpm device device.
87 * @return: the interrupt status value.
89 static u8
clear_interruption(struct st33zp24_dev
*tpm_dev
)
93 tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_INT_STATUS
, &interrupt
, 1);
94 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_INT_STATUS
, &interrupt
, 1);
96 } /* clear_interruption() */
99 * st33zp24_cancel, cancel the current command execution or
100 * set STS to COMMAND READY.
101 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
103 static void st33zp24_cancel(struct tpm_chip
*chip
)
105 struct st33zp24_dev
*tpm_dev
;
108 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
110 data
= TPM_STS_COMMAND_READY
;
111 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
112 } /* st33zp24_cancel() */
115 * st33zp24_status return the TPM_STS register
116 * @param: chip, the tpm chip description
117 * @return: the TPM_STS register value.
119 static u8
st33zp24_status(struct tpm_chip
*chip
)
121 struct st33zp24_dev
*tpm_dev
;
124 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
126 tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
128 } /* st33zp24_status() */
131 * check_locality if the locality is active
132 * @param: chip, the tpm chip description
133 * @return: the active locality or -EACCESS.
135 static int check_locality(struct tpm_chip
*chip
)
137 struct st33zp24_dev
*tpm_dev
;
141 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
143 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
144 if (status
&& (data
&
145 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
)) ==
146 (TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
))
147 return chip
->vendor
.locality
;
150 } /* check_locality() */
153 * request_locality request the TPM locality
154 * @param: chip, the chip description
155 * @return: the active locality or negative value.
157 static int request_locality(struct tpm_chip
*chip
)
161 struct st33zp24_dev
*tpm_dev
;
164 if (check_locality(chip
) == chip
->vendor
.locality
)
165 return chip
->vendor
.locality
;
167 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
169 data
= TPM_ACCESS_REQUEST_USE
;
170 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
174 stop
= jiffies
+ chip
->vendor
.timeout_a
;
176 /* Request locality is usually effective after the request */
178 if (check_locality(chip
) >= 0)
179 return chip
->vendor
.locality
;
181 } while (time_before(jiffies
, stop
));
183 /* could not get locality */
185 } /* request_locality() */
188 * release_locality release the active locality
189 * @param: chip, the tpm chip description.
191 static void release_locality(struct tpm_chip
*chip
)
193 struct st33zp24_dev
*tpm_dev
;
196 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
197 data
= TPM_ACCESS_ACTIVE_LOCALITY
;
199 tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_ACCESS
, &data
, 1);
203 * get_burstcount return the burstcount value
204 * @param: chip, the chip description
205 * return: the burstcount or negative value.
207 static int get_burstcount(struct tpm_chip
*chip
)
210 int burstcnt
, status
;
212 struct st33zp24_dev
*tpm_dev
;
214 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
216 stop
= jiffies
+ chip
->vendor
.timeout_d
;
218 tpm_reg
= TPM_STS
+ 1;
219 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, tpm_reg
, &temp
, 1);
223 tpm_reg
= TPM_STS
+ 2;
225 status
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, tpm_reg
, &temp
, 1);
229 burstcnt
|= temp
<< 8;
233 } while (time_before(jiffies
, stop
));
235 } /* get_burstcount() */
239 * wait_for_tpm_stat_cond
240 * @param: chip, chip description
241 * @param: mask, expected mask value
242 * @param: check_cancel, does the command expected to be canceled ?
243 * @param: canceled, did we received a cancel request ?
244 * @return: true if status == mask or if the command is canceled.
245 * false in other cases.
247 static bool wait_for_tpm_stat_cond(struct tpm_chip
*chip
, u8 mask
,
248 bool check_cancel
, bool *canceled
)
250 u8 status
= chip
->ops
->status(chip
);
253 if ((status
& mask
) == mask
)
255 if (check_cancel
&& chip
->ops
->req_canceled(chip
, status
)) {
263 * wait_for_stat wait for a TPM_STS value
264 * @param: chip, the tpm chip description
265 * @param: mask, the value mask to wait
266 * @param: timeout, the timeout
267 * @param: queue, the wait queue.
268 * @param: check_cancel, does the command can be cancelled ?
269 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
271 static int wait_for_stat(struct tpm_chip
*chip
, u8 mask
, unsigned long timeout
,
272 wait_queue_head_t
*queue
, bool check_cancel
)
276 bool canceled
= false;
280 struct st33zp24_dev
*tpm_dev
;
282 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
284 /* check current status */
285 status
= st33zp24_status(chip
);
286 if ((status
& mask
) == mask
)
289 stop
= jiffies
+ timeout
;
291 if (chip
->vendor
.irq
) {
292 cur_intrs
= tpm_dev
->intrs
;
293 clear_interruption(tpm_dev
);
294 enable_irq(chip
->vendor
.irq
);
297 if (ret
== -ERESTARTSYS
&& freezing(current
))
298 clear_thread_flag(TIF_SIGPENDING
);
300 timeout
= stop
- jiffies
;
301 if ((long) timeout
<= 0)
304 ret
= wait_event_interruptible_timeout(*queue
,
305 cur_intrs
!= tpm_dev
->intrs
,
307 clear_interruption(tpm_dev
);
308 condition
= wait_for_tpm_stat_cond(chip
, mask
,
309 check_cancel
, &canceled
);
310 if (ret
>= 0 && condition
) {
315 } while (ret
== -ERESTARTSYS
&& freezing(current
));
317 disable_irq_nosync(chip
->vendor
.irq
);
322 status
= chip
->ops
->status(chip
);
323 if ((status
& mask
) == mask
)
325 } while (time_before(jiffies
, stop
));
329 } /* wait_for_stat() */
332 * recv_data receive data
333 * @param: chip, the tpm chip description
334 * @param: buf, the buffer where the data are received
335 * @param: count, the number of data to receive
336 * @return: the number of bytes read from TPM FIFO.
338 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
340 int size
= 0, burstcnt
, len
, ret
;
341 struct st33zp24_dev
*tpm_dev
;
343 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
345 while (size
< count
&&
347 TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
348 chip
->vendor
.timeout_c
,
349 &chip
->vendor
.read_queue
, true) == 0) {
350 burstcnt
= get_burstcount(chip
);
353 len
= min_t(int, burstcnt
, count
- size
);
354 ret
= tpm_dev
->ops
->recv(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
365 * tpm_ioserirq_handler the serirq irq handler
366 * @param: irq, the tpm chip description
367 * @param: dev_id, the description of the chip
368 * @return: the status of the handler.
370 static irqreturn_t
tpm_ioserirq_handler(int irq
, void *dev_id
)
372 struct tpm_chip
*chip
= dev_id
;
373 struct st33zp24_dev
*tpm_dev
;
375 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
378 wake_up_interruptible(&chip
->vendor
.read_queue
);
379 disable_irq_nosync(chip
->vendor
.irq
);
382 } /* tpm_ioserirq_handler() */
385 * st33zp24_send send TPM commands through the I2C bus.
387 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
388 * @param: buf, the buffer to send.
389 * @param: count, the number of bytes to send.
390 * @return: In case of success the number of bytes sent.
391 * In other case, a < 0 value describing the issue.
393 static int st33zp24_send(struct tpm_chip
*chip
, unsigned char *buf
,
396 u32 status
, i
, size
, ordinal
;
400 struct st33zp24_dev
*tpm_dev
;
404 if (len
< TPM_HEADER_SIZE
)
407 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
409 ret
= request_locality(chip
);
413 status
= st33zp24_status(chip
);
414 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
415 st33zp24_cancel(chip
);
417 (chip
, TPM_STS_COMMAND_READY
, chip
->vendor
.timeout_b
,
418 &chip
->vendor
.read_queue
, false) < 0) {
424 for (i
= 0; i
< len
- 1;) {
425 burstcnt
= get_burstcount(chip
);
428 size
= min_t(int, len
- i
- 1, burstcnt
);
429 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
437 status
= st33zp24_status(chip
);
438 if ((status
& TPM_STS_DATA_EXPECT
) == 0) {
443 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_DATA_FIFO
,
448 status
= st33zp24_status(chip
);
449 if ((status
& TPM_STS_DATA_EXPECT
) != 0) {
455 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_STS
, &data
, 1);
459 if (chip
->vendor
.irq
) {
460 ordinal
= be32_to_cpu(*((__be32
*) (buf
+ 6)));
462 ret
= wait_for_stat(chip
, TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
463 tpm_calc_ordinal_duration(chip
, ordinal
),
464 &chip
->vendor
.read_queue
, false);
471 st33zp24_cancel(chip
);
472 release_locality(chip
);
477 * st33zp24_recv received TPM response through TPM phy.
478 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
479 * @param: buf, the buffer to store datas.
480 * @param: count, the number of bytes to send.
481 * @return: In case of success the number of bytes received.
482 * In other case, a < 0 value describing the issue.
484 static int st33zp24_recv(struct tpm_chip
*chip
, unsigned char *buf
,
493 if (count
< TPM_HEADER_SIZE
) {
498 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
499 if (size
< TPM_HEADER_SIZE
) {
500 dev_err(&chip
->dev
, "Unable to read header\n");
504 expected
= be32_to_cpu(*(__be32
*)(buf
+ 2));
505 if (expected
> count
) {
510 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
511 expected
- TPM_HEADER_SIZE
);
512 if (size
< expected
) {
513 dev_err(&chip
->dev
, "Unable to read remainder of result\n");
518 st33zp24_cancel(chip
);
519 release_locality(chip
);
524 * st33zp24_req_canceled
525 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
526 * @param: status, the TPM status.
527 * @return: Does TPM ready to compute a new command ? true.
529 static bool st33zp24_req_canceled(struct tpm_chip
*chip
, u8 status
)
531 return (status
== TPM_STS_COMMAND_READY
);
534 static const struct tpm_class_ops st33zp24_tpm
= {
535 .send
= st33zp24_send
,
536 .recv
= st33zp24_recv
,
537 .cancel
= st33zp24_cancel
,
538 .status
= st33zp24_status
,
539 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
540 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
541 .req_canceled
= st33zp24_req_canceled
,
545 * st33zp24_probe initialize the TPM device
546 * @param: client, the i2c_client drescription (TPM I2C description).
547 * @param: id, the i2c_device_id struct.
548 * @return: 0 in case of success.
551 int st33zp24_probe(void *phy_id
, const struct st33zp24_phy_ops
*ops
,
552 struct device
*dev
, int irq
, int io_lpcpd
)
556 struct tpm_chip
*chip
;
557 struct st33zp24_dev
*tpm_dev
;
559 chip
= tpmm_chip_alloc(dev
, &st33zp24_tpm
);
561 return PTR_ERR(chip
);
563 tpm_dev
= devm_kzalloc(dev
, sizeof(struct st33zp24_dev
),
568 TPM_VPRIV(chip
) = tpm_dev
;
569 tpm_dev
->phy_id
= phy_id
;
572 chip
->vendor
.timeout_a
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
573 chip
->vendor
.timeout_b
= msecs_to_jiffies(TIS_LONG_TIMEOUT
);
574 chip
->vendor
.timeout_c
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
575 chip
->vendor
.timeout_d
= msecs_to_jiffies(TIS_SHORT_TIMEOUT
);
577 chip
->vendor
.locality
= LOCALITY0
;
580 /* INTERRUPT Setup */
581 init_waitqueue_head(&chip
->vendor
.read_queue
);
584 if (request_locality(chip
) != LOCALITY0
) {
586 goto _tpm_clean_answer
;
589 clear_interruption(tpm_dev
);
590 ret
= devm_request_irq(dev
, irq
, tpm_ioserirq_handler
,
591 IRQF_TRIGGER_HIGH
, "TPM SERIRQ management",
594 dev_err(&chip
->dev
, "TPM SERIRQ signals %d not available\n",
596 goto _tpm_clean_answer
;
599 intmask
|= TPM_INTF_CMD_READY_INT
600 | TPM_INTF_STS_VALID_INT
601 | TPM_INTF_DATA_AVAIL_INT
;
603 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, TPM_INT_ENABLE
,
606 goto _tpm_clean_answer
;
608 intmask
= TPM_GLOBAL_INT_ENABLE
;
609 ret
= tpm_dev
->ops
->send(tpm_dev
->phy_id
, (TPM_INT_ENABLE
+ 3),
612 goto _tpm_clean_answer
;
614 chip
->vendor
.irq
= irq
;
616 disable_irq_nosync(chip
->vendor
.irq
);
618 tpm_gen_interrupt(chip
);
621 tpm_get_timeouts(chip
);
622 tpm_do_selftest(chip
);
624 return tpm_chip_register(chip
);
626 dev_info(&chip
->dev
, "TPM initialization fail\n");
629 EXPORT_SYMBOL(st33zp24_probe
);
632 * st33zp24_remove remove the TPM device
633 * @param: tpm_data, the tpm phy.
634 * @return: 0 in case of success.
636 int st33zp24_remove(struct tpm_chip
*chip
)
638 tpm_chip_unregister(chip
);
641 EXPORT_SYMBOL(st33zp24_remove
);
643 #ifdef CONFIG_PM_SLEEP
645 * st33zp24_pm_suspend suspend the TPM device
646 * @param: tpm_data, the tpm phy.
647 * @param: mesg, the power management message.
648 * @return: 0 in case of success.
650 int st33zp24_pm_suspend(struct device
*dev
)
652 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
653 struct st33zp24_dev
*tpm_dev
;
656 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
658 if (gpio_is_valid(tpm_dev
->io_lpcpd
))
659 gpio_set_value(tpm_dev
->io_lpcpd
, 0);
661 ret
= tpm_pm_suspend(dev
);
664 } /* st33zp24_pm_suspend() */
665 EXPORT_SYMBOL(st33zp24_pm_suspend
);
668 * st33zp24_pm_resume resume the TPM device
669 * @param: tpm_data, the tpm phy.
670 * @return: 0 in case of success.
672 int st33zp24_pm_resume(struct device
*dev
)
674 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
675 struct st33zp24_dev
*tpm_dev
;
678 tpm_dev
= (struct st33zp24_dev
*)TPM_VPRIV(chip
);
680 if (gpio_is_valid(tpm_dev
->io_lpcpd
)) {
681 gpio_set_value(tpm_dev
->io_lpcpd
, 1);
682 ret
= wait_for_stat(chip
,
683 TPM_STS_VALID
, chip
->vendor
.timeout_b
,
684 &chip
->vendor
.read_queue
, false);
686 ret
= tpm_pm_resume(dev
);
688 tpm_do_selftest(chip
);
691 } /* st33zp24_pm_resume() */
692 EXPORT_SYMBOL(st33zp24_pm_resume
);
695 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
696 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
697 MODULE_VERSION("1.3.0");
698 MODULE_LICENSE("GPL");