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 #ifndef CONFIG_DRM_ROCKCHIP 76 struct udevice *dev; 77 #endif 78 ulong size; 79 80 gd->video_top = *addrp; 81 #ifdef CONFIG_DRM_ROCKCHIP 82 size = DRM_ROCKCHIP_FB_SIZE + MEMORY_POOL_SIZE; 83 *addrp = *addrp - size; 84 *addrp &= ~((1 << 20) - 1); 85 debug("Reserving %lx Bytes for video at: %lx\n", size, *addrp); 86 #else 87 for (uclass_find_first_device(UCLASS_VIDEO, &dev); 88 dev; 89 uclass_find_next_device(&dev)) { 90 size = alloc_fb(dev, addrp); 91 debug("%s: Reserving %lx bytes at %lx for video device '%s'\n", 92 __func__, size, *addrp, dev->name); 93 } 94 #endif 95 gd->video_bottom = *addrp; 96 debug("Video frame buffers from %lx to %lx\n", gd->video_bottom, 97 gd->video_top); 98 99 return 0; 100 } 101 102 static int video_clear(struct udevice *dev) 103 { 104 struct video_priv *priv = dev_get_uclass_priv(dev); 105 106 if (priv->bpix == VIDEO_BPP32) { 107 u32 *ppix = priv->fb; 108 u32 *end = priv->fb + priv->fb_size; 109 110 while (ppix < end) 111 *ppix++ = priv->colour_bg; 112 } else { 113 memset(priv->fb, priv->colour_bg, priv->fb_size); 114 } 115 116 return 0; 117 } 118 119 /* Flush video activity to the caches */ 120 void video_sync(struct udevice *vid) 121 { 122 /* 123 * flush_dcache_range() is declared in common.h but it seems that some 124 * architectures do not actually implement it. Is there a way to find 125 * out whether it exists? For now, ARM is safe. 126 */ 127 #if defined(CONFIG_ARM) && !defined(CONFIG_SYS_DCACHE_OFF) 128 struct video_priv *priv = dev_get_uclass_priv(vid); 129 130 if (priv->flush_dcache) { 131 flush_dcache_range((ulong)priv->fb, 132 ALIGN((ulong)priv->fb + priv->fb_size, 133 CONFIG_SYS_CACHELINE_SIZE)); 134 } 135 #elif defined(CONFIG_VIDEO_SANDBOX_SDL) 136 struct video_priv *priv = dev_get_uclass_priv(vid); 137 static ulong last_sync; 138 139 if (get_timer(last_sync) > 10) { 140 sandbox_sdl_sync(priv->fb); 141 last_sync = get_timer(0); 142 } 143 #endif 144 } 145 146 void video_sync_all(void) 147 { 148 struct udevice *dev; 149 150 for (uclass_find_first_device(UCLASS_VIDEO, &dev); 151 dev; 152 uclass_find_next_device(&dev)) { 153 if (device_active(dev)) 154 video_sync(dev); 155 } 156 } 157 158 int video_get_xsize(struct udevice *dev) 159 { 160 struct video_priv *priv = dev_get_uclass_priv(dev); 161 162 return priv->xsize; 163 } 164 165 int video_get_ysize(struct udevice *dev) 166 { 167 struct video_priv *priv = dev_get_uclass_priv(dev); 168 169 return priv->ysize; 170 } 171 172 /* Set up the colour map */ 173 static int video_pre_probe(struct udevice *dev) 174 { 175 struct video_priv *priv = dev_get_uclass_priv(dev); 176 177 priv->cmap = calloc(256, sizeof(ushort)); 178 if (!priv->cmap) 179 return -ENOMEM; 180 181 return 0; 182 } 183 184 static int video_pre_remove(struct udevice *dev) 185 { 186 struct video_priv *priv = dev_get_uclass_priv(dev); 187 188 free(priv->cmap); 189 190 return 0; 191 } 192 193 /* Set up the display ready for use */ 194 static int video_post_probe(struct udevice *dev) 195 { 196 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev); 197 struct video_priv *priv = dev_get_uclass_priv(dev); 198 char name[30], drv[15], *str; 199 const char *drv_name = drv; 200 struct udevice *cons; 201 int ret; 202 203 /* Set up the line and display size */ 204 priv->fb = map_sysmem(plat->base, plat->size); 205 priv->line_length = priv->xsize * VNBYTES(priv->bpix); 206 priv->fb_size = priv->line_length * priv->ysize; 207 208 /* Set up colours - we could in future support other colours */ 209 #ifdef CONFIG_SYS_WHITE_ON_BLACK 210 priv->colour_fg = 0xffffff; 211 #else 212 priv->colour_bg = 0xffffff; 213 #endif 214 video_clear(dev); 215 216 /* 217 * Create a text console device. For now we always do this, although 218 * it might be useful to support only bitmap drawing on the device 219 * for boards that don't need to display text. We create a TrueType 220 * console if enabled, a rotated console if the video driver requests 221 * it, otherwise a normal console. 222 * 223 * The console can be override by setting vidconsole_drv_name before 224 * probing this video driver, or in the probe() method. 225 * 226 * TrueType does not support rotation at present so fall back to the 227 * rotated console in that case. 228 */ 229 if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) { 230 snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name); 231 strcpy(drv, "vidconsole_tt"); 232 } else { 233 snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name, 234 priv->rot); 235 snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot); 236 } 237 238 str = strdup(name); 239 if (!str) 240 return -ENOMEM; 241 if (priv->vidconsole_drv_name) 242 drv_name = priv->vidconsole_drv_name; 243 ret = device_bind_driver(dev, drv_name, str, &cons); 244 if (ret) { 245 debug("%s: Cannot bind console driver\n", __func__); 246 return ret; 247 } 248 249 ret = device_probe(cons); 250 if (ret) { 251 debug("%s: Cannot probe console driver\n", __func__); 252 return ret; 253 } 254 255 return 0; 256 }; 257 258 /* Post-relocation, allocate memory for the frame buffer */ 259 static int video_post_bind(struct udevice *dev) 260 { 261 ulong addr = gd->video_top; 262 ulong size; 263 264 /* Before relocation there is nothing to do here */ 265 if ((!gd->flags & GD_FLG_RELOC)) 266 return 0; 267 size = alloc_fb(dev, &addr); 268 if (addr < gd->video_bottom) { 269 /* Device tree node may need the 'u-boot,dm-pre-reloc' tag */ 270 printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n", 271 dev->name); 272 return -ENOSPC; 273 } 274 debug("%s: Claiming %lx bytes at %lx for video device '%s'\n", 275 __func__, size, addr, dev->name); 276 gd->video_bottom = addr; 277 278 return 0; 279 } 280 281 UCLASS_DRIVER(video) = { 282 .id = UCLASS_VIDEO, 283 .name = "video", 284 .flags = DM_UC_FLAG_SEQ_ALIAS, 285 .post_bind = video_post_bind, 286 .pre_probe = video_pre_probe, 287 .post_probe = video_post_probe, 288 .pre_remove = video_pre_remove, 289 .per_device_auto_alloc_size = sizeof(struct video_priv), 290 .per_device_platdata_auto_alloc_size = sizeof(struct video_uc_platdata), 291 }; 292