1 /*	$OpenBSD: geodesc.c,v 1.3 2003/11/26 02:21:53 kevlo Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * Geode SC1100 Information Appliance On a Chip
21  * http://www.national.com/ds.cgi/SC/SC1100.pdf
22  */
23 
24 #include <sys/cdefs.h>
25 #include <sys/param.h>
26 #include <sys/systm.h>
27 #include <sys/device.h>
28 
29 #include <machine/bus.h>
30 
31 #include <dev/pci/pcivar.h>
32 #include <dev/pci/pcidevs.h>
33 
34 #include <arch/i386/pci/geodescreg.h>
35 
36 struct geodesc_softc {
37 	struct device		sc_dev;
38 	bus_space_tag_t		sc_iot;
39 	bus_space_handle_t	sc_ioh;
40 };
41 
42 int	geodesc_match(struct device *, void *, void *);
43 void	geodesc_attach(struct device *, struct device *, void *);
44 int	geodesc_wdogctl_cb(void *, int);
45 
46 struct cfattach geodesc_ca = {
47 	sizeof(struct geodesc_softc), geodesc_match, geodesc_attach
48 };
49 
50 struct cfdriver geodesc_cd = {
51 	NULL, "geodesc", DV_DULL
52 };
53 
54 int
geodesc_match(struct device * parent,void * match,void * aux)55 geodesc_match(struct device *parent, void *match, void *aux)
56 {
57 	struct pci_attach_args *pa = aux;
58 
59 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
60 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_SC1100_XBUS ||
61 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_SCx200_XBUS))
62 		return (1);
63 	return (0);
64 }
65 
66 #define WDSTSBITS "\20\x04WDRST\x03WDSMI\x02WDINT\x01WDOVF"
67 
68 void
geodesc_attach(struct device * parent,struct device * self,void * aux)69 geodesc_attach(struct device *parent, struct device *self, void *aux)
70 {
71 	struct geodesc_softc *sc = (void *) self;
72 	struct pci_attach_args *pa = aux;
73 	uint16_t cnfg, cba;
74 	uint8_t sts, rev, iid;
75 	pcireg_t reg;
76 
77 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, SC1100_F5_SCRATCHPAD);
78 	sc->sc_iot = pa->pa_iot;
79 	if (bus_space_map(sc->sc_iot, reg, 64, 0, &sc->sc_ioh)) {
80 		printf(": unable to map registers at 0x%x\n", reg);
81 		return;
82 	}
83 	cba = bus_space_read_2(sc->sc_iot, sc->sc_ioh, GCB_CBA);
84 	if (cba != reg) {
85 		printf(": cba mismatch: cba 0x%x != reg 0x%x\n", cba, reg);
86 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 64);
87 		return;
88 	}
89 	sts = bus_space_read_1(sc->sc_iot, sc->sc_ioh, GCB_WDSTS);
90 	cnfg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, GCB_WDCNFG);
91 	iid = bus_space_read_1(sc->sc_iot, sc->sc_ioh, GCB_IID);
92 	rev = bus_space_read_1(sc->sc_iot, sc->sc_ioh, GCB_REV);
93 
94 	printf(": iid %d revision %d wdstatus %b\n", iid, rev, sts, WDSTSBITS);
95 
96 	/* setup and register watchdog */
97 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, GCB_WDTO, 0);
98 	sts |= WDOVF_CLEAR;
99 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, GCB_WDSTS, sts);
100 	cnfg &= ~WDCNFG_MASK;;
101 	cnfg |= WDTYPE1_RESET|WDPRES_DIV_512;
102 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, GCB_WDCNFG, cnfg);
103 
104 	wdog_register(sc, geodesc_wdogctl_cb);
105 }
106 
107 int
geodesc_wdogctl_cb(void * self,int period)108 geodesc_wdogctl_cb(void *self, int period)
109 {
110 	struct geodesc_softc *sc = self;
111 
112 	if (period > 0x03ff)
113 		period = 0x03ff;
114 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, GCB_WDTO, period * 64);
115 	return (period);
116 }
117