1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Simplefb device tree support
4  *
5  * (C) Copyright 2015
6  * Stephen Warren <swarren@wwwdotorg.org>
7  */
8 
9 #include <dm.h>
10 #include <fdt_support.h>
11 #include <asm/global_data.h>
12 #include <linux/libfdt.h>
13 #include <video.h>
14 #include <spl.h>
15 #include <bloblist.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
fdt_simplefb_configure_node(void * blob,int off)19 static int fdt_simplefb_configure_node(void *blob, int off)
20 {
21 	int xsize, ysize;
22 	int bpix; /* log2 of bits per pixel */
23 	const char *name;
24 	ulong fb_base;
25 	struct video_uc_plat *plat;
26 	struct video_priv *uc_priv;
27 	struct udevice *dev;
28 	int ret;
29 
30 	if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && xpl_phase() > PHASE_SPL) {
31 		struct video_handoff *ho;
32 
33 		ho = bloblist_find(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho));
34 		if (!ho)
35 			return log_msg_ret("Missing video bloblist", -ENOENT);
36 
37 		xsize = ho->xsize;
38 		ysize = ho->ysize;
39 		bpix = ho->bpix;
40 		fb_base = ho->fb;
41 	} else {
42 		ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
43 		if (ret)
44 			return ret;
45 		uc_priv = dev_get_uclass_priv(dev);
46 		plat = dev_get_uclass_plat(dev);
47 		xsize = uc_priv->xsize;
48 		ysize = uc_priv->ysize;
49 		bpix = uc_priv->bpix;
50 		fb_base = plat->base;
51 	}
52 
53 	switch (bpix) {
54 	case 4: /* VIDEO_BPP16 */
55 		name = "r5g6b5";
56 		break;
57 	case 5: /* VIDEO_BPP32 */
58 		name = "a8r8g8b8";
59 		break;
60 	default:
61 		return -EINVAL;
62 	}
63 
64 	return fdt_setup_simplefb_node(blob, off, fb_base, xsize, ysize,
65 				       xsize * (1 << bpix) / 8, name);
66 }
67 
fdt_simplefb_add_node(void * blob)68 int fdt_simplefb_add_node(void *blob)
69 {
70 	static const char compat[] = "simple-framebuffer";
71 	static const char disabled[] = "disabled";
72 	int off, ret;
73 
74 	off = fdt_add_subnode(blob, 0, "framebuffer");
75 	if (off < 0)
76 		return -1;
77 
78 	ret = fdt_setprop(blob, off, "status", disabled, sizeof(disabled));
79 	if (ret < 0)
80 		return -1;
81 
82 	ret = fdt_setprop(blob, off, "compatible", compat, sizeof(compat));
83 	if (ret < 0)
84 		return -1;
85 
86 	return fdt_simplefb_configure_node(blob, off);
87 }
88 
89 /**
90  * fdt_simplefb_enable_existing_node() - enable simple-framebuffer DT node
91  *
92  * @blob:	device-tree
93  * Return:	0 on success, non-zero otherwise
94  */
fdt_simplefb_enable_existing_node(void * blob)95 static int fdt_simplefb_enable_existing_node(void *blob)
96 {
97 	int off;
98 
99 	off = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
100 	if (off < 0)
101 		return -1;
102 
103 	return fdt_simplefb_configure_node(blob, off);
104 }
105 
fdt_simplefb_enable_and_mem_rsv(void * blob)106 int fdt_simplefb_enable_and_mem_rsv(void *blob)
107 {
108 	int ret;
109 
110 	/* nothing to do when video is not active */
111 	if (!video_is_active())
112 		return 0;
113 
114 	ret = fdt_simplefb_enable_existing_node(blob);
115 	if (ret)
116 		return ret;
117 
118 	return fdt_add_fb_mem_rsv(blob);
119 }
120