tpm_atmel.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #define TPM_ATML_BASE 0x400
  24. /* write status bits */
  25. #define ATML_STATUS_ABORT 0x01
  26. #define ATML_STATUS_LASTBYTE 0x04
  27. /* read status bits */
  28. #define ATML_STATUS_BUSY 0x01
  29. #define ATML_STATUS_DATA_AVAIL 0x02
  30. #define ATML_STATUS_REWRITE 0x04
  31. static int tpm_atml_recv(struct tpm_chip *chip, u8 * buf, size_t count)
  32. {
  33. u8 status, *hdr = buf;
  34. u32 size;
  35. int i;
  36. __be32 *native_size;
  37. /* start reading header */
  38. if (count < 6)
  39. return -EIO;
  40. for (i = 0; i < 6; i++) {
  41. status = inb(chip->vendor->base + 1);
  42. if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
  43. dev_err(&chip->pci_dev->dev,
  44. "error reading header\n");
  45. return -EIO;
  46. }
  47. *buf++ = inb(chip->vendor->base);
  48. }
  49. /* size of the data received */
  50. native_size = (__force __be32 *) (hdr + 2);
  51. size = be32_to_cpu(*native_size);
  52. if (count < size) {
  53. dev_err(&chip->pci_dev->dev,
  54. "Recv size(%d) less than available space\n", size);
  55. for (; i < size; i++) { /* clear the waiting data anyway */
  56. status = inb(chip->vendor->base + 1);
  57. if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
  58. dev_err(&chip->pci_dev->dev,
  59. "error reading data\n");
  60. return -EIO;
  61. }
  62. }
  63. return -EIO;
  64. }
  65. /* read all the data available */
  66. for (; i < size; i++) {
  67. status = inb(chip->vendor->base + 1);
  68. if ((status & ATML_STATUS_DATA_AVAIL) == 0) {
  69. dev_err(&chip->pci_dev->dev,
  70. "error reading data\n");
  71. return -EIO;
  72. }
  73. *buf++ = inb(chip->vendor->base);
  74. }
  75. /* make sure data available is gone */
  76. status = inb(chip->vendor->base + 1);
  77. if (status & ATML_STATUS_DATA_AVAIL) {
  78. dev_err(&chip->pci_dev->dev, "data available is stuck\n");
  79. return -EIO;
  80. }
  81. return size;
  82. }
  83. static int tpm_atml_send(struct tpm_chip *chip, u8 * buf, size_t count)
  84. {
  85. int i;
  86. dev_dbg(&chip->pci_dev->dev, "tpm_atml_send: ");
  87. for (i = 0; i < count; i++) {
  88. dev_dbg(&chip->pci_dev->dev, "0x%x(%d) ", buf[i], buf[i]);
  89. outb(buf[i], chip->vendor->base);
  90. }
  91. return count;
  92. }
  93. static void tpm_atml_cancel(struct tpm_chip *chip)
  94. {
  95. outb(ATML_STATUS_ABORT, chip->vendor->base + 1);
  96. }
  97. static struct file_operations atmel_ops = {
  98. .owner = THIS_MODULE,
  99. .llseek = no_llseek,
  100. .open = tpm_open,
  101. .read = tpm_read,
  102. .write = tpm_write,
  103. .release = tpm_release,
  104. };
  105. static struct tpm_vendor_specific tpm_atmel = {
  106. .recv = tpm_atml_recv,
  107. .send = tpm_atml_send,
  108. .cancel = tpm_atml_cancel,
  109. .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL,
  110. .req_complete_val = ATML_STATUS_DATA_AVAIL,
  111. .base = TPM_ATML_BASE,
  112. .miscdev = { .fops = &atmel_ops, },
  113. };
  114. static int __devinit tpm_atml_init(struct pci_dev *pci_dev,
  115. const struct pci_device_id *pci_id)
  116. {
  117. u8 version[4];
  118. int rc = 0;
  119. if (pci_enable_device(pci_dev))
  120. return -EIO;
  121. if (tpm_lpc_bus_init(pci_dev, TPM_ATML_BASE)) {
  122. rc = -ENODEV;
  123. goto out_err;
  124. }
  125. /* verify that it is an Atmel part */
  126. if (tpm_read_index(4) != 'A' || tpm_read_index(5) != 'T'
  127. || tpm_read_index(6) != 'M' || tpm_read_index(7) != 'L') {
  128. rc = -ENODEV;
  129. goto out_err;
  130. }
  131. /* query chip for its version number */
  132. if ((version[0] = tpm_read_index(0x00)) != 0xFF) {
  133. version[1] = tpm_read_index(0x01);
  134. version[2] = tpm_read_index(0x02);
  135. version[3] = tpm_read_index(0x03);
  136. } else {
  137. dev_info(&pci_dev->dev, "version query failed\n");
  138. rc = -ENODEV;
  139. goto out_err;
  140. }
  141. if ((rc = tpm_register_hardware(pci_dev, &tpm_atmel)) < 0)
  142. goto out_err;
  143. dev_info(&pci_dev->dev,
  144. "Atmel TPM version %d.%d.%d.%d\n", version[0], version[1],
  145. version[2], version[3]);
  146. return 0;
  147. out_err:
  148. pci_disable_device(pci_dev);
  149. return rc;
  150. }
  151. static struct pci_device_id tpm_pci_tbl[] __devinitdata = {
  152. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0)},
  153. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12)},
  154. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0)},
  155. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12)},
  156. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0)},
  157. {PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_LPC)},
  158. {0,}
  159. };
  160. MODULE_DEVICE_TABLE(pci, tpm_pci_tbl);
  161. static struct pci_driver atmel_pci_driver = {
  162. .name = "tpm_atmel",
  163. .id_table = tpm_pci_tbl,
  164. .probe = tpm_atml_init,
  165. .remove = __devexit_p(tpm_remove),
  166. .suspend = tpm_pm_suspend,
  167. .resume = tpm_pm_resume,
  168. };
  169. static int __init init_atmel(void)
  170. {
  171. return pci_register_driver(&atmel_pci_driver);
  172. }
  173. static void __exit cleanup_atmel(void)
  174. {
  175. pci_unregister_driver(&atmel_pci_driver);
  176. }
  177. module_init(init_atmel);
  178. module_exit(cleanup_atmel);
  179. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  180. MODULE_DESCRIPTION("TPM Driver");
  181. MODULE_VERSION("2.0");
  182. MODULE_LICENSE("GPL");