Home | History | Annotate | Line # | Download | only in ic
mk48txx.c revision 1.6.4.2
      1  1.6.4.2  nathanw /*	$NetBSD: mk48txx.c,v 1.6.4.2 2001/11/14 19:14:30 nathanw Exp $ */
      2      1.1       pk /*-
      3      1.1       pk  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      4      1.1       pk  * All rights reserved.
      5      1.1       pk  *
      6      1.1       pk  * This code is derived from software contributed to The NetBSD Foundation
      7      1.1       pk  * by Paul Kranenburg.
      8      1.1       pk  *
      9      1.1       pk  * Redistribution and use in source and binary forms, with or without
     10      1.1       pk  * modification, are permitted provided that the following conditions
     11      1.1       pk  * are met:
     12      1.1       pk  * 1. Redistributions of source code must retain the above copyright
     13      1.1       pk  *    notice, this list of conditions and the following disclaimer.
     14      1.1       pk  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1       pk  *    notice, this list of conditions and the following disclaimer in the
     16      1.1       pk  *    documentation and/or other materials provided with the distribution.
     17      1.1       pk  * 3. All advertising materials mentioning features or use of this software
     18      1.1       pk  *    must display the following acknowledgement:
     19      1.1       pk  *        This product includes software developed by the NetBSD
     20      1.1       pk  *        Foundation, Inc. and its contributors.
     21      1.1       pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22      1.1       pk  *    contributors may be used to endorse or promote products derived
     23      1.1       pk  *    from this software without specific prior written permission.
     24      1.1       pk  *
     25      1.1       pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26      1.1       pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27      1.1       pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28      1.1       pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29      1.1       pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30      1.1       pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31      1.1       pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32      1.1       pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33      1.1       pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34      1.1       pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35      1.1       pk  * POSSIBILITY OF SUCH DAMAGE.
     36      1.1       pk  */
     37      1.1       pk 
     38      1.1       pk /*
     39      1.1       pk  * Mostek MK48T02, MK48T08, MK48T59 time-of-day chip subroutines.
     40      1.1       pk  */
     41  1.6.4.2  nathanw 
     42  1.6.4.2  nathanw #include <sys/cdefs.h>
     43  1.6.4.2  nathanw __KERNEL_RCSID(0, "$NetBSD: mk48txx.c,v 1.6.4.2 2001/11/14 19:14:30 nathanw Exp $");
     44      1.1       pk 
     45      1.1       pk #include <sys/param.h>
     46      1.1       pk #include <sys/malloc.h>
     47      1.1       pk #include <sys/systm.h>
     48      1.1       pk #include <sys/errno.h>
     49      1.1       pk 
     50      1.1       pk #include <machine/bus.h>
     51      1.1       pk #include <dev/clock_subr.h>
     52      1.1       pk #include <dev/ic/mk48txxreg.h>
     53      1.1       pk 
     54      1.1       pk 
     55      1.1       pk struct mk48txx {
     56      1.1       pk 	bus_space_tag_t	mk_bt;		/* bus tag & handle */
     57      1.1       pk 	bus_space_handle_t mk_bh;	/* */
     58      1.6       pk 	bus_size_t	mk_nvramsz;	/* Size of NVRAM on the chip */
     59      1.1       pk 	bus_size_t	mk_clkoffset;	/* Offset in NVRAM to clock bits */
     60      1.1       pk 	u_int		mk_year0;	/* What year is represented on the system
     61      1.1       pk 					   by the chip's year counter at 0 */
     62      1.1       pk };
     63      1.1       pk 
     64      1.1       pk int mk48txx_gettime(todr_chip_handle_t, struct timeval *);
     65      1.1       pk int mk48txx_settime(todr_chip_handle_t, struct timeval *);
     66      1.1       pk int mk48txx_getcal(todr_chip_handle_t, int *);
     67      1.1       pk int mk48txx_setcal(todr_chip_handle_t, int);
     68      1.1       pk 
     69      1.1       pk int mk48txx_auto_century_adjust = 1;
     70      1.1       pk 
     71      1.1       pk struct {
     72      1.1       pk 	const char *name;
     73      1.6       pk 	bus_size_t nvramsz;
     74      1.1       pk 	bus_size_t clkoff;
     75      1.1       pk 	int flags;
     76      1.1       pk #define MK48TXX_EXT_REGISTERS	1	/* Has extended register set */
     77      1.1       pk } mk48txx_models[] = {
     78      1.1       pk 	{ "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
     79      1.1       pk 	{ "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
     80      1.1       pk 	{ "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
     81      1.1       pk };
     82      1.1       pk 
     83      1.1       pk todr_chip_handle_t
     84      1.1       pk mk48txx_attach(bt, bh, model, year0)
     85      1.1       pk 	bus_space_tag_t bt;
     86      1.1       pk 	bus_space_handle_t bh;
     87      1.1       pk 	const char *model;
     88      1.1       pk 	int year0;
     89      1.1       pk {
     90      1.1       pk 	todr_chip_handle_t handle;
     91      1.1       pk 	struct mk48txx *mk;
     92      1.6       pk 	bus_size_t nvramsz, clkoff;
     93      1.6       pk 	int sz;
     94      1.1       pk 	int i;
     95      1.1       pk 
     96      1.1       pk 	printf(": %s", model);
     97      1.1       pk 
     98      1.1       pk 	i = sizeof(mk48txx_models)/sizeof(mk48txx_models[0]);
     99      1.1       pk 	while (--i >= 0) {
    100      1.1       pk 		if (strcmp(model, mk48txx_models[i].name) == 0) {
    101      1.1       pk 			nvramsz = mk48txx_models[i].nvramsz;
    102      1.1       pk 			clkoff = mk48txx_models[i].clkoff;
    103      1.1       pk 			break;
    104      1.1       pk 		}
    105      1.1       pk 	}
    106      1.1       pk 	if (i < 0) {
    107      1.1       pk 		printf(": unsupported model");
    108      1.1       pk 		return (NULL);
    109      1.1       pk 	}
    110      1.1       pk 
    111      1.1       pk 	sz = ALIGN(sizeof(struct todr_chip_handle)) + sizeof(struct mk48txx);
    112      1.1       pk 	handle = malloc(sz, M_DEVBUF, M_NOWAIT);
    113      1.1       pk 	mk = (struct mk48txx *)((u_long)handle +
    114      1.1       pk 				 ALIGN(sizeof(struct todr_chip_handle)));
    115      1.1       pk 	handle->cookie = mk;
    116      1.1       pk 	handle->todr_gettime = mk48txx_gettime;
    117      1.1       pk 	handle->todr_settime = mk48txx_settime;
    118      1.1       pk 	handle->todr_getcal = mk48txx_getcal;
    119      1.1       pk 	handle->todr_setcal = mk48txx_setcal;
    120      1.2      eeh 	handle->todr_setwen = NULL;
    121      1.1       pk 	mk->mk_bt = bt;
    122      1.1       pk 	mk->mk_bh = bh;
    123      1.1       pk 	mk->mk_nvramsz = nvramsz;
    124      1.1       pk 	mk->mk_clkoffset = clkoff;
    125      1.1       pk 	mk->mk_year0 = year0;
    126      1.1       pk 
    127      1.1       pk 	return (handle);
    128      1.1       pk }
    129      1.1       pk 
    130      1.1       pk /*
    131      1.1       pk  * Get time-of-day and convert to a `struct timeval'
    132      1.1       pk  * Return 0 on success; an error number otherwise.
    133      1.1       pk  */
    134      1.1       pk int
    135      1.1       pk mk48txx_gettime(handle, tv)
    136      1.1       pk 	todr_chip_handle_t handle;
    137      1.1       pk 	struct timeval *tv;
    138      1.1       pk {
    139      1.1       pk 	struct mk48txx *mk = handle->cookie;
    140      1.1       pk 	bus_space_tag_t bt = mk->mk_bt;
    141      1.1       pk 	bus_space_handle_t bh = mk->mk_bh;
    142      1.1       pk 	bus_size_t clkoff = mk->mk_clkoffset;
    143      1.1       pk 	struct clock_ymdhms dt;
    144      1.1       pk 	int year;
    145      1.1       pk 	u_int8_t csr;
    146      1.1       pk 
    147      1.2      eeh 	todr_wenable(handle, 1);
    148      1.1       pk 
    149      1.1       pk 	/* enable read (stop time) */
    150      1.1       pk 	csr = bus_space_read_1(bt, bh, clkoff + MK48TXX_ICSR);
    151      1.1       pk 	csr |= MK48TXX_CSR_READ;
    152      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_ICSR, csr);
    153      1.1       pk 
    154      1.1       pk 	dt.dt_sec = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_ISEC));
    155      1.1       pk 	dt.dt_min = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IMIN));
    156      1.1       pk 	dt.dt_hour = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IHOUR));
    157      1.1       pk 	dt.dt_day = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IDAY));
    158      1.1       pk 	dt.dt_wday = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IWDAY));
    159      1.1       pk 	dt.dt_mon = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IMON));
    160      1.1       pk 	year = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IYEAR));
    161      1.1       pk 
    162      1.1       pk 	year += mk->mk_year0;
    163      1.3  tsutsui 	if (year < POSIX_BASE_YEAR && mk48txx_auto_century_adjust != 0)
    164      1.1       pk 		year += 100;
    165      1.1       pk 
    166      1.1       pk 	dt.dt_year = year;
    167      1.1       pk 
    168      1.1       pk 	/* time wears on */
    169      1.1       pk 	csr = bus_space_read_1(bt, bh, clkoff + MK48TXX_ICSR);
    170      1.1       pk 	csr &= ~MK48TXX_CSR_READ;
    171      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_ICSR, csr);
    172      1.2      eeh 	todr_wenable(handle, 0);
    173      1.1       pk 
    174      1.1       pk 	/* simple sanity checks */
    175  1.6.4.1  nathanw 	if (dt.dt_mon > 12 || dt.dt_day > 31 ||
    176  1.6.4.1  nathanw 	    dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
    177      1.4  tsutsui 		return (1);
    178      1.4  tsutsui 
    179      1.1       pk 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
    180      1.1       pk 	tv->tv_usec = 0;
    181      1.1       pk 	return (0);
    182      1.1       pk }
    183      1.1       pk 
    184      1.1       pk /*
    185      1.1       pk  * Set the time-of-day clock based on the value of the `struct timeval' arg.
    186      1.1       pk  * Return 0 on success; an error number otherwise.
    187      1.1       pk  */
    188      1.1       pk int
    189      1.1       pk mk48txx_settime(handle, tv)
    190      1.1       pk 	todr_chip_handle_t handle;
    191      1.1       pk 	struct timeval *tv;
    192      1.1       pk {
    193      1.1       pk 	struct mk48txx *mk = handle->cookie;
    194      1.1       pk 	bus_space_tag_t bt = mk->mk_bt;
    195      1.1       pk 	bus_space_handle_t bh = mk->mk_bh;
    196      1.1       pk 	bus_size_t clkoff = mk->mk_clkoffset;
    197      1.1       pk 	struct clock_ymdhms dt;
    198      1.1       pk 	u_int8_t csr;
    199      1.1       pk 	int year;
    200      1.1       pk 
    201      1.1       pk 	/* Note: we ignore `tv_usec' */
    202      1.1       pk 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
    203      1.1       pk 
    204      1.1       pk 	year = dt.dt_year - mk->mk_year0;
    205      1.1       pk 	if (year > 99 && mk48txx_auto_century_adjust != 0)
    206      1.1       pk 		year -= 100;
    207      1.1       pk 
    208      1.2      eeh 	todr_wenable(handle, 1);
    209      1.1       pk 	/* enable write */
    210      1.1       pk 	csr = bus_space_read_1(bt, bh, clkoff + MK48TXX_ICSR);
    211      1.1       pk 	csr |= MK48TXX_CSR_WRITE;
    212      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_ICSR, csr);
    213      1.1       pk 
    214      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_ISEC, TOBCD(dt.dt_sec));
    215      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_IMIN, TOBCD(dt.dt_min));
    216      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_IHOUR, TOBCD(dt.dt_hour));
    217      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_IWDAY, TOBCD(dt.dt_wday));
    218      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_IDAY, TOBCD(dt.dt_day));
    219      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_IMON, TOBCD(dt.dt_mon));
    220      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_IYEAR, TOBCD(year));
    221      1.1       pk 
    222      1.1       pk 	/* load them up */
    223      1.1       pk 	csr = bus_space_read_1(bt, bh, clkoff + MK48TXX_ICSR);
    224      1.1       pk 	csr &= ~MK48TXX_CSR_WRITE;
    225      1.1       pk 	bus_space_write_1(bt, bh, clkoff + MK48TXX_ICSR, csr);
    226      1.2      eeh 	todr_wenable(handle, 0);
    227      1.1       pk 	return (0);
    228      1.1       pk }
    229      1.1       pk 
    230      1.1       pk int
    231      1.1       pk mk48txx_getcal(handle, vp)
    232      1.1       pk 	todr_chip_handle_t handle;
    233      1.1       pk 	int *vp;
    234      1.1       pk {
    235      1.1       pk 	return (EOPNOTSUPP);
    236      1.1       pk }
    237      1.1       pk 
    238      1.1       pk int
    239      1.1       pk mk48txx_setcal(handle, v)
    240      1.1       pk 	todr_chip_handle_t handle;
    241      1.1       pk 	int v;
    242      1.1       pk {
    243      1.1       pk 	return (EOPNOTSUPP);
    244      1.5       pk }
    245      1.5       pk 
    246      1.5       pk int
    247      1.5       pk mk48txx_get_nvram_size(handle, vp)
    248      1.5       pk 	todr_chip_handle_t handle;
    249      1.6       pk 	bus_size_t *vp;
    250      1.5       pk {
    251      1.5       pk 	struct mk48txx *mk = handle->cookie;
    252      1.5       pk 	*vp = mk->mk_nvramsz;
    253      1.5       pk 	return (0);
    254      1.1       pk }
    255