at91_rtc.c revision 166901
1/*-
2 * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
23 */
24
25#include <sys/cdefs.h>
26__FBSDID("$FreeBSD: head/sys/arm/at91/at91_rtc.c 166901 2007-02-23 12:19:07Z piso $");
27
28#include <sys/param.h>
29#include <sys/systm.h>
30#include <sys/bus.h>
31#include <sys/clock.h>
32#include <sys/conf.h>
33#include <sys/kernel.h>
34#include <sys/lock.h>
35#include <sys/mbuf.h>
36#include <sys/malloc.h>
37#include <sys/module.h>
38#include <sys/mutex.h>
39#include <sys/rman.h>
40#include <machine/bus.h>
41
42#include <arm/at91/at91_rtcreg.h>
43
44#include "clock_if.h"
45
46struct at91_rtc_softc
47{
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 */
53};
54
55static inline uint32_t
56RD4(struct at91_rtc_softc *sc, bus_size_t off)
57{
58	return bus_read_4(sc->mem_res, off);
59}
60
61static inline void
62WR4(struct at91_rtc_softc *sc, bus_size_t off, uint32_t val)
63{
64	bus_write_4(sc->mem_res, off, val);
65}
66
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), \
71	    "rtc", MTX_SPIN)
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);
75
76static devclass_t at91_rtc_devclass;
77
78/* bus entry points */
79
80static int at91_rtc_probe(device_t dev);
81static int at91_rtc_attach(device_t dev);
82static int at91_rtc_detach(device_t dev);
83static void at91_rtc_intr(void *);
84
85/* helper routines */
86static int at91_rtc_activate(device_t dev);
87static void at91_rtc_deactivate(device_t dev);
88
89static int
90at91_rtc_probe(device_t dev)
91{
92	device_set_desc(dev, "RTC");
93	return (0);
94}
95
96static int
97at91_rtc_attach(device_t dev)
98{
99	struct at91_rtc_softc *sc = device_get_softc(dev);
100	int err;
101
102	sc->dev = dev;
103	err = at91_rtc_activate(dev);
104	if (err)
105		goto out;
106
107	AT91_RTC_LOCK_INIT(sc);
108
109	/*
110	 * Activate the interrupt, but disable all interrupts in the hardware
111	 */
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);
115	if (err) {
116		AT91_RTC_LOCK_DESTROY(sc);
117		goto out;
118	}
119	clock_register(dev, 1000000);
120out:;
121	if (err)
122		at91_rtc_deactivate(dev);
123	return (err);
124}
125
126static int
127at91_rtc_detach(device_t dev)
128{
129	return (EBUSY);	/* XXX */
130}
131
132static int
133at91_rtc_activate(device_t dev)
134{
135	struct at91_rtc_softc *sc;
136	int rid;
137
138	sc = device_get_softc(dev);
139	rid = 0;
140	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
141	    RF_ACTIVE);
142	if (sc->mem_res == NULL)
143		goto errout;
144	rid = 0;
145	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
146	    RF_ACTIVE | RF_SHAREABLE);
147	if (sc->irq_res == NULL)
148		goto errout;
149	return (0);
150errout:
151	at91_rtc_deactivate(dev);
152	return (ENOMEM);
153}
154
155static void
156at91_rtc_deactivate(device_t dev)
157{
158	struct at91_rtc_softc *sc;
159
160	sc = device_get_softc(dev);
161	if (sc->intrhand)
162		bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
163	sc->intrhand = 0;
164	bus_generic_detach(sc->dev);
165	if (sc->mem_res)
166		bus_release_resource(dev, SYS_RES_IOPORT,
167		    rman_get_rid(sc->mem_res), sc->mem_res);
168	sc->mem_res = 0;
169	if (sc->irq_res)
170		bus_release_resource(dev, SYS_RES_IRQ,
171		    rman_get_rid(sc->irq_res), sc->irq_res);
172	sc->irq_res = 0;
173	return;
174}
175
176static int
177at91_rtc_intr(void *xsc)
178{
179	struct at91_rtc_softc *sc = xsc;
180#if 0
181	uint32_t status;
182
183	/* Reading the status also clears the interrupt */
184	status = RD4(sc, RTC_SR);
185	if (status == 0)
186		return;
187	AT91_RTC_LOCK(sc);
188	AT91_RTC_UNLOCK(sc);
189#endif
190	wakeup(sc);
191	return (FILTER_HANDLED);
192}
193
194/*
195 * Get the time of day clock and return it in ts.
196 * Return 0 on success, an error number otherwise.
197 */
198static int
199at91_rtc_gettime(device_t dev, struct timespec *ts)
200{
201	struct clocktime ct;
202	uint32_t timr, calr;
203	struct at91_rtc_softc *sc;
204
205	sc = device_get_softc(dev);
206	timr = RD4(sc, RTC_TIMR);
207	calr = RD4(sc, RTC_CALR);
208	ct.nsec = 0;
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);
215	ct.dow = -1;
216	return clock_ct_to_ts(&ct, ts);
217}
218
219/*
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.
222 */
223static int
224at91_rtc_settime(device_t dev, struct timespec *ts)
225{
226	struct at91_rtc_softc *sc;
227	struct clocktime ct;
228
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));
233	return (0);
234}
235
236static 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),
241
242        /* clock interface */
243        DEVMETHOD(clock_gettime,        at91_rtc_gettime),
244        DEVMETHOD(clock_settime,        at91_rtc_settime),
245
246	{ 0, 0 }
247};
248
249static driver_t at91_rtc_driver = {
250	"at91_rtc",
251	at91_rtc_methods,
252	sizeof(struct at91_rtc_softc),
253};
254
255DRIVER_MODULE(at91_rtc, atmelarm, at91_rtc_driver, at91_rtc_devclass, 0, 0);
256