]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/rtc.c
MFV r337022:
[FreeBSD/FreeBSD.git] / sys / sparc64 / sparc64 / rtc.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004 Marius Strobl <marius@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 /*
33  * The `rtc' device is found on the ISA bus and the EBus.  The ISA version
34  * always is a MC146818 compatible clock while the EBus variant either is the
35  * MC146818 compatible Real-Time Clock function of a National Semiconductor
36  * PC87317/PC97317 which also provides Advanced Power Control functionality
37  * or a Texas Instruments bq4802.
38  */
39
40 #include "opt_isa.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bus.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/resource.h>
50
51 #include <dev/ofw/ofw_bus.h>
52
53 #include <machine/bus.h>
54 #include <machine/resource.h>
55
56 #include <sys/rman.h>
57
58 #include <isa/isavar.h>
59
60 #include <dev/mc146818/mc146818reg.h>
61 #include <dev/mc146818/mc146818var.h>
62
63 #include "clock_if.h"
64
65 #define RTC_DESC        "Real-Time Clock"
66
67 #define RTC_READ        mc146818_def_read
68 #define RTC_WRITE       mc146818_def_write
69
70 #define PC87317_COMMON          MC_REGA_DV0     /* bank 0 */
71 #define PC87317_RTC             (MC_REGA_DV1 | MC_REGA_DV0) /* bank 1 */
72 #define PC87317_RTC_CR          0x48            /* Century Register */
73 #define PC87317_APC             MC_REGA_DV2     /* bank 2 */
74 #define PC87317_APC_CADDR       0x51            /* Century Address Register */
75 #define PC87317_APC_CADDR_BANK0 0x00            /* locate CR in bank 0 */
76 #define PC87317_APC_CADDR_BANK1 0x80            /* locate CR in bank 1 */
77
78 static devclass_t rtc_devclass;
79
80 static device_attach_t rtc_attach;
81 static device_probe_t rtc_ebus_probe;
82 #ifdef DEV_ISA
83 static device_probe_t rtc_isa_probe;
84 #endif
85
86 static device_method_t rtc_ebus_methods[] = {
87         /* Device interface */
88         DEVMETHOD(device_probe,         rtc_ebus_probe),
89         DEVMETHOD(device_attach,        rtc_attach),
90
91         /* clock interface */
92         DEVMETHOD(clock_gettime,        mc146818_gettime),
93         DEVMETHOD(clock_settime,        mc146818_settime),
94
95         DEVMETHOD_END
96 };
97
98 static driver_t rtc_ebus_driver = {
99         "rtc",
100         rtc_ebus_methods,
101         sizeof(struct mc146818_softc),
102 };
103
104 DRIVER_MODULE(rtc, ebus, rtc_ebus_driver, rtc_devclass, 0, 0);
105
106 #ifdef DEV_ISA
107 static device_method_t rtc_isa_methods[] = {
108         /* Device interface */
109         DEVMETHOD(device_probe,         rtc_isa_probe),
110         DEVMETHOD(device_attach,        rtc_attach),
111
112         /* clock interface */
113         DEVMETHOD(clock_gettime,        mc146818_gettime),
114         DEVMETHOD(clock_settime,        mc146818_settime),
115
116         DEVMETHOD_END
117 };
118
119 static driver_t rtc_isa_driver = {
120         "rtc",
121         rtc_isa_methods,
122         sizeof(struct mc146818_softc),
123 };
124
125 static struct isa_pnp_id rtc_isa_ids[] = {
126         { 0x000bd041, RTC_DESC }, /* PNP0B00 */
127         { 0 }
128 };
129
130 DRIVER_MODULE(rtc, isa, rtc_isa_driver, rtc_devclass, 0, 0);
131 ISA_PNP_INFO(rtc_isa_ids);
132 #endif
133
134 static u_int pc87317_getcent(device_t dev);
135 static void pc87317_setcent(device_t dev, u_int cent);
136
137 static int
138 rtc_ebus_probe(device_t dev)
139 {
140
141         if (strcmp(ofw_bus_get_name(dev), "rtc") == 0) {
142                 /* The bq4802 is not supported, yet. */
143                 if (ofw_bus_get_compat(dev) != NULL &&
144                     strcmp(ofw_bus_get_compat(dev), "bq4802") == 0)
145                         return (ENXIO);
146                 device_set_desc(dev, RTC_DESC);
147                 return (0);
148         }
149
150         return (ENXIO);
151 }
152
153 #ifdef DEV_ISA
154 static int
155 rtc_isa_probe(device_t dev)
156 {
157
158         if (ISA_PNP_PROBE(device_get_parent(dev), dev, rtc_isa_ids) == 0)
159                 return (0);
160
161         return (ENXIO);
162 }
163 #endif
164
165 static int
166 rtc_attach(device_t dev)
167 {
168         struct timespec ts;
169         struct mc146818_softc *sc;
170         struct resource *res;
171         int ebus, error, rid;
172
173         sc = device_get_softc(dev);
174
175         mtx_init(&sc->sc_mtx, "rtc_mtx", NULL, MTX_SPIN);
176
177         ebus = 0;
178         if (strcmp(device_get_name(device_get_parent(dev)), "ebus") == 0)
179                 ebus = 1;
180
181         rid = 0;
182         res = bus_alloc_resource_any(dev, ebus ? SYS_RES_MEMORY :
183             SYS_RES_IOPORT, &rid, RF_ACTIVE);
184         if (res == NULL) {
185                 device_printf(dev, "cannot allocate resources\n");
186                 error = ENXIO;
187                 goto fail_mtx;
188         }
189         sc->sc_bst = rman_get_bustag(res);
190         sc->sc_bsh = rman_get_bushandle(res);
191
192         sc->sc_mcread = RTC_READ;
193         sc->sc_mcwrite = RTC_WRITE;
194         /* The TOD clock year 0 is 0. */
195         sc->sc_year0 = 0;
196         /*
197          * For ISA use the default century get/set functions, for EBus we
198          * provide our own versions.
199          */
200         sc->sc_flag = MC146818_NO_CENT_ADJUST;
201         if (ebus) {
202                 /*
203                  * Make sure the CR is at the default location (also used
204                  * by Solaris).
205                  */
206                 RTC_WRITE(dev, MC_REGA, PC87317_APC);
207                 RTC_WRITE(dev, PC87317_APC_CADDR, PC87317_APC_CADDR_BANK1 |
208                     PC87317_RTC_CR);
209                 RTC_WRITE(dev, MC_REGA, PC87317_COMMON);
210                 sc->sc_getcent = pc87317_getcent;
211                 sc->sc_setcent = pc87317_setcent;
212         }
213         if ((error = mc146818_attach(dev)) != 0) {
214                 device_printf(dev, "cannot attach time of day clock\n");
215                 goto fail_res;
216         }
217
218         if (bootverbose) {
219                 if (mc146818_gettime(dev, &ts) != 0)
220                         device_printf(dev, "invalid time");
221                 else
222                         device_printf(dev, "current time: %ld.%09ld\n",
223                             (long)ts.tv_sec, ts.tv_nsec);
224         }
225
226         return (0);
227
228  fail_res:
229         bus_release_resource(dev, ebus ? SYS_RES_MEMORY : SYS_RES_IOPORT, rid,
230             res);
231  fail_mtx:
232         mtx_destroy(&sc->sc_mtx);
233
234         return (error);
235 }
236
237 static u_int
238 pc87317_getcent(device_t dev)
239 {
240         u_int cent;
241
242         RTC_WRITE(dev, MC_REGA, PC87317_RTC);
243         cent = RTC_READ(dev, PC87317_RTC_CR);
244         RTC_WRITE(dev, MC_REGA, PC87317_COMMON);
245         return (cent);
246 }
247
248 static void
249 pc87317_setcent(device_t dev, u_int cent)
250 {
251
252         RTC_WRITE(dev, MC_REGA, PC87317_RTC);
253         RTC_WRITE(dev, PC87317_RTC_CR, cent);
254         RTC_WRITE(dev, MC_REGA, PC87317_COMMON);
255 }