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 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/clock.h> 34 #include <sys/kernel.h> 35 #include <sys/lock.h> 36 #include <sys/module.h> 37 #include <sys/mutex.h> 38 #include <sys/rman.h> 39 40 #include <machine/bus.h> 41 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> 46 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) 52 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) 57 58 #define RTC_MAXIRQS 2 59 60 struct am335x_rtc_softc { 61 device_t sc_dev; 62 struct mtx sc_mtx; 63 struct resource *sc_irq_res[RTC_MAXIRQS]; 64 struct resource *sc_mem_res; 65 }; 66 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 }, 71 { -1, 0, 0 } 72 }; 73 74 static int 75 am335x_rtc_probe(device_t dev) 76 { 77 78 if (!ofw_bus_status_okay(dev)) 79 return (ENXIO); 80 if (!ofw_bus_is_compatible(dev, "ti,da830-rtc")) 81 return (ENXIO); 82 device_set_desc(dev, "AM335x RTC (power management mode)"); 83 84 return (BUS_PROBE_DEFAULT); 85 } 86 87 static int 88 am335x_rtc_attach(device_t dev) 89 { 90 int rid; 91 struct am335x_rtc_softc *sc; 92 uint32_t rev; 93 94 if (rtc_sc != NULL) 95 return (ENXIO); 96 rtc_sc = sc = device_get_softc(dev); 97 sc->sc_dev = dev; 98 rid = 0; 99 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 100 RF_ACTIVE); 101 if (!sc->sc_mem_res) { 102 device_printf(dev, "cannot allocate memory resources\n"); 103 return (ENXIO); 104 } 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"); 108 return (ENXIO); 109 } 110 RTC_LOCK_INIT(sc); 111 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); 130 131 return (0); 132 } 133 134 static int 135 am335x_rtc_detach(device_t dev) 136 { 137 struct am335x_rtc_softc *sc; 138 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); 142 if (sc->sc_mem_res) 143 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res); 144 RTC_LOCK_DESTROY(sc); 145 146 return (0); 147 } 148 149 void 150 am335x_rtc_pmic_pwr_toggle(void) 151 { 152 int timeout; 153 struct clocktime ct; 154 struct timespec ts; 155 156 /* 157 * We stop the RTC so we don't need to check the STATUS.BUSY bit 158 * before update ALARM2 registers. 159 */ 160 timeout = 10; 161 RTC_WRITE4(rtc_sc, RTC_CTRL, 0); 162 while (--timeout && RTC_READ4(rtc_sc, RTC_STATUS) & RTC_STATUS_RUN) 163 DELAY(100); 164 if (timeout == 0) { 165 device_printf(rtc_sc->sc_dev, "RTC does not stop.\n"); 166 return; 167 } 168 /* Program the ALARM2 to fire in 2 seconds. */ 169 ct.dow = 0; 170 ct.nsec = 0; 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); 179 ts.tv_sec += 2; 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); 189 /* Start count. */ 190 RTC_WRITE4(rtc_sc, RTC_CTRL, RTC_CTRL_RUN); 191 } 192 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), 197 198 DEVMETHOD_END 199 }; 200 201 static driver_t am335x_rtc_driver = { 202 "am335x_rtc", 203 am335x_rtc_methods, 204 sizeof(struct am335x_rtc_softc), 205 }; 206 207 static devclass_t am335x_rtc_devclass; 208 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); 212