mkclock.c revision 1.13 1 1.13 thorpej /* $NetBSD: mkclock.c,v 1.13 2025/09/07 21:45:14 thorpej Exp $ */
2 1.1 wdk
3 1.1 wdk /*-
4 1.1 wdk * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 wdk * All rights reserved.
6 1.1 wdk *
7 1.1 wdk * This code is derived from software contributed to The NetBSD Foundation
8 1.1 wdk * by Wayne Knowles
9 1.1 wdk *
10 1.1 wdk * Redistribution and use in source and binary forms, with or without
11 1.1 wdk * modification, are permitted provided that the following conditions
12 1.1 wdk * are met:
13 1.1 wdk * 1. Redistributions of source code must retain the above copyright
14 1.1 wdk * notice, this list of conditions and the following disclaimer.
15 1.1 wdk * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 wdk * notice, this list of conditions and the following disclaimer in the
17 1.1 wdk * documentation and/or other materials provided with the distribution.
18 1.1 wdk *
19 1.1 wdk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 wdk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 wdk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 wdk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 wdk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 wdk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 wdk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 wdk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 wdk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 wdk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 wdk * POSSIBILITY OF SUCH DAMAGE.
30 1.1 wdk */
31 1.5 lukem
32 1.5 lukem #include <sys/cdefs.h>
33 1.13 thorpej __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.13 2025/09/07 21:45:14 thorpej Exp $");
34 1.1 wdk
35 1.1 wdk #include <sys/param.h>
36 1.1 wdk #include <sys/kernel.h>
37 1.1 wdk #include <sys/device.h>
38 1.1 wdk #include <sys/systm.h>
39 1.1 wdk
40 1.1 wdk #include <dev/clock_subr.h>
41 1.1 wdk
42 1.1 wdk #include <machine/cpu.h>
43 1.1 wdk #include <machine/mainboard.h>
44 1.1 wdk #include <machine/autoconf.h>
45 1.1 wdk #include <machine/sysconf.h>
46 1.1 wdk #include <machine/bus.h>
47 1.1 wdk
48 1.1 wdk #include <mipsco/obio/clockreg.h>
49 1.1 wdk
50 1.1 wdk struct mkclock_softc {
51 1.13 thorpej device_t sc_dev;
52 1.1 wdk bus_space_tag_t sc_bst;
53 1.1 wdk bus_space_handle_t sc_bsh;
54 1.7 gdamore struct todr_chip_handle sc_todr;
55 1.1 wdk };
56 1.1 wdk
57 1.11 chs static int mkclock_match (device_t, cfdata_t, void *);
58 1.11 chs static void mkclock_attach (device_t, device_t, void *);
59 1.1 wdk
60 1.11 chs CFATTACH_DECL_NEW(mkclock, sizeof(struct mkclock_softc),
61 1.4 thorpej mkclock_match, mkclock_attach, NULL, NULL);
62 1.1 wdk
63 1.7 gdamore int mkclock_read (todr_chip_handle_t, struct clock_ymdhms *);
64 1.7 gdamore int mkclock_write (todr_chip_handle_t, struct clock_ymdhms *);
65 1.2 matt
66 1.2 matt static int mk_read (struct mkclock_softc *, int);
67 1.2 matt static void mk_write (struct mkclock_softc *, int, int);
68 1.1 wdk
69 1.1 wdk int
70 1.11 chs mkclock_match(device_t parent, cfdata_t cf, void *aux)
71 1.1 wdk {
72 1.1 wdk return 1;
73 1.1 wdk }
74 1.1 wdk
75 1.1 wdk void
76 1.11 chs mkclock_attach(device_t parent, device_t self, void *aux)
77 1.1 wdk {
78 1.11 chs struct mkclock_softc *sc = device_private(self);
79 1.1 wdk struct confargs *ca = aux;
80 1.1 wdk
81 1.13 thorpej sc->sc_dev = self;
82 1.1 wdk sc->sc_bst = ca->ca_bustag;
83 1.1 wdk if (bus_space_map(ca->ca_bustag, ca->ca_addr,
84 1.1 wdk 0x10000,
85 1.1 wdk BUS_SPACE_MAP_LINEAR,
86 1.1 wdk &sc->sc_bsh) != 0) {
87 1.1 wdk printf(": cannot map registers\n");
88 1.1 wdk return;
89 1.1 wdk }
90 1.1 wdk
91 1.7 gdamore sc->sc_todr.todr_settime_ymdhms = mkclock_write;
92 1.7 gdamore sc->sc_todr.todr_gettime_ymdhms = mkclock_read;
93 1.13 thorpej sc->sc_todr.todr_dev = self;
94 1.7 gdamore todr_attach(&sc->sc_todr);
95 1.7 gdamore
96 1.1 wdk printf("\n");
97 1.1 wdk }
98 1.1 wdk
99 1.1 wdk static int
100 1.9 dsl mk_read(struct mkclock_softc *sc, int reg)
101 1.1 wdk {
102 1.1 wdk u_int8_t val;
103 1.1 wdk
104 1.1 wdk val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4);
105 1.12 christos return bcdtobin(val);
106 1.1 wdk }
107 1.1 wdk
108 1.1 wdk static void
109 1.10 dsl mk_write(struct mkclock_softc *sc, int reg, int val)
110 1.1 wdk {
111 1.1 wdk bus_space_write_1(sc->sc_bst, sc->sc_bsh,
112 1.12 christos DATA_PORT + reg*4, bintobcd(val));
113 1.1 wdk }
114 1.1 wdk
115 1.7 gdamore int
116 1.7 gdamore mkclock_read(todr_chip_handle_t tch, struct clock_ymdhms *dt)
117 1.1 wdk {
118 1.13 thorpej struct mkclock_softc *sc = device_private(tch->todr_dev);
119 1.1 wdk int s = splclock();
120 1.1 wdk
121 1.1 wdk bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK);
122 1.1 wdk dt->dt_sec = mk_read(sc, 0);
123 1.1 wdk dt->dt_min = mk_read(sc, 1);
124 1.1 wdk dt->dt_hour = mk_read(sc, 2);
125 1.1 wdk dt->dt_wday = mk_read(sc, 3);
126 1.1 wdk dt->dt_day = mk_read(sc, 4);
127 1.1 wdk dt->dt_mon = mk_read(sc, 5);
128 1.1 wdk dt->dt_year = mk_read(sc, 6);
129 1.1 wdk bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
130 1.1 wdk splx(s);
131 1.1 wdk
132 1.1 wdk dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000);
133 1.7 gdamore return 0;
134 1.1 wdk }
135 1.1 wdk
136 1.7 gdamore int
137 1.7 gdamore mkclock_write(todr_chip_handle_t tch, struct clock_ymdhms *dt)
138 1.1 wdk {
139 1.13 thorpej struct mkclock_softc *sc = device_private(tch->todr_dev);
140 1.1 wdk int year, s;
141 1.1 wdk
142 1.1 wdk year = dt->dt_year % 100;
143 1.1 wdk
144 1.1 wdk s = splclock();
145 1.1 wdk bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK);
146 1.1 wdk mk_write(sc, 0, dt->dt_sec);
147 1.1 wdk mk_write(sc, 1, dt->dt_min);
148 1.1 wdk mk_write(sc, 2, dt->dt_hour);
149 1.1 wdk /* week day not set */
150 1.1 wdk mk_write(sc, 4, dt->dt_day);
151 1.1 wdk mk_write(sc, 5, dt->dt_mon);
152 1.1 wdk mk_write(sc, 6, year);
153 1.1 wdk bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
154 1.1 wdk splx(s);
155 1.7 gdamore return 0;
156 1.1 wdk }
157