Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
669 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 "s5l8701.h"
27
#include "util.h"
704 theseven 28
#include "lcd.h"
669 theseven 29
 
30
 
31
static struct mutex lcd_mutex IDATA_ATTR;
32
static struct wakeup lcd_wakeup IDATA_ATTR;
33
 
34
static bool lcd_dma_busy IDATA_ATTR;
35
 
36
 
37
void lcd_init()
38
{
39
    mutex_init(&lcd_mutex);
40
    wakeup_init(&lcd_wakeup);
41
    DMACON8 = 0x20590000;
42
    LCDCON = 0xd01;
43
    LCDPHTIME = 0;
44
    lcd_dma_busy = false;
45
}
46
 
47
int lcd_get_width()
48
{
49
    return LCD_WIDTH;
50
}
51
 
52
int lcd_get_height()
53
{
54
    return LCD_HEIGHT;
55
}
56
 
57
int lcd_get_bytes_per_pixel()
58
{
59
    return LCD_BYTESPERPIXEL;
60
}
61
 
62
int lcd_get_format()
63
{
64
    return LCD_FORMAT;
65
}
66
 
835 theseven 67
static void lcd_send_cmd(uint32_t cmd) ICODE_ATTR __attribute__((noinline));
68
static void lcd_send_cmd(uint32_t cmd)
669 theseven 69
{
70
    while (LCDSTATUS & 0x10);
71
    LCDWCMD = cmd;
72
}
73
 
835 theseven 74
static void lcd_send_data(uint32_t data) ICODE_ATTR __attribute__((noinline));
75
static void lcd_send_data(uint32_t data)
669 theseven 76
{
77
    while (LCDSTATUS & 0x10);
78
    LCDWDATA = data;
79
}
80
 
835 theseven 81
static uint32_t lcd_detect() ICODE_ATTR __attribute__((noinline));
669 theseven 82
static uint32_t lcd_detect()
83
{
84
    return (PDAT13 & 1) | (PDAT14 & 2);
85
}
86
 
87
bool displaylcd_busy() ICODE_ATTR;
88
bool displaylcd_busy()
89
{
90
    return lcd_dma_busy;
91
}
92
 
93
void displaylcd_sync() ICODE_ATTR;
94
void displaylcd_sync()
95
{
96
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
97
    while (displaylcd_busy()) wakeup_wait(&lcd_wakeup, TIMEOUT_BLOCK);
98
    mutex_unlock(&lcd_mutex);
99
}
100
 
101
void displaylcd_setup(unsigned int startx, unsigned int endx,
704 theseven 102
                      unsigned int starty, unsigned int endy, bool safe) ICODE_ATTR;
669 theseven 103
void displaylcd_setup(unsigned int startx, unsigned int endx,
704 theseven 104
                      unsigned int starty, unsigned int endy, bool safe)
669 theseven 105
{
704 theseven 106
    if (!safe)
669 theseven 107
    {
108
        mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
109
        displaylcd_sync();
110
    }
704 theseven 111
    else while (DMAALLST2 & 0x70000);
669 theseven 112
    if (lcd_detect() == 2)
113
    {
114
        lcd_send_cmd(0x50);
115
        lcd_send_data(startx);
116
        lcd_send_cmd(0x51);
117
        lcd_send_data(endx);
118
        lcd_send_cmd(0x52);
119
        lcd_send_data(starty);
120
        lcd_send_cmd(0x53);
121
        lcd_send_data(endy);
122
        lcd_send_cmd(0x20);
123
        lcd_send_data(startx);
124
        lcd_send_cmd(0x21);
125
        lcd_send_data(starty);
126
        lcd_send_cmd(0x22);
127
    }
128
    else
129
    {
130
        lcd_send_cmd(0x2a);
131
        lcd_send_data(startx);
132
        lcd_send_data(endx);
133
        lcd_send_cmd(0x2b);
134
        lcd_send_data(starty);
135
        lcd_send_data(endy);
136
        lcd_send_cmd(0x2c);
137
    }
138
}
139
 
140
static void displaylcd_dma(void* data, int pixels) ICODE_ATTR;
141
static void displaylcd_dma(void* data, int pixels)
142
{
143
    uint16_t* in = (uint16_t*)data;
144
    while (LCDSTATUS & 8);
145
    while (pixels & 3)
146
	{
147
        LCDWDATA = *in++;
148
        pixels--;
149
	}
868 user890104 150
    if (!pixels) return;
669 theseven 151
    lcd_dma_busy = true;
152
    DMABASE8 = in;
153
    DMACON8 = 0x20590000;
868 user890104 154
    DMATCNT8 = (pixels / 4) - 1;
669 theseven 155
    clean_dcache();
156
    DMACOM8 = 4;
157
}
158
 
159
static void displaylcd_solid(uint16_t data, int pixels) ICODE_ATTR;
160
static void displaylcd_solid(uint16_t data, int pixels)
161
{
162
    while (pixels >= 4)
163
    {
164
        while (LCDSTATUS & 8);
165
        LCDWDATA = data;
166
        LCDWDATA = data;
167
        LCDWDATA = data;
168
        LCDWDATA = data;
169
        pixels -= 4;
170
    }
171
    while (LCDSTATUS & 8);
172
    while (pixels & 3)
173
	{
174
        LCDWDATA = data;
175
        pixels--;
176
	}
177
}
178
 
179
void displaylcd_native(unsigned int startx, unsigned int endx,
180
                       unsigned int starty, unsigned int endy, void* data)
181
{
182
    int pixels = (endx - startx + 1) * (endy - starty + 1);
183
    if (pixels <= 0) return;
704 theseven 184
    displaylcd_setup(startx, endx, starty, endy, false);
669 theseven 185
    displaylcd_dma(data, pixels);
704 theseven 186
    mutex_unlock(&lcd_mutex);
669 theseven 187
}
188
 
704 theseven 189
void displaylcd_safe_native(unsigned int startx, unsigned int endx,
190
                            unsigned int starty, unsigned int endy, void* data)
191
{
192
    int pixels = (endx - startx + 1) * (endy - starty + 1);
193
    if (pixels <= 0) return;
194
    displaylcd_setup(startx, endx, starty, endy, true);
195
    displaylcd_dma(data, pixels);
196
}
197
 
669 theseven 198
void filllcd_native(unsigned int startx, unsigned int endx,
199
                    unsigned int starty, unsigned int endy, int color)
200
{
201
    int pixels = (endx - startx + 1) * (endy - starty + 1);
202
    if (pixels <= 0) return;
704 theseven 203
    displaylcd_setup(startx, endx, starty, endy, false);
669 theseven 204
    displaylcd_solid(color, pixels);
704 theseven 205
    mutex_unlock(&lcd_mutex);
669 theseven 206
}
207
 
208
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
209
                       unsigned int height, void* data, unsigned int datax,
210
                       unsigned int datay, unsigned int stride, bool solid)
211
     ICODE_ATTR __attribute__((naked,noinline));
212
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
213
                       unsigned int height, void* data, unsigned int datax,
214
                       unsigned int datay, unsigned int stride, bool solid)
215
{
216
    __asm__ volatile("    muls r12, r2, r3             \n");
217
    __asm__ volatile("    bxeq lr                      \n");
704 theseven 218
    __asm__ volatile("    stmfd sp!, {r1-r11,lr}       \n");
219
    __asm__ volatile("    mov r12, #0                  \n");
220
    __asm__ volatile("    str r12, [sp]                \n");
669 theseven 221
    __asm__ volatile("    mov r12, r2                  \n");
222
    __asm__ volatile("    add r8, r2, r2,lsl#1         \n");
751 theseven 223
    __asm__ volatile("    add r8, r8, #3               \n");
669 theseven 224
    __asm__ volatile("    add r3, r1, r3               \n");
225
    __asm__ volatile("    sub r3, r3, #1               \n");
226
    __asm__ volatile("    mov r2, r1                   \n");
227
    __asm__ volatile("    add r1, r0, r12              \n");
228
    __asm__ volatile("    sub r1, r1, #1               \n");
229
    __asm__ volatile("    bl displaylcd_setup          \n");
704 theseven 230
    __asm__ volatile("    add sp, sp, #4               \n");
669 theseven 231
    __asm__ volatile("    mov r0, r8                   \n");
232
    __asm__ volatile("    bl malloc                    \n");
233
    __asm__ volatile("    cmp r0, #0                   \n");
234
    __asm__ volatile("    beq displaylcd_dither_unlock \n");
235
    __asm__ volatile("    mov r2, r8                   \n");
236
    __asm__ volatile("    mov r1, #0                   \n");
237
    __asm__ volatile("    mov r8, r0                   \n");
238
    __asm__ volatile("    bl memset                    \n");
239
    __asm__ volatile("    ldr r0, [sp,#0x30]           \n");
240
    __asm__ volatile("    ldr r1, [sp,#0x34]           \n");
241
    __asm__ volatile("    ldr r11, [sp,#0x38]          \n");
242
    __asm__ volatile("    ldr r3, [sp,#0x2c]           \n");
243
    __asm__ volatile("    mla r0, r1, r11, r0          \n");
244
    __asm__ volatile("    ldr r12, [sp,#0x04]          \n");
245
    __asm__ volatile("    ldr r2, [sp,#0x3c]           \n");
246
    __asm__ volatile("    add r3, r3, r0,lsl#1         \n");
247
    __asm__ volatile("    cmp r2, #0                   \n");
248
    __asm__ volatile("    ldreq r1, [sp]               \n");
249
    __asm__ volatile("    add r3, r3, r0               \n");
250
    __asm__ volatile("    subeq r11, r11, r1           \n");
251
    __asm__ volatile("    add r11, r11, r11,lsl#1      \n");
252
    __asm__ volatile("    movne r10, #3                \n");
253
    __asm__ volatile("    moveq r10, #0                \n");
254
    __asm__ volatile("    ldr r9, =0x38600040          \n");
255
    __asm__ volatile("displaylcd_dither_y:             \n");
256
    __asm__ volatile("    ldr lr, [sp]                 \n");
257
    __asm__ volatile("    mov r4, #0                   \n");
258
    __asm__ volatile("    mov r5, #0                   \n");
259
    __asm__ volatile("    mov r6, #0                   \n");
260
    __asm__ volatile("    mov r7, r8                   \n");
261
    __asm__ volatile("displaylcd_dither_x:             \n");
262
    __asm__ volatile("    ldrb r1, [r3], #1            \n");
751 theseven 263
    __asm__ volatile("    ldrsb r0, [r7,#3]            \n");
669 theseven 264
    __asm__ volatile("    add r1, r1, r4               \n");
265
    __asm__ volatile("    add r1, r1, r0               \n");
648 theseven 266
    __asm__ volatile("    cmp r1, #0xff                \n");
267
    __asm__ volatile("    mvnhi r1, r1,asr#31          \n");
268
    __asm__ volatile("    andhi r1, r1, #0xff          \n");
669 theseven 269
    __asm__ volatile("    mov r0, r1,lsr#3             \n");
747 theseven 270
    __asm__ volatile("    mov r2, r0,lsl#11            \n");
669 theseven 271
    __asm__ volatile("    sub r1, r1, r0,lsl#3         \n");
272
    __asm__ volatile("    sub r1, r1, r0,lsr#2         \n");
273
    __asm__ volatile("    mov r4, r4,lsr#1             \n");
274
    __asm__ volatile("    add r4, r4, r1,lsr#2         \n");
275
    __asm__ volatile("    strb r4, [r7], #1            \n");
276
    __asm__ volatile("    mov r4, r1,asr#1             \n");
277
    __asm__ volatile("    ldrb r1, [r3], #1            \n");
751 theseven 278
    __asm__ volatile("    ldrsb r0, [r7,#3]            \n");
669 theseven 279
    __asm__ volatile("    add r1, r1, r5               \n");
280
    __asm__ volatile("    add r1, r1, r0               \n");
648 theseven 281
    __asm__ volatile("    cmp r1, #0xff                \n");
282
    __asm__ volatile("    mvnhi r1, r1,asr#31          \n");
283
    __asm__ volatile("    andhi r1, r1, #0xff          \n");
669 theseven 284
    __asm__ volatile("    mov r0, r1,lsr#2             \n");
751 theseven 285
    __asm__ volatile("    orr r2, r2, r0,lsl#5         \n");
669 theseven 286
    __asm__ volatile("    sub r1, r1, r0,lsl#2         \n");
287
    __asm__ volatile("    sub r1, r1, r0,lsr#4         \n");
288
    __asm__ volatile("    mov r5, r5,lsr#1             \n");
289
    __asm__ volatile("    add r5, r5, r1,lsr#2         \n");
290
    __asm__ volatile("    strb r5, [r7], #1            \n");
291
    __asm__ volatile("    mov r5, r1,asr#1             \n");
292
    __asm__ volatile("    ldrb r1, [r3], #1            \n");
751 theseven 293
    __asm__ volatile("    ldrsb r0, [r7,#3]            \n");
669 theseven 294
    __asm__ volatile("    add r1, r1, r6               \n");
295
    __asm__ volatile("    add r1, r1, r0               \n");
648 theseven 296
    __asm__ volatile("    cmp r1, #0xff                \n");
297
    __asm__ volatile("    mvnhi r1, r1,asr#31          \n");
298
    __asm__ volatile("    andhi r1, r1, #0xff          \n");
669 theseven 299
    __asm__ volatile("    mov r0, r1,lsr#3             \n");
747 theseven 300
    __asm__ volatile("    orr r2, r2, r0               \n");
669 theseven 301
    __asm__ volatile("    sub r1, r1, r0,lsl#3         \n");
302
    __asm__ volatile("    sub r1, r1, r0,lsr#2         \n");
303
    __asm__ volatile("    mov r6, r6,lsr#1             \n");
304
    __asm__ volatile("    add r6, r6, r1,lsr#2         \n");
305
    __asm__ volatile("    strb r6, [r7], #1            \n");
306
    __asm__ volatile("displaylcd_dither_waitlcd:       \n");
307
    __asm__ volatile("    ldr r0, [r9,#-0x24]          \n");
308
    __asm__ volatile("    mov r6, r1,asr#1             \n");
309
    __asm__ volatile("    tst r0, #0x10                \n");
310
    __asm__ volatile("    bne displaylcd_dither_waitlcd\n");
311
    __asm__ volatile("    str r2, [r9]                 \n");
312
    __asm__ volatile("    sub r3, r3, r10              \n");
313
    __asm__ volatile("    subs lr, lr, #1              \n");
314
    __asm__ volatile("    bne displaylcd_dither_x      \n");
315
    __asm__ volatile("    add r3, r3, r11              \n");
316
    __asm__ volatile("    subs r12, r12, #1            \n");
317
    __asm__ volatile("    bne displaylcd_dither_y      \n");
318
    __asm__ volatile("displaylcd_dither_free:          \n");
319
    __asm__ volatile("    mov r0, r8                   \n");
320
    __asm__ volatile("    bl free                      \n");
321
    __asm__ volatile("displaylcd_dither_unlock:        \n");
322
    __asm__ volatile("    ldr r0, =lcd_mutex           \n");
323
    __asm__ volatile("    bl mutex_unlock              \n");
324
    __asm__ volatile("    ldmfd sp!, {r2-r11,pc}       \n");
325
}
326
 
327
void displaylcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height,
328
                void* data, unsigned int datax, unsigned int datay, unsigned int stride)
329
{
330
    displaylcd_dither(x, y, width, height, data, datax, datay, stride, false);
331
}
332
 
333
void filllcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height, int color)
334
{
335
    if (width * height <= 0) return;
336
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
337
    displaylcd_sync();
338
    displaylcd_dither(x, y, width, height, &color, 0, 0, 0, true);
339
    mutex_unlock(&lcd_mutex);
340
}
341
 
342
void lcd_shutdown()
343
{
344
    displaylcd_sync();
345
    uint32_t type = lcd_detect();
346
    if (type == 2)
347
    {
348
        lcd_send_cmd(0x7);
349
        lcd_send_data(0x232);
350
        lcd_send_cmd(0x13);
351
        lcd_send_data(0x1137);
352
        lcd_send_cmd(0x7);
353
        lcd_send_data(0x201);
354
        lcd_send_cmd(0x13);
355
        lcd_send_data(0x137);
356
        lcd_send_cmd(0x7);
357
        lcd_send_data(0x200);
358
        lcd_send_cmd(0x10);
359
        lcd_send_data(0x680);
360
        lcd_send_cmd(0x12);
361
        lcd_send_data(0x160);
362
        lcd_send_cmd(0x13);
363
        lcd_send_data(0x127);
364
        lcd_send_cmd(0x10);
365
        lcd_send_data(0x600);
366
    }
367
    else
368
    {
369
        lcd_send_cmd(0x28);
370
        lcd_send_cmd(0x10);
371
    }
711 theseven 372
    sleep(20000);
669 theseven 373
}
374
 
375
void INT_DMA8()
376
{
377
    DMACOM8 = 7;
378
    lcd_dma_busy = false;
379
    lcdconsole_callback();
380
    wakeup_signal(&lcd_wakeup);
381
}
382
 
383
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
384
    ICODE_ATTR __attribute__((naked, noinline));
385
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
386
{
387
    asm volatile(
388
        "cmp r0, #0xff             \n\t"
389
        "moveq r0, #-1             \n\t"
390
        "moveq pc, lr              \n\t"
391
        "cmp r0, #0                \n\t"
392
        "movne r0, #0xff000000     \n\t"
393
        "orrne r0, r0, #0xff0000   \n\t"
394
        "mov r2, r2,lsr#2          \n\t"
395
        "orr r0, r0, r3,lsr#3      \n\t"
396
        "mov r1, r1,lsr#3          \n\t"
397
        "orr r0, r0, r2,lsl#5      \n\t"
398
        "orr r0, r0, r1,lsl#11     \n\t"
399
        "mov pc, lr                \n\t"
400
    );
401
}