tpm_nsc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. *
  4. * Authors:
  5. * Leendert van Doorn <leendert@watson.ibm.com>
  6. * Dave Safford <safford@watson.ibm.com>
  7. * Reiner Sailer <sailer@watson.ibm.com>
  8. * Kylene Hall <kjhall@us.ibm.com>
  9. *
  10. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  11. *
  12. * Device driver for TCG/TCPA TPM (trusted platform module).
  13. * Specifications at www.trustedcomputinggroup.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation, version 2 of the
  18. * License.
  19. *
  20. */
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include "tpm.h"
  24. /* National definitions */
  25. enum tpm_nsc_addr{
  26. TPM_NSC_IRQ = 0x07,
  27. TPM_NSC_BASE0_HI = 0x60,
  28. TPM_NSC_BASE0_LO = 0x61,
  29. TPM_NSC_BASE1_HI = 0x62,
  30. TPM_NSC_BASE1_LO = 0x63
  31. };
  32. enum tpm_nsc_index {
  33. NSC_LDN_INDEX = 0x07,
  34. NSC_SID_INDEX = 0x20,
  35. NSC_LDC_INDEX = 0x30,
  36. NSC_DIO_INDEX = 0x60,
  37. NSC_CIO_INDEX = 0x62,
  38. NSC_IRQ_INDEX = 0x70,
  39. NSC_ITS_INDEX = 0x71
  40. };
  41. enum tpm_nsc_status_loc {
  42. NSC_STATUS = 0x01,
  43. NSC_COMMAND = 0x01,
  44. NSC_DATA = 0x00
  45. };
  46. /* status bits */
  47. enum tpm_nsc_status {
  48. NSC_STATUS_OBF = 0x01, /* output buffer full */
  49. NSC_STATUS_IBF = 0x02, /* input buffer full */
  50. NSC_STATUS_F0 = 0x04, /* F0 */
  51. NSC_STATUS_A2 = 0x08, /* A2 */
  52. NSC_STATUS_RDY = 0x10, /* ready to receive command */
  53. NSC_STATUS_IBR = 0x20 /* ready to receive data */
  54. };
  55. /* command bits */
  56. enum tpm_nsc_cmd_mode {
  57. NSC_COMMAND_NORMAL = 0x01, /* normal mode */
  58. NSC_COMMAND_EOC = 0x03,
  59. NSC_COMMAND_CANCEL = 0x22
  60. };
  61. /*
  62. * Wait for a certain status to appear
  63. */
  64. static int wait_for_stat(struct tpm_chip *chip, u8 mask, u8 val, u8 * data)
  65. {
  66. unsigned long stop;
  67. /* status immediately available check */
  68. *data = inb(chip->vendor.base + NSC_STATUS);
  69. if ((*data & mask) == val)
  70. return 0;
  71. /* wait for status */
  72. stop = jiffies + 10 * HZ;
  73. do {
  74. msleep(TPM_TIMEOUT);
  75. *data = inb(chip->vendor.base + 1);
  76. if ((*data & mask) == val)
  77. return 0;
  78. }
  79. while (time_before(jiffies, stop));
  80. return -EBUSY;
  81. }
  82. static int nsc_wait_for_ready(struct tpm_chip *chip)
  83. {
  84. int status;
  85. unsigned long stop;
  86. /* status immediately available check */
  87. status = inb(chip->vendor.base + NSC_STATUS);
  88. if (status & NSC_STATUS_OBF)
  89. status = inb(chip->vendor.base + NSC_DATA);
  90. if (status & NSC_STATUS_RDY)
  91. return 0;
  92. /* wait for status */
  93. stop = jiffies + 100;
  94. do {
  95. msleep(TPM_TIMEOUT);
  96. status = inb(chip->vendor.base + NSC_STATUS);
  97. if (status & NSC_STATUS_OBF)
  98. status = inb(chip->vendor.base + NSC_DATA);
  99. if (status & NSC_STATUS_RDY)
  100. return 0;
  101. }
  102. while (time_before(jiffies, stop));
  103. dev_info(&chip->dev, "wait for ready failed\n");
  104. return -EBUSY;
  105. }
  106. static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
  107. {
  108. u8 *buffer = buf;
  109. u8 data, *p;
  110. u32 size;
  111. __be32 *native_size;
  112. if (count < 6)
  113. return -EIO;
  114. if (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0) {
  115. dev_err(&chip->dev, "F0 timeout\n");
  116. return -EIO;
  117. }
  118. if ((data =
  119. inb(chip->vendor.base + NSC_DATA)) != NSC_COMMAND_NORMAL) {
  120. dev_err(&chip->dev, "not in normal mode (0x%x)\n",
  121. data);
  122. return -EIO;
  123. }
  124. /* read the whole packet */
  125. for (p = buffer; p < &buffer[count]; p++) {
  126. if (wait_for_stat
  127. (chip, NSC_STATUS_OBF, NSC_STATUS_OBF, &data) < 0) {
  128. dev_err(&chip->dev,
  129. "OBF timeout (while reading data)\n");
  130. return -EIO;
  131. }
  132. if (data & NSC_STATUS_F0)
  133. break;
  134. *p = inb(chip->vendor.base + NSC_DATA);
  135. }
  136. if ((data & NSC_STATUS_F0) == 0 &&
  137. (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0)) {
  138. dev_err(&chip->dev, "F0 not set\n");
  139. return -EIO;
  140. }
  141. if ((data = inb(chip->vendor.base + NSC_DATA)) != NSC_COMMAND_EOC) {
  142. dev_err(&chip->dev,
  143. "expected end of command(0x%x)\n", data);
  144. return -EIO;
  145. }
  146. native_size = (__force __be32 *) (buf + 2);
  147. size = be32_to_cpu(*native_size);
  148. if (count < size)
  149. return -EIO;
  150. return size;
  151. }
  152. static int tpm_nsc_send(struct tpm_chip *chip, u8 * buf, size_t count)
  153. {
  154. u8 data;
  155. int i;
  156. /*
  157. * If we hit the chip with back to back commands it locks up
  158. * and never set IBF. Hitting it with this "hammer" seems to
  159. * fix it. Not sure why this is needed, we followed the flow
  160. * chart in the manual to the letter.
  161. */
  162. outb(NSC_COMMAND_CANCEL, chip->vendor.base + NSC_COMMAND);
  163. if (nsc_wait_for_ready(chip) != 0)
  164. return -EIO;
  165. if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
  166. dev_err(&chip->dev, "IBF timeout\n");
  167. return -EIO;
  168. }
  169. outb(NSC_COMMAND_NORMAL, chip->vendor.base + NSC_COMMAND);
  170. if (wait_for_stat(chip, NSC_STATUS_IBR, NSC_STATUS_IBR, &data) < 0) {
  171. dev_err(&chip->dev, "IBR timeout\n");
  172. return -EIO;
  173. }
  174. for (i = 0; i < count; i++) {
  175. if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
  176. dev_err(&chip->dev,
  177. "IBF timeout (while writing data)\n");
  178. return -EIO;
  179. }
  180. outb(buf[i], chip->vendor.base + NSC_DATA);
  181. }
  182. if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
  183. dev_err(&chip->dev, "IBF timeout\n");
  184. return -EIO;
  185. }
  186. outb(NSC_COMMAND_EOC, chip->vendor.base + NSC_COMMAND);
  187. return count;
  188. }
  189. static void tpm_nsc_cancel(struct tpm_chip *chip)
  190. {
  191. outb(NSC_COMMAND_CANCEL, chip->vendor.base + NSC_COMMAND);
  192. }
  193. static u8 tpm_nsc_status(struct tpm_chip *chip)
  194. {
  195. return inb(chip->vendor.base + NSC_STATUS);
  196. }
  197. static bool tpm_nsc_req_canceled(struct tpm_chip *chip, u8 status)
  198. {
  199. return (status == NSC_STATUS_RDY);
  200. }
  201. static const struct tpm_class_ops tpm_nsc = {
  202. .recv = tpm_nsc_recv,
  203. .send = tpm_nsc_send,
  204. .cancel = tpm_nsc_cancel,
  205. .status = tpm_nsc_status,
  206. .req_complete_mask = NSC_STATUS_OBF,
  207. .req_complete_val = NSC_STATUS_OBF,
  208. .req_canceled = tpm_nsc_req_canceled,
  209. };
  210. static struct platform_device *pdev = NULL;
  211. static void tpm_nsc_remove(struct device *dev)
  212. {
  213. struct tpm_chip *chip = dev_get_drvdata(dev);
  214. tpm_chip_unregister(chip);
  215. release_region(chip->vendor.base, 2);
  216. }
  217. static SIMPLE_DEV_PM_OPS(tpm_nsc_pm, tpm_pm_suspend, tpm_pm_resume);
  218. static struct platform_driver nsc_drv = {
  219. .driver = {
  220. .name = "tpm_nsc",
  221. .pm = &tpm_nsc_pm,
  222. },
  223. };
  224. static int __init init_nsc(void)
  225. {
  226. int rc = 0;
  227. int lo, hi, err;
  228. int nscAddrBase = TPM_ADDR;
  229. struct tpm_chip *chip;
  230. unsigned long base;
  231. /* verify that it is a National part (SID) */
  232. if (tpm_read_index(TPM_ADDR, NSC_SID_INDEX) != 0xEF) {
  233. nscAddrBase = (tpm_read_index(TPM_SUPERIO_ADDR, 0x2C)<<8)|
  234. (tpm_read_index(TPM_SUPERIO_ADDR, 0x2B)&0xFE);
  235. if (tpm_read_index(nscAddrBase, NSC_SID_INDEX) != 0xF6)
  236. return -ENODEV;
  237. }
  238. err = platform_driver_register(&nsc_drv);
  239. if (err)
  240. return err;
  241. hi = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_HI);
  242. lo = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_LO);
  243. base = (hi<<8) | lo;
  244. /* enable the DPM module */
  245. tpm_write_index(nscAddrBase, NSC_LDC_INDEX, 0x01);
  246. pdev = platform_device_alloc("tpm_nscl0", -1);
  247. if (!pdev) {
  248. rc = -ENOMEM;
  249. goto err_unreg_drv;
  250. }
  251. pdev->num_resources = 0;
  252. pdev->dev.driver = &nsc_drv.driver;
  253. pdev->dev.release = tpm_nsc_remove;
  254. if ((rc = platform_device_add(pdev)) < 0)
  255. goto err_put_dev;
  256. if (request_region(base, 2, "tpm_nsc0") == NULL ) {
  257. rc = -EBUSY;
  258. goto err_del_dev;
  259. }
  260. chip = tpmm_chip_alloc(&pdev->dev, &tpm_nsc);
  261. if (IS_ERR(chip)) {
  262. rc = -ENODEV;
  263. goto err_rel_reg;
  264. }
  265. rc = tpm_chip_register(chip);
  266. if (rc)
  267. goto err_rel_reg;
  268. dev_dbg(&pdev->dev, "NSC TPM detected\n");
  269. dev_dbg(&pdev->dev,
  270. "NSC LDN 0x%x, SID 0x%x, SRID 0x%x\n",
  271. tpm_read_index(nscAddrBase,0x07), tpm_read_index(nscAddrBase,0x20),
  272. tpm_read_index(nscAddrBase,0x27));
  273. dev_dbg(&pdev->dev,
  274. "NSC SIOCF1 0x%x SIOCF5 0x%x SIOCF6 0x%x SIOCF8 0x%x\n",
  275. tpm_read_index(nscAddrBase,0x21), tpm_read_index(nscAddrBase,0x25),
  276. tpm_read_index(nscAddrBase,0x26), tpm_read_index(nscAddrBase,0x28));
  277. dev_dbg(&pdev->dev, "NSC IO Base0 0x%x\n",
  278. (tpm_read_index(nscAddrBase,0x60) << 8) | tpm_read_index(nscAddrBase,0x61));
  279. dev_dbg(&pdev->dev, "NSC IO Base1 0x%x\n",
  280. (tpm_read_index(nscAddrBase,0x62) << 8) | tpm_read_index(nscAddrBase,0x63));
  281. dev_dbg(&pdev->dev, "NSC Interrupt number and wakeup 0x%x\n",
  282. tpm_read_index(nscAddrBase,0x70));
  283. dev_dbg(&pdev->dev, "NSC IRQ type select 0x%x\n",
  284. tpm_read_index(nscAddrBase,0x71));
  285. dev_dbg(&pdev->dev,
  286. "NSC DMA channel select0 0x%x, select1 0x%x\n",
  287. tpm_read_index(nscAddrBase,0x74), tpm_read_index(nscAddrBase,0x75));
  288. dev_dbg(&pdev->dev,
  289. "NSC Config "
  290. "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  291. tpm_read_index(nscAddrBase,0xF0), tpm_read_index(nscAddrBase,0xF1),
  292. tpm_read_index(nscAddrBase,0xF2), tpm_read_index(nscAddrBase,0xF3),
  293. tpm_read_index(nscAddrBase,0xF4), tpm_read_index(nscAddrBase,0xF5),
  294. tpm_read_index(nscAddrBase,0xF6), tpm_read_index(nscAddrBase,0xF7),
  295. tpm_read_index(nscAddrBase,0xF8), tpm_read_index(nscAddrBase,0xF9));
  296. dev_info(&pdev->dev,
  297. "NSC TPM revision %d\n",
  298. tpm_read_index(nscAddrBase, 0x27) & 0x1F);
  299. chip->vendor.base = base;
  300. return 0;
  301. err_rel_reg:
  302. release_region(base, 2);
  303. err_del_dev:
  304. platform_device_del(pdev);
  305. err_put_dev:
  306. platform_device_put(pdev);
  307. err_unreg_drv:
  308. platform_driver_unregister(&nsc_drv);
  309. return rc;
  310. }
  311. static void __exit cleanup_nsc(void)
  312. {
  313. if (pdev) {
  314. tpm_nsc_remove(&pdev->dev);
  315. platform_device_unregister(pdev);
  316. }
  317. platform_driver_unregister(&nsc_drv);
  318. }
  319. module_init(init_nsc);
  320. module_exit(cleanup_nsc);
  321. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  322. MODULE_DESCRIPTION("TPM Driver");
  323. MODULE_VERSION("2.0");
  324. MODULE_LICENSE("GPL");