wbsio(4): support W83667HG (0xa5); its hw mon is already supported by lm(4) as W83627...
[dragonfly.git] / sys / dev / powermng / wbsio / wbsio.c
1 /*      $NetBSD: wbsio.c,v 1.1 2010/02/21 05:16:29 cnst Exp $   */
2 /*      $OpenBSD: wbsio.c,v 1.5 2009/03/29 21:53:52 sthen Exp $ */
3 /*
4  * Copyright (c) 2008 Mark Kettenis <kettenis@openbsd.org>
5  * Copyright (c) 2010 Constantine A. Murenin <cnst++@dragonflybsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 /*
21  * Winbond LPC Super I/O driver.
22  */
23
24 #include <sys/param.h>
25 #include <sys/bus.h>
26 #include <sys/kernel.h>
27 #include <sys/module.h>
28 #include <sys/rman.h>
29 #include <sys/systm.h>
30
31 #include <bus/isa/isavar.h>
32
33 /* ISA bus registers */
34 #define WBSIO_INDEX             0x00    /* Configuration Index Register */
35 #define WBSIO_DATA              0x01    /* Configuration Data Register */
36
37 #define WBSIO_IOSIZE            0x02    /* ISA I/O space size */
38
39 #define WBSIO_CONF_EN_MAGIC     0x87    /* enable configuration mode */
40 #define WBSIO_CONF_DS_MAGIC     0xaa    /* disable configuration mode */
41
42 /* Configuration Space Registers */
43 #define WBSIO_LDN               0x07    /* Logical Device Number */
44 #define WBSIO_ID                0x20    /* Device ID */
45 #define WBSIO_REV               0x21    /* Device Revision */
46
47 #define WBSIO_ID_W83627HF       0x52
48 #define WBSIO_ID_W83627THF      0x82
49 #define WBSIO_ID_W83627EHF      0x88
50 #define WBSIO_ID_W83627DHG      0xa0
51 #define WBSIO_ID_W83627SF       0x59
52 #define WBSIO_ID_W83637HF       0x70
53 #define WBSIO_ID_W83667HG       0xa5
54 #define WBSIO_ID_W83697HF       0x60
55
56 /* Logical Device Number (LDN) Assignments */
57 #define WBSIO_LDN_HM            0x0b
58
59 /* Hardware Monitor Control Registers (LDN B) */
60 #define WBSIO_HM_ADDR_MSB       0x60    /* Address [15:8] */
61 #define WBSIO_HM_ADDR_LSB       0x61    /* Address [7:0] */
62
63 struct wbsio_softc {
64         struct device           *sc_dev;
65
66         struct resource         *sc_iores;
67         int                     sc_iorid;
68
69         bus_space_tag_t         sc_iot;
70         bus_space_handle_t      sc_ioh;
71 };
72
73 static int      wbsio_probe(struct device *);
74 static int      wbsio_attach(struct device *);
75 static int      wbsio_detach(struct device *);
76
77 static device_method_t wbsio_methods[] = {
78         DEVMETHOD(device_probe,         wbsio_probe),
79         DEVMETHOD(device_attach,        wbsio_attach),
80         DEVMETHOD(device_detach,        wbsio_detach),
81
82         { NULL, NULL}
83 };
84
85 static driver_t wbsio_driver = {
86         "wbsio",
87         wbsio_methods,
88         sizeof(struct wbsio_softc)
89 };
90
91 static devclass_t wbsio_devclass;
92
93 DRIVER_MODULE(wbsio, isa, wbsio_driver, wbsio_devclass, NULL, NULL);
94
95
96 static __inline void
97 wbsio_conf_enable(bus_space_tag_t iot, bus_space_handle_t ioh)
98 {
99         bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
100         bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
101 }
102
103 static __inline void
104 wbsio_conf_disable(bus_space_tag_t iot, bus_space_handle_t ioh)
105 {
106         bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_DS_MAGIC);
107 }
108
109 static __inline u_int8_t
110 wbsio_conf_read(bus_space_tag_t iot, bus_space_handle_t ioh, u_int8_t index)
111 {
112         bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
113         return (bus_space_read_1(iot, ioh, WBSIO_DATA));
114 }
115
116 static __inline void
117 wbsio_conf_write(bus_space_tag_t iot, bus_space_handle_t ioh, u_int8_t index,
118     u_int8_t data)
119 {
120         bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
121         bus_space_write_1(iot, ioh, WBSIO_DATA, data);
122 }
123
124 static int
125 wbsio_probe(struct device *dev)
126 {
127         struct resource *iores;
128         int iorid = 0;
129         bus_space_tag_t iot;
130         bus_space_handle_t ioh;
131         uint8_t reg_id, reg_rev;
132         const char *desc = NULL;
133         char fulldesc[64];
134
135         /* Match by device ID */
136
137         iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &iorid,
138             0ul, ~0ul, WBSIO_IOSIZE,
139             RF_ACTIVE);
140         if (iores == NULL)
141                 return ENXIO;
142         iot = rman_get_bustag(iores);
143         ioh = rman_get_bushandle(iores);
144
145         wbsio_conf_enable(iot, ioh);
146         /* Read device ID */
147         reg_id = wbsio_conf_read(iot, ioh, WBSIO_ID);
148         /* Read device revision */
149         reg_rev = wbsio_conf_read(iot, ioh, WBSIO_REV);
150         wbsio_conf_disable(iot, ioh);
151         bus_release_resource(dev, SYS_RES_IOPORT, iorid, iores);
152
153         switch (reg_id) {
154         case WBSIO_ID_W83627HF:
155                 desc = "W83627HF";
156                 break;
157         case WBSIO_ID_W83627THF:
158                 desc = "W83627THF";
159                 break;
160         case WBSIO_ID_W83627EHF:
161                 desc = "W83627EHF";
162                 break;
163         case WBSIO_ID_W83627DHG:
164                 desc = "W83627DHG";
165                 break;
166         case WBSIO_ID_W83637HF:
167                 desc = "W83637HF";
168                 break;
169         case WBSIO_ID_W83667HG:
170                 desc = "W83667HG";
171                 break;
172         case WBSIO_ID_W83697HF:
173                 desc = "W83697HF";
174                 break;
175         }
176
177         if (desc == NULL)
178                 return ENXIO;
179
180         ksnprintf(fulldesc, sizeof(fulldesc),
181             "Winbond LPC Super I/O %s rev 0x%02x", desc, reg_rev);
182         device_set_desc_copy(dev, fulldesc);
183         return 0;
184 }
185
186 static int
187 wbsio_attach(struct device *dev)
188 {
189         struct wbsio_softc *sc = device_get_softc(dev);
190         uint8_t reg0, reg1;
191         uint16_t iobase;
192         struct device *parent = device_get_parent(dev);
193         struct device *child;
194         struct devclass *c_dc;
195         int c_maxunit;
196
197         /* Map ISA I/O space */
198         sc->sc_iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->sc_iorid,
199             0ul, ~0ul, WBSIO_IOSIZE,
200             RF_ACTIVE);
201         if (sc->sc_iores == NULL) {
202                 device_printf(dev, "can't map i/o space\n");
203                 return ENXIO;
204         }
205         sc->sc_iot = rman_get_bustag(sc->sc_iores);
206         sc->sc_ioh = rman_get_bushandle(sc->sc_iores);
207
208         /* Enter configuration mode */
209         wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
210
211         /* Select HM logical device */
212         wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
213
214         /*
215          * The address should be 8-byte aligned, but it seems some
216          * BIOSes ignore this.  They get away with it, because
217          * Apparently the hardware simply ignores the lower three
218          * bits.  We do the same here.
219          */
220         reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_LSB);
221         reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_MSB);
222         iobase = (reg1 << 8) | (reg0 & ~0x7);
223         device_printf(dev, "hardware monitor iobase is 0x%x\n", iobase);
224
225         /* Escape from configuration mode */
226         wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
227
228         if (iobase == 0) {
229                 device_printf(dev, "no hardware monitor configured\n");
230                 return 0;
231         }
232
233         child = NULL;
234         c_dc = devclass_find("lm");
235         if (c_dc == NULL) {
236                 device_printf(dev, "lm devclass not found\n");
237                 return ENXIO;
238         }
239         c_maxunit = devclass_get_maxunit(c_dc);
240         for (int u = 0; u < c_maxunit; u++) {
241                 child = devclass_get_device(c_dc, u);
242                 if (child == NULL)
243                         continue;
244                 if (isa_get_port(child) == iobase) {
245                         if (device_is_attached(child)) {
246                                 device_printf(dev,
247                                     "%s is already attached at 0x%x\n",
248                                     device_get_nameunit(child), iobase);
249                                 return 0;
250                         }
251                         break;
252                 }
253                 if (device_is_attached(child)) {
254                         child = NULL;
255                         continue;
256                 }
257                 device_printf(dev,
258                     "found unused %s at 0x%x with state %i, reusing at 0x%x\n",
259                     device_get_nameunit(child), isa_get_port(child),
260                     device_get_state(child), iobase);
261                 break;
262         }
263         if (child == NULL)
264                 child = BUS_ADD_CHILD(parent, parent, ISA_ORDER_PNP,
265                     "lm", -1);
266 //      child = BUS_ADD_CHILD(parent, parent, ISA_ORDER_PNP,
267 //          "lm", 3 + device_get_unit(dev));
268         if (child == NULL) {
269                 device_printf(dev, "cannot add child\n");
270                 return ENXIO;
271         }
272         if (bus_set_resource(child, SYS_RES_IOPORT, 0, iobase, 8)) {
273                 device_printf(dev, "cannot set resource\n");
274                 return ENXIO;
275         }
276         return device_probe_and_attach(child);
277 }
278
279 static int
280 wbsio_detach(struct device *dev)
281 {
282         struct wbsio_softc *sc = device_get_softc(dev);
283
284         return bus_release_resource(dev, SYS_RES_IOPORT,
285             sc->sc_iorid, sc->sc_iores);
286 }