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 "strongarm.h"
31 #include "qemu-error.h"
40 - Implement cp15, c14 ?
41 - Implement cp15, c15 !!! (idle used in L)
42 - Implement idle mode handling/DIM
43 - Implement sleep mode/Wake sources
44 - Implement reset control
45 - Implement memory control regs
47 - Maybe support MBGNT/MBREQ
52 - Enhance UART with modem signals
56 # define DPRINTF(format, ...) printf(format , ## __VA_ARGS__)
58 # define DPRINTF(format, ...) do { } while (0)
62 target_phys_addr_t io_base
;
65 { 0x80010000, SA_PIC_UART1
},
66 { 0x80030000, SA_PIC_UART2
},
67 { 0x80050000, SA_PIC_UART3
},
71 /* Interrupt Controller */
91 #define SA_PIC_SRCS 32
94 static void strongarm_pic_update(void *opaque
)
96 StrongARMPICState
*s
= opaque
;
98 /* FIXME: reflect DIM */
99 qemu_set_irq(s
->fiq
, s
->pending
& s
->enabled
& s
->is_fiq
);
100 qemu_set_irq(s
->irq
, s
->pending
& s
->enabled
& ~s
->is_fiq
);
103 static void strongarm_pic_set_irq(void *opaque
, int irq
, int level
)
105 StrongARMPICState
*s
= opaque
;
108 s
->pending
|= 1 << irq
;
110 s
->pending
&= ~(1 << irq
);
113 strongarm_pic_update(s
);
116 static uint64_t strongarm_pic_mem_read(void *opaque
, target_phys_addr_t offset
,
119 StrongARMPICState
*s
= opaque
;
123 return s
->pending
& ~s
->is_fiq
& s
->enabled
;
129 return s
->int_idle
== 0;
131 return s
->pending
& s
->is_fiq
& s
->enabled
;
135 printf("%s: Bad register offset 0x" TARGET_FMT_plx
"\n",
141 static void strongarm_pic_mem_write(void *opaque
, target_phys_addr_t offset
,
142 uint64_t value
, unsigned size
)
144 StrongARMPICState
*s
= opaque
;
154 s
->int_idle
= (value
& 1) ? 0 : ~0;
157 printf("%s: Bad register offset 0x" TARGET_FMT_plx
"\n",
161 strongarm_pic_update(s
);
164 static const MemoryRegionOps strongarm_pic_ops
= {
165 .read
= strongarm_pic_mem_read
,
166 .write
= strongarm_pic_mem_write
,
167 .endianness
= DEVICE_NATIVE_ENDIAN
,
170 static int strongarm_pic_initfn(SysBusDevice
*dev
)
172 StrongARMPICState
*s
= FROM_SYSBUS(StrongARMPICState
, dev
);
174 qdev_init_gpio_in(&dev
->qdev
, strongarm_pic_set_irq
, SA_PIC_SRCS
);
175 memory_region_init_io(&s
->iomem
, &strongarm_pic_ops
, s
, "pic", 0x1000);
176 sysbus_init_mmio(dev
, &s
->iomem
);
177 sysbus_init_irq(dev
, &s
->irq
);
178 sysbus_init_irq(dev
, &s
->fiq
);
183 static int strongarm_pic_post_load(void *opaque
, int version_id
)
185 strongarm_pic_update(opaque
);
189 static VMStateDescription vmstate_strongarm_pic_regs
= {
190 .name
= "strongarm_pic",
192 .minimum_version_id
= 0,
193 .minimum_version_id_old
= 0,
194 .post_load
= strongarm_pic_post_load
,
195 .fields
= (VMStateField
[]) {
196 VMSTATE_UINT32(pending
, StrongARMPICState
),
197 VMSTATE_UINT32(enabled
, StrongARMPICState
),
198 VMSTATE_UINT32(is_fiq
, StrongARMPICState
),
199 VMSTATE_UINT32(int_idle
, StrongARMPICState
),
200 VMSTATE_END_OF_LIST(),
204 static void strongarm_pic_class_init(ObjectClass
*klass
, void *data
)
206 DeviceClass
*dc
= DEVICE_CLASS(klass
);
207 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
209 k
->init
= strongarm_pic_initfn
;
210 dc
->desc
= "StrongARM PIC";
211 dc
->vmsd
= &vmstate_strongarm_pic_regs
;
214 static TypeInfo strongarm_pic_info
= {
215 .name
= "strongarm_pic",
216 .parent
= TYPE_SYS_BUS_DEVICE
,
217 .instance_size
= sizeof(StrongARMPICState
),
218 .class_init
= strongarm_pic_class_init
,
221 /* Real-Time Clock */
222 #define RTAR 0x00 /* RTC Alarm register */
223 #define RCNR 0x04 /* RTC Counter register */
224 #define RTTR 0x08 /* RTC Timer Trim register */
225 #define RTSR 0x10 /* RTC Status register */
227 #define RTSR_AL (1 << 0) /* RTC Alarm detected */
228 #define RTSR_HZ (1 << 1) /* RTC 1Hz detected */
229 #define RTSR_ALE (1 << 2) /* RTC Alarm enable */
230 #define RTSR_HZE (1 << 3) /* RTC 1Hz enable */
232 /* 16 LSB of RTTR are clockdiv for internal trim logic,
233 * trim delete isn't emulated, so
234 * f = 32 768 / (RTTR_trim + 1) */
244 QEMUTimer
*rtc_alarm
;
250 static inline void strongarm_rtc_int_update(StrongARMRTCState
*s
)
252 qemu_set_irq(s
->rtc_irq
, s
->rtsr
& RTSR_AL
);
253 qemu_set_irq(s
->rtc_hz_irq
, s
->rtsr
& RTSR_HZ
);
256 static void strongarm_rtc_hzupdate(StrongARMRTCState
*s
)
258 int64_t rt
= qemu_get_clock_ms(rtc_clock
);
259 s
->last_rcnr
+= ((rt
- s
->last_hz
) << 15) /
260 (1000 * ((s
->rttr
& 0xffff) + 1));
264 static inline void strongarm_rtc_timer_update(StrongARMRTCState
*s
)
266 if ((s
->rtsr
& RTSR_HZE
) && !(s
->rtsr
& RTSR_HZ
)) {
267 qemu_mod_timer(s
->rtc_hz
, s
->last_hz
+ 1000);
269 qemu_del_timer(s
->rtc_hz
);
272 if ((s
->rtsr
& RTSR_ALE
) && !(s
->rtsr
& RTSR_AL
)) {
273 qemu_mod_timer(s
->rtc_alarm
, s
->last_hz
+
274 (((s
->rtar
- s
->last_rcnr
) * 1000 *
275 ((s
->rttr
& 0xffff) + 1)) >> 15));
277 qemu_del_timer(s
->rtc_alarm
);
281 static inline void strongarm_rtc_alarm_tick(void *opaque
)
283 StrongARMRTCState
*s
= opaque
;
285 strongarm_rtc_timer_update(s
);
286 strongarm_rtc_int_update(s
);
289 static inline void strongarm_rtc_hz_tick(void *opaque
)
291 StrongARMRTCState
*s
= opaque
;
293 strongarm_rtc_timer_update(s
);
294 strongarm_rtc_int_update(s
);
297 static uint64_t strongarm_rtc_read(void *opaque
, target_phys_addr_t addr
,
300 StrongARMRTCState
*s
= opaque
;
310 return s
->last_rcnr
+
311 ((qemu_get_clock_ms(rtc_clock
) - s
->last_hz
) << 15) /
312 (1000 * ((s
->rttr
& 0xffff) + 1));
314 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
319 static void strongarm_rtc_write(void *opaque
, target_phys_addr_t addr
,
320 uint64_t value
, unsigned size
)
322 StrongARMRTCState
*s
= opaque
;
327 strongarm_rtc_hzupdate(s
);
329 strongarm_rtc_timer_update(s
);
334 s
->rtsr
= (value
& (RTSR_ALE
| RTSR_HZE
)) |
335 (s
->rtsr
& ~(value
& (RTSR_AL
| RTSR_HZ
)));
337 if (s
->rtsr
!= old_rtsr
) {
338 strongarm_rtc_timer_update(s
);
341 strongarm_rtc_int_update(s
);
346 strongarm_rtc_timer_update(s
);
350 strongarm_rtc_hzupdate(s
);
351 s
->last_rcnr
= value
;
352 strongarm_rtc_timer_update(s
);
356 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
360 static const MemoryRegionOps strongarm_rtc_ops
= {
361 .read
= strongarm_rtc_read
,
362 .write
= strongarm_rtc_write
,
363 .endianness
= DEVICE_NATIVE_ENDIAN
,
366 static int strongarm_rtc_init(SysBusDevice
*dev
)
368 StrongARMRTCState
*s
= FROM_SYSBUS(StrongARMRTCState
, dev
);
374 qemu_get_timedate(&tm
, 0);
376 s
->last_rcnr
= (uint32_t) mktimegm(&tm
);
377 s
->last_hz
= qemu_get_clock_ms(rtc_clock
);
379 s
->rtc_alarm
= qemu_new_timer_ms(rtc_clock
, strongarm_rtc_alarm_tick
, s
);
380 s
->rtc_hz
= qemu_new_timer_ms(rtc_clock
, strongarm_rtc_hz_tick
, s
);
382 sysbus_init_irq(dev
, &s
->rtc_irq
);
383 sysbus_init_irq(dev
, &s
->rtc_hz_irq
);
385 memory_region_init_io(&s
->iomem
, &strongarm_rtc_ops
, s
, "rtc", 0x10000);
386 sysbus_init_mmio(dev
, &s
->iomem
);
391 static void strongarm_rtc_pre_save(void *opaque
)
393 StrongARMRTCState
*s
= opaque
;
395 strongarm_rtc_hzupdate(s
);
398 static int strongarm_rtc_post_load(void *opaque
, int version_id
)
400 StrongARMRTCState
*s
= opaque
;
402 strongarm_rtc_timer_update(s
);
403 strongarm_rtc_int_update(s
);
408 static const VMStateDescription vmstate_strongarm_rtc_regs
= {
409 .name
= "strongarm-rtc",
411 .minimum_version_id
= 0,
412 .minimum_version_id_old
= 0,
413 .pre_save
= strongarm_rtc_pre_save
,
414 .post_load
= strongarm_rtc_post_load
,
415 .fields
= (VMStateField
[]) {
416 VMSTATE_UINT32(rttr
, StrongARMRTCState
),
417 VMSTATE_UINT32(rtsr
, StrongARMRTCState
),
418 VMSTATE_UINT32(rtar
, StrongARMRTCState
),
419 VMSTATE_UINT32(last_rcnr
, StrongARMRTCState
),
420 VMSTATE_INT64(last_hz
, StrongARMRTCState
),
421 VMSTATE_END_OF_LIST(),
425 static void strongarm_rtc_sysbus_class_init(ObjectClass
*klass
, void *data
)
427 DeviceClass
*dc
= DEVICE_CLASS(klass
);
428 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
430 k
->init
= strongarm_rtc_init
;
431 dc
->desc
= "StrongARM RTC Controller";
432 dc
->vmsd
= &vmstate_strongarm_rtc_regs
;
435 static TypeInfo strongarm_rtc_sysbus_info
= {
436 .name
= "strongarm-rtc",
437 .parent
= TYPE_SYS_BUS_DEVICE
,
438 .instance_size
= sizeof(StrongARMRTCState
),
439 .class_init
= strongarm_rtc_sysbus_class_init
,
452 typedef struct StrongARMGPIOInfo StrongARMGPIOInfo
;
453 struct StrongARMGPIOInfo
{
456 qemu_irq handler
[28];
473 static void strongarm_gpio_irq_update(StrongARMGPIOInfo
*s
)
476 for (i
= 0; i
< 11; i
++) {
477 qemu_set_irq(s
->irqs
[i
], s
->status
& (1 << i
));
480 qemu_set_irq(s
->irqX
, (s
->status
& ~0x7ff));
483 static void strongarm_gpio_set(void *opaque
, int line
, int level
)
485 StrongARMGPIOInfo
*s
= opaque
;
491 s
->status
|= s
->rising
& mask
&
492 ~s
->ilevel
& ~s
->dir
;
495 s
->status
|= s
->falling
& mask
&
500 if (s
->status
& mask
) {
501 strongarm_gpio_irq_update(s
);
505 static void strongarm_gpio_handler_update(StrongARMGPIOInfo
*s
)
507 uint32_t level
, diff
;
510 level
= s
->olevel
& s
->dir
;
512 for (diff
= s
->prev_level
^ level
; diff
; diff
^= 1 << bit
) {
514 qemu_set_irq(s
->handler
[bit
], (level
>> bit
) & 1);
517 s
->prev_level
= level
;
520 static uint64_t strongarm_gpio_read(void *opaque
, target_phys_addr_t offset
,
523 StrongARMGPIOInfo
*s
= opaque
;
526 case GPDR
: /* GPIO Pin-Direction registers */
529 case GPSR
: /* GPIO Pin-Output Set registers */
530 DPRINTF("%s: Read from a write-only register 0x" TARGET_FMT_plx
"\n",
532 return s
->gpsr
; /* Return last written value. */
534 case GPCR
: /* GPIO Pin-Output Clear registers */
535 DPRINTF("%s: Read from a write-only register 0x" TARGET_FMT_plx
"\n",
537 return 31337; /* Specified as unpredictable in the docs. */
539 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
542 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
545 case GAFR
: /* GPIO Alternate Function registers */
548 case GPLR
: /* GPIO Pin-Level registers */
549 return (s
->olevel
& s
->dir
) |
550 (s
->ilevel
& ~s
->dir
);
552 case GEDR
: /* GPIO Edge Detect Status registers */
556 printf("%s: Bad offset 0x" TARGET_FMT_plx
"\n", __func__
, offset
);
562 static void strongarm_gpio_write(void *opaque
, target_phys_addr_t offset
,
563 uint64_t value
, unsigned size
)
565 StrongARMGPIOInfo
*s
= opaque
;
568 case GPDR
: /* GPIO Pin-Direction registers */
570 strongarm_gpio_handler_update(s
);
573 case GPSR
: /* GPIO Pin-Output Set registers */
575 strongarm_gpio_handler_update(s
);
579 case GPCR
: /* GPIO Pin-Output Clear registers */
581 strongarm_gpio_handler_update(s
);
584 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
588 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
592 case GAFR
: /* GPIO Alternate Function registers */
596 case GEDR
: /* GPIO Edge Detect Status registers */
598 strongarm_gpio_irq_update(s
);
602 printf("%s: Bad offset 0x" TARGET_FMT_plx
"\n", __func__
, offset
);
606 static const MemoryRegionOps strongarm_gpio_ops
= {
607 .read
= strongarm_gpio_read
,
608 .write
= strongarm_gpio_write
,
609 .endianness
= DEVICE_NATIVE_ENDIAN
,
612 static DeviceState
*strongarm_gpio_init(target_phys_addr_t base
,
618 dev
= qdev_create(NULL
, "strongarm-gpio");
619 qdev_init_nofail(dev
);
621 sysbus_mmio_map(sysbus_from_qdev(dev
), 0, base
);
622 for (i
= 0; i
< 12; i
++)
623 sysbus_connect_irq(sysbus_from_qdev(dev
), i
,
624 qdev_get_gpio_in(pic
, SA_PIC_GPIO0_EDGE
+ i
));
629 static int strongarm_gpio_initfn(SysBusDevice
*dev
)
631 StrongARMGPIOInfo
*s
;
634 s
= FROM_SYSBUS(StrongARMGPIOInfo
, dev
);
636 qdev_init_gpio_in(&dev
->qdev
, strongarm_gpio_set
, 28);
637 qdev_init_gpio_out(&dev
->qdev
, s
->handler
, 28);
639 memory_region_init_io(&s
->iomem
, &strongarm_gpio_ops
, s
, "gpio", 0x1000);
641 sysbus_init_mmio(dev
, &s
->iomem
);
642 for (i
= 0; i
< 11; i
++) {
643 sysbus_init_irq(dev
, &s
->irqs
[i
]);
645 sysbus_init_irq(dev
, &s
->irqX
);
650 static const VMStateDescription vmstate_strongarm_gpio_regs
= {
651 .name
= "strongarm-gpio",
653 .minimum_version_id
= 0,
654 .minimum_version_id_old
= 0,
655 .fields
= (VMStateField
[]) {
656 VMSTATE_UINT32(ilevel
, StrongARMGPIOInfo
),
657 VMSTATE_UINT32(olevel
, StrongARMGPIOInfo
),
658 VMSTATE_UINT32(dir
, StrongARMGPIOInfo
),
659 VMSTATE_UINT32(rising
, StrongARMGPIOInfo
),
660 VMSTATE_UINT32(falling
, StrongARMGPIOInfo
),
661 VMSTATE_UINT32(status
, StrongARMGPIOInfo
),
662 VMSTATE_UINT32(gafr
, StrongARMGPIOInfo
),
663 VMSTATE_END_OF_LIST(),
667 static void strongarm_gpio_class_init(ObjectClass
*klass
, void *data
)
669 DeviceClass
*dc
= DEVICE_CLASS(klass
);
670 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
672 k
->init
= strongarm_gpio_initfn
;
673 dc
->desc
= "StrongARM GPIO controller";
676 static TypeInfo strongarm_gpio_info
= {
677 .name
= "strongarm-gpio",
678 .parent
= TYPE_SYS_BUS_DEVICE
,
679 .instance_size
= sizeof(StrongARMGPIOInfo
),
680 .class_init
= strongarm_gpio_class_init
,
683 /* Peripheral Pin Controller */
690 typedef struct StrongARMPPCInfo StrongARMPPCInfo
;
691 struct StrongARMPPCInfo
{
694 qemu_irq handler
[28];
706 static void strongarm_ppc_set(void *opaque
, int line
, int level
)
708 StrongARMPPCInfo
*s
= opaque
;
711 s
->ilevel
|= 1 << line
;
713 s
->ilevel
&= ~(1 << line
);
717 static void strongarm_ppc_handler_update(StrongARMPPCInfo
*s
)
719 uint32_t level
, diff
;
722 level
= s
->olevel
& s
->dir
;
724 for (diff
= s
->prev_level
^ level
; diff
; diff
^= 1 << bit
) {
726 qemu_set_irq(s
->handler
[bit
], (level
>> bit
) & 1);
729 s
->prev_level
= level
;
732 static uint64_t strongarm_ppc_read(void *opaque
, target_phys_addr_t offset
,
735 StrongARMPPCInfo
*s
= opaque
;
738 case PPDR
: /* PPC Pin Direction registers */
739 return s
->dir
| ~0x3fffff;
741 case PPSR
: /* PPC Pin State registers */
742 return (s
->olevel
& s
->dir
) |
743 (s
->ilevel
& ~s
->dir
) |
747 return s
->ppar
| ~0x41000;
753 return s
->ppfr
| ~0x7f001;
756 printf("%s: Bad offset 0x" TARGET_FMT_plx
"\n", __func__
, offset
);
762 static void strongarm_ppc_write(void *opaque
, target_phys_addr_t offset
,
763 uint64_t value
, unsigned size
)
765 StrongARMPPCInfo
*s
= opaque
;
768 case PPDR
: /* PPC Pin Direction registers */
769 s
->dir
= value
& 0x3fffff;
770 strongarm_ppc_handler_update(s
);
773 case PPSR
: /* PPC Pin State registers */
774 s
->olevel
= value
& s
->dir
& 0x3fffff;
775 strongarm_ppc_handler_update(s
);
779 s
->ppar
= value
& 0x41000;
783 s
->psdr
= value
& 0x3fffff;
787 s
->ppfr
= value
& 0x7f001;
791 printf("%s: Bad offset 0x" TARGET_FMT_plx
"\n", __func__
, offset
);
795 static const MemoryRegionOps strongarm_ppc_ops
= {
796 .read
= strongarm_ppc_read
,
797 .write
= strongarm_ppc_write
,
798 .endianness
= DEVICE_NATIVE_ENDIAN
,
801 static int strongarm_ppc_init(SysBusDevice
*dev
)
805 s
= FROM_SYSBUS(StrongARMPPCInfo
, dev
);
807 qdev_init_gpio_in(&dev
->qdev
, strongarm_ppc_set
, 22);
808 qdev_init_gpio_out(&dev
->qdev
, s
->handler
, 22);
810 memory_region_init_io(&s
->iomem
, &strongarm_ppc_ops
, s
, "ppc", 0x1000);
812 sysbus_init_mmio(dev
, &s
->iomem
);
817 static const VMStateDescription vmstate_strongarm_ppc_regs
= {
818 .name
= "strongarm-ppc",
820 .minimum_version_id
= 0,
821 .minimum_version_id_old
= 0,
822 .fields
= (VMStateField
[]) {
823 VMSTATE_UINT32(ilevel
, StrongARMPPCInfo
),
824 VMSTATE_UINT32(olevel
, StrongARMPPCInfo
),
825 VMSTATE_UINT32(dir
, StrongARMPPCInfo
),
826 VMSTATE_UINT32(ppar
, StrongARMPPCInfo
),
827 VMSTATE_UINT32(psdr
, StrongARMPPCInfo
),
828 VMSTATE_UINT32(ppfr
, StrongARMPPCInfo
),
829 VMSTATE_END_OF_LIST(),
833 static void strongarm_ppc_class_init(ObjectClass
*klass
, void *data
)
835 DeviceClass
*dc
= DEVICE_CLASS(klass
);
836 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
838 k
->init
= strongarm_ppc_init
;
839 dc
->desc
= "StrongARM PPC controller";
842 static TypeInfo strongarm_ppc_info
= {
843 .name
= "strongarm-ppc",
844 .parent
= TYPE_SYS_BUS_DEVICE
,
845 .instance_size
= sizeof(StrongARMPPCInfo
),
846 .class_init
= strongarm_ppc_class_init
,
858 #define UTCR0_PE (1 << 0) /* Parity enable */
859 #define UTCR0_OES (1 << 1) /* Even parity */
860 #define UTCR0_SBS (1 << 2) /* 2 stop bits */
861 #define UTCR0_DSS (1 << 3) /* 8-bit data */
863 #define UTCR3_RXE (1 << 0) /* Rx enable */
864 #define UTCR3_TXE (1 << 1) /* Tx enable */
865 #define UTCR3_BRK (1 << 2) /* Force Break */
866 #define UTCR3_RIE (1 << 3) /* Rx int enable */
867 #define UTCR3_TIE (1 << 4) /* Tx int enable */
868 #define UTCR3_LBM (1 << 5) /* Loopback */
870 #define UTSR0_TFS (1 << 0) /* Tx FIFO nearly empty */
871 #define UTSR0_RFS (1 << 1) /* Rx FIFO nearly full */
872 #define UTSR0_RID (1 << 2) /* Receiver Idle */
873 #define UTSR0_RBB (1 << 3) /* Receiver begin break */
874 #define UTSR0_REB (1 << 4) /* Receiver end break */
875 #define UTSR0_EIF (1 << 5) /* Error in FIFO */
877 #define UTSR1_RNE (1 << 1) /* Receive FIFO not empty */
878 #define UTSR1_TNF (1 << 2) /* Transmit FIFO not full */
879 #define UTSR1_PRE (1 << 3) /* Parity error */
880 #define UTSR1_FRE (1 << 4) /* Frame error */
881 #define UTSR1_ROR (1 << 5) /* Receive Over Run */
883 #define RX_FIFO_PRE (1 << 8)
884 #define RX_FIFO_FRE (1 << 9)
885 #define RX_FIFO_ROR (1 << 10)
890 CharDriverState
*chr
;
902 uint16_t rx_fifo
[12]; /* value + error flags in high bits */
906 uint64_t char_transmit_time
; /* time to transmit a char in ticks*/
908 QEMUTimer
*rx_timeout_timer
;
910 } StrongARMUARTState
;
912 static void strongarm_uart_update_status(StrongARMUARTState
*s
)
916 if (s
->tx_len
!= 8) {
920 if (s
->rx_len
!= 0) {
921 uint16_t ent
= s
->rx_fifo
[s
->rx_start
];
924 if (ent
& RX_FIFO_PRE
) {
925 s
->utsr1
|= UTSR1_PRE
;
927 if (ent
& RX_FIFO_FRE
) {
928 s
->utsr1
|= UTSR1_FRE
;
930 if (ent
& RX_FIFO_ROR
) {
931 s
->utsr1
|= UTSR1_ROR
;
938 static void strongarm_uart_update_int_status(StrongARMUARTState
*s
)
940 uint16_t utsr0
= s
->utsr0
&
941 (UTSR0_REB
| UTSR0_RBB
| UTSR0_RID
);
944 if ((s
->utcr3
& UTCR3_TXE
) &&
945 (s
->utcr3
& UTCR3_TIE
) &&
950 if ((s
->utcr3
& UTCR3_RXE
) &&
951 (s
->utcr3
& UTCR3_RIE
) &&
956 for (i
= 0; i
< s
->rx_len
&& i
< 4; i
++)
957 if (s
->rx_fifo
[(s
->rx_start
+ i
) % 12] & ~0xff) {
963 qemu_set_irq(s
->irq
, utsr0
);
966 static void strongarm_uart_update_parameters(StrongARMUARTState
*s
)
968 int speed
, parity
, data_bits
, stop_bits
, frame_size
;
969 QEMUSerialSetParams ssp
;
973 if (s
->utcr0
& UTCR0_PE
) {
976 if (s
->utcr0
& UTCR0_OES
) {
984 if (s
->utcr0
& UTCR0_SBS
) {
990 data_bits
= (s
->utcr0
& UTCR0_DSS
) ? 8 : 7;
991 frame_size
+= data_bits
+ stop_bits
;
992 speed
= 3686400 / 16 / (s
->brd
+ 1);
995 ssp
.data_bits
= data_bits
;
996 ssp
.stop_bits
= stop_bits
;
997 s
->char_transmit_time
= (get_ticks_per_sec() / speed
) * frame_size
;
999 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
1002 DPRINTF(stderr
, "%s speed=%d parity=%c data=%d stop=%d\n", s
->chr
->label
,
1003 speed
, parity
, data_bits
, stop_bits
);
1006 static void strongarm_uart_rx_to(void *opaque
)
1008 StrongARMUARTState
*s
= opaque
;
1011 s
->utsr0
|= UTSR0_RID
;
1012 strongarm_uart_update_int_status(s
);
1016 static void strongarm_uart_rx_push(StrongARMUARTState
*s
, uint16_t c
)
1018 if ((s
->utcr3
& UTCR3_RXE
) == 0) {
1023 if (s
->wait_break_end
) {
1024 s
->utsr0
|= UTSR0_REB
;
1025 s
->wait_break_end
= false;
1028 if (s
->rx_len
< 12) {
1029 s
->rx_fifo
[(s
->rx_start
+ s
->rx_len
) % 12] = c
;
1032 s
->rx_fifo
[(s
->rx_start
+ 11) % 12] |= RX_FIFO_ROR
;
1035 static int strongarm_uart_can_receive(void *opaque
)
1037 StrongARMUARTState
*s
= opaque
;
1039 if (s
->rx_len
== 12) {
1042 /* It's best not to get more than 2/3 of RX FIFO, so advertise that much */
1043 if (s
->rx_len
< 8) {
1044 return 8 - s
->rx_len
;
1049 static void strongarm_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
1051 StrongARMUARTState
*s
= opaque
;
1054 for (i
= 0; i
< size
; i
++) {
1055 strongarm_uart_rx_push(s
, buf
[i
]);
1058 /* call the timeout receive callback in 3 char transmit time */
1059 qemu_mod_timer(s
->rx_timeout_timer
,
1060 qemu_get_clock_ns(vm_clock
) + s
->char_transmit_time
* 3);
1062 strongarm_uart_update_status(s
);
1063 strongarm_uart_update_int_status(s
);
1066 static void strongarm_uart_event(void *opaque
, int event
)
1068 StrongARMUARTState
*s
= opaque
;
1069 if (event
== CHR_EVENT_BREAK
) {
1070 s
->utsr0
|= UTSR0_RBB
;
1071 strongarm_uart_rx_push(s
, RX_FIFO_FRE
);
1072 s
->wait_break_end
= true;
1073 strongarm_uart_update_status(s
);
1074 strongarm_uart_update_int_status(s
);
1078 static void strongarm_uart_tx(void *opaque
)
1080 StrongARMUARTState
*s
= opaque
;
1081 uint64_t new_xmit_ts
= qemu_get_clock_ns(vm_clock
);
1083 if (s
->utcr3
& UTCR3_LBM
) /* loopback */ {
1084 strongarm_uart_receive(s
, &s
->tx_fifo
[s
->tx_start
], 1);
1085 } else if (s
->chr
) {
1086 qemu_chr_fe_write(s
->chr
, &s
->tx_fifo
[s
->tx_start
], 1);
1089 s
->tx_start
= (s
->tx_start
+ 1) % 8;
1092 qemu_mod_timer(s
->tx_timer
, new_xmit_ts
+ s
->char_transmit_time
);
1094 strongarm_uart_update_status(s
);
1095 strongarm_uart_update_int_status(s
);
1098 static uint64_t strongarm_uart_read(void *opaque
, target_phys_addr_t addr
,
1101 StrongARMUARTState
*s
= opaque
;
1112 return s
->brd
& 0xff;
1118 if (s
->rx_len
!= 0) {
1119 ret
= s
->rx_fifo
[s
->rx_start
];
1120 s
->rx_start
= (s
->rx_start
+ 1) % 12;
1122 strongarm_uart_update_status(s
);
1123 strongarm_uart_update_int_status(s
);
1135 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1140 static void strongarm_uart_write(void *opaque
, target_phys_addr_t addr
,
1141 uint64_t value
, unsigned size
)
1143 StrongARMUARTState
*s
= opaque
;
1147 s
->utcr0
= value
& 0x7f;
1148 strongarm_uart_update_parameters(s
);
1152 s
->brd
= (s
->brd
& 0xff) | ((value
& 0xf) << 8);
1153 strongarm_uart_update_parameters(s
);
1157 s
->brd
= (s
->brd
& 0xf00) | (value
& 0xff);
1158 strongarm_uart_update_parameters(s
);
1162 s
->utcr3
= value
& 0x3f;
1163 if ((s
->utcr3
& UTCR3_RXE
) == 0) {
1166 if ((s
->utcr3
& UTCR3_TXE
) == 0) {
1169 strongarm_uart_update_status(s
);
1170 strongarm_uart_update_int_status(s
);
1174 if ((s
->utcr3
& UTCR3_TXE
) && s
->tx_len
!= 8) {
1175 s
->tx_fifo
[(s
->tx_start
+ s
->tx_len
) % 8] = value
;
1177 strongarm_uart_update_status(s
);
1178 strongarm_uart_update_int_status(s
);
1179 if (s
->tx_len
== 1) {
1180 strongarm_uart_tx(s
);
1186 s
->utsr0
= s
->utsr0
& ~(value
&
1187 (UTSR0_REB
| UTSR0_RBB
| UTSR0_RID
));
1188 strongarm_uart_update_int_status(s
);
1192 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1196 static const MemoryRegionOps strongarm_uart_ops
= {
1197 .read
= strongarm_uart_read
,
1198 .write
= strongarm_uart_write
,
1199 .endianness
= DEVICE_NATIVE_ENDIAN
,
1202 static int strongarm_uart_init(SysBusDevice
*dev
)
1204 StrongARMUARTState
*s
= FROM_SYSBUS(StrongARMUARTState
, dev
);
1206 memory_region_init_io(&s
->iomem
, &strongarm_uart_ops
, s
, "uart", 0x10000);
1207 sysbus_init_mmio(dev
, &s
->iomem
);
1208 sysbus_init_irq(dev
, &s
->irq
);
1210 s
->rx_timeout_timer
= qemu_new_timer_ns(vm_clock
, strongarm_uart_rx_to
, s
);
1211 s
->tx_timer
= qemu_new_timer_ns(vm_clock
, strongarm_uart_tx
, s
);
1214 qemu_chr_add_handlers(s
->chr
,
1215 strongarm_uart_can_receive
,
1216 strongarm_uart_receive
,
1217 strongarm_uart_event
,
1224 static void strongarm_uart_reset(DeviceState
*dev
)
1226 StrongARMUARTState
*s
= DO_UPCAST(StrongARMUARTState
, busdev
.qdev
, dev
);
1228 s
->utcr0
= UTCR0_DSS
; /* 8 data, no parity */
1229 s
->brd
= 23; /* 9600 */
1230 /* enable send & recv - this actually violates spec */
1231 s
->utcr3
= UTCR3_TXE
| UTCR3_RXE
;
1233 s
->rx_len
= s
->tx_len
= 0;
1235 strongarm_uart_update_parameters(s
);
1236 strongarm_uart_update_status(s
);
1237 strongarm_uart_update_int_status(s
);
1240 static int strongarm_uart_post_load(void *opaque
, int version_id
)
1242 StrongARMUARTState
*s
= opaque
;
1244 strongarm_uart_update_parameters(s
);
1245 strongarm_uart_update_status(s
);
1246 strongarm_uart_update_int_status(s
);
1248 /* tx and restart timer */
1250 strongarm_uart_tx(s
);
1253 /* restart rx timeout timer */
1255 qemu_mod_timer(s
->rx_timeout_timer
,
1256 qemu_get_clock_ns(vm_clock
) + s
->char_transmit_time
* 3);
1262 static const VMStateDescription vmstate_strongarm_uart_regs
= {
1263 .name
= "strongarm-uart",
1265 .minimum_version_id
= 0,
1266 .minimum_version_id_old
= 0,
1267 .post_load
= strongarm_uart_post_load
,
1268 .fields
= (VMStateField
[]) {
1269 VMSTATE_UINT8(utcr0
, StrongARMUARTState
),
1270 VMSTATE_UINT16(brd
, StrongARMUARTState
),
1271 VMSTATE_UINT8(utcr3
, StrongARMUARTState
),
1272 VMSTATE_UINT8(utsr0
, StrongARMUARTState
),
1273 VMSTATE_UINT8_ARRAY(tx_fifo
, StrongARMUARTState
, 8),
1274 VMSTATE_UINT8(tx_start
, StrongARMUARTState
),
1275 VMSTATE_UINT8(tx_len
, StrongARMUARTState
),
1276 VMSTATE_UINT16_ARRAY(rx_fifo
, StrongARMUARTState
, 12),
1277 VMSTATE_UINT8(rx_start
, StrongARMUARTState
),
1278 VMSTATE_UINT8(rx_len
, StrongARMUARTState
),
1279 VMSTATE_BOOL(wait_break_end
, StrongARMUARTState
),
1280 VMSTATE_END_OF_LIST(),
1284 static Property strongarm_uart_properties
[] = {
1285 DEFINE_PROP_CHR("chardev", StrongARMUARTState
, chr
),
1286 DEFINE_PROP_END_OF_LIST(),
1289 static void strongarm_uart_class_init(ObjectClass
*klass
, void *data
)
1291 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1292 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
1294 k
->init
= strongarm_uart_init
;
1295 dc
->desc
= "StrongARM UART controller";
1296 dc
->reset
= strongarm_uart_reset
;
1297 dc
->vmsd
= &vmstate_strongarm_uart_regs
;
1298 dc
->props
= strongarm_uart_properties
;
1301 static TypeInfo strongarm_uart_info
= {
1302 .name
= "strongarm-uart",
1303 .parent
= TYPE_SYS_BUS_DEVICE
,
1304 .instance_size
= sizeof(StrongARMUARTState
),
1305 .class_init
= strongarm_uart_class_init
,
1308 /* Synchronous Serial Ports */
1310 SysBusDevice busdev
;
1318 uint16_t rx_fifo
[8];
1321 } StrongARMSSPState
;
1323 #define SSCR0 0x60 /* SSP Control register 0 */
1324 #define SSCR1 0x64 /* SSP Control register 1 */
1325 #define SSDR 0x6c /* SSP Data register */
1326 #define SSSR 0x74 /* SSP Status register */
1328 /* Bitfields for above registers */
1329 #define SSCR0_SPI(x) (((x) & 0x30) == 0x00)
1330 #define SSCR0_SSP(x) (((x) & 0x30) == 0x10)
1331 #define SSCR0_UWIRE(x) (((x) & 0x30) == 0x20)
1332 #define SSCR0_PSP(x) (((x) & 0x30) == 0x30)
1333 #define SSCR0_SSE (1 << 7)
1334 #define SSCR0_DSS(x) (((x) & 0xf) + 1)
1335 #define SSCR1_RIE (1 << 0)
1336 #define SSCR1_TIE (1 << 1)
1337 #define SSCR1_LBM (1 << 2)
1338 #define SSSR_TNF (1 << 2)
1339 #define SSSR_RNE (1 << 3)
1340 #define SSSR_TFS (1 << 5)
1341 #define SSSR_RFS (1 << 6)
1342 #define SSSR_ROR (1 << 7)
1343 #define SSSR_RW 0x0080
1345 static void strongarm_ssp_int_update(StrongARMSSPState
*s
)
1349 level
|= (s
->sssr
& SSSR_ROR
);
1350 level
|= (s
->sssr
& SSSR_RFS
) && (s
->sscr
[1] & SSCR1_RIE
);
1351 level
|= (s
->sssr
& SSSR_TFS
) && (s
->sscr
[1] & SSCR1_TIE
);
1352 qemu_set_irq(s
->irq
, level
);
1355 static void strongarm_ssp_fifo_update(StrongARMSSPState
*s
)
1357 s
->sssr
&= ~SSSR_TFS
;
1358 s
->sssr
&= ~SSSR_TNF
;
1359 if (s
->sscr
[0] & SSCR0_SSE
) {
1360 if (s
->rx_level
>= 4) {
1361 s
->sssr
|= SSSR_RFS
;
1363 s
->sssr
&= ~SSSR_RFS
;
1366 s
->sssr
|= SSSR_RNE
;
1368 s
->sssr
&= ~SSSR_RNE
;
1370 /* TX FIFO is never filled, so it is always in underrun
1371 condition if SSP is enabled */
1372 s
->sssr
|= SSSR_TFS
;
1373 s
->sssr
|= SSSR_TNF
;
1376 strongarm_ssp_int_update(s
);
1379 static uint64_t strongarm_ssp_read(void *opaque
, target_phys_addr_t addr
,
1382 StrongARMSSPState
*s
= opaque
;
1393 if (~s
->sscr
[0] & SSCR0_SSE
) {
1396 if (s
->rx_level
< 1) {
1397 printf("%s: SSP Rx Underrun\n", __func__
);
1401 retval
= s
->rx_fifo
[s
->rx_start
++];
1403 strongarm_ssp_fifo_update(s
);
1406 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1412 static void strongarm_ssp_write(void *opaque
, target_phys_addr_t addr
,
1413 uint64_t value
, unsigned size
)
1415 StrongARMSSPState
*s
= opaque
;
1419 s
->sscr
[0] = value
& 0xffbf;
1420 if ((s
->sscr
[0] & SSCR0_SSE
) && SSCR0_DSS(value
) < 4) {
1421 printf("%s: Wrong data size: %i bits\n", __func__
,
1422 (int)SSCR0_DSS(value
));
1424 if (!(value
& SSCR0_SSE
)) {
1428 strongarm_ssp_fifo_update(s
);
1432 s
->sscr
[1] = value
& 0x2f;
1433 if (value
& SSCR1_LBM
) {
1434 printf("%s: Attempt to use SSP LBM mode\n", __func__
);
1436 strongarm_ssp_fifo_update(s
);
1440 s
->sssr
&= ~(value
& SSSR_RW
);
1441 strongarm_ssp_int_update(s
);
1445 if (SSCR0_UWIRE(s
->sscr
[0])) {
1448 /* Note how 32bits overflow does no harm here */
1449 value
&= (1 << SSCR0_DSS(s
->sscr
[0])) - 1;
1451 /* Data goes from here to the Tx FIFO and is shifted out from
1452 * there directly to the slave, no need to buffer it.
1454 if (s
->sscr
[0] & SSCR0_SSE
) {
1456 if (s
->sscr
[1] & SSCR1_LBM
) {
1459 readval
= ssi_transfer(s
->bus
, value
);
1462 if (s
->rx_level
< 0x08) {
1463 s
->rx_fifo
[(s
->rx_start
+ s
->rx_level
++) & 0x7] = readval
;
1465 s
->sssr
|= SSSR_ROR
;
1468 strongarm_ssp_fifo_update(s
);
1472 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1477 static const MemoryRegionOps strongarm_ssp_ops
= {
1478 .read
= strongarm_ssp_read
,
1479 .write
= strongarm_ssp_write
,
1480 .endianness
= DEVICE_NATIVE_ENDIAN
,
1483 static int strongarm_ssp_post_load(void *opaque
, int version_id
)
1485 StrongARMSSPState
*s
= opaque
;
1487 strongarm_ssp_fifo_update(s
);
1492 static int strongarm_ssp_init(SysBusDevice
*dev
)
1494 StrongARMSSPState
*s
= FROM_SYSBUS(StrongARMSSPState
, dev
);
1496 sysbus_init_irq(dev
, &s
->irq
);
1498 memory_region_init_io(&s
->iomem
, &strongarm_ssp_ops
, s
, "ssp", 0x1000);
1499 sysbus_init_mmio(dev
, &s
->iomem
);
1501 s
->bus
= ssi_create_bus(&dev
->qdev
, "ssi");
1505 static void strongarm_ssp_reset(DeviceState
*dev
)
1507 StrongARMSSPState
*s
= DO_UPCAST(StrongARMSSPState
, busdev
.qdev
, dev
);
1508 s
->sssr
= 0x03; /* 3 bit data, SPI, disabled */
1513 static const VMStateDescription vmstate_strongarm_ssp_regs
= {
1514 .name
= "strongarm-ssp",
1516 .minimum_version_id
= 0,
1517 .minimum_version_id_old
= 0,
1518 .post_load
= strongarm_ssp_post_load
,
1519 .fields
= (VMStateField
[]) {
1520 VMSTATE_UINT16_ARRAY(sscr
, StrongARMSSPState
, 2),
1521 VMSTATE_UINT16(sssr
, StrongARMSSPState
),
1522 VMSTATE_UINT16_ARRAY(rx_fifo
, StrongARMSSPState
, 8),
1523 VMSTATE_UINT8(rx_start
, StrongARMSSPState
),
1524 VMSTATE_UINT8(rx_level
, StrongARMSSPState
),
1525 VMSTATE_END_OF_LIST(),
1529 static void strongarm_ssp_class_init(ObjectClass
*klass
, void *data
)
1531 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1532 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
1534 k
->init
= strongarm_ssp_init
;
1535 dc
->desc
= "StrongARM SSP controller";
1536 dc
->reset
= strongarm_ssp_reset
;
1537 dc
->vmsd
= &vmstate_strongarm_ssp_regs
;
1540 static TypeInfo strongarm_ssp_info
= {
1541 .name
= "strongarm-ssp",
1542 .parent
= TYPE_SYS_BUS_DEVICE
,
1543 .instance_size
= sizeof(StrongARMSSPState
),
1544 .class_init
= strongarm_ssp_class_init
,
1547 /* Main CPU functions */
1548 StrongARMState
*sa1110_init(MemoryRegion
*sysmem
,
1549 unsigned int sdram_size
, const char *rev
)
1555 s
= g_malloc0(sizeof(StrongARMState
));
1561 if (strncmp(rev
, "sa1110", 6)) {
1562 error_report("Machine requires a SA1110 processor.");
1566 s
->env
= cpu_init(rev
);
1569 error_report("Unable to find CPU definition");
1573 memory_region_init_ram(&s
->sdram
, "strongarm.sdram", sdram_size
);
1574 vmstate_register_ram_global(&s
->sdram
);
1575 memory_region_add_subregion(sysmem
, SA_SDCS0
, &s
->sdram
);
1577 pic
= arm_pic_init_cpu(s
->env
);
1578 s
->pic
= sysbus_create_varargs("strongarm_pic", 0x90050000,
1579 pic
[ARM_PIC_CPU_IRQ
], pic
[ARM_PIC_CPU_FIQ
], NULL
);
1581 sysbus_create_varargs("pxa25x-timer", 0x90000000,
1582 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC0
),
1583 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC1
),
1584 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC2
),
1585 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC3
),
1588 sysbus_create_simple("strongarm-rtc", 0x90010000,
1589 qdev_get_gpio_in(s
->pic
, SA_PIC_RTC_ALARM
));
1591 s
->gpio
= strongarm_gpio_init(0x90040000, s
->pic
);
1593 s
->ppc
= sysbus_create_varargs("strongarm-ppc", 0x90060000, NULL
);
1595 for (i
= 0; sa_serial
[i
].io_base
; i
++) {
1596 DeviceState
*dev
= qdev_create(NULL
, "strongarm-uart");
1597 qdev_prop_set_chr(dev
, "chardev", serial_hds
[i
]);
1598 qdev_init_nofail(dev
);
1599 sysbus_mmio_map(sysbus_from_qdev(dev
), 0,
1600 sa_serial
[i
].io_base
);
1601 sysbus_connect_irq(sysbus_from_qdev(dev
), 0,
1602 qdev_get_gpio_in(s
->pic
, sa_serial
[i
].irq
));
1605 s
->ssp
= sysbus_create_varargs("strongarm-ssp", 0x80070000,
1606 qdev_get_gpio_in(s
->pic
, SA_PIC_SSP
), NULL
);
1607 s
->ssp_bus
= (SSIBus
*)qdev_get_child_bus(s
->ssp
, "ssi");
1612 static void strongarm_register_types(void)
1614 type_register_static(&strongarm_pic_info
);
1615 type_register_static(&strongarm_rtc_sysbus_info
);
1616 type_register_static(&strongarm_gpio_info
);
1617 type_register_static(&strongarm_ppc_info
);
1618 type_register_static(&strongarm_uart_info
);
1619 type_register_static(&strongarm_ssp_info
);
1622 type_init(strongarm_register_types
)