wmcom.c revision 1.1.4.2 1 1.1.4.2 tls /* $NetBSD: wmcom.c,v 1.1.4.2 2013/06/23 06:20:03 tls Exp $ */
2 1.1.4.2 tls /*
3 1.1.4.2 tls * Copyright (c) 2012 KIYOHARA Takashi
4 1.1.4.2 tls * All rights reserved.
5 1.1.4.2 tls *
6 1.1.4.2 tls * Redistribution and use in source and binary forms, with or without
7 1.1.4.2 tls * modification, are permitted provided that the following conditions
8 1.1.4.2 tls * are met:
9 1.1.4.2 tls * 1. Redistributions of source code must retain the above copyright
10 1.1.4.2 tls * notice, this list of conditions and the following disclaimer.
11 1.1.4.2 tls * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.4.2 tls * notice, this list of conditions and the following disclaimer in the
13 1.1.4.2 tls * documentation and/or other materials provided with the distribution.
14 1.1.4.2 tls *
15 1.1.4.2 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1.4.2 tls * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1.4.2 tls * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1.4.2 tls * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 1.1.4.2 tls * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 1.1.4.2 tls * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1.4.2 tls * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1.4.2 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 1.1.4.2 tls * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1.4.2 tls * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1.4.2 tls * POSSIBILITY OF SUCH DAMAGE.
26 1.1.4.2 tls */
27 1.1.4.2 tls #include <sys/cdefs.h>
28 1.1.4.2 tls __KERNEL_RCSID(0, "$NetBSD: wmcom.c,v 1.1.4.2 2013/06/23 06:20:03 tls Exp $");
29 1.1.4.2 tls
30 1.1.4.2 tls #include "rnd.h"
31 1.1.4.2 tls
32 1.1.4.2 tls #include <sys/param.h>
33 1.1.4.2 tls #include <sys/bus.h>
34 1.1.4.2 tls #include <sys/conf.h>
35 1.1.4.2 tls #include <sys/device.h>
36 1.1.4.2 tls #include <sys/errno.h>
37 1.1.4.2 tls #include <sys/fcntl.h>
38 1.1.4.2 tls #include <sys/intr.h>
39 1.1.4.2 tls #include <sys/kauth.h>
40 1.1.4.2 tls #include <sys/lwp.h>
41 1.1.4.2 tls #include <sys/malloc.h>
42 1.1.4.2 tls #include <sys/systm.h>
43 1.1.4.2 tls #include <sys/termios.h>
44 1.1.4.2 tls #include <sys/tty.h>
45 1.1.4.2 tls #include <sys/types.h>
46 1.1.4.2 tls
47 1.1.4.2 tls #include <epoc32/windermere/windermerereg.h>
48 1.1.4.2 tls #include <epoc32/windermere/windermerevar.h>
49 1.1.4.2 tls
50 1.1.4.2 tls #include <dev/cons.h>
51 1.1.4.2 tls
52 1.1.4.2 tls #ifdef RND_COM
53 1.1.4.2 tls #include <sys/rnd.h>
54 1.1.4.2 tls #endif
55 1.1.4.2 tls
56 1.1.4.2 tls #include "ioconf.h"
57 1.1.4.2 tls #include "locators.h"
58 1.1.4.2 tls
59 1.1.4.2 tls #define COMUNIT_MASK 0x7ffff
60 1.1.4.2 tls #define COMDIALOUT_MASK 0x80000
61 1.1.4.2 tls
62 1.1.4.2 tls #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
63 1.1.4.2 tls #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
64 1.1.4.2 tls
65 1.1.4.2 tls #define WMCOM_RING_SIZE 2048
66 1.1.4.2 tls
67 1.1.4.2 tls struct wmcom_softc {
68 1.1.4.2 tls device_t sc_dev;
69 1.1.4.2 tls bus_space_tag_t sc_iot;
70 1.1.4.2 tls bus_space_handle_t sc_ioh;
71 1.1.4.2 tls
72 1.1.4.2 tls void *sc_si;
73 1.1.4.2 tls
74 1.1.4.2 tls struct tty *sc_tty;
75 1.1.4.2 tls
76 1.1.4.2 tls u_char *sc_tba;
77 1.1.4.2 tls u_int sc_tbc;
78 1.1.4.2 tls u_char *sc_rbuf;
79 1.1.4.2 tls char *volatile sc_rbget;
80 1.1.4.2 tls char *volatile sc_rbput;
81 1.1.4.2 tls volatile int sc_rbavail;
82 1.1.4.2 tls
83 1.1.4.2 tls int sc_tx_done;
84 1.1.4.2 tls int sc_rx_ready;
85 1.1.4.2 tls
86 1.1.4.2 tls int sc_hwflags;
87 1.1.4.2 tls #define COM_HW_CONSOLE (1 << 0)
88 1.1.4.2 tls #define COM_HW_DEV_OK (1 << 1)
89 1.1.4.2 tls #define COM_HW_KGDB (1 << 2)
90 1.1.4.2 tls int sc_swflags;
91 1.1.4.2 tls
92 1.1.4.2 tls int sc_flags;
93 1.1.4.2 tls #define WMCOM_IRDA (1 << 0)
94 1.1.4.2 tls
95 1.1.4.2 tls #ifdef RND_COM
96 1.1.4.2 tls krandsource_t rnd_source;
97 1.1.4.2 tls #endif
98 1.1.4.2 tls };
99 1.1.4.2 tls
100 1.1.4.2 tls static int wmcom_match(device_t, cfdata_t, void *);
101 1.1.4.2 tls static void wmcom_attach(device_t, device_t, void *);
102 1.1.4.2 tls
103 1.1.4.2 tls static int wmcom_intr(void *);
104 1.1.4.2 tls static void wmcom_soft(void *);
105 1.1.4.2 tls
106 1.1.4.2 tls static void wmcom_start(struct tty *);
107 1.1.4.2 tls static int wmcom_param(struct tty *, struct termios *);
108 1.1.4.2 tls static int wmcom_hwiflow(struct tty *, int);
109 1.1.4.2 tls
110 1.1.4.2 tls dev_type_open(wmcomopen);
111 1.1.4.2 tls dev_type_close(wmcomclose);
112 1.1.4.2 tls dev_type_read(wmcomread);
113 1.1.4.2 tls dev_type_write(wmcomwrite);
114 1.1.4.2 tls dev_type_ioctl(wmcomioctl);
115 1.1.4.2 tls dev_type_stop(wmcomstop);
116 1.1.4.2 tls dev_type_tty(wmcomtty);
117 1.1.4.2 tls dev_type_poll(wmcompoll);
118 1.1.4.2 tls
119 1.1.4.2 tls static void wmcom_iflush(struct wmcom_softc *);
120 1.1.4.2 tls static void wmcom_shutdown(struct wmcom_softc *);
121 1.1.4.2 tls static void wmcom_break(struct wmcom_softc *, int);
122 1.1.4.2 tls
123 1.1.4.2 tls static void wmcom_rxsoft(struct wmcom_softc *, struct tty *);
124 1.1.4.2 tls
125 1.1.4.2 tls static inline uint32_t wmcom_rate2lcr(int);
126 1.1.4.2 tls static uint8_t wmcom_cflag2fcr(tcflag_t);
127 1.1.4.2 tls
128 1.1.4.2 tls static int wmcom_cngetc(dev_t);
129 1.1.4.2 tls static void wmcom_cnputc(dev_t, int);
130 1.1.4.2 tls static void wmcom_cnpollc(dev_t, int);
131 1.1.4.2 tls
132 1.1.4.2 tls CFATTACH_DECL_NEW(wmcom, sizeof(struct wmcom_softc),
133 1.1.4.2 tls wmcom_match, wmcom_attach, NULL, NULL);
134 1.1.4.2 tls
135 1.1.4.2 tls const struct cdevsw wmcom_cdevsw = {
136 1.1.4.2 tls wmcomopen, wmcomclose, wmcomread, wmcomwrite, wmcomioctl,
137 1.1.4.2 tls wmcomstop, wmcomtty, wmcompoll, nommap, ttykqfilter, D_TTY
138 1.1.4.2 tls };
139 1.1.4.2 tls
140 1.1.4.2 tls static struct cnm_state wmcom_cnm_state;
141 1.1.4.2 tls static vaddr_t wmcom_cnaddr;
142 1.1.4.2 tls static int wmcom_cnrate;
143 1.1.4.2 tls static tcflag_t wmcom_cncflag;
144 1.1.4.2 tls
145 1.1.4.2 tls
146 1.1.4.2 tls /* ARGSUSED */
147 1.1.4.2 tls static int
148 1.1.4.2 tls wmcom_match(device_t parent, cfdata_t match, void *aux)
149 1.1.4.2 tls {
150 1.1.4.2 tls struct windermere_attach_args *aa = aux;
151 1.1.4.2 tls
152 1.1.4.2 tls /* Wildcard not accept */
153 1.1.4.2 tls if (aa->aa_offset == WINDERMERECF_OFFSET_DEFAULT ||
154 1.1.4.2 tls aa->aa_irq == WINDERMERECF_IRQ_DEFAULT)
155 1.1.4.2 tls return 0;
156 1.1.4.2 tls
157 1.1.4.2 tls aa->aa_size = UART_SIZE;
158 1.1.4.2 tls return 1;
159 1.1.4.2 tls }
160 1.1.4.2 tls
161 1.1.4.2 tls /* ARGSUSED */
162 1.1.4.2 tls static void
163 1.1.4.2 tls wmcom_attach(device_t parent, device_t self, void *aux)
164 1.1.4.2 tls {
165 1.1.4.2 tls struct wmcom_softc *sc = device_private(self);
166 1.1.4.2 tls struct windermere_attach_args *aa = aux;
167 1.1.4.2 tls
168 1.1.4.2 tls aprint_naive("\n");
169 1.1.4.2 tls aprint_normal("\n");
170 1.1.4.2 tls
171 1.1.4.2 tls sc->sc_dev = self;
172 1.1.4.2 tls if (windermere_bus_space_subregion(aa->aa_iot, *aa->aa_ioh,
173 1.1.4.2 tls aa->aa_offset, aa->aa_size, &sc->sc_ioh) != 0) {
174 1.1.4.2 tls aprint_error_dev(self, "can't map registers\n");
175 1.1.4.2 tls return;
176 1.1.4.2 tls }
177 1.1.4.2 tls sc->sc_iot = aa->aa_iot;
178 1.1.4.2 tls if (intr_establish(aa->aa_irq, IPL_SERIAL, 0, wmcom_intr, sc) == NULL) {
179 1.1.4.2 tls aprint_error_dev(self, "can't establish interrupt\n");
180 1.1.4.2 tls return;
181 1.1.4.2 tls }
182 1.1.4.2 tls
183 1.1.4.2 tls if (aa->aa_offset == (wmcom_cnaddr & 0xfff))
184 1.1.4.2 tls SET(sc->sc_hwflags, COM_HW_CONSOLE);
185 1.1.4.2 tls
186 1.1.4.2 tls if (aa->aa_offset == WINDERMERE_COM0_OFFSET)
187 1.1.4.2 tls SET(sc->sc_flags, WMCOM_IRDA);
188 1.1.4.2 tls
189 1.1.4.2 tls sc->sc_tty = tty_alloc();
190 1.1.4.2 tls sc->sc_tty->t_oproc = wmcom_start;
191 1.1.4.2 tls sc->sc_tty->t_param = wmcom_param;
192 1.1.4.2 tls sc->sc_tty->t_hwiflow = wmcom_hwiflow;
193 1.1.4.2 tls
194 1.1.4.2 tls sc->sc_tbc = 0;
195 1.1.4.2 tls sc->sc_rbuf = malloc(WMCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
196 1.1.4.2 tls if (sc->sc_rbuf == NULL) {
197 1.1.4.2 tls aprint_error_dev(self, "unable to allocate ring buffer\n");
198 1.1.4.2 tls return;
199 1.1.4.2 tls }
200 1.1.4.2 tls sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
201 1.1.4.2 tls sc->sc_rbavail = WMCOM_RING_SIZE;
202 1.1.4.2 tls
203 1.1.4.2 tls tty_attach(sc->sc_tty);
204 1.1.4.2 tls
205 1.1.4.2 tls if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
206 1.1.4.2 tls int maj = cdevsw_lookup_major(&wmcom_cdevsw);
207 1.1.4.2 tls
208 1.1.4.2 tls sc->sc_tty->t_dev = makedev(maj, device_unit(sc->sc_dev));
209 1.1.4.2 tls cn_tab->cn_dev = sc->sc_tty->t_dev;
210 1.1.4.2 tls
211 1.1.4.2 tls aprint_normal_dev(self, "console\n");
212 1.1.4.2 tls }
213 1.1.4.2 tls
214 1.1.4.2 tls sc->sc_si = softint_establish(SOFTINT_SERIAL, wmcom_soft, sc);
215 1.1.4.2 tls
216 1.1.4.2 tls #ifdef RND_COM
217 1.1.4.2 tls rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
218 1.1.4.2 tls RND_TYPE_TTY, 0);
219 1.1.4.2 tls #endif
220 1.1.4.2 tls
221 1.1.4.2 tls SET(sc->sc_hwflags, COM_HW_DEV_OK);
222 1.1.4.2 tls }
223 1.1.4.2 tls
224 1.1.4.2 tls static int
225 1.1.4.2 tls wmcom_intr(void *arg)
226 1.1.4.2 tls {
227 1.1.4.2 tls struct wmcom_softc *sc = arg;
228 1.1.4.2 tls bus_space_tag_t iot = sc->sc_iot;
229 1.1.4.2 tls bus_space_handle_t ioh = sc->sc_ioh;
230 1.1.4.2 tls int cc;
231 1.1.4.2 tls uint32_t data;
232 1.1.4.2 tls uint8_t fr, intm;
233 1.1.4.2 tls u_char *put;
234 1.1.4.2 tls
235 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
236 1.1.4.2 tls return 0;
237 1.1.4.2 tls
238 1.1.4.2 tls fr = bus_space_read_1(iot, ioh, UARTFR);
239 1.1.4.2 tls intm = bus_space_read_1(iot, ioh, UARTINTM);
240 1.1.4.2 tls if (bus_space_read_1(iot, ioh, UARTINT) & INT_RXINT) {
241 1.1.4.2 tls put = sc->sc_rbput;
242 1.1.4.2 tls cc = sc->sc_rbavail;
243 1.1.4.2 tls while (cc > 0) {
244 1.1.4.2 tls if (ISSET(fr, FR_RXFE))
245 1.1.4.2 tls break;
246 1.1.4.2 tls data = bus_space_read_4(iot, ioh, UARTDR);
247 1.1.4.2 tls cn_check_magic(sc->sc_tty->t_dev, data & 0xff,
248 1.1.4.2 tls wmcom_cnm_state);
249 1.1.4.2 tls
250 1.1.4.2 tls put[0] = data & 0xff;
251 1.1.4.2 tls put[1] = (data >> 8) & 0xff;
252 1.1.4.2 tls put += 2;
253 1.1.4.2 tls if (put >= sc->sc_rbuf + (WMCOM_RING_SIZE << 1))
254 1.1.4.2 tls put = sc->sc_rbuf;
255 1.1.4.2 tls cc--;
256 1.1.4.2 tls sc->sc_rx_ready = 1;
257 1.1.4.2 tls
258 1.1.4.2 tls fr = bus_space_read_1(iot, ioh, UARTFR);
259 1.1.4.2 tls }
260 1.1.4.2 tls
261 1.1.4.2 tls /*
262 1.1.4.2 tls * Current string of incoming characters ended because
263 1.1.4.2 tls * no more data was available or we ran out of space.
264 1.1.4.2 tls * Schedule a receive event if any data was received.
265 1.1.4.2 tls * If we're out of space, turn off receive interrupts.
266 1.1.4.2 tls */
267 1.1.4.2 tls sc->sc_rbput = put;
268 1.1.4.2 tls sc->sc_rbavail = cc;
269 1.1.4.2 tls
270 1.1.4.2 tls /*
271 1.1.4.2 tls * See if we are in danger of overflowing a buffer. If
272 1.1.4.2 tls * so, use hardware flow control to ease the pressure.
273 1.1.4.2 tls */
274 1.1.4.2 tls
275 1.1.4.2 tls /* but wmcom cannot. X-( */
276 1.1.4.2 tls
277 1.1.4.2 tls /*
278 1.1.4.2 tls * If we're out of space, disable receive interrupts
279 1.1.4.2 tls * until the queue has drained a bit.
280 1.1.4.2 tls */
281 1.1.4.2 tls if (cc <= 0)
282 1.1.4.2 tls CLR(intm, INT_RXINT);
283 1.1.4.2 tls }
284 1.1.4.2 tls
285 1.1.4.2 tls /*
286 1.1.4.2 tls * Done handling any receive interrupts. See if data can be
287 1.1.4.2 tls * transmitted as well. Schedule tx done event if no data left
288 1.1.4.2 tls * and tty was marked busy.
289 1.1.4.2 tls */
290 1.1.4.2 tls
291 1.1.4.2 tls if (!ISSET(fr, FR_TXFF)) {
292 1.1.4.2 tls /* Output the next chunk of the contiguous buffer, if any. */
293 1.1.4.2 tls if (sc->sc_tbc > 0) {
294 1.1.4.2 tls while (sc->sc_tbc > 0 && !ISSET(fr, FR_TXFF)) {
295 1.1.4.2 tls bus_space_write_1(iot, ioh, UARTDR,
296 1.1.4.2 tls *sc->sc_tba);
297 1.1.4.2 tls sc->sc_tba++;
298 1.1.4.2 tls sc->sc_tbc--;
299 1.1.4.2 tls fr = bus_space_read_1(iot, ioh, UARTFR);
300 1.1.4.2 tls }
301 1.1.4.2 tls } else if (!ISSET(fr, FR_BUSY) && ISSET(intm, INT_TXINT)) {
302 1.1.4.2 tls CLR(intm, INT_TXINT);
303 1.1.4.2 tls sc->sc_tx_done = 1;
304 1.1.4.2 tls }
305 1.1.4.2 tls }
306 1.1.4.2 tls
307 1.1.4.2 tls bus_space_write_1(iot, ioh, UARTINTM, intm);
308 1.1.4.2 tls
309 1.1.4.2 tls /* Wake up the poller. */
310 1.1.4.2 tls softint_schedule(sc->sc_si);
311 1.1.4.2 tls
312 1.1.4.2 tls return 1;
313 1.1.4.2 tls }
314 1.1.4.2 tls
315 1.1.4.2 tls static void
316 1.1.4.2 tls wmcom_soft(void *arg)
317 1.1.4.2 tls {
318 1.1.4.2 tls struct wmcom_softc *sc = arg;
319 1.1.4.2 tls struct tty *tp = sc->sc_tty;
320 1.1.4.2 tls
321 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
322 1.1.4.2 tls return;
323 1.1.4.2 tls
324 1.1.4.2 tls if (sc->sc_rx_ready) {
325 1.1.4.2 tls sc->sc_rx_ready = 0;
326 1.1.4.2 tls wmcom_rxsoft(sc, tp);
327 1.1.4.2 tls }
328 1.1.4.2 tls if (sc->sc_tx_done) {
329 1.1.4.2 tls sc->sc_tx_done = 0;
330 1.1.4.2 tls CLR(tp->t_state, TS_BUSY);
331 1.1.4.2 tls if (ISSET(tp->t_state, TS_FLUSH))
332 1.1.4.2 tls CLR(tp->t_state, TS_FLUSH);
333 1.1.4.2 tls else
334 1.1.4.2 tls ndflush(&tp->t_outq,
335 1.1.4.2 tls (int)(sc->sc_tba - tp->t_outq.c_cf));
336 1.1.4.2 tls (*tp->t_linesw->l_start)(tp);
337 1.1.4.2 tls }
338 1.1.4.2 tls }
339 1.1.4.2 tls
340 1.1.4.2 tls static void
341 1.1.4.2 tls wmcom_start(struct tty *tp)
342 1.1.4.2 tls {
343 1.1.4.2 tls struct wmcom_softc *sc
344 1.1.4.2 tls = device_lookup_private(&wmcom_cd, COMUNIT(tp->t_dev));
345 1.1.4.2 tls bus_space_tag_t iot = sc->sc_iot;
346 1.1.4.2 tls bus_space_handle_t ioh = sc->sc_ioh;
347 1.1.4.2 tls int s, n;
348 1.1.4.2 tls uint8_t intm;
349 1.1.4.2 tls
350 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
351 1.1.4.2 tls return;
352 1.1.4.2 tls
353 1.1.4.2 tls s = spltty();
354 1.1.4.2 tls if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
355 1.1.4.2 tls goto out;
356 1.1.4.2 tls if (!ttypull(tp))
357 1.1.4.2 tls goto out;
358 1.1.4.2 tls
359 1.1.4.2 tls /* Grab the first contiguous region of buffer space. */
360 1.1.4.2 tls {
361 1.1.4.2 tls u_char *tba;
362 1.1.4.2 tls int tbc;
363 1.1.4.2 tls
364 1.1.4.2 tls tba = tp->t_outq.c_cf;
365 1.1.4.2 tls tbc = ndqb(&tp->t_outq, 0);
366 1.1.4.2 tls
367 1.1.4.2 tls (void)splserial();
368 1.1.4.2 tls
369 1.1.4.2 tls sc->sc_tba = tba;
370 1.1.4.2 tls sc->sc_tbc = tbc;
371 1.1.4.2 tls }
372 1.1.4.2 tls
373 1.1.4.2 tls SET(tp->t_state, TS_BUSY);
374 1.1.4.2 tls
375 1.1.4.2 tls intm = bus_space_read_1(iot, ioh, UARTINTM);
376 1.1.4.2 tls if (!ISSET(intm, INT_TXINT)) {
377 1.1.4.2 tls bus_space_write_1(iot, ioh, UARTINTM, intm | INT_TXINT);
378 1.1.4.2 tls
379 1.1.4.2 tls /* Output the first chunk of the contiguous buffer. */
380 1.1.4.2 tls n = min(sc->sc_tbc, UART_FIFO_SIZE);
381 1.1.4.2 tls bus_space_write_multi_1(iot, ioh, UARTDR, sc->sc_tba, n);
382 1.1.4.2 tls sc->sc_tba += n;
383 1.1.4.2 tls sc->sc_tbc -= n;
384 1.1.4.2 tls }
385 1.1.4.2 tls out:
386 1.1.4.2 tls splx(s);
387 1.1.4.2 tls return;
388 1.1.4.2 tls }
389 1.1.4.2 tls
390 1.1.4.2 tls static int
391 1.1.4.2 tls wmcom_param(struct tty *tp, struct termios *t)
392 1.1.4.2 tls {
393 1.1.4.2 tls struct wmcom_softc *sc =
394 1.1.4.2 tls device_lookup_private(&wmcom_cd, COMUNIT(tp->t_dev));
395 1.1.4.2 tls bus_space_tag_t iot = sc->sc_iot;
396 1.1.4.2 tls bus_space_handle_t ioh = sc->sc_ioh;
397 1.1.4.2 tls int s;
398 1.1.4.2 tls
399 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
400 1.1.4.2 tls return ENXIO;
401 1.1.4.2 tls if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
402 1.1.4.2 tls return EINVAL;
403 1.1.4.2 tls
404 1.1.4.2 tls /*
405 1.1.4.2 tls * For the console, always force CLOCAL and !HUPCL, so that the port
406 1.1.4.2 tls * is always active.
407 1.1.4.2 tls */
408 1.1.4.2 tls if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
409 1.1.4.2 tls ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
410 1.1.4.2 tls SET(t->c_cflag, CLOCAL);
411 1.1.4.2 tls CLR(t->c_cflag, HUPCL);
412 1.1.4.2 tls }
413 1.1.4.2 tls
414 1.1.4.2 tls /*
415 1.1.4.2 tls * If there were no changes, don't do anything. This avoids dropping
416 1.1.4.2 tls * input and improves performance when all we did was frob things like
417 1.1.4.2 tls * VMIN and VTIME.
418 1.1.4.2 tls */
419 1.1.4.2 tls if (tp->t_ospeed == t->c_ospeed &&
420 1.1.4.2 tls tp->t_cflag == t->c_cflag)
421 1.1.4.2 tls return 0;
422 1.1.4.2 tls
423 1.1.4.2 tls s = splserial();
424 1.1.4.2 tls bus_space_write_4(iot, ioh, UARTLCR, wmcom_rate2lcr(t->c_ospeed));
425 1.1.4.2 tls bus_space_write_1(iot, ioh, UARTFCR, wmcom_cflag2fcr(t->c_cflag));
426 1.1.4.2 tls
427 1.1.4.2 tls /* And copy to tty. */
428 1.1.4.2 tls tp->t_ispeed = 0;
429 1.1.4.2 tls tp->t_ospeed = t->c_ospeed;
430 1.1.4.2 tls tp->t_cflag = t->c_cflag;
431 1.1.4.2 tls splx(s);
432 1.1.4.2 tls
433 1.1.4.2 tls /*
434 1.1.4.2 tls * Update the tty layer's idea of the carrier bit.
435 1.1.4.2 tls * We tell tty the carrier is always on.
436 1.1.4.2 tls */
437 1.1.4.2 tls (*tp->t_linesw->l_modem)(tp, 1);
438 1.1.4.2 tls
439 1.1.4.2 tls return 0;
440 1.1.4.2 tls }
441 1.1.4.2 tls
442 1.1.4.2 tls static int
443 1.1.4.2 tls wmcom_hwiflow(struct tty *tp, int block)
444 1.1.4.2 tls {
445 1.1.4.2 tls /* Nothing */
446 1.1.4.2 tls return 0;
447 1.1.4.2 tls }
448 1.1.4.2 tls
449 1.1.4.2 tls /* ARGSUSED */
450 1.1.4.2 tls int
451 1.1.4.2 tls wmcomopen(dev_t dev, int flag, int mode, struct lwp *l)
452 1.1.4.2 tls {
453 1.1.4.2 tls struct wmcom_softc *sc;
454 1.1.4.2 tls struct tty *tp;
455 1.1.4.2 tls int error, s, s2;
456 1.1.4.2 tls uint8_t con;
457 1.1.4.2 tls
458 1.1.4.2 tls sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
459 1.1.4.2 tls if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
460 1.1.4.2 tls return ENXIO;
461 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
462 1.1.4.2 tls return ENXIO;
463 1.1.4.2 tls
464 1.1.4.2 tls #ifdef KGDB
465 1.1.4.2 tls /*
466 1.1.4.2 tls * If this is the kgdb port, no other use is permitted.
467 1.1.4.2 tls */
468 1.1.4.2 tls if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
469 1.1.4.2 tls return EBUSY;
470 1.1.4.2 tls #endif
471 1.1.4.2 tls
472 1.1.4.2 tls tp = sc->sc_tty;
473 1.1.4.2 tls
474 1.1.4.2 tls if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
475 1.1.4.2 tls return EBUSY;
476 1.1.4.2 tls
477 1.1.4.2 tls s = spltty();
478 1.1.4.2 tls
479 1.1.4.2 tls /*
480 1.1.4.2 tls * Do the following iff this is a first open.
481 1.1.4.2 tls */
482 1.1.4.2 tls if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
483 1.1.4.2 tls struct termios t;
484 1.1.4.2 tls
485 1.1.4.2 tls tp->t_dev = dev;
486 1.1.4.2 tls
487 1.1.4.2 tls /* Enable and turn on interrupt */
488 1.1.4.2 tls con = CON_UARTEN;
489 1.1.4.2 tls if (ISSET(sc->sc_flags, WMCOM_IRDA))
490 1.1.4.2 tls con |= CON_IRTXM;
491 1.1.4.2 tls bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTCON, con);
492 1.1.4.2 tls bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTINTM, INT_RXINT);
493 1.1.4.2 tls
494 1.1.4.2 tls /*
495 1.1.4.2 tls * Initialize the termios status to the defaults. Add in the
496 1.1.4.2 tls * sticky bits from TIOCSFLAGS.
497 1.1.4.2 tls */
498 1.1.4.2 tls t.c_ispeed = 0;
499 1.1.4.2 tls if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
500 1.1.4.2 tls t.c_ospeed = wmcom_cnrate;
501 1.1.4.2 tls t.c_cflag = wmcom_cncflag;
502 1.1.4.2 tls } else {
503 1.1.4.2 tls t.c_ospeed = TTYDEF_SPEED;
504 1.1.4.2 tls t.c_cflag = TTYDEF_CFLAG;
505 1.1.4.2 tls }
506 1.1.4.2 tls if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
507 1.1.4.2 tls SET(t.c_cflag, CLOCAL);
508 1.1.4.2 tls if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
509 1.1.4.2 tls SET(t.c_cflag, CRTSCTS);
510 1.1.4.2 tls if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
511 1.1.4.2 tls SET(t.c_cflag, MDMBUF);
512 1.1.4.2 tls /* Make sure wmcom_param() we do something */
513 1.1.4.2 tls tp->t_ospeed = 0;
514 1.1.4.2 tls wmcom_param(tp, &t);
515 1.1.4.2 tls tp->t_iflag = TTYDEF_IFLAG;
516 1.1.4.2 tls tp->t_oflag = TTYDEF_OFLAG;
517 1.1.4.2 tls tp->t_lflag = TTYDEF_LFLAG;
518 1.1.4.2 tls ttychars(tp);
519 1.1.4.2 tls ttsetwater(tp);
520 1.1.4.2 tls
521 1.1.4.2 tls s2 = splserial();
522 1.1.4.2 tls
523 1.1.4.2 tls /* Clear the input ring. */
524 1.1.4.2 tls sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
525 1.1.4.2 tls sc->sc_rbavail = WMCOM_RING_SIZE;
526 1.1.4.2 tls wmcom_iflush(sc);
527 1.1.4.2 tls
528 1.1.4.2 tls splx(s2);
529 1.1.4.2 tls }
530 1.1.4.2 tls
531 1.1.4.2 tls splx(s);
532 1.1.4.2 tls
533 1.1.4.2 tls error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
534 1.1.4.2 tls if (error)
535 1.1.4.2 tls goto bad;
536 1.1.4.2 tls
537 1.1.4.2 tls error = (*tp->t_linesw->l_open)(dev, tp);
538 1.1.4.2 tls if (error)
539 1.1.4.2 tls goto bad;
540 1.1.4.2 tls return 0;
541 1.1.4.2 tls
542 1.1.4.2 tls bad:
543 1.1.4.2 tls if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
544 1.1.4.2 tls /*
545 1.1.4.2 tls * We failed to open the device, and nobody else had it opened.
546 1.1.4.2 tls * Clean up the state as appropriate.
547 1.1.4.2 tls */
548 1.1.4.2 tls wmcom_shutdown(sc);
549 1.1.4.2 tls
550 1.1.4.2 tls /* Disable UART */
551 1.1.4.2 tls if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
552 1.1.4.2 tls bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTCON, 0);
553 1.1.4.2 tls }
554 1.1.4.2 tls
555 1.1.4.2 tls return error;
556 1.1.4.2 tls }
557 1.1.4.2 tls
558 1.1.4.2 tls /* ARGSUSED */
559 1.1.4.2 tls int
560 1.1.4.2 tls wmcomclose(dev_t dev, int flag, int mode, struct lwp *l)
561 1.1.4.2 tls {
562 1.1.4.2 tls struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
563 1.1.4.2 tls struct tty *tp = sc->sc_tty;
564 1.1.4.2 tls
565 1.1.4.2 tls /* XXXX This is for cons.c. */
566 1.1.4.2 tls if (!ISSET(tp->t_state, TS_ISOPEN))
567 1.1.4.2 tls return 0;
568 1.1.4.2 tls
569 1.1.4.2 tls (*tp->t_linesw->l_close)(tp, flag);
570 1.1.4.2 tls ttyclose(tp);
571 1.1.4.2 tls
572 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
573 1.1.4.2 tls return 0;
574 1.1.4.2 tls
575 1.1.4.2 tls if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
576 1.1.4.2 tls /*
577 1.1.4.2 tls * Although we got a last close, the device may still be in
578 1.1.4.2 tls * use; e.g. if this was the dialout node, and there are still
579 1.1.4.2 tls * processes waiting for carrier on the non-dialout node.
580 1.1.4.2 tls */
581 1.1.4.2 tls wmcom_shutdown(sc);
582 1.1.4.2 tls
583 1.1.4.2 tls /* Disable UART */
584 1.1.4.2 tls if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
585 1.1.4.2 tls bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTCON, 0);
586 1.1.4.2 tls }
587 1.1.4.2 tls
588 1.1.4.2 tls return 0;
589 1.1.4.2 tls }
590 1.1.4.2 tls
591 1.1.4.2 tls int
592 1.1.4.2 tls wmcomread(dev_t dev, struct uio *uio, int flag)
593 1.1.4.2 tls {
594 1.1.4.2 tls struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
595 1.1.4.2 tls struct tty *tp = sc->sc_tty;
596 1.1.4.2 tls
597 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
598 1.1.4.2 tls return EIO;
599 1.1.4.2 tls
600 1.1.4.2 tls return (*tp->t_linesw->l_read)(tp, uio, flag);
601 1.1.4.2 tls }
602 1.1.4.2 tls
603 1.1.4.2 tls int
604 1.1.4.2 tls wmcomwrite(dev_t dev, struct uio *uio, int flag)
605 1.1.4.2 tls {
606 1.1.4.2 tls struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
607 1.1.4.2 tls struct tty *tp = sc->sc_tty;
608 1.1.4.2 tls
609 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
610 1.1.4.2 tls return EIO;
611 1.1.4.2 tls
612 1.1.4.2 tls return (*tp->t_linesw->l_write)(tp, uio, flag);
613 1.1.4.2 tls }
614 1.1.4.2 tls
615 1.1.4.2 tls int
616 1.1.4.2 tls wmcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
617 1.1.4.2 tls {
618 1.1.4.2 tls struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
619 1.1.4.2 tls struct tty *tp = sc->sc_tty;
620 1.1.4.2 tls int error, s;
621 1.1.4.2 tls
622 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
623 1.1.4.2 tls return EIO;
624 1.1.4.2 tls
625 1.1.4.2 tls error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
626 1.1.4.2 tls if (error != EPASSTHROUGH)
627 1.1.4.2 tls return error;
628 1.1.4.2 tls
629 1.1.4.2 tls error = ttioctl(tp, cmd, data, flag, l);
630 1.1.4.2 tls if (error != EPASSTHROUGH)
631 1.1.4.2 tls return error;
632 1.1.4.2 tls
633 1.1.4.2 tls switch (cmd) {
634 1.1.4.2 tls case TIOCSFLAGS:
635 1.1.4.2 tls error = kauth_authorize_device_tty(l->l_cred,
636 1.1.4.2 tls KAUTH_DEVICE_TTY_PRIVSET, tp);
637 1.1.4.2 tls break;
638 1.1.4.2 tls default:
639 1.1.4.2 tls break;
640 1.1.4.2 tls }
641 1.1.4.2 tls if (error)
642 1.1.4.2 tls return error;
643 1.1.4.2 tls
644 1.1.4.2 tls s = splserial();
645 1.1.4.2 tls error = 0;
646 1.1.4.2 tls switch (cmd) {
647 1.1.4.2 tls case TIOCSBRK:
648 1.1.4.2 tls wmcom_break(sc, 1);
649 1.1.4.2 tls break;
650 1.1.4.2 tls
651 1.1.4.2 tls case TIOCCBRK:
652 1.1.4.2 tls wmcom_break(sc, 0);
653 1.1.4.2 tls break;
654 1.1.4.2 tls
655 1.1.4.2 tls case TIOCGFLAGS:
656 1.1.4.2 tls *(int *)data = sc->sc_swflags;
657 1.1.4.2 tls break;
658 1.1.4.2 tls
659 1.1.4.2 tls case TIOCSFLAGS:
660 1.1.4.2 tls sc->sc_swflags = *(int *)data;
661 1.1.4.2 tls break;
662 1.1.4.2 tls
663 1.1.4.2 tls default:
664 1.1.4.2 tls error = EPASSTHROUGH;
665 1.1.4.2 tls break;
666 1.1.4.2 tls }
667 1.1.4.2 tls splx(s);
668 1.1.4.2 tls return error;
669 1.1.4.2 tls }
670 1.1.4.2 tls
671 1.1.4.2 tls int
672 1.1.4.2 tls wmcompoll(dev_t dev, int events, struct lwp *l)
673 1.1.4.2 tls {
674 1.1.4.2 tls struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
675 1.1.4.2 tls struct tty *tp = sc->sc_tty;
676 1.1.4.2 tls
677 1.1.4.2 tls if (!device_is_active(sc->sc_dev))
678 1.1.4.2 tls return EIO;
679 1.1.4.2 tls
680 1.1.4.2 tls return (*tp->t_linesw->l_poll)(tp, events, l);
681 1.1.4.2 tls }
682 1.1.4.2 tls
683 1.1.4.2 tls struct tty *
684 1.1.4.2 tls wmcomtty(dev_t dev)
685 1.1.4.2 tls {
686 1.1.4.2 tls struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
687 1.1.4.2 tls
688 1.1.4.2 tls return sc->sc_tty;
689 1.1.4.2 tls }
690 1.1.4.2 tls
691 1.1.4.2 tls void
692 1.1.4.2 tls wmcomstop(struct tty *tp, int flag)
693 1.1.4.2 tls {
694 1.1.4.2 tls int s;
695 1.1.4.2 tls
696 1.1.4.2 tls s = splserial();
697 1.1.4.2 tls if (ISSET(tp->t_state, TS_BUSY)) {
698 1.1.4.2 tls /* Stop transmitting at the next chunk. */
699 1.1.4.2 tls if (!ISSET(tp->t_state, TS_TTSTOP))
700 1.1.4.2 tls SET(tp->t_state, TS_FLUSH);
701 1.1.4.2 tls }
702 1.1.4.2 tls splx(s);
703 1.1.4.2 tls }
704 1.1.4.2 tls
705 1.1.4.2 tls
706 1.1.4.2 tls static void
707 1.1.4.2 tls wmcom_iflush(struct wmcom_softc *sc)
708 1.1.4.2 tls {
709 1.1.4.2 tls bus_space_tag_t iot = sc->sc_iot;
710 1.1.4.2 tls bus_space_handle_t ioh = sc->sc_ioh;
711 1.1.4.2 tls int timo;
712 1.1.4.2 tls
713 1.1.4.2 tls timo = 50000;
714 1.1.4.2 tls while ((bus_space_read_1(iot, ioh, UARTFR) & FR_RXFE) == 0 &&
715 1.1.4.2 tls timo--)
716 1.1.4.2 tls bus_space_read_1(iot, ioh, UARTDR);
717 1.1.4.2 tls if (timo == 0)
718 1.1.4.2 tls printf("%s: iflush timeout\n", device_xname(sc->sc_dev));
719 1.1.4.2 tls }
720 1.1.4.2 tls
721 1.1.4.2 tls static void
722 1.1.4.2 tls wmcom_shutdown(struct wmcom_softc *sc)
723 1.1.4.2 tls {
724 1.1.4.2 tls int s;
725 1.1.4.2 tls
726 1.1.4.2 tls s = splserial();
727 1.1.4.2 tls
728 1.1.4.2 tls /* Turn off interrupt */
729 1.1.4.2 tls bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTINTM, 0);
730 1.1.4.2 tls
731 1.1.4.2 tls /* Clear any break condition set with TIOCSBRK. */
732 1.1.4.2 tls wmcom_break(sc, 0);
733 1.1.4.2 tls
734 1.1.4.2 tls splx(s);
735 1.1.4.2 tls }
736 1.1.4.2 tls
737 1.1.4.2 tls static void
738 1.1.4.2 tls wmcom_break(struct wmcom_softc *sc, int onoff)
739 1.1.4.2 tls {
740 1.1.4.2 tls int s;
741 1.1.4.2 tls uint8_t fcr;
742 1.1.4.2 tls
743 1.1.4.2 tls s = splserial();
744 1.1.4.2 tls fcr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, UARTFCR);
745 1.1.4.2 tls if (onoff)
746 1.1.4.2 tls SET(fcr, FCR_BREAK);
747 1.1.4.2 tls else
748 1.1.4.2 tls CLR(fcr, FCR_BREAK);
749 1.1.4.2 tls bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTFCR, fcr);
750 1.1.4.2 tls splx(s);
751 1.1.4.2 tls }
752 1.1.4.2 tls
753 1.1.4.2 tls static void
754 1.1.4.2 tls wmcom_rxsoft(struct wmcom_softc *sc, struct tty *tp)
755 1.1.4.2 tls {
756 1.1.4.2 tls bus_space_tag_t iot = sc->sc_iot;
757 1.1.4.2 tls bus_space_handle_t ioh = sc->sc_ioh;
758 1.1.4.2 tls int code, s;
759 1.1.4.2 tls u_int cc, scc;
760 1.1.4.2 tls uint8_t intm;
761 1.1.4.2 tls u_char sts, *get;
762 1.1.4.2 tls
763 1.1.4.2 tls get = sc->sc_rbget;
764 1.1.4.2 tls scc = cc = WMCOM_RING_SIZE - sc->sc_rbavail;
765 1.1.4.2 tls while (cc) {
766 1.1.4.2 tls code = get[0];
767 1.1.4.2 tls sts = get[1];
768 1.1.4.2 tls if (ISSET(sts, RSR_FE | RSR_PE | RSR_OE)) {
769 1.1.4.2 tls if (ISSET(sts, (RSR_FE)))
770 1.1.4.2 tls SET(code, TTY_FE);
771 1.1.4.2 tls if (ISSET(sts, RSR_PE))
772 1.1.4.2 tls SET(code, TTY_PE);
773 1.1.4.2 tls if (ISSET(sts, RSR_OE))
774 1.1.4.2 tls ; /* XXXXX: Overrun */
775 1.1.4.2 tls }
776 1.1.4.2 tls if ((*tp->t_linesw->l_rint)(code, tp) == -1) {
777 1.1.4.2 tls /*
778 1.1.4.2 tls * The line discipline's buffer is out of space.
779 1.1.4.2 tls */
780 1.1.4.2 tls /*
781 1.1.4.2 tls * We're either not using flow control, or the
782 1.1.4.2 tls * line discipline didn't tell us to block for
783 1.1.4.2 tls * some reason. Either way, we have no way to
784 1.1.4.2 tls * know when there's more space available, so
785 1.1.4.2 tls * just drop the rest of the data.
786 1.1.4.2 tls */
787 1.1.4.2 tls get += cc << 1;
788 1.1.4.2 tls if (get >= sc->sc_rbuf + (WMCOM_RING_SIZE << 1))
789 1.1.4.2 tls get -= (WMCOM_RING_SIZE << 1);
790 1.1.4.2 tls cc = 0;
791 1.1.4.2 tls break;
792 1.1.4.2 tls }
793 1.1.4.2 tls get += 2;
794 1.1.4.2 tls if (get >= sc->sc_rbuf + (WMCOM_RING_SIZE << 1))
795 1.1.4.2 tls get = sc->sc_rbuf;
796 1.1.4.2 tls cc--;
797 1.1.4.2 tls }
798 1.1.4.2 tls
799 1.1.4.2 tls if (cc != scc) {
800 1.1.4.2 tls sc->sc_rbget = get;
801 1.1.4.2 tls s = splserial();
802 1.1.4.2 tls
803 1.1.4.2 tls cc = sc->sc_rbavail += scc - cc;
804 1.1.4.2 tls /* Buffers should be ok again, release possible block. */
805 1.1.4.2 tls if (cc >= 1) {
806 1.1.4.2 tls intm = bus_space_read_1(iot, ioh, UARTINTM);
807 1.1.4.2 tls SET(intm, INT_RXINT);
808 1.1.4.2 tls bus_space_write_1(iot, ioh, UARTINTM, intm);
809 1.1.4.2 tls }
810 1.1.4.2 tls splx(s);
811 1.1.4.2 tls }
812 1.1.4.2 tls }
813 1.1.4.2 tls
814 1.1.4.2 tls static inline uint32_t
815 1.1.4.2 tls wmcom_rate2lcr(int rate)
816 1.1.4.2 tls {
817 1.1.4.2 tls
818 1.1.4.2 tls return 7372800 / (16 * rate) - 1;
819 1.1.4.2 tls }
820 1.1.4.2 tls
821 1.1.4.2 tls static uint8_t
822 1.1.4.2 tls wmcom_cflag2fcr(tcflag_t cflag)
823 1.1.4.2 tls {
824 1.1.4.2 tls int8_t fcr = FCR_UFIFOEN;
825 1.1.4.2 tls
826 1.1.4.2 tls switch (cflag & CSIZE) {
827 1.1.4.2 tls case CS5: SET(fcr, FCR_WLEN_5); break;
828 1.1.4.2 tls case CS6: SET(fcr, FCR_WLEN_6); break;
829 1.1.4.2 tls case CS7: SET(fcr, FCR_WLEN_7); break;
830 1.1.4.2 tls case CS8: SET(fcr, FCR_WLEN_8); break;
831 1.1.4.2 tls default: SET(fcr, FCR_WLEN_8); break;
832 1.1.4.2 tls }
833 1.1.4.2 tls if (cflag & CSTOPB)
834 1.1.4.2 tls SET(fcr, FCR_XSTOP);
835 1.1.4.2 tls if (cflag & PARENB) {
836 1.1.4.2 tls SET(fcr, (FCR_PRTEN | FCR_EVENPRT));
837 1.1.4.2 tls if (cflag & PARODD)
838 1.1.4.2 tls CLR(fcr, FCR_EVENPRT);
839 1.1.4.2 tls }
840 1.1.4.2 tls return fcr;
841 1.1.4.2 tls }
842 1.1.4.2 tls
843 1.1.4.2 tls #define WMCOM_CNREAD_1(offset) \
844 1.1.4.2 tls (*(volatile uint8_t *)(wmcom_cnaddr + ((offset) << 2)))
845 1.1.4.2 tls #define WMCOM_CNREAD_4(offset) \
846 1.1.4.2 tls (*(volatile uint32_t *)(wmcom_cnaddr + ((offset) << 2)))
847 1.1.4.2 tls #define WMCOM_CNWRITE_1(offset, val) \
848 1.1.4.2 tls (*(volatile uint8_t *)(wmcom_cnaddr + ((offset) << 2)) = val)
849 1.1.4.2 tls #define WMCOM_CNWRITE_4(offset, val) \
850 1.1.4.2 tls (*(volatile uint32_t *)(wmcom_cnaddr + ((offset) << 2)) = val)
851 1.1.4.2 tls
852 1.1.4.2 tls static struct consdev wmcomcons = {
853 1.1.4.2 tls NULL, NULL, wmcom_cngetc, wmcom_cnputc, wmcom_cnpollc, NULL, NULL, NULL,
854 1.1.4.2 tls NODEV, CN_NORMAL
855 1.1.4.2 tls };
856 1.1.4.2 tls
857 1.1.4.2 tls int
858 1.1.4.2 tls wmcom_cnattach(vaddr_t addr, int rate, tcflag_t cflag, int irda)
859 1.1.4.2 tls {
860 1.1.4.2 tls
861 1.1.4.2 tls wmcom_cnaddr = addr;
862 1.1.4.2 tls wmcom_cnrate = rate;
863 1.1.4.2 tls wmcom_cncflag = cflag;
864 1.1.4.2 tls WMCOM_CNWRITE_4(UARTLCR, wmcom_rate2lcr(rate));
865 1.1.4.2 tls WMCOM_CNWRITE_1(UARTFCR, wmcom_cflag2fcr(cflag));
866 1.1.4.2 tls if (irda)
867 1.1.4.2 tls WMCOM_CNWRITE_1(UARTCON, CON_UARTEN | CON_IRTXM);
868 1.1.4.2 tls else
869 1.1.4.2 tls WMCOM_CNWRITE_1(UARTCON, CON_UARTEN);
870 1.1.4.2 tls
871 1.1.4.2 tls cn_tab = &wmcomcons;
872 1.1.4.2 tls cn_init_magic(&wmcom_cnm_state);
873 1.1.4.2 tls cn_set_magic("\047\001"); /* default magic is BREAK */
874 1.1.4.2 tls
875 1.1.4.2 tls return 0;
876 1.1.4.2 tls }
877 1.1.4.2 tls
878 1.1.4.2 tls /* ARGSUSED */
879 1.1.4.2 tls static int
880 1.1.4.2 tls wmcom_cngetc(dev_t dev)
881 1.1.4.2 tls {
882 1.1.4.2 tls int s = splserial();
883 1.1.4.2 tls char ch;
884 1.1.4.2 tls
885 1.1.4.2 tls while (WMCOM_CNREAD_1(UARTFR) & FR_RXFE);
886 1.1.4.2 tls
887 1.1.4.2 tls ch = WMCOM_CNREAD_4(UARTDR);
888 1.1.4.2 tls
889 1.1.4.2 tls {
890 1.1.4.2 tls #ifdef DDB
891 1.1.4.2 tls extern int db_active;
892 1.1.4.2 tls if (!db_active)
893 1.1.4.2 tls #endif
894 1.1.4.2 tls cn_check_magic(dev, ch, wmcom_cnm_state);
895 1.1.4.2 tls }
896 1.1.4.2 tls
897 1.1.4.2 tls splx(s);
898 1.1.4.2 tls return ch;
899 1.1.4.2 tls }
900 1.1.4.2 tls
901 1.1.4.2 tls /* ARGSUSED */
902 1.1.4.2 tls static void
903 1.1.4.2 tls wmcom_cnputc(dev_t dev, int c)
904 1.1.4.2 tls {
905 1.1.4.2 tls int s = splserial();
906 1.1.4.2 tls
907 1.1.4.2 tls while (WMCOM_CNREAD_1(UARTFR) & FR_TXFF);
908 1.1.4.2 tls
909 1.1.4.2 tls WMCOM_CNWRITE_1(UARTDR, c);
910 1.1.4.2 tls
911 1.1.4.2 tls /* Make sure output. */
912 1.1.4.2 tls while (WMCOM_CNREAD_1(UARTFR) & FR_BUSY);
913 1.1.4.2 tls
914 1.1.4.2 tls splx(s);
915 1.1.4.2 tls }
916 1.1.4.2 tls
917 1.1.4.2 tls /* ARGSUSED */
918 1.1.4.2 tls static void
919 1.1.4.2 tls wmcom_cnpollc(dev_t dev, int on)
920 1.1.4.2 tls {
921 1.1.4.2 tls /* Nothing */
922 1.1.4.2 tls }
923