tpm_i2c_stm_st33.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. #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>
  38. #include <linux/kernel.h>
  39. #include <linux/delay.h>
  40. #include <linux/init.h>
  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>
  51. #include "tpm.h"
  52. #include "tpm_i2c_stm_st33.h"
  53. enum stm33zp24_access {
  54. TPM_ACCESS_VALID = 0x80,
  55. TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
  56. TPM_ACCESS_REQUEST_PENDING = 0x04,
  57. TPM_ACCESS_REQUEST_USE = 0x02,
  58. };
  59. enum stm33zp24_status {
  60. TPM_STS_VALID = 0x80,
  61. TPM_STS_COMMAND_READY = 0x40,
  62. TPM_STS_GO = 0x20,
  63. TPM_STS_DATA_AVAIL = 0x10,
  64. TPM_STS_DATA_EXPECT = 0x08,
  65. };
  66. enum stm33zp24_int_flags {
  67. TPM_GLOBAL_INT_ENABLE = 0x80,
  68. TPM_INTF_CMD_READY_INT = 0x080,
  69. TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
  70. TPM_INTF_WAKE_UP_READY_INT = 0x020,
  71. TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
  72. TPM_INTF_STS_VALID_INT = 0x002,
  73. TPM_INTF_DATA_AVAIL_INT = 0x001,
  74. };
  75. enum tis_defaults {
  76. TIS_SHORT_TIMEOUT = 750,
  77. TIS_LONG_TIMEOUT = 2000,
  78. };
  79. /*
  80. * write8_reg
  81. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  82. * @param: tpm_register, the tpm tis register where the data should be written
  83. * @param: tpm_data, the tpm_data to write inside the tpm_register
  84. * @param: tpm_size, The length of the data
  85. * @return: Returns negative errno, or else the number of bytes written.
  86. */
  87. static int write8_reg(struct i2c_client *client, u8 tpm_register,
  88. u8 *tpm_data, u16 tpm_size)
  89. {
  90. struct st33zp24_platform_data *pin_infos;
  91. pin_infos = client->dev.platform_data;
  92. pin_infos->tpm_i2c_buffer[0][0] = tpm_register;
  93. memcpy(&pin_infos->tpm_i2c_buffer[0][1], tpm_data, tpm_size);
  94. return i2c_master_send(client, pin_infos->tpm_i2c_buffer[0],
  95. tpm_size + 1);
  96. } /* write8_reg() */
  97. /*
  98. * read8_reg
  99. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  100. * @param: tpm_register, the tpm tis register where the data should be read
  101. * @param: tpm_data, the TPM response
  102. * @param: tpm_size, tpm TPM response size to read.
  103. * @return: number of byte read successfully: should be one if success.
  104. */
  105. static int read8_reg(struct i2c_client *client, u8 tpm_register,
  106. u8 *tpm_data, int tpm_size)
  107. {
  108. u8 status = 0;
  109. u8 data;
  110. data = TPM_DUMMY_BYTE;
  111. status = write8_reg(client, tpm_register, &data, 1);
  112. if (status == 2)
  113. status = i2c_master_recv(client, tpm_data, tpm_size);
  114. return status;
  115. } /* read8_reg() */
  116. /*
  117. * I2C_WRITE_DATA
  118. * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
  119. * @param: client, the chip description
  120. * @param: tpm_register, the tpm tis register where the data should be written
  121. * @param: tpm_data, the tpm_data to write inside the tpm_register
  122. * @param: tpm_size, The length of the data
  123. * @return: number of byte written successfully: should be one if success.
  124. */
  125. #define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size) \
  126. (write8_reg(client, tpm_register | \
  127. TPM_WRITE_DIRECTION, tpm_data, tpm_size))
  128. /*
  129. * I2C_READ_DATA
  130. * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
  131. * @param: tpm, the chip description
  132. * @param: tpm_register, the tpm tis register where the data should be read
  133. * @param: tpm_data, the TPM response
  134. * @param: tpm_size, tpm TPM response size to read.
  135. * @return: number of byte read successfully: should be one if success.
  136. */
  137. #define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size) \
  138. (read8_reg(client, tpm_register, tpm_data, tpm_size))
  139. /*
  140. * clear_interruption
  141. * clear the TPM interrupt register.
  142. * @param: tpm, the chip description
  143. */
  144. static void clear_interruption(struct i2c_client *client)
  145. {
  146. u8 interrupt;
  147. I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
  148. I2C_WRITE_DATA(client, TPM_INT_STATUS, &interrupt, 1);
  149. I2C_READ_DATA(client, TPM_INT_STATUS, &interrupt, 1);
  150. } /* clear_interruption() */
  151. /*
  152. * _wait_for_interrupt_serirq_timeout
  153. * @param: tpm, the chip description
  154. * @param: timeout, the timeout of the interrupt
  155. * @return: the status of the interruption.
  156. */
  157. static long _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
  158. unsigned long timeout)
  159. {
  160. long status;
  161. struct i2c_client *client;
  162. struct st33zp24_platform_data *pin_infos;
  163. client = (struct i2c_client *)TPM_VPRIV(chip);
  164. pin_infos = client->dev.platform_data;
  165. status = wait_for_completion_interruptible_timeout(
  166. &pin_infos->irq_detection,
  167. timeout);
  168. if (status > 0)
  169. enable_irq(gpio_to_irq(pin_infos->io_serirq));
  170. gpio_direction_input(pin_infos->io_serirq);
  171. return status;
  172. } /* wait_for_interrupt_serirq_timeout() */
  173. static int wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
  174. unsigned long timeout)
  175. {
  176. int status = 2;
  177. struct i2c_client *client;
  178. client = (struct i2c_client *)TPM_VPRIV(chip);
  179. status = _wait_for_interrupt_serirq_timeout(chip, timeout);
  180. if (!status) {
  181. status = -EBUSY;
  182. } else {
  183. clear_interruption(client);
  184. if (condition)
  185. status = 1;
  186. }
  187. return status;
  188. }
  189. /*
  190. * tpm_stm_i2c_cancel, cancel is not implemented.
  191. * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
  192. */
  193. static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
  194. {
  195. struct i2c_client *client;
  196. u8 data;
  197. client = (struct i2c_client *)TPM_VPRIV(chip);
  198. data = TPM_STS_COMMAND_READY;
  199. I2C_WRITE_DATA(client, TPM_STS, &data, 1);
  200. if (chip->vendor.irq)
  201. wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
  202. } /* tpm_stm_i2c_cancel() */
  203. /*
  204. * tpm_stm_spi_status return the TPM_STS register
  205. * @param: chip, the tpm chip description
  206. * @return: the TPM_STS register value.
  207. */
  208. static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
  209. {
  210. struct i2c_client *client;
  211. u8 data;
  212. client = (struct i2c_client *)TPM_VPRIV(chip);
  213. I2C_READ_DATA(client, TPM_STS, &data, 1);
  214. return data;
  215. } /* tpm_stm_i2c_status() */
  216. /*
  217. * check_locality if the locality is active
  218. * @param: chip, the tpm chip description
  219. * @return: the active locality or -EACCESS.
  220. */
  221. static int check_locality(struct tpm_chip *chip)
  222. {
  223. struct i2c_client *client;
  224. u8 data;
  225. u8 status;
  226. client = (struct i2c_client *)TPM_VPRIV(chip);
  227. status = I2C_READ_DATA(client, TPM_ACCESS, &data, 1);
  228. if (status && (data &
  229. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
  230. (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
  231. return chip->vendor.locality;
  232. return -EACCES;
  233. } /* check_locality() */
  234. /*
  235. * request_locality request the TPM locality
  236. * @param: chip, the chip description
  237. * @return: the active locality or EACCESS.
  238. */
  239. static int request_locality(struct tpm_chip *chip)
  240. {
  241. unsigned long stop;
  242. long rc;
  243. struct i2c_client *client;
  244. u8 data;
  245. client = (struct i2c_client *)TPM_VPRIV(chip);
  246. if (check_locality(chip) == chip->vendor.locality)
  247. return chip->vendor.locality;
  248. data = TPM_ACCESS_REQUEST_USE;
  249. rc = I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
  250. if (rc < 0)
  251. goto end;
  252. if (chip->vendor.irq) {
  253. rc = wait_for_serirq_timeout(chip, (check_locality
  254. (chip) >= 0),
  255. chip->vendor.timeout_a);
  256. if (rc > 0)
  257. return chip->vendor.locality;
  258. } else {
  259. stop = jiffies + chip->vendor.timeout_a;
  260. do {
  261. if (check_locality(chip) >= 0)
  262. return chip->vendor.locality;
  263. msleep(TPM_TIMEOUT);
  264. } while (time_before(jiffies, stop));
  265. }
  266. rc = -EACCES;
  267. end:
  268. return rc;
  269. } /* request_locality() */
  270. /*
  271. * release_locality release the active locality
  272. * @param: chip, the tpm chip description.
  273. */
  274. static void release_locality(struct tpm_chip *chip)
  275. {
  276. struct i2c_client *client;
  277. u8 data;
  278. client = (struct i2c_client *)TPM_VPRIV(chip);
  279. data = TPM_ACCESS_ACTIVE_LOCALITY;
  280. I2C_WRITE_DATA(client, TPM_ACCESS, &data, 1);
  281. }
  282. /*
  283. * get_burstcount return the burstcount address 0x19 0x1A
  284. * @param: chip, the chip description
  285. * return: the burstcount.
  286. */
  287. static int get_burstcount(struct tpm_chip *chip)
  288. {
  289. unsigned long stop;
  290. int burstcnt, status;
  291. u8 tpm_reg, temp;
  292. struct i2c_client *client = (struct i2c_client *)TPM_VPRIV(chip);
  293. stop = jiffies + chip->vendor.timeout_d;
  294. do {
  295. tpm_reg = TPM_STS + 1;
  296. status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
  297. if (status < 0)
  298. goto end;
  299. tpm_reg = tpm_reg + 1;
  300. burstcnt = temp;
  301. status = I2C_READ_DATA(client, tpm_reg, &temp, 1);
  302. if (status < 0)
  303. goto end;
  304. burstcnt |= temp << 8;
  305. if (burstcnt)
  306. return burstcnt;
  307. msleep(TPM_TIMEOUT);
  308. } while (time_before(jiffies, stop));
  309. end:
  310. return -EBUSY;
  311. } /* get_burstcount() */
  312. /*
  313. * wait_for_stat wait for a TPM_STS value
  314. * @param: chip, the tpm chip description
  315. * @param: mask, the value mask to wait
  316. * @param: timeout, the timeout
  317. * @param: queue, the wait queue.
  318. * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
  319. */
  320. static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  321. wait_queue_head_t *queue)
  322. {
  323. unsigned long stop;
  324. long rc;
  325. u8 status;
  326. if (chip->vendor.irq) {
  327. rc = wait_for_serirq_timeout(chip, ((tpm_stm_i2c_status
  328. (chip) & mask) ==
  329. mask), timeout);
  330. if (rc > 0)
  331. return 0;
  332. } else {
  333. stop = jiffies + timeout;
  334. do {
  335. msleep(TPM_TIMEOUT);
  336. status = tpm_stm_i2c_status(chip);
  337. if ((status & mask) == mask)
  338. return 0;
  339. } while (time_before(jiffies, stop));
  340. }
  341. return -ETIME;
  342. } /* wait_for_stat() */
  343. /*
  344. * recv_data receive data
  345. * @param: chip, the tpm chip description
  346. * @param: buf, the buffer where the data are received
  347. * @param: count, the number of data to receive
  348. * @return: the number of bytes read from TPM FIFO.
  349. */
  350. static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
  351. {
  352. int size = 0, burstcnt, len;
  353. struct i2c_client *client;
  354. client = (struct i2c_client *)TPM_VPRIV(chip);
  355. while (size < count &&
  356. wait_for_stat(chip,
  357. TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  358. chip->vendor.timeout_c,
  359. &chip->vendor.read_queue)
  360. == 0) {
  361. burstcnt = get_burstcount(chip);
  362. len = min_t(int, burstcnt, count - size);
  363. I2C_READ_DATA(client, TPM_DATA_FIFO, buf + size, len);
  364. size += len;
  365. }
  366. return size;
  367. }
  368. /*
  369. * tpm_ioserirq_handler the serirq irq handler
  370. * @param: irq, the tpm chip description
  371. * @param: dev_id, the description of the chip
  372. * @return: the status of the handler.
  373. */
  374. static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
  375. {
  376. struct tpm_chip *chip = dev_id;
  377. struct i2c_client *client;
  378. struct st33zp24_platform_data *pin_infos;
  379. disable_irq_nosync(irq);
  380. client = (struct i2c_client *)TPM_VPRIV(chip);
  381. pin_infos = client->dev.platform_data;
  382. complete(&pin_infos->irq_detection);
  383. return IRQ_HANDLED;
  384. } /* tpm_ioserirq_handler() */
  385. /*
  386. * tpm_stm_i2c_send send TPM commands through the I2C bus.
  387. *
  388. * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h
  389. * @param: buf, the buffer to send.
  390. * @param: count, the number of bytes to send.
  391. * @return: In case of success the number of bytes sent.
  392. * In other case, a < 0 value describing the issue.
  393. */
  394. static int tpm_stm_i2c_send(struct tpm_chip *chip, unsigned char *buf,
  395. size_t len)
  396. {
  397. u32 status, burstcnt = 0, i, size;
  398. int ret;
  399. u8 data;
  400. struct i2c_client *client;
  401. if (chip == NULL)
  402. return -EBUSY;
  403. if (len < TPM_HEADER_SIZE)
  404. return -EBUSY;
  405. client = (struct i2c_client *)TPM_VPRIV(chip);
  406. client->flags = 0;
  407. ret = request_locality(chip);
  408. if (ret < 0)
  409. return ret;
  410. status = tpm_stm_i2c_status(chip);
  411. if ((status & TPM_STS_COMMAND_READY) == 0) {
  412. tpm_stm_i2c_cancel(chip);
  413. if (wait_for_stat
  414. (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
  415. &chip->vendor.int_queue) < 0) {
  416. ret = -ETIME;
  417. goto out_err;
  418. }
  419. }
  420. for (i = 0; i < len - 1;) {
  421. burstcnt = get_burstcount(chip);
  422. size = min_t(int, len - i - 1, burstcnt);
  423. ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf, size);
  424. if (ret < 0)
  425. goto out_err;
  426. i += size;
  427. }
  428. status = tpm_stm_i2c_status(chip);
  429. if ((status & TPM_STS_DATA_EXPECT) == 0) {
  430. ret = -EIO;
  431. goto out_err;
  432. }
  433. ret = I2C_WRITE_DATA(client, TPM_DATA_FIFO, buf + len - 1, 1);
  434. if (ret < 0)
  435. goto out_err;
  436. status = tpm_stm_i2c_status(chip);
  437. if ((status & TPM_STS_DATA_EXPECT) != 0) {
  438. ret = -EIO;
  439. goto out_err;
  440. }
  441. data = TPM_STS_GO;
  442. I2C_WRITE_DATA(client, TPM_STS, &data, 1);
  443. return len;
  444. out_err:
  445. tpm_stm_i2c_cancel(chip);
  446. release_locality(chip);
  447. return ret;
  448. }
  449. /*
  450. * tpm_stm_i2c_recv received TPM response through the I2C bus.
  451. * @param: chip, the tpm_chip description as specified in driver/char/tpm/tpm.h.
  452. * @param: buf, the buffer to store datas.
  453. * @param: count, the number of bytes to send.
  454. * @return: In case of success the number of bytes received.
  455. * In other case, a < 0 value describing the issue.
  456. */
  457. static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
  458. size_t count)
  459. {
  460. int size = 0;
  461. int expected;
  462. if (chip == NULL)
  463. return -EBUSY;
  464. if (count < TPM_HEADER_SIZE) {
  465. size = -EIO;
  466. goto out;
  467. }
  468. size = recv_data(chip, buf, TPM_HEADER_SIZE);
  469. if (size < TPM_HEADER_SIZE) {
  470. dev_err(chip->dev, "Unable to read header\n");
  471. goto out;
  472. }
  473. expected = be32_to_cpu(*(__be32 *)(buf + 2));
  474. if (expected > count) {
  475. size = -EIO;
  476. goto out;
  477. }
  478. size += recv_data(chip, &buf[TPM_HEADER_SIZE],
  479. expected - TPM_HEADER_SIZE);
  480. if (size < expected) {
  481. dev_err(chip->dev, "Unable to read remainder of result\n");
  482. size = -ETIME;
  483. goto out;
  484. }
  485. out:
  486. chip->vendor.cancel(chip);
  487. release_locality(chip);
  488. return size;
  489. }
  490. static bool tpm_st33_i2c_req_canceled(struct tpm_chip *chip, u8 status)
  491. {
  492. return (status == TPM_STS_COMMAND_READY);
  493. }
  494. static const struct file_operations tpm_st33_i2c_fops = {
  495. .owner = THIS_MODULE,
  496. .llseek = no_llseek,
  497. .read = tpm_read,
  498. .write = tpm_write,
  499. .open = tpm_open,
  500. .release = tpm_release,
  501. };
  502. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  503. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  504. static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
  505. static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
  506. static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
  507. static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
  508. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
  509. static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
  510. static struct attribute *stm_tpm_attrs[] = {
  511. &dev_attr_pubek.attr,
  512. &dev_attr_pcrs.attr,
  513. &dev_attr_enabled.attr,
  514. &dev_attr_active.attr,
  515. &dev_attr_owned.attr,
  516. &dev_attr_temp_deactivated.attr,
  517. &dev_attr_caps.attr,
  518. &dev_attr_cancel.attr, NULL,
  519. };
  520. static struct attribute_group stm_tpm_attr_grp = {
  521. .attrs = stm_tpm_attrs
  522. };
  523. static struct tpm_vendor_specific st_i2c_tpm = {
  524. .send = tpm_stm_i2c_send,
  525. .recv = tpm_stm_i2c_recv,
  526. .cancel = tpm_stm_i2c_cancel,
  527. .status = tpm_stm_i2c_status,
  528. .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  529. .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
  530. .req_canceled = tpm_st33_i2c_req_canceled,
  531. .attr_group = &stm_tpm_attr_grp,
  532. .miscdev = {.fops = &tpm_st33_i2c_fops,},
  533. };
  534. static int interrupts;
  535. module_param(interrupts, int, 0444);
  536. MODULE_PARM_DESC(interrupts, "Enable interrupts");
  537. static int power_mgt = 1;
  538. module_param(power_mgt, int, 0444);
  539. MODULE_PARM_DESC(power_mgt, "Power Management");
  540. /*
  541. * tpm_st33_i2c_probe initialize the TPM device
  542. * @param: client, the i2c_client drescription (TPM I2C description).
  543. * @param: id, the i2c_device_id struct.
  544. * @return: 0 in case of success.
  545. * -1 in other case.
  546. */
  547. static int
  548. tpm_st33_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  549. {
  550. int err;
  551. u8 intmask;
  552. struct tpm_chip *chip;
  553. struct st33zp24_platform_data *platform_data;
  554. if (client == NULL) {
  555. pr_info("%s: i2c client is NULL. Device not accessible.\n",
  556. __func__);
  557. err = -ENODEV;
  558. goto end;
  559. }
  560. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  561. dev_info(&client->dev, "client not i2c capable\n");
  562. err = -ENODEV;
  563. goto end;
  564. }
  565. chip = tpm_register_hardware(&client->dev, &st_i2c_tpm);
  566. if (!chip) {
  567. dev_info(&client->dev, "fail chip\n");
  568. err = -ENODEV;
  569. goto end;
  570. }
  571. platform_data = client->dev.platform_data;
  572. if (!platform_data) {
  573. dev_info(&client->dev, "chip not available\n");
  574. err = -ENODEV;
  575. goto _tpm_clean_answer;
  576. }
  577. platform_data->tpm_i2c_buffer[0] =
  578. kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  579. if (platform_data->tpm_i2c_buffer[0] == NULL) {
  580. err = -ENOMEM;
  581. goto _tpm_clean_answer;
  582. }
  583. platform_data->tpm_i2c_buffer[1] =
  584. kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  585. if (platform_data->tpm_i2c_buffer[1] == NULL) {
  586. err = -ENOMEM;
  587. goto _tpm_clean_response1;
  588. }
  589. TPM_VPRIV(chip) = client;
  590. chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  591. chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
  592. chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  593. chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
  594. chip->vendor.locality = LOCALITY0;
  595. if (power_mgt) {
  596. err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
  597. if (err)
  598. goto _gpio_init1;
  599. gpio_set_value(platform_data->io_lpcpd, 1);
  600. }
  601. if (interrupts) {
  602. init_completion(&platform_data->irq_detection);
  603. if (request_locality(chip) != LOCALITY0) {
  604. err = -ENODEV;
  605. goto _tpm_clean_response2;
  606. }
  607. err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
  608. if (err)
  609. goto _gpio_init2;
  610. clear_interruption(client);
  611. err = request_irq(gpio_to_irq(platform_data->io_serirq),
  612. &tpm_ioserirq_handler,
  613. IRQF_TRIGGER_HIGH,
  614. "TPM SERIRQ management", chip);
  615. if (err < 0) {
  616. dev_err(chip->dev , "TPM SERIRQ signals %d not available\n",
  617. gpio_to_irq(platform_data->io_serirq));
  618. goto _irq_set;
  619. }
  620. err = I2C_READ_DATA(client, TPM_INT_ENABLE, &intmask, 1);
  621. if (err < 0)
  622. goto _irq_set;
  623. intmask |= TPM_INTF_CMD_READY_INT
  624. | TPM_INTF_FIFO_AVALAIBLE_INT
  625. | TPM_INTF_WAKE_UP_READY_INT
  626. | TPM_INTF_LOCALITY_CHANGE_INT
  627. | TPM_INTF_STS_VALID_INT
  628. | TPM_INTF_DATA_AVAIL_INT;
  629. err = I2C_WRITE_DATA(client, TPM_INT_ENABLE, &intmask, 1);
  630. if (err < 0)
  631. goto _irq_set;
  632. intmask = TPM_GLOBAL_INT_ENABLE;
  633. err = I2C_WRITE_DATA(client, (TPM_INT_ENABLE + 3), &intmask, 1);
  634. if (err < 0)
  635. goto _irq_set;
  636. err = I2C_READ_DATA(client, TPM_INT_STATUS, &intmask, 1);
  637. if (err < 0)
  638. goto _irq_set;
  639. chip->vendor.irq = interrupts;
  640. tpm_gen_interrupt(chip);
  641. }
  642. tpm_get_timeouts(chip);
  643. i2c_set_clientdata(client, chip);
  644. dev_info(chip->dev, "TPM I2C Initialized\n");
  645. return 0;
  646. _irq_set:
  647. free_irq(gpio_to_irq(platform_data->io_serirq), (void *)chip);
  648. _gpio_init2:
  649. if (interrupts)
  650. gpio_free(platform_data->io_serirq);
  651. _gpio_init1:
  652. if (power_mgt)
  653. gpio_free(platform_data->io_lpcpd);
  654. _tpm_clean_response2:
  655. kzfree(platform_data->tpm_i2c_buffer[1]);
  656. platform_data->tpm_i2c_buffer[1] = NULL;
  657. _tpm_clean_response1:
  658. kzfree(platform_data->tpm_i2c_buffer[0]);
  659. platform_data->tpm_i2c_buffer[0] = NULL;
  660. _tpm_clean_answer:
  661. tpm_remove_hardware(chip->dev);
  662. end:
  663. pr_info("TPM I2C initialisation fail\n");
  664. return err;
  665. }
  666. /*
  667. * tpm_st33_i2c_remove remove the TPM device
  668. * @param: client, the i2c_client drescription (TPM I2C description).
  669. clear_bit(0, &chip->is_open);
  670. * @return: 0 in case of success.
  671. */
  672. static int tpm_st33_i2c_remove(struct i2c_client *client)
  673. {
  674. struct tpm_chip *chip = (struct tpm_chip *)i2c_get_clientdata(client);
  675. struct st33zp24_platform_data *pin_infos =
  676. ((struct i2c_client *)TPM_VPRIV(chip))->dev.platform_data;
  677. if (pin_infos != NULL) {
  678. free_irq(pin_infos->io_serirq, chip);
  679. gpio_free(pin_infos->io_serirq);
  680. gpio_free(pin_infos->io_lpcpd);
  681. tpm_remove_hardware(chip->dev);
  682. if (pin_infos->tpm_i2c_buffer[1] != NULL) {
  683. kzfree(pin_infos->tpm_i2c_buffer[1]);
  684. pin_infos->tpm_i2c_buffer[1] = NULL;
  685. }
  686. if (pin_infos->tpm_i2c_buffer[0] != NULL) {
  687. kzfree(pin_infos->tpm_i2c_buffer[0]);
  688. pin_infos->tpm_i2c_buffer[0] = NULL;
  689. }
  690. }
  691. return 0;
  692. }
  693. #ifdef CONFIG_PM_SLEEP
  694. /*
  695. * tpm_st33_i2c_pm_suspend suspend the TPM device
  696. * Added: Work around when suspend and no tpm application is running, suspend
  697. * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
  698. * TPM core)
  699. * @param: client, the i2c_client drescription (TPM I2C description).
  700. * @param: mesg, the power management message.
  701. * @return: 0 in case of success.
  702. */
  703. static int tpm_st33_i2c_pm_suspend(struct device *dev)
  704. {
  705. struct tpm_chip *chip = dev_get_drvdata(dev);
  706. struct st33zp24_platform_data *pin_infos = dev->platform_data;
  707. int ret = 0;
  708. if (power_mgt) {
  709. gpio_set_value(pin_infos->io_lpcpd, 0);
  710. } else {
  711. if (chip->data_buffer == NULL)
  712. chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
  713. ret = tpm_pm_suspend(dev);
  714. }
  715. return ret;
  716. } /* tpm_st33_i2c_suspend() */
  717. /*
  718. * tpm_st33_i2c_pm_resume resume the TPM device
  719. * @param: client, the i2c_client drescription (TPM I2C description).
  720. * @return: 0 in case of success.
  721. */
  722. static int tpm_st33_i2c_pm_resume(struct device *dev)
  723. {
  724. struct tpm_chip *chip = dev_get_drvdata(dev);
  725. struct st33zp24_platform_data *pin_infos = dev->platform_data;
  726. int ret = 0;
  727. if (power_mgt) {
  728. gpio_set_value(pin_infos->io_lpcpd, 1);
  729. ret = wait_for_serirq_timeout(chip,
  730. (chip->vendor.status(chip) &
  731. TPM_STS_VALID) == TPM_STS_VALID,
  732. chip->vendor.timeout_b);
  733. } else {
  734. if (chip->data_buffer == NULL)
  735. chip->data_buffer = pin_infos->tpm_i2c_buffer[0];
  736. ret = tpm_pm_resume(dev);
  737. if (!ret)
  738. tpm_do_selftest(chip);
  739. }
  740. return ret;
  741. } /* tpm_st33_i2c_pm_resume() */
  742. #endif
  743. static const struct i2c_device_id tpm_st33_i2c_id[] = {
  744. {TPM_ST33_I2C, 0},
  745. {}
  746. };
  747. MODULE_DEVICE_TABLE(i2c, tpm_st33_i2c_id);
  748. static SIMPLE_DEV_PM_OPS(tpm_st33_i2c_ops, tpm_st33_i2c_pm_suspend,
  749. tpm_st33_i2c_pm_resume);
  750. static struct i2c_driver tpm_st33_i2c_driver = {
  751. .driver = {
  752. .owner = THIS_MODULE,
  753. .name = TPM_ST33_I2C,
  754. .pm = &tpm_st33_i2c_ops,
  755. },
  756. .probe = tpm_st33_i2c_probe,
  757. .remove = tpm_st33_i2c_remove,
  758. .id_table = tpm_st33_i2c_id
  759. };
  760. module_i2c_driver(tpm_st33_i2c_driver);
  761. MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
  762. MODULE_DESCRIPTION("STM TPM I2C ST33 Driver");
  763. MODULE_VERSION("1.2.0");
  764. MODULE_LICENSE("GPL");