2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
37 #include <sys/clock.h>
38 #include <sys/resource.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <mips/atheros/pcf2123reg.h>
46 #include <dev/spibus/spi.h>
47 #include "spibus_if.h"
51 #define YEAR_BASE 1970
52 #define PCF2123_DELAY 50
54 struct pcf2123_rtc_softc {
58 static int pcf2123_rtc_probe(device_t dev);
59 static int pcf2123_rtc_attach(device_t dev);
61 static int pcf2123_rtc_gettime(device_t dev, struct timespec *ts);
62 static int pcf2123_rtc_settime(device_t dev, struct timespec *ts);
65 pcf2123_rtc_probe(device_t dev)
68 device_set_desc(dev, "PCF2123 SPI RTC");
73 pcf2123_rtc_attach(device_t dev)
75 struct pcf2123_rtc_softc *sc;
76 struct spi_command cmd;
77 unsigned char rxBuf[3];
78 unsigned char txBuf[3];
81 sc = device_get_softc(dev);
84 clock_register(dev, 1000000);
86 memset(&cmd, 0, sizeof(cmd));
87 memset(rxBuf, 0, sizeof(rxBuf));
88 memset(txBuf, 0, sizeof(txBuf));
90 /* Make sure Ctrl1 and Ctrl2 are zeroes */
91 txBuf[0] = PCF2123_WRITE(PCF2123_REG_CTRL1);
94 cmd.rx_cmd_sz = sizeof(rxBuf);
95 cmd.tx_cmd_sz = sizeof(txBuf);
96 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
103 pcf2123_rtc_gettime(device_t dev, struct timespec *ts)
106 struct spi_command cmd;
107 unsigned char rxTimedate[8];
108 unsigned char txTimedate[8];
111 memset(&cmd, 0, sizeof(cmd));
112 memset(rxTimedate, 0, sizeof(rxTimedate));
113 memset(txTimedate, 0, sizeof(txTimedate));
116 * Counter is stopped when access to time registers is in progress
117 * So there is no need to stop/start counter
119 /* Start reading from seconds */
120 txTimedate[0] = PCF2123_READ(PCF2123_REG_SECONDS);
121 cmd.rx_cmd = rxTimedate;
122 cmd.tx_cmd = txTimedate;
123 cmd.rx_cmd_sz = sizeof(rxTimedate);
124 cmd.tx_cmd_sz = sizeof(txTimedate);
125 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
126 DELAY(PCF2123_DELAY);
129 ct.sec = FROMBCD(rxTimedate[1] & 0x7f);
130 ct.min = FROMBCD(rxTimedate[2] & 0x7f);
131 ct.hour = FROMBCD(rxTimedate[3] & 0x3f);
133 ct.dow = FROMBCD(rxTimedate[5] & 0x3f);
135 ct.day = FROMBCD(rxTimedate[4] & 0x3f);
136 ct.mon = FROMBCD(rxTimedate[6] & 0x1f);
137 ct.year = YEAR_BASE + FROMBCD(rxTimedate[7]);
139 return (clock_ct_to_ts(&ct, ts));
143 pcf2123_rtc_settime(device_t dev, struct timespec *ts)
146 struct pcf2123_rtc_softc *sc;
147 struct spi_command cmd;
148 unsigned char rxTimedate[8];
149 unsigned char txTimedate[8];
152 sc = device_get_softc(dev);
154 /* Resolution: 1 sec */
155 if (ts->tv_nsec >= 500000000)
158 clock_ts_to_ct(ts, &ct);
160 memset(&cmd, 0, sizeof(cmd));
161 memset(rxTimedate, 0, sizeof(rxTimedate));
162 memset(txTimedate, 0, sizeof(txTimedate));
164 /* Start reading from seconds */
165 cmd.rx_cmd = rxTimedate;
166 cmd.tx_cmd = txTimedate;
167 cmd.rx_cmd_sz = sizeof(rxTimedate);
168 cmd.tx_cmd_sz = sizeof(txTimedate);
171 * Counter is stopped when access to time registers is in progress
172 * So there is no need to stop/start counter
174 txTimedate[0] = PCF2123_WRITE(PCF2123_REG_SECONDS);
175 txTimedate[1] = TOBCD(ct.sec);
176 txTimedate[2] = TOBCD(ct.min);
177 txTimedate[3] = TOBCD(ct.hour);
178 txTimedate[4] = TOBCD(ct.day);
179 txTimedate[5] = TOBCD(ct.dow);
180 txTimedate[6] = TOBCD(ct.mon);
181 txTimedate[7] = TOBCD(ct.year - YEAR_BASE);
183 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
184 DELAY(PCF2123_DELAY);
189 static device_method_t pcf2123_rtc_methods[] = {
190 DEVMETHOD(device_probe, pcf2123_rtc_probe),
191 DEVMETHOD(device_attach, pcf2123_rtc_attach),
193 DEVMETHOD(clock_gettime, pcf2123_rtc_gettime),
194 DEVMETHOD(clock_settime, pcf2123_rtc_settime),
199 static driver_t pcf2123_rtc_driver = {
202 sizeof(struct pcf2123_rtc_softc),
204 static devclass_t pcf2123_rtc_devclass;
206 DRIVER_MODULE(pcf2123_rtc, spibus, pcf2123_rtc_driver, pcf2123_rtc_devclass, 0, 0);