2 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/clock.h>
33 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
40 #include <machine/bus.h>
42 #include <arm/at91/at91_rtcreg.h>
48 device_t dev; /* Myself */
49 void *intrhand; /* Interrupt handle */
50 struct resource *irq_res; /* IRQ resource */
51 struct resource *mem_res; /* Memory resource */
52 struct mtx sc_mtx; /* basically a perimeter lock */
55 static inline uint32_t
56 RD4(struct at91_rtc_softc *sc, bus_size_t off)
58 return bus_read_4(sc->mem_res, off);
62 WR4(struct at91_rtc_softc *sc, bus_size_t off, uint32_t val)
64 bus_write_4(sc->mem_res, off, val);
67 #define AT91_RTC_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx)
68 #define AT91_RTC_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx)
69 #define AT91_RTC_LOCK_INIT(_sc) \
70 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
72 #define AT91_RTC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
73 #define AT91_RTC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
74 #define AT91_RTC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
76 static devclass_t at91_rtc_devclass;
78 /* bus entry points */
80 static int at91_rtc_probe(device_t dev);
81 static int at91_rtc_attach(device_t dev);
82 static int at91_rtc_detach(device_t dev);
83 static int at91_rtc_intr(void *);
86 static int at91_rtc_activate(device_t dev);
87 static void at91_rtc_deactivate(device_t dev);
90 at91_rtc_probe(device_t dev)
92 device_set_desc(dev, "RTC");
97 at91_rtc_attach(device_t dev)
99 struct at91_rtc_softc *sc = device_get_softc(dev);
103 err = at91_rtc_activate(dev);
107 AT91_RTC_LOCK_INIT(sc);
110 * Activate the interrupt, but disable all interrupts in the hardware
112 WR4(sc, RTC_IDR, 0xffffffff);
113 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC,
114 at91_rtc_intr, NULL, sc, &sc->intrhand);
116 AT91_RTC_LOCK_DESTROY(sc);
119 clock_register(dev, 1000000);
122 at91_rtc_deactivate(dev);
127 at91_rtc_detach(device_t dev)
129 return (EBUSY); /* XXX */
133 at91_rtc_activate(device_t dev)
135 struct at91_rtc_softc *sc;
138 sc = device_get_softc(dev);
140 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
142 if (sc->mem_res == NULL)
145 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
146 RF_ACTIVE | RF_SHAREABLE);
147 if (sc->irq_res == NULL)
151 at91_rtc_deactivate(dev);
156 at91_rtc_deactivate(device_t dev)
158 struct at91_rtc_softc *sc;
160 sc = device_get_softc(dev);
162 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
164 bus_generic_detach(sc->dev);
166 bus_release_resource(dev, SYS_RES_IOPORT,
167 rman_get_rid(sc->mem_res), sc->mem_res);
170 bus_release_resource(dev, SYS_RES_IRQ,
171 rman_get_rid(sc->irq_res), sc->irq_res);
177 at91_rtc_intr(void *xsc)
179 struct at91_rtc_softc *sc = xsc;
183 /* Reading the status also clears the interrupt */
184 status = RD4(sc, RTC_SR);
191 return (FILTER_HANDLED);
195 * Get the time of day clock and return it in ts.
196 * Return 0 on success, an error number otherwise.
199 at91_rtc_gettime(device_t dev, struct timespec *ts)
203 struct at91_rtc_softc *sc;
205 sc = device_get_softc(dev);
206 timr = RD4(sc, RTC_TIMR);
207 calr = RD4(sc, RTC_CALR);
209 ct.sec = RTC_TIMR_SEC(timr);
210 ct.min = RTC_TIMR_MIN(timr);
211 ct.hour = RTC_TIMR_HR(timr);
212 ct.year = RTC_CALR_CEN(calr) * 100 + RTC_CALR_YEAR(calr);
213 ct.mon = RTC_CALR_MON(calr);
214 ct.day = RTC_CALR_DAY(calr);
216 return clock_ct_to_ts(&ct, ts);
220 * Set the time of day clock based on the value of the struct timespec arg.
221 * Return 0 on success, an error number otherwise.
224 at91_rtc_settime(device_t dev, struct timespec *ts)
226 struct at91_rtc_softc *sc;
229 sc = device_get_softc(dev);
230 clock_ts_to_ct(ts, &ct);
231 WR4(sc, RTC_TIMR, RTC_TIMR_MK(ct.hour, ct.min, ct.sec));
232 WR4(sc, RTC_CALR, RTC_CALR_MK(ct.year, ct.mon, ct.day, ct.dow));
236 static device_method_t at91_rtc_methods[] = {
237 /* Device interface */
238 DEVMETHOD(device_probe, at91_rtc_probe),
239 DEVMETHOD(device_attach, at91_rtc_attach),
240 DEVMETHOD(device_detach, at91_rtc_detach),
242 /* clock interface */
243 DEVMETHOD(clock_gettime, at91_rtc_gettime),
244 DEVMETHOD(clock_settime, at91_rtc_settime),
249 static driver_t at91_rtc_driver = {
252 sizeof(struct at91_rtc_softc),
255 DRIVER_MODULE(at91_rtc, atmelarm, at91_rtc_driver, at91_rtc_devclass, 0, 0);