Prepend user to subject string
[kugel-rb.git] / firmware / kernel.c
blob221f8a3b38f40a44cb639445ef6cc6e07eabbf19
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 */
254 #endif
256 int tick_add_task(void (*f)(void))
258 int i;
259 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
261 /* Add a task if there is room */
262 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
264 if(tick_funcs[i] == NULL)
266 tick_funcs[i] = f;
267 set_irq_level(oldlevel);
268 return 0;
271 set_irq_level(oldlevel);
272 panicf("Error! tick_add_task(): out of tasks");
273 return -1;
276 int tick_remove_task(void (*f)(void))
278 int i;
279 int oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL);
281 /* Remove a task if it is there */
282 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
284 if(tick_funcs[i] == f)
286 tick_funcs[i] = NULL;
287 set_irq_level(oldlevel);
288 return 0;
292 set_irq_level(oldlevel);
293 return -1;
296 /****************************************************************************
297 * Simple mutex functions
298 ****************************************************************************/
299 void mutex_init(struct mutex *m)
301 m->locked = false;
304 void mutex_lock(struct mutex *m)
306 /* Wait until the lock is open... */
307 while(m->locked)
308 sleep_thread();
309 wake_up_thread();
311 /* ...and lock it */
312 m->locked = true;
315 void mutex_unlock(struct mutex *m)
317 m->locked = false;