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