1*cbab9cadSchs /* $NetBSD: drbbc.c,v 1.20 2012/10/27 17:17:28 chs Exp $ */
2fc3de9d8Sis
3dc068e0dSis /*-
42c7fa23cSis * Copyright (c) 1997 The NetBSD Foundation, Inc.
5fc3de9d8Sis * All rights reserved.
6fc3de9d8Sis *
7dc068e0dSis * This code is derived from software contributed to The NetBSD Foundation
8dc068e0dSis * by Ignatios Souvatzis.
9dc068e0dSis *
10fc3de9d8Sis * Redistribution and use in source and binary forms, with or without
11fc3de9d8Sis * modification, are permitted provided that the following conditions
12fc3de9d8Sis * are met:
13fc3de9d8Sis * 1. Redistributions of source code must retain the above copyright
14fc3de9d8Sis * notice, this list of conditions and the following disclaimer.
15fc3de9d8Sis * 2. Redistributions in binary form must reproduce the above copyright
16fc3de9d8Sis * notice, this list of conditions and the following disclaimer in the
17fc3de9d8Sis * documentation and/or other materials provided with the distribution.
18fc3de9d8Sis *
19dc068e0dSis * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20dc068e0dSis * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21dc068e0dSis * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22dc068e0dSis * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23dc068e0dSis * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24dc068e0dSis * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25dc068e0dSis * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26dc068e0dSis * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27dc068e0dSis * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28dc068e0dSis * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29dc068e0dSis * POSSIBILITY OF SUCH DAMAGE.
30fc3de9d8Sis */
31fc3de9d8Sis
321ea4df81Saymeric #include <sys/cdefs.h>
33*cbab9cadSchs __KERNEL_RCSID(0, "$NetBSD: drbbc.c,v 1.20 2012/10/27 17:17:28 chs Exp $");
341ea4df81Saymeric
35fc3de9d8Sis #include <sys/param.h>
36fc3de9d8Sis #include <sys/kernel.h>
37fc3de9d8Sis #include <sys/device.h>
38fc3de9d8Sis #include <sys/systm.h>
398818afa4Sthorpej
40fc3de9d8Sis #if 0
41fc3de9d8Sis #include <machine/psl.h>
42fc3de9d8Sis #endif
43fc3de9d8Sis #include <machine/cpu.h>
44fc3de9d8Sis #include <amiga/amiga/device.h>
45fc3de9d8Sis #include <amiga/amiga/custom.h>
46fc3de9d8Sis #include <amiga/amiga/cia.h>
47fc3de9d8Sis #include <amiga/amiga/drcustom.h>
48fc3de9d8Sis #include <amiga/dev/rtc.h>
49fc3de9d8Sis
5014c9ea00Smhitch #include <dev/clock_subr.h>
51fc3de9d8Sis #include <dev/ic/ds.h>
52fc3de9d8Sis
539382c873Saymeric int draco_ds_read_bit(void *);
549382c873Saymeric void draco_ds_write_bit(void *, int);
559382c873Saymeric void draco_ds_reset(void *);
56fc3de9d8Sis
57*cbab9cadSchs void drbbc_attach(device_t, device_t, void *);
58*cbab9cadSchs int drbbc_match(device_t, cfdata_t, void *);
59fc3de9d8Sis
60471e528bStsutsui int dracougettod(todr_chip_handle_t, struct timeval *);
61471e528bStsutsui int dracousettod(todr_chip_handle_t, struct timeval *);
62fc3de9d8Sis
6314c9ea00Smhitch static struct todr_chip_handle dracotodr;
64fc3de9d8Sis struct drbbc_softc {
65fc3de9d8Sis struct ds_handle sc_dsh;
66fc3de9d8Sis };
67fc3de9d8Sis
68*cbab9cadSchs CFATTACH_DECL_NEW(drbbc, sizeof(struct drbbc_softc),
69c5e91d44Sthorpej drbbc_match, drbbc_attach, NULL, NULL);
70fc3de9d8Sis
71fc3de9d8Sis struct drbbc_softc *drbbc_sc;
72fc3de9d8Sis
73fc3de9d8Sis int
drbbc_match(device_t parent,cfdata_t cf,void * aux)74*cbab9cadSchs drbbc_match(device_t parent, cfdata_t cf, void *aux)
75fc3de9d8Sis {
768bc084e1Skleink static int drbbc_matched = 0;
778bc084e1Skleink
788bc084e1Skleink /* Allow only one instance. */
79*cbab9cadSchs if (!is_draco() || !matchname(aux, "drbbc") || drbbc_matched)
80fc3de9d8Sis return (0);
818bc084e1Skleink
828bc084e1Skleink drbbc_matched = 1;
838bc084e1Skleink return (1);
84fc3de9d8Sis }
85fc3de9d8Sis
86fc3de9d8Sis void
drbbc_attach(device_t parent,device_t self,void * aux)87*cbab9cadSchs drbbc_attach(device_t parent, device_t self, void *aux)
88fc3de9d8Sis {
89fc3de9d8Sis int i;
90fc3de9d8Sis struct drbbc_softc *sc;
91fc3de9d8Sis u_int8_t rombuf[8];
92fc3de9d8Sis
93*cbab9cadSchs sc = device_private(self);
94fc3de9d8Sis
95fc3de9d8Sis sc->sc_dsh.ds_read_bit = draco_ds_read_bit;
96fc3de9d8Sis sc->sc_dsh.ds_write_bit = draco_ds_write_bit;
97fc3de9d8Sis sc->sc_dsh.ds_reset = draco_ds_reset;
988818afa4Sthorpej sc->sc_dsh.ds_hw_handle = (void *)(DRCCADDR + DRIOCTLPG*PAGE_SIZE);
99fc3de9d8Sis
100fc3de9d8Sis sc->sc_dsh.ds_reset(sc->sc_dsh.ds_hw_handle);
101fc3de9d8Sis
102fc3de9d8Sis ds_write_byte(&sc->sc_dsh, DS_ROM_READ);
103fc3de9d8Sis for (i=0; i<8; ++i)
104fc3de9d8Sis rombuf[i] = ds_read_byte(&sc->sc_dsh);
105fc3de9d8Sis
106fc3de9d8Sis hostid = (rombuf[3] << 24) + (rombuf[2] << 16) +
107fc3de9d8Sis (rombuf[1] << 8) + rombuf[7];
108fc3de9d8Sis
109fc3de9d8Sis printf(": ROM %02x %02x%02x%02x%02x%02x%02x %02x (DraCo sernum %ld)\n",
110fc3de9d8Sis rombuf[7], rombuf[6], rombuf[5], rombuf[4],
111fc3de9d8Sis rombuf[3], rombuf[2], rombuf[1], rombuf[0],
112fc3de9d8Sis hostid);
113fc3de9d8Sis
114fc3de9d8Sis drbbc_sc = sc;
11514c9ea00Smhitch dracotodr.cookie = sc;
11614c9ea00Smhitch dracotodr.todr_gettime = dracougettod;
11714c9ea00Smhitch dracotodr.todr_settime = dracousettod;
11814c9ea00Smhitch todr_attach(&dracotodr);
119fc3de9d8Sis }
120fc3de9d8Sis
121fc3de9d8Sis int
draco_ds_read_bit(void * p)1229382c873Saymeric draco_ds_read_bit(void *p)
123fc3de9d8Sis {
124095fcda9Sjmc struct drioct *draco_ioctl;
125fc3de9d8Sis
126095fcda9Sjmc draco_ioctl = p;
127fc3de9d8Sis
128095fcda9Sjmc while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
129fc3de9d8Sis
130095fcda9Sjmc draco_ioctl->io_clockw1 = 0;
131fc3de9d8Sis
132095fcda9Sjmc while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
133fc3de9d8Sis
134095fcda9Sjmc return (draco_ioctl->io_status & DRSTAT_CLKDAT);
135fc3de9d8Sis }
136fc3de9d8Sis
137fc3de9d8Sis void
draco_ds_write_bit(void * p,int b)1389382c873Saymeric draco_ds_write_bit(void *p, int b)
139fc3de9d8Sis {
140095fcda9Sjmc struct drioct *draco_ioctl;
141fc3de9d8Sis
142095fcda9Sjmc draco_ioctl = p;
143fc3de9d8Sis
144095fcda9Sjmc while (draco_ioctl->io_status & DRSTAT_CLKBUSY);
145fc3de9d8Sis
146fc3de9d8Sis if (b)
147095fcda9Sjmc draco_ioctl->io_clockw1 = 0;
148fc3de9d8Sis else
149095fcda9Sjmc draco_ioctl->io_clockw0 = 0;
150fc3de9d8Sis }
151fc3de9d8Sis
152fc3de9d8Sis void
draco_ds_reset(void * p)1539382c873Saymeric draco_ds_reset(void *p)
154fc3de9d8Sis {
155095fcda9Sjmc struct drioct *draco_ioctl;
156fc3de9d8Sis
157095fcda9Sjmc draco_ioctl = p;
158fc3de9d8Sis
159095fcda9Sjmc draco_ioctl->io_clockrst = 0;
160fc3de9d8Sis }
161fc3de9d8Sis
162ba07737bSis int
dracougettod(todr_chip_handle_t h,struct timeval * tvp)163471e528bStsutsui dracougettod(todr_chip_handle_t h, struct timeval *tvp)
164fc3de9d8Sis {
165fc3de9d8Sis u_int32_t clkbuf;
166ba07737bSis u_int32_t usecs;
167fc3de9d8Sis
168fc3de9d8Sis drbbc_sc->sc_dsh.ds_reset(drbbc_sc->sc_dsh.ds_hw_handle);
169fc3de9d8Sis
170fc3de9d8Sis ds_write_byte(&drbbc_sc->sc_dsh, DS_ROM_SKIP);
171fc3de9d8Sis
172fc3de9d8Sis ds_write_byte(&drbbc_sc->sc_dsh, DS_MEM_READ_MEMORY);
173ba07737bSis /* address of seconds/256: */
174ba07737bSis ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
175fc3de9d8Sis ds_write_byte(&drbbc_sc->sc_dsh, 0x02);
176fc3de9d8Sis
177ba07737bSis usecs = (ds_read_byte(&drbbc_sc->sc_dsh) * 1000000) / 256;
178fc3de9d8Sis clkbuf = ds_read_byte(&drbbc_sc->sc_dsh)
179fc3de9d8Sis + (ds_read_byte(&drbbc_sc->sc_dsh)<<8)
180fc3de9d8Sis + (ds_read_byte(&drbbc_sc->sc_dsh)<<16)
181fc3de9d8Sis + (ds_read_byte(&drbbc_sc->sc_dsh)<<24);
182fc3de9d8Sis
183d6a3b8feSis /* BSD time is wrt. 1.1.1970; AmigaOS time wrt. 1.1.1978 */
184fc3de9d8Sis
185fc3de9d8Sis clkbuf += (8*365 + 2) * 86400;
186fc3de9d8Sis
187ba07737bSis tvp->tv_sec = clkbuf;
188ba07737bSis tvp->tv_usec = usecs;
189ba07737bSis
19014c9ea00Smhitch return (0);
19114c9ea00Smhitch }
19214c9ea00Smhitch
19314c9ea00Smhitch int
dracousettod(todr_chip_handle_t h,struct timeval * tvp)194471e528bStsutsui dracousettod(todr_chip_handle_t h, struct timeval *tvp)
19514c9ea00Smhitch {
1967b2d0951Smhitch return (ENXIO);
197fc3de9d8Sis }
198