4 * Copyright (c) 2009 CodeSourcery
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "framebuffer.h"
30 //#define DEBUG_SYBORG_FB
32 #ifdef DEBUG_SYBORG_FB
33 #define DPRINTF(fmt, ...) \
34 do { printf("syborg_fb: " fmt , ## __VA_ARGS__); } while (0)
35 #define BADF(fmt, ...) \
36 do { fprintf(stderr, "syborg_fb: error: " fmt , ## __VA_ARGS__); \
39 #define DPRINTF(fmt, ...) do {} while(0)
40 #define BADF(fmt, ...) \
41 do { fprintf(stderr, "syborg_fb: error: " fmt , ## __VA_ARGS__);} while (0)
52 FB_INTERRUPT_CAUSE
= 7,
59 FB_PALETTE_START
= 0x400 >> 2,
60 FB_PALETTE_END
= FB_PALETTE_START
+256-1,
63 #define FB_INT_VSYNC (1U << 0)
64 #define FB_INT_BASE_UPDATE_DONE (1U << 1)
70 /*QEMUConsole *console;*/
71 uint32_t need_update
: 1;
72 uint32_t need_int
: 1;
84 int rgb
; /* 0 = BGR, 1 = RGB */
85 int endian
; /* 0 = Little, 1 = Big */
86 uint32_t raw_palette
[256];
87 uint32_t palette
[256];
97 /* TODO: Implement these. */
102 #include "pixel_ops.h"
105 #include "pl110_template.h"
107 #include "pl110_template.h"
109 #include "pl110_template.h"
111 #include "pl110_template.h"
113 #include "pl110_template.h"
115 /* Update interrupts. */
116 static void syborg_fb_update(SyborgFBState
*s
)
118 if ((s
->int_status
& s
->int_enable
) != 0) {
119 DPRINTF("Raise IRQ\n");
120 qemu_irq_raise(s
->irq
);
122 DPRINTF("Lower IRQ\n");
123 qemu_irq_lower(s
->irq
);
127 static int syborg_fb_enabled(const SyborgFBState
*s
)
132 static void syborg_fb_update_palette(SyborgFBState
*s
)
136 unsigned int r
, g
, b
;
139 case BPP_SRC_1
: n
= 2; break;
140 case BPP_SRC_2
: n
= 4; break;
141 case BPP_SRC_4
: n
= 16; break;
142 case BPP_SRC_8
: n
= 256; break;
146 for (i
= 0; i
< n
; i
++) {
147 raw
= s
->raw_palette
[i
];
148 r
= (raw
>> 16) & 0xff;
149 g
= (raw
>> 8) & 0xff;
151 switch (ds_get_bits_per_pixel(s
->ds
)) {
153 s
->palette
[i
] = rgb_to_pixel8(r
, g
, b
);
156 s
->palette
[i
] = rgb_to_pixel15(r
, g
, b
);
159 s
->palette
[i
] = rgb_to_pixel16(r
, g
, b
);
163 s
->palette
[i
] = rgb_to_pixel32(r
, g
, b
);
172 static void syborg_fb_update_display(void *opaque
)
174 SyborgFBState
*s
= (SyborgFBState
*)opaque
;
183 if (!syborg_fb_enabled(s
))
186 switch (ds_get_bits_per_pixel(s
->ds
)) {
190 fntable
= pl110_draw_fn_8
;
194 fntable
= pl110_draw_fn_15
;
198 fntable
= pl110_draw_fn_16
;
202 fntable
= pl110_draw_fn_24
;
206 fntable
= pl110_draw_fn_32
;
210 fprintf(stderr
, "syborg_fb: Bad color depth\n");
215 s
->int_status
|= FB_INT_BASE_UPDATE_DONE
;
228 /* Our bpp constants mostly match the PL110/PL111 but
229 * not for the 16 bit case
236 bpp_offset
+= s
->bpp
;
238 fn
= fntable
[bpp_offset
];
241 src_width
= s
->pitch
;
268 dest_width
*= s
->cols
;
270 /* TODO: Implement blanking. */
272 if (s
->need_update
&& s
->bpp
<= BPP_SRC_8
) {
273 syborg_fb_update_palette(s
);
275 framebuffer_update_display(s
->ds
,
276 s
->base
, s
->cols
, s
->rows
,
277 src_width
, dest_width
, 0,
282 dpy_update(s
->ds
, 0, first
, s
->cols
, last
- first
+ 1);
285 s
->int_status
|= FB_INT_VSYNC
;
292 static void syborg_fb_invalidate_display(void * opaque
)
294 SyborgFBState
*s
= (SyborgFBState
*)opaque
;
298 static uint64_t syborg_fb_read(void *opaque
, target_phys_addr_t offset
,
301 SyborgFBState
*s
= opaque
;
303 DPRINTF("read reg %d\n", (int)offset
);
305 switch (offset
>> 2) {
307 return SYBORG_ID_FRAMEBUFFER
;
325 return s
->int_enable
;
327 case FB_INTERRUPT_CAUSE
:
328 return s
->int_status
;
332 case BPP_SRC_1
: return 1;
333 case BPP_SRC_2
: return 2;
334 case BPP_SRC_4
: return 4;
335 case BPP_SRC_8
: return 8;
336 case BPP_SRC_15
: return 15;
337 case BPP_SRC_16
: return 16;
338 case BPP_SRC_24
: return 24;
339 case BPP_SRC_32
: return 32;
359 if ((offset
>> 2) >= FB_PALETTE_START
360 && (offset
>> 2) <= FB_PALETTE_END
) {
361 return s
->raw_palette
[(offset
>> 2) - FB_PALETTE_START
];
363 cpu_abort (cpu_single_env
, "syborg_fb_read: Bad offset %x\n",
370 static void syborg_fb_write(void *opaque
, target_phys_addr_t offset
,
371 uint64_t val
, unsigned size
)
373 SyborgFBState
*s
= opaque
;
375 DPRINTF("write reg %d = %d\n", (int)offset
, val
);
378 switch (offset
>> 2) {
395 /* TODO: Implement rotation. */
407 case FB_INTERRUPT_CAUSE
:
408 s
->int_status
&= ~val
;
414 case 1: val
= BPP_SRC_1
; break;
415 case 2: val
= BPP_SRC_2
; break;
416 case 4: val
= BPP_SRC_4
; break;
417 case 8: val
= BPP_SRC_8
; break;
418 /* case 15: val = BPP_SRC_15; break; */
419 case 16: val
= BPP_SRC_16
; break;
420 /* case 24: val = BPP_SRC_24; break; */
421 case 32: val
= BPP_SRC_32
; break;
422 default: val
= s
->bpp
; break;
432 s
->endian
= (val
!= 0);
436 /* TODO: Implement this. */
448 if ((offset
>> 2) >= FB_PALETTE_START
449 && (offset
>> 2) <= FB_PALETTE_END
) {
450 s
->raw_palette
[(offset
>> 2) - FB_PALETTE_START
] = val
;
452 cpu_abort (cpu_single_env
, "syborg_fb_write: Bad offset %x\n",
459 static const MemoryRegionOps syborg_fb_ops
= {
460 .read
= syborg_fb_read
,
461 .write
= syborg_fb_write
,
462 .endianness
= DEVICE_NATIVE_ENDIAN
,
465 static void syborg_fb_save(QEMUFile
*f
, void *opaque
)
467 SyborgFBState
*s
= opaque
;
470 qemu_put_be32(f
, s
->need_int
);
471 qemu_put_be32(f
, s
->int_status
);
472 qemu_put_be32(f
, s
->int_enable
);
473 qemu_put_be32(f
, s
->enabled
);
474 qemu_put_be32(f
, s
->base
);
475 qemu_put_be32(f
, s
->pitch
);
476 qemu_put_be32(f
, s
->rows
);
477 qemu_put_be32(f
, s
->cols
);
478 qemu_put_be32(f
, s
->bpp
);
479 qemu_put_be32(f
, s
->rgb
);
480 for (i
= 0; i
< 256; i
++) {
481 qemu_put_be32(f
, s
->raw_palette
[i
]);
485 static int syborg_fb_load(QEMUFile
*f
, void *opaque
, int version_id
)
487 SyborgFBState
*s
= opaque
;
493 s
->need_int
= qemu_get_be32(f
);
494 s
->int_status
= qemu_get_be32(f
);
495 s
->int_enable
= qemu_get_be32(f
);
496 s
->enabled
= qemu_get_be32(f
);
497 s
->base
= qemu_get_be32(f
);
498 s
->pitch
= qemu_get_be32(f
);
499 s
->rows
= qemu_get_be32(f
);
500 s
->cols
= qemu_get_be32(f
);
501 s
->bpp
= qemu_get_be32(f
);
502 s
->rgb
= qemu_get_be32(f
);
503 for (i
= 0; i
< 256; i
++) {
504 s
->raw_palette
[i
] = qemu_get_be32(f
);
511 static int syborg_fb_init(SysBusDevice
*dev
)
513 SyborgFBState
*s
= FROM_SYSBUS(SyborgFBState
, dev
);
515 sysbus_init_irq(dev
, &s
->irq
);
516 memory_region_init_io(&s
->iomem
, &syborg_fb_ops
, s
,
517 "framebuffer", 0x1000);
518 sysbus_init_mmio(dev
, &s
->iomem
);
520 s
->ds
= graphic_console_init(syborg_fb_update_display
,
521 syborg_fb_invalidate_display
,
524 if (s
->cols
!= 0 && s
->rows
!= 0) {
525 qemu_console_resize(s
->ds
, s
->cols
, s
->rows
);
529 s
->cols
= ds_get_width(s
->ds
);
531 s
->rows
= ds_get_height(s
->ds
);
533 register_savevm(&dev
->qdev
, "syborg_framebuffer", -1, 1,
534 syborg_fb_save
, syborg_fb_load
, s
);
538 static SysBusDeviceInfo syborg_fb_info
= {
539 .init
= syborg_fb_init
,
540 .qdev
.name
= "syborg,framebuffer",
541 .qdev
.size
= sizeof(SyborgFBState
),
542 .qdev
.props
= (Property
[]) {
543 DEFINE_PROP_UINT32("width", SyborgFBState
, cols
, 0),
544 DEFINE_PROP_UINT32("height", SyborgFBState
, rows
, 0),
545 DEFINE_PROP_END_OF_LIST(),
549 static void syborg_fb_register_devices(void)
551 sysbus_register_withprop(&syborg_fb_info
);
554 device_init(syborg_fb_register_devices
)