mk48txx.c revision 161837
193836Stmm/*-
293836Stmm * Copyright (c) 2000 The NetBSD Foundation, Inc.
393836Stmm * All rights reserved.
493836Stmm *
593836Stmm * This code is derived from software contributed to The NetBSD Foundation
693836Stmm * by Paul Kranenburg.
793836Stmm *
893836Stmm * Redistribution and use in source and binary forms, with or without
993836Stmm * modification, are permitted provided that the following conditions
1093836Stmm * are met:
1193836Stmm * 1. Redistributions of source code must retain the above copyright
1293836Stmm *    notice, this list of conditions and the following disclaimer.
1393836Stmm * 2. Redistributions in binary form must reproduce the above copyright
1493836Stmm *    notice, this list of conditions and the following disclaimer in the
1593836Stmm *    documentation and/or other materials provided with the distribution.
1693836Stmm * 3. All advertising materials mentioning features or use of this software
1793836Stmm *    must display the following acknowledgement:
1893836Stmm *        This product includes software developed by the NetBSD
1993836Stmm *        Foundation, Inc. and its contributors.
2093836Stmm * 4. Neither the name of The NetBSD Foundation nor the names of its
2193836Stmm *    contributors may be used to endorse or promote products derived
2293836Stmm *    from this software without specific prior written permission.
2393836Stmm *
2493836Stmm * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2593836Stmm * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2693836Stmm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2793836Stmm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2893836Stmm * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2993836Stmm * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3093836Stmm * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3193836Stmm * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3293836Stmm * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3393836Stmm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3493836Stmm * POSSIBILITY OF SUCH DAMAGE.
3593836Stmm *
36137813Smarius *	from: NetBSD: mk48txx.c,v 1.15 2004/07/05 09:24:31 pk Exp
3793836Stmm */
3893836Stmm
39119418Sobrien#include <sys/cdefs.h>
40119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/mk48txx/mk48txx.c 161837 2006-09-01 20:14:06Z marius $");
41119418Sobrien
4293836Stmm/*
43137813Smarius * Mostek MK48T02, MK48T08, MK48T18, MK48T59 time-of-day chip subroutines.
4493836Stmm */
4593836Stmm
4693836Stmm#include <sys/param.h>
4793836Stmm#include <sys/systm.h>
4893836Stmm#include <sys/bus.h>
4993836Stmm#include <sys/clock.h>
50146416Smarius#include <sys/eventhandler.h>
51146416Smarius#include <sys/lock.h>
52146416Smarius#include <sys/mutex.h>
53146416Smarius#include <sys/watchdog.h>
5493836Stmm
5593836Stmm#include <machine/bus.h>
5693836Stmm
57119352Smarcel#include <dev/mk48txx/mk48txxreg.h>
58137813Smarius#include <dev/mk48txx/mk48txxvar.h>
5993836Stmm
6093836Stmm#include "clock_if.h"
6193836Stmm
62137813Smariusstatic uint8_t	mk48txx_def_nvrd(device_t, int);
63146416Smariusstatic void	mk48txx_def_nvwr(device_t, int, uint8_t);
64146416Smariusstatic void	mk48txx_watchdog(void *, u_int, int *);
6593836Stmm
6693836Stmmstruct {
6793836Stmm	const char *name;
6893836Stmm	bus_size_t nvramsz;
6993836Stmm	bus_size_t clkoff;
7093836Stmm	int flags;
7193836Stmm#define MK48TXX_EXT_REGISTERS	1	/* Has extended register set */
7293836Stmm} mk48txx_models[] = {
7393836Stmm	{ "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
7493836Stmm	{ "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
75137813Smarius	{ "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 },
7693836Stmm	{ "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
7793836Stmm};
7893836Stmm
7993836Stmmint
80137813Smariusmk48txx_attach(device_t dev)
8193836Stmm{
8293836Stmm	struct mk48txx_softc *sc;
8393836Stmm	int i;
84146416Smarius	uint8_t wday;
8593836Stmm
86137813Smarius	sc = device_get_softc(dev);
87137813Smarius
88146416Smarius	if (mtx_initialized(&sc->sc_mtx) == 0) {
89146416Smarius		device_printf(dev, "%s: mutex not initialized\n", __func__);
90146416Smarius		return (ENXIO);
91146416Smarius	}
92146416Smarius
93137813Smarius	device_printf(dev, "model %s", sc->sc_model);
9493836Stmm	i = sizeof(mk48txx_models) / sizeof(mk48txx_models[0]);
9593836Stmm	while (--i >= 0) {
96137813Smarius		if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0) {
9793836Stmm			break;
9893836Stmm		}
9993836Stmm	}
10093836Stmm	if (i < 0) {
10193836Stmm		device_printf(dev, " (unsupported)\n");
10293836Stmm		return (ENXIO);
10393836Stmm	}
10493836Stmm	printf("\n");
105137813Smarius	sc->sc_nvramsz = mk48txx_models[i].nvramsz;
106137813Smarius	sc->sc_clkoffset = mk48txx_models[i].clkoff;
10793836Stmm
108137813Smarius	if (sc->sc_nvrd == NULL)
109137813Smarius		sc->sc_nvrd = mk48txx_def_nvrd;
110137813Smarius	if (sc->sc_nvwr == NULL)
111137813Smarius		sc->sc_nvwr = mk48txx_def_nvwr;
112137813Smarius
113146416Smarius	if (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS) {
114146416Smarius		mtx_lock(&sc->sc_mtx);
115146416Smarius	    	if ((*sc->sc_nvrd)(dev, sc->sc_clkoffset + MK48TXX_FLAGS) &
116146416Smarius		    MK48TXX_FLAGS_BL) {
117146416Smarius			mtx_unlock(&sc->sc_mtx);
118146416Smarius			device_printf(dev, "%s: battery low\n", __func__);
119146416Smarius			return (ENXIO);
120146416Smarius		}
121146416Smarius		mtx_unlock(&sc->sc_mtx);
122135481Smarius	}
123135481Smarius
124146416Smarius	if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) {
125146416Smarius		/*
126146416Smarius		 * Use MK48TXX_WDAY_CB instead of manually adjusting the
127146416Smarius		 * century.
128146416Smarius		 */
129146416Smarius		if (!(mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) {
130146416Smarius			device_printf(dev, "%s: no century bit\n", __func__);
131146416Smarius			return (ENXIO);
132146416Smarius		} else {
133146416Smarius			mtx_lock(&sc->sc_mtx);
134146416Smarius			wday = (*sc->sc_nvrd)
135146416Smarius			    (dev, sc->sc_clkoffset + MK48TXX_IWDAY);
136146416Smarius			wday |= MK48TXX_WDAY_CEB;
137146416Smarius			(*sc->sc_nvwr)
138146416Smarius			    (dev, sc->sc_clkoffset + MK48TXX_IWDAY, wday);
139146416Smarius			mtx_unlock(&sc->sc_mtx);
140146416Smarius		}
141146416Smarius	}
142146416Smarius
14393836Stmm	clock_register(dev, 1000000);	/* 1 second resolution. */
14493836Stmm
145146416Smarius	if ((sc->sc_flag & MK48TXX_WDOG_REGISTER) &&
146146416Smarius	    (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) {
147146416Smarius		sc->sc_wet = EVENTHANDLER_REGISTER(watchdog_list,
148146416Smarius		    mk48txx_watchdog, dev, 0);
149146416Smarius		device_printf(dev,
150161837Smarius		    "watchdog registered, timeout interval max. 128 sec\n");
151146416Smarius	}
152146416Smarius
15393836Stmm	return (0);
15493836Stmm}
15593836Stmm
15693836Stmm/*
15793836Stmm * Get time-of-day and convert to a `struct timespec'
15893836Stmm * Return 0 on success; an error number otherwise.
15993836Stmm */
16093836Stmmint
16193836Stmmmk48txx_gettime(device_t dev, struct timespec *ts)
16293836Stmm{
163137813Smarius	struct mk48txx_softc *sc;
164137813Smarius	bus_size_t clkoff;
16593836Stmm	struct clocktime ct;
16693836Stmm	int year;
167146416Smarius	uint8_t csr;
16893836Stmm
169137813Smarius	sc = device_get_softc(dev);
170137813Smarius	clkoff = sc->sc_clkoffset;
171137813Smarius
172146416Smarius	mtx_lock(&sc->sc_mtx);
17393836Stmm	/* enable read (stop time) */
174137813Smarius	csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
17593836Stmm	csr |= MK48TXX_CSR_READ;
176137813Smarius	(*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
17793836Stmm
178137813Smarius#define	FROMREG(reg, mask)	((*sc->sc_nvrd)(dev, clkoff + (reg)) & (mask))
179135481Smarius
18093836Stmm	ct.nsec = 0;
181135481Smarius	ct.sec = FROMBCD(FROMREG(MK48TXX_ISEC, MK48TXX_SEC_MASK));
182135481Smarius	ct.min = FROMBCD(FROMREG(MK48TXX_IMIN, MK48TXX_MIN_MASK));
183135481Smarius	ct.hour = FROMBCD(FROMREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK));
184135481Smarius	ct.day = FROMBCD(FROMREG(MK48TXX_IDAY, MK48TXX_DAY_MASK));
185135481Smarius	/* Map dow from 1 - 7 to 0 - 6; FROMBCD() isn't necessary here. */
186135481Smarius	ct.dow = FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK) - 1;
187135481Smarius	ct.mon = FROMBCD(FROMREG(MK48TXX_IMON, MK48TXX_MON_MASK));
188135481Smarius	year = FROMBCD(FROMREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK));
189146416Smarius	year += sc->sc_year0;
190146416Smarius	if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST)
191146416Smarius		year += (FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB) >>
192146416Smarius		    MK48TXX_WDAY_CB_SHIFT) * 100;
193146416Smarius	else if (year < POSIX_BASE_YEAR)
194146416Smarius		year += 100;
19593836Stmm
196135481Smarius#undef FROMREG
197135481Smarius
19893836Stmm	ct.year = year;
19993836Stmm
20093836Stmm	/* time wears on */
201137813Smarius	csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
20293836Stmm	csr &= ~MK48TXX_CSR_READ;
203137813Smarius	(*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
204146416Smarius	mtx_unlock(&sc->sc_mtx);
20593836Stmm
20693836Stmm	return (clock_ct_to_ts(&ct, ts));
20793836Stmm}
20893836Stmm
20993836Stmm/*
21093836Stmm * Set the time-of-day clock based on the value of the `struct timespec' arg.
21193836Stmm * Return 0 on success; an error number otherwise.
21293836Stmm */
21393836Stmmint
21493836Stmmmk48txx_settime(device_t dev, struct timespec *ts)
21593836Stmm{
216137813Smarius	struct mk48txx_softc *sc;
217137813Smarius	bus_size_t clkoff;
21893836Stmm	struct clocktime ct;
219146416Smarius	uint8_t csr;
220146416Smarius	int cent, year;
22193836Stmm
222137813Smarius	sc = device_get_softc(dev);
223137813Smarius	clkoff = sc->sc_clkoffset;
224137813Smarius
22593836Stmm	/* Accuracy is only one second. */
22693836Stmm	if (ts->tv_nsec >= 500000000)
22793836Stmm		ts->tv_sec++;
22893836Stmm	ts->tv_nsec = 0;
22993836Stmm	clock_ts_to_ct(ts, &ct);
23093836Stmm
231146416Smarius	mtx_lock(&sc->sc_mtx);
23293836Stmm	/* enable write */
233137813Smarius	csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
23493836Stmm	csr |= MK48TXX_CSR_WRITE;
235137813Smarius	(*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
23693836Stmm
237135481Smarius#define	TOREG(reg, mask, val)						\
238137813Smarius	((*sc->sc_nvwr)(dev, clkoff + (reg),				\
239137813Smarius	((*sc->sc_nvrd)(dev, clkoff + (reg)) & ~(mask)) |		\
240135481Smarius	((val) & (mask))))
24193836Stmm
242135481Smarius	TOREG(MK48TXX_ISEC, MK48TXX_SEC_MASK, TOBCD(ct.sec));
243135481Smarius	TOREG(MK48TXX_IMIN, MK48TXX_MIN_MASK, TOBCD(ct.min));
244135481Smarius	TOREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK, TOBCD(ct.hour));
245135481Smarius	/* Map dow from 0 - 6 to 1 - 7; TOBCD() isn't necessary here. */
246135481Smarius	TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK, ct.dow + 1);
247135481Smarius	TOREG(MK48TXX_IDAY, MK48TXX_DAY_MASK, TOBCD(ct.day));
248135481Smarius	TOREG(MK48TXX_IMON, MK48TXX_MON_MASK, TOBCD(ct.mon));
249146416Smarius
250146416Smarius	year = ct.year - sc->sc_year0;
251146416Smarius	if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) {
252146416Smarius		cent = year / 100;
253146416Smarius		TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB,
254146416Smarius		    cent << MK48TXX_WDAY_CB_SHIFT);
255146416Smarius		year -= cent * 100;
256146416Smarius	} else if (year > 99)
257146416Smarius		year -= 100;
258135481Smarius	TOREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK, TOBCD(year));
259135481Smarius
260135481Smarius#undef TOREG
261135481Smarius
26293836Stmm	/* load them up */
263137813Smarius	csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
26493836Stmm	csr &= ~MK48TXX_CSR_WRITE;
265137813Smarius	(*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
266146416Smarius	mtx_unlock(&sc->sc_mtx);
26793836Stmm	return (0);
26893836Stmm}
26993836Stmm
270146416Smariusstatic uint8_t
271137813Smariusmk48txx_def_nvrd(device_t dev, int off)
27293836Stmm{
273137813Smarius	struct mk48txx_softc *sc;
27493836Stmm
275137813Smarius	sc = device_get_softc(dev);
276137813Smarius	return (bus_space_read_1(sc->sc_bst, sc->sc_bsh, off));
27793836Stmm}
278137813Smarius
279137813Smariusstatic void
280146416Smariusmk48txx_def_nvwr(device_t dev, int off, uint8_t v)
281137813Smarius{
282137813Smarius	struct mk48txx_softc *sc;
283137813Smarius
284137813Smarius	sc = device_get_softc(dev);
285137813Smarius	bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, v);
286137813Smarius}
287146416Smarius
288146416Smariusstatic void
289146416Smariusmk48txx_watchdog(void *arg, u_int cmd, int *error)
290146416Smarius{
291146416Smarius	device_t dev;
292146416Smarius	struct mk48txx_softc *sc;
293146416Smarius	uint8_t t, wdog;
294146416Smarius
295146416Smarius	dev = arg;
296146416Smarius	sc = device_get_softc(dev);
297146416Smarius
298146416Smarius	wdog = 0;
299146416Smarius	t = cmd & WD_INTERVAL;
300146416Smarius	if (cmd != 0 && t >= 26 && t <= 37) {
301146416Smarius		if (t <= WD_TO_2SEC) {
302146416Smarius			wdog |= MK48TXX_WDOG_RB_1_16;
303146416Smarius			t -= 26;
304146416Smarius		} else if (t <= WD_TO_8SEC) {
305146416Smarius			wdog |= MK48TXX_WDOG_RB_1_4;
306146416Smarius			t -= WD_TO_250MS;
307146416Smarius		} else if (t <= WD_TO_32SEC) {
308146416Smarius			wdog |= MK48TXX_WDOG_RB_1;
309146416Smarius			t -= WD_TO_1SEC;
310146416Smarius		} else {
311146416Smarius			wdog |= MK48TXX_WDOG_RB_4;
312146416Smarius			t -= WD_TO_4SEC;
313146416Smarius		}
314146416Smarius		wdog |= (min(1 << t,
315146416Smarius		    MK48TXX_WDOG_BMB_MASK >> MK48TXX_WDOG_BMB_SHIFT)) <<
316146416Smarius		    MK48TXX_WDOG_BMB_SHIFT;
317146416Smarius		if (sc->sc_flag & MK48TXX_WDOG_ENABLE_WDS)
318146416Smarius			wdog |= MK48TXX_WDOG_WDS;
319146416Smarius		*error = 0;
320146416Smarius	}
321146416Smarius	mtx_lock(&sc->sc_mtx);
322146416Smarius	(*sc->sc_nvwr)(dev, sc->sc_clkoffset + MK48TXX_WDOG, wdog);
323146416Smarius	mtx_unlock(&sc->sc_mtx);
324146416Smarius}
325