pl030_rtc.c revision 1.2 1 1.2 thorpej /* $NetBSD: pl030_rtc.c,v 1.2 2001/11/23 19:36:50 thorpej Exp $ */
2 1.1 rearnsha
3 1.1 rearnsha /*
4 1.1 rearnsha * Copyright (c) 2001 ARM Ltd
5 1.1 rearnsha * All rights reserved.
6 1.1 rearnsha *
7 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
8 1.1 rearnsha * modification, are permitted provided that the following conditions
9 1.1 rearnsha * are met:
10 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
11 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
12 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
14 1.1 rearnsha * documentation and/or other materials provided with the distribution.
15 1.1 rearnsha * 3. The name of the company may not be used to endorse or promote
16 1.1 rearnsha * products derived from this software without specific prior written
17 1.1 rearnsha * permission.
18 1.1 rearnsha *
19 1.1 rearnsha * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 1.1 rearnsha * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 1.1 rearnsha * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 rearnsha * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 1.1 rearnsha * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 1.1 rearnsha * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.1 rearnsha * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 rearnsha * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 rearnsha * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 rearnsha * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 rearnsha * SUCH DAMAGE.
30 1.1 rearnsha */
31 1.1 rearnsha
32 1.1 rearnsha /* Include header files */
33 1.1 rearnsha
34 1.1 rearnsha #include <sys/types.h>
35 1.1 rearnsha #include <sys/param.h>
36 1.1 rearnsha #include <sys/systm.h>
37 1.1 rearnsha #include <sys/kernel.h>
38 1.1 rearnsha #include <sys/time.h>
39 1.1 rearnsha #include <sys/device.h>
40 1.1 rearnsha
41 1.2 thorpej #include <arm/cpufunc.h>
42 1.1 rearnsha #include <machine/intr.h>
43 1.1 rearnsha #include <evbarm/ifpga/ifpgavar.h>
44 1.1 rearnsha #include <evbarm/ifpga/ifpgamem.h>
45 1.1 rearnsha #include <evbarm/ifpga/ifpgareg.h>
46 1.1 rearnsha
47 1.1 rearnsha #define PL030_RTC_SIZE 0x14
48 1.1 rearnsha
49 1.1 rearnsha struct plrtc_softc {
50 1.1 rearnsha struct device sc_dev;
51 1.1 rearnsha bus_space_tag_t sc_iot;
52 1.1 rearnsha bus_space_handle_t sc_ioh;
53 1.1 rearnsha };
54 1.1 rearnsha
55 1.1 rearnsha static int plrtc_probe (struct device *, struct cfdata *, void *);
56 1.1 rearnsha static void plrtc_attach (struct device *, struct device *, void *);
57 1.1 rearnsha
58 1.1 rearnsha struct cfattach plrtc_ca = {
59 1.1 rearnsha sizeof(struct plrtc_softc), plrtc_probe, plrtc_attach
60 1.1 rearnsha };
61 1.1 rearnsha
62 1.1 rearnsha /* Remember our handle, since it isn't passed in by inittodr and
63 1.1 rearnsha resettodr. */
64 1.1 rearnsha static struct plrtc_softc *plrtc_sc;
65 1.1 rearnsha
66 1.1 rearnsha static int timeset = 0;
67 1.1 rearnsha
68 1.1 rearnsha static int
69 1.1 rearnsha plrtc_probe(struct device *parent, struct cfdata *cf, void *aux)
70 1.1 rearnsha {
71 1.1 rearnsha if (strcmp(cf->cf_driver->cd_name, "plrtc") == 0)
72 1.1 rearnsha return 1;
73 1.1 rearnsha return 0;
74 1.1 rearnsha }
75 1.1 rearnsha
76 1.1 rearnsha static void
77 1.1 rearnsha plrtc_attach(struct device *parent, struct device *self, void *aux)
78 1.1 rearnsha {
79 1.1 rearnsha struct ifpga_attach_args *ifa = aux;
80 1.1 rearnsha struct plrtc_softc *sc = (struct plrtc_softc *)self;
81 1.1 rearnsha
82 1.1 rearnsha sc->sc_iot = ifa->ifa_iot;
83 1.1 rearnsha if (bus_space_map(ifa->ifa_iot, ifa->ifa_addr, PL030_RTC_SIZE, 0,
84 1.1 rearnsha &sc->sc_ioh)) {
85 1.1 rearnsha printf("%s: unable to map device\n", sc->sc_dev.dv_xname);
86 1.1 rearnsha return;
87 1.1 rearnsha }
88 1.1 rearnsha
89 1.1 rearnsha plrtc_sc = sc;
90 1.1 rearnsha
91 1.1 rearnsha printf("\n");
92 1.1 rearnsha }
93 1.1 rearnsha
94 1.1 rearnsha void
95 1.1 rearnsha inittodr(time_t base)
96 1.1 rearnsha {
97 1.1 rearnsha time_t rtc_time;
98 1.1 rearnsha struct plrtc_softc *sc = plrtc_sc;
99 1.1 rearnsha
100 1.1 rearnsha if (sc == NULL)
101 1.1 rearnsha panic("RTC not attached");
102 1.1 rearnsha
103 1.1 rearnsha /* Default to the suggested time, but replace that with one from the
104 1.1 rearnsha RTC if it seems more sensible. */
105 1.1 rearnsha rtc_time = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IFPGA_RTC_DR);
106 1.1 rearnsha
107 1.1 rearnsha time.tv_usec = 0;
108 1.1 rearnsha time.tv_sec = rtc_time;
109 1.1 rearnsha
110 1.1 rearnsha timeset = 1;
111 1.1 rearnsha
112 1.1 rearnsha if (base > rtc_time) {
113 1.1 rearnsha printf("inittodr: rtc value suspect, ignoring it.\n");
114 1.1 rearnsha time.tv_usec = 0;
115 1.1 rearnsha time.tv_sec = base;
116 1.1 rearnsha return;
117 1.1 rearnsha }
118 1.1 rearnsha }
119 1.1 rearnsha
120 1.1 rearnsha void
121 1.1 rearnsha resettodr()
122 1.1 rearnsha {
123 1.1 rearnsha struct plrtc_softc *sc = plrtc_sc;
124 1.1 rearnsha
125 1.1 rearnsha /* The time hasn't been set yet, so avoid writing a dubious value
126 1.1 rearnsha to the RTC. */
127 1.1 rearnsha if (!timeset)
128 1.1 rearnsha return;
129 1.1 rearnsha
130 1.1 rearnsha bus_space_write_4(sc->sc_iot, sc->sc_ioh, IFPGA_RTC_LR, time.tv_sec);
131 1.1 rearnsha }
132