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_status(StrongARMUARTState
*s
)
900 if (s
->tx_len
!= 8) {
904 if (s
->rx_len
!= 0) {
905 uint16_t ent
= s
->rx_fifo
[s
->rx_start
];
908 if (ent
& RX_FIFO_PRE
) {
909 s
->utsr1
|= UTSR1_PRE
;
911 if (ent
& RX_FIFO_FRE
) {
912 s
->utsr1
|= UTSR1_FRE
;
914 if (ent
& RX_FIFO_ROR
) {
915 s
->utsr1
|= UTSR1_ROR
;
922 static void strongarm_uart_update_int_status(StrongARMUARTState
*s
)
924 uint16_t utsr0
= s
->utsr0
&
925 (UTSR0_REB
| UTSR0_RBB
| UTSR0_RID
);
928 if ((s
->utcr3
& UTCR3_TXE
) &&
929 (s
->utcr3
& UTCR3_TIE
) &&
934 if ((s
->utcr3
& UTCR3_RXE
) &&
935 (s
->utcr3
& UTCR3_RIE
) &&
940 for (i
= 0; i
< s
->rx_len
&& i
< 4; i
++)
941 if (s
->rx_fifo
[(s
->rx_start
+ i
) % 12] & ~0xff) {
947 qemu_set_irq(s
->irq
, utsr0
);
950 static void strongarm_uart_update_parameters(StrongARMUARTState
*s
)
952 int speed
, parity
, data_bits
, stop_bits
, frame_size
;
953 QEMUSerialSetParams ssp
;
957 if (s
->utcr0
& UTCR0_PE
) {
960 if (s
->utcr0
& UTCR0_OES
) {
968 if (s
->utcr0
& UTCR0_SBS
) {
974 data_bits
= (s
->utcr0
& UTCR0_DSS
) ? 8 : 7;
975 frame_size
+= data_bits
+ stop_bits
;
976 speed
= 3686400 / 16 / (s
->brd
+ 1);
979 ssp
.data_bits
= data_bits
;
980 ssp
.stop_bits
= stop_bits
;
981 s
->char_transmit_time
= (get_ticks_per_sec() / speed
) * frame_size
;
983 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
986 DPRINTF(stderr
, "%s speed=%d parity=%c data=%d stop=%d\n", s
->chr
->label
,
987 speed
, parity
, data_bits
, stop_bits
);
990 static void strongarm_uart_rx_to(void *opaque
)
992 StrongARMUARTState
*s
= opaque
;
995 s
->utsr0
|= UTSR0_RID
;
996 strongarm_uart_update_int_status(s
);
1000 static void strongarm_uart_rx_push(StrongARMUARTState
*s
, uint16_t c
)
1002 if ((s
->utcr3
& UTCR3_RXE
) == 0) {
1007 if (s
->wait_break_end
) {
1008 s
->utsr0
|= UTSR0_REB
;
1009 s
->wait_break_end
= false;
1012 if (s
->rx_len
< 12) {
1013 s
->rx_fifo
[(s
->rx_start
+ s
->rx_len
) % 12] = c
;
1016 s
->rx_fifo
[(s
->rx_start
+ 11) % 12] |= RX_FIFO_ROR
;
1019 static int strongarm_uart_can_receive(void *opaque
)
1021 StrongARMUARTState
*s
= opaque
;
1023 if (s
->rx_len
== 12) {
1026 /* It's best not to get more than 2/3 of RX FIFO, so advertise that much */
1027 if (s
->rx_len
< 8) {
1028 return 8 - s
->rx_len
;
1033 static void strongarm_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
1035 StrongARMUARTState
*s
= opaque
;
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_event(void *opaque
, int event
)
1052 StrongARMUARTState
*s
= opaque
;
1053 if (event
== CHR_EVENT_BREAK
) {
1054 s
->utsr0
|= UTSR0_RBB
;
1055 strongarm_uart_rx_push(s
, RX_FIFO_FRE
);
1056 s
->wait_break_end
= true;
1057 strongarm_uart_update_status(s
);
1058 strongarm_uart_update_int_status(s
);
1062 static void strongarm_uart_tx(void *opaque
)
1064 StrongARMUARTState
*s
= opaque
;
1065 uint64_t new_xmit_ts
= qemu_get_clock_ns(vm_clock
);
1067 if (s
->utcr3
& UTCR3_LBM
) /* loopback */ {
1068 strongarm_uart_receive(s
, &s
->tx_fifo
[s
->tx_start
], 1);
1069 } else if (s
->chr
) {
1070 qemu_chr_write(s
->chr
, &s
->tx_fifo
[s
->tx_start
], 1);
1073 s
->tx_start
= (s
->tx_start
+ 1) % 8;
1076 qemu_mod_timer(s
->tx_timer
, new_xmit_ts
+ s
->char_transmit_time
);
1078 strongarm_uart_update_status(s
);
1079 strongarm_uart_update_int_status(s
);
1082 static uint32_t strongarm_uart_read(void *opaque
, target_phys_addr_t addr
)
1084 StrongARMUARTState
*s
= opaque
;
1095 return s
->brd
& 0xff;
1101 if (s
->rx_len
!= 0) {
1102 ret
= s
->rx_fifo
[s
->rx_start
];
1103 s
->rx_start
= (s
->rx_start
+ 1) % 12;
1105 strongarm_uart_update_status(s
);
1106 strongarm_uart_update_int_status(s
);
1118 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1123 static void strongarm_uart_write(void *opaque
, target_phys_addr_t addr
,
1126 StrongARMUARTState
*s
= opaque
;
1130 s
->utcr0
= value
& 0x7f;
1131 strongarm_uart_update_parameters(s
);
1135 s
->brd
= (s
->brd
& 0xff) | ((value
& 0xf) << 8);
1136 strongarm_uart_update_parameters(s
);
1140 s
->brd
= (s
->brd
& 0xf00) | (value
& 0xff);
1141 strongarm_uart_update_parameters(s
);
1145 s
->utcr3
= value
& 0x3f;
1146 if ((s
->utcr3
& UTCR3_RXE
) == 0) {
1149 if ((s
->utcr3
& UTCR3_TXE
) == 0) {
1152 strongarm_uart_update_status(s
);
1153 strongarm_uart_update_int_status(s
);
1157 if ((s
->utcr3
& UTCR3_TXE
) && s
->tx_len
!= 8) {
1158 s
->tx_fifo
[(s
->tx_start
+ s
->tx_len
) % 8] = value
;
1160 strongarm_uart_update_status(s
);
1161 strongarm_uart_update_int_status(s
);
1162 if (s
->tx_len
== 1) {
1163 strongarm_uart_tx(s
);
1169 s
->utsr0
= s
->utsr0
& ~(value
&
1170 (UTSR0_REB
| UTSR0_RBB
| UTSR0_RID
));
1171 strongarm_uart_update_int_status(s
);
1175 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1179 static CPUReadMemoryFunc
* const strongarm_uart_readfn
[] = {
1180 strongarm_uart_read
,
1181 strongarm_uart_read
,
1182 strongarm_uart_read
,
1185 static CPUWriteMemoryFunc
* const strongarm_uart_writefn
[] = {
1186 strongarm_uart_write
,
1187 strongarm_uart_write
,
1188 strongarm_uart_write
,
1191 static int strongarm_uart_init(SysBusDevice
*dev
)
1193 StrongARMUARTState
*s
= FROM_SYSBUS(StrongARMUARTState
, dev
);
1196 iomemtype
= cpu_register_io_memory(strongarm_uart_readfn
,
1197 strongarm_uart_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
1198 sysbus_init_mmio(dev
, 0x10000, iomemtype
);
1199 sysbus_init_irq(dev
, &s
->irq
);
1201 s
->rx_timeout_timer
= qemu_new_timer_ns(vm_clock
, strongarm_uart_rx_to
, s
);
1202 s
->tx_timer
= qemu_new_timer_ns(vm_clock
, strongarm_uart_tx
, s
);
1205 qemu_chr_add_handlers(s
->chr
,
1206 strongarm_uart_can_receive
,
1207 strongarm_uart_receive
,
1208 strongarm_uart_event
,
1215 static void strongarm_uart_reset(DeviceState
*dev
)
1217 StrongARMUARTState
*s
= DO_UPCAST(StrongARMUARTState
, busdev
.qdev
, dev
);
1219 s
->utcr0
= UTCR0_DSS
; /* 8 data, no parity */
1220 s
->brd
= 23; /* 9600 */
1221 /* enable send & recv - this actually violates spec */
1222 s
->utcr3
= UTCR3_TXE
| UTCR3_RXE
;
1224 s
->rx_len
= s
->tx_len
= 0;
1226 strongarm_uart_update_parameters(s
);
1227 strongarm_uart_update_status(s
);
1228 strongarm_uart_update_int_status(s
);
1231 static int strongarm_uart_post_load(void *opaque
, int version_id
)
1233 StrongARMUARTState
*s
= opaque
;
1235 strongarm_uart_update_parameters(s
);
1236 strongarm_uart_update_status(s
);
1237 strongarm_uart_update_int_status(s
);
1239 /* tx and restart timer */
1241 strongarm_uart_tx(s
);
1244 /* restart rx timeout timer */
1246 qemu_mod_timer(s
->rx_timeout_timer
,
1247 qemu_get_clock_ns(vm_clock
) + s
->char_transmit_time
* 3);
1253 static const VMStateDescription vmstate_strongarm_uart_regs
= {
1254 .name
= "strongarm-uart",
1256 .minimum_version_id
= 0,
1257 .minimum_version_id_old
= 0,
1258 .post_load
= strongarm_uart_post_load
,
1259 .fields
= (VMStateField
[]) {
1260 VMSTATE_UINT8(utcr0
, StrongARMUARTState
),
1261 VMSTATE_UINT16(brd
, StrongARMUARTState
),
1262 VMSTATE_UINT8(utcr3
, StrongARMUARTState
),
1263 VMSTATE_UINT8(utsr0
, StrongARMUARTState
),
1264 VMSTATE_UINT8_ARRAY(tx_fifo
, StrongARMUARTState
, 8),
1265 VMSTATE_UINT8(tx_start
, StrongARMUARTState
),
1266 VMSTATE_UINT8(tx_len
, StrongARMUARTState
),
1267 VMSTATE_UINT16_ARRAY(rx_fifo
, StrongARMUARTState
, 12),
1268 VMSTATE_UINT8(rx_start
, StrongARMUARTState
),
1269 VMSTATE_UINT8(rx_len
, StrongARMUARTState
),
1270 VMSTATE_BOOL(wait_break_end
, StrongARMUARTState
),
1271 VMSTATE_END_OF_LIST(),
1275 static SysBusDeviceInfo strongarm_uart_info
= {
1276 .init
= strongarm_uart_init
,
1277 .qdev
.name
= "strongarm-uart",
1278 .qdev
.desc
= "StrongARM UART controller",
1279 .qdev
.size
= sizeof(StrongARMUARTState
),
1280 .qdev
.reset
= strongarm_uart_reset
,
1281 .qdev
.vmsd
= &vmstate_strongarm_uart_regs
,
1282 .qdev
.props
= (Property
[]) {
1283 DEFINE_PROP_CHR("chardev", StrongARMUARTState
, chr
),
1284 DEFINE_PROP_END_OF_LIST(),
1288 /* Synchronous Serial Ports */
1290 SysBusDevice busdev
;
1297 uint16_t rx_fifo
[8];
1300 } StrongARMSSPState
;
1302 #define SSCR0 0x60 /* SSP Control register 0 */
1303 #define SSCR1 0x64 /* SSP Control register 1 */
1304 #define SSDR 0x6c /* SSP Data register */
1305 #define SSSR 0x74 /* SSP Status register */
1307 /* Bitfields for above registers */
1308 #define SSCR0_SPI(x) (((x) & 0x30) == 0x00)
1309 #define SSCR0_SSP(x) (((x) & 0x30) == 0x10)
1310 #define SSCR0_UWIRE(x) (((x) & 0x30) == 0x20)
1311 #define SSCR0_PSP(x) (((x) & 0x30) == 0x30)
1312 #define SSCR0_SSE (1 << 7)
1313 #define SSCR0_DSS(x) (((x) & 0xf) + 1)
1314 #define SSCR1_RIE (1 << 0)
1315 #define SSCR1_TIE (1 << 1)
1316 #define SSCR1_LBM (1 << 2)
1317 #define SSSR_TNF (1 << 2)
1318 #define SSSR_RNE (1 << 3)
1319 #define SSSR_TFS (1 << 5)
1320 #define SSSR_RFS (1 << 6)
1321 #define SSSR_ROR (1 << 7)
1322 #define SSSR_RW 0x0080
1324 static void strongarm_ssp_int_update(StrongARMSSPState
*s
)
1328 level
|= (s
->sssr
& SSSR_ROR
);
1329 level
|= (s
->sssr
& SSSR_RFS
) && (s
->sscr
[1] & SSCR1_RIE
);
1330 level
|= (s
->sssr
& SSSR_TFS
) && (s
->sscr
[1] & SSCR1_TIE
);
1331 qemu_set_irq(s
->irq
, level
);
1334 static void strongarm_ssp_fifo_update(StrongARMSSPState
*s
)
1336 s
->sssr
&= ~SSSR_TFS
;
1337 s
->sssr
&= ~SSSR_TNF
;
1338 if (s
->sscr
[0] & SSCR0_SSE
) {
1339 if (s
->rx_level
>= 4) {
1340 s
->sssr
|= SSSR_RFS
;
1342 s
->sssr
&= ~SSSR_RFS
;
1345 s
->sssr
|= SSSR_RNE
;
1347 s
->sssr
&= ~SSSR_RNE
;
1349 /* TX FIFO is never filled, so it is always in underrun
1350 condition if SSP is enabled */
1351 s
->sssr
|= SSSR_TFS
;
1352 s
->sssr
|= SSSR_TNF
;
1355 strongarm_ssp_int_update(s
);
1358 static uint32_t strongarm_ssp_read(void *opaque
, target_phys_addr_t addr
)
1360 StrongARMSSPState
*s
= opaque
;
1371 if (~s
->sscr
[0] & SSCR0_SSE
) {
1374 if (s
->rx_level
< 1) {
1375 printf("%s: SSP Rx Underrun\n", __func__
);
1379 retval
= s
->rx_fifo
[s
->rx_start
++];
1381 strongarm_ssp_fifo_update(s
);
1384 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1390 static void strongarm_ssp_write(void *opaque
, target_phys_addr_t addr
,
1393 StrongARMSSPState
*s
= opaque
;
1397 s
->sscr
[0] = value
& 0xffbf;
1398 if ((s
->sscr
[0] & SSCR0_SSE
) && SSCR0_DSS(value
) < 4) {
1399 printf("%s: Wrong data size: %i bits\n", __func__
,
1402 if (!(value
& SSCR0_SSE
)) {
1406 strongarm_ssp_fifo_update(s
);
1410 s
->sscr
[1] = value
& 0x2f;
1411 if (value
& SSCR1_LBM
) {
1412 printf("%s: Attempt to use SSP LBM mode\n", __func__
);
1414 strongarm_ssp_fifo_update(s
);
1418 s
->sssr
&= ~(value
& SSSR_RW
);
1419 strongarm_ssp_int_update(s
);
1423 if (SSCR0_UWIRE(s
->sscr
[0])) {
1426 /* Note how 32bits overflow does no harm here */
1427 value
&= (1 << SSCR0_DSS(s
->sscr
[0])) - 1;
1429 /* Data goes from here to the Tx FIFO and is shifted out from
1430 * there directly to the slave, no need to buffer it.
1432 if (s
->sscr
[0] & SSCR0_SSE
) {
1434 if (s
->sscr
[1] & SSCR1_LBM
) {
1437 readval
= ssi_transfer(s
->bus
, value
);
1440 if (s
->rx_level
< 0x08) {
1441 s
->rx_fifo
[(s
->rx_start
+ s
->rx_level
++) & 0x7] = readval
;
1443 s
->sssr
|= SSSR_ROR
;
1446 strongarm_ssp_fifo_update(s
);
1450 printf("%s: Bad register 0x" TARGET_FMT_plx
"\n", __func__
, addr
);
1455 static CPUReadMemoryFunc
* const strongarm_ssp_readfn
[] = {
1461 static CPUWriteMemoryFunc
* const strongarm_ssp_writefn
[] = {
1462 strongarm_ssp_write
,
1463 strongarm_ssp_write
,
1464 strongarm_ssp_write
,
1467 static int strongarm_ssp_post_load(void *opaque
, int version_id
)
1469 StrongARMSSPState
*s
= opaque
;
1471 strongarm_ssp_fifo_update(s
);
1476 static int strongarm_ssp_init(SysBusDevice
*dev
)
1479 StrongARMSSPState
*s
= FROM_SYSBUS(StrongARMSSPState
, dev
);
1481 sysbus_init_irq(dev
, &s
->irq
);
1483 iomemtype
= cpu_register_io_memory(strongarm_ssp_readfn
,
1484 strongarm_ssp_writefn
, s
,
1485 DEVICE_NATIVE_ENDIAN
);
1486 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
1488 s
->bus
= ssi_create_bus(&dev
->qdev
, "ssi");
1492 static void strongarm_ssp_reset(DeviceState
*dev
)
1494 StrongARMSSPState
*s
= DO_UPCAST(StrongARMSSPState
, busdev
.qdev
, dev
);
1495 s
->sssr
= 0x03; /* 3 bit data, SPI, disabled */
1500 static const VMStateDescription vmstate_strongarm_ssp_regs
= {
1501 .name
= "strongarm-ssp",
1503 .minimum_version_id
= 0,
1504 .minimum_version_id_old
= 0,
1505 .post_load
= strongarm_ssp_post_load
,
1506 .fields
= (VMStateField
[]) {
1507 VMSTATE_UINT16_ARRAY(sscr
, StrongARMSSPState
, 2),
1508 VMSTATE_UINT16(sssr
, StrongARMSSPState
),
1509 VMSTATE_UINT16_ARRAY(rx_fifo
, StrongARMSSPState
, 8),
1510 VMSTATE_UINT8(rx_start
, StrongARMSSPState
),
1511 VMSTATE_UINT8(rx_level
, StrongARMSSPState
),
1512 VMSTATE_END_OF_LIST(),
1516 static SysBusDeviceInfo strongarm_ssp_info
= {
1517 .init
= strongarm_ssp_init
,
1518 .qdev
.name
= "strongarm-ssp",
1519 .qdev
.desc
= "StrongARM SSP controller",
1520 .qdev
.size
= sizeof(StrongARMSSPState
),
1521 .qdev
.reset
= strongarm_ssp_reset
,
1522 .qdev
.vmsd
= &vmstate_strongarm_ssp_regs
,
1525 /* Main CPU functions */
1526 StrongARMState
*sa1110_init(unsigned int sdram_size
, const char *rev
)
1532 s
= g_malloc0(sizeof(StrongARMState
));
1538 if (strncmp(rev
, "sa1110", 6)) {
1539 error_report("Machine requires a SA1110 processor.");
1543 s
->env
= cpu_init(rev
);
1546 error_report("Unable to find CPU definition");
1550 cpu_register_physical_memory(SA_SDCS0
,
1551 sdram_size
, qemu_ram_alloc(NULL
, "strongarm.sdram",
1552 sdram_size
) | IO_MEM_RAM
);
1554 pic
= arm_pic_init_cpu(s
->env
);
1555 s
->pic
= sysbus_create_varargs("strongarm_pic", 0x90050000,
1556 pic
[ARM_PIC_CPU_IRQ
], pic
[ARM_PIC_CPU_FIQ
], NULL
);
1558 sysbus_create_varargs("pxa25x-timer", 0x90000000,
1559 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC0
),
1560 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC1
),
1561 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC2
),
1562 qdev_get_gpio_in(s
->pic
, SA_PIC_OSTC3
),
1565 sysbus_create_simple("strongarm-rtc", 0x90010000,
1566 qdev_get_gpio_in(s
->pic
, SA_PIC_RTC_ALARM
));
1568 s
->gpio
= strongarm_gpio_init(0x90040000, s
->pic
);
1570 s
->ppc
= sysbus_create_varargs("strongarm-ppc", 0x90060000, NULL
);
1572 for (i
= 0; sa_serial
[i
].io_base
; i
++) {
1573 DeviceState
*dev
= qdev_create(NULL
, "strongarm-uart");
1574 qdev_prop_set_chr(dev
, "chardev", serial_hds
[i
]);
1575 qdev_init_nofail(dev
);
1576 sysbus_mmio_map(sysbus_from_qdev(dev
), 0,
1577 sa_serial
[i
].io_base
);
1578 sysbus_connect_irq(sysbus_from_qdev(dev
), 0,
1579 qdev_get_gpio_in(s
->pic
, sa_serial
[i
].irq
));
1582 s
->ssp
= sysbus_create_varargs("strongarm-ssp", 0x80070000,
1583 qdev_get_gpio_in(s
->pic
, SA_PIC_SSP
), NULL
);
1584 s
->ssp_bus
= (SSIBus
*)qdev_get_child_bus(s
->ssp
, "ssi");
1589 static void strongarm_register_devices(void)
1591 sysbus_register_withprop(&strongarm_pic_info
);
1592 sysbus_register_withprop(&strongarm_rtc_sysbus_info
);
1593 sysbus_register_withprop(&strongarm_gpio_info
);
1594 sysbus_register_withprop(&strongarm_ppc_info
);
1595 sysbus_register_withprop(&strongarm_uart_info
);
1596 sysbus_register_withprop(&strongarm_ssp_info
);
1598 device_init(strongarm_register_devices
)