video-uclass.c 11 KB

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