1*2f16049cSEmmanuel Vadot /*-
2*2f16049cSEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause
3*2f16049cSEmmanuel Vadot *
4*2f16049cSEmmanuel Vadot * Copyright (c) 2017 Ian Lepore <ian@freebsd.org>
5*2f16049cSEmmanuel Vadot *
6*2f16049cSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
7*2f16049cSEmmanuel Vadot * modification, are permitted provided that the following conditions
8*2f16049cSEmmanuel Vadot * are met:
9*2f16049cSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
10*2f16049cSEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
11*2f16049cSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
12*2f16049cSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
13*2f16049cSEmmanuel Vadot * documentation and/or other materials provided with the distribution.
14*2f16049cSEmmanuel Vadot *
15*2f16049cSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*2f16049cSEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*2f16049cSEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*2f16049cSEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*2f16049cSEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*2f16049cSEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*2f16049cSEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*2f16049cSEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*2f16049cSEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*2f16049cSEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*2f16049cSEmmanuel Vadot * SUCH DAMAGE.
26*2f16049cSEmmanuel Vadot */
27*2f16049cSEmmanuel Vadot
28*2f16049cSEmmanuel Vadot #include <sys/cdefs.h>
29*2f16049cSEmmanuel Vadot /*
30*2f16049cSEmmanuel Vadot * Driver for Dallas/Maxim DS13xx real-time clock/calendar chips:
31*2f16049cSEmmanuel Vadot *
32*2f16049cSEmmanuel Vadot * - DS1307 = Original/basic rtc + 56 bytes ram; 5v only.
33*2f16049cSEmmanuel Vadot * - DS1308 = Updated 1307, available in 1.8v-5v variations.
34*2f16049cSEmmanuel Vadot * - DS1337 = Like 1308, integrated xtal, 32khz output on at powerup.
35*2f16049cSEmmanuel Vadot * - DS1338 = Like 1308, integrated xtal.
36*2f16049cSEmmanuel Vadot * - DS1339 = Like 1337, integrated xtal, integrated trickle charger.
37*2f16049cSEmmanuel Vadot * - DS1340 = Like 1338, ST M41T00 compatible.
38*2f16049cSEmmanuel Vadot * - DS1341 = Like 1338, can slave-sync osc to external clock signal.
39*2f16049cSEmmanuel Vadot * - DS1342 = Like 1341 but requires different xtal.
40*2f16049cSEmmanuel Vadot * - DS1371 = 32-bit binary counter, watchdog timer.
41*2f16049cSEmmanuel Vadot * - DS1372 = 32-bit binary counter, 64-bit unique id in rom.
42*2f16049cSEmmanuel Vadot * - DS1374 = 32-bit binary counter, watchdog timer, trickle charger.
43*2f16049cSEmmanuel Vadot * - DS1375 = Like 1308 but only 16 bytes ram.
44*2f16049cSEmmanuel Vadot * - DS1388 = Rtc, watchdog timer, 512 bytes eeprom (not sram).
45*2f16049cSEmmanuel Vadot *
46*2f16049cSEmmanuel Vadot * This driver supports only basic timekeeping functions. It provides no access
47*2f16049cSEmmanuel Vadot * to or control over any other functionality provided by the chips.
48*2f16049cSEmmanuel Vadot */
49*2f16049cSEmmanuel Vadot
50*2f16049cSEmmanuel Vadot #include "opt_platform.h"
51*2f16049cSEmmanuel Vadot
52*2f16049cSEmmanuel Vadot #include <sys/param.h>
53*2f16049cSEmmanuel Vadot #include <sys/systm.h>
54*2f16049cSEmmanuel Vadot #include <sys/bus.h>
55*2f16049cSEmmanuel Vadot #include <sys/clock.h>
56*2f16049cSEmmanuel Vadot #include <sys/endian.h>
57*2f16049cSEmmanuel Vadot #include <sys/kernel.h>
58*2f16049cSEmmanuel Vadot #include <sys/libkern.h>
59*2f16049cSEmmanuel Vadot #include <sys/module.h>
60*2f16049cSEmmanuel Vadot
61*2f16049cSEmmanuel Vadot #include <dev/iicbus/iicbus.h>
62*2f16049cSEmmanuel Vadot #include <dev/iicbus/iiconf.h>
63*2f16049cSEmmanuel Vadot #ifdef FDT
64*2f16049cSEmmanuel Vadot #include <dev/ofw/openfirm.h>
65*2f16049cSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
66*2f16049cSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
67*2f16049cSEmmanuel Vadot #endif
68*2f16049cSEmmanuel Vadot
69*2f16049cSEmmanuel Vadot #include "clock_if.h"
70*2f16049cSEmmanuel Vadot #include "iicbus_if.h"
71*2f16049cSEmmanuel Vadot
72*2f16049cSEmmanuel Vadot /*
73*2f16049cSEmmanuel Vadot * I2C address 1101 000x
74*2f16049cSEmmanuel Vadot */
75*2f16049cSEmmanuel Vadot #define DS13xx_ADDR 0xd0
76*2f16049cSEmmanuel Vadot
77*2f16049cSEmmanuel Vadot /*
78*2f16049cSEmmanuel Vadot * Registers, bits within them, and masks for the various chip types.
79*2f16049cSEmmanuel Vadot */
80*2f16049cSEmmanuel Vadot
81*2f16049cSEmmanuel Vadot #define DS13xx_R_NONE 0xff /* Placeholder */
82*2f16049cSEmmanuel Vadot
83*2f16049cSEmmanuel Vadot #define DS130x_R_CONTROL 0x07
84*2f16049cSEmmanuel Vadot #define DS133x_R_CONTROL 0x0e
85*2f16049cSEmmanuel Vadot #define DS1340_R_CONTROL 0x07
86*2f16049cSEmmanuel Vadot #define DS1341_R_CONTROL 0x0e
87*2f16049cSEmmanuel Vadot #define DS1371_R_CONTROL 0x07
88*2f16049cSEmmanuel Vadot #define DS1372_R_CONTROL 0x07
89*2f16049cSEmmanuel Vadot #define DS1374_R_CONTROL 0x07
90*2f16049cSEmmanuel Vadot #define DS1375_R_CONTROL 0x0e
91*2f16049cSEmmanuel Vadot #define DS1388_R_CONTROL 0x0c
92*2f16049cSEmmanuel Vadot
93*2f16049cSEmmanuel Vadot #define DS13xx_R_SECOND 0x00
94*2f16049cSEmmanuel Vadot #define DS1388_R_SECOND 0x01
95*2f16049cSEmmanuel Vadot
96*2f16049cSEmmanuel Vadot #define DS130x_R_STATUS DS13xx_R_NONE
97*2f16049cSEmmanuel Vadot #define DS133x_R_STATUS 0x0f
98*2f16049cSEmmanuel Vadot #define DS1340_R_STATUS 0x09
99*2f16049cSEmmanuel Vadot #define DS137x_R_STATUS 0x08
100*2f16049cSEmmanuel Vadot #define DS1388_R_STATUS 0x0b
101*2f16049cSEmmanuel Vadot
102*2f16049cSEmmanuel Vadot #define DS13xx_B_STATUS_OSF 0x80 /* OSF is 1<<7 in status and sec regs */
103*2f16049cSEmmanuel Vadot #define DS13xx_B_HOUR_AMPM 0x40 /* AMPM mode is bit 1<<6 */
104*2f16049cSEmmanuel Vadot #define DS13xx_B_HOUR_PM 0x20 /* PM hours indicated by 1<<5 */
105*2f16049cSEmmanuel Vadot #define DS13xx_B_MONTH_CENTURY 0x80 /* 21st century indicated by 1<<7 */
106*2f16049cSEmmanuel Vadot
107*2f16049cSEmmanuel Vadot #define DS13xx_M_SECOND 0x7f /* Masks for all BCD time regs... */
108*2f16049cSEmmanuel Vadot #define DS13xx_M_MINUTE 0x7f
109*2f16049cSEmmanuel Vadot #define DS13xx_M_12HOUR 0x1f
110*2f16049cSEmmanuel Vadot #define DS13xx_M_24HOUR 0x3f
111*2f16049cSEmmanuel Vadot #define DS13xx_M_DAY 0x3f
112*2f16049cSEmmanuel Vadot #define DS13xx_M_MONTH 0x1f
113*2f16049cSEmmanuel Vadot #define DS13xx_M_YEAR 0xff
114*2f16049cSEmmanuel Vadot
115*2f16049cSEmmanuel Vadot /*
116*2f16049cSEmmanuel Vadot * The chip types we support.
117*2f16049cSEmmanuel Vadot */
118*2f16049cSEmmanuel Vadot enum {
119*2f16049cSEmmanuel Vadot TYPE_NONE,
120*2f16049cSEmmanuel Vadot TYPE_DS1307,
121*2f16049cSEmmanuel Vadot TYPE_DS1308,
122*2f16049cSEmmanuel Vadot TYPE_DS1337,
123*2f16049cSEmmanuel Vadot TYPE_DS1338,
124*2f16049cSEmmanuel Vadot TYPE_DS1339,
125*2f16049cSEmmanuel Vadot TYPE_DS1340,
126*2f16049cSEmmanuel Vadot TYPE_DS1341,
127*2f16049cSEmmanuel Vadot TYPE_DS1342,
128*2f16049cSEmmanuel Vadot TYPE_DS1371,
129*2f16049cSEmmanuel Vadot TYPE_DS1372,
130*2f16049cSEmmanuel Vadot TYPE_DS1374,
131*2f16049cSEmmanuel Vadot TYPE_DS1375,
132*2f16049cSEmmanuel Vadot TYPE_DS1388,
133*2f16049cSEmmanuel Vadot
134*2f16049cSEmmanuel Vadot TYPE_COUNT
135*2f16049cSEmmanuel Vadot };
136*2f16049cSEmmanuel Vadot static const char *desc_strings[] = {
137*2f16049cSEmmanuel Vadot "",
138*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1307 RTC",
139*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1308 RTC",
140*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1337 RTC",
141*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1338 RTC",
142*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1339 RTC",
143*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1340 RTC",
144*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1341 RTC",
145*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1342 RTC",
146*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1371 RTC",
147*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1372 RTC",
148*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1374 RTC",
149*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1375 RTC",
150*2f16049cSEmmanuel Vadot "Dallas/Maxim DS1388 RTC",
151*2f16049cSEmmanuel Vadot };
152*2f16049cSEmmanuel Vadot CTASSERT(nitems(desc_strings) == TYPE_COUNT);
153*2f16049cSEmmanuel Vadot
154*2f16049cSEmmanuel Vadot /*
155*2f16049cSEmmanuel Vadot * The time registers in the order they are laid out in hardware.
156*2f16049cSEmmanuel Vadot */
157*2f16049cSEmmanuel Vadot struct time_regs {
158*2f16049cSEmmanuel Vadot uint8_t sec, min, hour, wday, day, month, year;
159*2f16049cSEmmanuel Vadot };
160*2f16049cSEmmanuel Vadot
161*2f16049cSEmmanuel Vadot struct ds13rtc_softc {
162*2f16049cSEmmanuel Vadot device_t dev;
163*2f16049cSEmmanuel Vadot device_t busdev;
164*2f16049cSEmmanuel Vadot u_int chiptype; /* Type of DS13xx chip */
165*2f16049cSEmmanuel Vadot uint8_t secaddr; /* Address of seconds register */
166*2f16049cSEmmanuel Vadot uint8_t osfaddr; /* Address of register with OSF */
167*2f16049cSEmmanuel Vadot bool use_ampm; /* Use AM/PM mode. */
168*2f16049cSEmmanuel Vadot bool use_century; /* Use the Century bit. */
169*2f16049cSEmmanuel Vadot bool is_binary_counter; /* Chip has 32-bit binary counter. */
170*2f16049cSEmmanuel Vadot };
171*2f16049cSEmmanuel Vadot
172*2f16049cSEmmanuel Vadot /*
173*2f16049cSEmmanuel Vadot * We use the compat_data table to look up hint strings in the non-FDT case, so
174*2f16049cSEmmanuel Vadot * define the struct locally when we don't get it from ofw_bus_subr.h.
175*2f16049cSEmmanuel Vadot */
176*2f16049cSEmmanuel Vadot #ifdef FDT
177*2f16049cSEmmanuel Vadot typedef struct ofw_compat_data ds13_compat_data;
178*2f16049cSEmmanuel Vadot #else
179*2f16049cSEmmanuel Vadot typedef struct {
180*2f16049cSEmmanuel Vadot const char *ocd_str;
181*2f16049cSEmmanuel Vadot uintptr_t ocd_data;
182*2f16049cSEmmanuel Vadot } ds13_compat_data;
183*2f16049cSEmmanuel Vadot #endif
184*2f16049cSEmmanuel Vadot
185*2f16049cSEmmanuel Vadot static ds13_compat_data compat_data[] = {
186*2f16049cSEmmanuel Vadot {"dallas,ds1307", TYPE_DS1307},
187*2f16049cSEmmanuel Vadot {"dallas,ds1308", TYPE_DS1308},
188*2f16049cSEmmanuel Vadot {"dallas,ds1337", TYPE_DS1337},
189*2f16049cSEmmanuel Vadot {"dallas,ds1338", TYPE_DS1338},
190*2f16049cSEmmanuel Vadot {"dallas,ds1339", TYPE_DS1339},
191*2f16049cSEmmanuel Vadot {"dallas,ds1340", TYPE_DS1340},
192*2f16049cSEmmanuel Vadot {"dallas,ds1341", TYPE_DS1341},
193*2f16049cSEmmanuel Vadot {"dallas,ds1342", TYPE_DS1342},
194*2f16049cSEmmanuel Vadot {"dallas,ds1371", TYPE_DS1371},
195*2f16049cSEmmanuel Vadot {"dallas,ds1372", TYPE_DS1372},
196*2f16049cSEmmanuel Vadot {"dallas,ds1374", TYPE_DS1374},
197*2f16049cSEmmanuel Vadot {"dallas,ds1375", TYPE_DS1375},
198*2f16049cSEmmanuel Vadot {"dallas,ds1388", TYPE_DS1388},
199*2f16049cSEmmanuel Vadot
200*2f16049cSEmmanuel Vadot {NULL, TYPE_NONE},
201*2f16049cSEmmanuel Vadot };
202*2f16049cSEmmanuel Vadot
203*2f16049cSEmmanuel Vadot static int
read_reg(struct ds13rtc_softc * sc,uint8_t reg,uint8_t * val)204*2f16049cSEmmanuel Vadot read_reg(struct ds13rtc_softc *sc, uint8_t reg, uint8_t *val)
205*2f16049cSEmmanuel Vadot {
206*2f16049cSEmmanuel Vadot
207*2f16049cSEmmanuel Vadot return (iicdev_readfrom(sc->dev, reg, val, sizeof(*val), IIC_WAIT));
208*2f16049cSEmmanuel Vadot }
209*2f16049cSEmmanuel Vadot
210*2f16049cSEmmanuel Vadot static int
write_reg(struct ds13rtc_softc * sc,uint8_t reg,uint8_t val)211*2f16049cSEmmanuel Vadot write_reg(struct ds13rtc_softc *sc, uint8_t reg, uint8_t val)
212*2f16049cSEmmanuel Vadot {
213*2f16049cSEmmanuel Vadot
214*2f16049cSEmmanuel Vadot return (iicdev_writeto(sc->dev, reg, &val, sizeof(val), IIC_WAIT));
215*2f16049cSEmmanuel Vadot }
216*2f16049cSEmmanuel Vadot
217*2f16049cSEmmanuel Vadot static int
read_timeregs(struct ds13rtc_softc * sc,struct time_regs * tregs)218*2f16049cSEmmanuel Vadot read_timeregs(struct ds13rtc_softc *sc, struct time_regs *tregs)
219*2f16049cSEmmanuel Vadot {
220*2f16049cSEmmanuel Vadot int err;
221*2f16049cSEmmanuel Vadot
222*2f16049cSEmmanuel Vadot if ((err = iicdev_readfrom(sc->dev, sc->secaddr, tregs,
223*2f16049cSEmmanuel Vadot sizeof(*tregs), IIC_WAIT)) != 0)
224*2f16049cSEmmanuel Vadot return (err);
225*2f16049cSEmmanuel Vadot
226*2f16049cSEmmanuel Vadot return (err);
227*2f16049cSEmmanuel Vadot }
228*2f16049cSEmmanuel Vadot
229*2f16049cSEmmanuel Vadot static int
write_timeregs(struct ds13rtc_softc * sc,struct time_regs * tregs)230*2f16049cSEmmanuel Vadot write_timeregs(struct ds13rtc_softc *sc, struct time_regs *tregs)
231*2f16049cSEmmanuel Vadot {
232*2f16049cSEmmanuel Vadot
233*2f16049cSEmmanuel Vadot return (iicdev_writeto(sc->dev, sc->secaddr, tregs,
234*2f16049cSEmmanuel Vadot sizeof(*tregs), IIC_WAIT));
235*2f16049cSEmmanuel Vadot }
236*2f16049cSEmmanuel Vadot
237*2f16049cSEmmanuel Vadot static int
read_timeword(struct ds13rtc_softc * sc,time_t * secs)238*2f16049cSEmmanuel Vadot read_timeword(struct ds13rtc_softc *sc, time_t *secs)
239*2f16049cSEmmanuel Vadot {
240*2f16049cSEmmanuel Vadot int err;
241*2f16049cSEmmanuel Vadot uint8_t buf[4];
242*2f16049cSEmmanuel Vadot
243*2f16049cSEmmanuel Vadot if ((err = iicdev_readfrom(sc->dev, sc->secaddr, buf, sizeof(buf),
244*2f16049cSEmmanuel Vadot IIC_WAIT)) == 0)
245*2f16049cSEmmanuel Vadot *secs = le32dec(buf);
246*2f16049cSEmmanuel Vadot
247*2f16049cSEmmanuel Vadot return (err);
248*2f16049cSEmmanuel Vadot }
249*2f16049cSEmmanuel Vadot
250*2f16049cSEmmanuel Vadot static int
write_timeword(struct ds13rtc_softc * sc,time_t secs)251*2f16049cSEmmanuel Vadot write_timeword(struct ds13rtc_softc *sc, time_t secs)
252*2f16049cSEmmanuel Vadot {
253*2f16049cSEmmanuel Vadot uint8_t buf[4];
254*2f16049cSEmmanuel Vadot
255*2f16049cSEmmanuel Vadot le32enc(buf, (uint32_t)secs);
256*2f16049cSEmmanuel Vadot return (iicdev_writeto(sc->dev, sc->secaddr, buf, sizeof(buf),
257*2f16049cSEmmanuel Vadot IIC_WAIT));
258*2f16049cSEmmanuel Vadot }
259*2f16049cSEmmanuel Vadot
260*2f16049cSEmmanuel Vadot static void
ds13rtc_start(void * arg)261*2f16049cSEmmanuel Vadot ds13rtc_start(void *arg)
262*2f16049cSEmmanuel Vadot {
263*2f16049cSEmmanuel Vadot struct ds13rtc_softc *sc;
264*2f16049cSEmmanuel Vadot uint8_t ctlreg, statreg;
265*2f16049cSEmmanuel Vadot
266*2f16049cSEmmanuel Vadot sc = arg;
267*2f16049cSEmmanuel Vadot
268*2f16049cSEmmanuel Vadot /*
269*2f16049cSEmmanuel Vadot * Every chip in this family can be usefully initialized by writing 0 to
270*2f16049cSEmmanuel Vadot * the control register, except DS1375 which has an external oscillator
271*2f16049cSEmmanuel Vadot * controlled by values in the ctlreg that we know nothing about, so
272*2f16049cSEmmanuel Vadot * we'd best leave them alone. For all other chips, writing 0 enables
273*2f16049cSEmmanuel Vadot * the oscillator, disables signals/outputs in battery-backed mode
274*2f16049cSEmmanuel Vadot * (saves power) and disables features like watchdog timers and alarms.
275*2f16049cSEmmanuel Vadot */
276*2f16049cSEmmanuel Vadot switch (sc->chiptype) {
277*2f16049cSEmmanuel Vadot case TYPE_DS1307:
278*2f16049cSEmmanuel Vadot case TYPE_DS1308:
279*2f16049cSEmmanuel Vadot case TYPE_DS1338:
280*2f16049cSEmmanuel Vadot case TYPE_DS1340:
281*2f16049cSEmmanuel Vadot case TYPE_DS1371:
282*2f16049cSEmmanuel Vadot case TYPE_DS1372:
283*2f16049cSEmmanuel Vadot case TYPE_DS1374:
284*2f16049cSEmmanuel Vadot ctlreg = DS130x_R_CONTROL;
285*2f16049cSEmmanuel Vadot break;
286*2f16049cSEmmanuel Vadot case TYPE_DS1337:
287*2f16049cSEmmanuel Vadot case TYPE_DS1339:
288*2f16049cSEmmanuel Vadot ctlreg = DS133x_R_CONTROL;
289*2f16049cSEmmanuel Vadot break;
290*2f16049cSEmmanuel Vadot case TYPE_DS1341:
291*2f16049cSEmmanuel Vadot case TYPE_DS1342:
292*2f16049cSEmmanuel Vadot ctlreg = DS1341_R_CONTROL;
293*2f16049cSEmmanuel Vadot break;
294*2f16049cSEmmanuel Vadot case TYPE_DS1375:
295*2f16049cSEmmanuel Vadot ctlreg = DS13xx_R_NONE;
296*2f16049cSEmmanuel Vadot break;
297*2f16049cSEmmanuel Vadot case TYPE_DS1388:
298*2f16049cSEmmanuel Vadot ctlreg = DS1388_R_CONTROL;
299*2f16049cSEmmanuel Vadot break;
300*2f16049cSEmmanuel Vadot default:
301*2f16049cSEmmanuel Vadot device_printf(sc->dev, "missing init code for this chiptype\n");
302*2f16049cSEmmanuel Vadot return;
303*2f16049cSEmmanuel Vadot }
304*2f16049cSEmmanuel Vadot if (ctlreg != DS13xx_R_NONE)
305*2f16049cSEmmanuel Vadot write_reg(sc, ctlreg, 0);
306*2f16049cSEmmanuel Vadot
307*2f16049cSEmmanuel Vadot /*
308*2f16049cSEmmanuel Vadot * Common init. Read the OSF/CH status bit and report stopped clocks to
309*2f16049cSEmmanuel Vadot * the user. The status bit will be cleared the first time we write
310*2f16049cSEmmanuel Vadot * valid time to the chip (and must not be cleared before that).
311*2f16049cSEmmanuel Vadot */
312*2f16049cSEmmanuel Vadot if (read_reg(sc, sc->osfaddr, &statreg) != 0) {
313*2f16049cSEmmanuel Vadot device_printf(sc->dev, "cannot read RTC clock status bit\n");
314*2f16049cSEmmanuel Vadot return;
315*2f16049cSEmmanuel Vadot }
316*2f16049cSEmmanuel Vadot if (statreg & DS13xx_B_STATUS_OSF) {
317*2f16049cSEmmanuel Vadot device_printf(sc->dev,
318*2f16049cSEmmanuel Vadot "WARNING: RTC battery failed; time is invalid\n");
319*2f16049cSEmmanuel Vadot }
320*2f16049cSEmmanuel Vadot
321*2f16049cSEmmanuel Vadot /*
322*2f16049cSEmmanuel Vadot * Figure out whether the chip is configured for AM/PM mode. On all
323*2f16049cSEmmanuel Vadot * chips that do AM/PM mode, the flag bit is in the hours register,
324*2f16049cSEmmanuel Vadot * which is secaddr+2.
325*2f16049cSEmmanuel Vadot */
326*2f16049cSEmmanuel Vadot if ((sc->chiptype != TYPE_DS1340) && !sc->is_binary_counter) {
327*2f16049cSEmmanuel Vadot if (read_reg(sc, sc->secaddr + 2, &statreg) != 0) {
328*2f16049cSEmmanuel Vadot device_printf(sc->dev,
329*2f16049cSEmmanuel Vadot "cannot read RTC clock AM/PM bit\n");
330*2f16049cSEmmanuel Vadot return;
331*2f16049cSEmmanuel Vadot }
332*2f16049cSEmmanuel Vadot if (statreg & DS13xx_B_HOUR_AMPM)
333*2f16049cSEmmanuel Vadot sc->use_ampm = true;
334*2f16049cSEmmanuel Vadot }
335*2f16049cSEmmanuel Vadot
336*2f16049cSEmmanuel Vadot /*
337*2f16049cSEmmanuel Vadot * Everything looks good if we make it to here; register as an RTC.
338*2f16049cSEmmanuel Vadot * Schedule RTC updates to happen just after top-of-second.
339*2f16049cSEmmanuel Vadot */
340*2f16049cSEmmanuel Vadot clock_register_flags(sc->dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
341*2f16049cSEmmanuel Vadot clock_schedule(sc->dev, 1);
342*2f16049cSEmmanuel Vadot }
343*2f16049cSEmmanuel Vadot
344*2f16049cSEmmanuel Vadot static int
ds13rtc_gettime(device_t dev,struct timespec * ts)345*2f16049cSEmmanuel Vadot ds13rtc_gettime(device_t dev, struct timespec *ts)
346*2f16049cSEmmanuel Vadot {
347*2f16049cSEmmanuel Vadot struct bcd_clocktime bct;
348*2f16049cSEmmanuel Vadot struct time_regs tregs;
349*2f16049cSEmmanuel Vadot struct ds13rtc_softc *sc;
350*2f16049cSEmmanuel Vadot int err;
351*2f16049cSEmmanuel Vadot uint8_t statreg, hourmask;
352*2f16049cSEmmanuel Vadot
353*2f16049cSEmmanuel Vadot sc = device_get_softc(dev);
354*2f16049cSEmmanuel Vadot
355*2f16049cSEmmanuel Vadot /* Read the OSF/CH bit; if the clock stopped we can't provide time. */
356*2f16049cSEmmanuel Vadot if ((err = read_reg(sc, sc->osfaddr, &statreg)) != 0) {
357*2f16049cSEmmanuel Vadot return (err);
358*2f16049cSEmmanuel Vadot }
359*2f16049cSEmmanuel Vadot if (statreg & DS13xx_B_STATUS_OSF)
360*2f16049cSEmmanuel Vadot return (EINVAL); /* hardware is good, time is not. */
361*2f16049cSEmmanuel Vadot
362*2f16049cSEmmanuel Vadot /* If the chip counts time in binary, we just read and return it. */
363*2f16049cSEmmanuel Vadot if (sc->is_binary_counter) {
364*2f16049cSEmmanuel Vadot ts->tv_nsec = 0;
365*2f16049cSEmmanuel Vadot return (read_timeword(sc, &ts->tv_sec));
366*2f16049cSEmmanuel Vadot }
367*2f16049cSEmmanuel Vadot
368*2f16049cSEmmanuel Vadot /*
369*2f16049cSEmmanuel Vadot * Chip counts in BCD, read and decode it...
370*2f16049cSEmmanuel Vadot */
371*2f16049cSEmmanuel Vadot if ((err = read_timeregs(sc, &tregs)) != 0) {
372*2f16049cSEmmanuel Vadot device_printf(dev, "cannot read RTC time\n");
373*2f16049cSEmmanuel Vadot return (err);
374*2f16049cSEmmanuel Vadot }
375*2f16049cSEmmanuel Vadot
376*2f16049cSEmmanuel Vadot if (sc->use_ampm)
377*2f16049cSEmmanuel Vadot hourmask = DS13xx_M_12HOUR;
378*2f16049cSEmmanuel Vadot else
379*2f16049cSEmmanuel Vadot hourmask = DS13xx_M_24HOUR;
380*2f16049cSEmmanuel Vadot
381*2f16049cSEmmanuel Vadot bct.nsec = 0;
382*2f16049cSEmmanuel Vadot bct.ispm = tregs.hour & DS13xx_B_HOUR_PM;
383*2f16049cSEmmanuel Vadot bct.sec = tregs.sec & DS13xx_M_SECOND;
384*2f16049cSEmmanuel Vadot bct.min = tregs.min & DS13xx_M_MINUTE;
385*2f16049cSEmmanuel Vadot bct.hour = tregs.hour & hourmask;
386*2f16049cSEmmanuel Vadot bct.day = tregs.day & DS13xx_M_DAY;
387*2f16049cSEmmanuel Vadot bct.mon = tregs.month & DS13xx_M_MONTH;
388*2f16049cSEmmanuel Vadot bct.year = tregs.year & DS13xx_M_YEAR;
389*2f16049cSEmmanuel Vadot
390*2f16049cSEmmanuel Vadot /*
391*2f16049cSEmmanuel Vadot * If this chip has a century bit, honor it. Otherwise let
392*2f16049cSEmmanuel Vadot * clock_ct_to_ts() infer the century from the 2-digit year.
393*2f16049cSEmmanuel Vadot */
394*2f16049cSEmmanuel Vadot if (sc->use_century)
395*2f16049cSEmmanuel Vadot bct.year += (tregs.month & DS13xx_B_MONTH_CENTURY) ? 0x100 : 0;
396*2f16049cSEmmanuel Vadot
397*2f16049cSEmmanuel Vadot clock_dbgprint_bcd(sc->dev, CLOCK_DBG_READ, &bct);
398*2f16049cSEmmanuel Vadot err = clock_bcd_to_ts(&bct, ts, sc->use_ampm);
399*2f16049cSEmmanuel Vadot
400*2f16049cSEmmanuel Vadot return (err);
401*2f16049cSEmmanuel Vadot }
402*2f16049cSEmmanuel Vadot
403*2f16049cSEmmanuel Vadot static int
ds13rtc_settime(device_t dev,struct timespec * ts)404*2f16049cSEmmanuel Vadot ds13rtc_settime(device_t dev, struct timespec *ts)
405*2f16049cSEmmanuel Vadot {
406*2f16049cSEmmanuel Vadot struct bcd_clocktime bct;
407*2f16049cSEmmanuel Vadot struct time_regs tregs;
408*2f16049cSEmmanuel Vadot struct ds13rtc_softc *sc;
409*2f16049cSEmmanuel Vadot int err;
410*2f16049cSEmmanuel Vadot uint8_t cflag, statreg, pmflags;
411*2f16049cSEmmanuel Vadot
412*2f16049cSEmmanuel Vadot sc = device_get_softc(dev);
413*2f16049cSEmmanuel Vadot
414*2f16049cSEmmanuel Vadot /*
415*2f16049cSEmmanuel Vadot * We request a timespec with no resolution-adjustment. That also
416*2f16049cSEmmanuel Vadot * disables utc adjustment, so apply that ourselves.
417*2f16049cSEmmanuel Vadot */
418*2f16049cSEmmanuel Vadot ts->tv_sec -= utc_offset();
419*2f16049cSEmmanuel Vadot
420*2f16049cSEmmanuel Vadot /* If the chip counts time in binary, store tv_sec and we're done. */
421*2f16049cSEmmanuel Vadot if (sc->is_binary_counter)
422*2f16049cSEmmanuel Vadot return (write_timeword(sc, ts->tv_sec));
423*2f16049cSEmmanuel Vadot
424*2f16049cSEmmanuel Vadot clock_ts_to_bcd(ts, &bct, sc->use_ampm);
425*2f16049cSEmmanuel Vadot clock_dbgprint_bcd(sc->dev, CLOCK_DBG_WRITE, &bct);
426*2f16049cSEmmanuel Vadot
427*2f16049cSEmmanuel Vadot /* If the chip is in AMPM mode deal with the PM flag. */
428*2f16049cSEmmanuel Vadot pmflags = 0;
429*2f16049cSEmmanuel Vadot if (sc->use_ampm) {
430*2f16049cSEmmanuel Vadot pmflags = DS13xx_B_HOUR_AMPM;
431*2f16049cSEmmanuel Vadot if (bct.ispm)
432*2f16049cSEmmanuel Vadot pmflags |= DS13xx_B_HOUR_PM;
433*2f16049cSEmmanuel Vadot }
434*2f16049cSEmmanuel Vadot
435*2f16049cSEmmanuel Vadot /* If the chip has a century bit, set it as needed. */
436*2f16049cSEmmanuel Vadot cflag = 0;
437*2f16049cSEmmanuel Vadot if (sc->use_century) {
438*2f16049cSEmmanuel Vadot if (bct.year >= 2000)
439*2f16049cSEmmanuel Vadot cflag |= DS13xx_B_MONTH_CENTURY;
440*2f16049cSEmmanuel Vadot }
441*2f16049cSEmmanuel Vadot
442*2f16049cSEmmanuel Vadot tregs.sec = bct.sec;
443*2f16049cSEmmanuel Vadot tregs.min = bct.min;
444*2f16049cSEmmanuel Vadot tregs.hour = bct.hour | pmflags;
445*2f16049cSEmmanuel Vadot tregs.day = bct.day;
446*2f16049cSEmmanuel Vadot tregs.month = bct.mon | cflag;
447*2f16049cSEmmanuel Vadot tregs.year = bct.year & 0xff;
448*2f16049cSEmmanuel Vadot tregs.wday = bct.dow;
449*2f16049cSEmmanuel Vadot
450*2f16049cSEmmanuel Vadot /*
451*2f16049cSEmmanuel Vadot * Set the time. Reset the OSF bit if it is on and it is not part of
452*2f16049cSEmmanuel Vadot * the time registers (in which case writing time resets it).
453*2f16049cSEmmanuel Vadot */
454*2f16049cSEmmanuel Vadot if ((err = write_timeregs(sc, &tregs)) != 0)
455*2f16049cSEmmanuel Vadot goto errout;
456*2f16049cSEmmanuel Vadot if (sc->osfaddr != sc->secaddr) {
457*2f16049cSEmmanuel Vadot if ((err = read_reg(sc, sc->osfaddr, &statreg)) != 0)
458*2f16049cSEmmanuel Vadot goto errout;
459*2f16049cSEmmanuel Vadot if (statreg & DS13xx_B_STATUS_OSF) {
460*2f16049cSEmmanuel Vadot statreg &= ~DS13xx_B_STATUS_OSF;
461*2f16049cSEmmanuel Vadot err = write_reg(sc, sc->osfaddr, statreg);
462*2f16049cSEmmanuel Vadot }
463*2f16049cSEmmanuel Vadot }
464*2f16049cSEmmanuel Vadot
465*2f16049cSEmmanuel Vadot errout:
466*2f16049cSEmmanuel Vadot
467*2f16049cSEmmanuel Vadot if (err != 0)
468*2f16049cSEmmanuel Vadot device_printf(dev, "cannot update RTC time\n");
469*2f16049cSEmmanuel Vadot
470*2f16049cSEmmanuel Vadot return (err);
471*2f16049cSEmmanuel Vadot }
472*2f16049cSEmmanuel Vadot
473*2f16049cSEmmanuel Vadot static int
ds13rtc_get_chiptype(device_t dev)474*2f16049cSEmmanuel Vadot ds13rtc_get_chiptype(device_t dev)
475*2f16049cSEmmanuel Vadot {
476*2f16049cSEmmanuel Vadot #ifdef FDT
477*2f16049cSEmmanuel Vadot
478*2f16049cSEmmanuel Vadot return (ofw_bus_search_compatible(dev, compat_data)->ocd_data);
479*2f16049cSEmmanuel Vadot #else
480*2f16049cSEmmanuel Vadot ds13_compat_data *cdata;
481*2f16049cSEmmanuel Vadot const char *htype;
482*2f16049cSEmmanuel Vadot
483*2f16049cSEmmanuel Vadot /*
484*2f16049cSEmmanuel Vadot * We can only attach if provided a chiptype hint string.
485*2f16049cSEmmanuel Vadot */
486*2f16049cSEmmanuel Vadot if (resource_string_value(device_get_name(dev),
487*2f16049cSEmmanuel Vadot device_get_unit(dev), "compatible", &htype) != 0)
488*2f16049cSEmmanuel Vadot return (TYPE_NONE);
489*2f16049cSEmmanuel Vadot
490*2f16049cSEmmanuel Vadot /*
491*2f16049cSEmmanuel Vadot * Loop through the ofw compat data comparing the hinted chip type to
492*2f16049cSEmmanuel Vadot * the compat strings.
493*2f16049cSEmmanuel Vadot */
494*2f16049cSEmmanuel Vadot for (cdata = compat_data; cdata->ocd_str != NULL; ++cdata) {
495*2f16049cSEmmanuel Vadot if (strcmp(htype, cdata->ocd_str) == 0)
496*2f16049cSEmmanuel Vadot break;
497*2f16049cSEmmanuel Vadot }
498*2f16049cSEmmanuel Vadot return (cdata->ocd_data);
499*2f16049cSEmmanuel Vadot #endif
500*2f16049cSEmmanuel Vadot }
501*2f16049cSEmmanuel Vadot
502*2f16049cSEmmanuel Vadot static int
ds13rtc_probe(device_t dev)503*2f16049cSEmmanuel Vadot ds13rtc_probe(device_t dev)
504*2f16049cSEmmanuel Vadot {
505*2f16049cSEmmanuel Vadot int chiptype, goodrv;
506*2f16049cSEmmanuel Vadot
507*2f16049cSEmmanuel Vadot #ifdef FDT
508*2f16049cSEmmanuel Vadot if (!ofw_bus_status_okay(dev))
509*2f16049cSEmmanuel Vadot return (ENXIO);
510*2f16049cSEmmanuel Vadot goodrv = BUS_PROBE_GENERIC;
511*2f16049cSEmmanuel Vadot #else
512*2f16049cSEmmanuel Vadot goodrv = BUS_PROBE_NOWILDCARD;
513*2f16049cSEmmanuel Vadot #endif
514*2f16049cSEmmanuel Vadot
515*2f16049cSEmmanuel Vadot chiptype = ds13rtc_get_chiptype(dev);
516*2f16049cSEmmanuel Vadot if (chiptype == TYPE_NONE)
517*2f16049cSEmmanuel Vadot return (ENXIO);
518*2f16049cSEmmanuel Vadot
519*2f16049cSEmmanuel Vadot device_set_desc(dev, desc_strings[chiptype]);
520*2f16049cSEmmanuel Vadot return (goodrv);
521*2f16049cSEmmanuel Vadot }
522*2f16049cSEmmanuel Vadot
523*2f16049cSEmmanuel Vadot static int
ds13rtc_attach(device_t dev)524*2f16049cSEmmanuel Vadot ds13rtc_attach(device_t dev)
525*2f16049cSEmmanuel Vadot {
526*2f16049cSEmmanuel Vadot struct ds13rtc_softc *sc;
527*2f16049cSEmmanuel Vadot
528*2f16049cSEmmanuel Vadot sc = device_get_softc(dev);
529*2f16049cSEmmanuel Vadot sc->dev = dev;
530*2f16049cSEmmanuel Vadot sc->busdev = device_get_parent(dev);
531*2f16049cSEmmanuel Vadot
532*2f16049cSEmmanuel Vadot /*
533*2f16049cSEmmanuel Vadot * We need to know what kind of chip we're driving.
534*2f16049cSEmmanuel Vadot */
535*2f16049cSEmmanuel Vadot if ((sc->chiptype = ds13rtc_get_chiptype(dev)) == TYPE_NONE) {
536*2f16049cSEmmanuel Vadot device_printf(dev, "impossible: cannot determine chip type\n");
537*2f16049cSEmmanuel Vadot return (ENXIO);
538*2f16049cSEmmanuel Vadot }
539*2f16049cSEmmanuel Vadot
540*2f16049cSEmmanuel Vadot /* The seconds register is in the same place on all except DS1388. */
541*2f16049cSEmmanuel Vadot if (sc->chiptype == TYPE_DS1388)
542*2f16049cSEmmanuel Vadot sc->secaddr = DS1388_R_SECOND;
543*2f16049cSEmmanuel Vadot else
544*2f16049cSEmmanuel Vadot sc->secaddr = DS13xx_R_SECOND;
545*2f16049cSEmmanuel Vadot
546*2f16049cSEmmanuel Vadot /*
547*2f16049cSEmmanuel Vadot * The OSF/CH (osc failed/clock-halted) bit appears in different
548*2f16049cSEmmanuel Vadot * registers for different chip types. The DS1375 has no OSF indicator
549*2f16049cSEmmanuel Vadot * because it has no internal oscillator; we just point to an always-
550*2f16049cSEmmanuel Vadot * zero bit in the status register for that chip.
551*2f16049cSEmmanuel Vadot */
552*2f16049cSEmmanuel Vadot switch (sc->chiptype) {
553*2f16049cSEmmanuel Vadot case TYPE_DS1307:
554*2f16049cSEmmanuel Vadot case TYPE_DS1308:
555*2f16049cSEmmanuel Vadot case TYPE_DS1338:
556*2f16049cSEmmanuel Vadot sc->osfaddr = DS13xx_R_SECOND;
557*2f16049cSEmmanuel Vadot break;
558*2f16049cSEmmanuel Vadot case TYPE_DS1337:
559*2f16049cSEmmanuel Vadot case TYPE_DS1339:
560*2f16049cSEmmanuel Vadot case TYPE_DS1341:
561*2f16049cSEmmanuel Vadot case TYPE_DS1342:
562*2f16049cSEmmanuel Vadot case TYPE_DS1375:
563*2f16049cSEmmanuel Vadot sc->osfaddr = DS133x_R_STATUS;
564*2f16049cSEmmanuel Vadot sc->use_century = true;
565*2f16049cSEmmanuel Vadot break;
566*2f16049cSEmmanuel Vadot case TYPE_DS1340:
567*2f16049cSEmmanuel Vadot sc->osfaddr = DS1340_R_STATUS;
568*2f16049cSEmmanuel Vadot break;
569*2f16049cSEmmanuel Vadot case TYPE_DS1371:
570*2f16049cSEmmanuel Vadot case TYPE_DS1372:
571*2f16049cSEmmanuel Vadot case TYPE_DS1374:
572*2f16049cSEmmanuel Vadot sc->osfaddr = DS137x_R_STATUS;
573*2f16049cSEmmanuel Vadot sc->is_binary_counter = true;
574*2f16049cSEmmanuel Vadot break;
575*2f16049cSEmmanuel Vadot case TYPE_DS1388:
576*2f16049cSEmmanuel Vadot sc->osfaddr = DS1388_R_STATUS;
577*2f16049cSEmmanuel Vadot break;
578*2f16049cSEmmanuel Vadot }
579*2f16049cSEmmanuel Vadot
580*2f16049cSEmmanuel Vadot /*
581*2f16049cSEmmanuel Vadot * We have to wait until interrupts are enabled. Sometimes I2C read
582*2f16049cSEmmanuel Vadot * and write only works when the interrupts are available.
583*2f16049cSEmmanuel Vadot */
584*2f16049cSEmmanuel Vadot config_intrhook_oneshot(ds13rtc_start, sc);
585*2f16049cSEmmanuel Vadot
586*2f16049cSEmmanuel Vadot return (0);
587*2f16049cSEmmanuel Vadot }
588*2f16049cSEmmanuel Vadot
589*2f16049cSEmmanuel Vadot static int
ds13rtc_detach(device_t dev)590*2f16049cSEmmanuel Vadot ds13rtc_detach(device_t dev)
591*2f16049cSEmmanuel Vadot {
592*2f16049cSEmmanuel Vadot
593*2f16049cSEmmanuel Vadot clock_unregister(dev);
594*2f16049cSEmmanuel Vadot return (0);
595*2f16049cSEmmanuel Vadot }
596*2f16049cSEmmanuel Vadot
597*2f16049cSEmmanuel Vadot static device_method_t ds13rtc_methods[] = {
598*2f16049cSEmmanuel Vadot DEVMETHOD(device_probe, ds13rtc_probe),
599*2f16049cSEmmanuel Vadot DEVMETHOD(device_attach, ds13rtc_attach),
600*2f16049cSEmmanuel Vadot DEVMETHOD(device_detach, ds13rtc_detach),
601*2f16049cSEmmanuel Vadot
602*2f16049cSEmmanuel Vadot DEVMETHOD(clock_gettime, ds13rtc_gettime),
603*2f16049cSEmmanuel Vadot DEVMETHOD(clock_settime, ds13rtc_settime),
604*2f16049cSEmmanuel Vadot
605*2f16049cSEmmanuel Vadot DEVMETHOD_END
606*2f16049cSEmmanuel Vadot };
607*2f16049cSEmmanuel Vadot
608*2f16049cSEmmanuel Vadot static driver_t ds13rtc_driver = {
609*2f16049cSEmmanuel Vadot "ds13rtc",
610*2f16049cSEmmanuel Vadot ds13rtc_methods,
611*2f16049cSEmmanuel Vadot sizeof(struct ds13rtc_softc),
612*2f16049cSEmmanuel Vadot };
613*2f16049cSEmmanuel Vadot
614*2f16049cSEmmanuel Vadot DRIVER_MODULE(ds13rtc, iicbus, ds13rtc_driver, NULL, NULL);
615*2f16049cSEmmanuel Vadot MODULE_VERSION(ds13rtc, 1);
616*2f16049cSEmmanuel Vadot MODULE_DEPEND(ds13rtc, iicbus, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
617*2f16049cSEmmanuel Vadot IICBUS_FDT_PNP_INFO(compat_data);
618