xref: /linux/drivers/char/tpm/tpm_atmel.h (revision 4eea703caaac025dd422bd3916b14523977f4332)
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 	void __iomem *iobase;
30 };
31 
32 static inline struct tpm_atmel_priv *atmel_get_priv(struct tpm_chip *chip)
33 {
34 	return chip->vendor.priv;
35 }
36 
37 #ifdef CONFIG_PPC64
38 
39 #include <asm/prom.h>
40 
41 #define atmel_getb(priv, offset) readb(priv->iobase + offset)
42 #define atmel_putb(val, priv, offset) writeb(val, priv->iobase + offset)
43 #define atmel_request_region request_mem_region
44 #define atmel_release_region release_mem_region
45 
46 static inline void atmel_put_base_addr(void __iomem *iobase)
47 {
48 	iounmap(iobase);
49 }
50 
51 static void __iomem * atmel_get_base_addr(unsigned long *base, int *region_size)
52 {
53 	struct device_node *dn;
54 	unsigned long address, size;
55 	const unsigned int *reg;
56 	int reglen;
57 	int naddrc;
58 	int nsizec;
59 
60 	dn = of_find_node_by_name(NULL, "tpm");
61 
62 	if (!dn)
63 		return NULL;
64 
65 	if (!of_device_is_compatible(dn, "AT97SC3201")) {
66 		of_node_put(dn);
67 		return NULL;
68 	}
69 
70 	reg = of_get_property(dn, "reg", &reglen);
71 	naddrc = of_n_addr_cells(dn);
72 	nsizec = of_n_size_cells(dn);
73 
74 	of_node_put(dn);
75 
76 
77 	if (naddrc == 2)
78 		address = ((unsigned long) reg[0] << 32) | reg[1];
79 	else
80 		address = reg[0];
81 
82 	if (nsizec == 2)
83 		size =
84 		    ((unsigned long) reg[naddrc] << 32) | reg[naddrc + 1];
85 	else
86 		size = reg[naddrc];
87 
88 	*base = address;
89 	*region_size = size;
90 	return ioremap(*base, *region_size);
91 }
92 #else
93 #define atmel_getb(chip, offset) inb(atmel_get_priv(chip)->base + offset)
94 #define atmel_putb(val, chip, offset) \
95 	outb(val, atmel_get_priv(chip)->base + offset)
96 #define atmel_request_region request_region
97 #define atmel_release_region release_region
98 /* Atmel definitions */
99 enum tpm_atmel_addr {
100 	TPM_ATMEL_BASE_ADDR_LO = 0x08,
101 	TPM_ATMEL_BASE_ADDR_HI = 0x09
102 };
103 
104 /* Verify this is a 1.1 Atmel TPM */
105 static int atmel_verify_tpm11(void)
106 {
107 
108 	/* verify that it is an Atmel part */
109 	if (tpm_read_index(TPM_ADDR, 4) != 'A' ||
110 	    tpm_read_index(TPM_ADDR, 5) != 'T' ||
111 	    tpm_read_index(TPM_ADDR, 6) != 'M' ||
112 	    tpm_read_index(TPM_ADDR, 7) != 'L')
113 		return 1;
114 
115 	/* query chip for its version number */
116 	if (tpm_read_index(TPM_ADDR, 0x00) != 1 ||
117 	    tpm_read_index(TPM_ADDR, 0x01) != 1)
118 		return 1;
119 
120 	/* This is an atmel supported part */
121 	return 0;
122 }
123 
124 static inline void atmel_put_base_addr(void __iomem *iobase)
125 {
126 }
127 
128 /* Determine where to talk to device */
129 static void __iomem * atmel_get_base_addr(unsigned long *base, int *region_size)
130 {
131 	int lo, hi;
132 
133 	if (atmel_verify_tpm11() != 0)
134 		return NULL;
135 
136 	lo = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_LO);
137 	hi = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_HI);
138 
139 	*base = (hi << 8) | lo;
140 	*region_size = 2;
141 
142 	return ioport_map(*base, *region_size);
143 }
144 #endif
145