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