1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 */ 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/clock.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 37 #include <machine/bus.h> 38 39 #include <powerpc/mpc85xx/ds1553_reg.h> 40 41 static uint8_t ds1553_direct_read(device_t, bus_size_t); 42 static void ds1553_direct_write(device_t, bus_size_t, uint8_t); 43 44 int 45 ds1553_attach(device_t dev) 46 { 47 struct ds1553_softc *sc; 48 uint8_t sec, flags; 49 50 sc = device_get_softc(dev); 51 52 if (mtx_initialized(&sc->sc_mtx) == 0) { 53 device_printf(dev, "%s: mutex not initialized\n", __func__); 54 return (ENXIO); 55 } 56 57 if (sc->sc_read == NULL) 58 sc->sc_read = ds1553_direct_read; 59 if (sc->sc_write == NULL) 60 sc->sc_write = ds1553_direct_write; 61 62 sc->year_offset = POSIX_BASE_YEAR; 63 64 mtx_lock_spin(&sc->sc_mtx); 65 66 /* Turn RTC on if it was not on */ 67 sec = (*sc->sc_read)(dev, DS1553_OFF_SECONDS); 68 if (sec & DS1553_BIT_OSC) { 69 sec &= ~(DS1553_BIT_OSC); 70 (*sc->sc_write)(dev, DS1553_OFF_SECONDS, sec); 71 } 72 73 /* Low-battery check */ 74 flags = (*sc->sc_read)(dev, DS1553_OFF_FLAGS); 75 if (flags & DS1553_BIT_BLF) 76 device_printf(dev, "voltage-low detected.\n"); 77 78 mtx_unlock_spin(&sc->sc_mtx); 79 80 return (0); 81 } 82 83 /* 84 * Get time of day and convert it to a struct timespec. 85 * Return 0 on success, an error number otherwise. 86 */ 87 int 88 ds1553_gettime(device_t dev, struct timespec *ts) 89 { 90 struct clocktime ct; 91 struct ds1553_softc *sc; 92 uint8_t control; 93 94 sc = device_get_softc(dev); 95 96 mtx_lock_spin(&sc->sc_mtx); 97 98 control = (*sc->sc_read)(dev, DS1553_OFF_CONTROL) | DS1553_BIT_READ; 99 (*sc->sc_write)(dev, DS1553_OFF_CONTROL, control); 100 101 ct.nsec = 0; 102 ct.sec = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_SECONDS) & 103 DS1553_MASK_SECONDS); 104 ct.min = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_MINUTES) & 105 DS1553_MASK_MINUTES); 106 ct.hour = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_HOURS) & 107 DS1553_MASK_HOUR); 108 ct.dow = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_DAYOFWEEK) & 109 DS1553_MASK_DAYOFWEEK) - 1; 110 ct.day = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_DATE) & 111 DS1553_MASK_DATE); 112 ct.mon = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_MONTH) & 113 DS1553_MASK_MONTH); 114 ct.year = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_YEAR)); 115 116 control &= ~DS1553_BIT_READ; 117 (*sc->sc_write)(dev, DS1553_OFF_CONTROL, control); 118 119 ct.year += sc->year_offset; 120 121 mtx_unlock_spin(&sc->sc_mtx); 122 123 return (clock_ct_to_ts(&ct, ts)); 124 } 125 126 /* 127 * Set the time of day clock based on the value of the struct timespec arg. 128 * Return 0 on success, an error number otherwise. 129 */ 130 int 131 ds1553_settime(device_t dev, struct timespec *ts) 132 { 133 struct clocktime ct; 134 struct ds1553_softc *sc; 135 uint8_t control; 136 137 sc = device_get_softc(dev); 138 bzero(&ct, sizeof(struct clocktime)); 139 140 /* Accuracy is only one second. */ 141 if (ts->tv_nsec >= 500000000) 142 ts->tv_sec++; 143 ts->tv_nsec = 0; 144 clock_ts_to_ct(ts, &ct); 145 146 ct.year -= sc->year_offset; 147 148 mtx_lock_spin(&sc->sc_mtx); 149 150 /* Halt updates to external registers */ 151 control = (*sc->sc_read)(dev, DS1553_OFF_CONTROL) | DS1553_BIT_WRITE; 152 (*sc->sc_write)(dev, DS1553_OFF_CONTROL, control); 153 154 (*sc->sc_write)(dev, DS1553_OFF_SECONDS, TOBCD(ct.sec) & 155 DS1553_MASK_SECONDS); 156 (*sc->sc_write)(dev, DS1553_OFF_MINUTES, TOBCD(ct.min) & 157 DS1553_MASK_MINUTES); 158 (*sc->sc_write)(dev, DS1553_OFF_HOURS, TOBCD(ct.hour) & 159 DS1553_MASK_HOUR); 160 (*sc->sc_write)(dev, DS1553_OFF_DAYOFWEEK, TOBCD(ct.dow + 1) & 161 DS1553_MASK_DAYOFWEEK); 162 (*sc->sc_write)(dev, DS1553_OFF_DATE, TOBCD(ct.day) & 163 DS1553_MASK_DATE); 164 (*sc->sc_write)(dev, DS1553_OFF_MONTH, TOBCD(ct.mon) & 165 DS1553_MASK_MONTH); 166 (*sc->sc_write)(dev, DS1553_OFF_YEAR, TOBCD(ct.year)); 167 168 /* Resume updates to external registers */ 169 control &= ~DS1553_BIT_WRITE; 170 (*sc->sc_write)(dev, DS1553_OFF_CONTROL, control); 171 172 mtx_unlock_spin(&sc->sc_mtx); 173 174 return (0); 175 } 176 177 static uint8_t 178 ds1553_direct_read(device_t dev, bus_size_t off) 179 { 180 struct ds1553_softc *sc; 181 182 sc = device_get_softc(dev); 183 return (bus_space_read_1(sc->sc_bst, sc->sc_bsh, off)); 184 } 185 186 static void 187 ds1553_direct_write(device_t dev, bus_size_t off, uint8_t val) 188 { 189 struct ds1553_softc *sc; 190 191 sc = device_get_softc(dev); 192 bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, val); 193 } 194