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
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"
66 struct MilkymistVgafbState
{
68 MemoryRegion regs_region
;
77 typedef struct MilkymistVgafbState MilkymistVgafbState
;
79 static int vgafb_enabled(MilkymistVgafbState
*s
)
81 return !(s
->regs
[R_CTRL
] & CTRL_RESET
);
84 static void vgafb_update_display(void *opaque
)
86 MilkymistVgafbState
*s
= opaque
;
91 if (!vgafb_enabled(s
)) {
95 int dest_width
= s
->regs
[R_HRES
];
97 switch (ds_get_bits_per_pixel(s
->ds
)) {
120 hw_error("milkymist_vgafb: bad color depth\n");
124 framebuffer_update_display(s
->ds
, sysbus_address_space(&s
->busdev
),
125 s
->regs
[R_BASEADDRESS
] + s
->fb_offset
,
137 dpy_update(s
->ds
, 0, first
, s
->regs
[R_HRES
], last
- first
+ 1);
142 static void vgafb_invalidate_display(void *opaque
)
144 MilkymistVgafbState
*s
= opaque
;
148 static void vgafb_resize(MilkymistVgafbState
*s
)
150 if (!vgafb_enabled(s
)) {
154 qemu_console_resize(s
->ds
, s
->regs
[R_HRES
], s
->regs
[R_VRES
]);
158 static uint64_t vgafb_read(void *opaque
, target_phys_addr_t addr
,
161 MilkymistVgafbState
*s
= opaque
;
181 case R_BASEADDRESS_ACT
:
182 r
= s
->regs
[R_BASEADDRESS
];
186 error_report("milkymist_vgafb: read access to unknown register 0x"
187 TARGET_FMT_plx
, addr
<< 2);
191 trace_milkymist_vgafb_memory_read(addr
<< 2, r
);
196 static void vgafb_write(void *opaque
, target_phys_addr_t addr
, uint64_t value
,
199 MilkymistVgafbState
*s
= opaque
;
201 trace_milkymist_vgafb_memory_write(addr
, value
);
206 s
->regs
[addr
] = value
;
218 s
->regs
[addr
] = value
;
222 error_report("milkymist_vgafb: framebuffer base address have to "
223 "be 32 byte aligned");
226 s
->regs
[addr
] = value
& s
->fb_mask
;
231 s
->regs
[addr
] = value
;
234 case R_BASEADDRESS_ACT
:
235 error_report("milkymist_vgafb: write to read-only register 0x"
236 TARGET_FMT_plx
, addr
<< 2);
240 error_report("milkymist_vgafb: write access to unknown register 0x"
241 TARGET_FMT_plx
, addr
<< 2);
246 static const MemoryRegionOps vgafb_mmio_ops
= {
248 .write
= vgafb_write
,
250 .min_access_size
= 4,
251 .max_access_size
= 4,
253 .endianness
= DEVICE_NATIVE_ENDIAN
,
256 static void milkymist_vgafb_reset(DeviceState
*d
)
258 MilkymistVgafbState
*s
= container_of(d
, MilkymistVgafbState
, busdev
.qdev
);
261 for (i
= 0; i
< R_MAX
; i
++) {
266 s
->regs
[R_CTRL
] = CTRL_RESET
;
267 s
->regs
[R_HRES
] = 640;
268 s
->regs
[R_VRES
] = 480;
269 s
->regs
[R_BASEADDRESS
] = 0;
272 static int milkymist_vgafb_init(SysBusDevice
*dev
)
274 MilkymistVgafbState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
276 memory_region_init_io(&s
->regs_region
, &vgafb_mmio_ops
, s
,
277 "milkymist-vgafb", R_MAX
* 4);
278 sysbus_init_mmio(dev
, &s
->regs_region
);
280 s
->ds
= graphic_console_init(vgafb_update_display
,
281 vgafb_invalidate_display
,
287 static int vgafb_post_load(void *opaque
, int version_id
)
289 vgafb_invalidate_display(opaque
);
293 static const VMStateDescription vmstate_milkymist_vgafb
= {
294 .name
= "milkymist-vgafb",
296 .minimum_version_id
= 1,
297 .minimum_version_id_old
= 1,
298 .post_load
= vgafb_post_load
,
299 .fields
= (VMStateField
[]) {
300 VMSTATE_UINT32_ARRAY(regs
, MilkymistVgafbState
, R_MAX
),
301 VMSTATE_END_OF_LIST()
305 static Property milkymist_vgafb_properties
[] = {
306 DEFINE_PROP_UINT32("fb_offset", MilkymistVgafbState
, fb_offset
, 0x0),
307 DEFINE_PROP_UINT32("fb_mask", MilkymistVgafbState
, fb_mask
, 0xffffffff),
308 DEFINE_PROP_END_OF_LIST(),
311 static void milkymist_vgafb_class_init(ObjectClass
*klass
, void *data
)
313 DeviceClass
*dc
= DEVICE_CLASS(klass
);
314 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
316 k
->init
= milkymist_vgafb_init
;
317 dc
->reset
= milkymist_vgafb_reset
;
318 dc
->vmsd
= &vmstate_milkymist_vgafb
;
319 dc
->props
= milkymist_vgafb_properties
;
322 static TypeInfo milkymist_vgafb_info
= {
323 .name
= "milkymist-vgafb",
324 .parent
= TYPE_SYS_BUS_DEVICE
,
325 .instance_size
= sizeof(MilkymistVgafbState
),
326 .class_init
= milkymist_vgafb_class_init
,
329 static void milkymist_vgafb_register_types(void)
331 type_register_static(&milkymist_vgafb_info
);
334 type_init(milkymist_vgafb_register_types
)