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