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 | * |
Kent Yoder | 8e81cc1 | 2007-08-22 14:01:04 -0700 | [diff] [blame] | 10 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 50 | status = ioread8(chip->vendor.iobase + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | if ((status & ATML_STATUS_DATA_AVAIL) == 0) { |
Kylene Jo Hall | 90612b3 | 2005-11-18 01:10:58 -0800 | [diff] [blame] | 52 | dev_err(chip->dev, "error reading header\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return -EIO; |
| 54 | } |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 55 | *buf++ = ioread8(chip->vendor.iobase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* size of the data received */ |
| 59 | native_size = (__force __be32 *) (hdr + 2); |
| 60 | size = be32_to_cpu(*native_size); |
| 61 | |
| 62 | if (count < size) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 63 | dev_err(chip->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | "Recv size(%d) less than available space\n", size); |
| 65 | for (; i < size; i++) { /* clear the waiting data anyway */ |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 66 | status = ioread8(chip->vendor.iobase + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | if ((status & ATML_STATUS_DATA_AVAIL) == 0) { |
Kylene Jo Hall | 90612b3 | 2005-11-18 01:10:58 -0800 | [diff] [blame] | 68 | dev_err(chip->dev, "error reading data\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return -EIO; |
| 70 | } |
| 71 | } |
| 72 | return -EIO; |
| 73 | } |
| 74 | |
| 75 | /* read all the data available */ |
| 76 | for (; i < size; i++) { |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 77 | status = ioread8(chip->vendor.iobase + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | if ((status & ATML_STATUS_DATA_AVAIL) == 0) { |
Kylene Jo Hall | 90612b3 | 2005-11-18 01:10:58 -0800 | [diff] [blame] | 79 | dev_err(chip->dev, "error reading data\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | return -EIO; |
| 81 | } |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 82 | *buf++ = ioread8(chip->vendor.iobase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* make sure data available is gone */ |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 86 | status = ioread8(chip->vendor.iobase + 1); |
Kylene Jo Hall | 90612b3 | 2005-11-18 01:10:58 -0800 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (status & ATML_STATUS_DATA_AVAIL) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 89 | dev_err(chip->dev, "data available is stuck\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | return -EIO; |
| 91 | } |
| 92 | |
| 93 | return size; |
| 94 | } |
| 95 | |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 96 | 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] | 97 | { |
| 98 | int i; |
| 99 | |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 100 | dev_dbg(chip->dev, "tpm_atml_send:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | for (i = 0; i < count; i++) { |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 102 | dev_dbg(chip->dev, "%d 0x%x(%d)\n", i, buf[i], buf[i]); |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 103 | iowrite8(buf[i], chip->vendor.iobase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return count; |
| 107 | } |
| 108 | |
| 109 | static void tpm_atml_cancel(struct tpm_chip *chip) |
| 110 | { |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 111 | iowrite8(ATML_STATUS_ABORT, chip->vendor.iobase + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Kylene Jo Hall | b4ed3e3 | 2005-10-30 15:03:23 -0800 | [diff] [blame] | 114 | static u8 tpm_atml_status(struct tpm_chip *chip) |
| 115 | { |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 116 | return ioread8(chip->vendor.iobase + 1); |
Kylene Jo Hall | b4ed3e3 | 2005-10-30 15:03:23 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Stefan Berger | 1f86605 | 2013-01-22 13:52:35 -0600 | [diff] [blame] | 119 | static bool tpm_atml_req_canceled(struct tpm_chip *chip, u8 status) |
| 120 | { |
| 121 | return (status == ATML_STATUS_READY); |
| 122 | } |
| 123 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 124 | static const struct file_operations atmel_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | .owner = THIS_MODULE, |
| 126 | .llseek = no_llseek, |
| 127 | .open = tpm_open, |
| 128 | .read = tpm_read, |
| 129 | .write = tpm_write, |
| 130 | .release = tpm_release, |
| 131 | }; |
| 132 | |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 133 | static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL); |
| 134 | static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL); |
| 135 | static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL); |
| 136 | static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel); |
| 137 | |
| 138 | static struct attribute* atmel_attrs[] = { |
| 139 | &dev_attr_pubek.attr, |
| 140 | &dev_attr_pcrs.attr, |
| 141 | &dev_attr_caps.attr, |
| 142 | &dev_attr_cancel.attr, |
Randy Dunlap | 874ec33 | 2005-10-30 15:03:29 -0800 | [diff] [blame] | 143 | NULL, |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs }; |
| 147 | |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 148 | static const struct tpm_vendor_specific tpm_atmel = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | .recv = tpm_atml_recv, |
| 150 | .send = tpm_atml_send, |
| 151 | .cancel = tpm_atml_cancel, |
Kylene Jo Hall | b4ed3e3 | 2005-10-30 15:03:23 -0800 | [diff] [blame] | 152 | .status = tpm_atml_status, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL, |
| 154 | .req_complete_val = ATML_STATUS_DATA_AVAIL, |
Stefan Berger | 1f86605 | 2013-01-22 13:52:35 -0600 | [diff] [blame] | 155 | .req_canceled = tpm_atml_req_canceled, |
Kylene Hall | 6659ca2 | 2005-06-23 22:02:00 -0700 | [diff] [blame] | 156 | .attr_group = &atmel_attr_grp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | .miscdev = { .fops = &atmel_ops, }, |
| 158 | }; |
| 159 | |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 160 | static struct platform_device *pdev; |
Kylene Jo Hall | 682e97a | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 161 | |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 162 | static void atml_plat_remove(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 164 | struct tpm_chip *chip = dev_get_drvdata(&pdev->dev); |
| 165 | |
Andrew Morton | b888c87 | 2005-10-30 15:03:28 -0800 | [diff] [blame] | 166 | if (chip) { |
Kylene Jo Hall | 90dda52 | 2006-04-22 02:37:15 -0700 | [diff] [blame] | 167 | if (chip->vendor.have_region) |
| 168 | atmel_release_region(chip->vendor.base, |
| 169 | chip->vendor.region_size); |
| 170 | atmel_put_base_addr(chip->vendor.iobase); |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 171 | tpm_remove_hardware(chip->dev); |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 172 | platform_device_unregister(pdev); |
Kylene Jo Hall | 682e97a | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 173 | } |
Kylene Jo Hall | e659a3f | 2005-10-30 15:03:24 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Rafael J. Wysocki | 8324be0 | 2012-07-06 19:09:13 +0200 | [diff] [blame] | 176 | static SIMPLE_DEV_PM_OPS(tpm_atml_pm, tpm_pm_suspend, tpm_pm_resume); |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 177 | |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 178 | static struct platform_driver atml_drv = { |
| 179 | .driver = { |
| 180 | .name = "tpm_atmel", |
| 181 | .owner = THIS_MODULE, |
Rafael J. Wysocki | 8324be0 | 2012-07-06 19:09:13 +0200 | [diff] [blame] | 182 | .pm = &tpm_atml_pm, |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 183 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | static int __init init_atmel(void) |
| 187 | { |
Kylene Jo Hall | 682e97a | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 188 | int rc = 0; |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 189 | void __iomem *iobase = NULL; |
| 190 | int have_region, region_size; |
| 191 | unsigned long base; |
| 192 | struct tpm_chip *chip; |
Kylene Jo Hall | 682e97a | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 193 | |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 194 | rc = platform_driver_register(&atml_drv); |
Jeff Garzik | f33d9bd | 2006-10-11 01:21:51 -0700 | [diff] [blame] | 195 | if (rc) |
| 196 | return rc; |
Kylene Jo Hall | 682e97a | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 197 | |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 198 | if ((iobase = atmel_get_base_addr(&base, ®ion_size)) == NULL) { |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 199 | rc = -ENODEV; |
| 200 | goto err_unreg_drv; |
Kylene Jo Hall | 682e97a | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 203 | have_region = |
Kylene Jo Hall | 90612b3 | 2005-11-18 01:10:58 -0800 | [diff] [blame] | 204 | (atmel_request_region |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 205 | (tpm_atmel.base, region_size, "tpm_atmel0") == NULL) ? 0 : 1; |
| 206 | |
Jeff Garzik | f33d9bd | 2006-10-11 01:21:51 -0700 | [diff] [blame] | 207 | pdev = platform_device_register_simple("tpm_atmel", -1, NULL, 0); |
| 208 | if (IS_ERR(pdev)) { |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 209 | rc = PTR_ERR(pdev); |
| 210 | goto err_rel_reg; |
Kylene Jo Hall | 682e97a | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 213 | if (!(chip = tpm_register_hardware(&pdev->dev, &tpm_atmel))) { |
| 214 | rc = -ENODEV; |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 215 | goto err_unreg_dev; |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | chip->vendor.iobase = iobase; |
| 219 | chip->vendor.base = base; |
| 220 | chip->vendor.have_region = have_region; |
| 221 | chip->vendor.region_size = region_size; |
| 222 | |
Kylene Jo Hall | 682e97a | 2005-10-30 15:03:25 -0800 | [diff] [blame] | 223 | return 0; |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 224 | |
| 225 | err_unreg_dev: |
| 226 | platform_device_unregister(pdev); |
| 227 | err_rel_reg: |
Kylene Jo Hall | e0dd03c | 2006-04-22 02:37:26 -0700 | [diff] [blame] | 228 | atmel_put_base_addr(iobase); |
| 229 | if (have_region) |
| 230 | atmel_release_region(base, |
| 231 | region_size); |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 232 | err_unreg_drv: |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 233 | platform_driver_unregister(&atml_drv); |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 234 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void __exit cleanup_atmel(void) |
| 238 | { |
Ming Lei | 7a192ec | 2009-02-06 23:40:12 +0800 | [diff] [blame] | 239 | platform_driver_unregister(&atml_drv); |
Kylene Jo Hall | ad5ea3c | 2005-11-13 16:07:41 -0800 | [diff] [blame] | 240 | atml_plat_remove(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | module_init(init_atmel); |
| 244 | module_exit(cleanup_atmel); |
| 245 | |
| 246 | MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); |
| 247 | MODULE_DESCRIPTION("TPM Driver"); |
| 248 | MODULE_VERSION("2.0"); |
| 249 | MODULE_LICENSE("GPL"); |