scif.c revision 1.1.4.2 1 1.1.4.2 rmind /* $NetBSD: scif.c,v 1.1.4.2 2011/03/05 20:51:16 rmind Exp $ */
2 1.1.4.2 rmind /*
3 1.1.4.2 rmind * Copyright (c) 2011 KIYOHARA Takashi
4 1.1.4.2 rmind * All rights reserved.
5 1.1.4.2 rmind *
6 1.1.4.2 rmind * Redistribution and use in source and binary forms, with or without
7 1.1.4.2 rmind * modification, are permitted provided that the following conditions
8 1.1.4.2 rmind * are met:
9 1.1.4.2 rmind * 1. Redistributions of source code must retain the above copyright
10 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer.
11 1.1.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer in the
13 1.1.4.2 rmind * documentation and/or other materials provided with the distribution.
14 1.1.4.2 rmind *
15 1.1.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1.4.2 rmind * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1.4.2 rmind * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1.4.2 rmind * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1.4.2 rmind * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1.4.2 rmind * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1.4.2 rmind * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1.4.2 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1.4.2 rmind * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1.4.2 rmind * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
26 1.1.4.2 rmind */
27 1.1.4.2 rmind
28 1.1.4.2 rmind #ifdef CONS_SCIF
29 1.1.4.2 rmind
30 1.1.4.2 rmind #include <lib/libsa/stand.h>
31 1.1.4.2 rmind #include <lib/libkern/libkern.h>
32 1.1.4.2 rmind
33 1.1.4.2 rmind #include <sh3/scifreg.h>
34 1.1.4.2 rmind
35 1.1.4.2 rmind #include <machine/cpu.h>
36 1.1.4.2 rmind
37 1.1.4.2 rmind #include "boot.h"
38 1.1.4.2 rmind #include "scif.h"
39 1.1.4.2 rmind
40 1.1.4.2 rmind #define BOOT_PCLOCK 40000000
41 1.1.4.2 rmind
42 1.1.4.2 rmind #if defined(SH3) && defined(SH4)
43 1.1.4.2 rmind #error "mmeye port don't support SH3,SH4 common boot."
44 1.1.4.2 rmind #elif defined(SH3)
45 1.1.4.2 rmind #error "don't support SH3 common boot."
46 1.1.4.2 rmind #elif defined(SH4)
47 1.1.4.2 rmind #define CPU_IS_SH4 1
48 1.1.4.2 rmind #endif
49 1.1.4.2 rmind
50 1.1.4.2 rmind void *
51 1.1.4.2 rmind scif_init(int speed)
52 1.1.4.2 rmind {
53 1.1.4.2 rmind
54 1.1.4.2 rmind #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
55 1.1.4.2 rmind
56 1.1.4.2 rmind /* Initialize SCR */
57 1.1.4.2 rmind SHREG_SCSCR2 = 0x00;
58 1.1.4.2 rmind
59 1.1.4.2 rmind SHREG_SCFCR2 = SCFCR2_TFRST | SCFCR2_RFRST;
60 1.1.4.2 rmind
61 1.1.4.2 rmind /* Serial Mode Register */
62 1.1.4.2 rmind SHREG_SCSMR2 = 0x00; /* 8bit,NonParity,Even,1Stop */
63 1.1.4.2 rmind
64 1.1.4.2 rmind /* Bit Rate Register */
65 1.1.4.2 rmind SHREG_SCBRR2 = divrnd(BOOT_PCLOCK, 32 * speed) - 1;
66 1.1.4.2 rmind
67 1.1.4.2 rmind /*
68 1.1.4.2 rmind * wait 2m Sec, because Send/Recv must begin 1 bit period after
69 1.1.4.2 rmind * BRR is set.
70 1.1.4.2 rmind */
71 1.1.4.2 rmind delay(2000);
72 1.1.4.2 rmind
73 1.1.4.2 rmind SHREG_SCFCR2 = FIFO_RCV_TRIGGER_14 | FIFO_XMT_TRIGGER_1;
74 1.1.4.2 rmind
75 1.1.4.2 rmind /* Send permission, Receive permission ON */
76 1.1.4.2 rmind SHREG_SCSCR2 = SCSCR2_TE | SCSCR2_RE;
77 1.1.4.2 rmind
78 1.1.4.2 rmind /* Serial Status Register */
79 1.1.4.2 rmind SHREG_SCSSR2 = (SHREG_SCSSR2 & SCSSR2_TDFE); /* Clear Status */
80 1.1.4.2 rmind
81 1.1.4.2 rmind return NULL;
82 1.1.4.2 rmind }
83 1.1.4.2 rmind
84 1.1.4.2 rmind void
85 1.1.4.2 rmind scif_putc(int c)
86 1.1.4.2 rmind {
87 1.1.4.2 rmind
88 1.1.4.2 rmind /* wait for ready */
89 1.1.4.2 rmind while ((SHREG_SCFDR2 & SCFDR2_TXCNT) == SCFDR2_TXF_FULL)
90 1.1.4.2 rmind continue;
91 1.1.4.2 rmind
92 1.1.4.2 rmind /* write send data to send register */
93 1.1.4.2 rmind SHREG_SCFTDR2 = c;
94 1.1.4.2 rmind
95 1.1.4.2 rmind /* clear ready flag */
96 1.1.4.2 rmind SHREG_SCSSR2 = (SHREG_SCSSR2 & ~(SCSSR2_TDFE | SCSSR2_TEND));
97 1.1.4.2 rmind }
98 1.1.4.2 rmind
99 1.1.4.2 rmind int
100 1.1.4.2 rmind scif_getc(void)
101 1.1.4.2 rmind {
102 1.1.4.2 rmind unsigned char c, err_c;
103 1.1.4.2 rmind #ifdef SH4
104 1.1.4.2 rmind unsigned short err_c2 = 0;
105 1.1.4.2 rmind #endif
106 1.1.4.2 rmind
107 1.1.4.2 rmind for (;;) {
108 1.1.4.2 rmind /* wait for ready */
109 1.1.4.2 rmind while ((SHREG_SCFDR2 & SCFDR2_RECVCNT) == 0)
110 1.1.4.2 rmind continue;
111 1.1.4.2 rmind
112 1.1.4.2 rmind c = SHREG_SCFRDR2;
113 1.1.4.2 rmind err_c = SHREG_SCSSR2;
114 1.1.4.2 rmind SHREG_SCSSR2 = (SHREG_SCSSR2 &
115 1.1.4.2 rmind ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR));
116 1.1.4.2 rmind #ifdef SH4
117 1.1.4.2 rmind if (CPU_IS_SH4) {
118 1.1.4.2 rmind err_c2 = SHREG_SCLSR2;
119 1.1.4.2 rmind SHREG_SCLSR2 = (SHREG_SCLSR2 & ~SCLSR2_ORER);
120 1.1.4.2 rmind }
121 1.1.4.2 rmind #endif
122 1.1.4.2 rmind if ((err_c &
123 1.1.4.2 rmind (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) == 0) {
124 1.1.4.2 rmind #ifdef SH4
125 1.1.4.2 rmind if (CPU_IS_SH4 && ((err_c2 & SCLSR2_ORER) == 0))
126 1.1.4.2 rmind #endif
127 1.1.4.2 rmind return c;
128 1.1.4.2 rmind }
129 1.1.4.2 rmind }
130 1.1.4.2 rmind }
131 1.1.4.2 rmind
132 1.1.4.2 rmind int
133 1.1.4.2 rmind scif_scankbd(void)
134 1.1.4.2 rmind {
135 1.1.4.2 rmind unsigned char c, err_c;
136 1.1.4.2 rmind #ifdef SH4
137 1.1.4.2 rmind unsigned short err_c2 = 0;
138 1.1.4.2 rmind #endif
139 1.1.4.2 rmind
140 1.1.4.2 rmind for (;;) {
141 1.1.4.2 rmind /* wait for ready */
142 1.1.4.2 rmind if ((SHREG_SCFDR2 & SCFDR2_RECVCNT) == 0)
143 1.1.4.2 rmind return -1;
144 1.1.4.2 rmind
145 1.1.4.2 rmind c = SHREG_SCFRDR2;
146 1.1.4.2 rmind err_c = SHREG_SCSSR2;
147 1.1.4.2 rmind SHREG_SCSSR2 = (SHREG_SCSSR2 &
148 1.1.4.2 rmind ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR));
149 1.1.4.2 rmind #ifdef SH4
150 1.1.4.2 rmind if (CPU_IS_SH4) {
151 1.1.4.2 rmind err_c2 = SHREG_SCLSR2;
152 1.1.4.2 rmind SHREG_SCLSR2 = (SHREG_SCLSR2 & ~SCLSR2_ORER);
153 1.1.4.2 rmind }
154 1.1.4.2 rmind #endif
155 1.1.4.2 rmind if ((err_c &
156 1.1.4.2 rmind (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) == 0) {
157 1.1.4.2 rmind #ifdef SH4
158 1.1.4.2 rmind if (CPU_IS_SH4 && ((err_c2 & SCLSR2_ORER) == 0))
159 1.1.4.2 rmind #endif
160 1.1.4.2 rmind return c;
161 1.1.4.2 rmind }
162 1.1.4.2 rmind }
163 1.1.4.2 rmind }
164 1.1.4.2 rmind #endif /* CONS_SCIF */
165