fuzev2: prevent button light flickering when accessing µSD
[kugel-rb.git] / firmware / system.c
blob87fc40850f88b0e2cdfa6f02d7fe2a522a4e0dce
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Alan Korr
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
22 #include "system.h"
23 #include <stdio.h>
24 #include "kernel.h"
25 #include "thread.h"
26 #include "string.h"
27 #include "file.h"
29 #ifndef SIMULATOR
30 long cpu_frequency SHAREDBSS_ATTR = CPU_FREQ;
31 #endif
33 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
34 static int boost_counter SHAREDBSS_ATTR = 0;
35 static bool cpu_idle SHAREDBSS_ATTR = false;
36 #if NUM_CORES > 1
37 static struct corelock boostctrl_cl SHAREDBSS_ATTR;
38 void cpu_boost_init(void)
40 corelock_init(&boostctrl_cl);
42 #endif
44 int get_cpu_boost_counter(void)
46 return boost_counter;
48 #ifdef CPU_BOOST_LOGGING
49 #define MAX_BOOST_LOG 64
50 static char cpu_boost_calls[MAX_BOOST_LOG][MAX_PATH];
51 static int cpu_boost_first = 0;
52 static int cpu_boost_calls_count = 0;
53 static int cpu_boost_track_message = 0;
54 int cpu_boost_log_getcount(void)
56 return cpu_boost_calls_count;
58 char * cpu_boost_log_getlog_first(void)
60 char *first;
61 corelock_lock(&boostctrl_cl);
63 first = NULL;
65 if (cpu_boost_calls_count)
67 cpu_boost_track_message = 1;
68 first = cpu_boost_calls[cpu_boost_first];
71 corelock_unlock(&boostctrl_cl);
72 return first;
75 char * cpu_boost_log_getlog_next(void)
77 int message;
78 char *next;
80 corelock_lock(&boostctrl_cl);
82 message = (cpu_boost_track_message+cpu_boost_first)%MAX_BOOST_LOG;
83 next = NULL;
85 if (cpu_boost_track_message < cpu_boost_calls_count)
87 cpu_boost_track_message++;
88 next = cpu_boost_calls[message];
91 corelock_unlock(&boostctrl_cl);
92 return next;
95 void cpu_boost_(bool on_off, char* location, int line)
97 corelock_lock(&boostctrl_cl);
99 if (cpu_boost_calls_count == MAX_BOOST_LOG)
101 cpu_boost_first = (cpu_boost_first+1)%MAX_BOOST_LOG;
102 cpu_boost_calls_count--;
103 if (cpu_boost_calls_count < 0)
104 cpu_boost_calls_count = 0;
106 if (cpu_boost_calls_count < MAX_BOOST_LOG)
108 int message = (cpu_boost_first+cpu_boost_calls_count)%MAX_BOOST_LOG;
109 snprintf(cpu_boost_calls[message], MAX_PATH,
110 "%c %s:%d",on_off?'B':'U',location,line);
111 cpu_boost_calls_count++;
113 #else
114 void cpu_boost(bool on_off)
116 corelock_lock(&boostctrl_cl);
117 #endif /* CPU_BOOST_LOGGING */
118 if(on_off)
120 /* Boost the frequency if not already boosted */
121 if(++boost_counter == 1)
122 set_cpu_frequency(CPUFREQ_MAX);
124 else
126 /* Lower the frequency if the counter reaches 0 */
127 if(--boost_counter <= 0)
129 if(cpu_idle)
130 set_cpu_frequency(CPUFREQ_DEFAULT);
131 else
132 set_cpu_frequency(CPUFREQ_NORMAL);
134 /* Safety measure */
135 if (boost_counter < 0)
137 boost_counter = 0;
142 corelock_unlock(&boostctrl_cl);
145 void cpu_idle_mode(bool on_off)
147 corelock_lock(&boostctrl_cl);
149 cpu_idle = on_off;
151 /* We need to adjust the frequency immediately if the CPU
152 isn't boosted */
153 if(boost_counter == 0)
155 if(cpu_idle)
156 set_cpu_frequency(CPUFREQ_DEFAULT);
157 else
158 set_cpu_frequency(CPUFREQ_NORMAL);
161 corelock_unlock(&boostctrl_cl);
163 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
166 #ifdef HAVE_FLASHED_ROCKBOX
167 static bool detect_flash_header(uint8_t *addr)
169 #ifndef BOOTLOADER
170 int oldmode = system_memory_guard(MEMGUARD_NONE);
171 #endif
172 struct flash_header hdr;
173 memcpy(&hdr, addr, sizeof(struct flash_header));
174 #ifndef BOOTLOADER
175 system_memory_guard(oldmode);
176 #endif
177 return hdr.magic == FLASH_MAGIC;
179 #endif
181 bool detect_flashed_romimage(void)
183 #ifdef HAVE_FLASHED_ROCKBOX
184 return detect_flash_header((uint8_t *)FLASH_ROMIMAGE_ENTRY);
185 #else
186 return false;
187 #endif /* HAVE_FLASHED_ROCKBOX */
190 bool detect_flashed_ramimage(void)
192 #ifdef HAVE_FLASHED_ROCKBOX
193 return detect_flash_header((uint8_t *)FLASH_RAMIMAGE_ENTRY);
194 #else
195 return false;
196 #endif /* HAVE_FLASHED_ROCKBOX */
199 bool detect_original_firmware(void)
201 return !(detect_flashed_ramimage() || detect_flashed_romimage());