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