tpm_atmel.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. /* Atmel definitions */
  23. enum tpm_atmel_addr {
  24. TPM_ATMEL_BASE_ADDR_LO = 0x08,
  25. TPM_ATMEL_BASE_ADDR_HI = 0x09
  26. };
  27. /* write status bits */
  28. enum tpm_atmel_write_status {
  29. ATML_STATUS_ABORT = 0x01,
  30. ATML_STATUS_LASTBYTE = 0x04
  31. };
  32. /* read status bits */
  33. enum tpm_atmel_read_status {
  34. ATML_STATUS_BUSY = 0x01,
  35. ATML_STATUS_DATA_AVAIL = 0x02,
  36. ATML_STATUS_REWRITE = 0x04,
  37. ATML_STATUS_READY = 0x08
  38. };
  39. static int tpm_atml_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  40. {
  41. u8 status, *hdr = buf;
  42. u32 size;
  43. int i;
  44. __be32 *native_size;
  45. /* start reading header */
  46. if (count < 6)
  47. return -EIO;
  48. for (i = 0; i < 6; i++) {
  49. status = inb(chip->vendor->base + 1);
  50. if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
  51. dev_err(chip->dev,
  52. "error reading header\n");
  53. return -EIO;
  54. }
  55. *buf++ = inb(chip->vendor->base);
  56. }
  57. /* size of the data received */
  58. native_size = (__force __be32 *) (hdr + 2);
  59. size = be32_to_cpu(*native_size);
  60. if (count < size) {
  61. dev_err(chip->dev,
  62. "Recv size(%d) less than available space\n", size);
  63. for (; i < size; i++) { /* clear the waiting data anyway */
  64. status = inb(chip->vendor->base + 1);
  65. if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
  66. dev_err(chip->dev,
  67. "error reading data\n");
  68. return -EIO;
  69. }
  70. }
  71. return -EIO;
  72. }
  73. /* read all the data available */
  74. for (; i < size; i++) {
  75. status = inb(chip->vendor->base + 1);
  76. if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
  77. dev_err(chip->dev,
  78. "error reading data\n");
  79. return -EIO;
  80. }
  81. *buf++ = inb(chip->vendor->base);
  82. }
  83. /* make sure data available is gone */
  84. status = inb(chip->vendor->base + 1);
  85. if (status & ATML_STATUS_DATA_AVAIL) {
  86. dev_err(chip->dev, "data available is stuck\n");
  87. return -EIO;
  88. }
  89. return size;
  90. }
  91. static int tpm_atml_send(struct tpm_chip *chip, u8 *buf, size_t count)
  92. {
  93. int i;
  94. dev_dbg(chip->dev, "tpm_atml_send:\n");
  95. for (i = 0; i < count; i++) {
  96. dev_dbg(chip->dev, "%d 0x%x(%d)\n", i, buf[i], buf[i]);
  97. outb(buf[i], chip->vendor->base);
  98. }
  99. return count;
  100. }
  101. static void tpm_atml_cancel(struct tpm_chip *chip)
  102. {
  103. outb(ATML_STATUS_ABORT, chip->vendor->base + 1);
  104. }
  105. static u8 tpm_atml_status(struct tpm_chip *chip)
  106. {
  107. return inb(chip->vendor->base + 1);
  108. }
  109. static struct file_operations atmel_ops = {
  110. .owner = THIS_MODULE,
  111. .llseek = no_llseek,
  112. .open = tpm_open,
  113. .read = tpm_read,
  114. .write = tpm_write,
  115. .release = tpm_release,
  116. };
  117. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  118. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  119. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
  120. static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
  121. static struct attribute* atmel_attrs[] = {
  122. &dev_attr_pubek.attr,
  123. &dev_attr_pcrs.attr,
  124. &dev_attr_caps.attr,
  125. &dev_attr_cancel.attr,
  126. 0,
  127. };
  128. static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs };
  129. static struct tpm_vendor_specific tpm_atmel = {
  130. .recv = tpm_atml_recv,
  131. .send = tpm_atml_send,
  132. .cancel = tpm_atml_cancel,
  133. .status = tpm_atml_status,
  134. .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
  135. .req_complete_val = ATML_STATUS_DATA_AVAIL,
  136. .req_canceled = ATML_STATUS_READY,
  137. .attr_group = &atmel_attr_grp,
  138. .miscdev = { .fops = &atmel_ops, },
  139. };
  140. static struct platform_device *pdev;
  141. static void __devexit tpm_atml_remove(struct device *dev)
  142. {
  143. struct tpm_chip *chip = dev_get_drvdata(dev);
  144. if (chip) {
  145. release_region(chip->vendor->base, 2);
  146. tpm_remove_hardware(chip->dev);
  147. }
  148. }
  149. static struct device_driver atml_drv = {
  150. .name = "tpm_atmel",
  151. .bus = &platform_bus_type,
  152. .owner = THIS_MODULE,
  153. .suspend = tpm_pm_suspend,
  154. .resume = tpm_pm_resume,
  155. };
  156. static int __init init_atmel(void)
  157. {
  158. int rc = 0;
  159. int lo, hi;
  160. driver_register(&atml_drv);
  161. lo = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_LO);
  162. hi = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_HI);
  163. tpm_atmel.base = (hi<<8)|lo;
  164. /* verify that it is an Atmel part */
  165. if (tpm_read_index(TPM_ADDR, 4) != 'A' || tpm_read_index(TPM_ADDR, 5) != 'T'
  166. || tpm_read_index(TPM_ADDR, 6) != 'M' || tpm_read_index(TPM_ADDR, 7) != 'L') {
  167. return -ENODEV;
  168. }
  169. /* verify chip version number is 1.1 */
  170. if ( (tpm_read_index(TPM_ADDR, 0x00) != 0x01) ||
  171. (tpm_read_index(TPM_ADDR, 0x01) != 0x01 ))
  172. return -ENODEV;
  173. pdev = kzalloc(sizeof(struct platform_device), GFP_KERNEL);
  174. if ( !pdev )
  175. return -ENOMEM;
  176. pdev->name = "tpm_atmel0";
  177. pdev->id = -1;
  178. pdev->num_resources = 0;
  179. pdev->dev.release = tpm_atml_remove;
  180. pdev->dev.driver = &atml_drv;
  181. if ((rc = platform_device_register(pdev)) < 0) {
  182. kfree(pdev);
  183. pdev = NULL;
  184. return rc;
  185. }
  186. if (request_region(tpm_atmel.base, 2, "tpm_atmel0") == NULL ) {
  187. platform_device_unregister(pdev);
  188. kfree(pdev);
  189. pdev = NULL;
  190. return -EBUSY;
  191. }
  192. if ((rc = tpm_register_hardware(&pdev->dev, &tpm_atmel)) < 0) {
  193. release_region(tpm_atmel.base, 2);
  194. platform_device_unregister(pdev);
  195. kfree(pdev);
  196. pdev = NULL;
  197. return rc;
  198. }
  199. dev_info(&pdev->dev, "Atmel TPM 1.1, Base Address: 0x%x\n",
  200. tpm_atmel.base);
  201. return 0;
  202. }
  203. static void __exit cleanup_atmel(void)
  204. {
  205. if (pdev) {
  206. tpm_atml_remove(&pdev->dev);
  207. platform_device_unregister(pdev);
  208. kfree(pdev);
  209. pdev = NULL;
  210. }
  211. driver_unregister(&atml_drv);
  212. }
  213. module_init(init_atmel);
  214. module_exit(cleanup_atmel);
  215. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  216. MODULE_DESCRIPTION("TPM Driver");
  217. MODULE_VERSION("2.0");
  218. MODULE_LICENSE("GPL");