blob: ba37e77e8af349554b9a924d0b786fcd048831b7 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -08002/*
3 * Copyright (C) 2005 IBM Corporation
4 *
5 * Authors:
6 * Kylene Hall <kjhall@us.ibm.com>
7 *
Kent Yoder8e81cc12007-08-22 14:01:04 -07008 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -08009 *
10 * Device driver for TCG/TCPA TPM (trusted platform module).
11 * Specifications at www.trustedcomputinggroup.org
12 *
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080013 * These difference are required on power because the device must be
14 * discovered through the device tree and iomap must be used to get
15 * around the need for holes in the io_page_mask. This does not happen
16 * automatically because the tpm is not a normal pci device and lives
17 * under the root node.
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080018 */
19
Jarkko Sakkinen23d06ff2016-03-23 07:10:22 +020020struct tpm_atmel_priv {
21 int region_size;
22 int have_region;
Jarkko Sakkinenee177982016-03-23 08:16:09 +020023 unsigned long base;
Christophe Ricard4eea7032016-03-31 22:56:55 +020024 void __iomem *iobase;
Jarkko Sakkinen23d06ff2016-03-23 07:10:22 +020025};
26
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080027#ifdef CONFIG_PPC64
Stephen Rothwellddf526e2007-05-12 10:36:56 -070028
29#include <asm/prom.h>
30
Christophe Ricard4eea7032016-03-31 22:56:55 +020031#define atmel_getb(priv, offset) readb(priv->iobase + offset)
32#define atmel_putb(val, priv, offset) writeb(val, priv->iobase + offset)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080033#define atmel_request_region request_mem_region
34#define atmel_release_region release_mem_region
Kylene Jo Hall90612b32005-11-18 01:10:58 -080035
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -070036static inline void atmel_put_base_addr(void __iomem *iobase)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080037{
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -070038 iounmap(iobase);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080039}
40
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -070041static void __iomem * atmel_get_base_addr(unsigned long *base, int *region_size)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080042{
43 struct device_node *dn;
44 unsigned long address, size;
Jeremy Kerr5c339e92006-07-12 15:41:30 +100045 const unsigned int *reg;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080046 int reglen;
47 int naddrc;
48 int nsizec;
49
50 dn = of_find_node_by_name(NULL, "tpm");
51
52 if (!dn)
Kylene Jo Hall90612b32005-11-18 01:10:58 -080053 return NULL;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080054
Stephen Rothwell55b61fe2007-05-03 17:26:52 +100055 if (!of_device_is_compatible(dn, "AT97SC3201")) {
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080056 of_node_put(dn);
Kylene Jo Hall90612b32005-11-18 01:10:58 -080057 return NULL;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080058 }
59
Stephen Rothwell40cd3a42007-05-01 13:54:02 +100060 reg = of_get_property(dn, "reg", &reglen);
Stephen Rothwella8bda5d2007-04-03 10:56:50 +100061 naddrc = of_n_addr_cells(dn);
Stephen Rothwell9213fee2007-04-03 10:57:48 +100062 nsizec = of_n_size_cells(dn);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080063
64 of_node_put(dn);
65
66
67 if (naddrc == 2)
68 address = ((unsigned long) reg[0] << 32) | reg[1];
69 else
70 address = reg[0];
71
72 if (nsizec == 2)
73 size =
74 ((unsigned long) reg[naddrc] << 32) | reg[naddrc + 1];
75 else
76 size = reg[naddrc];
77
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -070078 *base = address;
79 *region_size = size;
80 return ioremap(*base, *region_size);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080081}
82#else
Jarkko Sakkinenee177982016-03-23 08:16:09 +020083#define atmel_getb(chip, offset) inb(atmel_get_priv(chip)->base + offset)
84#define atmel_putb(val, chip, offset) \
85 outb(val, atmel_get_priv(chip)->base + offset)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080086#define atmel_request_region request_region
87#define atmel_release_region release_region
88/* Atmel definitions */
89enum tpm_atmel_addr {
90 TPM_ATMEL_BASE_ADDR_LO = 0x08,
91 TPM_ATMEL_BASE_ADDR_HI = 0x09
92};
93
Jarkko Sakkinen1d191552017-01-25 16:47:39 +020094static inline int tpm_read_index(int base, int index)
95{
96 outb(index, base);
97 return inb(base+1) & 0xFF;
98}
99
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800100/* Verify this is a 1.1 Atmel TPM */
101static int atmel_verify_tpm11(void)
102{
103
104 /* verify that it is an Atmel part */
105 if (tpm_read_index(TPM_ADDR, 4) != 'A' ||
106 tpm_read_index(TPM_ADDR, 5) != 'T' ||
107 tpm_read_index(TPM_ADDR, 6) != 'M' ||
108 tpm_read_index(TPM_ADDR, 7) != 'L')
109 return 1;
110
111 /* query chip for its version number */
112 if (tpm_read_index(TPM_ADDR, 0x00) != 1 ||
113 tpm_read_index(TPM_ADDR, 0x01) != 1)
114 return 1;
115
116 /* This is an atmel supported part */
117 return 0;
118}
119
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700120static inline void atmel_put_base_addr(void __iomem *iobase)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800121{
122}
123
124/* Determine where to talk to device */
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700125static void __iomem * atmel_get_base_addr(unsigned long *base, int *region_size)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800126{
127 int lo, hi;
128
129 if (atmel_verify_tpm11() != 0)
Kylene Jo Hall90612b32005-11-18 01:10:58 -0800130 return NULL;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800131
132 lo = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_LO);
133 hi = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_HI);
134
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700135 *base = (hi << 8) | lo;
136 *region_size = 2;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800137
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700138 return ioport_map(*base, *region_size);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800139}
140#endif