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