File Index
Files and directories
Sign in
FILE

tpm_tis_i2c_cr50.c

File preview with syntax highlighting.

linux-6.19.14/drivers/char/tpm/tpm_tis_i2c_cr50.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright 2020 Google Inc.
  4 *
  5 * Based on Infineon TPM driver by Peter Huewe.
  6 *
  7 * cr50 is a firmware for H1 secure modules that requires special
  8 * handling for the I2C interface.
  9 *
 10 * - Use an interrupt for transaction status instead of hardcoded delays.
 11 * - Must use write+wait+read read protocol.
 12 * - All 4 bytes of status register must be read/written at once.
 13 * - Burst count max is 63 bytes, and burst count behaves slightly differently
 14 *   than other I2C TPMs.
 15 * - When reading from FIFO the full burstcnt must be read instead of just
 16 *   reading header and determining the remainder.
 17 */
 18
 19#include <linux/acpi.h>
 20#include <linux/bug.h>
 21#include <linux/completion.h>
 22#include <linux/i2c.h>
 23#include <linux/interrupt.h>
 24#include <linux/module.h>
 25#include <linux/pm.h>
 26#include <linux/slab.h>
 27#include <linux/wait.h>
 28
 29#include "tpm_tis_core.h"
 30
 31#define TPM_CR50_MAX_BUFSIZE		64
 32#define TPM_CR50_TIMEOUT_SHORT_MS	2		/* Short timeout during transactions */
 33#define TPM_CR50_TIMEOUT_NOIRQ_MS	20		/* Timeout for TPM ready without IRQ */
 34#define TPM_CR50_I2C_DID_VID		0x00281ae0L	/* Device and vendor ID for Cr50 H1 */
 35#define TPM_TI50_DT_I2C_DID_VID		0x504a6666L	/* Device and vendor ID for Ti50 DT */
 36#define TPM_TI50_OT_I2C_DID_VID		0x50666666L	/* Device and vendor ID for TI50 OT */
 37#define TPM_CR50_I2C_MAX_RETRIES	3		/* Max retries due to I2C errors */
 38#define TPM_CR50_I2C_RETRY_DELAY_LO	55		/* Min usecs between retries on I2C */
 39#define TPM_CR50_I2C_RETRY_DELAY_HI	65		/* Max usecs between retries on I2C */
 40#define TPM_CR50_I2C_DEFAULT_LOC	0
 41
 42#define TPM_I2C_ACCESS(l)	(0x0000 | ((l) << 4))
 43#define TPM_I2C_STS(l)		(0x0001 | ((l) << 4))
 44#define TPM_I2C_DATA_FIFO(l)	(0x0005 | ((l) << 4))
 45#define TPM_I2C_DID_VID(l)	(0x0006 | ((l) << 4))
 46
 47/**
 48 * struct tpm_i2c_cr50_priv_data - Driver private data.
 49 * @irq:	Irq number used for this chip.
 50 *		If irq <= 0, then a fixed timeout is used instead of waiting for irq.
 51 * @tpm_ready:	Struct used by irq handler to signal R/W readiness.
 52 * @buf:	Buffer used for i2c writes, with i2c address prepended to content.
 53 *
 54 * Private driver struct used by kernel threads and interrupt context.
 55 */
 56struct tpm_i2c_cr50_priv_data {
 57	int irq;
 58	struct completion tpm_ready;
 59	u8 buf[TPM_CR50_MAX_BUFSIZE];
 60};
 61
 62/**
 63 * tpm_cr50_i2c_int_handler() - cr50 interrupt handler.
 64 * @dummy:	Unused parameter.
 65 * @tpm_info:	TPM chip information.
 66 *
 67 * The cr50 interrupt handler signals waiting threads that the
 68 * interrupt has been asserted. It does not do any interrupt triggered
 69 * processing but is instead used to avoid fixed delays.
 70 *
 71 * Return:
 72 *	IRQ_HANDLED signifies irq was handled by this device.
 73 */
 74static irqreturn_t tpm_cr50_i2c_int_handler(int dummy, void *tpm_info)
 75{
 76	struct tpm_chip *chip = tpm_info;
 77	struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
 78
 79	complete(&priv->tpm_ready);
 80
 81	return IRQ_HANDLED;
 82}
 83
 84/**
 85 * tpm_cr50_i2c_wait_tpm_ready() - Wait for tpm to signal ready.
 86 * @chip: A TPM chip.
 87 *
 88 * Wait for completion interrupt if available, otherwise use a fixed
 89 * delay for the TPM to be ready.
 90 *
 91 * Return:
 92 * - 0:		Success.
 93 * - -errno:	A POSIX error code.
 94 */
 95static int tpm_cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
 96{
 97	struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
 98
 99	/* Use a safe fixed delay if interrupt is not supported */
100	if (priv->irq <= 0) {
101		msleep(TPM_CR50_TIMEOUT_NOIRQ_MS);
102		return 0;
103	}
104
105	/* Wait for interrupt to indicate TPM is ready to respond */
106	if (!wait_for_completion_timeout(&priv->tpm_ready, chip->timeout_a)) {
107		dev_warn(&chip->dev, "Timeout waiting for TPM ready\n");
108		return -ETIMEDOUT;
109	}
110
111	return 0;
112}
113
114/**
115 * tpm_cr50_i2c_enable_tpm_irq() - Enable TPM irq.
116 * @chip: A TPM chip.
117 */
118static void tpm_cr50_i2c_enable_tpm_irq(struct tpm_chip *chip)
119{
120	struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
121
122	if (priv->irq > 0) {
123		reinit_completion(&priv->tpm_ready);
124		enable_irq(priv->irq);
125	}
126}
127
128/**
129 * tpm_cr50_i2c_disable_tpm_irq() - Disable TPM irq.
130 * @chip: A TPM chip.
131 */
132static void tpm_cr50_i2c_disable_tpm_irq(struct tpm_chip *chip)
133{
134	struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
135
136	if (priv->irq > 0)
137		disable_irq(priv->irq);
138}
139
140/**
141 * tpm_cr50_i2c_transfer_message() - Transfer a message over i2c.
142 * @dev:	Device information.
143 * @adapter:	I2C adapter.
144 * @msg:	Message to transfer.
145 *
146 * Call unlocked i2c transfer routine with the provided parameters and
147 * retry in case of bus errors.
148 *
149 * Return:
150 * - 0:		Success.
151 * - -errno:	A POSIX error code.
152 */
153static int tpm_cr50_i2c_transfer_message(struct device *dev,
154					 struct i2c_adapter *adapter,
155					 struct i2c_msg *msg)
156{
157	unsigned int try;
158	int rc;
159
160	for (try = 0; try < TPM_CR50_I2C_MAX_RETRIES; try++) {
161		rc = __i2c_transfer(adapter, msg, 1);
162		if (rc == 1)
163			return 0; /* Successfully transferred the message */
164		if (try)
165			dev_warn(dev, "i2c transfer failed (attempt %d/%d): %d\n",
166				 try + 1, TPM_CR50_I2C_MAX_RETRIES, rc);
167		usleep_range(TPM_CR50_I2C_RETRY_DELAY_LO, TPM_CR50_I2C_RETRY_DELAY_HI);
168	}
169
170	/* No i2c message transferred */
171	return -EIO;
172}
173
174/**
175 * tpm_cr50_i2c_read() - Read from TPM register.
176 * @chip:	A TPM chip.
177 * @addr:	Register address to read from.
178 * @buffer:	Read destination, provided by caller.
179 * @len:	Number of bytes to read.
180 *
181 * Sends the register address byte to the TPM, then waits until TPM
182 * is ready via interrupt signal or timeout expiration, then 'len'
183 * bytes are read from TPM response into the provided 'buffer'.
184 *
185 * Return:
186 * - 0:		Success.
187 * - -errno:	A POSIX error code.
188 */
189static int tpm_cr50_i2c_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t len)
190{
191	struct i2c_client *client = to_i2c_client(chip->dev.parent);
192	struct i2c_msg msg_reg_addr = {
193		.addr = client->addr,
194		.len = 1,
195		.buf = &addr
196	};
197	struct i2c_msg msg_response = {
198		.addr = client->addr,
199		.flags = I2C_M_RD,
200		.len = len,
201		.buf = buffer
202	};
203	int rc;
204
205	/* Prepare for completion interrupt */
206	tpm_cr50_i2c_enable_tpm_irq(chip);
207
208	/* Send the register address byte to the TPM */
209	rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_reg_addr);
210	if (rc < 0)
211		goto out;
212
213	/* Wait for TPM to be ready with response data */
214	rc = tpm_cr50_i2c_wait_tpm_ready(chip);
215	if (rc < 0)
216		goto out;
217
218	/* Read response data from the TPM */
219	rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_response);
220
221out:
222	tpm_cr50_i2c_disable_tpm_irq(chip);
223
224	if (rc < 0)
225		return rc;
226
227	return 0;
228}
229
230/**
231 * tpm_cr50_i2c_write()- Write to TPM register.
232 * @chip:	A TPM chip.
233 * @addr:	Register address to write to.
234 * @buffer:	Data to write.
235 * @len:	Number of bytes to write.
236 *
237 * The provided address is prepended to the data in 'buffer', the
238 * combined address+data is sent to the TPM, then wait for TPM to
239 * indicate it is done writing.
240 *
241 * Return:
242 * - 0:		Success.
243 * - -errno:	A POSIX error code.
244 */
245static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer,
246			      size_t len)
247{
248	struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev);
249	struct i2c_client *client = to_i2c_client(chip->dev.parent);
250	struct i2c_msg msg = {
251		.addr = client->addr,
252		.len = len + 1,
253		.buf = priv->buf
254	};
255	int rc;
256
257	if (len > TPM_CR50_MAX_BUFSIZE - 1)
258		return -EINVAL;
259
260	/* Prepend the 'register address' to the buffer */
261	priv->buf[0] = addr;
262	memcpy(priv->buf + 1, buffer, len);
263
264	/* Prepare for completion interrupt */
265	tpm_cr50_i2c_enable_tpm_irq(chip);
266
267	/* Send write request buffer with address */
268	rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg);
269	if (rc < 0)
270		goto out;
271
272	/* Wait for TPM to be ready, ignore timeout */
273	tpm_cr50_i2c_wait_tpm_ready(chip);
274
275out:
276	tpm_cr50_i2c_disable_tpm_irq(chip);
277
278	if (rc < 0)
279		return rc;
280
281	return 0;
282}
283
284/**
285 * tpm_cr50_check_locality() - Verify if required TPM locality is active.
286 * @chip: A TPM chip.
287 * @loc: Locality to be verified
288 *
289 * Return:
290 * - loc:	Success.
291 * - -errno:	A POSIX error code.
292 */
293static int tpm_cr50_check_locality(struct tpm_chip *chip, int loc)
294{
295	u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
296	u8 buf;
297	int rc;
298
299	rc = tpm_cr50_i2c_read(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
300	if (rc < 0)
301		return rc;
302
303	if ((buf & mask) == mask)
304		return loc;
305
306	return -EIO;
307}
308
309/**
310 * tpm_cr50_release_locality() - Release TPM locality.
311 * @chip:	A TPM chip.
312 * @loc:	Locality to be released
313 *
314 * Return:
315 * - 0:		Success.
316 * - -errno:	A POSIX error code.
317 */
318static int tpm_cr50_release_locality(struct tpm_chip *chip, int loc)
319{
320	struct i2c_client *client = to_i2c_client(chip->dev.parent);
321	u8 mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
322	u8 addr = TPM_I2C_ACCESS(loc);
323	u8 buf;
324	int rc;
325
326	rc = tpm_cr50_i2c_read(chip, addr, &buf, sizeof(buf));
327	if (rc < 0)
328		goto unlock_out;
329
330	if ((buf & mask) == mask) {
331		buf = TPM_ACCESS_ACTIVE_LOCALITY;
332		rc = tpm_cr50_i2c_write(chip, addr, &buf, sizeof(buf));
333	}
334
335unlock_out:
336	i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
337	return rc;
338}
339
340/**
341 * tpm_cr50_request_locality() - Request TPM locality.
342 * @chip: A TPM chip.
343 * @loc: Locality to be requested.
344 *
345 * Return:
346 * - loc:	Success.
347 * - -errno:	A POSIX error code.
348 */
349static int tpm_cr50_request_locality(struct tpm_chip *chip, int loc)
350{
351	struct i2c_client *client = to_i2c_client(chip->dev.parent);
352	u8 buf = TPM_ACCESS_REQUEST_USE;
353	unsigned long stop;
354	int rc;
355
356	i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT);
357
358	if (tpm_cr50_check_locality(chip, loc) == loc)
359		return loc;
360
361	rc = tpm_cr50_i2c_write(chip, TPM_I2C_ACCESS(loc), &buf, sizeof(buf));
362	if (rc < 0)
363		goto unlock_out;
364
365	stop = jiffies + chip->timeout_a;
366	do {
367		if (tpm_cr50_check_locality(chip, loc) == loc)
368			return loc;
369
370		msleep(TPM_CR50_TIMEOUT_SHORT_MS);
371	} while (time_before(jiffies, stop));
372
373	rc = -ETIMEDOUT;
374
375unlock_out:
376	i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT);
377	return rc;
378}
379
380/**
381 * tpm_cr50_i2c_tis_status() - Read cr50 tis status.
382 * @chip: A TPM chip.
383 *
384 * cr50 requires all 4 bytes of status register to be read.
385 *
386 * Return:
387 *	TPM status byte.
388 */
389static u8 tpm_cr50_i2c_tis_status(struct tpm_chip *chip)
390{
391	u8 buf[4];
392
393	if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf)) < 0)
394		return 0;
395
396	return buf[0];
397}
398
399/**
400 * tpm_cr50_i2c_tis_set_ready() - Set status register to ready.
401 * @chip: A TPM chip.
402 *
403 * cr50 requires all 4 bytes of status register to be written.
404 */
405static void tpm_cr50_i2c_tis_set_ready(struct tpm_chip *chip)
406{
407	u8 buf[4] = { TPM_STS_COMMAND_READY };
408
409	tpm_cr50_i2c_write(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf));
410	msleep(TPM_CR50_TIMEOUT_SHORT_MS);
411}
412
413/**
414 * tpm_cr50_i2c_get_burst_and_status() - Get burst count and status.
415 * @chip:	A TPM chip.
416 * @mask:	Status mask.
417 * @burst:	Return value for burst.
418 * @status:	Return value for status.
419 *
420 * cr50 uses bytes 3:2 of status register for burst count and
421 * all 4 bytes must be read.
422 *
423 * Return:
424 * - 0:		Success.
425 * - -errno:	A POSIX error code.
426 */
427static int tpm_cr50_i2c_get_burst_and_status(struct tpm_chip *chip, u8 mask,
428					     size_t *burst, u32 *status)
429{
430	unsigned long stop;
431	u8 buf[4];
432
433	*status = 0;
434
435	/* wait for burstcount */
436	stop = jiffies + chip->timeout_b;
437
438	do {
439		if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(chip->locality), buf, sizeof(buf)) < 0) {
440			msleep(TPM_CR50_TIMEOUT_SHORT_MS);
441			continue;
442		}
443
444		*status = *buf;
445		*burst = le16_to_cpup((__le16 *)(buf + 1));
446
447		if ((*status & mask) == mask &&
448		    *burst > 0 && *burst <= TPM_CR50_MAX_BUFSIZE - 1)
449			return 0;
450
451		msleep(TPM_CR50_TIMEOUT_SHORT_MS);
452	} while (time_before(jiffies, stop));
453
454	dev_err(&chip->dev, "Timeout reading burst and status\n");
455	return -ETIMEDOUT;
456}
457
458/**
459 * tpm_cr50_i2c_tis_recv() - TPM reception callback.
460 * @chip:	A TPM chip.
461 * @buf:	Reception buffer.
462 * @buf_len:	Buffer length to read.
463 *
464 * Return:
465 * - >= 0:	Number of read bytes.
466 * - -errno:	A POSIX error code.
467 */
468static int tpm_cr50_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t buf_len)
469{
470
471	u8 mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
472	size_t burstcnt, cur, len, expected;
473	u8 addr = TPM_I2C_DATA_FIFO(chip->locality);
474	u32 status;
475	int rc;
476
477	if (buf_len < TPM_HEADER_SIZE)
478		return -EINVAL;
479
480	rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
481	if (rc < 0)
482		goto out_err;
483
484	if (burstcnt > buf_len || burstcnt < TPM_HEADER_SIZE) {
485		dev_err(&chip->dev,
486			"Unexpected burstcnt: %zu (max=%zu, min=%d)\n",
487			burstcnt, buf_len, TPM_HEADER_SIZE);
488		rc = -EIO;
489		goto out_err;
490	}
491
492	/* Read first chunk of burstcnt bytes */
493	rc = tpm_cr50_i2c_read(chip, addr, buf, burstcnt);
494	if (rc < 0) {
495		dev_err(&chip->dev, "Read of first chunk failed\n");
496		goto out_err;
497	}
498
499	/* Determine expected data in the return buffer */
500	expected = be32_to_cpup((__be32 *)(buf + 2));
501	if (expected > buf_len) {
502		dev_err(&chip->dev, "Buffer too small to receive i2c data\n");
503		rc = -E2BIG;
504		goto out_err;
505	}
506
507	/* Now read the rest of the data */
508	cur = burstcnt;
509	while (cur < expected) {
510		/* Read updated burst count and check status */
511		rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
512		if (rc < 0)
513			goto out_err;
514
515		len = min_t(size_t, burstcnt, expected - cur);
516		rc = tpm_cr50_i2c_read(chip, addr, buf + cur, len);
517		if (rc < 0) {
518			dev_err(&chip->dev, "Read failed\n");
519			goto out_err;
520		}
521
522		cur += len;
523	}
524
525	/* Ensure TPM is done reading data */
526	rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status);
527	if (rc < 0)
528		goto out_err;
529	if (status & TPM_STS_DATA_AVAIL) {
530		dev_err(&chip->dev, "Data still available\n");
531		rc = -EIO;
532		goto out_err;
533	}
534
535	return cur;
536
537out_err:
538	/* Abort current transaction if still pending */
539	if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
540		tpm_cr50_i2c_tis_set_ready(chip);
541
542	return rc;
543}
544
545/**
546 * tpm_cr50_i2c_tis_send() - TPM transmission callback.
547 * @chip:	A TPM chip.
548 * @buf:	Buffer to send.
549 * @bufsiz:	Buffer size.
550 * @len:	Command length.
551 *
552 * Return:
553 * - 0:		Success.
554 * - -errno:	A POSIX error code.
555 */
556static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t bufsiz,
557				 size_t len)
558{
559	size_t burstcnt, limit, sent = 0;
560	u8 tpm_go[4] = { TPM_STS_GO };
561	unsigned long stop;
562	u32 status;
563	int rc;
564
565	/* Wait until TPM is ready for a command */
566	stop = jiffies + chip->timeout_b;
567	while (!(tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
568		if (time_after(jiffies, stop)) {
569			rc = -ETIMEDOUT;
570			goto out_err;
571		}
572
573		tpm_cr50_i2c_tis_set_ready(chip);
574	}
575
576	while (len > 0) {
577		u8 mask = TPM_STS_VALID;
578
579		/* Wait for data if this is not the first chunk */
580		if (sent > 0)
581			mask |= TPM_STS_DATA_EXPECT;
582
583		/* Read burst count and check status */
584		rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status);
585		if (rc < 0)
586			goto out_err;
587
588		/*
589		 * Use burstcnt - 1 to account for the address byte
590		 * that is inserted by tpm_cr50_i2c_write()
591		 */
592		limit = min_t(size_t, burstcnt - 1, len);
593		rc = tpm_cr50_i2c_write(chip, TPM_I2C_DATA_FIFO(chip->locality),
594					&buf[sent], limit);
595		if (rc < 0) {
596			dev_err(&chip->dev, "Write failed\n");
597			goto out_err;
598		}
599
600		sent += limit;
601		len -= limit;
602	}
603
604	/* Ensure TPM is not expecting more data */
605	rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status);
606	if (rc < 0)
607		goto out_err;
608	if (status & TPM_STS_DATA_EXPECT) {
609		dev_err(&chip->dev, "Data still expected\n");
610		rc = -EIO;
611		goto out_err;
612	}
613
614	/* Start the TPM command */
615	rc = tpm_cr50_i2c_write(chip, TPM_I2C_STS(chip->locality), tpm_go,
616				sizeof(tpm_go));
617	if (rc < 0) {
618		dev_err(&chip->dev, "Start command failed\n");
619		goto out_err;
620	}
621	return 0;
622
623out_err:
624	/* Abort current transaction if still pending */
625	if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
626		tpm_cr50_i2c_tis_set_ready(chip);
627
628	return rc;
629}
630
631/**
632 * tpm_cr50_i2c_req_canceled() - Callback to notify a request cancel.
633 * @chip:	A TPM chip.
634 * @status:	Status given by the cancel callback.
635 *
636 * Return:
637 *	True if command is ready, False otherwise.
638 */
639static bool tpm_cr50_i2c_req_canceled(struct tpm_chip *chip, u8 status)
640{
641	return status == TPM_STS_COMMAND_READY;
642}
643
644static bool tpm_cr50_i2c_is_firmware_power_managed(struct device *dev)
645{
646	u8 val;
647	int ret;
648
649	/* This flag should default true when the device property is not present */
650	ret = device_property_read_u8(dev, "firmware-power-managed", &val);
651	if (ret)
652		return true;
653
654	return val;
655}
656
657static const struct tpm_class_ops cr50_i2c = {
658	.flags = TPM_OPS_AUTO_STARTUP,
659	.status = &tpm_cr50_i2c_tis_status,
660	.recv = &tpm_cr50_i2c_tis_recv,
661	.send = &tpm_cr50_i2c_tis_send,
662	.cancel = &tpm_cr50_i2c_tis_set_ready,
663	.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
664	.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
665	.req_canceled = &tpm_cr50_i2c_req_canceled,
666	.request_locality = &tpm_cr50_request_locality,
667	.relinquish_locality = &tpm_cr50_release_locality,
668};
669
670#ifdef CONFIG_ACPI
671static const struct acpi_device_id cr50_i2c_acpi_id[] = {
672	{ "GOOG0005", 0 },
673	{}
674};
675MODULE_DEVICE_TABLE(acpi, cr50_i2c_acpi_id);
676#endif
677
678#ifdef CONFIG_OF
679static const struct of_device_id of_cr50_i2c_match[] = {
680	{ .compatible = "google,cr50", },
681	{}
682};
683MODULE_DEVICE_TABLE(of, of_cr50_i2c_match);
684#endif
685
686/**
687 * tpm_cr50_vid_to_name() - Maps VID to name.
688 * @vendor:	Vendor identifier to map to name
689 *
690 * Return:
691 *	A valid string for the vendor or empty string
692 */
693static const char *tpm_cr50_vid_to_name(u32 vendor)
694{
695	switch (vendor) {
696	case TPM_CR50_I2C_DID_VID:
697		return "cr50";
698	case TPM_TI50_DT_I2C_DID_VID:
699		return "ti50 DT";
700	case TPM_TI50_OT_I2C_DID_VID:
701		return "ti50 OT";
702	default:
703		return "unknown";
704	}
705}
706
707/**
708 * tpm_cr50_i2c_probe() - Driver probe function.
709 * @client:	I2C client information.
710 *
711 * Return:
712 * - 0:		Success.
713 * - -errno:	A POSIX error code.
714 */
715static int tpm_cr50_i2c_probe(struct i2c_client *client)
716{
717	struct tpm_i2c_cr50_priv_data *priv;
718	struct device *dev = &client->dev;
719	struct tpm_chip *chip;
720	u32 vendor;
721	u8 buf[4];
722	int rc;
723	int loc;
724
725	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
726		return -ENODEV;
727
728	chip = tpmm_chip_alloc(dev, &cr50_i2c);
729	if (IS_ERR(chip))
730		return PTR_ERR(chip);
731
732	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
733	if (!priv)
734		return -ENOMEM;
735
736	/* cr50 is a TPM 2.0 chip */
737	chip->flags |= TPM_CHIP_FLAG_TPM2;
738	if (tpm_cr50_i2c_is_firmware_power_managed(dev))
739		chip->flags |= TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED;
740
741	/* Default timeouts */
742	chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
743	chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
744	chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
745	chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
746
747	dev_set_drvdata(&chip->dev, priv);
748	init_completion(&priv->tpm_ready);
749
750	if (client->irq > 0) {
751		rc = devm_request_irq(dev, client->irq, tpm_cr50_i2c_int_handler,
752				      IRQF_TRIGGER_FALLING | IRQF_NO_AUTOEN,
753				      dev->driver->name, chip);
754		if (rc < 0) {
755			dev_err(dev, "Failed to probe IRQ %d\n", client->irq);
756			return rc;
757		}
758
759		priv->irq = client->irq;
760	} else {
761		dev_warn(dev, "No IRQ, will use %ums delay for TPM ready\n",
762			 TPM_CR50_TIMEOUT_NOIRQ_MS);
763	}
764
765	loc = tpm_cr50_request_locality(chip, TPM_CR50_I2C_DEFAULT_LOC);
766	if (loc < 0) {
767		dev_err(dev, "Could not request locality\n");
768		return loc;
769	}
770
771	/* Read four bytes from DID_VID register */
772	rc = tpm_cr50_i2c_read(chip, TPM_I2C_DID_VID(loc), buf, sizeof(buf));
773	if (rc < 0) {
774		dev_err(dev, "Could not read vendor id\n");
775		if (tpm_cr50_release_locality(chip, loc))
776			dev_err(dev, "Could not release locality\n");
777		return rc;
778	}
779
780	rc = tpm_cr50_release_locality(chip, loc);
781	if (rc) {
782		dev_err(dev, "Could not release locality\n");
783		return rc;
784	}
785
786	vendor = le32_to_cpup((__le32 *)buf);
787	if (vendor != TPM_CR50_I2C_DID_VID &&
788	    vendor != TPM_TI50_DT_I2C_DID_VID &&
789	    vendor != TPM_TI50_OT_I2C_DID_VID) {
790		dev_err(dev, "Vendor ID did not match! ID was %08x\n", vendor);
791		return -ENODEV;
792	}
793
794	dev_info(dev, "%s TPM 2.0 (i2c 0x%02x irq %d id 0x%x)\n",
795		 tpm_cr50_vid_to_name(vendor),
796		 client->addr, client->irq, vendor >> 16);
797	return tpm_chip_register(chip);
798}
799
800/**
801 * tpm_cr50_i2c_remove() - Driver remove function.
802 * @client: I2C client information.
803 *
804 * Return:
805 * - 0:		Success.
806 * - -errno:	A POSIX error code.
807 */
808static void tpm_cr50_i2c_remove(struct i2c_client *client)
809{
810	struct tpm_chip *chip = i2c_get_clientdata(client);
811	struct device *dev = &client->dev;
812
813	if (!chip) {
814		dev_crit(dev, "Could not get client data at remove, memory corruption ahead\n");
815		return;
816	}
817
818	tpm_chip_unregister(chip);
819}
820
821static SIMPLE_DEV_PM_OPS(cr50_i2c_pm, tpm_pm_suspend, tpm_pm_resume);
822
823static struct i2c_driver cr50_i2c_driver = {
824	.probe = tpm_cr50_i2c_probe,
825	.remove = tpm_cr50_i2c_remove,
826	.driver = {
827		.name = "cr50_i2c",
828		.pm = &cr50_i2c_pm,
829		.acpi_match_table = ACPI_PTR(cr50_i2c_acpi_id),
830		.of_match_table = of_match_ptr(of_cr50_i2c_match),
831	},
832};
833
834module_i2c_driver(cr50_i2c_driver);
835
836MODULE_DESCRIPTION("cr50 TPM I2C Driver");
837MODULE_LICENSE("GPL");
~/ Read-only file preview.