4 * Copyright (C) 2006-2007 Andrzej Zaborowski <balrog@zabor.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "framebuffer.h"
24 struct omap_lcd_panel_s
{
41 struct omap_dma_lcd_channel_s
*dma
;
42 uint16_t palette
[256];
49 static void omap_lcd_interrupts(struct omap_lcd_panel_s
*s
)
51 if (s
->frame_done
&& (s
->interrupts
& 1)) {
52 qemu_irq_raise(s
->irq
);
56 if (s
->palette_done
&& (s
->interrupts
& 2)) {
57 qemu_irq_raise(s
->irq
);
62 qemu_irq_raise(s
->irq
);
66 qemu_irq_lower(s
->irq
);
69 #include "pixel_ops.h"
71 #define draw_line_func drawfn
74 #include "omap_lcd_template.h"
76 #include "omap_lcd_template.h"
78 #include "omap_lcd_template.h"
80 #include "omap_lcd_template.h"
82 static draw_line_func draw_line_table2
[33] = {
88 }, draw_line_table4
[33] = {
94 }, draw_line_table8
[33] = {
100 }, draw_line_table12
[33] = {
103 [15] = draw_line12_15
,
104 [16] = draw_line12_16
,
105 [32] = draw_line12_32
,
106 }, draw_line_table16
[33] = {
109 [15] = draw_line16_15
,
110 [16] = draw_line16_16
,
111 [32] = draw_line16_32
,
114 static void omap_update_display(void *opaque
)
116 struct omap_lcd_panel_s
*omap_lcd
= (struct omap_lcd_panel_s
*) opaque
;
117 draw_line_func draw_line
;
118 int size
, height
, first
, last
;
119 int width
, linesize
, step
, bpp
, frame_offset
;
120 target_phys_addr_t frame_base
;
122 if (!omap_lcd
|| omap_lcd
->plm
== 1 ||
123 !omap_lcd
->enable
|| !ds_get_bits_per_pixel(omap_lcd
->state
))
127 if (omap_lcd
->plm
!= 2) {
128 cpu_physical_memory_read(omap_lcd
->dma
->phys_framebuffer
[
129 omap_lcd
->dma
->current_frame
],
130 (void *)omap_lcd
->palette
, 0x200);
131 switch (omap_lcd
->palette
[0] >> 12 & 7) {
133 frame_offset
+= 0x200;
136 frame_offset
+= 0x20;
141 switch ((omap_lcd
->palette
[0] >> 12) & 7) {
143 draw_line
= draw_line_table2
[ds_get_bits_per_pixel(omap_lcd
->state
)];
148 draw_line
= draw_line_table4
[ds_get_bits_per_pixel(omap_lcd
->state
)];
153 draw_line
= draw_line_table8
[ds_get_bits_per_pixel(omap_lcd
->state
)];
159 draw_line
= draw_line_table12
[ds_get_bits_per_pixel(omap_lcd
->state
)];
161 draw_line
= draw_line_table16
[ds_get_bits_per_pixel(omap_lcd
->state
)];
166 /* Unsupported at the moment. */
171 width
= omap_lcd
->width
;
172 if (width
!= ds_get_width(omap_lcd
->state
) ||
173 omap_lcd
->height
!= ds_get_height(omap_lcd
->state
)) {
174 qemu_console_resize(omap_lcd
->state
,
175 omap_lcd
->width
, omap_lcd
->height
);
176 omap_lcd
->invalidate
= 1;
179 if (omap_lcd
->dma
->current_frame
== 0)
180 size
= omap_lcd
->dma
->src_f1_bottom
- omap_lcd
->dma
->src_f1_top
;
182 size
= omap_lcd
->dma
->src_f2_bottom
- omap_lcd
->dma
->src_f2_top
;
184 if (frame_offset
+ ((width
* omap_lcd
->height
* bpp
) >> 3) > size
+ 2) {
185 omap_lcd
->sync_error
= 1;
186 omap_lcd_interrupts(omap_lcd
);
187 omap_lcd
->enable
= 0;
192 frame_base
= omap_lcd
->dma
->phys_framebuffer
[
193 omap_lcd
->dma
->current_frame
] + frame_offset
;
194 omap_lcd
->dma
->condition
|= 1 << omap_lcd
->dma
->current_frame
;
195 if (omap_lcd
->dma
->interrupts
& 1)
196 qemu_irq_raise(omap_lcd
->dma
->irq
);
197 if (omap_lcd
->dma
->dual
)
198 omap_lcd
->dma
->current_frame
^= 1;
200 if (!ds_get_bits_per_pixel(omap_lcd
->state
))
204 height
= omap_lcd
->height
;
205 if (omap_lcd
->subpanel
& (1 << 31)) {
206 if (omap_lcd
->subpanel
& (1 << 29))
207 first
= (omap_lcd
->subpanel
>> 16) & 0x3ff;
209 height
= (omap_lcd
->subpanel
>> 16) & 0x3ff;
210 /* TODO: fill the rest of the panel with DPD */
213 step
= width
* bpp
>> 3;
214 linesize
= ds_get_linesize(omap_lcd
->state
);
215 framebuffer_update_display(omap_lcd
->state
, omap_lcd
->sysmem
,
216 frame_base
, width
, height
,
218 omap_lcd
->invalidate
,
219 draw_line
, omap_lcd
->palette
,
222 dpy_update(omap_lcd
->state
, 0, first
, width
, last
- first
+ 1);
224 omap_lcd
->invalidate
= 0;
227 static int ppm_save(const char *filename
, uint8_t *data
,
228 int w
, int h
, int linesize
)
235 f
= fopen(filename
, "wb");
238 fprintf(f
, "P6\n%d %d\n%d\n", w
, h
, 255);
241 for (y
= 0; y
< h
; y
++) {
243 for (x
= 0; x
< w
; x
++) {
247 fputc((v
>> 8) & 0xf8, f
);
248 fputc((v
>> 3) & 0xfc, f
);
249 fputc((v
<< 3) & 0xf8, f
);
254 fputc((v
>> 16) & 0xff, f
);
255 fputc((v
>> 8) & 0xff, f
);
256 fputc((v
) & 0xff, f
);
267 static void omap_screen_dump(void *opaque
, const char *filename
, bool cswitch
)
269 struct omap_lcd_panel_s
*omap_lcd
= opaque
;
271 omap_update_display(opaque
);
273 if (omap_lcd
&& ds_get_data(omap_lcd
->state
))
274 ppm_save(filename
, ds_get_data(omap_lcd
->state
),
275 omap_lcd
->width
, omap_lcd
->height
,
276 ds_get_linesize(omap_lcd
->state
));
279 static void omap_invalidate_display(void *opaque
) {
280 struct omap_lcd_panel_s
*omap_lcd
= opaque
;
281 omap_lcd
->invalidate
= 1;
284 static void omap_lcd_update(struct omap_lcd_panel_s
*s
) {
286 s
->dma
->current_frame
= -1;
290 omap_lcd_interrupts(s
);
294 if (s
->dma
->current_frame
== -1) {
297 s
->dma
->current_frame
= 0;
300 if (!s
->dma
->mpu
->port
[s
->dma
->src
].addr_valid(s
->dma
->mpu
,
301 s
->dma
->src_f1_top
) ||
303 s
->dma
->src
].addr_valid(s
->dma
->mpu
,
304 s
->dma
->src_f1_bottom
) ||
307 s
->dma
->src
].addr_valid(s
->dma
->mpu
,
308 s
->dma
->src_f2_top
) ||
310 s
->dma
->src
].addr_valid(s
->dma
->mpu
,
311 s
->dma
->src_f2_bottom
)))) {
312 s
->dma
->condition
|= 1 << 2;
313 if (s
->dma
->interrupts
& (1 << 1))
314 qemu_irq_raise(s
->dma
->irq
);
319 s
->dma
->phys_framebuffer
[0] = s
->dma
->src_f1_top
;
320 s
->dma
->phys_framebuffer
[1] = s
->dma
->src_f2_top
;
322 if (s
->plm
!= 2 && !s
->palette_done
) {
323 cpu_physical_memory_read(
324 s
->dma
->phys_framebuffer
[s
->dma
->current_frame
],
325 (void *)s
->palette
, 0x200);
327 omap_lcd_interrupts(s
);
331 static uint64_t omap_lcdc_read(void *opaque
, target_phys_addr_t addr
,
334 struct omap_lcd_panel_s
*s
= (struct omap_lcd_panel_s
*) opaque
;
337 case 0x00: /* LCD_CONTROL */
338 return (s
->tft
<< 23) | (s
->plm
<< 20) |
339 (s
->tft
<< 7) | (s
->interrupts
<< 3) |
340 (s
->mono
<< 1) | s
->enable
| s
->ctrl
| 0xfe000c34;
342 case 0x04: /* LCD_TIMING0 */
343 return (s
->timing
[0] << 10) | (s
->width
- 1) | 0x0000000f;
345 case 0x08: /* LCD_TIMING1 */
346 return (s
->timing
[1] << 10) | (s
->height
- 1);
348 case 0x0c: /* LCD_TIMING2 */
349 return s
->timing
[2] | 0xfc000000;
351 case 0x10: /* LCD_STATUS */
352 return (s
->palette_done
<< 6) | (s
->sync_error
<< 2) | s
->frame_done
;
354 case 0x14: /* LCD_SUBPANEL */
364 static void omap_lcdc_write(void *opaque
, target_phys_addr_t addr
,
365 uint64_t value
, unsigned size
)
367 struct omap_lcd_panel_s
*s
= (struct omap_lcd_panel_s
*) opaque
;
370 case 0x00: /* LCD_CONTROL */
371 s
->plm
= (value
>> 20) & 3;
372 s
->tft
= (value
>> 7) & 1;
373 s
->interrupts
= (value
>> 3) & 3;
374 s
->mono
= (value
>> 1) & 1;
375 s
->ctrl
= value
& 0x01cff300;
376 if (s
->enable
!= (value
& 1)) {
377 s
->enable
= value
& 1;
382 case 0x04: /* LCD_TIMING0 */
383 s
->timing
[0] = value
>> 10;
384 s
->width
= (value
& 0x3ff) + 1;
387 case 0x08: /* LCD_TIMING1 */
388 s
->timing
[1] = value
>> 10;
389 s
->height
= (value
& 0x3ff) + 1;
392 case 0x0c: /* LCD_TIMING2 */
393 s
->timing
[2] = value
;
396 case 0x10: /* LCD_STATUS */
399 case 0x14: /* LCD_SUBPANEL */
400 s
->subpanel
= value
& 0xa1ffffff;
408 static const MemoryRegionOps omap_lcdc_ops
= {
409 .read
= omap_lcdc_read
,
410 .write
= omap_lcdc_write
,
411 .endianness
= DEVICE_NATIVE_ENDIAN
,
414 void omap_lcdc_reset(struct omap_lcd_panel_s
*s
)
416 s
->dma
->current_frame
= -1;
436 struct omap_lcd_panel_s
*omap_lcdc_init(MemoryRegion
*sysmem
,
437 target_phys_addr_t base
,
439 struct omap_dma_lcd_channel_s
*dma
,
442 struct omap_lcd_panel_s
*s
= (struct omap_lcd_panel_s
*)
443 g_malloc0(sizeof(struct omap_lcd_panel_s
));
450 memory_region_init_io(&s
->iomem
, &omap_lcdc_ops
, s
, "omap.lcdc", 0x100);
451 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
453 s
->state
= graphic_console_init(omap_update_display
,
454 omap_invalidate_display
,
455 omap_screen_dump
, NULL
, s
);