Home | History | Annotate | Line # | Download | only in amlogic
meson_rtc.c revision 1.2.16.1
      1  1.2.16.1   thorpej /* $NetBSD: meson_rtc.c,v 1.2.16.1 2021/04/03 22:28:16 thorpej Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*-
      4       1.1  jmcneill  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5       1.1  jmcneill  * All rights reserved.
      6       1.1  jmcneill  *
      7       1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8       1.1  jmcneill  * modification, are permitted provided that the following conditions
      9       1.1  jmcneill  * are met:
     10       1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12       1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15       1.1  jmcneill  *
     16       1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1  jmcneill  */
     28       1.1  jmcneill 
     29       1.1  jmcneill #include <sys/cdefs.h>
     30  1.2.16.1   thorpej __KERNEL_RCSID(0, "$NetBSD: meson_rtc.c,v 1.2.16.1 2021/04/03 22:28:16 thorpej Exp $");
     31       1.1  jmcneill 
     32       1.1  jmcneill #include <sys/param.h>
     33       1.1  jmcneill #include <sys/atomic.h>
     34       1.1  jmcneill #include <sys/device.h>
     35       1.1  jmcneill #include <sys/kernel.h>
     36       1.1  jmcneill #include <sys/systm.h>
     37       1.1  jmcneill 
     38       1.1  jmcneill #include <dev/clock_subr.h>
     39       1.1  jmcneill 
     40       1.1  jmcneill #include <arm/amlogic/meson_rtcreg.h>
     41       1.1  jmcneill 
     42       1.1  jmcneill #include <dev/fdt/fdtvar.h>
     43       1.1  jmcneill 
     44       1.1  jmcneill #define RESET_RETRY_TIMES	3
     45       1.1  jmcneill #define RTC_COMM_DELAY		5
     46       1.1  jmcneill #define RTC_RESET_DELAY		100
     47       1.1  jmcneill #define RTC_STATIC_VALUE_INIT	0x180a	/* XXX: MAGIC? */
     48       1.1  jmcneill 
     49       1.1  jmcneill struct meson_rtc_softc {
     50       1.1  jmcneill 	device_t		sc_dev;
     51       1.1  jmcneill 	bus_space_tag_t		sc_bst;
     52       1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     53       1.1  jmcneill 	struct todr_chip_handle	sc_todr;
     54       1.1  jmcneill 	int			sc_osc_failed;
     55       1.1  jmcneill 	unsigned int		sc_busy;
     56       1.1  jmcneill };
     57       1.1  jmcneill 
     58  1.2.16.1   thorpej static const struct device_compatible_entry compat_data[] = {
     59  1.2.16.1   thorpej 	{ .compat = "amlogic,meson8b-rtc" },
     60  1.2.16.1   thorpej 	DEVICE_COMPAT_EOL
     61       1.1  jmcneill };
     62       1.1  jmcneill 
     63       1.1  jmcneill static int meson_rtc_match(device_t, cfdata_t, void *);
     64       1.1  jmcneill static void meson_rtc_attach(device_t, device_t, void *);
     65       1.1  jmcneill static int meson_rtc_todr_gettime(todr_chip_handle_t, struct timeval *);
     66       1.1  jmcneill static int meson_rtc_todr_settime(todr_chip_handle_t, struct timeval *);
     67       1.1  jmcneill 
     68       1.1  jmcneill CFATTACH_DECL_NEW(meson_rtc, sizeof(struct meson_rtc_softc),
     69       1.1  jmcneill 	meson_rtc_match, meson_rtc_attach, NULL, NULL);
     70       1.1  jmcneill 
     71       1.1  jmcneill #define RTC_WRITE(sc, reg, val) \
     72       1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     73       1.1  jmcneill #define RTC_READ(sc, reg) \
     74       1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     75       1.1  jmcneill 
     76       1.1  jmcneill static inline void
     77       1.1  jmcneill setbits(struct meson_rtc_softc *sc, uint32_t reg, uint32_t bits)
     78       1.1  jmcneill {
     79       1.1  jmcneill 
     80       1.1  jmcneill 	RTC_WRITE(sc, reg, RTC_READ(sc, reg) | bits);
     81       1.1  jmcneill }
     82       1.1  jmcneill 
     83       1.1  jmcneill static inline void
     84       1.1  jmcneill clrbits(struct meson_rtc_softc *sc, uint32_t reg, uint32_t bits)
     85       1.1  jmcneill {
     86       1.1  jmcneill 
     87       1.1  jmcneill 	RTC_WRITE(sc, reg, RTC_READ(sc, reg) & ~bits);
     88       1.1  jmcneill }
     89       1.1  jmcneill 
     90       1.1  jmcneill static int
     91       1.1  jmcneill meson_rtc_check_osc_clk(struct meson_rtc_softc *sc)
     92       1.1  jmcneill {
     93       1.1  jmcneill 	uint32_t cnt1, cnt2;
     94       1.1  jmcneill 
     95       1.1  jmcneill 	setbits(sc, AO_RTC_REG3, AO_RTC_REG3_COUNT_ALWAYS);
     96       1.1  jmcneill 
     97       1.1  jmcneill 	/*
     98       1.1  jmcneill 	 * Wait for 50uS.  32.768khz is 30.5uS.  This should be long
     99       1.1  jmcneill 	 * enough for one full cycle of 32.768 khz.
    100       1.1  jmcneill 	 */
    101       1.1  jmcneill 	cnt1 = RTC_READ(sc, AO_RTC_REG2);
    102       1.1  jmcneill 	delay(50);
    103       1.1  jmcneill 	cnt2 = RTC_READ(sc, AO_RTC_REG2);
    104       1.1  jmcneill 
    105       1.1  jmcneill 	clrbits(sc, AO_RTC_REG3, AO_RTC_REG3_COUNT_ALWAYS);
    106       1.1  jmcneill 
    107       1.1  jmcneill 	return cnt1 == cnt2;
    108       1.1  jmcneill }
    109       1.1  jmcneill 
    110       1.1  jmcneill static int
    111       1.1  jmcneill meson_rtc_match(device_t parent, cfdata_t cf, void *aux)
    112       1.1  jmcneill {
    113       1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    114       1.1  jmcneill 
    115  1.2.16.1   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    116       1.1  jmcneill }
    117       1.1  jmcneill 
    118       1.1  jmcneill static void
    119       1.1  jmcneill meson_rtc_attach(device_t parent, device_t self, void *aux)
    120       1.1  jmcneill {
    121       1.1  jmcneill 	struct meson_rtc_softc * const sc = device_private(self);
    122       1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    123       1.1  jmcneill 	const int phandle = faa->faa_phandle;
    124       1.1  jmcneill 	bus_addr_t addr;
    125       1.1  jmcneill 	bus_size_t size;
    126       1.1  jmcneill 
    127       1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    128       1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    129       1.1  jmcneill 		return;
    130       1.1  jmcneill 	}
    131       1.1  jmcneill 
    132       1.1  jmcneill 	sc->sc_dev = self;
    133       1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    134       1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    135       1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    136       1.1  jmcneill 		return;
    137       1.1  jmcneill 	}
    138       1.1  jmcneill 
    139       1.1  jmcneill 	sc->sc_osc_failed = meson_rtc_check_osc_clk(sc);
    140       1.1  jmcneill 
    141       1.1  jmcneill 	memset(&sc->sc_todr, 0, sizeof(sc->sc_todr));
    142       1.1  jmcneill 	sc->sc_todr.cookie = sc;
    143       1.1  jmcneill 	sc->sc_todr.todr_gettime = meson_rtc_todr_gettime;
    144       1.1  jmcneill 	sc->sc_todr.todr_settime = meson_rtc_todr_settime;
    145       1.1  jmcneill 
    146       1.1  jmcneill 	aprint_naive("\n");
    147       1.1  jmcneill 	aprint_normal(": RTC");
    148       1.1  jmcneill 	if (sc->sc_osc_failed) {
    149       1.1  jmcneill 		aprint_normal(" battery not present or discharged\n");
    150       1.1  jmcneill 	} else {
    151       1.1  jmcneill 		aprint_normal("\n");
    152       1.2  jmcneill 		fdtbus_todr_attach(self, phandle, &sc->sc_todr);
    153       1.1  jmcneill 	}
    154       1.1  jmcneill }
    155       1.1  jmcneill 
    156       1.1  jmcneill static void
    157       1.1  jmcneill meson_rtc_sclk_pulse(struct meson_rtc_softc *sc)
    158       1.1  jmcneill {
    159       1.1  jmcneill 
    160       1.1  jmcneill 	delay(RTC_COMM_DELAY);
    161       1.1  jmcneill 	setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SCLK);
    162       1.1  jmcneill 
    163       1.1  jmcneill 	delay(RTC_COMM_DELAY);
    164       1.1  jmcneill 	clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SCLK);
    165       1.1  jmcneill }
    166       1.1  jmcneill 
    167       1.1  jmcneill static void
    168       1.1  jmcneill meson_rtc_send_bit(struct meson_rtc_softc *sc, uint32_t bitset)
    169       1.1  jmcneill {
    170       1.1  jmcneill 
    171       1.1  jmcneill 	if (bitset)
    172       1.1  jmcneill 		setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
    173       1.1  jmcneill 	else
    174       1.1  jmcneill 		clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
    175       1.1  jmcneill 
    176       1.1  jmcneill 	meson_rtc_sclk_pulse(sc);
    177       1.1  jmcneill }
    178       1.1  jmcneill 
    179       1.1  jmcneill #define SERIAL_ADDR_BITS	3
    180       1.1  jmcneill #define SERIAL_DATA_BITS	32
    181       1.1  jmcneill #define	SERIAL_TYPE_ADDR	(1 << (SERIAL_ADDR_BITS - 1))
    182       1.1  jmcneill #define	SERIAL_TYPE_DATA	(1 << (SERIAL_DATA_BITS - 1))
    183       1.1  jmcneill 
    184       1.1  jmcneill static void
    185       1.1  jmcneill meson_rtc_send_data(struct meson_rtc_softc *sc,
    186       1.1  jmcneill     uint32_t nextbit, uint32_t data)
    187       1.1  jmcneill {
    188       1.1  jmcneill 
    189       1.1  jmcneill 	KASSERT(nextbit == SERIAL_TYPE_ADDR || nextbit == SERIAL_TYPE_DATA);
    190       1.1  jmcneill 
    191       1.1  jmcneill 	while (nextbit) {
    192       1.1  jmcneill 		meson_rtc_send_bit(sc, data & nextbit);
    193       1.1  jmcneill 		nextbit >>= 1;
    194       1.1  jmcneill 	}
    195       1.1  jmcneill }
    196       1.1  jmcneill 
    197       1.1  jmcneill static uint32_t
    198       1.1  jmcneill meson_rtc_get_data(struct meson_rtc_softc *sc)
    199       1.1  jmcneill {
    200       1.1  jmcneill 	uint32_t data;
    201       1.1  jmcneill 	size_t i;
    202       1.1  jmcneill 
    203       1.1  jmcneill 	data = 0;
    204       1.1  jmcneill 	for (i = 0; i < SERIAL_DATA_BITS; i++) {
    205       1.1  jmcneill 		meson_rtc_sclk_pulse(sc);
    206       1.1  jmcneill 		data <<= 1;
    207       1.1  jmcneill 		data |= __SHIFTOUT(RTC_READ(sc, AO_RTC_REG1), AO_RTC_REG1_SDO);
    208       1.1  jmcneill 	}
    209       1.1  jmcneill 	return data;
    210       1.1  jmcneill }
    211       1.1  jmcneill 
    212       1.1  jmcneill enum serial_mode {
    213       1.1  jmcneill 	SERIAL_MODE_READ,
    214       1.1  jmcneill 	SERIAL_MODE_WRITE,
    215       1.1  jmcneill };
    216       1.1  jmcneill 
    217       1.1  jmcneill static void
    218       1.1  jmcneill meson_rtc_set_mode(struct meson_rtc_softc *sc, enum serial_mode mode)
    219       1.1  jmcneill {
    220       1.1  jmcneill 
    221       1.1  jmcneill 	clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SEN);
    222       1.1  jmcneill 
    223       1.1  jmcneill 	switch(mode) {
    224       1.1  jmcneill 	case SERIAL_MODE_READ:
    225       1.1  jmcneill 		clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
    226       1.1  jmcneill 		break;
    227       1.1  jmcneill 	case SERIAL_MODE_WRITE:
    228       1.1  jmcneill 		setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
    229       1.1  jmcneill 		break;
    230       1.1  jmcneill 	default:
    231       1.1  jmcneill 		KASSERT(1);
    232       1.1  jmcneill 		return;
    233       1.1  jmcneill 	}
    234       1.1  jmcneill 	meson_rtc_sclk_pulse(sc);
    235       1.1  jmcneill 
    236       1.1  jmcneill 	clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
    237       1.1  jmcneill }
    238       1.1  jmcneill 
    239       1.1  jmcneill static int
    240       1.1  jmcneill meson_rtc_wait_s_ready(struct meson_rtc_softc *sc)
    241       1.1  jmcneill {
    242       1.1  jmcneill 	size_t s_nrdy_cnt, retry_cnt;
    243       1.1  jmcneill 
    244       1.1  jmcneill 	s_nrdy_cnt = 40000;
    245       1.1  jmcneill 	retry_cnt = 0;
    246       1.1  jmcneill 	while (!(RTC_READ(sc, AO_RTC_REG1) & AO_RTC_REG1_S_READY)) {
    247       1.1  jmcneill 		if (s_nrdy_cnt-- == 0) {
    248       1.1  jmcneill 			s_nrdy_cnt = 40000;
    249       1.1  jmcneill 			if (retry_cnt++ == RESET_RETRY_TIMES)
    250       1.1  jmcneill 				return 0;
    251       1.1  jmcneill 			/* XXX: reset_s_ready?  Linux does not. */
    252       1.1  jmcneill 			setbits(sc, AO_RTC_REG1, AO_RTC_REG1_S_READY);
    253       1.1  jmcneill 			delay(RTC_RESET_DELAY);
    254       1.1  jmcneill 		}
    255       1.1  jmcneill 	}
    256       1.1  jmcneill 	return 1;
    257       1.1  jmcneill }
    258       1.1  jmcneill 
    259       1.1  jmcneill static int
    260       1.1  jmcneill meson_rtc_comm_init(struct meson_rtc_softc *sc)
    261       1.1  jmcneill {
    262       1.1  jmcneill 
    263       1.1  jmcneill 	clrbits(sc, AO_RTC_REG0,
    264       1.1  jmcneill 	    AO_RTC_REG0_SEN | AO_RTC_REG0_SCLK | AO_RTC_REG0_SDI);
    265       1.1  jmcneill 
    266       1.1  jmcneill 	if (meson_rtc_wait_s_ready(sc)) {
    267       1.1  jmcneill 		setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SEN);
    268       1.1  jmcneill 		return 0;
    269       1.1  jmcneill 	}
    270       1.1  jmcneill 	return -1;
    271       1.1  jmcneill }
    272       1.1  jmcneill 
    273       1.1  jmcneill static void
    274       1.1  jmcneill meson_rtc_static_register_write(struct meson_rtc_softc *sc, uint32_t data)
    275       1.1  jmcneill {
    276       1.1  jmcneill 	uint32_t u;
    277       1.1  jmcneill 
    278       1.1  jmcneill 	/* Program MSB 15-8 */
    279       1.1  jmcneill 	u = RTC_READ(sc, AO_RTC_REG4);
    280       1.1  jmcneill 	u &= AO_RTC_REG4_STATIC_REG_MSB;
    281       1.1  jmcneill 	u |= __SHIFTIN(data, AO_RTC_REG4_STATIC_REG_MSB);
    282       1.1  jmcneill 	RTC_WRITE(sc, AO_RTC_REG4, u);
    283       1.1  jmcneill 
    284       1.1  jmcneill 	/* Program LSB 7-0, and start serializing */
    285       1.1  jmcneill 	u = RTC_READ(sc, AO_RTC_REG0);
    286       1.1  jmcneill 	u &= ~AO_RTC_REG0_STATIC_REG_LSB;
    287       1.1  jmcneill 	u |= __SHIFTIN(data, AO_RTC_REG0_STATIC_REG_LSB);
    288       1.1  jmcneill 	u |= AO_RTC_REG0_SERIAL_START;
    289       1.1  jmcneill 	RTC_WRITE(sc, AO_RTC_REG0, u);
    290       1.1  jmcneill 
    291       1.1  jmcneill 	/* Poll auto_serializer_busy bit until it's low (IDLE) */
    292       1.1  jmcneill 	while ((RTC_READ(sc, AO_RTC_REG0) & AO_RTC_REG0_SERIAL_BUSY) != 0)
    293       1.1  jmcneill 		continue;
    294       1.1  jmcneill }
    295       1.1  jmcneill 
    296       1.1  jmcneill static void
    297       1.1  jmcneill meson_rtc_reset(struct meson_rtc_softc *sc)
    298       1.1  jmcneill {
    299       1.1  jmcneill 
    300       1.1  jmcneill 	meson_rtc_static_register_write(sc, RTC_STATIC_VALUE_INIT);
    301       1.1  jmcneill }
    302       1.1  jmcneill 
    303       1.1  jmcneill static int
    304       1.1  jmcneill meson_rtc_serial_init(struct meson_rtc_softc *sc)
    305       1.1  jmcneill {
    306       1.1  jmcneill 	size_t init_cnt, retry_cnt;
    307       1.1  jmcneill 
    308       1.1  jmcneill 	init_cnt = 0;
    309       1.1  jmcneill 	retry_cnt = 0;
    310       1.1  jmcneill 	while (meson_rtc_comm_init(sc) == -1) {
    311       1.1  jmcneill 		if (init_cnt++ == RESET_RETRY_TIMES) {
    312       1.1  jmcneill 			init_cnt = 0;
    313       1.1  jmcneill 			if (retry_cnt++ == RESET_RETRY_TIMES) {
    314       1.1  jmcneill 				aprint_error_dev(sc->sc_dev,
    315       1.1  jmcneill 				    "cannot init rtc\n");
    316       1.1  jmcneill 				return -1;
    317       1.1  jmcneill 			}
    318       1.1  jmcneill 			meson_rtc_reset(sc);
    319       1.1  jmcneill 		}
    320       1.1  jmcneill 		delay(RTC_RESET_DELAY);
    321       1.1  jmcneill 	}
    322       1.1  jmcneill 	return 0;
    323       1.1  jmcneill }
    324       1.1  jmcneill 
    325       1.1  jmcneill static int
    326       1.1  jmcneill meson_rtc_serial_read(struct meson_rtc_softc *sc, uint32_t addr,
    327       1.1  jmcneill     uint32_t *sec)
    328       1.1  jmcneill {
    329       1.1  jmcneill 
    330       1.1  jmcneill 	if (meson_rtc_serial_init(sc) == -1)
    331       1.1  jmcneill 		return EIO;
    332       1.1  jmcneill 
    333       1.1  jmcneill 	meson_rtc_send_data(sc, SERIAL_TYPE_ADDR, addr);
    334       1.1  jmcneill 	meson_rtc_set_mode(sc, SERIAL_MODE_READ);
    335       1.1  jmcneill 	*sec = meson_rtc_get_data(sc);
    336       1.1  jmcneill 	return 0;
    337       1.1  jmcneill }
    338       1.1  jmcneill 
    339       1.1  jmcneill static int
    340       1.1  jmcneill meson_rtc_serial_write(struct meson_rtc_softc *sc, uint32_t addr,
    341       1.1  jmcneill     uint32_t data)
    342       1.1  jmcneill {
    343       1.1  jmcneill 
    344       1.1  jmcneill 	if (meson_rtc_serial_init(sc) == -1)
    345       1.1  jmcneill 		return EIO;
    346       1.1  jmcneill 
    347       1.1  jmcneill 	meson_rtc_send_data(sc, SERIAL_TYPE_DATA, data);
    348       1.1  jmcneill 	meson_rtc_send_data(sc, SERIAL_TYPE_ADDR, addr);
    349       1.1  jmcneill 	meson_rtc_set_mode(sc, SERIAL_MODE_WRITE);
    350       1.1  jmcneill 	return 0;
    351       1.1  jmcneill }
    352       1.1  jmcneill 
    353       1.1  jmcneill static int
    354       1.1  jmcneill meson_rtc_todr_gettime(todr_chip_handle_t ch, struct timeval *tv)
    355       1.1  jmcneill {
    356       1.1  jmcneill 	struct meson_rtc_softc * const sc = ch->cookie;
    357       1.1  jmcneill 	uint32_t sec;
    358       1.1  jmcneill 	int rv;
    359       1.1  jmcneill 
    360       1.1  jmcneill 	if (atomic_swap_uint(&sc->sc_busy, 1))
    361       1.1  jmcneill 		return EBUSY;	/* XXX: EAGAIN? */
    362       1.1  jmcneill 
    363       1.1  jmcneill 	rv = meson_rtc_serial_read(sc, RTC_COUNTER_ADDR, &sec);
    364       1.1  jmcneill 	sc->sc_busy = 0;
    365       1.1  jmcneill 
    366       1.1  jmcneill 	if (rv == 0) {
    367       1.1  jmcneill 		tv->tv_sec = sec;
    368       1.1  jmcneill 		tv->tv_usec = 0;
    369       1.1  jmcneill 	}
    370       1.1  jmcneill 	return rv;
    371       1.1  jmcneill }
    372       1.1  jmcneill 
    373       1.1  jmcneill static int
    374       1.1  jmcneill meson_rtc_todr_settime(todr_chip_handle_t ch, struct timeval *tv)
    375       1.1  jmcneill {
    376       1.1  jmcneill 	struct meson_rtc_softc * const sc = ch->cookie;
    377       1.1  jmcneill 	int rv;
    378       1.1  jmcneill 
    379       1.1  jmcneill 	if (atomic_swap_uint(&sc->sc_busy, 1))
    380       1.1  jmcneill 		return EBUSY;	/* XXX: EAGAIN? */
    381       1.1  jmcneill 
    382       1.1  jmcneill 	rv = meson_rtc_serial_write(sc, RTC_COUNTER_ADDR, tv->tv_sec);
    383       1.1  jmcneill 	sc->sc_busy = 0;
    384       1.1  jmcneill 
    385       1.1  jmcneill 	return rv;
    386       1.1  jmcneill }
    387