Submit FS#9890 by Boris Gjenero. Enabling option for iPod Video to shut down LCD...
[kugel-rb.git] / firmware / target / arm / ipod / video / lcd-video.c
blobd1701ea3d707f05a1646260e7cfa94e1c55ef942
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * LCD driver for iPod Video
12 * Based on code from the ipodlinux project - http://ipodlinux.org/
13 * Adapted for Rockbox in December 2005
15 * Original file: linux/arch/armnommu/mach-ipod/fb.c
17 * Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org)
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
25 * KIND, either express or implied.
27 ****************************************************************************/
28 #include "config.h"
29 #include "cpu.h"
30 #include "lcd.h"
31 #include "kernel.h"
32 #include "system.h"
33 #ifdef HAVE_LCD_SLEEP
34 /* Included only for lcd_awake() prototype */
35 #include "backlight-target.h"
36 #endif
38 /* The BCM bus width is 16 bits. But since the low address bits aren't decoded
39 * by the chip (the 3 BCM address bits are mapped to address bits 16..18 of the
40 * PP5022), writing 32 bits (and even more, using 'stmia') at once works. */
41 #define BCM_DATA (*(volatile unsigned short*)(0x30000000))
42 #define BCM_DATA32 (*(volatile unsigned long *)(0x30000000))
43 #define BCM_WR_ADDR (*(volatile unsigned short*)(0x30010000))
44 #define BCM_WR_ADDR32 (*(volatile unsigned long *)(0x30010000))
45 #define BCM_RD_ADDR (*(volatile unsigned short*)(0x30020000))
46 #define BCM_RD_ADDR32 (*(volatile unsigned long *)(0x30020000))
47 #define BCM_CONTROL (*(volatile unsigned short*)(0x30030000))
49 #define BCM_ALT_DATA (*(volatile unsigned short*)(0x30040000))
50 #define BCM_ALT_DATA32 (*(volatile unsigned long *)(0x30040000))
51 #define BCM_ALT_WR_ADDR (*(volatile unsigned short*)(0x30050000))
52 #define BCM_ALT_WR_ADDR32 (*(volatile unsigned long *)(0x30050000))
53 #define BCM_ALT_RD_ADDR (*(volatile unsigned short*)(0x30060000))
54 #define BCM_ALT_RD_ADDR32 (*(volatile unsigned long *)(0x30060000))
55 #define BCM_ALT_CONTROL (*(volatile unsigned short*)(0x30070000))
57 /* Time until the BCM is considered stalled and will be re-kicked.
58 * Must be guaranteed to be >~ 20ms. */
59 #define BCM_UPDATE_TIMEOUT (HZ/20)
61 /* Addresses within BCM */
62 #define BCMA_SRAM_BASE 0
63 #define BCMA_COMMAND 0x1F8
64 #define BCMA_STATUS 0x1FC
65 #define BCMA_CMDPARAM 0xE0000 /* Parameters/data for commands */
66 #define BCMA_SDRAM_BASE 0xC0000000
67 #define BCMA_TV_FB 0xC0000000 /* TV out framebuffer */
68 #define BCMA_TV_BMPDATA 0xC0200000 /* BMP data for TV out functions */
70 /* BCM commands. Write them to BCMA_COMMAND. Note BCM_CMD encoding. */
71 #define BCM_CMD(x) ((~((unsigned long)x) << 16) | ((unsigned long)x))
72 #define BCMCMD_LCD_UPDATE BCM_CMD(0)
73 /* Execute "M25 Diagnostics". Status displayed on LCD. Takes <40s */
74 #define BCMCMD_SELFTEST BCM_CMD(1)
75 #define BCMCMD_TV_PALBMP BCM_CMD(2)
76 #define BCMCMD_TV_NTSCBMP BCM_CMD(3)
77 /* BCM_CMD(4) may be another TV-related command */
78 /* The following might do more depending on word at 0xE00000 */
79 #define BCMCMD_LCD_UPDATERECT BCM_CMD(5)
80 #define BCMCMD_LCD_SLEEP BCM_CMD(8)
81 /* BCM_CMD(12) involved in shutdown */
82 /* Macrovision analog copy prevention is on by default on TV output.
83 Execute this command after enabling TV out to turn it off.
85 #define BCMCMD_TV_MVOFF BCM_CMD(14)
87 enum lcd_status {
88 LCD_IDLE,
89 LCD_INITIAL,
90 LCD_NEED_UPDATE,
91 LCD_UPDATING
94 struct {
95 long update_timeout;
96 enum lcd_status state;
97 bool blocked;
98 #if NUM_CORES > 1
99 struct corelock cl; /* inter-core sync */
100 #endif
101 #ifdef HAVE_LCD_SLEEP
102 bool display_on;
103 #endif
104 } lcd_state IBSS_ATTR;
106 #ifdef HAVE_LCD_SLEEP
107 static long bcm_off_wait;
108 const fb_data *flash_vmcs_offset;
109 unsigned flash_vmcs_length;
110 /* This mutex exists because enabling the backlight by changing a setting
111 will cause multiple concurrent lcd_wake() calls.
113 static struct mutex lcdstate_lock SHAREDBSS_ATTR;
115 #define ROM_BASE 0x20000000
116 #define ROM_ID(a,b,c,d) (unsigned int)( ((unsigned int)(d)) | \
117 (((unsigned int)(c)) << 8) | \
118 (((unsigned int)(b)) << 16) | \
119 (((unsigned int)(a)) << 24) )
121 /* Get address and length of iPod flash section.
122 Based on part of FS#6721. This may belong elsewhere.
123 (BCM initialization uploads the vmcs section to the BCM.)
125 bool flash_get_section(const unsigned int imageid,
126 void **offset,
127 unsigned int *length)
129 unsigned long *p = (unsigned long*)(ROM_BASE + 0xffe00);
130 unsigned char *csp, *csend;
131 unsigned long checksum;
133 /* Find the image in the directory */
134 while (1) {
135 if (p[0] != ROM_ID('f','l','s','h'))
136 return false;
137 if (p[1] == imageid)
138 break;
139 p += 10;
142 *offset = (void *)(ROM_BASE + p[3]);
143 *length = p[4];
145 /* Verify checksum. Probably unnecessary, but it's fast. */
146 checksum = 0;
147 csend = (unsigned char *)(ROM_BASE + p[3] + p[4]);
148 for(csp = (unsigned char *)(ROM_BASE + p[3]); csp < csend; csp++) {
149 checksum += *csp;
152 return checksum == p[7];
154 #endif /* HAVE_LCD_SLEEP */
156 static inline void bcm_write_addr(unsigned address)
158 BCM_WR_ADDR32 = address; /* write destination address */
160 while (!(BCM_CONTROL & 0x2)); /* wait for it to be write ready */
163 static inline void bcm_write32(unsigned address, unsigned value)
166 bcm_write_addr(address); /* set destination address */
168 BCM_DATA32 = value; /* write value */
171 static inline unsigned bcm_read32(unsigned address)
173 while (!(BCM_RD_ADDR & 1));
175 BCM_RD_ADDR32 = address; /* write source address */
177 while (!(BCM_CONTROL & 0x10)); /* wait for it to be read ready */
179 return BCM_DATA32; /* read value */
182 #ifndef BOOTLOADER
183 static void lcd_tick(void)
185 /* No core level interrupt mask - already in interrupt context */
186 #if NUM_CORES > 1
187 corelock_lock(&lcd_state.cl);
188 #endif
190 if (!lcd_state.blocked && lcd_state.state >= LCD_NEED_UPDATE)
192 unsigned data = bcm_read32(BCMA_COMMAND);
193 bool bcm_is_busy = (data == BCMCMD_LCD_UPDATE || data == 0xFFFF);
195 if (((lcd_state.state == LCD_NEED_UPDATE) && !bcm_is_busy)
196 /* Update requested and BCM is no longer busy. */
197 || (TIME_AFTER(current_tick, lcd_state.update_timeout) && bcm_is_busy))
198 /* BCM still busy after timeout, i.e. stalled. */
200 bcm_write32(BCMA_COMMAND, BCMCMD_LCD_UPDATE); /* Kick off update */
201 BCM_CONTROL = 0x31;
202 lcd_state.update_timeout = current_tick + BCM_UPDATE_TIMEOUT;
203 lcd_state.state = LCD_UPDATING;
205 else if ((lcd_state.state == LCD_UPDATING) && !bcm_is_busy)
207 /* Update finished properly and no new update pending. */
208 lcd_state.state = LCD_IDLE;
211 #if NUM_CORES > 1
212 corelock_unlock(&lcd_state.cl);
213 #endif
216 static inline void lcd_block_tick(void)
218 int oldlevel = disable_irq_save();
220 #if NUM_CORES > 1
221 corelock_lock(&lcd_state.cl);
222 lcd_state.blocked = true;
223 corelock_unlock(&lcd_state.cl);
224 #else
225 lcd_state.blocked = true;
226 #endif
227 restore_irq(oldlevel);
230 static void lcd_unblock_and_update(void)
232 unsigned data;
233 bool bcm_is_busy;
234 int oldlevel = disable_irq_save();
236 #if NUM_CORES > 1
237 corelock_lock(&lcd_state.cl);
238 #endif
239 data = bcm_read32(BCMA_COMMAND);
240 bcm_is_busy = (data == BCMCMD_LCD_UPDATE || data == 0xFFFF);
242 if (!bcm_is_busy || (lcd_state.state == LCD_INITIAL) ||
243 TIME_AFTER(current_tick, lcd_state.update_timeout))
245 bcm_write32(BCMA_COMMAND, BCMCMD_LCD_UPDATE); /* Kick off update */
246 BCM_CONTROL = 0x31;
247 lcd_state.update_timeout = current_tick + BCM_UPDATE_TIMEOUT;
248 lcd_state.state = LCD_UPDATING;
250 else
252 lcd_state.state = LCD_NEED_UPDATE; /* Post update request */
254 lcd_state.blocked = false;
256 #if NUM_CORES > 1
257 corelock_unlock(&lcd_state.cl);
258 #endif
259 restore_irq(oldlevel);
262 #else /* BOOTLOADER */
264 #define lcd_block_tick()
266 static void lcd_unblock_and_update(void)
268 unsigned data;
270 if (lcd_state.state != LCD_INITIAL)
272 data = bcm_read32(BCMA_COMMAND);
273 while (data == BCMCMD_LCD_UPDATE || data == 0xFFFF)
275 yield();
276 data = bcm_read32(BCMA_COMMAND);
279 bcm_write32(BCMA_COMMAND, BCMCMD_LCD_UPDATE); /* Kick off update */
280 BCM_CONTROL = 0x31;
281 lcd_state.state = LCD_IDLE;
283 #endif /* BOOTLOADER */
285 /*** hardware configuration ***/
287 void lcd_set_contrast(int val)
289 /* TODO: Implement lcd_set_contrast() */
290 (void)val;
293 void lcd_set_invert_display(bool yesno)
295 /* TODO: Implement lcd_set_invert_display() */
296 (void)yesno;
299 /* turn the display upside down (call lcd_update() afterwards) */
300 void lcd_set_flip(bool yesno)
302 /* TODO: Implement lcd_set_flip() */
303 (void)yesno;
306 /* LCD init */
307 void lcd_init_device(void)
309 /* These port initializations are supposed to be done when initializing
310 the BCM. None of it is changed when shutting down the BCM.
312 GPO32_ENABLE |= 0x4000;
313 /* GPO32_VAL & 0x8000 may supply power for BCM sleep state */
314 GPO32_ENABLE |= 0x8000;
315 GPO32_VAL &= ~0x8000;
316 GPIO_CLEAR_BITWISE(GPIOC_ENABLE, 0x80);
317 /* This pin is used for BCM interrupts */
318 GPIOC_ENABLE |= 0x40;
319 GPIOC_OUTPUT_EN &= ~0x40;
320 GPO32_ENABLE &= ~1;
322 lcd_state.blocked = false;
323 lcd_state.state = LCD_INITIAL;
324 #ifndef BOOTLOADER
325 #if NUM_CORES > 1
326 corelock_init(&lcd_state.cl);
327 #endif
328 #ifdef HAVE_LCD_SLEEP
329 lcd_state.display_on = true; /* Code in flash turned it on */
330 if (!flash_get_section(ROM_ID('v', 'm', 'c', 's'),
331 (void **)(&flash_vmcs_offset), &flash_vmcs_length))
332 /* BCM cannot be shut down because firmware wasn't found */
333 flash_vmcs_length = 0;
334 else {
335 /* lcd_write_data needs an even number of 16 bit values */
336 flash_vmcs_length = ((flash_vmcs_length + 3) >> 1) & ~1;
338 mutex_init(&lcdstate_lock);
339 #endif
340 tick_add_task(&lcd_tick);
341 #endif /* !BOOTLOADER */
344 /*** update functions ***/
346 /* Update a fraction of the display. */
347 void lcd_update_rect(int x, int y, int width, int height)
349 const fb_data *addr;
350 unsigned bcmaddr;
352 #ifdef HAVE_LCD_SLEEP
353 if (!lcd_state.display_on)
354 return;
355 #endif
357 if (x + width >= LCD_WIDTH)
358 width = LCD_WIDTH - x;
359 if (y + height >= LCD_HEIGHT)
360 height = LCD_HEIGHT - y;
362 if ((width <= 0) || (height <= 0))
363 return; /* Nothing left to do. */
365 /* Ensure x and width are both even. The BCM doesn't like small unaligned
366 * writes and would just ignore them. */
367 width = (width + (x & 1) + 1) & ~1;
368 x &= ~1;
370 /* Prevent the tick from triggering BCM updates while we're writing. */
371 lcd_block_tick();
373 addr = &lcd_framebuffer[y][x];
374 bcmaddr = BCMA_CMDPARAM + (LCD_WIDTH*2) * y + (x << 1);
376 if (width == LCD_WIDTH)
378 bcm_write_addr(bcmaddr);
379 lcd_write_data(addr, width * height);
381 else
385 bcm_write_addr(bcmaddr);
386 bcmaddr += (LCD_WIDTH*2);
387 lcd_write_data(addr, width);
388 addr += LCD_WIDTH;
390 while (--height > 0);
392 lcd_unblock_and_update();
395 /* Update the display.
396 This must be called after all other LCD functions that change the display. */
397 void lcd_update(void)
399 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
402 /* Line write helper function for lcd_yuv_blit. Writes two lines of yuv420. */
403 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
404 unsigned bcmaddr,
405 int width,
406 int stride);
408 /* Performance function to blit a YUV bitmap directly to the LCD */
409 void lcd_blit_yuv(unsigned char * const src[3],
410 int src_x, int src_y, int stride,
411 int x, int y, int width, int height)
413 unsigned bcmaddr;
414 off_t z;
415 unsigned char const * yuv_src[3];
417 #ifdef HAVE_LCD_SLEEP
418 if (!lcd_state.display_on)
419 return;
420 #endif
422 /* Sorry, but width and height must be >= 2 or else */
423 width &= ~1;
425 z = stride * src_y;
426 yuv_src[0] = src[0] + z + src_x;
427 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
428 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
430 /* Prevent the tick from triggering BCM updates while we're writing. */
431 lcd_block_tick();
433 bcmaddr = BCMA_CMDPARAM + (LCD_WIDTH*2) * y + (x << 1);
434 height >>= 1;
438 lcd_write_yuv420_lines(yuv_src, bcmaddr, width, stride);
439 bcmaddr += (LCD_WIDTH*4); /* Skip up two lines */
440 yuv_src[0] += stride << 1;
441 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
442 yuv_src[2] += stride >> 1;
444 while (--height > 0);
446 lcd_unblock_and_update();
449 #ifdef HAVE_LCD_SLEEP
450 /* Executes a BCM command immediately and waits for it to complete.
451 Other BCM commands (eg. LCD updates or lcd_tick) must not interfere.
453 static void bcm_command(unsigned cmd) {
454 unsigned status;
456 bcm_write32(BCMA_COMMAND, cmd);
458 BCM_CONTROL = 0x31;
460 while (1) {
461 status = bcm_read32(BCMA_COMMAND);
462 if (status != cmd && status != 0xFFFF)
463 break;
464 yield();
468 void bcm_powerdown(void)
470 bcm_write32(0x10001400, bcm_read32(0x10001400) & ~0xF0);
472 /* Blanks the LCD and decreases power consumption
473 below what clearing the LCD would achieve.
474 Executing an LCD update command wakes it.
476 bcm_command(BCMCMD_LCD_SLEEP);
478 /* Not sure if this does anything */
479 bcm_command(BCM_CMD(0xC));
481 /* Further cuts power use, probably by powering down BCM.
482 After this point, BCM needs to be bootstrapped
484 GPO32_VAL &= ~0x4000;
487 /* Data written to BCM_CONTROL and BCM_ALT_CONTROL */
488 const unsigned char bcm_bootstrapdata[] = {
489 0xA1, 0x81, 0x91, 0x02, 0x12, 0x22, 0x72, 0x62
492 void bcm_init(void)
494 int i;
496 /* Power up BCM */
497 GPO32_VAL |= 0x4000;
499 /* Changed from HZ/2 to speed up this function */
500 sleep(HZ/8);
502 /* Bootstrap stage 1 */
504 STRAP_OPT_A &= ~0xF00;
505 outl(0x1313, 0x70000040);
507 /* Interrupt-related code for future use
508 GPIOC_INT_LEV |= 0x40;
509 GPIOC_INT_EN |= 0x40;
510 CPU_HI_INT_EN |= 0x40000;
513 /* Bootstrap stage 2 */
515 for (i = 0; i < 8; i++) {
516 BCM_CONTROL = bcm_bootstrapdata[i];
519 for (i = 3; i < 8; i++) {
520 BCM_ALT_CONTROL = bcm_bootstrapdata[i];
523 while ((BCM_RD_ADDR & 1) == 0 || (BCM_ALT_RD_ADDR & 1) == 0)
524 yield();
526 (void)BCM_WR_ADDR;
527 (void)BCM_ALT_WR_ADDR;
529 /* Bootstrap stage 3: upload firmware */
531 while (BCM_ALT_CONTROL & 0x80)
532 yield();
534 while (!(BCM_ALT_CONTROL & 0x40))
535 yield();
537 /* Upload firmware to BCM SRAM */
538 bcm_write_addr(BCMA_SRAM_BASE);
539 lcd_write_data(flash_vmcs_offset, flash_vmcs_length);
541 bcm_write32(BCMA_COMMAND, 0);
542 bcm_write32(0x10000C00, 0xC0000000);
544 while (!(bcm_read32(0x10000C00) & 1))
545 yield();
547 bcm_write32(0x10000C00, 0);
548 bcm_write32(0x10000400, 0xA5A50002);
550 while (bcm_read32(BCMA_COMMAND) == 0)
551 yield();
553 /* sleep(HZ/2) apparently unneeded */
556 void lcd_awake(void)
558 mutex_lock(&lcdstate_lock);
559 if (!lcd_state.display_on && flash_vmcs_length != 0)
561 /* Ensure BCM has been off for 1/2 s at least */
562 while (!TIME_AFTER(current_tick, lcd_state.update_timeout))
563 yield();
565 bcm_init();
567 /* Update LCD, but don't use lcd_update(). Instead, wait here
568 until the command completes so LCD isn't white when the
569 backlight turns on
571 bcm_write_addr(BCMA_CMDPARAM);
572 lcd_write_data(&(lcd_framebuffer[0][0]), LCD_WIDTH * LCD_HEIGHT);
573 bcm_command(BCMCMD_LCD_UPDATE);
575 lcd_state.state = LCD_INITIAL;
576 tick_add_task(&lcd_tick);
577 lcd_state.display_on = true;
578 /* Note that only the RGB data from lcd_framebuffer has been
579 displayed. If YUV data was displayed, it needs to be updated
580 now. (eg. see lcd_call_enable_hook())
583 mutex_unlock(&lcdstate_lock);
586 void lcd_sleep(void)
588 mutex_lock(&lcdstate_lock);
589 if (lcd_state.display_on && flash_vmcs_length != 0) {
590 lcd_state.display_on = false;
592 /* Wait for BCM to finish work */
593 while (lcd_state.state != LCD_INITIAL && lcd_state.state != LCD_IDLE)
594 yield();
596 tick_remove_task(&lcd_tick);
597 bcm_powerdown();
598 bcm_off_wait = current_tick + HZ/2;
600 mutex_unlock(&lcdstate_lock);
603 #ifdef HAVE_LCD_SHUTDOWN
604 void lcd_shutdown(void)
606 lcd_sleep();
608 #endif /* HAVE_LCD_SHUTDOWN */
609 #endif /* HAVE_LCD_SLEEP */