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 <asm/hardware.h>
  30. #include <asm/io.h>
  31. #include <asm/mach-types.h>
  32. #include <asm/mach/map.h>
  33. #include <asm/arch/board.h>
  34. #include <asm/arch/sram.h>
  35. #include <asm/arch/omapfb.h>
  36. #if defined(CONFIG_FB_OMAP) || defined(CONFIG_FB_OMAP_MODULE)
  37. static struct omapfb_platform_data omapfb_config;
  38. static int config_invalid;
  39. static int configured_regions;
  40. static u64 omap_fb_dma_mask = ~(u32)0;
  41. static struct platform_device omap_fb_device = {
  42. .name = "omapfb",
  43. .id = -1,
  44. .dev = {
  45. .dma_mask = &omap_fb_dma_mask,
  46. .coherent_dma_mask = ~(u32)0,
  47. .platform_data = &omapfb_config,
  48. },
  49. .num_resources = 0,
  50. };
  51. static inline int ranges_overlap(unsigned long start1, unsigned long size1,
  52. unsigned long start2, unsigned long size2)
  53. {
  54. return (start1 >= start2 && start1 < start2 + size2) ||
  55. (start2 >= start1 && start2 < start1 + size1);
  56. }
  57. static inline int range_included(unsigned long start1, unsigned long size1,
  58. unsigned long start2, unsigned long size2)
  59. {
  60. return start1 >= start2 && start1 + size1 <= start2 + size2;
  61. }
  62. /* Check if there is an overlapping region. */
  63. static int fbmem_region_reserved(unsigned long start, size_t size)
  64. {
  65. struct omapfb_mem_region *rg;
  66. int i;
  67. rg = &omapfb_config.mem_desc.region[0];
  68. for (i = 0; i < OMAPFB_PLANE_NUM; i++, rg++) {
  69. if (!rg->paddr)
  70. /* Empty slot. */
  71. continue;
  72. if (ranges_overlap(start, size, rg->paddr, rg->size))
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. /*
  78. * Get the region_idx`th region from board config/ATAG and convert it to
  79. * our internal format.
  80. */
  81. static int get_fbmem_region(int region_idx, struct omapfb_mem_region *rg)
  82. {
  83. const struct omap_fbmem_config *conf;
  84. u32 paddr;
  85. conf = omap_get_nr_config(OMAP_TAG_FBMEM,
  86. struct omap_fbmem_config, region_idx);
  87. if (conf == NULL)
  88. return -ENOENT;
  89. paddr = conf->start;
  90. /*
  91. * Low bits encode the page allocation mode, if high bits
  92. * are zero. Otherwise we need a page aligned fixed
  93. * address.
  94. */
  95. memset(rg, 0, sizeof(*rg));
  96. rg->type = paddr & ~PAGE_MASK;
  97. rg->paddr = paddr & PAGE_MASK;
  98. rg->size = PAGE_ALIGN(conf->size);
  99. return 0;
  100. }
  101. static int set_fbmem_region_type(struct omapfb_mem_region *rg, int mem_type,
  102. unsigned long mem_start,
  103. unsigned long mem_size)
  104. {
  105. /*
  106. * Check if the configuration specifies the type explicitly.
  107. * type = 0 && paddr = 0, a default don't care case maps to
  108. * the SDRAM type.
  109. */
  110. if (rg->type || (!rg->type && !rg->paddr))
  111. return 0;
  112. if (ranges_overlap(rg->paddr, rg->size, mem_start, mem_size)) {
  113. rg->type = mem_type;
  114. return 0;
  115. }
  116. /* Can't determine it. */
  117. return -1;
  118. }
  119. static int check_fbmem_region(int region_idx, struct omapfb_mem_region *rg,
  120. unsigned long start_avail, unsigned size_avail)
  121. {
  122. unsigned long paddr = rg->paddr;
  123. size_t size = rg->size;
  124. if (rg->type > OMAPFB_MEMTYPE_MAX) {
  125. printk(KERN_ERR
  126. "Invalid start address for FB region %d\n", region_idx);
  127. return -EINVAL;
  128. }
  129. if (!rg->size) {
  130. printk(KERN_ERR "Zero size for FB region %d\n", region_idx);
  131. return -EINVAL;
  132. }
  133. if (!paddr)
  134. /* Allocate this dynamically, leave paddr 0 for now. */
  135. return 0;
  136. /*
  137. * Fixed region for the given RAM range. Check if it's already
  138. * reserved by the FB code or someone else.
  139. */
  140. if (fbmem_region_reserved(paddr, size) ||
  141. !range_included(paddr, size, start_avail, size_avail)) {
  142. printk(KERN_ERR "Trying to use reserved memory "
  143. "for FB region %d\n", region_idx);
  144. return -EINVAL;
  145. }
  146. return 0;
  147. }
  148. /*
  149. * Called from map_io. We need to call to this early enough so that we
  150. * can reserve the fixed SDRAM regions before VM could get hold of them.
  151. */
  152. void __init omapfb_reserve_sdram(void)
  153. {
  154. struct bootmem_data *bdata;
  155. unsigned long sdram_start, sdram_size;
  156. unsigned long reserved;
  157. int i;
  158. if (config_invalid)
  159. return;
  160. bdata = NODE_DATA(0)->bdata;
  161. sdram_start = bdata->node_min_pfn << PAGE_SHIFT;
  162. sdram_size = (bdata->node_low_pfn << PAGE_SHIFT) - sdram_start;
  163. reserved = 0;
  164. for (i = 0; ; i++) {
  165. struct omapfb_mem_region rg;
  166. if (get_fbmem_region(i, &rg) < 0)
  167. break;
  168. if (i == OMAPFB_PLANE_NUM) {
  169. printk(KERN_ERR
  170. "Extraneous FB mem configuration entries\n");
  171. config_invalid = 1;
  172. return;
  173. }
  174. /* Check if it's our memory type. */
  175. if (set_fbmem_region_type(&rg, OMAPFB_MEMTYPE_SDRAM,
  176. sdram_start, sdram_size) < 0 ||
  177. (rg.type != OMAPFB_MEMTYPE_SDRAM))
  178. continue;
  179. BUG_ON(omapfb_config.mem_desc.region[i].size);
  180. if (check_fbmem_region(i, &rg, sdram_start, sdram_size) < 0) {
  181. config_invalid = 1;
  182. return;
  183. }
  184. if (rg.paddr)
  185. reserve_bootmem(rg.paddr, rg.size, BOOTMEM_DEFAULT);
  186. reserved += rg.size;
  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