Home | History | Annotate | Line # | Download | only in xscale
ixp425_wdog.c revision 1.1.56.1
      1 /*	$NetBSD: ixp425_wdog.c,v 1.1.56.1 2008/05/16 02:22:04 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Steve C. Woodford.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "opt_ddb.h"
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: ixp425_wdog.c,v 1.1.56.1 2008/05/16 02:22:04 yamt Exp $");
     36 
     37 #include <sys/systm.h>
     38 #include <sys/param.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/wdog.h>
     42 
     43 #include <dev/clock_subr.h>
     44 #include <dev/sysmon/sysmonvar.h>
     45 
     46 #include <machine/intr.h>
     47 #include <machine/bus.h>
     48 
     49 #include <arm/xscale/ixp425reg.h>
     50 #include <arm/xscale/ixp425var.h>
     51 #include <arm/xscale/ixp425_sipvar.h>
     52 
     53 #ifdef DDB
     54 #include <machine/db_machdep.h>
     55 #include <ddb/db_extern.h>
     56 #endif
     57 
     58 struct ixpdog_softc {
     59 	struct device sc_dev;
     60 	struct sysmon_wdog sc_smw;
     61 	bus_space_tag_t sc_bust;
     62 	bus_space_handle_t sc_bush;
     63 	uint32_t sc_preset;
     64 	int sc_armed;
     65 };
     66 
     67 #define	IXPDOG_DEFAULT_PERIOD	8
     68 #ifndef	IXP425_CLOCK_FREQ
     69 #define	IXPDOG_COUNTS_PER_SEC	66666600
     70 #else
     71 #define	IXPDOG_COUNTS_PER_SEC	IXP425_CLOCK_FREQ
     72 #endif
     73 
     74 static int ixpdog_match(struct device *, struct cfdata *, void *);
     75 static void ixpdog_attach(struct device *, struct device *, void *);
     76 
     77 CFATTACH_DECL(ixpdog, sizeof(struct ixpdog_softc),
     78     ixpdog_match, ixpdog_attach, NULL, NULL);
     79 
     80 static void ixpdog_control(struct ixpdog_softc *, int);
     81 static int ixpdog_tickle(struct sysmon_wdog *);
     82 static int ixpdog_setmode(struct sysmon_wdog *);
     83 
     84 #ifdef DDB
     85 static struct ixpdog_softc *ixpdog_softc;
     86 static void ixpdog_ddb_trap(int);
     87 #endif
     88 
     89 
     90 static int
     91 ixpdog_match(struct device *parent, struct cfdata *match, void *arg)
     92 {
     93 	struct ixpsip_attach_args *sa = arg;
     94 
     95 	return (sa->sa_addr == IXP425_OST_WDOG_HWBASE);
     96 }
     97 
     98 static void
     99 ixpdog_attach(struct device *parent, struct device *self, void *arg)
    100 {
    101 	struct ixpdog_softc *sc = (struct ixpdog_softc *)self;
    102 	struct ixpsip_attach_args *sa = arg;
    103 
    104 	aprint_naive("\n");
    105 	aprint_normal(": Watchdog Timer\n");
    106 
    107 	sc->sc_bust = sa->sa_iot;
    108 
    109 	if (bus_space_map(sc->sc_bust, sa->sa_addr, IXP425_OST_WDOG_SIZE, 0,
    110 	    &sc->sc_bush)) {
    111 		aprint_error("%s: Failed to map watchdog registers\n",
    112 		    sc->sc_dev.dv_xname);
    113 		return;
    114 	}
    115 
    116 	sc->sc_preset = IXPDOG_COUNTS_PER_SEC * hz * IXPDOG_DEFAULT_PERIOD;
    117 	sc->sc_armed = 0;
    118 	ixpdog_control(sc, 0);
    119 
    120 #ifdef DDB
    121 	ixpdog_softc = sc;
    122 	db_trap_callback = ixpdog_ddb_trap;
    123 #endif
    124 
    125 	sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
    126 	sc->sc_smw.smw_cookie = sc;
    127 	sc->sc_smw.smw_setmode = ixpdog_setmode;
    128 	sc->sc_smw.smw_tickle = ixpdog_tickle;
    129 	sc->sc_smw.smw_period = IXPDOG_DEFAULT_PERIOD;
    130 
    131 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    132 		aprint_error("%s: unable to register watchdog with sysmon\n",
    133 		    sc->sc_dev.dv_xname);
    134 }
    135 
    136 static void
    137 ixpdog_control(struct ixpdog_softc *sc, int enable)
    138 {
    139 	uint32_t reg;
    140 	int s;
    141 
    142 	s = disable_interrupts(I32_bit);
    143 	bus_space_write_2(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_KEY,
    144 	    OST_WDOG_KEY_MAJICK);
    145 	reg = bus_space_read_4(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_ENAB);
    146 
    147 	if (!enable)
    148 		reg &= ~(OST_WDOG_ENAB_CNT_ENA | OST_WDOG_ENAB_RST_ENA);
    149 	else {
    150 		bus_space_write_4(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG,
    151 		    sc->sc_preset);
    152 		reg |= OST_WDOG_ENAB_CNT_ENA | OST_WDOG_ENAB_RST_ENA;
    153 	}
    154 
    155 	bus_space_write_4(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_ENAB, reg);
    156 	bus_space_write_2(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_KEY,
    157 	    ~OST_WDOG_KEY_MAJICK);
    158 	restore_interrupts(s);
    159 }
    160 
    161 static int
    162 ixpdog_tickle(struct sysmon_wdog *smw)
    163 {
    164 	struct ixpdog_softc *sc = smw->smw_cookie;
    165 	int s;
    166 
    167 	s = disable_interrupts(I32_bit);
    168 	bus_space_write_2(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_KEY,
    169 	    OST_WDOG_KEY_MAJICK);
    170 	bus_space_write_4(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG,
    171 	    sc->sc_preset);
    172 	bus_space_write_2(sc->sc_bust, sc->sc_bush, IXP425_OST_WDOG_KEY,
    173 	    ~OST_WDOG_KEY_MAJICK);
    174 	restore_interrupts(s);
    175 
    176 	return (0);
    177 }
    178 
    179 static int
    180 ixpdog_setmode(struct sysmon_wdog *smw)
    181 {
    182 	struct ixpdog_softc *sc = smw->smw_cookie;
    183 
    184 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED)
    185 		sc->sc_armed = 0;
    186 	else {
    187 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    188 			smw->smw_period = IXPDOG_DEFAULT_PERIOD;
    189 		sc->sc_preset = IXPDOG_COUNTS_PER_SEC * hz * smw->smw_period;
    190 		if (sc->sc_preset < (IXPDOG_COUNTS_PER_SEC * hz) ||
    191 		    sc->sc_preset < smw->smw_period)
    192 			return (EOPNOTSUPP);
    193 
    194 		sc->sc_armed = 1;
    195 	}
    196 
    197 	ixpdog_control(sc, sc->sc_armed);
    198 
    199 	return (0);
    200 }
    201 
    202 #ifdef DDB
    203 static void
    204 ixpdog_ddb_trap(int enter)
    205 {
    206 	struct ixpdog_softc *sc;
    207 
    208 	if ((sc = ixpdog_softc) == NULL)
    209 		return;
    210 
    211 	sc->sc_armed += enter ? (-1) : 1;
    212 
    213 	if ((enter && sc->sc_armed == 0) || (!enter && sc->sc_armed == 1))
    214 		ixpdog_control(sc, sc->sc_armed);
    215 }
    216 #endif
    217