Home | History | Annotate | Line # | Download | only in dev
      1  1.26   thorpej /*	$NetBSD: rtclock.c,v 1.26 2025/09/07 21:45:15 thorpej Exp $	*/
      2   1.1       oki 
      3   1.1       oki /*
      4   1.1       oki  * Copyright 1993, 1994 Masaru Oki
      5   1.1       oki  * All rights reserved.
      6   1.1       oki  *
      7   1.1       oki  * Redistribution and use in source and binary forms, with or without
      8   1.1       oki  * modification, are permitted provided that the following conditions
      9   1.1       oki  * are met:
     10   1.1       oki  * 1. Redistributions of source code must retain the above copyright
     11   1.1       oki  *    notice, this list of conditions and the following disclaimer.
     12   1.1       oki  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       oki  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       oki  *    documentation and/or other materials provided with the distribution.
     15   1.1       oki  * 3. All advertising materials mentioning features or use of this software
     16   1.1       oki  *    must display the following acknowledgement:
     17   1.1       oki  *      This product includes software developed by Masaru Oki.
     18   1.1       oki  * 4. The name of the author may not be used to endorse or promote products
     19   1.1       oki  *    derived from this software without specific prior written permission
     20   1.1       oki  *
     21   1.1       oki  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1       oki  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1       oki  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1       oki  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1       oki  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1       oki  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1       oki  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1       oki  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1       oki  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1       oki  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1       oki  */
     32   1.1       oki 
     33   1.1       oki /*
     34   1.1       oki  * X680x0 internal real time clock interface
     35   1.1       oki  * alarm is not supported.
     36   1.1       oki  */
     37  1.15     lukem 
     38  1.15     lukem #include <sys/cdefs.h>
     39  1.26   thorpej __KERNEL_RCSID(0, "$NetBSD: rtclock.c,v 1.26 2025/09/07 21:45:15 thorpej Exp $");
     40   1.1       oki 
     41   1.1       oki #include <sys/param.h>
     42   1.1       oki #include <sys/systm.h>
     43   1.1       oki #include <sys/buf.h>
     44   1.1       oki #include <sys/proc.h>
     45   1.1       oki #include <sys/reboot.h>
     46   1.1       oki #include <sys/file.h>
     47   1.2     perry #include <sys/kernel.h>
     48   1.4   minoura #include <sys/device.h>
     49   1.1       oki 
     50   1.4   minoura #include <machine/bus.h>
     51   1.4   minoura 
     52   1.8   minoura #include <dev/clock_subr.h>
     53   1.8   minoura 
     54   1.4   minoura #include <arch/x68k/dev/rtclock_var.h>
     55   1.4   minoura #include <arch/x68k/dev/intiovar.h>
     56   1.1       oki 
     57  1.19   gdamore static int rtgettod(todr_chip_handle_t, struct clock_ymdhms *);
     58  1.19   gdamore static int rtsettod(todr_chip_handle_t, struct clock_ymdhms *);
     59   1.1       oki 
     60  1.22     isaki static int rtc_match(device_t, cfdata_t, void *);
     61  1.22     isaki static void rtc_attach(device_t, device_t, void *);
     62   1.4   minoura 
     63  1.22     isaki CFATTACH_DECL_NEW(rtc, sizeof(struct rtc_softc),
     64  1.14   thorpej     rtc_match, rtc_attach, NULL, NULL);
     65   1.4   minoura 
     66  1.16       chs static int rtc_attached;
     67  1.16       chs 
     68  1.20     isaki static int
     69  1.22     isaki rtc_match(device_t parent, cfdata_t cf, void *aux)
     70   1.4   minoura {
     71   1.4   minoura 	struct intio_attach_args *ia = aux;
     72   1.4   minoura 
     73  1.21     isaki 	if (strcmp(ia->ia_name, "rtc") != 0)
     74   1.4   minoura 		return (0);
     75  1.16       chs 	if (rtc_attached)
     76   1.4   minoura 		return (0);
     77   1.4   minoura 
     78   1.4   minoura 	/* fixed address */
     79   1.4   minoura 	if (ia->ia_addr != RTC_ADDR)
     80   1.4   minoura 		return (0);
     81   1.4   minoura 	if (ia->ia_intr != -1)
     82   1.4   minoura 		return (0);
     83   1.4   minoura 
     84   1.4   minoura 	return (1);
     85   1.4   minoura }
     86   1.4   minoura 
     87  1.20     isaki static void
     88  1.22     isaki rtc_attach(device_t parent, device_t self, void *aux)
     89   1.4   minoura {
     90  1.22     isaki 	struct rtc_softc *sc = device_private(self);
     91   1.4   minoura 	struct intio_attach_args *ia = aux;
     92  1.24  christos 	int r __diagused;
     93   1.4   minoura 
     94  1.16       chs 	rtc_attached = 1;
     95  1.16       chs 
     96   1.4   minoura 	ia->ia_size = 0x20;
     97  1.21     isaki 	r = intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
     98   1.4   minoura #ifdef DIAGNOSTIC
     99   1.4   minoura 	if (r)
    100  1.21     isaki 		panic("IO map for RTC corruption??");
    101   1.4   minoura #endif
    102   1.4   minoura 
    103   1.4   minoura 
    104   1.4   minoura 	sc->sc_bst = ia->ia_bst;
    105   1.4   minoura 	bus_space_map(sc->sc_bst, ia->ia_addr, 0x2000, 0, &sc->sc_bht);
    106   1.4   minoura 
    107  1.26   thorpej 	sc->sc_todr.todr_dev = self;
    108  1.19   gdamore 	sc->sc_todr.todr_gettime_ymdhms = rtgettod;
    109  1.19   gdamore 	sc->sc_todr.todr_settime_ymdhms = rtsettod;
    110  1.19   gdamore 	todr_attach(&sc->sc_todr);
    111  1.19   gdamore 
    112  1.22     isaki 	aprint_normal(": RP5C15\n");
    113   1.4   minoura }
    114   1.4   minoura 
    115  1.19   gdamore static int
    116  1.19   gdamore rtgettod(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    117   1.1       oki {
    118  1.26   thorpej 	struct rtc_softc *rtc = device_private(tch->todr_dev);
    119   1.1       oki 
    120   1.1       oki 	/* hold clock */
    121   1.4   minoura 	RTC_WRITE(RTC_MODE, RTC_HOLD_CLOCK);
    122   1.1       oki 
    123   1.1       oki 	/* read it */
    124  1.19   gdamore 	dt->dt_sec  = RTC_REG(RTC_SEC10)  * 10 + RTC_REG(RTC_SEC);
    125  1.19   gdamore 	dt->dt_min  = RTC_REG(RTC_MIN10)  * 10 + RTC_REG(RTC_MIN);
    126  1.19   gdamore 	dt->dt_hour = RTC_REG(RTC_HOUR10) * 10 + RTC_REG(RTC_HOUR);
    127  1.19   gdamore 	dt->dt_day  = RTC_REG(RTC_DAY10)  * 10 + RTC_REG(RTC_DAY);
    128  1.19   gdamore 	dt->dt_mon  = RTC_REG(RTC_MON10)  * 10 + RTC_REG(RTC_MON);
    129  1.19   gdamore 	dt->dt_year = RTC_REG(RTC_YEAR10) * 10 + RTC_REG(RTC_YEAR)
    130   1.9     itohy 							+RTC_BASE_YEAR;
    131   1.1       oki 
    132   1.1       oki 	/* let it run again.. */
    133   1.4   minoura 	RTC_WRITE(RTC_MODE, RTC_FREE_CLOCK);
    134   1.1       oki 
    135  1.19   gdamore 	return 0;
    136   1.1       oki }
    137   1.1       oki 
    138   1.1       oki static int
    139  1.19   gdamore rtsettod(todr_chip_handle_t tch, struct clock_ymdhms *dt)
    140   1.1       oki {
    141  1.26   thorpej 	struct rtc_softc *rtc = device_private(tch->todr_dev);
    142   1.1       oki 	u_char sec1, sec2;
    143   1.1       oki 	u_char min1, min2;
    144   1.1       oki 	u_char hour1, hour2;
    145   1.1       oki 	u_char day1, day2;
    146   1.1       oki 	u_char mon1, mon2;
    147   1.1       oki 	u_char year1, year2;
    148   1.1       oki 
    149   1.1       oki 	/* prepare values to be written to clock */
    150  1.19   gdamore 	sec1  = dt->dt_sec  / 10;
    151  1.19   gdamore 	sec2  = dt->dt_sec  % 10;
    152  1.19   gdamore 	min1  = dt->dt_min  / 10;
    153  1.19   gdamore 	min2  = dt->dt_min  % 10;
    154  1.19   gdamore 	hour1 = dt->dt_hour / 10;
    155  1.19   gdamore 	hour2 = dt->dt_hour % 10;
    156  1.19   gdamore 
    157  1.19   gdamore 	day1  = dt->dt_day  / 10;
    158  1.19   gdamore 	day2  = dt->dt_day  % 10;
    159  1.19   gdamore 	mon1  = dt->dt_mon  / 10;
    160  1.19   gdamore 	mon2  = dt->dt_mon  % 10;
    161  1.19   gdamore 	year1 = (dt->dt_year - RTC_BASE_YEAR) / 10;
    162  1.19   gdamore 	year2 = dt->dt_year % 10;
    163   1.1       oki 
    164   1.4   minoura 	RTC_WRITE(RTC_MODE,   RTC_HOLD_CLOCK);
    165   1.4   minoura 	RTC_WRITE(RTC_SEC10,  sec1);
    166   1.4   minoura 	RTC_WRITE(RTC_SEC,    sec2);
    167   1.4   minoura 	RTC_WRITE(RTC_MIN10,  min1);
    168   1.4   minoura 	RTC_WRITE(RTC_MIN,    min2);
    169   1.4   minoura 	RTC_WRITE(RTC_HOUR10, hour1);
    170   1.4   minoura 	RTC_WRITE(RTC_HOUR,   hour2);
    171   1.4   minoura 	RTC_WRITE(RTC_DAY10,  day1);
    172   1.4   minoura 	RTC_WRITE(RTC_DAY,    day2);
    173   1.4   minoura 	RTC_WRITE(RTC_MON10,  mon1);
    174   1.4   minoura 	RTC_WRITE(RTC_MON,    mon2);
    175   1.4   minoura 	RTC_WRITE(RTC_YEAR10, year1);
    176   1.4   minoura 	RTC_WRITE(RTC_YEAR,   year2);
    177   1.4   minoura 	RTC_WRITE(RTC_MODE,   RTC_FREE_CLOCK);
    178   1.1       oki 
    179  1.19   gdamore 	return 0;
    180   1.1       oki }
    181