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