summaryrefslogtreecommitdiff
path: root/bsp/spacemit/dm/mailbox/mailbox-k1x.c
blob: 133908e9f064603c7a4b1813fdb7b43ebd0fdd4d (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-5-1       GuEe-GUI     first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#include <drivers/clk.h>

#define DBG_TAG "mailbox.spacemit"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#define SPACEMIT_NUM_CHANNELS   4
#define SPACEMIT_TX_ACK_OFFSET  4

#define MBOX_REG_IPC_DW         0x00
#define MBOX_REG_IPC_WDR        0x04
#define MBOX_REG_ISRW           0x08
#define MBOX_REG_ICR            0x0C
#define MBOX_REG_IIR            0x10
#define MBOX_REG_RDR            0x14

struct spacemit_mailbox
{
    struct rt_mbox_controller parent;

    int irq;
    void *regs;
    rt_bool_t clk_enabled;
    struct rt_clk *clk;
    struct rt_reset_control *rstc;
};

#define raw_to_spacemit_mailbox(raw) rt_container_of(raw, struct spacemit_mailbox, parent)

static rt_err_t spacemit_mailbox_request(struct rt_mbox_chan *chan)
{
    return RT_EOK;
}

static void spacemit_mailbox_release(struct rt_mbox_chan *chan)
{
    struct spacemit_mailbox *sp_mbox = raw_to_spacemit_mailbox(chan->ctrl);

    HWREG32(sp_mbox->regs + MBOX_REG_ICR) =
            RT_BIT(chan - sp_mbox->parent.chans);
}

static rt_err_t spacemit_mailbox_send(struct rt_mbox_chan *chan, const void *data)
{
    struct spacemit_mailbox *sp_mbox = raw_to_spacemit_mailbox(chan->ctrl);

    HWREG32(sp_mbox->regs + MBOX_REG_ISRW) =
            RT_BIT(chan - sp_mbox->parent.chans);

    return RT_EOK;
}

static rt_bool_t spacemit_mailbox_peek(struct rt_mbox_chan *chan)
{
    struct spacemit_mailbox *sp_mbox = raw_to_spacemit_mailbox(chan->ctrl);

    return !!(HWREG32(sp_mbox->regs + MBOX_REG_RDR) &
            RT_BIT(chan - sp_mbox->parent.chans));
}

static const struct rt_mbox_controller_ops spacemit_mailbox_ops =
{
    .request = spacemit_mailbox_request,
    .release = spacemit_mailbox_release,
    .send = spacemit_mailbox_send,
    .peek = spacemit_mailbox_peek,
};

static void spacemit_mailbox_isr(int irqno, void *param)
{
    rt_uint32_t status, msg = 0;
    struct rt_mbox_chan *chan;
    struct spacemit_mailbox *sp_mbox = param;

    HWREG32(sp_mbox->regs + MBOX_REG_IIR) = 0;

    status = HWREG32(sp_mbox->regs + MBOX_REG_IIR);

    if (!(status & 0xff))
    {
        return;
    }

    for (int i = SPACEMIT_TX_ACK_OFFSET; i < SPACEMIT_NUM_CHANNELS + SPACEMIT_TX_ACK_OFFSET; ++i)
    {
        chan = &sp_mbox->parent.chans[i - SPACEMIT_TX_ACK_OFFSET];

        if (!(status & RT_BIT(i)))
        {
            continue;
        }

        /* Clear the irq pending */
        HWREG32(sp_mbox->regs + MBOX_REG_ICR) = 1 << i;

        rt_mbox_send_done(chan, 0);
    }

    for (int i = 0; i < SPACEMIT_NUM_CHANNELS; ++i)
    {
        chan = &sp_mbox->parent.chans[i];

        if (!(status & RT_BIT(i)))
        {
            continue;
        }

        rt_mbox_recv(chan, &msg);

        /* Clear the irq pending */
        HWREG32(sp_mbox->regs + MBOX_REG_ICR) = RT_BIT(i);

        /* Then send an ack */
        HWREG32(sp_mbox->regs + MBOX_REG_ISRW) = RT_BIT(i + SPACEMIT_TX_ACK_OFFSET);
    }
}

static void spacemit_mailbox_free_resource(struct spacemit_mailbox *sp_mbox)
{
    if (sp_mbox->regs)
    {
        rt_iounmap(sp_mbox->regs);
    }

    if (!rt_is_err_or_null(sp_mbox->clk))
    {
        if (sp_mbox->clk_enabled)
        {
            rt_clk_disable_unprepare(sp_mbox->clk);
        }
        rt_clk_put(sp_mbox->clk);
    }

    if (!rt_is_err_or_null(sp_mbox->rstc))
    {
        rt_reset_control_put(sp_mbox->rstc);
    }

    rt_free(sp_mbox);
}

static rt_err_t spacemit_mailbox_probe(struct rt_platform_device *pdev)
{
    rt_err_t err;
    struct rt_device *dev = &pdev->parent;
    struct spacemit_mailbox *sp_mbox = rt_calloc(1, sizeof(*sp_mbox));

    if (!sp_mbox)
    {
        return -RT_ENOMEM;
    }

    sp_mbox->regs = rt_dm_dev_iomap(dev, 0);

    if (!sp_mbox->regs)
    {
        err = -RT_EIO;
        goto _fail;
    }

    sp_mbox->clk = rt_clk_get_by_name(dev, "core");

    if (rt_is_err(sp_mbox->clk))
    {
        err = rt_ptr_err(sp_mbox->clk);
        goto _fail;
    }

    if ((err = rt_clk_prepare_enable(sp_mbox->clk)))
    {
        goto _fail;
    }
    sp_mbox->clk_enabled = RT_TRUE;

    sp_mbox->rstc = rt_reset_control_get_by_name(dev, "core_reset");

    if (rt_is_err(sp_mbox->rstc))
    {
        err = rt_ptr_err(sp_mbox->rstc);
        goto _fail;
    }

    if ((err = rt_reset_control_deassert(sp_mbox->rstc)))
    {
        goto _fail;
    }

    sp_mbox->irq = rt_dm_dev_get_irq(dev, 0);

    if (sp_mbox->irq < 0)
    {
        err = sp_mbox->irq;
        goto _fail;
    }

    rt_hw_interrupt_install(sp_mbox->irq, spacemit_mailbox_isr, sp_mbox, "sp_mbox");
    rt_hw_interrupt_umask(sp_mbox->irq);

    dev->user_data = sp_mbox;

    sp_mbox->parent.dev = dev;
    sp_mbox->parent.num_chans = SPACEMIT_NUM_CHANNELS;
    sp_mbox->parent.ops = &spacemit_mailbox_ops;

    if ((err = rt_mbox_controller_register(&sp_mbox->parent)))
    {
        goto _free_irq;
    }

    return RT_EOK;

_free_irq:
    rt_hw_interrupt_mask(sp_mbox->irq);
    rt_pic_detach_irq(sp_mbox->irq, sp_mbox);

_fail:
    spacemit_mailbox_free_resource(sp_mbox);

    return err;
}

static rt_err_t spacemit_mailbox_remove(struct rt_platform_device *pdev)
{
    struct spacemit_mailbox *sp_mbox = pdev->parent.user_data;

    rt_hw_interrupt_mask(sp_mbox->irq);
    rt_pic_detach_irq(sp_mbox->irq, sp_mbox);

    rt_mbox_controller_unregister(&sp_mbox->parent);

    spacemit_mailbox_free_resource(sp_mbox);

    return RT_EOK;
}

static const struct rt_ofw_node_id spacemit_mailbox_ofw_ids[] =
{
    { .compatible = "spacemit,k1-x-mailbox", },
    { /* sentinel */ }
};

static struct rt_platform_driver spacemit_mailbox_driver =
{
    .name = "mailbox-spacemit",
    .ids = spacemit_mailbox_ofw_ids,

    .probe = spacemit_mailbox_probe,
    .remove = spacemit_mailbox_remove,
};

static int spacemit_mailbox_drv_register(void)
{
    rt_platform_driver_register(&spacemit_mailbox_driver);

    return 0;
}
INIT_SUBSYS_EXPORT(spacemit_mailbox_drv_register);