tpm_i2c_stm_st33.c 23 KB

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