#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: drbbc.c,v 1.22 2025/09/07 21:45:10 thorpej Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/systm.h>
#if 0
#include <machine/psl.h>
#endif
#include <machine/cpu.h>
#include <amiga/amiga/device.h>
#include <amiga/amiga/custom.h>
#include <amiga/amiga/cia.h>
#include <amiga/amiga/drcustom.h>
#include <amiga/dev/rtc.h>
#include <dev/clock_subr.h>
#include <dev/ic/ds.h>
static int draco_ds_read_bit(void *);
static void draco_ds_write_bit(void *, int);
static void draco_ds_reset(void *);
static void drbbc_attach(device_t, device_t, void *);
static int drbbc_match(device_t, cfdata_t, void *);
static int dracougettod(todr_chip_handle_t, struct timeval *);
static int dracousettod(todr_chip_handle_t, struct timeval *);
struct drbbc_softc {
device_t sc_dev;
struct ds_handle sc_dsh;
struct todr_chip_handle sc_todr;
};
CFATTACH_DECL_NEW(drbbc, sizeof(struct drbbc_softc),
drbbc_match, drbbc_attach, NULL, NULL);
static int
drbbc_match(device_t parent, cfdata_t cf, void *aux)
{
static int drbbc_matched = 0;
if (!is_draco() || !matchname(aux, "drbbc") || drbbc_matched)
return (0);
drbbc_matched = 1;
return (1);
}
static void
drbbc_attach(device_t parent, device_t self, void *aux)
{
int i;
struct drbbc_softc *sc;
u_int8_t rombuf[8];
sc = device_private(self);
sc->sc_dev = self;
sc->sc_dsh.ds_read_bit = draco_ds_read_bit;
sc->sc_dsh.ds_write_bit = draco_ds_write_bit;
sc->sc_dsh.ds_reset = draco_ds_reset;
sc->sc_dsh.ds_hw_handle = (void *)(DRCCADDR + DRIOCTLPG*PAGE_SIZE);
sc->sc_dsh.ds_reset(sc->sc_dsh.ds_hw_handle);
ds_write_byte(&sc->sc_dsh, DS_ROM_READ);
for (i=0; i<8; ++i)
rombuf[i] = ds_read_byte(&sc->sc_dsh);
hostid = (rombuf[3] << 24) + (rombuf[2] << 16) +
(rombuf[1] << 8) + rombuf[7];
printf(": ROM %02x %02x%02x%02x%02x%02x%02x %02x (DraCo sernum %ld)\n",
rombuf[7], rombuf[6], rombuf[5], rombuf[4],
rombuf[3], rombuf[2], rombuf[1], rombuf[0],
hostid);
sc->sc_todr.todr_dev = self;
sc->sc_todr.todr_gettime = dracougettod;
sc->sc_todr.todr_settime = dracousettod;
todr_attach(&sc->sc_todr);
}
static int
draco_ds_read_bit(void *p)
{
struct drioct *draco_ioctl;
draco_ioctl = p;
while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
draco_ioctl->io_clockw1 = 0;
while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
return (draco_ioctl->io_status & DRSTAT_CLKDAT);
}
static void
draco_ds_write_bit(void *p, int b)
{
struct drioct *draco_ioctl;
draco_ioctl = p;
while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
if (b)
draco_ioctl->io_clockw1 = 0;
else
draco_ioctl->io_clockw0 = 0;
}
static void
draco_ds_reset(void *p)
{
struct drioct *draco_ioctl;
draco_ioctl = p;
draco_ioctl->io_clockrst = 0;
}
static int
dracougettod(todr_chip_handle_t h, struct timeval *tvp)
{
struct drbbc_softc *sc = device_private(h->todr_dev);
u_int32_t clkbuf;
u_int32_t usecs;
sc->sc_dsh.ds_reset(sc->sc_dsh.ds_hw_handle);
ds_write_byte(&sc->sc_dsh, DS_ROM_SKIP);
ds_write_byte(&sc->sc_dsh, DS_MEM_READ_MEMORY);
ds_write_byte(&sc->sc_dsh, 0x02);
ds_write_byte(&sc->sc_dsh, 0x02);
usecs = (ds_read_byte(&sc->sc_dsh) * 1000000) / 256;
clkbuf = ds_read_byte(&sc->sc_dsh)
+ (ds_read_byte(&sc->sc_dsh)<<8)
+ (ds_read_byte(&sc->sc_dsh)<<16)
+ (ds_read_byte(&sc->sc_dsh)<<24);
clkbuf += (8*365 + 2) * 86400;
tvp->tv_sec = clkbuf;
tvp->tv_usec = usecs;
return (0);
}
static int
dracousettod(todr_chip_handle_t h, struct timeval *tvp)
{
return (ENXIO);
}