2 * Copyright (c) 2005, 2006 Mark Kettenis
3 * Copyright (c) 2007 Constantine A. Murenin, Google Summer of Code
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $OpenBSD: lm78_isa.c,v 1.2 2007/07/01 21:48:57 cnst Exp $
18 * $DragonFly: src/sys/dev/powermng/lm/lm78_isa.c,v 1.1 2007/10/02 13:37:38 hasso Exp $
21 #include <sys/cdefs.h>
22 #include <sys/param.h>
23 #include <sys/kernel.h>
25 #include <sys/module.h>
27 #include <sys/module.h>
31 #include <bus/isa/isavar.h>
33 #include <sys/systm.h>
35 #include <sys/sensors.h>
43 extern struct cfdriver lm_cd;
46 #define DPRINTF(x) do { printf x; } while (0)
52 struct lm_softc sc_lmsc;
54 struct resource *sc_iores;
56 bus_space_tag_t sc_iot;
57 bus_space_handle_t sc_ioh;
60 static int lm_isa_probe(struct device *);
61 static int lm_isa_attach(struct device *);
62 static int lm_isa_detach(struct device *);
63 u_int8_t lm_isa_readreg(struct lm_softc *, int);
64 void lm_isa_writereg(struct lm_softc *, int, int);
66 static device_method_t lm_isa_methods[] = {
67 /* Methods from the device interface */
68 DEVMETHOD(device_probe, lm_isa_probe),
69 DEVMETHOD(device_attach, lm_isa_attach),
70 DEVMETHOD(device_detach, lm_isa_detach),
72 /* Terminate method list */
76 static driver_t lm_isa_driver = {
79 sizeof (struct lm_isa_softc)
82 static devclass_t lm_devclass;
84 DRIVER_MODULE(lm, isa, lm_isa_driver, lm_devclass, NULL, NULL);
87 lm_isa_probe(struct device *dev)
89 struct lm_isa_softc *sc = device_get_softc(dev);
90 struct resource *iores;
93 bus_space_handle_t ioh;
94 int banksel, vendid, chipid, addr;
96 iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &iorid,
97 0ul, ~0ul, 8, RF_ACTIVE);
99 DPRINTF(("%s: can't map i/o space\n", __func__));
102 iot = rman_get_bustag(iores);
103 ioh = rman_get_bushandle(iores);
105 /* Probe for Winbond chips. */
106 bus_space_write_1(iot, ioh, LMC_ADDR, WB_BANKSEL);
107 banksel = bus_space_read_1(iot, ioh, LMC_DATA);
108 bus_space_write_1(iot, ioh, LMC_ADDR, WB_VENDID);
109 vendid = bus_space_read_1(iot, ioh, LMC_DATA);
110 if (((banksel & 0x80) && vendid == (WB_VENDID_WINBOND >> 8)) ||
111 (!(banksel & 0x80) && vendid == (WB_VENDID_WINBOND & 0xff)))
114 /* Probe for ITE chips (and don't attach if we find one). */
115 bus_space_write_1(iot, ioh, LMC_ADDR, 0x58 /*ITD_CHIPID*/);
116 vendid = bus_space_read_1(iot, ioh, LMC_DATA);
117 if (vendid == 0x90 /*IT_ID_IT87*/)
121 * Probe for National Semiconductor LM78/79/81.
123 * XXX This assumes the address has not been changed from the
124 * power up default. This is probably a reasonable
125 * assumption, and if it isn't true, we should be able to
126 * access the chip using the serial bus.
128 bus_space_write_1(iot, ioh, LMC_ADDR, LM_SBUSADDR);
129 addr = bus_space_read_1(iot, ioh, LMC_DATA);
130 if ((addr & 0xfc) == 0x2c) {
131 bus_space_write_1(iot, ioh, LMC_ADDR, LM_CHIPID);
132 chipid = bus_space_read_1(iot, ioh, LMC_DATA);
134 switch (chipid & LM_CHIPID_MASK) {
136 case LM_CHIPID_LM78J:
144 bus_release_resource(dev, SYS_RES_IOPORT, iorid, iores);
149 /* Bus-independent probe */
150 sc->sc_lmsc.sc_dev = dev;
153 sc->sc_lmsc.lm_writereg = lm_isa_writereg;
154 sc->sc_lmsc.lm_readreg = lm_isa_readreg;
155 lm_probe(&sc->sc_lmsc);
157 bus_release_resource(dev, SYS_RES_IOPORT, iorid, iores);
165 lm_isa_attach(struct device *dev)
167 struct lm_isa_softc *sc = device_get_softc(dev);
169 struct lm_softc *lmsc;
174 sc->sc_iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->sc_iorid,
175 0ul, ~0ul, 8, RF_ACTIVE);
176 if (sc->sc_iores == NULL) {
177 device_printf(dev, "can't map i/o space\n");
180 sc->sc_iot = rman_get_bustag(sc->sc_iores);
181 sc->sc_ioh = rman_get_bushandle(sc->sc_iores);
183 /* Bus-independent attachment */
184 lm_attach(&sc->sc_lmsc);
188 * Most devices supported by this driver can attach to iic(4)
189 * as well. However, we prefer to attach them to isa(4) since
190 * that causes less overhead and is more reliable. We look
191 * through all previously attached devices, and if we find an
192 * identical chip at the same serial bus address, we stop
193 * updating its sensors and mark them as invalid.
196 sbusaddr = lm_isa_readreg(&sc->sc_lmsc, LM_SBUSADDR);
200 for (i = 0; i < lm_cd.cd_ndevs; i++) {
201 lmsc = lm_cd.cd_devs[i];
202 if (lmsc == &sc->sc_lmsc)
204 if (lmsc && lmsc->sbusaddr == sbusaddr &&
205 lmsc->chipid == sc->sc_lmsc.chipid)
206 config_detach(&lmsc->sc_dev, 0);
213 lm_isa_detach(struct device *dev)
215 struct lm_isa_softc *sc = device_get_softc(dev);
218 /* Bus-independent detachment */
219 error = lm_detach(&sc->sc_lmsc);
223 error = bus_release_resource(dev, SYS_RES_IOPORT, sc->sc_iorid,
232 lm_isa_readreg(struct lm_softc *lmsc, int reg)
234 struct lm_isa_softc *sc = (struct lm_isa_softc *)lmsc;
236 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMC_ADDR, reg);
237 return (bus_space_read_1(sc->sc_iot, sc->sc_ioh, LMC_DATA));
241 lm_isa_writereg(struct lm_softc *lmsc, int reg, int val)
243 struct lm_isa_softc *sc = (struct lm_isa_softc *)lmsc;
245 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMC_ADDR, reg);
246 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LMC_DATA, val);