Home | History | Annotate | Line # | Download | only in gemini
gemini_lpc.c revision 1.2
      1  1.2  cliff /*	$NetBSD: gemini_lpc.c,v 1.2 2008/11/10 04:05:35 cliff Exp $	*/
      2  1.1  cliff 
      3  1.1  cliff #include "opt_gemini.h"
      4  1.1  cliff #include "locators.h"
      5  1.1  cliff 
      6  1.1  cliff #include <sys/cdefs.h>
      7  1.2  cliff __KERNEL_RCSID(0, "$NetBSD: gemini_lpc.c,v 1.2 2008/11/10 04:05:35 cliff Exp $");
      8  1.1  cliff 
      9  1.1  cliff #include <sys/param.h>
     10  1.1  cliff #include <sys/systm.h>
     11  1.1  cliff #include <sys/device.h>
     12  1.1  cliff #include <arch/arm/gemini/gemini_lpcvar.h>
     13  1.1  cliff #include <arch/arm/gemini/gemini_lpchcvar.h>
     14  1.1  cliff #include <arch/arm/gemini/gemini_reg.h>
     15  1.1  cliff 
     16  1.1  cliff 
     17  1.1  cliff /* XXX these should be in lpcreg.h or it8721reg.h */
     18  1.1  cliff #define IT8712_CFGCTL		0x02
     19  1.1  cliff # define CFGCTL_WAITKEY		__BIT(1)
     20  1.1  cliff #define IT8712_LDN		0x07
     21  1.1  cliff #define IT8712_CHIPID1		0x20
     22  1.1  cliff #define IT8712_CHIPID2		0x21
     23  1.1  cliff #define IT8712_CSCV		0x22
     24  1.1  cliff # define CSCV_VERSION		__BITS(3,0);
     25  1.1  cliff #define IT8712_CLKSEL		0x23
     26  1.1  cliff #define IT8712_SWSUSPEND	0x24
     27  1.1  cliff #define IT8712_ADDR		0x2e
     28  1.1  cliff #define IT8712_DATA		0x2f
     29  1.1  cliff 
     30  1.1  cliff static int  gemini_lpc_match(struct device *, struct cfdata *, void *);
     31  1.1  cliff static void gemini_lpc_attach(struct device *, struct device *, void *);
     32  1.1  cliff static int  gemini_lpc_search(device_t, cfdata_t, const int *, void *);
     33  1.1  cliff static int  gemini_lpc_busprint(void *, const char *);
     34  1.1  cliff 
     35  1.1  cliff static uint8_t it8712_pnp_read(lpctag_t, int, uint);
     36  1.1  cliff static void    it8712_pnp_write(lpctag_t, int, uint, uint8_t);
     37  1.1  cliff static void    it8712_pnp_enter(lpctag_t);
     38  1.1  cliff static void    it8712_pnp_exit(lpctag_t);
     39  1.1  cliff 
     40  1.1  cliff CFATTACH_DECL_NEW(lpc, sizeof(struct gemini_lpc_softc),
     41  1.1  cliff     gemini_lpc_match, gemini_lpc_attach, NULL, NULL);
     42  1.1  cliff 
     43  1.2  cliff gemini_lpc_bus_ops_t gemini_lpc_bus_ops = {
     44  1.1  cliff 	it8712_pnp_read,
     45  1.1  cliff 	it8712_pnp_write,
     46  1.1  cliff 	it8712_pnp_enter,
     47  1.1  cliff 	it8712_pnp_exit,
     48  1.1  cliff 	gemini_lpchc_intr_establish,
     49  1.1  cliff 	gemini_lpchc_intr_disestablish,
     50  1.1  cliff };
     51  1.1  cliff 
     52  1.1  cliff 
     53  1.1  cliff static int
     54  1.1  cliff gemini_lpc_match(struct device *parent, struct cfdata *cf, void *aux)
     55  1.1  cliff {
     56  1.1  cliff 	struct gemini_lpc_attach_args *lpc = aux;
     57  1.1  cliff 
     58  1.1  cliff 	if (lpc->lpc_addr == LPCCF_ADDR_DEFAULT)
     59  1.1  cliff 		panic("lpc must have addr specified in config.");
     60  1.1  cliff 
     61  1.1  cliff 	return 1;
     62  1.1  cliff }
     63  1.1  cliff 
     64  1.1  cliff static void
     65  1.1  cliff gemini_lpc_attach(struct device *parent, struct device *self, void *aux)
     66  1.1  cliff {
     67  1.1  cliff 	gemini_lpc_softc_t *sc = device_private(self);
     68  1.1  cliff 	struct gemini_lpchc_attach_args *lpchc = aux;
     69  1.1  cliff 	bus_space_tag_t iot;
     70  1.1  cliff 	bus_space_handle_t ioh;
     71  1.1  cliff 	uint8_t id1, id2, vers, clk, susp;
     72  1.1  cliff 
     73  1.1  cliff 	sc->sc_addr = lpchc->lpchc_addr;
     74  1.1  cliff #if 0
     75  1.1  cliff 	sc->sc_size = lpchc->lpchc_size;
     76  1.1  cliff #else
     77  1.1  cliff 	/*
     78  1.1  cliff 	 * we just map the configuration registers
     79  1.1  cliff 	 * child devices can map their own I/O
     80  1.1  cliff 	 */
     81  1.1  cliff 	sc->sc_size = 4096;
     82  1.1  cliff #endif
     83  1.1  cliff 
     84  1.1  cliff 	iot = lpchc->lpchc_iot;
     85  1.1  cliff 	if (bus_space_map(iot, sc->sc_addr, sc->sc_size, 0, &ioh))
     86  1.1  cliff 		panic("%s: Cannot map registers", device_xname(self));
     87  1.1  cliff 	sc->sc_iot = iot;
     88  1.1  cliff 	sc->sc_ioh = ioh;
     89  1.1  cliff 
     90  1.1  cliff 	it8712_pnp_enter(sc);
     91  1.1  cliff 	id1  = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CHIPID1);
     92  1.1  cliff 	id2  = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CHIPID2);
     93  1.1  cliff 	vers = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CSCV);
     94  1.1  cliff 	vers &= CSCV_VERSION;
     95  1.1  cliff 	clk  = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CLKSEL);
     96  1.1  cliff 	susp = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_SWSUSPEND);
     97  1.1  cliff 	it8712_pnp_exit(sc);
     98  1.1  cliff 
     99  1.1  cliff 	aprint_normal("\n%s: chip ID %x%x, version %d",
    100  1.1  cliff 		device_xname(self), id1, id2, vers);
    101  1.1  cliff 	aprint_normal("\n%s: clksel %#x, susp %#x ",
    102  1.1  cliff 		device_xname(self), clk, susp);
    103  1.1  cliff 
    104  1.1  cliff 	sc->sc_lpchctag = lpchc->lpchc_tag;
    105  1.1  cliff 
    106  1.1  cliff 	aprint_normal("\n");
    107  1.1  cliff 	aprint_naive("\n");
    108  1.1  cliff 
    109  1.1  cliff 	/*
    110  1.1  cliff 	 * attach the rest of our devices
    111  1.1  cliff 	 */
    112  1.1  cliff 	config_search_ia(gemini_lpc_search, self, "lpc", NULL);
    113  1.1  cliff }
    114  1.1  cliff 
    115  1.1  cliff static int
    116  1.1  cliff gemini_lpc_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    117  1.1  cliff {
    118  1.1  cliff 	gemini_lpc_softc_t *sc = device_private(parent);
    119  1.1  cliff 	gemini_lpc_attach_args_t lpc;
    120  1.1  cliff 
    121  1.1  cliff 	lpc.lpc_ldn      = cf->cf_loc[LPCCF_LDN];
    122  1.1  cliff 	lpc.lpc_addr     = cf->cf_loc[LPCCF_ADDR];
    123  1.1  cliff 	lpc.lpc_size     = cf->cf_loc[LPCCF_SIZE];
    124  1.1  cliff 	lpc.lpc_intr     = cf->cf_loc[LPCCF_INTR];
    125  1.1  cliff 	lpc.lpc_iot      = sc->sc_iot;
    126  1.1  cliff 	lpc.lpc_tag      = sc;
    127  1.1  cliff 	lpc.lpc_base     = sc->sc_addr;
    128  1.1  cliff 	lpc.lpc_intrtag  = sc->sc_lpchctag;
    129  1.1  cliff 	lpc.lpc_bus_ops  = &gemini_lpc_bus_ops;
    130  1.1  cliff 
    131  1.1  cliff 	if (config_match(parent, cf, &lpc)) {
    132  1.1  cliff 		config_attach(parent, cf, &lpc, gemini_lpc_busprint);
    133  1.1  cliff 		return 0;			/* love it */
    134  1.1  cliff 	}
    135  1.1  cliff 
    136  1.1  cliff 	return UNCONF;				/* hate it */
    137  1.1  cliff }
    138  1.1  cliff 
    139  1.1  cliff static int
    140  1.1  cliff gemini_lpc_busprint(void *aux, const char *name)
    141  1.1  cliff {
    142  1.1  cliff         struct gemini_lpc_attach_args *lpc = aux;
    143  1.1  cliff 
    144  1.1  cliff 	if (lpc->lpc_ldn != LPCCF_LDN_DEFAULT)
    145  1.1  cliff 		aprint_normal(" ldn %d", lpc->lpc_ldn);
    146  1.1  cliff 	if (lpc->lpc_addr != LPCCF_ADDR_DEFAULT)
    147  1.1  cliff 		aprint_normal(" addr %#lx", lpc->lpc_addr);
    148  1.1  cliff 	if (lpc->lpc_size != LPCCF_SIZE_DEFAULT)
    149  1.1  cliff 		aprint_normal(" size %#lx", lpc->lpc_size);
    150  1.1  cliff 	if (lpc->lpc_intr != LPCCF_INTR_DEFAULT)
    151  1.1  cliff 		aprint_normal(" intr %d", lpc->lpc_intr);
    152  1.1  cliff 
    153  1.1  cliff 	return UNCONF;
    154  1.1  cliff }
    155  1.1  cliff 
    156  1.1  cliff /*
    157  1.1  cliff  * LPC bus ops
    158  1.1  cliff  */
    159  1.1  cliff 
    160  1.1  cliff static uint8_t
    161  1.1  cliff it8712_pnp_read(lpctag_t tag, int ldn, uint index)
    162  1.1  cliff {
    163  1.1  cliff 	gemini_lpc_softc_t *sc = tag;
    164  1.1  cliff 	bus_space_tag_t iot = sc->sc_iot;
    165  1.1  cliff 	bus_space_handle_t ioh = sc->sc_ioh;
    166  1.1  cliff 
    167  1.1  cliff 	if (ldn != GEMINI_LPC_LDN_ALL) {
    168  1.1  cliff 		bus_space_write_1(iot, ioh, IT8712_ADDR, IT8712_LDN);
    169  1.1  cliff 		bus_space_write_1(iot, ioh, IT8712_DATA, ldn);
    170  1.1  cliff 	}
    171  1.1  cliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, index);
    172  1.1  cliff 	return bus_space_read_1(iot, ioh, IT8712_DATA);
    173  1.1  cliff }
    174  1.1  cliff 
    175  1.1  cliff static void
    176  1.1  cliff it8712_pnp_write(lpctag_t tag, int ldn, uint index, uint8_t val)
    177  1.1  cliff {
    178  1.1  cliff 	gemini_lpc_softc_t *sc = tag;
    179  1.1  cliff 	bus_space_tag_t iot = sc->sc_iot;
    180  1.1  cliff 	bus_space_handle_t ioh = sc->sc_ioh;
    181  1.1  cliff 
    182  1.1  cliff 	if (ldn != GEMINI_LPC_LDN_ALL) {
    183  1.1  cliff 		bus_space_write_1(iot, ioh, IT8712_ADDR, IT8712_LDN);
    184  1.1  cliff 		bus_space_write_1(iot, ioh, IT8712_DATA, ldn);
    185  1.1  cliff 	}
    186  1.1  cliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, index);
    187  1.1  cliff 	bus_space_write_1(iot, ioh, IT8712_DATA, val);
    188  1.1  cliff }
    189  1.1  cliff 
    190  1.1  cliff static void
    191  1.1  cliff it8712_pnp_enter(lpctag_t tag)
    192  1.1  cliff {
    193  1.1  cliff 	gemini_lpc_softc_t *sc = tag;
    194  1.1  cliff 	bus_space_tag_t iot = sc->sc_iot;
    195  1.1  cliff 	bus_space_handle_t ioh = sc->sc_ioh;
    196  1.1  cliff 
    197  1.1  cliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, 0x87);
    198  1.1  cliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, 0x01);
    199  1.1  cliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, 0x55);
    200  1.1  cliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, 0x55);
    201  1.1  cliff }
    202  1.1  cliff 
    203  1.1  cliff static void
    204  1.1  cliff it8712_pnp_exit(lpctag_t tag)
    205  1.1  cliff {
    206  1.1  cliff 	gemini_lpc_softc_t *sc = tag;
    207  1.1  cliff 	bus_space_tag_t iot = sc->sc_iot;
    208  1.1  cliff 	bus_space_handle_t ioh = sc->sc_ioh;
    209  1.1  cliff 
    210  1.1  cliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, IT8712_CFGCTL);
    211  1.1  cliff 	bus_space_write_1(iot, ioh, IT8712_DATA, CFGCTL_WAITKEY);
    212  1.1  cliff }
    213