cz.c revision 1.4 1 /* $NetBSD: cz.c,v 1.4 2000/05/23 01:02:21 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 | C_IN_RXBRK)
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_MDSR:
761 case C_CM_MRI:
762 case C_CM_CTS:
763 case C_CM_RTS:
764 break;
765
766 case C_CM_PR_ERROR:
767 sc->sc_parity_errors++;
768 goto error_common;
769
770 case C_CM_FR_ERROR:
771 sc->sc_framing_errors++;
772 goto error_common;
773
774 case C_CM_OVR_ERROR:
775 sc->sc_overflows++;
776 error_common:
777 if (sc->sc_errors++ == 0)
778 callout_reset(&sc->sc_diag_ch, 60 * hz,
779 cztty_diag, sc);
780 break;
781
782 case C_CM_RXBRK:
783 printf("Got BREAK\n");
784 break;
785
786 default:
787 #ifdef CZ_DEBUG
788 printf("%s: channel %d: Unknown interrupt 0x%x\n",
789 cz->cz_dev.dv_xname, sc->sc_channel, command);
790 #endif
791 break;
792 }
793 }
794
795 return (rval);
796 }
797
798 /*
799 * cz_wait_pci_doorbell:
800 *
801 * Wait for the pci doorbell to be clear - wait for pending
802 * activity to drain.
803 */
804 int
805 cz_wait_pci_doorbell(struct cz_softc *cz, const char *wstring)
806 {
807 int error;
808
809 while (CZ_PLX_READ(cz, PLX_PCI_LOCAL_DOORBELL)) {
810 error = tsleep(cz, TTIPRI | PCATCH, wstring, max(1, hz/100));
811 if ((error != 0) && (error != EWOULDBLOCK))
812 return (error);
813 }
814 return (0);
815 }
816
817 /*****************************************************************************
818 * Cyclades-Z TTY code starts here...
819 *****************************************************************************/
820
821 #define CZTTYCHAN_MASK 0x0003f
822 #define CZTTYBOARD_MASK (0x7ffff & ~CZTTYCHAN_MASK)
823 #define CZTTYBOARD_SHIFT 6
824 #define CZTTYDIALOUT_MASK 0x80000
825
826 #define CZTTY_CHAN(dev) (minor((dev)) & CZTTYCHAN_MASK)
827 #define CZTTY_BOARD(dev) ((minor((dev)) & CZTTYBOARD_MASK) >> \
828 CZTTYBOARD_SHIFT)
829 #define CZTTY_DIALOUT(dev) (minor((dev)) & CZTTYDIALOUT_MASK)
830 #define CZTTY_CZ(sc) ((sc)->sc_parent)
831
832 #define CZ_SOFTC(dev) \
833 ((struct cz_softc *)(CZTTY_BOARD(dev) < cz_cd.cd_ndevs ? \
834 cz_cd.cd_devs[CZTTY_BOARD(dev)] : NULL))
835
836 #define CZTTY_SOFTC(dev) \
837 ((CZ_SOFTC(dev) != NULL && \
838 CZTTY_CHAN(dev) < CZ_SOFTC(dev)->cz_nchannels) ? \
839 &CZ_SOFTC(dev)->cz_ports[CZTTY_CHAN(dev)] : NULL)
840
841 int
842 cztty_findmajor(void)
843 {
844 int maj;
845
846 for (maj = 0; maj < nchrdev; maj++) {
847 if (cdevsw[maj].d_open == czttyopen)
848 break;
849 }
850
851 return (maj == nchrdev) ? 0 : maj;
852 }
853
854 /*
855 * czttytty:
856 *
857 * Return a pointer to our tty.
858 */
859 struct tty *
860 czttytty(dev_t dev)
861 {
862 struct cztty_softc *sc = CZTTY_SOFTC(dev);
863
864 #ifdef DIAGNOSTIC
865 if (sc == NULL)
866 panic("czttytty");
867 #endif
868
869 return (sc->sc_tty);
870 }
871
872 /*
873 * cztty_shutdown:
874 *
875 * Shut down a port.
876 */
877 void
878 cztty_shutdown(struct cztty_softc *sc)
879 {
880 struct cz_softc *cz = CZTTY_CZ(sc);
881 struct tty *tp = sc->sc_tty;
882 int s;
883
884 s = spltty();
885
886 /* Clear any break condition set with TIOCSBRK. */
887 cztty_break(sc, 0);
888
889 /*
890 * Hang up if necessary. Wait a bit, so the other side has time to
891 * notice even if we immediately open the port again.
892 */
893 if (ISSET(tp->t_cflag, HUPCL)) {
894 cztty_modem(sc, 0);
895 (void) tsleep(tp, TTIPRI, ttclos, hz);
896 }
897
898 /* Disable the channel. */
899 cz_wait_pci_doorbell(cz, "czdis");
900 CZTTY_CHAN_WRITE(sc, CHNCTL_OP_MODE, C_CH_DISABLE);
901 CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
902 CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTL);
903
904 #ifdef CZ_DEBUG
905 printf("last close with %d open channels and no interrupt handler == %d", cz->cz_nopenchan, (cz->cz_ih == NULL));
906 #endif
907 if ((--cz->cz_nopenchan == 0) && (cz->cz_ih == NULL)) {
908 #ifdef CZ_DEBUG
909 printf("%s: Disabling polling\n", cz->cz_dev.dv_xname);
910 #endif
911 callout_stop(&cz->cz_callout);
912 }
913
914 splx(s);
915 }
916
917 /*
918 * czttyopen:
919 *
920 * Open a Cyclades-Z serial port.
921 */
922 int
923 czttyopen(dev_t dev, int flags, int mode, struct proc *p)
924 {
925 struct cztty_softc *sc = CZTTY_SOFTC(dev);
926 struct cz_softc *cz;
927 struct tty *tp;
928 int s, error;
929
930 if (sc == NULL)
931 return (ENXIO);
932
933 if (sc->sc_channel == CZTTY_CHANNEL_DEAD)
934 return (ENXIO);
935
936 cz = CZTTY_CZ(sc);
937 tp = sc->sc_tty;
938
939 if (ISSET(tp->t_state, TS_ISOPEN) &&
940 ISSET(tp->t_state, TS_XCLUDE) &&
941 p->p_ucred->cr_uid != 0)
942 return (EBUSY);
943
944 s = spltty();
945
946 /*
947 * Do the following iff this is a first open.
948 */
949 if (!ISSET(tp->t_state, TS_ISOPEN) && (tp->t_wopen == 0)) {
950 struct termios t;
951
952 tp->t_dev = dev;
953
954 /* If we're turning things on, enable interrupts */
955 if ((cz->cz_nopenchan++ == 0) && (cz->cz_ih == NULL)) {
956 #ifdef CZ_DEBUG
957 printf("%s: Enabling polling.\n",
958 cz->cz_dev.dv_xname);
959 #endif
960 callout_reset(&cz->cz_callout, cz_timeout_ticks,
961 cz_poll, cz);
962 }
963
964 /*
965 * Enable the channel. Don't actually ring the
966 * doorbell here; czttyparam() will do it for us.
967 */
968 cz_wait_pci_doorbell(cz, "czopen");
969
970 CZTTY_CHAN_WRITE(sc, CHNCTL_OP_MODE, C_CH_ENABLE);
971 sc->sc_chanctl_rs_control |= C_RS_DTR | C_RS_CTS | C_RS_RTS;
972
973 /*
974 * Initialize the termios status to the defaults. Add in the
975 * sticky bits from TIOCSFLAGS.
976 */
977 t.c_ispeed = 0;
978 t.c_ospeed = TTYDEF_SPEED;
979 t.c_cflag = TTYDEF_CFLAG;
980 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
981 SET(t.c_cflag, CLOCAL);
982 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
983 SET(t.c_cflag, CRTSCTS);
984
985 /*
986 * Reset the input and output rings. Do this before
987 * we call czttyparam(), as that function enables
988 * the channel.
989 */
990 CZTTY_BUF_WRITE(sc, BUFCTL_RX_GET,
991 CZTTY_BUF_READ(sc, BUFCTL_RX_PUT));
992 CZTTY_BUF_WRITE(sc, BUFCTL_TX_PUT,
993 CZTTY_BUF_READ(sc, BUFCTL_TX_GET));
994
995 /* Make sure czttyparam() will see changes. */
996 tp->t_ospeed = 0;
997 (void) czttyparam(tp, &t);
998 tp->t_iflag = TTYDEF_IFLAG;
999 tp->t_oflag = TTYDEF_OFLAG;
1000 tp->t_lflag = TTYDEF_LFLAG;
1001 ttychars(tp);
1002 ttsetwater(tp);
1003
1004 /*
1005 * Turn on DTR. We must always do this, even if carrier is not
1006 * present, because otherwise we'd have to use TIOCSDTR
1007 * immediately after setting CLOCAL, which applications do not
1008 * expect. We always assert DTR while the device is open
1009 * unless explicitly requested to deassert it.
1010 */
1011 cztty_modem(sc, 1);
1012 }
1013
1014 splx(s);
1015
1016 error = ttyopen(tp, CZTTY_DIALOUT(dev), ISSET(flags, O_NONBLOCK));
1017 if (error)
1018 goto bad;
1019
1020 error = (*linesw[tp->t_line].l_open)(dev, tp);
1021 if (error)
1022 goto bad;
1023
1024 return (0);
1025
1026 bad:
1027 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
1028 /*
1029 * We failed to open the device, and nobody else had it opened.
1030 * Clean up the state as appropriate.
1031 */
1032 cztty_shutdown(sc);
1033 }
1034
1035 return (error);
1036 }
1037
1038 /*
1039 * czttyclose:
1040 *
1041 * Close a Cyclades-Z serial port.
1042 */
1043 int
1044 czttyclose(dev_t dev, int flags, int mode, struct proc *p)
1045 {
1046 struct cztty_softc *sc = CZTTY_SOFTC(dev);
1047 struct tty *tp = sc->sc_tty;
1048
1049 /* XXX This is for cons.c. */
1050 if (!ISSET(tp->t_state, TS_ISOPEN))
1051 return (0);
1052
1053 (*linesw[tp->t_line].l_close)(tp, flags);
1054 ttyclose(tp);
1055
1056 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
1057 /*
1058 * Although we got a last close, the device may still be in
1059 * use; e.g. if this was the dialout node, and there are still
1060 * processes waiting for carrier on the non-dialout node.
1061 */
1062 cztty_shutdown(sc);
1063 }
1064
1065 return (0);
1066 }
1067
1068 /*
1069 * czttyread:
1070 *
1071 * Read from a Cyclades-Z serial port.
1072 */
1073 int
1074 czttyread(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_read)(tp, uio, flags));
1080 }
1081
1082 /*
1083 * czttywrite:
1084 *
1085 * Write to a Cyclades-Z serial port.
1086 */
1087 int
1088 czttywrite(dev_t dev, struct uio *uio, int flags)
1089 {
1090 struct cztty_softc *sc = CZTTY_SOFTC(dev);
1091 struct tty *tp = sc->sc_tty;
1092
1093 return ((*linesw[tp->t_line].l_write)(tp, uio, flags));
1094 }
1095
1096 /*
1097 * czttyioctl:
1098 *
1099 * Perform a control operation on a Cyclades-Z serial port.
1100 */
1101 int
1102 czttyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1103 {
1104 struct cztty_softc *sc = CZTTY_SOFTC(dev);
1105 struct tty *tp = sc->sc_tty;
1106 int s, error;
1107
1108 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1109 if (error >= 0)
1110 return (error);
1111
1112 error = ttioctl(tp, cmd, data, flag, p);
1113 if (error >= 0)
1114 return (error);
1115
1116 error = 0;
1117
1118 s = spltty();
1119
1120 switch (cmd) {
1121 case TIOCSBRK:
1122 cztty_break(sc, 1);
1123 break;
1124
1125 case TIOCCBRK:
1126 cztty_break(sc, 0);
1127 break;
1128
1129 case TIOCGFLAGS:
1130 *(int *)data = sc->sc_swflags;
1131 break;
1132
1133 case TIOCSFLAGS:
1134 error = suser(p->p_ucred, &p->p_acflag);
1135 if (error)
1136 break;
1137 sc->sc_swflags = *(int *)data;
1138 break;
1139
1140 case TIOCSDTR:
1141 cztty_modem(sc, 1);
1142 break;
1143
1144 case TIOCCDTR:
1145 cztty_modem(sc, 0);
1146 break;
1147
1148 case TIOCMSET:
1149 case TIOCMBIS:
1150 case TIOCMBIC:
1151 tiocm_to_cztty(sc, cmd, *(int *)data);
1152 break;
1153
1154 case TIOCMGET:
1155 *(int *)data = cztty_to_tiocm(sc);
1156 break;
1157
1158 default:
1159 error = ENOTTY;
1160 break;
1161 }
1162
1163 splx(s);
1164
1165 return (error);
1166 }
1167
1168 /*
1169 * cztty_break:
1170 *
1171 * Set or clear BREAK on a port.
1172 */
1173 void
1174 cztty_break(struct cztty_softc *sc, int onoff)
1175 {
1176 struct cz_softc *cz = CZTTY_CZ(sc);
1177
1178 cz_wait_pci_doorbell(cz, "czbreak");
1179
1180 CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1181 CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL,
1182 onoff ? C_CM_SET_BREAK : C_CM_CLR_BREAK);
1183 }
1184
1185 /*
1186 * cztty_modem:
1187 *
1188 * Set or clear DTR on a port.
1189 */
1190 void
1191 cztty_modem(struct cztty_softc *sc, int onoff)
1192 {
1193 struct cz_softc *cz = CZTTY_CZ(sc);
1194
1195 if (sc->sc_rs_control_dtr == 0)
1196 return;
1197
1198 cz_wait_pci_doorbell(cz, "czmod");
1199
1200 if (onoff)
1201 sc->sc_chanctl_rs_control |= sc->sc_rs_control_dtr;
1202 else
1203 sc->sc_chanctl_rs_control &= ~sc->sc_rs_control_dtr;
1204 CZTTY_CHAN_WRITE(sc, CHNCTL_RS_CONTROL, sc->sc_chanctl_rs_control);
1205
1206 CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1207 CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTLM);
1208 }
1209
1210 /*
1211 * tiocm_to_cztty:
1212 *
1213 * Process TIOCM* ioctls.
1214 */
1215 void
1216 tiocm_to_cztty(struct cztty_softc *sc, u_long how, int ttybits)
1217 {
1218 struct cz_softc *cz = CZTTY_CZ(sc);
1219 u_int32_t czttybits;
1220
1221 czttybits = 0;
1222 if (ISSET(ttybits, TIOCM_DTR))
1223 SET(czttybits, C_RS_DTR);
1224 if (ISSET(ttybits, TIOCM_RTS))
1225 SET(czttybits, C_RS_RTS);
1226
1227 cz_wait_pci_doorbell(cz, "cztiocm");
1228
1229 switch (how) {
1230 case TIOCMBIC:
1231 CLR(sc->sc_chanctl_rs_control, czttybits);
1232 break;
1233
1234 case TIOCMBIS:
1235 SET(sc->sc_chanctl_rs_control, czttybits);
1236 break;
1237
1238 case TIOCMSET:
1239 CLR(sc->sc_chanctl_rs_control, C_RS_DTR | C_RS_RTS);
1240 SET(sc->sc_chanctl_rs_control, czttybits);
1241 break;
1242 }
1243
1244 CZTTY_CHAN_WRITE(sc, CHNCTL_RS_CONTROL, sc->sc_chanctl_rs_control);
1245
1246 CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1247 CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTLM);
1248 }
1249
1250 /*
1251 * cztty_to_tiocm:
1252 *
1253 * Process the TIOCMGET ioctl.
1254 */
1255 int
1256 cztty_to_tiocm(struct cztty_softc *sc)
1257 {
1258 struct cz_softc *cz = CZTTY_CZ(sc);
1259 u_int32_t rs_status, op_mode;
1260 int ttybits = 0;
1261
1262 cz_wait_pci_doorbell(cz, "cztty");
1263
1264 rs_status = CZTTY_CHAN_READ(sc, CHNCTL_RS_STATUS);
1265 op_mode = CZTTY_CHAN_READ(sc, CHNCTL_OP_MODE);
1266
1267 if (ISSET(rs_status, C_RS_RTS))
1268 SET(ttybits, TIOCM_RTS);
1269 if (ISSET(rs_status, C_RS_CTS))
1270 SET(ttybits, TIOCM_CTS);
1271 if (ISSET(rs_status, C_RS_DCD))
1272 SET(ttybits, TIOCM_CAR);
1273 if (ISSET(rs_status, C_RS_DTR))
1274 SET(ttybits, TIOCM_DTR);
1275 if (ISSET(rs_status, C_RS_RI))
1276 SET(ttybits, TIOCM_RNG);
1277 if (ISSET(rs_status, C_RS_DSR))
1278 SET(ttybits, TIOCM_DSR);
1279
1280 if (ISSET(op_mode, C_CH_ENABLE))
1281 SET(ttybits, TIOCM_LE);
1282
1283 return (ttybits);
1284 }
1285
1286 /*
1287 * czttyparam:
1288 *
1289 * Set Cyclades-Z serial port parameters from termios.
1290 *
1291 * XXX Should just copy the whole termios after making
1292 * XXX sure all the changes could be done.
1293 */
1294 int
1295 czttyparam(struct tty *tp, struct termios *t)
1296 {
1297 struct cztty_softc *sc = CZTTY_SOFTC(tp->t_dev);
1298 struct cz_softc *cz = CZTTY_CZ(sc);
1299 u_int32_t rs_status;
1300 int ospeed, cflag;
1301
1302 ospeed = t->c_ospeed;
1303 cflag = t->c_cflag;
1304
1305 /* Check requested parameters. */
1306 if (ospeed < 0)
1307 return (EINVAL);
1308 if (t->c_ispeed && t->c_ispeed != ospeed)
1309 return (EINVAL);
1310
1311 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
1312 SET(cflag, CLOCAL);
1313 CLR(cflag, HUPCL);
1314 }
1315
1316 /*
1317 * If there were no changes, don't do anything. This avoids dropping
1318 * input and improves performance when all we did was frob things like
1319 * VMIN and VTIME.
1320 */
1321 if (tp->t_ospeed == ospeed &&
1322 tp->t_cflag == cflag)
1323 return (0);
1324
1325 /* Data bits. */
1326 sc->sc_chanctl_comm_data_l = 0;
1327 switch (t->c_cflag & CSIZE) {
1328 case CS5:
1329 sc->sc_chanctl_comm_data_l |= C_DL_CS5;
1330 break;
1331
1332 case CS6:
1333 sc->sc_chanctl_comm_data_l |= C_DL_CS6;
1334 break;
1335
1336 case CS7:
1337 sc->sc_chanctl_comm_data_l |= C_DL_CS7;
1338 break;
1339
1340 case CS8:
1341 sc->sc_chanctl_comm_data_l |= C_DL_CS8;
1342 break;
1343 }
1344
1345 /* Stop bits. */
1346 if (t->c_cflag & CSTOPB) {
1347 if ((sc->sc_chanctl_comm_data_l & C_DL_CS) == C_DL_CS5)
1348 sc->sc_chanctl_comm_data_l |= C_DL_15STOP;
1349 else
1350 sc->sc_chanctl_comm_data_l |= C_DL_2STOP;
1351 } else
1352 sc->sc_chanctl_comm_data_l |= C_DL_1STOP;
1353
1354 /* Parity. */
1355 if (t->c_cflag & PARENB) {
1356 if (t->c_cflag & PARODD)
1357 sc->sc_chanctl_comm_parity = C_PR_ODD;
1358 else
1359 sc->sc_chanctl_comm_parity = C_PR_EVEN;
1360 } else
1361 sc->sc_chanctl_comm_parity = C_PR_NONE;
1362
1363 /*
1364 * Initialize flow control pins depending on the current flow control
1365 * mode.
1366 */
1367 if (ISSET(t->c_cflag, CRTSCTS)) {
1368 sc->sc_rs_control_dtr = C_RS_DTR;
1369 sc->sc_chanctl_hw_flow = C_RS_CTS | C_RS_RTS;
1370 } else if (ISSET(t->c_cflag, MDMBUF)) {
1371 sc->sc_rs_control_dtr = 0;
1372 sc->sc_chanctl_hw_flow = C_RS_DCD | C_RS_DTR;
1373 } else {
1374 /*
1375 * If no flow control, then always set RTS. This will make
1376 * the other side happy if it mistakenly thinks we're doing
1377 * RTS/CTS flow control.
1378 */
1379 sc->sc_rs_control_dtr = C_RS_DTR | C_RS_RTS;
1380 sc->sc_chanctl_hw_flow = 0;
1381 if (ISSET(sc->sc_chanctl_rs_control, C_RS_DTR))
1382 SET(sc->sc_chanctl_rs_control, C_RS_RTS);
1383 else
1384 CLR(sc->sc_chanctl_rs_control, C_RS_RTS);
1385 }
1386
1387 /* Baud rate. */
1388 sc->sc_chanctl_comm_baud = ospeed;
1389
1390 /* Copy to tty. */
1391 tp->t_ispeed = 0;
1392 tp->t_ospeed = t->c_ospeed;
1393 tp->t_cflag = t->c_cflag;
1394
1395 /*
1396 * Now load the channel control structure.
1397 */
1398
1399 cz_wait_pci_doorbell(cz, "czparam");
1400
1401 CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_BAUD, sc->sc_chanctl_comm_baud);
1402 CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_DATA_L, sc->sc_chanctl_comm_data_l);
1403 CZTTY_CHAN_WRITE(sc, CHNCTL_COMM_PARITY, sc->sc_chanctl_comm_parity);
1404 CZTTY_CHAN_WRITE(sc, CHNCTL_HW_FLOW, sc->sc_chanctl_hw_flow);
1405 CZTTY_CHAN_WRITE(sc, CHNCTL_RS_CONTROL, sc->sc_chanctl_rs_control);
1406
1407 #ifdef CZ_DEBUG
1408 printf(
1409 "cz0: baud %d data %x parity %x\n\thwflow %x rscont %x dtrflg %x\n",
1410 sc->sc_chanctl_comm_baud, sc->sc_chanctl_comm_data_l,
1411 sc->sc_chanctl_comm_parity, sc->sc_chanctl_hw_flow,
1412 sc->sc_chanctl_rs_control, sc->sc_rs_control_dtr);
1413 #endif
1414
1415 CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1416 CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTL);
1417
1418 cz_wait_pci_doorbell(cz, "czparam");
1419
1420 CZ_FWCTL_WRITE(cz, BRDCTL_HCMD_CHANNEL, sc->sc_channel);
1421 CZ_PLX_WRITE(cz, PLX_PCI_LOCAL_DOORBELL, C_CM_IOCTLM);
1422
1423 cz_wait_pci_doorbell(cz, "czparam");
1424
1425 /*
1426 * Update the tty layer's idea of the carrier bit, in case we changed
1427 * CLOCAL. We don't hang up here; we only do that by explicit
1428 * request.
1429 */
1430 rs_status = CZTTY_CHAN_READ(sc, CHNCTL_RS_STATUS);
1431 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(rs_status, C_RS_DCD));
1432
1433 return (0);
1434 }
1435
1436 /*
1437 * czttystart:
1438 *
1439 * Start or restart transmission.
1440 */
1441 void
1442 czttystart(struct tty *tp)
1443 {
1444 struct cztty_softc *sc = CZTTY_SOFTC(tp->t_dev);
1445 int s;
1446
1447 s = spltty();
1448 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1449 goto out;
1450
1451 if (tp->t_outq.c_cc <= tp->t_lowat) {
1452 if (ISSET(tp->t_state, TS_ASLEEP)) {
1453 CLR(tp->t_state, TS_ASLEEP);
1454 wakeup(&tp->t_outq);
1455 }
1456 selwakeup(&tp->t_wsel);
1457 if (tp->t_outq.c_cc == 0)
1458 goto out;
1459 }
1460
1461 cztty_transmit(sc, tp);
1462 out:
1463 splx(s);
1464 }
1465
1466 /*
1467 * czttystop:
1468 *
1469 * Stop output, e.g., for ^S or output flush.
1470 */
1471 void
1472 czttystop(struct tty *tp, int flag)
1473 {
1474
1475 /*
1476 * XXX We don't do anything here, yet. Mostly, I don't know
1477 * XXX exactly how this should be implemented on this device.
1478 * XXX We've given a big chunk of data to the MIPS already,
1479 * XXX and I don't know how we request the MIPS to stop sending
1480 * XXX the data. So, punt for now. --thorpej
1481 */
1482 }
1483
1484 /*
1485 * cztty_diag:
1486 *
1487 * Issue a scheduled diagnostic message.
1488 */
1489 void
1490 cztty_diag(void *arg)
1491 {
1492 struct cztty_softc *sc = arg;
1493 struct cz_softc *cz = CZTTY_CZ(sc);
1494 u_int overflows, parity_errors, framing_errors;
1495 int s;
1496
1497 s = spltty();
1498
1499 overflows = sc->sc_overflows;
1500 sc->sc_overflows = 0;
1501
1502 parity_errors = sc->sc_parity_errors;
1503 sc->sc_parity_errors = 0;
1504
1505 framing_errors = sc->sc_framing_errors;
1506 sc->sc_framing_errors = 0;
1507
1508 sc->sc_errors = 0;
1509
1510 splx(s);
1511
1512 log(LOG_WARNING,
1513 "%s: channel %d: %u overflow%s, %u parity, %u framing error%s\n",
1514 cz->cz_dev.dv_xname, sc->sc_channel,
1515 overflows, overflows == 1 ? "" : "s",
1516 parity_errors,
1517 framing_errors, framing_errors == 1 ? "" : "s");
1518 }
1519
1520 /*
1521 * tx and rx ring buffer size macros:
1522 *
1523 * The transmitter and receiver both use ring buffers. For each one, there
1524 * is a get (consumer) and a put (producer) offset. The get value is the
1525 * next byte to be read from the ring, and the put is the next one to be
1526 * put into the ring. get == put means the ring is empty.
1527 *
1528 * For each ring, the firmware controls one of (get, put) and this driver
1529 * controls the other. For transmission, this driver updates put to point
1530 * past the valid data, and the firmware moves get as bytes are sent. Likewise
1531 * for receive, the driver controls put, and this driver controls get.
1532 */
1533 #define TX_MOVEABLE(g, p, s) (((g) > (p)) ? ((g) - (p) - 1) : ((s) - (p)))
1534 #define RX_MOVEABLE(g, p, s) (((g) > (p)) ? ((s) - (g)) : ((p) - (g)))
1535
1536 /*
1537 * cztty_transmit()
1538 *
1539 * Look at the tty for this port and start sending.
1540 */
1541 int
1542 cztty_transmit(struct cztty_softc *sc, struct tty *tp)
1543 {
1544 struct cz_softc *cz = CZTTY_CZ(sc);
1545 u_int move, get, put, size, address;
1546 #ifdef HOSTRAMCODE
1547 int error, done = 0;
1548 #else
1549 int done = 0;
1550 #endif
1551
1552 size = CZTTY_BUF_READ(sc, BUFCTL_TX_BUFSIZE);
1553 get = CZTTY_BUF_READ(sc, BUFCTL_TX_GET);
1554 put = CZTTY_BUF_READ(sc, BUFCTL_TX_PUT);
1555 address = CZTTY_BUF_READ(sc, BUFCTL_TX_BUFADDR);
1556
1557 while ((tp->t_outq.c_cc > 0) && ((move = TX_MOVEABLE(get, put, size)))){
1558 #ifdef HOSTRAMCODE
1559 if (0) {
1560 move = min(tp->t_outq.c_cc, move);
1561 error = q_to_b(&tp->t_outq, 0, move);
1562 if (error != move) {
1563 printf("%s: channel %d: error moving to "
1564 "transmit buf\n", cz->cz_dev.dv_xname,
1565 sc->sc_channel);
1566 move = error;
1567 }
1568 } else {
1569 #endif
1570 move = min(ndqb(&tp->t_outq, 0), move);
1571 bus_space_write_region_1(cz->cz_win_st, cz->cz_win_sh,
1572 address + put, tp->t_outq.c_cf, move);
1573 ndflush(&tp->t_outq, move);
1574 #ifdef HOSTRAMCODE
1575 }
1576 #endif
1577
1578 put = ((put + move) % size);
1579 done = 1;
1580 }
1581 if (done) {
1582 CZTTY_BUF_WRITE(sc, BUFCTL_TX_PUT, put);
1583 }
1584 return (done);
1585 }
1586
1587 int
1588 cztty_receive(struct cztty_softc *sc, struct tty *tp)
1589 {
1590 struct cz_softc *cz = CZTTY_CZ(sc);
1591 u_int get, put, size, address;
1592 int done = 0, ch;
1593
1594 size = CZTTY_BUF_READ(sc, BUFCTL_RX_BUFSIZE);
1595 get = CZTTY_BUF_READ(sc, BUFCTL_RX_GET);
1596 put = CZTTY_BUF_READ(sc, BUFCTL_RX_PUT);
1597 address = CZTTY_BUF_READ(sc, BUFCTL_RX_BUFADDR);
1598
1599 while ((get != put) && ((tp->t_canq.c_cc + tp->t_rawq.c_cc) < tp->t_hiwat)) {
1600 #ifdef HOSTRAMCODE
1601 if (hostram)
1602 ch = ((char *)fifoaddr)[get];
1603 } else {
1604 #endif
1605 ch = bus_space_read_1(cz->cz_win_st, cz->cz_win_sh,
1606 address + get);
1607 #ifdef HOSTRAMCODE
1608 }
1609 #endif
1610 (*linesw[tp->t_line].l_rint)(ch, tp);
1611 get = (get + 1) % size;
1612 done = 1;
1613 }
1614 if (done) {
1615 CZTTY_BUF_WRITE(sc, BUFCTL_RX_GET, get);
1616 }
1617 return (done);
1618 }
1619