Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / amiga / dev / drbbc.c
blobc94886bc4878ab51efad6df7c2710abae6a5089f
1 /* $NetBSD: drbbc.c,v 1.17 2008/04/28 20:23:12 martin Exp $ */
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
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.
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>
42 #if 0
43 #include <machine/psl.h>
44 #endif
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;
66 struct drbbc_softc {
67 struct device sc_dev;
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;
76 int
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)
83 return (0);
85 drbbc_matched = 1;
86 return (1);
89 void
90 drbbc_attach(struct device *pdp, struct device *dp, void *auxp)
92 int i;
93 struct drbbc_softc *sc;
94 u_int8_t rombuf[8];
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);
106 for (i=0; i<8; ++i)
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],
115 hostid);
117 drbbc_sc = sc;
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;
129 draco_ioctl = p;
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);
140 void
141 draco_ds_write_bit(void *p, int b)
143 struct drioct *draco_ioctl;
145 draco_ioctl = p;
147 while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
149 if (b)
150 draco_ioctl->io_clockw1 = 0;
151 else
152 draco_ioctl->io_clockw0 = 0;
155 void
156 draco_ds_reset(void *p)
158 struct drioct *draco_ioctl;
160 draco_ioctl = p;
162 draco_ioctl->io_clockrst = 0;
166 dracougettod(todr_chip_handle_t h, struct timeval *tvp)
168 u_int32_t clkbuf;
169 u_int32_t usecs;
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;
193 return (0);
197 dracousettod(todr_chip_handle_t h, struct timeval *tvp)
199 return (ENXIO);