as3722_rtc.c revision 303975
1236769Sobrien/*- 2236769Sobrien * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org> 3236769Sobrien * All rights reserved. 4236769Sobrien * 5236769Sobrien * Redistribution and use in source and binary forms, with or without 6236769Sobrien * modification, are permitted provided that the following conditions 7236769Sobrien * are met: 8236769Sobrien * 1. Redistributions of source code must retain the above copyright 9236769Sobrien * notice, this list of conditions and the following disclaimer. 10236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 11236769Sobrien * notice, this list of conditions and the following disclaimer in the 12236769Sobrien * documentation and/or other materials provided with the distribution. 13236769Sobrien * 14236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17236769Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24236769Sobrien * SUCH DAMAGE. 25236769Sobrien */ 26236769Sobrien 27236769Sobrien#include <sys/cdefs.h> 28236769Sobrien__FBSDID("$FreeBSD: releng/11.0/sys/arm/nvidia/as3722_rtc.c 296936 2016-03-16 13:01:48Z mmel $"); 29236769Sobrien 30236769Sobrien#include <sys/param.h> 31236769Sobrien#include <sys/systm.h> 32236769Sobrien#include <sys/bus.h> 33236769Sobrien#include <sys/clock.h> 34236769Sobrien#include <sys/kernel.h> 35236769Sobrien 36236769Sobrien#include <dev/ofw/ofw_bus.h> 37236769Sobrien 38236769Sobrien#include "clock_if.h" 39236769Sobrien#include "as3722.h" 40236769Sobrien 41236769Sobrien#define AS3722_RTC_START_YEAR 2000 42236769Sobrien 43236769Sobrienint 44236769Sobrienas3722_rtc_gettime(device_t dev, struct timespec *ts) 45236769Sobrien{ 46236769Sobrien struct as3722_softc *sc; 47236769Sobrien struct clocktime ct; 48236769Sobrien uint8_t buf[6]; 49236769Sobrien int rv; 50236769Sobrien 51236769Sobrien sc = device_get_softc(dev); 52236769Sobrien 53236769Sobrien rv = as3722_read_buf(sc, AS3722_RTC_SECOND, buf, 6); 54236769Sobrien if (rv != 0) { 55236769Sobrien device_printf(sc->dev, "Failed to read RTC data\n"); 56236769Sobrien return (rv); 57236769Sobrien } 58236769Sobrien ct.nsec = 0; 59236769Sobrien ct.sec = bcd2bin(buf[0] & 0x7F); 60236769Sobrien ct.min = bcd2bin(buf[1] & 0x7F); 61236769Sobrien ct.hour = bcd2bin(buf[2] & 0x3F); 62236769Sobrien ct.day = bcd2bin(buf[3] & 0x3F); 63236769Sobrien ct.mon = bcd2bin(buf[4] & 0x1F); 64236769Sobrien ct.year = bcd2bin(buf[5] & 0x7F) + AS3722_RTC_START_YEAR; 65236769Sobrien ct.dow = -1; 66236769Sobrien 67236769Sobrien return clock_ct_to_ts(&ct, ts); 68236769Sobrien} 69236769Sobrien 70236769Sobrienint 71236769Sobrienas3722_rtc_settime(device_t dev, struct timespec *ts) 72236769Sobrien{ 73236769Sobrien struct as3722_softc *sc; 74236769Sobrien struct clocktime ct; 75236769Sobrien uint8_t buf[6]; 76236769Sobrien int rv; 77236769Sobrien 78236769Sobrien sc = device_get_softc(dev); 79236769Sobrien clock_ts_to_ct(ts, &ct); 80 81 if (ct.year < AS3722_RTC_START_YEAR) 82 return (EINVAL); 83 84 buf[0] = bin2bcd(ct.sec); 85 buf[1] = bin2bcd(ct.min); 86 buf[2] = bin2bcd(ct.hour); 87 buf[3] = bin2bcd(ct.day); 88 buf[4] = bin2bcd(ct.mon); 89 buf[5] = bin2bcd(ct.year - AS3722_RTC_START_YEAR); 90 91 rv = as3722_write_buf(sc, AS3722_RTC_SECOND, buf, 6); 92 if (rv != 0) { 93 device_printf(sc->dev, "Failed to write RTC data\n"); 94 return (rv); 95 } 96 return (0); 97} 98 99int 100as3722_rtc_attach(struct as3722_softc *sc, phandle_t node) 101{ 102 int rv; 103 104 /* Enable RTC, set 24 hours mode and alarms */ 105 rv = RM1(sc, AS3722_RTC_CONTROL, 106 AS3722_RTC_ON | AS3722_RTC_ALARM_WAKEUP_EN | AS3722_RTC_AM_PM_MODE, 107 AS3722_RTC_ON | AS3722_RTC_ALARM_WAKEUP_EN); 108 if (rv < 0) { 109 device_printf(sc->dev, "Failed to initialize RTC controller\n"); 110 return (ENXIO); 111 } 112 clock_register(sc->dev, 1000000); 113 114 return (0); 115} 116