Home | History | Annotate | Line # | Download | only in samsung
exynos_wdt.c revision 1.1
      1  1.1  matt /*	$NetBSD: exynos_wdt.c,v 1.1 2014/04/13 02:26:26 matt Exp $	*/
      2  1.1  matt 
      3  1.1  matt /*-
      4  1.1  matt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  1.1  matt  * All rights reserved.
      6  1.1  matt  *
      7  1.1  matt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  matt  * by Matt Thomas
      9  1.1  matt  *
     10  1.1  matt  * Redistribution and use in source and binary forms, with or without
     11  1.1  matt  * modification, are permitted provided that the following conditions
     12  1.1  matt  * are met:
     13  1.1  matt  * 1. Redistributions of source code must retain the above copyright
     14  1.1  matt  *    notice, this list of conditions and the following disclaimer.
     15  1.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  matt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  matt  *    documentation and/or other materials provided with the distribution.
     18  1.1  matt  *
     19  1.1  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  matt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  matt  */
     31  1.1  matt 
     32  1.1  matt #include "exynos_wdt.h"
     33  1.1  matt 
     34  1.1  matt #include <sys/cdefs.h>
     35  1.1  matt __KERNEL_RCSID(0, "$NetBSD: exynos_wdt.c,v 1.1 2014/04/13 02:26:26 matt Exp $");
     36  1.1  matt 
     37  1.1  matt #include <sys/param.h>
     38  1.1  matt #include <sys/bus.h>
     39  1.1  matt #include <sys/cpu.h>
     40  1.1  matt #include <sys/device.h>
     41  1.1  matt #include <sys/wdog.h>
     42  1.1  matt 
     43  1.1  matt #include <prop/proplib.h>
     44  1.1  matt 
     45  1.1  matt #include <dev/sysmon/sysmonvar.h>
     46  1.1  matt 
     47  1.1  matt #include <arm/samsung/exynos_reg.h>
     48  1.1  matt #include <arm/samsung/exynos_var.h>
     49  1.1  matt 
     50  1.1  matt #if NEXYNOS_WDT > 0
     51  1.1  matt static int exynos_wdt_match(device_t, cfdata_t, void *);
     52  1.1  matt static void exynos_wdt_attach(device_t, device_t, void *);
     53  1.1  matt 
     54  1.1  matt struct exynos_wdt_softc {
     55  1.1  matt 	struct sysmon_wdog sc_smw;
     56  1.1  matt 	device_t sc_dev;
     57  1.1  matt 	bus_space_tag_t sc_bst;
     58  1.1  matt 	bus_space_handle_t sc_wdog_bsh;
     59  1.1  matt 	u_int sc_wdog_period;
     60  1.1  matt 	u_int sc_wdog_clock_select;
     61  1.1  matt 	u_int sc_wdog_prescaler;
     62  1.1  matt 	uint32_t sc_freq;
     63  1.1  matt 	uint32_t sc_wdog_wtdat;
     64  1.1  matt 	uint32_t sc_wdog_wtcon;
     65  1.1  matt 	bool sc_wdog_armed;
     66  1.1  matt };
     67  1.1  matt 
     68  1.1  matt #ifndef EXYNOS_WDT_PERIOD_DEFAULT
     69  1.1  matt #define	EXYNOS_WDT_PERIOD_DEFAULT	12
     70  1.1  matt #endif
     71  1.1  matt 
     72  1.1  matt CFATTACH_DECL_NEW(exynos_wdt, sizeof(struct exynos_wdt_softc),
     73  1.1  matt     exynos_wdt_match, exynos_wdt_attach, NULL, NULL);
     74  1.1  matt 
     75  1.1  matt static inline uint32_t
     76  1.1  matt exynos_wdt_wdog_read(struct exynos_wdt_softc *sc, bus_size_t o)
     77  1.1  matt {
     78  1.1  matt 	return bus_space_read_4(sc->sc_bst, sc->sc_wdog_bsh, o);
     79  1.1  matt }
     80  1.1  matt 
     81  1.1  matt static inline void
     82  1.1  matt exynos_wdt_wdog_write(struct exynos_wdt_softc *sc, bus_size_t o, uint32_t v)
     83  1.1  matt {
     84  1.1  matt 	bus_space_write_4(sc->sc_bst, sc->sc_wdog_bsh, o, v);
     85  1.1  matt }
     86  1.1  matt 
     87  1.1  matt /* ARGSUSED */
     88  1.1  matt static int
     89  1.1  matt exynos_wdt_match(device_t parent, cfdata_t cf, void *aux)
     90  1.1  matt {
     91  1.1  matt 	return 1;
     92  1.1  matt }
     93  1.1  matt 
     94  1.1  matt static int
     95  1.1  matt exynos_wdt_tickle(struct sysmon_wdog *smw)
     96  1.1  matt {
     97  1.1  matt 	struct exynos_wdt_softc * const sc = smw->smw_cookie;
     98  1.1  matt 
     99  1.1  matt 	/*
    100  1.1  matt 	 * Cause the WDOG to restart counting.
    101  1.1  matt 	 */
    102  1.1  matt 	exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCNT, sc->sc_wdog_wtdat);
    103  1.1  matt 	aprint_debug_dev(sc->sc_dev, "tickle\n");
    104  1.1  matt 	return 0;
    105  1.1  matt }
    106  1.1  matt 
    107  1.1  matt static int
    108  1.1  matt exynos_wdt_setmode(struct sysmon_wdog *smw)
    109  1.1  matt {
    110  1.1  matt 	struct exynos_wdt_softc * const sc = smw->smw_cookie;
    111  1.1  matt 
    112  1.1  matt 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    113  1.1  matt 		/*
    114  1.1  matt 		 * Emit magic sequence to turn off WDOG
    115  1.1  matt 		 */
    116  1.1  matt 		sc->sc_wdog_wtcon &= ~(WTCON_ENABLE|WTCON_RESET_ENABLE);
    117  1.1  matt 		exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCON, sc->sc_wdog_wtcon);
    118  1.1  matt 		delay(1);
    119  1.1  matt 		aprint_debug_dev(sc->sc_dev, "setmode disable\n");
    120  1.1  matt 		return 0;
    121  1.1  matt 	}
    122  1.1  matt 
    123  1.1  matt 	/*
    124  1.1  matt 	 * If no changes, just tickle it and return.
    125  1.1  matt 	 */
    126  1.1  matt 	if (sc->sc_wdog_armed && smw->smw_period == sc->sc_wdog_period) {
    127  1.1  matt 		sc->sc_wdog_wtdat = sc->sc_freq * sc->sc_wdog_period - 1;
    128  1.1  matt 		sc->sc_wdog_wtcon = WTCON_ENABLE | WTCON_RESET_ENABLE
    129  1.1  matt 		    | __SHIFTIN(sc->sc_wdog_clock_select, WTCON_CLOCK_SELECT)
    130  1.1  matt 		    | __SHIFTIN(sc->sc_wdog_prescaler - 1, WTCON_PRESCALER);
    131  1.1  matt 
    132  1.1  matt 		exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCNT, sc->sc_wdog_wtdat);
    133  1.1  matt 		exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTDAT, sc->sc_wdog_wtdat);
    134  1.1  matt 		exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCON, sc->sc_wdog_wtcon);
    135  1.1  matt 		aprint_debug_dev(sc->sc_dev, "setmode refresh\n");
    136  1.1  matt 		return 0;
    137  1.1  matt 	}
    138  1.1  matt 
    139  1.1  matt 	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
    140  1.1  matt 		sc->sc_wdog_period = EXYNOS_WDT_PERIOD_DEFAULT;
    141  1.1  matt 		smw->smw_period = EXYNOS_WDT_PERIOD_DEFAULT;
    142  1.1  matt 	}
    143  1.1  matt 
    144  1.1  matt 	/*
    145  1.1  matt 	 * Make sure we don't overflow the counter.
    146  1.1  matt 	 */
    147  1.1  matt 	if (smw->smw_period * sc->sc_freq >= UINT16_MAX) {
    148  1.1  matt 		return EINVAL;
    149  1.1  matt 	}
    150  1.1  matt 
    151  1.1  matt 	sc->sc_wdog_wtdat = sc->sc_freq * sc->sc_wdog_period - 1;
    152  1.1  matt 	sc->sc_wdog_wtcon = WTCON_ENABLE | WTCON_RESET_ENABLE
    153  1.1  matt 	    | __SHIFTIN(sc->sc_wdog_clock_select, WTCON_CLOCK_SELECT)
    154  1.1  matt 	    | __SHIFTIN(sc->sc_wdog_prescaler - 1, WTCON_PRESCALER);
    155  1.1  matt 
    156  1.1  matt 	/*
    157  1.1  matt 	 * Have to disable to be able to write WTDAT
    158  1.1  matt 	 */
    159  1.1  matt 	exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCON,
    160  1.1  matt 	    sc->sc_wdog_wtcon & ~(WTCON_ENABLE | WTCON_RESET_ENABLE));
    161  1.1  matt 	exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCNT, sc->sc_wdog_wtdat);
    162  1.1  matt 	exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTDAT, sc->sc_wdog_wtdat);
    163  1.1  matt 	exynos_wdt_wdog_write(sc, EXYNOS_WDT_WTCON, sc->sc_wdog_wtcon);
    164  1.1  matt 
    165  1.1  matt 	aprint_debug_dev(sc->sc_dev, "setmode enable\n");
    166  1.1  matt 	return 0;
    167  1.1  matt }
    168  1.1  matt 
    169  1.1  matt 
    170  1.1  matt static void
    171  1.1  matt exynos_wdt_attach(device_t parent, device_t self, void *aux)
    172  1.1  matt {
    173  1.1  matt         struct exynos_wdt_softc * const sc = device_private(self);
    174  1.1  matt 	struct exyo_attach_args * const exyo = aux;
    175  1.1  matt 	prop_dictionary_t dict = device_properties(self);
    176  1.1  matt 
    177  1.1  matt 	sc->sc_dev = self;
    178  1.1  matt 	sc->sc_bst = exyo->exyo_core_bst;
    179  1.1  matt 
    180  1.1  matt 	if (bus_space_map(sc->sc_bst, exyo->exyo_loc.loc_offset,
    181  1.1  matt 	    exyo->exyo_loc.loc_size, 0, &sc->sc_wdog_bsh)) {
    182  1.1  matt 		aprint_error(": failed to map registers\n");
    183  1.1  matt 		return;
    184  1.1  matt 	}
    185  1.1  matt 
    186  1.1  matt 	/*
    187  1.1  matt 	 * This runs at the Exynos Pclk.
    188  1.1  matt 	 */
    189  1.1  matt 	prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq);
    190  1.1  matt 
    191  1.1  matt 	sc->sc_wdog_wtcon = exynos_wdt_wdog_read(sc, EXYNOS_WDT_WTCON);
    192  1.1  matt 	sc->sc_wdog_armed = (sc->sc_wdog_wtcon & WTCON_ENABLE)
    193  1.1  matt 	    && (sc->sc_wdog_wtcon & WTCON_RESET_ENABLE);
    194  1.1  matt 	if (sc->sc_wdog_armed) {
    195  1.1  matt 		sc->sc_wdog_prescaler =
    196  1.1  matt 		    __SHIFTOUT(sc->sc_wdog_wtcon, WTCON_PRESCALER) + 1;
    197  1.1  matt 		sc->sc_wdog_clock_select =
    198  1.1  matt 		    __SHIFTOUT(sc->sc_wdog_wtcon, WTCON_CLOCK_SELECT);
    199  1.1  matt 		sc->sc_freq /= sc->sc_wdog_prescaler;
    200  1.1  matt 		sc->sc_freq >>= 4 + sc->sc_wdog_clock_select;
    201  1.1  matt 		sc->sc_wdog_wtdat = exynos_wdt_wdog_read(sc, EXYNOS_WDT_WTDAT);
    202  1.1  matt 		sc->sc_wdog_period = (sc->sc_wdog_wtdat + 1) / sc->sc_freq;
    203  1.1  matt 	} else {
    204  1.1  matt 		sc->sc_wdog_period = EXYNOS_WDT_PERIOD_DEFAULT;
    205  1.1  matt 		sc->sc_wdog_prescaler = 1;
    206  1.1  matt 		/*
    207  1.1  matt 		 * Let's see what clock select we should use.
    208  1.1  matt 		 */
    209  1.1  matt 		u_int n = __builtin_ffs(sc->sc_freq) - 1;
    210  1.1  matt 		if (n > 7) {
    211  1.1  matt 			sc->sc_wdog_clock_select = WTCON_CLOCK_SELECT_128;
    212  1.1  matt 			sc->sc_freq >>= 7;
    213  1.1  matt 		} else if (n >= 4) {
    214  1.1  matt 			sc->sc_wdog_clock_select = n - 4;
    215  1.1  matt 			sc->sc_freq >>= n;
    216  1.1  matt 		}
    217  1.1  matt 		/*
    218  1.1  matt 		 * Let's hope the timer frequency isn't prime.  If it is, find
    219  1.1  matt 		 * the highest divisor which gives us the least remainder.
    220  1.1  matt 		 */
    221  1.1  matt 		sc->sc_wdog_prescaler = 0;
    222  1.1  matt 		u_int best_remainder = 256;
    223  1.1  matt 		u_int max_period = 2 * EXYNOS_WDT_PERIOD_DEFAULT * sc->sc_freq;
    224  1.1  matt 		for (size_t div = 256; UINT16_MAX > div * max_period; div++) {
    225  1.1  matt 			u_int remainder = sc->sc_freq % div;
    226  1.1  matt 			if (remainder == 0) {
    227  1.1  matt 				sc->sc_wdog_prescaler = div;
    228  1.1  matt 				break;
    229  1.1  matt 			}
    230  1.1  matt 			if (remainder < best_remainder) {
    231  1.1  matt 				sc->sc_wdog_prescaler = div;
    232  1.1  matt 				best_remainder = remainder;
    233  1.1  matt 			}
    234  1.1  matt 		}
    235  1.1  matt 		KASSERT(sc->sc_wdog_prescaler != 0);
    236  1.1  matt 		sc->sc_freq /= sc->sc_wdog_prescaler;
    237  1.1  matt 	}
    238  1.1  matt 
    239  1.1  matt 	/*
    240  1.1  matt 	 * Does the config file tell us to turn on the watchdog?
    241  1.1  matt 	 */
    242  1.1  matt 	if (device_cfdata(self)->cf_flags & 1)
    243  1.1  matt 		sc->sc_wdog_armed = true;
    244  1.1  matt 
    245  1.1  matt 	aprint_naive("\n");
    246  1.1  matt 	aprint_normal(": Exynos Watchdog Timer, default period is %u seconds%s\n",
    247  1.1  matt 	    sc->sc_wdog_period,
    248  1.1  matt 	    sc->sc_wdog_armed ? " (armed)" : "");
    249  1.1  matt 
    250  1.1  matt 	sc->sc_smw.smw_name = device_xname(self);
    251  1.1  matt 	sc->sc_smw.smw_cookie = sc;
    252  1.1  matt 	sc->sc_smw.smw_setmode = exynos_wdt_setmode;
    253  1.1  matt 	sc->sc_smw.smw_tickle = exynos_wdt_tickle;
    254  1.1  matt 	sc->sc_smw.smw_period = sc->sc_wdog_period;
    255  1.1  matt 
    256  1.1  matt 	if (sc->sc_wdog_armed) {
    257  1.1  matt 		int error = sysmon_wdog_setmode(&sc->sc_smw, WDOG_MODE_KTICKLE,
    258  1.1  matt 		    sc->sc_wdog_period);
    259  1.1  matt 		if (error)
    260  1.1  matt 			aprint_error_dev(self,
    261  1.1  matt 			    "failed to start kernel tickler: %d\n", error);
    262  1.1  matt  	}
    263  1.1  matt }
    264  1.1  matt #endif /* NEXYNOS_WDOG > 0 */
    265  1.1  matt 
    266  1.1  matt void
    267  1.1  matt exynos_wdt_reset(void)
    268  1.1  matt {
    269  1.1  matt 	bus_space_tag_t bst = &exynos_bs_tag;
    270  1.1  matt 	bus_space_handle_t bsh = exynos_core_bsh;
    271  1.1  matt 	bus_addr_t wdt_offset = 0;
    272  1.1  matt #ifdef EXYNOS4
    273  1.1  matt 	if (IS_EXYNOS4_P()) {
    274  1.1  matt 		wdt_offset = EXYNOS4_WDT_OFFSET;
    275  1.1  matt 	}
    276  1.1  matt #endif
    277  1.1  matt #ifdef EXYNOS5
    278  1.1  matt 	if (IS_EXYNOS5_P()) {
    279  1.1  matt 		wdt_offset = EXYNOS5_WDT_OFFSET;
    280  1.1  matt 	}
    281  1.1  matt #endif
    282  1.1  matt 	KASSERT(wdt_offset);
    283  1.1  matt 
    284  1.1  matt 	(void) splhigh();
    285  1.1  matt 	bus_space_write_4(bst, bsh, wdt_offset + EXYNOS_WDT_WTCON, 0);
    286  1.1  matt 	bus_space_write_4(bst, bsh, wdt_offset + EXYNOS_WDT_WTCNT, 1);
    287  1.1  matt 	bus_space_write_4(bst, bsh, wdt_offset + EXYNOS_WDT_WTCON,
    288  1.1  matt 	   WTCON_ENABLE | WTCON_RESET_ENABLE);
    289  1.1  matt }
    290