xref: /netbsd-src/sys/dev/pci/cz.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: cz.c,v 1.38 2006/07/21 16:48:51 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 Zembu Labs, Inc.
5  * All rights reserved.
6  *
7  * Authors: Jason R. Thorpe <thorpej@zembu.com>
8  *          Bill Studenmund <wrstuden@zembu.com>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by Zembu Labs, Inc.
21  * 4. Neither the name of Zembu Labs nor the names of its employees may
22  *    be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
26  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
27  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
28  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 /*
38  * Cyclades-Z series multi-port serial adapter driver for NetBSD.
39  *
40  * Some notes:
41  *
42  *	- The Cyclades-Z has fully automatic hardware (and software!)
43  *	  flow control.  We only use RTS/CTS flow control here,
44  *	  and it is implemented in a very simplistic manner.  This
45  *	  may be an area of future work.
46  *
47  *	- The PLX can map the either the board's RAM or host RAM
48  *	  into the MIPS's memory window.  This would enable us to
49  *	  use less expensive (for us) memory reads/writes to host
50  *	  RAM, rather than time-consuming reads/writes to PCI
51  *	  memory space.  However, the PLX can only map a 0-128M
52  *	  window, so we would have to ensure that the DMA address
53  *	  of the host RAM fits there.  This is kind of a pain,
54  *	  so we just don't bother right now.
55  *
56  *	- In a perfect world, we would use the autoconfiguration
57  *	  mechanism to attach the TTYs that we find.  However,
58  *	  that leads to somewhat icky looking autoconfiguration
59  *	  messages (one for every TTY, up to 64 per board!).  So
60  *	  we don't do it that way, but assign minors as if there
61  *	  were the max of 64 ports per board.
62  *
63  *	- We don't bother with PPS support here.  There are so many
64  *	  ports, each with a large amount of buffer space, that the
65  *	  normal mode of operation is to poll the boards regularly
66  *	  (generally, every 20ms or so).  This makes this driver
67  *	  unsuitable for PPS, as the latency will be generally too
68  *	  high.
69  */
70 /*
71  * This driver inspired by the FreeBSD driver written by Brian J. McGovern
72  * for FreeBSD 3.2.
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: cz.c,v 1.38 2006/07/21 16:48:51 ad Exp $");
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/proc.h>
81 #include <sys/device.h>
82 #include <sys/malloc.h>
83 #include <sys/tty.h>
84 #include <sys/conf.h>
85 #include <sys/time.h>
86 #include <sys/kernel.h>
87 #include <sys/fcntl.h>
88 #include <sys/syslog.h>
89 #include <sys/kauth.h>
90 
91 #include <sys/callout.h>
92 
93 #include <dev/pci/pcireg.h>
94 #include <dev/pci/pcivar.h>
95 #include <dev/pci/pcidevs.h>
96 #include <dev/pci/czreg.h>
97 
98 #include <dev/pci/plx9060reg.h>
99 #include <dev/pci/plx9060var.h>
100 
101 #include <dev/microcode/cyclades-z/cyzfirm.h>
102 
103 #define	CZ_DRIVER_VERSION	0x20000411
104 
105 #define CZ_POLL_MS			20
106 
107 /* These are the interrupts we always use. */
108 #define	CZ_INTERRUPTS							\
109 	(C_IN_MDSR | C_IN_MRI | C_IN_MRTS | C_IN_MCTS | C_IN_TXBEMPTY |	\
110 	 C_IN_TXFEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM | C_IN_RXNNDT |	\
111 	 C_IN_MDCD | C_IN_PR_ERROR | C_IN_FR_ERROR | C_IN_OVR_ERROR |	\
112 	 C_IN_RXOFL | C_IN_IOCTLW | C_IN_RXBRK)
113 
114 /*
115  * cztty_softc:
116  *
117  *	Per-channel (TTY) state.
118  */
119 struct cztty_softc {
120 	struct cz_softc *sc_parent;
121 	struct tty *sc_tty;
122 
123 	struct callout sc_diag_ch;
124 
125 	int sc_channel;			/* Also used to flag unattached chan */
126 #define CZTTY_CHANNEL_DEAD	-1
127 
128 	bus_space_tag_t sc_chan_st;	/* channel space tag */
129 	bus_space_handle_t sc_chan_sh;	/* channel space handle */
130 	bus_space_handle_t sc_buf_sh;	/* buffer space handle */
131 
132 	u_int sc_overflows,
133 	      sc_parity_errors,
134 	      sc_framing_errors,
135 	      sc_errors;
136 
137 	int sc_swflags;
138 
139 	u_int32_t sc_rs_control_dtr,
140 		  sc_chanctl_hw_flow,
141 		  sc_chanctl_comm_baud,
142 		  sc_chanctl_rs_control,
143 		  sc_chanctl_comm_data_l,
144 		  sc_chanctl_comm_parity;
145 };
146 
147 /*
148  * cz_softc:
149  *
150  *	Per-board state.
151  */
152 struct cz_softc {
153 	struct device cz_dev;		/* generic device info */
154 	struct plx9060_config cz_plx;	/* PLX 9060 config info */
155 	bus_space_tag_t cz_win_st;	/* window space tag */
156 	bus_space_handle_t cz_win_sh;	/* window space handle */
157 	struct callout cz_callout;	/* callout for polling-mode */
158 
159 	void *cz_ih;			/* interrupt handle */
160 
161 	u_int32_t cz_mailbox0;		/* our MAILBOX0 value */
162 	int cz_nchannels;		/* number of channels */
163 	int cz_nopenchan;		/* number of open channels */
164 	struct cztty_softc *cz_ports;	/* our array of ports */
165 
166 	bus_addr_t cz_fwctl;		/* offset of firmware control */
167 };
168 
169 static int	cz_wait_pci_doorbell(struct cz_softc *, const char *);
170 
171 static int	cz_load_firmware(struct cz_softc *);
172 
173 static int	cz_intr(void *);
174 static void	cz_poll(void *);
175 static int	cztty_transmit(struct cztty_softc *, struct tty *);
176 static int	cztty_receive(struct cztty_softc *, struct tty *);
177 
178 static struct	cztty_softc *cztty_getttysoftc(dev_t dev);
179 static int	cztty_attached_ttys;
180 static int	cz_timeout_ticks;
181 
182 static void	czttystart(struct tty *tp);
183 static int	czttyparam(struct tty *tp, struct termios *t);
184 static void	cztty_shutdown(struct cztty_softc *sc);
185 static void	cztty_modem(struct cztty_softc *sc, int onoff);
186 static void	cztty_break(struct cztty_softc *sc, int onoff);
187 static void	tiocm_to_cztty(struct cztty_softc *sc, u_long how, int ttybits);
188 static int	cztty_to_tiocm(struct cztty_softc *sc);
189 static void	cztty_diag(void *arg);
190 
191 extern struct cfdriver cz_cd;
192 
193 /*
194  * Macros to read and write the PLX.
195  */
196 #define	CZ_PLX_READ(cz, reg)						\
197 	bus_space_read_4((cz)->cz_plx.plx_st, (cz)->cz_plx.plx_sh, (reg))
198 #define	CZ_PLX_WRITE(cz, reg, val)					\
199 	bus_space_write_4((cz)->cz_plx.plx_st, (cz)->cz_plx.plx_sh,	\
200 	    (reg), (val))
201 
202 /*
203  * Macros to read and write the FPGA.  We must already be in the FPGA
204  * window for this.
205  */
206 #define	CZ_FPGA_READ(cz, reg)						\
207 	bus_space_read_4((cz)->cz_win_st, (cz)->cz_win_sh, (reg))
208 #define	CZ_FPGA_WRITE(cz, reg, val)					\
209 	bus_space_write_4((cz)->cz_win_st, (cz)->cz_win_sh, (reg), (val))
210 
211 /*
212  * Macros to read and write the firmware control structures in board RAM.
213  */
214 #define	CZ_FWCTL_READ(cz, off)						\
215 	bus_space_read_4((cz)->cz_win_st, (cz)->cz_win_sh,		\
216 	    (cz)->cz_fwctl + (off))
217 
218 #define	CZ_FWCTL_WRITE(cz, off, val)					\
219 	bus_space_write_4((cz)->cz_win_st, (cz)->cz_win_sh,		\
220 	    (cz)->cz_fwctl + (off), (val))
221 
222 /*
223  * Convenience macros for cztty routines.  PLX window MUST be to RAM.
224  */
225 #define CZTTY_CHAN_READ(sc, off)					\
226 	bus_space_read_4((sc)->sc_chan_st, (sc)->sc_chan_sh, (off))
227 
228 #define CZTTY_CHAN_WRITE(sc, off, val)					\
229 	bus_space_write_4((sc)->sc_chan_st, (sc)->sc_chan_sh,		\
230 	    (off), (val))
231 
232 #define CZTTY_BUF_READ(sc, off)						\
233 	bus_space_read_4((sc)->sc_chan_st, (sc)->sc_buf_sh, (off))
234 
235 #define CZTTY_BUF_WRITE(sc, off, val)					\
236 	bus_space_write_4((sc)->sc_chan_st, (sc)->sc_buf_sh,		\
237 	    (off), (val))
238 
239 /*
240  * Convenience macros.
241  */
242 #define	CZ_WIN_RAM(cz)							\
243 do {									\
244 	CZ_PLX_WRITE((cz), PLX_LAS0BA, LOCAL_ADDR0_RAM);		\
245 	delay(100);							\
246 } while (0)
247 
248 #define	CZ_WIN_FPGA(cz)							\
249 do {									\
250 	CZ_PLX_WRITE((cz), PLX_LAS0BA, LOCAL_ADDR0_FPGA);		\
251 	delay(100);							\
252 } while (0)
253 
254 /*****************************************************************************
255  * Cyclades-Z controller code starts here...
256  *****************************************************************************/
257 
258 /*
259  * cz_match:
260  *
261  *	Determine if the given PCI device is a Cyclades-Z board.
262  */
263 static int
264 cz_match(struct device *parent,
265     struct cfdata *match,
266     void *aux)
267 {
268 	struct pci_attach_args *pa = aux;
269 
270 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CYCLADES) {
271 		switch (PCI_PRODUCT(pa->pa_id)) {
272 		case PCI_PRODUCT_CYCLADES_CYCLOMZ_2:
273 			return (1);
274 		}
275 	}
276 
277 	return (0);
278 }
279 
280 /*
281  * cz_attach:
282  *
283  *	A Cyclades-Z board was found; attach it.
284  */
285 static void
286 cz_attach(struct device *parent,
287     struct device *self,
288     void *aux)
289 {
290 	extern const struct cdevsw cz_cdevsw;	/* XXX */
291 	struct cz_softc *cz = (void *) self;
292 	struct pci_attach_args *pa = aux;
293 	pci_intr_handle_t ih;
294 	const char *intrstr = NULL;
295 	struct cztty_softc *sc;
296 	struct tty *tp;
297 	int i;
298 
299 	aprint_naive(": Multi-port serial controller\n");
300 	aprint_normal(": Cyclades-Z multiport serial\n");
301 
302 	cz->cz_plx.plx_pc = pa->pa_pc;
303 	cz->cz_plx.plx_tag = pa->pa_tag;
304 
305 	if (pci_mapreg_map(pa, PLX_PCI_RUNTIME_MEMADDR,
306 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
307 	    &cz->cz_plx.plx_st, &cz->cz_plx.plx_sh, NULL, NULL) != 0) {
308 		aprint_error("%s: unable to map PLX registers\n",
309 		    cz->cz_dev.dv_xname);
310 		return;
311 	}
312 	if (pci_mapreg_map(pa, PLX_PCI_LOCAL_ADDR0,
313 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
314 	    &cz->cz_win_st, &cz->cz_win_sh, NULL, NULL) != 0) {
315 		aprint_error("%s: unable to map device window\n",
316 		    cz->cz_dev.dv_xname);
317 		return;
318 	}
319 
320 	cz->cz_mailbox0 = CZ_PLX_READ(cz, PLX_MAILBOX0);
321 	cz->cz_nopenchan = 0;
322 
323 	/*
324 	 * Make sure that the board is completely stopped.
325 	 */
326 	CZ_WIN_FPGA(cz);
327 	CZ_FPGA_WRITE(cz, FPGA_CPU_STOP, 0);
328 
329 	/*
330 	 * Load the board's firmware.
331 	 */
332 	if (cz_load_firmware(cz) != 0)
333 		return;
334 
335 	/*
336 	 * Now that we're ready to roll, map and establish the interrupt
337 	 * handler.
338 	 */
339 	if (pci_intr_map(pa, &ih) != 0) {
340 		/*
341 		 * The common case is for Cyclades-Z boards to run
342 		 * in polling mode, and thus not have an interrupt
343 		 * mapped for them.  Don't bother reporting that
344 		 * the interrupt is not mappable, since this isn't
345 		 * really an error.
346 		 */
347 		cz->cz_ih = NULL;
348 		goto polling_mode;
349 	} else {
350 		intrstr = pci_intr_string(pa->pa_pc, ih);
351 		cz->cz_ih = pci_intr_establish(pa->pa_pc, ih, IPL_TTY,
352 		    cz_intr, cz);
353 	}
354 	if (cz->cz_ih == NULL) {
355 		aprint_error("%s: unable to establish interrupt",
356 		    cz->cz_dev.dv_xname);
357 		if (intrstr != NULL)
358 			aprint_normal(" at %s", intrstr);
359 		aprint_normal("\n");
360 		/* We will fall-back on polling mode. */
361 	} else
362 		aprint_normal("%s: interrupting at %s\n",
363 		    cz->cz_dev.dv_xname, intrstr);
364 
365  polling_mode:
366 	if (cz->cz_ih == NULL) {
367 		callout_init(&cz->cz_callout);
368 		if (cz_timeout_ticks == 0)
369 			cz_timeout_ticks = max(1, hz * CZ_POLL_MS / 1000);
370 		aprint_normal("%s: polling mode, %d ms interval (%d tick%s)\n",
371 		    cz->cz_dev.dv_xname, CZ_POLL_MS, cz_timeout_ticks,
372 		    cz_timeout_ticks == 1 ? "" : "s");
373 	}
374 
375 	/*
376 	 * Allocate sufficient pointers for the children and
377 	 * attach them.  Set all ports to a reasonable initial
378 	 * configuration while we're at it:
379 	 *
380 	 *	disabled
381 	 *	8N1
382 	 *	default baud rate
383 	 *	hardware flow control.
384 	 */
385 	CZ_WIN_RAM(cz);
386 
387 	if (cz->cz_nchannels == 0) {
388 		/* No channels?  No more work to do! */
389 		return;
390 	}
391 
392 	cz->cz_ports = malloc(sizeof(struct cztty_softc) * cz->cz_nchannels,
393 	    M_DEVBUF, M_WAITOK|M_ZERO);
394 	cztty_attached_ttys += cz->cz_nchannels;
395 
396 	for (i = 0; i < cz->cz_nchannels; i++) {
397 		sc = &cz->cz_ports[i];
398 
399 		sc->sc_channel = i;
400 		sc->sc_chan_st = cz->cz_win_st;
401 		sc->sc_parent = cz;
402 
403 		if (bus_space_subregion(cz->cz_win_st, cz->cz_win_sh,
404 		    cz->cz_fwctl + ZFIRM_CHNCTL_OFF(i, 0),
405 		    ZFIRM_CHNCTL_SIZE, &sc->sc_chan_sh)) {
406 			aprint_error(
407 			    "%s: unable to subregion channel %d control\n",
408 			    cz->cz_dev.dv_xname, i);
409 			sc->sc_channel = CZTTY_CHANNEL_DEAD;
410 			continue;
411 		}
412 		if (bus_space_subregion(cz->cz_win_st, cz->cz_win_sh,
413 		    cz->cz_fwctl + ZFIRM_BUFCTL_OFF(i, 0),
414 		    ZFIRM_BUFCTL_SIZE, &sc->sc_buf_sh)) {
415 			aprint_error(
416 			    "%s: unable to subregion channel %d buffer\n",
417 			    cz->cz_dev.dv_xname, i);
418 			sc->sc_channel = CZTTY_CHANNEL_DEAD;
419 			continue;
420 		}
421 
422 		callout_init(&sc->sc_diag_ch);
423 
424 		tp = ttymalloc();
425 		tp->t_dev = makedev(cdevsw_lookup_major(&cz_cdevsw),
426 		    (device_unit(&cz->cz_dev) * ZFIRM_MAX_CHANNELS) + i);
427 		tp->t_oproc = czttystart;
428 		tp->t_param = czttyparam;
429 		tty_attach(tp);
430 
431 		sc->sc_tty = tp;
432 
433 		CZTTY_CHAN_WRITE(sc, CHNCTL_OP_MODE, C_CH_DISABLE);
434 		CZTTY_CHAN_WRITE(sc, CHNCTL_INTR_ENABLE, CZ_INTERRUPTS);
435 		CZTTY_CHAN_WRITE(sc, CHNCTL_SW_FLOW, 0);
436 		CZTTY_CHAN_WRITE(sc, CHNCTL_FLOW_XON, 0x11);
437 		CZTTY_CHAN_WRITE(sc, CHNCTL_FLOW_XOFF, 0x13);
438 		CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_BAUD, TTYDEF_SPEED);
439 		CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_PARITY, C_PR_NONE);
440 		CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_DATA_L, C_DL_CS8 | C_DL_1STOP);
441 		CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_FLAGS, 0);
442 		CZTTY_CHAN_WRITE(sc, CHNCTL_HW_FLOW, C_RS_CTS | C_RS_RTS);
443 		CZTTY_CHAN_WRITE(sc, CHNCTL_RS_CONTROL, 0);
444 	}
445 }
446 
447 CFATTACH_DECL(cz, sizeof(struct cz_softc),
448     cz_match, cz_attach, NULL, NULL);
449 
450 #if 0
451 /*
452  * cz_reset_board:
453  *
454  *	Reset the board via the PLX.
455  */
456 static void
457 cz_reset_board(struct cz_softc *cz)
458 {
459 	u_int32_t reg;
460 
461 	reg = CZ_PLX_READ(cz, PLX_CONTROL);
462 	CZ_PLX_WRITE(cz, PLX_CONTROL, reg | CONTROL_SWR);
463 	delay(1000);
464 
465 	CZ_PLX_WRITE(cz, PLX_CONTROL, reg);
466 	delay(1000);
467 
468 	/* Now reload the PLX from its EEPROM. */
469 	reg = CZ_PLX_READ(cz, PLX_CONTROL);
470 	CZ_PLX_WRITE(cz, PLX_CONTROL, reg | CONTROL_RELOADCFG);
471 	delay(1000);
472 	CZ_PLX_WRITE(cz, PLX_CONTROL, reg);
473 }
474 #endif
475 
476 /*
477  * cz_load_firmware:
478  *
479  *	Load the ZFIRM firmware into the board's RAM and start it
480  *	running.
481  */
482 static int
483 cz_load_firmware(struct cz_softc *cz)
484 {
485 	const struct zfirm_header *zfh;
486 	const struct zfirm_config *zfc;
487 	const struct zfirm_block *zfb, *zblocks;
488 	const u_int8_t *cp;
489 	const char *board;
490 	u_int32_t fid;
491 	int i, j, nconfigs, nblocks, nbytes;
492 
493 	zfh = (const struct zfirm_header *) cycladesz_firmware;
494 
495 	/* Find the config header. */
496 	if (le32toh(zfh->zfh_configoff) & (sizeof(u_int32_t) - 1)) {
497 		aprint_error("%s: bad ZFIRM config offset: 0x%x\n",
498 		    cz->cz_dev.dv_xname, le32toh(zfh->zfh_configoff));
499 		return (EIO);
500 	}
501 	zfc = (const struct zfirm_config *)(cycladesz_firmware +
502 	    le32toh(zfh->zfh_configoff));
503 	nconfigs = le32toh(zfh->zfh_nconfig);
504 
505 	/* Locate the correct configuration for our board. */
506 	for (i = 0; i < nconfigs; i++, zfc++) {
507 		if (le32toh(zfc->zfc_mailbox) == cz->cz_mailbox0 &&
508 		    le32toh(zfc->zfc_function) == ZFC_FUNCTION_NORMAL)
509 			break;
510 	}
511 	if (i == nconfigs) {
512 		aprint_error("%s: unable to locate config header\n",
513 		    cz->cz_dev.dv_xname);
514 		return (EIO);
515 	}
516 
517 	nblocks = le32toh(zfc->zfc_nblocks);
518 	zblocks = (const struct zfirm_block *)(cycladesz_firmware +
519 	    le32toh(zfh->zfh_blockoff));
520 
521 	/*
522 	 * 8Zo ver. 1 doesn't have an FPGA.  Load it on all others if
523 	 * necessary.
524 	 */
525 	if (cz->cz_mailbox0 != MAILBOX0_8Zo_V1
526 #if 0
527 	    && ((CZ_PLX_READ(cz, PLX_CONTROL) & CONTROL_FPGA_LOADED) == 0)
528 #endif
529 								) {
530 #ifdef CZ_DEBUG
531 		aprint_debug("%s: Loading FPGA...", cz->cz_dev.dv_xname);
532 #endif
533 		CZ_WIN_FPGA(cz);
534 		for (i = 0; i < nblocks; i++) {
535 			/* zfb = zblocks + le32toh(zfc->zfc_blocklist[i]) ?? */
536 			zfb = &zblocks[le32toh(zfc->zfc_blocklist[i])];
537 			if (le32toh(zfb->zfb_type) == ZFB_TYPE_FPGA) {
538 				nbytes = le32toh(zfb->zfb_size);
539 				cp = &cycladesz_firmware[
540 				    le32toh(zfb->zfb_fileoff)];
541 				for (j = 0; j < nbytes; j++, cp++) {
542 					bus_space_write_1(cz->cz_win_st,
543 					    cz->cz_win_sh, 0, *cp);
544 					/* FPGA needs 30-100us to settle. */
545 					delay(10);
546 				}
547 			}
548 		}
549 #ifdef CZ_DEBUG
550 		aprint_debug("done\n");
551 #endif
552 	}
553 
554 	/* Now load the firmware. */
555 	CZ_WIN_RAM(cz);
556 
557 	for (i = 0; i < nblocks; i++) {
558 		/* zfb = zblocks + le32toh(zfc->zfc_blocklist[i]) ?? */
559 		zfb = &zblocks[le32toh(zfc->zfc_blocklist[i])];
560 		if (le32toh(zfb->zfb_type) == ZFB_TYPE_FIRMWARE) {
561 			const u_int32_t *lp;
562 			u_int32_t ro = le32toh(zfb->zfb_ramoff);
563 			nbytes = le32toh(zfb->zfb_size);
564 			lp = (const u_int32_t *)
565 			    &cycladesz_firmware[le32toh(zfb->zfb_fileoff)];
566 			for (j = 0; j < nbytes; j += 4, lp++) {
567 				bus_space_write_4(cz->cz_win_st, cz->cz_win_sh,
568 				    ro + j, le32toh(*lp));
569 				delay(10);
570 			}
571 		}
572 	}
573 
574 	/* Now restart the MIPS. */
575 	CZ_WIN_FPGA(cz);
576 	CZ_FPGA_WRITE(cz, FPGA_CPU_START, 0);
577 
578 	/* Wait for the MIPS to start, then report the results. */
579 	CZ_WIN_RAM(cz);
580 
581 #ifdef CZ_DEBUG
582 	aprint_debug("%s: waiting for MIPS to start", cz->cz_dev.dv_xname);
583 #endif
584 	for (i = 0; i < 100; i++) {
585 		fid = bus_space_read_4(cz->cz_win_st, cz->cz_win_sh,
586 		    ZFIRM_SIG_OFF);
587 		if (fid == ZFIRM_SIG) {
588 			/* MIPS has booted. */
589 			break;
590 		} else if (fid == ZFIRM_HLT) {
591 			/*
592 			 * The MIPS has halted, usually due to a power
593 			 * shortage on the expansion module.
594 			 */
595 			aprint_error("%s: MIPS halted; possible power supply "
596 			    "problem\n", cz->cz_dev.dv_xname);
597 			return (EIO);
598 		} else {
599 #ifdef CZ_DEBUG
600 			if ((i % 8) == 0)
601 				aprint_debug(".");
602 #endif
603 			delay(250000);
604 		}
605 	}
606 #ifdef CZ_DEBUG
607 	aprint_debug("\n");
608 #endif
609 	if (i == 100) {
610 		CZ_WIN_FPGA(cz);
611 		aprint_error(
612 		    "%s: MIPS failed to start; wanted 0x%08x got 0x%08x\n",
613 		    cz->cz_dev.dv_xname, ZFIRM_SIG, fid);
614 		aprint_error("%s: FPGA ID 0x%08x, FPGA version 0x%08x\n",
615 		    cz->cz_dev.dv_xname, CZ_FPGA_READ(cz, FPGA_ID),
616 		    CZ_FPGA_READ(cz, FPGA_VERSION));
617 		return (EIO);
618 	}
619 
620 	/*
621 	 * Locate the firmware control structures.
622 	 */
623 	cz->cz_fwctl = bus_space_read_4(cz->cz_win_st, cz->cz_win_sh,
624 	    ZFIRM_CTRLADDR_OFF);
625 #ifdef CZ_DEBUG
626 	aprint_debug("%s: FWCTL structure at offset 0x%08lx\n",
627 	    cz->cz_dev.dv_xname, cz->cz_fwctl);
628 #endif
629 
630 	CZ_FWCTL_WRITE(cz, BRDCTL_C_OS, C_OS_BSD);
631 	CZ_FWCTL_WRITE(cz, BRDCTL_DRVERSION, CZ_DRIVER_VERSION);
632 
633 	cz->cz_nchannels = CZ_FWCTL_READ(cz, BRDCTL_NCHANNEL);
634 
635 	switch (cz->cz_mailbox0) {
636 	case MAILBOX0_8Zo_V1:
637 		board = "Cyclades-8Zo ver. 1";
638 		break;
639 
640 	case MAILBOX0_8Zo_V2:
641 		board = "Cyclades-8Zo ver. 2";
642 		break;
643 
644 	case MAILBOX0_Ze_V1:
645 		board = "Cyclades-Ze";
646 		break;
647 
648 	default:
649 		board = "unknown Cyclades Z-series";
650 		break;
651 	}
652 
653 	fid = CZ_FWCTL_READ(cz, BRDCTL_FWVERSION);
654 	aprint_normal("%s: %s, ", cz->cz_dev.dv_xname, board);
655 	if (cz->cz_nchannels == 0)
656 		aprint_normal("no channels attached, ");
657 	else
658 		aprint_normal("%d channels (ttyCZ%04d..ttyCZ%04d), ",
659 		    cz->cz_nchannels, cztty_attached_ttys,
660 		    cztty_attached_ttys + (cz->cz_nchannels - 1));
661 	aprint_normal("firmware %x.%x.%x\n",
662 	    (fid >> 8) & 0xf, (fid >> 4) & 0xf, fid & 0xf);
663 
664 	return (0);
665 }
666 
667 /*
668  * cz_poll:
669  *
670  * This card doesn't do interrupts, so scan it for activity every CZ_POLL_MS
671  * ms.
672  */
673 static void
674 cz_poll(void *arg)
675 {
676 	int s = spltty();
677 	struct cz_softc *cz = arg;
678 
679 	cz_intr(cz);
680 	callout_reset(&cz->cz_callout, cz_timeout_ticks, cz_poll, cz);
681 
682 	splx(s);
683 }
684 
685 /*
686  * cz_intr:
687  *
688  *	Interrupt service routine.
689  *
690  * We either are receiving an interrupt directly from the board, or we are
691  * in polling mode and it's time to poll.
692  */
693 static int
694 cz_intr(void *arg)
695 {
696 	int	rval = 0;
697 	u_int	command, channel, param;
698 	struct	cz_softc *cz = arg;
699 	struct	cztty_softc *sc;
700 	struct	tty *tp;
701 
702 	while ((command = (CZ_PLX_READ(cz, PLX_LOCAL_PCI_DOORBELL) & 0xff))) {
703 		rval = 1;
704 		channel = CZ_FWCTL_READ(cz, BRDCTL_FWCMD_CHANNEL);
705 		param = CZ_FWCTL_READ(cz, BRDCTL_FWCMD_PARAM);
706 
707 		/* now clear this interrupt, posslibly enabling another */
708 		CZ_PLX_WRITE(cz, PLX_LOCAL_PCI_DOORBELL, command);
709 
710 		if (cz->cz_ports == NULL) {
711 #ifdef CZ_DEBUG
712 			printf("%s: interrupt on channel %d, but no channels\n",
713 			    cz->cz_dev.dv_xname, channel);
714 #endif
715 			continue;
716 		}
717 
718 		sc = &cz->cz_ports[channel];
719 
720 		if (sc->sc_channel == CZTTY_CHANNEL_DEAD)
721 			break;
722 
723 		tp = sc->sc_tty;
724 
725 		switch (command) {
726 		case C_CM_TXFEMPTY:		/* transmit cases */
727 		case C_CM_TXBEMPTY:
728 		case C_CM_TXLOWWM:
729 		case C_CM_INTBACK:
730 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
731 #ifdef CZ_DEBUG
732 				printf("%s: tx intr on closed channel %d\n",
733 				    cz->cz_dev.dv_xname, channel);
734 #endif
735 				break;
736 			}
737 
738 			if (cztty_transmit(sc, tp)) {
739 				/*
740 				 * Do wakeup stuff here.
741 				 */
742 				ttwakeup(tp);
743 				wakeup(tp);
744 			}
745 			break;
746 
747 		case C_CM_RXNNDT:		/* receive cases */
748 		case C_CM_RXHIWM:
749 		case C_CM_INTBACK2:		/* from restart ?? */
750 #if 0
751 		case C_CM_ICHAR:
752 #endif
753 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
754 				CZTTY_BUF_WRITE(sc, BUFCTL_RX_GET,
755 				    CZTTY_BUF_READ(sc, BUFCTL_RX_PUT));
756 				break;
757 			}
758 
759 			if (cztty_receive(sc, tp)) {
760 				/*
761 				 * Do wakeup stuff here.
762 				 */
763 				ttwakeup(tp);
764 				wakeup(tp);
765 			}
766 			break;
767 
768 		case C_CM_MDCD:
769 			if (!ISSET(tp->t_state, TS_ISOPEN))
770 				break;
771 
772 			(void) (*tp->t_linesw->l_modem)(tp,
773 			    ISSET(C_RS_DCD, CZTTY_CHAN_READ(sc,
774 			    CHNCTL_RS_STATUS)));
775 			break;
776 
777 		case C_CM_MDSR:
778 		case C_CM_MRI:
779 		case C_CM_MCTS:
780 		case C_CM_MRTS:
781 			break;
782 
783 		case C_CM_IOCTLW:
784 			break;
785 
786 		case C_CM_PR_ERROR:
787 			sc->sc_parity_errors++;
788 			goto error_common;
789 
790 		case C_CM_FR_ERROR:
791 			sc->sc_framing_errors++;
792 			goto error_common;
793 
794 		case C_CM_OVR_ERROR:
795 			sc->sc_overflows++;
796  error_common:
797 			if (sc->sc_errors++ == 0)
798 				callout_reset(&sc->sc_diag_ch, 60 * hz,
799 				    cztty_diag, sc);
800 			break;
801 
802 		case C_CM_RXBRK:
803 			if (!ISSET(tp->t_state, TS_ISOPEN))
804 				break;
805 
806 			/*
807 			 * A break is a \000 character with TTY_FE error
808 			 * flags set. So TTY_FE by itself works.
809 			 */
810 			(*tp->t_linesw->l_rint)(TTY_FE, tp);
811 			ttwakeup(tp);
812 			wakeup(tp);
813 			break;
814 
815 		default:
816 #ifdef CZ_DEBUG
817 			printf("%s: channel %d: Unknown interrupt 0x%x\n",
818 			    cz->cz_dev.dv_xname, sc->sc_channel, command);
819 #endif
820 			break;
821 		}
822 	}
823 
824 	return (rval);
825 }
826 
827 /*
828  * cz_wait_pci_doorbell:
829  *
830  *	Wait for the pci doorbell to be clear - wait for pending
831  *	activity to drain.
832  */
833 static int
834 cz_wait_pci_doorbell(struct cz_softc *cz, const char *wstring)
835 {
836 	int	error;
837 
838 	while (CZ_PLX_READ(cz, PLX_PCI_LOCAL_DOORBELL)) {
839 		error = tsleep(cz, TTIPRI | PCATCH, wstring, max(1, hz/100));
840 		if ((error != 0) && (error != EWOULDBLOCK))
841 			return (error);
842 	}
843 	return (0);
844 }
845 
846 /*****************************************************************************
847  * Cyclades-Z TTY code starts here...
848  *****************************************************************************/
849 
850 #define CZTTYDIALOUT_MASK	0x80000
851 
852 #define	CZTTY_DIALOUT(dev)	(minor((dev)) & CZTTYDIALOUT_MASK)
853 #define	CZTTY_CZ(sc)		((sc)->sc_parent)
854 
855 #define	CZTTY_SOFTC(dev)	cztty_getttysoftc(dev)
856 
857 static struct cztty_softc *
858 cztty_getttysoftc(dev_t dev)
859 {
860 	int i, j, k = 0, u = minor(dev) & ~CZTTYDIALOUT_MASK;
861 	struct cz_softc *cz = NULL;
862 
863 	for (i = 0, j = 0; i < cz_cd.cd_ndevs; i++) {
864 		k = j;
865 		cz = device_lookup(&cz_cd, i);
866 		if (cz == NULL)
867 			continue;
868 		if (cz->cz_ports == NULL)
869 			continue;
870 		j += cz->cz_nchannels;
871 		if (j > u)
872 			break;
873 	}
874 
875 	if (i >= cz_cd.cd_ndevs)
876 		return (NULL);
877 	else
878 		return (&cz->cz_ports[u - k]);
879 }
880 
881 /*
882  * czttytty:
883  *
884  *	Return a pointer to our tty.
885  */
886 static struct tty *
887 czttytty(dev_t dev)
888 {
889 	struct cztty_softc *sc = CZTTY_SOFTC(dev);
890 
891 #ifdef DIAGNOSTIC
892 	if (sc == NULL)
893 		panic("czttytty");
894 #endif
895 
896 	return (sc->sc_tty);
897 }
898 
899 /*
900  * cztty_shutdown:
901  *
902  *	Shut down a port.
903  */
904 static void
905 cztty_shutdown(struct cztty_softc *sc)
906 {
907 	struct cz_softc *cz = CZTTY_CZ(sc);
908 	struct tty *tp = sc->sc_tty;
909 	int s;
910 
911 	s = spltty();
912 
913 	/* Clear any break condition set with TIOCSBRK. */
914 	cztty_break(sc, 0);
915 
916 	/*
917 	 * Hang up if necessary.  Wait a bit, so the other side has time to
918 	 * notice even if we immediately open the port again.
919 	 */
920 	if (ISSET(tp->t_cflag, HUPCL)) {
921 		cztty_modem(sc, 0);
922 		(void) tsleep(tp, TTIPRI, ttclos, hz);
923 	}
924 
925 	/* Disable the channel. */
926 	cz_wait_pci_doorbell(cz, "czdis");
927 	CZTTY_CHAN_WRITE(sc, CHNCTL_OP_MODE, C_CH_DISABLE);
928 	CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
929 	CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTL);
930 
931 	if ((--cz->cz_nopenchan == 0) && (cz->cz_ih == NULL)) {
932 #ifdef CZ_DEBUG
933 		printf("%s: Disabling polling\n", cz->cz_dev.dv_xname);
934 #endif
935 		callout_stop(&cz->cz_callout);
936 	}
937 
938 	splx(s);
939 }
940 
941 /*
942  * czttyopen:
943  *
944  *	Open a Cyclades-Z serial port.
945  */
946 static int
947 czttyopen(dev_t dev, int flags, int mode, struct lwp *l)
948 {
949 	struct cztty_softc *sc = CZTTY_SOFTC(dev);
950 	struct cz_softc *cz;
951 	struct tty *tp;
952 	int s, error;
953 
954 	if (sc == NULL)
955 		return (ENXIO);
956 
957 	if (sc->sc_channel == CZTTY_CHANNEL_DEAD)
958 		return (ENXIO);
959 
960 	cz = CZTTY_CZ(sc);
961 	tp = sc->sc_tty;
962 
963 	if (ISSET(tp->t_state, TS_ISOPEN) &&
964 	    ISSET(tp->t_state, TS_XCLUDE) &&
965 	    kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
966 	    &l->l_acflag) != 0)
967 		return (EBUSY);
968 
969 	s = spltty();
970 
971 	/*
972 	 * Do the following iff this is a first open.
973 	 */
974 	if (!ISSET(tp->t_state, TS_ISOPEN) && (tp->t_wopen == 0)) {
975 		struct termios t;
976 
977 		tp->t_dev = dev;
978 
979 		/* If we're turning things on, enable interrupts */
980 		if ((cz->cz_nopenchan++ == 0) && (cz->cz_ih == NULL)) {
981 #ifdef CZ_DEBUG
982 			printf("%s: Enabling polling.\n",
983 			    cz->cz_dev.dv_xname);
984 #endif
985 			callout_reset(&cz->cz_callout, cz_timeout_ticks,
986 			    cz_poll, cz);
987 		}
988 
989 		/*
990 		 * Enable the channel.  Don't actually ring the
991 		 * doorbell here; czttyparam() will do it for us.
992 		 */
993 		cz_wait_pci_doorbell(cz, "czopen");
994 
995 		CZTTY_CHAN_WRITE(sc, CHNCTL_OP_MODE, C_CH_ENABLE);
996 
997 		/*
998 		 * Initialize the termios status to the defaults.  Add in the
999 		 * sticky bits from TIOCSFLAGS.
1000 		 */
1001 		t.c_ispeed = 0;
1002 		t.c_ospeed = TTYDEF_SPEED;
1003 		t.c_cflag = TTYDEF_CFLAG;
1004 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
1005 			SET(t.c_cflag, CLOCAL);
1006 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
1007 			SET(t.c_cflag, CRTSCTS);
1008 
1009 		/*
1010 		 * Reset the input and output rings.  Do this before
1011 		 * we call czttyparam(), as that function enables
1012 		 * the channel.
1013 		 */
1014 		CZTTY_BUF_WRITE(sc, BUFCTL_RX_GET,
1015 		    CZTTY_BUF_READ(sc, BUFCTL_RX_PUT));
1016 		CZTTY_BUF_WRITE(sc, BUFCTL_TX_PUT,
1017 		    CZTTY_BUF_READ(sc, BUFCTL_TX_GET));
1018 
1019 		/* Make sure czttyparam() will see changes. */
1020 		tp->t_ospeed = 0;
1021 		(void) czttyparam(tp, &t);
1022 		tp->t_iflag = TTYDEF_IFLAG;
1023 		tp->t_oflag = TTYDEF_OFLAG;
1024 		tp->t_lflag = TTYDEF_LFLAG;
1025 		ttychars(tp);
1026 		ttsetwater(tp);
1027 
1028 		/*
1029 		 * Turn on DTR.  We must always do this, even if carrier is not
1030 		 * present, because otherwise we'd have to use TIOCSDTR
1031 		 * immediately after setting CLOCAL, which applications do not
1032 		 * expect.  We always assert DTR while the device is open
1033 		 * unless explicitly requested to deassert it.
1034 		 */
1035 		cztty_modem(sc, 1);
1036 	}
1037 
1038 	splx(s);
1039 
1040 	error = ttyopen(tp, CZTTY_DIALOUT(dev), ISSET(flags, O_NONBLOCK));
1041 	if (error)
1042 		goto bad;
1043 
1044 	error = (*tp->t_linesw->l_open)(dev, tp);
1045 	if (error)
1046 		goto bad;
1047 
1048 	return (0);
1049 
1050  bad:
1051 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
1052 		/*
1053 		 * We failed to open the device, and nobody else had it opened.
1054 		 * Clean up the state as appropriate.
1055 		 */
1056 		cztty_shutdown(sc);
1057 	}
1058 
1059 	return (error);
1060 }
1061 
1062 /*
1063  * czttyclose:
1064  *
1065  *	Close a Cyclades-Z serial port.
1066  */
1067 static int
1068 czttyclose(dev_t dev, int flags, int mode, struct lwp *l)
1069 {
1070 	struct cztty_softc *sc = CZTTY_SOFTC(dev);
1071 	struct tty *tp = sc->sc_tty;
1072 
1073 	/* XXX This is for cons.c. */
1074 	if (!ISSET(tp->t_state, TS_ISOPEN))
1075 		return (0);
1076 
1077 	(*tp->t_linesw->l_close)(tp, flags);
1078 	ttyclose(tp);
1079 
1080 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
1081 		/*
1082 		 * Although we got a last close, the device may still be in
1083 		 * use; e.g. if this was the dialout node, and there are still
1084 		 * processes waiting for carrier on the non-dialout node.
1085 		 */
1086 		cztty_shutdown(sc);
1087 	}
1088 
1089 	return (0);
1090 }
1091 
1092 /*
1093  * czttyread:
1094  *
1095  *	Read from a Cyclades-Z serial port.
1096  */
1097 static int
1098 czttyread(dev_t dev, struct uio *uio, int flags)
1099 {
1100 	struct cztty_softc *sc = CZTTY_SOFTC(dev);
1101 	struct tty *tp = sc->sc_tty;
1102 
1103 	return ((*tp->t_linesw->l_read)(tp, uio, flags));
1104 }
1105 
1106 /*
1107  * czttywrite:
1108  *
1109  *	Write to a Cyclades-Z serial port.
1110  */
1111 static int
1112 czttywrite(dev_t dev, struct uio *uio, int flags)
1113 {
1114 	struct cztty_softc *sc = CZTTY_SOFTC(dev);
1115 	struct tty *tp = sc->sc_tty;
1116 
1117 	return ((*tp->t_linesw->l_write)(tp, uio, flags));
1118 }
1119 
1120 /*
1121  * czttypoll:
1122  *
1123  *	Poll a Cyclades-Z serial port.
1124  */
1125 static int
1126 czttypoll(dev_t dev, int events, struct lwp *l)
1127 {
1128 	struct cztty_softc *sc = CZTTY_SOFTC(dev);
1129 	struct tty *tp = sc->sc_tty;
1130 
1131 	return ((*tp->t_linesw->l_poll)(tp, events, l));
1132 }
1133 
1134 /*
1135  * czttyioctl:
1136  *
1137  *	Perform a control operation on a Cyclades-Z serial port.
1138  */
1139 static int
1140 czttyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
1141 {
1142 	struct cztty_softc *sc = CZTTY_SOFTC(dev);
1143 	struct tty *tp = sc->sc_tty;
1144 	int s, error;
1145 
1146 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1147 	if (error != EPASSTHROUGH)
1148 		return (error);
1149 
1150 	error = ttioctl(tp, cmd, data, flag, l);
1151 	if (error != EPASSTHROUGH)
1152 		return (error);
1153 
1154 	error = 0;
1155 
1156 	s = spltty();
1157 
1158 	switch (cmd) {
1159 	case TIOCSBRK:
1160 		cztty_break(sc, 1);
1161 		break;
1162 
1163 	case TIOCCBRK:
1164 		cztty_break(sc, 0);
1165 		break;
1166 
1167 	case TIOCGFLAGS:
1168 		*(int *)data = sc->sc_swflags;
1169 		break;
1170 
1171 	case TIOCSFLAGS:
1172 		error = kauth_authorize_generic(l->l_cred,
1173 		    KAUTH_GENERIC_ISSUSER, &l->l_acflag);
1174 		if (error)
1175 			break;
1176 		sc->sc_swflags = *(int *)data;
1177 		break;
1178 
1179 	case TIOCSDTR:
1180 		cztty_modem(sc, 1);
1181 		break;
1182 
1183 	case TIOCCDTR:
1184 		cztty_modem(sc, 0);
1185 		break;
1186 
1187 	case TIOCMSET:
1188 	case TIOCMBIS:
1189 	case TIOCMBIC:
1190 		tiocm_to_cztty(sc, cmd, *(int *)data);
1191 		break;
1192 
1193 	case TIOCMGET:
1194 		*(int *)data = cztty_to_tiocm(sc);
1195 		break;
1196 
1197 	default:
1198 		error = EPASSTHROUGH;
1199 		break;
1200 	}
1201 
1202 	splx(s);
1203 
1204 	return (error);
1205 }
1206 
1207 /*
1208  * cztty_break:
1209  *
1210  *	Set or clear BREAK on a port.
1211  */
1212 static void
1213 cztty_break(struct cztty_softc *sc, int onoff)
1214 {
1215 	struct cz_softc *cz = CZTTY_CZ(sc);
1216 
1217 	cz_wait_pci_doorbell(cz, "czbreak");
1218 
1219 	CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1220 	CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL,
1221 	    onoff ? C_CM_SET_BREAK : C_CM_CLR_BREAK);
1222 }
1223 
1224 /*
1225  * cztty_modem:
1226  *
1227  *	Set or clear DTR on a port.
1228  */
1229 static void
1230 cztty_modem(struct cztty_softc *sc, int onoff)
1231 {
1232 	struct cz_softc *cz = CZTTY_CZ(sc);
1233 
1234 	if (sc->sc_rs_control_dtr == 0)
1235 		return;
1236 
1237 	cz_wait_pci_doorbell(cz, "czmod");
1238 
1239 	if (onoff)
1240 		sc->sc_chanctl_rs_control |= sc->sc_rs_control_dtr;
1241 	else
1242 		sc->sc_chanctl_rs_control &= ~sc->sc_rs_control_dtr;
1243 	CZTTY_CHAN_WRITE(sc, CHNCTL_RS_CONTROL, sc->sc_chanctl_rs_control);
1244 
1245 	CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1246 	CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTLM);
1247 }
1248 
1249 /*
1250  * tiocm_to_cztty:
1251  *
1252  *	Process TIOCM* ioctls.
1253  */
1254 static void
1255 tiocm_to_cztty(struct cztty_softc *sc, u_long how, int ttybits)
1256 {
1257 	struct cz_softc *cz = CZTTY_CZ(sc);
1258 	u_int32_t czttybits;
1259 
1260 	czttybits = 0;
1261 	if (ISSET(ttybits, TIOCM_DTR))
1262 		SET(czttybits, C_RS_DTR);
1263 	if (ISSET(ttybits, TIOCM_RTS))
1264 		SET(czttybits, C_RS_RTS);
1265 
1266 	cz_wait_pci_doorbell(cz, "cztiocm");
1267 
1268 	switch (how) {
1269 	case TIOCMBIC:
1270 		CLR(sc->sc_chanctl_rs_control, czttybits);
1271 		break;
1272 
1273 	case TIOCMBIS:
1274 		SET(sc->sc_chanctl_rs_control, czttybits);
1275 		break;
1276 
1277 	case TIOCMSET:
1278 		CLR(sc->sc_chanctl_rs_control, C_RS_DTR | C_RS_RTS);
1279 		SET(sc->sc_chanctl_rs_control, czttybits);
1280 		break;
1281 	}
1282 
1283 	CZTTY_CHAN_WRITE(sc, CHNCTL_RS_CONTROL, sc->sc_chanctl_rs_control);
1284 
1285 	CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1286 	CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTLM);
1287 }
1288 
1289 /*
1290  * cztty_to_tiocm:
1291  *
1292  *	Process the TIOCMGET ioctl.
1293  */
1294 static int
1295 cztty_to_tiocm(struct cztty_softc *sc)
1296 {
1297 	struct cz_softc *cz = CZTTY_CZ(sc);
1298 	u_int32_t rs_status, op_mode;
1299 	int ttybits = 0;
1300 
1301 	cz_wait_pci_doorbell(cz, "cztty");
1302 
1303 	rs_status = CZTTY_CHAN_READ(sc, CHNCTL_RS_STATUS);
1304 	op_mode = CZTTY_CHAN_READ(sc, CHNCTL_OP_MODE);
1305 
1306 	if (ISSET(rs_status, C_RS_RTS))
1307 		SET(ttybits, TIOCM_RTS);
1308 	if (ISSET(rs_status, C_RS_CTS))
1309 		SET(ttybits, TIOCM_CTS);
1310 	if (ISSET(rs_status, C_RS_DCD))
1311 		SET(ttybits, TIOCM_CAR);
1312 	if (ISSET(rs_status, C_RS_DTR))
1313 		SET(ttybits, TIOCM_DTR);
1314 	if (ISSET(rs_status, C_RS_RI))
1315 		SET(ttybits, TIOCM_RNG);
1316 	if (ISSET(rs_status, C_RS_DSR))
1317 		SET(ttybits, TIOCM_DSR);
1318 
1319 	if (ISSET(op_mode, C_CH_ENABLE))
1320 		SET(ttybits, TIOCM_LE);
1321 
1322 	return (ttybits);
1323 }
1324 
1325 /*
1326  * czttyparam:
1327  *
1328  *	Set Cyclades-Z serial port parameters from termios.
1329  *
1330  *	XXX Should just copy the whole termios after making
1331  *	XXX sure all the changes could be done.
1332  */
1333 static int
1334 czttyparam(struct tty *tp, struct termios *t)
1335 {
1336 	struct cztty_softc *sc = CZTTY_SOFTC(tp->t_dev);
1337 	struct cz_softc *cz = CZTTY_CZ(sc);
1338 	u_int32_t rs_status;
1339 	int ospeed, cflag;
1340 
1341 	ospeed = t->c_ospeed;
1342 	cflag = t->c_cflag;
1343 
1344 	/* Check requested parameters. */
1345 	if (ospeed < 0)
1346 		return (EINVAL);
1347 	if (t->c_ispeed && t->c_ispeed != ospeed)
1348 		return (EINVAL);
1349 
1350 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
1351 		SET(cflag, CLOCAL);
1352 		CLR(cflag, HUPCL);
1353 	}
1354 
1355 	/*
1356 	 * If there were no changes, don't do anything.  This avoids dropping
1357 	 * input and improves performance when all we did was frob things like
1358 	 * VMIN and VTIME.
1359 	 */
1360 	if (tp->t_ospeed == ospeed &&
1361 	    tp->t_cflag == cflag)
1362 		return (0);
1363 
1364 	/* Data bits. */
1365 	sc->sc_chanctl_comm_data_l = 0;
1366 	switch (t->c_cflag & CSIZE) {
1367 	case CS5:
1368 		sc->sc_chanctl_comm_data_l |= C_DL_CS5;
1369 		break;
1370 
1371 	case CS6:
1372 		sc->sc_chanctl_comm_data_l |= C_DL_CS6;
1373 		break;
1374 
1375 	case CS7:
1376 		sc->sc_chanctl_comm_data_l |= C_DL_CS7;
1377 		break;
1378 
1379 	case CS8:
1380 		sc->sc_chanctl_comm_data_l |= C_DL_CS8;
1381 		break;
1382 	}
1383 
1384 	/* Stop bits. */
1385 	if (t->c_cflag & CSTOPB) {
1386 		if ((sc->sc_chanctl_comm_data_l & C_DL_CS) == C_DL_CS5)
1387 			sc->sc_chanctl_comm_data_l |= C_DL_15STOP;
1388 		else
1389 			sc->sc_chanctl_comm_data_l |= C_DL_2STOP;
1390 	} else
1391 		sc->sc_chanctl_comm_data_l |= C_DL_1STOP;
1392 
1393 	/* Parity. */
1394 	if (t->c_cflag & PARENB) {
1395 		if (t->c_cflag & PARODD)
1396 			sc->sc_chanctl_comm_parity = C_PR_ODD;
1397 		else
1398 			sc->sc_chanctl_comm_parity = C_PR_EVEN;
1399 	} else
1400 		sc->sc_chanctl_comm_parity = C_PR_NONE;
1401 
1402 	/*
1403 	 * Initialize flow control pins depending on the current flow control
1404 	 * mode.
1405 	 */
1406 	if (ISSET(t->c_cflag, CRTSCTS)) {
1407 		sc->sc_rs_control_dtr = C_RS_DTR;
1408 		sc->sc_chanctl_hw_flow = C_RS_CTS | C_RS_RTS;
1409 	} else if (ISSET(t->c_cflag, MDMBUF)) {
1410 		sc->sc_rs_control_dtr = 0;
1411 		sc->sc_chanctl_hw_flow = C_RS_DCD | C_RS_DTR;
1412 	} else {
1413 		/*
1414 		 * If no flow control, then always set RTS.  This will make
1415 		 * the other side happy if it mistakenly thinks we're doing
1416 		 * RTS/CTS flow control.
1417 		 */
1418 		sc->sc_rs_control_dtr = C_RS_DTR | C_RS_RTS;
1419 		sc->sc_chanctl_hw_flow = 0;
1420 		if (ISSET(sc->sc_chanctl_rs_control, C_RS_DTR))
1421 			SET(sc->sc_chanctl_rs_control, C_RS_RTS);
1422 		else
1423 			CLR(sc->sc_chanctl_rs_control, C_RS_RTS);
1424 	}
1425 
1426 	/* Baud rate. */
1427 	sc->sc_chanctl_comm_baud = ospeed;
1428 
1429 	/* Copy to tty. */
1430 	tp->t_ispeed =  0;
1431 	tp->t_ospeed = t->c_ospeed;
1432 	tp->t_cflag = t->c_cflag;
1433 
1434 	/*
1435 	 * Now load the channel control structure.
1436 	 */
1437 
1438 	cz_wait_pci_doorbell(cz, "czparam");
1439 
1440 	CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_BAUD, sc->sc_chanctl_comm_baud);
1441 	CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_DATA_L, sc->sc_chanctl_comm_data_l);
1442 	CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_PARITY, sc->sc_chanctl_comm_parity);
1443 	CZTTY_CHAN_WRITE(sc, CHNCTL_HW_FLOW, sc->sc_chanctl_hw_flow);
1444 	CZTTY_CHAN_WRITE(sc, CHNCTL_RS_CONTROL, sc->sc_chanctl_rs_control);
1445 
1446 	CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1447 	CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTLW);
1448 
1449 	cz_wait_pci_doorbell(cz, "czparam");
1450 
1451 	CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1452 	CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTLM);
1453 
1454 	cz_wait_pci_doorbell(cz, "czparam");
1455 
1456 	/*
1457 	 * Update the tty layer's idea of the carrier bit, in case we changed
1458 	 * CLOCAL.  We don't hang up here; we only do that by explicit
1459 	 * request.
1460 	 */
1461 	rs_status = CZTTY_CHAN_READ(sc, CHNCTL_RS_STATUS);
1462 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(rs_status, C_RS_DCD));
1463 
1464 	return (0);
1465 }
1466 
1467 /*
1468  * czttystart:
1469  *
1470  *	Start or restart transmission.
1471  */
1472 static void
1473 czttystart(struct tty *tp)
1474 {
1475 	struct cztty_softc *sc = CZTTY_SOFTC(tp->t_dev);
1476 	int s;
1477 
1478 	s = spltty();
1479 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1480 		goto out;
1481 
1482 	if (tp->t_outq.c_cc <= tp->t_lowat) {
1483 		if (ISSET(tp->t_state, TS_ASLEEP)) {
1484 			CLR(tp->t_state, TS_ASLEEP);
1485 			wakeup(&tp->t_outq);
1486 		}
1487 		selwakeup(&tp->t_wsel);
1488 		if (tp->t_outq.c_cc == 0)
1489 			goto out;
1490 	}
1491 
1492 	cztty_transmit(sc, tp);
1493  out:
1494 	splx(s);
1495 }
1496 
1497 /*
1498  * czttystop:
1499  *
1500  *	Stop output, e.g., for ^S or output flush.
1501  */
1502 static void
1503 czttystop(struct tty *tp, int flag)
1504 {
1505 
1506 	/*
1507 	 * XXX We don't do anything here, yet.  Mostly, I don't know
1508 	 * XXX exactly how this should be implemented on this device.
1509 	 * XXX We've given a big chunk of data to the MIPS already,
1510 	 * XXX and I don't know how we request the MIPS to stop sending
1511 	 * XXX the data.  So, punt for now.  --thorpej
1512 	 */
1513 }
1514 
1515 /*
1516  * cztty_diag:
1517  *
1518  *	Issue a scheduled diagnostic message.
1519  */
1520 static void
1521 cztty_diag(void *arg)
1522 {
1523 	struct cztty_softc *sc = arg;
1524 	struct cz_softc *cz = CZTTY_CZ(sc);
1525 	u_int overflows, parity_errors, framing_errors;
1526 	int s;
1527 
1528 	s = spltty();
1529 
1530 	overflows = sc->sc_overflows;
1531 	sc->sc_overflows = 0;
1532 
1533 	parity_errors = sc->sc_parity_errors;
1534 	sc->sc_parity_errors = 0;
1535 
1536 	framing_errors = sc->sc_framing_errors;
1537 	sc->sc_framing_errors = 0;
1538 
1539 	sc->sc_errors = 0;
1540 
1541 	splx(s);
1542 
1543 	log(LOG_WARNING,
1544 	    "%s: channel %d: %u overflow%s, %u parity, %u framing error%s\n",
1545 	    cz->cz_dev.dv_xname, sc->sc_channel,
1546 	    overflows, overflows == 1 ? "" : "s",
1547 	    parity_errors,
1548 	    framing_errors, framing_errors == 1 ? "" : "s");
1549 }
1550 
1551 const struct cdevsw cz_cdevsw = {
1552 	czttyopen, czttyclose, czttyread, czttywrite, czttyioctl,
1553 	    czttystop, czttytty, czttypoll, nommap, ttykqfilter, D_TTY
1554 };
1555 
1556 /*
1557  * tx and rx ring buffer size macros:
1558  *
1559  * The transmitter and receiver both use ring buffers. For each one, there
1560  * is a get (consumer) and a put (producer) offset. The get value is the
1561  * next byte to be read from the ring, and the put is the next one to be
1562  * put into the ring.  get == put means the ring is empty.
1563  *
1564  * For each ring, the firmware controls one of (get, put) and this driver
1565  * controls the other. For transmission, this driver updates put to point
1566  * past the valid data, and the firmware moves get as bytes are sent. Likewise
1567  * for receive, the driver controls put, and this driver controls get.
1568  */
1569 #define	TX_MOVEABLE(g, p, s)	(((g) > (p)) ? ((g) - (p) - 1) : ((s) - (p)))
1570 #define RX_MOVEABLE(g, p, s)	(((g) > (p)) ? ((s) - (g)) : ((p) - (g)))
1571 
1572 /*
1573  * cztty_transmit()
1574  *
1575  * Look at the tty for this port and start sending.
1576  */
1577 static int
1578 cztty_transmit(struct cztty_softc *sc, struct tty *tp)
1579 {
1580 	struct cz_softc *cz = CZTTY_CZ(sc);
1581 	u_int move, get, put, size, address;
1582 #ifdef HOSTRAMCODE
1583 	int error, done = 0;
1584 #else
1585 	int done = 0;
1586 #endif
1587 
1588 	size	= CZTTY_BUF_READ(sc, BUFCTL_TX_BUFSIZE);
1589 	get	= CZTTY_BUF_READ(sc, BUFCTL_TX_GET);
1590 	put	= CZTTY_BUF_READ(sc, BUFCTL_TX_PUT);
1591 	address	= CZTTY_BUF_READ(sc, BUFCTL_TX_BUFADDR);
1592 
1593 	while ((tp->t_outq.c_cc > 0) && ((move = TX_MOVEABLE(get, put, size)))){
1594 #ifdef HOSTRAMCODE
1595 		if (0) {
1596 			move = min(tp->t_outq.c_cc, move);
1597 			error = q_to_b(&tp->t_outq, 0, move);
1598 			if (error != move) {
1599 				printf("%s: channel %d: error moving to "
1600 				    "transmit buf\n", cz->cz_dev.dv_xname,
1601 				    sc->sc_channel);
1602 				move = error;
1603 			}
1604 		} else {
1605 #endif
1606 			move = min(ndqb(&tp->t_outq, 0), move);
1607 			bus_space_write_region_1(cz->cz_win_st, cz->cz_win_sh,
1608 			    address + put, tp->t_outq.c_cf, move);
1609 			ndflush(&tp->t_outq, move);
1610 #ifdef HOSTRAMCODE
1611 		}
1612 #endif
1613 
1614 		put = ((put + move) % size);
1615 		done = 1;
1616 	}
1617 	if (done) {
1618 		CZTTY_BUF_WRITE(sc, BUFCTL_TX_PUT, put);
1619 	}
1620 	return (done);
1621 }
1622 
1623 static int
1624 cztty_receive(struct cztty_softc *sc, struct tty *tp)
1625 {
1626 	struct cz_softc *cz = CZTTY_CZ(sc);
1627 	u_int get, put, size, address;
1628 	int done = 0, ch;
1629 
1630 	size	= CZTTY_BUF_READ(sc, BUFCTL_RX_BUFSIZE);
1631 	get	= CZTTY_BUF_READ(sc, BUFCTL_RX_GET);
1632 	put	= CZTTY_BUF_READ(sc, BUFCTL_RX_PUT);
1633 	address	= CZTTY_BUF_READ(sc, BUFCTL_RX_BUFADDR);
1634 
1635 	while ((get != put) && ((tp->t_canq.c_cc + tp->t_rawq.c_cc) < tp->t_hiwat)) {
1636 #ifdef HOSTRAMCODE
1637 		if (hostram)
1638 			ch = ((char *)fifoaddr)[get];
1639 		} else {
1640 #endif
1641 			ch = bus_space_read_1(cz->cz_win_st, cz->cz_win_sh,
1642 			    address + get);
1643 #ifdef HOSTRAMCODE
1644 		}
1645 #endif
1646 		(*tp->t_linesw->l_rint)(ch, tp);
1647 		get = (get + 1) % size;
1648 		done = 1;
1649 	}
1650 	if (done) {
1651 		CZTTY_BUF_WRITE(sc, BUFCTL_RX_GET, get);
1652 	}
1653 	return (done);
1654 }
1655