summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/hal/source/msgbox/msgbox_sx/msgbox_sx.c
blob: 93312445bba045cf83f9f8198303bb825f73f438 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "hal_msgbox.h"
#include "bitops.h"
#include "msgbox_sx.h"
#include "compiler_attributes.h"


#define MSGBOX_CTRL_REG_OFFSET    0x00
#define MSGBOX_VER_REG_OFFSET     0x10
#define MSGBOX_IRQ_EN_REG_OFFSET  0x40
#define MSGBOX_IRQ_STATUS_OFFSET  0x50
#define MSGBOX_FIFO_STATUS_OFFSET 0x100
#define MSGBOX_FIFO_STATUS_MASK   BIT(0)
#define MSGBOX_MSG_STATUS_OFFSET  0x140
#define MSGBOX_MSG_STATUS_MASK    0x0f
#define MSGBOX_MSG_QUEUE_OFFSET   0x180

static int msg_queue_t_set(struct messagebox *mb, int queue)
{
    int user = mb->this_user;
    int offset_reg = (queue / 4 ) * 4 + MSGBOX_CTRL_REG_OFFSET;
    int offset_bit_t = 4 + ((queue % 4) * 8 );
    int offset_bit_r = ((queue % 4) * 8 );
    unsigned long *reg = (unsigned long *)(mb->base + offset_reg);
    unsigned long data;

    if (queue >= MSGBOX_MAX_QUEUE || user >= MSGBOX_MAX_USER)
        return -EINVAL;

    data = *reg;
    data &= ~(BIT(offset_bit_r) | BIT(offset_bit_t));

    if (user)
        *reg = data | BIT(offset_bit_t);
    else
        *reg = data | BIT(offset_bit_r);

    return 0;
}

static int msg_queue_r_set(struct messagebox *mb, int queue)
{
    int user = mb->this_user;
    int offset_reg = (queue / 4 ) * 4 + MSGBOX_CTRL_REG_OFFSET;
    int offset_bit_t = 4 + ((queue % 4) * 8 );
    int offset_bit_r = ((queue % 4) * 8 );
    unsigned long *reg = (unsigned long *)(mb->base + offset_reg);
    unsigned long data;

    if (queue >= MSGBOX_MAX_QUEUE || user >= MSGBOX_MAX_USER)
        return -EINVAL;

    data = *reg;
    data &= ~(BIT(offset_bit_r) | BIT(offset_bit_t));

    if (user)
        *reg = data | BIT(offset_bit_r);
    else
        *reg = data | BIT(offset_bit_t);

    return 0;
}

static int msg_queue_irq_t(struct messagebox *mb, int queue, int en)
{
    int user = mb->this_user;
    long *reg = (long *)(mb->base + MSGBOX_IRQ_EN_REG_OFFSET + user * 0x20);
    int offset_bit = 1 + (queue << 1);

    if (queue >= MSGBOX_MAX_QUEUE || user >= MSGBOX_MAX_USER)
        return -EINVAL;

    if (en)
        *reg = SET_BIT(*reg, offset_bit);
    else
        *reg = CLR_BIT(*reg, offset_bit);

    return 0;
}

static int msg_queue_irq_r(struct messagebox *mb, int queue, int en)
{
    int user = mb->this_user;
    long *reg = (long *)(mb->base + MSGBOX_IRQ_EN_REG_OFFSET + user * 0x20);
    int offset_bit = (queue << 1);

    if (queue >= MSGBOX_MAX_QUEUE || user >= MSGBOX_MAX_USER)
        return -EINVAL;

    if (en)
        *reg = SET_BIT(*reg, offset_bit);
    else
        *reg = CLR_BIT(*reg, offset_bit);

    return 0;
}

/*
 * static int msg_queue_status_pending(struct messagebox *mb, int queue)
 * {
 *         int user = mb->this_user;
 *
 *         if (queue >= MSGBOX_MAX_QUEUE || user >= MSGBOX_MAX_USER)
 *                 return -EINVAL;
 *
 *         return 0;
 * }
 */

static __maybe_unused int msg_queue_is_full(struct messagebox *mb, int queue)
{
    long *reg = (long *)(mb->base + MSGBOX_FIFO_STATUS_OFFSET + queue * 4);

    if (queue >= MSGBOX_MAX_QUEUE)
        return -EINVAL;

    if (*reg & MSGBOX_FIFO_STATUS_MASK)
        return 1;

    return 0;
}

static int msg_queue_msg_cnt_sx(struct messagebox *mb, int queue)
{
    long *reg = (long *)(mb->base + MSGBOX_MSG_STATUS_OFFSET + 4 * queue);

    if (queue >= MSGBOX_MAX_QUEUE)
        return -EINVAL;

    return *reg & MSGBOX_MSG_STATUS_MASK;
}

static int msg_queue_read_sx(struct messagebox *mb, int queue, unsigned long *read)
{
    long *reg = (long *)(mb->base + MSGBOX_MSG_QUEUE_OFFSET + 4 * queue);

    *read = *reg;

    return 0;
}

static int msg_queue_write_sx(struct messagebox *mb, int queue,
               unsigned long write)
{
    long *reg = (long *)(mb->base + MSGBOX_MSG_QUEUE_OFFSET + 4 * queue);

    *reg = write;

    return 0;
}

static inline unsigned long msg_irq_enable_get(struct messagebox *mb)
{
    int user = mb->this_user;
    long *reg = (long *)(mb->base + MSGBOX_IRQ_EN_REG_OFFSET + user * 0x20);

    return *reg;
}

static inline unsigned long msg_irq_status(struct messagebox *mb)
{
    int user = mb->this_user;
    long *reg = (long *)(mb->base + MSGBOX_IRQ_STATUS_OFFSET + 0x20 * user);

    if (user >= MSGBOX_MAX_USER)
        return -EINVAL;

    return *reg;
}

static inline int msg_irq_clear(struct messagebox *mb, unsigned long clr)
{
    int user = mb->this_user;
    long *reg = (long *)(mb->base + MSGBOX_IRQ_STATUS_OFFSET + 0x20 * user);

    if (user >= MSGBOX_MAX_USER)
        return -EINVAL;

    *reg = clr;

    return 0;
}

int msg_irq_handler_sx(int i, void *d)
{
    int channel;
    struct messagebox *mb = d;
    unsigned long status = msg_irq_status(mb);
    unsigned long enable = msg_irq_enable_get(mb);


    for (channel = 0; channel < MSGBOX_MAX_QUEUE; channel++) {
        unsigned long tbit = channel << 1;
        struct msg_channel *h = &mb->msg_handler[channel];

        tbit = (1 << tbit) | (1 << (tbit + 1));
        if ((tbit & status & enable) && h->cb_of_msg_queue) {
            h->cb_of_msg_queue(channel, h);
        }
    }

    msg_irq_clear(mb, status & enable);

    return 0;
}

static inline int
msg_channel_set_direction_sx(struct messagebox *mb, int channel,
                 enum msgbox_channel_direction dir)
{
    if (dir == MSGBOX_CHANNEL_SEND)
        return msg_queue_t_set(mb, channel);
    else
        return msg_queue_r_set(mb, channel);
}

static inline int msg_channel_irq_set_sx(struct messagebox *mb, int channel,
                     int enable)
{
    struct msg_channel *ch;

    if (channel >= MSGBOX_MAX_QUEUE)
        return -EINVAL;

    ch = &mb->msg_handler[channel];
    if (ch->dir == MSGBOX_CHANNEL_SEND)
        return msg_queue_irq_t(mb, channel, enable);
    else
        return msg_queue_irq_r(mb, channel, enable);
}

int msg_ops_init_sx(struct messagebox *mb)
{

    mb->channel_set_direction = msg_channel_set_direction_sx;
    mb->channel_irq_set = msg_channel_irq_set_sx;
    mb->channel_fifo_len = msg_queue_msg_cnt_sx;
    mb->channel_read = msg_queue_read_sx;
    mb->channel_write = msg_queue_write_sx;

    return 0;
}