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