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