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