blob: 2c21e293a2c32b5b168bfbabde9eeaee96af4203 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
York Sunb7145172007-10-29 13:58:39 -05002/*
Timur Tabi32f709e2011-04-11 14:18:22 -05003 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc.
4 * Authors: York Sun <yorksun@freescale.com>
5 * Timur Tabi <timur@freescale.com>
York Sunb7145172007-10-29 13:58:39 -05006 *
7 * FSL DIU Framebuffer driver
York Sunb7145172007-10-29 13:58:39 -05008 */
9
York Sunb7145172007-10-29 13:58:39 -050010#include <common.h>
York Sunb7145172007-10-29 13:58:39 -050011#include <malloc.h>
Jerry Huang54e4b892010-04-08 15:56:07 +080012#include <asm/io.h>
York Sunb7145172007-10-29 13:58:39 -050013
Timur Tabi32f709e2011-04-11 14:18:22 -050014#include "videomodes.h"
15#include <video_fb.h>
Anatolij Gustschin43ffc532010-09-24 01:06:37 +020016#include <fsl_diu_fb.h>
Timur Tabid8cf0b52011-08-22 10:54:02 -050017#include <linux/list.h>
18#include <linux/fb.h>
York Sunb7145172007-10-29 13:58:39 -050019
Anatolij Gustschin81cad142010-04-24 19:27:09 +020020/* This setting is used for the ifm pdm360ng with PRIMEVIEW PM070WL3 */
Timur Tabi6d99ce22011-05-26 09:02:17 -050021static struct fb_videomode fsl_diu_mode_800_480 = {
22 .name = "800x480-60",
Anatolij Gustschin81cad142010-04-24 19:27:09 +020023 .refresh = 60,
24 .xres = 800,
25 .yres = 480,
26 .pixclock = 31250,
27 .left_margin = 86,
28 .right_margin = 42,
29 .upper_margin = 33,
30 .lower_margin = 10,
31 .hsync_len = 128,
32 .vsync_len = 2,
33 .sync = 0,
34 .vmode = FB_VMODE_NONINTERLACED
35};
36
Timur Tabi6d99ce22011-05-26 09:02:17 -050037/* For the SHARP LQ084S3LG01, used on the P1022DS board */
38static struct fb_videomode fsl_diu_mode_800_600 = {
39 .name = "800x600-60",
40 .refresh = 60,
41 .xres = 800,
42 .yres = 600,
43 .pixclock = 25000,
44 .left_margin = 88,
45 .right_margin = 40,
46 .upper_margin = 23,
47 .lower_margin = 1,
48 .hsync_len = 128,
49 .vsync_len = 4,
50 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
51 .vmode = FB_VMODE_NONINTERLACED
52};
53
York Sunb7145172007-10-29 13:58:39 -050054/*
55 * These parameters give default parameters
56 * for video output 1024x768,
57 * FIXME - change timing to proper amounts
58 * hsync 31.5kHz, vsync 60Hz
59 */
Timur Tabi6d99ce22011-05-26 09:02:17 -050060static struct fb_videomode fsl_diu_mode_1024_768 = {
Timur Tabi32f709e2011-04-11 14:18:22 -050061 .name = "1024x768-60",
York Sunb7145172007-10-29 13:58:39 -050062 .refresh = 60,
63 .xres = 1024,
64 .yres = 768,
65 .pixclock = 15385,
66 .left_margin = 160,
67 .right_margin = 24,
68 .upper_margin = 29,
69 .lower_margin = 3,
70 .hsync_len = 136,
71 .vsync_len = 6,
72 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
73 .vmode = FB_VMODE_NONINTERLACED
74};
75
Timur Tabi6d99ce22011-05-26 09:02:17 -050076static struct fb_videomode fsl_diu_mode_1280_1024 = {
York Sunb7145172007-10-29 13:58:39 -050077 .name = "1280x1024-60",
78 .refresh = 60,
79 .xres = 1280,
80 .yres = 1024,
81 .pixclock = 9375,
82 .left_margin = 38,
83 .right_margin = 128,
84 .upper_margin = 2,
85 .lower_margin = 7,
86 .hsync_len = 216,
87 .vsync_len = 37,
88 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
89 .vmode = FB_VMODE_NONINTERLACED
90};
91
Jerry Huang6d2e9622011-11-01 15:16:43 +000092static struct fb_videomode fsl_diu_mode_1280_720 = {
93 .name = "1280x720-60",
94 .refresh = 60,
95 .xres = 1280,
96 .yres = 720,
97 .pixclock = 13426,
98 .left_margin = 192,
99 .right_margin = 64,
100 .upper_margin = 22,
101 .lower_margin = 1,
102 .hsync_len = 136,
103 .vsync_len = 3,
104 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
105 .vmode = FB_VMODE_NONINTERLACED
106};
107
108static struct fb_videomode fsl_diu_mode_1920_1080 = {
109 .name = "1920x1080-60",
110 .refresh = 60,
111 .xres = 1920,
112 .yres = 1080,
113 .pixclock = 5787,
114 .left_margin = 328,
115 .right_margin = 120,
116 .upper_margin = 34,
117 .lower_margin = 1,
118 .hsync_len = 208,
119 .vsync_len = 3,
120 .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
121 .vmode = FB_VMODE_NONINTERLACED
122};
123
York Sunb7145172007-10-29 13:58:39 -0500124/*
125 * These are the fields of area descriptor(in DDR memory) for every plane
126 */
127struct diu_ad {
128 /* Word 0(32-bit) in DDR memory */
Timur Tabi32f709e2011-04-11 14:18:22 -0500129 __le32 pix_fmt; /* hard coding pixel format */
York Sunb7145172007-10-29 13:58:39 -0500130 /* Word 1(32-bit) in DDR memory */
Timur Tabi32f709e2011-04-11 14:18:22 -0500131 __le32 addr;
York Sunb7145172007-10-29 13:58:39 -0500132 /* Word 2(32-bit) in DDR memory */
Timur Tabi32f709e2011-04-11 14:18:22 -0500133 __le32 src_size_g_alpha;
York Sunb7145172007-10-29 13:58:39 -0500134 /* Word 3(32-bit) in DDR memory */
Timur Tabi32f709e2011-04-11 14:18:22 -0500135 __le32 aoi_size;
York Sunb7145172007-10-29 13:58:39 -0500136 /* Word 4(32-bit) in DDR memory */
Timur Tabi32f709e2011-04-11 14:18:22 -0500137 __le32 offset_xyi;
York Sunb7145172007-10-29 13:58:39 -0500138 /* Word 5(32-bit) in DDR memory */
Timur Tabi32f709e2011-04-11 14:18:22 -0500139 __le32 offset_xyd;
York Sunb7145172007-10-29 13:58:39 -0500140 /* Word 6(32-bit) in DDR memory */
Timur Tabi32f709e2011-04-11 14:18:22 -0500141 __le32 ckmax_r:8;
142 __le32 ckmax_g:8;
143 __le32 ckmax_b:8;
144 __le32 res9:8;
York Sunb7145172007-10-29 13:58:39 -0500145 /* Word 7(32-bit) in DDR memory */
Timur Tabi32f709e2011-04-11 14:18:22 -0500146 __le32 ckmin_r:8;
147 __le32 ckmin_g:8;
148 __le32 ckmin_b:8;
149 __le32 res10:8;
York Sunb7145172007-10-29 13:58:39 -0500150 /* Word 8(32-bit) in DDR memory */
Timur Tabi32f709e2011-04-11 14:18:22 -0500151 __le32 next_ad;
York Sunb7145172007-10-29 13:58:39 -0500152 /* Word 9(32-bit) in DDR memory, just for 64-bit aligned */
Timur Tabi32f709e2011-04-11 14:18:22 -0500153 __le32 res[3];
154} __attribute__ ((packed));
York Sunb7145172007-10-29 13:58:39 -0500155
156/*
157 * DIU register map
158 */
159struct diu {
Timur Tabi32f709e2011-04-11 14:18:22 -0500160 __be32 desc[3];
161 __be32 gamma;
162 __be32 pallete;
163 __be32 cursor;
164 __be32 curs_pos;
165 __be32 diu_mode;
166 __be32 bgnd;
167 __be32 bgnd_wb;
168 __be32 disp_size;
169 __be32 wb_size;
170 __be32 wb_mem_addr;
171 __be32 hsyn_para;
172 __be32 vsyn_para;
173 __be32 syn_pol;
174 __be32 thresholds;
175 __be32 int_status;
176 __be32 int_mask;
177 __be32 colorbar[8];
178 __be32 filling;
179 __be32 plut;
York Sunb7145172007-10-29 13:58:39 -0500180} __attribute__ ((packed));
181
York Sunb7145172007-10-29 13:58:39 -0500182struct diu_addr {
Timur Tabi32f709e2011-04-11 14:18:22 -0500183 void *vaddr; /* Virtual address */
184 u32 paddr; /* 32-bit physical address */
185 unsigned int offset; /* Alignment offset */
York Sunb7145172007-10-29 13:58:39 -0500186};
187
Timur Tabi32f709e2011-04-11 14:18:22 -0500188static struct fb_info info;
189
York Sunb7145172007-10-29 13:58:39 -0500190/*
Timur Tabi32f709e2011-04-11 14:18:22 -0500191 * Align to 64-bit(8-byte), 32-byte, etc.
York Sunb7145172007-10-29 13:58:39 -0500192 */
Timur Tabi32f709e2011-04-11 14:18:22 -0500193static int allocate_buf(struct diu_addr *buf, u32 size, u32 bytes_align)
194{
195 u32 offset, ssize;
196 u32 mask;
York Sunb7145172007-10-29 13:58:39 -0500197
Timur Tabi32f709e2011-04-11 14:18:22 -0500198 ssize = size + bytes_align;
199 buf->vaddr = malloc(ssize);
200 if (!buf->vaddr)
201 return -1;
York Sunb7145172007-10-29 13:58:39 -0500202
Timur Tabi32f709e2011-04-11 14:18:22 -0500203 memset(buf->vaddr, 0, ssize);
204 mask = bytes_align - 1;
205 offset = (u32)buf->vaddr & mask;
206 if (offset) {
207 buf->offset = bytes_align - offset;
208 buf->vaddr += offset;
209 } else
210 buf->offset = 0;
211
212 buf->paddr = virt_to_phys(buf->vaddr);
213 return 0;
214}
215
216/*
217 * Allocate a framebuffer and an Area Descriptor that points to it. Both
218 * are created in the same memory block. The Area Descriptor is updated to
219 * point to the framebuffer memory. Memory is aligned as needed.
220 */
221static struct diu_ad *allocate_fb(unsigned int xres, unsigned int yres,
Timur Tabid8cf0b52011-08-22 10:54:02 -0500222 unsigned int depth, char **fb)
Timur Tabi32f709e2011-04-11 14:18:22 -0500223{
224 unsigned long size = xres * yres * depth;
225 struct diu_addr addr;
226 struct diu_ad *ad;
227 size_t ad_size = roundup(sizeof(struct diu_ad), 32);
228
229 /*
230 * Allocate a memory block that holds the Area Descriptor and the
231 * frame buffer right behind it. To keep the code simple, everything
232 * is aligned on a 32-byte address.
233 */
234 if (allocate_buf(&addr, ad_size + size, 32) < 0)
235 return NULL;
236
237 ad = addr.vaddr;
238 ad->addr = cpu_to_le32(addr.paddr + ad_size);
239 ad->aoi_size = cpu_to_le32((yres << 16) | xres);
240 ad->src_size_g_alpha = cpu_to_le32((yres << 12) | xres);
241 ad->offset_xyi = 0;
242 ad->offset_xyd = 0;
York Sunb7145172007-10-29 13:58:39 -0500243
Timur Tabi32f709e2011-04-11 14:18:22 -0500244 if (fb)
245 *fb = addr.vaddr + ad_size;
York Sunb7145172007-10-29 13:58:39 -0500246
Timur Tabi32f709e2011-04-11 14:18:22 -0500247 return ad;
248}
York Sunb7145172007-10-29 13:58:39 -0500249
Timur Tabi6d99ce22011-05-26 09:02:17 -0500250int fsl_diu_init(u16 xres, u16 yres, u32 pixel_format, int gamma_fix)
York Sunb7145172007-10-29 13:58:39 -0500251{
252 struct fb_videomode *fsl_diu_mode_db;
Timur Tabi32f709e2011-04-11 14:18:22 -0500253 struct diu_ad *ad;
254 struct diu *hw = (struct diu *)CONFIG_SYS_DIU_ADDR;
255 u8 *gamma_table_base;
York Sunb7145172007-10-29 13:58:39 -0500256 unsigned int i, j;
Timur Tabi32f709e2011-04-11 14:18:22 -0500257 struct diu_addr gamma;
258 struct diu_addr cursor;
York Sunb7145172007-10-29 13:58:39 -0500259
Timur Tabi6d99ce22011-05-26 09:02:17 -0500260/* Convert the X,Y resolution pair into a single number */
261#define RESOLUTION(x, y) (((u32)(x) << 16) | (y))
262
263 switch (RESOLUTION(xres, yres)) {
264 case RESOLUTION(800, 480):
265 fsl_diu_mode_db = &fsl_diu_mode_800_480;
Anatolij Gustschin81cad142010-04-24 19:27:09 +0200266 break;
Timur Tabi6d99ce22011-05-26 09:02:17 -0500267 case RESOLUTION(800, 600):
268 fsl_diu_mode_db = &fsl_diu_mode_800_600;
Jerry Huang9d84f7a2011-10-30 20:19:54 +0000269 break;
Timur Tabi6d99ce22011-05-26 09:02:17 -0500270 case RESOLUTION(1024, 768):
271 fsl_diu_mode_db = &fsl_diu_mode_1024_768;
Jerry Huang9d84f7a2011-10-30 20:19:54 +0000272 break;
Timur Tabi6d99ce22011-05-26 09:02:17 -0500273 case RESOLUTION(1280, 1024):
274 fsl_diu_mode_db = &fsl_diu_mode_1280_1024;
Anatolij Gustschin81cad142010-04-24 19:27:09 +0200275 break;
Jerry Huang6d2e9622011-11-01 15:16:43 +0000276 case RESOLUTION(1280, 720):
277 fsl_diu_mode_db = &fsl_diu_mode_1280_720;
278 break;
279 case RESOLUTION(1920, 1080):
280 fsl_diu_mode_db = &fsl_diu_mode_1920_1080;
281 break;
Anatolij Gustschin81cad142010-04-24 19:27:09 +0200282 default:
Timur Tabi6d99ce22011-05-26 09:02:17 -0500283 printf("DIU: Unsupported resolution %ux%u\n", xres, yres);
284 return -1;
York Sunb7145172007-10-29 13:58:39 -0500285 }
286
York Sunb7145172007-10-29 13:58:39 -0500287 /* read mode info */
Timur Tabi32f709e2011-04-11 14:18:22 -0500288 info.var.xres = fsl_diu_mode_db->xres;
289 info.var.yres = fsl_diu_mode_db->yres;
290 info.var.bits_per_pixel = 32;
291 info.var.pixclock = fsl_diu_mode_db->pixclock;
292 info.var.left_margin = fsl_diu_mode_db->left_margin;
293 info.var.right_margin = fsl_diu_mode_db->right_margin;
294 info.var.upper_margin = fsl_diu_mode_db->upper_margin;
295 info.var.lower_margin = fsl_diu_mode_db->lower_margin;
296 info.var.hsync_len = fsl_diu_mode_db->hsync_len;
297 info.var.vsync_len = fsl_diu_mode_db->vsync_len;
298 info.var.sync = fsl_diu_mode_db->sync;
299 info.var.vmode = fsl_diu_mode_db->vmode;
Timur Tabid8cf0b52011-08-22 10:54:02 -0500300 info.fix.line_length = info.var.xres * info.var.bits_per_pixel / 8;
Timur Tabi32f709e2011-04-11 14:18:22 -0500301
302 /* Memory allocation for framebuffer */
Timur Tabid8cf0b52011-08-22 10:54:02 -0500303 info.screen_size =
Timur Tabi32f709e2011-04-11 14:18:22 -0500304 info.var.xres * info.var.yres * (info.var.bits_per_pixel / 8);
305 ad = allocate_fb(info.var.xres, info.var.yres,
306 info.var.bits_per_pixel / 8, &info.screen_base);
307 if (!ad) {
308 printf("DIU: Out of memory\n");
309 return -1;
310 }
York Sunb7145172007-10-29 13:58:39 -0500311
312 ad->pix_fmt = pixel_format;
York Sunb7145172007-10-29 13:58:39 -0500313
314 /* Disable chroma keying function */
315 ad->ckmax_r = 0;
316 ad->ckmax_g = 0;
317 ad->ckmax_b = 0;
318
319 ad->ckmin_r = 255;
320 ad->ckmin_g = 255;
321 ad->ckmin_b = 255;
322
Timur Tabi32f709e2011-04-11 14:18:22 -0500323 /* Initialize the gamma table */
324 if (allocate_buf(&gamma, 256 * 3, 32) < 0) {
325 printf("DIU: Out of memory\n");
326 return -1;
327 }
328 gamma_table_base = gamma.vaddr;
York Sunb7145172007-10-29 13:58:39 -0500329 for (i = 0; i <= 2; i++)
Timur Tabi32f709e2011-04-11 14:18:22 -0500330 for (j = 0; j < 256; j++)
York Sunb7145172007-10-29 13:58:39 -0500331 *gamma_table_base++ = j;
332
333 if (gamma_fix == 1) { /* fix the gamma */
Timur Tabi32f709e2011-04-11 14:18:22 -0500334 gamma_table_base = gamma.vaddr;
335 for (i = 0; i < 256 * 3; i++) {
York Sunb7145172007-10-29 13:58:39 -0500336 gamma_table_base[i] = (gamma_table_base[i] << 2)
337 | ((gamma_table_base[i] >> 6) & 0x03);
338 }
339 }
340
Timur Tabi32f709e2011-04-11 14:18:22 -0500341 /* Initialize the cursor */
342 if (allocate_buf(&cursor, 32 * 32 * 2, 32) < 0) {
343 printf("DIU: Can't alloc cursor data\n");
344 return -1;
345 }
York Sunb7145172007-10-29 13:58:39 -0500346
347 /* Program DIU registers */
Timur Tabi32f709e2011-04-11 14:18:22 -0500348 out_be32(&hw->diu_mode, 0); /* Temporarily disable the DIU */
York Sunb7145172007-10-29 13:58:39 -0500349
Timur Tabi32f709e2011-04-11 14:18:22 -0500350 out_be32(&hw->gamma, gamma.paddr);
351 out_be32(&hw->cursor, cursor.paddr);
Jerry Huang54e4b892010-04-08 15:56:07 +0800352 out_be32(&hw->bgnd, 0x007F7F7F);
Timur Tabi32f709e2011-04-11 14:18:22 -0500353 out_be32(&hw->disp_size, info.var.yres << 16 | info.var.xres);
Timur Tabi32f709e2011-04-11 14:18:22 -0500354 out_be32(&hw->hsyn_para, info.var.left_margin << 22 |
355 info.var.hsync_len << 11 |
356 info.var.right_margin);
York Sunb7145172007-10-29 13:58:39 -0500357
Timur Tabi32f709e2011-04-11 14:18:22 -0500358 out_be32(&hw->vsyn_para, info.var.upper_margin << 22 |
359 info.var.vsync_len << 11 |
360 info.var.lower_margin);
York Sunb7145172007-10-29 13:58:39 -0500361
York Sunc4964382008-05-05 10:19:59 -0500362 /* Pixel Clock configuration */
Timur Tabi32f709e2011-04-11 14:18:22 -0500363 diu_set_pixel_clock(info.var.pixclock);
York Sunb7145172007-10-29 13:58:39 -0500364
Timur Tabi32f709e2011-04-11 14:18:22 -0500365 /* Set the frame buffers */
366 out_be32(&hw->desc[0], virt_to_phys(ad));
Timur Tabid82cb112012-09-17 11:12:42 +0000367 out_be32(&hw->desc[1], 0);
368 out_be32(&hw->desc[2], 0);
York Sunb7145172007-10-29 13:58:39 -0500369
Timur Tabi32f709e2011-04-11 14:18:22 -0500370 /* Enable the DIU, set display to all three planes */
371 out_be32(&hw->diu_mode, 1);
York Sunb7145172007-10-29 13:58:39 -0500372
York Sunb7145172007-10-29 13:58:39 -0500373 return 0;
374}
375
Timur Tabi32f709e2011-04-11 14:18:22 -0500376void *video_hw_init(void)
York Sunb7145172007-10-29 13:58:39 -0500377{
Timur Tabi32f709e2011-04-11 14:18:22 -0500378 static GraphicDevice ctfb;
379 const char *options;
380 unsigned int depth = 0, freq = 0;
York Sunb7145172007-10-29 13:58:39 -0500381
Timur Tabi32f709e2011-04-11 14:18:22 -0500382 if (!video_get_video_mode(&ctfb.winSizeX, &ctfb.winSizeY, &depth, &freq,
383 &options))
384 return NULL;
Anatolij Gustschin43ffc532010-09-24 01:06:37 +0200385
Timur Tabi32f709e2011-04-11 14:18:22 -0500386 /* Find the monitor port, which is a required option */
387 if (!options)
388 return NULL;
389 if (strncmp(options, "monitor=", 8) != 0)
390 return NULL;
Anatolij Gustschin43ffc532010-09-24 01:06:37 +0200391
Timur Tabi32f709e2011-04-11 14:18:22 -0500392 if (platform_diu_init(ctfb.winSizeX, ctfb.winSizeY, options + 8) < 0)
Anatolij Gustschin43ffc532010-09-24 01:06:37 +0200393 return NULL;
394
395 /* fill in Graphic device struct */
396 sprintf(ctfb.modeIdent, "%ix%ix%i %ikHz %iHz",
Timur Tabi32f709e2011-04-11 14:18:22 -0500397 ctfb.winSizeX, ctfb.winSizeY, depth, 64, freq);
Anatolij Gustschin43ffc532010-09-24 01:06:37 +0200398
Timur Tabi32f709e2011-04-11 14:18:22 -0500399 ctfb.frameAdrs = (unsigned int)info.screen_base;
Anatolij Gustschin43ffc532010-09-24 01:06:37 +0200400 ctfb.plnSizeX = ctfb.winSizeX;
401 ctfb.plnSizeY = ctfb.winSizeY;
402
403 ctfb.gdfBytesPP = 4;
404 ctfb.gdfIndex = GDF_32BIT_X888RGB;
405
406 ctfb.isaBase = 0;
407 ctfb.pciBase = 0;
Timur Tabi32f709e2011-04-11 14:18:22 -0500408 ctfb.memSize = info.screen_size;
Anatolij Gustschin43ffc532010-09-24 01:06:37 +0200409
410 /* Cursor Start Address */
411 ctfb.dprBase = 0;
412 ctfb.vprBase = 0;
413 ctfb.cprBase = 0;
414
415 return &ctfb;
416}