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