1 /* $NetBSD: gtmpsc.c,v 1.36 2009/05/12 12:18:45 cegger Exp $ */
4 * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
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 $");
52 #include <sys/param.h>
54 #include <sys/device.h>
55 #include <sys/kauth.h>
57 #include <sys/systm.h>
59 #include <sys/callout.h>
60 #include <sys/fcntl.h>
63 #include <sys/kernel.h>
67 #include <uvm/uvm_extern.h>
69 #include <powerpc/atomic.h>
72 #include <sys/cpu.h> /* for DELAY */
73 #include <machine/stdarg.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)
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
106 unsigned int gtmpsc_debug
= 0;
108 # define DPRINTF(x) do { if (gtmpsc_debug) gtmpsc_mem_printf x ; } while (0)
110 # define STATIC static
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 *);
138 STATIC
void gtmpsc_kgdb_poll(void *);
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);
150 void gtmpsc_printf(const char *fmt
, ...);
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
= {
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 */
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);
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)
252 gtmpsc_cache_flush(void *p
)
254 __asm
volatile ("eieio; dcbf 0,%0; lwz %0,0(%0); sync;"
259 gtmpsc_cache_invalidate(void *p
)
261 __asm
volatile ("eieio; dcbi 0,%0; sync;" :: "r"(p
));
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 { \
278 GT_WRITE(sc, SDMA_ICAUSE, ~(bit)); \
279 if (gt_reva_gtmpsc_bug) \
282 __r = GT_READ(sc, SDMA_IMASK); \
285 GT_WRITE(sc, SDMA_IMASK, __r); \
286 } while (/*CONSTCOND*/ 0)
288 #define SDMA_IMASK_DISABLE(sc, bit) do { \
290 if (gt_reva_gtmpsc_bug) \
293 __r = GT_READ(sc, SDMA_IMASK); \
296 GT_WRITE(sc, SDMA_IMASK, __r); \
297 } while (/*CONSTCOND*/ 0)
299 static inline unsigned int
300 desc_read(unsigned int *ip
)
304 __asm
volatile ("lwzx %0,0,%1; eieio;"
305 : "=r"(rv
) : "r"(ip
));
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
321 gtmpsc_txdesc_init(gtmpsc_poll_sdma_t
*vmps
, gtmpsc_poll_sdma_t
*pmps
)
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];
332 next_ptxp
= ptxp
+ 1;
333 for (n
= (GTMPSC_NTXDESC
- 1); n
--; ) {
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
);
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
356 gtmpsc_rxdesc_init(gtmpsc_poll_sdma_t
*vmps
, gtmpsc_poll_sdma_t
*pmps
)
360 gtmpsc_pollrx_t
*vrxp
;
361 gtmpsc_pollrx_t
*prxp
;
362 gtmpsc_pollrx_t
*next_prxp
;
363 gtmpsc_pollrx_t
*first_prxp
;
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];
369 next_prxp
= prxp
+ 1;
370 for (n
= (GTMPSC_NRXDESC
- 1); n
--; ) {
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
);
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)
396 compute_cdv(unsigned int baud
)
402 cdv
= (GT_MPSC_FREQUENCY
/ (baud
* GTMPSC_CLOCK_DIVIDER
) + 1) / 2 - 1;
403 if (cdv
> BRG_BCR_CDV_MAX
)
409 gtmpsc_loadchannelregs(struct gtmpsc_softc
*sc
)
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
);
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
, >mpsc_cd
);
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
;
445 DPRINTF(("mpscattach\n"));
447 GT_MPSCFOUND(gt
, ga
);
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
);
460 PRINTF(("mpscattach: bus_dmamem_alloc error 0x%x\n", err
));
464 #ifndef SDMA_COHERENT
465 err
= bus_dmamem_map(sc
->gtmpsc_dmat
, sc
->gtmpsc_dma_segs
, 1, PAGE_SIZE
,
466 &kva
, BUS_DMA_NOWAIT
);
468 err
= bus_dmamem_map(sc
->gtmpsc_dmat
, sc
->gtmpsc_dma_segs
, 1, PAGE_SIZE
,
469 &kva
, BUS_DMA_NOWAIT
|BUS_DMA_NOCACHE
);
472 PRINTF(("mpscattach: bus_dmamem_map error 0x%x\n", err
));
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
);
480 PRINTF(("mpscattach: bus_dmamap_create error 0x%x\n", err
));
485 err
= bus_dmamap_load(sc
->gtmpsc_dmat
, sc
->gtmpsc_dma_map
, kva
,
488 PRINTF(("mpscattach: bus_dmamap_load error 0x%x\n", err
));
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 */
497 printf(" at %p/%p", vmps
, pmps
);
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
;
511 if (gtmpsc_sdma_ih
== NULL
) {
512 gtmpsc_sdma_ih
= intr_establish(IRQ_SDMA
, IST_LEVEL
, IPL_SERIAL
,
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
;
527 if (cn_tab
== >mpsc_consdev
&&
528 cn_tab
->cn_dev
== makedev(0, sc
->gtmpsc_unit
)) {
529 cn_tab
->cn_dev
= makedev(cdevsw_lookup_major(>mpsc_cdevsw
),
530 device_unit(&sc
->gtmpsc_dev
));
534 aprint_normal(" irq %s%s\n",
535 intr_string(IRQ_SDMA
),
536 (gt_reva_gtmpsc_bug
) ? " [Rev A. bug]" : "");
539 aprint_normal_dev(&sc
->gtmpsc_dev
, "console\n");
543 SDMA_IMASK_ENABLE(sc
, SDMA_INTR_RXBUF(sc
->gtmpsc_unit
));
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");
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
));
571 gtmpscshutdown(struct gtmpsc_softc
*sc
)
577 if (sc
->gtmpsc_flags
& GTMPSCF_KGDB
!= 0)
582 /* Fake carrier off */
583 (void) (*tp
->t_linesw
->l_modem
)(tp
, 0);
584 SDMA_IMASK_DISABLE(sc
, SDMA_INTR_RXBUF(sc
->gtmpsc_unit
));
589 gtmpscopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
591 struct gtmpsc_softc
*sc
;
592 int unit
= GTMPSCUNIT(dev
);
598 sc
= device_lookup_private(>mpsc_cd
, unit
);
603 * If this is the kgdb port, no other use is permitted.
605 if (sc
->gtmpsc_flags
& GTMPSCF_KGDB
!= 0)
609 if (kauth_authorize_device_tty(l
->l_cred
, KAUTH_DEVICE_TTY_OPEN
, tp
))
614 if (!(tp
->t_state
& TS_ISOPEN
)) {
619 SDMA_IMASK_ENABLE(sc
, SDMA_INTR_RXBUF(unit
));
623 t
.c_ospeed
= TTYDEF_SPEED
;
625 t
.c_ospeed
= GT_MPSC_DEFAULT_BAUD_RATE
;
627 t
.c_cflag
= TTYDEF_CFLAG
;
628 /* Make sure gtmpscparam() will do something. */
630 (void) gtmpscparam(tp
, &t
);
631 tp
->t_iflag
= TTYDEF_IFLAG
;
632 tp
->t_oflag
= TTYDEF_OFLAG
;
633 tp
->t_lflag
= TTYDEF_LFLAG
;
637 /* Clear the input ring */
638 sc
->gtmpsc_rxfifo_putix
= 0;
639 sc
->gtmpsc_rxfifo_getix
= 0;
640 sc
->gtmpsc_rxfifo_navail
= GTMPSC_RXFIFOSZ
;
645 error
= ttyopen(tp
, GTMPSCDIALOUT(dev
), ISSET(flag
, O_NONBLOCK
));
649 error
= (*tp
->t_linesw
->l_open
)(dev
, tp
);
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.
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(>mpsc_cd
, unit
);
672 struct tty
*tp
= sc
->gtmpsc_tty
;
676 if (!ISSET(tp
->t_state
, TS_ISOPEN
)) {
681 (*tp
->t_linesw
->l_close
)(tp
, flag
);
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.
697 gtmpscread(dev_t dev
, struct uio
*uio
, int flag
)
699 struct gtmpsc_softc
*sc
= device_lookup_private(>mpsc_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(>mpsc_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(>mpsc_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(>mpsc_cd
, GTMPSCUNIT(dev
));
727 struct tty
*tp
= sc
->gtmpsc_tty
;
730 if ((error
= (*tp
->t_linesw
->l_ioctl
)(tp
, cmd
, data
, flag
, l
)) >= 0)
732 if ((error
= ttioctl(tp
, cmd
, data
, flag
, l
)) >= 0)
740 struct gtmpsc_softc
*sc
= device_lookup_private(>mpsc_cd
, GTMPSCUNIT(dev
));
742 return sc
->gtmpsc_tty
;
746 gtmpscstop(struct tty
*tp
, int flag
)
751 gtmpscstart(struct tty
*tp
)
753 struct gtmpsc_softc
*sc
;
758 unit
= GTMPSCUNIT(tp
->t_dev
);
759 sc
= device_lookup_private(>mpsc_cd
, unit
);
764 if (tp
->t_state
& (TS_TIMEOUT
| TS_BUSY
| TS_TTSTOP
))
766 if (sc
->sc_tx_stopped
)
771 /* Grab the first contiguous region of buffer space. */
772 tba
= tp
->t_outq
.c_cf
;
773 tbc
= ndqb(&tp
->t_outq
, 0);
779 sc
->cnt_tx_from_ldisc
+= tbc
;
780 SDMA_IMASK_ENABLE(sc
, SDMA_INTR_TXBUF(unit
));
781 tp
->t_state
|= TS_BUSY
;
783 gtmpsc_common_putn(sc
);
791 gtmpscparam(struct tty
*tp
, struct termios
*t
)
793 struct gtmpsc_softc
*sc
= device_lookup_private(>mpsc_cd
, GTMPSCUNIT(tp
->t_dev
));
794 int ospeed
= compute_cdv(t
->c_ospeed
);
797 /* Check requested parameters. */
800 if (t
->c_ispeed
&& t
->c_ispeed
!= t
->c_ospeed
)
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
808 if (tp
->t_ospeed
== t
->c_ospeed
&&
809 tp
->t_cflag
== t
->c_cflag
)
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. */
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
;
826 sc
->sc_heldchange
= 1;
828 gtmpsc_loadchannelregs(sc
);
833 /* Fake carrier on */
834 (void) (*tp
->t_linesw
->l_modem
)(tp
, 1);
846 gtmpsc_get_causes(void)
849 struct gtmpsc_softc
*sc
;
850 unsigned int cause
= 0;
851 static unsigned int bits
[4] = {
857 sdma_desc_t
*desc_addr
[4];
858 static unsigned int fake_once
= SDMA_INTR_RXBUF(0)
859 | SDMA_INTR_RXBUF(1);
865 sc
= device_lookup_private(>mpsc_cd
, 0);
867 if (sdma_imask
& SDMA_INTR_RXBUF(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)) {
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(>mpsc_cd
, 1);
882 if (sdma_imask
& SDMA_INTR_RXBUF(1)) {
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)) {
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)
900 if (fake_once
& sdma_imask
) {
901 cause
|= fake_once
& sdma_imask
;
902 /* fake_once &= ~(cause & fake_once); */
908 gtmpsc_intr(void *arg
)
910 struct gtmpsc_softc
*sc
;
914 unsigned int cause
=0;
916 if (gt_reva_gtmpsc_bug
)
917 cause
= gtmpsc_get_causes();
920 if (kgdb_break_immediate
) {
922 sc
= device_lookup_private(>mpsc_cd
, unit
);
923 if (sc
== 0 || (sc
->gtmpsc_flags
& GTMPSCF_KGDB
) == 0)
925 if (gt_reva_gtmpsc_bug
)
926 r
= cause
& sdma_imask
;
928 r
= GT_READ(sc
, SDMA_ICAUSE
);
929 r
&= GT_READ(sc
, SDMA_IMASK
);
931 r
&= SDMA_INTR_RXBUF(unit
);
934 GT_WRITE(sc
, SDMA_ICAUSE
, ~r
);
936 gtmpsc_kgdb_poll(sc
);
940 for (unit
= 0; unit
< GTMPSC_NCHAN
; ++unit
) {
941 sc
= device_lookup_private(>mpsc_cd
, unit
);
944 if (gt_reva_gtmpsc_bug
)
945 r
= cause
& sdma_imask
;
947 r
= GT_READ(sc
, SDMA_ICAUSE
);
948 r
&= GT_READ(sc
, SDMA_IMASK
);
950 r
&= SDMA_U_INTR_MASK(unit
);
953 GT_WRITE(sc
, SDMA_ICAUSE
, ~r
);
955 if (r
& SDMA_INTR_RXBUF(unit
)) {
957 if (sc
->gtmpsc_flags
& GTMPSCF_KGDB
)
958 gtmpsc_kgdb_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
;
975 /* Output the next chunk of the contiguous buffer,
978 gtmpsc_common_putn(sc
);
979 if (sc
->sc_tbc
== 0 && sc
->sc_tx_busy
) {
982 softint_schedule(sc
->sc_si
);
983 SDMA_IMASK_DISABLE(sc
, SDMA_INTR_TXBUF(unit
));
988 /* return !spurious; */
992 gtmpsc_softintr(void *arg
)
994 struct gtmpsc_softc
*sc
= arg
;
996 int (*rint
)(int, struct tty
*);
1000 tp
= sc
->gtmpsc_tty
;
1001 rint
= tp
->t_linesw
->l_rint
;
1004 if (sc
->gtmpsc_rxfifo_navail
< GTMPSC_RXFIFOSZ
) {
1006 rint(sc
->gtmpsc_rxfifo
[sc
->gtmpsc_rxfifo_getix
++],
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
1016 if (sc
->sc_tx_done
) {
1020 tp
->t_state
&= ~TS_BUSY
;
1021 if ((tp
->t_state
& TS_FLUSH
) != 0)
1022 tp
->t_state
&= ~TS_FLUSH
;
1024 ndflush(&tp
->t_outq
, (int)(sc
->sc_tba
- tp
->t_outq
.c_cf
));
1025 (*tp
->t_linesw
->l_start
)(tp
);
1032 * Console support functions
1035 gtmpsccnprobe(struct consdev
*cd
)
1038 /* {extern void return_to_dink(int); return_to_dink(gtbase);} */
1040 if (!gtmpsc_probe())
1043 maj
= cdevsw_lookup_major(>mpsc_cdevsw
);
1044 cd
->cn_dev
= makedev(maj
, 0);
1045 cd
->cn_pri
= CN_INTERNAL
;
1049 * gtmpsc_hackinit - hacks required to supprt GTMPSC console
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
1079 gtmpsc_txflush(gtmpsc_softc_t
*sc
)
1083 gtmpsc_polltx_t
*vtxp
;
1084 int limit
= 4000000; /* 4 seconds */
1087 ix
= sc
->gtmpsc_poll_txix
- 1;
1089 ix
= GTMPSC_NTXDESC
- 1;
1091 vtxp
= &sc
->gtmpsc_poll_sdmapage
->tx
[ix
];
1092 csrp
= &vtxp
->txdesc
.sdma_csr
;
1094 GTMPSC_CACHE_INVALIDATE(csrp
);
1095 csr
= desc_read(csrp
);
1096 if ((csr
& SDMA_CSR_TX_OWN
) == 0)
1098 DELAY(GTMPSC_POLL_DELAY
);
1099 limit
-= GTMPSC_POLL_DELAY
;
1104 gtmpsc_iflush(gtmpsc_softc_t
*sc
)
1110 for (timo
= 50000; timo
; timo
--)
1111 if (gtmpsc_common_pollc(sc
->gtmpsc_unit
, &c
, &stat
) == 0)
1114 printf("%s: gtmpsc_iflush timeout %02x\n", device_xname(&sc
->gtmpsc_dev
), c
);
1119 gtmpscinit_stop(struct gtmpsc_softc
*sc
, int once
)
1122 unsigned int unit
= sc
->gtmpsc_unit
;
1126 * PMON output has not been flushed. give him a chance
1130 DELAY(100000); /* XXX */
1133 DPRINTF(("gtmpscinit_stop: unit 0x%x\n", unit
));
1134 if (unit
>= GTMPSC_NCHAN
) {
1135 PRINTF(("mpscinit: undefined unit %d\n", sc
->gtmpsc_unit
));
1139 sc
->gtmpsc_chr2
= 0; /* Default value of CHR2 */
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
);
1156 * Determine if this is the buggy GT-64260A case.
1157 * If this is, then most of GTMPSC and SDMA registers
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;
1168 * If Rx is disabled, we don't need to wait around for
1171 if ((GT_READ(sc
, GTMPSC_U_MMCR_LO(unit
)) & GTMPSC_MMCR_LO_ER
) == 0)
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
);
1183 r
= GT_READ(sc
, GTMPSC_U_CHRN(unit
, 2));
1184 if (! (r
& GTMPSC_CHR2_RXABORT
))
1189 * poll for SDMA RX abort completion
1192 r
= GT_READ(sc
, SDMA_U_SDCM(unit
));
1193 if (! (r
& (SDMA_SDCM_AR
|SDMA_SDCM_AT
)))
1201 gtmpscinit_start(struct gtmpsc_softc
*sc
, int once
)
1204 unsigned int unit
= sc
->gtmpsc_unit
;
1207 * initialize softc's "current" transfer indicies & counts
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
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
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
);
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:
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
1284 |GTMPSC_MMCR_LO_NLM
;
1285 GT_WRITE(sc
, GTMPSC_U_MMCR_LO(unit
), r
);
1288 GTMPSC_MMCR_HI_TCDV_DEFAULT
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
1315 gtmpscinit(struct gtmpsc_softc
*sc
)
1317 static int once
= 0;
1319 gtmpscinit_stop(sc
, once
);
1320 gtmpscinit_start(sc
, once
);
1325 * gtmpsccninit - initialize the driver and the mpsc device
1328 gtmpsccninit(struct consdev
*cd
)
1333 gtmpsccngetc(dev_t dev
)
1335 unsigned int unit
= 0;
1338 unit
= GTMPSCUNIT(dev
);
1339 if (major(dev
) != 0) {
1340 struct gtmpsc_softc
*sc
= device_lookup_private(>mpsc_cd
, unit
);
1343 unit
= sc
->gtmpsc_unit
;
1345 if (unit
>= GTMPSC_NCHAN
)
1347 c
= gtmpsc_common_getc(unit
);
1353 gtmpsccnputc(dev_t dev
, int c
)
1355 unsigned int unit
= 0;
1359 if (gtmpsccninit_done
== 0) {
1360 if ((minor(dev
) == 0) && (ix
< sizeof(gtmpsc_earlybuf
)))
1361 gtmpsc_earlybuf
[ix
++] = (unsigned char)c
;
1365 unit
= GTMPSCUNIT(dev
);
1366 if (major(dev
) != 0) {
1367 struct gtmpsc_softc
*sc
= device_lookup_private(>mpsc_cd
, unit
);
1370 unit
= sc
->gtmpsc_unit
;
1373 if (unit
>= GTMPSC_NCHAN
)
1376 gtmpsc_common_putc(unit
, ch
);
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
= >mpsc_fake_softc
;
1393 if (gtmpsccninit_done
)
1396 DPRINTF(("gtmpsccnattach\n"));
1397 gtmpsc_hackinit(sc
, memt
, memh
, unit
);
1398 DPRINTF(("gtmpscinit\n"));
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
++) {
1407 gtmpsc_common_putc(unit
, c
);
1409 DPRINTF(("switching cn_tab\n"));
1410 gtmpsc_consdev
.cn_dev
= makedev(0, unit
);
1411 cn_tab
= >mpsc_consdev
;
1412 DPRINTF(("switched cn_tab!\n"));
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
1428 gtmpsc_common_pollc(unsigned int unit
, char *cp
, int *statp
)
1430 struct gtmpsc_softc
*sc
= gtmpsc_scp
[unit
];
1431 gtmpsc_pollrx_t
*vrxp
;
1436 *statp
= GTMPSC_STAT_NONE
;
1437 ix
= sc
->gtmpsc_poll_rxix
;
1444 vrxp
= &sc
->gtmpsc_poll_sdmapage
->rx
[ix
];
1445 csrp
= &vrxp
->rxdesc
.sdma_csr
;
1448 GTMPSC_CACHE_INVALIDATE(csrp
);
1449 csr
= desc_read(csrp
);
1450 if (csr
& SDMA_CSR_RX_OWN
)
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
1462 if ((++ix
) >= GTMPSC_NRXDESC
)
1464 sc
->gtmpsc_poll_rxix
= ix
;
1465 desc_write(csrp
, csr
);
1466 GTMPSC_CACHE_FLUSH(csrp
);
1469 memcpy(sc
->gtmpsc_rxbuf
, vrxp
->rxbuf
, nc
);
1470 desc_write(csrp
, csr
);
1471 GTMPSC_CACHE_FLUSH(csrp
);
1473 gtmpsc_poll_pollc_cnt
++;
1475 *cp
= sc
->gtmpsc_rxbuf
[cx
++];
1477 if ((++ix
) >= GTMPSC_NRXDESC
)
1479 sc
->gtmpsc_poll_rxix
= ix
;
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
1498 gtmpsc_common_getc(unsigned int unit
)
1500 struct gtmpsc_softc
*sc
= gtmpsc_scp
[unit
];
1501 gtmpsc_pollrx_t
*vrxp
;
1505 unsigned int wdog_interval
;
1508 ix
= sc
->gtmpsc_poll_rxix
;
1517 vrxp
= &sc
->gtmpsc_poll_sdmapage
->rx
[ix
];
1518 csrp
= &vrxp
->rxdesc
.sdma_csr
;
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
);
1527 if (wdog_interval
++ % 32)
1528 gt_watchdog_service();
1529 gtmpsc_poll_getc_miss
++;
1530 GTMPSC_CACHE_INVALIDATE(csrp
);
1532 csr
= desc_read(csrp
);
1533 } while (csr
& SDMA_CSR_RX_OWN
);
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
;
1543 memcpy(sc
->gtmpsc_rxbuf
, vrxp
->rxbuf
, nc
);
1545 if ((++ix
) >= GTMPSC_NRXDESC
)
1547 sc
->gtmpsc_poll_rxix
= ix
;
1549 csr
= SDMA_CSR_RX_L
|SDMA_CSR_RX_F
|SDMA_CSR_RX_OWN
1551 desc_write(csrp
, csr
);
1552 GTMPSC_CACHE_FLUSH(csrp
);
1554 if (unit
== comkgdbport
&& gt_reva_gtmpsc_bug
)
1555 GT_WRITE(sc
, SDMA_ICAUSE
, ~SDMA_INTR_RXBUF(unit
));
1558 gtmpsc_poll_getc_cnt
++;
1560 c
= (int)sc
->gtmpsc_rxbuf
[cx
++];
1562 if ((++ix
) >= GTMPSC_NRXDESC
)
1564 sc
->gtmpsc_poll_rxix
= ix
;
1568 gt_watchdog_service();
1573 * gtmpsc_common_putn - write a buffer into the hardware
1575 * assumes we are called at ipl >= IPL_SERIAL
1578 gtmpsc_common_putn(struct gtmpsc_softc
*sc
)
1581 int unit
= sc
->gtmpsc_unit
;
1584 gtmpsc_polltx_t
*vtxp
;
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)
1599 if (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
);
1610 gtmpsc_poll_putn_cnt
+= n
;
1611 sc
->cnt_tx_to_sdma
+= n
;
1614 if (++ix
>= GTMPSC_NTXDESC
)
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
1637 gtmpsc_common_putc(unsigned int unit
, unsigned char c
)
1639 struct gtmpsc_softc
*sc
= gtmpsc_scp
[unit
];
1640 gtmpsc_polltx_t
*vtxp
;
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
;
1652 if (nix
>= GTMPSC_NTXDESC
)
1654 sc
->gtmpsc_poll_txix
= nix
;
1655 vtxp
= &sc
->gtmpsc_poll_sdmapage
->tx
[ix
];
1656 csrp
= &vtxp
->txdesc
.sdma_csr
;
1660 GTMPSC_CACHE_INVALIDATE(csrp
);
1661 csr
= desc_read(csrp
);
1662 if ((csr
& SDMA_CSR_TX_OWN
) == 0)
1664 gtmpsc_poll_putc_miss
++;
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
++;
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();
1689 gtmpsc_poll(void *arg
)
1691 struct gtmpsc_softc
*sc
= (struct gtmpsc_softc
*)arg
;
1695 static struct timeval msg_time
= {0,0};
1696 static struct timeval cur_time
;
1697 static int fifo_full
= 0;
1700 while (gtmpsc_common_pollc(sc
->gtmpsc_unit
, &ch
, &stat
)) {
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
;
1715 if (fifo_full
== 0) {
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",
1730 if (cn_tab
== >mpsc_consdev
) {
1736 softint_schedule(sc
->sc_si
);
1742 gtmpsc_kgdb_getc(void *arg
)
1745 return (gtmpsc_common_getc(comkgdbport
));
1750 gtmpsc_kgdb_putc(void *arg
, int c
)
1753 return (gtmpsc_common_putc(comkgdbport
, c
));
1757 gtmpsc_kgdb_poll(void *arg
)
1759 struct gtmpsc_softc
*sc
= (struct gtmpsc_softc
*)arg
;
1765 if (kgdb_recover
== 0) { /* gdb is not currently talking to its agent */
1766 while (gtmpsc_common_pollc(sc
->gtmpsc_unit
, &c
, &brk
)) {
1768 brk
= GTMPSC_STAT_BREAK
;
1769 if (brk
== GTMPSC_STAT_BREAK
)
1772 if (brk
== GTMPSC_STAT_BREAK
) {
1773 if (kgdb_break_immediate
)
1776 printf("connecting to kgdb\n");
1788 gtmpsc_printf(const char *fmt
, ...)
1790 struct consdev
*ocd
;
1796 cn_tab
= &constab
[0];
1806 gtmpsc_mem_printf(const char *fmt
, ...)
1809 static unsigned char *p
= gtmpsc_print_buf
;
1811 if (p
>= >mpsc_print_buf
[GTMPSC_PRINT_BUF_SIZE
- 128]) {
1812 memset(gtmpsc_print_buf
, 0, GTMPSC_PRINT_BUF_SIZE
);
1813 p
= gtmpsc_print_buf
;
1816 p
+= vsprintf(p
, fmt
, ap
);
1821 gtmpsc_shutdownhook(void *arg
)
1823 gtmpsc_softc_t
*sc
= (gtmpsc_softc_t
*)arg
;
1829 gtmpsccnhalt(dev_t dev
)
1834 for (unit
= 0; unit
< GTMPSC_NCHAN
; unit
++) {
1835 gtmpsc_softc_t
*sc
= gtmpsc_scp
[unit
];
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
);
1862 gtmpsc_puts(char *str
)
1868 while ((c
= *str
++) != 0)