Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / char / tpm / st33zp24 / st33zp24.c
blob8d626784cd8dd8bec4f61ceb568fe06cac2bb995
1 /*
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>
20 #include <linux/fs.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>
31 #include <linux/io.h>
32 #include <linux/slab.h>
34 #include "../tpm.h"
35 #include "st33zp24.h"
37 #define TPM_ACCESS 0x0
38 #define TPM_STS 0x18
39 #define TPM_DATA_FIFO 0x24
40 #define TPM_INTF_CAPABILITY 0x14
41 #define TPM_INT_STATUS 0x10
42 #define TPM_INT_ENABLE 0x08
44 #define LOCALITY0 0
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 {
54 TPM_STS_VALID = 0x80,
55 TPM_STS_COMMAND_READY = 0x40,
56 TPM_STS_GO = 0x20,
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,
71 enum tis_defaults {
72 TIS_SHORT_TIMEOUT = 750,
73 TIS_LONG_TIMEOUT = 2000,
76 struct st33zp24_dev {
77 struct tpm_chip *chip;
78 void *phy_id;
79 const struct st33zp24_phy_ops *ops;
80 u32 intrs;
81 int io_lpcpd;
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)
91 u8 interrupt;
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);
95 return interrupt;
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;
106 u8 data;
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;
122 u8 data;
124 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
126 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1);
127 return data;
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;
138 u8 data;
139 u8 status;
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;
149 return -EACCES;
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)
159 unsigned long stop;
160 long ret;
161 struct st33zp24_dev *tpm_dev;
162 u8 data;
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);
171 if (ret < 0)
172 return ret;
174 stop = jiffies + chip->vendor.timeout_a;
176 /* Request locality is usually effective after the request */
177 do {
178 if (check_locality(chip) >= 0)
179 return chip->vendor.locality;
180 msleep(TPM_TIMEOUT);
181 } while (time_before(jiffies, stop));
183 /* could not get locality */
184 return -EACCES;
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;
194 u8 data;
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)
209 unsigned long stop;
210 int burstcnt, status;
211 u8 tpm_reg, temp;
212 struct st33zp24_dev *tpm_dev;
214 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
216 stop = jiffies + chip->vendor.timeout_d;
217 do {
218 tpm_reg = TPM_STS + 1;
219 status = tpm_dev->ops->recv(tpm_dev->phy_id, tpm_reg, &temp, 1);
220 if (status < 0)
221 return -EBUSY;
223 tpm_reg = TPM_STS + 2;
224 burstcnt = temp;
225 status = tpm_dev->ops->recv(tpm_dev->phy_id, tpm_reg, &temp, 1);
226 if (status < 0)
227 return -EBUSY;
229 burstcnt |= temp << 8;
230 if (burstcnt)
231 return burstcnt;
232 msleep(TPM_TIMEOUT);
233 } while (time_before(jiffies, stop));
234 return -EBUSY;
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);
252 *canceled = false;
253 if ((status & mask) == mask)
254 return true;
255 if (check_cancel && chip->ops->req_canceled(chip, status)) {
256 *canceled = true;
257 return true;
259 return false;
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)
274 unsigned long stop;
275 int ret = 0;
276 bool canceled = false;
277 bool condition;
278 u32 cur_intrs;
279 u8 status;
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)
287 return 0;
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);
296 do {
297 if (ret == -ERESTARTSYS && freezing(current))
298 clear_thread_flag(TIF_SIGPENDING);
300 timeout = stop - jiffies;
301 if ((long) timeout <= 0)
302 return -1;
304 ret = wait_event_interruptible_timeout(*queue,
305 cur_intrs != tpm_dev->intrs,
306 timeout);
307 clear_interruption(tpm_dev);
308 condition = wait_for_tpm_stat_cond(chip, mask,
309 check_cancel, &canceled);
310 if (ret >= 0 && condition) {
311 if (canceled)
312 return -ECANCELED;
313 return 0;
315 } while (ret == -ERESTARTSYS && freezing(current));
317 disable_irq_nosync(chip->vendor.irq);
319 } else {
320 do {
321 msleep(TPM_TIMEOUT);
322 status = chip->ops->status(chip);
323 if ((status & mask) == mask)
324 return 0;
325 } while (time_before(jiffies, stop));
328 return -ETIME;
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 &&
346 wait_for_stat(chip,
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);
351 if (burstcnt < 0)
352 return burstcnt;
353 len = min_t(int, burstcnt, count - size);
354 ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO,
355 buf + size, len);
356 if (ret < 0)
357 return ret;
359 size += len;
361 return size;
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);
377 tpm_dev->intrs++;
378 wake_up_interruptible(&chip->vendor.read_queue);
379 disable_irq_nosync(chip->vendor.irq);
381 return IRQ_HANDLED;
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,
394 size_t len)
396 u32 status, i, size, ordinal;
397 int burstcnt = 0;
398 int ret;
399 u8 data;
400 struct st33zp24_dev *tpm_dev;
402 if (!chip)
403 return -EBUSY;
404 if (len < TPM_HEADER_SIZE)
405 return -EBUSY;
407 tpm_dev = (struct st33zp24_dev *)TPM_VPRIV(chip);
409 ret = request_locality(chip);
410 if (ret < 0)
411 return ret;
413 status = st33zp24_status(chip);
414 if ((status & TPM_STS_COMMAND_READY) == 0) {
415 st33zp24_cancel(chip);
416 if (wait_for_stat
417 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
418 &chip->vendor.read_queue, false) < 0) {
419 ret = -ETIME;
420 goto out_err;
424 for (i = 0; i < len - 1;) {
425 burstcnt = get_burstcount(chip);
426 if (burstcnt < 0)
427 return burstcnt;
428 size = min_t(int, len - i - 1, burstcnt);
429 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
430 buf + i, size);
431 if (ret < 0)
432 goto out_err;
434 i += size;
437 status = st33zp24_status(chip);
438 if ((status & TPM_STS_DATA_EXPECT) == 0) {
439 ret = -EIO;
440 goto out_err;
443 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
444 buf + len - 1, 1);
445 if (ret < 0)
446 goto out_err;
448 status = st33zp24_status(chip);
449 if ((status & TPM_STS_DATA_EXPECT) != 0) {
450 ret = -EIO;
451 goto out_err;
454 data = TPM_STS_GO;
455 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
456 if (ret < 0)
457 goto out_err;
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);
465 if (ret < 0)
466 goto out_err;
469 return len;
470 out_err:
471 st33zp24_cancel(chip);
472 release_locality(chip);
473 return ret;
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,
485 size_t count)
487 int size = 0;
488 int expected;
490 if (!chip)
491 return -EBUSY;
493 if (count < TPM_HEADER_SIZE) {
494 size = -EIO;
495 goto out;
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");
501 goto out;
504 expected = be32_to_cpu(*(__be32 *)(buf + 2));
505 if (expected > count) {
506 size = -EIO;
507 goto out;
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");
514 size = -ETIME;
517 out:
518 st33zp24_cancel(chip);
519 release_locality(chip);
520 return size;
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.
549 * -1 in other case.
551 int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
552 struct device *dev, int irq, int io_lpcpd)
554 int ret;
555 u8 intmask = 0;
556 struct tpm_chip *chip;
557 struct st33zp24_dev *tpm_dev;
559 chip = tpmm_chip_alloc(dev, &st33zp24_tpm);
560 if (IS_ERR(chip))
561 return PTR_ERR(chip);
563 tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev),
564 GFP_KERNEL);
565 if (!tpm_dev)
566 return -ENOMEM;
568 TPM_VPRIV(chip) = tpm_dev;
569 tpm_dev->phy_id = phy_id;
570 tpm_dev->ops = ops;
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;
579 if (irq) {
580 /* INTERRUPT Setup */
581 init_waitqueue_head(&chip->vendor.read_queue);
582 tpm_dev->intrs = 0;
584 if (request_locality(chip) != LOCALITY0) {
585 ret = -ENODEV;
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",
592 chip);
593 if (ret < 0) {
594 dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n",
595 irq);
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,
604 &intmask, 1);
605 if (ret < 0)
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),
610 &intmask, 1);
611 if (ret < 0)
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);
625 _tpm_clean_answer:
626 dev_info(&chip->dev, "TPM initialization fail\n");
627 return ret;
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);
639 return 0;
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;
654 int ret = 0;
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);
660 else
661 ret = tpm_pm_suspend(dev);
663 return ret;
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;
676 int ret = 0;
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);
685 } else {
686 ret = tpm_pm_resume(dev);
687 if (!ret)
688 tpm_do_selftest(chip);
690 return ret;
691 } /* st33zp24_pm_resume() */
692 EXPORT_SYMBOL(st33zp24_pm_resume);
693 #endif
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");