blob: e7bce8dc2c09be0e3e91442d146f3d387465d531 [file] [log] [blame]
Michael Sevakisdd886632008-04-02 05:17:05 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2007 by Michael Sevakis
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Michael Sevakisdd886632008-04-02 05:17:05 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Michael Sevakis1f021af2008-02-05 04:43:19 +000021#include "config.h"
22#include "system.h"
23#include "avic-imx31.h"
Michael Sevakis0b1d7e72008-04-11 08:51:27 +000024#include "spi-imx31.h"
25#include "mc13783.h"
26#include "clkctl-imx31.h"
Will Robertson590501c2007-09-21 15:51:53 +000027#include "kernel.h"
28#include "thread.h"
Will Robertson590501c2007-09-21 15:51:53 +000029
30extern void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);
31
Michael Sevakis1f021af2008-02-05 04:43:19 +000032static __attribute__((interrupt("IRQ"))) void EPIT1_HANDLER(void)
33{
34 int i;
35
Michael Sevakisdd886632008-04-02 05:17:05 +000036 EPITSR1 = EPITSR_OCIF; /* Clear the pending status */
Michael Sevakis1f021af2008-02-05 04:43:19 +000037
Will Robertson590501c2007-09-21 15:51:53 +000038 /* Run through the list of tick tasks */
Michael Sevakis1f021af2008-02-05 04:43:19 +000039 for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
Will Robertson590501c2007-09-21 15:51:53 +000040 {
41 if(tick_funcs[i])
Will Robertson590501c2007-09-21 15:51:53 +000042 tick_funcs[i]();
Will Robertson590501c2007-09-21 15:51:53 +000043 }
44
45 current_tick++;
Will Robertson590501c2007-09-21 15:51:53 +000046}
47
Michael Sevakis1f021af2008-02-05 04:43:19 +000048void tick_start(unsigned int interval_in_ms)
49{
Michael Sevakis0b1d7e72008-04-11 08:51:27 +000050 imx31_clkctl_module_clock_gating(CG_EPIT1, CGM_ON_ALL); /* EPIT1 module
51 clock ON - before writing
52 regs! */
Michael Sevakisdd886632008-04-02 05:17:05 +000053 EPITCR1 &= ~(EPITCR_OCIEN | EPITCR_EN); /* Disable the counter */
Michael Sevakis0b1d7e72008-04-11 08:51:27 +000054 CLKCTL_WIMR0 &= ~WIM_IPI_INT_EPIT1; /* Clear wakeup mask */
Michael Sevakis1f021af2008-02-05 04:43:19 +000055
Michael Sevakisdd886632008-04-02 05:17:05 +000056 /* mcu_main_clk = 528MHz = 27MHz * 2 * ((9 + 7/9) / 1)
57 * CLKSRC = ipg_clk = 528MHz / 4 / 2 = 66MHz,
58 * EPIT Output Disconnected,
59 * Enabled in wait mode
60 * Prescale 1/2640 for 25KHz
61 * Reload from modulus register,
62 * Compare interrupt enabled,
Michael Sevakis1f021af2008-02-05 04:43:19 +000063 * Count from load value */
Michael Sevakisdd886632008-04-02 05:17:05 +000064 EPITCR1 = EPITCR_CLKSRC_IPG_CLK | EPITCR_WAITEN | EPITCR_IOVW |
65 EPITCR_PRESCALER(2640-1) | EPITCR_RLD | EPITCR_OCIEN |
66 EPITCR_ENMOD;
67
68 EPITLR1 = interval_in_ms*25; /* Count down from interval */
69 EPITCMPR1 = 0; /* Event when counter reaches 0 */
70 EPITSR1 = EPITSR_OCIF; /* Clear any pending interrupt */
Michael Sevakisa07c0342008-02-08 02:20:05 +000071 avic_enable_int(EPIT1, IRQ, 7, EPIT1_HANDLER);
Michael Sevakisdd886632008-04-02 05:17:05 +000072 EPITCR1 |= EPITCR_EN; /* Enable the counter */
Michael Sevakis1f021af2008-02-05 04:43:19 +000073}
Michael Sevakisa07c0342008-02-08 02:20:05 +000074
Michael Sevakis0b1d7e72008-04-11 08:51:27 +000075void kernel_device_init(void)
76{
77 spi_init();
78 mc13783_init();
79}
80
Michael Sevakisa07c0342008-02-08 02:20:05 +000081#ifdef BOOTLOADER
82void tick_stop(void)
83{
Michael Sevakisdd886632008-04-02 05:17:05 +000084 avic_disable_int(EPIT1); /* Disable insterrupt */
85 EPITCR1 &= ~(EPITCR_OCIEN | EPITCR_EN); /* Disable counter */
Michael Sevakis0b1d7e72008-04-11 08:51:27 +000086 EPITSR1 = EPITSR_OCIF; /* Clear pending */
87 imx31_clkctl_module_clock_gating(CG_EPIT1, CGM_OFF); /* Turn off module clock */
Michael Sevakisa07c0342008-02-08 02:20:05 +000088}
89#endif