rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2012 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <debug.h> |
| 24 | #include <trace.h> |
| 25 | #include <err.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <kernel/thread.h> |
| 28 | #include <platform.h> |
| 29 | #include <platform/timer.h> |
| 30 | #include <arch/arm/cm.h> |
| 31 | |
| 32 | #include <pmc/pmc.h> |
| 33 | #include <tc/tc.h> |
| 34 | |
| 35 | #define MCLK 84000000 /* XXX read this */ |
| 36 | #define TICK_RATE (MCLK / 2) |
| 37 | |
| 38 | #define LOCAL_TRACE 0 |
| 39 | |
| 40 | void sam3_tc0_irq(void) |
| 41 | { |
| 42 | #if 0 |
| 43 | tc_get_status(TC0, 0); |
| 44 | |
| 45 | ticks += 0xffff; |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | void sam_timer_early_init(void) |
| 50 | { |
| 51 | #if 0 |
| 52 | pmc_enable_periph_clk(ID_TC0); |
| 53 | |
| 54 | |
| 55 | uint32_t ul_div; |
| 56 | uint32_t ul_tcclks; |
| 57 | uint32_t ul_sysclk = MCLK; // sysclk_get_cpu_hz(); |
| 58 | |
| 59 | tc_find_mck_divisor(100, ul_sysclk, &ul_div, &ul_tcclks, ul_sysclk); |
| 60 | tc_init(TC0, 0, TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_CPCTRG); |
| 61 | tc_write_rc(TC0, 0, (ul_sysclk / ul_div) / 4); |
| 62 | |
| 63 | tc_find_mck_divisor(100, ul_sysclk, &ul_div, &ul_tcclks, ul_sysclk); |
| 64 | tc_init(TC0, 0, TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_CPCTRG); |
| 65 | tc_write_rc(TC0, 0, 0xffff); // slowest we can run |
| 66 | |
| 67 | /* Configure and enable interrupt on RC compare */ |
| 68 | NVIC_SetPriority(ID_TC0, arm_cm_highest_priority()); |
| 69 | NVIC_EnableIRQ((IRQn_Type) ID_TC0); |
| 70 | tc_enable_interrupt(TC0, 0, TC_IER_CPCS); |
| 71 | #endif |
| 72 | |
| 73 | tc_start(TC0, 0); |
| 74 | |
| 75 | arm_cm_systick_init(MCLK); |
| 76 | } |
| 77 | |
| 78 | void sam_timer_init(void) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | |