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