Commit | Line | Data |
---|---|---|
251a7b08 ML |
1 | /* |
2 | * STMicroelectronics TPM I2C Linux driver for TPM ST33ZP24 | |
3 | * Copyright (C) 2009, 2010 STMicroelectronics | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | * | |
19 | * STMicroelectronics version 1.2.0, Copyright (C) 2010 | |
20 | * STMicroelectronics comes with ABSOLUTELY NO WARRANTY. | |
21 | * This is free software, and you are welcome to redistribute it | |
22 | * under certain conditions. | |
23 | * | |
24 | * @Author: Christophe RICARD tpmsupport@st.com | |
25 | * | |
26 | * @File: tpm_stm_st33_i2c.c | |
27 | * | |
28 | * @Synopsis: | |
29 | * 09/15/2010: First shot driver tpm_tis driver for | |
30 | lpc is used as model. | |
31 | */ | |
32 | ||
3d7a7bd7 KY |
33 | #include <linux/pci.h> |
34 | #include <linux/module.h> | |
35 | #include <linux/platform_device.h> | |
36 | #include <linux/i2c.h> | |
37 | #include <linux/fs.h> | |
38 | #include <linux/miscdevice.h> | |
3d7a7bd7 KY |
39 | #include <linux/kernel.h> |
40 | #include <linux/delay.h> | |
3d7a7bd7 KY |
41 | #include <linux/wait.h> |
42 | #include <linux/string.h> | |
43 | #include <linux/interrupt.h> | |
44 | #include <linux/spinlock.h> | |
45 | #include <linux/sysfs.h> | |
46 | #include <linux/gpio.h> | |
47 | #include <linux/sched.h> | |
48 | #include <linux/uaccess.h> | |
49 | #include <linux/io.h> | |
50 | #include <linux/slab.h> | |
3d7a7bd7 KY |
51 | |
52 | #include "tpm.h" | |
9da228ea | 53 | #include "tpm_i2c_stm_st33.h" |
251a7b08 ML |
54 | |
55 | enum stm33zp24_access { | |
56 | TPM_ACCESS_VALID = 0x80, | |
57 | TPM_ACCESS_ACTIVE_LOCALITY = 0x20, | |
58 | TPM_ACCESS_REQUEST_PENDING = 0x04, | |
59 | TPM_ACCESS_REQUEST_USE = 0x02, | |
60 | }; | |
61 | ||
62 | enum stm33zp24_status { | |
63 | TPM_STS_VALID = 0x80, | |
64 | TPM_STS_COMMAND_READY = 0x40, | |
65 | TPM_STS_GO = 0x20, | |
66 | TPM_STS_DATA_AVAIL = 0x10, | |
67 | TPM_STS_DATA_EXPECT = 0x08, | |
68 | }; | |
69 | ||
70 | enum stm33zp24_int_flags { | |
71 | TPM_GLOBAL_INT_ENABLE = 0x80, | |
72 | TPM_INTF_CMD_READY_INT = 0x080, | |
73 | TPM_INTF_FIFO_AVALAIBLE_INT = 0x040, | |
74 | TPM_INTF_WAKE_UP_READY_INT = 0x020, | |
75 | TPM_INTF_LOCALITY_CHANGE_INT = 0x004, | |
76 | TPM_INTF_STS_VALID_INT = 0x002, | |
77 | TPM_INTF_DATA_AVAIL_INT = 0x001, | |
78 | }; | |
79 | ||
80 | enum tis_defaults { | |
81 | TIS_SHORT_TIMEOUT = 750, | |
82 | TIS_LONG_TIMEOUT = 2000, | |
83 | }; | |
84 | ||
85 | /* | |
86 | * write8_reg | |
87 | * Send byte to the TIS register according to the ST33ZP24 I2C protocol. | |
88 | * @param: tpm_register, the tpm tis register where the data should be written | |
89 | * @param: tpm_data, the tpm_data to write inside the tpm_register | |
90 | * @param: tpm_size, The length of the data | |
91 | * @return: Returns negative errno, or else the number of bytes written. | |
92 | */ | |
93 | static int write8_reg(struct i2c_client *client, u8 tpm_register, | |
94 | u8 *tpm_data, u16 tpm_size) | |
95 | { | |
251a7b08 ML |
96 | struct st33zp24_platform_data *pin_infos; |
97 | ||
98 | pin_infos = client->dev.platform_data; | |
99 | ||
64298919 PH |
100 | pin_infos->tpm_i2c_buffer[0][0] = tpm_register; |
101 | memcpy(&pin_infos->tpm_i2c_buffer[0][1], tpm_data, tpm_size); | |
7333549b | 102 | return i2c_master_send(client, pin_infos->tpm_i2c_buffer[0], |
251a7b08 | 103 | tpm_size + 1); |
251a7b08 ML |
104 | } /* write8_reg() */ |
105 | ||
106 | /* | |
107 | * read8_reg | |
108 | * Recv byte from the TIS register according to the ST33ZP24 I2C protocol. | |
109 | * @param: tpm_register, the tpm tis register where the data should be read | |
110 | * @param: tpm_data, the TPM response | |
111 | * @param: tpm_size, tpm TPM response size to read. | |
112 | * @return: number of byte read successfully: should be one if success. | |
113 | */ | |
114 | static int read8_reg(struct i2c_client *client, u8 tpm_register, | |
115 | u8 *tpm_data, int tpm_size) | |
116 | { | |
117 | u8 status = 0; | |
118 | u8 data; | |
251a7b08 ML |
119 | |
120 | data = TPM_DUMMY_BYTE; | |
121 | status = write8_reg(client, tpm_register, &data, 1); | |
122 | if (status == 2) | |
123 | status = i2c_master_recv(client, tpm_data, tpm_size); | |
124 | return status; | |
125 | } /* read8_reg() */ | |
126 | ||
127 | /* | |
128 | * I2C_WRITE_DATA | |
129 | * Send byte to the TIS register according to the ST33ZP24 I2C protocol. | |
130 | * @param: client, the chip description | |
131 | * @param: tpm_register, the tpm tis register where the data should be written | |
132 | * @param: tpm_data, the tpm_data to write inside the tpm_register | |
133 | * @param: tpm_size, The length of the data | |
134 | * @return: number of byte written successfully: should be one if success. | |
135 | */ | |
136 | #define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size) \ | |
137 | (write8_reg(client, tpm_register | \ | |
138 | TPM_WRITE_DIRECTION, tpm_data, tpm_size)) | |
139 | ||
140 | /* | |
141 | * I2C_READ_DATA | |
142 | * Recv byte from the TIS register according to the ST33ZP24 I2C protocol. | |
143 | * @param: tpm, the chip description | |
144 | * @param: tpm_register, the tpm tis register where the data should be read | |
145 | * @param: tpm_data, the TPM response | |
146 | * @param: tpm_size, tpm TPM response size to read. | |
147 | * @return: number of byte read successfully: should be one if success. | |
148 | */ | |
149 | #define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size) \ | |
150 | (read8_reg(client, tpm_register, tpm_data, tpm_size)) | |
151 | ||
152 | /* | |
153 | * clear_interruption | |
154 | * clear the TPM interrupt register. | |
155 | * @param: tpm, the chip description | |
156 | */ | |
157 | static void clear_interruption(struct i2c_client *client) | |
158 | { | |
159 | u8 interrupt; | |
160 | I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1); | |
161 | I2C_WRITE_DATA(client, TPM_INT_STATUS, &interrupt, 1); | |
162 | I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1); | |
163 | } /* clear_interruption() */ | |
164 | ||
165 | /* | |
166 | * _wait_for_interrupt_serirq_timeout | |
167 | * @param: tpm, the chip description | |
168 | * @param: timeout, the timeout of the interrupt | |
169 | * @return: the status of the interruption. | |
170 | */ | |
171 | static long _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip, | |
172 | unsigned long timeout) | |
173 | { | |
174 | long status; | |
175 | struct i2c_client *client; | |
176 | struct st33zp24_platform_data *pin_infos; | |
177 | ||
2d089f82 | 178 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
179 | pin_infos = client->dev.platform_data; |
180 | ||
181 | status = wait_for_completion_interruptible_timeout( | |
182 | &pin_infos->irq_detection, | |
183 | timeout); | |
184 | if (status > 0) | |
185 | enable_irq(gpio_to_irq(pin_infos->io_serirq)); | |
186 | gpio_direction_input(pin_infos->io_serirq); | |
187 | ||
188 | return status; | |
189 | } /* wait_for_interrupt_serirq_timeout() */ | |
190 | ||
3d7a7bd7 | 191 | static int wait_for_serirq_timeout(struct tpm_chip *chip, bool condition, |
251a7b08 ML |
192 | unsigned long timeout) |
193 | { | |
194 | int status = 2; | |
195 | struct i2c_client *client; | |
251a7b08 | 196 | |
2d089f82 | 197 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
198 | |
199 | status = _wait_for_interrupt_serirq_timeout(chip, timeout); | |
200 | if (!status) { | |
201 | status = -EBUSY; | |
2d089f82 | 202 | } else { |
251a7b08 ML |
203 | clear_interruption(client); |
204 | if (condition) | |
205 | status = 1; | |
206 | } | |
207 | return status; | |
208 | } | |
209 | ||
210 | /* | |
211 | * tpm_stm_i2c_cancel, cancel is not implemented. | |
212 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h | |
213 | */ | |
214 | static void tpm_stm_i2c_cancel(struct tpm_chip *chip) | |
215 | { | |
216 | struct i2c_client *client; | |
217 | u8 data; | |
218 | ||
2d089f82 | 219 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
220 | |
221 | data = TPM_STS_COMMAND_READY; | |
222 | I2C_WRITE_DATA(client, TPM_STS, &data, 1); | |
223 | if (chip->vendor.irq) | |
224 | wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a); | |
225 | } /* tpm_stm_i2c_cancel() */ | |
226 | ||
227 | /* | |
228 | * tpm_stm_spi_status return the TPM_STS register | |
229 | * @param: chip, the tpm chip description | |
230 | * @return: the TPM_STS register value. | |
231 | */ | |
232 | static u8 tpm_stm_i2c_status(struct tpm_chip *chip) | |
233 | { | |
234 | struct i2c_client *client; | |
235 | u8 data; | |
2d089f82 | 236 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
237 | |
238 | I2C_READ_DATA(client, TPM_STS, &data, 1); | |
239 | return data; | |
240 | } /* tpm_stm_i2c_status() */ | |
241 | ||
242 | ||
243 | /* | |
244 | * check_locality if the locality is active | |
245 | * @param: chip, the tpm chip description | |
246 | * @return: the active locality or -EACCESS. | |
247 | */ | |
248 | static int check_locality(struct tpm_chip *chip) | |
249 | { | |
250 | struct i2c_client *client; | |
251 | u8 data; | |
252 | u8 status; | |
253 | ||
2d089f82 | 254 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
255 | |
256 | status = I2C_READ_DATA(client, TPM_ACCESS, &data, 1); | |
257 | if (status && (data & | |
258 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) == | |
259 | (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) | |
260 | return chip->vendor.locality; | |
261 | ||
262 | return -EACCES; | |
263 | ||
264 | } /* check_locality() */ | |
265 | ||
266 | /* | |
267 | * request_locality request the TPM locality | |
268 | * @param: chip, the chip description | |
269 | * @return: the active locality or EACCESS. | |
270 | */ | |
271 | static int request_locality(struct tpm_chip *chip) | |
272 | { | |
273 | unsigned long stop; | |
274 | long rc; | |
275 | struct i2c_client *client; | |
276 | u8 data; | |
277 | ||
2d089f82 | 278 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
279 | |
280 | if (check_locality(chip) == chip->vendor.locality) | |
281 | return chip->vendor.locality; | |
282 | ||
283 | data = TPM_ACCESS_REQUEST_USE; | |
284 | rc = I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1); | |
285 | if (rc < 0) | |
286 | goto end; | |
287 | ||
288 | if (chip->vendor.irq) { | |
289 | rc = wait_for_serirq_timeout(chip, (check_locality | |
290 | (chip) >= 0), | |
291 | chip->vendor.timeout_a); | |
292 | if (rc > 0) | |
293 | return chip->vendor.locality; | |
2d089f82 | 294 | } else { |
251a7b08 ML |
295 | stop = jiffies + chip->vendor.timeout_a; |
296 | do { | |
297 | if (check_locality(chip) >= 0) | |
298 | return chip->vendor.locality; | |
299 | msleep(TPM_TIMEOUT); | |
300 | } while (time_before(jiffies, stop)); | |
301 | } | |
302 | rc = -EACCES; | |
303 | end: | |
304 | return rc; | |
305 | } /* request_locality() */ | |
306 | ||
307 | /* | |
308 | * release_locality release the active locality | |
309 | * @param: chip, the tpm chip description. | |
310 | */ | |
311 | static void release_locality(struct tpm_chip *chip) | |
312 | { | |
313 | struct i2c_client *client; | |
314 | u8 data; | |
315 | ||
2d089f82 | 316 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
317 | data = TPM_ACCESS_ACTIVE_LOCALITY; |
318 | ||
319 | I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1); | |
320 | } | |
321 | ||
322 | /* | |
323 | * get_burstcount return the burstcount address 0x19 0x1A | |
324 | * @param: chip, the chip description | |
325 | * return: the burstcount. | |
326 | */ | |
327 | static int get_burstcount(struct tpm_chip *chip) | |
328 | { | |
329 | unsigned long stop; | |
330 | int burstcnt, status; | |
331 | u8 tpm_reg, temp; | |
332 | ||
2d089f82 | 333 | struct i2c_client *client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
334 | |
335 | stop = jiffies + chip->vendor.timeout_d; | |
336 | do { | |
337 | tpm_reg = TPM_STS + 1; | |
338 | status = I2C_READ_DATA(client, tpm_reg, &temp, 1); | |
339 | if (status < 0) | |
340 | goto end; | |
341 | ||
342 | tpm_reg = tpm_reg + 1; | |
343 | burstcnt = temp; | |
344 | status = I2C_READ_DATA(client, tpm_reg, &temp, 1); | |
345 | if (status < 0) | |
346 | goto end; | |
347 | ||
348 | burstcnt |= temp << 8; | |
349 | if (burstcnt) | |
350 | return burstcnt; | |
351 | msleep(TPM_TIMEOUT); | |
352 | } while (time_before(jiffies, stop)); | |
353 | ||
354 | end: | |
355 | return -EBUSY; | |
356 | } /* get_burstcount() */ | |
357 | ||
358 | /* | |
359 | * wait_for_stat wait for a TPM_STS value | |
360 | * @param: chip, the tpm chip description | |
361 | * @param: mask, the value mask to wait | |
362 | * @param: timeout, the timeout | |
363 | * @param: queue, the wait queue. | |
364 | * @return: the tpm status, 0 if success, -ETIME if timeout is reached. | |
365 | */ | |
366 | static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, | |
367 | wait_queue_head_t *queue) | |
368 | { | |
369 | unsigned long stop; | |
370 | long rc; | |
371 | u8 status; | |
372 | ||
373 | if (chip->vendor.irq) { | |
374 | rc = wait_for_serirq_timeout(chip, ((tpm_stm_i2c_status | |
375 | (chip) & mask) == | |
376 | mask), timeout); | |
377 | if (rc > 0) | |
378 | return 0; | |
2d089f82 | 379 | } else { |
251a7b08 ML |
380 | stop = jiffies + timeout; |
381 | do { | |
382 | msleep(TPM_TIMEOUT); | |
383 | status = tpm_stm_i2c_status(chip); | |
384 | if ((status & mask) == mask) | |
385 | return 0; | |
386 | } while (time_before(jiffies, stop)); | |
387 | } | |
388 | return -ETIME; | |
389 | } /* wait_for_stat() */ | |
390 | ||
391 | /* | |
392 | * recv_data receive data | |
393 | * @param: chip, the tpm chip description | |
394 | * @param: buf, the buffer where the data are received | |
395 | * @param: count, the number of data to receive | |
396 | * @return: the number of bytes read from TPM FIFO. | |
397 | */ | |
398 | static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) | |
399 | { | |
400 | int size = 0, burstcnt, len; | |
401 | struct i2c_client *client; | |
402 | ||
2d089f82 | 403 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
404 | |
405 | while (size < count && | |
406 | wait_for_stat(chip, | |
407 | TPM_STS_DATA_AVAIL | TPM_STS_VALID, | |
408 | chip->vendor.timeout_c, | |
409 | &chip->vendor.read_queue) | |
410 | == 0) { | |
411 | burstcnt = get_burstcount(chip); | |
85c5e0d4 PH |
412 | if (burstcnt < 0) |
413 | return burstcnt; | |
251a7b08 ML |
414 | len = min_t(int, burstcnt, count - size); |
415 | I2C_READ_DATA(client, TPM_DATA_FIFO, buf + size, len); | |
416 | size += len; | |
417 | } | |
418 | return size; | |
419 | } | |
420 | ||
421 | /* | |
422 | * tpm_ioserirq_handler the serirq irq handler | |
423 | * @param: irq, the tpm chip description | |
424 | * @param: dev_id, the description of the chip | |
425 | * @return: the status of the handler. | |
426 | */ | |
427 | static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id) | |
428 | { | |
429 | struct tpm_chip *chip = dev_id; | |
430 | struct i2c_client *client; | |
431 | struct st33zp24_platform_data *pin_infos; | |
432 | ||
433 | disable_irq_nosync(irq); | |
434 | ||
2d089f82 | 435 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
436 | pin_infos = client->dev.platform_data; |
437 | ||
438 | complete(&pin_infos->irq_detection); | |
439 | return IRQ_HANDLED; | |
440 | } /* tpm_ioserirq_handler() */ | |
441 | ||
442 | ||
443 | /* | |
444 | * tpm_stm_i2c_send send TPM commands through the I2C bus. | |
445 | * | |
446 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h | |
447 | * @param: buf, the buffer to send. | |
448 | * @param: count, the number of bytes to send. | |
449 | * @return: In case of success the number of bytes sent. | |
450 | * In other case, a < 0 value describing the issue. | |
451 | */ | |
452 | static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf, | |
453 | size_t len) | |
454 | { | |
85c5e0d4 PH |
455 | u32 status, i, size; |
456 | int burstcnt = 0; | |
3d7a7bd7 | 457 | int ret; |
251a7b08 ML |
458 | u8 data; |
459 | struct i2c_client *client; | |
251a7b08 ML |
460 | |
461 | if (chip == NULL) | |
462 | return -EBUSY; | |
463 | if (len < TPM_HEADER_SIZE) | |
464 | return -EBUSY; | |
465 | ||
3d7a7bd7 | 466 | client = (struct i2c_client *)TPM_VPRIV(chip); |
251a7b08 ML |
467 | |
468 | client->flags = 0; | |
469 | ||
470 | ret = request_locality(chip); | |
471 | if (ret < 0) | |
472 | return ret; | |
473 | ||
474 | status = tpm_stm_i2c_status(chip); | |
475 | if ((status & TPM_STS_COMMAND_READY) == 0) { | |
476 | tpm_stm_i2c_cancel(chip); | |
477 | if (wait_for_stat | |
478 | (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b, | |
479 | &chip->vendor.int_queue) < 0) { | |
480 | ret = -ETIME; | |
481 | goto out_err; | |
482 | } | |
483 | } | |
484 | ||
2d089f82 | 485 | for (i = 0; i < len - 1;) { |
251a7b08 | 486 | burstcnt = get_burstcount(chip); |
85c5e0d4 PH |
487 | if (burstcnt < 0) |
488 | return burstcnt; | |
251a7b08 | 489 | size = min_t(int, len - i - 1, burstcnt); |
1ba3b0b6 | 490 | ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + i, size); |
251a7b08 ML |
491 | if (ret < 0) |
492 | goto out_err; | |
493 | ||
494 | i += size; | |
495 | } | |
496 | ||
497 | status = tpm_stm_i2c_status(chip); | |
498 | if ((status & TPM_STS_DATA_EXPECT) == 0) { | |
499 | ret = -EIO; | |
500 | goto out_err; | |
501 | } | |
502 | ||
503 | ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + len - 1, 1); | |
504 | if (ret < 0) | |
505 | goto out_err; | |
506 | ||
507 | status = tpm_stm_i2c_status(chip); | |
508 | if ((status & TPM_STS_DATA_EXPECT) != 0) { | |
509 | ret = -EIO; | |
510 | goto out_err; | |
511 | } | |
512 | ||
513 | data = TPM_STS_GO; | |
514 | I2C_WRITE_DATA(client, TPM_STS, &data, 1); | |
515 | ||
516 | return len; | |
517 | out_err: | |
518 | tpm_stm_i2c_cancel(chip); | |
519 | release_locality(chip); | |
520 | return ret; | |
521 | } | |
522 | ||
523 | /* | |
524 | * tpm_stm_i2c_recv received TPM response through the I2C bus. | |
525 | * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h. | |
526 | * @param: buf, the buffer to store datas. | |
527 | * @param: count, the number of bytes to send. | |
528 | * @return: In case of success the number of bytes received. | |
529 | * In other case, a < 0 value describing the issue. | |
530 | */ | |
531 | static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf, | |
532 | size_t count) | |
533 | { | |
534 | int size = 0; | |
535 | int expected; | |
536 | ||
251a7b08 ML |
537 | if (chip == NULL) |
538 | return -EBUSY; | |
539 | ||
540 | if (count < TPM_HEADER_SIZE) { | |
541 | size = -EIO; | |
542 | goto out; | |
543 | } | |
544 | ||
545 | size = recv_data(chip, buf, TPM_HEADER_SIZE); | |
546 | if (size < TPM_HEADER_SIZE) { | |
547 | dev_err(chip->dev, "Unable to read header\n"); | |
548 | goto out; | |
549 | } | |
550 | ||
2d089f82 | 551 | expected = be32_to_cpu(*(__be32 *)(buf + 2)); |
251a7b08 ML |
552 | if (expected > count) { |
553 | size = -EIO; | |
554 | goto out; | |
555 | } | |
556 | ||
557 | size += recv_data(chip, &buf[TPM_HEADER_SIZE], | |
558 | expected - TPM_HEADER_SIZE); | |
559 | if (size < expected) { | |
560 | dev_err(chip->dev, "Unable to read remainder of result\n"); | |
561 | size = -ETIME; | |
562 | goto out; | |
563 | } | |
564 | ||
565 | out: | |
5f82e9f0 | 566 | chip->ops->cancel(chip); |
251a7b08 ML |
567 | release_locality(chip); |
568 | return size; | |
569 | } | |
570 | ||
1f866057 SB |
571 | static bool tpm_st33_i2c_req_canceled(struct tpm_chip *chip, u8 status) |
572 | { | |
2d089f82 | 573 | return (status == TPM_STS_COMMAND_READY); |
1f866057 SB |
574 | } |
575 | ||
01ad1fa7 | 576 | static const struct tpm_class_ops st_i2c_tpm = { |
251a7b08 ML |
577 | .send = tpm_stm_i2c_send, |
578 | .recv = tpm_stm_i2c_recv, | |
579 | .cancel = tpm_stm_i2c_cancel, | |
580 | .status = tpm_stm_i2c_status, | |
581 | .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID, | |
582 | .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID, | |
1f866057 | 583 | .req_canceled = tpm_st33_i2c_req_canceled, |
251a7b08 ML |
584 | }; |
585 | ||
2d089f82 | 586 | static int interrupts; |
251a7b08 ML |
587 | module_param(interrupts, int, 0444); |
588 | MODULE_PARM_DESC(interrupts, "Enable interrupts"); | |
589 | ||
590 | static int power_mgt = 1; | |
591 | module_param(power_mgt, int, 0444); | |
592 | MODULE_PARM_DESC(power_mgt, "Power Management"); | |
593 | ||
594 | /* | |
595 | * tpm_st33_i2c_probe initialize the TPM device | |
596 | * @param: client, the i2c_client drescription (TPM I2C description). | |
597 | * @param: id, the i2c_device_id struct. | |
598 | * @return: 0 in case of success. | |
599 | * -1 in other case. | |
600 | */ | |
601 | static int | |
602 | tpm_st33_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id) | |
603 | { | |
3d7a7bd7 | 604 | int err; |
251a7b08 ML |
605 | u8 intmask; |
606 | struct tpm_chip *chip; | |
607 | struct st33zp24_platform_data *platform_data; | |
608 | ||
251a7b08 | 609 | if (client == NULL) { |
1fbc5e95 KY |
610 | pr_info("%s: i2c client is NULL. Device not accessible.\n", |
611 | __func__); | |
251a7b08 ML |
612 | err = -ENODEV; |
613 | goto end; | |
614 | } | |
615 | ||
616 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | |
617 | dev_info(&client->dev, "client not i2c capable\n"); | |
618 | err = -ENODEV; | |
619 | goto end; | |
620 | } | |
621 | ||
622 | chip = tpm_register_hardware(&client->dev, &st_i2c_tpm); | |
623 | if (!chip) { | |
624 | dev_info(&client->dev, "fail chip\n"); | |
625 | err = -ENODEV; | |
626 | goto end; | |
627 | } | |
628 | ||
629 | platform_data = client->dev.platform_data; | |
1fbc5e95 KY |
630 | |
631 | if (!platform_data) { | |
632 | dev_info(&client->dev, "chip not available\n"); | |
633 | err = -ENODEV; | |
634 | goto _tpm_clean_answer; | |
635 | } | |
636 | ||
251a7b08 | 637 | platform_data->tpm_i2c_buffer[0] = |
9fd8e5a2 | 638 | kmalloc(TPM_BUFSIZE, GFP_KERNEL); |
251a7b08 ML |
639 | if (platform_data->tpm_i2c_buffer[0] == NULL) { |
640 | err = -ENOMEM; | |
641 | goto _tpm_clean_answer; | |
642 | } | |
643 | platform_data->tpm_i2c_buffer[1] = | |
9fd8e5a2 | 644 | kmalloc(TPM_BUFSIZE, GFP_KERNEL); |
251a7b08 ML |
645 | if (platform_data->tpm_i2c_buffer[1] == NULL) { |
646 | err = -ENOMEM; | |
3d7a7bd7 | 647 | goto _tpm_clean_response1; |
251a7b08 ML |
648 | } |
649 | ||
3d7a7bd7 | 650 | TPM_VPRIV(chip) = client; |
251a7b08 ML |
651 | |
652 | chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | |
653 | chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT); | |
654 | chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | |
655 | chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT); | |
656 | ||
657 | chip->vendor.locality = LOCALITY0; | |
658 | ||
659 | if (power_mgt) { | |
660 | err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD"); | |
661 | if (err) | |
662 | goto _gpio_init1; | |
663 | gpio_set_value(platform_data->io_lpcpd, 1); | |
664 | } | |
665 | ||
666 | if (interrupts) { | |
667 | init_completion(&platform_data->irq_detection); | |
668 | if (request_locality(chip) != LOCALITY0) { | |
669 | err = -ENODEV; | |
3d7a7bd7 | 670 | goto _tpm_clean_response2; |
251a7b08 ML |
671 | } |
672 | err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ"); | |
673 | if (err) | |
674 | goto _gpio_init2; | |
675 | ||
676 | clear_interruption(client); | |
677 | err = request_irq(gpio_to_irq(platform_data->io_serirq), | |
678 | &tpm_ioserirq_handler, | |
679 | IRQF_TRIGGER_HIGH, | |
680 | "TPM SERIRQ management", chip); | |
681 | if (err < 0) { | |
682 | dev_err(chip->dev , "TPM SERIRQ signals %d not available\n", | |
2d089f82 | 683 | gpio_to_irq(platform_data->io_serirq)); |
251a7b08 ML |
684 | goto _irq_set; |
685 | } | |
686 | ||
687 | err = I2C_READ_DATA(client, TPM_INT_ENABLE, &intmask, 1); | |
688 | if (err < 0) | |
689 | goto _irq_set; | |
690 | ||
691 | intmask |= TPM_INTF_CMD_READY_INT | |
692 | | TPM_INTF_FIFO_AVALAIBLE_INT | |
693 | | TPM_INTF_WAKE_UP_READY_INT | |
694 | | TPM_INTF_LOCALITY_CHANGE_INT | |
695 | | TPM_INTF_STS_VALID_INT | |
696 | | TPM_INTF_DATA_AVAIL_INT; | |
697 | ||
698 | err = I2C_WRITE_DATA(client, TPM_INT_ENABLE, &intmask, 1); | |
699 | if (err < 0) | |
700 | goto _irq_set; | |
701 | ||
702 | intmask = TPM_GLOBAL_INT_ENABLE; | |
703 | err = I2C_WRITE_DATA(client, (TPM_INT_ENABLE + 3), &intmask, 1); | |
704 | if (err < 0) | |
705 | goto _irq_set; | |
706 | ||
707 | err = I2C_READ_DATA(client, TPM_INT_STATUS, &intmask, 1); | |
708 | if (err < 0) | |
709 | goto _irq_set; | |
710 | ||
711 | chip->vendor.irq = interrupts; | |
712 | ||
713 | tpm_gen_interrupt(chip); | |
714 | } | |
715 | ||
716 | tpm_get_timeouts(chip); | |
f07a5e9a | 717 | tpm_do_selftest(chip); |
251a7b08 | 718 | |
251a7b08 ML |
719 | dev_info(chip->dev, "TPM I2C Initialized\n"); |
720 | return 0; | |
721 | _irq_set: | |
2d089f82 | 722 | free_irq(gpio_to_irq(platform_data->io_serirq), (void *)chip); |
251a7b08 | 723 | _gpio_init2: |
3d7a7bd7 | 724 | if (interrupts) |
251a7b08 ML |
725 | gpio_free(platform_data->io_serirq); |
726 | _gpio_init1: | |
3d7a7bd7 | 727 | if (power_mgt) |
251a7b08 | 728 | gpio_free(platform_data->io_lpcpd); |
3d7a7bd7 KY |
729 | _tpm_clean_response2: |
730 | kzfree(platform_data->tpm_i2c_buffer[1]); | |
731 | platform_data->tpm_i2c_buffer[1] = NULL; | |
732 | _tpm_clean_response1: | |
733 | kzfree(platform_data->tpm_i2c_buffer[0]); | |
734 | platform_data->tpm_i2c_buffer[0] = NULL; | |
251a7b08 | 735 | _tpm_clean_answer: |
3d7a7bd7 | 736 | tpm_remove_hardware(chip->dev); |
251a7b08 ML |
737 | end: |
738 | pr_info("TPM I2C initialisation fail\n"); | |
739 | return err; | |
740 | } | |
741 | ||
742 | /* | |
743 | * tpm_st33_i2c_remove remove the TPM device | |
744 | * @param: client, the i2c_client drescription (TPM I2C description). | |
745 | clear_bit(0, &chip->is_open); | |
746 | * @return: 0 in case of success. | |
747 | */ | |
e02983cf | 748 | static int tpm_st33_i2c_remove(struct i2c_client *client) |
251a7b08 ML |
749 | { |
750 | struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata(client); | |
751 | struct st33zp24_platform_data *pin_infos = | |
2d089f82 | 752 | ((struct i2c_client *)TPM_VPRIV(chip))->dev.platform_data; |
251a7b08 ML |
753 | |
754 | if (pin_infos != NULL) { | |
755 | free_irq(pin_infos->io_serirq, chip); | |
756 | ||
757 | gpio_free(pin_infos->io_serirq); | |
758 | gpio_free(pin_infos->io_lpcpd); | |
759 | ||
1fbc5e95 KY |
760 | tpm_remove_hardware(chip->dev); |
761 | ||
251a7b08 ML |
762 | if (pin_infos->tpm_i2c_buffer[1] != NULL) { |
763 | kzfree(pin_infos->tpm_i2c_buffer[1]); | |
764 | pin_infos->tpm_i2c_buffer[1] = NULL; | |
765 | } | |
766 | if (pin_infos->tpm_i2c_buffer[0] != NULL) { | |
767 | kzfree(pin_infos->tpm_i2c_buffer[0]); | |
768 | pin_infos->tpm_i2c_buffer[0] = NULL; | |
769 | } | |
770 | } | |
771 | ||
772 | return 0; | |
773 | } | |
774 | ||
d4593353 | 775 | #ifdef CONFIG_PM_SLEEP |
251a7b08 ML |
776 | /* |
777 | * tpm_st33_i2c_pm_suspend suspend the TPM device | |
251a7b08 ML |
778 | * @param: client, the i2c_client drescription (TPM I2C description). |
779 | * @param: mesg, the power management message. | |
780 | * @return: 0 in case of success. | |
781 | */ | |
d4593353 | 782 | static int tpm_st33_i2c_pm_suspend(struct device *dev) |
251a7b08 | 783 | { |
d4593353 | 784 | struct st33zp24_platform_data *pin_infos = dev->platform_data; |
251a7b08 ML |
785 | int ret = 0; |
786 | ||
2d089f82 | 787 | if (power_mgt) { |
251a7b08 | 788 | gpio_set_value(pin_infos->io_lpcpd, 0); |
2d089f82 | 789 | } else { |
d4593353 | 790 | ret = tpm_pm_suspend(dev); |
251a7b08 ML |
791 | } |
792 | return ret; | |
793 | } /* tpm_st33_i2c_suspend() */ | |
794 | ||
795 | /* | |
796 | * tpm_st33_i2c_pm_resume resume the TPM device | |
797 | * @param: client, the i2c_client drescription (TPM I2C description). | |
798 | * @return: 0 in case of success. | |
799 | */ | |
d4593353 | 800 | static int tpm_st33_i2c_pm_resume(struct device *dev) |
251a7b08 | 801 | { |
d4593353 PH |
802 | struct tpm_chip *chip = dev_get_drvdata(dev); |
803 | struct st33zp24_platform_data *pin_infos = dev->platform_data; | |
251a7b08 ML |
804 | |
805 | int ret = 0; | |
806 | ||
807 | if (power_mgt) { | |
808 | gpio_set_value(pin_infos->io_lpcpd, 1); | |
809 | ret = wait_for_serirq_timeout(chip, | |
5f82e9f0 | 810 | (chip->ops->status(chip) & |
251a7b08 ML |
811 | TPM_STS_VALID) == TPM_STS_VALID, |
812 | chip->vendor.timeout_b); | |
2d089f82 | 813 | } else { |
2d089f82 PH |
814 | ret = tpm_pm_resume(dev); |
815 | if (!ret) | |
816 | tpm_do_selftest(chip); | |
251a7b08 ML |
817 | } |
818 | return ret; | |
819 | } /* tpm_st33_i2c_pm_resume() */ | |
d4593353 | 820 | #endif |
251a7b08 ML |
821 | |
822 | static const struct i2c_device_id tpm_st33_i2c_id[] = { | |
823 | {TPM_ST33_I2C, 0}, | |
824 | {} | |
825 | }; | |
251a7b08 | 826 | MODULE_DEVICE_TABLE(i2c, tpm_st33_i2c_id); |
2d089f82 PH |
827 | static SIMPLE_DEV_PM_OPS(tpm_st33_i2c_ops, tpm_st33_i2c_pm_suspend, |
828 | tpm_st33_i2c_pm_resume); | |
251a7b08 ML |
829 | static struct i2c_driver tpm_st33_i2c_driver = { |
830 | .driver = { | |
831 | .owner = THIS_MODULE, | |
832 | .name = TPM_ST33_I2C, | |
d4593353 | 833 | .pm = &tpm_st33_i2c_ops, |
251a7b08 ML |
834 | }, |
835 | .probe = tpm_st33_i2c_probe, | |
836 | .remove = tpm_st33_i2c_remove, | |
251a7b08 ML |
837 | .id_table = tpm_st33_i2c_id |
838 | }; | |
839 | ||
3d7a7bd7 | 840 | module_i2c_driver(tpm_st33_i2c_driver); |
251a7b08 ML |
841 | |
842 | MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)"); | |
843 | MODULE_DESCRIPTION("STM TPM I2C ST33 Driver"); | |
844 | MODULE_VERSION("1.2.0"); | |
3d7a7bd7 | 845 | MODULE_LICENSE("GPL"); |