Statistics
| Branch: | Revision:

root / hw / sm501.c @ e7b43f7e

History | View | Annotate | Download (39 kB)

1
/*
2
 * QEMU SM501 Device
3
 *
4
 * Copyright (c) 2008 Shin-ichiro KAWASAKI
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
#include <stdio.h>
26
#include "hw.h"
27
#include "pc.h"
28
#include "console.h"
29
#include "devices.h"
30
#include "sysbus.h"
31
#include "qdev-addr.h"
32
#include "range.h"
33

    
34
/*
35
 * Status: 2010/05/07
36
 *   - Minimum implementation for Linux console : mmio regs and CRT layer.
37
 *   - 2D grapihcs acceleration partially supported : only fill rectangle.
38
 *
39
 * TODO:
40
 *   - Panel support
41
 *   - Touch panel support
42
 *   - USB support
43
 *   - UART support
44
 *   - More 2D graphics engine support
45
 *   - Performance tuning
46
 */
47

    
48
//#define DEBUG_SM501
49
//#define DEBUG_BITBLT
50

    
51
#ifdef DEBUG_SM501
52
#define SM501_DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
53
#else
54
#define SM501_DPRINTF(fmt, ...) do {} while(0)
55
#endif
56

    
57

    
58
#define MMIO_BASE_OFFSET 0x3e00000
59

    
60
/* SM501 register definitions taken from "linux/include/linux/sm501-regs.h" */
61

    
62
/* System Configuration area */
63
/* System config base */
64
#define SM501_SYS_CONFIG                (0x000000)
65

    
66
/* config 1 */
67
#define SM501_SYSTEM_CONTROL                 (0x000000)
68

    
69
#define SM501_SYSCTRL_PANEL_TRISTATE        (1<<0)
70
#define SM501_SYSCTRL_MEM_TRISTATE        (1<<1)
71
#define SM501_SYSCTRL_CRT_TRISTATE        (1<<2)
72

    
73
#define SM501_SYSCTRL_PCI_SLAVE_BURST_MASK (3<<4)
74
#define SM501_SYSCTRL_PCI_SLAVE_BURST_1        (0<<4)
75
#define SM501_SYSCTRL_PCI_SLAVE_BURST_2        (1<<4)
76
#define SM501_SYSCTRL_PCI_SLAVE_BURST_4        (2<<4)
77
#define SM501_SYSCTRL_PCI_SLAVE_BURST_8        (3<<4)
78

    
79
#define SM501_SYSCTRL_PCI_CLOCK_RUN_EN        (1<<6)
80
#define SM501_SYSCTRL_PCI_RETRY_DISABLE        (1<<7)
81
#define SM501_SYSCTRL_PCI_SUBSYS_LOCK        (1<<11)
82
#define SM501_SYSCTRL_PCI_BURST_READ_EN        (1<<15)
83

    
84
/* miscellaneous control */
85

    
86
#define SM501_MISC_CONTROL                (0x000004)
87

    
88
#define SM501_MISC_BUS_SH                (0x0)
89
#define SM501_MISC_BUS_PCI                (0x1)
90
#define SM501_MISC_BUS_XSCALE                (0x2)
91
#define SM501_MISC_BUS_NEC                (0x6)
92
#define SM501_MISC_BUS_MASK                (0x7)
93

    
94
#define SM501_MISC_VR_62MB                (1<<3)
95
#define SM501_MISC_CDR_RESET                (1<<7)
96
#define SM501_MISC_USB_LB                (1<<8)
97
#define SM501_MISC_USB_SLAVE                (1<<9)
98
#define SM501_MISC_BL_1                        (1<<10)
99
#define SM501_MISC_MC                        (1<<11)
100
#define SM501_MISC_DAC_POWER                (1<<12)
101
#define SM501_MISC_IRQ_INVERT                (1<<16)
102
#define SM501_MISC_SH                        (1<<17)
103

    
104
#define SM501_MISC_HOLD_EMPTY                (0<<18)
105
#define SM501_MISC_HOLD_8                (1<<18)
106
#define SM501_MISC_HOLD_16                (2<<18)
107
#define SM501_MISC_HOLD_24                (3<<18)
108
#define SM501_MISC_HOLD_32                (4<<18)
109
#define SM501_MISC_HOLD_MASK                (7<<18)
110

    
111
#define SM501_MISC_FREQ_12                (1<<24)
112
#define SM501_MISC_PNL_24BIT                (1<<25)
113
#define SM501_MISC_8051_LE                (1<<26)
114

    
115

    
116

    
117
#define SM501_GPIO31_0_CONTROL                (0x000008)
118
#define SM501_GPIO63_32_CONTROL                (0x00000C)
119
#define SM501_DRAM_CONTROL                (0x000010)
120

    
121
/* command list */
122
#define SM501_ARBTRTN_CONTROL                (0x000014)
123

    
124
/* command list */
125
#define SM501_COMMAND_LIST_STATUS        (0x000024)
126

    
127
/* interrupt debug */
128
#define SM501_RAW_IRQ_STATUS                (0x000028)
129
#define SM501_RAW_IRQ_CLEAR                (0x000028)
130
#define SM501_IRQ_STATUS                (0x00002C)
131
#define SM501_IRQ_MASK                        (0x000030)
132
#define SM501_DEBUG_CONTROL                (0x000034)
133

    
134
/* power management */
135
#define SM501_POWERMODE_P2X_SRC                (1<<29)
136
#define SM501_POWERMODE_V2X_SRC                (1<<20)
137
#define SM501_POWERMODE_M_SRC                (1<<12)
138
#define SM501_POWERMODE_M1_SRC                (1<<4)
139

    
140
#define SM501_CURRENT_GATE                (0x000038)
141
#define SM501_CURRENT_CLOCK                (0x00003C)
142
#define SM501_POWER_MODE_0_GATE                (0x000040)
143
#define SM501_POWER_MODE_0_CLOCK        (0x000044)
144
#define SM501_POWER_MODE_1_GATE                (0x000048)
145
#define SM501_POWER_MODE_1_CLOCK        (0x00004C)
146
#define SM501_SLEEP_MODE_GATE                (0x000050)
147
#define SM501_POWER_MODE_CONTROL        (0x000054)
148

    
149
/* power gates for units within the 501 */
150
#define SM501_GATE_HOST                        (0)
151
#define SM501_GATE_MEMORY                (1)
152
#define SM501_GATE_DISPLAY                (2)
153
#define SM501_GATE_2D_ENGINE                (3)
154
#define SM501_GATE_CSC                        (4)
155
#define SM501_GATE_ZVPORT                (5)
156
#define SM501_GATE_GPIO                        (6)
157
#define SM501_GATE_UART0                (7)
158
#define SM501_GATE_UART1                (8)
159
#define SM501_GATE_SSP                        (10)
160
#define SM501_GATE_USB_HOST                (11)
161
#define SM501_GATE_USB_GADGET                (12)
162
#define SM501_GATE_UCONTROLLER                (17)
163
#define SM501_GATE_AC97                        (18)
164

    
165
/* panel clock */
166
#define SM501_CLOCK_P2XCLK                (24)
167
/* crt clock */
168
#define SM501_CLOCK_V2XCLK                (16)
169
/* main clock */
170
#define SM501_CLOCK_MCLK                (8)
171
/* SDRAM controller clock */
172
#define SM501_CLOCK_M1XCLK                (0)
173

    
174
/* config 2 */
175
#define SM501_PCI_MASTER_BASE                (0x000058)
176
#define SM501_ENDIAN_CONTROL                (0x00005C)
177
#define SM501_DEVICEID                        (0x000060)
178
/* 0x050100A0 */
179

    
180
#define SM501_DEVICEID_SM501                (0x05010000)
181
#define SM501_DEVICEID_IDMASK                (0xffff0000)
182
#define SM501_DEVICEID_REVMASK                (0x000000ff)
183

    
184
#define SM501_PLLCLOCK_COUNT                (0x000064)
185
#define SM501_MISC_TIMING                (0x000068)
186
#define SM501_CURRENT_SDRAM_CLOCK        (0x00006C)
187

    
188
#define SM501_PROGRAMMABLE_PLL_CONTROL        (0x000074)
189

    
190
/* GPIO base */
191
#define SM501_GPIO                        (0x010000)
192
#define SM501_GPIO_DATA_LOW                (0x00)
193
#define SM501_GPIO_DATA_HIGH                (0x04)
194
#define SM501_GPIO_DDR_LOW                (0x08)
195
#define SM501_GPIO_DDR_HIGH                (0x0C)
196
#define SM501_GPIO_IRQ_SETUP                (0x10)
197
#define SM501_GPIO_IRQ_STATUS                (0x14)
198
#define SM501_GPIO_IRQ_RESET                (0x14)
199

    
200
/* I2C controller base */
201
#define SM501_I2C                        (0x010040)
202
#define SM501_I2C_BYTE_COUNT                (0x00)
203
#define SM501_I2C_CONTROL                (0x01)
204
#define SM501_I2C_STATUS                (0x02)
205
#define SM501_I2C_RESET                        (0x02)
206
#define SM501_I2C_SLAVE_ADDRESS                (0x03)
207
#define SM501_I2C_DATA                        (0x04)
208

    
209
/* SSP base */
210
#define SM501_SSP                        (0x020000)
211

    
212
/* Uart 0 base */
213
#define SM501_UART0                        (0x030000)
214

    
215
/* Uart 1 base */
216
#define SM501_UART1                        (0x030020)
217

    
218
/* USB host port base */
219
#define SM501_USB_HOST                        (0x040000)
220

    
221
/* USB slave/gadget base */
222
#define SM501_USB_GADGET                (0x060000)
223

    
224
/* USB slave/gadget data port base */
225
#define SM501_USB_GADGET_DATA                (0x070000)
226

    
227
/* Display controller/video engine base */
228
#define SM501_DC                        (0x080000)
229

    
230
/* common defines for the SM501 address registers */
231
#define SM501_ADDR_FLIP                        (1<<31)
232
#define SM501_ADDR_EXT                        (1<<27)
233
#define SM501_ADDR_CS1                        (1<<26)
234
#define SM501_ADDR_MASK                        (0x3f << 26)
235

    
236
#define SM501_FIFO_MASK                        (0x3 << 16)
237
#define SM501_FIFO_1                        (0x0 << 16)
238
#define SM501_FIFO_3                        (0x1 << 16)
239
#define SM501_FIFO_7                        (0x2 << 16)
240
#define SM501_FIFO_11                        (0x3 << 16)
241

    
242
/* common registers for panel and the crt */
243
#define SM501_OFF_DC_H_TOT                (0x000)
244
#define SM501_OFF_DC_V_TOT                (0x008)
245
#define SM501_OFF_DC_H_SYNC                (0x004)
246
#define SM501_OFF_DC_V_SYNC                (0x00C)
247

    
248
#define SM501_DC_PANEL_CONTROL                (0x000)
249

    
250
#define SM501_DC_PANEL_CONTROL_FPEN        (1<<27)
251
#define SM501_DC_PANEL_CONTROL_BIAS        (1<<26)
252
#define SM501_DC_PANEL_CONTROL_DATA        (1<<25)
253
#define SM501_DC_PANEL_CONTROL_VDD        (1<<24)
254
#define SM501_DC_PANEL_CONTROL_DP        (1<<23)
255

    
256
#define SM501_DC_PANEL_CONTROL_TFT_888        (0<<21)
257
#define SM501_DC_PANEL_CONTROL_TFT_333        (1<<21)
258
#define SM501_DC_PANEL_CONTROL_TFT_444        (2<<21)
259

    
260
#define SM501_DC_PANEL_CONTROL_DE        (1<<20)
261

    
262
#define SM501_DC_PANEL_CONTROL_LCD_TFT        (0<<18)
263
#define SM501_DC_PANEL_CONTROL_LCD_STN8        (1<<18)
264
#define SM501_DC_PANEL_CONTROL_LCD_STN12 (2<<18)
265

    
266
#define SM501_DC_PANEL_CONTROL_CP        (1<<14)
267
#define SM501_DC_PANEL_CONTROL_VSP        (1<<13)
268
#define SM501_DC_PANEL_CONTROL_HSP        (1<<12)
269
#define SM501_DC_PANEL_CONTROL_CK        (1<<9)
270
#define SM501_DC_PANEL_CONTROL_TE        (1<<8)
271
#define SM501_DC_PANEL_CONTROL_VPD        (1<<7)
272
#define SM501_DC_PANEL_CONTROL_VP        (1<<6)
273
#define SM501_DC_PANEL_CONTROL_HPD        (1<<5)
274
#define SM501_DC_PANEL_CONTROL_HP        (1<<4)
275
#define SM501_DC_PANEL_CONTROL_GAMMA        (1<<3)
276
#define SM501_DC_PANEL_CONTROL_EN        (1<<2)
277

    
278
#define SM501_DC_PANEL_CONTROL_8BPP        (0<<0)
279
#define SM501_DC_PANEL_CONTROL_16BPP        (1<<0)
280
#define SM501_DC_PANEL_CONTROL_32BPP        (2<<0)
281

    
282

    
283
#define SM501_DC_PANEL_PANNING_CONTROL        (0x004)
284
#define SM501_DC_PANEL_COLOR_KEY        (0x008)
285
#define SM501_DC_PANEL_FB_ADDR                (0x00C)
286
#define SM501_DC_PANEL_FB_OFFSET        (0x010)
287
#define SM501_DC_PANEL_FB_WIDTH                (0x014)
288
#define SM501_DC_PANEL_FB_HEIGHT        (0x018)
289
#define SM501_DC_PANEL_TL_LOC                (0x01C)
290
#define SM501_DC_PANEL_BR_LOC                (0x020)
291
#define SM501_DC_PANEL_H_TOT                (0x024)
292
#define SM501_DC_PANEL_H_SYNC                (0x028)
293
#define SM501_DC_PANEL_V_TOT                (0x02C)
294
#define SM501_DC_PANEL_V_SYNC                (0x030)
295
#define SM501_DC_PANEL_CUR_LINE                (0x034)
296

    
297
#define SM501_DC_VIDEO_CONTROL                (0x040)
298
#define SM501_DC_VIDEO_FB0_ADDR                (0x044)
299
#define SM501_DC_VIDEO_FB_WIDTH                (0x048)
300
#define SM501_DC_VIDEO_FB0_LAST_ADDR        (0x04C)
301
#define SM501_DC_VIDEO_TL_LOC                (0x050)
302
#define SM501_DC_VIDEO_BR_LOC                (0x054)
303
#define SM501_DC_VIDEO_SCALE                (0x058)
304
#define SM501_DC_VIDEO_INIT_SCALE        (0x05C)
305
#define SM501_DC_VIDEO_YUV_CONSTANTS        (0x060)
306
#define SM501_DC_VIDEO_FB1_ADDR                (0x064)
307
#define SM501_DC_VIDEO_FB1_LAST_ADDR        (0x068)
308

    
309
#define SM501_DC_VIDEO_ALPHA_CONTROL        (0x080)
310
#define SM501_DC_VIDEO_ALPHA_FB_ADDR        (0x084)
311
#define SM501_DC_VIDEO_ALPHA_FB_OFFSET        (0x088)
312
#define SM501_DC_VIDEO_ALPHA_FB_LAST_ADDR        (0x08C)
313
#define SM501_DC_VIDEO_ALPHA_TL_LOC        (0x090)
314
#define SM501_DC_VIDEO_ALPHA_BR_LOC        (0x094)
315
#define SM501_DC_VIDEO_ALPHA_SCALE        (0x098)
316
#define SM501_DC_VIDEO_ALPHA_INIT_SCALE        (0x09C)
317
#define SM501_DC_VIDEO_ALPHA_CHROMA_KEY        (0x0A0)
318
#define SM501_DC_VIDEO_ALPHA_COLOR_LOOKUP        (0x0A4)
319

    
320
#define SM501_DC_PANEL_HWC_BASE                (0x0F0)
321
#define SM501_DC_PANEL_HWC_ADDR                (0x0F0)
322
#define SM501_DC_PANEL_HWC_LOC                (0x0F4)
323
#define SM501_DC_PANEL_HWC_COLOR_1_2        (0x0F8)
324
#define SM501_DC_PANEL_HWC_COLOR_3        (0x0FC)
325

    
326
#define SM501_HWC_EN                        (1<<31)
327

    
328
#define SM501_OFF_HWC_ADDR                (0x00)
329
#define SM501_OFF_HWC_LOC                (0x04)
330
#define SM501_OFF_HWC_COLOR_1_2                (0x08)
331
#define SM501_OFF_HWC_COLOR_3                (0x0C)
332

    
333
#define SM501_DC_ALPHA_CONTROL                (0x100)
334
#define SM501_DC_ALPHA_FB_ADDR                (0x104)
335
#define SM501_DC_ALPHA_FB_OFFSET        (0x108)
336
#define SM501_DC_ALPHA_TL_LOC                (0x10C)
337
#define SM501_DC_ALPHA_BR_LOC                (0x110)
338
#define SM501_DC_ALPHA_CHROMA_KEY        (0x114)
339
#define SM501_DC_ALPHA_COLOR_LOOKUP        (0x118)
340

    
341
#define SM501_DC_CRT_CONTROL                (0x200)
342

    
343
#define SM501_DC_CRT_CONTROL_TVP        (1<<15)
344
#define SM501_DC_CRT_CONTROL_CP                (1<<14)
345
#define SM501_DC_CRT_CONTROL_VSP        (1<<13)
346
#define SM501_DC_CRT_CONTROL_HSP        (1<<12)
347
#define SM501_DC_CRT_CONTROL_VS                (1<<11)
348
#define SM501_DC_CRT_CONTROL_BLANK        (1<<10)
349
#define SM501_DC_CRT_CONTROL_SEL        (1<<9)
350
#define SM501_DC_CRT_CONTROL_TE                (1<<8)
351
#define SM501_DC_CRT_CONTROL_PIXEL_MASK (0xF << 4)
352
#define SM501_DC_CRT_CONTROL_GAMMA        (1<<3)
353
#define SM501_DC_CRT_CONTROL_ENABLE        (1<<2)
354

    
355
#define SM501_DC_CRT_CONTROL_8BPP        (0<<0)
356
#define SM501_DC_CRT_CONTROL_16BPP        (1<<0)
357
#define SM501_DC_CRT_CONTROL_32BPP        (2<<0)
358

    
359
#define SM501_DC_CRT_FB_ADDR                (0x204)
360
#define SM501_DC_CRT_FB_OFFSET                (0x208)
361
#define SM501_DC_CRT_H_TOT                (0x20C)
362
#define SM501_DC_CRT_H_SYNC                (0x210)
363
#define SM501_DC_CRT_V_TOT                (0x214)
364
#define SM501_DC_CRT_V_SYNC                (0x218)
365
#define SM501_DC_CRT_SIGNATURE_ANALYZER        (0x21C)
366
#define SM501_DC_CRT_CUR_LINE                (0x220)
367
#define SM501_DC_CRT_MONITOR_DETECT        (0x224)
368

    
369
#define SM501_DC_CRT_HWC_BASE                (0x230)
370
#define SM501_DC_CRT_HWC_ADDR                (0x230)
371
#define SM501_DC_CRT_HWC_LOC                (0x234)
372
#define SM501_DC_CRT_HWC_COLOR_1_2        (0x238)
373
#define SM501_DC_CRT_HWC_COLOR_3        (0x23C)
374

    
375
#define SM501_DC_PANEL_PALETTE                (0x400)
376

    
377
#define SM501_DC_VIDEO_PALETTE                (0x800)
378

    
379
#define SM501_DC_CRT_PALETTE                (0xC00)
380

    
381
/* Zoom Video port base */
382
#define SM501_ZVPORT                        (0x090000)
383

    
384
/* AC97/I2S base */
385
#define SM501_AC97                        (0x0A0000)
386

    
387
/* 8051 micro controller base */
388
#define SM501_UCONTROLLER                (0x0B0000)
389

    
390
/* 8051 micro controller SRAM base */
391
#define SM501_UCONTROLLER_SRAM                (0x0C0000)
392

    
393
/* DMA base */
394
#define SM501_DMA                        (0x0D0000)
395

    
396
/* 2d engine base */
397
#define SM501_2D_ENGINE                        (0x100000)
398
#define SM501_2D_SOURCE                        (0x00)
399
#define SM501_2D_DESTINATION                (0x04)
400
#define SM501_2D_DIMENSION                (0x08)
401
#define SM501_2D_CONTROL                (0x0C)
402
#define SM501_2D_PITCH                        (0x10)
403
#define SM501_2D_FOREGROUND                (0x14)
404
#define SM501_2D_BACKGROUND                (0x18)
405
#define SM501_2D_STRETCH                (0x1C)
406
#define SM501_2D_COLOR_COMPARE                (0x20)
407
#define SM501_2D_COLOR_COMPARE_MASK         (0x24)
408
#define SM501_2D_MASK                        (0x28)
409
#define SM501_2D_CLIP_TL                (0x2C)
410
#define SM501_2D_CLIP_BR                (0x30)
411
#define SM501_2D_MONO_PATTERN_LOW        (0x34)
412
#define SM501_2D_MONO_PATTERN_HIGH        (0x38)
413
#define SM501_2D_WINDOW_WIDTH                (0x3C)
414
#define SM501_2D_SOURCE_BASE                (0x40)
415
#define SM501_2D_DESTINATION_BASE        (0x44)
416
#define SM501_2D_ALPHA                        (0x48)
417
#define SM501_2D_WRAP                        (0x4C)
418
#define SM501_2D_STATUS                        (0x50)
419

    
420
#define SM501_CSC_Y_SOURCE_BASE                (0xC8)
421
#define SM501_CSC_CONSTANTS                (0xCC)
422
#define SM501_CSC_Y_SOURCE_X                (0xD0)
423
#define SM501_CSC_Y_SOURCE_Y                (0xD4)
424
#define SM501_CSC_U_SOURCE_BASE                (0xD8)
425
#define SM501_CSC_V_SOURCE_BASE                (0xDC)
426
#define SM501_CSC_SOURCE_DIMENSION        (0xE0)
427
#define SM501_CSC_SOURCE_PITCH                (0xE4)
428
#define SM501_CSC_DESTINATION                (0xE8)
429
#define SM501_CSC_DESTINATION_DIMENSION        (0xEC)
430
#define SM501_CSC_DESTINATION_PITCH        (0xF0)
431
#define SM501_CSC_SCALE_FACTOR                (0xF4)
432
#define SM501_CSC_DESTINATION_BASE        (0xF8)
433
#define SM501_CSC_CONTROL                (0xFC)
434

    
435
/* 2d engine data port base */
436
#define SM501_2D_ENGINE_DATA                (0x110000)
437

    
438
/* end of register definitions */
439

    
440
#define SM501_HWC_WIDTH                       (64)
441
#define SM501_HWC_HEIGHT                      (64)
442

    
443
/* SM501 local memory size taken from "linux/drivers/mfd/sm501.c" */
444
static const uint32_t sm501_mem_local_size[] = {
445
        [0]        = 4*1024*1024,
446
        [1]        = 8*1024*1024,
447
        [2]        = 16*1024*1024,
448
        [3]        = 32*1024*1024,
449
        [4]        = 64*1024*1024,
450
        [5]        = 2*1024*1024,
451
};
452
#define get_local_mem_size(s) sm501_mem_local_size[(s)->local_mem_size_index]
453

    
454
typedef struct SM501State {
455
    /* graphic console status */
456
    DisplayState *ds;
457

    
458
    /* status & internal resources */
459
    target_phys_addr_t base;
460
    uint32_t local_mem_size_index;
461
    uint8_t * local_mem;
462
    ram_addr_t local_mem_offset;
463
    uint32_t last_width;
464
    uint32_t last_height;
465

    
466
    /* mmio registers */
467
    uint32_t system_control;
468
    uint32_t misc_control;
469
    uint32_t gpio_31_0_control;
470
    uint32_t gpio_63_32_control;
471
    uint32_t dram_control;
472
    uint32_t irq_mask;
473
    uint32_t misc_timing;
474
    uint32_t power_mode_control;
475

    
476
    uint32_t uart0_ier;
477
    uint32_t uart0_lcr;
478
    uint32_t uart0_mcr;
479
    uint32_t uart0_scr;
480

    
481
    uint8_t dc_palette[0x400 * 3];
482

    
483
    uint32_t dc_panel_control;
484
    uint32_t dc_panel_panning_control;
485
    uint32_t dc_panel_fb_addr;
486
    uint32_t dc_panel_fb_offset;
487
    uint32_t dc_panel_fb_width;
488
    uint32_t dc_panel_fb_height;
489
    uint32_t dc_panel_tl_location;
490
    uint32_t dc_panel_br_location;
491
    uint32_t dc_panel_h_total;
492
    uint32_t dc_panel_h_sync;
493
    uint32_t dc_panel_v_total;
494
    uint32_t dc_panel_v_sync;
495

    
496
    uint32_t dc_panel_hwc_addr;
497
    uint32_t dc_panel_hwc_location;
498
    uint32_t dc_panel_hwc_color_1_2;
499
    uint32_t dc_panel_hwc_color_3;
500

    
501
    uint32_t dc_crt_control;
502
    uint32_t dc_crt_fb_addr;
503
    uint32_t dc_crt_fb_offset;
504
    uint32_t dc_crt_h_total;
505
    uint32_t dc_crt_h_sync;
506
    uint32_t dc_crt_v_total;
507
    uint32_t dc_crt_v_sync;
508

    
509
    uint32_t dc_crt_hwc_addr;
510
    uint32_t dc_crt_hwc_location;
511
    uint32_t dc_crt_hwc_color_1_2;
512
    uint32_t dc_crt_hwc_color_3;
513

    
514
    uint32_t twoD_destination;
515
    uint32_t twoD_dimension;
516
    uint32_t twoD_control;
517
    uint32_t twoD_pitch;
518
    uint32_t twoD_foreground;
519
    uint32_t twoD_stretch;
520
    uint32_t twoD_color_compare_mask;
521
    uint32_t twoD_mask;
522
    uint32_t twoD_window_width;
523
    uint32_t twoD_source_base;
524
    uint32_t twoD_destination_base;
525

    
526
} SM501State;
527

    
528
static uint32_t get_local_mem_size_index(uint32_t size)
529
{
530
    uint32_t norm_size = 0;
531
    int i, index = 0;
532

    
533
    for (i = 0; i < ARRAY_SIZE(sm501_mem_local_size); i++) {
534
        uint32_t new_size = sm501_mem_local_size[i];
535
        if (new_size >= size) {
536
            if (norm_size == 0 || norm_size > new_size) {
537
                norm_size = new_size;
538
                index = i;
539
            }
540
        }
541
    }
542

    
543
    return index;
544
}
545

    
546
/**
547
 * Check the availability of hardware cursor.
548
 * @param crt  0 for PANEL, 1 for CRT.
549
 */
550
static inline int is_hwc_enabled(SM501State *state, int crt)
551
{
552
    uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
553
    return addr & 0x80000000;
554
}
555

    
556
/**
557
 * Get the address which holds cursor pattern data.
558
 * @param crt  0 for PANEL, 1 for CRT.
559
 */
560
static inline uint32_t get_hwc_address(SM501State *state, int crt)
561
{
562
    uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
563
    return (addr & 0x03FFFFF0)/* >> 4*/;
564
}
565

    
566
/**
567
 * Get the cursor position in y coordinate.
568
 * @param crt  0 for PANEL, 1 for CRT.
569
 */
570
static inline uint32_t get_hwc_y(SM501State *state, int crt)
571
{
572
    uint32_t location = crt ? state->dc_crt_hwc_location
573
                            : state->dc_panel_hwc_location;
574
    return (location & 0x07FF0000) >> 16;
575
}
576

    
577
/**
578
 * Get the cursor position in x coordinate.
579
 * @param crt  0 for PANEL, 1 for CRT.
580
 */
581
static inline uint32_t get_hwc_x(SM501State *state, int crt)
582
{
583
    uint32_t location = crt ? state->dc_crt_hwc_location
584
                            : state->dc_panel_hwc_location;
585
    return location & 0x000007FF;
586
}
587

    
588
/**
589
 * Get the cursor position in x coordinate.
590
 * @param crt  0 for PANEL, 1 for CRT.
591
 * @param index  0, 1, 2 or 3 which specifies color of corsor dot.
592
 */
593
static inline uint16_t get_hwc_color(SM501State *state, int crt, int index)
594
{
595
    uint16_t color_reg = 0;
596
    uint16_t color_565 = 0;
597

    
598
    if (index == 0) {
599
        return 0;
600
    }
601

    
602
    switch (index) {
603
    case 1:
604
    case 2:
605
        color_reg = crt ? state->dc_crt_hwc_color_1_2
606
                        : state->dc_panel_hwc_color_1_2;
607
        break;
608
    case 3:
609
        color_reg = crt ? state->dc_crt_hwc_color_3
610
                        : state->dc_panel_hwc_color_3;
611
        break;
612
    default:
613
        printf("invalid hw cursor color.\n");
614
        abort();
615
    }
616

    
617
    switch (index) {
618
    case 1:
619
    case 3:
620
        color_565 = (uint16_t)(color_reg & 0xFFFF);
621
        break;
622
    case 2:
623
        color_565 = (uint16_t)((color_reg >> 16) & 0xFFFF);
624
        break;
625
    }
626
    return color_565;
627
}
628

    
629
static int within_hwc_y_range(SM501State *state, int y, int crt)
630
{
631
    int hwc_y = get_hwc_y(state, crt);
632
    return (hwc_y <= y && y < hwc_y + SM501_HWC_HEIGHT);
633
}
634

    
635
static void sm501_2d_operation(SM501State * s)
636
{
637
    /* obtain operation parameters */
638
    int operation = (s->twoD_control >> 16) & 0x1f;
639
    int dst_x = (s->twoD_destination >> 16) & 0x01FFF;
640
    int dst_y = s->twoD_destination & 0xFFFF;
641
    int operation_width = (s->twoD_dimension >> 16) & 0x1FFF;
642
    int operation_height = s->twoD_dimension & 0xFFFF;
643
    uint32_t color = s->twoD_foreground;
644
    int format_flags = (s->twoD_stretch >> 20) & 0x3;
645
    int addressing = (s->twoD_stretch >> 16) & 0xF;
646

    
647
    /* get frame buffer info */
648
#if 0 /* for future use */
649
    uint8_t * src = s->local_mem + (s->twoD_source_base & 0x03FFFFFF);
650
#endif
651
    uint8_t * dst = s->local_mem + (s->twoD_destination_base & 0x03FFFFFF);
652
    int dst_width = (s->dc_crt_h_total & 0x00000FFF) + 1;
653

    
654
    if (addressing != 0x0) {
655
        printf("%s: only XY addressing is supported.\n", __func__);
656
        abort();
657
    }
658

    
659
    if ((s->twoD_source_base & 0x08000000) ||
660
        (s->twoD_destination_base & 0x08000000)) {
661
        printf("%s: only local memory is supported.\n", __func__);
662
        abort();
663
    }
664

    
665
    switch (operation) {
666
    case 0x01: /* fill rectangle */
667

    
668
#define FILL_RECT(_bpp, _pixel_type) {                                      \
669
        int y, x;                                                           \
670
        for (y = 0; y < operation_height; y++) {                            \
671
            for (x = 0; x < operation_width; x++) {                         \
672
                int index = ((dst_y + y) * dst_width + dst_x + x) * _bpp;   \
673
                *(_pixel_type*)&dst[index] = (_pixel_type)color;            \
674
            }                                                               \
675
        }                                                                   \
676
    }
677

    
678
        switch (format_flags) {
679
        case 0:
680
            FILL_RECT(1, uint8_t);
681
            break;
682
        case 1:
683
            FILL_RECT(2, uint16_t);
684
            break;
685
        case 2:
686
            FILL_RECT(4, uint32_t);
687
            break;
688
        }
689
        break;
690

    
691
    default:
692
        printf("non-implemented SM501 2D operation. %d\n", operation);
693
        abort();
694
        break;
695
    }
696
}
697

    
698
static uint32_t sm501_system_config_read(void *opaque, target_phys_addr_t addr)
699
{
700
    SM501State * s = (SM501State *)opaque;
701
    uint32_t ret = 0;
702
    SM501_DPRINTF("sm501 system config regs : read addr=%x\n", (int)addr);
703

    
704
    switch(addr) {
705
    case SM501_SYSTEM_CONTROL:
706
        ret = s->system_control;
707
        break;
708
    case SM501_MISC_CONTROL:
709
        ret = s->misc_control;
710
        break;
711
    case SM501_GPIO31_0_CONTROL:
712
        ret = s->gpio_31_0_control;
713
        break;
714
    case SM501_GPIO63_32_CONTROL:
715
        ret = s->gpio_63_32_control;
716
        break;
717
    case SM501_DEVICEID:
718
        ret = 0x050100A0;
719
        break;
720
    case SM501_DRAM_CONTROL:
721
        ret = (s->dram_control & 0x07F107C0) | s->local_mem_size_index << 13;
722
        break;
723
    case SM501_IRQ_MASK:
724
        ret = s->irq_mask;
725
        break;
726
    case SM501_MISC_TIMING:
727
        /* TODO : simulate gate control */
728
        ret = s->misc_timing;
729
        break;
730
    case SM501_CURRENT_GATE:
731
        /* TODO : simulate gate control */
732
        ret = 0x00021807;
733
        break;
734
    case SM501_CURRENT_CLOCK:
735
        ret = 0x2A1A0A09;
736
        break;
737
    case SM501_POWER_MODE_CONTROL:
738
        ret = s->power_mode_control;
739
        break;
740

    
741
    default:
742
        printf("sm501 system config : not implemented register read."
743
               " addr=%x\n", (int)addr);
744
        abort();
745
    }
746

    
747
    return ret;
748
}
749

    
750
static void sm501_system_config_write(void *opaque,
751
                                      target_phys_addr_t addr, uint32_t value)
752
{
753
    SM501State * s = (SM501State *)opaque;
754
    SM501_DPRINTF("sm501 system config regs : write addr=%x, val=%x\n",
755
                  addr, value);
756

    
757
    switch(addr) {
758
    case SM501_SYSTEM_CONTROL:
759
        s->system_control = value & 0xE300B8F7;
760
        break;
761
    case SM501_MISC_CONTROL:
762
        s->misc_control = value & 0xFF7FFF20;
763
        break;
764
    case SM501_GPIO31_0_CONTROL:
765
        s->gpio_31_0_control = value;
766
        break;
767
    case SM501_GPIO63_32_CONTROL:
768
        s->gpio_63_32_control = value;
769
        break;
770
    case SM501_DRAM_CONTROL:
771
        s->local_mem_size_index = (value >> 13) & 0x7;
772
        /* rODO : check validity of size change */
773
        s->dram_control |=  value & 0x7FFFFFC3;
774
        break;
775
    case SM501_IRQ_MASK:
776
        s->irq_mask = value;
777
        break;
778
    case SM501_MISC_TIMING:
779
        s->misc_timing = value & 0xF31F1FFF;
780
        break;
781
    case SM501_POWER_MODE_0_GATE:
782
    case SM501_POWER_MODE_1_GATE:
783
    case SM501_POWER_MODE_0_CLOCK:
784
    case SM501_POWER_MODE_1_CLOCK:
785
        /* TODO : simulate gate & clock control */
786
        break;
787
    case SM501_POWER_MODE_CONTROL:
788
        s->power_mode_control = value & 0x00000003;
789
        break;
790

    
791
    default:
792
        printf("sm501 system config : not implemented register write."
793
               " addr=%x, val=%x\n", (int)addr, value);
794
        abort();
795
    }
796
}
797

    
798
static CPUReadMemoryFunc * const sm501_system_config_readfn[] = {
799
    NULL,
800
    NULL,
801
    &sm501_system_config_read,
802
};
803

    
804
static CPUWriteMemoryFunc * const sm501_system_config_writefn[] = {
805
    NULL,
806
    NULL,
807
    &sm501_system_config_write,
808
};
809

    
810
static uint32_t sm501_palette_read(void *opaque, target_phys_addr_t addr)
811
{
812
    SM501State * s = (SM501State *)opaque;
813
    SM501_DPRINTF("sm501 palette read addr=%x\n", (int)addr);
814

    
815
    /* TODO : consider BYTE/WORD access */
816
    /* TODO : consider endian */
817

    
818
    assert(range_covers_byte(0, 0x400 * 3, addr));
819
    return *(uint32_t*)&s->dc_palette[addr];
820
}
821

    
822
static void sm501_palette_write(void *opaque,
823
                                target_phys_addr_t addr, uint32_t value)
824
{
825
    SM501State * s = (SM501State *)opaque;
826
    SM501_DPRINTF("sm501 palette write addr=%x, val=%x\n",
827
                  (int)addr, value);
828

    
829
    /* TODO : consider BYTE/WORD access */
830
    /* TODO : consider endian */
831

    
832
    assert(range_covers_byte(0, 0x400 * 3, addr));
833
    *(uint32_t*)&s->dc_palette[addr] = value;
834
}
835

    
836
static uint32_t sm501_disp_ctrl_read(void *opaque, target_phys_addr_t addr)
837
{
838
    SM501State * s = (SM501State *)opaque;
839
    uint32_t ret = 0;
840
    SM501_DPRINTF("sm501 disp ctrl regs : read addr=%x\n", (int)addr);
841

    
842
    switch(addr) {
843

    
844
    case SM501_DC_PANEL_CONTROL:
845
        ret = s->dc_panel_control;
846
        break;
847
    case SM501_DC_PANEL_PANNING_CONTROL:
848
        ret = s->dc_panel_panning_control;
849
        break;
850
    case SM501_DC_PANEL_FB_ADDR:
851
        ret = s->dc_panel_fb_addr;
852
        break;
853
    case SM501_DC_PANEL_FB_OFFSET:
854
        ret = s->dc_panel_fb_offset;
855
        break;
856
    case SM501_DC_PANEL_FB_WIDTH:
857
        ret = s->dc_panel_fb_width;
858
        break;
859
    case SM501_DC_PANEL_FB_HEIGHT:
860
        ret = s->dc_panel_fb_height;
861
        break;
862
    case SM501_DC_PANEL_TL_LOC:
863
        ret = s->dc_panel_tl_location;
864
        break;
865
    case SM501_DC_PANEL_BR_LOC:
866
        ret = s->dc_panel_br_location;
867
        break;
868

    
869
    case SM501_DC_PANEL_H_TOT:
870
        ret = s->dc_panel_h_total;
871
        break;
872
    case SM501_DC_PANEL_H_SYNC:
873
        ret = s->dc_panel_h_sync;
874
        break;
875
    case SM501_DC_PANEL_V_TOT:
876
        ret = s->dc_panel_v_total;
877
        break;
878
    case SM501_DC_PANEL_V_SYNC:
879
        ret = s->dc_panel_v_sync;
880
        break;
881

    
882
    case SM501_DC_CRT_CONTROL:
883
        ret = s->dc_crt_control;
884
        break;
885
    case SM501_DC_CRT_FB_ADDR:
886
        ret = s->dc_crt_fb_addr;
887
        break;
888
    case SM501_DC_CRT_FB_OFFSET:
889
        ret = s->dc_crt_fb_offset;
890
        break;
891
    case SM501_DC_CRT_H_TOT:
892
        ret = s->dc_crt_h_total;
893
        break;
894
    case SM501_DC_CRT_H_SYNC:
895
        ret = s->dc_crt_h_sync;
896
        break;
897
    case SM501_DC_CRT_V_TOT:
898
        ret = s->dc_crt_v_total;
899
        break;
900
    case SM501_DC_CRT_V_SYNC:
901
        ret = s->dc_crt_v_sync;
902
        break;
903

    
904
    case SM501_DC_CRT_HWC_ADDR:
905
        ret = s->dc_crt_hwc_addr;
906
        break;
907
    case SM501_DC_CRT_HWC_LOC:
908
        ret = s->dc_crt_hwc_location;
909
        break;
910
    case SM501_DC_CRT_HWC_COLOR_1_2:
911
        ret = s->dc_crt_hwc_color_1_2;
912
        break;
913
    case SM501_DC_CRT_HWC_COLOR_3:
914
        ret = s->dc_crt_hwc_color_3;
915
        break;
916

    
917
    case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400*3 - 4:
918
        ret = sm501_palette_read(opaque, addr - SM501_DC_PANEL_PALETTE);
919
        break;
920

    
921
    default:
922
        printf("sm501 disp ctrl : not implemented register read."
923
               " addr=%x\n", (int)addr);
924
        abort();
925
    }
926

    
927
    return ret;
928
}
929

    
930
static void sm501_disp_ctrl_write(void *opaque,
931
                                           target_phys_addr_t addr,
932
                                           uint32_t value)
933
{
934
    SM501State * s = (SM501State *)opaque;
935
    SM501_DPRINTF("sm501 disp ctrl regs : write addr=%x, val=%x\n",
936
                  addr, value);
937

    
938
    switch(addr) {
939
    case SM501_DC_PANEL_CONTROL:
940
        s->dc_panel_control = value & 0x0FFF73FF;
941
        break;
942
    case SM501_DC_PANEL_PANNING_CONTROL:
943
        s->dc_panel_panning_control = value & 0xFF3FFF3F;
944
        break;
945
    case SM501_DC_PANEL_FB_ADDR:
946
        s->dc_panel_fb_addr = value & 0x8FFFFFF0;
947
        break;
948
    case SM501_DC_PANEL_FB_OFFSET:
949
        s->dc_panel_fb_offset = value & 0x3FF03FF0;
950
        break;
951
    case SM501_DC_PANEL_FB_WIDTH:
952
        s->dc_panel_fb_width = value & 0x0FFF0FFF;
953
        break;
954
    case SM501_DC_PANEL_FB_HEIGHT:
955
        s->dc_panel_fb_height = value & 0x0FFF0FFF;
956
        break;
957
    case SM501_DC_PANEL_TL_LOC:
958
        s->dc_panel_tl_location = value & 0x07FF07FF;
959
        break;
960
    case SM501_DC_PANEL_BR_LOC:
961
        s->dc_panel_br_location = value & 0x07FF07FF;
962
        break;
963

    
964
    case SM501_DC_PANEL_H_TOT:
965
        s->dc_panel_h_total = value & 0x0FFF0FFF;
966
        break;
967
    case SM501_DC_PANEL_H_SYNC:
968
        s->dc_panel_h_sync = value & 0x00FF0FFF;
969
        break;
970
    case SM501_DC_PANEL_V_TOT:
971
        s->dc_panel_v_total = value & 0x0FFF0FFF;
972
        break;
973
    case SM501_DC_PANEL_V_SYNC:
974
        s->dc_panel_v_sync = value & 0x003F0FFF;
975
        break;
976

    
977
    case SM501_DC_PANEL_HWC_ADDR:
978
        s->dc_panel_hwc_addr = value & 0x8FFFFFF0;
979
        break;
980
    case SM501_DC_PANEL_HWC_LOC:
981
        s->dc_panel_hwc_location = value & 0x0FFF0FFF;
982
        break;
983
    case SM501_DC_PANEL_HWC_COLOR_1_2:
984
        s->dc_panel_hwc_color_1_2 = value;
985
        break;
986
    case SM501_DC_PANEL_HWC_COLOR_3:
987
        s->dc_panel_hwc_color_3 = value & 0x0000FFFF;
988
        break;
989

    
990
    case SM501_DC_CRT_CONTROL:
991
        s->dc_crt_control = value & 0x0003FFFF;
992
        break;
993
    case SM501_DC_CRT_FB_ADDR:
994
        s->dc_crt_fb_addr = value & 0x8FFFFFF0;
995
        break;
996
    case SM501_DC_CRT_FB_OFFSET:
997
        s->dc_crt_fb_offset = value & 0x3FF03FF0;
998
        break;
999
    case SM501_DC_CRT_H_TOT:
1000
        s->dc_crt_h_total = value & 0x0FFF0FFF;
1001
        break;
1002
    case SM501_DC_CRT_H_SYNC:
1003
        s->dc_crt_h_sync = value & 0x00FF0FFF;
1004
        break;
1005
    case SM501_DC_CRT_V_TOT:
1006
        s->dc_crt_v_total = value & 0x0FFF0FFF;
1007
        break;
1008
    case SM501_DC_CRT_V_SYNC:
1009
        s->dc_crt_v_sync = value & 0x003F0FFF;
1010
        break;
1011

    
1012
    case SM501_DC_CRT_HWC_ADDR:
1013
        s->dc_crt_hwc_addr = value & 0x8FFFFFF0;
1014
        break;
1015
    case SM501_DC_CRT_HWC_LOC:
1016
        s->dc_crt_hwc_location = value & 0x0FFF0FFF;
1017
        break;
1018
    case SM501_DC_CRT_HWC_COLOR_1_2:
1019
        s->dc_crt_hwc_color_1_2 = value;
1020
        break;
1021
    case SM501_DC_CRT_HWC_COLOR_3:
1022
        s->dc_crt_hwc_color_3 = value & 0x0000FFFF;
1023
        break;
1024

    
1025
    case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400*3 - 4:
1026
        sm501_palette_write(opaque, addr - SM501_DC_PANEL_PALETTE, value);
1027
        break;
1028

    
1029
    default:
1030
        printf("sm501 disp ctrl : not implemented register write."
1031
               " addr=%x, val=%x\n", (int)addr, value);
1032
        abort();
1033
    }
1034
}
1035

    
1036
static CPUReadMemoryFunc * const sm501_disp_ctrl_readfn[] = {
1037
    NULL,
1038
    NULL,
1039
    &sm501_disp_ctrl_read,
1040
};
1041

    
1042
static CPUWriteMemoryFunc * const sm501_disp_ctrl_writefn[] = {
1043
    NULL,
1044
    NULL,
1045
    &sm501_disp_ctrl_write,
1046
};
1047

    
1048
static uint32_t sm501_2d_engine_read(void *opaque, target_phys_addr_t addr)
1049
{
1050
    SM501State * s = (SM501State *)opaque;
1051
    uint32_t ret = 0;
1052
    SM501_DPRINTF("sm501 2d engine regs : read addr=%x\n", (int)addr);
1053

    
1054
    switch(addr) {
1055
    case SM501_2D_SOURCE_BASE:
1056
        ret = s->twoD_source_base;
1057
        break;
1058
    default:
1059
        printf("sm501 disp ctrl : not implemented register read."
1060
               " addr=%x\n", (int)addr);
1061
        abort();
1062
    }
1063

    
1064
    return ret;
1065
}
1066

    
1067
static void sm501_2d_engine_write(void *opaque,
1068
                                  target_phys_addr_t addr, uint32_t value)
1069
{
1070
    SM501State * s = (SM501State *)opaque;
1071
    SM501_DPRINTF("sm501 2d engine regs : write addr=%x, val=%x\n",
1072
                  addr, value);
1073

    
1074
    switch(addr) {
1075
    case SM501_2D_DESTINATION:
1076
        s->twoD_destination = value;
1077
        break;
1078
    case SM501_2D_DIMENSION:
1079
        s->twoD_dimension = value;
1080
        break;
1081
    case SM501_2D_CONTROL:
1082
        s->twoD_control = value;
1083

    
1084
        /* do 2d operation if start flag is set. */
1085
        if (value & 0x80000000) {
1086
            sm501_2d_operation(s);
1087
            s->twoD_control &= ~0x80000000; /* start flag down */
1088
        }
1089

    
1090
        break;
1091
    case SM501_2D_PITCH:
1092
        s->twoD_pitch = value;
1093
        break;
1094
    case SM501_2D_FOREGROUND:
1095
        s->twoD_foreground = value;
1096
        break;
1097
    case SM501_2D_STRETCH:
1098
        s->twoD_stretch = value;
1099
        break;
1100
    case SM501_2D_COLOR_COMPARE_MASK:
1101
        s->twoD_color_compare_mask = value;
1102
        break;
1103
    case SM501_2D_MASK:
1104
        s->twoD_mask = value;
1105
        break;
1106
    case SM501_2D_WINDOW_WIDTH:
1107
        s->twoD_window_width = value;
1108
        break;
1109
    case SM501_2D_SOURCE_BASE:
1110
        s->twoD_source_base = value;
1111
        break;
1112
    case SM501_2D_DESTINATION_BASE:
1113
        s->twoD_destination_base = value;
1114
        break;
1115
    default:
1116
        printf("sm501 2d engine : not implemented register write."
1117
               " addr=%x, val=%x\n", (int)addr, value);
1118
        abort();
1119
    }
1120
}
1121

    
1122
static CPUReadMemoryFunc * const sm501_2d_engine_readfn[] = {
1123
    NULL,
1124
    NULL,
1125
    &sm501_2d_engine_read,
1126
};
1127

    
1128
static CPUWriteMemoryFunc * const sm501_2d_engine_writefn[] = {
1129
    NULL,
1130
    NULL,
1131
    &sm501_2d_engine_write,
1132
};
1133

    
1134
/* draw line functions for all console modes */
1135

    
1136
#include "pixel_ops.h"
1137

    
1138
typedef void draw_line_func(uint8_t *d, const uint8_t *s,
1139
                            int width, const uint32_t *pal);
1140

    
1141
typedef void draw_hwc_line_func(SM501State * s, int crt, uint8_t * palette,
1142
                                int c_y, uint8_t *d, int width);
1143

    
1144
#define DEPTH 8
1145
#include "sm501_template.h"
1146

    
1147
#define DEPTH 15
1148
#include "sm501_template.h"
1149

    
1150
#define BGR_FORMAT
1151
#define DEPTH 15
1152
#include "sm501_template.h"
1153

    
1154
#define DEPTH 16
1155
#include "sm501_template.h"
1156

    
1157
#define BGR_FORMAT
1158
#define DEPTH 16
1159
#include "sm501_template.h"
1160

    
1161
#define DEPTH 32
1162
#include "sm501_template.h"
1163

    
1164
#define BGR_FORMAT
1165
#define DEPTH 32
1166
#include "sm501_template.h"
1167

    
1168
static draw_line_func * draw_line8_funcs[] = {
1169
    draw_line8_8,
1170
    draw_line8_15,
1171
    draw_line8_16,
1172
    draw_line8_32,
1173
    draw_line8_32bgr,
1174
    draw_line8_15bgr,
1175
    draw_line8_16bgr,
1176
};
1177

    
1178
static draw_line_func * draw_line16_funcs[] = {
1179
    draw_line16_8,
1180
    draw_line16_15,
1181
    draw_line16_16,
1182
    draw_line16_32,
1183
    draw_line16_32bgr,
1184
    draw_line16_15bgr,
1185
    draw_line16_16bgr,
1186
};
1187

    
1188
static draw_line_func * draw_line32_funcs[] = {
1189
    draw_line32_8,
1190
    draw_line32_15,
1191
    draw_line32_16,
1192
    draw_line32_32,
1193
    draw_line32_32bgr,
1194
    draw_line32_15bgr,
1195
    draw_line32_16bgr,
1196
};
1197

    
1198
static draw_hwc_line_func * draw_hwc_line_funcs[] = {
1199
    draw_hwc_line_8,
1200
    draw_hwc_line_15,
1201
    draw_hwc_line_16,
1202
    draw_hwc_line_32,
1203
    draw_hwc_line_32bgr,
1204
    draw_hwc_line_15bgr,
1205
    draw_hwc_line_16bgr,
1206
};
1207

    
1208
static inline int get_depth_index(DisplayState *s)
1209
{
1210
    switch(ds_get_bits_per_pixel(s)) {
1211
    default:
1212
    case 8:
1213
        return 0;
1214
    case 15:
1215
        return 1;
1216
    case 16:
1217
        return 2;
1218
    case 32:
1219
        if (is_surface_bgr(s->surface))
1220
            return 4;
1221
        else
1222
            return 3;
1223
    }
1224
}
1225

    
1226
static void sm501_draw_crt(SM501State * s)
1227
{
1228
    int y;
1229
    int width = (s->dc_crt_h_total & 0x00000FFF) + 1;
1230
    int height = (s->dc_crt_v_total & 0x00000FFF) + 1;
1231

    
1232
    uint8_t  * src = s->local_mem;
1233
    int src_bpp = 0;
1234
    int dst_bpp = ds_get_bytes_per_pixel(s->ds) + (ds_get_bits_per_pixel(s->ds) % 8 ? 1 : 0);
1235
    uint32_t * palette = (uint32_t *)&s->dc_palette[SM501_DC_CRT_PALETTE
1236
                                                    - SM501_DC_PANEL_PALETTE];
1237
    uint8_t hwc_palette[3 * 3];
1238
    int ds_depth_index = get_depth_index(s->ds);
1239
    draw_line_func * draw_line = NULL;
1240
    draw_hwc_line_func * draw_hwc_line = NULL;
1241
    int full_update = 0;
1242
    int y_start = -1;
1243
    int page_min = 0x7fffffff;
1244
    int page_max = -1;
1245
    ram_addr_t offset = s->local_mem_offset;
1246

    
1247
    /* choose draw_line function */
1248
    switch (s->dc_crt_control & 3) {
1249
    case SM501_DC_CRT_CONTROL_8BPP:
1250
        src_bpp = 1;
1251
        draw_line = draw_line8_funcs[ds_depth_index];
1252
        break;
1253
    case SM501_DC_CRT_CONTROL_16BPP:
1254
        src_bpp = 2;
1255
        draw_line = draw_line16_funcs[ds_depth_index];
1256
        break;
1257
    case SM501_DC_CRT_CONTROL_32BPP:
1258
        src_bpp = 4;
1259
        draw_line = draw_line32_funcs[ds_depth_index];
1260
        break;
1261
    default:
1262
        printf("sm501 draw crt : invalid DC_CRT_CONTROL=%x.\n",
1263
               s->dc_crt_control);
1264
        abort();
1265
        break;
1266
    }
1267

    
1268
    /* set up to draw hardware cursor */
1269
    if (is_hwc_enabled(s, 1)) {
1270
        int i;
1271

    
1272
        /* get cursor palette */
1273
        for (i = 0; i < 3; i++) {
1274
            uint16_t rgb565 = get_hwc_color(s, 1, i + 1);
1275
            hwc_palette[i * 3 + 0] = (rgb565 & 0xf800) >> 8; /* red */
1276
            hwc_palette[i * 3 + 1] = (rgb565 & 0x07e0) >> 3; /* green */
1277
            hwc_palette[i * 3 + 2] = (rgb565 & 0x001f) << 3; /* blue */
1278
        }
1279

    
1280
        /* choose cursor draw line function */
1281
        draw_hwc_line = draw_hwc_line_funcs[ds_depth_index];
1282
    }
1283

    
1284
    /* adjust console size */
1285
    if (s->last_width != width || s->last_height != height) {
1286
        qemu_console_resize(s->ds, width, height);
1287
        s->last_width = width;
1288
        s->last_height = height;
1289
        full_update = 1;
1290
    }
1291

    
1292
    /* draw each line according to conditions */
1293
    for (y = 0; y < height; y++) {
1294
        int update_hwc = draw_hwc_line ? within_hwc_y_range(s, y, 1) : 0;
1295
        int update = full_update || update_hwc;
1296
        ram_addr_t page0 = offset & TARGET_PAGE_MASK;
1297
        ram_addr_t page1 = (offset + width * src_bpp - 1) & TARGET_PAGE_MASK;
1298
        ram_addr_t page;
1299

    
1300
        /* check dirty flags for each line */
1301
        for (page = page0; page <= page1; page += TARGET_PAGE_SIZE)
1302
            if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG))
1303
                update = 1;
1304

    
1305
        /* draw line and change status */
1306
        if (update) {
1307
            uint8_t * d = &(ds_get_data(s->ds)[y * width * dst_bpp]);
1308

    
1309
            /* draw graphics layer */
1310
            draw_line(d, src, width, palette);
1311

    
1312
            /* draw haredware cursor */
1313
            if (update_hwc) {
1314
                draw_hwc_line(s, 1, hwc_palette, y - get_hwc_y(s, 1), d, width);
1315
            }
1316

    
1317
            if (y_start < 0)
1318
                y_start = y;
1319
            if (page0 < page_min)
1320
                page_min = page0;
1321
            if (page1 > page_max)
1322
                page_max = page1;
1323
        } else {
1324
            if (y_start >= 0) {
1325
                /* flush to display */
1326
                dpy_update(s->ds, 0, y_start, width, y - y_start);
1327
                y_start = -1;
1328
            }
1329
        }
1330

    
1331
        src += width * src_bpp;
1332
        offset += width * src_bpp;
1333
    }
1334

    
1335
    /* complete flush to display */
1336
    if (y_start >= 0)
1337
        dpy_update(s->ds, 0, y_start, width, y - y_start);
1338

    
1339
    /* clear dirty flags */
1340
    if (page_max != -1)
1341
        cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
1342
                                        VGA_DIRTY_FLAG);
1343
}
1344

    
1345
static void sm501_update_display(void *opaque)
1346
{
1347
    SM501State * s = (SM501State *)opaque;
1348

    
1349
    if (s->dc_crt_control & SM501_DC_CRT_CONTROL_ENABLE)
1350
        sm501_draw_crt(s);
1351
}
1352

    
1353
void sm501_init(uint32_t base, uint32_t local_mem_bytes, qemu_irq irq,
1354
                CharDriverState *chr)
1355
{
1356
    SM501State * s;
1357
    DeviceState *dev;
1358
    int sm501_system_config_index;
1359
    int sm501_disp_ctrl_index;
1360
    int sm501_2d_engine_index;
1361

    
1362
    /* allocate management data region */
1363
    s = (SM501State *)qemu_mallocz(sizeof(SM501State));
1364
    s->base = base;
1365
    s->local_mem_size_index
1366
        = get_local_mem_size_index(local_mem_bytes);
1367
    SM501_DPRINTF("local mem size=%x. index=%d\n", get_local_mem_size(s),
1368
                  s->local_mem_size_index);
1369
    s->system_control = 0x00100000;
1370
    s->misc_control = 0x00001000; /* assumes SH, active=low */
1371
    s->dc_panel_control = 0x00010000;
1372
    s->dc_crt_control = 0x00010000;
1373

    
1374
    /* allocate local memory */
1375
    s->local_mem_offset = qemu_ram_alloc(NULL, "sm501.local", local_mem_bytes);
1376
    s->local_mem = qemu_get_ram_ptr(s->local_mem_offset);
1377
    cpu_register_physical_memory(base, local_mem_bytes, s->local_mem_offset);
1378

    
1379
    /* map mmio */
1380
    sm501_system_config_index
1381
        = cpu_register_io_memory(sm501_system_config_readfn,
1382
                                 sm501_system_config_writefn, s);
1383
    cpu_register_physical_memory(base + MMIO_BASE_OFFSET,
1384
                                 0x6c, sm501_system_config_index);
1385
    sm501_disp_ctrl_index = cpu_register_io_memory(sm501_disp_ctrl_readfn,
1386
                                                   sm501_disp_ctrl_writefn, s);
1387
    cpu_register_physical_memory(base + MMIO_BASE_OFFSET + SM501_DC,
1388
                                 0x1000, sm501_disp_ctrl_index);
1389
    sm501_2d_engine_index = cpu_register_io_memory(sm501_2d_engine_readfn,
1390
                                                   sm501_2d_engine_writefn, s);
1391
    cpu_register_physical_memory(base + MMIO_BASE_OFFSET + SM501_2D_ENGINE,
1392
                                 0x54, sm501_2d_engine_index);
1393

    
1394
    /* bridge to usb host emulation module */
1395
    dev = qdev_create(NULL, "sysbus-ohci");
1396
    qdev_prop_set_uint32(dev, "num-ports", 2);
1397
    qdev_prop_set_taddr(dev, "dma-offset", base);
1398
    qdev_init_nofail(dev);
1399
    sysbus_mmio_map(sysbus_from_qdev(dev), 0,
1400
                    base + MMIO_BASE_OFFSET + SM501_USB_HOST);
1401
    sysbus_connect_irq(sysbus_from_qdev(dev), 0, irq);
1402

    
1403
    /* bridge to serial emulation module */
1404
    if (chr) {
1405
#ifdef TARGET_WORDS_BIGENDIAN
1406
        serial_mm_init(base + MMIO_BASE_OFFSET + SM501_UART0, 2,
1407
                       NULL, /* TODO : chain irq to IRL */
1408
                       115200, chr, 1, 1);
1409
#else
1410
        serial_mm_init(base + MMIO_BASE_OFFSET + SM501_UART0, 2,
1411
                       NULL, /* TODO : chain irq to IRL */
1412
                       115200, chr, 1, 0);
1413
#endif
1414
    }
1415

    
1416
    /* create qemu graphic console */
1417
    s->ds = graphic_console_init(sm501_update_display, NULL,
1418
                                 NULL, NULL, s);
1419
}