tpm_infineon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Description:
  3. * Device Driver for the Infineon Technologies
  4. * SLD 9630 TT 1.1 and SLB 9635 TT 1.2 Trusted Platform Module
  5. * Specifications at www.trustedcomputinggroup.org
  6. *
  7. * Copyright (C) 2005, Marcel Selhorst <selhorst@crypto.rub.de>
  8. * Sirrix AG - security technologies, http://www.sirrix.com and
  9. * Applied Data Security Group, Ruhr-University Bochum, Germany
  10. * Project-Homepage: http://www.prosec.rub.de/tpm
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation, version 2 of the
  15. * License.
  16. */
  17. #include <linux/pnp.h>
  18. #include "tpm.h"
  19. /* Infineon specific definitions */
  20. /* maximum number of WTX-packages */
  21. #define TPM_MAX_WTX_PACKAGES 50
  22. /* msleep-Time for WTX-packages */
  23. #define TPM_WTX_MSLEEP_TIME 20
  24. /* msleep-Time --> Interval to check status register */
  25. #define TPM_MSLEEP_TIME 3
  26. /* gives number of max. msleep()-calls before throwing timeout */
  27. #define TPM_MAX_TRIES 5000
  28. #define TPM_INFINEON_DEV_VEN_VALUE 0x15D1
  29. /* These values will be filled after PnP-call */
  30. static int TPM_INF_DATA;
  31. static int TPM_INF_ADDR;
  32. static int TPM_INF_BASE;
  33. static int TPM_INF_ADDR_LEN;
  34. static int TPM_INF_PORT_LEN;
  35. /* TPM header definitions */
  36. enum infineon_tpm_header {
  37. TPM_VL_VER = 0x01,
  38. TPM_VL_CHANNEL_CONTROL = 0x07,
  39. TPM_VL_CHANNEL_PERSONALISATION = 0x0A,
  40. TPM_VL_CHANNEL_TPM = 0x0B,
  41. TPM_VL_CONTROL = 0x00,
  42. TPM_INF_NAK = 0x15,
  43. TPM_CTRL_WTX = 0x10,
  44. TPM_CTRL_WTX_ABORT = 0x18,
  45. TPM_CTRL_WTX_ABORT_ACK = 0x18,
  46. TPM_CTRL_ERROR = 0x20,
  47. TPM_CTRL_CHAININGACK = 0x40,
  48. TPM_CTRL_CHAINING = 0x80,
  49. TPM_CTRL_DATA = 0x04,
  50. TPM_CTRL_DATA_CHA = 0x84,
  51. TPM_CTRL_DATA_CHA_ACK = 0xC4
  52. };
  53. enum infineon_tpm_register {
  54. WRFIFO = 0x00,
  55. RDFIFO = 0x01,
  56. STAT = 0x02,
  57. CMD = 0x03
  58. };
  59. enum infineon_tpm_command_bits {
  60. CMD_DIS = 0x00,
  61. CMD_LP = 0x01,
  62. CMD_RES = 0x02,
  63. CMD_IRQC = 0x06
  64. };
  65. enum infineon_tpm_status_bits {
  66. STAT_XFE = 0x00,
  67. STAT_LPA = 0x01,
  68. STAT_FOK = 0x02,
  69. STAT_TOK = 0x03,
  70. STAT_IRQA = 0x06,
  71. STAT_RDA = 0x07
  72. };
  73. /* some outgoing values */
  74. enum infineon_tpm_values {
  75. CHIP_ID1 = 0x20,
  76. CHIP_ID2 = 0x21,
  77. TPM_DAR = 0x30,
  78. RESET_LP_IRQC_DISABLE = 0x41,
  79. ENABLE_REGISTER_PAIR = 0x55,
  80. IOLIMH = 0x60,
  81. IOLIML = 0x61,
  82. DISABLE_REGISTER_PAIR = 0xAA,
  83. IDVENL = 0xF1,
  84. IDVENH = 0xF2,
  85. IDPDL = 0xF3,
  86. IDPDH = 0xF4
  87. };
  88. static int number_of_wtx;
  89. static int empty_fifo(struct tpm_chip *chip, int clear_wrfifo)
  90. {
  91. int status;
  92. int check = 0;
  93. int i;
  94. if (clear_wrfifo) {
  95. for (i = 0; i < 4096; i++) {
  96. status = inb(chip->vendor->base + WRFIFO);
  97. if (status == 0xff) {
  98. if (check == 5)
  99. break;
  100. else
  101. check++;
  102. }
  103. }
  104. }
  105. /* Note: The values which are currently in the FIFO of the TPM
  106. are thrown away since there is no usage for them. Usually,
  107. this has nothing to say, since the TPM will give its answer
  108. immediately or will be aborted anyway, so the data here is
  109. usually garbage and useless.
  110. We have to clean this, because the next communication with
  111. the TPM would be rubbish, if there is still some old data
  112. in the Read FIFO.
  113. */
  114. i = 0;
  115. do {
  116. status = inb(chip->vendor->base + RDFIFO);
  117. status = inb(chip->vendor->base + STAT);
  118. i++;
  119. if (i == TPM_MAX_TRIES)
  120. return -EIO;
  121. } while ((status & (1 << STAT_RDA)) != 0);
  122. return 0;
  123. }
  124. static int wait(struct tpm_chip *chip, int wait_for_bit)
  125. {
  126. int status;
  127. int i;
  128. for (i = 0; i < TPM_MAX_TRIES; i++) {
  129. status = inb(chip->vendor->base + STAT);
  130. /* check the status-register if wait_for_bit is set */
  131. if (status & 1 << wait_for_bit)
  132. break;
  133. msleep(TPM_MSLEEP_TIME);
  134. }
  135. if (i == TPM_MAX_TRIES) { /* timeout occurs */
  136. if (wait_for_bit == STAT_XFE)
  137. dev_err(chip->dev, "Timeout in wait(STAT_XFE)\n");
  138. if (wait_for_bit == STAT_RDA)
  139. dev_err(chip->dev, "Timeout in wait(STAT_RDA)\n");
  140. return -EIO;
  141. }
  142. return 0;
  143. };
  144. static void wait_and_send(struct tpm_chip *chip, u8 sendbyte)
  145. {
  146. wait(chip, STAT_XFE);
  147. outb(sendbyte, chip->vendor->base + WRFIFO);
  148. }
  149. /* Note: WTX means Waiting-Time-Extension. Whenever the TPM needs more
  150. calculation time, it sends a WTX-package, which has to be acknowledged
  151. or aborted. This usually occurs if you are hammering the TPM with key
  152. creation. Set the maximum number of WTX-packages in the definitions
  153. above, if the number is reached, the waiting-time will be denied
  154. and the TPM command has to be resend.
  155. */
  156. static void tpm_wtx(struct tpm_chip *chip)
  157. {
  158. number_of_wtx++;
  159. dev_info(chip->dev, "Granting WTX (%02d / %02d)\n",
  160. number_of_wtx, TPM_MAX_WTX_PACKAGES);
  161. wait_and_send(chip, TPM_VL_VER);
  162. wait_and_send(chip, TPM_CTRL_WTX);
  163. wait_and_send(chip, 0x00);
  164. wait_and_send(chip, 0x00);
  165. msleep(TPM_WTX_MSLEEP_TIME);
  166. }
  167. static void tpm_wtx_abort(struct tpm_chip *chip)
  168. {
  169. dev_info(chip->dev, "Aborting WTX\n");
  170. wait_and_send(chip, TPM_VL_VER);
  171. wait_and_send(chip, TPM_CTRL_WTX_ABORT);
  172. wait_and_send(chip, 0x00);
  173. wait_and_send(chip, 0x00);
  174. number_of_wtx = 0;
  175. msleep(TPM_WTX_MSLEEP_TIME);
  176. }
  177. static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
  178. {
  179. int i;
  180. int ret;
  181. u32 size = 0;
  182. number_of_wtx = 0;
  183. recv_begin:
  184. /* start receiving header */
  185. for (i = 0; i < 4; i++) {
  186. ret = wait(chip, STAT_RDA);
  187. if (ret)
  188. return -EIO;
  189. buf[i] = inb(chip->vendor->base + RDFIFO);
  190. }
  191. if (buf[0] != TPM_VL_VER) {
  192. dev_err(chip->dev,
  193. "Wrong transport protocol implementation!\n");
  194. return -EIO;
  195. }
  196. if (buf[1] == TPM_CTRL_DATA) {
  197. /* size of the data received */
  198. size = ((buf[2] << 8) | buf[3]);
  199. for (i = 0; i < size; i++) {
  200. wait(chip, STAT_RDA);
  201. buf[i] = inb(chip->vendor->base + RDFIFO);
  202. }
  203. if ((size == 0x6D00) && (buf[1] == 0x80)) {
  204. dev_err(chip->dev, "Error handling on vendor layer!\n");
  205. return -EIO;
  206. }
  207. for (i = 0; i < size; i++)
  208. buf[i] = buf[i + 6];
  209. size = size - 6;
  210. return size;
  211. }
  212. if (buf[1] == TPM_CTRL_WTX) {
  213. dev_info(chip->dev, "WTX-package received\n");
  214. if (number_of_wtx < TPM_MAX_WTX_PACKAGES) {
  215. tpm_wtx(chip);
  216. goto recv_begin;
  217. } else {
  218. tpm_wtx_abort(chip);
  219. goto recv_begin;
  220. }
  221. }
  222. if (buf[1] == TPM_CTRL_WTX_ABORT_ACK) {
  223. dev_info(chip->dev, "WTX-abort acknowledged\n");
  224. return size;
  225. }
  226. if (buf[1] == TPM_CTRL_ERROR) {
  227. dev_err(chip->dev, "ERROR-package received:\n");
  228. if (buf[4] == TPM_INF_NAK)
  229. dev_err(chip->dev,
  230. "-> Negative acknowledgement"
  231. " - retransmit command!\n");
  232. return -EIO;
  233. }
  234. return -EIO;
  235. }
  236. static int tpm_inf_send(struct tpm_chip *chip, u8 * buf, size_t count)
  237. {
  238. int i;
  239. int ret;
  240. u8 count_high, count_low, count_4, count_3, count_2, count_1;
  241. /* Disabling Reset, LP and IRQC */
  242. outb(RESET_LP_IRQC_DISABLE, chip->vendor->base + CMD);
  243. ret = empty_fifo(chip, 1);
  244. if (ret) {
  245. dev_err(chip->dev, "Timeout while clearing FIFO\n");
  246. return -EIO;
  247. }
  248. ret = wait(chip, STAT_XFE);
  249. if (ret)
  250. return -EIO;
  251. count_4 = (count & 0xff000000) >> 24;
  252. count_3 = (count & 0x00ff0000) >> 16;
  253. count_2 = (count & 0x0000ff00) >> 8;
  254. count_1 = (count & 0x000000ff);
  255. count_high = ((count + 6) & 0xffffff00) >> 8;
  256. count_low = ((count + 6) & 0x000000ff);
  257. /* Sending Header */
  258. wait_and_send(chip, TPM_VL_VER);
  259. wait_and_send(chip, TPM_CTRL_DATA);
  260. wait_and_send(chip, count_high);
  261. wait_and_send(chip, count_low);
  262. /* Sending Data Header */
  263. wait_and_send(chip, TPM_VL_VER);
  264. wait_and_send(chip, TPM_VL_CHANNEL_TPM);
  265. wait_and_send(chip, count_4);
  266. wait_and_send(chip, count_3);
  267. wait_and_send(chip, count_2);
  268. wait_and_send(chip, count_1);
  269. /* Sending Data */
  270. for (i = 0; i < count; i++) {
  271. wait_and_send(chip, buf[i]);
  272. }
  273. return count;
  274. }
  275. static void tpm_inf_cancel(struct tpm_chip *chip)
  276. {
  277. /*
  278. Since we are using the legacy mode to communicate
  279. with the TPM, we have no cancel functions, but have
  280. a workaround for interrupting the TPM through WTX.
  281. */
  282. }
  283. static u8 tpm_inf_status(struct tpm_chip *chip)
  284. {
  285. return inb(chip->vendor->base + STAT);
  286. }
  287. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  288. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  289. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
  290. static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
  291. static struct attribute *inf_attrs[] = {
  292. &dev_attr_pubek.attr,
  293. &dev_attr_pcrs.attr,
  294. &dev_attr_caps.attr,
  295. &dev_attr_cancel.attr,
  296. NULL,
  297. };
  298. static struct attribute_group inf_attr_grp = {.attrs = inf_attrs };
  299. static struct file_operations inf_ops = {
  300. .owner = THIS_MODULE,
  301. .llseek = no_llseek,
  302. .open = tpm_open,
  303. .read = tpm_read,
  304. .write = tpm_write,
  305. .release = tpm_release,
  306. };
  307. static struct tpm_vendor_specific tpm_inf = {
  308. .recv = tpm_inf_recv,
  309. .send = tpm_inf_send,
  310. .cancel = tpm_inf_cancel,
  311. .status = tpm_inf_status,
  312. .req_complete_mask = 0,
  313. .req_complete_val = 0,
  314. .attr_group = &inf_attr_grp,
  315. .miscdev = {.fops = &inf_ops,},
  316. };
  317. static const struct pnp_device_id tpm_pnp_tbl[] = {
  318. /* Infineon TPMs */
  319. {"IFX0101", 0},
  320. {"IFX0102", 0},
  321. {"", 0}
  322. };
  323. MODULE_DEVICE_TABLE(pnp, tpm_pnp_tbl);
  324. static int __devinit tpm_inf_pnp_probe(struct pnp_dev *dev,
  325. const struct pnp_device_id *dev_id)
  326. {
  327. int rc = 0;
  328. u8 iol, ioh;
  329. int vendorid[2];
  330. int version[2];
  331. int productid[2];
  332. char chipname[20];
  333. /* read IO-ports through PnP */
  334. if (pnp_port_valid(dev, 0) && pnp_port_valid(dev, 1) &&
  335. !(pnp_port_flags(dev, 0) & IORESOURCE_DISABLED)) {
  336. TPM_INF_ADDR = pnp_port_start(dev, 0);
  337. TPM_INF_ADDR_LEN = pnp_port_len(dev, 0);
  338. TPM_INF_DATA = (TPM_INF_ADDR + 1);
  339. TPM_INF_BASE = pnp_port_start(dev, 1);
  340. TPM_INF_PORT_LEN = pnp_port_len(dev, 1);
  341. if ((TPM_INF_PORT_LEN < 4) || (TPM_INF_ADDR_LEN < 2)) {
  342. rc = -EINVAL;
  343. goto err_last;
  344. }
  345. dev_info(&dev->dev, "Found %s with ID %s\n",
  346. dev->name, dev_id->id);
  347. if (!((TPM_INF_BASE >> 8) & 0xff)) {
  348. rc = -EINVAL;
  349. goto err_last;
  350. }
  351. /* publish my base address and request region */
  352. tpm_inf.base = TPM_INF_BASE;
  353. if (request_region
  354. (tpm_inf.base, TPM_INF_PORT_LEN, "tpm_infineon0") == NULL) {
  355. rc = -EINVAL;
  356. goto err_last;
  357. }
  358. if (request_region(TPM_INF_ADDR, TPM_INF_ADDR_LEN,
  359. "tpm_infineon0") == NULL) {
  360. rc = -EINVAL;
  361. goto err_last;
  362. }
  363. } else {
  364. rc = -EINVAL;
  365. goto err_last;
  366. }
  367. /* query chip for its vendor, its version number a.s.o. */
  368. outb(ENABLE_REGISTER_PAIR, TPM_INF_ADDR);
  369. outb(IDVENL, TPM_INF_ADDR);
  370. vendorid[1] = inb(TPM_INF_DATA);
  371. outb(IDVENH, TPM_INF_ADDR);
  372. vendorid[0] = inb(TPM_INF_DATA);
  373. outb(IDPDL, TPM_INF_ADDR);
  374. productid[1] = inb(TPM_INF_DATA);
  375. outb(IDPDH, TPM_INF_ADDR);
  376. productid[0] = inb(TPM_INF_DATA);
  377. outb(CHIP_ID1, TPM_INF_ADDR);
  378. version[1] = inb(TPM_INF_DATA);
  379. outb(CHIP_ID2, TPM_INF_ADDR);
  380. version[0] = inb(TPM_INF_DATA);
  381. switch ((productid[0] << 8) | productid[1]) {
  382. case 6:
  383. snprintf(chipname, sizeof(chipname), " (SLD 9630 TT 1.1)");
  384. break;
  385. case 11:
  386. snprintf(chipname, sizeof(chipname), " (SLB 9635 TT 1.2)");
  387. break;
  388. default:
  389. snprintf(chipname, sizeof(chipname), " (unknown chip)");
  390. break;
  391. }
  392. if ((vendorid[0] << 8 | vendorid[1]) == (TPM_INFINEON_DEV_VEN_VALUE)) {
  393. /* configure TPM with IO-ports */
  394. outb(IOLIMH, TPM_INF_ADDR);
  395. outb(((tpm_inf.base >> 8) & 0xff), TPM_INF_DATA);
  396. outb(IOLIML, TPM_INF_ADDR);
  397. outb((tpm_inf.base & 0xff), TPM_INF_DATA);
  398. /* control if IO-ports are set correctly */
  399. outb(IOLIMH, TPM_INF_ADDR);
  400. ioh = inb(TPM_INF_DATA);
  401. outb(IOLIML, TPM_INF_ADDR);
  402. iol = inb(TPM_INF_DATA);
  403. if ((ioh << 8 | iol) != tpm_inf.base) {
  404. dev_err(&dev->dev,
  405. "Could not set IO-ports to 0x%lx\n",
  406. tpm_inf.base);
  407. rc = -EIO;
  408. goto err_release_region;
  409. }
  410. /* activate register */
  411. outb(TPM_DAR, TPM_INF_ADDR);
  412. outb(0x01, TPM_INF_DATA);
  413. outb(DISABLE_REGISTER_PAIR, TPM_INF_ADDR);
  414. /* disable RESET, LP and IRQC */
  415. outb(RESET_LP_IRQC_DISABLE, tpm_inf.base + CMD);
  416. /* Finally, we're done, print some infos */
  417. dev_info(&dev->dev, "TPM found: "
  418. "config base 0x%x, "
  419. "io base 0x%x, "
  420. "chip version %02x%02x, "
  421. "vendor id %x%x (Infineon), "
  422. "product id %02x%02x"
  423. "%s\n",
  424. TPM_INF_ADDR,
  425. TPM_INF_BASE,
  426. version[0], version[1],
  427. vendorid[0], vendorid[1],
  428. productid[0], productid[1], chipname);
  429. rc = tpm_register_hardware(&dev->dev, &tpm_inf);
  430. if (rc < 0) {
  431. rc = -ENODEV;
  432. goto err_release_region;
  433. }
  434. return 0;
  435. } else {
  436. rc = -ENODEV;
  437. goto err_release_region;
  438. }
  439. err_release_region:
  440. release_region(tpm_inf.base, TPM_INF_PORT_LEN);
  441. release_region(TPM_INF_ADDR, TPM_INF_ADDR_LEN);
  442. err_last:
  443. return rc;
  444. }
  445. static __devexit void tpm_inf_pnp_remove(struct pnp_dev *dev)
  446. {
  447. struct tpm_chip *chip = pnp_get_drvdata(dev);
  448. if (chip) {
  449. release_region(chip->vendor->base, TPM_INF_PORT_LEN);
  450. tpm_remove_hardware(chip->dev);
  451. }
  452. }
  453. static struct pnp_driver tpm_inf_pnp = {
  454. .name = "tpm_inf_pnp",
  455. .driver = {
  456. .owner = THIS_MODULE,
  457. .suspend = tpm_pm_suspend,
  458. .resume = tpm_pm_resume,
  459. },
  460. .id_table = tpm_pnp_tbl,
  461. .probe = tpm_inf_pnp_probe,
  462. .remove = tpm_inf_pnp_remove,
  463. };
  464. static int __init init_inf(void)
  465. {
  466. return pnp_register_driver(&tpm_inf_pnp);
  467. }
  468. static void __exit cleanup_inf(void)
  469. {
  470. pnp_unregister_driver(&tpm_inf_pnp);
  471. }
  472. module_init(init_inf);
  473. module_exit(cleanup_inf);
  474. MODULE_AUTHOR("Marcel Selhorst <selhorst@crypto.rub.de>");
  475. MODULE_DESCRIPTION("Driver for Infineon TPM SLD 9630 TT 1.1 / SLB 9635 TT 1.2");
  476. MODULE_VERSION("1.7");
  477. MODULE_LICENSE("GPL");