kvmclock: use the updated system_timer_msr
[qemu/ar7.git] / hw / display / milkymist-vgafb.c
blob177fdac7d87992a38ef041ec3ec19f041547c20f
2 /*
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"
26 #include "hw/hw.h"
27 #include "hw/sysbus.h"
28 #include "trace.h"
29 #include "ui/console.h"
30 #include "framebuffer.h"
31 #include "ui/pixel_ops.h"
32 #include "qemu/error-report.h"
34 #define BITS 8
35 #include "milkymist-vgafb_template.h"
36 #define BITS 15
37 #include "milkymist-vgafb_template.h"
38 #define BITS 16
39 #include "milkymist-vgafb_template.h"
40 #define BITS 24
41 #include "milkymist-vgafb_template.h"
42 #define BITS 32
43 #include "milkymist-vgafb_template.h"
45 enum {
46 R_CTRL = 0,
47 R_HRES,
48 R_HSYNC_START,
49 R_HSYNC_END,
50 R_HSCAN,
51 R_VRES,
52 R_VSYNC_START,
53 R_VSYNC_END,
54 R_VSCAN,
55 R_BASEADDRESS,
56 R_BASEADDRESS_ACT,
57 R_BURST_COUNT,
58 R_DDC,
59 R_SOURCE_CLOCK,
60 R_MAX
63 enum {
64 CTRL_RESET = (1<<0),
67 #define TYPE_MILKYMIST_VGAFB "milkymist-vgafb"
68 #define MILKYMIST_VGAFB(obj) \
69 OBJECT_CHECK(MilkymistVgafbState, (obj), TYPE_MILKYMIST_VGAFB)
71 struct MilkymistVgafbState {
72 SysBusDevice parent_obj;
74 MemoryRegion regs_region;
75 MemoryRegionSection fbsection;
76 QemuConsole *con;
78 int invalidate;
79 uint32_t fb_offset;
80 uint32_t fb_mask;
82 uint32_t regs[R_MAX];
84 typedef struct MilkymistVgafbState MilkymistVgafbState;
86 static int vgafb_enabled(MilkymistVgafbState *s)
88 return !(s->regs[R_CTRL] & CTRL_RESET);
91 static void vgafb_update_display(void *opaque)
93 MilkymistVgafbState *s = opaque;
94 SysBusDevice *sbd;
95 DisplaySurface *surface = qemu_console_surface(s->con);
96 int src_width;
97 int first = 0;
98 int last = 0;
99 drawfn fn;
101 if (!vgafb_enabled(s)) {
102 return;
105 sbd = SYS_BUS_DEVICE(s);
106 int dest_width = s->regs[R_HRES];
108 switch (surface_bits_per_pixel(surface)) {
109 case 0:
110 return;
111 case 8:
112 fn = draw_line_8;
113 break;
114 case 15:
115 fn = draw_line_15;
116 dest_width *= 2;
117 break;
118 case 16:
119 fn = draw_line_16;
120 dest_width *= 2;
121 break;
122 case 24:
123 fn = draw_line_24;
124 dest_width *= 3;
125 break;
126 case 32:
127 fn = draw_line_32;
128 dest_width *= 4;
129 break;
130 default:
131 hw_error("milkymist_vgafb: bad color depth\n");
132 break;
135 src_width = s->regs[R_HRES] * 2;
136 if (s->invalidate) {
137 framebuffer_update_memory_section(&s->fbsection,
138 sysbus_address_space(sbd),
139 s->regs[R_BASEADDRESS] + s->fb_offset,
140 s->regs[R_VRES], src_width);
143 framebuffer_update_display(surface, &s->fbsection,
144 s->regs[R_HRES],
145 s->regs[R_VRES],
146 src_width,
147 dest_width,
149 s->invalidate,
151 NULL,
152 &first, &last);
154 if (first >= 0) {
155 dpy_gfx_update(s->con, 0, first, s->regs[R_HRES], last - first + 1);
157 s->invalidate = 0;
160 static void vgafb_invalidate_display(void *opaque)
162 MilkymistVgafbState *s = opaque;
163 s->invalidate = 1;
166 static void vgafb_resize(MilkymistVgafbState *s)
168 if (!vgafb_enabled(s)) {
169 return;
172 qemu_console_resize(s->con, s->regs[R_HRES], s->regs[R_VRES]);
173 s->invalidate = 1;
176 static uint64_t vgafb_read(void *opaque, hwaddr addr,
177 unsigned size)
179 MilkymistVgafbState *s = opaque;
180 uint32_t r = 0;
182 addr >>= 2;
183 switch (addr) {
184 case R_CTRL:
185 case R_HRES:
186 case R_HSYNC_START:
187 case R_HSYNC_END:
188 case R_HSCAN:
189 case R_VRES:
190 case R_VSYNC_START:
191 case R_VSYNC_END:
192 case R_VSCAN:
193 case R_BASEADDRESS:
194 case R_BURST_COUNT:
195 case R_DDC:
196 case R_SOURCE_CLOCK:
197 r = s->regs[addr];
198 break;
199 case R_BASEADDRESS_ACT:
200 r = s->regs[R_BASEADDRESS];
201 break;
203 default:
204 error_report("milkymist_vgafb: read access to unknown register 0x"
205 TARGET_FMT_plx, addr << 2);
206 break;
209 trace_milkymist_vgafb_memory_read(addr << 2, r);
211 return r;
214 static void vgafb_write(void *opaque, hwaddr addr, uint64_t value,
215 unsigned size)
217 MilkymistVgafbState *s = opaque;
219 trace_milkymist_vgafb_memory_write(addr, value);
221 addr >>= 2;
222 switch (addr) {
223 case R_CTRL:
224 s->regs[addr] = value;
225 vgafb_resize(s);
226 break;
227 case R_HSYNC_START:
228 case R_HSYNC_END:
229 case R_HSCAN:
230 case R_VSYNC_START:
231 case R_VSYNC_END:
232 case R_VSCAN:
233 case R_BURST_COUNT:
234 case R_DDC:
235 case R_SOURCE_CLOCK:
236 s->regs[addr] = value;
237 break;
238 case R_BASEADDRESS:
239 if (value & 0x1f) {
240 error_report("milkymist_vgafb: framebuffer base address have to "
241 "be 32 byte aligned");
242 break;
244 s->regs[addr] = value & s->fb_mask;
245 s->invalidate = 1;
246 break;
247 case R_HRES:
248 case R_VRES:
249 s->regs[addr] = value;
250 vgafb_resize(s);
251 break;
252 case R_BASEADDRESS_ACT:
253 error_report("milkymist_vgafb: write to read-only register 0x"
254 TARGET_FMT_plx, addr << 2);
255 break;
257 default:
258 error_report("milkymist_vgafb: write access to unknown register 0x"
259 TARGET_FMT_plx, addr << 2);
260 break;
264 static const MemoryRegionOps vgafb_mmio_ops = {
265 .read = vgafb_read,
266 .write = vgafb_write,
267 .valid = {
268 .min_access_size = 4,
269 .max_access_size = 4,
271 .endianness = DEVICE_NATIVE_ENDIAN,
274 static void milkymist_vgafb_reset(DeviceState *d)
276 MilkymistVgafbState *s = MILKYMIST_VGAFB(d);
277 int i;
279 for (i = 0; i < R_MAX; i++) {
280 s->regs[i] = 0;
283 /* defaults */
284 s->regs[R_CTRL] = CTRL_RESET;
285 s->regs[R_HRES] = 640;
286 s->regs[R_VRES] = 480;
287 s->regs[R_BASEADDRESS] = 0;
290 static const GraphicHwOps vgafb_ops = {
291 .invalidate = vgafb_invalidate_display,
292 .gfx_update = vgafb_update_display,
295 static void milkymist_vgafb_init(Object *obj)
297 MilkymistVgafbState *s = MILKYMIST_VGAFB(obj);
298 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
300 memory_region_init_io(&s->regs_region, OBJECT(s), &vgafb_mmio_ops, s,
301 "milkymist-vgafb", R_MAX * 4);
302 sysbus_init_mmio(dev, &s->regs_region);
305 static void milkymist_vgafb_realize(DeviceState *dev, Error **errp)
307 MilkymistVgafbState *s = MILKYMIST_VGAFB(dev);
309 s->con = graphic_console_init(dev, 0, &vgafb_ops, s);
312 static int vgafb_post_load(void *opaque, int version_id)
314 vgafb_invalidate_display(opaque);
315 return 0;
318 static const VMStateDescription vmstate_milkymist_vgafb = {
319 .name = "milkymist-vgafb",
320 .version_id = 1,
321 .minimum_version_id = 1,
322 .post_load = vgafb_post_load,
323 .fields = (VMStateField[]) {
324 VMSTATE_UINT32_ARRAY(regs, MilkymistVgafbState, R_MAX),
325 VMSTATE_END_OF_LIST()
329 static Property milkymist_vgafb_properties[] = {
330 DEFINE_PROP_UINT32("fb_offset", MilkymistVgafbState, fb_offset, 0x0),
331 DEFINE_PROP_UINT32("fb_mask", MilkymistVgafbState, fb_mask, 0xffffffff),
332 DEFINE_PROP_END_OF_LIST(),
335 static void milkymist_vgafb_class_init(ObjectClass *klass, void *data)
337 DeviceClass *dc = DEVICE_CLASS(klass);
339 dc->reset = milkymist_vgafb_reset;
340 dc->vmsd = &vmstate_milkymist_vgafb;
341 dc->props = milkymist_vgafb_properties;
342 dc->realize = milkymist_vgafb_realize;
345 static const TypeInfo milkymist_vgafb_info = {
346 .name = TYPE_MILKYMIST_VGAFB,
347 .parent = TYPE_SYS_BUS_DEVICE,
348 .instance_size = sizeof(MilkymistVgafbState),
349 .instance_init = milkymist_vgafb_init,
350 .class_init = milkymist_vgafb_class_init,
353 static void milkymist_vgafb_register_types(void)
355 type_register_static(&milkymist_vgafb_info);
358 type_init(milkymist_vgafb_register_types)