xref: /freebsd/sys/dev/iicbus/rtc/ds1307.c (revision 2f16049c985a364e2bd2b256f5bef9af17e10c62)
1*2f16049cSEmmanuel Vadot /*-
2*2f16049cSEmmanuel Vadot  * Copyright (c) 2015 Luiz Otavio O Souza <loos@FreeBSD.org>
3*2f16049cSEmmanuel Vadot  * Copyright (c) 2022 Mathew McBride <matt@traverse.com.au>
4*2f16049cSEmmanuel Vadot  * All rights reserved.
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 Maxim DS1307 I2C real-time clock/calendar.
31*2f16049cSEmmanuel Vadot  */
32*2f16049cSEmmanuel Vadot 
33*2f16049cSEmmanuel Vadot #include "opt_platform.h"
34*2f16049cSEmmanuel Vadot 
35*2f16049cSEmmanuel Vadot #include <sys/param.h>
36*2f16049cSEmmanuel Vadot #include <sys/systm.h>
37*2f16049cSEmmanuel Vadot #include <sys/bus.h>
38*2f16049cSEmmanuel Vadot #include <sys/clock.h>
39*2f16049cSEmmanuel Vadot #include <sys/kernel.h>
40*2f16049cSEmmanuel Vadot #include <sys/module.h>
41*2f16049cSEmmanuel Vadot #include <sys/sysctl.h>
42*2f16049cSEmmanuel Vadot 
43*2f16049cSEmmanuel Vadot #include <dev/iicbus/iicbus.h>
44*2f16049cSEmmanuel Vadot #include <dev/iicbus/iiconf.h>
45*2f16049cSEmmanuel Vadot #ifdef FDT
46*2f16049cSEmmanuel Vadot #include <dev/ofw/openfirm.h>
47*2f16049cSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
48*2f16049cSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
49*2f16049cSEmmanuel Vadot #endif
50*2f16049cSEmmanuel Vadot 
51*2f16049cSEmmanuel Vadot #include <dev/iicbus/rtc/ds1307reg.h>
52*2f16049cSEmmanuel Vadot 
53*2f16049cSEmmanuel Vadot #include "clock_if.h"
54*2f16049cSEmmanuel Vadot #include "iicbus_if.h"
55*2f16049cSEmmanuel Vadot 
56*2f16049cSEmmanuel Vadot enum {
57*2f16049cSEmmanuel Vadot 	TYPE_DS1307,
58*2f16049cSEmmanuel Vadot 	TYPE_MAXIM1307,
59*2f16049cSEmmanuel Vadot 	TYPE_MICROCHIP_MCP7491X,
60*2f16049cSEmmanuel Vadot 	TYPE_EPSON_RX8035,
61*2f16049cSEmmanuel Vadot 	TYPE_COUNT
62*2f16049cSEmmanuel Vadot };
63*2f16049cSEmmanuel Vadot 
64*2f16049cSEmmanuel Vadot struct ds1307_softc {
65*2f16049cSEmmanuel Vadot 	device_t			sc_dev;
66*2f16049cSEmmanuel Vadot 	struct intr_config_hook		enum_hook;
67*2f16049cSEmmanuel Vadot 	uint32_t			chiptype;
68*2f16049cSEmmanuel Vadot 	uint8_t				sc_ctrl;
69*2f16049cSEmmanuel Vadot 	bool				sc_use_ampm;
70*2f16049cSEmmanuel Vadot };
71*2f16049cSEmmanuel Vadot 
72*2f16049cSEmmanuel Vadot static void ds1307_start(void *);
73*2f16049cSEmmanuel Vadot 
74*2f16049cSEmmanuel Vadot #ifdef FDT
75*2f16049cSEmmanuel Vadot static const struct ofw_compat_data ds1307_compat_data[] = {
76*2f16049cSEmmanuel Vadot 	{"dallas,ds1307",		TYPE_DS1307},
77*2f16049cSEmmanuel Vadot 	{"maxim,ds1307",		TYPE_MAXIM1307},
78*2f16049cSEmmanuel Vadot 	{"microchip,mcp7941x",		TYPE_MICROCHIP_MCP7491X},
79*2f16049cSEmmanuel Vadot 	{"epson,rx8035",		TYPE_EPSON_RX8035},
80*2f16049cSEmmanuel Vadot 	{ NULL, 0 }
81*2f16049cSEmmanuel Vadot };
82*2f16049cSEmmanuel Vadot #endif
83*2f16049cSEmmanuel Vadot 
84*2f16049cSEmmanuel Vadot static int
85*2f16049cSEmmanuel Vadot ds1307_read1(device_t dev, uint8_t reg, uint8_t *data)
86*2f16049cSEmmanuel Vadot {
87*2f16049cSEmmanuel Vadot 
88*2f16049cSEmmanuel Vadot 	return (iicdev_readfrom(dev, reg, data, 1, IIC_INTRWAIT));
89*2f16049cSEmmanuel Vadot }
90*2f16049cSEmmanuel Vadot 
91*2f16049cSEmmanuel Vadot static int
92*2f16049cSEmmanuel Vadot ds1307_write1(device_t dev, uint8_t reg, uint8_t data)
93*2f16049cSEmmanuel Vadot {
94*2f16049cSEmmanuel Vadot 
95*2f16049cSEmmanuel Vadot 	return (iicdev_writeto(dev, reg, &data, 1, IIC_INTRWAIT));
96*2f16049cSEmmanuel Vadot }
97*2f16049cSEmmanuel Vadot 
98*2f16049cSEmmanuel Vadot static int
99*2f16049cSEmmanuel Vadot ds1307_ctrl_read(struct ds1307_softc *sc)
100*2f16049cSEmmanuel Vadot {
101*2f16049cSEmmanuel Vadot 	int error;
102*2f16049cSEmmanuel Vadot 
103*2f16049cSEmmanuel Vadot 	sc->sc_ctrl = 0;
104*2f16049cSEmmanuel Vadot 	error = ds1307_read1(sc->sc_dev, DS1307_CONTROL, &sc->sc_ctrl);
105*2f16049cSEmmanuel Vadot 	if (error) {
106*2f16049cSEmmanuel Vadot 		device_printf(sc->sc_dev, "%s: cannot read from RTC: %d\n",
107*2f16049cSEmmanuel Vadot 		    __func__, error);
108*2f16049cSEmmanuel Vadot 		return (error);
109*2f16049cSEmmanuel Vadot 	}
110*2f16049cSEmmanuel Vadot 
111*2f16049cSEmmanuel Vadot 	return (0);
112*2f16049cSEmmanuel Vadot }
113*2f16049cSEmmanuel Vadot 
114*2f16049cSEmmanuel Vadot static int
115*2f16049cSEmmanuel Vadot ds1307_ctrl_write(struct ds1307_softc *sc)
116*2f16049cSEmmanuel Vadot {
117*2f16049cSEmmanuel Vadot 	int error;
118*2f16049cSEmmanuel Vadot 	uint8_t ctrl;
119*2f16049cSEmmanuel Vadot 
120*2f16049cSEmmanuel Vadot 	ctrl = sc->sc_ctrl & DS1307_CTRL_MASK;
121*2f16049cSEmmanuel Vadot 	error = ds1307_write1(sc->sc_dev, DS1307_CONTROL, ctrl);
122*2f16049cSEmmanuel Vadot 	if (error != 0)
123*2f16049cSEmmanuel Vadot 		device_printf(sc->sc_dev, "%s: cannot write to RTC: %d\n",
124*2f16049cSEmmanuel Vadot 		    __func__, error);
125*2f16049cSEmmanuel Vadot 
126*2f16049cSEmmanuel Vadot 	return (error);
127*2f16049cSEmmanuel Vadot }
128*2f16049cSEmmanuel Vadot 
129*2f16049cSEmmanuel Vadot static int
130*2f16049cSEmmanuel Vadot ds1307_sqwe_sysctl(SYSCTL_HANDLER_ARGS)
131*2f16049cSEmmanuel Vadot {
132*2f16049cSEmmanuel Vadot 	int sqwe, error, newv, sqwe_bit;
133*2f16049cSEmmanuel Vadot 	struct ds1307_softc *sc;
134*2f16049cSEmmanuel Vadot 
135*2f16049cSEmmanuel Vadot 	sc = (struct ds1307_softc *)arg1;
136*2f16049cSEmmanuel Vadot 	error = ds1307_ctrl_read(sc);
137*2f16049cSEmmanuel Vadot 	if (error != 0)
138*2f16049cSEmmanuel Vadot 		return (error);
139*2f16049cSEmmanuel Vadot 	if (sc->chiptype == TYPE_MICROCHIP_MCP7491X)
140*2f16049cSEmmanuel Vadot 		sqwe_bit = MCP7941X_CTRL_SQWE;
141*2f16049cSEmmanuel Vadot 	else
142*2f16049cSEmmanuel Vadot 		sqwe_bit = DS1307_CTRL_SQWE;
143*2f16049cSEmmanuel Vadot 	sqwe = newv = (sc->sc_ctrl & sqwe_bit) ? 1 : 0;
144*2f16049cSEmmanuel Vadot 	error = sysctl_handle_int(oidp, &newv, 0, req);
145*2f16049cSEmmanuel Vadot 	if (error != 0 || req->newptr == NULL)
146*2f16049cSEmmanuel Vadot 		return (error);
147*2f16049cSEmmanuel Vadot 	if (sqwe != newv) {
148*2f16049cSEmmanuel Vadot 		sc->sc_ctrl &= ~sqwe_bit;
149*2f16049cSEmmanuel Vadot 		if (newv)
150*2f16049cSEmmanuel Vadot 			sc->sc_ctrl |= sqwe_bit;
151*2f16049cSEmmanuel Vadot 		error = ds1307_ctrl_write(sc);
152*2f16049cSEmmanuel Vadot 		if (error != 0)
153*2f16049cSEmmanuel Vadot 			return (error);
154*2f16049cSEmmanuel Vadot 	}
155*2f16049cSEmmanuel Vadot 
156*2f16049cSEmmanuel Vadot 	return (error);
157*2f16049cSEmmanuel Vadot }
158*2f16049cSEmmanuel Vadot 
159*2f16049cSEmmanuel Vadot static int
160*2f16049cSEmmanuel Vadot ds1307_sqw_freq_sysctl(SYSCTL_HANDLER_ARGS)
161*2f16049cSEmmanuel Vadot {
162*2f16049cSEmmanuel Vadot 	int ds1307_sqw_freq[] = { 1, 4096, 8192, 32768 };
163*2f16049cSEmmanuel Vadot 	int error, freq, i, newf, tmp;
164*2f16049cSEmmanuel Vadot 	struct ds1307_softc *sc;
165*2f16049cSEmmanuel Vadot 
166*2f16049cSEmmanuel Vadot 	sc = (struct ds1307_softc *)arg1;
167*2f16049cSEmmanuel Vadot 	error = ds1307_ctrl_read(sc);
168*2f16049cSEmmanuel Vadot 	if (error != 0)
169*2f16049cSEmmanuel Vadot 		return (error);
170*2f16049cSEmmanuel Vadot 	tmp = (sc->sc_ctrl & DS1307_CTRL_RS_MASK);
171*2f16049cSEmmanuel Vadot 	if (tmp >= nitems(ds1307_sqw_freq))
172*2f16049cSEmmanuel Vadot 		tmp = nitems(ds1307_sqw_freq) - 1;
173*2f16049cSEmmanuel Vadot 	freq = ds1307_sqw_freq[tmp];
174*2f16049cSEmmanuel Vadot 	error = sysctl_handle_int(oidp, &freq, 0, req);
175*2f16049cSEmmanuel Vadot 	if (error != 0 || req->newptr == NULL)
176*2f16049cSEmmanuel Vadot 		return (error);
177*2f16049cSEmmanuel Vadot 	if (freq != ds1307_sqw_freq[tmp]) {
178*2f16049cSEmmanuel Vadot 		newf = 0;
179*2f16049cSEmmanuel Vadot 		for (i = 0; i < nitems(ds1307_sqw_freq); i++)
180*2f16049cSEmmanuel Vadot 			if (freq >= ds1307_sqw_freq[i])
181*2f16049cSEmmanuel Vadot 				newf = i;
182*2f16049cSEmmanuel Vadot 		sc->sc_ctrl &= ~DS1307_CTRL_RS_MASK;
183*2f16049cSEmmanuel Vadot 		sc->sc_ctrl |= newf;
184*2f16049cSEmmanuel Vadot 		error = ds1307_ctrl_write(sc);
185*2f16049cSEmmanuel Vadot 		if (error != 0)
186*2f16049cSEmmanuel Vadot 			return (error);
187*2f16049cSEmmanuel Vadot 	}
188*2f16049cSEmmanuel Vadot 
189*2f16049cSEmmanuel Vadot 	return (error);
190*2f16049cSEmmanuel Vadot }
191*2f16049cSEmmanuel Vadot 
192*2f16049cSEmmanuel Vadot static int
193*2f16049cSEmmanuel Vadot ds1307_sqw_out_sysctl(SYSCTL_HANDLER_ARGS)
194*2f16049cSEmmanuel Vadot {
195*2f16049cSEmmanuel Vadot 	int sqwe, error, newv;
196*2f16049cSEmmanuel Vadot 	struct ds1307_softc *sc;
197*2f16049cSEmmanuel Vadot 
198*2f16049cSEmmanuel Vadot 	sc = (struct ds1307_softc *)arg1;
199*2f16049cSEmmanuel Vadot 	error = ds1307_ctrl_read(sc);
200*2f16049cSEmmanuel Vadot 	if (error != 0)
201*2f16049cSEmmanuel Vadot 		return (error);
202*2f16049cSEmmanuel Vadot 	sqwe = newv = (sc->sc_ctrl & DS1307_CTRL_OUT) ? 1 : 0;
203*2f16049cSEmmanuel Vadot 	error = sysctl_handle_int(oidp, &newv, 0, req);
204*2f16049cSEmmanuel Vadot 	if (error != 0 || req->newptr == NULL)
205*2f16049cSEmmanuel Vadot 		return (error);
206*2f16049cSEmmanuel Vadot 	if (sqwe != newv) {
207*2f16049cSEmmanuel Vadot 		sc->sc_ctrl &= ~DS1307_CTRL_OUT;
208*2f16049cSEmmanuel Vadot 		if (newv)
209*2f16049cSEmmanuel Vadot 			sc->sc_ctrl |= DS1307_CTRL_OUT;
210*2f16049cSEmmanuel Vadot 		error = ds1307_ctrl_write(sc);
211*2f16049cSEmmanuel Vadot 		if (error != 0)
212*2f16049cSEmmanuel Vadot 			return (error);
213*2f16049cSEmmanuel Vadot 	}
214*2f16049cSEmmanuel Vadot 
215*2f16049cSEmmanuel Vadot 	return (error);
216*2f16049cSEmmanuel Vadot }
217*2f16049cSEmmanuel Vadot 
218*2f16049cSEmmanuel Vadot static int
219*2f16049cSEmmanuel Vadot ds1307_probe(device_t dev)
220*2f16049cSEmmanuel Vadot {
221*2f16049cSEmmanuel Vadot #ifdef FDT
222*2f16049cSEmmanuel Vadot 	const struct ofw_compat_data *compat;
223*2f16049cSEmmanuel Vadot 
224*2f16049cSEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
225*2f16049cSEmmanuel Vadot 		return (ENXIO);
226*2f16049cSEmmanuel Vadot 
227*2f16049cSEmmanuel Vadot 	compat = ofw_bus_search_compatible(dev, ds1307_compat_data);
228*2f16049cSEmmanuel Vadot 	if (compat->ocd_str == NULL)
229*2f16049cSEmmanuel Vadot 		return (ENXIO);
230*2f16049cSEmmanuel Vadot 
231*2f16049cSEmmanuel Vadot 	switch(compat->ocd_data) {
232*2f16049cSEmmanuel Vadot 		case TYPE_DS1307:
233*2f16049cSEmmanuel Vadot 			device_set_desc(dev, "Dallas DS1307");
234*2f16049cSEmmanuel Vadot 			break;
235*2f16049cSEmmanuel Vadot 		case TYPE_MAXIM1307:
236*2f16049cSEmmanuel Vadot 			device_set_desc(dev, "Maxim DS1307");
237*2f16049cSEmmanuel Vadot 			break;
238*2f16049cSEmmanuel Vadot 		case TYPE_MICROCHIP_MCP7491X:
239*2f16049cSEmmanuel Vadot 			device_set_desc(dev, "Microchip MCP7491X");
240*2f16049cSEmmanuel Vadot 			break;
241*2f16049cSEmmanuel Vadot 		case TYPE_EPSON_RX8035:
242*2f16049cSEmmanuel Vadot 			device_set_desc(dev, "Epson RX-8035");
243*2f16049cSEmmanuel Vadot 			break;
244*2f16049cSEmmanuel Vadot 		default:
245*2f16049cSEmmanuel Vadot 			device_set_desc(dev, "Unknown DS1307-like device");
246*2f16049cSEmmanuel Vadot 			break;
247*2f16049cSEmmanuel Vadot 	}
248*2f16049cSEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
249*2f16049cSEmmanuel Vadot #endif
250*2f16049cSEmmanuel Vadot 
251*2f16049cSEmmanuel Vadot 	device_set_desc(dev, "Maxim DS1307 RTC");
252*2f16049cSEmmanuel Vadot 	return (BUS_PROBE_NOWILDCARD);
253*2f16049cSEmmanuel Vadot }
254*2f16049cSEmmanuel Vadot 
255*2f16049cSEmmanuel Vadot static int
256*2f16049cSEmmanuel Vadot ds1307_attach(device_t dev)
257*2f16049cSEmmanuel Vadot {
258*2f16049cSEmmanuel Vadot #ifdef FDT
259*2f16049cSEmmanuel Vadot 	const struct ofw_compat_data *compat;
260*2f16049cSEmmanuel Vadot #endif
261*2f16049cSEmmanuel Vadot 	struct ds1307_softc *sc;
262*2f16049cSEmmanuel Vadot 
263*2f16049cSEmmanuel Vadot 	sc = device_get_softc(dev);
264*2f16049cSEmmanuel Vadot 	sc->sc_dev = dev;
265*2f16049cSEmmanuel Vadot 	sc->enum_hook.ich_func = ds1307_start;
266*2f16049cSEmmanuel Vadot 	sc->enum_hook.ich_arg = dev;
267*2f16049cSEmmanuel Vadot #ifdef FDT
268*2f16049cSEmmanuel Vadot 	compat = ofw_bus_search_compatible(dev, ds1307_compat_data);
269*2f16049cSEmmanuel Vadot 	sc->chiptype = compat->ocd_data;
270*2f16049cSEmmanuel Vadot 	/* Unify the chiptypes to DS1307 where possible. */
271*2f16049cSEmmanuel Vadot 	if (sc->chiptype == TYPE_MAXIM1307)
272*2f16049cSEmmanuel Vadot 		sc->chiptype = TYPE_DS1307;
273*2f16049cSEmmanuel Vadot #else
274*2f16049cSEmmanuel Vadot 	sc->chiptype = TYPE_DS1307;
275*2f16049cSEmmanuel Vadot #endif
276*2f16049cSEmmanuel Vadot 
277*2f16049cSEmmanuel Vadot 	/*
278*2f16049cSEmmanuel Vadot 	 * We have to wait until interrupts are enabled.  Usually I2C read
279*2f16049cSEmmanuel Vadot 	 * and write only works when the interrupts are available.
280*2f16049cSEmmanuel Vadot 	 */
281*2f16049cSEmmanuel Vadot 	if (config_intrhook_establish(&sc->enum_hook) != 0)
282*2f16049cSEmmanuel Vadot 		return (ENOMEM);
283*2f16049cSEmmanuel Vadot 
284*2f16049cSEmmanuel Vadot 	return (0);
285*2f16049cSEmmanuel Vadot }
286*2f16049cSEmmanuel Vadot 
287*2f16049cSEmmanuel Vadot static int
288*2f16049cSEmmanuel Vadot ds1307_detach(device_t dev)
289*2f16049cSEmmanuel Vadot {
290*2f16049cSEmmanuel Vadot 
291*2f16049cSEmmanuel Vadot 	clock_unregister(dev);
292*2f16049cSEmmanuel Vadot 	return (0);
293*2f16049cSEmmanuel Vadot }
294*2f16049cSEmmanuel Vadot 
295*2f16049cSEmmanuel Vadot static bool
296*2f16049cSEmmanuel Vadot is_epson_time_valid(struct ds1307_softc *sc)
297*2f16049cSEmmanuel Vadot {
298*2f16049cSEmmanuel Vadot 	device_t dev;
299*2f16049cSEmmanuel Vadot 	int error;
300*2f16049cSEmmanuel Vadot 	uint8_t ctrl2;
301*2f16049cSEmmanuel Vadot 
302*2f16049cSEmmanuel Vadot 	dev = sc->sc_dev;
303*2f16049cSEmmanuel Vadot 
304*2f16049cSEmmanuel Vadot 	/*
305*2f16049cSEmmanuel Vadot 	 * The RX-8035 single register read is non-standard
306*2f16049cSEmmanuel Vadot 	 * Refer to section 8.9.5 of the RX-8035 application manual:
307*2f16049cSEmmanuel Vadot 	 * "I2C bus basic transfer format", under "Standard Read Method".
308*2f16049cSEmmanuel Vadot 	 * Basically, register to read goes into the top 4 bits.
309*2f16049cSEmmanuel Vadot 	 */
310*2f16049cSEmmanuel Vadot 	error = ds1307_read1(dev, (RX8035_CTRL_2 << 4), &ctrl2);
311*2f16049cSEmmanuel Vadot 	if (error) {
312*2f16049cSEmmanuel Vadot 		device_printf(dev, "%s cannot read Control 2 register: %d\n",
313*2f16049cSEmmanuel Vadot 		    __func__, error);
314*2f16049cSEmmanuel Vadot 		return (false);
315*2f16049cSEmmanuel Vadot 	}
316*2f16049cSEmmanuel Vadot 
317*2f16049cSEmmanuel Vadot 	if (ctrl2 & RX8035_CTRL_2_XSTP) {
318*2f16049cSEmmanuel Vadot 		device_printf(dev, "Oscillation stop detected (ctrl2=%#02x)\n",
319*2f16049cSEmmanuel Vadot 		    ctrl2);
320*2f16049cSEmmanuel Vadot 		return (false);
321*2f16049cSEmmanuel Vadot 	}
322*2f16049cSEmmanuel Vadot 
323*2f16049cSEmmanuel Vadot 	/*
324*2f16049cSEmmanuel Vadot 	 * Power on reset (PON) generally implies oscillation stop,
325*2f16049cSEmmanuel Vadot 	 * but catch it as well to be sure.
326*2f16049cSEmmanuel Vadot 	 */
327*2f16049cSEmmanuel Vadot 	if (ctrl2 & RX8035_CTRL_2_PON) {
328*2f16049cSEmmanuel Vadot 		device_printf(dev, "Power-on reset detected (ctrl2=%#02x)\n",
329*2f16049cSEmmanuel Vadot 		    ctrl2);
330*2f16049cSEmmanuel Vadot 		return (false);
331*2f16049cSEmmanuel Vadot 	}
332*2f16049cSEmmanuel Vadot 
333*2f16049cSEmmanuel Vadot 	return (true);
334*2f16049cSEmmanuel Vadot }
335*2f16049cSEmmanuel Vadot 
336*2f16049cSEmmanuel Vadot static int
337*2f16049cSEmmanuel Vadot mark_epson_time_valid(struct ds1307_softc *sc)
338*2f16049cSEmmanuel Vadot {
339*2f16049cSEmmanuel Vadot 	device_t dev;
340*2f16049cSEmmanuel Vadot 	int error;
341*2f16049cSEmmanuel Vadot 	uint8_t ctrl2;
342*2f16049cSEmmanuel Vadot 	uint8_t control_mask;
343*2f16049cSEmmanuel Vadot 
344*2f16049cSEmmanuel Vadot 	dev = sc->sc_dev;
345*2f16049cSEmmanuel Vadot 
346*2f16049cSEmmanuel Vadot 	error = ds1307_read1(dev, (RX8035_CTRL_2 << 4), &ctrl2);
347*2f16049cSEmmanuel Vadot 	if (error) {
348*2f16049cSEmmanuel Vadot 		device_printf(dev, "%s cannot read Control 2 register: %d\n",
349*2f16049cSEmmanuel Vadot 		    __func__, error);
350*2f16049cSEmmanuel Vadot 		return (false);
351*2f16049cSEmmanuel Vadot 	}
352*2f16049cSEmmanuel Vadot 
353*2f16049cSEmmanuel Vadot 	control_mask = (RX8035_CTRL_2_PON | RX8035_CTRL_2_XSTP | RX8035_CTRL_2_VDET);
354*2f16049cSEmmanuel Vadot 	ctrl2 = ctrl2 & ~(control_mask);
355*2f16049cSEmmanuel Vadot 
356*2f16049cSEmmanuel Vadot 	error = ds1307_write1(dev, (RX8035_CTRL_2 << 4), ctrl2);
357*2f16049cSEmmanuel Vadot 	if (error) {
358*2f16049cSEmmanuel Vadot 		device_printf(dev, "%s cannot write to Control 2 register: %d\n",
359*2f16049cSEmmanuel Vadot 		    __func__, error);
360*2f16049cSEmmanuel Vadot 		return (false);
361*2f16049cSEmmanuel Vadot 	}
362*2f16049cSEmmanuel Vadot 	return (true);
363*2f16049cSEmmanuel Vadot }
364*2f16049cSEmmanuel Vadot 
365*2f16049cSEmmanuel Vadot static bool is_dev_time_valid(struct ds1307_softc *sc)
366*2f16049cSEmmanuel Vadot {
367*2f16049cSEmmanuel Vadot 	device_t dev;
368*2f16049cSEmmanuel Vadot 	int error;
369*2f16049cSEmmanuel Vadot 	uint8_t osc_en;
370*2f16049cSEmmanuel Vadot 	uint8_t secs;
371*2f16049cSEmmanuel Vadot 
372*2f16049cSEmmanuel Vadot 	/* Epson RTCs have different control/status registers. */
373*2f16049cSEmmanuel Vadot 	if (sc->chiptype == TYPE_EPSON_RX8035)
374*2f16049cSEmmanuel Vadot 		return (is_epson_time_valid(sc));
375*2f16049cSEmmanuel Vadot 
376*2f16049cSEmmanuel Vadot 	dev = sc->sc_dev;
377*2f16049cSEmmanuel Vadot 	/* Check if the oscillator is disabled. */
378*2f16049cSEmmanuel Vadot 	error = ds1307_read1(dev, DS1307_SECS, &secs);
379*2f16049cSEmmanuel Vadot 	if (error) {
380*2f16049cSEmmanuel Vadot 		device_printf(dev, "%s: cannot read from RTC: %d\n",
381*2f16049cSEmmanuel Vadot 		    __func__, error);
382*2f16049cSEmmanuel Vadot 		return (false);
383*2f16049cSEmmanuel Vadot 	}
384*2f16049cSEmmanuel Vadot 
385*2f16049cSEmmanuel Vadot 	switch (sc->chiptype) {
386*2f16049cSEmmanuel Vadot 	case TYPE_MICROCHIP_MCP7491X:
387*2f16049cSEmmanuel Vadot 		osc_en = 0x80;
388*2f16049cSEmmanuel Vadot 		break;
389*2f16049cSEmmanuel Vadot 	default:
390*2f16049cSEmmanuel Vadot 		osc_en = 0x00;
391*2f16049cSEmmanuel Vadot 		break;
392*2f16049cSEmmanuel Vadot 	}
393*2f16049cSEmmanuel Vadot 	if (((secs & DS1307_SECS_CH) ^ osc_en) != 0)
394*2f16049cSEmmanuel Vadot 		return (false);
395*2f16049cSEmmanuel Vadot 
396*2f16049cSEmmanuel Vadot 	return (true);
397*2f16049cSEmmanuel Vadot }
398*2f16049cSEmmanuel Vadot 
399*2f16049cSEmmanuel Vadot static void
400*2f16049cSEmmanuel Vadot ds1307_start(void *xdev)
401*2f16049cSEmmanuel Vadot {
402*2f16049cSEmmanuel Vadot 	device_t dev;
403*2f16049cSEmmanuel Vadot 	struct ds1307_softc *sc;
404*2f16049cSEmmanuel Vadot 	struct sysctl_ctx_list *ctx;
405*2f16049cSEmmanuel Vadot 	struct sysctl_oid *tree_node;
406*2f16049cSEmmanuel Vadot 	struct sysctl_oid_list *tree;
407*2f16049cSEmmanuel Vadot 
408*2f16049cSEmmanuel Vadot 	dev = (device_t)xdev;
409*2f16049cSEmmanuel Vadot 	sc = device_get_softc(dev);
410*2f16049cSEmmanuel Vadot 
411*2f16049cSEmmanuel Vadot 	config_intrhook_disestablish(&sc->enum_hook);
412*2f16049cSEmmanuel Vadot 
413*2f16049cSEmmanuel Vadot 	if (!is_dev_time_valid(sc))
414*2f16049cSEmmanuel Vadot 		device_printf(dev,
415*2f16049cSEmmanuel Vadot 		    "WARNING: RTC clock stopped, check the battery.\n");
416*2f16049cSEmmanuel Vadot 
417*2f16049cSEmmanuel Vadot 	/*
418*2f16049cSEmmanuel Vadot 	 * Configuration parameters:
419*2f16049cSEmmanuel Vadot 	 * square wave output cannot be changed or inhibited on the RX-8035,
420*2f16049cSEmmanuel Vadot 	 * so don't present the sysctls there.
421*2f16049cSEmmanuel Vadot 	 */
422*2f16049cSEmmanuel Vadot 	if (sc->chiptype == TYPE_EPSON_RX8035)
423*2f16049cSEmmanuel Vadot 		goto skip_sysctl;
424*2f16049cSEmmanuel Vadot 
425*2f16049cSEmmanuel Vadot 	ctx = device_get_sysctl_ctx(dev);
426*2f16049cSEmmanuel Vadot 	tree_node = device_get_sysctl_tree(dev);
427*2f16049cSEmmanuel Vadot 	tree = SYSCTL_CHILDREN(tree_node);
428*2f16049cSEmmanuel Vadot 
429*2f16049cSEmmanuel Vadot 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "sqwe",
430*2f16049cSEmmanuel Vadot 	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
431*2f16049cSEmmanuel Vadot 	    ds1307_sqwe_sysctl, "IU", "DS1307 square-wave enable");
432*2f16049cSEmmanuel Vadot 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "sqw_freq",
433*2f16049cSEmmanuel Vadot 	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
434*2f16049cSEmmanuel Vadot 	    ds1307_sqw_freq_sysctl, "IU",
435*2f16049cSEmmanuel Vadot 	    "DS1307 square-wave output frequency");
436*2f16049cSEmmanuel Vadot 	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "sqw_out",
437*2f16049cSEmmanuel Vadot 	    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,
438*2f16049cSEmmanuel Vadot 	    ds1307_sqw_out_sysctl, "IU", "DS1307 square-wave output state");
439*2f16049cSEmmanuel Vadot skip_sysctl:
440*2f16049cSEmmanuel Vadot 
441*2f16049cSEmmanuel Vadot 	/*
442*2f16049cSEmmanuel Vadot 	 * Register as a clock with 1 second resolution.  Schedule the
443*2f16049cSEmmanuel Vadot 	 * clock_settime() method to be called just after top-of-second;
444*2f16049cSEmmanuel Vadot 	 * resetting the time resets top-of-second in the hardware.
445*2f16049cSEmmanuel Vadot 	 */
446*2f16049cSEmmanuel Vadot 	clock_register_flags(dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
447*2f16049cSEmmanuel Vadot 	clock_schedule(dev, 1);
448*2f16049cSEmmanuel Vadot }
449*2f16049cSEmmanuel Vadot 
450*2f16049cSEmmanuel Vadot static int
451*2f16049cSEmmanuel Vadot ds1307_gettime(device_t dev, struct timespec *ts)
452*2f16049cSEmmanuel Vadot {
453*2f16049cSEmmanuel Vadot 	struct bcd_clocktime bct;
454*2f16049cSEmmanuel Vadot 	struct ds1307_softc *sc;
455*2f16049cSEmmanuel Vadot 	int error;
456*2f16049cSEmmanuel Vadot 	uint8_t data[7], hourmask, ampm_mode;
457*2f16049cSEmmanuel Vadot 
458*2f16049cSEmmanuel Vadot 	sc = device_get_softc(dev);
459*2f16049cSEmmanuel Vadot 	error = iicdev_readfrom(sc->sc_dev, DS1307_SECS, data, sizeof(data),
460*2f16049cSEmmanuel Vadot 	    IIC_INTRWAIT);
461*2f16049cSEmmanuel Vadot 	if (error != 0) {
462*2f16049cSEmmanuel Vadot 		device_printf(dev, "%s: cannot read from RTC: %d\n",
463*2f16049cSEmmanuel Vadot 		    __func__, error);
464*2f16049cSEmmanuel Vadot 		return (error);
465*2f16049cSEmmanuel Vadot 	}
466*2f16049cSEmmanuel Vadot 
467*2f16049cSEmmanuel Vadot 	if (!is_dev_time_valid(sc)) {
468*2f16049cSEmmanuel Vadot 		device_printf(dev, "Device time not valid.\n");
469*2f16049cSEmmanuel Vadot 		return (EINVAL);
470*2f16049cSEmmanuel Vadot 	}
471*2f16049cSEmmanuel Vadot 
472*2f16049cSEmmanuel Vadot 	/*
473*2f16049cSEmmanuel Vadot 	 * If the chip is in AM/PM mode remember that.
474*2f16049cSEmmanuel Vadot 	 * The EPSON uses a 1 to signify 24 hour mode, while the DS uses a 0,
475*2f16049cSEmmanuel Vadot 	 * in slighly different positions.
476*2f16049cSEmmanuel Vadot 	 */
477*2f16049cSEmmanuel Vadot 	if (sc->chiptype == TYPE_EPSON_RX8035)
478*2f16049cSEmmanuel Vadot 		ampm_mode = !(data[DS1307_HOUR] & RX8035_HOUR_USE_24);
479*2f16049cSEmmanuel Vadot 	else
480*2f16049cSEmmanuel Vadot 		ampm_mode = data[DS1307_HOUR] & DS1307_HOUR_USE_AMPM;
481*2f16049cSEmmanuel Vadot 
482*2f16049cSEmmanuel Vadot 	if (ampm_mode) {
483*2f16049cSEmmanuel Vadot 		sc->sc_use_ampm = true;
484*2f16049cSEmmanuel Vadot 		hourmask = DS1307_HOUR_MASK_12HR;
485*2f16049cSEmmanuel Vadot 	} else
486*2f16049cSEmmanuel Vadot 		hourmask = DS1307_HOUR_MASK_24HR;
487*2f16049cSEmmanuel Vadot 
488*2f16049cSEmmanuel Vadot 	bct.nsec = 0;
489*2f16049cSEmmanuel Vadot 	bct.ispm = (data[DS1307_HOUR] & DS1307_HOUR_IS_PM) != 0;
490*2f16049cSEmmanuel Vadot 	bct.sec  = data[DS1307_SECS]  & DS1307_SECS_MASK;
491*2f16049cSEmmanuel Vadot 	bct.min  = data[DS1307_MINS]  & DS1307_MINS_MASK;
492*2f16049cSEmmanuel Vadot 	bct.hour = data[DS1307_HOUR]  & hourmask;
493*2f16049cSEmmanuel Vadot 	bct.day  = data[DS1307_DATE]  & DS1307_DATE_MASK;
494*2f16049cSEmmanuel Vadot 	bct.mon  = data[DS1307_MONTH] & DS1307_MONTH_MASK;
495*2f16049cSEmmanuel Vadot 	bct.year = data[DS1307_YEAR]  & DS1307_YEAR_MASK;
496*2f16049cSEmmanuel Vadot 
497*2f16049cSEmmanuel Vadot 	clock_dbgprint_bcd(sc->sc_dev, CLOCK_DBG_READ, &bct);
498*2f16049cSEmmanuel Vadot 	return (clock_bcd_to_ts(&bct, ts, sc->sc_use_ampm));
499*2f16049cSEmmanuel Vadot }
500*2f16049cSEmmanuel Vadot 
501*2f16049cSEmmanuel Vadot static int
502*2f16049cSEmmanuel Vadot ds1307_settime(device_t dev, struct timespec *ts)
503*2f16049cSEmmanuel Vadot {
504*2f16049cSEmmanuel Vadot 	struct bcd_clocktime bct;
505*2f16049cSEmmanuel Vadot 	struct ds1307_softc *sc;
506*2f16049cSEmmanuel Vadot 	int error, year;
507*2f16049cSEmmanuel Vadot 	uint8_t data[7];
508*2f16049cSEmmanuel Vadot 	uint8_t pmflags;
509*2f16049cSEmmanuel Vadot 
510*2f16049cSEmmanuel Vadot 	sc = device_get_softc(dev);
511*2f16049cSEmmanuel Vadot 
512*2f16049cSEmmanuel Vadot 	/*
513*2f16049cSEmmanuel Vadot 	 * We request a timespec with no resolution-adjustment.  That also
514*2f16049cSEmmanuel Vadot 	 * disables utc adjustment, so apply that ourselves.
515*2f16049cSEmmanuel Vadot 	 */
516*2f16049cSEmmanuel Vadot 	ts->tv_sec -= utc_offset();
517*2f16049cSEmmanuel Vadot 	clock_ts_to_bcd(ts, &bct, sc->sc_use_ampm);
518*2f16049cSEmmanuel Vadot 	clock_dbgprint_bcd(sc->sc_dev, CLOCK_DBG_WRITE, &bct);
519*2f16049cSEmmanuel Vadot 
520*2f16049cSEmmanuel Vadot 	/*
521*2f16049cSEmmanuel Vadot 	 * If the chip is in AM/PM mode, adjust hour and set flags as needed.
522*2f16049cSEmmanuel Vadot 	 * The AM/PM bit polarity and position is different on the EPSON.
523*2f16049cSEmmanuel Vadot 	 */
524*2f16049cSEmmanuel Vadot 	if (sc->sc_use_ampm) {
525*2f16049cSEmmanuel Vadot 		pmflags = (sc->chiptype != TYPE_EPSON_RX8035) ?
526*2f16049cSEmmanuel Vadot 				DS1307_HOUR_USE_AMPM : 0;
527*2f16049cSEmmanuel Vadot 		if (bct.ispm)
528*2f16049cSEmmanuel Vadot 			pmflags |= DS1307_HOUR_IS_PM;
529*2f16049cSEmmanuel Vadot 
530*2f16049cSEmmanuel Vadot 	} else if (sc->chiptype == TYPE_EPSON_RX8035)
531*2f16049cSEmmanuel Vadot 		pmflags = RX8035_HOUR_USE_24;
532*2f16049cSEmmanuel Vadot 	else
533*2f16049cSEmmanuel Vadot 		pmflags = 0;
534*2f16049cSEmmanuel Vadot 
535*2f16049cSEmmanuel Vadot 	data[DS1307_SECS]    = bct.sec;
536*2f16049cSEmmanuel Vadot 	data[DS1307_MINS]    = bct.min;
537*2f16049cSEmmanuel Vadot 	data[DS1307_HOUR]    = bct.hour | pmflags;
538*2f16049cSEmmanuel Vadot 	data[DS1307_DATE]    = bct.day;
539*2f16049cSEmmanuel Vadot 	data[DS1307_WEEKDAY] = bct.dow;
540*2f16049cSEmmanuel Vadot 	data[DS1307_MONTH]   = bct.mon;
541*2f16049cSEmmanuel Vadot 	data[DS1307_YEAR]    = bct.year & 0xff;
542*2f16049cSEmmanuel Vadot 	if (sc->chiptype == TYPE_MICROCHIP_MCP7491X) {
543*2f16049cSEmmanuel Vadot 		data[DS1307_SECS] |= MCP7941X_SECS_ST;
544*2f16049cSEmmanuel Vadot 		data[DS1307_WEEKDAY] |= MCP7941X_WEEKDAY_VBATEN;
545*2f16049cSEmmanuel Vadot 		year = bcd2bin(bct.year >> 8) * 100 + bcd2bin(bct.year & 0xff);
546*2f16049cSEmmanuel Vadot 		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
547*2f16049cSEmmanuel Vadot 			data[DS1307_MONTH] |= MCP7941X_MONTH_LPYR;
548*2f16049cSEmmanuel Vadot 	}
549*2f16049cSEmmanuel Vadot 
550*2f16049cSEmmanuel Vadot 	/* Write the time back to RTC. */
551*2f16049cSEmmanuel Vadot 	error = iicdev_writeto(sc->sc_dev, DS1307_SECS, data, sizeof(data),
552*2f16049cSEmmanuel Vadot 	    IIC_INTRWAIT);
553*2f16049cSEmmanuel Vadot 	if (error != 0)
554*2f16049cSEmmanuel Vadot 		device_printf(dev, "%s: cannot write to RTC: %d\n",
555*2f16049cSEmmanuel Vadot 		    __func__, error);
556*2f16049cSEmmanuel Vadot 
557*2f16049cSEmmanuel Vadot 	if (sc->chiptype == TYPE_EPSON_RX8035)
558*2f16049cSEmmanuel Vadot 		error = mark_epson_time_valid(sc);
559*2f16049cSEmmanuel Vadot 
560*2f16049cSEmmanuel Vadot 	return (error);
561*2f16049cSEmmanuel Vadot }
562*2f16049cSEmmanuel Vadot 
563*2f16049cSEmmanuel Vadot static device_method_t ds1307_methods[] = {
564*2f16049cSEmmanuel Vadot 	DEVMETHOD(device_probe,		ds1307_probe),
565*2f16049cSEmmanuel Vadot 	DEVMETHOD(device_attach,	ds1307_attach),
566*2f16049cSEmmanuel Vadot 	DEVMETHOD(device_detach,	ds1307_detach),
567*2f16049cSEmmanuel Vadot 
568*2f16049cSEmmanuel Vadot 	DEVMETHOD(clock_gettime,	ds1307_gettime),
569*2f16049cSEmmanuel Vadot 	DEVMETHOD(clock_settime,	ds1307_settime),
570*2f16049cSEmmanuel Vadot 
571*2f16049cSEmmanuel Vadot 	DEVMETHOD_END
572*2f16049cSEmmanuel Vadot };
573*2f16049cSEmmanuel Vadot 
574*2f16049cSEmmanuel Vadot static driver_t ds1307_driver = {
575*2f16049cSEmmanuel Vadot 	"ds1307",
576*2f16049cSEmmanuel Vadot 	ds1307_methods,
577*2f16049cSEmmanuel Vadot 	sizeof(struct ds1307_softc),
578*2f16049cSEmmanuel Vadot };
579*2f16049cSEmmanuel Vadot 
580*2f16049cSEmmanuel Vadot DRIVER_MODULE(ds1307, iicbus, ds1307_driver, NULL, NULL);
581*2f16049cSEmmanuel Vadot MODULE_VERSION(ds1307, 1);
582*2f16049cSEmmanuel Vadot MODULE_DEPEND(ds1307, iicbus, 1, 1, 1);
583*2f16049cSEmmanuel Vadot IICBUS_FDT_PNP_INFO(ds1307_compat_data);
584