Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / marvell / gtmpsc.c
blob78c5bb177d98bd16835716d63b5d77c7a6a07945
1 /* $NetBSD: gtmpsc.c,v 1.36 2009/05/12 12:18:45 cegger Exp $ */
3 /*
4 * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
5 * All rights reserved.
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.
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.
41 * mpsc.c - MPSC serial driver, supports UART mode only
44 * creation Mon Apr 9 19:40:15 PDT 2001 cliff
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: gtmpsc.c,v 1.36 2009/05/12 12:18:45 cegger Exp $");
50 #include "opt_kgdb.h"
52 #include <sys/param.h>
53 #include <sys/conf.h>
54 #include <sys/device.h>
55 #include <sys/kauth.h>
56 #include <sys/proc.h>
57 #include <sys/systm.h>
58 #include <sys/tty.h>
59 #include <sys/callout.h>
60 #include <sys/fcntl.h>
61 #include <sys/intr.h>
62 #ifdef KGDB
63 #include <sys/kernel.h>
64 #include <sys/kgdb.h>
65 #endif
67 #include <uvm/uvm_extern.h>
69 #include <powerpc/atomic.h>
70 #include <dev/cons.h>
71 #include <sys/bus.h>
72 #include <sys/cpu.h> /* for DELAY */
73 #include <machine/stdarg.h>
74 #include "gtmpsc.h"
77 #include <dev/marvell/gtreg.h>
78 #include <dev/marvell/gtvar.h>
79 #include <dev/marvell/gtintrreg.h>
80 #include <dev/marvell/gtmpscreg.h>
81 #include <dev/marvell/gtsdmareg.h>
82 #include <dev/marvell/gtmpscvar.h>
83 #include <dev/marvell/gtbrgreg.h>
86 * XXX these delays were derived empiracaly
88 #define GTMPSC_POLL_DELAY 1 /* 1 usec */
90 * Wait 2 characters time for RESET_DELAY
92 #define GTMPSC_RESET_DELAY (2*8*1000000 / GT_MPSC_DEFAULT_BAUD_RATE)
94 #define BURSTLEN 128
97 * stat values for gtmpsc_common_pollc
99 #define GTMPSC_STAT_NONE 0
100 #define GTMPSC_STAT_BREAK 1
103 #define PRINTF(x) gtmpsc_mem_printf x
105 #if defined(DEBUG)
106 unsigned int gtmpsc_debug = 0;
107 # define STATIC
108 # define DPRINTF(x) do { if (gtmpsc_debug) gtmpsc_mem_printf x ; } while (0)
109 #else
110 # define STATIC static
111 # define DPRINTF(x)
112 #endif
114 #define GTMPSCUNIT_MASK 0x7ffff
115 #define GTMPSCDIALOUT_MASK 0x80000
117 #define GTMPSCUNIT(x) (minor(x) & GTMPSCUNIT_MASK)
118 #define GTMPSCDIALOUT(x) (minor(x) & GTMPSCDIALOUT_MASK)
120 STATIC void gtmpscinit(struct gtmpsc_softc *);
121 STATIC int gtmpscmatch(device_t, cfdata_t, void *);
122 STATIC void gtmpscattach(device_t, device_t, void *);
123 STATIC int compute_cdv(unsigned int);
124 STATIC void gtmpsc_loadchannelregs(struct gtmpsc_softc *);
125 STATIC void gtmpscshutdown(struct gtmpsc_softc *);
126 STATIC void gtmpscstart(struct tty *);
127 STATIC int gtmpscparam(struct tty *, struct termios *);
128 STATIC int gtmpsc_probe(void);
129 STATIC int gtmpsc_intr(void *);
130 STATIC void gtmpsc_softintr(void *);
132 STATIC void gtmpsc_common_putn(struct gtmpsc_softc *);
133 STATIC void gtmpsc_common_putc(unsigned int, unsigned char);
134 STATIC int gtmpsc_common_getc(unsigned int);
135 STATIC int gtmpsc_common_pollc(unsigned int, char *, int *);
136 STATIC void gtmpsc_poll(void *);
137 #ifdef KGDB
138 STATIC void gtmpsc_kgdb_poll(void *);
139 #endif
140 STATIC void gtmpsc_mem_printf(const char *, ...);
142 STATIC void gtmpsc_txdesc_init(gtmpsc_poll_sdma_t *, gtmpsc_poll_sdma_t *);
143 STATIC void gtmpsc_rxdesc_init(gtmpsc_poll_sdma_t *, gtmpsc_poll_sdma_t *);
144 STATIC unsigned int gtmpsc_get_causes(void);
145 STATIC void gtmpsc_hackinit(struct gtmpsc_softc *, bus_space_tag_t,
146 bus_space_handle_t, int);
147 STATIC void gtmpscinit_stop(struct gtmpsc_softc *, int);
148 STATIC void gtmpscinit_start(struct gtmpsc_softc *, int);
149 #if 0
150 void gtmpsc_printf(const char *fmt, ...);
151 #endif
152 void gtmpsc_puts(char *);
154 void gtmpsccnprobe(struct consdev *);
155 void gtmpsccninit(struct consdev *);
156 int gtmpsccngetc(dev_t);
157 void gtmpsccnputc(dev_t, int);
158 void gtmpsccnpollc(dev_t, int);
159 void gtmpsccnhalt(dev_t);
161 STATIC void gtmpsc_txflush(gtmpsc_softc_t *);
162 STATIC void gtmpsc_iflush(gtmpsc_softc_t *);
163 STATIC void gtmpsc_shutdownhook(void *);
165 dev_type_open(gtmpscopen);
166 dev_type_close(gtmpscclose);
167 dev_type_read(gtmpscread);
168 dev_type_write(gtmpscwrite);
169 dev_type_ioctl(gtmpscioctl);
170 dev_type_stop(gtmpscstop);
171 dev_type_tty(gtmpsctty);
172 dev_type_poll(gtmpscpoll);
174 const struct cdevsw gtmpsc_cdevsw = {
175 gtmpscopen, gtmpscclose, gtmpscread, gtmpscwrite, gtmpscioctl,
176 gtmpscstop, gtmpsctty, gtmpscpoll, nommap, ttykqfilter, D_TTY
179 CFATTACH_DECL(gtmpsc, sizeof(struct gtmpsc_softc),
180 gtmpscmatch, gtmpscattach, NULL, NULL);
182 extern struct cfdriver gtmpsc_cd;
184 static struct consdev gtmpsc_consdev = {
186 gtmpsccninit,
187 gtmpsccngetc,
188 gtmpsccnputc,
189 gtmpsccnpollc,
190 NULL, /* cn_bell */
191 gtmpsccnhalt,
192 NULL, /* cn_flush */
193 NODEV,
194 CN_NORMAL
197 STATIC void *gtmpsc_sdma_ih = NULL;
199 gtmpsc_softc_t *gtmpsc_scp[GTMPSC_NCHAN] = { 0 };
201 STATIC int gt_reva_gtmpsc_bug;
202 unsigned int sdma_imask; /* soft copy of SDMA IMASK reg */
204 #ifdef KGDB
205 static int gtmpsc_kgdb_addr;
206 static int gtmpsc_kgdb_attached;
208 int kgdb_break_immediate /* = 0 */ ;
210 STATIC int gtmpsc_kgdb_getc(void *);
211 STATIC void gtmpsc_kgdb_putc(void *, int);
212 #endif /* KGDB */
215 * hacks for console initialization
216 * which happens prior to autoconfig "attach"
218 * XXX Assumes PAGE_SIZE is a constant!
220 STATIC unsigned int gtmpsccninit_done = 0;
221 STATIC gtmpsc_softc_t gtmpsc_fake_softc;
222 STATIC unsigned char gtmpsc_earlybuf[PAGE_SIZE]
223 __aligned(PAGE_SIZE);
224 STATIC unsigned char gtmpsc_fake_dmapage[PAGE_SIZE]
225 __aligned(PAGE_SIZE);
228 #define GTMPSC_PRINT_BUF_SIZE 4096
229 STATIC unsigned char gtmpsc_print_buf[GTMPSC_PRINT_BUF_SIZE] = { 0 };
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;
240 #ifndef SDMA_COHERENT
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
248 #define GTMPSC_CACHE_FLUSH(p) gtmpsc_cache_flush(p)
249 #define GTMPSC_CACHE_INVALIDATE(p) gtmpsc_cache_invalidate(p)
251 static inline void
252 gtmpsc_cache_flush(void *p)
254 __asm volatile ("eieio; dcbf 0,%0; lwz %0,0(%0); sync;"
255 : "+r"(p):);
258 static inline void
259 gtmpsc_cache_invalidate(void *p)
261 __asm volatile ("eieio; dcbi 0,%0; sync;" :: "r"(p));
263 #else
265 #define GTMPSC_CACHE_FLUSH(p)
266 #define GTMPSC_CACHE_INVALIDATE(p)
268 #endif /* SDMA_COHERENT */
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))
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)
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)
299 static inline unsigned int
300 desc_read(unsigned int *ip)
302 unsigned int rv;
304 __asm volatile ("lwzx %0,0,%1; eieio;"
305 : "=r"(rv) : "r"(ip));
306 return rv;
309 static inline void
310 desc_write(unsigned int *ip, unsigned int val)
312 __asm volatile ("stwx %0,0,%1; eieio;"
313 :: "r"(val), "r"(ip));
318 * gtmpsc_txdesc_init - set up TX descriptor ring
320 STATIC void
321 gtmpsc_txdesc_init(gtmpsc_poll_sdma_t *vmps, gtmpsc_poll_sdma_t *pmps)
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;
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++;
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);
353 * gtmpsc_rxdesc_init - set up RX descriptor ring
355 STATIC void
356 gtmpsc_rxdesc_init(gtmpsc_poll_sdma_t *vmps, gtmpsc_poll_sdma_t *pmps)
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;
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++;
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);
392 * Compute the BRG countdown value (CDV in BRG_BCR)
395 STATIC int
396 compute_cdv(unsigned int baud)
398 unsigned int cdv;
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;
408 STATIC void
409 gtmpsc_loadchannelregs(struct gtmpsc_softc *sc)
411 u_int brg_bcr;
412 u_int unit;
414 unit = sc->gtmpsc_unit;
415 brg_bcr = unit ? BRG_BCR1 : BRG_BCR0;
417 GT_WRITE(sc, brg_bcr, sc->gtmpsc_brg_bcr);
418 GT_WRITE(sc, GTMPSC_U_CHRN(unit, 3), sc->gtmpsc_chr3);
421 STATIC int
422 gtmpscmatch(device_t parent, cfdata_t self, void *aux)
424 struct gt_softc *gt = device_private(parent);
425 struct gt_attach_args *ga = aux;
427 return GT_MPSCOK(gt, ga, &gtmpsc_cd);
430 STATIC void
431 gtmpscattach(device_t parent, device_t self, void *aux)
433 struct gt_attach_args *ga = aux;
434 struct gt_softc *gt = device_private(parent);
435 struct gtmpsc_softc *sc = device_private(self);
436 gtmpsc_poll_sdma_t *vmps;
437 gtmpsc_poll_sdma_t *pmps;
438 struct tty *tp;
439 void *kva;
440 int rsegs;
441 int err;
442 int s;
443 int is_console = 0;
445 DPRINTF(("mpscattach\n"));
447 GT_MPSCFOUND(gt, ga);
449 s = splhigh();
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;
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;
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;
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;
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;
492 memset(kva, 0, PAGE_SIZE); /* paranoid/superfluous */
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;
503 if (gtmpsc_scp[sc->gtmpsc_unit] != NULL)
504 gtmpsc_txflush(gtmpsc_scp[sc->gtmpsc_unit]);
506 sc->gtmpsc_tty = tp = ttymalloc();
507 tp->t_oproc = gtmpscstart;
508 tp->t_param = gtmpscparam;
509 tty_attach(tp);
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");
518 sc->sc_si = softint_establish(SOFTINT_SERIAL, gtmpsc_softintr, sc);
519 if (sc->sc_si == NULL)
520 panic("mpscattach: cannot softint_establish IPL_SOFTSERIAL");
522 shutdownhook_establish(gtmpsc_shutdownhook, sc);
524 gtmpsc_scp[sc->gtmpsc_unit] = sc;
525 gtmpscinit(sc);
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 device_unit(&sc->gtmpsc_dev));
531 is_console = 1;
534 aprint_normal(" irq %s%s\n",
535 intr_string(IRQ_SDMA),
536 (gt_reva_gtmpsc_bug) ? " [Rev A. bug]" : "");
538 if (is_console)
539 aprint_normal_dev(&sc->gtmpsc_dev, "console\n");
541 #ifdef DDB
542 if (is_console == 0)
543 SDMA_IMASK_ENABLE(sc, SDMA_INTR_RXBUF(sc->gtmpsc_unit));
544 #endif /* DDB */
546 splx(s);
547 #ifdef KGDB
549 * Allow kgdb to "take over" this port. If this is
550 * the kgdb device, it has exclusive use.
552 if (sc->gtmpsc_unit == comkgdbport) {
553 if (comkgdbport == 0) { /* FIXME */
554 aprint_error_dev(&sc->gtmpsc_dev, "(kgdb): cannot share with console\n");
555 return;
558 sc->gtmpsc_flags |= GTMPSCF_KGDB;
559 printf("%s: kgdb\n", device_xname(&sc->gtmpsc_dev));
560 gtmpsc_txflush(gtmpsc_scp[0]);
561 kgdb_attach(gtmpsc_kgdb_getc, gtmpsc_kgdb_putc, NULL);
562 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
563 gtmpsc_kgdb_attached = 1;
564 SDMA_IMASK_ENABLE(sc, SDMA_INTR_RXBUF(sc->gtmpsc_unit));
565 kgdb_connect(1);
567 #endif /* KGDB */
570 STATIC void
571 gtmpscshutdown(struct gtmpsc_softc *sc)
573 struct tty *tp;
574 int s;
576 #ifdef KGDB
577 if (sc->gtmpsc_flags & GTMPSCF_KGDB != 0)
578 return;
579 #endif
580 tp = sc->gtmpsc_tty;
581 s = splserial();
582 /* Fake carrier off */
583 (void) (*tp->t_linesw->l_modem)(tp, 0);
584 SDMA_IMASK_DISABLE(sc, SDMA_INTR_RXBUF(sc->gtmpsc_unit));
585 splx(s);
589 gtmpscopen(dev_t dev, int flag, int mode, struct lwp *l)
591 struct gtmpsc_softc *sc;
592 int unit = GTMPSCUNIT(dev);
593 struct tty *tp;
594 int s;
595 int s2;
596 int error;
598 sc = device_lookup_private(&gtmpsc_cd, unit);
599 if (!sc)
600 return ENXIO;
601 #ifdef KGDB
603 * If this is the kgdb port, no other use is permitted.
605 if (sc->gtmpsc_flags & GTMPSCF_KGDB != 0)
606 return (EBUSY);
607 #endif
608 tp = sc->gtmpsc_tty;
609 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
610 return (EBUSY);
612 s = spltty();
614 if (!(tp->t_state & TS_ISOPEN)) {
615 struct termios t;
617 tp->t_dev = dev;
618 s2 = splserial();
619 SDMA_IMASK_ENABLE(sc, SDMA_INTR_RXBUF(unit));
620 splx(s2);
621 t.c_ispeed = 0;
622 #if 0
623 t.c_ospeed = TTYDEF_SPEED;
624 #else
625 t.c_ospeed = GT_MPSC_DEFAULT_BAUD_RATE;
626 #endif
627 t.c_cflag = TTYDEF_CFLAG;
628 /* Make sure gtmpscparam() will do something. */
629 tp->t_ospeed = 0;
630 (void) gtmpscparam(tp, &t);
631 tp->t_iflag = TTYDEF_IFLAG;
632 tp->t_oflag = TTYDEF_OFLAG;
633 tp->t_lflag = TTYDEF_LFLAG;
634 ttychars(tp);
635 ttsetwater(tp);
636 s2 = splserial();
637 /* Clear the input ring */
638 sc->gtmpsc_rxfifo_putix = 0;
639 sc->gtmpsc_rxfifo_getix = 0;
640 sc->gtmpsc_rxfifo_navail = GTMPSC_RXFIFOSZ;
641 gtmpsc_iflush(sc);
642 splx(s2);
644 splx(s);
645 error = ttyopen(tp, GTMPSCDIALOUT(dev), ISSET(flag, O_NONBLOCK));
646 if (error)
647 goto bad;
649 error = (*tp->t_linesw->l_open)(dev, tp);
650 if (error)
651 goto bad;
653 return (0);
655 bad:
656 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
658 * We failed to open the device, and nobody else had it opened.
659 * Clean up the state as appropriate.
661 gtmpscshutdown(sc);
664 return (error);
668 gtmpscclose(dev_t dev, int flag, int mode, struct lwp *l)
670 int unit = GTMPSCUNIT(dev);
671 struct gtmpsc_softc *sc = device_lookup_private(&gtmpsc_cd, unit);
672 struct tty *tp = sc->gtmpsc_tty;
673 int s;
675 s = splserial();
676 if (!ISSET(tp->t_state, TS_ISOPEN)) {
677 splx(s);
678 return (0);
681 (*tp->t_linesw->l_close)(tp, flag);
682 ttyclose(tp);
683 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
685 * Although we got a last close, the device may still be in
686 * use; e.g. if this was the dialout node, and there are still
687 * processes waiting for carrier on the non-dialout node.
689 gtmpscshutdown(sc);
692 splx(s);
693 return (0);
697 gtmpscread(dev_t dev, struct uio *uio, int flag)
699 struct gtmpsc_softc *sc = device_lookup_private(&gtmpsc_cd, GTMPSCUNIT(dev));
700 struct tty *tp = sc->gtmpsc_tty;
702 return (*tp->t_linesw->l_read)(tp, uio, flag);
706 gtmpscwrite(dev_t dev, struct uio *uio, int flag)
708 struct gtmpsc_softc *sc = device_lookup_private(&gtmpsc_cd, GTMPSCUNIT(dev));
709 struct tty *tp = sc->gtmpsc_tty;
711 return (*tp->t_linesw->l_write)(tp, uio, flag);
715 gtmpscpoll(dev_t dev, int events, struct lwp *l)
717 struct gtmpsc_softc *sc = device_lookup_private(&gtmpsc_cd, GTMPSCUNIT(dev));
718 struct tty *tp = sc->gtmpsc_tty;
720 return ((*tp->t_linesw->l_poll)(tp, events, l));
724 gtmpscioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
726 struct gtmpsc_softc *sc = device_lookup_private(&gtmpsc_cd, GTMPSCUNIT(dev));
727 struct tty *tp = sc->gtmpsc_tty;
728 int error;
730 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) >= 0)
731 return error;
732 if ((error = ttioctl(tp, cmd, data, flag, l)) >= 0)
733 return error;
734 return ENOTTY;
737 struct tty *
738 gtmpsctty(dev_t dev)
740 struct gtmpsc_softc *sc = device_lookup_private(&gtmpsc_cd, GTMPSCUNIT(dev));
742 return sc->gtmpsc_tty;
745 void
746 gtmpscstop(struct tty *tp, int flag)
750 STATIC void
751 gtmpscstart(struct tty *tp)
753 struct gtmpsc_softc *sc;
754 unsigned char *tba;
755 unsigned int unit;
756 int s, s2, tbc;
758 unit = GTMPSCUNIT(tp->t_dev);
759 sc = device_lookup_private(&gtmpsc_cd, unit);
760 if (sc == NULL)
761 return;
763 s = spltty();
764 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
765 goto out;
766 if (sc->sc_tx_stopped)
767 goto out;
768 if (!ttypull(tp))
769 goto out;
771 /* Grab the first contiguous region of buffer space. */
772 tba = tp->t_outq.c_cf;
773 tbc = ndqb(&tp->t_outq, 0);
775 s2 = splserial();
777 sc->sc_tba = tba;
778 sc->sc_tbc = tbc;
779 sc->cnt_tx_from_ldisc += tbc;
780 SDMA_IMASK_ENABLE(sc, SDMA_INTR_TXBUF(unit));
781 tp->t_state |= TS_BUSY;
782 sc->sc_tx_busy = 1;
783 gtmpsc_common_putn(sc);
785 splx(s2);
786 out:
787 splx(s);
790 STATIC int
791 gtmpscparam(struct tty *tp, struct termios *t)
793 struct gtmpsc_softc *sc = device_lookup_private(&gtmpsc_cd, GTMPSCUNIT(tp->t_dev));
794 int ospeed = compute_cdv(t->c_ospeed);
795 int s;
797 /* Check requested parameters. */
798 if (ospeed < 0)
799 return (EINVAL);
800 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
801 return (EINVAL);
804 * If there were no changes, don't do anything. This avoids dropping
805 * input and improves performance when all we did was frob things like
806 * VMIN and VTIME.
808 if (tp->t_ospeed == t->c_ospeed &&
809 tp->t_cflag == t->c_cflag)
810 return (0);
812 s = splserial();
814 sc->gtmpsc_brg_bcr = BRG_BCR_EN | GT_MPSC_CLOCK_SOURCE | ospeed;
815 sc->gtmpsc_chr3 = GTMPSC_MAXIDLE(t->c_ospeed);
817 /* And copy to tty. */
818 tp->t_ispeed = 0;
819 tp->t_ospeed = t->c_ospeed;
820 tp->t_cflag = t->c_cflag;
822 if (!sc->sc_heldchange) {
823 if (sc->sc_tx_busy) {
824 sc->sc_heldtbc = sc->sc_tbc;
825 sc->sc_tbc = 0;
826 sc->sc_heldchange = 1;
827 } else
828 gtmpsc_loadchannelregs(sc);
831 splx(s);
833 /* Fake carrier on */
834 (void) (*tp->t_linesw->l_modem)(tp, 1);
836 return 0;
839 STATIC int
840 gtmpsc_probe(void)
842 return 1; /* XXX */
845 STATIC unsigned int
846 gtmpsc_get_causes(void)
848 int i;
849 struct gtmpsc_softc *sc;
850 unsigned int cause = 0;
851 static unsigned int bits[4] = {
852 SDMA_INTR_RXBUF(0),
853 SDMA_INTR_TXBUF(0),
854 SDMA_INTR_RXBUF(1),
855 SDMA_INTR_TXBUF(1),
857 sdma_desc_t *desc_addr[4];
858 static unsigned int fake_once = SDMA_INTR_RXBUF(0)
859 | SDMA_INTR_RXBUF(1);
861 desc_addr[0] = 0;
862 desc_addr[1] = 0;
863 desc_addr[2] = 0;
864 desc_addr[3] = 0;
865 sc = device_lookup_private(&gtmpsc_cd, 0);
866 if (sc != 0) {
867 if (sdma_imask & SDMA_INTR_RXBUF(0)) {
868 desc_addr[0] =
869 &sc->gtmpsc_poll_sdmapage->rx[sc->gtmpsc_poll_rxix].rxdesc;
870 GTMPSC_CACHE_INVALIDATE(desc_addr[0]);
871 __asm volatile ("dcbt 0,%0" :: "r"(desc_addr[0]));
873 if (sdma_imask & SDMA_INTR_TXBUF(0)) {
874 desc_addr[1] =
875 &sc->gtmpsc_poll_sdmapage->tx[sc->gtmpsc_poll_txix].txdesc;
876 GTMPSC_CACHE_INVALIDATE(desc_addr[1]);
877 __asm volatile ("dcbt 0,%0" :: "r"(desc_addr[1]));
880 sc = device_lookup_private(&gtmpsc_cd, 1);
881 if (sc != 0) {
882 if (sdma_imask & SDMA_INTR_RXBUF(1)) {
883 desc_addr[2] =
884 &sc->gtmpsc_poll_sdmapage->rx[sc->gtmpsc_poll_rxix].rxdesc;
885 GTMPSC_CACHE_INVALIDATE(desc_addr[2]);
886 __asm volatile ("dcbt 0,%0" :: "r"(desc_addr[2]));
888 if (sdma_imask & SDMA_INTR_TXBUF(1)) {
889 desc_addr[3] =
890 &sc->gtmpsc_poll_sdmapage->tx[sc->gtmpsc_poll_txix].txdesc;
891 GTMPSC_CACHE_INVALIDATE(desc_addr[3]);
892 __asm volatile ("dcbt 0,%0" :: "r"(desc_addr[3]));
896 for (i = 0; i < 4; ++i)
897 if ((sdma_imask & bits[i]) && desc_addr[i] != 0 &&
898 (desc_addr[i]->sdma_csr & SDMA_CSR_TX_OWN) == 0)
899 cause |= bits[i];
900 if (fake_once & sdma_imask) {
901 cause |= fake_once & sdma_imask;
902 /* fake_once &= ~(cause & fake_once); */
904 return cause;
907 STATIC int
908 gtmpsc_intr(void *arg)
910 struct gtmpsc_softc *sc;
911 unsigned int unit;
912 int spurious = 1;
913 unsigned int r;
914 unsigned int cause=0;
916 if (gt_reva_gtmpsc_bug)
917 cause = gtmpsc_get_causes();
919 #ifdef KGDB
920 if (kgdb_break_immediate) {
921 unit = comkgdbport;
922 sc = device_lookup_private(&gtmpsc_cd, unit);
923 if (sc == 0 || (sc->gtmpsc_flags & GTMPSCF_KGDB) == 0)
924 goto skip_kgdb;
925 if (gt_reva_gtmpsc_bug)
926 r = cause & sdma_imask;
927 else {
928 r = GT_READ(sc, SDMA_ICAUSE);
929 r &= GT_READ(sc, SDMA_IMASK);
931 r &= SDMA_INTR_RXBUF(unit);
932 if (r == 0)
933 goto skip_kgdb;
934 GT_WRITE(sc, SDMA_ICAUSE, ~r);
935 spurious = 0;
936 gtmpsc_kgdb_poll(sc);
938 skip_kgdb:
939 #endif
940 for (unit = 0; unit < GTMPSC_NCHAN; ++unit) {
941 sc = device_lookup_private(&gtmpsc_cd, unit);
942 if (sc == NULL)
943 continue;
944 if (gt_reva_gtmpsc_bug)
945 r = cause & sdma_imask;
946 else {
947 r = GT_READ(sc, SDMA_ICAUSE);
948 r &= GT_READ(sc, SDMA_IMASK);
950 r &= SDMA_U_INTR_MASK(unit);
951 if (r == 0)
952 continue;
953 GT_WRITE(sc, SDMA_ICAUSE, ~r);
954 spurious = 0;
955 if (r & SDMA_INTR_RXBUF(unit)) {
956 #ifdef KGDB
957 if (sc->gtmpsc_flags & GTMPSCF_KGDB)
958 gtmpsc_kgdb_poll(sc);
959 else
960 #endif
961 gtmpsc_poll(sc);
963 if (r & SDMA_INTR_TXBUF(unit)) {
965 * If we've delayed a parameter change, do it now,
966 * and restart output.
968 if (sc->sc_heldchange) {
969 gtmpsc_loadchannelregs(sc);
970 sc->sc_heldchange = 0;
971 sc->sc_tbc = sc->sc_heldtbc;
972 sc->sc_heldtbc = 0;
975 /* Output the next chunk of the contiguous buffer,
976 if any. */
977 if (sc->sc_tbc > 0)
978 gtmpsc_common_putn(sc);
979 if (sc->sc_tbc == 0 && sc->sc_tx_busy) {
980 sc->sc_tx_busy = 0;
981 sc->sc_tx_done = 1;
982 softint_schedule(sc->sc_si);
983 SDMA_IMASK_DISABLE(sc, SDMA_INTR_TXBUF(unit));
987 return 1;
988 /* return !spurious; */
991 STATIC void
992 gtmpsc_softintr(void *arg)
994 struct gtmpsc_softc *sc = arg;
995 struct tty *tp;
996 int (*rint)(int, struct tty *);
997 int jobs;
998 int s;
1000 tp = sc->gtmpsc_tty;
1001 rint = tp->t_linesw->l_rint;
1002 do {
1003 jobs = 0;
1004 if (sc->gtmpsc_rxfifo_navail < GTMPSC_RXFIFOSZ) {
1005 s = spltty();
1006 rint(sc->gtmpsc_rxfifo[sc->gtmpsc_rxfifo_getix++],
1007 tp);
1008 if (sc->gtmpsc_rxfifo_getix >= GTMPSC_RXFIFOSZ)
1009 sc->gtmpsc_rxfifo_getix = 0;
1010 ++sc->cnt_rx_from_fifo;
1011 /* atomic_add() returns the previous value */
1012 jobs += atomic_add(&sc->gtmpsc_rxfifo_navail, 1) + 1
1013 < GTMPSC_RXFIFOSZ;
1014 splx(s);
1016 if (sc->sc_tx_done) {
1017 ++jobs;
1018 sc->sc_tx_done = 0;
1019 s = spltty();
1020 tp->t_state &= ~TS_BUSY;
1021 if ((tp->t_state & TS_FLUSH) != 0)
1022 tp->t_state &= ~TS_FLUSH;
1023 else
1024 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1025 (*tp->t_linesw->l_start)(tp);
1026 splx(s);
1028 } while (jobs);
1032 * Console support functions
1034 void
1035 gtmpsccnprobe(struct consdev *cd)
1037 int maj;
1038 /* {extern void return_to_dink(int); return_to_dink(gtbase);} */
1040 if (!gtmpsc_probe())
1041 return;
1043 maj = cdevsw_lookup_major(&gtmpsc_cdevsw);
1044 cd->cn_dev = makedev(maj, 0);
1045 cd->cn_pri = CN_INTERNAL;
1049 * gtmpsc_hackinit - hacks required to supprt GTMPSC console
1051 STATIC void
1052 gtmpsc_hackinit(struct gtmpsc_softc *sc, bus_space_tag_t memt,
1053 bus_space_handle_t memh, int unit)
1055 gtmpsc_poll_sdma_t *vmps;
1056 gtmpsc_poll_sdma_t *pmps;
1058 DPRINTF(("hackinit\n"));
1060 memset(sc, 0, sizeof(struct gtmpsc_softc));
1061 sc->gtmpsc_memt = memt;
1062 sc->gtmpsc_memh = memh;
1063 sc->gtmpsc_unit = unit;
1064 gtmpsc_scp[sc->gtmpsc_unit] = sc;
1066 vmps = (gtmpsc_poll_sdma_t *)gtmpsc_fake_dmapage; /* KVA */
1067 pmps = (gtmpsc_poll_sdma_t *)gtmpsc_fake_dmapage; /* PA */
1069 gtmpsc_txdesc_init(vmps, pmps);
1070 gtmpsc_rxdesc_init(vmps, pmps);
1072 sc->gtmpsc_poll_sdmapage = vmps;
1076 * gtmpsc_txflush - wait for output to drain
1078 STATIC void
1079 gtmpsc_txflush(gtmpsc_softc_t *sc)
1081 unsigned int csr;
1082 unsigned int *csrp;
1083 gtmpsc_polltx_t *vtxp;
1084 int limit = 4000000; /* 4 seconds */
1085 int ix;
1087 ix = sc->gtmpsc_poll_txix - 1;
1088 if (ix < 0)
1089 ix = GTMPSC_NTXDESC - 1;
1091 vtxp = &sc->gtmpsc_poll_sdmapage->tx[ix];
1092 csrp = &vtxp->txdesc.sdma_csr;
1093 while (limit > 0) {
1094 GTMPSC_CACHE_INVALIDATE(csrp);
1095 csr = desc_read(csrp);
1096 if ((csr & SDMA_CSR_TX_OWN) == 0)
1097 break;
1098 DELAY(GTMPSC_POLL_DELAY);
1099 limit -= GTMPSC_POLL_DELAY;
1103 STATIC void
1104 gtmpsc_iflush(gtmpsc_softc_t *sc)
1106 int timo;
1107 char c;
1108 int stat;
1110 for (timo = 50000; timo; timo--)
1111 if (gtmpsc_common_pollc(sc->gtmpsc_unit, &c, &stat) == 0)
1112 return;
1113 #ifdef DIAGNOSTIC
1114 printf("%s: gtmpsc_iflush timeout %02x\n", device_xname(&sc->gtmpsc_dev), c);
1115 #endif
1118 STATIC void
1119 gtmpscinit_stop(struct gtmpsc_softc *sc, int once)
1121 unsigned int r;
1122 unsigned int unit = sc->gtmpsc_unit;
1125 * XXX HACK FIXME
1126 * PMON output has not been flushed. give him a chance
1128 #if 1
1129 if (! once)
1130 DELAY(100000); /* XXX */
1131 #endif
1133 DPRINTF(("gtmpscinit_stop: unit 0x%x\n", unit));
1134 if (unit >= GTMPSC_NCHAN) {
1135 PRINTF(("mpscinit: undefined unit %d\n", sc->gtmpsc_unit));
1136 return;
1139 sc->gtmpsc_chr2 = 0; /* Default value of CHR2 */
1142 * stop GTMPSC unit
1144 r = sc->gtmpsc_chr2 | GTMPSC_CHR2_RXABORT|GTMPSC_CHR2_TXABORT;
1145 GT_WRITE(sc, GTMPSC_U_CHRN(unit, 2), r);
1147 DELAY(GTMPSC_RESET_DELAY);
1150 * abort SDMA TX, RX for GTMPSC unit
1152 GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_AR|SDMA_SDCM_AT);
1154 if (once == 0) {
1156 * Determine if this is the buggy GT-64260A case.
1157 * If this is, then most of GTMPSC and SDMA registers
1158 * are unreadable.
1159 * (They always yield -1).
1161 GT_WRITE(sc, SDMA_IMASK, 0);
1162 r = GT_READ(sc, SDMA_IMASK);
1163 gt_reva_gtmpsc_bug = r == ~0;
1164 sdma_imask = 0;
1168 * If Rx is disabled, we don't need to wait around for
1169 * abort completion.
1171 if ((GT_READ(sc, GTMPSC_U_MMCR_LO(unit)) & GTMPSC_MMCR_LO_ER) == 0)
1172 return;
1175 * poll for GTMPSC RX abort completion
1177 if (gt_reva_gtmpsc_bug) {
1178 /* Sync up with the device first */
1179 r = GT_READ(sc, GTMPSC_U_CHRN(unit, 2));
1180 DELAY(GTMPSC_RESET_DELAY);
1181 } else
1182 for (;;) {
1183 r = GT_READ(sc, GTMPSC_U_CHRN(unit, 2));
1184 if (! (r & GTMPSC_CHR2_RXABORT))
1185 break;
1189 * poll for SDMA RX abort completion
1191 for (;;) {
1192 r = GT_READ(sc, SDMA_U_SDCM(unit));
1193 if (! (r & (SDMA_SDCM_AR|SDMA_SDCM_AT)))
1194 break;
1195 DELAY(50);
1200 STATIC void
1201 gtmpscinit_start(struct gtmpsc_softc *sc, int once)
1203 unsigned int r;
1204 unsigned int unit = sc->gtmpsc_unit;
1207 * initialize softc's "current" transfer indicies & counts
1209 sc->gtmpsc_cx = 0;
1210 sc->gtmpsc_nc = 0;
1211 sc->gtmpsc_poll_txix = 0;
1212 sc->gtmpsc_poll_rxix = 0;
1215 * initialize softc's RX softintr FIFO
1217 sc->gtmpsc_rxfifo_putix = 0;
1218 sc->gtmpsc_rxfifo_getix = 0;
1219 sc->gtmpsc_rxfifo_navail = GTMPSC_RXFIFOSZ;
1220 memset(&sc->gtmpsc_rxfifo[0], 0, GTMPSC_RXFIFOSZ);
1223 * set SDMA unit port TX descriptor pointers
1224 * "next" pointer of last descriptor is start of ring
1226 r = desc_read(
1227 &sc->gtmpsc_poll_sdmapage->tx[GTMPSC_NTXDESC-1].txdesc.sdma_next);
1228 GT_WRITE(sc, SDMA_U_SCTDP(unit), r); /* current */
1229 (void)GT_READ(sc, SDMA_U_SCTDP(unit));
1230 GT_WRITE(sc, SDMA_U_SFTDP(unit), r); /* first */
1231 (void)GT_READ(sc, SDMA_U_SFTDP(unit));
1233 * set SDMA unit port RX descriptor pointer
1234 * "next" pointer of last descriptor is start of ring
1236 r = desc_read(
1237 &sc->gtmpsc_poll_sdmapage->rx[GTMPSC_NRXDESC-1].rxdesc.sdma_next);
1238 GT_WRITE(sc, SDMA_U_SCRDP(unit), r); /* current */
1241 * initialize SDMA unit Configuration Register
1243 r = SDMA_SDC_BSZ_8x64|SDMA_SDC_SFM|SDMA_SDC_RFT;
1244 GT_WRITE(sc, SDMA_U_SDC(unit), r);
1247 * enable SDMA receive
1249 GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_ERD);
1251 if (once == 0) {
1253 * GTMPSC Routing:
1254 * MR0 --> Serial Port 0
1255 * MR1 --> Serial Port 1
1257 GT_WRITE(sc, GTMPSC_MRR, GTMPSC_MRR_RES);
1260 * RX and TX Clock Routing:
1261 * CRR0 --> BRG0
1262 * CRR1 --> BRG1
1264 r = GTMPSC_CRR_BRG0 | (GTMPSC_CRR_BRG1 << GTMPSC_CRR1_SHIFT);
1265 GT_WRITE(sc, GTMPSC_RCRR, r);
1266 GT_WRITE(sc, GTMPSC_TCRR, r);
1268 sc->gtmpsc_brg_bcr = BRG_BCR_EN | GT_MPSC_CLOCK_SOURCE |
1269 compute_cdv(GT_MPSC_DEFAULT_BAUD_RATE);
1270 sc->gtmpsc_chr3 = GTMPSC_MAXIDLE(GT_MPSC_DEFAULT_BAUD_RATE);
1271 gtmpsc_loadchannelregs(sc);
1274 * set MPSC Protocol configuration register for GTMPSC unit
1276 GT_WRITE(sc, GTMPSC_U_MPCR(unit), GTMPSC_MPCR_CL_8);
1279 * set MPSC LO and HI port config registers for GTMPSC unit
1281 r = GTMPSC_MMCR_LO_MODE_UART
1282 |GTMPSC_MMCR_LO_ET
1283 |GTMPSC_MMCR_LO_ER
1284 |GTMPSC_MMCR_LO_NLM;
1285 GT_WRITE(sc, GTMPSC_U_MMCR_LO(unit), r);
1288 GTMPSC_MMCR_HI_TCDV_DEFAULT
1289 |GTMPSC_MMCR_HI_RDW
1290 |GTMPSC_MMCR_HI_RCDV_DEFAULT;
1291 GT_WRITE(sc, GTMPSC_U_MMCR_HI(unit), r);
1294 * tell MPSC receive the Enter Hunt
1296 r = sc->gtmpsc_chr2 | GTMPSC_CHR2_EH;
1297 GT_WRITE(sc, GTMPSC_U_CHRN(unit, 2), r);
1300 * clear any pending SDMA interrupts for this unit
1302 r = GT_READ(sc, SDMA_ICAUSE);
1303 r &= ~SDMA_U_INTR_MASK(unit);
1304 GT_WRITE(sc, SDMA_ICAUSE, r); /* ??? */
1306 DPRINTF(("gtmpscinit: OK\n"));
1310 * gtmpscinit - prepare MPSC for operation
1312 * assumes we are called at ipl >= IPL_SERIAL
1314 STATIC void
1315 gtmpscinit(struct gtmpsc_softc *sc)
1317 static int once = 0;
1319 gtmpscinit_stop(sc, once);
1320 gtmpscinit_start(sc, once);
1321 once = 1;
1325 * gtmpsccninit - initialize the driver and the mpsc device
1327 void
1328 gtmpsccninit(struct consdev *cd)
1333 gtmpsccngetc(dev_t dev)
1335 unsigned int unit = 0;
1336 int c;
1338 unit = GTMPSCUNIT(dev);
1339 if (major(dev) != 0) {
1340 struct gtmpsc_softc *sc = device_lookup_private(&gtmpsc_cd, unit);
1341 if (sc == NULL)
1342 return 0;
1343 unit = sc->gtmpsc_unit;
1345 if (unit >= GTMPSC_NCHAN)
1346 return 0;
1347 c = gtmpsc_common_getc(unit);
1349 return c;
1352 void
1353 gtmpsccnputc(dev_t dev, int c)
1355 unsigned int unit = 0;
1356 char ch = c;
1357 static int ix = 0;
1359 if (gtmpsccninit_done == 0) {
1360 if ((minor(dev) == 0) && (ix < sizeof(gtmpsc_earlybuf)))
1361 gtmpsc_earlybuf[ix++] = (unsigned char)c;
1362 return;
1365 unit = GTMPSCUNIT(dev);
1366 if (major(dev) != 0) {
1367 struct gtmpsc_softc *sc = device_lookup_private(&gtmpsc_cd, unit);
1368 if (sc == NULL)
1369 return;
1370 unit = sc->gtmpsc_unit;
1373 if (unit >= GTMPSC_NCHAN)
1374 return;
1376 gtmpsc_common_putc(unit, ch);
1379 void
1380 gtmpsccnpollc(dev_t dev, int on)
1385 gtmpsccnattach(bus_space_tag_t memt, bus_space_handle_t memh, int unit,
1386 int speed, tcflag_t tcflag)
1388 struct gtmpsc_softc *sc = &gtmpsc_fake_softc;
1389 unsigned char *cp;
1390 unsigned char c;
1391 unsigned int i;
1393 if (gtmpsccninit_done)
1394 return 0;
1396 DPRINTF(("gtmpsccnattach\n"));
1397 gtmpsc_hackinit(sc, memt, memh, unit);
1398 DPRINTF(("gtmpscinit\n"));
1399 gtmpscinit(sc);
1400 gtmpsccninit_done = 1;
1401 cp = gtmpsc_earlybuf;
1402 strcpy(gtmpsc_earlybuf, "\r\nMPSC Lives!\r\n");
1403 for (i=0; i < sizeof(gtmpsc_earlybuf); i++) {
1404 c = *cp++;
1405 if (c == 0)
1406 break;
1407 gtmpsc_common_putc(unit, c);
1409 DPRINTF(("switching cn_tab\n"));
1410 gtmpsc_consdev.cn_dev = makedev(0, unit);
1411 cn_tab = &gtmpsc_consdev;
1412 DPRINTF(("switched cn_tab!\n"));
1413 return 0;
1417 * gtmpsc_common_pollc - non-blocking console read
1419 * if there is an RX char, return it in *cp
1420 * set *statp if Break detected
1422 * assumes we are called at ipl >= IPL_SERIAL
1424 * return 1 if there is RX data
1425 * otherwise return 0
1427 STATIC int
1428 gtmpsc_common_pollc(unsigned int unit, char *cp, int *statp)
1430 struct gtmpsc_softc *sc = gtmpsc_scp[unit];
1431 gtmpsc_pollrx_t *vrxp;
1432 unsigned int ix;
1433 unsigned int cx;
1434 unsigned int nc;
1436 *statp = GTMPSC_STAT_NONE;
1437 ix = sc->gtmpsc_poll_rxix;
1438 nc = sc->gtmpsc_nc;
1439 cx = sc->gtmpsc_cx;
1440 if (nc == 0) {
1441 unsigned int *csrp;
1442 unsigned int csr;
1444 vrxp = &sc->gtmpsc_poll_sdmapage->rx[ix];
1445 csrp = &vrxp->rxdesc.sdma_csr;
1446 cx = 0;
1448 GTMPSC_CACHE_INVALIDATE(csrp);
1449 csr = desc_read(csrp);
1450 if (csr & SDMA_CSR_RX_OWN)
1451 return 0;
1452 if (csr & SDMA_CSR_RX_BR)
1453 *statp = GTMPSC_STAT_BREAK;
1454 if (csr & SDMA_CSR_RX_ES)
1455 PRINTF(("mpsc 0 RX error, rxdesc csr 0x%x\n", csr));
1457 nc = desc_read(&vrxp->rxdesc.sdma_cnt);
1458 nc &= SDMA_RX_CNT_BCNT_MASK;
1459 csr = SDMA_CSR_RX_L|SDMA_CSR_RX_F|SDMA_CSR_RX_OWN
1460 |SDMA_CSR_RX_EI;
1461 if (nc == 0) {
1462 if ((++ix) >= GTMPSC_NRXDESC)
1463 ix = 0;
1464 sc->gtmpsc_poll_rxix = ix;
1465 desc_write(csrp, csr);
1466 GTMPSC_CACHE_FLUSH(csrp);
1467 return 0;
1469 memcpy(sc->gtmpsc_rxbuf, vrxp->rxbuf, nc);
1470 desc_write(csrp, csr);
1471 GTMPSC_CACHE_FLUSH(csrp);
1473 gtmpsc_poll_pollc_cnt++;
1474 nc--;
1475 *cp = sc->gtmpsc_rxbuf[cx++];
1476 if (nc == 0) {
1477 if ((++ix) >= GTMPSC_NRXDESC)
1478 ix = 0;
1479 sc->gtmpsc_poll_rxix = ix;
1481 sc->gtmpsc_cx = cx;
1482 sc->gtmpsc_nc = nc;
1483 return 1;
1487 * gtmpsc_common_getc - polled console read
1489 * We copy data from the DMA buffers into a buffer in the softc
1490 * to reduce descriptor ownership turnaround time
1491 * MPSC can crater if it wraps descriptor rings,
1492 * which is asynchronous and throttled only by line speed.
1494 * This code assumes the buffer PA==KVA
1495 * and assumes we are called at ipl >= IPL_SERIAL
1497 STATIC int
1498 gtmpsc_common_getc(unsigned int unit)
1500 struct gtmpsc_softc *sc = gtmpsc_scp[unit];
1501 gtmpsc_pollrx_t *vrxp;
1502 unsigned int ix;
1503 unsigned int cx;
1504 unsigned int nc;
1505 unsigned int wdog_interval;
1506 int c;
1508 ix = sc->gtmpsc_poll_rxix;
1509 nc = sc->gtmpsc_nc;
1510 cx = sc->gtmpsc_cx;
1511 wdog_interval = 0;
1512 while (nc == 0) {
1513 unsigned int *csrp;
1514 unsigned int csr;
1515 unsigned int r;
1517 vrxp = &sc->gtmpsc_poll_sdmapage->rx[ix];
1518 csrp = &vrxp->rxdesc.sdma_csr;
1519 cx = 0;
1521 GTMPSC_CACHE_INVALIDATE(csrp);
1522 csr = desc_read(csrp);
1523 if (csr & SDMA_CSR_RX_OWN) {
1524 r = sc->gtmpsc_chr2 | GTMPSC_CHR2_CRD;
1525 GT_WRITE(sc, GTMPSC_U_CHRN(unit, 2), r);
1526 do {
1527 if (wdog_interval++ % 32)
1528 gt_watchdog_service();
1529 gtmpsc_poll_getc_miss++;
1530 GTMPSC_CACHE_INVALIDATE(csrp);
1531 DELAY(50);
1532 csr = desc_read(csrp);
1533 } while (csr & SDMA_CSR_RX_OWN);
1534 } else
1535 if (wdog_interval++ % 32)
1536 gt_watchdog_service();
1537 if (csr & SDMA_CSR_RX_ES)
1538 PRINTF(("mpsc 0 RX error, rxdesc csr 0x%x\n", csr));
1540 nc = desc_read(&vrxp->rxdesc.sdma_cnt);
1541 nc &= SDMA_RX_CNT_BCNT_MASK;
1542 if (nc) {
1543 memcpy(sc->gtmpsc_rxbuf, vrxp->rxbuf, nc);
1544 } else {
1545 if ((++ix) >= GTMPSC_NRXDESC)
1546 ix = 0;
1547 sc->gtmpsc_poll_rxix = ix;
1549 csr = SDMA_CSR_RX_L|SDMA_CSR_RX_F|SDMA_CSR_RX_OWN
1550 |SDMA_CSR_RX_EI;
1551 desc_write(csrp, csr);
1552 GTMPSC_CACHE_FLUSH(csrp);
1553 #ifdef KGDB
1554 if (unit == comkgdbport && gt_reva_gtmpsc_bug)
1555 GT_WRITE(sc, SDMA_ICAUSE, ~SDMA_INTR_RXBUF(unit));
1556 #endif
1558 gtmpsc_poll_getc_cnt++;
1559 nc--;
1560 c = (int)sc->gtmpsc_rxbuf[cx++];
1561 if (nc == 0) {
1562 if ((++ix) >= GTMPSC_NRXDESC)
1563 ix = 0;
1564 sc->gtmpsc_poll_rxix = ix;
1566 sc->gtmpsc_cx = cx;
1567 sc->gtmpsc_nc = nc;
1568 gt_watchdog_service();
1569 return c;
1573 * gtmpsc_common_putn - write a buffer into the hardware
1575 * assumes we are called at ipl >= IPL_SERIAL
1577 STATIC void
1578 gtmpsc_common_putn(struct gtmpsc_softc *sc)
1581 int unit = sc->gtmpsc_unit;
1582 int n;
1583 int kick;
1584 gtmpsc_polltx_t *vtxp;
1585 unsigned int *csrp;
1586 unsigned int csr;
1587 unsigned int ix;
1588 unsigned int sdcm;
1590 kick = 0;
1591 for (ix = sc->gtmpsc_poll_txix; sc->sc_tbc;) {
1592 vtxp = &sc->gtmpsc_poll_sdmapage->tx[ix];
1593 csrp = &vtxp->txdesc.sdma_csr;
1594 GTMPSC_CACHE_INVALIDATE(csrp);
1595 csr = desc_read(csrp);
1596 if ((csr & SDMA_CSR_TX_OWN) != 0)
1597 break;
1598 n = sc->sc_tbc;
1599 if (n > GTMPSC_TXBUFSZ)
1600 n = GTMPSC_TXBUFSZ;
1601 memcpy(vtxp->txbuf, sc->sc_tba, n);
1602 csr = SDMA_CSR_TX_L | SDMA_CSR_TX_F
1603 | SDMA_CSR_TX_EI | SDMA_CSR_TX_OWN;
1604 desc_write(&vtxp->txdesc.sdma_cnt,
1605 (n << SDMA_TX_CNT_BCNT_SHIFT) | n);
1606 desc_write(csrp, csr);
1607 GTMPSC_CACHE_FLUSH(csrp);
1608 sc->sc_tbc -= n;
1609 sc->sc_tba += n;
1610 gtmpsc_poll_putn_cnt += n;
1611 sc->cnt_tx_to_sdma += n;
1612 kick = 1;
1614 if (++ix >= GTMPSC_NTXDESC)
1615 ix = 0;
1617 if (kick) {
1618 sc->gtmpsc_poll_txix = ix;
1621 * now kick some SDMA
1623 sdcm = GT_READ(sc, SDMA_U_SDCM(unit));
1625 if ((sdcm & SDMA_SDCM_TXD) == 0) {
1626 GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_TXD);
1632 * gtmpsc_common_putc - polled console putc
1634 * assumes we are called at ipl >= IPL_SERIAL
1636 STATIC void
1637 gtmpsc_common_putc(unsigned int unit, unsigned char c)
1639 struct gtmpsc_softc *sc = gtmpsc_scp[unit];
1640 gtmpsc_polltx_t *vtxp;
1641 unsigned int *csrp;
1642 unsigned int csr;
1643 unsigned int ix;
1644 unsigned int nix;
1645 unsigned int wdog_interval = 0;
1647 DPRINTF(("gtmpsc_common_putc(%d, '%c'): cur=%#x, first=%#x", unit, c,
1648 GT_READ(sc, SDMA_U_SCTDP(unit)), /* current */
1649 GT_READ(sc, SDMA_U_SFTDP(unit)))); /* first */
1650 ix = sc->gtmpsc_poll_txix;
1651 nix = ix + 1;
1652 if (nix >= GTMPSC_NTXDESC)
1653 nix = 0;
1654 sc->gtmpsc_poll_txix = nix;
1655 vtxp = &sc->gtmpsc_poll_sdmapage->tx[ix];
1656 csrp = &vtxp->txdesc.sdma_csr;
1659 for (;;) {
1660 GTMPSC_CACHE_INVALIDATE(csrp);
1661 csr = desc_read(csrp);
1662 if ((csr & SDMA_CSR_TX_OWN) == 0)
1663 break;
1664 gtmpsc_poll_putc_miss++;
1665 DELAY(40);
1666 DPRINTF(("."));
1667 if (wdog_interval++ % 32)
1668 gt_watchdog_service();
1670 if (csr & SDMA_CSR_TX_ES)
1671 PRINTF(("mpsc %d TX error, txdesc csr 0x%x\n", unit, csr));
1673 gtmpsc_poll_putc_cnt++;
1674 vtxp->txbuf[0] = c;
1675 csr = SDMA_CSR_TX_L | SDMA_CSR_TX_F | SDMA_CSR_TX_EI | SDMA_CSR_TX_OWN;
1676 desc_write(&vtxp->txdesc.sdma_cnt, (1 << SDMA_TX_CNT_BCNT_SHIFT) | 1);
1677 desc_write(csrp, csr);
1678 GTMPSC_CACHE_FLUSH(csrp);
1681 * now kick some SDMA
1683 GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_TXD);
1684 gt_watchdog_service();
1688 STATIC void
1689 gtmpsc_poll(void *arg)
1691 struct gtmpsc_softc *sc = (struct gtmpsc_softc *)arg;
1692 int kick;
1693 char ch;
1694 int stat;
1695 static struct timeval msg_time = {0,0};
1696 static struct timeval cur_time;
1697 static int fifo_full = 0;
1699 kick = 0;
1700 while (gtmpsc_common_pollc(sc->gtmpsc_unit, &ch, &stat)) {
1701 #ifdef DDB
1702 if (stat)
1703 break;
1704 #endif
1705 ++sc->cnt_rx_from_sdma;
1706 if (sc->gtmpsc_rxfifo_navail != 0) {
1707 sc->gtmpsc_rxfifo[sc->gtmpsc_rxfifo_putix++] = ch;
1708 if (sc->gtmpsc_rxfifo_putix == GTMPSC_RXFIFOSZ)
1709 sc->gtmpsc_rxfifo_putix = 0;
1710 atomic_add(&sc->gtmpsc_rxfifo_navail, -1);
1711 ++sc->cnt_rx_to_fifo;
1712 fifo_full = 0;
1713 kick = 1;
1714 } else {
1715 if (fifo_full == 0) {
1716 fifo_full = 1;
1717 microtime(&cur_time);
1718 if (cur_time.tv_sec - msg_time.tv_sec >= 5) {
1719 /* Only do this once in 5 sec */
1720 msg_time = cur_time;
1721 printf("mpsc%d: input FIFO full, "
1722 "dropping incoming characters\n",
1723 sc->gtmpsc_unit);
1728 #ifdef DDB
1729 if (stat) {
1730 if (cn_tab == &gtmpsc_consdev) {
1731 Debugger();
1734 #endif
1735 if (kick)
1736 softint_schedule(sc->sc_si);
1739 #ifdef KGDB
1740 /* ARGSUSED */
1741 STATIC int
1742 gtmpsc_kgdb_getc(void *arg)
1745 return (gtmpsc_common_getc(comkgdbport));
1748 /* ARGSUSED */
1749 STATIC void
1750 gtmpsc_kgdb_putc(void *arg, int c)
1753 return (gtmpsc_common_putc(comkgdbport, c));
1756 STATIC void
1757 gtmpsc_kgdb_poll(void *arg)
1759 struct gtmpsc_softc *sc = (struct gtmpsc_softc *)arg;
1760 int s;
1761 char c;
1762 int brk;
1764 s = splserial();
1765 if (kgdb_recover == 0) { /* gdb is not currently talking to its agent */
1766 while (gtmpsc_common_pollc(sc->gtmpsc_unit, &c, &brk)) {
1767 if (c == CTRL('c'))
1768 brk = GTMPSC_STAT_BREAK;
1769 if (brk == GTMPSC_STAT_BREAK)
1770 break;
1772 if (brk == GTMPSC_STAT_BREAK) {
1773 if (kgdb_break_immediate)
1774 breakpoint();
1775 else {
1776 printf("connecting to kgdb\n");
1777 kgdb_connect(1);
1781 splx(s);
1784 #endif /* KGDB */
1786 #if 0
1787 void
1788 gtmpsc_printf(const char *fmt, ...)
1790 struct consdev *ocd;
1791 int s;
1792 va_list ap;
1794 s = splserial();
1795 ocd = cn_tab;
1796 cn_tab = &constab[0];
1797 va_start(ap, fmt);
1798 printf(fmt, ap);
1799 va_end(ap);
1800 cn_tab = ocd;
1801 splx(s);
1803 #endif
1805 void
1806 gtmpsc_mem_printf(const char *fmt, ...)
1808 va_list ap;
1809 static unsigned char *p = gtmpsc_print_buf;
1811 if (p >= &gtmpsc_print_buf[GTMPSC_PRINT_BUF_SIZE - 128]) {
1812 memset(gtmpsc_print_buf, 0, GTMPSC_PRINT_BUF_SIZE);
1813 p = gtmpsc_print_buf;
1815 va_start(ap, fmt);
1816 p += vsprintf(p, fmt, ap);
1817 va_end(ap);
1820 void
1821 gtmpsc_shutdownhook(void *arg)
1823 gtmpsc_softc_t *sc = (gtmpsc_softc_t *)arg;
1825 gtmpsc_txflush(sc);
1828 void
1829 gtmpsccnhalt(dev_t dev)
1831 unsigned int unit;
1832 u_int32_t r;
1834 for (unit = 0; unit < GTMPSC_NCHAN; unit++) {
1835 gtmpsc_softc_t *sc = gtmpsc_scp[unit];
1836 if (sc == NULL)
1837 continue;
1840 * flush TX buffers
1842 gtmpsc_txflush(sc);
1845 * stop MPSC unit RX
1847 r = GT_READ(sc, GTMPSC_U_CHRN(unit, 2));
1848 r &= ~GTMPSC_CHR2_EH;
1849 r |= GTMPSC_CHR2_RXABORT;
1850 GT_WRITE(sc, GTMPSC_U_CHRN(unit, 2), r);
1852 DELAY(GTMPSC_RESET_DELAY);
1855 * abort SDMA RX for MPSC unit
1857 GT_WRITE(sc, SDMA_U_SDCM(unit), SDMA_SDCM_AR);
1861 void
1862 gtmpsc_puts(char *str)
1864 char c;
1866 if (str == NULL)
1867 return;
1868 while ((c = *str++) != 0)
1869 gtmpsccnputc(0, c);