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