blob: 648ce508cc67cbd54c3ec0d8bcb0b6fe1656708d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +01002/*
Hannes Schmelzere880a5e2018-01-09 19:01:32 +01003 * Copyright (C) 2013-2018 Hannes Schmelzer <oe5hpm@oevsv.at>
4 * B&R Industrial Automation GmbH - http://www.br-automation.com
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +01005 *
6 * minimal framebuffer driver for TI's AM335x SoC to be compatible with
7 * Wolfgang Denk's LCD-Framework (CONFIG_LCD, common/lcd.c)
8 *
Martin Pietryka7d045172016-04-27 21:39:15 +02009 * - supporting 16/24/32bit RGB/TFT raster Mode (not using palette)
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010010 * - sets up LCD controller as in 'am335x_lcdpanel' struct given
11 * - starts output DMA from gd->fb_base buffer
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010012 */
13#include <common.h>
Hannes Schmelzer8a094f52018-01-09 19:01:34 +010014#include <asm/io.h>
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010015#include <asm/arch/hardware.h>
Hannes Schmelzer8a094f52018-01-09 19:01:34 +010016#include <asm/arch/omap.h>
17#include <asm/arch/clock.h>
18#include <asm/arch/sys_proto.h>
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010019#include <lcd.h>
20#include "am335x-fb.h"
21
22#if !defined(LCD_CNTL_BASE)
23#error "hw-base address of LCD-Controller (LCD_CNTL_BASE) not defined!"
24#endif
25
Hannes Schmelzer8a094f52018-01-09 19:01:34 +010026#define LCDC_FMAX 200000000
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010027
28/* LCD Control Register */
Dario Binacchia2f47062020-02-22 14:05:37 +010029#define LCD_RASTER_MODE BIT(0)
Dario Binacchi017295f2020-02-22 14:05:40 +010030#define LCD_CLK_DIVISOR(x) (((x) & GENMASK(7, 0)) << 8)
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010031/* LCD Clock Enable Register */
Dario Binacchia2f47062020-02-22 14:05:37 +010032#define LCD_CORECLKEN BIT(0)
33#define LCD_LIDDCLKEN BIT(1)
34#define LCD_DMACLKEN BIT(2)
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010035/* LCD DMA Control Register */
Dario Binacchie3f82b82020-02-22 14:05:38 +010036#define LCD_DMA_BURST_SIZE(x) (((x) & GENMASK(2, 0)) << 4)
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010037#define LCD_DMA_BURST_1 0x0
38#define LCD_DMA_BURST_2 0x1
39#define LCD_DMA_BURST_4 0x2
40#define LCD_DMA_BURST_8 0x3
41#define LCD_DMA_BURST_16 0x4
42/* LCD Timing_0 Register */
Dario Binacchi3af43752020-02-22 14:05:39 +010043#define LCD_HORMSB(x) (((((x) >> 4) - 1) & 0x40) >> 4)
Dario Binacchi017295f2020-02-22 14:05:40 +010044#define LCD_HORLSB(x) (((((x) >> 4) - 1) & GENMASK(5, 0)) << 4)
45#define LCD_HSWLSB(x) ((((x) - 1) & GENMASK(5, 0)) << 10)
46#define LCD_HFPLSB(x) ((((x) - 1) & GENMASK(7, 0)) << 16)
47#define LCD_HBPLSB(x) ((((x) - 1) & GENMASK(7, 0)) << 24)
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010048/* LCD Timing_1 Register */
Dario Binacchia2f47062020-02-22 14:05:37 +010049#define LCD_VERLSB(x) (((x) - 1) & GENMASK(9, 0))
Dario Binacchi017295f2020-02-22 14:05:40 +010050#define LCD_VSW(x) ((((x) - 1) & GENMASK(5, 0)) << 10)
51#define LCD_VFP(x) (((x) & GENMASK(7, 0)) << 16)
52#define LCD_VBP(x) (((x) & GENMASK(7, 0)) << 24)
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010053/* LCD Timing_2 Register */
Dario Binacchia2f47062020-02-22 14:05:37 +010054#define LCD_HFPMSB(x) ((((x) - 1) & GENMASK(9, 8)) >> 8)
Dario Binacchi017295f2020-02-22 14:05:40 +010055#define LCD_HBPMSB(x) ((((x) - 1) & GENMASK(9, 8)) >> 4)
Dario Binacchia2f47062020-02-22 14:05:37 +010056#define LCD_INVMASK(x) ((x) & GENMASK(25, 20))
Dario Binacchi017295f2020-02-22 14:05:40 +010057#define LCD_VERMSB(x) ((((x) - 1) & BIT(10)) << 16)
58#define LCD_HSWMSB(x) ((((x) - 1) & GENMASK(9, 6)) << 21)
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010059/* LCD Raster Ctrl Register */
Dario Binacchi017295f2020-02-22 14:05:40 +010060#define LCD_RASTER_ENABLE BIT(0)
61#define LCD_TFT_MODE BIT(7)
62#define LCD_PALMODE_RAWDATA (0x02 << 20)
Dario Binacchia2f47062020-02-22 14:05:37 +010063#define LCD_TFT_24BPP_MODE BIT(25)
64#define LCD_TFT_24BPP_UNPACK BIT(26)
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +010065
66/* Macro definitions */
67#define FBSIZE(x) ((x->hactive * x->vactive * x->bpp) >> 3)
68
69struct am335x_lcdhw {
70 unsigned int pid; /* 0x00 */
71 unsigned int ctrl; /* 0x04 */
72 unsigned int gap0; /* 0x08 */
73 unsigned int lidd_ctrl; /* 0x0C */
74 unsigned int lidd_cs0_conf; /* 0x10 */
75 unsigned int lidd_cs0_addr; /* 0x14 */
76 unsigned int lidd_cs0_data; /* 0x18 */
77 unsigned int lidd_cs1_conf; /* 0x1C */
78 unsigned int lidd_cs1_addr; /* 0x20 */
79 unsigned int lidd_cs1_data; /* 0x24 */
80 unsigned int raster_ctrl; /* 0x28 */
81 unsigned int raster_timing0; /* 0x2C */
82 unsigned int raster_timing1; /* 0x30 */
83 unsigned int raster_timing2; /* 0x34 */
84 unsigned int raster_subpanel; /* 0x38 */
85 unsigned int raster_subpanel2; /* 0x3C */
86 unsigned int lcddma_ctrl; /* 0x40 */
87 unsigned int lcddma_fb0_base; /* 0x44 */
88 unsigned int lcddma_fb0_ceiling; /* 0x48 */
89 unsigned int lcddma_fb1_base; /* 0x4C */
90 unsigned int lcddma_fb1_ceiling; /* 0x50 */
91 unsigned int sysconfig; /* 0x54 */
92 unsigned int irqstatus_raw; /* 0x58 */
93 unsigned int irqstatus; /* 0x5C */
94 unsigned int irqenable_set; /* 0x60 */
95 unsigned int irqenable_clear; /* 0x64 */
96 unsigned int gap1; /* 0x68 */
97 unsigned int clkc_enable; /* 0x6C */
98 unsigned int clkc_reset; /* 0x70 */
99};
100
101static struct am335x_lcdhw *lcdhw = (void *)LCD_CNTL_BASE;
Hannes Schmelzer8a094f52018-01-09 19:01:34 +0100102
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100103DECLARE_GLOBAL_DATA_PTR;
104
105int lcd_get_size(int *line_length)
106{
107 *line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8;
108 return *line_length * panel_info.vl_row + 0x20;
109}
110
111int am335xfb_init(struct am335x_lcdpanel *panel)
112{
Martin Pietryka7d045172016-04-27 21:39:15 +0200113 u32 raster_ctrl = 0;
114
Hannes Schmelzer8a094f52018-01-09 19:01:34 +0100115 struct cm_dpll *const cmdpll = (struct cm_dpll *)CM_DPLL;
116 struct dpll_params dpll_disp = { 1, 0, 1, -1, -1, -1, -1 };
117 unsigned int m, n, d, best_d = 2;
118 int err = 0, err_r = 0;
119
Hannes Schmelzer0d8a7d62018-01-09 19:01:33 +0100120 if (gd->fb_base == 0) {
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100121 printf("ERROR: no valid fb_base stored in GLOBAL_DATA_PTR!\n");
122 return -1;
123 }
Hannes Schmelzer0d8a7d62018-01-09 19:01:33 +0100124 if (panel == NULL) {
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100125 printf("ERROR: missing ptr to am335x_lcdpanel!\n");
126 return -1;
127 }
128
Martin Pietryka7d045172016-04-27 21:39:15 +0200129 /* We can already set the bits for the raster_ctrl in this check */
130 switch (panel->bpp) {
131 case 16:
132 break;
133 case 32:
134 raster_ctrl |= LCD_TFT_24BPP_UNPACK;
135 /* fallthrough */
136 case 24:
137 raster_ctrl |= LCD_TFT_24BPP_MODE;
138 break;
139 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900140 pr_err("am335x-fb: invalid bpp value: %d\n", panel->bpp);
Martin Pietryka7d045172016-04-27 21:39:15 +0200141 return -1;
142 }
143
Hannes Schmelzer8a094f52018-01-09 19:01:34 +0100144 /* check given clock-frequency */
145 if (panel->pxl_clk > (LCDC_FMAX / 2)) {
146 pr_err("am335x-fb: requested pxl-clk: %d not supported!\n",
147 panel->pxl_clk);
148 return -1;
149 }
150
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100151 debug("setting up LCD-Controller for %dx%dx%d (hfp=%d,hbp=%d,hsw=%d / ",
152 panel->hactive, panel->vactive, panel->bpp,
153 panel->hfp, panel->hbp, panel->hsw);
Hannes Schmelzer8a094f52018-01-09 19:01:34 +0100154 debug("vfp=%d,vbp=%d,vsw=%d / clk=%d)\n",
155 panel->vfp, panel->vfp, panel->vsw, panel->pxl_clk);
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100156 debug("using frambuffer at 0x%08x with size %d.\n",
157 (unsigned int)gd->fb_base, FBSIZE(panel));
158
Hannes Schmelzer8a094f52018-01-09 19:01:34 +0100159 /* setup display pll for requested clock frequency */
160 err = panel->pxl_clk;
161 err_r = err;
162
163 for (d = 2; d < 255; d++) {
164 for (m = 2; m < 2047; m++) {
165 if ((V_OSCK * m) < (panel->pxl_clk * d))
166 continue;
167 n = (V_OSCK * m) / (panel->pxl_clk * d);
168 if (n > 127)
169 break;
170 if (((V_OSCK * m) / n) > LCDC_FMAX)
171 break;
172
173 err = abs((V_OSCK * m) / n / d - panel->pxl_clk);
174 if (err < err_r) {
175 err_r = err;
176 dpll_disp.m = m;
177 dpll_disp.n = n;
178 best_d = d;
179 }
180 }
181 }
182 debug("%s: PLL: best error %d Hz (M %d, N %d, DISP %d)\n",
183 __func__, err_r, dpll_disp.m, dpll_disp.n, best_d);
184 do_setup_dpll(&dpll_disp_regs, &dpll_disp);
185
186 /* clock source for LCDC from dispPLL M2 */
187 writel(0x0, &cmdpll->clklcdcpixelclk);
188
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100189 /* palette default entry */
190 memset((void *)gd->fb_base, 0, 0x20);
191 *(unsigned int *)gd->fb_base = 0x4000;
Martin Pietryka3d47b2d2016-04-27 21:39:16 +0200192 /* point fb behind palette */
193 gd->fb_base += 0x20;
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100194
Hannes Petermaier3b4e16e2015-02-03 13:22:23 +0100195 /* turn ON display through powercontrol function if accessible */
Hannes Schmelzer0d8a7d62018-01-09 19:01:33 +0100196 if (panel->panel_power_ctrl != NULL)
Hannes Petermaier3b4e16e2015-02-03 13:22:23 +0100197 panel->panel_power_ctrl(1);
198
199 debug("am335x-fb: wait for stable power ...\n");
200 mdelay(panel->pup_delay);
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100201 lcdhw->clkc_enable = LCD_CORECLKEN | LCD_LIDDCLKEN | LCD_DMACLKEN;
202 lcdhw->raster_ctrl = 0;
Hannes Schmelzer8a094f52018-01-09 19:01:34 +0100203 lcdhw->ctrl = LCD_CLK_DIVISOR(best_d) | LCD_RASTER_MODE;
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100204 lcdhw->lcddma_fb0_base = gd->fb_base;
Martin Pietryka3d47b2d2016-04-27 21:39:16 +0200205 lcdhw->lcddma_fb0_ceiling = gd->fb_base + FBSIZE(panel);
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100206 lcdhw->lcddma_fb1_base = gd->fb_base;
Martin Pietryka3d47b2d2016-04-27 21:39:16 +0200207 lcdhw->lcddma_fb1_ceiling = gd->fb_base + FBSIZE(panel);
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100208 lcdhw->lcddma_ctrl = LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
209
210 lcdhw->raster_timing0 = LCD_HORLSB(panel->hactive) |
211 LCD_HORMSB(panel->hactive) |
212 LCD_HFPLSB(panel->hfp) |
213 LCD_HBPLSB(panel->hbp) |
214 LCD_HSWLSB(panel->hsw);
215 lcdhw->raster_timing1 = LCD_VBP(panel->vbp) |
216 LCD_VFP(panel->vfp) |
217 LCD_VSW(panel->vsw) |
218 LCD_VERLSB(panel->vactive);
219 lcdhw->raster_timing2 = LCD_HSWMSB(panel->hsw) |
220 LCD_VERMSB(panel->vactive) |
221 LCD_INVMASK(panel->pol) |
222 LCD_HBPMSB(panel->hbp) |
223 LCD_HFPMSB(panel->hfp) |
224 0x0000FF00; /* clk cycles for ac-bias */
Martin Pietryka7d045172016-04-27 21:39:15 +0200225 lcdhw->raster_ctrl = raster_ctrl |
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100226 LCD_PALMODE_RAWDATA |
227 LCD_TFT_MODE |
228 LCD_RASTER_ENABLE;
229
Hannes Petermaier3b4e16e2015-02-03 13:22:23 +0100230 debug("am335x-fb: waiting picture to be stable.\n.");
231 mdelay(panel->pon_delay);
Hannes Petermaier3c5fabd2014-03-06 14:39:06 +0100232
233 return 0;
234}