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