root / hw / display / milkymist-vgafb.c @ b3b162c3
History | View | Annotate | Download (8.1 kB)
1 | d23948b1 | Michael Walle | |
---|---|---|---|
2 | d23948b1 | Michael Walle | /*
|
3 | d23948b1 | Michael Walle | * QEMU model of the Milkymist VGA framebuffer.
|
4 | d23948b1 | Michael Walle | *
|
5 | a3b6181e | Michael Walle | * Copyright (c) 2010-2012 Michael Walle <michael@walle.cc>
|
6 | d23948b1 | Michael Walle | *
|
7 | d23948b1 | Michael Walle | * This library is free software; you can redistribute it and/or
|
8 | d23948b1 | Michael Walle | * modify it under the terms of the GNU Lesser General Public
|
9 | d23948b1 | Michael Walle | * License as published by the Free Software Foundation; either
|
10 | d23948b1 | Michael Walle | * version 2 of the License, or (at your option) any later version.
|
11 | d23948b1 | Michael Walle | *
|
12 | d23948b1 | Michael Walle | * This library is distributed in the hope that it will be useful,
|
13 | d23948b1 | Michael Walle | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | d23948b1 | Michael Walle | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15 | d23948b1 | Michael Walle | * Lesser General Public License for more details.
|
16 | d23948b1 | Michael Walle | *
|
17 | d23948b1 | Michael Walle | * You should have received a copy of the GNU Lesser General Public
|
18 | d23948b1 | Michael Walle | * License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
19 | d23948b1 | Michael Walle | *
|
20 | d23948b1 | Michael Walle | *
|
21 | d23948b1 | Michael Walle | * Specification available at:
|
22 | d23948b1 | Michael Walle | * http://www.milkymist.org/socdoc/vgafb.pdf
|
23 | d23948b1 | Michael Walle | */
|
24 | d23948b1 | Michael Walle | |
25 | 83c9f4ca | Paolo Bonzini | #include "hw/hw.h" |
26 | 83c9f4ca | Paolo Bonzini | #include "hw/sysbus.h" |
27 | d23948b1 | Michael Walle | #include "trace.h" |
28 | 28ecbaee | Paolo Bonzini | #include "ui/console.h" |
29 | 47b43a1f | Paolo Bonzini | #include "framebuffer.h" |
30 | 28ecbaee | Paolo Bonzini | #include "ui/pixel_ops.h" |
31 | 1de7afc9 | Paolo Bonzini | #include "qemu/error-report.h" |
32 | d23948b1 | Michael Walle | |
33 | d23948b1 | Michael Walle | #define BITS 8 |
34 | 47b43a1f | Paolo Bonzini | #include "milkymist-vgafb_template.h" |
35 | d23948b1 | Michael Walle | #define BITS 15 |
36 | 47b43a1f | Paolo Bonzini | #include "milkymist-vgafb_template.h" |
37 | d23948b1 | Michael Walle | #define BITS 16 |
38 | 47b43a1f | Paolo Bonzini | #include "milkymist-vgafb_template.h" |
39 | d23948b1 | Michael Walle | #define BITS 24 |
40 | 47b43a1f | Paolo Bonzini | #include "milkymist-vgafb_template.h" |
41 | d23948b1 | Michael Walle | #define BITS 32 |
42 | 47b43a1f | Paolo Bonzini | #include "milkymist-vgafb_template.h" |
43 | d23948b1 | Michael Walle | |
44 | d23948b1 | Michael Walle | enum {
|
45 | d23948b1 | Michael Walle | R_CTRL = 0,
|
46 | d23948b1 | Michael Walle | R_HRES, |
47 | d23948b1 | Michael Walle | R_HSYNC_START, |
48 | d23948b1 | Michael Walle | R_HSYNC_END, |
49 | d23948b1 | Michael Walle | R_HSCAN, |
50 | d23948b1 | Michael Walle | R_VRES, |
51 | d23948b1 | Michael Walle | R_VSYNC_START, |
52 | d23948b1 | Michael Walle | R_VSYNC_END, |
53 | d23948b1 | Michael Walle | R_VSCAN, |
54 | d23948b1 | Michael Walle | R_BASEADDRESS, |
55 | d23948b1 | Michael Walle | R_BASEADDRESS_ACT, |
56 | d23948b1 | Michael Walle | R_BURST_COUNT, |
57 | a3b6181e | Michael Walle | R_DDC, |
58 | d23948b1 | Michael Walle | R_SOURCE_CLOCK, |
59 | d23948b1 | Michael Walle | R_MAX |
60 | d23948b1 | Michael Walle | }; |
61 | d23948b1 | Michael Walle | |
62 | d23948b1 | Michael Walle | enum {
|
63 | d23948b1 | Michael Walle | CTRL_RESET = (1<<0), |
64 | d23948b1 | Michael Walle | }; |
65 | d23948b1 | Michael Walle | |
66 | d23948b1 | Michael Walle | struct MilkymistVgafbState {
|
67 | d23948b1 | Michael Walle | SysBusDevice busdev; |
68 | 883abf8d | Michael Walle | MemoryRegion regs_region; |
69 | c78f7137 | Gerd Hoffmann | QemuConsole *con; |
70 | d23948b1 | Michael Walle | |
71 | d23948b1 | Michael Walle | int invalidate;
|
72 | d23948b1 | Michael Walle | uint32_t fb_offset; |
73 | d23948b1 | Michael Walle | uint32_t fb_mask; |
74 | d23948b1 | Michael Walle | |
75 | d23948b1 | Michael Walle | uint32_t regs[R_MAX]; |
76 | d23948b1 | Michael Walle | }; |
77 | d23948b1 | Michael Walle | typedef struct MilkymistVgafbState MilkymistVgafbState; |
78 | d23948b1 | Michael Walle | |
79 | d23948b1 | Michael Walle | static int vgafb_enabled(MilkymistVgafbState *s) |
80 | d23948b1 | Michael Walle | { |
81 | d23948b1 | Michael Walle | return !(s->regs[R_CTRL] & CTRL_RESET);
|
82 | d23948b1 | Michael Walle | } |
83 | d23948b1 | Michael Walle | |
84 | d23948b1 | Michael Walle | static void vgafb_update_display(void *opaque) |
85 | d23948b1 | Michael Walle | { |
86 | d23948b1 | Michael Walle | MilkymistVgafbState *s = opaque; |
87 | c78f7137 | Gerd Hoffmann | DisplaySurface *surface = qemu_console_surface(s->con); |
88 | d23948b1 | Michael Walle | int first = 0; |
89 | d23948b1 | Michael Walle | int last = 0; |
90 | d23948b1 | Michael Walle | drawfn fn; |
91 | d23948b1 | Michael Walle | |
92 | d23948b1 | Michael Walle | if (!vgafb_enabled(s)) {
|
93 | d23948b1 | Michael Walle | return;
|
94 | d23948b1 | Michael Walle | } |
95 | d23948b1 | Michael Walle | |
96 | d23948b1 | Michael Walle | int dest_width = s->regs[R_HRES];
|
97 | d23948b1 | Michael Walle | |
98 | c78f7137 | Gerd Hoffmann | switch (surface_bits_per_pixel(surface)) {
|
99 | d23948b1 | Michael Walle | case 0: |
100 | d23948b1 | Michael Walle | return;
|
101 | d23948b1 | Michael Walle | case 8: |
102 | d23948b1 | Michael Walle | fn = draw_line_8; |
103 | d23948b1 | Michael Walle | break;
|
104 | d23948b1 | Michael Walle | case 15: |
105 | d23948b1 | Michael Walle | fn = draw_line_15; |
106 | d23948b1 | Michael Walle | dest_width *= 2;
|
107 | d23948b1 | Michael Walle | break;
|
108 | d23948b1 | Michael Walle | case 16: |
109 | d23948b1 | Michael Walle | fn = draw_line_16; |
110 | d23948b1 | Michael Walle | dest_width *= 2;
|
111 | d23948b1 | Michael Walle | break;
|
112 | d23948b1 | Michael Walle | case 24: |
113 | d23948b1 | Michael Walle | fn = draw_line_24; |
114 | d23948b1 | Michael Walle | dest_width *= 3;
|
115 | d23948b1 | Michael Walle | break;
|
116 | d23948b1 | Michael Walle | case 32: |
117 | d23948b1 | Michael Walle | fn = draw_line_32; |
118 | d23948b1 | Michael Walle | dest_width *= 4;
|
119 | d23948b1 | Michael Walle | break;
|
120 | d23948b1 | Michael Walle | default:
|
121 | d23948b1 | Michael Walle | hw_error("milkymist_vgafb: bad color depth\n");
|
122 | d23948b1 | Michael Walle | break;
|
123 | d23948b1 | Michael Walle | } |
124 | d23948b1 | Michael Walle | |
125 | c78f7137 | Gerd Hoffmann | framebuffer_update_display(surface, sysbus_address_space(&s->busdev), |
126 | d23948b1 | Michael Walle | s->regs[R_BASEADDRESS] + s->fb_offset, |
127 | d23948b1 | Michael Walle | s->regs[R_HRES], |
128 | d23948b1 | Michael Walle | s->regs[R_VRES], |
129 | d23948b1 | Michael Walle | s->regs[R_HRES] * 2,
|
130 | d23948b1 | Michael Walle | dest_width, |
131 | d23948b1 | Michael Walle | 0,
|
132 | d23948b1 | Michael Walle | s->invalidate, |
133 | d23948b1 | Michael Walle | fn, |
134 | d23948b1 | Michael Walle | NULL,
|
135 | d23948b1 | Michael Walle | &first, &last); |
136 | d23948b1 | Michael Walle | |
137 | d23948b1 | Michael Walle | if (first >= 0) { |
138 | c78f7137 | Gerd Hoffmann | dpy_gfx_update(s->con, 0, first, s->regs[R_HRES], last - first + 1); |
139 | d23948b1 | Michael Walle | } |
140 | d23948b1 | Michael Walle | s->invalidate = 0;
|
141 | d23948b1 | Michael Walle | } |
142 | d23948b1 | Michael Walle | |
143 | d23948b1 | Michael Walle | static void vgafb_invalidate_display(void *opaque) |
144 | d23948b1 | Michael Walle | { |
145 | d23948b1 | Michael Walle | MilkymistVgafbState *s = opaque; |
146 | d23948b1 | Michael Walle | s->invalidate = 1;
|
147 | d23948b1 | Michael Walle | } |
148 | d23948b1 | Michael Walle | |
149 | d23948b1 | Michael Walle | static void vgafb_resize(MilkymistVgafbState *s) |
150 | d23948b1 | Michael Walle | { |
151 | d23948b1 | Michael Walle | if (!vgafb_enabled(s)) {
|
152 | d23948b1 | Michael Walle | return;
|
153 | d23948b1 | Michael Walle | } |
154 | d23948b1 | Michael Walle | |
155 | c78f7137 | Gerd Hoffmann | qemu_console_resize(s->con, s->regs[R_HRES], s->regs[R_VRES]); |
156 | d23948b1 | Michael Walle | s->invalidate = 1;
|
157 | d23948b1 | Michael Walle | } |
158 | d23948b1 | Michael Walle | |
159 | a8170e5e | Avi Kivity | static uint64_t vgafb_read(void *opaque, hwaddr addr, |
160 | 883abf8d | Michael Walle | unsigned size)
|
161 | d23948b1 | Michael Walle | { |
162 | d23948b1 | Michael Walle | MilkymistVgafbState *s = opaque; |
163 | d23948b1 | Michael Walle | uint32_t r = 0;
|
164 | d23948b1 | Michael Walle | |
165 | d23948b1 | Michael Walle | addr >>= 2;
|
166 | d23948b1 | Michael Walle | switch (addr) {
|
167 | d23948b1 | Michael Walle | case R_CTRL:
|
168 | d23948b1 | Michael Walle | case R_HRES:
|
169 | d23948b1 | Michael Walle | case R_HSYNC_START:
|
170 | d23948b1 | Michael Walle | case R_HSYNC_END:
|
171 | d23948b1 | Michael Walle | case R_HSCAN:
|
172 | d23948b1 | Michael Walle | case R_VRES:
|
173 | d23948b1 | Michael Walle | case R_VSYNC_START:
|
174 | d23948b1 | Michael Walle | case R_VSYNC_END:
|
175 | d23948b1 | Michael Walle | case R_VSCAN:
|
176 | d23948b1 | Michael Walle | case R_BASEADDRESS:
|
177 | d23948b1 | Michael Walle | case R_BURST_COUNT:
|
178 | a3b6181e | Michael Walle | case R_DDC:
|
179 | d23948b1 | Michael Walle | case R_SOURCE_CLOCK:
|
180 | d23948b1 | Michael Walle | r = s->regs[addr]; |
181 | d23948b1 | Michael Walle | break;
|
182 | d23948b1 | Michael Walle | case R_BASEADDRESS_ACT:
|
183 | d23948b1 | Michael Walle | r = s->regs[R_BASEADDRESS]; |
184 | d23948b1 | Michael Walle | break;
|
185 | d23948b1 | Michael Walle | |
186 | d23948b1 | Michael Walle | default:
|
187 | d23948b1 | Michael Walle | error_report("milkymist_vgafb: read access to unknown register 0x"
|
188 | d23948b1 | Michael Walle | TARGET_FMT_plx, addr << 2);
|
189 | d23948b1 | Michael Walle | break;
|
190 | d23948b1 | Michael Walle | } |
191 | d23948b1 | Michael Walle | |
192 | d23948b1 | Michael Walle | trace_milkymist_vgafb_memory_read(addr << 2, r);
|
193 | d23948b1 | Michael Walle | |
194 | d23948b1 | Michael Walle | return r;
|
195 | d23948b1 | Michael Walle | } |
196 | d23948b1 | Michael Walle | |
197 | a8170e5e | Avi Kivity | static void vgafb_write(void *opaque, hwaddr addr, uint64_t value, |
198 | 883abf8d | Michael Walle | unsigned size)
|
199 | d23948b1 | Michael Walle | { |
200 | d23948b1 | Michael Walle | MilkymistVgafbState *s = opaque; |
201 | d23948b1 | Michael Walle | |
202 | d23948b1 | Michael Walle | trace_milkymist_vgafb_memory_write(addr, value); |
203 | d23948b1 | Michael Walle | |
204 | d23948b1 | Michael Walle | addr >>= 2;
|
205 | d23948b1 | Michael Walle | switch (addr) {
|
206 | d23948b1 | Michael Walle | case R_CTRL:
|
207 | c07050dd | Michael Walle | s->regs[addr] = value; |
208 | c07050dd | Michael Walle | vgafb_resize(s); |
209 | c07050dd | Michael Walle | break;
|
210 | d23948b1 | Michael Walle | case R_HSYNC_START:
|
211 | d23948b1 | Michael Walle | case R_HSYNC_END:
|
212 | d23948b1 | Michael Walle | case R_HSCAN:
|
213 | d23948b1 | Michael Walle | case R_VSYNC_START:
|
214 | d23948b1 | Michael Walle | case R_VSYNC_END:
|
215 | d23948b1 | Michael Walle | case R_VSCAN:
|
216 | d23948b1 | Michael Walle | case R_BURST_COUNT:
|
217 | a3b6181e | Michael Walle | case R_DDC:
|
218 | d23948b1 | Michael Walle | case R_SOURCE_CLOCK:
|
219 | d23948b1 | Michael Walle | s->regs[addr] = value; |
220 | d23948b1 | Michael Walle | break;
|
221 | d23948b1 | Michael Walle | case R_BASEADDRESS:
|
222 | d23948b1 | Michael Walle | if (value & 0x1f) { |
223 | d23948b1 | Michael Walle | error_report("milkymist_vgafb: framebuffer base address have to "
|
224 | d23948b1 | Michael Walle | "be 32 byte aligned");
|
225 | d23948b1 | Michael Walle | break;
|
226 | d23948b1 | Michael Walle | } |
227 | d23948b1 | Michael Walle | s->regs[addr] = value & s->fb_mask; |
228 | d23948b1 | Michael Walle | s->invalidate = 1;
|
229 | d23948b1 | Michael Walle | break;
|
230 | d23948b1 | Michael Walle | case R_HRES:
|
231 | d23948b1 | Michael Walle | case R_VRES:
|
232 | d23948b1 | Michael Walle | s->regs[addr] = value; |
233 | d23948b1 | Michael Walle | vgafb_resize(s); |
234 | d23948b1 | Michael Walle | break;
|
235 | d23948b1 | Michael Walle | case R_BASEADDRESS_ACT:
|
236 | d23948b1 | Michael Walle | error_report("milkymist_vgafb: write to read-only register 0x"
|
237 | d23948b1 | Michael Walle | TARGET_FMT_plx, addr << 2);
|
238 | d23948b1 | Michael Walle | break;
|
239 | d23948b1 | Michael Walle | |
240 | d23948b1 | Michael Walle | default:
|
241 | d23948b1 | Michael Walle | error_report("milkymist_vgafb: write access to unknown register 0x"
|
242 | d23948b1 | Michael Walle | TARGET_FMT_plx, addr << 2);
|
243 | d23948b1 | Michael Walle | break;
|
244 | d23948b1 | Michael Walle | } |
245 | d23948b1 | Michael Walle | } |
246 | d23948b1 | Michael Walle | |
247 | 883abf8d | Michael Walle | static const MemoryRegionOps vgafb_mmio_ops = { |
248 | 883abf8d | Michael Walle | .read = vgafb_read, |
249 | 883abf8d | Michael Walle | .write = vgafb_write, |
250 | 883abf8d | Michael Walle | .valid = { |
251 | 883abf8d | Michael Walle | .min_access_size = 4,
|
252 | 883abf8d | Michael Walle | .max_access_size = 4,
|
253 | 883abf8d | Michael Walle | }, |
254 | 883abf8d | Michael Walle | .endianness = DEVICE_NATIVE_ENDIAN, |
255 | d23948b1 | Michael Walle | }; |
256 | d23948b1 | Michael Walle | |
257 | d23948b1 | Michael Walle | static void milkymist_vgafb_reset(DeviceState *d) |
258 | d23948b1 | Michael Walle | { |
259 | d23948b1 | Michael Walle | MilkymistVgafbState *s = container_of(d, MilkymistVgafbState, busdev.qdev); |
260 | d23948b1 | Michael Walle | int i;
|
261 | d23948b1 | Michael Walle | |
262 | d23948b1 | Michael Walle | for (i = 0; i < R_MAX; i++) { |
263 | d23948b1 | Michael Walle | s->regs[i] = 0;
|
264 | d23948b1 | Michael Walle | } |
265 | d23948b1 | Michael Walle | |
266 | d23948b1 | Michael Walle | /* defaults */
|
267 | d23948b1 | Michael Walle | s->regs[R_CTRL] = CTRL_RESET; |
268 | d23948b1 | Michael Walle | s->regs[R_HRES] = 640;
|
269 | d23948b1 | Michael Walle | s->regs[R_VRES] = 480;
|
270 | d23948b1 | Michael Walle | s->regs[R_BASEADDRESS] = 0;
|
271 | d23948b1 | Michael Walle | } |
272 | d23948b1 | Michael Walle | |
273 | 380cd056 | Gerd Hoffmann | static const GraphicHwOps vgafb_ops = { |
274 | 380cd056 | Gerd Hoffmann | .invalidate = vgafb_invalidate_display, |
275 | 380cd056 | Gerd Hoffmann | .gfx_update = vgafb_update_display, |
276 | 380cd056 | Gerd Hoffmann | }; |
277 | 380cd056 | Gerd Hoffmann | |
278 | d23948b1 | Michael Walle | static int milkymist_vgafb_init(SysBusDevice *dev) |
279 | d23948b1 | Michael Walle | { |
280 | d23948b1 | Michael Walle | MilkymistVgafbState *s = FROM_SYSBUS(typeof(*s), dev); |
281 | d23948b1 | Michael Walle | |
282 | 3eadad55 | Paolo Bonzini | memory_region_init_io(&s->regs_region, OBJECT(s), &vgafb_mmio_ops, s, |
283 | 883abf8d | Michael Walle | "milkymist-vgafb", R_MAX * 4); |
284 | 750ecd44 | Avi Kivity | sysbus_init_mmio(dev, &s->regs_region); |
285 | d23948b1 | Michael Walle | |
286 | aa2beaa1 | Gerd Hoffmann | s->con = graphic_console_init(DEVICE(dev), &vgafb_ops, s); |
287 | d23948b1 | Michael Walle | |
288 | d23948b1 | Michael Walle | return 0; |
289 | d23948b1 | Michael Walle | } |
290 | d23948b1 | Michael Walle | |
291 | d23948b1 | Michael Walle | static int vgafb_post_load(void *opaque, int version_id) |
292 | d23948b1 | Michael Walle | { |
293 | d23948b1 | Michael Walle | vgafb_invalidate_display(opaque); |
294 | d23948b1 | Michael Walle | return 0; |
295 | d23948b1 | Michael Walle | } |
296 | d23948b1 | Michael Walle | |
297 | d23948b1 | Michael Walle | static const VMStateDescription vmstate_milkymist_vgafb = { |
298 | d23948b1 | Michael Walle | .name = "milkymist-vgafb",
|
299 | d23948b1 | Michael Walle | .version_id = 1,
|
300 | d23948b1 | Michael Walle | .minimum_version_id = 1,
|
301 | d23948b1 | Michael Walle | .minimum_version_id_old = 1,
|
302 | d23948b1 | Michael Walle | .post_load = vgafb_post_load, |
303 | d23948b1 | Michael Walle | .fields = (VMStateField[]) { |
304 | d23948b1 | Michael Walle | VMSTATE_UINT32_ARRAY(regs, MilkymistVgafbState, R_MAX), |
305 | d23948b1 | Michael Walle | VMSTATE_END_OF_LIST() |
306 | d23948b1 | Michael Walle | } |
307 | d23948b1 | Michael Walle | }; |
308 | d23948b1 | Michael Walle | |
309 | 999e12bb | Anthony Liguori | static Property milkymist_vgafb_properties[] = {
|
310 | 999e12bb | Anthony Liguori | DEFINE_PROP_UINT32("fb_offset", MilkymistVgafbState, fb_offset, 0x0), |
311 | 999e12bb | Anthony Liguori | DEFINE_PROP_UINT32("fb_mask", MilkymistVgafbState, fb_mask, 0xffffffff), |
312 | 999e12bb | Anthony Liguori | DEFINE_PROP_END_OF_LIST(), |
313 | 999e12bb | Anthony Liguori | }; |
314 | 999e12bb | Anthony Liguori | |
315 | 999e12bb | Anthony Liguori | static void milkymist_vgafb_class_init(ObjectClass *klass, void *data) |
316 | 999e12bb | Anthony Liguori | { |
317 | 39bffca2 | Anthony Liguori | DeviceClass *dc = DEVICE_CLASS(klass); |
318 | 999e12bb | Anthony Liguori | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
319 | 999e12bb | Anthony Liguori | |
320 | 999e12bb | Anthony Liguori | k->init = milkymist_vgafb_init; |
321 | 39bffca2 | Anthony Liguori | dc->reset = milkymist_vgafb_reset; |
322 | 39bffca2 | Anthony Liguori | dc->vmsd = &vmstate_milkymist_vgafb; |
323 | 39bffca2 | Anthony Liguori | dc->props = milkymist_vgafb_properties; |
324 | 999e12bb | Anthony Liguori | } |
325 | 999e12bb | Anthony Liguori | |
326 | 8c43a6f0 | Andreas Färber | static const TypeInfo milkymist_vgafb_info = { |
327 | 39bffca2 | Anthony Liguori | .name = "milkymist-vgafb",
|
328 | 39bffca2 | Anthony Liguori | .parent = TYPE_SYS_BUS_DEVICE, |
329 | 39bffca2 | Anthony Liguori | .instance_size = sizeof(MilkymistVgafbState),
|
330 | 39bffca2 | Anthony Liguori | .class_init = milkymist_vgafb_class_init, |
331 | d23948b1 | Michael Walle | }; |
332 | d23948b1 | Michael Walle | |
333 | 83f7d43a | Andreas Färber | static void milkymist_vgafb_register_types(void) |
334 | d23948b1 | Michael Walle | { |
335 | 39bffca2 | Anthony Liguori | type_register_static(&milkymist_vgafb_info); |
336 | d23948b1 | Michael Walle | } |
337 | d23948b1 | Michael Walle | |
338 | 83f7d43a | Andreas Färber | type_init(milkymist_vgafb_register_types) |