video-uclass.c 7.1 KB

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