fb.c 9.8 KB

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