summaryrefslogtreecommitdiff
path: root/tools/updater/utils.c
blob: e230e19f9282eaf2bf4519cf9b0b39b9a47f51ff (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
#include <common.h>
#include <asm/processor.h>
#include <memio.h>
#include <linux/ctype.h>

static __inline__ unsigned long
get_msr(void)
{
	unsigned long msr;

	asm volatile("mfmsr %0" : "=r" (msr) :);
	return msr;
}

static __inline__ void
set_msr(unsigned long msr)
{
	asm volatile("mtmsr %0" : : "r" (msr)); 
}

static __inline__ unsigned long
get_dec(void)
{
	unsigned long val;

	asm volatile("mfdec %0" : "=r" (val) :);
	return val;
}


static __inline__ void
set_dec(unsigned long val)
{
	asm volatile("mtdec %0" : : "r" (val)); 
}


void
enable_interrupts(void)
{
    set_msr (get_msr() | MSR_EE);
}

/* returns flag if MSR_EE was set before */
int
disable_interrupts(void)
{
    ulong msr;

    msr = get_msr();
    set_msr (msr & ~MSR_EE);
    return ((msr & MSR_EE) != 0);
}

u8 in8(u32 port)
{
    return in_byte(port);
}

void out8(u32 port, u8 val)
{
    out_byte(port, val);
}

unsigned long in32(u32 port)
{
    return in_long(port);
}

unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
{
        unsigned long result = 0,value;

        if (*cp == '0') {
                cp++;
                if ((*cp == 'x') && isxdigit(cp[1])) {
                        base = 16;
                        cp++;
                }
                if (!base) {
                        base = 8;
                }
        }
        if (!base) {
                base = 10;
        }
        while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
            ? toupper(*cp) : *cp)-'A'+10) < base) {
                result = result*base + value;
                cp++;
        }
        if (endp)
                *endp = (char *)cp;
        return result;
}

long simple_strtol(const char *cp,char **endp,unsigned int base)
{
        if(*cp=='-')
                return -simple_strtoul(cp+1,endp,base);
        return simple_strtoul(cp,endp,base);
}

static inline void
soft_restart(unsigned long addr)
{
        /* SRR0 has system reset vector, SRR1 has default MSR value */
        /* rfi restores MSR from SRR1 and sets the PC to the SRR0 value */

        __asm__ __volatile__ ("mtspr    26, %0"         :: "r" (addr));
        __asm__ __volatile__ ("li       4, (1 << 6)"    ::: "r4");
        __asm__ __volatile__ ("mtspr    27, 4");
        __asm__ __volatile__ ("rfi");

        while(1);       /* not reached */
}

void
do_reset (void)
{
        ulong addr;
        /* flush and disable I/D cache */
        __asm__ __volatile__ ("mfspr    3, 1008"        ::: "r3");
        __asm__ __volatile__ ("ori      5, 5, 0xcc00"   ::: "r5");
        __asm__ __volatile__ ("ori      4, 3, 0xc00"    ::: "r4");
        __asm__ __volatile__ ("andc     5, 3, 5"        ::: "r5");
        __asm__ __volatile__ ("sync");
        __asm__ __volatile__ ("mtspr    1008, 4");
        __asm__ __volatile__ ("isync");
        __asm__ __volatile__ ("sync");
        __asm__ __volatile__ ("mtspr    1008, 5");
        __asm__ __volatile__ ("isync");
        __asm__ __volatile__ ("sync");

#ifdef CFG_RESET_ADDRESS
        addr = CFG_RESET_ADDRESS;
#else
        /*
         * note: when CFG_MONITOR_BASE points to a RAM address,
         * CFG_MONITOR_BASE - sizeof (ulong) is usually a valid
         * address. Better pick an address known to be invalid on your
         * system and assign it to CFG_RESET_ADDRESS.
         */
        addr = CFG_MONITOR_BASE - sizeof (ulong);
#endif
        soft_restart(addr);
        while(1);       /* not reached */
}