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