tpm_nsc.c 9.3 KB

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