video-uclass.c 10 KB

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