1 1.5 thorpej /* $NetBSD: s3c2440_rtc.c,v 1.5 2025/09/07 21:45:12 thorpej Exp $ */ 2 1.3 thorpej 3 1.1 nisimura /*-- 4 1.1 nisimura * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 1.1 nisimura * All rights reserved. 6 1.1 nisimura * 7 1.1 nisimura * This code is derived from software contributed to The NetBSD Foundation 8 1.1 nisimura * by Paul Fleischer <paul (at) xpg.dk> 9 1.1 nisimura * 10 1.1 nisimura * Redistribution and use in source and binary forms, with or without 11 1.1 nisimura * modification, are permitted provided that the following conditions 12 1.1 nisimura * are met: 13 1.1 nisimura * 1. Redistributions of source code must retain the above copyright 14 1.1 nisimura * notice, this list of conditions and the following disclaimer. 15 1.1 nisimura * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 nisimura * notice, this list of conditions and the following disclaimer in the 17 1.1 nisimura * documentation and/or other materials provided with the distribution. 18 1.1 nisimura * 19 1.1 nisimura * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 nisimura * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 nisimura * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 nisimura * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 nisimura * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 nisimura * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 nisimura * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 nisimura * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 nisimura * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 nisimura * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 nisimura * POSSIBILITY OF SUCH DAMAGE. 30 1.1 nisimura */ 31 1.3 thorpej 32 1.1 nisimura #include <sys/cdefs.h> 33 1.1 nisimura #include <sys/types.h> 34 1.1 nisimura #include <sys/param.h> 35 1.1 nisimura #include <sys/device.h> 36 1.1 nisimura #include <sys/bus.h> 37 1.1 nisimura #include <sys/time.h> 38 1.1 nisimura 39 1.1 nisimura #include <dev/clock_subr.h> 40 1.1 nisimura 41 1.1 nisimura #include <arm/s3c2xx0/s3c24x0var.h> 42 1.1 nisimura #include <arm/s3c2xx0/s3c2440var.h> 43 1.1 nisimura #include <arm/s3c2xx0/s3c2440reg.h> 44 1.1 nisimura 45 1.1 nisimura #ifdef SSRTC_DEBUG 46 1.1 nisimura #define DPRINTF(s) do { printf s; } while (/*CONSTCOND*/0) 47 1.1 nisimura #else 48 1.1 nisimura #define DPRINTF(s) do {} while (/*CONSTCOND*/0) 49 1.1 nisimura #endif 50 1.1 nisimura 51 1.1 nisimura /* As the RTC keeps track of Leap years, we need to use the right zero-year */ 52 1.1 nisimura #define SSRTC_YEAR_ZERO 2000 53 1.1 nisimura 54 1.1 nisimura struct ssrtc_softc { 55 1.1 nisimura device_t sc_dev; 56 1.1 nisimura bus_space_tag_t sc_iot; 57 1.1 nisimura bus_space_handle_t sc_ioh; 58 1.1 nisimura struct todr_chip_handle sc_todr; 59 1.1 nisimura }; 60 1.1 nisimura 61 1.1 nisimura static int ssrtc_match(device_t, cfdata_t, void *); 62 1.1 nisimura static void ssrtc_attach(device_t, device_t, void *); 63 1.1 nisimura 64 1.1 nisimura CFATTACH_DECL_NEW(ssrtc, sizeof(struct ssrtc_softc), 65 1.1 nisimura ssrtc_match, ssrtc_attach, NULL, NULL); 66 1.1 nisimura 67 1.3 thorpej static int ssrtc_todr_gettime_ymdhms(struct todr_chip_handle *, 68 1.3 thorpej struct clock_ymdhms *); 69 1.3 thorpej static int ssrtc_todr_settime_ymdhms(struct todr_chip_handle *, 70 1.3 thorpej struct clock_ymdhms *); 71 1.1 nisimura 72 1.1 nisimura static int 73 1.1 nisimura ssrtc_match(device_t parent, cfdata_t cf, void *aux) 74 1.1 nisimura { 75 1.1 nisimura return 1; 76 1.1 nisimura } 77 1.1 nisimura 78 1.1 nisimura static void 79 1.1 nisimura ssrtc_attach(device_t parent, device_t self, void *aux) 80 1.1 nisimura { 81 1.1 nisimura struct ssrtc_softc *sc = device_private(self); 82 1.1 nisimura struct s3c2xx0_attach_args *sa = aux; 83 1.1 nisimura 84 1.1 nisimura sc->sc_dev = self; 85 1.1 nisimura sc->sc_iot = sa->sa_iot; 86 1.1 nisimura 87 1.1 nisimura if (bus_space_map(sc->sc_iot, S3C2440_RTC_BASE, 88 1.1 nisimura S3C2440_RTC_SIZE, 0, &sc->sc_ioh)) { 89 1.1 nisimura aprint_error(": failed to map registers"); 90 1.1 nisimura return; 91 1.1 nisimura } 92 1.1 nisimura aprint_normal(": RTC \n"); 93 1.1 nisimura 94 1.5 thorpej sc->sc_todr.todr_dev = self; 95 1.3 thorpej sc->sc_todr.todr_gettime_ymdhms = ssrtc_todr_gettime_ymdhms; 96 1.3 thorpej sc->sc_todr.todr_settime_ymdhms = ssrtc_todr_settime_ymdhms; 97 1.1 nisimura 98 1.1 nisimura todr_attach(&sc->sc_todr); 99 1.1 nisimura } 100 1.1 nisimura 101 1.1 nisimura static int 102 1.3 thorpej ssrtc_todr_gettime_ymdhms(struct todr_chip_handle *h, struct clock_ymdhms *dt) 103 1.1 nisimura { 104 1.5 thorpej struct ssrtc_softc *sc = device_private(h->todr_dev); 105 1.1 nisimura uint8_t reg; 106 1.1 nisimura 107 1.1 nisimura reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDSEC); 108 1.1 nisimura DPRINTF(("BCDSEC: %02X\n", reg)); 109 1.3 thorpej dt->dt_sec = bcdtobin(reg); 110 1.1 nisimura 111 1.1 nisimura reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMIN); 112 1.1 nisimura DPRINTF(("BCDMIN: %02X\n", reg)); 113 1.3 thorpej dt->dt_min = bcdtobin(reg); 114 1.1 nisimura 115 1.1 nisimura reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDHOUR); 116 1.1 nisimura DPRINTF(("BCDHOUR: %02X\n", reg)); 117 1.3 thorpej dt->dt_hour = bcdtobin(reg); 118 1.1 nisimura 119 1.1 nisimura reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDATE); 120 1.1 nisimura DPRINTF(("BCDDATE: %02X\n", reg)); 121 1.3 thorpej dt->dt_day = bcdtobin(reg); 122 1.1 nisimura 123 1.1 nisimura reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDAY); 124 1.1 nisimura DPRINTF(("BCDDAY: %02X\n", reg)); 125 1.3 thorpej dt->dt_wday = bcdtobin(reg); 126 1.1 nisimura 127 1.1 nisimura reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMON); 128 1.1 nisimura DPRINTF(("BCDMON: %02X\n", reg)); 129 1.3 thorpej dt->dt_mon = bcdtobin(reg); 130 1.1 nisimura 131 1.1 nisimura reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDYEAR); 132 1.1 nisimura DPRINTF(("BCDYEAR: %02X\n", reg)); 133 1.3 thorpej dt->dt_year = SSRTC_YEAR_ZERO + bcdtobin(reg); 134 1.1 nisimura 135 1.3 thorpej DPRINTF(("Seconds: %d\n", dt->dt_sec)); 136 1.3 thorpej DPRINTF(("Minutes: %d\n", dt->dt_min)); 137 1.3 thorpej DPRINTF(("Hour: %d\n", dt->dt_hour)); 138 1.3 thorpej DPRINTF(("Mon: %d\n", dt->dt_mon)); 139 1.3 thorpej DPRINTF(("Date: %d\n", dt->dt_day)); 140 1.3 thorpej DPRINTF(("Day: %d\n", dt->dt_wday)); 141 1.3 thorpej DPRINTF(("Year: %d\n", dt->dt_year)); 142 1.1 nisimura 143 1.1 nisimura return 0; 144 1.1 nisimura } 145 1.1 nisimura 146 1.1 nisimura static int 147 1.3 thorpej ssrtc_todr_settime_ymdhms(struct todr_chip_handle *h, struct clock_ymdhms *dt) 148 1.1 nisimura { 149 1.5 thorpej struct ssrtc_softc *sc = device_private(h->todr_dev); 150 1.1 nisimura uint8_t reg; 151 1.1 nisimura 152 1.1 nisimura DPRINTF(("ssrtc_todr_settime")); 153 1.1 nisimura 154 1.1 nisimura /* Set RTCEN */ 155 1.1 nisimura reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON); 156 1.1 nisimura bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON, reg | RTCCON_RTCEN); 157 1.1 nisimura 158 1.3 thorpej bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDSEC, bintobcd(dt->dt_sec)); 159 1.3 thorpej bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMIN, bintobcd(dt->dt_min)); 160 1.3 thorpej bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDHOUR, bintobcd(dt->dt_hour)); 161 1.3 thorpej bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDATE, bintobcd(dt->dt_day)); 162 1.3 thorpej bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDAY, bintobcd(dt->dt_wday)); 163 1.3 thorpej bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMON, bintobcd(dt->dt_mon)); 164 1.3 thorpej bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDYEAR, bintobcd(dt->dt_year-SSRTC_YEAR_ZERO)); 165 1.1 nisimura 166 1.1 nisimura /* Clear RTCEN */ 167 1.1 nisimura reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON); 168 1.1 nisimura bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON, reg & ~RTCCON_RTCEN); 169 1.1 nisimura return 0; 170 1.1 nisimura } 171