1 /* $NetBSD: drbbc.c,v 1.17 2008/04/28 20:23:12 martin Exp $ */
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ignatios Souvatzis.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drbbc.c,v 1.17 2008/04/28 20:23:12 martin Exp $");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
40 #include <uvm/uvm_extern.h>
43 #include <machine/psl.h>
45 #include <machine/cpu.h>
46 #include <amiga/amiga/device.h>
47 #include <amiga/amiga/custom.h>
48 #include <amiga/amiga/cia.h>
49 #include <amiga/amiga/drcustom.h>
50 #include <amiga/dev/rtc.h>
52 #include <dev/clock_subr.h>
53 #include <dev/ic/ds.h>
55 int draco_ds_read_bit(void *);
56 void draco_ds_write_bit(void *, int);
57 void draco_ds_reset(void *);
59 void drbbc_attach(struct device
*, struct device
*, void *);
60 int drbbc_match(struct device
*, struct cfdata
*, void *);
62 int dracougettod(todr_chip_handle_t
, struct timeval
*);
63 int dracousettod(todr_chip_handle_t
, struct timeval
*);
65 static struct todr_chip_handle dracotodr
;
68 struct ds_handle sc_dsh
;
71 CFATTACH_DECL(drbbc
, sizeof(struct drbbc_softc
),
72 drbbc_match
, drbbc_attach
, NULL
, NULL
);
74 struct drbbc_softc
*drbbc_sc
;
77 drbbc_match(struct device
*pdp
, struct cfdata
*cfp
, void *auxp
)
79 static int drbbc_matched
= 0;
81 /* Allow only one instance. */
82 if (!is_draco() || !matchname(auxp
, "drbbc") || drbbc_matched
)
90 drbbc_attach(struct device
*pdp
, struct device
*dp
, void *auxp
)
93 struct drbbc_softc
*sc
;
96 sc
= (struct drbbc_softc
*)dp
;
98 sc
->sc_dsh
.ds_read_bit
= draco_ds_read_bit
;
99 sc
->sc_dsh
.ds_write_bit
= draco_ds_write_bit
;
100 sc
->sc_dsh
.ds_reset
= draco_ds_reset
;
101 sc
->sc_dsh
.ds_hw_handle
= (void *)(DRCCADDR
+ DRIOCTLPG
*PAGE_SIZE
);
103 sc
->sc_dsh
.ds_reset(sc
->sc_dsh
.ds_hw_handle
);
105 ds_write_byte(&sc
->sc_dsh
, DS_ROM_READ
);
107 rombuf
[i
] = ds_read_byte(&sc
->sc_dsh
);
109 hostid
= (rombuf
[3] << 24) + (rombuf
[2] << 16) +
110 (rombuf
[1] << 8) + rombuf
[7];
112 printf(": ROM %02x %02x%02x%02x%02x%02x%02x %02x (DraCo sernum %ld)\n",
113 rombuf
[7], rombuf
[6], rombuf
[5], rombuf
[4],
114 rombuf
[3], rombuf
[2], rombuf
[1], rombuf
[0],
118 dracotodr
.cookie
= sc
;
119 dracotodr
.todr_gettime
= dracougettod
;
120 dracotodr
.todr_settime
= dracousettod
;
121 todr_attach(&dracotodr
);
125 draco_ds_read_bit(void *p
)
127 struct drioct
*draco_ioctl
;
131 while (draco_ioctl
->io_status
& DRSTAT_CLKBUSY
);
133 draco_ioctl
->io_clockw1
= 0;
135 while (draco_ioctl
->io_status
& DRSTAT_CLKBUSY
);
137 return (draco_ioctl
->io_status
& DRSTAT_CLKDAT
);
141 draco_ds_write_bit(void *p
, int b
)
143 struct drioct
*draco_ioctl
;
147 while (draco_ioctl
->io_status
& DRSTAT_CLKBUSY
);
150 draco_ioctl
->io_clockw1
= 0;
152 draco_ioctl
->io_clockw0
= 0;
156 draco_ds_reset(void *p
)
158 struct drioct
*draco_ioctl
;
162 draco_ioctl
->io_clockrst
= 0;
166 dracougettod(todr_chip_handle_t h
, struct timeval
*tvp
)
171 drbbc_sc
->sc_dsh
.ds_reset(drbbc_sc
->sc_dsh
.ds_hw_handle
);
173 ds_write_byte(&drbbc_sc
->sc_dsh
, DS_ROM_SKIP
);
175 ds_write_byte(&drbbc_sc
->sc_dsh
, DS_MEM_READ_MEMORY
);
176 /* address of seconds/256: */
177 ds_write_byte(&drbbc_sc
->sc_dsh
, 0x02);
178 ds_write_byte(&drbbc_sc
->sc_dsh
, 0x02);
180 usecs
= (ds_read_byte(&drbbc_sc
->sc_dsh
) * 1000000) / 256;
181 clkbuf
= ds_read_byte(&drbbc_sc
->sc_dsh
)
182 + (ds_read_byte(&drbbc_sc
->sc_dsh
)<<8)
183 + (ds_read_byte(&drbbc_sc
->sc_dsh
)<<16)
184 + (ds_read_byte(&drbbc_sc
->sc_dsh
)<<24);
186 /* BSD time is wrt. 1.1.1970; AmigaOS time wrt. 1.1.1978 */
188 clkbuf
+= (8*365 + 2) * 86400;
190 tvp
->tv_sec
= clkbuf
;
191 tvp
->tv_usec
= usecs
;
197 dracousettod(todr_chip_handle_t h
, struct timeval
*tvp
)