3 * QEMU model of the Milkymist VGA framebuffer.
5 * Copyright (c) 2010-2012 Michael Walle <michael@walle.cc>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Specification available at:
22 * http://www.milkymist.org/socdoc/vgafb.pdf
26 #include "hw/sysbus.h"
28 #include "ui/console.h"
29 #include "framebuffer.h"
30 #include "ui/pixel_ops.h"
31 #include "qemu/error-report.h"
34 #include "milkymist-vgafb_template.h"
36 #include "milkymist-vgafb_template.h"
38 #include "milkymist-vgafb_template.h"
40 #include "milkymist-vgafb_template.h"
42 #include "milkymist-vgafb_template.h"
66 #define TYPE_MILKYMIST_VGAFB "milkymist-vgafb"
67 #define MILKYMIST_VGAFB(obj) \
68 OBJECT_CHECK(MilkymistVgafbState, (obj), TYPE_MILKYMIST_VGAFB)
70 struct MilkymistVgafbState
{
71 SysBusDevice parent_obj
;
73 MemoryRegion regs_region
;
74 MemoryRegionSection fbsection
;
83 typedef struct MilkymistVgafbState MilkymistVgafbState
;
85 static int vgafb_enabled(MilkymistVgafbState
*s
)
87 return !(s
->regs
[R_CTRL
] & CTRL_RESET
);
90 static void vgafb_update_display(void *opaque
)
92 MilkymistVgafbState
*s
= opaque
;
94 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
100 if (!vgafb_enabled(s
)) {
104 sbd
= SYS_BUS_DEVICE(s
);
105 int dest_width
= s
->regs
[R_HRES
];
107 switch (surface_bits_per_pixel(surface
)) {
130 hw_error("milkymist_vgafb: bad color depth\n");
134 src_width
= s
->regs
[R_HRES
] * 2;
136 framebuffer_update_memory_section(&s
->fbsection
,
137 sysbus_address_space(sbd
),
138 s
->regs
[R_BASEADDRESS
] + s
->fb_offset
,
139 s
->regs
[R_VRES
], src_width
);
142 framebuffer_update_display(surface
, &s
->fbsection
,
154 dpy_gfx_update(s
->con
, 0, first
, s
->regs
[R_HRES
], last
- first
+ 1);
159 static void vgafb_invalidate_display(void *opaque
)
161 MilkymistVgafbState
*s
= opaque
;
165 static void vgafb_resize(MilkymistVgafbState
*s
)
167 if (!vgafb_enabled(s
)) {
171 qemu_console_resize(s
->con
, s
->regs
[R_HRES
], s
->regs
[R_VRES
]);
175 static uint64_t vgafb_read(void *opaque
, hwaddr addr
,
178 MilkymistVgafbState
*s
= opaque
;
198 case R_BASEADDRESS_ACT
:
199 r
= s
->regs
[R_BASEADDRESS
];
203 error_report("milkymist_vgafb: read access to unknown register 0x"
204 TARGET_FMT_plx
, addr
<< 2);
208 trace_milkymist_vgafb_memory_read(addr
<< 2, r
);
213 static void vgafb_write(void *opaque
, hwaddr addr
, uint64_t value
,
216 MilkymistVgafbState
*s
= opaque
;
218 trace_milkymist_vgafb_memory_write(addr
, value
);
223 s
->regs
[addr
] = value
;
235 s
->regs
[addr
] = value
;
239 error_report("milkymist_vgafb: framebuffer base address have to "
240 "be 32 byte aligned");
243 s
->regs
[addr
] = value
& s
->fb_mask
;
248 s
->regs
[addr
] = value
;
251 case R_BASEADDRESS_ACT
:
252 error_report("milkymist_vgafb: write to read-only register 0x"
253 TARGET_FMT_plx
, addr
<< 2);
257 error_report("milkymist_vgafb: write access to unknown register 0x"
258 TARGET_FMT_plx
, addr
<< 2);
263 static const MemoryRegionOps vgafb_mmio_ops
= {
265 .write
= vgafb_write
,
267 .min_access_size
= 4,
268 .max_access_size
= 4,
270 .endianness
= DEVICE_NATIVE_ENDIAN
,
273 static void milkymist_vgafb_reset(DeviceState
*d
)
275 MilkymistVgafbState
*s
= MILKYMIST_VGAFB(d
);
278 for (i
= 0; i
< R_MAX
; i
++) {
283 s
->regs
[R_CTRL
] = CTRL_RESET
;
284 s
->regs
[R_HRES
] = 640;
285 s
->regs
[R_VRES
] = 480;
286 s
->regs
[R_BASEADDRESS
] = 0;
289 static const GraphicHwOps vgafb_ops
= {
290 .invalidate
= vgafb_invalidate_display
,
291 .gfx_update
= vgafb_update_display
,
294 static int milkymist_vgafb_init(SysBusDevice
*dev
)
296 MilkymistVgafbState
*s
= MILKYMIST_VGAFB(dev
);
298 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &vgafb_mmio_ops
, s
,
299 "milkymist-vgafb", R_MAX
* 4);
300 sysbus_init_mmio(dev
, &s
->regs_region
);
302 s
->con
= graphic_console_init(DEVICE(dev
), 0, &vgafb_ops
, s
);
307 static int vgafb_post_load(void *opaque
, int version_id
)
309 vgafb_invalidate_display(opaque
);
313 static const VMStateDescription vmstate_milkymist_vgafb
= {
314 .name
= "milkymist-vgafb",
316 .minimum_version_id
= 1,
317 .post_load
= vgafb_post_load
,
318 .fields
= (VMStateField
[]) {
319 VMSTATE_UINT32_ARRAY(regs
, MilkymistVgafbState
, R_MAX
),
320 VMSTATE_END_OF_LIST()
324 static Property milkymist_vgafb_properties
[] = {
325 DEFINE_PROP_UINT32("fb_offset", MilkymistVgafbState
, fb_offset
, 0x0),
326 DEFINE_PROP_UINT32("fb_mask", MilkymistVgafbState
, fb_mask
, 0xffffffff),
327 DEFINE_PROP_END_OF_LIST(),
330 static void milkymist_vgafb_class_init(ObjectClass
*klass
, void *data
)
332 DeviceClass
*dc
= DEVICE_CLASS(klass
);
333 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
335 k
->init
= milkymist_vgafb_init
;
336 dc
->reset
= milkymist_vgafb_reset
;
337 dc
->vmsd
= &vmstate_milkymist_vgafb
;
338 dc
->props
= milkymist_vgafb_properties
;
341 static const TypeInfo milkymist_vgafb_info
= {
342 .name
= TYPE_MILKYMIST_VGAFB
,
343 .parent
= TYPE_SYS_BUS_DEVICE
,
344 .instance_size
= sizeof(MilkymistVgafbState
),
345 .class_init
= milkymist_vgafb_class_init
,
348 static void milkymist_vgafb_register_types(void)
350 type_register_static(&milkymist_vgafb_info
);
353 type_init(milkymist_vgafb_register_types
)