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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/clock.h>
34 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
41 #include <machine/bus.h>
43 #include <arm/at91/at91_rtcreg.h>
49 device_t dev; /* Myself */
50 void *intrhand; /* Interrupt handle */
51 struct resource *irq_res; /* IRQ resource */
52 struct resource *mem_res; /* Memory resource */
53 struct mtx sc_mtx; /* basically a perimeter lock */
56 static inline uint32_t
57 RD4(struct at91_rtc_softc *sc, bus_size_t off)
59 return bus_read_4(sc->mem_res, off);
63 WR4(struct at91_rtc_softc *sc, bus_size_t off, uint32_t val)
65 bus_write_4(sc->mem_res, off, val);
68 #define AT91_RTC_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx)
69 #define AT91_RTC_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx)
70 #define AT91_RTC_LOCK_INIT(_sc) \
71 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
73 #define AT91_RTC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
74 #define AT91_RTC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
75 #define AT91_RTC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
77 static devclass_t at91_rtc_devclass;
79 /* bus entry points */
81 static int at91_rtc_probe(device_t dev);
82 static int at91_rtc_attach(device_t dev);
83 static int at91_rtc_detach(device_t dev);
84 static int at91_rtc_intr(void *);
87 static int at91_rtc_activate(device_t dev);
88 static void at91_rtc_deactivate(device_t dev);
91 at91_rtc_probe(device_t dev)
93 device_set_desc(dev, "RTC");
98 at91_rtc_attach(device_t dev)
100 struct at91_rtc_softc *sc = device_get_softc(dev);
104 err = at91_rtc_activate(dev);
108 AT91_RTC_LOCK_INIT(sc);
111 * Activate the interrupt, but disable all interrupts in the hardware
113 WR4(sc, RTC_IDR, 0xffffffff);
114 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC,
115 at91_rtc_intr, NULL, sc, &sc->intrhand);
117 AT91_RTC_LOCK_DESTROY(sc);
120 clock_register(dev, 1000000);
123 at91_rtc_deactivate(dev);
128 at91_rtc_detach(device_t dev)
130 return (EBUSY); /* XXX */
134 at91_rtc_activate(device_t dev)
136 struct at91_rtc_softc *sc;
139 sc = device_get_softc(dev);
141 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
143 if (sc->mem_res == NULL)
146 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
147 RF_ACTIVE | RF_SHAREABLE);
148 if (sc->irq_res == NULL)
152 at91_rtc_deactivate(dev);
157 at91_rtc_deactivate(device_t dev)
159 struct at91_rtc_softc *sc;
161 sc = device_get_softc(dev);
163 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
165 bus_generic_detach(sc->dev);
167 bus_release_resource(dev, SYS_RES_IOPORT,
168 rman_get_rid(sc->mem_res), sc->mem_res);
171 bus_release_resource(dev, SYS_RES_IRQ,
172 rman_get_rid(sc->irq_res), sc->irq_res);
178 at91_rtc_intr(void *xsc)
180 struct at91_rtc_softc *sc = xsc;
184 /* Reading the status also clears the interrupt */
185 status = RD4(sc, RTC_SR);
192 return (FILTER_HANDLED);
196 * Get the time of day clock and return it in ts.
197 * Return 0 on success, an error number otherwise.
200 at91_rtc_gettime(device_t dev, struct timespec *ts)
204 struct at91_rtc_softc *sc;
206 sc = device_get_softc(dev);
207 timr = RD4(sc, RTC_TIMR);
208 calr = RD4(sc, RTC_CALR);
210 ct.sec = RTC_TIMR_SEC(timr);
211 ct.min = RTC_TIMR_MIN(timr);
212 ct.hour = RTC_TIMR_HR(timr);
213 ct.year = RTC_CALR_CEN(calr) * 100 + RTC_CALR_YEAR(calr);
214 ct.mon = RTC_CALR_MON(calr);
215 ct.day = RTC_CALR_DAY(calr);
217 return clock_ct_to_ts(&ct, ts);
221 * Set the time of day clock based on the value of the struct timespec arg.
222 * Return 0 on success, an error number otherwise.
225 at91_rtc_settime(device_t dev, struct timespec *ts)
227 struct at91_rtc_softc *sc;
230 sc = device_get_softc(dev);
231 clock_ts_to_ct(ts, &ct);
232 WR4(sc, RTC_TIMR, RTC_TIMR_MK(ct.hour, ct.min, ct.sec));
233 WR4(sc, RTC_CALR, RTC_CALR_MK(ct.year, ct.mon, ct.day, ct.dow));
237 static device_method_t at91_rtc_methods[] = {
238 /* Device interface */
239 DEVMETHOD(device_probe, at91_rtc_probe),
240 DEVMETHOD(device_attach, at91_rtc_attach),
241 DEVMETHOD(device_detach, at91_rtc_detach),
243 /* clock interface */
244 DEVMETHOD(clock_gettime, at91_rtc_gettime),
245 DEVMETHOD(clock_settime, at91_rtc_settime),
250 static driver_t at91_rtc_driver = {
253 sizeof(struct at91_rtc_softc),
256 DRIVER_MODULE(at91_rtc, atmelarm, at91_rtc_driver, at91_rtc_devclass, 0, 0);