scifcons.c revision 1.1 1 1.1 uwe /* $NetBSD: scifcons.c,v 1.1 2006/09/01 21:26:18 uwe Exp $ */
2 1.1 uwe /* NetBSD: scif.c,v 1.38 2004/12/13 02:14:13 chs Exp */
3 1.1 uwe
4 1.1 uwe /*-
5 1.1 uwe * Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu. All rights reserved.
6 1.1 uwe *
7 1.1 uwe * Redistribution and use in source and binary forms, with or without
8 1.1 uwe * modification, are permitted provided that the following conditions
9 1.1 uwe * are met:
10 1.1 uwe * 1. Redistributions of source code must retain the above copyright
11 1.1 uwe * notice, this list of conditions and the following disclaimer.
12 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uwe * notice, this list of conditions and the following disclaimer in the
14 1.1 uwe * documentation and/or other materials provided with the distribution.
15 1.1 uwe * 3. The name of the author may not be used to endorse or promote products
16 1.1 uwe * derived from this software without specific prior written permission.
17 1.1 uwe *
18 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 uwe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 uwe * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 uwe * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 uwe * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 uwe * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 uwe * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 uwe * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 uwe * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 1.1 uwe * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 uwe */
29 1.1 uwe
30 1.1 uwe /*-
31 1.1 uwe * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
32 1.1 uwe * All rights reserved.
33 1.1 uwe *
34 1.1 uwe * This code is derived from software contributed to The NetBSD Foundation
35 1.1 uwe * by Charles M. Hannum.
36 1.1 uwe *
37 1.1 uwe * Redistribution and use in source and binary forms, with or without
38 1.1 uwe * modification, are permitted provided that the following conditions
39 1.1 uwe * are met:
40 1.1 uwe * 1. Redistributions of source code must retain the above copyright
41 1.1 uwe * notice, this list of conditions and the following disclaimer.
42 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 uwe * notice, this list of conditions and the following disclaimer in the
44 1.1 uwe * documentation and/or other materials provided with the distribution.
45 1.1 uwe * 3. All advertising materials mentioning features or use of this software
46 1.1 uwe * must display the following acknowledgement:
47 1.1 uwe * This product includes software developed by the NetBSD
48 1.1 uwe * Foundation, Inc. and its contributors.
49 1.1 uwe * 4. Neither the name of The NetBSD Foundation nor the names of its
50 1.1 uwe * contributors may be used to endorse or promote products derived
51 1.1 uwe * from this software without specific prior written permission.
52 1.1 uwe *
53 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54 1.1 uwe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 1.1 uwe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56 1.1 uwe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57 1.1 uwe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58 1.1 uwe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59 1.1 uwe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60 1.1 uwe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61 1.1 uwe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62 1.1 uwe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63 1.1 uwe * POSSIBILITY OF SUCH DAMAGE.
64 1.1 uwe */
65 1.1 uwe
66 1.1 uwe /*
67 1.1 uwe * Copyright (c) 1991 The Regents of the University of California.
68 1.1 uwe * All rights reserved.
69 1.1 uwe *
70 1.1 uwe * Redistribution and use in source and binary forms, with or without
71 1.1 uwe * modification, are permitted provided that the following conditions
72 1.1 uwe * are met:
73 1.1 uwe * 1. Redistributions of source code must retain the above copyright
74 1.1 uwe * notice, this list of conditions and the following disclaimer.
75 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
76 1.1 uwe * notice, this list of conditions and the following disclaimer in the
77 1.1 uwe * documentation and/or other materials provided with the distribution.
78 1.1 uwe * 3. Neither the name of the University nor the names of its contributors
79 1.1 uwe * may be used to endorse or promote products derived from this software
80 1.1 uwe * without specific prior written permission.
81 1.1 uwe *
82 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 1.1 uwe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 1.1 uwe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 1.1 uwe * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 1.1 uwe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 1.1 uwe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 1.1 uwe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 1.1 uwe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 1.1 uwe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 1.1 uwe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 1.1 uwe * SUCH DAMAGE.
93 1.1 uwe *
94 1.1 uwe * @(#)com.c 7.5 (Berkeley) 5/16/91
95 1.1 uwe */
96 1.1 uwe
97 1.1 uwe /*
98 1.1 uwe * SH internal serial driver
99 1.1 uwe *
100 1.1 uwe * This code is derived from both z8530tty.c and com.c
101 1.1 uwe */
102 1.1 uwe
103 1.1 uwe #include <lib/libsa/stand.h>
104 1.1 uwe #include <lib/libkern/libkern.h>
105 1.1 uwe
106 1.1 uwe #include <sh3/scifreg.h>
107 1.1 uwe
108 1.1 uwe #include "boot.h"
109 1.1 uwe #include "cons.h"
110 1.1 uwe
111 1.1 uwe #define scif_smr_read() SHREG_SCSMR2
112 1.1 uwe #define scif_smr_write(v) (SHREG_SCSMR2 = (v))
113 1.1 uwe
114 1.1 uwe #define scif_brr_read() SHREG_SCBRR2
115 1.1 uwe #define scif_brr_write(v) (SHREG_SCBRR2 = (v))
116 1.1 uwe
117 1.1 uwe #define scif_scr_read() SHREG_SCSCR2
118 1.1 uwe #define scif_scr_write(v) (SHREG_SCSCR2 = (v))
119 1.1 uwe
120 1.1 uwe #define scif_ftdr_write(v) (SHREG_SCFTDR2 = (v))
121 1.1 uwe
122 1.1 uwe #define scif_ssr_read() SHREG_SCSSR2
123 1.1 uwe #define scif_ssr_write(v) (SHREG_SCSSR2 = (v))
124 1.1 uwe
125 1.1 uwe #define scif_frdr_read() SHREG_SCFRDR2
126 1.1 uwe
127 1.1 uwe #define scif_fcr_read() SHREG_SCFCR2
128 1.1 uwe #define scif_fcr_write(v) (SHREG_SCFCR2 = (v))
129 1.1 uwe
130 1.1 uwe #define scif_fdr_read() SHREG_SCFDR2
131 1.1 uwe
132 1.1 uwe #define scif_sptr_read() SHREG_SCSPTR2
133 1.1 uwe #define scif_sptr_write(v) (SHREG_SCSPTR2 = (v))
134 1.1 uwe
135 1.1 uwe #define scif_lsr_read() SHREG_SCLSR2
136 1.1 uwe #define scif_lsr_write(v) (SHREG_SCLSR2 = (v))
137 1.1 uwe
138 1.1 uwe #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
139 1.1 uwe
140 1.1 uwe #define SERBUFSIZE 16
141 1.1 uwe static u_char serbuf[SERBUFSIZE];
142 1.1 uwe static int serbuf_read = 0;
143 1.1 uwe static int serbuf_write = 0;
144 1.1 uwe
145 1.1 uwe void
146 1.1 uwe scif_init(unsigned int bps)
147 1.1 uwe {
148 1.1 uwe
149 1.1 uwe serbuf_read = 0;
150 1.1 uwe serbuf_write = 0;
151 1.1 uwe
152 1.1 uwe /* Initialize SCR */
153 1.1 uwe scif_scr_write(0x00);
154 1.1 uwe
155 1.1 uwe /* Clear FIFO */
156 1.1 uwe scif_fcr_write(SCFCR2_TFRST | SCFCR2_RFRST);
157 1.1 uwe
158 1.1 uwe /* Serial Mode Register */
159 1.1 uwe scif_smr_write(0x00); /* 8bit,NonParity,Even,1Stop */
160 1.1 uwe
161 1.1 uwe /* Bit Rate Register */
162 1.1 uwe scif_brr_write(divrnd(PCLOCK, 32 * bps) - 1);
163 1.1 uwe
164 1.1 uwe delay(100);
165 1.1 uwe
166 1.1 uwe scif_sptr_write(SCSPTR2_RTSIO);
167 1.1 uwe
168 1.1 uwe scif_fcr_write(FIFO_RCV_TRIGGER_1 | FIFO_XMT_TRIGGER_8);
169 1.1 uwe
170 1.1 uwe /* Send permission, Receive permission ON */
171 1.1 uwe scif_scr_write(SCSCR2_TE | SCSCR2_RE);
172 1.1 uwe
173 1.1 uwe /* Serial Status Register */
174 1.1 uwe scif_ssr_write(scif_ssr_read() & SCSSR2_TDFE); /* Clear Status */
175 1.1 uwe }
176 1.1 uwe
177 1.1 uwe int
178 1.1 uwe scif_getc(void)
179 1.1 uwe {
180 1.1 uwe unsigned char c, err_c;
181 1.1 uwe unsigned short err_c2;
182 1.1 uwe
183 1.1 uwe if (serbuf_read != serbuf_write) {
184 1.1 uwe c = serbuf[serbuf_read];
185 1.1 uwe serbuf_read = (serbuf_read + 1) % SERBUFSIZE;
186 1.1 uwe return (c);
187 1.1 uwe }
188 1.1 uwe
189 1.1 uwe for (;;) {
190 1.1 uwe /* wait for ready */
191 1.1 uwe while ((scif_fdr_read() & SCFDR2_RECVCNT) == 0)
192 1.1 uwe continue;
193 1.1 uwe
194 1.1 uwe c = scif_frdr_read();
195 1.1 uwe err_c = scif_ssr_read();
196 1.1 uwe scif_ssr_write(scif_ssr_read()
197 1.1 uwe & ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR));
198 1.1 uwe
199 1.1 uwe err_c2 = scif_lsr_read();
200 1.1 uwe scif_lsr_write(scif_lsr_read() & ~SCLSR2_ORER);
201 1.1 uwe
202 1.1 uwe if ((err_c & (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER
203 1.1 uwe | SCSSR2_PER)) == 0) {
204 1.1 uwe if ((err_c2 & SCLSR2_ORER) == 0) {
205 1.1 uwe return (c);
206 1.1 uwe }
207 1.1 uwe }
208 1.1 uwe }
209 1.1 uwe }
210 1.1 uwe
211 1.1 uwe void
212 1.1 uwe scif_putc(int c)
213 1.1 uwe {
214 1.1 uwe
215 1.1 uwe /* wait for ready */
216 1.1 uwe while ((scif_fdr_read() & SCFDR2_TXCNT) == SCFDR2_TXF_FULL)
217 1.1 uwe continue;
218 1.1 uwe
219 1.1 uwe /* write send data to send register */
220 1.1 uwe scif_ftdr_write(c);
221 1.1 uwe
222 1.1 uwe /* clear ready flag */
223 1.1 uwe scif_ssr_write(scif_ssr_read() & ~(SCSSR2_TDFE | SCSSR2_TEND));
224 1.1 uwe }
225 1.1 uwe
226 1.1 uwe int
227 1.1 uwe scif_status(void)
228 1.1 uwe {
229 1.1 uwe unsigned char c, err_c;
230 1.1 uwe unsigned short err_c2;
231 1.1 uwe
232 1.1 uwe /* check if any preread input is already there */
233 1.1 uwe if (serbuf_read != serbuf_write)
234 1.1 uwe return (1);
235 1.1 uwe
236 1.1 uwe if (scif_fdr_read() & SCFDR2_RECVCNT) {
237 1.1 uwe c = scif_frdr_read();
238 1.1 uwe err_c = scif_ssr_read();
239 1.1 uwe scif_ssr_write(scif_ssr_read()
240 1.1 uwe & ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR));
241 1.1 uwe
242 1.1 uwe err_c2 = scif_lsr_read();
243 1.1 uwe scif_lsr_write(scif_lsr_read() & ~SCLSR2_ORER);
244 1.1 uwe
245 1.1 uwe if ((err_c & (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER
246 1.1 uwe | SCSSR2_PER)) == 0) {
247 1.1 uwe if ((err_c2 & SCLSR2_ORER) == 0) {
248 1.1 uwe /* stuff char into preread buffer */
249 1.1 uwe serbuf[serbuf_write] = (u_char)c;
250 1.1 uwe serbuf_write = (serbuf_write + 1) % SERBUFSIZE;
251 1.1 uwe return (1);
252 1.1 uwe }
253 1.1 uwe }
254 1.1 uwe }
255 1.1 uwe return (0); /* nothing out there... */
256 1.1 uwe }
257 1.1 uwe
258 1.1 uwe int
259 1.1 uwe scif_status2(void)
260 1.1 uwe {
261 1.1 uwe
262 1.1 uwe if (scif_fdr_read() & SCFDR2_RECVCNT) {
263 1.1 uwe return (1);
264 1.1 uwe }
265 1.1 uwe return (0);
266 1.1 uwe }
267