xref: /netbsd-src/sys/arch/amiga/dev/drbbc.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: drbbc.c,v 1.6 2000/03/16 16:37:20 kleink 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 	static int drbbc_matched = 0;
87 
88 	/* Allow only one instance. */
89 	if (!is_draco() || !matchname(auxp, "drbbc") || drbbc_matched)
90 		return (0);
91 
92 	drbbc_matched = 1;
93 	return (1);
94 }
95 
96 void
97 drbbc_attach(pdp, dp, auxp)
98 	struct device *pdp, *dp;
99 	void *auxp;
100 {
101 	int i;
102 	struct drbbc_softc *sc;
103 	u_int8_t rombuf[8];
104 
105 	sc = (struct drbbc_softc *)dp;
106 
107 	sc->sc_dsh.ds_read_bit = draco_ds_read_bit;
108 	sc->sc_dsh.ds_write_bit = draco_ds_write_bit;
109 	sc->sc_dsh.ds_reset = draco_ds_reset;
110 	sc->sc_dsh.ds_hw_handle = (void *)(DRCCADDR + DRIOCTLPG*NBPG);
111 
112 	sc->sc_dsh.ds_reset(sc->sc_dsh.ds_hw_handle);
113 
114 	ds_write_byte(&sc->sc_dsh, DS_ROM_READ);
115 	for (i=0; i<8; ++i)
116 		rombuf[i] = ds_read_byte(&sc->sc_dsh);
117 
118 	hostid = (rombuf[3] << 24) + (rombuf[2] << 16) +
119 		(rombuf[1] << 8) + rombuf[7];
120 
121 	printf(": ROM %02x %02x%02x%02x%02x%02x%02x %02x (DraCo sernum %ld)\n",
122 		rombuf[7], rombuf[6], rombuf[5], rombuf[4],
123 		rombuf[3], rombuf[2], rombuf[1], rombuf[0],
124 		hostid);
125 
126 	ugettod = dracougettod;
127 	usettod = (void *)0;
128 	drbbc_sc = sc;
129 }
130 
131 int
132 draco_ds_read_bit(p)
133 	void *p;
134 {
135 	struct drioct *draco_ioct;
136 
137 	draco_ioct = p;
138 
139 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
140 
141 	draco_ioct->io_clockw1 = 0;
142 
143 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
144 
145 	return (draco_ioct->io_status & DRSTAT_CLKDAT);
146 }
147 
148 void
149 draco_ds_write_bit(p, b)
150 	void *p;
151 	int b;
152 {
153 	struct drioct *draco_ioct;
154 
155 	draco_ioct = p;
156 
157 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
158 
159 	if (b)
160 		draco_ioct->io_clockw1 = 0;
161 	else
162 		draco_ioct->io_clockw0 = 0;
163 }
164 
165 void
166 draco_ds_reset(p)
167 	void *p;
168 {
169 	struct drioct *draco_ioct;
170 
171 	draco_ioct = p;
172 
173 	draco_ioct->io_clockrst = 0;
174 }
175 
176 int
177 dracougettod(tvp)
178 	struct timeval *tvp;
179 {
180 	u_int32_t clkbuf;
181 	u_int32_t usecs;
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 seconds/256: */
189 	ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
190 	ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
191 
192 	usecs = (ds_read_byte(&drbbc_sc->sc_dsh) * 1000000) / 256;
193 	clkbuf = ds_read_byte(&drbbc_sc->sc_dsh)
194 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<8)
195 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<16)
196 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<24);
197 
198 	/* BSD time is wr. 1.1.1970; AmigaOS time wrt. 1.1.1978 */
199 
200 	clkbuf += (8*365 + 2) * 86400;
201 
202 	tvp->tv_sec = clkbuf;
203 	tvp->tv_usec = usecs;
204 
205 	return (1);
206 }
207