xref: /netbsd-src/sys/dev/acpi/tpm_acpi.c (revision 1897181a7231d5fc7ab48994d1447fcbc4e13a49)
1 /* $NetBSD: tpm_acpi.c,v 1.1 2012/01/22 06:44:28 christos 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.1 2012/01/22 06:44:28 christos 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 #define _COMPONENT          ACPI_RESOURCE_COMPONENT
73 ACPI_MODULE_NAME            ("tpm_acpi")
74 
75 static int	tpm_acpi_match(device_t, cfdata_t, void *);
76 static void	tpm_acpi_attach(device_t, device_t, void *);
77 
78 
79 CFATTACH_DECL_NEW(tpm_acpi, sizeof(struct tpm_softc), tpm_acpi_match,
80     tpm_acpi_attach, NULL, NULL);
81 
82 /*
83  * Supported device IDs
84  */
85 
86 static const char * const tpm_acpi_ids[] = {
87 	"IFX0101",
88 	"IFX0102",
89 	NULL
90 };
91 
92 extern struct cfdriver tpm_cd;
93 
94 static int
95 tpm_acpi_match(device_t parent, cfdata_t match, void *aux)
96 {
97 	struct acpi_attach_args *aa = aux;
98 
99 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
100 		return 0;
101 
102 	/* There can be only one. */
103 	if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
104 		return 0;
105 
106 #ifdef notyet
107 	// XXX: My sony can't find the IRQ
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_mem *mem;
121 	struct acpi_irq *irq;
122 	int rv;
123 
124 	sc->sc_dev = self;
125 
126         /* Parse our resources */
127         rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", &res,
128             &acpi_resource_parse_ops_default);
129 
130         if (ACPI_FAILURE(rv)) {
131 		aprint_error_dev(sc->sc_dev, "cannot parse resources %d\n", rv);
132                 return;
133 	}
134 
135 	mem = acpi_res_mem(&res, 0);
136 	if (mem == NULL) {
137 		aprint_error_dev(sc->sc_dev, "cannot find mem\n");
138 		goto out;
139 	}
140 
141 	if (mem->ar_length != TPM_SIZE) {
142 		aprint_error_dev(sc->sc_dev, "wrong size mem %u != %u\n",
143 		    mem->ar_length, TPM_SIZE);
144 		goto out;
145 	}
146 
147 	sc->sc_bt = aa->aa_memt;
148 	sc->sc_init = tpm_tis12_init;
149 	sc->sc_start = tpm_tis12_start;
150 	sc->sc_read = tpm_tis12_read;
151 	sc->sc_write = tpm_tis12_write;
152 	sc->sc_end = tpm_tis12_end;
153 
154 	if (bus_space_map(sc->sc_bt, mem->ar_base, mem->ar_length, 0,
155 	    &sc->sc_bh)) {
156 		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
157 		goto out;
158 	}
159 
160 	irq = acpi_res_irq(&res, 0);
161 	if (irq == NULL) {
162 		aprint_error_dev(sc->sc_dev, "cannot find irq\n");
163 		goto out1;
164 	}
165 
166 	if ((rv = (*sc->sc_init)(sc, irq->ar_irq,
167 	    device_xname(sc->sc_dev))) != 0)
168 		aprint_error_dev(sc->sc_dev, "cannot init device %d\n", rv);
169 		goto out1;
170 
171 	if ((sc->sc_ih = isa_intr_establish(aa->aa_ic, irq->ar_irq,
172 	    IST_EDGE, IPL_TTY, tpm_intr, sc)) == NULL) {
173 		aprint_error_dev(sc->sc_dev, "cannot establish interrupt\n");
174 		goto out1;
175 	}
176 
177 	if (!pmf_device_register(sc->sc_dev, tpm_suspend, tpm_resume))
178 		aprint_error_dev(sc->sc_dev, "Cannot set power mgmt handler\n");
179 	return;
180 out1:
181 	bus_space_unmap(sc->sc_bt, sc->sc_bh, TPM_SIZE);
182 out:
183 	acpi_resource_cleanup(&res);
184 }
185