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