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