1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/lock.h>
36 #include <sys/time.h>
37 #include <sys/clock.h>
38 #include <sys/resource.h>
39 #include <sys/systm.h>
40 #include <sys/rman.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43
44 #include <mips/atheros/pcf2123reg.h>
45
46 #include <dev/spibus/spi.h>
47 #include "spibus_if.h"
48
49 #include "clock_if.h"
50
51 #define YEAR_BASE 1970
52 #define PCF2123_DELAY 50
53
54 struct pcf2123_rtc_softc {
55 device_t dev;
56 };
57
58 static int pcf2123_rtc_probe(device_t dev);
59 static int pcf2123_rtc_attach(device_t dev);
60
61 static int pcf2123_rtc_gettime(device_t dev, struct timespec *ts);
62 static int pcf2123_rtc_settime(device_t dev, struct timespec *ts);
63
64 static int
65 pcf2123_rtc_probe(device_t dev)
66 {
67
68 device_set_desc(dev, "PCF2123 SPI RTC");
69 return (0);
70 }
71
72 static int
73 pcf2123_rtc_attach(device_t dev)
74 {
75 struct pcf2123_rtc_softc *sc;
76 struct spi_command cmd;
77 unsigned char rxBuf[3];
78 unsigned char txBuf[3];
79 int err;
80
81 sc = device_get_softc(dev);
82 sc->dev = dev;
83
84 clock_register(dev, 1000000);
85
86 memset(&cmd, 0, sizeof(cmd));
87 memset(rxBuf, 0, sizeof(rxBuf));
88 memset(txBuf, 0, sizeof(txBuf));
89
90 /* Make sure Ctrl1 and Ctrl2 are zeroes */
91 txBuf[0] = PCF2123_WRITE(PCF2123_REG_CTRL1);
92 cmd.rx_cmd = rxBuf;
93 cmd.tx_cmd = txBuf;
94 cmd.rx_cmd_sz = sizeof(rxBuf);
95 cmd.tx_cmd_sz = sizeof(txBuf);
96 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
97 DELAY(PCF2123_DELAY);
98
99 return (0);
100 }
101
102 static int
103 pcf2123_rtc_gettime(device_t dev, struct timespec *ts)
104 {
105 struct clocktime ct;
106 struct spi_command cmd;
107 unsigned char rxTimedate[8];
108 unsigned char txTimedate[8];
109 int err;
110
111 memset(&cmd, 0, sizeof(cmd));
112 memset(rxTimedate, 0, sizeof(rxTimedate));
113 memset(txTimedate, 0, sizeof(txTimedate));
114
115 /*
116 * Counter is stopped when access to time registers is in progress
117 * So there is no need to stop/start counter
118 */
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);
127
128 ct.nsec = 0;
129 ct.sec = FROMBCD(rxTimedate[1] & 0x7f);
130 ct.min = FROMBCD(rxTimedate[2] & 0x7f);
131 ct.hour = FROMBCD(rxTimedate[3] & 0x3f);
132
133 ct.dow = FROMBCD(rxTimedate[5] & 0x3f);
134
135 ct.day = FROMBCD(rxTimedate[4] & 0x3f);
136 ct.mon = FROMBCD(rxTimedate[6] & 0x1f);
137 ct.year = YEAR_BASE + FROMBCD(rxTimedate[7]);
138
139 return (clock_ct_to_ts(&ct, ts));
140 }
141
142 static int
143 pcf2123_rtc_settime(device_t dev, struct timespec *ts)
144 {
145 struct clocktime ct;
146 struct pcf2123_rtc_softc *sc;
147 struct spi_command cmd;
148 unsigned char rxTimedate[8];
149 unsigned char txTimedate[8];
150 int err;
151
152 sc = device_get_softc(dev);
153
154 /* Resolution: 1 sec */
155 if (ts->tv_nsec >= 500000000)
156 ts->tv_sec++;
157 ts->tv_nsec = 0;
158 clock_ts_to_ct(ts, &ct);
159
160 memset(&cmd, 0, sizeof(cmd));
161 memset(rxTimedate, 0, sizeof(rxTimedate));
162 memset(txTimedate, 0, sizeof(txTimedate));
163
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);
169
170 /*
171 * Counter is stopped when access to time registers is in progress
172 * So there is no need to stop/start counter
173 */
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);
182
183 err = SPIBUS_TRANSFER(device_get_parent(dev), dev, &cmd);
184 DELAY(PCF2123_DELAY);
185
186 return (err);
187 }
188
189 static device_method_t pcf2123_rtc_methods[] = {
190 DEVMETHOD(device_probe, pcf2123_rtc_probe),
191 DEVMETHOD(device_attach, pcf2123_rtc_attach),
192
193 DEVMETHOD(clock_gettime, pcf2123_rtc_gettime),
194 DEVMETHOD(clock_settime, pcf2123_rtc_settime),
195
196 { 0, 0 },
197 };
198
199 static driver_t pcf2123_rtc_driver = {
200 "rtc",
201 pcf2123_rtc_methods,
202 sizeof(struct pcf2123_rtc_softc),
203 };
204 static devclass_t pcf2123_rtc_devclass;
205
206 DRIVER_MODULE(pcf2123_rtc, spibus, pcf2123_rtc_driver, pcf2123_rtc_devclass, 0, 0);
Cache object: 9aac77b486c0ae3ed1ac6276342608b3
|