xref: /netbsd/sys/arch/arm/xscale/i80321_wdog.c (revision 6550d01e)
1 /*	$NetBSD: i80321_wdog.c,v 1.8 2005/12/24 20:06:52 perry Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Watchdog timer support for the Intel i80321 I/O processor.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: i80321_wdog.c,v 1.8 2005/12/24 20:06:52 perry Exp $");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/wdog.h>
49 
50 #include <machine/bus.h>
51 #include <arm/cpufunc.h>
52 
53 #include <arm/xscale/i80321reg.h>
54 #include <arm/xscale/i80321var.h>
55 
56 #include <dev/sysmon/sysmonvar.h>
57 
58 struct iopwdog_softc {
59 	struct device sc_dev;
60 	struct sysmon_wdog sc_smw;
61 	int sc_wdog_armed;
62 	int sc_wdog_period;
63 };
64 
65 static inline void
66 wdtcr_write(uint32_t val)
67 {
68 
69 	__asm volatile("mcr p6, 0, %0, c7, c1, 0"
70 		:
71 		: "r" (val));
72 }
73 
74 static int
75 iopwdog_tickle(struct sysmon_wdog *smw)
76 {
77 
78 	wdtcr_write(WDTCR_ENABLE1);
79 	wdtcr_write(WDTCR_ENABLE2);
80 	return (0);
81 }
82 
83 static int
84 iopwdog_setmode(struct sysmon_wdog *smw)
85 {
86 	struct iopwdog_softc *sc = smw->smw_cookie;
87 
88 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
89 		/* The i80321 watchdog can't be disarmed once armed. */
90 		if (sc->sc_wdog_armed)
91 			return (EOPNOTSUPP);
92 	} else {
93 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
94 			smw->smw_period = sc->sc_wdog_period;
95 		else if (smw->smw_period != sc->sc_wdog_period) {
96 			/* Can't change the i80321 watchdog period. */
97 			return (EOPNOTSUPP);
98 		}
99 		sc->sc_wdog_armed = 1;
100 		/* Watchdog is armed by tickling it. */
101 		iopwdog_tickle(smw);
102 	}
103 	return (0);
104 }
105 
106 static int
107 iopwdog_match(struct device *parent, struct cfdata *cf, void *aux)
108 {
109 	struct iopxs_attach_args *ia = aux;
110 
111 	if (strcmp(cf->cf_name, ia->ia_name) == 0)
112 		return (1);
113 
114 	return (0);
115 }
116 
117 static void
118 iopwdog_attach(struct device *parent, struct device *self, void *aux)
119 {
120 	struct iopwdog_softc *sc = (void *) self;
121 
122 	/*
123 	 * XXX Should compute the period based on processor speed.
124 	 * For a 600MHz XScale core, the wdog must be tickled approx.
125 	 * every 7 seconds.
126 	 */
127 	sc->sc_wdog_period = 7;
128 
129 	aprint_naive(": Watchdog timer\n");
130 	aprint_normal(": %d second period\n", sc->sc_wdog_period);
131 
132 	sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
133 	sc->sc_smw.smw_cookie = sc;
134 	sc->sc_smw.smw_setmode = iopwdog_setmode;
135 	sc->sc_smw.smw_tickle = iopwdog_tickle;
136 	sc->sc_smw.smw_period = sc->sc_wdog_period;
137 
138 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
139 		aprint_error("%s: unable to register with sysmon\n",
140 		    sc->sc_dev.dv_xname);
141 }
142 
143 CFATTACH_DECL(iopwdog, sizeof(struct iopwdog_softc),
144     iopwdog_match, iopwdog_attach, NULL, NULL);
145