1139825Simp/*-
293839Stmm * Copyright (c) 1992, 1993
393839Stmm *	The Regents of the University of California.  All rights reserved.
493839Stmm * Copyright (c) 1994 Gordon W. Ross
593839Stmm * Copyright (c) 1993 Adam Glass
693839Stmm * Copyright (c) 1996 Paul Kranenburg
793839Stmm * Copyright (c) 1996
893839Stmm * 	The President and Fellows of Harvard College. All rights reserved.
993839Stmm *
1093839Stmm * This software was developed by the Computer Systems Engineering group
1193839Stmm * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
1293839Stmm * contributed to Berkeley.
1393839Stmm *
1493839Stmm * All advertising materials mentioning features or use of this software
1593839Stmm * must display the following acknowledgement:
1693839Stmm *	This product includes software developed by Harvard University.
1793839Stmm *
1893839Stmm * Redistribution and use in source and binary forms, with or without
1993839Stmm * modification, are permitted provided that the following conditions
2093839Stmm * are met:
2193839Stmm *
2293839Stmm * 1. Redistributions of source code must retain the above copyright
2393839Stmm *    notice, this list of conditions and the following disclaimer.
2493839Stmm * 2. Redistributions in binary form must reproduce the above copyright
2593839Stmm *    notice, this list of conditions and the following disclaimer in the
2693839Stmm *    documentation and/or other materials provided with the distribution.
2793839Stmm * 3. All advertising materials mentioning features or use of this software
2893839Stmm *    must display the following acknowledgement:
2993839Stmm *	This product includes software developed by Paul Kranenburg.
3093839Stmm *	This product includes software developed by Harvard University.
3193839Stmm * 4. Neither the name of the University nor the names of its contributors
3293839Stmm *    may be used to endorse or promote products derived from this software
3393839Stmm *    without specific prior written permission.
3493839Stmm *
3593839Stmm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3693839Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3793839Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3893839Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3993839Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4093839Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4193839Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4293839Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4393839Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4493839Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4593839Stmm * SUCH DAMAGE.
4693839Stmm *
4793839Stmm *	from: @(#)clock.c	8.1 (Berkeley) 6/11/93
4893839Stmm *	from: NetBSD: clock.c,v 1.41 2001/07/24 19:29:25 eeh Exp
4993839Stmm */
5093839Stmm
51146411Smarius#include <sys/cdefs.h>
52146411Smarius__FBSDID("$FreeBSD$");
53146411Smarius
54146411Smarius/*
55146411Smarius * clock (eeprom) attaches at EBus, FireHose or SBus
56146411Smarius */
57146411Smarius
5893839Stmm#include <sys/param.h>
5993839Stmm#include <sys/systm.h>
6093839Stmm#include <sys/bus.h>
61146416Smarius#include <sys/eventhandler.h>
6293839Stmm#include <sys/kernel.h>
63146416Smarius#include <sys/lock.h>
64146411Smarius#include <sys/module.h>
65146416Smarius#include <sys/mutex.h>
6693839Stmm#include <sys/resource.h>
6793839Stmm
68133589Smarius#include <dev/ofw/ofw_bus.h>
69133589Smarius
7093839Stmm#include <machine/bus.h>
7193839Stmm#include <machine/resource.h>
72155726Smarius#include <machine/ver.h>
7393839Stmm
7493839Stmm#include <sys/rman.h>
7593839Stmm
76137813Smarius#include <dev/mk48txx/mk48txxvar.h>
7793839Stmm
7893839Stmm#include "clock_if.h"
7993839Stmm
80146411Smariusstatic devclass_t eeprom_devclass;
81146411Smarius
82146411Smariusstatic device_probe_t eeprom_probe;
83146411Smariusstatic device_attach_t eeprom_attach;
84146411Smarius
85146411Smariusstatic device_method_t eeprom_methods[] = {
86146411Smarius	/* Device interface */
87146411Smarius	DEVMETHOD(device_probe,		eeprom_probe),
88146411Smarius	DEVMETHOD(device_attach,	eeprom_attach),
89146411Smarius
90146411Smarius	/* clock interface */
91146411Smarius	DEVMETHOD(clock_gettime,	mk48txx_gettime),
92146411Smarius	DEVMETHOD(clock_settime,	mk48txx_settime),
93146411Smarius
94229093Shselasky	DEVMETHOD_END
95146411Smarius};
96146411Smarius
97146411Smariusstatic driver_t eeprom_driver = {
98146411Smarius	"eeprom",
99146411Smarius	eeprom_methods,
100146411Smarius	sizeof(struct mk48txx_softc),
101146411Smarius};
102146411Smarius
103146411SmariusDRIVER_MODULE(eeprom, ebus, eeprom_driver, eeprom_devclass, 0, 0);
104146411SmariusDRIVER_MODULE(eeprom, fhc, eeprom_driver, eeprom_devclass, 0, 0);
105146411SmariusDRIVER_MODULE(eeprom, sbus, eeprom_driver, eeprom_devclass, 0, 0);
106146411Smarius
107146411Smariusstatic int
108133589Smariuseeprom_probe(device_t dev)
10993839Stmm{
110221958Smarius	const char *name;
111201005Smarius
112221958Smarius	name = ofw_bus_get_name(dev);
113221958Smarius	if (strcmp(name, "eeprom") == 0 ||
114221958Smarius	    strcmp(name, "FJSV,eeprom") == 0) {
115133589Smarius		device_set_desc(dev, "EEPROM/clock");
116133589Smarius		return (0);
117133589Smarius	}
118133589Smarius	return (ENXIO);
119133589Smarius}
120133589Smarius
121146411Smariusstatic int
122137813Smariuseeprom_attach(device_t dev)
123133589Smarius{
124137813Smarius	struct mk48txx_softc *sc;
12593839Stmm	struct timespec ts;
126170843Smarius	int error, rid;
12793839Stmm
128137813Smarius	sc = device_get_softc(dev);
129137813Smarius
130146416Smarius	mtx_init(&sc->sc_mtx, "eeprom_mtx", NULL, MTX_DEF);
131146416Smarius
132146411Smarius	rid = 0;
133201371Smarius	sc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
134201371Smarius	    RF_ACTIVE);
135201371Smarius	if (sc->sc_res == NULL) {
136146411Smarius		device_printf(dev, "cannot allocate resources\n");
137146416Smarius		error = ENXIO;
138146416Smarius		goto fail_mtx;
139146411Smarius	}
14093839Stmm
141146411Smarius	if ((sc->sc_model = ofw_bus_get_model(dev)) == NULL) {
142146411Smarius		device_printf(dev, "cannot determine model\n");
143146411Smarius		error = ENXIO;
144146411Smarius		goto fail_res;
145146411Smarius	}
146146411Smarius
147155726Smarius	/* Our TOD clock year 0 is 1968. */
148137813Smarius	sc->sc_year0 = 1968;
149146411Smarius	/* Use default register read/write functions. */
150137813Smarius	sc->sc_flag = 0;
151155726Smarius	/*
152155726Smarius	 * Generally, if the `eeprom' node has a `watchdog-enable' property
153155726Smarius	 * this indicates that the watchdog part of the MK48T59 is usable,
154155726Smarius	 * i.e. its RST pin is connected to the WDR input of the CPUs or
155155726Smarius	 * something. The `eeprom' nodes of E250, E450 and the clock board
156155726Smarius	 * variant in Exx00 have such properties. For E250 and E450 the
157155726Smarius	 * watchdog just works, for Exx00 the delivery of the reset signal
158155726Smarius	 * apparently has to be additionally enabled elsewhere...
159155726Smarius	 * The OFW environment variable `watchdog-reboot?' is ignored for
160155726Smarius	 * these watchdogs as they always trigger a system reset when they
161155726Smarius	 * time out and can't be made to issue a break to the boot monitor
162155726Smarius	 * instead.
163155726Smarius	 */
164155726Smarius	if (OF_getproplen(ofw_bus_get_node(dev), "watchdog-enable") != -1 &&
165155726Smarius	    (strcmp(sparc64_model, "SUNW,Ultra-250") == 0 ||
166155726Smarius	    strcmp(sparc64_model, "SUNW,Ultra-4") == 0))
167155726Smarius		sc->sc_flag |= MK48TXX_WDOG_REGISTER | MK48TXX_WDOG_ENABLE_WDS;
168137813Smarius	if ((error = mk48txx_attach(dev)) != 0) {
169137813Smarius		device_printf(dev, "cannot attach time of day clock\n");
170146411Smarius		goto fail_res;
17193839Stmm	}
17293839Stmm
17393839Stmm	if (bootverbose) {
174171553Sdwmalone		if (mk48txx_gettime(dev, &ts) != 0)
175171553Sdwmalone			device_printf(dev, "invalid time");
176171553Sdwmalone		else
177171553Sdwmalone			device_printf(dev, "current time: %ld.%09ld\n",
178171553Sdwmalone			    (long)ts.tv_sec, ts.tv_nsec);
17993839Stmm	}
18093839Stmm
18193839Stmm	return (0);
182146411Smarius
183146411Smarius fail_res:
184201371Smarius	bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->sc_res);
185146416Smarius fail_mtx:
186146416Smarius	mtx_destroy(&sc->sc_mtx);
187146411Smarius
188146411Smarius	return (error);
18993839Stmm}
190