xref: /linux/drivers/char/tpm/tpm_atmel.h (revision ee1779840d093ebf6893c97115422fb5171b54d7)
1 /*
2  * Copyright (C) 2005 IBM Corporation
3  *
4  * Authors:
5  * Kylene Hall <kjhall@us.ibm.com>
6  *
7  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
8  *
9  * Device driver for TCG/TCPA TPM (trusted platform module).
10  * Specifications at www.trustedcomputinggroup.org
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation, version 2 of the
15  * License.
16  *
17  * These difference are required on power because the device must be
18  * discovered through the device tree and iomap must be used to get
19  * around the need for holes in the io_page_mask.  This does not happen
20  * automatically because the tpm is not a normal pci device and lives
21  * under the root node.
22  *
23  */
24 
25 struct tpm_atmel_priv {
26 	int region_size;
27 	int have_region;
28 	unsigned long base;
29 };
30 
31 static inline struct tpm_atmel_priv *atmel_get_priv(struct tpm_chip *chip)
32 {
33 	return chip->vendor.priv;
34 }
35 
36 #ifdef CONFIG_PPC64
37 
38 #include <asm/prom.h>
39 
40 #define atmel_getb(chip, offset) readb(chip->vendor->iobase + offset);
41 #define atmel_putb(val, chip, offset) writeb(val, chip->vendor->iobase + offset)
42 #define atmel_request_region request_mem_region
43 #define atmel_release_region release_mem_region
44 
45 static inline void atmel_put_base_addr(void __iomem *iobase)
46 {
47 	iounmap(iobase);
48 }
49 
50 static void __iomem * atmel_get_base_addr(unsigned long *base, int *region_size)
51 {
52 	struct device_node *dn;
53 	unsigned long address, size;
54 	const unsigned int *reg;
55 	int reglen;
56 	int naddrc;
57 	int nsizec;
58 
59 	dn = of_find_node_by_name(NULL, "tpm");
60 
61 	if (!dn)
62 		return NULL;
63 
64 	if (!of_device_is_compatible(dn, "AT97SC3201")) {
65 		of_node_put(dn);
66 		return NULL;
67 	}
68 
69 	reg = of_get_property(dn, "reg", &reglen);
70 	naddrc = of_n_addr_cells(dn);
71 	nsizec = of_n_size_cells(dn);
72 
73 	of_node_put(dn);
74 
75 
76 	if (naddrc == 2)
77 		address = ((unsigned long) reg[0] << 32) | reg[1];
78 	else
79 		address = reg[0];
80 
81 	if (nsizec == 2)
82 		size =
83 		    ((unsigned long) reg[naddrc] << 32) | reg[naddrc + 1];
84 	else
85 		size = reg[naddrc];
86 
87 	*base = address;
88 	*region_size = size;
89 	return ioremap(*base, *region_size);
90 }
91 #else
92 #define atmel_getb(chip, offset) inb(atmel_get_priv(chip)->base + offset)
93 #define atmel_putb(val, chip, offset) \
94 	outb(val, atmel_get_priv(chip)->base + offset)
95 #define atmel_request_region request_region
96 #define atmel_release_region release_region
97 /* Atmel definitions */
98 enum tpm_atmel_addr {
99 	TPM_ATMEL_BASE_ADDR_LO = 0x08,
100 	TPM_ATMEL_BASE_ADDR_HI = 0x09
101 };
102 
103 /* Verify this is a 1.1 Atmel TPM */
104 static int atmel_verify_tpm11(void)
105 {
106 
107 	/* verify that it is an Atmel part */
108 	if (tpm_read_index(TPM_ADDR, 4) != 'A' ||
109 	    tpm_read_index(TPM_ADDR, 5) != 'T' ||
110 	    tpm_read_index(TPM_ADDR, 6) != 'M' ||
111 	    tpm_read_index(TPM_ADDR, 7) != 'L')
112 		return 1;
113 
114 	/* query chip for its version number */
115 	if (tpm_read_index(TPM_ADDR, 0x00) != 1 ||
116 	    tpm_read_index(TPM_ADDR, 0x01) != 1)
117 		return 1;
118 
119 	/* This is an atmel supported part */
120 	return 0;
121 }
122 
123 static inline void atmel_put_base_addr(void __iomem *iobase)
124 {
125 }
126 
127 /* Determine where to talk to device */
128 static void __iomem * atmel_get_base_addr(unsigned long *base, int *region_size)
129 {
130 	int lo, hi;
131 
132 	if (atmel_verify_tpm11() != 0)
133 		return NULL;
134 
135 	lo = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_LO);
136 	hi = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_HI);
137 
138 	*base = (hi << 8) | lo;
139 	*region_size = 2;
140 
141 	return ioport_map(*base, *region_size);
142 }
143 #endif
144