vhost-user: allow slave to send fds via slave channel
[qemu/ar7.git] / hw / arm / strongarm.c
blobec2627374d070abda2f3564aeeddb090042a61ab
1 /*
2 * StrongARM SA-1100/SA-1110 emulation
4 * Copyright (C) 2011 Dmitry Eremin-Solenikov
6 * Largely based on StrongARM emulation:
7 * Copyright (c) 2006 Openedhand Ltd.
8 * Written by Andrzej Zaborowski <balrog@zabor.org>
10 * UART code based on QEMU 16550A UART emulation
11 * Copyright (c) 2003-2004 Fabrice Bellard
12 * Copyright (c) 2008 Citrix Systems, Inc.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, see <http://www.gnu.org/licenses/>.
26 * Contributions after 2012-01-13 are licensed under the terms of the
27 * GNU GPL, version 2 or (at your option) any later version.
30 #include "qemu/osdep.h"
31 #include "cpu.h"
32 #include "hw/boards.h"
33 #include "hw/sysbus.h"
34 #include "strongarm.h"
35 #include "qemu/error-report.h"
36 #include "hw/arm/arm.h"
37 #include "chardev/char-fe.h"
38 #include "chardev/char-serial.h"
39 #include "sysemu/sysemu.h"
40 #include "hw/ssi/ssi.h"
41 #include "qemu/cutils.h"
42 #include "qemu/log.h"
44 //#define DEBUG
47 TODO
48 - Implement cp15, c14 ?
49 - Implement cp15, c15 !!! (idle used in L)
50 - Implement idle mode handling/DIM
51 - Implement sleep mode/Wake sources
52 - Implement reset control
53 - Implement memory control regs
54 - PCMCIA handling
55 - Maybe support MBGNT/MBREQ
56 - DMA channels
57 - GPCLK
58 - IrDA
59 - MCP
60 - Enhance UART with modem signals
63 #ifdef DEBUG
64 # define DPRINTF(format, ...) printf(format , ## __VA_ARGS__)
65 #else
66 # define DPRINTF(format, ...) do { } while (0)
67 #endif
69 static struct {
70 hwaddr io_base;
71 int irq;
72 } sa_serial[] = {
73 { 0x80010000, SA_PIC_UART1 },
74 { 0x80030000, SA_PIC_UART2 },
75 { 0x80050000, SA_PIC_UART3 },
76 { 0, 0 }
79 /* Interrupt Controller */
81 #define TYPE_STRONGARM_PIC "strongarm_pic"
82 #define STRONGARM_PIC(obj) \
83 OBJECT_CHECK(StrongARMPICState, (obj), TYPE_STRONGARM_PIC)
85 typedef struct StrongARMPICState {
86 SysBusDevice parent_obj;
88 MemoryRegion iomem;
89 qemu_irq irq;
90 qemu_irq fiq;
92 uint32_t pending;
93 uint32_t enabled;
94 uint32_t is_fiq;
95 uint32_t int_idle;
96 } StrongARMPICState;
98 #define ICIP 0x00
99 #define ICMR 0x04
100 #define ICLR 0x08
101 #define ICFP 0x10
102 #define ICPR 0x20
103 #define ICCR 0x0c
105 #define SA_PIC_SRCS 32
108 static void strongarm_pic_update(void *opaque)
110 StrongARMPICState *s = opaque;
112 /* FIXME: reflect DIM */
113 qemu_set_irq(s->fiq, s->pending & s->enabled & s->is_fiq);
114 qemu_set_irq(s->irq, s->pending & s->enabled & ~s->is_fiq);
117 static void strongarm_pic_set_irq(void *opaque, int irq, int level)
119 StrongARMPICState *s = opaque;
121 if (level) {
122 s->pending |= 1 << irq;
123 } else {
124 s->pending &= ~(1 << irq);
127 strongarm_pic_update(s);
130 static uint64_t strongarm_pic_mem_read(void *opaque, hwaddr offset,
131 unsigned size)
133 StrongARMPICState *s = opaque;
135 switch (offset) {
136 case ICIP:
137 return s->pending & ~s->is_fiq & s->enabled;
138 case ICMR:
139 return s->enabled;
140 case ICLR:
141 return s->is_fiq;
142 case ICCR:
143 return s->int_idle == 0;
144 case ICFP:
145 return s->pending & s->is_fiq & s->enabled;
146 case ICPR:
147 return s->pending;
148 default:
149 printf("%s: Bad register offset 0x" TARGET_FMT_plx "\n",
150 __func__, offset);
151 return 0;
155 static void strongarm_pic_mem_write(void *opaque, hwaddr offset,
156 uint64_t value, unsigned size)
158 StrongARMPICState *s = opaque;
160 switch (offset) {
161 case ICMR:
162 s->enabled = value;
163 break;
164 case ICLR:
165 s->is_fiq = value;
166 break;
167 case ICCR:
168 s->int_idle = (value & 1) ? 0 : ~0;
169 break;
170 default:
171 printf("%s: Bad register offset 0x" TARGET_FMT_plx "\n",
172 __func__, offset);
173 break;
175 strongarm_pic_update(s);
178 static const MemoryRegionOps strongarm_pic_ops = {
179 .read = strongarm_pic_mem_read,
180 .write = strongarm_pic_mem_write,
181 .endianness = DEVICE_NATIVE_ENDIAN,
184 static void strongarm_pic_initfn(Object *obj)
186 DeviceState *dev = DEVICE(obj);
187 StrongARMPICState *s = STRONGARM_PIC(obj);
188 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
190 qdev_init_gpio_in(dev, strongarm_pic_set_irq, SA_PIC_SRCS);
191 memory_region_init_io(&s->iomem, obj, &strongarm_pic_ops, s,
192 "pic", 0x1000);
193 sysbus_init_mmio(sbd, &s->iomem);
194 sysbus_init_irq(sbd, &s->irq);
195 sysbus_init_irq(sbd, &s->fiq);
198 static int strongarm_pic_post_load(void *opaque, int version_id)
200 strongarm_pic_update(opaque);
201 return 0;
204 static VMStateDescription vmstate_strongarm_pic_regs = {
205 .name = "strongarm_pic",
206 .version_id = 0,
207 .minimum_version_id = 0,
208 .post_load = strongarm_pic_post_load,
209 .fields = (VMStateField[]) {
210 VMSTATE_UINT32(pending, StrongARMPICState),
211 VMSTATE_UINT32(enabled, StrongARMPICState),
212 VMSTATE_UINT32(is_fiq, StrongARMPICState),
213 VMSTATE_UINT32(int_idle, StrongARMPICState),
214 VMSTATE_END_OF_LIST(),
218 static void strongarm_pic_class_init(ObjectClass *klass, void *data)
220 DeviceClass *dc = DEVICE_CLASS(klass);
222 dc->desc = "StrongARM PIC";
223 dc->vmsd = &vmstate_strongarm_pic_regs;
226 static const TypeInfo strongarm_pic_info = {
227 .name = TYPE_STRONGARM_PIC,
228 .parent = TYPE_SYS_BUS_DEVICE,
229 .instance_size = sizeof(StrongARMPICState),
230 .instance_init = strongarm_pic_initfn,
231 .class_init = strongarm_pic_class_init,
234 /* Real-Time Clock */
235 #define RTAR 0x00 /* RTC Alarm register */
236 #define RCNR 0x04 /* RTC Counter register */
237 #define RTTR 0x08 /* RTC Timer Trim register */
238 #define RTSR 0x10 /* RTC Status register */
240 #define RTSR_AL (1 << 0) /* RTC Alarm detected */
241 #define RTSR_HZ (1 << 1) /* RTC 1Hz detected */
242 #define RTSR_ALE (1 << 2) /* RTC Alarm enable */
243 #define RTSR_HZE (1 << 3) /* RTC 1Hz enable */
245 /* 16 LSB of RTTR are clockdiv for internal trim logic,
246 * trim delete isn't emulated, so
247 * f = 32 768 / (RTTR_trim + 1) */
249 #define TYPE_STRONGARM_RTC "strongarm-rtc"
250 #define STRONGARM_RTC(obj) \
251 OBJECT_CHECK(StrongARMRTCState, (obj), TYPE_STRONGARM_RTC)
253 typedef struct StrongARMRTCState {
254 SysBusDevice parent_obj;
256 MemoryRegion iomem;
257 uint32_t rttr;
258 uint32_t rtsr;
259 uint32_t rtar;
260 uint32_t last_rcnr;
261 int64_t last_hz;
262 QEMUTimer *rtc_alarm;
263 QEMUTimer *rtc_hz;
264 qemu_irq rtc_irq;
265 qemu_irq rtc_hz_irq;
266 } StrongARMRTCState;
268 static inline void strongarm_rtc_int_update(StrongARMRTCState *s)
270 qemu_set_irq(s->rtc_irq, s->rtsr & RTSR_AL);
271 qemu_set_irq(s->rtc_hz_irq, s->rtsr & RTSR_HZ);
274 static void strongarm_rtc_hzupdate(StrongARMRTCState *s)
276 int64_t rt = qemu_clock_get_ms(rtc_clock);
277 s->last_rcnr += ((rt - s->last_hz) << 15) /
278 (1000 * ((s->rttr & 0xffff) + 1));
279 s->last_hz = rt;
282 static inline void strongarm_rtc_timer_update(StrongARMRTCState *s)
284 if ((s->rtsr & RTSR_HZE) && !(s->rtsr & RTSR_HZ)) {
285 timer_mod(s->rtc_hz, s->last_hz + 1000);
286 } else {
287 timer_del(s->rtc_hz);
290 if ((s->rtsr & RTSR_ALE) && !(s->rtsr & RTSR_AL)) {
291 timer_mod(s->rtc_alarm, s->last_hz +
292 (((s->rtar - s->last_rcnr) * 1000 *
293 ((s->rttr & 0xffff) + 1)) >> 15));
294 } else {
295 timer_del(s->rtc_alarm);
299 static inline void strongarm_rtc_alarm_tick(void *opaque)
301 StrongARMRTCState *s = opaque;
302 s->rtsr |= RTSR_AL;
303 strongarm_rtc_timer_update(s);
304 strongarm_rtc_int_update(s);
307 static inline void strongarm_rtc_hz_tick(void *opaque)
309 StrongARMRTCState *s = opaque;
310 s->rtsr |= RTSR_HZ;
311 strongarm_rtc_timer_update(s);
312 strongarm_rtc_int_update(s);
315 static uint64_t strongarm_rtc_read(void *opaque, hwaddr addr,
316 unsigned size)
318 StrongARMRTCState *s = opaque;
320 switch (addr) {
321 case RTTR:
322 return s->rttr;
323 case RTSR:
324 return s->rtsr;
325 case RTAR:
326 return s->rtar;
327 case RCNR:
328 return s->last_rcnr +
329 ((qemu_clock_get_ms(rtc_clock) - s->last_hz) << 15) /
330 (1000 * ((s->rttr & 0xffff) + 1));
331 default:
332 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr);
333 return 0;
337 static void strongarm_rtc_write(void *opaque, hwaddr addr,
338 uint64_t value, unsigned size)
340 StrongARMRTCState *s = opaque;
341 uint32_t old_rtsr;
343 switch (addr) {
344 case RTTR:
345 strongarm_rtc_hzupdate(s);
346 s->rttr = value;
347 strongarm_rtc_timer_update(s);
348 break;
350 case RTSR:
351 old_rtsr = s->rtsr;
352 s->rtsr = (value & (RTSR_ALE | RTSR_HZE)) |
353 (s->rtsr & ~(value & (RTSR_AL | RTSR_HZ)));
355 if (s->rtsr != old_rtsr) {
356 strongarm_rtc_timer_update(s);
359 strongarm_rtc_int_update(s);
360 break;
362 case RTAR:
363 s->rtar = value;
364 strongarm_rtc_timer_update(s);
365 break;
367 case RCNR:
368 strongarm_rtc_hzupdate(s);
369 s->last_rcnr = value;
370 strongarm_rtc_timer_update(s);
371 break;
373 default:
374 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr);
378 static const MemoryRegionOps strongarm_rtc_ops = {
379 .read = strongarm_rtc_read,
380 .write = strongarm_rtc_write,
381 .endianness = DEVICE_NATIVE_ENDIAN,
384 static void strongarm_rtc_init(Object *obj)
386 StrongARMRTCState *s = STRONGARM_RTC(obj);
387 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
388 struct tm tm;
390 s->rttr = 0x0;
391 s->rtsr = 0;
393 qemu_get_timedate(&tm, 0);
395 s->last_rcnr = (uint32_t) mktimegm(&tm);
396 s->last_hz = qemu_clock_get_ms(rtc_clock);
398 s->rtc_alarm = timer_new_ms(rtc_clock, strongarm_rtc_alarm_tick, s);
399 s->rtc_hz = timer_new_ms(rtc_clock, strongarm_rtc_hz_tick, s);
401 sysbus_init_irq(dev, &s->rtc_irq);
402 sysbus_init_irq(dev, &s->rtc_hz_irq);
404 memory_region_init_io(&s->iomem, obj, &strongarm_rtc_ops, s,
405 "rtc", 0x10000);
406 sysbus_init_mmio(dev, &s->iomem);
409 static int strongarm_rtc_pre_save(void *opaque)
411 StrongARMRTCState *s = opaque;
413 strongarm_rtc_hzupdate(s);
415 return 0;
418 static int strongarm_rtc_post_load(void *opaque, int version_id)
420 StrongARMRTCState *s = opaque;
422 strongarm_rtc_timer_update(s);
423 strongarm_rtc_int_update(s);
425 return 0;
428 static const VMStateDescription vmstate_strongarm_rtc_regs = {
429 .name = "strongarm-rtc",
430 .version_id = 0,
431 .minimum_version_id = 0,
432 .pre_save = strongarm_rtc_pre_save,
433 .post_load = strongarm_rtc_post_load,
434 .fields = (VMStateField[]) {
435 VMSTATE_UINT32(rttr, StrongARMRTCState),
436 VMSTATE_UINT32(rtsr, StrongARMRTCState),
437 VMSTATE_UINT32(rtar, StrongARMRTCState),
438 VMSTATE_UINT32(last_rcnr, StrongARMRTCState),
439 VMSTATE_INT64(last_hz, StrongARMRTCState),
440 VMSTATE_END_OF_LIST(),
444 static void strongarm_rtc_sysbus_class_init(ObjectClass *klass, void *data)
446 DeviceClass *dc = DEVICE_CLASS(klass);
448 dc->desc = "StrongARM RTC Controller";
449 dc->vmsd = &vmstate_strongarm_rtc_regs;
452 static const TypeInfo strongarm_rtc_sysbus_info = {
453 .name = TYPE_STRONGARM_RTC,
454 .parent = TYPE_SYS_BUS_DEVICE,
455 .instance_size = sizeof(StrongARMRTCState),
456 .instance_init = strongarm_rtc_init,
457 .class_init = strongarm_rtc_sysbus_class_init,
460 /* GPIO */
461 #define GPLR 0x00
462 #define GPDR 0x04
463 #define GPSR 0x08
464 #define GPCR 0x0c
465 #define GRER 0x10
466 #define GFER 0x14
467 #define GEDR 0x18
468 #define GAFR 0x1c
470 #define TYPE_STRONGARM_GPIO "strongarm-gpio"
471 #define STRONGARM_GPIO(obj) \
472 OBJECT_CHECK(StrongARMGPIOInfo, (obj), TYPE_STRONGARM_GPIO)
474 typedef struct StrongARMGPIOInfo StrongARMGPIOInfo;
475 struct StrongARMGPIOInfo {
476 SysBusDevice busdev;
477 MemoryRegion iomem;
478 qemu_irq handler[28];
479 qemu_irq irqs[11];
480 qemu_irq irqX;
482 uint32_t ilevel;
483 uint32_t olevel;
484 uint32_t dir;
485 uint32_t rising;
486 uint32_t falling;
487 uint32_t status;
488 uint32_t gafr;
490 uint32_t prev_level;
494 static void strongarm_gpio_irq_update(StrongARMGPIOInfo *s)
496 int i;
497 for (i = 0; i < 11; i++) {
498 qemu_set_irq(s->irqs[i], s->status & (1 << i));
501 qemu_set_irq(s->irqX, (s->status & ~0x7ff));
504 static void strongarm_gpio_set(void *opaque, int line, int level)
506 StrongARMGPIOInfo *s = opaque;
507 uint32_t mask;
509 mask = 1 << line;
511 if (level) {
512 s->status |= s->rising & mask &
513 ~s->ilevel & ~s->dir;
514 s->ilevel |= mask;
515 } else {
516 s->status |= s->falling & mask &
517 s->ilevel & ~s->dir;
518 s->ilevel &= ~mask;
521 if (s->status & mask) {
522 strongarm_gpio_irq_update(s);
526 static void strongarm_gpio_handler_update(StrongARMGPIOInfo *s)
528 uint32_t level, diff;
529 int bit;
531 level = s->olevel & s->dir;
533 for (diff = s->prev_level ^ level; diff; diff ^= 1 << bit) {
534 bit = ctz32(diff);
535 qemu_set_irq(s->handler[bit], (level >> bit) & 1);
538 s->prev_level = level;
541 static uint64_t strongarm_gpio_read(void *opaque, hwaddr offset,
542 unsigned size)
544 StrongARMGPIOInfo *s = opaque;
546 switch (offset) {
547 case GPDR: /* GPIO Pin-Direction registers */
548 return s->dir;
550 case GPSR: /* GPIO Pin-Output Set registers */
551 qemu_log_mask(LOG_GUEST_ERROR,
552 "strongarm GPIO: read from write only register GPSR\n");
553 return 0;
555 case GPCR: /* GPIO Pin-Output Clear registers */
556 qemu_log_mask(LOG_GUEST_ERROR,
557 "strongarm GPIO: read from write only register GPCR\n");
558 return 0;
560 case GRER: /* GPIO Rising-Edge Detect Enable registers */
561 return s->rising;
563 case GFER: /* GPIO Falling-Edge Detect Enable registers */
564 return s->falling;
566 case GAFR: /* GPIO Alternate Function registers */
567 return s->gafr;
569 case GPLR: /* GPIO Pin-Level registers */
570 return (s->olevel & s->dir) |
571 (s->ilevel & ~s->dir);
573 case GEDR: /* GPIO Edge Detect Status registers */
574 return s->status;
576 default:
577 printf("%s: Bad offset 0x" TARGET_FMT_plx "\n", __func__, offset);
580 return 0;
583 static void strongarm_gpio_write(void *opaque, hwaddr offset,
584 uint64_t value, unsigned size)
586 StrongARMGPIOInfo *s = opaque;
588 switch (offset) {
589 case GPDR: /* GPIO Pin-Direction registers */
590 s->dir = value;
591 strongarm_gpio_handler_update(s);
592 break;
594 case GPSR: /* GPIO Pin-Output Set registers */
595 s->olevel |= value;
596 strongarm_gpio_handler_update(s);
597 break;
599 case GPCR: /* GPIO Pin-Output Clear registers */
600 s->olevel &= ~value;
601 strongarm_gpio_handler_update(s);
602 break;
604 case GRER: /* GPIO Rising-Edge Detect Enable registers */
605 s->rising = value;
606 break;
608 case GFER: /* GPIO Falling-Edge Detect Enable registers */
609 s->falling = value;
610 break;
612 case GAFR: /* GPIO Alternate Function registers */
613 s->gafr = value;
614 break;
616 case GEDR: /* GPIO Edge Detect Status registers */
617 s->status &= ~value;
618 strongarm_gpio_irq_update(s);
619 break;
621 default:
622 printf("%s: Bad offset 0x" TARGET_FMT_plx "\n", __func__, offset);
626 static const MemoryRegionOps strongarm_gpio_ops = {
627 .read = strongarm_gpio_read,
628 .write = strongarm_gpio_write,
629 .endianness = DEVICE_NATIVE_ENDIAN,
632 static DeviceState *strongarm_gpio_init(hwaddr base,
633 DeviceState *pic)
635 DeviceState *dev;
636 int i;
638 dev = qdev_create(NULL, TYPE_STRONGARM_GPIO);
639 qdev_init_nofail(dev);
641 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
642 for (i = 0; i < 12; i++)
643 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
644 qdev_get_gpio_in(pic, SA_PIC_GPIO0_EDGE + i));
646 return dev;
649 static void strongarm_gpio_initfn(Object *obj)
651 DeviceState *dev = DEVICE(obj);
652 StrongARMGPIOInfo *s = STRONGARM_GPIO(obj);
653 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
654 int i;
656 qdev_init_gpio_in(dev, strongarm_gpio_set, 28);
657 qdev_init_gpio_out(dev, s->handler, 28);
659 memory_region_init_io(&s->iomem, obj, &strongarm_gpio_ops, s,
660 "gpio", 0x1000);
662 sysbus_init_mmio(sbd, &s->iomem);
663 for (i = 0; i < 11; i++) {
664 sysbus_init_irq(sbd, &s->irqs[i]);
666 sysbus_init_irq(sbd, &s->irqX);
669 static const VMStateDescription vmstate_strongarm_gpio_regs = {
670 .name = "strongarm-gpio",
671 .version_id = 0,
672 .minimum_version_id = 0,
673 .fields = (VMStateField[]) {
674 VMSTATE_UINT32(ilevel, StrongARMGPIOInfo),
675 VMSTATE_UINT32(olevel, StrongARMGPIOInfo),
676 VMSTATE_UINT32(dir, StrongARMGPIOInfo),
677 VMSTATE_UINT32(rising, StrongARMGPIOInfo),
678 VMSTATE_UINT32(falling, StrongARMGPIOInfo),
679 VMSTATE_UINT32(status, StrongARMGPIOInfo),
680 VMSTATE_UINT32(gafr, StrongARMGPIOInfo),
681 VMSTATE_UINT32(prev_level, StrongARMGPIOInfo),
682 VMSTATE_END_OF_LIST(),
686 static void strongarm_gpio_class_init(ObjectClass *klass, void *data)
688 DeviceClass *dc = DEVICE_CLASS(klass);
690 dc->desc = "StrongARM GPIO controller";
691 dc->vmsd = &vmstate_strongarm_gpio_regs;
694 static const TypeInfo strongarm_gpio_info = {
695 .name = TYPE_STRONGARM_GPIO,
696 .parent = TYPE_SYS_BUS_DEVICE,
697 .instance_size = sizeof(StrongARMGPIOInfo),
698 .instance_init = strongarm_gpio_initfn,
699 .class_init = strongarm_gpio_class_init,
702 /* Peripheral Pin Controller */
703 #define PPDR 0x00
704 #define PPSR 0x04
705 #define PPAR 0x08
706 #define PSDR 0x0c
707 #define PPFR 0x10
709 #define TYPE_STRONGARM_PPC "strongarm-ppc"
710 #define STRONGARM_PPC(obj) \
711 OBJECT_CHECK(StrongARMPPCInfo, (obj), TYPE_STRONGARM_PPC)
713 typedef struct StrongARMPPCInfo StrongARMPPCInfo;
714 struct StrongARMPPCInfo {
715 SysBusDevice parent_obj;
717 MemoryRegion iomem;
718 qemu_irq handler[28];
720 uint32_t ilevel;
721 uint32_t olevel;
722 uint32_t dir;
723 uint32_t ppar;
724 uint32_t psdr;
725 uint32_t ppfr;
727 uint32_t prev_level;
730 static void strongarm_ppc_set(void *opaque, int line, int level)
732 StrongARMPPCInfo *s = opaque;
734 if (level) {
735 s->ilevel |= 1 << line;
736 } else {
737 s->ilevel &= ~(1 << line);
741 static void strongarm_ppc_handler_update(StrongARMPPCInfo *s)
743 uint32_t level, diff;
744 int bit;
746 level = s->olevel & s->dir;
748 for (diff = s->prev_level ^ level; diff; diff ^= 1 << bit) {
749 bit = ctz32(diff);
750 qemu_set_irq(s->handler[bit], (level >> bit) & 1);
753 s->prev_level = level;
756 static uint64_t strongarm_ppc_read(void *opaque, hwaddr offset,
757 unsigned size)
759 StrongARMPPCInfo *s = opaque;
761 switch (offset) {
762 case PPDR: /* PPC Pin Direction registers */
763 return s->dir | ~0x3fffff;
765 case PPSR: /* PPC Pin State registers */
766 return (s->olevel & s->dir) |
767 (s->ilevel & ~s->dir) |
768 ~0x3fffff;
770 case PPAR:
771 return s->ppar | ~0x41000;
773 case PSDR:
774 return s->psdr;
776 case PPFR:
777 return s->ppfr | ~0x7f001;
779 default:
780 printf("%s: Bad offset 0x" TARGET_FMT_plx "\n", __func__, offset);
783 return 0;
786 static void strongarm_ppc_write(void *opaque, hwaddr offset,
787 uint64_t value, unsigned size)
789 StrongARMPPCInfo *s = opaque;
791 switch (offset) {
792 case PPDR: /* PPC Pin Direction registers */
793 s->dir = value & 0x3fffff;
794 strongarm_ppc_handler_update(s);
795 break;
797 case PPSR: /* PPC Pin State registers */
798 s->olevel = value & s->dir & 0x3fffff;
799 strongarm_ppc_handler_update(s);
800 break;
802 case PPAR:
803 s->ppar = value & 0x41000;
804 break;
806 case PSDR:
807 s->psdr = value & 0x3fffff;
808 break;
810 case PPFR:
811 s->ppfr = value & 0x7f001;
812 break;
814 default:
815 printf("%s: Bad offset 0x" TARGET_FMT_plx "\n", __func__, offset);
819 static const MemoryRegionOps strongarm_ppc_ops = {
820 .read = strongarm_ppc_read,
821 .write = strongarm_ppc_write,
822 .endianness = DEVICE_NATIVE_ENDIAN,
825 static void strongarm_ppc_init(Object *obj)
827 DeviceState *dev = DEVICE(obj);
828 StrongARMPPCInfo *s = STRONGARM_PPC(obj);
829 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
831 qdev_init_gpio_in(dev, strongarm_ppc_set, 22);
832 qdev_init_gpio_out(dev, s->handler, 22);
834 memory_region_init_io(&s->iomem, obj, &strongarm_ppc_ops, s,
835 "ppc", 0x1000);
837 sysbus_init_mmio(sbd, &s->iomem);
840 static const VMStateDescription vmstate_strongarm_ppc_regs = {
841 .name = "strongarm-ppc",
842 .version_id = 0,
843 .minimum_version_id = 0,
844 .fields = (VMStateField[]) {
845 VMSTATE_UINT32(ilevel, StrongARMPPCInfo),
846 VMSTATE_UINT32(olevel, StrongARMPPCInfo),
847 VMSTATE_UINT32(dir, StrongARMPPCInfo),
848 VMSTATE_UINT32(ppar, StrongARMPPCInfo),
849 VMSTATE_UINT32(psdr, StrongARMPPCInfo),
850 VMSTATE_UINT32(ppfr, StrongARMPPCInfo),
851 VMSTATE_UINT32(prev_level, StrongARMPPCInfo),
852 VMSTATE_END_OF_LIST(),
856 static void strongarm_ppc_class_init(ObjectClass *klass, void *data)
858 DeviceClass *dc = DEVICE_CLASS(klass);
860 dc->desc = "StrongARM PPC controller";
861 dc->vmsd = &vmstate_strongarm_ppc_regs;
864 static const TypeInfo strongarm_ppc_info = {
865 .name = TYPE_STRONGARM_PPC,
866 .parent = TYPE_SYS_BUS_DEVICE,
867 .instance_size = sizeof(StrongARMPPCInfo),
868 .instance_init = strongarm_ppc_init,
869 .class_init = strongarm_ppc_class_init,
872 /* UART Ports */
873 #define UTCR0 0x00
874 #define UTCR1 0x04
875 #define UTCR2 0x08
876 #define UTCR3 0x0c
877 #define UTDR 0x14
878 #define UTSR0 0x1c
879 #define UTSR1 0x20
881 #define UTCR0_PE (1 << 0) /* Parity enable */
882 #define UTCR0_OES (1 << 1) /* Even parity */
883 #define UTCR0_SBS (1 << 2) /* 2 stop bits */
884 #define UTCR0_DSS (1 << 3) /* 8-bit data */
886 #define UTCR3_RXE (1 << 0) /* Rx enable */
887 #define UTCR3_TXE (1 << 1) /* Tx enable */
888 #define UTCR3_BRK (1 << 2) /* Force Break */
889 #define UTCR3_RIE (1 << 3) /* Rx int enable */
890 #define UTCR3_TIE (1 << 4) /* Tx int enable */
891 #define UTCR3_LBM (1 << 5) /* Loopback */
893 #define UTSR0_TFS (1 << 0) /* Tx FIFO nearly empty */
894 #define UTSR0_RFS (1 << 1) /* Rx FIFO nearly full */
895 #define UTSR0_RID (1 << 2) /* Receiver Idle */
896 #define UTSR0_RBB (1 << 3) /* Receiver begin break */
897 #define UTSR0_REB (1 << 4) /* Receiver end break */
898 #define UTSR0_EIF (1 << 5) /* Error in FIFO */
900 #define UTSR1_RNE (1 << 1) /* Receive FIFO not empty */
901 #define UTSR1_TNF (1 << 2) /* Transmit FIFO not full */
902 #define UTSR1_PRE (1 << 3) /* Parity error */
903 #define UTSR1_FRE (1 << 4) /* Frame error */
904 #define UTSR1_ROR (1 << 5) /* Receive Over Run */
906 #define RX_FIFO_PRE (1 << 8)
907 #define RX_FIFO_FRE (1 << 9)
908 #define RX_FIFO_ROR (1 << 10)
910 #define TYPE_STRONGARM_UART "strongarm-uart"
911 #define STRONGARM_UART(obj) \
912 OBJECT_CHECK(StrongARMUARTState, (obj), TYPE_STRONGARM_UART)
914 typedef struct StrongARMUARTState {
915 SysBusDevice parent_obj;
917 MemoryRegion iomem;
918 CharBackend chr;
919 qemu_irq irq;
921 uint8_t utcr0;
922 uint16_t brd;
923 uint8_t utcr3;
924 uint8_t utsr0;
925 uint8_t utsr1;
927 uint8_t tx_fifo[8];
928 uint8_t tx_start;
929 uint8_t tx_len;
930 uint16_t rx_fifo[12]; /* value + error flags in high bits */
931 uint8_t rx_start;
932 uint8_t rx_len;
934 uint64_t char_transmit_time; /* time to transmit a char in ticks*/
935 bool wait_break_end;
936 QEMUTimer *rx_timeout_timer;
937 QEMUTimer *tx_timer;
938 } StrongARMUARTState;
940 static void strongarm_uart_update_status(StrongARMUARTState *s)
942 uint16_t utsr1 = 0;
944 if (s->tx_len != 8) {
945 utsr1 |= UTSR1_TNF;
948 if (s->rx_len != 0) {
949 uint16_t ent = s->rx_fifo[s->rx_start];
951 utsr1 |= UTSR1_RNE;
952 if (ent & RX_FIFO_PRE) {
953 s->utsr1 |= UTSR1_PRE;
955 if (ent & RX_FIFO_FRE) {
956 s->utsr1 |= UTSR1_FRE;
958 if (ent & RX_FIFO_ROR) {
959 s->utsr1 |= UTSR1_ROR;
963 s->utsr1 = utsr1;
966 static void strongarm_uart_update_int_status(StrongARMUARTState *s)
968 uint16_t utsr0 = s->utsr0 &
969 (UTSR0_REB | UTSR0_RBB | UTSR0_RID);
970 int i;
972 if ((s->utcr3 & UTCR3_TXE) &&
973 (s->utcr3 & UTCR3_TIE) &&
974 s->tx_len <= 4) {
975 utsr0 |= UTSR0_TFS;
978 if ((s->utcr3 & UTCR3_RXE) &&
979 (s->utcr3 & UTCR3_RIE) &&
980 s->rx_len > 4) {
981 utsr0 |= UTSR0_RFS;
984 for (i = 0; i < s->rx_len && i < 4; i++)
985 if (s->rx_fifo[(s->rx_start + i) % 12] & ~0xff) {
986 utsr0 |= UTSR0_EIF;
987 break;
990 s->utsr0 = utsr0;
991 qemu_set_irq(s->irq, utsr0);
994 static void strongarm_uart_update_parameters(StrongARMUARTState *s)
996 int speed, parity, data_bits, stop_bits, frame_size;
997 QEMUSerialSetParams ssp;
999 /* Start bit. */
1000 frame_size = 1;
1001 if (s->utcr0 & UTCR0_PE) {
1002 /* Parity bit. */
1003 frame_size++;
1004 if (s->utcr0 & UTCR0_OES) {
1005 parity = 'E';
1006 } else {
1007 parity = 'O';
1009 } else {
1010 parity = 'N';
1012 if (s->utcr0 & UTCR0_SBS) {
1013 stop_bits = 2;
1014 } else {
1015 stop_bits = 1;
1018 data_bits = (s->utcr0 & UTCR0_DSS) ? 8 : 7;
1019 frame_size += data_bits + stop_bits;
1020 speed = 3686400 / 16 / (s->brd + 1);
1021 ssp.speed = speed;
1022 ssp.parity = parity;
1023 ssp.data_bits = data_bits;
1024 ssp.stop_bits = stop_bits;
1025 s->char_transmit_time = (NANOSECONDS_PER_SECOND / speed) * frame_size;
1026 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
1028 DPRINTF(stderr, "%s speed=%d parity=%c data=%d stop=%d\n", s->chr->label,
1029 speed, parity, data_bits, stop_bits);
1032 static void strongarm_uart_rx_to(void *opaque)
1034 StrongARMUARTState *s = opaque;
1036 if (s->rx_len) {
1037 s->utsr0 |= UTSR0_RID;
1038 strongarm_uart_update_int_status(s);
1042 static void strongarm_uart_rx_push(StrongARMUARTState *s, uint16_t c)
1044 if ((s->utcr3 & UTCR3_RXE) == 0) {
1045 /* rx disabled */
1046 return;
1049 if (s->wait_break_end) {
1050 s->utsr0 |= UTSR0_REB;
1051 s->wait_break_end = false;
1054 if (s->rx_len < 12) {
1055 s->rx_fifo[(s->rx_start + s->rx_len) % 12] = c;
1056 s->rx_len++;
1057 } else
1058 s->rx_fifo[(s->rx_start + 11) % 12] |= RX_FIFO_ROR;
1061 static int strongarm_uart_can_receive(void *opaque)
1063 StrongARMUARTState *s = opaque;
1065 if (s->rx_len == 12) {
1066 return 0;
1068 /* It's best not to get more than 2/3 of RX FIFO, so advertise that much */
1069 if (s->rx_len < 8) {
1070 return 8 - s->rx_len;
1072 return 1;
1075 static void strongarm_uart_receive(void *opaque, const uint8_t *buf, int size)
1077 StrongARMUARTState *s = opaque;
1078 int i;
1080 for (i = 0; i < size; i++) {
1081 strongarm_uart_rx_push(s, buf[i]);
1084 /* call the timeout receive callback in 3 char transmit time */
1085 timer_mod(s->rx_timeout_timer,
1086 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->char_transmit_time * 3);
1088 strongarm_uart_update_status(s);
1089 strongarm_uart_update_int_status(s);
1092 static void strongarm_uart_event(void *opaque, int event)
1094 StrongARMUARTState *s = opaque;
1095 if (event == CHR_EVENT_BREAK) {
1096 s->utsr0 |= UTSR0_RBB;
1097 strongarm_uart_rx_push(s, RX_FIFO_FRE);
1098 s->wait_break_end = true;
1099 strongarm_uart_update_status(s);
1100 strongarm_uart_update_int_status(s);
1104 static void strongarm_uart_tx(void *opaque)
1106 StrongARMUARTState *s = opaque;
1107 uint64_t new_xmit_ts = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1109 if (s->utcr3 & UTCR3_LBM) /* loopback */ {
1110 strongarm_uart_receive(s, &s->tx_fifo[s->tx_start], 1);
1111 } else if (qemu_chr_fe_backend_connected(&s->chr)) {
1112 /* XXX this blocks entire thread. Rewrite to use
1113 * qemu_chr_fe_write and background I/O callbacks */
1114 qemu_chr_fe_write_all(&s->chr, &s->tx_fifo[s->tx_start], 1);
1117 s->tx_start = (s->tx_start + 1) % 8;
1118 s->tx_len--;
1119 if (s->tx_len) {
1120 timer_mod(s->tx_timer, new_xmit_ts + s->char_transmit_time);
1122 strongarm_uart_update_status(s);
1123 strongarm_uart_update_int_status(s);
1126 static uint64_t strongarm_uart_read(void *opaque, hwaddr addr,
1127 unsigned size)
1129 StrongARMUARTState *s = opaque;
1130 uint16_t ret;
1132 switch (addr) {
1133 case UTCR0:
1134 return s->utcr0;
1136 case UTCR1:
1137 return s->brd >> 8;
1139 case UTCR2:
1140 return s->brd & 0xff;
1142 case UTCR3:
1143 return s->utcr3;
1145 case UTDR:
1146 if (s->rx_len != 0) {
1147 ret = s->rx_fifo[s->rx_start];
1148 s->rx_start = (s->rx_start + 1) % 12;
1149 s->rx_len--;
1150 strongarm_uart_update_status(s);
1151 strongarm_uart_update_int_status(s);
1152 return ret;
1154 return 0;
1156 case UTSR0:
1157 return s->utsr0;
1159 case UTSR1:
1160 return s->utsr1;
1162 default:
1163 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr);
1164 return 0;
1168 static void strongarm_uart_write(void *opaque, hwaddr addr,
1169 uint64_t value, unsigned size)
1171 StrongARMUARTState *s = opaque;
1173 switch (addr) {
1174 case UTCR0:
1175 s->utcr0 = value & 0x7f;
1176 strongarm_uart_update_parameters(s);
1177 break;
1179 case UTCR1:
1180 s->brd = (s->brd & 0xff) | ((value & 0xf) << 8);
1181 strongarm_uart_update_parameters(s);
1182 break;
1184 case UTCR2:
1185 s->brd = (s->brd & 0xf00) | (value & 0xff);
1186 strongarm_uart_update_parameters(s);
1187 break;
1189 case UTCR3:
1190 s->utcr3 = value & 0x3f;
1191 if ((s->utcr3 & UTCR3_RXE) == 0) {
1192 s->rx_len = 0;
1194 if ((s->utcr3 & UTCR3_TXE) == 0) {
1195 s->tx_len = 0;
1197 strongarm_uart_update_status(s);
1198 strongarm_uart_update_int_status(s);
1199 break;
1201 case UTDR:
1202 if ((s->utcr3 & UTCR3_TXE) && s->tx_len != 8) {
1203 s->tx_fifo[(s->tx_start + s->tx_len) % 8] = value;
1204 s->tx_len++;
1205 strongarm_uart_update_status(s);
1206 strongarm_uart_update_int_status(s);
1207 if (s->tx_len == 1) {
1208 strongarm_uart_tx(s);
1211 break;
1213 case UTSR0:
1214 s->utsr0 = s->utsr0 & ~(value &
1215 (UTSR0_REB | UTSR0_RBB | UTSR0_RID));
1216 strongarm_uart_update_int_status(s);
1217 break;
1219 default:
1220 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr);
1224 static const MemoryRegionOps strongarm_uart_ops = {
1225 .read = strongarm_uart_read,
1226 .write = strongarm_uart_write,
1227 .endianness = DEVICE_NATIVE_ENDIAN,
1230 static void strongarm_uart_init(Object *obj)
1232 StrongARMUARTState *s = STRONGARM_UART(obj);
1233 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
1235 memory_region_init_io(&s->iomem, obj, &strongarm_uart_ops, s,
1236 "uart", 0x10000);
1237 sysbus_init_mmio(dev, &s->iomem);
1238 sysbus_init_irq(dev, &s->irq);
1240 s->rx_timeout_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, strongarm_uart_rx_to, s);
1241 s->tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, strongarm_uart_tx, s);
1244 static void strongarm_uart_realize(DeviceState *dev, Error **errp)
1246 StrongARMUARTState *s = STRONGARM_UART(dev);
1248 qemu_chr_fe_set_handlers(&s->chr,
1249 strongarm_uart_can_receive,
1250 strongarm_uart_receive,
1251 strongarm_uart_event,
1252 NULL, s, NULL, true);
1255 static void strongarm_uart_reset(DeviceState *dev)
1257 StrongARMUARTState *s = STRONGARM_UART(dev);
1259 s->utcr0 = UTCR0_DSS; /* 8 data, no parity */
1260 s->brd = 23; /* 9600 */
1261 /* enable send & recv - this actually violates spec */
1262 s->utcr3 = UTCR3_TXE | UTCR3_RXE;
1264 s->rx_len = s->tx_len = 0;
1266 strongarm_uart_update_parameters(s);
1267 strongarm_uart_update_status(s);
1268 strongarm_uart_update_int_status(s);
1271 static int strongarm_uart_post_load(void *opaque, int version_id)
1273 StrongARMUARTState *s = opaque;
1275 strongarm_uart_update_parameters(s);
1276 strongarm_uart_update_status(s);
1277 strongarm_uart_update_int_status(s);
1279 /* tx and restart timer */
1280 if (s->tx_len) {
1281 strongarm_uart_tx(s);
1284 /* restart rx timeout timer */
1285 if (s->rx_len) {
1286 timer_mod(s->rx_timeout_timer,
1287 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->char_transmit_time * 3);
1290 return 0;
1293 static const VMStateDescription vmstate_strongarm_uart_regs = {
1294 .name = "strongarm-uart",
1295 .version_id = 0,
1296 .minimum_version_id = 0,
1297 .post_load = strongarm_uart_post_load,
1298 .fields = (VMStateField[]) {
1299 VMSTATE_UINT8(utcr0, StrongARMUARTState),
1300 VMSTATE_UINT16(brd, StrongARMUARTState),
1301 VMSTATE_UINT8(utcr3, StrongARMUARTState),
1302 VMSTATE_UINT8(utsr0, StrongARMUARTState),
1303 VMSTATE_UINT8_ARRAY(tx_fifo, StrongARMUARTState, 8),
1304 VMSTATE_UINT8(tx_start, StrongARMUARTState),
1305 VMSTATE_UINT8(tx_len, StrongARMUARTState),
1306 VMSTATE_UINT16_ARRAY(rx_fifo, StrongARMUARTState, 12),
1307 VMSTATE_UINT8(rx_start, StrongARMUARTState),
1308 VMSTATE_UINT8(rx_len, StrongARMUARTState),
1309 VMSTATE_BOOL(wait_break_end, StrongARMUARTState),
1310 VMSTATE_END_OF_LIST(),
1314 static Property strongarm_uart_properties[] = {
1315 DEFINE_PROP_CHR("chardev", StrongARMUARTState, chr),
1316 DEFINE_PROP_END_OF_LIST(),
1319 static void strongarm_uart_class_init(ObjectClass *klass, void *data)
1321 DeviceClass *dc = DEVICE_CLASS(klass);
1323 dc->desc = "StrongARM UART controller";
1324 dc->reset = strongarm_uart_reset;
1325 dc->vmsd = &vmstate_strongarm_uart_regs;
1326 dc->props = strongarm_uart_properties;
1327 dc->realize = strongarm_uart_realize;
1330 static const TypeInfo strongarm_uart_info = {
1331 .name = TYPE_STRONGARM_UART,
1332 .parent = TYPE_SYS_BUS_DEVICE,
1333 .instance_size = sizeof(StrongARMUARTState),
1334 .instance_init = strongarm_uart_init,
1335 .class_init = strongarm_uart_class_init,
1338 /* Synchronous Serial Ports */
1340 #define TYPE_STRONGARM_SSP "strongarm-ssp"
1341 #define STRONGARM_SSP(obj) \
1342 OBJECT_CHECK(StrongARMSSPState, (obj), TYPE_STRONGARM_SSP)
1344 typedef struct StrongARMSSPState {
1345 SysBusDevice parent_obj;
1347 MemoryRegion iomem;
1348 qemu_irq irq;
1349 SSIBus *bus;
1351 uint16_t sscr[2];
1352 uint16_t sssr;
1354 uint16_t rx_fifo[8];
1355 uint8_t rx_level;
1356 uint8_t rx_start;
1357 } StrongARMSSPState;
1359 #define SSCR0 0x60 /* SSP Control register 0 */
1360 #define SSCR1 0x64 /* SSP Control register 1 */
1361 #define SSDR 0x6c /* SSP Data register */
1362 #define SSSR 0x74 /* SSP Status register */
1364 /* Bitfields for above registers */
1365 #define SSCR0_SPI(x) (((x) & 0x30) == 0x00)
1366 #define SSCR0_SSP(x) (((x) & 0x30) == 0x10)
1367 #define SSCR0_UWIRE(x) (((x) & 0x30) == 0x20)
1368 #define SSCR0_PSP(x) (((x) & 0x30) == 0x30)
1369 #define SSCR0_SSE (1 << 7)
1370 #define SSCR0_DSS(x) (((x) & 0xf) + 1)
1371 #define SSCR1_RIE (1 << 0)
1372 #define SSCR1_TIE (1 << 1)
1373 #define SSCR1_LBM (1 << 2)
1374 #define SSSR_TNF (1 << 2)
1375 #define SSSR_RNE (1 << 3)
1376 #define SSSR_TFS (1 << 5)
1377 #define SSSR_RFS (1 << 6)
1378 #define SSSR_ROR (1 << 7)
1379 #define SSSR_RW 0x0080
1381 static void strongarm_ssp_int_update(StrongARMSSPState *s)
1383 int level = 0;
1385 level |= (s->sssr & SSSR_ROR);
1386 level |= (s->sssr & SSSR_RFS) && (s->sscr[1] & SSCR1_RIE);
1387 level |= (s->sssr & SSSR_TFS) && (s->sscr[1] & SSCR1_TIE);
1388 qemu_set_irq(s->irq, level);
1391 static void strongarm_ssp_fifo_update(StrongARMSSPState *s)
1393 s->sssr &= ~SSSR_TFS;
1394 s->sssr &= ~SSSR_TNF;
1395 if (s->sscr[0] & SSCR0_SSE) {
1396 if (s->rx_level >= 4) {
1397 s->sssr |= SSSR_RFS;
1398 } else {
1399 s->sssr &= ~SSSR_RFS;
1401 if (s->rx_level) {
1402 s->sssr |= SSSR_RNE;
1403 } else {
1404 s->sssr &= ~SSSR_RNE;
1406 /* TX FIFO is never filled, so it is always in underrun
1407 condition if SSP is enabled */
1408 s->sssr |= SSSR_TFS;
1409 s->sssr |= SSSR_TNF;
1412 strongarm_ssp_int_update(s);
1415 static uint64_t strongarm_ssp_read(void *opaque, hwaddr addr,
1416 unsigned size)
1418 StrongARMSSPState *s = opaque;
1419 uint32_t retval;
1421 switch (addr) {
1422 case SSCR0:
1423 return s->sscr[0];
1424 case SSCR1:
1425 return s->sscr[1];
1426 case SSSR:
1427 return s->sssr;
1428 case SSDR:
1429 if (~s->sscr[0] & SSCR0_SSE) {
1430 return 0xffffffff;
1432 if (s->rx_level < 1) {
1433 printf("%s: SSP Rx Underrun\n", __func__);
1434 return 0xffffffff;
1436 s->rx_level--;
1437 retval = s->rx_fifo[s->rx_start++];
1438 s->rx_start &= 0x7;
1439 strongarm_ssp_fifo_update(s);
1440 return retval;
1441 default:
1442 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr);
1443 break;
1445 return 0;
1448 static void strongarm_ssp_write(void *opaque, hwaddr addr,
1449 uint64_t value, unsigned size)
1451 StrongARMSSPState *s = opaque;
1453 switch (addr) {
1454 case SSCR0:
1455 s->sscr[0] = value & 0xffbf;
1456 if ((s->sscr[0] & SSCR0_SSE) && SSCR0_DSS(value) < 4) {
1457 printf("%s: Wrong data size: %i bits\n", __func__,
1458 (int)SSCR0_DSS(value));
1460 if (!(value & SSCR0_SSE)) {
1461 s->sssr = 0;
1462 s->rx_level = 0;
1464 strongarm_ssp_fifo_update(s);
1465 break;
1467 case SSCR1:
1468 s->sscr[1] = value & 0x2f;
1469 if (value & SSCR1_LBM) {
1470 printf("%s: Attempt to use SSP LBM mode\n", __func__);
1472 strongarm_ssp_fifo_update(s);
1473 break;
1475 case SSSR:
1476 s->sssr &= ~(value & SSSR_RW);
1477 strongarm_ssp_int_update(s);
1478 break;
1480 case SSDR:
1481 if (SSCR0_UWIRE(s->sscr[0])) {
1482 value &= 0xff;
1483 } else
1484 /* Note how 32bits overflow does no harm here */
1485 value &= (1 << SSCR0_DSS(s->sscr[0])) - 1;
1487 /* Data goes from here to the Tx FIFO and is shifted out from
1488 * there directly to the slave, no need to buffer it.
1490 if (s->sscr[0] & SSCR0_SSE) {
1491 uint32_t readval;
1492 if (s->sscr[1] & SSCR1_LBM) {
1493 readval = value;
1494 } else {
1495 readval = ssi_transfer(s->bus, value);
1498 if (s->rx_level < 0x08) {
1499 s->rx_fifo[(s->rx_start + s->rx_level++) & 0x7] = readval;
1500 } else {
1501 s->sssr |= SSSR_ROR;
1504 strongarm_ssp_fifo_update(s);
1505 break;
1507 default:
1508 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr);
1509 break;
1513 static const MemoryRegionOps strongarm_ssp_ops = {
1514 .read = strongarm_ssp_read,
1515 .write = strongarm_ssp_write,
1516 .endianness = DEVICE_NATIVE_ENDIAN,
1519 static int strongarm_ssp_post_load(void *opaque, int version_id)
1521 StrongARMSSPState *s = opaque;
1523 strongarm_ssp_fifo_update(s);
1525 return 0;
1528 static void strongarm_ssp_init(Object *obj)
1530 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
1531 DeviceState *dev = DEVICE(sbd);
1532 StrongARMSSPState *s = STRONGARM_SSP(dev);
1534 sysbus_init_irq(sbd, &s->irq);
1536 memory_region_init_io(&s->iomem, obj, &strongarm_ssp_ops, s,
1537 "ssp", 0x1000);
1538 sysbus_init_mmio(sbd, &s->iomem);
1540 s->bus = ssi_create_bus(dev, "ssi");
1543 static void strongarm_ssp_reset(DeviceState *dev)
1545 StrongARMSSPState *s = STRONGARM_SSP(dev);
1547 s->sssr = 0x03; /* 3 bit data, SPI, disabled */
1548 s->rx_start = 0;
1549 s->rx_level = 0;
1552 static const VMStateDescription vmstate_strongarm_ssp_regs = {
1553 .name = "strongarm-ssp",
1554 .version_id = 0,
1555 .minimum_version_id = 0,
1556 .post_load = strongarm_ssp_post_load,
1557 .fields = (VMStateField[]) {
1558 VMSTATE_UINT16_ARRAY(sscr, StrongARMSSPState, 2),
1559 VMSTATE_UINT16(sssr, StrongARMSSPState),
1560 VMSTATE_UINT16_ARRAY(rx_fifo, StrongARMSSPState, 8),
1561 VMSTATE_UINT8(rx_start, StrongARMSSPState),
1562 VMSTATE_UINT8(rx_level, StrongARMSSPState),
1563 VMSTATE_END_OF_LIST(),
1567 static void strongarm_ssp_class_init(ObjectClass *klass, void *data)
1569 DeviceClass *dc = DEVICE_CLASS(klass);
1571 dc->desc = "StrongARM SSP controller";
1572 dc->reset = strongarm_ssp_reset;
1573 dc->vmsd = &vmstate_strongarm_ssp_regs;
1576 static const TypeInfo strongarm_ssp_info = {
1577 .name = TYPE_STRONGARM_SSP,
1578 .parent = TYPE_SYS_BUS_DEVICE,
1579 .instance_size = sizeof(StrongARMSSPState),
1580 .instance_init = strongarm_ssp_init,
1581 .class_init = strongarm_ssp_class_init,
1584 /* Main CPU functions */
1585 StrongARMState *sa1110_init(MemoryRegion *sysmem,
1586 unsigned int sdram_size, const char *cpu_type)
1588 StrongARMState *s;
1589 int i;
1591 s = g_new0(StrongARMState, 1);
1593 if (strncmp(cpu_type, "sa1110", 6)) {
1594 error_report("Machine requires a SA1110 processor.");
1595 exit(1);
1598 s->cpu = ARM_CPU(cpu_create(cpu_type));
1600 memory_region_allocate_system_memory(&s->sdram, NULL, "strongarm.sdram",
1601 sdram_size);
1602 memory_region_add_subregion(sysmem, SA_SDCS0, &s->sdram);
1604 s->pic = sysbus_create_varargs("strongarm_pic", 0x90050000,
1605 qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_IRQ),
1606 qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_FIQ),
1607 NULL);
1609 sysbus_create_varargs("pxa25x-timer", 0x90000000,
1610 qdev_get_gpio_in(s->pic, SA_PIC_OSTC0),
1611 qdev_get_gpio_in(s->pic, SA_PIC_OSTC1),
1612 qdev_get_gpio_in(s->pic, SA_PIC_OSTC2),
1613 qdev_get_gpio_in(s->pic, SA_PIC_OSTC3),
1614 NULL);
1616 sysbus_create_simple(TYPE_STRONGARM_RTC, 0x90010000,
1617 qdev_get_gpio_in(s->pic, SA_PIC_RTC_ALARM));
1619 s->gpio = strongarm_gpio_init(0x90040000, s->pic);
1621 s->ppc = sysbus_create_varargs(TYPE_STRONGARM_PPC, 0x90060000, NULL);
1623 for (i = 0; sa_serial[i].io_base; i++) {
1624 DeviceState *dev = qdev_create(NULL, TYPE_STRONGARM_UART);
1625 qdev_prop_set_chr(dev, "chardev", serial_hd(i));
1626 qdev_init_nofail(dev);
1627 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0,
1628 sa_serial[i].io_base);
1629 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
1630 qdev_get_gpio_in(s->pic, sa_serial[i].irq));
1633 s->ssp = sysbus_create_varargs(TYPE_STRONGARM_SSP, 0x80070000,
1634 qdev_get_gpio_in(s->pic, SA_PIC_SSP), NULL);
1635 s->ssp_bus = (SSIBus *)qdev_get_child_bus(s->ssp, "ssi");
1637 return s;
1640 static void strongarm_register_types(void)
1642 type_register_static(&strongarm_pic_info);
1643 type_register_static(&strongarm_rtc_sysbus_info);
1644 type_register_static(&strongarm_gpio_info);
1645 type_register_static(&strongarm_ppc_info);
1646 type_register_static(&strongarm_uart_info);
1647 type_register_static(&strongarm_ssp_info);
1650 type_init(strongarm_register_types)