Statistics
| Branch: | Revision:

root / hw / sm501.c @ 7b9cbadb

History | View | Annotate | Download (33.4 kB)

1 ffd39257 blueswir1
/*
2 ffd39257 blueswir1
 * QEMU SM501 Device
3 ffd39257 blueswir1
 *
4 ffd39257 blueswir1
 * Copyright (c) 2008 Shin-ichiro KAWASAKI
5 ffd39257 blueswir1
 *
6 ffd39257 blueswir1
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 ffd39257 blueswir1
 * of this software and associated documentation files (the "Software"), to deal
8 ffd39257 blueswir1
 * in the Software without restriction, including without limitation the rights
9 ffd39257 blueswir1
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 ffd39257 blueswir1
 * copies of the Software, and to permit persons to whom the Software is
11 ffd39257 blueswir1
 * furnished to do so, subject to the following conditions:
12 ffd39257 blueswir1
 *
13 ffd39257 blueswir1
 * The above copyright notice and this permission notice shall be included in
14 ffd39257 blueswir1
 * all copies or substantial portions of the Software.
15 ffd39257 blueswir1
 *
16 ffd39257 blueswir1
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 ffd39257 blueswir1
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 ffd39257 blueswir1
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 ffd39257 blueswir1
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 ffd39257 blueswir1
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 ffd39257 blueswir1
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 ffd39257 blueswir1
 * THE SOFTWARE.
23 ffd39257 blueswir1
 */
24 ffd39257 blueswir1
25 ffd39257 blueswir1
#include <stdio.h>
26 ffd39257 blueswir1
#include "hw.h"
27 ffd39257 blueswir1
#include "pc.h"
28 ffd39257 blueswir1
#include "console.h"
29 b79e1752 aurel32
#include "devices.h"
30 ffd39257 blueswir1
31 ffd39257 blueswir1
/*
32 ffd39257 blueswir1
 * Status: 2008/11/02
33 ffd39257 blueswir1
 *   - Minimum implementation for Linux console : mmio regs and CRT layer.
34 ffd39257 blueswir1
 *   - Always updates full screen.
35 ffd39257 blueswir1
 *
36 ffd39257 blueswir1
 * TODO:
37 ffd39257 blueswir1
 *   - Panel support
38 ffd39257 blueswir1
 *   - Hardware cursor support
39 ffd39257 blueswir1
 *   - Touch panel support
40 ffd39257 blueswir1
 *   - USB support
41 ffd39257 blueswir1
 *   - UART support
42 ffd39257 blueswir1
 *   - Performance tuning
43 ffd39257 blueswir1
 */
44 ffd39257 blueswir1
45 ffd39257 blueswir1
//#define DEBUG_SM501
46 ffd39257 blueswir1
//#define DEBUG_BITBLT
47 ffd39257 blueswir1
48 ffd39257 blueswir1
#ifdef DEBUG_SM501
49 001faf32 Blue Swirl
#define SM501_DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
50 ffd39257 blueswir1
#else
51 001faf32 Blue Swirl
#define SM501_DPRINTF(fmt, ...) do {} while(0)
52 ffd39257 blueswir1
#endif
53 ffd39257 blueswir1
54 ffd39257 blueswir1
55 ffd39257 blueswir1
#define MMIO_BASE_OFFSET 0x3e00000
56 ffd39257 blueswir1
57 ffd39257 blueswir1
/* SM501 register definitions taken from "linux/include/linux/sm501-regs.h" */
58 ffd39257 blueswir1
59 ffd39257 blueswir1
/* System Configuration area */
60 ffd39257 blueswir1
/* System config base */
61 ffd39257 blueswir1
#define SM501_SYS_CONFIG                (0x000000)
62 ffd39257 blueswir1
63 ffd39257 blueswir1
/* config 1 */
64 ffd39257 blueswir1
#define SM501_SYSTEM_CONTROL                 (0x000000)
65 ffd39257 blueswir1
66 ffd39257 blueswir1
#define SM501_SYSCTRL_PANEL_TRISTATE        (1<<0)
67 ffd39257 blueswir1
#define SM501_SYSCTRL_MEM_TRISTATE        (1<<1)
68 ffd39257 blueswir1
#define SM501_SYSCTRL_CRT_TRISTATE        (1<<2)
69 ffd39257 blueswir1
70 ffd39257 blueswir1
#define SM501_SYSCTRL_PCI_SLAVE_BURST_MASK (3<<4)
71 ffd39257 blueswir1
#define SM501_SYSCTRL_PCI_SLAVE_BURST_1        (0<<4)
72 ffd39257 blueswir1
#define SM501_SYSCTRL_PCI_SLAVE_BURST_2        (1<<4)
73 ffd39257 blueswir1
#define SM501_SYSCTRL_PCI_SLAVE_BURST_4        (2<<4)
74 ffd39257 blueswir1
#define SM501_SYSCTRL_PCI_SLAVE_BURST_8        (3<<4)
75 ffd39257 blueswir1
76 ffd39257 blueswir1
#define SM501_SYSCTRL_PCI_CLOCK_RUN_EN        (1<<6)
77 ffd39257 blueswir1
#define SM501_SYSCTRL_PCI_RETRY_DISABLE        (1<<7)
78 ffd39257 blueswir1
#define SM501_SYSCTRL_PCI_SUBSYS_LOCK        (1<<11)
79 ffd39257 blueswir1
#define SM501_SYSCTRL_PCI_BURST_READ_EN        (1<<15)
80 ffd39257 blueswir1
81 ffd39257 blueswir1
/* miscellaneous control */
82 ffd39257 blueswir1
83 ffd39257 blueswir1
#define SM501_MISC_CONTROL                (0x000004)
84 ffd39257 blueswir1
85 ffd39257 blueswir1
#define SM501_MISC_BUS_SH                (0x0)
86 ffd39257 blueswir1
#define SM501_MISC_BUS_PCI                (0x1)
87 ffd39257 blueswir1
#define SM501_MISC_BUS_XSCALE                (0x2)
88 ffd39257 blueswir1
#define SM501_MISC_BUS_NEC                (0x6)
89 ffd39257 blueswir1
#define SM501_MISC_BUS_MASK                (0x7)
90 ffd39257 blueswir1
91 ffd39257 blueswir1
#define SM501_MISC_VR_62MB                (1<<3)
92 ffd39257 blueswir1
#define SM501_MISC_CDR_RESET                (1<<7)
93 ffd39257 blueswir1
#define SM501_MISC_USB_LB                (1<<8)
94 ffd39257 blueswir1
#define SM501_MISC_USB_SLAVE                (1<<9)
95 ffd39257 blueswir1
#define SM501_MISC_BL_1                        (1<<10)
96 ffd39257 blueswir1
#define SM501_MISC_MC                        (1<<11)
97 ffd39257 blueswir1
#define SM501_MISC_DAC_POWER                (1<<12)
98 ffd39257 blueswir1
#define SM501_MISC_IRQ_INVERT                (1<<16)
99 ffd39257 blueswir1
#define SM501_MISC_SH                        (1<<17)
100 ffd39257 blueswir1
101 ffd39257 blueswir1
#define SM501_MISC_HOLD_EMPTY                (0<<18)
102 ffd39257 blueswir1
#define SM501_MISC_HOLD_8                (1<<18)
103 ffd39257 blueswir1
#define SM501_MISC_HOLD_16                (2<<18)
104 ffd39257 blueswir1
#define SM501_MISC_HOLD_24                (3<<18)
105 ffd39257 blueswir1
#define SM501_MISC_HOLD_32                (4<<18)
106 ffd39257 blueswir1
#define SM501_MISC_HOLD_MASK                (7<<18)
107 ffd39257 blueswir1
108 ffd39257 blueswir1
#define SM501_MISC_FREQ_12                (1<<24)
109 ffd39257 blueswir1
#define SM501_MISC_PNL_24BIT                (1<<25)
110 ffd39257 blueswir1
#define SM501_MISC_8051_LE                (1<<26)
111 ffd39257 blueswir1
112 ffd39257 blueswir1
113 ffd39257 blueswir1
114 ffd39257 blueswir1
#define SM501_GPIO31_0_CONTROL                (0x000008)
115 ffd39257 blueswir1
#define SM501_GPIO63_32_CONTROL                (0x00000C)
116 ffd39257 blueswir1
#define SM501_DRAM_CONTROL                (0x000010)
117 ffd39257 blueswir1
118 ffd39257 blueswir1
/* command list */
119 ffd39257 blueswir1
#define SM501_ARBTRTN_CONTROL                (0x000014)
120 ffd39257 blueswir1
121 ffd39257 blueswir1
/* command list */
122 ffd39257 blueswir1
#define SM501_COMMAND_LIST_STATUS        (0x000024)
123 ffd39257 blueswir1
124 ffd39257 blueswir1
/* interrupt debug */
125 ffd39257 blueswir1
#define SM501_RAW_IRQ_STATUS                (0x000028)
126 ffd39257 blueswir1
#define SM501_RAW_IRQ_CLEAR                (0x000028)
127 ffd39257 blueswir1
#define SM501_IRQ_STATUS                (0x00002C)
128 ffd39257 blueswir1
#define SM501_IRQ_MASK                        (0x000030)
129 ffd39257 blueswir1
#define SM501_DEBUG_CONTROL                (0x000034)
130 ffd39257 blueswir1
131 ffd39257 blueswir1
/* power management */
132 ffd39257 blueswir1
#define SM501_POWERMODE_P2X_SRC                (1<<29)
133 ffd39257 blueswir1
#define SM501_POWERMODE_V2X_SRC                (1<<20)
134 ffd39257 blueswir1
#define SM501_POWERMODE_M_SRC                (1<<12)
135 ffd39257 blueswir1
#define SM501_POWERMODE_M1_SRC                (1<<4)
136 ffd39257 blueswir1
137 ffd39257 blueswir1
#define SM501_CURRENT_GATE                (0x000038)
138 ffd39257 blueswir1
#define SM501_CURRENT_CLOCK                (0x00003C)
139 ffd39257 blueswir1
#define SM501_POWER_MODE_0_GATE                (0x000040)
140 ffd39257 blueswir1
#define SM501_POWER_MODE_0_CLOCK        (0x000044)
141 ffd39257 blueswir1
#define SM501_POWER_MODE_1_GATE                (0x000048)
142 ffd39257 blueswir1
#define SM501_POWER_MODE_1_CLOCK        (0x00004C)
143 ffd39257 blueswir1
#define SM501_SLEEP_MODE_GATE                (0x000050)
144 ffd39257 blueswir1
#define SM501_POWER_MODE_CONTROL        (0x000054)
145 ffd39257 blueswir1
146 ffd39257 blueswir1
/* power gates for units within the 501 */
147 ffd39257 blueswir1
#define SM501_GATE_HOST                        (0)
148 ffd39257 blueswir1
#define SM501_GATE_MEMORY                (1)
149 ffd39257 blueswir1
#define SM501_GATE_DISPLAY                (2)
150 ffd39257 blueswir1
#define SM501_GATE_2D_ENGINE                (3)
151 ffd39257 blueswir1
#define SM501_GATE_CSC                        (4)
152 ffd39257 blueswir1
#define SM501_GATE_ZVPORT                (5)
153 ffd39257 blueswir1
#define SM501_GATE_GPIO                        (6)
154 ffd39257 blueswir1
#define SM501_GATE_UART0                (7)
155 ffd39257 blueswir1
#define SM501_GATE_UART1                (8)
156 ffd39257 blueswir1
#define SM501_GATE_SSP                        (10)
157 ffd39257 blueswir1
#define SM501_GATE_USB_HOST                (11)
158 ffd39257 blueswir1
#define SM501_GATE_USB_GADGET                (12)
159 ffd39257 blueswir1
#define SM501_GATE_UCONTROLLER                (17)
160 ffd39257 blueswir1
#define SM501_GATE_AC97                        (18)
161 ffd39257 blueswir1
162 ffd39257 blueswir1
/* panel clock */
163 ffd39257 blueswir1
#define SM501_CLOCK_P2XCLK                (24)
164 ffd39257 blueswir1
/* crt clock */
165 ffd39257 blueswir1
#define SM501_CLOCK_V2XCLK                (16)
166 ffd39257 blueswir1
/* main clock */
167 ffd39257 blueswir1
#define SM501_CLOCK_MCLK                (8)
168 ffd39257 blueswir1
/* SDRAM controller clock */
169 ffd39257 blueswir1
#define SM501_CLOCK_M1XCLK                (0)
170 ffd39257 blueswir1
171 ffd39257 blueswir1
/* config 2 */
172 ffd39257 blueswir1
#define SM501_PCI_MASTER_BASE                (0x000058)
173 ffd39257 blueswir1
#define SM501_ENDIAN_CONTROL                (0x00005C)
174 ffd39257 blueswir1
#define SM501_DEVICEID                        (0x000060)
175 ffd39257 blueswir1
/* 0x050100A0 */
176 ffd39257 blueswir1
177 ffd39257 blueswir1
#define SM501_DEVICEID_SM501                (0x05010000)
178 ffd39257 blueswir1
#define SM501_DEVICEID_IDMASK                (0xffff0000)
179 ffd39257 blueswir1
#define SM501_DEVICEID_REVMASK                (0x000000ff)
180 ffd39257 blueswir1
181 ffd39257 blueswir1
#define SM501_PLLCLOCK_COUNT                (0x000064)
182 ffd39257 blueswir1
#define SM501_MISC_TIMING                (0x000068)
183 ffd39257 blueswir1
#define SM501_CURRENT_SDRAM_CLOCK        (0x00006C)
184 ffd39257 blueswir1
185 ffd39257 blueswir1
#define SM501_PROGRAMMABLE_PLL_CONTROL        (0x000074)
186 ffd39257 blueswir1
187 ffd39257 blueswir1
/* GPIO base */
188 ffd39257 blueswir1
#define SM501_GPIO                        (0x010000)
189 ffd39257 blueswir1
#define SM501_GPIO_DATA_LOW                (0x00)
190 ffd39257 blueswir1
#define SM501_GPIO_DATA_HIGH                (0x04)
191 ffd39257 blueswir1
#define SM501_GPIO_DDR_LOW                (0x08)
192 ffd39257 blueswir1
#define SM501_GPIO_DDR_HIGH                (0x0C)
193 ffd39257 blueswir1
#define SM501_GPIO_IRQ_SETUP                (0x10)
194 ffd39257 blueswir1
#define SM501_GPIO_IRQ_STATUS                (0x14)
195 ffd39257 blueswir1
#define SM501_GPIO_IRQ_RESET                (0x14)
196 ffd39257 blueswir1
197 ffd39257 blueswir1
/* I2C controller base */
198 ffd39257 blueswir1
#define SM501_I2C                        (0x010040)
199 ffd39257 blueswir1
#define SM501_I2C_BYTE_COUNT                (0x00)
200 ffd39257 blueswir1
#define SM501_I2C_CONTROL                (0x01)
201 ffd39257 blueswir1
#define SM501_I2C_STATUS                (0x02)
202 ffd39257 blueswir1
#define SM501_I2C_RESET                        (0x02)
203 ffd39257 blueswir1
#define SM501_I2C_SLAVE_ADDRESS                (0x03)
204 ffd39257 blueswir1
#define SM501_I2C_DATA                        (0x04)
205 ffd39257 blueswir1
206 ffd39257 blueswir1
/* SSP base */
207 ffd39257 blueswir1
#define SM501_SSP                        (0x020000)
208 ffd39257 blueswir1
209 ffd39257 blueswir1
/* Uart 0 base */
210 ffd39257 blueswir1
#define SM501_UART0                        (0x030000)
211 ffd39257 blueswir1
212 ffd39257 blueswir1
/* Uart 1 base */
213 ffd39257 blueswir1
#define SM501_UART1                        (0x030020)
214 ffd39257 blueswir1
215 ffd39257 blueswir1
/* USB host port base */
216 ffd39257 blueswir1
#define SM501_USB_HOST                        (0x040000)
217 ffd39257 blueswir1
218 ffd39257 blueswir1
/* USB slave/gadget base */
219 ffd39257 blueswir1
#define SM501_USB_GADGET                (0x060000)
220 ffd39257 blueswir1
221 ffd39257 blueswir1
/* USB slave/gadget data port base */
222 ffd39257 blueswir1
#define SM501_USB_GADGET_DATA                (0x070000)
223 ffd39257 blueswir1
224 ffd39257 blueswir1
/* Display controller/video engine base */
225 ffd39257 blueswir1
#define SM501_DC                        (0x080000)
226 ffd39257 blueswir1
227 ffd39257 blueswir1
/* common defines for the SM501 address registers */
228 ffd39257 blueswir1
#define SM501_ADDR_FLIP                        (1<<31)
229 ffd39257 blueswir1
#define SM501_ADDR_EXT                        (1<<27)
230 ffd39257 blueswir1
#define SM501_ADDR_CS1                        (1<<26)
231 ffd39257 blueswir1
#define SM501_ADDR_MASK                        (0x3f << 26)
232 ffd39257 blueswir1
233 ffd39257 blueswir1
#define SM501_FIFO_MASK                        (0x3 << 16)
234 ffd39257 blueswir1
#define SM501_FIFO_1                        (0x0 << 16)
235 ffd39257 blueswir1
#define SM501_FIFO_3                        (0x1 << 16)
236 ffd39257 blueswir1
#define SM501_FIFO_7                        (0x2 << 16)
237 ffd39257 blueswir1
#define SM501_FIFO_11                        (0x3 << 16)
238 ffd39257 blueswir1
239 ffd39257 blueswir1
/* common registers for panel and the crt */
240 ffd39257 blueswir1
#define SM501_OFF_DC_H_TOT                (0x000)
241 ffd39257 blueswir1
#define SM501_OFF_DC_V_TOT                (0x008)
242 ffd39257 blueswir1
#define SM501_OFF_DC_H_SYNC                (0x004)
243 ffd39257 blueswir1
#define SM501_OFF_DC_V_SYNC                (0x00C)
244 ffd39257 blueswir1
245 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL                (0x000)
246 ffd39257 blueswir1
247 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_FPEN        (1<<27)
248 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_BIAS        (1<<26)
249 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_DATA        (1<<25)
250 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_VDD        (1<<24)
251 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_DP        (1<<23)
252 ffd39257 blueswir1
253 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_TFT_888        (0<<21)
254 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_TFT_333        (1<<21)
255 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_TFT_444        (2<<21)
256 ffd39257 blueswir1
257 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_DE        (1<<20)
258 ffd39257 blueswir1
259 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_LCD_TFT        (0<<18)
260 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_LCD_STN8        (1<<18)
261 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_LCD_STN12 (2<<18)
262 ffd39257 blueswir1
263 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_CP        (1<<14)
264 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_VSP        (1<<13)
265 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_HSP        (1<<12)
266 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_CK        (1<<9)
267 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_TE        (1<<8)
268 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_VPD        (1<<7)
269 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_VP        (1<<6)
270 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_HPD        (1<<5)
271 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_HP        (1<<4)
272 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_GAMMA        (1<<3)
273 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_EN        (1<<2)
274 ffd39257 blueswir1
275 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_8BPP        (0<<0)
276 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_16BPP        (1<<0)
277 ffd39257 blueswir1
#define SM501_DC_PANEL_CONTROL_32BPP        (2<<0)
278 ffd39257 blueswir1
279 ffd39257 blueswir1
280 ffd39257 blueswir1
#define SM501_DC_PANEL_PANNING_CONTROL        (0x004)
281 ffd39257 blueswir1
#define SM501_DC_PANEL_COLOR_KEY        (0x008)
282 ffd39257 blueswir1
#define SM501_DC_PANEL_FB_ADDR                (0x00C)
283 ffd39257 blueswir1
#define SM501_DC_PANEL_FB_OFFSET        (0x010)
284 ffd39257 blueswir1
#define SM501_DC_PANEL_FB_WIDTH                (0x014)
285 ffd39257 blueswir1
#define SM501_DC_PANEL_FB_HEIGHT        (0x018)
286 ffd39257 blueswir1
#define SM501_DC_PANEL_TL_LOC                (0x01C)
287 ffd39257 blueswir1
#define SM501_DC_PANEL_BR_LOC                (0x020)
288 ffd39257 blueswir1
#define SM501_DC_PANEL_H_TOT                (0x024)
289 ffd39257 blueswir1
#define SM501_DC_PANEL_H_SYNC                (0x028)
290 ffd39257 blueswir1
#define SM501_DC_PANEL_V_TOT                (0x02C)
291 ffd39257 blueswir1
#define SM501_DC_PANEL_V_SYNC                (0x030)
292 ffd39257 blueswir1
#define SM501_DC_PANEL_CUR_LINE                (0x034)
293 ffd39257 blueswir1
294 ffd39257 blueswir1
#define SM501_DC_VIDEO_CONTROL                (0x040)
295 ffd39257 blueswir1
#define SM501_DC_VIDEO_FB0_ADDR                (0x044)
296 ffd39257 blueswir1
#define SM501_DC_VIDEO_FB_WIDTH                (0x048)
297 ffd39257 blueswir1
#define SM501_DC_VIDEO_FB0_LAST_ADDR        (0x04C)
298 ffd39257 blueswir1
#define SM501_DC_VIDEO_TL_LOC                (0x050)
299 ffd39257 blueswir1
#define SM501_DC_VIDEO_BR_LOC                (0x054)
300 ffd39257 blueswir1
#define SM501_DC_VIDEO_SCALE                (0x058)
301 ffd39257 blueswir1
#define SM501_DC_VIDEO_INIT_SCALE        (0x05C)
302 ffd39257 blueswir1
#define SM501_DC_VIDEO_YUV_CONSTANTS        (0x060)
303 ffd39257 blueswir1
#define SM501_DC_VIDEO_FB1_ADDR                (0x064)
304 ffd39257 blueswir1
#define SM501_DC_VIDEO_FB1_LAST_ADDR        (0x068)
305 ffd39257 blueswir1
306 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_CONTROL        (0x080)
307 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_FB_ADDR        (0x084)
308 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_FB_OFFSET        (0x088)
309 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_FB_LAST_ADDR        (0x08C)
310 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_TL_LOC        (0x090)
311 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_BR_LOC        (0x094)
312 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_SCALE        (0x098)
313 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_INIT_SCALE        (0x09C)
314 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_CHROMA_KEY        (0x0A0)
315 ffd39257 blueswir1
#define SM501_DC_VIDEO_ALPHA_COLOR_LOOKUP        (0x0A4)
316 ffd39257 blueswir1
317 ffd39257 blueswir1
#define SM501_DC_PANEL_HWC_BASE                (0x0F0)
318 ffd39257 blueswir1
#define SM501_DC_PANEL_HWC_ADDR                (0x0F0)
319 ffd39257 blueswir1
#define SM501_DC_PANEL_HWC_LOC                (0x0F4)
320 ffd39257 blueswir1
#define SM501_DC_PANEL_HWC_COLOR_1_2        (0x0F8)
321 ffd39257 blueswir1
#define SM501_DC_PANEL_HWC_COLOR_3        (0x0FC)
322 ffd39257 blueswir1
323 ffd39257 blueswir1
#define SM501_HWC_EN                        (1<<31)
324 ffd39257 blueswir1
325 ffd39257 blueswir1
#define SM501_OFF_HWC_ADDR                (0x00)
326 ffd39257 blueswir1
#define SM501_OFF_HWC_LOC                (0x04)
327 ffd39257 blueswir1
#define SM501_OFF_HWC_COLOR_1_2                (0x08)
328 ffd39257 blueswir1
#define SM501_OFF_HWC_COLOR_3                (0x0C)
329 ffd39257 blueswir1
330 ffd39257 blueswir1
#define SM501_DC_ALPHA_CONTROL                (0x100)
331 ffd39257 blueswir1
#define SM501_DC_ALPHA_FB_ADDR                (0x104)
332 ffd39257 blueswir1
#define SM501_DC_ALPHA_FB_OFFSET        (0x108)
333 ffd39257 blueswir1
#define SM501_DC_ALPHA_TL_LOC                (0x10C)
334 ffd39257 blueswir1
#define SM501_DC_ALPHA_BR_LOC                (0x110)
335 ffd39257 blueswir1
#define SM501_DC_ALPHA_CHROMA_KEY        (0x114)
336 ffd39257 blueswir1
#define SM501_DC_ALPHA_COLOR_LOOKUP        (0x118)
337 ffd39257 blueswir1
338 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL                (0x200)
339 ffd39257 blueswir1
340 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_TVP        (1<<15)
341 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_CP                (1<<14)
342 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_VSP        (1<<13)
343 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_HSP        (1<<12)
344 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_VS                (1<<11)
345 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_BLANK        (1<<10)
346 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_SEL        (1<<9)
347 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_TE                (1<<8)
348 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_PIXEL_MASK (0xF << 4)
349 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_GAMMA        (1<<3)
350 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_ENABLE        (1<<2)
351 ffd39257 blueswir1
352 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_8BPP        (0<<0)
353 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_16BPP        (1<<0)
354 ffd39257 blueswir1
#define SM501_DC_CRT_CONTROL_32BPP        (2<<0)
355 ffd39257 blueswir1
356 ffd39257 blueswir1
#define SM501_DC_CRT_FB_ADDR                (0x204)
357 ffd39257 blueswir1
#define SM501_DC_CRT_FB_OFFSET                (0x208)
358 ffd39257 blueswir1
#define SM501_DC_CRT_H_TOT                (0x20C)
359 ffd39257 blueswir1
#define SM501_DC_CRT_H_SYNC                (0x210)
360 ffd39257 blueswir1
#define SM501_DC_CRT_V_TOT                (0x214)
361 ffd39257 blueswir1
#define SM501_DC_CRT_V_SYNC                (0x218)
362 ffd39257 blueswir1
#define SM501_DC_CRT_SIGNATURE_ANALYZER        (0x21C)
363 ffd39257 blueswir1
#define SM501_DC_CRT_CUR_LINE                (0x220)
364 ffd39257 blueswir1
#define SM501_DC_CRT_MONITOR_DETECT        (0x224)
365 ffd39257 blueswir1
366 ffd39257 blueswir1
#define SM501_DC_CRT_HWC_BASE                (0x230)
367 ffd39257 blueswir1
#define SM501_DC_CRT_HWC_ADDR                (0x230)
368 ffd39257 blueswir1
#define SM501_DC_CRT_HWC_LOC                (0x234)
369 ffd39257 blueswir1
#define SM501_DC_CRT_HWC_COLOR_1_2        (0x238)
370 ffd39257 blueswir1
#define SM501_DC_CRT_HWC_COLOR_3        (0x23C)
371 ffd39257 blueswir1
372 ffd39257 blueswir1
#define SM501_DC_PANEL_PALETTE                (0x400)
373 ffd39257 blueswir1
374 ffd39257 blueswir1
#define SM501_DC_VIDEO_PALETTE                (0x800)
375 ffd39257 blueswir1
376 ffd39257 blueswir1
#define SM501_DC_CRT_PALETTE                (0xC00)
377 ffd39257 blueswir1
378 ffd39257 blueswir1
/* Zoom Video port base */
379 ffd39257 blueswir1
#define SM501_ZVPORT                        (0x090000)
380 ffd39257 blueswir1
381 ffd39257 blueswir1
/* AC97/I2S base */
382 ffd39257 blueswir1
#define SM501_AC97                        (0x0A0000)
383 ffd39257 blueswir1
384 ffd39257 blueswir1
/* 8051 micro controller base */
385 ffd39257 blueswir1
#define SM501_UCONTROLLER                (0x0B0000)
386 ffd39257 blueswir1
387 ffd39257 blueswir1
/* 8051 micro controller SRAM base */
388 ffd39257 blueswir1
#define SM501_UCONTROLLER_SRAM                (0x0C0000)
389 ffd39257 blueswir1
390 ffd39257 blueswir1
/* DMA base */
391 ffd39257 blueswir1
#define SM501_DMA                        (0x0D0000)
392 ffd39257 blueswir1
393 ffd39257 blueswir1
/* 2d engine base */
394 ffd39257 blueswir1
#define SM501_2D_ENGINE                        (0x100000)
395 ffd39257 blueswir1
#define SM501_2D_SOURCE                        (0x00)
396 ffd39257 blueswir1
#define SM501_2D_DESTINATION                (0x04)
397 ffd39257 blueswir1
#define SM501_2D_DIMENSION                (0x08)
398 ffd39257 blueswir1
#define SM501_2D_CONTROL                (0x0C)
399 ffd39257 blueswir1
#define SM501_2D_PITCH                        (0x10)
400 ffd39257 blueswir1
#define SM501_2D_FOREGROUND                (0x14)
401 ffd39257 blueswir1
#define SM501_2D_BACKGROUND                (0x18)
402 ffd39257 blueswir1
#define SM501_2D_STRETCH                (0x1C)
403 ffd39257 blueswir1
#define SM501_2D_COLOR_COMPARE                (0x20)
404 ffd39257 blueswir1
#define SM501_2D_COLOR_COMPARE_MASK         (0x24)
405 ffd39257 blueswir1
#define SM501_2D_MASK                        (0x28)
406 ffd39257 blueswir1
#define SM501_2D_CLIP_TL                (0x2C)
407 ffd39257 blueswir1
#define SM501_2D_CLIP_BR                (0x30)
408 ffd39257 blueswir1
#define SM501_2D_MONO_PATTERN_LOW        (0x34)
409 ffd39257 blueswir1
#define SM501_2D_MONO_PATTERN_HIGH        (0x38)
410 ffd39257 blueswir1
#define SM501_2D_WINDOW_WIDTH                (0x3C)
411 ffd39257 blueswir1
#define SM501_2D_SOURCE_BASE                (0x40)
412 ffd39257 blueswir1
#define SM501_2D_DESTINATION_BASE        (0x44)
413 ffd39257 blueswir1
#define SM501_2D_ALPHA                        (0x48)
414 ffd39257 blueswir1
#define SM501_2D_WRAP                        (0x4C)
415 ffd39257 blueswir1
#define SM501_2D_STATUS                        (0x50)
416 ffd39257 blueswir1
417 ffd39257 blueswir1
#define SM501_CSC_Y_SOURCE_BASE                (0xC8)
418 ffd39257 blueswir1
#define SM501_CSC_CONSTANTS                (0xCC)
419 ffd39257 blueswir1
#define SM501_CSC_Y_SOURCE_X                (0xD0)
420 ffd39257 blueswir1
#define SM501_CSC_Y_SOURCE_Y                (0xD4)
421 ffd39257 blueswir1
#define SM501_CSC_U_SOURCE_BASE                (0xD8)
422 ffd39257 blueswir1
#define SM501_CSC_V_SOURCE_BASE                (0xDC)
423 ffd39257 blueswir1
#define SM501_CSC_SOURCE_DIMENSION        (0xE0)
424 ffd39257 blueswir1
#define SM501_CSC_SOURCE_PITCH                (0xE4)
425 ffd39257 blueswir1
#define SM501_CSC_DESTINATION                (0xE8)
426 ffd39257 blueswir1
#define SM501_CSC_DESTINATION_DIMENSION        (0xEC)
427 ffd39257 blueswir1
#define SM501_CSC_DESTINATION_PITCH        (0xF0)
428 ffd39257 blueswir1
#define SM501_CSC_SCALE_FACTOR                (0xF4)
429 ffd39257 blueswir1
#define SM501_CSC_DESTINATION_BASE        (0xF8)
430 ffd39257 blueswir1
#define SM501_CSC_CONTROL                (0xFC)
431 ffd39257 blueswir1
432 ffd39257 blueswir1
/* 2d engine data port base */
433 ffd39257 blueswir1
#define SM501_2D_ENGINE_DATA                (0x110000)
434 ffd39257 blueswir1
435 ffd39257 blueswir1
/* end of register definitions */
436 ffd39257 blueswir1
437 0a4e7cd2 Shin-ichiro KAWASAKI
#define SM501_HWC_WIDTH                       (64)
438 0a4e7cd2 Shin-ichiro KAWASAKI
#define SM501_HWC_HEIGHT                      (64)
439 ffd39257 blueswir1
440 ffd39257 blueswir1
/* SM501 local memory size taken from "linux/drivers/mfd/sm501.c" */
441 ffd39257 blueswir1
static const uint32_t sm501_mem_local_size[] = {
442 ffd39257 blueswir1
        [0]        = 4*1024*1024,
443 ffd39257 blueswir1
        [1]        = 8*1024*1024,
444 ffd39257 blueswir1
        [2]        = 16*1024*1024,
445 ffd39257 blueswir1
        [3]        = 32*1024*1024,
446 ffd39257 blueswir1
        [4]        = 64*1024*1024,
447 ffd39257 blueswir1
        [5]        = 2*1024*1024,
448 ffd39257 blueswir1
};
449 ffd39257 blueswir1
#define get_local_mem_size(s) sm501_mem_local_size[(s)->local_mem_size_index]
450 ffd39257 blueswir1
451 ffd39257 blueswir1
typedef struct SM501State {
452 ffd39257 blueswir1
    /* graphic console status */
453 ffd39257 blueswir1
    DisplayState *ds;
454 ffd39257 blueswir1
455 ffd39257 blueswir1
    /* status & internal resources */
456 c227f099 Anthony Liguori
    target_phys_addr_t base;
457 ffd39257 blueswir1
    uint32_t local_mem_size_index;
458 ffd39257 blueswir1
    uint8_t * local_mem;
459 c227f099 Anthony Liguori
    ram_addr_t local_mem_offset;
460 ffd39257 blueswir1
    uint32_t last_width;
461 ffd39257 blueswir1
    uint32_t last_height;
462 ffd39257 blueswir1
463 ffd39257 blueswir1
    /* mmio registers */
464 ffd39257 blueswir1
    uint32_t system_control;
465 ffd39257 blueswir1
    uint32_t misc_control;
466 ffd39257 blueswir1
    uint32_t gpio_31_0_control;
467 ffd39257 blueswir1
    uint32_t gpio_63_32_control;
468 ffd39257 blueswir1
    uint32_t dram_control;
469 ffd39257 blueswir1
    uint32_t irq_mask;
470 ffd39257 blueswir1
    uint32_t misc_timing;
471 ffd39257 blueswir1
    uint32_t power_mode_control;
472 ffd39257 blueswir1
473 ffd39257 blueswir1
    uint32_t uart0_ier;
474 ffd39257 blueswir1
    uint32_t uart0_lcr;
475 ffd39257 blueswir1
    uint32_t uart0_mcr;
476 ffd39257 blueswir1
    uint32_t uart0_scr;
477 ffd39257 blueswir1
478 ffd39257 blueswir1
    uint8_t dc_palette[0x400 * 3];
479 ffd39257 blueswir1
480 ffd39257 blueswir1
    uint32_t dc_panel_control;
481 ffd39257 blueswir1
    uint32_t dc_panel_panning_control;
482 ffd39257 blueswir1
    uint32_t dc_panel_fb_addr;
483 ffd39257 blueswir1
    uint32_t dc_panel_fb_offset;
484 ffd39257 blueswir1
    uint32_t dc_panel_fb_width;
485 ffd39257 blueswir1
    uint32_t dc_panel_fb_height;
486 ffd39257 blueswir1
    uint32_t dc_panel_tl_location;
487 ffd39257 blueswir1
    uint32_t dc_panel_br_location;
488 ffd39257 blueswir1
    uint32_t dc_panel_h_total;
489 ffd39257 blueswir1
    uint32_t dc_panel_h_sync;
490 ffd39257 blueswir1
    uint32_t dc_panel_v_total;
491 ffd39257 blueswir1
    uint32_t dc_panel_v_sync;
492 ffd39257 blueswir1
493 ffd39257 blueswir1
    uint32_t dc_panel_hwc_addr;
494 ffd39257 blueswir1
    uint32_t dc_panel_hwc_location;
495 ffd39257 blueswir1
    uint32_t dc_panel_hwc_color_1_2;
496 ffd39257 blueswir1
    uint32_t dc_panel_hwc_color_3;
497 ffd39257 blueswir1
498 ffd39257 blueswir1
    uint32_t dc_crt_control;
499 ffd39257 blueswir1
    uint32_t dc_crt_fb_addr;
500 ffd39257 blueswir1
    uint32_t dc_crt_fb_offset;
501 ffd39257 blueswir1
    uint32_t dc_crt_h_total;
502 ffd39257 blueswir1
    uint32_t dc_crt_h_sync;
503 ffd39257 blueswir1
    uint32_t dc_crt_v_total;
504 ffd39257 blueswir1
    uint32_t dc_crt_v_sync;
505 ffd39257 blueswir1
506 ffd39257 blueswir1
    uint32_t dc_crt_hwc_addr;
507 ffd39257 blueswir1
    uint32_t dc_crt_hwc_location;
508 ffd39257 blueswir1
    uint32_t dc_crt_hwc_color_1_2;
509 ffd39257 blueswir1
    uint32_t dc_crt_hwc_color_3;
510 ffd39257 blueswir1
511 ffd39257 blueswir1
} SM501State;
512 ffd39257 blueswir1
513 ffd39257 blueswir1
static uint32_t get_local_mem_size_index(uint32_t size)
514 ffd39257 blueswir1
{
515 ffd39257 blueswir1
    uint32_t norm_size = 0;
516 ffd39257 blueswir1
    int i, index = 0;
517 ffd39257 blueswir1
518 b1503cda malc
    for (i = 0; i < ARRAY_SIZE(sm501_mem_local_size); i++) {
519 ffd39257 blueswir1
        uint32_t new_size = sm501_mem_local_size[i];
520 ffd39257 blueswir1
        if (new_size >= size) {
521 ffd39257 blueswir1
            if (norm_size == 0 || norm_size > new_size) {
522 ffd39257 blueswir1
                norm_size = new_size;
523 ffd39257 blueswir1
                index = i;
524 ffd39257 blueswir1
            }
525 ffd39257 blueswir1
        }
526 ffd39257 blueswir1
    }
527 ffd39257 blueswir1
528 ffd39257 blueswir1
    return index;
529 ffd39257 blueswir1
}
530 ffd39257 blueswir1
531 0a4e7cd2 Shin-ichiro KAWASAKI
/**
532 0a4e7cd2 Shin-ichiro KAWASAKI
 * Check the availability of hardware cursor.
533 0a4e7cd2 Shin-ichiro KAWASAKI
 * @param crt  0 for PANEL, 1 for CRT.
534 0a4e7cd2 Shin-ichiro KAWASAKI
 */
535 0a4e7cd2 Shin-ichiro KAWASAKI
static inline int is_hwc_enabled(SM501State *state, int crt)
536 0a4e7cd2 Shin-ichiro KAWASAKI
{
537 0a4e7cd2 Shin-ichiro KAWASAKI
    uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
538 0a4e7cd2 Shin-ichiro KAWASAKI
    return addr & 0x80000000;
539 0a4e7cd2 Shin-ichiro KAWASAKI
}
540 0a4e7cd2 Shin-ichiro KAWASAKI
541 0a4e7cd2 Shin-ichiro KAWASAKI
/**
542 0a4e7cd2 Shin-ichiro KAWASAKI
 * Get the address which holds cursor pattern data.
543 0a4e7cd2 Shin-ichiro KAWASAKI
 * @param crt  0 for PANEL, 1 for CRT.
544 0a4e7cd2 Shin-ichiro KAWASAKI
 */
545 0a4e7cd2 Shin-ichiro KAWASAKI
static inline uint32_t get_hwc_address(SM501State *state, int crt)
546 0a4e7cd2 Shin-ichiro KAWASAKI
{
547 0a4e7cd2 Shin-ichiro KAWASAKI
    uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
548 0a4e7cd2 Shin-ichiro KAWASAKI
    return (addr & 0x03FFFFF0)/* >> 4*/;
549 0a4e7cd2 Shin-ichiro KAWASAKI
}
550 0a4e7cd2 Shin-ichiro KAWASAKI
551 0a4e7cd2 Shin-ichiro KAWASAKI
/**
552 0a4e7cd2 Shin-ichiro KAWASAKI
 * Get the cursor position in y coordinate.
553 0a4e7cd2 Shin-ichiro KAWASAKI
 * @param crt  0 for PANEL, 1 for CRT.
554 0a4e7cd2 Shin-ichiro KAWASAKI
 */
555 0a4e7cd2 Shin-ichiro KAWASAKI
static inline uint32_t get_hwc_y(SM501State *state, int crt)
556 0a4e7cd2 Shin-ichiro KAWASAKI
{
557 0a4e7cd2 Shin-ichiro KAWASAKI
    uint32_t location = crt ? state->dc_crt_hwc_location
558 0a4e7cd2 Shin-ichiro KAWASAKI
                            : state->dc_panel_hwc_location;
559 0a4e7cd2 Shin-ichiro KAWASAKI
    return (location & 0x07FF0000) >> 16;
560 0a4e7cd2 Shin-ichiro KAWASAKI
}
561 0a4e7cd2 Shin-ichiro KAWASAKI
562 0a4e7cd2 Shin-ichiro KAWASAKI
/**
563 0a4e7cd2 Shin-ichiro KAWASAKI
 * Get the cursor position in x coordinate.
564 0a4e7cd2 Shin-ichiro KAWASAKI
 * @param crt  0 for PANEL, 1 for CRT.
565 0a4e7cd2 Shin-ichiro KAWASAKI
 */
566 0a4e7cd2 Shin-ichiro KAWASAKI
static inline uint32_t get_hwc_x(SM501State *state, int crt)
567 0a4e7cd2 Shin-ichiro KAWASAKI
{
568 0a4e7cd2 Shin-ichiro KAWASAKI
    uint32_t location = crt ? state->dc_crt_hwc_location
569 0a4e7cd2 Shin-ichiro KAWASAKI
                            : state->dc_panel_hwc_location;
570 0a4e7cd2 Shin-ichiro KAWASAKI
    return location & 0x000007FF;
571 0a4e7cd2 Shin-ichiro KAWASAKI
}
572 0a4e7cd2 Shin-ichiro KAWASAKI
573 0a4e7cd2 Shin-ichiro KAWASAKI
/**
574 0a4e7cd2 Shin-ichiro KAWASAKI
 * Get the cursor position in x coordinate.
575 0a4e7cd2 Shin-ichiro KAWASAKI
 * @param crt  0 for PANEL, 1 for CRT.
576 0a4e7cd2 Shin-ichiro KAWASAKI
 * @param index  0, 1, 2 or 3 which specifies color of corsor dot.
577 0a4e7cd2 Shin-ichiro KAWASAKI
 */
578 0a4e7cd2 Shin-ichiro KAWASAKI
static inline uint16_t get_hwc_color(SM501State *state, int crt, int index)
579 0a4e7cd2 Shin-ichiro KAWASAKI
{
580 0a4e7cd2 Shin-ichiro KAWASAKI
    uint16_t color_reg = 0;
581 0a4e7cd2 Shin-ichiro KAWASAKI
    uint16_t color_565 = 0;
582 0a4e7cd2 Shin-ichiro KAWASAKI
583 0a4e7cd2 Shin-ichiro KAWASAKI
    if (index == 0) {
584 0a4e7cd2 Shin-ichiro KAWASAKI
        return 0;
585 0a4e7cd2 Shin-ichiro KAWASAKI
    }
586 0a4e7cd2 Shin-ichiro KAWASAKI
587 0a4e7cd2 Shin-ichiro KAWASAKI
    switch (index) {
588 0a4e7cd2 Shin-ichiro KAWASAKI
    case 1:
589 0a4e7cd2 Shin-ichiro KAWASAKI
    case 2:
590 0a4e7cd2 Shin-ichiro KAWASAKI
        color_reg = crt ? state->dc_crt_hwc_color_1_2
591 0a4e7cd2 Shin-ichiro KAWASAKI
                        : state->dc_panel_hwc_color_1_2;
592 0a4e7cd2 Shin-ichiro KAWASAKI
        break;
593 0a4e7cd2 Shin-ichiro KAWASAKI
    case 3:
594 0a4e7cd2 Shin-ichiro KAWASAKI
        color_reg = crt ? state->dc_crt_hwc_color_3
595 0a4e7cd2 Shin-ichiro KAWASAKI
                        : state->dc_panel_hwc_color_3;
596 0a4e7cd2 Shin-ichiro KAWASAKI
        break;
597 0a4e7cd2 Shin-ichiro KAWASAKI
    default:
598 0a4e7cd2 Shin-ichiro KAWASAKI
        printf("invalid hw cursor color.\n");
599 0a4e7cd2 Shin-ichiro KAWASAKI
        assert(0);
600 0a4e7cd2 Shin-ichiro KAWASAKI
    }
601 0a4e7cd2 Shin-ichiro KAWASAKI
602 0a4e7cd2 Shin-ichiro KAWASAKI
    switch (index) {
603 0a4e7cd2 Shin-ichiro KAWASAKI
    case 1:
604 0a4e7cd2 Shin-ichiro KAWASAKI
    case 3:
605 0a4e7cd2 Shin-ichiro KAWASAKI
        color_565 = (uint16_t)(color_reg & 0xFFFF);
606 0a4e7cd2 Shin-ichiro KAWASAKI
        break;
607 0a4e7cd2 Shin-ichiro KAWASAKI
    case 2:
608 0a4e7cd2 Shin-ichiro KAWASAKI
        color_565 = (uint16_t)((color_reg >> 16) & 0xFFFF);
609 0a4e7cd2 Shin-ichiro KAWASAKI
        break;
610 0a4e7cd2 Shin-ichiro KAWASAKI
    }
611 0a4e7cd2 Shin-ichiro KAWASAKI
    return color_565;
612 0a4e7cd2 Shin-ichiro KAWASAKI
}
613 0a4e7cd2 Shin-ichiro KAWASAKI
614 0a4e7cd2 Shin-ichiro KAWASAKI
static int within_hwc_y_range(SM501State *state, int y, int crt)
615 0a4e7cd2 Shin-ichiro KAWASAKI
{
616 0a4e7cd2 Shin-ichiro KAWASAKI
    int hwc_y = get_hwc_y(state, crt);
617 0a4e7cd2 Shin-ichiro KAWASAKI
    return (hwc_y <= y && y < hwc_y + SM501_HWC_HEIGHT);
618 0a4e7cd2 Shin-ichiro KAWASAKI
}
619 0a4e7cd2 Shin-ichiro KAWASAKI
620 c227f099 Anthony Liguori
static uint32_t sm501_system_config_read(void *opaque, target_phys_addr_t addr)
621 ffd39257 blueswir1
{
622 ffd39257 blueswir1
    SM501State * s = (SM501State *)opaque;
623 ffd39257 blueswir1
    uint32_t ret = 0;
624 8da3ff18 pbrook
    SM501_DPRINTF("sm501 system config regs : read addr=%x\n", (int)addr);
625 ffd39257 blueswir1
626 8da3ff18 pbrook
    switch(addr) {
627 ffd39257 blueswir1
    case SM501_SYSTEM_CONTROL:
628 ffd39257 blueswir1
        ret = s->system_control;
629 ffd39257 blueswir1
        break;
630 ffd39257 blueswir1
    case SM501_MISC_CONTROL:
631 ffd39257 blueswir1
        ret = s->misc_control;
632 ffd39257 blueswir1
        break;
633 ffd39257 blueswir1
    case SM501_GPIO31_0_CONTROL:
634 ffd39257 blueswir1
        ret = s->gpio_31_0_control;
635 ffd39257 blueswir1
        break;
636 ffd39257 blueswir1
    case SM501_GPIO63_32_CONTROL:
637 ffd39257 blueswir1
        ret = s->gpio_63_32_control;
638 ffd39257 blueswir1
        break;
639 ffd39257 blueswir1
    case SM501_DEVICEID:
640 ffd39257 blueswir1
        ret = 0x050100A0;
641 ffd39257 blueswir1
        break;
642 ffd39257 blueswir1
    case SM501_DRAM_CONTROL:
643 ffd39257 blueswir1
        ret = (s->dram_control & 0x07F107C0) | s->local_mem_size_index << 13;
644 ffd39257 blueswir1
        break;
645 ffd39257 blueswir1
    case SM501_IRQ_MASK:
646 ffd39257 blueswir1
        ret = s->irq_mask;
647 ffd39257 blueswir1
        break;
648 ffd39257 blueswir1
    case SM501_MISC_TIMING:
649 ffd39257 blueswir1
        /* TODO : simulate gate control */
650 ffd39257 blueswir1
        ret = s->misc_timing;
651 ffd39257 blueswir1
        break;
652 ffd39257 blueswir1
    case SM501_CURRENT_GATE:
653 ffd39257 blueswir1
        /* TODO : simulate gate control */
654 ffd39257 blueswir1
        ret = 0x00021807;
655 ffd39257 blueswir1
        break;
656 ffd39257 blueswir1
    case SM501_CURRENT_CLOCK:
657 ffd39257 blueswir1
        ret = 0x2A1A0A09;
658 ffd39257 blueswir1
        break;
659 ffd39257 blueswir1
    case SM501_POWER_MODE_CONTROL:
660 ffd39257 blueswir1
        ret = s->power_mode_control;
661 ffd39257 blueswir1
        break;
662 ffd39257 blueswir1
663 ffd39257 blueswir1
    default:
664 ffd39257 blueswir1
        printf("sm501 system config : not implemented register read."
665 8da3ff18 pbrook
               " addr=%x\n", (int)addr);
666 ffd39257 blueswir1
        assert(0);
667 ffd39257 blueswir1
    }
668 ffd39257 blueswir1
669 ffd39257 blueswir1
    return ret;
670 ffd39257 blueswir1
}
671 ffd39257 blueswir1
672 ffd39257 blueswir1
static void sm501_system_config_write(void *opaque,
673 c227f099 Anthony Liguori
                                      target_phys_addr_t addr, uint32_t value)
674 ffd39257 blueswir1
{
675 ffd39257 blueswir1
    SM501State * s = (SM501State *)opaque;
676 8da3ff18 pbrook
    SM501_DPRINTF("sm501 system config regs : write addr=%x, val=%x\n",
677 8da3ff18 pbrook
                  addr, value);
678 ffd39257 blueswir1
679 8da3ff18 pbrook
    switch(addr) {
680 ffd39257 blueswir1
    case SM501_SYSTEM_CONTROL:
681 ffd39257 blueswir1
        s->system_control = value & 0xE300B8F7;
682 ffd39257 blueswir1
        break;
683 ffd39257 blueswir1
    case SM501_MISC_CONTROL:
684 ffd39257 blueswir1
        s->misc_control = value & 0xFF7FFF20;
685 ffd39257 blueswir1
        break;
686 ffd39257 blueswir1
    case SM501_GPIO31_0_CONTROL:
687 ffd39257 blueswir1
        s->gpio_31_0_control = value;
688 ffd39257 blueswir1
        break;
689 ffd39257 blueswir1
    case SM501_GPIO63_32_CONTROL:
690 ffd39257 blueswir1
        s->gpio_63_32_control = value;
691 ffd39257 blueswir1
        break;
692 ffd39257 blueswir1
    case SM501_DRAM_CONTROL:
693 ffd39257 blueswir1
        s->local_mem_size_index = (value >> 13) & 0x7;
694 ffd39257 blueswir1
        /* rODO : check validity of size change */
695 ffd39257 blueswir1
        s->dram_control |=  value & 0x7FFFFFC3;
696 ffd39257 blueswir1
        break;
697 ffd39257 blueswir1
    case SM501_IRQ_MASK:
698 ffd39257 blueswir1
        s->irq_mask = value;
699 ffd39257 blueswir1
        break;
700 ffd39257 blueswir1
    case SM501_MISC_TIMING:
701 ffd39257 blueswir1
        s->misc_timing = value & 0xF31F1FFF;
702 ffd39257 blueswir1
        break;
703 ffd39257 blueswir1
    case SM501_POWER_MODE_0_GATE:
704 ffd39257 blueswir1
    case SM501_POWER_MODE_1_GATE:
705 ffd39257 blueswir1
    case SM501_POWER_MODE_0_CLOCK:
706 ffd39257 blueswir1
    case SM501_POWER_MODE_1_CLOCK:
707 ffd39257 blueswir1
        /* TODO : simulate gate & clock control */
708 ffd39257 blueswir1
        break;
709 ffd39257 blueswir1
    case SM501_POWER_MODE_CONTROL:
710 ffd39257 blueswir1
        s->power_mode_control = value & 0x00000003;
711 ffd39257 blueswir1
        break;
712 ffd39257 blueswir1
713 ffd39257 blueswir1
    default:
714 ffd39257 blueswir1
        printf("sm501 system config : not implemented register write."
715 8da3ff18 pbrook
               " addr=%x, val=%x\n", (int)addr, value);
716 ffd39257 blueswir1
        assert(0);
717 ffd39257 blueswir1
    }
718 ffd39257 blueswir1
}
719 ffd39257 blueswir1
720 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const sm501_system_config_readfn[] = {
721 ffd39257 blueswir1
    NULL,
722 ffd39257 blueswir1
    NULL,
723 ffd39257 blueswir1
    &sm501_system_config_read,
724 ffd39257 blueswir1
};
725 ffd39257 blueswir1
726 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const sm501_system_config_writefn[] = {
727 ffd39257 blueswir1
    NULL,
728 ffd39257 blueswir1
    NULL,
729 ffd39257 blueswir1
    &sm501_system_config_write,
730 ffd39257 blueswir1
};
731 ffd39257 blueswir1
732 c227f099 Anthony Liguori
static uint32_t sm501_palette_read(void *opaque, target_phys_addr_t addr)
733 486579de balrog
{
734 486579de balrog
    SM501State * s = (SM501State *)opaque;
735 486579de balrog
    SM501_DPRINTF("sm501 palette read addr=%x\n", (int)addr);
736 486579de balrog
737 486579de balrog
    /* TODO : consider BYTE/WORD access */
738 486579de balrog
    /* TODO : consider endian */
739 486579de balrog
740 486579de balrog
    assert(0 <= addr && addr < 0x400 * 3);
741 486579de balrog
    return *(uint32_t*)&s->dc_palette[addr];
742 486579de balrog
}
743 486579de balrog
744 486579de balrog
static void sm501_palette_write(void *opaque,
745 c227f099 Anthony Liguori
                                target_phys_addr_t addr, uint32_t value)
746 486579de balrog
{
747 486579de balrog
    SM501State * s = (SM501State *)opaque;
748 486579de balrog
    SM501_DPRINTF("sm501 palette write addr=%x, val=%x\n",
749 486579de balrog
                  (int)addr, value);
750 486579de balrog
751 486579de balrog
    /* TODO : consider BYTE/WORD access */
752 486579de balrog
    /* TODO : consider endian */
753 486579de balrog
754 486579de balrog
    assert(0 <= addr && addr < 0x400 * 3);
755 486579de balrog
    *(uint32_t*)&s->dc_palette[addr] = value;
756 486579de balrog
}
757 486579de balrog
758 c227f099 Anthony Liguori
static uint32_t sm501_disp_ctrl_read(void *opaque, target_phys_addr_t addr)
759 ffd39257 blueswir1
{
760 ffd39257 blueswir1
    SM501State * s = (SM501State *)opaque;
761 ffd39257 blueswir1
    uint32_t ret = 0;
762 8da3ff18 pbrook
    SM501_DPRINTF("sm501 disp ctrl regs : read addr=%x\n", (int)addr);
763 ffd39257 blueswir1
764 8da3ff18 pbrook
    switch(addr) {
765 ffd39257 blueswir1
766 ffd39257 blueswir1
    case SM501_DC_PANEL_CONTROL:
767 ffd39257 blueswir1
        ret = s->dc_panel_control;
768 ffd39257 blueswir1
        break;
769 ffd39257 blueswir1
    case SM501_DC_PANEL_PANNING_CONTROL:
770 ffd39257 blueswir1
        ret = s->dc_panel_panning_control;
771 ffd39257 blueswir1
        break;
772 ffd39257 blueswir1
    case SM501_DC_PANEL_FB_ADDR:
773 ffd39257 blueswir1
        ret = s->dc_panel_fb_addr;
774 ffd39257 blueswir1
        break;
775 ffd39257 blueswir1
    case SM501_DC_PANEL_FB_OFFSET:
776 ffd39257 blueswir1
        ret = s->dc_panel_fb_offset;
777 ffd39257 blueswir1
        break;
778 ffd39257 blueswir1
    case SM501_DC_PANEL_FB_WIDTH:
779 ffd39257 blueswir1
        ret = s->dc_panel_fb_width;
780 ffd39257 blueswir1
        break;
781 ffd39257 blueswir1
    case SM501_DC_PANEL_FB_HEIGHT:
782 ffd39257 blueswir1
        ret = s->dc_panel_fb_height;
783 ffd39257 blueswir1
        break;
784 ffd39257 blueswir1
    case SM501_DC_PANEL_TL_LOC:
785 ffd39257 blueswir1
        ret = s->dc_panel_tl_location;
786 ffd39257 blueswir1
        break;
787 ffd39257 blueswir1
    case SM501_DC_PANEL_BR_LOC:
788 ffd39257 blueswir1
        ret = s->dc_panel_br_location;
789 ffd39257 blueswir1
        break;
790 ffd39257 blueswir1
791 ffd39257 blueswir1
    case SM501_DC_PANEL_H_TOT:
792 ffd39257 blueswir1
        ret = s->dc_panel_h_total;
793 ffd39257 blueswir1
        break;
794 ffd39257 blueswir1
    case SM501_DC_PANEL_H_SYNC:
795 ffd39257 blueswir1
        ret = s->dc_panel_h_sync;
796 ffd39257 blueswir1
        break;
797 ffd39257 blueswir1
    case SM501_DC_PANEL_V_TOT:
798 ffd39257 blueswir1
        ret = s->dc_panel_v_total;
799 ffd39257 blueswir1
        break;
800 ffd39257 blueswir1
    case SM501_DC_PANEL_V_SYNC:
801 ffd39257 blueswir1
        ret = s->dc_panel_v_sync;
802 ffd39257 blueswir1
        break;
803 ffd39257 blueswir1
804 ffd39257 blueswir1
    case SM501_DC_CRT_CONTROL:
805 ffd39257 blueswir1
        ret = s->dc_crt_control;
806 ffd39257 blueswir1
        break;
807 ffd39257 blueswir1
    case SM501_DC_CRT_FB_ADDR:
808 ffd39257 blueswir1
        ret = s->dc_crt_fb_addr;
809 ffd39257 blueswir1
        break;
810 ffd39257 blueswir1
    case SM501_DC_CRT_FB_OFFSET:
811 ffd39257 blueswir1
        ret = s->dc_crt_fb_offset;
812 ffd39257 blueswir1
        break;
813 ffd39257 blueswir1
    case SM501_DC_CRT_H_TOT:
814 ffd39257 blueswir1
        ret = s->dc_crt_h_total;
815 ffd39257 blueswir1
        break;
816 ffd39257 blueswir1
    case SM501_DC_CRT_H_SYNC:
817 ffd39257 blueswir1
        ret = s->dc_crt_h_sync;
818 ffd39257 blueswir1
        break;
819 ffd39257 blueswir1
    case SM501_DC_CRT_V_TOT:
820 ffd39257 blueswir1
        ret = s->dc_crt_v_total;
821 ffd39257 blueswir1
        break;
822 ffd39257 blueswir1
    case SM501_DC_CRT_V_SYNC:
823 ffd39257 blueswir1
        ret = s->dc_crt_v_sync;
824 ffd39257 blueswir1
        break;
825 ffd39257 blueswir1
826 ffd39257 blueswir1
    case SM501_DC_CRT_HWC_ADDR:
827 ffd39257 blueswir1
        ret = s->dc_crt_hwc_addr;
828 ffd39257 blueswir1
        break;
829 ffd39257 blueswir1
    case SM501_DC_CRT_HWC_LOC:
830 0a4e7cd2 Shin-ichiro KAWASAKI
        ret = s->dc_crt_hwc_location;
831 ffd39257 blueswir1
        break;
832 ffd39257 blueswir1
    case SM501_DC_CRT_HWC_COLOR_1_2:
833 0a4e7cd2 Shin-ichiro KAWASAKI
        ret = s->dc_crt_hwc_color_1_2;
834 ffd39257 blueswir1
        break;
835 ffd39257 blueswir1
    case SM501_DC_CRT_HWC_COLOR_3:
836 0a4e7cd2 Shin-ichiro KAWASAKI
        ret = s->dc_crt_hwc_color_3;
837 ffd39257 blueswir1
        break;
838 ffd39257 blueswir1
839 486579de balrog
    case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400*3 - 4:
840 486579de balrog
        ret = sm501_palette_read(opaque, addr - SM501_DC_PANEL_PALETTE);
841 486579de balrog
        break;
842 486579de balrog
843 ffd39257 blueswir1
    default:
844 ffd39257 blueswir1
        printf("sm501 disp ctrl : not implemented register read."
845 8da3ff18 pbrook
               " addr=%x\n", (int)addr);
846 ffd39257 blueswir1
        assert(0);
847 ffd39257 blueswir1
    }
848 ffd39257 blueswir1
849 ffd39257 blueswir1
    return ret;
850 ffd39257 blueswir1
}
851 ffd39257 blueswir1
852 ffd39257 blueswir1
static void sm501_disp_ctrl_write(void *opaque,
853 c227f099 Anthony Liguori
                                           target_phys_addr_t addr,
854 ffd39257 blueswir1
                                           uint32_t value)
855 ffd39257 blueswir1
{
856 ffd39257 blueswir1
    SM501State * s = (SM501State *)opaque;
857 8da3ff18 pbrook
    SM501_DPRINTF("sm501 disp ctrl regs : write addr=%x, val=%x\n",
858 8da3ff18 pbrook
                  addr, value);
859 ffd39257 blueswir1
860 8da3ff18 pbrook
    switch(addr) {
861 ffd39257 blueswir1
    case SM501_DC_PANEL_CONTROL:
862 ffd39257 blueswir1
        s->dc_panel_control = value & 0x0FFF73FF;
863 ffd39257 blueswir1
        break;
864 ffd39257 blueswir1
    case SM501_DC_PANEL_PANNING_CONTROL:
865 ffd39257 blueswir1
        s->dc_panel_panning_control = value & 0xFF3FFF3F;
866 ffd39257 blueswir1
        break;
867 ffd39257 blueswir1
    case SM501_DC_PANEL_FB_ADDR:
868 ffd39257 blueswir1
        s->dc_panel_fb_addr = value & 0x8FFFFFF0;
869 ffd39257 blueswir1
        break;
870 ffd39257 blueswir1
    case SM501_DC_PANEL_FB_OFFSET:
871 ffd39257 blueswir1
        s->dc_panel_fb_offset = value & 0x3FF03FF0;
872 ffd39257 blueswir1
        break;
873 ffd39257 blueswir1
    case SM501_DC_PANEL_FB_WIDTH:
874 ffd39257 blueswir1
        s->dc_panel_fb_width = value & 0x0FFF0FFF;
875 ffd39257 blueswir1
        break;
876 ffd39257 blueswir1
    case SM501_DC_PANEL_FB_HEIGHT:
877 ffd39257 blueswir1
        s->dc_panel_fb_height = value & 0x0FFF0FFF;
878 ffd39257 blueswir1
        break;
879 ffd39257 blueswir1
    case SM501_DC_PANEL_TL_LOC:
880 ffd39257 blueswir1
        s->dc_panel_tl_location = value & 0x07FF07FF;
881 ffd39257 blueswir1
        break;
882 ffd39257 blueswir1
    case SM501_DC_PANEL_BR_LOC:
883 ffd39257 blueswir1
        s->dc_panel_br_location = value & 0x07FF07FF;
884 ffd39257 blueswir1
        break;
885 ffd39257 blueswir1
886 ffd39257 blueswir1
    case SM501_DC_PANEL_H_TOT:
887 ffd39257 blueswir1
        s->dc_panel_h_total = value & 0x0FFF0FFF;
888 ffd39257 blueswir1
        break;
889 ffd39257 blueswir1
    case SM501_DC_PANEL_H_SYNC:
890 ffd39257 blueswir1
        s->dc_panel_h_sync = value & 0x00FF0FFF;
891 ffd39257 blueswir1
        break;
892 ffd39257 blueswir1
    case SM501_DC_PANEL_V_TOT:
893 ffd39257 blueswir1
        s->dc_panel_v_total = value & 0x0FFF0FFF;
894 ffd39257 blueswir1
        break;
895 ffd39257 blueswir1
    case SM501_DC_PANEL_V_SYNC:
896 ffd39257 blueswir1
        s->dc_panel_v_sync = value & 0x003F0FFF;
897 ffd39257 blueswir1
        break;
898 ffd39257 blueswir1
899 ffd39257 blueswir1
    case SM501_DC_PANEL_HWC_ADDR:
900 ffd39257 blueswir1
        s->dc_panel_hwc_addr = value & 0x8FFFFFF0;
901 ffd39257 blueswir1
        break;
902 ffd39257 blueswir1
    case SM501_DC_PANEL_HWC_LOC:
903 0a4e7cd2 Shin-ichiro KAWASAKI
        s->dc_panel_hwc_location = value & 0x0FFF0FFF;
904 ffd39257 blueswir1
        break;
905 ffd39257 blueswir1
    case SM501_DC_PANEL_HWC_COLOR_1_2:
906 0a4e7cd2 Shin-ichiro KAWASAKI
        s->dc_panel_hwc_color_1_2 = value;
907 ffd39257 blueswir1
        break;
908 ffd39257 blueswir1
    case SM501_DC_PANEL_HWC_COLOR_3:
909 0a4e7cd2 Shin-ichiro KAWASAKI
        s->dc_panel_hwc_color_3 = value & 0x0000FFFF;
910 ffd39257 blueswir1
        break;
911 ffd39257 blueswir1
912 ffd39257 blueswir1
    case SM501_DC_CRT_CONTROL:
913 ffd39257 blueswir1
        s->dc_crt_control = value & 0x0003FFFF;
914 ffd39257 blueswir1
        break;
915 ffd39257 blueswir1
    case SM501_DC_CRT_FB_ADDR:
916 ffd39257 blueswir1
        s->dc_crt_fb_addr = value & 0x8FFFFFF0;
917 ffd39257 blueswir1
        break;
918 ffd39257 blueswir1
    case SM501_DC_CRT_FB_OFFSET:
919 ffd39257 blueswir1
        s->dc_crt_fb_offset = value & 0x3FF03FF0;
920 ffd39257 blueswir1
        break;
921 ffd39257 blueswir1
    case SM501_DC_CRT_H_TOT:
922 ffd39257 blueswir1
        s->dc_crt_h_total = value & 0x0FFF0FFF;
923 ffd39257 blueswir1
        break;
924 ffd39257 blueswir1
    case SM501_DC_CRT_H_SYNC:
925 ffd39257 blueswir1
        s->dc_crt_h_sync = value & 0x00FF0FFF;
926 ffd39257 blueswir1
        break;
927 ffd39257 blueswir1
    case SM501_DC_CRT_V_TOT:
928 ffd39257 blueswir1
        s->dc_crt_v_total = value & 0x0FFF0FFF;
929 ffd39257 blueswir1
        break;
930 ffd39257 blueswir1
    case SM501_DC_CRT_V_SYNC:
931 ffd39257 blueswir1
        s->dc_crt_v_sync = value & 0x003F0FFF;
932 ffd39257 blueswir1
        break;
933 ffd39257 blueswir1
934 ffd39257 blueswir1
    case SM501_DC_CRT_HWC_ADDR:
935 ffd39257 blueswir1
        s->dc_crt_hwc_addr = value & 0x8FFFFFF0;
936 ffd39257 blueswir1
        break;
937 ffd39257 blueswir1
    case SM501_DC_CRT_HWC_LOC:
938 0a4e7cd2 Shin-ichiro KAWASAKI
        s->dc_crt_hwc_location = value & 0x0FFF0FFF;
939 ffd39257 blueswir1
        break;
940 ffd39257 blueswir1
    case SM501_DC_CRT_HWC_COLOR_1_2:
941 0a4e7cd2 Shin-ichiro KAWASAKI
        s->dc_crt_hwc_color_1_2 = value;
942 ffd39257 blueswir1
        break;
943 ffd39257 blueswir1
    case SM501_DC_CRT_HWC_COLOR_3:
944 0a4e7cd2 Shin-ichiro KAWASAKI
        s->dc_crt_hwc_color_3 = value & 0x0000FFFF;
945 ffd39257 blueswir1
        break;
946 ffd39257 blueswir1
947 486579de balrog
    case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400*3 - 4:
948 486579de balrog
        sm501_palette_write(opaque, addr - SM501_DC_PANEL_PALETTE, value);
949 486579de balrog
        break;
950 486579de balrog
951 ffd39257 blueswir1
    default:
952 ffd39257 blueswir1
        printf("sm501 disp ctrl : not implemented register write."
953 8da3ff18 pbrook
               " addr=%x, val=%x\n", (int)addr, value);
954 ffd39257 blueswir1
        assert(0);
955 ffd39257 blueswir1
    }
956 ffd39257 blueswir1
}
957 ffd39257 blueswir1
958 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const sm501_disp_ctrl_readfn[] = {
959 ffd39257 blueswir1
    NULL,
960 ffd39257 blueswir1
    NULL,
961 ffd39257 blueswir1
    &sm501_disp_ctrl_read,
962 ffd39257 blueswir1
};
963 ffd39257 blueswir1
964 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const sm501_disp_ctrl_writefn[] = {
965 ffd39257 blueswir1
    NULL,
966 ffd39257 blueswir1
    NULL,
967 ffd39257 blueswir1
    &sm501_disp_ctrl_write,
968 ffd39257 blueswir1
};
969 ffd39257 blueswir1
970 ffd39257 blueswir1
/* draw line functions for all console modes */
971 ffd39257 blueswir1
972 ffd39257 blueswir1
#include "pixel_ops.h"
973 ffd39257 blueswir1
974 ffd39257 blueswir1
typedef void draw_line_func(uint8_t *d, const uint8_t *s,
975 ffd39257 blueswir1
                            int width, const uint32_t *pal);
976 ffd39257 blueswir1
977 0a4e7cd2 Shin-ichiro KAWASAKI
typedef void draw_hwc_line_func(SM501State * s, int crt, uint8_t * palette,
978 0a4e7cd2 Shin-ichiro KAWASAKI
                                int c_y, uint8_t *d, int width);
979 0a4e7cd2 Shin-ichiro KAWASAKI
980 ffd39257 blueswir1
#define DEPTH 8
981 ffd39257 blueswir1
#include "sm501_template.h"
982 ffd39257 blueswir1
983 ffd39257 blueswir1
#define DEPTH 15
984 ffd39257 blueswir1
#include "sm501_template.h"
985 ffd39257 blueswir1
986 ffd39257 blueswir1
#define BGR_FORMAT
987 ffd39257 blueswir1
#define DEPTH 15
988 ffd39257 blueswir1
#include "sm501_template.h"
989 ffd39257 blueswir1
990 ffd39257 blueswir1
#define DEPTH 16
991 ffd39257 blueswir1
#include "sm501_template.h"
992 ffd39257 blueswir1
993 ffd39257 blueswir1
#define BGR_FORMAT
994 ffd39257 blueswir1
#define DEPTH 16
995 ffd39257 blueswir1
#include "sm501_template.h"
996 ffd39257 blueswir1
997 ffd39257 blueswir1
#define DEPTH 32
998 ffd39257 blueswir1
#include "sm501_template.h"
999 ffd39257 blueswir1
1000 ffd39257 blueswir1
#define BGR_FORMAT
1001 ffd39257 blueswir1
#define DEPTH 32
1002 ffd39257 blueswir1
#include "sm501_template.h"
1003 ffd39257 blueswir1
1004 ffd39257 blueswir1
static draw_line_func * draw_line8_funcs[] = {
1005 ffd39257 blueswir1
    draw_line8_8,
1006 ffd39257 blueswir1
    draw_line8_15,
1007 ffd39257 blueswir1
    draw_line8_16,
1008 ffd39257 blueswir1
    draw_line8_32,
1009 ffd39257 blueswir1
    draw_line8_32bgr,
1010 ffd39257 blueswir1
    draw_line8_15bgr,
1011 ffd39257 blueswir1
    draw_line8_16bgr,
1012 ffd39257 blueswir1
};
1013 ffd39257 blueswir1
1014 ffd39257 blueswir1
static draw_line_func * draw_line16_funcs[] = {
1015 ffd39257 blueswir1
    draw_line16_8,
1016 ffd39257 blueswir1
    draw_line16_15,
1017 ffd39257 blueswir1
    draw_line16_16,
1018 ffd39257 blueswir1
    draw_line16_32,
1019 ffd39257 blueswir1
    draw_line16_32bgr,
1020 ffd39257 blueswir1
    draw_line16_15bgr,
1021 ffd39257 blueswir1
    draw_line16_16bgr,
1022 ffd39257 blueswir1
};
1023 ffd39257 blueswir1
1024 ffd39257 blueswir1
static draw_line_func * draw_line32_funcs[] = {
1025 ffd39257 blueswir1
    draw_line32_8,
1026 ffd39257 blueswir1
    draw_line32_15,
1027 ffd39257 blueswir1
    draw_line32_16,
1028 ffd39257 blueswir1
    draw_line32_32,
1029 ffd39257 blueswir1
    draw_line32_32bgr,
1030 ffd39257 blueswir1
    draw_line32_15bgr,
1031 ffd39257 blueswir1
    draw_line32_16bgr,
1032 ffd39257 blueswir1
};
1033 ffd39257 blueswir1
1034 0a4e7cd2 Shin-ichiro KAWASAKI
static draw_hwc_line_func * draw_hwc_line_funcs[] = {
1035 0a4e7cd2 Shin-ichiro KAWASAKI
    draw_hwc_line_8,
1036 0a4e7cd2 Shin-ichiro KAWASAKI
    draw_hwc_line_15,
1037 0a4e7cd2 Shin-ichiro KAWASAKI
    draw_hwc_line_16,
1038 0a4e7cd2 Shin-ichiro KAWASAKI
    draw_hwc_line_32,
1039 0a4e7cd2 Shin-ichiro KAWASAKI
    draw_hwc_line_32bgr,
1040 0a4e7cd2 Shin-ichiro KAWASAKI
    draw_hwc_line_15bgr,
1041 0a4e7cd2 Shin-ichiro KAWASAKI
    draw_hwc_line_16bgr,
1042 0a4e7cd2 Shin-ichiro KAWASAKI
};
1043 0a4e7cd2 Shin-ichiro KAWASAKI
1044 ffd39257 blueswir1
static inline int get_depth_index(DisplayState *s)
1045 ffd39257 blueswir1
{
1046 8927bcfd aliguori
    switch(ds_get_bits_per_pixel(s)) {
1047 ffd39257 blueswir1
    default:
1048 ffd39257 blueswir1
    case 8:
1049 ffd39257 blueswir1
        return 0;
1050 ffd39257 blueswir1
    case 15:
1051 8927bcfd aliguori
        return 1;
1052 ffd39257 blueswir1
    case 16:
1053 8927bcfd aliguori
        return 2;
1054 ffd39257 blueswir1
    case 32:
1055 7b5d76da aliguori
        if (is_surface_bgr(s->surface))
1056 7b5d76da aliguori
            return 4;
1057 7b5d76da aliguori
        else
1058 7b5d76da aliguori
            return 3;
1059 ffd39257 blueswir1
    }
1060 ffd39257 blueswir1
}
1061 ffd39257 blueswir1
1062 ffd39257 blueswir1
static void sm501_draw_crt(SM501State * s)
1063 ffd39257 blueswir1
{
1064 ffd39257 blueswir1
    int y;
1065 ffd39257 blueswir1
    int width = (s->dc_crt_h_total & 0x00000FFF) + 1;
1066 ffd39257 blueswir1
    int height = (s->dc_crt_v_total & 0x00000FFF) + 1;
1067 ffd39257 blueswir1
1068 ffd39257 blueswir1
    uint8_t  * src = s->local_mem;
1069 ffd39257 blueswir1
    int src_bpp = 0;
1070 8927bcfd aliguori
    int dst_bpp = ds_get_bytes_per_pixel(s->ds) + (ds_get_bits_per_pixel(s->ds) % 8 ? 1 : 0);
1071 ffd39257 blueswir1
    uint32_t * palette = (uint32_t *)&s->dc_palette[SM501_DC_CRT_PALETTE
1072 ffd39257 blueswir1
                                                    - SM501_DC_PANEL_PALETTE];
1073 0a4e7cd2 Shin-ichiro KAWASAKI
    uint8_t hwc_palette[3 * 3];
1074 ffd39257 blueswir1
    int ds_depth_index = get_depth_index(s->ds);
1075 ffd39257 blueswir1
    draw_line_func * draw_line = NULL;
1076 0a4e7cd2 Shin-ichiro KAWASAKI
    draw_hwc_line_func * draw_hwc_line = NULL;
1077 ffd39257 blueswir1
    int full_update = 0;
1078 ffd39257 blueswir1
    int y_start = -1;
1079 ffd39257 blueswir1
    int page_min = 0x7fffffff;
1080 ffd39257 blueswir1
    int page_max = -1;
1081 c227f099 Anthony Liguori
    ram_addr_t offset = s->local_mem_offset;
1082 ffd39257 blueswir1
1083 ffd39257 blueswir1
    /* choose draw_line function */
1084 ffd39257 blueswir1
    switch (s->dc_crt_control & 3) {
1085 ffd39257 blueswir1
    case SM501_DC_CRT_CONTROL_8BPP:
1086 ffd39257 blueswir1
        src_bpp = 1;
1087 ffd39257 blueswir1
        draw_line = draw_line8_funcs[ds_depth_index];
1088 ffd39257 blueswir1
        break;
1089 ffd39257 blueswir1
    case SM501_DC_CRT_CONTROL_16BPP:
1090 ffd39257 blueswir1
        src_bpp = 2;
1091 ffd39257 blueswir1
        draw_line = draw_line16_funcs[ds_depth_index];
1092 ffd39257 blueswir1
        break;
1093 ffd39257 blueswir1
    case SM501_DC_CRT_CONTROL_32BPP:
1094 ffd39257 blueswir1
        src_bpp = 4;
1095 ffd39257 blueswir1
        draw_line = draw_line32_funcs[ds_depth_index];
1096 ffd39257 blueswir1
        break;
1097 ffd39257 blueswir1
    default:
1098 ffd39257 blueswir1
        printf("sm501 draw crt : invalid DC_CRT_CONTROL=%x.\n",
1099 ffd39257 blueswir1
               s->dc_crt_control);
1100 ffd39257 blueswir1
        assert(0);
1101 ffd39257 blueswir1
        break;
1102 ffd39257 blueswir1
    }
1103 ffd39257 blueswir1
1104 0a4e7cd2 Shin-ichiro KAWASAKI
    /* set up to draw hardware cursor */
1105 0a4e7cd2 Shin-ichiro KAWASAKI
    if (is_hwc_enabled(s, 1)) {
1106 0a4e7cd2 Shin-ichiro KAWASAKI
        int i;
1107 0a4e7cd2 Shin-ichiro KAWASAKI
1108 0a4e7cd2 Shin-ichiro KAWASAKI
        /* get cursor palette */
1109 0a4e7cd2 Shin-ichiro KAWASAKI
        for (i = 0; i < 3; i++) {
1110 0a4e7cd2 Shin-ichiro KAWASAKI
            uint16_t rgb565 = get_hwc_color(s, 1, i + 1);
1111 0a4e7cd2 Shin-ichiro KAWASAKI
            hwc_palette[i * 3 + 0] = (rgb565 & 0xf800) >> 8; /* red */
1112 0a4e7cd2 Shin-ichiro KAWASAKI
            hwc_palette[i * 3 + 1] = (rgb565 & 0x07e0) >> 3; /* green */
1113 0a4e7cd2 Shin-ichiro KAWASAKI
            hwc_palette[i * 3 + 2] = (rgb565 & 0x001f) << 3; /* blue */
1114 0a4e7cd2 Shin-ichiro KAWASAKI
        }
1115 0a4e7cd2 Shin-ichiro KAWASAKI
1116 0a4e7cd2 Shin-ichiro KAWASAKI
        /* choose cursor draw line function */
1117 0a4e7cd2 Shin-ichiro KAWASAKI
        draw_hwc_line = draw_hwc_line_funcs[ds_depth_index];
1118 0a4e7cd2 Shin-ichiro KAWASAKI
    }
1119 0a4e7cd2 Shin-ichiro KAWASAKI
1120 ffd39257 blueswir1
    /* adjust console size */
1121 ffd39257 blueswir1
    if (s->last_width != width || s->last_height != height) {
1122 3023f332 aliguori
        qemu_console_resize(s->ds, width, height);
1123 ffd39257 blueswir1
        s->last_width = width;
1124 ffd39257 blueswir1
        s->last_height = height;
1125 ffd39257 blueswir1
        full_update = 1;
1126 ffd39257 blueswir1
    }
1127 ffd39257 blueswir1
1128 ffd39257 blueswir1
    /* draw each line according to conditions */
1129 ffd39257 blueswir1
    for (y = 0; y < height; y++) {
1130 0a4e7cd2 Shin-ichiro KAWASAKI
        int update_hwc = draw_hwc_line ? within_hwc_y_range(s, y, 1) : 0;
1131 0a4e7cd2 Shin-ichiro KAWASAKI
        int update = full_update || update_hwc;
1132 c227f099 Anthony Liguori
        ram_addr_t page0 = offset & TARGET_PAGE_MASK;
1133 c227f099 Anthony Liguori
        ram_addr_t page1 = (offset + width * src_bpp - 1) & TARGET_PAGE_MASK;
1134 c227f099 Anthony Liguori
        ram_addr_t page;
1135 ffd39257 blueswir1
1136 ffd39257 blueswir1
        /* check dirty flags for each line */
1137 ffd39257 blueswir1
        for (page = page0; page <= page1; page += TARGET_PAGE_SIZE)
1138 ffd39257 blueswir1
            if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG))
1139 ffd39257 blueswir1
                update = 1;
1140 ffd39257 blueswir1
1141 ffd39257 blueswir1
        /* draw line and change status */
1142 ffd39257 blueswir1
        if (update) {
1143 0a4e7cd2 Shin-ichiro KAWASAKI
            uint8_t * d = &(ds_get_data(s->ds)[y * width * dst_bpp]);
1144 0a4e7cd2 Shin-ichiro KAWASAKI
1145 0a4e7cd2 Shin-ichiro KAWASAKI
            /* draw graphics layer */
1146 0a4e7cd2 Shin-ichiro KAWASAKI
            draw_line(d, src, width, palette);
1147 0a4e7cd2 Shin-ichiro KAWASAKI
1148 0a4e7cd2 Shin-ichiro KAWASAKI
            /* draw haredware cursor */
1149 0a4e7cd2 Shin-ichiro KAWASAKI
            if (update_hwc) {
1150 0a4e7cd2 Shin-ichiro KAWASAKI
                draw_hwc_line(s, 1, hwc_palette, y - get_hwc_y(s, 1), d, width);
1151 0a4e7cd2 Shin-ichiro KAWASAKI
            }
1152 0a4e7cd2 Shin-ichiro KAWASAKI
1153 ffd39257 blueswir1
            if (y_start < 0)
1154 ffd39257 blueswir1
                y_start = y;
1155 ffd39257 blueswir1
            if (page0 < page_min)
1156 ffd39257 blueswir1
                page_min = page0;
1157 ffd39257 blueswir1
            if (page1 > page_max)
1158 ffd39257 blueswir1
                page_max = page1;
1159 ffd39257 blueswir1
        } else {
1160 ffd39257 blueswir1
            if (y_start >= 0) {
1161 ffd39257 blueswir1
                /* flush to display */
1162 ffd39257 blueswir1
                dpy_update(s->ds, 0, y_start, width, y - y_start);
1163 ffd39257 blueswir1
                y_start = -1;
1164 ffd39257 blueswir1
            }
1165 ffd39257 blueswir1
        }
1166 ffd39257 blueswir1
1167 ffd39257 blueswir1
        src += width * src_bpp;
1168 44654490 pbrook
        offset += width * src_bpp;
1169 ffd39257 blueswir1
    }
1170 ffd39257 blueswir1
1171 ffd39257 blueswir1
    /* complete flush to display */
1172 ffd39257 blueswir1
    if (y_start >= 0)
1173 ffd39257 blueswir1
        dpy_update(s->ds, 0, y_start, width, y - y_start);
1174 ffd39257 blueswir1
1175 ffd39257 blueswir1
    /* clear dirty flags */
1176 ffd39257 blueswir1
    if (page_max != -1)
1177 ffd39257 blueswir1
        cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
1178 ffd39257 blueswir1
                                        VGA_DIRTY_FLAG);
1179 ffd39257 blueswir1
}
1180 ffd39257 blueswir1
1181 ffd39257 blueswir1
static void sm501_update_display(void *opaque)
1182 ffd39257 blueswir1
{
1183 ffd39257 blueswir1
    SM501State * s = (SM501State *)opaque;
1184 ffd39257 blueswir1
1185 ffd39257 blueswir1
    if (s->dc_crt_control & SM501_DC_CRT_CONTROL_ENABLE)
1186 ffd39257 blueswir1
        sm501_draw_crt(s);
1187 ffd39257 blueswir1
}
1188 ffd39257 blueswir1
1189 ac611340 aurel32
void sm501_init(uint32_t base, uint32_t local_mem_bytes, qemu_irq irq,
1190 ac611340 aurel32
                CharDriverState *chr)
1191 ffd39257 blueswir1
{
1192 ffd39257 blueswir1
    SM501State * s;
1193 ffd39257 blueswir1
    int sm501_system_config_index;
1194 ffd39257 blueswir1
    int sm501_disp_ctrl_index;
1195 ffd39257 blueswir1
1196 ffd39257 blueswir1
    /* allocate management data region */
1197 ffd39257 blueswir1
    s = (SM501State *)qemu_mallocz(sizeof(SM501State));
1198 ffd39257 blueswir1
    s->base = base;
1199 ffd39257 blueswir1
    s->local_mem_size_index
1200 ffd39257 blueswir1
        = get_local_mem_size_index(local_mem_bytes);
1201 ffd39257 blueswir1
    SM501_DPRINTF("local mem size=%x. index=%d\n", get_local_mem_size(s),
1202 ffd39257 blueswir1
                  s->local_mem_size_index);
1203 ffd39257 blueswir1
    s->system_control = 0x00100000;
1204 ffd39257 blueswir1
    s->misc_control = 0x00001000; /* assumes SH, active=low */
1205 ffd39257 blueswir1
    s->dc_panel_control = 0x00010000;
1206 ffd39257 blueswir1
    s->dc_crt_control = 0x00010000;
1207 ffd39257 blueswir1
1208 ffd39257 blueswir1
    /* allocate local memory */
1209 44654490 pbrook
    s->local_mem_offset = qemu_ram_alloc(local_mem_bytes);
1210 44654490 pbrook
    s->local_mem = qemu_get_ram_ptr(s->local_mem_offset);
1211 44654490 pbrook
    cpu_register_physical_memory(base, local_mem_bytes, s->local_mem_offset);
1212 ffd39257 blueswir1
1213 ffd39257 blueswir1
    /* map mmio */
1214 ffd39257 blueswir1
    sm501_system_config_index
1215 1eed09cb Avi Kivity
        = cpu_register_io_memory(sm501_system_config_readfn,
1216 ffd39257 blueswir1
                                 sm501_system_config_writefn, s);
1217 ffd39257 blueswir1
    cpu_register_physical_memory(base + MMIO_BASE_OFFSET,
1218 ffd39257 blueswir1
                                 0x6c, sm501_system_config_index);
1219 1eed09cb Avi Kivity
    sm501_disp_ctrl_index = cpu_register_io_memory(sm501_disp_ctrl_readfn,
1220 ffd39257 blueswir1
                                                   sm501_disp_ctrl_writefn, s);
1221 ffd39257 blueswir1
    cpu_register_physical_memory(base + MMIO_BASE_OFFSET + SM501_DC,
1222 486579de balrog
                                 0x1000, sm501_disp_ctrl_index);
1223 ffd39257 blueswir1
1224 ac611340 aurel32
    /* bridge to usb host emulation module */
1225 ac611340 aurel32
    usb_ohci_init_sm501(base + MMIO_BASE_OFFSET + SM501_USB_HOST, base,
1226 ac611340 aurel32
                        2, -1, irq);
1227 ac611340 aurel32
1228 ffd39257 blueswir1
    /* bridge to serial emulation module */
1229 ffd39257 blueswir1
    if (chr)
1230 ffd39257 blueswir1
        serial_mm_init(base + MMIO_BASE_OFFSET + SM501_UART0, 2,
1231 b9d38e95 Blue Swirl
                       NULL, /* TODO : chain irq to IRL */
1232 ffd39257 blueswir1
                       115200, chr, 1);
1233 ffd39257 blueswir1
1234 ffd39257 blueswir1
    /* create qemu graphic console */
1235 3023f332 aliguori
    s->ds = graphic_console_init(sm501_update_display, NULL,
1236 3023f332 aliguori
                                 NULL, NULL, s);
1237 ffd39257 blueswir1
}