Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / arm / footbridge / todclock.c
blob490ab140ae2598e7b2a9f32e7ac52efb2691e3b1
1 /* $NetBSD: todclock.c,v 1.13 2009/03/14 15:36:02 dsl Exp $ */
3 /*
4 * Copyright (c) 1994-1997 Mark Brinicombe.
5 * Copyright (c) 1994 Brini.
6 * All rights reserved.
8 * This code is derived from software written for Brini by Mark Brinicombe
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 Mark Brinicombe.
21 * 4. The name of the company nor the name of the author may be used to
22 * endorse or promote products derived from this software without specific
23 * prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 * RiscBSD kernel project
39 * clock.c
41 * Timer related machine specific code
43 * Created : 29/09/94
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: todclock.c,v 1.13 2009/03/14 15:36:02 dsl Exp $");
49 /* Include header files */
51 #include <sys/types.h>
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/time.h>
56 #include <sys/device.h>
58 #include <machine/rtc.h>
59 #include <arm/footbridge/todclockvar.h>
60 #include <dev/clock_subr.h>
62 #include "todclock.h"
64 #if NTODCLOCK > 1
65 #error "Can only had 1 todclock device"
66 #endif
68 static int tod_get_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
69 static int tod_set_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
72 * softc structure for the todclock device
75 struct todclock_softc {
76 struct device sc_dev; /* device node */
77 void *sc_rtc_arg; /* arg to read/write */
78 int (*sc_rtc_write)(void *, rtc_t *); /* rtc write function */
79 int (*sc_rtc_read)(void *, rtc_t *); /* rtc read function */
82 /* prototypes for functions */
84 static void todclockattach(device_t parent, device_t self, void *aux);
85 static int todclockmatch(device_t parent, cfdata_t cf, void *aux);
88 * We need to remember our softc for functions like inittodr()
89 * and resettodr()
90 * since we only ever have one time-of-day device we can just store
91 * the direct pointer to softc.
94 static struct todclock_softc *todclock_sc = NULL;
96 /* driver and attach structures */
98 CFATTACH_DECL_NEW(todclock, sizeof(struct todclock_softc),
99 todclockmatch, todclockattach, NULL, NULL);
102 * int todclockmatch(device_t parent, cfdata_t cf, void *aux)
104 * todclock device probe function.
105 * just validate the attach args
109 todclockmatch(device_t parent, cfdata_t cf, void *aux)
111 struct todclock_attach_args *ta = aux;
113 if (todclock_sc != NULL)
114 return(0);
115 if (strcmp(ta->ta_name, "todclock") != 0)
116 return(0);
118 if (ta->ta_flags & TODCLOCK_FLAG_FAKE)
119 return(1);
120 return(2);
124 * void todclockattach(device_t parent, device_t self, void *aux)
126 * todclock device attach function.
127 * Initialise the softc structure and do a search for children
130 void
131 todclockattach(device_t parent, device_t self, void *aux)
133 static struct todr_chip_handle tch;
135 struct todclock_softc *sc = (void *)self;
136 struct todclock_attach_args *ta = aux;
138 /* set up our softc */
139 todclock_sc = sc;
140 todclock_sc->sc_dev = self;
141 todclock_sc->sc_rtc_arg = ta->ta_rtc_arg;
142 todclock_sc->sc_rtc_write = ta->ta_rtc_write;
143 todclock_sc->sc_rtc_read = ta->ta_rtc_read;
145 tch.todr_gettime_ymdhms = tod_get_ymdhms;
146 tch.todr_settime_ymdhms = tod_set_ymdhms;
147 tch.todr_setwen = NULL;
149 todr_attach(&tch);
151 aprint_normal("\n");
154 static int
155 tod_set_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
157 rtc_t rtc;
158 int s;
160 rtc.rtc_cen = dt->dt_year / 100;
161 rtc.rtc_year = dt->dt_year % 100;
162 rtc.rtc_mon = dt->dt_mon + 1;
163 rtc.rtc_day = dt->dt_day + 1;
164 rtc.rtc_hour = dt->dt_hour;
165 rtc.rtc_min = dt->dt_min;
166 rtc.rtc_sec = dt->dt_sec;
167 rtc.rtc_centi = 0;
168 rtc.rtc_micro = 0;
170 aprint_normal("resettod: %02d/%02d/%02d%02d %02d:%02d:%02d\n", rtc.rtc_day,
171 rtc.rtc_mon, rtc.rtc_cen, rtc.rtc_year, rtc.rtc_hour,
172 rtc.rtc_min, rtc.rtc_sec);
174 s = splclock();
175 todclock_sc->sc_rtc_write(todclock_sc->sc_rtc_arg, &rtc);
176 splx(s);
179 static int
180 tod_get_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt)
182 rtc_t rtc;
183 int s, err;
185 s = splclock();
186 if (todclock_sc->sc_rtc_read(todclock_sc->sc_rtc_arg, &rtc) == 0) {
187 splx(s);
188 return -1;
190 splx(s);
192 dt->dt_year = rtc.rtc_cen * 100 + rtc.rtc_year;
193 dt->dt_mon = rtc.rtc_mon - 1;
194 dt->dt_day = rtc.rtc_day - 1;
195 dt->dt_hour = rtc.rtc_hour;
196 dt->dt_min = rtc.rtc_min;
197 dt->dt_sec = rtc.rtc_sec;
199 return 0;
202 /* End of todclock.c */