#ifdef CONS_SCIF
#include <lib/libsa/stand.h>
#include <lib/libkern/libkern.h>
#include <sh3/scifreg.h>
#include <machine/cpu.h>
#include "boot.h"
#include "scif.h"
#define BOOT_PCLOCK 40000000
#if defined(SH3) && defined(SH4)
#error "mmeye port don't support SH3,SH4 common boot."
#elif defined(SH3)
#error "don't support SH3 common boot."
#elif defined(SH4)
#define CPU_IS_SH4 1
#endif
void *
scif_init(int speed)
{
#define divrnd(n, q) (((n)*2/(q)+1)/2)
SHREG_SCSCR2 = 0x00;
SHREG_SCFCR2 = SCFCR2_TFRST | SCFCR2_RFRST;
SHREG_SCSMR2 = 0x00;
SHREG_SCBRR2 = divrnd(BOOT_PCLOCK, 32 * speed) - 1;
delay(2000);
SHREG_SCFCR2 = FIFO_RCV_TRIGGER_14 | FIFO_XMT_TRIGGER_1;
SHREG_SCSCR2 = SCSCR2_TE | SCSCR2_RE;
SHREG_SCSSR2 = (SHREG_SCSSR2 & SCSSR2_TDFE);
return NULL;
}
void
scif_putc(int c)
{
while ((SHREG_SCFDR2 & SCFDR2_TXCNT) == SCFDR2_TXF_FULL)
continue;
SHREG_SCFTDR2 = c;
SHREG_SCSSR2 = (SHREG_SCSSR2 & ~(SCSSR2_TDFE | SCSSR2_TEND));
}
int
scif_getc(void)
{
unsigned char c, err_c;
#ifdef SH4
unsigned short err_c2 = 0;
#endif
for (;;) {
while ((SHREG_SCFDR2 & SCFDR2_RECVCNT) == 0)
continue;
c = SHREG_SCFRDR2;
err_c = SHREG_SCSSR2;
SHREG_SCSSR2 = (SHREG_SCSSR2 &
~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR));
#ifdef SH4
if (CPU_IS_SH4) {
err_c2 = SHREG_SCLSR2;
SHREG_SCLSR2 = (SHREG_SCLSR2 & ~SCLSR2_ORER);
}
#endif
if ((err_c &
(SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) == 0) {
#ifdef SH4
if (CPU_IS_SH4 && ((err_c2 & SCLSR2_ORER) == 0))
#endif
return c;
}
}
}
int
scif_scankbd(void)
{
unsigned char c, err_c;
#ifdef SH4
unsigned short err_c2 = 0;
#endif
for (;;) {
if ((SHREG_SCFDR2 & SCFDR2_RECVCNT) == 0)
return -1;
c = SHREG_SCFRDR2;
err_c = SHREG_SCSSR2;
SHREG_SCSSR2 = (SHREG_SCSSR2 &
~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR));
#ifdef SH4
if (CPU_IS_SH4) {
err_c2 = SHREG_SCLSR2;
SHREG_SCLSR2 = (SHREG_SCLSR2 & ~SCLSR2_ORER);
}
#endif
if ((err_c &
(SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) == 0) {
#ifdef SH4
if (CPU_IS_SH4 && ((err_c2 & SCLSR2_ORER) == 0))
#endif
return c;
}
}
}
#endif