Home | History | Annotate | Line # | Download | only in dev
drbbc.c revision 1.5
      1  1.5  is /*	$NetBSD: drbbc.c,v 1.5 1999/03/14 22:42:12 is Exp $	*/
      2  1.1  is 
      3  1.3  is /*-
      4  1.4  is  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  1.1  is  * All rights reserved.
      6  1.1  is  *
      7  1.3  is  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3  is  * by Ignatios Souvatzis.
      9  1.3  is  *
     10  1.1  is  * Redistribution and use in source and binary forms, with or without
     11  1.1  is  * modification, are permitted provided that the following conditions
     12  1.1  is  * are met:
     13  1.1  is  * 1. Redistributions of source code must retain the above copyright
     14  1.1  is  *    notice, this list of conditions and the following disclaimer.
     15  1.1  is  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  is  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  is  *    documentation and/or other materials provided with the distribution.
     18  1.1  is  * 3. All advertising materials mentioning features or use of this software
     19  1.1  is  *    must display the following acknowledgement:
     20  1.3  is  *        This product includes software developed by the NetBSD
     21  1.3  is  *        Foundation, Inc. and its contributors.
     22  1.3  is  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.3  is  *    contributors may be used to endorse or promote products derived
     24  1.3  is  *    from this software without specific prior written permission.
     25  1.1  is  *
     26  1.3  is  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.3  is  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.3  is  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.3  is  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.3  is  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.3  is  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.3  is  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.3  is  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.3  is  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.3  is  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.3  is  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  is  */
     38  1.1  is 
     39  1.1  is #include <sys/param.h>
     40  1.1  is #include <sys/kernel.h>
     41  1.1  is #include <sys/device.h>
     42  1.1  is #include <sys/systm.h>
     43  1.1  is #if 0
     44  1.1  is #include <machine/psl.h>
     45  1.1  is #endif
     46  1.1  is #include <machine/cpu.h>
     47  1.1  is #include <amiga/amiga/device.h>
     48  1.1  is #include <amiga/amiga/custom.h>
     49  1.1  is #include <amiga/amiga/cia.h>
     50  1.1  is #include <amiga/amiga/drcustom.h>
     51  1.1  is #include <amiga/dev/rtc.h>
     52  1.1  is 
     53  1.1  is #include <dev/ic/ds.h>
     54  1.1  is 
     55  1.1  is int draco_ds_read_bit __P((void *));
     56  1.1  is void draco_ds_write_bit __P((void *, int));
     57  1.1  is void draco_ds_reset __P((void *));
     58  1.1  is 
     59  1.1  is void drbbc_attach __P((struct device *, struct device *, void *));
     60  1.1  is int drbbc_match __P((struct device *, struct cfdata *, void *));
     61  1.1  is 
     62  1.5  is int dracougettod __P((struct timeval *));
     63  1.1  is #ifdef __NOTYET__
     64  1.5  is int dracousettod __P((struct timeval *));
     65  1.1  is #endif
     66  1.1  is 
     67  1.1  is struct drbbc_softc {
     68  1.1  is 	struct device sc_dev;
     69  1.1  is 	struct ds_handle sc_dsh;
     70  1.1  is };
     71  1.1  is 
     72  1.1  is struct cfattach drbbc_ca = {
     73  1.1  is 	sizeof(struct drbbc_softc),
     74  1.1  is 	drbbc_match,
     75  1.1  is 	drbbc_attach
     76  1.1  is };
     77  1.1  is 
     78  1.1  is struct drbbc_softc *drbbc_sc;
     79  1.1  is 
     80  1.1  is int
     81  1.1  is drbbc_match(pdp, cfp, auxp)
     82  1.1  is 	struct device *pdp;
     83  1.1  is 	struct cfdata *cfp;
     84  1.1  is 	void *auxp;
     85  1.1  is {
     86  1.1  is 	if (is_draco() && matchname(auxp, "drbbc") && (cfp->cf_unit == 0))
     87  1.1  is 		return (1);
     88  1.1  is 	else
     89  1.1  is 		return (0);
     90  1.1  is }
     91  1.1  is 
     92  1.1  is void
     93  1.1  is drbbc_attach(pdp, dp, auxp)
     94  1.1  is 	struct device *pdp, *dp;
     95  1.1  is 	void *auxp;
     96  1.1  is {
     97  1.1  is 	int i;
     98  1.1  is 	struct drbbc_softc *sc;
     99  1.1  is 	u_int8_t rombuf[8];
    100  1.1  is 
    101  1.1  is 	sc = (struct drbbc_softc *)dp;
    102  1.1  is 
    103  1.1  is 	sc->sc_dsh.ds_read_bit = draco_ds_read_bit;
    104  1.1  is 	sc->sc_dsh.ds_write_bit = draco_ds_write_bit;
    105  1.1  is 	sc->sc_dsh.ds_reset = draco_ds_reset;
    106  1.1  is 	sc->sc_dsh.ds_hw_handle = (void *)(DRCCADDR + DRIOCTLPG*NBPG);
    107  1.1  is 
    108  1.1  is 	sc->sc_dsh.ds_reset(sc->sc_dsh.ds_hw_handle);
    109  1.1  is 
    110  1.1  is 	ds_write_byte(&sc->sc_dsh, DS_ROM_READ);
    111  1.1  is 	for (i=0; i<8; ++i)
    112  1.1  is 		rombuf[i] = ds_read_byte(&sc->sc_dsh);
    113  1.1  is 
    114  1.1  is 	hostid = (rombuf[3] << 24) + (rombuf[2] << 16) +
    115  1.1  is 		(rombuf[1] << 8) + rombuf[7];
    116  1.1  is 
    117  1.1  is 	printf(": ROM %02x %02x%02x%02x%02x%02x%02x %02x (DraCo sernum %ld)\n",
    118  1.1  is 		rombuf[7], rombuf[6], rombuf[5], rombuf[4],
    119  1.1  is 		rombuf[3], rombuf[2], rombuf[1], rombuf[0],
    120  1.1  is 		hostid);
    121  1.1  is 
    122  1.5  is 	ugettod = dracougettod;
    123  1.5  is 	usettod = (void *)0;
    124  1.1  is 	drbbc_sc = sc;
    125  1.1  is }
    126  1.1  is 
    127  1.1  is int
    128  1.1  is draco_ds_read_bit(p)
    129  1.1  is 	void *p;
    130  1.1  is {
    131  1.1  is 	struct drioct *draco_ioct;
    132  1.1  is 
    133  1.1  is 	draco_ioct = p;
    134  1.1  is 
    135  1.1  is 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
    136  1.1  is 
    137  1.1  is 	draco_ioct->io_clockw1 = 0;
    138  1.1  is 
    139  1.1  is 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
    140  1.1  is 
    141  1.1  is 	return (draco_ioct->io_status & DRSTAT_CLKDAT);
    142  1.1  is }
    143  1.1  is 
    144  1.1  is void
    145  1.1  is draco_ds_write_bit(p, b)
    146  1.1  is 	void *p;
    147  1.1  is 	int b;
    148  1.1  is {
    149  1.1  is 	struct drioct *draco_ioct;
    150  1.1  is 
    151  1.1  is 	draco_ioct = p;
    152  1.1  is 
    153  1.1  is 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
    154  1.1  is 
    155  1.1  is 	if (b)
    156  1.1  is 		draco_ioct->io_clockw1 = 0;
    157  1.1  is 	else
    158  1.1  is 		draco_ioct->io_clockw0 = 0;
    159  1.1  is }
    160  1.1  is 
    161  1.1  is void
    162  1.1  is draco_ds_reset(p)
    163  1.1  is 	void *p;
    164  1.1  is {
    165  1.1  is 	struct drioct *draco_ioct;
    166  1.1  is 
    167  1.1  is 	draco_ioct = p;
    168  1.1  is 
    169  1.1  is 	draco_ioct->io_clockrst = 0;
    170  1.1  is }
    171  1.1  is 
    172  1.5  is int
    173  1.5  is dracougettod(tvp)
    174  1.5  is 	struct timeval *tvp;
    175  1.1  is {
    176  1.1  is 	u_int32_t clkbuf;
    177  1.5  is 	u_int32_t usecs;
    178  1.1  is 
    179  1.1  is 	drbbc_sc->sc_dsh.ds_reset(drbbc_sc->sc_dsh.ds_hw_handle);
    180  1.1  is 
    181  1.1  is 	ds_write_byte(&drbbc_sc->sc_dsh, DS_ROM_SKIP);
    182  1.1  is 
    183  1.1  is 	ds_write_byte(&drbbc_sc->sc_dsh, DS_MEM_READ_MEMORY);
    184  1.5  is 	/* address of seconds/256: */
    185  1.5  is 	ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
    186  1.1  is 	ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
    187  1.1  is 
    188  1.5  is 	usecs = (ds_read_byte(&drbbc_sc->sc_dsh) * 1000000) / 256;
    189  1.1  is 	clkbuf = ds_read_byte(&drbbc_sc->sc_dsh)
    190  1.1  is 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<8)
    191  1.1  is 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<16)
    192  1.1  is 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<24);
    193  1.1  is 
    194  1.1  is 	/* BSD time is wr. 1.1.1970; AmigaOS time wrt. 1.1.1978 */
    195  1.1  is 
    196  1.1  is 	clkbuf += (8*365 + 2) * 86400;
    197  1.1  is 
    198  1.5  is 	tvp->tv_sec = clkbuf;
    199  1.5  is 	tvp->tv_usec = usecs;
    200  1.5  is 
    201  1.5  is 	return (1);
    202  1.1  is }
    203