]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mk48txx/mk48txx.c
Add support for USB 3.0 XHCI via ACPI
[FreeBSD/FreeBSD.git] / sys / dev / mk48txx / mk48txx.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  *      $NetBSD: mk48txx.c,v 1.25 2008/04/28 20:23:50 martin Exp $
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 /*
38  * Mostek MK48T02, MK48T08, MK48T18, MK48T37 and MK48T59 time-of-day chip
39  * subroutines
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bus.h>
45 #include <sys/clock.h>
46 #include <sys/eventhandler.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/rman.h>
50 #include <sys/watchdog.h>
51
52 #include <machine/bus.h>
53
54 #include <dev/mk48txx/mk48txxreg.h>
55 #include <dev/mk48txx/mk48txxvar.h>
56
57 #include "clock_if.h"
58
59 static uint8_t  mk48txx_def_nvrd(device_t dev, int off);
60 static void     mk48txx_def_nvwr(device_t dev, int off, uint8_t v);
61 static void     mk48txx_watchdog(void *arg, u_int cmd, int *error);
62
63 static const struct {
64         const char *name;
65         bus_size_t nvramsz;
66         bus_size_t clkoff;
67         u_int flags;
68 #define MK48TXX_EXT_REGISTERS   1       /* Has extended register set. */
69 } mk48txx_models[] = {
70         { "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 },
71         { "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 },
72         { "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 },
73         { "mk48t37", MK48T37_CLKSZ, MK48T37_CLKOFF, MK48TXX_EXT_REGISTERS },
74         { "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS },
75 };
76
77 int
78 mk48txx_attach(device_t dev)
79 {
80         struct mk48txx_softc *sc;
81         int i;
82         uint8_t wday;
83
84         sc = device_get_softc(dev);
85
86         if (mtx_initialized(&sc->sc_mtx) == 0) {
87                 device_printf(dev, "%s: mutex not initialized\n", __func__);
88                 return (ENXIO);
89         }
90
91         device_printf(dev, "model %s", sc->sc_model);
92         i = sizeof(mk48txx_models) / sizeof(mk48txx_models[0]);
93         while (--i >= 0) {
94                 if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0) {
95                         break;
96                 }
97         }
98         if (i < 0) {
99                 device_printf(dev, " (unsupported)\n");
100                 return (ENXIO);
101         }
102         printf("\n");
103         sc->sc_nvramsz = mk48txx_models[i].nvramsz;
104         sc->sc_clkoffset = mk48txx_models[i].clkoff;
105
106         if (sc->sc_nvrd == NULL)
107                 sc->sc_nvrd = mk48txx_def_nvrd;
108         if (sc->sc_nvwr == NULL)
109                 sc->sc_nvwr = mk48txx_def_nvwr;
110
111         if (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS) {
112                 mtx_lock(&sc->sc_mtx);
113                 if ((*sc->sc_nvrd)(dev, sc->sc_clkoffset + MK48TXX_FLAGS) &
114                     MK48TXX_FLAGS_BL) {
115                         mtx_unlock(&sc->sc_mtx);
116                         device_printf(dev, "%s: battery low\n", __func__);
117                         return (ENXIO);
118                 }
119                 mtx_unlock(&sc->sc_mtx);
120         }
121
122         if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) {
123                 /*
124                  * Use MK48TXX_WDAY_CB instead of manually adjusting the
125                  * century.
126                  */
127                 if (!(mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) {
128                         device_printf(dev, "%s: no century bit\n", __func__);
129                         return (ENXIO);
130                 } else {
131                         mtx_lock(&sc->sc_mtx);
132                         wday = (*sc->sc_nvrd)
133                             (dev, sc->sc_clkoffset + MK48TXX_IWDAY);
134                         wday |= MK48TXX_WDAY_CEB;
135                         (*sc->sc_nvwr)
136                             (dev, sc->sc_clkoffset + MK48TXX_IWDAY, wday);
137                         mtx_unlock(&sc->sc_mtx);
138                 }
139         }
140
141         clock_register(dev, 1000000);   /* 1 second resolution */
142
143         if ((sc->sc_flag & MK48TXX_WDOG_REGISTER) &&
144             (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) {
145                 sc->sc_wet = EVENTHANDLER_REGISTER(watchdog_list,
146                     mk48txx_watchdog, dev, 0);
147                 device_printf(dev,
148                     "watchdog registered, timeout interval max. 128 sec\n");
149         }
150
151         return (0);
152 }
153
154 /*
155  * Get time-of-day and convert to a `struct timespec'
156  * Return 0 on success; an error number otherwise.
157  */
158 int
159 mk48txx_gettime(device_t dev, struct timespec *ts)
160 {
161         struct mk48txx_softc *sc;
162         bus_size_t clkoff;
163         struct clocktime ct;
164         int year;
165         uint8_t csr;
166
167         sc = device_get_softc(dev);
168         clkoff = sc->sc_clkoffset;
169
170         mtx_lock(&sc->sc_mtx);
171         /* enable read (stop time) */
172         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
173         csr |= MK48TXX_CSR_READ;
174         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
175
176 #define FROMREG(reg, mask)      ((*sc->sc_nvrd)(dev, clkoff + (reg)) & (mask))
177
178         ct.nsec = 0;
179         ct.sec = FROMBCD(FROMREG(MK48TXX_ISEC, MK48TXX_SEC_MASK));
180         ct.min = FROMBCD(FROMREG(MK48TXX_IMIN, MK48TXX_MIN_MASK));
181         ct.hour = FROMBCD(FROMREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK));
182         ct.day = FROMBCD(FROMREG(MK48TXX_IDAY, MK48TXX_DAY_MASK));
183 #if 0
184         /* Map dow from 1 - 7 to 0 - 6; FROMBCD() isn't necessary here. */
185         ct.dow = FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK) - 1;
186 #else
187         /*
188          * Set dow = -1 because some drivers (for example the NetBSD and
189          * OpenBSD mk48txx(4)) don't set it correctly.
190          */
191         ct.dow = -1;
192 #endif
193         ct.mon = FROMBCD(FROMREG(MK48TXX_IMON, MK48TXX_MON_MASK));
194         year = FROMBCD(FROMREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK));
195         year += sc->sc_year0;
196         if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST)
197                 year += (FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB) >>
198                     MK48TXX_WDAY_CB_SHIFT) * 100;
199         else if (year < POSIX_BASE_YEAR)
200                 year += 100;
201
202 #undef FROMREG
203
204         ct.year = year;
205
206         /* time wears on */
207         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
208         csr &= ~MK48TXX_CSR_READ;
209         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
210         mtx_unlock(&sc->sc_mtx);
211
212         return (clock_ct_to_ts(&ct, ts));
213 }
214
215 /*
216  * Set the time-of-day clock based on the value of the `struct timespec' arg.
217  * Return 0 on success; an error number otherwise.
218  */
219 int
220 mk48txx_settime(device_t dev, struct timespec *ts)
221 {
222         struct mk48txx_softc *sc;
223         bus_size_t clkoff;
224         struct clocktime ct;
225         uint8_t csr;
226         int cent, year;
227
228         sc = device_get_softc(dev);
229         clkoff = sc->sc_clkoffset;
230
231         /* Accuracy is only one second. */
232         if (ts->tv_nsec >= 500000000)
233                 ts->tv_sec++;
234         ts->tv_nsec = 0;
235         clock_ts_to_ct(ts, &ct);
236
237         mtx_lock(&sc->sc_mtx);
238         /* enable write */
239         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
240         csr |= MK48TXX_CSR_WRITE;
241         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
242
243 #define TOREG(reg, mask, val)                                           \
244         ((*sc->sc_nvwr)(dev, clkoff + (reg),                            \
245         ((*sc->sc_nvrd)(dev, clkoff + (reg)) & ~(mask)) |               \
246         ((val) & (mask))))
247
248         TOREG(MK48TXX_ISEC, MK48TXX_SEC_MASK, TOBCD(ct.sec));
249         TOREG(MK48TXX_IMIN, MK48TXX_MIN_MASK, TOBCD(ct.min));
250         TOREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK, TOBCD(ct.hour));
251         /* Map dow from 0 - 6 to 1 - 7; TOBCD() isn't necessary here. */
252         TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK, ct.dow + 1);
253         TOREG(MK48TXX_IDAY, MK48TXX_DAY_MASK, TOBCD(ct.day));
254         TOREG(MK48TXX_IMON, MK48TXX_MON_MASK, TOBCD(ct.mon));
255
256         year = ct.year - sc->sc_year0;
257         if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) {
258                 cent = year / 100;
259                 TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB,
260                     cent << MK48TXX_WDAY_CB_SHIFT);
261                 year -= cent * 100;
262         } else if (year > 99)
263                 year -= 100;
264         TOREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK, TOBCD(year));
265
266 #undef TOREG
267
268         /* load them up */
269         csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR);
270         csr &= ~MK48TXX_CSR_WRITE;
271         (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr);
272         mtx_unlock(&sc->sc_mtx);
273         return (0);
274 }
275
276 static uint8_t
277 mk48txx_def_nvrd(device_t dev, int off)
278 {
279         struct mk48txx_softc *sc;
280
281         sc = device_get_softc(dev);
282         return (bus_read_1(sc->sc_res, off));
283 }
284
285 static void
286 mk48txx_def_nvwr(device_t dev, int off, uint8_t v)
287 {
288         struct mk48txx_softc *sc;
289
290         sc = device_get_softc(dev);
291         bus_write_1(sc->sc_res, off, v);
292 }
293
294 static void
295 mk48txx_watchdog(void *arg, u_int cmd, int *error)
296 {
297         device_t dev;
298         struct mk48txx_softc *sc;
299         uint8_t t, wdog;
300
301         dev = arg;
302         sc = device_get_softc(dev);
303
304         t = cmd & WD_INTERVAL;
305         if (t >= 26 && t <= 37) {
306                 wdog = 0;
307                 if (t <= WD_TO_2SEC) {
308                         wdog |= MK48TXX_WDOG_RB_1_16;
309                         t -= 26;
310                 } else if (t <= WD_TO_8SEC) {
311                         wdog |= MK48TXX_WDOG_RB_1_4;
312                         t -= WD_TO_250MS;
313                 } else if (t <= WD_TO_32SEC) {
314                         wdog |= MK48TXX_WDOG_RB_1;
315                         t -= WD_TO_1SEC;
316                 } else {
317                         wdog |= MK48TXX_WDOG_RB_4;
318                         t -= WD_TO_4SEC;
319                 }
320                 wdog |= (min(1 << t,
321                     MK48TXX_WDOG_BMB_MASK >> MK48TXX_WDOG_BMB_SHIFT)) <<
322                     MK48TXX_WDOG_BMB_SHIFT;
323                 if (sc->sc_flag & MK48TXX_WDOG_ENABLE_WDS)
324                         wdog |= MK48TXX_WDOG_WDS;
325                 *error = 0;
326         } else {
327                 wdog = 0;
328         }
329         mtx_lock(&sc->sc_mtx);
330         (*sc->sc_nvwr)(dev, sc->sc_clkoffset + MK48TXX_WDOG, wdog);
331         mtx_unlock(&sc->sc_mtx);
332 }