Date: Sat, 26 May 2012 08:58:29 +0000 (UTC) From: Marius Strobl <marius@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org Subject: svn commit: r236079 - in stable/8/sys: conf dev/iicbus Message-ID: <201205260858.q4Q8wTlx064222@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: marius Date: Sat May 26 08:58:29 2012 New Revision: 236079 URL: http://svn.freebsd.org/changeset/base/236079 Log: MFC: r234248 Add a driver for the NXP (Philips) PCF8563 RTC. Obtained from: NetBSD (pcf8563reg.h) Added: stable/8/sys/dev/iicbus/pcf8563.c - copied unchanged from r234248, head/sys/dev/iicbus/pcf8563.c stable/8/sys/dev/iicbus/pcf8563reg.h - copied unchanged from r234248, head/sys/dev/iicbus/pcf8563reg.h Modified: stable/8/sys/conf/files Directory Properties: stable/8/sys/ (props changed) stable/8/sys/amd64/include/xen/ (props changed) stable/8/sys/boot/ (props changed) stable/8/sys/cddl/contrib/opensolaris/ (props changed) stable/8/sys/contrib/dev/acpica/ (props changed) stable/8/sys/contrib/pf/ (props changed) stable/8/sys/dev/e1000/ (props changed) Modified: stable/8/sys/conf/files ============================================================================== --- stable/8/sys/conf/files Sat May 26 08:58:27 2012 (r236078) +++ stable/8/sys/conf/files Sat May 26 08:58:29 2012 (r236079) @@ -1007,6 +1007,7 @@ dev/iicbus/iicbus_if.m optional iicbus dev/iicbus/iiconf.c optional iicbus dev/iicbus/iicsmb.c optional iicsmb \ dependency "iicbus_if.h" +dev/iicbus/pcf8563.c optional pcf8563 dev/iir/iir.c optional iir dev/iir/iir_ctrl.c optional iir dev/iir/iir_pci.c optional iir pci Copied: stable/8/sys/dev/iicbus/pcf8563.c (from r234248, head/sys/dev/iicbus/pcf8563.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/8/sys/dev/iicbus/pcf8563.c Sat May 26 08:58:29 2012 (r236079, copy of r234248, head/sys/dev/iicbus/pcf8563.c) @@ -0,0 +1,202 @@ +/*- + * Copyright (c) 2012 Marius Strobl <marius@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +/* + * Driver for NXP PCF8563 real-time clock/calendar + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/bus.h> +#include <sys/clock.h> +#include <sys/kernel.h> +#include <sys/module.h> + +#include <dev/iicbus/iicbus.h> +#include <dev/iicbus/iiconf.h> +#include <dev/iicbus/pcf8563reg.h> + +#include "clock_if.h" +#include "iicbus_if.h" + +#define PCF8563_NCLOCKREGS (PCF8563_R_YEAR - PCF8563_R_CS1 + 1) + +struct pcf8563_softc { + uint32_t sc_flags; +#define PCF8563_CPOL (1 << 0) /* PCF8563_R_MONTH_C means 19xx */ + uint16_t sc_addr; /* PCF8563 slave address */ + uint16_t sc_year0; /* TOD clock year 0 */ +}; + +static device_attach_t pcf8563_attach; +static device_probe_t pcf8563_probe; +static clock_gettime_t pcf8563_gettime; +static clock_settime_t pcf8563_settime; + +static int +pcf8563_probe(device_t dev) +{ + + device_set_desc(dev, "NXP PCF8563 RTC"); + return (BUS_PROBE_NOWILDCARD); +} + +static int +pcf8563_attach(device_t dev) +{ + uint8_t reg = PCF8563_R_SECOND, val; + struct iic_msg msgs[] = { + { 0, IIC_M_WR, sizeof(reg), ® }, + { 0, IIC_M_RD, sizeof(val), &val } + }; + struct pcf8563_softc *sc; + int error; + + sc = device_get_softc(dev); + sc->sc_addr = iicbus_get_addr(dev); + if (sc->sc_addr == 0) + sc->sc_addr = PCF8563_ADDR; + + msgs[0].slave = msgs[1].slave = sc->sc_addr; + error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) / + sizeof(*msgs)); + if (error != 0) { + device_printf(dev, "%s: cannot read RTC\n", __func__); + return (error); + } + if ((val & PCF8563_R_SECOND_VL) != 0) { + device_printf(dev, "%s: battery low\n", __func__); + return (ENXIO); + } + + sc->sc_year0 = 1900; + clock_register(dev, 1000000); /* 1 second resolution */ + return (0); +} + +static int +pcf8563_gettime(device_t dev, struct timespec *ts) +{ + struct clocktime ct; + uint8_t reg = PCF8563_R_SECOND, val[PCF8563_NCLOCKREGS]; + struct iic_msg msgs[] = { + { 0, IIC_M_WR, sizeof(reg), ® }, + { 0, IIC_M_RD, PCF8563_NCLOCKREGS, &val[PCF8563_R_SECOND] } + }; + struct pcf8563_softc *sc; + int error; + + sc = device_get_softc(dev); + msgs[0].slave = msgs[1].slave = sc->sc_addr; + error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) / + sizeof(*msgs)); + if (error != 0) { + device_printf(dev, "%s: cannot read RTC\n", __func__); + return (error); + } + + ct.nsec = 0; + ct.sec = FROMBCD(val[PCF8563_R_SECOND] & PCF8563_M_SECOND); + ct.min = FROMBCD(val[PCF8563_R_MINUTE] & PCF8563_M_MINUTE); + ct.hour = FROMBCD(val[PCF8563_R_HOUR] & PCF8563_M_HOUR); + ct.day = FROMBCD(val[PCF8563_R_DAY] & PCF8563_M_DAY); + ct.dow = val[PCF8563_R_WEEKDAY] & PCF8563_M_WEEKDAY; + ct.mon = FROMBCD(val[PCF8563_R_MONTH] & PCF8563_M_MONTH); + ct.year = FROMBCD(val[PCF8563_R_YEAR] & PCF8563_M_YEAR); + ct.year += sc->sc_year0; + if (ct.year < POSIX_BASE_YEAR) + ct.year += 100; /* assume [1970, 2069] */ + if ((val[PCF8563_R_MONTH] & PCF8563_R_MONTH_C) != 0) { + if (ct.year >= 100 + sc->sc_year0) + sc->sc_flags |= PCF8563_CPOL; + } else if (ct.year < 100 + sc->sc_year0) + sc->sc_flags |= PCF8563_CPOL; + return (clock_ct_to_ts(&ct, ts)); +} + +static int +pcf8563_settime(device_t dev, struct timespec *ts) +{ + struct clocktime ct; + uint8_t val[PCF8563_NCLOCKREGS]; + struct iic_msg msgs[] = { + { 0, IIC_M_WR, PCF8563_NCLOCKREGS - 1, &val[PCF8563_R_CS2] } + }; + struct pcf8563_softc *sc; + int error; + + sc = device_get_softc(dev); + val[PCF8563_R_CS2] = PCF8563_R_SECOND; /* abuse */ + /* Accuracy is only one second. */ + if (ts->tv_nsec >= 500000000) + ts->tv_sec++; + ts->tv_nsec = 0; + clock_ts_to_ct(ts, &ct); + val[PCF8563_R_SECOND] = TOBCD(ct.sec); + val[PCF8563_R_MINUTE] = TOBCD(ct.min); + val[PCF8563_R_HOUR] = TOBCD(ct.hour); + val[PCF8563_R_DAY] = TOBCD(ct.day); + val[PCF8563_R_WEEKDAY] = ct.dow; + val[PCF8563_R_MONTH] = TOBCD(ct.mon); + val[PCF8563_R_YEAR] = TOBCD(ct.year % 100); + if ((sc->sc_flags & PCF8563_CPOL) != 0) { + if (ct.year >= 100 + sc->sc_year0) + val[PCF8563_R_MONTH] |= PCF8563_R_MONTH_C; + } else if (ct.year < 100 + sc->sc_year0) + val[PCF8563_R_MONTH] |= PCF8563_R_MONTH_C; + + msgs[0].slave = sc->sc_addr; + error = iicbus_transfer(device_get_parent(dev), msgs, sizeof(msgs) / + sizeof(*msgs)); + if (error != 0) + device_printf(dev, "%s: cannot write RTC\n", __func__); + return (error); +} + +static device_method_t pcf8563_methods[] = { + DEVMETHOD(device_probe, pcf8563_probe), + DEVMETHOD(device_attach, pcf8563_attach), + + DEVMETHOD(clock_gettime, pcf8563_gettime), + DEVMETHOD(clock_settime, pcf8563_settime), + + DEVMETHOD_END +}; + +static driver_t pcf8563_driver = { + "pcf8563_rtc", + pcf8563_methods, + sizeof(struct pcf8563_softc), +}; + +static devclass_t pcf8563_devclass; + +DRIVER_MODULE(pcf8563, iicbus, pcf8563_driver, pcf8563_devclass, NULL, NULL); +MODULE_VERSION(pcf8563, 1); +MODULE_DEPEND(pcf8563, iicbus, 1, 1, 1); Copied: stable/8/sys/dev/iicbus/pcf8563reg.h (from r234248, head/sys/dev/iicbus/pcf8563reg.h) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/8/sys/dev/iicbus/pcf8563reg.h Sat May 26 08:58:29 2012 (r236079, copy of r234248, head/sys/dev/iicbus/pcf8563reg.h) @@ -0,0 +1,58 @@ +/* $NetBSD: pcf8563reg.h,v 1.1 2011/01/21 19:11:47 jakllsch Exp $ */ + +/*- + * Jonathan Kollasch, 2011 + * + * This file is in the public domain. + * + * $FreeBSD$ + */ + +/* + * NXP (Philips) PCF8563 RTC registers + */ + +/* We only have clock mode registers here. */ + +#ifndef _PCF8563REG_H_ +#define _PCF8563REG_H_ + +/* + * PCF8563 RTC I2C address: + * + * 101 0001 + */ +#define PCF8563_ADDR 0xa2 + +#define PCF8563_R_CS1 0x00 +#define PCF8563_R_CS2 0x01 +#define PCF8563_R_SECOND 0x02 +#define PCF8563_R_MINUTE 0x03 +#define PCF8563_R_HOUR 0x04 +#define PCF8563_R_DAY 0x05 +#define PCF8563_R_WEEKDAY 0x06 +#define PCF8563_R_MONTH 0x07 +#define PCF8563_R_YEAR 0x08 +#define PCF8563_R_MINUTE_ALARM 0x09 +#define PCF8563_R_HOUR_ALARM 0x0a +#define PCF8563_R_DAY_ALARM 0x0b +#define PCF8563_R_WEEKDAY_ALARM 0x0c +#define PCF8563_R_CLKOUT_CNTRL 0x0d +#define PCF8563_R_TIMER_CNTRL 0x0e +#define PCF8563_R_TIMER 0x0f + +#define PCF8563_R_SECOND_VL 0x80 +#define PCF8563_R_MONTH_C 0x80 + +#define PCF8563_NREGS 0x10 + +#define PCF8563_M_SECOND 0x7f +#define PCF8563_M_MINUTE 0x7f +#define PCF8563_M_HOUR 0x3f +#define PCF8563_M_DAY 0x3f +#define PCF8563_M_WEEKDAY 0x07 +#define PCF8563_M_MONTH 0x1f +#define PCF8563_M_CENTURY 0x80 +#define PCF8563_M_YEAR 0xff + +#endif /* _PCF8563REG_H_ */
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201205260858.q4Q8wTlx064222>