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