1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
lcdif-rk27xx.c
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 Marcin Bukat
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h"
#include "kernel.h"
#include "lcd.h"
#include "system.h"
#include "cpu.h"
#include "lcdif-rk27xx.h"

/* dwdma linked list struct */
struct llp_t {
    uint32_t sar;
    uint32_t dar;
    struct llp_t *llp;
    uint32_t ctl_l;
    uint32_t ctl_h;
    uint32_t dstat;
};

/* structs which describe full screen update */
struct llp_t scr_llp[LCD_HEIGHT];

static void llp_setup(void *src, void *dst, struct llp_t *llp, uint32_t size)
{
    llp->sar = (uint32_t)src;
    llp->dar = (uint32_t)dst;
    llp->llp = llp + 1;
    llp->ctl_h = size;
    llp->ctl_l = (1<<20) |
                 (1<<23) |
                 (1<<17) |
                 (2<<1)  |
                 (2<<4)  |
                 (3<<11) |
                 (3<<14) |
                 (1<<27) |
                 (1<<28);
}

static void llp_end(struct llp_t *llp)
{
    llp->ctl_l &= ~((1<<27)|(1<<28));
}

static void create_llp(void)
{
    int i = 0;

    /* build LLPs */
    for (i=0; i<LCD_HEIGHT; i++)
        llp_setup(FBADDR(0,i), (void *)&LCD_BUFF+(i%4)*LCD_WIDTH/2, &scr_llp[i], LCD_WIDTH/2);
    llp_end(&scr_llp[LCD_HEIGHT-1]);
}

static void dwdma_init(void)
{
    DWDMA_DMA_CHEN = 0xf00;
    DWDMA_CLEAR_BLOCK = 0x0f;
    DWDMA_DMA_CFG = 1; /* global enable */
}

static void dwdma_start(uint8_t ch, struct llp_t *llp, uint8_t handshake)
{
    DWDMA_SAR(ch) = 0;
    DWDMA_DAR(ch) = 0;
    DWDMA_LLP(ch) = (uint32_t)llp;
    DWDMA_CTL_L(ch) = (1<<20) |
                      (1<<23) |
                      (1<<17) |
                      (2<<1)  |
                      (2<<4)  |
                      (3<<11) |
                      (3<<14) |
                      (1<<27) |
                      (1<<28);

   DWDMA_CTL_H(ch) = 1;
   DWDMA_CFG_L(ch) = (7<<5);
   DWDMA_CFG_H(ch) = (handshake<<11)|(1<<2);
   DWDMA_SGR(ch) = (13<<20);
   DWDMA_DMA_CHEN = (0x101<<ch);

}

unsigned int lcd_data_transform(unsigned int data)
{
    unsigned int r, g, b;

#if defined(RK27_GENERIC)
    /* 18 bit interface */
    r = (data & 0x0000fc00)<<8;
    /* g = ((data & 0x00000300) >> 2) | ((data & 0x000000e0) >> 3); */
    g = ((data & 0x00000300) << 6) | ((data & 0x000000e0) << 5);
    b = (data & 0x00000001f) << 3;
#elif defined(HM60X) || defined(HM801)
    /* 16 bit interface */
    r = (data & 0x0000f800) << 8;
    g = (data & 0x000007e0) << 5;
    b = (data & 0x0000001f) << 3;
#else
#error "Unknown target"
#endif

    return (r | g | b);
}

void lcd_cmd(unsigned int cmd)
{
    LCD_COMMAND = lcd_data_transform(cmd);
}

void lcd_data(unsigned int data)
{
    LCD_DATA = lcd_data_transform(data);
}

void lcd_write_reg(unsigned int reg, unsigned int val)
{
    lcd_cmd(reg);
    lcd_data(val);
}

void lcdctrl_bypass(unsigned int on_off)
{
    while (!(LCDC_STA & LCDC_MCU_IDLE));

    if (on_off)
        MCU_CTRL |= MCU_CTRL_BYPASS;
    else
        MCU_CTRL &= ~MCU_CTRL_BYPASS;
}

/* This part is unclear. I am unable to use buffered/FIFO based writes
 * to lcd. Depending on settings of IF I get various patterns on display
 * but not what I want to display apparently.
 */
static void lcdctrl_init(void)
{
    /* alpha b111
     * stop at current frame complete
     * MCU mode
     * 24b RGB
     */
    LCDC_CTRL = ALPHA(7) | LCDC_STOP | LCDC_MCU;
    MCU_CTRL = ALPHA_BASE(0x3f) | MCU_CTRL_BYPASS;

    HOR_BP = LCD_WIDTH + 3;    /* define horizonatal active region */
    VERT_BP = LCD_HEIGHT;       /* define vertical active region */
 //   VERT_PERIOD = 0xfff;  /* CSn/WEn/RDn signal timings */

    LINE0_YADDR = 0;
    LINE1_YADDR = 1 * LCD_WIDTH/2;
    LINE2_YADDR = 2 * LCD_WIDTH/2;
    LINE3_YADDR = 3 * LCD_WIDTH/2;

    LINE0_UVADDR = 1;
    LINE1_UVADDR = (1 * LCD_WIDTH/2) + 1;
    LINE2_UVADDR = (2 * LCD_WIDTH/2) + 1;
    LINE3_UVADDR = (3 * LCD_WIDTH/2) + 1;

    LCDC_INTR_MASK = 0; /* INTR_MASK_EVENLINE; */
}

/* configure pins to drive lcd in 18bit mode (16bit mode for HiFiMAN's) */
static void iomux_lcd(enum lcdif_mode_t mode)
{
    unsigned long muxa;

    muxa = SCU_IOMUXA_CON & ~(IOMUX_LCD_VSYNC|IOMUX_LCD_DEN|0xff);

    if (mode == LCDIF_18BIT)
    {
        muxa |= IOMUX_LCD_D18|IOMUX_LCD_D20|IOMUX_LCD_D22|IOMUX_LCD_D17|IOMUX_LCD_D16;
    }

    SCU_IOMUXA_CON = muxa;
    SCU_IOMUXB_CON |= IOMUX_LCD_D815;
}

void lcdif_init(enum lcdif_mode_t mode)
{
    iomux_lcd(mode);   /* setup pins for lcd interface */
    dwdma_init();
    lcdctrl_init();    /* basic lcdc module configuration */
    create_llp();
    lcdctrl_bypass(1); /* run in bypass mode - all writes goes directly to lcd controller */
}

/* This is ugly hack. We drive lcd in bypass mode
 * where all writes goes directly to lcd controller.
 * This is suboptimal at best. IF module povides
 * FIFO, internal sram buffer, hardware scaller,
 * DMA signals, hardware alpha blending and more.
 * BUT the fact is that I have no idea how to use
 * this modes. Datasheet floating around is very
 * unclean in this regard and OF uses ackward
 * lcd update routines which are hard to understand.
 * Moreover OF sets some bits in IF module registers
 * which are referred as reseved in datasheet.
 */
void lcd_update()
{
    lcdctrl_bypass(0);
    while (!(LCDC_STA & LCDC_MCU_IDLE));
    commit_discard_dcache_range(FBADDR(0,0), 2*LCD_WIDTH*LCD_HEIGHT);
    dwdma_start(0, scr_llp, 6);
    udelay(100);

    MCU_CTRL=(1<<1)|(1<<2)|(1<<5);
    //lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
}


lcd-rk27generic.c
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 Marcin Bukat
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h"
#include "kernel.h"
#include "lcd.h"
#include "system.h"
#include "cpu.h"
#include "spfd5420a.h"
#include "lcdif-rk27xx.h"

/* TODO: convert to udelay() */
static inline void delay_nop(int cycles)
{
    asm volatile ("1: subs %[n], %[n], #1     \n\t"
                  "   bne  1b"
                  :  
                  : [n] "r" (cycles));
}


/* converts RGB565 pixel into internal lcd bus format */
static unsigned int lcd_pixel_transform(unsigned short rgb565)
{
    unsigned int r, g, b;
    b = rgb565 & 0x1f;
    g = (rgb565 >> 5) & 0x3f;
    r = (rgb565 >> 11) & 0x1f;

    return r<<19 | g<<10 | b<<3;
}

/* not tested */
static void lcd_sleep(bool sleep)
{
    if (sleep)
    {
        /* enter sleep mode */
        lcd_write_reg(DISPLAY_CTRL1, 0x0170);
        delay_nop(50);
        lcd_write_reg(DISPLAY_CTRL1, 0x0000);
        delay_nop(50);
        lcd_write_reg(PWR_CTRL1,     0x14B4);
    }
    else
    {
         /* return to normal operation */
        lcd_write_reg(PWR_CTRL1,     0x14B0);
        delay_nop(50);
        lcd_write_reg(DISPLAY_CTRL1, 0x0173);
    }

    lcd_cmd(GRAM_WRITE);
}

static void lcd_display_init(void)
{
    unsigned int x, y;

    lcd_write_reg(RESET, 0x0001);
    delay_nop(10000);
    lcd_write_reg(RESET, 0x0000);
    delay_nop(10000);
    lcd_write_reg(IF_ENDIAN,       0x0000); /* order of receiving data */
    lcd_write_reg(DRIVER_OUT_CTRL, 0x0000);
    lcd_write_reg(ENTRY_MODE,      0x1038);
    lcd_write_reg(WAVEFORM_CTRL,   0x0100); 
    lcd_write_reg(SHAPENING_CTRL,  0x0000);
    lcd_write_reg(DISPLAY_CTRL2,   0x0808);
    lcd_write_reg(LOW_PWR_CTRL1,   0x0001);
    lcd_write_reg(LOW_PWR_CTRL2,   0x0010);
    lcd_write_reg(EXT_DISP_CTRL1,  0x0000);
    lcd_write_reg(EXT_DISP_CTRL2,  0x0000);
    lcd_write_reg(BASE_IMG_SIZE,   0x3100);
    lcd_write_reg(BASE_IMG_CTRL,   0x0001);
    lcd_write_reg(VSCROLL_CTRL,    0x0000);
    lcd_write_reg(PART1_POS,       0x0000);
    lcd_write_reg(PART1_START,     0x0000);
    lcd_write_reg(PART1_END,       0x018F);
    lcd_write_reg(PART2_POS,       0x0000);
    lcd_write_reg(PART2_START,     0x0000);
    lcd_write_reg(PART2_END,       0x0000);

    lcd_write_reg(PANEL_IF_CTRL1,  0x0011);
    delay_nop(10000);
    lcd_write_reg(PANEL_IF_CTRL2,  0x0202);
    lcd_write_reg(PANEL_IF_CTRL3,  0x0300);
    delay_nop(10000);
    lcd_write_reg(PANEL_IF_CTRL4,  0x021E);
    lcd_write_reg(PANEL_IF_CTRL5,  0x0202);
    lcd_write_reg(PANEL_IF_CTRL6,  0x0100);
    lcd_write_reg(FRAME_MKR_CTRL,  0x0000);
    lcd_write_reg(MDDI_CTRL,       0x0000);

    lcd_write_reg(GAMMA_CTRL1,     0x0101);
    lcd_write_reg(GAMMA_CTRL2,     0x0000);
    lcd_write_reg(GAMMA_CTRL3,     0x0016);
    lcd_write_reg(GAMMA_CTRL4,     0x2913);
    lcd_write_reg(GAMMA_CTRL5,     0x260B);
    lcd_write_reg(GAMMA_CTRL6,     0x0101);
    lcd_write_reg(GAMMA_CTRL7,     0x1204);
    lcd_write_reg(GAMMA_CTRL8,     0x0415);
    lcd_write_reg(GAMMA_CTRL9,     0x0205);
    lcd_write_reg(GAMMA_CTRL10,    0x0303);
    lcd_write_reg(GAMMA_CTRL11,    0x0E05);
    lcd_write_reg(GAMMA_CTRL12,    0x0D01);
    lcd_write_reg(GAMMA_CTRL13,    0x010D);
    lcd_write_reg(GAMMA_CTRL14,    0x050E);
    lcd_write_reg(GAMMA_CTRL15,    0x0303);
    lcd_write_reg(GAMMA_CTRL16,    0x0502);

    /* power on */
    lcd_write_reg(DISPLAY_CTRL1,   0x0001);
    lcd_write_reg(PWR_CTRL6,       0x0001);
    lcd_write_reg(PWR_CTRL7,       0x0060);
    delay_nop(50000);
    lcd_write_reg(PWR_CTRL1,       0x16B0);
    delay_nop(10000);
    lcd_write_reg(PWR_CTRL2,       0x0147);
    delay_nop(10000);
    lcd_write_reg(PWR_CTRL3,       0x0117);
    delay_nop(10000);
    lcd_write_reg(PWR_CTRL4,       0x2F00);
    delay_nop(50000);
    lcd_write_reg(VCOM_HV2,        0x0000); /* src 0x0090 */
    delay_nop(10000);
    lcd_write_reg(VCOM_HV1,        0x0008); /* src 0x000A */
    lcd_write_reg(PWR_CTRL3,       0x01BE);
    delay_nop(10000);

    /* addresses setup */
    lcd_write_reg(WINDOW_H_START,  0x0000);
    lcd_write_reg(WINDOW_H_END,    0x00EF); /* 239 */
    lcd_write_reg(WINDOW_V_START,  0x0000);
    lcd_write_reg(WINDOW_V_END,    0x018F); /* 399 */
    lcd_write_reg(GRAM_H_ADDR,     0x0000);
    lcd_write_reg(GRAM_V_ADDR,     0x0000);

    /* display on */
    lcd_write_reg(DISPLAY_CTRL1,   0x0021);
    delay_nop(40000);
    lcd_write_reg(DISPLAY_CTRL1,   0x0061);
    delay_nop(100000);
    lcd_write_reg(DISPLAY_CTRL1,   0x0173);
    delay_nop(300000);
   
 
    /* clear screen */
    lcd_cmd(GRAM_WRITE);

    for (x=0; x<LCD_WIDTH; x++)
        for(y=0; y<LCD_HEIGHT; y++)
            lcd_data(0x000000);

    lcd_sleep(false);
}

void lcd_init_device(void)
{
    lcdif_init(LCDIF_18BIT);
    lcd_display_init();
}

void lcd_update_rect(int x, int y, int width, int height)
{
    (void)x;
    (void)y;
    (void)width;
    (void)height;

    lcd_update();
#if 0
    int px = x, py = y;
    int pxmax = x + width, pymax = y + height;

    /* addresses setup */
    lcd_write_reg(WINDOW_H_START,  y);
    lcd_write_reg(WINDOW_H_END,    pymax-1);
    lcd_write_reg(WINDOW_V_START,  x);
    lcd_write_reg(WINDOW_V_END,    pxmax-1);
    lcd_write_reg(GRAM_H_ADDR,     y);
    lcd_write_reg(GRAM_V_ADDR,     x);

    lcd_cmd(GRAM_WRITE);

    for (py=y; py<pymax; py++)
    {
        for (px=x; px<pxmax; px++)
            LCD_DATA = lcd_pixel_transform(*FBADDR(px,py));
    }
#endif
}

/* Blit a YUV bitmap directly to the LCD */
void lcd_blit_yuv(unsigned char * const src[3],
                  int src_x, int src_y, int stride,
                  int x, int y, int width, int height)
{
    (void)src;
    (void)src_x;
    (void)src_y;
    (void)stride;
    (void)x;
    (void)y;
    (void)width;
    (void)height;
}