virtio-pci: drop unused wmb macro
[qemu/qmp-unstable.git] / hw / display / ssd0323.c
blobc3231c61167c2d09861a0fff4ea87c324e094486
1 /*
2 * SSD0323 OLED controller with OSRAM Pictiva 128x64 display.
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
8 */
10 /* The controller can support a variety of different displays, but we only
11 implement one. Most of the commends relating to brightness and geometry
12 setup are ignored. */
13 #include "hw/ssi.h"
14 #include "ui/console.h"
16 //#define DEBUG_SSD0323 1
18 #ifdef DEBUG_SSD0323
19 #define DPRINTF(fmt, ...) \
20 do { printf("ssd0323: " fmt , ## __VA_ARGS__); } while (0)
21 #define BADF(fmt, ...) \
22 do { \
23 fprintf(stderr, "ssd0323: error: " fmt , ## __VA_ARGS__); abort(); \
24 } while (0)
25 #else
26 #define DPRINTF(fmt, ...) do {} while(0)
27 #define BADF(fmt, ...) \
28 do { fprintf(stderr, "ssd0323: error: " fmt , ## __VA_ARGS__);} while (0)
29 #endif
31 /* Scaling factor for pixels. */
32 #define MAGNIFY 4
34 #define REMAP_SWAP_COLUMN 0x01
35 #define REMAP_SWAP_NYBBLE 0x02
36 #define REMAP_VERTICAL 0x04
37 #define REMAP_SWAP_COM 0x10
38 #define REMAP_SPLIT_COM 0x40
40 enum ssd0323_mode
42 SSD0323_CMD,
43 SSD0323_DATA
46 typedef struct {
47 SSISlave ssidev;
48 QemuConsole *con;
50 int cmd_len;
51 int cmd;
52 int cmd_data[8];
53 int row;
54 int row_start;
55 int row_end;
56 int col;
57 int col_start;
58 int col_end;
59 int redraw;
60 int remap;
61 enum ssd0323_mode mode;
62 uint8_t framebuffer[128 * 80 / 2];
63 } ssd0323_state;
65 static uint32_t ssd0323_transfer(SSISlave *dev, uint32_t data)
67 ssd0323_state *s = FROM_SSI_SLAVE(ssd0323_state, dev);
69 switch (s->mode) {
70 case SSD0323_DATA:
71 DPRINTF("data 0x%02x\n", data);
72 s->framebuffer[s->col + s->row * 64] = data;
73 if (s->remap & REMAP_VERTICAL) {
74 s->row++;
75 if (s->row > s->row_end) {
76 s->row = s->row_start;
77 s->col++;
79 if (s->col > s->col_end) {
80 s->col = s->col_start;
82 } else {
83 s->col++;
84 if (s->col > s->col_end) {
85 s->row++;
86 s->col = s->col_start;
88 if (s->row > s->row_end) {
89 s->row = s->row_start;
92 s->redraw = 1;
93 break;
94 case SSD0323_CMD:
95 DPRINTF("cmd 0x%02x\n", data);
96 if (s->cmd_len == 0) {
97 s->cmd = data;
98 } else {
99 s->cmd_data[s->cmd_len - 1] = data;
101 s->cmd_len++;
102 switch (s->cmd) {
103 #define DATA(x) if (s->cmd_len <= (x)) return 0
104 case 0x15: /* Set column. */
105 DATA(2);
106 s->col = s->col_start = s->cmd_data[0] % 64;
107 s->col_end = s->cmd_data[1] % 64;
108 break;
109 case 0x75: /* Set row. */
110 DATA(2);
111 s->row = s->row_start = s->cmd_data[0] % 80;
112 s->row_end = s->cmd_data[1] % 80;
113 break;
114 case 0x81: /* Set contrast */
115 DATA(1);
116 break;
117 case 0x84: case 0x85: case 0x86: /* Max current. */
118 DATA(0);
119 break;
120 case 0xa0: /* Set remapping. */
121 /* FIXME: Implement this. */
122 DATA(1);
123 s->remap = s->cmd_data[0];
124 break;
125 case 0xa1: /* Set display start line. */
126 case 0xa2: /* Set display offset. */
127 /* FIXME: Implement these. */
128 DATA(1);
129 break;
130 case 0xa4: /* Normal mode. */
131 case 0xa5: /* All on. */
132 case 0xa6: /* All off. */
133 case 0xa7: /* Inverse. */
134 /* FIXME: Implement these. */
135 DATA(0);
136 break;
137 case 0xa8: /* Set multiplex ratio. */
138 case 0xad: /* Set DC-DC converter. */
139 DATA(1);
140 /* Ignored. Don't care. */
141 break;
142 case 0xae: /* Display off. */
143 case 0xaf: /* Display on. */
144 DATA(0);
145 /* TODO: Implement power control. */
146 break;
147 case 0xb1: /* Set phase length. */
148 case 0xb2: /* Set row period. */
149 case 0xb3: /* Set clock rate. */
150 case 0xbc: /* Set precharge. */
151 case 0xbe: /* Set VCOMH. */
152 case 0xbf: /* Set segment low. */
153 DATA(1);
154 /* Ignored. Don't care. */
155 break;
156 case 0xb8: /* Set grey scale table. */
157 /* FIXME: Implement this. */
158 DATA(8);
159 break;
160 case 0xe3: /* NOP. */
161 DATA(0);
162 break;
163 case 0xff: /* Nasty hack because we don't handle chip selects
164 properly. */
165 break;
166 default:
167 BADF("Unknown command: 0x%x\n", data);
169 s->cmd_len = 0;
170 return 0;
172 return 0;
175 static void ssd0323_update_display(void *opaque)
177 ssd0323_state *s = (ssd0323_state *)opaque;
178 DisplaySurface *surface = qemu_console_surface(s->con);
179 uint8_t *dest;
180 uint8_t *src;
181 int x;
182 int y;
183 int i;
184 int line;
185 char *colors[16];
186 char colortab[MAGNIFY * 64];
187 char *p;
188 int dest_width;
190 if (!s->redraw)
191 return;
193 switch (surface_bits_per_pixel(surface)) {
194 case 0:
195 return;
196 case 15:
197 dest_width = 2;
198 break;
199 case 16:
200 dest_width = 2;
201 break;
202 case 24:
203 dest_width = 3;
204 break;
205 case 32:
206 dest_width = 4;
207 break;
208 default:
209 BADF("Bad color depth\n");
210 return;
212 p = colortab;
213 for (i = 0; i < 16; i++) {
214 int n;
215 colors[i] = p;
216 switch (surface_bits_per_pixel(surface)) {
217 case 15:
218 n = i * 2 + (i >> 3);
219 p[0] = n | (n << 5);
220 p[1] = (n << 2) | (n >> 3);
221 break;
222 case 16:
223 n = i * 2 + (i >> 3);
224 p[0] = n | (n << 6) | ((n << 1) & 0x20);
225 p[1] = (n << 3) | (n >> 2);
226 break;
227 case 24:
228 case 32:
229 n = (i << 4) | i;
230 p[0] = p[1] = p[2] = n;
231 break;
232 default:
233 BADF("Bad color depth\n");
234 return;
236 p += dest_width;
238 /* TODO: Implement row/column remapping. */
239 dest = surface_data(surface);
240 for (y = 0; y < 64; y++) {
241 line = y;
242 src = s->framebuffer + 64 * line;
243 for (x = 0; x < 64; x++) {
244 int val;
245 val = *src >> 4;
246 for (i = 0; i < MAGNIFY; i++) {
247 memcpy(dest, colors[val], dest_width);
248 dest += dest_width;
250 val = *src & 0xf;
251 for (i = 0; i < MAGNIFY; i++) {
252 memcpy(dest, colors[val], dest_width);
253 dest += dest_width;
255 src++;
257 for (i = 1; i < MAGNIFY; i++) {
258 memcpy(dest, dest - dest_width * MAGNIFY * 128,
259 dest_width * 128 * MAGNIFY);
260 dest += dest_width * 128 * MAGNIFY;
263 s->redraw = 0;
264 dpy_gfx_update(s->con, 0, 0, 128 * MAGNIFY, 64 * MAGNIFY);
267 static void ssd0323_invalidate_display(void * opaque)
269 ssd0323_state *s = (ssd0323_state *)opaque;
270 s->redraw = 1;
273 /* Command/data input. */
274 static void ssd0323_cd(void *opaque, int n, int level)
276 ssd0323_state *s = (ssd0323_state *)opaque;
277 DPRINTF("%s mode\n", level ? "Data" : "Command");
278 s->mode = level ? SSD0323_DATA : SSD0323_CMD;
281 static void ssd0323_save(QEMUFile *f, void *opaque)
283 SSISlave *ss = SSI_SLAVE(opaque);
284 ssd0323_state *s = (ssd0323_state *)opaque;
285 int i;
287 qemu_put_be32(f, s->cmd_len);
288 qemu_put_be32(f, s->cmd);
289 for (i = 0; i < 8; i++)
290 qemu_put_be32(f, s->cmd_data[i]);
291 qemu_put_be32(f, s->row);
292 qemu_put_be32(f, s->row_start);
293 qemu_put_be32(f, s->row_end);
294 qemu_put_be32(f, s->col);
295 qemu_put_be32(f, s->col_start);
296 qemu_put_be32(f, s->col_end);
297 qemu_put_be32(f, s->redraw);
298 qemu_put_be32(f, s->remap);
299 qemu_put_be32(f, s->mode);
300 qemu_put_buffer(f, s->framebuffer, sizeof(s->framebuffer));
302 qemu_put_be32(f, ss->cs);
305 static int ssd0323_load(QEMUFile *f, void *opaque, int version_id)
307 SSISlave *ss = SSI_SLAVE(opaque);
308 ssd0323_state *s = (ssd0323_state *)opaque;
309 int i;
311 if (version_id != 1)
312 return -EINVAL;
314 s->cmd_len = qemu_get_be32(f);
315 s->cmd = qemu_get_be32(f);
316 for (i = 0; i < 8; i++)
317 s->cmd_data[i] = qemu_get_be32(f);
318 s->row = qemu_get_be32(f);
319 s->row_start = qemu_get_be32(f);
320 s->row_end = qemu_get_be32(f);
321 s->col = qemu_get_be32(f);
322 s->col_start = qemu_get_be32(f);
323 s->col_end = qemu_get_be32(f);
324 s->redraw = qemu_get_be32(f);
325 s->remap = qemu_get_be32(f);
326 s->mode = qemu_get_be32(f);
327 qemu_get_buffer(f, s->framebuffer, sizeof(s->framebuffer));
329 ss->cs = qemu_get_be32(f);
331 return 0;
334 static const GraphicHwOps ssd0323_ops = {
335 .invalidate = ssd0323_invalidate_display,
336 .gfx_update = ssd0323_update_display,
339 static int ssd0323_init(SSISlave *dev)
341 ssd0323_state *s = FROM_SSI_SLAVE(ssd0323_state, dev);
343 s->col_end = 63;
344 s->row_end = 79;
345 s->con = graphic_console_init(DEVICE(dev), &ssd0323_ops, s);
346 qemu_console_resize(s->con, 128 * MAGNIFY, 64 * MAGNIFY);
348 qdev_init_gpio_in(&dev->qdev, ssd0323_cd, 1);
350 register_savevm(&dev->qdev, "ssd0323_oled", -1, 1,
351 ssd0323_save, ssd0323_load, s);
352 return 0;
355 static void ssd0323_class_init(ObjectClass *klass, void *data)
357 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
359 k->init = ssd0323_init;
360 k->transfer = ssd0323_transfer;
361 k->cs_polarity = SSI_CS_HIGH;
364 static const TypeInfo ssd0323_info = {
365 .name = "ssd0323",
366 .parent = TYPE_SSI_SLAVE,
367 .instance_size = sizeof(ssd0323_state),
368 .class_init = ssd0323_class_init,
371 static void ssd03232_register_types(void)
373 type_register_static(&ssd0323_info);
376 type_init(ssd03232_register_types)