Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
560 theseven 1
 
513 theseven 2
//
3
//
4
//    Copyright 2010 TheSeven
5
//
6
//
7
//    This file is part of emCORE.
8
//
9
//    emCORE is free software: you can redistribute it and/or
10
//    modify it under the terms of the GNU General Public License as
11
//    published by the Free Software Foundation, either version 2 of the
12
//    License, or (at your option) any later version.
13
//
14
//    emCORE is distributed in the hope that it will be useful,
15
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
//    See the GNU General Public License for more details.
18
//
19
//    You should have received a copy of the GNU General Public License along
20
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
21
//
22
//
23
 
24
 
25
#include "global.h"
26
#include "thread.h"
27
#include "s5l8702.h"
28
#include "util.h"
560 theseven 29
#include "clockgates-target.h"
704 theseven 30
#include "lcd.h"
513 theseven 31
 
32
 
33
static struct dma_lli lcd_lli[(LCD_WIDTH * LCD_HEIGHT - 1) / 0xfff]
34
    IDATA_ATTR __attribute__((aligned(16)));
35
 
36
static uint16_t lcd_color IDATA_ATTR;
37
 
560 theseven 38
static struct mutex lcd_mutex IDATA_ATTR;
39
static struct wakeup lcd_wakeup IDATA_ATTR;
513 theseven 40
 
560 theseven 41
static bool lcd_dma_busy IDATA_ATTR;
513 theseven 42
 
560 theseven 43
 
513 theseven 44
int lcd_get_width()
45
{
46
    return LCD_WIDTH;
47
}
48
 
49
int lcd_get_height()
50
{
51
    return LCD_HEIGHT;
52
}
53
 
54
int lcd_get_bytes_per_pixel()
55
{
56
    return LCD_BYTESPERPIXEL;
57
}
58
 
59
int lcd_get_format()
60
{
61
    return LCD_FORMAT;
62
}
63
 
64
static void lcd_send_cmd(uint16_t cmd) ICODE_ATTR __attribute__((noinline));
65
static void lcd_send_cmd(uint16_t cmd)
66
{
67
    while (LCDSTATUS & 0x10);
68
    LCDWCMD = cmd;
69
}
70
 
71
static void lcd_send_data(uint16_t data) ICODE_ATTR __attribute__((noinline));
72
static void lcd_send_data(uint16_t data)
73
{
74
    while (LCDSTATUS & 0x10);
75
    LCDWDATA = (data & 0xff) | ((data & 0x7f00) << 1);
76
}
77
 
78
static uint32_t lcd_detect() ICODE_ATTR;
79
static uint32_t lcd_detect()
80
{
81
    return (PDAT6 & 0x30) >> 4;
82
}
83
 
581 theseven 84
void lcd_init()
85
{
86
    mutex_init(&lcd_mutex);
87
    wakeup_init(&lcd_wakeup);
88
    lcd_dma_busy = true;
89
    clockgate_dma(0, 4, true);
90
    if (!(DMAC0C4CONFIG & 1))
91
    {
92
        lcd_dma_busy = false;
93
        clockgate_dma(0, 4, false);
94
    }
95
    switch (lcd_detect())
96
    {
648 theseven 97
//    case 0:
98
//        pmu_write(0x31, 0x0b);  // Vlcd @ 2.000V
99
//        break;
581 theseven 100
    case 1:
683 theseven 101
//        pmu_write(0x31, 0x0e);  // Vlcd @ 2.300V
648 theseven 102
        break;
581 theseven 103
    case 2:
683 theseven 104
//        pmu_write(0x31, 0x12);  // Vlcd @ 2.700V
648 theseven 105
        break;
106
//    case 3:
107
//        pmu_write(0x31, 0x0b);  // Vlcd @ 2.000V
108
//        break;
683 theseven 109
//    default:
110
//        pmu_write(0x31, 0x0b);  // Vlcd @ 2.000V
648 theseven 111
    }
581 theseven 112
}
113
 
513 theseven 114
bool displaylcd_busy() ICODE_ATTR;
115
bool displaylcd_busy()
116
{
560 theseven 117
    return lcd_dma_busy;
513 theseven 118
}
119
 
120
void displaylcd_sync() ICODE_ATTR;
121
void displaylcd_sync()
122
{
560 theseven 123
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
124
    while (displaylcd_busy()) wakeup_wait(&lcd_wakeup, TIMEOUT_BLOCK);
125
    mutex_unlock(&lcd_mutex);
513 theseven 126
}
127
 
128
void displaylcd_setup(unsigned int startx, unsigned int endx,
704 theseven 129
                      unsigned int starty, unsigned int endy, bool safe) ICODE_ATTR;
513 theseven 130
void displaylcd_setup(unsigned int startx, unsigned int endx,
704 theseven 131
                      unsigned int starty, unsigned int endy, bool safe)
513 theseven 132
{
704 theseven 133
    if (!safe)
560 theseven 134
    {
135
        mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
136
        displaylcd_sync();
137
    }
704 theseven 138
    else while (DMAC0C4CONFIG & 1);
513 theseven 139
    if (lcd_detect() & 2)
140
    {
141
        lcd_send_cmd(0x210);
142
        lcd_send_data(startx);
143
        lcd_send_cmd(0x211);
144
        lcd_send_data(endx);
145
        lcd_send_cmd(0x212);
146
        lcd_send_data(starty);
147
        lcd_send_cmd(0x213);
148
        lcd_send_data(endy);
149
        lcd_send_cmd(0x200);
150
        lcd_send_data(startx);
151
        lcd_send_cmd(0x201);
152
        lcd_send_data(starty);
153
        lcd_send_cmd(0x202);
154
    }
155
    else
156
    {
157
        lcd_send_cmd(0x2a);
158
        lcd_send_data(startx >> 8);
159
        lcd_send_data(startx & 0xff);
160
        lcd_send_data(endx >> 8);
161
        lcd_send_data(endx & 0xff);
162
        lcd_send_cmd(0x2b);
163
        lcd_send_data(starty >> 8);
164
        lcd_send_data(starty & 0xff);
165
        lcd_send_data(endy >> 8);
166
        lcd_send_data(endy & 0xff);
167
        lcd_send_cmd(0x2c);
168
    }
169
}
170
 
171
static void displaylcd_dma(void* data, int pixels, bool solid) ICODE_ATTR;
172
static void displaylcd_dma(void* data, int pixels, bool solid)
173
{
174
    int i;
560 theseven 175
    lcd_dma_busy = true;
176
    clockgate_dma(0, 4, true);
513 theseven 177
    for (i = -1; i < (int)ARRAYLEN(lcd_lli) && pixels > 0; i++, pixels -= 0xfff)
178
    {
179
        bool last = i + 1 >= ARRAYLEN(lcd_lli) || pixels <= 0xfff;
180
        struct dma_lli* lli = i < 0 ? (struct dma_lli*)((int)&DMAC0C4LLI) : &lcd_lli[i];
181
        lli->srcaddr = data;
182
        lli->dstaddr = (void*)((int)&LCDWDATA);
183
        lli->nextlli = last ? NULL : &lcd_lli[i + 1];
184
        lli->control = 0x70240000 | (last ? pixels : 0xfff)
185
                     | (last ? 0x80000000 : 0) | (solid ? 0 : 0x4000000);
648 theseven 186
        if (!solid) data += 0x1ffe;
513 theseven 187
    }
188
    clean_dcache();
189
    DMAC0C4CONFIG = 0x88c1;
190
}
191
 
192
void displaylcd_native(unsigned int startx, unsigned int endx,
193
                       unsigned int starty, unsigned int endy, void* data)
194
{
195
    int pixels = (endx - startx + 1) * (endy - starty + 1);
196
    if (pixels <= 0) return;
704 theseven 197
    displaylcd_setup(startx, endx, starty, endy, false);
513 theseven 198
    displaylcd_dma(data, pixels, false);
704 theseven 199
    mutex_unlock(&lcd_mutex);
513 theseven 200
}
201
 
704 theseven 202
void displaylcd_safe_native(unsigned int startx, unsigned int endx,
203
                            unsigned int starty, unsigned int endy, void* data)
204
{
205
    int pixels = (endx - startx + 1) * (endy - starty + 1);
206
    if (pixels <= 0) return;
207
    displaylcd_setup(startx, endx, starty, endy, true);
208
    displaylcd_dma(data, pixels, false);
209
}
210
 
513 theseven 211
void filllcd_native(unsigned int startx, unsigned int endx,
212
                    unsigned int starty, unsigned int endy, int color)
213
{
214
    int pixels = (endx - startx + 1) * (endy - starty + 1);
215
    if (pixels <= 0) return;
704 theseven 216
    displaylcd_setup(startx, endx, starty, endy, false);
513 theseven 217
    lcd_color = color;
218
    displaylcd_dma(&lcd_color, pixels, true);
704 theseven 219
    mutex_unlock(&lcd_mutex);
513 theseven 220
}
221
 
222
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
223
                       unsigned int height, void* data, unsigned int datax,
224
                       unsigned int datay, unsigned int stride, bool solid)
225
     ICODE_ATTR __attribute__((naked,noinline));
226
void displaylcd_dither(unsigned int x, unsigned int y, unsigned int width,
227
                       unsigned int height, void* data, unsigned int datax,
228
                       unsigned int datay, unsigned int stride, bool solid)
229
{
230
    __asm__ volatile("    muls r12, r2, r3             \n");
231
    __asm__ volatile("    bxeq lr                      \n");
704 theseven 232
    __asm__ volatile("    stmfd sp!, {r1-r11,lr}       \n");
233
    __asm__ volatile("    mov r12, #0                  \n");
234
    __asm__ volatile("    str r12, [sp]                \n");
513 theseven 235
    __asm__ volatile("    mov r12, r2                  \n");
236
    __asm__ volatile("    add r8, r2, r2,lsl#1         \n");
237
    __asm__ volatile("    add r3, r1, r3               \n");
238
    __asm__ volatile("    sub r3, r3, #1               \n");
239
    __asm__ volatile("    mov r2, r1                   \n");
240
    __asm__ volatile("    add r1, r0, r12              \n");
241
    __asm__ volatile("    sub r1, r1, #1               \n");
242
    __asm__ volatile("    bl displaylcd_setup          \n");
704 theseven 243
    __asm__ volatile("    add sp, sp, #4               \n");
513 theseven 244
    __asm__ volatile("    mov r0, r8                   \n");
245
    __asm__ volatile("    bl malloc                    \n");
246
    __asm__ volatile("    cmp r0, #0                   \n");
247
    __asm__ volatile("    beq displaylcd_dither_unlock \n");
248
    __asm__ volatile("    mov r2, r8                   \n");
249
    __asm__ volatile("    mov r1, #0                   \n");
250
    __asm__ volatile("    mov r8, r0                   \n");
251
    __asm__ volatile("    bl memset                    \n");
252
    __asm__ volatile("    ldr r0, [sp,#0x30]           \n");
253
    __asm__ volatile("    ldr r1, [sp,#0x34]           \n");
254
    __asm__ volatile("    ldr r11, [sp,#0x38]          \n");
255
    __asm__ volatile("    ldr r3, [sp,#0x2c]           \n");
256
    __asm__ volatile("    mla r0, r1, r11, r0          \n");
257
    __asm__ volatile("    ldr r12, [sp,#0x04]          \n");
258
    __asm__ volatile("    ldr r2, [sp,#0x3c]           \n");
259
    __asm__ volatile("    add r3, r3, r0,lsl#1         \n");
260
    __asm__ volatile("    cmp r2, #0                   \n");
261
    __asm__ volatile("    ldreq r1, [sp]               \n");
262
    __asm__ volatile("    add r3, r3, r0               \n");
263
    __asm__ volatile("    subeq r11, r11, r1           \n");
264
    __asm__ volatile("    add r11, r11, r11,lsl#1      \n");
265
    __asm__ volatile("    movne r10, #3                \n");
266
    __asm__ volatile("    moveq r10, #0                \n");
267
    __asm__ volatile("    ldr r9, =0x38300040          \n");
268
    __asm__ volatile("displaylcd_dither_y:             \n");
269
    __asm__ volatile("    ldr lr, [sp]                 \n");
270
    __asm__ volatile("    mov r4, #0                   \n");
271
    __asm__ volatile("    mov r5, #0                   \n");
272
    __asm__ volatile("    mov r6, #0                   \n");
273
    __asm__ volatile("    mov r7, r8                   \n");
274
    __asm__ volatile("displaylcd_dither_x:             \n");
275
    __asm__ volatile("    mov r2, #0                   \n");
276
    __asm__ volatile("    ldrb r1, [r3], #1            \n");
277
    __asm__ volatile("    ldrsb r0, [r7]               \n");
278
    __asm__ volatile("    add r1, r1, r4               \n");
279
    __asm__ volatile("    add r1, r1, r0               \n");
280
    __asm__ volatile("    cmp r1, #0xff                \n");
648 theseven 281
    __asm__ volatile("    mvnhi r1, r1,asr#31          \n");
282
    __asm__ volatile("    andhi r1, r1, #0xff          \n");
513 theseven 283
    __asm__ volatile("    mov r0, r1,lsr#3             \n");
284
    __asm__ volatile("    orr r2, r0,lsl#11            \n");
285
    __asm__ volatile("    sub r1, r1, r0,lsl#3         \n");
286
    __asm__ volatile("    sub r1, r1, r0,lsr#2         \n");
287
    __asm__ volatile("    mov r4, r4,lsr#1             \n");
288
    __asm__ volatile("    add r4, r4, r1,lsr#2         \n");
289
    __asm__ volatile("    strb r4, [r7], #1            \n");
290
    __asm__ volatile("    mov r4, r1,asr#1             \n");
291
    __asm__ volatile("    ldrb r1, [r3], #1            \n");
292
    __asm__ volatile("    ldrsb r0, [r7]               \n");
293
    __asm__ volatile("    add r1, r1, r5               \n");
294
    __asm__ volatile("    add r1, r1, r0               \n");
295
    __asm__ volatile("    cmp r1, #0xff                \n");
648 theseven 296
    __asm__ volatile("    mvnhi r1, r1,asr#31          \n");
297
    __asm__ volatile("    andhi r1, r1, #0xff          \n");
513 theseven 298
    __asm__ volatile("    mov r0, r1,lsr#2             \n");
299
    __asm__ volatile("    orr r2, r0,lsl#5             \n");
300
    __asm__ volatile("    sub r1, r1, r0,lsl#2         \n");
301
    __asm__ volatile("    sub r1, r1, r0,lsr#4         \n");
302
    __asm__ volatile("    mov r5, r5,lsr#1             \n");
303
    __asm__ volatile("    add r5, r5, r1,lsr#2         \n");
304
    __asm__ volatile("    strb r5, [r7], #1            \n");
305
    __asm__ volatile("    mov r5, r1,asr#1             \n");
306
    __asm__ volatile("    ldrb r1, [r3], #1            \n");
307
    __asm__ volatile("    ldrsb r0, [r7]               \n");
308
    __asm__ volatile("    add r1, r1, r6               \n");
309
    __asm__ volatile("    add r1, r1, r0               \n");
310
    __asm__ volatile("    cmp r1, #0xff                \n");
648 theseven 311
    __asm__ volatile("    mvnhi r1, r1,asr#31          \n");
312
    __asm__ volatile("    andhi r1, r1, #0xff          \n");
513 theseven 313
    __asm__ volatile("    mov r0, r1,lsr#3             \n");
314
    __asm__ volatile("    orr r2, r0                   \n");
315
    __asm__ volatile("    sub r1, r1, r0,lsl#3         \n");
316
    __asm__ volatile("    sub r1, r1, r0,lsr#2         \n");
317
    __asm__ volatile("    mov r6, r6,lsr#1             \n");
318
    __asm__ volatile("    add r6, r6, r1,lsr#2         \n");
319
    __asm__ volatile("    strb r6, [r7], #1            \n");
320
    __asm__ volatile("    mov r6, r1,asr#1             \n");
321
    __asm__ volatile("    str r2, [r9]                 \n");
322
    __asm__ volatile("    sub r3, r3, r10              \n");
323
    __asm__ volatile("    subs lr, lr, #1              \n");
324
    __asm__ volatile("    bne displaylcd_dither_x      \n");
325
    __asm__ volatile("    add r3, r3, r11              \n");
326
    __asm__ volatile("    subs r12, r12, #1            \n");
327
    __asm__ volatile("    bne displaylcd_dither_y      \n");
328
    __asm__ volatile("displaylcd_dither_free:          \n");
329
    __asm__ volatile("    mov r0, r8                   \n");
330
    __asm__ volatile("    bl free                      \n");
331
    __asm__ volatile("displaylcd_dither_unlock:        \n");
332
    __asm__ volatile("    ldr r0, =lcd_mutex           \n");
333
    __asm__ volatile("    bl mutex_unlock              \n");
334
    __asm__ volatile("    ldmfd sp!, {r2-r11,pc}       \n");
335
}
336
 
337
void displaylcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height,
338
                void* data, unsigned int datax, unsigned int datay, unsigned int stride)
339
{
340
    displaylcd_dither(x, y, width, height, data, datax, datay, stride, false);
341
}
342
 
343
void filllcd(unsigned int x, unsigned int y, unsigned int width, unsigned int height, int color)
344
{
345
    if (width * height <= 0) return;
560 theseven 346
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
521 theseven 347
    displaylcd_sync();
513 theseven 348
    lcd_color = color;
349
    displaylcd_dither(x, y, width, height, &lcd_color, 0, 0, 0, true);
350
    mutex_unlock(&lcd_mutex);
351
}
352
 
353
void lcd_shutdown()
354
{
683 theseven 355
    mutex_lock(&lcd_mutex, TIMEOUT_BLOCK);
513 theseven 356
    displaylcd_sync();
357
    uint32_t type = lcd_detect();
358
    if (type & 2)
359
    {
360
        lcd_send_cmd(0x7);
361
        lcd_send_data(0x172);
362
        lcd_send_cmd(0x30);
363
        lcd_send_data(0x3ff);
364
        sleep(90000);
365
        lcd_send_cmd(0x7);
366
        lcd_send_data(0x120);
367
        lcd_send_cmd(0x30);
368
        lcd_send_data(0x0);
369
        lcd_send_cmd(0x100);
370
        lcd_send_data(0x780);
371
        lcd_send_cmd(0x7);
372
        lcd_send_data(0x0);
373
        lcd_send_cmd(0x101);
374
        lcd_send_data(0x260);
375
        lcd_send_cmd(0x102);
376
        lcd_send_data(0xa9);
377
        sleep(30000);
378
        lcd_send_cmd(0x100);
379
        lcd_send_data(0x700);
380
        lcd_send_cmd(0x100);
381
        lcd_send_data(0x704);
382
    }
383
    else if (type == 1)
384
    {
385
        lcd_send_cmd(0x28);
386
        lcd_send_cmd(0x10);
387
        sleep(100000);
388
    }
389
    else
390
    {
391
        lcd_send_cmd(0x28);
392
        sleep(50000);
393
        lcd_send_cmd(0x10);
394
        sleep(50000);
395
    }
396
}
397
 
398
void INT_DMAC0C4()
399
{
400
    DMAC0INTTCCLR = 0x10;
560 theseven 401
    lcd_dma_busy = false;
402
    clockgate_dma(0, 4, false);
513 theseven 403
    lcdconsole_callback();
560 theseven 404
    wakeup_signal(&lcd_wakeup);
513 theseven 405
}
406
 
407
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
408
    ICODE_ATTR __attribute__((naked, noinline));
409
int lcd_translate_color(uint8_t alpha, uint8_t red, uint8_t green, uint8_t blue)
410
{
411
    asm volatile(
412
        "cmp r0, #0xff             \n\t"
413
        "moveq r0, #-1             \n\t"
414
        "moveq pc, lr              \n\t"
415
        "cmp r0, #0                \n\t"
416
        "movne r0, #0xff000000     \n\t"
417
        "orrne r0, r0, #0xff0000   \n\t"
418
        "mov r2, r2,lsr#2          \n\t"
419
        "orr r0, r0, r3,lsr#3      \n\t"
420
        "mov r1, r1,lsr#3          \n\t"
421
        "orr r0, r0, r2,lsl#5      \n\t"
422
        "orr r0, r0, r1,lsl#11     \n\t"
423
        "mov pc, lr                \n\t"
424
    );
425
}