fb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 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->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. struct memblock_property res;
  154. res.base = addr;
  155. res.size = size;
  156. return !memblock_find(&res) && res.base == addr && res.size == size;
  157. }
  158. static int reserve_sdram(unsigned long addr, unsigned long size)
  159. {
  160. if (memblock_is_region_reserved(addr, size))
  161. return -EBUSY;
  162. if (memblock_reserve(addr, size))
  163. return -ENOMEM;
  164. return 0;
  165. }
  166. /*
  167. * Called from map_io. We need to call to this early enough so that we
  168. * can reserve the fixed SDRAM regions before VM could get hold of them.
  169. */
  170. void __init omapfb_reserve_sdram_memblock(void)
  171. {
  172. unsigned long reserved = 0;
  173. int i;
  174. if (config_invalid)
  175. return;
  176. for (i = 0; ; i++) {
  177. struct omapfb_mem_region rg;
  178. if (get_fbmem_region(i, &rg) < 0)
  179. break;
  180. if (i == OMAPFB_PLANE_NUM) {
  181. pr_err("Extraneous FB mem configuration entries\n");
  182. config_invalid = 1;
  183. return;
  184. }
  185. /* Check if it's our memory type. */
  186. if (rg.type != OMAPFB_MEMTYPE_SDRAM)
  187. continue;
  188. /* Check if the region falls within SDRAM */
  189. if (rg.paddr && !valid_sdram(rg.paddr, rg.size))
  190. continue;
  191. if (rg.size == 0) {
  192. pr_err("Zero size for FB region %d\n", i);
  193. config_invalid = 1;
  194. return;
  195. }
  196. if (rg.paddr) {
  197. if (reserve_sdram(rg.paddr, rg.size)) {
  198. pr_err("Trying to use reserved memory for FB region %d\n",
  199. i);
  200. config_invalid = 1;
  201. return;
  202. }
  203. reserved += rg.size;
  204. }
  205. if (omapfb_config.mem_desc.region[i].size) {
  206. pr_err("FB region %d already set\n", i);
  207. config_invalid = 1;
  208. return;
  209. }
  210. omapfb_config.mem_desc.region[i] = rg;
  211. configured_regions++;
  212. }
  213. omapfb_config.mem_desc.region_cnt = i;
  214. if (reserved)
  215. pr_info("Reserving %lu bytes SDRAM for frame buffer\n",
  216. reserved);
  217. }
  218. /*
  219. * Called at sram init time, before anything is pushed to the SRAM stack.
  220. * Because of the stack scheme, we will allocate everything from the
  221. * start of the lowest address region to the end of SRAM. This will also
  222. * include padding for page alignment and possible holes between regions.
  223. *
  224. * As opposed to the SDRAM case, we'll also do any dynamic allocations at
  225. * this point, since the driver built as a module would have problem with
  226. * freeing / reallocating the regions.
  227. */
  228. unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
  229. unsigned long sram_vstart,
  230. unsigned long sram_size,
  231. unsigned long pstart_avail,
  232. unsigned long size_avail)
  233. {
  234. struct omapfb_mem_region rg;
  235. unsigned long pend_avail;
  236. unsigned long reserved;
  237. int i;
  238. if (config_invalid)
  239. return 0;
  240. reserved = 0;
  241. pend_avail = pstart_avail + size_avail;
  242. for (i = 0; ; i++) {
  243. if (get_fbmem_region(i, &rg) < 0)
  244. break;
  245. if (i == OMAPFB_PLANE_NUM) {
  246. printk(KERN_ERR
  247. "Extraneous FB mem configuration entries\n");
  248. config_invalid = 1;
  249. return 0;
  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. if (check_fbmem_region(i, &rg, pstart_avail, size_avail) < 0) {
  258. config_invalid = 1;
  259. return 0;
  260. }
  261. if (!rg.paddr) {
  262. /* Dynamic allocation */
  263. if ((size_avail & PAGE_MASK) < rg.size) {
  264. printk("Not enough SRAM for FB region %d\n",
  265. i);
  266. config_invalid = 1;
  267. return 0;
  268. }
  269. size_avail = (size_avail - rg.size) & PAGE_MASK;
  270. rg.paddr = pstart_avail + size_avail;
  271. }
  272. /* Reserve everything above the start of the region. */
  273. if (pend_avail - rg.paddr > reserved)
  274. reserved = pend_avail - rg.paddr;
  275. size_avail = pend_avail - reserved - pstart_avail;
  276. /*
  277. * We have a kernel mapping for this already, so the
  278. * driver won't have to make one.
  279. */
  280. rg.vaddr = (void *)(sram_vstart + rg.paddr - sram_pstart);
  281. omapfb_config.mem_desc.region[i] = rg;
  282. configured_regions++;
  283. }
  284. omapfb_config.mem_desc.region_cnt = i;
  285. if (reserved)
  286. pr_info("Reserving %lu bytes SRAM for frame buffer\n",
  287. reserved);
  288. return reserved;
  289. }
  290. void omapfb_set_ctrl_platform_data(void *data)
  291. {
  292. omapfb_config.ctrl_platform_data = data;
  293. }
  294. static inline int omap_init_fb(void)
  295. {
  296. const struct omap_lcd_config *conf;
  297. if (config_invalid)
  298. return 0;
  299. if (configured_regions != omapfb_config.mem_desc.region_cnt) {
  300. printk(KERN_ERR "Invalid FB mem configuration entries\n");
  301. return 0;
  302. }
  303. conf = omap_get_config(OMAP_TAG_LCD, struct omap_lcd_config);
  304. if (conf == NULL) {
  305. if (configured_regions)
  306. /* FB mem config, but no LCD config? */
  307. printk(KERN_ERR "Missing LCD configuration\n");
  308. return 0;
  309. }
  310. omapfb_config.lcd = *conf;
  311. return platform_device_register(&omap_fb_device);
  312. }
  313. arch_initcall(omap_init_fb);
  314. #elif defined(CONFIG_FB_OMAP2) || defined(CONFIG_FB_OMAP2_MODULE)
  315. static u64 omap_fb_dma_mask = ~(u32)0;
  316. static struct omapfb_platform_data omapfb_config;
  317. static struct platform_device omap_fb_device = {
  318. .name = "omapfb",
  319. .id = -1,
  320. .dev = {
  321. .dma_mask = &omap_fb_dma_mask,
  322. .coherent_dma_mask = ~(u32)0,
  323. .platform_data = &omapfb_config,
  324. },
  325. .num_resources = 0,
  326. };
  327. void omapfb_set_platform_data(struct omapfb_platform_data *data)
  328. {
  329. omapfb_config = *data;
  330. }
  331. static inline int omap_init_fb(void)
  332. {
  333. return platform_device_register(&omap_fb_device);
  334. }
  335. arch_initcall(omap_init_fb);
  336. void omapfb_reserve_sdram_memblock(void)
  337. {
  338. }
  339. unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
  340. unsigned long sram_vstart,
  341. unsigned long sram_size,
  342. unsigned long start_avail,
  343. unsigned long size_avail)
  344. {
  345. return 0;
  346. }
  347. #else
  348. void omapfb_set_platform_data(struct omapfb_platform_data *data)
  349. {
  350. }
  351. void omapfb_reserve_sdram_memblock(void)
  352. {
  353. }
  354. unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
  355. unsigned long sram_vstart,
  356. unsigned long sram_size,
  357. unsigned long start_avail,
  358. unsigned long size_avail)
  359. {
  360. return 0;
  361. }
  362. #endif