1/*	$NetBSD: tpm_isa.c,v 1.8 2021/01/16 00:43:03 thorpej Exp $	*/
2
3/*
4 * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Maxime Villard.
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/*
33 * Copyright (c) 2008, 2009 Michael Shalayeff
34 * Copyright (c) 2009, 2010 Hans-Joerg Hoexer
35 * All rights reserved.
36 *
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
40 *
41 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
46 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
47 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 */
49
50#include <sys/cdefs.h>
51__KERNEL_RCSID(0, "$NetBSD: tpm_isa.c,v 1.8 2021/01/16 00:43:03 thorpej Exp $");
52
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/device.h>
56#include <sys/bus.h>
57#include <sys/pmf.h>
58
59#include <dev/ic/tpmreg.h>
60#include <dev/ic/tpmvar.h>
61
62#include <dev/isa/isareg.h>
63#include <dev/isa/isavar.h>
64
65#include "ioconf.h"
66
67static int	tpm_isa_match(device_t, cfdata_t, void *);
68static void	tpm_isa_attach(device_t, device_t, void *);
69
70CFATTACH_DECL_NEW(tpm_isa, sizeof(struct tpm_softc), tpm_isa_match,
71    tpm_isa_attach, NULL, NULL);
72
73static int
74tpm_isa_match(device_t parent, cfdata_t match, void *aux)
75{
76	struct isa_attach_args *ia = aux;
77	bus_space_tag_t bt = ia->ia_memt;
78	bus_space_handle_t bh;
79	int rv;
80
81	/* There can be only one. */
82	if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
83		return 0;
84
85	if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM)
86		return 0;
87
88	/* XXX: integer locator sign extension */
89	if (bus_space_map(bt, (unsigned int)ia->ia_iomem[0].ir_addr,
90	    TPM_SPACE_SIZE, 0, &bh))
91		return 0;
92
93	if ((rv = (*tpm_intf_tis12.probe)(bt, bh)) == 0) {
94		ia->ia_nio = 0;
95		ia->ia_io[0].ir_size = 0;
96		ia->ia_iomem[0].ir_size = TPM_SPACE_SIZE;
97	}
98	ia->ia_ndrq = 0;
99
100	bus_space_unmap(bt, bh, TPM_SPACE_SIZE);
101	return (rv == 0) ? 1 : 0;
102}
103
104static void
105tpm_isa_attach(device_t parent, device_t self, void *aux)
106{
107	struct tpm_softc *sc = device_private(self);
108	struct isa_attach_args *ia = aux;
109	bus_addr_t base;
110	bus_size_t size;
111	int rv;
112
113	base = (unsigned int)ia->ia_iomem[0].ir_addr;
114	size = TPM_SPACE_SIZE;
115
116	aprint_normal("\n");
117	aprint_naive("\n");
118
119	sc->sc_dev = self;
120	sc->sc_ver = TPM_1_2;
121	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
122	sc->sc_busy = false;
123	sc->sc_intf = &tpm_intf_tis12;
124	sc->sc_bt = ia->ia_memt;
125	if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) {
126		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
127		return;
128	}
129
130	if ((rv = (*sc->sc_intf->init)(sc)) != 0) {
131		aprint_error_dev(sc->sc_dev, "cannot init device, rv=%d\n", rv);
132		bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
133		return;
134	}
135
136	if (!pmf_device_register(self, tpm_suspend, tpm_resume))
137		aprint_error_dev(self, "couldn't establish power handler\n");
138}
139