]>
Commit | Line | Data |
---|---|---|
e33d8cdb AZ |
1 | /* |
2 | * Copyright (c) 2006-2008 Openedhand Ltd. | |
3 | * Written by Andrzej Zaborowski <[email protected]> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License as | |
7 | * published by the Free Software Foundation; either version 2 or | |
8 | * (at your option) version 3 of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
fad6cb1a | 15 | * You should have received a copy of the GNU General Public License along |
8167ee88 | 16 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
e33d8cdb AZ |
17 | */ |
18 | #include "hw.h" | |
e33d8cdb | 19 | #include "sharpsl.h" |
383d01c6 | 20 | #include "sysbus.h" |
e33d8cdb | 21 | |
e33d8cdb | 22 | #undef REG_FMT |
e33d8cdb | 23 | #define REG_FMT "0x%02lx" |
e33d8cdb AZ |
24 | |
25 | /* SCOOP devices */ | |
26 | ||
383d01c6 | 27 | typedef struct ScoopInfo ScoopInfo; |
bc24a225 | 28 | struct ScoopInfo { |
383d01c6 | 29 | SysBusDevice busdev; |
e33d8cdb | 30 | qemu_irq handler[16]; |
e71ceafc | 31 | MemoryRegion iomem; |
e33d8cdb AZ |
32 | uint16_t status; |
33 | uint16_t power; | |
34 | uint32_t gpio_level; | |
35 | uint32_t gpio_dir; | |
36 | uint32_t prev_level; | |
37 | ||
38 | uint16_t mcr; | |
39 | uint16_t cdr; | |
40 | uint16_t ccr; | |
41 | uint16_t irr; | |
42 | uint16_t imr; | |
43 | uint16_t isr; | |
e33d8cdb AZ |
44 | }; |
45 | ||
46 | #define SCOOP_MCR 0x00 | |
47 | #define SCOOP_CDR 0x04 | |
48 | #define SCOOP_CSR 0x08 | |
49 | #define SCOOP_CPR 0x0c | |
50 | #define SCOOP_CCR 0x10 | |
51 | #define SCOOP_IRR_IRM 0x14 | |
52 | #define SCOOP_IMR 0x18 | |
53 | #define SCOOP_ISR 0x1c | |
54 | #define SCOOP_GPCR 0x20 | |
55 | #define SCOOP_GPWR 0x24 | |
56 | #define SCOOP_GPRR 0x28 | |
57 | ||
bc24a225 | 58 | static inline void scoop_gpio_handler_update(ScoopInfo *s) { |
e33d8cdb AZ |
59 | uint32_t level, diff; |
60 | int bit; | |
61 | level = s->gpio_level & s->gpio_dir; | |
62 | ||
63 | for (diff = s->prev_level ^ level; diff; diff ^= 1 << bit) { | |
64 | bit = ffs(diff) - 1; | |
65 | qemu_set_irq(s->handler[bit], (level >> bit) & 1); | |
66 | } | |
67 | ||
68 | s->prev_level = level; | |
69 | } | |
70 | ||
a8170e5e | 71 | static uint64_t scoop_read(void *opaque, hwaddr addr, |
e71ceafc | 72 | unsigned size) |
e33d8cdb | 73 | { |
bc24a225 | 74 | ScoopInfo *s = (ScoopInfo *) opaque; |
e33d8cdb | 75 | |
aa9438d9 | 76 | switch (addr & 0x3f) { |
e33d8cdb AZ |
77 | case SCOOP_MCR: |
78 | return s->mcr; | |
79 | case SCOOP_CDR: | |
80 | return s->cdr; | |
81 | case SCOOP_CSR: | |
82 | return s->status; | |
83 | case SCOOP_CPR: | |
84 | return s->power; | |
85 | case SCOOP_CCR: | |
86 | return s->ccr; | |
87 | case SCOOP_IRR_IRM: | |
88 | return s->irr; | |
89 | case SCOOP_IMR: | |
90 | return s->imr; | |
91 | case SCOOP_ISR: | |
92 | return s->isr; | |
93 | case SCOOP_GPCR: | |
94 | return s->gpio_dir; | |
95 | case SCOOP_GPWR: | |
e33d8cdb | 96 | case SCOOP_GPRR: |
1f163b14 | 97 | return s->gpio_level; |
e33d8cdb | 98 | default: |
a8b7063b | 99 | zaurus_printf("Bad register offset " REG_FMT "\n", (unsigned long)addr); |
e33d8cdb AZ |
100 | } |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
a8170e5e | 105 | static void scoop_write(void *opaque, hwaddr addr, |
e71ceafc | 106 | uint64_t value, unsigned size) |
e33d8cdb | 107 | { |
bc24a225 | 108 | ScoopInfo *s = (ScoopInfo *) opaque; |
e33d8cdb AZ |
109 | value &= 0xffff; |
110 | ||
aa9438d9 | 111 | switch (addr & 0x3f) { |
e33d8cdb AZ |
112 | case SCOOP_MCR: |
113 | s->mcr = value; | |
114 | break; | |
115 | case SCOOP_CDR: | |
116 | s->cdr = value; | |
117 | break; | |
118 | case SCOOP_CPR: | |
119 | s->power = value; | |
120 | if (value & 0x80) | |
121 | s->power |= 0x8040; | |
122 | break; | |
123 | case SCOOP_CCR: | |
124 | s->ccr = value; | |
125 | break; | |
126 | case SCOOP_IRR_IRM: | |
127 | s->irr = value; | |
128 | break; | |
129 | case SCOOP_IMR: | |
130 | s->imr = value; | |
131 | break; | |
132 | case SCOOP_ISR: | |
133 | s->isr = value; | |
134 | break; | |
135 | case SCOOP_GPCR: | |
136 | s->gpio_dir = value; | |
137 | scoop_gpio_handler_update(s); | |
138 | break; | |
139 | case SCOOP_GPWR: | |
1f163b14 | 140 | case SCOOP_GPRR: /* GPRR is probably R/O in real HW */ |
e33d8cdb AZ |
141 | s->gpio_level = value & s->gpio_dir; |
142 | scoop_gpio_handler_update(s); | |
143 | break; | |
e33d8cdb | 144 | default: |
a8b7063b | 145 | zaurus_printf("Bad register offset " REG_FMT "\n", (unsigned long)addr); |
e33d8cdb AZ |
146 | } |
147 | } | |
148 | ||
e71ceafc AK |
149 | static const MemoryRegionOps scoop_ops = { |
150 | .read = scoop_read, | |
151 | .write = scoop_write, | |
152 | .endianness = DEVICE_NATIVE_ENDIAN, | |
e33d8cdb AZ |
153 | }; |
154 | ||
383d01c6 | 155 | static void scoop_gpio_set(void *opaque, int line, int level) |
e33d8cdb | 156 | { |
8d30b794 | 157 | ScoopInfo *s = (ScoopInfo *) opaque; |
e33d8cdb AZ |
158 | |
159 | if (level) | |
160 | s->gpio_level |= (1 << line); | |
161 | else | |
162 | s->gpio_level &= ~(1 << line); | |
163 | } | |
164 | ||
383d01c6 | 165 | static int scoop_init(SysBusDevice *dev) |
e33d8cdb | 166 | { |
383d01c6 | 167 | ScoopInfo *s = FROM_SYSBUS(ScoopInfo, dev); |
e33d8cdb | 168 | |
383d01c6 DES |
169 | s->status = 0x02; |
170 | qdev_init_gpio_out(&s->busdev.qdev, s->handler, 16); | |
171 | qdev_init_gpio_in(&s->busdev.qdev, scoop_gpio_set, 16); | |
e71ceafc | 172 | memory_region_init_io(&s->iomem, &scoop_ops, s, "scoop", 0x1000); |
e33d8cdb | 173 | |
750ecd44 | 174 | sysbus_init_mmio(dev, &s->iomem); |
e33d8cdb | 175 | |
383d01c6 | 176 | return 0; |
e33d8cdb AZ |
177 | } |
178 | ||
7fe63a17 DES |
179 | static int scoop_post_load(void *opaque, int version_id) |
180 | { | |
181 | ScoopInfo *s = (ScoopInfo *) opaque; | |
182 | int i; | |
183 | uint32_t level; | |
184 | ||
185 | level = s->gpio_level & s->gpio_dir; | |
186 | ||
187 | for (i = 0; i < 16; i++) { | |
188 | qemu_set_irq(s->handler[i], (level >> i) & 1); | |
189 | } | |
190 | ||
191 | s->prev_level = level; | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
383d01c6 | 196 | static bool is_version_0 (void *opaque, int version_id) |
e33d8cdb | 197 | { |
383d01c6 | 198 | return version_id == 0; |
e33d8cdb AZ |
199 | } |
200 | ||
383d01c6 DES |
201 | static const VMStateDescription vmstate_scoop_regs = { |
202 | .name = "scoop", | |
203 | .version_id = 1, | |
204 | .minimum_version_id = 0, | |
205 | .minimum_version_id_old = 0, | |
7fe63a17 | 206 | .post_load = scoop_post_load, |
383d01c6 DES |
207 | .fields = (VMStateField []) { |
208 | VMSTATE_UINT16(status, ScoopInfo), | |
209 | VMSTATE_UINT16(power, ScoopInfo), | |
210 | VMSTATE_UINT32(gpio_level, ScoopInfo), | |
211 | VMSTATE_UINT32(gpio_dir, ScoopInfo), | |
212 | VMSTATE_UINT32(prev_level, ScoopInfo), | |
213 | VMSTATE_UINT16(mcr, ScoopInfo), | |
214 | VMSTATE_UINT16(cdr, ScoopInfo), | |
215 | VMSTATE_UINT16(ccr, ScoopInfo), | |
216 | VMSTATE_UINT16(irr, ScoopInfo), | |
217 | VMSTATE_UINT16(imr, ScoopInfo), | |
218 | VMSTATE_UINT16(isr, ScoopInfo), | |
219 | VMSTATE_UNUSED_TEST(is_version_0, 2), | |
220 | VMSTATE_END_OF_LIST(), | |
221 | }, | |
222 | }; | |
e33d8cdb | 223 | |
999e12bb AL |
224 | static Property scoop_sysbus_properties[] = { |
225 | DEFINE_PROP_END_OF_LIST(), | |
226 | }; | |
227 | ||
228 | static void scoop_sysbus_class_init(ObjectClass *klass, void *data) | |
229 | { | |
39bffca2 | 230 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb AL |
231 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
232 | ||
233 | k->init = scoop_init; | |
39bffca2 AL |
234 | dc->desc = "Scoop2 Sharp custom ASIC"; |
235 | dc->vmsd = &vmstate_scoop_regs; | |
236 | dc->props = scoop_sysbus_properties; | |
999e12bb AL |
237 | } |
238 | ||
39bffca2 AL |
239 | static TypeInfo scoop_sysbus_info = { |
240 | .name = "scoop", | |
241 | .parent = TYPE_SYS_BUS_DEVICE, | |
242 | .instance_size = sizeof(ScoopInfo), | |
243 | .class_init = scoop_sysbus_class_init, | |
383d01c6 | 244 | }; |
e33d8cdb | 245 | |
83f7d43a | 246 | static void scoop_register_types(void) |
383d01c6 | 247 | { |
39bffca2 | 248 | type_register_static(&scoop_sysbus_info); |
e33d8cdb | 249 | } |
83f7d43a AF |
250 | |
251 | type_init(scoop_register_types) | |
e33d8cdb AZ |
252 | |
253 | /* Write the bootloader parameters memory area. */ | |
254 | ||
255 | #define MAGIC_CHG(a, b, c, d) ((d << 24) | (c << 16) | (b << 8) | a) | |
256 | ||
541dc0d4 | 257 | static struct QEMU_PACKED sl_param_info { |
e33d8cdb AZ |
258 | uint32_t comadj_keyword; |
259 | int32_t comadj; | |
260 | ||
261 | uint32_t uuid_keyword; | |
262 | char uuid[16]; | |
263 | ||
264 | uint32_t touch_keyword; | |
265 | int32_t touch_xp; | |
266 | int32_t touch_yp; | |
267 | int32_t touch_xd; | |
268 | int32_t touch_yd; | |
269 | ||
270 | uint32_t adadj_keyword; | |
271 | int32_t adadj; | |
272 | ||
273 | uint32_t phad_keyword; | |
274 | int32_t phadadj; | |
275 | } zaurus_bootparam = { | |
276 | .comadj_keyword = MAGIC_CHG('C', 'M', 'A', 'D'), | |
277 | .comadj = 125, | |
278 | .uuid_keyword = MAGIC_CHG('U', 'U', 'I', 'D'), | |
279 | .uuid = { -1 }, | |
280 | .touch_keyword = MAGIC_CHG('T', 'U', 'C', 'H'), | |
281 | .touch_xp = -1, | |
282 | .adadj_keyword = MAGIC_CHG('B', 'V', 'A', 'D'), | |
283 | .adadj = -1, | |
284 | .phad_keyword = MAGIC_CHG('P', 'H', 'A', 'D'), | |
285 | .phadadj = 0x01, | |
286 | }; | |
287 | ||
a8170e5e | 288 | void sl_bootparam_write(hwaddr ptr) |
e33d8cdb | 289 | { |
f78630ab PB |
290 | cpu_physical_memory_write(ptr, (void *)&zaurus_bootparam, |
291 | sizeof(struct sl_param_info)); | |
e33d8cdb | 292 | } |