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