Also fix Debug->View partitions when SECTOR_SIZE!=512
[kugel-rb.git] / firmware / target / arm / lcd-c200_c200v2.c
blob62f1dc20f6d1fc6109cc801f00af9f0de8a34dfb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Mark Arigo
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 <sys/types.h>
23 #include "config.h"
24 #include "cpu.h"
25 #include "lcd.h"
26 #include "kernel.h"
27 #include "system.h"
29 #ifdef SANSA_C200V2
30 /* button driver needs to know if a lcd operation is in progress */
31 static bool lcd_busy = false;
32 static unsigned short dbop_input = 0xFFFF;
33 #endif
35 /* Display status */
36 #if MEMORYSIZE > 2
37 static unsigned lcd_yuv_options SHAREDBSS_ATTR = 0;
38 #endif
39 static bool is_lcd_enabled = true;
41 /* LCD command set for Samsung S6B33B2 */
43 #define R_NOP 0x00
44 #define R_OSCILLATION_MODE 0x02
45 #define R_DRIVER_OUTPUT_MODE 0x10
46 #define R_DCDC_SET 0x20
47 #define R_BIAS_SET 0x22
48 #define R_DCDC_CLOCK_DIV 0x24
49 #define R_DCDC_AMP_ONOFF 0x26
50 #define R_TEMP_COMPENSATION 0x28
51 #define R_CONTRAST_CONTROL1 0x2a
52 #define R_CONTRAST_CONTROL2 0x2b
53 #define R_STANDBY_OFF 0x2c
54 #define R_STANDBY_ON 0x2d
55 #define R_DDRAM_BURST_OFF 0x2e
56 #define R_DDRAM_BURST_ON 0x2f
57 #define R_ADDRESSING_MODE 0x30
58 #define R_ROW_VECTOR_MODE 0x32
59 #define R_N_LINE_INVERSION 0x34
60 #define R_FRAME_FREQ_CONTROL 0x36
61 #define R_RED_PALETTE 0x38
62 #define R_GREEN_PALETTE 0x3a
63 #define R_BLUE_PALETTE 0x3c
64 #define R_ENTRY_MODE 0x40
65 #define R_X_ADDR_AREA 0x42
66 #define R_Y_ADDR_AREA 0x43
67 #define R_RAM_SKIP_AREA 0x45
68 #define R_DISPLAY_OFF 0x50
69 #define R_DISPLAY_ON 0x51
70 #define R_SPEC_DISPLAY_PATTERN 0x53
71 #define R_PARTIAL_DISPLAY_MODE 0x55
72 #define R_PARTIAL_START_LINE 0x56
73 #define R_PARTIAL_END_LINE 0x57
74 #define R_AREA_SCROLL_MODE 0x59
75 #define R_SCROLL_START_LINE 0x5a
76 #define R_DATA_FORMAT_SELECT 0x60
78 #if defined(SANSA_C200)
79 /* wait for LCD */
80 static inline void lcd_wait_write(void)
82 while (LCD1_CONTROL & LCD1_BUSY_MASK);
85 /* send LCD data */
86 static void lcd_send_pixel(const fb_data data)
88 lcd_wait_write();
89 LCD1_DATA = data >> 8;
90 lcd_wait_write();
91 LCD1_DATA = data & 0xff;
94 inline void lcd_write_data(const fb_data *data, int width)
96 do {
97 lcd_send_pixel(*data++);
98 } while(--width);
101 /* send LCD command */
102 static void lcd_send_command(unsigned char cmd, unsigned char arg)
104 lcd_wait_write();
105 LCD1_CMD = cmd;
106 /* if the argument is 0, we send a NOP (= 0) command */
107 lcd_wait_write();
108 LCD1_CMD = arg;
111 static inline void c200v1_lcd_init(void)
113 /* This is from the c200 of bootloader beginning at offset 0xbbf4 */
114 outl(inl(0x70000010) & ~0xfc000000, 0x70000010);
115 outl(inl(0x70000010), 0x70000010);
117 DEV_INIT2 &= ~0x400;
118 udelay(10000);
120 LCD1_CONTROL &= ~0x4;
121 udelay(15);
123 LCD1_CONTROL |= 0x4;
124 udelay(10);
126 LCD1_CONTROL = 0x0084; /* bits (9,10) = 00 -> fastest setting */
127 udelay(10000);
130 #define lcd_delay(delay) udelay((delay) * 1000)
132 #elif defined(SANSA_C200V2)
134 static inline void lcd_delay(int delay)
135 { //TUNEME : delay is in milliseconds
136 delay <<= 14;
137 while(delay--) ;
140 /* send LCD data */
141 void lcd_write_data(const fb_data *data, int width)
143 do {
144 DBOP_DOUT = *data << 8 | *data >> 8;
145 data++;
147 /* Wait if push fifo is full */
148 while ((DBOP_STAT & (1<<6)) != 0);
149 } while(--width);
151 /* While push fifo is not empty */
152 while ((DBOP_STAT & (1<<10)) == 0);
155 /* send LCD command */
156 static void lcd_send_command(unsigned char cmd, unsigned char val)
158 DBOP_TIMPOL_23 = 0xa167006e;
160 DBOP_DOUT = cmd | val << 8;
162 while ((DBOP_STAT & (1<<10)) == 0);
164 DBOP_TIMPOL_23 = 0xa167e06f;
167 static inline void as3525_dbop_init(void)
169 CGU_DBOP = (1<<3) | AS3525_DBOP_DIV;
171 DBOP_TIMPOL_01 = 0xe167e167;
172 DBOP_TIMPOL_23 = 0xe167006e;
173 DBOP_CTRL = 0x40008;
175 GPIOB_AFSEL = 0xc;
176 GPIOC_AFSEL = 0xff;
178 DBOP_TIMPOL_23 = 0x6006e;
179 DBOP_CTRL = 0x52008;
180 DBOP_TIMPOL_01 = 0x6e167;
181 DBOP_TIMPOL_23 = 0xa167e06f;
183 lcd_delay(20);
186 static unsigned short lcd_dbop_read(void)
188 unsigned int dbop_ctrl_old = DBOP_CTRL;
189 unsigned int dbop_timpol23_old = DBOP_TIMPOL_23;
190 unsigned int value;
192 /* make sure that the DBOP FIFO is empty */
193 while ((DBOP_STAT & (1<<10)) == 0);
195 /* write DBOP_DOUT to pre-charge DBOP data lines with a high level */
196 DBOP_TIMPOL_23 = 0xe167e167; /* no strobe towards lcd */
197 DBOP_CTRL = (1 << 16) | /* enw=1 (enable write) */
198 (1 << 12); /* ow=1 (16-bit data width) */
199 DBOP_DOUT = 0xFFFF; /* all pins high */
200 while ((DBOP_STAT & (1<<10)) == 0);
202 /* perform a DBOP read */
203 DBOP_CTRL = (1 << 15) | /* strd=1 (start read) */
204 (1 << 12) | /* ow=1 (16-bit data width) */
205 (31 << 0); /* rs_t=31 (read DBOP at end of cycle) */
206 while ((DBOP_STAT & (1<<16)) == 0);
207 value = DBOP_DIN;
209 /* restore previous values */
210 DBOP_TIMPOL_23 = dbop_timpol23_old;
211 DBOP_CTRL = dbop_ctrl_old;
213 return value;
216 /* get the DBOP input value, either directly or cached if DBOP is busy */
217 unsigned short int lcd_dbop_input(void)
219 if (!lcd_busy) {
220 dbop_input = lcd_dbop_read();
222 return dbop_input;
225 #endif
227 /* LCD init */
228 void lcd_init_device(void)
230 #if defined(SANSA_C200)
231 c200v1_lcd_init();
232 #elif defined(SANSA_C200V2)
233 as3525_dbop_init();
234 #endif
236 lcd_send_command(R_STANDBY_OFF, 0);
237 lcd_delay(20);
239 lcd_send_command(R_OSCILLATION_MODE, 0x01);
240 lcd_delay(20);
242 lcd_send_command(R_DCDC_AMP_ONOFF, 0x01);
243 lcd_delay(20);
245 lcd_send_command(R_DCDC_AMP_ONOFF, 0x09);
246 lcd_delay(20);
248 lcd_send_command(R_DCDC_AMP_ONOFF, 0x0b);
249 lcd_delay(20);
251 lcd_send_command(R_DCDC_AMP_ONOFF, 0x0f);
252 lcd_delay(20);
254 lcd_send_command(R_DRIVER_OUTPUT_MODE, 0x07);
256 lcd_send_command(R_DCDC_SET, 0x03);
258 lcd_send_command(R_DCDC_CLOCK_DIV, 0x03);
260 lcd_send_command(R_TEMP_COMPENSATION, 0x01);
262 lcd_send_command(R_CONTRAST_CONTROL1, 0x55);
264 lcd_send_command(R_ADDRESSING_MODE, 0x10);
266 lcd_send_command(R_ROW_VECTOR_MODE, 0x0e);
268 lcd_send_command(R_N_LINE_INVERSION, 0x0d);
270 lcd_send_command(R_FRAME_FREQ_CONTROL, 0);
272 lcd_send_command(R_ENTRY_MODE, 0x82);
274 /* vertical dimensions */
275 lcd_send_command(R_Y_ADDR_AREA, 0x1a); /* y1 + 0x1a */
276 lcd_send_command(LCD_HEIGHT - 1 + 0x1a, 0); /* y2 + 0x1a */
278 /* horizontal dimensions */
279 lcd_send_command(R_X_ADDR_AREA, 0); /* x1 */
280 lcd_send_command(LCD_WIDTH - 1, 0); /* x2 */
282 lcd_delay(100);
284 lcd_send_command(R_DISPLAY_ON, 0);
287 /*** hardware configuration ***/
288 int lcd_default_contrast(void)
290 return DEFAULT_CONTRAST_SETTING;
293 void lcd_set_contrast(int val)
295 #ifdef SANSA_C200V2
296 lcd_busy = true;
297 #endif
298 lcd_send_command(R_CONTRAST_CONTROL1, val);
299 #ifdef SANSA_C200V2
300 lcd_busy = false;
301 #endif
304 void lcd_set_invert_display(bool yesno)
306 /* TODO: Implement lcd_set_invert_display() */
307 (void)yesno;
310 #if defined(HAVE_LCD_ENABLE)
311 void lcd_enable(bool yesno)
313 if (yesno == is_lcd_enabled)
314 return;
316 #ifdef SANSA_C200V2
317 lcd_busy = true;
318 #endif
319 if ((is_lcd_enabled = yesno))
321 lcd_send_command(R_STANDBY_OFF, 0);
322 lcd_send_command(R_DISPLAY_ON, 0);
323 send_event(LCD_EVENT_ACTIVATION, NULL);
325 else
327 lcd_send_command(R_STANDBY_ON, 0);
329 #ifdef SANSA_C200V2
330 lcd_busy = false;
331 #endif
333 #endif
335 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
336 bool lcd_active(void)
338 return is_lcd_enabled;
340 #endif
343 /* turn the display upside down (call lcd_update() afterwards) */
344 void lcd_set_flip(bool yesno)
346 #ifdef SANSA_C200V2
347 lcd_busy = true;
348 #endif
349 lcd_send_command(R_DRIVER_OUTPUT_MODE, yesno ? 0x02 : 0x07);
350 #ifdef SANSA_C200V2
351 lcd_busy = false;
352 #endif
355 /*** update functions ***/
357 #if MEMORYSIZE > 2
358 void lcd_yuv_set_options(unsigned options)
360 lcd_yuv_options = options;
363 /* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
364 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
365 int width,
366 int stride);
367 extern void lcd_write_yuv420_lines_odither(unsigned char const * const src[3],
368 int width,
369 int stride,
370 int x_screen, /* To align dither pattern */
371 int y_screen);
372 /* Performance function to blit a YUV bitmap directly to the LCD */
373 void lcd_blit_yuv(unsigned char * const src[3],
374 int src_x, int src_y, int stride,
375 int x, int y, int width, int height)
377 unsigned char const * yuv_src[3];
378 off_t z;
380 /* Sorry, but width and height must be >= 2 or else */
381 width &= ~1;
382 height >>= 1;
384 y += 0x1a;
386 z = stride*src_y;
387 yuv_src[0] = src[0] + z + src_x;
388 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
389 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
391 lcd_send_command(R_ENTRY_MODE, 0x80);
393 lcd_send_command(R_X_ADDR_AREA, x);
394 lcd_send_command(x + width - 1, 0);
396 if (lcd_yuv_options & LCD_YUV_DITHER)
400 lcd_send_command(R_Y_ADDR_AREA, y);
401 lcd_send_command(y + 1, 0);
403 lcd_write_yuv420_lines_odither(yuv_src, width, stride, x, y);
405 yuv_src[0] += stride << 1; /* Skip down two luma lines */
406 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
407 yuv_src[2] += stride >> 1;
408 y += 2;
410 while (--height > 0);
412 else
416 lcd_send_command(R_Y_ADDR_AREA, y);
417 lcd_send_command(y + 1, 0);
419 lcd_write_yuv420_lines(yuv_src, width, stride);
421 yuv_src[0] += stride << 1; /* Skip down two luma lines */
422 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
423 yuv_src[2] += stride >> 1;
424 y += 2;
426 while (--height > 0);
429 #endif /* MEMORYSIZE > 2 */
431 /* Update the display.
432 This must be called after all other LCD functions that change the display. */
433 void lcd_update(void)
435 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
438 /* Update a fraction of the display. */
439 void lcd_update_rect(int x, int y, int width, int height)
441 const fb_data *addr;
443 if (x + width >= LCD_WIDTH)
444 width = LCD_WIDTH - x;
445 if (y + height >= LCD_HEIGHT)
446 height = LCD_HEIGHT - y;
448 if ((width <= 0) || (height <= 0))
449 return; /* Nothing left to do. */
451 addr = &lcd_framebuffer[y][x];
453 #ifdef SANSA_C200V2
454 lcd_busy = true;
455 /* perform a dbop read before doing a potentially lengthy lcd update */
456 dbop_input = lcd_dbop_read();
457 #endif
459 if (width <= 1) {
460 /* The X end address must be larger than the X start address, so we
461 * switch to vertical mode for single column updates and set the
462 * window width to 2 */
463 lcd_send_command(R_ENTRY_MODE, 0x80);
464 lcd_send_command(R_X_ADDR_AREA, x);
465 lcd_send_command(x + 1, 0);
466 } else {
467 lcd_send_command(R_ENTRY_MODE, 0x82);
468 lcd_send_command(R_X_ADDR_AREA, x);
469 lcd_send_command(x + width - 1, 0);
472 lcd_send_command(R_Y_ADDR_AREA, y + 0x1a);
473 lcd_send_command(y + height - 1 + 0x1a, 0);
475 do {
476 lcd_write_data(addr, width);
477 addr += LCD_WIDTH;
478 } while (--height > 0);
480 #ifdef SANSA_C200V2
481 lcd_busy = false;
482 #endif