a7c99a284724f3cf02dc3e2027fcdcbfe4d0c48e
[cascardo/linux.git] / drivers / char / tpm / st33zp24 / st33zp24.c
1 /*
2  * STMicroelectronics TPM Linux driver for TPM ST33ZP24
3  * Copyright (C) 2009 - 2016 STMicroelectronics
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
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>
33
34 #include "../tpm.h"
35 #include "st33zp24.h"
36
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
43
44 #define LOCALITY0                       0
45
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,
51 };
52
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,
59 };
60
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,
69 };
70
71 enum tis_defaults {
72         TIS_SHORT_TIMEOUT = 750,
73         TIS_LONG_TIMEOUT = 2000,
74 };
75
76 /*
77  * clear_interruption clear the pending interrupt.
78  * @param: tpm_dev, the tpm device device.
79  * @return: the interrupt status value.
80  */
81 static u8 clear_interruption(struct st33zp24_dev *tpm_dev)
82 {
83         u8 interrupt;
84
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() */
89
90 /*
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
94  */
95 static void st33zp24_cancel(struct tpm_chip *chip)
96 {
97         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
98         u8 data;
99
100         data = TPM_STS_COMMAND_READY;
101         tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
102 } /* st33zp24_cancel() */
103
104 /*
105  * st33zp24_status return the TPM_STS register
106  * @param: chip, the tpm chip description
107  * @return: the TPM_STS register value.
108  */
109 static u8 st33zp24_status(struct tpm_chip *chip)
110 {
111         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
112         u8 data;
113
114         tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1);
115         return data;
116 } /* st33zp24_status() */
117
118 /*
119  * check_locality if the locality is active
120  * @param: chip, the tpm chip description
121  * @return: the active locality or -EACCESS.
122  */
123 static int check_locality(struct tpm_chip *chip)
124 {
125         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
126         u8 data;
127         u8 status;
128
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;
134
135         return -EACCES;
136 } /* check_locality() */
137
138 /*
139  * request_locality request the TPM locality
140  * @param: chip, the chip description
141  * @return: the active locality or negative value.
142  */
143 static int request_locality(struct tpm_chip *chip)
144 {
145         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
146         unsigned long stop;
147         long ret;
148         u8 data;
149
150         if (check_locality(chip) == tpm_dev->locality)
151                 return tpm_dev->locality;
152
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;
157
158         stop = jiffies + chip->timeout_a;
159
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));
166
167         /* could not get locality */
168         return -EACCES;
169 } /* request_locality() */
170
171 /*
172  * release_locality release the active locality
173  * @param: chip, the tpm chip description.
174  */
175 static void release_locality(struct tpm_chip *chip)
176 {
177         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
178         u8 data;
179
180         data = TPM_ACCESS_ACTIVE_LOCALITY;
181
182         tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
183 }
184
185 /*
186  * get_burstcount return the burstcount value
187  * @param: chip, the chip description
188  * return: the burstcount or negative value.
189  */
190 static int get_burstcount(struct tpm_chip *chip)
191 {
192         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
193         unsigned long stop;
194         int burstcnt, status;
195         u8 temp;
196
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;
203
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;
209
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() */
217
218
219 /*
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.
227  */
228 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
229                                 bool check_cancel, bool *canceled)
230 {
231         u8 status = chip->ops->status(chip);
232
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;
239         }
240         return false;
241 }
242
243 /*
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.
251  */
252 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
253                         wait_queue_head_t *queue, bool check_cancel)
254 {
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;
262
263         /* check current status */
264         status = st33zp24_status(chip);
265         if ((status & mask) == mask)
266                 return 0;
267
268         stop = jiffies + timeout;
269
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);
274
275                 do {
276                         if (ret == -ERESTARTSYS && freezing(current))
277                                 clear_thread_flag(TIF_SIGPENDING);
278
279                         timeout = stop - jiffies;
280                         if ((long) timeout <= 0)
281                                 return -1;
282
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;
293                         }
294                 } while (ret == -ERESTARTSYS && freezing(current));
295
296                 disable_irq_nosync(tpm_dev->irq);
297
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));
305         }
306
307         return -ETIME;
308 } /* wait_for_stat() */
309
310 /*
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.
316  */
317 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
318 {
319         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
320         int size = 0, burstcnt, len, ret;
321
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;
335
336                 size += len;
337         }
338         return size;
339 }
340
341 /*
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.
346  */
347 static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
348 {
349         struct tpm_chip *chip = dev_id;
350         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
351
352         tpm_dev->intrs++;
353         wake_up_interruptible(&tpm_dev->read_queue);
354         disable_irq_nosync(tpm_dev->irq);
355
356         return IRQ_HANDLED;
357 } /* tpm_ioserirq_handler() */
358
359 /*
360  * st33zp24_send send TPM commands through the I2C bus.
361  *
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.
367  */
368 static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf,
369                          size_t len)
370 {
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;
376
377         if (!chip)
378                 return -EBUSY;
379         if (len < TPM_HEADER_SIZE)
380                 return -EBUSY;
381
382         ret = request_locality(chip);
383         if (ret < 0)
384                 return ret;
385
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;
394                 }
395         }
396
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;
406
407                 i += size;
408         }
409
410         status = st33zp24_status(chip);
411         if ((status & TPM_STS_DATA_EXPECT) == 0) {
412                 ret = -EIO;
413                 goto out_err;
414         }
415
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;
420
421         status = st33zp24_status(chip);
422         if ((status & TPM_STS_DATA_EXPECT) != 0) {
423                 ret = -EIO;
424                 goto out_err;
425         }
426
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;
431
432         if (chip->flags & TPM_CHIP_FLAG_IRQ) {
433                 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
434
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;
440         }
441
442         return len;
443 out_err:
444         st33zp24_cancel(chip);
445         release_locality(chip);
446         return ret;
447 }
448
449 /*
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.
456  */
457 static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf,
458                             size_t count)
459 {
460         int size = 0;
461         int expected;
462
463         if (!chip)
464                 return -EBUSY;
465
466         if (count < TPM_HEADER_SIZE) {
467                 size = -EIO;
468                 goto out;
469         }
470
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;
475         }
476
477         expected = be32_to_cpu(*(__be32 *)(buf + 2));
478         if (expected > count) {
479                 size = -EIO;
480                 goto out;
481         }
482
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;
488         }
489
490 out:
491         st33zp24_cancel(chip);
492         release_locality(chip);
493         return size;
494 }
495
496 /*
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.
501  */
502 static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status)
503 {
504         return (status == TPM_STS_COMMAND_READY);
505 }
506
507 static const struct tpm_class_ops st33zp24_tpm = {
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,
515 };
516
517 /*
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.
522  *       -1 in other case.
523  */
524 int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
525                    struct device *dev, int irq, int io_lpcpd)
526 {
527         int ret;
528         u8 intmask = 0;
529         struct tpm_chip *chip;
530         struct st33zp24_dev *tpm_dev;
531
532         chip = tpmm_chip_alloc(dev, &st33zp24_tpm);
533         if (IS_ERR(chip))
534                 return PTR_ERR(chip);
535
536         tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev),
537                                GFP_KERNEL);
538         if (!tpm_dev)
539                 return -ENOMEM;
540
541         tpm_dev->phy_id = phy_id;
542         tpm_dev->ops = ops;
543         dev_set_drvdata(&chip->dev, tpm_dev);
544
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);
549
550         tpm_dev->locality = LOCALITY0;
551
552         if (irq) {
553                 /* INTERRUPT Setup */
554                 init_waitqueue_head(&tpm_dev->read_queue);
555                 tpm_dev->intrs = 0;
556
557                 if (request_locality(chip) != LOCALITY0) {
558                         ret = -ENODEV;
559                         goto _tpm_clean_answer;
560                 }
561
562                 clear_interruption(tpm_dev);
563                 ret = devm_request_irq(dev, irq, tpm_ioserirq_handler,
564                                 IRQF_TRIGGER_HIGH, "TPM SERIRQ management",
565                                 chip);
566                 if (ret < 0) {
567                         dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n",
568                                 irq);
569                         goto _tpm_clean_answer;
570                 }
571
572                 intmask |= TPM_INTF_CMD_READY_INT
573                         |  TPM_INTF_STS_VALID_INT
574                         |  TPM_INTF_DATA_AVAIL_INT;
575
576                 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE,
577                                          &intmask, 1);
578                 if (ret < 0)
579                         goto _tpm_clean_answer;
580
581                 intmask = TPM_GLOBAL_INT_ENABLE;
582                 ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3),
583                                          &intmask, 1);
584                 if (ret < 0)
585                         goto _tpm_clean_answer;
586
587                 tpm_dev->irq = irq;
588                 chip->flags |= TPM_CHIP_FLAG_IRQ;
589
590                 disable_irq_nosync(tpm_dev->irq);
591
592                 tpm_gen_interrupt(chip);
593         }
594
595         tpm_get_timeouts(chip);
596         tpm_do_selftest(chip);
597
598         return tpm_chip_register(chip);
599 _tpm_clean_answer:
600         dev_info(&chip->dev, "TPM initialization fail\n");
601         return ret;
602 }
603 EXPORT_SYMBOL(st33zp24_probe);
604
605 /*
606  * st33zp24_remove remove the TPM device
607  * @param: tpm_data, the tpm phy.
608  * @return: 0 in case of success.
609  */
610 int st33zp24_remove(struct tpm_chip *chip)
611 {
612         tpm_chip_unregister(chip);
613         return 0;
614 }
615 EXPORT_SYMBOL(st33zp24_remove);
616
617 #ifdef CONFIG_PM_SLEEP
618 /*
619  * st33zp24_pm_suspend suspend the TPM device
620  * @param: tpm_data, the tpm phy.
621  * @param: mesg, the power management message.
622  * @return: 0 in case of success.
623  */
624 int st33zp24_pm_suspend(struct device *dev)
625 {
626         struct tpm_chip *chip = dev_get_drvdata(dev);
627         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
628
629         int ret = 0;
630
631         if (gpio_is_valid(tpm_dev->io_lpcpd))
632                 gpio_set_value(tpm_dev->io_lpcpd, 0);
633         else
634                 ret = tpm_pm_suspend(dev);
635
636         return ret;
637 } /* st33zp24_pm_suspend() */
638 EXPORT_SYMBOL(st33zp24_pm_suspend);
639
640 /*
641  * st33zp24_pm_resume resume the TPM device
642  * @param: tpm_data, the tpm phy.
643  * @return: 0 in case of success.
644  */
645 int st33zp24_pm_resume(struct device *dev)
646 {
647         struct tpm_chip *chip = dev_get_drvdata(dev);
648         struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
649         int ret = 0;
650
651         if (gpio_is_valid(tpm_dev->io_lpcpd)) {
652                 gpio_set_value(tpm_dev->io_lpcpd, 1);
653                 ret = wait_for_stat(chip,
654                                 TPM_STS_VALID, chip->timeout_b,
655                                 &tpm_dev->read_queue, false);
656         } else {
657                 ret = tpm_pm_resume(dev);
658                 if (!ret)
659                         tpm_do_selftest(chip);
660         }
661         return ret;
662 } /* st33zp24_pm_resume() */
663 EXPORT_SYMBOL(st33zp24_pm_resume);
664 #endif
665
666 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
667 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
668 MODULE_VERSION("1.3.0");
669 MODULE_LICENSE("GPL");