1 /* $NetBSD: mkclock.c,v 1.7 2006/09/15 16:37:19 gdamore Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Wayne Knowles 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.7 2006/09/15 16:37:19 gdamore Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/kernel.h> 44 #include <sys/device.h> 45 #include <sys/systm.h> 46 47 #include <dev/clock_subr.h> 48 49 #include <machine/cpu.h> 50 #include <machine/mainboard.h> 51 #include <machine/autoconf.h> 52 #include <machine/sysconf.h> 53 #include <machine/bus.h> 54 55 #include <mipsco/obio/clockreg.h> 56 57 struct mkclock_softc { 58 struct device dev; 59 bus_space_tag_t sc_bst; 60 bus_space_handle_t sc_bsh; 61 struct todr_chip_handle sc_todr; 62 }; 63 64 static int mkclock_match (struct device *, struct cfdata *, void *); 65 static void mkclock_attach (struct device *, struct device *, void *); 66 67 CFATTACH_DECL(mkclock, sizeof(struct mkclock_softc), 68 mkclock_match, mkclock_attach, NULL, NULL); 69 70 int mkclock_read (todr_chip_handle_t, struct clock_ymdhms *); 71 int mkclock_write (todr_chip_handle_t, struct clock_ymdhms *); 72 73 static int mk_read (struct mkclock_softc *, int); 74 static void mk_write (struct mkclock_softc *, int, int); 75 76 int 77 mkclock_match(parent, cf, aux) 78 struct device *parent; 79 struct cfdata *cf; 80 void *aux; 81 { 82 return 1; 83 } 84 85 void 86 mkclock_attach(parent, self, aux) 87 struct device *parent, *self; 88 void *aux; 89 { 90 struct mkclock_softc *sc = (void *)self; 91 struct confargs *ca = aux; 92 93 sc->sc_bst = ca->ca_bustag; 94 if (bus_space_map(ca->ca_bustag, ca->ca_addr, 95 0x10000, 96 BUS_SPACE_MAP_LINEAR, 97 &sc->sc_bsh) != 0) { 98 printf(": cannot map registers\n"); 99 return; 100 } 101 102 sc->sc_todr.todr_settime_ymdhms = mkclock_write; 103 sc->sc_todr.todr_gettime_ymdhms = mkclock_read; 104 sc->sc_todr.cookie = sc; 105 todr_attach(&sc->sc_todr); 106 107 printf("\n"); 108 } 109 110 static int 111 mk_read(sc, reg) 112 struct mkclock_softc *sc; 113 int reg; 114 { 115 u_int8_t val; 116 117 val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4); 118 return FROMBCD(val); 119 } 120 121 static void 122 mk_write(sc, reg, val) 123 struct mkclock_softc *sc; 124 int reg, val; 125 { 126 bus_space_write_1(sc->sc_bst, sc->sc_bsh, 127 DATA_PORT + reg*4, TOBCD(val)); 128 } 129 130 int 131 mkclock_read(todr_chip_handle_t tch, struct clock_ymdhms *dt) 132 { 133 struct mkclock_softc *sc = tch->cookie; 134 int s = splclock(); 135 136 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK); 137 dt->dt_sec = mk_read(sc, 0); 138 dt->dt_min = mk_read(sc, 1); 139 dt->dt_hour = mk_read(sc, 2); 140 dt->dt_wday = mk_read(sc, 3); 141 dt->dt_day = mk_read(sc, 4); 142 dt->dt_mon = mk_read(sc, 5); 143 dt->dt_year = mk_read(sc, 6); 144 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0); 145 splx(s); 146 147 dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000); 148 return 0; 149 } 150 151 int 152 mkclock_write(todr_chip_handle_t tch, struct clock_ymdhms *dt) 153 { 154 struct mkclock_softc *sc = tch->cookie; 155 int year, s; 156 157 year = dt->dt_year % 100; 158 159 s = splclock(); 160 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK); 161 mk_write(sc, 0, dt->dt_sec); 162 mk_write(sc, 1, dt->dt_min); 163 mk_write(sc, 2, dt->dt_hour); 164 /* week day not set */ 165 mk_write(sc, 4, dt->dt_day); 166 mk_write(sc, 5, dt->dt_mon); 167 mk_write(sc, 6, year); 168 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0); 169 splx(s); 170 return 0; 171 } 172