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