| Commit | Line | Data |
|---|---|---|
| a5261280 CM |
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> | |
| f81520ed | 5 | * Copyright (c) 2010 Constantine A. Murenin <cnst++@dragonflybsd.org> |
| a5261280 CM |
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> | |
| f81520ed | 25 | #include <sys/bus.h> |
| a5261280 | 26 | #include <sys/kernel.h> |
| f81520ed CM |
27 | #include <sys/module.h> |
| 28 | #include <sys/rman.h> | |
| a5261280 CM |
29 | #include <sys/systm.h> |
| 30 | ||
| f81520ed | 31 | #include <bus/isa/isavar.h> |
| a5261280 CM |
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 | |
| 2c20825b | 51 | #define WBSIO_ID_W83627DHGP 0xb0 |
| a5261280 CM |
52 | #define WBSIO_ID_W83627SF 0x59 |
| 53 | #define WBSIO_ID_W83637HF 0x70 | |
| 1c486d4b | 54 | #define WBSIO_ID_W83667HG 0xa5 |
| 98037633 | 55 | #define WBSIO_ID_W83687THF 0x85 |
| a5261280 CM |
56 | #define WBSIO_ID_W83697HF 0x60 |
| 57 | ||
| 58 | /* Logical Device Number (LDN) Assignments */ | |
| 59 | #define WBSIO_LDN_HM 0x0b | |
| 60 | ||
| 61 | /* Hardware Monitor Control Registers (LDN B) */ | |
| 62 | #define WBSIO_HM_ADDR_MSB 0x60 /* Address [15:8] */ | |
| 63 | #define WBSIO_HM_ADDR_LSB 0x61 /* Address [7:0] */ | |
| 64 | ||
| 65 | struct wbsio_softc { | |
| f81520ed CM |
66 | struct device *sc_dev; |
| 67 | ||
| 68 | struct resource *sc_iores; | |
| 69 | int sc_iorid; | |
| a5261280 CM |
70 | |
| 71 | bus_space_tag_t sc_iot; | |
| 72 | bus_space_handle_t sc_ioh; | |
| 73 | }; | |
| 74 | ||
| fa130c02 | 75 | static void wbsio_identify(driver_t *, struct device *); |
| f81520ed CM |
76 | static int wbsio_probe(struct device *); |
| 77 | static int wbsio_attach(struct device *); | |
| 78 | static int wbsio_detach(struct device *); | |
| 79 | ||
| 80 | static device_method_t wbsio_methods[] = { | |
| fa130c02 | 81 | DEVMETHOD(device_identify, wbsio_identify), |
| f81520ed CM |
82 | DEVMETHOD(device_probe, wbsio_probe), |
| 83 | DEVMETHOD(device_attach, wbsio_attach), | |
| 84 | DEVMETHOD(device_detach, wbsio_detach), | |
| 85 | ||
| 86 | { NULL, NULL} | |
| 87 | }; | |
| 88 | ||
| 89 | static driver_t wbsio_driver = { | |
| 90 | "wbsio", | |
| 91 | wbsio_methods, | |
| 92 | sizeof(struct wbsio_softc) | |
| 93 | }; | |
| 94 | ||
| 95 | static devclass_t wbsio_devclass; | |
| 96 | ||
| 97 | DRIVER_MODULE(wbsio, isa, wbsio_driver, wbsio_devclass, NULL, NULL); | |
| a5261280 | 98 | |
| a5261280 CM |
99 | |
| 100 | static __inline void | |
| 101 | wbsio_conf_enable(bus_space_tag_t iot, bus_space_handle_t ioh) | |
| 102 | { | |
| 103 | bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC); | |
| 104 | bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC); | |
| 105 | } | |
| 106 | ||
| 107 | static __inline void | |
| 108 | wbsio_conf_disable(bus_space_tag_t iot, bus_space_handle_t ioh) | |
| 109 | { | |
| 110 | bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_DS_MAGIC); | |
| 111 | } | |
| 112 | ||
| 113 | static __inline u_int8_t | |
| 114 | wbsio_conf_read(bus_space_tag_t iot, bus_space_handle_t ioh, u_int8_t index) | |
| 115 | { | |
| 116 | bus_space_write_1(iot, ioh, WBSIO_INDEX, index); | |
| 117 | return (bus_space_read_1(iot, ioh, WBSIO_DATA)); | |
| 118 | } | |
| 119 | ||
| 120 | static __inline void | |
| 121 | wbsio_conf_write(bus_space_tag_t iot, bus_space_handle_t ioh, u_int8_t index, | |
| 122 | u_int8_t data) | |
| 123 | { | |
| 124 | bus_space_write_1(iot, ioh, WBSIO_INDEX, index); | |
| 125 | bus_space_write_1(iot, ioh, WBSIO_DATA, data); | |
| 126 | } | |
| 127 | ||
| fa130c02 CM |
128 | static void |
| 129 | wbsio_identify(driver_t *driver, struct device *parent) | |
| 130 | { | |
| 131 | #ifdef KLD_MODULE | |
| 132 | struct device *child[2]; | |
| 133 | const int port[2] = { 0x2e, 0x4e }; | |
| 134 | ||
| 135 | for (int i = 0; i < 2; i++) { | |
| 136 | child[i] = device_find_child(parent, driver->name, i); | |
| 137 | if (child[i] == NULL) { | |
| 138 | child[i] = BUS_ADD_CHILD(parent, parent, ISA_ORDER_PNP, | |
| 139 | driver->name, i); | |
| 140 | if (child[i] == NULL) { | |
| 141 | kprintf("%s: cannot add child[%i]\n", | |
| 142 | __func__, i); | |
| 143 | continue; | |
| 144 | } | |
| 145 | } else | |
| 146 | continue; | |
| 147 | if (bus_set_resource(child[i], SYS_RES_IOPORT, 0, | |
| 148 | port[i], WBSIO_IOSIZE)) | |
| 149 | kprintf("%s: cannot set resource for child[%i]\n", | |
| 150 | __func__, i); | |
| 151 | } | |
| 152 | #endif | |
| 153 | } | |
| 154 | ||
| f81520ed CM |
155 | static int |
| 156 | wbsio_probe(struct device *dev) | |
| a5261280 | 157 | { |
| f81520ed CM |
158 | struct resource *iores; |
| 159 | int iorid = 0; | |
| a5261280 CM |
160 | bus_space_tag_t iot; |
| 161 | bus_space_handle_t ioh; | |
| f81520ed CM |
162 | uint8_t reg_id, reg_rev; |
| 163 | const char *desc = NULL; | |
| 164 | char fulldesc[64]; | |
| a5261280 | 165 | |
| f81520ed | 166 | /* Match by device ID */ |
| a5261280 | 167 | |
| f81520ed CM |
168 | iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &iorid, |
| 169 | 0ul, ~0ul, WBSIO_IOSIZE, | |
| 170 | RF_ACTIVE); | |
| 171 | if (iores == NULL) | |
| 172 | return ENXIO; | |
| 173 | iot = rman_get_bustag(iores); | |
| 174 | ioh = rman_get_bushandle(iores); | |
| a5261280 | 175 | |
| a5261280 | 176 | wbsio_conf_enable(iot, ioh); |
| f81520ed CM |
177 | /* Read device ID */ |
| 178 | reg_id = wbsio_conf_read(iot, ioh, WBSIO_ID); | |
| 179 | /* Read device revision */ | |
| 180 | reg_rev = wbsio_conf_read(iot, ioh, WBSIO_REV); | |
| a5261280 | 181 | wbsio_conf_disable(iot, ioh); |
| f81520ed | 182 | bus_release_resource(dev, SYS_RES_IOPORT, iorid, iores); |
| a5261280 | 183 | |
| f81520ed | 184 | switch (reg_id) { |
| a5261280 CM |
185 | case WBSIO_ID_W83627HF: |
| 186 | desc = "W83627HF"; | |
| 187 | break; | |
| 188 | case WBSIO_ID_W83627THF: | |
| 189 | desc = "W83627THF"; | |
| 190 | break; | |
| 191 | case WBSIO_ID_W83627EHF: | |
| 192 | desc = "W83627EHF"; | |
| 193 | break; | |
| 194 | case WBSIO_ID_W83627DHG: | |
| 195 | desc = "W83627DHG"; | |
| 196 | break; | |
| 2c20825b CM |
197 | case WBSIO_ID_W83627DHGP: |
| 198 | desc = "W83627DHG-P"; | |
| 199 | break; | |
| a5261280 CM |
200 | case WBSIO_ID_W83637HF: |
| 201 | desc = "W83637HF"; | |
| 202 | break; | |
| 1c486d4b CM |
203 | case WBSIO_ID_W83667HG: |
| 204 | desc = "W83667HG"; | |
| 205 | break; | |
| 98037633 CM |
206 | case WBSIO_ID_W83687THF: |
| 207 | desc = "W83687THF"; | |
| 208 | break; | |
| a5261280 CM |
209 | case WBSIO_ID_W83697HF: |
| 210 | desc = "W83697HF"; | |
| 211 | break; | |
| 212 | } | |
| 213 | ||
| 2d0bc204 CM |
214 | if (desc == NULL) { |
| 215 | #ifndef KLD_MODULE | |
| 216 | if (bootverbose) | |
| 217 | #endif | |
| 27a3408e CM |
218 | if (!(reg_id == 0xff && reg_rev == 0xff)) |
| 219 | device_printf(dev, "%s port 0x%02x: " | |
| 220 | "Device ID 0x%02x, Rev 0x%02x\n", | |
| 221 | __func__, isa_get_port(dev), | |
| 222 | reg_id, reg_rev); | |
| f81520ed | 223 | return ENXIO; |
| 2d0bc204 | 224 | } |
| a5261280 | 225 | |
| f81520ed CM |
226 | ksnprintf(fulldesc, sizeof(fulldesc), |
| 227 | "Winbond LPC Super I/O %s rev 0x%02x", desc, reg_rev); | |
| 228 | device_set_desc_copy(dev, fulldesc); | |
| 229 | return 0; | |
| 230 | } | |
| 231 | ||
| 232 | static int | |
| 233 | wbsio_attach(struct device *dev) | |
| 234 | { | |
| 235 | struct wbsio_softc *sc = device_get_softc(dev); | |
| 236 | uint8_t reg0, reg1; | |
| 237 | uint16_t iobase; | |
| 238 | struct device *parent = device_get_parent(dev); | |
| 239 | struct device *child; | |
| 240 | struct devclass *c_dc; | |
| 241 | int c_maxunit; | |
| 242 | ||
| 243 | /* Map ISA I/O space */ | |
| 244 | sc->sc_iores = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->sc_iorid, | |
| 245 | 0ul, ~0ul, WBSIO_IOSIZE, | |
| 246 | RF_ACTIVE); | |
| 247 | if (sc->sc_iores == NULL) { | |
| 248 | device_printf(dev, "can't map i/o space\n"); | |
| 249 | return ENXIO; | |
| 250 | } | |
| 251 | sc->sc_iot = rman_get_bustag(sc->sc_iores); | |
| 252 | sc->sc_ioh = rman_get_bushandle(sc->sc_iores); | |
| 253 | ||
| 254 | /* Enter configuration mode */ | |
| 255 | wbsio_conf_enable(sc->sc_iot, sc->sc_ioh); | |
| a5261280 CM |
256 | |
| 257 | /* Select HM logical device */ | |
| 258 | wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM); | |
| 259 | ||
| 260 | /* | |
| 261 | * The address should be 8-byte aligned, but it seems some | |
| 262 | * BIOSes ignore this. They get away with it, because | |
| 263 | * Apparently the hardware simply ignores the lower three | |
| 264 | * bits. We do the same here. | |
| 265 | */ | |
| 266 | reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_LSB); | |
| 267 | reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_MSB); | |
| 268 | iobase = (reg1 << 8) | (reg0 & ~0x7); | |
| f81520ed | 269 | device_printf(dev, "hardware monitor iobase is 0x%x\n", iobase); |
| a5261280 CM |
270 | |
| 271 | /* Escape from configuration mode */ | |
| 272 | wbsio_conf_disable(sc->sc_iot, sc->sc_ioh); | |
| 273 | ||
| f81520ed CM |
274 | if (iobase == 0) { |
| 275 | device_printf(dev, "no hardware monitor configured\n"); | |
| 276 | return 0; | |
| 277 | } | |
| a5261280 | 278 | |
| f81520ed CM |
279 | child = NULL; |
| 280 | c_dc = devclass_find("lm"); | |
| 281 | if (c_dc == NULL) { | |
| 282 | device_printf(dev, "lm devclass not found\n"); | |
| 283 | return ENXIO; | |
| 284 | } | |
| 285 | c_maxunit = devclass_get_maxunit(c_dc); | |
| 286 | for (int u = 0; u < c_maxunit; u++) { | |
| 287 | child = devclass_get_device(c_dc, u); | |
| 288 | if (child == NULL) | |
| 289 | continue; | |
| 290 | if (isa_get_port(child) == iobase) { | |
| 291 | if (device_is_attached(child)) { | |
| 292 | device_printf(dev, | |
| 293 | "%s is already attached at 0x%x\n", | |
| 294 | device_get_nameunit(child), iobase); | |
| 295 | return 0; | |
| 296 | } | |
| 297 | break; | |
| 298 | } | |
| 647ed4b2 CM |
299 | if (device_is_attached(child)) { |
| 300 | child = NULL; | |
| f81520ed | 301 | continue; |
| 647ed4b2 | 302 | } |
| f81520ed CM |
303 | device_printf(dev, |
| 304 | "found unused %s at 0x%x with state %i, reusing at 0x%x\n", | |
| 305 | device_get_nameunit(child), isa_get_port(child), | |
| 306 | device_get_state(child), iobase); | |
| 307 | break; | |
| 308 | } | |
| 309 | if (child == NULL) | |
| 310 | child = BUS_ADD_CHILD(parent, parent, ISA_ORDER_PNP, | |
| 311 | "lm", -1); | |
| 312 | // child = BUS_ADD_CHILD(parent, parent, ISA_ORDER_PNP, | |
| 313 | // "lm", 3 + device_get_unit(dev)); | |
| 314 | if (child == NULL) { | |
| 315 | device_printf(dev, "cannot add child\n"); | |
| 316 | return ENXIO; | |
| 317 | } | |
| 318 | if (bus_set_resource(child, SYS_RES_IOPORT, 0, iobase, 8)) { | |
| 319 | device_printf(dev, "cannot set resource\n"); | |
| 320 | return ENXIO; | |
| 321 | } | |
| 322 | return device_probe_and_attach(child); | |
| a5261280 CM |
323 | } |
| 324 | ||
| f81520ed CM |
325 | static int |
| 326 | wbsio_detach(struct device *dev) | |
| a5261280 | 327 | { |
| f81520ed CM |
328 | struct wbsio_softc *sc = device_get_softc(dev); |
| 329 | ||
| 330 | return bus_release_resource(dev, SYS_RES_IOPORT, | |
| 331 | sc->sc_iorid, sc->sc_iores); | |
| a5261280 | 332 | } |