summaryrefslogtreecommitdiff
path: root/bsp/rockchip/dm/mailbox/mailbox-rockchip.c
blob: bf58a245ef08208876ec3853e066b97deeb66fb7 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-09-23     GuEe-GUI     first version
 */

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

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

#define MAILBOX_A2B_INTEN               0x00
#define MAILBOX_A2B_STATUS              0x04
#define MAILBOX_A2B_CMD(x)              (0x08 + (x) * 8)
#define MAILBOX_A2B_DAT(x)              (0x0c + (x) * 8)

#define MAILBOX_B2A_INTEN               0x28
#define MAILBOX_B2A_STATUS              0x2C
#define MAILBOX_B2A_CMD(x)              (0x30 + (x) * 8)
#define MAILBOX_B2A_DAT(x)              (0x34 + (x) * 8)

#define MAILBOX_V2_A2B_INTEN            MAILBOX_A2B_INTEN
#define MAILBOX_V2_A2B_STATUS           MAILBOX_A2B_STATUS
#define MAILBOX_V2_A2B_CMD              0x08
#define MAILBOX_V2_A2B_DAT              0x0c

#define MAILBOX_V2_B2A_INTEN            0x10
#define MAILBOX_V2_B2A_STATUS           0x14
#define MAILBOX_V2_B2A_CMD              0x18
#define MAILBOX_V2_B2A_DAT              0x1c

#define MAILBOX_V2_TRIGGER_SHIFT        8
#define MAILBOX_V2_TRIGGER_MASK         RT_BIT(8)

#define MAILBOX_V2_INTEN_TX_DONE        RT_BIT(0)
#define MAILBOX_V2_INTEN_RX_DONE        RT_BIT(1)
#define MAILBOX_V2_INTEN_RX_DONE_SHIFT  1

#define MAILBOX_V2_STATUS_TX_DONE       RT_BIT(0)
#define MAILBOX_V2_STATUS_RX_DONE       RT_BIT(1)
#define MAILBOX_V2_STATUS_MASK          RT_GENMASK(1, 0)

#define MAILBOX_POLLING_MS              5 /* default polling interval 5ms */
#define BIT_WRITEABLE_SHIFT             16

struct rockchip_mbox;

struct rockchip_mbox_reg
{
    rt_uint32_t tx_int;
    rt_uint32_t tx_sts;
    rt_uint32_t tx_cmd;
    rt_uint32_t tx_dat;
    rt_uint32_t rx_int;
    rt_uint32_t rx_sts;
    rt_uint32_t rx_cmd;
    rt_uint32_t rx_dat;
};

struct rockchip_mbox_msg
{
    rt_uint32_t cmd;
    rt_uint32_t data;
};

struct rockchip_mbox_soc_data
{
    int num_chans;
    struct rockchip_mbox_reg reg_a2b;
    struct rockchip_mbox_reg reg_b2a;
    const struct rt_mbox_controller_ops *ops;
    void (*isr)(int irqno, void *param);
    void (*thread_isr)(void *param);
};

struct rockchip_mbox_chan
{
    int idx;
    int irq;
    struct rockchip_mbox_msg *msg;
    struct rockchip_mbox *rk_mbox;
};

struct rockchip_mbox
{
    struct rt_mbox_controller parent;

    void *regs;
    struct rt_clk *pclk;
    struct rt_thread *irq_thread;
    const struct rockchip_mbox_reg *reg;

    rt_uint32_t buf_size;
    rt_bool_t txdone_irq;
    rt_bool_t trigger_method;
    struct rockchip_mbox_chan chans[];
};

#define raw_to_rockchip_mbox(raw) rt_container_of(raw, struct rockchip_mbox, parent)

rt_inline rt_uint32_t rockchip_mbox_readl(struct rockchip_mbox *rk_mbox, int offset)
{
    return HWREG32(rk_mbox->regs + offset);
}

rt_inline void rockchip_mbox_writel(struct rockchip_mbox *rk_mbox, int offset,
        rt_uint32_t value)
{
    HWREG32(rk_mbox->regs + offset) = value;
}

static rt_err_t rockchip_mbox_request(struct rt_mbox_chan *chan)
{
    int idx;
    rt_uint32_t value;
    struct rockchip_mbox *rk_mbox = raw_to_rockchip_mbox(chan->ctrl);

    idx = chan - rk_mbox->parent.chans;

    /* Enable the corresponding B2A interrupts */
    value = rockchip_mbox_readl(rk_mbox, MAILBOX_B2A_INTEN);
    value |= RT_BIT(idx);
    rockchip_mbox_writel(rk_mbox, MAILBOX_B2A_INTEN, value);

    return RT_EOK;
}

static void rockchip_mbox_release(struct rt_mbox_chan *chan)
{
    int idx;
    rt_uint32_t value;
    struct rockchip_mbox *rk_mbox = raw_to_rockchip_mbox(chan->ctrl);

    idx = chan - rk_mbox->parent.chans;

    /* Disable the corresponding B2A interrupts */
    value = rockchip_mbox_readl(rk_mbox, MAILBOX_B2A_INTEN);
    value &= ~RT_BIT(idx);
    rockchip_mbox_writel(rk_mbox, MAILBOX_B2A_INTEN, value);

    rk_mbox->chans[idx].msg = RT_NULL;
}

static rt_err_t rockchip_mbox_send(struct rt_mbox_chan *chan, const void *data)
{
    int idx;
    struct rockchip_mbox_msg *msg = (void *)data;
    struct rockchip_mbox *rk_mbox = raw_to_rockchip_mbox(chan->ctrl);

    idx = chan - rk_mbox->parent.chans;

    if (rockchip_mbox_readl(rk_mbox, MAILBOX_A2B_STATUS) & RT_BIT(idx))
    {
        LOG_E("The mailbox channel(%d) is busy", idx);

        return -RT_EBUSY;
    }

    rk_mbox->chans[idx].msg = msg;

    rockchip_mbox_writel(rk_mbox, MAILBOX_A2B_CMD(idx), msg->cmd);
    rockchip_mbox_writel(rk_mbox, MAILBOX_A2B_DAT(idx), msg->data);

    return RT_EOK;
}

static const struct rt_mbox_controller_ops rockchip_mbox_ops =
{
    .request = rockchip_mbox_request,
    .release = rockchip_mbox_release,
    .send = rockchip_mbox_send,
};

static void rockchip_mbox_thread_isr(void *param)
{
    struct rockchip_mbox_msg *msg;
    struct rockchip_mbox *rk_mbox = param;

    while (RT_TRUE)
    {
        rt_thread_suspend(rk_mbox->irq_thread);
        rt_schedule();

        for (int idx = 0; idx < rk_mbox->parent.num_chans; ++idx)
        {
            msg = rk_mbox->chans[idx].msg;

            if (!msg)
            {
                LOG_E("Chan[%d]: B2A message is NULL", idx);

                break;
            }

            rt_mbox_recv(&rk_mbox->parent.chans[idx], msg);
            rk_mbox->chans[idx].msg = RT_NULL;
        }
    }
}

static void rockchip_mbox_isr(int irqno, void *param)
{
    rt_uint32_t status;
    struct rockchip_mbox_msg *msg;
    struct rockchip_mbox *rk_mbox = param;

    status = rockchip_mbox_readl(rk_mbox, MAILBOX_B2A_STATUS);

    for (int idx = 0; idx < rk_mbox->parent.num_chans; ++idx)
    {
        if ((status & RT_BIT(idx)) && (irqno == rk_mbox->chans[idx].irq))
        {
            msg = rk_mbox->chans[idx].msg;
            msg->cmd = rockchip_mbox_readl(rk_mbox, MAILBOX_B2A_CMD(idx));
            msg->data = rockchip_mbox_readl(rk_mbox, MAILBOX_B2A_DAT(idx));

            /* Clear mbox interrupt */
            rockchip_mbox_writel(rk_mbox, MAILBOX_B2A_STATUS, RT_BIT(idx));

            rt_thread_resume(rk_mbox->irq_thread);

            break;
        }
    }
}

static rt_err_t rockchip_mbox_v2_request(struct rt_mbox_chan *chan)
{
    struct rockchip_mbox *rk_mbox = raw_to_rockchip_mbox(chan->ctrl);

    /* Set the TX interrupt trigger method */
    rockchip_mbox_writel(rk_mbox, rk_mbox->reg->tx_int,
            (1U << (BIT_WRITEABLE_SHIFT + MAILBOX_V2_TRIGGER_SHIFT) |
                    rk_mbox->trigger_method << MAILBOX_V2_TRIGGER_SHIFT));

    /* Enable the tx_done interrupt */
    rockchip_mbox_writel(rk_mbox, rk_mbox->reg->rx_int,
            (1U << BIT_WRITEABLE_SHIFT | MAILBOX_V2_INTEN_TX_DONE));

    /* Enable the rx_done interrupt */
    if (rk_mbox->txdone_irq)
    {
        rockchip_mbox_writel(rk_mbox, rk_mbox->reg->rx_int,
                (1U << (BIT_WRITEABLE_SHIFT + MAILBOX_V2_INTEN_RX_DONE_SHIFT) |
                        MAILBOX_V2_INTEN_RX_DONE));
    }

    return RT_EOK;
}

static void rockchip_mbox_v2_release(struct rt_mbox_chan *chan)
{
    struct rockchip_mbox *rk_mbox = raw_to_rockchip_mbox(chan->ctrl);

    /* Disable the tx_done interrupt */
    rockchip_mbox_writel(rk_mbox, rk_mbox->reg->rx_int, 1U << BIT_WRITEABLE_SHIFT);

    /* Disable the rx_done interrupt */
    if (rk_mbox->txdone_irq)
    {
        rockchip_mbox_writel(rk_mbox, rk_mbox->reg->rx_int,
                1U << (BIT_WRITEABLE_SHIFT + MAILBOX_V2_INTEN_RX_DONE_SHIFT));
    }
}

static rt_err_t rockchip_mbox_v2_send(struct rt_mbox_chan *chan, const void *data)
{
    struct rockchip_mbox_msg *msg = (void *)data;
    struct rockchip_mbox *rk_mbox = raw_to_rockchip_mbox(chan->ctrl);

    if (rockchip_mbox_readl(rk_mbox, rk_mbox->reg->tx_sts) & MAILBOX_V2_STATUS_TX_DONE)
    {
        LOG_E("The mailbox channel(%d) is busy", 0);

        return -RT_EBUSY;
    }

    rk_mbox->chans[0].msg = msg;

    if (rk_mbox->trigger_method)
    {
        rockchip_mbox_writel(rk_mbox, rk_mbox->reg->tx_cmd, msg->cmd);
        rockchip_mbox_writel(rk_mbox, rk_mbox->reg->tx_dat, msg->data);
    }
    else
    {
        rockchip_mbox_writel(rk_mbox, rk_mbox->reg->tx_cmd, msg->cmd);
    }

    return RT_EOK;
}

static const struct rt_mbox_controller_ops rockchip_mbox_v2_ops =
{
    .request = rockchip_mbox_v2_request,
    .release = rockchip_mbox_v2_release,
    .send = rockchip_mbox_v2_send,
};

static void rockchip_mbox_v2_thread_isr(void *param)
{
    struct rockchip_mbox_msg *msg;
    struct rockchip_mbox *rk_mbox = param;

    while (RT_TRUE)
    {
        rt_thread_suspend(rk_mbox->irq_thread);
        rt_schedule();

        msg = rk_mbox->chans[0].msg;

        if (!msg)
        {
            LOG_E("Chan[%d]: B2A message is NULL", 0);

            continue;
        }

        rt_mbox_recv(&rk_mbox->parent.chans[0], msg);
        rk_mbox->chans[0].msg = RT_NULL;
    }
}

static void rockchip_mbox_v2_isr(int irqno, void *param)
{
    rt_uint32_t status;
    struct rockchip_mbox_msg *msg;
    struct rockchip_mbox *rk_mbox = param;

    status = rockchip_mbox_readl(rk_mbox, rk_mbox->reg->rx_sts);

    if (!(status & MAILBOX_V2_STATUS_MASK))
    {
        return;
    }

    if (status & MAILBOX_V2_STATUS_TX_DONE)
    {
        msg = rk_mbox->chans[0].msg;

        /* Get cmd/data from the channel */
        msg->cmd = rockchip_mbox_readl(rk_mbox, rk_mbox->reg->rx_cmd);
        msg->data = rockchip_mbox_readl(rk_mbox, rk_mbox->reg->rx_dat);

        /* Clear the tx_done interrupt */
        rockchip_mbox_writel(rk_mbox, rk_mbox->reg->rx_sts, MAILBOX_V2_STATUS_TX_DONE);

        rt_thread_resume(rk_mbox->irq_thread);
    }

    if (status & MAILBOX_V2_STATUS_RX_DONE)
    {
        if (rk_mbox->txdone_irq)
        {
            rt_mbox_send_done(&rk_mbox->parent.chans[0], 0);
        }

        /* Clear the rx_done interrupt */
        rockchip_mbox_writel(rk_mbox, rk_mbox->reg->rx_sts, MAILBOX_V2_STATUS_RX_DONE);
    }
}

static void rockchip_mbox_free_resource(struct rockchip_mbox *rk_mbox)
{
    if (rk_mbox->regs)
    {
        rt_iounmap(rk_mbox->regs);
    }

    if (!rt_is_err_or_null(rk_mbox->pclk))
    {
        rt_clk_disable_unprepare(rk_mbox->pclk);
        rt_clk_put(rk_mbox->pclk);
    }

    if (rk_mbox->irq_thread)
    {
        rt_thread_delete(rk_mbox->irq_thread);
    }

    rt_free(rk_mbox);
}

static rt_err_t rockchip_mbox_probe(struct rt_platform_device *pdev)
{
    rt_err_t err;
    char chan_name[RT_NAME_MAX];
    rt_uint64_t io_addr, io_size;
    struct rockchip_mbox *rk_mbox;
    struct rockchip_mbox_chan *chan;
    struct rt_device *dev = &pdev->parent;
    const struct rockchip_mbox_soc_data *soc_data = pdev->id->data;

    rk_mbox = rt_calloc(1, sizeof(*rk_mbox) +
            soc_data->num_chans * sizeof(struct rockchip_mbox_chan));

    if (!rk_mbox)
    {
        return -RT_ENOMEM;
    }

    if (rt_dm_dev_prop_read_bool(dev, "rockchip,tx-direction-b2a"))
    {
        rk_mbox->reg = &soc_data->reg_b2a;
    }
    else
    {
        rk_mbox->reg = &soc_data->reg_a2b;
    }

    if (rt_dm_dev_prop_read_bool(dev, "rockchip,txdone-ack"))
    {
        rk_mbox->txdone_irq = RT_FALSE;
    }
    else if (rt_dm_dev_prop_read_bool(dev, "rockchip,txdone-irq"))
    {
        rk_mbox->txdone_irq = RT_TRUE;
    }

    rk_mbox->trigger_method = !rt_dm_dev_prop_read_bool(dev, "rockchip,enable-cmd-trigger");

    if ((err = rt_dm_dev_get_address(dev, 0, &io_addr, &io_size)))
    {
        goto _fail;
    }

    rk_mbox->regs = rt_ioremap((void *)io_addr, (size_t)io_size);

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

    rk_mbox->pclk = rt_clk_get_by_name(dev, "pclk_mailbox");

    if (rt_is_err(rk_mbox->pclk))
    {
        err = rt_ptr_err(rk_mbox->pclk);

        goto _fail;
    }

    if ((err = rt_clk_prepare_enable(rk_mbox->pclk)))
    {
        goto _fail;
    }

    rk_mbox->irq_thread = rt_thread_create("rk_mbox", soc_data->thread_isr,
            rk_mbox, DM_THREAD_STACK_SIZE, RT_THREAD_PRIORITY_MAX / 2, 10);

    if (!rk_mbox->irq_thread)
    {
        LOG_E("Create Mailbox IRQ thread fail");
        goto _fail;
    }

    rt_thread_startup(rk_mbox->irq_thread);

    chan = &rk_mbox->chans[0];

    for (int i = 0; i < soc_data->num_chans; ++i, ++chan)
    {
        int irq = rt_dm_dev_get_irq(dev, i);

        if (irq < 0)
        {
            err = irq;

            goto _fail;
        }

        rt_snprintf(chan_name, sizeof(chan_name), "rk_mbox-%d", i);

        rt_hw_interrupt_install(irq, soc_data->isr, rk_mbox, chan_name);
        rt_hw_interrupt_umask(irq);

        chan->idx = i;
        chan->irq = irq;
        chan->rk_mbox = rk_mbox;
    }

    rk_mbox->buf_size = io_size / (soc_data->num_chans * 2);

    dev->user_data = rk_mbox;

    rk_mbox->parent.dev = dev;
    rk_mbox->parent.num_chans = soc_data->num_chans;
    rk_mbox->parent.ops = soc_data->ops;

    if ((err = rt_mbox_controller_register(&rk_mbox->parent)))
    {
        goto _fail;
    }

    return RT_EOK;

_fail:
    rockchip_mbox_free_resource(rk_mbox);

    return err;
}

static rt_err_t rockchip_mbox_remove(struct rt_platform_device *pdev)
{
    struct rockchip_mbox_chan *chan;
    struct rockchip_mbox *rk_mbox = pdev->parent.user_data;

    chan = &rk_mbox->chans[0];

    for (int i = 0; i < rk_mbox->parent.num_chans; ++i, ++chan)
    {
        rt_hw_interrupt_mask(chan->irq);
        rt_pic_detach_irq(chan->irq, rk_mbox);
    }

    rt_mbox_controller_unregister(&rk_mbox->parent);

    rockchip_mbox_free_resource(rk_mbox);

    return RT_EOK;
}

static const struct rockchip_mbox_soc_data rk3368_data =
{
    .num_chans = 4,
    .ops = &rockchip_mbox_ops,
    .isr = rockchip_mbox_isr,
    .thread_isr = rockchip_mbox_thread_isr,
};

static const struct rockchip_mbox_soc_data rk3576_data =
{
    .num_chans = 1,
    .reg_a2b =
    {
        MAILBOX_V2_A2B_INTEN, MAILBOX_V2_A2B_STATUS,
        MAILBOX_V2_A2B_CMD, MAILBOX_V2_A2B_DAT,
        MAILBOX_V2_B2A_INTEN, MAILBOX_V2_B2A_STATUS,
        MAILBOX_V2_B2A_CMD, MAILBOX_V2_B2A_DAT
    },
    .reg_b2a =
    {
        MAILBOX_V2_B2A_INTEN, MAILBOX_V2_B2A_STATUS,
        MAILBOX_V2_B2A_CMD, MAILBOX_V2_B2A_DAT,
        MAILBOX_V2_A2B_INTEN, MAILBOX_V2_A2B_STATUS,
        MAILBOX_V2_A2B_CMD, MAILBOX_V2_A2B_DAT
    },
    .ops = &rockchip_mbox_v2_ops,
    .isr = rockchip_mbox_v2_isr,
    .thread_isr = rockchip_mbox_v2_thread_isr,
};

static const struct rt_ofw_node_id rockchip_mbox_ofw_ids[] =
{
    { .compatible = "rockchip,rk3368-mailbox", .data = &rk3368_data },
    { .compatible = "rockchip,rk3576-mailbox", .data = &rk3576_data },
    { /* sentinel */ }
};

static struct rt_platform_driver rockchip_mbox_driver =
{
    .name = "mailbox-rockchip",
    .ids = rockchip_mbox_ofw_ids,

    .probe = rockchip_mbox_probe,
    .remove = rockchip_mbox_remove,
};
RT_PLATFORM_DRIVER_EXPORT(rockchip_mbox_driver);