2 * File: arch/arm/plat-omap/fb.c
4 * Framebuffer device registration for TI OMAP platforms
6 * Copyright (C) 2006 Nokia Corporation
7 * Author: Imre Deak <imre.deak@nokia.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/module.h>
25 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/platform_device.h>
29 #include <linux/memblock.h>
31 #include <linux/omapfb.h>
33 #include <mach/hardware.h>
34 #include <asm/mach/map.h>
36 #include <plat/board.h>
37 #include <plat/sram.h>
41 #if defined(CONFIG_FB_OMAP) || defined(CONFIG_FB_OMAP_MODULE)
43 static struct omapfb_platform_data omapfb_config;
44 static int config_invalid;
45 static int configured_regions;
47 static u64 omap_fb_dma_mask = ~(u32)0;
49 static struct platform_device omap_fb_device = {
53 .dma_mask = &omap_fb_dma_mask,
54 .coherent_dma_mask = ~(u32)0,
55 .platform_data = &omapfb_config,
60 void omapfb_set_platform_data(struct omapfb_platform_data *data)
64 static inline int ranges_overlap(unsigned long start1, unsigned long size1,
65 unsigned long start2, unsigned long size2)
67 return (start1 >= start2 && start1 < start2 + size2) ||
68 (start2 >= start1 && start2 < start1 + size1);
71 static inline int range_included(unsigned long start1, unsigned long size1,
72 unsigned long start2, unsigned long size2)
74 return start1 >= start2 && start1 + size1 <= start2 + size2;
78 /* Check if there is an overlapping region. */
79 static int fbmem_region_reserved(unsigned long start, size_t size)
81 struct omapfb_mem_region *rg;
84 rg = &omapfb_config.mem_desc.region[0];
85 for (i = 0; i < OMAPFB_PLANE_NUM; i++, rg++) {
89 if (ranges_overlap(start, size, rg->paddr, rg->size))
96 * Get the region_idx`th region from board config/ATAG and convert it to
97 * our internal format.
99 static int get_fbmem_region(int region_idx, struct omapfb_mem_region *rg)
101 const struct omap_fbmem_config *conf;
104 conf = omap_get_nr_config(OMAP_TAG_FBMEM,
105 struct omap_fbmem_config, region_idx);
111 * Low bits encode the page allocation mode, if high bits
112 * are zero. Otherwise we need a page aligned fixed
115 memset(rg, 0, sizeof(*rg));
116 rg->type = paddr & ~PAGE_MASK;
117 rg->paddr = paddr & PAGE_MASK;
118 rg->size = PAGE_ALIGN(conf->size);
122 static int set_fbmem_region_type(struct omapfb_mem_region *rg, int mem_type,
123 unsigned long mem_start,
124 unsigned long mem_size)
127 * Check if the configuration specifies the type explicitly.
128 * type = 0 && paddr = 0, a default don't care case maps to
131 if (rg->type || (!rg->type && !rg->paddr))
133 if (ranges_overlap(rg->paddr, rg->size, mem_start, mem_size)) {
137 /* Can't determine it. */
141 static int check_fbmem_region(int region_idx, struct omapfb_mem_region *rg,
142 unsigned long start_avail, unsigned size_avail)
144 unsigned long paddr = rg->paddr;
145 size_t size = rg->size;
147 if (rg->type > OMAPFB_MEMTYPE_MAX) {
149 "Invalid start address for FB region %d\n", region_idx);
154 printk(KERN_ERR "Zero size for FB region %d\n", region_idx);
159 /* Allocate this dynamically, leave paddr 0 for now. */
163 * Fixed region for the given RAM range. Check if it's already
164 * reserved by the FB code or someone else.
166 if (fbmem_region_reserved(paddr, size) ||
167 !range_included(paddr, size, start_avail, size_avail)) {
168 printk(KERN_ERR "Trying to use reserved memory "
169 "for FB region %d\n", region_idx);
176 static int valid_sdram(unsigned long addr, unsigned long size)
178 struct memblock_property res;
182 return !memblock_find(&res) && res.base == addr && res.size == size;
185 static int reserve_sdram(unsigned long addr, unsigned long size)
187 if (memblock_is_region_reserved(addr, size))
189 if (memblock_reserve(addr, size))
195 * Called from map_io. We need to call to this early enough so that we
196 * can reserve the fixed SDRAM regions before VM could get hold of them.
198 void __init omapfb_reserve_sdram_memblock(void)
200 unsigned long reserved = 0;
207 struct omapfb_mem_region rg;
209 if (get_fbmem_region(i, &rg) < 0)
212 if (i == OMAPFB_PLANE_NUM) {
213 pr_err("Extraneous FB mem configuration entries\n");
218 /* Check if it's our memory type. */
219 if (rg.type != OMAPFB_MEMTYPE_SDRAM)
222 /* Check if the region falls within SDRAM */
223 if (rg.paddr && !valid_sdram(rg.paddr, rg.size))
227 pr_err("Zero size for FB region %d\n", i);
233 if (reserve_sdram(rg.paddr, rg.size)) {
234 pr_err("Trying to use reserved memory for FB region %d\n",
242 if (omapfb_config.mem_desc.region[i].size) {
243 pr_err("FB region %d already set\n", i);
248 omapfb_config.mem_desc.region[i] = rg;
249 configured_regions++;
251 omapfb_config.mem_desc.region_cnt = i;
253 pr_info("Reserving %lu bytes SDRAM for frame buffer\n",
258 * Called at sram init time, before anything is pushed to the SRAM stack.
259 * Because of the stack scheme, we will allocate everything from the
260 * start of the lowest address region to the end of SRAM. This will also
261 * include padding for page alignment and possible holes between regions.
263 * As opposed to the SDRAM case, we'll also do any dynamic allocations at
264 * this point, since the driver built as a module would have problem with
265 * freeing / reallocating the regions.
267 unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
268 unsigned long sram_vstart,
269 unsigned long sram_size,
270 unsigned long pstart_avail,
271 unsigned long size_avail)
273 struct omapfb_mem_region rg;
274 unsigned long pend_avail;
275 unsigned long reserved;
282 pend_avail = pstart_avail + size_avail;
284 if (get_fbmem_region(i, &rg) < 0)
286 if (i == OMAPFB_PLANE_NUM) {
288 "Extraneous FB mem configuration entries\n");
293 /* Check if it's our memory type. */
294 if (set_fbmem_region_type(&rg, OMAPFB_MEMTYPE_SRAM,
295 sram_pstart, sram_size) < 0 ||
296 (rg.type != OMAPFB_MEMTYPE_SRAM))
298 BUG_ON(omapfb_config.mem_desc.region[i].size);
300 if (check_fbmem_region(i, &rg, pstart_avail, size_avail) < 0) {
306 /* Dynamic allocation */
307 if ((size_avail & PAGE_MASK) < rg.size) {
308 printk("Not enough SRAM for FB region %d\n",
313 size_avail = (size_avail - rg.size) & PAGE_MASK;
314 rg.paddr = pstart_avail + size_avail;
316 /* Reserve everything above the start of the region. */
317 if (pend_avail - rg.paddr > reserved)
318 reserved = pend_avail - rg.paddr;
319 size_avail = pend_avail - reserved - pstart_avail;
322 * We have a kernel mapping for this already, so the
323 * driver won't have to make one.
325 rg.vaddr = (void *)(sram_vstart + rg.paddr - sram_pstart);
326 omapfb_config.mem_desc.region[i] = rg;
327 configured_regions++;
329 omapfb_config.mem_desc.region_cnt = i;
331 pr_info("Reserving %lu bytes SRAM for frame buffer\n",
336 void omapfb_set_ctrl_platform_data(void *data)
338 omapfb_config.ctrl_platform_data = data;
341 static inline int omap_init_fb(void)
343 const struct omap_lcd_config *conf;
347 if (configured_regions != omapfb_config.mem_desc.region_cnt) {
348 printk(KERN_ERR "Invalid FB mem configuration entries\n");
351 conf = omap_get_config(OMAP_TAG_LCD, struct omap_lcd_config);
353 if (configured_regions)
354 /* FB mem config, but no LCD config? */
355 printk(KERN_ERR "Missing LCD configuration\n");
358 omapfb_config.lcd = *conf;
360 return platform_device_register(&omap_fb_device);
363 arch_initcall(omap_init_fb);
365 #elif defined(CONFIG_FB_OMAP2) || defined(CONFIG_FB_OMAP2_MODULE)
367 static u64 omap_fb_dma_mask = ~(u32)0;
368 static struct omapfb_platform_data omapfb_config;
370 static struct platform_device omap_fb_device = {
374 .dma_mask = &omap_fb_dma_mask,
375 .coherent_dma_mask = ~(u32)0,
376 .platform_data = &omapfb_config,
381 void omapfb_set_platform_data(struct omapfb_platform_data *data)
383 omapfb_config = *data;
386 static inline int omap_init_fb(void)
388 return platform_device_register(&omap_fb_device);
391 arch_initcall(omap_init_fb);
393 void omapfb_reserve_sdram_memblock(void)
397 unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
398 unsigned long sram_vstart,
399 unsigned long sram_size,
400 unsigned long start_avail,
401 unsigned long size_avail)
408 void omapfb_set_platform_data(struct omapfb_platform_data *data)
412 void omapfb_reserve_sdram_memblock(void)
416 unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
417 unsigned long sram_vstart,
418 unsigned long sram_size,
419 unsigned long start_avail,
420 unsigned long size_avail)