video-uclass.c 8.0 KB

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