video-uclass.c 7.7 KB

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