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