tpm_i2c_stm_st33.c 23 KB

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