Merge branches 'upstream' and 'upstream-fixes' into for-linus
[pandora-kernel.git] / arch / arm / plat-omap / fb.c
1 /*
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
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/mm.h>
27 #include <linux/init.h>
28 #include <linux/platform_device.h>
29 #include <linux/memblock.h>
30 #include <linux/io.h>
31 #include <linux/omapfb.h>
32
33 #include <mach/hardware.h>
34 #include <asm/mach/map.h>
35
36 #include <plat/board.h>
37 #include <plat/sram.h>
38
39 #if defined(CONFIG_FB_OMAP) || defined(CONFIG_FB_OMAP_MODULE)
40
41 static struct omapfb_platform_data omapfb_config;
42 static int config_invalid;
43 static int configured_regions;
44
45 static u64 omap_fb_dma_mask = ~(u32)0;
46
47 static struct platform_device omap_fb_device = {
48         .name           = "omapfb",
49         .id             = -1,
50         .dev = {
51                 .dma_mask               = &omap_fb_dma_mask,
52                 .coherent_dma_mask      = ~(u32)0,
53                 .platform_data          = &omapfb_config,
54         },
55         .num_resources = 0,
56 };
57
58 void omapfb_set_platform_data(struct omapfb_platform_data *data)
59 {
60 }
61
62 static inline int ranges_overlap(unsigned long start1, unsigned long size1,
63                                  unsigned long start2, unsigned long size2)
64 {
65         return (start1 >= start2 && start1 < start2 + size2) ||
66                (start2 >= start1 && start2 < start1 + size1);
67 }
68
69 static inline int range_included(unsigned long start1, unsigned long size1,
70                                  unsigned long start2, unsigned long size2)
71 {
72         return start1 >= start2 && start1 + size1 <= start2 + size2;
73 }
74
75
76 /* Check if there is an overlapping region. */
77 static int fbmem_region_reserved(unsigned long start, size_t size)
78 {
79         struct omapfb_mem_region *rg;
80         int i;
81
82         rg = &omapfb_config.mem_desc.region[0];
83         for (i = 0; i < OMAPFB_PLANE_NUM; i++, rg++) {
84                 if (!rg->paddr)
85                         /* Empty slot. */
86                         continue;
87                 if (ranges_overlap(start, size, rg->paddr, rg->size))
88                         return 1;
89         }
90         return 0;
91 }
92
93 /*
94  * Get the region_idx`th region from board config/ATAG and convert it to
95  * our internal format.
96  */
97 static int get_fbmem_region(int region_idx, struct omapfb_mem_region *rg)
98 {
99         const struct omap_fbmem_config  *conf;
100         u32                             paddr;
101
102         conf = omap_get_nr_config(OMAP_TAG_FBMEM,
103                                   struct omap_fbmem_config, region_idx);
104         if (conf == NULL)
105                 return -ENOENT;
106
107         paddr = conf->start;
108         /*
109          * Low bits encode the page allocation mode, if high bits
110          * are zero. Otherwise we need a page aligned fixed
111          * address.
112          */
113         memset(rg, 0, sizeof(*rg));
114         rg->type = paddr & ~PAGE_MASK;
115         rg->paddr = paddr & PAGE_MASK;
116         rg->size = PAGE_ALIGN(conf->size);
117         return 0;
118 }
119
120 static int set_fbmem_region_type(struct omapfb_mem_region *rg, int mem_type,
121                                   unsigned long mem_start,
122                                   unsigned long mem_size)
123 {
124         /*
125          * Check if the configuration specifies the type explicitly.
126          * type = 0 && paddr = 0, a default don't care case maps to
127          * the SDRAM type.
128          */
129         if (rg->type || (!rg->type && !rg->paddr))
130                 return 0;
131         if (ranges_overlap(rg->paddr, rg->size, mem_start, mem_size)) {
132                 rg->type = mem_type;
133                 return 0;
134         }
135         /* Can't determine it. */
136         return -1;
137 }
138
139 static int check_fbmem_region(int region_idx, struct omapfb_mem_region *rg,
140                               unsigned long start_avail, unsigned size_avail)
141 {
142         unsigned long   paddr = rg->paddr;
143         size_t          size = rg->size;
144
145         if (rg->type > OMAPFB_MEMTYPE_MAX) {
146                 printk(KERN_ERR
147                         "Invalid start address for FB region %d\n", region_idx);
148                 return -EINVAL;
149         }
150
151         if (!rg->size) {
152                 printk(KERN_ERR "Zero size for FB region %d\n", region_idx);
153                 return -EINVAL;
154         }
155
156         if (!paddr)
157                 /* Allocate this dynamically, leave paddr 0 for now. */
158                 return 0;
159
160         /*
161          * Fixed region for the given RAM range. Check if it's already
162          * reserved by the FB code or someone else.
163          */
164         if (fbmem_region_reserved(paddr, size) ||
165             !range_included(paddr, size, start_avail, size_avail)) {
166                 printk(KERN_ERR "Trying to use reserved memory "
167                         "for FB region %d\n", region_idx);
168                 return -EINVAL;
169         }
170
171         return 0;
172 }
173
174 static int valid_sdram(unsigned long addr, unsigned long size)
175 {
176         return memblock_is_region_memory(addr, size);
177 }
178
179 static int reserve_sdram(unsigned long addr, unsigned long size)
180 {
181         if (memblock_is_region_reserved(addr, size))
182                 return -EBUSY;
183         if (memblock_reserve(addr, size))
184                 return -ENOMEM;
185         return 0;
186 }
187
188 /*
189  * Called from map_io. We need to call to this early enough so that we
190  * can reserve the fixed SDRAM regions before VM could get hold of them.
191  */
192 void __init omapfb_reserve_sdram_memblock(void)
193 {
194         unsigned long reserved = 0;
195         int i;
196
197         if (config_invalid)
198                 return;
199
200         for (i = 0; ; i++) {
201                 struct omapfb_mem_region rg;
202
203                 if (get_fbmem_region(i, &rg) < 0)
204                         break;
205
206                 if (i == OMAPFB_PLANE_NUM) {
207                         pr_err("Extraneous FB mem configuration entries\n");
208                         config_invalid = 1;
209                         return;
210                 }
211
212                 /* Check if it's our memory type. */
213                 if (rg.type != OMAPFB_MEMTYPE_SDRAM)
214                         continue;
215
216                 /* Check if the region falls within SDRAM */
217                 if (rg.paddr && !valid_sdram(rg.paddr, rg.size))
218                         continue;
219
220                 if (rg.size == 0) {
221                         pr_err("Zero size for FB region %d\n", i);
222                         config_invalid = 1;
223                         return;
224                 }
225
226                 if (rg.paddr) {
227                         if (reserve_sdram(rg.paddr, rg.size)) {
228                                 pr_err("Trying to use reserved memory for FB region %d\n",
229                                         i);
230                                 config_invalid = 1;
231                                 return;
232                         }
233                         reserved += rg.size;
234                 }
235
236                 if (omapfb_config.mem_desc.region[i].size) {
237                         pr_err("FB region %d already set\n", i);
238                         config_invalid = 1;
239                         return;
240                 }
241
242                 omapfb_config.mem_desc.region[i] = rg;
243                 configured_regions++;
244         }
245         omapfb_config.mem_desc.region_cnt = i;
246         if (reserved)
247                 pr_info("Reserving %lu bytes SDRAM for frame buffer\n",
248                          reserved);
249 }
250
251 /*
252  * Called at sram init time, before anything is pushed to the SRAM stack.
253  * Because of the stack scheme, we will allocate everything from the
254  * start of the lowest address region to the end of SRAM. This will also
255  * include padding for page alignment and possible holes between regions.
256  *
257  * As opposed to the SDRAM case, we'll also do any dynamic allocations at
258  * this point, since the driver built as a module would have problem with
259  * freeing / reallocating the regions.
260  */
261 unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
262                                   unsigned long sram_vstart,
263                                   unsigned long sram_size,
264                                   unsigned long pstart_avail,
265                                   unsigned long size_avail)
266 {
267         struct omapfb_mem_region        rg;
268         unsigned long                   pend_avail;
269         unsigned long                   reserved;
270         int                             i;
271
272         if (config_invalid)
273                 return 0;
274
275         reserved = 0;
276         pend_avail = pstart_avail + size_avail;
277         for (i = 0; ; i++) {
278                 if (get_fbmem_region(i, &rg) < 0)
279                         break;
280                 if (i == OMAPFB_PLANE_NUM) {
281                         printk(KERN_ERR
282                                 "Extraneous FB mem configuration entries\n");
283                         config_invalid = 1;
284                         return 0;
285                 }
286
287                 /* Check if it's our memory type. */
288                 if (set_fbmem_region_type(&rg, OMAPFB_MEMTYPE_SRAM,
289                                           sram_pstart, sram_size) < 0 ||
290                     (rg.type != OMAPFB_MEMTYPE_SRAM))
291                         continue;
292                 BUG_ON(omapfb_config.mem_desc.region[i].size);
293
294                 if (check_fbmem_region(i, &rg, pstart_avail, size_avail) < 0) {
295                         config_invalid = 1;
296                         return 0;
297                 }
298
299                 if (!rg.paddr) {
300                         /* Dynamic allocation */
301                         if ((size_avail & PAGE_MASK) < rg.size) {
302                                 printk("Not enough SRAM for FB region %d\n",
303                                         i);
304                                 config_invalid = 1;
305                                 return 0;
306                         }
307                         size_avail = (size_avail - rg.size) & PAGE_MASK;
308                         rg.paddr = pstart_avail + size_avail;
309                 }
310                 /* Reserve everything above the start of the region. */
311                 if (pend_avail - rg.paddr > reserved)
312                         reserved = pend_avail - rg.paddr;
313                 size_avail = pend_avail - reserved - pstart_avail;
314
315                 /*
316                  * We have a kernel mapping for this already, so the
317                  * driver won't have to make one.
318                  */
319                 rg.vaddr = (void *)(sram_vstart + rg.paddr - sram_pstart);
320                 omapfb_config.mem_desc.region[i] = rg;
321                 configured_regions++;
322         }
323         omapfb_config.mem_desc.region_cnt = i;
324         if (reserved)
325                 pr_info("Reserving %lu bytes SRAM for frame buffer\n",
326                          reserved);
327         return reserved;
328 }
329
330 void omapfb_set_ctrl_platform_data(void *data)
331 {
332         omapfb_config.ctrl_platform_data = data;
333 }
334
335 static inline int omap_init_fb(void)
336 {
337         const struct omap_lcd_config *conf;
338
339         if (config_invalid)
340                 return 0;
341         if (configured_regions != omapfb_config.mem_desc.region_cnt) {
342                 printk(KERN_ERR "Invalid FB mem configuration entries\n");
343                 return 0;
344         }
345         conf = omap_get_config(OMAP_TAG_LCD, struct omap_lcd_config);
346         if (conf == NULL) {
347                 if (configured_regions)
348                         /* FB mem config, but no LCD config? */
349                         printk(KERN_ERR "Missing LCD configuration\n");
350                 return 0;
351         }
352         omapfb_config.lcd = *conf;
353
354         return platform_device_register(&omap_fb_device);
355 }
356
357 arch_initcall(omap_init_fb);
358
359 #elif defined(CONFIG_FB_OMAP2) || defined(CONFIG_FB_OMAP2_MODULE)
360
361 static u64 omap_fb_dma_mask = ~(u32)0;
362 static struct omapfb_platform_data omapfb_config;
363
364 static struct platform_device omap_fb_device = {
365         .name           = "omapfb",
366         .id             = -1,
367         .dev = {
368                 .dma_mask               = &omap_fb_dma_mask,
369                 .coherent_dma_mask      = ~(u32)0,
370                 .platform_data          = &omapfb_config,
371         },
372         .num_resources = 0,
373 };
374
375 void omapfb_set_platform_data(struct omapfb_platform_data *data)
376 {
377         omapfb_config = *data;
378 }
379
380 static inline int omap_init_fb(void)
381 {
382         return platform_device_register(&omap_fb_device);
383 }
384
385 arch_initcall(omap_init_fb);
386
387 void omapfb_reserve_sdram_memblock(void)
388 {
389 }
390
391 unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
392                                   unsigned long sram_vstart,
393                                   unsigned long sram_size,
394                                   unsigned long start_avail,
395                                   unsigned long size_avail)
396 {
397         return 0;
398 }
399
400 #else
401
402 void omapfb_set_platform_data(struct omapfb_platform_data *data)
403 {
404 }
405
406 void omapfb_reserve_sdram_memblock(void)
407 {
408 }
409
410 unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
411                                   unsigned long sram_vstart,
412                                   unsigned long sram_size,
413                                   unsigned long start_avail,
414                                   unsigned long size_avail)
415 {
416         return 0;
417 }
418
419 #endif