Fix FS#12824 : Malfunctioning FFT plugin in Sansa Clip Zip
[maemo-rb.git] / firmware / system.c
blob37631b873502497740146b03386273c7b7b8813a
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 CPU_FREQ
30 #define CPU_FREQ (-1)
31 #endif
33 long cpu_frequency SHAREDBSS_ATTR = CPU_FREQ;
35 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
36 static int boost_counter SHAREDBSS_ATTR = 0;
37 static bool cpu_idle SHAREDBSS_ATTR = false;
38 #if NUM_CORES > 1
39 static struct corelock boostctrl_cl SHAREDBSS_ATTR;
40 void cpu_boost_init(void)
42 corelock_init(&boostctrl_cl);
44 #endif
46 int get_cpu_boost_counter(void)
48 return boost_counter;
50 #ifdef CPU_BOOST_LOGGING
51 #define MAX_BOOST_LOG 64
52 static char cpu_boost_calls[MAX_BOOST_LOG][MAX_PATH];
53 static int cpu_boost_first = 0;
54 static int cpu_boost_calls_count = 0;
55 static int cpu_boost_track_message = 0;
56 int cpu_boost_log_getcount(void)
58 return cpu_boost_calls_count;
60 char * cpu_boost_log_getlog_first(void)
62 char *first;
63 corelock_lock(&boostctrl_cl);
65 first = NULL;
67 if (cpu_boost_calls_count)
69 cpu_boost_track_message = 1;
70 first = cpu_boost_calls[cpu_boost_first];
73 corelock_unlock(&boostctrl_cl);
74 return first;
77 char * cpu_boost_log_getlog_next(void)
79 int message;
80 char *next;
82 corelock_lock(&boostctrl_cl);
84 message = (cpu_boost_track_message+cpu_boost_first)%MAX_BOOST_LOG;
85 next = NULL;
87 if (cpu_boost_track_message < cpu_boost_calls_count)
89 cpu_boost_track_message++;
90 next = cpu_boost_calls[message];
93 corelock_unlock(&boostctrl_cl);
94 return next;
97 void cpu_boost_(bool on_off, char* location, int line)
99 corelock_lock(&boostctrl_cl);
101 if (cpu_boost_calls_count == MAX_BOOST_LOG)
103 cpu_boost_first = (cpu_boost_first+1)%MAX_BOOST_LOG;
104 cpu_boost_calls_count--;
105 if (cpu_boost_calls_count < 0)
106 cpu_boost_calls_count = 0;
108 if (cpu_boost_calls_count < MAX_BOOST_LOG)
110 int message = (cpu_boost_first+cpu_boost_calls_count)%MAX_BOOST_LOG;
111 snprintf(cpu_boost_calls[message], MAX_PATH,
112 "%c %s:%d",on_off?'B':'U',location,line);
113 cpu_boost_calls_count++;
115 #else
116 void cpu_boost(bool on_off)
118 corelock_lock(&boostctrl_cl);
119 #endif /* CPU_BOOST_LOGGING */
120 if(on_off)
122 /* Boost the frequency if not already boosted */
123 if(++boost_counter == 1)
124 set_cpu_frequency(CPUFREQ_MAX);
126 else
128 /* Lower the frequency if the counter reaches 0 */
129 if(--boost_counter <= 0)
131 if(cpu_idle)
132 set_cpu_frequency(CPUFREQ_DEFAULT);
133 else
134 set_cpu_frequency(CPUFREQ_NORMAL);
136 /* Safety measure */
137 if (boost_counter < 0)
139 boost_counter = 0;
144 corelock_unlock(&boostctrl_cl);
147 void cpu_idle_mode(bool on_off)
149 corelock_lock(&boostctrl_cl);
151 cpu_idle = on_off;
153 /* We need to adjust the frequency immediately if the CPU
154 isn't boosted */
155 if(boost_counter == 0)
157 if(cpu_idle)
158 set_cpu_frequency(CPUFREQ_DEFAULT);
159 else
160 set_cpu_frequency(CPUFREQ_NORMAL);
163 corelock_unlock(&boostctrl_cl);
165 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
168 #ifdef HAVE_FLASHED_ROCKBOX
169 static bool detect_flash_header(uint8_t *addr)
171 #ifndef BOOTLOADER
172 int oldmode = system_memory_guard(MEMGUARD_NONE);
173 #endif
174 struct flash_header hdr;
175 memcpy(&hdr, addr, sizeof(struct flash_header));
176 #ifndef BOOTLOADER
177 system_memory_guard(oldmode);
178 #endif
179 return hdr.magic == FLASH_MAGIC;
181 #endif
183 bool detect_flashed_romimage(void)
185 #ifdef HAVE_FLASHED_ROCKBOX
186 return detect_flash_header((uint8_t *)FLASH_ROMIMAGE_ENTRY);
187 #else
188 return false;
189 #endif /* HAVE_FLASHED_ROCKBOX */
192 bool detect_flashed_ramimage(void)
194 #ifdef HAVE_FLASHED_ROCKBOX
195 return detect_flash_header((uint8_t *)FLASH_RAMIMAGE_ENTRY);
196 #else
197 return false;
198 #endif /* HAVE_FLASHED_ROCKBOX */
201 bool detect_original_firmware(void)
203 return !(detect_flashed_ramimage() || detect_flashed_romimage());