121308Sache/*	$NetBSD: tpm_isa.c,v 1.8 2021/01/16 00:43:03 thorpej Exp $	*/
221308Sache
321308Sache/*
421308Sache * Copyright (c) 2019 The NetBSD Foundation, Inc.
521308Sache * All rights reserved.
621308Sache *
721308Sache * This code is derived from software contributed to The NetBSD Foundation
821308Sache * by Maxime Villard.
921308Sache *
1021308Sache * Redistribution and use in source and binary forms, with or without
1121308Sache * modification, are permitted provided that the following conditions
1221308Sache * are met:
1321308Sache * 1. Redistributions of source code must retain the above copyright
1421308Sache *    notice, this list of conditions and the following disclaimer.
1521308Sache * 2. Redistributions in binary form must reproduce the above copyright
1621308Sache *    notice, this list of conditions and the following disclaimer in the
1721308Sache *    documentation and/or other materials provided with the distribution.
1821308Sache *
1921308Sache * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2021308Sache * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2121308Sache * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2221308Sache * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2321308Sache * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2421308Sache * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2521308Sache * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2621308Sache * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2721308Sache * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2821308Sache * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2921308Sache * POSSIBILITY OF SUCH DAMAGE.
3021308Sache */
3121308Sache
3221308Sache/*
3321308Sache * Copyright (c) 2008, 2009 Michael Shalayeff
3421308Sache * Copyright (c) 2009, 2010 Hans-Joerg Hoexer
3521308Sache * All rights reserved.
3621308Sache *
3721308Sache * Permission to use, copy, modify, and distribute this software for any
3821308Sache * purpose with or without fee is hereby granted, provided that the above
3921308Sache * copyright notice and this permission notice appear in all copies.
4021308Sache *
4121308Sache * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
4221308Sache * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
4321308Sache * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
4421308Sache * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
4521308Sache * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
4621308Sache * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
4721308Sache * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4821308Sache */
4921308Sache
5021308Sache#include <sys/cdefs.h>
5121308Sache__KERNEL_RCSID(0, "$NetBSD: tpm_isa.c,v 1.8 2021/01/16 00:43:03 thorpej Exp $");
5221308Sache
5321308Sache#include <sys/param.h>
5421308Sache#include <sys/systm.h>
5521308Sache#include <sys/device.h>
5621308Sache#include <sys/bus.h>
5721308Sache#include <sys/pmf.h>
5821308Sache
5921308Sache#include <dev/ic/tpmreg.h>
6021308Sache#include <dev/ic/tpmvar.h>
6121308Sache
6221308Sache#include <dev/isa/isareg.h>
6321308Sache#include <dev/isa/isavar.h>
6421308Sache
6521308Sache#include "ioconf.h"
6621308Sache
6721308Sachestatic int	tpm_isa_match(device_t, cfdata_t, void *);
6821308Sachestatic void	tpm_isa_attach(device_t, device_t, void *);
6921308Sache
7021308SacheCFATTACH_DECL_NEW(tpm_isa, sizeof(struct tpm_softc), tpm_isa_match,
7121308Sache    tpm_isa_attach, NULL, NULL);
7221308Sache
7321308Sachestatic int
7421308Sachetpm_isa_match(device_t parent, cfdata_t match, void *aux)
7521308Sache{
7621308Sache	struct isa_attach_args *ia = aux;
7721308Sache	bus_space_tag_t bt = ia->ia_memt;
7821308Sache	bus_space_handle_t bh;
7921308Sache	int rv;
8021308Sache
8121308Sache	/* There can be only one. */
8221308Sache	if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
8321308Sache		return 0;
8421308Sache
8521308Sache	if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM)
8621308Sache		return 0;
8721308Sache
8821308Sache	/* XXX: integer locator sign extension */
8921308Sache	if (bus_space_map(bt, (unsigned int)ia->ia_iomem[0].ir_addr,
9026497Sache	    TPM_SPACE_SIZE, 0, &bh))
9126497Sache		return 0;
9226497Sache
9326497Sache	if ((rv = (*tpm_intf_tis12.probe)(bt, bh)) == 0) {
9426497Sache		ia->ia_nio = 0;
9526497Sache		ia->ia_io[0].ir_size = 0;
9626497Sache		ia->ia_iomem[0].ir_size = TPM_SPACE_SIZE;
9726497Sache	}
9821308Sache	ia->ia_ndrq = 0;
9921308Sache
10021308Sache	bus_space_unmap(bt, bh, TPM_SPACE_SIZE);
10121308Sache	return (rv == 0) ? 1 : 0;
10221308Sache}
10321308Sache
10421308Sachestatic void
10521308Sachetpm_isa_attach(device_t parent, device_t self, void *aux)
10621308Sache{
10721308Sache	struct tpm_softc *sc = device_private(self);
10826497Sache	struct isa_attach_args *ia = aux;
10926497Sache	bus_addr_t base;
11026497Sache	bus_size_t size;
11121308Sache	int rv;
11226497Sache
11326497Sache	base = (unsigned int)ia->ia_iomem[0].ir_addr;
11426497Sache	size = TPM_SPACE_SIZE;
11526497Sache
11621308Sache	aprint_normal("\n");
11721308Sache	aprint_naive("\n");
11821308Sache
11921308Sache	sc->sc_dev = self;
12021308Sache	sc->sc_ver = TPM_1_2;
12121308Sache	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
12221308Sache	sc->sc_busy = false;
12321308Sache	sc->sc_intf = &tpm_intf_tis12;
12421308Sache	sc->sc_bt = ia->ia_memt;
12521308Sache	if (bus_space_map(sc->sc_bt, base, size, 0, &sc->sc_bh)) {
12621308Sache		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
12721308Sache		return;
12821308Sache	}
12921308Sache
13021308Sache	if ((rv = (*sc->sc_intf->init)(sc)) != 0) {
13126497Sache		aprint_error_dev(sc->sc_dev, "cannot init device, rv=%d\n", rv);
13226497Sache		bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
13326497Sache		return;
13421308Sache	}
13521308Sache
13621308Sache	if (!pmf_device_register(self, tpm_suspend, tpm_resume))
13721308Sache		aprint_error_dev(self, "couldn't establish power handler\n");
13821308Sache}
13921308Sache