Home | History | Annotate | Line # | Download | only in marvell
      1  1.5   thorpej /*	$NetBSD: mvsocrtc.c,v 1.5 2025/09/07 21:45:11 thorpej Exp $	*/
      2  1.1      matt 
      3  1.1      matt /*-
      4  1.1      matt  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1      matt  * All rights reserved.
      6  1.1      matt  *
      7  1.1      matt  * Redistribution and use in source and binary forms, with or without
      8  1.1      matt  * modification, are permitted provided that the following conditions
      9  1.1      matt  * are met:
     10  1.1      matt  * 1. Redistributions of source code must retain the above copyright
     11  1.1      matt  *    notice, this list of conditions and the following disclaimer.
     12  1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      matt  *    documentation and/or other materials provided with the distribution.
     15  1.1      matt  *
     16  1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1      matt  */
     28  1.1      matt 
     29  1.1      matt /*
     30  1.1      matt  * Driver for real time clock unit on Marvell kirkwood and possibly other
     31  1.1      matt  * SoCs.  Supports only the time/date keeping functions.  No support for
     32  1.1      matt  * the rtc unit alarm / interrupt functions.
     33  1.1      matt  *
     34  1.1      matt  * Written 10/2010 by Brett Slager -- all rights assigned to the NetBSD
     35  1.1      matt  * Foundation.
     36  1.1      matt  */
     37  1.1      matt 
     38  1.1      matt #include <sys/cdefs.h>
     39  1.5   thorpej __KERNEL_RCSID(0, "$NetBSD: mvsocrtc.c,v 1.5 2025/09/07 21:45:11 thorpej Exp $");
     40  1.1      matt 
     41  1.1      matt #include <sys/param.h>
     42  1.1      matt #include <sys/systm.h>
     43  1.1      matt #include <sys/device.h>
     44  1.1      matt #include <sys/kernel.h>
     45  1.1      matt 
     46  1.1      matt #include <dev/clock_subr.h>
     47  1.1      matt 
     48  1.2    dyoung #include <sys/bus.h>
     49  1.1      matt 
     50  1.1      matt #include <arm/marvell/mvsocrtcreg.h>
     51  1.1      matt #include <dev/marvell/marvellvar.h>
     52  1.1      matt 
     53  1.1      matt struct mvsocrtc_softc {
     54  1.1      matt 	device_t		sc_dev;
     55  1.1      matt 	bus_space_tag_t		sc_iot;
     56  1.1      matt 	bus_space_handle_t	sc_ioh;
     57  1.1      matt 	struct todr_chip_handle	sc_todr;
     58  1.1      matt };
     59  1.1      matt 
     60  1.1      matt static int mvsocrtc_match(device_t, cfdata_t, void *);
     61  1.1      matt static void mvsocrtc_attach(device_t, device_t, void *);
     62  1.1      matt static int mvsocrtc_todr_gettime(todr_chip_handle_t, struct clock_ymdhms *);
     63  1.1      matt static int mvsocrtc_todr_settime(todr_chip_handle_t, struct clock_ymdhms *);
     64  1.1      matt 
     65  1.1      matt CFATTACH_DECL_NEW(mvsocrtc, sizeof(struct mvsocrtc_softc), mvsocrtc_match,
     66  1.1      matt     mvsocrtc_attach, NULL, NULL);
     67  1.1      matt 
     68  1.1      matt static int
     69  1.1      matt mvsocrtc_match(device_t parent, cfdata_t cf, void *aux)
     70  1.1      matt {
     71  1.1      matt 	struct marvell_attach_args * const mva = aux;
     72  1.1      matt 
     73  1.1      matt 	if (strcmp(mva->mva_name, cf->cf_name) != 0)
     74  1.1      matt 		return 0;
     75  1.1      matt 	if (mva->mva_offset == MVA_OFFSET_DEFAULT)
     76  1.1      matt 		return 0;
     77  1.1      matt 
     78  1.1      matt 	mva->mva_size = MVSOCRTC_SIZE;
     79  1.1      matt 	return 1;
     80  1.1      matt }
     81  1.1      matt 
     82  1.1      matt static void
     83  1.1      matt mvsocrtc_attach(device_t parent, device_t self, void *aux)
     84  1.1      matt {
     85  1.1      matt 	struct mvsocrtc_softc * const sc = device_private(self);
     86  1.1      matt 	struct marvell_attach_args * const mva = aux;
     87  1.1      matt 
     88  1.1      matt 	sc->sc_dev = self;
     89  1.1      matt 	sc->sc_iot = mva->mva_iot;
     90  1.1      matt 
     91  1.1      matt 	aprint_normal(": Marvell SoC Real Time Clock\n");
     92  1.1      matt 	aprint_naive("\n");
     93  1.1      matt 
     94  1.1      matt 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
     95  1.1      matt 	   mva->mva_size, &sc->sc_ioh)) {
     96  1.1      matt 		aprint_error_dev(self, "failed to subregion register space\n");
     97  1.1      matt 		return;
     98  1.1      matt 	}
     99  1.1      matt 
    100  1.5   thorpej 	sc->sc_todr.todr_dev = self;
    101  1.1      matt 	sc->sc_todr.todr_gettime_ymdhms = mvsocrtc_todr_gettime;
    102  1.1      matt 	sc->sc_todr.todr_settime_ymdhms = mvsocrtc_todr_settime;
    103  1.1      matt 
    104  1.1      matt 	todr_attach(&sc->sc_todr);
    105  1.1      matt }
    106  1.1      matt 
    107  1.1      matt static int
    108  1.1      matt mvsocrtc_todr_gettime(todr_chip_handle_t ch, struct clock_ymdhms *dt)
    109  1.1      matt {
    110  1.5   thorpej 	struct mvsocrtc_softc * const sc = device_private(ch->todr_dev);
    111  1.1      matt 	bool tried = false;
    112  1.1      matt 	uint32_t rtcdate, rtctime;
    113  1.1      matt 	uint8_t rtcday, rtchour, rtcmin, rtcmonth, rtcsec, rtcyear;
    114  1.1      matt 
    115  1.1      matt again:
    116  1.1      matt 	/* read the rtc date and time registers */
    117  1.1      matt 	rtcdate = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSOCRTC_DATE);
    118  1.1      matt 	rtctime = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSOCRTC_TIME);
    119  1.1      matt 
    120  1.1      matt 	rtcyear = (rtcdate >> MVSOCRTC_YEAR_OFFSET) & MVSOCRTC_YEAR_MASK;
    121  1.1      matt 	rtcmonth = (rtcdate >> MVSOCRTC_MONTH_OFFSET) & MVSOCRTC_MONTH_MASK;
    122  1.1      matt 	rtcday = (rtcdate >> MVSOCRTC_DAY_OFFSET) & MVSOCRTC_DAY_MASK;
    123  1.1      matt 
    124  1.1      matt 	rtchour = (rtctime >> MVSOCRTC_HOUR_OFFSET) & MVSOCRTC_HOUR_MASK;
    125  1.1      matt 	rtcmin = (rtctime >> MVSOCRTC_MINUTE_OFFSET) & MVSOCRTC_MINUTE_MASK;
    126  1.1      matt 	rtcsec = (rtctime >> MVSOCRTC_SECOND_OFFSET) & MVSOCRTC_SECOND_MASK;
    127  1.1      matt 
    128  1.1      matt 	/*
    129  1.1      matt 	 * if seconds == 0, we may have read while the registers were
    130  1.1      matt 	 * updating.  Read again to get consistant data.
    131  1.1      matt 	 */
    132  1.1      matt 	if (rtcsec == 0 && !tried) {
    133  1.1      matt 		tried = true;
    134  1.1      matt 		goto again;
    135  1.1      matt 	}
    136  1.1      matt 
    137  1.1      matt 	/*
    138  1.1      matt 	 * Assume year "00" to be year 2000.
    139  1.1      matt 	 * XXXX this assumption will fail in 2100, but somehow I don't think
    140  1.1      matt 	 * I or the hardware will be functioning to see it.
    141  1.1      matt 	 */
    142  1.3  christos 	dt->dt_year = bcdtobin(rtcyear) + 2000;
    143  1.3  christos 	dt->dt_mon = bcdtobin(rtcmonth);
    144  1.3  christos 	dt->dt_day = bcdtobin(rtcday);
    145  1.3  christos 	dt->dt_hour = bcdtobin(rtchour);
    146  1.3  christos 	dt->dt_min = bcdtobin(rtcmin);
    147  1.3  christos 	dt->dt_sec = bcdtobin(rtcsec);
    148  1.1      matt 
    149  1.1      matt 	return 0;
    150  1.1      matt }
    151  1.1      matt 
    152  1.1      matt static int
    153  1.1      matt mvsocrtc_todr_settime(todr_chip_handle_t ch, struct clock_ymdhms *dt)
    154  1.1      matt {
    155  1.5   thorpej 	struct mvsocrtc_softc * const sc = device_private(ch->todr_dev);
    156  1.1      matt 	uint32_t reg;
    157  1.1      matt 
    158  1.1      matt 	/* compose & write time register contents */
    159  1.3  christos 	reg = (bintobcd(dt->dt_sec) << MVSOCRTC_SECOND_OFFSET) |
    160  1.3  christos 	   (bintobcd(dt->dt_min) << MVSOCRTC_MINUTE_OFFSET) |
    161  1.3  christos 	   (bintobcd(dt->dt_hour) << MVSOCRTC_HOUR_OFFSET) |
    162  1.3  christos 	   (bintobcd(dt->dt_wday) << MVSOCRTC_WDAY_OFFSET);
    163  1.1      matt 
    164  1.1      matt 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSOCRTC_TIME, reg);
    165  1.1      matt 
    166  1.1      matt 	/* compose & write date register contents */
    167  1.3  christos 	reg = (bintobcd(dt->dt_day) << MVSOCRTC_DAY_OFFSET) |
    168  1.3  christos 	   (bintobcd(dt->dt_mon) << MVSOCRTC_MONTH_OFFSET) |
    169  1.3  christos 	   (bintobcd(dt->dt_year % 100) << MVSOCRTC_YEAR_OFFSET);
    170  1.1      matt 
    171  1.1      matt 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSOCRTC_DATE, reg);
    172  1.1      matt 
    173  1.1      matt 	return 0;
    174  1.1      matt }
    175