Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: drbbc.c,v 1.22 2025/09/07 21:45:10 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Ignatios Souvatzis.
      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: drbbc.c,v 1.22 2025/09/07 21:45:10 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 #if 0
     41 #include <machine/psl.h>
     42 #endif
     43 #include <machine/cpu.h>
     44 #include <amiga/amiga/device.h>
     45 #include <amiga/amiga/custom.h>
     46 #include <amiga/amiga/cia.h>
     47 #include <amiga/amiga/drcustom.h>
     48 #include <amiga/dev/rtc.h>
     49 
     50 #include <dev/clock_subr.h>
     51 #include <dev/ic/ds.h>
     52 
     53 static int	draco_ds_read_bit(void *);
     54 static void	draco_ds_write_bit(void *, int);
     55 static void	draco_ds_reset(void *);
     56 
     57 static void	drbbc_attach(device_t, device_t, void *);
     58 static int	drbbc_match(device_t, cfdata_t, void *);
     59 
     60 static int	dracougettod(todr_chip_handle_t, struct timeval *);
     61 static int	dracousettod(todr_chip_handle_t, struct timeval *);
     62 
     63 struct drbbc_softc {
     64 	device_t sc_dev;
     65 	struct ds_handle sc_dsh;
     66 	struct todr_chip_handle sc_todr;
     67 };
     68 
     69 CFATTACH_DECL_NEW(drbbc, sizeof(struct drbbc_softc),
     70     drbbc_match, drbbc_attach, NULL, NULL);
     71 
     72 static int
     73 drbbc_match(device_t parent, cfdata_t cf, void *aux)
     74 {
     75 	static int drbbc_matched = 0;
     76 
     77 	/* Allow only one instance. */
     78 	if (!is_draco() || !matchname(aux, "drbbc") || drbbc_matched)
     79 		return (0);
     80 
     81 	drbbc_matched = 1;
     82 	return (1);
     83 }
     84 
     85 static void
     86 drbbc_attach(device_t parent, device_t self, void *aux)
     87 {
     88 	int i;
     89 	struct drbbc_softc *sc;
     90 	u_int8_t rombuf[8];
     91 
     92 	sc = device_private(self);
     93 	sc->sc_dev = self;
     94 
     95 	sc->sc_dsh.ds_read_bit = draco_ds_read_bit;
     96 	sc->sc_dsh.ds_write_bit = draco_ds_write_bit;
     97 	sc->sc_dsh.ds_reset = draco_ds_reset;
     98 	sc->sc_dsh.ds_hw_handle = (void *)(DRCCADDR + DRIOCTLPG*PAGE_SIZE);
     99 
    100 	sc->sc_dsh.ds_reset(sc->sc_dsh.ds_hw_handle);
    101 
    102 	ds_write_byte(&sc->sc_dsh, DS_ROM_READ);
    103 	for (i=0; i<8; ++i)
    104 		rombuf[i] = ds_read_byte(&sc->sc_dsh);
    105 
    106 	hostid = (rombuf[3] << 24) + (rombuf[2] << 16) +
    107 		(rombuf[1] << 8) + rombuf[7];
    108 
    109 	printf(": ROM %02x %02x%02x%02x%02x%02x%02x %02x (DraCo sernum %ld)\n",
    110 		rombuf[7], rombuf[6], rombuf[5], rombuf[4],
    111 		rombuf[3], rombuf[2], rombuf[1], rombuf[0],
    112 		hostid);
    113 
    114 	sc->sc_todr.todr_dev = self;
    115 	sc->sc_todr.todr_gettime = dracougettod;
    116 	sc->sc_todr.todr_settime = dracousettod;
    117 	todr_attach(&sc->sc_todr);
    118 }
    119 
    120 static int
    121 draco_ds_read_bit(void *p)
    122 {
    123 	struct drioct *draco_ioctl;
    124 
    125 	draco_ioctl = p;
    126 
    127 	while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
    128 
    129 	draco_ioctl->io_clockw1 = 0;
    130 
    131 	while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
    132 
    133 	return (draco_ioctl->io_status & DRSTAT_CLKDAT);
    134 }
    135 
    136 static void
    137 draco_ds_write_bit(void *p, int b)
    138 {
    139 	struct drioct *draco_ioctl;
    140 
    141 	draco_ioctl = p;
    142 
    143 	while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
    144 
    145 	if (b)
    146 		draco_ioctl->io_clockw1 = 0;
    147 	else
    148 		draco_ioctl->io_clockw0 = 0;
    149 }
    150 
    151 static void
    152 draco_ds_reset(void *p)
    153 {
    154 	struct drioct *draco_ioctl;
    155 
    156 	draco_ioctl = p;
    157 
    158 	draco_ioctl->io_clockrst = 0;
    159 }
    160 
    161 static int
    162 dracougettod(todr_chip_handle_t h, struct timeval *tvp)
    163 {
    164 	struct drbbc_softc *sc = device_private(h->todr_dev);
    165 	u_int32_t clkbuf;
    166 	u_int32_t usecs;
    167 
    168 	sc->sc_dsh.ds_reset(sc->sc_dsh.ds_hw_handle);
    169 
    170 	ds_write_byte(&sc->sc_dsh, DS_ROM_SKIP);
    171 
    172 	ds_write_byte(&sc->sc_dsh, DS_MEM_READ_MEMORY);
    173 	/* address of seconds/256: */
    174 	ds_write_byte(&sc->sc_dsh, 0x02);
    175 	ds_write_byte(&sc->sc_dsh, 0x02);
    176 
    177 	usecs = (ds_read_byte(&sc->sc_dsh) * 1000000) / 256;
    178 	clkbuf = ds_read_byte(&sc->sc_dsh)
    179 	    + (ds_read_byte(&sc->sc_dsh)<<8)
    180 	    + (ds_read_byte(&sc->sc_dsh)<<16)
    181 	    + (ds_read_byte(&sc->sc_dsh)<<24);
    182 
    183 	/* BSD time is wrt. 1.1.1970; AmigaOS time wrt. 1.1.1978 */
    184 
    185 	clkbuf += (8*365 + 2) * 86400;
    186 
    187 	tvp->tv_sec = clkbuf;
    188 	tvp->tv_usec = usecs;
    189 
    190 	return (0);
    191 }
    192 
    193 static int
    194 dracousettod(todr_chip_handle_t h, struct timeval *tvp)
    195 {
    196 	return (ENXIO);
    197 }
    198