Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

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