Sansa Fuze+: initial commit (bootloader only, LCD basically working)
[kugel-rb.git] / firmware / target / arm / imx233 / timrot-imx233.c
blob64a7c63f24c71b696b237c7d70bad22d06177ffd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2011 by Amaury Pouly
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 "timrot-imx233.h"
22 #include "clkctrl-imx233.h"
24 imx233_timer_fn_t timer_fns[4];
26 #define define_timer_irq(nr) \
27 void INT_TIMER##nr(void) \
28 { \
29 if(timer_fns[nr]) \
30 timer_fns[nr](); \
31 __REG_CLR(HW_TIMROT_TIMCTRL(nr)) = HW_TIMROT_TIMCTRL__IRQ; \
34 define_timer_irq(0)
35 define_timer_irq(1)
36 define_timer_irq(2)
37 define_timer_irq(3)
39 void imx233_setup_timer(unsigned timer_nr, bool reload, unsigned count,
40 unsigned src, unsigned prescale, bool polarity, imx233_timer_fn_t fn)
42 timer_fns[timer_nr] = fn;
44 HW_TIMROT_TIMCTRL(timer_nr) = src | prescale;
45 if(polarity)
46 __REG_SET(HW_TIMROT_TIMCTRL(timer_nr)) = HW_TIMROT_TIMCTRL__POLARITY;
47 if(reload)
49 __REG_SET(HW_TIMROT_TIMCTRL(timer_nr)) = HW_TIMROT_TIMCTRL__RELOAD;
50 /* manual says count - 1 for reload timers */
51 HW_TIMROT_TIMCOUNT(timer_nr) = count - 1;
53 else
54 HW_TIMROT_TIMCOUNT(timer_nr) = count;
55 /* only enable interrupt if function is set */
56 if(fn != NULL)
58 /* enable interrupt */
59 imx233_enable_interrupt(INT_SRC_TIMER(timer_nr), true);
60 /* clear irq bit and enable */
61 __REG_CLR(HW_TIMROT_TIMCTRL(timer_nr)) = HW_TIMROT_TIMCTRL__IRQ;
62 __REG_SET(HW_TIMROT_TIMCTRL(timer_nr)) = HW_TIMROT_TIMCTRL__IRQ_EN;
64 else
65 imx233_enable_interrupt(INT_SRC_TIMER(timer_nr), false);
66 /* finally update */
67 __REG_SET(HW_TIMROT_TIMCTRL(timer_nr)) = HW_TIMROT_TIMCTRL__UPDATE;
70 void imx233_timrot_init(void)
72 __REG_CLR(HW_TIMROT_ROTCTRL) = __BLOCK_CLKGATE;
73 __REG_CLR(HW_TIMROT_ROTCTRL) = __BLOCK_SFTRST;
74 /* enable xtal path to timrot */
75 imx233_enable_timrot_xtal_clk32k(true);