blob: c1ee0852585c21786f9dc4804d6a46ba3c6ed79d [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 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 <reg.h>
24#include <trace.h>
25#include <err.h>
26#include <lk/init.h>
27#include <kernel/thread.h>
28#include <platform.h>
29#include <platform/timer.h>
30#include <platform/interrupts.h>
31#include <platform/debug.h>
32#include <sys/types.h>
33#include <target/microblaze-config.h>
34
35#define LOCAL_TRACE 0
36
37#define R_TCSR 0
38#define R_TLR 1
39#define R_TCR 2
40#define R_MAX 4
41
42#define TCSR_MDT (1<<0)
43#define TCSR_UDT (1<<1)
44#define TCSR_GENT (1<<2)
45#define TCSR_CAPT (1<<3)
46#define TCSR_ARHT (1<<4)
47#define TCSR_LOAD (1<<5)
48#define TCSR_ENIT (1<<6)
49#define TCSR_ENT (1<<7)
50#define TCSR_TINT (1<<8)
51#define TCSR_PWMA (1<<9)
52#define TCSR_ENALL (1<<10)
53
54#define TIMER_REG(reg) (*REG32(TIMER_BASEADDR + (reg) * 4))
55
56static platform_timer_callback timer_cb;
57static void *timer_arg;
58
59static uint32_t ticks = 0;
60
61status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, lk_time_t interval)
62{
63 LTRACEF("cb %p, arg %p, interval %u\n", callback, arg, interval);
64
65 uint32_t count = ((uint64_t)TIMER_RATE * interval / 1000);
66
67 LTRACEF("count 0x%x\n", count);
68
69 timer_cb = callback;
70 timer_arg = arg;
71
72 TIMER_REG(R_TCSR) = 0;
73 TIMER_REG(R_TLR) = count;
74 TIMER_REG(R_TCSR) = TCSR_ENIT | TCSR_UDT | TCSR_ARHT | TCSR_ENT;
75
76 return NO_ERROR;
77}
78
79lk_bigtime_t current_time_hires(void)
80{
81 return (lk_bigtime_t)ticks * 10000;
82}
83
84lk_time_t current_time(void)
85{
86 return (lk_time_t)ticks * 10;
87}
88
89enum handler_return timer_irq(void *arg)
90{
91 LTRACE;
92
93 TIMER_REG(R_TCSR) |= TCSR_TINT;
94
95 ticks += 1;
96
97 enum handler_return ret = timer_cb(timer_arg, ticks * 10);
98
99 return ret;
100}
101
102static void timer_init(uint level)
103{
104 LTRACE;
105
106 register_int_handler(TIMER_IRQ, timer_irq, NULL);
107 unmask_interrupt(TIMER_IRQ);
108}
109
110LK_INIT_HOOK(timer, timer_init, LK_INIT_LEVEL_PLATFORM_EARLY + 1);
111
112