Also fix Debug->View partitions when SECTOR_SIZE!=512
[kugel-rb.git] / firmware / target / arm / as3525 / sansa-e200v2 / lcd-e200v2.c
blobf48861f4d7d927ea431eb6c3637940c064254110
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #include "cpu.h"
24 #include "lcd.h"
25 #include "kernel.h"
26 #include "thread.h"
27 #include <string.h>
28 #include <stdlib.h>
29 #include "file.h"
30 #include "debug.h"
31 #include "system.h"
32 #include "font.h"
33 #include "bidi.h"
34 #include "clock-target.h"
36 static bool display_on = false; /* is the display turned on? */
37 static bool display_flipped = false;
38 /* we need to write a red pixel for correct button reads
39 * (see lcd_button_support()), but that must not happen while the lcd is
40 * updating so block lcd_button_support the during updates */
41 static volatile bool lcd_busy = false;
43 /* register defines */
44 #define R_START_OSC 0x00
45 #define R_DRV_OUTPUT_CONTROL 0x01
46 #define R_DRV_WAVEFORM_CONTROL 0x02
47 #define R_ENTRY_MODE 0x03
48 #define R_COMPARE_REG1 0x04
49 #define R_COMPARE_REG2 0x05
51 #define R_DISP_CONTROL1 0x07
52 #define R_DISP_CONTROL2 0x08
53 #define R_DISP_CONTROL3 0x09
55 #define R_FRAME_CYCLE_CONTROL 0x0b
56 #define R_EXT_DISP_IF_CONTROL 0x0c
58 #define R_POWER_CONTROL1 0x10
59 #define R_POWER_CONTROL2 0x11
60 #define R_POWER_CONTROL3 0x12
61 #define R_POWER_CONTROL4 0x13
63 #define R_RAM_ADDR_SET 0x21
64 #define R_WRITE_DATA_2_GRAM 0x22
66 #define R_GAMMA_FINE_ADJ_POS1 0x30
67 #define R_GAMMA_FINE_ADJ_POS2 0x31
68 #define R_GAMMA_FINE_ADJ_POS3 0x32
69 #define R_GAMMA_GRAD_ADJ_POS 0x33
71 #define R_GAMMA_FINE_ADJ_NEG1 0x34
72 #define R_GAMMA_FINE_ADJ_NEG2 0x35
73 #define R_GAMMA_FINE_ADJ_NEG3 0x36
74 #define R_GAMMA_GRAD_ADJ_NEG 0x37
76 #define R_GAMMA_AMP_ADJ_RES_POS 0x38
77 #define R_GAMMA_AMP_AVG_ADJ_RES_NEG 0x39
79 #define R_GATE_SCAN_POS 0x40
80 #define R_VERT_SCROLL_CONTROL 0x41
81 #define R_1ST_SCR_DRV_POS 0x42
82 #define R_2ND_SCR_DRV_POS 0x43
83 #define R_HORIZ_RAM_ADDR_POS 0x44
84 #define R_VERT_RAM_ADDR_POS 0x45
86 /* Flip Flag */
87 #define R_ENTRY_MODE_HORZ_NORMAL 0x7030
88 #define R_ENTRY_MODE_HORZ_FLIPPED 0x7000
89 static unsigned short r_entry_mode = R_ENTRY_MODE_HORZ_NORMAL;
90 #define R_ENTRY_MODE_VERT 0x7038
91 #define R_ENTRY_MODE_SOLID_VERT 0x1038
92 #define R_ENTRY_MODE_VIDEO_NORMAL 0x7020
93 #define R_ENTRY_MODE_VIDEO_FLIPPED 0x7010
96 /* Reverse Flag */
97 #define R_DISP_CONTROL_NORMAL 0x0004
98 #define R_DISP_CONTROL_REV 0x0000
99 static unsigned short r_disp_control_rev = R_DISP_CONTROL_NORMAL;
101 static inline void lcd_delay(int x)
103 do {
104 asm volatile ("nop\n");
105 } while (x--);
108 /* DBOP initialisation, do what OF does */
109 static void ams3525_dbop_init(void)
111 CGU_DBOP = (1<<3) | AS3525_DBOP_DIV;
113 DBOP_TIMPOL_01 = 0xe167e167;
114 DBOP_TIMPOL_23 = 0xe167006e;
116 /* short count, 16bit write, read-timing =8 */
117 DBOP_CTRL = (1<<18)|(1<<12)|(8<<0);
119 GPIOB_AFSEL = 0xfc;
120 GPIOC_AFSEL = 0xff;
122 DBOP_TIMPOL_23 = 0x6000e;
124 /* short count,write enable, 16bit write, read-timing =8 */
125 DBOP_CTRL = (1<<18)|(1<<16)|(1<<12)|(8<<0);
126 DBOP_TIMPOL_01 = 0x6e167;
127 DBOP_TIMPOL_23 = 0xa167e06f;
129 /* TODO: The OF calls some other functions here, but maybe not important */
132 static void lcd_write_single_data16(unsigned short value)
134 DBOP_CTRL &= ~(1<<14|1<<13);
135 lcd_delay(10);
136 DBOP_DOUT16 = value;
137 while ((DBOP_STAT & (1<<10)) == 0);
140 static void lcd_write_cmd(int cmd)
142 /* Write register */
143 DBOP_TIMPOL_23 = 0xa167006e;
144 lcd_write_single_data16(cmd);
146 /* Wait for fifo to empty */
147 while ((DBOP_STAT & (1<<10)) == 0);
149 /* Fuze OF has this loop and it seems to help us now also */
150 int delay=8;
151 while(delay--);
153 DBOP_TIMPOL_23 = 0xa167e06f;
156 void lcd_write_data(const fb_data* p_bytes, int count)
158 const long *data;
159 if ((int)p_bytes & 0x3)
160 { /* need to do a single 16bit write beforehand if the address is */
161 /* not word aligned*/
162 lcd_write_single_data16(*p_bytes);
163 count--;p_bytes++;
165 /* from here, 32bit transfers are save */
166 /* set it to transfer 4*(outputwidth) units at a time, */
167 /* if bit 12 is set it only does 2 halfwords though */
168 DBOP_CTRL |= (1<<13|1<<14);
169 data = (long*)p_bytes;
170 while (count > 1)
172 DBOP_DOUT32 = *data++;
173 count -= 2;
175 /* Wait if push fifo is full */
176 while ((DBOP_STAT & (1<<6)) != 0);
178 /* While push fifo is not empty */
179 while ((DBOP_STAT & (1<<10)) == 0);
181 /* due to the 32bit alignment requirement or uneven count,
182 * we possibly need to do a 16bit transfer at the end also */
183 if (count > 0)
184 lcd_write_single_data16(*(fb_data*)data);
187 static void lcd_write_reg(int reg, int value)
189 fb_data data = value;
191 lcd_write_cmd(reg);
192 lcd_write_single_data16(data);
195 /*** hardware configuration ***/
197 void lcd_set_contrast(int val)
199 (void)val;
202 void lcd_set_invert_display(bool yesno)
204 r_disp_control_rev = yesno ? R_DISP_CONTROL_REV :
205 R_DISP_CONTROL_NORMAL;
207 if (display_on)
209 lcd_write_reg(R_DISP_CONTROL1, 0x0033 | r_disp_control_rev);
214 /* turn the display upside down */
215 void lcd_set_flip(bool yesno)
217 display_flipped = yesno;
219 r_entry_mode = yesno ? R_ENTRY_MODE_HORZ_FLIPPED :
220 R_ENTRY_MODE_HORZ_NORMAL;
223 static void lcd_window(int xmin, int ymin, int xmax, int ymax)
225 if (!display_flipped)
227 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (xmax << 8) | xmin);
228 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
229 lcd_write_reg(R_RAM_ADDR_SET, (ymin << 8) | xmin);
231 else
233 lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
234 ((LCD_WIDTH-1 - xmin) << 8) | (LCD_WIDTH-1 - xmax));
235 lcd_write_reg(R_VERT_RAM_ADDR_POS,
236 ((LCD_HEIGHT-1 - ymin) << 8) | (LCD_HEIGHT-1 - ymax));
237 lcd_write_reg(R_RAM_ADDR_SET,
238 ((LCD_HEIGHT-1 - ymin) << 8) | (LCD_WIDTH-1 - xmin));
242 static void _display_on(void)
244 /* Initialisation the display the same way as the original firmware */
246 lcd_write_reg(R_START_OSC, 0x0001); /* Start Oscilation */
248 lcd_write_reg(R_DRV_OUTPUT_CONTROL, 0x011b); /* 220 lines, GS=0, SS=1 */
250 /* B/C = 1: n-line inversion form
251 * EOR = 1: polarity inversion occurs by applying an EOR to odd/even
252 * frame select signal and an n-line inversion signal.
253 * FLD = 01b: 1 field interlaced scan, external display iface */
254 lcd_write_reg(R_DRV_WAVEFORM_CONTROL, 0x0700);
256 /* Address counter updated in horizontal direction; left to right;
257 * vertical increment horizontal increment.
258 * data format for 8bit transfer or spi = 65k (5,6,5) */
259 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
261 /* Replace data on writing to GRAM */
262 lcd_write_reg(R_COMPARE_REG1, 0);
263 lcd_write_reg(R_COMPARE_REG2, 0);
265 /* GON = 0, DTE = 0, D1-0 = 00b */
266 lcd_write_reg(R_DISP_CONTROL1, 0x0000 | r_disp_control_rev);
268 /* Front porch lines: 2; Back porch lines: 2; */
269 lcd_write_reg(R_DISP_CONTROL2, 0x0203);
271 /* Scan cycle = 0 frames */
272 lcd_write_reg(R_DISP_CONTROL3, 0x0000);
274 /* 16 clocks */
275 lcd_write_reg(R_FRAME_CYCLE_CONTROL, 0x0000);
277 /* 18-bit RGB interface (one transfer/pixel)
278 * internal clock operation;
279 * System interface/VSYNC interface */
280 lcd_write_reg(R_EXT_DISP_IF_CONTROL, 0x0000);
283 /* zero everything*/
284 lcd_write_reg(R_POWER_CONTROL1, 0x0000); /* STB = 0, SLP = 0 */
286 lcd_delay(10);
288 /* initialise power supply */
290 /* DC12-10 = 000b: Step-up1 = clock/8,
291 * DC02-00 = 000b: Step-up2 = clock/16,
292 * VC2-0 = 010b: VciOUT = 0.87 * VciLVL */
293 lcd_write_reg(R_POWER_CONTROL2, 0x0002);
295 /* VRH3-0 = 1000b: Vreg1OUT = REGP * 1.90 */
296 lcd_write_reg(R_POWER_CONTROL3, 0x0008);
298 lcd_delay(40);
300 lcd_write_reg(R_POWER_CONTROL4, 0x0000); /* VCOMG = 0 */
302 /* This register is unknown */
303 lcd_write_reg(0x56, 0x80f);
306 lcd_write_reg(R_POWER_CONTROL1, 0x4140);
308 lcd_delay(10);
310 lcd_write_reg(R_POWER_CONTROL2, 0x0000);
311 lcd_write_reg(R_POWER_CONTROL3, 0x0013);
313 lcd_delay(20);
315 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
317 lcd_delay(20);
319 lcd_write_reg(R_POWER_CONTROL4, 0x6d0e);
321 lcd_write_reg(R_GAMMA_FINE_ADJ_POS1, 0x0002);
322 lcd_write_reg(R_GAMMA_FINE_ADJ_POS2, 0x0707);
323 lcd_write_reg(R_GAMMA_FINE_ADJ_POS3, 0x0182);
324 lcd_write_reg(R_GAMMA_GRAD_ADJ_POS, 0x0203);
325 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG1, 0x0706);
326 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG2, 0x0006);
327 lcd_write_reg(R_GAMMA_FINE_ADJ_NEG3, 0x0706);
328 lcd_write_reg(R_GAMMA_GRAD_ADJ_NEG, 0x0000);
329 lcd_write_reg(R_GAMMA_AMP_ADJ_RES_POS, 0x030f);
330 lcd_write_reg(R_GAMMA_AMP_AVG_ADJ_RES_NEG, 0x0f08);
333 lcd_write_reg(R_GATE_SCAN_POS, 0);
334 lcd_write_reg(R_VERT_SCROLL_CONTROL, 0);
336 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
337 lcd_write_reg(R_1ST_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
338 lcd_write_reg(R_2ND_SCR_DRV_POS, (LCD_HEIGHT-1) << 8);
340 lcd_write_reg(R_DISP_CONTROL1, 0x0033 | r_disp_control_rev);
342 display_on=true; /* must be done before calling lcd_update() */
343 lcd_update();
346 /* LCD init */
347 void lcd_init_device(void)
349 ams3525_dbop_init();
351 /* Init GPIOs the same as the OF */
353 GPIOA_DIR |= (1<<5);
354 GPIOA_PIN(5) = 0;
356 GPIOA_PIN(4) = 0; /*c80b0040 := 0;*/
358 lcd_delay(1);
360 GPIOA_PIN(5) = (1<<5);
362 lcd_delay(1);
364 _display_on();
367 #if defined(HAVE_LCD_ENABLE)
368 void lcd_enable(bool on)
370 if(display_on!=on)
372 if(on)
374 _display_on();
375 send_event(LCD_EVENT_ACTIVATION, NULL);
377 else
379 display_on=false;
380 lcd_write_reg(R_POWER_CONTROL1, 0x0001);
384 #endif
386 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
387 bool lcd_active(void)
389 return display_on;
392 #endif
394 /*** update functions ***/
396 static unsigned lcd_yuv_options = 0;
398 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
399 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
400 int width,
401 int stride);
402 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src[3],
403 int width,
404 int stride,
405 int x_screen, /* To align dither pattern */
406 int y_screen);
408 void lcd_yuv_set_options(unsigned options)
410 lcd_yuv_options = options;
413 static void lcd_window_blit(int xmin, int ymin, int xmax, int ymax)
415 if (!display_flipped)
417 lcd_write_reg(R_HORIZ_RAM_ADDR_POS,
418 ((LCD_WIDTH-1 - xmin) << 8) | (LCD_WIDTH-1 - xmax));
419 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
420 lcd_write_reg(R_RAM_ADDR_SET,
421 (ymin << 8) | (LCD_WIDTH-1 - xmin));
423 else
425 lcd_write_reg(R_HORIZ_RAM_ADDR_POS, (xmax << 8) | xmin);
426 lcd_write_reg(R_VERT_RAM_ADDR_POS, (ymax << 8) | ymin);
427 lcd_write_reg(R_RAM_ADDR_SET, (ymax << 8) | xmin);
431 /* Performance function to blit a YUV bitmap directly to the LCD
432 * src_x, src_y, width and height should be even
433 * x, y, width and height have to be within LCD bounds
435 void lcd_blit_yuv(unsigned char * const src[3],
436 int src_x, int src_y, int stride,
437 int x, int y, int width, int height)
439 unsigned char const * yuv_src[3];
440 off_t z;
442 lcd_busy = true;
444 /* Sorry, but width and height must be >= 2 or else */
445 width &= ~1;
446 height >>= 1;
448 z = stride*src_y;
449 yuv_src[0] = src[0] + z + src_x;
450 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
451 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
453 if (!display_flipped)
455 lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_VIDEO_NORMAL);
457 else
459 lcd_write_reg(R_ENTRY_MODE, R_ENTRY_MODE_VIDEO_FLIPPED);
462 if (lcd_yuv_options & LCD_YUV_DITHER)
466 lcd_window_blit(y, x, y+1, x+width-1);
468 /* Start write to GRAM */
469 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
471 lcd_write_yuv420_lines_odither(yuv_src, width, stride, x, y);
472 yuv_src[0] += stride << 1; /* Skip down two luma lines */
473 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
474 yuv_src[2] += stride >> 1;
475 y+=2;
477 while (--height > 0);
479 else
483 lcd_window_blit(y, x, y+1, x+width-1);
485 /* Start write to GRAM */
486 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
488 lcd_write_yuv420_lines(yuv_src, width, stride);
489 yuv_src[0] += stride << 1; /* Skip down two luma lines */
490 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
491 yuv_src[2] += stride >> 1;
492 y+=2;
494 while (--height > 0);
497 lcd_busy = false;
500 /* Update the display.
501 This must be called after all other LCD functions that change the display. */
502 void lcd_update(void)
504 if (!display_on)
505 return;
507 lcd_busy = true;
509 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
511 /* Set start position and window */
512 lcd_window(0, 0, LCD_WIDTH-1, LCD_HEIGHT-1);
514 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
516 lcd_write_data((fb_data*)lcd_framebuffer, LCD_WIDTH*LCD_HEIGHT);
518 lcd_busy = false;
519 } /* lcd_update */
522 /* Update a fraction of the display. */
523 void lcd_update_rect(int x, int y, int width, int height)
525 const fb_data *ptr;
526 int ymax, xmax;
529 if (!display_on)
530 return;
532 xmax = x + width;
533 if (xmax >= LCD_WIDTH)
534 xmax = LCD_WIDTH - 1; /* Clip right */
535 if (x < 0)
536 x = 0; /* Clip left */
537 if (x >= xmax)
538 return; /* nothing left to do */
540 width = xmax - x + 1; /* Fix width */
542 ymax = y + height;
543 if (ymax >= LCD_HEIGHT)
544 ymax = LCD_HEIGHT - 1; /* Clip bottom */
545 if (y < 0)
546 y = 0; /* Clip top */
547 if (y >= ymax)
548 return; /* nothing left to do */
550 lcd_busy = true;
552 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
554 lcd_window(x, y, xmax, ymax);
555 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
557 ptr = (fb_data*)&lcd_framebuffer[y][x];
560 height = ymax - y; /* fix height */
564 lcd_write_data(ptr, width);
565 ptr += LCD_WIDTH;
567 while (--height >= 0);
569 lcd_busy = false;
570 } /* lcd_update_rect */
572 /* writes one red pixel outside the visible area, needed for correct
573 * dbop reads */
574 bool lcd_button_support(void)
576 fb_data data = (0xf<<12);
578 if (lcd_busy)
579 return false;
581 lcd_write_reg(R_ENTRY_MODE, r_entry_mode);
582 /* Set start position and window */
583 lcd_window(LCD_WIDTH+1, LCD_HEIGHT+1, LCD_WIDTH+2, LCD_HEIGHT+2);
585 lcd_write_cmd(R_WRITE_DATA_2_GRAM);
587 lcd_write_single_data16(data);
588 return true;