1 /*        $NetBSD: lm_isa_common.c,v 1.7 2020/06/24 19:11:49 jdolecek Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Bill Squier.
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: lm_isa_common.c,v 1.7 2020/06/24 19:11:49 jdolecek Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/module.h>
40 #include <sys/conf.h>
41 #include <sys/kmem.h>
42 
43 #include <sys/bus.h>
44 
45 #include <dev/isa/isareg.h>
46 #include <dev/isa/isavar.h>
47 
48 #include <dev/ic/nslm7xvar.h>
49 
50 int       lm_isa_match(device_t, cfdata_t, void *);
51 void      lm_isa_attach(device_t, device_t, void *);
52 int       lm_isa_detach(device_t, int);
53 
54 static uint8_t      lm_isa_readreg(struct lm_softc *, int);
55 static void         lm_isa_writereg(struct lm_softc *, int, uint8_t);
56 
57 struct lm_isa_softc {
58           struct lm_softc lmsc;
59           bus_space_tag_t lm_iot;
60           bus_space_handle_t lm_ioh;
61 };
62 
63 int
lm_isa_match(device_t parent,cfdata_t match,void * aux)64 lm_isa_match(device_t parent, cfdata_t match, void *aux)
65 {
66           bus_space_handle_t ioh;
67           struct isa_attach_args *ia = aux;
68           struct lm_isa_softc *sc;
69           int rv;
70 
71           /* Must supply an address */
72           if (ia->ia_nio < 1)
73                     return 0;
74 
75           if (ISA_DIRECT_CONFIG(ia))
76                     return 0;
77 
78           if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
79                     return 0;
80 
81           if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 8, 0, &ioh))
82                     return 0;
83 
84           /* Bus independent probe */
85           sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
86           sc->lm_iot = ia->ia_iot;
87           sc->lm_ioh = ioh;
88           sc->lmsc.lm_writereg = lm_isa_writereg;
89           sc->lmsc.lm_readreg = lm_isa_readreg;
90           rv = lm_match(&sc->lmsc);
91           kmem_free(sc, sizeof(*sc));
92 
93           bus_space_unmap(ia->ia_iot, ioh, 8);
94 
95           if (rv) {
96                     ia->ia_nio = 1;
97                     ia->ia_io[0].ir_size = 8;
98 
99                     ia->ia_niomem = 0;
100                     ia->ia_nirq = 0;
101                     ia->ia_ndrq = 0;
102           }
103 
104           return rv;
105 }
106 
107 
108 void
lm_isa_attach(device_t parent,device_t self,void * aux)109 lm_isa_attach(device_t parent, device_t self, void *aux)
110 {
111           struct lm_isa_softc *sc = device_private(self);
112           struct isa_attach_args *ia = aux;
113 
114           sc->lm_iot = ia->ia_iot;
115 
116           if (bus_space_map(ia->ia_iot, ia->ia_io[0].ir_addr, 8, 0,
117               &sc->lm_ioh)) {
118                     aprint_error(": can't map i/o space\n");
119                     return;
120           }
121 
122           /* Bus-independent attachment */
123           sc->lmsc.sc_dev = self;
124           sc->lmsc.lm_writereg = lm_isa_writereg;
125           sc->lmsc.lm_readreg = lm_isa_readreg;
126           /* pass wbsio Device ID */
127           sc->lmsc.sioid = (uint16_t)(uintptr_t)ia->ia_aux;
128 
129           lm_attach(&sc->lmsc);
130 }
131 
132 int
lm_isa_detach(device_t self,int flags)133 lm_isa_detach(device_t self, int flags)
134 {
135           struct lm_isa_softc *sc = device_private(self);
136 
137           lm_detach(&sc->lmsc);
138           bus_space_unmap(sc->lm_iot, sc->lm_ioh, 8);
139           return 0;
140 }
141 
142 static uint8_t
lm_isa_readreg(struct lm_softc * lmsc,int reg)143 lm_isa_readreg(struct lm_softc *lmsc, int reg)
144 {
145           struct lm_isa_softc *sc = (struct lm_isa_softc *)lmsc;
146 
147           bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
148           return bus_space_read_1(sc->lm_iot, sc->lm_ioh, LMC_DATA);
149 }
150 
151 static void
lm_isa_writereg(struct lm_softc * lmsc,int reg,uint8_t val)152 lm_isa_writereg(struct lm_softc *lmsc, int reg, uint8_t val)
153 {
154           struct lm_isa_softc *sc = (struct lm_isa_softc *)lmsc;
155 
156           bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_ADDR, reg);
157           bus_space_write_1(sc->lm_iot, sc->lm_ioh, LMC_DATA, val);
158 }
159 
160 MODULE(MODULE_CLASS_DRIVER, lm_isa_common, "lm");
161 
162 static int
lm_isa_common_modcmd(modcmd_t cmd,void * priv)163 lm_isa_common_modcmd(modcmd_t cmd, void *priv)
164 {
165           if ((cmd == MODULE_CMD_INIT) || (cmd == MODULE_CMD_FINI))
166                     return 0;
167           return ENOTTY;
168 }
169