tpm_nsc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_IRQ = 0x07,
  25. TPM_NSC_BASE0_HI = 0x60,
  26. TPM_NSC_BASE0_LO = 0x61,
  27. TPM_NSC_BASE1_HI = 0x62,
  28. TPM_NSC_BASE1_LO = 0x63
  29. };
  30. enum tpm_nsc_index {
  31. NSC_LDN_INDEX = 0x07,
  32. NSC_SID_INDEX = 0x20,
  33. NSC_LDC_INDEX = 0x30,
  34. NSC_DIO_INDEX = 0x60,
  35. NSC_CIO_INDEX = 0x62,
  36. NSC_IRQ_INDEX = 0x70,
  37. NSC_ITS_INDEX = 0x71
  38. };
  39. enum tpm_nsc_status_loc {
  40. NSC_STATUS = 0x01,
  41. NSC_COMMAND = 0x01,
  42. NSC_DATA = 0x00
  43. };
  44. /* status bits */
  45. enum tpm_nsc_status {
  46. NSC_STATUS_OBF = 0x01, /* output buffer full */
  47. NSC_STATUS_IBF = 0x02, /* input buffer full */
  48. NSC_STATUS_F0 = 0x04, /* F0 */
  49. NSC_STATUS_A2 = 0x08, /* A2 */
  50. NSC_STATUS_RDY = 0x10, /* ready to receive command */
  51. NSC_STATUS_IBR = 0x20 /* ready to receive data */
  52. };
  53. /* command bits */
  54. enum tpm_nsc_cmd_mode {
  55. NSC_COMMAND_NORMAL = 0x01, /* normal mode */
  56. NSC_COMMAND_EOC = 0x03,
  57. NSC_COMMAND_CANCEL = 0x22
  58. };
  59. /*
  60. * Wait for a certain status to appear
  61. */
  62. static int wait_for_stat(struct tpm_chip *chip, u8 mask, u8 val, u8 * data)
  63. {
  64. unsigned long stop;
  65. /* status immediately available check */
  66. *data = inb(chip->vendor->base + NSC_STATUS);
  67. if ((*data & mask) == val)
  68. return 0;
  69. /* wait for status */
  70. stop = jiffies + 10 * HZ;
  71. do {
  72. msleep(TPM_TIMEOUT);
  73. *data = inb(chip->vendor->base + 1);
  74. if ((*data & mask) == val)
  75. return 0;
  76. }
  77. while (time_before(jiffies, stop));
  78. return -EBUSY;
  79. }
  80. static int nsc_wait_for_ready(struct tpm_chip *chip)
  81. {
  82. int status;
  83. unsigned long stop;
  84. /* status immediately available check */
  85. status = inb(chip->vendor->base + NSC_STATUS);
  86. if (status & NSC_STATUS_OBF)
  87. status = inb(chip->vendor->base + NSC_DATA);
  88. if (status & NSC_STATUS_RDY)
  89. return 0;
  90. /* wait for status */
  91. stop = jiffies + 100;
  92. do {
  93. msleep(TPM_TIMEOUT);
  94. status = inb(chip->vendor->base + NSC_STATUS);
  95. if (status & NSC_STATUS_OBF)
  96. status = inb(chip->vendor->base + NSC_DATA);
  97. if (status & NSC_STATUS_RDY)
  98. return 0;
  99. }
  100. while (time_before(jiffies, stop));
  101. dev_info(&chip->pci_dev->dev, "wait for ready failed\n");
  102. return -EBUSY;
  103. }
  104. static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
  105. {
  106. u8 *buffer = buf;
  107. u8 data, *p;
  108. u32 size;
  109. __be32 *native_size;
  110. if (count < 6)
  111. return -EIO;
  112. if (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0) {
  113. dev_err(&chip->pci_dev->dev, "F0 timeout\n");
  114. return -EIO;
  115. }
  116. if ((data =
  117. inb(chip->vendor->base + NSC_DATA)) != NSC_COMMAND_NORMAL) {
  118. dev_err(&chip->pci_dev->dev, "not in normal mode (0x%x)\n",
  119. data);
  120. return -EIO;
  121. }
  122. /* read the whole packet */
  123. for (p = buffer; p < &buffer[count]; p++) {
  124. if (wait_for_stat
  125. (chip, NSC_STATUS_OBF, NSC_STATUS_OBF, &data) < 0) {
  126. dev_err(&chip->pci_dev->dev,
  127. "OBF timeout (while reading data)\n");
  128. return -EIO;
  129. }
  130. if (data & NSC_STATUS_F0)
  131. break;
  132. *p = inb(chip->vendor->base + NSC_DATA);
  133. }
  134. if ((data & NSC_STATUS_F0) == 0 &&
  135. (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 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. int nscAddrBase = TPM_ADDR;
  227. if (pci_enable_device(pci_dev))
  228. return -EIO;
  229. /* select PM channel 1 */
  230. tpm_write_index(nscAddrBase,NSC_LDN_INDEX, 0x12);
  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. rc = -ENODEV;
  237. goto out_err;
  238. }
  239. }
  240. hi = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_HI);
  241. lo = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_LO);
  242. tpm_nsc.base = (hi<<8) | lo;
  243. dev_dbg(&pci_dev->dev, "NSC TPM detected\n");
  244. dev_dbg(&pci_dev->dev,
  245. "NSC LDN 0x%x, SID 0x%x, SRID 0x%x\n",
  246. tpm_read_index(nscAddrBase,0x07), tpm_read_index(nscAddrBase,0x20),
  247. tpm_read_index(nscAddrBase,0x27));
  248. dev_dbg(&pci_dev->dev,
  249. "NSC SIOCF1 0x%x SIOCF5 0x%x SIOCF6 0x%x SIOCF8 0x%x\n",
  250. tpm_read_index(nscAddrBase,0x21), tpm_read_index(nscAddrBase,0x25),
  251. tpm_read_index(nscAddrBase,0x26), tpm_read_index(nscAddrBase,0x28));
  252. dev_dbg(&pci_dev->dev, "NSC IO Base0 0x%x\n",
  253. (tpm_read_index(nscAddrBase,0x60) << 8) | tpm_read_index(nscAddrBase,0x61));
  254. dev_dbg(&pci_dev->dev, "NSC IO Base1 0x%x\n",
  255. (tpm_read_index(nscAddrBase,0x62) << 8) | tpm_read_index(nscAddrBase,0x63));
  256. dev_dbg(&pci_dev->dev, "NSC Interrupt number and wakeup 0x%x\n",
  257. tpm_read_index(nscAddrBase,0x70));
  258. dev_dbg(&pci_dev->dev, "NSC IRQ type select 0x%x\n",
  259. tpm_read_index(nscAddrBase,0x71));
  260. dev_dbg(&pci_dev->dev,
  261. "NSC DMA channel select0 0x%x, select1 0x%x\n",
  262. tpm_read_index(nscAddrBase,0x74), tpm_read_index(nscAddrBase,0x75));
  263. dev_dbg(&pci_dev->dev,
  264. "NSC Config "
  265. "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  266. tpm_read_index(nscAddrBase,0xF0), tpm_read_index(nscAddrBase,0xF1),
  267. tpm_read_index(nscAddrBase,0xF2), tpm_read_index(nscAddrBase,0xF3),
  268. tpm_read_index(nscAddrBase,0xF4), tpm_read_index(nscAddrBase,0xF5),
  269. tpm_read_index(nscAddrBase,0xF6), tpm_read_index(nscAddrBase,0xF7),
  270. tpm_read_index(nscAddrBase,0xF8), tpm_read_index(nscAddrBase,0xF9));
  271. dev_info(&pci_dev->dev,
  272. "NSC TPM revision %d\n",
  273. tpm_read_index(nscAddrBase, 0x27) & 0x1F);
  274. /* enable the DPM module */
  275. tpm_write_index(nscAddrBase, NSC_LDC_INDEX, 0x01);
  276. if ((rc = tpm_register_hardware(pci_dev, &tpm_nsc)) < 0)
  277. goto out_err;
  278. return 0;
  279. out_err:
  280. pci_disable_device(pci_dev);
  281. return rc;
  282. }
  283. static struct pci_device_id tpm_pci_tbl[] __devinitdata = {
  284. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0)},
  285. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12)},
  286. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0)},
  287. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12)},
  288. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0)},
  289. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_0)},
  290. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1)},
  291. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0)},
  292. {PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_LPC)},
  293. {0,}
  294. };
  295. MODULE_DEVICE_TABLE(pci, tpm_pci_tbl);
  296. static struct pci_driver nsc_pci_driver = {
  297. .name = "tpm_nsc",
  298. .id_table = tpm_pci_tbl,
  299. .probe = tpm_nsc_init,
  300. .remove = __devexit_p(tpm_remove),
  301. .suspend = tpm_pm_suspend,
  302. .resume = tpm_pm_resume,
  303. };
  304. static int __init init_nsc(void)
  305. {
  306. return pci_register_driver(&nsc_pci_driver);
  307. }
  308. static void __exit cleanup_nsc(void)
  309. {
  310. pci_unregister_driver(&nsc_pci_driver);
  311. }
  312. module_init(init_nsc);
  313. module_exit(cleanup_nsc);
  314. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  315. MODULE_DESCRIPTION("TPM Driver");
  316. MODULE_VERSION("2.0");
  317. MODULE_LICENSE("GPL");