1 1.8 thorpej /* $NetBSD: tpm_isa.c,v 1.8 2021/01/16 00:43:03 thorpej Exp $ */ 2 1.5 maxv 3 1.5 maxv /* 4 1.5 maxv * Copyright (c) 2019 The NetBSD Foundation, Inc. 5 1.5 maxv * All rights reserved. 6 1.5 maxv * 7 1.5 maxv * This code is derived from software contributed to The NetBSD Foundation 8 1.5 maxv * by Maxime Villard. 9 1.5 maxv * 10 1.5 maxv * Redistribution and use in source and binary forms, with or without 11 1.5 maxv * modification, are permitted provided that the following conditions 12 1.5 maxv * are met: 13 1.5 maxv * 1. Redistributions of source code must retain the above copyright 14 1.5 maxv * notice, this list of conditions and the following disclaimer. 15 1.5 maxv * 2. Redistributions in binary form must reproduce the above copyright 16 1.5 maxv * notice, this list of conditions and the following disclaimer in the 17 1.5 maxv * documentation and/or other materials provided with the distribution. 18 1.5 maxv * 19 1.5 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.5 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.5 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.5 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.5 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.5 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.5 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.5 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.5 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.5 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.5 maxv * POSSIBILITY OF SUCH DAMAGE. 30 1.5 maxv */ 31 1.1 christos 32 1.1 christos /* 33 1.1 christos * Copyright (c) 2008, 2009 Michael Shalayeff 34 1.5 maxv * Copyright (c) 2009, 2010 Hans-Joerg Hoexer 35 1.1 christos * All rights reserved. 36 1.1 christos * 37 1.1 christos * Permission to use, copy, modify, and distribute this software for any 38 1.1 christos * purpose with or without fee is hereby granted, provided that the above 39 1.1 christos * copyright notice and this permission notice appear in all copies. 40 1.1 christos * 41 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 42 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 43 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 44 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 45 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN 46 1.1 christos * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 47 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 48 1.1 christos */ 49 1.1 christos 50 1.1 christos #include <sys/cdefs.h> 51 1.8 thorpej __KERNEL_RCSID(0, "$NetBSD: tpm_isa.c,v 1.8 2021/01/16 00:43:03 thorpej Exp $"); 52 1.1 christos 53 1.1 christos #include <sys/param.h> 54 1.1 christos #include <sys/systm.h> 55 1.1 christos #include <sys/device.h> 56 1.1 christos #include <sys/bus.h> 57 1.1 christos #include <sys/pmf.h> 58 1.1 christos 59 1.1 christos #include <dev/ic/tpmreg.h> 60 1.1 christos #include <dev/ic/tpmvar.h> 61 1.1 christos 62 1.1 christos #include <dev/isa/isareg.h> 63 1.1 christos #include <dev/isa/isavar.h> 64 1.1 christos 65 1.5 maxv #include "ioconf.h" 66 1.5 maxv 67 1.1 christos static int tpm_isa_match(device_t, cfdata_t, void *); 68 1.1 christos static void tpm_isa_attach(device_t, device_t, void *); 69 1.1 christos 70 1.5 maxv CFATTACH_DECL_NEW(tpm_isa, sizeof(struct tpm_softc), tpm_isa_match, 71 1.5 maxv tpm_isa_attach, NULL, NULL); 72 1.1 christos 73 1.1 christos static int 74 1.1 christos tpm_isa_match(device_t parent, cfdata_t match, void *aux) 75 1.1 christos { 76 1.1 christos struct isa_attach_args *ia = aux; 77 1.1 christos bus_space_tag_t bt = ia->ia_memt; 78 1.1 christos bus_space_handle_t bh; 79 1.1 christos int rv; 80 1.1 christos 81 1.1 christos /* There can be only one. */ 82 1.1 christos if (tpm_cd.cd_devs && tpm_cd.cd_devs[0]) 83 1.1 christos return 0; 84 1.1 christos 85 1.1 christos if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM) 86 1.1 christos return 0; 87 1.1 christos 88 1.1 christos /* XXX: integer locator sign extension */ 89 1.5 maxv if (bus_space_map(bt, (unsigned int)ia->ia_iomem[0].ir_addr, 90 1.5 maxv TPM_SPACE_SIZE, 0, &bh)) 91 1.1 christos return 0; 92 1.1 christos 93 1.7 maxv if ((rv = (*tpm_intf_tis12.probe)(bt, bh)) == 0) { 94 1.1 christos ia->ia_nio = 0; 95 1.1 christos ia->ia_io[0].ir_size = 0; 96 1.4 maxv ia->ia_iomem[0].ir_size = TPM_SPACE_SIZE; 97 1.1 christos } 98 1.1 christos ia->ia_ndrq = 0; 99 1.1 christos 100 1.4 maxv bus_space_unmap(bt, bh, TPM_SPACE_SIZE); 101 1.7 maxv return (rv == 0) ? 1 : 0; 102 1.1 christos } 103 1.1 christos 104 1.1 christos static void 105 1.1 christos tpm_isa_attach(device_t parent, device_t self, void *aux) 106 1.1 christos { 107 1.1 christos struct tpm_softc *sc = device_private(self); 108 1.1 christos struct isa_attach_args *ia = aux; 109 1.5 maxv bus_addr_t base; 110 1.1 christos bus_size_t size; 111 1.7 maxv int rv; 112 1.7 maxv 113 1.7 maxv base = (unsigned int)ia->ia_iomem[0].ir_addr; 114 1.7 maxv size = TPM_SPACE_SIZE; 115 1.1 christos 116 1.8 thorpej aprint_normal("\n"); 117 1.8 thorpej aprint_naive("\n"); 118 1.8 thorpej 119 1.1 christos sc->sc_dev = self; 120 1.4 maxv sc->sc_ver = TPM_1_2; 121 1.5 maxv mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 122 1.5 maxv sc->sc_busy = false; 123 1.7 maxv sc->sc_intf = &tpm_intf_tis12; 124 1.5 maxv sc->sc_bt = ia->ia_memt; 125 1.5 maxv if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) { 126 1.1 christos aprint_error_dev(sc->sc_dev, "cannot map registers\n"); 127 1.1 christos return; 128 1.1 christos } 129 1.1 christos 130 1.7 maxv if ((rv = (*sc->sc_intf->init)(sc)) != 0) { 131 1.7 maxv aprint_error_dev(sc->sc_dev, "cannot init device, rv=%d\n", rv); 132 1.1 christos bus_space_unmap(sc->sc_bt, sc->sc_bh, size); 133 1.1 christos return; 134 1.2 christos } 135 1.1 christos 136 1.6 maxv if (!pmf_device_register(self, tpm_suspend, tpm_resume)) 137 1.6 maxv aprint_error_dev(self, "couldn't establish power handler\n"); 138 1.1 christos } 139