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