drbbc.c revision 1.2 1 1.2 thorpej /* $NetBSD: drbbc.c,v 1.2 1998/01/12 10:39:19 thorpej Exp $ */
2 1.1 is
3 1.1 is /*
4 1.1 is * Copyright (c) 1997 Ignatios Souvatzis.
5 1.1 is * All rights reserved.
6 1.1 is *
7 1.1 is * Redistribution and use in source and binary forms, with or without
8 1.1 is * modification, are permitted provided that the following conditions
9 1.1 is * are met:
10 1.1 is * 1. Redistributions of source code must retain the above copyright
11 1.1 is * notice, this list of conditions and the following disclaimer.
12 1.1 is * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 is * notice, this list of conditions and the following disclaimer in the
14 1.1 is * documentation and/or other materials provided with the distribution.
15 1.1 is * 3. All advertising materials mentioning features or use of this software
16 1.1 is * must display the following acknowledgement:
17 1.1 is * This product includes software developed by Ignatios Souvatzis
18 1.1 is * for the NetBSD project.
19 1.1 is * 4. The name of the author may not be used to endorse or promote
20 1.1 is * products derived from this software without specific prior written
21 1.1 is * permission.
22 1.1 is *
23 1.1 is * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
24 1.1 is * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 is * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 is * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
27 1.1 is * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 is * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 is * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 is * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 is * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 is * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 is * SUCH DAMAGE.
34 1.1 is *
35 1.1 is */
36 1.1 is
37 1.1 is #include <sys/param.h>
38 1.1 is #include <sys/kernel.h>
39 1.1 is #include <sys/device.h>
40 1.1 is #include <sys/systm.h>
41 1.1 is #if 0
42 1.1 is #include <machine/psl.h>
43 1.1 is #endif
44 1.1 is #include <machine/cpu.h>
45 1.1 is #include <amiga/amiga/device.h>
46 1.1 is #include <amiga/amiga/custom.h>
47 1.1 is #include <amiga/amiga/cia.h>
48 1.1 is #include <amiga/amiga/drcustom.h>
49 1.1 is #include <amiga/dev/rtc.h>
50 1.1 is
51 1.1 is #include <dev/ic/ds.h>
52 1.1 is
53 1.1 is int draco_ds_read_bit __P((void *));
54 1.1 is void draco_ds_write_bit __P((void *, int));
55 1.1 is void draco_ds_reset __P((void *));
56 1.1 is
57 1.1 is void drbbc_attach __P((struct device *, struct device *, void *));
58 1.1 is int drbbc_match __P((struct device *, struct cfdata *, void *));
59 1.1 is
60 1.1 is time_t dracogettod __P((void));
61 1.1 is #ifdef __NOTYET__
62 1.1 is int dracosettod __P((time_t));
63 1.1 is #endif
64 1.1 is
65 1.1 is struct drbbc_softc {
66 1.1 is struct device sc_dev;
67 1.1 is struct ds_handle sc_dsh;
68 1.1 is };
69 1.1 is
70 1.1 is struct cfattach drbbc_ca = {
71 1.1 is sizeof(struct drbbc_softc),
72 1.1 is drbbc_match,
73 1.1 is drbbc_attach
74 1.1 is };
75 1.1 is
76 1.1 is struct drbbc_softc *drbbc_sc;
77 1.1 is
78 1.1 is int
79 1.1 is drbbc_match(pdp, cfp, auxp)
80 1.1 is struct device *pdp;
81 1.1 is struct cfdata *cfp;
82 1.1 is void *auxp;
83 1.1 is {
84 1.1 is if (is_draco() && matchname(auxp, "drbbc") && (cfp->cf_unit == 0))
85 1.1 is return (1);
86 1.1 is else
87 1.1 is return (0);
88 1.1 is }
89 1.1 is
90 1.1 is void
91 1.1 is drbbc_attach(pdp, dp, auxp)
92 1.1 is struct device *pdp, *dp;
93 1.1 is void *auxp;
94 1.1 is {
95 1.1 is int i;
96 1.1 is struct drbbc_softc *sc;
97 1.1 is u_int8_t rombuf[8];
98 1.1 is
99 1.1 is sc = (struct drbbc_softc *)dp;
100 1.1 is
101 1.1 is sc->sc_dsh.ds_read_bit = draco_ds_read_bit;
102 1.1 is sc->sc_dsh.ds_write_bit = draco_ds_write_bit;
103 1.1 is sc->sc_dsh.ds_reset = draco_ds_reset;
104 1.1 is sc->sc_dsh.ds_hw_handle = (void *)(DRCCADDR + DRIOCTLPG*NBPG);
105 1.1 is
106 1.1 is sc->sc_dsh.ds_reset(sc->sc_dsh.ds_hw_handle);
107 1.1 is
108 1.1 is ds_write_byte(&sc->sc_dsh, DS_ROM_READ);
109 1.1 is for (i=0; i<8; ++i)
110 1.1 is rombuf[i] = ds_read_byte(&sc->sc_dsh);
111 1.1 is
112 1.1 is hostid = (rombuf[3] << 24) + (rombuf[2] << 16) +
113 1.1 is (rombuf[1] << 8) + rombuf[7];
114 1.1 is
115 1.1 is printf(": ROM %02x %02x%02x%02x%02x%02x%02x %02x (DraCo sernum %ld)\n",
116 1.1 is rombuf[7], rombuf[6], rombuf[5], rombuf[4],
117 1.1 is rombuf[3], rombuf[2], rombuf[1], rombuf[0],
118 1.1 is hostid);
119 1.1 is
120 1.1 is gettod = dracogettod;
121 1.1 is settod = (void *)0;
122 1.1 is drbbc_sc = sc;
123 1.1 is }
124 1.1 is
125 1.1 is int
126 1.1 is draco_ds_read_bit(p)
127 1.1 is void *p;
128 1.1 is {
129 1.1 is struct drioct *draco_ioct;
130 1.1 is
131 1.1 is draco_ioct = p;
132 1.1 is
133 1.1 is while (draco_ioct->io_status & DRSTAT_CLKBUSY);
134 1.1 is
135 1.1 is draco_ioct->io_clockw1 = 0;
136 1.1 is
137 1.1 is while (draco_ioct->io_status & DRSTAT_CLKBUSY);
138 1.1 is
139 1.1 is return (draco_ioct->io_status & DRSTAT_CLKDAT);
140 1.1 is }
141 1.1 is
142 1.1 is void
143 1.1 is draco_ds_write_bit(p, b)
144 1.1 is void *p;
145 1.1 is int b;
146 1.1 is {
147 1.1 is struct drioct *draco_ioct;
148 1.1 is
149 1.1 is draco_ioct = p;
150 1.1 is
151 1.1 is while (draco_ioct->io_status & DRSTAT_CLKBUSY);
152 1.1 is
153 1.1 is if (b)
154 1.1 is draco_ioct->io_clockw1 = 0;
155 1.1 is else
156 1.1 is draco_ioct->io_clockw0 = 0;
157 1.1 is }
158 1.1 is
159 1.1 is void
160 1.1 is draco_ds_reset(p)
161 1.1 is void *p;
162 1.1 is {
163 1.1 is struct drioct *draco_ioct;
164 1.1 is
165 1.1 is draco_ioct = p;
166 1.1 is
167 1.1 is draco_ioct->io_clockrst = 0;
168 1.1 is }
169 1.1 is
170 1.1 is /*
171 1.1 is * We could return 1/256 of a seconds, but would need to change the interface
172 1.1 is */
173 1.1 is
174 1.1 is time_t
175 1.1 is dracogettod()
176 1.1 is {
177 1.1 is u_int32_t clkbuf;
178 1.1 is
179 1.1 is drbbc_sc->sc_dsh.ds_reset(drbbc_sc->sc_dsh.ds_hw_handle);
180 1.1 is
181 1.1 is ds_write_byte(&drbbc_sc->sc_dsh, DS_ROM_SKIP);
182 1.1 is
183 1.1 is ds_write_byte(&drbbc_sc->sc_dsh, DS_MEM_READ_MEMORY);
184 1.1 is /* address of full seconds: */
185 1.1 is ds_write_byte(&drbbc_sc->sc_dsh, 0x03);
186 1.1 is ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
187 1.1 is
188 1.1 is clkbuf = ds_read_byte(&drbbc_sc->sc_dsh)
189 1.1 is + (ds_read_byte(&drbbc_sc->sc_dsh)<<8)
190 1.1 is + (ds_read_byte(&drbbc_sc->sc_dsh)<<16)
191 1.1 is + (ds_read_byte(&drbbc_sc->sc_dsh)<<24);
192 1.1 is
193 1.1 is /* BSD time is wr. 1.1.1970; AmigaOS time wrt. 1.1.1978 */
194 1.1 is
195 1.1 is clkbuf += (8*365 + 2) * 86400;
196 1.1 is
197 1.1 is return ((time_t)clkbuf);
198 1.1 is }
199