xref: /openbsd-src/sys/arch/i386/pci/geodesc.c (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /*	$OpenBSD: geodesc.c,v 1.15 2020/07/06 13:33:07 pirofti 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/param.h>
25 #include <sys/systm.h>
26 #include <sys/device.h>
27 #include <sys/timetc.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_activate(struct device *, int);
45 void	sc1100_sysreset(void);
46 
47 #ifndef SMALL_KERNEL
48 int	geodesc_wdogctl_cb(void *, int);
49 #endif /* SMALL_KERNEL */
50 
51 struct cfattach geodesc_ca = {
52 	sizeof(struct geodesc_softc), geodesc_match, geodesc_attach,
53 	NULL, geodesc_activate
54 };
55 
56 struct cfdriver geodesc_cd = {
57 	NULL, "geodesc", DV_DULL
58 };
59 
60 u_int   geodesc_get_timecount(struct timecounter *tc);
61 
62 struct timecounter geodesc_timecounter = {
63 	geodesc_get_timecount,	/* get_timecount */
64 	0,			/* no poll_pps */
65 	0xffffffff,		/* counter_mask */
66 	27000000,		/* frequency */
67 	"GEOTSC",		/* name */
68 	2000,			/* quality */
69 	NULL,			/* private bits */
70 	0			/* expose to user */
71 };
72 
73 int
74 geodesc_match(struct device *parent, void *match, void *aux)
75 {
76 	struct pci_attach_args *pa = aux;
77 
78 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
79 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_SC1100_XBUS ||
80 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_SCX200_XBUS))
81 		return (1);
82 	return (0);
83 }
84 
85 #define WDSTSBITS "\20\x04WDRST\x03WDSMI\x02WDINT\x01WDOVF"
86 
87 void
88 geodesc_attach(struct device *parent, struct device *self, void *aux)
89 {
90 	struct geodesc_softc *sc = (void *) self;
91 	struct pci_attach_args *pa = aux;
92 	uint16_t cnfg, cba;
93 	uint8_t sts, rev, iid;
94 	pcireg_t reg;
95 	extern void (*cpuresetfn)(void);
96 
97 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, SC1100_F5_SCRATCHPAD);
98 	sc->sc_iot = pa->pa_iot;
99 	if (reg == 0 ||
100 	    bus_space_map(sc->sc_iot, reg, 64, 0, &sc->sc_ioh)) {
101 		printf(": unable to map registers at 0x%x\n", reg);
102 		return;
103 	}
104 	cba = bus_space_read_2(sc->sc_iot, sc->sc_ioh, GCB_CBA);
105 	if (cba != reg) {
106 		printf(": cba mismatch: cba 0x%x != reg 0x%x\n", cba, reg);
107 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, 64);
108 		return;
109 	}
110 	sts = bus_space_read_1(sc->sc_iot, sc->sc_ioh, GCB_WDSTS);
111 	cnfg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, GCB_WDCNFG);
112 	iid = bus_space_read_1(sc->sc_iot, sc->sc_ioh, GCB_IID);
113 	rev = bus_space_read_1(sc->sc_iot, sc->sc_ioh, GCB_REV);
114 
115 	printf(": iid %d revision %d wdstatus %b\n", iid, rev, sts, WDSTSBITS);
116 
117 #ifndef SMALL_KERNEL
118 	/* setup and register watchdog */
119 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, GCB_WDTO, 0);
120 	sts |= WDOVF_CLEAR;
121 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, GCB_WDSTS, sts);
122 	cnfg &= ~WDCNFG_MASK;
123 	cnfg |= WDTYPE1_RESET|WDPRES_DIV_512;
124 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, GCB_WDCNFG, cnfg);
125 
126 	wdog_register(geodesc_wdogctl_cb, sc);
127 #endif /* SMALL_KERNEL */
128 
129 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, GCB_TSCNFG, TSC_ENABLE);
130 	/* Hook into the kern_tc */
131 	geodesc_timecounter.tc_priv = sc;
132 	tc_init(&geodesc_timecounter);
133 
134 	/* We have a special way to reset the CPU on the SC1100 */
135 	cpuresetfn = sc1100_sysreset;
136 }
137 
138 int
139 geodesc_activate(struct device *self, int act)
140 {
141 	switch (act) {
142 	case DVACT_POWERDOWN:
143 #ifndef SMALL_KERNEL
144 		wdog_shutdown(self);
145 #endif
146 		break;
147 	}
148 
149 	return (0);
150 }
151 
152 #ifndef SMALL_KERNEL
153 int
154 geodesc_wdogctl_cb(void *self, int period)
155 {
156 	struct geodesc_softc *sc = self;
157 
158 	if (period > 0x03ff)
159 		period = 0x03ff;
160 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, GCB_WDTO, period * 64);
161 	return (period);
162 }
163 #endif /* SMALL_KERNEL */
164 
165 u_int
166 geodesc_get_timecount(struct timecounter *tc)
167 {
168 	struct geodesc_softc *sc = tc->tc_priv;
169 
170 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, GCB_TSC));
171 }
172 
173 void
174 sc1100_sysreset(void)
175 {
176 	/*
177 	 * Reset AMD Geode SC1100.
178 	 *
179 	 * 1) Write PCI Configuration Address Register (0xcf8) to
180 	 *    select Function 0, Register 0x44: Bridge Configuration,
181 	 *    GPIO and LPC Configuration Register Space, Reset
182 	 *    Control Register.
183 	 *
184 	 * 2) Write 0xf to PCI Configuration Data Register (0xcfc)
185 	 *    to reset IDE controller, IDE bus, and PCI bus, and
186 	 *    to trigger a system-wide reset.
187 	 *
188 	 * See AMD Geode SC1100 Processor Data Book, Revision 2.0,
189 	 * sections 6.3.1, 6.3.2, and 6.4.1.
190 	 */
191 	outl(0xCF8, 0x80009044UL);
192 	outb(0xCFC, 0x0F);
193 }
194