/* 2009-08-18: File added by Sony Corporation */
/*
 *  File Name	    : linux/include/asm-arm/arch-mp200/em1_mem.h
 *  Function	    : memory map difinition
 *  Release Version : Ver 1.08
 *  Release Date    : 2009/01/28
 *
 *  Copyright (C) NEC Electronics Corporation 2005-2009
 *
 *
 *  This program is free software;you can redistribute it and/or modify it under the terms of
 *  the GNU General Public License as published by Free Softwere Foundation; either version 2
 *  of License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warrnty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with this program;
 *  If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 *  MA 02111-1307, USA.
 *
 */

#ifndef __PGPF_MEM_H
#define __PGPF_MEM_H

#ifdef __KERNEL__
#define PGPF_PAGE_ALIGN(addr) PAGE_ALIGN(addr)
#define PGPF_PAGE_MASK        PAGE_MASK
#else /* __KERNEL__ */
#define PGPF_PAGE_SHIFT		12
#define PGPF_PAGE_SIZE		(1UL << PGPF_PAGE_SHIFT)
#define PGPF_PAGE_MASK		(~(PGPF_PAGE_SIZE-1))
#define PGPF_PAGE_ALIGN(addr) (((addr)+PGPF_PAGE_SIZE-1)&PGPF_PAGE_MASK)
#endif /* __KERNEL__ */

#ifndef MAX
#define MAX(x,y) (((x) > (y)) ? (x) : (y))
#endif

#ifndef MIN
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#endif

#if defined(CONFIG_PGPF_LCD_800x480)
#define LCD_WIDTH (800)
#define LCD_HEIGHT (480)
#elif defined(CONFIG_PGPF_LCD_480x272)
#define LCD_WIDTH (480)
#define LCD_HEIGHT (272)
#elif defined(CONFIG_PGPF_LCD_320x240)
#define LCD_WIDTH (320)
#define LCD_HEIGHT (240)
#else /* CONFIG_PGPF_LCD_* */
#error "Unsupported LCD size!"
#endif /* CONFIG_PGPF_LCD_* */

#ifdef CONFIG_PGPF_FB_UX_EXT
#define FB_BUF_NUM (2) /* 1: single buffer (don't use)
						  2: double buffer
						  3: triple buffer */
#define FB_RESERVED_SIZE (2 * 1024 * 1024) /* @todo temporary size */
#endif /* CONFIG_PGPF_FB_UX_EXT */

#define NUM_BUF_V4L2 			3 /* set 2 or 3 */

/******* SDRAM ********/
#define INTERDSP_SHARED_ADDR		0x33FE0000 /* CPU<->DSP command buffer */
#define INTERDSP_SHARED_SIZE		0x00020000 /*   128KB */

#if defined(CONFIG_PGPF_OMF_PND)
#define INTERDSP_DL_TOP_ADDR		0x33600000 /* DSP-FW downlaod range (top) */
#elif defined(CONFIG_PGPF_OMF_MODELH)
#define INTERDSP_DL_TOP_ADDR		0x33380000 /* DSP-FW downlaod range (top) */
#endif /* CONFIG_PGPF_OMF_* */
#define INTERDSP_DL_BOTTOM_ADDR		0x33FDFFFF /*                       (end) */

#if defined(CONFIG_PGPF_OMF_PND)
#define OMF_MC_SDRAM_BASE               0x33B00000 /* shared memory for OMF */ 
#elif defined(CONFIG_PGPF_OMF_MODELH)
#define OMF_MC_SDRAM_BASE   			0x33900000 /* shared memory for OMF */
#endif /* CONFIG_PGPF_OMF_* */

#if defined(CONFIG_PGPF_OMF_PND)
#define OMF_MC_ACOUSTIC_PARAM_MEM_ADDR  0x335FF000 /* for param (4KB)  */
#elif defined(CONFIG_PGPF_OMF_MODELH)
#define OMF_MC_ACOUSTIC_PARAM_MEM_ADDR  0x3337F000 /* for param (4KB)  */
#endif /* CONFIG_PGPF_OMF_* */
#define OMF_MC_ACOUSTIC_PARAM_MEM_SIZE	0x00001000 /*   for param (4KB)  */

#if defined(CONFIG_PGPF_OMF_PND)
#define OMF_MC_FRAME_BUFFER_ADDR		(0x33700000)
#define OMF_MC_FRAME_BUFFER_SIZE		(0x00380000)
#elif defined(CONFIG_PGPF_OMF_MODELH)
#define OMF_MC_FRAME_BUFFER_ADDR		(0x33480000)
#define OMF_MC_FRAME_BUFFER_SIZE		(0x00400000)
#endif /* CONFIG_PGPF_OMF_* */

#define OMF_MC_DSP_COMMUNICATION_ADDR	(INTERDSP_SHARED_ADDR - 0x00001000)
#define OMF_MC_DSP_COMMUNICATION_SIZE	(0x00001000)

#ifdef CONFIG_VIDEO_MP200		/* V4L2 driver */
#define IMG_SHARED_SIZE			(int)(LCD_WIDTH * LCD_HEIGHT * 1.5 * 3) /* YUV420 LCD * 3 */
#define IMG_SHARED_ADDR			((OMF_MC_ACOUSTIC_PARAM_MEM_ADDR - IMG_SHARED_SIZE) & PGPF_PAGE_MASK) /* buffer for IPU-IMG */

#if defined(CONFIG_PGPF_OMF_PND)
#define ROT_SHARED_SIZE			(0) /* not use rotation functionality */
#elif defined(CONFIG_PGPF_OMF_MODELH)
#define ROT_SHARED_SIZE			(int)(720 * 480 * 1.5 * 3) /* YUV420 D2 * 3 */
#endif /* CONFIG_PGPF_OMF_* */
#define ROT_SHARED_ADDR			((IMG_SHARED_ADDR - ROT_SHARED_SIZE) & PGPF_PAGE_MASK)
#endif /* CONFIG_VIDEO_MP200 */

#ifdef CONFIG_FB_MP200
#define FB_BPP (16)
#ifdef CONFIG_PGPF_FB_LAYER2
#ifdef CONFIG_PGPF_FB_LAYER2_RGB666_SUPPORT
#define SUB_BPP (18)
#else /* CONFIG_PGPF_FB_LAYER2_RGB666_SUPPORT */
#define SUB_BPP (16)
#endif /* CONFIG_PGPF_FB_LAYER2_RGB666_SUPPORT */
#endif /* CONFIG_PGPF_FB_LAYER2 */

#ifdef CONFIG_PGPF_FB_PND_EXT
#define BASE_BPP (16)
#define OVERLAY_BPP (16)

#ifdef CONFIG_PGPF_LCD_800x480
#define LCD_BASE_MAX_XRES (800)
#define LCD_BASE_MAX_YRES (480)
#else
#define LCD_BASE_MAX_XRES (768)
#ifdef CONFIG_PGPF_FB_PND_REDUCE_MEMORY
#define LCD_BASE_MAX_YRES (488)
#else /* CONFIG_PGPF_FB_PND_REDUCE_MEMORY */
#define LCD_BASE_MAX_YRES (512)
#endif /* CONFIG_PGPF_FB_PND_REDUCE_MEMORY */
#endif

#define LCD_BASE_MIN_XRES LCD_WIDTH
#define LCD_BASE_MIN_YRES LCD_HEIGHT

#define LCD_OVERLAY_MAX_XRES LCD_WIDTH
#define LCD_OVERLAY_MAX_YRES LCD_HEIGHT
#define LCD_CONSOLE_MAX_XRES LCD_WIDTH
#define LCD_CONSOLE_MAX_YRES LCD_HEIGHT
#endif /* CONFIG_PGPF_FB_PND_EXT */

#ifdef CONFIG_PGPF_FB_LAYER2
#ifdef CONFIG_PGPF_FB_PND_EXT
#ifdef CONFIG_PGPF_FB_PND_REDUCE_MEMORY
#define LCD_SUB_MAP_MAX_XRES (LCD_WIDTH)
#define LCD_SUB_MAP_MAX_YRES (LCD_HEIGHT)
#define LCD_SUB_MAP_MIN_XRES (LCD_WIDTH)
#define LCD_SUB_MAP_MIN_YRES (LCD_HEIGHT)
#else /* CONFIG_PGPF_FB_PND_REDUCE_MEMORY */
#define LCD_SUB_MAP_MAX_XRES (LCD_BASE_MAX_XRES / 2)
#define LCD_SUB_MAP_MAX_YRES (LCD_BASE_MAX_YRES)
#define LCD_SUB_MAP_MIN_XRES (LCD_WIDTH / 2)
#define LCD_SUB_MAP_MIN_YRES (LCD_HEIGHT)
#endif /* CONFIG_PGPF_FB_PND_REDUCE_MEMORY */
#define LCD_SUB_PHOTO_XRES   (LCD_WIDTH)
#define LCD_SUB_PHOTO_YRES   (LCD_HEIGHT)
#define LCD_SUB_MAX_XRES     (MAX(LCD_SUB_MAP_MAX_XRES, LCD_SUB_PHOTO_XRES))
#define LCD_SUB_MAX_YRES     (MAX(LCD_SUB_MAP_MAX_YRES, LCD_SUB_PHOTO_YRES))
#define LCD_SUB_MIN_XRES     (MIN(LCD_SUB_MAP_MIN_XRES, LCD_SUB_PHOTO_XRES))
#define LCD_SUB_MIN_YRES     (MIN(LCD_SUB_MAP_MIN_XRES, LCD_SUB_PHOTO_YRES))
#define LCD_SUB_MAP_SIZE     ((int)(LCD_SUB_MAP_MAX_XRES * LCD_SUB_MAP_MAX_YRES * (double)SUB_BPP / 8))
#define LCD_SUB_PHOTO_SIZE   ((int)(LCD_SUB_PHOTO_XRES * LCD_SUB_PHOTO_YRES * (double)SUB_BPP / 8))
#define SUBBUF_LENGTH      (MAX(LCD_SUB_MAP_SIZE, LCD_SUB_PHOTO_SIZE))
#define LCD_SUB_DEFAULT_FRAME_XRES LCD_SUB_MAP_MAX_XRES
#define LCD_SUB_DEFAULT_FRAME_YRES LCD_SUB_MAP_MAX_YRES
#define LCD_SUB_DEFAULT_SCREEN_XRES LCD_SUB_MAP_MIN_XRES
#define LCD_SUB_DEFAULT_SCREEN_YRES LCD_SUB_MAP_MIN_YRES
#else /* CONFIG_PGPF_FB_PND_EXT */
#define LCD_SUB_MAX_XRES   (LCD_WIDTH)
#define LCD_SUB_MAX_YRES   (LCD_HEIGHT)
#define LCD_SUB_MIN_XRES   (LCD_WIDTH)
#define LCD_SUB_MIN_YRES   (LCD_HEIGHT)
#define SUBBUF_LENGTH      ((int)(LCD_SUB_MAX_XRES * LCD_SUB_MAX_YRES * (double)SUB_BPP / 8))
#define LCD_SUB_DEFAULT_FRAME_XRES LCD_SUB_MAX_XRES
#define LCD_SUB_DEFAULT_FRAME_YRES LCD_SUB_MAX_YRES
#define LCD_SUB_DEFAULT_SCREEN_XRES LCD_SUB_MIN_XRES
#define LCD_SUB_DEFAULT_SCREEN_YRES LCD_SUB_MIN_YRES
#endif /* CONFIG_PGPF_FB_PND_EXT */

#endif /* CONFIG_PGPF_FB_LAYER2 */

#if defined(CONFIG_PGPF_FB_PND_EXT)
#define CONSOLEBUF_LENGTH   (LCD_CONSOLE_MAX_XRES * LCD_CONSOLE_MAX_YRES * FB_BPP / 8)
#define DISPBUF_LENGTH      (LCD_OVERLAY_MAX_XRES * LCD_OVERLAY_MAX_YRES * OVERLAY_BPP / 8)
#define BASEBUF_LENGTH      (LCD_BASE_MAX_XRES * LCD_BASE_MAX_YRES * BASE_BPP / 8)
#else /* CONFIG_PGPF_FB_PND_EXT */
#define DISPBUF_LENGTH      (LCD_WIDTH * LCD_HEIGHT * FB_BPP / 8)
#endif /* CONFIG_PGPF_FB_PND_EXT */

#ifdef CONFIG_PGPF_FB_PND_EXT
#define CONSOLE_OFFSET      0x00000000
#define DISPBUF_A_OFFSET    (PGPF_PAGE_ALIGN(CONSOLE_OFFSET + CONSOLEBUF_LENGTH))
#define DISPBUF_B_OFFSET	(PGPF_PAGE_ALIGN(DISPBUF_A_OFFSET + DISPBUF_LENGTH))
#define BASE_A_OFFSET	    (PGPF_PAGE_ALIGN(DISPBUF_B_OFFSET + DISPBUF_LENGTH))
#define BASE_B_OFFSET	    (PGPF_PAGE_ALIGN(BASE_A_OFFSET + BASEBUF_LENGTH))
#ifdef CONFIG_PGPF_FB_LAYER2
#ifdef CONFIG_PGPF_FB_PND_REDUCE_MEMORY
#define SUB_A_OFFSET        (CONSOLE_OFFSET)
#define SUB_B_OFFSET        (PGPF_PAGE_ALIGN(BASE_B_OFFSET + BASEBUF_LENGTH))
#else /* CONFIG_PGPF_FB_PND_REDUCE_MEMORY */
#define SUB_A_OFFSET        (PGPF_PAGE_ALIGN(BASE_B_OFFSET + BASEBUF_LENGTH))
#define SUB_B_OFFSET        (PGPF_PAGE_ALIGN(SUB_A_OFFSET + SUBBUF_LENGTH))
#endif /* CONFIG_PGPF_FB_PND_REDUCE_MEMORY */
#define FB_FRAME_BUFFER_FB_SIZE (PGPF_PAGE_ALIGN(SUB_B_OFFSET + SUBBUF_LENGTH))
#else /* CONFIG_PGPF_FB_LAYER2 */
#define FB_FRAME_BUFFER_FB_SIZE (PGPF_PAGE_ALIGN(BASE_B_OFFSET + BASEBUF_LENGTH))
#endif /* CONFIG_PGPF_FB_LAYER2 */
#else /* CONFIG_PGPF_FB_PND_EXT */
#define DISPBUF_A_OFFSET    0x00000000
#define DISPBUF_B_OFFSET    (PGPF_PAGE_ALIGN(DISPBUF_A_OFFSET + DISPBUF_LENGTH))
#ifdef CONFIG_PGPF_FB_UX_EXT
#define FB_FRAME_BUFFER_DISP_SIZE (PGPF_PAGE_ALIGN(DISPBUF_LENGTH) * FB_BUF_NUM + FB_RESERVED_SIZE) 
#else /* CONFIG_PGPF_FB_UX_EXT */
#define FB_FRAME_BUFFER_DISP_SIZE (PGPF_PAGE_ALIGN(DISPBUF_B_OFFSET + DISPBUF_LENGTH))
#endif /* CONFIG_PGPF_FB_UX_EXT */
#ifdef CONFIG_PGPF_FB_LAYER2
#define SUB_A_OFFSET            (FB_FRAME_BUFFER_DISP_SIZE)
#define SUB_B_OFFSET            (PGPF_PAGE_ALIGN(SUB_A_OFFSET + SUBBUF_LENGTH))
#define FB_FRAME_BUFFER_FB_SIZE (PGPF_PAGE_ALIGN(SUB_B_OFFSET + SUBBUF_LENGTH))
#else /* CONFIG_PGPF_FB_LAYER2 */
#define FB_FRAME_BUFFER_FB_SIZE (FB_FRAME_BUFFER_DISP_SIZE)
#endif /* CONFIG_PGPF_FB_LAYER2 */
#endif /* CONFIG_PGPF_FB_PND_EXT */

#ifdef CONFIG_MP200_NTS
#define NTSCFRAME_LENGTH	(NTS_WIDTH * NTS_HEIGHT * 2) /* YUV422 PAL (720 * 576 * 2) */
#define NTSCFRAME_A_OFFSET	FB_FRAME_BUFFER_FB_SIZE
#define NTSCFRAME_B_OFFSET	(PGPF_PAGE_ALIGN(FB_FRAME_BUFFER_FB_SIZE + NTSCFRAME_LENGTH))

#ifdef CONFIG_FB_MP200_NTS
#define NTSCBUF_LENGTH		(NTS_WIDTH * NTS_HEIGHT * 2) /* RGB565 (720 * 576 * 2) */
#define NTSCBUF_A_OFFSET	(PGPF_PAGE_ALIGN(NTSCFRAME_B_OFFSET + NTSCFRAME_LENGTH))
#define NTSCBUF_B_OFFSET	(PGPF_PAGE_ALIGN(NTSCBUF_A_OFFSET + NTSCBUF_LENGTH))

#define NTSCWORK_LENGTH		(NTS_WIDTH * NTS_HEIGHT * 2) /* RGB565 PAL (720 * 576 * 2) */
#define NTSCWORK_OFFSET		(PGPF_PAGE_ALIGN(NTSCBUF_B_OFFSET + NTSCBUF_LENGTH))
#define FB_FRAME_BUFFER_NTS_SIZE (PGPF_PAGE_ALIGN(NTSCWORK_OFFSET + NTSCWORK_LENGTH)
#else /* CONFIG_FB_MP200_NTS */
#define FB_FRAME_BUFFER_NTS_SIZE (PGPF_PAGE_ALIGN(NTSCFRAME_B_OFFSET + NTSCFRAME_LENGTH_LENGTH)
#endif /* CONFIG_FB_MP200_NTS */
#else /* CONFIG_MP200_NTS */
#define FB_FRAME_BUFFER_NTS_SIZE (0)
#endif /* CONFIG_MP200_NTS */

#define FB_FRAME_BUFFER_SIZE (FB_FRAME_BUFFER_FB_SIZE + FB_FRAME_BUFFER_NTS_SIZE)
#ifdef CONFIG_VIDEO_MP200
#define FB_FRAME_BUFFER_ADDR ((ROT_SHARED_ADDR - FB_FRAME_BUFFER_FB_SIZE) & PGPF_PAGE_MASK)
#else /* CONFIG_VIDEO_MP200 */
#define FB_FRAME_BUFFER_ADDR ((OMF_MC_ACOUSTIC_PARAM_MEM_ADDR - FB_FRAME_BUFFER_FB_SIZE) & PGPF_PAGE_MASK)
#endif /* CONFIG_VIDEO_MP200 */
#endif

/* SDRAM difinition for OMF/kenrel drivers  */

#define SHARED_MEM_ADDRESS 	INTERDSP_SHARED_ADDR	/* CPU<->DSP command buffer start address*/
#define SHARED_MEM_SIZE 	INTERDSP_SHARED_SIZE
#define DOWNLOAD_RANGE_START 	INTERDSP_DL_TOP_ADDR
#define DOWNLOAD_RANGE_END 	INTERDSP_DL_BOTTOM_ADDR

#define OMF_MC_AUDIO_SHARED_MEMORY_ADDR (OMF_MC_SDRAM_BASE + 0x00000000) /* for audio (564KB) */
#define OMF_MC_VIDEO_SHARED_MEMORY_ADDR (OMF_MC_SDRAM_BASE + 0x0008D000) /* for video (6468KB) */

#define FRONT_WIDTH    LCD_WIDTH                       /* for FrameBuf (LCDC) Width  */
#define FRONT_HEIGHT   LCD_HEIGHT                      /* for FrameBuf (LCDC) Height */
#define FRONT_WIDTH_V  FRONT_WIDTH                     /* for DispBuf  (2D)   Width  */
#define FRONT_HEIGHT_V FRONT_HEIGHT            /* for DispBuf  (2D)   Height */

#define BYTES_PER_PIXEL		2	/* for DispBuf  (2D)   - 16bpp */
#ifdef CONFIG_MP200_LCD_RGB666
#define LCD_DISPLAY_RGB666      1
#define BITS_PER_PIXEL          18      /* for FrameBuf (LCDC RGB666) - 18bpp */
#else
#define LCD_DISPLAY_RGB666      0
#define BITS_PER_PIXEL          16      /* for FrameBuf (LCDC RGB565) - 16bpp */
#endif

/* NTS display size */
#ifdef CONFIG_MP200_NTS
#define NTSC_WIDTH		720
#define NTSC_HEIGHT		486
#define PAL_WIDTH		720
#define PAL_HEIGHT		576
#define NTS_WIDTH		(NTSC_WIDTH  > PAL_WIDTH  ? NTSC_WIDTH  : PAL_WIDTH)
#define NTS_HEIGHT		(NTSC_HEIGHT > PAL_HEIGHT ? NTSC_HEIGHT : PAL_HEIGHT)
#define NTS_WIDTH_V		(NTSC_WIDTH  > PAL_WIDTH  ? NTSC_WIDTH  : PAL_WIDTH)
#define NTS_HEIGHT_V		(NTSC_HEIGHT > PAL_HEIGHT ? NTSC_HEIGHT : PAL_HEIGHT)
#endif /* CONFIG_MP200_NTS */

#ifdef CONFIG_FB_MP200
#define SMEM_START		FB_FRAME_BUFFER_ADDR	/* frame buffer, X/fb driver common, etc */
#define SMEM_LENGTH		FB_FRAME_BUFFER_SIZE
#endif

#ifdef CONFIG_VIDEO_MP200	/* V4L2 driver */
#define ROT_SMEM_START		ROT_SHARED_ADDR		/* for rotate image (common with DSP) */
#define ROT_SMEM_LENGTH		ROT_SHARED_SIZE		/* YUV420 WVGA (800 * 480 * 1.5) */
#define IMG_SMEM_START		IMG_SHARED_ADDR		/* for resize image (common with DSP) */
#define IMG_SMEM_LENGTH		IMG_SHARED_SIZE		/* YUV420 WVGA (800 * 480 * 1.5) */
#endif
#endif /* __PGPF_MEM_H */
