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
);
76 static void draw_line2_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
77 int width
, int deststep
)
79 uint16_t *pal
= opaque
;
83 v
= ldub_p((void *) s
);
84 r
= (pal
[v
& 3] >> 4) & 0xf0;
85 g
= pal
[v
& 3] & 0xf0;
86 b
= (pal
[v
& 3] << 4) & 0xf0;
87 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
90 r
= (pal
[v
& 3] >> 4) & 0xf0;
91 g
= pal
[v
& 3] & 0xf0;
92 b
= (pal
[v
& 3] << 4) & 0xf0;
93 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
96 r
= (pal
[v
& 3] >> 4) & 0xf0;
97 g
= pal
[v
& 3] & 0xf0;
98 b
= (pal
[v
& 3] << 4) & 0xf0;
99 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
102 r
= (pal
[v
& 3] >> 4) & 0xf0;
103 g
= pal
[v
& 3] & 0xf0;
104 b
= (pal
[v
& 3] << 4) & 0xf0;
105 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
115 static void draw_line4_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
116 int width
, int deststep
)
118 uint16_t *pal
= opaque
;
122 v
= ldub_p((void *) s
);
123 r
= (pal
[v
& 0xf] >> 4) & 0xf0;
124 g
= pal
[v
& 0xf] & 0xf0;
125 b
= (pal
[v
& 0xf] << 4) & 0xf0;
126 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
129 r
= (pal
[v
& 0xf] >> 4) & 0xf0;
130 g
= pal
[v
& 0xf] & 0xf0;
131 b
= (pal
[v
& 0xf] << 4) & 0xf0;
132 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
142 static void draw_line8_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
143 int width
, int deststep
)
145 uint16_t *pal
= opaque
;
149 v
= ldub_p((void *) s
);
150 r
= (pal
[v
] >> 4) & 0xf0;
152 b
= (pal
[v
] << 4) & 0xf0;
153 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
156 } while (-- width
!= 0);
162 static void draw_line12_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
163 int width
, int deststep
)
169 v
= lduw_le_p((void *) s
);
173 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
176 } while (-- width
!= 0);
182 static void draw_line16_32(void *opaque
, uint8_t *d
, const uint8_t *s
,
183 int width
, int deststep
)
189 v
= lduw_le_p((void *) s
);
193 ((uint32_t *) d
)[0] = rgb_to_pixel32(r
, g
, b
);
196 } while (-- width
!= 0);
199 static void omap_update_display(void *opaque
)
201 struct omap_lcd_panel_s
*omap_lcd
= (struct omap_lcd_panel_s
*) opaque
;
202 DisplaySurface
*surface
;
204 int size
, height
, first
, last
;
205 int width
, linesize
, step
, bpp
, frame_offset
;
208 if (!omap_lcd
|| omap_lcd
->plm
== 1 || !omap_lcd
->enable
) {
212 surface
= qemu_console_surface(omap_lcd
->con
);
213 if (!surface_bits_per_pixel(surface
)) {
218 if (omap_lcd
->plm
!= 2) {
219 cpu_physical_memory_read(
220 omap_lcd
->dma
->phys_framebuffer
[omap_lcd
->dma
->current_frame
],
221 omap_lcd
->palette
, 0x200);
222 switch (omap_lcd
->palette
[0] >> 12 & 7) {
224 frame_offset
+= 0x200;
227 frame_offset
+= 0x20;
232 switch ((omap_lcd
->palette
[0] >> 12) & 7) {
234 draw_line
= draw_line2_32
;
239 draw_line
= draw_line4_32
;
244 draw_line
= draw_line8_32
;
250 draw_line
= draw_line12_32
;
252 draw_line
= draw_line16_32
;
257 /* Unsupported at the moment. */
262 width
= omap_lcd
->width
;
263 if (width
!= surface_width(surface
) ||
264 omap_lcd
->height
!= surface_height(surface
)) {
265 qemu_console_resize(omap_lcd
->con
,
266 omap_lcd
->width
, omap_lcd
->height
);
267 surface
= qemu_console_surface(omap_lcd
->con
);
268 omap_lcd
->invalidate
= 1;
271 if (omap_lcd
->dma
->current_frame
== 0)
272 size
= omap_lcd
->dma
->src_f1_bottom
- omap_lcd
->dma
->src_f1_top
;
274 size
= omap_lcd
->dma
->src_f2_bottom
- omap_lcd
->dma
->src_f2_top
;
276 if (frame_offset
+ ((width
* omap_lcd
->height
* bpp
) >> 3) > size
+ 2) {
277 omap_lcd
->sync_error
= 1;
278 omap_lcd_interrupts(omap_lcd
);
279 omap_lcd
->enable
= 0;
284 frame_base
= omap_lcd
->dma
->phys_framebuffer
[
285 omap_lcd
->dma
->current_frame
] + frame_offset
;
286 omap_lcd
->dma
->condition
|= 1 << omap_lcd
->dma
->current_frame
;
287 if (omap_lcd
->dma
->interrupts
& 1)
288 qemu_irq_raise(omap_lcd
->dma
->irq
);
289 if (omap_lcd
->dma
->dual
)
290 omap_lcd
->dma
->current_frame
^= 1;
292 if (!surface_bits_per_pixel(surface
)) {
297 height
= omap_lcd
->height
;
298 if (omap_lcd
->subpanel
& (1 << 31)) {
299 if (omap_lcd
->subpanel
& (1 << 29))
300 first
= (omap_lcd
->subpanel
>> 16) & 0x3ff;
302 height
= (omap_lcd
->subpanel
>> 16) & 0x3ff;
303 /* TODO: fill the rest of the panel with DPD */
306 step
= width
* bpp
>> 3;
307 linesize
= surface_stride(surface
);
308 if (omap_lcd
->invalidate
) {
309 framebuffer_update_memory_section(&omap_lcd
->fbsection
,
310 omap_lcd
->sysmem
, frame_base
,
314 framebuffer_update_display(surface
, &omap_lcd
->fbsection
,
317 omap_lcd
->invalidate
,
318 draw_line
, omap_lcd
->palette
,
322 dpy_gfx_update(omap_lcd
->con
, 0, first
, width
, last
- first
+ 1);
324 omap_lcd
->invalidate
= 0;
327 static void omap_invalidate_display(void *opaque
) {
328 struct omap_lcd_panel_s
*omap_lcd
= opaque
;
329 omap_lcd
->invalidate
= 1;
332 static void omap_lcd_update(struct omap_lcd_panel_s
*s
) {
334 s
->dma
->current_frame
= -1;
338 omap_lcd_interrupts(s
);
342 if (s
->dma
->current_frame
== -1) {
345 s
->dma
->current_frame
= 0;
348 if (!s
->dma
->mpu
->port
[s
->dma
->src
].addr_valid(s
->dma
->mpu
,
349 s
->dma
->src_f1_top
) ||
351 s
->dma
->src
].addr_valid(s
->dma
->mpu
,
352 s
->dma
->src_f1_bottom
) ||
355 s
->dma
->src
].addr_valid(s
->dma
->mpu
,
356 s
->dma
->src_f2_top
) ||
358 s
->dma
->src
].addr_valid(s
->dma
->mpu
,
359 s
->dma
->src_f2_bottom
)))) {
360 s
->dma
->condition
|= 1 << 2;
361 if (s
->dma
->interrupts
& (1 << 1))
362 qemu_irq_raise(s
->dma
->irq
);
367 s
->dma
->phys_framebuffer
[0] = s
->dma
->src_f1_top
;
368 s
->dma
->phys_framebuffer
[1] = s
->dma
->src_f2_top
;
370 if (s
->plm
!= 2 && !s
->palette_done
) {
371 cpu_physical_memory_read(
372 s
->dma
->phys_framebuffer
[s
->dma
->current_frame
],
375 omap_lcd_interrupts(s
);
379 static uint64_t omap_lcdc_read(void *opaque
, hwaddr addr
,
382 struct omap_lcd_panel_s
*s
= (struct omap_lcd_panel_s
*) opaque
;
385 case 0x00: /* LCD_CONTROL */
386 return (s
->tft
<< 23) | (s
->plm
<< 20) |
387 (s
->tft
<< 7) | (s
->interrupts
<< 3) |
388 (s
->mono
<< 1) | s
->enable
| s
->ctrl
| 0xfe000c34;
390 case 0x04: /* LCD_TIMING0 */
391 return (s
->timing
[0] << 10) | (s
->width
- 1) | 0x0000000f;
393 case 0x08: /* LCD_TIMING1 */
394 return (s
->timing
[1] << 10) | (s
->height
- 1);
396 case 0x0c: /* LCD_TIMING2 */
397 return s
->timing
[2] | 0xfc000000;
399 case 0x10: /* LCD_STATUS */
400 return (s
->palette_done
<< 6) | (s
->sync_error
<< 2) | s
->frame_done
;
402 case 0x14: /* LCD_SUBPANEL */
412 static void omap_lcdc_write(void *opaque
, hwaddr addr
,
413 uint64_t value
, unsigned size
)
415 struct omap_lcd_panel_s
*s
= (struct omap_lcd_panel_s
*) opaque
;
418 case 0x00: /* LCD_CONTROL */
419 s
->plm
= (value
>> 20) & 3;
420 s
->tft
= (value
>> 7) & 1;
421 s
->interrupts
= (value
>> 3) & 3;
422 s
->mono
= (value
>> 1) & 1;
423 s
->ctrl
= value
& 0x01cff300;
424 if (s
->enable
!= (value
& 1)) {
425 s
->enable
= value
& 1;
430 case 0x04: /* LCD_TIMING0 */
431 s
->timing
[0] = value
>> 10;
432 s
->width
= (value
& 0x3ff) + 1;
435 case 0x08: /* LCD_TIMING1 */
436 s
->timing
[1] = value
>> 10;
437 s
->height
= (value
& 0x3ff) + 1;
440 case 0x0c: /* LCD_TIMING2 */
441 s
->timing
[2] = value
;
444 case 0x10: /* LCD_STATUS */
447 case 0x14: /* LCD_SUBPANEL */
448 s
->subpanel
= value
& 0xa1ffffff;
456 static const MemoryRegionOps omap_lcdc_ops
= {
457 .read
= omap_lcdc_read
,
458 .write
= omap_lcdc_write
,
459 .endianness
= DEVICE_NATIVE_ENDIAN
,
462 void omap_lcdc_reset(struct omap_lcd_panel_s
*s
)
464 s
->dma
->current_frame
= -1;
484 static const GraphicHwOps omap_ops
= {
485 .invalidate
= omap_invalidate_display
,
486 .gfx_update
= omap_update_display
,
489 struct omap_lcd_panel_s
*omap_lcdc_init(MemoryRegion
*sysmem
,
492 struct omap_dma_lcd_channel_s
*dma
,
495 struct omap_lcd_panel_s
*s
= g_new0(struct omap_lcd_panel_s
, 1);
502 memory_region_init_io(&s
->iomem
, NULL
, &omap_lcdc_ops
, s
, "omap.lcdc", 0x100);
503 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
505 s
->con
= graphic_console_init(NULL
, 0, &omap_ops
, s
);