1 /* $NetBSD: ofrtc.c,v 1.10 2001/11/13 07:26:28 lukem 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 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: ofrtc.c,v 1.10 2001/11/13 07:26:28 lukem Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 #include <sys/conf.h> 41 42 #include <dev/ofw/openfirm.h> 43 44 cdev_decl(ofrtc_); 45 46 struct ofrtc_softc { 47 struct device sc_dev; 48 int sc_phandle; 49 int sc_ihandle; 50 }; 51 52 static int ofrtc_match __P((struct device *, struct cfdata *, void *)); 53 static void ofrtc_attach __P((struct device *, struct device *, void *)); 54 55 struct cfattach ofrtc_ca = { 56 sizeof(struct ofrtc_softc), ofrtc_match, ofrtc_attach 57 }; 58 59 extern struct cfdriver ofrtc_cd; 60 61 static int 62 ofrtc_match(struct device *parent, struct cfdata *match, void *aux) 63 { 64 struct ofbus_attach_args *oba = aux; 65 char type[8]; 66 int l; 67 68 if (strcmp(oba->oba_busname, "ofw")) 69 return (0); 70 if ((l = OF_getprop(oba->oba_phandle, "device_type", type, 71 sizeof type - 1)) < 0 || 72 l >= sizeof type) 73 return 0; 74 75 return !strcmp(type, "rtc"); 76 } 77 78 static void 79 ofrtc_attach(struct device *parent, struct device *self, void *aux) 80 { 81 struct ofrtc_softc *of = (void *)self; 82 struct ofbus_attach_args *oba = aux; 83 char name[32]; 84 int l; 85 86 of->sc_phandle = oba->oba_phandle; 87 of->sc_ihandle = 0; 88 if ((l = OF_getprop(of->sc_phandle, "name", name, 89 sizeof name - 1)) < 0) 90 panic("Device without name?"); 91 if (l >= sizeof name) 92 l = sizeof name - 1; 93 name[l] = 0; 94 printf(": %s\n", name); 95 } 96 97 int 98 ofrtc_open(dev_t dev, int flags, int fmt, struct proc *p) 99 { 100 struct ofrtc_softc *of; 101 int unit = minor(dev); 102 char path[256]; 103 int l; 104 105 if (unit >= ofrtc_cd.cd_ndevs) 106 return ENXIO; 107 if (!(of = ofrtc_cd.cd_devs[unit])) 108 return ENXIO; 109 if (!of->sc_ihandle) { 110 if ((l = OF_package_to_path(of->sc_phandle, path, 111 sizeof path - 1)) < 0 || 112 l >= sizeof path) 113 return ENXIO; 114 path[l] = 0; 115 116 if (!(of->sc_ihandle = OF_open(path))) { 117 if (of->sc_ihandle) { 118 OF_close(of->sc_ihandle); 119 of->sc_ihandle = 0; 120 } 121 return ENXIO; 122 } 123 124 } 125 126 return 0; 127 } 128 129 int 130 ofrtc_close(dev_t dev, int flags, int fmt, struct proc *p) 131 { 132 return 0; 133 } 134 135 static void 136 twodigit(char *bp, int i) 137 { 138 *bp++ = i / 10 + '0'; 139 *bp = i % 10 + '0'; 140 } 141 142 static int 143 twodigits(char *bp) 144 { 145 int i; 146 147 i = *bp++ - '0'; 148 return i * 10 + *bp++ - '0'; 149 } 150 151 int 152 ofrtc_read(dev_t dev, struct uio *uio, int flag) 153 { 154 struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)]; 155 int date[6]; 156 char buf[14]; 157 int xlen; 158 159 if (uio->uio_offset >= sizeof buf) 160 return 0; 161 162 if (OF_call_method("get-time", of->sc_ihandle, 0, 6, 163 date, date + 1, date + 2, 164 date + 3, date + 4, date + 5)) 165 return EIO; 166 167 twodigit(buf, date[5] % 100); 168 twodigit(buf + 2, date[4]); 169 twodigit(buf + 4, date[3]); 170 twodigit(buf + 6, date[2]); 171 twodigit(buf + 8, date[1]); 172 buf[10] = '.'; 173 twodigit(buf + 11, date[0]); 174 buf[13] = '\n'; 175 176 xlen = sizeof(buf) - uio->uio_offset; 177 if (xlen > uio->uio_resid) 178 xlen = uio->uio_resid; 179 180 return uiomove((caddr_t)buf, xlen, uio); 181 } 182 183 int 184 ofrtc_write(dev_t dev, struct uio *uio, int flag) 185 { 186 struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)]; 187 char buf[14]; 188 int cnt, year, error; 189 190 /* 191 * We require atomic updates! 192 */ 193 cnt = uio->uio_resid; 194 if (uio->uio_offset || (cnt != sizeof buf && cnt != sizeof buf - 1)) 195 return EINVAL; 196 197 if ((error = uiomove((caddr_t)buf, sizeof buf, uio)) != 0) 198 return error; 199 200 if (cnt == sizeof buf && buf[sizeof buf - 1] != '\n') 201 return EINVAL; 202 203 year = twodigits(buf) + 1900; 204 if (year < 1970) 205 year += 100; 206 if (OF_call_method("set-time", of->sc_ihandle, 6, 0, 207 twodigits(buf + 11), twodigits(buf + 8), twodigits(buf + 6), 208 twodigits(buf + 4), twodigits(buf + 2), year)) 209 return EIO; 210 return 0; 211 } 212