This source file includes following definitions.
- clear_interruption
- st33zp24_cancel
- st33zp24_status
- check_locality
- request_locality
- release_locality
- get_burstcount
- wait_for_tpm_stat_cond
- wait_for_stat
- recv_data
- tpm_ioserirq_handler
- st33zp24_send
- st33zp24_recv
- st33zp24_req_canceled
- st33zp24_probe
- st33zp24_remove
- st33zp24_pm_suspend
- st33zp24_pm_resume
1
2
3
4
5
6
7 #include <linux/module.h>
8 #include <linux/fs.h>
9 #include <linux/kernel.h>
10 #include <linux/delay.h>
11 #include <linux/wait.h>
12 #include <linux/freezer.h>
13 #include <linux/string.h>
14 #include <linux/interrupt.h>
15 #include <linux/gpio.h>
16 #include <linux/sched.h>
17 #include <linux/uaccess.h>
18 #include <linux/io.h>
19 #include <linux/slab.h>
20
21 #include "../tpm.h"
22 #include "st33zp24.h"
23
24 #define TPM_ACCESS 0x0
25 #define TPM_STS 0x18
26 #define TPM_DATA_FIFO 0x24
27 #define TPM_INTF_CAPABILITY 0x14
28 #define TPM_INT_STATUS 0x10
29 #define TPM_INT_ENABLE 0x08
30
31 #define LOCALITY0 0
32
33 enum st33zp24_access {
34 TPM_ACCESS_VALID = 0x80,
35 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
36 TPM_ACCESS_REQUEST_PENDING = 0x04,
37 TPM_ACCESS_REQUEST_USE = 0x02,
38 };
39
40 enum st33zp24_status {
41 TPM_STS_VALID = 0x80,
42 TPM_STS_COMMAND_READY = 0x40,
43 TPM_STS_GO = 0x20,
44 TPM_STS_DATA_AVAIL = 0x10,
45 TPM_STS_DATA_EXPECT = 0x08,
46 };
47
48 enum st33zp24_int_flags {
49 TPM_GLOBAL_INT_ENABLE = 0x80,
50 TPM_INTF_CMD_READY_INT = 0x080,
51 TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
52 TPM_INTF_WAKE_UP_READY_INT = 0x020,
53 TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
54 TPM_INTF_STS_VALID_INT = 0x002,
55 TPM_INTF_DATA_AVAIL_INT = 0x001,
56 };
57
58 enum tis_defaults {
59 TIS_SHORT_TIMEOUT = 750,
60 TIS_LONG_TIMEOUT = 2000,
61 };
62
63
64
65
66
67
68 static u8 clear_interruption(struct st33zp24_dev *tpm_dev)
69 {
70 u8 interrupt;
71
72 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
73 tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1);
74 return interrupt;
75 }
76
77
78
79
80
81
82 static void st33zp24_cancel(struct tpm_chip *chip)
83 {
84 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
85 u8 data;
86
87 data = TPM_STS_COMMAND_READY;
88 tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
89 }
90
91
92
93
94
95
96 static u8 st33zp24_status(struct tpm_chip *chip)
97 {
98 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
99 u8 data;
100
101 tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1);
102 return data;
103 }
104
105
106
107
108
109
110 static bool check_locality(struct tpm_chip *chip)
111 {
112 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
113 u8 data;
114 u8 status;
115
116 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
117 if (status && (data &
118 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
119 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
120 return true;
121
122 return false;
123 }
124
125
126
127
128
129
130 static int request_locality(struct tpm_chip *chip)
131 {
132 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
133 unsigned long stop;
134 long ret;
135 u8 data;
136
137 if (check_locality(chip))
138 return tpm_dev->locality;
139
140 data = TPM_ACCESS_REQUEST_USE;
141 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
142 if (ret < 0)
143 return ret;
144
145 stop = jiffies + chip->timeout_a;
146
147
148 do {
149 if (check_locality(chip))
150 return tpm_dev->locality;
151 msleep(TPM_TIMEOUT);
152 } while (time_before(jiffies, stop));
153
154
155 return -EACCES;
156 }
157
158
159
160
161
162 static void release_locality(struct tpm_chip *chip)
163 {
164 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
165 u8 data;
166
167 data = TPM_ACCESS_ACTIVE_LOCALITY;
168
169 tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1);
170 }
171
172
173
174
175
176
177 static int get_burstcount(struct tpm_chip *chip)
178 {
179 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
180 unsigned long stop;
181 int burstcnt, status;
182 u8 temp;
183
184 stop = jiffies + chip->timeout_d;
185 do {
186 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 1,
187 &temp, 1);
188 if (status < 0)
189 return -EBUSY;
190
191 burstcnt = temp;
192 status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 2,
193 &temp, 1);
194 if (status < 0)
195 return -EBUSY;
196
197 burstcnt |= temp << 8;
198 if (burstcnt)
199 return burstcnt;
200 msleep(TPM_TIMEOUT);
201 } while (time_before(jiffies, stop));
202 return -EBUSY;
203 }
204
205
206
207
208
209
210
211
212
213
214
215 static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
216 bool check_cancel, bool *canceled)
217 {
218 u8 status = chip->ops->status(chip);
219
220 *canceled = false;
221 if ((status & mask) == mask)
222 return true;
223 if (check_cancel && chip->ops->req_canceled(chip, status)) {
224 *canceled = true;
225 return true;
226 }
227 return false;
228 }
229
230
231
232
233
234
235
236
237
238
239 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
240 wait_queue_head_t *queue, bool check_cancel)
241 {
242 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
243 unsigned long stop;
244 int ret = 0;
245 bool canceled = false;
246 bool condition;
247 u32 cur_intrs;
248 u8 status;
249
250
251 status = st33zp24_status(chip);
252 if ((status & mask) == mask)
253 return 0;
254
255 stop = jiffies + timeout;
256
257 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
258 cur_intrs = tpm_dev->intrs;
259 clear_interruption(tpm_dev);
260 enable_irq(tpm_dev->irq);
261
262 do {
263 if (ret == -ERESTARTSYS && freezing(current))
264 clear_thread_flag(TIF_SIGPENDING);
265
266 timeout = stop - jiffies;
267 if ((long) timeout <= 0)
268 return -1;
269
270 ret = wait_event_interruptible_timeout(*queue,
271 cur_intrs != tpm_dev->intrs,
272 timeout);
273 clear_interruption(tpm_dev);
274 condition = wait_for_tpm_stat_cond(chip, mask,
275 check_cancel, &canceled);
276 if (ret >= 0 && condition) {
277 if (canceled)
278 return -ECANCELED;
279 return 0;
280 }
281 } while (ret == -ERESTARTSYS && freezing(current));
282
283 disable_irq_nosync(tpm_dev->irq);
284
285 } else {
286 do {
287 msleep(TPM_TIMEOUT);
288 status = chip->ops->status(chip);
289 if ((status & mask) == mask)
290 return 0;
291 } while (time_before(jiffies, stop));
292 }
293
294 return -ETIME;
295 }
296
297
298
299
300
301
302
303
304 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
305 {
306 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
307 int size = 0, burstcnt, len, ret;
308
309 while (size < count &&
310 wait_for_stat(chip,
311 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
312 chip->timeout_c,
313 &tpm_dev->read_queue, true) == 0) {
314 burstcnt = get_burstcount(chip);
315 if (burstcnt < 0)
316 return burstcnt;
317 len = min_t(int, burstcnt, count - size);
318 ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO,
319 buf + size, len);
320 if (ret < 0)
321 return ret;
322
323 size += len;
324 }
325 return size;
326 }
327
328
329
330
331
332
333
334 static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
335 {
336 struct tpm_chip *chip = dev_id;
337 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
338
339 tpm_dev->intrs++;
340 wake_up_interruptible(&tpm_dev->read_queue);
341 disable_irq_nosync(tpm_dev->irq);
342
343 return IRQ_HANDLED;
344 }
345
346
347
348
349
350
351
352
353
354
355 static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf,
356 size_t len)
357 {
358 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
359 u32 status, i, size, ordinal;
360 int burstcnt = 0;
361 int ret;
362 u8 data;
363
364 if (len < TPM_HEADER_SIZE)
365 return -EBUSY;
366
367 ret = request_locality(chip);
368 if (ret < 0)
369 return ret;
370
371 status = st33zp24_status(chip);
372 if ((status & TPM_STS_COMMAND_READY) == 0) {
373 st33zp24_cancel(chip);
374 if (wait_for_stat
375 (chip, TPM_STS_COMMAND_READY, chip->timeout_b,
376 &tpm_dev->read_queue, false) < 0) {
377 ret = -ETIME;
378 goto out_err;
379 }
380 }
381
382 for (i = 0; i < len - 1;) {
383 burstcnt = get_burstcount(chip);
384 if (burstcnt < 0)
385 return burstcnt;
386 size = min_t(int, len - i - 1, burstcnt);
387 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
388 buf + i, size);
389 if (ret < 0)
390 goto out_err;
391
392 i += size;
393 }
394
395 status = st33zp24_status(chip);
396 if ((status & TPM_STS_DATA_EXPECT) == 0) {
397 ret = -EIO;
398 goto out_err;
399 }
400
401 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO,
402 buf + len - 1, 1);
403 if (ret < 0)
404 goto out_err;
405
406 status = st33zp24_status(chip);
407 if ((status & TPM_STS_DATA_EXPECT) != 0) {
408 ret = -EIO;
409 goto out_err;
410 }
411
412 data = TPM_STS_GO;
413 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1);
414 if (ret < 0)
415 goto out_err;
416
417 if (chip->flags & TPM_CHIP_FLAG_IRQ) {
418 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
419
420 ret = wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID,
421 tpm_calc_ordinal_duration(chip, ordinal),
422 &tpm_dev->read_queue, false);
423 if (ret < 0)
424 goto out_err;
425 }
426
427 return 0;
428 out_err:
429 st33zp24_cancel(chip);
430 release_locality(chip);
431 return ret;
432 }
433
434
435
436
437
438
439
440
441
442 static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf,
443 size_t count)
444 {
445 int size = 0;
446 u32 expected;
447
448 if (!chip)
449 return -EBUSY;
450
451 if (count < TPM_HEADER_SIZE) {
452 size = -EIO;
453 goto out;
454 }
455
456 size = recv_data(chip, buf, TPM_HEADER_SIZE);
457 if (size < TPM_HEADER_SIZE) {
458 dev_err(&chip->dev, "Unable to read header\n");
459 goto out;
460 }
461
462 expected = be32_to_cpu(*(__be32 *)(buf + 2));
463 if (expected > count || expected < TPM_HEADER_SIZE) {
464 size = -EIO;
465 goto out;
466 }
467
468 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
469 expected - TPM_HEADER_SIZE);
470 if (size < expected) {
471 dev_err(&chip->dev, "Unable to read remainder of result\n");
472 size = -ETIME;
473 }
474
475 out:
476 st33zp24_cancel(chip);
477 release_locality(chip);
478 return size;
479 }
480
481
482
483
484
485
486
487 static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status)
488 {
489 return (status == TPM_STS_COMMAND_READY);
490 }
491
492 static const struct tpm_class_ops st33zp24_tpm = {
493 .flags = TPM_OPS_AUTO_STARTUP,
494 .send = st33zp24_send,
495 .recv = st33zp24_recv,
496 .cancel = st33zp24_cancel,
497 .status = st33zp24_status,
498 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
499 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
500 .req_canceled = st33zp24_req_canceled,
501 };
502
503
504
505
506
507
508
509
510 int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops,
511 struct device *dev, int irq, int io_lpcpd)
512 {
513 int ret;
514 u8 intmask = 0;
515 struct tpm_chip *chip;
516 struct st33zp24_dev *tpm_dev;
517
518 chip = tpmm_chip_alloc(dev, &st33zp24_tpm);
519 if (IS_ERR(chip))
520 return PTR_ERR(chip);
521
522 tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev),
523 GFP_KERNEL);
524 if (!tpm_dev)
525 return -ENOMEM;
526
527 tpm_dev->phy_id = phy_id;
528 tpm_dev->ops = ops;
529 dev_set_drvdata(&chip->dev, tpm_dev);
530
531 chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
532 chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
533 chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
534 chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
535
536 tpm_dev->locality = LOCALITY0;
537
538 if (irq) {
539
540 init_waitqueue_head(&tpm_dev->read_queue);
541 tpm_dev->intrs = 0;
542
543 if (request_locality(chip) != LOCALITY0) {
544 ret = -ENODEV;
545 goto _tpm_clean_answer;
546 }
547
548 clear_interruption(tpm_dev);
549 ret = devm_request_irq(dev, irq, tpm_ioserirq_handler,
550 IRQF_TRIGGER_HIGH, "TPM SERIRQ management",
551 chip);
552 if (ret < 0) {
553 dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n",
554 irq);
555 goto _tpm_clean_answer;
556 }
557
558 intmask |= TPM_INTF_CMD_READY_INT
559 | TPM_INTF_STS_VALID_INT
560 | TPM_INTF_DATA_AVAIL_INT;
561
562 ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE,
563 &intmask, 1);
564 if (ret < 0)
565 goto _tpm_clean_answer;
566
567 intmask = TPM_GLOBAL_INT_ENABLE;
568 ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3),
569 &intmask, 1);
570 if (ret < 0)
571 goto _tpm_clean_answer;
572
573 tpm_dev->irq = irq;
574 chip->flags |= TPM_CHIP_FLAG_IRQ;
575
576 disable_irq_nosync(tpm_dev->irq);
577 }
578
579 return tpm_chip_register(chip);
580 _tpm_clean_answer:
581 dev_info(&chip->dev, "TPM initialization fail\n");
582 return ret;
583 }
584 EXPORT_SYMBOL(st33zp24_probe);
585
586
587
588
589
590
591 int st33zp24_remove(struct tpm_chip *chip)
592 {
593 tpm_chip_unregister(chip);
594 return 0;
595 }
596 EXPORT_SYMBOL(st33zp24_remove);
597
598 #ifdef CONFIG_PM_SLEEP
599
600
601
602
603
604
605 int st33zp24_pm_suspend(struct device *dev)
606 {
607 struct tpm_chip *chip = dev_get_drvdata(dev);
608 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
609
610 int ret = 0;
611
612 if (gpio_is_valid(tpm_dev->io_lpcpd))
613 gpio_set_value(tpm_dev->io_lpcpd, 0);
614 else
615 ret = tpm_pm_suspend(dev);
616
617 return ret;
618 }
619 EXPORT_SYMBOL(st33zp24_pm_suspend);
620
621
622
623
624
625
626 int st33zp24_pm_resume(struct device *dev)
627 {
628 struct tpm_chip *chip = dev_get_drvdata(dev);
629 struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev);
630 int ret = 0;
631
632 if (gpio_is_valid(tpm_dev->io_lpcpd)) {
633 gpio_set_value(tpm_dev->io_lpcpd, 1);
634 ret = wait_for_stat(chip,
635 TPM_STS_VALID, chip->timeout_b,
636 &tpm_dev->read_queue, false);
637 } else {
638 ret = tpm_pm_resume(dev);
639 if (!ret)
640 tpm1_do_selftest(chip);
641 }
642 return ret;
643 }
644 EXPORT_SYMBOL(st33zp24_pm_resume);
645 #endif
646
647 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
648 MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver");
649 MODULE_VERSION("1.3.0");
650 MODULE_LICENSE("GPL");