1 1.24 thorpej /* $NetBSD: ofrtc.c,v 1.24 2025/09/07 21:45:16 thorpej Exp $ */ 2 1.1 ws 3 1.1 ws /* 4 1.1 ws * Copyright (C) 1996 Wolfgang Solfrank. 5 1.1 ws * Copyright (C) 1996 TooLs GmbH. 6 1.1 ws * All rights reserved. 7 1.1 ws * 8 1.1 ws * Redistribution and use in source and binary forms, with or without 9 1.1 ws * modification, are permitted provided that the following conditions 10 1.1 ws * are met: 11 1.1 ws * 1. Redistributions of source code must retain the above copyright 12 1.1 ws * notice, this list of conditions and the following disclaimer. 13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 ws * notice, this list of conditions and the following disclaimer in the 15 1.1 ws * documentation and/or other materials provided with the distribution. 16 1.1 ws * 3. All advertising materials mentioning features or use of this software 17 1.1 ws * must display the following acknowledgement: 18 1.1 ws * This product includes software developed by TooLs GmbH. 19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 1.1 ws * derived from this software without specific prior written permission. 21 1.1 ws * 22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.1 ws */ 33 1.20 gdamore /* 34 1.20 gdamore * Copyright 1997 35 1.20 gdamore * Digital Equipment Corporation. All rights reserved. 36 1.20 gdamore * 37 1.20 gdamore * This software is furnished under license and may be used and 38 1.20 gdamore * copied only in accordance with the following terms and conditions. 39 1.20 gdamore * Subject to these conditions, you may download, copy, install, 40 1.20 gdamore * use, modify and distribute this software in source and/or binary 41 1.20 gdamore * form. No title or ownership is transferred hereby. 42 1.20 gdamore * 43 1.20 gdamore * 1) Any source code used, modified or distributed must reproduce 44 1.20 gdamore * and retain this copyright notice and list of conditions as 45 1.20 gdamore * they appear in the source file. 46 1.20 gdamore * 47 1.20 gdamore * 2) No right is granted to use any trade name, trademark, or logo of 48 1.20 gdamore * Digital Equipment Corporation. Neither the "Digital Equipment 49 1.20 gdamore * Corporation" name nor any trademark or logo of Digital Equipment 50 1.20 gdamore * Corporation may be used to endorse or promote products derived 51 1.20 gdamore * from this software without the prior written permission of 52 1.20 gdamore * Digital Equipment Corporation. 53 1.20 gdamore * 54 1.20 gdamore * 3) This software is provided "AS-IS" and any express or implied 55 1.20 gdamore * warranties, including but not limited to, any implied warranties 56 1.20 gdamore * of merchantability, fitness for a particular purpose, or 57 1.20 gdamore * non-infringement are disclaimed. In no event shall DIGITAL be 58 1.20 gdamore * liable for any damages whatsoever, and in particular, DIGITAL 59 1.20 gdamore * shall not be liable for special, indirect, consequential, or 60 1.20 gdamore * incidental damages or damages for lost profits, loss of 61 1.20 gdamore * revenue or loss of use, whether such damages arise in contract, 62 1.20 gdamore * negligence, tort, under statute, in equity, at law or otherwise, 63 1.20 gdamore * even if advised of the possibility of such damage. 64 1.20 gdamore */ 65 1.10 lukem 66 1.10 lukem #include <sys/cdefs.h> 67 1.24 thorpej __KERNEL_RCSID(0, "$NetBSD: ofrtc.c,v 1.24 2025/09/07 21:45:16 thorpej Exp $"); 68 1.1 ws 69 1.1 ws #include <sys/param.h> 70 1.7 cgd #include <sys/systm.h> 71 1.1 ws #include <sys/device.h> 72 1.8 matt #include <sys/conf.h> 73 1.15 jdolecek #include <sys/event.h> 74 1.1 ws 75 1.20 gdamore #include <dev/clock_subr.h> 76 1.1 ws #include <dev/ofw/openfirm.h> 77 1.1 ws 78 1.20 gdamore #define OFRTC_SEC 0 79 1.20 gdamore #define OFRTC_MIN 1 80 1.20 gdamore #define OFRTC_HR 2 81 1.20 gdamore #define OFRTC_DOM 3 82 1.20 gdamore #define OFRTC_MON 4 83 1.20 gdamore #define OFRTC_YR 5 84 1.20 gdamore 85 1.1 ws struct ofrtc_softc { 86 1.1 ws int sc_phandle; 87 1.1 ws int sc_ihandle; 88 1.20 gdamore struct todr_chip_handle sc_todr; 89 1.1 ws }; 90 1.1 ws 91 1.22 cegger static int ofrtc_match(device_t, cfdata_t, void *); 92 1.22 cegger static void ofrtc_attach(device_t, device_t, void *); 93 1.20 gdamore static int ofrtc_gettod(todr_chip_handle_t, struct clock_ymdhms *); 94 1.20 gdamore static int ofrtc_settod(todr_chip_handle_t, struct clock_ymdhms *); 95 1.1 ws 96 1.23 mrg CFATTACH_DECL_NEW(ofrtc, sizeof(struct ofrtc_softc), 97 1.14 thorpej ofrtc_match, ofrtc_attach, NULL, NULL); 98 1.1 ws 99 1.1 ws static int 100 1.22 cegger ofrtc_match(device_t parent, cfdata_t match, void *aux) 101 1.1 ws { 102 1.6 mycroft struct ofbus_attach_args *oba = aux; 103 1.1 ws char type[8]; 104 1.1 ws int l; 105 1.17 perry 106 1.6 mycroft if (strcmp(oba->oba_busname, "ofw")) 107 1.6 mycroft return (0); 108 1.6 mycroft if ((l = OF_getprop(oba->oba_phandle, "device_type", type, 109 1.6 mycroft sizeof type - 1)) < 0 || 110 1.6 mycroft l >= sizeof type) 111 1.1 ws return 0; 112 1.17 perry 113 1.20 gdamore /* ensure null termination */ 114 1.20 gdamore type[l] = 0; 115 1.1 ws return !strcmp(type, "rtc"); 116 1.1 ws } 117 1.1 ws 118 1.1 ws static void 119 1.22 cegger ofrtc_attach(device_t parent, device_t self, void *aux) 120 1.1 ws { 121 1.19 thorpej struct ofrtc_softc *of = device_private(self); 122 1.6 mycroft struct ofbus_attach_args *oba = aux; 123 1.1 ws char name[32]; 124 1.20 gdamore char path[256]; 125 1.1 ws int l; 126 1.17 perry 127 1.6 mycroft of->sc_phandle = oba->oba_phandle; 128 1.1 ws of->sc_ihandle = 0; 129 1.6 mycroft if ((l = OF_getprop(of->sc_phandle, "name", name, 130 1.6 mycroft sizeof name - 1)) < 0) 131 1.1 ws panic("Device without name?"); 132 1.1 ws if (l >= sizeof name) 133 1.1 ws l = sizeof name - 1; 134 1.1 ws name[l] = 0; 135 1.1 ws 136 1.1 ws if (!of->sc_ihandle) { 137 1.6 mycroft if ((l = OF_package_to_path(of->sc_phandle, path, 138 1.20 gdamore sizeof path - 1)) < 0 || 139 1.20 gdamore l >= sizeof path) { 140 1.20 gdamore aprint_error(": cannot determine package path\n"); 141 1.20 gdamore return; 142 1.20 gdamore } 143 1.1 ws path[l] = 0; 144 1.17 perry 145 1.1 ws if (!(of->sc_ihandle = OF_open(path))) { 146 1.20 gdamore aprint_error(": cannot open path\n"); 147 1.20 gdamore return; 148 1.20 gdamore } 149 1.20 gdamore } 150 1.20 gdamore 151 1.20 gdamore of->sc_todr.todr_gettime_ymdhms = ofrtc_gettod; 152 1.20 gdamore of->sc_todr.todr_settime_ymdhms = ofrtc_settod; 153 1.24 thorpej of->sc_todr.todr_dev = self; 154 1.20 gdamore todr_attach(&of->sc_todr); 155 1.20 gdamore printf(": %s\n", name); 156 1.1 ws } 157 1.1 ws 158 1.1 ws int 159 1.20 gdamore ofrtc_gettod(todr_chip_handle_t tch, struct clock_ymdhms *dt) 160 1.1 ws { 161 1.24 thorpej struct ofrtc_softc *of = device_private(tch->todr_dev); 162 1.1 ws int date[6]; 163 1.17 perry 164 1.20 gdamore /* 165 1.20 gdamore * We mostly ignore the suggested time and go for the RTC clock time 166 1.20 gdamore * stored in the CMOS RAM. If the time can't be obtained from the 167 1.20 gdamore * CMOS, or if the time obtained from the CMOS is 5 or more years 168 1.20 gdamore * less than the suggested time, we used the suggested time. (In 169 1.20 gdamore * the latter case, it's likely that the CMOS battery has died.) 170 1.20 gdamore */ 171 1.17 perry 172 1.1 ws if (OF_call_method("get-time", of->sc_ihandle, 0, 6, 173 1.20 gdamore date, date + 1, date + 2, date + 3, date + 4, date + 5)) { 174 1.1 ws return EIO; 175 1.20 gdamore } 176 1.1 ws 177 1.20 gdamore dt->dt_sec = date[OFRTC_SEC]; 178 1.20 gdamore dt->dt_min = date[OFRTC_MIN]; 179 1.20 gdamore dt->dt_hour = date[OFRTC_HR]; 180 1.20 gdamore dt->dt_day = date[OFRTC_DOM]; 181 1.20 gdamore dt->dt_mon = date[OFRTC_MON]; 182 1.20 gdamore dt->dt_year = date[OFRTC_YR]; 183 1.17 perry 184 1.20 gdamore return 0; 185 1.1 ws } 186 1.1 ws 187 1.1 ws int 188 1.20 gdamore ofrtc_settod(todr_chip_handle_t tch, struct clock_ymdhms *dt) 189 1.1 ws { 190 1.24 thorpej struct ofrtc_softc *of = device_private(tch->todr_dev); 191 1.20 gdamore int sec, minute, hr, dom, mon, yr; 192 1.20 gdamore 193 1.20 gdamore sec = dt->dt_sec; 194 1.20 gdamore minute = dt->dt_min; 195 1.20 gdamore hr = dt->dt_hour; 196 1.20 gdamore dom = dt->dt_day; 197 1.20 gdamore mon = dt->dt_mon; 198 1.20 gdamore yr = dt->dt_year; 199 1.17 perry 200 1.1 ws if (OF_call_method("set-time", of->sc_ihandle, 6, 0, 201 1.20 gdamore sec, minute, hr, dom, mon, yr)) 202 1.1 ws return EIO; 203 1.20 gdamore 204 1.1 ws return 0; 205 1.1 ws } 206