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