Lines Matching +full:chip +full:- +full:to +full:- +full:chip

1 // SPDX-License-Identifier: GPL-2.0
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
15 * - When reading from FIFO the full burstcnt must be read instead of just
35 #define TPM_CR50_I2C_MAX_RETRIES 3 /* Max retries due to I2C errors */
45 * struct tpm_i2c_cr50_priv_data - Driver private data.
46 * @irq: Irq number used for this chip.
48 * @tpm_ready: Struct used by irq handler to signal R/W readiness.
49 * @buf: Buffer used for i2c writes, with i2c address prepended to content.
60 * tpm_cr50_i2c_int_handler() - cr50 interrupt handler.
62 * @tpm_info: TPM chip information.
66 * processing but is instead used to avoid fixed delays.
73 struct tpm_chip *chip = tpm_info; in tpm_cr50_i2c_int_handler() local
74 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev); in tpm_cr50_i2c_int_handler()
76 complete(&priv->tpm_ready); in tpm_cr50_i2c_int_handler()
82 * tpm_cr50_i2c_wait_tpm_ready() - Wait for tpm to signal ready.
83 * @chip: A TPM chip.
86 * delay for the TPM to be ready.
89 * - 0: Success.
90 * - -errno: A POSIX error code.
92 static int tpm_cr50_i2c_wait_tpm_ready(struct tpm_chip *chip) in tpm_cr50_i2c_wait_tpm_ready() argument
94 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev); in tpm_cr50_i2c_wait_tpm_ready()
97 if (priv->irq <= 0) { in tpm_cr50_i2c_wait_tpm_ready()
102 /* Wait for interrupt to indicate TPM is ready to respond */ in tpm_cr50_i2c_wait_tpm_ready()
103 if (!wait_for_completion_timeout(&priv->tpm_ready, chip->timeout_a)) { in tpm_cr50_i2c_wait_tpm_ready()
104 dev_warn(&chip->dev, "Timeout waiting for TPM ready\n"); in tpm_cr50_i2c_wait_tpm_ready()
105 return -ETIMEDOUT; in tpm_cr50_i2c_wait_tpm_ready()
112 * tpm_cr50_i2c_enable_tpm_irq() - Enable TPM irq.
113 * @chip: A TPM chip.
115 static void tpm_cr50_i2c_enable_tpm_irq(struct tpm_chip *chip) in tpm_cr50_i2c_enable_tpm_irq() argument
117 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev); in tpm_cr50_i2c_enable_tpm_irq()
119 if (priv->irq > 0) { in tpm_cr50_i2c_enable_tpm_irq()
120 reinit_completion(&priv->tpm_ready); in tpm_cr50_i2c_enable_tpm_irq()
121 enable_irq(priv->irq); in tpm_cr50_i2c_enable_tpm_irq()
126 * tpm_cr50_i2c_disable_tpm_irq() - Disable TPM irq.
127 * @chip: A TPM chip.
129 static void tpm_cr50_i2c_disable_tpm_irq(struct tpm_chip *chip) in tpm_cr50_i2c_disable_tpm_irq() argument
131 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev); in tpm_cr50_i2c_disable_tpm_irq()
133 if (priv->irq > 0) in tpm_cr50_i2c_disable_tpm_irq()
134 disable_irq(priv->irq); in tpm_cr50_i2c_disable_tpm_irq()
138 * tpm_cr50_i2c_transfer_message() - Transfer a message over i2c.
141 * @msg: Message to transfer.
147 * - 0: Success.
148 * - -errno: A POSIX error code.
168 return -EIO; in tpm_cr50_i2c_transfer_message()
172 * tpm_cr50_i2c_read() - Read from TPM register.
173 * @chip: A TPM chip.
174 * @addr: Register address to read from.
176 * @len: Number of bytes to read.
178 * Sends the register address byte to the TPM, then waits until TPM
183 * - 0: Success.
184 * - -errno: A POSIX error code.
186 static int tpm_cr50_i2c_read(struct tpm_chip *chip, u8 addr, u8 *buffer, size_t len) in tpm_cr50_i2c_read() argument
188 struct i2c_client *client = to_i2c_client(chip->dev.parent); in tpm_cr50_i2c_read()
190 .addr = client->addr, in tpm_cr50_i2c_read()
195 .addr = client->addr, in tpm_cr50_i2c_read()
202 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT); in tpm_cr50_i2c_read()
205 tpm_cr50_i2c_enable_tpm_irq(chip); in tpm_cr50_i2c_read()
207 /* Send the register address byte to the TPM */ in tpm_cr50_i2c_read()
208 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_reg_addr); in tpm_cr50_i2c_read()
212 /* Wait for TPM to be ready with response data */ in tpm_cr50_i2c_read()
213 rc = tpm_cr50_i2c_wait_tpm_ready(chip); in tpm_cr50_i2c_read()
218 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg_response); in tpm_cr50_i2c_read()
221 tpm_cr50_i2c_disable_tpm_irq(chip); in tpm_cr50_i2c_read()
222 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT); in tpm_cr50_i2c_read()
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.
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
242 * - 0: Success.
243 * - -errno: A POSIX error code.
245 static int tpm_cr50_i2c_write(struct tpm_chip *chip, u8 addr, u8 *buffer, in tpm_cr50_i2c_write() argument
248 struct tpm_i2c_cr50_priv_data *priv = dev_get_drvdata(&chip->dev); in tpm_cr50_i2c_write()
249 struct i2c_client *client = to_i2c_client(chip->dev.parent); in tpm_cr50_i2c_write()
251 .addr = client->addr, in tpm_cr50_i2c_write()
253 .buf = priv->buf in tpm_cr50_i2c_write()
257 if (len > TPM_CR50_MAX_BUFSIZE - 1) in tpm_cr50_i2c_write()
258 return -EINVAL; in tpm_cr50_i2c_write()
260 /* Prepend the 'register address' to the buffer */ in tpm_cr50_i2c_write()
261 priv->buf[0] = addr; in tpm_cr50_i2c_write()
262 memcpy(priv->buf + 1, buffer, len); in tpm_cr50_i2c_write()
264 i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT); in tpm_cr50_i2c_write()
267 tpm_cr50_i2c_enable_tpm_irq(chip); in tpm_cr50_i2c_write()
270 rc = tpm_cr50_i2c_transfer_message(&chip->dev, client->adapter, &msg); in tpm_cr50_i2c_write()
274 /* Wait for TPM to be ready, ignore timeout */ in tpm_cr50_i2c_write()
275 tpm_cr50_i2c_wait_tpm_ready(chip); in tpm_cr50_i2c_write()
278 tpm_cr50_i2c_disable_tpm_irq(chip); in tpm_cr50_i2c_write()
279 i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT); in tpm_cr50_i2c_write()
288 * tpm_cr50_check_locality() - Verify TPM locality 0 is active.
289 * @chip: A TPM chip.
292 * - 0: Success.
293 * - -errno: A POSIX error code.
295 static int tpm_cr50_check_locality(struct tpm_chip *chip) in tpm_cr50_check_locality() argument
301 rc = tpm_cr50_i2c_read(chip, TPM_I2C_ACCESS(0), &buf, sizeof(buf)); in tpm_cr50_check_locality()
308 return -EIO; in tpm_cr50_check_locality()
312 * tpm_cr50_release_locality() - Release TPM locality.
313 * @chip: A TPM chip.
314 * @force: Flag to force release if set.
316 static void tpm_cr50_release_locality(struct tpm_chip *chip, bool force) in tpm_cr50_release_locality() argument
322 if (tpm_cr50_i2c_read(chip, addr, &buf, sizeof(buf)) < 0) in tpm_cr50_release_locality()
327 tpm_cr50_i2c_write(chip, addr, &buf, sizeof(buf)); in tpm_cr50_release_locality()
332 * tpm_cr50_request_locality() - Request TPM locality 0.
333 * @chip: A TPM chip.
336 * - 0: Success.
337 * - -errno: A POSIX error code.
339 static int tpm_cr50_request_locality(struct tpm_chip *chip) in tpm_cr50_request_locality() argument
345 if (!tpm_cr50_check_locality(chip)) in tpm_cr50_request_locality()
348 rc = tpm_cr50_i2c_write(chip, TPM_I2C_ACCESS(0), &buf, sizeof(buf)); in tpm_cr50_request_locality()
352 stop = jiffies + chip->timeout_a; in tpm_cr50_request_locality()
354 if (!tpm_cr50_check_locality(chip)) in tpm_cr50_request_locality()
360 return -ETIMEDOUT; in tpm_cr50_request_locality()
364 * tpm_cr50_i2c_tis_status() - Read cr50 tis status.
365 * @chip: A TPM chip.
367 * cr50 requires all 4 bytes of status register to be read.
372 static u8 tpm_cr50_i2c_tis_status(struct tpm_chip *chip) in tpm_cr50_i2c_tis_status() argument
376 if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(0), buf, sizeof(buf)) < 0) in tpm_cr50_i2c_tis_status()
383 * tpm_cr50_i2c_tis_set_ready() - Set status register to ready.
384 * @chip: A TPM chip.
386 * cr50 requires all 4 bytes of status register to be written.
388 static void tpm_cr50_i2c_tis_set_ready(struct tpm_chip *chip) in tpm_cr50_i2c_tis_set_ready() argument
392 tpm_cr50_i2c_write(chip, TPM_I2C_STS(0), buf, sizeof(buf)); in tpm_cr50_i2c_tis_set_ready()
397 * tpm_cr50_i2c_get_burst_and_status() - Get burst count and status.
398 * @chip: A TPM chip.
407 * - 0: Success.
408 * - -errno: A POSIX error code.
410 static int tpm_cr50_i2c_get_burst_and_status(struct tpm_chip *chip, u8 mask, in tpm_cr50_i2c_get_burst_and_status() argument
419 stop = jiffies + chip->timeout_b; in tpm_cr50_i2c_get_burst_and_status()
422 if (tpm_cr50_i2c_read(chip, TPM_I2C_STS(0), buf, sizeof(buf)) < 0) { in tpm_cr50_i2c_get_burst_and_status()
431 *burst > 0 && *burst <= TPM_CR50_MAX_BUFSIZE - 1) in tpm_cr50_i2c_get_burst_and_status()
437 dev_err(&chip->dev, "Timeout reading burst and status\n"); in tpm_cr50_i2c_get_burst_and_status()
438 return -ETIMEDOUT; in tpm_cr50_i2c_get_burst_and_status()
442 * tpm_cr50_i2c_tis_recv() - TPM reception callback.
443 * @chip: A TPM chip.
445 * @buf_len: Buffer length to read.
448 * - >= 0: Number of read bytes.
449 * - -errno: A POSIX error code.
451 static int tpm_cr50_i2c_tis_recv(struct tpm_chip *chip, u8 *buf, size_t buf_len) in tpm_cr50_i2c_tis_recv() argument
461 return -EINVAL; in tpm_cr50_i2c_tis_recv()
463 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status); in tpm_cr50_i2c_tis_recv()
468 dev_err(&chip->dev, in tpm_cr50_i2c_tis_recv()
471 rc = -EIO; in tpm_cr50_i2c_tis_recv()
476 rc = tpm_cr50_i2c_read(chip, addr, buf, burstcnt); in tpm_cr50_i2c_tis_recv()
478 dev_err(&chip->dev, "Read of first chunk failed\n"); in tpm_cr50_i2c_tis_recv()
485 dev_err(&chip->dev, "Buffer too small to receive i2c data\n"); in tpm_cr50_i2c_tis_recv()
486 rc = -E2BIG; in tpm_cr50_i2c_tis_recv()
494 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status); in tpm_cr50_i2c_tis_recv()
498 len = min_t(size_t, burstcnt, expected - cur); in tpm_cr50_i2c_tis_recv()
499 rc = tpm_cr50_i2c_read(chip, addr, buf + cur, len); in tpm_cr50_i2c_tis_recv()
501 dev_err(&chip->dev, "Read failed\n"); in tpm_cr50_i2c_tis_recv()
509 rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status); in tpm_cr50_i2c_tis_recv()
513 dev_err(&chip->dev, "Data still available\n"); in tpm_cr50_i2c_tis_recv()
514 rc = -EIO; in tpm_cr50_i2c_tis_recv()
518 tpm_cr50_release_locality(chip, false); in tpm_cr50_i2c_tis_recv()
523 if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY) in tpm_cr50_i2c_tis_recv()
524 tpm_cr50_i2c_tis_set_ready(chip); in tpm_cr50_i2c_tis_recv()
526 tpm_cr50_release_locality(chip, false); in tpm_cr50_i2c_tis_recv()
531 * tpm_cr50_i2c_tis_send() - TPM transmission callback.
532 * @chip: A TPM chip.
533 * @buf: Buffer to send.
537 * - 0: Success.
538 * - -errno: A POSIX error code.
540 static int tpm_cr50_i2c_tis_send(struct tpm_chip *chip, u8 *buf, size_t len) in tpm_cr50_i2c_tis_send() argument
548 rc = tpm_cr50_request_locality(chip); in tpm_cr50_i2c_tis_send()
553 stop = jiffies + chip->timeout_b; in tpm_cr50_i2c_tis_send()
554 while (!(tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) { in tpm_cr50_i2c_tis_send()
556 rc = -ETIMEDOUT; in tpm_cr50_i2c_tis_send()
560 tpm_cr50_i2c_tis_set_ready(chip); in tpm_cr50_i2c_tis_send()
571 rc = tpm_cr50_i2c_get_burst_and_status(chip, mask, &burstcnt, &status); in tpm_cr50_i2c_tis_send()
576 * Use burstcnt - 1 to account for the address byte in tpm_cr50_i2c_tis_send()
579 limit = min_t(size_t, burstcnt - 1, len); in tpm_cr50_i2c_tis_send()
580 rc = tpm_cr50_i2c_write(chip, TPM_I2C_DATA_FIFO(0), &buf[sent], limit); in tpm_cr50_i2c_tis_send()
582 dev_err(&chip->dev, "Write failed\n"); in tpm_cr50_i2c_tis_send()
587 len -= limit; in tpm_cr50_i2c_tis_send()
591 rc = tpm_cr50_i2c_get_burst_and_status(chip, TPM_STS_VALID, &burstcnt, &status); in tpm_cr50_i2c_tis_send()
595 dev_err(&chip->dev, "Data still expected\n"); in tpm_cr50_i2c_tis_send()
596 rc = -EIO; in tpm_cr50_i2c_tis_send()
601 rc = tpm_cr50_i2c_write(chip, TPM_I2C_STS(0), tpm_go, in tpm_cr50_i2c_tis_send()
604 dev_err(&chip->dev, "Start command failed\n"); in tpm_cr50_i2c_tis_send()
611 if (tpm_cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY) in tpm_cr50_i2c_tis_send()
612 tpm_cr50_i2c_tis_set_ready(chip); in tpm_cr50_i2c_tis_send()
614 tpm_cr50_release_locality(chip, false); in tpm_cr50_i2c_tis_send()
619 * tpm_cr50_i2c_req_canceled() - Callback to notify a request cancel.
620 * @chip: A TPM chip.
626 static bool tpm_cr50_i2c_req_canceled(struct tpm_chip *chip, u8 status) in tpm_cr50_i2c_req_canceled() argument
637 ret = device_property_read_u8(dev, "firmware-power-managed", &val); in tpm_cr50_i2c_is_firmware_power_managed()
672 * tpm_cr50_i2c_probe() - Driver probe function.
676 * - 0: Success.
677 * - -errno: A POSIX error code.
682 struct device *dev = &client->dev; in tpm_cr50_i2c_probe()
683 struct tpm_chip *chip; in tpm_cr50_i2c_probe() local
688 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) in tpm_cr50_i2c_probe()
689 return -ENODEV; in tpm_cr50_i2c_probe()
691 chip = tpmm_chip_alloc(dev, &cr50_i2c); in tpm_cr50_i2c_probe()
692 if (IS_ERR(chip)) in tpm_cr50_i2c_probe()
693 return PTR_ERR(chip); in tpm_cr50_i2c_probe()
697 return -ENOMEM; in tpm_cr50_i2c_probe()
699 /* cr50 is a TPM 2.0 chip */ in tpm_cr50_i2c_probe()
700 chip->flags |= TPM_CHIP_FLAG_TPM2; in tpm_cr50_i2c_probe()
702 chip->flags |= TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED; in tpm_cr50_i2c_probe()
705 chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); in tpm_cr50_i2c_probe()
706 chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); in tpm_cr50_i2c_probe()
707 chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); in tpm_cr50_i2c_probe()
708 chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); in tpm_cr50_i2c_probe()
710 dev_set_drvdata(&chip->dev, priv); in tpm_cr50_i2c_probe()
711 init_completion(&priv->tpm_ready); in tpm_cr50_i2c_probe()
713 if (client->irq > 0) { in tpm_cr50_i2c_probe()
714 rc = devm_request_irq(dev, client->irq, tpm_cr50_i2c_int_handler, in tpm_cr50_i2c_probe()
717 dev->driver->name, chip); in tpm_cr50_i2c_probe()
719 dev_err(dev, "Failed to probe IRQ %d\n", client->irq); in tpm_cr50_i2c_probe()
723 priv->irq = client->irq; in tpm_cr50_i2c_probe()
729 rc = tpm_cr50_request_locality(chip); in tpm_cr50_i2c_probe()
736 rc = tpm_cr50_i2c_read(chip, TPM_I2C_DID_VID(0), buf, sizeof(buf)); in tpm_cr50_i2c_probe()
739 tpm_cr50_release_locality(chip, true); in tpm_cr50_i2c_probe()
746 tpm_cr50_release_locality(chip, true); in tpm_cr50_i2c_probe()
747 return -ENODEV; in tpm_cr50_i2c_probe()
752 client->addr, client->irq, vendor >> 16); in tpm_cr50_i2c_probe()
753 return tpm_chip_register(chip); in tpm_cr50_i2c_probe()
757 * tpm_cr50_i2c_remove() - Driver remove function.
761 * - 0: Success.
762 * - -errno: A POSIX error code.
766 struct tpm_chip *chip = i2c_get_clientdata(client); in tpm_cr50_i2c_remove() local
767 struct device *dev = &client->dev; in tpm_cr50_i2c_remove()
769 if (!chip) { in tpm_cr50_i2c_remove()
774 tpm_chip_unregister(chip); in tpm_cr50_i2c_remove()
775 tpm_cr50_release_locality(chip, true); in tpm_cr50_i2c_remove()