efi_loader: round the memory area in efi_add_memory_map()
[oweals/u-boot.git] / drivers / video / meson / meson_vpu.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Amlogic Meson Video Processing Unit driver
4  *
5  * Copyright (c) 2018 BayLibre, SAS.
6  * Author: Neil Armstrong <narmstrong@baylibre.com>
7  */
8
9 #include <common.h>
10 #include <display.h>
11 #include <dm.h>
12 #include <efi_loader.h>
13 #include <fdt_support.h>
14 #include <linux/sizes.h>
15 #include <asm/arch/mem.h>
16 #include <dm/device-internal.h>
17 #include <dm/uclass-internal.h>
18
19 #include "meson_vpu.h"
20 #include "meson_registers.h"
21 #include "simplefb_common.h"
22
23 #define MESON_VPU_OVERSCAN SZ_64K
24
25 /* Static variable for use in meson_vpu_rsv_fb() */
26 static struct meson_framebuffer {
27         u64 base;
28         u64 fb_size;
29         unsigned int xsize;
30         unsigned int ysize;
31         bool is_cvbs;
32 } meson_fb = { 0 };
33
34 bool meson_vpu_is_compatible(struct meson_vpu_priv *priv,
35                              enum vpu_compatible family)
36 {
37         enum vpu_compatible compat = dev_get_driver_data(priv->dev);
38
39         return compat == family;
40 }
41
42 static int meson_vpu_setup_mode(struct udevice *dev, struct udevice *disp)
43 {
44         struct video_uc_platdata *uc_plat = dev_get_uclass_platdata(dev);
45         struct video_priv *uc_priv = dev_get_uclass_priv(dev);
46         struct display_timing timing;
47         bool is_cvbs = false;
48         int ret = 0;
49
50         if (disp) {
51                 ret = display_read_timing(disp, &timing);
52                 if (ret) {
53                         debug("%s: Failed to read timings\n", __func__);
54                         goto cvbs;
55                 }
56
57                 uc_priv->xsize = timing.hactive.typ;
58                 uc_priv->ysize = timing.vactive.typ;
59
60                 ret = display_enable(disp, 0, &timing);
61                 if (ret)
62                         goto cvbs;
63         } else {
64 cvbs:
65                 /* CVBS has a fixed 720x480i (NTSC) and 720x576i (PAL) */
66                 is_cvbs = true;
67                 timing.flags = DISPLAY_FLAGS_INTERLACED;
68                 uc_priv->xsize = 720;
69                 uc_priv->ysize = 576;
70         }
71
72         uc_priv->bpix = VPU_MAX_LOG2_BPP;
73
74         meson_fb.is_cvbs = is_cvbs;
75         meson_fb.xsize = uc_priv->xsize;
76         meson_fb.ysize = uc_priv->ysize;
77
78         /* Move the framebuffer to the end of addressable ram */
79         meson_fb.fb_size = ALIGN(meson_fb.xsize * meson_fb.ysize *
80                                  ((1 << VPU_MAX_LOG2_BPP) / 8) +
81                                  MESON_VPU_OVERSCAN, EFI_PAGE_SIZE);
82         meson_fb.base = gd->bd->bi_dram[0].start +
83                         gd->bd->bi_dram[0].size - meson_fb.fb_size;
84
85         /* Override the framebuffer address */
86         uc_plat->base = meson_fb.base;
87
88         meson_vpu_setup_plane(dev, timing.flags & DISPLAY_FLAGS_INTERLACED);
89         meson_vpu_setup_venc(dev, &timing, is_cvbs);
90         meson_vpu_setup_vclk(dev, &timing, is_cvbs);
91
92         video_set_flush_dcache(dev, 1);
93
94         return 0;
95 }
96
97 static const struct udevice_id meson_vpu_ids[] = {
98         { .compatible = "amlogic,meson-gxbb-vpu", .data = VPU_COMPATIBLE_GXBB },
99         { .compatible = "amlogic,meson-gxl-vpu", .data = VPU_COMPATIBLE_GXL },
100         { .compatible = "amlogic,meson-gxm-vpu", .data = VPU_COMPATIBLE_GXM },
101         { .compatible = "amlogic,meson-g12a-vpu", .data = VPU_COMPATIBLE_G12A },
102         { }
103 };
104
105 static int meson_vpu_probe(struct udevice *dev)
106 {
107         struct meson_vpu_priv *priv = dev_get_priv(dev);
108         struct udevice *disp;
109         int ret;
110
111         /* Before relocation we don't need to do anything */
112         if (!(gd->flags & GD_FLG_RELOC))
113                 return 0;
114
115         priv->dev = dev;
116
117         priv->io_base = dev_remap_addr_index(dev, 0);
118         if (!priv->io_base)
119                 return -EINVAL;
120
121         priv->hhi_base = dev_remap_addr_index(dev, 1);
122         if (!priv->hhi_base)
123                 return -EINVAL;
124
125         priv->dmc_base = dev_remap_addr_index(dev, 2);
126         if (!priv->dmc_base)
127                 return -EINVAL;
128
129         meson_vpu_init(dev);
130
131         /* probe the display */
132         ret = uclass_get_device(UCLASS_DISPLAY, 0, &disp);
133
134         return meson_vpu_setup_mode(dev, ret ? NULL : disp);
135 }
136
137 static int meson_vpu_bind(struct udevice *dev)
138 {
139         struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
140
141         plat->size = VPU_MAX_WIDTH * VPU_MAX_HEIGHT *
142                 (1 << VPU_MAX_LOG2_BPP) / 8;
143
144         return 0;
145 }
146
147 #if defined(CONFIG_VIDEO_DT_SIMPLEFB)
148 static void meson_vpu_setup_simplefb(void *fdt)
149 {
150         const char *pipeline = NULL;
151         u64 mem_start, mem_size;
152         int offset, ret;
153
154         if (meson_fb.is_cvbs)
155                 pipeline = "vpu-cvbs";
156         else
157                 pipeline = "vpu-hdmi";
158
159         offset = meson_simplefb_fdt_match(fdt, pipeline);
160         if (offset < 0) {
161                 eprintf("Cannot setup simplefb: node not found\n");
162
163                 /* If simplefb is missing, add it as reserved memory */
164                 meson_board_add_reserved_memory(fdt, meson_fb.base,
165                                                 meson_fb.fb_size);
166
167                 return;
168         }
169
170         /*
171          * SimpleFB will try to iomap the framebuffer, so we can't use
172          * fdt_add_mem_rsv on the memory area. Instead, the FB is stored
173          * at the end of the RAM and we strip this portion from the kernel
174          * allowed region
175          */
176         mem_start = gd->bd->bi_dram[0].start;
177         mem_size = gd->bd->bi_dram[0].size - meson_fb.fb_size;
178         ret = fdt_fixup_memory_banks(fdt, &mem_start, &mem_size, 1);
179         if (ret) {
180                 eprintf("Cannot setup simplefb: Error reserving memory\n");
181                 return;
182         }
183
184         ret = fdt_setup_simplefb_node(fdt, offset, meson_fb.base,
185                                       meson_fb.xsize, meson_fb.ysize,
186                                       meson_fb.xsize * 4, "x8r8g8b8");
187         if (ret)
188                 eprintf("Cannot setup simplefb: Error setting properties\n");
189 }
190 #endif
191
192 void meson_vpu_rsv_fb(void *fdt)
193 {
194         if (!meson_fb.base || !meson_fb.xsize || !meson_fb.ysize)
195                 return;
196
197 #if defined(CONFIG_EFI_LOADER)
198         efi_add_memory_map(meson_fb.base, meson_fb.fb_size,
199                            EFI_RESERVED_MEMORY_TYPE);
200 #endif
201 #if defined(CONFIG_VIDEO_DT_SIMPLEFB)
202         meson_vpu_setup_simplefb(fdt);
203 #endif
204 }
205
206 U_BOOT_DRIVER(meson_vpu) = {
207         .name = "meson_vpu",
208         .id = UCLASS_VIDEO,
209         .of_match = meson_vpu_ids,
210         .probe = meson_vpu_probe,
211         .bind = meson_vpu_bind,
212         .priv_auto_alloc_size = sizeof(struct meson_vpu_priv),
213         .flags  = DM_FLAG_PRE_RELOC | DM_FLAG_REMOVE_WITH_PD_ON,
214 };