iris_zs.c revision 1.1.6.2 1 1.1.6.2 christos /* $NetBSD: iris_zs.c,v 1.1.6.2 2019/06/10 22:06:44 christos Exp $ */
2 1.1.6.2 christos
3 1.1.6.2 christos /*
4 1.1.6.2 christos * Copyright (c) 2018 Naruaki Etomi
5 1.1.6.2 christos * All rights reserved.
6 1.1.6.2 christos *
7 1.1.6.2 christos * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 christos * modification, are permitted provided that the following conditions
9 1.1.6.2 christos * are met:
10 1.1.6.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 christos * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 christos * documentation and/or other materials provided with the distribution.
15 1.1.6.2 christos *
16 1.1.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.6.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.6.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.6.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.6.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1.6.2 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1.6.2 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1.6.2 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1.6.2 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1.6.2 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1.6.2 christos */
27 1.1.6.2 christos
28 1.1.6.2 christos /*-
29 1.1.6.2 christos * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
30 1.1.6.2 christos * All rights reserved.
31 1.1.6.2 christos *
32 1.1.6.2 christos * This code is derived from software contributed to The NetBSD Foundation
33 1.1.6.2 christos * by Gordon W. Ross and Wayne Knowles
34 1.1.6.2 christos *
35 1.1.6.2 christos * Redistribution and use in source and binary forms, with or without
36 1.1.6.2 christos * modification, are permitted provided that the following conditions
37 1.1.6.2 christos * are met:
38 1.1.6.2 christos * 1. Redistributions of source code must retain the above copyright
39 1.1.6.2 christos * notice, this list of conditions and the following disclaimer.
40 1.1.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
41 1.1.6.2 christos * notice, this list of conditions and the following disclaimer in the
42 1.1.6.2 christos * documentation and/or other materials provided with the distribution.
43 1.1.6.2 christos *
44 1.1.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
45 1.1.6.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46 1.1.6.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47 1.1.6.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
48 1.1.6.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 1.1.6.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 1.1.6.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 1.1.6.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 1.1.6.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 1.1.6.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54 1.1.6.2 christos * POSSIBILITY OF SUCH DAMAGE.
55 1.1.6.2 christos */
56 1.1.6.2 christos
57 1.1.6.2 christos /*
58 1.1.6.2 christos * Silicon Graphics "IRIS" series MIPS processors machine bootloader.
59 1.1.6.2 christos * Zilog Z8530 Dual UART driver.
60 1.1.6.2 christos * Most of the following was adapted from /sys/arch/sgimips/dev/zs.c.
61 1.1.6.2 christos * NetBSD: zs.c,v 1.39 2015/02/18 16:47:58 macallan Exp
62 1.1.6.2 christos */
63 1.1.6.2 christos
64 1.1.6.2 christos #include <lib/libsa/stand.h>
65 1.1.6.2 christos #include <lib/libkern/libkern.h>
66 1.1.6.2 christos
67 1.1.6.2 christos #include <dev/ic/z8530reg.h>
68 1.1.6.2 christos
69 1.1.6.2 christos #include <mips/cpuregs.h>
70 1.1.6.2 christos #include <machine/cpu.h>
71 1.1.6.2 christos
72 1.1.6.2 christos #include "iris_machdep.h"
73 1.1.6.2 christos #include "iris_zs.h"
74 1.1.6.2 christos
75 1.1.6.2 christos #define ZSCLOCK 3672000 /* PCLK pin input clock rate */
76 1.1.6.2 christos #define ZS_DELAY() DELAY(3)
77 1.1.6.2 christos #define ZS_DEFSPEED 9600
78 1.1.6.2 christos
79 1.1.6.2 christos static void zs_write(void *, uint8_t);
80 1.1.6.2 christos static void zs_write_reg(void *, uint8_t, uint8_t);
81 1.1.6.2 christos static void zs_reset(void *);
82 1.1.6.2 christos static struct zschan *zs_get_chan_addr(int zs_unit, int channel);
83 1.1.6.2 christos int zs_getc(void *);
84 1.1.6.2 christos void zs_putc(void *, int);
85 1.1.6.2 christos int zs_scan(void *);
86 1.1.6.2 christos
87 1.1.6.2 christos static int cons_port;
88 1.1.6.2 christos
89 1.1.6.2 christos static void
90 1.1.6.2 christos zs_write(void *dev, uint8_t val)
91 1.1.6.2 christos {
92 1.1.6.2 christos struct zschan *zc = dev;
93 1.1.6.2 christos
94 1.1.6.2 christos zc->zc_csr = val;
95 1.1.6.2 christos ZS_DELAY();
96 1.1.6.2 christos }
97 1.1.6.2 christos
98 1.1.6.2 christos static void
99 1.1.6.2 christos zs_write_reg(void *dev, uint8_t reg, uint8_t val)
100 1.1.6.2 christos {
101 1.1.6.2 christos
102 1.1.6.2 christos zs_write(dev, reg);
103 1.1.6.2 christos zs_write(dev, val);
104 1.1.6.2 christos }
105 1.1.6.2 christos
106 1.1.6.2 christos static void
107 1.1.6.2 christos zs_reset(void *dev)
108 1.1.6.2 christos {
109 1.1.6.2 christos
110 1.1.6.2 christos /* clear errors */
111 1.1.6.2 christos zs_write_reg(dev, 9, 0);
112 1.1.6.2 christos /* hardware reset */
113 1.1.6.2 christos zs_write_reg(dev, 9, ZSWR9_HARD_RESET);
114 1.1.6.2 christos DELAY(1000);
115 1.1.6.2 christos
116 1.1.6.2 christos /* disable all inerttupts */
117 1.1.6.2 christos zs_write_reg(dev, 1, 0);
118 1.1.6.2 christos
119 1.1.6.2 christos /* set TX/RX misc parameters and modes */
120 1.1.6.2 christos zs_write_reg(dev, 4, ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP);
121 1.1.6.2 christos zs_write_reg(dev, 10, ZSWR10_NRZ);
122 1.1.6.2 christos zs_write_reg(dev, 3, ZSWR3_RX_8);
123 1.1.6.2 christos zs_write_reg(dev, 5, ZSWR5_TX_8 | ZSWR5_DTR | ZSWR5_RTS);
124 1.1.6.2 christos
125 1.1.6.2 christos /* sync registers unused */
126 1.1.6.2 christos zs_write_reg(dev, 6, 0);
127 1.1.6.2 christos zs_write_reg(dev, 7, 0);
128 1.1.6.2 christos
129 1.1.6.2 christos /* set clock mode */
130 1.1.6.2 christos zs_write_reg(dev, 11,
131 1.1.6.2 christos ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD | ZSWR11_TRXC_OUT_ENA);
132 1.1.6.2 christos
133 1.1.6.2 christos /* set baud rate constant */
134 1.1.6.2 christos zs_write_reg(dev, 12, BPS_TO_TCONST(ZSCLOCK / 16, ZS_DEFSPEED));
135 1.1.6.2 christos zs_write_reg(dev, 13, 0);
136 1.1.6.2 christos
137 1.1.6.2 christos /* enable baud rate generator */
138 1.1.6.2 christos zs_write_reg(dev, 14, ZSWR14_BAUD_ENA);
139 1.1.6.2 christos
140 1.1.6.2 christos /* disable all external interrupts */
141 1.1.6.2 christos zs_write_reg(dev, 15, 0);
142 1.1.6.2 christos
143 1.1.6.2 christos /* reset external status twice (see src/sys/dev/ic/z8530sc.c) */
144 1.1.6.2 christos zs_write(dev, ZSWR0_RESET_STATUS);
145 1.1.6.2 christos zs_write(dev, ZSWR0_RESET_STATUS);
146 1.1.6.2 christos
147 1.1.6.2 christos /* enable TX and RX */
148 1.1.6.2 christos zs_write_reg(dev, 3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
149 1.1.6.2 christos zs_write_reg(dev, 5,
150 1.1.6.2 christos ZSWR5_TX_8 | ZSWR5_DTR | ZSWR5_RTS | ZSWR5_TX_ENABLE);
151 1.1.6.2 christos }
152 1.1.6.2 christos
153 1.1.6.2 christos static struct zschan *
154 1.1.6.2 christos zs_get_chan_addr(int zs_unit, int channel)
155 1.1.6.2 christos {
156 1.1.6.2 christos struct zsdevice *addr;
157 1.1.6.2 christos struct zschan *zc;
158 1.1.6.2 christos
159 1.1.6.2 christos addr = (struct zsdevice *)MIPS_PHYS_TO_KSEG1(ZS_ADDR);
160 1.1.6.2 christos
161 1.1.6.2 christos zc = &addr->zs_chan_b;
162 1.1.6.2 christos
163 1.1.6.2 christos return zc;
164 1.1.6.2 christos }
165 1.1.6.2 christos
166 1.1.6.2 christos void *
167 1.1.6.2 christos zs_init(int addr, int speed)
168 1.1.6.2 christos {
169 1.1.6.2 christos struct zschan *zs;
170 1.1.6.2 christos cons_port = 0;
171 1.1.6.2 christos
172 1.1.6.2 christos zs = zs_get_chan_addr(1, cons_port);
173 1.1.6.2 christos
174 1.1.6.2 christos zs_reset(zs);
175 1.1.6.2 christos
176 1.1.6.2 christos return zs;
177 1.1.6.2 christos }
178 1.1.6.2 christos
179 1.1.6.2 christos void
180 1.1.6.2 christos zscnputc(void *dev, int c)
181 1.1.6.2 christos {
182 1.1.6.2 christos struct zschan *zs;
183 1.1.6.2 christos
184 1.1.6.2 christos zs = zs_get_chan_addr(1, cons_port);
185 1.1.6.2 christos
186 1.1.6.2 christos zs_putc(zs, c);
187 1.1.6.2 christos }
188 1.1.6.2 christos
189 1.1.6.2 christos void
190 1.1.6.2 christos zs_putc(void *arg, int c)
191 1.1.6.2 christos {
192 1.1.6.2 christos register volatile struct zschan *zc = arg;
193 1.1.6.2 christos register int rr0;
194 1.1.6.2 christos
195 1.1.6.2 christos /* Wait for transmitter to become ready. */
196 1.1.6.2 christos do {
197 1.1.6.2 christos rr0 = zc->zc_csr;
198 1.1.6.2 christos ZS_DELAY();
199 1.1.6.2 christos } while ((rr0 & ZSRR0_TX_READY) == 0);
200 1.1.6.2 christos
201 1.1.6.2 christos zc->zc_data = c;
202 1.1.6.2 christos ZS_DELAY();
203 1.1.6.2 christos }
204 1.1.6.2 christos
205 1.1.6.2 christos int
206 1.1.6.2 christos zscngetc(void *dev)
207 1.1.6.2 christos {
208 1.1.6.2 christos struct zschan *zs;
209 1.1.6.2 christos
210 1.1.6.2 christos zs = zs_get_chan_addr(1, cons_port);
211 1.1.6.2 christos
212 1.1.6.2 christos return zs_getc(zs);
213 1.1.6.2 christos }
214 1.1.6.2 christos
215 1.1.6.2 christos int
216 1.1.6.2 christos zs_getc(void *arg)
217 1.1.6.2 christos {
218 1.1.6.2 christos struct zschan *zc = arg;
219 1.1.6.2 christos int c, rr0;
220 1.1.6.2 christos
221 1.1.6.2 christos /* Wait for a character to arrive. */
222 1.1.6.2 christos do {
223 1.1.6.2 christos rr0 = zc->zc_csr;
224 1.1.6.2 christos ZS_DELAY();
225 1.1.6.2 christos } while ((rr0 & ZSRR0_RX_READY) == 0);
226 1.1.6.2 christos
227 1.1.6.2 christos c = zc->zc_data;
228 1.1.6.2 christos ZS_DELAY();
229 1.1.6.2 christos
230 1.1.6.2 christos return c;
231 1.1.6.2 christos }
232 1.1.6.2 christos
233 1.1.6.2 christos int
234 1.1.6.2 christos zscnscanc(void *dev)
235 1.1.6.2 christos {
236 1.1.6.2 christos struct zschan *zs;
237 1.1.6.2 christos
238 1.1.6.2 christos zs = zs_get_chan_addr(1, cons_port);
239 1.1.6.2 christos
240 1.1.6.2 christos return zs_scan(zs);
241 1.1.6.2 christos }
242 1.1.6.2 christos
243 1.1.6.2 christos int
244 1.1.6.2 christos zs_scan(void *arg)
245 1.1.6.2 christos {
246 1.1.6.2 christos struct zschan *zc = arg;
247 1.1.6.2 christos int c, rr0;
248 1.1.6.2 christos
249 1.1.6.2 christos /* Wait for a character to arrive. */
250 1.1.6.2 christos rr0 = zc->zc_csr;
251 1.1.6.2 christos ZS_DELAY();
252 1.1.6.2 christos
253 1.1.6.2 christos if ((rr0 & ZSRR0_RX_READY) == 0) {
254 1.1.6.2 christos return -1;
255 1.1.6.2 christos }
256 1.1.6.2 christos
257 1.1.6.2 christos c = zc->zc_data;
258 1.1.6.2 christos ZS_DELAY();
259 1.1.6.2 christos
260 1.1.6.2 christos return c;
261 1.1.6.2 christos }
262