Home | History | Annotate | Line # | Download | only in marvell
gtmpsc.c revision 1.7
      1 /*	$NetBSD: gtmpsc.c,v 1.7 2003/05/01 12:13:26 scw Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project by
     18  *      Allegro Networks, Inc., and Wasabi Systems, Inc.
     19  * 4. The name of Allegro Networks, Inc. may not be used to endorse
     20  *    or promote products derived from this software without specific prior
     21  *    written permission.
     22  * 5. The name of Wasabi Systems, Inc. may not be used to endorse
     23  *    or promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY ALLEGRO NETWORKS, INC. AND
     27  * WASABI SYSTEMS, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL EITHER ALLEGRO NETWORKS, INC. OR WASABI SYSTEMS, INC.
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * mpsc.c - MPSC serial driver, supports UART mode only
     42  *
     43  *
     44  * creation	Mon Apr  9 19:40:15 PDT 2001	cliff
     45  */
     46 
     47 #include "opt_kgdb.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/conf.h>
     51 #include <sys/device.h>
     52 #include <sys/proc.h>
     53 #include <sys/systm.h>
     54 #include <sys/tty.h>
     55 #include <sys/callout.h>
     56 #include <sys/fcntl.h>
     57 #ifdef KGDB
     58 #include <sys/kernel.h>
     59 #include <sys/kgdb.h>
     60 #endif
     61 
     62 #include <uvm/uvm_extern.h>
     63 
     64 #include <powerpc/atomic.h>
     65 #include <dev/cons.h>
     66 #include <machine/bus.h>
     67 #include <machine/cpu.h>		/* for DELAY */
     68 #include <machine/stdarg.h>
     69 #include "gtmpsc.h"
     70 
     71 
     72 #include <dev/marvell/gtreg.h>
     73 #include <dev/marvell/gtvar.h>
     74 #include <dev/marvell/gtintrreg.h>
     75 #include <dev/marvell/gtmpscreg.h>
     76 #include <dev/marvell/gtsdmareg.h>
     77 #include <dev/marvell/gtmpscvar.h>
     78 #include <dev/marvell/gtbrgreg.h>
     79 
     80 /*
     81  * XXX these delays were derived empiracaly
     82  */
     83 #define GTMPSC_POLL_DELAY	1	/* 1 usec */
     84 /*
     85  * Wait 2 characters time for RESET_DELAY
     86  */
     87 #define GTMPSC_RESET_DELAY	(2*8*1000000 / GT_MPSC_DEFAULT_BAUD_RATE)
     88 
     89 #define BURSTLEN 128
     90 
     91 /*
     92  * stat values for gtmpsc_common_pollc
     93  */
     94 #define GTMPSC_STAT_NONE	0
     95 #define GTMPSC_STAT_BREAK	1
     96 
     97 
     98 #define PRINTF(x)	gtmpsc_mem_printf x
     99 
    100 #if defined(DEBUG)
    101 unsigned int gtmpsc_debug = 0;
    102 # define STATIC
    103 # define DPRINTF(x)	do { if (gtmpsc_debug) gtmpsc_mem_printf x ; } while (0)
    104 #else
    105 # define STATIC static
    106 # define DPRINTF(x)
    107 #endif
    108 
    109 #define GTMPSCUNIT_MASK    0x7ffff
    110 #define GTMPSCDIALOUT_MASK 0x80000
    111 
    112 #define GTMPSCUNIT(x)      (minor(x) & GTMPSCUNIT_MASK)
    113 #define GTMPSCDIALOUT(x)   (minor(x) & GTMPSCDIALOUT_MASK)
    114 
    115 STATIC void gtmpscinit(struct gtmpsc_softc *);
    116 STATIC int  gtmpscmatch(struct device *, struct cfdata *, void *);
    117 STATIC void gtmpscattach(struct device *, struct device *, void *);
    118 STATIC int  compute_cdv(unsigned int);
    119 STATIC void gtmpsc_loadchannelregs(struct gtmpsc_softc *);
    120 STATIC void gtmpscshutdown(struct gtmpsc_softc *);
    121 STATIC void gtmpscstart(struct tty *);
    122 STATIC int  gtmpscparam(struct tty *, struct termios *);
    123 STATIC int  gtmpsc_probe(void);
    124 STATIC int  gtmpsc_intr(void *);
    125 STATIC void gtmpsc_softintr(void *);
    126 
    127 STATIC void gtmpsc_common_putn(struct gtmpsc_softc *);
    128 STATIC void gtmpsc_common_putc(unsigned int, unsigned char);
    129 STATIC int  gtmpsc_common_getc(unsigned int);
    130 STATIC int  gtmpsc_common_pollc(unsigned int, char *, int *);
    131 STATIC void gtmpsc_poll(void *);
    132 #ifdef KGDB
    133 STATIC void gtmpsc_kgdb_poll(void *);
    134 #endif
    135 STATIC void gtmpsc_mem_printf(const char *, ...);
    136 
    137 STATIC void gtmpsc_txdesc_init(gtmpsc_poll_sdma_t *, gtmpsc_poll_sdma_t *);
    138 STATIC void gtmpsc_rxdesc_init(gtmpsc_poll_sdma_t *, gtmpsc_poll_sdma_t *);
    139 STATIC unsigned int gtmpsc_get_causes(void);
    140 STATIC void gtmpsc_hackinit(struct gtmpsc_softc *, bus_space_tag_t,
    141 	bus_space_handle_t, int);
    142 STATIC void gtmpscinit_stop(struct gtmpsc_softc *, int);
    143 STATIC void gtmpscinit_start(struct gtmpsc_softc *, int);
    144 #if 0
    145 void gtmpsc_printf(const char *fmt, ...);
    146 #endif
    147 void gtmpsc_puts(char *);
    148 
    149 void gtmpsccnprobe(struct consdev *);
    150 void gtmpsccninit(struct consdev *);
    151 int  gtmpsccngetc(dev_t);
    152 void gtmpsccnputc(dev_t, int);
    153 void gtmpsccnpollc(dev_t, int);
    154 void gtmpsccnhalt(dev_t);
    155 
    156 STATIC void gtmpsc_txflush(gtmpsc_softc_t *);
    157 STATIC void gtmpsc_iflush(gtmpsc_softc_t *);
    158 STATIC void gtmpsc_shutdownhook(void *);
    159 
    160 dev_type_open(gtmpscopen);
    161 dev_type_close(gtmpscclose);
    162 dev_type_read(gtmpscread);
    163 dev_type_write(gtmpscwrite);
    164 dev_type_ioctl(gtmpscioctl);
    165 dev_type_stop(gtmpscstop);
    166 dev_type_tty(gtmpsctty);
    167 dev_type_poll(gtmpscpoll);
    168 
    169 const struct cdevsw gtmpsc_cdevsw = {
    170 	gtmpscopen, gtmpscclose, gtmpscread, gtmpscwrite, gtmpscioctl,
    171 	gtmpscstop, gtmpsctty, gtmpscpoll, nommap, ttykqfilter, D_TTY
    172 };
    173 
    174 CFATTACH_DECL(gtmpsc, sizeof(struct gtmpsc_softc),
    175     gtmpscmatch, gtmpscattach, NULL, NULL);
    176 
    177 extern struct cfdriver gtmpsc_cd;
    178 
    179 static struct consdev gtmpsc_consdev = {
    180 	0,
    181 	gtmpsccninit,
    182 	gtmpsccngetc,
    183 	gtmpsccnputc,
    184 	gtmpsccnpollc,
    185 	NULL,		/* cn_bell */
    186 	gtmpsccnhalt,
    187 	NULL,		/* cn_flush */
    188 	NODEV,
    189 	CN_NORMAL
    190 };
    191 
    192 STATIC void *gtmpsc_sdma_ih = NULL;
    193 
    194 gtmpsc_softc_t *gtmpsc_scp[GTMPSC_NCHAN] = { 0 };
    195 
    196 STATIC int gt_reva_gtmpsc_bug;
    197 unsigned int sdma_imask;        /* soft copy of SDMA IMASK reg */
    198 
    199 #ifdef KGDB
    200 #include <sys/kgdb.h>
    201 
    202 static int gtmpsc_kgdb_addr;
    203 static int gtmpsc_kgdb_attached;
    204 
    205 int kgdb_break_immediate /* = 0 */ ;
    206 
    207 STATIC int      gtmpsc_kgdb_getc(void *);
    208 STATIC void     gtmpsc_kgdb_putc(void *, int);
    209 #endif /* KGDB */
    210 
    211 /*
    212  * hacks for console initialization
    213  * which happens prior to autoconfig "attach"
    214  *
    215  * XXX Assumes PAGE_SIZE is a constant!
    216  */
    217 STATIC unsigned int gtmpsccninit_done = 0;
    218 STATIC gtmpsc_softc_t gtmpsc_fake_softc;
    219 STATIC unsigned char gtmpsc_earlybuf[PAGE_SIZE]
    220     __attribute__ ((aligned(PAGE_SIZE)));
    221 STATIC unsigned char gtmpsc_fake_dmapage[PAGE_SIZE]
    222     __attribute__ ((aligned(PAGE_SIZE)));
    223 
    224 
    225 #define GTMPSC_PRINT_BUF_SIZE	4096
    226 STATIC unsigned char gtmpsc_print_buf[GTMPSC_PRINT_BUF_SIZE] = { 0 };
    227 
    228 unsigned int gtmpsc_poll_putc_cnt = 0;
    229 unsigned int gtmpsc_poll_putn_cnt = 0;
    230 unsigned int gtmpsc_poll_getc_cnt = 0;
    231 unsigned int gtmpsc_poll_pollc_cnt = 0;
    232 unsigned int gtmpsc_poll_putc_miss = 0;
    233 unsigned int gtmpsc_poll_putn_miss = 0;
    234 unsigned int gtmpsc_poll_getc_miss = 0;
    235 unsigned int gtmpsc_poll_pollc_miss = 0;
    236 
    237 #ifndef SDMA_COHERENT
    238 /*
    239  * inlines to flush, invalidate cache
    240  * required if DMA cache coherency is broken
    241  * note that pointer `p' args are assumed to be cache aligned
    242  * and the size is assumed to be one CACHELINESIZE block
    243  */
    244 
    245 #define GTMPSC_CACHE_FLUSH(p)		gtmpsc_cache_flush(p)
    246 #define GTMPSC_CACHE_INVALIDATE(p)	gtmpsc_cache_invalidate(p)
    247 
    248 static volatile inline void
    249 gtmpsc_cache_flush(void *p)
    250 {
    251 	__asm __volatile ("eieio; dcbf 0,%0; lwz %0,0(%0); sync;"
    252 					: "+r"(p):);
    253 }
    254 
    255 static volatile inline void
    256 gtmpsc_cache_invalidate(void *p)
    257 {
    258 	__asm __volatile ("eieio; dcbi 0,%0; sync;" :: "r"(p));
    259 }
    260 #else
    261 
    262 #define GTMPSC_CACHE_FLUSH(p)
    263 #define GTMPSC_CACHE_INVALIDATE(p)
    264 
    265 #endif	/* SDMA_COHERENT */
    266 
    267 #define GT_READ(sc,o) \
    268 	bus_space_read_4((sc)->gtmpsc_memt, (sc)->gtmpsc_memh, (o))
    269 #define GT_WRITE(sc,o,v) \
    270 	bus_space_write_4((sc)->gtmpsc_memt, (sc)->gtmpsc_memh, (o), (v))
    271 
    272 
    273 #define SDMA_IMASK_ENABLE(sc, bit)  do { \
    274 	unsigned int    r; \
    275 	GT_WRITE(sc, SDMA_ICAUSE, ~(bit)); \
    276 	if (gt_reva_gtmpsc_bug) \
    277 		r = sdma_imask; \
    278 	else \
    279 		r = GT_READ(sc, SDMA_IMASK); \
    280 	r |= (bit); \
    281 	sdma_imask = r; \
    282 	GT_WRITE(sc, SDMA_IMASK, r); \
    283 } while (/*CONSTCOND*/ 0)
    284 
    285 #define SDMA_IMASK_DISABLE(sc, bit)  do { \
    286 	unsigned int    r; \
    287 	if (gt_reva_gtmpsc_bug) \
    288 		r = sdma_imask; \
    289 	else \
    290 		r = GT_READ(sc, SDMA_IMASK); \
    291 	r &= ~(bit); \
    292 	sdma_imask = r; \
    293 	GT_WRITE(sc, SDMA_IMASK, r); \
    294 } while (/*CONSTCOND*/ 0)
    295 
    296 static volatile inline unsigned int
    297 desc_read(unsigned int *ip)
    298 {
    299 	unsigned int rv;
    300 
    301 	__asm __volatile ("lwzx %0,0,%1; eieio;"
    302 		: "=r"(rv) : "r"(ip));
    303 	return rv;
    304 }
    305 
    306 static volatile inline void
    307 desc_write(unsigned int *ip, unsigned int val)
    308 {
    309 	__asm __volatile ("stwx %0,0,%1; eieio;"
    310 		:: "r"(val), "r"(ip));
    311 }
    312 
    313 
    314 /*
    315  * gtmpsc_txdesc_init - set up TX descriptor ring
    316  */
    317 STATIC void
    318 gtmpsc_txdesc_init(gtmpsc_poll_sdma_t *vmps, gtmpsc_poll_sdma_t *pmps)
    319 {
    320 	int n;
    321 	sdma_desc_t *dp;
    322 	gtmpsc_polltx_t *vtxp;
    323 	gtmpsc_polltx_t *ptxp;
    324 	gtmpsc_polltx_t *next_ptxp;
    325 	gtmpsc_polltx_t *first_ptxp;
    326 
    327 	first_ptxp = ptxp = &pmps->tx[0];
    328 	vtxp = &vmps->tx[0];
    329 	next_ptxp = ptxp + 1;
    330 	for (n = (GTMPSC_NTXDESC - 1); n--; ) {
    331 		dp = &vtxp->txdesc;
    332 		desc_write(&dp->sdma_csr, 0);
    333 		desc_write(&dp->sdma_cnt, 0);
    334 		desc_write(&dp->sdma_bufp, (u_int32_t)&ptxp->txbuf);
    335 		desc_write(&dp->sdma_next, (u_int32_t)&next_ptxp->txdesc);
    336 		GTMPSC_CACHE_FLUSH(dp);
    337 		vtxp++;
    338 		ptxp++;
    339 		next_ptxp++;
    340 	}
    341 	dp = &vtxp->txdesc;
    342 	desc_write(&dp->sdma_csr, 0);
    343 	desc_write(&dp->sdma_cnt, 0);
    344 	desc_write(&dp->sdma_bufp, (u_int32_t)&ptxp->txbuf);
    345 	desc_write(&dp->sdma_next, (u_int32_t)&first_ptxp->txdesc);
    346 	GTMPSC_CACHE_FLUSH(dp);
    347 }
    348 
    349 /*
    350  * gtmpsc_rxdesc_init - set up RX descriptor ring
    351  */
    352 STATIC void
    353 gtmpsc_rxdesc_init(gtmpsc_poll_sdma_t *vmps, gtmpsc_poll_sdma_t *pmps)
    354 {
    355 	int n;
    356 	sdma_desc_t *dp;
    357 	gtmpsc_pollrx_t *vrxp;
    358 	gtmpsc_pollrx_t *prxp;
    359 	gtmpsc_pollrx_t *next_prxp;
    360 	gtmpsc_pollrx_t *first_prxp;
    361 	unsigned int csr;
    362 
    363 	csr = SDMA_CSR_RX_L|SDMA_CSR_RX_F|SDMA_CSR_RX_OWN|SDMA_CSR_RX_EI;
    364 	first_prxp = prxp = &pmps->rx[0];
    365 	vrxp = &vmps->rx[0];
    366 	next_prxp = prxp + 1;
    367 	for (n = (GTMPSC_NRXDESC - 1); n--; ) {
    368 		dp = &vrxp->rxdesc;
    369 		desc_write(&dp->sdma_csr, csr);
    370 		desc_write(&dp->sdma_cnt,
    371 			GTMPSC_RXBUFSZ << SDMA_RX_CNT_BUFSZ_SHIFT);
    372 		desc_write(&dp->sdma_bufp, (u_int32_t)&prxp->rxbuf);
    373 		desc_write(&dp->sdma_next, (u_int32_t)&next_prxp->rxdesc);
    374 		GTMPSC_CACHE_FLUSH(dp);
    375 		vrxp++;
    376 		prxp++;
    377 		next_prxp++;
    378 	}
    379 	dp = &vrxp->rxdesc;
    380 	desc_write(&dp->sdma_csr, SDMA_CSR_RX_OWN);
    381 	desc_write(&dp->sdma_cnt,
    382 		GTMPSC_RXBUFSZ << SDMA_RX_CNT_BUFSZ_SHIFT);
    383 	desc_write(&dp->sdma_bufp, (u_int32_t)&prxp->rxbuf);
    384 	desc_write(&dp->sdma_next, (u_int32_t)&first_prxp->rxdesc);
    385 	GTMPSC_CACHE_FLUSH(dp);
    386 }
    387 
    388 /*
    389  * Compute the BRG countdown value (CDV in BRG_BCR)
    390  */
    391 
    392 STATIC int
    393 compute_cdv(unsigned int baud)
    394 {
    395 	unsigned int    cdv;
    396 
    397 	if (baud == 0)
    398 		return 0;
    399 	cdv = (GT_MPSC_FREQUENCY / (baud * GTMPSC_CLOCK_DIVIDER) + 1) / 2 - 1;
    400 	if (cdv > BRG_BCR_CDV_MAX)
    401 		return -1;
    402 	return cdv;
    403 }
    404 
    405 STATIC void
    406 gtmpsc_loadchannelregs(struct gtmpsc_softc *sc)
    407 {
    408 	u_int   brg_bcr;
    409 	u_int   unit;
    410 
    411 	unit = sc->gtmpsc_unit;
    412 	brg_bcr = unit ? BRG_BCR1 : BRG_BCR0;
    413 
    414 	GT_WRITE(sc, brg_bcr, sc->gtmpsc_brg_bcr);
    415 	GT_WRITE(sc, GTMPSC_U_CHRN(unit, 3), sc->gtmpsc_chr3);
    416 }
    417 
    418 STATIC int
    419 gtmpscmatch(struct device *parent, struct cfdata *self, void *aux)
    420 {
    421 	struct gt_softc *gt = (struct gt_softc *) parent;
    422 	struct gt_attach_args *ga = aux;
    423 
    424 	return GT_MPSCOK(gt, ga, &gtmpsc_cd);
    425 }
    426 
    427 STATIC void
    428 gtmpscattach(struct device *parent, struct device *self, void *aux)
    429 {
    430 	struct gt_attach_args *ga = aux;
    431 	struct gt_softc *gt = (struct gt_softc *) parent;
    432 	struct gtmpsc_softc *sc = (struct gtmpsc_softc *) self;
    433 	gtmpsc_poll_sdma_t *vmps;
    434 	gtmpsc_poll_sdma_t *pmps;
    435 	struct tty *tp;
    436 	caddr_t kva;
    437 	int rsegs;
    438 	int err;
    439 	int s;
    440 	int is_console = 0;
    441 
    442 	DPRINTF(("mpscattach\n"));
    443 
    444 	GT_MPSCFOUND(gt, ga);
    445 
    446 	s = splhigh();
    447 
    448 	sc->gtmpsc_memt = ga->ga_memt;
    449 	sc->gtmpsc_memh = ga->ga_memh;
    450 	sc->gtmpsc_dmat = ga->ga_dmat;
    451 	sc->gtmpsc_unit = ga->ga_unit;
    452 
    453 	aprint_normal(": SDMA");
    454 	err = bus_dmamem_alloc(sc->gtmpsc_dmat, PAGE_SIZE, PAGE_SIZE, PAGE_SIZE,
    455 	    sc->gtmpsc_dma_segs, 1, &rsegs, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW);
    456 	if (err) {
    457 		PRINTF(("mpscattach: bus_dmamem_alloc error 0x%x\n", err));
    458 		splx(s);
    459 		return;
    460 	}
    461 #ifndef SDMA_COHERENT
    462 	err = bus_dmamem_map(sc->gtmpsc_dmat, sc->gtmpsc_dma_segs, 1, PAGE_SIZE,
    463 	    &kva, BUS_DMA_NOWAIT);
    464 #else
    465 	err = bus_dmamem_map(sc->gtmpsc_dmat, sc->gtmpsc_dma_segs, 1, PAGE_SIZE,
    466 	    &kva, BUS_DMA_NOWAIT|BUS_DMA_NOCACHE);
    467 #endif
    468 	if (err) {
    469 		PRINTF(("mpscattach: bus_dmamem_map error 0x%x\n", err));
    470 		splx(s);
    471 		return;
    472 	}
    473 
    474 	err = bus_dmamap_create(sc->gtmpsc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
    475 	    PAGE_SIZE, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &sc->gtmpsc_dma_map);
    476 	if (err) {
    477 		PRINTF(("mpscattach: bus_dmamap_create error 0x%x\n", err));
    478 		splx(s);
    479 		return;
    480 	}
    481 
    482 	err = bus_dmamap_load(sc->gtmpsc_dmat, sc->gtmpsc_dma_map, kva,
    483 	    PAGE_SIZE, NULL, 0);
    484 	if (err) {
    485 		PRINTF(("mpscattach: bus_dmamap_load error 0x%x\n", err));
    486 		splx(s);
    487 		return;
    488 	}
    489 	memset(kva, 0, PAGE_SIZE);	/* paranoid/superfluous */
    490 
    491 	vmps = (gtmpsc_poll_sdma_t *)kva;				    /* KVA */
    492 	pmps = (gtmpsc_poll_sdma_t *)sc->gtmpsc_dma_map->dm_segs[0].ds_addr;    /* PA */
    493 #if defined(DEBUG)
    494 	printf(" at %p/%p", vmps, pmps);
    495 #endif
    496 	gtmpsc_txdesc_init(vmps, pmps);
    497 	gtmpsc_rxdesc_init(vmps, pmps);
    498 	sc->gtmpsc_poll_sdmapage = vmps;
    499 
    500 	if (gtmpsc_scp[sc->gtmpsc_unit] != NULL)
    501 		gtmpsc_txflush(gtmpsc_scp[sc->gtmpsc_unit]);
    502 
    503 	sc->gtmpsc_tty = tp = ttymalloc();
    504 	tp->t_oproc = gtmpscstart;
    505 	tp->t_param = gtmpscparam;
    506 	tty_attach(tp);
    507 
    508 	if (gtmpsc_sdma_ih == NULL) {
    509 		gtmpsc_sdma_ih = intr_establish(IRQ_SDMA, IST_LEVEL, IPL_SERIAL,
    510 			gtmpsc_intr, &sc);
    511 		if (gtmpsc_sdma_ih == NULL)
    512 			panic("mpscattach: cannot intr_establish IRQ_SDMA");
    513 	}
    514 
    515 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, gtmpsc_softintr, sc);
    516 	if (sc->sc_si == NULL)
    517 		panic("mpscattach: cannot softintr_establish IPL_SOFTSERIAL");
    518 
    519 	shutdownhook_establish(gtmpsc_shutdownhook, sc);
    520 
    521 	gtmpsc_scp[sc->gtmpsc_unit] = sc;
    522 	gtmpscinit(sc);
    523 
    524 	if (cn_tab == &gtmpsc_consdev &&
    525 	    cn_tab->cn_dev == makedev(0, sc->gtmpsc_unit)) {
    526 		cn_tab->cn_dev = makedev(cdevsw_lookup_major(&gtmpsc_cdevsw),
    527 		    sc->gtmpsc_dev.dv_unit);
    528 		is_console = 1;
    529 	}
    530 
    531 	aprint_normal(" irq %s%s\n",
    532 	    intr_string(IRQ_SDMA),
    533 	    (gt_reva_gtmpsc_bug) ? " [Rev A. bug]" : "");
    534 
    535 	if (is_console)
    536 		aprint_normal("%s: console\n", sc->gtmpsc_dev.dv_xname);
    537 
    538 #ifdef DDB
    539 	if (is_console == 0)
    540 		SDMA_IMASK_ENABLE(sc, SDMA_INTR_RXBUF(sc->gtmpsc_unit));
    541 #endif	/* DDB */
    542 
    543 	splx(s);
    544 #ifdef KGDB
    545 	/*
    546 	 * Allow kgdb to "take over" this port.  If this is
    547 	 * the kgdb device, it has exclusive use.
    548 	 */
    549 	if (sc->gtmpsc_unit == comkgdbport) {
    550 		if (comkgdbport == 0) { /* FIXME */
    551 			printf("%s(kgdb): cannot share with console\n",
    552 				sc->gtmpsc_dev.dv_xname);
    553 			return;
    554 		}
    555 
    556 		sc->gtmpsc_flags |= GTMPSCF_KGDB;
    557 		printf("%s: kgdb\n", sc->gtmpsc_dev.dv_xname);
    558 		gtmpsc_txflush(gtmpsc_scp[0]);
    559 		kgdb_attach(gtmpsc_kgdb_getc, gtmpsc_kgdb_putc, NULL);
    560 		kgdb_dev = 123; /* unneeded, only to satisfy some tests */
    561 		gtmpsc_kgdb_attached = 1;
    562 		SDMA_IMASK_ENABLE(sc, SDMA_INTR_RXBUF(sc->gtmpsc_unit));
    563 		kgdb_connect(1);
    564 	}
    565 #endif /* KGDB */
    566 }
    567 
    568 STATIC void
    569 gtmpscshutdown(struct gtmpsc_softc *sc)
    570 {
    571 	struct tty *tp;
    572 	int     s;
    573 
    574 #ifdef KGDB
    575 	if (sc->gtmpsc_flags & GTMPSCF_KGDB != 0)
    576 		return;
    577 #endif
    578 	tp = sc->gtmpsc_tty;
    579 	s = splserial();
    580 	/* Fake carrier off */
    581 	(void) (*tp->t_linesw->l_modem)(tp, 0);
    582 	SDMA_IMASK_DISABLE(sc, SDMA_INTR_RXBUF(sc->gtmpsc_unit));
    583 	splx(s);
    584 }
    585 
    586 int
    587 gtmpscopen(dev_t dev, int flag, int mode, struct proc *p)
    588 {
    589 	struct gtmpsc_softc *sc;
    590 	int unit = GTMPSCUNIT(dev);
    591 	struct tty *tp;
    592 	int s;
    593 	int s2;
    594 	int error;
    595 
    596 	if (unit >= gtmpsc_cd.cd_ndevs)
    597 		return ENXIO;
    598 	sc = gtmpsc_cd.cd_devs[unit];
    599 	if (!sc)
    600 		return ENXIO;
    601 #ifdef KGDB
    602 	/*
    603 	 * If this is the kgdb port, no other use is permitted.
    604 	 */
    605 	if (sc->gtmpsc_flags & GTMPSCF_KGDB != 0)
    606 		return (EBUSY);
    607 #endif
    608 	tp = sc->gtmpsc_tty;
    609 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    610 	    ISSET(tp->t_state, TS_XCLUDE) &&
    611 	    p->p_ucred->cr_uid != 0)
    612 		return (EBUSY);
    613 
    614 	s = spltty();
    615 
    616 	if (!(tp->t_state & TS_ISOPEN)) {
    617 		struct termios t;
    618 
    619 		tp->t_dev = dev;
    620 		s2 = splserial();
    621 		SDMA_IMASK_ENABLE(sc, SDMA_INTR_RXBUF(unit));
    622 		splx(s2);
    623 		t.c_ispeed = 0;
    624 #if 0
    625 		t.c_ospeed = TTYDEF_SPEED;
    626 #else
    627 		t.c_ospeed = GT_MPSC_DEFAULT_BAUD_RATE;
    628 #endif
    629 		t.c_cflag = TTYDEF_CFLAG;
    630 		/* Make sure gtmpscparam() will do something. */
    631 		tp->t_ospeed = 0;
    632 		(void) gtmpscparam(tp, &t);
    633 		tp->t_iflag = TTYDEF_IFLAG;
    634 		tp->t_oflag = TTYDEF_OFLAG;
    635 		tp->t_lflag = TTYDEF_LFLAG;
    636 		ttychars(tp);
    637 		ttsetwater(tp);
    638 		s2 = splserial();
    639 		/* Clear the input ring */
    640 		sc->gtmpsc_rxfifo_putix = 0;
    641 		sc->gtmpsc_rxfifo_getix = 0;
    642 		sc->gtmpsc_rxfifo_navail = GTMPSC_RXFIFOSZ;
    643 		gtmpsc_iflush(sc);
    644 		splx(s2);
    645 	}
    646 	splx(s);
    647 	error = ttyopen(tp, GTMPSCDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    648 	if (error)
    649 		goto bad;
    650 
    651 	error = (*tp->t_linesw->l_open)(dev, tp);
    652 	if (error)
    653 		goto bad;
    654 
    655 	return (0);
    656 
    657 bad:
    658 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    659 		/*
    660 		 * We failed to open the device, and nobody else had it opened.
    661 		 * Clean up the state as appropriate.
    662 		 */
    663 		gtmpscshutdown(sc);
    664 	}
    665 
    666 	return (error);
    667 }
    668 
    669 int
    670 gtmpscclose(dev_t dev, int flag, int mode, struct proc *p)
    671 {
    672 	int unit = GTMPSCUNIT(dev);
    673 	struct gtmpsc_softc *sc = gtmpsc_cd.cd_devs[unit];
    674 	struct tty *tp = sc->gtmpsc_tty;
    675 	int s;
    676 
    677 	s = splserial();
    678 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    679 		splx(s);
    680 		return (0);
    681 	}
    682 
    683 	(*tp->t_linesw->l_close)(tp, flag);
    684 	ttyclose(tp);
    685 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    686 		/*
    687 		 * Although we got a last close, the device may still be in
    688 		 * use; e.g. if this was the dialout node, and there are still
    689 		 * processes waiting for carrier on the non-dialout node.
    690 		 */
    691 		gtmpscshutdown(sc);
    692 	}
    693 
    694 	splx(s);
    695 	return (0);
    696 }
    697 
    698 int
    699 gtmpscread(dev_t dev, struct uio *uio, int flag)
    700 {
    701 	struct gtmpsc_softc *sc = gtmpsc_cd.cd_devs[GTMPSCUNIT(dev)];
    702 	struct tty *tp = sc->gtmpsc_tty;
    703 
    704 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    705 }
    706 
    707 int
    708 gtmpscwrite(dev_t dev, struct uio *uio, int flag)
    709 {
    710 	struct gtmpsc_softc *sc = gtmpsc_cd.cd_devs[GTMPSCUNIT(dev)];
    711 	struct tty *tp = sc->gtmpsc_tty;
    712 
    713 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    714 }
    715 
    716 int
    717 gtmpscpoll(dev_t dev, int events, struct proc *p)
    718 {
    719 	struct gtmpsc_softc *sc = gtmpsc_cd.cd_devs[GTMPSCUNIT(dev)];
    720 	struct tty *tp = sc->gtmpsc_tty;
    721 
    722 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    723 }
    724 
    725 int
    726 gtmpscioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    727 {
    728 	struct gtmpsc_softc *sc = gtmpsc_cd.cd_devs[GTMPSCUNIT(dev)];
    729 	struct tty *tp = sc->gtmpsc_tty;
    730 	int error;
    731 
    732 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) >= 0)
    733 		return error;
    734 	if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
    735 		return error;
    736 	return ENOTTY;
    737 }
    738 
    739 struct tty *
    740 gtmpsctty(dev_t dev)
    741 {
    742 	struct gtmpsc_softc *sc = gtmpsc_cd.cd_devs[GTMPSCUNIT(dev)];
    743 
    744 	return sc->gtmpsc_tty;
    745 }
    746 
    747 void
    748 gtmpscstop(struct tty *tp, int flag)
    749 {
    750 }
    751 
    752 STATIC void
    753 gtmpscstart(struct tty *tp)
    754 {
    755 	struct gtmpsc_softc *sc;
    756 	unsigned char *tba;
    757 	unsigned int unit;
    758 	int s, s2, tbc;
    759 
    760 	unit = GTMPSCUNIT(tp->t_dev);
    761 	sc = gtmpsc_cd.cd_devs[unit];
    762 	if (sc == NULL)
    763 		return;
    764 
    765 	s = spltty();
    766 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
    767 		goto out;
    768 	if (sc->sc_tx_stopped)
    769 		goto out;
    770 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    771 		if ((tp->t_state & TS_ASLEEP) != 0) {
    772 			tp->t_state &= ~TS_ASLEEP;
    773 			wakeup(&tp->t_outq);
    774 		}
    775 		selwakeup(&tp->t_wsel);
    776 		if (tp->t_outq.c_cc == 0)
    777 			goto out;
    778 	}
    779 
    780 	/* Grab the first contiguous region of buffer space. */
    781 	tba = tp->t_outq.c_cf;
    782 	tbc = ndqb(&tp->t_outq, 0);
    783 
    784 	s2 = splserial();
    785 
    786 	sc->sc_tba = tba;
    787 	sc->sc_tbc = tbc;
    788 	sc->cnt_tx_from_ldisc += tbc;
    789 	SDMA_IMASK_ENABLE(sc, SDMA_INTR_TXBUF(unit));
    790 	tp->t_state |= TS_BUSY;
    791 	sc->sc_tx_busy = 1;
    792 	gtmpsc_common_putn(sc);
    793 
    794 	splx(s2);
    795 out:
    796 	splx(s);
    797 }
    798 
    799 STATIC int
    800 gtmpscparam(struct tty *tp, struct termios *t)
    801 {
    802 	struct gtmpsc_softc *sc = gtmpsc_cd.cd_devs[GTMPSCUNIT(tp->t_dev)];
    803 	int ospeed = compute_cdv(t->c_ospeed);
    804 	int s;
    805 
    806 	/* Check requested parameters. */
    807 	if (ospeed < 0)
    808 		return (EINVAL);
    809 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    810 		return (EINVAL);
    811 
    812 	/*
    813 	 * If there were no changes, don't do anything.  This avoids dropping
    814 	 * input and improves performance when all we did was frob things like
    815 	 * VMIN and VTIME.
    816 	 */
    817 	if (tp->t_ospeed == t->c_ospeed &&
    818 	    tp->t_cflag == t->c_cflag)
    819 		return (0);
    820 
    821 	s = splserial();
    822 
    823 	sc->gtmpsc_brg_bcr = BRG_BCR_EN | GT_MPSC_CLOCK_SOURCE | ospeed;
    824 	sc->gtmpsc_chr3 = GTMPSC_MAXIDLE(t->c_ospeed);
    825 
    826 	/* And copy to tty. */
    827 	tp->t_ispeed = 0;
    828 	tp->t_ospeed = t->c_ospeed;
    829 	tp->t_cflag = t->c_cflag;
    830 
    831 	if (!sc->sc_heldchange) {
    832 		if (sc->sc_tx_busy) {
    833 			sc->sc_heldtbc = sc->sc_tbc;
    834 			sc->sc_tbc = 0;
    835 			sc->sc_heldchange = 1;
    836 		} else
    837 			gtmpsc_loadchannelregs(sc);
    838 	}
    839 
    840 	splx(s);
    841 
    842 	/* Fake carrier on */
    843 	(void) (*tp->t_linesw->l_modem)(tp, 1);
    844 
    845 	return 0;
    846 }
    847 
    848 STATIC int
    849 gtmpsc_probe(void)
    850 {
    851 	return 1;		/* XXX */
    852 }
    853 
    854 STATIC unsigned int
    855 gtmpsc_get_causes(void)
    856 {
    857 	int                     i;
    858 	struct gtmpsc_softc       *sc;
    859 	unsigned int            cause = 0;
    860 	static unsigned int     bits[4] = {
    861 				SDMA_INTR_RXBUF(0),
    862 				SDMA_INTR_TXBUF(0),
    863 				SDMA_INTR_RXBUF(1),
    864 				SDMA_INTR_TXBUF(1),
    865 	};
    866 	sdma_desc_t             *desc_addr[4];
    867 	static unsigned int     fake_once = SDMA_INTR_RXBUF(0)
    868 						| SDMA_INTR_RXBUF(1);
    869 
    870 	desc_addr[0] = 0;
    871 	desc_addr[1] = 0;
    872 	desc_addr[2] = 0;
    873 	desc_addr[3] = 0;
    874 	sc = gtmpsc_cd.cd_devs[0];
    875 	if (sc != 0) {
    876 	    if (sdma_imask & SDMA_INTR_RXBUF(0)) {
    877 		desc_addr[0] =
    878 		    &sc->gtmpsc_poll_sdmapage->rx[sc->gtmpsc_poll_rxix].rxdesc;
    879 		    GTMPSC_CACHE_INVALIDATE(desc_addr[0]);
    880 		    __asm __volatile ("dcbt 0,%0" :: "r"(desc_addr[0]));
    881 	    }
    882 	    if (sdma_imask & SDMA_INTR_TXBUF(0)) {
    883 		desc_addr[1] =
    884 		    &sc->gtmpsc_poll_sdmapage->tx[sc->gtmpsc_poll_txix].txdesc;
    885 		    GTMPSC_CACHE_INVALIDATE(desc_addr[1]);
    886 		    __asm __volatile ("dcbt 0,%0" :: "r"(desc_addr[1]));
    887 	    }
    888 	}
    889 	sc = gtmpsc_cd.cd_devs[1];
    890 	if (sc != 0) {
    891 	    if (sdma_imask & SDMA_INTR_RXBUF(1)) {
    892 		desc_addr[2] =
    893 		    &sc->gtmpsc_poll_sdmapage->rx[sc->gtmpsc_poll_rxix].rxdesc;
    894 		    GTMPSC_CACHE_INVALIDATE(desc_addr[2]);
    895 		    __asm __volatile ("dcbt 0,%0" :: "r"(desc_addr[2]));
    896 	    }
    897 	    if (sdma_imask & SDMA_INTR_TXBUF(1)) {
    898 		desc_addr[3] =
    899 		    &sc->gtmpsc_poll_sdmapage->tx[sc->gtmpsc_poll_txix].txdesc;
    900 		    GTMPSC_CACHE_INVALIDATE(desc_addr[3]);
    901 		    __asm __volatile ("dcbt 0,%0" :: "r"(desc_addr[3]));
    902 	    }
    903 	}
    904 
    905 	for (i = 0; i < 4; ++i)
    906 		if ((sdma_imask & bits[i]) && desc_addr[i] != 0 &&
    907 			    (desc_addr[i]->sdma_csr & SDMA_CSR_TX_OWN) == 0)
    908 			cause |= bits[i];
    909 	if (fake_once & sdma_imask) {
    910 		cause |= fake_once & sdma_imask;
    911 	/*	fake_once &= ~(cause & fake_once); */
    912 	}
    913 	return cause;
    914 }
    915 
    916 STATIC int
    917 gtmpsc_intr(void *arg)
    918 {
    919 	struct gtmpsc_softc       *sc;
    920 	unsigned int            unit;
    921 	int                     spurious = 1;
    922 	unsigned int            r;
    923 	unsigned int            cause=0;
    924 
    925 	if (gt_reva_gtmpsc_bug)
    926 		cause = gtmpsc_get_causes();
    927 
    928 #ifdef KGDB
    929 	if (kgdb_break_immediate) {
    930 		unit = comkgdbport;
    931 		sc = gtmpsc_cd.cd_devs[unit];
    932 		if (sc == 0 || (sc->gtmpsc_flags & GTMPSCF_KGDB) == 0)
    933 			goto skip_kgdb;
    934 		if (gt_reva_gtmpsc_bug)
    935 			r = cause & sdma_imask;
    936 		else {
    937 			r = GT_READ(sc, SDMA_ICAUSE);
    938 			r &= GT_READ(sc, SDMA_IMASK);
    939 		}
    940 		r &= SDMA_INTR_RXBUF(unit);
    941 		if (r == 0)
    942 			goto skip_kgdb;
    943 		GT_WRITE(sc, SDMA_ICAUSE, ~r);
    944 		spurious = 0;
    945 		gtmpsc_kgdb_poll(sc);
    946 	}
    947 skip_kgdb:
    948 #endif
    949 	for (unit = 0; unit < GTMPSC_NCHAN; ++unit) {
    950 		sc = gtmpsc_cd.cd_devs[unit];
    951 		if (sc == 0)
    952 			continue;
    953 		if (gt_reva_gtmpsc_bug)
    954 			r = cause & sdma_imask;
    955 		else {
    956 			r = GT_READ(sc, SDMA_ICAUSE);
    957 			r &= GT_READ(sc, SDMA_IMASK);
    958 		}
    959 		r &= SDMA_U_INTR_MASK(unit);
    960 		if (r == 0)
    961 			continue;
    962 		GT_WRITE(sc, SDMA_ICAUSE, ~r);
    963 		spurious = 0;
    964 		if (r & SDMA_INTR_RXBUF(unit)) {
    965 #ifdef KGDB
    966 			if (sc->gtmpsc_flags & GTMPSCF_KGDB)
    967 				gtmpsc_kgdb_poll(sc);
    968 			else
    969 #endif
    970 				gtmpsc_poll(sc);
    971 		}
    972 		if (r & SDMA_INTR_TXBUF(unit)) {
    973 			/*
    974 			 * If we've delayed a parameter change, do it now,
    975 			 * and restart output.
    976 			 */
    977 			if (sc->sc_heldchange) {
    978 				gtmpsc_loadchannelregs(sc);
    979 				sc->sc_heldchange = 0;
    980 				sc->sc_tbc = sc->sc_heldtbc;
    981 				sc->sc_heldtbc = 0;
    982 			}
    983 
    984 			/* Output the next chunk of the contiguous buffer,
    985 								if any. */
    986 			if (sc->sc_tbc > 0)
    987 				gtmpsc_common_putn(sc);
    988 			if (sc->sc_tbc == 0 && sc->sc_tx_busy) {
    989 				sc->sc_tx_busy = 0;
    990 				sc->sc_tx_done = 1;
    991 				softintr_schedule(sc->sc_si);
    992 				SDMA_IMASK_DISABLE(sc, SDMA_INTR_TXBUF(unit));
    993 			}
    994 		}
    995 	}
    996 	return 1;
    997 	/* return !spurious; */
    998 }
    999 
   1000 STATIC void
   1001 gtmpsc_softintr(void *arg)
   1002 {
   1003 	struct gtmpsc_softc	*sc = arg;
   1004 	struct tty              *tp;
   1005 	int                     (*rint)(int, struct tty *);
   1006 	int                     jobs;
   1007 	int                     s;
   1008 
   1009 	tp = sc->gtmpsc_tty;
   1010 	rint = tp->t_linesw->l_rint;
   1011 	do {
   1012 		jobs = 0;
   1013 		if (sc->gtmpsc_rxfifo_navail < GTMPSC_RXFIFOSZ) {
   1014 			s = spltty();
   1015 			rint(sc->gtmpsc_rxfifo[sc->gtmpsc_rxfifo_getix++],
   1016 								tp);
   1017 			if (sc->gtmpsc_rxfifo_getix >= GTMPSC_RXFIFOSZ)
   1018 				sc->gtmpsc_rxfifo_getix = 0;
   1019 			++sc->cnt_rx_from_fifo;
   1020 			/* atomic_add() returns the previous value */
   1021 			jobs += atomic_add(&sc->gtmpsc_rxfifo_navail, 1) + 1
   1022 								< GTMPSC_RXFIFOSZ;
   1023 			splx(s);
   1024 		}
   1025 		if (sc->sc_tx_done) {
   1026 			++jobs;
   1027 			sc->sc_tx_done = 0;
   1028 			s = spltty();
   1029 			tp->t_state &= ~TS_BUSY;
   1030 			if ((tp->t_state & TS_FLUSH) != 0)
   1031 			    tp->t_state &= ~TS_FLUSH;
   1032 			else
   1033 			    ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
   1034 			(*tp->t_linesw->l_start)(tp);
   1035 			splx(s);
   1036 		}
   1037 	} while (jobs);
   1038 }
   1039 
   1040 /*
   1041  * Console support functions
   1042  */
   1043 void
   1044 gtmpsccnprobe(struct consdev *cd)
   1045 {
   1046 	int maj;
   1047 /* {extern void return_to_dink(int); return_to_dink(gtbase);} */
   1048 
   1049 	if (!gtmpsc_probe())
   1050 		return;
   1051 
   1052 	maj = cdevsw_lookup_major(&gtmpsc_cdevsw);
   1053 	cd->cn_dev = makedev(maj, 0);
   1054 	cd->cn_pri = CN_INTERNAL;
   1055 }
   1056 
   1057 /*
   1058  * gtmpsc_hackinit - hacks required to supprt GTMPSC console
   1059  */
   1060 STATIC void
   1061 gtmpsc_hackinit(struct gtmpsc_softc *sc, bus_space_tag_t memt,
   1062 	bus_space_handle_t memh, int unit)
   1063 {
   1064 	gtmpsc_poll_sdma_t *vmps;
   1065 	gtmpsc_poll_sdma_t *pmps;
   1066 
   1067 	DPRINTF(("hackinit\n"));
   1068 
   1069 	bzero(sc, sizeof(struct gtmpsc_softc));
   1070 	sc->gtmpsc_memt = memt;
   1071 	sc->gtmpsc_memh = memh;
   1072 	sc->gtmpsc_unit = unit;
   1073 	gtmpsc_scp[sc->gtmpsc_unit] = sc;
   1074 
   1075 	vmps = (gtmpsc_poll_sdma_t *)gtmpsc_fake_dmapage;	/* KVA */
   1076 	pmps = (gtmpsc_poll_sdma_t *)gtmpsc_fake_dmapage;	/* PA */
   1077 
   1078 	gtmpsc_txdesc_init(vmps, pmps);
   1079 	gtmpsc_rxdesc_init(vmps, pmps);
   1080 
   1081 	sc->gtmpsc_poll_sdmapage = vmps;
   1082 }
   1083 
   1084 /*
   1085  * gtmpsc_txflush - wait for output to drain
   1086  */
   1087 STATIC void
   1088 gtmpsc_txflush(gtmpsc_softc_t *sc)
   1089 {
   1090 	unsigned int csr;
   1091 	unsigned int *csrp;
   1092 	gtmpsc_polltx_t *vtxp;
   1093 	int limit = 4000000;	/* 4 seconds */
   1094 	int ix;
   1095 
   1096 	ix = sc->gtmpsc_poll_txix - 1;
   1097 	if (ix < 0)
   1098 		ix = GTMPSC_NTXDESC - 1;
   1099 
   1100 	vtxp = &sc->gtmpsc_poll_sdmapage->tx[ix];
   1101 	csrp = &vtxp->txdesc.sdma_csr;
   1102 	while (limit > 0) {
   1103 		GTMPSC_CACHE_INVALIDATE(csrp);
   1104 		csr = desc_read(csrp);
   1105 		if ((csr & SDMA_CSR_TX_OWN) == 0)
   1106 			break;
   1107 		DELAY(GTMPSC_POLL_DELAY);
   1108 		limit -= GTMPSC_POLL_DELAY;
   1109 	}
   1110 }
   1111 
   1112 STATIC void
   1113 gtmpsc_iflush(gtmpsc_softc_t *sc)
   1114 {
   1115 	int     timo;
   1116 	char    c;
   1117 	int     stat;
   1118 
   1119 	for (timo = 50000; timo; timo--)
   1120 		if (gtmpsc_common_pollc(sc->gtmpsc_unit, &c, &stat) == 0)
   1121 			return;
   1122 #ifdef DIAGNOSTIC
   1123 	printf("%s: gtmpsc_iflush timeout %02x\n", sc->gtmpsc_dev.dv_xname, c);
   1124 #endif
   1125 }
   1126 
   1127 STATIC void
   1128 gtmpscinit_stop(struct gtmpsc_softc *sc, int once)
   1129 {
   1130 	unsigned int r;
   1131 	unsigned int unit = sc->gtmpsc_unit;
   1132 
   1133 	/*
   1134 	 * XXX HACK FIXME
   1135 	 * PMON output has not been flushed.  give him a chance
   1136 	 */
   1137 #if 1
   1138 	if (! once)
   1139 		DELAY(100000);	/* XXX */
   1140 #endif
   1141 
   1142 	DPRINTF(("gtmpscinit_stop: unit 0x%x\n", unit));
   1143 	if (unit >= GTMPSC_NCHAN) {
   1144 		PRINTF(("mpscinit: undefined unit %d\n", sc->gtmpsc_unit));
   1145 		return;
   1146 	}
   1147 
   1148 	sc->gtmpsc_chr2 = 0;      /* Default value of CHR2 */
   1149 
   1150 	/*
   1151 	 * stop GTMPSC unit
   1152 	 */
   1153 	r = sc->gtmpsc_chr2 | GTMPSC_CHR2_RXABORT|GTMPSC_CHR2_TXABORT;
   1154 	GT_WRITE(sc, GTMPSC_U_CHRN(unit, 2), r);
   1155 
   1156 	DELAY(GTMPSC_RESET_DELAY);
   1157 
   1158 	/*
   1159 	 * abort SDMA TX, RX for GTMPSC unit
   1160 	 */
   1161 	GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_AR|SDMA_SDCM_AT);
   1162 
   1163 	if (once == 0) {
   1164 		/*
   1165 		 * Determine if this is the buggy GT-64260A case.
   1166 		 * If this is, then most of GTMPSC and SDMA registers
   1167 		 * are unreadable.
   1168 		 * (They always yield -1).
   1169 		 */
   1170 		GT_WRITE(sc, SDMA_IMASK, 0);
   1171 		r = GT_READ(sc, SDMA_IMASK);
   1172 		gt_reva_gtmpsc_bug = r == ~0;
   1173 		sdma_imask = 0;
   1174 	}
   1175 
   1176 	/*
   1177 	 * If Rx is disabled, we don't need to wait around for
   1178 	 * abort completion.
   1179 	 */
   1180 	if ((GT_READ(sc, GTMPSC_U_MMCR_LO(unit)) & GTMPSC_MMCR_LO_ER) == 0)
   1181 		return;
   1182 
   1183 	/*
   1184 	 * poll for GTMPSC RX abort completion
   1185 	 */
   1186 	if (gt_reva_gtmpsc_bug) {
   1187 		/* Sync up with the device first */
   1188 		r = GT_READ(sc, GTMPSC_U_CHRN(unit, 2));
   1189 		DELAY(GTMPSC_RESET_DELAY);
   1190 	} else
   1191 		for (;;) {
   1192 			r = GT_READ(sc, GTMPSC_U_CHRN(unit, 2));
   1193 			if (! (r & GTMPSC_CHR2_RXABORT))
   1194 				break;
   1195 		}
   1196 
   1197 	/*
   1198 	 * poll for SDMA RX abort completion
   1199 	 */
   1200 	for (;;) {
   1201 		r = GT_READ(sc, SDMA_U_SDCM(unit));
   1202 		if (! (r & (SDMA_SDCM_AR|SDMA_SDCM_AT)))
   1203 			break;
   1204 		DELAY(50);
   1205 	}
   1206 
   1207 }
   1208 
   1209 STATIC void
   1210 gtmpscinit_start(struct gtmpsc_softc *sc, int once)
   1211 {
   1212 	unsigned int r;
   1213 	unsigned int unit = sc->gtmpsc_unit;
   1214 
   1215 	/*
   1216 	 * initialize softc's "current" transfer indicies & counts
   1217 	 */
   1218 	sc->gtmpsc_cx = 0;
   1219 	sc->gtmpsc_nc = 0;
   1220 	sc->gtmpsc_poll_txix = 0;
   1221 	sc->gtmpsc_poll_rxix = 0;
   1222 
   1223 	/*
   1224 	 * initialize softc's RX softintr FIFO
   1225 	 */
   1226 	sc->gtmpsc_rxfifo_putix = 0;
   1227 	sc->gtmpsc_rxfifo_getix = 0;
   1228 	sc->gtmpsc_rxfifo_navail = GTMPSC_RXFIFOSZ;
   1229 	memset(&sc->gtmpsc_rxfifo[0], 0, GTMPSC_RXFIFOSZ);
   1230 
   1231 	/*
   1232 	 * set SDMA unit port TX descriptor pointers
   1233 	 * "next" pointer of last descriptor is start of ring
   1234 	 */
   1235 	r = desc_read(
   1236 	    &sc->gtmpsc_poll_sdmapage->tx[GTMPSC_NTXDESC-1].txdesc.sdma_next);
   1237 	GT_WRITE(sc, SDMA_U_SCTDP(unit), r);   /* current */
   1238 	(void)GT_READ(sc, SDMA_U_SCTDP(unit));
   1239 	GT_WRITE(sc, SDMA_U_SFTDP(unit), r);   /* first   */
   1240 	(void)GT_READ(sc, SDMA_U_SFTDP(unit));
   1241 	/*
   1242 	 * set SDMA unit port RX descriptor pointer
   1243 	 * "next" pointer of last descriptor is start of ring
   1244 	 */
   1245 	r = desc_read(
   1246 	    &sc->gtmpsc_poll_sdmapage->rx[GTMPSC_NRXDESC-1].rxdesc.sdma_next);
   1247 	GT_WRITE(sc, SDMA_U_SCRDP(unit), r);   /* current */
   1248 
   1249 	/*
   1250 	 * initialize SDMA unit Configuration Register
   1251 	 */
   1252 	r = SDMA_SDC_BSZ_8x64|SDMA_SDC_SFM|SDMA_SDC_RFT;
   1253 	GT_WRITE(sc, SDMA_U_SDC(unit), r);
   1254 
   1255 	/*
   1256 	 * enable SDMA receive
   1257 	 */
   1258 	GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_ERD);
   1259 
   1260 	if (once == 0) {
   1261 		/*
   1262 		 * GTMPSC Routing:
   1263 		 *	MR0 --> Serial Port 0
   1264 		 *	MR1 --> Serial Port 1
   1265 		 */
   1266 		GT_WRITE(sc, GTMPSC_MRR, GTMPSC_MRR_RES);
   1267 
   1268 		/*
   1269 		 * RX and TX Clock Routing:
   1270 		 *      CRR0 --> BRG0
   1271 		 *      CRR1 --> BRG1
   1272 		 */
   1273 		r = GTMPSC_CRR_BRG0 | (GTMPSC_CRR_BRG1 << GTMPSC_CRR1_SHIFT);
   1274 		GT_WRITE(sc, GTMPSC_RCRR, r);
   1275 		GT_WRITE(sc, GTMPSC_TCRR, r);
   1276 	}
   1277 	sc->gtmpsc_brg_bcr = BRG_BCR_EN | GT_MPSC_CLOCK_SOURCE |
   1278 	    compute_cdv(GT_MPSC_DEFAULT_BAUD_RATE);
   1279 	sc->gtmpsc_chr3 = GTMPSC_MAXIDLE(GT_MPSC_DEFAULT_BAUD_RATE);
   1280 	gtmpsc_loadchannelregs(sc);
   1281 
   1282 	/*
   1283 	 * set MPSC Protocol configuration register for GTMPSC unit
   1284 	 */
   1285 	GT_WRITE(sc, GTMPSC_U_MPCR(unit), GTMPSC_MPCR_CL_8);
   1286 
   1287 	/*
   1288 	 * set MPSC LO and HI port config registers for GTMPSC unit
   1289  	 */
   1290 	r = GTMPSC_MMCR_LO_MODE_UART
   1291 	   |GTMPSC_MMCR_LO_ET
   1292 	   |GTMPSC_MMCR_LO_ER
   1293 	   |GTMPSC_MMCR_LO_NLM;
   1294 	GT_WRITE(sc, GTMPSC_U_MMCR_LO(unit), r);
   1295 
   1296 	r =
   1297 	    GTMPSC_MMCR_HI_TCDV_DEFAULT
   1298 	   |GTMPSC_MMCR_HI_RDW
   1299 	   |GTMPSC_MMCR_HI_RCDV_DEFAULT;
   1300 	GT_WRITE(sc, GTMPSC_U_MMCR_HI(unit), r);
   1301 
   1302 	/*
   1303 	 * tell MPSC receive the Enter Hunt
   1304 	 */
   1305 	r = sc->gtmpsc_chr2 | GTMPSC_CHR2_EH;
   1306 	GT_WRITE(sc, GTMPSC_U_CHRN(unit, 2), r);
   1307 
   1308 	/*
   1309 	 * clear any pending SDMA interrupts for this unit
   1310 	 */
   1311 	r = GT_READ(sc, SDMA_ICAUSE);
   1312 	r &= ~SDMA_U_INTR_MASK(unit);
   1313 	GT_WRITE(sc, SDMA_ICAUSE, r);	/* ??? */
   1314 
   1315 	DPRINTF(("gtmpscinit: OK\n"));
   1316 }
   1317 
   1318 /*
   1319  * gtmpscinit - prepare MPSC for operation
   1320  *
   1321  *	assumes we are called at ipl >= IPL_SERIAL
   1322  */
   1323 STATIC void
   1324 gtmpscinit(struct gtmpsc_softc *sc)
   1325 {
   1326 	static int once = 0;
   1327 
   1328 	gtmpscinit_stop(sc, once);
   1329 	gtmpscinit_start(sc, once);
   1330 	once = 1;
   1331 }
   1332 
   1333 /*
   1334  * gtmpsccninit - initialize the driver and the mpsc device
   1335  */
   1336 void
   1337 gtmpsccninit(struct consdev *cd)
   1338 {
   1339 }
   1340 
   1341 int
   1342 gtmpsccngetc(dev_t dev)
   1343 {
   1344 	unsigned int unit = 0;
   1345 	int c;
   1346 
   1347 	unit = GTMPSCUNIT(dev);
   1348 	if (major(dev) != 0) {
   1349 		struct gtmpsc_softc *sc = device_lookup(&gtmpsc_cd, unit);
   1350 		if (sc == NULL)
   1351 			return 0;
   1352 		unit = sc->gtmpsc_unit;
   1353 	}
   1354 	if (unit >= GTMPSC_NCHAN)
   1355 		return 0;
   1356 	c = gtmpsc_common_getc(unit);
   1357 
   1358 	return c;
   1359 }
   1360 
   1361 void
   1362 gtmpsccnputc(dev_t dev, int c)
   1363 {
   1364 	unsigned int unit = 0;
   1365 	char ch = c;
   1366 	static int ix = 0;
   1367 
   1368 	if (gtmpsccninit_done == 0) {
   1369 		if ((minor(dev) == 0) && (ix < sizeof(gtmpsc_earlybuf)))
   1370 			gtmpsc_earlybuf[ix++] = (unsigned char)c;
   1371 		return;
   1372 	}
   1373 
   1374 	unit = GTMPSCUNIT(dev);
   1375 	if (major(dev) != 0) {
   1376 		struct gtmpsc_softc *sc = device_lookup(&gtmpsc_cd, unit);
   1377 		if (sc == NULL)
   1378 			return;
   1379 		unit = sc->gtmpsc_unit;
   1380 	}
   1381 
   1382 	if (unit >= GTMPSC_NCHAN)
   1383 		return;
   1384 
   1385 	gtmpsc_common_putc(unit, ch);
   1386 }
   1387 
   1388 void
   1389 gtmpsccnpollc(dev_t dev, int on)
   1390 {
   1391 }
   1392 
   1393 int
   1394 gtmpsccnattach(bus_space_tag_t memt, bus_space_handle_t memh, int unit,
   1395 	int speed, tcflag_t tcflag)
   1396 {
   1397 	struct gtmpsc_softc *sc = &gtmpsc_fake_softc;
   1398 	unsigned char *cp;
   1399 	unsigned char c;
   1400 	unsigned int i;
   1401 
   1402 	if (gtmpsccninit_done)
   1403 		return 0;
   1404 
   1405 	DPRINTF(("gtmpsccnattach\n"));
   1406 	gtmpsc_hackinit(sc, memt, memh, unit);
   1407 	DPRINTF(("gtmpscinit\n"));
   1408 	gtmpscinit(sc);
   1409 	gtmpsccninit_done = 1;
   1410 	cp = gtmpsc_earlybuf;
   1411 	strcpy(gtmpsc_earlybuf, "\r\nMPSC Lives!\r\n");
   1412 	for (i=0; i < sizeof(gtmpsc_earlybuf); i++) {
   1413 		c = *cp++;
   1414 		if (c == 0)
   1415 			break;
   1416 		gtmpsc_common_putc(unit, c);
   1417 	}
   1418 	DPRINTF(("switching cn_tab\n"));
   1419 	gtmpsc_consdev.cn_dev = makedev(0, unit);
   1420 	cn_tab = &gtmpsc_consdev;
   1421 	DPRINTF(("switched cn_tab!\n"));
   1422 	return 0;
   1423 }
   1424 
   1425 /*
   1426  * gtmpsc_common_pollc - non-blocking console read
   1427  *
   1428  *	if there is an RX char, return it in *cp
   1429  *	set *statp if Break detected
   1430  *
   1431  *	assumes we are called at ipl >= IPL_SERIAL
   1432  *
   1433  * return 1 if there is RX data
   1434  * otherwise return 0
   1435  */
   1436 STATIC int
   1437 gtmpsc_common_pollc(unsigned int unit, char *cp, int *statp)
   1438 {
   1439 	struct gtmpsc_softc *sc = gtmpsc_scp[unit];
   1440 	gtmpsc_pollrx_t *vrxp;
   1441 	unsigned int ix;
   1442 	unsigned int cx;
   1443 	unsigned int nc;
   1444 
   1445 	*statp = GTMPSC_STAT_NONE;
   1446 	ix = sc->gtmpsc_poll_rxix;
   1447 	nc = sc->gtmpsc_nc;
   1448 	cx = sc->gtmpsc_cx;
   1449 	if (nc == 0) {
   1450 		unsigned int *csrp;
   1451 		unsigned int csr;
   1452 
   1453 		vrxp = &sc->gtmpsc_poll_sdmapage->rx[ix];
   1454 		csrp = &vrxp->rxdesc.sdma_csr;
   1455 		cx = 0;
   1456 
   1457 		GTMPSC_CACHE_INVALIDATE(csrp);
   1458 		csr = desc_read(csrp);
   1459 		if (csr & SDMA_CSR_RX_OWN)
   1460 			return 0;
   1461 		if (csr & SDMA_CSR_RX_BR)
   1462 			*statp = GTMPSC_STAT_BREAK;
   1463 		if (csr & SDMA_CSR_RX_ES)
   1464 			PRINTF(("mpsc 0 RX error, rxdesc csr 0x%x\n", csr));
   1465 
   1466 		nc = desc_read(&vrxp->rxdesc.sdma_cnt);
   1467 		nc &= SDMA_RX_CNT_BCNT_MASK;
   1468 		csr = SDMA_CSR_RX_L|SDMA_CSR_RX_F|SDMA_CSR_RX_OWN
   1469 							      |SDMA_CSR_RX_EI;
   1470 		if (nc == 0) {
   1471 			if ((++ix) >= GTMPSC_NRXDESC)
   1472 				ix = 0;
   1473 			sc->gtmpsc_poll_rxix = ix;
   1474 			desc_write(csrp, csr);
   1475 			GTMPSC_CACHE_FLUSH(csrp);
   1476 			return 0;
   1477 		}
   1478 		bcopy(vrxp->rxbuf, sc->gtmpsc_rxbuf, nc);
   1479 		desc_write(csrp, csr);
   1480 		GTMPSC_CACHE_FLUSH(csrp);
   1481 	}
   1482 	gtmpsc_poll_pollc_cnt++;
   1483 	nc--;
   1484 	*cp = sc->gtmpsc_rxbuf[cx++];
   1485 	if (nc == 0) {
   1486 		if ((++ix) >= GTMPSC_NRXDESC)
   1487 			ix = 0;
   1488 		sc->gtmpsc_poll_rxix = ix;
   1489 	}
   1490 	sc->gtmpsc_cx = cx;
   1491 	sc->gtmpsc_nc = nc;
   1492 	return 1;
   1493 }
   1494 
   1495 /*
   1496  * gtmpsc_common_getc - polled console read
   1497  *
   1498  *	We copy data from the DMA buffers into a buffer in the softc
   1499  *	to reduce descriptor ownership turnaround time
   1500  *	MPSC can crater if it wraps descriptor rings,
   1501  *	which is asynchronous and throttled only by line speed.
   1502  *
   1503  *	This code assumes the buffer PA==KVA
   1504  *	and assumes we are called at ipl >= IPL_SERIAL
   1505  */
   1506 STATIC int
   1507 gtmpsc_common_getc(unsigned int unit)
   1508 {
   1509 	struct gtmpsc_softc *sc = gtmpsc_scp[unit];
   1510 	gtmpsc_pollrx_t *vrxp;
   1511 	unsigned int ix;
   1512 	unsigned int cx;
   1513 	unsigned int nc;
   1514 	int c;
   1515 
   1516 	ix = sc->gtmpsc_poll_rxix;
   1517 	nc = sc->gtmpsc_nc;
   1518 	cx = sc->gtmpsc_cx;
   1519 	while (nc == 0) {
   1520 		unsigned int *csrp;
   1521 		unsigned int csr;
   1522 		unsigned int r;
   1523 
   1524 		vrxp = &sc->gtmpsc_poll_sdmapage->rx[ix];
   1525 		csrp = &vrxp->rxdesc.sdma_csr;
   1526 		cx = 0;
   1527 
   1528 		GTMPSC_CACHE_INVALIDATE(csrp);
   1529 		csr = desc_read(csrp);
   1530 		if (csr & SDMA_CSR_RX_OWN) {
   1531 			r = sc->gtmpsc_chr2 | GTMPSC_CHR2_CRD;
   1532 			GT_WRITE(sc, GTMPSC_U_CHRN(unit, 2), r);
   1533 			do {
   1534 
   1535 				gtmpsc_poll_getc_miss++;
   1536 				GTMPSC_CACHE_INVALIDATE(csrp);
   1537 				DELAY(50);
   1538 				csr = desc_read(csrp);
   1539 			} while (csr & SDMA_CSR_RX_OWN);
   1540 		}
   1541 		if (csr & SDMA_CSR_RX_ES)
   1542 			PRINTF(("mpsc 0 RX error, rxdesc csr 0x%x\n", csr));
   1543 
   1544 		nc = desc_read(&vrxp->rxdesc.sdma_cnt);
   1545 		nc &= SDMA_RX_CNT_BCNT_MASK;
   1546 		if (nc) {
   1547 			bcopy(vrxp->rxbuf, sc->gtmpsc_rxbuf, nc);
   1548 		} else {
   1549 			if ((++ix) >= GTMPSC_NRXDESC)
   1550 				ix = 0;
   1551 			sc->gtmpsc_poll_rxix = ix;
   1552 		}
   1553 		csr = SDMA_CSR_RX_L|SDMA_CSR_RX_F|SDMA_CSR_RX_OWN
   1554 							|SDMA_CSR_RX_EI;
   1555 		desc_write(csrp, csr);
   1556 		GTMPSC_CACHE_FLUSH(csrp);
   1557 #ifdef KGDB
   1558 		if (unit == comkgdbport && gt_reva_gtmpsc_bug)
   1559 			GT_WRITE(sc, SDMA_ICAUSE, ~SDMA_INTR_RXBUF(unit));
   1560 #endif
   1561 	}
   1562 	gtmpsc_poll_getc_cnt++;
   1563 	nc--;
   1564 	c = (int)sc->gtmpsc_rxbuf[cx++];
   1565 	if (nc == 0) {
   1566 		if ((++ix) >= GTMPSC_NRXDESC)
   1567 			ix = 0;
   1568 		sc->gtmpsc_poll_rxix = ix;
   1569 	}
   1570 	sc->gtmpsc_cx = cx;
   1571 	sc->gtmpsc_nc = nc;
   1572 	return c;
   1573 }
   1574 
   1575 /*
   1576  * gtmpsc_common_putn - write a buffer into the hardware
   1577  *
   1578  *	assumes we are called at ipl >= IPL_SERIAL
   1579  */
   1580 STATIC void
   1581 gtmpsc_common_putn(struct gtmpsc_softc *sc)
   1582 
   1583 {
   1584 	int     unit = sc->gtmpsc_unit;
   1585 	int     n;
   1586 	int     kick;
   1587 	gtmpsc_polltx_t *vtxp;
   1588 	unsigned int *csrp;
   1589 	unsigned int csr;
   1590 	unsigned int ix;
   1591 	unsigned int sdcm;
   1592 
   1593 	kick = 0;
   1594 	for (ix = sc->gtmpsc_poll_txix; sc->sc_tbc;) {
   1595 		vtxp = &sc->gtmpsc_poll_sdmapage->tx[ix];
   1596 		csrp = &vtxp->txdesc.sdma_csr;
   1597 		GTMPSC_CACHE_INVALIDATE(csrp);
   1598 		csr = desc_read(csrp);
   1599 		if ((csr & SDMA_CSR_TX_OWN) != 0)
   1600 			break;
   1601 		n = sc->sc_tbc;
   1602 		if (n > GTMPSC_TXBUFSZ)
   1603 			n = GTMPSC_TXBUFSZ;
   1604 		bcopy(sc->sc_tba, vtxp->txbuf, n);
   1605 		csr = SDMA_CSR_TX_L | SDMA_CSR_TX_F
   1606 					| SDMA_CSR_TX_EI | SDMA_CSR_TX_OWN;
   1607 		desc_write(&vtxp->txdesc.sdma_cnt,
   1608 				(n << SDMA_TX_CNT_BCNT_SHIFT) | n);
   1609 		desc_write(csrp, csr);
   1610 		GTMPSC_CACHE_FLUSH(csrp);
   1611 		sc->sc_tbc -= n;
   1612 		sc->sc_tba += n;
   1613 		gtmpsc_poll_putn_cnt += n;
   1614 		sc->cnt_tx_to_sdma += n;
   1615 		kick = 1;
   1616 
   1617 		if (++ix >= GTMPSC_NTXDESC)
   1618 			ix = 0;
   1619 	}
   1620 	if (kick) {
   1621 		sc->gtmpsc_poll_txix = ix;
   1622 
   1623 		/*
   1624 		 * now kick some SDMA
   1625 		 */
   1626 		sdcm = GT_READ(sc, SDMA_U_SDCM(unit));
   1627 
   1628 		if ((sdcm & SDMA_SDCM_TXD) == 0) {
   1629 			GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_TXD);
   1630 		}
   1631 	}
   1632 }
   1633 
   1634 /*
   1635  * gtmpsc_common_putc - polled console putc
   1636  *
   1637  *	assumes we are called at ipl >= IPL_SERIAL
   1638  */
   1639 STATIC void
   1640 gtmpsc_common_putc(unsigned int unit, unsigned char c)
   1641 {
   1642 	struct gtmpsc_softc *sc = gtmpsc_scp[unit];
   1643 	gtmpsc_polltx_t *vtxp;
   1644 	unsigned int *csrp;
   1645 	unsigned int csr;
   1646 	unsigned int ix;
   1647 	unsigned int nix;
   1648 
   1649 	DPRINTF(("gtmpsc_common_putc(%d, '%c'): cur=%#x, first=%#x", unit, c,
   1650 	    GT_READ(sc, SDMA_U_SCTDP(unit)),   /* current */
   1651 	    GT_READ(sc, SDMA_U_SFTDP(unit))));   /* first   */
   1652 	ix = sc->gtmpsc_poll_txix;
   1653 	nix = ix + 1;
   1654 	if (nix >= GTMPSC_NTXDESC)
   1655 		nix = 0;
   1656 	sc->gtmpsc_poll_txix = nix;
   1657 	vtxp = &sc->gtmpsc_poll_sdmapage->tx[ix];
   1658 	csrp = &vtxp->txdesc.sdma_csr;
   1659 
   1660 
   1661 	for (;;) {
   1662 		GTMPSC_CACHE_INVALIDATE(csrp);
   1663 		csr = desc_read(csrp);
   1664 		if ((csr & SDMA_CSR_TX_OWN) == 0)
   1665 			break;
   1666 		gtmpsc_poll_putc_miss++;
   1667 		DELAY(40);
   1668 		DPRINTF(("."));
   1669 	}
   1670 	if (csr & SDMA_CSR_TX_ES)
   1671 		PRINTF(("mpsc %d TX error, txdesc csr 0x%x\n", unit, csr));
   1672 
   1673 	gtmpsc_poll_putc_cnt++;
   1674 	vtxp->txbuf[0] = c;
   1675 	csr = SDMA_CSR_TX_L | SDMA_CSR_TX_F | SDMA_CSR_TX_EI | SDMA_CSR_TX_OWN;
   1676 	desc_write(&vtxp->txdesc.sdma_cnt, (1 << SDMA_TX_CNT_BCNT_SHIFT) | 1);
   1677 	desc_write(csrp, csr);
   1678 	GTMPSC_CACHE_FLUSH(csrp);
   1679 
   1680 	/*
   1681 	 * now kick some SDMA
   1682 	 */
   1683 	GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_TXD);
   1684 }
   1685 
   1686 
   1687 STATIC void
   1688 gtmpsc_poll(void *arg)
   1689 {
   1690 	struct gtmpsc_softc *sc = (struct gtmpsc_softc *)arg;
   1691 	int kick;
   1692 	char ch;
   1693 	int stat;
   1694 	static struct timeval   msg_time = {0,0};
   1695 	static struct timeval   cur_time;
   1696 	static int fifo_full = 0;
   1697 
   1698 	kick = 0;
   1699 	while (gtmpsc_common_pollc(sc->gtmpsc_unit, &ch, &stat)) {
   1700 #ifdef DDB
   1701 		if (stat)
   1702 			break;
   1703 #endif
   1704 		++sc->cnt_rx_from_sdma;
   1705 		if (sc->gtmpsc_rxfifo_navail != 0) {
   1706 			sc->gtmpsc_rxfifo[sc->gtmpsc_rxfifo_putix++] = ch;
   1707 			if (sc->gtmpsc_rxfifo_putix == GTMPSC_RXFIFOSZ)
   1708 				sc->gtmpsc_rxfifo_putix = 0;
   1709 			atomic_add(&sc->gtmpsc_rxfifo_navail, -1);
   1710 			++sc->cnt_rx_to_fifo;
   1711 			fifo_full = 0;
   1712 			kick = 1;
   1713 		} else {
   1714 			if (fifo_full == 0) {
   1715 				fifo_full = 1;
   1716 				microtime(&cur_time);
   1717 				if (cur_time.tv_sec - msg_time.tv_sec >= 5) {
   1718 					/* Only do this once in 5 sec */
   1719 					msg_time = cur_time;
   1720 					printf("mpsc%d: input FIFO full, "
   1721 					       "dropping incoming characters\n",
   1722 					       sc->gtmpsc_unit);
   1723 				}
   1724 			}
   1725 		}
   1726 	}
   1727 #ifdef DDB
   1728 	if (stat) {
   1729 		if (cn_tab == &gtmpsc_consdev) {
   1730 			Debugger();
   1731 		}
   1732 	}
   1733 #endif
   1734 	if (kick)
   1735 		softintr_schedule(sc->sc_si);
   1736 }
   1737 
   1738 #ifdef KGDB
   1739 /* ARGSUSED */
   1740 STATIC int
   1741 gtmpsc_kgdb_getc(arg)
   1742 	void *arg;
   1743 {
   1744 
   1745 	return (gtmpsc_common_getc(comkgdbport));
   1746 }
   1747 
   1748 /* ARGSUSED */
   1749 STATIC void
   1750 gtmpsc_kgdb_putc(arg, c)
   1751 	void *arg;
   1752 	int c;
   1753 {
   1754 
   1755 	return (gtmpsc_common_putc(comkgdbport, c));
   1756 }
   1757 
   1758 STATIC void
   1759 gtmpsc_kgdb_poll(void *arg)
   1760 {
   1761 	struct gtmpsc_softc       *sc = (struct gtmpsc_softc *)arg;
   1762 	int                     s;
   1763 	char                    c;
   1764 	int                     brk;
   1765 
   1766 	s = splserial();
   1767 	if (kgdb_recover == 0) { /* gdb is not currently talking to its agent */
   1768 		while (gtmpsc_common_pollc(sc->gtmpsc_unit, &c, &brk)) {
   1769 			if (c == CTRL('c'))
   1770 				brk = GTMPSC_STAT_BREAK;
   1771 			if (brk == GTMPSC_STAT_BREAK)
   1772 				break;
   1773 		}
   1774 		if (brk == GTMPSC_STAT_BREAK) {
   1775 			if (kgdb_break_immediate)
   1776 				breakpoint();
   1777 			else {
   1778 				printf("connecting to kgdb\n");
   1779 				kgdb_connect(1);
   1780 			}
   1781 		}
   1782 	}
   1783 	splx(s);
   1784 }
   1785 
   1786 #endif /* KGDB */
   1787 
   1788 #if 0
   1789 void
   1790 gtmpsc_printf(const char *fmt, ...)
   1791 {
   1792 	struct consdev *ocd;
   1793 	int s;
   1794 	va_list ap;
   1795 
   1796 	s = splserial();
   1797 	ocd = cn_tab;
   1798 	cn_tab = &constab[0];
   1799 	va_start(ap, fmt);
   1800 	printf(fmt, ap);
   1801 	va_end(ap);
   1802 	cn_tab = ocd;
   1803 	splx(s);
   1804 }
   1805 #endif
   1806 
   1807 void
   1808 gtmpsc_mem_printf(const char *fmt, ...)
   1809 {
   1810 	va_list ap;
   1811 	static unsigned char *p = gtmpsc_print_buf;
   1812 
   1813 	if (p >= &gtmpsc_print_buf[GTMPSC_PRINT_BUF_SIZE - 128]) {
   1814 		bzero(gtmpsc_print_buf, GTMPSC_PRINT_BUF_SIZE);
   1815 		p = gtmpsc_print_buf;
   1816 	}
   1817 	va_start(ap, fmt);
   1818 	p += vsprintf(p, fmt, ap);
   1819 	va_end(ap);
   1820 }
   1821 
   1822 void
   1823 gtmpsc_shutdownhook(void *arg)
   1824 {
   1825 	gtmpsc_softc_t *sc = (gtmpsc_softc_t *)arg;
   1826 
   1827 	gtmpsc_txflush(sc);
   1828 }
   1829 
   1830 void
   1831 gtmpsccnhalt(dev_t dev)
   1832 {
   1833 	unsigned int unit;
   1834 	u_int32_t r;
   1835 
   1836 	for (unit = 0; unit < GTMPSC_NCHAN; unit++) {
   1837 		gtmpsc_softc_t *sc = gtmpsc_scp[unit];
   1838 		if (sc == NULL)
   1839 			continue;
   1840 
   1841 		/*
   1842 		 * flush TX buffers
   1843 		 */
   1844 		gtmpsc_txflush(sc);
   1845 
   1846 		/*
   1847 		 * stop MPSC unit RX
   1848 		 */
   1849 		r = GT_READ(sc, GTMPSC_U_CHRN(unit, 2));
   1850 		r &= ~GTMPSC_CHR2_EH;
   1851 		r |= GTMPSC_CHR2_RXABORT;
   1852 		GT_WRITE(sc, GTMPSC_U_CHRN(unit, 2), r);
   1853 
   1854 		DELAY(GTMPSC_RESET_DELAY);
   1855 
   1856 		/*
   1857 		 * abort SDMA RX for MPSC unit
   1858 		 */
   1859 		GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_AR);
   1860 	}
   1861 }
   1862 
   1863 void
   1864 gtmpsc_puts(char *str)
   1865 {
   1866 	char c;
   1867 
   1868 	if (str == NULL)
   1869 		return;
   1870 	while ((c = *str++) != 0)
   1871 		gtmpsccnputc(0, c);
   1872 }
   1873