Home | History | Annotate | Line # | Download | only in isa
wbsio.c revision 1.4
      1  1.4  jakllsch /*	$NetBSD: wbsio.c,v 1.4 2012/01/17 16:28:33 jakllsch Exp $	*/
      2  1.1      cnst /*	$OpenBSD: wbsio.c,v 1.5 2009/03/29 21:53:52 sthen Exp $	*/
      3  1.1      cnst /*
      4  1.1      cnst  * Copyright (c) 2008 Mark Kettenis <kettenis (at) openbsd.org>
      5  1.1      cnst  *
      6  1.1      cnst  * Permission to use, copy, modify, and distribute this software for any
      7  1.1      cnst  * purpose with or without fee is hereby granted, provided that the above
      8  1.1      cnst  * copyright notice and this permission notice appear in all copies.
      9  1.1      cnst  *
     10  1.1      cnst  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  1.1      cnst  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  1.1      cnst  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  1.1      cnst  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  1.1      cnst  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  1.1      cnst  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  1.1      cnst  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.1      cnst  */
     18  1.1      cnst 
     19  1.1      cnst /*
     20  1.1      cnst  * Winbond LPC Super I/O driver.
     21  1.1      cnst  */
     22  1.1      cnst 
     23  1.1      cnst #include <sys/param.h>
     24  1.1      cnst #include <sys/device.h>
     25  1.1      cnst #include <sys/kernel.h>
     26  1.1      cnst #include <sys/systm.h>
     27  1.1      cnst 
     28  1.3    dyoung #include <sys/bus.h>
     29  1.1      cnst 
     30  1.1      cnst #include <dev/isa/isareg.h>
     31  1.1      cnst #include <dev/isa/isavar.h>
     32  1.1      cnst 
     33  1.1      cnst /* ISA bus registers */
     34  1.1      cnst #define WBSIO_INDEX		0x00	/* Configuration Index Register */
     35  1.1      cnst #define WBSIO_DATA		0x01	/* Configuration Data Register */
     36  1.1      cnst 
     37  1.1      cnst #define WBSIO_IOSIZE		0x02	/* ISA I/O space size */
     38  1.1      cnst 
     39  1.1      cnst #define WBSIO_CONF_EN_MAGIC	0x87	/* enable configuration mode */
     40  1.1      cnst #define WBSIO_CONF_DS_MAGIC	0xaa	/* disable configuration mode */
     41  1.1      cnst 
     42  1.1      cnst /* Configuration Space Registers */
     43  1.1      cnst #define WBSIO_LDN		0x07	/* Logical Device Number */
     44  1.1      cnst #define WBSIO_ID		0x20	/* Device ID */
     45  1.1      cnst #define WBSIO_REV		0x21	/* Device Revision */
     46  1.1      cnst 
     47  1.1      cnst #define WBSIO_ID_W83627HF	0x52
     48  1.1      cnst #define WBSIO_ID_W83627THF	0x82
     49  1.1      cnst #define WBSIO_ID_W83627EHF	0x88
     50  1.1      cnst #define WBSIO_ID_W83627DHG	0xa0
     51  1.1      cnst #define WBSIO_ID_W83627SF	0x59
     52  1.1      cnst #define WBSIO_ID_W83637HF	0x70
     53  1.2      cnst #define WBSIO_ID_W83667HG	0xa5
     54  1.1      cnst #define WBSIO_ID_W83697HF	0x60
     55  1.1      cnst 
     56  1.1      cnst /* Logical Device Number (LDN) Assignments */
     57  1.1      cnst #define WBSIO_LDN_HM		0x0b
     58  1.1      cnst 
     59  1.1      cnst /* Hardware Monitor Control Registers (LDN B) */
     60  1.1      cnst #define WBSIO_HM_ADDR_MSB	0x60	/* Address [15:8] */
     61  1.1      cnst #define WBSIO_HM_ADDR_LSB	0x61	/* Address [7:0] */
     62  1.1      cnst 
     63  1.1      cnst struct wbsio_softc {
     64  1.1      cnst 	struct device		sc_dev;
     65  1.1      cnst 
     66  1.1      cnst 	bus_space_tag_t		sc_iot;
     67  1.1      cnst 	bus_space_handle_t	sc_ioh;
     68  1.1      cnst };
     69  1.1      cnst 
     70  1.1      cnst int	wbsio_probe(device_t, cfdata_t, void *);
     71  1.1      cnst void	wbsio_attach(device_t, device_t, void *);
     72  1.1      cnst int	wbsio_print(void *, const char *);
     73  1.1      cnst 
     74  1.1      cnst CFATTACH_DECL_NEW(wbsio, sizeof(struct wbsio_softc),
     75  1.1      cnst     wbsio_probe, wbsio_attach, NULL, NULL);
     76  1.1      cnst 
     77  1.1      cnst static __inline void
     78  1.1      cnst wbsio_conf_enable(bus_space_tag_t iot, bus_space_handle_t ioh)
     79  1.1      cnst {
     80  1.1      cnst 	bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
     81  1.1      cnst 	bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_EN_MAGIC);
     82  1.1      cnst }
     83  1.1      cnst 
     84  1.1      cnst static __inline void
     85  1.1      cnst wbsio_conf_disable(bus_space_tag_t iot, bus_space_handle_t ioh)
     86  1.1      cnst {
     87  1.1      cnst 	bus_space_write_1(iot, ioh, WBSIO_INDEX, WBSIO_CONF_DS_MAGIC);
     88  1.1      cnst }
     89  1.1      cnst 
     90  1.1      cnst static __inline u_int8_t
     91  1.1      cnst wbsio_conf_read(bus_space_tag_t iot, bus_space_handle_t ioh, u_int8_t index)
     92  1.1      cnst {
     93  1.1      cnst 	bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
     94  1.1      cnst 	return (bus_space_read_1(iot, ioh, WBSIO_DATA));
     95  1.1      cnst }
     96  1.1      cnst 
     97  1.1      cnst static __inline void
     98  1.1      cnst wbsio_conf_write(bus_space_tag_t iot, bus_space_handle_t ioh, u_int8_t index,
     99  1.1      cnst     u_int8_t data)
    100  1.1      cnst {
    101  1.1      cnst 	bus_space_write_1(iot, ioh, WBSIO_INDEX, index);
    102  1.1      cnst 	bus_space_write_1(iot, ioh, WBSIO_DATA, data);
    103  1.1      cnst }
    104  1.1      cnst 
    105  1.1      cnst int
    106  1.1      cnst wbsio_probe(device_t parent, cfdata_t match, void *aux)
    107  1.1      cnst {
    108  1.1      cnst 	struct isa_attach_args *ia = aux;
    109  1.1      cnst 	bus_space_tag_t iot;
    110  1.1      cnst 	bus_space_handle_t ioh;
    111  1.1      cnst 	u_int8_t reg;
    112  1.1      cnst 
    113  1.1      cnst 	/* Must supply an address */
    114  1.1      cnst 	if (ia->ia_nio < 1)
    115  1.1      cnst 		return 0;
    116  1.1      cnst 
    117  1.1      cnst 	if (ISA_DIRECT_CONFIG(ia))
    118  1.1      cnst 		return 0;
    119  1.1      cnst 
    120  1.1      cnst 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    121  1.1      cnst 		return 0;
    122  1.1      cnst 
    123  1.1      cnst 	/* Match by device ID */
    124  1.1      cnst 	iot = ia->ia_iot;
    125  1.1      cnst 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, WBSIO_IOSIZE, 0, &ioh))
    126  1.1      cnst 		return 0;
    127  1.1      cnst 	wbsio_conf_enable(iot, ioh);
    128  1.1      cnst 	reg = wbsio_conf_read(iot, ioh, WBSIO_ID);
    129  1.1      cnst 	aprint_debug("wbsio_probe: id 0x%02x\n", reg);
    130  1.1      cnst 	wbsio_conf_disable(iot, ioh);
    131  1.1      cnst 	bus_space_unmap(iot, ioh, WBSIO_IOSIZE);
    132  1.1      cnst 	switch (reg) {
    133  1.1      cnst 	case WBSIO_ID_W83627HF:
    134  1.1      cnst 	case WBSIO_ID_W83627THF:
    135  1.1      cnst 	case WBSIO_ID_W83627EHF:
    136  1.1      cnst 	case WBSIO_ID_W83627DHG:
    137  1.1      cnst 	case WBSIO_ID_W83637HF:
    138  1.1      cnst 	case WBSIO_ID_W83697HF:
    139  1.1      cnst 		ia->ia_nio = 1;
    140  1.1      cnst 		ia->ia_io[0].ir_size = WBSIO_IOSIZE;
    141  1.1      cnst 		ia->ia_niomem = 0;
    142  1.1      cnst 		ia->ia_nirq = 0;
    143  1.1      cnst 		ia->ia_ndrq = 0;
    144  1.1      cnst 		return 1;
    145  1.1      cnst 	}
    146  1.1      cnst 
    147  1.1      cnst 	return 0;
    148  1.1      cnst }
    149  1.1      cnst 
    150  1.1      cnst void
    151  1.1      cnst wbsio_attach(device_t parent, device_t self, void *aux)
    152  1.1      cnst {
    153  1.1      cnst 	struct wbsio_softc *sc = (void *)self;
    154  1.1      cnst 	struct isa_attach_args *ia = aux;
    155  1.1      cnst 	struct isa_attach_args nia;
    156  1.1      cnst 	const char *desc = NULL;
    157  1.1      cnst 	u_int8_t reg, reg0, reg1;
    158  1.1      cnst 	u_int16_t iobase;
    159  1.1      cnst 
    160  1.1      cnst 	/* Map ISA I/O space */
    161  1.1      cnst 	sc->sc_iot = ia->ia_iot;
    162  1.1      cnst 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
    163  1.1      cnst 	    WBSIO_IOSIZE, 0, &sc->sc_ioh)) {
    164  1.1      cnst 		aprint_error(": can't map i/o space\n");
    165  1.1      cnst 		return;
    166  1.1      cnst 	}
    167  1.1      cnst 
    168  1.1      cnst 	/* Enter configuration mode */
    169  1.1      cnst 	wbsio_conf_enable(sc->sc_iot, sc->sc_ioh);
    170  1.1      cnst 
    171  1.1      cnst 	/* Read device ID */
    172  1.1      cnst 	reg = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_ID);
    173  1.1      cnst 	switch (reg) {
    174  1.1      cnst 	case WBSIO_ID_W83627HF:
    175  1.1      cnst 		desc = "W83627HF";
    176  1.1      cnst 		break;
    177  1.1      cnst 	case WBSIO_ID_W83627THF:
    178  1.1      cnst 		desc = "W83627THF";
    179  1.1      cnst 		break;
    180  1.1      cnst 	case WBSIO_ID_W83627EHF:
    181  1.1      cnst 		desc = "W83627EHF";
    182  1.1      cnst 		break;
    183  1.1      cnst 	case WBSIO_ID_W83627DHG:
    184  1.1      cnst 		desc = "W83627DHG";
    185  1.1      cnst 		break;
    186  1.1      cnst 	case WBSIO_ID_W83637HF:
    187  1.1      cnst 		desc = "W83637HF";
    188  1.1      cnst 		break;
    189  1.2      cnst 	case WBSIO_ID_W83667HG:
    190  1.2      cnst 		desc = "W83667HG";
    191  1.2      cnst 		break;
    192  1.1      cnst 	case WBSIO_ID_W83697HF:
    193  1.1      cnst 		desc = "W83697HF";
    194  1.1      cnst 		break;
    195  1.1      cnst 	}
    196  1.1      cnst 
    197  1.1      cnst 	/* Read device revision */
    198  1.1      cnst 	reg = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_REV);
    199  1.1      cnst 
    200  1.1      cnst 	aprint_naive("\n");
    201  1.1      cnst 	aprint_normal(": Winbond LPC Super I/O %s rev 0x%02x\n", desc, reg);
    202  1.1      cnst 
    203  1.1      cnst 	/* Select HM logical device */
    204  1.1      cnst 	wbsio_conf_write(sc->sc_iot, sc->sc_ioh, WBSIO_LDN, WBSIO_LDN_HM);
    205  1.1      cnst 
    206  1.1      cnst 	/*
    207  1.1      cnst 	 * The address should be 8-byte aligned, but it seems some
    208  1.1      cnst 	 * BIOSes ignore this.  They get away with it, because
    209  1.1      cnst 	 * Apparently the hardware simply ignores the lower three
    210  1.1      cnst 	 * bits.  We do the same here.
    211  1.1      cnst 	 */
    212  1.1      cnst 	reg0 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_LSB);
    213  1.1      cnst 	reg1 = wbsio_conf_read(sc->sc_iot, sc->sc_ioh, WBSIO_HM_ADDR_MSB);
    214  1.1      cnst 	iobase = (reg1 << 8) | (reg0 & ~0x7);
    215  1.1      cnst 
    216  1.1      cnst 	/* Escape from configuration mode */
    217  1.1      cnst 	wbsio_conf_disable(sc->sc_iot, sc->sc_ioh);
    218  1.1      cnst 
    219  1.4  jakllsch 	if (!pmf_device_register(self, NULL, NULL))
    220  1.4  jakllsch 		aprint_error_dev(self, "couldn't establish power handler\n");
    221  1.4  jakllsch 
    222  1.1      cnst 	if (iobase == 0)
    223  1.1      cnst 		return;
    224  1.1      cnst 
    225  1.1      cnst 	nia = *ia;
    226  1.1      cnst 	nia.ia_io[0].ir_addr = iobase;
    227  1.1      cnst 	config_found(self, &nia, wbsio_print);
    228  1.1      cnst }
    229  1.1      cnst 
    230  1.1      cnst int
    231  1.1      cnst wbsio_print(void *aux, const char *pnp)
    232  1.1      cnst {
    233  1.1      cnst 	struct isa_attach_args *ia = aux;
    234  1.1      cnst 
    235  1.1      cnst 	if (pnp)
    236  1.1      cnst 		aprint_normal("%s", pnp);
    237  1.1      cnst 	if (ia->ia_io[0].ir_size)
    238  1.1      cnst 		aprint_normal(" port 0x%x", ia->ia_io[0].ir_addr);
    239  1.1      cnst 	if (ia->ia_io[0].ir_size > 1)
    240  1.1      cnst 		aprint_normal("-0x%x", ia->ia_io[0].ir_addr +
    241  1.1      cnst 		    ia->ia_io[0].ir_size - 1);
    242  1.1      cnst 	return (UNCONF);
    243  1.1      cnst }
    244