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