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