fb.c 10 KB

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