mcclock_pnpbus.c revision 1.3 1 /* $NetBSD: mcclock_pnpbus.c,v 1.3 2006/07/12 21:28:33 garbled Exp $ */
2 /*-
3 * Copyright (c) 2006 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Tim Rightnour
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * PNPBus driver for the mc146818 compatible time-of-day clock found on all
40 * IBM Prep machines. This one is only discernable from the motorola clock
41 * part by the interface portion of the residual data. (or by the small vendor
42 * item chip id)
43 * NOTE: The IBM machines actually have either a dallas 1385 or a dallas 1585
44 * chip. This driver should probably be replaced by something that does that
45 * one day.
46 */
47
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: mcclock_pnpbus.c,v 1.3 2006/07/12 21:28:33 garbled Exp $");
50
51 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/device.h>
55 #include <sys/malloc.h>
56
57 #include <machine/bus.h>
58 #include <machine/intr.h>
59 #include <machine/isa_machdep.h>
60 #include <machine/residual.h>
61 #include <machine/chpidpnp.h>
62 /* XXX */
63 #include <machine/pio.h>
64
65 #include <dev/clock_subr.h>
66 #include <dev/ic/mc146818reg.h>
67 #include <dev/ic/mc146818var.h>
68 #include <dev/ic/ds1687reg.h>
69
70 #include <dev/isa/isavar.h>
71
72 #include <prep/pnpbus/pnpbusvar.h>
73
74 static int mcclock_pnpbus_probe(struct device *, struct cfdata *, void *);
75 static void mcclock_pnpbus_attach(struct device *, struct device *, void *);
76
77 extern struct cfdriver mcclock_cd;
78
79 CFATTACH_DECL(mcclock_pnpbus, sizeof (struct mc146818_softc),
80 mcclock_pnpbus_probe, mcclock_pnpbus_attach, NULL, NULL);
81
82 void mcclock_pnpbus_write(struct mc146818_softc *, u_int, u_int);
83 u_int mcclock_pnpbus_read(struct mc146818_softc *, u_int);
84 void ds1585_reboot(void);
85
86 int have_ds1585 = 0;
87 #define MCCLOCK_STD_DEV 0
88
89 static int
90 mcclock_pnpbus_probe(struct device *parent, struct cfdata *match, void *aux)
91 {
92 struct pnpbus_dev_attach_args *pna = aux;
93 int ret = 0;
94
95 if (strcmp(pna->pna_devid, "PNP0B00") == 0 &&
96 pna->subtype == RealTimeClock && pna->chipid == DallasRTC)
97 ret = 1;
98 if (strcmp(pna->pna_devid, "PNP0B00") == 0 &&
99 pna->subtype == RealTimeClock && pna->chipid == Dallas1585)
100 ret = 1;
101
102 if (ret)
103 pnpbus_scan(pna, pna->pna_ppc_dev);
104 return ret;
105 }
106
107 static void
108 mcclock_pnpbus_attach(struct device *parent, struct device *self, void *aux)
109 {
110 struct mc146818_softc *sc = (void *)self;
111 struct pnpbus_dev_attach_args *pna = aux;
112
113 sc->sc_bst = pna->pna_iot;
114 if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_bst, &sc->sc_bsh)) {
115 /* XXX should we panic instead? */
116 aprint_error("%s: couldn't map clock I/O space",
117 device_xname(self));
118 return;
119 }
120
121 if (pna->chipid == Dallas1585)
122 have_ds1585 = 1;
123 sc->sc_year0 = 1900;
124 sc->sc_mcread = mcclock_pnpbus_read;
125 sc->sc_mcwrite = mcclock_pnpbus_write;
126 sc->sc_flag = MC146818_BCD;
127 mc146818_attach(sc);
128
129 aprint_normal("\n");
130
131 (*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
132 todr_attach(&sc->sc_handle);
133 }
134
135 void
136 ds1585_reboot(void)
137 {
138 struct mc146818_softc *sc = mcclock_cd.cd_devs[MCCLOCK_STD_DEV];
139 int i, j;
140
141 if (!have_ds1585)
142 return;
143
144 /* monitors b0: 05,03,01 b1: 49, WIE=1 */
145
146 (*sc->sc_mcwrite)(sc, DS1687_ASEC, 0);
147
148 /* If we are nearing 59 minutes on the hour, the math is too complex
149 * to compute out when to reboot, so we just wait for the clock to
150 * flip back over to zero, then set the alarm time.
151 */
152 i = (*sc->sc_mcread)(sc, DS1687_MIN);
153 if (i == 0x58) {
154 (*sc->sc_mcwrite)(sc, DS1687_AMIN, 0x59);
155 (*sc->sc_mcwrite)(sc, DS1687_ASEC, 0x59);
156 i = 0x59;
157 } else {
158 while (i == 0x59) {
159 delay(100);
160 i = (*sc->sc_mcread)(sc, DS1687_MIN);
161 }
162 i += 2;
163 if ((i & 0x0f) > 8)
164 i = (i & 0xf0) + 0x10;
165 (*sc->sc_mcwrite)(sc, DS1687_AMIN, i);
166 }
167 i = (*sc->sc_mcread)(sc, DS1687_HOUR);
168 (*sc->sc_mcwrite)(sc, DS1687_AHOUR, i);
169 j = (*sc->sc_mcread)(sc, DS1687_DOM); /* get date */
170 i = (*sc->sc_mcread)(sc, DS1687_CONTROLA);
171 i |= DS1687_BANK1; /* set DV0 on */
172 (*sc->sc_mcwrite)(sc, DS1687_CONTROLA, i);
173 /* write today into wakeup */
174 (*sc->sc_mcwrite)(sc, DS1687_BANK1_ADATE, j);
175 i = (*sc->sc_mcread)(sc, 0x4b); /* bank1 reg B */
176 i |= 0x82; /* WIE + ABE */
177 (*sc->sc_mcwrite)(sc, 0x4b, i);
178 i = (*sc->sc_mcread)(sc, 0x4b);
179 /* XXX power control bit on 7024/7025 */
180 outb(PREP_BUS_SPACE_IO + 0x856, 1);
181 for (;;);
182 }
183
184 void
185 mcclock_pnpbus_write(struct mc146818_softc *sc, u_int reg, u_int datum)
186 {
187 bus_space_tag_t iot;
188 bus_space_handle_t ioh;
189
190 iot = sc->sc_bst;
191 ioh = sc->sc_bsh;
192
193 bus_space_write_1(iot, ioh, 0, reg);
194 bus_space_write_1(iot, ioh, 1, datum);
195 }
196
197 u_int
198 mcclock_pnpbus_read(struct mc146818_softc *sc, u_int reg)
199 {
200 bus_space_tag_t iot;
201 bus_space_handle_t ioh;
202 u_int datum;
203
204 iot = sc->sc_bst;
205 ioh = sc->sc_bsh;
206
207 bus_space_write_1(iot, ioh, 0, reg);
208 datum = bus_space_read_1(iot, ioh, 1);
209
210 return (datum);
211 }
212