xref: /qemu/hw/gpio/zaurus.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
1 /*
2  * Copyright (c) 2006-2008 Openedhand Ltd.
3  * Written by Andrzej Zaborowski <balrog@zabor.org>
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  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "qemu/osdep.h"
20 #include "hw/irq.h"
21 #include "hw/arm/sharpsl.h"
22 #include "hw/sysbus.h"
23 #include "migration/vmstate.h"
24 #include "qemu/module.h"
25 #include "qemu/log.h"
26 #include "qom/object.h"
27 
28 /* SCOOP devices */
29 
30 #define TYPE_SCOOP "scoop"
31 typedef struct ScoopInfo ScoopInfo;
32 #define SCOOP(obj) OBJECT_CHECK(ScoopInfo, (obj), TYPE_SCOOP)
33 
34 struct ScoopInfo {
35     SysBusDevice parent_obj;
36 
37     qemu_irq handler[16];
38     MemoryRegion iomem;
39     uint16_t status;
40     uint16_t power;
41     uint32_t gpio_level;
42     uint32_t gpio_dir;
43     uint32_t prev_level;
44 
45     uint16_t mcr;
46     uint16_t cdr;
47     uint16_t ccr;
48     uint16_t irr;
49     uint16_t imr;
50     uint16_t isr;
51 };
52 
53 #define SCOOP_MCR	0x00
54 #define SCOOP_CDR	0x04
55 #define SCOOP_CSR	0x08
56 #define SCOOP_CPR	0x0c
57 #define SCOOP_CCR	0x10
58 #define SCOOP_IRR_IRM	0x14
59 #define SCOOP_IMR	0x18
60 #define SCOOP_ISR	0x1c
61 #define SCOOP_GPCR	0x20
62 #define SCOOP_GPWR	0x24
63 #define SCOOP_GPRR	0x28
64 
65 static inline void scoop_gpio_handler_update(ScoopInfo *s) {
66     uint32_t level, diff;
67     int bit;
68     level = s->gpio_level & s->gpio_dir;
69 
70     for (diff = s->prev_level ^ level; diff; diff ^= 1 << bit) {
71         bit = ctz32(diff);
72         qemu_set_irq(s->handler[bit], (level >> bit) & 1);
73     }
74 
75     s->prev_level = level;
76 }
77 
78 static uint64_t scoop_read(void *opaque, hwaddr addr,
79                            unsigned size)
80 {
81     ScoopInfo *s = (ScoopInfo *) opaque;
82 
83     switch (addr & 0x3f) {
84     case SCOOP_MCR:
85         return s->mcr;
86     case SCOOP_CDR:
87         return s->cdr;
88     case SCOOP_CSR:
89         return s->status;
90     case SCOOP_CPR:
91         return s->power;
92     case SCOOP_CCR:
93         return s->ccr;
94     case SCOOP_IRR_IRM:
95         return s->irr;
96     case SCOOP_IMR:
97         return s->imr;
98     case SCOOP_ISR:
99         return s->isr;
100     case SCOOP_GPCR:
101         return s->gpio_dir;
102     case SCOOP_GPWR:
103     case SCOOP_GPRR:
104         return s->gpio_level;
105     default:
106         qemu_log_mask(LOG_GUEST_ERROR,
107                       "scoop_read: bad register offset 0x%02" HWADDR_PRIx "\n",
108                       addr);
109     }
110 
111     return 0;
112 }
113 
114 static void scoop_write(void *opaque, hwaddr addr,
115                         uint64_t value, unsigned size)
116 {
117     ScoopInfo *s = (ScoopInfo *) opaque;
118     value &= 0xffff;
119 
120     switch (addr & 0x3f) {
121     case SCOOP_MCR:
122         s->mcr = value;
123         break;
124     case SCOOP_CDR:
125         s->cdr = value;
126         break;
127     case SCOOP_CPR:
128         s->power = value;
129         if (value & 0x80)
130             s->power |= 0x8040;
131         break;
132     case SCOOP_CCR:
133         s->ccr = value;
134         break;
135     case SCOOP_IRR_IRM:
136         s->irr = value;
137         break;
138     case SCOOP_IMR:
139         s->imr = value;
140         break;
141     case SCOOP_ISR:
142         s->isr = value;
143         break;
144     case SCOOP_GPCR:
145         s->gpio_dir = value;
146         scoop_gpio_handler_update(s);
147         break;
148     case SCOOP_GPWR:
149     case SCOOP_GPRR:	/* GPRR is probably R/O in real HW */
150         s->gpio_level = value & s->gpio_dir;
151         scoop_gpio_handler_update(s);
152         break;
153     default:
154         qemu_log_mask(LOG_GUEST_ERROR,
155                       "scoop_write: bad register offset 0x%02" HWADDR_PRIx "\n",
156                       addr);
157     }
158 }
159 
160 static const MemoryRegionOps scoop_ops = {
161     .read = scoop_read,
162     .write = scoop_write,
163     .endianness = DEVICE_NATIVE_ENDIAN,
164 };
165 
166 static void scoop_gpio_set(void *opaque, int line, int level)
167 {
168     ScoopInfo *s = (ScoopInfo *) opaque;
169 
170     if (level)
171         s->gpio_level |= (1 << line);
172     else
173         s->gpio_level &= ~(1 << line);
174 }
175 
176 static void scoop_init(Object *obj)
177 {
178     DeviceState *dev = DEVICE(obj);
179     ScoopInfo *s = SCOOP(obj);
180     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
181 
182     s->status = 0x02;
183     qdev_init_gpio_out(dev, s->handler, 16);
184     qdev_init_gpio_in(dev, scoop_gpio_set, 16);
185     memory_region_init_io(&s->iomem, obj, &scoop_ops, s, "scoop", 0x1000);
186 
187     sysbus_init_mmio(sbd, &s->iomem);
188 }
189 
190 static int scoop_post_load(void *opaque, int version_id)
191 {
192     ScoopInfo *s = (ScoopInfo *) opaque;
193     int i;
194     uint32_t level;
195 
196     level = s->gpio_level & s->gpio_dir;
197 
198     for (i = 0; i < 16; i++) {
199         qemu_set_irq(s->handler[i], (level >> i) & 1);
200     }
201 
202     s->prev_level = level;
203 
204     return 0;
205 }
206 
207 static bool is_version_0 (void *opaque, int version_id)
208 {
209     return version_id == 0;
210 }
211 
212 static bool vmstate_scoop_validate(void *opaque, int version_id)
213 {
214     ScoopInfo *s = opaque;
215 
216     return !(s->prev_level & 0xffff0000) &&
217         !(s->gpio_level & 0xffff0000) &&
218         !(s->gpio_dir & 0xffff0000);
219 }
220 
221 static const VMStateDescription vmstate_scoop_regs = {
222     .name = "scoop",
223     .version_id = 1,
224     .minimum_version_id = 0,
225     .post_load = scoop_post_load,
226     .fields = (VMStateField[]) {
227         VMSTATE_UINT16(status, ScoopInfo),
228         VMSTATE_UINT16(power, ScoopInfo),
229         VMSTATE_UINT32(gpio_level, ScoopInfo),
230         VMSTATE_UINT32(gpio_dir, ScoopInfo),
231         VMSTATE_UINT32(prev_level, ScoopInfo),
232         VMSTATE_VALIDATE("irq levels are 16 bit", vmstate_scoop_validate),
233         VMSTATE_UINT16(mcr, ScoopInfo),
234         VMSTATE_UINT16(cdr, ScoopInfo),
235         VMSTATE_UINT16(ccr, ScoopInfo),
236         VMSTATE_UINT16(irr, ScoopInfo),
237         VMSTATE_UINT16(imr, ScoopInfo),
238         VMSTATE_UINT16(isr, ScoopInfo),
239         VMSTATE_UNUSED_TEST(is_version_0, 2),
240         VMSTATE_END_OF_LIST(),
241     },
242 };
243 
244 static void scoop_sysbus_class_init(ObjectClass *klass, void *data)
245 {
246     DeviceClass *dc = DEVICE_CLASS(klass);
247 
248     dc->desc = "Scoop2 Sharp custom ASIC";
249     dc->vmsd = &vmstate_scoop_regs;
250 }
251 
252 static const TypeInfo scoop_sysbus_info = {
253     .name          = TYPE_SCOOP,
254     .parent        = TYPE_SYS_BUS_DEVICE,
255     .instance_size = sizeof(ScoopInfo),
256     .instance_init = scoop_init,
257     .class_init    = scoop_sysbus_class_init,
258 };
259 
260 static void scoop_register_types(void)
261 {
262     type_register_static(&scoop_sysbus_info);
263 }
264 
265 type_init(scoop_register_types)
266 
267 /* Write the bootloader parameters memory area.  */
268 
269 #define MAGIC_CHG(a, b, c, d)	((d << 24) | (c << 16) | (b << 8) | a)
270 
271 static struct QEMU_PACKED sl_param_info {
272     uint32_t comadj_keyword;
273     int32_t comadj;
274 
275     uint32_t uuid_keyword;
276     char uuid[16];
277 
278     uint32_t touch_keyword;
279     int32_t touch_xp;
280     int32_t touch_yp;
281     int32_t touch_xd;
282     int32_t touch_yd;
283 
284     uint32_t adadj_keyword;
285     int32_t adadj;
286 
287     uint32_t phad_keyword;
288     int32_t phadadj;
289 } zaurus_bootparam = {
290     .comadj_keyword	= MAGIC_CHG('C', 'M', 'A', 'D'),
291     .comadj		= 125,
292     .uuid_keyword	= MAGIC_CHG('U', 'U', 'I', 'D'),
293     .uuid		= { -1 },
294     .touch_keyword	= MAGIC_CHG('T', 'U', 'C', 'H'),
295     .touch_xp		= -1,
296     .adadj_keyword	= MAGIC_CHG('B', 'V', 'A', 'D'),
297     .adadj		= -1,
298     .phad_keyword	= MAGIC_CHG('P', 'H', 'A', 'D'),
299     .phadadj		= 0x01,
300 };
301 
302 void sl_bootparam_write(hwaddr ptr)
303 {
304     cpu_physical_memory_write(ptr, &zaurus_bootparam,
305                               sizeof(struct sl_param_info));
306 }
307