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