1/* $NetBSD: tpm_acpi.c,v 1.2 2012/01/22 20:24:27 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.2 2012/01/22 20:24:27 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
73ACPI_MODULE_NAME            ("tpm_acpi")
74
75static int	tpm_acpi_match(device_t, cfdata_t, void *);
76static void	tpm_acpi_attach(device_t, device_t, void *);
77
78
79CFATTACH_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
86static const char * const tpm_acpi_ids[] = {
87	"IFX0101",
88	"IFX0102",
89	NULL
90};
91
92extern struct cfdriver tpm_cd;
93
94static int
95tpm_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#ifdef notyet
106	return acpi_match_hid(aa->aa_node->ad_devinfo, tpm_acpi_ids);
107#else
108	return 0;
109#endif
110}
111
112static void
113tpm_acpi_attach(device_t parent, device_t self, void *aux)
114{
115	struct tpm_softc *sc = device_private(self);
116	struct acpi_attach_args *aa = aux;
117	struct acpi_resources res;
118	struct acpi_io *io;
119	struct acpi_mem *mem;
120	struct acpi_irq *irq;
121	bus_addr_t base;
122	bus_addr_t size;
123	int rv, inum;
124
125	sc->sc_dev = self;
126
127        /* Parse our resources */
128        rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", &res,
129            &acpi_resource_parse_ops_default);
130
131        if (ACPI_FAILURE(rv)) {
132		aprint_error_dev(sc->sc_dev, "cannot parse resources %d\n", rv);
133                return;
134	}
135
136	io = acpi_res_io(&res, 0);
137	if (io && tpm_legacy_probe(aa->aa_iot, io->ar_base)) {
138		sc->sc_bt = aa->aa_iot;
139		base = io->ar_base;
140		size = io->ar_length;
141		sc->sc_batm = aa->aa_iot;
142		sc->sc_init = tpm_legacy_init;
143		sc->sc_start = tpm_legacy_start;
144		sc->sc_read = tpm_legacy_read;
145		sc->sc_write = tpm_legacy_write;
146		sc->sc_end = tpm_legacy_end;
147		mem = NULL;
148	} else {
149		mem = acpi_res_mem(&res, 0);
150		if (mem == NULL) {
151			aprint_error_dev(sc->sc_dev, "cannot find mem\n");
152			goto out;
153		}
154
155		if (mem->ar_length != TPM_SIZE) {
156			aprint_error_dev(sc->sc_dev,
157			    "wrong size mem %u != %u\n",
158			    mem->ar_length, TPM_SIZE);
159			goto out;
160		}
161
162		base = mem->ar_base;
163		size = mem->ar_length;
164		sc->sc_bt = aa->aa_memt;
165		sc->sc_init = tpm_tis12_init;
166		sc->sc_start = tpm_tis12_start;
167		sc->sc_read = tpm_tis12_read;
168		sc->sc_write = tpm_tis12_write;
169		sc->sc_end = tpm_tis12_end;
170	}
171
172	if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) {
173		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
174		goto out;
175	}
176
177	if (mem && !tpm_tis12_probe(sc->sc_bt, sc->sc_bh)) {
178		aprint_error_dev(sc->sc_dev, "1.2 probe failed\n");
179		goto out1;
180	}
181
182	irq = acpi_res_irq(&res, 0);
183	if (irq == NULL)
184		inum = -1;
185	else
186		inum = irq->ar_irq;
187
188	if ((rv = (*sc->sc_init)(sc, inum, device_xname(sc->sc_dev))) != 0)
189		aprint_error_dev(sc->sc_dev, "cannot init device %d\n", rv);
190		goto out1;
191
192	if (inum != -1 &&
193	    (sc->sc_ih = isa_intr_establish(aa->aa_ic, irq->ar_irq,
194	    IST_EDGE, IPL_TTY, tpm_intr, sc)) == NULL) {
195		aprint_error_dev(sc->sc_dev, "cannot establish interrupt\n");
196		goto out1;
197	}
198
199	if (!pmf_device_register(sc->sc_dev, tpm_suspend, tpm_resume))
200		aprint_error_dev(sc->sc_dev, "Cannot set power mgmt handler\n");
201	return;
202out1:
203	bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
204out:
205	acpi_resource_cleanup(&res);
206}
207