1 /*        $NetBSD: tsdio.c,v 1.13 2021/08/07 16:19:12 thorpej Exp $   */
2 
3 /*-
4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jesse Off
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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: tsdio.c,v 1.13 2021/08/07 16:19:12 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 
39 #include <sys/bus.h>
40 #include <sys/intr.h>
41 
42 #include <dev/isa/isareg.h>
43 #include <dev/isa/isavar.h>
44 #include <dev/isa/isadmavar.h>
45 #include <dev/isa/tsdiovar.h>
46 
47 int       tsdio_probe(device_t, cfdata_t, void *);
48 void      tsdio_attach(device_t, device_t, void *);
49 int       tsdio_search(device_t, cfdata_t, const int *, void *);
50 int       tsdio_print(void *, const char *);
51 
52 CFATTACH_DECL_NEW(tsdio, sizeof(struct tsdio_softc),
53     tsdio_probe, tsdio_attach, NULL, NULL);
54 
55 int
tsdio_probe(device_t parent,cfdata_t cf,void * aux)56 tsdio_probe(device_t parent, cfdata_t cf, void *aux)
57 {
58           struct isa_attach_args *ia = aux;
59           bus_space_tag_t iot = ia->ia_iot;
60           bus_space_handle_t dioh;
61           int rv = 0, have_io = 0;
62 
63           if (ia->ia_nio < 1)
64                     return (0);
65           if (ia->ia_nirq < 1)
66                     return (0);
67 
68           if (ISA_DIRECT_CONFIG(ia))
69                     return (0);
70 
71           /*
72            * Disallow wildcarded I/O base.
73            */
74           if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
75                     return (0);
76 
77           /*
78            * Map the I/O space.
79            */
80           if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 8,
81               0, &dioh))
82                     goto out;
83           have_io = 1;
84 
85           if (bus_space_read_1(ia->ia_iot, dioh, 0) != 0x54) {
86                     goto out;
87           }
88 
89           rv = 1;
90           ia->ia_nio = 1;
91           ia->ia_io[0].ir_size = 8;
92           ia->ia_niomem = 0;
93           ia->ia_nirq = 0;
94           ia->ia_ndrq = 0;
95 
96  out:
97           if (have_io)
98                     bus_space_unmap(iot, dioh, 8);
99 
100           return (rv);
101 }
102 
103 void
tsdio_attach(device_t parent,device_t self,void * aux)104 tsdio_attach(device_t parent, device_t self, void *aux)
105 {
106           struct tsdio_softc *sc = device_private(self);
107           struct isa_attach_args *ia = aux;
108 
109           sc->sc_iot = ia->ia_iot;
110 
111           aprint_normal(": Technologic Systems TS-DIO24\n");
112 
113           /*
114            * Map the device.
115            */
116           if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr, 8,
117               0, &sc->sc_ioh)) {
118                     aprint_error_dev(self, "unable to map i/o space\n");
119                     return;
120           }
121 
122           /*
123            *  Attach sub-devices
124            */
125           config_search(self, NULL,
126               CFARGS(.search = tsdio_search));
127 }
128 
129 int
tsdio_search(device_t parent,cfdata_t cf,const int * l,void * aux)130 tsdio_search(device_t parent, cfdata_t cf, const int *l, void *aux)
131 {
132           struct tsdio_softc *sc = device_private(parent);
133           struct tsdio_attach_args sa;
134 
135           sa.ta_iot = sc->sc_iot;
136           sa.ta_ioh = sc->sc_ioh;
137 
138           if (config_probe(parent, cf, &sa))
139                     config_attach(parent, cf, &sa, tsdio_print, CFARGS_NONE);
140 
141           return (0);
142 }
143 
144 int
tsdio_print(void * aux,const char * name)145 tsdio_print(void *aux, const char *name)
146 {
147 
148           return (UNCONF);
149 }
150