fb.c 10.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/*
 * File: arch/arm/plat-omap/fb.c
 *
 * Framebuffer device registration for TI OMAP platforms
 *
 * Copyright (C) 2006 Nokia Corporation
 * Author: Imre Deak <imre.deak@nokia.com>
 *
 * 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 the
 * Free Software Foundation; either version 2 of the 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 warranty 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.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/memblock.h>
#include <linux/io.h>
#include <linux/omapfb.h>

#include <mach/hardware.h>
#include <asm/mach/map.h>

#include <plat/board.h>
#include <plat/sram.h>

#include "fb.h"

#if defined(CONFIG_FB_OMAP) || defined(CONFIG_FB_OMAP_MODULE)

static struct omapfb_platform_data omapfb_config;
static int config_invalid;
static int configured_regions;

static u64 omap_fb_dma_mask = ~(u32)0;

static struct platform_device omap_fb_device = {
	.name		= "omapfb",
	.id		= -1,
	.dev = {
		.dma_mask		= &omap_fb_dma_mask,
		.coherent_dma_mask	= ~(u32)0,
		.platform_data		= &omapfb_config,
	},
	.num_resources = 0,
};

void omapfb_set_platform_data(struct omapfb_platform_data *data)
{
}

static inline int ranges_overlap(unsigned long start1, unsigned long size1,
				 unsigned long start2, unsigned long size2)
{
	return (start1 >= start2 && start1 < start2 + size2) ||
	       (start2 >= start1 && start2 < start1 + size1);
}

static inline int range_included(unsigned long start1, unsigned long size1,
				 unsigned long start2, unsigned long size2)
{
	return start1 >= start2 && start1 + size1 <= start2 + size2;
}


/* Check if there is an overlapping region. */
static int fbmem_region_reserved(unsigned long start, size_t size)
{
	struct omapfb_mem_region *rg;
	int i;

	rg = &omapfb_config.mem_desc.region[0];
	for (i = 0; i < OMAPFB_PLANE_NUM; i++, rg++) {
		if (!rg->paddr)
			/* Empty slot. */
			continue;
		if (ranges_overlap(start, size, rg->paddr, rg->size))
			return 1;
	}
	return 0;
}

/*
 * Get the region_idx`th region from board config/ATAG and convert it to
 * our internal format.
 */
static int __init get_fbmem_region(int region_idx, struct omapfb_mem_region *rg)
{
	const struct omap_fbmem_config	*conf;
	u32				paddr;

	conf = omap_get_nr_config(OMAP_TAG_FBMEM,
				  struct omap_fbmem_config, region_idx);
	if (conf == NULL)
		return -ENOENT;

	paddr = conf->start;
	/*
	 * Low bits encode the page allocation mode, if high bits
	 * are zero. Otherwise we need a page aligned fixed
	 * address.
	 */
	memset(rg, 0, sizeof(*rg));
	rg->type = paddr & ~PAGE_MASK;
	rg->paddr = paddr & PAGE_MASK;
	rg->size = PAGE_ALIGN(conf->size);
	return 0;
}

static int set_fbmem_region_type(struct omapfb_mem_region *rg, int mem_type,
				  unsigned long mem_start,
				  unsigned long mem_size)
{
	/*
	 * Check if the configuration specifies the type explicitly.
	 * type = 0 && paddr = 0, a default don't care case maps to
	 * the SDRAM type.
	 */
	if (rg->type || !rg->paddr)
		return 0;
	if (ranges_overlap(rg->paddr, rg->size, mem_start, mem_size)) {
		rg->type = mem_type;
		return 0;
	}
	/* Can't determine it. */
	return -1;
}

static int check_fbmem_region(int region_idx, struct omapfb_mem_region *rg,
			      unsigned long start_avail, unsigned size_avail)
{
	unsigned long	paddr = rg->paddr;
	size_t		size = rg->size;

	if (rg->type > OMAPFB_MEMTYPE_MAX) {
		printk(KERN_ERR
			"Invalid start address for FB region %d\n", region_idx);
		return -EINVAL;
	}

	if (!rg->size) {
		printk(KERN_ERR "Zero size for FB region %d\n", region_idx);
		return -EINVAL;
	}

	if (!paddr)
		/* Allocate this dynamically, leave paddr 0 for now. */
		return 0;

	/*
	 * Fixed region for the given RAM range. Check if it's already
	 * reserved by the FB code or someone else.
	 */
	if (fbmem_region_reserved(paddr, size) ||
	    !range_included(paddr, size, start_avail, size_avail)) {
		printk(KERN_ERR "Trying to use reserved memory "
			"for FB region %d\n", region_idx);
		return -EINVAL;
	}

	return 0;
}

static int valid_sdram(unsigned long addr, unsigned long size)
{
	return memblock_is_region_memory(addr, size);
}

static int reserve_sdram(unsigned long addr, unsigned long size)
{
	if (memblock_is_region_reserved(addr, size))
		return -EBUSY;
	if (memblock_reserve(addr, size))
		return -ENOMEM;
	return 0;
}

/*
 * Called from map_io. We need to call to this early enough so that we
 * can reserve the fixed SDRAM regions before VM could get hold of them.
 */
void __init omapfb_reserve_sdram_memblock(void)
{
	unsigned long reserved = 0;
	int i;

	if (config_invalid)
		return;

	for (i = 0; ; i++) {
		struct omapfb_mem_region rg;

		if (get_fbmem_region(i, &rg) < 0)
			break;

		if (i == OMAPFB_PLANE_NUM) {
			pr_err("Extraneous FB mem configuration entries\n");
			config_invalid = 1;
			return;
		}

		/* Check if it's our memory type. */
		if (rg.type != OMAPFB_MEMTYPE_SDRAM)
			continue;

		/* Check if the region falls within SDRAM */
		if (rg.paddr && !valid_sdram(rg.paddr, rg.size))
			continue;

		if (rg.size == 0) {
			pr_err("Zero size for FB region %d\n", i);
			config_invalid = 1;
			return;
		}

		if (rg.paddr) {
			if (reserve_sdram(rg.paddr, rg.size)) {
				pr_err("Trying to use reserved memory for FB region %d\n",
					i);
				config_invalid = 1;
				return;
			}
			reserved += rg.size;
		}

		if (omapfb_config.mem_desc.region[i].size) {
			pr_err("FB region %d already set\n", i);
			config_invalid = 1;
			return;
		}

		omapfb_config.mem_desc.region[i] = rg;
		configured_regions++;
	}
	omapfb_config.mem_desc.region_cnt = i;
	if (reserved)
		pr_info("Reserving %lu bytes SDRAM for frame buffer\n",
			 reserved);
}

/*
 * Called at sram init time, before anything is pushed to the SRAM stack.
 * Because of the stack scheme, we will allocate everything from the
 * start of the lowest address region to the end of SRAM. This will also
 * include padding for page alignment and possible holes between regions.
 *
 * As opposed to the SDRAM case, we'll also do any dynamic allocations at
 * this point, since the driver built as a module would have problem with
 * freeing / reallocating the regions.
 */
unsigned long __init omapfb_reserve_sram(unsigned long sram_pstart,
				  unsigned long sram_vstart,
				  unsigned long sram_size,
				  unsigned long pstart_avail,
				  unsigned long size_avail)
{
	struct omapfb_mem_region	rg;
	unsigned long			pend_avail;
	unsigned long			reserved;
	int				i;

	if (config_invalid)
		return 0;

	reserved = 0;
	pend_avail = pstart_avail + size_avail;
	for (i = 0; ; i++) {
		if (get_fbmem_region(i, &rg) < 0)
			break;
		if (i == OMAPFB_PLANE_NUM) {
			printk(KERN_ERR
				"Extraneous FB mem configuration entries\n");
			config_invalid = 1;
			return 0;
		}

		/* Check if it's our memory type. */
		if (set_fbmem_region_type(&rg, OMAPFB_MEMTYPE_SRAM,
				          sram_pstart, sram_size) < 0 ||
		    (rg.type != OMAPFB_MEMTYPE_SRAM))
			continue;
		BUG_ON(omapfb_config.mem_desc.region[i].size);

		if (check_fbmem_region(i, &rg, pstart_avail, size_avail) < 0) {
			config_invalid = 1;
			return 0;
		}

		if (!rg.paddr) {
			/* Dynamic allocation */
			if ((size_avail & PAGE_MASK) < rg.size) {
				printk("Not enough SRAM for FB region %d\n",
					i);
				config_invalid = 1;
				return 0;
			}
			size_avail = (size_avail - rg.size) & PAGE_MASK;
			rg.paddr = pstart_avail + size_avail;
		}
		/* Reserve everything above the start of the region. */
		if (pend_avail - rg.paddr > reserved)
			reserved = pend_avail - rg.paddr;
		size_avail = pend_avail - reserved - pstart_avail;

		/*
		 * We have a kernel mapping for this already, so the
		 * driver won't have to make one.
		 */
		rg.vaddr = (void *)(sram_vstart + rg.paddr - sram_pstart);
		omapfb_config.mem_desc.region[i] = rg;
		configured_regions++;
	}
	omapfb_config.mem_desc.region_cnt = i;
	if (reserved)
		pr_info("Reserving %lu bytes SRAM for frame buffer\n",
			 reserved);
	return reserved;
}

void omapfb_set_ctrl_platform_data(void *data)
{
	omapfb_config.ctrl_platform_data = data;
}

static int __init omap_init_fb(void)
{
	const struct omap_lcd_config *conf;

	if (config_invalid)
		return 0;
	if (configured_regions != omapfb_config.mem_desc.region_cnt) {
		printk(KERN_ERR "Invalid FB mem configuration entries\n");
		return 0;
	}
	conf = omap_get_config(OMAP_TAG_LCD, struct omap_lcd_config);
	if (conf == NULL) {
		if (configured_regions)
			/* FB mem config, but no LCD config? */
			printk(KERN_ERR "Missing LCD configuration\n");
		return 0;
	}
	omapfb_config.lcd = *conf;

	return platform_device_register(&omap_fb_device);
}

arch_initcall(omap_init_fb);

#elif defined(CONFIG_FB_OMAP2) || defined(CONFIG_FB_OMAP2_MODULE)

static u64 omap_fb_dma_mask = ~(u32)0;
static struct omapfb_platform_data omapfb_config;

static struct platform_device omap_fb_device = {
	.name		= "omapfb",
	.id		= -1,
	.dev = {
		.dma_mask		= &omap_fb_dma_mask,
		.coherent_dma_mask	= ~(u32)0,
		.platform_data		= &omapfb_config,
	},
	.num_resources = 0,
};

void omapfb_set_platform_data(struct omapfb_platform_data *data)
{
	omapfb_config = *data;
}

static int __init omap_init_fb(void)
{
	return platform_device_register(&omap_fb_device);
}

arch_initcall(omap_init_fb);

void omapfb_reserve_sdram_memblock(void)
{
}

unsigned long __init omapfb_reserve_sram(unsigned long sram_pstart,
				  unsigned long sram_vstart,
				  unsigned long sram_size,
				  unsigned long start_avail,
				  unsigned long size_avail)
{
	return 0;
}

#else

void omapfb_set_platform_data(struct omapfb_platform_data *data)
{
}

void omapfb_reserve_sdram_memblock(void)
{
}

unsigned long __init omapfb_reserve_sram(unsigned long sram_pstart,
				  unsigned long sram_vstart,
				  unsigned long sram_size,
				  unsigned long start_avail,
				  unsigned long size_avail)
{
	return 0;
}

#endif