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