tpm_nsc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. struct tpm_nsc_priv {
  62. unsigned long base;
  63. };
  64. /*
  65. * Wait for a certain status to appear
  66. */
  67. static int wait_for_stat(struct tpm_chip *chip, u8 mask, u8 val, u8 * data)
  68. {
  69. struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
  70. unsigned long stop;
  71. /* status immediately available check */
  72. *data = inb(priv->base + NSC_STATUS);
  73. if ((*data & mask) == val)
  74. return 0;
  75. /* wait for status */
  76. stop = jiffies + 10 * HZ;
  77. do {
  78. msleep(TPM_TIMEOUT);
  79. *data = inb(priv->base + 1);
  80. if ((*data & mask) == val)
  81. return 0;
  82. }
  83. while (time_before(jiffies, stop));
  84. return -EBUSY;
  85. }
  86. static int nsc_wait_for_ready(struct tpm_chip *chip)
  87. {
  88. struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
  89. int status;
  90. unsigned long stop;
  91. /* status immediately available check */
  92. status = inb(priv->base + NSC_STATUS);
  93. if (status & NSC_STATUS_OBF)
  94. status = inb(priv->base + NSC_DATA);
  95. if (status & NSC_STATUS_RDY)
  96. return 0;
  97. /* wait for status */
  98. stop = jiffies + 100;
  99. do {
  100. msleep(TPM_TIMEOUT);
  101. status = inb(priv->base + NSC_STATUS);
  102. if (status & NSC_STATUS_OBF)
  103. status = inb(priv->base + NSC_DATA);
  104. if (status & NSC_STATUS_RDY)
  105. return 0;
  106. }
  107. while (time_before(jiffies, stop));
  108. dev_info(&chip->dev, "wait for ready failed\n");
  109. return -EBUSY;
  110. }
  111. static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
  112. {
  113. struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
  114. u8 *buffer = buf;
  115. u8 data, *p;
  116. u32 size;
  117. __be32 *native_size;
  118. if (count < 6)
  119. return -EIO;
  120. if (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0) {
  121. dev_err(&chip->dev, "F0 timeout\n");
  122. return -EIO;
  123. }
  124. data = inb(priv->base + NSC_DATA);
  125. if (data != NSC_COMMAND_NORMAL) {
  126. dev_err(&chip->dev, "not in normal mode (0x%x)\n",
  127. data);
  128. return -EIO;
  129. }
  130. /* read the whole packet */
  131. for (p = buffer; p < &buffer[count]; p++) {
  132. if (wait_for_stat
  133. (chip, NSC_STATUS_OBF, NSC_STATUS_OBF, &data) < 0) {
  134. dev_err(&chip->dev,
  135. "OBF timeout (while reading data)\n");
  136. return -EIO;
  137. }
  138. if (data & NSC_STATUS_F0)
  139. break;
  140. *p = inb(priv->base + NSC_DATA);
  141. }
  142. if ((data & NSC_STATUS_F0) == 0 &&
  143. (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0)) {
  144. dev_err(&chip->dev, "F0 not set\n");
  145. return -EIO;
  146. }
  147. data = inb(priv->base + NSC_DATA);
  148. if (data != NSC_COMMAND_EOC) {
  149. dev_err(&chip->dev,
  150. "expected end of command(0x%x)\n", data);
  151. return -EIO;
  152. }
  153. native_size = (__force __be32 *) (buf + 2);
  154. size = be32_to_cpu(*native_size);
  155. if (count < size)
  156. return -EIO;
  157. return size;
  158. }
  159. static int tpm_nsc_send(struct tpm_chip *chip, u8 * buf, size_t count)
  160. {
  161. struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
  162. u8 data;
  163. int i;
  164. /*
  165. * If we hit the chip with back to back commands it locks up
  166. * and never set IBF. Hitting it with this "hammer" seems to
  167. * fix it. Not sure why this is needed, we followed the flow
  168. * chart in the manual to the letter.
  169. */
  170. outb(NSC_COMMAND_CANCEL, priv->base + NSC_COMMAND);
  171. if (nsc_wait_for_ready(chip) != 0)
  172. return -EIO;
  173. if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
  174. dev_err(&chip->dev, "IBF timeout\n");
  175. return -EIO;
  176. }
  177. outb(NSC_COMMAND_NORMAL, priv->base + NSC_COMMAND);
  178. if (wait_for_stat(chip, NSC_STATUS_IBR, NSC_STATUS_IBR, &data) < 0) {
  179. dev_err(&chip->dev, "IBR timeout\n");
  180. return -EIO;
  181. }
  182. for (i = 0; i < count; i++) {
  183. if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
  184. dev_err(&chip->dev,
  185. "IBF timeout (while writing data)\n");
  186. return -EIO;
  187. }
  188. outb(buf[i], priv->base + NSC_DATA);
  189. }
  190. if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
  191. dev_err(&chip->dev, "IBF timeout\n");
  192. return -EIO;
  193. }
  194. outb(NSC_COMMAND_EOC, priv->base + NSC_COMMAND);
  195. return 0;
  196. }
  197. static void tpm_nsc_cancel(struct tpm_chip *chip)
  198. {
  199. struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
  200. outb(NSC_COMMAND_CANCEL, priv->base + NSC_COMMAND);
  201. }
  202. static u8 tpm_nsc_status(struct tpm_chip *chip)
  203. {
  204. struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
  205. return inb(priv->base + NSC_STATUS);
  206. }
  207. static bool tpm_nsc_req_canceled(struct tpm_chip *chip, u8 status)
  208. {
  209. return (status == NSC_STATUS_RDY);
  210. }
  211. static const struct tpm_class_ops tpm_nsc = {
  212. .recv = tpm_nsc_recv,
  213. .send = tpm_nsc_send,
  214. .cancel = tpm_nsc_cancel,
  215. .status = tpm_nsc_status,
  216. .req_complete_mask = NSC_STATUS_OBF,
  217. .req_complete_val = NSC_STATUS_OBF,
  218. .req_canceled = tpm_nsc_req_canceled,
  219. };
  220. static struct platform_device *pdev = NULL;
  221. static void tpm_nsc_remove(struct device *dev)
  222. {
  223. struct tpm_chip *chip = dev_get_drvdata(dev);
  224. struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
  225. tpm_chip_unregister(chip);
  226. release_region(priv->base, 2);
  227. }
  228. static SIMPLE_DEV_PM_OPS(tpm_nsc_pm, tpm_pm_suspend, tpm_pm_resume);
  229. static struct platform_driver nsc_drv = {
  230. .driver = {
  231. .name = "tpm_nsc",
  232. .pm = &tpm_nsc_pm,
  233. },
  234. };
  235. static inline int tpm_read_index(int base, int index)
  236. {
  237. outb(index, base);
  238. return inb(base+1) & 0xFF;
  239. }
  240. static inline void tpm_write_index(int base, int index, int value)
  241. {
  242. outb(index, base);
  243. outb(value & 0xFF, base+1);
  244. }
  245. static int __init init_nsc(void)
  246. {
  247. int rc = 0;
  248. int lo, hi, err;
  249. int nscAddrBase = TPM_ADDR;
  250. struct tpm_chip *chip;
  251. unsigned long base;
  252. struct tpm_nsc_priv *priv;
  253. /* verify that it is a National part (SID) */
  254. if (tpm_read_index(TPM_ADDR, NSC_SID_INDEX) != 0xEF) {
  255. nscAddrBase = (tpm_read_index(TPM_SUPERIO_ADDR, 0x2C)<<8)|
  256. (tpm_read_index(TPM_SUPERIO_ADDR, 0x2B)&0xFE);
  257. if (tpm_read_index(nscAddrBase, NSC_SID_INDEX) != 0xF6)
  258. return -ENODEV;
  259. }
  260. err = platform_driver_register(&nsc_drv);
  261. if (err)
  262. return err;
  263. hi = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_HI);
  264. lo = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_LO);
  265. base = (hi<<8) | lo;
  266. /* enable the DPM module */
  267. tpm_write_index(nscAddrBase, NSC_LDC_INDEX, 0x01);
  268. pdev = platform_device_alloc("tpm_nscl0", -1);
  269. if (!pdev) {
  270. rc = -ENOMEM;
  271. goto err_unreg_drv;
  272. }
  273. pdev->num_resources = 0;
  274. pdev->dev.driver = &nsc_drv.driver;
  275. pdev->dev.release = tpm_nsc_remove;
  276. if ((rc = platform_device_add(pdev)) < 0)
  277. goto err_put_dev;
  278. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  279. if (!priv) {
  280. rc = -ENOMEM;
  281. goto err_del_dev;
  282. }
  283. priv->base = base;
  284. if (request_region(base, 2, "tpm_nsc0") == NULL ) {
  285. rc = -EBUSY;
  286. goto err_del_dev;
  287. }
  288. chip = tpmm_chip_alloc(&pdev->dev, &tpm_nsc);
  289. if (IS_ERR(chip)) {
  290. rc = -ENODEV;
  291. goto err_rel_reg;
  292. }
  293. dev_set_drvdata(&chip->dev, priv);
  294. rc = tpm_chip_register(chip);
  295. if (rc)
  296. goto err_rel_reg;
  297. dev_dbg(&pdev->dev, "NSC TPM detected\n");
  298. dev_dbg(&pdev->dev,
  299. "NSC LDN 0x%x, SID 0x%x, SRID 0x%x\n",
  300. tpm_read_index(nscAddrBase,0x07), tpm_read_index(nscAddrBase,0x20),
  301. tpm_read_index(nscAddrBase,0x27));
  302. dev_dbg(&pdev->dev,
  303. "NSC SIOCF1 0x%x SIOCF5 0x%x SIOCF6 0x%x SIOCF8 0x%x\n",
  304. tpm_read_index(nscAddrBase,0x21), tpm_read_index(nscAddrBase,0x25),
  305. tpm_read_index(nscAddrBase,0x26), tpm_read_index(nscAddrBase,0x28));
  306. dev_dbg(&pdev->dev, "NSC IO Base0 0x%x\n",
  307. (tpm_read_index(nscAddrBase,0x60) << 8) | tpm_read_index(nscAddrBase,0x61));
  308. dev_dbg(&pdev->dev, "NSC IO Base1 0x%x\n",
  309. (tpm_read_index(nscAddrBase,0x62) << 8) | tpm_read_index(nscAddrBase,0x63));
  310. dev_dbg(&pdev->dev, "NSC Interrupt number and wakeup 0x%x\n",
  311. tpm_read_index(nscAddrBase,0x70));
  312. dev_dbg(&pdev->dev, "NSC IRQ type select 0x%x\n",
  313. tpm_read_index(nscAddrBase,0x71));
  314. dev_dbg(&pdev->dev,
  315. "NSC DMA channel select0 0x%x, select1 0x%x\n",
  316. tpm_read_index(nscAddrBase,0x74), tpm_read_index(nscAddrBase,0x75));
  317. dev_dbg(&pdev->dev,
  318. "NSC Config "
  319. "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  320. tpm_read_index(nscAddrBase,0xF0), tpm_read_index(nscAddrBase,0xF1),
  321. tpm_read_index(nscAddrBase,0xF2), tpm_read_index(nscAddrBase,0xF3),
  322. tpm_read_index(nscAddrBase,0xF4), tpm_read_index(nscAddrBase,0xF5),
  323. tpm_read_index(nscAddrBase,0xF6), tpm_read_index(nscAddrBase,0xF7),
  324. tpm_read_index(nscAddrBase,0xF8), tpm_read_index(nscAddrBase,0xF9));
  325. dev_info(&pdev->dev,
  326. "NSC TPM revision %d\n",
  327. tpm_read_index(nscAddrBase, 0x27) & 0x1F);
  328. return 0;
  329. err_rel_reg:
  330. release_region(base, 2);
  331. err_del_dev:
  332. platform_device_del(pdev);
  333. err_put_dev:
  334. platform_device_put(pdev);
  335. err_unreg_drv:
  336. platform_driver_unregister(&nsc_drv);
  337. return rc;
  338. }
  339. static void __exit cleanup_nsc(void)
  340. {
  341. if (pdev) {
  342. tpm_nsc_remove(&pdev->dev);
  343. platform_device_unregister(pdev);
  344. }
  345. platform_driver_unregister(&nsc_drv);
  346. }
  347. module_init(init_nsc);
  348. module_exit(cleanup_nsc);
  349. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  350. MODULE_DESCRIPTION("TPM Driver");
  351. MODULE_VERSION("2.0");
  352. MODULE_LICENSE("GPL");