1303231Sdim/*-
2303231Sdim * Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki
3353358Sdim * All rights reserved.
4353358Sdim *
5353358Sdim * Redistribution and use in source and binary forms, with or without
6303231Sdim * modification, are permitted provided that the following conditions
7303231Sdim * are met:
8303231Sdim * 1. Redistributions of source code must retain the above copyright
9303231Sdim *    notice, this list of conditions and the following disclaimer.
10303231Sdim * 2. Redistributions in binary form must reproduce the above copyright
11303231Sdim *    notice, this list of conditions and the following disclaimer in the
12327952Sdim *    documentation and/or other materials provided with the distribution.
13303231Sdim *
14303231Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15303231Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16303231Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17303231Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18303231Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19303231Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20303231Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21303231Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22303231Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23303231Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24303231Sdim * SUCH DAMAGE.
25303231Sdim *
26303231Sdim */
27303231Sdim
28303231Sdim#include <sys/cdefs.h>
29303231Sdim__FBSDID("$FreeBSD: releng/10.3/sys/powerpc/mpc85xx/ds1553_core.c 194632 2009-06-22 15:48:47Z raj $");
30303231Sdim
31303231Sdim#include <sys/param.h>
32327952Sdim#include <sys/systm.h>
33303231Sdim#include <sys/bus.h>
34303231Sdim#include <sys/clock.h>
35303231Sdim#include <sys/lock.h>
36303231Sdim#include <sys/mutex.h>
37303231Sdim
38303231Sdim#include <machine/bus.h>
39303231Sdim
40303231Sdim#include <powerpc/mpc85xx/ds1553_reg.h>
41303231Sdim
42303231Sdimstatic uint8_t ds1553_direct_read(device_t, bus_size_t);
43303231Sdimstatic void ds1553_direct_write(device_t, bus_size_t, uint8_t);
44303231Sdim
45303231Sdimint
46ds1553_attach(device_t dev)
47{
48	struct ds1553_softc *sc;
49	uint8_t sec, flags;
50
51	sc = device_get_softc(dev);
52
53	if (mtx_initialized(&sc->sc_mtx) == 0) {
54		device_printf(dev, "%s: mutex not initialized\n", __func__);
55		return (ENXIO);
56	}
57
58	if (sc->sc_read == NULL)
59		sc->sc_read = ds1553_direct_read;
60	if (sc->sc_write == NULL)
61		sc->sc_write = ds1553_direct_write;
62
63	sc->year_offset = POSIX_BASE_YEAR;
64
65	mtx_lock_spin(&sc->sc_mtx);
66
67	/* Turn RTC on if it was not on */
68	sec = (*sc->sc_read)(dev, DS1553_OFF_SECONDS);
69	if (sec & DS1553_BIT_OSC) {
70		sec &= ~(DS1553_BIT_OSC);
71		(*sc->sc_write)(dev, DS1553_OFF_SECONDS, sec);
72	}
73
74	/* Low-battery check */
75	flags = (*sc->sc_read)(dev, DS1553_OFF_FLAGS);
76	if (flags & DS1553_BIT_BLF)
77		device_printf(dev, "voltage-low detected.\n");
78
79	mtx_unlock_spin(&sc->sc_mtx);
80
81	return (0);
82}
83
84/*
85 * Get time of day and convert it to a struct timespec.
86 * Return 0 on success, an error number otherwise.
87 */
88int
89ds1553_gettime(device_t dev, struct timespec *ts)
90{
91	struct clocktime ct;
92	struct ds1553_softc *sc;
93	uint8_t control;
94
95	sc = device_get_softc(dev);
96
97	mtx_lock_spin(&sc->sc_mtx);
98
99	control = (*sc->sc_read)(dev, DS1553_OFF_CONTROL) | DS1553_BIT_READ;
100	(*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
101
102	ct.nsec = 0;
103	ct.sec = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_SECONDS) &
104	    DS1553_MASK_SECONDS);
105	ct.min = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_MINUTES) &
106	    DS1553_MASK_MINUTES);
107	ct.hour = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_HOURS) &
108	    DS1553_MASK_HOUR);
109	ct.dow = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_DAYOFWEEK) &
110	    DS1553_MASK_DAYOFWEEK) - 1;
111	ct.day = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_DATE) &
112	    DS1553_MASK_DATE);
113	ct.mon = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_MONTH) &
114	    DS1553_MASK_MONTH);
115	ct.year = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_YEAR));
116
117	control &= ~DS1553_BIT_READ;
118	(*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
119
120	ct.year += sc->year_offset;
121
122	mtx_unlock_spin(&sc->sc_mtx);
123
124	return (clock_ct_to_ts(&ct, ts));
125}
126
127/*
128 * Set the time of day clock based on the value of the struct timespec arg.
129 * Return 0 on success, an error number otherwise.
130 */
131int
132ds1553_settime(device_t dev, struct timespec *ts)
133{
134	struct clocktime ct;
135	struct ds1553_softc *sc;
136	uint8_t control;
137
138	sc = device_get_softc(dev);
139	bzero(&ct, sizeof(struct clocktime));
140
141	/* Accuracy is only one second. */
142	if (ts->tv_nsec >= 500000000)
143		ts->tv_sec++;
144	ts->tv_nsec = 0;
145	clock_ts_to_ct(ts, &ct);
146
147	ct.year -= sc->year_offset;
148
149	mtx_lock_spin(&sc->sc_mtx);
150
151	/* Halt updates to external registers */
152	control = (*sc->sc_read)(dev, DS1553_OFF_CONTROL) | DS1553_BIT_WRITE;
153	(*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
154
155	(*sc->sc_write)(dev, DS1553_OFF_SECONDS, TOBCD(ct.sec) &
156	    DS1553_MASK_SECONDS);
157	(*sc->sc_write)(dev, DS1553_OFF_MINUTES, TOBCD(ct.min) &
158	    DS1553_MASK_MINUTES);
159	(*sc->sc_write)(dev, DS1553_OFF_HOURS, TOBCD(ct.hour) &
160	    DS1553_MASK_HOUR);
161	(*sc->sc_write)(dev, DS1553_OFF_DAYOFWEEK, TOBCD(ct.dow + 1) &
162	    DS1553_MASK_DAYOFWEEK);
163	(*sc->sc_write)(dev, DS1553_OFF_DATE, TOBCD(ct.day) &
164	    DS1553_MASK_DATE);
165	(*sc->sc_write)(dev, DS1553_OFF_MONTH, TOBCD(ct.mon) &
166	    DS1553_MASK_MONTH);
167	(*sc->sc_write)(dev, DS1553_OFF_YEAR, TOBCD(ct.year));
168
169	/* Resume updates to external registers */
170	control &= ~DS1553_BIT_WRITE;
171	(*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
172
173	mtx_unlock_spin(&sc->sc_mtx);
174
175	return (0);
176}
177
178static uint8_t
179ds1553_direct_read(device_t dev, bus_size_t off)
180{
181	struct ds1553_softc *sc;
182
183	sc = device_get_softc(dev);
184	return (bus_space_read_1(sc->sc_bst, sc->sc_bsh, off));
185}
186
187static void
188ds1553_direct_write(device_t dev, bus_size_t off, uint8_t val)
189{
190	struct ds1553_softc *sc;
191
192	sc = device_get_softc(dev);
193	bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, val);
194}
195