Home | History | Annotate | Line # | Download | only in obio
      1 /*	$NetBSD: mkclock.c,v 1.13 2025/09/07 21:45:14 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Wayne Knowles
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.13 2025/09/07 21:45:14 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/kernel.h>
     37 #include <sys/device.h>
     38 #include <sys/systm.h>
     39 
     40 #include <dev/clock_subr.h>
     41 
     42 #include <machine/cpu.h>
     43 #include <machine/mainboard.h>
     44 #include <machine/autoconf.h>
     45 #include <machine/sysconf.h>
     46 #include <machine/bus.h>
     47 
     48 #include <mipsco/obio/clockreg.h>
     49 
     50 struct	mkclock_softc {
     51 	device_t sc_dev;
     52 	bus_space_tag_t	sc_bst;
     53 	bus_space_handle_t sc_bsh;
     54 	struct todr_chip_handle sc_todr;
     55 };
     56 
     57 static int mkclock_match (device_t, cfdata_t, void *);
     58 static void mkclock_attach (device_t, device_t, void *);
     59 
     60 CFATTACH_DECL_NEW(mkclock, sizeof(struct mkclock_softc),
     61     mkclock_match, mkclock_attach, NULL, NULL);
     62 
     63 int mkclock_read (todr_chip_handle_t, struct clock_ymdhms *);
     64 int mkclock_write (todr_chip_handle_t, struct clock_ymdhms *);
     65 
     66 static int mk_read (struct mkclock_softc *, int);
     67 static void mk_write (struct mkclock_softc *, int, int);
     68 
     69 int
     70 mkclock_match(device_t parent, cfdata_t cf, void *aux)
     71 {
     72 	return 1;
     73 }
     74 
     75 void
     76 mkclock_attach(device_t parent, device_t self, void *aux)
     77 {
     78         struct mkclock_softc *sc = device_private(self);
     79 	struct confargs *ca = aux;
     80 
     81 	sc->sc_dev = self;
     82 	sc->sc_bst = ca->ca_bustag;
     83 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
     84 			  0x10000,
     85 			  BUS_SPACE_MAP_LINEAR,
     86 			  &sc->sc_bsh) != 0) {
     87 		printf(": cannot map registers\n");
     88 		return;
     89 	}
     90 
     91 	sc->sc_todr.todr_settime_ymdhms = mkclock_write;
     92 	sc->sc_todr.todr_gettime_ymdhms = mkclock_read;
     93 	sc->sc_todr.todr_dev = self;
     94 	todr_attach(&sc->sc_todr);
     95 
     96 	printf("\n");
     97 }
     98 
     99 static int
    100 mk_read(struct mkclock_softc *sc, int reg)
    101 {
    102 	u_int8_t val;
    103 
    104 	val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4);
    105 	return bcdtobin(val);
    106 }
    107 
    108 static void
    109 mk_write(struct mkclock_softc *sc, int reg, int val)
    110 {
    111 	bus_space_write_1(sc->sc_bst, sc->sc_bsh,
    112 			  DATA_PORT + reg*4, bintobcd(val));
    113 }
    114 
    115 int
    116 mkclock_read(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    117 {
    118 	struct mkclock_softc *sc = device_private(tch->todr_dev);
    119 	int s = splclock();
    120 
    121 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK);
    122 	dt->dt_sec  = mk_read(sc, 0);
    123 	dt->dt_min  = mk_read(sc, 1);
    124 	dt->dt_hour = mk_read(sc, 2);
    125 	dt->dt_wday = mk_read(sc, 3);
    126 	dt->dt_day  = mk_read(sc, 4);
    127 	dt->dt_mon  = mk_read(sc, 5);
    128 	dt->dt_year = mk_read(sc, 6);
    129 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
    130 	splx(s);
    131 
    132 	dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000);
    133 	return 0;
    134 }
    135 
    136 int
    137 mkclock_write(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    138 {
    139 	struct mkclock_softc *sc = device_private(tch->todr_dev);
    140 	int year, s;
    141 
    142 	year = dt->dt_year % 100;
    143 
    144 	s = splclock();
    145 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK);
    146 	mk_write(sc, 0, dt->dt_sec);
    147 	mk_write(sc, 1, dt->dt_min);
    148 	mk_write(sc, 2, dt->dt_hour);
    149 	/* week day not set */
    150 	mk_write(sc, 4, dt->dt_day);
    151 	mk_write(sc, 5, dt->dt_mon);
    152 	mk_write(sc, 6, year);
    153 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
    154 	splx(s);
    155 	return 0;
    156 }
    157