Home | History | Annotate | Line # | Download | only in ofw
ofrtc.c revision 1.7.26.5
      1  1.7.26.3   nathanw /*	$NetBSD: ofrtc.c,v 1.7.26.5 2002/11/11 22:10:59 nathanw Exp $	*/
      2       1.1        ws 
      3       1.1        ws /*
      4       1.1        ws  * Copyright (C) 1996 Wolfgang Solfrank.
      5       1.1        ws  * Copyright (C) 1996 TooLs GmbH.
      6       1.1        ws  * All rights reserved.
      7       1.1        ws  *
      8       1.1        ws  * Redistribution and use in source and binary forms, with or without
      9       1.1        ws  * modification, are permitted provided that the following conditions
     10       1.1        ws  * are met:
     11       1.1        ws  * 1. Redistributions of source code must retain the above copyright
     12       1.1        ws  *    notice, this list of conditions and the following disclaimer.
     13       1.1        ws  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1        ws  *    notice, this list of conditions and the following disclaimer in the
     15       1.1        ws  *    documentation and/or other materials provided with the distribution.
     16       1.1        ws  * 3. All advertising materials mentioning features or use of this software
     17       1.1        ws  *    must display the following acknowledgement:
     18       1.1        ws  *	This product includes software developed by TooLs GmbH.
     19       1.1        ws  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20       1.1        ws  *    derived from this software without specific prior written permission.
     21       1.1        ws  *
     22       1.1        ws  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23       1.1        ws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24       1.1        ws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25       1.1        ws  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26       1.1        ws  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27       1.1        ws  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28       1.1        ws  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29       1.1        ws  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30       1.1        ws  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31       1.1        ws  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1        ws  */
     33  1.7.26.2   nathanw 
     34  1.7.26.2   nathanw #include <sys/cdefs.h>
     35  1.7.26.2   nathanw __KERNEL_RCSID(0, "$NetBSD: ofrtc.c,v 1.7.26.5 2002/11/11 22:10:59 nathanw Exp $");
     36       1.1        ws 
     37       1.1        ws #include <sys/param.h>
     38       1.7       cgd #include <sys/systm.h>
     39       1.1        ws #include <sys/device.h>
     40  1.7.26.1   nathanw #include <sys/conf.h>
     41  1.7.26.5   nathanw #include <sys/event.h>
     42       1.1        ws 
     43       1.1        ws #include <dev/ofw/openfirm.h>
     44       1.1        ws 
     45       1.1        ws struct ofrtc_softc {
     46       1.1        ws 	struct device sc_dev;
     47       1.1        ws 	int sc_phandle;
     48       1.1        ws 	int sc_ihandle;
     49       1.1        ws };
     50       1.1        ws 
     51       1.6   mycroft static int ofrtc_match __P((struct device *, struct cfdata *, void *));
     52       1.6   mycroft static void ofrtc_attach __P((struct device *, struct device *, void *));
     53       1.1        ws 
     54  1.7.26.4   nathanw CFATTACH_DECL(ofrtc, sizeof(struct ofrtc_softc),
     55  1.7.26.4   nathanw     ofrtc_match, ofrtc_attach, NULL, NULL);
     56       1.1        ws 
     57       1.5   thorpej extern struct cfdriver ofrtc_cd;
     58       1.1        ws 
     59  1.7.26.3   nathanw dev_type_open(ofrtc_open);
     60  1.7.26.3   nathanw dev_type_read(ofrtc_read);
     61  1.7.26.3   nathanw dev_type_write(ofrtc_write);
     62  1.7.26.3   nathanw 
     63  1.7.26.3   nathanw const struct cdevsw ofrtc_cdevsw = {
     64  1.7.26.3   nathanw 	ofrtc_open, nullclose, ofrtc_read, ofrtc_write, noioctl,
     65  1.7.26.5   nathanw 	nostop, notty, nopoll, nommap, nokqfilter,
     66  1.7.26.3   nathanw };
     67  1.7.26.3   nathanw 
     68       1.1        ws static int
     69  1.7.26.1   nathanw ofrtc_match(struct device *parent, struct cfdata *match, void *aux)
     70       1.1        ws {
     71       1.6   mycroft 	struct ofbus_attach_args *oba = aux;
     72       1.1        ws 	char type[8];
     73       1.1        ws 	int l;
     74       1.1        ws 
     75       1.6   mycroft 	if (strcmp(oba->oba_busname, "ofw"))
     76       1.6   mycroft 		return (0);
     77       1.6   mycroft 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
     78       1.6   mycroft 	    sizeof type - 1)) < 0 ||
     79       1.6   mycroft 	    l >= sizeof type)
     80       1.1        ws 		return 0;
     81       1.1        ws 
     82       1.1        ws 	return !strcmp(type, "rtc");
     83       1.1        ws }
     84       1.1        ws 
     85       1.1        ws static void
     86  1.7.26.1   nathanw ofrtc_attach(struct device *parent, struct device *self, void *aux)
     87       1.1        ws {
     88       1.1        ws 	struct ofrtc_softc *of = (void *)self;
     89       1.6   mycroft 	struct ofbus_attach_args *oba = aux;
     90       1.1        ws 	char name[32];
     91       1.1        ws 	int l;
     92       1.1        ws 
     93       1.6   mycroft 	of->sc_phandle = oba->oba_phandle;
     94       1.1        ws 	of->sc_ihandle = 0;
     95       1.6   mycroft 	if ((l = OF_getprop(of->sc_phandle, "name", name,
     96       1.6   mycroft 	    sizeof name - 1)) < 0)
     97       1.1        ws 		panic("Device without name?");
     98       1.1        ws 	if (l >= sizeof name)
     99       1.1        ws 		l = sizeof name - 1;
    100       1.1        ws 	name[l] = 0;
    101       1.3  christos 	printf(": %s\n", name);
    102       1.1        ws }
    103       1.1        ws 
    104       1.1        ws int
    105  1.7.26.1   nathanw ofrtc_open(dev_t dev, int flags, int fmt, struct proc *p)
    106       1.1        ws {
    107       1.1        ws 	struct ofrtc_softc *of;
    108       1.1        ws 	int unit = minor(dev);
    109       1.1        ws 	char path[256];
    110       1.1        ws 	int l;
    111       1.1        ws 
    112       1.1        ws 	if (unit >= ofrtc_cd.cd_ndevs)
    113       1.1        ws 		return ENXIO;
    114       1.1        ws 	if (!(of = ofrtc_cd.cd_devs[unit]))
    115       1.1        ws 		return ENXIO;
    116       1.1        ws 	if (!of->sc_ihandle) {
    117       1.6   mycroft 		if ((l = OF_package_to_path(of->sc_phandle, path,
    118       1.6   mycroft 		    sizeof path - 1)) < 0 ||
    119       1.6   mycroft 		    l >= sizeof path)
    120       1.1        ws 			return ENXIO;
    121       1.1        ws 		path[l] = 0;
    122       1.1        ws 
    123       1.1        ws 		if (!(of->sc_ihandle = OF_open(path))) {
    124       1.1        ws 			if (of->sc_ihandle) {
    125       1.1        ws 				OF_close(of->sc_ihandle);
    126       1.1        ws 				of->sc_ihandle = 0;
    127       1.1        ws 			}
    128       1.1        ws 			return ENXIO;
    129       1.1        ws 		}
    130       1.1        ws 
    131       1.1        ws 	}
    132       1.1        ws 
    133       1.1        ws 	return 0;
    134       1.1        ws }
    135       1.1        ws 
    136       1.1        ws static void
    137  1.7.26.1   nathanw twodigit(char *bp, int i)
    138       1.1        ws {
    139       1.1        ws 	*bp++ = i / 10 + '0';
    140       1.1        ws 	*bp = i % 10 + '0';
    141       1.1        ws }
    142       1.1        ws 
    143       1.1        ws static int
    144  1.7.26.1   nathanw twodigits(char *bp)
    145       1.1        ws {
    146       1.1        ws 	int i;
    147       1.1        ws 
    148       1.1        ws 	i = *bp++ - '0';
    149       1.1        ws 	return i * 10 + *bp++ - '0';
    150       1.1        ws }
    151       1.1        ws 
    152       1.1        ws int
    153  1.7.26.1   nathanw ofrtc_read(dev_t dev, struct uio *uio, int flag)
    154       1.1        ws {
    155       1.1        ws 	struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
    156       1.1        ws 	int date[6];
    157       1.1        ws 	char buf[14];
    158       1.1        ws 	int xlen;
    159       1.1        ws 
    160       1.1        ws 	if (uio->uio_offset >= sizeof buf)
    161       1.1        ws 		return 0;
    162       1.1        ws 
    163       1.1        ws 	if (OF_call_method("get-time", of->sc_ihandle, 0, 6,
    164       1.1        ws 			   date, date + 1, date + 2,
    165       1.1        ws 			   date + 3, date + 4, date + 5))
    166       1.1        ws 		return EIO;
    167       1.1        ws 
    168       1.1        ws 	twodigit(buf, date[5] % 100);
    169       1.1        ws 	twodigit(buf + 2, date[4]);
    170       1.1        ws 	twodigit(buf + 4, date[3]);
    171       1.1        ws 	twodigit(buf + 6, date[2]);
    172       1.1        ws 	twodigit(buf + 8, date[1]);
    173       1.1        ws 	buf[10] = '.';
    174       1.1        ws 	twodigit(buf + 11, date[0]);
    175       1.1        ws 	buf[13] = '\n';
    176       1.1        ws 
    177       1.1        ws 	xlen = sizeof(buf) - uio->uio_offset;
    178       1.1        ws 	if (xlen > uio->uio_resid)
    179       1.1        ws 		xlen = uio->uio_resid;
    180       1.1        ws 
    181       1.1        ws 	return uiomove((caddr_t)buf, xlen, uio);
    182       1.1        ws }
    183       1.1        ws 
    184       1.1        ws int
    185  1.7.26.1   nathanw ofrtc_write(dev_t dev, struct uio *uio, int flag)
    186       1.1        ws {
    187       1.1        ws 	struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
    188       1.1        ws 	char buf[14];
    189       1.1        ws 	int cnt, year, error;
    190       1.1        ws 
    191       1.1        ws 	/*
    192       1.1        ws 	 * We require atomic updates!
    193       1.1        ws 	 */
    194       1.1        ws 	cnt = uio->uio_resid;
    195       1.1        ws 	if (uio->uio_offset || (cnt != sizeof buf && cnt != sizeof buf - 1))
    196       1.1        ws 		return EINVAL;
    197       1.1        ws 
    198       1.7       cgd 	if ((error = uiomove((caddr_t)buf, sizeof buf, uio)) != 0)
    199       1.1        ws 		return error;
    200       1.1        ws 
    201       1.1        ws 	if (cnt == sizeof buf && buf[sizeof buf - 1] != '\n')
    202       1.1        ws 		return EINVAL;
    203       1.1        ws 
    204       1.1        ws 	year = twodigits(buf) + 1900;
    205       1.1        ws 	if (year < 1970)
    206       1.1        ws 		year += 100;
    207       1.1        ws 	if (OF_call_method("set-time", of->sc_ihandle, 6, 0,
    208       1.1        ws 			   twodigits(buf + 11), twodigits(buf + 8), twodigits(buf + 6),
    209       1.1        ws 			   twodigits(buf + 4), twodigits(buf + 2), year))
    210       1.1        ws 		return EIO;
    211       1.1        ws 	return 0;
    212       1.1        ws }
    213