blob: 493e2f18cd2df18252d789f3f8d9c6c6a59475f8 [file] [log] [blame]
Dario Binacchi260bdb32023-01-28 16:55:31 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Dario Binacchi <dariobin@libero.it>
4 */
5
Dario Binacchi260bdb32023-01-28 16:55:31 +01006#include <clk.h>
7#include <dm.h>
8#include <dm/device_compat.h>
9#include <log.h>
10#include <panel.h>
11#include <video.h>
12#include <asm/global_data.h>
13#include <asm/io.h>
14#include <asm/utils.h>
15#include "tilcdc.h"
16#include "tilcdc-panel.h"
17
18#define LCDC_FMAX 200000000
19
20/* LCD Control Register */
21#define LCDC_CTRL_CLK_DIVISOR_MASK GENMASK(15, 8)
22#define LCDC_CTRL_RASTER_MODE BIT(0)
23#define LCDC_CTRL_CLK_DIVISOR(x) (((x) & GENMASK(7, 0)) << 8)
24/* LCD Clock Enable Register */
25#define LCDC_CLKC_ENABLE_CORECLKEN BIT(0)
26#define LCDC_CLKC_ENABLE_LIDDCLKEN BIT(1)
27#define LCDC_CLKC_ENABLE_DMACLKEN BIT(2)
28/* LCD DMA Control Register */
29#define LCDC_DMA_CTRL_BURST_SIZE(x) (((x) & GENMASK(2, 0)) << 4)
30#define LCDC_DMA_CTRL_BURST_1 0x0
31#define LCDC_DMA_CTRL_BURST_2 0x1
32#define LCDC_DMA_CTRL_BURST_4 0x2
33#define LCDC_DMA_CTRL_BURST_8 0x3
34#define LCDC_DMA_CTRL_BURST_16 0x4
35#define LCDC_DMA_CTRL_FIFO_TH(x) (((x) & GENMASK(2, 0)) << 8)
36/* LCD Timing_0 Register */
37#define LCDC_RASTER_TIMING_0_HORMSB(x) ((((x) - 1) & BIT(10)) >> 7)
38#define LCDC_RASTER_TIMING_0_HORLSB(x) (((((x) >> 4) - 1) & GENMASK(5, 0)) << 4)
39#define LCDC_RASTER_TIMING_0_HSWLSB(x) ((((x) - 1) & GENMASK(5, 0)) << 10)
40#define LCDC_RASTER_TIMING_0_HFPLSB(x) ((((x) - 1) & GENMASK(7, 0)) << 16)
41#define LCDC_RASTER_TIMING_0_HBPLSB(x) ((((x) - 1) & GENMASK(7, 0)) << 24)
42/* LCD Timing_1 Register */
43#define LCDC_RASTER_TIMING_1_VERLSB(x) (((x) - 1) & GENMASK(9, 0))
44#define LCDC_RASTER_TIMING_1_VSW(x) ((((x) - 1) & GENMASK(5, 0)) << 10)
45#define LCDC_RASTER_TIMING_1_VFP(x) (((x) & GENMASK(7, 0)) << 16)
46#define LCDC_RASTER_TIMING_1_VBP(x) (((x) & GENMASK(7, 0)) << 24)
47/* LCD Timing_2 Register */
48#define LCDC_RASTER_TIMING_2_HFPMSB(x) ((((x) - 1) & GENMASK(9, 8)) >> 8)
49#define LCDC_RASTER_TIMING_2_HBPMSB(x) ((((x) - 1) & GENMASK(9, 8)) >> 4)
50#define LCDC_RASTER_TIMING_2_ACB(x) (((x) & GENMASK(7, 0)) << 8)
51#define LCDC_RASTER_TIMING_2_ACBI(x) (((x) & GENMASK(3, 0)) << 16)
52#define LCDC_RASTER_TIMING_2_VSYNC_INVERT BIT(20)
53#define LCDC_RASTER_TIMING_2_HSYNC_INVERT BIT(21)
54#define LCDC_RASTER_TIMING_2_PXCLK_INVERT BIT(22)
55#define LCDC_RASTER_TIMING_2_DE_INVERT BIT(23)
56#define LCDC_RASTER_TIMING_2_HSVS_RISEFALL BIT(24)
57#define LCDC_RASTER_TIMING_2_HSVS_CONTROL BIT(25)
58#define LCDC_RASTER_TIMING_2_VERMSB(x) ((((x) - 1) & BIT(10)) << 16)
59#define LCDC_RASTER_TIMING_2_HSWMSB(x) ((((x) - 1) & GENMASK(9, 6)) << 21)
60/* LCD Raster Ctrl Register */
61#define LCDC_RASTER_CTRL_ENABLE BIT(0)
62#define LCDC_RASTER_CTRL_TFT_MODE BIT(7)
63#define LCDC_RASTER_CTRL_DATA_ORDER BIT(8)
64#define LCDC_RASTER_CTRL_REQDLY(x) (((x) & GENMASK(7, 0)) << 12)
65#define LCDC_RASTER_CTRL_PALMODE_RAWDATA (0x02 << 20)
66#define LCDC_RASTER_CTRL_TFT_ALT_ENABLE BIT(23)
67#define LCDC_RASTER_CTRL_TFT_24BPP_MODE BIT(25)
68#define LCDC_RASTER_CTRL_TFT_24BPP_UNPACK BIT(26)
69
70enum {
71 LCDC_MAX_WIDTH = 2048,
72 LCDC_MAX_HEIGHT = 2048,
73 LCDC_MAX_LOG2_BPP = VIDEO_BPP32,
74};
75
76struct tilcdc_regs {
77 u32 pid;
78 u32 ctrl;
79 u32 gap0;
80 u32 lidd_ctrl;
81 u32 lidd_cs0_conf;
82 u32 lidd_cs0_addr;
83 u32 lidd_cs0_data;
84 u32 lidd_cs1_conf;
85 u32 lidd_cs1_addr;
86 u32 lidd_cs1_data;
87 u32 raster_ctrl;
88 u32 raster_timing0;
89 u32 raster_timing1;
90 u32 raster_timing2;
91 u32 raster_subpanel;
92 u32 raster_subpanel2;
93 u32 lcddma_ctrl;
94 u32 lcddma_fb0_base;
95 u32 lcddma_fb0_ceiling;
96 u32 lcddma_fb1_base;
97 u32 lcddma_fb1_ceiling;
98 u32 sysconfig;
99 u32 irqstatus_raw;
100 u32 irqstatus;
101 u32 irqenable_set;
102 u32 irqenable_clear;
103 u32 gap1;
104 u32 clkc_enable;
105 u32 clkc_reset;
106};
107
108struct tilcdc_priv {
109 struct tilcdc_regs *regs;
110 struct clk gclk;
111 struct clk dpll_m2_clk;
112};
113
114DECLARE_GLOBAL_DATA_PTR;
115
116static ulong tilcdc_set_pixel_clk_rate(struct udevice *dev, ulong rate)
117{
118 struct tilcdc_priv *priv = dev_get_priv(dev);
119 struct tilcdc_regs *regs = priv->regs;
120 ulong mult_rate, mult_round_rate, best_err, err;
121 u32 v;
122 int div, i;
123
124 best_err = rate;
125 div = 0;
126 for (i = 2; i <= 255; i++) {
127 mult_rate = rate * i;
128 mult_round_rate = clk_round_rate(&priv->gclk, mult_rate);
129 if (IS_ERR_VALUE(mult_round_rate))
130 return mult_round_rate;
131
132 err = mult_rate - mult_round_rate;
133 if (err < best_err) {
134 best_err = err;
135 div = i;
136 if (err == 0)
137 break;
138 }
139 }
140
141 if (div == 0) {
142 dev_err(dev, "failed to find a divisor\n");
143 return -EFAULT;
144 }
145
146 mult_rate = clk_set_rate(&priv->gclk, rate * div);
147 v = readl(&regs->ctrl) & ~LCDC_CTRL_CLK_DIVISOR_MASK;
148 v |= LCDC_CTRL_CLK_DIVISOR(div);
149 writel(v, &regs->ctrl);
150 rate = mult_rate / div;
151 dev_dbg(dev, "rate=%ld, div=%d, err=%ld\n", rate, div, err);
152 return rate;
153}
154
155static int tilcdc_remove(struct udevice *dev)
156{
157 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
158 struct tilcdc_priv *priv = dev_get_priv(dev);
159
160 uc_plat->base -= 0x20;
161 uc_plat->size += 0x20;
162 clk_release_all(&priv->gclk, 1);
163 clk_release_all(&priv->dpll_m2_clk, 1);
164 return 0;
165}
166
167static int tilcdc_probe(struct udevice *dev)
168{
169 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
170 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
171 struct tilcdc_priv *priv = dev_get_priv(dev);
172 struct tilcdc_regs *regs = priv->regs;
173 struct udevice *panel, *clk_dev;
174 struct tilcdc_panel_info info;
175 struct display_timing timing;
176 ulong rate;
177 u32 reg;
178 int err;
179
180 /* Before relocation we don't need to do anything */
181 if (!(gd->flags & GD_FLG_RELOC))
182 return 0;
183
184 err = uclass_get_device(UCLASS_PANEL, 0, &panel);
185 if (err) {
186 dev_err(dev, "failed to get panel\n");
187 return err;
188 }
189
190 err = panel_get_display_timing(panel, &timing);
191 if (err) {
192 dev_err(dev, "failed to get display timing\n");
193 return err;
194 }
195
196 if (timing.pixelclock.typ > (LCDC_FMAX / 2)) {
197 dev_err(dev, "invalid display clock-frequency: %d Hz\n",
198 timing.pixelclock.typ);
199 return -EINVAL;
200 }
201
202 if (timing.hactive.typ > LCDC_MAX_WIDTH)
203 timing.hactive.typ = LCDC_MAX_WIDTH;
204
205 if (timing.vactive.typ > LCDC_MAX_HEIGHT)
206 timing.vactive.typ = LCDC_MAX_HEIGHT;
207
208 err = tilcdc_panel_get_display_info(panel, &info);
209 if (err) {
210 dev_err(dev, "failed to get panel info\n");
211 return err;
212 }
213
214 switch (info.bpp) {
215 case 16:
216 case 24:
217 case 32:
218 break;
219 default:
220 dev_err(dev, "invalid seting, bpp: %d\n", info.bpp);
221 return -EINVAL;
222 }
223
224 switch (info.dma_burst_sz) {
225 case 1:
226 case 2:
227 case 4:
228 case 8:
229 case 16:
230 break;
231 default:
232 dev_err(dev, "invalid setting, dma-burst-sz: %d\n",
233 info.dma_burst_sz);
234 return -EINVAL;
235 }
236
237 err = uclass_get_device_by_name(UCLASS_CLK, "lcd_gclk@534", &clk_dev);
238 if (err) {
239 dev_err(dev, "failed to get lcd_gclk device\n");
240 return err;
241 }
242
243 err = clk_request(clk_dev, &priv->gclk);
244 if (err) {
245 dev_err(dev, "failed to get %s clock\n", clk_dev->name);
246 return err;
247 }
248
249 rate = tilcdc_set_pixel_clk_rate(dev, timing.pixelclock.typ);
250 if (IS_ERR_VALUE(rate)) {
251 dev_err(dev, "failed to set pixel clock rate\n");
252 return rate;
253 }
254
255 err = uclass_get_device_by_name(UCLASS_CLK, "dpll_disp_m2_ck@4a4",
256 &clk_dev);
257 if (err) {
258 dev_err(dev, "failed to get dpll_disp_m2 clock device\n");
259 return err;
260 }
261
262 err = clk_request(clk_dev, &priv->dpll_m2_clk);
263 if (err) {
264 dev_err(dev, "failed to get %s clock\n", clk_dev->name);
265 return err;
266 }
267
268 err = clk_set_parent(&priv->gclk, &priv->dpll_m2_clk);
269 if (err) {
270 dev_err(dev, "failed to set %s clock as %s's parent\n",
271 priv->dpll_m2_clk.dev->name, priv->gclk.dev->name);
272 return err;
273 }
274
275 /* palette default entry */
276 memset((void *)uc_plat->base, 0, 0x20);
277 *(unsigned int *)uc_plat->base = 0x4000;
278 /* point fb behind palette */
279 uc_plat->base += 0x20;
280 uc_plat->size -= 0x20;
281
282 writel(LCDC_CLKC_ENABLE_CORECLKEN | LCDC_CLKC_ENABLE_LIDDCLKEN |
283 LCDC_CLKC_ENABLE_DMACLKEN, &regs->clkc_enable);
284 writel(0, &regs->raster_ctrl);
285
286 reg = readl(&regs->ctrl) & LCDC_CTRL_CLK_DIVISOR_MASK;
287 reg |= LCDC_CTRL_RASTER_MODE;
288 writel(reg, &regs->ctrl);
289
290 reg = (timing.hactive.typ * timing.vactive.typ * info.bpp) >> 3;
291 reg += uc_plat->base;
292 writel(uc_plat->base, &regs->lcddma_fb0_base);
293 writel(reg, &regs->lcddma_fb0_ceiling);
294 writel(uc_plat->base, &regs->lcddma_fb1_base);
295 writel(reg, &regs->lcddma_fb1_ceiling);
296
297 reg = LCDC_DMA_CTRL_FIFO_TH(info.fifo_th);
298 switch (info.dma_burst_sz) {
299 case 1:
300 reg |= LCDC_DMA_CTRL_BURST_SIZE(LCDC_DMA_CTRL_BURST_1);
301 break;
302 case 2:
303 reg |= LCDC_DMA_CTRL_BURST_SIZE(LCDC_DMA_CTRL_BURST_2);
304 break;
305 case 4:
306 reg |= LCDC_DMA_CTRL_BURST_SIZE(LCDC_DMA_CTRL_BURST_4);
307 break;
308 case 8:
309 reg |= LCDC_DMA_CTRL_BURST_SIZE(LCDC_DMA_CTRL_BURST_8);
310 break;
311 case 16:
312 reg |= LCDC_DMA_CTRL_BURST_SIZE(LCDC_DMA_CTRL_BURST_16);
313 break;
314 }
315
316 writel(reg, &regs->lcddma_ctrl);
317
318 writel(LCDC_RASTER_TIMING_0_HORLSB(timing.hactive.typ) |
319 LCDC_RASTER_TIMING_0_HORMSB(timing.hactive.typ) |
320 LCDC_RASTER_TIMING_0_HFPLSB(timing.hfront_porch.typ) |
321 LCDC_RASTER_TIMING_0_HBPLSB(timing.hback_porch.typ) |
322 LCDC_RASTER_TIMING_0_HSWLSB(timing.hsync_len.typ),
323 &regs->raster_timing0);
324
325 writel(LCDC_RASTER_TIMING_1_VBP(timing.vback_porch.typ) |
326 LCDC_RASTER_TIMING_1_VFP(timing.vfront_porch.typ) |
327 LCDC_RASTER_TIMING_1_VSW(timing.vsync_len.typ) |
328 LCDC_RASTER_TIMING_1_VERLSB(timing.vactive.typ),
329 &regs->raster_timing1);
330
331 reg = LCDC_RASTER_TIMING_2_ACB(info.ac_bias) |
332 LCDC_RASTER_TIMING_2_ACBI(info.ac_bias_intrpt) |
333 LCDC_RASTER_TIMING_2_HSWMSB(timing.hsync_len.typ) |
334 LCDC_RASTER_TIMING_2_VERMSB(timing.vactive.typ) |
335 LCDC_RASTER_TIMING_2_HBPMSB(timing.hback_porch.typ) |
336 LCDC_RASTER_TIMING_2_HFPMSB(timing.hfront_porch.typ);
337
338 if (timing.flags & DISPLAY_FLAGS_VSYNC_LOW)
339 reg |= LCDC_RASTER_TIMING_2_VSYNC_INVERT;
340
341 if (timing.flags & DISPLAY_FLAGS_HSYNC_LOW)
342 reg |= LCDC_RASTER_TIMING_2_HSYNC_INVERT;
343
344 if (info.invert_pxl_clk)
345 reg |= LCDC_RASTER_TIMING_2_PXCLK_INVERT;
346
347 if (info.sync_edge)
348 reg |= LCDC_RASTER_TIMING_2_HSVS_RISEFALL;
349
350 if (info.sync_ctrl)
351 reg |= LCDC_RASTER_TIMING_2_HSVS_CONTROL;
352
353 writel(reg, &regs->raster_timing2);
354
355 reg = LCDC_RASTER_CTRL_PALMODE_RAWDATA | LCDC_RASTER_CTRL_TFT_MODE |
356 LCDC_RASTER_CTRL_ENABLE | LCDC_RASTER_CTRL_REQDLY(info.fdd);
357
358 if (info.tft_alt_mode)
359 reg |= LCDC_RASTER_CTRL_TFT_ALT_ENABLE;
360
361 if (info.bpp == 24)
362 reg |= LCDC_RASTER_CTRL_TFT_24BPP_MODE;
363 else if (info.bpp == 32)
364 reg |= LCDC_RASTER_CTRL_TFT_24BPP_MODE |
365 LCDC_RASTER_CTRL_TFT_24BPP_UNPACK;
366
367 if (info.raster_order)
368 reg |= LCDC_RASTER_CTRL_DATA_ORDER;
369
370 writel(reg, &regs->raster_ctrl);
371
372 uc_priv->xsize = timing.hactive.typ;
373 uc_priv->ysize = timing.vactive.typ;
374 uc_priv->bpix = log_2_n_round_up(info.bpp);
375
376 err = panel_enable_backlight(panel);
377 if (err) {
378 dev_err(dev, "failed to enable panel backlight\n");
379 return err;
380 }
381
382 return 0;
383}
384
385static int tilcdc_of_to_plat(struct udevice *dev)
386{
387 struct tilcdc_priv *priv = dev_get_priv(dev);
388
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100389 priv->regs = dev_read_addr_ptr(dev);
390 if (!priv->regs) {
Dario Binacchi260bdb32023-01-28 16:55:31 +0100391 dev_err(dev, "failed to get base address\n");
392 return -EINVAL;
393 }
394
395 dev_dbg(dev, "LCD: base address=0x%x\n", (unsigned int)priv->regs);
396 return 0;
397}
398
399static int tilcdc_bind(struct udevice *dev)
400{
401 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
402
403 uc_plat->size = ((LCDC_MAX_WIDTH * LCDC_MAX_HEIGHT *
404 (1 << LCDC_MAX_LOG2_BPP)) >> 3) + 0x20;
405
406 dev_dbg(dev, "frame buffer size 0x%x\n", uc_plat->size);
407 return 0;
408}
409
410static const struct udevice_id tilcdc_ids[] = {
411 {.compatible = "ti,am33xx-tilcdc"},
412 {}
413};
414
415U_BOOT_DRIVER(tilcdc) = {
416 .name = "tilcdc",
417 .id = UCLASS_VIDEO,
418 .of_match = tilcdc_ids,
419 .bind = tilcdc_bind,
420 .of_to_plat = tilcdc_of_to_plat,
421 .probe = tilcdc_probe,
422 .remove = tilcdc_remove,
423 .priv_auto = sizeof(struct tilcdc_priv)
424};