tpm_i2c_stm_st33.c 23 KB

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