tpm_nsc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 "tpm.h"
  22. /* National definitions */
  23. enum tpm_nsc_addr{
  24. TPM_NSC_BASE = 0x360,
  25. TPM_NSC_IRQ = 0x07,
  26. TPM_NSC_BASE0_HI = 0x60,
  27. TPM_NSC_BASE0_LO = 0x61,
  28. TPM_NSC_BASE1_HI = 0x62,
  29. TPM_NSC_BASE1_LO = 0x63
  30. };
  31. enum tpm_nsc_index {
  32. NSC_LDN_INDEX = 0x07,
  33. NSC_SID_INDEX = 0x20,
  34. NSC_LDC_INDEX = 0x30,
  35. NSC_DIO_INDEX = 0x60,
  36. NSC_CIO_INDEX = 0x62,
  37. NSC_IRQ_INDEX = 0x70,
  38. NSC_ITS_INDEX = 0x71
  39. };
  40. enum tpm_nsc_status_loc {
  41. NSC_STATUS = 0x01,
  42. NSC_COMMAND = 0x01,
  43. NSC_DATA = 0x00
  44. };
  45. /* status bits */
  46. enum tpm_nsc_status {
  47. NSC_STATUS_OBF = 0x01, /* output buffer full */
  48. NSC_STATUS_IBF = 0x02, /* input buffer full */
  49. NSC_STATUS_F0 = 0x04, /* F0 */
  50. NSC_STATUS_A2 = 0x08, /* A2 */
  51. NSC_STATUS_RDY = 0x10, /* ready to receive command */
  52. NSC_STATUS_IBR = 0x20 /* ready to receive data */
  53. };
  54. /* command bits */
  55. enum tpm_nsc_cmd_mode {
  56. NSC_COMMAND_NORMAL = 0x01, /* normal mode */
  57. NSC_COMMAND_EOC = 0x03,
  58. NSC_COMMAND_CANCEL = 0x22
  59. };
  60. /*
  61. * Wait for a certain status to appear
  62. */
  63. static int wait_for_stat(struct tpm_chip *chip, u8 mask, u8 val, u8 * data)
  64. {
  65. unsigned long stop;
  66. /* status immediately available check */
  67. *data = inb(chip->vendor->base + NSC_STATUS);
  68. if ((*data & mask) == val)
  69. return 0;
  70. /* wait for status */
  71. stop = jiffies + 10 * HZ;
  72. do {
  73. msleep(TPM_TIMEOUT);
  74. *data = inb(chip->vendor->base + 1);
  75. if ((*data & mask) == val)
  76. return 0;
  77. }
  78. while (time_before(jiffies, stop));
  79. return -EBUSY;
  80. }
  81. static int nsc_wait_for_ready(struct tpm_chip *chip)
  82. {
  83. int status;
  84. unsigned long stop;
  85. /* status immediately available check */
  86. status = inb(chip->vendor->base + NSC_STATUS);
  87. if (status & NSC_STATUS_OBF)
  88. status = inb(chip->vendor->base + NSC_DATA);
  89. if (status & NSC_STATUS_RDY)
  90. return 0;
  91. /* wait for status */
  92. stop = jiffies + 100;
  93. do {
  94. msleep(TPM_TIMEOUT);
  95. status = inb(chip->vendor->base + NSC_STATUS);
  96. if (status & NSC_STATUS_OBF)
  97. status = inb(chip->vendor->base + NSC_DATA);
  98. if (status & NSC_STATUS_RDY)
  99. return 0;
  100. }
  101. while (time_before(jiffies, stop));
  102. dev_info(&chip->pci_dev->dev, "wait for ready failed\n");
  103. return -EBUSY;
  104. }
  105. static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
  106. {
  107. u8 *buffer = buf;
  108. u8 data, *p;
  109. u32 size;
  110. __be32 *native_size;
  111. if (count < 6)
  112. return -EIO;
  113. if (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0) {
  114. dev_err(&chip->pci_dev->dev, "F0 timeout\n");
  115. return -EIO;
  116. }
  117. if ((data =
  118. inb(chip->vendor->base + NSC_DATA)) != NSC_COMMAND_NORMAL) {
  119. dev_err(&chip->pci_dev->dev, "not in normal mode (0x%x)\n",
  120. data);
  121. return -EIO;
  122. }
  123. /* read the whole packet */
  124. for (p = buffer; p < &buffer[count]; p++) {
  125. if (wait_for_stat
  126. (chip, NSC_STATUS_OBF, NSC_STATUS_OBF, &data) < 0) {
  127. dev_err(&chip->pci_dev->dev,
  128. "OBF timeout (while reading data)\n");
  129. return -EIO;
  130. }
  131. if (data & NSC_STATUS_F0)
  132. break;
  133. *p = inb(chip->vendor->base + NSC_DATA);
  134. }
  135. if ((data & NSC_STATUS_F0) == 0) {
  136. dev_err(&chip->pci_dev->dev, "F0 not set\n");
  137. return -EIO;
  138. }
  139. if ((data = inb(chip->vendor->base + NSC_DATA)) != NSC_COMMAND_EOC) {
  140. dev_err(&chip->pci_dev->dev,
  141. "expected end of command(0x%x)\n", data);
  142. return -EIO;
  143. }
  144. native_size = (__force __be32 *) (buf + 2);
  145. size = be32_to_cpu(*native_size);
  146. if (count < size)
  147. return -EIO;
  148. return size;
  149. }
  150. static int tpm_nsc_send(struct tpm_chip *chip, u8 * buf, size_t count)
  151. {
  152. u8 data;
  153. int i;
  154. /*
  155. * If we hit the chip with back to back commands it locks up
  156. * and never set IBF. Hitting it with this "hammer" seems to
  157. * fix it. Not sure why this is needed, we followed the flow
  158. * chart in the manual to the letter.
  159. */
  160. outb(NSC_COMMAND_CANCEL, chip->vendor->base + NSC_COMMAND);
  161. if (nsc_wait_for_ready(chip) != 0)
  162. return -EIO;
  163. if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
  164. dev_err(&chip->pci_dev->dev, "IBF timeout\n");
  165. return -EIO;
  166. }
  167. outb(NSC_COMMAND_NORMAL, chip->vendor->base + NSC_COMMAND);
  168. if (wait_for_stat(chip, NSC_STATUS_IBR, NSC_STATUS_IBR, &data) < 0) {
  169. dev_err(&chip->pci_dev->dev, "IBR timeout\n");
  170. return -EIO;
  171. }
  172. for (i = 0; i < count; i++) {
  173. if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
  174. dev_err(&chip->pci_dev->dev,
  175. "IBF timeout (while writing data)\n");
  176. return -EIO;
  177. }
  178. outb(buf[i], chip->vendor->base + NSC_DATA);
  179. }
  180. if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
  181. dev_err(&chip->pci_dev->dev, "IBF timeout\n");
  182. return -EIO;
  183. }
  184. outb(NSC_COMMAND_EOC, chip->vendor->base + NSC_COMMAND);
  185. return count;
  186. }
  187. static void tpm_nsc_cancel(struct tpm_chip *chip)
  188. {
  189. outb(NSC_COMMAND_CANCEL, chip->vendor->base + NSC_COMMAND);
  190. }
  191. static struct file_operations nsc_ops = {
  192. .owner = THIS_MODULE,
  193. .llseek = no_llseek,
  194. .open = tpm_open,
  195. .read = tpm_read,
  196. .write = tpm_write,
  197. .release = tpm_release,
  198. };
  199. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  200. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  201. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
  202. static DEVICE_ATTR(cancel, S_IWUSR|S_IWGRP, NULL, tpm_store_cancel);
  203. static struct attribute * nsc_attrs[] = {
  204. &dev_attr_pubek.attr,
  205. &dev_attr_pcrs.attr,
  206. &dev_attr_caps.attr,
  207. &dev_attr_cancel.attr,
  208. 0,
  209. };
  210. static struct attribute_group nsc_attr_grp = { .attrs = nsc_attrs };
  211. static struct tpm_vendor_specific tpm_nsc = {
  212. .recv = tpm_nsc_recv,
  213. .send = tpm_nsc_send,
  214. .cancel = tpm_nsc_cancel,
  215. .req_complete_mask = NSC_STATUS_OBF,
  216. .req_complete_val = NSC_STATUS_OBF,
  217. .req_canceled = NSC_STATUS_RDY,
  218. .attr_group = &nsc_attr_grp,
  219. .miscdev = { .fops = &nsc_ops, },
  220. };
  221. static int __devinit tpm_nsc_init(struct pci_dev *pci_dev,
  222. const struct pci_device_id *pci_id)
  223. {
  224. int rc = 0;
  225. int lo, hi;
  226. hi = tpm_read_index(TPM_NSC_BASE0_HI);
  227. lo = tpm_read_index(TPM_NSC_BASE0_LO);
  228. tpm_nsc.base = (hi<<8) | lo;
  229. if (pci_enable_device(pci_dev))
  230. return -EIO;
  231. /* verify that it is a National part (SID) */
  232. if (tpm_read_index(NSC_SID_INDEX) != 0xEF) {
  233. rc = -ENODEV;
  234. goto out_err;
  235. }
  236. dev_dbg(&pci_dev->dev, "NSC TPM detected\n");
  237. dev_dbg(&pci_dev->dev,
  238. "NSC LDN 0x%x, SID 0x%x, SRID 0x%x\n",
  239. tpm_read_index(0x07), tpm_read_index(0x20),
  240. tpm_read_index(0x27));
  241. dev_dbg(&pci_dev->dev,
  242. "NSC SIOCF1 0x%x SIOCF5 0x%x SIOCF6 0x%x SIOCF8 0x%x\n",
  243. tpm_read_index(0x21), tpm_read_index(0x25),
  244. tpm_read_index(0x26), tpm_read_index(0x28));
  245. dev_dbg(&pci_dev->dev, "NSC IO Base0 0x%x\n",
  246. (tpm_read_index(0x60) << 8) | tpm_read_index(0x61));
  247. dev_dbg(&pci_dev->dev, "NSC IO Base1 0x%x\n",
  248. (tpm_read_index(0x62) << 8) | tpm_read_index(0x63));
  249. dev_dbg(&pci_dev->dev, "NSC Interrupt number and wakeup 0x%x\n",
  250. tpm_read_index(0x70));
  251. dev_dbg(&pci_dev->dev, "NSC IRQ type select 0x%x\n",
  252. tpm_read_index(0x71));
  253. dev_dbg(&pci_dev->dev,
  254. "NSC DMA channel select0 0x%x, select1 0x%x\n",
  255. tpm_read_index(0x74), tpm_read_index(0x75));
  256. dev_dbg(&pci_dev->dev,
  257. "NSC Config "
  258. "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  259. tpm_read_index(0xF0), tpm_read_index(0xF1),
  260. tpm_read_index(0xF2), tpm_read_index(0xF3),
  261. tpm_read_index(0xF4), tpm_read_index(0xF5),
  262. tpm_read_index(0xF6), tpm_read_index(0xF7),
  263. tpm_read_index(0xF8), tpm_read_index(0xF9));
  264. dev_info(&pci_dev->dev,
  265. "NSC PC21100 TPM revision %d\n",
  266. tpm_read_index(0x27) & 0x1F);
  267. if (tpm_read_index(NSC_LDC_INDEX) == 0)
  268. dev_info(&pci_dev->dev, ": NSC TPM not active\n");
  269. /* select PM channel 1 */
  270. tpm_write_index(NSC_LDN_INDEX, 0x12);
  271. tpm_read_index(NSC_LDN_INDEX);
  272. /* disable the DPM module */
  273. tpm_write_index(NSC_LDC_INDEX, 0);
  274. tpm_read_index(NSC_LDC_INDEX);
  275. /* set the data register base addresses */
  276. tpm_write_index(NSC_DIO_INDEX, TPM_NSC_BASE >> 8);
  277. tpm_write_index(NSC_DIO_INDEX + 1, TPM_NSC_BASE);
  278. tpm_read_index(NSC_DIO_INDEX);
  279. tpm_read_index(NSC_DIO_INDEX + 1);
  280. /* set the command register base addresses */
  281. tpm_write_index(NSC_CIO_INDEX, (TPM_NSC_BASE + 1) >> 8);
  282. tpm_write_index(NSC_CIO_INDEX + 1, (TPM_NSC_BASE + 1));
  283. tpm_read_index(NSC_DIO_INDEX);
  284. tpm_read_index(NSC_DIO_INDEX + 1);
  285. /* set the interrupt number to be used for the host interface */
  286. tpm_write_index(NSC_IRQ_INDEX, TPM_NSC_IRQ);
  287. tpm_write_index(NSC_ITS_INDEX, 0x00);
  288. tpm_read_index(NSC_IRQ_INDEX);
  289. /* enable the DPM module */
  290. tpm_write_index(NSC_LDC_INDEX, 0x01);
  291. tpm_read_index(NSC_LDC_INDEX);
  292. if ((rc = tpm_register_hardware(pci_dev, &tpm_nsc)) < 0)
  293. goto out_err;
  294. return 0;
  295. out_err:
  296. pci_disable_device(pci_dev);
  297. return rc;
  298. }
  299. static struct pci_device_id tpm_pci_tbl[] __devinitdata = {
  300. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0)},
  301. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12)},
  302. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0)},
  303. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12)},
  304. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0)},
  305. {PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_LPC)},
  306. {0,}
  307. };
  308. MODULE_DEVICE_TABLE(pci, tpm_pci_tbl);
  309. static struct pci_driver nsc_pci_driver = {
  310. .name = "tpm_nsc",
  311. .id_table = tpm_pci_tbl,
  312. .probe = tpm_nsc_init,
  313. .remove = __devexit_p(tpm_remove),
  314. .suspend = tpm_pm_suspend,
  315. .resume = tpm_pm_resume,
  316. };
  317. static int __init init_nsc(void)
  318. {
  319. return pci_register_driver(&nsc_pci_driver);
  320. }
  321. static void __exit cleanup_nsc(void)
  322. {
  323. pci_unregister_driver(&nsc_pci_driver);
  324. }
  325. module_init(init_nsc);
  326. module_exit(cleanup_nsc);
  327. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  328. MODULE_DESCRIPTION("TPM Driver");
  329. MODULE_VERSION("2.0");
  330. MODULE_LICENSE("GPL");