xref: /netbsd-src/sys/arch/arm/gemini/gemini_lpc.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: gemini_lpc.c,v 1.6 2021/08/07 16:18:44 thorpej Exp $	*/
22842dccfScliff 
32842dccfScliff #include "opt_gemini.h"
42842dccfScliff #include "locators.h"
52842dccfScliff 
62842dccfScliff #include <sys/cdefs.h>
7*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: gemini_lpc.c,v 1.6 2021/08/07 16:18:44 thorpej Exp $");
82842dccfScliff 
92842dccfScliff #include <sys/param.h>
102842dccfScliff #include <sys/systm.h>
112842dccfScliff #include <sys/device.h>
122842dccfScliff #include <arch/arm/gemini/gemini_lpcvar.h>
132842dccfScliff #include <arch/arm/gemini/gemini_lpchcvar.h>
142842dccfScliff #include <arch/arm/gemini/gemini_reg.h>
152842dccfScliff 
162842dccfScliff 
172842dccfScliff /* XXX these should be in lpcreg.h or it8721reg.h */
182842dccfScliff #define IT8712_CFGCTL		0x02
192842dccfScliff # define CFGCTL_WAITKEY		__BIT(1)
202842dccfScliff #define IT8712_LDN		0x07
212842dccfScliff #define IT8712_CHIPID1		0x20
222842dccfScliff #define IT8712_CHIPID2		0x21
232842dccfScliff #define IT8712_CSCV		0x22
242842dccfScliff # define CSCV_VERSION		__BITS(3,0);
252842dccfScliff #define IT8712_CLKSEL		0x23
262842dccfScliff #define IT8712_SWSUSPEND	0x24
272842dccfScliff #define IT8712_ADDR		0x2e
282842dccfScliff #define IT8712_DATA		0x2f
292842dccfScliff 
30cbab9cadSchs static int  gemini_lpc_match(device_t, cfdata_t, void *);
31cbab9cadSchs static void gemini_lpc_attach(device_t, device_t, void *);
322842dccfScliff static int  gemini_lpc_search(device_t, cfdata_t, const int *, void *);
332842dccfScliff static int  gemini_lpc_busprint(void *, const char *);
342842dccfScliff 
352842dccfScliff static uint8_t it8712_pnp_read(lpctag_t, int, uint);
362842dccfScliff static void    it8712_pnp_write(lpctag_t, int, uint, uint8_t);
372842dccfScliff static void    it8712_pnp_enter(lpctag_t);
382842dccfScliff static void    it8712_pnp_exit(lpctag_t);
39e08eff94Scliff static void   *it8712_intr_establish(lpctag_t, uint, int, int, int (*)(void *), void *);
40e08eff94Scliff static void    it8712_intr_disestablish(lpctag_t, void *);
412842dccfScliff 
422842dccfScliff CFATTACH_DECL_NEW(lpc, sizeof(struct gemini_lpc_softc),
432842dccfScliff     gemini_lpc_match, gemini_lpc_attach, NULL, NULL);
442842dccfScliff 
45f5b85be6Scliff gemini_lpc_bus_ops_t gemini_lpc_bus_ops = {
462842dccfScliff 	it8712_pnp_read,
472842dccfScliff 	it8712_pnp_write,
482842dccfScliff 	it8712_pnp_enter,
492842dccfScliff 	it8712_pnp_exit,
50e08eff94Scliff 	it8712_intr_establish,
51e08eff94Scliff 	it8712_intr_disestablish,
522842dccfScliff };
532842dccfScliff 
542842dccfScliff 
552842dccfScliff static int
gemini_lpc_match(device_t parent,cfdata_t cf,void * aux)56cbab9cadSchs gemini_lpc_match(device_t parent, cfdata_t cf, void *aux)
572842dccfScliff {
582842dccfScliff 	struct gemini_lpc_attach_args *lpc = aux;
592842dccfScliff 
602842dccfScliff 	if (lpc->lpc_addr == LPCCF_ADDR_DEFAULT)
612842dccfScliff 		panic("lpc must have addr specified in config.");
622842dccfScliff 
632842dccfScliff 	return 1;
642842dccfScliff }
652842dccfScliff 
662842dccfScliff static void
gemini_lpc_attach(device_t parent,device_t self,void * aux)67cbab9cadSchs gemini_lpc_attach(device_t parent, device_t self, void *aux)
682842dccfScliff {
692842dccfScliff 	gemini_lpc_softc_t *sc = device_private(self);
702842dccfScliff 	struct gemini_lpchc_attach_args *lpchc = aux;
712842dccfScliff 	bus_space_tag_t iot;
722842dccfScliff 	bus_space_handle_t ioh;
732842dccfScliff 	uint8_t id1, id2, vers, clk, susp;
742842dccfScliff 
752842dccfScliff 	sc->sc_addr = lpchc->lpchc_addr;
762842dccfScliff #if 0
772842dccfScliff 	sc->sc_size = lpchc->lpchc_size;
782842dccfScliff #else
792842dccfScliff 	/*
802842dccfScliff 	 * we just map the configuration registers
812842dccfScliff 	 * child devices can map their own I/O
822842dccfScliff 	 */
832842dccfScliff 	sc->sc_size = 4096;
842842dccfScliff #endif
852842dccfScliff 
862842dccfScliff 	iot = lpchc->lpchc_iot;
872842dccfScliff 	if (bus_space_map(iot, sc->sc_addr, sc->sc_size, 0, &ioh))
882842dccfScliff 		panic("%s: Cannot map registers", device_xname(self));
892842dccfScliff 	sc->sc_iot = iot;
902842dccfScliff 	sc->sc_ioh = ioh;
912842dccfScliff 
922842dccfScliff 	it8712_pnp_enter(sc);
932842dccfScliff 	id1  = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CHIPID1);
942842dccfScliff 	id2  = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CHIPID2);
952842dccfScliff 	vers = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CSCV);
962842dccfScliff 	vers &= CSCV_VERSION;
972842dccfScliff 	clk  = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_CLKSEL);
982842dccfScliff 	susp = it8712_pnp_read(sc, GEMINI_LPC_LDN_ALL, IT8712_SWSUSPEND);
992842dccfScliff 	it8712_pnp_exit(sc);
1002842dccfScliff 
1012842dccfScliff 	aprint_normal("\n%s: chip ID %x%x, version %d",
1022842dccfScliff 		device_xname(self), id1, id2, vers);
1032842dccfScliff 	aprint_normal("\n%s: clksel %#x, susp %#x ",
1042842dccfScliff 		device_xname(self), clk, susp);
1052842dccfScliff 
1062842dccfScliff 	sc->sc_lpchctag = lpchc->lpchc_tag;
107e08eff94Scliff 	sc->sc_bus_ops  = &gemini_lpc_bus_ops;
1082842dccfScliff 
1092842dccfScliff 	aprint_normal("\n");
1102842dccfScliff 	aprint_naive("\n");
1112842dccfScliff 
1122842dccfScliff 	/*
1132842dccfScliff 	 * attach the rest of our devices
1142842dccfScliff 	 */
1152685996bSthorpej 	config_search(self, NULL,
116*c7fb772bSthorpej 	    CFARGS(.search = gemini_lpc_search));
1172842dccfScliff }
1182842dccfScliff 
1192842dccfScliff static int
gemini_lpc_search(device_t parent,cfdata_t cf,const int * ldesc,void * aux)1202842dccfScliff gemini_lpc_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
1212842dccfScliff {
1222842dccfScliff 	gemini_lpc_softc_t *sc = device_private(parent);
1232842dccfScliff 	gemini_lpc_attach_args_t lpc;
1242842dccfScliff 
1252842dccfScliff 	lpc.lpc_ldn      = cf->cf_loc[LPCCF_LDN];
1262842dccfScliff 	lpc.lpc_addr     = cf->cf_loc[LPCCF_ADDR];
1272842dccfScliff 	lpc.lpc_size     = cf->cf_loc[LPCCF_SIZE];
1282842dccfScliff 	lpc.lpc_intr     = cf->cf_loc[LPCCF_INTR];
1292842dccfScliff 	lpc.lpc_iot      = sc->sc_iot;
1302842dccfScliff 	lpc.lpc_tag      = sc;
1312842dccfScliff 	lpc.lpc_base     = sc->sc_addr;
1322842dccfScliff 
1332685996bSthorpej 	if (config_probe(parent, cf, &lpc)) {
134*c7fb772bSthorpej 		config_attach(parent, cf, &lpc, gemini_lpc_busprint, CFARGS_NONE);
1352842dccfScliff 		return 0;			/* love it */
1362842dccfScliff 	}
1372842dccfScliff 
1382842dccfScliff 	return UNCONF;				/* hate it */
1392842dccfScliff }
1402842dccfScliff 
1412842dccfScliff static int
gemini_lpc_busprint(void * aux,const char * name)1422842dccfScliff gemini_lpc_busprint(void *aux, const char *name)
1432842dccfScliff {
1442842dccfScliff         struct gemini_lpc_attach_args *lpc = aux;
1452842dccfScliff 
1462842dccfScliff 	if (lpc->lpc_ldn != LPCCF_LDN_DEFAULT)
1472842dccfScliff 		aprint_normal(" ldn %d", lpc->lpc_ldn);
1482842dccfScliff 	if (lpc->lpc_addr != LPCCF_ADDR_DEFAULT)
1492842dccfScliff 		aprint_normal(" addr %#lx", lpc->lpc_addr);
1502842dccfScliff 	if (lpc->lpc_size != LPCCF_SIZE_DEFAULT)
1512842dccfScliff 		aprint_normal(" size %#lx", lpc->lpc_size);
1522842dccfScliff 	if (lpc->lpc_intr != LPCCF_INTR_DEFAULT)
1532842dccfScliff 		aprint_normal(" intr %d", lpc->lpc_intr);
1542842dccfScliff 
1552842dccfScliff 	return UNCONF;
1562842dccfScliff }
1572842dccfScliff 
1582842dccfScliff /*
1592842dccfScliff  * LPC bus ops
1602842dccfScliff  */
1612842dccfScliff 
1622842dccfScliff static uint8_t
it8712_pnp_read(lpctag_t tag,int ldn,uint index)1632842dccfScliff it8712_pnp_read(lpctag_t tag, int ldn, uint index)
1642842dccfScliff {
1652842dccfScliff 	gemini_lpc_softc_t *sc = tag;
1662842dccfScliff 	bus_space_tag_t iot = sc->sc_iot;
1672842dccfScliff 	bus_space_handle_t ioh = sc->sc_ioh;
1682842dccfScliff 
1692842dccfScliff 	if (ldn != GEMINI_LPC_LDN_ALL) {
1702842dccfScliff 		bus_space_write_1(iot, ioh, IT8712_ADDR, IT8712_LDN);
1712842dccfScliff 		bus_space_write_1(iot, ioh, IT8712_DATA, ldn);
1722842dccfScliff 	}
1732842dccfScliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, index);
1742842dccfScliff 	return bus_space_read_1(iot, ioh, IT8712_DATA);
1752842dccfScliff }
1762842dccfScliff 
1772842dccfScliff static void
it8712_pnp_write(lpctag_t tag,int ldn,uint index,uint8_t val)1782842dccfScliff it8712_pnp_write(lpctag_t tag, int ldn, uint index, uint8_t val)
1792842dccfScliff {
1802842dccfScliff 	gemini_lpc_softc_t *sc = tag;
1812842dccfScliff 	bus_space_tag_t iot = sc->sc_iot;
1822842dccfScliff 	bus_space_handle_t ioh = sc->sc_ioh;
1832842dccfScliff 
1842842dccfScliff 	if (ldn != GEMINI_LPC_LDN_ALL) {
1852842dccfScliff 		bus_space_write_1(iot, ioh, IT8712_ADDR, IT8712_LDN);
1862842dccfScliff 		bus_space_write_1(iot, ioh, IT8712_DATA, ldn);
1872842dccfScliff 	}
1882842dccfScliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, index);
1892842dccfScliff 	bus_space_write_1(iot, ioh, IT8712_DATA, val);
1902842dccfScliff }
1912842dccfScliff 
1922842dccfScliff static void
it8712_pnp_enter(lpctag_t tag)1932842dccfScliff it8712_pnp_enter(lpctag_t tag)
1942842dccfScliff {
1952842dccfScliff 	gemini_lpc_softc_t *sc = tag;
1962842dccfScliff 	bus_space_tag_t iot = sc->sc_iot;
1972842dccfScliff 	bus_space_handle_t ioh = sc->sc_ioh;
1982842dccfScliff 
1992842dccfScliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, 0x87);
2002842dccfScliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, 0x01);
2012842dccfScliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, 0x55);
2022842dccfScliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, 0x55);
2032842dccfScliff }
2042842dccfScliff 
2052842dccfScliff static void
it8712_pnp_exit(lpctag_t tag)2062842dccfScliff it8712_pnp_exit(lpctag_t tag)
2072842dccfScliff {
2082842dccfScliff 	gemini_lpc_softc_t *sc = tag;
2092842dccfScliff 	bus_space_tag_t iot = sc->sc_iot;
2102842dccfScliff 	bus_space_handle_t ioh = sc->sc_ioh;
2112842dccfScliff 
2122842dccfScliff 	bus_space_write_1(iot, ioh, IT8712_ADDR, IT8712_CFGCTL);
2132842dccfScliff 	bus_space_write_1(iot, ioh, IT8712_DATA, CFGCTL_WAITKEY);
2142842dccfScliff }
215e08eff94Scliff 
216e08eff94Scliff static void *
it8712_intr_establish(lpctag_t tag,uint intr,int ipl,int ist,int (* func)(void *),void * arg)217e08eff94Scliff it8712_intr_establish(lpctag_t tag, uint intr, int ipl, int ist,
218e08eff94Scliff 	int (*func)(void *), void *arg)
219e08eff94Scliff {
220e08eff94Scliff 	gemini_lpc_softc_t *sc = tag;
221e08eff94Scliff 	void *ih;
222e08eff94Scliff 
223e08eff94Scliff 	ih = gemini_lpchc_intr_establish(sc->sc_lpchctag, intr, ipl, ist, func, arg);
224e08eff94Scliff 
225e08eff94Scliff 	return ih;
226e08eff94Scliff }
227e08eff94Scliff 
228e08eff94Scliff static void
it8712_intr_disestablish(lpctag_t tag,void * ih)229e08eff94Scliff it8712_intr_disestablish(lpctag_t tag, void *ih)
230e08eff94Scliff {
231e08eff94Scliff 	gemini_lpc_softc_t *sc = tag;
232e08eff94Scliff 
233e08eff94Scliff 	gemini_lpchc_intr_disestablish(sc->sc_lpchctag, ih);
234e08eff94Scliff }
235