xref: /netbsd-src/sys/dev/acpi/tpm_acpi.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /* $NetBSD: tpm_acpi.c,v 1.7 2018/12/09 11:12:58 jdolecek Exp $ */
2 
3 /*-
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*
32  * Copyright (c) 2008, 2009 Michael Shalayeff
33  * Copyright (c) 2009, 2010 Hans-J�rg H�xer
34  * All rights reserved.
35  *
36  * Permission to use, copy, modify, and distribute this software for any
37  * purpose with or without fee is hereby granted, provided that the above
38  * copyright notice and this permission notice appear in all copies.
39  *
40  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
41  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
43  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
45  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
46  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47  */
48 
49 /*
50  * ACPI attachment for the Infineon SLD 9630 TT 1.1 and SLB 9635 TT 1.2
51  * trusted platform module. See www.trustedcomputinggroup.org
52  */
53 
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: tpm_acpi.c,v 1.7 2018/12/09 11:12:58 jdolecek Exp $");
56 
57 #include <sys/param.h>
58 #include <sys/device.h>
59 #include <sys/systm.h>
60 #include <sys/device.h>
61 #include <sys/bus.h>
62 #include <sys/pmf.h>
63 
64 #include <dev/ic/tpmreg.h>
65 #include <dev/ic/tpmvar.h>
66 
67 #include <dev/acpi/acpireg.h>
68 #include <dev/acpi/acpivar.h>
69 
70 #include <dev/isa/isavar.h>
71 
72 #include "ioconf.h"
73 
74 #define _COMPONENT          ACPI_RESOURCE_COMPONENT
75 ACPI_MODULE_NAME            ("tpm_acpi")
76 
77 static int	tpm_acpi_match(device_t, cfdata_t, void *);
78 static void	tpm_acpi_attach(device_t, device_t, void *);
79 
80 
81 CFATTACH_DECL_NEW(tpm_acpi, sizeof(struct tpm_softc), tpm_acpi_match,
82     tpm_acpi_attach, NULL, NULL);
83 
84 /*
85  * Supported device IDs
86  */
87 
88 #ifdef notyet
89 static const char * const tpm_acpi_ids[] = {
90 	"IFX0101",
91 	"IFX0102",
92 	NULL
93 };
94 #endif
95 
96 static int
97 tpm_acpi_match(device_t parent, cfdata_t match, void *aux)
98 {
99 	struct acpi_attach_args *aa = aux;
100 
101 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
102 		return 0;
103 
104 	/* There can be only one. */
105 	if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
106 		return 0;
107 #ifdef notyet
108 	return acpi_match_hid(aa->aa_node->ad_devinfo, tpm_acpi_ids);
109 #else
110 	return 0;
111 #endif
112 }
113 
114 static void
115 tpm_acpi_attach(device_t parent, device_t self, void *aux)
116 {
117 	struct tpm_softc *sc = device_private(self);
118 	struct acpi_attach_args *aa = aux;
119 	struct acpi_resources res;
120 	struct acpi_io *io;
121 	struct acpi_mem *mem;
122 	struct acpi_irq *irq;
123 	bus_addr_t base;
124 	bus_addr_t size;
125 	int rv, inum;
126 
127 	sc->sc_dev = self;
128 
129         /* Parse our resources */
130         rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", &res,
131             &acpi_resource_parse_ops_default);
132 
133         if (ACPI_FAILURE(rv)) {
134 		aprint_error_dev(sc->sc_dev, "cannot parse resources %d\n", rv);
135                 return;
136 	}
137 
138 	io = acpi_res_io(&res, 0);
139 	if (io && tpm_legacy_probe(aa->aa_iot, io->ar_base)) {
140 		sc->sc_bt = aa->aa_iot;
141 		base = io->ar_base;
142 		size = io->ar_length;
143 		sc->sc_batm = aa->aa_iot;
144 		sc->sc_init = tpm_legacy_init;
145 		sc->sc_start = tpm_legacy_start;
146 		sc->sc_read = tpm_legacy_read;
147 		sc->sc_write = tpm_legacy_write;
148 		sc->sc_end = tpm_legacy_end;
149 		mem = NULL;
150 	} else {
151 		mem = acpi_res_mem(&res, 0);
152 		if (mem == NULL) {
153 			aprint_error_dev(sc->sc_dev, "cannot find mem\n");
154 			goto out;
155 		}
156 
157 		if (mem->ar_length != TPM_SIZE) {
158 			aprint_error_dev(sc->sc_dev,
159 			    "wrong size mem %"PRIu64" != %u\n",
160 			    (uint64_t)mem->ar_length, TPM_SIZE);
161 			goto out;
162 		}
163 
164 		base = mem->ar_base;
165 		size = mem->ar_length;
166 		sc->sc_bt = aa->aa_memt;
167 		sc->sc_init = tpm_tis12_init;
168 		sc->sc_start = tpm_tis12_start;
169 		sc->sc_read = tpm_tis12_read;
170 		sc->sc_write = tpm_tis12_write;
171 		sc->sc_end = tpm_tis12_end;
172 	}
173 
174 	if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) {
175 		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
176 		goto out;
177 	}
178 
179 	if (mem && !tpm_tis12_probe(sc->sc_bt, sc->sc_bh)) {
180 		aprint_error_dev(sc->sc_dev, "1.2 probe failed\n");
181 		goto out1;
182 	}
183 
184 	irq = acpi_res_irq(&res, 0);
185 	if (irq == NULL)
186 		inum = -1;
187 	else
188 		inum = irq->ar_irq;
189 
190 	if ((rv = (*sc->sc_init)(sc, inum, device_xname(sc->sc_dev))) != 0) {
191 		aprint_error_dev(sc->sc_dev, "cannot init device %d\n", rv);
192 		goto out1;
193 	}
194 
195 	if (inum != -1 &&
196 	    (sc->sc_ih = isa_intr_establish(aa->aa_ic, irq->ar_irq,
197 	    IST_EDGE, IPL_TTY, tpm_intr, sc)) == NULL) {
198 		aprint_error_dev(sc->sc_dev, "cannot establish interrupt\n");
199 		goto out1;
200 	}
201 
202 	if (!pmf_device_register(sc->sc_dev, tpm_suspend, tpm_resume))
203 		aprint_error_dev(sc->sc_dev, "Cannot set power mgmt handler\n");
204 	return;
205 out1:
206 	bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
207 out:
208 	acpi_resource_cleanup(&res);
209 }
210