tpm_stm_st33_i2c.c 24 KB

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