1 /* $NetBSD: mkclock.c,v 1.10 2009/03/14 21:04:12 dsl 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.10 2009/03/14 21:04:12 dsl Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/device.h> 38 #include <sys/systm.h> 39 40 #include <dev/clock_subr.h> 41 42 #include <machine/cpu.h> 43 #include <machine/mainboard.h> 44 #include <machine/autoconf.h> 45 #include <machine/sysconf.h> 46 #include <machine/bus.h> 47 48 #include <mipsco/obio/clockreg.h> 49 50 struct mkclock_softc { 51 struct device dev; 52 bus_space_tag_t sc_bst; 53 bus_space_handle_t sc_bsh; 54 struct todr_chip_handle sc_todr; 55 }; 56 57 static int mkclock_match (struct device *, struct cfdata *, void *); 58 static void mkclock_attach (struct device *, struct device *, void *); 59 60 CFATTACH_DECL(mkclock, sizeof(struct mkclock_softc), 61 mkclock_match, mkclock_attach, NULL, NULL); 62 63 int mkclock_read (todr_chip_handle_t, struct clock_ymdhms *); 64 int mkclock_write (todr_chip_handle_t, struct clock_ymdhms *); 65 66 static int mk_read (struct mkclock_softc *, int); 67 static void mk_write (struct mkclock_softc *, int, int); 68 69 int 70 mkclock_match(struct device *parent, struct cfdata *cf, void *aux) 71 { 72 return 1; 73 } 74 75 void 76 mkclock_attach(struct device *parent, struct device *self, void *aux) 77 { 78 struct mkclock_softc *sc = (void *)self; 79 struct confargs *ca = aux; 80 81 sc->sc_bst = ca->ca_bustag; 82 if (bus_space_map(ca->ca_bustag, ca->ca_addr, 83 0x10000, 84 BUS_SPACE_MAP_LINEAR, 85 &sc->sc_bsh) != 0) { 86 printf(": cannot map registers\n"); 87 return; 88 } 89 90 sc->sc_todr.todr_settime_ymdhms = mkclock_write; 91 sc->sc_todr.todr_gettime_ymdhms = mkclock_read; 92 sc->sc_todr.cookie = sc; 93 todr_attach(&sc->sc_todr); 94 95 printf("\n"); 96 } 97 98 static int 99 mk_read(struct mkclock_softc *sc, int reg) 100 { 101 u_int8_t val; 102 103 val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4); 104 return FROMBCD(val); 105 } 106 107 static void 108 mk_write(struct mkclock_softc *sc, int reg, int val) 109 { 110 bus_space_write_1(sc->sc_bst, sc->sc_bsh, 111 DATA_PORT + reg*4, TOBCD(val)); 112 } 113 114 int 115 mkclock_read(todr_chip_handle_t tch, struct clock_ymdhms *dt) 116 { 117 struct mkclock_softc *sc = tch->cookie; 118 int s = splclock(); 119 120 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK); 121 dt->dt_sec = mk_read(sc, 0); 122 dt->dt_min = mk_read(sc, 1); 123 dt->dt_hour = mk_read(sc, 2); 124 dt->dt_wday = mk_read(sc, 3); 125 dt->dt_day = mk_read(sc, 4); 126 dt->dt_mon = mk_read(sc, 5); 127 dt->dt_year = mk_read(sc, 6); 128 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0); 129 splx(s); 130 131 dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000); 132 return 0; 133 } 134 135 int 136 mkclock_write(todr_chip_handle_t tch, struct clock_ymdhms *dt) 137 { 138 struct mkclock_softc *sc = tch->cookie; 139 int year, s; 140 141 year = dt->dt_year % 100; 142 143 s = splclock(); 144 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK); 145 mk_write(sc, 0, dt->dt_sec); 146 mk_write(sc, 1, dt->dt_min); 147 mk_write(sc, 2, dt->dt_hour); 148 /* week day not set */ 149 mk_write(sc, 4, dt->dt_day); 150 mk_write(sc, 5, dt->dt_mon); 151 mk_write(sc, 6, year); 152 bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0); 153 splx(s); 154 return 0; 155 } 156