summaryrefslogtreecommitdiffstats
path: root/firmware/target/mips/ingenic_jz47xx/lcd-jz4740.c
blob: 1401bf8a3f1ad9bdd40c292de8b6d9ead6b46112 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by Maurus Cuelenaere
 *
 * 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 "config.h"
#include "jz4740.h"
#include "lcd.h"
#include "lcd-target.h"
#include "system.h"
#include "kernel.h"

static volatile bool lcd_is_on = false;
static struct mutex lcd_mtx;

/* LCD init */
void lcd_init_device(void)
{
    lcd_init_controller();
    lcd_is_on = true;
    mutex_init(&lcd_mtx);
}

void lcd_enable(bool state)
{
    if(state)
    {
        lcd_on();
        lcd_call_enable_hook();
    }
    else
        lcd_off();
    
    lcd_is_on = state;
}

bool lcd_enabled(void)
{
    return lcd_is_on;
}

/* Don't switch threads when in interrupt mode! */
static inline void lcd_lock(void)
{
    if(LIKELY(!in_interrupt_mode()))
        mutex_lock(&lcd_mtx);
    else
        while( !(REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_TT));
}

static inline void lcd_unlock(void)
{
    if(LIKELY(!in_interrupt_mode()))
        mutex_unlock(&lcd_mtx);
}

static inline void lcd_wait(void)
{
    if(LIKELY(!in_interrupt_mode()))
    {
        while( !(REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_TT) )
            yield();
    }
    else
        while( !(REG_DMAC_DCCSR(DMA_LCD_CHANNEL) & DMAC_DCCSR_TT));
}

/* Update a fraction of the display. */
void lcd_update_rect(int x, int y, int width, int height)
{
    lcd_lock();
    
    lcd_set_target(x, y, width, height);
    
    REG_DMAC_DCCSR(DMA_LCD_CHANNEL) = 0;
    REG_DMAC_DRSR(DMA_LCD_CHANNEL)  = DMAC_DRSR_RS_SLCD; /* source = SLCD */
    REG_DMAC_DSAR(DMA_LCD_CHANNEL)  = ((unsigned int)&lcd_framebuffer[y][x]) & 0x1FFFFFFF;
    REG_DMAC_DTAR(DMA_LCD_CHANNEL)  = 0x130500B0; /* SLCD_FIFO */
    REG_DMAC_DTCR(DMA_LCD_CHANNEL)  = width*height;
    
    REG_DMAC_DCMD(DMA_LCD_CHANNEL)  = ( DMAC_DCMD_SAI     | DMAC_DCMD_RDIL_IGN | DMAC_DCMD_SWDH_32
                                      | DMAC_DCMD_DWDH_16 | DMAC_DCMD_DS_16BIT );
    REG_DMAC_DCCSR(DMA_LCD_CHANNEL) = DMAC_DCCSR_NDES;
    
    __dcache_writeback_all(); /* Size of framebuffer is way bigger than cache size;
                                     we need to find a way to make the framebuffer uncached, so this statement can get removed. */
    
    while(REG_SLCD_STATE & SLCD_STATE_BUSY);
    
    REG_SLCD_CTRL |= SLCD_CTRL_DMA_EN;
    REG_DMAC_DCCSR(DMA_LCD_CHANNEL) |= DMAC_DCCSR_EN;

    lcd_wait();
    
    REG_DMAC_DCCSR(DMA_LCD_CHANNEL) &= ~DMAC_DCCSR_EN;
    
    while(REG_SLCD_STATE & SLCD_STATE_BUSY);
    
    REG_SLCD_CTRL &= ~SLCD_CTRL_DMA_EN;
    
    lcd_unlock();
}

/* Update the display.
   This must be called after all other LCD functions that change the display. */
void lcd_update(void)
{
    if (!lcd_is_on)
        return;
    
    lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
}