Use bus reset detection for all ARC OTG devices. Remove conflict from LV24020LP drive...
[kugel-rb.git] / firmware / system.c
blobbefc785823651a7b9c39c43a514a745f395bd806
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 struct spinlock boostctrl_spin SHAREDBSS_ATTR;
37 void cpu_boost_init(void)
39 spinlock_init(&boostctrl_spin);
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 #if NUM_CORES > 1
61 spinlock_lock(&boostctrl_spin);
62 #endif
64 first = NULL;
66 if (cpu_boost_calls_count)
68 cpu_boost_track_message = 1;
69 first = cpu_boost_calls[cpu_boost_first];
72 #if NUM_CORES > 1
73 spinlock_unlock(&boostctrl_spin);
74 #endif
76 return first;
79 char * cpu_boost_log_getlog_next(void)
81 int message;
82 char *next;
84 #if NUM_CORES > 1
85 spinlock_lock(&boostctrl_spin);
86 #endif
88 message = (cpu_boost_track_message+cpu_boost_first)%MAX_BOOST_LOG;
89 next = NULL;
91 if (cpu_boost_track_message < cpu_boost_calls_count)
93 cpu_boost_track_message++;
94 next = cpu_boost_calls[message];
97 #if NUM_CORES > 1
98 spinlock_unlock(&boostctrl_spin);
99 #endif
101 return next;
104 void cpu_boost_(bool on_off, char* location, int line)
106 #if NUM_CORES > 1
107 spinlock_lock(&boostctrl_spin);
108 #endif
110 if (cpu_boost_calls_count == MAX_BOOST_LOG)
112 cpu_boost_first = (cpu_boost_first+1)%MAX_BOOST_LOG;
113 cpu_boost_calls_count--;
114 if (cpu_boost_calls_count < 0)
115 cpu_boost_calls_count = 0;
117 if (cpu_boost_calls_count < MAX_BOOST_LOG)
119 int message = (cpu_boost_first+cpu_boost_calls_count)%MAX_BOOST_LOG;
120 snprintf(cpu_boost_calls[message], MAX_PATH,
121 "%c %s:%d",on_off==true?'B':'U',location,line);
122 cpu_boost_calls_count++;
124 #else
125 void cpu_boost(bool on_off)
127 #if NUM_CORES > 1
128 spinlock_lock(&boostctrl_spin);
129 #endif
131 #endif /* CPU_BOOST_LOGGING */
132 if(on_off)
134 /* Boost the frequency if not already boosted */
135 if(++boost_counter == 1)
136 set_cpu_frequency(CPUFREQ_MAX);
138 else
140 /* Lower the frequency if the counter reaches 0 */
141 if(--boost_counter <= 0)
143 if(cpu_idle)
144 set_cpu_frequency(CPUFREQ_DEFAULT);
145 else
146 set_cpu_frequency(CPUFREQ_NORMAL);
148 /* Safety measure */
149 if (boost_counter < 0)
151 boost_counter = 0;
156 #if NUM_CORES > 1
157 spinlock_unlock(&boostctrl_spin);
158 #endif
161 void cpu_idle_mode(bool on_off)
163 #if NUM_CORES > 1
164 spinlock_lock(&boostctrl_spin);
165 #endif
167 cpu_idle = on_off;
169 /* We need to adjust the frequency immediately if the CPU
170 isn't boosted */
171 if(boost_counter == 0)
173 if(cpu_idle)
174 set_cpu_frequency(CPUFREQ_DEFAULT);
175 else
176 set_cpu_frequency(CPUFREQ_NORMAL);
179 #if NUM_CORES > 1
180 spinlock_unlock(&boostctrl_spin);
181 #endif
183 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
186 #ifdef HAVE_FLASHED_ROCKBOX
187 static bool detect_flash_header(uint8_t *addr)
189 #ifndef BOOTLOADER
190 int oldmode = system_memory_guard(MEMGUARD_NONE);
191 #endif
192 struct flash_header hdr;
193 memcpy(&hdr, addr, sizeof(struct flash_header));
194 #ifndef BOOTLOADER
195 system_memory_guard(oldmode);
196 #endif
197 return hdr.magic == FLASH_MAGIC;
199 #endif
201 bool detect_flashed_romimage(void)
203 #ifdef HAVE_FLASHED_ROCKBOX
204 return detect_flash_header((uint8_t *)FLASH_ROMIMAGE_ENTRY);
205 #else
206 return false;
207 #endif /* HAVE_FLASHED_ROCKBOX */
210 bool detect_flashed_ramimage(void)
212 #ifdef HAVE_FLASHED_ROCKBOX
213 return detect_flash_header((uint8_t *)FLASH_RAMIMAGE_ENTRY);
214 #else
215 return false;
216 #endif /* HAVE_FLASHED_ROCKBOX */
219 bool detect_original_firmware(void)
221 return !(detect_flashed_ramimage() || detect_flashed_romimage());