Subversion Repositories freemyipod

Rev

Go to most recent revision | 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
	}
150
    lcd_dma_busy = true;
151
    DMABASE8 = in;
152
    DMACON8 = 0x20590000;
153
    DMATCNT8 = pixels / 4;
154
    clean_dcache();
155
    DMACOM8 = 4;
156
}
157
 
158
static void displaylcd_solid(uint16_t data, int pixels) ICODE_ATTR;
159
static void displaylcd_solid(uint16_t data, int pixels)
160
{
161
    while (pixels >= 4)
162
    {
163
        while (LCDSTATUS & 8);
164
        LCDWDATA = data;
165
        LCDWDATA = data;
166
        LCDWDATA = data;
167
        LCDWDATA = data;
168
        pixels -= 4;
169
    }
170
    while (LCDSTATUS & 8);
171
    while (pixels & 3)
172
	{
173
        LCDWDATA = data;
174
        pixels--;
175
	}
176
}
177
 
178
void displaylcd_native(unsigned int startx, unsigned int endx,
179
                       unsigned int starty, unsigned int endy, void* data)
180
{
181
    int pixels = (endx - startx + 1) * (endy - starty + 1);
182
    if (pixels <= 0) return;
704 theseven 183
    displaylcd_setup(startx, endx, starty, endy, false);
669 theseven 184
    displaylcd_dma(data, pixels);
704 theseven 185
    mutex_unlock(&lcd_mutex);
669 theseven 186
}
187
 
704 theseven 188
void displaylcd_safe_native(unsigned int startx, unsigned int endx,
189
                            unsigned int starty, unsigned int endy, void* data)
190
{
191
    int pixels = (endx - startx + 1) * (endy - starty + 1);
192
    if (pixels <= 0) return;
193
    displaylcd_setup(startx, endx, starty, endy, true);
194
    displaylcd_dma(data, pixels);
195
}
196
 
669 theseven 197
void filllcd_native(unsigned int startx, unsigned int endx,
198
                    unsigned int starty, unsigned int endy, int color)
199
{
200
    int pixels = (endx - startx + 1) * (endy - starty + 1);
201
    if (pixels <= 0) return;
704 theseven 202
    displaylcd_setup(startx, endx, starty, endy, false);
669 theseven 203
    displaylcd_solid(color, pixels);
704 theseven 204
    mutex_unlock(&lcd_mutex);
669 theseven 205
}
206
 
207
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
208
                       unsigned int height, void* data, unsigned int datax,
209
                       unsigned int datay, unsigned int stride, bool solid)
210
     ICODE_ATTR __attribute__((naked,noinline));
211
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
212
                       unsigned int height, void* data, unsigned int datax,
213
                       unsigned int datay, unsigned int stride, bool solid)
214
{
215
    __asm__ volatile("    muls r12, r2, r3             \n");
216
    __asm__ volatile("    bxeq lr                      \n");
704 theseven 217
    __asm__ volatile("    stmfd sp!, {r1-r11,lr}       \n");
218
    __asm__ volatile("    mov r12, #0                  \n");
219
    __asm__ volatile("    str r12, [sp]                \n");
669 theseven 220
    __asm__ volatile("    mov r12, r2                  \n");
221
    __asm__ volatile("    add r8, r2, r2,lsl#1         \n");
751 theseven 222
    __asm__ volatile("    add r8, r8, #3               \n");
669 theseven 223
    __asm__ volatile("    add r3, r1, r3               \n");
224
    __asm__ volatile("    sub r3, r3, #1               \n");
225
    __asm__ volatile("    mov r2, r1                   \n");
226
    __asm__ volatile("    add r1, r0, r12              \n");
227
    __asm__ volatile("    sub r1, r1, #1               \n");
228
    __asm__ volatile("    bl displaylcd_setup          \n");
704 theseven 229
    __asm__ volatile("    add sp, sp, #4               \n");
669 theseven 230
    __asm__ volatile("    mov r0, r8                   \n");
231
    __asm__ volatile("    bl malloc                    \n");
232
    __asm__ volatile("    cmp r0, #0                   \n");
233
    __asm__ volatile("    beq displaylcd_dither_unlock \n");
234
    __asm__ volatile("    mov r2, r8                   \n");
235
    __asm__ volatile("    mov r1, #0                   \n");
236
    __asm__ volatile("    mov r8, r0                   \n");
237
    __asm__ volatile("    bl memset                    \n");
238
    __asm__ volatile("    ldr r0, [sp,#0x30]           \n");
239
    __asm__ volatile("    ldr r1, [sp,#0x34]           \n");
240
    __asm__ volatile("    ldr r11, [sp,#0x38]          \n");
241
    __asm__ volatile("    ldr r3, [sp,#0x2c]           \n");
242
    __asm__ volatile("    mla r0, r1, r11, r0          \n");
243
    __asm__ volatile("    ldr r12, [sp,#0x04]          \n");
244
    __asm__ volatile("    ldr r2, [sp,#0x3c]           \n");
245
    __asm__ volatile("    add r3, r3, r0,lsl#1         \n");
246
    __asm__ volatile("    cmp r2, #0                   \n");
247
    __asm__ volatile("    ldreq r1, [sp]               \n");
248
    __asm__ volatile("    add r3, r3, r0               \n");
249
    __asm__ volatile("    subeq r11, r11, r1           \n");
250
    __asm__ volatile("    add r11, r11, r11,lsl#1      \n");
251
    __asm__ volatile("    movne r10, #3                \n");
252
    __asm__ volatile("    moveq r10, #0                \n");
253
    __asm__ volatile("    ldr r9, =0x38600040          \n");
254
    __asm__ volatile("displaylcd_dither_y:             \n");
255
    __asm__ volatile("    ldr lr, [sp]                 \n");
256
    __asm__ volatile("    mov r4, #0                   \n");
257
    __asm__ volatile("    mov r5, #0                   \n");
258
    __asm__ volatile("    mov r6, #0                   \n");
259
    __asm__ volatile("    mov r7, r8                   \n");
260
    __asm__ volatile("displaylcd_dither_x:             \n");
261
    __asm__ volatile("    ldrb r1, [r3], #1            \n");
751 theseven 262
    __asm__ volatile("    ldrsb r0, [r7,#3]            \n");
669 theseven 263
    __asm__ volatile("    add r1, r1, r4               \n");
264
    __asm__ volatile("    add r1, r1, r0               \n");
648 theseven 265
    __asm__ volatile("    cmp r1, #0xff                \n");
266
    __asm__ volatile("    mvnhi r1, r1,asr#31          \n");
267
    __asm__ volatile("    andhi r1, r1, #0xff          \n");
669 theseven 268
    __asm__ volatile("    mov r0, r1,lsr#3             \n");
747 theseven 269
    __asm__ volatile("    mov r2, r0,lsl#11            \n");
669 theseven 270
    __asm__ volatile("    sub r1, r1, r0,lsl#3         \n");
271
    __asm__ volatile("    sub r1, r1, r0,lsr#2         \n");
272
    __asm__ volatile("    mov r4, r4,lsr#1             \n");
273
    __asm__ volatile("    add r4, r4, r1,lsr#2         \n");
274
    __asm__ volatile("    strb r4, [r7], #1            \n");
275
    __asm__ volatile("    mov r4, r1,asr#1             \n");
276
    __asm__ volatile("    ldrb r1, [r3], #1            \n");
751 theseven 277
    __asm__ volatile("    ldrsb r0, [r7,#3]            \n");
669 theseven 278
    __asm__ volatile("    add r1, r1, r5               \n");
279
    __asm__ volatile("    add r1, r1, r0               \n");
648 theseven 280
    __asm__ volatile("    cmp r1, #0xff                \n");
281
    __asm__ volatile("    mvnhi r1, r1,asr#31          \n");
282
    __asm__ volatile("    andhi r1, r1, #0xff          \n");
669 theseven 283
    __asm__ volatile("    mov r0, r1,lsr#2             \n");
751 theseven 284
    __asm__ volatile("    orr r2, r2, r0,lsl#5         \n");
669 theseven 285
    __asm__ volatile("    sub r1, r1, r0,lsl#2         \n");
286
    __asm__ volatile("    sub r1, r1, r0,lsr#4         \n");
287
    __asm__ volatile("    mov r5, r5,lsr#1             \n");
288
    __asm__ volatile("    add r5, r5, r1,lsr#2         \n");
289
    __asm__ volatile("    strb r5, [r7], #1            \n");
290
    __asm__ volatile("    mov r5, r1,asr#1             \n");
291
    __asm__ volatile("    ldrb r1, [r3], #1            \n");
751 theseven 292
    __asm__ volatile("    ldrsb r0, [r7,#3]            \n");
669 theseven 293
    __asm__ volatile("    add r1, r1, r6               \n");
294
    __asm__ volatile("    add r1, r1, r0               \n");
648 theseven 295
    __asm__ volatile("    cmp r1, #0xff                \n");
296
    __asm__ volatile("    mvnhi r1, r1,asr#31          \n");
297
    __asm__ volatile("    andhi r1, r1, #0xff          \n");
669 theseven 298
    __asm__ volatile("    mov r0, r1,lsr#3             \n");
747 theseven 299
    __asm__ volatile("    orr r2, r2, r0               \n");
669 theseven 300
    __asm__ volatile("    sub r1, r1, r0,lsl#3         \n");
301
    __asm__ volatile("    sub r1, r1, r0,lsr#2         \n");
302
    __asm__ volatile("    mov r6, r6,lsr#1             \n");
303
    __asm__ volatile("    add r6, r6, r1,lsr#2         \n");
304
    __asm__ volatile("    strb r6, [r7], #1            \n");
305
    __asm__ volatile("displaylcd_dither_waitlcd:       \n");
306
    __asm__ volatile("    ldr r0, [r9,#-0x24]          \n");
307
    __asm__ volatile("    mov r6, r1,asr#1             \n");
308
    __asm__ volatile("    tst r0, #0x10                \n");
309
    __asm__ volatile("    bne displaylcd_dither_waitlcd\n");
310
    __asm__ volatile("    str r2, [r9]                 \n");
311
    __asm__ volatile("    sub r3, r3, r10              \n");
312
    __asm__ volatile("    subs lr, lr, #1              \n");
313
    __asm__ volatile("    bne displaylcd_dither_x      \n");
314
    __asm__ volatile("    add r3, r3, r11              \n");
315
    __asm__ volatile("    subs r12, r12, #1            \n");
316
    __asm__ volatile("    bne displaylcd_dither_y      \n");
317
    __asm__ volatile("displaylcd_dither_free:          \n");
318
    __asm__ volatile("    mov r0, r8                   \n");
319
    __asm__ volatile("    bl free                      \n");
320
    __asm__ volatile("displaylcd_dither_unlock:        \n");
321
    __asm__ volatile("    ldr r0, =lcd_mutex           \n");
322
    __asm__ volatile("    bl mutex_unlock              \n");
323
    __asm__ volatile("    ldmfd sp!, {r2-r11,pc}       \n");
324
}
325
 
326
void displaylcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height,
327
                void* data, unsigned int datax, unsigned int datay, unsigned int stride)
328
{
329
    displaylcd_dither(x, y, width, height, data, datax, datay, stride, false);
330
}
331
 
332
void filllcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height, int color)
333
{
334
    if (width * height <= 0) return;
335
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
336
    displaylcd_sync();
337
    displaylcd_dither(x, y, width, height, &color, 0, 0, 0, true);
338
    mutex_unlock(&lcd_mutex);
339
}
340
 
341
void lcd_shutdown()
342
{
343
    displaylcd_sync();
344
    uint32_t type = lcd_detect();
345
    if (type == 2)
346
    {
347
        lcd_send_cmd(0x7);
348
        lcd_send_data(0x232);
349
        lcd_send_cmd(0x13);
350
        lcd_send_data(0x1137);
351
        lcd_send_cmd(0x7);
352
        lcd_send_data(0x201);
353
        lcd_send_cmd(0x13);
354
        lcd_send_data(0x137);
355
        lcd_send_cmd(0x7);
356
        lcd_send_data(0x200);
357
        lcd_send_cmd(0x10);
358
        lcd_send_data(0x680);
359
        lcd_send_cmd(0x12);
360
        lcd_send_data(0x160);
361
        lcd_send_cmd(0x13);
362
        lcd_send_data(0x127);
363
        lcd_send_cmd(0x10);
364
        lcd_send_data(0x600);
365
    }
366
    else
367
    {
368
        lcd_send_cmd(0x28);
369
        lcd_send_cmd(0x10);
370
    }
711 theseven 371
    sleep(20000);
669 theseven 372
}
373
 
374
void INT_DMA8()
375
{
376
    DMACOM8 = 7;
377
    lcd_dma_busy = false;
378
    lcdconsole_callback();
379
    wakeup_signal(&lcd_wakeup);
380
}
381
 
382
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
383
    ICODE_ATTR __attribute__((naked, noinline));
384
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
385
{
386
    asm volatile(
387
        "cmp r0, #0xff             \n\t"
388
        "moveq r0, #-1             \n\t"
389
        "moveq pc, lr              \n\t"
390
        "cmp r0, #0                \n\t"
391
        "movne r0, #0xff000000     \n\t"
392
        "orrne r0, r0, #0xff0000   \n\t"
393
        "mov r2, r2,lsr#2          \n\t"
394
        "orr r0, r0, r3,lsr#3      \n\t"
395
        "mov r1, r1,lsr#3          \n\t"
396
        "orr r0, r0, r2,lsl#5      \n\t"
397
        "orr r0, r0, r1,lsl#11     \n\t"
398
        "mov pc, lr                \n\t"
399
    );
400
}