4 * Copyright (C) 2006-2007 Andrzej Zaborowski <balrog@zabor.org>
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.
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "ui/console.h"
23 #include "hw/arm/omap.h"
24 #include "framebuffer.h"
25 #include "ui/pixel_ops.h"
27 struct omap_lcd_panel_s
{
30 MemoryRegionSection fbsection
;
45 struct omap_dma_lcd_channel_s
*dma
;
46 uint16_t palette
[256];
53 static void omap_lcd_interrupts(struct omap_lcd_panel_s
*s
)
55 if (s
->frame_done
&& (s
->interrupts
& 1)) {
56 qemu_irq_raise(s
->irq
);
60 if (s
->palette_done
&& (s
->interrupts
& 2)) {
61 qemu_irq_raise(s
->irq
);
66 qemu_irq_raise(s
->irq
);
70 qemu_irq_lower(s
->irq
);
73 #define draw_line_func drawfn
78 static void draw_line2_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
79 int width
, int deststep
)
81 uint16_t *pal
= opaque
;
85 v
= ldub_p((void *) s
);
86 r
= (pal
[v
& 3] >> 4) & 0xf0;
87 g
= pal
[v
& 3] & 0xf0;
88 b
= (pal
[v
& 3] << 4) & 0xf0;
89 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
92 r
= (pal
[v
& 3] >> 4) & 0xf0;
93 g
= pal
[v
& 3] & 0xf0;
94 b
= (pal
[v
& 3] << 4) & 0xf0;
95 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
98 r
= (pal
[v
& 3] >> 4) & 0xf0;
99 g
= pal
[v
& 3] & 0xf0;
100 b
= (pal
[v
& 3] << 4) & 0xf0;
101 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
104 r
= (pal
[v
& 3] >> 4) & 0xf0;
105 g
= pal
[v
& 3] & 0xf0;
106 b
= (pal
[v
& 3] << 4) & 0xf0;
107 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
117 static void draw_line4_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
118 int width
, int deststep
)
120 uint16_t *pal
= opaque
;
124 v
= ldub_p((void *) s
);
125 r
= (pal
[v
& 0xf] >> 4) & 0xf0;
126 g
= pal
[v
& 0xf] & 0xf0;
127 b
= (pal
[v
& 0xf] << 4) & 0xf0;
128 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
131 r
= (pal
[v
& 0xf] >> 4) & 0xf0;
132 g
= pal
[v
& 0xf] & 0xf0;
133 b
= (pal
[v
& 0xf] << 4) & 0xf0;
134 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
144 static void draw_line8_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
145 int width
, int deststep
)
147 uint16_t *pal
= opaque
;
151 v
= ldub_p((void *) s
);
152 r
= (pal
[v
] >> 4) & 0xf0;
154 b
= (pal
[v
] << 4) & 0xf0;
155 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
158 } while (-- width
!= 0);
164 static void draw_line12_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
165 int width
, int deststep
)
171 v
= lduw_le_p((void *) s
);
175 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
178 } while (-- width
!= 0);
184 static void draw_line16_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
185 int width
, int deststep
)
191 v
= lduw_le_p((void *) s
);
195 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
198 } while (-- width
!= 0);
201 static void omap_update_display(void *opaque
)
203 struct omap_lcd_panel_s
*omap_lcd
= (struct omap_lcd_panel_s
*) opaque
;
204 DisplaySurface
*surface
;
205 draw_line_func draw_line
;
206 int size
, height
, first
, last
;
207 int width
, linesize
, step
, bpp
, frame_offset
;
210 if (!omap_lcd
|| omap_lcd
->plm
== 1 || !omap_lcd
->enable
) {
214 surface
= qemu_console_surface(omap_lcd
->con
);
215 if (!surface_bits_per_pixel(surface
)) {
220 if (omap_lcd
->plm
!= 2) {
221 cpu_physical_memory_read(
222 omap_lcd
->dma
->phys_framebuffer
[omap_lcd
->dma
->current_frame
],
223 omap_lcd
->palette
, 0x200);
224 switch (omap_lcd
->palette
[0] >> 12 & 7) {
226 frame_offset
+= 0x200;
229 frame_offset
+= 0x20;
234 switch ((omap_lcd
->palette
[0] >> 12) & 7) {
236 draw_line
= draw_line2_32
;
241 draw_line
= draw_line4_32
;
246 draw_line
= draw_line8_32
;
252 draw_line
= draw_line12_32
;
254 draw_line
= draw_line16_32
;
259 /* Unsupported at the moment. */
264 width
= omap_lcd
->width
;
265 if (width
!= surface_width(surface
) ||
266 omap_lcd
->height
!= surface_height(surface
)) {
267 qemu_console_resize(omap_lcd
->con
,
268 omap_lcd
->width
, omap_lcd
->height
);
269 surface
= qemu_console_surface(omap_lcd
->con
);
270 omap_lcd
->invalidate
= 1;
273 if (omap_lcd
->dma
->current_frame
== 0)
274 size
= omap_lcd
->dma
->src_f1_bottom
- omap_lcd
->dma
->src_f1_top
;
276 size
= omap_lcd
->dma
->src_f2_bottom
- omap_lcd
->dma
->src_f2_top
;
278 if (frame_offset
+ ((width
* omap_lcd
->height
* bpp
) >> 3) > size
+ 2) {
279 omap_lcd
->sync_error
= 1;
280 omap_lcd_interrupts(omap_lcd
);
281 omap_lcd
->enable
= 0;
286 frame_base
= omap_lcd
->dma
->phys_framebuffer
[
287 omap_lcd
->dma
->current_frame
] + frame_offset
;
288 omap_lcd
->dma
->condition
|= 1 << omap_lcd
->dma
->current_frame
;
289 if (omap_lcd
->dma
->interrupts
& 1)
290 qemu_irq_raise(omap_lcd
->dma
->irq
);
291 if (omap_lcd
->dma
->dual
)
292 omap_lcd
->dma
->current_frame
^= 1;
294 if (!surface_bits_per_pixel(surface
)) {
299 height
= omap_lcd
->height
;
300 if (omap_lcd
->subpanel
& (1 << 31)) {
301 if (omap_lcd
->subpanel
& (1 << 29))
302 first
= (omap_lcd
->subpanel
>> 16) & 0x3ff;
304 height
= (omap_lcd
->subpanel
>> 16) & 0x3ff;
305 /* TODO: fill the rest of the panel with DPD */
308 step
= width
* bpp
>> 3;
309 linesize
= surface_stride(surface
);
310 if (omap_lcd
->invalidate
) {
311 framebuffer_update_memory_section(&omap_lcd
->fbsection
,
312 omap_lcd
->sysmem
, frame_base
,
316 framebuffer_update_display(surface
, &omap_lcd
->fbsection
,
319 omap_lcd
->invalidate
,
320 draw_line
, omap_lcd
->palette
,
324 dpy_gfx_update(omap_lcd
->con
, 0, first
, width
, last
- first
+ 1);
326 omap_lcd
->invalidate
= 0;
329 static void omap_invalidate_display(void *opaque
) {
330 struct omap_lcd_panel_s
*omap_lcd
= opaque
;
331 omap_lcd
->invalidate
= 1;
334 static void omap_lcd_update(struct omap_lcd_panel_s
*s
) {
336 s
->dma
->current_frame
= -1;
340 omap_lcd_interrupts(s
);
344 if (s
->dma
->current_frame
== -1) {
347 s
->dma
->current_frame
= 0;
350 if (!s
->dma
->mpu
->port
[s
->dma
->src
].addr_valid(s
->dma
->mpu
,
351 s
->dma
->src_f1_top
) ||
353 s
->dma
->src
].addr_valid(s
->dma
->mpu
,
354 s
->dma
->src_f1_bottom
) ||
357 s
->dma
->src
].addr_valid(s
->dma
->mpu
,
358 s
->dma
->src_f2_top
) ||
360 s
->dma
->src
].addr_valid(s
->dma
->mpu
,
361 s
->dma
->src_f2_bottom
)))) {
362 s
->dma
->condition
|= 1 << 2;
363 if (s
->dma
->interrupts
& (1 << 1))
364 qemu_irq_raise(s
->dma
->irq
);
369 s
->dma
->phys_framebuffer
[0] = s
->dma
->src_f1_top
;
370 s
->dma
->phys_framebuffer
[1] = s
->dma
->src_f2_top
;
372 if (s
->plm
!= 2 && !s
->palette_done
) {
373 cpu_physical_memory_read(
374 s
->dma
->phys_framebuffer
[s
->dma
->current_frame
],
377 omap_lcd_interrupts(s
);
381 static uint64_t omap_lcdc_read(void *opaque
, hwaddr addr
,
384 struct omap_lcd_panel_s
*s
= (struct omap_lcd_panel_s
*) opaque
;
387 case 0x00: /* LCD_CONTROL */
388 return (s
->tft
<< 23) | (s
->plm
<< 20) |
389 (s
->tft
<< 7) | (s
->interrupts
<< 3) |
390 (s
->mono
<< 1) | s
->enable
| s
->ctrl
| 0xfe000c34;
392 case 0x04: /* LCD_TIMING0 */
393 return (s
->timing
[0] << 10) | (s
->width
- 1) | 0x0000000f;
395 case 0x08: /* LCD_TIMING1 */
396 return (s
->timing
[1] << 10) | (s
->height
- 1);
398 case 0x0c: /* LCD_TIMING2 */
399 return s
->timing
[2] | 0xfc000000;
401 case 0x10: /* LCD_STATUS */
402 return (s
->palette_done
<< 6) | (s
->sync_error
<< 2) | s
->frame_done
;
404 case 0x14: /* LCD_SUBPANEL */
414 static void omap_lcdc_write(void *opaque
, hwaddr addr
,
415 uint64_t value
, unsigned size
)
417 struct omap_lcd_panel_s
*s
= (struct omap_lcd_panel_s
*) opaque
;
420 case 0x00: /* LCD_CONTROL */
421 s
->plm
= (value
>> 20) & 3;
422 s
->tft
= (value
>> 7) & 1;
423 s
->interrupts
= (value
>> 3) & 3;
424 s
->mono
= (value
>> 1) & 1;
425 s
->ctrl
= value
& 0x01cff300;
426 if (s
->enable
!= (value
& 1)) {
427 s
->enable
= value
& 1;
432 case 0x04: /* LCD_TIMING0 */
433 s
->timing
[0] = value
>> 10;
434 s
->width
= (value
& 0x3ff) + 1;
437 case 0x08: /* LCD_TIMING1 */
438 s
->timing
[1] = value
>> 10;
439 s
->height
= (value
& 0x3ff) + 1;
442 case 0x0c: /* LCD_TIMING2 */
443 s
->timing
[2] = value
;
446 case 0x10: /* LCD_STATUS */
449 case 0x14: /* LCD_SUBPANEL */
450 s
->subpanel
= value
& 0xa1ffffff;
458 static const MemoryRegionOps omap_lcdc_ops
= {
459 .read
= omap_lcdc_read
,
460 .write
= omap_lcdc_write
,
461 .endianness
= DEVICE_NATIVE_ENDIAN
,
464 void omap_lcdc_reset(struct omap_lcd_panel_s
*s
)
466 s
->dma
->current_frame
= -1;
486 static const GraphicHwOps omap_ops
= {
487 .invalidate
= omap_invalidate_display
,
488 .gfx_update
= omap_update_display
,
491 struct omap_lcd_panel_s
*omap_lcdc_init(MemoryRegion
*sysmem
,
494 struct omap_dma_lcd_channel_s
*dma
,
497 struct omap_lcd_panel_s
*s
= g_new0(struct omap_lcd_panel_s
, 1);
504 memory_region_init_io(&s
->iomem
, NULL
, &omap_lcdc_ops
, s
, "omap.lcdc", 0x100);
505 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
507 s
->con
= graphic_console_init(NULL
, 0, &omap_ops
, s
);