svn commit: r194632 - in head/sys: conf powerpc/conf powerpc/mpc85xx

Rafal Jaworowski raj at FreeBSD.org
Mon Jun 22 15:48:49 UTC 2009


Author: raj
Date: Mon Jun 22 15:48:47 2009
New Revision: 194632
URL: http://svn.freebsd.org/changeset/base/194632

Log:
  DS1553 RTC module driver. On the MPC8555CDS system it hangs off of the LBC bus.
  
  Obtained from:	Semihalf

Added:
  head/sys/powerpc/mpc85xx/ds1553_bus_lbc.c   (contents, props changed)
  head/sys/powerpc/mpc85xx/ds1553_core.c   (contents, props changed)
  head/sys/powerpc/mpc85xx/ds1553_reg.h   (contents, props changed)
Modified:
  head/sys/conf/files.powerpc
  head/sys/powerpc/conf/MPC85XX

Modified: head/sys/conf/files.powerpc
==============================================================================
--- head/sys/conf/files.powerpc	Mon Jun 22 15:45:26 2009	(r194631)
+++ head/sys/conf/files.powerpc	Mon Jun 22 15:48:47 2009	(r194632)
@@ -109,6 +109,8 @@ powerpc/fpu/fpu_mul.c		optional	fpu_emu
 powerpc/fpu/fpu_sqrt.c		optional	fpu_emu
 powerpc/fpu/fpu_subr.c		optional	fpu_emu
 powerpc/mpc85xx/atpic.c		optional	mpc85xx isa
+powerpc/mpc85xx/ds1553_bus_lbc.c	optional	ds1553
+powerpc/mpc85xx/ds1553_core.c	optional	ds1553
 powerpc/mpc85xx/isa.c		optional	mpc85xx isa
 powerpc/mpc85xx/i2c.c		optional	iicbus mpc85xx
 powerpc/mpc85xx/lbc.c		optional	mpc85xx

Modified: head/sys/powerpc/conf/MPC85XX
==============================================================================
--- head/sys/powerpc/conf/MPC85XX	Mon Jun 22 15:45:26 2009	(r194631)
+++ head/sys/powerpc/conf/MPC85XX	Mon Jun 22 15:48:47 2009	(r194632)
@@ -56,6 +56,7 @@ device		cfi
 device		crypto
 device		cryptodev
 device		da
+device		ds1553
 device		em
 device		ether
 device		fxp

Added: head/sys/powerpc/mpc85xx/ds1553_bus_lbc.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/powerpc/mpc85xx/ds1553_bus_lbc.c	Mon Jun 22 15:48:47 2009	(r194632)
@@ -0,0 +1,134 @@
+/*-
+ * Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki
+ * 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 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$");
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
+#include <sys/clock.h>
+#include <sys/kernel.h>
+#include <sys/lock.h>
+#include <sys/module.h>
+#include <sys/mutex.h>
+#include <sys/resource.h>
+#include <sys/rman.h>
+
+#include <machine/bus.h>
+#include <machine/resource.h>
+
+#include <powerpc/mpc85xx/ds1553_reg.h>
+#include <powerpc/mpc85xx/lbc.h>
+
+#include "clock_if.h"
+
+static devclass_t rtc_devclass;
+
+static int rtc_attach(device_t dev);
+static int rtc_probe(device_t dev);
+
+static device_method_t rtc_methods[] = {
+	/* Device interface */
+	DEVMETHOD(device_probe,		rtc_probe),
+	DEVMETHOD(device_attach,	rtc_attach),
+
+	/* clock interface */
+	DEVMETHOD(clock_gettime,	ds1553_gettime),
+	DEVMETHOD(clock_settime,	ds1553_settime),
+
+	{ 0, 0 }
+};
+
+static driver_t rtc_driver = {
+	"rtc",
+	rtc_methods,
+	sizeof(struct ds1553_softc),
+};
+
+DRIVER_MODULE(rtc, lbc, rtc_driver, rtc_devclass, 0, 0);
+
+static int
+rtc_probe(device_t dev)
+{
+	uintptr_t devtype;
+	int error;
+
+	error = BUS_READ_IVAR(device_get_parent(dev), dev, LBC_IVAR_DEVTYPE,
+	    &devtype);
+	if (error)
+		return (error);
+
+	if (devtype != LBC_DEVTYPE_RTC)
+		return (EINVAL);
+
+	device_set_desc(dev, "Real Time Clock");
+
+	return (0);
+}
+
+static int
+rtc_attach(device_t dev)
+{
+	struct timespec ts;
+	struct ds1553_softc *sc;
+	int error;
+
+	sc = device_get_softc(dev);
+	bzero(sc, sizeof(struct ds1553_softc));
+
+	mtx_init(&sc->sc_mtx, "rtc_mtx", NULL, MTX_SPIN);
+
+	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
+	    RF_ACTIVE);
+	if (sc->res == NULL) {
+		device_printf(dev, "cannot allocate resources\n");
+		mtx_destroy(&sc->sc_mtx);
+		return (ENXIO);
+	}
+
+	sc->sc_bst = rman_get_bustag(sc->res);
+	sc->sc_bsh = rman_get_bushandle(sc->res);
+
+	if ((error = ds1553_attach(dev)) != 0) {
+		device_printf(dev, "cannot attach time of day clock\n");
+		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
+		mtx_destroy(&sc->sc_mtx);
+		return (error);
+	}
+
+	clock_register(dev, 1000000);
+
+	if (bootverbose) {
+		ds1553_gettime(dev, &ts);
+		device_printf(dev, "current time: %ld.%09ld\n",
+		    (long)ts.tv_sec, ts.tv_nsec);
+	}
+
+	return (0);
+}

Added: head/sys/powerpc/mpc85xx/ds1553_core.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/powerpc/mpc85xx/ds1553_core.c	Mon Jun 22 15:48:47 2009	(r194632)
@@ -0,0 +1,194 @@
+/*-
+ * Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki
+ * 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 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$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
+#include <sys/clock.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+
+#include <machine/bus.h>
+
+#include <powerpc/mpc85xx/ds1553_reg.h>
+
+static uint8_t ds1553_direct_read(device_t, bus_size_t);
+static void ds1553_direct_write(device_t, bus_size_t, uint8_t);
+
+int
+ds1553_attach(device_t dev)
+{
+	struct ds1553_softc *sc;
+	uint8_t sec, flags;
+
+	sc = device_get_softc(dev);
+
+	if (mtx_initialized(&sc->sc_mtx) == 0) {
+		device_printf(dev, "%s: mutex not initialized\n", __func__);
+		return (ENXIO);
+	}
+
+	if (sc->sc_read == NULL)
+		sc->sc_read = ds1553_direct_read;
+	if (sc->sc_write == NULL)
+		sc->sc_write = ds1553_direct_write;
+
+	sc->year_offset = POSIX_BASE_YEAR;
+
+	mtx_lock_spin(&sc->sc_mtx);
+
+	/* Turn RTC on if it was not on */
+	sec = (*sc->sc_read)(dev, DS1553_OFF_SECONDS);
+	if (sec & DS1553_BIT_OSC) {
+		sec &= ~(DS1553_BIT_OSC);
+		(*sc->sc_write)(dev, DS1553_OFF_SECONDS, sec);
+	}
+
+	/* Low-battery check */
+	flags = (*sc->sc_read)(dev, DS1553_OFF_FLAGS);
+	if (flags & DS1553_BIT_BLF)
+		device_printf(dev, "voltage-low detected.\n");
+
+	mtx_unlock_spin(&sc->sc_mtx);
+
+	return (0);
+}
+
+/*
+ * Get time of day and convert it to a struct timespec.
+ * Return 0 on success, an error number otherwise.
+ */
+int
+ds1553_gettime(device_t dev, struct timespec *ts)
+{
+	struct clocktime ct;
+	struct ds1553_softc *sc;
+	uint8_t control;
+
+	sc = device_get_softc(dev);
+
+	mtx_lock_spin(&sc->sc_mtx);
+
+	control = (*sc->sc_read)(dev, DS1553_OFF_CONTROL) | DS1553_BIT_READ;
+	(*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
+
+	ct.nsec = 0;
+	ct.sec = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_SECONDS) &
+	    DS1553_MASK_SECONDS);
+	ct.min = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_MINUTES) &
+	    DS1553_MASK_MINUTES);
+	ct.hour = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_HOURS) &
+	    DS1553_MASK_HOUR);
+	ct.dow = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_DAYOFWEEK) &
+	    DS1553_MASK_DAYOFWEEK) - 1;
+	ct.day = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_DATE) &
+	    DS1553_MASK_DATE);
+	ct.mon = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_MONTH) &
+	    DS1553_MASK_MONTH);
+	ct.year = FROMBCD((*sc->sc_read)(dev, DS1553_OFF_YEAR));
+
+	control &= ~DS1553_BIT_READ;
+	(*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
+
+	ct.year += sc->year_offset;
+
+	mtx_unlock_spin(&sc->sc_mtx);
+
+	return (clock_ct_to_ts(&ct, ts));
+}
+
+/*
+ * Set the time of day clock based on the value of the struct timespec arg.
+ * Return 0 on success, an error number otherwise.
+ */
+int
+ds1553_settime(device_t dev, struct timespec *ts)
+{
+	struct clocktime ct;
+	struct ds1553_softc *sc;
+	uint8_t control;
+
+	sc = device_get_softc(dev);
+	bzero(&ct, sizeof(struct clocktime));
+
+	/* Accuracy is only one second. */
+	if (ts->tv_nsec >= 500000000)
+		ts->tv_sec++;
+	ts->tv_nsec = 0;
+	clock_ts_to_ct(ts, &ct);
+
+	ct.year -= sc->year_offset;
+
+	mtx_lock_spin(&sc->sc_mtx);
+
+	/* Halt updates to external registers */
+	control = (*sc->sc_read)(dev, DS1553_OFF_CONTROL) | DS1553_BIT_WRITE;
+	(*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
+
+	(*sc->sc_write)(dev, DS1553_OFF_SECONDS, TOBCD(ct.sec) &
+	    DS1553_MASK_SECONDS);
+	(*sc->sc_write)(dev, DS1553_OFF_MINUTES, TOBCD(ct.min) &
+	    DS1553_MASK_MINUTES);
+	(*sc->sc_write)(dev, DS1553_OFF_HOURS, TOBCD(ct.hour) &
+	    DS1553_MASK_HOUR);
+	(*sc->sc_write)(dev, DS1553_OFF_DAYOFWEEK, TOBCD(ct.dow + 1) &
+	    DS1553_MASK_DAYOFWEEK);
+	(*sc->sc_write)(dev, DS1553_OFF_DATE, TOBCD(ct.day) &
+	    DS1553_MASK_DATE);
+	(*sc->sc_write)(dev, DS1553_OFF_MONTH, TOBCD(ct.mon) &
+	    DS1553_MASK_MONTH);
+	(*sc->sc_write)(dev, DS1553_OFF_YEAR, TOBCD(ct.year));
+
+	/* Resume updates to external registers */
+	control &= ~DS1553_BIT_WRITE;
+	(*sc->sc_write)(dev, DS1553_OFF_CONTROL, control);
+
+	mtx_unlock_spin(&sc->sc_mtx);
+
+	return (0);
+}
+
+static uint8_t
+ds1553_direct_read(device_t dev, bus_size_t off)
+{
+	struct ds1553_softc *sc;
+
+	sc = device_get_softc(dev);
+	return (bus_space_read_1(sc->sc_bst, sc->sc_bsh, off));
+}
+
+static void
+ds1553_direct_write(device_t dev, bus_size_t off, uint8_t val)
+{
+	struct ds1553_softc *sc;
+
+	sc = device_get_softc(dev);
+	bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, val);
+}

Added: head/sys/powerpc/mpc85xx/ds1553_reg.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sys/powerpc/mpc85xx/ds1553_reg.h	Mon Jun 22 15:48:47 2009	(r194632)
@@ -0,0 +1,107 @@
+/*-
+ * Copyright (C) 2006-2008 Semihalf, Grzegorz Bernacki
+ * 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 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _DEV_RTC_DS1553_H_
+#define _DEV_RTC_DS1553_H_
+
+/* DS1553 registers */
+#define DS1553_NVRAM_SIZE		0x1ff0
+#define DS1553_OFF_FLAGS		0x1ff0
+#define DS1553_OFF_ALARM_SECONDS	0x1ff2
+#define DS1553_OFF_ALARM_MINUTES	0x1ff3
+#define DS1553_OFF_ALARM_HOURS		0x1ff4
+#define DS1553_OFF_ALARM_DATE		0x1ff5
+#define DS1553_OFF_INTERRUPTS		0x1ff6
+#define DS1553_OFF_WATCHDOG		0x1ff7
+#define DS1553_OFF_CONTROL		0x1ff8
+#define DS1553_OFF_SECONDS		0x1ff9
+#define DS1553_OFF_MINUTES		0x1ffa
+#define DS1553_OFF_HOURS		0x1ffb
+#define DS1553_OFF_DAYOFWEEK		0x1ffc
+#define DS1553_OFF_DATE			0x1ffd
+#define DS1553_OFF_MONTH		0x1ffe
+#define DS1553_OFF_YEAR			0x1fff
+
+/* dayofweek register's bits */
+#define DS1553_BIT_FREQ_TEST		0x40 /* frequency test bit */
+
+/* seconds register's bit */
+#define DS1553_BIT_OSC			0x80 /* oscillator start/stop bit */
+
+/* control register's bits */
+#define DS1553_BIT_WRITE		0x80 /* write */
+#define DS1553_BIT_READ			0x40 /* read */
+
+/* watchdog register's bits */
+#define DS1553_BIT_WATCHDOG		0x80 /* watchdog steering bit */
+#define DS1553_BIT_BMB4			0x40 /* watchdog multiplier bit4 */
+#define DS1553_BIT_BMB3			0x20 /* watchdog multiplier bit3 */
+#define DS1553_BIT_BMB2			0x10 /* watchdog multiplier bit2 */
+#define DS1553_BIT_BMB1			0x8  /* watchdog multiplier bit1 */
+#define DS1553_BIT_BMB0			0x4  /* watchdog multiplier bit0 */
+#define DS1553_BIT_RB1			0x2  /* watchdog resolution bit1 */
+#define DS1553_BIT_RB0			0x1  /* watchdog resolution bit0 */
+
+/* alarm seconds/minutes/hours/date register's bit */
+#define DS1553_BIT_AM			0x80 /* alarm mask bit */
+
+/* flag register's bits */
+#define DS1553_BIT_BLF			0x10 /* battery flag */
+#define DS1553_BIT_WF			0x80 /* watchdog flag */
+
+/* register's mask */
+#define DS1553_MASK_MONTH		0x1f
+#define DS1553_MASK_DATE		0x3f
+#define DS1553_MASK_DAYOFWEEK		0x7
+#define DS1553_MASK_HOUR		0x3f
+#define DS1553_MASK_MINUTES		0x7f
+#define DS1553_MASK_SECONDS		0x7f
+
+struct ds1553_softc {
+
+	bus_space_tag_t		sc_bst;	/* bus space tag */
+	bus_space_handle_t	sc_bsh;	/* bus space handle */
+
+	int			rid;	/* resource id */
+	struct resource		*res;
+	struct mtx		sc_mtx;	/* hardware mutex */
+
+	uint32_t		year_offset;
+	/* read/write functions */
+	uint8_t			(*sc_read)(device_t, bus_size_t);
+	void			(*sc_write)(device_t, bus_size_t, uint8_t);
+};
+
+/* device interface */
+int ds1553_attach(device_t);
+
+/* clock interface */
+int ds1553_gettime(device_t, struct timespec *);
+int ds1553_settime(device_t, struct timespec *);
+
+#endif /* _DEV_RTC_DS1553_H_ */


More information about the svn-src-all mailing list