summaryrefslogtreecommitdiff
path: root/cleopatre/u-boot-1.1.6/cpu/spc300/wdt.c
blob: 270984f443456ca708dbdf5ee76c00029b9ac6da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * cpu/psc300/wdt.c
 *
 * Copyright (C) 2009 SPiDCOM Technologies
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <config.h>
#include <asm/arch/hardware.h>
#include <asm/arch/wdt.h>
#include <asm/arch/nvram.h>

/**
 * Get master clock from NVRAM
 *
 * \return  master clock value in Hz.
 */
static int get_master_clock(void)
{
        ulong masterclk;

#ifdef CONFIG_CHIP_FEATURE_FIXED_MASTER_CLOCK
        masterclk = CONFIG_CHIP_MAX_MASTER_CLOCK;
#else
	DECLARE_GLOBAL_DATA_PTR;
        bd_t *bd = gd->bd;
        spidcom_nvram_t *nvram = (spidcom_nvram_t*)bd->bi_nvram_addr;

        switch(NVRAM_BFEXT(FREQ, nvram->pkg_cfg))
        {
        case NVRAM_FREQ_100:
            masterclk = 100000000;
            break;
        case NVRAM_FREQ_125:
            masterclk = 125000000;
            break;
        case NVRAM_FREQ_133:
            masterclk = 133000000;
            break;
        case NVRAM_FREQ_150:
        default:
            masterclk = 147000000;
            break;
        }
#endif

        return masterclk;
}

/**
 * Start watchdog timer.
 */
void wdt_start(void)
{
    unsigned int *ptr = (unsigned int *)WDT_CR;

    //Enable WDT with reset pulse = 32pclk and without interrupt management
    *ptr = WDT_BF(RPL, 4) | WDT_BF(RMOD, 0) | WDT_BF(EN, 1);
}

/**
 * Stop watchdog timer.
 */
void wdt_stop(void)
{
    unsigned int *ptr = (unsigned int *)WDT_CR;

    *ptr = WDT_BF(EN, 0);
}

/**
 * Refresh watchdog timer.
 */
void wdt_refresh(void)
{
    unsigned int *ptr = (unsigned int *)WDT_CRR;

    *ptr = WDT_BF(CR, WDT_CR_VAL);
}

/**
 * Change the watchdog time interval.
 *
 * \param  new_time  new time interval in seconds.
 * \return  range error.
 */
int wdt_settimeout(int new_time)
{
    unsigned int *ptr = (unsigned int *)WDT_TORR;
    uint32_t cnt_value, top;

     //Timeout period = (2^(16+ TOP_reg)) / MASTER_CLK

    if ((new_time < 0) || (new_time > WDT_MAX_TIME))
        return -1;

    cnt_value = new_time * get_master_clock();

    for(top=15 ; top>0 ; top--)
    {
        if(cnt_value >= (1<<(top+16)))
        {
            *ptr = WDT_BFINS(TOP, top, *ptr);
            break;
        }
    }

    return 0;
}

/**
 * Get the watchdog time interval.
 *
 * \return  time interval.
 */
int wdt_gettimeout(void)
{
    unsigned int *ptr = (unsigned int *)WDT_TORR;
    uint32_t top;

     //Timeout period = (2^(16+ TOP_reg)) / MASTER_CLK

    top = WDT_BFEXT(TOP, *ptr);

    return ((1 << (top+16)) / get_master_clock());
}

/**
 * Restart watchdog timer.
 *
 * \param  timeout  time interval in seconds.
 * \return  if new timeout is done.
 */
int wdt_restart(int timeout)
{
    int result;

    wdt_stop();
    result = wdt_settimeout(timeout);
    wdt_start();
    return result;
}