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