blob: 5c82eb47665e0783ca07a444ec7b2ba659127da4 [file] [log] [blame]
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -08001/*
2 * Copyright (C) 2005 IBM Corporation
3 *
4 * Authors:
5 * Kylene Hall <kjhall@us.ibm.com>
6 *
Kent Yoder8e81cc12007-08-22 14:01:04 -07007 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -08008 *
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
Jarkko Sakkinen23d06ff2016-03-23 07:10:22 +020025struct tpm_atmel_priv {
26 int region_size;
27 int have_region;
Jarkko Sakkinenee177982016-03-23 08:16:09 +020028 unsigned long base;
Christophe Ricard4eea7032016-03-31 22:56:55 +020029 void __iomem *iobase;
Jarkko Sakkinen23d06ff2016-03-23 07:10:22 +020030};
31
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080032#ifdef CONFIG_PPC64
Stephen Rothwellddf526e2007-05-12 10:36:56 -070033
34#include <asm/prom.h>
35
Christophe Ricard4eea7032016-03-31 22:56:55 +020036#define atmel_getb(priv, offset) readb(priv->iobase + offset)
37#define atmel_putb(val, priv, offset) writeb(val, priv->iobase + offset)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080038#define atmel_request_region request_mem_region
39#define atmel_release_region release_mem_region
Kylene Jo Hall90612b32005-11-18 01:10:58 -080040
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -070041static inline void atmel_put_base_addr(void __iomem *iobase)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080042{
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -070043 iounmap(iobase);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080044}
45
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -070046static void __iomem * atmel_get_base_addr(unsigned long *base, int *region_size)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080047{
48 struct device_node *dn;
49 unsigned long address, size;
Jeremy Kerr5c339e92006-07-12 15:41:30 +100050 const unsigned int *reg;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080051 int reglen;
52 int naddrc;
53 int nsizec;
54
55 dn = of_find_node_by_name(NULL, "tpm");
56
57 if (!dn)
Kylene Jo Hall90612b32005-11-18 01:10:58 -080058 return NULL;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080059
Stephen Rothwell55b61fe2007-05-03 17:26:52 +100060 if (!of_device_is_compatible(dn, "AT97SC3201")) {
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080061 of_node_put(dn);
Kylene Jo Hall90612b32005-11-18 01:10:58 -080062 return NULL;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080063 }
64
Stephen Rothwell40cd3a42007-05-01 13:54:02 +100065 reg = of_get_property(dn, "reg", &reglen);
Stephen Rothwella8bda5d2007-04-03 10:56:50 +100066 naddrc = of_n_addr_cells(dn);
Stephen Rothwell9213fee2007-04-03 10:57:48 +100067 nsizec = of_n_size_cells(dn);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080068
69 of_node_put(dn);
70
71
72 if (naddrc == 2)
73 address = ((unsigned long) reg[0] << 32) | reg[1];
74 else
75 address = reg[0];
76
77 if (nsizec == 2)
78 size =
79 ((unsigned long) reg[naddrc] << 32) | reg[naddrc + 1];
80 else
81 size = reg[naddrc];
82
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -070083 *base = address;
84 *region_size = size;
85 return ioremap(*base, *region_size);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080086}
87#else
Jarkko Sakkinenee177982016-03-23 08:16:09 +020088#define atmel_getb(chip, offset) inb(atmel_get_priv(chip)->base + offset)
89#define atmel_putb(val, chip, offset) \
90 outb(val, atmel_get_priv(chip)->base + offset)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -080091#define atmel_request_region request_region
92#define atmel_release_region release_region
93/* Atmel definitions */
94enum tpm_atmel_addr {
95 TPM_ATMEL_BASE_ADDR_LO = 0x08,
96 TPM_ATMEL_BASE_ADDR_HI = 0x09
97};
98
Jarkko Sakkinen1d191552017-01-25 16:47:39 +020099static inline int tpm_read_index(int base, int index)
100{
101 outb(index, base);
102 return inb(base+1) & 0xFF;
103}
104
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800105/* Verify this is a 1.1 Atmel TPM */
106static int atmel_verify_tpm11(void)
107{
108
109 /* verify that it is an Atmel part */
110 if (tpm_read_index(TPM_ADDR, 4) != 'A' ||
111 tpm_read_index(TPM_ADDR, 5) != 'T' ||
112 tpm_read_index(TPM_ADDR, 6) != 'M' ||
113 tpm_read_index(TPM_ADDR, 7) != 'L')
114 return 1;
115
116 /* query chip for its version number */
117 if (tpm_read_index(TPM_ADDR, 0x00) != 1 ||
118 tpm_read_index(TPM_ADDR, 0x01) != 1)
119 return 1;
120
121 /* This is an atmel supported part */
122 return 0;
123}
124
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700125static inline void atmel_put_base_addr(void __iomem *iobase)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800126{
127}
128
129/* Determine where to talk to device */
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700130static void __iomem * atmel_get_base_addr(unsigned long *base, int *region_size)
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800131{
132 int lo, hi;
133
134 if (atmel_verify_tpm11() != 0)
Kylene Jo Hall90612b32005-11-18 01:10:58 -0800135 return NULL;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800136
137 lo = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_LO);
138 hi = tpm_read_index(TPM_ADDR, TPM_ATMEL_BASE_ADDR_HI);
139
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700140 *base = (hi << 8) | lo;
141 *region_size = 2;
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800142
Kylene Jo Halle0dd03c2006-04-22 02:37:26 -0700143 return ioport_map(*base, *region_size);
Kylene Jo Hallad5ea3c2005-11-13 16:07:41 -0800144}
145#endif