1 /* $NetBSD: mcclock_pnpbus.c,v 1.8 2008/03/29 17:52:46 matt Exp $ */
3 * Copyright (c) 2006 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 * PNPBus driver for the mc146818 compatible time-of-day clock found on all
33 * IBM Prep machines. This one is only discernable from the motorola clock
34 * part by the interface portion of the residual data. (or by the small vendor
36 * NOTE: The IBM machines actually have either a dallas 1385 or a dallas 1585
37 * chip. This driver should probably be replaced by something that does that
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: mcclock_pnpbus.c,v 1.8 2008/03/29 17:52:46 matt Exp $");
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/malloc.h>
50 #include <machine/bus.h>
51 #include <machine/intr.h>
52 #include <machine/isa_machdep.h>
53 #include <machine/residual.h>
54 #include <machine/chpidpnp.h>
56 #include <machine/pio.h>
58 #include <dev/clock_subr.h>
59 #include <dev/ic/mc146818reg.h>
60 #include <dev/ic/mc146818var.h>
61 #include <dev/ic/ds1687reg.h>
63 #include <dev/isa/isavar.h>
65 #include <prep/pnpbus/pnpbusvar.h>
67 static int mcclock_pnpbus_probe(device_t
, cfdata_t
, void *);
68 static void mcclock_pnpbus_attach(device_t
, device_t
, void *);
70 extern struct cfdriver mcclock_cd
;
72 CFATTACH_DECL_NEW(mcclock_pnpbus
, sizeof(struct mc146818_softc
),
73 mcclock_pnpbus_probe
, mcclock_pnpbus_attach
, NULL
, NULL
);
75 void mcclock_pnpbus_write(struct mc146818_softc
*, u_int
, u_int
);
76 u_int
mcclock_pnpbus_read(struct mc146818_softc
*, u_int
);
77 void ds1585_reboot(void);
80 #define MCCLOCK_STD_DEV 0
83 mcclock_pnpbus_probe(device_t parent
, cfdata_t cf
, void *aux
)
85 struct pnpbus_dev_attach_args
*pna
= aux
;
88 if (strcmp(pna
->pna_devid
, "PNP0B00") == 0 &&
89 pna
->subtype
== RealTimeClock
&& pna
->chipid
== DallasRTC
)
91 if (strcmp(pna
->pna_devid
, "PNP0B00") == 0 &&
92 pna
->subtype
== RealTimeClock
&& pna
->chipid
== Dallas1585
)
96 pnpbus_scan(pna
, pna
->pna_ppc_dev
);
101 mcclock_pnpbus_attach(device_t parent
, device_t self
, void *aux
)
103 struct mc146818_softc
*sc
= device_private(self
);
104 struct pnpbus_dev_attach_args
*pna
= aux
;
107 sc
->sc_bst
= pna
->pna_iot
;
108 if (pnpbus_io_map(&pna
->pna_res
, 0, &sc
->sc_bst
, &sc
->sc_bsh
)) {
109 /* XXX should we panic instead? */
110 aprint_error(": couldn't map clock I/O space\n");
114 if (pna
->chipid
== Dallas1585
)
117 sc
->sc_mcread
= mcclock_pnpbus_read
;
118 sc
->sc_mcwrite
= mcclock_pnpbus_write
;
119 sc
->sc_flag
= MC146818_BCD
;
124 (*sc
->sc_mcwrite
)(sc
, MC_REGB
, MC_REGB_24HR
);
130 struct mc146818_softc
*sc
;
136 sc
= device_lookup_private(&mcclock_cd
, MCCLOCK_STD_DEV
);
138 /* monitors b0: 05,03,01 b1: 49, WIE=1 */
140 (*sc
->sc_mcwrite
)(sc
, DS1687_ASEC
, 0);
142 /* If we are nearing 59 minutes on the hour, the math is too complex
143 * to compute out when to reboot, so we just wait for the clock to
144 * flip back over to zero, then set the alarm time.
146 i
= (*sc
->sc_mcread
)(sc
, DS1687_MIN
);
148 (*sc
->sc_mcwrite
)(sc
, DS1687_AMIN
, 0x59);
149 (*sc
->sc_mcwrite
)(sc
, DS1687_ASEC
, 0x59);
154 i
= (*sc
->sc_mcread
)(sc
, DS1687_MIN
);
158 i
= (i
& 0xf0) + 0x10;
159 (*sc
->sc_mcwrite
)(sc
, DS1687_AMIN
, i
);
161 i
= (*sc
->sc_mcread
)(sc
, DS1687_HOUR
);
162 (*sc
->sc_mcwrite
)(sc
, DS1687_AHOUR
, i
);
163 j
= (*sc
->sc_mcread
)(sc
, DS1687_DOM
); /* get date */
164 i
= (*sc
->sc_mcread
)(sc
, DS1687_CONTROLA
);
165 i
|= DS1687_BANK1
; /* set DV0 on */
166 (*sc
->sc_mcwrite
)(sc
, DS1687_CONTROLA
, i
);
167 /* write today into wakeup */
168 (*sc
->sc_mcwrite
)(sc
, DS1687_BANK1_ADATE
, j
);
169 i
= (*sc
->sc_mcread
)(sc
, 0x4b); /* bank1 reg B */
170 i
|= 0x82; /* WIE + ABE */
171 (*sc
->sc_mcwrite
)(sc
, 0x4b, i
);
172 i
= (*sc
->sc_mcread
)(sc
, 0x4b);
173 /* XXX power control bit on 7024/7025 */
174 outb(PREP_BUS_SPACE_IO
+ 0x856, 1);
179 mcclock_pnpbus_write(struct mc146818_softc
*sc
, u_int reg
, u_int datum
)
182 bus_space_handle_t ioh
;
187 bus_space_write_1(iot
, ioh
, 0, reg
);
188 bus_space_write_1(iot
, ioh
, 1, datum
);
192 mcclock_pnpbus_read(struct mc146818_softc
*sc
, u_int reg
)
195 bus_space_handle_t ioh
;
201 bus_space_write_1(iot
, ioh
, 0, reg
);
202 datum
= bus_space_read_1(iot
, ioh
, 1);