1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2016 Google, Inc
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This device driver implements a TCG PTP FIFO interface over SPI for chips
6*4882a593Smuzhiyun * with Cr50 firmware.
7*4882a593Smuzhiyun * It is based on tpm_tis_spi driver by Peter Huewe and Christophe Ricard.
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/completion.h>
11*4882a593Smuzhiyun #include <linux/interrupt.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/of.h>
14*4882a593Smuzhiyun #include <linux/pm.h>
15*4882a593Smuzhiyun #include <linux/spi/spi.h>
16*4882a593Smuzhiyun #include <linux/wait.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "tpm_tis_core.h"
19*4882a593Smuzhiyun #include "tpm_tis_spi.h"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun * Cr50 timing constants:
23*4882a593Smuzhiyun * - can go to sleep not earlier than after CR50_SLEEP_DELAY_MSEC.
24*4882a593Smuzhiyun * - needs up to CR50_WAKE_START_DELAY_USEC to wake after sleep.
25*4882a593Smuzhiyun * - requires waiting for "ready" IRQ, if supported; or waiting for at least
26*4882a593Smuzhiyun * CR50_NOIRQ_ACCESS_DELAY_MSEC between transactions, if IRQ is not supported.
27*4882a593Smuzhiyun * - waits for up to CR50_FLOW_CONTROL for flow control 'ready' indication.
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun #define CR50_SLEEP_DELAY_MSEC 1000
30*4882a593Smuzhiyun #define CR50_WAKE_START_DELAY_USEC 1000
31*4882a593Smuzhiyun #define CR50_NOIRQ_ACCESS_DELAY msecs_to_jiffies(2)
32*4882a593Smuzhiyun #define CR50_READY_IRQ_TIMEOUT msecs_to_jiffies(TPM2_TIMEOUT_A)
33*4882a593Smuzhiyun #define CR50_FLOW_CONTROL msecs_to_jiffies(TPM2_TIMEOUT_A)
34*4882a593Smuzhiyun #define MAX_IRQ_CONFIRMATION_ATTEMPTS 3
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #define TPM_CR50_FW_VER(l) (0x0f90 | ((l) << 12))
37*4882a593Smuzhiyun #define TPM_CR50_MAX_FW_VER_LEN 64
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun struct cr50_spi_phy {
40*4882a593Smuzhiyun struct tpm_tis_spi_phy spi_phy;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun struct mutex time_track_mutex;
43*4882a593Smuzhiyun unsigned long last_access;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun unsigned long access_delay;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun unsigned int irq_confirmation_attempt;
48*4882a593Smuzhiyun bool irq_needs_confirmation;
49*4882a593Smuzhiyun bool irq_confirmed;
50*4882a593Smuzhiyun };
51*4882a593Smuzhiyun
to_cr50_spi_phy(struct tpm_tis_spi_phy * phy)52*4882a593Smuzhiyun static inline struct cr50_spi_phy *to_cr50_spi_phy(struct tpm_tis_spi_phy *phy)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun return container_of(phy, struct cr50_spi_phy, spi_phy);
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * The cr50 interrupt handler just signals waiting threads that the
59*4882a593Smuzhiyun * interrupt was asserted. It does not do any processing triggered
60*4882a593Smuzhiyun * by interrupts but is instead used to avoid fixed delays.
61*4882a593Smuzhiyun */
cr50_spi_irq_handler(int dummy,void * dev_id)62*4882a593Smuzhiyun static irqreturn_t cr50_spi_irq_handler(int dummy, void *dev_id)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun struct cr50_spi_phy *cr50_phy = dev_id;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun cr50_phy->irq_confirmed = true;
67*4882a593Smuzhiyun complete(&cr50_phy->spi_phy.ready);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun return IRQ_HANDLED;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun * Cr50 needs to have at least some delay between consecutive
74*4882a593Smuzhiyun * transactions. Make sure we wait.
75*4882a593Smuzhiyun */
cr50_ensure_access_delay(struct cr50_spi_phy * phy)76*4882a593Smuzhiyun static void cr50_ensure_access_delay(struct cr50_spi_phy *phy)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun unsigned long allowed_access = phy->last_access + phy->access_delay;
79*4882a593Smuzhiyun unsigned long time_now = jiffies;
80*4882a593Smuzhiyun struct device *dev = &phy->spi_phy.spi_device->dev;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun * Note: There is a small chance, if Cr50 is not accessed in a few days,
84*4882a593Smuzhiyun * that time_in_range will not provide the correct result after the wrap
85*4882a593Smuzhiyun * around for jiffies. In this case, we'll have an unneeded short delay,
86*4882a593Smuzhiyun * which is fine.
87*4882a593Smuzhiyun */
88*4882a593Smuzhiyun if (time_in_range_open(time_now, phy->last_access, allowed_access)) {
89*4882a593Smuzhiyun unsigned long remaining, timeout = allowed_access - time_now;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun remaining = wait_for_completion_timeout(&phy->spi_phy.ready,
92*4882a593Smuzhiyun timeout);
93*4882a593Smuzhiyun if (!remaining && phy->irq_confirmed)
94*4882a593Smuzhiyun dev_warn(dev, "Timeout waiting for TPM ready IRQ\n");
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun if (phy->irq_needs_confirmation) {
98*4882a593Smuzhiyun unsigned int attempt = ++phy->irq_confirmation_attempt;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun if (phy->irq_confirmed) {
101*4882a593Smuzhiyun phy->irq_needs_confirmation = false;
102*4882a593Smuzhiyun phy->access_delay = CR50_READY_IRQ_TIMEOUT;
103*4882a593Smuzhiyun dev_info(dev, "TPM ready IRQ confirmed on attempt %u\n",
104*4882a593Smuzhiyun attempt);
105*4882a593Smuzhiyun } else if (attempt > MAX_IRQ_CONFIRMATION_ATTEMPTS) {
106*4882a593Smuzhiyun phy->irq_needs_confirmation = false;
107*4882a593Smuzhiyun dev_warn(dev, "IRQ not confirmed - will use delays\n");
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /*
113*4882a593Smuzhiyun * Cr50 might go to sleep if there is no SPI activity for some time and
114*4882a593Smuzhiyun * miss the first few bits/bytes on the bus. In such case, wake it up
115*4882a593Smuzhiyun * by asserting CS and give it time to start up.
116*4882a593Smuzhiyun */
cr50_needs_waking(struct cr50_spi_phy * phy)117*4882a593Smuzhiyun static bool cr50_needs_waking(struct cr50_spi_phy *phy)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * Note: There is a small chance, if Cr50 is not accessed in a few days,
121*4882a593Smuzhiyun * that time_in_range will not provide the correct result after the wrap
122*4882a593Smuzhiyun * around for jiffies. In this case, we'll probably timeout or read
123*4882a593Smuzhiyun * incorrect value from TPM_STS and just retry the operation.
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun return !time_in_range_open(jiffies, phy->last_access,
126*4882a593Smuzhiyun phy->spi_phy.wake_after);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
cr50_wake_if_needed(struct cr50_spi_phy * cr50_phy)129*4882a593Smuzhiyun static void cr50_wake_if_needed(struct cr50_spi_phy *cr50_phy)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct tpm_tis_spi_phy *phy = &cr50_phy->spi_phy;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (cr50_needs_waking(cr50_phy)) {
134*4882a593Smuzhiyun /* Assert CS, wait 1 msec, deassert CS */
135*4882a593Smuzhiyun struct spi_transfer spi_cs_wake = {
136*4882a593Smuzhiyun .delay = {
137*4882a593Smuzhiyun .value = 1000,
138*4882a593Smuzhiyun .unit = SPI_DELAY_UNIT_USECS
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun };
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun spi_sync_transfer(phy->spi_device, &spi_cs_wake, 1);
143*4882a593Smuzhiyun /* Wait for it to fully wake */
144*4882a593Smuzhiyun usleep_range(CR50_WAKE_START_DELAY_USEC,
145*4882a593Smuzhiyun CR50_WAKE_START_DELAY_USEC * 2);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* Reset the time when we need to wake Cr50 again */
149*4882a593Smuzhiyun phy->wake_after = jiffies + msecs_to_jiffies(CR50_SLEEP_DELAY_MSEC);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun * Flow control: clock the bus and wait for cr50 to set LSB before
154*4882a593Smuzhiyun * sending/receiving data. TCG PTP spec allows it to happen during
155*4882a593Smuzhiyun * the last byte of header, but cr50 never does that in practice,
156*4882a593Smuzhiyun * and earlier versions had a bug when it was set too early, so don't
157*4882a593Smuzhiyun * check for it during header transfer.
158*4882a593Smuzhiyun */
cr50_spi_flow_control(struct tpm_tis_spi_phy * phy,struct spi_transfer * spi_xfer)159*4882a593Smuzhiyun static int cr50_spi_flow_control(struct tpm_tis_spi_phy *phy,
160*4882a593Smuzhiyun struct spi_transfer *spi_xfer)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun struct device *dev = &phy->spi_device->dev;
163*4882a593Smuzhiyun unsigned long timeout = jiffies + CR50_FLOW_CONTROL;
164*4882a593Smuzhiyun struct spi_message m;
165*4882a593Smuzhiyun int ret;
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun spi_xfer->len = 1;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun do {
170*4882a593Smuzhiyun spi_message_init(&m);
171*4882a593Smuzhiyun spi_message_add_tail(spi_xfer, &m);
172*4882a593Smuzhiyun ret = spi_sync_locked(phy->spi_device, &m);
173*4882a593Smuzhiyun if (ret < 0)
174*4882a593Smuzhiyun return ret;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun if (time_after(jiffies, timeout)) {
177*4882a593Smuzhiyun dev_warn(dev, "Timeout during flow control\n");
178*4882a593Smuzhiyun return -EBUSY;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun } while (!(phy->iobuf[0] & 0x01));
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun return 0;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
tpm_tis_spi_cr50_transfer(struct tpm_tis_data * data,u32 addr,u16 len,u8 * in,const u8 * out)185*4882a593Smuzhiyun static int tpm_tis_spi_cr50_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
186*4882a593Smuzhiyun u8 *in, const u8 *out)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
189*4882a593Smuzhiyun struct cr50_spi_phy *cr50_phy = to_cr50_spi_phy(phy);
190*4882a593Smuzhiyun int ret;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun mutex_lock(&cr50_phy->time_track_mutex);
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun * Do this outside of spi_bus_lock in case cr50 is not the
195*4882a593Smuzhiyun * only device on that spi bus.
196*4882a593Smuzhiyun */
197*4882a593Smuzhiyun cr50_ensure_access_delay(cr50_phy);
198*4882a593Smuzhiyun cr50_wake_if_needed(cr50_phy);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun ret = tpm_tis_spi_transfer(data, addr, len, in, out);
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun cr50_phy->last_access = jiffies;
203*4882a593Smuzhiyun mutex_unlock(&cr50_phy->time_track_mutex);
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun return ret;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
tpm_tis_spi_cr50_read_bytes(struct tpm_tis_data * data,u32 addr,u16 len,u8 * result)208*4882a593Smuzhiyun static int tpm_tis_spi_cr50_read_bytes(struct tpm_tis_data *data, u32 addr,
209*4882a593Smuzhiyun u16 len, u8 *result)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun return tpm_tis_spi_cr50_transfer(data, addr, len, result, NULL);
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
tpm_tis_spi_cr50_write_bytes(struct tpm_tis_data * data,u32 addr,u16 len,const u8 * value)214*4882a593Smuzhiyun static int tpm_tis_spi_cr50_write_bytes(struct tpm_tis_data *data, u32 addr,
215*4882a593Smuzhiyun u16 len, const u8 *value)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun return tpm_tis_spi_cr50_transfer(data, addr, len, NULL, value);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun static const struct tpm_tis_phy_ops tpm_spi_cr50_phy_ops = {
221*4882a593Smuzhiyun .read_bytes = tpm_tis_spi_cr50_read_bytes,
222*4882a593Smuzhiyun .write_bytes = tpm_tis_spi_cr50_write_bytes,
223*4882a593Smuzhiyun .read16 = tpm_tis_spi_read16,
224*4882a593Smuzhiyun .read32 = tpm_tis_spi_read32,
225*4882a593Smuzhiyun .write32 = tpm_tis_spi_write32,
226*4882a593Smuzhiyun };
227*4882a593Smuzhiyun
cr50_print_fw_version(struct tpm_tis_data * data)228*4882a593Smuzhiyun static void cr50_print_fw_version(struct tpm_tis_data *data)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
231*4882a593Smuzhiyun int i, len = 0;
232*4882a593Smuzhiyun char fw_ver[TPM_CR50_MAX_FW_VER_LEN + 1];
233*4882a593Smuzhiyun char fw_ver_block[4];
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /*
236*4882a593Smuzhiyun * Write anything to TPM_CR50_FW_VER to start from the beginning
237*4882a593Smuzhiyun * of the version string
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun tpm_tis_write8(data, TPM_CR50_FW_VER(data->locality), 0);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /* Read the string, 4 bytes at a time, until we get '\0' */
242*4882a593Smuzhiyun do {
243*4882a593Smuzhiyun tpm_tis_read_bytes(data, TPM_CR50_FW_VER(data->locality), 4,
244*4882a593Smuzhiyun fw_ver_block);
245*4882a593Smuzhiyun for (i = 0; i < 4 && fw_ver_block[i]; ++len, ++i)
246*4882a593Smuzhiyun fw_ver[len] = fw_ver_block[i];
247*4882a593Smuzhiyun } while (i == 4 && len < TPM_CR50_MAX_FW_VER_LEN);
248*4882a593Smuzhiyun fw_ver[len] = '\0';
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun dev_info(&phy->spi_device->dev, "Cr50 firmware version: %s\n", fw_ver);
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
cr50_spi_probe(struct spi_device * spi)253*4882a593Smuzhiyun int cr50_spi_probe(struct spi_device *spi)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun struct tpm_tis_spi_phy *phy;
256*4882a593Smuzhiyun struct cr50_spi_phy *cr50_phy;
257*4882a593Smuzhiyun int ret;
258*4882a593Smuzhiyun struct tpm_chip *chip;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun cr50_phy = devm_kzalloc(&spi->dev, sizeof(*cr50_phy), GFP_KERNEL);
261*4882a593Smuzhiyun if (!cr50_phy)
262*4882a593Smuzhiyun return -ENOMEM;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun phy = &cr50_phy->spi_phy;
265*4882a593Smuzhiyun phy->flow_control = cr50_spi_flow_control;
266*4882a593Smuzhiyun phy->wake_after = jiffies;
267*4882a593Smuzhiyun init_completion(&phy->ready);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun cr50_phy->access_delay = CR50_NOIRQ_ACCESS_DELAY;
270*4882a593Smuzhiyun cr50_phy->last_access = jiffies;
271*4882a593Smuzhiyun mutex_init(&cr50_phy->time_track_mutex);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun if (spi->irq > 0) {
274*4882a593Smuzhiyun ret = devm_request_irq(&spi->dev, spi->irq,
275*4882a593Smuzhiyun cr50_spi_irq_handler,
276*4882a593Smuzhiyun IRQF_TRIGGER_RISING | IRQF_ONESHOT,
277*4882a593Smuzhiyun "cr50_spi", cr50_phy);
278*4882a593Smuzhiyun if (ret < 0) {
279*4882a593Smuzhiyun if (ret == -EPROBE_DEFER)
280*4882a593Smuzhiyun return ret;
281*4882a593Smuzhiyun dev_warn(&spi->dev, "Requesting IRQ %d failed: %d\n",
282*4882a593Smuzhiyun spi->irq, ret);
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun * This is not fatal, the driver will fall back to
285*4882a593Smuzhiyun * delays automatically, since ready will never
286*4882a593Smuzhiyun * be completed without a registered irq handler.
287*4882a593Smuzhiyun * So, just fall through.
288*4882a593Smuzhiyun */
289*4882a593Smuzhiyun } else {
290*4882a593Smuzhiyun /*
291*4882a593Smuzhiyun * IRQ requested, let's verify that it is actually
292*4882a593Smuzhiyun * triggered, before relying on it.
293*4882a593Smuzhiyun */
294*4882a593Smuzhiyun cr50_phy->irq_needs_confirmation = true;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun } else {
297*4882a593Smuzhiyun dev_warn(&spi->dev,
298*4882a593Smuzhiyun "No IRQ - will use delays between transactions.\n");
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun ret = tpm_tis_spi_init(spi, phy, -1, &tpm_spi_cr50_phy_ops);
302*4882a593Smuzhiyun if (ret)
303*4882a593Smuzhiyun return ret;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun cr50_print_fw_version(&phy->priv);
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun chip = dev_get_drvdata(&spi->dev);
308*4882a593Smuzhiyun chip->flags |= TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun return 0;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
tpm_tis_spi_resume(struct device * dev)314*4882a593Smuzhiyun int tpm_tis_spi_resume(struct device *dev)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun struct tpm_chip *chip = dev_get_drvdata(dev);
317*4882a593Smuzhiyun struct tpm_tis_data *data = dev_get_drvdata(&chip->dev);
318*4882a593Smuzhiyun struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
319*4882a593Smuzhiyun /*
320*4882a593Smuzhiyun * Jiffies not increased during suspend, so we need to reset
321*4882a593Smuzhiyun * the time to wake Cr50 after resume.
322*4882a593Smuzhiyun */
323*4882a593Smuzhiyun phy->wake_after = jiffies;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun return tpm_tis_resume(dev);
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun #endif
328