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