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