mcclock_pnpbus.c revision 1.5 1 /* $NetBSD: mcclock_pnpbus.c,v 1.5 2008/03/28 19:05:49 tsutsui 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.5 2008/03/28 19:05:49 tsutsui 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(device_t, cfdata_t, void *);
75 static void mcclock_pnpbus_attach(device_t, device_t, void *);
76
77 extern struct cfdriver mcclock_cd;
78
79 CFATTACH_DECL_NEW(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(device_t parent, cfdata_t cf, 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(device_t parent, device_t self, void *aux)
109 {
110 struct mc146818_softc *sc = device_private(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(": couldn't map clock I/O space\n");
117 return;
118 }
119
120 if (pna->chipid == Dallas1585)
121 have_ds1585 = 1;
122 sc->sc_year0 = 1900;
123 sc->sc_mcread = mcclock_pnpbus_read;
124 sc->sc_mcwrite = mcclock_pnpbus_write;
125 sc->sc_flag = MC146818_BCD;
126 mc146818_attach(sc);
127
128 aprint_normal("\n");
129
130 (*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
131 }
132
133 void
134 ds1585_reboot(void)
135 {
136 struct mc146818_softc *sc = mcclock_cd.cd_devs[MCCLOCK_STD_DEV];
137 int i, j;
138
139 if (!have_ds1585)
140 return;
141
142 /* monitors b0: 05,03,01 b1: 49, WIE=1 */
143
144 (*sc->sc_mcwrite)(sc, DS1687_ASEC, 0);
145
146 /* If we are nearing 59 minutes on the hour, the math is too complex
147 * to compute out when to reboot, so we just wait for the clock to
148 * flip back over to zero, then set the alarm time.
149 */
150 i = (*sc->sc_mcread)(sc, DS1687_MIN);
151 if (i == 0x58) {
152 (*sc->sc_mcwrite)(sc, DS1687_AMIN, 0x59);
153 (*sc->sc_mcwrite)(sc, DS1687_ASEC, 0x59);
154 i = 0x59;
155 } else {
156 while (i == 0x59) {
157 delay(100);
158 i = (*sc->sc_mcread)(sc, DS1687_MIN);
159 }
160 i += 2;
161 if ((i & 0x0f) > 8)
162 i = (i & 0xf0) + 0x10;
163 (*sc->sc_mcwrite)(sc, DS1687_AMIN, i);
164 }
165 i = (*sc->sc_mcread)(sc, DS1687_HOUR);
166 (*sc->sc_mcwrite)(sc, DS1687_AHOUR, i);
167 j = (*sc->sc_mcread)(sc, DS1687_DOM); /* get date */
168 i = (*sc->sc_mcread)(sc, DS1687_CONTROLA);
169 i |= DS1687_BANK1; /* set DV0 on */
170 (*sc->sc_mcwrite)(sc, DS1687_CONTROLA, i);
171 /* write today into wakeup */
172 (*sc->sc_mcwrite)(sc, DS1687_BANK1_ADATE, j);
173 i = (*sc->sc_mcread)(sc, 0x4b); /* bank1 reg B */
174 i |= 0x82; /* WIE + ABE */
175 (*sc->sc_mcwrite)(sc, 0x4b, i);
176 i = (*sc->sc_mcread)(sc, 0x4b);
177 /* XXX power control bit on 7024/7025 */
178 outb(PREP_BUS_SPACE_IO + 0x856, 1);
179 for (;;);
180 }
181
182 void
183 mcclock_pnpbus_write(struct mc146818_softc *sc, u_int reg, u_int datum)
184 {
185 bus_space_tag_t iot;
186 bus_space_handle_t ioh;
187
188 iot = sc->sc_bst;
189 ioh = sc->sc_bsh;
190
191 bus_space_write_1(iot, ioh, 0, reg);
192 bus_space_write_1(iot, ioh, 1, datum);
193 }
194
195 u_int
196 mcclock_pnpbus_read(struct mc146818_softc *sc, u_int reg)
197 {
198 bus_space_tag_t iot;
199 bus_space_handle_t ioh;
200 u_int datum;
201
202 iot = sc->sc_bst;
203 ioh = sc->sc_bsh;
204
205 bus_space_write_1(iot, ioh, 0, reg);
206 datum = bus_space_read_1(iot, ioh, 1);
207
208 return (datum);
209 }
210