2 * Copyright 2014 Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
6 * Extracted from Chromium coreboot commit 3f59b13d
13 #include <displayport.h>
19 #include <asm/arch/clock.h>
20 #include <asm/arch/pwm.h>
21 #include <asm/arch-tegra/dc.h>
22 #include "displayport.h"
24 DECLARE_GLOBAL_DATA_PTR;
26 /* return in 1000ths of a Hertz */
27 static int tegra_dc_calc_refresh(const struct display_timing *timing)
29 int h_total, v_total, refresh;
30 int pclk = timing->pixelclock.typ;
32 h_total = timing->hactive.typ + timing->hfront_porch.typ +
33 timing->hback_porch.typ + timing->hsync_len.typ;
34 v_total = timing->vactive.typ + timing->vfront_porch.typ +
35 timing->vback_porch.typ + timing->vsync_len.typ;
36 if (!pclk || !h_total || !v_total)
38 refresh = pclk / h_total;
45 static void print_mode(const struct display_timing *timing)
47 int refresh = tegra_dc_calc_refresh(timing);
49 debug("MODE:%dx%d@%d.%03uHz pclk=%d\n",
50 timing->hactive.typ, timing->vactive.typ, refresh / 1000,
51 refresh % 1000, timing->pixelclock.typ);
54 static int update_display_mode(struct dc_ctlr *disp_ctrl,
55 const struct display_timing *timing,
56 int href_to_sync, int vref_to_sync)
60 writel(0x1, &disp_ctrl->disp.disp_timing_opt);
62 writel(vref_to_sync << 16 | href_to_sync,
63 &disp_ctrl->disp.ref_to_sync);
65 writel(timing->vsync_len.typ << 16 | timing->hsync_len.typ,
66 &disp_ctrl->disp.sync_width);
68 writel(((timing->vback_porch.typ - vref_to_sync) << 16) |
69 timing->hback_porch.typ, &disp_ctrl->disp.back_porch);
71 writel(((timing->vfront_porch.typ + vref_to_sync) << 16) |
72 timing->hfront_porch.typ, &disp_ctrl->disp.front_porch);
74 writel(timing->hactive.typ | (timing->vactive.typ << 16),
75 &disp_ctrl->disp.disp_active);
78 * We want to use PLLD_out0, which is PLLD / 2:
79 * PixelClock = (PLLD / 2) / ShiftClockDiv / PixelClockDiv.
81 * Currently most panels work inside clock range 50MHz~100MHz, and PLLD
82 * has some requirements to have VCO in range 500MHz~1000MHz (see
83 * clock.c for more detail). To simplify calculation, we set
84 * PixelClockDiv to 1 and ShiftClockDiv to 1. In future these values
85 * may be calculated by clock_display, to allow wider frequency range.
87 * Note ShiftClockDiv is a 7.1 format value.
89 const u32 shift_clock_div = 1;
90 writel((PIXEL_CLK_DIVIDER_PCD1 << PIXEL_CLK_DIVIDER_SHIFT) |
91 ((shift_clock_div - 1) * 2) << SHIFT_CLK_DIVIDER_SHIFT,
92 &disp_ctrl->disp.disp_clk_ctrl);
93 debug("%s: PixelClock=%u, ShiftClockDiv=%u\n", __func__,
94 timing->pixelclock.typ, shift_clock_div);
98 static u32 tegra_dc_poll_register(void *reg,
99 u32 mask, u32 exp_val, u32 poll_interval_us, u32 timeout_us)
101 u32 temp = timeout_us;
105 udelay(poll_interval_us);
106 reg_val = readl(reg);
107 if (timeout_us > poll_interval_us)
108 timeout_us -= poll_interval_us;
111 } while ((reg_val & mask) != exp_val);
113 if ((reg_val & mask) == exp_val)
114 return 0; /* success */
119 int tegra_dc_sor_general_act(struct dc_ctlr *disp_ctrl)
121 writel(GENERAL_ACT_REQ, &disp_ctrl->cmd.state_ctrl);
123 if (tegra_dc_poll_register(&disp_ctrl->cmd.state_ctrl,
124 GENERAL_ACT_REQ, 0, 100,
125 DC_POLL_TIMEOUT_MS * 1000)) {
126 debug("dc timeout waiting for DC to stop\n");
133 static struct display_timing min_mode = {
134 .hsync_len = { .typ = 1 },
135 .vsync_len = { .typ = 1 },
136 .hback_porch = { .typ = 20 },
137 .vback_porch = { .typ = 0 },
138 .hactive = { .typ = 16 },
139 .vactive = { .typ = 16 },
140 .hfront_porch = { .typ = 1 },
141 .vfront_porch = { .typ = 2 },
144 /* Disable windows and set minimum raster timings */
145 void tegra_dc_sor_disable_win_short_raster(struct dc_ctlr *disp_ctrl,
148 const int href_to_sync = 0, vref_to_sync = 1;
149 int selected_windows, i;
151 selected_windows = readl(&disp_ctrl->cmd.disp_win_header);
153 /* Store and clear window options */
154 for (i = 0; i < DC_N_WINDOWS; ++i) {
155 writel(WINDOW_A_SELECT << i, &disp_ctrl->cmd.disp_win_header);
156 dc_reg_ctx[i] = readl(&disp_ctrl->win.win_opt);
157 writel(0, &disp_ctrl->win.win_opt);
158 writel(WIN_A_ACT_REQ << i, &disp_ctrl->cmd.state_ctrl);
161 writel(selected_windows, &disp_ctrl->cmd.disp_win_header);
163 /* Store current raster timings and set minimum timings */
164 dc_reg_ctx[i++] = readl(&disp_ctrl->disp.ref_to_sync);
165 writel(href_to_sync | (vref_to_sync << 16),
166 &disp_ctrl->disp.ref_to_sync);
168 dc_reg_ctx[i++] = readl(&disp_ctrl->disp.sync_width);
169 writel(min_mode.hsync_len.typ | (min_mode.vsync_len.typ << 16),
170 &disp_ctrl->disp.sync_width);
172 dc_reg_ctx[i++] = readl(&disp_ctrl->disp.back_porch);
173 writel(min_mode.hback_porch.typ | (min_mode.vback_porch.typ << 16),
174 &disp_ctrl->disp.back_porch);
176 dc_reg_ctx[i++] = readl(&disp_ctrl->disp.front_porch);
177 writel(min_mode.hfront_porch.typ | (min_mode.vfront_porch.typ << 16),
178 &disp_ctrl->disp.front_porch);
180 dc_reg_ctx[i++] = readl(&disp_ctrl->disp.disp_active);
181 writel(min_mode.hactive.typ | (min_mode.vactive.typ << 16),
182 &disp_ctrl->disp.disp_active);
184 writel(GENERAL_ACT_REQ, &disp_ctrl->cmd.state_ctrl);
187 /* Restore previous windows status and raster timings */
188 void tegra_dc_sor_restore_win_and_raster(struct dc_ctlr *disp_ctrl,
191 int selected_windows, i;
193 selected_windows = readl(&disp_ctrl->cmd.disp_win_header);
195 for (i = 0; i < DC_N_WINDOWS; ++i) {
196 writel(WINDOW_A_SELECT << i, &disp_ctrl->cmd.disp_win_header);
197 writel(dc_reg_ctx[i], &disp_ctrl->win.win_opt);
198 writel(WIN_A_ACT_REQ << i, &disp_ctrl->cmd.state_ctrl);
201 writel(selected_windows, &disp_ctrl->cmd.disp_win_header);
203 writel(dc_reg_ctx[i++], &disp_ctrl->disp.ref_to_sync);
204 writel(dc_reg_ctx[i++], &disp_ctrl->disp.sync_width);
205 writel(dc_reg_ctx[i++], &disp_ctrl->disp.back_porch);
206 writel(dc_reg_ctx[i++], &disp_ctrl->disp.front_porch);
207 writel(dc_reg_ctx[i++], &disp_ctrl->disp.disp_active);
209 writel(GENERAL_UPDATE, &disp_ctrl->cmd.state_ctrl);
212 static int tegra_depth_for_bpp(int bpp)
216 return COLOR_DEPTH_R8G8B8A8;
218 return COLOR_DEPTH_B5G6R5;
220 debug("Unsupported LCD bit depth");
225 static int update_window(struct dc_ctlr *disp_ctrl,
226 u32 frame_buffer, int fb_bits_per_pixel,
227 const struct display_timing *timing)
229 const u32 colour_white = 0xffffff;
233 writel(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header);
235 writel(((timing->vactive.typ << 16) | timing->hactive.typ),
236 &disp_ctrl->win.size);
237 writel(((timing->vactive.typ << 16) |
238 (timing->hactive.typ * fb_bits_per_pixel / 8)),
239 &disp_ctrl->win.prescaled_size);
240 writel(((timing->hactive.typ * fb_bits_per_pixel / 8 + 31) /
241 32 * 32), &disp_ctrl->win.line_stride);
243 colour_depth = tegra_depth_for_bpp(fb_bits_per_pixel);
244 if (colour_depth == -1)
247 writel(colour_depth, &disp_ctrl->win.color_depth);
249 writel(frame_buffer, &disp_ctrl->winbuf.start_addr);
250 writel(0x1000 << V_DDA_INC_SHIFT | 0x1000 << H_DDA_INC_SHIFT,
251 &disp_ctrl->win.dda_increment);
253 writel(colour_white, &disp_ctrl->disp.blend_background_color);
254 writel(CTRL_MODE_C_DISPLAY << CTRL_MODE_SHIFT,
255 &disp_ctrl->cmd.disp_cmd);
257 writel(WRITE_MUX_ACTIVE, &disp_ctrl->cmd.state_access);
259 val = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
260 val |= GENERAL_UPDATE | WIN_A_UPDATE;
261 writel(val, &disp_ctrl->cmd.state_ctrl);
264 val = readl(&disp_ctrl->win.win_opt);
265 writel(val | WIN_ENABLE, &disp_ctrl->win.win_opt);
270 static int tegra_dc_init(struct dc_ctlr *disp_ctrl)
272 /* do not accept interrupts during initialization */
273 writel(0x00000000, &disp_ctrl->cmd.int_mask);
274 writel(WRITE_MUX_ASSEMBLY | READ_MUX_ASSEMBLY,
275 &disp_ctrl->cmd.state_access);
276 writel(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header);
277 writel(0x00000000, &disp_ctrl->win.win_opt);
278 writel(0x00000000, &disp_ctrl->win.byte_swap);
279 writel(0x00000000, &disp_ctrl->win.buffer_ctrl);
281 writel(0x00000000, &disp_ctrl->win.pos);
282 writel(0x00000000, &disp_ctrl->win.h_initial_dda);
283 writel(0x00000000, &disp_ctrl->win.v_initial_dda);
284 writel(0x00000000, &disp_ctrl->win.dda_increment);
285 writel(0x00000000, &disp_ctrl->win.dv_ctrl);
287 writel(0x01000000, &disp_ctrl->win.blend_layer_ctrl);
288 writel(0x00000000, &disp_ctrl->win.blend_match_select);
289 writel(0x00000000, &disp_ctrl->win.blend_nomatch_select);
290 writel(0x00000000, &disp_ctrl->win.blend_alpha_1bit);
292 writel(0x00000000, &disp_ctrl->winbuf.start_addr_hi);
293 writel(0x00000000, &disp_ctrl->winbuf.addr_h_offset);
294 writel(0x00000000, &disp_ctrl->winbuf.addr_v_offset);
296 writel(0x00000000, &disp_ctrl->com.crc_checksum);
297 writel(0x00000000, &disp_ctrl->com.pin_output_enb[0]);
298 writel(0x00000000, &disp_ctrl->com.pin_output_enb[1]);
299 writel(0x00000000, &disp_ctrl->com.pin_output_enb[2]);
300 writel(0x00000000, &disp_ctrl->com.pin_output_enb[3]);
301 writel(0x00000000, &disp_ctrl->disp.disp_signal_opt0);
306 static void dump_config(int panel_bpp, struct display_timing *timing)
308 printf("timing->hactive.typ = %d\n", timing->hactive.typ);
309 printf("timing->vactive.typ = %d\n", timing->vactive.typ);
310 printf("timing->pixelclock.typ = %d\n", timing->pixelclock.typ);
312 printf("timing->hfront_porch.typ = %d\n", timing->hfront_porch.typ);
313 printf("timing->hsync_len.typ = %d\n", timing->hsync_len.typ);
314 printf("timing->hback_porch.typ = %d\n", timing->hback_porch.typ);
316 printf("timing->vfront_porch.typ %d\n", timing->vfront_porch.typ);
317 printf("timing->vsync_len.typ = %d\n", timing->vsync_len.typ);
318 printf("timing->vback_porch.typ = %d\n", timing->vback_porch.typ);
320 printf("panel_bits_per_pixel = %d\n", panel_bpp);
323 static int display_update_config_from_edid(struct udevice *dp_dev,
325 struct display_timing *timing)
330 ret = display_port_read_edid(dp_dev, buf, sizeof(buf));
333 ret = edid_get_timing(buf, ret, timing, &bpc);
337 /* Use this information if valid */
339 *panel_bppp = bpc * 3;
344 /* Somewhat torturous method */
345 static int get_backlight_info(const void *blob, struct gpio_desc *vdd,
346 struct gpio_desc *enable, int *pwmp)
348 int sor, panel, backlight, power;
354 sor = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA124_SOR);
357 panel = fdtdec_lookup_phandle(blob, sor, "nvidia,panel");
360 backlight = fdtdec_lookup_phandle(blob, panel, "backlight");
363 ret = gpio_request_by_name_nodev(blob, backlight, "enable-gpios", 0,
364 enable, GPIOD_IS_OUT);
367 prop = fdt_getprop(blob, backlight, "pwms", &len);
368 if (!prop || len != 3 * sizeof(u32))
370 *pwmp = fdt32_to_cpu(prop[1]);
372 power = fdtdec_lookup_phandle(blob, backlight, "power-supply");
375 ret = gpio_request_by_name_nodev(blob, power, "gpio", 0, vdd,
383 dm_gpio_free(NULL, enable);
387 int display_init(void *lcdbase, int fb_bits_per_pixel,
388 struct display_timing *timing)
390 struct dc_ctlr *dc_ctlr;
391 const void *blob = gd->fdt_blob;
392 struct udevice *dp_dev;
393 const int href_to_sync = 1, vref_to_sync = 1;
394 int panel_bpp = 18; /* default 18 bits per pixel */
396 struct gpio_desc vdd_gpio, enable_gpio;
401 ret = uclass_get_device(UCLASS_DISPLAY_PORT, 0, &dp_dev);
405 node = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA124_DC);
408 dc_ctlr = (struct dc_ctlr *)fdtdec_get_addr(blob, node, "reg");
409 if (fdtdec_decode_display_timing(blob, node, 0, timing))
412 ret = display_update_config_from_edid(dp_dev, &panel_bpp, timing);
414 debug("%s: Failed to decode EDID, using defaults\n", __func__);
415 dump_config(panel_bpp, timing);
418 if (!get_backlight_info(blob, &vdd_gpio, &enable_gpio, &pwm)) {
419 dm_gpio_set_value(&vdd_gpio, 1);
420 debug("%s: backlight vdd setting gpio %08x to %d\n",
421 __func__, gpio_get_number(&vdd_gpio), 1);
425 * The plld is programmed with the assumption of the SHIFT_CLK_DIVIDER
426 * and PIXEL_CLK_DIVIDER are zero (divide by 1). See the
427 * update_display_mode() for detail.
429 plld_rate = clock_set_display_rate(timing->pixelclock.typ * 2);
430 if (plld_rate == 0) {
431 printf("dc: clock init failed\n");
433 } else if (plld_rate != timing->pixelclock.typ * 2) {
434 debug("dc: plld rounded to %u\n", plld_rate);
435 timing->pixelclock.typ = plld_rate / 2;
439 ret = tegra_dc_init(dc_ctlr);
441 debug("dc: init failed\n");
445 /* Configure dc mode */
446 ret = update_display_mode(dc_ctlr, timing, href_to_sync, vref_to_sync);
448 debug("dc: failed to configure display mode\n");
453 ret = display_port_enable(dp_dev, panel_bpp, timing);
457 ret = update_window(dc_ctlr, (ulong)lcdbase, fb_bits_per_pixel, timing);
461 /* Set up Tegra PWM to drive the panel backlight */
462 pwm_enable(pwm, 0, 220, 0x2e);
465 if (dm_gpio_is_valid(&enable_gpio)) {
466 dm_gpio_set_value(&enable_gpio, 1);
467 debug("%s: backlight enable setting gpio %08x to %d\n",
468 __func__, gpio_get_number(&enable_gpio), 1);