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.1 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://milkymist.walle.cc/socdoc/vgafb.pdf
25 #include "qemu/osdep.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/sysbus.h"
30 #include "ui/console.h"
31 #include "framebuffer.h"
32 #include "ui/pixel_ops.h"
33 #include "qemu/error-report.h"
34 #include "qemu/module.h"
35 #include "qom/object.h"
38 #include "migration/vmstate.h"
39 #include "milkymist-vgafb_template.h"
41 #include "milkymist-vgafb_template.h"
43 #include "milkymist-vgafb_template.h"
45 #include "milkymist-vgafb_template.h"
47 #include "milkymist-vgafb_template.h"
71 #define TYPE_MILKYMIST_VGAFB "milkymist-vgafb"
72 OBJECT_DECLARE_SIMPLE_TYPE(MilkymistVgafbState
, MILKYMIST_VGAFB
)
74 struct MilkymistVgafbState
{
75 SysBusDevice parent_obj
;
77 MemoryRegion regs_region
;
78 MemoryRegionSection fbsection
;
88 static int vgafb_enabled(MilkymistVgafbState
*s
)
90 return !(s
->regs
[R_CTRL
] & CTRL_RESET
);
93 static void vgafb_update_display(void *opaque
)
95 MilkymistVgafbState
*s
= opaque
;
97 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
103 if (!vgafb_enabled(s
)) {
107 sbd
= SYS_BUS_DEVICE(s
);
108 int dest_width
= s
->regs
[R_HRES
];
110 switch (surface_bits_per_pixel(surface
)) {
133 hw_error("milkymist_vgafb: bad color depth\n");
137 src_width
= s
->regs
[R_HRES
] * 2;
139 framebuffer_update_memory_section(&s
->fbsection
,
140 sysbus_address_space(sbd
),
141 s
->regs
[R_BASEADDRESS
] + s
->fb_offset
,
142 s
->regs
[R_VRES
], src_width
);
145 framebuffer_update_display(surface
, &s
->fbsection
,
157 dpy_gfx_update(s
->con
, 0, first
, s
->regs
[R_HRES
], last
- first
+ 1);
162 static void vgafb_invalidate_display(void *opaque
)
164 MilkymistVgafbState
*s
= opaque
;
168 static void vgafb_resize(MilkymistVgafbState
*s
)
170 if (!vgafb_enabled(s
)) {
174 qemu_console_resize(s
->con
, s
->regs
[R_HRES
], s
->regs
[R_VRES
]);
178 static uint64_t vgafb_read(void *opaque
, hwaddr addr
,
181 MilkymistVgafbState
*s
= opaque
;
201 case R_BASEADDRESS_ACT
:
202 r
= s
->regs
[R_BASEADDRESS
];
206 error_report("milkymist_vgafb: read access to unknown register 0x"
207 TARGET_FMT_plx
, addr
<< 2);
211 trace_milkymist_vgafb_memory_read(addr
<< 2, r
);
216 static void vgafb_write(void *opaque
, hwaddr addr
, uint64_t value
,
219 MilkymistVgafbState
*s
= opaque
;
221 trace_milkymist_vgafb_memory_write(addr
, value
);
226 s
->regs
[addr
] = value
;
238 s
->regs
[addr
] = value
;
242 error_report("milkymist_vgafb: framebuffer base address have to "
243 "be 32 byte aligned");
246 s
->regs
[addr
] = value
& s
->fb_mask
;
251 s
->regs
[addr
] = value
;
254 case R_BASEADDRESS_ACT
:
255 error_report("milkymist_vgafb: write to read-only register 0x"
256 TARGET_FMT_plx
, addr
<< 2);
260 error_report("milkymist_vgafb: write access to unknown register 0x"
261 TARGET_FMT_plx
, addr
<< 2);
266 static const MemoryRegionOps vgafb_mmio_ops
= {
268 .write
= vgafb_write
,
270 .min_access_size
= 4,
271 .max_access_size
= 4,
273 .endianness
= DEVICE_NATIVE_ENDIAN
,
276 static void milkymist_vgafb_reset(DeviceState
*d
)
278 MilkymistVgafbState
*s
= MILKYMIST_VGAFB(d
);
281 for (i
= 0; i
< R_MAX
; i
++) {
286 s
->regs
[R_CTRL
] = CTRL_RESET
;
287 s
->regs
[R_HRES
] = 640;
288 s
->regs
[R_VRES
] = 480;
289 s
->regs
[R_BASEADDRESS
] = 0;
292 static const GraphicHwOps vgafb_ops
= {
293 .invalidate
= vgafb_invalidate_display
,
294 .gfx_update
= vgafb_update_display
,
297 static void milkymist_vgafb_init(Object
*obj
)
299 MilkymistVgafbState
*s
= MILKYMIST_VGAFB(obj
);
300 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
302 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &vgafb_mmio_ops
, s
,
303 "milkymist-vgafb", R_MAX
* 4);
304 sysbus_init_mmio(dev
, &s
->regs_region
);
307 static void milkymist_vgafb_realize(DeviceState
*dev
, Error
**errp
)
309 MilkymistVgafbState
*s
= MILKYMIST_VGAFB(dev
);
311 s
->con
= graphic_console_init(dev
, 0, &vgafb_ops
, s
);
314 static int vgafb_post_load(void *opaque
, int version_id
)
316 vgafb_invalidate_display(opaque
);
320 static const VMStateDescription vmstate_milkymist_vgafb
= {
321 .name
= "milkymist-vgafb",
323 .minimum_version_id
= 1,
324 .post_load
= vgafb_post_load
,
325 .fields
= (VMStateField
[]) {
326 VMSTATE_UINT32_ARRAY(regs
, MilkymistVgafbState
, R_MAX
),
327 VMSTATE_END_OF_LIST()
331 static Property milkymist_vgafb_properties
[] = {
332 DEFINE_PROP_UINT32("fb_offset", MilkymistVgafbState
, fb_offset
, 0x0),
333 DEFINE_PROP_UINT32("fb_mask", MilkymistVgafbState
, fb_mask
, 0xffffffff),
334 DEFINE_PROP_END_OF_LIST(),
337 static void milkymist_vgafb_class_init(ObjectClass
*klass
, void *data
)
339 DeviceClass
*dc
= DEVICE_CLASS(klass
);
341 dc
->reset
= milkymist_vgafb_reset
;
342 dc
->vmsd
= &vmstate_milkymist_vgafb
;
343 device_class_set_props(dc
, milkymist_vgafb_properties
);
344 dc
->realize
= milkymist_vgafb_realize
;
347 static const TypeInfo milkymist_vgafb_info
= {
348 .name
= TYPE_MILKYMIST_VGAFB
,
349 .parent
= TYPE_SYS_BUS_DEVICE
,
350 .instance_size
= sizeof(MilkymistVgafbState
),
351 .instance_init
= milkymist_vgafb_init
,
352 .class_init
= milkymist_vgafb_class_init
,
355 static void milkymist_vgafb_register_types(void)
357 type_register_static(&milkymist_vgafb_info
);
360 type_init(milkymist_vgafb_register_types
)