1 /* $NetBSD: s3c2440_rtc.c,v 1.3 2020/01/02 22:24:11 thorpej Exp $ */ 2 3 /*-- 4 * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Fleischer <paul@xpg.dk> 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 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/device.h> 36 #include <sys/bus.h> 37 #include <sys/time.h> 38 39 #include <dev/clock_subr.h> 40 41 #include <arm/s3c2xx0/s3c24x0var.h> 42 #include <arm/s3c2xx0/s3c2440var.h> 43 #include <arm/s3c2xx0/s3c2440reg.h> 44 45 #ifdef SSRTC_DEBUG 46 #define DPRINTF(s) do { printf s; } while (/*CONSTCOND*/0) 47 #else 48 #define DPRINTF(s) do {} while (/*CONSTCOND*/0) 49 #endif 50 51 /* As the RTC keeps track of Leap years, we need to use the right zero-year */ 52 #define SSRTC_YEAR_ZERO 2000 53 54 struct ssrtc_softc { 55 device_t sc_dev; 56 bus_space_tag_t sc_iot; 57 bus_space_handle_t sc_ioh; 58 struct todr_chip_handle sc_todr; 59 }; 60 61 static int ssrtc_match(device_t, cfdata_t, void *); 62 static void ssrtc_attach(device_t, device_t, void *); 63 64 CFATTACH_DECL_NEW(ssrtc, sizeof(struct ssrtc_softc), 65 ssrtc_match, ssrtc_attach, NULL, NULL); 66 67 static int ssrtc_todr_gettime_ymdhms(struct todr_chip_handle *, 68 struct clock_ymdhms *); 69 static int ssrtc_todr_settime_ymdhms(struct todr_chip_handle *, 70 struct clock_ymdhms *); 71 72 static int 73 ssrtc_match(device_t parent, cfdata_t cf, void *aux) 74 { 75 return 1; 76 } 77 78 static void 79 ssrtc_attach(device_t parent, device_t self, void *aux) 80 { 81 struct ssrtc_softc *sc = device_private(self); 82 struct s3c2xx0_attach_args *sa = aux; 83 84 sc->sc_dev = self; 85 sc->sc_iot = sa->sa_iot; 86 87 if (bus_space_map(sc->sc_iot, S3C2440_RTC_BASE, 88 S3C2440_RTC_SIZE, 0, &sc->sc_ioh)) { 89 aprint_error(": failed to map registers"); 90 return; 91 } 92 aprint_normal(": RTC \n"); 93 94 sc->sc_todr.cookie = sc; 95 sc->sc_todr.todr_gettime = NULL; 96 sc->sc_todr.todr_settime = NULL; 97 sc->sc_todr.todr_gettime_ymdhms = ssrtc_todr_gettime_ymdhms; 98 sc->sc_todr.todr_settime_ymdhms = ssrtc_todr_settime_ymdhms; 99 sc->sc_todr.todr_setwen = NULL; 100 101 todr_attach(&sc->sc_todr); 102 } 103 104 static int 105 ssrtc_todr_gettime_ymdhms(struct todr_chip_handle *h, struct clock_ymdhms *dt) 106 { 107 struct ssrtc_softc *sc = h->cookie; 108 uint8_t reg; 109 110 reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDSEC); 111 DPRINTF(("BCDSEC: %02X\n", reg)); 112 dt->dt_sec = bcdtobin(reg); 113 114 reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMIN); 115 DPRINTF(("BCDMIN: %02X\n", reg)); 116 dt->dt_min = bcdtobin(reg); 117 118 reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDHOUR); 119 DPRINTF(("BCDHOUR: %02X\n", reg)); 120 dt->dt_hour = bcdtobin(reg); 121 122 reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDATE); 123 DPRINTF(("BCDDATE: %02X\n", reg)); 124 dt->dt_day = bcdtobin(reg); 125 126 reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDAY); 127 DPRINTF(("BCDDAY: %02X\n", reg)); 128 dt->dt_wday = bcdtobin(reg); 129 130 reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMON); 131 DPRINTF(("BCDMON: %02X\n", reg)); 132 dt->dt_mon = bcdtobin(reg); 133 134 reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDYEAR); 135 DPRINTF(("BCDYEAR: %02X\n", reg)); 136 dt->dt_year = SSRTC_YEAR_ZERO + bcdtobin(reg); 137 138 DPRINTF(("Seconds: %d\n", dt->dt_sec)); 139 DPRINTF(("Minutes: %d\n", dt->dt_min)); 140 DPRINTF(("Hour: %d\n", dt->dt_hour)); 141 DPRINTF(("Mon: %d\n", dt->dt_mon)); 142 DPRINTF(("Date: %d\n", dt->dt_day)); 143 DPRINTF(("Day: %d\n", dt->dt_wday)); 144 DPRINTF(("Year: %d\n", dt->dt_year)); 145 146 return 0; 147 } 148 149 static int 150 ssrtc_todr_settime_ymdhms(struct todr_chip_handle *h, struct clock_ymdhms *dt) 151 { 152 struct ssrtc_softc *sc = h->cookie; 153 uint8_t reg; 154 155 DPRINTF(("ssrtc_todr_settime")); 156 157 /* Set RTCEN */ 158 reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON); 159 bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON, reg | RTCCON_RTCEN); 160 161 bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDSEC, bintobcd(dt->dt_sec)); 162 bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMIN, bintobcd(dt->dt_min)); 163 bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDHOUR, bintobcd(dt->dt_hour)); 164 bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDATE, bintobcd(dt->dt_day)); 165 bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDAY, bintobcd(dt->dt_wday)); 166 bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMON, bintobcd(dt->dt_mon)); 167 bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDYEAR, bintobcd(dt->dt_year-SSRTC_YEAR_ZERO)); 168 169 /* Clear RTCEN */ 170 reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON); 171 bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON, reg & ~RTCCON_RTCEN); 172 return 0; 173 } 174