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