NetBSD/sys/arch/x68k/dev/pow.c

315 lines
6.7 KiB
C
Raw Normal View History

/* $NetBSD: pow.c,v 1.8 2000/02/20 16:18:51 minoura Exp $ */
1996-05-05 16:17:03 +04:00
/*
* Copyright (c) 1995 MINOURA Makoto.
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Minoura Makoto.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
*/
/*
* Power switch device driver.
* Useful for
* 1. accessing boot information.
* 2. looking at the front or external power switch.
*/
#include <sys/param.h>
1996-05-15 20:12:48 +04:00
#include <sys/systm.h>
1996-05-05 16:17:03 +04:00
#include <sys/proc.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/fcntl.h>
1996-05-15 20:12:48 +04:00
#include <sys/signalvar.h>
1997-10-12 18:44:12 +04:00
#include <sys/conf.h>
1996-05-05 16:17:03 +04:00
#include <machine/powioctl.h>
#include <x68k/dev/powvar.h>
#include <x68k/x68k/iodevice.h>
#include "pow.h"
#define sramtop (IODEVbase->io_sram)
#define rtc (IODEVbase->io_rtc)
struct pow_softc pows[NPOW];
1997-10-12 18:44:12 +04:00
cdev_decl(pow);
void powattach __P((int));
void powintr __P((void));
static int setalarm __P((struct x68k_alarminfo *));
static void pow_check_switch __P((void*));
1996-05-05 16:17:03 +04:00
/* ARGSUSED */
1996-05-15 20:12:48 +04:00
void
1996-05-05 16:17:03 +04:00
powattach(num)
int num;
{
int minor;
int sw;
sw = ~mfp.gpip & 7;
mfp.ierb &= ~7; /* disable mfp power switch interrupt */
mfp.aer &= ~7;
for (minor = 0; minor < num; minor++) {
1996-05-15 20:12:48 +04:00
if (minor == 0)
1996-05-05 16:17:03 +04:00
pows[minor].status = POW_FREE;
1996-05-15 20:12:48 +04:00
else
1996-05-05 16:17:03 +04:00
pows[minor].status = POW_ANY;
1996-05-15 20:12:48 +04:00
1996-05-05 16:17:03 +04:00
pows[minor].sw = sw;
if (sw) {
mfp.aer |= sw;
mfp.ierb |= sw;
}
1996-10-13 07:29:05 +04:00
printf("pow%d: started by ", minor);
if (sw & POW_EXTERNALSW)
1996-10-13 07:29:05 +04:00
printf ("external power switch.\n");
1996-05-05 16:17:03 +04:00
else if (sw & POW_FRONTSW)
1996-10-13 07:29:05 +04:00
printf ("front power switch.\n");
/* XXX: I don't know why POW_ALARMSW should not be checked */
#if 0
else if ((sw & POW_ALARMSW) && sramtop[0x26] == 0)
printf ("RTC alarm.\n");
1996-05-05 16:17:03 +04:00
else
1996-10-13 07:29:05 +04:00
printf ("???.\n");
#else
else
printf ("RTC alarm.\n");
#endif
1996-05-05 16:17:03 +04:00
}
shutdownhook_establish(pow_check_switch, 0);
1996-05-05 16:17:03 +04:00
}
/*ARGSUSED*/
int
1997-10-12 18:44:12 +04:00
powopen(dev, flags, mode, p)
1996-05-05 16:17:03 +04:00
dev_t dev;
1997-10-12 18:44:12 +04:00
int flags, mode;
struct proc *p;
1996-05-05 16:17:03 +04:00
{
struct pow_softc *sc = &pows[minor(dev)];
if (minor(dev) >= NPOW)
return EXDEV;
if (sc->status == POW_BUSY)
return EBUSY;
sc->pid = 0;
if (sc->status == POW_FREE)
sc->status = POW_BUSY;
sc->rw = (flags & (FREAD|FWRITE));
return 0;
}
/*ARGSUSED*/
1997-10-12 18:44:12 +04:00
int
powclose (dev, flags, mode, p)
1996-05-05 16:17:03 +04:00
dev_t dev;
1997-10-12 18:44:12 +04:00
int flags, mode;
struct proc *p;
1996-05-05 16:17:03 +04:00
{
struct pow_softc *sc = &pows[minor(dev)];
if (sc->status == POW_BUSY)
sc->status = POW_FREE;
sc->pid = 0;
1997-10-12 18:44:12 +04:00
return 0;
1996-05-05 16:17:03 +04:00
}
#define SRAMINT(offset) (*((int*) (&sramtop[offset])))
#define RTCWAIT DELAY(100)
static int
setalarm (bp)
struct x68k_alarminfo *bp;
{
int s = splclock ();
int ontime;
sysport.sramwp = 0x31;
if (bp->al_enable) {
SRAMINT (0x1e) = bp->al_dowhat;
SRAMINT (0x22) = bp->al_ontime;
SRAMINT (0x14) = (bp->al_offtime / 60) - 1;
sramtop[0x26] = 0;
} else {
sramtop[0x26] = 7;
}
sysport.sramwp = 0;
rtc.bank0.mode = 9;
RTCWAIT;
rtc.bank1.reset = 5;
RTCWAIT;
if (bp->al_enable) {
ontime = bp->al_ontime;
if ((ontime & 0x0f) <= 9)
rtc.bank1.al_min = ontime & 0x0f;
RTCWAIT;
ontime >>= 4;
if ((ontime & 0x0f) <= 6)
rtc.bank1.al_min10 = ontime & 0x0f;
RTCWAIT;
ontime >>= 4;
if ((ontime & 0x0f) <= 9)
rtc.bank1.al_hour = ontime & 0x0f;
RTCWAIT;
ontime >>= 4;
if ((ontime & 0x0f) <= 2)
rtc.bank1.al_hour10 = ontime & 0x0f;
RTCWAIT;
ontime >>= 4;
if ((ontime & 0x0f) <= 9)
rtc.bank1.al_day = ontime & 0x0f;
RTCWAIT;
ontime >>= 4;
if ((ontime & 0x0f) <= 3)
rtc.bank1.al_day10 = ontime & 0x0f;
RTCWAIT;
ontime >>= 4;
if ((ontime & 0x0f) <= 6)
rtc.bank1.al_week = ontime & 0x0f;
RTCWAIT;
rtc.bank1.clkout = 0;
RTCWAIT;
rtc.bank1.mode = 0x0c;
} else {
rtc.bank1.clkout = 7;
RTCWAIT;
rtc.bank1.mode = 0x08;
}
1996-05-15 20:12:48 +04:00
splx(s);
1996-05-05 16:17:03 +04:00
return 0;
}
/*ARGSUSED*/
int
powioctl (dev, cmd, addr, flag, p)
dev_t dev;
u_long cmd;
caddr_t addr;
int flag;
struct proc *p;
{
struct pow_softc *sc = &pows[minor(dev)];
switch (cmd) {
case POWIOCGPOWERINFO:
{
1996-05-15 20:12:48 +04:00
struct x68k_powerinfo *bp = (struct x68k_powerinfo*)addr;
1996-05-05 16:17:03 +04:00
if (!(sc->rw & FREAD))
return EBADF;
bp->pow_switch_boottime = sc->sw;
bp->pow_switch_current = ~mfp.gpip & 7;
bp->pow_boottime = boottime.tv_sec;
bp->pow_bootcount = SRAMINT (0x44);
bp->pow_usedtotal = SRAMINT (0x40) * 60;
}
break;
case POWIOCGALARMINFO:
{
struct x68k_alarminfo *bp = (struct x68k_alarminfo*) addr;
if (!(sc->rw & FREAD))
return EBADF;
bp->al_enable = (sramtop[0x26] == 0);
bp->al_ontime = SRAMINT (0x22);
bp->al_dowhat = SRAMINT (0x1e);
bp->al_offtime = (SRAMINT (0x14) + 1) * 60;
}
break;
case POWIOCSALARMINFO:
if (!(sc->rw & FWRITE))
return EBADF;
return setalarm ((struct x68k_alarminfo*) addr);
case POWIOCSSIGNAL:
if (minor(dev) != 0)
return EOPNOTSUPP;
if (!(sc->rw & FWRITE))
return EBADF;
{
int signum = *(int*) addr;
if (signum <= 0 || signum > 31)
return EINVAL;
sc->signum = signum;
sc->proc = p;
sc->pid = p->p_pid;
}
break;
default:
return EINVAL;
}
return 0;
}
void
1996-05-15 20:12:48 +04:00
powintr()
1996-05-05 16:17:03 +04:00
{
int sw;
int s;
1996-05-15 20:12:48 +04:00
s = spl6();
1996-05-05 16:17:03 +04:00
sw = ~mfp.gpip & 6;
mfp.aer &= ~sw;
mfp.ierb |= sw;
if (pows[0].status == POW_BUSY && pows[0].pid != 0)
1996-05-15 20:12:48 +04:00
psignal(pows[0].proc, pows[0].signum);
1996-05-05 16:17:03 +04:00
1996-05-15 20:12:48 +04:00
splx(s);
1996-05-05 16:17:03 +04:00
}
static void
pow_check_switch(dummy)
void *dummy;
{
extern int power_switch_is_off;
if ((~mfp.gpip & (POW_FRONTSW | POW_EXTERNALSW)) == 0)
power_switch_is_off = 1;
}