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
68 * Wait for a certain status to appear
70 static int wait_for_stat(struct tpm_chip
*chip
, u8 mask
, u8 val
, u8
* data
)
74 /* status immediately available check */
75 *data
= inb(chip
->vendor
.base
+ NSC_STATUS
);
76 if ((*data
& mask
) == val
)
80 stop
= jiffies
+ 10 * HZ
;
83 *data
= inb(chip
->vendor
.base
+ 1);
84 if ((*data
& mask
) == val
)
87 while (time_before(jiffies
, stop
));
92 static int nsc_wait_for_ready(struct tpm_chip
*chip
)
97 /* status immediately available check */
98 status
= inb(chip
->vendor
.base
+ NSC_STATUS
);
99 if (status
& NSC_STATUS_OBF
)
100 status
= inb(chip
->vendor
.base
+ NSC_DATA
);
101 if (status
& NSC_STATUS_RDY
)
104 /* wait for status */
105 stop
= jiffies
+ 100;
108 status
= inb(chip
->vendor
.base
+ NSC_STATUS
);
109 if (status
& NSC_STATUS_OBF
)
110 status
= inb(chip
->vendor
.base
+ NSC_DATA
);
111 if (status
& NSC_STATUS_RDY
)
114 while (time_before(jiffies
, stop
));
116 dev_info(chip
->dev
, "wait for ready failed\n");
121 static int tpm_nsc_recv(struct tpm_chip
*chip
, u8
* buf
, size_t count
)
131 if (wait_for_stat(chip
, NSC_STATUS_F0
, NSC_STATUS_F0
, &data
) < 0) {
132 dev_err(chip
->dev
, "F0 timeout\n");
136 inb(chip
->vendor
.base
+ NSC_DATA
)) != NSC_COMMAND_NORMAL
) {
137 dev_err(chip
->dev
, "not in normal mode (0x%x)\n",
142 /* read the whole packet */
143 for (p
= buffer
; p
< &buffer
[count
]; p
++) {
145 (chip
, NSC_STATUS_OBF
, NSC_STATUS_OBF
, &data
) < 0) {
147 "OBF timeout (while reading data)\n");
150 if (data
& NSC_STATUS_F0
)
152 *p
= inb(chip
->vendor
.base
+ NSC_DATA
);
155 if ((data
& NSC_STATUS_F0
) == 0 &&
156 (wait_for_stat(chip
, NSC_STATUS_F0
, NSC_STATUS_F0
, &data
) < 0)) {
157 dev_err(chip
->dev
, "F0 not set\n");
160 if ((data
= inb(chip
->vendor
.base
+ NSC_DATA
)) != NSC_COMMAND_EOC
) {
162 "expected end of command(0x%x)\n", data
);
166 native_size
= (__force __be32
*) (buf
+ 2);
167 size
= be32_to_cpu(*native_size
);
175 static int tpm_nsc_send(struct tpm_chip
*chip
, u8
* buf
, size_t count
)
181 * If we hit the chip with back to back commands it locks up
182 * and never set IBF. Hitting it with this "hammer" seems to
183 * fix it. Not sure why this is needed, we followed the flow
184 * chart in the manual to the letter.
186 outb(NSC_COMMAND_CANCEL
, chip
->vendor
.base
+ NSC_COMMAND
);
188 if (nsc_wait_for_ready(chip
) != 0)
191 if (wait_for_stat(chip
, NSC_STATUS_IBF
, 0, &data
) < 0) {
192 dev_err(chip
->dev
, "IBF timeout\n");
196 outb(NSC_COMMAND_NORMAL
, chip
->vendor
.base
+ NSC_COMMAND
);
197 if (wait_for_stat(chip
, NSC_STATUS_IBR
, NSC_STATUS_IBR
, &data
) < 0) {
198 dev_err(chip
->dev
, "IBR timeout\n");
202 for (i
= 0; i
< count
; i
++) {
203 if (wait_for_stat(chip
, NSC_STATUS_IBF
, 0, &data
) < 0) {
205 "IBF timeout (while writing data)\n");
208 outb(buf
[i
], chip
->vendor
.base
+ NSC_DATA
);
211 if (wait_for_stat(chip
, NSC_STATUS_IBF
, 0, &data
) < 0) {
212 dev_err(chip
->dev
, "IBF timeout\n");
215 outb(NSC_COMMAND_EOC
, chip
->vendor
.base
+ NSC_COMMAND
);
220 static void tpm_nsc_cancel(struct tpm_chip
*chip
)
222 outb(NSC_COMMAND_CANCEL
, chip
->vendor
.base
+ NSC_COMMAND
);
225 static u8
tpm_nsc_status(struct tpm_chip
*chip
)
227 return inb(chip
->vendor
.base
+ NSC_STATUS
);
230 static const struct file_operations nsc_ops
= {
231 .owner
= THIS_MODULE
,
236 .release
= tpm_release
,
239 static DEVICE_ATTR(pubek
, S_IRUGO
, tpm_show_pubek
, NULL
);
240 static DEVICE_ATTR(pcrs
, S_IRUGO
, tpm_show_pcrs
, NULL
);
241 static DEVICE_ATTR(caps
, S_IRUGO
, tpm_show_caps
, NULL
);
242 static DEVICE_ATTR(cancel
, S_IWUSR
|S_IWGRP
, NULL
, tpm_store_cancel
);
244 static struct attribute
* nsc_attrs
[] = {
245 &dev_attr_pubek
.attr
,
248 &dev_attr_cancel
.attr
,
252 static struct attribute_group nsc_attr_grp
= { .attrs
= nsc_attrs
};
254 static const struct tpm_vendor_specific tpm_nsc
= {
255 .recv
= tpm_nsc_recv
,
256 .send
= tpm_nsc_send
,
257 .cancel
= tpm_nsc_cancel
,
258 .status
= tpm_nsc_status
,
259 .req_complete_mask
= NSC_STATUS_OBF
,
260 .req_complete_val
= NSC_STATUS_OBF
,
261 .req_canceled
= NSC_STATUS_RDY
,
262 .attr_group
= &nsc_attr_grp
,
263 .miscdev
= { .fops
= &nsc_ops
, },
266 static struct platform_device
*pdev
= NULL
;
268 static void tpm_nsc_remove(struct device
*dev
)
270 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
272 release_region(chip
->vendor
.base
, 2);
273 tpm_remove_hardware(chip
->dev
);
277 static int tpm_nsc_suspend(struct platform_device
*dev
, pm_message_t msg
)
279 return tpm_pm_suspend(&dev
->dev
, msg
);
282 static int tpm_nsc_resume(struct platform_device
*dev
)
284 return tpm_pm_resume(&dev
->dev
);
287 static struct platform_driver nsc_drv
= {
288 .suspend
= tpm_nsc_suspend
,
289 .resume
= tpm_nsc_resume
,
292 .owner
= THIS_MODULE
,
296 static int __init
init_nsc(void)
300 int nscAddrBase
= TPM_ADDR
;
301 struct tpm_chip
*chip
;
304 /* verify that it is a National part (SID) */
305 if (tpm_read_index(TPM_ADDR
, NSC_SID_INDEX
) != 0xEF) {
306 nscAddrBase
= (tpm_read_index(TPM_SUPERIO_ADDR
, 0x2C)<<8)|
307 (tpm_read_index(TPM_SUPERIO_ADDR
, 0x2B)&0xFE);
308 if (tpm_read_index(nscAddrBase
, NSC_SID_INDEX
) != 0xF6)
312 err
= platform_driver_register(&nsc_drv
);
316 hi
= tpm_read_index(nscAddrBase
, TPM_NSC_BASE0_HI
);
317 lo
= tpm_read_index(nscAddrBase
, TPM_NSC_BASE0_LO
);
320 /* enable the DPM module */
321 tpm_write_index(nscAddrBase
, NSC_LDC_INDEX
, 0x01);
323 pdev
= platform_device_alloc("tpm_nscl0", -1);
329 pdev
->num_resources
= 0;
330 pdev
->dev
.driver
= &nsc_drv
.driver
;
331 pdev
->dev
.release
= tpm_nsc_remove
;
333 if ((rc
= platform_device_add(pdev
)) < 0)
336 if (request_region(base
, 2, "tpm_nsc0") == NULL
) {
341 if (!(chip
= tpm_register_hardware(&pdev
->dev
, &tpm_nsc
))) {
346 dev_dbg(&pdev
->dev
, "NSC TPM detected\n");
348 "NSC LDN 0x%x, SID 0x%x, SRID 0x%x\n",
349 tpm_read_index(nscAddrBase
,0x07), tpm_read_index(nscAddrBase
,0x20),
350 tpm_read_index(nscAddrBase
,0x27));
352 "NSC SIOCF1 0x%x SIOCF5 0x%x SIOCF6 0x%x SIOCF8 0x%x\n",
353 tpm_read_index(nscAddrBase
,0x21), tpm_read_index(nscAddrBase
,0x25),
354 tpm_read_index(nscAddrBase
,0x26), tpm_read_index(nscAddrBase
,0x28));
355 dev_dbg(&pdev
->dev
, "NSC IO Base0 0x%x\n",
356 (tpm_read_index(nscAddrBase
,0x60) << 8) | tpm_read_index(nscAddrBase
,0x61));
357 dev_dbg(&pdev
->dev
, "NSC IO Base1 0x%x\n",
358 (tpm_read_index(nscAddrBase
,0x62) << 8) | tpm_read_index(nscAddrBase
,0x63));
359 dev_dbg(&pdev
->dev
, "NSC Interrupt number and wakeup 0x%x\n",
360 tpm_read_index(nscAddrBase
,0x70));
361 dev_dbg(&pdev
->dev
, "NSC IRQ type select 0x%x\n",
362 tpm_read_index(nscAddrBase
,0x71));
364 "NSC DMA channel select0 0x%x, select1 0x%x\n",
365 tpm_read_index(nscAddrBase
,0x74), tpm_read_index(nscAddrBase
,0x75));
368 "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
369 tpm_read_index(nscAddrBase
,0xF0), tpm_read_index(nscAddrBase
,0xF1),
370 tpm_read_index(nscAddrBase
,0xF2), tpm_read_index(nscAddrBase
,0xF3),
371 tpm_read_index(nscAddrBase
,0xF4), tpm_read_index(nscAddrBase
,0xF5),
372 tpm_read_index(nscAddrBase
,0xF6), tpm_read_index(nscAddrBase
,0xF7),
373 tpm_read_index(nscAddrBase
,0xF8), tpm_read_index(nscAddrBase
,0xF9));
376 "NSC TPM revision %d\n",
377 tpm_read_index(nscAddrBase
, 0x27) & 0x1F);
379 chip
->vendor
.base
= base
;
384 release_region(base
, 2);
386 platform_device_del(pdev
);
388 platform_device_put(pdev
);
390 platform_driver_unregister(&nsc_drv
);
394 static void __exit
cleanup_nsc(void)
397 tpm_nsc_remove(&pdev
->dev
);
398 platform_device_unregister(pdev
);
403 platform_driver_unregister(&nsc_drv
);
406 module_init(init_nsc
);
407 module_exit(cleanup_nsc
);
409 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
410 MODULE_DESCRIPTION("TPM Driver");
411 MODULE_VERSION("2.0");
412 MODULE_LICENSE("GPL");