Home | History | Annotate | Line # | Download | only in ic
intersil7170.c revision 1.5.8.1
      1 /*	$NetBSD: intersil7170.c,v 1.5.8.1 2006/09/14 12:31:30 yamt 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  * Intersil 7170 time-of-day chip subroutines.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: intersil7170.c,v 1.5.8.1 2006/09/14 12:31:30 yamt 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/intersil7170.h>
     53 
     54 #define intersil_command(run, interrupt) \
     55 	(run | interrupt | INTERSIL_CMD_FREQ_32K | INTERSIL_CMD_24HR_MODE | \
     56 	 INTERSIL_CMD_NORMAL_MODE)
     57 
     58 struct intersil7170_softc {
     59 	bus_space_tag_t		sil_bt;
     60 	bus_space_handle_t	sil_bh;
     61 	int			sil_year0;
     62 };
     63 
     64 int intersil7170_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     65 int intersil7170_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     66 
     67 int intersil7170_auto_century_adjust = 1;
     68 
     69 todr_chip_handle_t
     70 intersil7170_attach(bus_space_tag_t bt, bus_space_handle_t bh, int year0)
     71 {
     72 	todr_chip_handle_t handle;
     73 	struct intersil7170_softc *sil;
     74 	int sz;
     75 
     76 	printf(": intersil7170");
     77 
     78 	sz = ALIGN(sizeof(struct todr_chip_handle)) + sizeof(struct intersil7170);
     79 	handle = malloc(sz, M_DEVBUF, M_NOWAIT);
     80 	sil = (struct intersil7170_softc *)((u_long)handle +
     81 					ALIGN(sizeof(struct todr_chip_handle)));
     82 	handle->cookie = sil;
     83 	handle->todr_gettime = NULL;
     84 	handle->todr_settime = NULL;
     85 	handle->todr_setwen = NULL;
     86 	handle->todr_gettime_ymdhms = intersil7170_gettime_ymdhms;
     87 	handle->todr_settime_ymdhms = intersil7170_settime_ymdhms;
     88 	sil->sil_bt = bt;
     89 	sil->sil_bh = bh;
     90 	sil->sil_year0 = year0;
     91 
     92 	return (handle);
     93 }
     94 
     95 /*
     96  * Set up the system's time, given a `reasonable' time value.
     97  */
     98 int
     99 intersil7170_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
    100 {
    101 	struct intersil7170_softc *sil = handle->cookie;
    102 	bus_space_tag_t bt = sil->sil_bt;
    103 	bus_space_handle_t bh = sil->sil_bh;
    104 	u_int8_t cmd;
    105 	int year;
    106 	int s;
    107 
    108 	/* No interrupts while we're fiddling with the chip */
    109 	s = splhigh();
    110 
    111 	/* Enable read (stop time) */
    112 	cmd = intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
    113 	bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
    114 
    115 	/* The order of reading out the clock elements is important */
    116 	bus_space_read_1(bt, bh, INTERSIL_ICSEC);	/* not used */
    117 	dt->dt_hour = bus_space_read_1(bt, bh, INTERSIL_IHOUR);
    118 	dt->dt_min = bus_space_read_1(bt, bh, INTERSIL_IMIN);
    119 	dt->dt_sec = bus_space_read_1(bt, bh, INTERSIL_ISEC);
    120 	dt->dt_mon = bus_space_read_1(bt, bh, INTERSIL_IMON);
    121 	dt->dt_day = bus_space_read_1(bt, bh, INTERSIL_IDAY);
    122 	year = bus_space_read_1(bt, bh, INTERSIL_IYEAR);
    123 	dt->dt_wday = bus_space_read_1(bt, bh, INTERSIL_IDOW);
    124 
    125 	/* Done writing (time wears on) */
    126 	cmd = intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    127 	bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
    128 	splx(s);
    129 
    130 	year += sil->sil_year0;
    131 	if (year < 1970 && intersil7170_auto_century_adjust != 0)
    132 		year += 100;
    133 
    134 	dt->dt_year = year;
    135 
    136 	return (0);
    137 }
    138 
    139 /*
    140  * Reset the clock based on the current time.
    141  */
    142 int
    143 intersil7170_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
    144 {
    145 	struct intersil7170_softc *sil = handle->cookie;
    146 	bus_space_tag_t bt = sil->sil_bt;
    147 	bus_space_handle_t bh = sil->sil_bh;
    148 	u_int8_t cmd;
    149 	int year;
    150 	int s;
    151 
    152 	year = dt->dt_year - sil->sil_year0;
    153 	if (year > 99 && intersil7170_auto_century_adjust != 0)
    154 		year -= 100;
    155 
    156 	/* No interrupts while we're fiddling with the chip */
    157 	s = splhigh();
    158 
    159 	/* Enable write (stop time) */
    160 	cmd = intersil_command(INTERSIL_CMD_STOP, INTERSIL_CMD_IENABLE);
    161 	bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
    162 
    163 	/* The order of reading writing the clock elements is important */
    164 	bus_space_write_1(bt, bh, INTERSIL_ICSEC, 0);
    165 	bus_space_write_1(bt, bh, INTERSIL_IHOUR, dt->dt_hour);
    166 	bus_space_write_1(bt, bh, INTERSIL_IMIN, dt->dt_min);
    167 	bus_space_write_1(bt, bh, INTERSIL_ISEC, dt->dt_sec);
    168 	bus_space_write_1(bt, bh, INTERSIL_IMON, dt->dt_mon);
    169 	bus_space_write_1(bt, bh, INTERSIL_IDAY, dt->dt_day);
    170 	bus_space_write_1(bt, bh, INTERSIL_IYEAR, year);
    171 	bus_space_write_1(bt, bh, INTERSIL_IDOW, dt->dt_wday);
    172 
    173 	/* Done writing (time wears on) */
    174 	cmd = intersil_command(INTERSIL_CMD_RUN, INTERSIL_CMD_IENABLE);
    175 	bus_space_write_1(bt, bh, INTERSIL_ICMD, cmd);
    176 	splx(s);
    177 
    178 	return (0);
    179 }
    180