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