3 * QEMU model of the Milkymist VGA framebuffer.
5 * Copyright (c) 2010 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
29 #include "framebuffer.h"
30 #include "pixel_ops.h"
31 #include "qemu-error.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"
65 struct MilkymistVgafbState
{
67 MemoryRegion regs_region
;
76 typedef struct MilkymistVgafbState MilkymistVgafbState
;
78 static int vgafb_enabled(MilkymistVgafbState
*s
)
80 return !(s
->regs
[R_CTRL
] & CTRL_RESET
);
83 static void vgafb_update_display(void *opaque
)
85 MilkymistVgafbState
*s
= opaque
;
90 if (!vgafb_enabled(s
)) {
94 int dest_width
= s
->regs
[R_HRES
];
96 switch (ds_get_bits_per_pixel(s
->ds
)) {
119 hw_error("milkymist_vgafb: bad color depth\n");
123 framebuffer_update_display(s
->ds
, sysbus_address_space(&s
->busdev
),
124 s
->regs
[R_BASEADDRESS
] + s
->fb_offset
,
136 dpy_update(s
->ds
, 0, first
, s
->regs
[R_HRES
], last
- first
+ 1);
141 static void vgafb_invalidate_display(void *opaque
)
143 MilkymistVgafbState
*s
= opaque
;
147 static void vgafb_resize(MilkymistVgafbState
*s
)
149 if (!vgafb_enabled(s
)) {
153 qemu_console_resize(s
->ds
, s
->regs
[R_HRES
], s
->regs
[R_VRES
]);
157 static uint64_t vgafb_read(void *opaque
, target_phys_addr_t addr
,
160 MilkymistVgafbState
*s
= opaque
;
179 case R_BASEADDRESS_ACT
:
180 r
= s
->regs
[R_BASEADDRESS
];
184 error_report("milkymist_vgafb: read access to unknown register 0x"
185 TARGET_FMT_plx
, addr
<< 2);
189 trace_milkymist_vgafb_memory_read(addr
<< 2, r
);
194 static void vgafb_write(void *opaque
, target_phys_addr_t addr
, uint64_t value
,
197 MilkymistVgafbState
*s
= opaque
;
199 trace_milkymist_vgafb_memory_write(addr
, value
);
204 s
->regs
[addr
] = value
;
215 s
->regs
[addr
] = value
;
219 error_report("milkymist_vgafb: framebuffer base address have to "
220 "be 32 byte aligned");
223 s
->regs
[addr
] = value
& s
->fb_mask
;
228 s
->regs
[addr
] = value
;
231 case R_BASEADDRESS_ACT
:
232 error_report("milkymist_vgafb: write to read-only register 0x"
233 TARGET_FMT_plx
, addr
<< 2);
237 error_report("milkymist_vgafb: write access to unknown register 0x"
238 TARGET_FMT_plx
, addr
<< 2);
243 static const MemoryRegionOps vgafb_mmio_ops
= {
245 .write
= vgafb_write
,
247 .min_access_size
= 4,
248 .max_access_size
= 4,
250 .endianness
= DEVICE_NATIVE_ENDIAN
,
253 static void milkymist_vgafb_reset(DeviceState
*d
)
255 MilkymistVgafbState
*s
= container_of(d
, MilkymistVgafbState
, busdev
.qdev
);
258 for (i
= 0; i
< R_MAX
; i
++) {
263 s
->regs
[R_CTRL
] = CTRL_RESET
;
264 s
->regs
[R_HRES
] = 640;
265 s
->regs
[R_VRES
] = 480;
266 s
->regs
[R_BASEADDRESS
] = 0;
269 static int milkymist_vgafb_init(SysBusDevice
*dev
)
271 MilkymistVgafbState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
273 memory_region_init_io(&s
->regs_region
, &vgafb_mmio_ops
, s
,
274 "milkymist-vgafb", R_MAX
* 4);
275 sysbus_init_mmio(dev
, &s
->regs_region
);
277 s
->ds
= graphic_console_init(vgafb_update_display
,
278 vgafb_invalidate_display
,
284 static int vgafb_post_load(void *opaque
, int version_id
)
286 vgafb_invalidate_display(opaque
);
290 static const VMStateDescription vmstate_milkymist_vgafb
= {
291 .name
= "milkymist-vgafb",
293 .minimum_version_id
= 1,
294 .minimum_version_id_old
= 1,
295 .post_load
= vgafb_post_load
,
296 .fields
= (VMStateField
[]) {
297 VMSTATE_UINT32_ARRAY(regs
, MilkymistVgafbState
, R_MAX
),
298 VMSTATE_END_OF_LIST()
302 static Property milkymist_vgafb_properties
[] = {
303 DEFINE_PROP_UINT32("fb_offset", MilkymistVgafbState
, fb_offset
, 0x0),
304 DEFINE_PROP_UINT32("fb_mask", MilkymistVgafbState
, fb_mask
, 0xffffffff),
305 DEFINE_PROP_END_OF_LIST(),
308 static void milkymist_vgafb_class_init(ObjectClass
*klass
, void *data
)
310 DeviceClass
*dc
= DEVICE_CLASS(klass
);
311 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
313 k
->init
= milkymist_vgafb_init
;
314 dc
->reset
= milkymist_vgafb_reset
;
315 dc
->vmsd
= &vmstate_milkymist_vgafb
;
316 dc
->props
= milkymist_vgafb_properties
;
319 static TypeInfo milkymist_vgafb_info
= {
320 .name
= "milkymist-vgafb",
321 .parent
= TYPE_SYS_BUS_DEVICE
,
322 .instance_size
= sizeof(MilkymistVgafbState
),
323 .class_init
= milkymist_vgafb_class_init
,
326 static void milkymist_vgafb_register_types(void)
328 type_register_static(&milkymist_vgafb_info
);
331 type_init(milkymist_vgafb_register_types
)