source: rtems/bsps/riscv/riscv/clock/clockdrv.c @ 3be4478f

5
Last change on this file since 3be4478f was 3be4478f, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 26, 2018 at 5:13:28 AM

riscv: Avoid namespace pollution

Remove <rtems/score/riscv-utility.h> include from <rtems/score/cpu.h>
(which is visible via <rtems.h> for example).

Update #3433.

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup bsp_clock
5 *
6 * @brief riscv clock support.
7 */
8
9/*
10 * Copyright (c) 2018 embedded brains GmbH
11 * COPYRIGHT (c) 2015 Hesham Alatary <hesham@alumni.york.ac.uk>
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <rtems/timecounter.h>
36#include <rtems/score/riscv-utility.h>
37
38#include <bsp.h>
39#include <bsp/fatal.h>
40#include <bsp/fdt.h>
41#include <bsp/irq.h>
42
43#include <dev/irq/clint.h>
44
45#include <libfdt.h>
46
47#define CLINT ((volatile clint_regs *) 0x02000000)
48
49/* This is defined in dev/clock/clockimpl.h */
50void Clock_isr(void *arg);
51
52static struct timecounter riscv_clock_tc;
53
54static uint32_t riscv_clock_interval;
55
56static void riscv_clock_at_tick(void)
57{
58  volatile clint_regs *clint;
59  uint64_t cmp;
60
61  clint = CLINT;
62
63  cmp = clint->mtimecmp[0].val_64;
64  cmp += riscv_clock_interval;
65
66#if __riscv_xlen == 32
67  clint->mtimecmp[0].val_32[0] = 0xffffffff;
68  clint->mtimecmp[0].val_32[1] = (uint32_t) (cmp >> 32);
69  clint->mtimecmp[0].val_32[0] = (uint32_t) cmp;
70#elif __riscv_xlen == 64
71  clint->mtimecmp[0].val_64 = cmp;
72#endif
73}
74
75static void riscv_clock_handler_install(proc_ptr new_isr)
76{
77  _CPU_ISR_install_vector(RISCV_MACHINE_TIMER_INTERRUPT,
78                          new_isr,
79                          NULL);
80}
81
82static uint32_t riscv_clock_get_timecount(struct timecounter *tc)
83{
84  volatile clint_regs *clint;
85
86  clint = CLINT;
87  return clint->mtime.val_32[0];
88}
89
90CPU_Counter_ticks _CPU_Counter_read(void)
91{
92  return riscv_clock_get_timecount(NULL);
93}
94
95static uint32_t riscv_clock_get_timebase_frequency(const void *fdt)
96{
97  int node;
98  const uint32_t *val;
99  int len;
100
101  node = fdt_path_offset(fdt, "/cpus");
102  val = fdt_getprop(fdt, node, "timebase-frequency", &len);
103  if (val == NULL || len < 4) {
104    bsp_fatal(RISCV_FATAL_NO_TIMEBASE_FREQUENCY_IN_DEVICE_TREE);
105  }
106
107  return fdt32_to_cpu(*val);
108}
109
110static void riscv_clock_initialize(void)
111{
112  const char *fdt;
113  uint32_t tb_freq;
114  uint64_t us_per_tick;
115
116  fdt = bsp_fdt_get();
117  tb_freq = riscv_clock_get_timebase_frequency(fdt);
118  us_per_tick = rtems_configuration_get_microseconds_per_tick();
119  riscv_clock_interval = (uint32_t) ((tb_freq * us_per_tick) / 1000000);
120
121  riscv_clock_at_tick();
122
123  /* Enable mtimer interrupts */
124  set_csr(mie, MIP_MTIP);
125
126  /* Initialize timecounter */
127  riscv_clock_tc.tc_get_timecount = riscv_clock_get_timecount;
128  riscv_clock_tc.tc_counter_mask = 0xffffffff;
129  riscv_clock_tc.tc_frequency = tb_freq;
130  riscv_clock_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
131  rtems_timecounter_install(&riscv_clock_tc);
132}
133
134CPU_Counter_ticks _CPU_Counter_difference(
135  CPU_Counter_ticks second,
136  CPU_Counter_ticks first
137)
138{
139  return second - first;
140}
141
142#define Clock_driver_support_at_tick() riscv_clock_at_tick()
143
144#define Clock_driver_support_initialize_hardware() riscv_clock_initialize()
145
146#define Clock_driver_support_install_isr(isr) \
147  riscv_clock_handler_install(isr)
148
149#include "../../../shared/dev/clock/clockimpl.h"
Note: See TracBrowser for help on using the repository browser.