xref: /netbsd/sys/arch/amiga/dev/drbbc.c (revision bf9ec67e)
1 /*	$NetBSD: drbbc.c,v 1.8 2002/01/28 09:56:53 aymeric 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/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: drbbc.c,v 1.8 2002/01/28 09:56:53 aymeric Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 #include <sys/systm.h>
46 #if 0
47 #include <machine/psl.h>
48 #endif
49 #include <machine/cpu.h>
50 #include <amiga/amiga/device.h>
51 #include <amiga/amiga/custom.h>
52 #include <amiga/amiga/cia.h>
53 #include <amiga/amiga/drcustom.h>
54 #include <amiga/dev/rtc.h>
55 
56 #include <dev/ic/ds.h>
57 
58 int draco_ds_read_bit(void *);
59 void draco_ds_write_bit(void *, int);
60 void draco_ds_reset(void *);
61 
62 void drbbc_attach(struct device *, struct device *, void *);
63 int drbbc_match(struct device *, struct cfdata *, void *);
64 
65 int dracougettod(struct timeval *);
66 #ifdef __NOTYET__
67 int dracousettod(struct timeval *);
68 #endif
69 
70 struct drbbc_softc {
71 	struct device sc_dev;
72 	struct ds_handle sc_dsh;
73 };
74 
75 struct cfattach drbbc_ca = {
76 	sizeof(struct drbbc_softc),
77 	drbbc_match,
78 	drbbc_attach
79 };
80 
81 struct drbbc_softc *drbbc_sc;
82 
83 int
84 drbbc_match(struct device *pdp, struct cfdata *cfp, 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(struct device *pdp, struct device *dp, 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 	ugettod = dracougettod;
125 	usettod = (void *)0;
126 	drbbc_sc = sc;
127 }
128 
129 int
130 draco_ds_read_bit(void *p)
131 {
132 	struct drioct *draco_ioct;
133 
134 	draco_ioct = p;
135 
136 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
137 
138 	draco_ioct->io_clockw1 = 0;
139 
140 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
141 
142 	return (draco_ioct->io_status & DRSTAT_CLKDAT);
143 }
144 
145 void
146 draco_ds_write_bit(void *p, int b)
147 {
148 	struct drioct *draco_ioct;
149 
150 	draco_ioct = p;
151 
152 	while (draco_ioct->io_status & DRSTAT_CLKBUSY);
153 
154 	if (b)
155 		draco_ioct->io_clockw1 = 0;
156 	else
157 		draco_ioct->io_clockw0 = 0;
158 }
159 
160 void
161 draco_ds_reset(void *p)
162 {
163 	struct drioct *draco_ioct;
164 
165 	draco_ioct = p;
166 
167 	draco_ioct->io_clockrst = 0;
168 }
169 
170 int
171 dracougettod(struct timeval *tvp)
172 {
173 	u_int32_t clkbuf;
174 	u_int32_t usecs;
175 
176 	drbbc_sc->sc_dsh.ds_reset(drbbc_sc->sc_dsh.ds_hw_handle);
177 
178 	ds_write_byte(&drbbc_sc->sc_dsh, DS_ROM_SKIP);
179 
180 	ds_write_byte(&drbbc_sc->sc_dsh, DS_MEM_READ_MEMORY);
181 	/* address of seconds/256: */
182 	ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
183 	ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
184 
185 	usecs = (ds_read_byte(&drbbc_sc->sc_dsh) * 1000000) / 256;
186 	clkbuf = ds_read_byte(&drbbc_sc->sc_dsh)
187 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<8)
188 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<16)
189 	    + (ds_read_byte(&drbbc_sc->sc_dsh)<<24);
190 
191 	/* BSD time is wr. 1.1.1970; AmigaOS time wrt. 1.1.1978 */
192 
193 	clkbuf += (8*365 + 2) * 86400;
194 
195 	tvp->tv_sec = clkbuf;
196 	tvp->tv_usec = usecs;
197 
198 	return (1);
199 }
200