tpm_atmel.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 bool tpm_atml_req_canceled(struct tpm_chip *chip, u8 status)
  103. {
  104. return (status == ATML_STATUS_READY);
  105. }
  106. static const struct file_operations atmel_ops = {
  107. .owner = THIS_MODULE,
  108. .llseek = no_llseek,
  109. .open = tpm_open,
  110. .read = tpm_read,
  111. .write = tpm_write,
  112. .release = tpm_release,
  113. };
  114. static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
  115. static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
  116. static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL);
  117. static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel);
  118. static struct attribute* atmel_attrs[] = {
  119. &dev_attr_pubek.attr,
  120. &dev_attr_pcrs.attr,
  121. &dev_attr_caps.attr,
  122. &dev_attr_cancel.attr,
  123. NULL,
  124. };
  125. static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs };
  126. static const struct tpm_vendor_specific tpm_atmel = {
  127. .recv = tpm_atml_recv,
  128. .send = tpm_atml_send,
  129. .cancel = tpm_atml_cancel,
  130. .status = tpm_atml_status,
  131. .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
  132. .req_complete_val = ATML_STATUS_DATA_AVAIL,
  133. .req_canceled = tpm_atml_req_canceled,
  134. .attr_group = &atmel_attr_grp,
  135. .miscdev = { .fops = &atmel_ops, },
  136. };
  137. static struct platform_device *pdev;
  138. static void atml_plat_remove(void)
  139. {
  140. struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
  141. if (chip) {
  142. if (chip->vendor.have_region)
  143. atmel_release_region(chip->vendor.base,
  144. chip->vendor.region_size);
  145. atmel_put_base_addr(chip->vendor.iobase);
  146. tpm_remove_hardware(chip->dev);
  147. platform_device_unregister(pdev);
  148. }
  149. }
  150. static SIMPLE_DEV_PM_OPS(tpm_atml_pm, tpm_pm_suspend, tpm_pm_resume);
  151. static struct platform_driver atml_drv = {
  152. .driver = {
  153. .name = "tpm_atmel",
  154. .owner = THIS_MODULE,
  155. .pm = &tpm_atml_pm,
  156. },
  157. };
  158. static int __init init_atmel(void)
  159. {
  160. int rc = 0;
  161. void __iomem *iobase = NULL;
  162. int have_region, region_size;
  163. unsigned long base;
  164. struct tpm_chip *chip;
  165. rc = platform_driver_register(&atml_drv);
  166. if (rc)
  167. return rc;
  168. if ((iobase = atmel_get_base_addr(&base, &region_size)) == NULL) {
  169. rc = -ENODEV;
  170. goto err_unreg_drv;
  171. }
  172. have_region =
  173. (atmel_request_region
  174. (tpm_atmel.base, region_size, "tpm_atmel0") == NULL) ? 0 : 1;
  175. pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0);
  176. if (IS_ERR(pdev)) {
  177. rc = PTR_ERR(pdev);
  178. goto err_rel_reg;
  179. }
  180. if (!(chip = tpm_register_hardware(&pdev->dev, &tpm_atmel))) {
  181. rc = -ENODEV;
  182. goto err_unreg_dev;
  183. }
  184. chip->vendor.iobase = iobase;
  185. chip->vendor.base = base;
  186. chip->vendor.have_region = have_region;
  187. chip->vendor.region_size = region_size;
  188. return 0;
  189. err_unreg_dev:
  190. platform_device_unregister(pdev);
  191. err_rel_reg:
  192. atmel_put_base_addr(iobase);
  193. if (have_region)
  194. atmel_release_region(base,
  195. region_size);
  196. err_unreg_drv:
  197. platform_driver_unregister(&atml_drv);
  198. return rc;
  199. }
  200. static void __exit cleanup_atmel(void)
  201. {
  202. platform_driver_unregister(&atml_drv);
  203. atml_plat_remove();
  204. }
  205. module_init(init_atmel);
  206. module_exit(cleanup_atmel);
  207. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  208. MODULE_DESCRIPTION("TPM Driver");
  209. MODULE_VERSION("2.0");
  210. MODULE_LICENSE("GPL");