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