xref: /netbsd-src/sys/arch/arm/clps711x/clpsrtc.c (revision 0514024aff015d4b9c1f3221b3b09eaddae748dd)
1 /*      $NetBSD: clpsrtc.c,v 1.1 2013/04/28 11:57:13 kiyohara Exp $      */
2 /*
3  * Copyright (c) 2013 KIYOHARA Takashi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: clpsrtc.c,v 1.1 2013/04/28 11:57:13 kiyohara Exp $");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/device.h>
33 #include <sys/errno.h>
34 
35 #include <dev/clock_subr.h>
36 
37 #include <arm/clps711x/clps711xreg.h>
38 #include <arm/clps711x/clpssocvar.h>
39 
40 #include "locators.h"
41 
42 struct clpsrtc_softc {
43 	device_t sc_dev;
44 	bus_space_tag_t sc_iot;
45 	bus_space_handle_t sc_ioh;
46 
47 	struct todr_chip_handle sc_todr;
48 };
49 
50 static int clpsrtc_match(device_t, cfdata_t, void *);
51 static void clpsrtc_attach(device_t, device_t, void *);
52 
53 /* todr(9) interfaces */
54 static int clpsrtc_todr_gettime(todr_chip_handle_t, struct timeval *);
55 static int clpsrtc_todr_settime(todr_chip_handle_t, struct timeval *);
56 
57 CFATTACH_DECL_NEW(clpsrtc, sizeof(struct clpsrtc_softc),
58     clpsrtc_match, clpsrtc_attach, NULL, NULL);
59 
60 
61 /* ARGSUSED */
62 static int
clpsrtc_match(device_t parent,cfdata_t match,void * aux)63 clpsrtc_match(device_t parent, cfdata_t match, void *aux)
64 {
65 
66 	return 1;
67 }
68 
69 /* ARGSUSED */
70 static void
clpsrtc_attach(device_t parent,device_t self,void * aux)71 clpsrtc_attach(device_t parent, device_t self, void *aux)
72 {
73 	struct clpsrtc_softc *sc = device_private(self);
74 	struct clpssoc_attach_args *aa = aux;
75 
76 	aprint_naive("\n");
77 	aprint_normal("\n");
78 
79 	sc->sc_dev = self;
80 	sc->sc_iot = aa->aa_iot;
81 	sc->sc_ioh = *aa->aa_ioh;
82 
83 	/* XXXX: reset to compare registers */
84 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, PS711X_RTCMR, 0);
85 
86 	sc->sc_todr.cookie = sc;
87 	sc->sc_todr.todr_gettime = clpsrtc_todr_gettime;
88 	sc->sc_todr.todr_settime = clpsrtc_todr_settime;
89 	sc->sc_todr.todr_setwen = NULL;
90 	todr_attach(&sc->sc_todr);
91 }
92 
93 static int
clpsrtc_todr_gettime(todr_chip_handle_t ch,struct timeval * tv)94 clpsrtc_todr_gettime(todr_chip_handle_t ch, struct timeval *tv)
95 {
96 	struct clpsrtc_softc *sc = ch->cookie;
97 
98 	tv->tv_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, PS711X_RTCDR);
99 	tv->tv_usec = 0;
100 	return 0;
101 }
102 
103 static int
clpsrtc_todr_settime(todr_chip_handle_t ch,struct timeval * tv)104 clpsrtc_todr_settime(todr_chip_handle_t ch, struct timeval *tv)
105 {
106 	struct clpsrtc_softc *sc = ch->cookie;
107 
108 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, PS711X_RTCDR, tv->tv_sec);
109 	return 0;
110 }
111