blob: f74f451baf6ae039a8599b8a14da4ee398e387d1 [file] [log] [blame]
Jason Gunthorpe82cc1a42016-07-12 11:41:50 -06001 /******************************************************************************
2 * Nuvoton TPM I2C Device Driver Interface for WPCT301/NPCT501/NPCT6XX,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -06003 * based on the TCG TPM Interface Spec version 1.2.
4 * Specifications at www.trustedcomputinggroup.org
5 *
6 * Copyright (C) 2011, Nuvoton Technology Corporation.
7 * Dan Morav <dan.morav@nuvoton.com>
8 * Copyright (C) 2013, Obsidian Research Corp.
9 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/>.
23 *
24 * Nuvoton contact information: APC.Support@nuvoton.com
25 *****************************************************************************/
26
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/slab.h>
31#include <linux/interrupt.h>
32#include <linux/wait.h>
33#include <linux/i2c.h>
Jason Gunthorpe82cc1a42016-07-12 11:41:50 -060034#include <linux/of_device.h>
Jason Gunthorpe4c336e42013-10-06 12:43:13 -060035#include "tpm.h"
36
37/* I2C interface offsets */
38#define TPM_STS 0x00
39#define TPM_BURST_COUNT 0x01
40#define TPM_DATA_FIFO_W 0x20
41#define TPM_DATA_FIFO_R 0x40
42#define TPM_VID_DID_RID 0x60
43/* TPM command header size */
44#define TPM_HEADER_SIZE 10
45#define TPM_RETRY 5
46/*
47 * I2C bus device maximum buffer size w/o counting I2C address or command
48 * i.e. max size required for I2C write is 34 = addr, command, 32 bytes data
49 */
50#define TPM_I2C_MAX_BUF_SIZE 32
51#define TPM_I2C_RETRY_COUNT 32
Nayna Jaina233a022017-03-10 13:45:53 -050052#define TPM_I2C_BUS_DELAY 1000 /* usec */
53#define TPM_I2C_RETRY_DELAY_SHORT (2 * 1000) /* usec */
54#define TPM_I2C_RETRY_DELAY_LONG (10 * 1000) /* usec */
55#define TPM_I2C_DELAY_RANGE 300 /* usec */
Jason Gunthorpe4c336e42013-10-06 12:43:13 -060056
Jason Gunthorpe82cc1a42016-07-12 11:41:50 -060057#define OF_IS_TPM2 ((void *)1)
58#define I2C_IS_TPM2 1
Jason Gunthorpe4c336e42013-10-06 12:43:13 -060059
60struct priv_data {
Christophe Ricard570a3602016-03-31 22:56:56 +020061 int irq;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -060062 unsigned int intrs;
Christophe Ricard6e599f62016-03-31 22:56:57 +020063 wait_queue_head_t read_queue;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -060064};
65
66static s32 i2c_nuvoton_read_buf(struct i2c_client *client, u8 offset, u8 size,
67 u8 *data)
68{
69 s32 status;
70
71 status = i2c_smbus_read_i2c_block_data(client, offset, size, data);
72 dev_dbg(&client->dev,
73 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
74 offset, size, (int)size, data, status);
75 return status;
76}
77
78static s32 i2c_nuvoton_write_buf(struct i2c_client *client, u8 offset, u8 size,
79 u8 *data)
80{
81 s32 status;
82
83 status = i2c_smbus_write_i2c_block_data(client, offset, size, data);
84 dev_dbg(&client->dev,
85 "%s(offset=%u size=%u data=%*ph) -> sts=%d\n", __func__,
86 offset, size, (int)size, data, status);
87 return status;
88}
89
90#define TPM_STS_VALID 0x80
91#define TPM_STS_COMMAND_READY 0x40
92#define TPM_STS_GO 0x20
93#define TPM_STS_DATA_AVAIL 0x10
94#define TPM_STS_EXPECT 0x08
95#define TPM_STS_RESPONSE_RETRY 0x02
96#define TPM_STS_ERR_VAL 0x07 /* bit2...bit0 reads always 0 */
97
98#define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
99#define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
100
101/* read TPM_STS register */
102static u8 i2c_nuvoton_read_status(struct tpm_chip *chip)
103{
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500104 struct i2c_client *client = to_i2c_client(chip->dev.parent);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600105 s32 status;
106 u8 data;
107
108 status = i2c_nuvoton_read_buf(client, TPM_STS, 1, &data);
109 if (status <= 0) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500110 dev_err(&chip->dev, "%s() error return %d\n", __func__,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600111 status);
112 data = TPM_STS_ERR_VAL;
113 }
114
115 return data;
116}
117
118/* write byte to TPM_STS register */
119static s32 i2c_nuvoton_write_status(struct i2c_client *client, u8 data)
120{
121 s32 status;
122 int i;
123
124 /* this causes the current command to be aborted */
125 for (i = 0, status = -1; i < TPM_I2C_RETRY_COUNT && status < 0; i++) {
126 status = i2c_nuvoton_write_buf(client, TPM_STS, 1, &data);
Nayna Jain0afb7112017-03-10 13:45:54 -0500127 if (status < 0)
128 usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY
129 + TPM_I2C_DELAY_RANGE);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600130 }
131 return status;
132}
133
134/* write commandReady to TPM_STS register */
135static void i2c_nuvoton_ready(struct tpm_chip *chip)
136{
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500137 struct i2c_client *client = to_i2c_client(chip->dev.parent);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600138 s32 status;
139
140 /* this causes the current command to be aborted */
141 status = i2c_nuvoton_write_status(client, TPM_STS_COMMAND_READY);
142 if (status < 0)
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500143 dev_err(&chip->dev,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600144 "%s() fail to write TPM_STS.commandReady\n", __func__);
145}
146
147/* read burstCount field from TPM_STS register
148 * return -1 on fail to read */
149static int i2c_nuvoton_get_burstcount(struct i2c_client *client,
150 struct tpm_chip *chip)
151{
Christophe Ricardaf782f32016-03-31 22:56:59 +0200152 unsigned long stop = jiffies + chip->timeout_d;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600153 s32 status;
154 int burst_count = -1;
155 u8 data;
156
157 /* wait for burstcount to be non-zero */
158 do {
159 /* in I2C burstCount is 1 byte */
160 status = i2c_nuvoton_read_buf(client, TPM_BURST_COUNT, 1,
161 &data);
162 if (status > 0 && data > 0) {
163 burst_count = min_t(u8, TPM_I2C_MAX_BUF_SIZE, data);
164 break;
165 }
Nayna Jaina233a022017-03-10 13:45:53 -0500166 usleep_range(TPM_I2C_BUS_DELAY, TPM_I2C_BUS_DELAY
167 + TPM_I2C_DELAY_RANGE);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600168 } while (time_before(jiffies, stop));
169
170 return burst_count;
171}
172
173/*
Jason Gunthorpe82cc1a42016-07-12 11:41:50 -0600174 * WPCT301/NPCT501/NPCT6XX SINT# supports only dataAvail
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600175 * any call to this function which is not waiting for dataAvail will
176 * set queue to NULL to avoid waiting for interrupt
177 */
178static bool i2c_nuvoton_check_status(struct tpm_chip *chip, u8 mask, u8 value)
179{
180 u8 status = i2c_nuvoton_read_status(chip);
181 return (status != TPM_STS_ERR_VAL) && ((status & mask) == value);
182}
183
184static int i2c_nuvoton_wait_for_stat(struct tpm_chip *chip, u8 mask, u8 value,
185 u32 timeout, wait_queue_head_t *queue)
186{
Christophe Ricard570a3602016-03-31 22:56:56 +0200187 if ((chip->flags & TPM_CHIP_FLAG_IRQ) && queue) {
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600188 s32 rc;
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200189 struct priv_data *priv = dev_get_drvdata(&chip->dev);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600190 unsigned int cur_intrs = priv->intrs;
191
Christophe Ricard570a3602016-03-31 22:56:56 +0200192 enable_irq(priv->irq);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600193 rc = wait_event_interruptible_timeout(*queue,
194 cur_intrs != priv->intrs,
195 timeout);
196 if (rc > 0)
197 return 0;
198 /* At this point we know that the SINT pin is asserted, so we
199 * do not need to do i2c_nuvoton_check_status */
200 } else {
201 unsigned long ten_msec, stop;
202 bool status_valid;
203
204 /* check current status */
205 status_valid = i2c_nuvoton_check_status(chip, mask, value);
206 if (status_valid)
207 return 0;
208
209 /* use polling to wait for the event */
Nayna Jaina233a022017-03-10 13:45:53 -0500210 ten_msec = jiffies + usecs_to_jiffies(TPM_I2C_RETRY_DELAY_LONG);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600211 stop = jiffies + timeout;
212 do {
213 if (time_before(jiffies, ten_msec))
Nayna Jaina233a022017-03-10 13:45:53 -0500214 usleep_range(TPM_I2C_RETRY_DELAY_SHORT,
215 TPM_I2C_RETRY_DELAY_SHORT
216 + TPM_I2C_DELAY_RANGE);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600217 else
Nayna Jaina233a022017-03-10 13:45:53 -0500218 usleep_range(TPM_I2C_RETRY_DELAY_LONG,
219 TPM_I2C_RETRY_DELAY_LONG
220 + TPM_I2C_DELAY_RANGE);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600221 status_valid = i2c_nuvoton_check_status(chip, mask,
222 value);
223 if (status_valid)
224 return 0;
225 } while (time_before(jiffies, stop));
226 }
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500227 dev_err(&chip->dev, "%s(%02x, %02x) -> timeout\n", __func__, mask,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600228 value);
229 return -ETIMEDOUT;
230}
231
232/* wait for dataAvail field to be set in the TPM_STS register */
233static int i2c_nuvoton_wait_for_data_avail(struct tpm_chip *chip, u32 timeout,
234 wait_queue_head_t *queue)
235{
236 return i2c_nuvoton_wait_for_stat(chip,
237 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
238 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
239 timeout, queue);
240}
241
242/* Read @count bytes into @buf from TPM_RD_FIFO register */
243static int i2c_nuvoton_recv_data(struct i2c_client *client,
244 struct tpm_chip *chip, u8 *buf, size_t count)
245{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200246 struct priv_data *priv = dev_get_drvdata(&chip->dev);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600247 s32 rc;
248 int burst_count, bytes2read, size = 0;
249
250 while (size < count &&
251 i2c_nuvoton_wait_for_data_avail(chip,
Christophe Ricardaf782f32016-03-31 22:56:59 +0200252 chip->timeout_c,
Christophe Ricard6e599f62016-03-31 22:56:57 +0200253 &priv->read_queue) == 0) {
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600254 burst_count = i2c_nuvoton_get_burstcount(client, chip);
255 if (burst_count < 0) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500256 dev_err(&chip->dev,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600257 "%s() fail to read burstCount=%d\n", __func__,
258 burst_count);
259 return -EIO;
260 }
261 bytes2read = min_t(size_t, burst_count, count - size);
262 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_R,
263 bytes2read, &buf[size]);
264 if (rc < 0) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500265 dev_err(&chip->dev,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600266 "%s() fail on i2c_nuvoton_read_buf()=%d\n",
267 __func__, rc);
268 return -EIO;
269 }
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500270 dev_dbg(&chip->dev, "%s(%d):", __func__, bytes2read);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600271 size += bytes2read;
272 }
273
274 return size;
275}
276
277/* Read TPM command results */
278static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
279{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200280 struct priv_data *priv = dev_get_drvdata(&chip->dev);
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500281 struct device *dev = chip->dev.parent;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600282 struct i2c_client *client = to_i2c_client(dev);
283 s32 rc;
Jeremy Boone37dfbcc2018-02-08 12:31:16 -0800284 int status;
285 int burst_count;
286 int retries;
287 int size = 0;
288 u32 expected;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600289
290 if (count < TPM_HEADER_SIZE) {
291 i2c_nuvoton_ready(chip); /* return to idle */
292 dev_err(dev, "%s() count < header size\n", __func__);
293 return -EIO;
294 }
295 for (retries = 0; retries < TPM_RETRY; retries++) {
296 if (retries > 0) {
297 /* if this is not the first trial, set responseRetry */
298 i2c_nuvoton_write_status(client,
299 TPM_STS_RESPONSE_RETRY);
300 }
301 /*
302 * read first available (> 10 bytes), including:
303 * tag, paramsize, and result
304 */
305 status = i2c_nuvoton_wait_for_data_avail(
Christophe Ricardaf782f32016-03-31 22:56:59 +0200306 chip, chip->timeout_c, &priv->read_queue);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600307 if (status != 0) {
308 dev_err(dev, "%s() timeout on dataAvail\n", __func__);
309 size = -ETIMEDOUT;
310 continue;
311 }
312 burst_count = i2c_nuvoton_get_burstcount(client, chip);
313 if (burst_count < 0) {
314 dev_err(dev, "%s() fail to get burstCount\n", __func__);
315 size = -EIO;
316 continue;
317 }
318 size = i2c_nuvoton_recv_data(client, chip, buf,
319 burst_count);
320 if (size < TPM_HEADER_SIZE) {
321 dev_err(dev, "%s() fail to read header\n", __func__);
322 size = -EIO;
323 continue;
324 }
325 /*
326 * convert number of expected bytes field from big endian 32 bit
327 * to machine native
328 */
329 expected = be32_to_cpu(*(__be32 *) (buf + 2));
Jeremy Boone37dfbcc2018-02-08 12:31:16 -0800330 if (expected > count || expected < size) {
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600331 dev_err(dev, "%s() expected > count\n", __func__);
332 size = -EIO;
333 continue;
334 }
335 rc = i2c_nuvoton_recv_data(client, chip, &buf[size],
336 expected - size);
337 size += rc;
338 if (rc < 0 || size < expected) {
339 dev_err(dev, "%s() fail to read remainder of result\n",
340 __func__);
341 size = -EIO;
342 continue;
343 }
344 if (i2c_nuvoton_wait_for_stat(
345 chip, TPM_STS_VALID | TPM_STS_DATA_AVAIL,
Christophe Ricardaf782f32016-03-31 22:56:59 +0200346 TPM_STS_VALID, chip->timeout_c,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600347 NULL)) {
348 dev_err(dev, "%s() error left over data\n", __func__);
349 size = -ETIMEDOUT;
350 continue;
351 }
352 break;
353 }
354 i2c_nuvoton_ready(chip);
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500355 dev_dbg(&chip->dev, "%s() -> %d\n", __func__, size);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600356 return size;
357}
358
359/*
360 * Send TPM command.
361 *
362 * If interrupts are used (signaled by an irq set in the vendor structure)
363 * tpm.c can skip polling for the data to be available as the interrupt is
364 * waited for here
365 */
366static int i2c_nuvoton_send(struct tpm_chip *chip, u8 *buf, size_t len)
367{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200368 struct priv_data *priv = dev_get_drvdata(&chip->dev);
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500369 struct device *dev = chip->dev.parent;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600370 struct i2c_client *client = to_i2c_client(dev);
371 u32 ordinal;
Tomas Winklerc85a71f2018-10-19 21:22:47 +0300372 unsigned long duration;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600373 size_t count = 0;
374 int burst_count, bytes2write, retries, rc = -EIO;
375
376 for (retries = 0; retries < TPM_RETRY; retries++) {
377 i2c_nuvoton_ready(chip);
378 if (i2c_nuvoton_wait_for_stat(chip, TPM_STS_COMMAND_READY,
379 TPM_STS_COMMAND_READY,
Christophe Ricardaf782f32016-03-31 22:56:59 +0200380 chip->timeout_b, NULL)) {
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600381 dev_err(dev, "%s() timeout on commandReady\n",
382 __func__);
383 rc = -EIO;
384 continue;
385 }
386 rc = 0;
387 while (count < len - 1) {
388 burst_count = i2c_nuvoton_get_burstcount(client,
389 chip);
390 if (burst_count < 0) {
391 dev_err(dev, "%s() fail get burstCount\n",
392 __func__);
393 rc = -EIO;
394 break;
395 }
396 bytes2write = min_t(size_t, burst_count,
397 len - 1 - count);
398 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W,
399 bytes2write, &buf[count]);
400 if (rc < 0) {
401 dev_err(dev, "%s() fail i2cWriteBuf\n",
402 __func__);
403 break;
404 }
405 dev_dbg(dev, "%s(%d):", __func__, bytes2write);
406 count += bytes2write;
407 rc = i2c_nuvoton_wait_for_stat(chip,
408 TPM_STS_VALID |
409 TPM_STS_EXPECT,
410 TPM_STS_VALID |
411 TPM_STS_EXPECT,
Christophe Ricardaf782f32016-03-31 22:56:59 +0200412 chip->timeout_c,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600413 NULL);
414 if (rc < 0) {
415 dev_err(dev, "%s() timeout on Expect\n",
416 __func__);
417 rc = -ETIMEDOUT;
418 break;
419 }
420 }
421 if (rc < 0)
422 continue;
423
424 /* write last byte */
425 rc = i2c_nuvoton_write_buf(client, TPM_DATA_FIFO_W, 1,
426 &buf[count]);
427 if (rc < 0) {
428 dev_err(dev, "%s() fail to write last byte\n",
429 __func__);
430 rc = -EIO;
431 continue;
432 }
433 dev_dbg(dev, "%s(last): %02x", __func__, buf[count]);
434 rc = i2c_nuvoton_wait_for_stat(chip,
435 TPM_STS_VALID | TPM_STS_EXPECT,
436 TPM_STS_VALID,
Christophe Ricardaf782f32016-03-31 22:56:59 +0200437 chip->timeout_c, NULL);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600438 if (rc) {
439 dev_err(dev, "%s() timeout on Expect to clear\n",
440 __func__);
441 rc = -ETIMEDOUT;
442 continue;
443 }
444 break;
445 }
446 if (rc < 0) {
447 /* retries == TPM_RETRY */
448 i2c_nuvoton_ready(chip);
449 return rc;
450 }
451 /* execute the TPM command */
452 rc = i2c_nuvoton_write_status(client, TPM_STS_GO);
453 if (rc < 0) {
454 dev_err(dev, "%s() fail to write Go\n", __func__);
455 i2c_nuvoton_ready(chip);
456 return rc;
457 }
458 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
Tomas Winklerc85a71f2018-10-19 21:22:47 +0300459 if (chip->flags & TPM_CHIP_FLAG_TPM2)
460 duration = tpm2_calc_ordinal_duration(chip, ordinal);
461 else
462 duration = tpm_calc_ordinal_duration(chip, ordinal);
463
464 rc = i2c_nuvoton_wait_for_data_avail(chip, duration, &priv->read_queue);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600465 if (rc) {
466 dev_err(dev, "%s() timeout command duration\n", __func__);
467 i2c_nuvoton_ready(chip);
468 return rc;
469 }
470
471 dev_dbg(dev, "%s() -> %zd\n", __func__, len);
472 return len;
473}
474
475static bool i2c_nuvoton_req_canceled(struct tpm_chip *chip, u8 status)
476{
477 return (status == TPM_STS_COMMAND_READY);
478}
479
Jason Gunthorpe01ad1fa2013-11-26 13:30:43 -0700480static const struct tpm_class_ops tpm_i2c = {
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600481 .flags = TPM_OPS_AUTO_STARTUP,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600482 .status = i2c_nuvoton_read_status,
483 .recv = i2c_nuvoton_recv,
484 .send = i2c_nuvoton_send,
485 .cancel = i2c_nuvoton_ready,
486 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
487 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
488 .req_canceled = i2c_nuvoton_req_canceled,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600489};
490
491/* The only purpose for the handler is to signal to any waiting threads that
492 * the interrupt is currently being asserted. The driver does not do any
493 * processing triggered by interrupts, and the chip provides no way to mask at
494 * the source (plus that would be slow over I2C). Run the IRQ as a one-shot,
495 * this means it cannot be shared. */
496static irqreturn_t i2c_nuvoton_int_handler(int dummy, void *dev_id)
497{
498 struct tpm_chip *chip = dev_id;
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200499 struct priv_data *priv = dev_get_drvdata(&chip->dev);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600500
501 priv->intrs++;
Christophe Ricard6e599f62016-03-31 22:56:57 +0200502 wake_up(&priv->read_queue);
Christophe Ricard570a3602016-03-31 22:56:56 +0200503 disable_irq_nosync(priv->irq);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600504 return IRQ_HANDLED;
505}
506
507static int get_vid(struct i2c_client *client, u32 *res)
508{
509 static const u8 vid_did_rid_value[] = { 0x50, 0x10, 0xfe };
510 u32 temp;
511 s32 rc;
512
513 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
514 return -ENODEV;
515 rc = i2c_nuvoton_read_buf(client, TPM_VID_DID_RID, 4, (u8 *)&temp);
516 if (rc < 0)
517 return rc;
518
519 /* check WPCT301 values - ignore RID */
520 if (memcmp(&temp, vid_did_rid_value, sizeof(vid_did_rid_value))) {
521 /*
522 * f/w rev 2.81 has an issue where the VID_DID_RID is not
523 * reporting the right value. so give it another chance at
524 * offset 0x20 (FIFO_W).
525 */
526 rc = i2c_nuvoton_read_buf(client, TPM_DATA_FIFO_W, 4,
527 (u8 *) (&temp));
528 if (rc < 0)
529 return rc;
530
531 /* check WPCT301 values - ignore RID */
532 if (memcmp(&temp, vid_did_rid_value,
533 sizeof(vid_did_rid_value)))
534 return -ENODEV;
535 }
536
537 *res = temp;
538 return 0;
539}
540
541static int i2c_nuvoton_probe(struct i2c_client *client,
542 const struct i2c_device_id *id)
543{
544 int rc;
545 struct tpm_chip *chip;
546 struct device *dev = &client->dev;
Christophe Ricard570a3602016-03-31 22:56:56 +0200547 struct priv_data *priv;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600548 u32 vid = 0;
549
550 rc = get_vid(client, &vid);
551 if (rc)
552 return rc;
553
554 dev_info(dev, "VID: %04X DID: %02X RID: %02X\n", (u16) vid,
555 (u8) (vid >> 16), (u8) (vid >> 24));
556
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800557 chip = tpmm_chip_alloc(dev, &tpm_i2c);
558 if (IS_ERR(chip))
559 return PTR_ERR(chip);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600560
Christophe Ricard570a3602016-03-31 22:56:56 +0200561 priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
562 if (!priv)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800563 return -ENOMEM;
Kiran Padwalbb95cd32014-09-19 12:44:39 +0530564
Jason Gunthorpe82cc1a42016-07-12 11:41:50 -0600565 if (dev->of_node) {
566 const struct of_device_id *of_id;
567
568 of_id = of_match_device(dev->driver->of_match_table, dev);
569 if (of_id && of_id->data == OF_IS_TPM2)
570 chip->flags |= TPM_CHIP_FLAG_TPM2;
571 } else
572 if (id->driver_data == I2C_IS_TPM2)
573 chip->flags |= TPM_CHIP_FLAG_TPM2;
574
Christophe Ricard6e599f62016-03-31 22:56:57 +0200575 init_waitqueue_head(&priv->read_queue);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600576
577 /* Default timeouts */
Christophe Ricardaf782f32016-03-31 22:56:59 +0200578 chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
579 chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
580 chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
581 chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600582
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200583 dev_set_drvdata(&chip->dev, priv);
584
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600585 /*
586 * I2C intfcaps (interrupt capabilitieis) in the chip are hard coded to:
587 * TPM_INTF_INT_LEVEL_LOW | TPM_INTF_DATA_AVAIL_INT
588 * The IRQ should be set in the i2c_board_info (which is done
589 * automatically in of_i2c_register_devices, for device tree users */
Christophe Ricard570a3602016-03-31 22:56:56 +0200590 priv->irq = client->irq;
Andrew Zamanskyfa7539b2016-06-29 12:58:49 +0300591 if (client->irq) {
Christophe Ricard570a3602016-03-31 22:56:56 +0200592 dev_dbg(dev, "%s() priv->irq\n", __func__);
593 rc = devm_request_irq(dev, client->irq,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600594 i2c_nuvoton_int_handler,
595 IRQF_TRIGGER_LOW,
Jason Gunthorpe3635e2e2016-02-29 12:29:48 -0500596 dev_name(&chip->dev),
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600597 chip);
598 if (rc) {
599 dev_err(dev, "%s() Unable to request irq: %d for use\n",
Christophe Ricard570a3602016-03-31 22:56:56 +0200600 __func__, priv->irq);
Christophe Ricard570a3602016-03-31 22:56:56 +0200601 priv->irq = 0;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600602 } else {
Andrew Zamanskyfa7539b2016-06-29 12:58:49 +0300603 chip->flags |= TPM_CHIP_FLAG_IRQ;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600604 /* Clear any pending interrupt */
605 i2c_nuvoton_ready(chip);
606 /* - wait for TPM_STS==0xA0 (stsValid, commandReady) */
607 rc = i2c_nuvoton_wait_for_stat(chip,
608 TPM_STS_COMMAND_READY,
609 TPM_STS_COMMAND_READY,
Christophe Ricardaf782f32016-03-31 22:56:59 +0200610 chip->timeout_b,
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600611 NULL);
612 if (rc == 0) {
613 /*
614 * TIS is in ready state
615 * write dummy byte to enter reception state
616 * TPM_DATA_FIFO_W <- rc (0)
617 */
618 rc = i2c_nuvoton_write_buf(client,
619 TPM_DATA_FIFO_W,
620 1, (u8 *) (&rc));
621 if (rc < 0)
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800622 return rc;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600623 /* TPM_STS <- 0x40 (commandReady) */
624 i2c_nuvoton_ready(chip);
625 } else {
626 /*
627 * timeout_b reached - command was
628 * aborted. TIS should now be in idle state -
629 * only TPM_STS_VALID should be set
630 */
631 if (i2c_nuvoton_read_status(chip) !=
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800632 TPM_STS_VALID)
633 return -EIO;
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600634 }
635 }
636 }
637
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800638 return tpm_chip_register(chip);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600639}
640
641static int i2c_nuvoton_remove(struct i2c_client *client)
642{
Christophe Ricard9e0d39d2016-03-31 22:57:00 +0200643 struct tpm_chip *chip = i2c_get_clientdata(client);
644
Jarkko Sakkinenafb5abc2014-12-12 11:46:34 -0800645 tpm_chip_unregister(chip);
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600646 return 0;
647}
648
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600649static const struct i2c_device_id i2c_nuvoton_id[] = {
Jason Gunthorpe82cc1a42016-07-12 11:41:50 -0600650 {"tpm_i2c_nuvoton"},
651 {"tpm2_i2c_nuvoton", .driver_data = I2C_IS_TPM2},
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600652 {}
653};
654MODULE_DEVICE_TABLE(i2c, i2c_nuvoton_id);
655
656#ifdef CONFIG_OF
657static const struct of_device_id i2c_nuvoton_of_match[] = {
658 {.compatible = "nuvoton,npct501"},
659 {.compatible = "winbond,wpct301"},
Jason Gunthorpe82cc1a42016-07-12 11:41:50 -0600660 {.compatible = "nuvoton,npct601", .data = OF_IS_TPM2},
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600661 {},
662};
663MODULE_DEVICE_TABLE(of, i2c_nuvoton_of_match);
664#endif
665
666static SIMPLE_DEV_PM_OPS(i2c_nuvoton_pm_ops, tpm_pm_suspend, tpm_pm_resume);
667
668static struct i2c_driver i2c_nuvoton_driver = {
669 .id_table = i2c_nuvoton_id,
670 .probe = i2c_nuvoton_probe,
671 .remove = i2c_nuvoton_remove,
672 .driver = {
Jason Gunthorpe82cc1a42016-07-12 11:41:50 -0600673 .name = "tpm_i2c_nuvoton",
Jason Gunthorpe4c336e42013-10-06 12:43:13 -0600674 .pm = &i2c_nuvoton_pm_ops,
675 .of_match_table = of_match_ptr(i2c_nuvoton_of_match),
676 },
677};
678
679module_i2c_driver(i2c_nuvoton_driver);
680
681MODULE_AUTHOR("Dan Morav (dan.morav@nuvoton.com)");
682MODULE_DESCRIPTION("Nuvoton TPM I2C Driver");
683MODULE_LICENSE("GPL");