Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: a34kbbc.c,v 1.25 2025/09/07 21:45:10 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1988 University of Utah.
      5  * Copyright (c) 1982, 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * the Systems Programming Group of the University of Utah Computer
     10  * Science Department.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  * from: Utah $Hdr: clock.c 1.18 91/01/21$
     37  *
     38  *	@(#)clock.c	7.6 (Berkeley) 5/7/91
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: a34kbbc.c,v 1.25 2025/09/07 21:45:10 thorpej Exp $");
     43 
     44 #include <sys/param.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.h>
     47 #include <sys/systm.h>
     48 #include <machine/psl.h>
     49 #include <machine/cpu.h>
     50 #include <amiga/amiga/device.h>
     51 #include <amiga/amiga/custom.h>
     52 #include <amiga/amiga/cia.h>
     53 #include <amiga/dev/rtc.h>
     54 #include <amiga/dev/zbusvar.h>
     55 
     56 #include <dev/clock_subr.h>
     57 
     58 static int	a34kbbc_match(device_t, cfdata_t, void *);
     59 static void	a34kbbc_attach(device_t, device_t, void *);
     60 
     61 struct a34kbbc_softc {
     62 	device_t sc_dev;
     63 	struct todr_chip_handle sc_todr;
     64 	volatile struct rtclock3000 *sc_addr;
     65 };
     66 
     67 CFATTACH_DECL_NEW(a34kbbc, sizeof(struct a34kbbc_softc),
     68     a34kbbc_match, a34kbbc_attach, NULL, NULL);
     69 
     70 static int	a34kbbc_read(volatile struct rtclock3000 *,
     71 		    struct clock_ymdhms *);
     72 static int	a34kugettod(todr_chip_handle_t, struct clock_ymdhms *);
     73 static int	a34kusettod(todr_chip_handle_t, struct clock_ymdhms *);
     74 
     75 static int
     76 a34kbbc_match(device_t parent, cfdata_t cf, void *aux)
     77 {
     78 	struct clock_ymdhms dt;
     79 	static int a34kbbc_matched = 0;
     80 
     81 	if (!matchname("a34kbbc", aux))
     82 		return(0);
     83 
     84 	/* Allow only one instance. */
     85 	if (a34kbbc_matched)
     86 		return(0);
     87 
     88 	if (!(is_a3000() || is_a4000()))
     89 		return(0);
     90 
     91 	if (a34kbbc_read(ztwomap(0xdc0000), &dt) != 0)
     92 		return(0);
     93 
     94 	a34kbbc_matched = 1;
     95 	return(1);
     96 }
     97 
     98 /*
     99  * Attach us to the rtc function pointers.
    100  */
    101 static void
    102 a34kbbc_attach(device_t parent, device_t self, void *aux)
    103 {
    104 	struct a34kbbc_softc *sc = device_private(self);
    105 
    106 	printf("\n");
    107 
    108 	sc->sc_dev = self;
    109 	sc->sc_addr = ztwomap(0xdc0000);
    110 
    111 	sc->sc_todr.todr_dev = self;
    112 	sc->sc_todr.todr_gettime_ymdhms = a34kugettod;
    113 	sc->sc_todr.todr_settime_ymdhms = a34kusettod;
    114 	todr_attach(&sc->sc_todr);
    115 }
    116 
    117 static int
    118 a34kbbc_read(volatile struct rtclock3000 *rt, struct clock_ymdhms *dt)
    119 {
    120 
    121 	/* hold clock */
    122 	rt->control1 = A3CONTROL1_HOLD_CLOCK;
    123 
    124 	/* Copy the info.  Careful about the order! */
    125 	dt->dt_sec   = rt->second1 * 10 + rt->second2;
    126 	dt->dt_min   = rt->minute1 * 10 + rt->minute2;
    127 	dt->dt_hour  = rt->hour1   * 10 + rt->hour2;
    128 	dt->dt_wday  = rt->weekday;
    129 	dt->dt_day   = rt->day1    * 10 + rt->day2;
    130 	dt->dt_mon   = rt->month1  * 10 + rt->month2;
    131 	dt->dt_year  = rt->year1   * 10 + rt->year2;
    132 
    133 	dt->dt_year += CLOCK_BASE_YEAR;
    134 	/* let it run again.. */
    135 	rt->control1 = A3CONTROL1_FREE_CLOCK;
    136 
    137 	if (dt->dt_year < STARTOFTIME)
    138 		dt->dt_year += 100;
    139 
    140 	/*
    141 	 * These checks are mostly redundant against those already in the
    142 	 * generic todr, but apparently the attach code checks against the
    143 	 * return value of this function, so we have to include a check here,
    144 	 * too.
    145 	 */
    146 	if ((dt->dt_hour > 23) ||
    147 	    (dt->dt_wday > 6) ||
    148 	    (dt->dt_day  > 31) ||
    149 	    (dt->dt_mon  > 12) ||
    150 	    /* (dt.dt_year < STARTOFTIME) || */ (dt->dt_year > 2036))
    151 		return (EINVAL);
    152 
    153 	return (0);
    154 }
    155 
    156 static int
    157 a34kugettod(todr_chip_handle_t h, struct clock_ymdhms *dt)
    158 {
    159 	struct a34kbbc_softc *sc = device_private(h->todr_dev);
    160 
    161 	return a34kbbc_read(sc->sc_addr, dt);
    162 }
    163 
    164 static int
    165 a34kusettod(todr_chip_handle_t h, struct clock_ymdhms *dt)
    166 {
    167 	struct a34kbbc_softc *sc = device_private(h->todr_dev);
    168 	volatile struct rtclock3000 *rt = sc->sc_addr;
    169 
    170 	/*
    171 	 * there seem to be problems with the bitfield addressing
    172 	 * currently used..
    173 	 */
    174 
    175 	rt->control1 = A3CONTROL1_HOLD_CLOCK;		/* implies mode 0 */
    176 	rt->second1 = dt->dt_sec / 10;
    177 	rt->second2 = dt->dt_sec % 10;
    178 	rt->minute1 = dt->dt_min / 10;
    179 	rt->minute2 = dt->dt_min % 10;
    180 	rt->hour1   = dt->dt_hour / 10;
    181 	rt->hour2   = dt->dt_hour % 10;
    182 	rt->weekday = dt->dt_wday;
    183 	rt->day1    = dt->dt_day / 10;
    184 	rt->day2    = dt->dt_day % 10;
    185 	rt->month1  = dt->dt_mon / 10;
    186 	rt->month2  = dt->dt_mon % 10;
    187 	rt->year1   = (dt->dt_year / 10) % 10;
    188 	rt->year2   = dt->dt_year % 10;
    189 	rt->control1 = A3CONTROL1_HOLD_CLOCK | 1;	/* mode 1 registers */
    190 	rt->leapyear = dt->dt_year; 		/* XXX implicit % 4 */
    191 	rt->control1 = A3CONTROL1_FREE_CLOCK;		/* implies mode 1 */
    192 
    193 	return (0);
    194 }
    195