12566c526SLuiz Otavio O Souza /*-
22566c526SLuiz Otavio O Souza * Copyright (c) 2015 Luiz Otavio O Souza <loos@FreeBSD.org>
32566c526SLuiz Otavio O Souza * All rights reserved.
42566c526SLuiz Otavio O Souza *
52566c526SLuiz Otavio O Souza * Redistribution and use in source and binary forms, with or without
62566c526SLuiz Otavio O Souza * modification, are permitted provided that the following conditions
72566c526SLuiz Otavio O Souza * are met:
82566c526SLuiz Otavio O Souza * 1. Redistributions of source code must retain the above copyright
92566c526SLuiz Otavio O Souza * notice, this list of conditions and the following disclaimer.
102566c526SLuiz Otavio O Souza * 2. Redistributions in binary form must reproduce the above copyright
112566c526SLuiz Otavio O Souza * notice, this list of conditions and the following disclaimer in the
122566c526SLuiz Otavio O Souza * documentation and/or other materials provided with the distribution.
132566c526SLuiz Otavio O Souza *
142566c526SLuiz Otavio O Souza * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152566c526SLuiz Otavio O Souza * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162566c526SLuiz Otavio O Souza * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172566c526SLuiz Otavio O Souza * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182566c526SLuiz Otavio O Souza * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192566c526SLuiz Otavio O Souza * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202566c526SLuiz Otavio O Souza * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212566c526SLuiz Otavio O Souza * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222566c526SLuiz Otavio O Souza * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232566c526SLuiz Otavio O Souza * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242566c526SLuiz Otavio O Souza * SUCH DAMAGE.
252566c526SLuiz Otavio O Souza */
262566c526SLuiz Otavio O Souza
272566c526SLuiz Otavio O Souza #include <sys/param.h>
282566c526SLuiz Otavio O Souza #include <sys/systm.h>
292566c526SLuiz Otavio O Souza #include <sys/bus.h>
302566c526SLuiz Otavio O Souza #include <sys/clock.h>
312566c526SLuiz Otavio O Souza #include <sys/kernel.h>
322566c526SLuiz Otavio O Souza #include <sys/lock.h>
332566c526SLuiz Otavio O Souza #include <sys/module.h>
342566c526SLuiz Otavio O Souza #include <sys/mutex.h>
352566c526SLuiz Otavio O Souza #include <sys/rman.h>
362566c526SLuiz Otavio O Souza
372566c526SLuiz Otavio O Souza #include <machine/bus.h>
382566c526SLuiz Otavio O Souza
390050ea24SMichal Meloun #include <dev/ofw/ofw_bus.h>
402566c526SLuiz Otavio O Souza #include <dev/ofw/ofw_bus_subr.h>
410050ea24SMichal Meloun #include <arm/ti/ti_sysc.h>
422566c526SLuiz Otavio O Souza #include <arm/ti/am335x/am335x_rtcvar.h>
432566c526SLuiz Otavio O Souza #include <arm/ti/am335x/am335x_rtcreg.h>
442566c526SLuiz Otavio O Souza
452566c526SLuiz Otavio O Souza #define RTC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
462566c526SLuiz Otavio O Souza #define RTC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
472566c526SLuiz Otavio O Souza #define RTC_LOCK_INIT(_sc) mtx_init(&(_sc)->sc_mtx, \
482566c526SLuiz Otavio O Souza device_get_nameunit(_sc->sc_dev), "am335x_rtc", MTX_DEF)
492566c526SLuiz Otavio O Souza #define RTC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx)
502566c526SLuiz Otavio O Souza
512566c526SLuiz Otavio O Souza #define RTC_READ4(_sc, reg) \
522566c526SLuiz Otavio O Souza bus_read_4((_sc)->sc_mem_res, reg)
532566c526SLuiz Otavio O Souza #define RTC_WRITE4(_sc, reg, value) \
542566c526SLuiz Otavio O Souza bus_write_4((_sc)->sc_mem_res, reg, value)
552566c526SLuiz Otavio O Souza
562566c526SLuiz Otavio O Souza #define RTC_MAXIRQS 2
572566c526SLuiz Otavio O Souza
582566c526SLuiz Otavio O Souza struct am335x_rtc_softc {
592566c526SLuiz Otavio O Souza device_t sc_dev;
602566c526SLuiz Otavio O Souza struct mtx sc_mtx;
612566c526SLuiz Otavio O Souza struct resource *sc_irq_res[RTC_MAXIRQS];
622566c526SLuiz Otavio O Souza struct resource *sc_mem_res;
632566c526SLuiz Otavio O Souza };
642566c526SLuiz Otavio O Souza
652566c526SLuiz Otavio O Souza static struct am335x_rtc_softc *rtc_sc = NULL;
662566c526SLuiz Otavio O Souza static struct resource_spec am335x_rtc_irq_spec[] = {
672566c526SLuiz Otavio O Souza { SYS_RES_IRQ, 0, RF_ACTIVE },
682566c526SLuiz Otavio O Souza { SYS_RES_IRQ, 1, RF_ACTIVE },
692566c526SLuiz Otavio O Souza { -1, 0, 0 }
702566c526SLuiz Otavio O Souza };
712566c526SLuiz Otavio O Souza
722566c526SLuiz Otavio O Souza static int
am335x_rtc_probe(device_t dev)732566c526SLuiz Otavio O Souza am335x_rtc_probe(device_t dev)
742566c526SLuiz Otavio O Souza {
752566c526SLuiz Otavio O Souza
762566c526SLuiz Otavio O Souza if (!ofw_bus_status_okay(dev))
772566c526SLuiz Otavio O Souza return (ENXIO);
782566c526SLuiz Otavio O Souza if (!ofw_bus_is_compatible(dev, "ti,da830-rtc"))
792566c526SLuiz Otavio O Souza return (ENXIO);
802566c526SLuiz Otavio O Souza device_set_desc(dev, "AM335x RTC (power management mode)");
812566c526SLuiz Otavio O Souza
822566c526SLuiz Otavio O Souza return (BUS_PROBE_DEFAULT);
832566c526SLuiz Otavio O Souza }
842566c526SLuiz Otavio O Souza
852566c526SLuiz Otavio O Souza static int
am335x_rtc_attach(device_t dev)862566c526SLuiz Otavio O Souza am335x_rtc_attach(device_t dev)
872566c526SLuiz Otavio O Souza {
882566c526SLuiz Otavio O Souza int rid;
892566c526SLuiz Otavio O Souza struct am335x_rtc_softc *sc;
902566c526SLuiz Otavio O Souza uint32_t rev;
912566c526SLuiz Otavio O Souza
922566c526SLuiz Otavio O Souza if (rtc_sc != NULL)
932566c526SLuiz Otavio O Souza return (ENXIO);
942566c526SLuiz Otavio O Souza rtc_sc = sc = device_get_softc(dev);
952566c526SLuiz Otavio O Souza sc->sc_dev = dev;
962566c526SLuiz Otavio O Souza rid = 0;
972566c526SLuiz Otavio O Souza sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
982566c526SLuiz Otavio O Souza RF_ACTIVE);
992566c526SLuiz Otavio O Souza if (!sc->sc_mem_res) {
1002566c526SLuiz Otavio O Souza device_printf(dev, "cannot allocate memory resources\n");
1012566c526SLuiz Otavio O Souza return (ENXIO);
1022566c526SLuiz Otavio O Souza }
1032566c526SLuiz Otavio O Souza if (bus_alloc_resources(dev, am335x_rtc_irq_spec, sc->sc_irq_res) != 0) {
1042566c526SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
1052566c526SLuiz Otavio O Souza device_printf(dev, "cannot allocate irq resources\n");
1062566c526SLuiz Otavio O Souza return (ENXIO);
1072566c526SLuiz Otavio O Souza }
1082566c526SLuiz Otavio O Souza RTC_LOCK_INIT(sc);
1092566c526SLuiz Otavio O Souza
1102566c526SLuiz Otavio O Souza /* Enable the RTC module. */
1110050ea24SMichal Meloun ti_sysc_clock_enable(device_get_parent(dev));
1122566c526SLuiz Otavio O Souza rev = RTC_READ4(sc, RTC_REVISION);
1132566c526SLuiz Otavio O Souza device_printf(dev, "AM335X RTC v%d.%d.%d\n",
1142566c526SLuiz Otavio O Souza (rev >> 8) & 0x7, (rev >> 6) & 0x3, rev & 0x3f);
1152566c526SLuiz Otavio O Souza /* Unlock the RTC. */
1162566c526SLuiz Otavio O Souza RTC_WRITE4(sc, RTC_KICK0R, RTC_KICK0R_PASS);
1172566c526SLuiz Otavio O Souza RTC_WRITE4(sc, RTC_KICK1R, RTC_KICK1R_PASS);
1182566c526SLuiz Otavio O Souza /* Stop the RTC, we don't need it right now. */
1192566c526SLuiz Otavio O Souza RTC_WRITE4(sc, RTC_CTRL, 0);
1202566c526SLuiz Otavio O Souza /* Disable interrupts. */
1212566c526SLuiz Otavio O Souza RTC_WRITE4(sc, RTC_INTR, 0);
1222566c526SLuiz Otavio O Souza /* Ack any pending interrupt. */
1232566c526SLuiz Otavio O Souza RTC_WRITE4(sc, RTC_STATUS, RTC_STATUS_ALARM2 | RTC_STATUS_ALARM);
1242566c526SLuiz Otavio O Souza /* Enable external clock (xtal) and 32 kHz clock. */
1252566c526SLuiz Otavio O Souza RTC_WRITE4(sc, RTC_OSC, RTC_OSC_32KCLK_EN | RTC_OSC_32KCLK_SEL);
1262566c526SLuiz Otavio O Souza /* Enable pmic_pwr_enable. */
1272566c526SLuiz Otavio O Souza RTC_WRITE4(sc, RTC_PMIC, PMIC_PWR_ENABLE);
1282566c526SLuiz Otavio O Souza
1292566c526SLuiz Otavio O Souza return (0);
1302566c526SLuiz Otavio O Souza }
1312566c526SLuiz Otavio O Souza
1322566c526SLuiz Otavio O Souza static int
am335x_rtc_detach(device_t dev)1332566c526SLuiz Otavio O Souza am335x_rtc_detach(device_t dev)
1342566c526SLuiz Otavio O Souza {
1352566c526SLuiz Otavio O Souza struct am335x_rtc_softc *sc;
1362566c526SLuiz Otavio O Souza
1372566c526SLuiz Otavio O Souza sc = device_get_softc(dev);
1389e950417SDimitry Andric if (sc->sc_irq_res[0] != NULL)
1392566c526SLuiz Otavio O Souza bus_release_resources(dev, am335x_rtc_irq_spec, sc->sc_irq_res);
1402566c526SLuiz Otavio O Souza if (sc->sc_mem_res)
1412566c526SLuiz Otavio O Souza bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
1422566c526SLuiz Otavio O Souza RTC_LOCK_DESTROY(sc);
1432566c526SLuiz Otavio O Souza
1442566c526SLuiz Otavio O Souza return (0);
1452566c526SLuiz Otavio O Souza }
1462566c526SLuiz Otavio O Souza
1472566c526SLuiz Otavio O Souza void
am335x_rtc_pmic_pwr_toggle(void)1482566c526SLuiz Otavio O Souza am335x_rtc_pmic_pwr_toggle(void)
1492566c526SLuiz Otavio O Souza {
1502566c526SLuiz Otavio O Souza int timeout;
1512566c526SLuiz Otavio O Souza struct clocktime ct;
1522566c526SLuiz Otavio O Souza struct timespec ts;
1532566c526SLuiz Otavio O Souza
1542566c526SLuiz Otavio O Souza /*
1552566c526SLuiz Otavio O Souza * We stop the RTC so we don't need to check the STATUS.BUSY bit
1562566c526SLuiz Otavio O Souza * before update ALARM2 registers.
1572566c526SLuiz Otavio O Souza */
1582566c526SLuiz Otavio O Souza timeout = 10;
1592566c526SLuiz Otavio O Souza RTC_WRITE4(rtc_sc, RTC_CTRL, 0);
1602566c526SLuiz Otavio O Souza while (--timeout && RTC_READ4(rtc_sc, RTC_STATUS) & RTC_STATUS_RUN)
1612566c526SLuiz Otavio O Souza DELAY(100);
1622566c526SLuiz Otavio O Souza if (timeout == 0) {
1632566c526SLuiz Otavio O Souza device_printf(rtc_sc->sc_dev, "RTC does not stop.\n");
1642566c526SLuiz Otavio O Souza return;
1652566c526SLuiz Otavio O Souza }
1662566c526SLuiz Otavio O Souza /* Program the ALARM2 to fire in 2 seconds. */
1672566c526SLuiz Otavio O Souza ct.dow = 0;
1682566c526SLuiz Otavio O Souza ct.nsec = 0;
1692566c526SLuiz Otavio O Souza ct.sec = FROMBCD(RTC_READ4(rtc_sc, RTC_SECONDS) & 0x7f);
1702566c526SLuiz Otavio O Souza ct.min = FROMBCD(RTC_READ4(rtc_sc, RTC_MINUTES) & 0x7f);
1712566c526SLuiz Otavio O Souza ct.hour = FROMBCD(RTC_READ4(rtc_sc, RTC_HOURS) & 0x3f);
1722566c526SLuiz Otavio O Souza ct.day = FROMBCD(RTC_READ4(rtc_sc, RTC_DAYS) & 0x3f);
1732566c526SLuiz Otavio O Souza ct.mon = FROMBCD(RTC_READ4(rtc_sc, RTC_MONTHS) & 0x1f);
1742566c526SLuiz Otavio O Souza ct.year = FROMBCD(RTC_READ4(rtc_sc, RTC_YEARS) & 0xff);
1752566c526SLuiz Otavio O Souza ct.year += POSIX_BASE_YEAR;
1762566c526SLuiz Otavio O Souza clock_ct_to_ts(&ct, &ts);
1772566c526SLuiz Otavio O Souza ts.tv_sec += 2;
1782566c526SLuiz Otavio O Souza clock_ts_to_ct(&ts, &ct);
1792566c526SLuiz Otavio O Souza RTC_WRITE4(rtc_sc, RTC_ALARM2_SECONDS, TOBCD(ct.sec));
1802566c526SLuiz Otavio O Souza RTC_WRITE4(rtc_sc, RTC_ALARM2_MINUTES, TOBCD(ct.min));
1812566c526SLuiz Otavio O Souza RTC_WRITE4(rtc_sc, RTC_ALARM2_HOURS, TOBCD(ct.hour));
1822566c526SLuiz Otavio O Souza RTC_WRITE4(rtc_sc, RTC_ALARM2_DAYS, TOBCD(ct.day));
1832566c526SLuiz Otavio O Souza RTC_WRITE4(rtc_sc, RTC_ALARM2_MONTHS, TOBCD(ct.mon));
1842566c526SLuiz Otavio O Souza RTC_WRITE4(rtc_sc, RTC_ALARM2_YEARS, TOBCD(ct.year - POSIX_BASE_YEAR));
1852566c526SLuiz Otavio O Souza /* Enable ALARM2 interrupt. */
1862566c526SLuiz Otavio O Souza RTC_WRITE4(rtc_sc, RTC_INTR, RTC_INTR_ALARM2);
1872566c526SLuiz Otavio O Souza /* Start count. */
1882566c526SLuiz Otavio O Souza RTC_WRITE4(rtc_sc, RTC_CTRL, RTC_CTRL_RUN);
1892566c526SLuiz Otavio O Souza }
1902566c526SLuiz Otavio O Souza
1912566c526SLuiz Otavio O Souza static device_method_t am335x_rtc_methods[] = {
1922566c526SLuiz Otavio O Souza DEVMETHOD(device_probe, am335x_rtc_probe),
1932566c526SLuiz Otavio O Souza DEVMETHOD(device_attach, am335x_rtc_attach),
1942566c526SLuiz Otavio O Souza DEVMETHOD(device_detach, am335x_rtc_detach),
1952566c526SLuiz Otavio O Souza
1962566c526SLuiz Otavio O Souza DEVMETHOD_END
1972566c526SLuiz Otavio O Souza };
1982566c526SLuiz Otavio O Souza
1992566c526SLuiz Otavio O Souza static driver_t am335x_rtc_driver = {
2002566c526SLuiz Otavio O Souza "am335x_rtc",
2012566c526SLuiz Otavio O Souza am335x_rtc_methods,
2022566c526SLuiz Otavio O Souza sizeof(struct am335x_rtc_softc),
2032566c526SLuiz Otavio O Souza };
2042566c526SLuiz Otavio O Souza
205*8537e671SJohn Baldwin DRIVER_MODULE(am335x_rtc, simplebus, am335x_rtc_driver, 0, 0);
2062566c526SLuiz Otavio O Souza MODULE_VERSION(am335x_rtc, 1);
2072566c526SLuiz Otavio O Souza MODULE_DEPEND(am335x_rtc, simplebus, 1, 1, 1);
2080050ea24SMichal Meloun MODULE_DEPEND(am335x_rtc, ti_sysc, 1, 1, 1);
209