mcclock_pnpbus.c revision 1.7 1 /* $NetBSD: mcclock_pnpbus.c,v 1.7 2008/03/29 06:13:44 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.7 2008/03/29 06:13:44 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_dev = self;
114 sc->sc_bst = pna->pna_iot;
115 if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_bst, &sc->sc_bsh)) {
116 /* XXX should we panic instead? */
117 aprint_error(": couldn't map clock I/O space\n");
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 }
133
134 void
135 ds1585_reboot(void)
136 {
137 struct mc146818_softc *sc;
138 int i, j;
139
140 if (!have_ds1585)
141 return;
142
143 sc = device_private(mcclock_cd.cd_devs[MCCLOCK_STD_DEV]);
144
145 /* monitors b0: 05,03,01 b1: 49, WIE=1 */
146
147 (*sc->sc_mcwrite)(sc, DS1687_ASEC, 0);
148
149 /* If we are nearing 59 minutes on the hour, the math is too complex
150 * to compute out when to reboot, so we just wait for the clock to
151 * flip back over to zero, then set the alarm time.
152 */
153 i = (*sc->sc_mcread)(sc, DS1687_MIN);
154 if (i == 0x58) {
155 (*sc->sc_mcwrite)(sc, DS1687_AMIN, 0x59);
156 (*sc->sc_mcwrite)(sc, DS1687_ASEC, 0x59);
157 i = 0x59;
158 } else {
159 while (i == 0x59) {
160 delay(100);
161 i = (*sc->sc_mcread)(sc, DS1687_MIN);
162 }
163 i += 2;
164 if ((i & 0x0f) > 8)
165 i = (i & 0xf0) + 0x10;
166 (*sc->sc_mcwrite)(sc, DS1687_AMIN, i);
167 }
168 i = (*sc->sc_mcread)(sc, DS1687_HOUR);
169 (*sc->sc_mcwrite)(sc, DS1687_AHOUR, i);
170 j = (*sc->sc_mcread)(sc, DS1687_DOM); /* get date */
171 i = (*sc->sc_mcread)(sc, DS1687_CONTROLA);
172 i |= DS1687_BANK1; /* set DV0 on */
173 (*sc->sc_mcwrite)(sc, DS1687_CONTROLA, i);
174 /* write today into wakeup */
175 (*sc->sc_mcwrite)(sc, DS1687_BANK1_ADATE, j);
176 i = (*sc->sc_mcread)(sc, 0x4b); /* bank1 reg B */
177 i |= 0x82; /* WIE + ABE */
178 (*sc->sc_mcwrite)(sc, 0x4b, i);
179 i = (*sc->sc_mcread)(sc, 0x4b);
180 /* XXX power control bit on 7024/7025 */
181 outb(PREP_BUS_SPACE_IO + 0x856, 1);
182 for (;;);
183 }
184
185 void
186 mcclock_pnpbus_write(struct mc146818_softc *sc, u_int reg, u_int datum)
187 {
188 bus_space_tag_t iot;
189 bus_space_handle_t ioh;
190
191 iot = sc->sc_bst;
192 ioh = sc->sc_bsh;
193
194 bus_space_write_1(iot, ioh, 0, reg);
195 bus_space_write_1(iot, ioh, 1, datum);
196 }
197
198 u_int
199 mcclock_pnpbus_read(struct mc146818_softc *sc, u_int reg)
200 {
201 bus_space_tag_t iot;
202 bus_space_handle_t ioh;
203 u_int datum;
204
205 iot = sc->sc_bst;
206 ioh = sc->sc_bsh;
207
208 bus_space_write_1(iot, ioh, 0, reg);
209 datum = bus_space_read_1(iot, ioh, 1);
210
211 return (datum);
212 }
213