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