1 /* $NetBSD: ixp425_wdog.c,v 1.1 2006/12/10 10:03:22 scw Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Steve C. Woodford.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ixp425_wdog.c,v 1.1 2006/12/10 10:03:22 scw Exp $");
37 #include <sys/systm.h>
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
43 #include <dev/clock_subr.h>
44 #include <dev/sysmon/sysmonvar.h>
46 #include <machine/intr.h>
47 #include <machine/bus.h>
49 #include <arm/xscale/ixp425reg.h>
50 #include <arm/xscale/ixp425var.h>
51 #include <arm/xscale/ixp425_sipvar.h>
54 #include <machine/db_machdep.h>
55 #include <ddb/db_extern.h>
60 struct sysmon_wdog sc_smw
;
61 bus_space_tag_t sc_bust
;
62 bus_space_handle_t sc_bush
;
67 #define IXPDOG_DEFAULT_PERIOD 8
68 #ifndef IXP425_CLOCK_FREQ
69 #define IXPDOG_COUNTS_PER_SEC 66666600
71 #define IXPDOG_COUNTS_PER_SEC IXP425_CLOCK_FREQ
74 static int ixpdog_match(struct device
*, struct cfdata
*, void *);
75 static void ixpdog_attach(struct device
*, struct device
*, void *);
77 CFATTACH_DECL(ixpdog
, sizeof(struct ixpdog_softc
),
78 ixpdog_match
, ixpdog_attach
, NULL
, NULL
);
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
*);
85 static struct ixpdog_softc
*ixpdog_softc
;
86 static void ixpdog_ddb_trap(int);
91 ixpdog_match(struct device
*parent
, struct cfdata
*match
, void *arg
)
93 struct ixpsip_attach_args
*sa
= arg
;
95 return (sa
->sa_addr
== IXP425_OST_WDOG_HWBASE
);
99 ixpdog_attach(struct device
*parent
, struct device
*self
, void *arg
)
101 struct ixpdog_softc
*sc
= (struct ixpdog_softc
*)self
;
102 struct ixpsip_attach_args
*sa
= arg
;
105 aprint_normal(": Watchdog Timer\n");
107 sc
->sc_bust
= sa
->sa_iot
;
109 if (bus_space_map(sc
->sc_bust
, sa
->sa_addr
, IXP425_OST_WDOG_SIZE
, 0,
111 aprint_error("%s: Failed to map watchdog registers\n",
112 sc
->sc_dev
.dv_xname
);
116 sc
->sc_preset
= IXPDOG_COUNTS_PER_SEC
* hz
* IXPDOG_DEFAULT_PERIOD
;
118 ixpdog_control(sc
, 0);
122 db_trap_callback
= ixpdog_ddb_trap
;
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
;
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
);
137 ixpdog_control(struct ixpdog_softc
*sc
, int enable
)
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
);
148 reg
&= ~(OST_WDOG_ENAB_CNT_ENA
| OST_WDOG_ENAB_RST_ENA
);
150 bus_space_write_4(sc
->sc_bust
, sc
->sc_bush
, IXP425_OST_WDOG
,
152 reg
|= OST_WDOG_ENAB_CNT_ENA
| OST_WDOG_ENAB_RST_ENA
;
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
);
162 ixpdog_tickle(struct sysmon_wdog
*smw
)
164 struct ixpdog_softc
*sc
= smw
->smw_cookie
;
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
,
172 bus_space_write_2(sc
->sc_bust
, sc
->sc_bush
, IXP425_OST_WDOG_KEY
,
173 ~OST_WDOG_KEY_MAJICK
);
174 restore_interrupts(s
);
180 ixpdog_setmode(struct sysmon_wdog
*smw
)
182 struct ixpdog_softc
*sc
= smw
->smw_cookie
;
184 if ((smw
->smw_mode
& WDOG_MODE_MASK
) == WDOG_MODE_DISARMED
)
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
)
197 ixpdog_control(sc
, sc
->sc_armed
);
204 ixpdog_ddb_trap(int enter
)
206 struct ixpdog_softc
*sc
;
208 if ((sc
= ixpdog_softc
) == NULL
)
211 sc
->sc_armed
+= enter
? (-1) : 1;
213 if ((enter
&& sc
->sc_armed
== 0) || (!enter
&& sc
->sc_armed
== 1))
214 ixpdog_control(sc
, sc
->sc_armed
);