Convert bebox to MI todr. As part of this, separate out the mc146818

clock handling.  Originally reviewed with that committed as an MI driver
in dev/isa, crazy MD versions of mcclock made that impossible.  So for
now I'm only handling it as an MD driver.  Ok garbled@
This commit is contained in:
gdamore 2006-09-15 08:25:02 +00:00
parent 73f2f65977
commit 382a67e7e4
6 changed files with 264 additions and 233 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: GENERIC,v 1.101 2006/08/26 07:59:21 tsutsui Exp $
# $NetBSD: GENERIC,v 1.102 2006/09/15 08:25:02 gdamore Exp $
#
# GENERIC machine description file
#
@ -22,7 +22,7 @@ include "arch/bebox/conf/std.bebox"
options INCLUDE_CONFIG_FILE # embed config file in kernel binary
#ident "GENERIC-$Revision: 1.101 $"
#ident "GENERIC-$Revision: 1.102 $"
maxusers 32
@ -187,6 +187,8 @@ pcib* at pci? dev ? function ? # PCI-ISA bridges
isa* at pcib? # ISA on PCI-ISA bridge
mcclock0 at isa? port 0x70 # time-of-day clock
#pc0 at isa? port 0x60 irq 1 # generic PC console device
com0 at isa? port 0x3f8 irq 4 # standard PC serial ports

View File

@ -1,4 +1,4 @@
# $NetBSD: INSTALL,v 1.37 2006/02/05 05:01:49 cube Exp $
# $NetBSD: INSTALL,v 1.38 2006/09/15 08:25:02 gdamore Exp $
#
# First try for BEBOX config file
#
@ -107,6 +107,8 @@ isa* at pcib? # ISA on PCI-ISA bridge
pc0 at isa? port 0x60 irq 1 # generic PC console device
mcclock0 at isa? port 0x70 # generic time-of-day clock
com0 at isa? port 0x3f8 irq 4 # standard PC serial ports
com1 at isa? port 0x2f8 irq 3
#com2 at isa? port 0x380 irq 16

View File

@ -1,4 +1,4 @@
# $NetBSD: files.bebox,v 1.46 2005/12/11 12:17:03 christos Exp $
# $NetBSD: files.bebox,v 1.47 2006/09/15 08:25:02 gdamore Exp $
#
# First try for bebox specific configuration info
#
@ -83,6 +83,10 @@ file arch/bebox/isa/isadma_machdep.c isa
# PC clock
file arch/bebox/isa/isaclock.c isa
device mcclock: mc146818
attach mcclock at isa with mcclock_isa
file arch/bebox/isa/mcclock_isa.c mcclock_isa
# attribute used to represent the "keyboard controller"
# XXX should be a real device
define pckbcport { [irq = -1], [port = -1] }

View File

@ -1,3 +1,5 @@
/* $NetBSD: types.h,v 1.7 2006/09/03 13:51:23 bjh21 Exp $ */
/* $NetBSD: types.h,v 1.8 2006/09/15 08:25:02 gdamore Exp $ */
#include <powerpc/types.h>
#define __HAVE_GENERIC_TODR

View File

@ -1,4 +1,4 @@
/* $NetBSD: isaclock.c,v 1.13 2005/12/24 20:06:58 perry Exp $ */
/* $NetBSD: isaclock.c,v 1.14 2006/09/15 08:25:02 gdamore Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@ -121,7 +121,7 @@ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: isaclock.c,v 1.13 2005/12/24 20:06:58 perry Exp $");
__KERNEL_RCSID(0, "$NetBSD: isaclock.c,v 1.14 2006/09/15 08:25:02 gdamore Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@ -143,40 +143,6 @@ __KERNEL_RCSID(0, "$NetBSD: isaclock.c,v 1.13 2005/12/24 20:06:58 perry Exp $");
void sysbeepstop __P((void *));
void sysbeep __P((int, int));
void rtcinit __P((void));
int rtcget __P((mc_todregs *));
void rtcput __P((mc_todregs *));
static int yeartoday __P((int));
int hexdectodec __P((int));
int dectohexdec __P((int));
inline u_int mc146818_read __P((void *, u_int));
inline void mc146818_write __P((void *, u_int, u_int));
#define SECMIN ((unsigned)60) /* seconds per minute */
#define SECHOUR ((unsigned)(60*SECMIN)) /* seconds per hour */
#define SECDAY ((unsigned)(24*SECHOUR)) /* seconds per day */
#define SECYR ((unsigned)(365*SECDAY)) /* seconds per common year */
inline u_int
mc146818_read(sc, reg)
void *sc; /* XXX use it? */
u_int reg;
{
isa_outb(IO_RTC, (u_char)reg);
return (isa_inb(IO_RTC+1));
}
inline void
mc146818_write(sc, reg, datum)
void *sc; /* XXX use it? */
u_int reg, datum;
{
isa_outb(IO_RTC, reg);
isa_outb(IO_RTC+1, datum);
}
static int beeping;
@ -220,195 +186,3 @@ sysbeep(pitch, period)
callout_reset(&sysbeep_ch, period, sysbeepstop, NULL);
}
void
rtcinit()
{
static int first_rtcopen_ever = 1;
if (!first_rtcopen_ever)
return;
first_rtcopen_ever = 0;
mc146818_write(NULL, MC_REGA, /* XXX softc */
MC_BASE_32_KHz | MC_RATE_1024_Hz);
mc146818_write(NULL, MC_REGB, MC_REGB_24HR); /* XXX softc */
}
int
rtcget(regs)
mc_todregs *regs;
{
rtcinit();
if ((mc146818_read(NULL, MC_REGD) & MC_REGD_VRT) == 0) /* XXX softc */
return (-1);
MC146818_GETTOD(NULL, regs); /* XXX softc */
return (0);
}
void
rtcput(regs)
mc_todregs *regs;
{
rtcinit();
MC146818_PUTTOD(NULL, regs); /* XXX softc */
}
static int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static int
yeartoday(year)
int year;
{
return ((year % 4) ? 365 : 366);
}
int
hexdectodec(n)
int n;
{
return (((n >> 4) & 0x0f) * 10 + (n & 0x0f));
}
int
dectohexdec(n)
int n;
{
return ((u_char)(((n / 10) << 4) & 0xf0) | ((n % 10) & 0x0f));
}
static int timeset;
/*
* Initialize the time of day register, based on the time base which is, e.g.
* from a filesystem.
*/
void
inittodr(base)
time_t base;
{
mc_todregs rtclk;
time_t n;
int sec, mn, hr, dom, mon, yr;
int i, days = 0;
int s;
/*
* We mostly ignore the suggested time and go for the RTC clock time
* stored in the CMOS RAM. If the time can't be obtained from the
* CMOS, or if the time obtained from the CMOS is 5 or more years
* less than the suggested time, we used the suggested time. (In
* the latter case, it's likely that the CMOS battery has died.)
*/
if (base < 15*SECYR) { /* if before 1985, something's odd... */
printf("WARNING: preposterous time in file system\n");
/* read the system clock anyway */
base = 17*SECYR + 186*SECDAY + SECDAY/2;
}
s = splclock();
if (rtcget(&rtclk)) {
splx(s);
printf("WARNING: invalid time in clock chip\n");
goto fstime;
}
splx(s);
sec = hexdectodec(rtclk[MC_SEC]);
mn = hexdectodec(rtclk[MC_MIN]);
hr = hexdectodec(rtclk[MC_HOUR]);
dom = hexdectodec(rtclk[MC_DOM]);
mon = hexdectodec(rtclk[MC_MONTH]);
yr = hexdectodec(rtclk[MC_YEAR]);
yr = (yr < 70) ? yr+100 : yr;
n = sec + 60 * mn + 3600 * hr;
n += (dom - 1) * 3600 * 24;
if (yeartoday(yr) == 366)
month[1] = 29;
for (i = mon - 2; i >= 0; i--)
days += month[i];
month[1] = 28;
for (i = 70; i < yr; i++)
days += yeartoday(i);
n += days * 3600 * 24;
n += rtc_offset * 60;
if (base < n - 5*SECYR)
printf("WARNING: file system time much less than clock time\n");
else if (base > n + 5*SECYR) {
printf("WARNING: clock time much less than file system time\n");
printf("WARNING: using file system time\n");
goto fstime;
}
timeset = 1;
time.tv_sec = n;
time.tv_usec = 0;
return;
fstime:
timeset = 1;
time.tv_sec = base;
time.tv_usec = 0;
printf("WARNING: CHECK AND RESET THE DATE!\n");
}
/*
* Reset the clock.
*/
void
resettodr()
{
mc_todregs rtclk;
time_t n;
int diff, i, j;
int s;
/*
* We might have been called by boot() due to a crash early
* on. Don't reset the clock chip in this case.
*/
if (!timeset)
return;
s = splclock();
if (rtcget(&rtclk))
memset(&rtclk, 0, sizeof(rtclk));
splx(s);
diff = rtc_offset * 60;
n = (time.tv_sec - diff) % (3600 * 24); /* hrs+mins+secs */
rtclk[MC_SEC] = dectohexdec(n % 60);
n /= 60;
rtclk[MC_MIN] = dectohexdec(n % 60);
rtclk[MC_HOUR] = dectohexdec(n / 60);
n = (time.tv_sec - diff) / (3600 * 24); /* days */
rtclk[MC_DOW] = (n + 4) % 7; /* 1/1/70 is Thursday */
for (j = 1970, i = yeartoday(j); n >= i; j++, i = yeartoday(j))
n -= i;
rtclk[MC_YEAR] = dectohexdec(j - 1900);
if (i == 366)
month[1] = 29;
for (i = 0; n >= month[i]; i++)
n -= month[i];
month[1] = 28;
rtclk[MC_MONTH] = dectohexdec(++i);
rtclk[MC_DOM] = dectohexdec(++n);
s = splclock();
rtcput(&rtclk);
splx(s);
}

View File

@ -0,0 +1,247 @@
/* $NetBSD: mcclock_isa.c,v 1.1 2006/09/15 08:25:02 gdamore Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* William Jolitz and Don Ahn.
*
* 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.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)clock.c 7.2 (Berkeley) 5/12/91
*/
/*-
* Copyright (c) 1993, 1994 Charles M. Hannum.
*
* This code is derived from software contributed to Berkeley by
* William Jolitz and Don Ahn.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)clock.c 7.2 (Berkeley) 5/12/91
*/
/*
* Mach Operating System
* Copyright (c) 1991,1990,1989 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
Copyright 1988, 1989 by Intel Corporation, Santa Clara, California.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appears in all
copies and that both the copyright notice and this permission notice
appear in supporting documentation, and that the name of Intel
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.
INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mcclock_isa.c,v 1.1 2006/09/15 08:25:02 gdamore Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/time.h>
#include <machine/bus.h>
#include <dev/clock_subr.h>
#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <dev/ic/mc146818reg.h>
#include <dev/ic/mc146818var.h>
/*
* We only deal with the RTC portion of the mc146818 right now. Later
* we might want to add support for the NVRAM portion, since it
* appears that some systems use that.
*/
static int mcclock_isa_probe(struct device *, struct cfdata *, void *);
static void mcclock_isa_attach(struct device *, struct device *, void *);
CFATTACH_DECL(mcclock_isa, sizeof(struct mc146818_softc),
mcclock_isa_probe, mcclock_isa_attach, NULL, NULL);
static unsigned
mcclock_isa_read(struct mc146818_softc *sc, unsigned reg)
{
bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg);
return bus_space_read_1(sc->sc_bst, sc->sc_bsh, 1);
}
static void
mcclock_isa_write(struct mc146818_softc *sc, unsigned reg, unsigned datum)
{
bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg);
bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, datum);
}
int
mcclock_isa_probe(struct device *parent, struct cfdata *match, void *aux)
{
struct isa_attach_args *ia = aux;
bus_space_handle_t ioh;
if (ia->ia_nio < 1)
return 0;
if (ISA_DIRECT_CONFIG(ia))
return 0;
if ((ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) &&
(ia->ia_io[0].ir_addr != IO_RTC))
return 0;
if (ia->ia_niomem > 0 &&
(ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
return 0;
if (ia->ia_nirq > 0 &&
(ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ))
return 0;
if (ia->ia_ndrq > 0 &&
(ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
return 0;
if (bus_space_map(ia->ia_iot, IO_RTC, 2, 0, &ioh))
return 0;
bus_space_unmap(ia->ia_iot, ioh, 2);
ia->ia_io[0].ir_addr = IO_RTC;
ia->ia_io[0].ir_size = 2;
ia->ia_nio = 1;
ia->ia_niomem = 0;
ia->ia_nirq = 0;
ia->ia_ndrq = 0;
return 1;
}
void
mcclock_isa_attach(struct device *parent, struct device *self, void *aux)
{
struct mc146818_softc *sc = (struct mc146818_softc *)self;
struct isa_attach_args *ia = aux;
sc->sc_bst = ia->ia_iot;
if (bus_space_map(sc->sc_bst, ia->ia_io[0].ir_addr,
ia->ia_io[0].ir_size, 0, &sc->sc_bsh)) {
printf(": can't map registers!\n");
return;
}
/*
* Select a 32KHz crystal, periodic interrupt every 1024 Hz.
* XXX: We disable periodic interrupts, so why set a rate?
*/
mcclock_isa_write(sc, MC_REGA, MC_BASE_32_KHz | MC_RATE_1024_Hz);
/*
* 24 Hour clock, no interrupts please.
*/
mcclock_isa_write(sc, MC_REGB, MC_REGB_24HR);
sc->sc_year0 = 1900;
sc->sc_flag = 0;
sc->sc_mcread = mcclock_isa_read;
sc->sc_mcwrite = mcclock_isa_write;
#if 0
/*
* XXX: perhaps on some systems we should manage the century byte?
* For now we don't worry about it. (Ugly MD code.)
*/
sc->sc_getcent = mcclock_isa_getcent;
sc->sc_setcent = mcclock_isa_setcent;
#endif
mc146818_attach(sc);
printf("\n");
todr_attach(&sc->sc_handle);
}