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