fb.c 10 KB

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