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