1 /* $NetBSD: mcclock_mace.c,v 1.4 2005/12/11 12:18:54 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Antti Kantee. All Rights Reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the NetBSD 17 * Foundation, Inc. and its contributors. 18 * 4. The name of the company nor the name of the author may be used to 19 * endorse or promote products derived from this software without specific 20 * prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS 23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1998 Mark Brinicombe. 37 * Copyright (c) 1998 Causality Limited. 38 * All rights reserved. 39 * 40 * Written by Mark Brinicombe, Causality Limited 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by Mark Brinicombe 53 * for the NetBSD Project. 54 * 4. The name of the company nor the name of the author may be used to 55 * endorse or promote products derived from this software without specific 56 * prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS 59 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 60 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 61 * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 64 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #include <sys/cdefs.h> 72 __KERNEL_RCSID(0, "$NetBSD: mcclock_mace.c,v 1.4 2005/12/11 12:18:54 christos Exp $"); 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 77 #include <machine/cpu.h> 78 #include <machine/autoconf.h> 79 #include <machine/bus.h> 80 #include <machine/machtype.h> 81 82 #include <dev/clock_subr.h> 83 #include <dev/ic/ds1687reg.h> 84 85 #include <sgimips/mace/macevar.h> 86 87 #include <sgimips/sgimips/clockvar.h> 88 89 struct mcclock_mace_softc { 90 struct device sc_dev; 91 92 bus_space_tag_t sc_st; 93 bus_space_handle_t sc_sh; 94 }; 95 96 static int mcclock_mace_match(struct device *, struct cfdata *, void *); 97 static void mcclock_mace_attach(struct device*, struct device *, void *); 98 99 static void mcclock_mace_init(struct device *); 100 static void mcclock_mace_get(struct device *, struct clock_ymdhms *); 101 static void mcclock_mace_set(struct device *, struct clock_ymdhms *); 102 103 unsigned int ds1687_read(void *arg, unsigned int addr); 104 void ds1687_write(void *arg, unsigned int addr, unsigned int data); 105 106 const struct clockfns mcclock_mace_clockfns = { 107 mcclock_mace_init, mcclock_mace_get, mcclock_mace_set 108 }; 109 110 CFATTACH_DECL(mcclock_mace, sizeof(struct mcclock_mace_softc), 111 mcclock_mace_match, mcclock_mace_attach, NULL, NULL); 112 113 static int 114 mcclock_mace_match(struct device *parent, struct cfdata *match, void *aux) 115 { 116 return 1; 117 } 118 119 void 120 mcclock_mace_attach(struct device *parent, struct device *self, void *aux) 121 { 122 struct mcclock_mace_softc *sc = (void *)self; 123 struct mace_attach_args *maa = aux; 124 125 sc->sc_st = maa->maa_st; 126 /* XXX should be bus_space_map() */ 127 if (bus_space_subregion(maa->maa_st, maa->maa_sh, 128 maa->maa_offset, 0, &sc->sc_sh)) 129 panic("mcclock_mace_attach: couldn't map"); 130 131 /* 132 * We want a fixed format: 24-hour, BCD data, so just force the 133 * RTC to this mode; if there was junk there we'll be able to 134 * fix things up later when we discover the time read back is 135 * no good. 136 */ 137 ds1687_write(sc, DS1687_CONTROLA, DS1687_DV1 | DS1687_BANK1); 138 ds1687_write(sc, DS1687_CONTROLB, DS1687_24HRS); 139 140 /* XXXrkb: init kickstart/wakeup stuff */ 141 142 if (!(ds1687_read(sc, DS1687_CONTROLD) & DS1687_VRT)) 143 printf(": lithium cell is dead, RTC unreliable"); 144 145 printf("\n"); 146 clockattach(&sc->sc_dev, &mcclock_mace_clockfns); 147 } 148 149 /* 150 * We need to use the following two functions from 151 * DS1687_PUT/GETTOD(). Hence the names. 152 */ 153 unsigned int 154 ds1687_read(void *arg, unsigned int addr) 155 { 156 struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg; 157 158 return (bus_space_read_1(sc->sc_st, sc->sc_sh, addr)); 159 } 160 161 void 162 ds1687_write(void *arg, unsigned int addr, unsigned int data) 163 { 164 struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)arg; 165 166 bus_space_write_1(sc->sc_st, sc->sc_sh, addr, data); 167 } 168 169 static void 170 mcclock_mace_init(struct device *dev) 171 { 172 173 return; 174 } 175 176 static void 177 mcclock_mace_get(struct device *dev, struct clock_ymdhms *dt) 178 { 179 struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev; 180 ds1687_todregs regs; 181 int s; 182 183 s = splhigh(); 184 DS1687_GETTOD(sc, ®s); 185 splx(s); 186 187 dt->dt_sec = FROMBCD(regs[DS1687_SOFT_SEC]); 188 dt->dt_min = FROMBCD(regs[DS1687_SOFT_MIN]); 189 dt->dt_hour = FROMBCD(regs[DS1687_SOFT_HOUR]); 190 dt->dt_wday = FROMBCD(regs[DS1687_SOFT_DOW]); 191 dt->dt_day = FROMBCD(regs[DS1687_SOFT_DOM]); 192 dt->dt_mon = FROMBCD(regs[DS1687_SOFT_MONTH]); 193 dt->dt_year = FROMBCD(regs[DS1687_SOFT_YEAR]) + 194 (100 * FROMBCD(regs[DS1687_SOFT_CENTURY])); 195 } 196 197 static void 198 mcclock_mace_set(struct device *dev, struct clock_ymdhms *dt) 199 { 200 struct mcclock_mace_softc *sc = (struct mcclock_mace_softc *)dev; 201 ds1687_todregs regs; 202 int s; 203 204 memset(®s, 0, sizeof(regs)); 205 206 regs[DS1687_SOFT_SEC] = TOBCD(dt->dt_sec); 207 regs[DS1687_SOFT_MIN] = TOBCD(dt->dt_min); 208 regs[DS1687_SOFT_HOUR] = TOBCD(dt->dt_hour); 209 regs[DS1687_SOFT_DOW] = TOBCD(dt->dt_wday); 210 regs[DS1687_SOFT_DOM] = TOBCD(dt->dt_day); 211 regs[DS1687_SOFT_MONTH] = TOBCD(dt->dt_mon); 212 regs[DS1687_SOFT_YEAR] = TOBCD(dt->dt_year % 100); 213 regs[DS1687_SOFT_CENTURY] = TOBCD(dt->dt_year / 100); 214 s = splhigh(); 215 DS1687_PUTTOD(sc, ®s); 216 splx(s); 217 } 218