pc: acpi-build: create PCI0._CRS dynamically
[qemu/ar7.git] / hw / display / milkymist-vgafb.c
blob9b35e76ff1c8ff76a99368afbaaf66967dbf4f9b
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://www.milkymist.org/socdoc/vgafb.pdf
25 #include "hw/hw.h"
26 #include "hw/sysbus.h"
27 #include "trace.h"
28 #include "ui/console.h"
29 #include "framebuffer.h"
30 #include "ui/pixel_ops.h"
31 #include "qemu/error-report.h"
33 #define BITS 8
34 #include "milkymist-vgafb_template.h"
35 #define BITS 15
36 #include "milkymist-vgafb_template.h"
37 #define BITS 16
38 #include "milkymist-vgafb_template.h"
39 #define BITS 24
40 #include "milkymist-vgafb_template.h"
41 #define BITS 32
42 #include "milkymist-vgafb_template.h"
44 enum {
45 R_CTRL = 0,
46 R_HRES,
47 R_HSYNC_START,
48 R_HSYNC_END,
49 R_HSCAN,
50 R_VRES,
51 R_VSYNC_START,
52 R_VSYNC_END,
53 R_VSCAN,
54 R_BASEADDRESS,
55 R_BASEADDRESS_ACT,
56 R_BURST_COUNT,
57 R_DDC,
58 R_SOURCE_CLOCK,
59 R_MAX
62 enum {
63 CTRL_RESET = (1<<0),
66 #define TYPE_MILKYMIST_VGAFB "milkymist-vgafb"
67 #define MILKYMIST_VGAFB(obj) \
68 OBJECT_CHECK(MilkymistVgafbState, (obj), TYPE_MILKYMIST_VGAFB)
70 struct MilkymistVgafbState {
71 SysBusDevice parent_obj;
73 MemoryRegion regs_region;
74 QemuConsole *con;
76 int invalidate;
77 uint32_t fb_offset;
78 uint32_t fb_mask;
80 uint32_t regs[R_MAX];
82 typedef struct MilkymistVgafbState MilkymistVgafbState;
84 static int vgafb_enabled(MilkymistVgafbState *s)
86 return !(s->regs[R_CTRL] & CTRL_RESET);
89 static void vgafb_update_display(void *opaque)
91 MilkymistVgafbState *s = opaque;
92 SysBusDevice *sbd;
93 DisplaySurface *surface = qemu_console_surface(s->con);
94 int first = 0;
95 int last = 0;
96 drawfn fn;
98 if (!vgafb_enabled(s)) {
99 return;
102 sbd = SYS_BUS_DEVICE(s);
103 int dest_width = s->regs[R_HRES];
105 switch (surface_bits_per_pixel(surface)) {
106 case 0:
107 return;
108 case 8:
109 fn = draw_line_8;
110 break;
111 case 15:
112 fn = draw_line_15;
113 dest_width *= 2;
114 break;
115 case 16:
116 fn = draw_line_16;
117 dest_width *= 2;
118 break;
119 case 24:
120 fn = draw_line_24;
121 dest_width *= 3;
122 break;
123 case 32:
124 fn = draw_line_32;
125 dest_width *= 4;
126 break;
127 default:
128 hw_error("milkymist_vgafb: bad color depth\n");
129 break;
132 framebuffer_update_display(surface, sysbus_address_space(sbd),
133 s->regs[R_BASEADDRESS] + s->fb_offset,
134 s->regs[R_HRES],
135 s->regs[R_VRES],
136 s->regs[R_HRES] * 2,
137 dest_width,
139 s->invalidate,
141 NULL,
142 &first, &last);
144 if (first >= 0) {
145 dpy_gfx_update(s->con, 0, first, s->regs[R_HRES], last - first + 1);
147 s->invalidate = 0;
150 static void vgafb_invalidate_display(void *opaque)
152 MilkymistVgafbState *s = opaque;
153 s->invalidate = 1;
156 static void vgafb_resize(MilkymistVgafbState *s)
158 if (!vgafb_enabled(s)) {
159 return;
162 qemu_console_resize(s->con, s->regs[R_HRES], s->regs[R_VRES]);
163 s->invalidate = 1;
166 static uint64_t vgafb_read(void *opaque, hwaddr addr,
167 unsigned size)
169 MilkymistVgafbState *s = opaque;
170 uint32_t r = 0;
172 addr >>= 2;
173 switch (addr) {
174 case R_CTRL:
175 case R_HRES:
176 case R_HSYNC_START:
177 case R_HSYNC_END:
178 case R_HSCAN:
179 case R_VRES:
180 case R_VSYNC_START:
181 case R_VSYNC_END:
182 case R_VSCAN:
183 case R_BASEADDRESS:
184 case R_BURST_COUNT:
185 case R_DDC:
186 case R_SOURCE_CLOCK:
187 r = s->regs[addr];
188 break;
189 case R_BASEADDRESS_ACT:
190 r = s->regs[R_BASEADDRESS];
191 break;
193 default:
194 error_report("milkymist_vgafb: read access to unknown register 0x"
195 TARGET_FMT_plx, addr << 2);
196 break;
199 trace_milkymist_vgafb_memory_read(addr << 2, r);
201 return r;
204 static void vgafb_write(void *opaque, hwaddr addr, uint64_t value,
205 unsigned size)
207 MilkymistVgafbState *s = opaque;
209 trace_milkymist_vgafb_memory_write(addr, value);
211 addr >>= 2;
212 switch (addr) {
213 case R_CTRL:
214 s->regs[addr] = value;
215 vgafb_resize(s);
216 break;
217 case R_HSYNC_START:
218 case R_HSYNC_END:
219 case R_HSCAN:
220 case R_VSYNC_START:
221 case R_VSYNC_END:
222 case R_VSCAN:
223 case R_BURST_COUNT:
224 case R_DDC:
225 case R_SOURCE_CLOCK:
226 s->regs[addr] = value;
227 break;
228 case R_BASEADDRESS:
229 if (value & 0x1f) {
230 error_report("milkymist_vgafb: framebuffer base address have to "
231 "be 32 byte aligned");
232 break;
234 s->regs[addr] = value & s->fb_mask;
235 s->invalidate = 1;
236 break;
237 case R_HRES:
238 case R_VRES:
239 s->regs[addr] = value;
240 vgafb_resize(s);
241 break;
242 case R_BASEADDRESS_ACT:
243 error_report("milkymist_vgafb: write to read-only register 0x"
244 TARGET_FMT_plx, addr << 2);
245 break;
247 default:
248 error_report("milkymist_vgafb: write access to unknown register 0x"
249 TARGET_FMT_plx, addr << 2);
250 break;
254 static const MemoryRegionOps vgafb_mmio_ops = {
255 .read = vgafb_read,
256 .write = vgafb_write,
257 .valid = {
258 .min_access_size = 4,
259 .max_access_size = 4,
261 .endianness = DEVICE_NATIVE_ENDIAN,
264 static void milkymist_vgafb_reset(DeviceState *d)
266 MilkymistVgafbState *s = MILKYMIST_VGAFB(d);
267 int i;
269 for (i = 0; i < R_MAX; i++) {
270 s->regs[i] = 0;
273 /* defaults */
274 s->regs[R_CTRL] = CTRL_RESET;
275 s->regs[R_HRES] = 640;
276 s->regs[R_VRES] = 480;
277 s->regs[R_BASEADDRESS] = 0;
280 static const GraphicHwOps vgafb_ops = {
281 .invalidate = vgafb_invalidate_display,
282 .gfx_update = vgafb_update_display,
285 static int milkymist_vgafb_init(SysBusDevice *dev)
287 MilkymistVgafbState *s = MILKYMIST_VGAFB(dev);
289 memory_region_init_io(&s->regs_region, OBJECT(s), &vgafb_mmio_ops, s,
290 "milkymist-vgafb", R_MAX * 4);
291 sysbus_init_mmio(dev, &s->regs_region);
293 s->con = graphic_console_init(DEVICE(dev), 0, &vgafb_ops, s);
295 return 0;
298 static int vgafb_post_load(void *opaque, int version_id)
300 vgafb_invalidate_display(opaque);
301 return 0;
304 static const VMStateDescription vmstate_milkymist_vgafb = {
305 .name = "milkymist-vgafb",
306 .version_id = 1,
307 .minimum_version_id = 1,
308 .post_load = vgafb_post_load,
309 .fields = (VMStateField[]) {
310 VMSTATE_UINT32_ARRAY(regs, MilkymistVgafbState, R_MAX),
311 VMSTATE_END_OF_LIST()
315 static Property milkymist_vgafb_properties[] = {
316 DEFINE_PROP_UINT32("fb_offset", MilkymistVgafbState, fb_offset, 0x0),
317 DEFINE_PROP_UINT32("fb_mask", MilkymistVgafbState, fb_mask, 0xffffffff),
318 DEFINE_PROP_END_OF_LIST(),
321 static void milkymist_vgafb_class_init(ObjectClass *klass, void *data)
323 DeviceClass *dc = DEVICE_CLASS(klass);
324 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
326 k->init = milkymist_vgafb_init;
327 dc->reset = milkymist_vgafb_reset;
328 dc->vmsd = &vmstate_milkymist_vgafb;
329 dc->props = milkymist_vgafb_properties;
332 static const TypeInfo milkymist_vgafb_info = {
333 .name = TYPE_MILKYMIST_VGAFB,
334 .parent = TYPE_SYS_BUS_DEVICE,
335 .instance_size = sizeof(MilkymistVgafbState),
336 .class_init = milkymist_vgafb_class_init,
339 static void milkymist_vgafb_register_types(void)
341 type_register_static(&milkymist_vgafb_info);
344 type_init(milkymist_vgafb_register_types)