Home | History | Annotate | Line # | Download | only in sbus
magma.c revision 1.47
      1 /*	$NetBSD: magma.c,v 1.47 2008/06/11 21:25:31 drochner Exp $	*/
      2 /*
      3  * magma.c
      4  *
      5  * Copyright (c) 1998 Iain Hibbert
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Iain Hibbert
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 /*
     36  * Driver for Magma SBus Serial/Parallel cards using the Cirrus Logic
     37  * CD1400 & CD1190 chips
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: magma.c,v 1.47 2008/06/11 21:25:31 drochner Exp $");
     42 
     43 #if 0
     44 #define MAGMA_DEBUG
     45 #endif
     46 
     47 #include "magma.h"
     48 #if NMAGMA > 0
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/proc.h>
     53 #include <sys/device.h>
     54 #include <sys/file.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/malloc.h>
     57 #include <sys/tty.h>
     58 #include <sys/time.h>
     59 #include <sys/kernel.h>
     60 #include <sys/syslog.h>
     61 #include <sys/conf.h>
     62 #include <sys/errno.h>
     63 #include <sys/kauth.h>
     64 #include <sys/intr.h>
     65 
     66 #include <sys/bus.h>
     67 #include <machine/autoconf.h>
     68 
     69 #include <dev/sbus/sbusvar.h>
     70 
     71 #include <dev/ic/cd1400reg.h>
     72 #include <dev/ic/cd1190reg.h>
     73 
     74 #include <dev/sbus/mbppio.h>
     75 #include <dev/sbus/magmareg.h>
     76 
     77 /* supported cards
     78  *
     79  *  The table below lists the cards that this driver is likely to
     80  *  be able to support.
     81  *
     82  *  Cards with parallel ports: except for the LC2+1Sp, they all use
     83  *  the CD1190 chip which I know nothing about.  I've tried to leave
     84  *  hooks for it so it shouldn't be too hard to add support later.
     85  *  (I think somebody is working on this separately)
     86  *
     87  *  Thanks to Bruce at Magma for telling me the hardware offsets.
     88  */
     89 static struct magma_board_info supported_cards[] = {
     90 	{
     91 		"MAGMA_Sp", "MAGMA,4_Sp", "Magma 4 Sp", 4, 0,
     92 		1, 0xa000, 0xc000, 0xe000, { 0x8000, 0, 0, 0 },
     93 		0, { 0, 0 }
     94 	},
     95 	{
     96 		"MAGMA_Sp", "MAGMA,8_Sp", "Magma 8 Sp", 8, 0,
     97 		2, 0xa000, 0xc000, 0xe000, { 0x4000, 0x6000, 0, 0 },
     98 		0, { 0, 0 }
     99 	},
    100 	{
    101 		"MAGMA_Sp", "MAGMA,_8HS_Sp", "Magma Fast 8 Sp", 8, 0,
    102 		2, 0x2000, 0x4000, 0x6000, { 0x8000, 0xa000, 0, 0 },
    103 		0, { 0, 0 }
    104 	},
    105 	{
    106 		"MAGMA_Sp", "MAGMA,_8SP_422", "Magma 8 Sp - 422", 8, 0,
    107 		2, 0x2000, 0x4000, 0x6000, { 0x8000, 0xa000, 0, 0 },
    108 		0, { 0, 0 }
    109 	},
    110 	{
    111 		"MAGMA_Sp", "MAGMA,12_Sp", "Magma 12 Sp", 12, 0,
    112 		3, 0xa000, 0xc000, 0xe000, { 0x2000, 0x4000, 0x6000, 0 },
    113 		0, { 0, 0 }
    114 	},
    115 	{
    116 		"MAGMA_Sp", "MAGMA,16_Sp", "Magma 16 Sp", 16, 0,
    117 		4, 0xd000, 0xe000, 0xf000, { 0x8000, 0x9000, 0xa000, 0xb000 },
    118 		0, { 0, 0 }
    119 	},
    120 	{
    121 		"MAGMA_Sp", "MAGMA,16_Sp_2", "Magma 16 Sp", 16, 0,
    122 		4, 0x2000, 0x4000, 0x6000, { 0x8000, 0xa000, 0xc000, 0xe000 },
    123 		0, { 0, 0 }
    124 	},
    125 	{
    126 		"MAGMA_Sp", "MAGMA,16HS_Sp", "Magma Fast 16 Sp", 16, 0,
    127 		4, 0x2000, 0x4000, 0x6000, { 0x8000, 0xa000, 0xc000, 0xe000 },
    128 		0, { 0, 0 }
    129 	},
    130 	{
    131 		"MAGMA_Sp", "MAGMA,21_Sp", "Magma LC 2+1 Sp", 2, 1,
    132 		1, 0xa000, 0xc000, 0xe000, { 0x8000, 0, 0, 0 },
    133 		0, { 0, 0 }
    134 	},
    135 	{
    136 		"MAGMA_Sp", "MAGMA,21HS_Sp", "Magma 2+1 Sp", 2, 1,
    137 		1, 0xa000, 0xc000, 0xe000, { 0x4000, 0, 0, 0 },
    138 		1, { 0x6000, 0 }
    139 	},
    140 	{
    141 		"MAGMA_Sp", "MAGMA,41_Sp", "Magma 4+1 Sp", 4, 1,
    142 		1, 0xa000, 0xc000, 0xe000, { 0x4000, 0, 0, 0 },
    143 		1, { 0x6000, 0 }
    144 	},
    145 	{
    146 		"MAGMA_Sp", "MAGMA,82_Sp", "Magma 8+2 Sp", 8, 2,
    147 		2, 0xd000, 0xe000, 0xf000, { 0x8000, 0x9000, 0, 0 },
    148 		2, { 0xa000, 0xb000 }
    149 	},
    150 	{
    151 		"MAGMA_Sp", "MAGMA,P1_Sp", "Magma P1 Sp", 0, 1,
    152 		0, 0, 0, 0, { 0, 0, 0, 0 },
    153 		1, { 0x8000, 0 }
    154 	},
    155 	{
    156 		"MAGMA_Sp", "MAGMA,P2_Sp", "Magma P2 Sp", 0, 2,
    157 		0, 0, 0, 0, { 0, 0, 0, 0 },
    158 		2, { 0x4000, 0x8000 }
    159 	},
    160 	{
    161 		"MAGMA 2+1HS Sp", "", "Magma 2+1HS Sp", 2, 0,
    162 		1, 0xa000, 0xc000, 0xe000, { 0x4000, 0, 0, 0 },
    163 		1, { 0x8000, 0 }
    164 	},
    165 	{
    166 		NULL, NULL, NULL, 0, 0,
    167 		0, 0, 0, 0, { 0, 0, 0, 0 },
    168 		0, { 0, 0 }
    169 	}
    170 };
    171 
    172 /************************************************************************
    173  *
    174  *  Autoconfig Stuff
    175  */
    176 
    177 CFATTACH_DECL(magma, sizeof(struct magma_softc),
    178     magma_match, magma_attach, NULL, NULL);
    179 
    180 CFATTACH_DECL(mtty, sizeof(struct mtty_softc),
    181     mtty_match, mtty_attach, NULL, NULL);
    182 
    183 CFATTACH_DECL(mbpp, sizeof(struct mbpp_softc),
    184     mbpp_match, mbpp_attach, NULL, NULL);
    185 
    186 extern struct cfdriver mtty_cd;
    187 extern struct cfdriver mbpp_cd;
    188 
    189 dev_type_open(mttyopen);
    190 dev_type_close(mttyclose);
    191 dev_type_read(mttyread);
    192 dev_type_write(mttywrite);
    193 dev_type_ioctl(mttyioctl);
    194 dev_type_stop(mttystop);
    195 dev_type_tty(mttytty);
    196 dev_type_poll(mttypoll);
    197 
    198 const struct cdevsw mtty_cdevsw = {
    199 	mttyopen, mttyclose, mttyread, mttywrite, mttyioctl,
    200 	mttystop, mttytty, mttypoll, nommap, ttykqfilter, D_TTY
    201 };
    202 
    203 dev_type_open(mbppopen);
    204 dev_type_close(mbppclose);
    205 dev_type_read(mbpp_rw);
    206 dev_type_ioctl(mbppioctl);
    207 
    208 const struct cdevsw mbpp_cdevsw = {
    209 	mbppopen, mbppclose, mbpp_rw, mbpp_rw, mbppioctl,
    210 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
    211 };
    212 
    213 /************************************************************************
    214  *
    215  *  CD1400 Routines
    216  *
    217  *	cd1400_compute_baud		calculate COR/BPR register values
    218  *	cd1400_write_ccr		write a value to CD1400 ccr
    219  *	cd1400_read_reg			read from a CD1400 register
    220  *	cd1400_write_reg		write to a CD1400 register
    221  *	cd1400_enable_transmitter	enable transmitting on CD1400 channel
    222  */
    223 
    224 /*
    225  * compute the bpr/cor pair for any baud rate
    226  * returns 0 for success, 1 for failure
    227  */
    228 int
    229 cd1400_compute_baud(speed, clock, cor, bpr)
    230 	speed_t speed;
    231 	int clock;
    232 	int *cor, *bpr;
    233 {
    234 	int c, co, br;
    235 
    236 	if( speed < 50 || speed > 150000 )
    237 		return(1);
    238 
    239 	for( c = 0, co = 8 ; co <= 2048 ; co <<= 2, c++ ) {
    240 		br = ((clock * 1000000) + (co * speed) / 2) / (co * speed);
    241 		if( br < 0x100 ) {
    242 			*bpr = br;
    243 			*cor = c;
    244 			return(0);
    245 		}
    246 	}
    247 
    248 	return(1);
    249 }
    250 
    251 /*
    252  * Write a CD1400 channel command, should have a timeout?
    253  */
    254 inline void
    255 cd1400_write_ccr(cd, cmd)
    256 	struct cd1400 *cd;
    257 	u_char cmd;
    258 {
    259 	while( cd1400_read_reg(cd, CD1400_CCR) )
    260 		;
    261 
    262 	cd1400_write_reg(cd, CD1400_CCR, cmd);
    263 }
    264 
    265 /*
    266  * read a value from a cd1400 register
    267  */
    268 inline u_char
    269 cd1400_read_reg(cd, reg)
    270 	struct cd1400 *cd;
    271 	int reg;
    272 {
    273 	return(cd->cd_reg[reg]);
    274 }
    275 
    276 /*
    277  * write a value to a cd1400 register
    278  */
    279 inline void
    280 cd1400_write_reg(cd, reg, value)
    281 	struct cd1400 *cd;
    282 	int reg;
    283 	u_char value;
    284 {
    285 	cd->cd_reg[reg] = value;
    286 }
    287 
    288 /*
    289  * enable transmit service requests for cd1400 channel
    290  */
    291 void
    292 cd1400_enable_transmitter(cd, channel)
    293 	struct cd1400 *cd;
    294 	int channel;
    295 {
    296 	int s, srer;
    297 
    298 	s = spltty();
    299 	cd1400_write_reg(cd, CD1400_CAR, channel);
    300 	srer = cd1400_read_reg(cd, CD1400_SRER);
    301 	SET(srer, CD1400_SRER_TXRDY);
    302 	cd1400_write_reg(cd, CD1400_SRER, srer);
    303 	splx(s);
    304 }
    305 
    306 /************************************************************************
    307  *
    308  *  CD1190 Routines
    309  */
    310 
    311 /* well, there are none yet */
    312 
    313 /************************************************************************
    314  *
    315  *  Magma Routines
    316  *
    317  * magma_match		reports if we have a magma board available
    318  * magma_attach		attaches magma boards to the sbus
    319  * magma_hard		hardware level interrupt routine
    320  * magma_soft		software level interrupt routine
    321  */
    322 
    323 int
    324 magma_match(parent, cf, aux)
    325 	struct device *parent;
    326 	struct cfdata *cf;
    327 	void *aux;
    328 {
    329 	struct sbus_attach_args *sa = aux;
    330 	struct magma_board_info *card;
    331 
    332 	/* See if we support this device */
    333 	for (card = supported_cards; ; card++) {
    334 		if (card->mb_sbusname == NULL)
    335 			/* End of table: no match */
    336 			return (0);
    337 		if (strcmp(sa->sa_name, card->mb_sbusname) == 0)
    338 			break;
    339 	}
    340 
    341 	dprintf(("magma: matched `%s'\n", sa->sa_name));
    342 	dprintf(("magma: magma_prom `%s'\n",
    343 		prom_getpropstring(sa->sa_node, "magma_prom")));
    344 	dprintf(("magma: intlevels `%s'\n",
    345 		prom_getpropstring(sa->sa_node, "intlevels")));
    346 	dprintf(("magma: chiprev `%s'\n",
    347 		prom_getpropstring(sa->sa_node, "chiprev")));
    348 	dprintf(("magma: clock `%s'\n",
    349 		prom_getpropstring(sa->sa_node, "clock")));
    350 
    351 	return (1);
    352 }
    353 
    354 void
    355 magma_attach(parent, self, aux)
    356 	struct device *parent;
    357 	struct device *self;
    358 	void *aux;
    359 {
    360 	struct sbus_attach_args *sa = aux;
    361 	struct magma_softc *sc = device_private(self);
    362 	struct magma_board_info *card;
    363 	bus_space_handle_t bh;
    364 	char *magma_prom, *clockstr;
    365 	int cd_clock;
    366 	int node, chip;
    367 
    368 	node = sa->sa_node;
    369 
    370 	/*
    371 	 * Find the card model.
    372 	 * Older models all have sbus node name `MAGMA_Sp' (see
    373 	 * `supported_cards[]' above), and must be distinguished
    374 	 * by the `magma_prom' property.
    375 	 */
    376 	magma_prom = prom_getpropstring(node, "magma_prom");
    377 
    378 	for (card = supported_cards; card->mb_name != NULL; card++) {
    379 		if (strcmp(sa->sa_name, card->mb_sbusname) != 0)
    380 			/* Sbus node name doesn't match */
    381 			continue;
    382 		if (strcmp(magma_prom, card->mb_name) == 0)
    383 			/* Model name match */
    384 			break;
    385 	}
    386 
    387 	if( card->mb_name == NULL ) {
    388 		printf(": %s (unsupported)\n", magma_prom);
    389 		return;
    390 	}
    391 
    392 	dprintf((" addr %p", sc));
    393 	printf(": %s\n", card->mb_realname);
    394 
    395 	sc->ms_board = card;
    396 	sc->ms_ncd1400 = card->mb_ncd1400;
    397 	sc->ms_ncd1190 = card->mb_ncd1190;
    398 
    399 	if (sbus_bus_map(sa->sa_bustag,
    400 			 sa->sa_slot, sa->sa_offset, sa->sa_size,
    401 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    402 		aprint_error("%s @ sbus: cannot map registers\n",
    403 			device_xname(self));
    404 		return;
    405 	}
    406 
    407 	/* the SVCACK* lines are daisychained */
    408 	sc->ms_svcackr = (char *)bus_space_vaddr(sa->sa_bustag, bh)
    409 		+ card->mb_svcackr;
    410 	sc->ms_svcackt = (char *)bus_space_vaddr(sa->sa_bustag, bh)
    411 		+ card->mb_svcackt;
    412 	sc->ms_svcackm = (char *)bus_space_vaddr(sa->sa_bustag, bh)
    413 		+ card->mb_svcackm;
    414 
    415 	/*
    416 	 * Find the clock speed; it's the same for all CD1400 chips
    417 	 * on the board.
    418 	 */
    419 	clockstr = prom_getpropstring(node, "clock");
    420 	if (*clockstr == '\0')
    421 		/* Default to 25MHz */
    422 		cd_clock = 25;
    423 	else {
    424 		cd_clock = 0;
    425 		while (*clockstr != '\0')
    426 			cd_clock = (cd_clock * 10) + (*clockstr++ - '0');
    427 	}
    428 
    429 	/* init the cd1400 chips */
    430 	for( chip = 0 ; chip < card->mb_ncd1400 ; chip++ ) {
    431 		struct cd1400 *cd = &sc->ms_cd1400[chip];
    432 
    433 		cd->cd_clock = cd_clock;
    434 		cd->cd_reg = (char *)bus_space_vaddr(sa->sa_bustag, bh) +
    435 		    card->mb_cd1400[chip];
    436 
    437 		/* prom_getpropstring(node, "chiprev"); */
    438 		/* seemingly the Magma drivers just ignore the propstring */
    439 		cd->cd_chiprev = cd1400_read_reg(cd, CD1400_GFRCR);
    440 
    441 		dprintf(("%s attach CD1400 %d addr %p rev %x clock %dMHz\n",
    442 			device_xname(&sc->ms_dev), chip,
    443 			cd->cd_reg, cd->cd_chiprev, cd->cd_clock));
    444 
    445 		/* clear GFRCR */
    446 		cd1400_write_reg(cd, CD1400_GFRCR, 0x00);
    447 
    448 		/* reset whole chip */
    449 		cd1400_write_ccr(cd, CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
    450 
    451 		/* wait for revision code to be restored */
    452 		while( cd1400_read_reg(cd, CD1400_GFRCR) != cd->cd_chiprev )
    453 		        ;
    454 
    455 		/* set the Prescaler Period Register to tick at 1ms */
    456 		cd1400_write_reg(cd, CD1400_PPR,
    457 			((cd->cd_clock * 1000000 / CD1400_PPR_PRESCALER + 500) / 1000));
    458 
    459 		/* The LC2+1Sp card is the only card that doesn't have
    460 		 * a CD1190 for the parallel port, but uses channel 0 of
    461 		 * the CD1400, so we make a note of it for later and set up
    462 		 * the CD1400 for parallel mode operation.
    463 		 */
    464 		if( card->mb_npar && card->mb_ncd1190 == 0 ) {
    465 			cd1400_write_reg(cd, CD1400_GCR, CD1400_GCR_PARALLEL);
    466 			cd->cd_parmode = 1;
    467 		}
    468 	}
    469 
    470 	/* init the cd1190 chips */
    471 	for( chip = 0 ; chip < card->mb_ncd1190 ; chip++ ) {
    472 		struct cd1190 *cd = &sc->ms_cd1190[chip];
    473 
    474 		cd->cd_reg = (char *)bus_space_vaddr(sa->sa_bustag, bh) +
    475 		    card->mb_cd1190[chip];
    476 
    477 		/* XXX don't know anything about these chips yet */
    478 		printf("%s: CD1190 %d addr %p (unsupported)\n",
    479 			device_xname(self), chip, cd->cd_reg);
    480 	}
    481 
    482 	sbus_establish(&sc->ms_sd, &sc->ms_dev);
    483 
    484 	/* configure the children */
    485 	(void)config_found(self, mtty_match, NULL);
    486 	(void)config_found(self, mbpp_match, NULL);
    487 
    488 	/*
    489 	 * Establish the interrupt handlers.
    490 	 */
    491 	if (sa->sa_nintr == 0)
    492 		return;		/* No interrupts to service!? */
    493 
    494 	(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_SERIAL,
    495 				 magma_hard, sc);
    496 	sc->ms_sicookie = softint_establish(SOFTINT_SERIAL, magma_soft, sc);
    497 	if (sc->ms_sicookie == NULL) {
    498 		aprint_normal("\n");
    499 		aprint_error_dev(&sc->ms_dev, "cannot establish soft int handler\n");
    500 		return;
    501 	}
    502 	evcnt_attach_dynamic(&sc->ms_intrcnt, EVCNT_TYPE_INTR, NULL,
    503 	    device_xname(&sc->ms_dev), "intr");
    504 }
    505 
    506 /*
    507  * hard interrupt routine
    508  *
    509  *  returns 1 if it handled it, otherwise 0
    510  *
    511  *  runs at IPL_SERIAL
    512  */
    513 int
    514 magma_hard(arg)
    515 	void *arg;
    516 {
    517 	struct magma_softc *sc = arg;
    518 	struct cd1400 *cd;
    519 	int chip, status = 0;
    520 	int serviced = 0;
    521 	int needsoftint = 0;
    522 
    523 	/*
    524 	 * check status of all the CD1400 chips
    525 	 */
    526 	for( chip = 0 ; chip < sc->ms_ncd1400 ; chip++ )
    527 		status |= cd1400_read_reg(&sc->ms_cd1400[chip], CD1400_SVRR);
    528 
    529 	if( ISSET(status, CD1400_SVRR_RXRDY) ) {
    530 		u_char rivr = *sc->ms_svcackr;	/* enter rx service context */
    531 		int port = rivr >> 4;
    532 
    533 		if( rivr & (1<<3) ) {			/* parallel port */
    534 			struct mbpp_port *mbpp;
    535 			int n_chars;
    536 
    537 			mbpp = &sc->ms_mbpp->ms_port[port];
    538 			cd = mbpp->mp_cd1400;
    539 
    540 			/* don't think we have to handle exceptions */
    541 			n_chars = cd1400_read_reg(cd, CD1400_RDCR);
    542 			while (n_chars--) {
    543 				if( mbpp->mp_cnt == 0 ) {
    544 					SET(mbpp->mp_flags, MBPPF_WAKEUP);
    545 					needsoftint = 1;
    546 					break;
    547 				}
    548 				*mbpp->mp_ptr = cd1400_read_reg(cd,CD1400_RDSR);
    549 				mbpp->mp_ptr++;
    550 				mbpp->mp_cnt--;
    551 			}
    552 		} else {				/* serial port */
    553 			struct mtty_port *mtty;
    554 			u_char *ptr, n_chars, line_stat;
    555 
    556 			mtty = &sc->ms_mtty->ms_port[port];
    557 			cd = mtty->mp_cd1400;
    558 
    559 			if( ISSET(rivr, CD1400_RIVR_EXCEPTION) ) {
    560 				line_stat = cd1400_read_reg(cd, CD1400_RDSR);
    561 				n_chars = 1;
    562 			} else { /* no exception, received data OK */
    563 				line_stat = 0;
    564 				n_chars = cd1400_read_reg(cd, CD1400_RDCR);
    565 			}
    566 
    567 			ptr = mtty->mp_rput;
    568 			while( n_chars-- ) {
    569 				*ptr++ = line_stat;
    570 				*ptr++ = cd1400_read_reg(cd, CD1400_RDSR);
    571 				if( ptr == mtty->mp_rend ) ptr = mtty->mp_rbuf;
    572 				if( ptr == mtty->mp_rget ) {
    573 					if( ptr == mtty->mp_rbuf )
    574 						ptr = mtty->mp_rend;
    575 					ptr -= 2;
    576 					SET(mtty->mp_flags, MTTYF_RING_OVERFLOW);
    577 					break;
    578 				}
    579 			}
    580 			mtty->mp_rput = ptr;
    581 
    582 			needsoftint = 1;
    583 		}
    584 
    585 		cd1400_write_reg(cd, CD1400_EOSRR, 0);	/* end service context */
    586 		serviced = 1;
    587 	} /* if(rx_service...) */
    588 
    589 	if( ISSET(status, CD1400_SVRR_MDMCH) ) {
    590 		u_char mivr = *sc->ms_svcackm;	/* enter mdm service context */
    591 		int port = mivr >> 4;
    592 		struct mtty_port *mtty;
    593 		int carrier;
    594 		u_char msvr;
    595 
    596 		/*
    597 		 * Handle CD (LC2+1Sp = DSR) changes.
    598 		 */
    599 		mtty = &sc->ms_mtty->ms_port[port];
    600 		cd = mtty->mp_cd1400;
    601 		msvr = cd1400_read_reg(cd, CD1400_MSVR2);
    602 		carrier = ISSET(msvr, cd->cd_parmode ? CD1400_MSVR2_DSR : CD1400_MSVR2_CD);
    603 
    604 		if( mtty->mp_carrier != carrier ) {
    605 			SET(mtty->mp_flags, MTTYF_CARRIER_CHANGED);
    606 			mtty->mp_carrier = carrier;
    607 			needsoftint = 1;
    608 		}
    609 
    610 		cd1400_write_reg(cd, CD1400_EOSRR, 0);	/* end service context */
    611 		serviced = 1;
    612 	} /* if(mdm_service...) */
    613 
    614 	if( ISSET(status, CD1400_SVRR_TXRDY) ) {
    615 		u_char tivr = *sc->ms_svcackt;	/* enter tx service context */
    616 		int port = tivr >> 4;
    617 
    618 		if( tivr & (1<<3) ) {	/* parallel port */
    619 			struct mbpp_port *mbpp;
    620 
    621 			mbpp = &sc->ms_mbpp->ms_port[port];
    622 			cd = mbpp->mp_cd1400;
    623 
    624 			if( mbpp->mp_cnt ) {
    625 				int count = 0;
    626 
    627 				/* fill the fifo */
    628 				while (mbpp->mp_cnt &&
    629 					count++ < CD1400_PAR_FIFO_SIZE) {
    630 					cd1400_write_reg(cd, CD1400_TDR,
    631 							 *mbpp->mp_ptr);
    632 					mbpp->mp_ptr++;
    633 					mbpp->mp_cnt--;
    634 				}
    635 			} else {
    636 				/*
    637 				 * fifo is empty and we got no more data
    638 				 * to send, so shut off interrupts and
    639 				 * signal for a wakeup, which can't be
    640 				 * done here in case we beat mbpp_send to
    641 				 * the tsleep call (we are running at >spltty)
    642 				 */
    643 				cd1400_write_reg(cd, CD1400_SRER, 0);
    644 				SET(mbpp->mp_flags, MBPPF_WAKEUP);
    645 				needsoftint = 1;
    646 			}
    647 		} else {		/* serial port */
    648 			struct mtty_port *mtty;
    649 			struct tty *tp;
    650 
    651 			mtty = &sc->ms_mtty->ms_port[port];
    652 			cd = mtty->mp_cd1400;
    653 			tp = mtty->mp_tty;
    654 
    655 			if( !ISSET(mtty->mp_flags, MTTYF_STOP) ) {
    656 				int count = 0;
    657 
    658 				/* check if we should start/stop a break */
    659 				if( ISSET(mtty->mp_flags, MTTYF_SET_BREAK) ) {
    660 					cd1400_write_reg(cd, CD1400_TDR, 0);
    661 					cd1400_write_reg(cd, CD1400_TDR, 0x81);
    662 					/* should we delay too? */
    663 					CLR(mtty->mp_flags, MTTYF_SET_BREAK);
    664 					count += 2;
    665 				}
    666 
    667 				if( ISSET(mtty->mp_flags, MTTYF_CLR_BREAK) ) {
    668 					cd1400_write_reg(cd, CD1400_TDR, 0);
    669 					cd1400_write_reg(cd, CD1400_TDR, 0x83);
    670 					CLR(mtty->mp_flags, MTTYF_CLR_BREAK);
    671 					count += 2;
    672 				}
    673 
    674 				/* I don't quite fill the fifo in case the last one is a
    675 				 * NULL which I have to double up because its the escape
    676 				 * code for embedded transmit characters.
    677 				 */
    678 				while( mtty->mp_txc > 0 && count < CD1400_TX_FIFO_SIZE - 1 ) {
    679 					u_char ch;
    680 
    681 					ch = *mtty->mp_txp;
    682 
    683 					mtty->mp_txc--;
    684 					mtty->mp_txp++;
    685 
    686 					if( ch == 0 ) {
    687 						cd1400_write_reg(cd, CD1400_TDR, ch);
    688 						count++;
    689 					}
    690 
    691 					cd1400_write_reg(cd, CD1400_TDR, ch);
    692 					count++;
    693 				}
    694 			}
    695 
    696 			/* if we ran out of work or are requested to STOP then
    697 			 * shut off the txrdy interrupts and signal DONE to flush
    698 			 * out the chars we have sent.
    699 			 */
    700 			if( mtty->mp_txc == 0 || ISSET(mtty->mp_flags, MTTYF_STOP) ) {
    701 				register int srer;
    702 
    703 				srer = cd1400_read_reg(cd, CD1400_SRER);
    704 				CLR(srer, CD1400_SRER_TXRDY);
    705 				cd1400_write_reg(cd, CD1400_SRER, srer);
    706 				CLR(mtty->mp_flags, MTTYF_STOP);
    707 
    708 				SET(mtty->mp_flags, MTTYF_DONE);
    709 				needsoftint = 1;
    710 			}
    711 		}
    712 
    713 		cd1400_write_reg(cd, CD1400_EOSRR, 0);	/* end service context */
    714 		serviced = 1;
    715 	} /* if(tx_service...) */
    716 
    717 	/* XXX service CD1190 interrupts too
    718 	for( chip = 0 ; chip < sc->ms_ncd1190 ; chip++ ) {
    719 	}
    720 	*/
    721 
    722 	if (needsoftint)
    723 		/* trigger the soft interrupt */
    724 		softint_schedule(sc->ms_sicookie);
    725 
    726 	return(serviced);
    727 }
    728 
    729 /*
    730  * magma soft interrupt handler
    731  *
    732  * runs at IPL_SOFTSERIAL
    733  */
    734 void
    735 magma_soft(arg)
    736 	void *arg;
    737 {
    738 	struct magma_softc *sc = arg;
    739 	struct mtty_softc *mtty = sc->ms_mtty;
    740 	struct mbpp_softc *mbpp = sc->ms_mbpp;
    741 	int port;
    742 	int s, flags;
    743 
    744 	if (mtty == NULL)
    745 		goto chkbpp;
    746 
    747 	/*
    748 	 * check the tty ports to see what needs doing
    749 	 */
    750 	for( port = 0 ; port < mtty->ms_nports ; port++ ) {
    751 		struct mtty_port *mp = &mtty->ms_port[port];
    752 		struct tty *tp = mp->mp_tty;
    753 
    754 		if( !ISSET(tp->t_state, TS_ISOPEN) )
    755 			continue;
    756 
    757 		/*
    758 		 * handle any received data
    759 		 */
    760 		while( mp->mp_rget != mp->mp_rput ) {
    761 			u_char stat;
    762 			int data;
    763 
    764 			stat = mp->mp_rget[0];
    765 			data = mp->mp_rget[1];
    766 			mp->mp_rget = ((mp->mp_rget + 2) == mp->mp_rend)
    767 				? mp->mp_rbuf : (mp->mp_rget + 2);
    768 
    769 			if( stat & (CD1400_RDSR_BREAK | CD1400_RDSR_FE) )
    770 				data |= TTY_FE;
    771 			if( stat & CD1400_RDSR_PE )
    772 				data |= TTY_PE;
    773 
    774 			if( stat & CD1400_RDSR_OE )
    775 				log(LOG_WARNING, "%s%x: fifo overflow\n",
    776 				    device_xname(&mtty->ms_dev), port);
    777 
    778 			(*tp->t_linesw->l_rint)(data, tp);
    779 		}
    780 
    781 		s = splserial();	/* block out hard interrupt routine */
    782 		flags = mp->mp_flags;
    783 		CLR(mp->mp_flags, MTTYF_DONE | MTTYF_CARRIER_CHANGED | MTTYF_RING_OVERFLOW);
    784 		splx(s);	/* ok */
    785 
    786 		if( ISSET(flags, MTTYF_CARRIER_CHANGED) ) {
    787 			dprintf(("%s%x: cd %s\n", device_xname(&mtty->ms_dev),
    788 				port, mp->mp_carrier ? "on" : "off"));
    789 			(*tp->t_linesw->l_modem)(tp, mp->mp_carrier);
    790 		}
    791 
    792 		if( ISSET(flags, MTTYF_RING_OVERFLOW) ) {
    793 			log(LOG_WARNING, "%s%x: ring buffer overflow\n",
    794 			    device_xname(&mtty->ms_dev), port);
    795 		}
    796 
    797 		if( ISSET(flags, MTTYF_DONE) ) {
    798 			ndflush(&tp->t_outq, mp->mp_txp - tp->t_outq.c_cf);
    799 			CLR(tp->t_state, TS_BUSY);
    800 			(*tp->t_linesw->l_start)(tp);	/* might be some more */
    801 		}
    802 	} /* for(each mtty...) */
    803 
    804 
    805 chkbpp:
    806 	/*
    807 	 * Check the bpp ports (if any) to see what needs doing
    808 	 */
    809 	if (mbpp == NULL)
    810 		return;
    811 
    812 	for( port = 0 ; port < mbpp->ms_nports ; port++ ) {
    813 		struct mbpp_port *mp = &mbpp->ms_port[port];
    814 
    815 		if( !ISSET(mp->mp_flags, MBPPF_OPEN) )
    816 			continue;
    817 
    818 		s = splserial();
    819 		flags = mp->mp_flags;
    820 		CLR(mp->mp_flags, MBPPF_WAKEUP);
    821 		splx(s);
    822 
    823 		if( ISSET(flags, MBPPF_WAKEUP) ) {
    824 			wakeup(mp);
    825 		}
    826 
    827 	} /* for(each mbpp...) */
    828 }
    829 
    830 /************************************************************************
    831  *
    832  *  MTTY Routines
    833  *
    834  *	mtty_match		match one mtty device
    835  *	mtty_attach		attach mtty devices
    836  *	mttyopen		open mtty device
    837  *	mttyclose		close mtty device
    838  *	mttyread		read from mtty
    839  *	mttywrite		write to mtty
    840  *	mttyioctl		do ioctl on mtty
    841  *	mttytty			return tty pointer for mtty
    842  *	mttystop		stop mtty device
    843  *	mtty_start		start mtty device
    844  *	mtty_param		set mtty parameters
    845  *	mtty_modem_control	set modem control lines
    846  */
    847 
    848 int
    849 mtty_match(parent, cf, args)
    850 	struct device *parent;
    851 	struct cfdata *cf;
    852 	void *args;
    853 {
    854 	struct magma_softc *sc = device_private(parent);
    855 
    856 	return( args == mtty_match && sc->ms_board->mb_nser && sc->ms_mtty == NULL );
    857 }
    858 
    859 void
    860 mtty_attach(parent, dev, args)
    861 	struct device *parent;
    862 	struct device *dev;
    863 	void *args;
    864 {
    865 	struct magma_softc *sc = device_private(parent);
    866 	struct mtty_softc *ms = device_private(dev);
    867 	int port, chip, chan;
    868 
    869 	sc->ms_mtty = ms;
    870 	dprintf((" addr %p", ms));
    871 
    872 	for( port = 0, chip = 0, chan = 0 ; port < sc->ms_board->mb_nser ; port++ ) {
    873 		struct mtty_port *mp = &ms->ms_port[port];
    874 		struct tty *tp;
    875 
    876 		mp->mp_cd1400 = &sc->ms_cd1400[chip];
    877 		if (mp->mp_cd1400->cd_parmode && chan == 0)
    878 			chan = 1; /* skip channel 0 if parmode */
    879 		mp->mp_channel = chan;
    880 
    881 		tp = ttymalloc();
    882 		if (tp == NULL) break;
    883 		tty_attach(tp);
    884 		tp->t_oproc = mtty_start;
    885 		tp->t_param = mtty_param;
    886 
    887 		mp->mp_tty = tp;
    888 
    889 		mp->mp_rbuf = malloc(MTTY_RBUF_SIZE, M_DEVBUF, M_NOWAIT);
    890 		if (mp->mp_rbuf == NULL) break;
    891 
    892 		mp->mp_rend = mp->mp_rbuf + MTTY_RBUF_SIZE;
    893 
    894 		chan = (chan + 1) % CD1400_NO_OF_CHANNELS;
    895 		if (chan == 0)
    896 			chip++;
    897 	}
    898 
    899 	ms->ms_nports = port;
    900 	printf(": %d tty%s\n", port, port == 1 ? "" : "s");
    901 }
    902 
    903 /*
    904  * open routine. returns zero if successful, else error code
    905  */
    906 int
    907 mttyopen(dev, flags, mode, l)
    908 	dev_t dev;
    909 	int flags;
    910 	int mode;
    911 	struct lwp *l;
    912 {
    913 	int card = MAGMA_CARD(dev);
    914 	int port = MAGMA_PORT(dev);
    915 	struct mtty_softc *ms;
    916 	struct mtty_port *mp;
    917 	struct tty *tp;
    918 	struct cd1400 *cd;
    919 	int error, s;
    920 
    921 	if ((ms = device_lookup_private(&mtty_cd, card)) == NULL
    922 	    || port >= ms->ms_nports )
    923 		return(ENXIO);	/* device not configured */
    924 
    925 	mp = &ms->ms_port[port];
    926 	tp = mp->mp_tty;
    927 	tp->t_dev = dev;
    928 
    929 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    930 		return (EBUSY);
    931 
    932 	s = spltty();
    933 
    934 	if( !ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    935 
    936 		/* set defaults */
    937 		ttychars(tp);
    938 		tp->t_iflag = TTYDEF_IFLAG;
    939 		tp->t_oflag = TTYDEF_OFLAG;
    940 		tp->t_cflag = TTYDEF_CFLAG;
    941 		if( ISSET(mp->mp_openflags, TIOCFLAG_CLOCAL) )
    942 			SET(tp->t_cflag, CLOCAL);
    943 		if( ISSET(mp->mp_openflags, TIOCFLAG_CRTSCTS) )
    944 			SET(tp->t_cflag, CRTSCTS);
    945 		if( ISSET(mp->mp_openflags, TIOCFLAG_MDMBUF) )
    946 			SET(tp->t_cflag, MDMBUF);
    947 		tp->t_lflag = TTYDEF_LFLAG;
    948 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    949 
    950 		/* init ring buffer */
    951 		mp->mp_rput = mp->mp_rget = mp->mp_rbuf;
    952 
    953 		/* reset CD1400 channel */
    954 		cd = mp->mp_cd1400;
    955 		cd1400_write_reg(cd, CD1400_CAR, mp->mp_channel);
    956 		cd1400_write_ccr(cd, CD1400_CCR_CMDRESET);
    957 
    958 		/* encode the port number in top half of LIVR */
    959 		cd1400_write_reg(cd, CD1400_LIVR, port << 4 );
    960 
    961 		/* sets parameters and raises DTR */
    962 		(void)mtty_param(tp, &tp->t_termios);
    963 
    964 		/* set tty watermarks */
    965 		ttsetwater(tp);
    966 
    967 		/* enable service requests */
    968 		cd1400_write_reg(cd, CD1400_SRER,
    969 				 CD1400_SRER_RXDATA | CD1400_SRER_MDMCH);
    970 
    971 		/* tell the tty about the carrier status */
    972 		if( ISSET(mp->mp_openflags, TIOCFLAG_SOFTCAR) ||
    973 		    mp->mp_carrier )
    974 			SET(tp->t_state, TS_CARR_ON);
    975 		else
    976 			CLR(tp->t_state, TS_CARR_ON);
    977 	}
    978 	splx(s);
    979 
    980 	error = ttyopen(tp, MTTY_DIALOUT(dev), ISSET(flags, O_NONBLOCK));
    981 	if (error != 0)
    982 		goto bad;
    983 
    984 	error = (*tp->t_linesw->l_open)(dev, tp);
    985 	if (error != 0)
    986 		goto bad;
    987 
    988 bad:
    989 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    990 		/*
    991 		 * We failed to open the device, and nobody else had it opened.
    992 		 * Clean up the state as appropriate.
    993 		 */
    994 		/* XXX - do that here */
    995 	}
    996 
    997 	return (error);
    998 }
    999 
   1000 /*
   1001  * close routine. returns zero if successful, else error code
   1002  */
   1003 int
   1004 mttyclose(dev, flag, mode, l)
   1005 	dev_t dev;
   1006 	int flag;
   1007 	int mode;
   1008 	struct lwp *l;
   1009 {
   1010 	struct mtty_softc *ms = device_lookup_private(&mtty_cd,
   1011 						      MAGMA_CARD(dev));
   1012 	struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
   1013 	struct tty *tp = mp->mp_tty;
   1014 	int s;
   1015 
   1016 	(*tp->t_linesw->l_close)(tp, flag);
   1017 	ttyclose(tp);
   1018 
   1019 	s = spltty();
   1020 
   1021 	/* if HUPCL is set, and the tty is no longer open
   1022 	 * shut down the port
   1023 	 */
   1024 	if( ISSET(tp->t_cflag, HUPCL) || !ISSET(tp->t_state, TS_ISOPEN) ) {
   1025 		/* XXX wait until FIFO is empty before turning off the channel
   1026 		struct cd1400 *cd = mp->mp_cd1400;
   1027 		*/
   1028 
   1029 		/* drop DTR and RTS */
   1030 		(void)mtty_modem_control(mp, 0, DMSET);
   1031 
   1032 		/* turn off the channel
   1033 		cd1400_write_reg(cd, CD1400_CAR, mp->mp_channel);
   1034 		cd1400_write_ccr(cd, CD1400_CCR_CMDRESET);
   1035 		*/
   1036 	}
   1037 
   1038 	splx(s);
   1039 
   1040 	return(0);
   1041 }
   1042 
   1043 /*
   1044  * Read routine
   1045  */
   1046 int
   1047 mttyread(dev, uio, flags)
   1048 	dev_t dev;
   1049 	struct uio *uio;
   1050 	int flags;
   1051 {
   1052 	struct mtty_softc *ms = device_lookup_private(&mtty_cd,
   1053 						      MAGMA_CARD(dev));
   1054 	struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
   1055 	struct tty *tp = mp->mp_tty;
   1056 
   1057 	return( (*tp->t_linesw->l_read)(tp, uio, flags) );
   1058 }
   1059 
   1060 /*
   1061  * Write routine
   1062  */
   1063 int
   1064 mttywrite(dev, uio, flags)
   1065 	dev_t dev;
   1066 	struct uio *uio;
   1067 	int flags;
   1068 {
   1069 	struct mtty_softc *ms = device_lookup_private(&mtty_cd,
   1070 						      MAGMA_CARD(dev));
   1071 	struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
   1072 	struct tty *tp = mp->mp_tty;
   1073 
   1074 	return( (*tp->t_linesw->l_write)(tp, uio, flags) );
   1075 }
   1076 
   1077 /*
   1078  * Poll routine
   1079  */
   1080 int
   1081 mttypoll(dev, events, l)
   1082 	dev_t dev;
   1083 	int events;
   1084 	struct lwp *l;
   1085 {
   1086 	struct mtty_softc *ms = device_lookup_private(&mtty_cd,
   1087 						      MAGMA_CARD(dev));
   1088 	struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
   1089 	struct tty *tp = mp->mp_tty;
   1090 
   1091 	return ((*tp->t_linesw->l_poll)(tp, events, l));
   1092 }
   1093 
   1094 /*
   1095  * return tty pointer
   1096  */
   1097 struct tty *
   1098 mttytty(dev)
   1099 	dev_t dev;
   1100 {
   1101 	struct mtty_softc *ms = device_lookup_private(&mtty_cd,
   1102 						      MAGMA_CARD(dev));
   1103 	struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
   1104 
   1105 	return(mp->mp_tty);
   1106 }
   1107 
   1108 /*
   1109  * ioctl routine
   1110  */
   1111 int
   1112 mttyioctl(dev, cmd, data, flags, l)
   1113 	dev_t dev;
   1114 	u_long cmd;
   1115 	void *data;
   1116 	int flags;
   1117 	struct lwp *l;
   1118 {
   1119 	struct mtty_softc *ms = device_lookup_private(&mtty_cd,
   1120 						      MAGMA_CARD(dev));
   1121 	struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
   1122 	struct tty *tp = mp->mp_tty;
   1123 	int error;
   1124 
   1125 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flags, l);
   1126 	if( error != EPASSTHROUGH ) return(error);
   1127 
   1128 	error = ttioctl(tp, cmd, data, flags, l);
   1129 	if( error != EPASSTHROUGH ) return(error);
   1130 
   1131 	error = 0;
   1132 
   1133 	switch(cmd) {
   1134 	case TIOCSBRK:	/* set break */
   1135 		SET(mp->mp_flags, MTTYF_SET_BREAK);
   1136 		cd1400_enable_transmitter(mp->mp_cd1400, mp->mp_channel);
   1137 		break;
   1138 
   1139 	case TIOCCBRK:	/* clear break */
   1140 		SET(mp->mp_flags, MTTYF_CLR_BREAK);
   1141 		cd1400_enable_transmitter(mp->mp_cd1400, mp->mp_channel);
   1142 		break;
   1143 
   1144 	case TIOCSDTR:	/* set DTR */
   1145 		mtty_modem_control(mp, TIOCM_DTR, DMBIS);
   1146 		break;
   1147 
   1148 	case TIOCCDTR:	/* clear DTR */
   1149 		mtty_modem_control(mp, TIOCM_DTR, DMBIC);
   1150 		break;
   1151 
   1152 	case TIOCMSET:	/* set modem lines */
   1153 		mtty_modem_control(mp, *((int *)data), DMSET);
   1154 		break;
   1155 
   1156 	case TIOCMBIS:	/* bit set modem lines */
   1157 		mtty_modem_control(mp, *((int *)data), DMBIS);
   1158 		break;
   1159 
   1160 	case TIOCMBIC:	/* bit clear modem lines */
   1161 		mtty_modem_control(mp, *((int *)data), DMBIC);
   1162 		break;
   1163 
   1164 	case TIOCMGET:	/* get modem lines */
   1165 		*((int *)data) = mtty_modem_control(mp, 0, DMGET);
   1166 		break;
   1167 
   1168 	case TIOCGFLAGS:
   1169 		*((int *)data) = mp->mp_openflags;
   1170 		break;
   1171 
   1172 	case TIOCSFLAGS:
   1173 		if (kauth_authorize_device_tty(l->l_cred,
   1174 		    KAUTH_DEVICE_TTY_PRIVSET, tp))
   1175 			error = EPERM;
   1176 		else
   1177 			mp->mp_openflags = *((int *)data) &
   1178 				(TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
   1179 				TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
   1180 		break;
   1181 
   1182 	default:
   1183 		error = EPASSTHROUGH;
   1184 	}
   1185 
   1186 	return(error);
   1187 }
   1188 
   1189 /*
   1190  * Stop output, e.g., for ^S or output flush.
   1191  */
   1192 void
   1193 mttystop(tp, flags)
   1194 	struct tty *tp;
   1195 	int flags;
   1196 {
   1197 	struct mtty_softc *ms = device_lookup_private(&mtty_cd,
   1198 						      MAGMA_CARD(tp->t_dev));
   1199 	struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(tp->t_dev)];
   1200 	int s;
   1201 
   1202 	s = spltty();
   1203 
   1204 	if( ISSET(tp->t_state, TS_BUSY) ) {
   1205 		if( !ISSET(tp->t_state, TS_TTSTOP) )
   1206 			SET(tp->t_state, TS_FLUSH);
   1207 
   1208 		/*
   1209 		 * the transmit interrupt routine will disable transmit when it
   1210 		 * notices that MTTYF_STOP has been set.
   1211 		 */
   1212 		SET(mp->mp_flags, MTTYF_STOP);
   1213 	}
   1214 
   1215 	splx(s);
   1216 }
   1217 
   1218 /*
   1219  * Start output, after a stop.
   1220  */
   1221 void
   1222 mtty_start(tp)
   1223 	struct tty *tp;
   1224 {
   1225 	struct mtty_softc *ms = device_lookup_private(&mtty_cd,
   1226 						      MAGMA_CARD(tp->t_dev));
   1227 	struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(tp->t_dev)];
   1228 	int s;
   1229 
   1230 	s = spltty();
   1231 
   1232 	/* we only need to do something if we are not already busy
   1233 	 * or delaying or stopped
   1234 	 */
   1235 	if( !ISSET(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY) ) {
   1236 		if (ttypull(tp)) {
   1237 			mp->mp_txc = ndqb(&tp->t_outq, 0);
   1238 			mp->mp_txp = tp->t_outq.c_cf;
   1239 			SET(tp->t_state, TS_BUSY);
   1240 			cd1400_enable_transmitter(mp->mp_cd1400, mp->mp_channel);
   1241 		}
   1242 	}
   1243 
   1244 	splx(s);
   1245 }
   1246 
   1247 /*
   1248  * set/get modem line status
   1249  *
   1250  * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
   1251  *
   1252  * note that DTR and RTS lines are exchanged, and that DSR is
   1253  * not available on the LC2+1Sp card (used as CD)
   1254  *
   1255  * only let them fiddle with RTS if CRTSCTS is not enabled
   1256  */
   1257 int
   1258 mtty_modem_control(mp, bits, howto)
   1259 	struct mtty_port *mp;
   1260 	int bits;
   1261 	int howto;
   1262 {
   1263 	struct cd1400 *cd = mp->mp_cd1400;
   1264 	struct tty *tp = mp->mp_tty;
   1265 	int s, msvr;
   1266 
   1267 	s = spltty();
   1268 
   1269 	cd1400_write_reg(cd, CD1400_CAR, mp->mp_channel);
   1270 
   1271 	switch(howto) {
   1272 	case DMGET:	/* get bits */
   1273 		bits = 0;
   1274 
   1275 		bits |= TIOCM_LE;
   1276 
   1277 		msvr = cd1400_read_reg(cd, CD1400_MSVR1);
   1278 		if( msvr & CD1400_MSVR1_RTS ) bits |= TIOCM_DTR;
   1279 
   1280 		msvr = cd1400_read_reg(cd, CD1400_MSVR2);
   1281 		if( msvr & CD1400_MSVR2_DTR ) bits |= TIOCM_RTS;
   1282 		if( msvr & CD1400_MSVR2_CTS ) bits |= TIOCM_CTS;
   1283 		if( msvr & CD1400_MSVR2_RI ) bits |= TIOCM_RI;
   1284 		if( msvr & CD1400_MSVR2_DSR ) bits |= (cd->cd_parmode ? TIOCM_CD : TIOCM_DSR);
   1285 		if( msvr & CD1400_MSVR2_CD ) bits |= (cd->cd_parmode ? 0 : TIOCM_CD);
   1286 
   1287 		break;
   1288 
   1289 	case DMSET:	/* reset bits */
   1290 		if( !ISSET(tp->t_cflag, CRTSCTS) )
   1291 			cd1400_write_reg(cd, CD1400_MSVR2, ((bits & TIOCM_RTS) ? CD1400_MSVR2_DTR : 0));
   1292 
   1293 		cd1400_write_reg(cd, CD1400_MSVR1, ((bits & TIOCM_DTR) ? CD1400_MSVR1_RTS : 0));
   1294 
   1295 		break;
   1296 
   1297 	case DMBIS:	/* set bits */
   1298 		if( (bits & TIOCM_RTS) && !ISSET(tp->t_cflag, CRTSCTS) )
   1299 			cd1400_write_reg(cd, CD1400_MSVR2, CD1400_MSVR2_DTR);
   1300 
   1301 		if( bits & TIOCM_DTR )
   1302 			cd1400_write_reg(cd, CD1400_MSVR1, CD1400_MSVR1_RTS);
   1303 
   1304 		break;
   1305 
   1306 	case DMBIC:	/* clear bits */
   1307 		if( (bits & TIOCM_RTS) && !ISSET(tp->t_cflag, CRTSCTS) )
   1308 			cd1400_write_reg(cd, CD1400_MSVR2, 0);
   1309 
   1310 		if( bits & TIOCM_DTR )
   1311 			cd1400_write_reg(cd, CD1400_MSVR1, 0);
   1312 
   1313 		break;
   1314 	}
   1315 
   1316 	splx(s);
   1317 	return(bits);
   1318 }
   1319 
   1320 /*
   1321  * Set tty parameters, returns error or 0 on success
   1322  */
   1323 int
   1324 mtty_param(tp, t)
   1325 	struct tty *tp;
   1326 	struct termios *t;
   1327 {
   1328 	struct mtty_softc *ms = device_lookup_private(&mtty_cd,
   1329 						      MAGMA_CARD(tp->t_dev));
   1330 	struct mtty_port *mp = &ms->ms_port[MAGMA_PORT(tp->t_dev)];
   1331 	struct cd1400 *cd = mp->mp_cd1400;
   1332 	int rbpr, tbpr, rcor, tcor;
   1333 	u_char mcor1 = 0, mcor2 = 0;
   1334 	int s, opt;
   1335 
   1336 	if( t->c_ospeed && cd1400_compute_baud(t->c_ospeed, cd->cd_clock, &tcor, &tbpr) )
   1337 		return(EINVAL);
   1338 
   1339 	if( t->c_ispeed && cd1400_compute_baud(t->c_ispeed, cd->cd_clock, &rcor, &rbpr) )
   1340 		return(EINVAL);
   1341 
   1342 	s = spltty();
   1343 
   1344 	/* hang up the line if ospeed is zero, else raise DTR */
   1345 	(void)mtty_modem_control(mp, TIOCM_DTR, (t->c_ospeed == 0 ? DMBIC : DMBIS));
   1346 
   1347 	/* select channel, done in mtty_modem_control() */
   1348 	/* cd1400_write_reg(cd, CD1400_CAR, mp->mp_channel); */
   1349 
   1350 	/* set transmit speed */
   1351 	if( t->c_ospeed ) {
   1352 		cd1400_write_reg(cd, CD1400_TCOR, tcor);
   1353 		cd1400_write_reg(cd, CD1400_TBPR, tbpr);
   1354 	}
   1355 
   1356 	/* set receive speed */
   1357 	if( t->c_ispeed ) {
   1358 		cd1400_write_reg(cd, CD1400_RCOR, rcor);
   1359 		cd1400_write_reg(cd, CD1400_RBPR, rbpr);
   1360 	}
   1361 
   1362 	/* enable transmitting and receiving on this channel */
   1363 	opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN | CD1400_CCR_RCVEN;
   1364 	cd1400_write_ccr(cd, opt);
   1365 
   1366 	/* set parity, data and stop bits */
   1367 	opt = 0;
   1368 	if( ISSET(t->c_cflag, PARENB) )
   1369 		opt |= (ISSET(t->c_cflag, PARODD) ? CD1400_COR1_PARODD : CD1400_COR1_PARNORMAL);
   1370 
   1371 	if( !ISSET(t->c_iflag, INPCK) )
   1372 		opt |= CD1400_COR1_NOINPCK; /* no parity checking */
   1373 
   1374 	if( ISSET(t->c_cflag, CSTOPB) )
   1375 		opt |= CD1400_COR1_STOP2;
   1376 
   1377 	switch( t->c_cflag & CSIZE ) {
   1378 	case CS5:
   1379 		opt |= CD1400_COR1_CS5;
   1380 		break;
   1381 
   1382 	case CS6:
   1383 		opt |= CD1400_COR1_CS6;
   1384 		break;
   1385 
   1386 	case CS7:
   1387 		opt |= CD1400_COR1_CS7;
   1388 		break;
   1389 
   1390 	default:
   1391 		opt |= CD1400_COR1_CS8;
   1392 		break;
   1393 	}
   1394 
   1395 	cd1400_write_reg(cd, CD1400_COR1, opt);
   1396 
   1397 	/*
   1398 	 * enable Embedded Transmit Commands (for breaks)
   1399 	 * use the CD1400 automatic CTS flow control if CRTSCTS is set
   1400 	 */
   1401 	opt = CD1400_COR2_ETC;
   1402 	if( ISSET(t->c_cflag, CRTSCTS) ) opt |= CD1400_COR2_CCTS_OFLOW;
   1403 	cd1400_write_reg(cd, CD1400_COR2, opt);
   1404 
   1405 	cd1400_write_reg(cd, CD1400_COR3, MTTY_RX_FIFO_THRESHOLD);
   1406 
   1407 	cd1400_write_ccr(cd, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR1 | CD1400_CCR_COR2 | CD1400_CCR_COR3);
   1408 
   1409 	cd1400_write_reg(cd, CD1400_COR4, CD1400_COR4_PFO_EXCEPTION);
   1410 	cd1400_write_reg(cd, CD1400_COR5, 0);
   1411 
   1412 	/*
   1413 	 * if automatic RTS handshaking enabled, set DTR threshold
   1414 	 * (RTS and DTR lines are switched, CD1400 thinks its DTR)
   1415 	 */
   1416 	if( ISSET(t->c_cflag, CRTSCTS) )
   1417 		mcor1 = MTTY_RX_DTR_THRESHOLD;
   1418 
   1419 	/* set up `carrier detect' interrupts */
   1420 	if( cd->cd_parmode ) {
   1421 		SET(mcor1, CD1400_MCOR1_DSRzd);
   1422 		SET(mcor2, CD1400_MCOR2_DSRod);
   1423 	} else {
   1424 		SET(mcor1, CD1400_MCOR1_CDzd);
   1425 		SET(mcor2, CD1400_MCOR2_CDod);
   1426 	}
   1427 
   1428 	cd1400_write_reg(cd, CD1400_MCOR1, mcor1);
   1429 	cd1400_write_reg(cd, CD1400_MCOR2, mcor2);
   1430 
   1431 	/* receive timeout 2ms */
   1432 	cd1400_write_reg(cd, CD1400_RTPR, 2);
   1433 
   1434 	splx(s);
   1435 	return(0);
   1436 }
   1437 
   1438 /************************************************************************
   1439  *
   1440  *  MBPP Routines
   1441  *
   1442  *	mbpp_match	match one mbpp device
   1443  *	mbpp_attach	attach mbpp devices
   1444  *	mbppopen	open mbpp device
   1445  *	mbppclose	close mbpp device
   1446  *	mbppioctl	do ioctl on mbpp
   1447  *	mbpp_rw		general rw routine
   1448  *	mbpp_timeout	rw timeout
   1449  *	mbpp_start	rw start after delay
   1450  *	mbpp_send	send data
   1451  *	mbpp_recv	recv data
   1452  */
   1453 
   1454 int
   1455 mbpp_match(parent, cf, args)
   1456 	struct device *parent;
   1457 	struct cfdata *cf;
   1458 	void *args;
   1459 {
   1460 	struct magma_softc *sc = device_private(parent);
   1461 
   1462 	return( args == mbpp_match && sc->ms_board->mb_npar && sc->ms_mbpp == NULL );
   1463 }
   1464 
   1465 void
   1466 mbpp_attach(parent, dev, args)
   1467 	struct device *parent;
   1468 	struct device *dev;
   1469 	void *args;
   1470 {
   1471 	struct magma_softc *sc = device_private(parent);
   1472 	struct mbpp_softc *ms = device_private(dev);
   1473 	struct mbpp_port *mp;
   1474 	int port;
   1475 
   1476 	sc->ms_mbpp = ms;
   1477 	dprintf((" addr %p", ms));
   1478 
   1479 	for( port = 0 ; port < sc->ms_board->mb_npar ; port++ ) {
   1480 		mp = &ms->ms_port[port];
   1481 
   1482 		callout_init(&mp->mp_timeout_ch, 0);
   1483 		callout_init(&mp->mp_start_ch, 0);
   1484 
   1485 		if( sc->ms_ncd1190 )
   1486 			mp->mp_cd1190 = &sc->ms_cd1190[port];
   1487 		else
   1488 			mp->mp_cd1400 = &sc->ms_cd1400[0];
   1489 	}
   1490 
   1491 	ms->ms_nports = port;
   1492 	printf(": %d port%s\n", port, port == 1 ? "" : "s");
   1493 }
   1494 
   1495 /*
   1496  * open routine. returns zero if successful, else error code
   1497  */
   1498 int
   1499 mbppopen(dev, flags, mode, l)
   1500 	dev_t dev;
   1501 	int flags;
   1502 	int mode;
   1503 	struct lwp *l;
   1504 {
   1505 	int card = MAGMA_CARD(dev);
   1506 	int port = MAGMA_PORT(dev);
   1507 	struct mbpp_softc *ms;
   1508 	struct mbpp_port *mp;
   1509 	int s;
   1510 
   1511 	if ((ms = device_lookup_private(&mbpp_cd, card)) == NULL
   1512 	    || port >= ms->ms_nports )
   1513 		return(ENXIO);
   1514 
   1515 	mp = &ms->ms_port[port];
   1516 
   1517 	s = spltty();
   1518 	if( ISSET(mp->mp_flags, MBPPF_OPEN) ) {
   1519 		splx(s);
   1520 		return(EBUSY);
   1521 	}
   1522 	SET(mp->mp_flags, MBPPF_OPEN);
   1523 	splx(s);
   1524 
   1525 	/* set defaults */
   1526 	mp->mp_burst = MBPP_BURST;
   1527 	mp->mp_timeout = mbpp_mstohz(MBPP_TIMEOUT);
   1528 	mp->mp_delay = mbpp_mstohz(MBPP_DELAY);
   1529 
   1530 	/* init chips */
   1531 	if( mp->mp_cd1400 ) {	/* CD1400 */
   1532 		struct cd1400 *cd = mp->mp_cd1400;
   1533 
   1534 		/* set up CD1400 channel */
   1535 		s = spltty();
   1536 		cd1400_write_reg(cd, CD1400_CAR, 0);
   1537 		cd1400_write_ccr(cd, CD1400_CCR_CMDRESET);
   1538 		cd1400_write_reg(cd, CD1400_LIVR, (1<<3));
   1539 		splx(s);
   1540 	} else {		/* CD1190 */
   1541 		mp->mp_flags = 0;
   1542 		return (ENXIO);
   1543 	}
   1544 
   1545 	return (0);
   1546 }
   1547 
   1548 /*
   1549  * close routine. returns zero if successful, else error code
   1550  */
   1551 int
   1552 mbppclose(dev, flag, mode, l)
   1553 	dev_t dev;
   1554 	int flag;
   1555 	int mode;
   1556 	struct lwp *l;
   1557 {
   1558 	struct mbpp_softc *ms = device_lookup_private(&mbpp_cd,
   1559 						      MAGMA_CARD(dev));
   1560 	struct mbpp_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
   1561 
   1562 	mp->mp_flags = 0;
   1563 	return(0);
   1564 }
   1565 
   1566 /*
   1567  * ioctl routine
   1568  */
   1569 int
   1570 mbppioctl(dev, cmd, data, flags, l)
   1571 	dev_t dev;
   1572 	u_long cmd;
   1573 	void *data;
   1574 	int flags;
   1575 	struct lwp *l;
   1576 {
   1577 	struct mbpp_softc *ms = device_lookup_private(&mbpp_cd,
   1578 						      MAGMA_CARD(dev));
   1579 	struct mbpp_port *mp = &ms->ms_port[MAGMA_PORT(dev)];
   1580 	struct mbpp_param *bp;
   1581 	int error = 0;
   1582 	int s;
   1583 
   1584 	switch(cmd) {
   1585 	case MBPPIOCSPARAM:
   1586 		bp = (struct mbpp_param *)data;
   1587 		if( bp->bp_burst < MBPP_BURST_MIN || bp->bp_burst > MBPP_BURST_MAX ||
   1588 		    bp->bp_delay < MBPP_DELAY_MIN || bp->bp_delay > MBPP_DELAY_MIN ) {
   1589 			error = EINVAL;
   1590 		} else {
   1591 			mp->mp_burst = bp->bp_burst;
   1592 			mp->mp_timeout = mbpp_mstohz(bp->bp_timeout);
   1593 			mp->mp_delay = mbpp_mstohz(bp->bp_delay);
   1594 		}
   1595 		break;
   1596 	case MBPPIOCGPARAM:
   1597 		bp = (struct mbpp_param *)data;
   1598 		bp->bp_burst = mp->mp_burst;
   1599 		bp->bp_timeout = mbpp_hztoms(mp->mp_timeout);
   1600 		bp->bp_delay = mbpp_hztoms(mp->mp_delay);
   1601 		break;
   1602 	case MBPPIOCGSTAT:
   1603 		/* XXX make this more generic */
   1604 		s = spltty();
   1605 		cd1400_write_reg(mp->mp_cd1400, CD1400_CAR, 0);
   1606 		*(int *)data = cd1400_read_reg(mp->mp_cd1400, CD1400_PSVR);
   1607 		splx(s);
   1608 		break;
   1609 	default:
   1610 		error = ENOTTY;
   1611 	}
   1612 
   1613 	return(error);
   1614 }
   1615 
   1616 int
   1617 mbpp_rw(dev, uio, flag)
   1618 	dev_t dev;
   1619 	struct uio *uio;
   1620 	int flag;
   1621 {
   1622 	int card = MAGMA_CARD(dev);
   1623 	int port = MAGMA_PORT(dev);
   1624 	struct mbpp_softc *ms = device_lookup_private(&mbpp_cd, card);
   1625 	struct mbpp_port *mp = &ms->ms_port[port];
   1626 	char *buffer, *ptr;
   1627 	int buflen, cnt, len;
   1628 	int s, error = 0;
   1629 	int gotdata = 0;
   1630 
   1631 	if( uio->uio_resid == 0 )
   1632 		return(0);
   1633 
   1634 	buflen = min(uio->uio_resid, mp->mp_burst);
   1635 	buffer = malloc(buflen, M_DEVBUF, M_WAITOK);
   1636 	if( buffer == NULL )
   1637 		return(ENOMEM);
   1638 
   1639 	SET(mp->mp_flags, MBPPF_UIO);
   1640 
   1641 	/*
   1642 	 * start timeout, if needed
   1643 	 */
   1644 	if( mp->mp_timeout > 0 ) {
   1645 		SET(mp->mp_flags, MBPPF_TIMEOUT);
   1646 		callout_reset(&mp->mp_timeout_ch, mp->mp_timeout,
   1647 		    mbpp_timeout, mp);
   1648 	}
   1649 
   1650 	len = cnt = 0;
   1651 	while( uio->uio_resid > 0 ) {
   1652 		len = min(buflen, uio->uio_resid);
   1653 		ptr = buffer;
   1654 
   1655 		if( uio->uio_rw == UIO_WRITE ) {
   1656 			error = uiomove(ptr, len, uio);
   1657 			if( error ) break;
   1658 		}
   1659 again:		/* goto bad */
   1660 		/* timed out?  */
   1661 		if( !ISSET(mp->mp_flags, MBPPF_UIO) )
   1662 			break;
   1663 
   1664 		/*
   1665 		 * perform the operation
   1666 		 */
   1667 		if( uio->uio_rw == UIO_WRITE ) {
   1668 			cnt = mbpp_send(mp, ptr, len);
   1669 		} else {
   1670 			cnt = mbpp_recv(mp, ptr, len);
   1671 		}
   1672 
   1673 		if( uio->uio_rw == UIO_READ ) {
   1674 			if( cnt ) {
   1675 				error = uiomove(ptr, cnt, uio);
   1676 				if( error ) break;
   1677 				gotdata++;
   1678 			}
   1679 			else if( gotdata )	/* consider us done */
   1680 				break;
   1681 		}
   1682 
   1683 		/* timed out?  */
   1684 		if( !ISSET(mp->mp_flags, MBPPF_UIO) )
   1685 			break;
   1686 
   1687 		/*
   1688 		 * poll delay?
   1689 		 */
   1690 		if( mp->mp_delay > 0 ) {
   1691 			s = splsoftclock();
   1692 			SET(mp->mp_flags, MBPPF_DELAY);
   1693 			callout_reset(&mp->mp_start_ch, mp->mp_delay,
   1694 			    mbpp_start, mp);
   1695 			error = tsleep(mp, PCATCH | PZERO, "mbppdelay", 0);
   1696 			splx(s);
   1697 			if( error ) break;
   1698 		}
   1699 
   1700 		/*
   1701 		 * don't call uiomove again until we used all the data we grabbed
   1702 		 */
   1703 		if( uio->uio_rw == UIO_WRITE && cnt != len ) {
   1704 			ptr += cnt;
   1705 			len -= cnt;
   1706 			cnt = 0;
   1707 			goto again;
   1708 		}
   1709 	}
   1710 
   1711 	/*
   1712 	 * clear timeouts
   1713 	 */
   1714 	s = splsoftclock();
   1715 	if( ISSET(mp->mp_flags, MBPPF_TIMEOUT) ) {
   1716 		callout_stop(&mp->mp_timeout_ch);
   1717 		CLR(mp->mp_flags, MBPPF_TIMEOUT);
   1718 	}
   1719 	if( ISSET(mp->mp_flags, MBPPF_DELAY) ) {
   1720 		callout_stop(&mp->mp_start_ch);
   1721 		CLR(mp->mp_flags, MBPPF_DELAY);
   1722 	}
   1723 	splx(s);
   1724 
   1725 	/*
   1726 	 * adjust for those chars that we uiomoved but never actually wrote
   1727 	 */
   1728 	if( uio->uio_rw == UIO_WRITE && cnt != len ) {
   1729 		uio->uio_resid += (len - cnt);
   1730 	}
   1731 
   1732 	free(buffer, M_DEVBUF);
   1733 	return(error);
   1734 }
   1735 
   1736 void
   1737 mbpp_timeout(arg)
   1738 	void *arg;
   1739 {
   1740 	struct mbpp_port *mp = arg;
   1741 
   1742 	CLR(mp->mp_flags, MBPPF_UIO | MBPPF_TIMEOUT);
   1743 	wakeup(mp);
   1744 }
   1745 
   1746 void
   1747 mbpp_start(arg)
   1748 	void *arg;
   1749 {
   1750 	struct mbpp_port *mp = arg;
   1751 
   1752 	CLR(mp->mp_flags, MBPPF_DELAY);
   1753 	wakeup(mp);
   1754 }
   1755 
   1756 int
   1757 mbpp_send(mp, ptr, len)
   1758 	struct mbpp_port *mp;
   1759 	void *ptr;
   1760 	int len;
   1761 {
   1762 	int s;
   1763 	struct cd1400 *cd = mp->mp_cd1400;
   1764 
   1765 	/* set up io information */
   1766 	mp->mp_ptr = ptr;
   1767 	mp->mp_cnt = len;
   1768 
   1769 	/* start transmitting */
   1770 	s = spltty();
   1771 	if( cd ) {
   1772 		cd1400_write_reg(cd, CD1400_CAR, 0);
   1773 
   1774 		/* output strobe width ~1microsecond */
   1775 		cd1400_write_reg(cd, CD1400_TBPR, 10);
   1776 
   1777 		/* enable channel */
   1778 		cd1400_write_ccr(cd, CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN);
   1779 		cd1400_write_reg(cd, CD1400_SRER, CD1400_SRER_TXRDY);
   1780 	}
   1781 
   1782 	/* ZZzzz... */
   1783 	tsleep(mp, PCATCH | PZERO, "mbpp_send", 0);
   1784 
   1785 	/* stop transmitting */
   1786 	if( cd ) {
   1787 		cd1400_write_reg(cd, CD1400_CAR, 0);
   1788 
   1789 		/* disable transmitter */
   1790 		cd1400_write_reg(cd, CD1400_SRER, 0);
   1791 		cd1400_write_ccr(cd, CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTDIS);
   1792 
   1793 		/* flush fifo */
   1794 		cd1400_write_ccr(cd, CD1400_CCR_CMDRESET | CD1400_CCR_FTF);
   1795 	}
   1796 	splx(s);
   1797 
   1798 	/* return number of chars sent */
   1799 	return(len - mp->mp_cnt);
   1800 }
   1801 
   1802 int
   1803 mbpp_recv(mp, ptr, len)
   1804 	struct mbpp_port *mp;
   1805 	void *ptr;
   1806 	int len;
   1807 {
   1808 	int s;
   1809 	struct cd1400 *cd = mp->mp_cd1400;
   1810 
   1811 	/* set up io information */
   1812 	mp->mp_ptr = ptr;
   1813 	mp->mp_cnt = len;
   1814 
   1815 	/* start receiving */
   1816 	s = spltty();
   1817 	if( cd ) {
   1818 	int rcor, rbpr;
   1819 
   1820 		cd1400_write_reg(cd, CD1400_CAR, 0);
   1821 
   1822 		/* input strobe at 100kbaud (10microseconds) */
   1823 		cd1400_compute_baud(100000, cd->cd_clock, &rcor, &rbpr);
   1824 		cd1400_write_reg(cd, CD1400_RCOR, rcor);
   1825 		cd1400_write_reg(cd, CD1400_RBPR, rbpr);
   1826 
   1827 		/* rx threshold */
   1828 		cd1400_write_reg(cd, CD1400_COR3, MBPP_RX_FIFO_THRESHOLD);
   1829 		cd1400_write_ccr(cd, CD1400_CCR_CMDCORCHG | CD1400_CCR_COR3);
   1830 
   1831 		/* enable channel */
   1832 		cd1400_write_ccr(cd, CD1400_CCR_CMDCHANCTL | CD1400_CCR_RCVEN);
   1833 		cd1400_write_reg(cd, CD1400_SRER, CD1400_SRER_RXDATA);
   1834 	}
   1835 
   1836 	/* ZZzzz... */
   1837 	tsleep(mp, PCATCH | PZERO, "mbpp_recv", 0);
   1838 
   1839 	/* stop receiving */
   1840 	if( cd ) {
   1841 		cd1400_write_reg(cd, CD1400_CAR, 0);
   1842 
   1843 		/* disable receiving */
   1844 		cd1400_write_reg(cd, CD1400_SRER, 0);
   1845 		cd1400_write_ccr(cd, CD1400_CCR_CMDCHANCTL | CD1400_CCR_RCVDIS);
   1846 	}
   1847 	splx(s);
   1848 
   1849 	/* return number of chars received */
   1850 	return(len - mp->mp_cnt);
   1851 }
   1852 
   1853 int
   1854 mbpp_hztoms(h)
   1855 	int h;
   1856 {
   1857 	int m = h;
   1858 
   1859 	if( m > 0 )
   1860 		m = m * 1000 / hz;
   1861 	return(m);
   1862 }
   1863 
   1864 int
   1865 mbpp_mstohz(m)
   1866 	int m;
   1867 {
   1868 	int h = m;
   1869 
   1870 	if( h > 0 ) {
   1871 		h = h * hz / 1000;
   1872 		if( h == 0 )
   1873 			h = 1000 / hz;
   1874 	}
   1875 	return(h);
   1876 }
   1877 
   1878 #endif /* NMAGMA */
   1879