mvsocrtc.c revision 1.3 1 1.3 christos /* $NetBSD: mvsocrtc.c,v 1.3 2014/11/20 16:34:25 christos Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * Redistribution and use in source and binary forms, with or without
8 1.1 matt * modification, are permitted provided that the following conditions
9 1.1 matt * are met:
10 1.1 matt * 1. Redistributions of source code must retain the above copyright
11 1.1 matt * notice, this list of conditions and the following disclaimer.
12 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 matt * notice, this list of conditions and the following disclaimer in the
14 1.1 matt * documentation and/or other materials provided with the distribution.
15 1.1 matt *
16 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
27 1.1 matt */
28 1.1 matt
29 1.1 matt /*
30 1.1 matt * Driver for real time clock unit on Marvell kirkwood and possibly other
31 1.1 matt * SoCs. Supports only the time/date keeping functions. No support for
32 1.1 matt * the rtc unit alarm / interrupt functions.
33 1.1 matt *
34 1.1 matt * Written 10/2010 by Brett Slager -- all rights assigned to the NetBSD
35 1.1 matt * Foundation.
36 1.1 matt */
37 1.1 matt
38 1.1 matt #include <sys/cdefs.h>
39 1.3 christos __KERNEL_RCSID(0, "$NetBSD: mvsocrtc.c,v 1.3 2014/11/20 16:34:25 christos Exp $");
40 1.1 matt
41 1.1 matt #include <sys/param.h>
42 1.1 matt #include <sys/systm.h>
43 1.1 matt #include <sys/device.h>
44 1.1 matt #include <sys/kernel.h>
45 1.1 matt
46 1.1 matt #include <dev/clock_subr.h>
47 1.1 matt
48 1.2 dyoung #include <sys/bus.h>
49 1.1 matt
50 1.1 matt #include <arm/marvell/mvsocrtcreg.h>
51 1.1 matt #include <dev/marvell/marvellvar.h>
52 1.1 matt
53 1.1 matt struct mvsocrtc_softc {
54 1.1 matt device_t sc_dev;
55 1.1 matt bus_space_tag_t sc_iot;
56 1.1 matt bus_space_handle_t sc_ioh;
57 1.1 matt struct todr_chip_handle sc_todr;
58 1.1 matt };
59 1.1 matt
60 1.1 matt static int mvsocrtc_match(device_t, cfdata_t, void *);
61 1.1 matt static void mvsocrtc_attach(device_t, device_t, void *);
62 1.1 matt static int mvsocrtc_todr_gettime(todr_chip_handle_t, struct clock_ymdhms *);
63 1.1 matt static int mvsocrtc_todr_settime(todr_chip_handle_t, struct clock_ymdhms *);
64 1.1 matt
65 1.1 matt CFATTACH_DECL_NEW(mvsocrtc, sizeof(struct mvsocrtc_softc), mvsocrtc_match,
66 1.1 matt mvsocrtc_attach, NULL, NULL);
67 1.1 matt
68 1.1 matt static int
69 1.1 matt mvsocrtc_match(device_t parent, cfdata_t cf, void *aux)
70 1.1 matt {
71 1.1 matt struct marvell_attach_args * const mva = aux;
72 1.1 matt
73 1.1 matt if (strcmp(mva->mva_name, cf->cf_name) != 0)
74 1.1 matt return 0;
75 1.1 matt if (mva->mva_offset == MVA_OFFSET_DEFAULT)
76 1.1 matt return 0;
77 1.1 matt
78 1.1 matt mva->mva_size = MVSOCRTC_SIZE;
79 1.1 matt return 1;
80 1.1 matt }
81 1.1 matt
82 1.1 matt static void
83 1.1 matt mvsocrtc_attach(device_t parent, device_t self, void *aux)
84 1.1 matt {
85 1.1 matt struct mvsocrtc_softc * const sc = device_private(self);
86 1.1 matt struct marvell_attach_args * const mva = aux;
87 1.1 matt
88 1.1 matt sc->sc_dev = self;
89 1.1 matt sc->sc_iot = mva->mva_iot;
90 1.1 matt
91 1.1 matt aprint_normal(": Marvell SoC Real Time Clock\n");
92 1.1 matt aprint_naive("\n");
93 1.1 matt
94 1.1 matt if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
95 1.1 matt mva->mva_size, &sc->sc_ioh)) {
96 1.1 matt aprint_error_dev(self, "failed to subregion register space\n");
97 1.1 matt return;
98 1.1 matt }
99 1.1 matt
100 1.1 matt sc->sc_todr.cookie = sc;
101 1.1 matt sc->sc_todr.todr_gettime = NULL;
102 1.1 matt sc->sc_todr.todr_settime = NULL;
103 1.1 matt sc->sc_todr.todr_gettime_ymdhms = mvsocrtc_todr_gettime;
104 1.1 matt sc->sc_todr.todr_settime_ymdhms = mvsocrtc_todr_settime;
105 1.1 matt sc->sc_todr.todr_setwen = NULL;
106 1.1 matt
107 1.1 matt todr_attach(&sc->sc_todr);
108 1.1 matt }
109 1.1 matt
110 1.1 matt static int
111 1.1 matt mvsocrtc_todr_gettime(todr_chip_handle_t ch, struct clock_ymdhms *dt)
112 1.1 matt {
113 1.1 matt struct mvsocrtc_softc * const sc = ch->cookie;
114 1.1 matt bool tried = false;
115 1.1 matt uint32_t rtcdate, rtctime;
116 1.1 matt uint8_t rtcday, rtchour, rtcmin, rtcmonth, rtcsec, rtcyear;
117 1.1 matt
118 1.1 matt again:
119 1.1 matt /* read the rtc date and time registers */
120 1.1 matt rtcdate = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSOCRTC_DATE);
121 1.1 matt rtctime = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSOCRTC_TIME);
122 1.1 matt
123 1.1 matt rtcyear = (rtcdate >> MVSOCRTC_YEAR_OFFSET) & MVSOCRTC_YEAR_MASK;
124 1.1 matt rtcmonth = (rtcdate >> MVSOCRTC_MONTH_OFFSET) & MVSOCRTC_MONTH_MASK;
125 1.1 matt rtcday = (rtcdate >> MVSOCRTC_DAY_OFFSET) & MVSOCRTC_DAY_MASK;
126 1.1 matt
127 1.1 matt rtchour = (rtctime >> MVSOCRTC_HOUR_OFFSET) & MVSOCRTC_HOUR_MASK;
128 1.1 matt rtcmin = (rtctime >> MVSOCRTC_MINUTE_OFFSET) & MVSOCRTC_MINUTE_MASK;
129 1.1 matt rtcsec = (rtctime >> MVSOCRTC_SECOND_OFFSET) & MVSOCRTC_SECOND_MASK;
130 1.1 matt
131 1.1 matt /*
132 1.1 matt * if seconds == 0, we may have read while the registers were
133 1.1 matt * updating. Read again to get consistant data.
134 1.1 matt */
135 1.1 matt if (rtcsec == 0 && !tried) {
136 1.1 matt tried = true;
137 1.1 matt goto again;
138 1.1 matt }
139 1.1 matt
140 1.1 matt /*
141 1.1 matt * Assume year "00" to be year 2000.
142 1.1 matt * XXXX this assumption will fail in 2100, but somehow I don't think
143 1.1 matt * I or the hardware will be functioning to see it.
144 1.1 matt */
145 1.3 christos dt->dt_year = bcdtobin(rtcyear) + 2000;
146 1.3 christos dt->dt_mon = bcdtobin(rtcmonth);
147 1.3 christos dt->dt_day = bcdtobin(rtcday);
148 1.3 christos dt->dt_hour = bcdtobin(rtchour);
149 1.3 christos dt->dt_min = bcdtobin(rtcmin);
150 1.3 christos dt->dt_sec = bcdtobin(rtcsec);
151 1.1 matt
152 1.1 matt return 0;
153 1.1 matt }
154 1.1 matt
155 1.1 matt static int
156 1.1 matt mvsocrtc_todr_settime(todr_chip_handle_t ch, struct clock_ymdhms *dt)
157 1.1 matt {
158 1.1 matt struct mvsocrtc_softc * const sc = ch->cookie;
159 1.1 matt uint32_t reg;
160 1.1 matt
161 1.1 matt /* compose & write time register contents */
162 1.3 christos reg = (bintobcd(dt->dt_sec) << MVSOCRTC_SECOND_OFFSET) |
163 1.3 christos (bintobcd(dt->dt_min) << MVSOCRTC_MINUTE_OFFSET) |
164 1.3 christos (bintobcd(dt->dt_hour) << MVSOCRTC_HOUR_OFFSET) |
165 1.3 christos (bintobcd(dt->dt_wday) << MVSOCRTC_WDAY_OFFSET);
166 1.1 matt
167 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSOCRTC_TIME, reg);
168 1.1 matt
169 1.1 matt /* compose & write date register contents */
170 1.3 christos reg = (bintobcd(dt->dt_day) << MVSOCRTC_DAY_OFFSET) |
171 1.3 christos (bintobcd(dt->dt_mon) << MVSOCRTC_MONTH_OFFSET) |
172 1.3 christos (bintobcd(dt->dt_year % 100) << MVSOCRTC_YEAR_OFFSET);
173 1.1 matt
174 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSOCRTC_DATE, reg);
175 1.1 matt
176 1.1 matt return 0;
177 1.1 matt }
178