summaryrefslogtreecommitdiff
path: root/firmware/target/arm/s3c2440/lcd-s3c2440.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/s3c2440/lcd-s3c2440.c')
-rw-r--r--firmware/target/arm/s3c2440/lcd-s3c2440.c592
1 files changed, 592 insertions, 0 deletions
diff --git a/firmware/target/arm/s3c2440/lcd-s3c2440.c b/firmware/target/arm/s3c2440/lcd-s3c2440.c
new file mode 100644
index 0000000000..b9f76b7400
--- /dev/null
+++ b/firmware/target/arm/s3c2440/lcd-s3c2440.c
@@ -0,0 +1,592 @@
1/***************************************************************************
2* __________ __ ___.
3* Open \______ \ ____ ____ | | _\_ |__ _______ ___
4* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7* \/ \/ \/ \/ \/
8* $Id$
9*
10* Copyright (C) 2007 by Greg White
11* Copyright (C) 2009 by Bob Cousins
12*
13* This program is free software; you can redistribute it and/or
14* modify it under the terms of the GNU General Public License
15* as published by the Free Software Foundation; either version 2
16* of the License, or (at your option) any later version.
17*
18* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19* KIND, either express or implied.
20*
21****************************************************************************/
22#include <sys/types.h>
23
24#include "config.h"
25#include "system.h"
26#include "cpu.h"
27#include "string.h"
28#include "lcd.h"
29#include "kernel.h"
30#include "lcd-target.h"
31
32#define LCDADDR(x, y) (&lcd_framebuffer[(y)][(x)])
33
34static bool lcd_on = true;
35#if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
36static bool lcd_powered = true;
37#endif
38static unsigned lcd_yuv_options = 0;
39/*
40** This is imported from lcd-16bit.c
41*/
42extern struct viewport* current_vp;
43
44/* Copies a rectangle from one framebuffer to another. Can be used in
45 single transfer mode with width = num pixels, and height = 1 which
46 allows a full-width rectangle to be copied more efficiently. */
47extern void lcd_copy_buffer_rect(fb_data *dst, const fb_data *src,
48 int width, int height);
49
50#if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
51bool lcd_active(void)
52{
53 return lcd_on;
54}
55#endif
56
57static unsigned int LCDBANK(unsigned int address)
58{
59 return ((address >> 22) & 0xff);
60}
61
62static unsigned int LCDBASEU(unsigned int address)
63{
64 return (address & ((1 << 22)-1)) >> 1;
65}
66
67static unsigned int LCDBASEL(unsigned int address)
68{
69 address += 320*240*2;
70 return (address & ((1 << 22)-1)) >> 1;
71}
72
73static inline void delay_cycles(volatile int delay)
74{
75 while(delay>0) delay--;
76}
77
78static void LCD_CTRL_setup(void)
79{
80 LCDCON1 = (LCD_CLKVAL << 8) | (LCD_MMODE << 7) | (LCD_PNRMODE << 5) |
81 (LCD_BPPMODE << 1);
82 LCDCON2 = (LCD_UPPER_MARGIN << 24) | ((LCD_HEIGHT - 1) << 14) |
83 (LCD_LOWER_MARGIN << 6) | (LCD_VSYNC_LEN << 0);
84 LCDCON3 = (LCD_LEFT_MARGIN << 19) | ((LCD_WIDTH - 1) << 8) |
85 (LCD_RIGHT_MARGIN << 0);
86 LCDCON4 = (LCD_HSYNC_LEN << 0);
87
88 /* HWSWP = 1, INVVFRAM = 1, INVVLINE = 1, FRM565 = 1, All others = 0 */
89 LCDCON5 = 0xB01;
90
91 LCDSADDR1 = (LCDBANK((unsigned)FRAME) << 21) | (LCDBASEU((unsigned)FRAME));
92 LCDSADDR2 = LCDBASEL((unsigned)FRAME);
93 LCDSADDR3 = 0x000000F0;
94}
95
96static void LCD_CTRL_clock(bool onoff)
97{
98 if(onoff)
99 {
100 GPCCON &=~0xFFF000FC;
101 GPCCON |= 0xAAA000A8;
102 GPCUP |= 0xFC0E;
103
104 GPDCON &=~0xFFF0FFF0;
105 GPDCON |= 0xAAA0AAA0;
106 GPDUP |= 0xFCFC;
107
108 s3c_regset32(&CLKCON, 0x20); /* enable LCD clock */
109 LCDCON1 |= LCD_ENVID;
110 }
111 else
112 {
113 GPCCON &= ~0xFFF000FC;
114 GPCUP &= ~0xFC0E;
115
116 GPDCON &= ~0xFFF0FFF0;
117 GPDUP &= ~0xFCFC;
118
119 LCDCON1 &= ~LCD_ENVID; /* Must disable first or bus may freeze */
120 s3c_regclr32(&CLKCON, 0x20); /* disable LCD clock */
121 }
122}
123
124#ifdef GIGABEAT_F
125static void reset_LCD(bool reset)
126{
127 GPBCON&=~0xC000;
128 GPBCON|=0x4000;
129 if(reset)
130 GPBDAT|=0x80;
131 else
132 GPBDAT&=~0x80;
133}
134#endif
135
136
137/****************************************************************************/
138#ifdef GIGABEAT_F
139static void LCD_SPI_send(const unsigned char *array, int count)
140{
141 while (count--)
142 {
143 while ((SPSTA0&0x01)==0){};
144 SPTDAT0=*array++;
145 }
146}
147
148static void LCD_SPI_setreg(unsigned char reg, unsigned char value)
149{
150 unsigned char regval[] =
151 {
152 0x00,reg,0x01,value
153 };
154 LCD_SPI_send(regval, sizeof(regval));
155}
156
157static void LCD_SPI_SS(bool select)
158{
159 delay_cycles(0x4FFF);
160
161 GPBCON&=~0x30000;
162 GPBCON|=0x10000;
163
164 if(select)
165 GPBDAT|=0x100;
166 else
167 GPBDAT&=~0x100;
168}
169
170static void LCD_SPI_start(void)
171{
172 s3c_regset32(&CLKCON, 0x40000); /* enable SPI clock */
173 LCD_SPI_SS(false);
174 SPCON0=0x3E;
175 SPPRE0=24;
176
177 reset_LCD(true);
178 LCD_SPI_SS(true);
179}
180
181static void LCD_SPI_stop(void)
182{
183 LCD_SPI_SS(false);
184
185 SPCON0 &= ~0x10;
186 s3c_regclr32(&CLKCON, 0x40000); /* disable SPI clock */
187}
188
189static void LCD_SPI_init(void)
190{
191 /*
192 * SPI setup - Some of these registers are known; they are documented in
193 * the wiki. Many thanks to Alex Gerchanovsky for discovering this
194 * sequence.
195 */
196
197 LCD_CTRL_clock(true);
198
199 LCD_SPI_start();
200 LCD_SPI_setreg(0x0F, 0x01);
201 LCD_SPI_setreg(0x09, 0x06);
202 LCD_SPI_setreg(0x16, 0xA6);
203 LCD_SPI_setreg(0x1E, 0x49);
204 LCD_SPI_setreg(0x1F, 0x26);
205 LCD_SPI_setreg(0x0B, 0x2F);
206 LCD_SPI_setreg(0x0C, 0x2B);
207 LCD_SPI_setreg(0x19, 0x5E);
208 LCD_SPI_setreg(0x1A, 0x15);
209 LCD_SPI_setreg(0x1B, 0x15);
210 LCD_SPI_setreg(0x1D, 0x01);
211 LCD_SPI_setreg(0x00, 0x03);
212 LCD_SPI_setreg(0x01, 0x10);
213 LCD_SPI_setreg(0x02, 0x0A);
214 LCD_SPI_setreg(0x06, 0x04); /* Set the orientation */
215 LCD_SPI_setreg(0x08, 0x2E);
216 LCD_SPI_setreg(0x24, 0x12);
217 LCD_SPI_setreg(0x25, 0x3F);
218 LCD_SPI_setreg(0x26, 0x0B);
219 LCD_SPI_setreg(0x27, 0x00);
220 LCD_SPI_setreg(0x28, 0x00);
221 LCD_SPI_setreg(0x29, 0xF6);
222 LCD_SPI_setreg(0x2A, 0x03);
223 LCD_SPI_setreg(0x2B, 0x0A);
224 LCD_SPI_setreg(0x04, 0x01); /* Turn the display on */
225 LCD_SPI_stop();
226}
227#endif
228/****************************************************************************/
229
230/* LCD init */
231void lcd_init_device(void)
232{
233#ifdef BOOTLOADER
234 int i;
235 /* When the Rockbox bootloader starts the framebuffer address is changed
236 * but the LCD display should stay the same til an lcd_update() occurs.
237 * This copies the data from the old framebuffer to the new one to make the
238 * change non-visable to the user.
239 */
240 unsigned short *buf = (unsigned short*)(FRAME);
241 unsigned short *oldbuf = (unsigned short*)(LCDSADDR1<<1);
242
243 /* The Rockbox bootloader is transitioning from RGB555I to RGB565 mode
244 so convert the frambuffer data accordingly */
245 for(i=0; i< 320*240; i++)
246 {
247 *(buf++) = ((*oldbuf>>1) & 0x1F) | (*oldbuf & 0xffc0);
248 oldbuf++;
249 }
250#endif
251
252 /* Set pins up */
253 GPHUP &= 0x600;
254 GPECON |= 0x0A800000;
255 GPEUP |= 0x3800;
256#ifdef GIGABEAT_F
257 GPBUP |= 0x181;
258#endif
259
260 s3c_regset32(&CLKCON, 0x20); /* enable LCD clock */
261
262 LCD_CTRL_setup();
263#ifdef GIGABEAT_F
264 LCD_SPI_init();
265#else
266 LCD_CTRL_clock(true);
267#endif
268}
269
270#if defined(HAVE_LCD_SLEEP)
271static void LCD_SPI_powerdown(void)
272{
273 lcd_powered = false;
274
275 LCD_SPI_start();
276 LCD_SPI_setreg(0x04, 0x00);
277 LCD_SPI_stop();
278
279 reset_LCD(false); /* This makes a big difference on power */
280 LCD_CTRL_clock(false);
281}
282
283void lcd_sleep(void)
284{
285 if (lcd_powered)
286 {
287 /* "not powered" implies "disabled" */
288 if (lcd_on)
289 lcd_enable(false);
290
291 LCD_SPI_powerdown();
292 }
293}
294#endif
295
296#if defined(HAVE_LCD_ENABLE)
297static void LCD_SPI_powerup(void)
298{
299 LCD_CTRL_clock(true);
300
301 LCD_SPI_start();
302 LCD_SPI_setreg(0x04, 0x01);
303 LCD_SPI_stop();
304
305 lcd_powered = true;
306}
307
308void lcd_enable(bool state)
309{
310 if (state == lcd_on)
311 return;
312
313 if(state)
314 {
315 /* "enabled" implies "powered" */
316 if (!lcd_powered)
317 {
318 LCD_SPI_powerup();
319 /* Wait long enough for a frame to be written - yes, it
320 * takes awhile. */
321 sleep(HZ/5);
322 }
323
324 lcd_on = true;
325 lcd_update();
326 lcd_activation_call_hook();
327 }
328 else
329 {
330 lcd_on = false;
331 }
332}
333#endif
334
335#ifdef GIGABEAT_F
336void lcd_set_flip(bool yesno) {
337 if (!lcd_on)
338 return;
339
340 LCD_SPI_start();
341 if(yesno)
342 {
343 LCD_SPI_setreg(0x06, 0x02);
344 }
345 else
346 {
347 LCD_SPI_setreg(0x06, 0x04);
348 }
349 LCD_SPI_stop();
350}
351
352int lcd_default_contrast(void)
353{
354 return DEFAULT_CONTRAST_SETTING;
355}
356
357void lcd_set_contrast(int val) {
358 if (!lcd_on)
359 return;
360
361 LCD_SPI_start();
362 LCD_SPI_setreg(0x0B, (unsigned char) val);
363 LCD_SPI_stop();
364}
365
366void lcd_set_invert_display(bool yesno) {
367 if (!lcd_on)
368 return;
369
370 LCD_SPI_start();
371 if(yesno)
372 {
373 LCD_SPI_setreg(0x27, 0x10);
374 }
375 else
376 {
377 LCD_SPI_setreg(0x27, 0x00);
378 }
379 LCD_SPI_stop();
380}
381#else
382void lcd_set_flip(bool yesno)
383{
384 (void)yesno;
385 /* Not implemented */
386}
387
388int lcd_default_contrast(void)
389{
390 return DEFAULT_CONTRAST_SETTING;
391}
392
393void lcd_set_contrast(int val)
394{
395 (void)val;
396 /* Not implemented */
397}
398
399void lcd_set_invert_display(bool yesno)
400{
401 (void)yesno;
402 /* Not implemented */
403}
404
405#endif
406
407/* Update a fraction of the display. */
408void lcd_update_rect(int x, int y, int width, int height)
409{
410 fb_data *dst, *src;
411
412 if (!lcd_on)
413 return;
414
415 if (x + width > LCD_WIDTH)
416 width = LCD_WIDTH - x; /* Clip right */
417 if (x < 0)
418 width += x, x = 0; /* Clip left */
419 if (width <= 0)
420 return; /* nothing left to do */
421
422 if (y + height > LCD_HEIGHT)
423 height = LCD_HEIGHT - y; /* Clip bottom */
424 if (y < 0)
425 height += y, y = 0; /* Clip top */
426 if (height <= 0)
427 return; /* nothing left to do */
428
429 /* TODO: It may be faster to swap the addresses of lcd_driver_framebuffer
430 * and lcd_framebuffer */
431 dst = (fb_data *)FRAME + LCD_WIDTH*y + x;
432 src = &lcd_framebuffer[y][x];
433
434 /* Copy part of the Rockbox framebuffer to the second framebuffer */
435 if (width < LCD_WIDTH)
436 {
437 /* Not full width - do line-by-line */
438 lcd_copy_buffer_rect(dst, src, width, height);
439 }
440 else
441 {
442 /* Full width - copy as one line */
443 lcd_copy_buffer_rect(dst, src, LCD_WIDTH*height, 1);
444 }
445}
446
447/* Update the display.
448 This must be called after all other LCD functions that change the display. */
449void lcd_update(void)
450{
451 if (!lcd_on)
452 return;
453
454 lcd_copy_buffer_rect((fb_data *)FRAME, &lcd_framebuffer[0][0],
455 LCD_WIDTH*LCD_HEIGHT, 1);
456}
457
458#if defined(TOSHIBA_GIGABEAT_F) || defined(TOSHIBA_GIGABEAT_S)
459void lcd_bitmap_transparent_part(const fb_data *src, int src_x, int src_y,
460 int stride, int x, int y, int width,
461 int height)
462{
463 int w, px;
464 fb_data *dst;
465
466 if (x + width > current_vp->width)
467 width = current_vp->width - x; /* Clip right */
468
469 if (x < 0) /* Clip left */
470 {
471 width += x;
472 src_x -= x;
473 x = 0;
474 }
475
476 if (width <= 0)
477 return; /* nothing left to do */
478
479 if (y + height > current_vp->height)
480 height = current_vp->height - y; /* Clip bottom */
481
482 if (y < 0) /* Clip top */
483 {
484 height += y;
485 src_y -= y;
486 y = 0;
487 }
488
489 if (height <= 0)
490 return; /* nothing left to do */
491
492 src += stride * src_y + src_x; /* move starting point */
493 dst = &lcd_framebuffer[current_vp->y+y][current_vp->x+x];
494
495 asm volatile (
496 ".rowstart: \r\n"
497 "mov %[w], %[width] \r\n" /* Load width for inner loop */
498 ".nextpixel: \r\n"
499 "ldrh %[px], [%[s]], #2 \r\n" /* Load src pixel */
500 "add %[d], %[d], #2 \r\n" /* Uncoditionally increment dst */
501 "cmp %[px], %[fgcolor] \r\n" /* Compare to foreground color */
502 "streqh %[fgpat], [%[d], #-2] \r\n" /* Store foregroud if match */
503 "cmpne %[px], %[transcolor] \r\n" /* Compare to transparent color */
504 "strneh %[px], [%[d], #-2] \r\n" /* Store dst if not transparent */
505 "subs %[w], %[w], #1 \r\n" /* Width counter has run down? */
506 "bgt .nextpixel \r\n" /* More in this row? */
507 "add %[s], %[s], %[sstp], lsl #1 \r\n" /* Skip over to start of next line */
508 "add %[d], %[d], %[dstp], lsl #1 \r\n"
509 "subs %[h], %[h], #1 \r\n" /* Height counter has run down? */
510 "bgt .rowstart \r\n" /* More rows? */
511 : [w]"=&r"(w), [h]"+&r"(height), [px]"=&r"(px),
512 [s]"+&r"(src), [d]"+&r"(dst)
513 : [width]"r"(width),
514 [sstp]"r"(stride - width),
515 [dstp]"r"(LCD_WIDTH - width),
516 [transcolor]"r"(TRANSPARENT_COLOR),
517 [fgcolor]"r"(REPLACEWITHFG_COLOR),
518 [fgpat]"r"(current_vp->fg_pattern)
519 );
520}
521#endif
522
523void lcd_yuv_set_options(unsigned options)
524{
525 lcd_yuv_options = options;
526}
527
528/* Line write helper function for lcd_yuv_blit. Write two lines of yuv420. */
529extern void lcd_write_yuv420_lines(fb_data *dst,
530 unsigned char const * const src[3],
531 int width,
532 int stride);
533extern void lcd_write_yuv420_lines_odither(fb_data *dst,
534 unsigned char const * const src[3],
535 int width,
536 int stride,
537 int x_screen, /* To align dither pattern */
538 int y_screen);
539/* Performance function to blit a YUV bitmap directly to the LCD */
540/* For the Gigabeat - show it rotated */
541/* So the LCD_WIDTH is now the height */
542void lcd_blit_yuv(unsigned char * const src[3],
543 int src_x, int src_y, int stride,
544 int x, int y, int width, int height)
545{
546 /* Caches for chroma data so it only need be recaculated every other
547 line */
548 unsigned char const * yuv_src[3];
549 off_t z;
550
551 if (!lcd_on)
552 return;
553
554 /* Sorry, but width and height must be >= 2 or else */
555 width &= ~1;
556 height >>= 1;
557
558 y = LCD_WIDTH - 1 - y;
559 fb_data *dst = (fb_data*)FRAME + x * LCD_WIDTH + y;
560
561 z = stride*src_y;
562 yuv_src[0] = src[0] + z + src_x;
563 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
564 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
565
566 if (lcd_yuv_options & LCD_YUV_DITHER)
567 {
568 do
569 {
570 lcd_write_yuv420_lines_odither(dst, yuv_src, width, stride, y, x);
571 yuv_src[0] += stride << 1; /* Skip down two luma lines */
572 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
573 yuv_src[2] += stride >> 1;
574 dst -= 2;
575 y -= 2;
576 }
577 while (--height > 0);
578 }
579 else
580 {
581 do
582 {
583 lcd_write_yuv420_lines(dst, yuv_src, width, stride);
584 yuv_src[0] += stride << 1; /* Skip down two luma lines */
585 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
586 yuv_src[2] += stride >> 1;
587 dst -= 2;
588 }
589 while (--height > 0);
590 }
591}
592