eprtc.c revision 1.9
11.9Sthorpej/*	$NetBSD: eprtc.c,v 1.9 2025/09/07 21:45:11 thorpej Exp $	*/
21.1Shamajima
31.1Shamajima/*
41.1Shamajima * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
51.1Shamajima *
61.1Shamajima * Redistribution and use in source and binary forms, with or without
71.1Shamajima * modification, are permitted provided that the following conditions
81.1Shamajima * are met:
91.1Shamajima * 1. Redistributions of source code must retain the above copyright
101.1Shamajima *    notice, this list of conditions and the following disclaimer.
111.1Shamajima * 2. Redistributions in binary form must reproduce the above copyright
121.1Shamajima *    notice, this list of conditions and the following disclaimer in the
131.1Shamajima *    documentation and/or other materials provided with the distribution.
141.1Shamajima *
151.1Shamajima * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161.1Shamajima * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171.1Shamajima * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181.1Shamajima * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191.1Shamajima * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201.1Shamajima * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211.1Shamajima * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221.1Shamajima * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231.1Shamajima * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Shamajima * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Shamajima * SUCH DAMAGE.
261.1Shamajima */
271.1Shamajima
281.1Shamajima#include <sys/cdefs.h>
291.9Sthorpej__KERNEL_RCSID(0, "$NetBSD: eprtc.c,v 1.9 2025/09/07 21:45:11 thorpej Exp $");
301.1Shamajima
311.1Shamajima#include <sys/param.h>
321.1Shamajima#include <sys/systm.h>
331.1Shamajima#include <sys/kernel.h>
341.1Shamajima#include <sys/device.h>
351.1Shamajima#include <dev/clock_subr.h>
361.5Sdyoung#include <sys/bus.h>
371.7Sskrll#include <arm/ep93xx/ep93xxvar.h>
381.7Sskrll#include <arm/ep93xx/epsocvar.h>
391.7Sskrll#include <arm/ep93xx/eprtcreg.h>
401.1Shamajima
411.1Shamajimastruct eprtc_softc {
421.9Sthorpej	device_t		sc_dev;
431.1Shamajima	bus_space_tag_t		sc_iot;
441.1Shamajima	bus_space_handle_t	sc_ioh;
451.1Shamajima	struct todr_chip_handle	sc_todr;
461.1Shamajima};
471.1Shamajima
481.6Schsstatic int eprtc_match(device_t, cfdata_t, void *);
491.6Schsstatic void eprtc_attach(device_t, device_t, void *);
501.1Shamajima
511.6SchsCFATTACH_DECL_NEW(eprtc, sizeof(struct eprtc_softc),
521.1Shamajima	      eprtc_match, eprtc_attach, NULL, NULL);
531.1Shamajima
541.3Stsutsuistatic int eprtc_gettime(struct todr_chip_handle *, struct timeval *);
551.3Stsutsuistatic int eprtc_settime(struct todr_chip_handle *, struct timeval *);
561.1Shamajima
571.1Shamajimastatic int
581.6Schseprtc_match(device_t parent, cfdata_t match, void *aux)
591.1Shamajima{
601.1Shamajima	return 1;
611.1Shamajima}
621.1Shamajima
631.1Shamajimastatic void
641.6Schseprtc_attach(device_t parent, device_t self, void *aux)
651.1Shamajima{
661.6Schs	struct eprtc_softc *sc = device_private(self);
671.1Shamajima	struct epsoc_attach_args *sa = aux;
681.1Shamajima
691.1Shamajima	printf("\n");
701.9Sthorpej	sc->sc_dev = self;
711.1Shamajima	sc->sc_iot = sa->sa_iot;
721.1Shamajima
731.1Shamajima	if (bus_space_map(sa->sa_iot, sa->sa_addr,
741.6Schs			  sa->sa_size, 0, &sc->sc_ioh)) {
751.6Schs		printf("%s: Cannot map registers", device_xname(self));
761.1Shamajima		return;
771.1Shamajima	}
781.1Shamajima
791.9Sthorpej	sc->sc_todr.todr_dev = self;
801.1Shamajima	sc->sc_todr.todr_gettime = eprtc_gettime;
811.1Shamajima	sc->sc_todr.todr_settime = eprtc_settime;
821.1Shamajima	todr_attach(&sc->sc_todr);
831.1Shamajima}
841.1Shamajima
851.1Shamajimastatic int
861.3Stsutsuieprtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
871.1Shamajima{
881.9Sthorpej	struct eprtc_softc *sc = device_private(ch->todr_dev);
891.1Shamajima
901.1Shamajima	tv->tv_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EP93XX_RTC_Data);
911.1Shamajima	tv->tv_usec = 0;
921.1Shamajima	return 0;
931.1Shamajima}
941.1Shamajima
951.1Shamajimastatic int
961.3Stsutsuieprtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
971.1Shamajima{
981.9Sthorpej	struct eprtc_softc *sc = device_private(ch->todr_dev);
991.1Shamajima
1001.1Shamajima	bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_RTC_Load, tv->tv_sec);
1011.1Shamajima	return 0;
1021.1Shamajima}
103