xref: /netbsd-src/sys/dev/isa/tpm_isa.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: tpm_isa.c,v 1.3 2017/04/27 10:01:53 msaitoh Exp $	*/
2 
3 /*
4  * Copyright (c) 2008, 2009 Michael Shalayeff
5  * Copyright (c) 2009, 2010 Hans-J�rg H�xer
6  * All rights reserved.
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
17  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
18  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: tpm_isa.c,v 1.3 2017/04/27 10:01:53 msaitoh Exp $");
23 
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/kernel.h>
27 #include <sys/malloc.h>
28 #include <sys/proc.h>
29 #include <sys/device.h>
30 #include <sys/bus.h>
31 #include <sys/pmf.h>
32 
33 #include <dev/ic/tpmreg.h>
34 #include <dev/ic/tpmvar.h>
35 
36 #include <dev/isa/isareg.h>
37 #include <dev/isa/isavar.h>
38 
39 static int	tpm_isa_match(device_t, cfdata_t, void *);
40 static void	tpm_isa_attach(device_t, device_t, void *);
41 
42 CFATTACH_DECL_NEW(tpm_isa, sizeof(struct tpm_softc),
43     tpm_isa_match, tpm_isa_attach, NULL, NULL);
44 
45 extern struct cfdriver tpm_cd;
46 
47 static int
48 tpm_isa_match(device_t parent, cfdata_t match, void *aux)
49 {
50 	struct isa_attach_args *ia = aux;
51 	bus_space_tag_t bt = ia->ia_memt;
52 	bus_space_handle_t bh;
53 	int rv;
54 
55 	/* There can be only one. */
56 	if (tpm_cd.cd_devs && tpm_cd.cd_devs[0])
57 		return 0;
58 
59 	if (tpm_legacy_probe(ia->ia_iot, ia->ia_io[0].ir_addr)) {
60 		ia->ia_io[0].ir_size = 2;
61 		return 1;
62 	}
63 
64 	if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM)
65 		return 0;
66 
67 	/* XXX: integer locator sign extension */
68 	if (bus_space_map(bt, (unsigned int)ia->ia_iomem[0].ir_addr, TPM_SIZE,
69 	    0, &bh))
70 		return 0;
71 
72 	if ((rv = tpm_tis12_probe(bt, bh))) {
73 		ia->ia_nio = 0;
74 		ia->ia_io[0].ir_size = 0;
75 		ia->ia_iomem[0].ir_size = TPM_SIZE;
76 	}
77 	ia->ia_ndrq = 0;
78 
79 	bus_space_unmap(bt, bh, TPM_SIZE);
80 	return rv;
81 }
82 
83 static void
84 tpm_isa_attach(device_t parent, device_t self, void *aux)
85 {
86 	struct tpm_softc *sc = device_private(self);
87 	struct isa_attach_args *ia = aux;
88 	bus_addr_t iobase;
89 	bus_size_t size;
90 	int rv;
91 
92 	sc->sc_dev = self;
93 
94 	if (tpm_legacy_probe(ia->ia_iot, ia->ia_io[0].ir_addr)) {
95 		sc->sc_bt = ia->ia_iot;
96 		iobase = (unsigned int)ia->ia_io[0].ir_addr;
97 		size = ia->ia_io[0].ir_size;
98 		sc->sc_batm = ia->ia_iot;
99 		sc->sc_init = tpm_legacy_init;
100 		sc->sc_start = tpm_legacy_start;
101 		sc->sc_read = tpm_legacy_read;
102 		sc->sc_write = tpm_legacy_write;
103 		sc->sc_end = tpm_legacy_end;
104 	} else {
105 		sc->sc_bt = ia->ia_memt;
106 		iobase = (unsigned int)ia->ia_iomem[0].ir_addr;
107 		size = TPM_SIZE;
108 		sc->sc_init = tpm_tis12_init;
109 		sc->sc_start = tpm_tis12_start;
110 		sc->sc_read = tpm_tis12_read;
111 		sc->sc_write = tpm_tis12_write;
112 		sc->sc_end = tpm_tis12_end;
113 	}
114 
115 	if (bus_space_map(sc->sc_bt, iobase, size, 0, &sc->sc_bh)) {
116 		aprint_error_dev(sc->sc_dev, "cannot map registers\n");
117 		return;
118 	}
119 
120 	if ((rv = (*sc->sc_init)(sc, ia->ia_irq[0].ir_irq,
121 	    device_xname(sc->sc_dev))) != 0) {
122 		bus_space_unmap(sc->sc_bt, sc->sc_bh, size);
123 		return;
124 	}
125 
126 	/*
127 	 * Only setup interrupt handler when we have a vector and the
128 	 * chip is TIS 1.2 compliant.
129 	 */
130 	if (sc->sc_init == tpm_tis12_init &&
131 	    ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
132 	    (sc->sc_ih = isa_intr_establish_xname(ia->ia_ic,
133 	     ia->ia_irq[0].ir_irq, IST_EDGE, IPL_TTY, tpm_intr, sc,
134 	     device_xname(sc->sc_dev))) == NULL) {
135 		bus_space_unmap(sc->sc_bt, sc->sc_bh, TPM_SIZE);
136 		aprint_error_dev(sc->sc_dev, "cannot establish interrupt\n");
137 		return;
138 	}
139 
140 	if (!pmf_device_register(sc->sc_dev, tpm_suspend, tpm_resume))
141 		aprint_error_dev(sc->sc_dev, "Cannot set power mgmt handler\n");
142 }
143