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