tpm_atmel.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 tpm_class_ops tpm_atmel = {
  107. .recv = tpm_atml_recv,
  108. .send = tpm_atml_send,
  109. .cancel = tpm_atml_cancel,
  110. .status = tpm_atml_status,
  111. .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
  112. .req_complete_val = ATML_STATUS_DATA_AVAIL,
  113. .req_canceled = tpm_atml_req_canceled,
  114. };
  115. static struct platform_device *pdev;
  116. static void atml_plat_remove(void)
  117. {
  118. struct tpm_chip *chip = dev_get_drvdata(&pdev->dev);
  119. if (chip) {
  120. tpm_chip_unregister(chip);
  121. if (chip->vendor.have_region)
  122. atmel_release_region(chip->vendor.base,
  123. chip->vendor.region_size);
  124. atmel_put_base_addr(chip->vendor.iobase);
  125. platform_device_unregister(pdev);
  126. }
  127. }
  128. static SIMPLE_DEV_PM_OPS(tpm_atml_pm, tpm_pm_suspend, tpm_pm_resume);
  129. static struct platform_driver atml_drv = {
  130. .driver = {
  131. .name = "tpm_atmel",
  132. .pm = &tpm_atml_pm,
  133. },
  134. };
  135. static int __init init_atmel(void)
  136. {
  137. int rc = 0;
  138. void __iomem *iobase = NULL;
  139. int have_region, region_size;
  140. unsigned long base;
  141. struct tpm_chip *chip;
  142. rc = platform_driver_register(&atml_drv);
  143. if (rc)
  144. return rc;
  145. if ((iobase = atmel_get_base_addr(&base, &region_size)) == NULL) {
  146. rc = -ENODEV;
  147. goto err_unreg_drv;
  148. }
  149. have_region =
  150. (atmel_request_region
  151. (base, region_size, "tpm_atmel0") == NULL) ? 0 : 1;
  152. pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0);
  153. if (IS_ERR(pdev)) {
  154. rc = PTR_ERR(pdev);
  155. goto err_rel_reg;
  156. }
  157. chip = tpmm_chip_alloc(&pdev->dev, &tpm_atmel);
  158. if (IS_ERR(chip)) {
  159. rc = PTR_ERR(chip);
  160. goto err_unreg_dev;
  161. }
  162. chip->vendor.iobase = iobase;
  163. chip->vendor.base = base;
  164. chip->vendor.have_region = have_region;
  165. chip->vendor.region_size = region_size;
  166. rc = tpm_chip_register(chip);
  167. if (rc)
  168. goto err_unreg_dev;
  169. return 0;
  170. err_unreg_dev:
  171. platform_device_unregister(pdev);
  172. err_rel_reg:
  173. atmel_put_base_addr(iobase);
  174. if (have_region)
  175. atmel_release_region(base,
  176. region_size);
  177. err_unreg_drv:
  178. platform_driver_unregister(&atml_drv);
  179. return rc;
  180. }
  181. static void __exit cleanup_atmel(void)
  182. {
  183. platform_driver_unregister(&atml_drv);
  184. atml_plat_remove();
  185. }
  186. module_init(init_atmel);
  187. module_exit(cleanup_atmel);
  188. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  189. MODULE_DESCRIPTION("TPM Driver");
  190. MODULE_VERSION("2.0");
  191. MODULE_LICENSE("GPL");