blob: 725df5c1569dbc39cd8230ee3ab8f63ee7533531 [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>
Russell King98864ff2010-05-22 23:59:11 +010029#include <linux/memblock.h>
Russell Kingfced80c2008-09-06 12:10:45 +010030#include <linux/io.h>
Tomi Valkeinen91773a02009-08-03 15:06:36 +030031#include <linux/omapfb.h>
Tony Lindgren0dc5e772006-04-02 17:46:26 +010032
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/hardware.h>
Tony Lindgren0dc5e772006-04-02 17:46:26 +010034#include <asm/mach/map.h>
35
Tony Lindgrence491cf2009-10-20 09:40:47 -070036#include <plat/board.h>
37#include <plat/sram.h>
Tony Lindgren0dc5e772006-04-02 17:46:26 +010038
Manjunath Kondaiah Gb0a330d2010-10-08 10:00:19 -070039#include "fb.h"
40
Tony Lindgren0dc5e772006-04-02 17:46:26 +010041#if defined(CONFIG_FB_OMAP) || defined(CONFIG_FB_OMAP_MODULE)
42
43static struct omapfb_platform_data omapfb_config;
Imre Deakb7cc6d42007-03-06 03:16:36 -080044static int config_invalid;
45static int configured_regions;
Tony Lindgren0dc5e772006-04-02 17:46:26 +010046
47static u64 omap_fb_dma_mask = ~(u32)0;
48
49static struct platform_device omap_fb_device = {
50 .name = "omapfb",
51 .id = -1,
52 .dev = {
53 .dma_mask = &omap_fb_dma_mask,
54 .coherent_dma_mask = ~(u32)0,
55 .platform_data = &omapfb_config,
56 },
57 .num_resources = 0,
58};
59
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +030060void omapfb_set_platform_data(struct omapfb_platform_data *data)
61{
62}
63
Imre Deakb7cc6d42007-03-06 03:16:36 -080064static inline int ranges_overlap(unsigned long start1, unsigned long size1,
65 unsigned long start2, unsigned long size2)
Tony Lindgren0dc5e772006-04-02 17:46:26 +010066{
Imre Deakb7cc6d42007-03-06 03:16:36 -080067 return (start1 >= start2 && start1 < start2 + size2) ||
68 (start2 >= start1 && start2 < start1 + size1);
69}
70
71static inline int range_included(unsigned long start1, unsigned long size1,
72 unsigned long start2, unsigned long size2)
73{
74 return start1 >= start2 && start1 + size1 <= start2 + size2;
75}
76
77
78/* Check if there is an overlapping region. */
79static int fbmem_region_reserved(unsigned long start, size_t size)
80{
81 struct omapfb_mem_region *rg;
Tony Lindgrenc40fae952006-12-07 13:58:10 -080082 int i;
Tony Lindgren0dc5e772006-04-02 17:46:26 +010083
Imre Deakb7cc6d42007-03-06 03:16:36 -080084 rg = &omapfb_config.mem_desc.region[0];
85 for (i = 0; i < OMAPFB_PLANE_NUM; i++, rg++) {
86 if (!rg->paddr)
87 /* Empty slot. */
88 continue;
89 if (ranges_overlap(start, size, rg->paddr, rg->size))
90 return 1;
91 }
92 return 0;
93}
94
95/*
96 * Get the region_idx`th region from board config/ATAG and convert it to
97 * our internal format.
98 */
99static int get_fbmem_region(int region_idx, struct omapfb_mem_region *rg)
100{
101 const struct omap_fbmem_config *conf;
102 u32 paddr;
103
104 conf = omap_get_nr_config(OMAP_TAG_FBMEM,
105 struct omap_fbmem_config, region_idx);
106 if (conf == NULL)
107 return -ENOENT;
108
109 paddr = conf->start;
110 /*
111 * Low bits encode the page allocation mode, if high bits
112 * are zero. Otherwise we need a page aligned fixed
113 * address.
114 */
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);
119 return 0;
120}
121
122static int set_fbmem_region_type(struct omapfb_mem_region *rg, int mem_type,
123 unsigned long mem_start,
124 unsigned long mem_size)
125{
126 /*
127 * Check if the configuration specifies the type explicitly.
128 * type = 0 && paddr = 0, a default don't care case maps to
129 * the SDRAM type.
130 */
131 if (rg->type || (!rg->type && !rg->paddr))
132 return 0;
133 if (ranges_overlap(rg->paddr, rg->size, mem_start, mem_size)) {
134 rg->type = mem_type;
135 return 0;
136 }
137 /* Can't determine it. */
138 return -1;
139}
140
141static int check_fbmem_region(int region_idx, struct omapfb_mem_region *rg,
142 unsigned long start_avail, unsigned size_avail)
143{
144 unsigned long paddr = rg->paddr;
145 size_t size = rg->size;
146
147 if (rg->type > OMAPFB_MEMTYPE_MAX) {
148 printk(KERN_ERR
149 "Invalid start address for FB region %d\n", region_idx);
150 return -EINVAL;
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100151 }
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800152
Imre Deakb7cc6d42007-03-06 03:16:36 -0800153 if (!rg->size) {
154 printk(KERN_ERR "Zero size for FB region %d\n", region_idx);
155 return -EINVAL;
156 }
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800157
Imre Deakb7cc6d42007-03-06 03:16:36 -0800158 if (!paddr)
159 /* Allocate this dynamically, leave paddr 0 for now. */
160 return 0;
161
162 /*
163 * Fixed region for the given RAM range. Check if it's already
164 * reserved by the FB code or someone else.
165 */
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);
170 return -EINVAL;
171 }
172
173 return 0;
174}
175
Russell Kinga1af0fb2010-05-22 23:53:45 +0100176static int valid_sdram(unsigned long addr, unsigned long size)
177{
Russell King98864ff2010-05-22 23:59:11 +0100178 struct memblock_property res;
Russell Kinga1af0fb2010-05-22 23:53:45 +0100179
Russell King98864ff2010-05-22 23:59:11 +0100180 res.base = addr;
181 res.size = size;
182 return !memblock_find(&res) && res.base == addr && res.size == size;
Russell Kinga1af0fb2010-05-22 23:53:45 +0100183}
184
185static int reserve_sdram(unsigned long addr, unsigned long size)
186{
Russell King98864ff2010-05-22 23:59:11 +0100187 if (memblock_is_region_reserved(addr, size))
188 return -EBUSY;
189 if (memblock_reserve(addr, size))
190 return -ENOMEM;
191 return 0;
Russell Kinga1af0fb2010-05-22 23:53:45 +0100192}
193
Imre Deakb7cc6d42007-03-06 03:16:36 -0800194/*
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.
197 */
Russell King98864ff2010-05-22 23:59:11 +0100198void __init omapfb_reserve_sdram_memblock(void)
Imre Deakb7cc6d42007-03-06 03:16:36 -0800199{
Russell Kinga1af0fb2010-05-22 23:53:45 +0100200 unsigned long reserved = 0;
201 int i;
Imre Deakb7cc6d42007-03-06 03:16:36 -0800202
203 if (config_invalid)
204 return;
205
Imre Deakb7cc6d42007-03-06 03:16:36 -0800206 for (i = 0; ; i++) {
Russell Kinga1af0fb2010-05-22 23:53:45 +0100207 struct omapfb_mem_region rg;
Imre Deakb7cc6d42007-03-06 03:16:36 -0800208
209 if (get_fbmem_region(i, &rg) < 0)
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800210 break;
Russell Kinga1af0fb2010-05-22 23:53:45 +0100211
Imre Deakb7cc6d42007-03-06 03:16:36 -0800212 if (i == OMAPFB_PLANE_NUM) {
Russell Kinga1af0fb2010-05-22 23:53:45 +0100213 pr_err("Extraneous FB mem configuration entries\n");
Imre Deakb7cc6d42007-03-06 03:16:36 -0800214 config_invalid = 1;
215 return;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800216 }
Russell Kinga1af0fb2010-05-22 23:53:45 +0100217
Imre Deakb7cc6d42007-03-06 03:16:36 -0800218 /* Check if it's our memory type. */
Russell Kinga1af0fb2010-05-22 23:53:45 +0100219 if (rg.type != OMAPFB_MEMTYPE_SDRAM)
Imre Deakb7cc6d42007-03-06 03:16:36 -0800220 continue;
Russell Kinga1af0fb2010-05-22 23:53:45 +0100221
222 /* Check if the region falls within SDRAM */
223 if (rg.paddr && !valid_sdram(rg.paddr, rg.size))
224 continue;
225
226 if (rg.size == 0) {
227 pr_err("Zero size for FB region %d\n", i);
Imre Deakb7cc6d42007-03-06 03:16:36 -0800228 config_invalid = 1;
229 return;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800230 }
Russell Kinga1af0fb2010-05-22 23:53:45 +0100231
Tomi Valkeinen72af2b32009-05-11 09:58:19 -0700232 if (rg.paddr) {
Russell Kinga1af0fb2010-05-22 23:53:45 +0100233 if (reserve_sdram(rg.paddr, rg.size)) {
234 pr_err("Trying to use reserved memory for FB region %d\n",
235 i);
236 config_invalid = 1;
237 return;
238 }
Tomi Valkeinen72af2b32009-05-11 09:58:19 -0700239 reserved += rg.size;
240 }
Russell Kinga1af0fb2010-05-22 23:53:45 +0100241
242 if (omapfb_config.mem_desc.region[i].size) {
243 pr_err("FB region %d already set\n", i);
244 config_invalid = 1;
245 return;
246 }
247
Imre Deakb7cc6d42007-03-06 03:16:36 -0800248 omapfb_config.mem_desc.region[i] = rg;
249 configured_regions++;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800250 }
251 omapfb_config.mem_desc.region_cnt = i;
Imre Deakb7cc6d42007-03-06 03:16:36 -0800252 if (reserved)
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800253 pr_info("Reserving %lu bytes SDRAM for frame buffer\n",
Imre Deakb7cc6d42007-03-06 03:16:36 -0800254 reserved);
255}
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800256
Imre Deakb7cc6d42007-03-06 03:16:36 -0800257/*
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.
262 *
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.
266 */
267unsigned 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)
272{
273 struct omapfb_mem_region rg;
274 unsigned long pend_avail;
275 unsigned long reserved;
276 int i;
277
278 if (config_invalid)
279 return 0;
280
281 reserved = 0;
282 pend_avail = pstart_avail + size_avail;
283 for (i = 0; ; i++) {
284 if (get_fbmem_region(i, &rg) < 0)
285 break;
286 if (i == OMAPFB_PLANE_NUM) {
287 printk(KERN_ERR
288 "Extraneous FB mem configuration entries\n");
289 config_invalid = 1;
290 return 0;
291 }
292
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))
297 continue;
298 BUG_ON(omapfb_config.mem_desc.region[i].size);
299
300 if (check_fbmem_region(i, &rg, pstart_avail, size_avail) < 0) {
301 config_invalid = 1;
302 return 0;
303 }
304
305 if (!rg.paddr) {
306 /* Dynamic allocation */
307 if ((size_avail & PAGE_MASK) < rg.size) {
308 printk("Not enough SRAM for FB region %d\n",
309 i);
310 config_invalid = 1;
311 return 0;
312 }
313 size_avail = (size_avail - rg.size) & PAGE_MASK;
314 rg.paddr = pstart_avail + size_avail;
315 }
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;
320
321 /*
322 * We have a kernel mapping for this already, so the
323 * driver won't have to make one.
324 */
325 rg.vaddr = (void *)(sram_vstart + rg.paddr - sram_pstart);
326 omapfb_config.mem_desc.region[i] = rg;
327 configured_regions++;
328 }
329 omapfb_config.mem_desc.region_cnt = i;
330 if (reserved)
331 pr_info("Reserving %lu bytes SRAM for frame buffer\n",
332 reserved);
333 return reserved;
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100334}
335
Imre Deak771af222006-12-06 17:13:50 -0800336void omapfb_set_ctrl_platform_data(void *data)
337{
338 omapfb_config.ctrl_platform_data = data;
339}
340
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100341static inline int omap_init_fb(void)
342{
343 const struct omap_lcd_config *conf;
344
Imre Deakb7cc6d42007-03-06 03:16:36 -0800345 if (config_invalid)
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100346 return 0;
Imre Deakb7cc6d42007-03-06 03:16:36 -0800347 if (configured_regions != omapfb_config.mem_desc.region_cnt) {
348 printk(KERN_ERR "Invalid FB mem configuration entries\n");
349 return 0;
350 }
351 conf = omap_get_config(OMAP_TAG_LCD, struct omap_lcd_config);
352 if (conf == NULL) {
353 if (configured_regions)
354 /* FB mem config, but no LCD config? */
355 printk(KERN_ERR "Missing LCD configuration\n");
356 return 0;
357 }
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100358 omapfb_config.lcd = *conf;
359
360 return platform_device_register(&omap_fb_device);
361}
362
363arch_initcall(omap_init_fb);
364
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300365#elif defined(CONFIG_FB_OMAP2) || defined(CONFIG_FB_OMAP2_MODULE)
366
367static u64 omap_fb_dma_mask = ~(u32)0;
368static struct omapfb_platform_data omapfb_config;
369
370static struct platform_device omap_fb_device = {
371 .name = "omapfb",
372 .id = -1,
373 .dev = {
374 .dma_mask = &omap_fb_dma_mask,
375 .coherent_dma_mask = ~(u32)0,
376 .platform_data = &omapfb_config,
377 },
378 .num_resources = 0,
379};
380
381void omapfb_set_platform_data(struct omapfb_platform_data *data)
382{
383 omapfb_config = *data;
384}
385
386static inline int omap_init_fb(void)
387{
388 return platform_device_register(&omap_fb_device);
389}
390
391arch_initcall(omap_init_fb);
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100392
Russell King98864ff2010-05-22 23:59:11 +0100393void omapfb_reserve_sdram_memblock(void)
394{
395}
396
Imre Deakb7cc6d42007-03-06 03:16:36 -0800397unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
398 unsigned long sram_vstart,
399 unsigned long sram_size,
400 unsigned long start_avail,
David Brownellcc150b02007-03-28 16:38:14 -0700401 unsigned long size_avail)
402{
403 return 0;
404}
Imre Deakb7cc6d42007-03-06 03:16:36 -0800405
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300406#else
407
408void omapfb_set_platform_data(struct omapfb_platform_data *data)
409{
410}
411
Russell King98864ff2010-05-22 23:59:11 +0100412void omapfb_reserve_sdram_memblock(void)
413{
414}
415
Tomi Valkeinenb39a982d2009-08-04 16:12:50 +0300416unsigned 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)
421{
422 return 0;
423}
Tony Lindgren0dc5e772006-04-02 17:46:26 +0100424
425#endif