/****************************************************************************
 *  
 * Project: U-Boot support for the LPC22xx and LPC24xx boards from Olimex
 *
 * Copyright: Ivan Vasilev, Olimex Ltd. All rights reserved.
 * 
 * File: $File lpc2478stk.c $
 * Description: Initialize the LPC2478 LCD controller and display a picture
 * Developer: Ivan Vasilev, <ivan at l123.org>
 * This code is provided under the terms of the "beer-ware" license. You may do
 * whatever you like with it and if we meet some day, you may buy me a beer in
 * return.
 *
 ****************************************************************************/
#include <arch/nxp/lpc24xx.h>
#include "lpc2478stk_lcd.h"

/* initialize the LCD from the given configuration. The user has to call 
 * lpc24xx_EnableLCD() after that to actually enable the controller */
void lpc24xx_InitLCD(LCD_INIT_STRUCTURE *lcd)
{
	PCONP |= 1<<20;		/* Turn On LCD PCLK */
	
	PINSEL0 &= 0xfffc00ff;
	PINSEL0 |= 0x00015500;
	PINMODE0&= 0xfffc00ff;
	PINMODE0|= 0x0002aa00;
	PINSEL3 &= 0xf00000ff;
	PINSEL3 |= 0x05555500;
	PINMODE3&= 0xf00000ff;
	PINMODE3|= 0x0aaaaa00;
	PINSEL4 &= 0xf0300000;
	PINSEL4 |= 0x058fffff;
	PINMODE4&= 0xf0300000;
	PINMODE4|= 0x0a8aaaaa;
	PINSEL9 &= 0xf0ffffff;
	PINSEL9 |= 0x0a000000;
	PINMODE9&= 0xf0ffffff;
	PINMODE9|= 0x0a000000;
	PINSEL11&= 0xfffffff0;
	PINSEL11|= 0x0000000f;

	LCD_CFG = (unsigned int)lcd->hclk_divider;

	LCD_TIMH = ((((unsigned int)lcd->horiz_back_porch-1) << 24 ) |
		(((unsigned int)lcd->horiz_front_porch-1) << 16) |
		(((unsigned int)lcd->horiz_sync_pulse-1) << 8 ) | 
		(((lcd->xsize / 16) - 1) << 2 ) );

	LCD_TIMV = (((unsigned int)lcd->vert_back_porch) << 24 ) |
		(((unsigned int)lcd->vert_front_porch) << 16) |
		(((unsigned int)lcd->vert_sync_pulse) << 10) | ( lcd->ysize-1 );

	LCD_POL = 0x04003800 | (((unsigned int)lcd->xsize-1) << 16);
	LCD_LE = 0;
/* make sure the framebuffer is DWORD aligned - fixme */
	LCD_UPBASE = (lcd->framebuffer & (-8)); 
	LCD_LPBASE = (lcd->framebuffer & (-8)); 

	LCD_CTRL = ( 1 << LCD_CTRL_BGR ) |
		( 1 << LCD_CTRL_LCDPWR	) |
		( 1 << LCD_CTRL_LCDTFT	) |
		(((unsigned int)lcd->color_mode ) << LCD_CTRL_LCDBPP );
//	LCD_CTRL  =  0x00000928;
}

void lpc24xx_EnableLCD(void)
{
	LCD_CTRL |= 1 << LCD_CTRL_LCDEN;
}
