]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/plat-omap/fb.c
ARM: OMAP: FB sync with N800 tree (support for dynamic SRAM allocations)
[linux-2.6-omap-h63xx.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/init.h>
27 #include <linux/platform_device.h>
28 #include <linux/bootmem.h>
29
30 #include <asm/hardware.h>
31 #include <asm/io.h>
32 #include <asm/mach-types.h>
33 #include <asm/mach/map.h>
34
35 #include <asm/arch/board.h>
36 #include <asm/arch/sram.h>
37 #include <asm/arch/omapfb.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 static inline int ranges_overlap(unsigned long start1, unsigned long size1,
59                                  unsigned long start2, unsigned long size2)
60 {
61         return (start1 >= start2 && start1 < start2 + size2) ||
62                (start2 >= start1 && start2 < start1 + size1);
63 }
64
65 static inline int range_included(unsigned long start1, unsigned long size1,
66                                  unsigned long start2, unsigned long size2)
67 {
68         return start1 >= start2 && start1 + size1 <= start2 + size2;
69 }
70
71
72 /* Check if there is an overlapping region. */
73 static int fbmem_region_reserved(unsigned long start, size_t size)
74 {
75         struct omapfb_mem_region *rg;
76         int i;
77
78         rg = &omapfb_config.mem_desc.region[0];
79         for (i = 0; i < OMAPFB_PLANE_NUM; i++, rg++) {
80                 if (!rg->paddr)
81                         /* Empty slot. */
82                         continue;
83                 if (ranges_overlap(start, size, rg->paddr, rg->size))
84                         return 1;
85         }
86         return 0;
87 }
88
89 /* Get the region_idx`th region from board config/ATAG and convert it to
90  * our internal format.
91  */
92 static int get_fbmem_region(int region_idx, struct omapfb_mem_region *rg)
93 {
94         const struct omap_fbmem_config  *conf;
95         u32                             paddr;
96
97         conf = omap_get_nr_config(OMAP_TAG_FBMEM,
98                                   struct omap_fbmem_config, region_idx);
99         if (conf == NULL)
100                 return -ENOENT;
101
102         paddr = conf->start;
103         /* Low bits encode the page allocation mode, if high bits
104          * are zero. Otherwise we need a page aligned fixed
105          * address.
106          */
107         memset(rg, 0, sizeof(*rg));
108         rg->type = paddr & ~PAGE_MASK;
109         rg->paddr = paddr & PAGE_MASK;
110         rg->size = PAGE_ALIGN(conf->size);
111         return 0;
112 }
113
114 static int set_fbmem_region_type(struct omapfb_mem_region *rg, int mem_type,
115                                   unsigned long mem_start,
116                                   unsigned long mem_size)
117 {
118         /* Check if the configuration specifies the type explicitly.
119          * type = 0 && paddr = 0, a default don't care case maps to
120          * the SDRAM type.
121          */
122         if (rg->type || (!rg->type && !rg->paddr))
123                 return 0;
124         if (ranges_overlap(rg->paddr, rg->size, mem_start, mem_size)) {
125                 rg->type = mem_type;
126                 return 0;
127         }
128         /* Can't determine it. */
129         return -1;
130 }
131
132 static int check_fbmem_region(int region_idx, struct omapfb_mem_region *rg,
133                               unsigned long start_avail, unsigned size_avail)
134 {
135         unsigned long   paddr = rg->paddr;
136         size_t          size = rg->size;
137
138         if (rg->type > OMAPFB_MEMTYPE_MAX) {
139                 printk(KERN_ERR
140                         "Invalid start address for FB region %d\n", region_idx);
141                 return -EINVAL;
142         }
143
144         if (!rg->size) {
145                 printk(KERN_ERR "Zero size for FB region %d\n", region_idx);
146                 return -EINVAL;
147         }
148
149         if (!paddr)
150                 /* Allocate this dynamically, leave paddr 0 for now. */
151                 return 0;
152
153         /* Fixed region for the given RAM range. Check if it's already
154          * reserved by the FB code or someone else.
155          */
156         if (fbmem_region_reserved(paddr, size) ||
157             !range_included(paddr, size, start_avail, size_avail)) {
158                 printk(KERN_ERR "Trying to use reserved memory "
159                         "for FB region %d\n", region_idx);
160                 return -EINVAL;
161         }
162
163         return 0;
164 }
165
166 /* Called from map_io. We need to call to this early enough so that we
167  * can reserve the fixed SDRAM regions before VM could get hold of them.
168  */
169 void omapfb_reserve_sdram(void)
170 {
171         struct bootmem_data     *bdata;
172         unsigned long           sdram_start, sdram_size;
173         unsigned long           reserved;
174         int                     i;
175
176         if (config_invalid)
177                 return;
178
179         bdata = NODE_DATA(0)->bdata;
180         sdram_start = bdata->node_boot_start;
181         sdram_size = (bdata->node_low_pfn << PAGE_SHIFT) - sdram_start;
182         reserved = 0;
183         for (i = 0; ; i++) {
184                 struct omapfb_mem_region        rg;
185
186                 if (get_fbmem_region(i, &rg) < 0)
187                         break;
188                 if (i == OMAPFB_PLANE_NUM) {
189                         printk(KERN_ERR
190                                 "Extraneous FB mem configuration entries\n");
191                         config_invalid = 1;
192                         return;
193                 }
194                 /* Check if it's our memory type. */
195                 if (set_fbmem_region_type(&rg, OMAPFB_MEMTYPE_SDRAM,
196                                           sdram_start, sdram_size) < 0 ||
197                     (rg.type != OMAPFB_MEMTYPE_SDRAM))
198                         continue;
199                 BUG_ON(omapfb_config.mem_desc.region[i].size);
200                 if (check_fbmem_region(i, &rg, sdram_start, sdram_size) < 0) {
201                         config_invalid = 1;
202                         return;
203                 }
204                 if (rg.paddr)
205                         reserve_bootmem(rg.paddr, rg.size);
206                 reserved += rg.size;
207                 omapfb_config.mem_desc.region[i] = rg;
208                 configured_regions++;
209         }
210         omapfb_config.mem_desc.region_cnt = i;
211         if (reserved)
212                 pr_info("Reserving %lu bytes SDRAM for frame buffer\n",
213                          reserved);
214 }
215
216 /* Called at sram init time, before anything is pushed to the SRAM stack.
217  * Because of the stack scheme, we will allocate everything from the
218  * start of the lowest address region to the end of SRAM. This will also
219  * include padding for page alignment and possible holes between regions.
220  *
221  * As opposed to the SDRAM case, we'll also do any dynamic allocations at
222  * this point, since the driver built as a module would have problem with
223  * freeing / reallocating the regions.
224  */
225 unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
226                                   unsigned long sram_vstart,
227                                   unsigned long sram_size,
228                                   unsigned long pstart_avail,
229                                   unsigned long size_avail)
230 {
231         struct omapfb_mem_region        rg;
232         unsigned long                   pend_avail;
233         unsigned long                   reserved;
234         int                             i;
235
236         if (config_invalid)
237                 return 0;
238
239         reserved = 0;
240         pend_avail = pstart_avail + size_avail;
241         for (i = 0; ; i++) {
242                 if (get_fbmem_region(i, &rg) < 0)
243                         break;
244                 if (i == OMAPFB_PLANE_NUM) {
245                         printk(KERN_ERR
246                                 "Extraneous FB mem configuration entries\n");
247                         config_invalid = 1;
248                         return 0;
249                 }
250
251                 /* Check if it's our memory type. */
252                 if (set_fbmem_region_type(&rg, OMAPFB_MEMTYPE_SRAM,
253                                           sram_pstart, sram_size) < 0 ||
254                     (rg.type != OMAPFB_MEMTYPE_SRAM))
255                         continue;
256                 BUG_ON(omapfb_config.mem_desc.region[i].size);
257
258                 if (check_fbmem_region(i, &rg, pstart_avail, size_avail) < 0) {
259                         config_invalid = 1;
260                         return 0;
261                 }
262
263                 if (!rg.paddr) {
264                         /* Dynamic allocation */
265                         if ((size_avail & PAGE_MASK) < rg.size) {
266                                 printk("Not enough SRAM for FB region %d\n",
267                                         i);
268                                 config_invalid = 1;
269                                 return 0;
270                         }
271                         size_avail = (size_avail - rg.size) & PAGE_MASK;
272                         rg.paddr = pstart_avail + size_avail;
273                 }
274                 /* Reserve everything above the start of the region.
275                  */
276                 if (pend_avail - rg.paddr > reserved)
277                         reserved = pend_avail - rg.paddr;
278                 size_avail = pend_avail - reserved - pstart_avail;
279
280                 /* We have a kernel mapping for this already, so the
281                  * driver won't have to make one.
282                  */
283                 rg.vaddr = (void *)(sram_vstart + rg.paddr - sram_pstart);
284                 omapfb_config.mem_desc.region[i] = rg;
285                 configured_regions++;
286         }
287         omapfb_config.mem_desc.region_cnt = i;
288         if (reserved)
289                 pr_info("Reserving %lu bytes SRAM for frame buffer\n",
290                          reserved);
291         return reserved;
292 }
293
294 void omapfb_set_ctrl_platform_data(void *data)
295 {
296         omapfb_config.ctrl_platform_data = data;
297 }
298
299 static inline int omap_init_fb(void)
300 {
301         const struct omap_lcd_config *conf;
302
303         if (config_invalid)
304                 return 0;
305         if (configured_regions != omapfb_config.mem_desc.region_cnt) {
306                 printk(KERN_ERR "Invalid FB mem configuration entries\n");
307                 return 0;
308         }
309         conf = omap_get_config(OMAP_TAG_LCD, struct omap_lcd_config);
310         if (conf == NULL) {
311                 if (configured_regions)
312                         /* FB mem config, but no LCD config? */
313                         printk(KERN_ERR "Missing LCD configuration\n");
314                 return 0;
315         }
316         omapfb_config.lcd = *conf;
317
318         return platform_device_register(&omap_fb_device);
319 }
320
321 arch_initcall(omap_init_fb);
322
323 #else
324
325 void omapfb_reserve_sdram(void) {}
326 unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
327                                   unsigned long sram_vstart,
328                                   unsigned long sram_size,
329                                   unsigned long start_avail,
330                                   unsigned long size_avail) {}
331
332
333 #endif
334
335