blob: 5d107520e6b90348a5d168c25da557613d41d04f [file] [log] [blame]
Tony Lindgrenc40fae952006-12-07 13:58:10 -08001/*
2 * File: arch/arm/plat-omap/fb.c
3 *
4 * Framebuffer device registration for TI OMAP platforms
5 *
6 * Copyright (C) 2006 Nokia Corporation
7 * Author: Imre Deak <imre.deak@nokia.com>
8 *
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.
13 *
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.
18 *
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.
22 */
23
Tony Lindgren0dc5e772006-04-02 17:46:26 +010024#include <linux/module.h>
25#include <linux/kernel.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070026#include <linux/mm.h>
Tony Lindgren0dc5e772006-04-02 17:46:26 +010027#include <linux/init.h>
28#include <linux/platform_device.h>
29#include <linux/bootmem.h>
30
31#include <asm/hardware.h>
32#include <asm/io.h>
33#include <asm/mach-types.h>
34#include <asm/mach/map.h>
35
36#include <asm/arch/board.h>
37#include <asm/arch/sram.h>
38#include <asm/arch/omapfb.h>
39
40#if defined(CONFIG_FB_OMAP) || defined(CONFIG_FB_OMAP_MODULE)
41
42static struct omapfb_platform_data omapfb_config;
Imre Deakb7cc6d42007-03-06 03:16:36 -080043static int config_invalid;
44static int configured_regions;
Tony Lindgren0dc5e772006-04-02 17:46:26 +010045
46static u64 omap_fb_dma_mask = ~(u32)0;
47
48static struct platform_device omap_fb_device = {
49 .name = "omapfb",
50 .id = -1,
51 .dev = {
52 .dma_mask = &omap_fb_dma_mask,
53 .coherent_dma_mask = ~(u32)0,
54 .platform_data = &omapfb_config,
55 },
56 .num_resources = 0,
57};
58
Imre Deakb7cc6d42007-03-06 03:16:36 -080059static inline int ranges_overlap(unsigned long start1, unsigned long size1,
60 unsigned long start2, unsigned long size2)
Tony Lindgren0dc5e772006-04-02 17:46:26 +010061{
Imre Deakb7cc6d42007-03-06 03:16:36 -080062 return (start1 >= start2 && start1 < start2 + size2) ||
63 (start2 >= start1 && start2 < start1 + size1);
64}
65
66static inline int range_included(unsigned long start1, unsigned long size1,
67 unsigned long start2, unsigned long size2)
68{
69 return start1 >= start2 && start1 + size1 <= start2 + size2;
70}
71
72
73/* Check if there is an overlapping region. */
74static int fbmem_region_reserved(unsigned long start, size_t size)
75{
76 struct omapfb_mem_region *rg;
Tony Lindgrenc40fae952006-12-07 13:58:10 -080077 int i;
Tony Lindgren0dc5e772006-04-02 17:46:26 +010078
Imre Deakb7cc6d42007-03-06 03:16:36 -080079 rg = &omapfb_config.mem_desc.region[0];
80 for (i = 0; i < OMAPFB_PLANE_NUM; i++, rg++) {
81 if (!rg->paddr)
82 /* Empty slot. */
83 continue;
84 if (ranges_overlap(start, size, rg->paddr, rg->size))
85 return 1;
86 }
87 return 0;
88}
89
90/*
91 * Get the region_idx`th region from board config/ATAG and convert it to
92 * our internal format.
93 */
94static int get_fbmem_region(int region_idx, struct omapfb_mem_region *rg)
95{
96 const struct omap_fbmem_config *conf;
97 u32 paddr;
98
99 conf = omap_get_nr_config(OMAP_TAG_FBMEM,
100 struct omap_fbmem_config, region_idx);
101 if (conf == NULL)
102 return -ENOENT;
103
104 paddr = conf->start;
105 /*
106 * Low bits encode the page allocation mode, if high bits
107 * are zero. Otherwise we need a page aligned fixed
108 * address.
109 */
110 memset(rg, 0, sizeof(*rg));
111 rg->type = paddr & ~PAGE_MASK;
112 rg->paddr = paddr & PAGE_MASK;
113 rg->size = PAGE_ALIGN(conf->size);
114 return 0;
115}
116
117static int set_fbmem_region_type(struct omapfb_mem_region *rg, int mem_type,
118 unsigned long mem_start,
119 unsigned long mem_size)
120{
121 /*
122 * Check if the configuration specifies the type explicitly.
123 * type = 0 && paddr = 0, a default don't care case maps to
124 * the SDRAM type.
125 */
126 if (rg->type || (!rg->type && !rg->paddr))
127 return 0;
128 if (ranges_overlap(rg->paddr, rg->size, mem_start, mem_size)) {
129 rg->type = mem_type;
130 return 0;
131 }
132 /* Can't determine it. */
133 return -1;
134}
135
136static int check_fbmem_region(int region_idx, struct omapfb_mem_region *rg,
137 unsigned long start_avail, unsigned size_avail)
138{
139 unsigned long paddr = rg->paddr;
140 size_t size = rg->size;
141
142 if (rg->type > OMAPFB_MEMTYPE_MAX) {
143 printk(KERN_ERR
144 "Invalid start address for FB region %d\n", region_idx);
145 return -EINVAL;
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100146 }
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800147
Imre Deakb7cc6d42007-03-06 03:16:36 -0800148 if (!rg->size) {
149 printk(KERN_ERR "Zero size for FB region %d\n", region_idx);
150 return -EINVAL;
151 }
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800152
Imre Deakb7cc6d42007-03-06 03:16:36 -0800153 if (!paddr)
154 /* Allocate this dynamically, leave paddr 0 for now. */
155 return 0;
156
157 /*
158 * Fixed region for the given RAM range. Check if it's already
159 * reserved by the FB code or someone else.
160 */
161 if (fbmem_region_reserved(paddr, size) ||
162 !range_included(paddr, size, start_avail, size_avail)) {
163 printk(KERN_ERR "Trying to use reserved memory "
164 "for FB region %d\n", region_idx);
165 return -EINVAL;
166 }
167
168 return 0;
169}
170
171/*
172 * Called from map_io. We need to call to this early enough so that we
173 * can reserve the fixed SDRAM regions before VM could get hold of them.
174 */
David Brownell342fb3d2007-10-26 21:48:37 +0100175void __init omapfb_reserve_sdram(void)
Imre Deakb7cc6d42007-03-06 03:16:36 -0800176{
177 struct bootmem_data *bdata;
178 unsigned long sdram_start, sdram_size;
179 unsigned long reserved;
180 int i;
181
182 if (config_invalid)
183 return;
184
185 bdata = NODE_DATA(0)->bdata;
Johannes Weiner3560e242008-07-23 21:28:09 -0700186 sdram_start = bdata->node_min_pfn << PAGE_SHIFT;
Imre Deakb7cc6d42007-03-06 03:16:36 -0800187 sdram_size = (bdata->node_low_pfn << PAGE_SHIFT) - sdram_start;
188 reserved = 0;
189 for (i = 0; ; i++) {
190 struct omapfb_mem_region rg;
191
192 if (get_fbmem_region(i, &rg) < 0)
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800193 break;
Imre Deakb7cc6d42007-03-06 03:16:36 -0800194 if (i == OMAPFB_PLANE_NUM) {
195 printk(KERN_ERR
196 "Extraneous FB mem configuration entries\n");
197 config_invalid = 1;
198 return;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800199 }
Imre Deakb7cc6d42007-03-06 03:16:36 -0800200 /* Check if it's our memory type. */
201 if (set_fbmem_region_type(&rg, OMAPFB_MEMTYPE_SDRAM,
202 sdram_start, sdram_size) < 0 ||
203 (rg.type != OMAPFB_MEMTYPE_SDRAM))
204 continue;
205 BUG_ON(omapfb_config.mem_desc.region[i].size);
206 if (check_fbmem_region(i, &rg, sdram_start, sdram_size) < 0) {
207 config_invalid = 1;
208 return;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800209 }
Imre Deakb7cc6d42007-03-06 03:16:36 -0800210 if (rg.paddr)
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800211 reserve_bootmem(rg.paddr, rg.size, BOOTMEM_DEFAULT);
Imre Deakb7cc6d42007-03-06 03:16:36 -0800212 reserved += rg.size;
213 omapfb_config.mem_desc.region[i] = rg;
214 configured_regions++;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800215 }
216 omapfb_config.mem_desc.region_cnt = i;
Imre Deakb7cc6d42007-03-06 03:16:36 -0800217 if (reserved)
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800218 pr_info("Reserving %lu bytes SDRAM for frame buffer\n",
Imre Deakb7cc6d42007-03-06 03:16:36 -0800219 reserved);
220}
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800221
Imre Deakb7cc6d42007-03-06 03:16:36 -0800222/*
223 * Called at sram init time, before anything is pushed to the SRAM stack.
224 * Because of the stack scheme, we will allocate everything from the
225 * start of the lowest address region to the end of SRAM. This will also
226 * include padding for page alignment and possible holes between regions.
227 *
228 * As opposed to the SDRAM case, we'll also do any dynamic allocations at
229 * this point, since the driver built as a module would have problem with
230 * freeing / reallocating the regions.
231 */
232unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
233 unsigned long sram_vstart,
234 unsigned long sram_size,
235 unsigned long pstart_avail,
236 unsigned long size_avail)
237{
238 struct omapfb_mem_region rg;
239 unsigned long pend_avail;
240 unsigned long reserved;
241 int i;
242
243 if (config_invalid)
244 return 0;
245
246 reserved = 0;
247 pend_avail = pstart_avail + size_avail;
248 for (i = 0; ; i++) {
249 if (get_fbmem_region(i, &rg) < 0)
250 break;
251 if (i == OMAPFB_PLANE_NUM) {
252 printk(KERN_ERR
253 "Extraneous FB mem configuration entries\n");
254 config_invalid = 1;
255 return 0;
256 }
257
258 /* Check if it's our memory type. */
259 if (set_fbmem_region_type(&rg, OMAPFB_MEMTYPE_SRAM,
260 sram_pstart, sram_size) < 0 ||
261 (rg.type != OMAPFB_MEMTYPE_SRAM))
262 continue;
263 BUG_ON(omapfb_config.mem_desc.region[i].size);
264
265 if (check_fbmem_region(i, &rg, pstart_avail, size_avail) < 0) {
266 config_invalid = 1;
267 return 0;
268 }
269
270 if (!rg.paddr) {
271 /* Dynamic allocation */
272 if ((size_avail & PAGE_MASK) < rg.size) {
273 printk("Not enough SRAM for FB region %d\n",
274 i);
275 config_invalid = 1;
276 return 0;
277 }
278 size_avail = (size_avail - rg.size) & PAGE_MASK;
279 rg.paddr = pstart_avail + size_avail;
280 }
281 /* Reserve everything above the start of the region. */
282 if (pend_avail - rg.paddr > reserved)
283 reserved = pend_avail - rg.paddr;
284 size_avail = pend_avail - reserved - pstart_avail;
285
286 /*
287 * We have a kernel mapping for this already, so the
288 * driver won't have to make one.
289 */
290 rg.vaddr = (void *)(sram_vstart + rg.paddr - sram_pstart);
291 omapfb_config.mem_desc.region[i] = rg;
292 configured_regions++;
293 }
294 omapfb_config.mem_desc.region_cnt = i;
295 if (reserved)
296 pr_info("Reserving %lu bytes SRAM for frame buffer\n",
297 reserved);
298 return reserved;
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100299}
300
Imre Deak771af222006-12-06 17:13:50 -0800301void omapfb_set_ctrl_platform_data(void *data)
302{
303 omapfb_config.ctrl_platform_data = data;
304}
305
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100306static inline int omap_init_fb(void)
307{
308 const struct omap_lcd_config *conf;
309
Imre Deakb7cc6d42007-03-06 03:16:36 -0800310 if (config_invalid)
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100311 return 0;
Imre Deakb7cc6d42007-03-06 03:16:36 -0800312 if (configured_regions != omapfb_config.mem_desc.region_cnt) {
313 printk(KERN_ERR "Invalid FB mem configuration entries\n");
314 return 0;
315 }
316 conf = omap_get_config(OMAP_TAG_LCD, struct omap_lcd_config);
317 if (conf == NULL) {
318 if (configured_regions)
319 /* FB mem config, but no LCD config? */
320 printk(KERN_ERR "Missing LCD configuration\n");
321 return 0;
322 }
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100323 omapfb_config.lcd = *conf;
324
325 return platform_device_register(&omap_fb_device);
326}
327
328arch_initcall(omap_init_fb);
329
330#else
331
Imre Deakb7cc6d42007-03-06 03:16:36 -0800332void omapfb_reserve_sdram(void) {}
333unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
334 unsigned long sram_vstart,
335 unsigned long sram_size,
336 unsigned long start_avail,
David Brownellcc150b02007-03-28 16:38:14 -0700337 unsigned long size_avail)
338{
339 return 0;
340}
Imre Deakb7cc6d42007-03-06 03:16:36 -0800341
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100342
343#endif