Add myself to the manual credits
[maemo-rb.git] / firmware / system.c
blob52be7a1a713ef5259679e1b7f8457298ab536e13
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"
28 #ifndef SIMULATOR
29 long cpu_frequency SHAREDBSS_ATTR = CPU_FREQ;
30 #endif
32 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
33 static int boost_counter SHAREDBSS_ATTR = 0;
34 static bool cpu_idle SHAREDBSS_ATTR = false;
35 #if NUM_CORES > 1
36 static struct corelock boostctrl_cl SHAREDBSS_ATTR;
37 void cpu_boost_init(void)
39 corelock_init(&boostctrl_cl);
41 #endif
43 int get_cpu_boost_counter(void)
45 return boost_counter;
47 #ifdef CPU_BOOST_LOGGING
48 #define MAX_BOOST_LOG 64
49 static char cpu_boost_calls[MAX_BOOST_LOG][MAX_PATH];
50 static int cpu_boost_first = 0;
51 static int cpu_boost_calls_count = 0;
52 static int cpu_boost_track_message = 0;
53 int cpu_boost_log_getcount(void)
55 return cpu_boost_calls_count;
57 char * cpu_boost_log_getlog_first(void)
59 char *first;
60 corelock_lock(&boostctrl_cl);
62 first = NULL;
64 if (cpu_boost_calls_count)
66 cpu_boost_track_message = 1;
67 first = cpu_boost_calls[cpu_boost_first];
70 corelock_unlock(&boostctrl_cl);
71 return first;
74 char * cpu_boost_log_getlog_next(void)
76 int message;
77 char *next;
79 corelock_lock(&boostctrl_cl);
81 message = (cpu_boost_track_message+cpu_boost_first)%MAX_BOOST_LOG;
82 next = NULL;
84 if (cpu_boost_track_message < cpu_boost_calls_count)
86 cpu_boost_track_message++;
87 next = cpu_boost_calls[message];
90 corelock_unlock(&boostctrl_cl);
91 return next;
94 void cpu_boost_(bool on_off, char* location, int line)
96 corelock_lock(&boostctrl_cl);
98 if (cpu_boost_calls_count == MAX_BOOST_LOG)
100 cpu_boost_first = (cpu_boost_first+1)%MAX_BOOST_LOG;
101 cpu_boost_calls_count--;
102 if (cpu_boost_calls_count < 0)
103 cpu_boost_calls_count = 0;
105 if (cpu_boost_calls_count < MAX_BOOST_LOG)
107 int message = (cpu_boost_first+cpu_boost_calls_count)%MAX_BOOST_LOG;
108 snprintf(cpu_boost_calls[message], MAX_PATH,
109 "%c %s:%d",on_off==true?'B':'U',location,line);
110 cpu_boost_calls_count++;
112 #else
113 void cpu_boost(bool on_off)
115 corelock_lock(&boostctrl_cl);
116 #endif /* CPU_BOOST_LOGGING */
117 if(on_off)
119 /* Boost the frequency if not already boosted */
120 if(++boost_counter == 1)
121 set_cpu_frequency(CPUFREQ_MAX);
123 else
125 /* Lower the frequency if the counter reaches 0 */
126 if(--boost_counter <= 0)
128 if(cpu_idle)
129 set_cpu_frequency(CPUFREQ_DEFAULT);
130 else
131 set_cpu_frequency(CPUFREQ_NORMAL);
133 /* Safety measure */
134 if (boost_counter < 0)
136 boost_counter = 0;
141 corelock_unlock(&boostctrl_cl);
144 void cpu_idle_mode(bool on_off)
146 corelock_lock(&boostctrl_cl);
148 cpu_idle = on_off;
150 /* We need to adjust the frequency immediately if the CPU
151 isn't boosted */
152 if(boost_counter == 0)
154 if(cpu_idle)
155 set_cpu_frequency(CPUFREQ_DEFAULT);
156 else
157 set_cpu_frequency(CPUFREQ_NORMAL);
160 corelock_unlock(&boostctrl_cl);
162 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
165 #ifdef HAVE_FLASHED_ROCKBOX
166 static bool detect_flash_header(uint8_t *addr)
168 #ifndef BOOTLOADER
169 int oldmode = system_memory_guard(MEMGUARD_NONE);
170 #endif
171 struct flash_header hdr;
172 memcpy(&hdr, addr, sizeof(struct flash_header));
173 #ifndef BOOTLOADER
174 system_memory_guard(oldmode);
175 #endif
176 return hdr.magic == FLASH_MAGIC;
178 #endif
180 bool detect_flashed_romimage(void)
182 #ifdef HAVE_FLASHED_ROCKBOX
183 return detect_flash_header((uint8_t *)FLASH_ROMIMAGE_ENTRY);
184 #else
185 return false;
186 #endif /* HAVE_FLASHED_ROCKBOX */
189 bool detect_flashed_ramimage(void)
191 #ifdef HAVE_FLASHED_ROCKBOX
192 return detect_flash_header((uint8_t *)FLASH_RAMIMAGE_ENTRY);
193 #else
194 return false;
195 #endif /* HAVE_FLASHED_ROCKBOX */
198 bool detect_original_firmware(void)
200 return !(detect_flashed_ramimage() || detect_flashed_romimage());