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