Added ID3 database support. Still very early.
[kugel-rb.git] / firmware / kernel.c
blob7c0226421adc3dbbae2d063d4d2001bb1ea8314a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdlib.h>
20 #include <string.h>
21 #include "kernel.h"
22 #include "thread.h"
23 #include "cpu.h"
24 #include "system.h"
25 #include "panic.h"
27 long current_tick = 0;
29 static void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);
31 static void tick_start(unsigned int interval_in_ms);
33 /* This array holds all queues that are initiated. It is used for broadcast. */
34 static struct event_queue *all_queues[32];
35 static int num_queues;
37 void sleep(int ticks) __attribute__ ((section(".icode")));
38 void queue_wait(struct event_queue *q, struct event *ev) __attribute__ ((section(".icode")));
40 /****************************************************************************
41 * Standard kernel stuff
42 ****************************************************************************/
43 void kernel_init(void)
45 /* Init the threading API */
46 init_threads();
48 memset(tick_funcs, 0, sizeof(tick_funcs));
50 num_queues = 0;
51 memset(all_queues, 0, sizeof(all_queues));
53 tick_start(1000/HZ);
56 void sleep(int ticks)
58 /* Always sleep at least 1 tick */
59 int timeout = current_tick + ticks + 1;
61 while (TIME_BEFORE( current_tick, timeout )) {
62 sleep_thread();
64 wake_up_thread();
67 void yield(void)
69 switch_thread();
70 wake_up_thread();
73 /****************************************************************************
74 * Queue handling stuff
75 ****************************************************************************/
76 void queue_init(struct event_queue *q)
78 q->read = 0;
79 q->write = 0;
81 /* Add it to the all_queues array */
82 all_queues[num_queues++] = q;
85 void queue_wait(struct event_queue *q, struct event *ev)
87 while(q->read == q->write)
89 sleep_thread();
91 wake_up_thread();
93 *ev = q->events[(q->read++) & QUEUE_LENGTH_MASK];
96 void queue_wait_w_tmo(struct event_queue *q, struct event *ev, int ticks)
98 unsigned int timeout = current_tick + ticks;
100 while(q->read == q->write && TIME_BEFORE( current_tick, timeout ))
102 sleep_thread();
104 wake_up_thread();
106 if(q->read != q->write)
108 *ev = q->events[(q->read++) & QUEUE_LENGTH_MASK];
110 else
112 ev->id = SYS_TIMEOUT;
116 void queue_post(struct event_queue *q, int id, void *data)
118 int wr;
119 int oldlevel;
121 oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
122 wr = (q->write++) & QUEUE_LENGTH_MASK;
124 q->events[wr].id = id;
125 q->events[wr].data = data;
126 set_irq_level(oldlevel);
129 bool queue_empty(const struct event_queue* q)
131 return ( q->read == q->write );
134 void queue_clear(struct event_queue* q)
136 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
137 q->read = 0;
138 q->write = 0;
139 set_irq_level(oldlevel);
142 int queue_broadcast(int id, void *data)
144 int i;
146 for(i = 0;i < num_queues;i++)
148 queue_post(all_queues[i], id, data);
151 return num_queues;
154 /****************************************************************************
155 * Timer tick
156 ****************************************************************************/
157 #if CONFIG_CPU == SH7034
158 static void tick_start(unsigned int interval_in_ms)
160 unsigned int count;
162 count = FREQ * interval_in_ms / 1000 / 8;
164 if(count > 0xffff)
166 panicf("Error! The tick interval is too long (%d ms)\n",
167 interval_in_ms);
168 return;
171 /* We are using timer 0 */
173 TSTR &= ~0x01; /* Stop the timer */
174 TSNC &= ~0x01; /* No synchronization */
175 TMDR &= ~0x01; /* Operate normally */
177 TCNT0 = 0; /* Start counting at 0 */
178 GRA0 = count;
179 TCR0 = 0x23; /* Clear at GRA match, sysclock/8 */
181 /* Enable interrupt on level 1 */
182 IPRC = (IPRC & ~0x00f0) | 0x0010;
184 TSR0 &= ~0x01;
185 TIER0 = 0xf9; /* Enable GRA match interrupt */
187 TSTR |= 0x01; /* Start timer 1 */
190 #pragma interrupt
191 void IMIA0(void)
193 int i;
195 /* Run through the list of tick tasks */
196 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
198 if(tick_funcs[i])
200 tick_funcs[i]();
204 current_tick++;
205 wake_up_thread();
207 TSR0 &= ~0x01;
209 #elif CONFIG_CPU == MCF5249
210 static void tick_start(unsigned int interval_in_ms)
212 unsigned int count;
214 count = FREQ/2 * interval_in_ms / 1000 / 16;
216 if(count > 0xffff)
218 panicf("Error! The tick interval is too long (%d ms)\n",
219 interval_in_ms);
220 return;
223 /* We are using timer 0 */
225 TRR0 = count; /* The reference count */
226 TCN0 = 0; /* reset the timer */
227 TMR0 = 0x001d; /* no prescaler, restart, CLK/16, enabled */
229 TER0 = 0xff; /* Clear all events */
231 ICR0 = (ICR0 & 0xff00ffff) | 0x008c0000; /* Interrupt on level 3.0 */
232 IMR &= ~0x200;
235 void TIMER0(void) __attribute__ ((interrupt_handler));
236 void TIMER0(void)
238 int i;
240 /* Run through the list of tick tasks */
241 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
243 if(tick_funcs[i])
245 tick_funcs[i]();
249 current_tick++;
250 wake_up_thread();
252 TER0 = 0xff; /* Clear all events */
255 #elif CONFIG_CPU == TCC730
257 void TIMER0(void)
259 int i;
261 /* Run through the list of tick tasks */
262 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
264 if(tick_funcs[i])
266 tick_funcs[i]();
270 current_tick++;
271 wake_up_thread();
273 /* re-enable timer by clearing the counter */
274 TACON |= 0x80;
277 static void tick_start(unsigned int interval_in_ms)
279 long count;
280 count = (long)FREQ * (long)interval_in_ms / 1000 / 16;
282 if(count > 0xffffL)
284 panicf("Error! The tick interval is too long (%dms->%x)\n",
285 interval_in_ms, count);
286 return;
289 /* Use timer A */
290 TAPRE = 0x0;
291 TADATA = count;
293 TACON = 0x89;
294 /* counter clear; */
295 /* interval mode; */
296 /* TICS = F(osc) / 16 */
297 /* TCS = internal clock */
298 /* enable */
300 /* enable the interrupt */
301 interrupt_vector[2] = TIMER0;
302 IMR0 |= (1<<2);
305 #endif
307 int tick_add_task(void (*f)(void))
309 int i;
310 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
312 /* Add a task if there is room */
313 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
315 if(tick_funcs[i] == NULL)
317 tick_funcs[i] = f;
318 set_irq_level(oldlevel);
319 return 0;
322 set_irq_level(oldlevel);
323 panicf("Error! tick_add_task(): out of tasks");
324 return -1;
327 int tick_remove_task(void (*f)(void))
329 int i;
330 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
332 /* Remove a task if it is there */
333 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
335 if(tick_funcs[i] == f)
337 tick_funcs[i] = NULL;
338 set_irq_level(oldlevel);
339 return 0;
343 set_irq_level(oldlevel);
344 return -1;
347 /****************************************************************************
348 * Simple mutex functions
349 ****************************************************************************/
350 void mutex_init(struct mutex *m)
352 m->locked = false;
355 void mutex_lock(struct mutex *m)
357 /* Wait until the lock is open... */
358 while(m->locked)
359 sleep_thread();
360 wake_up_thread();
362 /* ...and lock it */
363 m->locked = true;
366 void mutex_unlock(struct mutex *m)
368 m->locked = false;