com.c revision 1.329.2.2 1 /* $NetBSD: com.c,v 1.329.2.2 2015/06/06 14:40:07 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2004, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1991 The Regents of the University of California.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)com.c 7.5 (Berkeley) 5/16/91
61 */
62
63 /*
64 * COM driver, uses National Semiconductor NS16450/NS16550AF UART
65 * Supports automatic hardware flow control on StarTech ST16C650A UART
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: com.c,v 1.329.2.2 2015/06/06 14:40:07 skrll Exp $");
70
71 #include "opt_com.h"
72 #include "opt_ddb.h"
73 #include "opt_kgdb.h"
74 #include "opt_lockdebug.h"
75 #include "opt_multiprocessor.h"
76 #include "opt_ntp.h"
77
78 /* The COM16650 option was renamed to COM_16650. */
79 #ifdef COM16650
80 #error Obsolete COM16650 option; use COM_16650 instead.
81 #endif
82
83 /*
84 * Override cnmagic(9) macro before including <sys/systm.h>.
85 * We need to know if cn_check_magic triggered debugger, so set a flag.
86 * Callers of cn_check_magic must declare int cn_trapped = 0;
87 * XXX: this is *ugly*!
88 */
89 #define cn_trap() \
90 do { \
91 console_debugger(); \
92 cn_trapped = 1; \
93 (void)cn_trapped; \
94 } while (/* CONSTCOND */ 0)
95
96 #include <sys/param.h>
97 #include <sys/systm.h>
98 #include <sys/ioctl.h>
99 #include <sys/select.h>
100 #include <sys/poll.h>
101 #include <sys/tty.h>
102 #include <sys/proc.h>
103 #include <sys/conf.h>
104 #include <sys/file.h>
105 #include <sys/uio.h>
106 #include <sys/kernel.h>
107 #include <sys/syslog.h>
108 #include <sys/device.h>
109 #include <sys/malloc.h>
110 #include <sys/timepps.h>
111 #include <sys/vnode.h>
112 #include <sys/kauth.h>
113 #include <sys/intr.h>
114 #ifdef RND_COM
115 #include <sys/rndsource.h>
116 #endif
117
118
119 #include <sys/bus.h>
120
121 #include <dev/ic/comreg.h>
122 #include <dev/ic/comvar.h>
123 #include <dev/ic/ns16550reg.h>
124 #include <dev/ic/st16650reg.h>
125 #ifdef COM_HAYESP
126 #include <dev/ic/hayespreg.h>
127 #endif
128 #define com_lcr com_cfcr
129 #include <dev/cons.h>
130
131 #ifdef COM_REGMAP
132 #define CSR_WRITE_1(r, o, v) \
133 bus_space_write_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], v)
134 #define CSR_READ_1(r, o) \
135 bus_space_read_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o])
136 #define CSR_WRITE_2(r, o, v) \
137 bus_space_write_2((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], v)
138 #define CSR_READ_2(r, o) \
139 bus_space_read_2((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o])
140 #define CSR_WRITE_MULTI(r, o, p, n) \
141 bus_space_write_multi_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], p, n)
142 #else
143 #define CSR_WRITE_1(r, o, v) \
144 bus_space_write_1((r)->cr_iot, (r)->cr_ioh, o, v)
145 #define CSR_READ_1(r, o) \
146 bus_space_read_1((r)->cr_iot, (r)->cr_ioh, o)
147 #define CSR_WRITE_2(r, o, v) \
148 bus_space_write_2((r)->cr_iot, (r)->cr_ioh, o, v)
149 #define CSR_READ_2(r, o) \
150 bus_space_read_2((r)->cr_iot, (r)->cr_ioh, o)
151 #define CSR_WRITE_MULTI(r, o, p, n) \
152 bus_space_write_multi_1((r)->cr_iot, (r)->cr_ioh, o, p, n)
153 #endif
154
155
156 static void com_enable_debugport(struct com_softc *);
157
158 void com_config(struct com_softc *);
159 void com_shutdown(struct com_softc *);
160 int comspeed(long, long, int);
161 static u_char cflag2lcr(tcflag_t);
162 int comparam(struct tty *, struct termios *);
163 void comstart(struct tty *);
164 int comhwiflow(struct tty *, int);
165
166 void com_loadchannelregs(struct com_softc *);
167 void com_hwiflow(struct com_softc *);
168 void com_break(struct com_softc *, int);
169 void com_modem(struct com_softc *, int);
170 void tiocm_to_com(struct com_softc *, u_long, int);
171 int com_to_tiocm(struct com_softc *);
172 void com_iflush(struct com_softc *);
173
174 int com_common_getc(dev_t, struct com_regs *);
175 static void com_common_putc(dev_t, struct com_regs *, int);
176
177 int cominit(struct com_regs *, int, int, int, tcflag_t);
178
179 static int comcnreattach(void);
180
181 int comcngetc(dev_t);
182 void comcnputc(dev_t, int);
183 void comcnpollc(dev_t, int);
184
185 #define integrate static inline
186 void comsoft(void *);
187 integrate void com_rxsoft(struct com_softc *, struct tty *);
188 integrate void com_txsoft(struct com_softc *, struct tty *);
189 integrate void com_stsoft(struct com_softc *, struct tty *);
190 integrate void com_schedrx(struct com_softc *);
191 void comdiag(void *);
192
193 extern struct cfdriver com_cd;
194
195 dev_type_open(comopen);
196 dev_type_close(comclose);
197 dev_type_read(comread);
198 dev_type_write(comwrite);
199 dev_type_ioctl(comioctl);
200 dev_type_stop(comstop);
201 dev_type_tty(comtty);
202 dev_type_poll(compoll);
203
204 static struct comcons_info comcons_info;
205
206 /*
207 * Following are all routines needed for COM to act as console
208 */
209 static struct consdev comcons = {
210 NULL, NULL, comcngetc, comcnputc, comcnpollc, NULL, NULL, NULL,
211 NODEV, CN_NORMAL
212 };
213
214
215 const struct cdevsw com_cdevsw = {
216 .d_open = comopen,
217 .d_close = comclose,
218 .d_read = comread,
219 .d_write = comwrite,
220 .d_ioctl = comioctl,
221 .d_stop = comstop,
222 .d_tty = comtty,
223 .d_poll = compoll,
224 .d_mmap = nommap,
225 .d_kqfilter = ttykqfilter,
226 .d_discard = nodiscard,
227 .d_flag = D_TTY
228 };
229
230 /*
231 * Make this an option variable one can patch.
232 * But be warned: this must be a power of 2!
233 */
234 u_int com_rbuf_size = COM_RING_SIZE;
235
236 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
237 u_int com_rbuf_hiwat = (COM_RING_SIZE * 1) / 4;
238 u_int com_rbuf_lowat = (COM_RING_SIZE * 3) / 4;
239
240 static int comconsattached;
241 static struct cnm_state com_cnm_state;
242
243 #ifdef KGDB
244 #include <sys/kgdb.h>
245
246 static struct com_regs comkgdbregs;
247 static int com_kgdb_attached;
248
249 int com_kgdb_getc(void *);
250 void com_kgdb_putc(void *, int);
251 #endif /* KGDB */
252
253 #ifdef COM_REGMAP
254 /* initializer for typical 16550-ish hardware */
255 #define COM_REG_16550 { \
256 com_data, com_data, com_dlbl, com_dlbh, com_ier, com_iir, com_fifo, \
257 com_efr, com_lcr, com_mcr, com_lsr, com_msr }
258 /* 16750-specific register set, additional UART status register */
259 #define COM_REG_16750 { \
260 com_data, com_data, com_dlbl, com_dlbh, com_ier, com_iir, com_fifo, \
261 com_efr, com_lcr, com_mcr, com_lsr, com_msr, 0, 0, 0, 0, 0, 0, 0, 0, \
262 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, com_usr }
263
264 #ifdef COM_16750
265 const bus_size_t com_std_map[32] = COM_REG_16750;
266 #else
267 const bus_size_t com_std_map[16] = COM_REG_16550;
268 #endif /* COM_16750 */
269 #endif /* COM_REGMAP */
270
271 #define COMDIALOUT_MASK TTDIALOUT_MASK
272
273 #define COMUNIT(x) TTUNIT(x)
274 #define COMDIALOUT(x) TTDIALOUT(x)
275
276 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \
277 device_is_active((sc)->sc_dev))
278
279 #define BR BUS_SPACE_BARRIER_READ
280 #define BW BUS_SPACE_BARRIER_WRITE
281 #define COM_BARRIER(r, f) \
282 bus_space_barrier((r)->cr_iot, (r)->cr_ioh, 0, (r)->cr_nports, (f))
283
284 /*ARGSUSED*/
285 int
286 comspeed(long speed, long frequency, int type)
287 {
288 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
289
290 int x, err;
291 int divisor = 16;
292
293 if ((type == COM_TYPE_OMAP) && (speed > 230400)) {
294 divisor = 13;
295 }
296
297 if (speed == 0)
298 return (0);
299 if (speed < 0)
300 return (-1);
301 x = divrnd(frequency / divisor, speed);
302 if (x <= 0)
303 return (-1);
304 err = divrnd(((quad_t)frequency) * 1000 / divisor, speed * x) - 1000;
305 if (err < 0)
306 err = -err;
307 if (err > COM_TOLERANCE)
308 return (-1);
309 return (x);
310
311 #undef divrnd
312 }
313
314 #ifdef COM_DEBUG
315 int com_debug = 0;
316
317 void comstatus(struct com_softc *, const char *);
318 void
319 comstatus(struct com_softc *sc, const char *str)
320 {
321 struct tty *tp = sc->sc_tty;
322
323 aprint_normal_dev(sc->sc_dev,
324 "%s %cclocal %cdcd %cts_carr_on %cdtr %ctx_stopped\n",
325 str,
326 ISSET(tp->t_cflag, CLOCAL) ? '+' : '-',
327 ISSET(sc->sc_msr, MSR_DCD) ? '+' : '-',
328 ISSET(tp->t_state, TS_CARR_ON) ? '+' : '-',
329 ISSET(sc->sc_mcr, MCR_DTR) ? '+' : '-',
330 sc->sc_tx_stopped ? '+' : '-');
331
332 aprint_normal_dev(sc->sc_dev,
333 "%s %ccrtscts %ccts %cts_ttstop %crts rx_flags=0x%x\n",
334 str,
335 ISSET(tp->t_cflag, CRTSCTS) ? '+' : '-',
336 ISSET(sc->sc_msr, MSR_CTS) ? '+' : '-',
337 ISSET(tp->t_state, TS_TTSTOP) ? '+' : '-',
338 ISSET(sc->sc_mcr, MCR_RTS) ? '+' : '-',
339 sc->sc_rx_flags);
340 }
341 #endif
342
343 int
344 com_probe_subr(struct com_regs *regs)
345 {
346
347 /* force access to id reg */
348 CSR_WRITE_1(regs, COM_REG_LCR, LCR_8BITS);
349 CSR_WRITE_1(regs, COM_REG_IIR, 0);
350 if ((CSR_READ_1(regs, COM_REG_LCR) != LCR_8BITS) ||
351 (CSR_READ_1(regs, COM_REG_IIR) & 0x38))
352 return (0);
353
354 return (1);
355 }
356
357 int
358 comprobe1(bus_space_tag_t iot, bus_space_handle_t ioh)
359 {
360 struct com_regs regs;
361
362 regs.cr_iot = iot;
363 regs.cr_ioh = ioh;
364 #ifdef COM_REGMAP
365 memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));
366 #endif
367
368 return com_probe_subr(®s);
369 }
370
371 /*
372 * No locking in this routine; it is only called during attach,
373 * or with the port already locked.
374 */
375 static void
376 com_enable_debugport(struct com_softc *sc)
377 {
378
379 /* Turn on line break interrupt, set carrier. */
380 sc->sc_ier = IER_ERXRDY;
381 if (sc->sc_type == COM_TYPE_PXA2x0)
382 sc->sc_ier |= IER_EUART | IER_ERXTOUT;
383 if (sc->sc_type == COM_TYPE_INGENIC ||
384 sc->sc_type == COM_TYPE_TEGRA)
385 sc->sc_ier |= IER_ERXTOUT;
386 CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
387 SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
388 CSR_WRITE_1(&sc->sc_regs, COM_REG_MCR, sc->sc_mcr);
389 }
390
391 void
392 com_attach_subr(struct com_softc *sc)
393 {
394 struct com_regs *regsp = &sc->sc_regs;
395 struct tty *tp;
396 #if defined(COM_16650) || defined(COM_16750)
397 u_int8_t lcr;
398 #endif
399 const char *fifo_msg = NULL;
400 prop_dictionary_t dict;
401 bool is_console = true;
402
403 aprint_naive("\n");
404
405 dict = device_properties(sc->sc_dev);
406 prop_dictionary_get_bool(dict, "is_console", &is_console);
407 callout_init(&sc->sc_diag_callout, 0);
408 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
409
410 /* Disable interrupts before configuring the device. */
411 if (sc->sc_type == COM_TYPE_PXA2x0)
412 sc->sc_ier = IER_EUART;
413 else
414 sc->sc_ier = 0;
415
416 CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
417
418 if (bus_space_is_equal(regsp->cr_iot, comcons_info.regs.cr_iot) &&
419 regsp->cr_iobase == comcons_info.regs.cr_iobase) {
420 comconsattached = 1;
421
422 if (cn_tab == NULL && comcnreattach() != 0) {
423 printf("can't re-init serial console @%lx\n",
424 (u_long)comcons_info.regs.cr_iobase);
425 }
426
427 #ifdef COM_16750
428 /* Use in comintr(). */
429 sc->sc_lcr = cflag2lcr(comcons_info.cflag);
430 #endif
431
432 /* Make sure the console is always "hardwired". */
433 delay(10000); /* wait for output to finish */
434 if (is_console) {
435 SET(sc->sc_hwflags, COM_HW_CONSOLE);
436 }
437
438 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
439 }
440
441 /* Probe for FIFO */
442 switch (sc->sc_type) {
443 case COM_TYPE_HAYESP:
444 goto fifodone;
445
446 case COM_TYPE_AU1x00:
447 sc->sc_fifolen = 16;
448 fifo_msg = "Au1X00 UART, working fifo";
449 SET(sc->sc_hwflags, COM_HW_FIFO);
450 goto fifodelay;
451
452 case COM_TYPE_16550_NOERS:
453 sc->sc_fifolen = 16;
454 fifo_msg = "ns16650, no ERS, working fifo";
455 SET(sc->sc_hwflags, COM_HW_FIFO);
456 goto fifodelay;
457
458 case COM_TYPE_OMAP:
459 sc->sc_fifolen = 64;
460 fifo_msg = "OMAP UART, working fifo";
461 SET(sc->sc_hwflags, COM_HW_FIFO);
462 goto fifodelay;
463
464 case COM_TYPE_INGENIC:
465 sc->sc_fifolen = 16;
466 fifo_msg = "Ingenic UART, working fifo";
467 SET(sc->sc_hwflags, COM_HW_FIFO);
468 SET(sc->sc_hwflags, COM_HW_NOIEN);
469 goto fifodelay;
470 }
471
472 sc->sc_fifolen = 1;
473 /* look for a NS 16550AF UART with FIFOs */
474 if (sc->sc_type == COM_TYPE_INGENIC) {
475 CSR_WRITE_1(regsp, COM_REG_FIFO,
476 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
477 FIFO_TRIGGER_14 | FIFO_UART_ON);
478 } else
479 CSR_WRITE_1(regsp, COM_REG_FIFO,
480 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
481 delay(100);
482 if (ISSET(CSR_READ_1(regsp, COM_REG_IIR), IIR_FIFO_MASK)
483 == IIR_FIFO_MASK)
484 if (ISSET(CSR_READ_1(regsp, COM_REG_FIFO), FIFO_TRIGGER_14)
485 == FIFO_TRIGGER_14) {
486 SET(sc->sc_hwflags, COM_HW_FIFO);
487
488 #ifdef COM_16650
489 /*
490 * IIR changes into the EFR if LCR is set to LCR_EERS
491 * on 16650s. We also know IIR != 0 at this point.
492 * Write 0 into the EFR, and read it. If the result
493 * is 0, we have a 16650.
494 *
495 * Older 16650s were broken; the test to detect them
496 * is taken from the Linux driver. Apparently
497 * setting DLAB enable gives access to the EFR on
498 * these chips.
499 */
500 lcr = CSR_READ_1(regsp, COM_REG_LCR);
501 CSR_WRITE_1(regsp, COM_REG_LCR, LCR_EERS);
502 CSR_WRITE_1(regsp, COM_REG_EFR, 0);
503 if (CSR_READ_1(regsp, COM_REG_EFR) == 0) {
504 CSR_WRITE_1(regsp, COM_REG_LCR, lcr | LCR_DLAB);
505 if (CSR_READ_1(regsp, COM_REG_EFR) == 0) {
506 CLR(sc->sc_hwflags, COM_HW_FIFO);
507 sc->sc_fifolen = 0;
508 } else {
509 SET(sc->sc_hwflags, COM_HW_FLOW);
510 sc->sc_fifolen = 32;
511 }
512 } else
513 #endif
514 sc->sc_fifolen = 16;
515
516 #ifdef COM_16750
517 /*
518 * TL16C750 can enable 64byte FIFO, only when DLAB
519 * is 1. However, some 16750 may always enable. For
520 * example, restrictions according to DLAB in a data
521 * sheet for SC16C750 were not described.
522 * Please enable 'options COM_16650', supposing you
523 * use SC16C750. Probably 32 bytes of FIFO and HW FLOW
524 * should become effective.
525 */
526 uint8_t iir1, iir2;
527 uint8_t fcr = FIFO_ENABLE | FIFO_TRIGGER_14;
528
529 if (sc->sc_type == COM_TYPE_INGENIC)
530 fcr |= FIFO_UART_ON;
531
532 lcr = CSR_READ_1(regsp, COM_REG_LCR);
533 CSR_WRITE_1(regsp, COM_REG_LCR, lcr & ~LCR_DLAB);
534 CSR_WRITE_1(regsp, COM_REG_FIFO, fcr | FIFO_64B_ENABLE);
535 iir1 = CSR_READ_1(regsp, COM_REG_IIR);
536 CSR_WRITE_1(regsp, COM_REG_FIFO, fcr);
537 CSR_WRITE_1(regsp, COM_REG_LCR, lcr | LCR_DLAB);
538 CSR_WRITE_1(regsp, COM_REG_FIFO, fcr | FIFO_64B_ENABLE);
539 iir2 = CSR_READ_1(regsp, COM_REG_IIR);
540
541 CSR_WRITE_1(regsp, COM_REG_LCR, lcr);
542
543 if (!ISSET(iir1, IIR_64B_FIFO) &&
544 ISSET(iir2, IIR_64B_FIFO)) {
545 /* It is TL16C750. */
546 sc->sc_fifolen = 64;
547 SET(sc->sc_hwflags, COM_HW_AFE);
548 } else
549 CSR_WRITE_1(regsp, COM_REG_FIFO, fcr);
550 #endif
551
552 #ifdef COM_16650
553 CSR_WRITE_1(regsp, COM_REG_LCR, lcr);
554 if (sc->sc_fifolen == 0)
555 fifo_msg = "st16650, broken fifo";
556 else if (sc->sc_fifolen == 32)
557 fifo_msg = "st16650a, working fifo";
558 else
559 #endif
560 #ifdef COM_16750
561 if (sc->sc_fifolen == 64)
562 fifo_msg = "tl16c750, working fifo";
563 else
564 #endif
565 fifo_msg = "ns16550a, working fifo";
566 } else
567 fifo_msg = "ns16550, broken fifo";
568 else
569 fifo_msg = "ns8250 or ns16450, no fifo";
570 if (sc->sc_type == COM_TYPE_INGENIC) {
571 CSR_WRITE_1(regsp, COM_REG_FIFO, FIFO_UART_ON);
572 } else
573 CSR_WRITE_1(regsp, COM_REG_FIFO, 0);
574 fifodelay:
575 /*
576 * Some chips will clear down both Tx and Rx FIFOs when zero is
577 * written to com_fifo. If this chip is the console, writing zero
578 * results in some of the chip/FIFO description being lost, so delay
579 * printing it until now.
580 */
581 delay(10);
582 aprint_normal(": %s\n", fifo_msg);
583 if (ISSET(sc->sc_hwflags, COM_HW_TXFIFO_DISABLE)) {
584 sc->sc_fifolen = 1;
585 aprint_normal_dev(sc->sc_dev, "txfifo disabled\n");
586 }
587
588 fifodone:
589
590 tp = tty_alloc();
591 tp->t_oproc = comstart;
592 tp->t_param = comparam;
593 tp->t_hwiflow = comhwiflow;
594 tp->t_softc = sc;
595
596 sc->sc_tty = tp;
597 sc->sc_rbuf = malloc(com_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
598 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
599 sc->sc_rbavail = com_rbuf_size;
600 if (sc->sc_rbuf == NULL) {
601 aprint_error_dev(sc->sc_dev,
602 "unable to allocate ring buffer\n");
603 return;
604 }
605 sc->sc_ebuf = sc->sc_rbuf + (com_rbuf_size << 1);
606
607 tty_attach(tp);
608
609 if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
610 SET(sc->sc_mcr, MCR_IENABLE);
611
612 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
613 int maj;
614
615 /* locate the major number */
616 maj = cdevsw_lookup_major(&com_cdevsw);
617
618 tp->t_dev = cn_tab->cn_dev = makedev(maj,
619 device_unit(sc->sc_dev));
620
621 aprint_normal_dev(sc->sc_dev, "console\n");
622 }
623
624 #ifdef KGDB
625 /*
626 * Allow kgdb to "take over" this port. If this is
627 * not the console and is the kgdb device, it has
628 * exclusive use. If it's the console _and_ the
629 * kgdb device, it doesn't.
630 */
631 if (bus_space_is_equal(regsp->cr_iot, comkgdbregs.cr_iot) &&
632 regsp->cr_iobase == comkgdbregs.cr_iobase) {
633 if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
634 com_kgdb_attached = 1;
635
636 SET(sc->sc_hwflags, COM_HW_KGDB);
637 }
638 aprint_normal_dev(sc->sc_dev, "kgdb\n");
639 }
640 #endif
641
642 sc->sc_si = softint_establish(SOFTINT_SERIAL, comsoft, sc);
643
644 #ifdef RND_COM
645 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
646 RND_TYPE_TTY, RND_FLAG_DEFAULT);
647 #endif
648
649 /* if there are no enable/disable functions, assume the device
650 is always enabled */
651 if (!sc->enable)
652 sc->enabled = 1;
653
654 com_config(sc);
655
656 SET(sc->sc_hwflags, COM_HW_DEV_OK);
657 }
658
659 void
660 com_config(struct com_softc *sc)
661 {
662 struct com_regs *regsp = &sc->sc_regs;
663
664 /* Disable interrupts before configuring the device. */
665 if (sc->sc_type == COM_TYPE_PXA2x0)
666 sc->sc_ier = IER_EUART;
667 else
668 sc->sc_ier = 0;
669 CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
670 (void) CSR_READ_1(regsp, COM_REG_IIR);
671
672 #ifdef COM_HAYESP
673 /* Look for a Hayes ESP board. */
674 if (sc->sc_type == COM_TYPE_HAYESP) {
675
676 /* Set 16550 compatibility mode */
677 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
678 HAYESP_SETMODE);
679 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
680 HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
681 HAYESP_MODE_SCALE);
682
683 /* Set RTS/CTS flow control */
684 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
685 HAYESP_SETFLOWTYPE);
686 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
687 HAYESP_FLOW_RTS);
688 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
689 HAYESP_FLOW_CTS);
690
691 /* Set flow control levels */
692 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
693 HAYESP_SETRXFLOW);
694 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
695 HAYESP_HIBYTE(HAYESP_RXHIWMARK));
696 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
697 HAYESP_LOBYTE(HAYESP_RXHIWMARK));
698 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
699 HAYESP_HIBYTE(HAYESP_RXLOWMARK));
700 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
701 HAYESP_LOBYTE(HAYESP_RXLOWMARK));
702 }
703 #endif
704
705 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE|COM_HW_KGDB))
706 com_enable_debugport(sc);
707 }
708
709 #if 0
710 static int
711 comcngetc_detached(dev_t dev)
712 {
713 return 0;
714 }
715
716 static void
717 comcnputc_detached(dev_t dev, int c)
718 {
719 }
720 #endif
721
722 int
723 com_detach(device_t self, int flags)
724 {
725 struct com_softc *sc = device_private(self);
726 int maj, mn;
727
728 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
729 return EBUSY;
730
731 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE) &&
732 (flags & DETACH_SHUTDOWN) != 0)
733 return EBUSY;
734
735 if (sc->disable != NULL && sc->enabled != 0) {
736 (*sc->disable)(sc);
737 sc->enabled = 0;
738 }
739
740 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
741 comconsattached = 0;
742 cn_tab = NULL;
743 }
744
745 /* locate the major number */
746 maj = cdevsw_lookup_major(&com_cdevsw);
747
748 /* Nuke the vnodes for any open instances. */
749 mn = device_unit(self);
750 vdevgone(maj, mn, mn, VCHR);
751
752 mn |= COMDIALOUT_MASK;
753 vdevgone(maj, mn, mn, VCHR);
754
755 if (sc->sc_rbuf == NULL) {
756 /*
757 * Ring buffer allocation failed in the com_attach_subr,
758 * only the tty is allocated, and nothing else.
759 */
760 tty_free(sc->sc_tty);
761 return 0;
762 }
763
764 /* Free the receive buffer. */
765 free(sc->sc_rbuf, M_DEVBUF);
766
767 /* Detach and free the tty. */
768 tty_detach(sc->sc_tty);
769 tty_free(sc->sc_tty);
770
771 /* Unhook the soft interrupt handler. */
772 softint_disestablish(sc->sc_si);
773
774 #ifdef RND_COM
775 /* Unhook the entropy source. */
776 rnd_detach_source(&sc->rnd_source);
777 #endif
778 callout_destroy(&sc->sc_diag_callout);
779
780 /* Destroy the lock. */
781 mutex_destroy(&sc->sc_lock);
782
783 return (0);
784 }
785
786 void
787 com_shutdown(struct com_softc *sc)
788 {
789 struct tty *tp = sc->sc_tty;
790
791 mutex_spin_enter(&sc->sc_lock);
792
793 /* If we were asserting flow control, then deassert it. */
794 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
795 com_hwiflow(sc);
796
797 /* Clear any break condition set with TIOCSBRK. */
798 com_break(sc, 0);
799
800 /*
801 * Hang up if necessary. Wait a bit, so the other side has time to
802 * notice even if we immediately open the port again.
803 * Avoid tsleeping above splhigh().
804 */
805 if (ISSET(tp->t_cflag, HUPCL)) {
806 com_modem(sc, 0);
807 mutex_spin_exit(&sc->sc_lock);
808 /* XXX will only timeout */
809 (void) kpause(ttclos, false, hz, NULL);
810 mutex_spin_enter(&sc->sc_lock);
811 }
812
813 /* Turn off interrupts. */
814 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
815 sc->sc_ier = IER_ERXRDY; /* interrupt on break */
816 if ((sc->sc_type == COM_TYPE_PXA2x0) ||
817 (sc->sc_type == COM_TYPE_INGENIC) ||
818 (sc->sc_type == COM_TYPE_TEGRA))
819 sc->sc_ier |= IER_ERXTOUT;
820 } else
821 sc->sc_ier = 0;
822
823 if (sc->sc_type == COM_TYPE_PXA2x0)
824 sc->sc_ier |= IER_EUART;
825
826 CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
827
828 mutex_spin_exit(&sc->sc_lock);
829
830 if (sc->disable) {
831 #ifdef DIAGNOSTIC
832 if (!sc->enabled)
833 panic("com_shutdown: not enabled?");
834 #endif
835 (*sc->disable)(sc);
836 sc->enabled = 0;
837 }
838 }
839
840 int
841 comopen(dev_t dev, int flag, int mode, struct lwp *l)
842 {
843 struct com_softc *sc;
844 struct tty *tp;
845 int s;
846 int error;
847
848 sc = device_lookup_private(&com_cd, COMUNIT(dev));
849 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
850 sc->sc_rbuf == NULL)
851 return (ENXIO);
852
853 if (!device_is_active(sc->sc_dev))
854 return (ENXIO);
855
856 #ifdef KGDB
857 /*
858 * If this is the kgdb port, no other use is permitted.
859 */
860 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
861 return (EBUSY);
862 #endif
863
864 tp = sc->sc_tty;
865
866 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
867 return (EBUSY);
868
869 s = spltty();
870
871 /*
872 * Do the following iff this is a first open.
873 */
874 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
875 struct termios t;
876
877 tp->t_dev = dev;
878
879 if (sc->enable) {
880 if ((*sc->enable)(sc)) {
881 splx(s);
882 aprint_error_dev(sc->sc_dev,
883 "device enable failed\n");
884 return (EIO);
885 }
886 mutex_spin_enter(&sc->sc_lock);
887 sc->enabled = 1;
888 com_config(sc);
889 } else {
890 mutex_spin_enter(&sc->sc_lock);
891 }
892
893 /* Turn on interrupts. */
894 sc->sc_ier = IER_ERXRDY | IER_ERLS;
895 if (!ISSET(tp->t_cflag, CLOCAL))
896 sc->sc_ier |= IER_EMSC;
897
898 if (sc->sc_type == COM_TYPE_PXA2x0)
899 sc->sc_ier |= IER_EUART | IER_ERXTOUT;
900 else if (sc->sc_type == COM_TYPE_INGENIC ||
901 sc->sc_type == COM_TYPE_TEGRA)
902 sc->sc_ier |= IER_ERXTOUT;
903 CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, sc->sc_ier);
904
905 /* Fetch the current modem control status, needed later. */
906 sc->sc_msr = CSR_READ_1(&sc->sc_regs, COM_REG_MSR);
907
908 /* Clear PPS capture state on first open. */
909 mutex_spin_enter(&timecounter_lock);
910 memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
911 sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
912 pps_init(&sc->sc_pps_state);
913 mutex_spin_exit(&timecounter_lock);
914
915 mutex_spin_exit(&sc->sc_lock);
916
917 /*
918 * Initialize the termios status to the defaults. Add in the
919 * sticky bits from TIOCSFLAGS.
920 */
921 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
922 t.c_ospeed = comcons_info.rate;
923 t.c_cflag = comcons_info.cflag;
924 } else {
925 t.c_ospeed = TTYDEF_SPEED;
926 t.c_cflag = TTYDEF_CFLAG;
927 }
928 t.c_ispeed = t.c_ospeed;
929 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
930 SET(t.c_cflag, CLOCAL);
931 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
932 SET(t.c_cflag, CRTSCTS);
933 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
934 SET(t.c_cflag, MDMBUF);
935 /* Make sure comparam() will do something. */
936 tp->t_ospeed = 0;
937 (void) comparam(tp, &t);
938 tp->t_iflag = TTYDEF_IFLAG;
939 tp->t_oflag = TTYDEF_OFLAG;
940 tp->t_lflag = TTYDEF_LFLAG;
941 ttychars(tp);
942 ttsetwater(tp);
943
944 mutex_spin_enter(&sc->sc_lock);
945
946 /*
947 * Turn on DTR. We must always do this, even if carrier is not
948 * present, because otherwise we'd have to use TIOCSDTR
949 * immediately after setting CLOCAL, which applications do not
950 * expect. We always assert DTR while the device is open
951 * unless explicitly requested to deassert it.
952 */
953 com_modem(sc, 1);
954
955 /* Clear the input ring, and unblock. */
956 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
957 sc->sc_rbavail = com_rbuf_size;
958 com_iflush(sc);
959 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
960 com_hwiflow(sc);
961
962 #ifdef COM_DEBUG
963 if (com_debug)
964 comstatus(sc, "comopen ");
965 #endif
966
967 mutex_spin_exit(&sc->sc_lock);
968 }
969
970 splx(s);
971
972 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
973 if (error)
974 goto bad;
975
976 error = (*tp->t_linesw->l_open)(dev, tp);
977 if (error)
978 goto bad;
979
980 return (0);
981
982 bad:
983 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
984 /*
985 * We failed to open the device, and nobody else had it opened.
986 * Clean up the state as appropriate.
987 */
988 com_shutdown(sc);
989 }
990
991 return (error);
992 }
993
994 int
995 comclose(dev_t dev, int flag, int mode, struct lwp *l)
996 {
997 struct com_softc *sc =
998 device_lookup_private(&com_cd, COMUNIT(dev));
999 struct tty *tp = sc->sc_tty;
1000
1001 /* XXX This is for cons.c. */
1002 if (!ISSET(tp->t_state, TS_ISOPEN))
1003 return (0);
1004
1005 (*tp->t_linesw->l_close)(tp, flag);
1006 ttyclose(tp);
1007
1008 if (COM_ISALIVE(sc) == 0)
1009 return (0);
1010
1011 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
1012 /*
1013 * Although we got a last close, the device may still be in
1014 * use; e.g. if this was the dialout node, and there are still
1015 * processes waiting for carrier on the non-dialout node.
1016 */
1017 com_shutdown(sc);
1018 }
1019
1020 return (0);
1021 }
1022
1023 int
1024 comread(dev_t dev, struct uio *uio, int flag)
1025 {
1026 struct com_softc *sc =
1027 device_lookup_private(&com_cd, COMUNIT(dev));
1028 struct tty *tp = sc->sc_tty;
1029
1030 if (COM_ISALIVE(sc) == 0)
1031 return (EIO);
1032
1033 return ((*tp->t_linesw->l_read)(tp, uio, flag));
1034 }
1035
1036 int
1037 comwrite(dev_t dev, struct uio *uio, int flag)
1038 {
1039 struct com_softc *sc =
1040 device_lookup_private(&com_cd, COMUNIT(dev));
1041 struct tty *tp = sc->sc_tty;
1042
1043 if (COM_ISALIVE(sc) == 0)
1044 return (EIO);
1045
1046 return ((*tp->t_linesw->l_write)(tp, uio, flag));
1047 }
1048
1049 int
1050 compoll(dev_t dev, int events, struct lwp *l)
1051 {
1052 struct com_softc *sc =
1053 device_lookup_private(&com_cd, COMUNIT(dev));
1054 struct tty *tp = sc->sc_tty;
1055
1056 if (COM_ISALIVE(sc) == 0)
1057 return (POLLHUP);
1058
1059 return ((*tp->t_linesw->l_poll)(tp, events, l));
1060 }
1061
1062 struct tty *
1063 comtty(dev_t dev)
1064 {
1065 struct com_softc *sc =
1066 device_lookup_private(&com_cd, COMUNIT(dev));
1067 struct tty *tp = sc->sc_tty;
1068
1069 return (tp);
1070 }
1071
1072 int
1073 comioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1074 {
1075 struct com_softc *sc;
1076 struct tty *tp;
1077 int error;
1078
1079 sc = device_lookup_private(&com_cd, COMUNIT(dev));
1080 if (sc == NULL)
1081 return ENXIO;
1082 if (COM_ISALIVE(sc) == 0)
1083 return (EIO);
1084
1085 tp = sc->sc_tty;
1086
1087 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1088 if (error != EPASSTHROUGH)
1089 return (error);
1090
1091 error = ttioctl(tp, cmd, data, flag, l);
1092 if (error != EPASSTHROUGH)
1093 return (error);
1094
1095 error = 0;
1096 switch (cmd) {
1097 case TIOCSFLAGS:
1098 error = kauth_authorize_device_tty(l->l_cred,
1099 KAUTH_DEVICE_TTY_PRIVSET, tp);
1100 break;
1101 default:
1102 /* nothing */
1103 break;
1104 }
1105 if (error) {
1106 return error;
1107 }
1108
1109 mutex_spin_enter(&sc->sc_lock);
1110
1111 switch (cmd) {
1112 case TIOCSBRK:
1113 com_break(sc, 1);
1114 break;
1115
1116 case TIOCCBRK:
1117 com_break(sc, 0);
1118 break;
1119
1120 case TIOCSDTR:
1121 com_modem(sc, 1);
1122 break;
1123
1124 case TIOCCDTR:
1125 com_modem(sc, 0);
1126 break;
1127
1128 case TIOCGFLAGS:
1129 *(int *)data = sc->sc_swflags;
1130 break;
1131
1132 case TIOCSFLAGS:
1133 sc->sc_swflags = *(int *)data;
1134 break;
1135
1136 case TIOCMSET:
1137 case TIOCMBIS:
1138 case TIOCMBIC:
1139 tiocm_to_com(sc, cmd, *(int *)data);
1140 break;
1141
1142 case TIOCMGET:
1143 *(int *)data = com_to_tiocm(sc);
1144 break;
1145
1146 case PPS_IOC_CREATE:
1147 case PPS_IOC_DESTROY:
1148 case PPS_IOC_GETPARAMS:
1149 case PPS_IOC_SETPARAMS:
1150 case PPS_IOC_GETCAP:
1151 case PPS_IOC_FETCH:
1152 #ifdef PPS_SYNC
1153 case PPS_IOC_KCBIND:
1154 #endif
1155 mutex_spin_enter(&timecounter_lock);
1156 error = pps_ioctl(cmd, data, &sc->sc_pps_state);
1157 mutex_spin_exit(&timecounter_lock);
1158 break;
1159
1160 case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */
1161 mutex_spin_enter(&timecounter_lock);
1162 #ifndef PPS_TRAILING_EDGE
1163 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
1164 &sc->sc_pps_state.ppsinfo.assert_timestamp);
1165 #else
1166 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
1167 &sc->sc_pps_state.ppsinfo.clear_timestamp);
1168 #endif
1169 mutex_spin_exit(&timecounter_lock);
1170 break;
1171
1172 default:
1173 error = EPASSTHROUGH;
1174 break;
1175 }
1176
1177 mutex_spin_exit(&sc->sc_lock);
1178
1179 #ifdef COM_DEBUG
1180 if (com_debug)
1181 comstatus(sc, "comioctl ");
1182 #endif
1183
1184 return (error);
1185 }
1186
1187 integrate void
1188 com_schedrx(struct com_softc *sc)
1189 {
1190
1191 sc->sc_rx_ready = 1;
1192
1193 /* Wake up the poller. */
1194 softint_schedule(sc->sc_si);
1195 }
1196
1197 void
1198 com_break(struct com_softc *sc, int onoff)
1199 {
1200
1201 if (onoff)
1202 SET(sc->sc_lcr, LCR_SBREAK);
1203 else
1204 CLR(sc->sc_lcr, LCR_SBREAK);
1205
1206 if (!sc->sc_heldchange) {
1207 if (sc->sc_tx_busy) {
1208 sc->sc_heldtbc = sc->sc_tbc;
1209 sc->sc_tbc = 0;
1210 sc->sc_heldchange = 1;
1211 } else
1212 com_loadchannelregs(sc);
1213 }
1214 }
1215
1216 void
1217 com_modem(struct com_softc *sc, int onoff)
1218 {
1219
1220 if (sc->sc_mcr_dtr == 0)
1221 return;
1222
1223 if (onoff)
1224 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1225 else
1226 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1227
1228 if (!sc->sc_heldchange) {
1229 if (sc->sc_tx_busy) {
1230 sc->sc_heldtbc = sc->sc_tbc;
1231 sc->sc_tbc = 0;
1232 sc->sc_heldchange = 1;
1233 } else
1234 com_loadchannelregs(sc);
1235 }
1236 }
1237
1238 void
1239 tiocm_to_com(struct com_softc *sc, u_long how, int ttybits)
1240 {
1241 u_char combits;
1242
1243 combits = 0;
1244 if (ISSET(ttybits, TIOCM_DTR))
1245 SET(combits, MCR_DTR);
1246 if (ISSET(ttybits, TIOCM_RTS))
1247 SET(combits, MCR_RTS);
1248
1249 switch (how) {
1250 case TIOCMBIC:
1251 CLR(sc->sc_mcr, combits);
1252 break;
1253
1254 case TIOCMBIS:
1255 SET(sc->sc_mcr, combits);
1256 break;
1257
1258 case TIOCMSET:
1259 CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
1260 SET(sc->sc_mcr, combits);
1261 break;
1262 }
1263
1264 if (!sc->sc_heldchange) {
1265 if (sc->sc_tx_busy) {
1266 sc->sc_heldtbc = sc->sc_tbc;
1267 sc->sc_tbc = 0;
1268 sc->sc_heldchange = 1;
1269 } else
1270 com_loadchannelregs(sc);
1271 }
1272 }
1273
1274 int
1275 com_to_tiocm(struct com_softc *sc)
1276 {
1277 u_char combits;
1278 int ttybits = 0;
1279
1280 combits = sc->sc_mcr;
1281 if (ISSET(combits, MCR_DTR))
1282 SET(ttybits, TIOCM_DTR);
1283 if (ISSET(combits, MCR_RTS))
1284 SET(ttybits, TIOCM_RTS);
1285
1286 combits = sc->sc_msr;
1287 if (sc->sc_type == COM_TYPE_INGENIC) {
1288 SET(ttybits, TIOCM_CD);
1289 } else {
1290 if (ISSET(combits, MSR_DCD))
1291 SET(ttybits, TIOCM_CD);
1292 }
1293 if (ISSET(combits, MSR_CTS))
1294 SET(ttybits, TIOCM_CTS);
1295 if (ISSET(combits, MSR_DSR))
1296 SET(ttybits, TIOCM_DSR);
1297 if (ISSET(combits, MSR_RI | MSR_TERI))
1298 SET(ttybits, TIOCM_RI);
1299
1300 if (ISSET(sc->sc_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC))
1301 SET(ttybits, TIOCM_LE);
1302
1303 return (ttybits);
1304 }
1305
1306 static u_char
1307 cflag2lcr(tcflag_t cflag)
1308 {
1309 u_char lcr = 0;
1310
1311 switch (ISSET(cflag, CSIZE)) {
1312 case CS5:
1313 SET(lcr, LCR_5BITS);
1314 break;
1315 case CS6:
1316 SET(lcr, LCR_6BITS);
1317 break;
1318 case CS7:
1319 SET(lcr, LCR_7BITS);
1320 break;
1321 case CS8:
1322 SET(lcr, LCR_8BITS);
1323 break;
1324 }
1325 if (ISSET(cflag, PARENB)) {
1326 SET(lcr, LCR_PENAB);
1327 if (!ISSET(cflag, PARODD))
1328 SET(lcr, LCR_PEVEN);
1329 }
1330 if (ISSET(cflag, CSTOPB))
1331 SET(lcr, LCR_STOPB);
1332
1333 return (lcr);
1334 }
1335
1336 int
1337 comparam(struct tty *tp, struct termios *t)
1338 {
1339 struct com_softc *sc =
1340 device_lookup_private(&com_cd, COMUNIT(tp->t_dev));
1341 int ospeed;
1342 u_char lcr;
1343
1344 if (COM_ISALIVE(sc) == 0)
1345 return (EIO);
1346
1347 #ifdef COM_HAYESP
1348 if (sc->sc_type == COM_TYPE_HAYESP) {
1349 int prescaler, speed;
1350
1351 /*
1352 * Calculate UART clock prescaler. It should be in
1353 * range of 0 .. 3.
1354 */
1355 for (prescaler = 0, speed = t->c_ospeed; prescaler < 4;
1356 prescaler++, speed /= 2)
1357 if ((ospeed = comspeed(speed, sc->sc_frequency,
1358 sc->sc_type)) > 0)
1359 break;
1360
1361 if (prescaler == 4)
1362 return (EINVAL);
1363 sc->sc_prescaler = prescaler;
1364 } else
1365 #endif
1366 ospeed = comspeed(t->c_ospeed, sc->sc_frequency, sc->sc_type);
1367
1368 /* Check requested parameters. */
1369 if (ospeed < 0)
1370 return (EINVAL);
1371 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1372 return (EINVAL);
1373
1374 /*
1375 * For the console, always force CLOCAL and !HUPCL, so that the port
1376 * is always active.
1377 */
1378 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1379 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1380 SET(t->c_cflag, CLOCAL);
1381 CLR(t->c_cflag, HUPCL);
1382 }
1383
1384 /*
1385 * If there were no changes, don't do anything. This avoids dropping
1386 * input and improves performance when all we did was frob things like
1387 * VMIN and VTIME.
1388 */
1389 if (tp->t_ospeed == t->c_ospeed &&
1390 tp->t_cflag == t->c_cflag)
1391 return (0);
1392
1393 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
1394
1395 mutex_spin_enter(&sc->sc_lock);
1396
1397 sc->sc_lcr = lcr;
1398
1399 /*
1400 * If we're not in a mode that assumes a connection is present, then
1401 * ignore carrier changes.
1402 */
1403 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1404 sc->sc_msr_dcd = 0;
1405 else
1406 sc->sc_msr_dcd = MSR_DCD;
1407 /*
1408 * Set the flow control pins depending on the current flow control
1409 * mode.
1410 */
1411 if (ISSET(t->c_cflag, CRTSCTS)) {
1412 sc->sc_mcr_dtr = MCR_DTR;
1413 sc->sc_mcr_rts = MCR_RTS;
1414 sc->sc_msr_cts = MSR_CTS;
1415 if (ISSET(sc->sc_hwflags, COM_HW_AFE)) {
1416 SET(sc->sc_mcr, MCR_AFE);
1417 } else {
1418 sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
1419 }
1420 } else if (ISSET(t->c_cflag, MDMBUF)) {
1421 /*
1422 * For DTR/DCD flow control, make sure we don't toggle DTR for
1423 * carrier detection.
1424 */
1425 sc->sc_mcr_dtr = 0;
1426 sc->sc_mcr_rts = MCR_DTR;
1427 sc->sc_msr_cts = MSR_DCD;
1428 if (ISSET(sc->sc_hwflags, COM_HW_AFE)) {
1429 CLR(sc->sc_mcr, MCR_AFE);
1430 } else {
1431 sc->sc_efr = 0;
1432 }
1433 } else {
1434 /*
1435 * If no flow control, then always set RTS. This will make
1436 * the other side happy if it mistakenly thinks we're doing
1437 * RTS/CTS flow control.
1438 */
1439 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
1440 sc->sc_mcr_rts = 0;
1441 sc->sc_msr_cts = 0;
1442 if (ISSET(sc->sc_hwflags, COM_HW_AFE)) {
1443 CLR(sc->sc_mcr, MCR_AFE);
1444 } else {
1445 sc->sc_efr = 0;
1446 }
1447 if (ISSET(sc->sc_mcr, MCR_DTR))
1448 SET(sc->sc_mcr, MCR_RTS);
1449 else
1450 CLR(sc->sc_mcr, MCR_RTS);
1451 }
1452 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1453
1454 if (t->c_ospeed == 0 && tp->t_ospeed != 0)
1455 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1456 else if (t->c_ospeed != 0 && tp->t_ospeed == 0)
1457 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1458
1459 sc->sc_dlbl = ospeed;
1460 sc->sc_dlbh = ospeed >> 8;
1461
1462 /*
1463 * Set the FIFO threshold based on the receive speed.
1464 *
1465 * * If it's a low speed, it's probably a mouse or some other
1466 * interactive device, so set the threshold low.
1467 * * If it's a high speed, trim the trigger level down to prevent
1468 * overflows.
1469 * * Otherwise set it a bit higher.
1470 */
1471 if (sc->sc_type == COM_TYPE_HAYESP)
1472 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
1473 else if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) {
1474 if (t->c_ospeed <= 1200)
1475 sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_1;
1476 else if (t->c_ospeed <= 38400)
1477 sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_8;
1478 else
1479 sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_4;
1480 } else
1481 sc->sc_fifo = 0;
1482
1483 if (sc->sc_type == COM_TYPE_INGENIC)
1484 sc->sc_fifo |= FIFO_UART_ON;
1485
1486 /* And copy to tty. */
1487 tp->t_ispeed = t->c_ospeed;
1488 tp->t_ospeed = t->c_ospeed;
1489 tp->t_cflag = t->c_cflag;
1490
1491 if (!sc->sc_heldchange) {
1492 if (sc->sc_tx_busy) {
1493 sc->sc_heldtbc = sc->sc_tbc;
1494 sc->sc_tbc = 0;
1495 sc->sc_heldchange = 1;
1496 } else
1497 com_loadchannelregs(sc);
1498 }
1499
1500 if (!ISSET(t->c_cflag, CHWFLOW)) {
1501 /* Disable the high water mark. */
1502 sc->sc_r_hiwat = 0;
1503 sc->sc_r_lowat = 0;
1504 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1505 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1506 com_schedrx(sc);
1507 }
1508 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1509 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1510 com_hwiflow(sc);
1511 }
1512 } else {
1513 sc->sc_r_hiwat = com_rbuf_hiwat;
1514 sc->sc_r_lowat = com_rbuf_lowat;
1515 }
1516
1517 mutex_spin_exit(&sc->sc_lock);
1518
1519 /*
1520 * Update the tty layer's idea of the carrier bit, in case we changed
1521 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1522 * explicit request.
1523 */
1524 if (sc->sc_type == COM_TYPE_INGENIC) {
1525 /* no DCD here */
1526 (void) (*tp->t_linesw->l_modem)(tp, 1);
1527 } else
1528 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
1529
1530 #ifdef COM_DEBUG
1531 if (com_debug)
1532 comstatus(sc, "comparam ");
1533 #endif
1534
1535 if (!ISSET(t->c_cflag, CHWFLOW)) {
1536 if (sc->sc_tx_stopped) {
1537 sc->sc_tx_stopped = 0;
1538 comstart(tp);
1539 }
1540 }
1541
1542 return (0);
1543 }
1544
1545 void
1546 com_iflush(struct com_softc *sc)
1547 {
1548 struct com_regs *regsp = &sc->sc_regs;
1549 #ifdef DIAGNOSTIC
1550 int reg;
1551 #endif
1552 int timo;
1553
1554 #ifdef DIAGNOSTIC
1555 reg = 0xffff;
1556 #endif
1557 timo = 50000;
1558 /* flush any pending I/O */
1559 while (ISSET(CSR_READ_1(regsp, COM_REG_LSR), LSR_RXRDY)
1560 && --timo)
1561 #ifdef DIAGNOSTIC
1562 reg =
1563 #else
1564 (void)
1565 #endif
1566 CSR_READ_1(regsp, COM_REG_RXDATA);
1567 #ifdef DIAGNOSTIC
1568 if (!timo)
1569 aprint_error_dev(sc->sc_dev, "com_iflush timeout %02x\n", reg);
1570 #endif
1571
1572 #ifdef COM_16750
1573 uint8_t fifo;
1574 /*
1575 * Reset all Rx/Tx FIFO, preserve current FIFO length.
1576 * This should prevent triggering busy interrupt while
1577 * manipulating divisors.
1578 */
1579 fifo = CSR_READ_1(regsp, COM_REG_FIFO) & (FIFO_TRIGGER_1 |
1580 FIFO_TRIGGER_4 | FIFO_TRIGGER_8 | FIFO_TRIGGER_14);
1581 CSR_WRITE_1(regsp, COM_REG_FIFO, fifo | FIFO_ENABLE | FIFO_RCV_RST |
1582 FIFO_XMT_RST);
1583 delay(100);
1584 #endif
1585 }
1586
1587 void
1588 com_loadchannelregs(struct com_softc *sc)
1589 {
1590 struct com_regs *regsp = &sc->sc_regs;
1591
1592 /* XXXXX necessary? */
1593 com_iflush(sc);
1594
1595 if (sc->sc_type == COM_TYPE_PXA2x0)
1596 CSR_WRITE_1(regsp, COM_REG_IER, IER_EUART);
1597 else
1598 CSR_WRITE_1(regsp, COM_REG_IER, 0);
1599
1600 if (sc->sc_type == COM_TYPE_OMAP) {
1601 /* disable before changing settings */
1602 CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_DISABLE);
1603 }
1604
1605 if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
1606 KASSERT(sc->sc_type != COM_TYPE_AU1x00);
1607 KASSERT(sc->sc_type != COM_TYPE_16550_NOERS);
1608 /* no EFR on alchemy */
1609 CSR_WRITE_1(regsp, COM_REG_LCR, LCR_EERS);
1610 CSR_WRITE_1(regsp, COM_REG_EFR, sc->sc_efr);
1611 }
1612 if (sc->sc_type == COM_TYPE_AU1x00) {
1613 /* alchemy has single separate 16-bit clock divisor register */
1614 CSR_WRITE_2(regsp, COM_REG_DLBL, sc->sc_dlbl +
1615 (sc->sc_dlbh << 8));
1616 } else {
1617 CSR_WRITE_1(regsp, COM_REG_LCR, sc->sc_lcr | LCR_DLAB);
1618 CSR_WRITE_1(regsp, COM_REG_DLBL, sc->sc_dlbl);
1619 CSR_WRITE_1(regsp, COM_REG_DLBH, sc->sc_dlbh);
1620 }
1621 CSR_WRITE_1(regsp, COM_REG_LCR, sc->sc_lcr);
1622 CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr_active = sc->sc_mcr);
1623 CSR_WRITE_1(regsp, COM_REG_FIFO, sc->sc_fifo);
1624 #ifdef COM_HAYESP
1625 if (sc->sc_type == COM_TYPE_HAYESP) {
1626 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD1,
1627 HAYESP_SETPRESCALER);
1628 bus_space_write_1(regsp->cr_iot, sc->sc_hayespioh, HAYESP_CMD2,
1629 sc->sc_prescaler);
1630 }
1631 #endif
1632 if (sc->sc_type == COM_TYPE_OMAP) {
1633 /* setup the fifos. the FCR value is not used as long
1634 as SCR[6] and SCR[7] are 0, which they are at reset
1635 and we never touch the SCR register */
1636 uint8_t rx_fifo_trig = 40;
1637 uint8_t tx_fifo_trig = 60;
1638 uint8_t rx_start = 8;
1639 uint8_t rx_halt = 60;
1640 uint8_t tlr_value = ((rx_fifo_trig>>2) << 4) | (tx_fifo_trig>>2);
1641 uint8_t tcr_value = ((rx_start>>2) << 4) | (rx_halt>>2);
1642
1643 /* enable access to TCR & TLR */
1644 CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr | MCR_TCR_TLR);
1645
1646 /* write tcr and tlr values */
1647 CSR_WRITE_1(regsp, COM_REG_TLR, tlr_value);
1648 CSR_WRITE_1(regsp, COM_REG_TCR, tcr_value);
1649
1650 /* disable access to TCR & TLR */
1651 CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr);
1652
1653 /* enable again, but mode is based on speed */
1654 if (sc->sc_tty->t_termios.c_ospeed > 230400) {
1655 CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_UART_13X);
1656 } else {
1657 CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_UART_16X);
1658 }
1659 }
1660
1661 CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
1662 }
1663
1664 int
1665 comhwiflow(struct tty *tp, int block)
1666 {
1667 struct com_softc *sc =
1668 device_lookup_private(&com_cd, COMUNIT(tp->t_dev));
1669
1670 if (COM_ISALIVE(sc) == 0)
1671 return (0);
1672
1673 if (sc->sc_mcr_rts == 0)
1674 return (0);
1675
1676 mutex_spin_enter(&sc->sc_lock);
1677
1678 if (block) {
1679 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1680 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1681 com_hwiflow(sc);
1682 }
1683 } else {
1684 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1685 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1686 com_schedrx(sc);
1687 }
1688 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1689 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1690 com_hwiflow(sc);
1691 }
1692 }
1693
1694 mutex_spin_exit(&sc->sc_lock);
1695 return (1);
1696 }
1697
1698 /*
1699 * (un)block input via hw flowcontrol
1700 */
1701 void
1702 com_hwiflow(struct com_softc *sc)
1703 {
1704 struct com_regs *regsp= &sc->sc_regs;
1705
1706 if (sc->sc_mcr_rts == 0)
1707 return;
1708
1709 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1710 CLR(sc->sc_mcr, sc->sc_mcr_rts);
1711 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1712 } else {
1713 SET(sc->sc_mcr, sc->sc_mcr_rts);
1714 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1715 }
1716 CSR_WRITE_1(regsp, COM_REG_MCR, sc->sc_mcr_active);
1717 }
1718
1719
1720 void
1721 comstart(struct tty *tp)
1722 {
1723 struct com_softc *sc =
1724 device_lookup_private(&com_cd, COMUNIT(tp->t_dev));
1725 struct com_regs *regsp = &sc->sc_regs;
1726 int s;
1727
1728 if (COM_ISALIVE(sc) == 0)
1729 return;
1730
1731 s = spltty();
1732 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1733 goto out;
1734 if (sc->sc_tx_stopped)
1735 goto out;
1736 if (!ttypull(tp))
1737 goto out;
1738
1739 /* Grab the first contiguous region of buffer space. */
1740 {
1741 u_char *tba;
1742 int tbc;
1743
1744 tba = tp->t_outq.c_cf;
1745 tbc = ndqb(&tp->t_outq, 0);
1746
1747 mutex_spin_enter(&sc->sc_lock);
1748
1749 sc->sc_tba = tba;
1750 sc->sc_tbc = tbc;
1751 }
1752
1753 SET(tp->t_state, TS_BUSY);
1754 sc->sc_tx_busy = 1;
1755
1756 /* Enable transmit completion interrupts if necessary. */
1757 if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
1758 SET(sc->sc_ier, IER_ETXRDY);
1759 CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
1760 }
1761
1762 /* Output the first chunk of the contiguous buffer. */
1763 if (!ISSET(sc->sc_hwflags, COM_HW_NO_TXPRELOAD)) {
1764 u_int n;
1765
1766 n = sc->sc_tbc;
1767 if (n > sc->sc_fifolen)
1768 n = sc->sc_fifolen;
1769 CSR_WRITE_MULTI(regsp, COM_REG_TXDATA, sc->sc_tba, n);
1770 sc->sc_tbc -= n;
1771 sc->sc_tba += n;
1772 }
1773
1774 mutex_spin_exit(&sc->sc_lock);
1775 out:
1776 splx(s);
1777 return;
1778 }
1779
1780 /*
1781 * Stop output on a line.
1782 */
1783 void
1784 comstop(struct tty *tp, int flag)
1785 {
1786 struct com_softc *sc =
1787 device_lookup_private(&com_cd, COMUNIT(tp->t_dev));
1788
1789 mutex_spin_enter(&sc->sc_lock);
1790 if (ISSET(tp->t_state, TS_BUSY)) {
1791 /* Stop transmitting at the next chunk. */
1792 sc->sc_tbc = 0;
1793 sc->sc_heldtbc = 0;
1794 if (!ISSET(tp->t_state, TS_TTSTOP))
1795 SET(tp->t_state, TS_FLUSH);
1796 }
1797 mutex_spin_exit(&sc->sc_lock);
1798 }
1799
1800 void
1801 comdiag(void *arg)
1802 {
1803 struct com_softc *sc = arg;
1804 int overflows, floods;
1805
1806 mutex_spin_enter(&sc->sc_lock);
1807 overflows = sc->sc_overflows;
1808 sc->sc_overflows = 0;
1809 floods = sc->sc_floods;
1810 sc->sc_floods = 0;
1811 sc->sc_errors = 0;
1812 mutex_spin_exit(&sc->sc_lock);
1813
1814 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1815 device_xname(sc->sc_dev),
1816 overflows, overflows == 1 ? "" : "s",
1817 floods, floods == 1 ? "" : "s");
1818 }
1819
1820 integrate void
1821 com_rxsoft(struct com_softc *sc, struct tty *tp)
1822 {
1823 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1824 u_char *get, *end;
1825 u_int cc, scc;
1826 u_char lsr;
1827 int code;
1828
1829 end = sc->sc_ebuf;
1830 get = sc->sc_rbget;
1831 scc = cc = com_rbuf_size - sc->sc_rbavail;
1832
1833 if (cc == com_rbuf_size) {
1834 sc->sc_floods++;
1835 if (sc->sc_errors++ == 0)
1836 callout_reset(&sc->sc_diag_callout, 60 * hz,
1837 comdiag, sc);
1838 }
1839
1840 /* If not yet open, drop the entire buffer content here */
1841 if (!ISSET(tp->t_state, TS_ISOPEN)) {
1842 get += cc << 1;
1843 if (get >= end)
1844 get -= com_rbuf_size << 1;
1845 cc = 0;
1846 }
1847 while (cc) {
1848 code = get[0];
1849 lsr = get[1];
1850 if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
1851 if (ISSET(lsr, LSR_OE)) {
1852 sc->sc_overflows++;
1853 if (sc->sc_errors++ == 0)
1854 callout_reset(&sc->sc_diag_callout,
1855 60 * hz, comdiag, sc);
1856 }
1857 if (ISSET(lsr, LSR_BI | LSR_FE))
1858 SET(code, TTY_FE);
1859 if (ISSET(lsr, LSR_PE))
1860 SET(code, TTY_PE);
1861 }
1862 if ((*rint)(code, tp) == -1) {
1863 /*
1864 * The line discipline's buffer is out of space.
1865 */
1866 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1867 /*
1868 * We're either not using flow control, or the
1869 * line discipline didn't tell us to block for
1870 * some reason. Either way, we have no way to
1871 * know when there's more space available, so
1872 * just drop the rest of the data.
1873 */
1874 get += cc << 1;
1875 if (get >= end)
1876 get -= com_rbuf_size << 1;
1877 cc = 0;
1878 } else {
1879 /*
1880 * Don't schedule any more receive processing
1881 * until the line discipline tells us there's
1882 * space available (through comhwiflow()).
1883 * Leave the rest of the data in the input
1884 * buffer.
1885 */
1886 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1887 }
1888 break;
1889 }
1890 get += 2;
1891 if (get >= end)
1892 get = sc->sc_rbuf;
1893 cc--;
1894 }
1895
1896 if (cc != scc) {
1897 sc->sc_rbget = get;
1898 mutex_spin_enter(&sc->sc_lock);
1899
1900 cc = sc->sc_rbavail += scc - cc;
1901 /* Buffers should be ok again, release possible block. */
1902 if (cc >= sc->sc_r_lowat) {
1903 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1904 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1905 SET(sc->sc_ier, IER_ERXRDY);
1906 #ifdef COM_PXA2X0
1907 if (sc->sc_type == COM_TYPE_PXA2x0)
1908 SET(sc->sc_ier, IER_ERXTOUT);
1909 #endif
1910 if (sc->sc_type == COM_TYPE_INGENIC ||
1911 sc->sc_type == COM_TYPE_TEGRA)
1912 SET(sc->sc_ier, IER_ERXTOUT);
1913
1914 CSR_WRITE_1(&sc->sc_regs, COM_REG_IER,
1915 sc->sc_ier);
1916 }
1917 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1918 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1919 com_hwiflow(sc);
1920 }
1921 }
1922 mutex_spin_exit(&sc->sc_lock);
1923 }
1924 }
1925
1926 integrate void
1927 com_txsoft(struct com_softc *sc, struct tty *tp)
1928 {
1929
1930 CLR(tp->t_state, TS_BUSY);
1931 if (ISSET(tp->t_state, TS_FLUSH))
1932 CLR(tp->t_state, TS_FLUSH);
1933 else
1934 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1935 (*tp->t_linesw->l_start)(tp);
1936 }
1937
1938 integrate void
1939 com_stsoft(struct com_softc *sc, struct tty *tp)
1940 {
1941 u_char msr, delta;
1942
1943 mutex_spin_enter(&sc->sc_lock);
1944 msr = sc->sc_msr;
1945 delta = sc->sc_msr_delta;
1946 sc->sc_msr_delta = 0;
1947 mutex_spin_exit(&sc->sc_lock);
1948
1949 if (ISSET(delta, sc->sc_msr_dcd)) {
1950 /*
1951 * Inform the tty layer that carrier detect changed.
1952 */
1953 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1954 }
1955
1956 if (ISSET(delta, sc->sc_msr_cts)) {
1957 /* Block or unblock output according to flow control. */
1958 if (ISSET(msr, sc->sc_msr_cts)) {
1959 sc->sc_tx_stopped = 0;
1960 (*tp->t_linesw->l_start)(tp);
1961 } else {
1962 sc->sc_tx_stopped = 1;
1963 }
1964 }
1965
1966 #ifdef COM_DEBUG
1967 if (com_debug)
1968 comstatus(sc, "com_stsoft");
1969 #endif
1970 }
1971
1972 void
1973 comsoft(void *arg)
1974 {
1975 struct com_softc *sc = arg;
1976 struct tty *tp;
1977
1978 if (COM_ISALIVE(sc) == 0)
1979 return;
1980
1981 tp = sc->sc_tty;
1982
1983 if (sc->sc_rx_ready) {
1984 sc->sc_rx_ready = 0;
1985 com_rxsoft(sc, tp);
1986 }
1987
1988 if (sc->sc_st_check) {
1989 sc->sc_st_check = 0;
1990 com_stsoft(sc, tp);
1991 }
1992
1993 if (sc->sc_tx_done) {
1994 sc->sc_tx_done = 0;
1995 com_txsoft(sc, tp);
1996 }
1997 }
1998
1999 int
2000 comintr(void *arg)
2001 {
2002 struct com_softc *sc = arg;
2003 struct com_regs *regsp = &sc->sc_regs;
2004
2005 u_char *put, *end;
2006 u_int cc;
2007 u_char lsr, iir;
2008
2009 if (COM_ISALIVE(sc) == 0)
2010 return (0);
2011
2012 KASSERT(regsp != NULL);
2013
2014 mutex_spin_enter(&sc->sc_lock);
2015 iir = CSR_READ_1(regsp, COM_REG_IIR);
2016
2017 /* Handle ns16750-specific busy interrupt. */
2018 #ifdef COM_16750
2019 int timeout;
2020 if ((iir & IIR_BUSY) == IIR_BUSY) {
2021 for (timeout = 10000;
2022 (CSR_READ_1(regsp, COM_REG_USR) & 0x1) != 0; timeout--)
2023 if (timeout <= 0) {
2024 aprint_error_dev(sc->sc_dev,
2025 "timeout while waiting for BUSY interrupt "
2026 "acknowledge\n");
2027 mutex_spin_exit(&sc->sc_lock);
2028 return (0);
2029 }
2030
2031 CSR_WRITE_1(regsp, COM_REG_LCR, sc->sc_lcr);
2032 iir = CSR_READ_1(regsp, COM_REG_IIR);
2033 }
2034 #endif /* COM_16750 */
2035
2036
2037 if (ISSET(iir, IIR_NOPEND)) {
2038 mutex_spin_exit(&sc->sc_lock);
2039 return (0);
2040 }
2041
2042 end = sc->sc_ebuf;
2043 put = sc->sc_rbput;
2044 cc = sc->sc_rbavail;
2045
2046 again: do {
2047 u_char msr, delta;
2048
2049 lsr = CSR_READ_1(regsp, COM_REG_LSR);
2050 if (ISSET(lsr, LSR_BI)) {
2051 int cn_trapped = 0; /* see above: cn_trap() */
2052
2053 cn_check_magic(sc->sc_tty->t_dev,
2054 CNC_BREAK, com_cnm_state);
2055 if (cn_trapped)
2056 continue;
2057 #if defined(KGDB) && !defined(DDB)
2058 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
2059 kgdb_connect(1);
2060 continue;
2061 }
2062 #endif
2063 }
2064
2065 if (ISSET(lsr, LSR_RCV_MASK) &&
2066 !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
2067 while (cc > 0) {
2068 int cn_trapped = 0;
2069 put[0] = CSR_READ_1(regsp, COM_REG_RXDATA);
2070 put[1] = lsr;
2071 cn_check_magic(sc->sc_tty->t_dev,
2072 put[0], com_cnm_state);
2073 if (cn_trapped)
2074 goto next;
2075 put += 2;
2076 if (put >= end)
2077 put = sc->sc_rbuf;
2078 cc--;
2079 next:
2080 lsr = CSR_READ_1(regsp, COM_REG_LSR);
2081 if (!ISSET(lsr, LSR_RCV_MASK))
2082 break;
2083 }
2084
2085 /*
2086 * Current string of incoming characters ended because
2087 * no more data was available or we ran out of space.
2088 * Schedule a receive event if any data was received.
2089 * If we're out of space, turn off receive interrupts.
2090 */
2091 sc->sc_rbput = put;
2092 sc->sc_rbavail = cc;
2093 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
2094 sc->sc_rx_ready = 1;
2095
2096 /*
2097 * See if we are in danger of overflowing a buffer. If
2098 * so, use hardware flow control to ease the pressure.
2099 */
2100 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
2101 cc < sc->sc_r_hiwat) {
2102 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
2103 com_hwiflow(sc);
2104 }
2105
2106 /*
2107 * If we're out of space, disable receive interrupts
2108 * until the queue has drained a bit.
2109 */
2110 if (!cc) {
2111 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
2112 #ifdef COM_PXA2X0
2113 if (sc->sc_type == COM_TYPE_PXA2x0)
2114 CLR(sc->sc_ier, IER_ERXRDY|IER_ERXTOUT);
2115 else
2116 #endif
2117 if (sc->sc_type == COM_TYPE_INGENIC ||
2118 sc->sc_type == COM_TYPE_TEGRA)
2119 CLR(sc->sc_ier,
2120 IER_ERXRDY | IER_ERXTOUT);
2121 else
2122 CLR(sc->sc_ier, IER_ERXRDY);
2123 CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
2124 }
2125 } else {
2126 if ((iir & (IIR_RXRDY|IIR_TXRDY)) == IIR_RXRDY) {
2127 (void) CSR_READ_1(regsp, COM_REG_RXDATA);
2128 continue;
2129 }
2130 }
2131
2132 msr = CSR_READ_1(regsp, COM_REG_MSR);
2133 delta = msr ^ sc->sc_msr;
2134 sc->sc_msr = msr;
2135 if ((sc->sc_pps_state.ppsparam.mode & PPS_CAPTUREBOTH) &&
2136 (delta & MSR_DCD)) {
2137 mutex_spin_enter(&timecounter_lock);
2138 pps_capture(&sc->sc_pps_state);
2139 pps_event(&sc->sc_pps_state,
2140 (msr & MSR_DCD) ?
2141 PPS_CAPTUREASSERT :
2142 PPS_CAPTURECLEAR);
2143 mutex_spin_exit(&timecounter_lock);
2144 }
2145
2146 /*
2147 * Process normal status changes
2148 */
2149 if (ISSET(delta, sc->sc_msr_mask)) {
2150 SET(sc->sc_msr_delta, delta);
2151
2152 /*
2153 * Stop output immediately if we lose the output
2154 * flow control signal or carrier detect.
2155 */
2156 if (ISSET(~msr, sc->sc_msr_mask)) {
2157 sc->sc_tbc = 0;
2158 sc->sc_heldtbc = 0;
2159 #ifdef COM_DEBUG
2160 if (com_debug)
2161 comstatus(sc, "comintr ");
2162 #endif
2163 }
2164
2165 sc->sc_st_check = 1;
2166 }
2167 } while (!ISSET((iir =
2168 CSR_READ_1(regsp, COM_REG_IIR)), IIR_NOPEND) &&
2169 /*
2170 * Since some device (e.g., ST16C1550) doesn't clear IIR_TXRDY
2171 * by IIR read, so we can't do this way: `process all interrupts,
2172 * then do TX if possible'.
2173 */
2174 (iir & IIR_IMASK) != IIR_TXRDY);
2175
2176 /*
2177 * Read LSR again, since there may be an interrupt between
2178 * the last LSR read and IIR read above.
2179 */
2180 lsr = CSR_READ_1(regsp, COM_REG_LSR);
2181
2182 /*
2183 * See if data can be transmitted as well.
2184 * Schedule tx done event if no data left
2185 * and tty was marked busy.
2186 */
2187 if (ISSET(lsr, LSR_TXRDY)) {
2188 /*
2189 * If we've delayed a parameter change, do it now, and restart
2190 * output.
2191 */
2192 if (sc->sc_heldchange) {
2193 com_loadchannelregs(sc);
2194 sc->sc_heldchange = 0;
2195 sc->sc_tbc = sc->sc_heldtbc;
2196 sc->sc_heldtbc = 0;
2197 }
2198
2199 /* Output the next chunk of the contiguous buffer, if any. */
2200 if (sc->sc_tbc > 0) {
2201 u_int n;
2202
2203 n = sc->sc_tbc;
2204 if (n > sc->sc_fifolen)
2205 n = sc->sc_fifolen;
2206 CSR_WRITE_MULTI(regsp, COM_REG_TXDATA, sc->sc_tba, n);
2207 sc->sc_tbc -= n;
2208 sc->sc_tba += n;
2209 } else {
2210 /* Disable transmit completion interrupts if necessary. */
2211 if (ISSET(sc->sc_ier, IER_ETXRDY)) {
2212 CLR(sc->sc_ier, IER_ETXRDY);
2213 CSR_WRITE_1(regsp, COM_REG_IER, sc->sc_ier);
2214 }
2215 if (sc->sc_tx_busy) {
2216 sc->sc_tx_busy = 0;
2217 sc->sc_tx_done = 1;
2218 }
2219 }
2220 }
2221
2222 if (!ISSET((iir = CSR_READ_1(regsp, COM_REG_IIR)), IIR_NOPEND))
2223 goto again;
2224
2225 mutex_spin_exit(&sc->sc_lock);
2226
2227 /* Wake up the poller. */
2228 softint_schedule(sc->sc_si);
2229
2230 #ifdef RND_COM
2231 rnd_add_uint32(&sc->rnd_source, iir | lsr);
2232 #endif
2233
2234 return (1);
2235 }
2236
2237 /*
2238 * The following functions are polled getc and putc routines, shared
2239 * by the console and kgdb glue.
2240 *
2241 * The read-ahead code is so that you can detect pending in-band
2242 * cn_magic in polled mode while doing output rather than having to
2243 * wait until the kernel decides it needs input.
2244 */
2245
2246 #define MAX_READAHEAD 20
2247 static int com_readahead[MAX_READAHEAD];
2248 static int com_readaheadcount = 0;
2249
2250 int
2251 com_common_getc(dev_t dev, struct com_regs *regsp)
2252 {
2253 int s = splserial();
2254 u_char stat, c;
2255
2256 /* got a character from reading things earlier */
2257 if (com_readaheadcount > 0) {
2258 int i;
2259
2260 c = com_readahead[0];
2261 for (i = 1; i < com_readaheadcount; i++) {
2262 com_readahead[i-1] = com_readahead[i];
2263 }
2264 com_readaheadcount--;
2265 splx(s);
2266 return (c);
2267 }
2268
2269 /* don't block until a character becomes available */
2270 if (!ISSET(stat = CSR_READ_1(regsp, COM_REG_LSR), LSR_RXRDY)) {
2271 splx(s);
2272 return -1;
2273 }
2274
2275 c = CSR_READ_1(regsp, COM_REG_RXDATA);
2276 stat = CSR_READ_1(regsp, COM_REG_IIR);
2277 {
2278 int cn_trapped = 0; /* required by cn_trap, see above */
2279 #ifdef DDB
2280 extern int db_active;
2281 if (!db_active)
2282 #endif
2283 cn_check_magic(dev, c, com_cnm_state);
2284 }
2285 splx(s);
2286 return (c);
2287 }
2288
2289 static void
2290 com_common_putc(dev_t dev, struct com_regs *regsp, int c)
2291 {
2292 int s = splserial();
2293 int cin, stat, timo;
2294
2295 if (com_readaheadcount < MAX_READAHEAD
2296 && ISSET(stat = CSR_READ_1(regsp, COM_REG_LSR), LSR_RXRDY)) {
2297 int cn_trapped = 0;
2298 cin = CSR_READ_1(regsp, COM_REG_RXDATA);
2299 stat = CSR_READ_1(regsp, COM_REG_IIR);
2300 cn_check_magic(dev, cin, com_cnm_state);
2301 com_readahead[com_readaheadcount++] = cin;
2302 }
2303
2304 /* wait for any pending transmission to finish */
2305 timo = 150000;
2306 while (!ISSET(CSR_READ_1(regsp, COM_REG_LSR), LSR_TXRDY) && --timo)
2307 continue;
2308
2309 CSR_WRITE_1(regsp, COM_REG_TXDATA, c);
2310 COM_BARRIER(regsp, BR | BW);
2311
2312 splx(s);
2313 }
2314
2315 /*
2316 * Initialize UART for use as console or KGDB line.
2317 */
2318 int
2319 cominit(struct com_regs *regsp, int rate, int frequency, int type,
2320 tcflag_t cflag)
2321 {
2322
2323 if (bus_space_map(regsp->cr_iot, regsp->cr_iobase, regsp->cr_nports, 0,
2324 ®sp->cr_ioh))
2325 return (ENOMEM); /* ??? */
2326
2327 if (type == COM_TYPE_OMAP) {
2328 /* disable before changing settings */
2329 CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_DISABLE);
2330 }
2331
2332 rate = comspeed(rate, frequency, type);
2333 if (__predict_true(rate != -1)) {
2334 if (type == COM_TYPE_AU1x00) {
2335 CSR_WRITE_2(regsp, COM_REG_DLBL, rate);
2336 } else {
2337 /* no EFR on alchemy */
2338 if ((type != COM_TYPE_16550_NOERS) &&
2339 (type != COM_TYPE_INGENIC)) {
2340 CSR_WRITE_1(regsp, COM_REG_LCR, LCR_EERS);
2341 CSR_WRITE_1(regsp, COM_REG_EFR, 0);
2342 }
2343 CSR_WRITE_1(regsp, COM_REG_LCR, LCR_DLAB);
2344 CSR_WRITE_1(regsp, COM_REG_DLBL, rate & 0xff);
2345 CSR_WRITE_1(regsp, COM_REG_DLBH, rate >> 8);
2346 }
2347 }
2348 CSR_WRITE_1(regsp, COM_REG_LCR, cflag2lcr(cflag));
2349 CSR_WRITE_1(regsp, COM_REG_MCR, MCR_DTR | MCR_RTS);
2350
2351 if (type == COM_TYPE_INGENIC) {
2352 CSR_WRITE_1(regsp, COM_REG_FIFO,
2353 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
2354 FIFO_TRIGGER_1 | FIFO_UART_ON);
2355 } else {
2356 CSR_WRITE_1(regsp, COM_REG_FIFO,
2357 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
2358 FIFO_TRIGGER_1);
2359 }
2360
2361 if (type == COM_TYPE_OMAP) {
2362 /* setup the fifos. the FCR value is not used as long
2363 as SCR[6] and SCR[7] are 0, which they are at reset
2364 and we never touch the SCR register */
2365 uint8_t rx_fifo_trig = 40;
2366 uint8_t tx_fifo_trig = 60;
2367 uint8_t rx_start = 8;
2368 uint8_t rx_halt = 60;
2369 uint8_t tlr_value = ((rx_fifo_trig>>2) << 4) | (tx_fifo_trig>>2);
2370 uint8_t tcr_value = ((rx_start>>2) << 4) | (rx_halt>>2);
2371
2372 /* enable access to TCR & TLR */
2373 CSR_WRITE_1(regsp, COM_REG_MCR, MCR_DTR | MCR_RTS | MCR_TCR_TLR);
2374
2375 /* write tcr and tlr values */
2376 CSR_WRITE_1(regsp, COM_REG_TLR, tlr_value);
2377 CSR_WRITE_1(regsp, COM_REG_TCR, tcr_value);
2378
2379 /* disable access to TCR & TLR */
2380 CSR_WRITE_1(regsp, COM_REG_MCR, MCR_DTR | MCR_RTS);
2381
2382 /* enable again, but mode is based on speed */
2383 if (rate > 230400) {
2384 CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_UART_13X);
2385 } else {
2386 CSR_WRITE_1(regsp, COM_REG_MDR1, MDR1_MODE_UART_16X);
2387 }
2388 }
2389
2390 #ifdef COM_PXA2X0
2391 if (type == COM_TYPE_PXA2x0)
2392 CSR_WRITE_1(regsp, COM_REG_IER, IER_EUART);
2393 else
2394 #endif
2395 CSR_WRITE_1(regsp, COM_REG_IER, 0);
2396
2397 return (0);
2398 }
2399
2400 int
2401 comcnattach1(struct com_regs *regsp, int rate, int frequency, int type,
2402 tcflag_t cflag)
2403 {
2404 int res;
2405
2406 comcons_info.regs = *regsp;
2407
2408 res = cominit(&comcons_info.regs, rate, frequency, type, cflag);
2409 if (res)
2410 return (res);
2411
2412 cn_tab = &comcons;
2413 cn_init_magic(&com_cnm_state);
2414 cn_set_magic("\047\001"); /* default magic is BREAK */
2415
2416 comcons_info.frequency = frequency;
2417 comcons_info.type = type;
2418 comcons_info.rate = rate;
2419 comcons_info.cflag = cflag;
2420
2421 return (0);
2422 }
2423
2424 int
2425 comcnattach(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
2426 int type, tcflag_t cflag)
2427 {
2428 struct com_regs regs;
2429
2430 memset(®s, 0, sizeof regs);
2431 regs.cr_iot = iot;
2432 regs.cr_iobase = iobase;
2433 regs.cr_nports = COM_NPORTS;
2434 #ifdef COM_REGMAP
2435 memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));
2436 #endif
2437
2438 return comcnattach1(®s, rate, frequency, type, cflag);
2439 }
2440
2441 static int
2442 comcnreattach(void)
2443 {
2444 return comcnattach1(&comcons_info.regs, comcons_info.rate,
2445 comcons_info.frequency, comcons_info.type, comcons_info.cflag);
2446 }
2447
2448 int
2449 comcngetc(dev_t dev)
2450 {
2451
2452 return (com_common_getc(dev, &comcons_info.regs));
2453 }
2454
2455 /*
2456 * Console kernel output character routine.
2457 */
2458 void
2459 comcnputc(dev_t dev, int c)
2460 {
2461
2462 com_common_putc(dev, &comcons_info.regs, c);
2463 }
2464
2465 void
2466 comcnpollc(dev_t dev, int on)
2467 {
2468
2469 com_readaheadcount = 0;
2470 }
2471
2472 #ifdef KGDB
2473 int
2474 com_kgdb_attach1(struct com_regs *regsp, int rate, int frequency, int type,
2475 tcflag_t cflag)
2476 {
2477 int res;
2478
2479 if (bus_space_is_equal(regsp->cr_iot, comcons_info.regs.cr_iot) &&
2480 regsp->cr_iobase == comcons_info.regs.cr_iobase) {
2481 #if !defined(DDB)
2482 return (EBUSY); /* cannot share with console */
2483 #else
2484 comkgdbregs = *regsp;
2485 comkgdbregs.cr_ioh = comcons_info.regs.cr_ioh;
2486 #endif
2487 } else {
2488 comkgdbregs = *regsp;
2489 res = cominit(&comkgdbregs, rate, frequency, type, cflag);
2490 if (res)
2491 return (res);
2492
2493 /*
2494 * XXXfvdl this shouldn't be needed, but the cn_magic goo
2495 * expects this to be initialized
2496 */
2497 cn_init_magic(&com_cnm_state);
2498 cn_set_magic("\047\001");
2499 }
2500
2501 kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
2502 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
2503
2504 return (0);
2505 }
2506
2507 int
2508 com_kgdb_attach(bus_space_tag_t iot, bus_addr_t iobase, int rate,
2509 int frequency, int type, tcflag_t cflag)
2510 {
2511 struct com_regs regs;
2512
2513 regs.cr_iot = iot;
2514 regs.cr_nports = COM_NPORTS;
2515 regs.cr_iobase = iobase;
2516 #ifdef COM_REGMAP
2517 memcpy(regs.cr_map, com_std_map, sizeof (regs.cr_map));
2518 #endif
2519
2520 return com_kgdb_attach1(®s, rate, frequency, type, cflag);
2521 }
2522
2523 /* ARGSUSED */
2524 int
2525 com_kgdb_getc(void *arg)
2526 {
2527
2528 return (com_common_getc(NODEV, &comkgdbregs));
2529 }
2530
2531 /* ARGSUSED */
2532 void
2533 com_kgdb_putc(void *arg, int c)
2534 {
2535
2536 com_common_putc(NODEV, &comkgdbregs, c);
2537 }
2538 #endif /* KGDB */
2539
2540 /* helper function to identify the com ports used by
2541 console or KGDB (and not yet autoconf attached) */
2542 int
2543 com_is_console(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t *ioh)
2544 {
2545 bus_space_handle_t help;
2546
2547 if (!comconsattached &&
2548 bus_space_is_equal(iot, comcons_info.regs.cr_iot) &&
2549 iobase == comcons_info.regs.cr_iobase)
2550 help = comcons_info.regs.cr_ioh;
2551 #ifdef KGDB
2552 else if (!com_kgdb_attached &&
2553 bus_space_is_equal(iot, comkgdbregs.cr_iot) &&
2554 iobase == comkgdbregs.cr_iobase)
2555 help = comkgdbregs.cr_ioh;
2556 #endif
2557 else
2558 return (0);
2559
2560 if (ioh)
2561 *ioh = help;
2562 return (1);
2563 }
2564
2565 /*
2566 * this routine exists to serve as a shutdown hook for systems that
2567 * have firmware which doesn't interact properly with a com device in
2568 * FIFO mode.
2569 */
2570 bool
2571 com_cleanup(device_t self, int how)
2572 {
2573 struct com_softc *sc = device_private(self);
2574
2575 if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
2576 CSR_WRITE_1(&sc->sc_regs, COM_REG_FIFO, 0);
2577
2578 return true;
2579 }
2580
2581 bool
2582 com_suspend(device_t self, const pmf_qual_t *qual)
2583 {
2584 struct com_softc *sc = device_private(self);
2585
2586 #if 0
2587 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE) && cn_tab == &comcons)
2588 cn_tab = &comcons_suspend;
2589 #endif
2590
2591 CSR_WRITE_1(&sc->sc_regs, COM_REG_IER, 0);
2592 (void)CSR_READ_1(&sc->sc_regs, COM_REG_IIR);
2593
2594 return true;
2595 }
2596
2597 bool
2598 com_resume(device_t self, const pmf_qual_t *qual)
2599 {
2600 struct com_softc *sc = device_private(self);
2601
2602 mutex_spin_enter(&sc->sc_lock);
2603 com_loadchannelregs(sc);
2604 mutex_spin_exit(&sc->sc_lock);
2605
2606 return true;
2607 }
2608