fb.c 8.9 KB

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