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