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