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://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"
37 #include "migration/vmstate.h"
38 #include "milkymist-vgafb_template.h"
40 #include "milkymist-vgafb_template.h"
42 #include "milkymist-vgafb_template.h"
44 #include "milkymist-vgafb_template.h"
46 #include "milkymist-vgafb_template.h"
70 #define TYPE_MILKYMIST_VGAFB "milkymist-vgafb"
71 #define MILKYMIST_VGAFB(obj) \
72 OBJECT_CHECK(MilkymistVgafbState, (obj), TYPE_MILKYMIST_VGAFB)
74 struct MilkymistVgafbState
{
75 SysBusDevice parent_obj
;
77 MemoryRegion regs_region
;
78 MemoryRegionSection fbsection
;
87 typedef struct MilkymistVgafbState MilkymistVgafbState
;
89 static int vgafb_enabled(MilkymistVgafbState
*s
)
91 return !(s
->regs
[R_CTRL
] & CTRL_RESET
);
94 static void vgafb_update_display(void *opaque
)
96 MilkymistVgafbState
*s
= opaque
;
98 DisplaySurface
*surface
= qemu_console_surface(s
->con
);
104 if (!vgafb_enabled(s
)) {
108 sbd
= SYS_BUS_DEVICE(s
);
109 int dest_width
= s
->regs
[R_HRES
];
111 switch (surface_bits_per_pixel(surface
)) {
134 hw_error("milkymist_vgafb: bad color depth\n");
138 src_width
= s
->regs
[R_HRES
] * 2;
140 framebuffer_update_memory_section(&s
->fbsection
,
141 sysbus_address_space(sbd
),
142 s
->regs
[R_BASEADDRESS
] + s
->fb_offset
,
143 s
->regs
[R_VRES
], src_width
);
146 framebuffer_update_display(surface
, &s
->fbsection
,
158 dpy_gfx_update(s
->con
, 0, first
, s
->regs
[R_HRES
], last
- first
+ 1);
163 static void vgafb_invalidate_display(void *opaque
)
165 MilkymistVgafbState
*s
= opaque
;
169 static void vgafb_resize(MilkymistVgafbState
*s
)
171 if (!vgafb_enabled(s
)) {
175 qemu_console_resize(s
->con
, s
->regs
[R_HRES
], s
->regs
[R_VRES
]);
179 static uint64_t vgafb_read(void *opaque
, hwaddr addr
,
182 MilkymistVgafbState
*s
= opaque
;
202 case R_BASEADDRESS_ACT
:
203 r
= s
->regs
[R_BASEADDRESS
];
207 error_report("milkymist_vgafb: read access to unknown register 0x"
208 TARGET_FMT_plx
, addr
<< 2);
212 trace_milkymist_vgafb_memory_read(addr
<< 2, r
);
217 static void vgafb_write(void *opaque
, hwaddr addr
, uint64_t value
,
220 MilkymistVgafbState
*s
= opaque
;
222 trace_milkymist_vgafb_memory_write(addr
, value
);
227 s
->regs
[addr
] = value
;
239 s
->regs
[addr
] = value
;
243 error_report("milkymist_vgafb: framebuffer base address have to "
244 "be 32 byte aligned");
247 s
->regs
[addr
] = value
& s
->fb_mask
;
252 s
->regs
[addr
] = value
;
255 case R_BASEADDRESS_ACT
:
256 error_report("milkymist_vgafb: write to read-only register 0x"
257 TARGET_FMT_plx
, addr
<< 2);
261 error_report("milkymist_vgafb: write access to unknown register 0x"
262 TARGET_FMT_plx
, addr
<< 2);
267 static const MemoryRegionOps vgafb_mmio_ops
= {
269 .write
= vgafb_write
,
271 .min_access_size
= 4,
272 .max_access_size
= 4,
274 .endianness
= DEVICE_NATIVE_ENDIAN
,
277 static void milkymist_vgafb_reset(DeviceState
*d
)
279 MilkymistVgafbState
*s
= MILKYMIST_VGAFB(d
);
282 for (i
= 0; i
< R_MAX
; i
++) {
287 s
->regs
[R_CTRL
] = CTRL_RESET
;
288 s
->regs
[R_HRES
] = 640;
289 s
->regs
[R_VRES
] = 480;
290 s
->regs
[R_BASEADDRESS
] = 0;
293 static const GraphicHwOps vgafb_ops
= {
294 .invalidate
= vgafb_invalidate_display
,
295 .gfx_update
= vgafb_update_display
,
298 static void milkymist_vgafb_init(Object
*obj
)
300 MilkymistVgafbState
*s
= MILKYMIST_VGAFB(obj
);
301 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
303 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &vgafb_mmio_ops
, s
,
304 "milkymist-vgafb", R_MAX
* 4);
305 sysbus_init_mmio(dev
, &s
->regs_region
);
308 static void milkymist_vgafb_realize(DeviceState
*dev
, Error
**errp
)
310 MilkymistVgafbState
*s
= MILKYMIST_VGAFB(dev
);
312 s
->con
= graphic_console_init(dev
, 0, &vgafb_ops
, s
);
315 static int vgafb_post_load(void *opaque
, int version_id
)
317 vgafb_invalidate_display(opaque
);
321 static const VMStateDescription vmstate_milkymist_vgafb
= {
322 .name
= "milkymist-vgafb",
324 .minimum_version_id
= 1,
325 .post_load
= vgafb_post_load
,
326 .fields
= (VMStateField
[]) {
327 VMSTATE_UINT32_ARRAY(regs
, MilkymistVgafbState
, R_MAX
),
328 VMSTATE_END_OF_LIST()
332 static Property milkymist_vgafb_properties
[] = {
333 DEFINE_PROP_UINT32("fb_offset", MilkymistVgafbState
, fb_offset
, 0x0),
334 DEFINE_PROP_UINT32("fb_mask", MilkymistVgafbState
, fb_mask
, 0xffffffff),
335 DEFINE_PROP_END_OF_LIST(),
338 static void milkymist_vgafb_class_init(ObjectClass
*klass
, void *data
)
340 DeviceClass
*dc
= DEVICE_CLASS(klass
);
342 dc
->reset
= milkymist_vgafb_reset
;
343 dc
->vmsd
= &vmstate_milkymist_vgafb
;
344 device_class_set_props(dc
, milkymist_vgafb_properties
);
345 dc
->realize
= milkymist_vgafb_realize
;
348 static const TypeInfo milkymist_vgafb_info
= {
349 .name
= TYPE_MILKYMIST_VGAFB
,
350 .parent
= TYPE_SYS_BUS_DEVICE
,
351 .instance_size
= sizeof(MilkymistVgafbState
),
352 .instance_init
= milkymist_vgafb_init
,
353 .class_init
= milkymist_vgafb_class_init
,
356 static void milkymist_vgafb_register_types(void)
358 type_register_static(&milkymist_vgafb_info
);
361 type_init(milkymist_vgafb_register_types
)