1 // SPDX-License-Identifier: GPL-2.0+
6 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
8 * Copyright (C) 2008-2009 MontaVista Software Inc.
9 * Copyright (C) 2008-2009 Texas Instruments Inc
11 * Based on the LCD driver for TI Avalanche processors written by
12 * Ajay Singh and Shalom Hai.
18 #include <linux/list.h>
21 #include <linux/errno.h>
23 #include <asm/arch/hardware.h>
25 #include "videomodes.h"
28 #if !defined(DA8XX_LCD_CNTL_BASE)
29 #define DA8XX_LCD_CNTL_BASE DAVINCI_LCD_CNTL_BASE
32 #define DRIVER_NAME "da8xx_lcdc"
34 #define LCD_VERSION_1 1
35 #define LCD_VERSION_2 2
37 /* LCD Status Register */
38 #define LCD_END_OF_FRAME1 (1 << 9)
39 #define LCD_END_OF_FRAME0 (1 << 8)
40 #define LCD_PL_LOAD_DONE (1 << 6)
41 #define LCD_FIFO_UNDERFLOW (1 << 5)
42 #define LCD_SYNC_LOST (1 << 2)
44 /* LCD DMA Control Register */
45 #define LCD_DMA_BURST_SIZE(x) ((x) << 4)
46 #define LCD_DMA_BURST_1 0x0
47 #define LCD_DMA_BURST_2 0x1
48 #define LCD_DMA_BURST_4 0x2
49 #define LCD_DMA_BURST_8 0x3
50 #define LCD_DMA_BURST_16 0x4
51 #define LCD_V1_END_OF_FRAME_INT_ENA (1 << 2)
52 #define LCD_V2_END_OF_FRAME0_INT_ENA (1 << 8)
53 #define LCD_V2_END_OF_FRAME1_INT_ENA (1 << 9)
54 #define LCD_DUAL_FRAME_BUFFER_ENABLE (1 << 0)
56 #define LCD_V2_TFT_24BPP_MODE (1 << 25)
57 #define LCD_V2_TFT_24BPP_UNPACK (1 << 26)
59 /* LCD Control Register */
60 #define LCD_CLK_DIVISOR(x) ((x) << 8)
61 #define LCD_RASTER_MODE 0x01
63 /* LCD Raster Control Register */
64 #define LCD_PALETTE_LOAD_MODE(x) ((x) << 20)
65 #define PALETTE_AND_DATA 0x00
66 #define PALETTE_ONLY 0x01
67 #define DATA_ONLY 0x02
69 #define LCD_MONO_8BIT_MODE (1 << 9)
70 #define LCD_RASTER_ORDER (1 << 8)
71 #define LCD_TFT_MODE (1 << 7)
72 #define LCD_V1_UNDERFLOW_INT_ENA (1 << 6)
73 #define LCD_V2_UNDERFLOW_INT_ENA (1 << 5)
74 #define LCD_V1_PL_INT_ENA (1 << 4)
75 #define LCD_V2_PL_INT_ENA (1 << 6)
76 #define LCD_MONOCHROME_MODE (1 << 1)
77 #define LCD_RASTER_ENABLE (1 << 0)
78 #define LCD_TFT_ALT_ENABLE (1 << 23)
79 #define LCD_STN_565_ENABLE (1 << 24)
80 #define LCD_V2_DMA_CLK_EN (1 << 2)
81 #define LCD_V2_LIDD_CLK_EN (1 << 1)
82 #define LCD_V2_CORE_CLK_EN (1 << 0)
83 #define LCD_V2_LPP_B10 26
84 #define LCD_V2_TFT_24BPP_MODE (1 << 25)
85 #define LCD_V2_TFT_24BPP_UNPACK (1 << 26)
87 /* LCD Raster Timing 2 Register */
88 #define LCD_AC_BIAS_TRANSITIONS_PER_INT(x) ((x) << 16)
89 #define LCD_AC_BIAS_FREQUENCY(x) ((x) << 8)
90 #define LCD_SYNC_CTRL (1 << 25)
91 #define LCD_SYNC_EDGE (1 << 24)
92 #define LCD_INVERT_PIXEL_CLOCK (1 << 22)
93 #define LCD_INVERT_LINE_CLOCK (1 << 21)
94 #define LCD_INVERT_FRAME_CLOCK (1 << 20)
96 /* Clock registers available only on Version 2 */
97 #define LCD_CLK_MAIN_RESET (1 << 3)
99 struct da8xx_lcd_regs {
117 u32 dma_frm_buf_base_addr_0;
118 u32 dma_frm_buf_ceiling_addr_0;
119 u32 dma_frm_buf_base_addr_1;
120 u32 dma_frm_buf_ceiling_addr_1;
127 /* Clock registers available only on Version 2 */
132 #define LCD_NUM_BUFFERS 1
134 #define WSI_TIMEOUT 50
135 #define PALETTE_SIZE 256
136 #define LEFT_MARGIN 64
137 #define RIGHT_MARGIN 64
138 #define UPPER_MARGIN 32
139 #define LOWER_MARGIN 32
140 #define WAIT_FOR_FRAME_DONE true
141 #define NO_WAIT_FOR_FRAME_DONE false
143 #define calc_fbsize() (panel.plnSizeX * panel.plnSizeY * panel.gdfBytesPP)
145 static struct da8xx_lcd_regs *da8xx_fb_reg_base;
147 DECLARE_GLOBAL_DATA_PTR;
150 static GraphicDevice gpanel;
151 static const struct da8xx_panel *lcd_panel;
152 static struct fb_info *da8xx_fb_info;
153 static int bits_x_pixel;
154 static unsigned int lcd_revision;
155 const struct lcd_ctrl_config *da8xx_lcd_cfg;
157 static inline unsigned int lcdc_read(u32 *addr)
159 return (unsigned int)readl(addr);
162 static inline void lcdc_write(unsigned int val, u32 *addr)
167 struct da8xx_fb_par {
169 unsigned char *v_palette_base;
170 dma_addr_t vram_phys;
171 unsigned long vram_size;
173 unsigned int dma_start;
174 unsigned int dma_end;
175 struct clk *lcdc_clk;
177 unsigned short pseudo_palette[16];
178 unsigned int palette_sz;
179 unsigned int pxl_clk;
186 /* Variable Screen Information */
187 static struct fb_var_screeninfo da8xx_fb_var = {
195 .pixclock = 46666, /* 46us - AUO display */
197 .left_margin = LEFT_MARGIN,
198 .right_margin = RIGHT_MARGIN,
199 .upper_margin = UPPER_MARGIN,
200 .lower_margin = LOWER_MARGIN,
202 .vmode = FB_VMODE_NONINTERLACED
205 static struct fb_fix_screeninfo da8xx_fb_fix = {
206 .id = "DA8xx FB Drv",
207 .type = FB_TYPE_PACKED_PIXELS,
209 .visual = FB_VISUAL_PSEUDOCOLOR,
213 .accel = FB_ACCEL_NONE
216 /* Enable the Raster Engine of the LCD Controller */
217 static inline void lcd_enable_raster(void)
221 /* Put LCDC in reset for several cycles */
222 if (lcd_revision == LCD_VERSION_2)
223 lcdc_write(LCD_CLK_MAIN_RESET,
224 &da8xx_fb_reg_base->clk_reset);
227 /* Bring LCDC out of reset */
228 if (lcd_revision == LCD_VERSION_2)
230 &da8xx_fb_reg_base->clk_reset);
234 reg = lcdc_read(&da8xx_fb_reg_base->raster_ctrl);
235 if (!(reg & LCD_RASTER_ENABLE))
236 lcdc_write(reg | LCD_RASTER_ENABLE,
237 &da8xx_fb_reg_base->raster_ctrl);
240 /* Disable the Raster Engine of the LCD Controller */
241 static inline void lcd_disable_raster(bool wait_for_frame_done)
248 if (wait_for_frame_done)
251 reg = lcdc_read(&da8xx_fb_reg_base->raster_ctrl);
252 if (reg & LCD_RASTER_ENABLE)
253 lcdc_write(reg & ~LCD_RASTER_ENABLE,
254 &da8xx_fb_reg_base->raster_ctrl);
256 /* Wait for the current frame to complete */
258 if (lcd_revision == LCD_VERSION_1)
259 stat = lcdc_read(&da8xx_fb_reg_base->stat);
261 stat = lcdc_read(&da8xx_fb_reg_base->raw_stat);
264 } while (!(stat & 0x01) && (i++ < loop_cnt));
266 if (lcd_revision == LCD_VERSION_1)
267 lcdc_write(stat, &da8xx_fb_reg_base->stat);
269 lcdc_write(stat, &da8xx_fb_reg_base->raw_stat);
271 if ((loop_cnt != 0) && (i >= loop_cnt)) {
272 printf("LCD Controller timed out\n");
277 static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
285 /* init reg to clear PLM (loading mode) fields */
286 reg_ras = lcdc_read(&da8xx_fb_reg_base->raster_ctrl);
287 reg_ras &= ~(3 << 20);
289 reg_dma = lcdc_read(&da8xx_fb_reg_base->dma_ctrl);
291 if (load_mode == LOAD_DATA) {
292 start = par->dma_start;
295 reg_ras |= LCD_PALETTE_LOAD_MODE(DATA_ONLY);
296 if (lcd_revision == LCD_VERSION_1) {
297 reg_dma |= LCD_V1_END_OF_FRAME_INT_ENA;
299 reg_int = lcdc_read(&da8xx_fb_reg_base->int_ena_set) |
300 LCD_V2_END_OF_FRAME0_INT_ENA |
301 LCD_V2_END_OF_FRAME1_INT_ENA |
302 LCD_V2_UNDERFLOW_INT_ENA | LCD_SYNC_LOST;
303 lcdc_write(reg_int, &da8xx_fb_reg_base->int_ena_set);
306 #if (LCD_NUM_BUFFERS == 2)
307 reg_dma |= LCD_DUAL_FRAME_BUFFER_ENABLE;
308 lcdc_write(start, &da8xx_fb_reg_base->dma_frm_buf_base_addr_0);
309 lcdc_write(end, &da8xx_fb_reg_base->dma_frm_buf_ceiling_addr_0);
310 lcdc_write(start, &da8xx_fb_reg_base->dma_frm_buf_base_addr_1);
311 lcdc_write(end, &da8xx_fb_reg_base->dma_frm_buf_ceiling_addr_1);
313 reg_dma &= ~LCD_DUAL_FRAME_BUFFER_ENABLE;
314 lcdc_write(start, &da8xx_fb_reg_base->dma_frm_buf_base_addr_0);
315 lcdc_write(end, &da8xx_fb_reg_base->dma_frm_buf_ceiling_addr_0);
316 lcdc_write(0, &da8xx_fb_reg_base->dma_frm_buf_base_addr_1);
317 lcdc_write(0, &da8xx_fb_reg_base->dma_frm_buf_ceiling_addr_1);
320 } else if (load_mode == LOAD_PALETTE) {
321 start = par->p_palette_base;
322 end = start + par->palette_sz - 1;
324 reg_ras |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
325 if (lcd_revision == LCD_VERSION_1) {
326 reg_ras |= LCD_V1_PL_INT_ENA;
328 reg_int = lcdc_read(&da8xx_fb_reg_base->int_ena_set) |
330 lcdc_write(reg_int, &da8xx_fb_reg_base->int_ena_set);
333 lcdc_write(start, &da8xx_fb_reg_base->dma_frm_buf_base_addr_0);
334 lcdc_write(end, &da8xx_fb_reg_base->dma_frm_buf_ceiling_addr_0);
337 lcdc_write(reg_dma, &da8xx_fb_reg_base->dma_ctrl);
338 lcdc_write(reg_ras, &da8xx_fb_reg_base->raster_ctrl);
341 * The Raster enable bit must be set after all other control fields are
347 /* Configure the Burst Size of DMA */
348 static int lcd_cfg_dma(int burst_size)
352 reg = lcdc_read(&da8xx_fb_reg_base->dma_ctrl) & 0x00000001;
353 switch (burst_size) {
355 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_1);
358 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_2);
361 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_4);
364 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_8);
367 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
372 lcdc_write(reg, &da8xx_fb_reg_base->dma_ctrl);
377 static void lcd_cfg_ac_bias(int period, int transitions_per_int)
381 /* Set the AC Bias Period and Number of Transitions per Interrupt */
382 reg = lcdc_read(&da8xx_fb_reg_base->raster_timing_2) & 0xFFF00000;
383 reg |= LCD_AC_BIAS_FREQUENCY(period) |
384 LCD_AC_BIAS_TRANSITIONS_PER_INT(transitions_per_int);
385 lcdc_write(reg, &da8xx_fb_reg_base->raster_timing_2);
388 static void lcd_cfg_horizontal_sync(int back_porch, int pulse_width,
393 reg = lcdc_read(&da8xx_fb_reg_base->raster_timing_0) & 0xf;
394 reg |= ((back_porch & 0xff) << 24)
395 | ((front_porch & 0xff) << 16)
396 | ((pulse_width & 0x3f) << 10);
397 lcdc_write(reg, &da8xx_fb_reg_base->raster_timing_0);
400 static void lcd_cfg_vertical_sync(int back_porch, int pulse_width,
405 reg = lcdc_read(&da8xx_fb_reg_base->raster_timing_1) & 0x3ff;
406 reg |= ((back_porch & 0xff) << 24)
407 | ((front_porch & 0xff) << 16)
408 | ((pulse_width & 0x3f) << 10);
409 lcdc_write(reg, &da8xx_fb_reg_base->raster_timing_1);
412 static int lcd_cfg_display(const struct lcd_ctrl_config *cfg)
417 reg = lcdc_read(&da8xx_fb_reg_base->raster_ctrl) & ~(LCD_TFT_MODE |
419 LCD_MONOCHROME_MODE);
421 switch (cfg->p_disp_panel->panel_shade) {
423 reg |= LCD_MONOCHROME_MODE;
424 if (cfg->mono_8bit_mode)
425 reg |= LCD_MONO_8BIT_MODE;
429 if (cfg->tft_alt_mode)
430 reg |= LCD_TFT_ALT_ENABLE;
434 if (cfg->stn_565_mode)
435 reg |= LCD_STN_565_ENABLE;
442 /* enable additional interrupts here */
443 if (lcd_revision == LCD_VERSION_1) {
444 reg |= LCD_V1_UNDERFLOW_INT_ENA;
446 reg_int = lcdc_read(&da8xx_fb_reg_base->int_ena_set) |
447 LCD_V2_UNDERFLOW_INT_ENA;
448 lcdc_write(reg_int, &da8xx_fb_reg_base->int_ena_set);
451 lcdc_write(reg, &da8xx_fb_reg_base->raster_ctrl);
453 reg = lcdc_read(&da8xx_fb_reg_base->raster_timing_2);
456 reg |= LCD_SYNC_CTRL;
458 reg &= ~LCD_SYNC_CTRL;
461 reg |= LCD_SYNC_EDGE;
463 reg &= ~LCD_SYNC_EDGE;
465 if (cfg->invert_line_clock)
466 reg |= LCD_INVERT_LINE_CLOCK;
468 reg &= ~LCD_INVERT_LINE_CLOCK;
470 if (cfg->invert_frm_clock)
471 reg |= LCD_INVERT_FRAME_CLOCK;
473 reg &= ~LCD_INVERT_FRAME_CLOCK;
475 lcdc_write(reg, &da8xx_fb_reg_base->raster_timing_2);
480 static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
481 u32 bpp, u32 raster_order)
485 /* Set the Panel Width */
486 /* Pixels per line = (PPL + 1)*16 */
487 if (lcd_revision == LCD_VERSION_1) {
489 * 0x3F in bits 4..9 gives max horizontal resolution = 1024
495 * 0x7F in bits 4..10 gives max horizontal resolution = 2048
500 reg = lcdc_read(&da8xx_fb_reg_base->raster_timing_0);
502 if (lcd_revision == LCD_VERSION_1) {
503 reg |= ((width >> 4) - 1) << 4;
505 width = (width >> 4) - 1;
506 reg |= ((width & 0x3f) << 4) | ((width & 0x40) >> 3);
508 lcdc_write(reg, &da8xx_fb_reg_base->raster_timing_0);
510 /* Set the Panel Height */
511 /* Set bits 9:0 of Lines Per Pixel */
512 reg = lcdc_read(&da8xx_fb_reg_base->raster_timing_1);
513 reg = ((height - 1) & 0x3ff) | (reg & 0xfffffc00);
514 lcdc_write(reg, &da8xx_fb_reg_base->raster_timing_1);
516 /* Set bit 10 of Lines Per Pixel */
517 if (lcd_revision == LCD_VERSION_2) {
518 reg = lcdc_read(&da8xx_fb_reg_base->raster_timing_2);
519 reg |= ((height - 1) & 0x400) << 16;
520 lcdc_write(reg, &da8xx_fb_reg_base->raster_timing_2);
523 /* Set the Raster Order of the Frame Buffer */
524 reg = lcdc_read(&da8xx_fb_reg_base->raster_ctrl) & ~(1 << 8);
526 reg |= LCD_RASTER_ORDER;
529 reg |= (LCD_TFT_MODE | LCD_V2_TFT_24BPP_MODE);
531 reg |= (LCD_TFT_MODE | LCD_V2_TFT_24BPP_MODE
532 | LCD_V2_TFT_24BPP_UNPACK);
534 lcdc_write(reg, &da8xx_fb_reg_base->raster_ctrl);
543 par->palette_sz = 16 * 2;
547 par->palette_sz = 256 * 2;
557 static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
558 unsigned blue, unsigned transp,
559 struct fb_info *info)
561 struct da8xx_fb_par *par = info->par;
562 unsigned short *palette = (unsigned short *) par->v_palette_base;
569 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
572 if (info->var.bits_per_pixel == 8) {
577 pal = (red & 0x0f00);
578 pal |= (green & 0x00f0);
579 pal |= (blue & 0x000f);
581 if (palette[regno] != pal) {
583 palette[regno] = pal;
585 } else if ((info->var.bits_per_pixel == 16) && regno < 16) {
586 red >>= (16 - info->var.red.length);
587 red <<= info->var.red.offset;
589 green >>= (16 - info->var.green.length);
590 green <<= info->var.green.offset;
592 blue >>= (16 - info->var.blue.length);
593 blue <<= info->var.blue.offset;
595 par->pseudo_palette[regno] = red | green | blue;
597 if (palette[0] != 0x4000) {
601 } else if (((info->var.bits_per_pixel == 32) && regno < 32) ||
602 ((info->var.bits_per_pixel == 24) && regno < 24)) {
603 red >>= (24 - info->var.red.length);
604 red <<= info->var.red.offset;
606 green >>= (24 - info->var.green.length);
607 green <<= info->var.green.offset;
609 blue >>= (24 - info->var.blue.length);
610 blue <<= info->var.blue.offset;
612 par->pseudo_palette[regno] = red | green | blue;
614 if (palette[0] != 0x4000) {
620 /* Update the palette in the h/w as needed. */
622 lcd_blit(LOAD_PALETTE, par);
627 static void lcd_reset(struct da8xx_fb_par *par)
629 /* Disable the Raster if previously Enabled */
630 lcd_disable_raster(NO_WAIT_FOR_FRAME_DONE);
632 /* DMA has to be disabled */
633 lcdc_write(0, &da8xx_fb_reg_base->dma_ctrl);
634 lcdc_write(0, &da8xx_fb_reg_base->raster_ctrl);
636 if (lcd_revision == LCD_VERSION_2) {
637 lcdc_write(0, &da8xx_fb_reg_base->int_ena_set);
638 /* Write 1 to reset */
639 lcdc_write(LCD_CLK_MAIN_RESET, &da8xx_fb_reg_base->clk_reset);
640 lcdc_write(0, &da8xx_fb_reg_base->clk_reset);
644 static void lcd_calc_clk_divider(struct da8xx_fb_par *par)
646 unsigned int lcd_clk, div;
648 /* Get clock from sysclk2 */
649 lcd_clk = clk_get(2);
651 div = lcd_clk / par->pxl_clk;
652 debug("LCD Clock: %d Divider: %d PixClk: %d\n",
653 lcd_clk, div, par->pxl_clk);
655 /* Configure the LCD clock divisor. */
656 lcdc_write(LCD_CLK_DIVISOR(div) |
657 (LCD_RASTER_MODE & 0x1), &da8xx_fb_reg_base->ctrl);
659 if (lcd_revision == LCD_VERSION_2)
660 lcdc_write(LCD_V2_DMA_CLK_EN | LCD_V2_LIDD_CLK_EN |
662 &da8xx_fb_reg_base->clk_ena);
665 static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
666 const struct da8xx_panel *panel)
673 /* Calculate the divider */
674 lcd_calc_clk_divider(par);
676 if (panel->invert_pxl_clk)
677 lcdc_write((lcdc_read(&da8xx_fb_reg_base->raster_timing_2) |
678 LCD_INVERT_PIXEL_CLOCK),
679 &da8xx_fb_reg_base->raster_timing_2);
681 lcdc_write((lcdc_read(&da8xx_fb_reg_base->raster_timing_2) &
682 ~LCD_INVERT_PIXEL_CLOCK),
683 &da8xx_fb_reg_base->raster_timing_2);
685 /* Configure the DMA burst size. */
686 ret = lcd_cfg_dma(cfg->dma_burst_sz);
690 /* Configure the AC bias properties. */
691 lcd_cfg_ac_bias(cfg->ac_bias, cfg->ac_bias_intrpt);
693 /* Configure the vertical and horizontal sync properties. */
694 lcd_cfg_vertical_sync(panel->vbp, panel->vsw, panel->vfp);
695 lcd_cfg_horizontal_sync(panel->hbp, panel->hsw, panel->hfp);
697 /* Configure for display */
698 ret = lcd_cfg_display(cfg);
702 if ((QVGA != cfg->p_disp_panel->panel_type) &&
703 (WVGA != cfg->p_disp_panel->panel_type))
706 if (cfg->bpp <= cfg->p_disp_panel->max_bpp &&
707 cfg->bpp >= cfg->p_disp_panel->min_bpp)
710 bpp = cfg->p_disp_panel->max_bpp;
713 ret = lcd_cfg_frame_buffer(par, (unsigned int)panel->width,
714 (unsigned int)panel->height, bpp,
720 lcdc_write((lcdc_read(&da8xx_fb_reg_base->raster_ctrl) & 0xfff00fff) |
721 (cfg->fdd << 12), &da8xx_fb_reg_base->raster_ctrl);
726 static void lcdc_dma_start(void)
728 struct da8xx_fb_par *par = da8xx_fb_info->par;
729 lcdc_write(par->dma_start,
730 &da8xx_fb_reg_base->dma_frm_buf_base_addr_0);
731 lcdc_write(par->dma_end,
732 &da8xx_fb_reg_base->dma_frm_buf_ceiling_addr_0);
734 &da8xx_fb_reg_base->dma_frm_buf_base_addr_1);
736 &da8xx_fb_reg_base->dma_frm_buf_ceiling_addr_1);
739 static u32 lcdc_irq_handler_rev01(void)
741 struct da8xx_fb_par *par = da8xx_fb_info->par;
742 u32 stat = lcdc_read(&da8xx_fb_reg_base->stat);
745 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
746 debug("LCD_SYNC_LOST\n");
747 lcd_disable_raster(NO_WAIT_FOR_FRAME_DONE);
748 lcdc_write(stat, &da8xx_fb_reg_base->stat);
750 return LCD_SYNC_LOST;
751 } else if (stat & LCD_PL_LOAD_DONE) {
752 debug("LCD_PL_LOAD_DONE\n");
754 * Must disable raster before changing state of any control bit.
755 * And also must be disabled before clearing the PL loading
756 * interrupt via the following write to the status register. If
757 * this is done after then one gets multiple PL done interrupts.
759 lcd_disable_raster(NO_WAIT_FOR_FRAME_DONE);
761 lcdc_write(stat, &da8xx_fb_reg_base->stat);
763 /* Disable PL completion interrupt */
764 reg_ras = lcdc_read(&da8xx_fb_reg_base->raster_ctrl);
765 reg_ras &= ~LCD_V1_PL_INT_ENA;
766 lcdc_write(reg_ras, &da8xx_fb_reg_base->raster_ctrl);
768 /* Setup and start data loading mode */
769 lcd_blit(LOAD_DATA, par);
770 return LCD_PL_LOAD_DONE;
772 lcdc_write(stat, &da8xx_fb_reg_base->stat);
774 if (stat & LCD_END_OF_FRAME0)
775 debug("LCD_END_OF_FRAME0\n");
777 lcdc_write(par->dma_start,
778 &da8xx_fb_reg_base->dma_frm_buf_base_addr_0);
779 lcdc_write(par->dma_end,
780 &da8xx_fb_reg_base->dma_frm_buf_ceiling_addr_0);
782 return LCD_END_OF_FRAME0;
787 static u32 lcdc_irq_handler_rev02(void)
789 struct da8xx_fb_par *par = da8xx_fb_info->par;
790 u32 stat = lcdc_read(&da8xx_fb_reg_base->masked_stat);
793 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
794 debug("LCD_SYNC_LOST\n");
795 lcd_disable_raster(NO_WAIT_FOR_FRAME_DONE);
796 lcdc_write(stat, &da8xx_fb_reg_base->masked_stat);
798 lcdc_write(0, &da8xx_fb_reg_base->end_of_int_ind);
799 return LCD_SYNC_LOST;
800 } else if (stat & LCD_PL_LOAD_DONE) {
801 debug("LCD_PL_LOAD_DONE\n");
803 * Must disable raster before changing state of any control bit.
804 * And also must be disabled before clearing the PL loading
805 * interrupt via the following write to the status register. If
806 * this is done after then one gets multiple PL done interrupts.
808 lcd_disable_raster(NO_WAIT_FOR_FRAME_DONE);
810 lcdc_write(stat, &da8xx_fb_reg_base->masked_stat);
812 /* Disable PL completion interrupt */
813 reg_int = lcdc_read(&da8xx_fb_reg_base->int_ena_clr) |
815 lcdc_write(reg_int, &da8xx_fb_reg_base->int_ena_clr);
817 /* Setup and start data loading mode */
818 lcd_blit(LOAD_DATA, par);
819 lcdc_write(0, &da8xx_fb_reg_base->end_of_int_ind);
820 return LCD_PL_LOAD_DONE;
822 lcdc_write(stat, &da8xx_fb_reg_base->masked_stat);
824 if (stat & LCD_END_OF_FRAME0)
825 debug("LCD_END_OF_FRAME0\n");
827 lcdc_write(par->dma_start,
828 &da8xx_fb_reg_base->dma_frm_buf_base_addr_0);
829 lcdc_write(par->dma_end,
830 &da8xx_fb_reg_base->dma_frm_buf_ceiling_addr_0);
832 lcdc_write(0, &da8xx_fb_reg_base->end_of_int_ind);
833 return LCD_END_OF_FRAME0;
835 lcdc_write(0, &da8xx_fb_reg_base->end_of_int_ind);
839 static u32 lcdc_irq_handler(void)
841 if (lcd_revision == LCD_VERSION_1)
842 return lcdc_irq_handler_rev01();
844 return lcdc_irq_handler_rev02();
847 static u32 wait_for_event(u32 event)
853 ret = lcdc_irq_handler();
856 } while (!(ret & event) && timeout);
858 if (!(ret & event)) {
859 printf("%s: event %d not hit\n", __func__, event);
867 void *video_hw_init(void)
869 struct da8xx_fb_par *par;
875 printf("Display not initialized\n");
878 gpanel.winSizeX = lcd_panel->width;
879 gpanel.winSizeY = lcd_panel->height;
880 gpanel.plnSizeX = lcd_panel->width;
881 gpanel.plnSizeY = lcd_panel->height;
883 switch (bits_x_pixel) {
885 gpanel.gdfBytesPP = 4;
886 gpanel.gdfIndex = GDF_32BIT_X888RGB;
889 gpanel.gdfBytesPP = 4;
890 gpanel.gdfIndex = GDF_32BIT_X888RGB;
893 gpanel.gdfBytesPP = 2;
894 gpanel.gdfIndex = GDF_16BIT_565RGB;
897 gpanel.gdfBytesPP = 1;
898 gpanel.gdfIndex = GDF__8BIT_INDEX;
902 da8xx_fb_reg_base = (struct da8xx_lcd_regs *)DA8XX_LCD_CNTL_BASE;
904 /* Determine LCD IP Version */
905 rev = lcdc_read(&da8xx_fb_reg_base->revid);
908 lcd_revision = LCD_VERSION_1;
912 lcd_revision = LCD_VERSION_2;
915 printf("Unknown PID Reg value 0x%x, defaulting to LCD revision 1\n",
917 lcd_revision = LCD_VERSION_1;
921 debug("rev: 0x%x Resolution: %dx%d %d\n", rev,
926 size = sizeof(struct fb_info) + sizeof(struct da8xx_fb_par);
927 da8xx_fb_info = malloc_cache_aligned(size);
928 debug("da8xx_fb_info at %x\n", (unsigned int)da8xx_fb_info);
930 if (!da8xx_fb_info) {
931 printf("Memory allocation failed for fb_info\n");
934 memset(da8xx_fb_info, 0, size);
935 p = (char *)da8xx_fb_info;
936 da8xx_fb_info->par = p + sizeof(struct fb_info);
937 debug("da8xx_par at %x\n", (unsigned int)da8xx_fb_info->par);
939 par = da8xx_fb_info->par;
940 par->pxl_clk = lcd_panel->pxl_clk;
942 if (lcd_init(par, da8xx_lcd_cfg, lcd_panel) < 0) {
943 printf("lcd_init failed\n");
947 /* allocate frame buffer */
948 par->vram_size = lcd_panel->width * lcd_panel->height *
950 par->vram_size = par->vram_size * LCD_NUM_BUFFERS / 8;
952 par->vram_virt = malloc_cache_aligned(par->vram_size);
954 par->vram_phys = (dma_addr_t) par->vram_virt;
955 debug("Requesting 0x%x bytes for framebuffer at 0x%x\n",
956 (unsigned int)par->vram_size,
957 (unsigned int)par->vram_virt);
958 if (!par->vram_virt) {
959 printf("GLCD: malloc for frame buffer failed\n");
962 gd->fb_base = (int)par->vram_virt;
964 gpanel.frameAdrs = (unsigned int)par->vram_virt;
965 da8xx_fb_info->screen_base = (char *) par->vram_virt;
966 da8xx_fb_fix.smem_start = gpanel.frameAdrs;
967 da8xx_fb_fix.smem_len = par->vram_size;
968 da8xx_fb_fix.line_length = (lcd_panel->width * da8xx_lcd_cfg->bpp) / 8;
970 par->dma_start = par->vram_phys;
971 par->dma_end = par->dma_start + lcd_panel->height *
972 da8xx_fb_fix.line_length - 1;
974 /* allocate palette buffer */
975 par->v_palette_base = malloc_cache_aligned(PALETTE_SIZE);
976 if (!par->v_palette_base) {
977 printf("GLCD: malloc for palette buffer failed\n");
978 goto err_release_fb_mem;
980 memset(par->v_palette_base, 0, PALETTE_SIZE);
981 par->p_palette_base = (unsigned int)par->v_palette_base;
984 da8xx_fb_info->var.bits_per_pixel = da8xx_lcd_cfg->bpp;
986 da8xx_fb_var.xres = lcd_panel->width;
987 da8xx_fb_var.xres_virtual = lcd_panel->width;
989 da8xx_fb_var.yres = lcd_panel->height;
990 da8xx_fb_var.yres_virtual = lcd_panel->height * LCD_NUM_BUFFERS;
992 da8xx_fb_var.grayscale =
993 da8xx_lcd_cfg->p_disp_panel->panel_shade == MONOCHROME ? 1 : 0;
994 da8xx_fb_var.bits_per_pixel = da8xx_lcd_cfg->bpp;
996 da8xx_fb_var.hsync_len = lcd_panel->hsw;
997 da8xx_fb_var.vsync_len = lcd_panel->vsw;
999 /* Initialize fbinfo */
1000 da8xx_fb_info->flags = FBINFO_FLAG_DEFAULT;
1001 da8xx_fb_info->fix = da8xx_fb_fix;
1002 da8xx_fb_info->var = da8xx_fb_var;
1003 da8xx_fb_info->pseudo_palette = par->pseudo_palette;
1004 da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
1005 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1007 /* Clear interrupt */
1008 memset((void *)par->vram_virt, 0, par->vram_size);
1009 lcd_disable_raster(NO_WAIT_FOR_FRAME_DONE);
1010 if (lcd_revision == LCD_VERSION_1)
1011 lcdc_write(0xFFFF, &da8xx_fb_reg_base->stat);
1013 lcdc_write(0xFFFF, &da8xx_fb_reg_base->masked_stat);
1014 debug("Palette at 0x%x size %d\n", par->p_palette_base,
1018 /* Load a default palette */
1019 fb_setcolreg(0, 0, 0, 0, 0xffff, da8xx_fb_info);
1021 /* Check that the palette is loaded */
1022 wait_for_event(LCD_PL_LOAD_DONE);
1024 /* Wait until DMA is working */
1025 wait_for_event(LCD_END_OF_FRAME0);
1027 return (void *)&gpanel;
1030 free(par->vram_virt);
1033 free(da8xx_fb_info);
1038 void da8xx_video_init(const struct da8xx_panel *panel,
1039 const struct lcd_ctrl_config *lcd_cfg, int bits_pixel)
1042 da8xx_lcd_cfg = lcd_cfg;
1043 bits_x_pixel = bits_pixel;