]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/video/fsl-diu-fb.c
freescale DIU: add virtual resolution and panning support
[mv-sheeva.git] / drivers / video / fsl-diu-fb.c
1 /*
2  * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
3  *
4  *  Freescale DIU Frame Buffer device driver
5  *
6  *  Authors: Hongjun Chen <hong-jun.chen@freescale.com>
7  *           Paul Widmer <paul.widmer@freescale.com>
8  *           Srikanth Srinivasan <srikanth.srinivasan@freescale.com>
9  *           York Sun <yorksun@freescale.com>
10  *
11  *   Based on imxfb.c Copyright (C) 2004 S.Hauer, Pengutronix
12  *
13  * This program is free software; you can redistribute  it and/or modify it
14  * under  the terms of  the GNU General  Public License as published by the
15  * Free Software Foundation;  either version 2 of the  License, or (at your
16  * option) any later version.
17  *
18  */
19
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <linux/fb.h>
26 #include <linux/init.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/platform_device.h>
29 #include <linux/interrupt.h>
30 #include <linux/clk.h>
31 #include <linux/uaccess.h>
32 #include <linux/vmalloc.h>
33
34 #include <linux/of_platform.h>
35
36 #include <sysdev/fsl_soc.h>
37 #include "fsl-diu-fb.h"
38
39 /*
40  * These parameters give default parameters
41  * for video output 1024x768,
42  * FIXME - change timing to proper amounts
43  * hsync 31.5kHz, vsync 60Hz
44  */
45 static struct fb_videomode __devinitdata fsl_diu_default_mode = {
46         .refresh        = 60,
47         .xres           = 1024,
48         .yres           = 768,
49         .pixclock       = 15385,
50         .left_margin    = 160,
51         .right_margin   = 24,
52         .upper_margin   = 29,
53         .lower_margin   = 3,
54         .hsync_len      = 136,
55         .vsync_len      = 6,
56         .sync           = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
57         .vmode          = FB_VMODE_NONINTERLACED
58 };
59
60 static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
61         {
62                 .name           = "1024x768-60",
63                 .refresh        = 60,
64                 .xres           = 1024,
65                 .yres           = 768,
66                 .pixclock       = 15385,
67                 .left_margin    = 160,
68                 .right_margin   = 24,
69                 .upper_margin   = 29,
70                 .lower_margin   = 3,
71                 .hsync_len      = 136,
72                 .vsync_len      = 6,
73                 .sync           = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
74                 .vmode          = FB_VMODE_NONINTERLACED
75         },
76         {
77                 .name           = "1024x768-70",
78                 .refresh        = 70,
79                 .xres           = 1024,
80                 .yres           = 768,
81                 .pixclock       = 16886,
82                 .left_margin    = 3,
83                 .right_margin   = 3,
84                 .upper_margin   = 2,
85                 .lower_margin   = 2,
86                 .hsync_len      = 40,
87                 .vsync_len      = 18,
88                 .sync           = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
89                 .vmode          = FB_VMODE_NONINTERLACED
90         },
91         {
92                 .name           = "1024x768-75",
93                 .refresh        = 75,
94                 .xres           = 1024,
95                 .yres           = 768,
96                 .pixclock       = 15009,
97                 .left_margin    = 3,
98                 .right_margin   = 3,
99                 .upper_margin   = 2,
100                 .lower_margin   = 2,
101                 .hsync_len      = 80,
102                 .vsync_len      = 32,
103                 .sync           = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
104                 .vmode          = FB_VMODE_NONINTERLACED
105         },
106         {
107                 .name           = "1280x1024-60",
108                 .refresh        = 60,
109                 .xres           = 1280,
110                 .yres           = 1024,
111                 .pixclock       = 9375,
112                 .left_margin    = 38,
113                 .right_margin   = 128,
114                 .upper_margin   = 2,
115                 .lower_margin   = 7,
116                 .hsync_len      = 216,
117                 .vsync_len      = 37,
118                 .sync           = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
119                 .vmode          = FB_VMODE_NONINTERLACED
120         },
121         {
122                 .name           = "1280x1024-70",
123                 .refresh        = 70,
124                 .xres           = 1280,
125                 .yres           = 1024,
126                 .pixclock       = 9380,
127                 .left_margin    = 6,
128                 .right_margin   = 6,
129                 .upper_margin   = 4,
130                 .lower_margin   = 4,
131                 .hsync_len      = 60,
132                 .vsync_len      = 94,
133                 .sync           = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
134                 .vmode          = FB_VMODE_NONINTERLACED
135         },
136         {
137                 .name           = "1280x1024-75",
138                 .refresh        = 75,
139                 .xres           = 1280,
140                 .yres           = 1024,
141                 .pixclock       = 9380,
142                 .left_margin    = 6,
143                 .right_margin   = 6,
144                 .upper_margin   = 4,
145                 .lower_margin   = 4,
146                 .hsync_len      = 60,
147                 .vsync_len      = 15,
148                 .sync           = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
149                 .vmode          = FB_VMODE_NONINTERLACED
150         },
151         {
152                 .name           = "320x240",            /* for AOI only */
153                 .refresh        = 60,
154                 .xres           = 320,
155                 .yres           = 240,
156                 .pixclock       = 15385,
157                 .left_margin    = 0,
158                 .right_margin   = 0,
159                 .upper_margin   = 0,
160                 .lower_margin   = 0,
161                 .hsync_len      = 0,
162                 .vsync_len      = 0,
163                 .sync           = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
164                 .vmode          = FB_VMODE_NONINTERLACED
165         },
166         {
167                 .name           = "1280x480-60",
168                 .refresh        = 60,
169                 .xres           = 1280,
170                 .yres           = 480,
171                 .pixclock       = 18939,
172                 .left_margin    = 353,
173                 .right_margin   = 47,
174                 .upper_margin   = 39,
175                 .lower_margin   = 4,
176                 .hsync_len      = 8,
177                 .vsync_len      = 2,
178                 .sync           = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
179                 .vmode          = FB_VMODE_NONINTERLACED
180         },
181 };
182
183 static char *fb_mode = "1024x768-32@60";
184 static unsigned long default_bpp = 32;
185 static int monitor_port;
186
187 #if defined(CONFIG_NOT_COHERENT_CACHE)
188 static u8 *coherence_data;
189 static size_t coherence_data_size;
190 static unsigned int d_cache_line_size;
191 #endif
192
193 static DEFINE_SPINLOCK(diu_lock);
194
195 struct fsl_diu_data {
196         struct fb_info *fsl_diu_info[FSL_AOI_NUM - 1];
197                                 /*FSL_AOI_NUM has one dummy AOI */
198         struct device_attribute dev_attr;
199         struct diu_ad *dummy_ad;
200         void *dummy_aoi_virt;
201         unsigned int irq;
202         int fb_enabled;
203         int monitor_port;
204 };
205
206 struct mfb_info {
207         int index;
208         int type;
209         char *id;
210         int registered;
211         int blank;
212         unsigned long pseudo_palette[16];
213         struct diu_ad *ad;
214         int cursor_reset;
215         unsigned char g_alpha;
216         unsigned int count;
217         int x_aoi_d;            /* aoi display x offset to physical screen */
218         int y_aoi_d;            /* aoi display y offset to physical screen */
219         struct fsl_diu_data *parent;
220 };
221
222
223 static struct mfb_info mfb_template[] = {
224         {               /* AOI 0 for plane 0 */
225         .index = 0,
226         .type = MFB_TYPE_OUTPUT,
227         .id = "Panel0",
228         .registered = 0,
229         .count = 0,
230         .x_aoi_d = 0,
231         .y_aoi_d = 0,
232         },
233         {               /* AOI 0 for plane 1 */
234         .index = 1,
235         .type = MFB_TYPE_OUTPUT,
236         .id = "Panel1 AOI0",
237         .registered = 0,
238         .g_alpha = 0xff,
239         .count = 0,
240         .x_aoi_d = 0,
241         .y_aoi_d = 0,
242         },
243         {               /* AOI 1 for plane 1 */
244         .index = 2,
245         .type = MFB_TYPE_OUTPUT,
246         .id = "Panel1 AOI1",
247         .registered = 0,
248         .g_alpha = 0xff,
249         .count = 0,
250         .x_aoi_d = 0,
251         .y_aoi_d = 480,
252         },
253         {               /* AOI 0 for plane 2 */
254         .index = 3,
255         .type = MFB_TYPE_OUTPUT,
256         .id = "Panel2 AOI0",
257         .registered = 0,
258         .g_alpha = 0xff,
259         .count = 0,
260         .x_aoi_d = 640,
261         .y_aoi_d = 0,
262         },
263         {               /* AOI 1 for plane 2 */
264         .index = 4,
265         .type = MFB_TYPE_OUTPUT,
266         .id = "Panel2 AOI1",
267         .registered = 0,
268         .g_alpha = 0xff,
269         .count = 0,
270         .x_aoi_d = 640,
271         .y_aoi_d = 480,
272         },
273 };
274
275 static struct diu_hw dr = {
276         .mode = MFB_MODE1,
277         .reg_lock = __SPIN_LOCK_UNLOCKED(diu_hw.reg_lock),
278 };
279
280 static struct diu_pool pool;
281
282 /**
283  * fsl_diu_alloc - allocate memory for the DIU
284  * @size: number of bytes to allocate
285  * @param: returned physical address of memory
286  *
287  * This function allocates a physically-contiguous block of memory.
288  */
289 static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
290 {
291         void *virt;
292
293         pr_debug("size=%zu\n", size);
294
295         virt = alloc_pages_exact(size, GFP_DMA | __GFP_ZERO);
296         if (virt) {
297                 *phys = virt_to_phys(virt);
298                 pr_debug("virt=%p phys=%llx\n", virt,
299                         (unsigned long long)*phys);
300         }
301
302         return virt;
303 }
304
305 /**
306  * fsl_diu_free - release DIU memory
307  * @virt: pointer returned by fsl_diu_alloc()
308  * @size: number of bytes allocated by fsl_diu_alloc()
309  *
310  * This function releases memory allocated by fsl_diu_alloc().
311  */
312 static void fsl_diu_free(void *virt, size_t size)
313 {
314         pr_debug("virt=%p size=%zu\n", virt, size);
315
316         if (virt && size)
317                 free_pages_exact(virt, size);
318 }
319
320 static int fsl_diu_enable_panel(struct fb_info *info)
321 {
322         struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
323         struct diu *hw = dr.diu_reg;
324         struct diu_ad *ad = mfbi->ad;
325         struct fsl_diu_data *machine_data = mfbi->parent;
326         int res = 0;
327
328         pr_debug("enable_panel index %d\n", mfbi->index);
329         if (mfbi->type != MFB_TYPE_OFF) {
330                 switch (mfbi->index) {
331                 case 0:                         /* plane 0 */
332                         if (hw->desc[0] != ad->paddr)
333                                 out_be32(&hw->desc[0], ad->paddr);
334                         break;
335                 case 1:                         /* plane 1 AOI 0 */
336                         cmfbi = machine_data->fsl_diu_info[2]->par;
337                         if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
338                                 if (cmfbi->count > 0)   /* AOI1 open */
339                                         ad->next_ad =
340                                                 cpu_to_le32(cmfbi->ad->paddr);
341                                 else
342                                         ad->next_ad = 0;
343                                 out_be32(&hw->desc[1], ad->paddr);
344                         }
345                         break;
346                 case 3:                         /* plane 2 AOI 0 */
347                         cmfbi = machine_data->fsl_diu_info[4]->par;
348                         if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
349                                 if (cmfbi->count > 0)   /* AOI1 open */
350                                         ad->next_ad =
351                                                 cpu_to_le32(cmfbi->ad->paddr);
352                                 else
353                                         ad->next_ad = 0;
354                                 out_be32(&hw->desc[2], ad->paddr);
355                         }
356                         break;
357                 case 2:                         /* plane 1 AOI 1 */
358                         pmfbi = machine_data->fsl_diu_info[1]->par;
359                         ad->next_ad = 0;
360                         if (hw->desc[1] == machine_data->dummy_ad->paddr)
361                                 out_be32(&hw->desc[1], ad->paddr);
362                         else                                    /* AOI0 open */
363                                 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
364                         break;
365                 case 4:                         /* plane 2 AOI 1 */
366                         pmfbi = machine_data->fsl_diu_info[3]->par;
367                         ad->next_ad = 0;
368                         if (hw->desc[2] == machine_data->dummy_ad->paddr)
369                                 out_be32(&hw->desc[2], ad->paddr);
370                         else                            /* AOI0 was open */
371                                 pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
372                         break;
373                 default:
374                         res = -EINVAL;
375                         break;
376                 }
377         } else
378                 res = -EINVAL;
379         return res;
380 }
381
382 static int fsl_diu_disable_panel(struct fb_info *info)
383 {
384         struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
385         struct diu *hw = dr.diu_reg;
386         struct diu_ad *ad = mfbi->ad;
387         struct fsl_diu_data *machine_data = mfbi->parent;
388         int res = 0;
389
390         switch (mfbi->index) {
391         case 0:                                 /* plane 0 */
392                 if (hw->desc[0] != machine_data->dummy_ad->paddr)
393                         out_be32(&hw->desc[0],
394                                 machine_data->dummy_ad->paddr);
395                 break;
396         case 1:                                 /* plane 1 AOI 0 */
397                 cmfbi = machine_data->fsl_diu_info[2]->par;
398                 if (cmfbi->count > 0)   /* AOI1 is open */
399                         out_be32(&hw->desc[1], cmfbi->ad->paddr);
400                                         /* move AOI1 to the first */
401                 else                    /* AOI1 was closed */
402                         out_be32(&hw->desc[1],
403                                 machine_data->dummy_ad->paddr);
404                                         /* close AOI 0 */
405                 break;
406         case 3:                                 /* plane 2 AOI 0 */
407                 cmfbi = machine_data->fsl_diu_info[4]->par;
408                 if (cmfbi->count > 0)   /* AOI1 is open */
409                         out_be32(&hw->desc[2], cmfbi->ad->paddr);
410                                         /* move AOI1 to the first */
411                 else                    /* AOI1 was closed */
412                         out_be32(&hw->desc[2],
413                                 machine_data->dummy_ad->paddr);
414                                         /* close AOI 0 */
415                 break;
416         case 2:                                 /* plane 1 AOI 1 */
417                 pmfbi = machine_data->fsl_diu_info[1]->par;
418                 if (hw->desc[1] != ad->paddr) {
419                                 /* AOI1 is not the first in the chain */
420                         if (pmfbi->count > 0)
421                                         /* AOI0 is open, must be the first */
422                                 pmfbi->ad->next_ad = 0;
423                 } else                  /* AOI1 is the first in the chain */
424                         out_be32(&hw->desc[1], machine_data->dummy_ad->paddr);
425                                         /* close AOI 1 */
426                 break;
427         case 4:                                 /* plane 2 AOI 1 */
428                 pmfbi = machine_data->fsl_diu_info[3]->par;
429                 if (hw->desc[2] != ad->paddr) {
430                                 /* AOI1 is not the first in the chain */
431                         if (pmfbi->count > 0)
432                                 /* AOI0 is open, must be the first */
433                                 pmfbi->ad->next_ad = 0;
434                 } else          /* AOI1 is the first in the chain */
435                         out_be32(&hw->desc[2], machine_data->dummy_ad->paddr);
436                                 /* close AOI 1 */
437                 break;
438         default:
439                 res = -EINVAL;
440                 break;
441         }
442
443         return res;
444 }
445
446 static void enable_lcdc(struct fb_info *info)
447 {
448         struct diu *hw = dr.diu_reg;
449         struct mfb_info *mfbi = info->par;
450         struct fsl_diu_data *machine_data = mfbi->parent;
451
452         if (!machine_data->fb_enabled) {
453                 out_be32(&hw->diu_mode, dr.mode);
454                 machine_data->fb_enabled++;
455         }
456 }
457
458 static void disable_lcdc(struct fb_info *info)
459 {
460         struct diu *hw = dr.diu_reg;
461         struct mfb_info *mfbi = info->par;
462         struct fsl_diu_data *machine_data = mfbi->parent;
463
464         if (machine_data->fb_enabled) {
465                 out_be32(&hw->diu_mode, 0);
466                 machine_data->fb_enabled = 0;
467         }
468 }
469
470 static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
471                                 struct fb_info *info)
472 {
473         struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
474         struct fsl_diu_data *machine_data = mfbi->parent;
475         int available_height, upper_aoi_bottom, index = mfbi->index;
476         int lower_aoi_is_open, upper_aoi_is_open;
477         __u32 base_plane_width, base_plane_height, upper_aoi_height;
478
479         base_plane_width = machine_data->fsl_diu_info[0]->var.xres;
480         base_plane_height = machine_data->fsl_diu_info[0]->var.yres;
481
482         switch (index) {
483         case 0:
484                 if (mfbi->x_aoi_d != 0)
485                         mfbi->x_aoi_d = 0;
486                 if (mfbi->y_aoi_d != 0)
487                         mfbi->y_aoi_d = 0;
488                 break;
489         case 1:                 /* AOI 0 */
490         case 3:
491                 lower_aoi_mfbi = machine_data->fsl_diu_info[index+1]->par;
492                 lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
493                 if (var->xres > base_plane_width)
494                         var->xres = base_plane_width;
495                 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
496                         mfbi->x_aoi_d = base_plane_width - var->xres;
497
498                 if (lower_aoi_is_open)
499                         available_height = lower_aoi_mfbi->y_aoi_d;
500                 else
501                         available_height = base_plane_height;
502                 if (var->yres > available_height)
503                         var->yres = available_height;
504                 if ((mfbi->y_aoi_d + var->yres) > available_height)
505                         mfbi->y_aoi_d = available_height - var->yres;
506                 break;
507         case 2:                 /* AOI 1 */
508         case 4:
509                 upper_aoi_mfbi = machine_data->fsl_diu_info[index-1]->par;
510                 upper_aoi_height =
511                                 machine_data->fsl_diu_info[index-1]->var.yres;
512                 upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
513                 upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
514                 if (var->xres > base_plane_width)
515                         var->xres = base_plane_width;
516                 if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
517                         mfbi->x_aoi_d = base_plane_width - var->xres;
518                 if (mfbi->y_aoi_d < 0)
519                         mfbi->y_aoi_d = 0;
520                 if (upper_aoi_is_open) {
521                         if (mfbi->y_aoi_d < upper_aoi_bottom)
522                                 mfbi->y_aoi_d = upper_aoi_bottom;
523                         available_height = base_plane_height
524                                                 - upper_aoi_bottom;
525                 } else
526                         available_height = base_plane_height;
527                 if (var->yres > available_height)
528                         var->yres = available_height;
529                 if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
530                         mfbi->y_aoi_d = base_plane_height - var->yres;
531                 break;
532         }
533 }
534 /*
535  * Checks to see if the hardware supports the state requested by var passed
536  * in. This function does not alter the hardware state! If the var passed in
537  * is slightly off by what the hardware can support then we alter the var
538  * PASSED in to what we can do. If the hardware doesn't support mode change
539  * a -EINVAL will be returned by the upper layers.
540  */
541 static int fsl_diu_check_var(struct fb_var_screeninfo *var,
542                                 struct fb_info *info)
543 {
544         unsigned long htotal, vtotal;
545
546         pr_debug("check_var xres: %d\n", var->xres);
547         pr_debug("check_var yres: %d\n", var->yres);
548
549         if (var->xres_virtual < var->xres)
550                 var->xres_virtual = var->xres;
551         if (var->yres_virtual < var->yres)
552                 var->yres_virtual = var->yres;
553
554         if (var->xoffset < 0)
555                 var->xoffset = 0;
556
557         if (var->yoffset < 0)
558                 var->yoffset = 0;
559
560         if (var->xoffset + info->var.xres > info->var.xres_virtual)
561                 var->xoffset = info->var.xres_virtual - info->var.xres;
562
563         if (var->yoffset + info->var.yres > info->var.yres_virtual)
564                 var->yoffset = info->var.yres_virtual - info->var.yres;
565
566         if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
567             (var->bits_per_pixel != 16))
568                 var->bits_per_pixel = default_bpp;
569
570         switch (var->bits_per_pixel) {
571         case 16:
572                 var->red.length = 5;
573                 var->red.offset = 11;
574                 var->red.msb_right = 0;
575
576                 var->green.length = 6;
577                 var->green.offset = 5;
578                 var->green.msb_right = 0;
579
580                 var->blue.length = 5;
581                 var->blue.offset = 0;
582                 var->blue.msb_right = 0;
583
584                 var->transp.length = 0;
585                 var->transp.offset = 0;
586                 var->transp.msb_right = 0;
587                 break;
588         case 24:
589                 var->red.length = 8;
590                 var->red.offset = 0;
591                 var->red.msb_right = 0;
592
593                 var->green.length = 8;
594                 var->green.offset = 8;
595                 var->green.msb_right = 0;
596
597                 var->blue.length = 8;
598                 var->blue.offset = 16;
599                 var->blue.msb_right = 0;
600
601                 var->transp.length = 0;
602                 var->transp.offset = 0;
603                 var->transp.msb_right = 0;
604                 break;
605         case 32:
606                 var->red.length = 8;
607                 var->red.offset = 16;
608                 var->red.msb_right = 0;
609
610                 var->green.length = 8;
611                 var->green.offset = 8;
612                 var->green.msb_right = 0;
613
614                 var->blue.length = 8;
615                 var->blue.offset = 0;
616                 var->blue.msb_right = 0;
617
618                 var->transp.length = 8;
619                 var->transp.offset = 24;
620                 var->transp.msb_right = 0;
621
622                 break;
623         }
624         /* If the pixclock is below the minimum spec'd value then set to
625          * refresh rate for 60Hz since this is supported by most monitors.
626          * Refer to Documentation/fb/ for calculations.
627          */
628         if ((var->pixclock < MIN_PIX_CLK) || (var->pixclock > MAX_PIX_CLK)) {
629                 htotal = var->xres + var->right_margin + var->hsync_len +
630                     var->left_margin;
631                 vtotal = var->yres + var->lower_margin + var->vsync_len +
632                     var->upper_margin;
633                 var->pixclock = (vtotal * htotal * 6UL) / 100UL;
634                 var->pixclock = KHZ2PICOS(var->pixclock);
635                 pr_debug("pixclock set for 60Hz refresh = %u ps\n",
636                         var->pixclock);
637         }
638
639         var->height = -1;
640         var->width = -1;
641         var->grayscale = 0;
642
643         /* Copy nonstd field to/from sync for fbset usage */
644         var->sync |= var->nonstd;
645         var->nonstd |= var->sync;
646
647         adjust_aoi_size_position(var, info);
648         return 0;
649 }
650
651 static void set_fix(struct fb_info *info)
652 {
653         struct fb_fix_screeninfo *fix = &info->fix;
654         struct fb_var_screeninfo *var = &info->var;
655         struct mfb_info *mfbi = info->par;
656
657         strncpy(fix->id, mfbi->id, strlen(mfbi->id));
658         fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
659         fix->type = FB_TYPE_PACKED_PIXELS;
660         fix->accel = FB_ACCEL_NONE;
661         fix->visual = FB_VISUAL_TRUECOLOR;
662         fix->xpanstep = 1;
663         fix->ypanstep = 1;
664 }
665
666 static void update_lcdc(struct fb_info *info)
667 {
668         struct fb_var_screeninfo *var = &info->var;
669         struct mfb_info *mfbi = info->par;
670         struct fsl_diu_data *machine_data = mfbi->parent;
671         struct diu *hw;
672         int i, j;
673         char __iomem *cursor_base, *gamma_table_base;
674
675         u32 temp;
676
677         hw = dr.diu_reg;
678
679         if (mfbi->type == MFB_TYPE_OFF) {
680                 fsl_diu_disable_panel(info);
681                 return;
682         }
683
684         diu_ops.set_monitor_port(machine_data->monitor_port);
685         gamma_table_base = pool.gamma.vaddr;
686         cursor_base = pool.cursor.vaddr;
687         /* Prep for DIU init  - gamma table, cursor table */
688
689         for (i = 0; i <= 2; i++)
690            for (j = 0; j <= 255; j++)
691               *gamma_table_base++ = j;
692
693         diu_ops.set_gamma_table(machine_data->monitor_port, pool.gamma.vaddr);
694
695         pr_debug("update-lcdc: HW - %p\n Disabling DIU\n", hw);
696         disable_lcdc(info);
697
698         /* Program DIU registers */
699
700         out_be32(&hw->gamma, pool.gamma.paddr);
701         out_be32(&hw->cursor, pool.cursor.paddr);
702
703         out_be32(&hw->bgnd, 0x007F7F7F);        /* BGND */
704         out_be32(&hw->bgnd_wb, 0);              /* BGND_WB */
705         out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
706                                                 /* DISP SIZE */
707         pr_debug("DIU xres: %d\n", var->xres);
708         pr_debug("DIU yres: %d\n", var->yres);
709
710         out_be32(&hw->wb_size, 0); /* WB SIZE */
711         out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
712
713         /* Horizontal and vertical configuration register */
714         temp = var->left_margin << 22 | /* BP_H */
715                var->hsync_len << 11 |   /* PW_H */
716                var->right_margin;       /* FP_H */
717
718         out_be32(&hw->hsyn_para, temp);
719
720         temp = var->upper_margin << 22 | /* BP_V */
721                var->vsync_len << 11 |    /* PW_V  */
722                var->lower_margin;        /* FP_V  */
723
724         out_be32(&hw->vsyn_para, temp);
725
726         pr_debug("DIU right_margin - %d\n", var->right_margin);
727         pr_debug("DIU left_margin - %d\n", var->left_margin);
728         pr_debug("DIU hsync_len - %d\n", var->hsync_len);
729         pr_debug("DIU upper_margin - %d\n", var->upper_margin);
730         pr_debug("DIU lower_margin - %d\n", var->lower_margin);
731         pr_debug("DIU vsync_len - %d\n", var->vsync_len);
732         pr_debug("DIU HSYNC - 0x%08x\n", hw->hsyn_para);
733         pr_debug("DIU VSYNC - 0x%08x\n", hw->vsyn_para);
734
735         diu_ops.set_pixel_clock(var->pixclock);
736
737         out_be32(&hw->syn_pol, 0);      /* SYNC SIGNALS POLARITY */
738         out_be32(&hw->thresholds, 0x00037800); /* The Thresholds */
739         out_be32(&hw->int_status, 0);   /* INTERRUPT STATUS */
740         out_be32(&hw->plut, 0x01F5F666);
741
742         /* Enable the DIU */
743         enable_lcdc(info);
744 }
745
746 static int map_video_memory(struct fb_info *info)
747 {
748         phys_addr_t phys;
749
750         pr_debug("info->var.xres_virtual = %d\n", info->var.xres_virtual);
751         pr_debug("info->var.yres_virtual = %d\n", info->var.yres_virtual);
752         pr_debug("info->fix.line_length  = %d\n", info->fix.line_length);
753
754         info->fix.smem_len = info->fix.line_length * info->var.yres_virtual;
755         pr_debug("MAP_VIDEO_MEMORY: smem_len = %d\n", info->fix.smem_len);
756         info->screen_base = fsl_diu_alloc(info->fix.smem_len, &phys);
757         if (info->screen_base == NULL) {
758                 printk(KERN_ERR "Unable to allocate fb memory\n");
759                 return -ENOMEM;
760         }
761         info->fix.smem_start = (unsigned long) phys;
762         info->screen_size = info->fix.smem_len;
763
764         pr_debug("Allocated fb @ paddr=0x%08lx, size=%d.\n",
765                                 info->fix.smem_start,
766                 info->fix.smem_len);
767         pr_debug("screen base %p\n", info->screen_base);
768
769         return 0;
770 }
771
772 static void unmap_video_memory(struct fb_info *info)
773 {
774         fsl_diu_free(info->screen_base, info->fix.smem_len);
775         info->screen_base = NULL;
776         info->fix.smem_start = 0;
777         info->fix.smem_len = 0;
778 }
779
780 /*
781  * Using the fb_var_screeninfo in fb_info we set the aoi of this
782  * particular framebuffer. It is a light version of fsl_diu_set_par.
783  */
784 static int fsl_diu_set_aoi(struct fb_info *info)
785 {
786         struct fb_var_screeninfo *var = &info->var;
787         struct mfb_info *mfbi = info->par;
788         struct diu_ad *ad = mfbi->ad;
789
790         /* AOI should not be greater than display size */
791         ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
792         ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
793         return 0;
794 }
795
796 /*
797  * Using the fb_var_screeninfo in fb_info we set the resolution of this
798  * particular framebuffer. This function alters the fb_fix_screeninfo stored
799  * in fb_info. It does not alter var in fb_info since we are using that
800  * data. This means we depend on the data in var inside fb_info to be
801  * supported by the hardware. fsl_diu_check_var is always called before
802  * fsl_diu_set_par to ensure this.
803  */
804 static int fsl_diu_set_par(struct fb_info *info)
805 {
806         unsigned long len;
807         struct fb_var_screeninfo *var = &info->var;
808         struct mfb_info *mfbi = info->par;
809         struct fsl_diu_data *machine_data = mfbi->parent;
810         struct diu_ad *ad = mfbi->ad;
811         struct diu *hw;
812
813         hw = dr.diu_reg;
814
815         set_fix(info);
816         mfbi->cursor_reset = 1;
817
818         len = info->var.yres_virtual * info->fix.line_length;
819         /* Alloc & dealloc each time resolution/bpp change */
820         if (len != info->fix.smem_len) {
821                 if (info->fix.smem_start)
822                         unmap_video_memory(info);
823                 pr_debug("SET PAR: smem_len = %d\n", info->fix.smem_len);
824
825                 /* Memory allocation for framebuffer */
826                 if (map_video_memory(info)) {
827                         printk(KERN_ERR "Unable to allocate fb memory 1\n");
828                         return -ENOMEM;
829                 }
830         }
831
832         ad->pix_fmt =
833                 diu_ops.get_pixel_format(var->bits_per_pixel,
834                                          machine_data->monitor_port);
835         ad->addr    = cpu_to_le32(info->fix.smem_start);
836         ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
837                                 var->xres_virtual) | mfbi->g_alpha;
838         /* AOI should not be greater than display size */
839         ad->aoi_size    = cpu_to_le32((var->yres << 16) | var->xres);
840         ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
841         ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
842
843         /* Disable chroma keying function */
844         ad->ckmax_r = 0;
845         ad->ckmax_g = 0;
846         ad->ckmax_b = 0;
847
848         ad->ckmin_r = 255;
849         ad->ckmin_g = 255;
850         ad->ckmin_b = 255;
851
852         if (mfbi->index == 0)
853                 update_lcdc(info);
854         return 0;
855 }
856
857 static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
858 {
859         return ((val<<width) + 0x7FFF - val)>>16;
860 }
861
862 /*
863  * Set a single color register. The values supplied have a 16 bit magnitude
864  * which needs to be scaled in this function for the hardware. Things to take
865  * into consideration are how many color registers, if any, are supported with
866  * the current color visual. With truecolor mode no color palettes are
867  * supported. Here a psuedo palette is created which we store the value in
868  * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
869  * color palette.
870  */
871 static int fsl_diu_setcolreg(unsigned regno, unsigned red, unsigned green,
872                            unsigned blue, unsigned transp, struct fb_info *info)
873 {
874         int ret = 1;
875
876         /*
877          * If greyscale is true, then we convert the RGB value
878          * to greyscale no matter what visual we are using.
879          */
880         if (info->var.grayscale)
881                 red = green = blue = (19595 * red + 38470 * green +
882                                       7471 * blue) >> 16;
883         switch (info->fix.visual) {
884         case FB_VISUAL_TRUECOLOR:
885                 /*
886                  * 16-bit True Colour.  We encode the RGB value
887                  * according to the RGB bitfield information.
888                  */
889                 if (regno < 16) {
890                         u32 *pal = info->pseudo_palette;
891                         u32 v;
892
893                         red = CNVT_TOHW(red, info->var.red.length);
894                         green = CNVT_TOHW(green, info->var.green.length);
895                         blue = CNVT_TOHW(blue, info->var.blue.length);
896                         transp = CNVT_TOHW(transp, info->var.transp.length);
897
898                         v = (red << info->var.red.offset) |
899                             (green << info->var.green.offset) |
900                             (blue << info->var.blue.offset) |
901                             (transp << info->var.transp.offset);
902
903                         pal[regno] = v;
904                         ret = 0;
905                 }
906                 break;
907         case FB_VISUAL_STATIC_PSEUDOCOLOR:
908         case FB_VISUAL_PSEUDOCOLOR:
909                 break;
910         }
911
912         return ret;
913 }
914
915 /*
916  * Pan (or wrap, depending on the `vmode' field) the display using the
917  * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
918  * don't fit, return -EINVAL.
919  */
920 static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
921                              struct fb_info *info)
922 {
923         if ((info->var.xoffset == var->xoffset) &&
924             (info->var.yoffset == var->yoffset))
925                 return 0;       /* No change, do nothing */
926
927         if (var->xoffset < 0 || var->yoffset < 0
928             || var->xoffset + info->var.xres > info->var.xres_virtual
929             || var->yoffset + info->var.yres > info->var.yres_virtual)
930                 return -EINVAL;
931
932         info->var.xoffset = var->xoffset;
933         info->var.yoffset = var->yoffset;
934
935         if (var->vmode & FB_VMODE_YWRAP)
936                 info->var.vmode |= FB_VMODE_YWRAP;
937         else
938                 info->var.vmode &= ~FB_VMODE_YWRAP;
939
940         fsl_diu_set_aoi(info);
941
942         return 0;
943 }
944
945 /*
946  * Blank the screen if blank_mode != 0, else unblank. Return 0 if blanking
947  * succeeded, != 0 if un-/blanking failed.
948  * blank_mode == 2: suspend vsync
949  * blank_mode == 3: suspend hsync
950  * blank_mode == 4: powerdown
951  */
952 static int fsl_diu_blank(int blank_mode, struct fb_info *info)
953 {
954         struct mfb_info *mfbi = info->par;
955
956         mfbi->blank = blank_mode;
957
958         switch (blank_mode) {
959         case FB_BLANK_VSYNC_SUSPEND:
960         case FB_BLANK_HSYNC_SUSPEND:
961         /* FIXME: fixes to enable_panel and enable lcdc needed */
962         case FB_BLANK_NORMAL:
963         /*      fsl_diu_disable_panel(info);*/
964                 break;
965         case FB_BLANK_POWERDOWN:
966         /*      disable_lcdc(info);     */
967                 break;
968         case FB_BLANK_UNBLANK:
969         /*      fsl_diu_enable_panel(info);*/
970                 break;
971         }
972
973         return 0;
974 }
975
976 static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
977                        unsigned long arg)
978 {
979         struct mfb_info *mfbi = info->par;
980         struct diu_ad *ad = mfbi->ad;
981         struct mfb_chroma_key ck;
982         unsigned char global_alpha;
983         struct aoi_display_offset aoi_d;
984         __u32 pix_fmt;
985         void __user *buf = (void __user *)arg;
986
987         if (!arg)
988                 return -EINVAL;
989         switch (cmd) {
990         case MFB_SET_PIXFMT:
991                 if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
992                         return -EFAULT;
993                 ad->pix_fmt = pix_fmt;
994                 pr_debug("Set pixel format to 0x%08x\n", ad->pix_fmt);
995                 break;
996         case MFB_GET_PIXFMT:
997                 pix_fmt = ad->pix_fmt;
998                 if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
999                         return -EFAULT;
1000                 pr_debug("get pixel format 0x%08x\n", ad->pix_fmt);
1001                 break;
1002         case MFB_SET_AOID:
1003                 if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
1004                         return -EFAULT;
1005                 mfbi->x_aoi_d = aoi_d.x_aoi_d;
1006                 mfbi->y_aoi_d = aoi_d.y_aoi_d;
1007                 pr_debug("set AOI display offset of index %d to (%d,%d)\n",
1008                                  mfbi->index, aoi_d.x_aoi_d, aoi_d.y_aoi_d);
1009                 fsl_diu_check_var(&info->var, info);
1010                 fsl_diu_set_aoi(info);
1011                 break;
1012         case MFB_GET_AOID:
1013                 aoi_d.x_aoi_d = mfbi->x_aoi_d;
1014                 aoi_d.y_aoi_d = mfbi->y_aoi_d;
1015                 if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
1016                         return -EFAULT;
1017                 pr_debug("get AOI display offset of index %d (%d,%d)\n",
1018                                 mfbi->index, aoi_d.x_aoi_d, aoi_d.y_aoi_d);
1019                 break;
1020         case MFB_GET_ALPHA:
1021                 global_alpha = mfbi->g_alpha;
1022                 if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
1023                         return -EFAULT;
1024                 pr_debug("get global alpha of index %d\n", mfbi->index);
1025                 break;
1026         case MFB_SET_ALPHA:
1027                 /* set panel information */
1028                 if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
1029                         return -EFAULT;
1030                 ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
1031                                                         (global_alpha & 0xff);
1032                 mfbi->g_alpha = global_alpha;
1033                 pr_debug("set global alpha for index %d\n", mfbi->index);
1034                 break;
1035         case MFB_SET_CHROMA_KEY:
1036                 /* set panel winformation */
1037                 if (copy_from_user(&ck, buf, sizeof(ck)))
1038                         return -EFAULT;
1039
1040                 if (ck.enable &&
1041                    (ck.red_max < ck.red_min ||
1042                     ck.green_max < ck.green_min ||
1043                     ck.blue_max < ck.blue_min))
1044                         return -EINVAL;
1045
1046                 if (!ck.enable) {
1047                         ad->ckmax_r = 0;
1048                         ad->ckmax_g = 0;
1049                         ad->ckmax_b = 0;
1050                         ad->ckmin_r = 255;
1051                         ad->ckmin_g = 255;
1052                         ad->ckmin_b = 255;
1053                 } else {
1054                         ad->ckmax_r = ck.red_max;
1055                         ad->ckmax_g = ck.green_max;
1056                         ad->ckmax_b = ck.blue_max;
1057                         ad->ckmin_r = ck.red_min;
1058                         ad->ckmin_g = ck.green_min;
1059                         ad->ckmin_b = ck.blue_min;
1060                 }
1061                 pr_debug("set chroma key\n");
1062                 break;
1063         case FBIOGET_GWINFO:
1064                 if (mfbi->type == MFB_TYPE_OFF)
1065                         return -ENODEV;
1066                 /* get graphic window information */
1067                 if (copy_to_user(buf, ad, sizeof(*ad)))
1068                         return -EFAULT;
1069                 break;
1070         case FBIOGET_HWCINFO:
1071                 pr_debug("FBIOGET_HWCINFO:0x%08x\n", FBIOGET_HWCINFO);
1072                 break;
1073         case FBIOPUT_MODEINFO:
1074                 pr_debug("FBIOPUT_MODEINFO:0x%08x\n", FBIOPUT_MODEINFO);
1075                 break;
1076         case FBIOGET_DISPINFO:
1077                 pr_debug("FBIOGET_DISPINFO:0x%08x\n", FBIOGET_DISPINFO);
1078                 break;
1079
1080         default:
1081                 printk(KERN_ERR "Unknown ioctl command (0x%08X)\n", cmd);
1082                 return -ENOIOCTLCMD;
1083         }
1084
1085         return 0;
1086 }
1087
1088 /* turn on fb if count == 1
1089  */
1090 static int fsl_diu_open(struct fb_info *info, int user)
1091 {
1092         struct mfb_info *mfbi = info->par;
1093         int res = 0;
1094
1095         spin_lock(&diu_lock);
1096         mfbi->count++;
1097         if (mfbi->count == 1) {
1098                 pr_debug("open plane index %d\n", mfbi->index);
1099                 fsl_diu_check_var(&info->var, info);
1100                 res = fsl_diu_set_par(info);
1101                 if (res < 0)
1102                         mfbi->count--;
1103                 else {
1104                         res = fsl_diu_enable_panel(info);
1105                         if (res < 0)
1106                                 mfbi->count--;
1107                 }
1108         }
1109
1110         spin_unlock(&diu_lock);
1111         return res;
1112 }
1113
1114 /* turn off fb if count == 0
1115  */
1116 static int fsl_diu_release(struct fb_info *info, int user)
1117 {
1118         struct mfb_info *mfbi = info->par;
1119         int res = 0;
1120
1121         spin_lock(&diu_lock);
1122         mfbi->count--;
1123         if (mfbi->count == 0) {
1124                 pr_debug("release plane index %d\n", mfbi->index);
1125                 res = fsl_diu_disable_panel(info);
1126                 if (res < 0)
1127                         mfbi->count++;
1128         }
1129         spin_unlock(&diu_lock);
1130         return res;
1131 }
1132
1133 static struct fb_ops fsl_diu_ops = {
1134         .owner = THIS_MODULE,
1135         .fb_check_var = fsl_diu_check_var,
1136         .fb_set_par = fsl_diu_set_par,
1137         .fb_setcolreg = fsl_diu_setcolreg,
1138         .fb_blank = fsl_diu_blank,
1139         .fb_pan_display = fsl_diu_pan_display,
1140         .fb_fillrect = cfb_fillrect,
1141         .fb_copyarea = cfb_copyarea,
1142         .fb_imageblit = cfb_imageblit,
1143         .fb_ioctl = fsl_diu_ioctl,
1144         .fb_open = fsl_diu_open,
1145         .fb_release = fsl_diu_release,
1146 };
1147
1148 static int init_fbinfo(struct fb_info *info)
1149 {
1150         struct mfb_info *mfbi = info->par;
1151
1152         info->device = NULL;
1153         info->var.activate = FB_ACTIVATE_NOW;
1154         info->fbops = &fsl_diu_ops;
1155         info->flags = FBINFO_FLAG_DEFAULT;
1156         info->pseudo_palette = &mfbi->pseudo_palette;
1157
1158         /* Allocate colormap */
1159         fb_alloc_cmap(&info->cmap, 16, 0);
1160         return 0;
1161 }
1162
1163 static int __devinit install_fb(struct fb_info *info)
1164 {
1165         int rc;
1166         struct mfb_info *mfbi = info->par;
1167         const char *aoi_mode, *init_aoi_mode = "320x240";
1168
1169         if (init_fbinfo(info))
1170                 return -EINVAL;
1171
1172         if (mfbi->index == 0)   /* plane 0 */
1173                 aoi_mode = fb_mode;
1174         else
1175                 aoi_mode = init_aoi_mode;
1176         pr_debug("mode used = %s\n", aoi_mode);
1177         rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
1178              ARRAY_SIZE(fsl_diu_mode_db), &fsl_diu_default_mode, default_bpp);
1179
1180         switch (rc) {
1181         case 1:
1182                 pr_debug("using mode specified in @mode\n");
1183                 break;
1184         case 2:
1185                 pr_debug("using mode specified in @mode "
1186                         "with ignored refresh rate\n");
1187                 break;
1188         case 3:
1189                 pr_debug("using mode default mode\n");
1190                 break;
1191         case 4:
1192                 pr_debug("using mode from list\n");
1193                 break;
1194         default:
1195                 pr_debug("rc = %d\n", rc);
1196                 pr_debug("failed to find mode\n");
1197                 return -EINVAL;
1198                 break;
1199         }
1200
1201         pr_debug("xres_virtual %d\n", info->var.xres_virtual);
1202         pr_debug("bits_per_pixel %d\n", info->var.bits_per_pixel);
1203
1204         pr_debug("info->var.yres_virtual = %d\n", info->var.yres_virtual);
1205         pr_debug("info->fix.line_length = %d\n", info->fix.line_length);
1206
1207         if (mfbi->type == MFB_TYPE_OFF)
1208                 mfbi->blank = FB_BLANK_NORMAL;
1209         else
1210                 mfbi->blank = FB_BLANK_UNBLANK;
1211
1212         if (fsl_diu_check_var(&info->var, info)) {
1213                 printk(KERN_ERR "fb_check_var failed");
1214                 fb_dealloc_cmap(&info->cmap);
1215                 return -EINVAL;
1216         }
1217
1218         if (fsl_diu_set_par(info)) {
1219                 printk(KERN_ERR "fb_set_par failed");
1220                 fb_dealloc_cmap(&info->cmap);
1221                 return -EINVAL;
1222         }
1223
1224         if (register_framebuffer(info) < 0) {
1225                 printk(KERN_ERR "register_framebuffer failed");
1226                 unmap_video_memory(info);
1227                 fb_dealloc_cmap(&info->cmap);
1228                 return -EINVAL;
1229         }
1230
1231         mfbi->registered = 1;
1232         printk(KERN_INFO "fb%d: %s fb device registered successfully.\n",
1233                  info->node, info->fix.id);
1234
1235         return 0;
1236 }
1237
1238 static void uninstall_fb(struct fb_info *info)
1239 {
1240         struct mfb_info *mfbi = info->par;
1241
1242         if (!mfbi->registered)
1243                 return;
1244
1245         unregister_framebuffer(info);
1246         unmap_video_memory(info);
1247         if (&info->cmap)
1248                 fb_dealloc_cmap(&info->cmap);
1249
1250         mfbi->registered = 0;
1251 }
1252
1253 static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
1254 {
1255         struct diu *hw = dr.diu_reg;
1256         unsigned int status = in_be32(&hw->int_status);
1257
1258         if (status) {
1259                 /* This is the workaround for underrun */
1260                 if (status & INT_UNDRUN) {
1261                         out_be32(&hw->diu_mode, 0);
1262                         pr_debug("Err: DIU occurs underrun!\n");
1263                         udelay(1);
1264                         out_be32(&hw->diu_mode, 1);
1265                 }
1266 #if defined(CONFIG_NOT_COHERENT_CACHE)
1267                 else if (status & INT_VSYNC) {
1268                         unsigned int i;
1269                         for (i = 0; i < coherence_data_size;
1270                                 i += d_cache_line_size)
1271                                 __asm__ __volatile__ (
1272                                         "dcbz 0, %[input]"
1273                                 ::[input]"r"(&coherence_data[i]));
1274                 }
1275 #endif
1276                 return IRQ_HANDLED;
1277         }
1278         return IRQ_NONE;
1279 }
1280
1281 static int request_irq_local(int irq)
1282 {
1283         unsigned long status, ints;
1284         struct diu *hw;
1285         int ret;
1286
1287         hw = dr.diu_reg;
1288
1289         /* Read to clear the status */
1290         status = in_be32(&hw->int_status);
1291
1292         ret = request_irq(irq, fsl_diu_isr, 0, "diu", NULL);
1293         if (ret)
1294                 pr_info("Request diu IRQ failed.\n");
1295         else {
1296                 ints = INT_PARERR | INT_LS_BF_VS;
1297 #if !defined(CONFIG_NOT_COHERENT_CACHE)
1298                 ints |= INT_VSYNC;
1299 #endif
1300                 if (dr.mode == MFB_MODE2 || dr.mode == MFB_MODE3)
1301                         ints |= INT_VSYNC_WB;
1302
1303                 /* Read to clear the status */
1304                 status = in_be32(&hw->int_status);
1305                 out_be32(&hw->int_mask, ints);
1306         }
1307         return ret;
1308 }
1309
1310 static void free_irq_local(int irq)
1311 {
1312         struct diu *hw = dr.diu_reg;
1313
1314         /* Disable all LCDC interrupt */
1315         out_be32(&hw->int_mask, 0x1f);
1316
1317         free_irq(irq, NULL);
1318 }
1319
1320 #ifdef CONFIG_PM
1321 /*
1322  * Power management hooks. Note that we won't be called from IRQ context,
1323  * unlike the blank functions above, so we may sleep.
1324  */
1325 static int fsl_diu_suspend(struct of_device *ofdev, pm_message_t state)
1326 {
1327         struct fsl_diu_data *machine_data;
1328
1329         machine_data = dev_get_drvdata(&ofdev->dev);
1330         disable_lcdc(machine_data->fsl_diu_info[0]);
1331
1332         return 0;
1333 }
1334
1335 static int fsl_diu_resume(struct of_device *ofdev)
1336 {
1337         struct fsl_diu_data *machine_data;
1338
1339         machine_data = dev_get_drvdata(&ofdev->dev);
1340         enable_lcdc(machine_data->fsl_diu_info[0]);
1341
1342         return 0;
1343 }
1344
1345 #else
1346 #define fsl_diu_suspend NULL
1347 #define fsl_diu_resume NULL
1348 #endif                          /* CONFIG_PM */
1349
1350 /* Align to 64-bit(8-byte), 32-byte, etc. */
1351 static int allocate_buf(struct diu_addr *buf, u32 size, u32 bytes_align)
1352 {
1353         u32 offset, ssize;
1354         u32 mask;
1355         dma_addr_t paddr = 0;
1356
1357         ssize = size + bytes_align;
1358         buf->vaddr = dma_alloc_coherent(NULL, ssize, &paddr, GFP_DMA |
1359                                                              __GFP_ZERO);
1360         if (!buf->vaddr)
1361                 return -ENOMEM;
1362
1363         buf->paddr = (__u32) paddr;
1364
1365         mask = bytes_align - 1;
1366         offset = (u32)buf->paddr & mask;
1367         if (offset) {
1368                 buf->offset = bytes_align - offset;
1369                 buf->paddr = (u32)buf->paddr + offset;
1370         } else
1371                 buf->offset = 0;
1372         return 0;
1373 }
1374
1375 static void free_buf(struct diu_addr *buf, u32 size, u32 bytes_align)
1376 {
1377         dma_free_coherent(NULL, size + bytes_align,
1378                                 buf->vaddr, (buf->paddr - buf->offset));
1379         return;
1380 }
1381
1382 static ssize_t store_monitor(struct device *device,
1383         struct device_attribute *attr, const char *buf, size_t count)
1384 {
1385         int old_monitor_port;
1386         unsigned long val;
1387         struct fsl_diu_data *machine_data =
1388                 container_of(attr, struct fsl_diu_data, dev_attr);
1389
1390         if (strict_strtoul(buf, 10, &val))
1391                 return 0;
1392
1393         old_monitor_port = machine_data->monitor_port;
1394         machine_data->monitor_port = diu_ops.set_sysfs_monitor_port(val);
1395
1396         if (old_monitor_port != machine_data->monitor_port) {
1397                 /* All AOIs need adjust pixel format
1398                  * fsl_diu_set_par only change the pixsel format here
1399                  * unlikely to fail. */
1400                 fsl_diu_set_par(machine_data->fsl_diu_info[0]);
1401                 fsl_diu_set_par(machine_data->fsl_diu_info[1]);
1402                 fsl_diu_set_par(machine_data->fsl_diu_info[2]);
1403                 fsl_diu_set_par(machine_data->fsl_diu_info[3]);
1404                 fsl_diu_set_par(machine_data->fsl_diu_info[4]);
1405         }
1406         return count;
1407 }
1408
1409 static ssize_t show_monitor(struct device *device,
1410         struct device_attribute *attr, char *buf)
1411 {
1412         struct fsl_diu_data *machine_data =
1413                 container_of(attr, struct fsl_diu_data, dev_attr);
1414         return diu_ops.show_monitor_port(machine_data->monitor_port, buf);
1415 }
1416
1417 static int __devinit fsl_diu_probe(struct of_device *ofdev,
1418         const struct of_device_id *match)
1419 {
1420         struct device_node *np = ofdev->node;
1421         struct mfb_info *mfbi;
1422         phys_addr_t dummy_ad_addr;
1423         int ret, i, error = 0;
1424         struct resource res;
1425         struct fsl_diu_data *machine_data;
1426
1427         machine_data = kzalloc(sizeof(struct fsl_diu_data), GFP_KERNEL);
1428         if (!machine_data)
1429                 return -ENOMEM;
1430
1431         for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1432                 machine_data->fsl_diu_info[i] =
1433                         framebuffer_alloc(sizeof(struct mfb_info), &ofdev->dev);
1434                 if (!machine_data->fsl_diu_info[i]) {
1435                         dev_err(&ofdev->dev, "cannot allocate memory\n");
1436                         ret = -ENOMEM;
1437                         goto error2;
1438                 }
1439                 mfbi = machine_data->fsl_diu_info[i]->par;
1440                 memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
1441                 mfbi->parent = machine_data;
1442         }
1443
1444         ret = of_address_to_resource(np, 0, &res);
1445         if (ret) {
1446                 dev_err(&ofdev->dev, "could not obtain DIU address\n");
1447                 goto error;
1448         }
1449         if (!res.start) {
1450                 dev_err(&ofdev->dev, "invalid DIU address\n");
1451                 goto error;
1452         }
1453         dev_dbg(&ofdev->dev, "%s, res.start: 0x%08x\n", __func__, res.start);
1454
1455         dr.diu_reg = ioremap(res.start, sizeof(struct diu));
1456         if (!dr.diu_reg) {
1457                 dev_err(&ofdev->dev, "Err: can't map DIU registers!\n");
1458                 ret = -EFAULT;
1459                 goto error2;
1460         }
1461
1462         out_be32(&dr.diu_reg->diu_mode, 0);             /* disable DIU anyway*/
1463
1464         /* Get the IRQ of the DIU */
1465         machine_data->irq = irq_of_parse_and_map(np, 0);
1466
1467         if (!machine_data->irq) {
1468                 dev_err(&ofdev->dev, "could not get DIU IRQ\n");
1469                 ret = -EINVAL;
1470                 goto error;
1471         }
1472         machine_data->monitor_port = monitor_port;
1473
1474         /* Area descriptor memory pool aligns to 64-bit boundary */
1475         if (allocate_buf(&pool.ad, sizeof(struct diu_ad) * FSL_AOI_NUM, 8))
1476                 return -ENOMEM;
1477
1478         /* Get memory for Gamma Table  - 32-byte aligned memory */
1479         if (allocate_buf(&pool.gamma, 768, 32)) {
1480                 ret = -ENOMEM;
1481                 goto error;
1482         }
1483
1484         /* For performance, cursor bitmap buffer aligns to 32-byte boundary */
1485         if (allocate_buf(&pool.cursor, MAX_CURS * MAX_CURS * 2, 32)) {
1486                 ret = -ENOMEM;
1487                 goto error;
1488         }
1489
1490         i = ARRAY_SIZE(machine_data->fsl_diu_info);
1491         machine_data->dummy_ad = (struct diu_ad *)
1492                         ((u32)pool.ad.vaddr + pool.ad.offset) + i;
1493         machine_data->dummy_ad->paddr = pool.ad.paddr +
1494                         i * sizeof(struct diu_ad);
1495         machine_data->dummy_aoi_virt = fsl_diu_alloc(64, &dummy_ad_addr);
1496         if (!machine_data->dummy_aoi_virt) {
1497                 ret = -ENOMEM;
1498                 goto error;
1499         }
1500         machine_data->dummy_ad->addr = cpu_to_le32(dummy_ad_addr);
1501         machine_data->dummy_ad->pix_fmt = 0x88882317;
1502         machine_data->dummy_ad->src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
1503         machine_data->dummy_ad->aoi_size = cpu_to_le32((4 << 16) |  2);
1504         machine_data->dummy_ad->offset_xyi = 0;
1505         machine_data->dummy_ad->offset_xyd = 0;
1506         machine_data->dummy_ad->next_ad = 0;
1507
1508         out_be32(&dr.diu_reg->desc[0], machine_data->dummy_ad->paddr);
1509         out_be32(&dr.diu_reg->desc[1], machine_data->dummy_ad->paddr);
1510         out_be32(&dr.diu_reg->desc[2], machine_data->dummy_ad->paddr);
1511
1512         for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
1513                 machine_data->fsl_diu_info[i]->fix.smem_start = 0;
1514                 mfbi = machine_data->fsl_diu_info[i]->par;
1515                 mfbi->ad = (struct diu_ad *)((u32)pool.ad.vaddr
1516                                         + pool.ad.offset) + i;
1517                 mfbi->ad->paddr = pool.ad.paddr + i * sizeof(struct diu_ad);
1518                 ret = install_fb(machine_data->fsl_diu_info[i]);
1519                 if (ret) {
1520                         dev_err(&ofdev->dev,
1521                                 "Failed to register framebuffer %d\n",
1522                                 i);
1523                         goto error;
1524                 }
1525         }
1526
1527         if (request_irq_local(machine_data->irq)) {
1528                 dev_err(machine_data->fsl_diu_info[0]->dev,
1529                         "could not request irq for diu.");
1530                 goto error;
1531         }
1532
1533         machine_data->dev_attr.attr.name = "monitor";
1534         machine_data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
1535         machine_data->dev_attr.show = show_monitor;
1536         machine_data->dev_attr.store = store_monitor;
1537         error = device_create_file(machine_data->fsl_diu_info[0]->dev,
1538                                   &machine_data->dev_attr);
1539         if (error) {
1540                 dev_err(machine_data->fsl_diu_info[0]->dev,
1541                         "could not create sysfs %s file\n",
1542                         machine_data->dev_attr.attr.name);
1543         }
1544
1545         dev_set_drvdata(&ofdev->dev, machine_data);
1546         return 0;
1547
1548 error:
1549         for (i = ARRAY_SIZE(machine_data->fsl_diu_info);
1550                 i > 0; i--)
1551                 uninstall_fb(machine_data->fsl_diu_info[i - 1]);
1552         if (pool.ad.vaddr)
1553                 free_buf(&pool.ad, sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
1554         if (pool.gamma.vaddr)
1555                 free_buf(&pool.gamma, 768, 32);
1556         if (pool.cursor.vaddr)
1557                 free_buf(&pool.cursor, MAX_CURS * MAX_CURS * 2, 32);
1558         if (machine_data->dummy_aoi_virt)
1559                 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1560         iounmap(dr.diu_reg);
1561
1562 error2:
1563         for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1564                 if (machine_data->fsl_diu_info[i])
1565                         framebuffer_release(machine_data->fsl_diu_info[i]);
1566         kfree(machine_data);
1567
1568         return ret;
1569 }
1570
1571
1572 static int fsl_diu_remove(struct of_device *ofdev)
1573 {
1574         struct fsl_diu_data *machine_data;
1575         int i;
1576
1577         machine_data = dev_get_drvdata(&ofdev->dev);
1578         disable_lcdc(machine_data->fsl_diu_info[0]);
1579         free_irq_local(machine_data->irq);
1580         for (i = ARRAY_SIZE(machine_data->fsl_diu_info); i > 0; i--)
1581                 uninstall_fb(machine_data->fsl_diu_info[i - 1]);
1582         if (pool.ad.vaddr)
1583                 free_buf(&pool.ad, sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
1584         if (pool.gamma.vaddr)
1585                 free_buf(&pool.gamma, 768, 32);
1586         if (pool.cursor.vaddr)
1587                 free_buf(&pool.cursor, MAX_CURS * MAX_CURS * 2, 32);
1588         if (machine_data->dummy_aoi_virt)
1589                 fsl_diu_free(machine_data->dummy_aoi_virt, 64);
1590         iounmap(dr.diu_reg);
1591         for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
1592                 if (machine_data->fsl_diu_info[i])
1593                         framebuffer_release(machine_data->fsl_diu_info[i]);
1594         kfree(machine_data);
1595
1596         return 0;
1597 }
1598
1599 #ifndef MODULE
1600 static int __init fsl_diu_setup(char *options)
1601 {
1602         char *opt;
1603         unsigned long val;
1604
1605         if (!options || !*options)
1606                 return 0;
1607
1608         while ((opt = strsep(&options, ",")) != NULL) {
1609                 if (!*opt)
1610                         continue;
1611                 if (!strncmp(opt, "monitor=", 8)) {
1612                         if (!strict_strtoul(opt + 8, 10, &val) && (val <= 2))
1613                                 monitor_port = val;
1614                 } else if (!strncmp(opt, "bpp=", 4)) {
1615                         if (!strict_strtoul(opt + 4, 10, &val))
1616                                 default_bpp = val;
1617                 } else
1618                         fb_mode = opt;
1619         }
1620
1621         return 0;
1622 }
1623 #endif
1624
1625 static struct of_device_id fsl_diu_match[] = {
1626         {
1627                 .compatible = "fsl,diu",
1628         },
1629         {}
1630 };
1631 MODULE_DEVICE_TABLE(of, fsl_diu_match);
1632
1633 static struct of_platform_driver fsl_diu_driver = {
1634         .owner          = THIS_MODULE,
1635         .name           = "fsl_diu",
1636         .match_table    = fsl_diu_match,
1637         .probe          = fsl_diu_probe,
1638         .remove         = fsl_diu_remove,
1639         .suspend        = fsl_diu_suspend,
1640         .resume         = fsl_diu_resume,
1641 };
1642
1643 static int __init fsl_diu_init(void)
1644 {
1645 #ifdef CONFIG_NOT_COHERENT_CACHE
1646         struct device_node *np;
1647         const u32 *prop;
1648 #endif
1649         int ret;
1650 #ifndef MODULE
1651         char *option;
1652
1653         /*
1654          * For kernel boot options (in 'video=xxxfb:<options>' format)
1655          */
1656         if (fb_get_options("fslfb", &option))
1657                 return -ENODEV;
1658         fsl_diu_setup(option);
1659 #endif
1660         printk(KERN_INFO "Freescale DIU driver\n");
1661
1662 #ifdef CONFIG_NOT_COHERENT_CACHE
1663         np = of_find_node_by_type(NULL, "cpu");
1664         if (!np) {
1665                 printk(KERN_ERR "Err: can't find device node 'cpu'\n");
1666                 return -ENODEV;
1667         }
1668
1669         prop = of_get_property(np, "d-cache-size", NULL);
1670         if (prop == NULL) {
1671                 of_node_put(np);
1672                 return -ENODEV;
1673         }
1674
1675         /* Freescale PLRU requires 13/8 times the cache size to do a proper
1676            displacement flush
1677          */
1678         coherence_data_size = *prop * 13;
1679         coherence_data_size /= 8;
1680
1681         prop = of_get_property(np, "d-cache-line-size", NULL);
1682         if (prop == NULL) {
1683                 of_node_put(np);
1684                 return -ENODEV;
1685         }
1686         d_cache_line_size = *prop;
1687
1688         of_node_put(np);
1689         coherence_data = vmalloc(coherence_data_size);
1690         if (!coherence_data)
1691                 return -ENOMEM;
1692 #endif
1693         ret = of_register_platform_driver(&fsl_diu_driver);
1694         if (ret) {
1695                 printk(KERN_ERR
1696                         "fsl-diu: failed to register platform driver\n");
1697 #if defined(CONFIG_NOT_COHERENT_CACHE)
1698                 vfree(coherence_data);
1699 #endif
1700                 iounmap(dr.diu_reg);
1701         }
1702         return ret;
1703 }
1704
1705 static void __exit fsl_diu_exit(void)
1706 {
1707         of_unregister_platform_driver(&fsl_diu_driver);
1708 #if defined(CONFIG_NOT_COHERENT_CACHE)
1709         vfree(coherence_data);
1710 #endif
1711 }
1712
1713 module_init(fsl_diu_init);
1714 module_exit(fsl_diu_exit);
1715
1716 MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
1717 MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
1718 MODULE_LICENSE("GPL");
1719
1720 module_param_named(mode, fb_mode, charp, 0);
1721 MODULE_PARM_DESC(mode,
1722         "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
1723 module_param_named(bpp, default_bpp, ulong, 0);
1724 MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified mode");
1725 module_param_named(monitor, monitor_port, int, 0);
1726 MODULE_PARM_DESC(monitor,
1727         "Specify the monitor port (0, 1 or 2) if supported by the platform");
1728