2 * Copyright (C) 2004 IBM Corporation
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>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
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
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
26 /* National definitions */
29 TPM_NSC_BASE0_HI = 0x60,
30 TPM_NSC_BASE0_LO = 0x61,
31 TPM_NSC_BASE1_HI = 0x62,
32 TPM_NSC_BASE1_LO = 0x63
45 enum tpm_nsc_status_loc {
53 NSC_STATUS_OBF = 0x01, /* output buffer full */
54 NSC_STATUS_IBF = 0x02, /* input buffer full */
55 NSC_STATUS_F0 = 0x04, /* F0 */
56 NSC_STATUS_A2 = 0x08, /* A2 */
57 NSC_STATUS_RDY = 0x10, /* ready to receive command */
58 NSC_STATUS_IBR = 0x20 /* ready to receive data */
62 enum tpm_nsc_cmd_mode {
63 NSC_COMMAND_NORMAL = 0x01, /* normal mode */
64 NSC_COMMAND_EOC = 0x03,
65 NSC_COMMAND_CANCEL = 0x22
72 static inline struct tpm_nsc_priv *nsc_get_priv(struct tpm_chip *chip)
74 return chip->vendor.priv;
78 * Wait for a certain status to appear
80 static int wait_for_stat(struct tpm_chip *chip, u8 mask, u8 val, u8 * data)
84 /* status immediately available check */
85 *data = inb(nsc_get_priv(chip)->base + NSC_STATUS);
86 if ((*data & mask) == val)
90 stop = jiffies + 10 * HZ;
93 *data = inb(nsc_get_priv(chip)->base + 1);
94 if ((*data & mask) == val)
97 while (time_before(jiffies, stop));
102 static int nsc_wait_for_ready(struct tpm_chip *chip)
107 /* status immediately available check */
108 status = inb(nsc_get_priv(chip)->base + NSC_STATUS);
109 if (status & NSC_STATUS_OBF)
110 status = inb(nsc_get_priv(chip)->base + NSC_DATA);
111 if (status & NSC_STATUS_RDY)
114 /* wait for status */
115 stop = jiffies + 100;
118 status = inb(nsc_get_priv(chip)->base + NSC_STATUS);
119 if (status & NSC_STATUS_OBF)
120 status = inb(nsc_get_priv(chip)->base + NSC_DATA);
121 if (status & NSC_STATUS_RDY)
124 while (time_before(jiffies, stop));
126 dev_info(&chip->dev, "wait for ready failed\n");
131 static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
141 if (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0) {
142 dev_err(&chip->dev, "F0 timeout\n");
146 data = inb(nsc_get_priv(chip)->base + NSC_DATA);
147 if (data != NSC_COMMAND_NORMAL) {
148 dev_err(&chip->dev, "not in normal mode (0x%x)\n",
153 /* read the whole packet */
154 for (p = buffer; p < &buffer[count]; p++) {
156 (chip, NSC_STATUS_OBF, NSC_STATUS_OBF, &data) < 0) {
158 "OBF timeout (while reading data)\n");
161 if (data & NSC_STATUS_F0)
163 *p = inb(nsc_get_priv(chip)->base + NSC_DATA);
166 if ((data & NSC_STATUS_F0) == 0 &&
167 (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0)) {
168 dev_err(&chip->dev, "F0 not set\n");
172 data = inb(nsc_get_priv(chip)->base + NSC_DATA);
173 if (data != NSC_COMMAND_EOC) {
175 "expected end of command(0x%x)\n", data);
179 native_size = (__force __be32 *) (buf + 2);
180 size = be32_to_cpu(*native_size);
188 static int tpm_nsc_send(struct tpm_chip *chip, u8 * buf, size_t count)
194 * If we hit the chip with back to back commands it locks up
195 * and never set IBF. Hitting it with this "hammer" seems to
196 * fix it. Not sure why this is needed, we followed the flow
197 * chart in the manual to the letter.
199 outb(NSC_COMMAND_CANCEL, nsc_get_priv(chip)->base + NSC_COMMAND);
201 if (nsc_wait_for_ready(chip) != 0)
204 if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
205 dev_err(&chip->dev, "IBF timeout\n");
209 outb(NSC_COMMAND_NORMAL, nsc_get_priv(chip)->base + NSC_COMMAND);
210 if (wait_for_stat(chip, NSC_STATUS_IBR, NSC_STATUS_IBR, &data) < 0) {
211 dev_err(&chip->dev, "IBR timeout\n");
215 for (i = 0; i < count; i++) {
216 if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
218 "IBF timeout (while writing data)\n");
221 outb(buf[i], nsc_get_priv(chip)->base + NSC_DATA);
224 if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
225 dev_err(&chip->dev, "IBF timeout\n");
228 outb(NSC_COMMAND_EOC, nsc_get_priv(chip)->base + NSC_COMMAND);
233 static void tpm_nsc_cancel(struct tpm_chip *chip)
235 outb(NSC_COMMAND_CANCEL, nsc_get_priv(chip)->base + NSC_COMMAND);
238 static u8 tpm_nsc_status(struct tpm_chip *chip)
240 return inb(nsc_get_priv(chip)->base + NSC_STATUS);
243 static bool tpm_nsc_req_canceled(struct tpm_chip *chip, u8 status)
245 return (status == NSC_STATUS_RDY);
248 static const struct tpm_class_ops tpm_nsc = {
249 .recv = tpm_nsc_recv,
250 .send = tpm_nsc_send,
251 .cancel = tpm_nsc_cancel,
252 .status = tpm_nsc_status,
253 .req_complete_mask = NSC_STATUS_OBF,
254 .req_complete_val = NSC_STATUS_OBF,
255 .req_canceled = tpm_nsc_req_canceled,
258 static struct platform_device *pdev = NULL;
260 static void tpm_nsc_remove(struct device *dev)
262 struct tpm_chip *chip = dev_get_drvdata(dev);
264 tpm_chip_unregister(chip);
265 release_region(nsc_get_priv(chip)->base, 2);
268 static SIMPLE_DEV_PM_OPS(tpm_nsc_pm, tpm_pm_suspend, tpm_pm_resume);
270 static struct platform_driver nsc_drv = {
277 static int __init init_nsc(void)
281 int nscAddrBase = TPM_ADDR;
282 struct tpm_chip *chip;
284 struct tpm_nsc_priv *priv;
286 /* verify that it is a National part (SID) */
287 if (tpm_read_index(TPM_ADDR, NSC_SID_INDEX) != 0xEF) {
288 nscAddrBase = (tpm_read_index(TPM_SUPERIO_ADDR, 0x2C)<<8)|
289 (tpm_read_index(TPM_SUPERIO_ADDR, 0x2B)&0xFE);
290 if (tpm_read_index(nscAddrBase, NSC_SID_INDEX) != 0xF6)
294 err = platform_driver_register(&nsc_drv);
298 hi = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_HI);
299 lo = tpm_read_index(nscAddrBase, TPM_NSC_BASE0_LO);
302 /* enable the DPM module */
303 tpm_write_index(nscAddrBase, NSC_LDC_INDEX, 0x01);
305 pdev = platform_device_alloc("tpm_nscl0", -1);
311 pdev->num_resources = 0;
312 pdev->dev.driver = &nsc_drv.driver;
313 pdev->dev.release = tpm_nsc_remove;
315 if ((rc = platform_device_add(pdev)) < 0)
318 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
326 if (request_region(base, 2, "tpm_nsc0") == NULL ) {
331 chip = tpmm_chip_alloc(&pdev->dev, &tpm_nsc);
337 chip->vendor.priv = priv;
339 rc = tpm_chip_register(chip);
343 dev_dbg(&pdev->dev, "NSC TPM detected\n");
345 "NSC LDN 0x%x, SID 0x%x, SRID 0x%x\n",
346 tpm_read_index(nscAddrBase,0x07), tpm_read_index(nscAddrBase,0x20),
347 tpm_read_index(nscAddrBase,0x27));
349 "NSC SIOCF1 0x%x SIOCF5 0x%x SIOCF6 0x%x SIOCF8 0x%x\n",
350 tpm_read_index(nscAddrBase,0x21), tpm_read_index(nscAddrBase,0x25),
351 tpm_read_index(nscAddrBase,0x26), tpm_read_index(nscAddrBase,0x28));
352 dev_dbg(&pdev->dev, "NSC IO Base0 0x%x\n",
353 (tpm_read_index(nscAddrBase,0x60) << 8) | tpm_read_index(nscAddrBase,0x61));
354 dev_dbg(&pdev->dev, "NSC IO Base1 0x%x\n",
355 (tpm_read_index(nscAddrBase,0x62) << 8) | tpm_read_index(nscAddrBase,0x63));
356 dev_dbg(&pdev->dev, "NSC Interrupt number and wakeup 0x%x\n",
357 tpm_read_index(nscAddrBase,0x70));
358 dev_dbg(&pdev->dev, "NSC IRQ type select 0x%x\n",
359 tpm_read_index(nscAddrBase,0x71));
361 "NSC DMA channel select0 0x%x, select1 0x%x\n",
362 tpm_read_index(nscAddrBase,0x74), tpm_read_index(nscAddrBase,0x75));
365 "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
366 tpm_read_index(nscAddrBase,0xF0), tpm_read_index(nscAddrBase,0xF1),
367 tpm_read_index(nscAddrBase,0xF2), tpm_read_index(nscAddrBase,0xF3),
368 tpm_read_index(nscAddrBase,0xF4), tpm_read_index(nscAddrBase,0xF5),
369 tpm_read_index(nscAddrBase,0xF6), tpm_read_index(nscAddrBase,0xF7),
370 tpm_read_index(nscAddrBase,0xF8), tpm_read_index(nscAddrBase,0xF9));
373 "NSC TPM revision %d\n",
374 tpm_read_index(nscAddrBase, 0x27) & 0x1F);
379 release_region(base, 2);
381 platform_device_del(pdev);
383 platform_device_put(pdev);
385 platform_driver_unregister(&nsc_drv);
389 static void __exit cleanup_nsc(void)
392 tpm_nsc_remove(&pdev->dev);
393 platform_device_unregister(pdev);
396 platform_driver_unregister(&nsc_drv);
399 module_init(init_nsc);
400 module_exit(cleanup_nsc);
402 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
403 MODULE_DESCRIPTION("TPM Driver");
404 MODULE_VERSION("2.0");
405 MODULE_LICENSE("GPL");