xref: /freebsd/sys/arm/ti/am335x/am335x_rtc.c (revision a8089ea5aee578e08acab2438e82fc9a9ae50ed8)
1 /*-
2  * Copyright (c) 2015 Luiz Otavio O Souza <loos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/bus.h>
30 #include <sys/clock.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/module.h>
34 #include <sys/mutex.h>
35 #include <sys/rman.h>
36 
37 #include <machine/bus.h>
38 
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 #include <arm/ti/ti_sysc.h>
42 #include <arm/ti/am335x/am335x_rtcvar.h>
43 #include <arm/ti/am335x/am335x_rtcreg.h>
44 
45 #define	RTC_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
46 #define	RTC_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
47 #define	RTC_LOCK_INIT(_sc)	mtx_init(&(_sc)->sc_mtx, \
48     device_get_nameunit(_sc->sc_dev), "am335x_rtc", MTX_DEF)
49 #define	RTC_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_mtx)
50 
51 #define	RTC_READ4(_sc, reg)		\
52 	bus_read_4((_sc)->sc_mem_res, reg)
53 #define	RTC_WRITE4(_sc, reg, value)	\
54 	bus_write_4((_sc)->sc_mem_res, reg, value)
55 
56 #define	RTC_MAXIRQS		2
57 
58 struct am335x_rtc_softc {
59 	device_t		sc_dev;
60 	struct mtx		sc_mtx;
61 	struct resource		*sc_irq_res[RTC_MAXIRQS];
62 	struct resource		*sc_mem_res;
63 };
64 
65 static struct am335x_rtc_softc *rtc_sc = NULL;
66 static struct resource_spec am335x_rtc_irq_spec[] = {
67 	{ SYS_RES_IRQ, 0,  RF_ACTIVE },
68 	{ SYS_RES_IRQ, 1,  RF_ACTIVE },
69 	{ -1, 0,  0 }
70 };
71 
72 static int
73 am335x_rtc_probe(device_t dev)
74 {
75 
76 	if (!ofw_bus_status_okay(dev))
77 		return (ENXIO);
78 	if (!ofw_bus_is_compatible(dev, "ti,da830-rtc"))
79 		return (ENXIO);
80 	device_set_desc(dev, "AM335x RTC (power management mode)");
81 
82 	return (BUS_PROBE_DEFAULT);
83 }
84 
85 static int
86 am335x_rtc_attach(device_t dev)
87 {
88 	int rid;
89 	struct am335x_rtc_softc *sc;
90 	uint32_t rev;
91 
92 	if (rtc_sc != NULL)
93 		return (ENXIO);
94 	rtc_sc = sc = device_get_softc(dev);
95 	sc->sc_dev = dev;
96 	rid = 0;
97 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
98 	    RF_ACTIVE);
99 	if (!sc->sc_mem_res) {
100 		device_printf(dev, "cannot allocate memory resources\n");
101 		return (ENXIO);
102 	}
103 	if (bus_alloc_resources(dev, am335x_rtc_irq_spec, sc->sc_irq_res) != 0) {
104 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
105 		device_printf(dev, "cannot allocate irq resources\n");
106 		return (ENXIO);
107 	}
108 	RTC_LOCK_INIT(sc);
109 
110 	/* Enable the RTC module. */
111 	ti_sysc_clock_enable(device_get_parent(dev));
112 	rev = RTC_READ4(sc, RTC_REVISION);
113 	device_printf(dev, "AM335X RTC v%d.%d.%d\n",
114             (rev >> 8) & 0x7, (rev >> 6) & 0x3, rev & 0x3f);
115 	/* Unlock the RTC. */
116 	RTC_WRITE4(sc, RTC_KICK0R, RTC_KICK0R_PASS);
117 	RTC_WRITE4(sc, RTC_KICK1R, RTC_KICK1R_PASS);
118 	/* Stop the RTC, we don't need it right now. */
119 	RTC_WRITE4(sc, RTC_CTRL, 0);
120 	/* Disable interrupts. */
121 	RTC_WRITE4(sc, RTC_INTR, 0);
122 	/* Ack any pending interrupt. */
123 	RTC_WRITE4(sc, RTC_STATUS, RTC_STATUS_ALARM2 | RTC_STATUS_ALARM);
124 	/* Enable external clock (xtal) and 32 kHz clock. */
125 	RTC_WRITE4(sc, RTC_OSC, RTC_OSC_32KCLK_EN | RTC_OSC_32KCLK_SEL);
126 	/* Enable pmic_pwr_enable. */
127 	RTC_WRITE4(sc, RTC_PMIC, PMIC_PWR_ENABLE);
128 
129 	return (0);
130 }
131 
132 static int
133 am335x_rtc_detach(device_t dev)
134 {
135 	struct am335x_rtc_softc *sc;
136 
137 	sc = device_get_softc(dev);
138 	if (sc->sc_irq_res[0] != NULL)
139 		bus_release_resources(dev, am335x_rtc_irq_spec, sc->sc_irq_res);
140 	if (sc->sc_mem_res)
141 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
142 	RTC_LOCK_DESTROY(sc);
143 
144 	return (0);
145 }
146 
147 void
148 am335x_rtc_pmic_pwr_toggle(void)
149 {
150 	int timeout;
151 	struct clocktime ct;
152 	struct timespec ts;
153 
154 	/*
155 	 * We stop the RTC so we don't need to check the STATUS.BUSY bit
156 	 * before update ALARM2 registers.
157 	 */
158 	timeout = 10;
159 	RTC_WRITE4(rtc_sc, RTC_CTRL, 0);
160 	while (--timeout && RTC_READ4(rtc_sc, RTC_STATUS) & RTC_STATUS_RUN)
161 		DELAY(100);
162 	if (timeout == 0) {
163 		device_printf(rtc_sc->sc_dev, "RTC does not stop.\n");
164 		return;
165 	}
166 	/* Program the ALARM2 to fire in 2 seconds. */
167 	ct.dow = 0;
168 	ct.nsec = 0;
169 	ct.sec = FROMBCD(RTC_READ4(rtc_sc, RTC_SECONDS) & 0x7f);
170 	ct.min = FROMBCD(RTC_READ4(rtc_sc, RTC_MINUTES) & 0x7f);
171 	ct.hour = FROMBCD(RTC_READ4(rtc_sc, RTC_HOURS) & 0x3f);
172 	ct.day = FROMBCD(RTC_READ4(rtc_sc, RTC_DAYS) & 0x3f);
173 	ct.mon = FROMBCD(RTC_READ4(rtc_sc, RTC_MONTHS) & 0x1f);
174 	ct.year = FROMBCD(RTC_READ4(rtc_sc, RTC_YEARS) & 0xff);
175 	ct.year += POSIX_BASE_YEAR;
176 	clock_ct_to_ts(&ct, &ts);
177 	ts.tv_sec += 2;
178 	clock_ts_to_ct(&ts, &ct);
179 	RTC_WRITE4(rtc_sc, RTC_ALARM2_SECONDS, TOBCD(ct.sec));
180 	RTC_WRITE4(rtc_sc, RTC_ALARM2_MINUTES, TOBCD(ct.min));
181 	RTC_WRITE4(rtc_sc, RTC_ALARM2_HOURS, TOBCD(ct.hour));
182 	RTC_WRITE4(rtc_sc, RTC_ALARM2_DAYS, TOBCD(ct.day));
183 	RTC_WRITE4(rtc_sc, RTC_ALARM2_MONTHS, TOBCD(ct.mon));
184 	RTC_WRITE4(rtc_sc, RTC_ALARM2_YEARS, TOBCD(ct.year - POSIX_BASE_YEAR));
185 	/* Enable ALARM2 interrupt. */
186 	RTC_WRITE4(rtc_sc, RTC_INTR, RTC_INTR_ALARM2);
187 	/* Start count. */
188 	RTC_WRITE4(rtc_sc, RTC_CTRL, RTC_CTRL_RUN);
189 }
190 
191 static device_method_t am335x_rtc_methods[] = {
192 	DEVMETHOD(device_probe,		am335x_rtc_probe),
193 	DEVMETHOD(device_attach,	am335x_rtc_attach),
194 	DEVMETHOD(device_detach,	am335x_rtc_detach),
195 
196 	DEVMETHOD_END
197 };
198 
199 static driver_t am335x_rtc_driver = {
200 	"am335x_rtc",
201 	am335x_rtc_methods,
202 	sizeof(struct am335x_rtc_softc),
203 };
204 
205 DRIVER_MODULE(am335x_rtc, simplebus, am335x_rtc_driver, 0, 0);
206 MODULE_VERSION(am335x_rtc, 1);
207 MODULE_DEPEND(am335x_rtc, simplebus, 1, 1, 1);
208 MODULE_DEPEND(am335x_rtc, ti_sysc, 1, 1, 1);
209