1 /* $NetBSD: g2rtc.c,v 1.4 2009/12/12 14:44:09 tsutsui Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: g2rtc.c,v 1.4 2009/12/12 14:44:09 tsutsui Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/device.h> 35 36 #include <dev/clock_subr.h> 37 38 #include <machine/bus.h> 39 #include <dreamcast/dev/g2/g2busvar.h> 40 41 42 #define G2RTC_REG_BASE 0x00710000 43 #define G2RTC_REG_SIZE 12 44 45 /* Offset by 20 years, 5 of them are leap */ 46 #define G2RTC_OFFSET (20 * SECYR + 5 * SECDAY) 47 48 struct g2rtc_softc { 49 struct device sc_dev; 50 51 bus_space_tag_t sc_bt; 52 bus_space_handle_t sc_bh; 53 }; 54 55 /* autoconf glue */ 56 static int g2rtc_match(struct device *, struct cfdata *, void *); 57 static void g2rtc_attach(struct device *, struct device *, void *); 58 59 CFATTACH_DECL(g2rtc, sizeof(struct g2rtc_softc), 60 g2rtc_match, g2rtc_attach, NULL, NULL); 61 62 63 /* todr(9) methods */ 64 static int g2rtc_todr_gettime(todr_chip_handle_t, struct timeval *); 65 static int g2rtc_todr_settime(todr_chip_handle_t, struct timeval *); 66 67 static struct todr_chip_handle g2rtc_todr_handle = { 68 .cookie = NULL, /* set on attach */ 69 .todr_gettime = g2rtc_todr_gettime, 70 .todr_settime = g2rtc_todr_settime, 71 }; 72 73 74 static inline uint32_t g2rtc_read(bus_space_tag_t, bus_space_handle_t); 75 76 77 static int 78 g2rtc_match(struct device *parent, struct cfdata *cf, void *aux) 79 { 80 static int g2rtc_matched = 0; 81 82 if (g2rtc_matched) 83 return 0; 84 85 g2rtc_matched = 1; 86 return 1; 87 } 88 89 90 static void 91 g2rtc_attach(struct device *parent, struct device *self, void *aux) 92 { 93 struct g2rtc_softc *sc = (void *)self; 94 struct g2bus_attach_args *ga = aux; 95 96 sc->sc_bt = ga->ga_memt; 97 if (bus_space_map(sc->sc_bt, G2RTC_REG_BASE, G2RTC_REG_SIZE, 0, 98 &sc->sc_bh) != 0) 99 { 100 printf(": unable to map registers\n"); 101 return; 102 } 103 printf(": time-of-day clock\n"); 104 105 g2rtc_todr_handle.cookie = sc; 106 todr_attach(&g2rtc_todr_handle); 107 } 108 109 110 static inline uint32_t 111 g2rtc_read(bus_space_tag_t bt, bus_space_handle_t bh) 112 { 113 114 return ((bus_space_read_4(bt, bh, 0) & 0xffff) << 16) 115 | (bus_space_read_4(bt, bh, 4) & 0xffff); 116 } 117 118 119 /* 120 * Get time-of-day and convert to `struct timeval'. 121 * Return 0 on success; an error number otherwise. 122 */ 123 static int 124 g2rtc_todr_gettime(todr_chip_handle_t handle, struct timeval *tv) 125 { 126 struct g2rtc_softc *sc = handle->cookie; 127 uint32_t new, old; 128 int i; 129 130 for (old = 0;;) { 131 for (i = 0; i < 3; i++) { 132 new = g2rtc_read(sc->sc_bt, sc->sc_bh); 133 if (new != old) 134 break; 135 } 136 if (i < 3) 137 old = new; 138 else 139 break; 140 } 141 142 tv->tv_sec = new - G2RTC_OFFSET; 143 tv->tv_usec = 0; 144 145 return 0; 146 } 147 148 149 /* 150 * Set the time-of-day clock based on the value of the `struct timeval' arg. 151 * Return 0 on success; an error number otherwise. 152 */ 153 static int 154 g2rtc_todr_settime(todr_chip_handle_t handle, struct timeval *tv) 155 { 156 struct g2rtc_softc *sc = handle->cookie; 157 uint32_t secs; 158 int i, retry; 159 160 secs = (uint32_t)tv->tv_sec + G2RTC_OFFSET; 161 162 for (retry = 0; retry < 5; retry++) { 163 164 /* Don't change the order */ 165 bus_space_write_4(sc->sc_bt, sc->sc_bh, 8, 1); 166 bus_space_write_4(sc->sc_bt, sc->sc_bh, 4, secs & 0xffff); 167 bus_space_write_4(sc->sc_bt, sc->sc_bh, 0, secs >> 16); 168 169 /* verify */ 170 for (i = 0; i < 3; i++) 171 if (g2rtc_read(sc->sc_bt, sc->sc_bh) == secs) 172 return 0; /* ok */ 173 } 174 175 return EIO; 176 177 } 178