File Index
Files and directories
Sign in
FILE

st33zp24.c

File preview with syntax highlighting.

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