1/*	$NetBSD: ds17485.c,v 1.7 2010/12/09 04:31:57 uebayasi Exp $	*/
2
3/*
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at Sandburst Corp.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*-
33 * Copyright (c) 1990 The Regents of the University of California.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * William Jolitz and Don Ahn.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 *    notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 *    notice, this list of conditions and the following disclaimer in the
46 *    documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 *    may be used to endorse or promote products derived from this software
49 *    without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 *	@(#)clock.c	7.2 (Berkeley) 5/12/91
64 */
65/*-
66 * Copyright (c) 1993, 1994 Charles M. Hannum.
67 *
68 * This code is derived from software contributed to Berkeley by
69 * William Jolitz and Don Ahn.
70 *
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions
73 * are met:
74 * 1. Redistributions of source code must retain the above copyright
75 *    notice, this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce the above copyright
77 *    notice, this list of conditions and the following disclaimer in the
78 *    documentation and/or other materials provided with the distribution.
79 * 3. All advertising materials mentioning features or use of this software
80 *    must display the following acknowledgement:
81 *	This product includes software developed by the University of
82 *	California, Berkeley and its contributors.
83 * 4. Neither the name of the University nor the names of its contributors
84 *    may be used to endorse or promote products derived from this software
85 *    without specific prior written permission.
86 *
87 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
88 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
91 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
93 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
94 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97 * SUCH DAMAGE.
98 *
99 *	@(#)clock.c	7.2 (Berkeley) 5/12/91
100 */
101/*
102 * Mach Operating System
103 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
104 * All Rights Reserved.
105 *
106 * Permission to use, copy, modify and distribute this software and its
107 * documentation is hereby granted, provided that both the copyright
108 * notice and this permission notice appear in all copies of the
109 * software, derivative works or modified versions, and any portions
110 * thereof, and that both notices appear in supporting documentation.
111 *
112 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
113 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
114 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
115 *
116 * Carnegie Mellon requests users of this software to return to
117 *
118 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
119 *  School of Computer Science
120 *  Carnegie Mellon University
121 *  Pittsburgh PA 15213-3890
122 *
123 * any improvements or extensions that they make and grant Carnegie Mellon
124 * the rights to redistribute these changes.
125 */
126/*
127  Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.
128
129		All Rights Reserved
130
131Permission to use, copy, modify, and distribute this software and
132its documentation for any purpose and without fee is hereby
133granted, provided that the above copyright notice appears in all
134copies and that both the copyright notice and this permission notice
135appear in supporting documentation, and that the name of Intel
136not be used in advertising or publicity pertaining to distribution
137of the software without specific, written prior permission.
138
139INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
140INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
141IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
142CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
143LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
144NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
145WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
146*/
147
148/*
149 * Primitive clock interrupt routines.
150 */
151
152#include <sys/cdefs.h>
153__KERNEL_RCSID(0, "$NetBSD: ds17485.c,v 1.7 2010/12/09 04:31:57 uebayasi Exp $");
154
155#include <sys/param.h>
156#include <sys/systm.h>
157#include <sys/kernel.h>
158#include <sys/device.h>
159
160#include <machine/cpu.h>
161#include <machine/intr.h>
162#include <sys/bus.h>
163
164#include <dev/clock_subr.h>
165#include <dev/ic/mc146818reg.h>
166#include <dev/ic/mc146818var.h>
167
168#include <arch/evbppc/pmppc/dev/mainbus.h>
169#include <machine/pmppc.h>
170
171static int	rtc_match(device_t, cfdata_t, void *);
172static void	rtc_attach(device_t, device_t, void *);
173
174static u_int rtc_read(struct mc146818_softc *, u_int);
175static void rtc_write(struct mc146818_softc *, u_int, u_int);
176
177CFATTACH_DECL_NEW(rtc, sizeof(struct mc146818_softc),
178    rtc_match, rtc_attach, NULL, NULL);
179
180static int	rtc_attached = 0;
181int
182rtc_match(device_t parent, cfdata_t cf, void *aux)
183{
184	struct mainbus_attach_args *maa = aux;
185
186	return (!rtc_attached && strcmp(maa->mb_name, "rtc") == 0);
187}
188
189void
190rtc_attach(device_t parent, device_t self, void *aux)
191{
192	struct mc146818_softc *sc = device_private(self);
193	struct mainbus_attach_args *maa = aux;
194
195	sc->sc_dev = self;
196	sc->sc_bst = maa->mb_bt;
197	if (bus_space_map(sc->sc_bst, maa->mb_addr, PMPPC_RTC_SIZE, 0,
198			  &sc->sc_bsh)) {
199		aprint_error(": can't map i/o space\n");
200		return;
201	}
202
203	rtc_attached = 1;
204	rtc_write(sc, MC_REGA, MC_BASE_32_KHz | MC_RATE_1024_Hz);
205	rtc_write(sc, MC_REGB, MC_REGB_24HR);
206
207	sc->sc_mcread = rtc_read;
208	sc->sc_mcwrite = rtc_write;
209	sc->sc_flag = 0;
210	sc->sc_year0 = 1900;
211	mc146818_attach(sc);
212
213	/* aprint_normal(": Dallas Semiconductor DS17485\n"); */
214	aprint_normal("\n");
215}
216
217u_int
218rtc_read(struct mc146818_softc  *sc, u_int reg)
219{
220
221	return (bus_space_read_1(sc->sc_bst, sc->sc_bsh, reg));
222}
223
224void
225rtc_write(struct mc146818_softc *sc, u_int reg, u_int datum)
226{
227
228	bus_space_write_1(sc->sc_bst, sc->sc_bsh, reg, datum);
229}
230