rtc.c revision 266152
1/*- 2 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD. 3 * All rights reserved. 4 * 5 * Developed by Semihalf. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of MARVELL nor the names of contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: stable/10/sys/arm/mv/rtc.c 266152 2014-05-15 16:11:06Z ian $"); 34#include <sys/param.h> 35#include <sys/bus.h> 36#include <sys/lock.h> 37#include <sys/time.h> 38#include <sys/clock.h> 39#include <sys/resource.h> 40#include <sys/systm.h> 41#include <sys/rman.h> 42#include <sys/kernel.h> 43#include <sys/module.h> 44 45#include <machine/bus.h> 46#include <machine/resource.h> 47 48#include <dev/ofw/ofw_bus.h> 49#include <dev/ofw/ofw_bus_subr.h> 50 51#include "clock_if.h" 52 53#define MV_RTC_TIME_REG 0x00 54#define MV_RTC_DATE_REG 0x04 55#define YEAR_BASE 2000 56 57struct mv_rtc_softc { 58 device_t dev; 59 struct resource *res[1]; 60}; 61 62static struct resource_spec res_spec[] = { 63 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 64 { -1, 0 } 65}; 66 67static int mv_rtc_probe(device_t dev); 68static int mv_rtc_attach(device_t dev); 69 70static int mv_rtc_gettime(device_t dev, struct timespec *ts); 71static int mv_rtc_settime(device_t dev, struct timespec *ts); 72 73static uint32_t mv_rtc_reg_read(struct mv_rtc_softc *sc, bus_size_t off); 74static int mv_rtc_reg_write(struct mv_rtc_softc *sc, bus_size_t off, 75 uint32_t val); 76 77static device_method_t mv_rtc_methods[] = { 78 DEVMETHOD(device_probe, mv_rtc_probe), 79 DEVMETHOD(device_attach, mv_rtc_attach), 80 81 DEVMETHOD(clock_gettime, mv_rtc_gettime), 82 DEVMETHOD(clock_settime, mv_rtc_settime), 83 84 { 0, 0 }, 85}; 86 87static driver_t mv_rtc_driver = { 88 "rtc", 89 mv_rtc_methods, 90 sizeof(struct mv_rtc_softc), 91}; 92static devclass_t mv_rtc_devclass; 93 94DRIVER_MODULE(mv_rtc, simplebus, mv_rtc_driver, mv_rtc_devclass, 0, 0); 95 96static int 97mv_rtc_probe(device_t dev) 98{ 99 100 if (!ofw_bus_status_okay(dev)) 101 return (ENXIO); 102 103 if (!ofw_bus_is_compatible(dev, "mrvl,rtc")) 104 return (ENXIO); 105 106 device_set_desc(dev, "Marvell Integrated RTC"); 107 return (BUS_PROBE_DEFAULT); 108} 109 110static int 111mv_rtc_attach(device_t dev) 112{ 113 struct mv_rtc_softc *sc; 114 115 sc = device_get_softc(dev); 116 sc->dev = dev; 117 118 clock_register(dev, 1000000); 119 120 if (bus_alloc_resources(dev, res_spec, sc->res)) { 121 device_printf(dev, "could not allocate resources\n"); 122 return (ENXIO); 123 } 124 125 return (0); 126} 127 128static int 129mv_rtc_gettime(device_t dev, struct timespec *ts) 130{ 131 struct clocktime ct; 132 struct mv_rtc_softc *sc; 133 uint32_t val; 134 135 sc = device_get_softc(dev); 136 137 val = mv_rtc_reg_read(sc, MV_RTC_TIME_REG); 138 139 ct.nsec = 0; 140 ct.sec = FROMBCD(val & 0x7f); 141 ct.min = FROMBCD((val & 0x7f00) >> 8); 142 ct.hour = FROMBCD((val & 0x3f0000) >> 16); 143 ct.dow = FROMBCD((val & 0x7000000) >> 24) - 1; 144 145 val = mv_rtc_reg_read(sc, MV_RTC_DATE_REG); 146 147 ct.day = FROMBCD(val & 0x7f); 148 ct.mon = FROMBCD((val & 0x1f00) >> 8); 149 ct.year = YEAR_BASE + FROMBCD((val & 0xff0000) >> 16); 150 151 return (clock_ct_to_ts(&ct, ts)); 152} 153 154static int 155mv_rtc_settime(device_t dev, struct timespec *ts) 156{ 157 struct clocktime ct; 158 struct mv_rtc_softc *sc; 159 uint32_t val; 160 161 sc = device_get_softc(dev); 162 163 /* Resolution: 1 sec */ 164 if (ts->tv_nsec >= 500000000) 165 ts->tv_sec++; 166 ts->tv_nsec = 0; 167 clock_ts_to_ct(ts, &ct); 168 169 val = TOBCD(ct.sec) | (TOBCD(ct.min) << 8) | 170 (TOBCD(ct.hour) << 16) | (TOBCD( ct.dow + 1) << 24); 171 mv_rtc_reg_write(sc, MV_RTC_TIME_REG, val); 172 173 val = TOBCD(ct.day) | (TOBCD(ct.mon) << 8) | 174 (TOBCD(ct.year - YEAR_BASE) << 16); 175 mv_rtc_reg_write(sc, MV_RTC_DATE_REG, val); 176 177 return (0); 178} 179 180static uint32_t 181mv_rtc_reg_read(struct mv_rtc_softc *sc, bus_size_t off) 182{ 183 184 return (bus_read_4(sc->res[0], off)); 185} 186 187static int 188mv_rtc_reg_write(struct mv_rtc_softc *sc, bus_size_t off, uint32_t val) 189{ 190 191 bus_write_4(sc->res[0], off, val); 192 return (0); 193} 194