1/*-
2 * Copyright (c) 2012 Marius Strobl <marius@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30/*
31 * Driver for NXP PCF8563 real-time clock/calendar
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/bus.h>
37#include <sys/clock.h>
38#include <sys/kernel.h>
39#include <sys/module.h>
40
41#include <dev/iicbus/iicbus.h>
42#include <dev/iicbus/iiconf.h>
43#include <dev/iicbus/pcf8563reg.h>
44
45#include "clock_if.h"
46#include "iicbus_if.h"
47
48#define	PCF8563_NCLOCKREGS	(PCF8563_R_YEAR - PCF8563_R_CS1 + 1)
49
50struct pcf8563_softc {
51	uint32_t	sc_flags;
52#define	PCF8563_CPOL	(1 << 0)	/* PCF8563_R_MONTH_C means 19xx */
53	uint16_t	sc_addr;	/* PCF8563 slave address */
54	uint16_t	sc_year0;	/* TOD clock year 0 */
55};
56
57static device_attach_t pcf8563_attach;
58static device_probe_t pcf8563_probe;
59static clock_gettime_t pcf8563_gettime;
60static clock_settime_t pcf8563_settime;
61
62static int
63pcf8563_probe(device_t dev)
64{
65
66	device_set_desc(dev, "NXP PCF8563 RTC");
67	return (BUS_PROBE_NOWILDCARD);
68}
69
70static int
71pcf8563_attach(device_t dev)
72{
73	uint8_t reg = PCF8563_R_SECOND, val;
74	struct iic_msg msgs[] = {
75		{ 0, IIC_M_WR, sizeof(reg), &reg },
76		{ 0, IIC_M_RD, sizeof(val), &val }
77	};
78	struct pcf8563_softc *sc;
79	int error;
80
81	sc = device_get_softc(dev);
82	sc->sc_addr = iicbus_get_addr(dev);
83	if (sc->sc_addr == 0)
84		sc->sc_addr = PCF8563_ADDR;
85
86	/*
87	 * NB: PCF8563_R_SECOND_VL doesn't automatically clear when VDD
88	 * rises above Vlow again and needs to be cleared manually.
89	 * However, apparently this needs all of the time registers to be
90	 * set, i.e. pcf8563_settime(), and not just PCF8563_R_SECOND in
91	 * order for PCF8563_R_SECOND_VL to stick.  Thus, we just issue a
92	 * warning here rather than failing with ENXIO in case it is set.
93	 * Note that pcf8563_settime() will also clear PCF8563_R_SECOND_VL
94	 * as a side-effect.
95	 */
96	msgs[0].slave = msgs[1].slave = sc->sc_addr;
97	error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) /
98	    sizeof(*msgs));
99	if (error != 0) {
100		device_printf(dev, "%s: cannot read RTC\n", __func__);
101		return (error);
102	}
103	if ((val & PCF8563_R_SECOND_VL) != 0)
104		device_printf(dev, "%s: battery low\n", __func__);
105
106	sc->sc_year0 = 1900;
107	clock_register(dev, 1000000);   /* 1 second resolution */
108	return (0);
109}
110
111static int
112pcf8563_gettime(device_t dev, struct timespec *ts)
113{
114	struct clocktime ct;
115	uint8_t reg = PCF8563_R_SECOND, val[PCF8563_NCLOCKREGS];
116	struct iic_msg msgs[] = {
117		{ 0, IIC_M_WR, sizeof(reg), &reg },
118		{ 0, IIC_M_RD, PCF8563_NCLOCKREGS, &val[PCF8563_R_SECOND] }
119	};
120	struct pcf8563_softc *sc;
121	int error;
122
123	sc = device_get_softc(dev);
124	msgs[0].slave = msgs[1].slave = sc->sc_addr;
125	error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) /
126	    sizeof(*msgs));
127	if (error != 0) {
128		device_printf(dev, "%s: cannot read RTC\n", __func__);
129		return (error);
130	}
131
132	ct.nsec = 0;
133	ct.sec = FROMBCD(val[PCF8563_R_SECOND] & PCF8563_M_SECOND);
134	ct.min = FROMBCD(val[PCF8563_R_MINUTE] & PCF8563_M_MINUTE);
135	ct.hour = FROMBCD(val[PCF8563_R_HOUR] & PCF8563_M_HOUR);
136	ct.day = FROMBCD(val[PCF8563_R_DAY] & PCF8563_M_DAY);
137	ct.dow = val[PCF8563_R_WEEKDAY] & PCF8563_M_WEEKDAY;
138	ct.mon = FROMBCD(val[PCF8563_R_MONTH] & PCF8563_M_MONTH);
139	ct.year = FROMBCD(val[PCF8563_R_YEAR] & PCF8563_M_YEAR);
140	ct.year += sc->sc_year0;
141	if (ct.year < POSIX_BASE_YEAR)
142		ct.year += 100;	/* assume [1970, 2069] */
143	if ((val[PCF8563_R_MONTH] & PCF8563_R_MONTH_C) != 0) {
144		if (ct.year >= 100 + sc->sc_year0)
145			sc->sc_flags |= PCF8563_CPOL;
146	} else if (ct.year < 100 + sc->sc_year0)
147			sc->sc_flags |= PCF8563_CPOL;
148	return (clock_ct_to_ts(&ct, ts));
149}
150
151static int
152pcf8563_settime(device_t dev, struct timespec *ts)
153{
154	struct clocktime ct;
155	uint8_t val[PCF8563_NCLOCKREGS];
156	struct iic_msg msgs[] = {
157		{ 0, IIC_M_WR, PCF8563_NCLOCKREGS - 1, &val[PCF8563_R_CS2] }
158	};
159	struct pcf8563_softc *sc;
160	int error;
161
162	sc = device_get_softc(dev);
163	val[PCF8563_R_CS2] = PCF8563_R_SECOND;	/* abuse */
164	/* Accuracy is only one second. */
165	if (ts->tv_nsec >= 500000000)
166		ts->tv_sec++;
167	ts->tv_nsec = 0;
168	clock_ts_to_ct(ts, &ct);
169	val[PCF8563_R_SECOND] = TOBCD(ct.sec);
170	val[PCF8563_R_MINUTE] = TOBCD(ct.min);
171	val[PCF8563_R_HOUR] = TOBCD(ct.hour);
172	val[PCF8563_R_DAY] = TOBCD(ct.day);
173	val[PCF8563_R_WEEKDAY] = ct.dow;
174	val[PCF8563_R_MONTH] = TOBCD(ct.mon);
175	val[PCF8563_R_YEAR] = TOBCD(ct.year % 100);
176	if ((sc->sc_flags & PCF8563_CPOL) != 0) {
177		if (ct.year >= 100 + sc->sc_year0)
178			val[PCF8563_R_MONTH] |= PCF8563_R_MONTH_C;
179	} else if (ct.year < 100 + sc->sc_year0)
180			val[PCF8563_R_MONTH] |= PCF8563_R_MONTH_C;
181
182	msgs[0].slave = sc->sc_addr;
183	error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) /
184	    sizeof(*msgs));
185	if (error != 0)
186		device_printf(dev, "%s: cannot write RTC\n", __func__);
187	return (error);
188}
189
190static device_method_t pcf8563_methods[] = {
191	DEVMETHOD(device_probe,		pcf8563_probe),
192	DEVMETHOD(device_attach,	pcf8563_attach),
193
194	DEVMETHOD(clock_gettime,	pcf8563_gettime),
195	DEVMETHOD(clock_settime,	pcf8563_settime),
196
197	DEVMETHOD_END
198};
199
200static driver_t pcf8563_driver = {
201	"pcf8563_rtc",
202	pcf8563_methods,
203	sizeof(struct pcf8563_softc),
204};
205
206static devclass_t pcf8563_devclass;
207
208DRIVER_MODULE(pcf8563, iicbus, pcf8563_driver, pcf8563_devclass, NULL, NULL);
209MODULE_VERSION(pcf8563, 1);
210MODULE_DEPEND(pcf8563, iicbus, 1, 1, 1);
211