2 * Copyright (c) 2015 Luiz Otavio O Souza <loos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/clock.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
40 #include <machine/bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 #include <arm/ti/ti_prcm.h>
44 #include <arm/ti/am335x/am335x_rtcvar.h>
45 #include <arm/ti/am335x/am335x_rtcreg.h>
47 #define RTC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
48 #define RTC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
49 #define RTC_LOCK_INIT(_sc) mtx_init(&(_sc)->sc_mtx, \
50 device_get_nameunit(_sc->sc_dev), "am335x_rtc", MTX_DEF)
51 #define RTC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx)
53 #define RTC_READ4(_sc, reg) \
54 bus_read_4((_sc)->sc_mem_res, reg)
55 #define RTC_WRITE4(_sc, reg, value) \
56 bus_write_4((_sc)->sc_mem_res, reg, value)
60 struct am335x_rtc_softc {
63 struct resource *sc_irq_res[RTC_MAXIRQS];
64 struct resource *sc_mem_res;
67 static struct am335x_rtc_softc *rtc_sc = NULL;
68 static struct resource_spec am335x_rtc_irq_spec[] = {
69 { SYS_RES_IRQ, 0, RF_ACTIVE },
70 { SYS_RES_IRQ, 1, RF_ACTIVE },
75 am335x_rtc_probe(device_t dev)
78 if (!ofw_bus_status_okay(dev))
80 if (!ofw_bus_is_compatible(dev, "ti,da830-rtc"))
82 device_set_desc(dev, "AM335x RTC (power management mode)");
84 return (BUS_PROBE_DEFAULT);
88 am335x_rtc_attach(device_t dev)
91 struct am335x_rtc_softc *sc;
96 rtc_sc = sc = device_get_softc(dev);
99 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
101 if (!sc->sc_mem_res) {
102 device_printf(dev, "cannot allocate memory resources\n");
105 if (bus_alloc_resources(dev, am335x_rtc_irq_spec, sc->sc_irq_res) != 0) {
106 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
107 device_printf(dev, "cannot allocate irq resources\n");
112 /* Enable the RTC module. */
113 ti_prcm_clk_enable(RTC_CLK);
114 rev = RTC_READ4(sc, RTC_REVISION);
115 device_printf(dev, "AM335X RTC v%d.%d.%d\n",
116 (rev >> 8) & 0x7, (rev >> 6) & 0x3, rev & 0x3f);
117 /* Unlock the RTC. */
118 RTC_WRITE4(sc, RTC_KICK0R, RTC_KICK0R_PASS);
119 RTC_WRITE4(sc, RTC_KICK1R, RTC_KICK1R_PASS);
120 /* Stop the RTC, we don't need it right now. */
121 RTC_WRITE4(sc, RTC_CTRL, 0);
122 /* Disable interrupts. */
123 RTC_WRITE4(sc, RTC_INTR, 0);
124 /* Ack any pending interrupt. */
125 RTC_WRITE4(sc, RTC_STATUS, RTC_STATUS_ALARM2 | RTC_STATUS_ALARM);
126 /* Enable external clock (xtal) and 32 kHz clock. */
127 RTC_WRITE4(sc, RTC_OSC, RTC_OSC_32KCLK_EN | RTC_OSC_32KCLK_SEL);
128 /* Enable pmic_pwr_enable. */
129 RTC_WRITE4(sc, RTC_PMIC, PMIC_PWR_ENABLE);
135 am335x_rtc_detach(device_t dev)
137 struct am335x_rtc_softc *sc;
139 sc = device_get_softc(dev);
140 if (sc->sc_irq_res[0] != NULL)
141 bus_release_resources(dev, am335x_rtc_irq_spec, sc->sc_irq_res);
143 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
144 RTC_LOCK_DESTROY(sc);
150 am335x_rtc_pmic_pwr_toggle(void)
157 * We stop the RTC so we don't need to check the STATUS.BUSY bit
158 * before update ALARM2 registers.
161 RTC_WRITE4(rtc_sc, RTC_CTRL, 0);
162 while (--timeout && RTC_READ4(rtc_sc, RTC_STATUS) & RTC_STATUS_RUN)
165 device_printf(rtc_sc->sc_dev, "RTC does not stop.\n");
168 /* Program the ALARM2 to fire in 2 seconds. */
171 ct.sec = FROMBCD(RTC_READ4(rtc_sc, RTC_SECONDS) & 0x7f);
172 ct.min = FROMBCD(RTC_READ4(rtc_sc, RTC_MINUTES) & 0x7f);
173 ct.hour = FROMBCD(RTC_READ4(rtc_sc, RTC_HOURS) & 0x3f);
174 ct.day = FROMBCD(RTC_READ4(rtc_sc, RTC_DAYS) & 0x3f);
175 ct.mon = FROMBCD(RTC_READ4(rtc_sc, RTC_MONTHS) & 0x1f);
176 ct.year = FROMBCD(RTC_READ4(rtc_sc, RTC_YEARS) & 0xff);
177 ct.year += POSIX_BASE_YEAR;
178 clock_ct_to_ts(&ct, &ts);
180 clock_ts_to_ct(&ts, &ct);
181 RTC_WRITE4(rtc_sc, RTC_ALARM2_SECONDS, TOBCD(ct.sec));
182 RTC_WRITE4(rtc_sc, RTC_ALARM2_MINUTES, TOBCD(ct.min));
183 RTC_WRITE4(rtc_sc, RTC_ALARM2_HOURS, TOBCD(ct.hour));
184 RTC_WRITE4(rtc_sc, RTC_ALARM2_DAYS, TOBCD(ct.day));
185 RTC_WRITE4(rtc_sc, RTC_ALARM2_MONTHS, TOBCD(ct.mon));
186 RTC_WRITE4(rtc_sc, RTC_ALARM2_YEARS, TOBCD(ct.year - POSIX_BASE_YEAR));
187 /* Enable ALARM2 interrupt. */
188 RTC_WRITE4(rtc_sc, RTC_INTR, RTC_INTR_ALARM2);
190 RTC_WRITE4(rtc_sc, RTC_CTRL, RTC_CTRL_RUN);
193 static device_method_t am335x_rtc_methods[] = {
194 DEVMETHOD(device_probe, am335x_rtc_probe),
195 DEVMETHOD(device_attach, am335x_rtc_attach),
196 DEVMETHOD(device_detach, am335x_rtc_detach),
201 static driver_t am335x_rtc_driver = {
204 sizeof(struct am335x_rtc_softc),
207 static devclass_t am335x_rtc_devclass;
209 DRIVER_MODULE(am335x_rtc, simplebus, am335x_rtc_driver, am335x_rtc_devclass, 0, 0);
210 MODULE_VERSION(am335x_rtc, 1);
211 MODULE_DEPEND(am335x_rtc, simplebus, 1, 1, 1);