sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / char / tpm / st33zp24 / st33zp24.c
blob6f060c76217b9a7ea02ab19b8909e7d708578bd5
1 /*
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>
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,
77 * clear_interruption clear the pending interrupt.
78 * @param: tpm_dev, the tpm device device.
79 * @return: the interrupt status value.
81 static u8 clear_interruption(struct st33zp24_dev *tpm_dev)
83 u8 interrupt;
85 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
86 tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
87 return interrupt;
88 } /* clear_interruption() */
91 * st33zp24_cancel, cancel the current command execution or
92 * set STS to COMMAND READY.
93 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
95 static void st33zp24_cancel(struct tpm_chip *chip)
97 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
98 u8 data;
100 data = TPM_STS_COMMAND_READY;
101 tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
102 } /* st33zp24_cancel() */
105 * st33zp24_status return the TPM_STS register
106 * @param: chip, the tpm chip description
107 * @return: the TPM_STS register value.
109 static u8 st33zp24_status(struct tpm_chip *chip)
111 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
112 u8 data;
114 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1);
115 return data;
116 } /* st33zp24_status() */
119 * check_locality if the locality is active
120 * @param: chip, the tpm chip description
121 * @return: the active locality or -EACCESS.
123 static int check_locality(struct tpm_chip *chip)
125 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
126 u8 data;
127 u8 status;
129 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
130 if (status && (data &
131 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
132 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
133 return tpm_dev->locality;
135 return -EACCES;
136 } /* check_locality() */
139 * request_locality request the TPM locality
140 * @param: chip, the chip description
141 * @return: the active locality or negative value.
143 static int request_locality(struct tpm_chip *chip)
145 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
146 unsigned long stop;
147 long ret;
148 u8 data;
150 if (check_locality(chip) == tpm_dev->locality)
151 return tpm_dev->locality;
153 data = TPM_ACCESS_REQUEST_USE;
154 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
155 if (ret < 0)
156 return ret;
158 stop = jiffies + chip->timeout_a;
160 /* Request locality is usually effective after the request */
161 do {
162 if (check_locality(chip) >= 0)
163 return tpm_dev->locality;
164 msleep(TPM_TIMEOUT);
165 } while (time_before(jiffies, stop));
167 /* could not get locality */
168 return -EACCES;
169 } /* request_locality() */
172 * release_locality release the active locality
173 * @param: chip, the tpm chip description.
175 static void release_locality(struct tpm_chip *chip)
177 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
178 u8 data;
180 data = TPM_ACCESS_ACTIVE_LOCALITY;
182 tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
186 * get_burstcount return the burstcount value
187 * @param: chip, the chip description
188 * return: the burstcount or negative value.
190 static int get_burstcount(struct tpm_chip *chip)
192 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
193 unsigned long stop;
194 int burstcnt, status;
195 u8 temp;
197 stop = jiffies + chip->timeout_d;
198 do {
199 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 1,
200 &temp, 1);
201 if (status < 0)
202 return -EBUSY;
204 burstcnt = temp;
205 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 2,
206 &temp, 1);
207 if (status < 0)
208 return -EBUSY;
210 burstcnt |= temp << 8;
211 if (burstcnt)
212 return burstcnt;
213 msleep(TPM_TIMEOUT);
214 } while (time_before(jiffies, stop));
215 return -EBUSY;
216 } /* get_burstcount() */
220 * wait_for_tpm_stat_cond
221 * @param: chip, chip description
222 * @param: mask, expected mask value
223 * @param: check_cancel, does the command expected to be canceled ?
224 * @param: canceled, did we received a cancel request ?
225 * @return: true if status == mask or if the command is canceled.
226 * false in other cases.
228 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
229 bool check_cancel, bool *canceled)
231 u8 status = chip->ops->status(chip);
233 *canceled = false;
234 if ((status & mask) == mask)
235 return true;
236 if (check_cancel && chip->ops->req_canceled(chip, status)) {
237 *canceled = true;
238 return true;
240 return false;
244 * wait_for_stat wait for a TPM_STS value
245 * @param: chip, the tpm chip description
246 * @param: mask, the value mask to wait
247 * @param: timeout, the timeout
248 * @param: queue, the wait queue.
249 * @param: check_cancel, does the command can be cancelled ?
250 * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
252 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
253 wait_queue_head_t *queue, bool check_cancel)
255 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
256 unsigned long stop;
257 int ret = 0;
258 bool canceled = false;
259 bool condition;
260 u32 cur_intrs;
261 u8 status;
263 /* check current status */
264 status = st33zp24_status(chip);
265 if ((status & mask) == mask)
266 return 0;
268 stop = jiffies + timeout;
270 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
271 cur_intrs = tpm_dev->intrs;
272 clear_interruption(tpm_dev);
273 enable_irq(tpm_dev->irq);
275 do {
276 if (ret == -ERESTARTSYS && freezing(current))
277 clear_thread_flag(TIF_SIGPENDING);
279 timeout = stop - jiffies;
280 if ((long) timeout <= 0)
281 return -1;
283 ret = wait_event_interruptible_timeout(*queue,
284 cur_intrs != tpm_dev->intrs,
285 timeout);
286 clear_interruption(tpm_dev);
287 condition = wait_for_tpm_stat_cond(chip, mask,
288 check_cancel, &canceled);
289 if (ret >= 0 && condition) {
290 if (canceled)
291 return -ECANCELED;
292 return 0;
294 } while (ret == -ERESTARTSYS && freezing(current));
296 disable_irq_nosync(tpm_dev->irq);
298 } else {
299 do {
300 msleep(TPM_TIMEOUT);
301 status = chip->ops->status(chip);
302 if ((status & mask) == mask)
303 return 0;
304 } while (time_before(jiffies, stop));
307 return -ETIME;
308 } /* wait_for_stat() */
311 * recv_data receive data
312 * @param: chip, the tpm chip description
313 * @param: buf, the buffer where the data are received
314 * @param: count, the number of data to receive
315 * @return: the number of bytes read from TPM FIFO.
317 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
319 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
320 int size = 0, burstcnt, len, ret;
322 while (size < count &&
323 wait_for_stat(chip,
324 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
325 chip->timeout_c,
326 &tpm_dev->read_queue, true) == 0) {
327 burstcnt = get_burstcount(chip);
328 if (burstcnt < 0)
329 return burstcnt;
330 len = min_t(int, burstcnt, count - size);
331 ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO,
332 buf + size, len);
333 if (ret < 0)
334 return ret;
336 size += len;
338 return size;
342 * tpm_ioserirq_handler the serirq irq handler
343 * @param: irq, the tpm chip description
344 * @param: dev_id, the description of the chip
345 * @return: the status of the handler.
347 static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
349 struct tpm_chip *chip = dev_id;
350 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
352 tpm_dev->intrs++;
353 wake_up_interruptible(&tpm_dev->read_queue);
354 disable_irq_nosync(tpm_dev->irq);
356 return IRQ_HANDLED;
357 } /* tpm_ioserirq_handler() */
360 * st33zp24_send send TPM commands through the I2C bus.
362 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
363 * @param: buf, the buffer to send.
364 * @param: count, the number of bytes to send.
365 * @return: In case of success the number of bytes sent.
366 * In other case, a < 0 value describing the issue.
368 static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf,
369 size_t len)
371 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
372 u32 status, i, size, ordinal;
373 int burstcnt = 0;
374 int ret;
375 u8 data;
377 if (!chip)
378 return -EBUSY;
379 if (len < TPM_HEADER_SIZE)
380 return -EBUSY;
382 ret = request_locality(chip);
383 if (ret < 0)
384 return ret;
386 status = st33zp24_status(chip);
387 if ((status & TPM_STS_COMMAND_READY) == 0) {
388 st33zp24_cancel(chip);
389 if (wait_for_stat
390 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
391 &tpm_dev->read_queue, false) < 0) {
392 ret = -ETIME;
393 goto out_err;
397 for (i = 0; i < len - 1;) {
398 burstcnt = get_burstcount(chip);
399 if (burstcnt < 0)
400 return burstcnt;
401 size = min_t(int, len - i - 1, burstcnt);
402 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
403 buf + i, size);
404 if (ret < 0)
405 goto out_err;
407 i += size;
410 status = st33zp24_status(chip);
411 if ((status & TPM_STS_DATA_EXPECT) == 0) {
412 ret = -EIO;
413 goto out_err;
416 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
417 buf + len - 1, 1);
418 if (ret < 0)
419 goto out_err;
421 status = st33zp24_status(chip);
422 if ((status & TPM_STS_DATA_EXPECT) != 0) {
423 ret = -EIO;
424 goto out_err;
427 data = TPM_STS_GO;
428 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
429 if (ret < 0)
430 goto out_err;
432 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
433 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
435 ret = wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
436 tpm_calc_ordinal_duration(chip, ordinal),
437 &tpm_dev->read_queue, false);
438 if (ret < 0)
439 goto out_err;
442 return len;
443 out_err:
444 st33zp24_cancel(chip);
445 release_locality(chip);
446 return ret;
450 * st33zp24_recv received TPM response through TPM phy.
451 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
452 * @param: buf, the buffer to store datas.
453 * @param: count, the number of bytes to send.
454 * @return: In case of success the number of bytes received.
455 * In other case, a < 0 value describing the issue.
457 static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf,
458 size_t count)
460 int size = 0;
461 int expected;
463 if (!chip)
464 return -EBUSY;
466 if (count < TPM_HEADER_SIZE) {
467 size = -EIO;
468 goto out;
471 size = recv_data(chip, buf, TPM_HEADER_SIZE);
472 if (size < TPM_HEADER_SIZE) {
473 dev_err(&chip->dev, "Unable to read header\n");
474 goto out;
477 expected = be32_to_cpu(*(__be32 *)(buf + 2));
478 if (expected > count) {
479 size = -EIO;
480 goto out;
483 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
484 expected - TPM_HEADER_SIZE);
485 if (size < expected) {
486 dev_err(&chip->dev, "Unable to read remainder of result\n");
487 size = -ETIME;
490 out:
491 st33zp24_cancel(chip);
492 release_locality(chip);
493 return size;
497 * st33zp24_req_canceled
498 * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
499 * @param: status, the TPM status.
500 * @return: Does TPM ready to compute a new command ? true.
502 static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status)
504 return (status == TPM_STS_COMMAND_READY);
507 static const struct tpm_class_ops st33zp24_tpm = {
508 .flags = TPM_OPS_AUTO_STARTUP,
509 .send = st33zp24_send,
510 .recv = st33zp24_recv,
511 .cancel = st33zp24_cancel,
512 .status = st33zp24_status,
513 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
514 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
515 .req_canceled = st33zp24_req_canceled,
519 * st33zp24_probe initialize the TPM device
520 * @param: client, the i2c_client drescription (TPM I2C description).
521 * @param: id, the i2c_device_id struct.
522 * @return: 0 in case of success.
523 * -1 in other case.
525 int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
526 struct device *dev, int irq, int io_lpcpd)
528 int ret;
529 u8 intmask = 0;
530 struct tpm_chip *chip;
531 struct st33zp24_dev *tpm_dev;
533 chip = tpmm_chip_alloc(dev, &st33zp24_tpm);
534 if (IS_ERR(chip))
535 return PTR_ERR(chip);
537 tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev),
538 GFP_KERNEL);
539 if (!tpm_dev)
540 return -ENOMEM;
542 tpm_dev->phy_id = phy_id;
543 tpm_dev->ops = ops;
544 dev_set_drvdata(&chip->dev, tpm_dev);
546 chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
547 chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
548 chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
549 chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
551 tpm_dev->locality = LOCALITY0;
553 if (irq) {
554 /* INTERRUPT Setup */
555 init_waitqueue_head(&tpm_dev->read_queue);
556 tpm_dev->intrs = 0;
558 if (request_locality(chip) != LOCALITY0) {
559 ret = -ENODEV;
560 goto _tpm_clean_answer;
563 clear_interruption(tpm_dev);
564 ret = devm_request_irq(dev, irq, tpm_ioserirq_handler,
565 IRQF_TRIGGER_HIGH, "TPM SERIRQ management",
566 chip);
567 if (ret < 0) {
568 dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n",
569 irq);
570 goto _tpm_clean_answer;
573 intmask |= TPM_INTF_CMD_READY_INT
574 | TPM_INTF_STS_VALID_INT
575 | TPM_INTF_DATA_AVAIL_INT;
577 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE,
578 &intmask, 1);
579 if (ret < 0)
580 goto _tpm_clean_answer;
582 intmask = TPM_GLOBAL_INT_ENABLE;
583 ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3),
584 &intmask, 1);
585 if (ret < 0)
586 goto _tpm_clean_answer;
588 tpm_dev->irq = irq;
589 chip->flags |= TPM_CHIP_FLAG_IRQ;
591 disable_irq_nosync(tpm_dev->irq);
594 return tpm_chip_register(chip);
595 _tpm_clean_answer:
596 dev_info(&chip->dev, "TPM initialization fail\n");
597 return ret;
599 EXPORT_SYMBOL(st33zp24_probe);
602 * st33zp24_remove remove the TPM device
603 * @param: tpm_data, the tpm phy.
604 * @return: 0 in case of success.
606 int st33zp24_remove(struct tpm_chip *chip)
608 tpm_chip_unregister(chip);
609 return 0;
611 EXPORT_SYMBOL(st33zp24_remove);
613 #ifdef CONFIG_PM_SLEEP
615 * st33zp24_pm_suspend suspend the TPM device
616 * @param: tpm_data, the tpm phy.
617 * @param: mesg, the power management message.
618 * @return: 0 in case of success.
620 int st33zp24_pm_suspend(struct device *dev)
622 struct tpm_chip *chip = dev_get_drvdata(dev);
623 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
625 int ret = 0;
627 if (gpio_is_valid(tpm_dev->io_lpcpd))
628 gpio_set_value(tpm_dev->io_lpcpd, 0);
629 else
630 ret = tpm_pm_suspend(dev);
632 return ret;
633 } /* st33zp24_pm_suspend() */
634 EXPORT_SYMBOL(st33zp24_pm_suspend);
637 * st33zp24_pm_resume resume the TPM device
638 * @param: tpm_data, the tpm phy.
639 * @return: 0 in case of success.
641 int st33zp24_pm_resume(struct device *dev)
643 struct tpm_chip *chip = dev_get_drvdata(dev);
644 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
645 int ret = 0;
647 if (gpio_is_valid(tpm_dev->io_lpcpd)) {
648 gpio_set_value(tpm_dev->io_lpcpd, 1);
649 ret = wait_for_stat(chip,
650 TPM_STS_VALID, chip->timeout_b,
651 &tpm_dev->read_queue, false);
652 } else {
653 ret = tpm_pm_resume(dev);
654 if (!ret)
655 tpm_do_selftest(chip);
657 return ret;
658 } /* st33zp24_pm_resume() */
659 EXPORT_SYMBOL(st33zp24_pm_resume);
660 #endif
662 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
663 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
664 MODULE_VERSION("1.3.0");
665 MODULE_LICENSE("GPL");