video-uclass.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <console.h>
  7. #include <cpu_func.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. #include <mapmem.h>
  12. #include <stdio_dev.h>
  13. #include <video.h>
  14. #include <video_console.h>
  15. #include <asm/cache.h>
  16. #include <dm/lists.h>
  17. #include <dm/device-internal.h>
  18. #include <dm/uclass-internal.h>
  19. #ifdef CONFIG_SANDBOX
  20. #include <asm/sdl.h>
  21. #endif
  22. /*
  23. * Theory of operation:
  24. *
  25. * Before relocation each device is bound. The driver for each device must
  26. * set the @align and @size values in struct video_uc_platdata. This
  27. * information represents the requires size and alignment of the frame buffer
  28. * for the device. The values can be an over-estimate but cannot be too
  29. * small. The actual values will be suppled (in the same manner) by the bind()
  30. * method after relocation.
  31. *
  32. * This information is then picked up by video_reserve() which works out how
  33. * much memory is needed for all devices. This is allocated between
  34. * gd->video_bottom and gd->video_top.
  35. *
  36. * After relocation the same process occurs. The driver supplies the same
  37. * @size and @align information and this time video_post_bind() checks that
  38. * the drivers does not overflow the allocated memory.
  39. *
  40. * The frame buffer address is actually set (to plat->base) in
  41. * video_post_probe(). This function also clears the frame buffer and
  42. * allocates a suitable text console device. This can then be used to write
  43. * text to the video device.
  44. */
  45. DECLARE_GLOBAL_DATA_PTR;
  46. void video_set_flush_dcache(struct udevice *dev, bool flush)
  47. {
  48. struct video_priv *priv = dev_get_uclass_priv(dev);
  49. priv->flush_dcache = flush;
  50. }
  51. static ulong alloc_fb(struct udevice *dev, ulong *addrp)
  52. {
  53. struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
  54. ulong base, align, size;
  55. if (!plat->size)
  56. return 0;
  57. align = plat->align ? plat->align : 1 << 20;
  58. base = *addrp - plat->size;
  59. base &= ~(align - 1);
  60. plat->base = base;
  61. size = *addrp - base;
  62. *addrp = base;
  63. return size;
  64. }
  65. int video_reserve(ulong *addrp)
  66. {
  67. struct udevice *dev;
  68. ulong size;
  69. gd->video_top = *addrp;
  70. for (uclass_find_first_device(UCLASS_VIDEO, &dev);
  71. dev;
  72. uclass_find_next_device(&dev)) {
  73. size = alloc_fb(dev, addrp);
  74. debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
  75. __func__, size, *addrp, dev->name);
  76. }
  77. gd->video_bottom = *addrp;
  78. gd->fb_base = *addrp;
  79. debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
  80. gd->video_top);
  81. return 0;
  82. }
  83. int video_clear(struct udevice *dev)
  84. {
  85. struct video_priv *priv = dev_get_uclass_priv(dev);
  86. int ret;
  87. switch (priv->bpix) {
  88. case VIDEO_BPP16:
  89. if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
  90. u16 *ppix = priv->fb;
  91. u16 *end = priv->fb + priv->fb_size;
  92. while (ppix < end)
  93. *ppix++ = priv->colour_bg;
  94. break;
  95. }
  96. case VIDEO_BPP32:
  97. if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
  98. u32 *ppix = priv->fb;
  99. u32 *end = priv->fb + priv->fb_size;
  100. while (ppix < end)
  101. *ppix++ = priv->colour_bg;
  102. break;
  103. }
  104. default:
  105. memset(priv->fb, priv->colour_bg, priv->fb_size);
  106. break;
  107. }
  108. ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
  109. if (ret)
  110. return ret;
  111. return 0;
  112. }
  113. void video_set_default_colors(struct udevice *dev, bool invert)
  114. {
  115. struct video_priv *priv = dev_get_uclass_priv(dev);
  116. int fore, back;
  117. if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
  118. /* White is used when switching to bold, use light gray here */
  119. fore = VID_LIGHT_GRAY;
  120. back = VID_BLACK;
  121. } else {
  122. fore = VID_BLACK;
  123. back = VID_WHITE;
  124. }
  125. if (invert) {
  126. int temp;
  127. temp = fore;
  128. fore = back;
  129. back = temp;
  130. }
  131. priv->fg_col_idx = fore;
  132. priv->bg_col_idx = back;
  133. priv->colour_fg = vid_console_color(priv, fore);
  134. priv->colour_bg = vid_console_color(priv, back);
  135. }
  136. /* Flush video activity to the caches */
  137. void video_sync(struct udevice *vid, bool force)
  138. {
  139. /*
  140. * flush_dcache_range() is declared in common.h but it seems that some
  141. * architectures do not actually implement it. Is there a way to find
  142. * out whether it exists? For now, ARM is safe.
  143. */
  144. #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
  145. struct video_priv *priv = dev_get_uclass_priv(vid);
  146. if (priv->flush_dcache) {
  147. flush_dcache_range((ulong)priv->fb,
  148. ALIGN((ulong)priv->fb + priv->fb_size,
  149. CONFIG_SYS_CACHELINE_SIZE));
  150. }
  151. #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
  152. struct video_priv *priv = dev_get_uclass_priv(vid);
  153. static ulong last_sync;
  154. if (force || get_timer(last_sync) > 10) {
  155. sandbox_sdl_sync(priv->fb);
  156. last_sync = get_timer(0);
  157. }
  158. #endif
  159. }
  160. void video_sync_all(void)
  161. {
  162. struct udevice *dev;
  163. for (uclass_find_first_device(UCLASS_VIDEO, &dev);
  164. dev;
  165. uclass_find_next_device(&dev)) {
  166. if (device_active(dev))
  167. video_sync(dev, true);
  168. }
  169. }
  170. int video_get_xsize(struct udevice *dev)
  171. {
  172. struct video_priv *priv = dev_get_uclass_priv(dev);
  173. return priv->xsize;
  174. }
  175. int video_get_ysize(struct udevice *dev)
  176. {
  177. struct video_priv *priv = dev_get_uclass_priv(dev);
  178. return priv->ysize;
  179. }
  180. #ifdef CONFIG_VIDEO_COPY
  181. int video_sync_copy(struct udevice *dev, void *from, void *to)
  182. {
  183. struct video_priv *priv = dev_get_uclass_priv(dev);
  184. if (priv->copy_fb) {
  185. long offset, size;
  186. /* Find the offset of the first byte to copy */
  187. if ((ulong)to > (ulong)from) {
  188. size = to - from;
  189. offset = from - priv->fb;
  190. } else {
  191. size = from - to;
  192. offset = to - priv->fb;
  193. }
  194. /*
  195. * Allow a bit of leeway for valid requests somewhere near the
  196. * frame buffer
  197. */
  198. if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
  199. #ifdef DEBUG
  200. char str[80];
  201. snprintf(str, sizeof(str),
  202. "[sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
  203. priv->fb, from, to, offset);
  204. console_puts_select_stderr(true, str);
  205. #endif
  206. return -EFAULT;
  207. }
  208. /*
  209. * Silently crop the memcpy. This allows callers to avoid doing
  210. * this themselves. It is common for the end pointer to go a
  211. * few lines after the end of the frame buffer, since most of
  212. * the update algorithms terminate a line after their last write
  213. */
  214. if (offset + size > priv->fb_size) {
  215. size = priv->fb_size - offset;
  216. } else if (offset < 0) {
  217. size += offset;
  218. offset = 0;
  219. }
  220. memcpy(priv->copy_fb + offset, priv->fb + offset, size);
  221. }
  222. return 0;
  223. }
  224. #endif
  225. /* Set up the colour map */
  226. static int video_pre_probe(struct udevice *dev)
  227. {
  228. struct video_priv *priv = dev_get_uclass_priv(dev);
  229. priv->cmap = calloc(256, sizeof(ushort));
  230. if (!priv->cmap)
  231. return -ENOMEM;
  232. return 0;
  233. }
  234. static int video_pre_remove(struct udevice *dev)
  235. {
  236. struct video_priv *priv = dev_get_uclass_priv(dev);
  237. free(priv->cmap);
  238. return 0;
  239. }
  240. /* Set up the display ready for use */
  241. static int video_post_probe(struct udevice *dev)
  242. {
  243. struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
  244. struct video_priv *priv = dev_get_uclass_priv(dev);
  245. char name[30], drv[15], *str;
  246. const char *drv_name = drv;
  247. struct udevice *cons;
  248. int ret;
  249. /* Set up the line and display size */
  250. priv->fb = map_sysmem(plat->base, plat->size);
  251. if (!priv->line_length)
  252. priv->line_length = priv->xsize * VNBYTES(priv->bpix);
  253. priv->fb_size = priv->line_length * priv->ysize;
  254. if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
  255. priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
  256. /* Set up colors */
  257. video_set_default_colors(dev, false);
  258. if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
  259. video_clear(dev);
  260. /*
  261. * Create a text console device. For now we always do this, although
  262. * it might be useful to support only bitmap drawing on the device
  263. * for boards that don't need to display text. We create a TrueType
  264. * console if enabled, a rotated console if the video driver requests
  265. * it, otherwise a normal console.
  266. *
  267. * The console can be override by setting vidconsole_drv_name before
  268. * probing this video driver, or in the probe() method.
  269. *
  270. * TrueType does not support rotation at present so fall back to the
  271. * rotated console in that case.
  272. */
  273. if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
  274. snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
  275. strcpy(drv, "vidconsole_tt");
  276. } else {
  277. snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
  278. priv->rot);
  279. snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
  280. }
  281. str = strdup(name);
  282. if (!str)
  283. return -ENOMEM;
  284. if (priv->vidconsole_drv_name)
  285. drv_name = priv->vidconsole_drv_name;
  286. ret = device_bind_driver(dev, drv_name, str, &cons);
  287. if (ret) {
  288. debug("%s: Cannot bind console driver\n", __func__);
  289. return ret;
  290. }
  291. ret = device_probe(cons);
  292. if (ret) {
  293. debug("%s: Cannot probe console driver\n", __func__);
  294. return ret;
  295. }
  296. return 0;
  297. };
  298. /* Post-relocation, allocate memory for the frame buffer */
  299. static int video_post_bind(struct udevice *dev)
  300. {
  301. ulong addr = gd->video_top;
  302. ulong size;
  303. /* Before relocation there is nothing to do here */
  304. if (!(gd->flags & GD_FLG_RELOC))
  305. return 0;
  306. size = alloc_fb(dev, &addr);
  307. if (addr < gd->video_bottom) {
  308. /* Device tree node may need the 'u-boot,dm-pre-reloc' or
  309. * 'u-boot,dm-pre-proper' tag
  310. */
  311. printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
  312. dev->name);
  313. return -ENOSPC;
  314. }
  315. debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
  316. __func__, size, addr, dev->name);
  317. gd->video_bottom = addr;
  318. return 0;
  319. }
  320. UCLASS_DRIVER(video) = {
  321. .id = UCLASS_VIDEO,
  322. .name = "video",
  323. .flags = DM_UC_FLAG_SEQ_ALIAS,
  324. .post_bind = video_post_bind,
  325. .pre_probe = video_pre_probe,
  326. .post_probe = video_post_probe,
  327. .pre_remove = video_pre_remove,
  328. .per_device_auto_alloc_size = sizeof(struct video_priv),
  329. .per_device_platdata_auto_alloc_size = sizeof(struct video_uc_platdata),
  330. };