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 <linux/platform_device.h> 23 #include "tpm.h" 24 25 /* Atmel definitions */ 26 enum tpm_atmel_addr { 27 TPM_ATMEL_BASE_ADDR_LO = 0x08, 28 TPM_ATMEL_BASE_ADDR_HI = 0x09 29 }; 30 31 /* write status bits */ 32 enum tpm_atmel_write_status { 33 ATML_STATUS_ABORT = 0x01, 34 ATML_STATUS_LASTBYTE = 0x04 35 }; 36 /* read status bits */ 37 enum tpm_atmel_read_status { 38 ATML_STATUS_BUSY = 0x01, 39 ATML_STATUS_DATA_AVAIL = 0x02, 40 ATML_STATUS_REWRITE = 0x04, 41 ATML_STATUS_READY = 0x08 42 }; 43 44 static int tpm_atml_recv(struct tpm_chip *chip, u8 *buf, size_t count) 45 { 46 u8 status, *hdr = buf; 47 u32 size; 48 int i; 49 __be32 *native_size; 50 51 /* start reading header */ 52 if (count < 6) 53 return -EIO; 54 55 for (i = 0; i < 6; i++) { 56 status = inb(chip->vendor->base + 1); 57 if ((status & ATML_STATUS_DATA_AVAIL) == 0) { 58 dev_err(chip->dev, 59 "error reading header\n"); 60 return -EIO; 61 } 62 *buf++ = inb(chip->vendor->base); 63 } 64 65 /* size of the data received */ 66 native_size = (__force __be32 *) (hdr + 2); 67 size = be32_to_cpu(*native_size); 68 69 if (count < size) { 70 dev_err(chip->dev, 71 "Recv size(%d) less than available space\n", size); 72 for (; i < size; i++) { /* clear the waiting data anyway */ 73 status = inb(chip->vendor->base + 1); 74 if ((status & ATML_STATUS_DATA_AVAIL) == 0) { 75 dev_err(chip->dev, 76 "error reading data\n"); 77 return -EIO; 78 } 79 } 80 return -EIO; 81 } 82 83 /* read all the data available */ 84 for (; i < size; i++) { 85 status = inb(chip->vendor->base + 1); 86 if ((status & ATML_STATUS_DATA_AVAIL) == 0) { 87 dev_err(chip->dev, 88 "error reading data\n"); 89 return -EIO; 90 } 91 *buf++ = inb(chip->vendor->base); 92 } 93 94 /* make sure data available is gone */ 95 status = inb(chip->vendor->base + 1); 96 if (status & ATML_STATUS_DATA_AVAIL) { 97 dev_err(chip->dev, "data available is stuck\n"); 98 return -EIO; 99 } 100 101 return size; 102 } 103 104 static int tpm_atml_send(struct tpm_chip *chip, u8 *buf, size_t count) 105 { 106 int i; 107 108 dev_dbg(chip->dev, "tpm_atml_send:\n"); 109 for (i = 0; i < count; i++) { 110 dev_dbg(chip->dev, "%d 0x%x(%d)\n", i, buf[i], buf[i]); 111 outb(buf[i], chip->vendor->base); 112 } 113 114 return count; 115 } 116 117 static void tpm_atml_cancel(struct tpm_chip *chip) 118 { 119 outb(ATML_STATUS_ABORT, chip->vendor->base + 1); 120 } 121 122 static u8 tpm_atml_status(struct tpm_chip *chip) 123 { 124 return inb(chip->vendor->base + 1); 125 } 126 127 static struct file_operations atmel_ops = { 128 .owner = THIS_MODULE, 129 .llseek = no_llseek, 130 .open = tpm_open, 131 .read = tpm_read, 132 .write = tpm_write, 133 .release = tpm_release, 134 }; 135 136 static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL); 137 static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL); 138 static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps, NULL); 139 static DEVICE_ATTR(cancel, S_IWUSR |S_IWGRP, NULL, tpm_store_cancel); 140 141 static struct attribute* atmel_attrs[] = { 142 &dev_attr_pubek.attr, 143 &dev_attr_pcrs.attr, 144 &dev_attr_caps.attr, 145 &dev_attr_cancel.attr, 146 NULL, 147 }; 148 149 static struct attribute_group atmel_attr_grp = { .attrs = atmel_attrs }; 150 151 static struct tpm_vendor_specific tpm_atmel = { 152 .recv = tpm_atml_recv, 153 .send = tpm_atml_send, 154 .cancel = tpm_atml_cancel, 155 .status = tpm_atml_status, 156 .req_complete_mask = ATML_STATUS_BUSY | ATML_STATUS_DATA_AVAIL, 157 .req_complete_val = ATML_STATUS_DATA_AVAIL, 158 .req_canceled = ATML_STATUS_READY, 159 .attr_group = &atmel_attr_grp, 160 .miscdev = { .fops = &atmel_ops, }, 161 }; 162 163 static struct platform_device *pdev; 164 165 static void __devexit tpm_atml_remove(struct device *dev) 166 { 167 struct tpm_chip *chip = dev_get_drvdata(dev); 168 if (chip) { 169 release_region(chip->vendor->base, 2); 170 tpm_remove_hardware(chip->dev); 171 } 172 } 173 174 static struct device_driver atml_drv = { 175 .name = "tpm_atmel", 176 .bus = &platform_bus_type, 177 .owner = THIS_MODULE, 178 .suspend = tpm_pm_suspend, 179 .resume = tpm_pm_resume, 180 }; 181 182 static int __init init_atmel(void) 183 { 184 int rc = 0; 185 int lo, hi; 186 187 driver_register(&atml_drv); 188 189 lo = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_LO); 190 hi = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_HI); 191 192 tpm_atmel.base = (hi<<8)|lo; 193 194 /* verify that it is an Atmel part */ 195 if (tpm_read_index(TPM_ADDR, 4) != 'A' || tpm_read_index(TPM_ADDR, 5) != 'T' 196 || tpm_read_index(TPM_ADDR, 6) != 'M' || tpm_read_index(TPM_ADDR, 7) != 'L') { 197 return -ENODEV; 198 } 199 200 /* verify chip version number is 1.1 */ 201 if ( (tpm_read_index(TPM_ADDR, 0x00) != 0x01) || 202 (tpm_read_index(TPM_ADDR, 0x01) != 0x01 )) 203 return -ENODEV; 204 205 pdev = kzalloc(sizeof(struct platform_device), GFP_KERNEL); 206 if ( !pdev ) 207 return -ENOMEM; 208 209 pdev->name = "tpm_atmel0"; 210 pdev->id = -1; 211 pdev->num_resources = 0; 212 pdev->dev.release = tpm_atml_remove; 213 pdev->dev.driver = &atml_drv; 214 215 if ((rc = platform_device_register(pdev)) < 0) { 216 kfree(pdev); 217 pdev = NULL; 218 return rc; 219 } 220 221 if (request_region(tpm_atmel.base, 2, "tpm_atmel0") == NULL ) { 222 platform_device_unregister(pdev); 223 kfree(pdev); 224 pdev = NULL; 225 return -EBUSY; 226 } 227 228 if ((rc = tpm_register_hardware(&pdev->dev, &tpm_atmel)) < 0) { 229 release_region(tpm_atmel.base, 2); 230 platform_device_unregister(pdev); 231 kfree(pdev); 232 pdev = NULL; 233 return rc; 234 } 235 236 dev_info(&pdev->dev, "Atmel TPM 1.1, Base Address: 0x%x\n", 237 tpm_atmel.base); 238 return 0; 239 } 240 241 static void __exit cleanup_atmel(void) 242 { 243 if (pdev) { 244 tpm_atml_remove(&pdev->dev); 245 platform_device_unregister(pdev); 246 kfree(pdev); 247 pdev = NULL; 248 } 249 250 driver_unregister(&atml_drv); 251 } 252 253 module_init(init_atmel); 254 module_exit(cleanup_atmel); 255 256 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); 257 MODULE_DESCRIPTION("TPM Driver"); 258 MODULE_VERSION("2.0"); 259 MODULE_LICENSE("GPL"); 260