blob: 485e032c192111536cadc722f3553366c47171f9 [file] [log] [blame]
Angel Ponsa2ee7612020-04-04 18:51:15 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Gabe Blackd40be112013-10-09 23:45:07 -07002
Kyösti Mälkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Gabe Blackd40be112013-10-09 23:45:07 -07004#include <boot/tables.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -07005#include <console/console.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -07006#include <delay.h>
7#include <device/device.h>
Hung-Te Lin2fc3b622013-10-21 21:43:03 +08008#include <edid.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -07009#include <soc/addressmap.h>
Gabe Blackd40be112013-10-09 23:45:07 -070010#include <soc/clock.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -070011#include <soc/display.h>
12#include <soc/sdram.h>
Gabe Blackd40be112013-10-09 23:45:07 -070013#include <soc/nvidia/tegra/dc.h>
Andrew Chew7f0cb152014-02-10 16:44:18 -080014#include <soc/nvidia/tegra/pwm.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -070015#include <stdint.h>
Julius Wernerf0d21ff32014-10-20 13:24:14 -070016#include <string.h>
17
Gabe Blackd40be112013-10-09 23:45:07 -070018#include "chip.h"
Gabe Blackd40be112013-10-09 23:45:07 -070019
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070020struct tegra_dc dc_data;
21
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080022int dump = 0;
Elyes HAOUAS39303d52018-07-08 12:40:45 +020023unsigned long READL(void *p)
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080024{
Elyes HAOUAS88607a42018-10-05 10:36:45 +020025 unsigned long value;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070026
27 /*
28 * In case of hard hung on readl(p), we can set dump > 1 to print out
29 * the address accessed.
30 */
Elyes HAOUAS88607a42018-10-05 10:36:45 +020031 if (dump > 1)
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070032 printk(BIOS_SPEW, "readl %p\n", p);
33
Elyes HAOUAS88607a42018-10-05 10:36:45 +020034 value = read32(p);
35 if (dump)
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080036 printk(BIOS_SPEW, "readl %p %08lx\n", p, value);
Elyes HAOUAS88607a42018-10-05 10:36:45 +020037 return value;
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080038}
39
Elyes HAOUAS39303d52018-07-08 12:40:45 +020040void WRITEL(unsigned long value, void *p)
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080041{
Elyes HAOUAS88607a42018-10-05 10:36:45 +020042 if (dump)
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080043 printk(BIOS_SPEW, "writel %p %08lx\n", p, value);
Elyes HAOUAS88607a42018-10-05 10:36:45 +020044 write32(p, value);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080045}
Gabe Blackd40be112013-10-09 23:45:07 -070046
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070047/* return in 1000ths of a Hertz */
48static int tegra_dc_calc_refresh(const struct soc_nvidia_tegra124_config *config)
49{
50 int h_total, v_total, refresh;
51 int pclk = config->pixel_clock;
Gabe Blackd40be112013-10-09 23:45:07 -070052
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070053 h_total = config->xres + config->hfront_porch + config->hback_porch +
54 config->hsync_width;
55 v_total = config->yres + config->vfront_porch + config->vback_porch +
56 config->vsync_width;
57 if (!pclk || !h_total || !v_total)
58 return 0;
59 refresh = pclk / h_total;
60 refresh *= 1000;
61 refresh /= v_total;
62 return refresh;
63}
Gabe Blackd40be112013-10-09 23:45:07 -070064
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070065static void print_mode(const struct soc_nvidia_tegra124_config *config)
66{
67 if (config) {
68 int refresh = tegra_dc_calc_refresh(config);
69 printk(BIOS_ERR,
70 "MODE:%dx%d@%d.%03uHz pclk=%d\n",
71 config->xres, config->yres,
72 refresh / 1000, refresh % 1000,
73 config->pixel_clock);
74 }
75}
Gabe Blackd40be112013-10-09 23:45:07 -070076
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070077static int update_display_mode(struct display_controller *disp_ctrl,
Julius Werneredf6b572013-10-25 17:49:26 -070078 struct soc_nvidia_tegra124_config *config)
Gabe Blackd40be112013-10-09 23:45:07 -070079{
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070080 print_mode(config);
81
82 WRITEL(0x1, &disp_ctrl->disp.disp_timing_opt);
Gabe Blackd40be112013-10-09 23:45:07 -070083
Hung-Te Lin2fc3b622013-10-21 21:43:03 +080084 WRITEL(config->vref_to_sync << 16 | config->href_to_sync,
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070085 &disp_ctrl->disp.ref_to_sync);
Gabe Blackd40be112013-10-09 23:45:07 -070086
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070087 WRITEL(config->vsync_width << 16 | config->hsync_width,
88 &disp_ctrl->disp.sync_width);
Gabe Blackd40be112013-10-09 23:45:07 -070089
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070090 WRITEL(((config->vback_porch - config->vref_to_sync) << 16) | config->hback_porch,
91 &disp_ctrl->disp.back_porch);
Gabe Blackd40be112013-10-09 23:45:07 -070092
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070093 WRITEL(((config->vfront_porch + config->vref_to_sync) << 16) | config->hfront_porch,
94 &disp_ctrl->disp.front_porch);
Gabe Blackd40be112013-10-09 23:45:07 -070095
Jimmy Zhangbd5925a2014-03-10 12:42:05 -070096 WRITEL(config->xres | (config->yres << 16),
97 &disp_ctrl->disp.disp_active);
Gabe Blackd40be112013-10-09 23:45:07 -070098
Hung-Te Lin1a8e0af2014-04-08 20:03:40 +080099 /**
100 * We want to use PLLD_out0, which is PLLD / 2:
101 * PixelClock = (PLLD / 2) / ShiftClockDiv / PixelClockDiv.
102 *
103 * Currently most panels work inside clock range 50MHz~100MHz, and PLLD
104 * has some requirements to have VCO in range 500MHz~1000MHz (see
105 * clock.c for more detail). To simplify calculation, we set
Ken Changcbae0de2014-04-18 13:52:48 +0800106 * PixelClockDiv to 1 and ShiftClockDiv to 1. In future these values
Hung-Te Lin1a8e0af2014-04-08 20:03:40 +0800107 * may be calculated by clock_display, to allow wider frequency range.
108 *
109 * Note ShiftClockDiv is a 7.1 format value.
110 */
Ken Changcbae0de2014-04-18 13:52:48 +0800111 const u32 shift_clock_div = 1;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700112 WRITEL((PIXEL_CLK_DIVIDER_PCD1 << PIXEL_CLK_DIVIDER_SHIFT) |
Hung-Te Lin1a8e0af2014-04-08 20:03:40 +0800113 ((shift_clock_div - 1) * 2) << SHIFT_CLK_DIVIDER_SHIFT,
114 &disp_ctrl->disp.disp_clk_ctrl);
115 printk(BIOS_DEBUG, "%s: PixelClock=%u, ShiftClockDiv=%u\n",
116 __func__, config->pixel_clock, shift_clock_div);
Vince Hsuc09642e2014-05-16 12:46:02 +0800117 return 0;
Gabe Blackd40be112013-10-09 23:45:07 -0700118}
119
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700120static void update_window(struct display_controller *disp_ctrl,
121 struct soc_nvidia_tegra124_config *config)
Gabe Blackd40be112013-10-09 23:45:07 -0700122{
Gabe Blackd40be112013-10-09 23:45:07 -0700123 u32 val;
124
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700125 WRITEL(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header);
Gabe Blackd40be112013-10-09 23:45:07 -0700126
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700127 WRITEL(((config->yres << 16) | config->xres), &disp_ctrl->win.size);
128 WRITEL(((config->yres << 16) |
129 (config->xres * config->framebuffer_bits_per_pixel / 8)),
130 &disp_ctrl->win.prescaled_size);
131 WRITEL(((config->xres * config->framebuffer_bits_per_pixel / 8 + 31) /
132 32 * 32), &disp_ctrl->win.line_stride);
Gabe Blackd40be112013-10-09 23:45:07 -0700133
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700134 WRITEL(config->color_depth, &disp_ctrl->win.color_depth);
Gabe Blackd40be112013-10-09 23:45:07 -0700135
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700136 WRITEL(config->framebuffer_base, &disp_ctrl->winbuf.start_addr);
137 WRITEL((V_DDA_INC(0x1000) | H_DDA_INC(0x1000)), &disp_ctrl->win.dda_increment);
Gabe Blackd40be112013-10-09 23:45:07 -0700138
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700139 WRITEL(COLOR_WHITE, &disp_ctrl->disp.blend_background_color);
140 WRITEL(DISP_CTRL_MODE_C_DISPLAY, &disp_ctrl->cmd.disp_cmd);
Gabe Blackd40be112013-10-09 23:45:07 -0700141
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700142 WRITEL(WRITE_MUX_ACTIVE, &disp_ctrl->cmd.state_access);
Gabe Blackd40be112013-10-09 23:45:07 -0700143
144 val = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
145 val |= GENERAL_UPDATE | WIN_A_UPDATE;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700146 WRITEL(val, &disp_ctrl->cmd.state_ctrl);
147
148 // Enable win_a
149 val = READL(&disp_ctrl->win.win_opt);
150 WRITEL(val | WIN_ENABLE, &disp_ctrl->win.win_opt);
151}
152
153static int tegra_dc_init(struct display_controller *disp_ctrl)
154{
155 /* do not accept interrupts during initialization */
156 WRITEL(0x00000000, &disp_ctrl->cmd.int_mask);
157 WRITEL(WRITE_MUX_ASSEMBLY | READ_MUX_ASSEMBLY,
158 &disp_ctrl->cmd.state_access);
159 WRITEL(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header);
160 WRITEL(0x00000000, &disp_ctrl->win.win_opt);
161 WRITEL(0x00000000, &disp_ctrl->win.byte_swap);
162 WRITEL(0x00000000, &disp_ctrl->win.buffer_ctrl);
163
164 WRITEL(0x00000000, &disp_ctrl->win.pos);
165 WRITEL(0x00000000, &disp_ctrl->win.h_initial_dda);
166 WRITEL(0x00000000, &disp_ctrl->win.v_initial_dda);
167 WRITEL(0x00000000, &disp_ctrl->win.dda_increment);
168 WRITEL(0x00000000, &disp_ctrl->win.dv_ctrl);
169
170 WRITEL(0x01000000, &disp_ctrl->win.blend_layer_ctrl);
171 WRITEL(0x00000000, &disp_ctrl->win.blend_match_select);
172 WRITEL(0x00000000, &disp_ctrl->win.blend_nomatch_select);
173 WRITEL(0x00000000, &disp_ctrl->win.blend_alpha_1bit);
174
175 WRITEL(0x00000000, &disp_ctrl->winbuf.start_addr_hi);
176 WRITEL(0x00000000, &disp_ctrl->winbuf.addr_h_offset);
177 WRITEL(0x00000000, &disp_ctrl->winbuf.addr_v_offset);
178
179 WRITEL(0x00000000, &disp_ctrl->com.crc_checksum);
180 WRITEL(0x00000000, &disp_ctrl->com.pin_output_enb[0]);
181 WRITEL(0x00000000, &disp_ctrl->com.pin_output_enb[1]);
182 WRITEL(0x00000000, &disp_ctrl->com.pin_output_enb[2]);
183 WRITEL(0x00000000, &disp_ctrl->com.pin_output_enb[3]);
184 WRITEL(0x00000000, &disp_ctrl->disp.disp_signal_opt0);
185
186 return 0;
Gabe Blackd40be112013-10-09 23:45:07 -0700187}
188
Tom Warren64982c502014-01-23 13:37:50 -0700189uint32_t fb_base_mb(void)
190{
Gabe Black5cbbc702014-02-08 05:17:38 -0800191 return sdram_max_addressable_mb() - FB_SIZE_MB;
Tom Warren64982c502014-01-23 13:37:50 -0700192}
193
Gabe Blackd40be112013-10-09 23:45:07 -0700194/* this is really aimed at the lcd panel. That said, there are two display
195 * devices on this part and we may someday want to extend it for other boards.
196 */
Elyes HAOUAS3fcb2182018-05-25 10:03:57 +0200197void display_startup(struct device *dev)
Gabe Blackd40be112013-10-09 23:45:07 -0700198{
Gabe Blackd40be112013-10-09 23:45:07 -0700199 struct soc_nvidia_tegra124_config *config = dev->chip_info;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700200 struct display_controller *disp_ctrl = (void *)config->display_controller;
Elyes HAOUAS05498a22018-05-28 16:26:43 +0200201 struct pwm_controller *pwm = (void *)TEGRA_PWM_BASE;
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700202 struct tegra_dc *dc = &dc_data;
Vince Hsu1e3679d2014-06-11 17:14:05 +0800203 u32 plld_rate;
Gabe Blackd40be112013-10-09 23:45:07 -0700204
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700205 /* init dc */
206 dc->base = (void *)TEGRA_ARM_DISPLAYA;
207 dc->config = config;
208 config->dc_data = dc;
209
Hung-Te Lin3af0d312014-04-02 21:57:40 +0800210 /* Note dp_init may read EDID and change some config values. */
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700211 dp_init(config);
212
Hung-Te Lin3af0d312014-04-02 21:57:40 +0800213 /* should probably just make it all MiB ... in future */
214 u32 framebuffer_size_mb = config->framebuffer_size / MiB;
215 u32 framebuffer_base_mb= config->framebuffer_base / MiB;
216
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800217 /* light it all up */
218 /* This one may have been done in romstage but that's ok for now. */
219 if (config->panel_vdd_gpio){
220 gpio_output(config->panel_vdd_gpio, 1);
221 printk(BIOS_SPEW,"%s: panel_vdd setting gpio %08x to %d\n",
222 __func__, config->panel_vdd_gpio, 1);
223 }
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700224 udelay(config->vdd_delay_ms * 1000);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800225 if (config->backlight_vdd_gpio){
226 gpio_output(config->backlight_vdd_gpio, 1);
227 printk(BIOS_SPEW,"%s: backlight vdd setting gpio %08x to %d\n",
228 __func__, config->backlight_vdd_gpio, 1);
229 }
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800230 if (config->lvds_shutdown_gpio){
231 gpio_output(config->lvds_shutdown_gpio, 0);
232 printk(BIOS_SPEW,"%s: lvds shutdown setting gpio %08x to %d\n",
233 __func__, config->lvds_shutdown_gpio, 0);
234 }
Andrew Chew7f0cb152014-02-10 16:44:18 -0800235
Gabe Blackd40be112013-10-09 23:45:07 -0700236 if (framebuffer_size_mb == 0){
237 framebuffer_size_mb = ALIGN_UP(config->xres * config->yres *
238 (config->framebuffer_bits_per_pixel / 8), MiB)/MiB;
239 }
240
241 if (! framebuffer_base_mb)
Tom Warren64982c502014-01-23 13:37:50 -0700242 framebuffer_base_mb = fb_base_mb();
Gabe Blackd40be112013-10-09 23:45:07 -0700243
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700244 config->framebuffer_size = framebuffer_size_mb * MiB;
245 config->framebuffer_base = framebuffer_base_mb * MiB;
246
Gabe Blackd40be112013-10-09 23:45:07 -0700247 mmu_config_range(framebuffer_base_mb, framebuffer_size_mb,
Aaron Durbin9cbc90a2016-07-25 11:30:43 -0500248 DCACHE_WRITETHROUGH);
Gabe Blackd40be112013-10-09 23:45:07 -0700249
Gabe Blackd40be112013-10-09 23:45:07 -0700250 printk(BIOS_SPEW, "LCD frame buffer at %dMiB to %dMiB\n", framebuffer_base_mb,
251 framebuffer_base_mb + framebuffer_size_mb);
252
253 /* GPIO magic here if needed to start powering up things. You
254 * really only want to enable vdd, wait a bit, and then enable
255 * the panel. However ... the timings in the tegra20 dts make
256 * no sense to me. I'm pretty sure they're wrong.
257 * The panel_vdd is done in the romstage, so we need only
258 * light things up here once we're sure it's all working.
259 */
Gabe Blackd40be112013-10-09 23:45:07 -0700260
Vince Hsuc09642e2014-05-16 12:46:02 +0800261 /* The plld is programmed with the assumption of the SHIFT_CLK_DIVIDER
262 * and PIXEL_CLK_DIVIDER are zero (divide by 1). See the
263 * update_display_mode() for detail.
264 */
Vince Hsu1e3679d2014-06-11 17:14:05 +0800265 plld_rate = clock_display(config->pixel_clock * 2);
266 if (plld_rate == 0) {
Vince Hsuc09642e2014-05-16 12:46:02 +0800267 printk(BIOS_ERR, "dc: clock init failed\n");
268 return;
Vince Hsu1e3679d2014-06-11 17:14:05 +0800269 } else if (plld_rate != config->pixel_clock * 2) {
270 printk(BIOS_WARNING, "dc: plld rounded to %u\n", plld_rate);
271 config->pixel_clock = plld_rate / 2;
272 }
Vince Hsuc09642e2014-05-16 12:46:02 +0800273
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700274 /* Init dc */
275 if (tegra_dc_init(disp_ctrl)) {
276 printk(BIOS_ERR, "dc: init failed\n");
277 return;
278 }
Gabe Blackd40be112013-10-09 23:45:07 -0700279
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700280 /* Configure dc mode */
Hung-Te Lin1a8e0af2014-04-08 20:03:40 +0800281 if (update_display_mode(disp_ctrl, config)) {
282 printk(BIOS_ERR, "dc: failed to configure display mode.\n");
283 return;
284 }
Gabe Blackd40be112013-10-09 23:45:07 -0700285
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700286 /* Enable dp */
287 dp_enable(dc->out);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800288
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700289 /* Init frame buffer */
Andrew Bresticker24d4f7f2013-12-18 22:41:34 -0800290 memset((void *)(framebuffer_base_mb*MiB), 0x00,
291 framebuffer_size_mb*MiB);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800292
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700293 update_window(disp_ctrl, config);
294
Ken Chang5a056d32014-04-22 12:55:00 +0800295 /* Set up Tegra PWM n (where n is specified in config->pwm) to drive the
296 * panel backlight.
297 */
298 printk(BIOS_SPEW, "%s: enable panel backlight pwm\n", __func__);
299 WRITEL(((1 << NV_PWM_CSR_ENABLE_SHIFT) |
300 (220 << NV_PWM_CSR_PULSE_WIDTH_SHIFT) | /* 220/256 */
301 0x02e), /* frequency divider */
302 &pwm->pwm[config->pwm].csr);
303
304 udelay(config->pwm_to_bl_delay_ms * 1000);
305 if (config->backlight_en_gpio){
306 gpio_output(config->backlight_en_gpio, 1);
307 printk(BIOS_SPEW,"%s: backlight enable setting gpio %08x to %d\n",
308 __func__, config->backlight_en_gpio, 1);
309 }
310
Jimmy Zhangbd5925a2014-03-10 12:42:05 -0700311 printk(BIOS_INFO, "%s: display init done.\n", __func__);
312
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800313 /* tell depthcharge ...
314 */
315 struct edid edid;
Julius Werner69112192016-03-14 17:29:55 -0700316 edid.mode.va = config->yres;
317 edid.mode.ha = config->xres;
318 edid_set_framebuffer_bits_per_pixel(&edid,
Paul Kocialkowskibc141de2016-05-14 15:25:51 +0200319 config->framebuffer_bits_per_pixel, 32);
Hung-Te Lin2fc3b622013-10-21 21:43:03 +0800320 set_vbe_mode_info_valid(&edid, (uintptr_t)(framebuffer_base_mb*MiB));
Gabe Blackd40be112013-10-09 23:45:07 -0700321}