4 * Copyright (c) 2005 Samuel Tardieu
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "sh7750_regs.h"
28 #include "sh7750_regnames.h"
32 uint8_t length
; /* Number of characters in the FIFO */
33 uint8_t write_idx
; /* Index of first character to write */
34 uint8_t read_idx
; /* Index of first character to read */
39 typedef struct SH7750State
{
42 /* Peripheral frequency in Hz */
44 /* SDRAM controller */
46 /* First serial port */
47 CharDriverState
*serial1
;
54 uint8_t sctsr1_loaded
;
57 /* Second serial port */
58 CharDriverState
*serial2
;
65 fifo serial2_receive_fifo
;
66 fifo serial2_transmit_fifo
;
78 uint16_t portdira
; /* Cached */
79 uint16_t portpullupa
; /* Cached */
80 uint16_t portdirb
; /* Cached */
81 uint16_t portpullupb
; /* Cached */
84 uint16_t periph_pdtra
; /* Imposed by the peripherals */
85 uint16_t periph_portdira
; /* Direction seen from the peripherals */
86 uint16_t periph_pdtrb
; /* Imposed by the peripherals */
87 uint16_t periph_portdirb
; /* Direction seen from the peripherals */
88 sh7750_io_device
*devices
[NB_DEVICES
]; /* External peripherals */
93 /**********************************************************************
95 **********************************************************************/
97 /* XXXXX At this time, timer0 works in underflow only mode, that is
98 the value of tcnt0 is read at alarm computation time and cannot
99 be read back by the guest OS */
101 static void start_timer0(SH7750State
* s
)
103 uint64_t now
, next
, prescaler
;
105 if ((s
->tcr0
& 6) == 6) {
106 fprintf(stderr
, "rtc clock for timer 0 not supported\n");
110 if ((s
->tcr0
& 7) == 5) {
111 fprintf(stderr
, "timer 0 configuration not supported\n");
115 if ((s
->tcr0
& 4) == 4)
118 prescaler
= 4 << (s
->tcr0
& 3);
120 now
= qemu_get_clock(vm_clock
);
123 now
+ muldiv64(prescaler
* s
->tcnt0
, ticks_per_sec
,
127 fprintf(stderr
, "now=%016" PRIx64
", next=%016" PRIx64
"\n", now
, next
);
128 fprintf(stderr
, "timer will underflow in %f seconds\n",
129 (float) (next
- now
) / (float) ticks_per_sec
);
131 qemu_mod_timer(s
->timer0
, next
);
134 static void timer_start_changed(SH7750State
* s
)
136 if (s
->tstr
& SH7750_TSTR_STR0
) {
139 fprintf(stderr
, "timer 0 is stopped\n");
140 qemu_del_timer(s
->timer0
);
144 static void timer0_cb(void *opaque
)
146 SH7750State
*s
= opaque
;
148 s
->tcnt0
= (uint32_t) 0; /* XXXXX */
149 if (--s
->tcnt0
== (uint32_t) - 1) {
150 fprintf(stderr
, "timer 0 underflow\n");
152 s
->tcr0
|= SH7750_TCR_UNF
;
153 if (s
->tcr0
& SH7750_TCR_UNIE
) {
155 "interrupt generation for timer 0 not supported\n");
162 static void init_timers(SH7750State
* s
)
164 s
->tcor0
= 0xffffffff;
165 s
->tcnt0
= 0xffffffff;
166 s
->timer0
= qemu_new_timer(vm_clock
, &timer0_cb
, s
);
169 /**********************************************************************
171 **********************************************************************/
173 static int serial1_can_receive(void *opaque
)
175 SH7750State
*s
= opaque
;
177 return s
->scscr1
& SH7750_SCSCR_RE
;
180 static void serial1_receive_char(SH7750State
* s
, uint8_t c
)
182 if (s
->scssr1
& SH7750_SCSSR1_RDRF
) {
183 s
->scssr1
|= SH7750_SCSSR1_ORER
;
188 s
->scssr1
|= SH7750_SCSSR1_RDRF
;
191 static void serial1_receive(void *opaque
, const uint8_t * buf
, int size
)
193 SH7750State
*s
= opaque
;
196 for (i
= 0; i
< size
; i
++) {
197 serial1_receive_char(s
, buf
[i
]);
201 static void serial1_event(void *opaque
, int event
)
206 static void serial1_maybe_send(SH7750State
* s
)
210 if (s
->scssr1
& SH7750_SCSSR1_TDRE
)
213 s
->scssr1
|= SH7750_SCSSR1_TDRE
| SH7750_SCSSR1_TEND
;
214 if (s
->scscr1
& SH7750_SCSCR_TIE
) {
215 fprintf(stderr
, "interrupts for serial port 1 not implemented\n");
218 /* XXXXX Check for errors in write */
219 qemu_chr_write(s
->serial1
, &c
, 1);
222 static void serial1_change_scssr1(SH7750State
* s
, uint8_t mem_value
)
226 /* If transmit disable, TDRE and TEND stays up */
227 if ((s
->scscr1
& SH7750_SCSCR_TE
) == 0) {
228 mem_value
|= SH7750_SCSSR1_TDRE
| SH7750_SCSSR1_TEND
;
231 /* Only clear bits which have been read before and do not set any bit
233 new_flags
= s
->scssr1
& ~s
->scssr1_read
; /* Preserve unread flags */
234 new_flags
&= mem_value
| ~s
->scssr1_read
; /* Clear read flags */
236 s
->scssr1
= (new_flags
& 0xf8) | (mem_value
& 1);
237 s
->scssr1_read
&= mem_value
;
239 /* If TDRE has been cleared, TEND will also be cleared */
240 if ((s
->scssr1
& SH7750_SCSSR1_TDRE
) == 0) {
241 s
->scssr1
&= ~SH7750_SCSSR1_TEND
;
244 /* Check for transmission to start */
245 serial1_maybe_send(s
);
248 static void serial1_update_parameters(SH7750State
* s
)
250 QEMUSerialSetParams ssp
;
252 if (s
->scsmr1
& SH7750_SCSMR_CHR_7
)
256 if (s
->scsmr1
& SH7750_SCSMR_PE
) {
257 if (s
->scsmr1
& SH7750_SCSMR_PM_ODD
)
263 if (s
->scsmr1
& SH7750_SCSMR_STOP_2
)
267 fprintf(stderr
, "SCSMR1=%04x SCBRR1=%02x\n", s
->scsmr1
, s
->scbrr1
);
268 ssp
.speed
= s
->periph_freq
/
269 (32 * s
->scbrr1
* (1 << (2 * (s
->scsmr1
& 3)))) - 1;
270 fprintf(stderr
, "data bits=%d, stop bits=%d, parity=%c, speed=%d\n",
271 ssp
.data_bits
, ssp
.stop_bits
, ssp
.parity
, ssp
.speed
);
272 qemu_chr_ioctl(s
->serial1
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
275 static void scscr1_changed(SH7750State
* s
)
277 if (s
->scscr1
& (SH7750_SCSCR_TE
| SH7750_SCSCR_RE
)) {
279 fprintf(stderr
, "serial port 1 not bound to anything\n");
282 serial1_update_parameters(s
);
284 if ((s
->scscr1
& SH7750_SCSCR_RE
) == 0) {
285 s
->scssr1
|= SH7750_SCSSR1_TDRE
;
289 static void init_serial1(SH7750State
* s
, int serial_nb
)
291 CharDriverState
*chr
;
294 chr
= serial_hds
[serial_nb
];
297 "no serial port associated to SH7750 first serial port\n");
302 qemu_chr_add_handlers(chr
, serial1_can_receive
,
303 serial1_receive
, serial1_event
, s
);
306 /**********************************************************************
308 **********************************************************************/
310 static int serial2_can_receive(void *opaque
)
312 SH7750State
*s
= opaque
;
313 static uint8_t max_fifo_size
[] = { 15, 1, 4, 6, 8, 10, 12, 14 };
315 return s
->serial2_receive_fifo
.length
<
316 max_fifo_size
[(s
->scfcr2
>> 9) & 7];
319 static void serial2_adjust_receive_flags(SH7750State
* s
)
321 static uint8_t max_fifo_size
[] = { 1, 4, 8, 14 };
323 /* XXXXX Add interrupt generation */
324 if (s
->serial2_receive_fifo
.length
>=
325 max_fifo_size
[(s
->scfcr2
>> 7) & 3]) {
326 s
->scfsr2
|= SH7750_SCFSR2_RDF
;
327 s
->scfsr2
&= ~SH7750_SCFSR2_DR
;
329 s
->scfsr2
&= ~SH7750_SCFSR2_RDF
;
330 if (s
->serial2_receive_fifo
.length
> 0)
331 s
->scfsr2
|= SH7750_SCFSR2_DR
;
333 s
->scfsr2
&= ~SH7750_SCFSR2_DR
;
337 static void serial2_append_char(SH7750State
* s
, uint8_t c
)
339 if (s
->serial2_receive_fifo
.length
== 16) {
341 s
->sclsr2
|= SH7750_SCLSR2_ORER
;
345 s
->serial2_receive_fifo
.data
[s
->serial2_receive_fifo
.write_idx
++] = c
;
346 s
->serial2_receive_fifo
.length
++;
347 serial2_adjust_receive_flags(s
);
350 static void serial2_receive(void *opaque
, const uint8_t * buf
, int size
)
352 SH7750State
*s
= opaque
;
355 for (i
= 0; i
< size
; i
++)
356 serial2_append_char(s
, buf
[i
]);
359 static void serial2_event(void *opaque
, int event
)
365 static void serial2_update_parameters(SH7750State
* s
)
367 QEMUSerialSetParams ssp
;
369 if (s
->scsmr2
& SH7750_SCSMR_CHR_7
)
373 if (s
->scsmr2
& SH7750_SCSMR_PE
) {
374 if (s
->scsmr2
& SH7750_SCSMR_PM_ODD
)
380 if (s
->scsmr2
& SH7750_SCSMR_STOP_2
)
384 fprintf(stderr
, "SCSMR2=%04x SCBRR2=%02x\n", s
->scsmr2
, s
->scbrr2
);
385 ssp
.speed
= s
->periph_freq
/
386 (32 * s
->scbrr2
* (1 << (2 * (s
->scsmr2
& 3)))) - 1;
387 fprintf(stderr
, "data bits=%d, stop bits=%d, parity=%c, speed=%d\n",
388 ssp
.data_bits
, ssp
.stop_bits
, ssp
.parity
, ssp
.speed
);
389 qemu_chr_ioctl(s
->serial2
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
392 static void scscr2_changed(SH7750State
* s
)
394 if (s
->scscr2
& (SH7750_SCSCR_TE
| SH7750_SCSCR_RE
)) {
396 fprintf(stderr
, "serial port 2 not bound to anything\n");
399 serial2_update_parameters(s
);
403 static void init_serial2(SH7750State
* s
, int serial_nb
)
405 CharDriverState
*chr
;
409 chr
= serial_hds
[serial_nb
];
412 "no serial port associated to SH7750 second serial port\n");
417 qemu_chr_add_handlers(chr
, serial2_can_receive
,
418 serial2_receive
, serial1_event
, s
);
421 static void init_serial_ports(SH7750State
* s
)
427 /**********************************************************************
429 **********************************************************************/
431 int sh7750_register_io_device(SH7750State
* s
, sh7750_io_device
* device
)
435 for (i
= 0; i
< NB_DEVICES
; i
++) {
436 if (s
->devices
[i
] == NULL
) {
437 s
->devices
[i
] = device
;
444 static uint16_t portdir(uint32_t v
)
446 #define EVENPORTMASK(n) ((v & (1<<((n)<<1))) >> (n))
448 EVENPORTMASK(15) | EVENPORTMASK(14) | EVENPORTMASK(13) |
449 EVENPORTMASK(12) | EVENPORTMASK(11) | EVENPORTMASK(10) |
450 EVENPORTMASK(9) | EVENPORTMASK(8) | EVENPORTMASK(7) |
451 EVENPORTMASK(6) | EVENPORTMASK(5) | EVENPORTMASK(4) |
452 EVENPORTMASK(3) | EVENPORTMASK(2) | EVENPORTMASK(1) |
456 static uint16_t portpullup(uint32_t v
)
458 #define ODDPORTMASK(n) ((v & (1<<(((n)<<1)+1))) >> (n))
460 ODDPORTMASK(15) | ODDPORTMASK(14) | ODDPORTMASK(13) |
461 ODDPORTMASK(12) | ODDPORTMASK(11) | ODDPORTMASK(10) |
462 ODDPORTMASK(9) | ODDPORTMASK(8) | ODDPORTMASK(7) | ODDPORTMASK(6) |
463 ODDPORTMASK(5) | ODDPORTMASK(4) | ODDPORTMASK(3) | ODDPORTMASK(2) |
464 ODDPORTMASK(1) | ODDPORTMASK(0);
467 static uint16_t porta_lines(SH7750State
* s
)
469 return (s
->portdira
& s
->pdtra
) | /* CPU */
470 (s
->periph_portdira
& s
->periph_pdtra
) | /* Peripherals */
471 (~(s
->portdira
| s
->periph_portdira
) & s
->portpullupa
); /* Pullups */
474 static uint16_t portb_lines(SH7750State
* s
)
476 return (s
->portdirb
& s
->pdtrb
) | /* CPU */
477 (s
->periph_portdirb
& s
->periph_pdtrb
) | /* Peripherals */
478 (~(s
->portdirb
| s
->periph_portdirb
) & s
->portpullupb
); /* Pullups */
481 static void gen_port_interrupts(SH7750State
* s
)
483 /* XXXXX interrupts not generated */
486 static void porta_changed(SH7750State
* s
, uint16_t prev
)
488 uint16_t currenta
, changes
;
492 fprintf(stderr
, "porta changed from 0x%04x to 0x%04x\n",
493 prev
, porta_lines(s
));
494 fprintf(stderr
, "pdtra=0x%04x, pctra=0x%08x\n", s
->pdtra
, s
->pctra
);
496 currenta
= porta_lines(s
);
497 if (currenta
== prev
)
499 changes
= currenta
^ prev
;
501 for (i
= 0; i
< NB_DEVICES
; i
++) {
502 if (s
->devices
[i
] && (s
->devices
[i
]->portamask_trigger
& changes
)) {
503 r
|= s
->devices
[i
]->port_change_cb(currenta
, portb_lines(s
),
507 &s
->periph_portdirb
);
512 gen_port_interrupts(s
);
515 static void portb_changed(SH7750State
* s
, uint16_t prev
)
517 uint16_t currentb
, changes
;
520 currentb
= portb_lines(s
);
521 if (currentb
== prev
)
523 changes
= currentb
^ prev
;
525 for (i
= 0; i
< NB_DEVICES
; i
++) {
526 if (s
->devices
[i
] && (s
->devices
[i
]->portbmask_trigger
& changes
)) {
527 r
|= s
->devices
[i
]->port_change_cb(portb_lines(s
), currentb
,
531 &s
->periph_portdirb
);
536 gen_port_interrupts(s
);
539 /**********************************************************************
541 **********************************************************************/
543 static void error_access(const char *kind
, target_phys_addr_t addr
)
545 fprintf(stderr
, "%s to %s (0x%08x) not supported\n",
546 kind
, regname(addr
), addr
);
549 static void ignore_access(const char *kind
, target_phys_addr_t addr
)
551 fprintf(stderr
, "%s to %s (0x%08x) ignored\n",
552 kind
, regname(addr
), addr
);
555 static uint32_t sh7750_mem_readb(void *opaque
, target_phys_addr_t addr
)
557 SH7750State
*s
= opaque
;
561 case SH7750_SCSSR1_A7
:
565 case SH7750_SCRDR1_A7
:
566 s
->scssr1
&= ~SH7750_SCSSR1_RDRF
;
569 error_access("byte read", addr
);
574 static uint32_t sh7750_mem_readw(void *opaque
, target_phys_addr_t addr
)
576 SH7750State
*s
= opaque
;
582 "Read access to refresh count register, incrementing\n");
586 case SH7750_SCLSR2_A7
:
587 /* Read and clear overflow bit */
591 case SH7750_SCSFR2_A7
:
593 case SH7750_PDTRA_A7
:
594 return porta_lines(s
);
595 case SH7750_PDTRB_A7
:
596 return portb_lines(s
);
598 error_access("word read", addr
);
603 static uint32_t sh7750_mem_readl(void *opaque
, target_phys_addr_t addr
)
605 SH7750State
*s
= opaque
;
608 case SH7750_MMUCR_A7
:
609 return s
->cpu
->mmucr
;
620 case SH7750_EXPEVT_A7
:
621 return s
->cpu
->expevt
;
622 case SH7750_INTEVT_A7
:
623 return s
->cpu
->intevt
;
626 case 0x1f000030: /* Processor version PVR */
627 return 0x00050000; /* SH7750R */
628 case 0x1f000040: /* Processor version CVR */
629 return 0x00110000; /* Minimum caches */
630 case 0x1f000044: /* Processor version PRR */
631 return 0x00000100; /* SH7750R */
633 error_access("long read", addr
);
638 static void sh7750_mem_writeb(void *opaque
, target_phys_addr_t addr
,
641 SH7750State
*s
= opaque
;
644 /* PRECHARGE ? XXXXX */
645 case SH7750_PRECHARGE0_A7
:
646 case SH7750_PRECHARGE1_A7
:
647 ignore_access("byte write", addr
);
649 case SH7750_SCBRR2_A7
:
650 s
->scbrr2
= mem_value
;
654 timer_start_changed(s
);
656 case SH7750_SCSCR1_A7
:
657 s
->scscr1
= mem_value
;
660 case SH7750_SCSMR1_A7
:
661 s
->scsmr1
= mem_value
;
663 case SH7750_SCBRR1_A7
:
664 s
->scbrr1
= mem_value
;
666 case SH7750_SCTDR1_A7
:
667 s
->scssr1
&= ~SH7750_SCSSR1_TEND
;
668 s
->sctdr1
= mem_value
;
670 case SH7750_SCSSR1_A7
:
671 serial1_change_scssr1(s
, mem_value
);
674 error_access("byte write", addr
);
679 static void sh7750_mem_writew(void *opaque
, target_phys_addr_t addr
,
682 SH7750State
*s
= opaque
;
686 /* SDRAM controller */
687 case SH7750_SCBRR1_A7
:
688 case SH7750_SCBRR2_A7
:
691 case SH7750_RTCOR_A7
:
692 case SH7750_RTCNT_A7
:
693 case SH7750_RTCSR_A7
:
694 ignore_access("word write", addr
);
697 case SH7750_PDTRA_A7
:
698 temp
= porta_lines(s
);
699 s
->pdtra
= mem_value
;
700 porta_changed(s
, temp
);
702 case SH7750_PDTRB_A7
:
703 temp
= portb_lines(s
);
704 s
->pdtrb
= mem_value
;
705 portb_changed(s
, temp
);
708 fprintf(stderr
, "Write access to refresh count register\n");
711 case SH7750_SCLSR2_A7
:
712 s
->sclsr2
= mem_value
;
714 case SH7750_SCSCR2_A7
:
715 s
->scscr2
= mem_value
;
718 case SH7750_SCFCR2_A7
:
719 s
->scfcr2
= mem_value
;
721 case SH7750_SCSMR2_A7
:
722 s
->scsmr2
= mem_value
;
727 case SH7750_GPIOIC_A7
:
728 s
->gpioic
= mem_value
;
729 if (mem_value
!= 0) {
730 fprintf(stderr
, "I/O interrupts not implemented\n");
735 error_access("word write", addr
);
740 static void sh7750_mem_writel(void *opaque
, target_phys_addr_t addr
,
743 SH7750State
*s
= opaque
;
747 /* SDRAM controller */
754 ignore_access("long write", addr
);
757 case SH7750_PCTRA_A7
:
758 temp
= porta_lines(s
);
759 s
->pctra
= mem_value
;
760 s
->portdira
= portdir(mem_value
);
761 s
->portpullupa
= portpullup(mem_value
);
762 porta_changed(s
, temp
);
764 case SH7750_PCTRB_A7
:
765 temp
= portb_lines(s
);
766 s
->pctrb
= mem_value
;
767 s
->portdirb
= portdir(mem_value
);
768 s
->portpullupb
= portpullup(mem_value
);
769 portb_changed(s
, temp
);
771 case SH7750_TCNT0_A7
:
772 s
->tcnt0
= mem_value
& 0xf;
774 case SH7750_MMUCR_A7
:
775 s
->cpu
->mmucr
= mem_value
;
778 s
->cpu
->pteh
= mem_value
;
781 s
->cpu
->ptel
= mem_value
;
784 s
->cpu
->ttb
= mem_value
;
787 s
->cpu
->tea
= mem_value
;
790 s
->cpu
->tra
= mem_value
& 0x000007ff;
792 case SH7750_EXPEVT_A7
:
793 s
->cpu
->expevt
= mem_value
& 0x000007ff;
795 case SH7750_INTEVT_A7
:
796 s
->cpu
->intevt
= mem_value
& 0x000007ff;
802 error_access("long write", addr
);
807 static CPUReadMemoryFunc
*sh7750_mem_read
[] = {
813 static CPUWriteMemoryFunc
*sh7750_mem_write
[] = {
819 SH7750State
*sh7750_init(CPUSH4State
* cpu
)
822 int sh7750_io_memory
;
824 s
= qemu_mallocz(sizeof(SH7750State
));
826 s
->periph_freq
= 60000000; /* 60MHz */
827 sh7750_io_memory
= cpu_register_io_memory(0,
829 sh7750_mem_write
, s
);
830 cpu_register_physical_memory(0x1c000000, 0x04000000, sh7750_io_memory
);
832 init_serial_ports(s
);