Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / firmware / target / arm / ipod / video / lcd-video.c
blob74d627730fd577474088a3a641ccaf592e9fc74c
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 ****************************************************************************/
29 #include <sys/types.h> /* off_t */
30 #include "config.h"
31 #include "cpu.h"
32 #include "lcd.h"
33 #include "kernel.h"
34 #include "system.h"
35 #ifdef HAVE_LCD_SLEEP
36 /* Included only for lcd_awake() prototype */
37 #include "backlight-target.h"
38 #endif
40 /* The BCM bus width is 16 bits. But since the low address bits aren't decoded
41 * by the chip (the 3 BCM address bits are mapped to address bits 16..18 of the
42 * PP5022), writing 32 bits (and even more, using 'stmia') at once works. */
43 #define BCM_DATA (*(volatile unsigned short*)(0x30000000))
44 #define BCM_DATA32 (*(volatile unsigned long *)(0x30000000))
45 #define BCM_WR_ADDR (*(volatile unsigned short*)(0x30010000))
46 #define BCM_WR_ADDR32 (*(volatile unsigned long *)(0x30010000))
47 #define BCM_RD_ADDR (*(volatile unsigned short*)(0x30020000))
48 #define BCM_RD_ADDR32 (*(volatile unsigned long *)(0x30020000))
49 #define BCM_CONTROL (*(volatile unsigned short*)(0x30030000))
51 #define BCM_ALT_DATA (*(volatile unsigned short*)(0x30040000))
52 #define BCM_ALT_DATA32 (*(volatile unsigned long *)(0x30040000))
53 #define BCM_ALT_WR_ADDR (*(volatile unsigned short*)(0x30050000))
54 #define BCM_ALT_WR_ADDR32 (*(volatile unsigned long *)(0x30050000))
55 #define BCM_ALT_RD_ADDR (*(volatile unsigned short*)(0x30060000))
56 #define BCM_ALT_RD_ADDR32 (*(volatile unsigned long *)(0x30060000))
57 #define BCM_ALT_CONTROL (*(volatile unsigned short*)(0x30070000))
59 /* Time until the BCM is considered stalled and will be re-kicked.
60 * Must be guaranteed to be >~ 20ms. */
61 #define BCM_UPDATE_TIMEOUT (HZ/20)
62 /* An LCD update command done while the LCD is off needs >~ 200ms */
63 #define BCM_LCDINIT_TIMEOUT (HZ/2)
65 /* Addresses within BCM */
66 #define BCMA_SRAM_BASE 0
67 #define BCMA_COMMAND 0x1F8
68 #define BCMA_STATUS 0x1FC
69 #define BCMA_CMDPARAM 0xE0000 /* Parameters/data for commands */
70 #define BCMA_SDRAM_BASE 0xC0000000
71 #define BCMA_TV_FB 0xC0000000 /* TV out framebuffer */
72 #define BCMA_TV_BMPDATA 0xC0200000 /* BMP data for TV out functions */
74 /* BCM commands. Write them to BCMA_COMMAND. Note BCM_CMD encoding. */
75 #define BCM_CMD(x) ((~((unsigned long)x) << 16) | ((unsigned long)x))
76 #define BCMCMD_LCD_UPDATE BCM_CMD(0)
77 /* Execute "M25 Diagnostics". Status displayed on LCD. Takes <40s */
78 #define BCMCMD_SELFTEST BCM_CMD(1)
79 #define BCMCMD_TV_PALBMP BCM_CMD(2)
80 #define BCMCMD_TV_NTSCBMP BCM_CMD(3)
81 /* BCM_CMD(4) may be another TV-related command */
82 /* The following might do more depending on word at 0xE00000 */
83 #define BCMCMD_LCD_UPDATERECT BCM_CMD(5)
84 #define BCMCMD_LCD_SLEEP BCM_CMD(8)
85 /* BCM_CMD(12) involved in shutdown */
86 /* Macrovision analog copy prevention is on by default on TV output.
87 Execute this command after enabling TV out to turn it off.
89 #define BCMCMD_TV_MVOFF BCM_CMD(14)
91 enum lcd_status
93 LCD_IDLE,
94 LCD_INITIAL,
95 LCD_NEED_UPDATE,
96 LCD_UPDATING
99 struct
101 long update_timeout; /* also used to ensure BCM stays off for >= 50 ms */
102 enum lcd_status state;
103 bool blocked;
104 #if NUM_CORES > 1
105 struct corelock cl; /* inter-core sync */
106 #endif
107 #ifdef HAVE_LCD_SLEEP
108 bool display_on;
109 bool waking;
110 struct wakeup initwakeup;
111 #endif
112 } lcd_state IBSS_ATTR;
114 #ifdef HAVE_LCD_SLEEP
115 const fb_data *flash_vmcs_offset;
116 unsigned flash_vmcs_length;
118 #define ROM_BASE 0x20000000
119 #define ROM_ID(a,b,c,d) (unsigned int)( ((unsigned int)(d)) | \
120 (((unsigned int)(c)) << 8) | \
121 (((unsigned int)(b)) << 16) | \
122 (((unsigned int)(a)) << 24) )
124 /* Get address and length of iPod flash section.
125 Based on part of FS#6721. This may belong elsewhere.
126 (BCM initialization uploads the vmcs section to the BCM.)
128 static bool flash_get_section(const unsigned int imageid,
129 void **offset,
130 unsigned int *length)
132 unsigned long *p = (unsigned long*)(ROM_BASE + 0xffe00);
133 unsigned char *csp, *csend;
134 unsigned long checksum;
136 /* Find the image in the directory */
137 while (1)
139 if (p[0] != ROM_ID('f','l','s','h'))
140 return false;
141 if (p[1] == imageid)
142 break;
143 p += 10;
146 *offset = (void *)(ROM_BASE + p[3]);
147 *length = p[4];
149 /* Verify checksum. Probably unnecessary, but it's fast. */
150 checksum = 0;
151 csend = (unsigned char *)(ROM_BASE + p[3] + p[4]);
152 for(csp = (unsigned char *)(ROM_BASE + p[3]); csp < csend; csp++)
154 checksum += *csp;
157 return checksum == p[7];
159 #endif /* HAVE_LCD_SLEEP */
161 static inline void bcm_write_addr(unsigned address)
163 BCM_WR_ADDR32 = address; /* write destination address */
165 while (!(BCM_CONTROL & 0x2)); /* wait for it to be write ready */
168 static inline void bcm_write32(unsigned address, unsigned value)
171 bcm_write_addr(address); /* set destination address */
173 BCM_DATA32 = value; /* write value */
176 static inline unsigned bcm_read32(unsigned address)
178 while (!(BCM_RD_ADDR & 1));
180 BCM_RD_ADDR32 = address; /* write source address */
182 while (!(BCM_CONTROL & 0x10)); /* wait for it to be read ready */
184 return BCM_DATA32; /* read value */
187 #ifdef HAVE_LCD_SLEEP
188 static void continue_lcd_awake(void)
190 lcd_state.waking = false;
191 wakeup_signal(&(lcd_state.initwakeup));
193 #endif
195 #ifndef BOOTLOADER
196 static void lcd_tick(void)
198 /* No core level interrupt mask - already in interrupt context */
199 #if NUM_CORES > 1
200 corelock_lock(&lcd_state.cl);
201 #endif
203 if (!lcd_state.blocked && lcd_state.state >= LCD_NEED_UPDATE)
205 unsigned data = bcm_read32(BCMA_COMMAND);
206 bool bcm_is_busy = (data == BCMCMD_LCD_UPDATE || data == 0xFFFF);
208 if (((lcd_state.state == LCD_NEED_UPDATE) && !bcm_is_busy)
209 /* Update requested and BCM is no longer busy. */
210 || (TIME_AFTER(current_tick, lcd_state.update_timeout) && bcm_is_busy))
211 /* BCM still busy after timeout, i.e. stalled. */
213 bcm_write32(BCMA_COMMAND, BCMCMD_LCD_UPDATE); /* Kick off update */
214 BCM_CONTROL = 0x31;
215 lcd_state.update_timeout = current_tick + BCM_UPDATE_TIMEOUT;
216 lcd_state.state = LCD_UPDATING;
217 #ifdef HAVE_LCD_SLEEP
218 if (lcd_state.waking)
219 continue_lcd_awake();
220 #endif
222 else if ((lcd_state.state == LCD_UPDATING) && !bcm_is_busy)
224 /* Update finished properly and no new update pending. */
225 lcd_state.state = LCD_IDLE;
226 #ifdef HAVE_LCD_SLEEP
227 if (lcd_state.waking)
228 continue_lcd_awake();
229 #endif
232 #if NUM_CORES > 1
233 corelock_unlock(&lcd_state.cl);
234 #endif
237 static inline void lcd_block_tick(void)
239 int oldlevel = disable_irq_save();
241 #if NUM_CORES > 1
242 corelock_lock(&lcd_state.cl);
243 lcd_state.blocked = true;
244 corelock_unlock(&lcd_state.cl);
245 #else
246 lcd_state.blocked = true;
247 #endif
248 restore_irq(oldlevel);
251 static void lcd_unblock_and_update(void)
253 unsigned data;
254 bool bcm_is_busy;
255 int oldlevel = disable_irq_save();
257 #if NUM_CORES > 1
258 corelock_lock(&lcd_state.cl);
259 #endif
260 data = bcm_read32(BCMA_COMMAND);
261 bcm_is_busy = (data == BCMCMD_LCD_UPDATE || data == 0xFFFF);
263 if (!bcm_is_busy || (lcd_state.state == LCD_INITIAL) ||
264 TIME_AFTER(current_tick, lcd_state.update_timeout))
266 bcm_write32(BCMA_COMMAND, BCMCMD_LCD_UPDATE); /* Kick off update */
267 BCM_CONTROL = 0x31;
268 lcd_state.update_timeout = current_tick + BCM_UPDATE_TIMEOUT;
269 lcd_state.state = LCD_UPDATING;
270 #ifdef HAVE_LCD_SLEEP
271 if (lcd_state.waking)
272 continue_lcd_awake();
273 #endif
275 else
277 lcd_state.state = LCD_NEED_UPDATE; /* Post update request */
279 lcd_state.blocked = false;
281 #if NUM_CORES > 1
282 corelock_unlock(&lcd_state.cl);
283 #endif
284 restore_irq(oldlevel);
287 #else /* BOOTLOADER */
289 #define lcd_block_tick()
291 static void lcd_unblock_and_update(void)
293 unsigned data;
295 if (lcd_state.state != LCD_INITIAL)
297 data = bcm_read32(BCMA_COMMAND);
298 while (data == BCMCMD_LCD_UPDATE || data == 0xFFFF)
300 yield();
301 data = bcm_read32(BCMA_COMMAND);
304 bcm_write32(BCMA_COMMAND, BCMCMD_LCD_UPDATE); /* Kick off update */
305 BCM_CONTROL = 0x31;
306 lcd_state.state = LCD_IDLE;
308 #endif /* BOOTLOADER */
310 /*** hardware configuration ***/
312 void lcd_set_contrast(int val)
314 /* TODO: Implement lcd_set_contrast() */
315 (void)val;
318 void lcd_set_invert_display(bool yesno)
320 /* TODO: Implement lcd_set_invert_display() */
321 (void)yesno;
324 /* turn the display upside down (call lcd_update() afterwards) */
325 void lcd_set_flip(bool yesno)
327 /* TODO: Implement lcd_set_flip() */
328 (void)yesno;
331 /* LCD init */
332 void lcd_init_device(void)
334 /* These port initializations are supposed to be done when initializing
335 the BCM. None of it is changed when shutting down the BCM.
337 GPO32_ENABLE |= 0xC000;
338 GPIO_CLEAR_BITWISE(GPIOC_ENABLE, 0x80);
339 /* This pin is used for BCM interrupts */
340 GPIOC_ENABLE |= 0x40;
341 GPIOC_OUTPUT_EN &= ~0x40;
342 GPO32_ENABLE &= ~1;
344 lcd_state.blocked = false;
345 lcd_state.state = LCD_INITIAL;
346 #ifndef BOOTLOADER
347 #if NUM_CORES > 1
348 corelock_init(&lcd_state.cl);
349 #endif
350 #ifdef HAVE_LCD_SLEEP
351 if (!flash_get_section(ROM_ID('v', 'm', 'c', 's'),
352 (void **)(&flash_vmcs_offset), &flash_vmcs_length))
353 /* BCM cannot be shut down because firmware wasn't found */
354 flash_vmcs_length = 0;
355 else
357 /* lcd_write_data needs an even number of 16 bit values */
358 flash_vmcs_length = ((flash_vmcs_length + 3) >> 1) & ~1;
360 wakeup_init(&(lcd_state.initwakeup));
361 lcd_state.waking = false;
363 if (GPO32_VAL & 0x4000)
365 /* BCM is powered. Assume it is initialized. */
366 lcd_state.display_on = true;
367 tick_add_task(&lcd_tick);
369 else
371 /* BCM is not powered, so it needs to be initialized.
372 This can only happen when loading Rockbox via ROLO.
374 lcd_state.update_timeout = current_tick;
375 lcd_state.display_on = false;
376 lcd_awake();
378 #else /* !HAVE_LCD_SLEEP */
379 tick_add_task(&lcd_tick);
380 #endif
381 #endif /* !BOOTLOADER */
384 /*** update functions ***/
386 /* Update a fraction of the display. */
387 void lcd_update_rect(int x, int y, int width, int height)
389 const fb_data *addr;
390 unsigned bcmaddr;
392 #ifdef HAVE_LCD_SLEEP
393 if (!lcd_state.display_on)
394 return;
395 #endif
397 if (x + width >= LCD_WIDTH)
398 width = LCD_WIDTH - x;
399 if (y + height >= LCD_HEIGHT)
400 height = LCD_HEIGHT - y;
402 if ((width <= 0) || (height <= 0))
403 return; /* Nothing left to do. */
405 /* Ensure x and width are both even. The BCM doesn't like small unaligned
406 * writes and would just ignore them. */
407 width = (width + (x & 1) + 1) & ~1;
408 x &= ~1;
410 /* Prevent the tick from triggering BCM updates while we're writing. */
411 lcd_block_tick();
413 addr = &lcd_framebuffer[y][x];
414 bcmaddr = BCMA_CMDPARAM + (LCD_WIDTH*2) * y + (x << 1);
416 if (width == LCD_WIDTH)
418 bcm_write_addr(bcmaddr);
419 lcd_write_data(addr, width * height);
421 else
425 bcm_write_addr(bcmaddr);
426 bcmaddr += (LCD_WIDTH*2);
427 lcd_write_data(addr, width);
428 addr += LCD_WIDTH;
430 while (--height > 0);
432 lcd_unblock_and_update();
435 /* Update the display.
436 This must be called after all other LCD functions that change the display. */
437 void lcd_update(void)
439 lcd_update_rect(0, 0, LCD_WIDTH, LCD_HEIGHT);
442 /* Line write helper function for lcd_yuv_blit. Writes two lines of yuv420. */
443 extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
444 unsigned bcmaddr,
445 int width,
446 int stride);
448 /* Performance function to blit a YUV bitmap directly to the LCD */
449 void lcd_blit_yuv(unsigned char * const src[3],
450 int src_x, int src_y, int stride,
451 int x, int y, int width, int height)
453 unsigned bcmaddr;
454 off_t z;
455 unsigned char const * yuv_src[3];
457 #ifdef HAVE_LCD_SLEEP
458 if (!lcd_state.display_on)
459 return;
460 #endif
462 /* Sorry, but width and height must be >= 2 or else */
463 width &= ~1;
465 z = stride * src_y;
466 yuv_src[0] = src[0] + z + src_x;
467 yuv_src[1] = src[1] + (z >> 2) + (src_x >> 1);
468 yuv_src[2] = src[2] + (yuv_src[1] - src[1]);
470 /* Prevent the tick from triggering BCM updates while we're writing. */
471 lcd_block_tick();
473 bcmaddr = BCMA_CMDPARAM + (LCD_WIDTH*2) * y + (x << 1);
474 height >>= 1;
478 lcd_write_yuv420_lines(yuv_src, bcmaddr, width, stride);
479 bcmaddr += (LCD_WIDTH*4); /* Skip up two lines */
480 yuv_src[0] += stride << 1;
481 yuv_src[1] += stride >> 1; /* Skip down one chroma line */
482 yuv_src[2] += stride >> 1;
484 while (--height > 0);
486 lcd_unblock_and_update();
489 #ifdef HAVE_LCD_SLEEP
490 /* Executes a BCM command immediately and waits for it to complete.
491 Other BCM commands (eg. LCD updates or lcd_tick) must not interfere.
493 static void bcm_command(unsigned cmd)
495 unsigned status;
497 bcm_write32(BCMA_COMMAND, cmd);
499 BCM_CONTROL = 0x31;
501 while (1)
503 status = bcm_read32(BCMA_COMMAND);
504 if (status != cmd && status != 0xFFFF)
505 break;
506 yield();
510 static void bcm_powerdown(void)
512 bcm_write32(0x10001400, bcm_read32(0x10001400) & ~0xF0);
514 /* Blanks the LCD and decreases power consumption
515 below what clearing the LCD would achieve.
516 Executing an LCD update command wakes it.
518 bcm_command(BCMCMD_LCD_SLEEP);
520 /* Not sure if this does anything */
521 bcm_command(BCM_CMD(0xC));
523 /* Further cuts power use, probably by powering down BCM.
524 After this point, BCM needs to be bootstrapped
526 GPO32_VAL &= ~0x4000;
529 /* Data written to BCM_CONTROL and BCM_ALT_CONTROL */
530 const unsigned char bcm_bootstrapdata[] =
532 0xA1, 0x81, 0x91, 0x02, 0x12, 0x22, 0x72, 0x62
535 static void bcm_init(void)
537 int i;
539 /* Power up BCM */
540 GPO32_VAL |= 0x4000;
541 sleep(HZ/20);
543 /* Bootstrap stage 1 */
545 STRAP_OPT_A &= ~0xF00;
546 outl(0x1313, 0x70000040);
548 /* Interrupt-related code for future use
549 GPIOC_INT_LEV |= 0x40;
550 GPIOC_INT_EN |= 0x40;
551 CPU_HI_INT_EN |= 0x40000;
554 /* Bootstrap stage 2 */
556 while (BCM_ALT_CONTROL & 0x80);
557 while (!(BCM_ALT_CONTROL & 0x40));
559 for (i = 0; i < 8; i++)
561 BCM_CONTROL = bcm_bootstrapdata[i];
564 for (i = 3; i < 8; i++)
566 BCM_ALT_CONTROL = bcm_bootstrapdata[i];
569 while ((BCM_RD_ADDR & 1) == 0 || (BCM_ALT_RD_ADDR & 1) == 0);
571 (void)BCM_WR_ADDR;
572 (void)BCM_ALT_WR_ADDR;
574 /* Bootstrap stage 3: upload firmware */
576 while (BCM_ALT_CONTROL & 0x80);
577 while (!(BCM_ALT_CONTROL & 0x40));
579 /* Upload firmware to BCM SRAM */
580 bcm_write_addr(BCMA_SRAM_BASE);
581 lcd_write_data(flash_vmcs_offset, flash_vmcs_length);
583 bcm_write32(BCMA_COMMAND, 0);
584 bcm_write32(0x10000C00, 0xC0000000);
586 while (!(bcm_read32(0x10000C00) & 1));
588 bcm_write32(0x10000C00, 0);
589 bcm_write32(0x10000400, 0xA5A50002);
591 while (bcm_read32(BCMA_COMMAND) == 0)
592 yield();
594 /* sleep(HZ/2) apparently unneeded */
597 void lcd_awake(void)
599 if (!lcd_state.display_on && flash_vmcs_length != 0)
601 /* Ensure BCM has been off for >= 50 ms */
602 long sleepwait = lcd_state.update_timeout + HZ/20 - current_tick;
603 if (sleepwait > 0 && sleepwait < HZ/20)
604 sleep(sleepwait);
606 bcm_init();
608 /* Start the first LCD update, which also initializes the LCD */
609 lcd_state.state = LCD_INITIAL;
610 lcd_state.display_on = true;
611 lcd_update();
612 lcd_state.update_timeout = current_tick + BCM_LCDINIT_TIMEOUT;
614 /* Wait for end of first LCD update, so LCD isn't white
615 when the backlight turns on.
617 lcd_state.waking = true;
618 tick_add_task(&lcd_tick);
619 wakeup_wait(&(lcd_state.initwakeup), TIMEOUT_BLOCK);
621 send_event(LCD_EVENT_ACTIVATION, NULL);
625 void lcd_sleep(void)
627 if (lcd_state.display_on && flash_vmcs_length != 0)
629 lcd_state.display_on = false;
631 /* Wait for BCM to finish work */
632 while (lcd_state.state != LCD_INITIAL && lcd_state.state != LCD_IDLE)
633 yield();
635 tick_remove_task(&lcd_tick);
636 bcm_powerdown();
638 /* Remember time to ensure BCM stays off for >= 50 ms */
639 lcd_state.update_timeout = current_tick;
643 bool lcd_active(void)
645 return lcd_state.display_on;
648 #ifdef HAVE_LCD_SHUTDOWN
649 void lcd_shutdown(void)
651 lcd_sleep();
653 #endif /* HAVE_LCD_SHUTDOWN */
654 #endif /* HAVE_LCD_SLEEP */