clmpcc.c revision 1.22.6.1 1 /* $NetBSD: clmpcc.c,v 1.22.6.1 2005/01/26 08:20:26 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Steve C. Woodford.
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Cirrus Logic CD2400/CD2401 Four Channel Multi-Protocol Comms. Controller.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: clmpcc.c,v 1.22.6.1 2005/01/26 08:20:26 skrll Exp $");
45
46 #include "opt_ddb.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ioctl.h>
51 #include <sys/select.h>
52 #include <sys/tty.h>
53 #include <sys/proc.h>
54 #include <sys/user.h>
55 #include <sys/conf.h>
56 #include <sys/file.h>
57 #include <sys/uio.h>
58 #include <sys/kernel.h>
59 #include <sys/syslog.h>
60 #include <sys/device.h>
61 #include <sys/malloc.h>
62
63 #include <machine/bus.h>
64 #include <machine/intr.h>
65 #include <machine/param.h>
66
67 #include <dev/ic/clmpccreg.h>
68 #include <dev/ic/clmpccvar.h>
69 #include <dev/cons.h>
70
71
72 #if defined(CLMPCC_ONLY_BYTESWAP_LOW) && defined(CLMPCC_ONLY_BYTESWAP_HIGH)
73 #error "CLMPCC_ONLY_BYTESWAP_LOW and CLMPCC_ONLY_BYTESWAP_HIGH are mutually exclusive."
74 #endif
75
76
77 static int clmpcc_init __P((struct clmpcc_softc *sc));
78 static void clmpcc_shutdown __P((struct clmpcc_chan *));
79 static int clmpcc_speed __P((struct clmpcc_softc *, speed_t,
80 int *, int *));
81 static int clmpcc_param __P((struct tty *, struct termios *));
82 static void clmpcc_set_params __P((struct clmpcc_chan *));
83 static void clmpcc_start __P((struct tty *));
84 static int clmpcc_modem_control __P((struct clmpcc_chan *, int, int));
85
86 #define CLMPCCUNIT(x) (minor(x) & 0x7fffc)
87 #define CLMPCCCHAN(x) (minor(x) & 0x00003)
88 #define CLMPCCDIALOUT(x) (minor(x) & 0x80000)
89
90 /*
91 * These should be in a header file somewhere...
92 */
93 #define ISSET(v, f) (((v) & (f)) != 0)
94 #define ISCLR(v, f) (((v) & (f)) == 0)
95 #define SET(v, f) (v) |= (f)
96 #define CLR(v, f) (v) &= ~(f)
97
98
99 extern struct cfdriver clmpcc_cd;
100
101 dev_type_open(clmpccopen);
102 dev_type_close(clmpccclose);
103 dev_type_read(clmpccread);
104 dev_type_write(clmpccwrite);
105 dev_type_ioctl(clmpccioctl);
106 dev_type_stop(clmpccstop);
107 dev_type_tty(clmpcctty);
108 dev_type_poll(clmpccpoll);
109
110 const struct cdevsw clmpcc_cdevsw = {
111 clmpccopen, clmpccclose, clmpccread, clmpccwrite, clmpccioctl,
112 clmpccstop, clmpcctty, clmpccpoll, nommap, ttykqfilter, D_TTY
113 };
114
115 /*
116 * Make this an option variable one can patch.
117 */
118 u_int clmpcc_ibuf_size = CLMPCC_RING_SIZE;
119
120
121 /*
122 * Things needed when the device is used as a console
123 */
124 static struct clmpcc_softc *cons_sc = NULL;
125 static int cons_chan;
126 static int cons_rate;
127
128 static int clmpcc_common_getc __P((struct clmpcc_softc *, int));
129 static void clmpcc_common_putc __P((struct clmpcc_softc *, int, int));
130 int clmpcccngetc __P((dev_t));
131 void clmpcccnputc __P((dev_t, int));
132
133
134 /*
135 * Convenience functions, inlined for speed
136 */
137 #define integrate static inline
138 integrate u_int8_t clmpcc_rdreg __P((struct clmpcc_softc *, u_int));
139 integrate void clmpcc_wrreg __P((struct clmpcc_softc *, u_int, u_int));
140 integrate u_int8_t clmpcc_rdreg_odd __P((struct clmpcc_softc *, u_int));
141 integrate void clmpcc_wrreg_odd __P((struct clmpcc_softc *, u_int, u_int));
142 integrate void clmpcc_wrtx_multi __P((struct clmpcc_softc *, u_int8_t *,
143 u_int));
144 integrate u_int8_t clmpcc_select_channel __P((struct clmpcc_softc *, u_int));
145 integrate void clmpcc_channel_cmd __P((struct clmpcc_softc *,int,int));
146 integrate void clmpcc_enable_transmitter __P((struct clmpcc_chan *));
147
148 #define clmpcc_rd_msvr(s) clmpcc_rdreg_odd(s,CLMPCC_REG_MSVR)
149 #define clmpcc_wr_msvr(s,r,v) clmpcc_wrreg_odd(s,r,v)
150 #define clmpcc_wr_pilr(s,r,v) clmpcc_wrreg_odd(s,r,v)
151 #define clmpcc_rd_rxdata(s) clmpcc_rdreg_odd(s,CLMPCC_REG_RDR)
152 #define clmpcc_wr_txdata(s,v) clmpcc_wrreg_odd(s,CLMPCC_REG_TDR,v)
153
154
155 integrate u_int8_t
156 clmpcc_rdreg(sc, offset)
157 struct clmpcc_softc *sc;
158 u_int offset;
159 {
160 #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
161 offset ^= sc->sc_byteswap;
162 #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
163 offset ^= CLMPCC_BYTESWAP_HIGH;
164 #endif
165 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
166 }
167
168 integrate void
169 clmpcc_wrreg(sc, offset, val)
170 struct clmpcc_softc *sc;
171 u_int offset;
172 u_int val;
173 {
174 #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
175 offset ^= sc->sc_byteswap;
176 #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
177 offset ^= CLMPCC_BYTESWAP_HIGH;
178 #endif
179 bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, val);
180 }
181
182 integrate u_int8_t
183 clmpcc_rdreg_odd(sc, offset)
184 struct clmpcc_softc *sc;
185 u_int offset;
186 {
187 #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
188 offset ^= (sc->sc_byteswap & 2);
189 #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
190 offset ^= (CLMPCC_BYTESWAP_HIGH & 2);
191 #endif
192 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, offset);
193 }
194
195 integrate void
196 clmpcc_wrreg_odd(sc, offset, val)
197 struct clmpcc_softc *sc;
198 u_int offset;
199 u_int val;
200 {
201 #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
202 offset ^= (sc->sc_byteswap & 2);
203 #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
204 offset ^= (CLMPCC_BYTESWAP_HIGH & 2);
205 #endif
206 bus_space_write_1(sc->sc_iot, sc->sc_ioh, offset, val);
207 }
208
209 integrate void
210 clmpcc_wrtx_multi(sc, buff, count)
211 struct clmpcc_softc *sc;
212 u_int8_t *buff;
213 u_int count;
214 {
215 u_int offset = CLMPCC_REG_TDR;
216
217 #if !defined(CLMPCC_ONLY_BYTESWAP_LOW) && !defined(CLMPCC_ONLY_BYTESWAP_HIGH)
218 offset ^= (sc->sc_byteswap & 2);
219 #elif defined(CLMPCC_ONLY_BYTESWAP_HIGH)
220 offset ^= (CLMPCC_BYTESWAP_HIGH & 2);
221 #endif
222 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh, offset, buff, count);
223 }
224
225 integrate u_int8_t
226 clmpcc_select_channel(sc, new_chan)
227 struct clmpcc_softc *sc;
228 u_int new_chan;
229 {
230 u_int old_chan = clmpcc_rdreg_odd(sc, CLMPCC_REG_CAR);
231
232 clmpcc_wrreg_odd(sc, CLMPCC_REG_CAR, new_chan);
233
234 return old_chan;
235 }
236
237 integrate void
238 clmpcc_channel_cmd(sc, chan, cmd)
239 struct clmpcc_softc *sc;
240 int chan;
241 int cmd;
242 {
243 int i;
244
245 for (i = 5000; i; i--) {
246 if ( clmpcc_rdreg(sc, CLMPCC_REG_CCR) == 0 )
247 break;
248 delay(1);
249 }
250
251 if ( i == 0 )
252 printf("%s: channel %d command timeout (idle)\n",
253 sc->sc_dev.dv_xname, chan);
254
255 clmpcc_wrreg(sc, CLMPCC_REG_CCR, cmd);
256 }
257
258 integrate void
259 clmpcc_enable_transmitter(ch)
260 struct clmpcc_chan *ch;
261 {
262 u_int old;
263 int s;
264
265 old = clmpcc_select_channel(ch->ch_sc, ch->ch_car);
266
267 s = splserial();
268 clmpcc_wrreg(ch->ch_sc, CLMPCC_REG_IER,
269 clmpcc_rdreg(ch->ch_sc, CLMPCC_REG_IER) | CLMPCC_IER_TX_EMPTY);
270 SET(ch->ch_tty->t_state, TS_BUSY);
271 splx(s);
272
273 clmpcc_select_channel(ch->ch_sc, old);
274 }
275
276 static int
277 clmpcc_speed(sc, speed, cor, bpr)
278 struct clmpcc_softc *sc;
279 speed_t speed;
280 int *cor, *bpr;
281 {
282 int c, co, br;
283
284 for (co = 0, c = 8; c <= 2048; co++, c *= 4) {
285 br = ((sc->sc_clk / c) / speed) - 1;
286 if ( br < 0x100 ) {
287 *cor = co;
288 *bpr = br;
289 return 0;
290 }
291 }
292
293 return -1;
294 }
295
296 void
297 clmpcc_attach(sc)
298 struct clmpcc_softc *sc;
299 {
300 struct clmpcc_chan *ch;
301 struct tty *tp;
302 int chan;
303
304 if ( cons_sc != NULL &&
305 sc->sc_iot == cons_sc->sc_iot && sc->sc_ioh == cons_sc->sc_ioh )
306 cons_sc = sc;
307
308 /* Initialise the chip */
309 clmpcc_init(sc);
310
311 printf(": Cirrus Logic CD240%c Serial Controller\n",
312 (clmpcc_rd_msvr(sc) & CLMPCC_MSVR_PORT_ID) ? '0' : '1');
313
314 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
315 sc->sc_soft_running = 0;
316 #else
317 sc->sc_softintr_cookie =
318 softintr_establish(IPL_SOFTSERIAL, clmpcc_softintr, sc);
319 #ifdef DEBUG
320 if (sc->sc_softintr_cookie == NULL)
321 panic("clmpcc_attach: softintr_establish");
322 #endif
323 #endif
324 memset(&(sc->sc_chans[0]), 0, sizeof(sc->sc_chans));
325
326 for (chan = 0; chan < CLMPCC_NUM_CHANS; chan++) {
327 ch = &sc->sc_chans[chan];
328
329 ch->ch_sc = sc;
330 ch->ch_car = chan;
331
332 tp = ttymalloc();
333 tp->t_oproc = clmpcc_start;
334 tp->t_param = clmpcc_param;
335
336 ch->ch_tty = tp;
337
338 ch->ch_ibuf = malloc(clmpcc_ibuf_size * 2, M_DEVBUF, M_NOWAIT);
339 if ( ch->ch_ibuf == NULL ) {
340 printf("%s(%d): unable to allocate ring buffer\n",
341 sc->sc_dev.dv_xname, chan);
342 return;
343 }
344
345 ch->ch_ibuf_end = &(ch->ch_ibuf[clmpcc_ibuf_size * 2]);
346 ch->ch_ibuf_rd = ch->ch_ibuf_wr = ch->ch_ibuf;
347
348 tty_attach(tp);
349 }
350
351 printf("%s: %d channels available", sc->sc_dev.dv_xname,
352 CLMPCC_NUM_CHANS);
353 if ( cons_sc == sc ) {
354 printf(", console on channel %d.\n", cons_chan);
355 SET(sc->sc_chans[cons_chan].ch_flags, CLMPCC_FLG_IS_CONSOLE);
356 SET(sc->sc_chans[cons_chan].ch_openflags, TIOCFLAG_SOFTCAR);
357 } else
358 printf(".\n");
359 }
360
361 static int
362 clmpcc_init(sc)
363 struct clmpcc_softc *sc;
364 {
365 u_int tcor, tbpr;
366 u_int rcor, rbpr;
367 u_int msvr_rts, msvr_dtr;
368 u_int ccr;
369 int is_console;
370 int i;
371
372 /*
373 * All we're really concerned about here is putting the chip
374 * into a quiescent state so that it won't do anything until
375 * clmpccopen() is called. (Except the console channel.)
376 */
377
378 /*
379 * If the chip is acting as console, set all channels to the supplied
380 * console baud rate. Otherwise, plump for 9600.
381 */
382 if ( cons_sc &&
383 sc->sc_ioh == cons_sc->sc_ioh && sc->sc_iot == cons_sc->sc_iot ) {
384 clmpcc_speed(sc, cons_rate, &tcor, &tbpr);
385 clmpcc_speed(sc, cons_rate, &rcor, &rbpr);
386 is_console = 1;
387 } else {
388 clmpcc_speed(sc, 9600, &tcor, &tbpr);
389 clmpcc_speed(sc, 9600, &rcor, &rbpr);
390 is_console = 0;
391 }
392
393 /* Allow any pending output to be sent */
394 delay(10000);
395
396 /* Send the Reset All command to channel 0 (resets all channels!) */
397 clmpcc_channel_cmd(sc, 0, CLMPCC_CCR_T0_RESET_ALL);
398
399 delay(1000);
400
401 /*
402 * The chip will set it's firmware revision register to a non-zero
403 * value to indicate completion of reset.
404 */
405 for (i = 10000; clmpcc_rdreg(sc, CLMPCC_REG_GFRCR) == 0 && i; i--)
406 delay(1);
407
408 if ( i == 0 ) {
409 /*
410 * Watch out... If this chip is console, the message
411 * probably won't be sent since we just reset it!
412 */
413 printf("%s: Failed to reset chip\n", sc->sc_dev.dv_xname);
414 return -1;
415 }
416
417 for (i = 0; i < CLMPCC_NUM_CHANS; i++) {
418 clmpcc_select_channel(sc, i);
419
420 /* All interrupts are disabled to begin with */
421 clmpcc_wrreg(sc, CLMPCC_REG_IER, 0);
422
423 /* Make sure the channel interrupts on the correct vectors */
424 clmpcc_wrreg(sc, CLMPCC_REG_LIVR, sc->sc_vector_base);
425 clmpcc_wr_pilr(sc, CLMPCC_REG_RPILR, sc->sc_rpilr);
426 clmpcc_wr_pilr(sc, CLMPCC_REG_TPILR, sc->sc_tpilr);
427 clmpcc_wr_pilr(sc, CLMPCC_REG_MPILR, sc->sc_mpilr);
428
429 /* Receive timer prescaler set to 1ms */
430 clmpcc_wrreg(sc, CLMPCC_REG_TPR,
431 CLMPCC_MSEC_TO_TPR(sc->sc_clk, 1));
432
433 /* We support Async mode only */
434 clmpcc_wrreg(sc, CLMPCC_REG_CMR, CLMPCC_CMR_ASYNC);
435
436 /* Set the required baud rate */
437 clmpcc_wrreg(sc, CLMPCC_REG_TCOR, CLMPCC_TCOR_CLK(tcor));
438 clmpcc_wrreg(sc, CLMPCC_REG_TBPR, tbpr);
439 clmpcc_wrreg(sc, CLMPCC_REG_RCOR, CLMPCC_RCOR_CLK(rcor));
440 clmpcc_wrreg(sc, CLMPCC_REG_RBPR, rbpr);
441
442 /* Always default to 8N1 (XXX what about console?) */
443 clmpcc_wrreg(sc, CLMPCC_REG_COR1, CLMPCC_COR1_CHAR_8BITS |
444 CLMPCC_COR1_NO_PARITY |
445 CLMPCC_COR1_IGNORE_PAR);
446
447 clmpcc_wrreg(sc, CLMPCC_REG_COR2, 0);
448
449 clmpcc_wrreg(sc, CLMPCC_REG_COR3, CLMPCC_COR3_STOP_1);
450
451 clmpcc_wrreg(sc, CLMPCC_REG_COR4, CLMPCC_COR4_DSRzd |
452 CLMPCC_COR4_CDzd |
453 CLMPCC_COR4_CTSzd);
454
455 clmpcc_wrreg(sc, CLMPCC_REG_COR5, CLMPCC_COR5_DSRod |
456 CLMPCC_COR5_CDod |
457 CLMPCC_COR5_CTSod |
458 CLMPCC_COR5_FLOW_NORM);
459
460 clmpcc_wrreg(sc, CLMPCC_REG_COR6, 0);
461 clmpcc_wrreg(sc, CLMPCC_REG_COR7, 0);
462
463 /* Set the receive FIFO timeout */
464 clmpcc_wrreg(sc, CLMPCC_REG_RTPRl, CLMPCC_RTPR_DEFAULT);
465 clmpcc_wrreg(sc, CLMPCC_REG_RTPRh, 0);
466
467 /* At this point, we set up the console differently */
468 if ( is_console && i == cons_chan ) {
469 msvr_rts = CLMPCC_MSVR_RTS;
470 msvr_dtr = CLMPCC_MSVR_DTR;
471 ccr = CLMPCC_CCR_T0_RX_EN | CLMPCC_CCR_T0_TX_EN;
472 } else {
473 msvr_rts = 0;
474 msvr_dtr = 0;
475 ccr = CLMPCC_CCR_T0_RX_DIS | CLMPCC_CCR_T0_TX_DIS;
476 }
477
478 clmpcc_wrreg(sc, CLMPCC_REG_MSVR_RTS, msvr_rts);
479 clmpcc_wrreg(sc, CLMPCC_REG_MSVR_DTR, msvr_dtr);
480 clmpcc_channel_cmd(sc, i, CLMPCC_CCR_T0_INIT | ccr);
481 delay(100);
482 }
483
484 return 0;
485 }
486
487 static void
488 clmpcc_shutdown(ch)
489 struct clmpcc_chan *ch;
490 {
491 int oldch;
492
493 oldch = clmpcc_select_channel(ch->ch_sc, ch->ch_car);
494
495 /* Turn off interrupts. */
496 clmpcc_wrreg(ch->ch_sc, CLMPCC_REG_IER, 0);
497
498 if ( ISCLR(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) ) {
499 /* Disable the transmitter and receiver */
500 clmpcc_channel_cmd(ch->ch_sc, ch->ch_car, CLMPCC_CCR_T0_RX_DIS |
501 CLMPCC_CCR_T0_TX_DIS);
502
503 /* Drop RTS and DTR */
504 clmpcc_modem_control(ch, TIOCM_RTS | TIOCM_DTR, DMBIS);
505 }
506
507 clmpcc_select_channel(ch->ch_sc, oldch);
508 }
509
510 int
511 clmpccopen(dev, flag, mode, l)
512 dev_t dev;
513 int flag, mode;
514 struct lwp *l;
515 {
516 struct clmpcc_softc *sc;
517 struct clmpcc_chan *ch;
518 struct tty *tp;
519 int oldch;
520 int error;
521
522 sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
523 if (sc == NULL)
524 return (ENXIO);
525
526 ch = &sc->sc_chans[CLMPCCCHAN(dev)];
527
528 tp = ch->ch_tty;
529
530 if ( ISSET(tp->t_state, TS_ISOPEN) &&
531 ISSET(tp->t_state, TS_XCLUDE) &&
532 l->l_proc->p_ucred->cr_uid != 0 )
533 return EBUSY;
534
535 /*
536 * Do the following iff this is a first open.
537 */
538 if ( ISCLR(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0 ) {
539
540 ttychars(tp);
541
542 tp->t_dev = dev;
543 tp->t_iflag = TTYDEF_IFLAG;
544 tp->t_oflag = TTYDEF_OFLAG;
545 tp->t_lflag = TTYDEF_LFLAG;
546 tp->t_cflag = TTYDEF_CFLAG;
547 tp->t_ospeed = tp->t_ispeed = TTYDEF_SPEED;
548
549 if ( ISSET(ch->ch_openflags, TIOCFLAG_CLOCAL) )
550 SET(tp->t_cflag, CLOCAL);
551 if ( ISSET(ch->ch_openflags, TIOCFLAG_CRTSCTS) )
552 SET(tp->t_cflag, CRTSCTS);
553 if ( ISSET(ch->ch_openflags, TIOCFLAG_MDMBUF) )
554 SET(tp->t_cflag, MDMBUF);
555
556 /*
557 * Override some settings if the channel is being
558 * used as the console.
559 */
560 if ( ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) ) {
561 tp->t_ospeed = tp->t_ispeed = cons_rate;
562 SET(tp->t_cflag, CLOCAL);
563 CLR(tp->t_cflag, CRTSCTS);
564 CLR(tp->t_cflag, HUPCL);
565 }
566
567 ch->ch_control = 0;
568
569 clmpcc_param(tp, &tp->t_termios);
570 ttsetwater(tp);
571
572 /* Clear the input ring */
573 ch->ch_ibuf_rd = ch->ch_ibuf_wr = ch->ch_ibuf;
574
575 /* Select the channel */
576 oldch = clmpcc_select_channel(sc, ch->ch_car);
577
578 /* Reset it */
579 clmpcc_channel_cmd(sc, ch->ch_car, CLMPCC_CCR_T0_CLEAR |
580 CLMPCC_CCR_T0_RX_EN |
581 CLMPCC_CCR_T0_TX_EN);
582
583 /* Enable receiver and modem change interrupts. */
584 clmpcc_wrreg(sc, CLMPCC_REG_IER, CLMPCC_IER_MODEM |
585 CLMPCC_IER_RET |
586 CLMPCC_IER_RX_FIFO);
587
588 /* Raise RTS and DTR */
589 clmpcc_modem_control(ch, TIOCM_RTS | TIOCM_DTR, DMBIS);
590
591 clmpcc_select_channel(sc, oldch);
592 } else
593 if ( ISSET(tp->t_state, TS_XCLUDE) && l->l_proc->p_ucred->cr_uid != 0 )
594 return EBUSY;
595
596 error = ttyopen(tp, CLMPCCDIALOUT(dev), ISSET(flag, O_NONBLOCK));
597 if (error)
598 goto bad;
599
600 error = (*tp->t_linesw->l_open)(dev, tp);
601 if (error)
602 goto bad;
603
604 return 0;
605
606 bad:
607 if ( ISCLR(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0 ) {
608 /*
609 * We failed to open the device, and nobody else had it opened.
610 * Clean up the state as appropriate.
611 */
612 clmpcc_shutdown(ch);
613 }
614
615 return error;
616 }
617
618 int
619 clmpccclose(dev, flag, mode, l)
620 dev_t dev;
621 int flag, mode;
622 struct lwp *l;
623 {
624 struct clmpcc_softc *sc =
625 device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
626 struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(dev)];
627 struct tty *tp = ch->ch_tty;
628 int s;
629
630 if ( ISCLR(tp->t_state, TS_ISOPEN) )
631 return 0;
632
633 (*tp->t_linesw->l_close)(tp, flag);
634
635 s = spltty();
636
637 if ( ISCLR(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0 ) {
638 /*
639 * Although we got a last close, the device may still be in
640 * use; e.g. if this was the dialout node, and there are still
641 * processes waiting for carrier on the non-dialout node.
642 */
643 clmpcc_shutdown(ch);
644 }
645
646 ttyclose(tp);
647
648 splx(s);
649
650 return 0;
651 }
652
653 int
654 clmpccread(dev, uio, flag)
655 dev_t dev;
656 struct uio *uio;
657 int flag;
658 {
659 struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
660 struct tty *tp = sc->sc_chans[CLMPCCCHAN(dev)].ch_tty;
661
662 return ((*tp->t_linesw->l_read)(tp, uio, flag));
663 }
664
665 int
666 clmpccwrite(dev, uio, flag)
667 dev_t dev;
668 struct uio *uio;
669 int flag;
670 {
671 struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
672 struct tty *tp = sc->sc_chans[CLMPCCCHAN(dev)].ch_tty;
673
674 return ((*tp->t_linesw->l_write)(tp, uio, flag));
675 }
676
677 int
678 clmpccpoll(dev, events, l)
679 dev_t dev;
680 int events;
681 struct lwp *l;
682 {
683 struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
684 struct tty *tp = sc->sc_chans[CLMPCCCHAN(dev)].ch_tty;
685
686 return ((*tp->t_linesw->l_poll)(tp, events, l));
687 }
688
689 struct tty *
690 clmpcctty(dev)
691 dev_t dev;
692 {
693 struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
694
695 return (sc->sc_chans[CLMPCCCHAN(dev)].ch_tty);
696 }
697
698 int
699 clmpccioctl(dev, cmd, data, flag, l)
700 dev_t dev;
701 u_long cmd;
702 caddr_t data;
703 int flag;
704 struct lwp *l;
705 {
706 struct clmpcc_softc *sc = device_lookup(&clmpcc_cd, CLMPCCUNIT(dev));
707 struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(dev)];
708 struct tty *tp = ch->ch_tty;
709 int error;
710
711 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
712 if (error != EPASSTHROUGH)
713 return error;
714
715 error = ttioctl(tp, cmd, data, flag, l);
716 if (error != EPASSTHROUGH)
717 return error;
718
719 error = 0;
720
721 switch (cmd) {
722 case TIOCSBRK:
723 SET(ch->ch_flags, CLMPCC_FLG_START_BREAK);
724 clmpcc_enable_transmitter(ch);
725 break;
726
727 case TIOCCBRK:
728 SET(ch->ch_flags, CLMPCC_FLG_END_BREAK);
729 clmpcc_enable_transmitter(ch);
730 break;
731
732 case TIOCSDTR:
733 clmpcc_modem_control(ch, TIOCM_DTR, DMBIS);
734 break;
735
736 case TIOCCDTR:
737 clmpcc_modem_control(ch, TIOCM_DTR, DMBIC);
738 break;
739
740 case TIOCMSET:
741 clmpcc_modem_control(ch, *((int *)data), DMSET);
742 break;
743
744 case TIOCMBIS:
745 clmpcc_modem_control(ch, *((int *)data), DMBIS);
746 break;
747
748 case TIOCMBIC:
749 clmpcc_modem_control(ch, *((int *)data), DMBIC);
750 break;
751
752 case TIOCMGET:
753 *((int *)data) = clmpcc_modem_control(ch, 0, DMGET);
754 break;
755
756 case TIOCGFLAGS:
757 *((int *)data) = ch->ch_openflags;
758 break;
759
760 case TIOCSFLAGS:
761 error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
762 if ( error )
763 break;
764 ch->ch_openflags = *((int *)data) &
765 (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
766 TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
767 if ( ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) )
768 SET(ch->ch_openflags, TIOCFLAG_SOFTCAR);
769 break;
770
771 default:
772 error = EPASSTHROUGH;
773 break;
774 }
775
776 return error;
777 }
778
779 int
780 clmpcc_modem_control(ch, bits, howto)
781 struct clmpcc_chan *ch;
782 int bits;
783 int howto;
784 {
785 struct clmpcc_softc *sc = ch->ch_sc;
786 struct tty *tp = ch->ch_tty;
787 int oldch;
788 int msvr;
789 int rbits = 0;
790
791 oldch = clmpcc_select_channel(sc, ch->ch_car);
792
793 switch ( howto ) {
794 case DMGET:
795 msvr = clmpcc_rd_msvr(sc);
796
797 if ( sc->sc_swaprtsdtr ) {
798 rbits |= (msvr & CLMPCC_MSVR_RTS) ? TIOCM_DTR : 0;
799 rbits |= (msvr & CLMPCC_MSVR_DTR) ? TIOCM_RTS : 0;
800 } else {
801 rbits |= (msvr & CLMPCC_MSVR_RTS) ? TIOCM_RTS : 0;
802 rbits |= (msvr & CLMPCC_MSVR_DTR) ? TIOCM_DTR : 0;
803 }
804
805 rbits |= (msvr & CLMPCC_MSVR_CTS) ? TIOCM_CTS : 0;
806 rbits |= (msvr & CLMPCC_MSVR_CD) ? TIOCM_CD : 0;
807 rbits |= (msvr & CLMPCC_MSVR_DSR) ? TIOCM_DSR : 0;
808 break;
809
810 case DMSET:
811 if ( sc->sc_swaprtsdtr ) {
812 if ( ISCLR(tp->t_cflag, CRTSCTS) )
813 clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR,
814 bits & TIOCM_RTS ? CLMPCC_MSVR_DTR : 0);
815 clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS,
816 bits & TIOCM_DTR ? CLMPCC_MSVR_RTS : 0);
817 } else {
818 if ( ISCLR(tp->t_cflag, CRTSCTS) )
819 clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS,
820 bits & TIOCM_RTS ? CLMPCC_MSVR_RTS : 0);
821 clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR,
822 bits & TIOCM_DTR ? CLMPCC_MSVR_DTR : 0);
823 }
824 break;
825
826 case DMBIS:
827 if ( sc->sc_swaprtsdtr ) {
828 if ( ISCLR(tp->t_cflag, CRTSCTS) && ISSET(bits, TIOCM_RTS) )
829 clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_DTR, CLMPCC_MSVR_DTR);
830 if ( ISSET(bits, TIOCM_DTR) )
831 clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_RTS, CLMPCC_MSVR_RTS);
832 } else {
833 if ( ISCLR(tp->t_cflag, CRTSCTS) && ISSET(bits, TIOCM_RTS) )
834 clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_RTS, CLMPCC_MSVR_RTS);
835 if ( ISSET(bits, TIOCM_DTR) )
836 clmpcc_wr_msvr(sc,CLMPCC_REG_MSVR_DTR, CLMPCC_MSVR_DTR);
837 }
838 break;
839
840 case DMBIC:
841 if ( sc->sc_swaprtsdtr ) {
842 if ( ISCLR(tp->t_cflag, CRTSCTS) && ISCLR(bits, TIOCM_RTS) )
843 clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR, 0);
844 if ( ISCLR(bits, TIOCM_DTR) )
845 clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS, 0);
846 } else {
847 if ( ISCLR(tp->t_cflag, CRTSCTS) && ISCLR(bits, TIOCM_RTS) )
848 clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_RTS, 0);
849 if ( ISCLR(bits, TIOCM_DTR) )
850 clmpcc_wr_msvr(sc, CLMPCC_REG_MSVR_DTR, 0);
851 }
852 break;
853 }
854
855 clmpcc_select_channel(sc, oldch);
856
857 return rbits;
858 }
859
860 static int
861 clmpcc_param(tp, t)
862 struct tty *tp;
863 struct termios *t;
864 {
865 struct clmpcc_softc *sc =
866 device_lookup(&clmpcc_cd, CLMPCCUNIT(tp->t_dev));
867 struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(tp->t_dev)];
868 u_char cor;
869 u_char oldch;
870 int oclk, obpr;
871 int iclk, ibpr;
872 int s;
873
874 /* Check requested parameters. */
875 if ( t->c_ospeed && clmpcc_speed(sc, t->c_ospeed, &oclk, &obpr) < 0 )
876 return EINVAL;
877
878 if ( t->c_ispeed && clmpcc_speed(sc, t->c_ispeed, &iclk, &ibpr) < 0 )
879 return EINVAL;
880
881 /*
882 * For the console, always force CLOCAL and !HUPCL, so that the port
883 * is always active.
884 */
885 if ( ISSET(ch->ch_openflags, TIOCFLAG_SOFTCAR) ||
886 ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) ) {
887 SET(t->c_cflag, CLOCAL);
888 CLR(t->c_cflag, HUPCL);
889 }
890
891 CLR(ch->ch_flags, CLMPCC_FLG_UPDATE_PARMS);
892
893 /* If ospeed it zero, hangup the line */
894 clmpcc_modem_control(ch, TIOCM_DTR, t->c_ospeed == 0 ? DMBIC : DMBIS);
895
896 if ( t->c_ospeed ) {
897 ch->ch_tcor = CLMPCC_TCOR_CLK(oclk);
898 ch->ch_tbpr = obpr;
899 } else {
900 ch->ch_tcor = 0;
901 ch->ch_tbpr = 0;
902 }
903
904 if ( t->c_ispeed ) {
905 ch->ch_rcor = CLMPCC_RCOR_CLK(iclk);
906 ch->ch_rbpr = ibpr;
907 } else {
908 ch->ch_rcor = 0;
909 ch->ch_rbpr = 0;
910 }
911
912 /* Work out value to use for COR1 */
913 cor = 0;
914 if ( ISSET(t->c_cflag, PARENB) ) {
915 cor |= CLMPCC_COR1_NORM_PARITY;
916 if ( ISSET(t->c_cflag, PARODD) )
917 cor |= CLMPCC_COR1_ODD_PARITY;
918 }
919
920 if ( ISCLR(t->c_cflag, INPCK) )
921 cor |= CLMPCC_COR1_IGNORE_PAR;
922
923 switch ( t->c_cflag & CSIZE ) {
924 case CS5:
925 cor |= CLMPCC_COR1_CHAR_5BITS;
926 break;
927
928 case CS6:
929 cor |= CLMPCC_COR1_CHAR_6BITS;
930 break;
931
932 case CS7:
933 cor |= CLMPCC_COR1_CHAR_7BITS;
934 break;
935
936 case CS8:
937 cor |= CLMPCC_COR1_CHAR_8BITS;
938 break;
939 }
940
941 ch->ch_cor1 = cor;
942
943 /*
944 * The only interesting bit in COR2 is 'CTS Automatic Enable'
945 * when hardware flow control is in effect.
946 */
947 ch->ch_cor2 = ISSET(t->c_cflag, CRTSCTS) ? CLMPCC_COR2_CtsAE : 0;
948
949 /* COR3 needs to be set to the number of stop bits... */
950 ch->ch_cor3 = ISSET(t->c_cflag, CSTOPB) ? CLMPCC_COR3_STOP_2 :
951 CLMPCC_COR3_STOP_1;
952
953 /*
954 * COR4 contains the FIFO threshold setting.
955 * We adjust the threshold depending on the input speed...
956 */
957 if ( t->c_ispeed <= 1200 )
958 ch->ch_cor4 = CLMPCC_COR4_FIFO_LOW;
959 else if ( t->c_ispeed <= 19200 )
960 ch->ch_cor4 = CLMPCC_COR4_FIFO_MED;
961 else
962 ch->ch_cor4 = CLMPCC_COR4_FIFO_HIGH;
963
964 /*
965 * If chip is used with CTS and DTR swapped, we can enable
966 * automatic hardware flow control.
967 */
968 if ( sc->sc_swaprtsdtr && ISSET(t->c_cflag, CRTSCTS) )
969 ch->ch_cor5 = CLMPCC_COR5_FLOW_NORM;
970 else
971 ch->ch_cor5 = 0;
972
973 s = splserial();
974 oldch = clmpcc_select_channel(sc, ch->ch_car);
975
976 /*
977 * COR2 needs to be set immediately otherwise we might never get
978 * a Tx EMPTY interrupt to change the other parameters.
979 */
980 if ( clmpcc_rdreg(sc, CLMPCC_REG_COR2) != ch->ch_cor2 )
981 clmpcc_wrreg(sc, CLMPCC_REG_COR2, ch->ch_cor2);
982
983 if ( ISCLR(ch->ch_tty->t_state, TS_BUSY) )
984 clmpcc_set_params(ch);
985 else
986 SET(ch->ch_flags, CLMPCC_FLG_UPDATE_PARMS);
987
988 clmpcc_select_channel(sc, oldch);
989
990 splx(s);
991
992 return 0;
993 }
994
995 static void
996 clmpcc_set_params(ch)
997 struct clmpcc_chan *ch;
998 {
999 struct clmpcc_softc *sc = ch->ch_sc;
1000 u_char r1;
1001 u_char r2;
1002
1003 if ( ch->ch_tcor || ch->ch_tbpr ) {
1004 r1 = clmpcc_rdreg(sc, CLMPCC_REG_TCOR);
1005 r2 = clmpcc_rdreg(sc, CLMPCC_REG_TBPR);
1006 /* Only write Tx rate if it really has changed */
1007 if ( ch->ch_tcor != r1 || ch->ch_tbpr != r2 ) {
1008 clmpcc_wrreg(sc, CLMPCC_REG_TCOR, ch->ch_tcor);
1009 clmpcc_wrreg(sc, CLMPCC_REG_TBPR, ch->ch_tbpr);
1010 }
1011 }
1012
1013 if ( ch->ch_rcor || ch->ch_rbpr ) {
1014 r1 = clmpcc_rdreg(sc, CLMPCC_REG_RCOR);
1015 r2 = clmpcc_rdreg(sc, CLMPCC_REG_RBPR);
1016 /* Only write Rx rate if it really has changed */
1017 if ( ch->ch_rcor != r1 || ch->ch_rbpr != r2 ) {
1018 clmpcc_wrreg(sc, CLMPCC_REG_RCOR, ch->ch_rcor);
1019 clmpcc_wrreg(sc, CLMPCC_REG_RBPR, ch->ch_rbpr);
1020 }
1021 }
1022
1023 if ( clmpcc_rdreg(sc, CLMPCC_REG_COR1) != ch->ch_cor1 ) {
1024 clmpcc_wrreg(sc, CLMPCC_REG_COR1, ch->ch_cor1);
1025 /* Any change to COR1 requires an INIT command */
1026 SET(ch->ch_flags, CLMPCC_FLG_NEED_INIT);
1027 }
1028
1029 if ( clmpcc_rdreg(sc, CLMPCC_REG_COR3) != ch->ch_cor3 )
1030 clmpcc_wrreg(sc, CLMPCC_REG_COR3, ch->ch_cor3);
1031
1032 r1 = clmpcc_rdreg(sc, CLMPCC_REG_COR4);
1033 if ( ch->ch_cor4 != (r1 & CLMPCC_COR4_FIFO_MASK) ) {
1034 /*
1035 * Note: If the FIFO has changed, we always set it to
1036 * zero here and disable the Receive Timeout interrupt.
1037 * It's up to the Rx Interrupt handler to pick the
1038 * appropriate moment to write the new FIFO length.
1039 */
1040 clmpcc_wrreg(sc, CLMPCC_REG_COR4, r1 & ~CLMPCC_COR4_FIFO_MASK);
1041 r1 = clmpcc_rdreg(sc, CLMPCC_REG_IER);
1042 clmpcc_wrreg(sc, CLMPCC_REG_IER, r1 & ~CLMPCC_IER_RET);
1043 SET(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR);
1044 }
1045
1046 r1 = clmpcc_rdreg(sc, CLMPCC_REG_COR5);
1047 if ( ch->ch_cor5 != (r1 & CLMPCC_COR5_FLOW_MASK) ) {
1048 r1 &= ~CLMPCC_COR5_FLOW_MASK;
1049 clmpcc_wrreg(sc, CLMPCC_REG_COR5, r1 | ch->ch_cor5);
1050 }
1051 }
1052
1053 static void
1054 clmpcc_start(tp)
1055 struct tty *tp;
1056 {
1057 struct clmpcc_softc *sc =
1058 device_lookup(&clmpcc_cd, CLMPCCUNIT(tp->t_dev));
1059 struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(tp->t_dev)];
1060 u_int oldch;
1061 int s;
1062
1063 s = spltty();
1064
1065 if ( ISCLR(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY) ) {
1066 if ( tp->t_outq.c_cc <= tp->t_lowat ) {
1067 if ( ISSET(tp->t_state, TS_ASLEEP) ) {
1068 CLR(tp->t_state, TS_ASLEEP);
1069 wakeup(&tp->t_outq);
1070 }
1071 selwakeup(&tp->t_wsel);
1072 }
1073
1074 if ( ISSET(ch->ch_flags, CLMPCC_FLG_START_BREAK |
1075 CLMPCC_FLG_END_BREAK) ||
1076 tp->t_outq.c_cc > 0 ) {
1077
1078 if ( ISCLR(ch->ch_flags, CLMPCC_FLG_START_BREAK |
1079 CLMPCC_FLG_END_BREAK) ) {
1080 ch->ch_obuf_addr = tp->t_outq.c_cf;
1081 ch->ch_obuf_size = ndqb(&tp->t_outq, 0);
1082 }
1083
1084 /* Enable TX empty interrupts */
1085 oldch = clmpcc_select_channel(ch->ch_sc, ch->ch_car);
1086 clmpcc_wrreg(ch->ch_sc, CLMPCC_REG_IER,
1087 clmpcc_rdreg(ch->ch_sc, CLMPCC_REG_IER) |
1088 CLMPCC_IER_TX_EMPTY);
1089 clmpcc_select_channel(ch->ch_sc, oldch);
1090 SET(tp->t_state, TS_BUSY);
1091 }
1092 }
1093
1094 splx(s);
1095 }
1096
1097 /*
1098 * Stop output on a line.
1099 */
1100 void
1101 clmpccstop(tp, flag)
1102 struct tty *tp;
1103 int flag;
1104 {
1105 struct clmpcc_softc *sc =
1106 device_lookup(&clmpcc_cd, CLMPCCUNIT(tp->t_dev));
1107 struct clmpcc_chan *ch = &sc->sc_chans[CLMPCCCHAN(tp->t_dev)];
1108 int s;
1109
1110 s = splserial();
1111
1112 if ( ISSET(tp->t_state, TS_BUSY) ) {
1113 if ( ISCLR(tp->t_state, TS_TTSTOP) )
1114 SET(tp->t_state, TS_FLUSH);
1115 ch->ch_obuf_size = 0;
1116 }
1117 splx(s);
1118 }
1119
1120 /*
1121 * RX interrupt routine
1122 */
1123 int
1124 clmpcc_rxintr(arg)
1125 void *arg;
1126 {
1127 struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
1128 struct clmpcc_chan *ch;
1129 u_int8_t *put, *end, rxd;
1130 u_char errstat;
1131 u_char fc, tc;
1132 u_char risr;
1133 u_char rir;
1134 #ifdef DDB
1135 int saw_break = 0;
1136 #endif
1137
1138 /* Receive interrupt active? */
1139 rir = clmpcc_rdreg(sc, CLMPCC_REG_RIR);
1140
1141 /*
1142 * If we're using auto-vectored interrupts, we have to
1143 * verify if the chip is generating the interrupt.
1144 */
1145 if ( sc->sc_vector_base == 0 && (rir & CLMPCC_RIR_RACT) == 0 )
1146 return 0;
1147
1148 /* Get pointer to interrupting channel's data structure */
1149 ch = &sc->sc_chans[rir & CLMPCC_RIR_RCN_MASK];
1150
1151 /* Get the interrupt status register */
1152 risr = clmpcc_rdreg(sc, CLMPCC_REG_RISRl);
1153 if ( risr & CLMPCC_RISR_TIMEOUT ) {
1154 u_char reg;
1155 /*
1156 * Set the FIFO threshold to zero, and disable
1157 * further receive timeout interrupts.
1158 */
1159 reg = clmpcc_rdreg(sc, CLMPCC_REG_COR4);
1160 clmpcc_wrreg(sc, CLMPCC_REG_COR4, reg & ~CLMPCC_COR4_FIFO_MASK);
1161 reg = clmpcc_rdreg(sc, CLMPCC_REG_IER);
1162 clmpcc_wrreg(sc, CLMPCC_REG_IER, reg & ~CLMPCC_IER_RET);
1163 clmpcc_wrreg(sc, CLMPCC_REG_REOIR, CLMPCC_REOIR_NO_TRANS);
1164 SET(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR);
1165 return 1;
1166 }
1167
1168 /* How many bytes are waiting in the FIFO? */
1169 fc = tc = clmpcc_rdreg(sc, CLMPCC_REG_RFOC) & CLMPCC_RFOC_MASK;
1170
1171 #ifdef DDB
1172 /*
1173 * Allow BREAK on the console to drop to the debugger.
1174 */
1175 if ( ISSET(ch->ch_flags, CLMPCC_FLG_IS_CONSOLE) &&
1176 risr & CLMPCC_RISR_BREAK ) {
1177 saw_break = 1;
1178 }
1179 #endif
1180
1181 if ( ISCLR(ch->ch_tty->t_state, TS_ISOPEN) && fc ) {
1182 /* Just get rid of the data */
1183 while ( fc-- )
1184 (void) clmpcc_rd_rxdata(sc);
1185 goto rx_done;
1186 }
1187
1188 put = ch->ch_ibuf_wr;
1189 end = ch->ch_ibuf_end;
1190
1191 /*
1192 * Note: The chip is completely hosed WRT these error
1193 * conditions; there seems to be no way to associate
1194 * the error with the correct character in the FIFO.
1195 * We compromise by tagging the first character we read
1196 * with the error. Not perfect, but there's no other way.
1197 */
1198 errstat = 0;
1199 if ( risr & CLMPCC_RISR_PARITY )
1200 errstat |= TTY_PE;
1201 if ( risr & (CLMPCC_RISR_FRAMING | CLMPCC_RISR_BREAK) )
1202 errstat |= TTY_FE;
1203
1204 /*
1205 * As long as there are characters in the FIFO, and we
1206 * have space for them...
1207 */
1208 while ( fc > 0 ) {
1209
1210 *put++ = rxd = clmpcc_rd_rxdata(sc);
1211 *put++ = errstat;
1212
1213 if ( put >= end )
1214 put = ch->ch_ibuf;
1215
1216 if ( put == ch->ch_ibuf_rd ) {
1217 put -= 2;
1218 if ( put < ch->ch_ibuf )
1219 put = end - 2;
1220 }
1221
1222 errstat = 0;
1223 fc--;
1224 }
1225
1226 ch->ch_ibuf_wr = put;
1227
1228 #if 0
1229 if ( sc->sc_swaprtsdtr == 0 &&
1230 ISSET(cy->cy_tty->t_cflag, CRTSCTS) && cc < ch->ch_r_hiwat) {
1231 /*
1232 * If RTS/DTR are not physically swapped, we have to
1233 * do hardware flow control manually
1234 */
1235 clmpcc_wr_msvr(sc, CLMPCC_MSVR_RTS, 0);
1236 }
1237 #endif
1238
1239 rx_done:
1240 if ( fc != tc ) {
1241 if ( ISSET(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR) ) {
1242 u_char reg;
1243 /*
1244 * Set the FIFO threshold to the preset value,
1245 * and enable receive timeout interrupts.
1246 */
1247 reg = clmpcc_rdreg(sc, CLMPCC_REG_COR4);
1248 reg = (reg & ~CLMPCC_COR4_FIFO_MASK) | ch->ch_cor4;
1249 clmpcc_wrreg(sc, CLMPCC_REG_COR4, reg);
1250 reg = clmpcc_rdreg(sc, CLMPCC_REG_IER);
1251 clmpcc_wrreg(sc, CLMPCC_REG_IER, reg | CLMPCC_IER_RET);
1252 CLR(ch->ch_flags, CLMPCC_FLG_FIFO_CLEAR);
1253 }
1254
1255 clmpcc_wrreg(sc, CLMPCC_REG_REOIR, 0);
1256 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1257 if ( sc->sc_soft_running == 0 ) {
1258 sc->sc_soft_running = 1;
1259 (sc->sc_softhook)(sc);
1260 }
1261 #else
1262 softintr_schedule(sc->sc_softintr_cookie);
1263 #endif
1264 } else
1265 clmpcc_wrreg(sc, CLMPCC_REG_REOIR, CLMPCC_REOIR_NO_TRANS);
1266
1267 #ifdef DDB
1268 /*
1269 * Only =after= we write REOIR is it safe to drop to the debugger.
1270 */
1271 if ( saw_break )
1272 Debugger();
1273 #endif
1274
1275 return 1;
1276 }
1277
1278 /*
1279 * Tx interrupt routine
1280 */
1281 int
1282 clmpcc_txintr(arg)
1283 void *arg;
1284 {
1285 struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
1286 struct clmpcc_chan *ch;
1287 struct tty *tp;
1288 u_char ftc, oftc;
1289 u_char tir, teoir;
1290 int etcmode = 0;
1291
1292 /* Tx interrupt active? */
1293 tir = clmpcc_rdreg(sc, CLMPCC_REG_TIR);
1294
1295 /*
1296 * If we're using auto-vectored interrupts, we have to
1297 * verify if the chip is generating the interrupt.
1298 */
1299 if ( sc->sc_vector_base == 0 && (tir & CLMPCC_TIR_TACT) == 0 )
1300 return 0;
1301
1302 /* Get pointer to interrupting channel's data structure */
1303 ch = &sc->sc_chans[tir & CLMPCC_TIR_TCN_MASK];
1304 tp = ch->ch_tty;
1305
1306 /* Dummy read of the interrupt status register */
1307 (void) clmpcc_rdreg(sc, CLMPCC_REG_TISR);
1308
1309 /* Make sure embedded transmit commands are disabled */
1310 clmpcc_wrreg(sc, CLMPCC_REG_COR2, ch->ch_cor2);
1311
1312 ftc = oftc = clmpcc_rdreg(sc, CLMPCC_REG_TFTC);
1313
1314 /* Handle a delayed parameter change */
1315 if ( ISSET(ch->ch_flags, CLMPCC_FLG_UPDATE_PARMS) ) {
1316 CLR(ch->ch_flags, CLMPCC_FLG_UPDATE_PARMS);
1317 clmpcc_set_params(ch);
1318 }
1319
1320 if ( ch->ch_obuf_size > 0 ) {
1321 u_int n = min(ch->ch_obuf_size, ftc);
1322
1323 clmpcc_wrtx_multi(sc, ch->ch_obuf_addr, n);
1324
1325 ftc -= n;
1326 ch->ch_obuf_size -= n;
1327 ch->ch_obuf_addr += n;
1328
1329 } else {
1330 /*
1331 * Check if we should start/stop a break
1332 */
1333 if ( ISSET(ch->ch_flags, CLMPCC_FLG_START_BREAK) ) {
1334 CLR(ch->ch_flags, CLMPCC_FLG_START_BREAK);
1335 /* Enable embedded transmit commands */
1336 clmpcc_wrreg(sc, CLMPCC_REG_COR2,
1337 ch->ch_cor2 | CLMPCC_COR2_ETC);
1338 clmpcc_wr_txdata(sc, CLMPCC_ETC_MAGIC);
1339 clmpcc_wr_txdata(sc, CLMPCC_ETC_SEND_BREAK);
1340 ftc -= 2;
1341 etcmode = 1;
1342 }
1343
1344 if ( ISSET(ch->ch_flags, CLMPCC_FLG_END_BREAK) ) {
1345 CLR(ch->ch_flags, CLMPCC_FLG_END_BREAK);
1346 /* Enable embedded transmit commands */
1347 clmpcc_wrreg(sc, CLMPCC_REG_COR2,
1348 ch->ch_cor2 | CLMPCC_COR2_ETC);
1349 clmpcc_wr_txdata(sc, CLMPCC_ETC_MAGIC);
1350 clmpcc_wr_txdata(sc, CLMPCC_ETC_STOP_BREAK);
1351 ftc -= 2;
1352 etcmode = 1;
1353 }
1354 }
1355
1356 tir = clmpcc_rdreg(sc, CLMPCC_REG_IER);
1357
1358 if ( ftc != oftc ) {
1359 /*
1360 * Enable/disable the Tx FIFO threshold interrupt
1361 * according to how much data is in the FIFO.
1362 * However, always disable the FIFO threshold if
1363 * we've left the channel in 'Embedded Transmit
1364 * Command' mode.
1365 */
1366 if ( etcmode || ftc >= ch->ch_cor4 )
1367 tir &= ~CLMPCC_IER_TX_FIFO;
1368 else
1369 tir |= CLMPCC_IER_TX_FIFO;
1370 teoir = 0;
1371 } else {
1372 /*
1373 * No data was sent.
1374 * Disable transmit interrupt.
1375 */
1376 tir &= ~(CLMPCC_IER_TX_EMPTY|CLMPCC_IER_TX_FIFO);
1377 teoir = CLMPCC_TEOIR_NO_TRANS;
1378
1379 /*
1380 * Request Tx processing in the soft interrupt handler
1381 */
1382 ch->ch_tx_done = 1;
1383 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1384 if ( sc->sc_soft_running == 0 ) {
1385 sc->sc_soft_running = 1;
1386 (sc->sc_softhook)(sc);
1387 }
1388 #else
1389 softintr_schedule(sc->sc_softintr_cookie);
1390 #endif
1391 }
1392
1393 clmpcc_wrreg(sc, CLMPCC_REG_IER, tir);
1394 clmpcc_wrreg(sc, CLMPCC_REG_TEOIR, teoir);
1395
1396 return 1;
1397 }
1398
1399 /*
1400 * Modem change interrupt routine
1401 */
1402 int
1403 clmpcc_mdintr(arg)
1404 void *arg;
1405 {
1406 struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
1407 u_char mir;
1408
1409 /* Modem status interrupt active? */
1410 mir = clmpcc_rdreg(sc, CLMPCC_REG_MIR);
1411
1412 /*
1413 * If we're using auto-vectored interrupts, we have to
1414 * verify if the chip is generating the interrupt.
1415 */
1416 if ( sc->sc_vector_base == 0 && (mir & CLMPCC_MIR_MACT) == 0 )
1417 return 0;
1418
1419 /* Dummy read of the interrupt status register */
1420 (void) clmpcc_rdreg(sc, CLMPCC_REG_MISR);
1421
1422 /* Retrieve current status of modem lines. */
1423 sc->sc_chans[mir & CLMPCC_MIR_MCN_MASK].ch_control |=
1424 clmpcc_rd_msvr(sc) & CLMPCC_MSVR_CD;
1425
1426 clmpcc_wrreg(sc, CLMPCC_REG_MEOIR, 0);
1427
1428 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1429 if ( sc->sc_soft_running == 0 ) {
1430 sc->sc_soft_running = 1;
1431 (sc->sc_softhook)(sc);
1432 }
1433 #else
1434 softintr_schedule(sc->sc_softintr_cookie);
1435 #endif
1436
1437 return 1;
1438 }
1439
1440 void
1441 clmpcc_softintr(arg)
1442 void *arg;
1443 {
1444 struct clmpcc_softc *sc = (struct clmpcc_softc *)arg;
1445 struct clmpcc_chan *ch;
1446 struct tty *tp;
1447 int (*rint) __P((int, struct tty *));
1448 u_char *get;
1449 u_char reg;
1450 u_int c;
1451 int chan;
1452
1453 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1454 sc->sc_soft_running = 0;
1455 #endif
1456
1457 /* Handle Modem state changes too... */
1458
1459 for (chan = 0; chan < CLMPCC_NUM_CHANS; chan++) {
1460 ch = &sc->sc_chans[chan];
1461 tp = ch->ch_tty;
1462
1463 get = ch->ch_ibuf_rd;
1464 rint = tp->t_linesw->l_rint;
1465
1466 /* Squirt buffered incoming data into the tty layer */
1467 while ( get != ch->ch_ibuf_wr ) {
1468 c = get[0];
1469 c |= ((u_int)get[1]) << 8;
1470 if ( (rint)(c, tp) == -1 ) {
1471 ch->ch_ibuf_rd = ch->ch_ibuf_wr;
1472 break;
1473 }
1474
1475 get += 2;
1476 if ( get == ch->ch_ibuf_end )
1477 get = ch->ch_ibuf;
1478
1479 ch->ch_ibuf_rd = get;
1480 }
1481
1482 /*
1483 * Is the transmitter idle and in need of attention?
1484 */
1485 if ( ch->ch_tx_done ) {
1486 ch->ch_tx_done = 0;
1487
1488 if ( ISSET(ch->ch_flags, CLMPCC_FLG_NEED_INIT) ) {
1489 clmpcc_channel_cmd(sc, ch->ch_car,
1490 CLMPCC_CCR_T0_INIT |
1491 CLMPCC_CCR_T0_RX_EN |
1492 CLMPCC_CCR_T0_TX_EN);
1493 CLR(ch->ch_flags, CLMPCC_FLG_NEED_INIT);
1494
1495 /*
1496 * Allow time for the channel to initialise.
1497 * (Empirically derived duration; there must
1498 * be another way to determine the command
1499 * has completed without busy-waiting...)
1500 */
1501 delay(800);
1502
1503 /*
1504 * Update the tty layer's idea of the carrier
1505 * bit, in case we changed CLOCAL or MDMBUF.
1506 * We don't hang up here; we only do that by
1507 * explicit request.
1508 */
1509 reg = clmpcc_rd_msvr(sc) & CLMPCC_MSVR_CD;
1510 (*tp->t_linesw->l_modem)(tp, reg != 0);
1511 }
1512
1513 CLR(tp->t_state, TS_BUSY);
1514 if ( ISSET(tp->t_state, TS_FLUSH) )
1515 CLR(tp->t_state, TS_FLUSH);
1516 else
1517 ndflush(&tp->t_outq,
1518 (int)(ch->ch_obuf_addr - tp->t_outq.c_cf));
1519
1520 (*tp->t_linesw->l_start)(tp);
1521 }
1522 }
1523 }
1524
1525
1526 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
1527 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
1528 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
1529 /*
1530 * Following are all routines needed for a cd240x channel to act as console
1531 */
1532 int
1533 clmpcc_cnattach(sc, chan, rate)
1534 struct clmpcc_softc *sc;
1535 int chan;
1536 int rate;
1537 {
1538 cons_sc = sc;
1539 cons_chan = chan;
1540 cons_rate = rate;
1541
1542 return (clmpcc_init(sc));
1543 }
1544
1545 /*
1546 * The following functions are polled getc and putc routines, for console use.
1547 */
1548 static int
1549 clmpcc_common_getc(sc, chan)
1550 struct clmpcc_softc *sc;
1551 int chan;
1552 {
1553 u_char old_chan;
1554 u_char old_ier;
1555 u_char ch, rir, risr;
1556 int s;
1557
1558 s = splhigh();
1559
1560 /* Save the currently active channel */
1561 old_chan = clmpcc_select_channel(sc, chan);
1562
1563 /*
1564 * We have to put the channel into RX interrupt mode before
1565 * trying to read the Rx data register. So save the previous
1566 * interrupt mode.
1567 */
1568 old_ier = clmpcc_rdreg(sc, CLMPCC_REG_IER);
1569 clmpcc_wrreg(sc, CLMPCC_REG_IER, CLMPCC_IER_RX_FIFO);
1570
1571 /* Loop until we get a character */
1572 for (;;) {
1573 /*
1574 * The REN bit will be set in the Receive Interrupt Register
1575 * when the CD240x has a character to process. Remember,
1576 * the RACT bit won't be set until we generate an interrupt
1577 * acknowledge cycle via the MD front-end.
1578 */
1579 rir = clmpcc_rdreg(sc, CLMPCC_REG_RIR);
1580 if ( (rir & CLMPCC_RIR_REN) == 0 )
1581 continue;
1582
1583 /* Acknowledge the request */
1584 if ( sc->sc_iackhook )
1585 (sc->sc_iackhook)(sc, CLMPCC_IACK_RX);
1586
1587 /*
1588 * Determine if the interrupt is for the required channel
1589 * and if valid data is available.
1590 */
1591 rir = clmpcc_rdreg(sc, CLMPCC_REG_RIR);
1592 risr = clmpcc_rdreg(sc, CLMPCC_REG_RISR);
1593 if ( (rir & CLMPCC_RIR_RCN_MASK) != chan ||
1594 risr != 0 ) {
1595 /* Rx error, or BREAK */
1596 clmpcc_wrreg(sc, CLMPCC_REG_REOIR,
1597 CLMPCC_REOIR_NO_TRANS);
1598 } else {
1599 /* Dummy read of the FIFO count register */
1600 (void) clmpcc_rdreg(sc, CLMPCC_REG_RFOC);
1601
1602 /* Fetch the received character */
1603 ch = clmpcc_rd_rxdata(sc);
1604
1605 clmpcc_wrreg(sc, CLMPCC_REG_REOIR, 0);
1606 break;
1607 }
1608 }
1609
1610 /* Restore the original IER and CAR register contents */
1611 clmpcc_wrreg(sc, CLMPCC_REG_IER, old_ier);
1612 clmpcc_select_channel(sc, old_chan);
1613
1614 splx(s);
1615 return ch;
1616 }
1617
1618
1619 static void
1620 clmpcc_common_putc(sc, chan, c)
1621 struct clmpcc_softc *sc;
1622 int chan;
1623 int c;
1624 {
1625 u_char old_chan;
1626 int s = splhigh();
1627
1628 /* Save the currently active channel */
1629 old_chan = clmpcc_select_channel(sc, chan);
1630
1631 /*
1632 * Since we can only access the Tx Data register from within
1633 * the interrupt handler, the easiest way to get console data
1634 * onto the wire is using one of the Special Transmit Character
1635 * registers.
1636 */
1637 clmpcc_wrreg(sc, CLMPCC_REG_SCHR4, c);
1638 clmpcc_wrreg(sc, CLMPCC_REG_STCR, CLMPCC_STCR_SSPC(4) |
1639 CLMPCC_STCR_SND_SPC);
1640
1641 /* Wait until the "Send Special Character" command is accepted */
1642 while ( clmpcc_rdreg(sc, CLMPCC_REG_STCR) != 0 )
1643 ;
1644
1645 /* Restore the previous channel selected */
1646 clmpcc_select_channel(sc, old_chan);
1647
1648 splx(s);
1649 }
1650
1651 int
1652 clmpcccngetc(dev)
1653 dev_t dev;
1654 {
1655 return clmpcc_common_getc(cons_sc, cons_chan);
1656 }
1657
1658 /*
1659 * Console kernel output character routine.
1660 */
1661 void
1662 clmpcccnputc(dev, c)
1663 dev_t dev;
1664 int c;
1665 {
1666 if ( c == '\n' )
1667 clmpcc_common_putc(cons_sc, cons_chan, '\r');
1668
1669 clmpcc_common_putc(cons_sc, cons_chan, c);
1670 }
1671