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