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