Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2440_rtc.c revision 1.2.20.1
      1  1.2.20.1    martin /* $NetBSD: s3c2440_rtc.c,v 1.2.20.1 2020/04/08 14:07:30 martin Exp $ */
      2  1.2.20.1    martin 
      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.2.20.1    martin 
     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.2.20.1    martin static int ssrtc_todr_gettime_ymdhms(struct todr_chip_handle *,
     68  1.2.20.1    martin 				     struct clock_ymdhms *);
     69  1.2.20.1    martin static int ssrtc_todr_settime_ymdhms(struct todr_chip_handle *,
     70  1.2.20.1    martin 				     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.1  nisimura 	sc->sc_todr.cookie = sc;
     95  1.2.20.1    martin 	sc->sc_todr.todr_gettime = NULL;
     96  1.2.20.1    martin 	sc->sc_todr.todr_settime = NULL;
     97  1.2.20.1    martin 	sc->sc_todr.todr_gettime_ymdhms = ssrtc_todr_gettime_ymdhms;
     98  1.2.20.1    martin 	sc->sc_todr.todr_settime_ymdhms = ssrtc_todr_settime_ymdhms;
     99       1.1  nisimura 	sc->sc_todr.todr_setwen = NULL;
    100       1.1  nisimura 
    101       1.1  nisimura 	todr_attach(&sc->sc_todr);
    102       1.1  nisimura }
    103       1.1  nisimura 
    104       1.1  nisimura static int
    105  1.2.20.1    martin ssrtc_todr_gettime_ymdhms(struct todr_chip_handle *h, struct clock_ymdhms *dt)
    106       1.1  nisimura {
    107       1.1  nisimura 	struct ssrtc_softc *sc = h->cookie;
    108       1.1  nisimura 	uint8_t reg;
    109       1.1  nisimura 
    110       1.1  nisimura 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDSEC);
    111       1.1  nisimura 	DPRINTF(("BCDSEC: %02X\n", reg));
    112  1.2.20.1    martin 	dt->dt_sec = bcdtobin(reg);
    113       1.1  nisimura 
    114       1.1  nisimura 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMIN);
    115       1.1  nisimura 	DPRINTF(("BCDMIN: %02X\n", reg));
    116  1.2.20.1    martin 	dt->dt_min = bcdtobin(reg);
    117       1.1  nisimura 
    118       1.1  nisimura 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDHOUR);
    119       1.1  nisimura 	DPRINTF(("BCDHOUR: %02X\n", reg));
    120  1.2.20.1    martin 	dt->dt_hour = bcdtobin(reg);
    121       1.1  nisimura 
    122       1.1  nisimura 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDATE);
    123       1.1  nisimura 	DPRINTF(("BCDDATE: %02X\n", reg));
    124  1.2.20.1    martin 	dt->dt_day = bcdtobin(reg);
    125       1.1  nisimura 
    126       1.1  nisimura 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDAY);
    127       1.1  nisimura 	DPRINTF(("BCDDAY: %02X\n", reg));
    128  1.2.20.1    martin 	dt->dt_wday = bcdtobin(reg);
    129       1.1  nisimura 
    130       1.1  nisimura 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMON);
    131       1.1  nisimura 	DPRINTF(("BCDMON: %02X\n", reg));
    132  1.2.20.1    martin 	dt->dt_mon = bcdtobin(reg);
    133       1.1  nisimura 
    134       1.1  nisimura 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_BCDYEAR);
    135       1.1  nisimura 	DPRINTF(("BCDYEAR: %02X\n", reg));
    136  1.2.20.1    martin 	dt->dt_year = SSRTC_YEAR_ZERO + bcdtobin(reg);
    137       1.1  nisimura 
    138  1.2.20.1    martin 	DPRINTF(("Seconds: %d\n", dt->dt_sec));
    139  1.2.20.1    martin 	DPRINTF(("Minutes: %d\n", dt->dt_min));
    140  1.2.20.1    martin 	DPRINTF(("Hour: %d\n", dt->dt_hour));
    141  1.2.20.1    martin 	DPRINTF(("Mon: %d\n", dt->dt_mon));
    142  1.2.20.1    martin 	DPRINTF(("Date: %d\n", dt->dt_day));
    143  1.2.20.1    martin 	DPRINTF(("Day: %d\n", dt->dt_wday));
    144  1.2.20.1    martin 	DPRINTF(("Year: %d\n", dt->dt_year));
    145       1.1  nisimura 
    146       1.1  nisimura 	return 0;
    147       1.1  nisimura }
    148       1.1  nisimura 
    149       1.1  nisimura static int
    150  1.2.20.1    martin ssrtc_todr_settime_ymdhms(struct todr_chip_handle *h, struct clock_ymdhms *dt)
    151       1.1  nisimura {
    152       1.1  nisimura 	struct ssrtc_softc *sc = h->cookie;
    153       1.1  nisimura 	uint8_t reg;
    154       1.1  nisimura 
    155       1.1  nisimura 	DPRINTF(("ssrtc_todr_settime"));
    156       1.1  nisimura 
    157       1.1  nisimura 	/* Set RTCEN */
    158       1.1  nisimura 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON);
    159       1.1  nisimura 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON, reg | RTCCON_RTCEN);
    160       1.1  nisimura 
    161  1.2.20.1    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDSEC, bintobcd(dt->dt_sec));
    162  1.2.20.1    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMIN, bintobcd(dt->dt_min));
    163  1.2.20.1    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDHOUR, bintobcd(dt->dt_hour));
    164  1.2.20.1    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDATE, bintobcd(dt->dt_day));
    165  1.2.20.1    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDDAY, bintobcd(dt->dt_wday));
    166  1.2.20.1    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDMON, bintobcd(dt->dt_mon));
    167  1.2.20.1    martin 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_BCDYEAR, bintobcd(dt->dt_year-SSRTC_YEAR_ZERO));
    168       1.1  nisimura 
    169       1.1  nisimura 	/* Clear RTCEN */
    170       1.1  nisimura 	reg = bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON);
    171       1.1  nisimura 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_RTCCON, reg & ~RTCCON_RTCEN);
    172       1.1  nisimura 	return 0;
    173       1.1  nisimura }
    174