Statistics
| Branch: | Revision:

root / hw / sm501.c @ 61d3cf93

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