summaryrefslogtreecommitdiffstats
path: root/firmware/target/mips/ingenic_x1000/dma-x1000.c
blob: 28fd328a85df0ba3d2bcf30ad7b3270bf0589d78 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2021 Aidan MacDonald
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "system.h"
#include "dma-x1000.h"
#include "irq-x1000.h"
#include "x1000/cpm.h"
#include "panic.h"

static dma_cb_func dma_callbacks[DMA_NUM_USED_CHANNELS];

static void dma_no_cb(int event)
{
    (void)event;
    panicf("Unhandled DMA channel interrupt");
}

void dma_init(void)
{
    for(int i = 0; i < DMA_NUM_USED_CHANNELS; ++i)
        dma_callbacks[i] = dma_no_cb;

    jz_writef(CPM_CLKGR, PDMA(0));
    jz_writef(DMA_CTRL, ENABLE(1), HALT(0), AR(0));
    jz_writef(DMA_CTRL, FMSC(1), FSSI(1), FTSSI(1), FUART(1), FAIC(1));
    system_enable_irq(IRQ_PDMA);
    system_enable_irq(IRQ_PDMAD);
}

void dma_set_callback(int chn, dma_cb_func cb)
{
    dma_callbacks[chn] = cb != NULL ? cb : dma_no_cb;
}

void PDMA(void)
{
    /* This is called when the last descriptor completes, or if the
     * channel hits an error.
     */
    unsigned pending = REG_DMA_IRQP;
    for(int i = 0; i < DMA_NUM_USED_CHANNELS; ++i) {
        if((pending & (1 << i)) == 0)
            continue;

        int evt;
        if(REG_DMA_CHN_CS(i) & jz_orm(DMA_CHN_CS, AR, HLT))
            evt = DMA_EVENT_ERROR;
        else
            evt = DMA_EVENT_COMPLETE;

        REG_DMA_CHN_CS(i) = 0;
        dma_callbacks[i](evt);
    }

    /* Clear any errors and clear interrupts */
    jz_writef(DMA_CTRL, HALT(0), AR(0));
    REG_DMA_IRQP = 0;
}

void PDMAD(void)
{
    /* Called when TIE is set on a non-final descriptor */
    unsigned pending = REG_DMA_DIP;
    for(int i = 0; i < DMA_NUM_USED_CHANNELS; ++i) {
        if((pending & (1 << i)) == 0)
            continue;

        dma_callbacks[i](DMA_EVENT_INTERRUPT);
    }

    /* This does not operate like other clear registers */
    REG_DMA_DIC &= ~pending;
}