symbian-qemu-0.9.1-12/qemu-symbian-svp/hw/omap_lcdc.c
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 /*
       
     2  * OMAP LCD controller.
       
     3  *
       
     4  * Copyright (C) 2006-2007 Andrzej Zaborowski  <balrog@zabor.org>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU General Public License as
       
     8  * published by the Free Software Foundation; either version 2 of
       
     9  * the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  * GNU General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License
       
    17  * along with this program; if not, write to the Free Software
       
    18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
       
    19  * MA 02111-1307 USA
       
    20  */
       
    21 #include "hw.h"
       
    22 #include "gui.h"
       
    23 #include "omap.h"
       
    24 
       
    25 struct omap_lcd_panel_s {
       
    26     qemu_irq irq;
       
    27     DisplayState *state;
       
    28     ram_addr_t imif_base;
       
    29     ram_addr_t emiff_base;
       
    30 
       
    31     int plm;
       
    32     int tft;
       
    33     int mono;
       
    34     int enable;
       
    35     int width;
       
    36     int height;
       
    37     int interrupts;
       
    38     uint32_t timing[3];
       
    39     uint32_t subpanel;
       
    40     uint32_t ctrl;
       
    41 
       
    42     struct omap_dma_lcd_channel_s *dma;
       
    43     uint16_t palette[256];
       
    44     int palette_done;
       
    45     int frame_done;
       
    46     int invalidate;
       
    47     int sync_error;
       
    48 };
       
    49 
       
    50 static void omap_lcd_interrupts(struct omap_lcd_panel_s *s)
       
    51 {
       
    52     if (s->frame_done && (s->interrupts & 1)) {
       
    53         qemu_irq_raise(s->irq);
       
    54         return;
       
    55     }
       
    56 
       
    57     if (s->palette_done && (s->interrupts & 2)) {
       
    58         qemu_irq_raise(s->irq);
       
    59         return;
       
    60     }
       
    61 
       
    62     if (s->sync_error) {
       
    63         qemu_irq_raise(s->irq);
       
    64         return;
       
    65     }
       
    66 
       
    67     qemu_irq_lower(s->irq);
       
    68 }
       
    69 
       
    70 #include "pixel_ops.h"
       
    71 
       
    72 typedef void draw_line_func(
       
    73                 uint8_t *d, const uint8_t *s, int width, const uint16_t *pal);
       
    74 
       
    75 #define DEPTH 8
       
    76 #include "omap_lcd_template.h"
       
    77 #define DEPTH 15
       
    78 #include "omap_lcd_template.h"
       
    79 #define DEPTH 16
       
    80 #include "omap_lcd_template.h"
       
    81 #define DEPTH 32
       
    82 #include "omap_lcd_template.h"
       
    83 
       
    84 static draw_line_func *draw_line_table2[33] = {
       
    85     [0 ... 32]	= 0,
       
    86     [8]		= draw_line2_8,
       
    87     [15]	= draw_line2_15,
       
    88     [16]	= draw_line2_16,
       
    89     [32]	= draw_line2_32,
       
    90 }, *draw_line_table4[33] = {
       
    91     [0 ... 32]	= 0,
       
    92     [8]		= draw_line4_8,
       
    93     [15]	= draw_line4_15,
       
    94     [16]	= draw_line4_16,
       
    95     [32]	= draw_line4_32,
       
    96 }, *draw_line_table8[33] = {
       
    97     [0 ... 32]	= 0,
       
    98     [8]		= draw_line8_8,
       
    99     [15]	= draw_line8_15,
       
   100     [16]	= draw_line8_16,
       
   101     [32]	= draw_line8_32,
       
   102 }, *draw_line_table12[33] = {
       
   103     [0 ... 32]	= 0,
       
   104     [8]		= draw_line12_8,
       
   105     [15]	= draw_line12_15,
       
   106     [16]	= draw_line12_16,
       
   107     [32]	= draw_line12_32,
       
   108 }, *draw_line_table16[33] = {
       
   109     [0 ... 32]	= 0,
       
   110     [8]		= draw_line16_8,
       
   111     [15]	= draw_line16_15,
       
   112     [16]	= draw_line16_16,
       
   113     [32]	= draw_line16_32,
       
   114 };
       
   115 
       
   116 static void omap_update_display(void *opaque)
       
   117 {
       
   118     struct omap_lcd_panel_s *omap_lcd = (struct omap_lcd_panel_s *) opaque;
       
   119     draw_line_func *draw_line;
       
   120     int size, dirty[2], minline, maxline, height;
       
   121     int line, width, linesize, step, bpp, frame_offset;
       
   122     ram_addr_t frame_base, scanline, newline, x;
       
   123     uint8_t *s, *d;
       
   124 
       
   125     if (!omap_lcd || omap_lcd->plm == 1 ||
       
   126                     !omap_lcd->enable || !ds_get_bits_per_pixel(omap_lcd->state))
       
   127         return;
       
   128 
       
   129     frame_offset = 0;
       
   130     if (omap_lcd->plm != 2) {
       
   131         /* FIXME: This is broken if it spans multiple RAM regions.  */
       
   132         memcpy(omap_lcd->palette,
       
   133                host_ram_addr(omap_lcd->dma->phys_framebuffer[
       
   134                              omap_lcd->dma->current_frame]), 0x200);
       
   135         switch (omap_lcd->palette[0] >> 12 & 7) {
       
   136         case 3 ... 7:
       
   137             frame_offset += 0x200;
       
   138             break;
       
   139         default:
       
   140             frame_offset += 0x20;
       
   141         }
       
   142     }
       
   143 
       
   144     /* Colour depth */
       
   145     switch ((omap_lcd->palette[0] >> 12) & 7) {
       
   146     case 1:
       
   147         draw_line = draw_line_table2[ds_get_bits_per_pixel(omap_lcd->state)];
       
   148         bpp = 2;
       
   149         break;
       
   150 
       
   151     case 2:
       
   152         draw_line = draw_line_table4[ds_get_bits_per_pixel(omap_lcd->state)];
       
   153         bpp = 4;
       
   154         break;
       
   155 
       
   156     case 3:
       
   157         draw_line = draw_line_table8[ds_get_bits_per_pixel(omap_lcd->state)];
       
   158         bpp = 8;
       
   159         break;
       
   160 
       
   161     case 4 ... 7:
       
   162         if (!omap_lcd->tft)
       
   163             draw_line = draw_line_table12[ds_get_bits_per_pixel(omap_lcd->state)];
       
   164         else
       
   165             draw_line = draw_line_table16[ds_get_bits_per_pixel(omap_lcd->state)];
       
   166         bpp = 16;
       
   167         break;
       
   168 
       
   169     default:
       
   170         /* Unsupported at the moment.  */
       
   171         return;
       
   172     }
       
   173 
       
   174     /* Resolution */
       
   175     width = omap_lcd->width;
       
   176     if (width != ds_get_width(omap_lcd->state) ||
       
   177             omap_lcd->height != ds_get_height(omap_lcd->state)) {
       
   178         gui_resize_vt(omap_lcd->state,
       
   179                             omap_lcd->width, omap_lcd->height);
       
   180         omap_lcd->invalidate = 1;
       
   181     }
       
   182 
       
   183     if (omap_lcd->dma->current_frame == 0)
       
   184         size = omap_lcd->dma->src_f1_bottom - omap_lcd->dma->src_f1_top;
       
   185     else
       
   186         size = omap_lcd->dma->src_f2_bottom - omap_lcd->dma->src_f2_top;
       
   187 
       
   188     if (frame_offset + ((width * omap_lcd->height * bpp) >> 3) > size + 2) {
       
   189         omap_lcd->sync_error = 1;
       
   190         omap_lcd_interrupts(omap_lcd);
       
   191         omap_lcd->enable = 0;
       
   192         return;
       
   193     }
       
   194 
       
   195     /* Content */
       
   196     frame_base = omap_lcd->dma->phys_framebuffer[
       
   197             omap_lcd->dma->current_frame] + frame_offset;
       
   198     omap_lcd->dma->condition |= 1 << omap_lcd->dma->current_frame;
       
   199     if (omap_lcd->dma->interrupts & 1)
       
   200         qemu_irq_raise(omap_lcd->dma->irq);
       
   201     if (omap_lcd->dma->dual)
       
   202         omap_lcd->dma->current_frame ^= 1;
       
   203 
       
   204     if (!ds_get_bits_per_pixel(omap_lcd->state))
       
   205         return;
       
   206 
       
   207     line = 0;
       
   208     height = omap_lcd->height;
       
   209     if (omap_lcd->subpanel & (1 << 31)) {
       
   210         if (omap_lcd->subpanel & (1 << 29))
       
   211             line = (omap_lcd->subpanel >> 16) & 0x3ff;
       
   212         else
       
   213             height = (omap_lcd->subpanel >> 16) & 0x3ff;
       
   214         /* TODO: fill the rest of the panel with DPD */
       
   215     }
       
   216     step = width * bpp >> 3;
       
   217     scanline = frame_base + step * line;
       
   218     /* FIXME: This is broken if it spans multiple RAM regions.  */
       
   219     s = host_ram_addr(scanline);
       
   220     d = ds_get_data(omap_lcd->state);
       
   221     linesize = ds_get_linesize(omap_lcd->state);
       
   222 
       
   223     dirty[0] = dirty[1] =
       
   224             cpu_physical_memory_get_dirty(scanline, VGA_DIRTY_FLAG);
       
   225     minline = height;
       
   226     maxline = line;
       
   227     for (; line < height; line ++) {
       
   228         newline = scanline + step;
       
   229         for (x = scanline + TARGET_PAGE_SIZE; x < newline;
       
   230                         x += TARGET_PAGE_SIZE) {
       
   231             dirty[1] = cpu_physical_memory_get_dirty(x, VGA_DIRTY_FLAG);
       
   232             dirty[0] |= dirty[1];
       
   233         }
       
   234         if (dirty[0] || omap_lcd->invalidate) {
       
   235             draw_line(d, s, width, omap_lcd->palette);
       
   236             if (line < minline)
       
   237                 minline = line;
       
   238             maxline = line + 1;
       
   239         }
       
   240         scanline = newline;
       
   241         dirty[0] = dirty[1];
       
   242         s += step;
       
   243         d += linesize;
       
   244     }
       
   245 
       
   246     if (maxline >= minline) {
       
   247         dpy_update(omap_lcd->state, 0, minline, width, maxline);
       
   248         cpu_physical_memory_reset_dirty(frame_base + step * minline,
       
   249                         frame_base + step * maxline, VGA_DIRTY_FLAG);
       
   250     }
       
   251 }
       
   252 
       
   253 static int ppm_save(const char *filename, uint8_t *data,
       
   254                 int w, int h, int linesize)
       
   255 {
       
   256     FILE *f;
       
   257     uint8_t *d, *d1;
       
   258     unsigned int v;
       
   259     int y, x, bpp;
       
   260 
       
   261     f = fopen(filename, "wb");
       
   262     if (!f)
       
   263         return -1;
       
   264     fprintf(f, "P6\n%d %d\n%d\n", w, h, 255);
       
   265     d1 = data;
       
   266     bpp = linesize / w;
       
   267     for (y = 0; y < h; y ++) {
       
   268         d = d1;
       
   269         for (x = 0; x < w; x ++) {
       
   270             v = *(uint32_t *) d;
       
   271             switch (bpp) {
       
   272             case 2:
       
   273                 fputc((v >> 8) & 0xf8, f);
       
   274                 fputc((v >> 3) & 0xfc, f);
       
   275                 fputc((v << 3) & 0xf8, f);
       
   276                 break;
       
   277             case 3:
       
   278             case 4:
       
   279             default:
       
   280                 fputc((v >> 16) & 0xff, f);
       
   281                 fputc((v >> 8) & 0xff, f);
       
   282                 fputc((v) & 0xff, f);
       
   283                 break;
       
   284             }
       
   285             d += bpp;
       
   286         }
       
   287         d1 += linesize;
       
   288     }
       
   289     fclose(f);
       
   290     return 0;
       
   291 }
       
   292 
       
   293 static void omap_screen_dump(void *opaque, const char *filename) {
       
   294     struct omap_lcd_panel_s *omap_lcd = opaque;
       
   295     omap_update_display(opaque);
       
   296     if (omap_lcd && ds_get_data(omap_lcd->state))
       
   297         ppm_save(filename, ds_get_data(omap_lcd->state),
       
   298                 omap_lcd->width, omap_lcd->height,
       
   299                 ds_get_linesize(omap_lcd->state));
       
   300 }
       
   301 
       
   302 static void omap_invalidate_display(void *opaque) {
       
   303     struct omap_lcd_panel_s *omap_lcd = opaque;
       
   304     omap_lcd->invalidate = 1;
       
   305 }
       
   306 
       
   307 static void omap_lcd_update(struct omap_lcd_panel_s *s) {
       
   308     if (!s->enable) {
       
   309         s->dma->current_frame = -1;
       
   310         s->sync_error = 0;
       
   311         if (s->plm != 1)
       
   312             s->frame_done = 1;
       
   313         omap_lcd_interrupts(s);
       
   314         return;
       
   315     }
       
   316 
       
   317     if (s->dma->current_frame == -1) {
       
   318         s->frame_done = 0;
       
   319         s->palette_done = 0;
       
   320         s->dma->current_frame = 0;
       
   321     }
       
   322 
       
   323     if (!s->dma->mpu->port[s->dma->src].addr_valid(s->dma->mpu,
       
   324                             s->dma->src_f1_top) ||
       
   325                     !s->dma->mpu->port[
       
   326                     s->dma->src].addr_valid(s->dma->mpu,
       
   327                             s->dma->src_f1_bottom) ||
       
   328                     (s->dma->dual &&
       
   329                      (!s->dma->mpu->port[
       
   330                       s->dma->src].addr_valid(s->dma->mpu,
       
   331                               s->dma->src_f2_top) ||
       
   332                       !s->dma->mpu->port[
       
   333                       s->dma->src].addr_valid(s->dma->mpu,
       
   334                               s->dma->src_f2_bottom)))) {
       
   335         s->dma->condition |= 1 << 2;
       
   336         if (s->dma->interrupts & (1 << 1))
       
   337             qemu_irq_raise(s->dma->irq);
       
   338         s->enable = 0;
       
   339         return;
       
   340     }
       
   341 
       
   342      if (s->dma->src == imif) {
       
   343         /* Framebuffers are in SRAM */
       
   344         s->dma->phys_framebuffer[0] = s->imif_base +
       
   345                 s->dma->src_f1_top - OMAP_IMIF_BASE;
       
   346 
       
   347         s->dma->phys_framebuffer[1] = s->imif_base +
       
   348                 s->dma->src_f2_top - OMAP_IMIF_BASE;
       
   349     } else {
       
   350         /* Framebuffers are in RAM */
       
   351         s->dma->phys_framebuffer[0] = s->emiff_base +
       
   352                 s->dma->src_f1_top - OMAP_EMIFF_BASE;
       
   353 
       
   354         s->dma->phys_framebuffer[1] = s->emiff_base +
       
   355                 s->dma->src_f2_top - OMAP_EMIFF_BASE;
       
   356     }
       
   357 
       
   358     if (s->plm != 2 && !s->palette_done) {
       
   359         /* FIXME: This is broken if it spans multiple RAM regions.  */
       
   360         memcpy(s->palette, host_ram_addr(
       
   361                 s->dma->phys_framebuffer[s->dma->current_frame]), 0x200);
       
   362         s->palette_done = 1;
       
   363         omap_lcd_interrupts(s);
       
   364     }
       
   365 }
       
   366 
       
   367 static uint32_t omap_lcdc_read(void *opaque, target_phys_addr_t addr)
       
   368 {
       
   369     struct omap_lcd_panel_s *s = (struct omap_lcd_panel_s *) opaque;
       
   370 
       
   371     switch (addr) {
       
   372     case 0x00:	/* LCD_CONTROL */
       
   373         return (s->tft << 23) | (s->plm << 20) |
       
   374                 (s->tft << 7) | (s->interrupts << 3) |
       
   375                 (s->mono << 1) | s->enable | s->ctrl | 0xfe000c34;
       
   376 
       
   377     case 0x04:	/* LCD_TIMING0 */
       
   378         return (s->timing[0] << 10) | (s->width - 1) | 0x0000000f;
       
   379 
       
   380     case 0x08:	/* LCD_TIMING1 */
       
   381         return (s->timing[1] << 10) | (s->height - 1);
       
   382 
       
   383     case 0x0c:	/* LCD_TIMING2 */
       
   384         return s->timing[2] | 0xfc000000;
       
   385 
       
   386     case 0x10:	/* LCD_STATUS */
       
   387         return (s->palette_done << 6) | (s->sync_error << 2) | s->frame_done;
       
   388 
       
   389     case 0x14:	/* LCD_SUBPANEL */
       
   390         return s->subpanel;
       
   391 
       
   392     default:
       
   393         break;
       
   394     }
       
   395     OMAP_BAD_REG(addr);
       
   396     return 0;
       
   397 }
       
   398 
       
   399 static void omap_lcdc_write(void *opaque, target_phys_addr_t addr,
       
   400                 uint32_t value)
       
   401 {
       
   402     struct omap_lcd_panel_s *s = (struct omap_lcd_panel_s *) opaque;
       
   403 
       
   404     switch (addr) {
       
   405     case 0x00:	/* LCD_CONTROL */
       
   406         s->plm = (value >> 20) & 3;
       
   407         s->tft = (value >> 7) & 1;
       
   408         s->interrupts = (value >> 3) & 3;
       
   409         s->mono = (value >> 1) & 1;
       
   410         s->ctrl = value & 0x01cff300;
       
   411         if (s->enable != (value & 1)) {
       
   412             s->enable = value & 1;
       
   413             omap_lcd_update(s);
       
   414         }
       
   415         break;
       
   416 
       
   417     case 0x04:	/* LCD_TIMING0 */
       
   418         s->timing[0] = value >> 10;
       
   419         s->width = (value & 0x3ff) + 1;
       
   420         break;
       
   421 
       
   422     case 0x08:	/* LCD_TIMING1 */
       
   423         s->timing[1] = value >> 10;
       
   424         s->height = (value & 0x3ff) + 1;
       
   425         break;
       
   426 
       
   427     case 0x0c:	/* LCD_TIMING2 */
       
   428         s->timing[2] = value;
       
   429         break;
       
   430 
       
   431     case 0x10:	/* LCD_STATUS */
       
   432         break;
       
   433 
       
   434     case 0x14:	/* LCD_SUBPANEL */
       
   435         s->subpanel = value & 0xa1ffffff;
       
   436         break;
       
   437 
       
   438     default:
       
   439         OMAP_BAD_REG(addr);
       
   440     }
       
   441 }
       
   442 
       
   443 static CPUReadMemoryFunc *omap_lcdc_readfn[] = {
       
   444     omap_lcdc_read,
       
   445     omap_lcdc_read,
       
   446     omap_lcdc_read,
       
   447 };
       
   448 
       
   449 static CPUWriteMemoryFunc *omap_lcdc_writefn[] = {
       
   450     omap_lcdc_write,
       
   451     omap_lcdc_write,
       
   452     omap_lcdc_write,
       
   453 };
       
   454 
       
   455 void omap_lcdc_reset(struct omap_lcd_panel_s *s)
       
   456 {
       
   457     s->dma->current_frame = -1;
       
   458     s->plm = 0;
       
   459     s->tft = 0;
       
   460     s->mono = 0;
       
   461     s->enable = 0;
       
   462     s->width = 0;
       
   463     s->height = 0;
       
   464     s->interrupts = 0;
       
   465     s->timing[0] = 0;
       
   466     s->timing[1] = 0;
       
   467     s->timing[2] = 0;
       
   468     s->subpanel = 0;
       
   469     s->palette_done = 0;
       
   470     s->frame_done = 0;
       
   471     s->sync_error = 0;
       
   472     s->invalidate = 1;
       
   473     s->subpanel = 0;
       
   474     s->ctrl = 0;
       
   475 }
       
   476 
       
   477 struct omap_lcd_panel_s *omap_lcdc_init(target_phys_addr_t base, qemu_irq irq,
       
   478                 struct omap_dma_lcd_channel_s *dma, DisplayState *ds,
       
   479                 ram_addr_t imif_base, ram_addr_t emiff_base, omap_clk clk)
       
   480 {
       
   481     int iomemtype;
       
   482     struct omap_lcd_panel_s *s = (struct omap_lcd_panel_s *)
       
   483             qemu_mallocz(sizeof(struct omap_lcd_panel_s));
       
   484 
       
   485     s->irq = irq;
       
   486     s->dma = dma;
       
   487     s->imif_base = imif_base;
       
   488     s->emiff_base = emiff_base;
       
   489     omap_lcdc_reset(s);
       
   490 
       
   491     iomemtype = cpu_register_io_memory(0, omap_lcdc_readfn,
       
   492                     omap_lcdc_writefn, s);
       
   493     cpu_register_physical_memory(base, 0x100, iomemtype);
       
   494 
       
   495     s->state = gui_get_graphic_console(NULL,
       
   496                                        omap_update_display,
       
   497                                        omap_invalidate_display,
       
   498                                        omap_screen_dump, s);
       
   499 
       
   500     return s;
       
   501 }