mk48txx.c revision 119418
1/*-
2 * Copyright (c) 2000 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Paul Kranenburg.
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 the NetBSD
19 *        Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 *    contributors may be used to endorse or promote products derived
22 *    from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 *	$NetBSD: mk48txx.c,v 1.7 2001/04/08 17:05:10 tsutsui Exp $
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/sys/dev/mk48txx/mk48txx.c 119418 2003-08-24 17:55:58Z obrien $");
41
42/*
43 * Mostek MK48T02, MK48T08, MK48T59 time-of-day chip subroutines.
44 */
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/bus.h>
49#include <sys/clock.h>
50#include <sys/malloc.h>
51
52#include <machine/bus.h>
53
54#include <dev/mk48txx/mk48txxreg.h>
55
56#include "clock_if.h"
57
58struct mk48txx_softc {
59	bus_space_tag_t	mk_bt;		/* bus tag & handle */
60	bus_space_handle_t mk_bh;	/* */
61	bus_size_t	mk_nvramsz;	/* Size of NVRAM on the chip */
62	bus_size_t	mk_clkoffset;	/* Offset in NVRAM to clock bits */
63	u_int		mk_year0;	/* What year is represented on the system
64					   by the chip's year counter at 0 */
65};
66
67int mk48txx_auto_century_adjust = 1;
68
69struct {
70	const char *name;
71	bus_size_t nvramsz;
72	bus_size_t clkoff;
73	int flags;
74#define MK48TXX_EXT_REGISTERS	1	/* Has extended register set */
75} mk48txx_models[] = {
76	{ "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
77	{ "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
78	{ "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
79};
80
81int
82mk48txx_attach(device_t dev, bus_space_tag_t bt, bus_space_handle_t bh,
83    const char *model, int year0)
84{
85	struct mk48txx_softc *sc;
86	bus_size_t clkoff = 0, nvramsz = 0;
87	int i;
88
89	device_printf(dev, "model %s", model);
90	i = sizeof(mk48txx_models) / sizeof(mk48txx_models[0]);
91	while (--i >= 0) {
92		if (strcmp(model, mk48txx_models[i].name) == 0) {
93			nvramsz = mk48txx_models[i].nvramsz;
94			clkoff = mk48txx_models[i].clkoff;
95			break;
96		}
97	}
98	if (i < 0) {
99		device_printf(dev, " (unsupported)\n");
100		return (ENXIO);
101	}
102	printf("\n");
103
104	sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
105	sc->mk_bt = bt;
106	sc->mk_bh = bh;
107	sc->mk_nvramsz = nvramsz;
108	sc->mk_clkoffset = clkoff;
109	sc->mk_year0 = year0;
110	device_set_softc(dev, sc);
111	clock_register(dev, 1000000);	/* 1 second resolution. */
112
113	return (0);
114}
115
116/*
117 * Get time-of-day and convert to a `struct timespec'
118 * Return 0 on success; an error number otherwise.
119 */
120int
121mk48txx_gettime(device_t dev, struct timespec *ts)
122{
123	struct mk48txx_softc *mk = device_get_softc(dev);
124	bus_space_tag_t bt = mk->mk_bt;
125	bus_space_handle_t bh = mk->mk_bh;
126	bus_size_t clkoff = mk->mk_clkoffset;
127	struct clocktime ct;
128	int year;
129	u_int8_t csr;
130
131	/* enable read (stop time) */
132	csr = bus_space_read_1(bt, bh, clkoff + MK48TXX_ICSR);
133	csr |= MK48TXX_CSR_READ;
134	bus_space_write_1(bt, bh, clkoff + MK48TXX_ICSR, csr);
135
136	ct.nsec = 0;
137	ct.sec = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_ISEC));
138	ct.min = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IMIN));
139	ct.hour = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IHOUR));
140	ct.day = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IDAY));
141	ct.dow = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IWDAY)) % 7;
142	ct.mon = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IMON));
143	year = FROMBCD(bus_space_read_1(bt, bh, clkoff + MK48TXX_IYEAR));
144
145	year += mk->mk_year0;
146	if (year < POSIX_BASE_YEAR && mk48txx_auto_century_adjust != 0)
147		year += 100;
148
149	ct.year = year;
150
151	/* time wears on */
152	csr = bus_space_read_1(bt, bh, clkoff + MK48TXX_ICSR);
153	csr &= ~MK48TXX_CSR_READ;
154	bus_space_write_1(bt, bh, clkoff + MK48TXX_ICSR, csr);
155
156	return (clock_ct_to_ts(&ct, ts));
157}
158
159/*
160 * Set the time-of-day clock based on the value of the `struct timespec' arg.
161 * Return 0 on success; an error number otherwise.
162 */
163int
164mk48txx_settime(device_t dev, struct timespec *ts)
165{
166	struct mk48txx_softc *mk = device_get_softc(dev);
167	bus_space_tag_t bt = mk->mk_bt;
168	bus_space_handle_t bh = mk->mk_bh;
169	bus_size_t clkoff = mk->mk_clkoffset;
170	struct clocktime ct;
171	u_int8_t csr;
172	int year;
173
174	/* Accuracy is only one second. */
175	if (ts->tv_nsec >= 500000000)
176		ts->tv_sec++;
177	ts->tv_nsec = 0;
178	clock_ts_to_ct(ts, &ct);
179
180	year = ct.year - mk->mk_year0;
181	if (year > 99 && mk48txx_auto_century_adjust != 0)
182		year -= 100;
183
184	/* enable write */
185	csr = bus_space_read_1(bt, bh, clkoff + MK48TXX_ICSR);
186	csr |= MK48TXX_CSR_WRITE;
187	bus_space_write_1(bt, bh, clkoff + MK48TXX_ICSR, csr);
188
189	bus_space_write_1(bt, bh, clkoff + MK48TXX_ISEC, TOBCD(ct.sec));
190	bus_space_write_1(bt, bh, clkoff + MK48TXX_IMIN, TOBCD(ct.min));
191	bus_space_write_1(bt, bh, clkoff + MK48TXX_IHOUR, TOBCD(ct.hour));
192	bus_space_write_1(bt, bh, clkoff + MK48TXX_IWDAY, TOBCD(ct.dow));
193	bus_space_write_1(bt, bh, clkoff + MK48TXX_IDAY, TOBCD(ct.day));
194	bus_space_write_1(bt, bh, clkoff + MK48TXX_IMON, TOBCD(ct.mon));
195	bus_space_write_1(bt, bh, clkoff + MK48TXX_IYEAR, TOBCD(year));
196
197	/* load them up */
198	csr = bus_space_read_1(bt, bh, clkoff + MK48TXX_ICSR);
199	csr &= ~MK48TXX_CSR_WRITE;
200	bus_space_write_1(bt, bh, clkoff + MK48TXX_ICSR, csr);
201	return (0);
202}
203
204int
205mk48txx_get_nvram_size(device_t dev, bus_size_t *vp)
206{
207	struct mk48txx_softc *mk;
208
209	mk = device_get_softc(dev);
210	*vp = mk->mk_nvramsz;
211	return (0);
212}
213