Home | History | Annotate | Line # | Download | only in dev
drbbc.c revision 1.20
      1 /*	$NetBSD: drbbc.c,v 1.20 2012/10/27 17:17:28 chs 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.20 2012/10/27 17:17:28 chs 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 int draco_ds_read_bit(void *);
     54 void draco_ds_write_bit(void *, int);
     55 void draco_ds_reset(void *);
     56 
     57 void drbbc_attach(device_t, device_t, void *);
     58 int drbbc_match(device_t, cfdata_t, void *);
     59 
     60 int dracougettod(todr_chip_handle_t, struct timeval *);
     61 int dracousettod(todr_chip_handle_t, struct timeval *);
     62 
     63 static struct todr_chip_handle dracotodr;
     64 struct drbbc_softc {
     65 	struct ds_handle sc_dsh;
     66 };
     67 
     68 CFATTACH_DECL_NEW(drbbc, sizeof(struct drbbc_softc),
     69     drbbc_match, drbbc_attach, NULL, NULL);
     70 
     71 struct drbbc_softc *drbbc_sc;
     72 
     73 int
     74 drbbc_match(device_t parent, cfdata_t cf, void *aux)
     75 {
     76 	static int drbbc_matched = 0;
     77 
     78 	/* Allow only one instance. */
     79 	if (!is_draco() || !matchname(aux, "drbbc") || drbbc_matched)
     80 		return (0);
     81 
     82 	drbbc_matched = 1;
     83 	return (1);
     84 }
     85 
     86 void
     87 drbbc_attach(device_t parent, device_t self, void *aux)
     88 {
     89 	int i;
     90 	struct drbbc_softc *sc;
     91 	u_int8_t rombuf[8];
     92 
     93 	sc = device_private(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 	drbbc_sc = sc;
    115 	dracotodr.cookie = sc;
    116 	dracotodr.todr_gettime = dracougettod;
    117 	dracotodr.todr_settime = dracousettod;
    118 	todr_attach(&dracotodr);
    119 }
    120 
    121 int
    122 draco_ds_read_bit(void *p)
    123 {
    124 	struct drioct *draco_ioctl;
    125 
    126 	draco_ioctl = p;
    127 
    128 	while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
    129 
    130 	draco_ioctl->io_clockw1 = 0;
    131 
    132 	while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
    133 
    134 	return (draco_ioctl->io_status & DRSTAT_CLKDAT);
    135 }
    136 
    137 void
    138 draco_ds_write_bit(void *p, int b)
    139 {
    140 	struct drioct *draco_ioctl;
    141 
    142 	draco_ioctl = p;
    143 
    144 	while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
    145 
    146 	if (b)
    147 		draco_ioctl->io_clockw1 = 0;
    148 	else
    149 		draco_ioctl->io_clockw0 = 0;
    150 }
    151 
    152 void
    153 draco_ds_reset(void *p)
    154 {
    155 	struct drioct *draco_ioctl;
    156 
    157 	draco_ioctl = p;
    158 
    159 	draco_ioctl->io_clockrst = 0;
    160 }
    161 
    162 int
    163 dracougettod(todr_chip_handle_t h, struct timeval *tvp)
    164 {
    165 	u_int32_t clkbuf;
    166 	u_int32_t usecs;
    167 
    168 	drbbc_sc->sc_dsh.ds_reset(drbbc_sc->sc_dsh.ds_hw_handle);
    169 
    170 	ds_write_byte(&drbbc_sc->sc_dsh, DS_ROM_SKIP);
    171 
    172 	ds_write_byte(&drbbc_sc->sc_dsh, DS_MEM_READ_MEMORY);
    173 	/* address of seconds/256: */
    174 	ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
    175 	ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
    176 
    177 	usecs = (ds_read_byte(&drbbc_sc->sc_dsh) * 1000000) / 256;
    178 	clkbuf = ds_read_byte(&drbbc_sc->sc_dsh)
    179 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<8)
    180 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<16)
    181 	    + (ds_read_byte(&drbbc_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 int
    194 dracousettod(todr_chip_handle_t h, struct timeval *tvp)
    195 {
    196 	return (ENXIO);
    197 }
    198