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/>.
27 #include "strongarm.h"
28 #include "qemu-error.h"
37 - Implement cp15, c14 ?
38 - Implement cp15, c15 !!! (idle used in L)
39 - Implement idle mode handling/DIM
40 - Implement sleep mode/Wake sources
41 - Implement reset control
42 - Implement memory control regs
44 - Maybe support MBGNT/MBREQ
49 - Enhance UART with modem signals
53 # define DPRINTF(format, ...) printf(format , ## __VA_ARGS__)
55 # define DPRINTF(format, ...) do { } while (0)
59 target_phys_addr_t io_base
;
62 { 0x80010000, SA_PIC_UART1
},
63 { 0x80030000, SA_PIC_UART2
},
64 { 0x80050000, SA_PIC_UART3
},
68 /* Interrupt Controller */
87 #define SA_PIC_SRCS 32
90 static void strongarm_pic_update(void *opaque
)
92 StrongARMPICState
*s
= opaque
;
94 /* FIXME: reflect DIM */
95 qemu_set_irq(s
->fiq
, s
->pending
& s
->enabled
& s
->is_fiq
);
96 qemu_set_irq(s
->irq
, s
->pending
& s
->enabled
& ~s
->is_fiq
);
99 static void strongarm_pic_set_irq(void *opaque
, int irq
, int level
)
101 StrongARMPICState
*s
= opaque
;
104 s
->pending
|= 1 << irq
;
106 s
->pending
&= ~(1 << irq
);
109 strongarm_pic_update(s
);
112 static uint32_t strongarm_pic_mem_read(void *opaque
, target_phys_addr_t offset
)
114 StrongARMPICState
*s
= opaque
;
118 return s
->pending
& ~s
->is_fiq
& s
->enabled
;
124 return s
->int_idle
== 0;
126 return s
->pending
& s
->is_fiq
& s
->enabled
;
130 printf("%s: Bad register offset 0x" TARGET_FMT_plx
"\n",
136 static void strongarm_pic_mem_write(void *opaque
, target_phys_addr_t offset
,
139 StrongARMPICState
*s
= opaque
;
149 s
->int_idle
= (value
& 1) ? 0 : ~0;
152 printf("%s: Bad register offset 0x" TARGET_FMT_plx
"\n",
156 strongarm_pic_update(s
);
159 static CPUReadMemoryFunc
* const strongarm_pic_readfn
[] = {
160 strongarm_pic_mem_read
,
161 strongarm_pic_mem_read
,
162 strongarm_pic_mem_read
,
165 static CPUWriteMemoryFunc
* const strongarm_pic_writefn
[] = {
166 strongarm_pic_mem_write
,
167 strongarm_pic_mem_write
,
168 strongarm_pic_mem_write
,
171 static int strongarm_pic_initfn(SysBusDevice
*dev
)
173 StrongARMPICState
*s
= FROM_SYSBUS(StrongARMPICState
, dev
);
176 qdev_init_gpio_in(&dev
->qdev
, strongarm_pic_set_irq
, SA_PIC_SRCS
);
177 iomemtype
= cpu_register_io_memory(strongarm_pic_readfn
,
178 strongarm_pic_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
179 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
180 sysbus_init_irq(dev
, &s
->irq
);
181 sysbus_init_irq(dev
, &s
->fiq
);
186 static int strongarm_pic_post_load(void *opaque
, int version_id
)
188 strongarm_pic_update(opaque
);
192 static VMStateDescription vmstate_strongarm_pic_regs
= {
193 .name
= "strongarm_pic",
195 .minimum_version_id
= 0,
196 .minimum_version_id_old
= 0,
197 .post_load
= strongarm_pic_post_load
,
198 .fields
= (VMStateField
[]) {
199 VMSTATE_UINT32(pending
, StrongARMPICState
),
200 VMSTATE_UINT32(enabled
, StrongARMPICState
),
201 VMSTATE_UINT32(is_fiq
, StrongARMPICState
),
202 VMSTATE_UINT32(int_idle
, StrongARMPICState
),
203 VMSTATE_END_OF_LIST(),
207 static SysBusDeviceInfo strongarm_pic_info
= {
208 .init
= strongarm_pic_initfn
,
209 .qdev
.name
= "strongarm_pic",
210 .qdev
.desc
= "StrongARM PIC",
211 .qdev
.size
= sizeof(StrongARMPICState
),
212 .qdev
.vmsd
= &vmstate_strongarm_pic_regs
,
215 /* Real-Time Clock */
216 #define RTAR 0x00 /* RTC Alarm register */
217 #define RCNR 0x04 /* RTC Counter register */
218 #define RTTR 0x08 /* RTC Timer Trim register */
219 #define RTSR 0x10 /* RTC Status register */
221 #define RTSR_AL (1 << 0) /* RTC Alarm detected */
222 #define RTSR_HZ (1 << 1) /* RTC 1Hz detected */
223 #define RTSR_ALE (1 << 2) /* RTC Alarm enable */
224 #define RTSR_HZE (1 << 3) /* RTC 1Hz enable */
226 /* 16 LSB of RTTR are clockdiv for internal trim logic,
227 * trim delete isn't emulated, so
228 * f = 32 768 / (RTTR_trim + 1) */
237 QEMUTimer
*rtc_alarm
;
243 static inline void strongarm_rtc_int_update(StrongARMRTCState
*s
)
245 qemu_set_irq(s
->rtc_irq
, s
->rtsr
& RTSR_AL
);
246 qemu_set_irq(s
->rtc_hz_irq
, s
->rtsr
& RTSR_HZ
);
249 static void strongarm_rtc_hzupdate(StrongARMRTCState
*s
)
251 int64_t rt
= qemu_get_clock_ms(rt_clock
);
252 s
->last_rcnr
+= ((rt
- s
->last_hz
) << 15) /
253 (1000 * ((s
->rttr
& 0xffff) + 1));
257 static inline void strongarm_rtc_timer_update(StrongARMRTCState
*s
)
259 if ((s
->rtsr
& RTSR_HZE
) && !(s
->rtsr
& RTSR_HZ
)) {
260 qemu_mod_timer(s
->rtc_hz
, s
->last_hz
+ 1000);
262 qemu_del_timer(s
->rtc_hz
);
265 if ((s
->rtsr
& RTSR_ALE
) && !(s
->rtsr
& RTSR_AL
)) {
266 qemu_mod_timer(s
->rtc_alarm
, s
->last_hz
+
267 (((s
->rtar
- s
->last_rcnr
) * 1000 *
268 ((s
->rttr
& 0xffff) + 1)) >> 15));
270 qemu_del_timer(s
->rtc_alarm
);
274 static inline void strongarm_rtc_alarm_tick(void *opaque
)
276 StrongARMRTCState
*s
= opaque
;
278 strongarm_rtc_timer_update(s
);
279 strongarm_rtc_int_update(s
);
282 static inline void strongarm_rtc_hz_tick(void *opaque
)
284 StrongARMRTCState
*s
= opaque
;
286 strongarm_rtc_timer_update(s
);
287 strongarm_rtc_int_update(s
);
290 static uint32_t strongarm_rtc_read(void *opaque
, target_phys_addr_t addr
)
292 StrongARMRTCState
*s
= opaque
;
302 return s
->last_rcnr
+
303 ((qemu_get_clock_ms(rt_clock
) - s
->last_hz
) << 15) /
304 (1000 * ((s
->rttr
& 0xffff) + 1));
306 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
311 static void strongarm_rtc_write(void *opaque
, target_phys_addr_t addr
,
314 StrongARMRTCState
*s
= opaque
;
319 strongarm_rtc_hzupdate(s
);
321 strongarm_rtc_timer_update(s
);
326 s
->rtsr
= (value
& (RTSR_ALE
| RTSR_HZE
)) |
327 (s
->rtsr
& ~(value
& (RTSR_AL
| RTSR_HZ
)));
329 if (s
->rtsr
!= old_rtsr
) {
330 strongarm_rtc_timer_update(s
);
333 strongarm_rtc_int_update(s
);
338 strongarm_rtc_timer_update(s
);
342 strongarm_rtc_hzupdate(s
);
343 s
->last_rcnr
= value
;
344 strongarm_rtc_timer_update(s
);
348 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
352 static CPUReadMemoryFunc
* const strongarm_rtc_readfn
[] = {
358 static CPUWriteMemoryFunc
* const strongarm_rtc_writefn
[] = {
364 static int strongarm_rtc_init(SysBusDevice
*dev
)
366 StrongARMRTCState
*s
= FROM_SYSBUS(StrongARMRTCState
, dev
);
373 qemu_get_timedate(&tm
, 0);
375 s
->last_rcnr
= (uint32_t) mktimegm(&tm
);
376 s
->last_hz
= qemu_get_clock_ms(rt_clock
);
378 s
->rtc_alarm
= qemu_new_timer_ms(rt_clock
, strongarm_rtc_alarm_tick
, s
);
379 s
->rtc_hz
= qemu_new_timer_ms(rt_clock
, strongarm_rtc_hz_tick
, s
);
381 sysbus_init_irq(dev
, &s
->rtc_irq
);
382 sysbus_init_irq(dev
, &s
->rtc_hz_irq
);
384 iomemtype
= cpu_register_io_memory(strongarm_rtc_readfn
,
385 strongarm_rtc_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
386 sysbus_init_mmio(dev
, 0x10000, iomemtype
);
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 SysBusDeviceInfo strongarm_rtc_sysbus_info
= {
426 .init
= strongarm_rtc_init
,
427 .qdev
.name
= "strongarm-rtc",
428 .qdev
.desc
= "StrongARM RTC Controller",
429 .qdev
.size
= sizeof(StrongARMRTCState
),
430 .qdev
.vmsd
= &vmstate_strongarm_rtc_regs
,
443 typedef struct StrongARMGPIOInfo StrongARMGPIOInfo
;
444 struct StrongARMGPIOInfo
{
446 qemu_irq handler
[28];
463 static void strongarm_gpio_irq_update(StrongARMGPIOInfo
*s
)
466 for (i
= 0; i
< 11; i
++) {
467 qemu_set_irq(s
->irqs
[i
], s
->status
& (1 << i
));
470 qemu_set_irq(s
->irqX
, (s
->status
& ~0x7ff));
473 static void strongarm_gpio_set(void *opaque
, int line
, int level
)
475 StrongARMGPIOInfo
*s
= opaque
;
481 s
->status
|= s
->rising
& mask
&
482 ~s
->ilevel
& ~s
->dir
;
485 s
->status
|= s
->falling
& mask
&
490 if (s
->status
& mask
) {
491 strongarm_gpio_irq_update(s
);
495 static void strongarm_gpio_handler_update(StrongARMGPIOInfo
*s
)
497 uint32_t level
, diff
;
500 level
= s
->olevel
& s
->dir
;
502 for (diff
= s
->prev_level
^ level
; diff
; diff
^= 1 << bit
) {
504 qemu_set_irq(s
->handler
[bit
], (level
>> bit
) & 1);
507 s
->prev_level
= level
;
510 static uint32_t strongarm_gpio_read(void *opaque
, target_phys_addr_t offset
)
512 StrongARMGPIOInfo
*s
= opaque
;
515 case GPDR
: /* GPIO Pin-Direction registers */
518 case GPSR
: /* GPIO Pin-Output Set registers */
519 DPRINTF("%s: Read from a write-only register 0x" TARGET_FMT_plx
"\n",
521 return s
->gpsr
; /* Return last written value. */
523 case GPCR
: /* GPIO Pin-Output Clear registers */
524 DPRINTF("%s: Read from a write-only register 0x" TARGET_FMT_plx
"\n",
526 return 31337; /* Specified as unpredictable in the docs. */
528 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
531 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
534 case GAFR
: /* GPIO Alternate Function registers */
537 case GPLR
: /* GPIO Pin-Level registers */
538 return (s
->olevel
& s
->dir
) |
539 (s
->ilevel
& ~s
->dir
);
541 case GEDR
: /* GPIO Edge Detect Status registers */
545 printf("%s: Bad offset 0x" TARGET_FMT_plx
"\n", __func__
, offset
);
551 static void strongarm_gpio_write(void *opaque
,
552 target_phys_addr_t offset
, uint32_t value
)
554 StrongARMGPIOInfo
*s
= opaque
;
557 case GPDR
: /* GPIO Pin-Direction registers */
559 strongarm_gpio_handler_update(s
);
562 case GPSR
: /* GPIO Pin-Output Set registers */
564 strongarm_gpio_handler_update(s
);
568 case GPCR
: /* GPIO Pin-Output Clear registers */
570 strongarm_gpio_handler_update(s
);
573 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
577 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
581 case GAFR
: /* GPIO Alternate Function registers */
585 case GEDR
: /* GPIO Edge Detect Status registers */
587 strongarm_gpio_irq_update(s
);
591 printf("%s: Bad offset 0x" TARGET_FMT_plx
"\n", __func__
, offset
);
595 static CPUReadMemoryFunc
* const strongarm_gpio_readfn
[] = {
601 static CPUWriteMemoryFunc
* const strongarm_gpio_writefn
[] = {
602 strongarm_gpio_write
,
603 strongarm_gpio_write
,
607 static DeviceState
*strongarm_gpio_init(target_phys_addr_t base
,
613 dev
= qdev_create(NULL
, "strongarm-gpio");
614 qdev_init_nofail(dev
);
616 sysbus_mmio_map(sysbus_from_qdev(dev
), 0, base
);
617 for (i
= 0; i
< 12; i
++)
618 sysbus_connect_irq(sysbus_from_qdev(dev
), i
,
619 qdev_get_gpio_in(pic
, SA_PIC_GPIO0_EDGE
+ i
));
624 static int strongarm_gpio_initfn(SysBusDevice
*dev
)
627 StrongARMGPIOInfo
*s
;
630 s
= FROM_SYSBUS(StrongARMGPIOInfo
, dev
);
632 qdev_init_gpio_in(&dev
->qdev
, strongarm_gpio_set
, 28);
633 qdev_init_gpio_out(&dev
->qdev
, s
->handler
, 28);
635 iomemtype
= cpu_register_io_memory(strongarm_gpio_readfn
,
636 strongarm_gpio_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
638 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
639 for (i
= 0; i
< 11; i
++) {
640 sysbus_init_irq(dev
, &s
->irqs
[i
]);
642 sysbus_init_irq(dev
, &s
->irqX
);
647 static const VMStateDescription vmstate_strongarm_gpio_regs
= {
648 .name
= "strongarm-gpio",
650 .minimum_version_id
= 0,
651 .minimum_version_id_old
= 0,
652 .fields
= (VMStateField
[]) {
653 VMSTATE_UINT32(ilevel
, StrongARMGPIOInfo
),
654 VMSTATE_UINT32(olevel
, StrongARMGPIOInfo
),
655 VMSTATE_UINT32(dir
, StrongARMGPIOInfo
),
656 VMSTATE_UINT32(rising
, StrongARMGPIOInfo
),
657 VMSTATE_UINT32(falling
, StrongARMGPIOInfo
),
658 VMSTATE_UINT32(status
, StrongARMGPIOInfo
),
659 VMSTATE_UINT32(gafr
, StrongARMGPIOInfo
),
660 VMSTATE_END_OF_LIST(),
664 static SysBusDeviceInfo strongarm_gpio_info
= {
665 .init
= strongarm_gpio_initfn
,
666 .qdev
.name
= "strongarm-gpio",
667 .qdev
.desc
= "StrongARM GPIO controller",
668 .qdev
.size
= sizeof(StrongARMGPIOInfo
),
671 /* Peripheral Pin Controller */
678 typedef struct StrongARMPPCInfo StrongARMPPCInfo
;
679 struct StrongARMPPCInfo
{
681 qemu_irq handler
[28];
693 static void strongarm_ppc_set(void *opaque
, int line
, int level
)
695 StrongARMPPCInfo
*s
= opaque
;
698 s
->ilevel
|= 1 << line
;
700 s
->ilevel
&= ~(1 << line
);
704 static void strongarm_ppc_handler_update(StrongARMPPCInfo
*s
)
706 uint32_t level
, diff
;
709 level
= s
->olevel
& s
->dir
;
711 for (diff
= s
->prev_level
^ level
; diff
; diff
^= 1 << bit
) {
713 qemu_set_irq(s
->handler
[bit
], (level
>> bit
) & 1);
716 s
->prev_level
= level
;
719 static uint32_t strongarm_ppc_read(void *opaque
, target_phys_addr_t offset
)
721 StrongARMPPCInfo
*s
= opaque
;
724 case PPDR
: /* PPC Pin Direction registers */
725 return s
->dir
| ~0x3fffff;
727 case PPSR
: /* PPC Pin State registers */
728 return (s
->olevel
& s
->dir
) |
729 (s
->ilevel
& ~s
->dir
) |
733 return s
->ppar
| ~0x41000;
739 return s
->ppfr
| ~0x7f001;
742 printf("%s: Bad offset 0x" TARGET_FMT_plx
"\n", __func__
, offset
);
748 static void strongarm_ppc_write(void *opaque
,
749 target_phys_addr_t offset
, uint32_t value
)
751 StrongARMPPCInfo
*s
= opaque
;
754 case PPDR
: /* PPC Pin Direction registers */
755 s
->dir
= value
& 0x3fffff;
756 strongarm_ppc_handler_update(s
);
759 case PPSR
: /* PPC Pin State registers */
760 s
->olevel
= value
& s
->dir
& 0x3fffff;
761 strongarm_ppc_handler_update(s
);
765 s
->ppar
= value
& 0x41000;
769 s
->psdr
= value
& 0x3fffff;
773 s
->ppfr
= value
& 0x7f001;
777 printf("%s: Bad offset 0x" TARGET_FMT_plx
"\n", __func__
, offset
);
781 static CPUReadMemoryFunc
* const strongarm_ppc_readfn
[] = {
787 static CPUWriteMemoryFunc
* const strongarm_ppc_writefn
[] = {
793 static int strongarm_ppc_init(SysBusDevice
*dev
)
798 s
= FROM_SYSBUS(StrongARMPPCInfo
, dev
);
800 qdev_init_gpio_in(&dev
->qdev
, strongarm_ppc_set
, 22);
801 qdev_init_gpio_out(&dev
->qdev
, s
->handler
, 22);
803 iomemtype
= cpu_register_io_memory(strongarm_ppc_readfn
,
804 strongarm_ppc_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
806 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
811 static const VMStateDescription vmstate_strongarm_ppc_regs
= {
812 .name
= "strongarm-ppc",
814 .minimum_version_id
= 0,
815 .minimum_version_id_old
= 0,
816 .fields
= (VMStateField
[]) {
817 VMSTATE_UINT32(ilevel
, StrongARMPPCInfo
),
818 VMSTATE_UINT32(olevel
, StrongARMPPCInfo
),
819 VMSTATE_UINT32(dir
, StrongARMPPCInfo
),
820 VMSTATE_UINT32(ppar
, StrongARMPPCInfo
),
821 VMSTATE_UINT32(psdr
, StrongARMPPCInfo
),
822 VMSTATE_UINT32(ppfr
, StrongARMPPCInfo
),
823 VMSTATE_END_OF_LIST(),
827 static SysBusDeviceInfo strongarm_ppc_info
= {
828 .init
= strongarm_ppc_init
,
829 .qdev
.name
= "strongarm-ppc",
830 .qdev
.desc
= "StrongARM PPC controller",
831 .qdev
.size
= sizeof(StrongARMPPCInfo
),
843 #define UTCR0_PE (1 << 0) /* Parity enable */
844 #define UTCR0_OES (1 << 1) /* Even parity */
845 #define UTCR0_SBS (1 << 2) /* 2 stop bits */
846 #define UTCR0_DSS (1 << 3) /* 8-bit data */
848 #define UTCR3_RXE (1 << 0) /* Rx enable */
849 #define UTCR3_TXE (1 << 1) /* Tx enable */
850 #define UTCR3_BRK (1 << 2) /* Force Break */
851 #define UTCR3_RIE (1 << 3) /* Rx int enable */
852 #define UTCR3_TIE (1 << 4) /* Tx int enable */
853 #define UTCR3_LBM (1 << 5) /* Loopback */
855 #define UTSR0_TFS (1 << 0) /* Tx FIFO nearly empty */
856 #define UTSR0_RFS (1 << 1) /* Rx FIFO nearly full */
857 #define UTSR0_RID (1 << 2) /* Receiver Idle */
858 #define UTSR0_RBB (1 << 3) /* Receiver begin break */
859 #define UTSR0_REB (1 << 4) /* Receiver end break */
860 #define UTSR0_EIF (1 << 5) /* Error in FIFO */
862 #define UTSR1_RNE (1 << 1) /* Receive FIFO not empty */
863 #define UTSR1_TNF (1 << 2) /* Transmit FIFO not full */
864 #define UTSR1_PRE (1 << 3) /* Parity error */
865 #define UTSR1_FRE (1 << 4) /* Frame error */
866 #define UTSR1_ROR (1 << 5) /* Receive Over Run */
868 #define RX_FIFO_PRE (1 << 8)
869 #define RX_FIFO_FRE (1 << 9)
870 #define RX_FIFO_ROR (1 << 10)
874 CharDriverState
*chr
;
886 uint16_t rx_fifo
[12]; /* value + error flags in high bits */
890 uint64_t char_transmit_time
; /* time to transmit a char in ticks*/
892 QEMUTimer
*rx_timeout_timer
;
894 } StrongARMUARTState
;
896 static void strongarm_uart_update_handlers(StrongARMUARTState
*s
);
898 static void strongarm_uart_update_status(StrongARMUARTState
*s
)
902 if (s
->tx_len
!= 8) {
906 if (s
->rx_len
!= 0) {
907 uint16_t ent
= s
->rx_fifo
[s
->rx_start
];
910 if (ent
& RX_FIFO_PRE
) {
911 s
->utsr1
|= UTSR1_PRE
;
913 if (ent
& RX_FIFO_FRE
) {
914 s
->utsr1
|= UTSR1_FRE
;
916 if (ent
& RX_FIFO_ROR
) {
917 s
->utsr1
|= UTSR1_ROR
;
924 static void strongarm_uart_update_int_status(StrongARMUARTState
*s
)
926 uint16_t utsr0
= s
->utsr0
&
927 (UTSR0_REB
| UTSR0_RBB
| UTSR0_RID
);
930 if ((s
->utcr3
& UTCR3_TXE
) &&
931 (s
->utcr3
& UTCR3_TIE
) &&
936 if ((s
->utcr3
& UTCR3_RXE
) &&
937 (s
->utcr3
& UTCR3_RIE
) &&
942 for (i
= 0; i
< s
->rx_len
&& i
< 4; i
++)
943 if (s
->rx_fifo
[(s
->rx_start
+ i
) % 12] & ~0xff) {
949 qemu_set_irq(s
->irq
, utsr0
);
952 static void strongarm_uart_update_parameters(StrongARMUARTState
*s
)
954 int speed
, parity
, data_bits
, stop_bits
, frame_size
;
955 QEMUSerialSetParams ssp
;
959 if (s
->utcr0
& UTCR0_PE
) {
962 if (s
->utcr0
& UTCR0_OES
) {
970 if (s
->utcr0
& UTCR0_SBS
) {
976 data_bits
= (s
->utcr0
& UTCR0_DSS
) ? 8 : 7;
977 frame_size
+= data_bits
+ stop_bits
;
978 speed
= 3686400 / 16 / (s
->brd
+ 1);
981 ssp
.data_bits
= data_bits
;
982 ssp
.stop_bits
= stop_bits
;
983 s
->char_transmit_time
= (get_ticks_per_sec() / speed
) * frame_size
;
985 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
988 DPRINTF(stderr
, "%s speed=%d parity=%c data=%d stop=%d\n", s
->chr
->label
,
989 speed
, parity
, data_bits
, stop_bits
);
992 static void strongarm_uart_rx_to(void *opaque
)
994 StrongARMUARTState
*s
= opaque
;
997 s
->utsr0
|= UTSR0_RID
;
998 strongarm_uart_update_int_status(s
);
1002 static void strongarm_uart_rx_push(StrongARMUARTState
*s
, uint16_t c
)
1004 if ((s
->utcr3
& UTCR3_RXE
) == 0) {
1009 if (s
->wait_break_end
) {
1010 s
->utsr0
|= UTSR0_REB
;
1011 s
->wait_break_end
= false;
1014 if (s
->rx_len
< 12) {
1015 s
->rx_fifo
[(s
->rx_start
+ s
->rx_len
) % 12] = c
;
1017 strongarm_uart_update_handlers(s
);
1019 s
->rx_fifo
[(s
->rx_start
+ 11) % 12] |= RX_FIFO_ROR
;
1022 static int strongarm_uart_can_receive(StrongARMUARTState
*s
)
1024 if (s
->rx_len
== 12) {
1027 /* It's best not to get more than 2/3 of RX FIFO, so advertise that much */
1028 if (s
->rx_len
< 8) {
1029 return 8 - s
->rx_len
;
1034 static void strongarm_uart_receive(StrongARMUARTState
*s
, const uint8_t *buf
, int size
)
1038 for (i
= 0; i
< size
; i
++) {
1039 strongarm_uart_rx_push(s
, buf
[i
]);
1042 /* call the timeout receive callback in 3 char transmit time */
1043 qemu_mod_timer(s
->rx_timeout_timer
,
1044 qemu_get_clock_ns(vm_clock
) + s
->char_transmit_time
* 3);
1046 strongarm_uart_update_status(s
);
1047 strongarm_uart_update_int_status(s
);
1050 static void strongarm_uart_receive_handler(void *opaque
)
1052 StrongARMUARTState
*s
= opaque
;
1056 size
= strongarm_uart_can_receive(s
);
1057 size
= MIN(size
, sizeof(buf
));
1058 size
= qemu_chr_fe_read(s
->chr
, buf
, size
);
1060 strongarm_uart_receive(s
, buf
, size
);
1063 static int strongarm_uart_event(void *opaque
, int event
, void *data
)
1065 StrongARMUARTState
*s
= opaque
;
1066 if (event
== CHR_EVENT_BREAK
) {
1067 s
->utsr0
|= UTSR0_RBB
;
1068 strongarm_uart_rx_push(s
, RX_FIFO_FRE
);
1069 s
->wait_break_end
= true;
1070 strongarm_uart_update_status(s
);
1071 strongarm_uart_update_int_status(s
);
1076 static void strongarm_uart_tx(void *opaque
)
1078 StrongARMUARTState
*s
= opaque
;
1079 uint64_t new_xmit_ts
= qemu_get_clock_ns(vm_clock
);
1081 if (s
->utcr3
& UTCR3_LBM
) /* loopback */ {
1082 strongarm_uart_receive(s
, &s
->tx_fifo
[s
->tx_start
], 1);
1083 } else if (s
->chr
) {
1084 qemu_chr_fe_write(s
->chr
, &s
->tx_fifo
[s
->tx_start
], 1);
1087 s
->tx_start
= (s
->tx_start
+ 1) % 8;
1090 qemu_mod_timer(s
->tx_timer
, new_xmit_ts
+ s
->char_transmit_time
);
1092 strongarm_uart_update_status(s
);
1093 strongarm_uart_update_int_status(s
);
1096 static uint32_t strongarm_uart_read(void *opaque
, target_phys_addr_t addr
)
1098 StrongARMUARTState
*s
= opaque
;
1109 return s
->brd
& 0xff;
1115 if (s
->rx_len
!= 0) {
1116 ret
= s
->rx_fifo
[s
->rx_start
];
1117 s
->rx_start
= (s
->rx_start
+ 1) % 12;
1119 strongarm_uart_update_handlers(s
);
1120 strongarm_uart_update_status(s
);
1121 strongarm_uart_update_int_status(s
);
1133 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1138 static void strongarm_uart_write(void *opaque
, target_phys_addr_t addr
,
1141 StrongARMUARTState
*s
= opaque
;
1145 s
->utcr0
= value
& 0x7f;
1146 strongarm_uart_update_parameters(s
);
1150 s
->brd
= (s
->brd
& 0xff) | ((value
& 0xf) << 8);
1151 strongarm_uart_update_parameters(s
);
1155 s
->brd
= (s
->brd
& 0xf00) | (value
& 0xff);
1156 strongarm_uart_update_parameters(s
);
1160 s
->utcr3
= value
& 0x3f;
1161 if ((s
->utcr3
& UTCR3_RXE
) == 0) {
1164 if ((s
->utcr3
& UTCR3_TXE
) == 0) {
1167 strongarm_uart_update_handlers(s
);
1168 strongarm_uart_update_status(s
);
1169 strongarm_uart_update_int_status(s
);
1173 if ((s
->utcr3
& UTCR3_TXE
) && s
->tx_len
!= 8) {
1174 s
->tx_fifo
[(s
->tx_start
+ s
->tx_len
) % 8] = value
;
1176 strongarm_uart_update_status(s
);
1177 strongarm_uart_update_int_status(s
);
1178 if (s
->tx_len
== 1) {
1179 strongarm_uart_tx(s
);
1185 s
->utsr0
= s
->utsr0
& ~(value
&
1186 (UTSR0_REB
| UTSR0_RBB
| UTSR0_RID
));
1187 strongarm_uart_update_int_status(s
);
1191 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1195 static CPUReadMemoryFunc
* const strongarm_uart_readfn
[] = {
1196 strongarm_uart_read
,
1197 strongarm_uart_read
,
1198 strongarm_uart_read
,
1201 static CPUWriteMemoryFunc
* const strongarm_uart_writefn
[] = {
1202 strongarm_uart_write
,
1203 strongarm_uart_write
,
1204 strongarm_uart_write
,
1207 static void strongarm_uart_update_handlers(StrongARMUARTState
*s
)
1209 if (strongarm_uart_can_receive(s
) > 0) {
1210 qemu_chr_fe_set_handlers(s
->chr
,
1211 strongarm_uart_receive_handler
,
1213 strongarm_uart_event
,
1216 qemu_chr_fe_set_handlers(s
->chr
,
1219 strongarm_uart_event
,
1224 static int strongarm_uart_init(SysBusDevice
*dev
)
1226 StrongARMUARTState
*s
= FROM_SYSBUS(StrongARMUARTState
, dev
);
1229 iomemtype
= cpu_register_io_memory(strongarm_uart_readfn
,
1230 strongarm_uart_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
1231 sysbus_init_mmio(dev
, 0x10000, iomemtype
);
1232 sysbus_init_irq(dev
, &s
->irq
);
1234 s
->rx_timeout_timer
= qemu_new_timer_ns(vm_clock
, strongarm_uart_rx_to
, s
);
1235 s
->tx_timer
= qemu_new_timer_ns(vm_clock
, strongarm_uart_tx
, s
);
1238 qemu_chr_fe_open(s
->chr
);
1239 strongarm_uart_update_handlers(s
);
1245 static void strongarm_uart_reset(DeviceState
*dev
)
1247 StrongARMUARTState
*s
= DO_UPCAST(StrongARMUARTState
, busdev
.qdev
, dev
);
1249 s
->utcr0
= UTCR0_DSS
; /* 8 data, no parity */
1250 s
->brd
= 23; /* 9600 */
1251 /* enable send & recv - this actually violates spec */
1252 s
->utcr3
= UTCR3_TXE
| UTCR3_RXE
;
1254 s
->rx_len
= s
->tx_len
= 0;
1256 strongarm_uart_update_handlers(s
);
1257 strongarm_uart_update_parameters(s
);
1258 strongarm_uart_update_status(s
);
1259 strongarm_uart_update_int_status(s
);
1262 static int strongarm_uart_post_load(void *opaque
, int version_id
)
1264 StrongARMUARTState
*s
= opaque
;
1266 strongarm_uart_update_parameters(s
);
1267 strongarm_uart_update_status(s
);
1268 strongarm_uart_update_int_status(s
);
1270 /* tx and restart timer */
1272 strongarm_uart_tx(s
);
1275 /* restart rx timeout timer */
1277 qemu_mod_timer(s
->rx_timeout_timer
,
1278 qemu_get_clock_ns(vm_clock
) + s
->char_transmit_time
* 3);
1284 static const VMStateDescription vmstate_strongarm_uart_regs
= {
1285 .name
= "strongarm-uart",
1287 .minimum_version_id
= 0,
1288 .minimum_version_id_old
= 0,
1289 .post_load
= strongarm_uart_post_load
,
1290 .fields
= (VMStateField
[]) {
1291 VMSTATE_UINT8(utcr0
, StrongARMUARTState
),
1292 VMSTATE_UINT16(brd
, StrongARMUARTState
),
1293 VMSTATE_UINT8(utcr3
, StrongARMUARTState
),
1294 VMSTATE_UINT8(utsr0
, StrongARMUARTState
),
1295 VMSTATE_UINT8_ARRAY(tx_fifo
, StrongARMUARTState
, 8),
1296 VMSTATE_UINT8(tx_start
, StrongARMUARTState
),
1297 VMSTATE_UINT8(tx_len
, StrongARMUARTState
),
1298 VMSTATE_UINT16_ARRAY(rx_fifo
, StrongARMUARTState
, 12),
1299 VMSTATE_UINT8(rx_start
, StrongARMUARTState
),
1300 VMSTATE_UINT8(rx_len
, StrongARMUARTState
),
1301 VMSTATE_BOOL(wait_break_end
, StrongARMUARTState
),
1302 VMSTATE_END_OF_LIST(),
1306 static SysBusDeviceInfo strongarm_uart_info
= {
1307 .init
= strongarm_uart_init
,
1308 .qdev
.name
= "strongarm-uart",
1309 .qdev
.desc
= "StrongARM UART controller",
1310 .qdev
.size
= sizeof(StrongARMUARTState
),
1311 .qdev
.reset
= strongarm_uart_reset
,
1312 .qdev
.vmsd
= &vmstate_strongarm_uart_regs
,
1313 .qdev
.props
= (Property
[]) {
1314 DEFINE_PROP_CHR("chardev", StrongARMUARTState
, chr
),
1315 DEFINE_PROP_END_OF_LIST(),
1319 /* Synchronous Serial Ports */
1321 SysBusDevice busdev
;
1328 uint16_t rx_fifo
[8];
1331 } StrongARMSSPState
;
1333 #define SSCR0 0x60 /* SSP Control register 0 */
1334 #define SSCR1 0x64 /* SSP Control register 1 */
1335 #define SSDR 0x6c /* SSP Data register */
1336 #define SSSR 0x74 /* SSP Status register */
1338 /* Bitfields for above registers */
1339 #define SSCR0_SPI(x) (((x) & 0x30) == 0x00)
1340 #define SSCR0_SSP(x) (((x) & 0x30) == 0x10)
1341 #define SSCR0_UWIRE(x) (((x) & 0x30) == 0x20)
1342 #define SSCR0_PSP(x) (((x) & 0x30) == 0x30)
1343 #define SSCR0_SSE (1 << 7)
1344 #define SSCR0_DSS(x) (((x) & 0xf) + 1)
1345 #define SSCR1_RIE (1 << 0)
1346 #define SSCR1_TIE (1 << 1)
1347 #define SSCR1_LBM (1 << 2)
1348 #define SSSR_TNF (1 << 2)
1349 #define SSSR_RNE (1 << 3)
1350 #define SSSR_TFS (1 << 5)
1351 #define SSSR_RFS (1 << 6)
1352 #define SSSR_ROR (1 << 7)
1353 #define SSSR_RW 0x0080
1355 static void strongarm_ssp_int_update(StrongARMSSPState
*s
)
1359 level
|= (s
->sssr
& SSSR_ROR
);
1360 level
|= (s
->sssr
& SSSR_RFS
) && (s
->sscr
[1] & SSCR1_RIE
);
1361 level
|= (s
->sssr
& SSSR_TFS
) && (s
->sscr
[1] & SSCR1_TIE
);
1362 qemu_set_irq(s
->irq
, level
);
1365 static void strongarm_ssp_fifo_update(StrongARMSSPState
*s
)
1367 s
->sssr
&= ~SSSR_TFS
;
1368 s
->sssr
&= ~SSSR_TNF
;
1369 if (s
->sscr
[0] & SSCR0_SSE
) {
1370 if (s
->rx_level
>= 4) {
1371 s
->sssr
|= SSSR_RFS
;
1373 s
->sssr
&= ~SSSR_RFS
;
1376 s
->sssr
|= SSSR_RNE
;
1378 s
->sssr
&= ~SSSR_RNE
;
1380 /* TX FIFO is never filled, so it is always in underrun
1381 condition if SSP is enabled */
1382 s
->sssr
|= SSSR_TFS
;
1383 s
->sssr
|= SSSR_TNF
;
1386 strongarm_ssp_int_update(s
);
1389 static uint32_t strongarm_ssp_read(void *opaque
, target_phys_addr_t addr
)
1391 StrongARMSSPState
*s
= opaque
;
1402 if (~s
->sscr
[0] & SSCR0_SSE
) {
1405 if (s
->rx_level
< 1) {
1406 printf("%s: SSP Rx Underrun\n", __func__
);
1410 retval
= s
->rx_fifo
[s
->rx_start
++];
1412 strongarm_ssp_fifo_update(s
);
1415 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1421 static void strongarm_ssp_write(void *opaque
, target_phys_addr_t addr
,
1424 StrongARMSSPState
*s
= opaque
;
1428 s
->sscr
[0] = value
& 0xffbf;
1429 if ((s
->sscr
[0] & SSCR0_SSE
) && SSCR0_DSS(value
) < 4) {
1430 printf("%s: Wrong data size: %i bits\n", __func__
,
1433 if (!(value
& SSCR0_SSE
)) {
1437 strongarm_ssp_fifo_update(s
);
1441 s
->sscr
[1] = value
& 0x2f;
1442 if (value
& SSCR1_LBM
) {
1443 printf("%s: Attempt to use SSP LBM mode\n", __func__
);
1445 strongarm_ssp_fifo_update(s
);
1449 s
->sssr
&= ~(value
& SSSR_RW
);
1450 strongarm_ssp_int_update(s
);
1454 if (SSCR0_UWIRE(s
->sscr
[0])) {
1457 /* Note how 32bits overflow does no harm here */
1458 value
&= (1 << SSCR0_DSS(s
->sscr
[0])) - 1;
1460 /* Data goes from here to the Tx FIFO and is shifted out from
1461 * there directly to the slave, no need to buffer it.
1463 if (s
->sscr
[0] & SSCR0_SSE
) {
1465 if (s
->sscr
[1] & SSCR1_LBM
) {
1468 readval
= ssi_transfer(s
->bus
, value
);
1471 if (s
->rx_level
< 0x08) {
1472 s
->rx_fifo
[(s
->rx_start
+ s
->rx_level
++) & 0x7] = readval
;
1474 s
->sssr
|= SSSR_ROR
;
1477 strongarm_ssp_fifo_update(s
);
1481 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1486 static CPUReadMemoryFunc
* const strongarm_ssp_readfn
[] = {
1492 static CPUWriteMemoryFunc
* const strongarm_ssp_writefn
[] = {
1493 strongarm_ssp_write
,
1494 strongarm_ssp_write
,
1495 strongarm_ssp_write
,
1498 static int strongarm_ssp_post_load(void *opaque
, int version_id
)
1500 StrongARMSSPState
*s
= opaque
;
1502 strongarm_ssp_fifo_update(s
);
1507 static int strongarm_ssp_init(SysBusDevice
*dev
)
1510 StrongARMSSPState
*s
= FROM_SYSBUS(StrongARMSSPState
, dev
);
1512 sysbus_init_irq(dev
, &s
->irq
);
1514 iomemtype
= cpu_register_io_memory(strongarm_ssp_readfn
,
1515 strongarm_ssp_writefn
, s
,
1516 DEVICE_NATIVE_ENDIAN
);
1517 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
1519 s
->bus
= ssi_create_bus(&dev
->qdev
, "ssi");
1523 static void strongarm_ssp_reset(DeviceState
*dev
)
1525 StrongARMSSPState
*s
= DO_UPCAST(StrongARMSSPState
, busdev
.qdev
, dev
);
1526 s
->sssr
= 0x03; /* 3 bit data, SPI, disabled */
1531 static const VMStateDescription vmstate_strongarm_ssp_regs
= {
1532 .name
= "strongarm-ssp",
1534 .minimum_version_id
= 0,
1535 .minimum_version_id_old
= 0,
1536 .post_load
= strongarm_ssp_post_load
,
1537 .fields
= (VMStateField
[]) {
1538 VMSTATE_UINT16_ARRAY(sscr
, StrongARMSSPState
, 2),
1539 VMSTATE_UINT16(sssr
, StrongARMSSPState
),
1540 VMSTATE_UINT16_ARRAY(rx_fifo
, StrongARMSSPState
, 8),
1541 VMSTATE_UINT8(rx_start
, StrongARMSSPState
),
1542 VMSTATE_UINT8(rx_level
, StrongARMSSPState
),
1543 VMSTATE_END_OF_LIST(),
1547 static SysBusDeviceInfo strongarm_ssp_info
= {
1548 .init
= strongarm_ssp_init
,
1549 .qdev
.name
= "strongarm-ssp",
1550 .qdev
.desc
= "StrongARM SSP controller",
1551 .qdev
.size
= sizeof(StrongARMSSPState
),
1552 .qdev
.reset
= strongarm_ssp_reset
,
1553 .qdev
.vmsd
= &vmstate_strongarm_ssp_regs
,
1556 /* Main CPU functions */
1557 StrongARMState
*sa1110_init(unsigned int sdram_size
, const char *rev
)
1563 s
= qemu_mallocz(sizeof(StrongARMState
));
1569 if (strncmp(rev
, "sa1110", 6)) {
1570 error_report("Machine requires a SA1110 processor.");
1574 s
->env
= cpu_init(rev
);
1577 error_report("Unable to find CPU definition");
1581 cpu_register_physical_memory(SA_SDCS0
,
1582 sdram_size
, qemu_ram_alloc(NULL
, "strongarm.sdram",
1583 sdram_size
) | IO_MEM_RAM
);
1585 pic
= arm_pic_init_cpu(s
->env
);
1586 s
->pic
= sysbus_create_varargs("strongarm_pic", 0x90050000,
1587 pic
[ARM_PIC_CPU_IRQ
], pic
[ARM_PIC_CPU_FIQ
], NULL
);
1589 sysbus_create_varargs("pxa25x-timer", 0x90000000,
1590 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC0
),
1591 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC1
),
1592 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC2
),
1593 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC3
),
1596 sysbus_create_simple("strongarm-rtc", 0x90010000,
1597 qdev_get_gpio_in(s
->pic
, SA_PIC_RTC_ALARM
));
1599 s
->gpio
= strongarm_gpio_init(0x90040000, s
->pic
);
1601 s
->ppc
= sysbus_create_varargs("strongarm-ppc", 0x90060000, NULL
);
1603 for (i
= 0; sa_serial
[i
].io_base
; i
++) {
1604 DeviceState
*dev
= qdev_create(NULL
, "strongarm-uart");
1605 qdev_prop_set_chr(dev
, "chardev", serial_hds
[i
]);
1606 qdev_init_nofail(dev
);
1607 sysbus_mmio_map(sysbus_from_qdev(dev
), 0,
1608 sa_serial
[i
].io_base
);
1609 sysbus_connect_irq(sysbus_from_qdev(dev
), 0,
1610 qdev_get_gpio_in(s
->pic
, sa_serial
[i
].irq
));
1613 s
->ssp
= sysbus_create_varargs("strongarm-ssp", 0x80070000,
1614 qdev_get_gpio_in(s
->pic
, SA_PIC_SSP
), NULL
);
1615 s
->ssp_bus
= (SSIBus
*)qdev_get_child_bus(s
->ssp
, "ssi");
1620 static void strongarm_register_devices(void)
1622 sysbus_register_withprop(&strongarm_pic_info
);
1623 sysbus_register_withprop(&strongarm_rtc_sysbus_info
);
1624 sysbus_register_withprop(&strongarm_gpio_info
);
1625 sysbus_register_withprop(&strongarm_ppc_info
);
1626 sysbus_register_withprop(&strongarm_uart_info
);
1627 sysbus_register_withprop(&strongarm_ssp_info
);
1629 device_init(strongarm_register_devices
)