1/*	$NetBSD: mcclock_isa.c,v 1.5 2011/07/01 20:34:53 dyoung Exp $	*/
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * William Jolitz and Don Ahn.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)clock.c	7.2 (Berkeley) 5/12/91
35 */
36/*-
37 * Copyright (c) 1993, 1994 Charles M. Hannum.
38 *
39 * This code is derived from software contributed to Berkeley by
40 * William Jolitz and Don Ahn.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 *    notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 *    notice, this list of conditions and the following disclaimer in the
49 *    documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 *    must display the following acknowledgement:
52 *	This product includes software developed by the University of
53 *	California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 *    may be used to endorse or promote products derived from this software
56 *    without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 *	@(#)clock.c	7.2 (Berkeley) 5/12/91
71 */
72/*
73 * Mach Operating System
74 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
75 * All Rights Reserved.
76 *
77 * Permission to use, copy, modify and distribute this software and its
78 * documentation is hereby granted, provided that both the copyright
79 * notice and this permission notice appear in all copies of the
80 * software, derivative works or modified versions, and any portions
81 * thereof, and that both notices appear in supporting documentation.
82 *
83 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
84 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
85 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
86 *
87 * Carnegie Mellon requests users of this software to return to
88 *
89 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
90 *  School of Computer Science
91 *  Carnegie Mellon University
92 *  Pittsburgh PA 15213-3890
93 *
94 * any improvements or extensions that they make and grant Carnegie Mellon
95 * the rights to redistribute these changes.
96 */
97/*
98  Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.
99
100		All Rights Reserved
101
102Permission to use, copy, modify, and distribute this software and
103its documentation for any purpose and without fee is hereby
104granted, provided that the above copyright notice appears in all
105copies and that both the copyright notice and this permission notice
106appear in supporting documentation, and that the name of Intel
107not be used in advertising or publicity pertaining to distribution
108of the software without specific, written prior permission.
109
110INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
111INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
112IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
113CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
114LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
115NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
116WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
117*/
118
119#include <sys/cdefs.h>
120__KERNEL_RCSID(0, "$NetBSD: mcclock_isa.c,v 1.5 2011/07/01 20:34:53 dyoung Exp $");
121
122#include <sys/param.h>
123#include <sys/systm.h>
124#include <sys/kernel.h>
125#include <sys/device.h>
126#include <sys/time.h>
127
128#include <sys/bus.h>
129
130#include <dev/clock_subr.h>
131#include <dev/isa/isareg.h>
132#include <dev/isa/isavar.h>
133#include <dev/ic/mc146818reg.h>
134#include <dev/ic/mc146818var.h>
135
136/*
137 * We only deal with the RTC portion of the mc146818 right now.  Later
138 * we might want to add support for the NVRAM portion, since it
139 * appears that some systems use that.
140 */
141
142static int mcclock_isa_probe(device_t, cfdata_t, void *);
143static void mcclock_isa_attach(device_t, device_t, void *);
144
145CFATTACH_DECL_NEW(mcclock_isa, sizeof(struct mc146818_softc),
146    mcclock_isa_probe, mcclock_isa_attach, NULL, NULL);
147
148static unsigned
149mcclock_isa_read(struct mc146818_softc *sc, unsigned reg)
150{
151
152	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg);
153	return bus_space_read_1(sc->sc_bst, sc->sc_bsh, 1);
154}
155
156static void
157mcclock_isa_write(struct mc146818_softc *sc, unsigned reg, unsigned datum)
158{
159
160	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg);
161	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, datum);
162}
163
164int
165mcclock_isa_probe(device_t parent, cfdata_t cf, void *aux)
166{
167	struct isa_attach_args *ia = aux;
168	bus_space_handle_t ioh;
169
170	if (ia->ia_nio < 1)
171		return 0;
172
173	if (ISA_DIRECT_CONFIG(ia))
174		return 0;
175
176	if ((ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) &&
177	    (ia->ia_io[0].ir_addr != IO_RTC))
178		return 0;
179
180	if (ia->ia_niomem > 0 &&
181	    (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
182		return 0;
183
184	if (ia->ia_nirq > 0 &&
185	    (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ))
186		return 0;
187
188	if (ia->ia_ndrq > 0 &&
189	    (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
190		return 0;
191
192	if (bus_space_map(ia->ia_iot, IO_RTC, 2, 0, &ioh))
193		return 0;
194	bus_space_unmap(ia->ia_iot, ioh, 2);
195
196	ia->ia_io[0].ir_addr = IO_RTC;
197	ia->ia_io[0].ir_size = 2;
198	ia->ia_nio = 1;
199
200	ia->ia_niomem = 0;
201	ia->ia_nirq = 0;
202	ia->ia_ndrq = 0;
203
204	return 1;
205}
206
207void
208mcclock_isa_attach(device_t parent, device_t self, void *aux)
209{
210	struct mc146818_softc *sc = device_private(self);
211	struct isa_attach_args *ia = aux;
212
213	sc->sc_dev = self;
214	sc->sc_bst = ia->ia_iot;
215	if (bus_space_map(sc->sc_bst, ia->ia_io[0].ir_addr,
216		ia->ia_io[0].ir_size, 0, &sc->sc_bsh)) {
217		aprint_error(": can't map registers!\n");
218		return;
219	}
220
221	/*
222	 * Select a 32KHz crystal, periodic interrupt every 1024 Hz.
223	 * XXX: We disable periodic interrupts, so why set a rate?
224	 */
225	mcclock_isa_write(sc, MC_REGA, MC_BASE_32_KHz | MC_RATE_1024_Hz);
226
227	/*
228	 * 24 Hour clock, binary-format, no auto-DST and no interrupts please.
229	 */
230	mcclock_isa_write(sc, MC_REGB, MC_REGB_24HR | MC_REGB_BINARY);
231
232	sc->sc_year0 = 1900;
233	sc->sc_flag = 0;
234	sc->sc_mcread = mcclock_isa_read;
235	sc->sc_mcwrite = mcclock_isa_write;
236#if 0
237	/*
238	 * XXX: perhaps on some systems we should manage the century byte?
239	 * For now we don't worry about it.  (Ugly MD code.)
240	 */
241	sc->sc_getcent = mcclock_isa_getcent;
242	sc->sc_setcent = mcclock_isa_setcent;
243#endif
244	mc146818_attach(sc);
245	aprint_normal("\n");
246}
247