Michael Sevakis | dd88663 | 2008-04-02 05:17:05 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2007 by Michael Sevakis |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Michael Sevakis | dd88663 | 2008-04-02 05:17:05 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "system.h" |
| 23 | #include "avic-imx31.h" |
Michael Sevakis | 0b1d7e7 | 2008-04-11 08:51:27 +0000 | [diff] [blame] | 24 | #include "spi-imx31.h" |
| 25 | #include "mc13783.h" |
| 26 | #include "clkctl-imx31.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 27 | #include "kernel.h" |
| 28 | #include "thread.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 29 | |
| 30 | extern void (*tick_funcs[MAX_NUM_TICK_TASKS])(void); |
| 31 | |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 32 | static __attribute__((interrupt("IRQ"))) void EPIT1_HANDLER(void) |
| 33 | { |
| 34 | int i; |
| 35 | |
Michael Sevakis | dd88663 | 2008-04-02 05:17:05 +0000 | [diff] [blame] | 36 | EPITSR1 = EPITSR_OCIF; /* Clear the pending status */ |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 37 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 38 | /* Run through the list of tick tasks */ |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 39 | for(i = 0;i < MAX_NUM_TICK_TASKS;i++) |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 40 | { |
| 41 | if(tick_funcs[i]) |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 42 | tick_funcs[i](); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | current_tick++; |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 48 | void tick_start(unsigned int interval_in_ms) |
| 49 | { |
Michael Sevakis | 0b1d7e7 | 2008-04-11 08:51:27 +0000 | [diff] [blame] | 50 | imx31_clkctl_module_clock_gating(CG_EPIT1, CGM_ON_ALL); /* EPIT1 module |
| 51 | clock ON - before writing |
| 52 | regs! */ |
Michael Sevakis | dd88663 | 2008-04-02 05:17:05 +0000 | [diff] [blame] | 53 | EPITCR1 &= ~(EPITCR_OCIEN | EPITCR_EN); /* Disable the counter */ |
Michael Sevakis | 0b1d7e7 | 2008-04-11 08:51:27 +0000 | [diff] [blame] | 54 | CLKCTL_WIMR0 &= ~WIM_IPI_INT_EPIT1; /* Clear wakeup mask */ |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 55 | |
Michael Sevakis | dd88663 | 2008-04-02 05:17:05 +0000 | [diff] [blame] | 56 | /* 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 Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 63 | * Count from load value */ |
Michael Sevakis | dd88663 | 2008-04-02 05:17:05 +0000 | [diff] [blame] | 64 | 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 Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 71 | avic_enable_int(EPIT1, IRQ, 7, EPIT1_HANDLER); |
Michael Sevakis | dd88663 | 2008-04-02 05:17:05 +0000 | [diff] [blame] | 72 | EPITCR1 |= EPITCR_EN; /* Enable the counter */ |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 73 | } |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 74 | |
Michael Sevakis | 0b1d7e7 | 2008-04-11 08:51:27 +0000 | [diff] [blame] | 75 | void kernel_device_init(void) |
| 76 | { |
| 77 | spi_init(); |
| 78 | mc13783_init(); |
| 79 | } |
| 80 | |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 81 | #ifdef BOOTLOADER |
| 82 | void tick_stop(void) |
| 83 | { |
Michael Sevakis | dd88663 | 2008-04-02 05:17:05 +0000 | [diff] [blame] | 84 | avic_disable_int(EPIT1); /* Disable insterrupt */ |
| 85 | EPITCR1 &= ~(EPITCR_OCIEN | EPITCR_EN); /* Disable counter */ |
Michael Sevakis | 0b1d7e7 | 2008-04-11 08:51:27 +0000 | [diff] [blame] | 86 | EPITSR1 = EPITSR_OCIF; /* Clear pending */ |
| 87 | imx31_clkctl_module_clock_gating(CG_EPIT1, CGM_OFF); /* Turn off module clock */ |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 88 | } |
| 89 | #endif |