Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
650 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "thread.h"
26
#include "s5l8720.h"
27
#include "util.h"
28
#include "clockgates-target.h"
704 theseven 29
#include "lcd.h"
650 theseven 30
 
31
 
32
static struct dma_lli lcd_lli[(LCD_WIDTH * LCD_HEIGHT - 1) / 0xfff]
738 theseven 33
    IDATA_ATTR CACHEALIGN_ATTR;
650 theseven 34
 
35
static uint16_t lcd_color IDATA_ATTR;
36
 
37
static struct mutex lcd_mutex IDATA_ATTR;
38
static struct wakeup lcd_wakeup IDATA_ATTR;
39
 
40
static bool lcd_dma_busy IDATA_ATTR;
41
 
42
 
43
void lcd_init()
44
{
45
    mutex_init(&lcd_mutex);
46
    wakeup_init(&lcd_wakeup);
47
    lcd_dma_busy = true;
48
    clockgate_dma(0, 4, true);
49
    if (!(DMAC0C4CONFIG & 1))
50
    {
51
        lcd_dma_busy = false;
52
        clockgate_dma(0, 4, false);
53
    }
54
}
55
 
56
int lcd_get_width()
57
{
58
    return LCD_WIDTH;
59
}
60
 
61
int lcd_get_height()
62
{
63
    return LCD_HEIGHT;
64
}
65
 
66
int lcd_get_bytes_per_pixel()
67
{
68
    return LCD_BYTESPERPIXEL;
69
}
70
 
71
int lcd_get_format()
72
{
73
    return LCD_FORMAT;
74
}
75
 
76
static void lcd_send_cmd(uint16_t cmd) ICODE_ATTR __attribute__((noinline));
77
static void lcd_send_cmd(uint16_t cmd)
78
{
79
    while (LCDSTATUS & 0x10);
80
    LCDWCMD = cmd;
81
}
82
 
83
static void lcd_send_data(uint16_t data) ICODE_ATTR __attribute__((noinline));
84
static void lcd_send_data(uint16_t data)
85
{
86
    while (LCDSTATUS & 0x10);
87
    LCDWDATA = (data & 0xff) | ((data & 0x7f00) << 1);
88
}
89
 
90
void lcd_shutdown()
91
{
748 theseven 92
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
93
    displaylcd_sync();
94
    while (!(LCDSTATUS & 0x2));
751 theseven 95
    LCDCON = 0x81100db8;
650 theseven 96
}
97
 
98
bool displaylcd_busy() ICODE_ATTR;
99
bool displaylcd_busy()
100
{
101
    return lcd_dma_busy;
102
}
103
 
104
void displaylcd_sync() ICODE_ATTR;
105
void displaylcd_sync()
106
{
107
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
108
    while (displaylcd_busy()) wakeup_wait(&lcd_wakeup, TIMEOUT_BLOCK);
109
    mutex_unlock(&lcd_mutex);
110
}
111
 
112
void displaylcd_setup(unsigned int startx, unsigned int endx,
704 theseven 113
                      unsigned int starty, unsigned int endy, bool safe) ICODE_ATTR;
650 theseven 114
void displaylcd_setup(unsigned int startx, unsigned int endx,
704 theseven 115
                      unsigned int starty, unsigned int endy, bool safe)
650 theseven 116
{
704 theseven 117
    if (!safe)
650 theseven 118
    {
119
        mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
120
        displaylcd_sync();
121
    }
704 theseven 122
    else while (DMAC0C4CONFIG & 1);
748 theseven 123
    while (!(LCDSTATUS & 0x2));
751 theseven 124
    LCDCON = 0x81100db8;
650 theseven 125
    lcd_send_cmd(0x2a);
126
    lcd_send_data(startx);
127
    lcd_send_data(endx);
128
    lcd_send_cmd(0x2b);
129
    lcd_send_data(starty);
130
    lcd_send_data(endy);
131
    lcd_send_cmd(0x2c);
132
}
133
 
134
static void displaylcd_dma(void* data, int pixels, bool solid) ICODE_ATTR;
135
static void displaylcd_dma(void* data, int pixels, bool solid)
136
{
137
    int i;
138
    lcd_dma_busy = true;
139
    clockgate_dma(0, 4, true);
140
    for (i = -1; i < (int)ARRAYLEN(lcd_lli) && pixels > 0; i++, pixels -= 0xfff)
141
    {
142
        bool last = i + 1 >= ARRAYLEN(lcd_lli) || pixels <= 0xfff;
143
        struct dma_lli* lli = i < 0 ? (struct dma_lli*)((int)&DMAC0C4LLI) : &lcd_lli[i];
144
        lli->srcaddr = data;
145
        lli->dstaddr = (void*)((int)&LCDWDATA);
146
        lli->nextlli = last ? NULL : &lcd_lli[i + 1];
147
        lli->control = 0x70240000 | (last ? pixels : 0xfff)
148
                     | (last ? 0x80000000 : 0) | (solid ? 0 : 0x4000000);
748 theseven 149
        if (!solid) data += 0x1ffe;
650 theseven 150
    }
151
    clean_dcache();
152
    DMAC0C4CONFIG = 0x88c1;
153
}
154
 
155
void displaylcd_native(unsigned int startx, unsigned int endx,
156
                       unsigned int starty, unsigned int endy, void* data)
157
{
158
    int pixels = (endx - startx + 1) * (endy - starty + 1);
159
    if (pixels <= 0) return;
704 theseven 160
    displaylcd_setup(startx, endx, starty, endy, false);
650 theseven 161
    displaylcd_dma(data, pixels, false);
704 theseven 162
    mutex_unlock(&lcd_mutex);
650 theseven 163
}
164
 
704 theseven 165
void displaylcd_safe_native(unsigned int startx, unsigned int endx,
748 theseven 166
                            unsigned int starty, unsigned int endy, void* data)
704 theseven 167
{
168
    int pixels = (endx - startx + 1) * (endy - starty + 1);
169
    if (pixels <= 0) return;
170
    displaylcd_setup(startx, endx, starty, endy, true);
171
    displaylcd_dma(data, pixels, false);
172
}
173
 
650 theseven 174
void filllcd_native(unsigned int startx, unsigned int endx,
175
                    unsigned int starty, unsigned int endy, int color)
176
{
177
    int pixels = (endx - startx + 1) * (endy - starty + 1);
178
    if (pixels <= 0) return;
704 theseven 179
    displaylcd_setup(startx, endx, starty, endy, false);
650 theseven 180
    lcd_color = color;
181
    displaylcd_dma(&lcd_color, pixels, true);
704 theseven 182
    mutex_unlock(&lcd_mutex);
650 theseven 183
}
184
 
185
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
186
                       unsigned int height, void* data, unsigned int datax,
187
                       unsigned int datay, unsigned int stride, bool solid)
188
     ICODE_ATTR __attribute__((naked,noinline));
189
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
190
                       unsigned int height, void* data, unsigned int datax,
191
                       unsigned int datay, unsigned int stride, bool solid)
192
{
193
    __asm__ volatile("    muls r12, r2, r3             \n");
194
    __asm__ volatile("    bxeq lr                      \n");
704 theseven 195
    __asm__ volatile("    stmfd sp!, {r1-r11,lr}       \n");
196
    __asm__ volatile("    mov r12, #0                  \n");
197
    __asm__ volatile("    str r12, [sp]                \n");
650 theseven 198
    __asm__ volatile("    mov r12, r2                  \n");
751 theseven 199
    __asm__ volatile("    mov r8, r2,lsl#2             \n");
650 theseven 200
    __asm__ volatile("    add r3, r1, r3               \n");
751 theseven 201
    __asm__ volatile("    add r8, r8, #4               \n");
650 theseven 202
    __asm__ volatile("    sub r3, r3, #1               \n");
203
    __asm__ volatile("    mov r2, r1                   \n");
204
    __asm__ volatile("    add r1, r0, r12              \n");
205
    __asm__ volatile("    sub r1, r1, #1               \n");
206
    __asm__ volatile("    bl displaylcd_setup          \n");
704 theseven 207
    __asm__ volatile("    add sp, sp, #4               \n");
650 theseven 208
    __asm__ volatile("    mov r0, r8                   \n");
209
    __asm__ volatile("    bl malloc                    \n");
210
    __asm__ volatile("    cmp r0, #0                   \n");
211
    __asm__ volatile("    beq displaylcd_dither_unlock \n");
212
    __asm__ volatile("    mov r2, r8                   \n");
213
    __asm__ volatile("    mov r1, #0                   \n");
214
    __asm__ volatile("    mov r8, r0                   \n");
215
    __asm__ volatile("    bl memset                    \n");
216
    __asm__ volatile("    ldr r0, [sp,#0x30]           \n");
217
    __asm__ volatile("    ldr r1, [sp,#0x34]           \n");
218
    __asm__ volatile("    ldr r11, [sp,#0x38]          \n");
219
    __asm__ volatile("    ldr r3, [sp,#0x2c]           \n");
220
    __asm__ volatile("    mla r0, r1, r11, r0          \n");
221
    __asm__ volatile("    ldr r12, [sp,#0x04]          \n");
222
    __asm__ volatile("    ldr r2, [sp,#0x3c]           \n");
223
    __asm__ volatile("    add r3, r3, r0,lsl#1         \n");
224
    __asm__ volatile("    cmp r2, #0                   \n");
225
    __asm__ volatile("    ldreq r1, [sp]               \n");
226
    __asm__ volatile("    add r3, r3, r0               \n");
227
    __asm__ volatile("    subeq r11, r11, r1           \n");
228
    __asm__ volatile("    add r11, r11, r11,lsl#1      \n");
751 theseven 229
    __asm__ volatile("    movne r10, #0                \n");
230
    __asm__ volatile("    moveq r10, #3                \n");
650 theseven 231
    __asm__ volatile("    ldr r9, =0x38300040          \n");
748 theseven 232
    __asm__ volatile("displaylcd_dither_wait :         \n");
233
    __asm__ volatile("    ldr r4, [r9,#-0x24]          \n");
234
    __asm__ volatile("    tst r4, #2                   \n");
235
    __asm__ volatile("    beq displaylcd_dither_wait   \n");
751 theseven 236
    __asm__ volatile("    ldr r4, =0x81104eb8          \n");
748 theseven 237
    __asm__ volatile("    str r4, [r9,#-0x40]          \n");
751 theseven 238
    __asm__ volatile("    ldr r6, =0x30303             \n");
239
    __asm__ volatile("    ldr r4, =0x808080            \n");
650 theseven 240
    __asm__ volatile("displaylcd_dither_y:             \n");
241
    __asm__ volatile("    ldr lr, [sp]                 \n");
242
    __asm__ volatile("    mov r5, #0                   \n");
243
    __asm__ volatile("    mov r7, r8                   \n");
751 theseven 244
    __asm__ volatile("displaylcd_dither_x:             \n"); // the lcd can accept one pixel every 25 clocks
245
    __asm__ volatile("    ldr r0, [r3]                 \n"); // 1 cycle, 2 mem, r0 latency 4, r3 early
246
    __asm__ volatile("    add r3, r3, r10              \n"); // 1 cycle
247
    __asm__ volatile("    ldr r1, [r7,#4]              \n"); // 1 cycle, 1 mem, r1 latency 3, r7 early
248
    __asm__ volatile("    subs lr, lr, #1              \n"); // 1 cycle
249
    __asm__ volatile("    ssub8 r0, r0, r4             \n"); // 1 cycle
250
    __asm__ volatile("    sadd8 r1, r1, r5             \n"); // 1 cycle
251
    __asm__ volatile("    qadd8 r0, r0, r1             \n"); // 1 cycle, r0 latency 2
252
                                                             // bubble (due to r0 latency)
253
    __asm__ volatile("    sadd8 r0, r0, r4             \n"); // 1 cycle
254
    __asm__ volatile("    str r0, [r9]                 \n"); // 1 cycle, 1 mem, r9 early
255
    __asm__ volatile("    bic r2, r0, r6               \n"); // 1 cycle
256
    __asm__ volatile("    and r1, r6, r0,lsr#6         \n"); // 1 cycle, r0 early
257
    __asm__ volatile("    orr r2, r2, r1               \n"); // 1 cycle
258
    __asm__ volatile("    mov r1, r5                   \n"); // 1 cycle
259
    __asm__ volatile("    shsub8 r5, r0, r2            \n"); // 1 cycle
260
    __asm__ volatile("    shadd8 r1, r1, r5            \n"); // 1 cycle
261
    __asm__ volatile("    str r1, [r7], #4             \n"); // 1 cycle, 1 mem, r7 early
262
    __asm__ volatile("    nop                          \n"); // 2 cycles
263
    __asm__ volatile("    nop                          \n"); // 2 cycles
264
    __asm__ volatile("    nop                          \n"); // 2 cycles
265
    __asm__ volatile("    nop                          \n"); // 2 cycles
650 theseven 266
    __asm__ volatile("    bne displaylcd_dither_x      \n");
267
    __asm__ volatile("    add r3, r3, r11              \n");
268
    __asm__ volatile("    subs r12, r12, #1            \n");
269
    __asm__ volatile("    bne displaylcd_dither_y      \n");
270
    __asm__ volatile("displaylcd_dither_free:          \n");
271
    __asm__ volatile("    mov r0, r8                   \n");
272
    __asm__ volatile("    bl free                      \n");
273
    __asm__ volatile("displaylcd_dither_unlock:        \n");
274
    __asm__ volatile("    ldr r0, =lcd_mutex           \n");
275
    __asm__ volatile("    bl mutex_unlock              \n");
276
    __asm__ volatile("    ldmfd sp!, {r2-r11,pc}       \n");
277
}
278
 
279
void displaylcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height,
280
                void* data, unsigned int datax, unsigned int datay, unsigned int stride)
281
{
282
    displaylcd_dither(x, y, width, height, data, datax, datay, stride, false);
283
}
284
 
285
void filllcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height, int color)
286
{
287
    if (width * height <= 0) return;
288
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
289
    displaylcd_sync();
290
    lcd_color = color;
291
    displaylcd_dither(x, y, width, height, &lcd_color, 0, 0, 0, true);
292
    mutex_unlock(&lcd_mutex);
293
}
294
 
295
void INT_DMAC0C4()
296
{
297
    DMAC0INTTCCLR = 0x10;
298
    lcd_dma_busy = false;
299
    clockgate_dma(0, 4, false);
300
    lcdconsole_callback();
301
    wakeup_signal(&lcd_wakeup);
302
}
303
 
304
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
305
    ICODE_ATTR __attribute__((naked, noinline));
306
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
307
{
308
    asm volatile(
309
        "cmp r0, #0xff             \n\t"
310
        "moveq r0, #-1             \n\t"
311
        "moveq pc, lr              \n\t"
312
        "cmp r0, #0                \n\t"
313
        "movne r0, #0xff000000     \n\t"
314
        "orrne r0, r0, #0xff0000   \n\t"
315
        "mov r2, r2,lsr#2          \n\t"
316
        "orr r0, r0, r3,lsr#3      \n\t"
317
        "mov r1, r1,lsr#3          \n\t"
318
        "orr r0, r0, r2,lsl#5      \n\t"
319
        "orr r0, r0, r1,lsl#11     \n\t"
320
        "mov pc, lr                \n\t"
321
    );
322
}