tpm_atmel.c 5.8 KB

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