blob: c90e392e0e9ba078e312ffe8da49612207113f88 [file] [log] [blame]
Gabe Blackd40be112013-10-09 23:45:07 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2013 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Gabe Blackd40be112013-10-09 23:45:07 -070014 */
15
Gabe Blackd40be112013-10-09 23:45:07 -070016#include <arch/io.h>
Gabe Blackd40be112013-10-09 23:45:07 -070017#include <boot/tables.h>
18#include <cbmem.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -070019#include <console/console.h>
20#include <cpu/cpu.h>
21#include <delay.h>
22#include <device/device.h>
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080023#include <edid.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -070024#include <lib.h>
25#include <soc/addressmap.h>
Gabe Blackd40be112013-10-09 23:45:07 -070026#include <soc/clock.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -070027#include <soc/display.h>
28#include <soc/sdram.h>
Gabe Blackd40be112013-10-09 23:45:07 -070029#include <soc/nvidia/tegra/dc.h>
Andrew Chew7f0cb152014-02-10 16:44:18 -080030#include <soc/nvidia/tegra/pwm.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -070031#include <stdint.h>
32#include <stdlib.h>
33#include <stdlib.h>
34#include <string.h>
35
Gabe Blackd40be112013-10-09 23:45:07 -070036#include "chip.h"
Gabe Blackd40be112013-10-09 23:45:07 -070037
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070038struct tegra_dc dc_data;
39
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080040int dump = 0;
Elyes HAOUAS39303d52018-07-08 12:40:45 +020041unsigned long READL(void *p)
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080042{
Elyes HAOUAS88607a42018-10-05 10:36:45 +020043 unsigned long value;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070044
45 /*
46 * In case of hard hung on readl(p), we can set dump > 1 to print out
47 * the address accessed.
48 */
Elyes HAOUAS88607a42018-10-05 10:36:45 +020049 if (dump > 1)
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070050 printk(BIOS_SPEW, "readl %p\n", p);
51
Elyes HAOUAS88607a42018-10-05 10:36:45 +020052 value = read32(p);
53 if (dump)
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080054 printk(BIOS_SPEW, "readl %p %08lx\n", p, value);
Elyes HAOUAS88607a42018-10-05 10:36:45 +020055 return value;
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080056}
57
Elyes HAOUAS39303d52018-07-08 12:40:45 +020058void WRITEL(unsigned long value, void *p)
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080059{
Elyes HAOUAS88607a42018-10-05 10:36:45 +020060 if (dump)
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080061 printk(BIOS_SPEW, "writel %p %08lx\n", p, value);
Elyes HAOUAS88607a42018-10-05 10:36:45 +020062 write32(p, value);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080063}
Gabe Blackd40be112013-10-09 23:45:07 -070064
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070065/* return in 1000ths of a Hertz */
66static int tegra_dc_calc_refresh(const struct soc_nvidia_tegra124_config *config)
67{
68 int h_total, v_total, refresh;
69 int pclk = config->pixel_clock;
Gabe Blackd40be112013-10-09 23:45:07 -070070
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070071 h_total = config->xres + config->hfront_porch + config->hback_porch +
72 config->hsync_width;
73 v_total = config->yres + config->vfront_porch + config->vback_porch +
74 config->vsync_width;
75 if (!pclk || !h_total || !v_total)
76 return 0;
77 refresh = pclk / h_total;
78 refresh *= 1000;
79 refresh /= v_total;
80 return refresh;
81}
Gabe Blackd40be112013-10-09 23:45:07 -070082
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070083static void print_mode(const struct soc_nvidia_tegra124_config *config)
84{
85 if (config) {
86 int refresh = tegra_dc_calc_refresh(config);
87 printk(BIOS_ERR,
88 "MODE:%dx%d@%d.%03uHz pclk=%d\n",
89 config->xres, config->yres,
90 refresh / 1000, refresh % 1000,
91 config->pixel_clock);
92 }
93}
Gabe Blackd40be112013-10-09 23:45:07 -070094
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070095static int update_display_mode(struct display_controller *disp_ctrl,
Julius Werneredf6b572013-10-25 17:49:26 -070096 struct soc_nvidia_tegra124_config *config)
Gabe Blackd40be112013-10-09 23:45:07 -070097{
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070098 print_mode(config);
99
100 WRITEL(0x1, &disp_ctrl->disp.disp_timing_opt);
Gabe Blackd40be112013-10-09 23:45:07 -0700101
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800102 WRITEL(config->vref_to_sync << 16 | config->href_to_sync,
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700103 &disp_ctrl->disp.ref_to_sync);
Gabe Blackd40be112013-10-09 23:45:07 -0700104
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700105 WRITEL(config->vsync_width << 16 | config->hsync_width,
106 &disp_ctrl->disp.sync_width);
Gabe Blackd40be112013-10-09 23:45:07 -0700107
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700108 WRITEL(((config->vback_porch - config->vref_to_sync) << 16) | config->hback_porch,
109 &disp_ctrl->disp.back_porch);
Gabe Blackd40be112013-10-09 23:45:07 -0700110
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700111 WRITEL(((config->vfront_porch + config->vref_to_sync) << 16) | config->hfront_porch,
112 &disp_ctrl->disp.front_porch);
Gabe Blackd40be112013-10-09 23:45:07 -0700113
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700114 WRITEL(config->xres | (config->yres << 16),
115 &disp_ctrl->disp.disp_active);
Gabe Blackd40be112013-10-09 23:45:07 -0700116
Hung-Te Lin1a8e0af2014-04-08 20:03:40 +0800117 /**
118 * We want to use PLLD_out0, which is PLLD / 2:
119 * PixelClock = (PLLD / 2) / ShiftClockDiv / PixelClockDiv.
120 *
121 * Currently most panels work inside clock range 50MHz~100MHz, and PLLD
122 * has some requirements to have VCO in range 500MHz~1000MHz (see
123 * clock.c for more detail). To simplify calculation, we set
Ken Changcbae0de2014-04-18 13:52:48 +0800124 * PixelClockDiv to 1 and ShiftClockDiv to 1. In future these values
Hung-Te Lin1a8e0af2014-04-08 20:03:40 +0800125 * may be calculated by clock_display, to allow wider frequency range.
126 *
127 * Note ShiftClockDiv is a 7.1 format value.
128 */
Ken Changcbae0de2014-04-18 13:52:48 +0800129 const u32 shift_clock_div = 1;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700130 WRITEL((PIXEL_CLK_DIVIDER_PCD1 << PIXEL_CLK_DIVIDER_SHIFT) |
Hung-Te Lin1a8e0af2014-04-08 20:03:40 +0800131 ((shift_clock_div - 1) * 2) << SHIFT_CLK_DIVIDER_SHIFT,
132 &disp_ctrl->disp.disp_clk_ctrl);
133 printk(BIOS_DEBUG, "%s: PixelClock=%u, ShiftClockDiv=%u\n",
134 __func__, config->pixel_clock, shift_clock_div);
Vince Hsuc09642e2014-05-16 12:46:02 +0800135 return 0;
Gabe Blackd40be112013-10-09 23:45:07 -0700136}
137
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700138static void update_window(struct display_controller *disp_ctrl,
139 struct soc_nvidia_tegra124_config *config)
Gabe Blackd40be112013-10-09 23:45:07 -0700140{
Gabe Blackd40be112013-10-09 23:45:07 -0700141 u32 val;
142
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700143 WRITEL(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header);
Gabe Blackd40be112013-10-09 23:45:07 -0700144
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700145 WRITEL(((config->yres << 16) | config->xres), &disp_ctrl->win.size);
146 WRITEL(((config->yres << 16) |
147 (config->xres * config->framebuffer_bits_per_pixel / 8)),
148 &disp_ctrl->win.prescaled_size);
149 WRITEL(((config->xres * config->framebuffer_bits_per_pixel / 8 + 31) /
150 32 * 32), &disp_ctrl->win.line_stride);
Gabe Blackd40be112013-10-09 23:45:07 -0700151
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700152 WRITEL(config->color_depth, &disp_ctrl->win.color_depth);
Gabe Blackd40be112013-10-09 23:45:07 -0700153
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700154 WRITEL(config->framebuffer_base, &disp_ctrl->winbuf.start_addr);
155 WRITEL((V_DDA_INC(0x1000) | H_DDA_INC(0x1000)), &disp_ctrl->win.dda_increment);
Gabe Blackd40be112013-10-09 23:45:07 -0700156
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700157 WRITEL(COLOR_WHITE, &disp_ctrl->disp.blend_background_color);
158 WRITEL(DISP_CTRL_MODE_C_DISPLAY, &disp_ctrl->cmd.disp_cmd);
Gabe Blackd40be112013-10-09 23:45:07 -0700159
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700160 WRITEL(WRITE_MUX_ACTIVE, &disp_ctrl->cmd.state_access);
Gabe Blackd40be112013-10-09 23:45:07 -0700161
162 val = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
163 val |= GENERAL_UPDATE | WIN_A_UPDATE;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700164 WRITEL(val, &disp_ctrl->cmd.state_ctrl);
165
166 // Enable win_a
167 val = READL(&disp_ctrl->win.win_opt);
168 WRITEL(val | WIN_ENABLE, &disp_ctrl->win.win_opt);
169}
170
171static int tegra_dc_init(struct display_controller *disp_ctrl)
172{
173 /* do not accept interrupts during initialization */
174 WRITEL(0x00000000, &disp_ctrl->cmd.int_mask);
175 WRITEL(WRITE_MUX_ASSEMBLY | READ_MUX_ASSEMBLY,
176 &disp_ctrl->cmd.state_access);
177 WRITEL(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header);
178 WRITEL(0x00000000, &disp_ctrl->win.win_opt);
179 WRITEL(0x00000000, &disp_ctrl->win.byte_swap);
180 WRITEL(0x00000000, &disp_ctrl->win.buffer_ctrl);
181
182 WRITEL(0x00000000, &disp_ctrl->win.pos);
183 WRITEL(0x00000000, &disp_ctrl->win.h_initial_dda);
184 WRITEL(0x00000000, &disp_ctrl->win.v_initial_dda);
185 WRITEL(0x00000000, &disp_ctrl->win.dda_increment);
186 WRITEL(0x00000000, &disp_ctrl->win.dv_ctrl);
187
188 WRITEL(0x01000000, &disp_ctrl->win.blend_layer_ctrl);
189 WRITEL(0x00000000, &disp_ctrl->win.blend_match_select);
190 WRITEL(0x00000000, &disp_ctrl->win.blend_nomatch_select);
191 WRITEL(0x00000000, &disp_ctrl->win.blend_alpha_1bit);
192
193 WRITEL(0x00000000, &disp_ctrl->winbuf.start_addr_hi);
194 WRITEL(0x00000000, &disp_ctrl->winbuf.addr_h_offset);
195 WRITEL(0x00000000, &disp_ctrl->winbuf.addr_v_offset);
196
197 WRITEL(0x00000000, &disp_ctrl->com.crc_checksum);
198 WRITEL(0x00000000, &disp_ctrl->com.pin_output_enb[0]);
199 WRITEL(0x00000000, &disp_ctrl->com.pin_output_enb[1]);
200 WRITEL(0x00000000, &disp_ctrl->com.pin_output_enb[2]);
201 WRITEL(0x00000000, &disp_ctrl->com.pin_output_enb[3]);
202 WRITEL(0x00000000, &disp_ctrl->disp.disp_signal_opt0);
203
204 return 0;
Gabe Blackd40be112013-10-09 23:45:07 -0700205}
206
Tom Warren64982c502014-01-23 13:37:50 -0700207uint32_t fb_base_mb(void)
208{
Gabe Black5cbbc702014-02-08 05:17:38 -0800209 return sdram_max_addressable_mb() - FB_SIZE_MB;
Tom Warren64982c502014-01-23 13:37:50 -0700210}
211
Gabe Blackd40be112013-10-09 23:45:07 -0700212/* this is really aimed at the lcd panel. That said, there are two display
213 * devices on this part and we may someday want to extend it for other boards.
214 */
Elyes HAOUAS3fcb2182018-05-25 10:03:57 +0200215void display_startup(struct device *dev)
Gabe Blackd40be112013-10-09 23:45:07 -0700216{
Gabe Blackd40be112013-10-09 23:45:07 -0700217 struct soc_nvidia_tegra124_config *config = dev->chip_info;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700218 struct display_controller *disp_ctrl = (void *)config->display_controller;
Elyes HAOUAS05498a22018-05-28 16:26:43 +0200219 struct pwm_controller *pwm = (void *)TEGRA_PWM_BASE;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700220 struct tegra_dc *dc = &dc_data;
Vince Hsu1e3679d2014-06-11 17:14:05 +0800221 u32 plld_rate;
Gabe Blackd40be112013-10-09 23:45:07 -0700222
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700223 /* init dc */
224 dc->base = (void *)TEGRA_ARM_DISPLAYA;
225 dc->config = config;
226 config->dc_data = dc;
227
Hung-Te Lin3af0d312014-04-02 21:57:40 +0800228 /* Note dp_init may read EDID and change some config values. */
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700229 dp_init(config);
230
Hung-Te Lin3af0d312014-04-02 21:57:40 +0800231 /* should probably just make it all MiB ... in future */
232 u32 framebuffer_size_mb = config->framebuffer_size / MiB;
233 u32 framebuffer_base_mb= config->framebuffer_base / MiB;
234
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800235 /* light it all up */
236 /* This one may have been done in romstage but that's ok for now. */
237 if (config->panel_vdd_gpio){
238 gpio_output(config->panel_vdd_gpio, 1);
239 printk(BIOS_SPEW,"%s: panel_vdd setting gpio %08x to %d\n",
240 __func__, config->panel_vdd_gpio, 1);
241 }
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700242 udelay(config->vdd_delay_ms * 1000);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800243 if (config->backlight_vdd_gpio){
244 gpio_output(config->backlight_vdd_gpio, 1);
245 printk(BIOS_SPEW,"%s: backlight vdd setting gpio %08x to %d\n",
246 __func__, config->backlight_vdd_gpio, 1);
247 }
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800248 if (config->lvds_shutdown_gpio){
249 gpio_output(config->lvds_shutdown_gpio, 0);
250 printk(BIOS_SPEW,"%s: lvds shutdown setting gpio %08x to %d\n",
251 __func__, config->lvds_shutdown_gpio, 0);
252 }
Andrew Chew7f0cb152014-02-10 16:44:18 -0800253
Gabe Blackd40be112013-10-09 23:45:07 -0700254 if (framebuffer_size_mb == 0){
255 framebuffer_size_mb = ALIGN_UP(config->xres * config->yres *
256 (config->framebuffer_bits_per_pixel / 8), MiB)/MiB;
257 }
258
259 if (! framebuffer_base_mb)
Tom Warren64982c502014-01-23 13:37:50 -0700260 framebuffer_base_mb = fb_base_mb();
Gabe Blackd40be112013-10-09 23:45:07 -0700261
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700262 config->framebuffer_size = framebuffer_size_mb * MiB;
263 config->framebuffer_base = framebuffer_base_mb * MiB;
264
Gabe Blackd40be112013-10-09 23:45:07 -0700265 mmu_config_range(framebuffer_base_mb, framebuffer_size_mb,
Aaron Durbin9cbc90a2016-07-25 11:30:43 -0500266 DCACHE_WRITETHROUGH);
Gabe Blackd40be112013-10-09 23:45:07 -0700267
Gabe Blackd40be112013-10-09 23:45:07 -0700268 printk(BIOS_SPEW, "LCD frame buffer at %dMiB to %dMiB\n", framebuffer_base_mb,
269 framebuffer_base_mb + framebuffer_size_mb);
270
271 /* GPIO magic here if needed to start powering up things. You
272 * really only want to enable vdd, wait a bit, and then enable
273 * the panel. However ... the timings in the tegra20 dts make
274 * no sense to me. I'm pretty sure they're wrong.
275 * The panel_vdd is done in the romstage, so we need only
276 * light things up here once we're sure it's all working.
277 */
Gabe Blackd40be112013-10-09 23:45:07 -0700278
Vince Hsuc09642e2014-05-16 12:46:02 +0800279 /* The plld is programmed with the assumption of the SHIFT_CLK_DIVIDER
280 * and PIXEL_CLK_DIVIDER are zero (divide by 1). See the
281 * update_display_mode() for detail.
282 */
Vince Hsu1e3679d2014-06-11 17:14:05 +0800283 plld_rate = clock_display(config->pixel_clock * 2);
284 if (plld_rate == 0) {
Vince Hsuc09642e2014-05-16 12:46:02 +0800285 printk(BIOS_ERR, "dc: clock init failed\n");
286 return;
Vince Hsu1e3679d2014-06-11 17:14:05 +0800287 } else if (plld_rate != config->pixel_clock * 2) {
288 printk(BIOS_WARNING, "dc: plld rounded to %u\n", plld_rate);
289 config->pixel_clock = plld_rate / 2;
290 }
Vince Hsuc09642e2014-05-16 12:46:02 +0800291
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700292 /* Init dc */
293 if (tegra_dc_init(disp_ctrl)) {
294 printk(BIOS_ERR, "dc: init failed\n");
295 return;
296 }
Gabe Blackd40be112013-10-09 23:45:07 -0700297
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700298 /* Configure dc mode */
Hung-Te Lin1a8e0af2014-04-08 20:03:40 +0800299 if (update_display_mode(disp_ctrl, config)) {
300 printk(BIOS_ERR, "dc: failed to configure display mode.\n");
301 return;
302 }
Gabe Blackd40be112013-10-09 23:45:07 -0700303
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700304 /* Enable dp */
305 dp_enable(dc->out);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800306
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700307 /* Init frame buffer */
Andrew Bresticker24d4f7f2013-12-18 22:41:34 -0800308 memset((void *)(framebuffer_base_mb*MiB), 0x00,
309 framebuffer_size_mb*MiB);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800310
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700311 update_window(disp_ctrl, config);
312
Ken Chang5a056d32014-04-22 12:55:00 +0800313 /* Set up Tegra PWM n (where n is specified in config->pwm) to drive the
314 * panel backlight.
315 */
316 printk(BIOS_SPEW, "%s: enable panel backlight pwm\n", __func__);
317 WRITEL(((1 << NV_PWM_CSR_ENABLE_SHIFT) |
318 (220 << NV_PWM_CSR_PULSE_WIDTH_SHIFT) | /* 220/256 */
319 0x02e), /* frequency divider */
320 &pwm->pwm[config->pwm].csr);
321
322 udelay(config->pwm_to_bl_delay_ms * 1000);
323 if (config->backlight_en_gpio){
324 gpio_output(config->backlight_en_gpio, 1);
325 printk(BIOS_SPEW,"%s: backlight enable setting gpio %08x to %d\n",
326 __func__, config->backlight_en_gpio, 1);
327 }
328
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700329 printk(BIOS_INFO, "%s: display init done.\n", __func__);
330
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800331 /* tell depthcharge ...
332 */
333 struct edid edid;
Julius Werner69112192016-03-14 17:29:55 -0700334 edid.mode.va = config->yres;
335 edid.mode.ha = config->xres;
336 edid_set_framebuffer_bits_per_pixel(&edid,
Paul Kocialkowskibc141de2016-05-14 15:25:51 +0200337 config->framebuffer_bits_per_pixel, 32);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800338 set_vbe_mode_info_valid(&edid, (uintptr_t)(framebuffer_base_mb*MiB));
Gabe Blackd40be112013-10-09 23:45:07 -0700339}