2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/clock.h>
38 #include <sys/mutex.h>
40 #include <machine/bus.h>
42 #include <powerpc/mpc85xx/ds1553_reg.h>
44 static uint8_t ds1553_direct_read(device_t, bus_size_t);
45 static void ds1553_direct_write(device_t, bus_size_t, uint8_t);
48 ds1553_attach(device_t dev)
50 struct ds1553_softc *sc;
53 sc = device_get_softc(dev);
55 if (mtx_initialized(&sc->sc_mtx) == 0) {
56 device_printf(dev, "%s: mutex not initialized\n", __func__);
60 if (sc->sc_read == NULL)
61 sc->sc_read = ds1553_direct_read;
62 if (sc->sc_write == NULL)
63 sc->sc_write = ds1553_direct_write;
65 sc->year_offset = POSIX_BASE_YEAR;
67 mtx_lock_spin(&sc->sc_mtx);
69 /* Turn RTC on if it was not on */
70 sec = (*sc->sc_read)(dev, DS1553_OFF_SECONDS);
71 if (sec & DS1553_BIT_OSC) {
72 sec &= ~(DS1553_BIT_OSC);
73 (*sc->sc_write)(dev, DS1553_OFF_SECONDS, sec);
76 /* Low-battery check */
77 flags = (*sc->sc_read)(dev, DS1553_OFF_FLAGS);
78 if (flags & DS1553_BIT_BLF)
79 device_printf(dev, "voltage-low detected.\n");
81 mtx_unlock_spin(&sc->sc_mtx);
87 * Get time of day and convert it to a struct timespec.
88 * Return 0 on success, an error number otherwise.
91 ds1553_gettime(device_t dev, struct timespec *ts)
94 struct ds1553_softc *sc;
97 sc = device_get_softc(dev);
99 mtx_lock_spin(&sc->sc_mtx);
101 control = (*sc->sc_read)(dev, DS1553_OFF_CONTROL) | DS1553_BIT_READ;
102 (*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
105 ct.sec = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_SECONDS) &
106 DS1553_MASK_SECONDS);
107 ct.min = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_MINUTES) &
108 DS1553_MASK_MINUTES);
109 ct.hour = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_HOURS) &
111 ct.dow = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_DAYOFWEEK) &
112 DS1553_MASK_DAYOFWEEK) - 1;
113 ct.day = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_DATE) &
115 ct.mon = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_MONTH) &
117 ct.year = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_YEAR));
119 control &= ~DS1553_BIT_READ;
120 (*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
122 ct.year += sc->year_offset;
124 mtx_unlock_spin(&sc->sc_mtx);
126 return (clock_ct_to_ts(&ct, ts));
130 * Set the time of day clock based on the value of the struct timespec arg.
131 * Return 0 on success, an error number otherwise.
134 ds1553_settime(device_t dev, struct timespec *ts)
137 struct ds1553_softc *sc;
140 sc = device_get_softc(dev);
141 bzero(&ct, sizeof(struct clocktime));
143 /* Accuracy is only one second. */
144 if (ts->tv_nsec >= 500000000)
147 clock_ts_to_ct(ts, &ct);
149 ct.year -= sc->year_offset;
151 mtx_lock_spin(&sc->sc_mtx);
153 /* Halt updates to external registers */
154 control = (*sc->sc_read)(dev, DS1553_OFF_CONTROL) | DS1553_BIT_WRITE;
155 (*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
157 (*sc->sc_write)(dev, DS1553_OFF_SECONDS, TOBCD(ct.sec) &
158 DS1553_MASK_SECONDS);
159 (*sc->sc_write)(dev, DS1553_OFF_MINUTES, TOBCD(ct.min) &
160 DS1553_MASK_MINUTES);
161 (*sc->sc_write)(dev, DS1553_OFF_HOURS, TOBCD(ct.hour) &
163 (*sc->sc_write)(dev, DS1553_OFF_DAYOFWEEK, TOBCD(ct.dow + 1) &
164 DS1553_MASK_DAYOFWEEK);
165 (*sc->sc_write)(dev, DS1553_OFF_DATE, TOBCD(ct.day) &
167 (*sc->sc_write)(dev, DS1553_OFF_MONTH, TOBCD(ct.mon) &
169 (*sc->sc_write)(dev, DS1553_OFF_YEAR, TOBCD(ct.year));
171 /* Resume updates to external registers */
172 control &= ~DS1553_BIT_WRITE;
173 (*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
175 mtx_unlock_spin(&sc->sc_mtx);
181 ds1553_direct_read(device_t dev, bus_size_t off)
183 struct ds1553_softc *sc;
185 sc = device_get_softc(dev);
186 return (bus_space_read_1(sc->sc_bst, sc->sc_bsh, off));
190 ds1553_direct_write(device_t dev, bus_size_t off, uint8_t val)
192 struct ds1553_softc *sc;
194 sc = device_get_softc(dev);
195 bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, val);