]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/sparc64/sparc64/eeprom.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sys / sparc64 / sparc64 / eeprom.c
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1994 Gordon W. Ross
5  * Copyright (c) 1993 Adam Glass
6  * Copyright (c) 1996 Paul Kranenburg
7  * Copyright (c) 1996
8  *      The President and Fellows of Harvard College. All rights reserved.
9  *
10  * This software was developed by the Computer Systems Engineering group
11  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
12  * contributed to Berkeley.
13  *
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed by Harvard University.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  *
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 3. All advertising materials mentioning features or use of this software
28  *    must display the following acknowledgement:
29  *      This product includes software developed by Paul Kranenburg.
30  *      This product includes software developed by Harvard University.
31  * 4. Neither the name of the University nor the names of its contributors
32  *    may be used to endorse or promote products derived from this software
33  *    without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  *
47  *      from: @(#)clock.c       8.1 (Berkeley) 6/11/93
48  *      from: NetBSD: clock.c,v 1.41 2001/07/24 19:29:25 eeh Exp
49  */
50
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
53
54 /*
55  * clock (eeprom) attaches at EBus, FireHose or SBus
56  */
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/bus.h>
61 #include <sys/eventhandler.h>
62 #include <sys/kernel.h>
63 #include <sys/lock.h>
64 #include <sys/module.h>
65 #include <sys/mutex.h>
66 #include <sys/resource.h>
67
68 #include <dev/ofw/ofw_bus.h>
69
70 #include <machine/bus.h>
71 #include <machine/idprom.h>
72 #include <machine/resource.h>
73 #include <machine/ver.h>
74
75 #include <sys/rman.h>
76
77 #include <dev/mk48txx/mk48txxvar.h>
78
79 #include "clock_if.h"
80
81 #define IDPROM_OFFSET   40
82
83 static devclass_t eeprom_devclass;
84
85 static device_probe_t eeprom_probe;
86 static device_attach_t eeprom_attach;
87
88 static device_method_t eeprom_methods[] = {
89         /* Device interface */
90         DEVMETHOD(device_probe,         eeprom_probe),
91         DEVMETHOD(device_attach,        eeprom_attach),
92
93         /* clock interface */
94         DEVMETHOD(clock_gettime,        mk48txx_gettime),
95         DEVMETHOD(clock_settime,        mk48txx_settime),
96
97         { 0, 0 }
98 };
99
100 static driver_t eeprom_driver = {
101         "eeprom",
102         eeprom_methods,
103         sizeof(struct mk48txx_softc),
104 };
105
106 DRIVER_MODULE(eeprom, ebus, eeprom_driver, eeprom_devclass, 0, 0);
107 DRIVER_MODULE(eeprom, fhc, eeprom_driver, eeprom_devclass, 0, 0);
108 DRIVER_MODULE(eeprom, sbus, eeprom_driver, eeprom_devclass, 0, 0);
109
110 static int
111 eeprom_probe(device_t dev)
112 {
113  
114         if (strcmp("eeprom", ofw_bus_get_name(dev)) == 0) {
115                 device_set_desc(dev, "EEPROM/clock");
116                 return (0);
117         }
118         return (ENXIO);
119 }
120
121 static int
122 eeprom_attach(device_t dev)
123 {
124         struct mk48txx_softc *sc;
125         struct resource *res;
126         struct timespec ts;
127         uint32_t h;
128         int error, i, rid;
129
130         sc = device_get_softc(dev);
131         bzero(sc, sizeof(struct mk48txx_softc));
132
133         mtx_init(&sc->sc_mtx, "eeprom_mtx", NULL, MTX_DEF);
134
135         rid = 0;
136         res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
137         if (res == NULL) {
138                 device_printf(dev, "cannot allocate resources\n");
139                 error = ENXIO;
140                 goto fail_mtx;
141         }
142         sc->sc_bst = rman_get_bustag(res);
143         sc->sc_bsh = rman_get_bushandle(res);
144
145         if ((sc->sc_model = ofw_bus_get_model(dev)) == NULL) {
146                 device_printf(dev, "cannot determine model\n");
147                 error = ENXIO;
148                 goto fail_res;
149         }
150
151         /* Our TOD clock year 0 is 1968. */
152         sc->sc_year0 = 1968;
153         /* Use default register read/write functions. */
154         sc->sc_flag = 0;
155         /*
156          * Generally, if the `eeprom' node has a `watchdog-enable' property
157          * this indicates that the watchdog part of the MK48T59 is usable,
158          * i.e. its RST pin is connected to the WDR input of the CPUs or
159          * something. The `eeprom' nodes of E250, E450 and the clock board
160          * variant in Exx00 have such properties. For E250 and E450 the
161          * watchdog just works, for Exx00 the delivery of the reset signal
162          * apparently has to be additionally enabled elsewhere...
163          * The OFW environment variable `watchdog-reboot?' is ignored for
164          * these watchdogs as they always trigger a system reset when they
165          * time out and can't be made to issue a break to the boot monitor
166          * instead.
167          */
168         if (OF_getproplen(ofw_bus_get_node(dev), "watchdog-enable") != -1 &&
169             (strcmp(sparc64_model, "SUNW,Ultra-250") == 0 ||
170             strcmp(sparc64_model, "SUNW,Ultra-4") == 0))
171                 sc->sc_flag |= MK48TXX_WDOG_REGISTER | MK48TXX_WDOG_ENABLE_WDS;
172         if ((error = mk48txx_attach(dev)) != 0) {
173                 device_printf(dev, "cannot attach time of day clock\n");
174                 goto fail_res;
175         }
176
177         /*
178          * Get the hostid from the NVRAM. This serves no real purpose other
179          * than being able to display it below as not all sparc64 models
180          * have an `eeprom' device and even some that do store the hostid
181          * elsewhere. The hostid in the NVRAM of the MK48Txx reads all zero
182          * on the latter models. A generic way to retrieve the hostid is to
183          * use the `idprom' node.
184          */
185         mtx_lock(&sc->sc_mtx);
186         h = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_nvramsz -
187             IDPROM_OFFSET + offsetof(struct idprom, id_machine)) << 24;
188         for (i = 0; i < 3; i++) {
189                 h |= bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_nvramsz -
190                     IDPROM_OFFSET + offsetof(struct idprom, id_hostid[i])) <<
191                     ((2 - i) * 8);
192         }
193         mtx_unlock(&sc->sc_mtx);
194         if (h != 0)
195                 device_printf(dev, "hostid %x\n", (u_int)h);
196
197         if (bootverbose) {
198                 mk48txx_gettime(dev, &ts);
199                 device_printf(dev, "current time: %ld.%09ld\n", (long)ts.tv_sec,
200                     ts.tv_nsec);
201         }
202
203         return (0);
204
205  fail_res:
206         bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
207  fail_mtx:
208         mtx_destroy(&sc->sc_mtx);
209
210         return (error);
211 }