Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 2 | /* |
| 3 | * STMicroelectronics TPM Linux driver for TPM ST33ZP24 |
Christophe RICARD | 2c2b217 | 2016-02-13 16:15:31 +0100 | [diff] [blame] | 4 | * Copyright (C) 2009 - 2016 STMicroelectronics |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/fs.h> |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/wait.h> |
| 12 | #include <linux/freezer.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/gpio.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/slab.h> |
| 20 | |
| 21 | #include "../tpm.h" |
| 22 | #include "st33zp24.h" |
| 23 | |
| 24 | #define TPM_ACCESS 0x0 |
| 25 | #define TPM_STS 0x18 |
| 26 | #define TPM_DATA_FIFO 0x24 |
| 27 | #define TPM_INTF_CAPABILITY 0x14 |
| 28 | #define TPM_INT_STATUS 0x10 |
| 29 | #define TPM_INT_ENABLE 0x08 |
| 30 | |
| 31 | #define LOCALITY0 0 |
| 32 | |
| 33 | enum st33zp24_access { |
| 34 | TPM_ACCESS_VALID = 0x80, |
| 35 | TPM_ACCESS_ACTIVE_LOCALITY = 0x20, |
| 36 | TPM_ACCESS_REQUEST_PENDING = 0x04, |
| 37 | TPM_ACCESS_REQUEST_USE = 0x02, |
| 38 | }; |
| 39 | |
| 40 | enum st33zp24_status { |
| 41 | TPM_STS_VALID = 0x80, |
| 42 | TPM_STS_COMMAND_READY = 0x40, |
| 43 | TPM_STS_GO = 0x20, |
| 44 | TPM_STS_DATA_AVAIL = 0x10, |
| 45 | TPM_STS_DATA_EXPECT = 0x08, |
| 46 | }; |
| 47 | |
| 48 | enum st33zp24_int_flags { |
| 49 | TPM_GLOBAL_INT_ENABLE = 0x80, |
| 50 | TPM_INTF_CMD_READY_INT = 0x080, |
| 51 | TPM_INTF_FIFO_AVALAIBLE_INT = 0x040, |
| 52 | TPM_INTF_WAKE_UP_READY_INT = 0x020, |
| 53 | TPM_INTF_LOCALITY_CHANGE_INT = 0x004, |
| 54 | TPM_INTF_STS_VALID_INT = 0x002, |
| 55 | TPM_INTF_DATA_AVAIL_INT = 0x001, |
| 56 | }; |
| 57 | |
| 58 | enum tis_defaults { |
| 59 | TIS_SHORT_TIMEOUT = 750, |
| 60 | TIS_LONG_TIMEOUT = 2000, |
| 61 | }; |
| 62 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 63 | /* |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 64 | * clear the pending interrupt. |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 65 | */ |
| 66 | static u8 clear_interruption(struct st33zp24_dev *tpm_dev) |
| 67 | { |
| 68 | u8 interrupt; |
| 69 | |
| 70 | tpm_dev->ops->recv(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1); |
| 71 | tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_STATUS, &interrupt, 1); |
| 72 | return interrupt; |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 73 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 74 | |
| 75 | /* |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 76 | * cancel the current command execution or set STS to COMMAND READY. |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 77 | */ |
| 78 | static void st33zp24_cancel(struct tpm_chip *chip) |
| 79 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 80 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 81 | u8 data; |
| 82 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 83 | data = TPM_STS_COMMAND_READY; |
| 84 | tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1); |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 85 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 86 | |
| 87 | /* |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 88 | * return the TPM_STS register |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 89 | */ |
| 90 | static u8 st33zp24_status(struct tpm_chip *chip) |
| 91 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 92 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 93 | u8 data; |
| 94 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 95 | tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS, &data, 1); |
| 96 | return data; |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 97 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 98 | |
| 99 | /* |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 100 | * if the locality is active |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 101 | */ |
Jerry Snitselaar | 84d2594 | 2017-03-18 01:59:57 -0700 | [diff] [blame] | 102 | static bool check_locality(struct tpm_chip *chip) |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 103 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 104 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 105 | u8 data; |
| 106 | u8 status; |
| 107 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 108 | status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_ACCESS, &data, 1); |
| 109 | if (status && (data & |
| 110 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == |
| 111 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) |
Jerry Snitselaar | 84d2594 | 2017-03-18 01:59:57 -0700 | [diff] [blame] | 112 | return true; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 113 | |
Jerry Snitselaar | 84d2594 | 2017-03-18 01:59:57 -0700 | [diff] [blame] | 114 | return false; |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 115 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 116 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 117 | static int request_locality(struct tpm_chip *chip) |
| 118 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 119 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 120 | unsigned long stop; |
| 121 | long ret; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 122 | u8 data; |
| 123 | |
Jerry Snitselaar | 84d2594 | 2017-03-18 01:59:57 -0700 | [diff] [blame] | 124 | if (check_locality(chip)) |
Christophe Ricard | 56671c8 | 2016-03-31 22:56:58 +0200 | [diff] [blame] | 125 | return tpm_dev->locality; |
| 126 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 127 | data = TPM_ACCESS_REQUEST_USE; |
| 128 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1); |
| 129 | if (ret < 0) |
| 130 | return ret; |
| 131 | |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame] | 132 | stop = jiffies + chip->timeout_a; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 133 | |
| 134 | /* Request locality is usually effective after the request */ |
| 135 | do { |
Jerry Snitselaar | 84d2594 | 2017-03-18 01:59:57 -0700 | [diff] [blame] | 136 | if (check_locality(chip)) |
Christophe Ricard | 56671c8 | 2016-03-31 22:56:58 +0200 | [diff] [blame] | 137 | return tpm_dev->locality; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 138 | msleep(TPM_TIMEOUT); |
| 139 | } while (time_before(jiffies, stop)); |
| 140 | |
| 141 | /* could not get locality */ |
| 142 | return -EACCES; |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 143 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 144 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 145 | static void release_locality(struct tpm_chip *chip) |
| 146 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 147 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 148 | u8 data; |
| 149 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 150 | data = TPM_ACCESS_ACTIVE_LOCALITY; |
| 151 | |
| 152 | tpm_dev->ops->send(tpm_dev->phy_id, TPM_ACCESS, &data, 1); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * get_burstcount return the burstcount value |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 157 | */ |
| 158 | static int get_burstcount(struct tpm_chip *chip) |
| 159 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 160 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 161 | unsigned long stop; |
| 162 | int burstcnt, status; |
Christophe Ricard | ddab0e3 | 2016-03-23 08:55:34 +0100 | [diff] [blame] | 163 | u8 temp; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 164 | |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame] | 165 | stop = jiffies + chip->timeout_d; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 166 | do { |
Christophe Ricard | ddab0e3 | 2016-03-23 08:55:34 +0100 | [diff] [blame] | 167 | status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 1, |
| 168 | &temp, 1); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 169 | if (status < 0) |
| 170 | return -EBUSY; |
| 171 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 172 | burstcnt = temp; |
Christophe Ricard | ddab0e3 | 2016-03-23 08:55:34 +0100 | [diff] [blame] | 173 | status = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_STS + 2, |
| 174 | &temp, 1); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 175 | if (status < 0) |
| 176 | return -EBUSY; |
| 177 | |
| 178 | burstcnt |= temp << 8; |
| 179 | if (burstcnt) |
| 180 | return burstcnt; |
| 181 | msleep(TPM_TIMEOUT); |
| 182 | } while (time_before(jiffies, stop)); |
| 183 | return -EBUSY; |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 184 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 185 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 186 | static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, |
| 187 | bool check_cancel, bool *canceled) |
| 188 | { |
| 189 | u8 status = chip->ops->status(chip); |
| 190 | |
| 191 | *canceled = false; |
| 192 | if ((status & mask) == mask) |
| 193 | return true; |
| 194 | if (check_cancel && chip->ops->req_canceled(chip, status)) { |
| 195 | *canceled = true; |
| 196 | return true; |
| 197 | } |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | /* |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 202 | * wait for a TPM_STS value |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 203 | */ |
| 204 | static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, |
| 205 | wait_queue_head_t *queue, bool check_cancel) |
| 206 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 207 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 208 | unsigned long stop; |
| 209 | int ret = 0; |
| 210 | bool canceled = false; |
| 211 | bool condition; |
| 212 | u32 cur_intrs; |
| 213 | u8 status; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 214 | |
| 215 | /* check current status */ |
| 216 | status = st33zp24_status(chip); |
| 217 | if ((status & mask) == mask) |
| 218 | return 0; |
| 219 | |
| 220 | stop = jiffies + timeout; |
| 221 | |
Christophe Ricard | 570a360 | 2016-03-31 22:56:56 +0200 | [diff] [blame] | 222 | if (chip->flags & TPM_CHIP_FLAG_IRQ) { |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 223 | cur_intrs = tpm_dev->intrs; |
| 224 | clear_interruption(tpm_dev); |
Christophe Ricard | 570a360 | 2016-03-31 22:56:56 +0200 | [diff] [blame] | 225 | enable_irq(tpm_dev->irq); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 226 | |
| 227 | do { |
| 228 | if (ret == -ERESTARTSYS && freezing(current)) |
| 229 | clear_thread_flag(TIF_SIGPENDING); |
| 230 | |
| 231 | timeout = stop - jiffies; |
| 232 | if ((long) timeout <= 0) |
| 233 | return -1; |
| 234 | |
| 235 | ret = wait_event_interruptible_timeout(*queue, |
| 236 | cur_intrs != tpm_dev->intrs, |
| 237 | timeout); |
| 238 | clear_interruption(tpm_dev); |
| 239 | condition = wait_for_tpm_stat_cond(chip, mask, |
| 240 | check_cancel, &canceled); |
| 241 | if (ret >= 0 && condition) { |
| 242 | if (canceled) |
| 243 | return -ECANCELED; |
| 244 | return 0; |
| 245 | } |
| 246 | } while (ret == -ERESTARTSYS && freezing(current)); |
| 247 | |
Christophe Ricard | 570a360 | 2016-03-31 22:56:56 +0200 | [diff] [blame] | 248 | disable_irq_nosync(tpm_dev->irq); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 249 | |
| 250 | } else { |
| 251 | do { |
| 252 | msleep(TPM_TIMEOUT); |
| 253 | status = chip->ops->status(chip); |
| 254 | if ((status & mask) == mask) |
| 255 | return 0; |
| 256 | } while (time_before(jiffies, stop)); |
| 257 | } |
| 258 | |
| 259 | return -ETIME; |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 260 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 261 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 262 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) |
| 263 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 264 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 265 | int size = 0, burstcnt, len, ret; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 266 | |
| 267 | while (size < count && |
| 268 | wait_for_stat(chip, |
| 269 | TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame] | 270 | chip->timeout_c, |
Christophe Ricard | 6e599f6 | 2016-03-31 22:56:57 +0200 | [diff] [blame] | 271 | &tpm_dev->read_queue, true) == 0) { |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 272 | burstcnt = get_burstcount(chip); |
| 273 | if (burstcnt < 0) |
| 274 | return burstcnt; |
| 275 | len = min_t(int, burstcnt, count - size); |
| 276 | ret = tpm_dev->ops->recv(tpm_dev->phy_id, TPM_DATA_FIFO, |
| 277 | buf + size, len); |
| 278 | if (ret < 0) |
| 279 | return ret; |
| 280 | |
| 281 | size += len; |
| 282 | } |
| 283 | return size; |
| 284 | } |
| 285 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 286 | static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id) |
| 287 | { |
| 288 | struct tpm_chip *chip = dev_id; |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 289 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 290 | |
| 291 | tpm_dev->intrs++; |
Christophe Ricard | 6e599f6 | 2016-03-31 22:56:57 +0200 | [diff] [blame] | 292 | wake_up_interruptible(&tpm_dev->read_queue); |
Christophe Ricard | 570a360 | 2016-03-31 22:56:56 +0200 | [diff] [blame] | 293 | disable_irq_nosync(tpm_dev->irq); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 294 | |
| 295 | return IRQ_HANDLED; |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 296 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 297 | |
| 298 | /* |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 299 | * send TPM commands through the I2C bus. |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 300 | */ |
| 301 | static int st33zp24_send(struct tpm_chip *chip, unsigned char *buf, |
| 302 | size_t len) |
| 303 | { |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 304 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | 7216ebc | 2015-03-23 22:29:56 +0100 | [diff] [blame] | 305 | u32 status, i, size, ordinal; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 306 | int burstcnt = 0; |
| 307 | int ret; |
| 308 | u8 data; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 309 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 310 | if (len < TPM_HEADER_SIZE) |
| 311 | return -EBUSY; |
| 312 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 313 | ret = request_locality(chip); |
| 314 | if (ret < 0) |
| 315 | return ret; |
| 316 | |
| 317 | status = st33zp24_status(chip); |
| 318 | if ((status & TPM_STS_COMMAND_READY) == 0) { |
| 319 | st33zp24_cancel(chip); |
| 320 | if (wait_for_stat |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame] | 321 | (chip, TPM_STS_COMMAND_READY, chip->timeout_b, |
Christophe Ricard | 6e599f6 | 2016-03-31 22:56:57 +0200 | [diff] [blame] | 322 | &tpm_dev->read_queue, false) < 0) { |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 323 | ret = -ETIME; |
| 324 | goto out_err; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | for (i = 0; i < len - 1;) { |
| 329 | burstcnt = get_burstcount(chip); |
| 330 | if (burstcnt < 0) |
| 331 | return burstcnt; |
| 332 | size = min_t(int, len - i - 1, burstcnt); |
| 333 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO, |
| 334 | buf + i, size); |
| 335 | if (ret < 0) |
| 336 | goto out_err; |
| 337 | |
| 338 | i += size; |
| 339 | } |
| 340 | |
| 341 | status = st33zp24_status(chip); |
| 342 | if ((status & TPM_STS_DATA_EXPECT) == 0) { |
| 343 | ret = -EIO; |
| 344 | goto out_err; |
| 345 | } |
| 346 | |
| 347 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_DATA_FIFO, |
| 348 | buf + len - 1, 1); |
| 349 | if (ret < 0) |
| 350 | goto out_err; |
| 351 | |
| 352 | status = st33zp24_status(chip); |
| 353 | if ((status & TPM_STS_DATA_EXPECT) != 0) { |
| 354 | ret = -EIO; |
| 355 | goto out_err; |
| 356 | } |
| 357 | |
| 358 | data = TPM_STS_GO; |
| 359 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_STS, &data, 1); |
| 360 | if (ret < 0) |
| 361 | goto out_err; |
| 362 | |
Christophe Ricard | 570a360 | 2016-03-31 22:56:56 +0200 | [diff] [blame] | 363 | if (chip->flags & TPM_CHIP_FLAG_IRQ) { |
Christophe Ricard | 7216ebc | 2015-03-23 22:29:56 +0100 | [diff] [blame] | 364 | ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); |
| 365 | |
| 366 | ret = wait_for_stat(chip, TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
Tomas Winkler | d856c00 | 2018-10-19 21:22:51 +0300 | [diff] [blame] | 367 | tpm_calc_ordinal_duration(chip, ordinal), |
Christophe Ricard | 6e599f6 | 2016-03-31 22:56:57 +0200 | [diff] [blame] | 368 | &tpm_dev->read_queue, false); |
Christophe Ricard | 7216ebc | 2015-03-23 22:29:56 +0100 | [diff] [blame] | 369 | if (ret < 0) |
| 370 | goto out_err; |
| 371 | } |
| 372 | |
Jarkko Sakkinen | f5595f5 | 2019-02-08 18:30:58 +0200 | [diff] [blame] | 373 | return 0; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 374 | out_err: |
| 375 | st33zp24_cancel(chip); |
| 376 | release_locality(chip); |
| 377 | return ret; |
| 378 | } |
| 379 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 380 | static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf, |
| 381 | size_t count) |
| 382 | { |
| 383 | int size = 0; |
Jeremy Boone | 6d24cd1 | 2018-02-08 12:29:09 -0800 | [diff] [blame] | 384 | u32 expected; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 385 | |
| 386 | if (!chip) |
| 387 | return -EBUSY; |
| 388 | |
| 389 | if (count < TPM_HEADER_SIZE) { |
| 390 | size = -EIO; |
| 391 | goto out; |
| 392 | } |
| 393 | |
| 394 | size = recv_data(chip, buf, TPM_HEADER_SIZE); |
| 395 | if (size < TPM_HEADER_SIZE) { |
| 396 | dev_err(&chip->dev, "Unable to read header\n"); |
| 397 | goto out; |
| 398 | } |
| 399 | |
| 400 | expected = be32_to_cpu(*(__be32 *)(buf + 2)); |
Jeremy Boone | 6d24cd1 | 2018-02-08 12:29:09 -0800 | [diff] [blame] | 401 | if (expected > count || expected < TPM_HEADER_SIZE) { |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 402 | size = -EIO; |
| 403 | goto out; |
| 404 | } |
| 405 | |
| 406 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], |
| 407 | expected - TPM_HEADER_SIZE); |
| 408 | if (size < expected) { |
| 409 | dev_err(&chip->dev, "Unable to read remainder of result\n"); |
| 410 | size = -ETIME; |
| 411 | } |
| 412 | |
| 413 | out: |
| 414 | st33zp24_cancel(chip); |
| 415 | release_locality(chip); |
| 416 | return size; |
| 417 | } |
| 418 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 419 | static bool st33zp24_req_canceled(struct tpm_chip *chip, u8 status) |
| 420 | { |
| 421 | return (status == TPM_STS_COMMAND_READY); |
| 422 | } |
| 423 | |
| 424 | static const struct tpm_class_ops st33zp24_tpm = { |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 425 | .flags = TPM_OPS_AUTO_STARTUP, |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 426 | .send = st33zp24_send, |
| 427 | .recv = st33zp24_recv, |
| 428 | .cancel = st33zp24_cancel, |
| 429 | .status = st33zp24_status, |
| 430 | .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 431 | .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, |
| 432 | .req_canceled = st33zp24_req_canceled, |
| 433 | }; |
| 434 | |
| 435 | /* |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 436 | * initialize the TPM device |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 437 | */ |
| 438 | int st33zp24_probe(void *phy_id, const struct st33zp24_phy_ops *ops, |
| 439 | struct device *dev, int irq, int io_lpcpd) |
| 440 | { |
| 441 | int ret; |
| 442 | u8 intmask = 0; |
| 443 | struct tpm_chip *chip; |
| 444 | struct st33zp24_dev *tpm_dev; |
| 445 | |
| 446 | chip = tpmm_chip_alloc(dev, &st33zp24_tpm); |
| 447 | if (IS_ERR(chip)) |
| 448 | return PTR_ERR(chip); |
| 449 | |
| 450 | tpm_dev = devm_kzalloc(dev, sizeof(struct st33zp24_dev), |
| 451 | GFP_KERNEL); |
| 452 | if (!tpm_dev) |
| 453 | return -ENOMEM; |
| 454 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 455 | tpm_dev->phy_id = phy_id; |
| 456 | tpm_dev->ops = ops; |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 457 | dev_set_drvdata(&chip->dev, tpm_dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 458 | |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame] | 459 | chip->timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 460 | chip->timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); |
| 461 | chip->timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
| 462 | chip->timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 463 | |
Christophe Ricard | 56671c8 | 2016-03-31 22:56:58 +0200 | [diff] [blame] | 464 | tpm_dev->locality = LOCALITY0; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 465 | |
| 466 | if (irq) { |
| 467 | /* INTERRUPT Setup */ |
Christophe Ricard | 6e599f6 | 2016-03-31 22:56:57 +0200 | [diff] [blame] | 468 | init_waitqueue_head(&tpm_dev->read_queue); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 469 | tpm_dev->intrs = 0; |
| 470 | |
| 471 | if (request_locality(chip) != LOCALITY0) { |
| 472 | ret = -ENODEV; |
| 473 | goto _tpm_clean_answer; |
| 474 | } |
| 475 | |
| 476 | clear_interruption(tpm_dev); |
| 477 | ret = devm_request_irq(dev, irq, tpm_ioserirq_handler, |
| 478 | IRQF_TRIGGER_HIGH, "TPM SERIRQ management", |
| 479 | chip); |
| 480 | if (ret < 0) { |
| 481 | dev_err(&chip->dev, "TPM SERIRQ signals %d not available\n", |
| 482 | irq); |
| 483 | goto _tpm_clean_answer; |
| 484 | } |
| 485 | |
| 486 | intmask |= TPM_INTF_CMD_READY_INT |
| 487 | | TPM_INTF_STS_VALID_INT |
| 488 | | TPM_INTF_DATA_AVAIL_INT; |
| 489 | |
| 490 | ret = tpm_dev->ops->send(tpm_dev->phy_id, TPM_INT_ENABLE, |
| 491 | &intmask, 1); |
| 492 | if (ret < 0) |
| 493 | goto _tpm_clean_answer; |
| 494 | |
| 495 | intmask = TPM_GLOBAL_INT_ENABLE; |
| 496 | ret = tpm_dev->ops->send(tpm_dev->phy_id, (TPM_INT_ENABLE + 3), |
| 497 | &intmask, 1); |
| 498 | if (ret < 0) |
| 499 | goto _tpm_clean_answer; |
| 500 | |
Christophe Ricard | 570a360 | 2016-03-31 22:56:56 +0200 | [diff] [blame] | 501 | tpm_dev->irq = irq; |
| 502 | chip->flags |= TPM_CHIP_FLAG_IRQ; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 503 | |
Christophe Ricard | 570a360 | 2016-03-31 22:56:56 +0200 | [diff] [blame] | 504 | disable_irq_nosync(tpm_dev->irq); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 505 | } |
| 506 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 507 | return tpm_chip_register(chip); |
| 508 | _tpm_clean_answer: |
| 509 | dev_info(&chip->dev, "TPM initialization fail\n"); |
| 510 | return ret; |
| 511 | } |
| 512 | EXPORT_SYMBOL(st33zp24_probe); |
| 513 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 514 | int st33zp24_remove(struct tpm_chip *chip) |
| 515 | { |
| 516 | tpm_chip_unregister(chip); |
| 517 | return 0; |
| 518 | } |
| 519 | EXPORT_SYMBOL(st33zp24_remove); |
| 520 | |
| 521 | #ifdef CONFIG_PM_SLEEP |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 522 | int st33zp24_pm_suspend(struct device *dev) |
| 523 | { |
| 524 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 525 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 526 | |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 527 | int ret = 0; |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 528 | |
| 529 | if (gpio_is_valid(tpm_dev->io_lpcpd)) |
| 530 | gpio_set_value(tpm_dev->io_lpcpd, 0); |
| 531 | else |
| 532 | ret = tpm_pm_suspend(dev); |
| 533 | |
| 534 | return ret; |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 535 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 536 | EXPORT_SYMBOL(st33zp24_pm_suspend); |
| 537 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 538 | int st33zp24_pm_resume(struct device *dev) |
| 539 | { |
| 540 | struct tpm_chip *chip = dev_get_drvdata(dev); |
Christophe Ricard | 9e0d39d | 2016-03-31 22:57:00 +0200 | [diff] [blame] | 541 | struct st33zp24_dev *tpm_dev = dev_get_drvdata(&chip->dev); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 542 | int ret = 0; |
| 543 | |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 544 | if (gpio_is_valid(tpm_dev->io_lpcpd)) { |
| 545 | gpio_set_value(tpm_dev->io_lpcpd, 1); |
| 546 | ret = wait_for_stat(chip, |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame] | 547 | TPM_STS_VALID, chip->timeout_b, |
Christophe Ricard | 6e599f6 | 2016-03-31 22:56:57 +0200 | [diff] [blame] | 548 | &tpm_dev->read_queue, false); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 549 | } else { |
| 550 | ret = tpm_pm_resume(dev); |
| 551 | if (!ret) |
Tomas Winkler | d4a31756 | 2018-10-19 21:22:56 +0300 | [diff] [blame] | 552 | tpm1_do_selftest(chip); |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 553 | } |
| 554 | return ret; |
Sohaib Mohamed | f04510f | 2021-10-13 11:21:50 +0200 | [diff] [blame] | 555 | } |
Christophe Ricard | bf38b87 | 2015-03-08 11:17:14 +0100 | [diff] [blame] | 556 | EXPORT_SYMBOL(st33zp24_pm_resume); |
| 557 | #endif |
| 558 | |
| 559 | MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)"); |
| 560 | MODULE_DESCRIPTION("ST33ZP24 TPM 1.2 driver"); |
| 561 | MODULE_VERSION("1.3.0"); |
| 562 | MODULE_LICENSE("GPL"); |