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