summaryrefslogtreecommitdiffstats
path: root/bsps/riscv/griscv/include/amba.h
blob: cff3a53d9e6b67dbfd0b32b3d8f547e9b81cb2a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSBSPsRISCVGRLIBAMBA
 */

/*
 *  AMBA Plag & Play Bus Driver Macros
 *
 *  Macros used for AMBA Plug & Play bus scanning
 *
 *  COPYRIGHT (c) 2004.
 *  Gaisler Research
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __AMBA_H__
#define __AMBA_H__

/**
 * @defgroup RTEMSBSPsRISCVGRLIBAMBA RISC-V AMBA Driver Handler
 *
 * @ingroup RTEMSBSPsRISCVGRLIB
 *
 * @ingroup RTEMSBSPsSharedGRLIB
 *
 * @brief AMBA Plag & Play Bus Driver Macros
 *
 * @{
 */

#ifndef GRLIB_IO_AREA
#define GRLIB_IO_AREA 0xfff00000
#endif

#define GRLIB_CONF_AREA 0xff000
#define GRLIB_AHB_SLAVE_CONF_AREA (1 << 11)

#define GRLIB_AHB_CONF_WORDS 8
#define GRLIB_APB_CONF_WORDS 2
#define GRLIB_AHB_MASTERS 64
#define GRLIB_AHB_SLAVES 64
#define GRLIB_APB_SLAVES 16

#if defined(RTEMS_MULTIPROCESSING)
  #define GRLIB_CLOCK_INDEX \
   (rtems_configuration_get_user_multiprocessing_table() ? GRLIB_Cpu_Index : 0)
#else
  #define GRLIB_CLOCK_INDEX 0
#endif

#if defined(RTEMS_SMP)
#define GRLIB_COUNTER_GPTIMER_INDEX (GRLIB_CLOCK_INDEX + 1)
#else
#define GRLIB_COUNTER_GPTIMER_INDEX GRLIB_CLOCK_INDEX
#endif

#define GRLIB_GPTIMER_0_FREQUENCY_SET_BY_BOOT_LOADER 1000000

#define GRLIB_IRQMPSTATUS_CPUNR     28

#include <grlib/ambapp.h>
#include <grlib/grlib.h>

#ifdef __cplusplus
extern "C" {
#endif

extern uint32_t GRLIB_Cpu_Index;
extern const unsigned char GRLIB_mp_irq;

extern volatile struct irqmp_regs *GRLIB_IrqCtrl_Regs;
extern struct ambapp_dev *GRLIB_IrqCtrl_Adev;
extern int GRLIB_IrqCtrl_EIrq;
extern volatile struct gptimer_regs *GRLIB_Timer_Regs;
extern struct ambapp_dev *GRLIB_Timer_Adev;
void gptimer_initialize(void);
void irqmp_initialize(void);

static inline uint32_t grlib_up_counter_frequency(void)
{
  /*
   * For simplicity, assume that the interrupt controller uses the processor
   * clock.  This is at least true on the GR740.
   */
  return ambapp_freq_get(ambapp_plb(), GRLIB_IrqCtrl_Adev);
}

extern rtems_interrupt_lock GRLIB_IrqCtrl_Lock;


static inline uint32_t grlib_get_cpu_count(
  volatile struct irqmp_regs *irqmp
)
{
  uint32_t mpstat = irqmp->mpstat;

  return ((mpstat >> GRLIB_IRQMPSTATUS_CPUNR) & 0xf)  + 1;
}

#define GRLIB_IRQCTRL_ACQUIRE( _lock_context ) \
  rtems_interrupt_lock_acquire( &GRLIB_IrqCtrl_Lock, _lock_context )

#define GRLIB_IRQCTRL_RELEASE( _lock_context ) \
  rtems_interrupt_lock_release( &GRLIB_IrqCtrl_Lock, _lock_context )

#define GRLIB_Cpu_Unmask_interrupt( _source, _cpu ) \
  do { \
    rtems_interrupt_lock_context _lock_context; \
    GRLIB_IRQCTRL_ACQUIRE( &_lock_context ); \
    GRLIB_IrqCtrl_Regs->mask[_cpu]  |= (1U << (_source)); \
    GRLIB_IRQCTRL_RELEASE( &_lock_context ); \
  } while (0)

#define GRLIB_Cpu_Mask_interrupt( _source, _cpu ) \
  do { \
    rtems_interrupt_lock_context _lock_context; \
    GRLIB_IRQCTRL_ACQUIRE( &_lock_context ); \
     GRLIB_IrqCtrl_Regs->mask[_cpu]  &= ~(1U << (_source)); \
    GRLIB_IRQCTRL_RELEASE( &_lock_context ); \
  } while (0)

#define GRLIB_Enable_interrupt_broadcast( _source ) \
  do { \
    rtems_interrupt_lock_context _lock_context; \
    uint32_t _mask = 1U << ( _source ); \
    GRLIB_IRQCTRL_ACQUIRE( &_lock_context ); \
    GRLIB_IrqCtrl_Regs->bcast |= _mask; \
    GRLIB_IRQCTRL_RELEASE( &_lock_context ); \
  } while (0)

#define GRLIB_Disable_interrupt_broadcast( _source ) \
  do { \
    rtems_interrupt_lock_context _lock_context; \
    uint32_t _mask = 1U << ( _source ); \
    GRLIB_IRQCTRL_ACQUIRE( &_lock_context ); \
    GRLIB_IrqCtrl_Regs->bcast &= ~_mask; \
    GRLIB_IRQCTRL_RELEASE( &_lock_context ); \
  } while (0)

#define BSP_Cpu_Is_interrupt_masked( _source, _cpu ) \
     (!(GRLIB_IrqCtrl_Regs->mask[_cpu] & (1U << (_source))))

#ifdef __cplusplus
}
#endif

/** @} */

#endif /* __AMBA_H__ */