ixp425_com.c revision 1.9 1 /* $NetBSD: ixp425_com.c,v 1.9 2003/06/29 10:51:31 ichiro Exp $ */
2 /*
3 * Copyright (c) 2003
4 * Ichiro FUKUHARA <ichiro (at) ichiro.org>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Ichiro FUKUHARA.
18 * 4. The name of the company nor the name of the author may be used to
19 * endorse or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
26 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1991 The Regents of the University of California.
37 * All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)com.c 7.5 (Berkeley) 5/16/91
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: ixp425_com.c,v 1.9 2003/06/29 10:51:31 ichiro Exp $");
72
73 #include "opt_ddb.h"
74 #include "opt_kgdb.h"
75
76 #include "rnd.h"
77 #if NRND > 0 && defined(RND_COM)
78 #include <sys/rnd.h>
79 #endif
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/types.h>
84 #include <sys/conf.h>
85 #include <sys/file.h>
86 #include <sys/device.h>
87 #include <sys/kernel.h>
88 #include <sys/malloc.h>
89 #include <sys/tty.h>
90 #include <sys/uio.h>
91 #include <sys/vnode.h>
92
93 #include <machine/intr.h>
94 #include <machine/bus.h>
95
96 #include <arm/xscale/ixp425reg.h>
97 #include <arm/xscale/ixp425_comvar.h>
98
99 #include <dev/cons.h>
100
101 #include "ixpcom.h"
102
103 static int ixp4xx_comparam(struct tty *, struct termios *);
104 static void ixp4xx_comstart(struct tty *);
105 static int ixp4xx_comhwiflow(struct tty *, int);
106
107 static u_int cflag2lcr(tcflag_t);
108 static void ixp4xx_com_modem(struct ixp4xx_com_softc *, int);
109 static void ixp4xx_com_iflush(struct ixp4xx_com_softc *);
110 static void tiocm_to_ixp4xx_com(struct ixp4xx_com_softc *, u_long, int);
111 static int ixp4xx_com_to_tiocm(struct ixp4xx_com_softc *);
112
113 static void ixp4xx_com_set_cr(struct ixp4xx_com_softc *);
114 static void ixpcom_hwiflow(struct ixp4xx_com_softc *);
115
116 static void ixp4xxcomsoft(void *);
117 static void ixpcomdiag(void *);
118 inline static void ixp4xx_com_txsoft(struct ixp4xx_com_softc *, struct tty *);
119 inline static void ixp4xx_com_rxsoft(struct ixp4xx_com_softc *, struct tty *);
120 inline static void ixpcom_schedrx(struct ixp4xx_com_softc *);
121
122 int ixp4xx_cominit(bus_space_tag_t, bus_space_handle_t, int, int, tcflag_t);
123 int ixp4xx_comcngetc(dev_t);
124 void ixp4xx_comcnputc(dev_t, int);
125 void ixp4xx_comcnpollc(dev_t, int);
126 void ixp4xx_comcnprobe(struct consdev *);
127 void ixp4xx_comcninit(struct consdev *);
128
129 static struct ixp4xx_com_cons_softc {
130 bus_space_tag_t sc_iot;
131 bus_space_handle_t sc_ioh;
132 bus_addr_t sc_baseaddr;
133 int sc_ospeed;
134 tcflag_t sc_cflag;
135 int sc_attached;
136 char *sc_name;
137 } ixp4xx_comcn_sc;
138
139 static struct cnm_state ixp4xx_com_cnm_state;
140
141 struct ixp4xx_com_softc* ixp4xx_com_sc = NULL;
142
143 extern struct cfdriver ixpcom_cd;
144
145 dev_type_open(ixp4xx_comopen);
146 dev_type_close(ixp4xx_comclose);
147 dev_type_read(ixp4xx_comread);
148 dev_type_write(ixp4xx_comwrite);
149 dev_type_ioctl(ixp4xx_comioctl);
150 dev_type_stop(ixp4xx_comstop);
151 dev_type_tty(ixp4xx_comtty);
152 dev_type_poll(ixp4xx_compoll);
153
154 const struct cdevsw ixpcom_cdevsw = {
155 ixp4xx_comopen, ixp4xx_comclose, ixp4xx_comread, ixp4xx_comwrite,
156 ixp4xx_comioctl, ixp4xx_comstop, ixp4xx_comtty, ixp4xx_compoll,
157 nommap, ttykqfilter, D_TTY
158 };
159
160 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
161 u_int ixpcom_rbuf_hiwat = (IXPCOM_RING_SIZE * 1) / 4;
162 u_int ixpcom_rbuf_lowat = (IXPCOM_RING_SIZE * 3) / 4;
163
164 #define COMUNIT_MASK 0x7ffff
165 #define COMDIALOUT_MASK 0x80000
166
167 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
168 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
169
170 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \
171 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
172
173 #define COM_LOCK(sc);
174 #define COM_UNLOCK(sc);
175
176 #ifndef COM_TOLERANCE
177 #define COM_TOLERANCE 30 /* XXX: baud rate tolerance, in 0.1% units */
178 #endif
179
180 #define SET(t, f) (t) |= (f)
181 #define CLR(t, f) (t) &= ~(f)
182 #define ISSET(t, f) ((t) & (f))
183
184 #ifdef COM_DEBUG
185 int com_debug = 0;
186 #endif
187
188 int
189 ixp4xx_comspeed(long speed, long frequency)
190 {
191 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
192
193 int x, err;
194
195 if (speed <= 0)
196 return (-1);
197 x = divrnd(frequency / 16, speed);
198 if (x <= 0)
199 return (-1);
200 err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
201 if (err < 0)
202 err = -err;
203 if (err > COM_TOLERANCE)
204 return (-1);
205 return (x);
206 #undef divrnd
207 }
208
209 #if XXX
210 static void
211 ixp4xx_enable_debugport(struct ixp4xx_com_softc *sc)
212 {
213 int s;
214
215 /* Turn on line break interrupt, set carrier. */
216 s = splserial();
217 COM_LOCK(sc);
218 SET(sc->sc_ier, IER_RAVIE | IER_UUE);
219 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_IER, sc->sc_ier);
220 SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
221 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_MCR, sc->sc_mcr);
222 COM_UNLOCK(sc);
223 splx(s);
224 }
225 #endif
226
227 void
228 ixp4xx_com_attach_subr(struct ixp4xx_com_softc *sc)
229 {
230 bus_space_tag_t iot = sc->sc_iot;
231 bus_space_handle_t ioh = sc->sc_ioh;
232
233 struct tty *tp;
234
235 ixp4xx_com_sc = sc;
236
237 callout_init(&sc->sc_diag_callout);
238
239 /* configuring the device. */
240 sc->sc_frequency = FREQ;
241 sc->sc_dlbl = ixp4xx_comspeed(CONSPEED, sc->sc_frequency);
242 sc->sc_dlbh = ixp4xx_comspeed(CONSPEED, sc->sc_frequency) >> 8;
243
244 /* Disabling interrupt */
245 sc->sc_ier = IER_UUE;
246 bus_space_write_4(iot, ioh, IXP425_UART_IER, sc->sc_ier);
247
248 sc->sc_fcr = FCR_TRIGGER_32 | FCR_RESETTF | FCR_RESETRF | FCR_ENABLE;
249 bus_space_write_4(iot, ioh, IXP425_UART_FCR, sc->sc_fcr);
250
251 if (iot == ixp4xx_comcn_sc.sc_iot
252 && sc->sc_baseaddr == ixp4xx_comcn_sc.sc_baseaddr) {
253 ixp4xx_comcn_sc.sc_attached = 1;
254
255 /* Make sure the console is always "hardwired". */
256 delay(20000); /* wait for output to finish */
257 SET(sc->sc_hwflags, COM_HW_CONSOLE);
258 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
259 }
260
261 #ifdef KGDB
262 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
263 ixp4xx_com_kgdb_attached = 1;
264 printf("%s: kgdb\n", sc->sc_dev.dv_xname);
265 ixp4xx_enable_debugport(sc);
266 return;
267 }
268 #endif
269
270 tp = ttymalloc();
271 tp->t_oproc = ixp4xx_comstart;
272 tp->t_param = ixp4xx_comparam;
273 tp->t_hwiflow = ixp4xx_comhwiflow;
274
275 sc->sc_tty = tp;
276 sc->sc_rbuf = malloc(IXPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
277 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
278 sc->sc_rbavail = IXPCOM_RING_SIZE;
279 if (sc->sc_rbuf == NULL) {
280 printf("%s: unable to allocate ring buffer\n",
281 sc->sc_dev.dv_xname);
282 return;
283 }
284 sc->sc_ebuf = sc->sc_rbuf + (IXPCOM_RING_SIZE << 1);
285
286 tty_attach(tp);
287
288 sc->sc_mcr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_MCR);
289 SET(sc->sc_mcr, MCR_IENABLE);
290 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_MCR, sc->sc_mcr);
291
292 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
293 int maj;
294
295 /* locate the major number */
296 maj = cdevsw_lookup_major(&ixpcom_cdevsw);
297 tp->t_dev = cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
298 printf("%s: %s (major=%d)\n", sc->sc_dev.dv_xname,
299 ixp4xx_comcn_sc.sc_name, maj);
300 }
301
302 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, ixp4xxcomsoft, sc);
303
304 #if NRND > 0 && defined(RND_COM)
305 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
306 RND_TYPE_TTY, 0);
307 #endif
308
309 /* if there are no enable/disable functions, assume the device
310 is always enabled */
311 if (!sc->enable)
312 sc->enabled = 1;
313
314 SET(sc->sc_hwflags, COM_HW_DEV_OK);
315 }
316
317 static int
318 ixp4xx_comparam(struct tty *tp, struct termios *t)
319 {
320 struct ixp4xx_com_softc *sc =
321 device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
322 int ospeed;
323 u_char lcr;
324 int s;
325
326 ospeed = ixp4xx_comspeed(t->c_ospeed, sc->sc_frequency);
327
328 /* Check requested parameters. */
329 if (ospeed < 0)
330 return EINVAL;
331 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
332 return EINVAL;
333
334 /*
335 * For the console, always force CLOCAL and !HUPCL, so that the port
336 * is always active.
337 */
338 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
339 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
340 SET(t->c_cflag, CLOCAL);
341 CLR(t->c_cflag, HUPCL);
342 }
343
344 /*
345 * If there were no changes, don't do anything. This avoids dropping
346 * input and improves performance when all we did was frob things like
347 * VMIN and VTIME.
348 */
349 if (tp->t_ospeed == t->c_ospeed &&
350 tp->t_cflag == t->c_cflag)
351 return (0);
352
353 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
354
355 s = splserial();
356 COM_LOCK(sc);
357
358 sc->sc_lcr = lcr;
359
360 /*
361 * If we're not in a mode that assumes a connection is present, then
362 * ignore carrier changes.
363 */
364 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
365 sc->sc_msr_dcd = 0;
366 else
367 sc->sc_msr_dcd = MSR_DCD;
368 /*
369 * Set the flow control pins depending on the current flow control
370 * mode.
371 */
372 if (ISSET(t->c_cflag, CRTSCTS)) {
373 sc->sc_mcr_dtr = MCR_DTR;
374 sc->sc_mcr_rts = MCR_RTS;
375 sc->sc_msr_cts = MSR_CTS;
376 } else if (ISSET(t->c_cflag, MDMBUF)) {
377 /*
378 * For DTR/DCD flow control, make sure we don't toggle DTR for
379 * carrier detection.
380 */
381 sc->sc_mcr_dtr = 0;
382 sc->sc_mcr_rts = MCR_DTR;
383 sc->sc_msr_cts = MSR_DCD;
384 } else {
385 /*
386 * If no flow control, then always set RTS. This will make
387 * the other side happy if it mistakenly thinks we're doing
388 * RTS/CTS flow control.
389 */
390 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
391 sc->sc_mcr_rts = 0;
392 sc->sc_msr_cts = 0;
393 if (ISSET(sc->sc_mcr, MCR_DTR))
394 SET(sc->sc_mcr, MCR_RTS);
395 else
396 CLR(sc->sc_mcr, MCR_RTS);
397 }
398 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
399
400 sc->sc_dlbl = ospeed;
401 sc->sc_dlbh = ospeed >> 8;
402
403 /*
404 * Set the FIFO threshold based on the receive speed.
405 *
406 * * If it's a low speed, it's probably a mouse or some other
407 * interactive device, so set the threshold low.
408 * * If it's a high speed, trim the trigger level down to prevent
409 * overflows.
410 * * Otherwise set it a bit higher.
411 */
412 #if 0
413 if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
414 sc->sc_fcr = FCR_ENABLE |
415 (t->c_ospeed <= 1200 ? FCR_TRIGGER_1 :
416 t->c_ospeed <= 38400 ? FCR_TRIGGER_16 : FCR_TRIGGER_8);
417 else
418 sc->sc_fcr = 0;
419 #endif
420
421 /* And copy to tty. */
422 tp->t_ispeed = 0;
423 tp->t_ospeed = t->c_ospeed;
424 tp->t_cflag = t->c_cflag;
425
426 if (!sc->sc_heldchange) {
427 if (sc->sc_tx_busy) {
428 sc->sc_heldtbc = sc->sc_tbc;
429 sc->sc_tbc = 0;
430 sc->sc_heldchange = 1;
431 } else
432 ixp4xx_com_set_cr(sc);
433 }
434
435 if (!ISSET(t->c_cflag, CHWFLOW)) {
436 /* Disable the high water mark. */
437 sc->sc_r_hiwat = 0;
438 sc->sc_r_lowat = 0;
439 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
440 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
441 ixpcom_schedrx(sc);
442 }
443 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
444 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
445 ixpcom_hwiflow(sc);
446 }
447 } else {
448 sc->sc_r_hiwat = ixpcom_rbuf_hiwat;
449 sc->sc_r_lowat = ixpcom_rbuf_lowat;
450 }
451
452 COM_UNLOCK(sc);
453 splx(s);
454
455 /*
456 * Update the tty layer's idea of the carrier bit.
457 * We tell tty the carrier is always on.
458 */
459 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
460
461 #ifdef COM_DEBUG
462 if (com_debug)
463 comstatus(sc, "comparam ");
464 #endif
465
466 if (!ISSET(t->c_cflag, CHWFLOW)) {
467 if (sc->sc_tx_stopped) {
468 sc->sc_tx_stopped = 0;
469 ixp4xx_comstart(tp);
470 }
471 }
472
473 return (0);
474 }
475
476 void
477 ixp4xx_com_set_cr(struct ixp4xx_com_softc *sc)
478 {
479 bus_space_tag_t iot = sc->sc_iot;
480 bus_space_handle_t ioh = sc->sc_ioh;
481
482 ixp4xx_com_iflush(sc);
483
484 bus_space_write_4(iot, ioh, IXP425_UART_IER, IER_UUE);
485
486 bus_space_write_4(iot, ioh, IXP425_UART_LCR, sc->sc_lcr | LCR_DLAB);
487 bus_space_write_4(iot, ioh, IXP425_UART_DLL, sc->sc_dlbl);
488 bus_space_write_4(iot, ioh, IXP425_UART_DLH, sc->sc_dlbh);
489 bus_space_write_4(iot, ioh, IXP425_UART_LCR, sc->sc_lcr);
490 bus_space_write_4(iot, ioh, IXP425_UART_MCR, sc->sc_mcr_active = sc->sc_mcr);
491 bus_space_write_4(iot, ioh, IXP425_UART_FCR, sc->sc_fcr);
492 bus_space_write_4(iot, ioh, IXP425_UART_IER, sc->sc_ier | IER_UUE);
493 }
494
495 static int
496 ixp4xx_comhwiflow(struct tty *tp, int block)
497 {
498 struct ixp4xx_com_softc *sc
499 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
500 int s;
501
502 if (COM_ISALIVE(sc) == 0)
503 return (0);
504
505 if (sc->sc_mcr_rts == 0)
506 return (0);
507
508 s = splserial();
509 COM_LOCK(sc);
510
511 if (block) {
512 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
513 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
514 ixpcom_hwiflow(sc);
515 }
516 } else {
517 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
518 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
519 ixpcom_schedrx(sc);
520 }
521 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
522 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
523 ixpcom_hwiflow(sc);
524 }
525 }
526
527 COM_UNLOCK(sc);
528 splx(s);
529 return (1);
530 }
531
532 /*
533 * (un)block input via hw flowcontrol
534 */
535 void
536 ixpcom_hwiflow(struct ixp4xx_com_softc *sc)
537 {
538 bus_space_tag_t iot = sc->sc_iot;
539 bus_space_handle_t ioh = sc->sc_ioh;
540
541 if (sc->sc_mcr_rts == 0)
542 return;
543
544 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
545 CLR(sc->sc_mcr, sc->sc_mcr_rts);
546 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
547 } else {
548 SET(sc->sc_mcr, sc->sc_mcr_rts);
549 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
550 }
551 bus_space_write_4(iot, ioh, IXP425_UART_MCR, sc->sc_mcr_active);
552 }
553
554 static void
555 ixp4xx_com_filltx(struct ixp4xx_com_softc *sc)
556 {
557 bus_space_tag_t iot = sc->sc_iot;
558 bus_space_handle_t ioh = sc->sc_ioh;
559 int n;
560
561 n = 0;
562 while (bus_space_read_4(iot, ioh, IXP425_UART_LSR) & LSR_TDRQ) {
563 if (n >= sc->sc_tbc)
564 break;
565 bus_space_write_4(iot, ioh, IXP425_UART_DATA,
566 0xff & *(sc->sc_tba + n));
567 n++;
568 }
569 sc->sc_tbc -= n;
570 sc->sc_tba += n;
571 }
572
573
574 static void
575 ixp4xx_comstart(struct tty *tp)
576 {
577 struct ixp4xx_com_softc *sc
578 = device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
579 int s;
580
581 if (COM_ISALIVE(sc) == 0)
582 return;
583
584 s = spltty();
585 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
586 goto out;
587 if (sc->sc_tx_stopped)
588 goto out;
589
590 if (tp->t_outq.c_cc <= tp->t_lowat) {
591 if (ISSET(tp->t_state, TS_ASLEEP)) {
592 CLR(tp->t_state, TS_ASLEEP);
593 wakeup(&tp->t_outq);
594 }
595 selwakeup(&tp->t_wsel);
596 if (tp->t_outq.c_cc == 0)
597 goto out;
598 }
599
600 /* Grab the first contiguous region of buffer space. */
601 {
602 u_char *tba;
603 int tbc;
604
605 tba = tp->t_outq.c_cf;
606 tbc = ndqb(&tp->t_outq, 0);
607
608 (void)splserial();
609 COM_LOCK(sc);
610
611 sc->sc_tba = tba;
612 sc->sc_tbc = tbc;
613 }
614
615 SET(tp->t_state, TS_BUSY);
616 sc->sc_tx_busy = 1;
617
618 /* Enable transmit completion interrupts if necessary. */
619 if (!ISSET(sc->sc_ier, IER_TIE)) {
620 SET(sc->sc_ier, IER_TIE);
621 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_IER,
622 sc->sc_ier);
623 }
624
625 /* Output the first chunk of the contiguous buffer. */
626 ixp4xx_com_filltx(sc);
627
628 COM_UNLOCK(sc);
629 out:
630 splx(s);
631 return;
632 }
633
634 inline static void
635 ixpcom_schedrx(struct ixp4xx_com_softc *sc)
636 {
637 sc->sc_rx_ready = 1;
638
639 /* Wake up the poller. */
640 softintr_schedule(sc->sc_si);
641 }
642
643 static void
644 ixp4xx_com_break(struct ixp4xx_com_softc *sc, int onoff)
645 {
646 if (onoff)
647 SET(sc->sc_lcr, LCR_SBREAK);
648 else
649 CLR(sc->sc_lcr, LCR_SBREAK);
650
651 if (!sc->sc_heldchange) {
652 if (sc->sc_tx_busy) {
653 sc->sc_heldtbc = sc->sc_tbc;
654 sc->sc_tbc = 0;
655 sc->sc_heldchange = 1;
656 } else
657 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_LCR,
658 sc->sc_lcr);
659 }
660 }
661
662 static void
663 ixp4xx_com_shutdown(struct ixp4xx_com_softc *sc)
664 {
665 struct tty *tp = sc->sc_tty;
666 int s;
667
668 s = splserial();
669 COM_LOCK(sc);
670
671 /* Clear any break condition set with TIOCSBRK. */
672 ixp4xx_com_break(sc, 0);
673
674 /*
675 * Hang up if necessary. Wait a bit, so the other side has time to
676 * notice even if we immediately open the port again.
677 * Avoid tsleeping above splhigh().
678 */
679 if (ISSET(tp->t_cflag, HUPCL)) {
680 ixp4xx_com_modem(sc, 0);
681 COM_UNLOCK(sc);
682 splx(s);
683 /* XXX tsleep will only timeout */
684 (void) tsleep(sc, TTIPRI, ttclos, hz);
685 s = splserial();
686 COM_LOCK(sc);
687 }
688
689 /* Turn off interrupts. */
690 #if 1
691 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
692 sc->sc_ier = IER_RAVIE | IER_RTOIE;
693 } else
694 sc->sc_ier = IER_UUE;
695 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_IER,
696 sc->sc_ier);
697 #else
698 sc->sc_ier &= ~(IER_RAVIE | IER_TIE);
699 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_IER,
700 sc->sc_ier | IER_UUE);
701 #endif
702
703 if (sc->disable) {
704 #ifdef DIAGNOSTIC
705 if (!sc->enabled)
706 panic("ixpcom_shutdown: not enabled?");
707 #endif
708 (*sc->disable)(sc);
709 sc->enabled = 0;
710 }
711 COM_UNLOCK(sc);
712 splx(s);
713 }
714
715 int
716 ixp4xx_comopen(dev_t dev, int flag, int mode, struct lwp *l)
717 {
718 struct ixp4xx_com_softc *sc;
719 struct tty *tp;
720 int s, s2;
721 int error;
722
723 sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
724 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
725 sc->sc_rbuf == NULL)
726 return (ENXIO);
727
728 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
729 return (ENXIO);
730
731 #ifdef KGDB
732 /*
733 * If this is the kgdb port, no other use is permitted.
734 */
735 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
736 return (EBUSY);
737 #endif
738
739 tp = sc->sc_tty;
740
741 if (ISSET(tp->t_state, TS_ISOPEN) &&
742 ISSET(tp->t_state, TS_XCLUDE) &&
743 l->l_proc->p_ucred->cr_uid != 0)
744 return (EBUSY);
745
746 s = spltty();
747
748 /*
749 * Do the following iff this is a first open.
750 */
751 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
752 struct termios t;
753
754 tp->t_dev = dev;
755
756 s2 = splserial();
757 COM_LOCK(sc);
758
759 if (sc->enable) {
760 if ((*sc->enable)(sc)) {
761 COM_UNLOCK(sc);
762 splx(s2);
763 splx(s);
764 printf("%s: device enable failed\n",
765 sc->sc_dev.dv_xname);
766 return (EIO);
767 }
768 sc->enabled = 1;
769 }
770 /* Turn on interrupts. */
771 SET(sc->sc_ier, IER_RAVIE | IER_RTOIE | IER_RLSE | IER_RIE);
772 bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_IER, sc->sc_ier);
773
774 /* Fetch the current modem control status, needed later. */
775 sc->sc_msr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IXP425_UART_MSR);
776
777 COM_UNLOCK(sc);
778 splx(s2);
779
780 /*
781 * Initialize the termios status to the defaults. Add in the
782 * sticky bits from TIOCSFLAGS.
783 */
784 t.c_ispeed = 0;
785 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
786 t.c_ospeed = ixp4xx_comcn_sc.sc_ospeed;
787 t.c_cflag = ixp4xx_comcn_sc.sc_cflag;
788 } else {
789 t.c_ospeed = TTYDEF_SPEED;
790 t.c_cflag = TTYDEF_CFLAG;
791 }
792 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
793 SET(t.c_cflag, CLOCAL);
794 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
795 SET(t.c_cflag, CRTSCTS);
796 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
797 SET(t.c_cflag, MDMBUF);
798 /* Make sure ixpcomparam() will do something. */
799 tp->t_ospeed = 0;
800 (void) ixp4xx_comparam(tp, &t);
801 tp->t_iflag = TTYDEF_IFLAG;
802 tp->t_oflag = TTYDEF_OFLAG;
803 tp->t_lflag = TTYDEF_LFLAG;
804 ttychars(tp);
805 ttsetwater(tp);
806
807 s2 = splserial();
808 COM_LOCK(sc);
809
810 /*
811 * Turn on DTR. We must always do this, even if carrier is not
812 * present, because otherwise we'd have to use TIOCSDTR
813 * immediately after setting CLOCAL, which applications do not
814 * expect. We always assert DTR while the device is open
815 * unless explicitly requested to deassert it.
816 */
817 ixp4xx_com_modem(sc, 1);
818 /* Clear the input ring, and unblock. */
819 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
820 sc->sc_rbavail = IXPCOM_RING_SIZE;
821 ixp4xx_com_iflush(sc);
822 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
823 ixpcom_hwiflow(sc);
824
825 #ifdef COM_DEBUG
826 if (com_debug)
827 comstatus(sc, "ixp4xx_comopen ");
828 #endif
829
830 COM_UNLOCK(sc);
831 splx(s2);
832 }
833 splx(s);
834
835 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
836 if (error)
837 goto bad;
838
839 error = (*tp->t_linesw->l_open)(dev, tp);
840 if (error)
841 goto bad;
842
843 return (0);
844
845 bad:
846 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
847 /*
848 * We failed to open the device, and nobody else had it opened.
849 * Clean up the state as appropriate.
850 */
851 ixp4xx_com_shutdown(sc);
852 }
853 return (error);
854 }
855
856 int
857 ixp4xx_comclose(dev_t dev, int flag, int mode, struct lwp *l)
858 {
859 struct ixp4xx_com_softc *sc =
860 device_lookup(&ixpcom_cd, COMUNIT(dev));
861 struct tty *tp = sc->sc_tty;
862
863 /* XXX This is for cons.c. */
864 if (!ISSET(tp->t_state, TS_ISOPEN))
865 return (0);
866
867 (*tp->t_linesw->l_close)(tp, flag);
868 ttyclose(tp);
869
870 if (COM_ISALIVE(sc) == 0)
871 return (0);
872
873 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
874 /*
875 * Although we got a last close, the device may still be in
876 * use; e.g. if this was the dialout node, and there are still
877 * processes waiting for carrier on the non-dialout node.
878 */
879 ixp4xx_com_shutdown(sc);
880 }
881
882 return (0);
883 }
884
885 int
886 ixp4xx_comread(dev_t dev, struct uio *uio, int flag)
887 {
888 struct ixp4xx_com_softc *sc =
889 device_lookup(&ixpcom_cd, COMUNIT(dev));
890 struct tty *tp = sc->sc_tty;
891
892 if (COM_ISALIVE(sc) == 0)
893 return (EIO);
894
895 return ((*tp->t_linesw->l_read)(tp, uio, flag));
896 }
897
898 int
899 ixp4xx_comwrite(dev_t dev, struct uio *uio, int flag)
900 {
901 struct ixp4xx_com_softc *sc =
902 device_lookup(&ixpcom_cd, COMUNIT(dev));
903 struct tty *tp = sc->sc_tty;
904
905 if (COM_ISALIVE(sc) == 0)
906 return (EIO);
907
908 return ((*tp->t_linesw->l_write)(tp, uio, flag));
909 }
910
911 int
912 ixp4xx_compoll(dev_t dev, int events, struct lwp *l)
913 {
914 struct ixp4xx_com_softc *sc =
915 device_lookup(&ixpcom_cd, COMUNIT(dev));
916 struct tty *tp = sc->sc_tty;
917
918 if (COM_ISALIVE(sc) == 0)
919 return (EIO);
920
921 return ((*tp->t_linesw->l_poll)(tp, events, l));
922 }
923
924 struct tty *
925 ixp4xx_comtty(dev_t dev)
926 {
927 struct ixp4xx_com_softc *sc =
928 device_lookup(&ixpcom_cd, COMUNIT(dev));
929 struct tty *tp = sc->sc_tty;
930
931 return (tp);
932 }
933
934 int
935 ixp4xx_comioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
936 {
937 struct ixp4xx_com_softc *sc =
938 device_lookup(&ixpcom_cd, COMUNIT(dev));
939 struct tty *tp = sc->sc_tty;
940 int error;
941 int s;
942
943 if (COM_ISALIVE(sc) == 0)
944 return (EIO);
945
946 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
947 if (error != EPASSTHROUGH)
948 return (error);
949
950 error = ttioctl(tp, cmd, data, flag, l);
951 if (error != EPASSTHROUGH)
952 return (error);
953
954 error = 0;
955
956 s = splserial();
957 COM_LOCK(sc);
958
959 switch (cmd) {
960 case TIOCSBRK:
961 ixp4xx_com_break(sc, 1);
962 break;
963
964 case TIOCCBRK:
965 ixp4xx_com_break(sc, 0);
966 break;
967
968 case TIOCSDTR:
969 ixp4xx_com_modem(sc, 1);
970 break;
971
972 case TIOCCDTR:
973 ixp4xx_com_modem(sc, 0);
974 break;
975
976 case TIOCGFLAGS:
977 *(int *)data = sc->sc_swflags;
978 break;
979
980 case TIOCSFLAGS:
981 error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
982 if (error)
983 break;
984 sc->sc_swflags = *(int *)data;
985 break;
986
987 case TIOCMSET:
988 case TIOCMBIS:
989 case TIOCMBIC:
990 tiocm_to_ixp4xx_com(sc, cmd, *(int *)data);
991 break;
992
993 case TIOCMGET:
994 *(int *)data = ixp4xx_com_to_tiocm(sc);
995 break;
996
997 default:
998 error = EPASSTHROUGH;
999 break;
1000 }
1001
1002 COM_UNLOCK(sc);
1003 splx(s);
1004
1005 #ifdef COM_DEBUG
1006 if (com_debug)
1007 comstatus(sc, "comioctl ");
1008 #endif
1009
1010 return (error);
1011 }
1012
1013 void
1014 ixp4xx_comstop(struct tty *tp, int flag)
1015 {
1016 struct ixp4xx_com_softc *sc =
1017 device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
1018 int s;
1019
1020 s = splserial();
1021 COM_LOCK(sc);
1022 if (ISSET(tp->t_state, TS_BUSY)) {
1023 /* Stop transmitting at the next chunk. */
1024 sc->sc_tbc = 0;
1025 sc->sc_heldtbc = 0;
1026 if (!ISSET(tp->t_state, TS_TTSTOP))
1027 SET(tp->t_state, TS_FLUSH);
1028 }
1029 COM_UNLOCK(sc);
1030 splx(s);
1031 }
1032
1033 static void
1034 ixp4xx_com_modem(struct ixp4xx_com_softc *sc, int onoff)
1035 {
1036 bus_space_tag_t iot = sc->sc_iot;
1037 bus_space_handle_t ioh = sc->sc_ioh;
1038
1039 if (sc->sc_mcr_dtr == 0)
1040 return;
1041
1042 if (onoff)
1043 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1044 else
1045 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1046
1047 if (!sc->sc_heldchange) {
1048 if (sc->sc_tx_busy) {
1049 sc->sc_heldtbc = sc->sc_tbc;
1050 sc->sc_tbc = 0;
1051 sc->sc_heldchange = 1;
1052 } else
1053 bus_space_write_4(iot, ioh, IXP425_UART_MCR, sc->sc_mcr);
1054 }
1055 }
1056
1057 static void
1058 tiocm_to_ixp4xx_com(struct ixp4xx_com_softc *sc, u_long how, int ttybits)
1059 {
1060 bus_space_tag_t iot = sc->sc_iot;
1061 bus_space_handle_t ioh = sc->sc_ioh;
1062 u_char combits;
1063
1064 combits = 0;
1065 if (ISSET(ttybits, TIOCM_DTR))
1066 SET(combits, MCR_DTR);
1067 if (ISSET(ttybits, TIOCM_RTS))
1068 SET(combits, MCR_RTS);
1069
1070 switch (how) {
1071 case TIOCMBIC:
1072 CLR(sc->sc_mcr, combits);
1073 break;
1074
1075 case TIOCMBIS:
1076 SET(sc->sc_mcr, combits);
1077 break;
1078
1079 case TIOCMSET:
1080 CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
1081 SET(sc->sc_mcr, combits);
1082 break;
1083 }
1084
1085 if (!sc->sc_heldchange) {
1086 if (sc->sc_tx_busy) {
1087 sc->sc_heldtbc = sc->sc_tbc;
1088 sc->sc_tbc = 0;
1089 sc->sc_heldchange = 1;
1090 } else
1091 bus_space_write_4(iot, ioh, IXP425_UART_MCR, sc->sc_mcr);
1092 }
1093 }
1094
1095 static int
1096 ixp4xx_com_to_tiocm(struct ixp4xx_com_softc *sc)
1097 {
1098 u_char combits;
1099 int ttybits = 0;
1100
1101 combits = sc->sc_mcr;
1102 if (ISSET(combits, MCR_DTR))
1103 SET(ttybits, TIOCM_DTR);
1104 if (ISSET(combits, MCR_RTS))
1105 SET(ttybits, TIOCM_RTS);
1106
1107 combits = sc->sc_msr;
1108 if (ISSET(combits, MSR_DCD))
1109 SET(ttybits, TIOCM_CD);
1110 if (ISSET(combits, MSR_CTS))
1111 SET(ttybits, TIOCM_CTS);
1112 if (ISSET(combits, MSR_DSR))
1113 SET(ttybits, TIOCM_DSR);
1114 if (ISSET(combits, MSR_RI | MSR_TERI))
1115 SET(ttybits, TIOCM_RI);
1116
1117 if ((sc->sc_ier & 0x0f) != 0)
1118 SET(ttybits, TIOCM_LE);
1119
1120 return (ttybits);
1121 }
1122
1123 static u_int
1124 cflag2lcr(tcflag_t cflag)
1125 {
1126 u_char lcr = 0;
1127
1128 switch (ISSET(cflag, CSIZE)) {
1129 case CS5:
1130 SET(lcr, LCR_5BITS);
1131 break;
1132 case CS6:
1133 SET(lcr, LCR_6BITS);
1134 break;
1135 case CS7:
1136 SET(lcr, LCR_7BITS);
1137 break;
1138 case CS8:
1139 SET(lcr, LCR_8BITS);
1140 break;
1141 }
1142 if (ISSET(cflag, PARENB)) {
1143 SET(lcr, LCR_PENE);
1144 if (!ISSET(cflag, PARODD))
1145 SET(lcr, LCR_PODD);
1146 }
1147 if (ISSET(cflag, CSTOPB))
1148 SET(lcr, LCR_1STOP);
1149
1150 return (lcr);
1151 }
1152
1153 static void
1154 ixp4xx_com_iflush(struct ixp4xx_com_softc *sc)
1155 {
1156 bus_space_tag_t iot = sc->sc_iot;
1157 bus_space_handle_t ioh = sc->sc_ioh;
1158 #ifdef DIAGNOSTIC
1159 int reg;
1160 #endif
1161 int timo;
1162
1163 #ifdef DIAGNOSTIC
1164 reg = 0xffff;
1165 #endif
1166 timo = 50000;
1167 /* flush any pending I/O */
1168 while (ISSET(bus_space_read_4(iot, ioh, IXP425_UART_LSR), LSR_DR)
1169 && --timo)
1170 #ifdef DIAGNOSTIC
1171 reg =
1172 #else
1173 (void)
1174 #endif
1175 bus_space_read_4(iot, ioh, IXP425_UART_DATA);
1176 #ifdef DIAGNOSTIC
1177 if (!timo)
1178 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
1179 reg);
1180 #endif
1181 }
1182
1183 static void
1184 ixp4xxcomsoft(void* arg)
1185 {
1186 struct ixp4xx_com_softc *sc = arg;
1187
1188 if (COM_ISALIVE(sc) == 0)
1189 return;
1190
1191 if (sc->sc_rx_ready) {
1192 sc->sc_rx_ready = 0;
1193 ixp4xx_com_rxsoft(sc, sc->sc_tty);
1194 }
1195 if (sc->sc_tx_done) {
1196 sc->sc_tx_done = 0;
1197 ixp4xx_com_txsoft(sc, sc->sc_tty);
1198 }
1199 }
1200
1201 inline static void
1202 ixp4xx_com_txsoft(struct ixp4xx_com_softc *sc, struct tty *tp)
1203 {
1204 CLR(tp->t_state, TS_BUSY);
1205 if (ISSET(tp->t_state, TS_FLUSH))
1206 CLR(tp->t_state, TS_FLUSH);
1207 else
1208 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1209 (*tp->t_linesw->l_start)(tp);
1210 }
1211
1212 void
1213 ixpcomdiag(void *arg)
1214 {
1215 struct ixp4xx_com_softc *sc = arg;
1216 int overflows, floods;
1217 int s;
1218
1219 s = splserial();
1220 COM_LOCK(sc);
1221 overflows = sc->sc_overflows;
1222 sc->sc_overflows = 0;
1223 floods = sc->sc_floods;
1224 sc->sc_floods = 0;
1225 sc->sc_errors = 0;
1226 COM_UNLOCK(sc);
1227 splx(s);
1228
1229 printf("%s: %d silo overflow%s, %d ibuf flood%s\n",
1230 sc->sc_dev.dv_xname,
1231 overflows, overflows == 1 ? "" : "s",
1232 floods, floods == 1 ? "" : "s");
1233 }
1234
1235 inline static void
1236 ixp4xx_com_rxsoft(struct ixp4xx_com_softc *sc, struct tty *tp)
1237 {
1238 int (*rint) __P((int c, struct tty *tp)) = tp->t_linesw->l_rint;
1239 u_char *get, *end;
1240 u_int cc, scc;
1241 u_char lsr;
1242 int code;
1243 int s;
1244
1245 end = sc->sc_ebuf;
1246 get = sc->sc_rbget;
1247 scc = cc = IXPCOM_RING_SIZE - sc->sc_rbavail;
1248 while (cc) {
1249 code = get[0];
1250 lsr = get[1];
1251 if (ISSET(lsr, LSR_OE | LSR_FE | LSR_PE | LSR_BI)) {
1252 if (ISSET(lsr, LSR_OE)) {
1253 sc->sc_overflows++;
1254 if (sc->sc_errors++ == 0)
1255 callout_reset(&sc->sc_diag_callout,
1256 60 * hz, ixpcomdiag, sc);
1257 }
1258 if (ISSET(lsr, LSR_BI | LSR_FE))
1259 SET(code, TTY_FE);
1260 if (ISSET(lsr, LSR_PE))
1261 SET(code, TTY_PE);
1262 }
1263 if ((*rint)(code, tp) == -1) {
1264 /*
1265 * The line discipline's buffer is out of space.
1266 */
1267 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1268 /*
1269 * We're either not using flow control, or the
1270 * line discipline didn't tell us to block for
1271 * some reason. Either way, we have no way to
1272 * know when there's more space available, so
1273 * just drop the rest of the data.
1274 */
1275 get += cc << 1;
1276 if (get >= end)
1277 get -= IXPCOM_RING_SIZE << 1;
1278 cc = 0;
1279 } else {
1280 /*
1281 * Don't schedule any more receive processing
1282 * until the line discipline tells us there's
1283 * space available (through comhwiflow()).
1284 * Leave the rest of the data in the input
1285 * buffer.
1286 */
1287 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1288 }
1289 break;
1290 }
1291 get += 2;
1292 if (get >= end)
1293 get = sc->sc_rbuf;
1294 cc--;
1295 }
1296
1297 if (cc != scc) {
1298 sc->sc_rbget = get;
1299 s = splserial();
1300 COM_LOCK(sc);
1301
1302 cc = sc->sc_rbavail += scc - cc;
1303 /* Buffers should be ok again, release possible block. */
1304 if (cc >= 1) {
1305 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1306 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1307 SET(sc->sc_ier, IER_RAVIE | IER_RTOIE);
1308 ixp4xx_com_set_cr(sc);
1309 }
1310 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1311 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1312 ixpcom_hwiflow(sc);
1313 }
1314 }
1315 COM_UNLOCK(sc);
1316 splx(s);
1317 }
1318 }
1319
1320 int
1321 ixp4xxcomintr(void* arg)
1322 {
1323 struct ixp4xx_com_softc *sc = arg;
1324 bus_space_tag_t iot = sc->sc_iot;
1325 bus_space_handle_t ioh = sc->sc_ioh;
1326 u_char *put, *end;
1327 u_int cc;
1328 u_char iir, lsr;
1329
1330 if (COM_ISALIVE(sc) == 0)
1331 return (0);
1332
1333 COM_LOCK(sc);
1334 iir = bus_space_read_4(iot, ioh, IXP425_UART_IIR);
1335 if (ISSET(iir, IIR_NOPEND)) {
1336 COM_UNLOCK(sc);
1337 return (0);
1338 }
1339
1340 end = sc->sc_ebuf;
1341 put = sc->sc_rbput;
1342 cc = sc->sc_rbavail;
1343
1344 again: do {
1345 u_char msr;
1346
1347 lsr = bus_space_read_4(iot, ioh, IXP425_UART_LSR);
1348 if (ISSET(lsr, LSR_BI)) {
1349 int cn_trapped = 0;
1350
1351 cn_check_magic(sc->sc_tty->t_dev,
1352 CNC_BREAK, ixp4xx_com_cnm_state);
1353 if (cn_trapped)
1354 continue;
1355 }
1356
1357 if (ISSET(lsr, LSR_RCV_MASK) &&
1358 !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1359 while (cc > 0) {
1360 int cn_trapped = 0;
1361 put[0] = bus_space_read_4(iot, ioh, IXP425_UART_DATA);
1362 put[1] = lsr;
1363 cn_check_magic(sc->sc_tty->t_dev,
1364 put[0], ixp4xx_com_cnm_state);
1365 if (cn_trapped) {
1366 lsr = bus_space_read_4(iot, ioh,
1367 IXP425_UART_LSR);
1368 if (!ISSET(lsr, LSR_RCV_MASK))
1369 break;
1370
1371 continue;
1372 }
1373 put += 2;
1374 if (put >= end)
1375 put = sc->sc_rbuf;
1376 cc--;
1377 lsr = bus_space_read_4(iot, ioh, IXP425_UART_LSR);
1378 if (!ISSET(lsr, LSR_RCV_MASK))
1379 break;
1380 }
1381
1382 /*
1383 * Current string of incoming characters ended because
1384 * no more data was available or we ran out of space.
1385 * Schedule a receive event if any data was received.
1386 * If we're out of space, turn off receive interrupts.
1387 */
1388 sc->sc_rbput = put;
1389 sc->sc_rbavail = cc;
1390 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1391 sc->sc_rx_ready = 1;
1392
1393 /*
1394 * See if we are in danger of overflowing a buffer. If
1395 * so, use hardware flow control to ease the pressure.
1396 */
1397
1398 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1399 cc < sc->sc_r_hiwat) {
1400 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1401 ixpcom_hwiflow(sc);
1402 }
1403
1404 /*
1405 * If we're out of space, disable receive interrupts
1406 * until the queue has drained a bit.
1407 */
1408 if (!cc) {
1409 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1410 CLR(sc->sc_ier, IER_RAVIE | IER_RTOIE);
1411 ixp4xx_com_set_cr(sc);
1412 }
1413 } else {
1414 if ((iir & IIR_IMASK) == IIR_RXRDY) {
1415 sc->sc_ier = IER_UUE;
1416 delay(10);
1417 ixp4xx_com_set_cr(sc);
1418 }
1419 }
1420
1421 msr = bus_space_read_4(iot, ioh, IXP425_UART_MSR);
1422 sc->sc_msr = msr;
1423
1424 } while (ISSET((iir = bus_space_read_4(iot, ioh, IXP425_UART_IIR)),
1425 IIR_RXRDY) || ((iir & IIR_IMASK) == 0));
1426
1427 /*
1428 * Done handling any receive interrupts. See if data can be
1429 * transmitted as well. Schedule tx done event if no data left
1430 * and tty was marked busy.
1431 */
1432 if (ISSET(lsr, LSR_TDRQ)) {
1433 /*
1434 * If we've delayed a parameter change, do it now, and restart
1435 * output.
1436 */
1437 if (sc->sc_heldchange) {
1438 ixp4xx_com_set_cr(sc);
1439 sc->sc_heldchange = 0;
1440 sc->sc_tbc = sc->sc_heldtbc;
1441 sc->sc_heldtbc = 0;
1442 }
1443
1444 /* Output the next chunk of the contiguous buffer, if any. */
1445 if (sc->sc_tbc > 0) {
1446 ixp4xx_com_filltx(sc);
1447 } else {
1448 /* Disable transmit completion interrupts if necessary. */
1449 if (ISSET(sc->sc_ier, IER_TIE)) {
1450 CLR(sc->sc_ier, IER_TIE);
1451 ixp4xx_com_set_cr(sc);
1452 }
1453 if (sc->sc_tx_busy) {
1454 sc->sc_tx_busy = 0;
1455 sc->sc_tx_done = 1;
1456 }
1457 }
1458 }
1459
1460 if (!ISSET((iir = bus_space_read_4(iot, ioh, IXP425_UART_IIR)), IIR_NOPEND))
1461 goto again;
1462
1463 COM_UNLOCK(sc);
1464
1465 /* Wake up the poller. */
1466 softintr_schedule(sc->sc_si);
1467
1468 #if NRND > 0 && defined(RND_COM)
1469 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1470 #endif
1471 return (1);
1472 }
1473
1474 /*
1475 * Initialize UART for use as console
1476 */
1477 int
1478 ixp4xx_cominit(bus_space_tag_t iot, bus_space_handle_t ioh, int rate,
1479 int frequency, tcflag_t cflag)
1480 {
1481 bus_space_write_4(iot, ioh, IXP425_UART_IER, IER_UUE);
1482
1483 bus_space_write_4(iot, ioh, IXP425_UART_LCR, LCR_DLAB);
1484 rate = ixp4xx_comspeed(rate, frequency);
1485 bus_space_write_4(iot, ioh, IXP425_UART_DLL, rate);
1486 bus_space_write_4(iot, ioh, IXP425_UART_DLH, rate >> 8);
1487 bus_space_write_4(iot, ioh, IXP425_UART_LCR, cflag2lcr(cflag));
1488 bus_space_write_4(iot, ioh, IXP425_UART_MCR, MCR_DTR | MCR_RTS);
1489 bus_space_write_4(iot, ioh, IXP425_UART_FCR,
1490 FCR_ENABLE | FCR_RESETRF | FCR_RESETTF | FCR_TRIGGER_1);
1491
1492 return (0);
1493 }
1494
1495
1496 /*
1497 * There are routines needed to act as console
1498 */
1499
1500 struct consdev ixp4xx_comcons = {
1501 NULL, NULL, ixp4xx_comcngetc, ixp4xx_comcnputc, ixp4xx_comcnpollc,
1502 NULL, NULL, NULL, NODEV, CN_NORMAL
1503 };
1504
1505 int
1506 ixp4xx_comcnattach(bus_space_tag_t iot, const struct uart_info *config,
1507 int rate, int frequency, tcflag_t cflag)
1508 {
1509
1510 bus_addr_t iobase = config->hw_addr;
1511 bus_space_handle_t ioh = config->v_addr;
1512 char *name = (char *)config->name;
1513
1514 int res;
1515 res = ixp4xx_cominit(iot, ioh, rate, frequency, cflag);
1516 if (res)
1517 return (res);
1518
1519 cn_tab = &ixp4xx_comcons;
1520 cn_init_magic(&ixp4xx_com_cnm_state);
1521
1522 /* default magic is a couple of BREAK. */
1523 cn_set_magic("\047\001\047\001");
1524
1525 ixp4xx_comcn_sc.sc_iot = iot;
1526 ixp4xx_comcn_sc.sc_ioh = ioh;
1527 ixp4xx_comcn_sc.sc_baseaddr = iobase;
1528 ixp4xx_comcn_sc.sc_ospeed = rate;
1529 ixp4xx_comcn_sc.sc_cflag = cflag;
1530 ixp4xx_comcn_sc.sc_name = name;
1531 return (0);
1532 }
1533
1534 void
1535 ixp4xx_comcnputc(dev_t dev, int c)
1536 {
1537 bus_space_tag_t iot = ixp4xx_comcn_sc.sc_iot;
1538 bus_space_handle_t ioh = ixp4xx_comcn_sc.sc_ioh;
1539 int s;
1540
1541 s = splserial();
1542
1543 while(!(bus_space_read_4(iot, ioh, IXP425_UART_LSR) & LSR_TDRQ))
1544 ;
1545
1546 bus_space_write_4(iot, ioh, IXP425_UART_DATA, c);
1547
1548 splx(s);
1549 }
1550
1551 int
1552 ixp4xx_comcngetc(dev_t dev)
1553 {
1554 int c,s;
1555 bus_space_tag_t iot = ixp4xx_comcn_sc.sc_iot;
1556 bus_space_handle_t ioh = ixp4xx_comcn_sc.sc_ioh;
1557
1558 s = splserial();
1559
1560 while(!(bus_space_read_4(iot, ioh, IXP425_UART_LSR) & LSR_DR))
1561 ;
1562 c = bus_space_read_4(iot, ioh, IXP425_UART_DATA);
1563 c &= 0xff;
1564 splx(s);
1565
1566 return (c);
1567 }
1568
1569
1570 void
1571 ixp4xx_comcnprobe(struct consdev *cp)
1572 {
1573 cp->cn_pri = CN_REMOTE;
1574 }
1575
1576 void
1577 ixp4xx_comcnpollc(dev_t dev, int on)
1578 {
1579 }
1580